cpci405.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * (C) Copyright 2001-2003
  3. * 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. #include <common.h>
  24. #include <asm/processor.h>
  25. #include <asm/io.h>
  26. #include <command.h>
  27. #include <malloc.h>
  28. #include <net.h>
  29. #include <pci.h>
  30. DECLARE_GLOBAL_DATA_PTR;
  31. extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]); /*cmd_boot.c*/
  32. #if 0
  33. #define FPGA_DEBUG
  34. #endif
  35. /* fpga configuration data - generated by bin2cc */
  36. const unsigned char fpgadata[] =
  37. {
  38. #ifdef CONFIG_CPCI405_VER2
  39. # ifdef CONFIG_CPCI405AB
  40. # include "fpgadata_cpci405ab.c"
  41. # else
  42. # include "fpgadata_cpci4052.c"
  43. # endif
  44. #else
  45. # include "fpgadata_cpci405.c"
  46. #endif
  47. };
  48. /*
  49. * include common fpga code (for esd boards)
  50. */
  51. #include "../common/fpga.c"
  52. #include "../common/auto_update.h"
  53. #ifdef CONFIG_CPCI405AB
  54. au_image_t au_image[] = {
  55. {"cpci405ab/preinst.img", 0, -1, AU_SCRIPT},
  56. {"cpci405ab/pImage", 0xffc00000, 0x000c0000, AU_NOR},
  57. {"cpci405ab/pImage.initrd", 0xffcc0000, 0x00300000, AU_NOR},
  58. {"cpci405ab/u-boot.img", 0xfffc0000, 0x00040000, AU_FIRMWARE},
  59. {"cpci405ab/postinst.img", 0, 0, AU_SCRIPT},
  60. };
  61. #else
  62. #ifdef CONFIG_CPCI405_VER2
  63. au_image_t au_image[] = {
  64. {"cpci4052/preinst.img", 0, -1, AU_SCRIPT},
  65. {"cpci4052/pImage", 0xffc00000, 0x000c0000, AU_NOR},
  66. {"cpci4052/pImage.initrd", 0xffcc0000, 0x00300000, AU_NOR},
  67. {"cpci4052/u-boot.img", 0xfffc0000, 0x00040000, AU_FIRMWARE},
  68. {"cpci4052/postinst.img", 0, 0, AU_SCRIPT},
  69. };
  70. #else
  71. au_image_t au_image[] = {
  72. {"cpci405/preinst.img", 0, -1, AU_SCRIPT},
  73. {"cpci405/pImage", 0xffc00000, 0x000c0000, AU_NOR},
  74. {"cpci405/pImage.initrd", 0xffcc0000, 0x00310000, AU_NOR},
  75. {"cpci405/u-boot.img", 0xfffd0000, 0x00030000, AU_FIRMWARE},
  76. {"cpci405/postinst.img", 0, 0, AU_SCRIPT},
  77. };
  78. #endif
  79. #endif
  80. int N_AU_IMAGES = (sizeof(au_image) / sizeof(au_image[0]));
  81. /* Prototypes */
  82. int cpci405_version(void);
  83. int gunzip(void *, int, unsigned char *, unsigned long *);
  84. void lxt971_no_sleep(void);
  85. int board_early_init_f (void)
  86. {
  87. #ifndef CONFIG_CPCI405_VER2
  88. int index, len, i;
  89. int status;
  90. #endif
  91. #ifdef FPGA_DEBUG
  92. /* set up serial port with default baudrate */
  93. (void) get_clocks ();
  94. gd->baudrate = CONFIG_BAUDRATE;
  95. serial_init ();
  96. console_init_f();
  97. #endif
  98. /*
  99. * First pull fpga-prg pin low, to disable fpga logic (on version 2 board)
  100. */
  101. out32(GPIO0_ODR, 0x00000000); /* no open drain pins */
  102. out32(GPIO0_TCR, CFG_FPGA_PRG); /* setup for output */
  103. out32(GPIO0_OR, CFG_FPGA_PRG); /* set output pins to high */
  104. out32(GPIO0_OR, 0); /* pull prg low */
  105. /*
  106. * Boot onboard FPGA
  107. */
  108. #ifndef CONFIG_CPCI405_VER2
  109. if (cpci405_version() == 1) {
  110. status = fpga_boot((unsigned char *)fpgadata, sizeof(fpgadata));
  111. if (status != 0) {
  112. /* booting FPGA failed */
  113. #ifndef FPGA_DEBUG
  114. /* set up serial port with default baudrate */
  115. (void) get_clocks ();
  116. gd->baudrate = CONFIG_BAUDRATE;
  117. serial_init ();
  118. console_init_f();
  119. #endif
  120. printf("\nFPGA: Booting failed ");
  121. switch (status) {
  122. case ERROR_FPGA_PRG_INIT_LOW:
  123. printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
  124. break;
  125. case ERROR_FPGA_PRG_INIT_HIGH:
  126. printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
  127. break;
  128. case ERROR_FPGA_PRG_DONE:
  129. printf("(Timeout: DONE not high after programming FPGA)\n ");
  130. break;
  131. }
  132. /* display infos on fpgaimage */
  133. index = 15;
  134. for (i=0; i<4; i++) {
  135. len = fpgadata[index];
  136. printf("FPGA: %s\n", &(fpgadata[index+1]));
  137. index += len+3;
  138. }
  139. putc ('\n');
  140. /* delayed reboot */
  141. for (i=20; i>0; i--) {
  142. printf("Rebooting in %2d seconds \r",i);
  143. for (index=0;index<1000;index++)
  144. udelay(1000);
  145. }
  146. putc ('\n');
  147. do_reset(NULL, 0, 0, NULL);
  148. }
  149. }
  150. #endif /* !CONFIG_CPCI405_VER2 */
  151. /*
  152. * IRQ 0-15 405GP internally generated; active high; level sensitive
  153. * IRQ 16 405GP internally generated; active low; level sensitive
  154. * IRQ 17-24 RESERVED
  155. * IRQ 25 (EXT IRQ 0) CAN0; active low; level sensitive
  156. * IRQ 26 (EXT IRQ 1) CAN1 (+FPGA on CPCI4052) ; active low; level sensitive
  157. * IRQ 27 (EXT IRQ 2) PCI SLOT 0; active low; level sensitive
  158. * IRQ 28 (EXT IRQ 3) PCI SLOT 1; active low; level sensitive
  159. * IRQ 29 (EXT IRQ 4) PCI SLOT 2; active low; level sensitive
  160. * IRQ 30 (EXT IRQ 5) PCI SLOT 3; active low; level sensitive
  161. * IRQ 31 (EXT IRQ 6) COMPACT FLASH; active high; level sensitive
  162. */
  163. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  164. mtdcr(uicer, 0x00000000); /* disable all ints */
  165. mtdcr(uiccr, 0x00000000); /* set all to be non-critical*/
  166. #ifdef CONFIG_CPCI405_6U
  167. if (cpci405_version() == 3) {
  168. mtdcr(uicpr, 0xFFFFFF99); /* set int polarities */
  169. } else {
  170. mtdcr(uicpr, 0xFFFFFF81); /* set int polarities */
  171. }
  172. #else
  173. mtdcr(uicpr, 0xFFFFFF81); /* set int polarities */
  174. #endif
  175. mtdcr(uictr, 0x10000000); /* set int trigger levels */
  176. mtdcr(uicvcr, 0x00000001); /* set vect base=0,INT0 highest priority*/
  177. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  178. return 0;
  179. }
  180. /* ------------------------------------------------------------------------- */
  181. int ctermm2(void)
  182. {
  183. #ifdef CONFIG_CPCI405_VER2
  184. return 0; /* no, board is cpci405 */
  185. #else
  186. if ((*(unsigned char *)0xf0000400 == 0x00) &&
  187. (*(unsigned char *)0xf0000401 == 0x01))
  188. return 0; /* no, board is cpci405 */
  189. else
  190. return -1; /* yes, board is cterm-m2 */
  191. #endif
  192. }
  193. int cpci405_host(void)
  194. {
  195. if (mfdcr(strap) & PSR_PCI_ARBIT_EN)
  196. return -1; /* yes, board is cpci405 host */
  197. else
  198. return 0; /* no, board is cpci405 adapter */
  199. }
  200. int cpci405_version(void)
  201. {
  202. unsigned long cntrl0Reg;
  203. unsigned long value;
  204. /*
  205. * Setup GPIO pins (CS2/GPIO11 and CS3/GPIO12 as GPIO)
  206. */
  207. cntrl0Reg = mfdcr(cntrl0);
  208. mtdcr(cntrl0, cntrl0Reg | 0x03000000);
  209. out_be32((void*)GPIO0_ODR, in_be32((void*)GPIO0_ODR) & ~0x00180000);
  210. out_be32((void*)GPIO0_TCR, in_be32((void*)GPIO0_TCR) & ~0x00180000);
  211. udelay(1000); /* wait some time before reading input */
  212. value = in_be32((void*)GPIO0_IR) & 0x00180000; /* get config bits */
  213. /*
  214. * Restore GPIO settings
  215. */
  216. mtdcr(cntrl0, cntrl0Reg);
  217. switch (value) {
  218. case 0x00180000:
  219. /* CS2==1 && CS3==1 -> version 1 */
  220. return 1;
  221. case 0x00080000:
  222. /* CS2==0 && CS3==1 -> version 2 */
  223. return 2;
  224. case 0x00100000:
  225. /* CS2==1 && CS3==0 -> version 3 or 6U board */
  226. return 3;
  227. case 0x00000000:
  228. /* CS2==0 && CS3==0 -> version 4 */
  229. return 4;
  230. default:
  231. /* should not be reached! */
  232. return 2;
  233. }
  234. }
  235. int misc_init_f (void)
  236. {
  237. return 0; /* dummy implementation */
  238. }
  239. int misc_init_r (void)
  240. {
  241. unsigned long cntrl0Reg;
  242. /* adjust flash start and offset */
  243. gd->bd->bi_flashstart = 0 - gd->bd->bi_flashsize;
  244. gd->bd->bi_flashoffset = 0;
  245. #ifdef CONFIG_CPCI405_VER2
  246. {
  247. unsigned char *dst;
  248. ulong len = sizeof(fpgadata);
  249. int status;
  250. int index;
  251. int i;
  252. /*
  253. * On CPCI-405 version 2 the environment is saved in eeprom!
  254. * FPGA can be gzip compressed (malloc) and booted this late.
  255. */
  256. if (cpci405_version() >= 2) {
  257. /*
  258. * Setup GPIO pins (CS6+CS7 as GPIO)
  259. */
  260. cntrl0Reg = mfdcr(cntrl0);
  261. mtdcr(cntrl0, cntrl0Reg | 0x00300000);
  262. dst = malloc(CFG_FPGA_MAX_SIZE);
  263. if (gunzip (dst, CFG_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
  264. printf ("GUNZIP ERROR - must RESET board to recover\n");
  265. do_reset (NULL, 0, 0, NULL);
  266. }
  267. status = fpga_boot(dst, len);
  268. if (status != 0) {
  269. printf("\nFPGA: Booting failed ");
  270. switch (status) {
  271. case ERROR_FPGA_PRG_INIT_LOW:
  272. printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
  273. break;
  274. case ERROR_FPGA_PRG_INIT_HIGH:
  275. printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
  276. break;
  277. case ERROR_FPGA_PRG_DONE:
  278. printf("(Timeout: DONE not high after programming FPGA)\n ");
  279. break;
  280. }
  281. /* display infos on fpgaimage */
  282. index = 15;
  283. for (i=0; i<4; i++) {
  284. len = dst[index];
  285. printf("FPGA: %s\n", &(dst[index+1]));
  286. index += len+3;
  287. }
  288. putc ('\n');
  289. /* delayed reboot */
  290. for (i=20; i>0; i--) {
  291. printf("Rebooting in %2d seconds \r",i);
  292. for (index=0;index<1000;index++)
  293. udelay(1000);
  294. }
  295. putc ('\n');
  296. do_reset(NULL, 0, 0, NULL);
  297. }
  298. /* restore gpio/cs settings */
  299. mtdcr(cntrl0, cntrl0Reg);
  300. puts("FPGA: ");
  301. /* display infos on fpgaimage */
  302. index = 15;
  303. for (i=0; i<4; i++) {
  304. len = dst[index];
  305. printf("%s ", &(dst[index+1]));
  306. index += len+3;
  307. }
  308. putc ('\n');
  309. free(dst);
  310. /*
  311. * Reset FPGA via FPGA_DATA pin
  312. */
  313. SET_FPGA(FPGA_PRG | FPGA_CLK);
  314. udelay(1000); /* wait 1ms */
  315. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
  316. udelay(1000); /* wait 1ms */
  317. #ifdef CONFIG_CPCI405_6U
  318. if (cpci405_version() == 3) {
  319. volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
  320. volatile unsigned char *leds = (unsigned char *)CFG_LED_ADDR;
  321. /*
  322. * Enable outputs in fpga on version 3 board
  323. */
  324. *fpga_mode |= CFG_FPGA_MODE_ENABLE_OUTPUT;
  325. /*
  326. * Set outputs to 0
  327. */
  328. *leds = 0x00;
  329. /*
  330. * Reset external DUART
  331. */
  332. *fpga_mode |= CFG_FPGA_MODE_DUART_RESET;
  333. udelay(100);
  334. *fpga_mode &= ~(CFG_FPGA_MODE_DUART_RESET);
  335. }
  336. #endif
  337. }
  338. else {
  339. puts("\n*** U-Boot Version does not match Board Version!\n");
  340. puts("*** CPCI-405 Version 1.x detected!\n");
  341. puts("*** Please use correct U-Boot version (CPCI405 instead of CPCI4052)!\n\n");
  342. }
  343. }
  344. #else /* CONFIG_CPCI405_VER2 */
  345. #if 0 /* test-only: code-plug now not relavant for ip-address any more */
  346. /*
  347. * Generate last byte of ip-addr from code-plug @ 0xf0000400
  348. */
  349. if (ctermm2()) {
  350. char str[32];
  351. unsigned char ipbyte = *(unsigned char *)0xf0000400;
  352. /*
  353. * Only overwrite ip-addr with allowed values
  354. */
  355. if ((ipbyte != 0x00) && (ipbyte != 0xff)) {
  356. bd->bi_ip_addr = (bd->bi_ip_addr & 0xffffff00) | ipbyte;
  357. sprintf(str, "%ld.%ld.%ld.%ld",
  358. (bd->bi_ip_addr & 0xff000000) >> 24,
  359. (bd->bi_ip_addr & 0x00ff0000) >> 16,
  360. (bd->bi_ip_addr & 0x0000ff00) >> 8,
  361. (bd->bi_ip_addr & 0x000000ff));
  362. setenv("ipaddr", str);
  363. }
  364. }
  365. #endif
  366. if (cpci405_version() >= 2) {
  367. puts("\n*** U-Boot Version does not match Board Version!\n");
  368. puts("*** CPCI-405 Board Version 2.x detected!\n");
  369. puts("*** Please use correct U-Boot version (CPCI4052 instead of CPCI405)!\n\n");
  370. }
  371. #endif /* CONFIG_CPCI405_VER2 */
  372. /*
  373. * Select cts (and not dsr) on uart1
  374. */
  375. cntrl0Reg = mfdcr(cntrl0);
  376. mtdcr(cntrl0, cntrl0Reg | 0x00001000);
  377. return (0);
  378. }
  379. /*
  380. * Check Board Identity:
  381. */
  382. int checkboard (void)
  383. {
  384. #ifndef CONFIG_CPCI405_VER2
  385. int index;
  386. int len;
  387. #endif
  388. char str[64];
  389. int i = getenv_r ("serial#", str, sizeof(str));
  390. unsigned short ver;
  391. puts ("Board: ");
  392. if (i == -1) {
  393. puts ("### No HW ID - assuming CPCI405");
  394. } else {
  395. puts(str);
  396. }
  397. ver = cpci405_version();
  398. printf(" (Ver %d.x, ", ver);
  399. #if 0 /* test-only */
  400. if (ver >= 2) {
  401. volatile u16 *fpga_status = (u16 *)CFG_FPGA_BASE_ADDR + 1;
  402. if (*fpga_status & CFG_FPGA_STATUS_FLASH) {
  403. puts ("FLASH Bank B, ");
  404. } else {
  405. puts ("FLASH Bank A, ");
  406. }
  407. }
  408. #endif
  409. if (ctermm2()) {
  410. char str[4];
  411. /*
  412. * Read board-id and save in env-variable
  413. */
  414. sprintf(str, "%d", *(unsigned char *)0xf0000400);
  415. setenv("boardid", str);
  416. printf("CTERM-M2 - Id=%s)", str);
  417. } else {
  418. if (cpci405_host()) {
  419. puts ("PCI Host Version)");
  420. } else {
  421. puts ("PCI Adapter Version)");
  422. }
  423. }
  424. #ifndef CONFIG_CPCI405_VER2
  425. puts ("\nFPGA: ");
  426. /* display infos on fpgaimage */
  427. index = 15;
  428. for (i=0; i<4; i++) {
  429. len = fpgadata[index];
  430. printf("%s ", &(fpgadata[index+1]));
  431. index += len+3;
  432. }
  433. #endif
  434. putc ('\n');
  435. return 0;
  436. }
  437. /* ------------------------------------------------------------------------- */
  438. long int initdram (int board_type)
  439. {
  440. unsigned long val;
  441. mtdcr(memcfga, mem_mb0cf);
  442. val = mfdcr(memcfgd);
  443. return (4*1024*1024 << ((val & 0x000e0000) >> 17));
  444. }
  445. void reset_phy(void)
  446. {
  447. #ifdef CONFIG_LXT971_NO_SLEEP
  448. /*
  449. * Disable sleep mode in LXT971
  450. */
  451. lxt971_no_sleep();
  452. #endif
  453. }
  454. /* ------------------------------------------------------------------------- */
  455. #ifdef CONFIG_CPCI405_VER2
  456. #ifdef CONFIG_IDE_RESET
  457. void ide_set_reset(int on)
  458. {
  459. volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
  460. /*
  461. * Assert or deassert CompactFlash Reset Pin
  462. */
  463. if (on) { /* assert RESET */
  464. *fpga_mode &= ~(CFG_FPGA_MODE_CF_RESET);
  465. } else { /* release RESET */
  466. *fpga_mode |= CFG_FPGA_MODE_CF_RESET;
  467. }
  468. }
  469. #endif /* CONFIG_IDE_RESET */
  470. #endif /* CONFIG_CPCI405_VER2 */
  471. #if defined(CONFIG_PCI)
  472. void cpci405_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
  473. {
  474. unsigned char int_line = 0xff;
  475. /*
  476. * Write pci interrupt line register (cpci405 specific)
  477. */
  478. switch (PCI_DEV(dev) & 0x03) {
  479. case 0:
  480. int_line = 27 + 2;
  481. break;
  482. case 1:
  483. int_line = 27 + 3;
  484. break;
  485. case 2:
  486. int_line = 27 + 0;
  487. break;
  488. case 3:
  489. int_line = 27 + 1;
  490. break;
  491. }
  492. pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, int_line);
  493. }
  494. int pci_pre_init(struct pci_controller *hose)
  495. {
  496. hose->fixup_irq = cpci405_pci_fixup_irq;
  497. return 1;
  498. }
  499. #endif /* defined(CONFIG_PCI) */
  500. #ifdef CONFIG_CPCI405AB
  501. #define ONE_WIRE_CLEAR (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
  502. |= CFG_FPGA_MODE_1WIRE_DIR)
  503. #define ONE_WIRE_SET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
  504. &= ~CFG_FPGA_MODE_1WIRE_DIR)
  505. #define ONE_WIRE_GET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_STATUS) \
  506. & CFG_FPGA_MODE_1WIRE)
  507. /*
  508. * Generate a 1-wire reset, return 1 if no presence detect was found,
  509. * return 0 otherwise.
  510. * (NOTE: Does not handle alarm presence from DS2404/DS1994)
  511. */
  512. int OWTouchReset(void)
  513. {
  514. int result;
  515. ONE_WIRE_CLEAR;
  516. udelay(480);
  517. ONE_WIRE_SET;
  518. udelay(70);
  519. result = ONE_WIRE_GET;
  520. udelay(410);
  521. return result;
  522. }
  523. /*
  524. * Send 1 a 1-wire write bit.
  525. * Provide 10us recovery time.
  526. */
  527. void OWWriteBit(int bit)
  528. {
  529. if (bit) {
  530. /*
  531. * write '1' bit
  532. */
  533. ONE_WIRE_CLEAR;
  534. udelay(6);
  535. ONE_WIRE_SET;
  536. udelay(64);
  537. } else {
  538. /*
  539. * write '0' bit
  540. */
  541. ONE_WIRE_CLEAR;
  542. udelay(60);
  543. ONE_WIRE_SET;
  544. udelay(10);
  545. }
  546. }
  547. /*
  548. * Read a bit from the 1-wire bus and return it.
  549. * Provide 10us recovery time.
  550. */
  551. int OWReadBit(void)
  552. {
  553. int result;
  554. ONE_WIRE_CLEAR;
  555. udelay(6);
  556. ONE_WIRE_SET;
  557. udelay(9);
  558. result = ONE_WIRE_GET;
  559. udelay(55);
  560. return result;
  561. }
  562. void OWWriteByte(int data)
  563. {
  564. int loop;
  565. for (loop=0; loop<8; loop++) {
  566. OWWriteBit(data & 0x01);
  567. data >>= 1;
  568. }
  569. }
  570. int OWReadByte(void)
  571. {
  572. int loop, result = 0;
  573. for (loop=0; loop<8; loop++) {
  574. result >>= 1;
  575. if (OWReadBit()) {
  576. result |= 0x80;
  577. }
  578. }
  579. return result;
  580. }
  581. int do_onewire(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  582. {
  583. volatile unsigned short val;
  584. int result;
  585. int i;
  586. unsigned char ow_id[6];
  587. char str[32];
  588. unsigned char ow_crc;
  589. /*
  590. * Clear 1-wire bit (open drain with pull-up)
  591. */
  592. val = *(volatile unsigned short *)0xf0400000;
  593. val &= ~0x1000; /* clear 1-wire bit */
  594. *(volatile unsigned short *)0xf0400000 = val;
  595. result = OWTouchReset();
  596. if (result != 0) {
  597. puts("No 1-wire device detected!\n");
  598. }
  599. OWWriteByte(0x33); /* send read rom command */
  600. OWReadByte(); /* skip family code ( == 0x01) */
  601. for (i=0; i<6; i++) {
  602. ow_id[i] = OWReadByte();
  603. }
  604. ow_crc = OWReadByte(); /* read crc */
  605. sprintf(str, "%08X%04X", *(unsigned int *)&ow_id[0], *(unsigned short *)&ow_id[4]);
  606. printf("Setting environment variable 'ow_id' to %s\n", str);
  607. setenv("ow_id", str);
  608. return 0;
  609. }
  610. U_BOOT_CMD(
  611. onewire, 1, 1, do_onewire,
  612. "onewire - Read 1-write ID\n",
  613. NULL
  614. );
  615. #define CFG_I2C_EEPROM_ADDR_2 0x51 /* EEPROM CAT28WC32 */
  616. #define CFG_ENV_SIZE_2 0x800 /* 2048 bytes may be used for env vars*/
  617. /*
  618. * Write backplane ip-address...
  619. */
  620. int do_get_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  621. {
  622. bd_t *bd = gd->bd;
  623. char *buf;
  624. ulong crc;
  625. char str[32];
  626. char *ptr;
  627. IPaddr_t ipaddr;
  628. buf = malloc(CFG_ENV_SIZE_2);
  629. if (eeprom_read(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CFG_ENV_SIZE_2)) {
  630. puts("\nError reading backplane EEPROM!\n");
  631. } else {
  632. crc = crc32(0, (uchar *)(buf+4), CFG_ENV_SIZE_2-4);
  633. if (crc != *(ulong *)buf) {
  634. printf("ERROR: crc mismatch %08lx %08lx\n", crc, *(ulong *)buf);
  635. return -1;
  636. }
  637. /*
  638. * Find bp_ip
  639. */
  640. ptr = strstr(buf+4, "bp_ip=");
  641. if (ptr == NULL) {
  642. printf("ERROR: bp_ip not found!\n");
  643. return -1;
  644. }
  645. ptr += 6;
  646. ipaddr = string_to_ip(ptr);
  647. /*
  648. * Update whole ip-addr
  649. */
  650. bd->bi_ip_addr = ipaddr;
  651. sprintf(str, "%ld.%ld.%ld.%ld",
  652. (bd->bi_ip_addr & 0xff000000) >> 24,
  653. (bd->bi_ip_addr & 0x00ff0000) >> 16,
  654. (bd->bi_ip_addr & 0x0000ff00) >> 8,
  655. (bd->bi_ip_addr & 0x000000ff));
  656. setenv("ipaddr", str);
  657. printf("Updated ip_addr from bp_eeprom to %s!\n", str);
  658. }
  659. free(buf);
  660. return 0;
  661. }
  662. U_BOOT_CMD(
  663. getbpip, 1, 1, do_get_bpip,
  664. "getbpip - Update IP-Address with Backplane IP-Address\n",
  665. NULL
  666. );
  667. /*
  668. * Set and print backplane ip...
  669. */
  670. int do_set_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  671. {
  672. char *buf;
  673. char str[32];
  674. ulong crc;
  675. if (argc < 2) {
  676. puts("ERROR!\n");
  677. return -1;
  678. }
  679. printf("Setting bp_ip to %s\n", argv[1]);
  680. buf = malloc(CFG_ENV_SIZE_2);
  681. memset(buf, 0, CFG_ENV_SIZE_2);
  682. sprintf(str, "bp_ip=%s", argv[1]);
  683. strcpy(buf+4, str);
  684. crc = crc32(0, (uchar *)(buf+4), CFG_ENV_SIZE_2-4);
  685. *(ulong *)buf = crc;
  686. if (eeprom_write(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CFG_ENV_SIZE_2)) {
  687. puts("\nError writing backplane EEPROM!\n");
  688. }
  689. free(buf);
  690. return 0;
  691. }
  692. U_BOOT_CMD(
  693. setbpip, 2, 1, do_set_bpip,
  694. "setbpip - Write Backplane IP-Address\n",
  695. NULL
  696. );
  697. #endif /* CONFIG_CPCI405AB */