cpci405.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  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_r (void)
  236. {
  237. unsigned long cntrl0Reg;
  238. /* adjust flash start and offset */
  239. gd->bd->bi_flashstart = 0 - gd->bd->bi_flashsize;
  240. gd->bd->bi_flashoffset = 0;
  241. #ifdef CONFIG_CPCI405_VER2
  242. {
  243. unsigned char *dst;
  244. ulong len = sizeof(fpgadata);
  245. int status;
  246. int index;
  247. int i;
  248. /*
  249. * On CPCI-405 version 2 the environment is saved in eeprom!
  250. * FPGA can be gzip compressed (malloc) and booted this late.
  251. */
  252. if (cpci405_version() >= 2) {
  253. /*
  254. * Setup GPIO pins (CS6+CS7 as GPIO)
  255. */
  256. cntrl0Reg = mfdcr(cntrl0);
  257. mtdcr(cntrl0, cntrl0Reg | 0x00300000);
  258. dst = malloc(CFG_FPGA_MAX_SIZE);
  259. if (gunzip (dst, CFG_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
  260. printf ("GUNZIP ERROR - must RESET board to recover\n");
  261. do_reset (NULL, 0, 0, NULL);
  262. }
  263. status = fpga_boot(dst, len);
  264. if (status != 0) {
  265. printf("\nFPGA: Booting failed ");
  266. switch (status) {
  267. case ERROR_FPGA_PRG_INIT_LOW:
  268. printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
  269. break;
  270. case ERROR_FPGA_PRG_INIT_HIGH:
  271. printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
  272. break;
  273. case ERROR_FPGA_PRG_DONE:
  274. printf("(Timeout: DONE not high after programming FPGA)\n ");
  275. break;
  276. }
  277. /* display infos on fpgaimage */
  278. index = 15;
  279. for (i=0; i<4; i++) {
  280. len = dst[index];
  281. printf("FPGA: %s\n", &(dst[index+1]));
  282. index += len+3;
  283. }
  284. putc ('\n');
  285. /* delayed reboot */
  286. for (i=20; i>0; i--) {
  287. printf("Rebooting in %2d seconds \r",i);
  288. for (index=0;index<1000;index++)
  289. udelay(1000);
  290. }
  291. putc ('\n');
  292. do_reset(NULL, 0, 0, NULL);
  293. }
  294. /* restore gpio/cs settings */
  295. mtdcr(cntrl0, cntrl0Reg);
  296. puts("FPGA: ");
  297. /* display infos on fpgaimage */
  298. index = 15;
  299. for (i=0; i<4; i++) {
  300. len = dst[index];
  301. printf("%s ", &(dst[index+1]));
  302. index += len+3;
  303. }
  304. putc ('\n');
  305. free(dst);
  306. /*
  307. * Reset FPGA via FPGA_DATA pin
  308. */
  309. SET_FPGA(FPGA_PRG | FPGA_CLK);
  310. udelay(1000); /* wait 1ms */
  311. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
  312. udelay(1000); /* wait 1ms */
  313. #ifdef CONFIG_CPCI405_6U
  314. if (cpci405_version() == 3) {
  315. volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
  316. volatile unsigned char *leds = (unsigned char *)CFG_LED_ADDR;
  317. /*
  318. * Enable outputs in fpga on version 3 board
  319. */
  320. *fpga_mode |= CFG_FPGA_MODE_ENABLE_OUTPUT;
  321. /*
  322. * Set outputs to 0
  323. */
  324. *leds = 0x00;
  325. /*
  326. * Reset external DUART
  327. */
  328. *fpga_mode |= CFG_FPGA_MODE_DUART_RESET;
  329. udelay(100);
  330. *fpga_mode &= ~(CFG_FPGA_MODE_DUART_RESET);
  331. }
  332. #endif
  333. }
  334. else {
  335. puts("\n*** U-Boot Version does not match Board Version!\n");
  336. puts("*** CPCI-405 Version 1.x detected!\n");
  337. puts("*** Please use correct U-Boot version (CPCI405 instead of CPCI4052)!\n\n");
  338. }
  339. }
  340. #else /* CONFIG_CPCI405_VER2 */
  341. #if 0 /* test-only: code-plug now not relavant for ip-address any more */
  342. /*
  343. * Generate last byte of ip-addr from code-plug @ 0xf0000400
  344. */
  345. if (ctermm2()) {
  346. char str[32];
  347. unsigned char ipbyte = *(unsigned char *)0xf0000400;
  348. /*
  349. * Only overwrite ip-addr with allowed values
  350. */
  351. if ((ipbyte != 0x00) && (ipbyte != 0xff)) {
  352. bd->bi_ip_addr = (bd->bi_ip_addr & 0xffffff00) | ipbyte;
  353. sprintf(str, "%ld.%ld.%ld.%ld",
  354. (bd->bi_ip_addr & 0xff000000) >> 24,
  355. (bd->bi_ip_addr & 0x00ff0000) >> 16,
  356. (bd->bi_ip_addr & 0x0000ff00) >> 8,
  357. (bd->bi_ip_addr & 0x000000ff));
  358. setenv("ipaddr", str);
  359. }
  360. }
  361. #endif
  362. if (cpci405_version() >= 2) {
  363. puts("\n*** U-Boot Version does not match Board Version!\n");
  364. puts("*** CPCI-405 Board Version 2.x detected!\n");
  365. puts("*** Please use correct U-Boot version (CPCI4052 instead of CPCI405)!\n\n");
  366. }
  367. #endif /* CONFIG_CPCI405_VER2 */
  368. /*
  369. * Select cts (and not dsr) on uart1
  370. */
  371. cntrl0Reg = mfdcr(cntrl0);
  372. mtdcr(cntrl0, cntrl0Reg | 0x00001000);
  373. return (0);
  374. }
  375. /*
  376. * Check Board Identity:
  377. */
  378. int checkboard (void)
  379. {
  380. #ifndef CONFIG_CPCI405_VER2
  381. int index;
  382. int len;
  383. #endif
  384. char str[64];
  385. int i = getenv_r ("serial#", str, sizeof(str));
  386. unsigned short ver;
  387. puts ("Board: ");
  388. if (i == -1) {
  389. puts ("### No HW ID - assuming CPCI405");
  390. } else {
  391. puts(str);
  392. }
  393. ver = cpci405_version();
  394. printf(" (Ver %d.x, ", ver);
  395. #if 0 /* test-only */
  396. if (ver >= 2) {
  397. volatile u16 *fpga_status = (u16 *)CFG_FPGA_BASE_ADDR + 1;
  398. if (*fpga_status & CFG_FPGA_STATUS_FLASH) {
  399. puts ("FLASH Bank B, ");
  400. } else {
  401. puts ("FLASH Bank A, ");
  402. }
  403. }
  404. #endif
  405. if (ctermm2()) {
  406. char str[4];
  407. /*
  408. * Read board-id and save in env-variable
  409. */
  410. sprintf(str, "%d", *(unsigned char *)0xf0000400);
  411. setenv("boardid", str);
  412. printf("CTERM-M2 - Id=%s)", str);
  413. } else {
  414. if (cpci405_host()) {
  415. puts ("PCI Host Version)");
  416. } else {
  417. puts ("PCI Adapter Version)");
  418. }
  419. }
  420. #ifndef CONFIG_CPCI405_VER2
  421. puts ("\nFPGA: ");
  422. /* display infos on fpgaimage */
  423. index = 15;
  424. for (i=0; i<4; i++) {
  425. len = fpgadata[index];
  426. printf("%s ", &(fpgadata[index+1]));
  427. index += len+3;
  428. }
  429. #endif
  430. putc ('\n');
  431. return 0;
  432. }
  433. void reset_phy(void)
  434. {
  435. #ifdef CONFIG_LXT971_NO_SLEEP
  436. /*
  437. * Disable sleep mode in LXT971
  438. */
  439. lxt971_no_sleep();
  440. #endif
  441. }
  442. #ifdef CONFIG_CPCI405_VER2
  443. #ifdef CONFIG_IDE_RESET
  444. void ide_set_reset(int on)
  445. {
  446. volatile unsigned short *fpga_mode = (unsigned short *)CFG_FPGA_BASE_ADDR;
  447. /*
  448. * Assert or deassert CompactFlash Reset Pin
  449. */
  450. if (on) { /* assert RESET */
  451. *fpga_mode &= ~(CFG_FPGA_MODE_CF_RESET);
  452. } else { /* release RESET */
  453. *fpga_mode |= CFG_FPGA_MODE_CF_RESET;
  454. }
  455. }
  456. #endif /* CONFIG_IDE_RESET */
  457. #endif /* CONFIG_CPCI405_VER2 */
  458. #if defined(CONFIG_PCI)
  459. void cpci405_pci_fixup_irq(struct pci_controller *hose, pci_dev_t dev)
  460. {
  461. unsigned char int_line = 0xff;
  462. /*
  463. * Write pci interrupt line register (cpci405 specific)
  464. */
  465. switch (PCI_DEV(dev) & 0x03) {
  466. case 0:
  467. int_line = 27 + 2;
  468. break;
  469. case 1:
  470. int_line = 27 + 3;
  471. break;
  472. case 2:
  473. int_line = 27 + 0;
  474. break;
  475. case 3:
  476. int_line = 27 + 1;
  477. break;
  478. }
  479. pci_hose_write_config_byte(hose, dev, PCI_INTERRUPT_LINE, int_line);
  480. }
  481. int pci_pre_init(struct pci_controller *hose)
  482. {
  483. hose->fixup_irq = cpci405_pci_fixup_irq;
  484. return 1;
  485. }
  486. #endif /* defined(CONFIG_PCI) */
  487. #ifdef CONFIG_CPCI405AB
  488. #define ONE_WIRE_CLEAR (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
  489. |= CFG_FPGA_MODE_1WIRE_DIR)
  490. #define ONE_WIRE_SET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_MODE) \
  491. &= ~CFG_FPGA_MODE_1WIRE_DIR)
  492. #define ONE_WIRE_GET (*(volatile unsigned short *)(CFG_FPGA_BASE_ADDR + CFG_FPGA_STATUS) \
  493. & CFG_FPGA_MODE_1WIRE)
  494. /*
  495. * Generate a 1-wire reset, return 1 if no presence detect was found,
  496. * return 0 otherwise.
  497. * (NOTE: Does not handle alarm presence from DS2404/DS1994)
  498. */
  499. int OWTouchReset(void)
  500. {
  501. int result;
  502. ONE_WIRE_CLEAR;
  503. udelay(480);
  504. ONE_WIRE_SET;
  505. udelay(70);
  506. result = ONE_WIRE_GET;
  507. udelay(410);
  508. return result;
  509. }
  510. /*
  511. * Send 1 a 1-wire write bit.
  512. * Provide 10us recovery time.
  513. */
  514. void OWWriteBit(int bit)
  515. {
  516. if (bit) {
  517. /*
  518. * write '1' bit
  519. */
  520. ONE_WIRE_CLEAR;
  521. udelay(6);
  522. ONE_WIRE_SET;
  523. udelay(64);
  524. } else {
  525. /*
  526. * write '0' bit
  527. */
  528. ONE_WIRE_CLEAR;
  529. udelay(60);
  530. ONE_WIRE_SET;
  531. udelay(10);
  532. }
  533. }
  534. /*
  535. * Read a bit from the 1-wire bus and return it.
  536. * Provide 10us recovery time.
  537. */
  538. int OWReadBit(void)
  539. {
  540. int result;
  541. ONE_WIRE_CLEAR;
  542. udelay(6);
  543. ONE_WIRE_SET;
  544. udelay(9);
  545. result = ONE_WIRE_GET;
  546. udelay(55);
  547. return result;
  548. }
  549. void OWWriteByte(int data)
  550. {
  551. int loop;
  552. for (loop=0; loop<8; loop++) {
  553. OWWriteBit(data & 0x01);
  554. data >>= 1;
  555. }
  556. }
  557. int OWReadByte(void)
  558. {
  559. int loop, result = 0;
  560. for (loop=0; loop<8; loop++) {
  561. result >>= 1;
  562. if (OWReadBit()) {
  563. result |= 0x80;
  564. }
  565. }
  566. return result;
  567. }
  568. int do_onewire(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  569. {
  570. volatile unsigned short val;
  571. int result;
  572. int i;
  573. unsigned char ow_id[6];
  574. char str[32];
  575. unsigned char ow_crc;
  576. /*
  577. * Clear 1-wire bit (open drain with pull-up)
  578. */
  579. val = *(volatile unsigned short *)0xf0400000;
  580. val &= ~0x1000; /* clear 1-wire bit */
  581. *(volatile unsigned short *)0xf0400000 = val;
  582. result = OWTouchReset();
  583. if (result != 0) {
  584. puts("No 1-wire device detected!\n");
  585. }
  586. OWWriteByte(0x33); /* send read rom command */
  587. OWReadByte(); /* skip family code ( == 0x01) */
  588. for (i=0; i<6; i++) {
  589. ow_id[i] = OWReadByte();
  590. }
  591. ow_crc = OWReadByte(); /* read crc */
  592. sprintf(str, "%08X%04X", *(unsigned int *)&ow_id[0], *(unsigned short *)&ow_id[4]);
  593. printf("Setting environment variable 'ow_id' to %s\n", str);
  594. setenv("ow_id", str);
  595. return 0;
  596. }
  597. U_BOOT_CMD(
  598. onewire, 1, 1, do_onewire,
  599. "onewire - Read 1-write ID\n",
  600. NULL
  601. );
  602. #define CFG_I2C_EEPROM_ADDR_2 0x51 /* EEPROM CAT28WC32 */
  603. #define CONFIG_ENV_SIZE_2 0x800 /* 2048 bytes may be used for env vars*/
  604. /*
  605. * Write backplane ip-address...
  606. */
  607. int do_get_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  608. {
  609. bd_t *bd = gd->bd;
  610. char *buf;
  611. ulong crc;
  612. char str[32];
  613. char *ptr;
  614. IPaddr_t ipaddr;
  615. buf = malloc(CONFIG_ENV_SIZE_2);
  616. if (eeprom_read(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CONFIG_ENV_SIZE_2)) {
  617. puts("\nError reading backplane EEPROM!\n");
  618. } else {
  619. crc = crc32(0, (uchar *)(buf+4), CONFIG_ENV_SIZE_2-4);
  620. if (crc != *(ulong *)buf) {
  621. printf("ERROR: crc mismatch %08lx %08lx\n", crc, *(ulong *)buf);
  622. return -1;
  623. }
  624. /*
  625. * Find bp_ip
  626. */
  627. ptr = strstr(buf+4, "bp_ip=");
  628. if (ptr == NULL) {
  629. printf("ERROR: bp_ip not found!\n");
  630. return -1;
  631. }
  632. ptr += 6;
  633. ipaddr = string_to_ip(ptr);
  634. /*
  635. * Update whole ip-addr
  636. */
  637. bd->bi_ip_addr = ipaddr;
  638. sprintf(str, "%ld.%ld.%ld.%ld",
  639. (bd->bi_ip_addr & 0xff000000) >> 24,
  640. (bd->bi_ip_addr & 0x00ff0000) >> 16,
  641. (bd->bi_ip_addr & 0x0000ff00) >> 8,
  642. (bd->bi_ip_addr & 0x000000ff));
  643. setenv("ipaddr", str);
  644. printf("Updated ip_addr from bp_eeprom to %s!\n", str);
  645. }
  646. free(buf);
  647. return 0;
  648. }
  649. U_BOOT_CMD(
  650. getbpip, 1, 1, do_get_bpip,
  651. "getbpip - Update IP-Address with Backplane IP-Address\n",
  652. NULL
  653. );
  654. /*
  655. * Set and print backplane ip...
  656. */
  657. int do_set_bpip(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  658. {
  659. char *buf;
  660. char str[32];
  661. ulong crc;
  662. if (argc < 2) {
  663. puts("ERROR!\n");
  664. return -1;
  665. }
  666. printf("Setting bp_ip to %s\n", argv[1]);
  667. buf = malloc(CONFIG_ENV_SIZE_2);
  668. memset(buf, 0, CONFIG_ENV_SIZE_2);
  669. sprintf(str, "bp_ip=%s", argv[1]);
  670. strcpy(buf+4, str);
  671. crc = crc32(0, (uchar *)(buf+4), CONFIG_ENV_SIZE_2-4);
  672. *(ulong *)buf = crc;
  673. if (eeprom_write(CFG_I2C_EEPROM_ADDR_2, 0, (uchar *)buf, CONFIG_ENV_SIZE_2)) {
  674. puts("\nError writing backplane EEPROM!\n");
  675. }
  676. free(buf);
  677. return 0;
  678. }
  679. U_BOOT_CMD(
  680. setbpip, 2, 1, do_set_bpip,
  681. "setbpip - Write Backplane IP-Address\n",
  682. NULL
  683. );
  684. #endif /* CONFIG_CPCI405AB */