armada100_spi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * (C) Copyright 2011
  3. * eInfochips Ltd. <www.einfochips.com>
  4. * Written-by: Ajay Bhargav <ajay.bhargav@einfochips.com>
  5. *
  6. * (C) Copyright 2009
  7. * Marvell Semiconductor <www.marvell.com>
  8. * Based on SSP driver
  9. * Written-by: Lei Wen <leiwen@marvell.com>
  10. *
  11. * See file CREDITS for list of people who contributed to this
  12. * project.
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public License as
  16. * published by the Free Software Foundation; either version 2 of
  17. * the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  27. * MA 02110-1301 USA
  28. */
  29. #include <common.h>
  30. #include <malloc.h>
  31. #include <spi.h>
  32. #include <asm/io.h>
  33. #include <asm/arch/spi.h>
  34. #include <asm/gpio.h>
  35. #define to_armd_spi_slave(s) container_of(s, struct armd_spi_slave, slave)
  36. struct armd_spi_slave {
  37. struct spi_slave slave;
  38. struct ssp_reg *spi_reg;
  39. u32 cr0, cr1;
  40. u32 int_cr1;
  41. u32 clear_sr;
  42. const void *tx;
  43. void *rx;
  44. int gpio_cs_inverted;
  45. };
  46. static int spi_armd_write(struct armd_spi_slave *pss)
  47. {
  48. int wait_timeout = SSP_FLUSH_NUM;
  49. while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_TNF))
  50. ;
  51. if (!wait_timeout) {
  52. debug("%s: timeout error\n", __func__);
  53. return -1;
  54. }
  55. if (pss->tx != NULL) {
  56. writel(*(u8 *)pss->tx, &pss->spi_reg->ssdr);
  57. ++pss->tx;
  58. } else {
  59. writel(0, &pss->spi_reg->ssdr);
  60. }
  61. return 0;
  62. }
  63. static int spi_armd_read(struct armd_spi_slave *pss)
  64. {
  65. int wait_timeout = SSP_FLUSH_NUM;
  66. while (--wait_timeout && !(readl(&pss->spi_reg->sssr) & SSSR_RNE))
  67. ;
  68. if (!wait_timeout) {
  69. debug("%s: timeout error\n", __func__);
  70. return -1;
  71. }
  72. if (pss->rx != NULL) {
  73. *(u8 *)pss->rx = readl(&pss->spi_reg->ssdr);
  74. ++pss->rx;
  75. } else {
  76. readl(&pss->spi_reg->ssdr);
  77. }
  78. return 0;
  79. }
  80. static int spi_armd_flush(struct armd_spi_slave *pss)
  81. {
  82. unsigned long limit = SSP_FLUSH_NUM;
  83. do {
  84. while (readl(&pss->spi_reg->sssr) & SSSR_RNE)
  85. readl(&pss->spi_reg->ssdr);
  86. } while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--);
  87. writel(SSSR_ROR, &pss->spi_reg->sssr);
  88. return limit;
  89. }
  90. void spi_cs_activate(struct spi_slave *slave)
  91. {
  92. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  93. gpio_set_value(slave->cs, pss->gpio_cs_inverted);
  94. }
  95. void spi_cs_deactivate(struct spi_slave *slave)
  96. {
  97. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  98. gpio_set_value(slave->cs, !pss->gpio_cs_inverted);
  99. }
  100. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  101. unsigned int max_hz, unsigned int mode)
  102. {
  103. struct armd_spi_slave *pss;
  104. pss = spi_alloc_slave(struct armd_spi_slave, bus, cs);
  105. if (!pss)
  106. return NULL;
  107. pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
  108. pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
  109. pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
  110. (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
  111. pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  112. pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  113. | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  114. pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  115. pss->clear_sr = SSSR_ROR | SSSR_TINT;
  116. pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
  117. gpio_set_value(cs, !pss->gpio_cs_inverted);
  118. return &pss->slave;
  119. }
  120. void spi_free_slave(struct spi_slave *slave)
  121. {
  122. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  123. free(pss);
  124. }
  125. int spi_claim_bus(struct spi_slave *slave)
  126. {
  127. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  128. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  129. if (spi_armd_flush(pss) == 0)
  130. return -1;
  131. return 0;
  132. }
  133. void spi_release_bus(struct spi_slave *slave)
  134. {
  135. }
  136. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  137. void *din, unsigned long flags)
  138. {
  139. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  140. uint bytes = bitlen / 8;
  141. unsigned long limit;
  142. int ret = 0;
  143. if (bitlen == 0)
  144. goto done;
  145. /* we can only do 8 bit transfers */
  146. if (bitlen % 8) {
  147. flags |= SPI_XFER_END;
  148. goto done;
  149. }
  150. if (dout)
  151. pss->tx = dout;
  152. else
  153. pss->tx = NULL;
  154. if (din)
  155. pss->rx = din;
  156. else
  157. pss->rx = NULL;
  158. if (flags & SPI_XFER_BEGIN) {
  159. spi_cs_activate(slave);
  160. writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
  161. writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
  162. writel(pss->cr0, &pss->spi_reg->sscr0);
  163. }
  164. while (bytes--) {
  165. limit = SSP_FLUSH_NUM;
  166. ret = spi_armd_write(pss);
  167. if (ret)
  168. break;
  169. while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
  170. udelay(1);
  171. ret = spi_armd_read(pss);
  172. if (ret)
  173. break;
  174. }
  175. done:
  176. if (flags & SPI_XFER_END) {
  177. /* Stop SSP */
  178. writel(pss->clear_sr, &pss->spi_reg->sssr);
  179. clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
  180. writel(0, &pss->spi_reg->ssto);
  181. spi_cs_deactivate(slave);
  182. }
  183. return ret;
  184. }