blob: e7686ad27b52b3b6870d72059537c36b30ffcb9b [file] [log] [blame]
Stefan Roese899620c2006-08-15 14:22:35 +02001/*
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 Tyser61f2b382010-04-12 22:28:07 -050033#include <asm/processor.h>
Stefan Roeseb36df562010-09-09 19:18:00 +020034#include <asm/ppc440.h>
Stefan Roese899620c2006-08-15 14:22:35 +020035#include "fpga.h"
36
37DECLARE_GLOBAL_DATA_PTR;
38
Stefan Roese1c2ce222006-11-27 14:12:17 +010039#if defined(CONFIG_FPGA)
Stefan Roese899620c2006-08-15 14:22:35 +020040
41#ifdef FPGA_DEBUG
Wolfgang Denk8298fd22011-09-05 12:52:21 +020042#define PRINTF(fmt, args...) printf(fmt , ##args)
Stefan Roese899620c2006-08-15 14:22:35 +020043#else
Wolfgang Denk8298fd22011-09-05 12:52:21 +020044#define PRINTF(fmt, args...)
Stefan Roese899620c2006-08-15 14:22:35 +020045#endif
46
Stefan Roese1c2ce222006-11-27 14:12:17 +010047static unsigned long regval;
Stefan Roese899620c2006-08-15 14:22:35 +020048
Wolfgang Denk8298fd22011-09-05 12:52:21 +020049#define SET_GPIO_REG_0(reg, bit) do { \
Stefan Roese1c2ce222006-11-27 14:12:17 +010050 regval = in32(reg); \
51 regval &= ~(0x80000000 >> bit); \
52 out32(reg, regval); \
Wolfgang Denk8298fd22011-09-05 12:52:21 +020053 } while (0)
Stefan Roese899620c2006-08-15 14:22:35 +020054
Wolfgang Denk8298fd22011-09-05 12:52:21 +020055#define SET_GPIO_REG_1(reg, bit) do { \
Stefan Roese1c2ce222006-11-27 14:12:17 +010056 regval = in32(reg); \
57 regval |= (0x80000000 >> bit); \
58 out32(reg, regval); \
Wolfgang Denk8298fd22011-09-05 12:52:21 +020059 } while (0)
Stefan Roese899620c2006-08-15 14:22:35 +020060
Stefan Roese1c2ce222006-11-27 14:12:17 +010061#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 Roese899620c2006-08-15 14:22:35 +020063
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020064#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 Roese1c2ce222006-11-27 14:12:17 +010068#define OLD_VAL (FPGA_PRG | FPGA_CONFIG)
Stefan Roese899620c2006-08-15 14:22:35 +020069
Stefan Roese1c2ce222006-11-27 14:12:17 +010070#define SET_FPGA(data) out32(GPIO0_OR, data)
Stefan Roese899620c2006-08-15 14:22:35 +020071
Wolfgang Denk8298fd22011-09-05 12:52:21 +020072#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 Roese899620c2006-08-15 14:22:35 +020076
Wolfgang Denk8298fd22011-09-05 12:52:21 +020077#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 Roese899620c2006-08-15 14:22:35 +020081
82/* Plattforminitializations */
83/* Here we have to set the FPGA Chain */
84/* PROGRAM_PROG_EN = HIGH */
85/* PROGRAM_SEL_DPR = LOW */
Wolfgang Denk8298fd22011-09-05 12:52:21 +020086int fpga_pre_fn(int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +020087{
88 unsigned long reg;
89
90 reg = in32(GPIO0_IR);
91 /* Enable the FPGA Chain */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020092 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_PROG_EN);
93 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_PROG_EN);
94 SET_GPIO_1(CONFIG_SYS_GPIO_PROG_EN);
95 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_SEL_DPR);
96 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_SEL_DPR);
97 SET_GPIO_0((CONFIG_SYS_GPIO_SEL_DPR));
Stefan Roese899620c2006-08-15 14:22:35 +020098
Stefan Roese1c2ce222006-11-27 14:12:17 +010099 /* initialize the GPIO Pins */
Stefan Roese899620c2006-08-15 14:22:35 +0200100 /* output */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200101 SET_GPIO_0(CONFIG_SYS_GPIO_CLK);
102 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CLK);
103 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CLK);
Stefan Roese899620c2006-08-15 14:22:35 +0200104
105 /* output */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200106 SET_GPIO_0(CONFIG_SYS_GPIO_DATA);
107 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_DATA);
108 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_DATA);
Stefan Roese899620c2006-08-15 14:22:35 +0200109
110 /* First we set STATUS to 0 then as an input */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200111 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
112 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
113 SET_GPIO_0(CONFIG_SYS_GPIO_STATUS);
114 SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_STATUS);
115 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_STATUS);
Stefan Roese899620c2006-08-15 14:22:35 +0200116
117 /* output */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200118 SET_GPIO_REG_1(GPIO0_TCR, CONFIG_SYS_GPIO_CONFIG);
119 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CONFIG);
120 SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
Stefan Roese899620c2006-08-15 14:22:35 +0200121
122 /* input */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200123 SET_GPIO_0(CONFIG_SYS_GPIO_CON_DON);
124 SET_GPIO_REG_0(GPIO0_TCR, CONFIG_SYS_GPIO_CON_DON);
125 SET_GPIO_REG_0(GPIO0_ODR, CONFIG_SYS_GPIO_CON_DON);
Stefan Roese899620c2006-08-15 14:22:35 +0200126
127 /* CONFIG = 0 STATUS = 0 -> FPGA in reset state */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200128 SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
Stefan Roese899620c2006-08-15 14:22:35 +0200129 return FPGA_SUCCESS;
130}
131
132/* Set the state of CONFIG Pin */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200133int fpga_config_fn(int assert_config, int flush, int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200134{
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200135 if (assert_config)
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200136 SET_GPIO_1(CONFIG_SYS_GPIO_CONFIG);
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200137 else
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200138 SET_GPIO_0(CONFIG_SYS_GPIO_CONFIG);
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200139
Stefan Roese899620c2006-08-15 14:22:35 +0200140 return FPGA_SUCCESS;
141}
142
143/* Returns the state of STATUS Pin */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200144int fpga_status_fn(int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200145{
146 unsigned long reg;
147
148 reg = in32(GPIO0_IR);
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200149 if (reg & (0x80000000 >> CONFIG_SYS_GPIO_STATUS)) {
Stefan Roese899620c2006-08-15 14:22:35 +0200150 PRINTF("STATUS = HIGH\n");
151 return FPGA_FAIL;
152 }
153 PRINTF("STATUS = LOW\n");
154 return FPGA_SUCCESS;
155}
156
157/* Returns the state of CONF_DONE Pin */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200158int fpga_done_fn(int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200159{
160 unsigned long reg;
161 reg = in32(GPIO0_IR);
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200162 if (reg & (0x80000000 >> CONFIG_SYS_GPIO_CON_DON)) {
Stefan Roese899620c2006-08-15 14:22:35 +0200163 PRINTF("CONF_DON = HIGH\n");
164 return FPGA_FAIL;
165 }
166 PRINTF("CONF_DON = LOW\n");
167 return FPGA_SUCCESS;
168}
169
170/* writes the complete buffer to the FPGA
Stefan Roese1c2ce222006-11-27 14:12:17 +0100171 writing the complete buffer in one function is much faster,
Stefan Roese899620c2006-08-15 14:22:35 +0200172 then calling it for every bit */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200173int fpga_write_fn(const void *buf, size_t len, int flush, int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200174{
175 size_t bytecount = 0;
176 unsigned char *data = (unsigned char *) buf;
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200177 unsigned char val = 0;
Stefan Roese899620c2006-08-15 14:22:35 +0200178 int i;
Stefan Roese1c2ce222006-11-27 14:12:17 +0100179 int len_40 = len / 40;
Stefan Roese899620c2006-08-15 14:22:35 +0200180
181 while (bytecount < len) {
Stefan Roese1c2ce222006-11-27 14:12:17 +0100182 val = data[bytecount++];
Stefan Roese899620c2006-08-15 14:22:35 +0200183 i = 8;
184 do {
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200185 if (val & 0x01)
Stefan Roese899620c2006-08-15 14:22:35 +0200186 FPGA_WRITE_1;
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200187 else
Stefan Roese899620c2006-08-15 14:22:35 +0200188 FPGA_WRITE_0;
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200189
Stefan Roese899620c2006-08-15 14:22:35 +0200190 val >>= 1;
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200191 i--;
Stefan Roese899620c2006-08-15 14:22:35 +0200192 } while (i > 0);
193
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200194#ifdef CONFIG_SYS_FPGA_PROG_FEEDBACK
Stefan Roese1c2ce222006-11-27 14:12:17 +0100195 if (bytecount % len_40 == 0) {
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200196 putc('.'); /* let them know we are alive */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200197#ifdef CONFIG_SYS_FPGA_CHECK_CTRLC
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200198 if (ctrlc())
Stefan Roese1c2ce222006-11-27 14:12:17 +0100199 return FPGA_FAIL;
200#endif
201 }
Stefan Roese899620c2006-08-15 14:22:35 +0200202#endif
203 }
204 return FPGA_SUCCESS;
205}
206
207/* called, when programming is aborted */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200208int fpga_abort_fn(int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200209{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200210 SET_GPIO_1((CONFIG_SYS_GPIO_SEL_DPR));
Stefan Roese899620c2006-08-15 14:22:35 +0200211 return FPGA_SUCCESS;
212}
213
214/* called, when programming was succesful */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200215int fpga_post_fn(int cookie)
Stefan Roese899620c2006-08-15 14:22:35 +0200216{
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200217 return fpga_abort_fn(cookie);
Stefan Roese899620c2006-08-15 14:22:35 +0200218}
219
220/* Note that these are pointers to code that is in Flash. They will be
221 * relocated at runtime.
222 */
223Altera_CYC2_Passive_Serial_fns fpga_fns = {
224 fpga_pre_fn,
225 fpga_config_fn,
226 fpga_status_fn,
227 fpga_done_fn,
228 fpga_write_fn,
229 fpga_abort_fn,
230 fpga_post_fn
231};
232
233Altera_desc fpga[CONFIG_FPGA_COUNT] = {
234 {Altera_CYC2,
235 passive_serial,
236 Altera_EP2C35_SIZE,
237 (void *) &fpga_fns,
238 NULL,
239 0}
240};
241
242/*
243 * Initialize the fpga. Return 1 on success, 0 on failure.
244 */
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200245int alpr_fpga_init(void)
Stefan Roese899620c2006-08-15 14:22:35 +0200246{
247 int i;
248
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200249 PRINTF("%s:%d: Initialize FPGA interface\n", __func__, __LINE__);
250 fpga_init();
Stefan Roese899620c2006-08-15 14:22:35 +0200251
252 for (i = 0; i < CONFIG_FPGA_COUNT; i++) {
Wolfgang Denk8298fd22011-09-05 12:52:21 +0200253 PRINTF("%s:%d: Adding fpga %d\n", __func__, __LINE__, i);
254 fpga_add(fpga_altera, &fpga[i]);
Stefan Roese899620c2006-08-15 14:22:35 +0200255 }
256 return 1;
257}
258
259#endif