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