designware_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Designware master SPI core controller driver
  3. *
  4. * Copyright (C) 2014 Stefan Roese <sr@denx.de>
  5. *
  6. * Very loosely based on the Linux driver:
  7. * drivers/spi/spi-dw.c, which is:
  8. * Copyright (c) 2009, Intel Corporation.
  9. *
  10. * SPDX-License-Identifier: GPL-2.0
  11. */
  12. #include <common.h>
  13. #include <clk.h>
  14. #include <dm.h>
  15. #include <errno.h>
  16. #include <malloc.h>
  17. #include <spi.h>
  18. #include <fdtdec.h>
  19. #include <linux/compat.h>
  20. #include <linux/iopoll.h>
  21. #include <asm/io.h>
  22. DECLARE_GLOBAL_DATA_PTR;
  23. /* Register offsets */
  24. #define DW_SPI_CTRL0 0x00
  25. #define DW_SPI_CTRL1 0x04
  26. #define DW_SPI_SSIENR 0x08
  27. #define DW_SPI_MWCR 0x0c
  28. #define DW_SPI_SER 0x10
  29. #define DW_SPI_BAUDR 0x14
  30. #define DW_SPI_TXFLTR 0x18
  31. #define DW_SPI_RXFLTR 0x1c
  32. #define DW_SPI_TXFLR 0x20
  33. #define DW_SPI_RXFLR 0x24
  34. #define DW_SPI_SR 0x28
  35. #define DW_SPI_IMR 0x2c
  36. #define DW_SPI_ISR 0x30
  37. #define DW_SPI_RISR 0x34
  38. #define DW_SPI_TXOICR 0x38
  39. #define DW_SPI_RXOICR 0x3c
  40. #define DW_SPI_RXUICR 0x40
  41. #define DW_SPI_MSTICR 0x44
  42. #define DW_SPI_ICR 0x48
  43. #define DW_SPI_DMACR 0x4c
  44. #define DW_SPI_DMATDLR 0x50
  45. #define DW_SPI_DMARDLR 0x54
  46. #define DW_SPI_IDR 0x58
  47. #define DW_SPI_VERSION 0x5c
  48. #define DW_SPI_DR 0x60
  49. /* Bit fields in CTRLR0 */
  50. #define SPI_DFS_OFFSET 0
  51. #define SPI_FRF_OFFSET 4
  52. #define SPI_FRF_SPI 0x0
  53. #define SPI_FRF_SSP 0x1
  54. #define SPI_FRF_MICROWIRE 0x2
  55. #define SPI_FRF_RESV 0x3
  56. #define SPI_MODE_OFFSET 6
  57. #define SPI_SCPH_OFFSET 6
  58. #define SPI_SCOL_OFFSET 7
  59. #define SPI_TMOD_OFFSET 8
  60. #define SPI_TMOD_MASK (0x3 << SPI_TMOD_OFFSET)
  61. #define SPI_TMOD_TR 0x0 /* xmit & recv */
  62. #define SPI_TMOD_TO 0x1 /* xmit only */
  63. #define SPI_TMOD_RO 0x2 /* recv only */
  64. #define SPI_TMOD_EPROMREAD 0x3 /* eeprom read mode */
  65. #define SPI_SLVOE_OFFSET 10
  66. #define SPI_SRL_OFFSET 11
  67. #define SPI_CFS_OFFSET 12
  68. /* Bit fields in SR, 7 bits */
  69. #define SR_MASK GENMASK(6, 0) /* cover 7 bits */
  70. #define SR_BUSY BIT(0)
  71. #define SR_TF_NOT_FULL BIT(1)
  72. #define SR_TF_EMPT BIT(2)
  73. #define SR_RF_NOT_EMPT BIT(3)
  74. #define SR_RF_FULL BIT(4)
  75. #define SR_TX_ERR BIT(5)
  76. #define SR_DCOL BIT(6)
  77. #define RX_TIMEOUT 1000 /* timeout in ms */
  78. struct dw_spi_platdata {
  79. s32 frequency; /* Default clock frequency, -1 for none */
  80. void __iomem *regs;
  81. };
  82. struct dw_spi_priv {
  83. void __iomem *regs;
  84. unsigned int freq; /* Default frequency */
  85. unsigned int mode;
  86. struct clk clk;
  87. unsigned long bus_clk_rate;
  88. int bits_per_word;
  89. u8 cs; /* chip select pin */
  90. u8 tmode; /* TR/TO/RO/EEPROM */
  91. u8 type; /* SPI/SSP/MicroWire */
  92. int len;
  93. u32 fifo_len; /* depth of the FIFO buffer */
  94. void *tx;
  95. void *tx_end;
  96. void *rx;
  97. void *rx_end;
  98. };
  99. static inline u32 dw_readl(struct dw_spi_priv *priv, u32 offset)
  100. {
  101. return __raw_readl(priv->regs + offset);
  102. }
  103. static inline void dw_writel(struct dw_spi_priv *priv, u32 offset, u32 val)
  104. {
  105. __raw_writel(val, priv->regs + offset);
  106. }
  107. static inline u16 dw_readw(struct dw_spi_priv *priv, u32 offset)
  108. {
  109. return __raw_readw(priv->regs + offset);
  110. }
  111. static inline void dw_writew(struct dw_spi_priv *priv, u32 offset, u16 val)
  112. {
  113. __raw_writew(val, priv->regs + offset);
  114. }
  115. static int dw_spi_ofdata_to_platdata(struct udevice *bus)
  116. {
  117. struct dw_spi_platdata *plat = bus->platdata;
  118. const void *blob = gd->fdt_blob;
  119. int node = dev_of_offset(bus);
  120. plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
  121. /* Use 500KHz as a suitable default */
  122. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  123. 500000);
  124. debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
  125. plat->frequency);
  126. return 0;
  127. }
  128. static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
  129. {
  130. dw_writel(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
  131. }
  132. /* Restart the controller, disable all interrupts, clean rx fifo */
  133. static void spi_hw_init(struct dw_spi_priv *priv)
  134. {
  135. spi_enable_chip(priv, 0);
  136. dw_writel(priv, DW_SPI_IMR, 0xff);
  137. spi_enable_chip(priv, 1);
  138. /*
  139. * Try to detect the FIFO depth if not set by interface driver,
  140. * the depth could be from 2 to 256 from HW spec
  141. */
  142. if (!priv->fifo_len) {
  143. u32 fifo;
  144. for (fifo = 1; fifo < 256; fifo++) {
  145. dw_writew(priv, DW_SPI_TXFLTR, fifo);
  146. if (fifo != dw_readw(priv, DW_SPI_TXFLTR))
  147. break;
  148. }
  149. priv->fifo_len = (fifo == 1) ? 0 : fifo;
  150. dw_writew(priv, DW_SPI_TXFLTR, 0);
  151. }
  152. debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
  153. }
  154. /*
  155. * We define dw_spi_get_clk function as 'weak' as some targets
  156. * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
  157. * and implement dw_spi_get_clk their own way in their clock manager.
  158. */
  159. __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
  160. {
  161. struct dw_spi_priv *priv = dev_get_priv(bus);
  162. int ret;
  163. ret = clk_get_by_index(bus, 0, &priv->clk);
  164. if (ret)
  165. return ret;
  166. ret = clk_enable(&priv->clk);
  167. if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
  168. return ret;
  169. *rate = clk_get_rate(&priv->clk);
  170. if (!*rate)
  171. goto err_rate;
  172. debug("%s: get spi controller clk via device tree: %lu Hz\n",
  173. __func__, *rate);
  174. return 0;
  175. err_rate:
  176. clk_disable(&priv->clk);
  177. clk_free(&priv->clk);
  178. return -EINVAL;
  179. }
  180. static int dw_spi_probe(struct udevice *bus)
  181. {
  182. struct dw_spi_platdata *plat = dev_get_platdata(bus);
  183. struct dw_spi_priv *priv = dev_get_priv(bus);
  184. int ret;
  185. priv->regs = plat->regs;
  186. priv->freq = plat->frequency;
  187. ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
  188. if (ret)
  189. return ret;
  190. /* Currently only bits_per_word == 8 supported */
  191. priv->bits_per_word = 8;
  192. priv->tmode = 0; /* Tx & Rx */
  193. /* Basic HW init */
  194. spi_hw_init(priv);
  195. return 0;
  196. }
  197. /* Return the max entries we can fill into tx fifo */
  198. static inline u32 tx_max(struct dw_spi_priv *priv)
  199. {
  200. u32 tx_left, tx_room, rxtx_gap;
  201. tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
  202. tx_room = priv->fifo_len - dw_readw(priv, DW_SPI_TXFLR);
  203. /*
  204. * Another concern is about the tx/rx mismatch, we
  205. * thought about using (priv->fifo_len - rxflr - txflr) as
  206. * one maximum value for tx, but it doesn't cover the
  207. * data which is out of tx/rx fifo and inside the
  208. * shift registers. So a control from sw point of
  209. * view is taken.
  210. */
  211. rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
  212. (priv->bits_per_word >> 3);
  213. return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
  214. }
  215. /* Return the max entries we should read out of rx fifo */
  216. static inline u32 rx_max(struct dw_spi_priv *priv)
  217. {
  218. u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
  219. return min_t(u32, rx_left, dw_readw(priv, DW_SPI_RXFLR));
  220. }
  221. static void dw_writer(struct dw_spi_priv *priv)
  222. {
  223. u32 max = tx_max(priv);
  224. u16 txw = 0;
  225. while (max--) {
  226. /* Set the tx word if the transfer's original "tx" is not null */
  227. if (priv->tx_end - priv->len) {
  228. if (priv->bits_per_word == 8)
  229. txw = *(u8 *)(priv->tx);
  230. else
  231. txw = *(u16 *)(priv->tx);
  232. }
  233. dw_writew(priv, DW_SPI_DR, txw);
  234. debug("%s: tx=0x%02x\n", __func__, txw);
  235. priv->tx += priv->bits_per_word >> 3;
  236. }
  237. }
  238. static int dw_reader(struct dw_spi_priv *priv)
  239. {
  240. unsigned start = get_timer(0);
  241. u32 max;
  242. u16 rxw;
  243. /* Wait for rx data to be ready */
  244. while (rx_max(priv) == 0) {
  245. if (get_timer(start) > RX_TIMEOUT)
  246. return -ETIMEDOUT;
  247. }
  248. max = rx_max(priv);
  249. while (max--) {
  250. rxw = dw_readw(priv, DW_SPI_DR);
  251. debug("%s: rx=0x%02x\n", __func__, rxw);
  252. /*
  253. * Care about rx only if the transfer's original "rx" is
  254. * not null
  255. */
  256. if (priv->rx_end - priv->len) {
  257. if (priv->bits_per_word == 8)
  258. *(u8 *)(priv->rx) = rxw;
  259. else
  260. *(u16 *)(priv->rx) = rxw;
  261. }
  262. priv->rx += priv->bits_per_word >> 3;
  263. }
  264. return 0;
  265. }
  266. static int poll_transfer(struct dw_spi_priv *priv)
  267. {
  268. int ret;
  269. do {
  270. dw_writer(priv);
  271. ret = dw_reader(priv);
  272. if (ret < 0)
  273. return ret;
  274. } while (priv->rx_end > priv->rx);
  275. return 0;
  276. }
  277. static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
  278. const void *dout, void *din, unsigned long flags)
  279. {
  280. struct udevice *bus = dev->parent;
  281. struct dw_spi_priv *priv = dev_get_priv(bus);
  282. const u8 *tx = dout;
  283. u8 *rx = din;
  284. int ret = 0;
  285. u32 cr0 = 0;
  286. u32 val;
  287. u32 cs;
  288. /* spi core configured to do 8 bit transfers */
  289. if (bitlen % 8) {
  290. debug("Non byte aligned SPI transfer.\n");
  291. return -1;
  292. }
  293. cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
  294. (priv->mode << SPI_MODE_OFFSET) |
  295. (priv->tmode << SPI_TMOD_OFFSET);
  296. if (rx && tx)
  297. priv->tmode = SPI_TMOD_TR;
  298. else if (rx)
  299. priv->tmode = SPI_TMOD_RO;
  300. else
  301. priv->tmode = SPI_TMOD_TO;
  302. cr0 &= ~SPI_TMOD_MASK;
  303. cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
  304. priv->len = bitlen >> 3;
  305. debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
  306. priv->tx = (void *)tx;
  307. priv->tx_end = priv->tx + priv->len;
  308. priv->rx = rx;
  309. priv->rx_end = priv->rx + priv->len;
  310. /* Disable controller before writing control registers */
  311. spi_enable_chip(priv, 0);
  312. debug("%s: cr0=%08x\n", __func__, cr0);
  313. /* Reprogram cr0 only if changed */
  314. if (dw_readw(priv, DW_SPI_CTRL0) != cr0)
  315. dw_writew(priv, DW_SPI_CTRL0, cr0);
  316. /*
  317. * Configure the desired SS (slave select 0...3) in the controller
  318. * The DW SPI controller will activate and deactivate this CS
  319. * automatically. So no cs_activate() etc is needed in this driver.
  320. */
  321. cs = spi_chip_select(dev);
  322. dw_writel(priv, DW_SPI_SER, 1 << cs);
  323. /* Enable controller after writing control registers */
  324. spi_enable_chip(priv, 1);
  325. /* Start transfer in a polling loop */
  326. ret = poll_transfer(priv);
  327. /*
  328. * Wait for current transmit operation to complete.
  329. * Otherwise if some data still exists in Tx FIFO it can be
  330. * silently flushed, i.e. dropped on disabling of the controller,
  331. * which happens when writing 0 to DW_SPI_SSIENR which happens
  332. * in the beginning of new transfer.
  333. */
  334. if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
  335. !(val & SR_TF_EMPT) || (val & SR_BUSY),
  336. RX_TIMEOUT * 1000)) {
  337. ret = -ETIMEDOUT;
  338. }
  339. return ret;
  340. }
  341. static int dw_spi_set_speed(struct udevice *bus, uint speed)
  342. {
  343. struct dw_spi_platdata *plat = bus->platdata;
  344. struct dw_spi_priv *priv = dev_get_priv(bus);
  345. u16 clk_div;
  346. if (speed > plat->frequency)
  347. speed = plat->frequency;
  348. /* Disable controller before writing control registers */
  349. spi_enable_chip(priv, 0);
  350. /* clk_div doesn't support odd number */
  351. clk_div = priv->bus_clk_rate / speed;
  352. clk_div = (clk_div + 1) & 0xfffe;
  353. dw_writel(priv, DW_SPI_BAUDR, clk_div);
  354. /* Enable controller after writing control registers */
  355. spi_enable_chip(priv, 1);
  356. priv->freq = speed;
  357. debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
  358. priv->freq, clk_div);
  359. return 0;
  360. }
  361. static int dw_spi_set_mode(struct udevice *bus, uint mode)
  362. {
  363. struct dw_spi_priv *priv = dev_get_priv(bus);
  364. /*
  365. * Can't set mode yet. Since this depends on if rx, tx, or
  366. * rx & tx is requested. So we have to defer this to the
  367. * real transfer function.
  368. */
  369. priv->mode = mode;
  370. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  371. return 0;
  372. }
  373. static const struct dm_spi_ops dw_spi_ops = {
  374. .xfer = dw_spi_xfer,
  375. .set_speed = dw_spi_set_speed,
  376. .set_mode = dw_spi_set_mode,
  377. /*
  378. * cs_info is not needed, since we require all chip selects to be
  379. * in the device tree explicitly
  380. */
  381. };
  382. static const struct udevice_id dw_spi_ids[] = {
  383. { .compatible = "snps,dw-apb-ssi" },
  384. { }
  385. };
  386. U_BOOT_DRIVER(dw_spi) = {
  387. .name = "dw_spi",
  388. .id = UCLASS_SPI,
  389. .of_match = dw_spi_ids,
  390. .ops = &dw_spi_ops,
  391. .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
  392. .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
  393. .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
  394. .probe = dw_spi_probe,
  395. };