pci405.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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 <asm/4xx_pci.h>
  29. #include <asm/io.h>
  30. #include "pci405.h"
  31. DECLARE_GLOBAL_DATA_PTR;
  32. /* Prototypes */
  33. int gunzip(void *, int, unsigned char *, unsigned long *);
  34. int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
  35. unsigned long fpga_done_state(void);
  36. unsigned long fpga_init_state(void);
  37. #if 0
  38. #define FPGA_DEBUG
  39. #endif
  40. /* predefine these here */
  41. #define FPGA_DONE_STATE (fpga_done_state())
  42. #define FPGA_INIT_STATE (fpga_init_state())
  43. /* fpga configuration data - generated by bin2cc */
  44. const unsigned char fpgadata[] =
  45. {
  46. #include "fpgadata.c"
  47. };
  48. /*
  49. * include common fpga code (for esd boards)
  50. */
  51. #include "../common/fpga.c"
  52. #define FPGA_DONE_STATE_V11 (in_be32((void*)GPIO0_IR) & CONFIG_SYS_FPGA_DONE)
  53. #define FPGA_DONE_STATE_V12 (in_be32((void*)GPIO0_IR) & CONFIG_SYS_FPGA_DONE_V12)
  54. #define FPGA_INIT_STATE_V11 (in_be32((void*)GPIO0_IR) & CONFIG_SYS_FPGA_INIT)
  55. #define FPGA_INIT_STATE_V12 (in_be32((void*)GPIO0_IR) & CONFIG_SYS_FPGA_INIT_V12)
  56. int board_revision(void)
  57. {
  58. unsigned long cntrl0Reg;
  59. unsigned long value;
  60. /*
  61. * Get version of PCI405 board from GPIO's
  62. */
  63. /*
  64. * Setup GPIO pins (CS2/GPIO11 and CS3/GPIO12 as GPIO)
  65. */
  66. cntrl0Reg = mfdcr(cntrl0);
  67. mtdcr(cntrl0, cntrl0Reg | 0x03000000);
  68. out_be32((void*)GPIO0_ODR, in_be32((void*)GPIO0_ODR) & ~0x00100200);
  69. out_be32((void*)GPIO0_TCR, in_be32((void*)GPIO0_TCR) & ~0x00100200);
  70. udelay(1000); /* wait some time before reading input */
  71. value = in_be32((void*)GPIO0_IR) & 0x00100200; /* get config bits */
  72. /*
  73. * Restore GPIO settings
  74. */
  75. mtdcr(cntrl0, cntrl0Reg);
  76. switch (value) {
  77. case 0x00100200:
  78. /* CS2==1 && IRQ5==1 -> version 1.0 and 1.1 */
  79. return 1;
  80. case 0x00000200:
  81. /* CS2==0 && IRQ5==1 -> version 1.2 */
  82. return 2;
  83. case 0x00000000:
  84. /* CS2==0 && IRQ5==0 -> version 1.3 */
  85. return 3;
  86. #if 0 /* not yet manufactured ! */
  87. case 0x00100000:
  88. /* CS2==1 && IRQ5==0 -> version 1.4 */
  89. return 4;
  90. #endif
  91. default:
  92. /* should not be reached! */
  93. return 0;
  94. }
  95. }
  96. unsigned long fpga_done_state(void)
  97. {
  98. if (gd->board_type < 2) {
  99. return FPGA_DONE_STATE_V11;
  100. } else {
  101. return FPGA_DONE_STATE_V12;
  102. }
  103. }
  104. unsigned long fpga_init_state(void)
  105. {
  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. out_be32((void*)GPIO0_ODR, 0x00000000); /* no open drain pins */
  119. out_be32((void*)GPIO0_TCR, CONFIG_SYS_FPGA_PRG); /* setup for output */
  120. out_be32((void*)GPIO0_OR, CONFIG_SYS_FPGA_PRG); /* set output pins to high */
  121. out_be32((void*)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. int misc_init_r (void)
  157. {
  158. unsigned char *dst;
  159. ulong len = sizeof(fpgadata);
  160. int status;
  161. int index;
  162. int i;
  163. unsigned int *ptr;
  164. unsigned int *magic;
  165. /*
  166. * On PCI-405 the environment is saved in eeprom!
  167. * FPGA can be gzip compressed (malloc) and booted this late.
  168. */
  169. dst = malloc(CONFIG_SYS_FPGA_MAX_SIZE);
  170. if (gunzip (dst, CONFIG_SYS_FPGA_MAX_SIZE, (uchar *)fpgadata, &len) != 0) {
  171. printf ("GUNZIP ERROR - must RESET board to recover\n");
  172. do_reset (NULL, 0, 0, NULL);
  173. }
  174. status = fpga_boot(dst, len);
  175. if (status != 0) {
  176. printf("\nFPGA: Booting failed ");
  177. switch (status) {
  178. case ERROR_FPGA_PRG_INIT_LOW:
  179. printf("(Timeout: INIT not low after asserting PROGRAM*)\n ");
  180. break;
  181. case ERROR_FPGA_PRG_INIT_HIGH:
  182. printf("(Timeout: INIT not high after deasserting PROGRAM*)\n ");
  183. break;
  184. case ERROR_FPGA_PRG_DONE:
  185. printf("(Timeout: DONE not high after programming FPGA)\n ");
  186. break;
  187. }
  188. /* display infos on fpgaimage */
  189. index = 15;
  190. for (i=0; i<4; i++) {
  191. len = dst[index];
  192. printf("FPGA: %s\n", &(dst[index+1]));
  193. index += len+3;
  194. }
  195. putc ('\n');
  196. /* delayed reboot */
  197. for (i=20; i>0; i--) {
  198. printf("Rebooting in %2d seconds \r",i);
  199. for (index=0;index<1000;index++)
  200. udelay(1000);
  201. }
  202. putc ('\n');
  203. do_reset(NULL, 0, 0, NULL);
  204. }
  205. puts("FPGA: ");
  206. /* display infos on fpgaimage */
  207. index = 15;
  208. for (i=0; i<4; i++) {
  209. len = dst[index];
  210. printf("%s ", &(dst[index+1]));
  211. index += len+3;
  212. }
  213. putc ('\n');
  214. /*
  215. * Reset FPGA via FPGA_DATA pin
  216. */
  217. SET_FPGA(FPGA_PRG | FPGA_CLK);
  218. udelay(1000); /* wait 1ms */
  219. SET_FPGA(FPGA_PRG | FPGA_CLK | FPGA_DATA);
  220. udelay(1000); /* wait 1ms */
  221. /*
  222. * Check if magic for pci reconfig is written
  223. */
  224. magic = (unsigned int *)0x00000004;
  225. if (*magic == PCI_RECONFIG_MAGIC) {
  226. /*
  227. * Rewrite pci config regs (only after soft-reset with magic set)
  228. */
  229. ptr = (unsigned int *)PCI_REGS_ADDR;
  230. if (crc32(0, (uchar *)PCI_REGS_ADDR+4, PCI_REGS_LEN-4) == *ptr) {
  231. puts("Restoring PCI Configurations Regs!\n");
  232. ptr = (unsigned int *)PCI_REGS_ADDR + 1;
  233. for (i=0; i<0x40; i+=4) {
  234. pci_write_config_dword(PCIDEVID_405GP, i, *ptr++);
  235. }
  236. }
  237. mtdcr(uicsr, 0xFFFFFFFF); /* clear all ints */
  238. *magic = 0; /* clear pci reconfig magic again */
  239. }
  240. /*
  241. * Decrease PLB latency timeout and reduce priority of the PCI bridge master
  242. */
  243. #define PCI0_BRDGOPT1 0x4a
  244. pci_write_config_word(PCIDEVID_405GP, PCI0_BRDGOPT1, 0x3f20);
  245. #define plb0_acr 0x87
  246. /*
  247. * Enable fairness and high bus utilization
  248. */
  249. mtdcr(plb0_acr, 0x98000000);
  250. free(dst);
  251. return (0);
  252. }
  253. /*
  254. * Check Board Identity:
  255. */
  256. int checkboard (void)
  257. {
  258. char str[64];
  259. int i = getenv_r ("serial#", str, sizeof(str));
  260. puts ("Board: ");
  261. if (i == -1) {
  262. puts ("### No HW ID - assuming PCI405");
  263. } else {
  264. puts (str);
  265. }
  266. gd->board_type = board_revision();
  267. printf(" (Rev 1.%ld", gd->board_type);
  268. if (gd->board_type >= 2) {
  269. unsigned long cntrl0Reg;
  270. unsigned long value;
  271. /*
  272. * Setup GPIO pins (Trace/GPIO1 to GPIO)
  273. */
  274. cntrl0Reg = mfdcr(cntrl0);
  275. mtdcr(cntrl0, cntrl0Reg & ~0x08000000);
  276. out_be32((void*)GPIO0_ODR, in_be32((void*)GPIO0_ODR) & ~0x40000000);
  277. out_be32((void*)GPIO0_TCR, in_be32((void*)GPIO0_TCR) & ~0x40000000);
  278. udelay(1000); /* wait some time before reading input */
  279. value = in_be32((void*)GPIO0_IR) & 0x40000000; /* get config bits */
  280. if (value) {
  281. puts(", 33 MHz PCI");
  282. } else {
  283. puts(", 66 MHz PCI");
  284. }
  285. }
  286. puts(")\n");
  287. return 0;
  288. }
  289. /* ------------------------------------------------------------------------- */
  290. #define UART1_MCR 0xef600404
  291. int wpeeprom(int wp)
  292. {
  293. int wp_state = wp;
  294. if (wp == 1) {
  295. out_8((void *)UART1_MCR, in_8((void *)UART1_MCR) & ~0x02);
  296. } else if (wp == 0) {
  297. out_8((void *)UART1_MCR, in_8((void *)UART1_MCR) | 0x02);
  298. } else {
  299. if (in_8((void *)UART1_MCR) & 0x02) {
  300. wp_state = 0;
  301. } else {
  302. wp_state = 1;
  303. }
  304. }
  305. return wp_state;
  306. }
  307. int do_wpeeprom(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  308. {
  309. int wp = -1;
  310. if (argc >= 2) {
  311. if (argv[1][0] == '1') {
  312. wp = 1;
  313. } else if (argv[1][0] == '0') {
  314. wp = 0;
  315. }
  316. }
  317. wp = wpeeprom(wp);
  318. printf("EEPROM write protection %s\n", wp ? "ENABLED" : "DISABLED");
  319. return 0;
  320. }
  321. U_BOOT_CMD(
  322. wpeeprom, 2, 1, do_wpeeprom,
  323. "Check/Enable/Disable I2C EEPROM write protection",
  324. "wpeeprom\n"
  325. " - check I2C EEPROM write protection state\n"
  326. "wpeeprom 1\n"
  327. " - enable I2C EEPROM write protection\n"
  328. "wpeeprom 0\n"
  329. " - disable I2C EEPROM write protection\n"
  330. );