pci405.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * (C) Copyright 2001-2004
  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 <command.h>
  26. #include <malloc.h>
  27. #include <pci.h>
  28. #include <405gp_pci.h>
  29. #include "pci405.h"
  30. /* Prototypes */
  31. int gunzip(void *, int, unsigned char *, unsigned long *);
  32. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);/*cmd_boot.c*/
  33. unsigned long fpga_done_state(void);
  34. unsigned long fpga_init_state(void);
  35. #if 0
  36. #define FPGA_DEBUG
  37. #endif
  38. /* predefine these here */
  39. #define FPGA_DONE_STATE (fpga_done_state())
  40. #define FPGA_INIT_STATE (fpga_init_state())
  41. /* fpga configuration data - generated by bin2cc */
  42. const unsigned char fpgadata[] =
  43. {
  44. #include "fpgadata.c"
  45. };
  46. /*
  47. * include common fpga code (for esd boards)
  48. */
  49. #include "../common/fpga.c"
  50. #define FPGA_DONE_STATE_V11 (in32(GPIO0_IR) & CFG_FPGA_DONE)
  51. #define FPGA_DONE_STATE_V12 (in32(GPIO0_IR) & CFG_FPGA_DONE_V12)
  52. #define FPGA_INIT_STATE_V11 (in32(GPIO0_IR) & CFG_FPGA_INIT)
  53. #define FPGA_INIT_STATE_V12 (in32(GPIO0_IR) & CFG_FPGA_INIT_V12)
  54. int board_revision(void)
  55. {
  56. unsigned long cntrl0Reg;
  57. unsigned long value;
  58. /*
  59. * Get version of PCI405 board from GPIO's
  60. */
  61. /*
  62. * Setup GPIO pins (CS2/GPIO11 and CS3/GPIO12 as GPIO)
  63. */
  64. cntrl0Reg = mfdcr(cntrl0);
  65. mtdcr(cntrl0, cntrl0Reg | 0x03000000);
  66. out32(GPIO0_ODR, in32(GPIO0_ODR) & ~0x00100200);
  67. out32(GPIO0_TCR, in32(GPIO0_TCR) & ~0x00100200);
  68. udelay(1000); /* wait some time before reading input */
  69. value = in32(GPIO0_IR) & 0x00100200; /* get config bits */
  70. /*
  71. * Restore GPIO settings
  72. */
  73. mtdcr(cntrl0, cntrl0Reg);
  74. switch (value) {
  75. case 0x00100200:
  76. /* CS2==1 && IRQ5==1 -> version 1.0 and 1.1 */
  77. return 1;
  78. case 0x00000200:
  79. /* CS2==0 && IRQ5==1 -> version 1.2 */
  80. return 2;
  81. case 0x00000000:
  82. /* CS2==0 && IRQ5==0 -> version 1.3 */
  83. return 3;
  84. #if 0 /* not yet manufactured ! */
  85. case 0x00100000:
  86. /* CS2==1 && IRQ5==0 -> version 1.4 */
  87. return 4;
  88. #endif
  89. default:
  90. /* should not be reached! */
  91. return 0;
  92. }
  93. }
  94. unsigned long fpga_done_state(void)
  95. {
  96. DECLARE_GLOBAL_DATA_PTR;
  97. if (gd->board_type < 2) {
  98. return FPGA_DONE_STATE_V11;
  99. } else {
  100. return FPGA_DONE_STATE_V12;
  101. }
  102. }
  103. unsigned long fpga_init_state(void)
  104. {
  105. DECLARE_GLOBAL_DATA_PTR;
  106. if (gd->board_type < 2) {
  107. return FPGA_INIT_STATE_V11;
  108. } else {
  109. return FPGA_INIT_STATE_V12;
  110. }
  111. }
  112. int board_early_init_f (void)
  113. {
  114. unsigned long cntrl0Reg;
  115. /*
  116. * First pull fpga-prg pin low, to disable fpga logic (on version 1.2 board)
  117. */
  118. out32(GPIO0_ODR, 0x00000000); /* no open drain pins */
  119. out32(GPIO0_TCR, CFG_FPGA_PRG); /* setup for output */
  120. out32(GPIO0_OR, CFG_FPGA_PRG); /* set output pins to high */
  121. out32(GPIO0_OR, 0); /* pull prg low */
  122. /*
  123. * IRQ 0-15 405GP internally generated; active high; level sensitive
  124. * IRQ 16 405GP internally generated; active low; level sensitive
  125. * IRQ 17-24 RESERVED
  126. * IRQ 25 (EXT IRQ 0) CAN0; active low; level sensitive
  127. * IRQ 26 (EXT IRQ 1) CAN1; active low; level sensitive
  128. * IRQ 27 (EXT IRQ 2) CAN2; active low; level sensitive
  129. * IRQ 28 (EXT IRQ 3) CAN3; active low; level sensitive
  130. * IRQ 29 (EXT IRQ 4) unused; active low; level sensitive
  131. * IRQ 30 (EXT IRQ 5) FPGA Timestamp; active low; level sensitive
  132. * IRQ 31 (EXT IRQ 6) PCI Reset; active low; level sensitive
  133. */
  134. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  135. mtdcr(uicer, 0x00000000); /* disable all ints */
  136. mtdcr(uiccr, 0x00000000); /* set all to be non-critical*/
  137. mtdcr(uicpr, 0xFFFFFF80); /* set int polarities */
  138. mtdcr(uictr, 0x10000000); /* set int trigger levels */
  139. mtdcr(uicvcr, 0x00000001); /* set vect base=0,INT0 highest priority*/
  140. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  141. /*
  142. * Setup GPIO pins (IRQ4/GPIO21 as GPIO)
  143. */
  144. cntrl0Reg = mfdcr(cntrl0);
  145. mtdcr(cntrl0, cntrl0Reg | 0x00008000);
  146. /*
  147. * Setup GPIO pins (CS6+CS7 as GPIO)
  148. */
  149. mtdcr(cntrl0, cntrl0Reg | 0x00300000);
  150. /*
  151. * EBC Configuration Register: set ready timeout to 512 ebc-clks -> ca. 25 us
  152. */
  153. mtebc (epcr, 0xa8400000); /* ebc always driven */
  154. return 0;
  155. }
  156. /* ------------------------------------------------------------------------- */
  157. int misc_init_f (void)
  158. {
  159. return 0; /* dummy implementation */
  160. }
  161. int misc_init_r (void)
  162. {
  163. unsigned char *dst;
  164. ulong len = sizeof(fpgadata);
  165. int status;
  166. int index;
  167. int i;
  168. unsigned int *ptr;
  169. unsigned int *magic;
  170. /*
  171. * On PCI-405 the environment is saved in eeprom!
  172. * FPGA can be gzip compressed (malloc) and booted this late.
  173. */
  174. dst = malloc(CFG_FPGA_MAX_SIZE);
  175. if (gunzip (dst, CFG_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
  176. printf ("GUNZIP ERROR - must RESET board to recover\n");
  177. do_reset (NULL, 0, 0, NULL);
  178. }
  179. status = fpga_boot(dst, len);
  180. if (status != 0) {
  181. printf("\nFPGA: Booting failed ");
  182. switch (status) {
  183. case ERROR_FPGA_PRG_INIT_LOW:
  184. printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
  185. break;
  186. case ERROR_FPGA_PRG_INIT_HIGH:
  187. printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
  188. break;
  189. case ERROR_FPGA_PRG_DONE:
  190. printf("(Timeout: DONE not high after programming FPGA)\n ");
  191. break;
  192. }
  193. /* display infos on fpgaimage */
  194. index = 15;
  195. for (i=0; i<4; i++) {
  196. len = dst[index];
  197. printf("FPGA: %s\n", &(dst[index+1]));
  198. index += len+3;
  199. }
  200. putc ('\n');
  201. /* delayed reboot */
  202. for (i=20; i>0; i--) {
  203. printf("Rebooting in %2d seconds \r",i);
  204. for (index=0;index<1000;index++)
  205. udelay(1000);
  206. }
  207. putc ('\n');
  208. do_reset(NULL, 0, 0, NULL);
  209. }
  210. puts("FPGA: ");
  211. /* display infos on fpgaimage */
  212. index = 15;
  213. for (i=0; i<4; i++) {
  214. len = dst[index];
  215. printf("%s ", &(dst[index+1]));
  216. index += len+3;
  217. }
  218. putc ('\n');
  219. /*
  220. * Reset FPGA via FPGA_DATA pin
  221. */
  222. SET_FPGA(FPGA_PRG | FPGA_CLK);
  223. udelay(1000); /* wait 1ms */
  224. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
  225. udelay(1000); /* wait 1ms */
  226. /*
  227. * Check if magic for pci reconfig is written
  228. */
  229. magic = (unsigned int *)0x00000004;
  230. if (*magic == PCI_RECONFIG_MAGIC) {
  231. /*
  232. * Rewrite pci config regs (only after soft-reset with magic set)
  233. */
  234. ptr = (unsigned int *)PCI_REGS_ADDR;
  235. if (crc32(0, (uchar *)PCI_REGS_ADDR+4, PCI_REGS_LEN-4) == *ptr) {
  236. puts("Restoring PCI Configurations Regs!\n");
  237. ptr = (unsigned int *)PCI_REGS_ADDR + 1;
  238. for (i=0; i<0x40; i+=4) {
  239. pci_write_config_dword(PCIDEVID_405GP, i, *ptr++);
  240. }
  241. }
  242. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  243. *magic = 0; /* clear pci reconfig magic again */
  244. }
  245. #if 1 /* test-only */
  246. /*
  247. * Decrease PLB latency timeout and reduce priority of the PCI bridge master
  248. */
  249. #define PCI0_BRDGOPT1 0x4a
  250. pci_write_config_word(PCIDEVID_405GP, PCI0_BRDGOPT1, 0x3f20);
  251. /* pci_write_config_word(PCIDEVID_405GP, PCI0_BRDGOPT1, 0x3f60); */
  252. #define plb0_acr 0x87
  253. /*
  254. * Enable fairness and high bus utilization
  255. */
  256. mtdcr(plb0_acr, 0x98000000);
  257. #if 0 /* test-only */
  258. printf("CCR0=%08x\n", mfspr(ccr0)); /* test-only */
  259. /* mtspr(ccr0, (mfspr(ccr0) & 0xff8fffff) | 0x00100000); */
  260. mtspr(ccr0, (mfspr(ccr0) & 0xff8fffff) | 0x00000000);
  261. #endif
  262. /* printf("CCR0=%08x\n", mfspr(ccr0)); */ /* test-only */
  263. #endif
  264. free(dst);
  265. return (0);
  266. }
  267. /*
  268. * Check Board Identity:
  269. */
  270. int checkboard (void)
  271. {
  272. DECLARE_GLOBAL_DATA_PTR;
  273. char str[64];
  274. int i = getenv_r ("serial#", str, sizeof(str));
  275. puts ("Board: ");
  276. if (i == -1) {
  277. puts ("### No HW ID - assuming PCI405");
  278. } else {
  279. puts (str);
  280. }
  281. gd->board_type = board_revision();
  282. printf(" (Rev 1.%ld", gd->board_type);
  283. if (gd->board_type >= 2) {
  284. unsigned long cntrl0Reg;
  285. unsigned long value;
  286. /*
  287. * Setup GPIO pins (Trace/GPIO1 to GPIO)
  288. */
  289. cntrl0Reg = mfdcr(cntrl0);
  290. mtdcr(cntrl0, cntrl0Reg & ~0x08000000);
  291. out32(GPIO0_ODR, in32(GPIO0_ODR) & ~0x40000000);
  292. out32(GPIO0_TCR, in32(GPIO0_TCR) & ~0x40000000);
  293. udelay(1000); /* wait some time before reading input */
  294. value = in32(GPIO0_IR) & 0x40000000; /* get config bits */
  295. if (value) {
  296. puts(", 33 MHz PCI");
  297. } else {
  298. puts(", 66 Mhz PCI");
  299. }
  300. }
  301. puts(")\n");
  302. return 0;
  303. }
  304. /* ------------------------------------------------------------------------- */
  305. long int initdram (int board_type)
  306. {
  307. unsigned long val;
  308. mtdcr(memcfga, mem_mb0cf);
  309. val = mfdcr(memcfgd);
  310. #if 0
  311. printf("\nmb0cf=%x\n", val); /* test-only */
  312. printf("strap=%x\n", mfdcr(strap)); /* test-only */
  313. #endif
  314. #if 0 /* test-only: all PCI405 version must report 16mb */
  315. return (4*1024*1024 << ((val & 0x000e0000) >> 17));
  316. #else
  317. return (16*1024*1024);
  318. #endif
  319. }
  320. /* ------------------------------------------------------------------------- */
  321. int testdram (void)
  322. {
  323. /* TODO: XXX XXX XXX */
  324. printf ("test: 16 MB - ok\n");
  325. return (0);
  326. }
  327. /* ------------------------------------------------------------------------- */
  328. int wpeeprom(int wp)
  329. {
  330. int wp_state = wp;
  331. volatile unsigned char *uart1_mcr = (volatile unsigned char *)0xef600404;
  332. if (wp == 1) {
  333. *uart1_mcr &= ~0x02;
  334. } else if (wp == 0) {
  335. *uart1_mcr |= 0x02;
  336. } else {
  337. if (*uart1_mcr & 0x02) {
  338. wp_state = 0;
  339. } else {
  340. wp_state = 1;
  341. }
  342. }
  343. return wp_state;
  344. }
  345. int do_wpeeprom(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  346. {
  347. int wp = -1;
  348. if (argc >= 2) {
  349. if (argv[1][0] == '1') {
  350. wp = 1;
  351. } else if (argv[1][0] == '0') {
  352. wp = 0;
  353. }
  354. }
  355. wp = wpeeprom(wp);
  356. printf("EEPROM write protection %s\n", wp ? "ENABLED" : "DISABLED");
  357. return 0;
  358. }
  359. U_BOOT_CMD(
  360. wpeeprom, 2, 1, do_wpeeprom,
  361. "wpeeprom - Check/Enable/Disable I2C EEPROM write protection\n",
  362. "wpeeprom\n"
  363. " - check I2C EEPROM write protection state\n"
  364. "wpeeprom 1\n"
  365. " - enable I2C EEPROM write protection\n"
  366. "wpeeprom 0\n"
  367. " - disable I2C EEPROM write protection\n"
  368. );