tegra_spi.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. * Copyright (c) 2010-2012 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 <asm/io.h>
  27. #include <asm/gpio.h>
  28. #include <asm/arch/clock.h>
  29. #include <asm/arch/pinmux.h>
  30. #include <asm/arch/uart-spi-switch.h>
  31. #include <asm/arch-tegra/clk_rst.h>
  32. #include <asm/arch-tegra/tegra_spi.h>
  33. #include <spi.h>
  34. #include <fdtdec.h>
  35. DECLARE_GLOBAL_DATA_PTR;
  36. #if defined(CONFIG_SPI_CORRUPTS_UART)
  37. #define corrupt_delay() udelay(CONFIG_SPI_CORRUPTS_UART_DLY);
  38. #else
  39. #define corrupt_delay()
  40. #endif
  41. struct tegra_spi_slave {
  42. struct spi_slave slave;
  43. struct spi_tegra *regs;
  44. unsigned int freq;
  45. unsigned int mode;
  46. int periph_id;
  47. };
  48. static inline struct tegra_spi_slave *to_tegra_spi(struct spi_slave *slave)
  49. {
  50. return container_of(slave, struct tegra_spi_slave, slave);
  51. }
  52. int spi_cs_is_valid(unsigned int bus, unsigned int cs)
  53. {
  54. /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
  55. if (bus != 0 || cs != 0)
  56. return 0;
  57. else
  58. return 1;
  59. }
  60. struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
  61. unsigned int max_hz, unsigned int mode)
  62. {
  63. struct tegra_spi_slave *spi;
  64. if (!spi_cs_is_valid(bus, cs)) {
  65. printf("SPI error: unsupported bus %d / chip select %d\n",
  66. bus, cs);
  67. return NULL;
  68. }
  69. if (max_hz > TEGRA_SPI_MAX_FREQ) {
  70. printf("SPI error: unsupported frequency %d Hz. Max frequency"
  71. " is %d Hz\n", max_hz, TEGRA_SPI_MAX_FREQ);
  72. return NULL;
  73. }
  74. spi = spi_alloc_slave(struct tegra_spi_slave, bus, cs);
  75. if (!spi) {
  76. printf("SPI error: malloc of SPI structure failed\n");
  77. return NULL;
  78. }
  79. #ifdef CONFIG_OF_CONTROL
  80. int node = fdtdec_next_compatible(gd->fdt_blob, 0,
  81. COMPAT_NVIDIA_TEGRA20_SFLASH);
  82. if (node < 0) {
  83. debug("%s: cannot locate sflash node\n", __func__);
  84. return NULL;
  85. }
  86. if (!fdtdec_get_is_enabled(gd->fdt_blob, node)) {
  87. debug("%s: sflash is disabled\n", __func__);
  88. return NULL;
  89. }
  90. spi->regs = (struct spi_tegra *)fdtdec_get_addr(gd->fdt_blob,
  91. node, "reg");
  92. if ((fdt_addr_t)spi->regs == FDT_ADDR_T_NONE) {
  93. debug("%s: no sflash register found\n", __func__);
  94. return NULL;
  95. }
  96. spi->freq = fdtdec_get_int(gd->fdt_blob, node, "spi-max-frequency", 0);
  97. if (!spi->freq) {
  98. debug("%s: no sflash max frequency found\n", __func__);
  99. return NULL;
  100. }
  101. spi->periph_id = clock_decode_periph_id(gd->fdt_blob, node);
  102. if (spi->periph_id == PERIPH_ID_NONE) {
  103. debug("%s: could not decode periph id\n", __func__);
  104. return NULL;
  105. }
  106. #else
  107. spi->regs = (struct spi_tegra *)NV_PA_SPI_BASE;
  108. spi->freq = TEGRA_SPI_MAX_FREQ;
  109. spi->periph_id = PERIPH_ID_SPI1;
  110. #endif
  111. if (max_hz < spi->freq) {
  112. debug("%s: limiting frequency from %u to %u\n", __func__,
  113. spi->freq, max_hz);
  114. spi->freq = max_hz;
  115. }
  116. debug("%s: controller initialized at %p, freq = %u, periph_id = %d\n",
  117. __func__, spi->regs, spi->freq, spi->periph_id);
  118. spi->mode = mode;
  119. return &spi->slave;
  120. }
  121. void spi_free_slave(struct spi_slave *slave)
  122. {
  123. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  124. free(spi);
  125. }
  126. void spi_init(void)
  127. {
  128. /* do nothing */
  129. }
  130. int spi_claim_bus(struct spi_slave *slave)
  131. {
  132. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  133. struct spi_tegra *regs = spi->regs;
  134. u32 reg;
  135. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  136. clock_start_periph_pll(spi->periph_id, CLOCK_ID_PERIPH, spi->freq);
  137. /* Clear stale status here */
  138. reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
  139. SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
  140. writel(reg, &regs->status);
  141. debug("spi_init: STATUS = %08x\n", readl(&regs->status));
  142. /*
  143. * Use sw-controlled CS, so we can clock in data after ReadID, etc.
  144. */
  145. reg = (spi->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
  146. if (spi->mode & 2)
  147. reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
  148. clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
  149. SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
  150. debug("spi_init: COMMAND = %08x\n", readl(&regs->command));
  151. /*
  152. * SPI pins on Tegra20 are muxed - change pinmux later due to UART
  153. * issue.
  154. */
  155. pinmux_set_func(PINGRP_GMD, PMUX_FUNC_SFLASH);
  156. pinmux_tristate_disable(PINGRP_LSPI);
  157. #ifndef CONFIG_SPI_UART_SWITCH
  158. /*
  159. * NOTE:
  160. * Only set PinMux bits 3:2 to SPI here on boards that don't have the
  161. * SPI UART switch or subsequent UART data won't go out! See
  162. * spi_uart_switch().
  163. */
  164. /* TODO: pinmux_set_func(PINGRP_GMC, PMUX_FUNC_SFLASH); */
  165. #endif
  166. return 0;
  167. }
  168. void spi_release_bus(struct spi_slave *slave)
  169. {
  170. /*
  171. * We can't release UART_DISABLE and set pinmux to UART4 here since
  172. * some code (e,g, spi_flash_probe) uses printf() while the SPI
  173. * bus is held. That is arguably bad, but it has the advantage of
  174. * already being in the source tree.
  175. */
  176. }
  177. void spi_cs_activate(struct spi_slave *slave)
  178. {
  179. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  180. pinmux_select_spi();
  181. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  182. setbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
  183. corrupt_delay(); /* Let UART settle */
  184. }
  185. void spi_cs_deactivate(struct spi_slave *slave)
  186. {
  187. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  188. pinmux_select_uart();
  189. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  190. clrbits_le32(&spi->regs->command, SPI_CMD_CS_VAL);
  191. corrupt_delay(); /* Let SPI settle */
  192. }
  193. int spi_xfer(struct spi_slave *slave, unsigned int bitlen,
  194. const void *data_out, void *data_in, unsigned long flags)
  195. {
  196. struct tegra_spi_slave *spi = to_tegra_spi(slave);
  197. struct spi_tegra *regs = spi->regs;
  198. u32 reg, tmpdout, tmpdin = 0;
  199. const u8 *dout = data_out;
  200. u8 *din = data_in;
  201. int num_bytes;
  202. int ret;
  203. debug("spi_xfer: slave %u:%u dout %08X din %08X bitlen %u\n",
  204. slave->bus, slave->cs, *(u8 *)dout, *(u8 *)din, bitlen);
  205. if (bitlen % 8)
  206. return -1;
  207. num_bytes = bitlen / 8;
  208. ret = 0;
  209. reg = readl(&regs->status);
  210. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  211. debug("spi_xfer entry: STATUS = %08x\n", reg);
  212. reg = readl(&regs->command);
  213. reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
  214. writel(reg, &regs->command);
  215. debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
  216. if (flags & SPI_XFER_BEGIN)
  217. spi_cs_activate(slave);
  218. /* handle data in 32-bit chunks */
  219. while (num_bytes > 0) {
  220. int bytes;
  221. int is_read = 0;
  222. int tm, i;
  223. tmpdout = 0;
  224. bytes = (num_bytes > 4) ? 4 : num_bytes;
  225. if (dout != NULL) {
  226. for (i = 0; i < bytes; ++i)
  227. tmpdout = (tmpdout << 8) | dout[i];
  228. }
  229. num_bytes -= bytes;
  230. if (dout)
  231. dout += bytes;
  232. clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
  233. bytes * 8 - 1);
  234. writel(tmpdout, &regs->tx_fifo);
  235. setbits_le32(&regs->command, SPI_CMD_GO);
  236. /*
  237. * Wait for SPI transmit FIFO to empty, or to time out.
  238. * The RX FIFO status will be read and cleared last
  239. */
  240. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  241. u32 status;
  242. status = readl(&regs->status);
  243. /* We can exit when we've had both RX and TX activity */
  244. if (is_read && (status & SPI_STAT_TXF_EMPTY))
  245. break;
  246. if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
  247. SPI_STAT_RDY)
  248. tm++;
  249. else if (!(status & SPI_STAT_RXF_EMPTY)) {
  250. tmpdin = readl(&regs->rx_fifo);
  251. is_read = 1;
  252. /* swap bytes read in */
  253. if (din != NULL) {
  254. for (i = bytes - 1; i >= 0; --i) {
  255. din[i] = tmpdin & 0xff;
  256. tmpdin >>= 8;
  257. }
  258. din += bytes;
  259. }
  260. }
  261. }
  262. if (tm >= SPI_TIMEOUT)
  263. ret = tm;
  264. /* clear ACK RDY, etc. bits */
  265. writel(readl(&regs->status), &regs->status);
  266. }
  267. if (flags & SPI_XFER_END)
  268. spi_cs_deactivate(slave);
  269. debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
  270. tmpdin, readl(&regs->status));
  271. if (ret) {
  272. printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
  273. return -1;
  274. }
  275. return 0;
  276. }