tegra210_qspi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * NVIDIA Tegra210 QSPI controller driver
  4. *
  5. * (C) Copyright 2015 NVIDIA Corporation <www.nvidia.com>
  6. */
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <asm/io.h>
  10. #include <asm/arch/clock.h>
  11. #include <asm/arch-tegra/clk_rst.h>
  12. #include <spi.h>
  13. #include <fdtdec.h>
  14. #include "tegra_spi.h"
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /* COMMAND1 */
  17. #define QSPI_CMD1_GO BIT(31)
  18. #define QSPI_CMD1_M_S BIT(30)
  19. #define QSPI_CMD1_MODE_MASK GENMASK(1,0)
  20. #define QSPI_CMD1_MODE_SHIFT 28
  21. #define QSPI_CMD1_CS_SEL_MASK GENMASK(1,0)
  22. #define QSPI_CMD1_CS_SEL_SHIFT 26
  23. #define QSPI_CMD1_CS_POL_INACTIVE0 BIT(22)
  24. #define QSPI_CMD1_CS_SW_HW BIT(21)
  25. #define QSPI_CMD1_CS_SW_VAL BIT(20)
  26. #define QSPI_CMD1_IDLE_SDA_MASK GENMASK(1,0)
  27. #define QSPI_CMD1_IDLE_SDA_SHIFT 18
  28. #define QSPI_CMD1_BIDIR BIT(17)
  29. #define QSPI_CMD1_LSBI_FE BIT(16)
  30. #define QSPI_CMD1_LSBY_FE BIT(15)
  31. #define QSPI_CMD1_BOTH_EN_BIT BIT(14)
  32. #define QSPI_CMD1_BOTH_EN_BYTE BIT(13)
  33. #define QSPI_CMD1_RX_EN BIT(12)
  34. #define QSPI_CMD1_TX_EN BIT(11)
  35. #define QSPI_CMD1_PACKED BIT(5)
  36. #define QSPI_CMD1_BITLEN_MASK GENMASK(4,0)
  37. #define QSPI_CMD1_BITLEN_SHIFT 0
  38. /* COMMAND2 */
  39. #define QSPI_CMD2_TX_CLK_TAP_DELAY BIT(6)
  40. #define QSPI_CMD2_TX_CLK_TAP_DELAY_MASK GENMASK(11,6)
  41. #define QSPI_CMD2_RX_CLK_TAP_DELAY BIT(0)
  42. #define QSPI_CMD2_RX_CLK_TAP_DELAY_MASK GENMASK(5,0)
  43. /* TRANSFER STATUS */
  44. #define QSPI_XFER_STS_RDY BIT(30)
  45. /* FIFO STATUS */
  46. #define QSPI_FIFO_STS_CS_INACTIVE BIT(31)
  47. #define QSPI_FIFO_STS_FRAME_END BIT(30)
  48. #define QSPI_FIFO_STS_RX_FIFO_FLUSH BIT(15)
  49. #define QSPI_FIFO_STS_TX_FIFO_FLUSH BIT(14)
  50. #define QSPI_FIFO_STS_ERR BIT(8)
  51. #define QSPI_FIFO_STS_TX_FIFO_OVF BIT(7)
  52. #define QSPI_FIFO_STS_TX_FIFO_UNR BIT(6)
  53. #define QSPI_FIFO_STS_RX_FIFO_OVF BIT(5)
  54. #define QSPI_FIFO_STS_RX_FIFO_UNR BIT(4)
  55. #define QSPI_FIFO_STS_TX_FIFO_FULL BIT(3)
  56. #define QSPI_FIFO_STS_TX_FIFO_EMPTY BIT(2)
  57. #define QSPI_FIFO_STS_RX_FIFO_FULL BIT(1)
  58. #define QSPI_FIFO_STS_RX_FIFO_EMPTY BIT(0)
  59. #define QSPI_TIMEOUT 1000
  60. struct qspi_regs {
  61. u32 command1; /* 000:QSPI_COMMAND1 register */
  62. u32 command2; /* 004:QSPI_COMMAND2 register */
  63. u32 timing1; /* 008:QSPI_CS_TIM1 register */
  64. u32 timing2; /* 00c:QSPI_CS_TIM2 register */
  65. u32 xfer_status;/* 010:QSPI_TRANS_STATUS register */
  66. u32 fifo_status;/* 014:QSPI_FIFO_STATUS register */
  67. u32 tx_data; /* 018:QSPI_TX_DATA register */
  68. u32 rx_data; /* 01c:QSPI_RX_DATA register */
  69. u32 dma_ctl; /* 020:QSPI_DMA_CTL register */
  70. u32 dma_blk; /* 024:QSPI_DMA_BLK register */
  71. u32 rsvd[56]; /* 028-107 reserved */
  72. u32 tx_fifo; /* 108:QSPI_FIFO1 register */
  73. u32 rsvd2[31]; /* 10c-187 reserved */
  74. u32 rx_fifo; /* 188:QSPI_FIFO2 register */
  75. u32 spare_ctl; /* 18c:QSPI_SPARE_CTRL register */
  76. };
  77. struct tegra210_qspi_priv {
  78. struct qspi_regs *regs;
  79. unsigned int freq;
  80. unsigned int mode;
  81. int periph_id;
  82. int valid;
  83. int last_transaction_us;
  84. };
  85. static int tegra210_qspi_ofdata_to_platdata(struct udevice *bus)
  86. {
  87. struct tegra_spi_platdata *plat = bus->platdata;
  88. const void *blob = gd->fdt_blob;
  89. int node = dev_of_offset(bus);
  90. plat->base = devfdt_get_addr(bus);
  91. plat->periph_id = clock_decode_periph_id(bus);
  92. if (plat->periph_id == PERIPH_ID_NONE) {
  93. debug("%s: could not decode periph id %d\n", __func__,
  94. plat->periph_id);
  95. return -FDT_ERR_NOTFOUND;
  96. }
  97. /* Use 500KHz as a suitable default */
  98. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  99. 500000);
  100. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  101. "spi-deactivate-delay", 0);
  102. debug("%s: base=%#08lx, periph_id=%d, max-frequency=%d, deactivate_delay=%d\n",
  103. __func__, plat->base, plat->periph_id, plat->frequency,
  104. plat->deactivate_delay_us);
  105. return 0;
  106. }
  107. static int tegra210_qspi_probe(struct udevice *bus)
  108. {
  109. struct tegra_spi_platdata *plat = dev_get_platdata(bus);
  110. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  111. priv->regs = (struct qspi_regs *)plat->base;
  112. priv->last_transaction_us = timer_get_us();
  113. priv->freq = plat->frequency;
  114. priv->periph_id = plat->periph_id;
  115. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  116. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq);
  117. return 0;
  118. }
  119. static int tegra210_qspi_claim_bus(struct udevice *bus)
  120. {
  121. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  122. struct qspi_regs *regs = priv->regs;
  123. /* Change SPI clock to correct frequency, PLLP_OUT0 source */
  124. clock_start_periph_pll(priv->periph_id, CLOCK_ID_PERIPH, priv->freq);
  125. debug("%s: FIFO STATUS = %08x\n", __func__, readl(&regs->fifo_status));
  126. /* Set master mode and sw controlled CS */
  127. setbits_le32(&regs->command1, QSPI_CMD1_M_S | QSPI_CMD1_CS_SW_HW |
  128. (priv->mode << QSPI_CMD1_MODE_SHIFT));
  129. debug("%s: COMMAND1 = %08x\n", __func__, readl(&regs->command1));
  130. return 0;
  131. }
  132. /**
  133. * Activate the CS by driving it LOW
  134. *
  135. * @param slave Pointer to spi_slave to which controller has to
  136. * communicate with
  137. */
  138. static void spi_cs_activate(struct udevice *dev)
  139. {
  140. struct udevice *bus = dev->parent;
  141. struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
  142. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  143. /* If it's too soon to do another transaction, wait */
  144. if (pdata->deactivate_delay_us &&
  145. priv->last_transaction_us) {
  146. ulong delay_us; /* The delay completed so far */
  147. delay_us = timer_get_us() - priv->last_transaction_us;
  148. if (delay_us < pdata->deactivate_delay_us)
  149. udelay(pdata->deactivate_delay_us - delay_us);
  150. }
  151. clrbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
  152. }
  153. /**
  154. * Deactivate the CS by driving it HIGH
  155. *
  156. * @param slave Pointer to spi_slave to which controller has to
  157. * communicate with
  158. */
  159. static void spi_cs_deactivate(struct udevice *dev)
  160. {
  161. struct udevice *bus = dev->parent;
  162. struct tegra_spi_platdata *pdata = dev_get_platdata(bus);
  163. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  164. setbits_le32(&priv->regs->command1, QSPI_CMD1_CS_SW_VAL);
  165. /* Remember time of this transaction so we can honour the bus delay */
  166. if (pdata->deactivate_delay_us)
  167. priv->last_transaction_us = timer_get_us();
  168. debug("Deactivate CS, bus '%s'\n", bus->name);
  169. }
  170. static int tegra210_qspi_xfer(struct udevice *dev, unsigned int bitlen,
  171. const void *data_out, void *data_in,
  172. unsigned long flags)
  173. {
  174. struct udevice *bus = dev->parent;
  175. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  176. struct qspi_regs *regs = priv->regs;
  177. u32 reg, tmpdout, tmpdin = 0;
  178. const u8 *dout = data_out;
  179. u8 *din = data_in;
  180. int num_bytes, tm, ret;
  181. debug("%s: slave %u:%u dout %p din %p bitlen %u\n",
  182. __func__, bus->seq, spi_chip_select(dev), dout, din, bitlen);
  183. if (bitlen % 8)
  184. return -1;
  185. num_bytes = bitlen / 8;
  186. ret = 0;
  187. /* clear all error status bits */
  188. reg = readl(&regs->fifo_status);
  189. writel(reg, &regs->fifo_status);
  190. /* flush RX/TX FIFOs */
  191. setbits_le32(&regs->fifo_status,
  192. (QSPI_FIFO_STS_RX_FIFO_FLUSH |
  193. QSPI_FIFO_STS_TX_FIFO_FLUSH));
  194. tm = QSPI_TIMEOUT;
  195. while ((tm && readl(&regs->fifo_status) &
  196. (QSPI_FIFO_STS_RX_FIFO_FLUSH |
  197. QSPI_FIFO_STS_TX_FIFO_FLUSH))) {
  198. tm--;
  199. udelay(1);
  200. }
  201. if (!tm) {
  202. printf("%s: timeout during QSPI FIFO flush!\n",
  203. __func__);
  204. return -1;
  205. }
  206. /*
  207. * Notes:
  208. * 1. don't set LSBY_FE, so no need to swap bytes from/to TX/RX FIFOs;
  209. * 2. don't set RX_EN and TX_EN yet.
  210. * (SW needs to make sure that while programming the blk_size,
  211. * tx_en and rx_en bits must be zero)
  212. * [TODO] I (Yen Lin) have problems when both RX/TX EN bits are set
  213. * i.e., both dout and din are not NULL.
  214. */
  215. clrsetbits_le32(&regs->command1,
  216. (QSPI_CMD1_LSBI_FE | QSPI_CMD1_LSBY_FE |
  217. QSPI_CMD1_RX_EN | QSPI_CMD1_TX_EN),
  218. (spi_chip_select(dev) << QSPI_CMD1_CS_SEL_SHIFT));
  219. /* set xfer size to 1 block (32 bits) */
  220. writel(0, &regs->dma_blk);
  221. if (flags & SPI_XFER_BEGIN)
  222. spi_cs_activate(dev);
  223. /* handle data in 32-bit chunks */
  224. while (num_bytes > 0) {
  225. int bytes;
  226. tmpdout = 0;
  227. bytes = (num_bytes > 4) ? 4 : num_bytes;
  228. if (dout != NULL) {
  229. memcpy((void *)&tmpdout, (void *)dout, bytes);
  230. dout += bytes;
  231. num_bytes -= bytes;
  232. writel(tmpdout, &regs->tx_fifo);
  233. setbits_le32(&regs->command1, QSPI_CMD1_TX_EN);
  234. }
  235. if (din != NULL)
  236. setbits_le32(&regs->command1, QSPI_CMD1_RX_EN);
  237. /* clear ready bit */
  238. setbits_le32(&regs->xfer_status, QSPI_XFER_STS_RDY);
  239. clrsetbits_le32(&regs->command1,
  240. QSPI_CMD1_BITLEN_MASK << QSPI_CMD1_BITLEN_SHIFT,
  241. (bytes * 8 - 1) << QSPI_CMD1_BITLEN_SHIFT);
  242. /* Need to stabilize other reg bits before GO bit set.
  243. * As per the TRM:
  244. * "For successful operation at various freq combinations,
  245. * a minimum of 4-5 spi_clk cycle delay might be required
  246. * before enabling the PIO or DMA bits. The worst case delay
  247. * calculation can be done considering slowest qspi_clk as
  248. * 1MHz. Based on that 1us delay should be enough before
  249. * enabling PIO or DMA." Padded another 1us for safety.
  250. */
  251. udelay(2);
  252. setbits_le32(&regs->command1, QSPI_CMD1_GO);
  253. udelay(1);
  254. /*
  255. * Wait for SPI transmit FIFO to empty, or to time out.
  256. * The RX FIFO status will be read and cleared last
  257. */
  258. for (tm = 0; tm < QSPI_TIMEOUT; ++tm) {
  259. u32 fifo_status, xfer_status;
  260. xfer_status = readl(&regs->xfer_status);
  261. if (!(xfer_status & QSPI_XFER_STS_RDY))
  262. continue;
  263. fifo_status = readl(&regs->fifo_status);
  264. if (fifo_status & QSPI_FIFO_STS_ERR) {
  265. debug("%s: got a fifo error: ", __func__);
  266. if (fifo_status & QSPI_FIFO_STS_TX_FIFO_OVF)
  267. debug("tx FIFO overflow ");
  268. if (fifo_status & QSPI_FIFO_STS_TX_FIFO_UNR)
  269. debug("tx FIFO underrun ");
  270. if (fifo_status & QSPI_FIFO_STS_RX_FIFO_OVF)
  271. debug("rx FIFO overflow ");
  272. if (fifo_status & QSPI_FIFO_STS_RX_FIFO_UNR)
  273. debug("rx FIFO underrun ");
  274. if (fifo_status & QSPI_FIFO_STS_TX_FIFO_FULL)
  275. debug("tx FIFO full ");
  276. if (fifo_status & QSPI_FIFO_STS_TX_FIFO_EMPTY)
  277. debug("tx FIFO empty ");
  278. if (fifo_status & QSPI_FIFO_STS_RX_FIFO_FULL)
  279. debug("rx FIFO full ");
  280. if (fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)
  281. debug("rx FIFO empty ");
  282. debug("\n");
  283. break;
  284. }
  285. if (!(fifo_status & QSPI_FIFO_STS_RX_FIFO_EMPTY)) {
  286. tmpdin = readl(&regs->rx_fifo);
  287. if (din != NULL) {
  288. memcpy(din, &tmpdin, bytes);
  289. din += bytes;
  290. num_bytes -= bytes;
  291. }
  292. }
  293. break;
  294. }
  295. if (tm >= QSPI_TIMEOUT)
  296. ret = tm;
  297. /* clear ACK RDY, etc. bits */
  298. writel(readl(&regs->fifo_status), &regs->fifo_status);
  299. }
  300. if (flags & SPI_XFER_END)
  301. spi_cs_deactivate(dev);
  302. debug("%s: transfer ended. Value=%08x, fifo_status = %08x\n",
  303. __func__, tmpdin, readl(&regs->fifo_status));
  304. if (ret) {
  305. printf("%s: timeout during SPI transfer, tm %d\n",
  306. __func__, ret);
  307. return -1;
  308. }
  309. return ret;
  310. }
  311. static int tegra210_qspi_set_speed(struct udevice *bus, uint speed)
  312. {
  313. struct tegra_spi_platdata *plat = bus->platdata;
  314. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  315. if (speed > plat->frequency)
  316. speed = plat->frequency;
  317. priv->freq = speed;
  318. debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, priv->freq);
  319. return 0;
  320. }
  321. static int tegra210_qspi_set_mode(struct udevice *bus, uint mode)
  322. {
  323. struct tegra210_qspi_priv *priv = dev_get_priv(bus);
  324. priv->mode = mode;
  325. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  326. return 0;
  327. }
  328. static const struct dm_spi_ops tegra210_qspi_ops = {
  329. .claim_bus = tegra210_qspi_claim_bus,
  330. .xfer = tegra210_qspi_xfer,
  331. .set_speed = tegra210_qspi_set_speed,
  332. .set_mode = tegra210_qspi_set_mode,
  333. /*
  334. * cs_info is not needed, since we require all chip selects to be
  335. * in the device tree explicitly
  336. */
  337. };
  338. static const struct udevice_id tegra210_qspi_ids[] = {
  339. { .compatible = "nvidia,tegra210-qspi" },
  340. { }
  341. };
  342. U_BOOT_DRIVER(tegra210_qspi) = {
  343. .name = "tegra210-qspi",
  344. .id = UCLASS_SPI,
  345. .of_match = tegra210_qspi_ids,
  346. .ops = &tegra210_qspi_ops,
  347. .ofdata_to_platdata = tegra210_qspi_ofdata_to_platdata,
  348. .platdata_auto_alloc_size = sizeof(struct tegra_spi_platdata),
  349. .priv_auto_alloc_size = sizeof(struct tegra210_qspi_priv),
  350. .per_child_auto_alloc_size = sizeof(struct spi_slave),
  351. .probe = tegra210_qspi_probe,
  352. };