designware_spi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Designware master SPI core controller driver
  4. *
  5. * Copyright (C) 2014 Stefan Roese <sr@denx.de>
  6. *
  7. * Very loosely based on the Linux driver:
  8. * drivers/spi/spi-dw.c, which is:
  9. * Copyright (c) 2009, Intel Corporation.
  10. */
  11. #include <asm-generic/gpio.h>
  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. struct gpio_desc cs_gpio; /* External chip-select gpio */
  89. int bits_per_word;
  90. u8 cs; /* chip select pin */
  91. u8 tmode; /* TR/TO/RO/EEPROM */
  92. u8 type; /* SPI/SSP/MicroWire */
  93. int len;
  94. u32 fifo_len; /* depth of the FIFO buffer */
  95. void *tx;
  96. void *tx_end;
  97. void *rx;
  98. void *rx_end;
  99. };
  100. static inline u32 dw_read(struct dw_spi_priv *priv, u32 offset)
  101. {
  102. return __raw_readl(priv->regs + offset);
  103. }
  104. static inline void dw_write(struct dw_spi_priv *priv, u32 offset, u32 val)
  105. {
  106. __raw_writel(val, priv->regs + offset);
  107. }
  108. static int request_gpio_cs(struct udevice *bus)
  109. {
  110. #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
  111. struct dw_spi_priv *priv = dev_get_priv(bus);
  112. int ret;
  113. /* External chip select gpio line is optional */
  114. ret = gpio_request_by_name(bus, "cs-gpio", 0, &priv->cs_gpio, 0);
  115. if (ret == -ENOENT)
  116. return 0;
  117. if (ret < 0) {
  118. printf("Error: %d: Can't get %s gpio!\n", ret, bus->name);
  119. return ret;
  120. }
  121. if (dm_gpio_is_valid(&priv->cs_gpio)) {
  122. dm_gpio_set_dir_flags(&priv->cs_gpio,
  123. GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
  124. }
  125. debug("%s: used external gpio for CS management\n", __func__);
  126. #endif
  127. return 0;
  128. }
  129. static int dw_spi_ofdata_to_platdata(struct udevice *bus)
  130. {
  131. struct dw_spi_platdata *plat = bus->platdata;
  132. const void *blob = gd->fdt_blob;
  133. int node = dev_of_offset(bus);
  134. plat->regs = (struct dw_spi *)devfdt_get_addr(bus);
  135. /* Use 500KHz as a suitable default */
  136. plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency",
  137. 500000);
  138. debug("%s: regs=%p max-frequency=%d\n", __func__, plat->regs,
  139. plat->frequency);
  140. return request_gpio_cs(bus);
  141. }
  142. static inline void spi_enable_chip(struct dw_spi_priv *priv, int enable)
  143. {
  144. dw_write(priv, DW_SPI_SSIENR, (enable ? 1 : 0));
  145. }
  146. /* Restart the controller, disable all interrupts, clean rx fifo */
  147. static void spi_hw_init(struct dw_spi_priv *priv)
  148. {
  149. spi_enable_chip(priv, 0);
  150. dw_write(priv, DW_SPI_IMR, 0xff);
  151. spi_enable_chip(priv, 1);
  152. /*
  153. * Try to detect the FIFO depth if not set by interface driver,
  154. * the depth could be from 2 to 256 from HW spec
  155. */
  156. if (!priv->fifo_len) {
  157. u32 fifo;
  158. for (fifo = 1; fifo < 256; fifo++) {
  159. dw_write(priv, DW_SPI_TXFLTR, fifo);
  160. if (fifo != dw_read(priv, DW_SPI_TXFLTR))
  161. break;
  162. }
  163. priv->fifo_len = (fifo == 1) ? 0 : fifo;
  164. dw_write(priv, DW_SPI_TXFLTR, 0);
  165. }
  166. debug("%s: fifo_len=%d\n", __func__, priv->fifo_len);
  167. }
  168. /*
  169. * We define dw_spi_get_clk function as 'weak' as some targets
  170. * (like SOCFPGA_GEN5 and SOCFPGA_ARRIA10) don't use standard clock API
  171. * and implement dw_spi_get_clk their own way in their clock manager.
  172. */
  173. __weak int dw_spi_get_clk(struct udevice *bus, ulong *rate)
  174. {
  175. struct dw_spi_priv *priv = dev_get_priv(bus);
  176. int ret;
  177. ret = clk_get_by_index(bus, 0, &priv->clk);
  178. if (ret)
  179. return ret;
  180. ret = clk_enable(&priv->clk);
  181. if (ret && ret != -ENOSYS && ret != -ENOTSUPP)
  182. return ret;
  183. *rate = clk_get_rate(&priv->clk);
  184. if (!*rate)
  185. goto err_rate;
  186. debug("%s: get spi controller clk via device tree: %lu Hz\n",
  187. __func__, *rate);
  188. return 0;
  189. err_rate:
  190. clk_disable(&priv->clk);
  191. clk_free(&priv->clk);
  192. return -EINVAL;
  193. }
  194. static int dw_spi_probe(struct udevice *bus)
  195. {
  196. struct dw_spi_platdata *plat = dev_get_platdata(bus);
  197. struct dw_spi_priv *priv = dev_get_priv(bus);
  198. int ret;
  199. priv->regs = plat->regs;
  200. priv->freq = plat->frequency;
  201. ret = dw_spi_get_clk(bus, &priv->bus_clk_rate);
  202. if (ret)
  203. return ret;
  204. /* Currently only bits_per_word == 8 supported */
  205. priv->bits_per_word = 8;
  206. priv->tmode = 0; /* Tx & Rx */
  207. /* Basic HW init */
  208. spi_hw_init(priv);
  209. return 0;
  210. }
  211. /* Return the max entries we can fill into tx fifo */
  212. static inline u32 tx_max(struct dw_spi_priv *priv)
  213. {
  214. u32 tx_left, tx_room, rxtx_gap;
  215. tx_left = (priv->tx_end - priv->tx) / (priv->bits_per_word >> 3);
  216. tx_room = priv->fifo_len - dw_read(priv, DW_SPI_TXFLR);
  217. /*
  218. * Another concern is about the tx/rx mismatch, we
  219. * thought about using (priv->fifo_len - rxflr - txflr) as
  220. * one maximum value for tx, but it doesn't cover the
  221. * data which is out of tx/rx fifo and inside the
  222. * shift registers. So a control from sw point of
  223. * view is taken.
  224. */
  225. rxtx_gap = ((priv->rx_end - priv->rx) - (priv->tx_end - priv->tx)) /
  226. (priv->bits_per_word >> 3);
  227. return min3(tx_left, tx_room, (u32)(priv->fifo_len - rxtx_gap));
  228. }
  229. /* Return the max entries we should read out of rx fifo */
  230. static inline u32 rx_max(struct dw_spi_priv *priv)
  231. {
  232. u32 rx_left = (priv->rx_end - priv->rx) / (priv->bits_per_word >> 3);
  233. return min_t(u32, rx_left, dw_read(priv, DW_SPI_RXFLR));
  234. }
  235. static void dw_writer(struct dw_spi_priv *priv)
  236. {
  237. u32 max = tx_max(priv);
  238. u16 txw = 0;
  239. while (max--) {
  240. /* Set the tx word if the transfer's original "tx" is not null */
  241. if (priv->tx_end - priv->len) {
  242. if (priv->bits_per_word == 8)
  243. txw = *(u8 *)(priv->tx);
  244. else
  245. txw = *(u16 *)(priv->tx);
  246. }
  247. dw_write(priv, DW_SPI_DR, txw);
  248. debug("%s: tx=0x%02x\n", __func__, txw);
  249. priv->tx += priv->bits_per_word >> 3;
  250. }
  251. }
  252. static void dw_reader(struct dw_spi_priv *priv)
  253. {
  254. u32 max = rx_max(priv);
  255. u16 rxw;
  256. while (max--) {
  257. rxw = dw_read(priv, DW_SPI_DR);
  258. debug("%s: rx=0x%02x\n", __func__, rxw);
  259. /* Care about rx if the transfer's original "rx" is not null */
  260. if (priv->rx_end - priv->len) {
  261. if (priv->bits_per_word == 8)
  262. *(u8 *)(priv->rx) = rxw;
  263. else
  264. *(u16 *)(priv->rx) = rxw;
  265. }
  266. priv->rx += priv->bits_per_word >> 3;
  267. }
  268. }
  269. static int poll_transfer(struct dw_spi_priv *priv)
  270. {
  271. do {
  272. dw_writer(priv);
  273. dw_reader(priv);
  274. } while (priv->rx_end > priv->rx);
  275. return 0;
  276. }
  277. static void external_cs_manage(struct udevice *dev, bool on)
  278. {
  279. #if defined(CONFIG_DM_GPIO) && !defined(CONFIG_SPL_BUILD)
  280. struct dw_spi_priv *priv = dev_get_priv(dev->parent);
  281. if (!dm_gpio_is_valid(&priv->cs_gpio))
  282. return;
  283. dm_gpio_set_value(&priv->cs_gpio, on ? 1 : 0);
  284. #endif
  285. }
  286. static int dw_spi_xfer(struct udevice *dev, unsigned int bitlen,
  287. const void *dout, void *din, unsigned long flags)
  288. {
  289. struct udevice *bus = dev->parent;
  290. struct dw_spi_priv *priv = dev_get_priv(bus);
  291. const u8 *tx = dout;
  292. u8 *rx = din;
  293. int ret = 0;
  294. u32 cr0 = 0;
  295. u32 val;
  296. u32 cs;
  297. /* spi core configured to do 8 bit transfers */
  298. if (bitlen % 8) {
  299. debug("Non byte aligned SPI transfer.\n");
  300. return -1;
  301. }
  302. /* Start the transaction if necessary. */
  303. if (flags & SPI_XFER_BEGIN)
  304. external_cs_manage(dev, false);
  305. cr0 = (priv->bits_per_word - 1) | (priv->type << SPI_FRF_OFFSET) |
  306. (priv->mode << SPI_MODE_OFFSET) |
  307. (priv->tmode << SPI_TMOD_OFFSET);
  308. if (rx && tx)
  309. priv->tmode = SPI_TMOD_TR;
  310. else if (rx)
  311. priv->tmode = SPI_TMOD_RO;
  312. else
  313. /*
  314. * In transmit only mode (SPI_TMOD_TO) input FIFO never gets
  315. * any data which breaks our logic in poll_transfer() above.
  316. */
  317. priv->tmode = SPI_TMOD_TR;
  318. cr0 &= ~SPI_TMOD_MASK;
  319. cr0 |= (priv->tmode << SPI_TMOD_OFFSET);
  320. priv->len = bitlen >> 3;
  321. debug("%s: rx=%p tx=%p len=%d [bytes]\n", __func__, rx, tx, priv->len);
  322. priv->tx = (void *)tx;
  323. priv->tx_end = priv->tx + priv->len;
  324. priv->rx = rx;
  325. priv->rx_end = priv->rx + priv->len;
  326. /* Disable controller before writing control registers */
  327. spi_enable_chip(priv, 0);
  328. debug("%s: cr0=%08x\n", __func__, cr0);
  329. /* Reprogram cr0 only if changed */
  330. if (dw_read(priv, DW_SPI_CTRL0) != cr0)
  331. dw_write(priv, DW_SPI_CTRL0, cr0);
  332. /*
  333. * Configure the desired SS (slave select 0...3) in the controller
  334. * The DW SPI controller will activate and deactivate this CS
  335. * automatically. So no cs_activate() etc is needed in this driver.
  336. */
  337. cs = spi_chip_select(dev);
  338. dw_write(priv, DW_SPI_SER, 1 << cs);
  339. /* Enable controller after writing control registers */
  340. spi_enable_chip(priv, 1);
  341. /* Start transfer in a polling loop */
  342. ret = poll_transfer(priv);
  343. /*
  344. * Wait for current transmit operation to complete.
  345. * Otherwise if some data still exists in Tx FIFO it can be
  346. * silently flushed, i.e. dropped on disabling of the controller,
  347. * which happens when writing 0 to DW_SPI_SSIENR which happens
  348. * in the beginning of new transfer.
  349. */
  350. if (readl_poll_timeout(priv->regs + DW_SPI_SR, val,
  351. (val & SR_TF_EMPT) && !(val & SR_BUSY),
  352. RX_TIMEOUT * 1000)) {
  353. ret = -ETIMEDOUT;
  354. }
  355. /* Stop the transaction if necessary */
  356. if (flags & SPI_XFER_END)
  357. external_cs_manage(dev, true);
  358. return ret;
  359. }
  360. static int dw_spi_set_speed(struct udevice *bus, uint speed)
  361. {
  362. struct dw_spi_platdata *plat = bus->platdata;
  363. struct dw_spi_priv *priv = dev_get_priv(bus);
  364. u16 clk_div;
  365. if (speed > plat->frequency)
  366. speed = plat->frequency;
  367. /* Disable controller before writing control registers */
  368. spi_enable_chip(priv, 0);
  369. /* clk_div doesn't support odd number */
  370. clk_div = priv->bus_clk_rate / speed;
  371. clk_div = (clk_div + 1) & 0xfffe;
  372. dw_write(priv, DW_SPI_BAUDR, clk_div);
  373. /* Enable controller after writing control registers */
  374. spi_enable_chip(priv, 1);
  375. priv->freq = speed;
  376. debug("%s: regs=%p speed=%d clk_div=%d\n", __func__, priv->regs,
  377. priv->freq, clk_div);
  378. return 0;
  379. }
  380. static int dw_spi_set_mode(struct udevice *bus, uint mode)
  381. {
  382. struct dw_spi_priv *priv = dev_get_priv(bus);
  383. /*
  384. * Can't set mode yet. Since this depends on if rx, tx, or
  385. * rx & tx is requested. So we have to defer this to the
  386. * real transfer function.
  387. */
  388. priv->mode = mode;
  389. debug("%s: regs=%p, mode=%d\n", __func__, priv->regs, priv->mode);
  390. return 0;
  391. }
  392. static const struct dm_spi_ops dw_spi_ops = {
  393. .xfer = dw_spi_xfer,
  394. .set_speed = dw_spi_set_speed,
  395. .set_mode = dw_spi_set_mode,
  396. /*
  397. * cs_info is not needed, since we require all chip selects to be
  398. * in the device tree explicitly
  399. */
  400. };
  401. static const struct udevice_id dw_spi_ids[] = {
  402. { .compatible = "snps,dw-apb-ssi" },
  403. { }
  404. };
  405. U_BOOT_DRIVER(dw_spi) = {
  406. .name = "dw_spi",
  407. .id = UCLASS_SPI,
  408. .of_match = dw_spi_ids,
  409. .ops = &dw_spi_ops,
  410. .ofdata_to_platdata = dw_spi_ofdata_to_platdata,
  411. .platdata_auto_alloc_size = sizeof(struct dw_spi_platdata),
  412. .priv_auto_alloc_size = sizeof(struct dw_spi_priv),
  413. .probe = dw_spi_probe,
  414. };