tegra20_sflash.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2010-2013 NVIDIA Corporation
  4. * With help from the mpc8xxx SPI driver
  5. * With more help from omap3_spi SPI driver
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <log.h>
  11. #include <time.h>
  12. #include <asm/global_data.h>
  13. #include <asm/io.h>
  14. #include <asm/gpio.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/arch/pinmux.h>
  17. #include <asm/arch-tegra/clk_rst.h>
  18. #include <spi.h>
  19. #include <fdtdec.h>
  20. #include <linux/bitops.h>
  21. #include <linux/delay.h>
  22. #include "tegra_spi.h"
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #define SPI_CMD_GO BIT(30)
  25. #define SPI_CMD_ACTIVE_SCLK_SHIFT 26
  26. #define SPI_CMD_ACTIVE_SCLK_MASK (3 << SPI_CMD_ACTIVE_SCLK_SHIFT)
  27. #define SPI_CMD_CK_SDA BIT(21)
  28. #define SPI_CMD_ACTIVE_SDA_SHIFT 18
  29. #define SPI_CMD_ACTIVE_SDA_MASK (3 << SPI_CMD_ACTIVE_SDA_SHIFT)
  30. #define SPI_CMD_CS_POL BIT(16)
  31. #define SPI_CMD_TXEN BIT(15)
  32. #define SPI_CMD_RXEN BIT(14)
  33. #define SPI_CMD_CS_VAL BIT(13)
  34. #define SPI_CMD_CS_SOFT BIT(12)
  35. #define SPI_CMD_CS_DELAY BIT(9)
  36. #define SPI_CMD_CS3_EN BIT(8)
  37. #define SPI_CMD_CS2_EN BIT(7)
  38. #define SPI_CMD_CS1_EN BIT(6)
  39. #define SPI_CMD_CS0_EN BIT(5)
  40. #define SPI_CMD_BIT_LENGTH BIT(4)
  41. #define SPI_CMD_BIT_LENGTH_MASK GENMASK(4, 0)
  42. #define SPI_STAT_BSY BIT(31)
  43. #define SPI_STAT_RDY BIT(30)
  44. #define SPI_STAT_RXF_FLUSH BIT(29)
  45. #define SPI_STAT_TXF_FLUSH BIT(28)
  46. #define SPI_STAT_RXF_UNR BIT(27)
  47. #define SPI_STAT_TXF_OVF BIT(26)
  48. #define SPI_STAT_RXF_EMPTY BIT(25)
  49. #define SPI_STAT_RXF_FULL BIT(24)
  50. #define SPI_STAT_TXF_EMPTY BIT(23)
  51. #define SPI_STAT_TXF_FULL BIT(22)
  52. #define SPI_STAT_SEL_TXRX_N BIT(16)
  53. #define SPI_STAT_CUR_BLKCNT BIT(15)
  54. #define SPI_TIMEOUT 1000
  55. #define TEGRA_SPI_MAX_FREQ 52000000
  56. struct spi_regs {
  57. u32 command; /* SPI_COMMAND_0 register */
  58. u32 status; /* SPI_STATUS_0 register */
  59. u32 rx_cmp; /* SPI_RX_CMP_0 register */
  60. u32 dma_ctl; /* SPI_DMA_CTL_0 register */
  61. u32 tx_fifo; /* SPI_TX_FIFO_0 register */
  62. u32 rsvd[3]; /* offsets 0x14 to 0x1F reserved */
  63. u32 rx_fifo; /* SPI_RX_FIFO_0 register */
  64. };
  65. struct tegra20_sflash_priv {
  66. struct spi_regs *regs;
  67. unsigned int freq;
  68. unsigned int mode;
  69. int periph_id;
  70. int valid;
  71. int last_transaction_us;
  72. };
  73. int tegra20_sflash_cs_info(struct udevice *bus, unsigned int cs,
  74. struct spi_cs_info *info)
  75. {
  76. /* Tegra20 SPI-Flash - only 1 device ('bus/cs') */
  77. if (cs != 0)
  78. return -EINVAL;
  79. else
  80. return 0;
  81. }
  82. static int tegra20_sflash_of_to_plat(struct udevice *bus)
  83. {
  84. struct tegra_spi_plat *plat = dev_get_plat(bus);
  85. const void *blob = gd->fdt_blob;
  86. int node = dev_of_offset(bus);
  87. plat->base = dev_read_addr(bus);
  88. plat->periph_id = clock_decode_periph_id(bus);
  89. if (plat->periph_id == PERIPH_ID_NONE) {
  90. debug("%s: could not decode periph id %d\n", __func__,
  91. plat->periph_id);
  92. return -FDT_ERR_NOTFOUND;
  93. }
  94. /* Use 500KHz as a suitable default */
  95. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  96. 500000);
  97. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  98. "spi-deactivate-delay", 0);
  99. debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
  100. __func__, plat->base, plat->periph_id, plat->frequency,
  101. plat->deactivate_delay_us);
  102. return 0;
  103. }
  104. static int tegra20_sflash_probe(struct udevice *bus)
  105. {
  106. struct tegra_spi_plat *plat = dev_get_plat(bus);
  107. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  108. priv->regs = (struct spi_regs *)plat->base;
  109. priv->last_transaction_us = timer_get_us();
  110. priv->freq = plat->frequency;
  111. priv->periph_id = plat->periph_id;
  112. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  113. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
  114. priv->freq);
  115. return 0;
  116. }
  117. static int tegra20_sflash_claim_bus(struct udevice *dev)
  118. {
  119. struct udevice *bus = dev->parent;
  120. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  121. struct spi_regs *regs = priv->regs;
  122. u32 reg;
  123. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  124. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH,
  125. priv->freq);
  126. /* Clear stale status here */
  127. reg = SPI_STAT_RDY | SPI_STAT_RXF_FLUSH | SPI_STAT_TXF_FLUSH | \
  128. SPI_STAT_RXF_UNR | SPI_STAT_TXF_OVF;
  129. writel(reg, &regs->status);
  130. debug("%s: STATUS = %08x\n", __func__, readl(&regs->status));
  131. /*
  132. * Use sw-controlled CS, so we can clock in data after ReadID, etc.
  133. */
  134. reg = (priv->mode & 1) << SPI_CMD_ACTIVE_SDA_SHIFT;
  135. if (priv->mode & 2)
  136. reg |= 1 << SPI_CMD_ACTIVE_SCLK_SHIFT;
  137. clrsetbits_le32(&regs->command, SPI_CMD_ACTIVE_SCLK_MASK |
  138. SPI_CMD_ACTIVE_SDA_MASK, SPI_CMD_CS_SOFT | reg);
  139. debug("%s: COMMAND = %08x\n", __func__, readl(&regs->command));
  140. /*
  141. * SPI pins on Tegra20 are muxed - change pinmux later due to UART
  142. * issue.
  143. */
  144. pinmux_set_func(PMUX_PINGRP_GMD, PMUX_FUNC_SFLASH);
  145. pinmux_tristate_disable(PMUX_PINGRP_LSPI);
  146. pinmux_set_func(PMUX_PINGRP_GMC, PMUX_FUNC_SFLASH);
  147. return 0;
  148. }
  149. static void spi_cs_activate(struct udevice *dev)
  150. {
  151. struct udevice *bus = dev->parent;
  152. struct tegra_spi_plat *pdata = dev_get_plat(bus);
  153. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  154. /* If it's too soon to do another transaction, wait */
  155. if (pdata->deactivate_delay_us &&
  156. priv->last_transaction_us) {
  157. ulong delay_us; /* The delay completed so far */
  158. delay_us = timer_get_us() - priv->last_transaction_us;
  159. if (delay_us < pdata->deactivate_delay_us)
  160. udelay(pdata->deactivate_delay_us - delay_us);
  161. }
  162. /* CS is negated on Tegra, so drive a 1 to get a 0 */
  163. setbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
  164. }
  165. static void spi_cs_deactivate(struct udevice *dev)
  166. {
  167. struct udevice *bus = dev->parent;
  168. struct tegra_spi_plat *pdata = dev_get_plat(bus);
  169. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  170. /* CS is negated on Tegra, so drive a 0 to get a 1 */
  171. clrbits_le32(&priv->regs->command, SPI_CMD_CS_VAL);
  172. /* Remember time of this transaction so we can honour the bus delay */
  173. if (pdata->deactivate_delay_us)
  174. priv->last_transaction_us = timer_get_us();
  175. }
  176. static int tegra20_sflash_xfer(struct udevice *dev, unsigned int bitlen,
  177. const void *data_out, void *data_in,
  178. unsigned long flags)
  179. {
  180. struct udevice *bus = dev->parent;
  181. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  182. struct spi_regs *regs = priv->regs;
  183. u32 reg, tmpdout, tmpdin = 0;
  184. const u8 *dout = data_out;
  185. u8 *din = data_in;
  186. int num_bytes;
  187. int ret;
  188. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  189. __func__, dev_seq(bus), spi_chip_select(dev), dout, din, bitlen);
  190. if (bitlen % 8)
  191. return -1;
  192. num_bytes = bitlen / 8;
  193. ret = 0;
  194. reg = readl(&regs->status);
  195. writel(reg, &regs->status); /* Clear all SPI events via R/W */
  196. debug("spi_xfer entry: STATUS = %08x\n", reg);
  197. reg = readl(&regs->command);
  198. reg |= SPI_CMD_TXEN | SPI_CMD_RXEN;
  199. writel(reg, &regs->command);
  200. debug("spi_xfer: COMMAND = %08x\n", readl(&regs->command));
  201. if (flags & SPI_XFER_BEGIN)
  202. spi_cs_activate(dev);
  203. /* handle data in 32-bit chunks */
  204. while (num_bytes > 0) {
  205. int bytes;
  206. int is_read = 0;
  207. int tm, i;
  208. tmpdout = 0;
  209. bytes = (num_bytes > 4) ? 4 : num_bytes;
  210. if (dout != NULL) {
  211. for (i = 0; i < bytes; ++i)
  212. tmpdout = (tmpdout << 8) | dout[i];
  213. }
  214. num_bytes -= bytes;
  215. if (dout)
  216. dout += bytes;
  217. clrsetbits_le32(&regs->command, SPI_CMD_BIT_LENGTH_MASK,
  218. bytes * 8 - 1);
  219. writel(tmpdout, &regs->tx_fifo);
  220. setbits_le32(&regs->command, SPI_CMD_GO);
  221. /*
  222. * Wait for SPI transmit FIFO to empty, or to time out.
  223. * The RX FIFO status will be read and cleared last
  224. */
  225. for (tm = 0, is_read = 0; tm < SPI_TIMEOUT; ++tm) {
  226. u32 status;
  227. status = readl(&regs->status);
  228. /* We can exit when we've had both RX and TX activity */
  229. if (is_read && (status & SPI_STAT_TXF_EMPTY))
  230. break;
  231. if ((status & (SPI_STAT_BSY | SPI_STAT_RDY)) !=
  232. SPI_STAT_RDY)
  233. tm++;
  234. else if (!(status & SPI_STAT_RXF_EMPTY)) {
  235. tmpdin = readl(&regs->rx_fifo);
  236. is_read = 1;
  237. /* swap bytes read in */
  238. if (din != NULL) {
  239. for (i = bytes - 1; i >= 0; --i) {
  240. din[i] = tmpdin & 0xff;
  241. tmpdin >>= 8;
  242. }
  243. din += bytes;
  244. }
  245. }
  246. }
  247. if (tm >= SPI_TIMEOUT)
  248. ret = tm;
  249. /* clear ACK RDY, etc. bits */
  250. writel(readl(&regs->status), &regs->status);
  251. }
  252. if (flags & SPI_XFER_END)
  253. spi_cs_deactivate(dev);
  254. debug("spi_xfer: transfer ended. Value=%08x, status = %08x\n",
  255. tmpdin, readl(&regs->status));
  256. if (ret) {
  257. printf("spi_xfer: timeout during SPI transfer, tm %d\n", ret);
  258. return -1;
  259. }
  260. return 0;
  261. }
  262. static int tegra20_sflash_set_speed(struct udevice *bus, uint speed)
  263. {
  264. struct tegra_spi_plat *plat = dev_get_plat(bus);
  265. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  266. if (speed > plat->frequency)
  267. speed = plat->frequency;
  268. priv->freq = speed;
  269. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  270. return 0;
  271. }
  272. static int tegra20_sflash_set_mode(struct udevice *bus, uint mode)
  273. {
  274. struct tegra20_sflash_priv *priv = dev_get_priv(bus);
  275. priv->mode = mode;
  276. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  277. return 0;
  278. }
  279. static const struct dm_spi_ops tegra20_sflash_ops = {
  280. .claim_bus = tegra20_sflash_claim_bus,
  281. .xfer = tegra20_sflash_xfer,
  282. .set_speed = tegra20_sflash_set_speed,
  283. .set_mode = tegra20_sflash_set_mode,
  284. .cs_info = tegra20_sflash_cs_info,
  285. };
  286. static const struct udevice_id tegra20_sflash_ids[] = {
  287. { .compatible = "nvidia,tegra20-sflash" },
  288. { }
  289. };
  290. U_BOOT_DRIVER(tegra20_sflash) = {
  291. .name = "tegra20_sflash",
  292. .id = UCLASS_SPI,
  293. .of_match = tegra20_sflash_ids,
  294. .ops = &tegra20_sflash_ops,
  295. .of_to_plat = tegra20_sflash_of_to_plat,
  296. .plat_auto = sizeof(struct tegra_spi_plat),
  297. .priv_auto = sizeof(struct tegra20_sflash_priv),
  298. .probe = tegra20_sflash_probe,
  299. };