spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* Driver for ATMEL DataFlash support
  2. * Author : Hamid Ikdoumi (Atmel)
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of
  7. * the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  17. * MA 02111-1307 USA
  18. *
  19. */
  20. #include <config.h>
  21. #include <common.h>
  22. #include <asm/hardware.h>
  23. #ifdef CONFIG_HAS_DATAFLASH
  24. #include <dataflash.h>
  25. #define AT91C_SPI_CLK 10000000 /* Max Value = 10MHz to be compliant to
  26. the Continuous Array Read function */
  27. /* AC Characteristics */
  28. /* DLYBS = tCSS = 250ns min and DLYBCT = tCSH = 250ns */
  29. #define DATAFLASH_TCSS (0xC << 16)
  30. #define DATAFLASH_TCHS (0x1 << 24)
  31. #define AT91C_TIMEOUT_WRDY 200000
  32. #define AT91C_SPI_PCS0_SERIAL_DATAFLASH 0xE /* Chip Select 0: NPCS0%1110 */
  33. #define AT91C_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
  34. /*-------------------------------------------------------------------*/
  35. /* SPI DataFlash Init */
  36. /*-------------------------------------------------------------------*/
  37. void AT91F_SpiInit(void)
  38. {
  39. /* Configure PIOs */
  40. AT91C_BASE_PIOA->PIO_ASR =
  41. AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI |
  42. AT91C_PA5_NPCS2 | AT91C_PA6_NPCS3 | AT91C_PA0_MISO |
  43. AT91C_PA2_SPCK;
  44. AT91C_BASE_PIOA->PIO_PDR =
  45. AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI |
  46. AT91C_PA5_NPCS2 | AT91C_PA6_NPCS3 | AT91C_PA0_MISO |
  47. AT91C_PA2_SPCK;
  48. /* Enable CLock */
  49. AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_SPI;
  50. /* Reset the SPI */
  51. AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST;
  52. /* Configure SPI in Master Mode with No CS selected !!! */
  53. AT91C_BASE_SPI->SPI_MR =
  54. AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS;
  55. /* Configure CS0 and CS3 */
  56. *(AT91C_SPI_CSR + 0) =
  57. AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & DATAFLASH_TCSS) |
  58. (AT91C_SPI_DLYBCT & DATAFLASH_TCHS) |
  59. ((AT91C_MASTER_CLOCK / (2*AT91C_SPI_CLK)) << 8);
  60. *(AT91C_SPI_CSR + 3) =
  61. AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & DATAFLASH_TCSS) |
  62. (AT91C_SPI_DLYBCT & DATAFLASH_TCHS) |
  63. ((AT91C_MASTER_CLOCK / (2*AT91C_SPI_CLK)) << 8);
  64. }
  65. void AT91F_SpiEnable(int cs)
  66. {
  67. switch(cs) {
  68. case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
  69. AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF;
  70. AT91C_BASE_SPI->SPI_MR |=
  71. ((AT91C_SPI_PCS0_SERIAL_DATAFLASH<<16) &
  72. AT91C_SPI_PCS);
  73. break;
  74. case 3: /* Configure SPI CS3 for Serial DataFlash Card */
  75. /* Set up PIO SDC_TYPE to switch on DataFlash Card */
  76. /* and not MMC/SDCard */
  77. AT91C_BASE_PIOB->PIO_PER =
  78. AT91C_PIO_PB7; /* Set in PIO mode */
  79. AT91C_BASE_PIOB->PIO_OER =
  80. AT91C_PIO_PB7; /* Configure in output */
  81. /* Clear Output */
  82. AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB7;
  83. /* Configure PCS */
  84. AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF;
  85. AT91C_BASE_SPI->SPI_MR |=
  86. ((AT91C_SPI_PCS3_DATAFLASH_CARD<<16) & AT91C_SPI_PCS);
  87. break;
  88. }
  89. /* SPI_Enable */
  90. AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN; }
  91. /*---------------------------------------------------------------------------*/
  92. /* \fn AT91F_SpiWrite */
  93. /* \brief Set the PDC registers for a transfert */
  94. /*---------------------------------------------------------------------------*/
  95. unsigned int AT91F_SpiWrite ( AT91PS_DataflashDesc pDesc )
  96. {
  97. unsigned int timeout;
  98. pDesc->state = BUSY;
  99. AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
  100. /* Initialize the Transmit and Receive Pointer */
  101. AT91C_BASE_SPI->SPI_RPR = (unsigned int)pDesc->rx_cmd_pt ;
  102. AT91C_BASE_SPI->SPI_TPR = (unsigned int)pDesc->tx_cmd_pt ;
  103. /* Intialize the Transmit and Receive Counters */
  104. AT91C_BASE_SPI->SPI_RCR = pDesc->rx_cmd_size;
  105. AT91C_BASE_SPI->SPI_TCR = pDesc->tx_cmd_size;
  106. if ( pDesc->tx_data_size != 0 ) {
  107. /* Initialize the Next Transmit and Next Receive Pointer */
  108. AT91C_BASE_SPI->SPI_RNPR = (unsigned int)pDesc->rx_data_pt ;
  109. AT91C_BASE_SPI->SPI_TNPR = (unsigned int)pDesc->tx_data_pt ;
  110. /* Intialize the Next Transmit and Next Receive Counters */
  111. AT91C_BASE_SPI->SPI_RNCR = pDesc->rx_data_size ;
  112. AT91C_BASE_SPI->SPI_TNCR = pDesc->tx_data_size ;
  113. }
  114. /* arm simple, non interrupt dependent timer */
  115. reset_timer_masked();
  116. timeout = 0;
  117. AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTEN + AT91C_PDC_RXTEN;
  118. while(!(AT91C_BASE_SPI->SPI_SR & AT91C_SPI_RXBUFF) &&
  119. ((timeout = get_timer_masked() ) < CONFIG_SYS_SPI_WRITE_TOUT));
  120. AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
  121. pDesc->state = IDLE;
  122. if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT){
  123. printf("Error Timeout\n\r");
  124. return DATAFLASH_ERROR;
  125. }
  126. return DATAFLASH_OK;
  127. }
  128. #endif