atmel_dataflash_spi.c 4.8 KB

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