designware_spi.c 11 KB

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