spi.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 <config.h>
  22. #include <common.h>
  23. #include <asm/hardware.h>
  24. #ifdef CONFIG_HAS_DATAFLASH
  25. #include <dataflash.h>
  26. /* Max Value = 10MHz to be compliant to the Continuous Array Read function */
  27. #define AT91C_SPI_CLK 10000000
  28. /* AC Characteristics: DLYBS = tCSS = 250ns min and DLYBCT = tCSH = 250ns */
  29. #define DATAFLASH_TCSS (0xFA << 16)
  30. #define DATAFLASH_TCHS (0x8 << 24)
  31. #define AT91C_TIMEOUT_WRDY 200000
  32. #define AT91C_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
  33. #define AT91C_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
  34. void AT91F_SpiInit(void)
  35. {
  36. /* Reset the SPI */
  37. AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SWRST;
  38. /* Configure SPI in Master Mode with No CS selected !!! */
  39. AT91C_BASE_SPI0->SPI_MR =
  40. AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS;
  41. /* Configure CS0 */
  42. AT91C_BASE_SPI0->SPI_CSR[0] =
  43. AT91C_SPI_CPOL |
  44. (AT91C_SPI_DLYBS & DATAFLASH_TCSS) |
  45. (AT91C_SPI_DLYBCT & DATAFLASH_TCHS) |
  46. ((AT91C_MASTER_CLOCK / (2*AT91C_SPI_CLK)) << 8);
  47. }
  48. void AT91F_SpiEnable(int cs)
  49. {
  50. switch (cs) {
  51. case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
  52. AT91C_BASE_SPI0->SPI_MR &= 0xFFF0FFFF;
  53. AT91C_BASE_SPI0->SPI_MR |=
  54. ((AT91C_SPI_PCS0_DATAFLASH_CARD<<16) & AT91C_SPI_PCS);
  55. break;
  56. case 3:
  57. AT91C_BASE_SPI0->SPI_MR &= 0xFFF0FFFF;
  58. AT91C_BASE_SPI0->SPI_MR |=
  59. ((AT91C_SPI_PCS3_DATAFLASH_CARD<<16) & AT91C_SPI_PCS);
  60. break;
  61. }
  62. /* SPI_Enable */
  63. AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SPIEN;
  64. }
  65. unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
  66. {
  67. unsigned int timeout;
  68. pDesc->state = BUSY;
  69. AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
  70. /* Initialize the Transmit and Receive Pointer */
  71. AT91C_BASE_SPI0->SPI_RPR = (unsigned int)pDesc->rx_cmd_pt;
  72. AT91C_BASE_SPI0->SPI_TPR = (unsigned int)pDesc->tx_cmd_pt;
  73. /* Intialize the Transmit and Receive Counters */
  74. AT91C_BASE_SPI0->SPI_RCR = pDesc->rx_cmd_size;
  75. AT91C_BASE_SPI0->SPI_TCR = pDesc->tx_cmd_size;
  76. if (pDesc->tx_data_size != 0) {
  77. /* Initialize the Next Transmit and Next Receive Pointer */
  78. AT91C_BASE_SPI0->SPI_RNPR = (unsigned int)pDesc->rx_data_pt;
  79. AT91C_BASE_SPI0->SPI_TNPR = (unsigned int)pDesc->tx_data_pt;
  80. /* Intialize the Next Transmit and Next Receive Counters */
  81. AT91C_BASE_SPI0->SPI_RNCR = pDesc->rx_data_size;
  82. AT91C_BASE_SPI0->SPI_TNCR = pDesc->tx_data_size;
  83. }
  84. /* arm simple, non interrupt dependent timer */
  85. reset_timer_masked();
  86. timeout = 0;
  87. AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTEN + AT91C_PDC_RXTEN;
  88. while (!(AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_RXBUFF) &&
  89. ((timeout = get_timer_masked()) < CFG_SPI_WRITE_TOUT));
  90. AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS;
  91. pDesc->state = IDLE;
  92. if (timeout >= CFG_SPI_WRITE_TOUT) {
  93. printf("Error Timeout\n\r");
  94. return DATAFLASH_ERROR;
  95. }
  96. return DATAFLASH_OK;
  97. }
  98. #endif