cpci405.c 18 KB

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