atmel_dataflash_spi.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. /*
  22. * This driver desperately needs rework:
  23. *
  24. * - use structure SoC access
  25. * - get rid of including asm/arch/at91_spi.h
  26. * - remove asm/arch/at91_spi.h
  27. * - get rid of all CONFIG_ATMEL_LEGACY defines and uses
  28. *
  29. * 02-Aug-2010 Reinhard Meyer <uboot@emk-elektronik.de>
  30. */
  31. #include <common.h>
  32. #ifndef CONFIG_ATMEL_LEGACY
  33. # define CONFIG_ATMEL_LEGACY
  34. #endif
  35. #include <spi.h>
  36. #include <malloc.h>
  37. #include <asm/io.h>
  38. #include <asm/arch/clk.h>
  39. #include <asm/arch/hardware.h>
  40. #include "atmel_spi.h"
  41. #include <asm/arch/gpio.h>
  42. #include <asm/arch/at91_pio.h>
  43. #include <asm/arch/at91_spi.h>
  44. #include <dataflash.h>
  45. #define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
  46. #define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 1: NPCS1%1101 */
  47. #define AT91_SPI_PCS2_DATAFLASH_CARD 0xB /* Chip Select 2: NPCS2%1011 */
  48. #define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
  49. void AT91F_SpiInit(void)
  50. {
  51. /* Reset the SPI */
  52. writel(AT91_SPI_SWRST, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  53. /* Configure SPI in Master Mode with No CS selected !!! */
  54. writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
  55. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  56. /* Configure CS0 */
  57. writel(AT91_SPI_NCPHA |
  58. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  59. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  60. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  61. ATMEL_BASE_SPI0 + AT91_SPI_CSR(0));
  62. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
  63. /* Configure CS1 */
  64. writel(AT91_SPI_NCPHA |
  65. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  66. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  67. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  68. ATMEL_BASE_SPI0 + AT91_SPI_CSR(1));
  69. #endif
  70. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS2
  71. /* Configure CS2 */
  72. writel(AT91_SPI_NCPHA |
  73. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  74. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  75. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  76. ATMEL_BASE_SPI0 + AT91_SPI_CSR(2));
  77. #endif
  78. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
  79. /* Configure CS3 */
  80. writel(AT91_SPI_NCPHA |
  81. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  82. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  83. ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
  84. ATMEL_BASE_SPI0 + AT91_SPI_CSR(3));
  85. #endif
  86. /* SPI_Enable */
  87. writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  88. while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_SPIENS))
  89. ;
  90. /*
  91. * Add tempo to get SPI in a safe state.
  92. * Should not be needed for new silicon (Rev B)
  93. */
  94. udelay(500000);
  95. readl(ATMEL_BASE_SPI0 + AT91_SPI_SR);
  96. readl(ATMEL_BASE_SPI0 + AT91_SPI_RDR);
  97. }
  98. void AT91F_SpiEnable(int cs)
  99. {
  100. unsigned long mode;
  101. switch (cs) {
  102. case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
  103. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  104. mode &= 0xFFF0FFFF;
  105. writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  106. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  107. break;
  108. case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
  109. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  110. mode &= 0xFFF0FFFF;
  111. writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  112. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  113. break;
  114. case 2: /* Configure SPI CS2 for Serial DataFlash AT45DBxx */
  115. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  116. mode &= 0xFFF0FFFF;
  117. writel(mode | ((AT91_SPI_PCS2_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  118. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  119. break;
  120. case 3:
  121. mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
  122. mode &= 0xFFF0FFFF;
  123. writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  124. ATMEL_BASE_SPI0 + AT91_SPI_MR);
  125. break;
  126. }
  127. /* SPI_Enable */
  128. writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
  129. }
  130. unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
  131. unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
  132. {
  133. unsigned int timeout;
  134. unsigned int timebase;
  135. pDesc->state = BUSY;
  136. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
  137. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  138. /* Initialize the Transmit and Receive Pointer */
  139. writel((unsigned int)pDesc->rx_cmd_pt,
  140. ATMEL_BASE_SPI0 + AT91_SPI_RPR);
  141. writel((unsigned int)pDesc->tx_cmd_pt,
  142. ATMEL_BASE_SPI0 + AT91_SPI_TPR);
  143. /* Intialize the Transmit and Receive Counters */
  144. writel(pDesc->rx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_RCR);
  145. writel(pDesc->tx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_TCR);
  146. if (pDesc->tx_data_size != 0) {
  147. /* Initialize the Next Transmit and Next Receive Pointer */
  148. writel((unsigned int)pDesc->rx_data_pt,
  149. ATMEL_BASE_SPI0 + AT91_SPI_RNPR);
  150. writel((unsigned int)pDesc->tx_data_pt,
  151. ATMEL_BASE_SPI0 + AT91_SPI_TNPR);
  152. /* Intialize the Next Transmit and Next Receive Counters */
  153. writel(pDesc->rx_data_size,
  154. ATMEL_BASE_SPI0 + AT91_SPI_RNCR);
  155. writel(pDesc->tx_data_size,
  156. ATMEL_BASE_SPI0 + AT91_SPI_TNCR);
  157. }
  158. /* arm simple, non interrupt dependent timer */
  159. timebase = get_timer(0);
  160. timeout = 0;
  161. writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN,
  162. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  163. while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
  164. ((timeout = get_timer(timebase)) < CONFIG_SYS_SPI_WRITE_TOUT))
  165. ;
  166. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
  167. ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
  168. pDesc->state = IDLE;
  169. if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
  170. printf("Error Timeout\n\r");
  171. return DATAFLASH_ERROR;
  172. }
  173. return DATAFLASH_OK;
  174. }