cpci405.c 18 KB

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