spi-sun4i.c 11 KB

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