mxs_spi.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Freescale i.MX28 SPI driver
  3. *
  4. * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
  5. * on behalf of DENX Software Engineering GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. * NOTE: This driver only supports the SPI-controller chipselects,
  23. * GPIO driven chipselects are not supported.
  24. */
  25. #include <common.h>
  26. #include <malloc.h>
  27. #include <spi.h>
  28. #include <asm/errno.h>
  29. #include <asm/io.h>
  30. #include <asm/arch/clock.h>
  31. #include <asm/arch/imx-regs.h>
  32. #include <asm/arch/sys_proto.h>
  33. #define MXS_SPI_MAX_TIMEOUT 1000000
  34. #define MXS_SPI_PORT_OFFSET 0x2000
  35. #define MXS_SSP_CHIPSELECT_MASK 0x00300000
  36. #define MXS_SSP_CHIPSELECT_SHIFT 20
  37. struct mxs_spi_slave {
  38. struct spi_slave slave;
  39. uint32_t max_khz;
  40. uint32_t mode;
  41. struct mx28_ssp_regs *regs;
  42. };
  43. static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave)
  44. {
  45. return container_of(slave, struct mxs_spi_slave, slave);
  46. }
  47. void spi_init(void)
  48. {
  49. }
  50. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  51. {
  52. /* MXS SPI: 4 ports and 3 chip selects maximum */
  53. if (bus > 3 || cs > 2)
  54. return 0;
  55. else
  56. return 1;
  57. }
  58. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  59. unsigned int max_hz, unsigned int mode)
  60. {
  61. struct mxs_spi_slave *mxs_slave;
  62. uint32_t addr;
  63. struct mx28_ssp_regs *ssp_regs;
  64. int reg;
  65. if (!spi_cs_is_valid(bus, cs)) {
  66. printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
  67. return NULL;
  68. }
  69. mxs_slave = malloc(sizeof(struct mxs_spi_slave));
  70. if (!mxs_slave)
  71. return NULL;
  72. addr = MXS_SSP0_BASE + (bus * MXS_SPI_PORT_OFFSET);
  73. mxs_slave->slave.bus = bus;
  74. mxs_slave->slave.cs = cs;
  75. mxs_slave->max_khz = max_hz / 1000;
  76. mxs_slave->mode = mode;
  77. mxs_slave->regs = (struct mx28_ssp_regs *)addr;
  78. ssp_regs = mxs_slave->regs;
  79. reg = readl(&ssp_regs->hw_ssp_ctrl0);
  80. reg &= ~(MXS_SSP_CHIPSELECT_MASK);
  81. reg |= cs << MXS_SSP_CHIPSELECT_SHIFT;
  82. writel(reg, &ssp_regs->hw_ssp_ctrl0);
  83. return &mxs_slave->slave;
  84. }
  85. void spi_free_slave(struct spi_slave *slave)
  86. {
  87. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  88. free(mxs_slave);
  89. }
  90. int spi_claim_bus(struct spi_slave *slave)
  91. {
  92. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  93. struct mx28_ssp_regs *ssp_regs = mxs_slave->regs;
  94. uint32_t reg = 0;
  95. mx28_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
  96. writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0);
  97. reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
  98. reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
  99. reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
  100. writel(reg, &ssp_regs->hw_ssp_ctrl1);
  101. writel(0, &ssp_regs->hw_ssp_cmd0);
  102. mx28_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
  103. return 0;
  104. }
  105. void spi_release_bus(struct spi_slave *slave)
  106. {
  107. }
  108. static void mxs_spi_start_xfer(struct mx28_ssp_regs *ssp_regs)
  109. {
  110. writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set);
  111. writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr);
  112. }
  113. static void mxs_spi_end_xfer(struct mx28_ssp_regs *ssp_regs)
  114. {
  115. writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr);
  116. writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set);
  117. }
  118. static int mxs_spi_xfer_pio(struct mxs_spi_slave *slave,
  119. char *data, int length, int write, unsigned long flags)
  120. {
  121. struct mx28_ssp_regs *ssp_regs = slave->regs;
  122. if (flags & SPI_XFER_BEGIN)
  123. mxs_spi_start_xfer(ssp_regs);
  124. while (length--) {
  125. /* We transfer 1 byte */
  126. writel(1, &ssp_regs->hw_ssp_xfer_size);
  127. if ((flags & SPI_XFER_END) && !length)
  128. mxs_spi_end_xfer(ssp_regs);
  129. if (write)
  130. writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
  131. else
  132. writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
  133. writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
  134. if (mx28_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
  135. SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
  136. printf("MXS SPI: Timeout waiting for start\n");
  137. return -ETIMEDOUT;
  138. }
  139. if (write)
  140. writel(*data++, &ssp_regs->hw_ssp_data);
  141. writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
  142. if (!write) {
  143. if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
  144. SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
  145. printf("MXS SPI: Timeout waiting for data\n");
  146. return -ETIMEDOUT;
  147. }
  148. *data = readl(&ssp_regs->hw_ssp_data);
  149. data++;
  150. }
  151. if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
  152. SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
  153. printf("MXS SPI: Timeout waiting for finish\n");
  154. return -ETIMEDOUT;
  155. }
  156. }
  157. return 0;
  158. }
  159. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  160. const void *dout, void *din, unsigned long flags)
  161. {
  162. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  163. int len = bitlen / 8;
  164. char dummy;
  165. int write = 0;
  166. char *data = NULL;
  167. if (bitlen == 0) {
  168. if (flags & SPI_XFER_END) {
  169. din = (void *)&dummy;
  170. len = 1;
  171. } else
  172. return 0;
  173. }
  174. /* Half-duplex only */
  175. if (din && dout)
  176. return -EINVAL;
  177. /* No data */
  178. if (!din && !dout)
  179. return 0;
  180. if (dout) {
  181. data = (char *)dout;
  182. write = 1;
  183. } else if (din) {
  184. data = (char *)din;
  185. write = 0;
  186. }
  187. return mxs_spi_xfer_pio(mxs_slave, data, len, write, flags);
  188. }