atmel_dataflash_spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #include <asm/arch/hardware.h>
  23. #include <asm/arch/gpio.h>
  24. #include <asm/arch/io.h>
  25. #include <asm/arch/at91_pio.h>
  26. #include <asm/arch/at91_spi.h>
  27. #include <dataflash.h>
  28. #define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
  29. #define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 0: NPCS0%1101 */
  30. #define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
  31. void AT91F_SpiInit(void)
  32. {
  33. /* Reset the SPI */
  34. writel(AT91_SPI_SWRST, AT91_BASE_SPI + AT91_SPI_CR);
  35. /* Configure SPI in Master Mode with No CS selected !!! */
  36. writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
  37. AT91_BASE_SPI + AT91_SPI_MR);
  38. /* Configure CS0 */
  39. writel(AT91_SPI_NCPHA |
  40. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  41. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  42. ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
  43. AT91_BASE_SPI + AT91_SPI_CSR(0));
  44. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
  45. /* Configure CS1 */
  46. writel(AT91_SPI_NCPHA |
  47. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  48. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  49. ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
  50. AT91_BASE_SPI + AT91_SPI_CSR(1));
  51. #endif
  52. #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
  53. /* Configure CS3 */
  54. writel(AT91_SPI_NCPHA |
  55. (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
  56. (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
  57. ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
  58. AT91_BASE_SPI + AT91_SPI_CSR(3));
  59. #endif
  60. /* SPI_Enable */
  61. writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
  62. while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_SPIENS));
  63. /*
  64. * Add tempo to get SPI in a safe state.
  65. * Should not be needed for new silicon (Rev B)
  66. */
  67. udelay(500000);
  68. readl(AT91_BASE_SPI + AT91_SPI_SR);
  69. readl(AT91_BASE_SPI + AT91_SPI_RDR);
  70. }
  71. void AT91F_SpiEnable(int cs)
  72. {
  73. unsigned long mode;
  74. switch (cs) {
  75. case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
  76. mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
  77. mode &= 0xFFF0FFFF;
  78. writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  79. AT91_BASE_SPI + AT91_SPI_MR);
  80. break;
  81. case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
  82. mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
  83. mode &= 0xFFF0FFFF;
  84. writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  85. AT91_BASE_SPI + AT91_SPI_MR);
  86. break;
  87. case 3:
  88. mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
  89. mode &= 0xFFF0FFFF;
  90. writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
  91. AT91_BASE_SPI + AT91_SPI_MR);
  92. break;
  93. }
  94. /* SPI_Enable */
  95. writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
  96. }
  97. unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
  98. unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
  99. {
  100. unsigned int timeout;
  101. pDesc->state = BUSY;
  102. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
  103. /* Initialize the Transmit and Receive Pointer */
  104. writel((unsigned int)pDesc->rx_cmd_pt, AT91_BASE_SPI + AT91_SPI_RPR);
  105. writel((unsigned int)pDesc->tx_cmd_pt, AT91_BASE_SPI + AT91_SPI_TPR);
  106. /* Intialize the Transmit and Receive Counters */
  107. writel(pDesc->rx_cmd_size, AT91_BASE_SPI + AT91_SPI_RCR);
  108. writel(pDesc->tx_cmd_size, AT91_BASE_SPI + AT91_SPI_TCR);
  109. if (pDesc->tx_data_size != 0) {
  110. /* Initialize the Next Transmit and Next Receive Pointer */
  111. writel((unsigned int)pDesc->rx_data_pt, AT91_BASE_SPI + AT91_SPI_RNPR);
  112. writel((unsigned int)pDesc->tx_data_pt, AT91_BASE_SPI + AT91_SPI_TNPR);
  113. /* Intialize the Next Transmit and Next Receive Counters */
  114. writel(pDesc->rx_data_size, AT91_BASE_SPI + AT91_SPI_RNCR);
  115. writel(pDesc->tx_data_size, AT91_BASE_SPI + AT91_SPI_TNCR);
  116. }
  117. /* arm simple, non interrupt dependent timer */
  118. reset_timer_masked();
  119. timeout = 0;
  120. writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN, AT91_BASE_SPI + AT91_SPI_PTCR);
  121. while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
  122. ((timeout = get_timer_masked()) < CONFIG_SYS_SPI_WRITE_TOUT));
  123. writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
  124. pDesc->state = IDLE;
  125. if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
  126. printf("Error Timeout\n\r");
  127. return DATAFLASH_ERROR;
  128. }
  129. return DATAFLASH_OK;
  130. }