atmel_dataflash_spi.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Driver for ATMEL DataFlash support
  3. * Author : Hamid Ikdoumi (Atmel)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  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. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. *
  20. */
  21. #include <common.h>
  22. #ifndef CONFIG_AT91_LEGACY
  23. # define CONFIG_AT91_LEGACY
  24. # warning Please update to use C structure SoC access !
  25. #endif
  26. #include <spi.h>
  27. #include <malloc.h>
  28. #include <asm/io.h>
  29. #include <asm/arch/clk.h>
  30. #include <asm/arch/hardware.h>
  31. #include "atmel_spi.h"
  32. #include <asm/arch/gpio.h>
  33. #include <asm/arch/at91_pio.h>
  34. #include <asm/arch/at91_spi.h>
  35. #include <dataflash.h>
  36. #define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
  37. #define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 1: NPCS1%1101 */
  38. #define AT91_SPI_PCS2_DATAFLASH_CARD 0xB /* Chip Select 2: NPCS2%1011 */
  39. #define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
  40. void AT91F_SpiInit(void)
  41. {
  42. /* Reset the SPI */
  43. writel(AT91_SPI_SWRST, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  44. /* Configure SPI in Master Mode with No CS selected !!! */
  45. writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
  46. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  47. /* Configure CS0 */
  48. writel(AT91_SPI_NCPHA |
  49. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  50. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  51. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  52. ATMEL_BASE_SPI0 + AT91_SPI_CSR(0));
  53. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
  54. /* Configure CS1 */
  55. writel(AT91_SPI_NCPHA |
  56. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  57. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  58. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  59. ATMEL_BASE_SPI0 + AT91_SPI_CSR(1));
  60. #endif
  61. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS2
  62. /* Configure CS2 */
  63. writel(AT91_SPI_NCPHA |
  64. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  65. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  66. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  67. ATMEL_BASE_SPI0 + AT91_SPI_CSR(2));
  68. #endif
  69. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
  70. /* Configure CS3 */
  71. writel(AT91_SPI_NCPHA |
  72. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  73. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  74. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  75. ATMEL_BASE_SPI0 + AT91_SPI_CSR(3));
  76. #endif
  77. /* SPI_Enable */
  78. writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  79. while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_SPIENS))
  80. ;
  81. /*
  82. * Add tempo to get SPI in a safe state.
  83. * Should not be needed for new silicon (Rev B)
  84. */
  85. udelay(500000);
  86. readl(ATMEL_BASE_SPI0 + AT91_SPI_SR);
  87. readl(ATMEL_BASE_SPI0 + AT91_SPI_RDR);
  88. }
  89. void AT91F_SpiEnable(int cs)
  90. {
  91. unsigned long mode;
  92. switch (cs) {
  93. case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
  94. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  95. mode &= 0xFFF0FFFF;
  96. writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  97. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  98. break;
  99. case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
  100. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  101. mode &= 0xFFF0FFFF;
  102. writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  103. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  104. break;
  105. case 2: /* Configure SPI CS2 for Serial DataFlash AT45DBxx */
  106. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  107. mode &= 0xFFF0FFFF;
  108. writel(mode | ((AT91_SPI_PCS2_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  109. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  110. break;
  111. case 3:
  112. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  113. mode &= 0xFFF0FFFF;
  114. writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  115. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  116. break;
  117. }
  118. /* SPI_Enable */
  119. writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  120. }
  121. unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
  122. unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
  123. {
  124. unsigned int timeout;
  125. unsigned int timebase;
  126. pDesc->state = BUSY;
  127. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
  128. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  129. /* Initialize the Transmit and Receive Pointer */
  130. writel((unsigned int)pDesc->rx_cmd_pt,
  131. ATMEL_BASE_SPI0 + AT91_SPI_RPR);
  132. writel((unsigned int)pDesc->tx_cmd_pt,
  133. ATMEL_BASE_SPI0 + AT91_SPI_TPR);
  134. /* Intialize the Transmit and Receive Counters */
  135. writel(pDesc->rx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_RCR);
  136. writel(pDesc->tx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_TCR);
  137. if (pDesc->tx_data_size != 0) {
  138. /* Initialize the Next Transmit and Next Receive Pointer */
  139. writel((unsigned int)pDesc->rx_data_pt,
  140. ATMEL_BASE_SPI0 + AT91_SPI_RNPR);
  141. writel((unsigned int)pDesc->tx_data_pt,
  142. ATMEL_BASE_SPI0 + AT91_SPI_TNPR);
  143. /* Intialize the Next Transmit and Next Receive Counters */
  144. writel(pDesc->rx_data_size,
  145. ATMEL_BASE_SPI0 + AT91_SPI_RNCR);
  146. writel(pDesc->tx_data_size,
  147. ATMEL_BASE_SPI0 + AT91_SPI_TNCR);
  148. }
  149. /* arm simple, non interrupt dependent timer */
  150. timebase = get_timer(0);
  151. timeout = 0;
  152. writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN,
  153. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  154. while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
  155. ((timeout = get_timer(timebase)) < CONFIG_SYS_SPI_WRITE_TOUT))
  156. ;
  157. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
  158. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  159. pDesc->state = IDLE;
  160. if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
  161. printf("Error Timeout\n\r");
  162. return DATAFLASH_ERROR;
  163. }
  164. return DATAFLASH_OK;
  165. }