tegra20_sflash.c 9.2 KB

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