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