blob: 03bf9c8fe5ce6dafa851135e5d8a1324fb085d91 [file] [log] [blame]
stroesee1cb7eb2003-09-12 08:41:56 +00001/*
2 * (C) Copyright 2001-2003
3 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
stroesee1cb7eb2003-09-12 08:41:56 +00006 */
7
8#include <common.h>
9#include <asm/processor.h>
Matthias Fuchsc8452fa2007-07-09 10:10:06 +020010#include <asm/io.h>
stroesee1cb7eb2003-09-12 08:41:56 +000011#include <command.h>
12#include <malloc.h>
Matthias Fuchsd1c60452009-10-26 09:58:45 +010013#include <sja1000.h>
stroesee1cb7eb2003-09-12 08:41:56 +000014
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +020015#undef FPGA_DEBUG
stroesee1cb7eb2003-09-12 08:41:56 +000016
Matthias Fuchsc8452fa2007-07-09 10:10:06 +020017DECLARE_GLOBAL_DATA_PTR;
18
stroese02ca1112004-12-16 18:39:03 +000019extern void lxt971_no_sleep(void);
stroesee1cb7eb2003-09-12 08:41:56 +000020
21/* fpga configuration data - gzip compressed and generated by bin2c */
22const unsigned char fpgadata[] =
23{
24#include "fpgadata.c"
25};
26
27/*
28 * include common fpga code (for esd boards)
29 */
30#include "../common/fpga.c"
31
Matthias Fuchsecf9a2e2010-02-01 13:53:59 +010032/*
33 * generate a short spike on the CAN tx line
34 * to bring the couplers in sync
35 */
36void init_coupler(u32 addr)
37{
38 struct sja1000_basic_s *ctrl = (struct sja1000_basic_s *)addr;
39
40 /* reset */
41 out_8(&ctrl->cr, CR_RR);
42
43 /* dominant */
44 out_8(&ctrl->btr0, 0x00); /* btr setup is required */
45 out_8(&ctrl->btr1, 0x14); /* we use 1Mbit/s */
46 out_8(&ctrl->oc, OC_TP1 | OC_TN1 | OC_POL1 |
47 OC_TP0 | OC_TN0 | OC_POL0 | OC_MODE1);
48 out_8(&ctrl->cr, 0x00);
49
50 /* delay */
51 in_8(&ctrl->cr);
52 in_8(&ctrl->cr);
53 in_8(&ctrl->cr);
54 in_8(&ctrl->cr);
55
56 /* reset */
57 out_8(&ctrl->cr, CR_RR);
58}
59
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +020060int board_early_init_f(void)
stroesee1cb7eb2003-09-12 08:41:56 +000061{
62 /*
63 * IRQ 0-15 405GP internally generated; active high; level sensitive
64 * IRQ 16 405GP internally generated; active low; level sensitive
65 * IRQ 17-24 RESERVED
66 * IRQ 25 (EXT IRQ 0) CAN0; active low; level sensitive
67 * IRQ 26 (EXT IRQ 1) SER0 ; active low; level sensitive
68 * IRQ 27 (EXT IRQ 2) SER1; active low; level sensitive
69 * IRQ 28 (EXT IRQ 3) FPGA 0; active low; level sensitive
70 * IRQ 29 (EXT IRQ 4) FPGA 1; active low; level sensitive
71 * IRQ 30 (EXT IRQ 5) PCI INTA; active low; level sensitive
72 * IRQ 31 (EXT IRQ 6) COMPACT FLASH; active high; level sensitive
73 */
Stefan Roese707fd362009-09-24 09:55:50 +020074 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
75 mtdcr(UIC0ER, 0x00000000); /* disable all ints */
76 mtdcr(UIC0CR, 0x00000000); /* set all to be non-critical*/
77 mtdcr(UIC0PR, 0xFFFFFF99); /* set int polarities */
78 mtdcr(UIC0TR, 0x10000000); /* set int trigger levels */
79 mtdcr(UIC0VCR, 0x00000001); /* set vect base=0,INT0 highest prio */
80 mtdcr(UIC0SR, 0xFFFFFFFF); /* clear all ints */
stroesee1cb7eb2003-09-12 08:41:56 +000081
82 /*
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +020083 * EBC Configuration Register: set ready timeout to
84 * 512 ebc-clks -> ca. 15 us
stroesee1cb7eb2003-09-12 08:41:56 +000085 */
Stefan Roese918010a2009-09-09 16:25:29 +020086 mtebc(EBC0_CFG, 0xa8400000); /* ebc always driven */
stroesee1cb7eb2003-09-12 08:41:56 +000087
88 return 0;
89}
90
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +020091int misc_init_r(void)
stroesee1cb7eb2003-09-12 08:41:56 +000092{
stroesee1cb7eb2003-09-12 08:41:56 +000093 unsigned char *dst;
Matthias Fuchseb89df72009-01-02 12:16:35 +010094 unsigned char fctr;
stroesee1cb7eb2003-09-12 08:41:56 +000095 ulong len = sizeof(fpgadata);
96 int status;
97 int index;
98 int i;
99
Matthias Fuchsc8452fa2007-07-09 10:10:06 +0200100 /* adjust flash start and offset */
101 gd->bd->bi_flashstart = 0 - gd->bd->bi_flashsize;
102 gd->bd->bi_flashoffset = 0;
103
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200104 dst = malloc(CONFIG_SYS_FPGA_MAX_SIZE);
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200105 if (gunzip(dst, CONFIG_SYS_FPGA_MAX_SIZE,
106 (uchar *)fpgadata, &len) != 0) {
107 printf("GUNZIP ERROR - must RESET board to recover\n");
108 do_reset(NULL, 0, 0, NULL);
stroesee1cb7eb2003-09-12 08:41:56 +0000109 }
110
111 status = fpga_boot(dst, len);
112 if (status != 0) {
113 printf("\nFPGA: Booting failed ");
114 switch (status) {
115 case ERROR_FPGA_PRG_INIT_LOW:
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200116 printf("(Timeout: INIT not low "
117 "after asserting PROGRAM*)\n");
stroesee1cb7eb2003-09-12 08:41:56 +0000118 break;
119 case ERROR_FPGA_PRG_INIT_HIGH:
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200120 printf("(Timeout: INIT not high "
121 "after deasserting PROGRAM*)\n");
stroesee1cb7eb2003-09-12 08:41:56 +0000122 break;
123 case ERROR_FPGA_PRG_DONE:
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200124 printf("(Timeout: DONE not high "
125 "after programming FPGA)\n");
stroesee1cb7eb2003-09-12 08:41:56 +0000126 break;
127 }
128
129 /* display infos on fpgaimage */
130 index = 15;
131 for (i=0; i<4; i++) {
132 len = dst[index];
133 printf("FPGA: %s\n", &(dst[index+1]));
134 index += len+3;
135 }
136 putc ('\n');
137 /* delayed reboot */
138 for (i=20; i>0; i--) {
139 printf("Rebooting in %2d seconds \r",i);
140 for (index=0;index<1000;index++)
141 udelay(1000);
142 }
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200143 putc('\n');
stroesee1cb7eb2003-09-12 08:41:56 +0000144 do_reset(NULL, 0, 0, NULL);
145 }
146
147 puts("FPGA: ");
148
149 /* display infos on fpgaimage */
150 index = 15;
151 for (i=0; i<4; i++) {
152 len = dst[index];
153 printf("%s ", &(dst[index+1]));
154 index += len+3;
155 }
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200156 putc('\n');
stroesee1cb7eb2003-09-12 08:41:56 +0000157
158 free(dst);
159
160 /*
161 * Reset FPGA via FPGA_DATA pin
162 */
163 SET_FPGA(FPGA_PRG | FPGA_CLK);
164 udelay(1000); /* wait 1ms */
165 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
166 udelay(1000); /* wait 1ms */
167
168 /*
169 * Reset external DUARTs
170 */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200171 out_be32((void*)GPIO0_OR,
172 in_be32((void*)GPIO0_OR) | CONFIG_SYS_DUART_RST);
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200173 udelay(10);
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200174 out_be32((void*)GPIO0_OR,
175 in_be32((void*)GPIO0_OR) & ~CONFIG_SYS_DUART_RST);
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200176 udelay(1000);
stroesee1cb7eb2003-09-12 08:41:56 +0000177
178 /*
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100179 * Set NAND-FLASH GPIO signals to default
180 */
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200181 out_be32((void*)GPIO0_OR,
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200182 in_be32((void*)GPIO0_OR) &
183 ~(CONFIG_SYS_NAND_CLE | CONFIG_SYS_NAND_ALE));
184 out_be32((void*)GPIO0_OR,
185 in_be32((void*)GPIO0_OR) | CONFIG_SYS_NAND_CE);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100186
187 /*
188 * Setup EEPROM write protection
189 */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200190 out_be32((void*)GPIO0_OR,
191 in_be32((void*)GPIO0_OR) | CONFIG_SYS_EEPROM_WP);
192 out_be32((void*)GPIO0_TCR,
193 in_be32((void*)GPIO0_TCR) | CONFIG_SYS_EEPROM_WP);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100194
195 /*
stroesee1cb7eb2003-09-12 08:41:56 +0000196 * Enable interrupts in exar duart mcr[3]
197 */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200198 out_8((void *)DUART0_BA + 4, 0x08);
199 out_8((void *)DUART1_BA + 4, 0x08);
stroesee1cb7eb2003-09-12 08:41:56 +0000200
Matthias Fuchseb89df72009-01-02 12:16:35 +0100201 /*
202 * Enable auto RS485 mode in 2nd external uart
203 */
204 out_8((void *)DUART1_BA + 3, 0xbf); /* write LCR */
205 fctr = in_8((void *)DUART1_BA + 1); /* read FCTR */
206 fctr |= 0x08; /* enable RS485 mode */
207 out_8((void *)DUART1_BA + 1, fctr); /* write FCTR */
208 out_8((void *)DUART1_BA + 3, 0); /* write LCR */
209
Matthias Fuchsd1c60452009-10-26 09:58:45 +0100210 /*
211 * Init magnetic couplers
212 */
213 if (!getenv("noinitcoupler")) {
214 init_coupler(CAN0_BA);
215 init_coupler(CAN1_BA);
216 }
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200217 return 0;
stroesee1cb7eb2003-09-12 08:41:56 +0000218}
219
stroesee1cb7eb2003-09-12 08:41:56 +0000220/*
221 * Check Board Identity:
222 */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200223int checkboard(void)
stroesee1cb7eb2003-09-12 08:41:56 +0000224{
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200225 char str[64];
Wolfgang Denk76af2782010-07-24 21:55:43 +0200226 int i = getenv_f("serial#", str, sizeof(str));
stroesee1cb7eb2003-09-12 08:41:56 +0000227
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200228 puts("Board: ");
stroesee1cb7eb2003-09-12 08:41:56 +0000229
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200230 if (i == -1)
231 puts("### No HW ID - assuming PLU405");
232 else
stroesee1cb7eb2003-09-12 08:41:56 +0000233 puts(str);
stroesee1cb7eb2003-09-12 08:41:56 +0000234
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200235 putc('\n');
stroesee1cb7eb2003-09-12 08:41:56 +0000236 return 0;
237}
238
stroesee1cb7eb2003-09-12 08:41:56 +0000239#ifdef CONFIG_IDE_RESET
Matthias Fuchs5dde4e22009-02-20 10:19:19 +0100240#define FPGA_CTRL (CONFIG_SYS_FPGA_BASE_ADDR + CONFIG_SYS_FPGA_CTRL)
stroesee1cb7eb2003-09-12 08:41:56 +0000241void ide_set_reset(int on)
242{
stroesee1cb7eb2003-09-12 08:41:56 +0000243 /*
244 * Assert or deassert CompactFlash Reset Pin
245 */
246 if (on) { /* assert RESET */
Matthias Fuchs5dde4e22009-02-20 10:19:19 +0100247 out_be16((void *)FPGA_CTRL,
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200248 in_be16((void *)FPGA_CTRL) &
249 ~CONFIG_SYS_FPGA_CTRL_CF_RESET);
stroesee1cb7eb2003-09-12 08:41:56 +0000250 } else { /* release RESET */
Matthias Fuchs5dde4e22009-02-20 10:19:19 +0100251 out_be16((void *)FPGA_CTRL,
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200252 in_be16((void *)FPGA_CTRL) |
253 CONFIG_SYS_FPGA_CTRL_CF_RESET);
stroesee1cb7eb2003-09-12 08:41:56 +0000254 }
255}
256#endif /* CONFIG_IDE_RESET */
257
Matthias Fuchs9ee77182007-03-07 15:32:01 +0100258void reset_phy(void)
259{
260#ifdef CONFIG_LXT971_NO_SLEEP
261
262 /*
263 * Disable sleep mode in LXT971
264 */
265 lxt971_no_sleep();
266#endif
267}
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100268
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200269#if defined(CONFIG_SYS_EEPROM_WREN)
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100270/* Input: <dev_addr> I2C address of EEPROM device to enable.
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200271 * <state> -1: deliver current state
272 * 0: disable write
273 * 1: enable write
274 * Returns: -1: wrong device address
275 * 0: dis-/en- able done
276 * 0/1: current state if <state> was -1.
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100277 */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200278int eeprom_write_enable(unsigned dev_addr, int state)
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100279{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200280 if (CONFIG_SYS_I2C_EEPROM_ADDR != dev_addr) {
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100281 return -1;
282 } else {
283 switch (state) {
284 case 1:
285 /* Enable write access, clear bit GPIO0. */
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200286 out_be32((void*)GPIO0_OR,
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200287 in_be32((void*)GPIO0_OR) &
288 ~CONFIG_SYS_EEPROM_WP);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100289 state = 0;
290 break;
291 case 0:
292 /* Disable write access, set bit GPIO0. */
Matthias Fuchsa1fe6fb2008-09-02 11:35:35 +0200293 out_be32((void*)GPIO0_OR,
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200294 in_be32((void*)GPIO0_OR) |
295 CONFIG_SYS_EEPROM_WP);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100296 state = 0;
297 break;
298 default:
299 /* Read current status back. */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200300 state = ((in_be32((void*)GPIO0_OR) &
301 CONFIG_SYS_EEPROM_WP) == 0);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100302 break;
303 }
304 }
305 return state;
306}
307
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200308int do_eep_wren(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100309{
310 int query = argc == 1;
311 int state = 0;
312
313 if (query) {
314 /* Query write access state. */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200315 state = eeprom_write_enable(CONFIG_SYS_I2C_EEPROM_ADDR, -1);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100316 if (state < 0) {
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200317 puts("Query of write access state failed.\n");
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100318 } else {
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200319 printf("Write access for device 0x%0x is %sabled.\n",
320 CONFIG_SYS_I2C_EEPROM_ADDR,
321 state ? "en" : "dis");
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100322 state = 0;
323 }
324 } else {
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200325 if (argv[1][0] == '0') {
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100326 /* Disable write access. */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200327 state = eeprom_write_enable(CONFIG_SYS_I2C_EEPROM_ADDR,
328 0);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100329 } else {
330 /* Enable write access. */
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200331 state = eeprom_write_enable(CONFIG_SYS_I2C_EEPROM_ADDR,
332 1);
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100333 }
Matthias Fuchs1fc0bd82009-07-16 22:13:57 +0200334 if (state < 0)
335 puts("Setup of write access state failed.\n");
Matthias Fuchs9f05e852007-12-28 17:10:36 +0100336 }
337
338 return state;
339}
340
341U_BOOT_CMD(eepwren, 2, 0, do_eep_wren,
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200342 "Enable / disable / query EEPROM write access",
343 ""
344);
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200345#endif /* #if defined(CONFIG_SYS_EEPROM_WREN) */