zynq_spi.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2013 Xilinx, Inc.
  4. * (C) Copyright 2015 Jagan Teki <jteki@openedev.com>
  5. *
  6. * Xilinx Zynq PS SPI controller driver (master mode only)
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <malloc.h>
  11. #include <spi.h>
  12. #include <asm/io.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /* zynq spi register bit masks ZYNQ_SPI_<REG>_<BIT>_MASK */
  15. #define ZYNQ_SPI_CR_MSA_MASK BIT(15) /* Manual start enb */
  16. #define ZYNQ_SPI_CR_MCS_MASK BIT(14) /* Manual chip select */
  17. #define ZYNQ_SPI_CR_CS_MASK GENMASK(13, 10) /* Chip select */
  18. #define ZYNQ_SPI_CR_BAUD_MASK GENMASK(5, 3) /* Baud rate div */
  19. #define ZYNQ_SPI_CR_CPHA_MASK BIT(2) /* Clock phase */
  20. #define ZYNQ_SPI_CR_CPOL_MASK BIT(1) /* Clock polarity */
  21. #define ZYNQ_SPI_CR_MSTREN_MASK BIT(0) /* Mode select */
  22. #define ZYNQ_SPI_IXR_RXNEMPTY_MASK BIT(4) /* RX_FIFO_not_empty */
  23. #define ZYNQ_SPI_IXR_TXOW_MASK BIT(2) /* TX_FIFO_not_full */
  24. #define ZYNQ_SPI_IXR_ALL_MASK GENMASK(6, 0) /* All IXR bits */
  25. #define ZYNQ_SPI_ENR_SPI_EN_MASK BIT(0) /* SPI Enable */
  26. #define ZYNQ_SPI_CR_BAUD_MAX 8 /* Baud rate divisor max val */
  27. #define ZYNQ_SPI_CR_BAUD_SHIFT 3 /* Baud rate divisor shift */
  28. #define ZYNQ_SPI_CR_SS_SHIFT 10 /* Slave select shift */
  29. #define ZYNQ_SPI_FIFO_DEPTH 128
  30. #ifndef CONFIG_SYS_ZYNQ_SPI_WAIT
  31. #define CONFIG_SYS_ZYNQ_SPI_WAIT (CONFIG_SYS_HZ/100) /* 10 ms */
  32. #endif
  33. /* zynq spi register set */
  34. struct zynq_spi_regs {
  35. u32 cr; /* 0x00 */
  36. u32 isr; /* 0x04 */
  37. u32 ier; /* 0x08 */
  38. u32 idr; /* 0x0C */
  39. u32 imr; /* 0x10 */
  40. u32 enr; /* 0x14 */
  41. u32 dr; /* 0x18 */
  42. u32 txdr; /* 0x1C */
  43. u32 rxdr; /* 0x20 */
  44. };
  45. /* zynq spi platform data */
  46. struct zynq_spi_platdata {
  47. struct zynq_spi_regs *regs;
  48. u32 frequency; /* input frequency */
  49. u32 speed_hz;
  50. uint deactivate_delay_us; /* Delay to wait after deactivate */
  51. uint activate_delay_us; /* Delay to wait after activate */
  52. };
  53. /* zynq spi priv */
  54. struct zynq_spi_priv {
  55. struct zynq_spi_regs *regs;
  56. u8 cs;
  57. u8 mode;
  58. ulong last_transaction_us; /* Time of last transaction end */
  59. u8 fifo_depth;
  60. u32 freq; /* required frequency */
  61. };
  62. static int zynq_spi_ofdata_to_platdata(struct udevice *bus)
  63. {
  64. struct zynq_spi_platdata *plat = bus->platdata;
  65. const void *blob = gd->fdt_blob;
  66. int node = dev_of_offset(bus);
  67. plat->regs = (struct zynq_spi_regs *)devfdt_get_addr(bus);
  68. /* FIXME: Use 250MHz as a suitable default */
  69. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  70. 250000000);
  71. plat->deactivate_delay_us = fdtdec_get_int(blob, node,
  72. "spi-deactivate-delay", 0);
  73. plat->activate_delay_us = fdtdec_get_int(blob, node,
  74. "spi-activate-delay", 0);
  75. plat->speed_hz = plat->frequency / 2;
  76. debug("%s: regs=%p max-frequency=%d\n", __func__,
  77. plat->regs, plat->frequency);
  78. return 0;
  79. }
  80. static void zynq_spi_init_hw(struct zynq_spi_priv *priv)
  81. {
  82. struct zynq_spi_regs *regs = priv->regs;
  83. u32 confr;
  84. /* Disable SPI */
  85. confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
  86. writel(~confr, &regs->enr);
  87. /* Disable Interrupts */
  88. writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->idr);
  89. /* Clear RX FIFO */
  90. while (readl(&regs->isr) &
  91. ZYNQ_SPI_IXR_RXNEMPTY_MASK)
  92. readl(&regs->rxdr);
  93. /* Clear Interrupts */
  94. writel(ZYNQ_SPI_IXR_ALL_MASK, &regs->isr);
  95. /* Manual slave select and Auto start */
  96. confr = ZYNQ_SPI_CR_MCS_MASK | ZYNQ_SPI_CR_CS_MASK |
  97. ZYNQ_SPI_CR_MSTREN_MASK;
  98. confr &= ~ZYNQ_SPI_CR_MSA_MASK;
  99. writel(confr, &regs->cr);
  100. /* Enable SPI */
  101. writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
  102. }
  103. static int zynq_spi_probe(struct udevice *bus)
  104. {
  105. struct zynq_spi_platdata *plat = dev_get_platdata(bus);
  106. struct zynq_spi_priv *priv = dev_get_priv(bus);
  107. priv->regs = plat->regs;
  108. priv->fifo_depth = ZYNQ_SPI_FIFO_DEPTH;
  109. /* init the zynq spi hw */
  110. zynq_spi_init_hw(priv);
  111. return 0;
  112. }
  113. static void spi_cs_activate(struct udevice *dev)
  114. {
  115. struct udevice *bus = dev->parent;
  116. struct zynq_spi_platdata *plat = bus->platdata;
  117. struct zynq_spi_priv *priv = dev_get_priv(bus);
  118. struct zynq_spi_regs *regs = priv->regs;
  119. u32 cr;
  120. /* If it's too soon to do another transaction, wait */
  121. if (plat->deactivate_delay_us && priv->last_transaction_us) {
  122. ulong delay_us; /* The delay completed so far */
  123. delay_us = timer_get_us() - priv->last_transaction_us;
  124. if (delay_us < plat->deactivate_delay_us)
  125. udelay(plat->deactivate_delay_us - delay_us);
  126. }
  127. clrbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
  128. cr = readl(&regs->cr);
  129. /*
  130. * CS cal logic: CS[13:10]
  131. * xxx0 - cs0
  132. * xx01 - cs1
  133. * x011 - cs2
  134. */
  135. cr |= (~(1 << priv->cs) << ZYNQ_SPI_CR_SS_SHIFT) & ZYNQ_SPI_CR_CS_MASK;
  136. writel(cr, &regs->cr);
  137. if (plat->activate_delay_us)
  138. udelay(plat->activate_delay_us);
  139. }
  140. static void spi_cs_deactivate(struct udevice *dev)
  141. {
  142. struct udevice *bus = dev->parent;
  143. struct zynq_spi_platdata *plat = bus->platdata;
  144. struct zynq_spi_priv *priv = dev_get_priv(bus);
  145. struct zynq_spi_regs *regs = priv->regs;
  146. setbits_le32(&regs->cr, ZYNQ_SPI_CR_CS_MASK);
  147. /* Remember time of this transaction so we can honour the bus delay */
  148. if (plat->deactivate_delay_us)
  149. priv->last_transaction_us = timer_get_us();
  150. }
  151. static int zynq_spi_claim_bus(struct udevice *dev)
  152. {
  153. struct udevice *bus = dev->parent;
  154. struct zynq_spi_priv *priv = dev_get_priv(bus);
  155. struct zynq_spi_regs *regs = priv->regs;
  156. writel(ZYNQ_SPI_ENR_SPI_EN_MASK, &regs->enr);
  157. return 0;
  158. }
  159. static int zynq_spi_release_bus(struct udevice *dev)
  160. {
  161. struct udevice *bus = dev->parent;
  162. struct zynq_spi_priv *priv = dev_get_priv(bus);
  163. struct zynq_spi_regs *regs = priv->regs;
  164. u32 confr;
  165. confr = ZYNQ_SPI_ENR_SPI_EN_MASK;
  166. writel(~confr, &regs->enr);
  167. return 0;
  168. }
  169. static int zynq_spi_xfer(struct udevice *dev, unsigned int bitlen,
  170. const void *dout, void *din, unsigned long flags)
  171. {
  172. struct udevice *bus = dev->parent;
  173. struct zynq_spi_priv *priv = dev_get_priv(bus);
  174. struct zynq_spi_regs *regs = priv->regs;
  175. struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
  176. u32 len = bitlen / 8;
  177. u32 tx_len = len, rx_len = len, tx_tvl;
  178. const u8 *tx_buf = dout;
  179. u8 *rx_buf = din, buf;
  180. u32 ts, status;
  181. debug("spi_xfer: bus:%i cs:%i bitlen:%i len:%i flags:%lx\n",
  182. bus->seq, slave_plat->cs, bitlen, len, flags);
  183. if (bitlen % 8) {
  184. debug("spi_xfer: Non byte aligned SPI transfer\n");
  185. return -1;
  186. }
  187. priv->cs = slave_plat->cs;
  188. if (flags & SPI_XFER_BEGIN)
  189. spi_cs_activate(dev);
  190. while (rx_len > 0) {
  191. /* Write the data into TX FIFO - tx threshold is fifo_depth */
  192. tx_tvl = 0;
  193. while ((tx_tvl < priv->fifo_depth) && tx_len) {
  194. if (tx_buf)
  195. buf = *tx_buf++;
  196. else
  197. buf = 0;
  198. writel(buf, &regs->txdr);
  199. tx_len--;
  200. tx_tvl++;
  201. }
  202. /* Check TX FIFO completion */
  203. ts = get_timer(0);
  204. status = readl(&regs->isr);
  205. while (!(status & ZYNQ_SPI_IXR_TXOW_MASK)) {
  206. if (get_timer(ts) > CONFIG_SYS_ZYNQ_SPI_WAIT) {
  207. printf("spi_xfer: Timeout! TX FIFO not full\n");
  208. return -1;
  209. }
  210. status = readl(&regs->isr);
  211. }
  212. /* Read the data from RX FIFO */
  213. status = readl(&regs->isr);
  214. while ((status & ZYNQ_SPI_IXR_RXNEMPTY_MASK) && rx_len) {
  215. buf = readl(&regs->rxdr);
  216. if (rx_buf)
  217. *rx_buf++ = buf;
  218. status = readl(&regs->isr);
  219. rx_len--;
  220. }
  221. }
  222. if (flags & SPI_XFER_END)
  223. spi_cs_deactivate(dev);
  224. return 0;
  225. }
  226. static int zynq_spi_set_speed(struct udevice *bus, uint speed)
  227. {
  228. struct zynq_spi_platdata *plat = bus->platdata;
  229. struct zynq_spi_priv *priv = dev_get_priv(bus);
  230. struct zynq_spi_regs *regs = priv->regs;
  231. uint32_t confr;
  232. u8 baud_rate_val = 0;
  233. if (speed > plat->frequency)
  234. speed = plat->frequency;
  235. /* Set the clock frequency */
  236. confr = readl(&regs->cr);
  237. if (speed == 0) {
  238. /* Set baudrate x8, if the freq is 0 */
  239. baud_rate_val = 0x2;
  240. } else if (plat->speed_hz != speed) {
  241. while ((baud_rate_val < ZYNQ_SPI_CR_BAUD_MAX) &&
  242. ((plat->frequency /
  243. (2 << baud_rate_val)) > speed))
  244. baud_rate_val++;
  245. plat->speed_hz = speed / (2 << baud_rate_val);
  246. }
  247. confr &= ~ZYNQ_SPI_CR_BAUD_MASK;
  248. confr |= (baud_rate_val << ZYNQ_SPI_CR_BAUD_SHIFT);
  249. writel(confr, &regs->cr);
  250. priv->freq = speed;
  251. debug("zynq_spi_set_speed: regs=%p, speed=%d\n",
  252. priv->regs, priv->freq);
  253. return 0;
  254. }
  255. static int zynq_spi_set_mode(struct udevice *bus, uint mode)
  256. {
  257. struct zynq_spi_priv *priv = dev_get_priv(bus);
  258. struct zynq_spi_regs *regs = priv->regs;
  259. uint32_t confr;
  260. /* Set the SPI Clock phase and polarities */
  261. confr = readl(&regs->cr);
  262. confr &= ~(ZYNQ_SPI_CR_CPHA_MASK | ZYNQ_SPI_CR_CPOL_MASK);
  263. if (mode & SPI_CPHA)
  264. confr |= ZYNQ_SPI_CR_CPHA_MASK;
  265. if (mode & SPI_CPOL)
  266. confr |= ZYNQ_SPI_CR_CPOL_MASK;
  267. writel(confr, &regs->cr);
  268. priv->mode = mode;
  269. debug("zynq_spi_set_mode: regs=%p, mode=%d\n", priv->regs, priv->mode);
  270. return 0;
  271. }
  272. static const struct dm_spi_ops zynq_spi_ops = {
  273. .claim_bus = zynq_spi_claim_bus,
  274. .release_bus = zynq_spi_release_bus,
  275. .xfer = zynq_spi_xfer,
  276. .set_speed = zynq_spi_set_speed,
  277. .set_mode = zynq_spi_set_mode,
  278. };
  279. static const struct udevice_id zynq_spi_ids[] = {
  280. { .compatible = "xlnx,zynq-spi-r1p6" },
  281. { .compatible = "cdns,spi-r1p6" },
  282. { }
  283. };
  284. U_BOOT_DRIVER(zynq_spi) = {
  285. .name = "zynq_spi",
  286. .id = UCLASS_SPI,
  287. .of_match = zynq_spi_ids,
  288. .ops = &zynq_spi_ops,
  289. .ofdata_to_platdata = zynq_spi_ofdata_to_platdata,
  290. .platdata_auto_alloc_size = sizeof(struct zynq_spi_platdata),
  291. .priv_auto_alloc_size = sizeof(struct zynq_spi_priv),
  292. .probe = zynq_spi_probe,
  293. };