tegra20_sflash.c 8.6 KB

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