Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2006 |
| 3 | * Heiko Schocher, DENX Software Engineering, hs@denx.de |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * Altera FPGA configuration support for the ALPR computer from prodrive |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <altera.h> |
| 14 | #include <ACEX1K.h> |
| 15 | #include <command.h> |
Peter Tyser | 61f2b38 | 2010-04-12 22:28:07 -0500 | [diff] [blame] | 16 | #include <asm/processor.h> |
Stefan Roese | b36df56 | 2010-09-09 19:18:00 +0200 | [diff] [blame] | 17 | #include <asm/ppc440.h> |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 18 | #include "fpga.h" |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 22 | #if defined(CONFIG_FPGA) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 23 | |
| 24 | #ifdef FPGA_DEBUG |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 25 | #define PRINTF(fmt, args...) printf(fmt , ##args) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 26 | #else |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 27 | #define PRINTF(fmt, args...) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 30 | static unsigned long regval; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 31 | |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 32 | #define SET_GPIO_REG_0(reg, bit) do { \ |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 33 | regval = in32(reg); \ |
| 34 | regval &= ~(0x80000000 >> bit); \ |
| 35 | out32(reg, regval); \ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 36 | } while (0) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 37 | |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 38 | #define SET_GPIO_REG_1(reg, bit) do { \ |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 39 | regval = in32(reg); \ |
| 40 | regval |= (0x80000000 >> bit); \ |
| 41 | out32(reg, regval); \ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 42 | } while (0) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 43 | |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 44 | #define SET_GPIO_0(bit) SET_GPIO_REG_0(GPIO0_OR, bit) |
| 45 | #define SET_GPIO_1(bit) SET_GPIO_REG_1(GPIO0_OR, bit) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 46 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | #define FPGA_PRG (0x80000000 >> CONFIG_SYS_GPIO_PROG_EN) |
| 48 | #define FPGA_CONFIG (0x80000000 >> CONFIG_SYS_GPIO_CONFIG) |
| 49 | #define FPGA_DATA (0x80000000 >> CONFIG_SYS_GPIO_DATA) |
| 50 | #define FPGA_CLK (0x80000000 >> CONFIG_SYS_GPIO_CLK) |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 51 | #define OLD_VAL (FPGA_PRG | FPGA_CONFIG) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 52 | |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 53 | #define SET_FPGA(data) out32(GPIO0_OR, data) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 54 | |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 55 | #define FPGA_WRITE_1 do { \ |
| 56 | SET_FPGA(OLD_VAL | 0 | FPGA_DATA); /* set data to 1 */ \ |
| 57 | SET_FPGA(OLD_VAL | FPGA_CLK | FPGA_DATA); /* set data to 1 */ \ |
| 58 | } while (0) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 59 | |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 60 | #define FPGA_WRITE_0 do { \ |
| 61 | SET_FPGA(OLD_VAL | 0 | 0); /* set data to 0 */ \ |
| 62 | SET_FPGA(OLD_VAL | FPGA_CLK | 0); /* set data to 1 */ \ |
| 63 | } while (0) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 64 | |
| 65 | /* Plattforminitializations */ |
| 66 | /* Here we have to set the FPGA Chain */ |
| 67 | /* PROGRAM_PROG_EN = HIGH */ |
| 68 | /* PROGRAM_SEL_DPR = LOW */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 69 | int fpga_pre_fn(int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 70 | { |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 71 | /* Enable the FPGA Chain */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 72 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_PROG_EN); |
| 73 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_PROG_EN); |
| 74 | SET_GPIO_1(CONFIG_SYS_GPIO_PROG_EN); |
| 75 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_SEL_DPR); |
| 76 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_SEL_DPR); |
| 77 | SET_GPIO_0((CONFIG_SYS_GPIO_SEL_DPR)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 78 | |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 79 | /* initialize the GPIO Pins */ |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 80 | /* output */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | SET_GPIO_0(CONFIG_SYS_GPIO_CLK); |
| 82 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CLK); |
| 83 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CLK); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 84 | |
| 85 | /* output */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 86 | SET_GPIO_0(CONFIG_SYS_GPIO_DATA); |
| 87 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_DATA); |
| 88 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_DATA); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 89 | |
| 90 | /* First we set STATUS to 0 then as an input */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 91 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS); |
| 92 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS); |
| 93 | SET_GPIO_0(CONFIG_SYS_GPIO_STATUS); |
| 94 | SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS); |
| 95 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 96 | |
| 97 | /* output */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 98 | SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CONFIG); |
| 99 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CONFIG); |
| 100 | SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 101 | |
| 102 | /* input */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 103 | SET_GPIO_0(CONFIG_SYS_GPIO_CON_DON); |
| 104 | SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_CON_DON); |
| 105 | SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CON_DON); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 106 | |
| 107 | /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 108 | SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 109 | return FPGA_SUCCESS; |
| 110 | } |
| 111 | |
| 112 | /* Set the state of CONFIG Pin */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 113 | int fpga_config_fn(int assert_config, int flush, int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 114 | { |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 115 | if (assert_config) |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 116 | SET_GPIO_1(CONFIG_SYS_GPIO_CONFIG); |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 117 | else |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 118 | SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG); |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 119 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 120 | return FPGA_SUCCESS; |
| 121 | } |
| 122 | |
| 123 | /* Returns the state of STATUS Pin */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 124 | int fpga_status_fn(int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 125 | { |
| 126 | unsigned long reg; |
| 127 | |
| 128 | reg = in32(GPIO0_IR); |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 129 | if (reg & (0x80000000 >> CONFIG_SYS_GPIO_STATUS)) { |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 130 | PRINTF("STATUS = HIGH\n"); |
| 131 | return FPGA_FAIL; |
| 132 | } |
| 133 | PRINTF("STATUS = LOW\n"); |
| 134 | return FPGA_SUCCESS; |
| 135 | } |
| 136 | |
| 137 | /* Returns the state of CONF_DONE Pin */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 138 | int fpga_done_fn(int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 139 | { |
| 140 | unsigned long reg; |
| 141 | reg = in32(GPIO0_IR); |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 142 | if (reg & (0x80000000 >> CONFIG_SYS_GPIO_CON_DON)) { |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 143 | PRINTF("CONF_DON = HIGH\n"); |
| 144 | return FPGA_FAIL; |
| 145 | } |
| 146 | PRINTF("CONF_DON = LOW\n"); |
| 147 | return FPGA_SUCCESS; |
| 148 | } |
| 149 | |
| 150 | /* writes the complete buffer to the FPGA |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 151 | writing the complete buffer in one function is much faster, |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 152 | then calling it for every bit */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 153 | int fpga_write_fn(const void *buf, size_t len, int flush, int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 154 | { |
| 155 | size_t bytecount = 0; |
| 156 | unsigned char *data = (unsigned char *) buf; |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 157 | unsigned char val = 0; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 158 | int i; |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 159 | int len_40 = len / 40; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 160 | |
| 161 | while (bytecount < len) { |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 162 | val = data[bytecount++]; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 163 | i = 8; |
| 164 | do { |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 165 | if (val & 0x01) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 166 | FPGA_WRITE_1; |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 167 | else |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 168 | FPGA_WRITE_0; |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 169 | |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 170 | val >>= 1; |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 171 | i--; |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 172 | } while (i > 0); |
| 173 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 174 | #ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 175 | if (bytecount % len_40 == 0) { |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 176 | putc('.'); /* let them know we are alive */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_SYS_FPGA_CHECK_CTRLC |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 178 | if (ctrlc()) |
Stefan Roese | 1c2ce22 | 2006-11-27 14:12:17 +0100 | [diff] [blame] | 179 | return FPGA_FAIL; |
| 180 | #endif |
| 181 | } |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 182 | #endif |
| 183 | } |
| 184 | return FPGA_SUCCESS; |
| 185 | } |
| 186 | |
| 187 | /* called, when programming is aborted */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 188 | int fpga_abort_fn(int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 189 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 190 | SET_GPIO_1((CONFIG_SYS_GPIO_SEL_DPR)); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 191 | return FPGA_SUCCESS; |
| 192 | } |
| 193 | |
| 194 | /* called, when programming was succesful */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 195 | int fpga_post_fn(int cookie) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 196 | { |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 197 | return fpga_abort_fn(cookie); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* Note that these are pointers to code that is in Flash. They will be |
| 201 | * relocated at runtime. |
| 202 | */ |
| 203 | Altera_CYC2_Passive_Serial_fns fpga_fns = { |
| 204 | fpga_pre_fn, |
| 205 | fpga_config_fn, |
| 206 | fpga_status_fn, |
| 207 | fpga_done_fn, |
| 208 | fpga_write_fn, |
| 209 | fpga_abort_fn, |
| 210 | fpga_post_fn |
| 211 | }; |
| 212 | |
| 213 | Altera_desc fpga[CONFIG_FPGA_COUNT] = { |
| 214 | {Altera_CYC2, |
| 215 | passive_serial, |
| 216 | Altera_EP2C35_SIZE, |
| 217 | (void *) &fpga_fns, |
| 218 | NULL, |
| 219 | 0} |
| 220 | }; |
| 221 | |
| 222 | /* |
| 223 | * Initialize the fpga. Return 1 on success, 0 on failure. |
| 224 | */ |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 225 | int alpr_fpga_init(void) |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 226 | { |
| 227 | int i; |
| 228 | |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 229 | PRINTF("%s:%d: Initialize FPGA interface\n", __func__, __LINE__); |
| 230 | fpga_init(); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 231 | |
| 232 | for (i = 0; i < CONFIG_FPGA_COUNT; i++) { |
Wolfgang Denk | 8298fd2 | 2011-09-05 12:52:21 +0200 | [diff] [blame] | 233 | PRINTF("%s:%d: Adding fpga %d\n", __func__, __LINE__, i); |
| 234 | fpga_add(fpga_altera, &fpga[i]); |
Stefan Roese | 899620c | 2006-08-15 14:22:35 +0200 | [diff] [blame] | 235 | } |
| 236 | return 1; |
| 237 | } |
| 238 | |
| 239 | #endif |