spi-sun4i.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  1. /*
  2. * Copyright (C) 2012 - 2014 Allwinner Tech
  3. * Pan Nan <pannan@allwinnertech.com>
  4. *
  5. * Copyright (C) 2014 Maxime Ripard
  6. * Maxime Ripard <maxime.ripard@free-electrons.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/io.h>
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_runtime.h>
  21. #include <linux/spi/spi.h>
  22. #define SUN4I_FIFO_DEPTH 64
  23. #define SUN4I_RXDATA_REG 0x00
  24. #define SUN4I_TXDATA_REG 0x04
  25. #define SUN4I_CTL_REG 0x08
  26. #define SUN4I_CTL_ENABLE BIT(0)
  27. #define SUN4I_CTL_MASTER BIT(1)
  28. #define SUN4I_CTL_CPHA BIT(2)
  29. #define SUN4I_CTL_CPOL BIT(3)
  30. #define SUN4I_CTL_CS_ACTIVE_LOW BIT(4)
  31. #define SUN4I_CTL_LMTF BIT(6)
  32. #define SUN4I_CTL_TF_RST BIT(8)
  33. #define SUN4I_CTL_RF_RST BIT(9)
  34. #define SUN4I_CTL_XCH BIT(10)
  35. #define SUN4I_CTL_CS_MASK 0x3000
  36. #define SUN4I_CTL_CS(cs) (((cs) << 12) & SUN4I_CTL_CS_MASK)
  37. #define SUN4I_CTL_DHB BIT(15)
  38. #define SUN4I_CTL_CS_MANUAL BIT(16)
  39. #define SUN4I_CTL_CS_LEVEL BIT(17)
  40. #define SUN4I_CTL_TP BIT(18)
  41. #define SUN4I_INT_CTL_REG 0x0c
  42. #define SUN4I_INT_CTL_TC BIT(16)
  43. #define SUN4I_INT_STA_REG 0x10
  44. #define SUN4I_DMA_CTL_REG 0x14
  45. #define SUN4I_WAIT_REG 0x18
  46. #define SUN4I_CLK_CTL_REG 0x1c
  47. #define SUN4I_CLK_CTL_CDR2_MASK 0xff
  48. #define SUN4I_CLK_CTL_CDR2(div) ((div) & SUN4I_CLK_CTL_CDR2_MASK)
  49. #define SUN4I_CLK_CTL_CDR1_MASK 0xf
  50. #define SUN4I_CLK_CTL_CDR1(div) (((div) & SUN4I_CLK_CTL_CDR1_MASK) << 8)
  51. #define SUN4I_CLK_CTL_DRS BIT(12)
  52. #define SUN4I_BURST_CNT_REG 0x20
  53. #define SUN4I_BURST_CNT(cnt) ((cnt) & 0xffffff)
  54. #define SUN4I_XMIT_CNT_REG 0x24
  55. #define SUN4I_XMIT_CNT(cnt) ((cnt) & 0xffffff)
  56. #define SUN4I_FIFO_STA_REG 0x28
  57. #define SUN4I_FIFO_STA_RF_CNT_MASK 0x7f
  58. #define SUN4I_FIFO_STA_RF_CNT_BITS 0
  59. #define SUN4I_FIFO_STA_TF_CNT_MASK 0x7f
  60. #define SUN4I_FIFO_STA_TF_CNT_BITS 16
  61. struct sun4i_spi {
  62. struct spi_master *master;
  63. void __iomem *base_addr;
  64. struct clk *hclk;
  65. struct clk *mclk;
  66. struct completion done;
  67. const u8 *tx_buf;
  68. u8 *rx_buf;
  69. int len;
  70. };
  71. static inline u32 sun4i_spi_read(struct sun4i_spi *sspi, u32 reg)
  72. {
  73. return readl(sspi->base_addr + reg);
  74. }
  75. static inline void sun4i_spi_write(struct sun4i_spi *sspi, u32 reg, u32 value)
  76. {
  77. writel(value, sspi->base_addr + reg);
  78. }
  79. static inline void sun4i_spi_drain_fifo(struct sun4i_spi *sspi, int len)
  80. {
  81. u32 reg, cnt;
  82. u8 byte;
  83. /* See how much data is available */
  84. reg = sun4i_spi_read(sspi, SUN4I_FIFO_STA_REG);
  85. reg &= SUN4I_FIFO_STA_RF_CNT_MASK;
  86. cnt = reg >> SUN4I_FIFO_STA_RF_CNT_BITS;
  87. if (len > cnt)
  88. len = cnt;
  89. while (len--) {
  90. byte = readb(sspi->base_addr + SUN4I_RXDATA_REG);
  91. if (sspi->rx_buf)
  92. *sspi->rx_buf++ = byte;
  93. }
  94. }
  95. static inline void sun4i_spi_fill_fifo(struct sun4i_spi *sspi, int len)
  96. {
  97. u8 byte;
  98. if (len > sspi->len)
  99. len = sspi->len;
  100. while (len--) {
  101. byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  102. writeb(byte, sspi->base_addr + SUN4I_TXDATA_REG);
  103. sspi->len--;
  104. }
  105. }
  106. static void sun4i_spi_set_cs(struct spi_device *spi, bool enable)
  107. {
  108. struct sun4i_spi *sspi = spi_master_get_devdata(spi->master);
  109. u32 reg;
  110. reg = sun4i_spi_read(sspi, SUN4I_CTL_REG);
  111. reg &= ~SUN4I_CTL_CS_MASK;
  112. reg |= SUN4I_CTL_CS(spi->chip_select);
  113. if (enable)
  114. reg |= SUN4I_CTL_CS_LEVEL;
  115. else
  116. reg &= ~SUN4I_CTL_CS_LEVEL;
  117. /*
  118. * Even though this looks irrelevant since we are supposed to
  119. * be controlling the chip select manually, this bit also
  120. * controls the levels of the chip select for inactive
  121. * devices.
  122. *
  123. * If we don't set it, the chip select level will go low by
  124. * default when the device is idle, which is not really
  125. * expected in the common case where the chip select is active
  126. * low.
  127. */
  128. if (spi->mode & SPI_CS_HIGH)
  129. reg &= ~SUN4I_CTL_CS_ACTIVE_LOW;
  130. else
  131. reg |= SUN4I_CTL_CS_ACTIVE_LOW;
  132. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg);
  133. }
  134. static int sun4i_spi_transfer_one(struct spi_master *master,
  135. struct spi_device *spi,
  136. struct spi_transfer *tfr)
  137. {
  138. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  139. unsigned int mclk_rate, div, timeout;
  140. unsigned int start, end, tx_time;
  141. unsigned int tx_len = 0;
  142. int ret = 0;
  143. u32 reg;
  144. /* We don't support transfer larger than the FIFO */
  145. if (tfr->len > SUN4I_FIFO_DEPTH)
  146. return -EMSGSIZE;
  147. if (tfr->tx_buf && tfr->len >= SUN4I_FIFO_DEPTH)
  148. return -EMSGSIZE;
  149. reinit_completion(&sspi->done);
  150. sspi->tx_buf = tfr->tx_buf;
  151. sspi->rx_buf = tfr->rx_buf;
  152. sspi->len = tfr->len;
  153. /* Clear pending interrupts */
  154. sun4i_spi_write(sspi, SUN4I_INT_STA_REG, ~0);
  155. reg = sun4i_spi_read(sspi, SUN4I_CTL_REG);
  156. /* Reset FIFOs */
  157. sun4i_spi_write(sspi, SUN4I_CTL_REG,
  158. reg | SUN4I_CTL_RF_RST | SUN4I_CTL_TF_RST);
  159. /*
  160. * Setup the transfer control register: Chip Select,
  161. * polarities, etc.
  162. */
  163. if (spi->mode & SPI_CPOL)
  164. reg |= SUN4I_CTL_CPOL;
  165. else
  166. reg &= ~SUN4I_CTL_CPOL;
  167. if (spi->mode & SPI_CPHA)
  168. reg |= SUN4I_CTL_CPHA;
  169. else
  170. reg &= ~SUN4I_CTL_CPHA;
  171. if (spi->mode & SPI_LSB_FIRST)
  172. reg |= SUN4I_CTL_LMTF;
  173. else
  174. reg &= ~SUN4I_CTL_LMTF;
  175. /*
  176. * If it's a TX only transfer, we don't want to fill the RX
  177. * FIFO with bogus data
  178. */
  179. if (sspi->rx_buf)
  180. reg &= ~SUN4I_CTL_DHB;
  181. else
  182. reg |= SUN4I_CTL_DHB;
  183. /* We want to control the chip select manually */
  184. reg |= SUN4I_CTL_CS_MANUAL;
  185. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg);
  186. /* Ensure that we have a parent clock fast enough */
  187. mclk_rate = clk_get_rate(sspi->mclk);
  188. if (mclk_rate < (2 * spi->max_speed_hz)) {
  189. clk_set_rate(sspi->mclk, 2 * spi->max_speed_hz);
  190. mclk_rate = clk_get_rate(sspi->mclk);
  191. }
  192. /*
  193. * Setup clock divider.
  194. *
  195. * We have two choices there. Either we can use the clock
  196. * divide rate 1, which is calculated thanks to this formula:
  197. * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
  198. * Or we can use CDR2, which is calculated with the formula:
  199. * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  200. * Wether we use the former or the latter is set through the
  201. * DRS bit.
  202. *
  203. * First try CDR2, and if we can't reach the expected
  204. * frequency, fall back to CDR1.
  205. */
  206. div = mclk_rate / (2 * spi->max_speed_hz);
  207. if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
  208. if (div > 0)
  209. div--;
  210. reg = SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
  211. } else {
  212. div = ilog2(mclk_rate) - ilog2(spi->max_speed_hz);
  213. reg = SUN4I_CLK_CTL_CDR1(div);
  214. }
  215. sun4i_spi_write(sspi, SUN4I_CLK_CTL_REG, reg);
  216. /* Setup the transfer now... */
  217. if (sspi->tx_buf)
  218. tx_len = tfr->len;
  219. /* Setup the counters */
  220. sun4i_spi_write(sspi, SUN4I_BURST_CNT_REG, SUN4I_BURST_CNT(tfr->len));
  221. sun4i_spi_write(sspi, SUN4I_XMIT_CNT_REG, SUN4I_XMIT_CNT(tx_len));
  222. /*
  223. * Fill the TX FIFO
  224. * Filling the FIFO fully causes timeout for some reason
  225. * at least on spi2 on A10s
  226. */
  227. sun4i_spi_fill_fifo(sspi, SUN4I_FIFO_DEPTH - 1);
  228. /* Enable the interrupts */
  229. sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, SUN4I_INT_CTL_TC);
  230. /* Start the transfer */
  231. reg = sun4i_spi_read(sspi, SUN4I_CTL_REG);
  232. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg | SUN4I_CTL_XCH);
  233. tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
  234. start = jiffies;
  235. timeout = wait_for_completion_timeout(&sspi->done,
  236. msecs_to_jiffies(tx_time));
  237. end = jiffies;
  238. if (!timeout) {
  239. dev_warn(&master->dev,
  240. "%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
  241. dev_name(&spi->dev), tfr->len, tfr->speed_hz,
  242. jiffies_to_msecs(end - start), tx_time);
  243. ret = -ETIMEDOUT;
  244. goto out;
  245. }
  246. sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
  247. out:
  248. sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
  249. return ret;
  250. }
  251. static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
  252. {
  253. struct sun4i_spi *sspi = dev_id;
  254. u32 status = sun4i_spi_read(sspi, SUN4I_INT_STA_REG);
  255. /* Transfer complete */
  256. if (status & SUN4I_INT_CTL_TC) {
  257. sun4i_spi_write(sspi, SUN4I_INT_STA_REG, SUN4I_INT_CTL_TC);
  258. complete(&sspi->done);
  259. return IRQ_HANDLED;
  260. }
  261. return IRQ_NONE;
  262. }
  263. static int sun4i_spi_runtime_resume(struct device *dev)
  264. {
  265. struct spi_master *master = dev_get_drvdata(dev);
  266. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  267. int ret;
  268. ret = clk_prepare_enable(sspi->hclk);
  269. if (ret) {
  270. dev_err(dev, "Couldn't enable AHB clock\n");
  271. goto out;
  272. }
  273. ret = clk_prepare_enable(sspi->mclk);
  274. if (ret) {
  275. dev_err(dev, "Couldn't enable module clock\n");
  276. goto err;
  277. }
  278. sun4i_spi_write(sspi, SUN4I_CTL_REG,
  279. SUN4I_CTL_ENABLE | SUN4I_CTL_MASTER | SUN4I_CTL_TP);
  280. return 0;
  281. err:
  282. clk_disable_unprepare(sspi->hclk);
  283. out:
  284. return ret;
  285. }
  286. static int sun4i_spi_runtime_suspend(struct device *dev)
  287. {
  288. struct spi_master *master = dev_get_drvdata(dev);
  289. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  290. clk_disable_unprepare(sspi->mclk);
  291. clk_disable_unprepare(sspi->hclk);
  292. return 0;
  293. }
  294. static int sun4i_spi_probe(struct platform_device *pdev)
  295. {
  296. struct spi_master *master;
  297. struct sun4i_spi *sspi;
  298. struct resource *res;
  299. int ret = 0, irq;
  300. master = spi_alloc_master(&pdev->dev, sizeof(struct sun4i_spi));
  301. if (!master) {
  302. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  303. return -ENOMEM;
  304. }
  305. platform_set_drvdata(pdev, master);
  306. sspi = spi_master_get_devdata(master);
  307. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  308. sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  309. if (IS_ERR(sspi->base_addr)) {
  310. ret = PTR_ERR(sspi->base_addr);
  311. goto err_free_master;
  312. }
  313. irq = platform_get_irq(pdev, 0);
  314. if (irq < 0) {
  315. dev_err(&pdev->dev, "No spi IRQ specified\n");
  316. ret = -ENXIO;
  317. goto err_free_master;
  318. }
  319. ret = devm_request_irq(&pdev->dev, irq, sun4i_spi_handler,
  320. 0, "sun4i-spi", sspi);
  321. if (ret) {
  322. dev_err(&pdev->dev, "Cannot request IRQ\n");
  323. goto err_free_master;
  324. }
  325. sspi->master = master;
  326. master->set_cs = sun4i_spi_set_cs;
  327. master->transfer_one = sun4i_spi_transfer_one;
  328. master->num_chipselect = 4;
  329. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  330. master->bits_per_word_mask = SPI_BPW_MASK(8);
  331. master->dev.of_node = pdev->dev.of_node;
  332. master->auto_runtime_pm = true;
  333. sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  334. if (IS_ERR(sspi->hclk)) {
  335. dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  336. ret = PTR_ERR(sspi->hclk);
  337. goto err_free_master;
  338. }
  339. sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  340. if (IS_ERR(sspi->mclk)) {
  341. dev_err(&pdev->dev, "Unable to acquire module clock\n");
  342. ret = PTR_ERR(sspi->mclk);
  343. goto err_free_master;
  344. }
  345. init_completion(&sspi->done);
  346. /*
  347. * This wake-up/shutdown pattern is to be able to have the
  348. * device woken up, even if runtime_pm is disabled
  349. */
  350. ret = sun4i_spi_runtime_resume(&pdev->dev);
  351. if (ret) {
  352. dev_err(&pdev->dev, "Couldn't resume the device\n");
  353. goto err_free_master;
  354. }
  355. pm_runtime_set_active(&pdev->dev);
  356. pm_runtime_enable(&pdev->dev);
  357. pm_runtime_idle(&pdev->dev);
  358. ret = devm_spi_register_master(&pdev->dev, master);
  359. if (ret) {
  360. dev_err(&pdev->dev, "cannot register SPI master\n");
  361. goto err_pm_disable;
  362. }
  363. return 0;
  364. err_pm_disable:
  365. pm_runtime_disable(&pdev->dev);
  366. sun4i_spi_runtime_suspend(&pdev->dev);
  367. err_free_master:
  368. spi_master_put(master);
  369. return ret;
  370. }
  371. static int sun4i_spi_remove(struct platform_device *pdev)
  372. {
  373. pm_runtime_force_suspend(&pdev->dev);
  374. return 0;
  375. }
  376. static const struct of_device_id sun4i_spi_match[] = {
  377. { .compatible = "allwinner,sun4i-a10-spi", },
  378. {}
  379. };
  380. MODULE_DEVICE_TABLE(of, sun4i_spi_match);
  381. static const struct dev_pm_ops sun4i_spi_pm_ops = {
  382. .runtime_resume = sun4i_spi_runtime_resume,
  383. .runtime_suspend = sun4i_spi_runtime_suspend,
  384. };
  385. static struct platform_driver sun4i_spi_driver = {
  386. .probe = sun4i_spi_probe,
  387. .remove = sun4i_spi_remove,
  388. .driver = {
  389. .name = "sun4i-spi",
  390. .of_match_table = sun4i_spi_match,
  391. .pm = &sun4i_spi_pm_ops,
  392. },
  393. };
  394. module_platform_driver(sun4i_spi_driver);
  395. MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  396. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  397. MODULE_DESCRIPTION("Allwinner A1X/A20 SPI controller driver");
  398. MODULE_LICENSE("GPL");