tegra210_qspi.c 12 KB

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