spi-sun4i.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. /* We want to control the chip select manually */
  114. reg |= SUN4I_CTL_CS_MANUAL;
  115. if (enable)
  116. reg |= SUN4I_CTL_CS_LEVEL;
  117. else
  118. reg &= ~SUN4I_CTL_CS_LEVEL;
  119. /*
  120. * Even though this looks irrelevant since we are supposed to
  121. * be controlling the chip select manually, this bit also
  122. * controls the levels of the chip select for inactive
  123. * devices.
  124. *
  125. * If we don't set it, the chip select level will go low by
  126. * default when the device is idle, which is not really
  127. * expected in the common case where the chip select is active
  128. * low.
  129. */
  130. if (spi->mode & SPI_CS_HIGH)
  131. reg &= ~SUN4I_CTL_CS_ACTIVE_LOW;
  132. else
  133. reg |= SUN4I_CTL_CS_ACTIVE_LOW;
  134. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg);
  135. }
  136. static size_t sun4i_spi_max_transfer_size(struct spi_device *spi)
  137. {
  138. return SUN4I_FIFO_DEPTH - 1;
  139. }
  140. static int sun4i_spi_transfer_one(struct spi_master *master,
  141. struct spi_device *spi,
  142. struct spi_transfer *tfr)
  143. {
  144. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  145. unsigned int mclk_rate, div, timeout;
  146. unsigned int start, end, tx_time;
  147. unsigned int tx_len = 0;
  148. int ret = 0;
  149. u32 reg;
  150. /* We don't support transfer larger than the FIFO */
  151. if (tfr->len > SUN4I_FIFO_DEPTH)
  152. return -EMSGSIZE;
  153. if (tfr->tx_buf && tfr->len >= SUN4I_FIFO_DEPTH)
  154. return -EMSGSIZE;
  155. reinit_completion(&sspi->done);
  156. sspi->tx_buf = tfr->tx_buf;
  157. sspi->rx_buf = tfr->rx_buf;
  158. sspi->len = tfr->len;
  159. /* Clear pending interrupts */
  160. sun4i_spi_write(sspi, SUN4I_INT_STA_REG, ~0);
  161. reg = sun4i_spi_read(sspi, SUN4I_CTL_REG);
  162. /* Reset FIFOs */
  163. sun4i_spi_write(sspi, SUN4I_CTL_REG,
  164. reg | SUN4I_CTL_RF_RST | SUN4I_CTL_TF_RST);
  165. /*
  166. * Setup the transfer control register: Chip Select,
  167. * polarities, etc.
  168. */
  169. if (spi->mode & SPI_CPOL)
  170. reg |= SUN4I_CTL_CPOL;
  171. else
  172. reg &= ~SUN4I_CTL_CPOL;
  173. if (spi->mode & SPI_CPHA)
  174. reg |= SUN4I_CTL_CPHA;
  175. else
  176. reg &= ~SUN4I_CTL_CPHA;
  177. if (spi->mode & SPI_LSB_FIRST)
  178. reg |= SUN4I_CTL_LMTF;
  179. else
  180. reg &= ~SUN4I_CTL_LMTF;
  181. /*
  182. * If it's a TX only transfer, we don't want to fill the RX
  183. * FIFO with bogus data
  184. */
  185. if (sspi->rx_buf)
  186. reg &= ~SUN4I_CTL_DHB;
  187. else
  188. reg |= SUN4I_CTL_DHB;
  189. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg);
  190. /* Ensure that we have a parent clock fast enough */
  191. mclk_rate = clk_get_rate(sspi->mclk);
  192. if (mclk_rate < (2 * tfr->speed_hz)) {
  193. clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
  194. mclk_rate = clk_get_rate(sspi->mclk);
  195. }
  196. /*
  197. * Setup clock divider.
  198. *
  199. * We have two choices there. Either we can use the clock
  200. * divide rate 1, which is calculated thanks to this formula:
  201. * SPI_CLK = MOD_CLK / (2 ^ (cdr + 1))
  202. * Or we can use CDR2, which is calculated with the formula:
  203. * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  204. * Wether we use the former or the latter is set through the
  205. * DRS bit.
  206. *
  207. * First try CDR2, and if we can't reach the expected
  208. * frequency, fall back to CDR1.
  209. */
  210. div = mclk_rate / (2 * tfr->speed_hz);
  211. if (div <= (SUN4I_CLK_CTL_CDR2_MASK + 1)) {
  212. if (div > 0)
  213. div--;
  214. reg = SUN4I_CLK_CTL_CDR2(div) | SUN4I_CLK_CTL_DRS;
  215. } else {
  216. div = ilog2(mclk_rate) - ilog2(tfr->speed_hz);
  217. reg = SUN4I_CLK_CTL_CDR1(div);
  218. }
  219. sun4i_spi_write(sspi, SUN4I_CLK_CTL_REG, reg);
  220. /* Setup the transfer now... */
  221. if (sspi->tx_buf)
  222. tx_len = tfr->len;
  223. /* Setup the counters */
  224. sun4i_spi_write(sspi, SUN4I_BURST_CNT_REG, SUN4I_BURST_CNT(tfr->len));
  225. sun4i_spi_write(sspi, SUN4I_XMIT_CNT_REG, SUN4I_XMIT_CNT(tx_len));
  226. /*
  227. * Fill the TX FIFO
  228. * Filling the FIFO fully causes timeout for some reason
  229. * at least on spi2 on A10s
  230. */
  231. sun4i_spi_fill_fifo(sspi, SUN4I_FIFO_DEPTH - 1);
  232. /* Enable the interrupts */
  233. sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, SUN4I_INT_CTL_TC);
  234. /* Start the transfer */
  235. reg = sun4i_spi_read(sspi, SUN4I_CTL_REG);
  236. sun4i_spi_write(sspi, SUN4I_CTL_REG, reg | SUN4I_CTL_XCH);
  237. tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
  238. start = jiffies;
  239. timeout = wait_for_completion_timeout(&sspi->done,
  240. msecs_to_jiffies(tx_time));
  241. end = jiffies;
  242. if (!timeout) {
  243. dev_warn(&master->dev,
  244. "%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
  245. dev_name(&spi->dev), tfr->len, tfr->speed_hz,
  246. jiffies_to_msecs(end - start), tx_time);
  247. ret = -ETIMEDOUT;
  248. goto out;
  249. }
  250. sun4i_spi_drain_fifo(sspi, SUN4I_FIFO_DEPTH);
  251. out:
  252. sun4i_spi_write(sspi, SUN4I_INT_CTL_REG, 0);
  253. return ret;
  254. }
  255. static irqreturn_t sun4i_spi_handler(int irq, void *dev_id)
  256. {
  257. struct sun4i_spi *sspi = dev_id;
  258. u32 status = sun4i_spi_read(sspi, SUN4I_INT_STA_REG);
  259. /* Transfer complete */
  260. if (status & SUN4I_INT_CTL_TC) {
  261. sun4i_spi_write(sspi, SUN4I_INT_STA_REG, SUN4I_INT_CTL_TC);
  262. complete(&sspi->done);
  263. return IRQ_HANDLED;
  264. }
  265. return IRQ_NONE;
  266. }
  267. static int sun4i_spi_runtime_resume(struct device *dev)
  268. {
  269. struct spi_master *master = dev_get_drvdata(dev);
  270. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  271. int ret;
  272. ret = clk_prepare_enable(sspi->hclk);
  273. if (ret) {
  274. dev_err(dev, "Couldn't enable AHB clock\n");
  275. goto out;
  276. }
  277. ret = clk_prepare_enable(sspi->mclk);
  278. if (ret) {
  279. dev_err(dev, "Couldn't enable module clock\n");
  280. goto err;
  281. }
  282. sun4i_spi_write(sspi, SUN4I_CTL_REG,
  283. SUN4I_CTL_ENABLE | SUN4I_CTL_MASTER | SUN4I_CTL_TP);
  284. return 0;
  285. err:
  286. clk_disable_unprepare(sspi->hclk);
  287. out:
  288. return ret;
  289. }
  290. static int sun4i_spi_runtime_suspend(struct device *dev)
  291. {
  292. struct spi_master *master = dev_get_drvdata(dev);
  293. struct sun4i_spi *sspi = spi_master_get_devdata(master);
  294. clk_disable_unprepare(sspi->mclk);
  295. clk_disable_unprepare(sspi->hclk);
  296. return 0;
  297. }
  298. static int sun4i_spi_probe(struct platform_device *pdev)
  299. {
  300. struct spi_master *master;
  301. struct sun4i_spi *sspi;
  302. struct resource *res;
  303. int ret = 0, irq;
  304. master = spi_alloc_master(&pdev->dev, sizeof(struct sun4i_spi));
  305. if (!master) {
  306. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  307. return -ENOMEM;
  308. }
  309. platform_set_drvdata(pdev, master);
  310. sspi = spi_master_get_devdata(master);
  311. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  312. sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  313. if (IS_ERR(sspi->base_addr)) {
  314. ret = PTR_ERR(sspi->base_addr);
  315. goto err_free_master;
  316. }
  317. irq = platform_get_irq(pdev, 0);
  318. if (irq < 0) {
  319. dev_err(&pdev->dev, "No spi IRQ specified\n");
  320. ret = -ENXIO;
  321. goto err_free_master;
  322. }
  323. ret = devm_request_irq(&pdev->dev, irq, sun4i_spi_handler,
  324. 0, "sun4i-spi", sspi);
  325. if (ret) {
  326. dev_err(&pdev->dev, "Cannot request IRQ\n");
  327. goto err_free_master;
  328. }
  329. sspi->master = master;
  330. master->max_speed_hz = 100 * 1000 * 1000;
  331. master->min_speed_hz = 3 * 1000;
  332. master->set_cs = sun4i_spi_set_cs;
  333. master->transfer_one = sun4i_spi_transfer_one;
  334. master->num_chipselect = 4;
  335. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  336. master->bits_per_word_mask = SPI_BPW_MASK(8);
  337. master->dev.of_node = pdev->dev.of_node;
  338. master->auto_runtime_pm = true;
  339. master->max_transfer_size = sun4i_spi_max_transfer_size;
  340. sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  341. if (IS_ERR(sspi->hclk)) {
  342. dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  343. ret = PTR_ERR(sspi->hclk);
  344. goto err_free_master;
  345. }
  346. sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  347. if (IS_ERR(sspi->mclk)) {
  348. dev_err(&pdev->dev, "Unable to acquire module clock\n");
  349. ret = PTR_ERR(sspi->mclk);
  350. goto err_free_master;
  351. }
  352. init_completion(&sspi->done);
  353. /*
  354. * This wake-up/shutdown pattern is to be able to have the
  355. * device woken up, even if runtime_pm is disabled
  356. */
  357. ret = sun4i_spi_runtime_resume(&pdev->dev);
  358. if (ret) {
  359. dev_err(&pdev->dev, "Couldn't resume the device\n");
  360. goto err_free_master;
  361. }
  362. pm_runtime_set_active(&pdev->dev);
  363. pm_runtime_enable(&pdev->dev);
  364. pm_runtime_idle(&pdev->dev);
  365. ret = devm_spi_register_master(&pdev->dev, master);
  366. if (ret) {
  367. dev_err(&pdev->dev, "cannot register SPI master\n");
  368. goto err_pm_disable;
  369. }
  370. return 0;
  371. err_pm_disable:
  372. pm_runtime_disable(&pdev->dev);
  373. sun4i_spi_runtime_suspend(&pdev->dev);
  374. err_free_master:
  375. spi_master_put(master);
  376. return ret;
  377. }
  378. static int sun4i_spi_remove(struct platform_device *pdev)
  379. {
  380. pm_runtime_force_suspend(&pdev->dev);
  381. return 0;
  382. }
  383. static const struct of_device_id sun4i_spi_match[] = {
  384. { .compatible = "allwinner,sun4i-a10-spi", },
  385. {}
  386. };
  387. MODULE_DEVICE_TABLE(of, sun4i_spi_match);
  388. static const struct dev_pm_ops sun4i_spi_pm_ops = {
  389. .runtime_resume = sun4i_spi_runtime_resume,
  390. .runtime_suspend = sun4i_spi_runtime_suspend,
  391. };
  392. static struct platform_driver sun4i_spi_driver = {
  393. .probe = sun4i_spi_probe,
  394. .remove = sun4i_spi_remove,
  395. .driver = {
  396. .name = "sun4i-spi",
  397. .of_match_table = sun4i_spi_match,
  398. .pm = &sun4i_spi_pm_ops,
  399. },
  400. };
  401. module_platform_driver(sun4i_spi_driver);
  402. MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  403. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  404. MODULE_DESCRIPTION("Allwinner A1X/A20 SPI controller driver");
  405. MODULE_LICENSE("GPL");