armada100_spi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = malloc(sizeof(*pss));
  105. if (!pss)
  106. return NULL;
  107. pss->slave.bus = bus;
  108. pss->slave.cs = cs;
  109. pss->spi_reg = (struct ssp_reg *)SSP_REG_BASE(CONFIG_SYS_SSP_PORT);
  110. pss->cr0 = SSCR0_MOTO | SSCR0_DATASIZE(DEFAULT_WORD_LEN) | SSCR0_SSE;
  111. pss->cr1 = (SSCR1_RXTRESH(RX_THRESH_DEF) & SSCR1_RFT) |
  112. (SSCR1_TXTRESH(TX_THRESH_DEF) & SSCR1_TFT);
  113. pss->cr1 &= ~(SSCR1_SPO | SSCR1_SPH);
  114. pss->cr1 |= (((mode & SPI_CPHA) != 0) ? SSCR1_SPH : 0)
  115. | (((mode & SPI_CPOL) != 0) ? SSCR1_SPO : 0);
  116. pss->int_cr1 = SSCR1_TIE | SSCR1_RIE | SSCR1_TINTE;
  117. pss->clear_sr = SSSR_ROR | SSSR_TINT;
  118. pss->gpio_cs_inverted = mode & SPI_CS_HIGH;
  119. gpio_set_value(cs, !pss->gpio_cs_inverted);
  120. return &pss->slave;
  121. }
  122. void spi_free_slave(struct spi_slave *slave)
  123. {
  124. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  125. free(pss);
  126. }
  127. int spi_claim_bus(struct spi_slave *slave)
  128. {
  129. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  130. debug("%s: bus:%i cs:%i\n", __func__, slave->bus, slave->cs);
  131. if (spi_armd_flush(pss) == 0)
  132. return -1;
  133. return 0;
  134. }
  135. void spi_release_bus(struct spi_slave *slave)
  136. {
  137. }
  138. int spi_xfer(struct spi_slave *slave, unsigned int bitlen, const void *dout,
  139. void *din, unsigned long flags)
  140. {
  141. struct armd_spi_slave *pss = to_armd_spi_slave(slave);
  142. uint bytes = bitlen / 8;
  143. unsigned long limit;
  144. int ret = 0;
  145. if (bitlen == 0)
  146. goto done;
  147. /* we can only do 8 bit transfers */
  148. if (bitlen % 8) {
  149. flags |= SPI_XFER_END;
  150. goto done;
  151. }
  152. if (dout)
  153. pss->tx = dout;
  154. else
  155. pss->tx = NULL;
  156. if (din)
  157. pss->rx = din;
  158. else
  159. pss->rx = NULL;
  160. if (flags & SPI_XFER_BEGIN) {
  161. spi_cs_activate(slave);
  162. writel(pss->cr1 | pss->int_cr1, &pss->spi_reg->sscr1);
  163. writel(TIMEOUT_DEF, &pss->spi_reg->ssto);
  164. writel(pss->cr0, &pss->spi_reg->sscr0);
  165. }
  166. while (bytes--) {
  167. limit = SSP_FLUSH_NUM;
  168. ret = spi_armd_write(pss);
  169. if (ret)
  170. break;
  171. while ((readl(&pss->spi_reg->sssr) & SSSR_BSY) && limit--)
  172. udelay(1);
  173. ret = spi_armd_read(pss);
  174. if (ret)
  175. break;
  176. }
  177. done:
  178. if (flags & SPI_XFER_END) {
  179. /* Stop SSP */
  180. writel(pss->clear_sr, &pss->spi_reg->sssr);
  181. clrbits_le32(&pss->spi_reg->sscr1, pss->int_cr1);
  182. writel(0, &pss->spi_reg->ssto);
  183. spi_cs_deactivate(slave);
  184. }
  185. return ret;
  186. }