spi-sun4i.c 11 KB

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