tegra2_spi.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Copyright (c) 2010-2011 NVIDIA Corporation
  3. * With help from the mpc8xxx SPI driver
  4. * With more help from omap3_spi SPI driver
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <common.h>
  25. #include <malloc.h>
  26. #include <spi.h>
  27. #include <asm/io.h>
  28. #include <asm/gpio.h>
  29. #include <asm/arch/clk_rst.h>
  30. #include <asm/arch/clock.h>
  31. #include <asm/arch/pinmux.h>
  32. #include <asm/arch/uart-spi-switch.h>
  33. #include <asm/arch/tegra2_spi.h>
  34. #if defined(CONFIG_SPI_CORRUPTS_UART)
  35. #define corrupt_delay() udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
  36. #else
  37. #define corrupt_delay()
  38. #endif
  39. struct tegra_spi_slave {
  40. struct spi_slave slave;
  41. struct spi_tegra *regs;
  42. unsigned int freq;
  43. unsigned int mode;
  44. };
  45. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  46. {
  47. return container_of(slave, struct tegra_spi_slave, slave);
  48. }
  49. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  50. {
  51. /* Tegra2 SPI-Flash - only 1 device ('bus/cs') */
  52. if (bus != 0 || cs != 0)
  53. return 0;
  54. else
  55. return 1;
  56. }
  57. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  58. unsigned int max_hz, unsigned int mode)
  59. {
  60. struct tegra_spi_slave *spi;
  61. if (!spi_cs_is_valid(bus, cs)) {
  62. printf("SPI error: unsupported bus %d / chip select %d\n",
  63. bus, cs);
  64. return NULL;
  65. }
  66. if (max_hz > TEGRA2_SPI_MAX_FREQ) {
  67. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  68. " is %d Hz\n", max_hz, TEGRA2_SPI_MAX_FREQ);
  69. return NULL;
  70. }
  71. spi = malloc(sizeof(struct tegra_spi_slave));
  72. if (!spi) {
  73. printf("SPI error: malloc of SPI structure failed\n");
  74. return NULL;
  75. }
  76. spi->slave.bus = bus;
  77. spi->slave.cs = cs;
  78. spi->freq = max_hz;
  79. spi->regs = (struct spi_tegra *)TEGRA2_SPI_BASE;
  80. spi->mode = mode;
  81. return &spi->slave;
  82. }
  83. void spi_free_slave(struct spi_slave *slave)
  84. {
  85. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  86. free(spi);
  87. }
  88. void spi_init(void)
  89. {
  90. /* do nothing */
  91. }
  92. int spi_claim_bus(struct spi_slave *slave)
  93. {
  94. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  95. struct spi_tegra *regs = spi->regs;
  96. u32 reg;
  97. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  98. clock_start_periph_pll(PERIPH_ID_SPI1, CLOCK_ID_PERIPH, spi->freq);
  99. /* Clear stale status here */
  100. reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
  101. SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
  102. writel(reg, &regs->status);
  103. debug("spi_init: STATUS = %08x\n", readl(&regs->status));
  104. /*
  105. * Use sw-controlled CS, so we can clock in data after ReadID, etc.
  106. */
  107. reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
  108. if (spi->mode & 2)
  109. reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
  110. clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
  111. SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
  112. debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
  113. /*
  114. * SPI pins on Tegra2 are muxed - change pinmux later due to UART
  115. * issue.
  116. */
  117. pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
  118. pinmux_tristate_disable(PINGRP_LSPI);
  119. #ifndef CONFIG_SPI_UART_SWITCH
  120. /*
  121. * NOTE:
  122. * Only set PinMux bits 3:2 to SPI here on boards that don't have the
  123. * SPI UART switch or subsequent UART data won't go out! See
  124. * spi_uart_switch().
  125. */
  126. /* TODO: pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); */
  127. #endif
  128. return 0;
  129. }
  130. void spi_release_bus(struct spi_slave *slave)
  131. {
  132. /*
  133. * We can't release UART_DISABLE and set pinmux to UART4 here since
  134. * some code (e,g, spi_flash_probe) uses printf() while the SPI
  135. * bus is held. That is arguably bad, but it has the advantage of
  136. * already being in the source tree.
  137. */
  138. }
  139. void spi_cs_activate(struct spi_slave *slave)
  140. {
  141. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  142. pinmux_select_spi();
  143. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  144. setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
  145. corrupt_delay(); /* Let UART settle */
  146. }
  147. void spi_cs_deactivate(struct spi_slave *slave)
  148. {
  149. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  150. pinmux_select_uart();
  151. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  152. clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
  153. corrupt_delay(); /* Let SPI settle */
  154. }
  155. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  156. const void *data_out, void *data_in, unsigned long flags)
  157. {
  158. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  159. struct spi_tegra *regs = spi->regs;
  160. u32 reg, tmpdout, tmpdin = 0;
  161. const u8 *dout = data_out;
  162. u8 *din = data_in;
  163. int num_bytes;
  164. int ret;
  165. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  166. slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
  167. if (bitlen % 8)
  168. return -1;
  169. num_bytes = bitlen / 8;
  170. ret = 0;
  171. reg = readl(&regs->status);
  172. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  173. debug("spi_xfer entry: STATUS = %08x\n", reg);
  174. reg = readl(&regs->command);
  175. reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
  176. writel(reg, &regs->command);
  177. debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
  178. if (flags & SPI_XFER_BEGIN)
  179. spi_cs_activate(slave);
  180. /* handle data in 32-bit chunks */
  181. while (num_bytes > 0) {
  182. int bytes;
  183. int is_read = 0;
  184. int tm, i;
  185. tmpdout = 0;
  186. bytes = (num_bytes > 4) ? 4 : num_bytes;
  187. if (dout != NULL) {
  188. for (i = 0; i < bytes; ++i)
  189. tmpdout = (tmpdout << 8) | dout[i];
  190. }
  191. num_bytes -= bytes;
  192. if (dout)
  193. dout += bytes;
  194. clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
  195. bytes * 8 - 1);
  196. writel(tmpdout, &regs->tx_fifo);
  197. setbits_le32(&regs->command, SPI_CMD_GO);
  198. /*
  199. * Wait for SPI transmit FIFO to empty, or to time out.
  200. * The RX FIFO status will be read and cleared last
  201. */
  202. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  203. u32 status;
  204. status = readl(&regs->status);
  205. /* We can exit when we've had both RX and TX activity */
  206. if (is_read && (status & SPI_STAT_TXF_EMPTY))
  207. break;
  208. if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
  209. SPI_STAT_RDY)
  210. tm++;
  211. else if (!(status & SPI_STAT_RXF_EMPTY)) {
  212. tmpdin = readl(&regs->rx_fifo);
  213. is_read = 1;
  214. /* swap bytes read in */
  215. if (din != NULL) {
  216. for (i = bytes - 1; i >= 0; --i) {
  217. din[i] = tmpdin & 0xff;
  218. tmpdin >>= 8;
  219. }
  220. din += bytes;
  221. }
  222. }
  223. }
  224. if (tm >= SPI_TIMEOUT)
  225. ret = tm;
  226. /* clear ACK RDY, etc. bits */
  227. writel(readl(&regs->status), &regs->status);
  228. }
  229. if (flags & SPI_XFER_END)
  230. spi_cs_deactivate(slave);
  231. debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
  232. tmpdin, readl(&regs->status));
  233. if (ret) {
  234. printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
  235. return -1;
  236. }
  237. return 0;
  238. }