mxs_spi.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. struct mxs_spi_slave {
  36. struct spi_slave slave;
  37. uint32_t max_khz;
  38. uint32_t mode;
  39. struct mx28_ssp_regs *regs;
  40. };
  41. static inline struct mxs_spi_slave *to_mxs_slave(struct spi_slave *slave)
  42. {
  43. return container_of(slave, struct mxs_spi_slave, slave);
  44. }
  45. void spi_init(void)
  46. {
  47. }
  48. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  49. {
  50. /* MXS SPI: 4 ports and 3 chip selects maximum */
  51. if (bus > 3 || cs > 2)
  52. return 0;
  53. else
  54. return 1;
  55. }
  56. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  57. unsigned int max_hz, unsigned int mode)
  58. {
  59. struct mxs_spi_slave *mxs_slave;
  60. uint32_t addr;
  61. if (!spi_cs_is_valid(bus, cs)) {
  62. printf("mxs_spi: invalid bus %d / chip select %d\n", bus, cs);
  63. return NULL;
  64. }
  65. mxs_slave = malloc(sizeof(struct mxs_spi_slave));
  66. if (!mxs_slave)
  67. return NULL;
  68. addr = MXS_SSP0_BASE + (bus * MXS_SPI_PORT_OFFSET);
  69. mxs_slave->slave.bus = bus;
  70. mxs_slave->slave.cs = cs;
  71. mxs_slave->max_khz = max_hz / 1000;
  72. mxs_slave->mode = mode;
  73. mxs_slave->regs = (struct mx28_ssp_regs *)addr;
  74. return &mxs_slave->slave;
  75. }
  76. void spi_free_slave(struct spi_slave *slave)
  77. {
  78. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  79. free(mxs_slave);
  80. }
  81. int spi_claim_bus(struct spi_slave *slave)
  82. {
  83. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  84. struct mx28_ssp_regs *ssp_regs = mxs_slave->regs;
  85. uint32_t reg = 0;
  86. mx28_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
  87. writel(SSP_CTRL0_BUS_WIDTH_ONE_BIT, &ssp_regs->hw_ssp_ctrl0);
  88. reg = SSP_CTRL1_SSP_MODE_SPI | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS;
  89. reg |= (mxs_slave->mode & SPI_CPOL) ? SSP_CTRL1_POLARITY : 0;
  90. reg |= (mxs_slave->mode & SPI_CPHA) ? SSP_CTRL1_PHASE : 0;
  91. writel(reg, &ssp_regs->hw_ssp_ctrl1);
  92. writel(0, &ssp_regs->hw_ssp_cmd0);
  93. mx28_set_ssp_busclock(slave->bus, mxs_slave->max_khz);
  94. return 0;
  95. }
  96. void spi_release_bus(struct spi_slave *slave)
  97. {
  98. }
  99. static void mxs_spi_start_xfer(struct mx28_ssp_regs *ssp_regs)
  100. {
  101. writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_set);
  102. writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_clr);
  103. }
  104. static void mxs_spi_end_xfer(struct mx28_ssp_regs *ssp_regs)
  105. {
  106. writel(SSP_CTRL0_LOCK_CS, &ssp_regs->hw_ssp_ctrl0_clr);
  107. writel(SSP_CTRL0_IGNORE_CRC, &ssp_regs->hw_ssp_ctrl0_set);
  108. }
  109. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  110. const void *dout, void *din, unsigned long flags)
  111. {
  112. struct mxs_spi_slave *mxs_slave = to_mxs_slave(slave);
  113. struct mx28_ssp_regs *ssp_regs = mxs_slave->regs;
  114. int len = bitlen / 8;
  115. const char *tx = dout;
  116. char *rx = din;
  117. char dummy;
  118. if (bitlen == 0) {
  119. if (flags & SPI_XFER_END) {
  120. rx = &dummy;
  121. len = 1;
  122. } else
  123. return 0;
  124. }
  125. if (!rx && !tx)
  126. return 0;
  127. if (flags & SPI_XFER_BEGIN)
  128. mxs_spi_start_xfer(ssp_regs);
  129. while (len--) {
  130. /* We transfer 1 byte */
  131. writel(1, &ssp_regs->hw_ssp_xfer_size);
  132. if ((flags & SPI_XFER_END) && !len)
  133. mxs_spi_end_xfer(ssp_regs);
  134. if (tx)
  135. writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_clr);
  136. else
  137. writel(SSP_CTRL0_READ, &ssp_regs->hw_ssp_ctrl0_set);
  138. writel(SSP_CTRL0_RUN, &ssp_regs->hw_ssp_ctrl0_set);
  139. if (mx28_wait_mask_set(&ssp_regs->hw_ssp_ctrl0_reg,
  140. SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
  141. printf("MXS SPI: Timeout waiting for start\n");
  142. return -ETIMEDOUT;
  143. }
  144. if (tx)
  145. writel(*tx++, &ssp_regs->hw_ssp_data);
  146. writel(SSP_CTRL0_DATA_XFER, &ssp_regs->hw_ssp_ctrl0_set);
  147. if (rx) {
  148. if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_status_reg,
  149. SSP_STATUS_FIFO_EMPTY, MXS_SPI_MAX_TIMEOUT)) {
  150. printf("MXS SPI: Timeout waiting for data\n");
  151. return -ETIMEDOUT;
  152. }
  153. *rx = readl(&ssp_regs->hw_ssp_data);
  154. rx++;
  155. }
  156. if (mx28_wait_mask_clr(&ssp_regs->hw_ssp_ctrl0_reg,
  157. SSP_CTRL0_RUN, MXS_SPI_MAX_TIMEOUT)) {
  158. printf("MXS SPI: Timeout waiting for finish\n");
  159. return -ETIMEDOUT;
  160. }
  161. }
  162. return 0;
  163. }