blob: f80361081a6c080d6011a5fa168724515f8fea3e [file] [log] [blame]
wdenkc6097192002-11-03 00:24:07 +00001/*
stroese768eb2d2003-03-20 15:31:19 +00002 * (C) Copyright 2001-2003
wdenkc6097192002-11-03 00:24:07 +00003 * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
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#include <common.h>
25#include <asm/processor.h>
26#include <command.h>
wdenkc6097192002-11-03 00:24:07 +000027#include <malloc.h>
stroese9b115e92004-12-16 18:27:05 +000028#include <net.h>
wdenkc6097192002-11-03 00:24:07 +000029
Wolfgang Denk6405a152006-03-31 18:32:53 +020030DECLARE_GLOBAL_DATA_PTR;
31
wdenk57b2d802003-06-27 21:31:46 +000032extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /*cmd_boot.c*/
wdenkc6097192002-11-03 00:24:07 +000033#if 0
34#define FPGA_DEBUG
35#endif
36
37/* fpga configuration data - generated by bin2cc */
38const unsigned char fpgadata[] =
39{
40#ifdef CONFIG_CPCI405_VER2
stroesed253d4b2003-05-23 11:30:39 +000041# ifdef CONFIG_CPCI405AB
42# include "fpgadata_cpci405ab.c"
43# else
44# include "fpgadata_cpci4052.c"
45# endif
wdenkc6097192002-11-03 00:24:07 +000046#else
47# include "fpgadata_cpci405.c"
48#endif
49};
50
51/*
52 * include common fpga code (for esd boards)
53 */
54#include "../common/fpga.c"
55
56
stroese9b115e92004-12-16 18:27:05 +000057#include "../common/auto_update.h"
58
59#ifdef CONFIG_CPCI405AB
60au_image_t au_image[] = {
61 {"cpci405ab/preinst.img", 0, -1, AU_SCRIPT},
62 {"cpci405ab/pImage", 0xffc00000, 0x000c0000, AU_NOR},
63 {"cpci405ab/pImage.initrd", 0xffcc0000, 0x00300000, AU_NOR},
64 {"cpci405ab/u-boot.img", 0xfffc0000, 0x00040000, AU_FIRMWARE},
65 {"cpci405ab/postinst.img", 0, 0, AU_SCRIPT},
66};
67#else
68#ifdef CONFIG_CPCI405_VER2
69au_image_t au_image[] = {
70 {"cpci4052/preinst.img", 0, -1, AU_SCRIPT},
71 {"cpci4052/pImage", 0xffc00000, 0x000c0000, AU_NOR},
72 {"cpci4052/pImage.initrd", 0xffcc0000, 0x00300000, AU_NOR},
73 {"cpci4052/u-boot.img", 0xfffc0000, 0x00040000, AU_FIRMWARE},
74 {"cpci4052/postinst.img", 0, 0, AU_SCRIPT},
75};
76#else
77au_image_t au_image[] = {
78 {"cpci405/preinst.img", 0, -1, AU_SCRIPT},
79 {"cpci405/pImage", 0xffc00000, 0x000c0000, AU_NOR},
80 {"cpci405/pImage.initrd", 0xffcc0000, 0x00310000, AU_NOR},
81 {"cpci405/u-boot.img", 0xfffd0000, 0x00030000, AU_FIRMWARE},
82 {"cpci405/postinst.img", 0, 0, AU_SCRIPT},
83};
84#endif
85#endif
86
87int N_AU_IMAGES = (sizeof(au_image) / sizeof(au_image[0]));
88
89
wdenkc6097192002-11-03 00:24:07 +000090/* Prototypes */
stroese768eb2d2003-03-20 15:31:19 +000091int cpci405_version(void);
wdenka0ebde52004-09-08 22:03:11 +000092int gunzip(void *, int, unsigned char *, unsigned long *);
stroese9b115e92004-12-16 18:27:05 +000093void lxt971_no_sleep(void);
wdenkc6097192002-11-03 00:24:07 +000094
95
wdenkda55c6e2004-01-20 23:12:12 +000096int board_early_init_f (void)
wdenkc6097192002-11-03 00:24:07 +000097{
98#ifndef CONFIG_CPCI405_VER2
99 int index, len, i;
100 int status;
101#endif
102
103#ifdef FPGA_DEBUG
wdenkc6097192002-11-03 00:24:07 +0000104 /* set up serial port with default baudrate */
105 (void) get_clocks ();
106 gd->baudrate = CONFIG_BAUDRATE;
107 serial_init ();
108 console_init_f();
109#endif
110
111 /*
112 * First pull fpga-prg pin low, to disable fpga logic (on version 2 board)
113 */
stroesed253d4b2003-05-23 11:30:39 +0000114 out32(GPIO0_ODR, 0x00000000); /* no open drain pins */
115 out32(GPIO0_TCR, CFG_FPGA_PRG); /* setup for output */
116 out32(GPIO0_OR, CFG_FPGA_PRG); /* set output pins to high */
117 out32(GPIO0_OR, 0); /* pull prg low */
wdenkc6097192002-11-03 00:24:07 +0000118
119 /*
120 * Boot onboard FPGA
121 */
122#ifndef CONFIG_CPCI405_VER2
stroese768eb2d2003-03-20 15:31:19 +0000123 if (cpci405_version() == 1) {
wdenkc6097192002-11-03 00:24:07 +0000124 status = fpga_boot((unsigned char *)fpgadata, sizeof(fpgadata));
125 if (status != 0) {
126 /* booting FPGA failed */
127#ifndef FPGA_DEBUG
wdenkc6097192002-11-03 00:24:07 +0000128 /* set up serial port with default baudrate */
129 (void) get_clocks ();
130 gd->baudrate = CONFIG_BAUDRATE;
131 serial_init ();
132 console_init_f();
133#endif
134 printf("\nFPGA: Booting failed ");
135 switch (status) {
136 case ERROR_FPGA_PRG_INIT_LOW:
137 printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
138 break;
139 case ERROR_FPGA_PRG_INIT_HIGH:
140 printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
141 break;
142 case ERROR_FPGA_PRG_DONE:
143 printf("(Timeout: DONE not high after programming FPGA)\n ");
144 break;
145 }
146
147 /* display infos on fpgaimage */
148 index = 15;
149 for (i=0; i<4; i++) {
150 len = fpgadata[index];
151 printf("FPGA: %s\n", &(fpgadata[index+1]));
152 index += len+3;
153 }
154 putc ('\n');
155 /* delayed reboot */
156 for (i=20; i>0; i--) {
157 printf("Rebooting in %2d seconds \r",i);
158 for (index=0;index<1000;index++)
159 udelay(1000);
160 }
161 putc ('\n');
162 do_reset(NULL, 0, 0, NULL);
163 }
164 }
165#endif /* !CONFIG_CPCI405_VER2 */
166
167 /*
168 * IRQ 0-15 405GP internally generated; active high; level sensitive
169 * IRQ 16 405GP internally generated; active low; level sensitive
170 * IRQ 17-24 RESERVED
171 * IRQ 25 (EXT IRQ 0) CAN0; active low; level sensitive
172 * IRQ 26 (EXT IRQ 1) CAN1 (+FPGA on CPCI4052) ; active low; level sensitive
173 * IRQ 27 (EXT IRQ 2) PCI SLOT 0; active low; level sensitive
174 * IRQ 28 (EXT IRQ 3) PCI SLOT 1; active low; level sensitive
175 * IRQ 29 (EXT IRQ 4) PCI SLOT 2; active low; level sensitive
176 * IRQ 30 (EXT IRQ 5) PCI SLOT 3; active low; level sensitive
177 * IRQ 31 (EXT IRQ 6) COMPACT FLASH; active high; level sensitive
178 */
179 mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
180 mtdcr(uicer, 0x00000000); /* disable all ints */
181 mtdcr(uiccr, 0x00000000); /* set all to be non-critical*/
stroese768eb2d2003-03-20 15:31:19 +0000182 if (cpci405_version() == 3) {
183 mtdcr(uicpr, 0xFFFFFF99); /* set int polarities */
184 } else {
185 mtdcr(uicpr, 0xFFFFFF81); /* set int polarities */
186 }
wdenkc6097192002-11-03 00:24:07 +0000187 mtdcr(uictr, 0x10000000); /* set int trigger levels */
188 mtdcr(uicvcr, 0x00000001); /* set vect base=0,INT0 highest priority*/
189 mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
190
191 return 0;
192}
193
194
195/* ------------------------------------------------------------------------- */
196
197int ctermm2(void)
198{
199#ifdef CONFIG_CPCI405_VER2
200 return 0; /* no, board is cpci405 */
201#else
202 if ((*(unsigned char *)0xf0000400 == 0x00) &&
203 (*(unsigned char *)0xf0000401 == 0x01))
204 return 0; /* no, board is cpci405 */
205 else
206 return -1; /* yes, board is cterm-m2 */
207#endif
208}
209
210
211int cpci405_host(void)
212{
213 if (mfdcr(strap) & PSR_PCI_ARBIT_EN)
214 return -1; /* yes, board is cpci405 host */
215 else
216 return 0; /* no, board is cpci405 adapter */
217}
218
219
stroese768eb2d2003-03-20 15:31:19 +0000220int cpci405_version(void)
wdenkc6097192002-11-03 00:24:07 +0000221{
222 unsigned long cntrl0Reg;
223 unsigned long value;
224
225 /*
stroese768eb2d2003-03-20 15:31:19 +0000226 * Setup GPIO pins (CS2/GPIO11 and CS3/GPIO12 as GPIO)
wdenkc6097192002-11-03 00:24:07 +0000227 */
228 cntrl0Reg = mfdcr(cntrl0);
stroese768eb2d2003-03-20 15:31:19 +0000229 mtdcr(cntrl0, cntrl0Reg | 0x03000000);
stroesed253d4b2003-05-23 11:30:39 +0000230 out32(GPIO0_ODR, in32(GPIO0_ODR) & ~0x00180000);
231 out32(GPIO0_TCR, in32(GPIO0_TCR) & ~0x00180000);
wdenkc6097192002-11-03 00:24:07 +0000232 udelay(1000); /* wait some time before reading input */
stroesed253d4b2003-05-23 11:30:39 +0000233 value = in32(GPIO0_IR) & 0x00180000; /* get config bits */
wdenkc6097192002-11-03 00:24:07 +0000234
235 /*
stroese768eb2d2003-03-20 15:31:19 +0000236 * Restore GPIO settings
wdenkc6097192002-11-03 00:24:07 +0000237 */
238 mtdcr(cntrl0, cntrl0Reg);
239
stroese768eb2d2003-03-20 15:31:19 +0000240 switch (value) {
241 case 0x00180000:
242 /* CS2==1 && CS3==1 -> version 1 */
243 return 1;
244 case 0x00080000:
245 /* CS2==0 && CS3==1 -> version 2 */
246 return 2;
247 case 0x00100000:
248 /* CS2==1 && CS3==0 -> version 3 */
249 return 3;
250 case 0x00000000:
251 /* CS2==0 && CS3==0 -> version 4 */
252 return 4;
253 default:
254 /* should not be reached! */
255 return 2;
256 }
wdenkc6097192002-11-03 00:24:07 +0000257}
258
259
260int misc_init_f (void)
261{
262 return 0; /* dummy implementation */
263}
264
265
266int misc_init_r (void)
267{
stroese67cb27d2003-04-04 16:52:57 +0000268 unsigned long cntrl0Reg;
wdenkc6097192002-11-03 00:24:07 +0000269
stroese9b115e92004-12-16 18:27:05 +0000270 /* adjust flash start and offset */
271 gd->bd->bi_flashstart = 0 - gd->bd->bi_flashsize;
272 gd->bd->bi_flashoffset = 0;
273
wdenkc6097192002-11-03 00:24:07 +0000274#ifdef CONFIG_CPCI405_VER2
stroese9b115e92004-12-16 18:27:05 +0000275 {
wdenkc6097192002-11-03 00:24:07 +0000276 unsigned char *dst;
277 ulong len = sizeof(fpgadata);
278 int status;
279 int index;
280 int i;
wdenkc6097192002-11-03 00:24:07 +0000281
282 /*
283 * On CPCI-405 version 2 the environment is saved in eeprom!
284 * FPGA can be gzip compressed (malloc) and booted this late.
285 */
286
stroese768eb2d2003-03-20 15:31:19 +0000287 if (cpci405_version() >= 2) {
wdenkc6097192002-11-03 00:24:07 +0000288 /*
289 * Setup GPIO pins (CS6+CS7 as GPIO)
290 */
291 cntrl0Reg = mfdcr(cntrl0);
292 mtdcr(cntrl0, cntrl0Reg | 0x00300000);
293
294 dst = malloc(CFG_FPGA_MAX_SIZE);
wdenka0ebde52004-09-08 22:03:11 +0000295 if (gunzip (dst, CFG_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
wdenkc6097192002-11-03 00:24:07 +0000296 printf ("GUNZIP ERROR - must RESET board to recover\n");
297 do_reset (NULL, 0, 0, NULL);
298 }
299
300 status = fpga_boot(dst, len);
301 if (status != 0) {
302 printf("\nFPGA: Booting failed ");
303 switch (status) {
304 case ERROR_FPGA_PRG_INIT_LOW:
305 printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
306 break;
307 case ERROR_FPGA_PRG_INIT_HIGH:
308 printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
309 break;
310 case ERROR_FPGA_PRG_DONE:
311 printf("(Timeout: DONE not high after programming FPGA)\n ");
312 break;
313 }
314
315 /* display infos on fpgaimage */
316 index = 15;
317 for (i=0; i<4; i++) {
318 len = dst[index];
319 printf("FPGA: %s\n", &(dst[index+1]));
320 index += len+3;
321 }
322 putc ('\n');
323 /* delayed reboot */
324 for (i=20; i>0; i--) {
325 printf("Rebooting in %2d seconds \r",i);
326 for (index=0;index<1000;index++)
327 udelay(1000);
328 }
329 putc ('\n');
330 do_reset(NULL, 0, 0, NULL);
331 }
332
333 /* restore gpio/cs settings */
334 mtdcr(cntrl0, cntrl0Reg);
335
336 puts("FPGA: ");
337
338 /* display infos on fpgaimage */
339 index = 15;
340 for (i=0; i<4; i++) {
341 len = dst[index];
342 printf("%s ", &(dst[index+1]));
343 index += len+3;
344 }
345 putc ('\n');
346
347 free(dst);
stroese768eb2d2003-03-20 15:31:19 +0000348
349 /*
350 * Reset FPGA via FPGA_DATA pin
351 */
352 SET_FPGA(FPGA_PRG | FPGA_CLK);
353 udelay(1000); /* wait 1ms */
354 SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
355 udelay(1000); /* wait 1ms */
356
357 if (cpci405_version() == 3) {
358 volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
359 volatile unsigned char *leds = (unsigned char *)CFG_LED_ADDR;
360
361 /*
362 * Enable outputs in fpga on version 3 board
363 */
364 *fpga_mode |= CFG_FPGA_MODE_ENABLE_OUTPUT;
365
366 /*
367 * Set outputs to 0
368 */
369 *leds = 0x00;
370
371 /*
372 * Reset external DUART
373 */
374 *fpga_mode |= CFG_FPGA_MODE_DUART_RESET;
375 udelay(100);
376 *fpga_mode &= ~(CFG_FPGA_MODE_DUART_RESET);
377 }
wdenkc6097192002-11-03 00:24:07 +0000378 }
379 else {
stroese768eb2d2003-03-20 15:31:19 +0000380 puts("\n*** U-Boot Version does not match Board Version!\n");
381 puts("*** CPCI-405 Version 1.x detected!\n");
382 puts("*** Please use correct U-Boot version (CPCI405 instead of CPCI4052)!\n\n");
wdenkc6097192002-11-03 00:24:07 +0000383 }
stroese9b115e92004-12-16 18:27:05 +0000384 }
wdenkc6097192002-11-03 00:24:07 +0000385
386#else /* CONFIG_CPCI405_VER2 */
387
stroese9b115e92004-12-16 18:27:05 +0000388#if 0 /* test-only: code-plug now not relavant for ip-address any more */
wdenkc6097192002-11-03 00:24:07 +0000389 /*
390 * Generate last byte of ip-addr from code-plug @ 0xf0000400
391 */
392 if (ctermm2()) {
393 char str[32];
394 unsigned char ipbyte = *(unsigned char *)0xf0000400;
395
396 /*
397 * Only overwrite ip-addr with allowed values
398 */
399 if ((ipbyte != 0x00) && (ipbyte != 0xff)) {
400 bd->bi_ip_addr = (bd->bi_ip_addr & 0xffffff00) | ipbyte;
401 sprintf(str, "%ld.%ld.%ld.%ld",
402 (bd->bi_ip_addr & 0xff000000) >> 24,
403 (bd->bi_ip_addr & 0x00ff0000) >> 16,
404 (bd->bi_ip_addr & 0x0000ff00) >> 8,
405 (bd->bi_ip_addr & 0x000000ff));
406 setenv("ipaddr", str);
407 }
408 }
stroese9b115e92004-12-16 18:27:05 +0000409#endif
wdenkc6097192002-11-03 00:24:07 +0000410
stroese768eb2d2003-03-20 15:31:19 +0000411 if (cpci405_version() >= 2) {
412 puts("\n*** U-Boot Version does not match Board Version!\n");
413 puts("*** CPCI-405 Board Version 2.x detected!\n");
414 puts("*** Please use correct U-Boot version (CPCI4052 instead of CPCI405)!\n\n");
wdenkc6097192002-11-03 00:24:07 +0000415 }
416
417#endif /* CONFIG_CPCI405_VER2 */
418
419 /*
stroese67cb27d2003-04-04 16:52:57 +0000420 * Select cts (and not dsr) on uart1
421 */
422 cntrl0Reg = mfdcr(cntrl0);
423 mtdcr(cntrl0, cntrl0Reg | 0x00001000);
424
wdenkc6097192002-11-03 00:24:07 +0000425 return (0);
426}
427
428
429/*
430 * Check Board Identity:
431 */
432
433int checkboard (void)
434{
435#ifndef CONFIG_CPCI405_VER2
436 int index;
437 int len;
438#endif
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200439 char str[64];
wdenkc6097192002-11-03 00:24:07 +0000440 int i = getenv_r ("serial#", str, sizeof(str));
stroese768eb2d2003-03-20 15:31:19 +0000441 unsigned short ver;
wdenkc6097192002-11-03 00:24:07 +0000442
443 puts ("Board: ");
444
445 if (i == -1) {
446 puts ("### No HW ID - assuming CPCI405");
447 } else {
448 puts(str);
449 }
450
stroese768eb2d2003-03-20 15:31:19 +0000451 ver = cpci405_version();
452 printf(" (Ver %d.x, ", ver);
wdenkc6097192002-11-03 00:24:07 +0000453
stroese768eb2d2003-03-20 15:31:19 +0000454#if 0 /* test-only */
455 if (ver >= 2) {
456 volatile u16 *fpga_status = (u16 *)CFG_FPGA_BASE_ADDR + 1;
457
458 if (*fpga_status & CFG_FPGA_STATUS_FLASH) {
459 puts ("FLASH Bank B, ");
460 } else {
461 puts ("FLASH Bank A, ");
462 }
463 }
wdenkc6097192002-11-03 00:24:07 +0000464#endif
465
466 if (ctermm2()) {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200467 char str[4];
stroesec8065c92003-09-12 08:44:46 +0000468
469 /*
470 * Read board-id and save in env-variable
471 */
472 sprintf(str, "%d", *(unsigned char *)0xf0000400);
473 setenv("boardid", str);
474 printf("CTERM-M2 - Id=%s)", str);
wdenkc6097192002-11-03 00:24:07 +0000475 } else {
476 if (cpci405_host()) {
477 puts ("PCI Host Version)");
478 } else {
479 puts ("PCI Adapter Version)");
480 }
481 }
482
483#ifndef CONFIG_CPCI405_VER2
484 puts ("\nFPGA: ");
485
486 /* display infos on fpgaimage */
487 index = 15;
488 for (i=0; i<4; i++) {
489 len = fpgadata[index];
490 printf("%s ", &(fpgadata[index+1]));
491 index += len+3;
492 }
493#endif
494
495 putc ('\n');
496
stroese9b115e92004-12-16 18:27:05 +0000497 /*
498 * Disable sleep mode in LXT971
499 */
500 lxt971_no_sleep();
501
wdenkc6097192002-11-03 00:24:07 +0000502 return 0;
503}
504
505/* ------------------------------------------------------------------------- */
506
507long int initdram (int board_type)
508{
509 unsigned long val;
510
511 mtdcr(memcfga, mem_mb0cf);
512 val = mfdcr(memcfgd);
513
514#if 0
515 printf("\nmb0cf=%x\n", val); /* test-only */
516 printf("strap=%x\n", mfdcr(strap)); /* test-only */
517#endif
518
519 return (4*1024*1024 << ((val & 0x000e0000) >> 17));
520}
521
522/* ------------------------------------------------------------------------- */
523
524int testdram (void)
525{
526 /* TODO: XXX XXX XXX */
527 printf ("test: 16 MB - ok\n");
528
529 return (0);
530}
531
532/* ------------------------------------------------------------------------- */
533
534#ifdef CONFIG_CPCI405_VER2
535#ifdef CONFIG_IDE_RESET
536
537void ide_set_reset(int on)
538{
539 volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
540
541 /*
542 * Assert or deassert CompactFlash Reset Pin
543 */
544 if (on) { /* assert RESET */
545 *fpga_mode &= ~(CFG_FPGA_MODE_CF_RESET);
546 } else { /* release RESET */
547 *fpga_mode |= CFG_FPGA_MODE_CF_RESET;
548 }
549}
550
551#endif /* CONFIG_IDE_RESET */
552#endif /* CONFIG_CPCI405_VER2 */
553
stroesec8065c92003-09-12 08:44:46 +0000554
555#ifdef CONFIG_CPCI405AB
stroesed253d4b2003-05-23 11:30:39 +0000556
stroese9b115e92004-12-16 18:27:05 +0000557#define ONE_WIRE_CLEAR (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
wdenk07d7e6b2004-12-16 21:44:03 +0000558 |= CFG_FPGA_MODE_1WIRE_DIR)
stroese9b115e92004-12-16 18:27:05 +0000559#define ONE_WIRE_SET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
wdenk07d7e6b2004-12-16 21:44:03 +0000560 &= ~CFG_FPGA_MODE_1WIRE_DIR)
stroese9b115e92004-12-16 18:27:05 +0000561#define ONE_WIRE_GET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_STATUS) \
wdenk07d7e6b2004-12-16 21:44:03 +0000562 & CFG_FPGA_MODE_1WIRE)
stroesec8065c92003-09-12 08:44:46 +0000563
564/*
565 * Generate a 1-wire reset, return 1 if no presence detect was found,
566 * return 0 otherwise.
567 * (NOTE: Does not handle alarm presence from DS2404/DS1994)
568 */
569int OWTouchReset(void)
stroesed253d4b2003-05-23 11:30:39 +0000570{
stroesec8065c92003-09-12 08:44:46 +0000571 int result;
stroesed253d4b2003-05-23 11:30:39 +0000572
stroesec8065c92003-09-12 08:44:46 +0000573 ONE_WIRE_CLEAR;
574 udelay(480);
575 ONE_WIRE_SET;
576 udelay(70);
577
578 result = ONE_WIRE_GET;
579
580 udelay(410);
581 return result;
582}
583
584
585/*
586 * Send 1 a 1-wire write bit.
587 * Provide 10us recovery time.
588 */
589void OWWriteBit(int bit)
590{
591 if (bit) {
592 /*
593 * write '1' bit
594 */
595 ONE_WIRE_CLEAR;
596 udelay(6);
597 ONE_WIRE_SET;
598 udelay(64);
599 } else {
600 /*
601 * write '0' bit
602 */
603 ONE_WIRE_CLEAR;
604 udelay(60);
605 ONE_WIRE_SET;
606 udelay(10);
stroesed253d4b2003-05-23 11:30:39 +0000607 }
stroesec8065c92003-09-12 08:44:46 +0000608}
609
610
611/*
612 * Read a bit from the 1-wire bus and return it.
613 * Provide 10us recovery time.
614 */
615int OWReadBit(void)
616{
617 int result;
stroesed253d4b2003-05-23 11:30:39 +0000618
stroesec8065c92003-09-12 08:44:46 +0000619 ONE_WIRE_CLEAR;
620 udelay(6);
621 ONE_WIRE_SET;
622 udelay(9);
623
624 result = ONE_WIRE_GET;
625
626 udelay(55);
627 return result;
stroesed253d4b2003-05-23 11:30:39 +0000628}
629
630
stroesec8065c92003-09-12 08:44:46 +0000631void OWWriteByte(int data)
632{
633 int loop;
634
635 for (loop=0; loop<8; loop++) {
636 OWWriteBit(data & 0x01);
637 data >>= 1;
638 }
639}
640
stroesed253d4b2003-05-23 11:30:39 +0000641
stroesec8065c92003-09-12 08:44:46 +0000642int OWReadByte(void)
stroesed253d4b2003-05-23 11:30:39 +0000643{
stroesec8065c92003-09-12 08:44:46 +0000644 int loop, result = 0;
645
646 for (loop=0; loop<8; loop++) {
647 result >>= 1;
648 if (OWReadBit()) {
649 result |= 0x80;
650 }
stroesed253d4b2003-05-23 11:30:39 +0000651 }
stroesec8065c92003-09-12 08:44:46 +0000652
653 return result;
stroesed253d4b2003-05-23 11:30:39 +0000654}
655
stroesec8065c92003-09-12 08:44:46 +0000656
657int do_onewire(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
658{
659 volatile unsigned short val;
660 int result;
661 int i;
662 unsigned char ow_id[6];
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200663 char str[32];
stroesec8065c92003-09-12 08:44:46 +0000664 unsigned char ow_crc;
665
666 /*
667 * Clear 1-wire bit (open drain with pull-up)
668 */
669 val = *(volatile unsigned short *)0xf0400000;
670 val &= ~0x1000; /* clear 1-wire bit */
671 *(volatile unsigned short *)0xf0400000 = val;
672
673 result = OWTouchReset();
674 if (result != 0) {
675 puts("No 1-wire device detected!\n");
676 }
677
678 OWWriteByte(0x33); /* send read rom command */
679 OWReadByte(); /* skip family code ( == 0x01) */
680 for (i=0; i<6; i++) {
681 ow_id[i] = OWReadByte();
682 }
683 ow_crc = OWReadByte(); /* read crc */
684
685 sprintf(str, "%08X%04X", *(unsigned int *)&ow_id[0], *(unsigned short *)&ow_id[4]);
686 printf("Setting environment variable 'ow_id' to %s\n", str);
687 setenv("ow_id", str);
688
689 return 0;
690}
691U_BOOT_CMD(
692 onewire, 1, 1, do_onewire,
693 "onewire - Read 1-write ID\n",
694 NULL
695 );
696
stroese9b115e92004-12-16 18:27:05 +0000697
698#define CFG_I2C_EEPROM_ADDR_2 0x51 /* EEPROM CAT28WC32 */
699#define CFG_ENV_SIZE_2 0x800 /* 2048 bytes may be used for env vars*/
700
701/*
702 * Write backplane ip-address...
703 */
704int do_get_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
705{
stroese9b115e92004-12-16 18:27:05 +0000706 bd_t *bd = gd->bd;
707 char *buf;
708 ulong crc;
709 char str[32];
710 char *ptr;
711 IPaddr_t ipaddr;
712
713 buf = malloc(CFG_ENV_SIZE_2);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200714 if (eeprom_read(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CFG_ENV_SIZE_2)) {
stroese9b115e92004-12-16 18:27:05 +0000715 puts("\nError reading backplane EEPROM!\n");
716 } else {
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200717 crc = crc32(0, (uchar *)(buf+4), CFG_ENV_SIZE_2-4);
stroese9b115e92004-12-16 18:27:05 +0000718 if (crc != *(ulong *)buf) {
719 printf("ERROR: crc mismatch %08lx %08lx\n", crc, *(ulong *)buf);
720 return -1;
721 }
722
723 /*
724 * Find bp_ip
725 */
726 ptr = strstr(buf+4, "bp_ip=");
727 if (ptr == NULL) {
728 printf("ERROR: bp_ip not found!\n");
729 return -1;
730 }
731 ptr += 6;
732 ipaddr = string_to_ip(ptr);
733
734 /*
735 * Update whole ip-addr
736 */
737 bd->bi_ip_addr = ipaddr;
738 sprintf(str, "%ld.%ld.%ld.%ld",
739 (bd->bi_ip_addr & 0xff000000) >> 24,
740 (bd->bi_ip_addr & 0x00ff0000) >> 16,
741 (bd->bi_ip_addr & 0x0000ff00) >> 8,
742 (bd->bi_ip_addr & 0x000000ff));
743 setenv("ipaddr", str);
744 printf("Updated ip_addr from bp_eeprom to %s!\n", str);
745 }
746
747 free(buf);
748
749 return 0;
750}
751U_BOOT_CMD(
752 getbpip, 1, 1, do_get_bpip,
753 "getbpip - Update IP-Address with Backplane IP-Address\n",
754 NULL
755 );
756
757/*
758 * Set and print backplane ip...
759 */
760int do_set_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
761{
762 char *buf;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200763 char str[32];
stroese9b115e92004-12-16 18:27:05 +0000764 ulong crc;
765
766 if (argc < 2) {
767 puts("ERROR!\n");
768 return -1;
769 }
770
771 printf("Setting bp_ip to %s\n", argv[1]);
772 buf = malloc(CFG_ENV_SIZE_2);
773 memset(buf, 0, CFG_ENV_SIZE_2);
774 sprintf(str, "bp_ip=%s", argv[1]);
775 strcpy(buf+4, str);
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200776 crc = crc32(0, (uchar *)(buf+4), CFG_ENV_SIZE_2-4);
stroese9b115e92004-12-16 18:27:05 +0000777 *(ulong *)buf = crc;
778
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200779 if (eeprom_write(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CFG_ENV_SIZE_2)) {
stroese9b115e92004-12-16 18:27:05 +0000780 puts("\nError writing backplane EEPROM!\n");
781 }
782
783 free(buf);
784
785 return 0;
786}
787U_BOOT_CMD(
788 setbpip, 2, 1, do_set_bpip,
789 "setbpip - Write Backplane IP-Address\n",
790 NULL
791 );
792
stroesec8065c92003-09-12 08:44:46 +0000793#endif /* CONFIG_CPCI405AB */