cpld-ap325rxa.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /***************************************************************
  2. * Project:
  3. * CPLD SlaveSerial Configuration via embedded microprocessor.
  4. *
  5. * Copyright info:
  6. *
  7. * This is free software; you can redistribute it and/or modify
  8. * it as you like.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. *
  14. * Description:
  15. *
  16. * This is the main source file that will allow a microprocessor
  17. * to configure Xilinx Virtex, Virtex-E, Virtex-EM, Virtex-II,
  18. * and Spartan-II devices via the SlaveSerial Configuration Mode.
  19. * This code is discussed in Xilinx Application Note, XAPP502.
  20. *
  21. * History:
  22. * 3-October-2001 MN/MP - Created
  23. * 20-August-2008 Renesas Solutions - Modified to SH7723
  24. ****************************************************************/
  25. #include <common.h>
  26. /* Serial */
  27. #define SCIF_BASE 0xffe00000 /* SCIF0 */
  28. #define SCSMR (vu_short *)(SCIF_BASE + 0x00)
  29. #define SCBRR (vu_char *)(SCIF_BASE + 0x04)
  30. #define SCSCR (vu_short *)(SCIF_BASE + 0x08)
  31. #define SC_TDR (vu_char *)(SCIF_BASE + 0x0C)
  32. #define SC_SR (vu_short *)(SCIF_BASE + 0x10)
  33. #define SCFCR (vu_short *)(SCIF_BASE + 0x18)
  34. #define RFCR (vu_long *)0xFE400020
  35. #define SCSCR_INIT 0x0038
  36. #define SCSCR_CLR 0x0000
  37. #define SCFCR_INIT 0x0006
  38. #define SCSMR_INIT 0x0080
  39. #define RFCR_CLR 0xA400
  40. #define SCI_TD_E 0x0020
  41. #define SCI_TDRE_CLEAR 0x00df
  42. #define BPS_SETTING_VALUE 1 /* 12.5MHz */
  43. #define WAIT_RFCR_COUNTER 500
  44. /* CPLD data size */
  45. #define CPLD_DATA_SIZE 169216
  46. /* out */
  47. #define CPLD_PFC_ADR ((vu_short *)0xA4050112)
  48. #define CPLD_PROG_ADR ((vu_char *)0xA4050132)
  49. #define CPLD_PROG_DAT 0x80
  50. /* in */
  51. #define CPLD_INIT_ADR ((vu_char *)0xA4050132)
  52. #define CPLD_INIT_DAT 0x40
  53. #define CPLD_DONE_ADR ((vu_char *)0xA4050132)
  54. #define CPLD_DONE_DAT 0x20
  55. #define HIZCRB ((vu_short *)0xA405015A)
  56. /* data */
  57. #define CPLD_NOMAL_START 0xA0A80000
  58. #define CPLD_SAFE_START 0xA0AC0000
  59. #define MODE_SW (vu_char *)0xA405012A
  60. static void init_cpld_loader(void)
  61. {
  62. *SCSCR = SCSCR_CLR;
  63. *SCFCR = SCFCR_INIT;
  64. *SCSMR = SCSMR_INIT;
  65. *SCBRR = BPS_SETTING_VALUE;
  66. *RFCR = RFCR_CLR; /* Refresh counter clear */
  67. while (*RFCR < WAIT_RFCR_COUNTER)
  68. ;
  69. *SCFCR = 0x0; /* RTRG=00, TTRG=00 */
  70. /* MCE=0,TFRST=0,RFRST=0,LOOP=0 */
  71. *SCSCR = SCSCR_INIT;
  72. }
  73. static int check_write_ready(void)
  74. {
  75. u16 status = *SC_SR;
  76. return status & SCI_TD_E;
  77. }
  78. static void write_cpld_data(char ch)
  79. {
  80. while (!check_write_ready())
  81. ;
  82. *SC_TDR = ch;
  83. *SC_SR;
  84. *SC_SR = SCI_TDRE_CLEAR;
  85. }
  86. static int delay(void)
  87. {
  88. int i;
  89. int c = 0;
  90. for (i = 0; i < 200; i++) {
  91. c = *(volatile int *)0xa0000000;
  92. }
  93. return c;
  94. }
  95. /***********************************************************************
  96. *
  97. * Function: slave_serial
  98. *
  99. * Description: Initiates SlaveSerial Configuration.
  100. * Calls ShiftDataOut() to output serial data
  101. *
  102. ***********************************************************************/
  103. static void slave_serial(void)
  104. {
  105. int i;
  106. unsigned char *flash;
  107. *CPLD_PROG_ADR |= CPLD_PROG_DAT; /* PROGRAM_OE HIGH */
  108. delay();
  109. /*
  110. * Toggle Program Pin by Toggling Program_OE bit
  111. * This is accomplished by writing to the Program Register in the CPLD
  112. *
  113. * NOTE: The Program_OE bit should be driven high to bring the Virtex
  114. * Program Pin low. Likewise, it should be driven low
  115. * to bring the Virtex Program Pin to High-Z
  116. */
  117. *CPLD_PROG_ADR &= ~CPLD_PROG_DAT; /* PROGRAM_OE LOW */
  118. delay();
  119. /*
  120. * Bring Program High-Z
  121. * (Drive Program_OE bit low to bring Virtex Program Pin to High-Z
  122. */
  123. /* Program_OE bit Low brings the Virtex Program Pin to High Z: */
  124. *CPLD_PROG_ADR |= CPLD_PROG_DAT; /* PROGRAM_OE HIGH */
  125. while ((*CPLD_INIT_ADR & CPLD_INIT_DAT) == 0)
  126. delay();
  127. /* Begin Slave-Serial Configuration */
  128. flash = (unsigned char *)CPLD_NOMAL_START;
  129. for (i = 0; i < CPLD_DATA_SIZE; i++)
  130. write_cpld_data(*flash++);
  131. }
  132. /***********************************************************************
  133. *
  134. * Function: check_done_bit
  135. *
  136. * Description: This function takes monitors the CPLD Input Register
  137. * by checking the status of the DONE bit in that Register.
  138. * By doing so, it monitors the Xilinx Virtex device's DONE
  139. * Pin to see if configuration bitstream has been properly
  140. * loaded
  141. *
  142. ***********************************************************************/
  143. static void check_done_bit(void)
  144. {
  145. while (!(*CPLD_DONE_ADR & CPLD_DONE_DAT))
  146. ;
  147. }
  148. /***********************************************************************
  149. *
  150. * Function: init_cpld
  151. *
  152. * Description: Begins Slave Serial configuration of Xilinx FPGA
  153. *
  154. ***********************************************************************/
  155. void init_cpld(void)
  156. {
  157. /* Init serial device */
  158. init_cpld_loader();
  159. if (*CPLD_DONE_ADR & CPLD_DONE_DAT) /* Already DONE */
  160. return;
  161. *HIZCRB = 0x0000;
  162. *CPLD_PFC_ADR = 0x7c00; /* FPGA PROG = OUTPUT */
  163. /* write CPLD data from NOR flash to device */
  164. slave_serial();
  165. /*
  166. * Monitor the DONE bit in the CPLD Input Register to see if
  167. * configuration successful
  168. */
  169. check_done_bit();
  170. }