designware_spi.c 12 KB

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