spi-sun6i.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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/reset.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/spi/spi.h>
  24. #define SUN6I_FIFO_DEPTH 128
  25. #define SUN6I_GBL_CTL_REG 0x04
  26. #define SUN6I_GBL_CTL_BUS_ENABLE BIT(0)
  27. #define SUN6I_GBL_CTL_MASTER BIT(1)
  28. #define SUN6I_GBL_CTL_TP BIT(7)
  29. #define SUN6I_GBL_CTL_RST BIT(31)
  30. #define SUN6I_TFR_CTL_REG 0x08
  31. #define SUN6I_TFR_CTL_CPHA BIT(0)
  32. #define SUN6I_TFR_CTL_CPOL BIT(1)
  33. #define SUN6I_TFR_CTL_SPOL BIT(2)
  34. #define SUN6I_TFR_CTL_CS_MASK 0x30
  35. #define SUN6I_TFR_CTL_CS(cs) (((cs) << 4) & SUN6I_TFR_CTL_CS_MASK)
  36. #define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
  37. #define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
  38. #define SUN6I_TFR_CTL_DHB BIT(8)
  39. #define SUN6I_TFR_CTL_FBS BIT(12)
  40. #define SUN6I_TFR_CTL_XCH BIT(31)
  41. #define SUN6I_INT_CTL_REG 0x10
  42. #define SUN6I_INT_CTL_RF_OVF BIT(8)
  43. #define SUN6I_INT_CTL_TC BIT(12)
  44. #define SUN6I_INT_STA_REG 0x14
  45. #define SUN6I_FIFO_CTL_REG 0x18
  46. #define SUN6I_FIFO_CTL_RF_RST BIT(15)
  47. #define SUN6I_FIFO_CTL_TF_RST BIT(31)
  48. #define SUN6I_FIFO_STA_REG 0x1c
  49. #define SUN6I_FIFO_STA_RF_CNT_MASK 0x7f
  50. #define SUN6I_FIFO_STA_RF_CNT_BITS 0
  51. #define SUN6I_FIFO_STA_TF_CNT_MASK 0x7f
  52. #define SUN6I_FIFO_STA_TF_CNT_BITS 16
  53. #define SUN6I_CLK_CTL_REG 0x24
  54. #define SUN6I_CLK_CTL_CDR2_MASK 0xff
  55. #define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
  56. #define SUN6I_CLK_CTL_CDR1_MASK 0xf
  57. #define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
  58. #define SUN6I_CLK_CTL_DRS BIT(12)
  59. #define SUN6I_BURST_CNT_REG 0x30
  60. #define SUN6I_BURST_CNT(cnt) ((cnt) & 0xffffff)
  61. #define SUN6I_XMIT_CNT_REG 0x34
  62. #define SUN6I_XMIT_CNT(cnt) ((cnt) & 0xffffff)
  63. #define SUN6I_BURST_CTL_CNT_REG 0x38
  64. #define SUN6I_BURST_CTL_CNT_STC(cnt) ((cnt) & 0xffffff)
  65. #define SUN6I_TXDATA_REG 0x200
  66. #define SUN6I_RXDATA_REG 0x300
  67. struct sun6i_spi {
  68. struct spi_master *master;
  69. void __iomem *base_addr;
  70. struct clk *hclk;
  71. struct clk *mclk;
  72. struct reset_control *rstc;
  73. struct completion done;
  74. const u8 *tx_buf;
  75. u8 *rx_buf;
  76. int len;
  77. };
  78. static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
  79. {
  80. return readl(sspi->base_addr + reg);
  81. }
  82. static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
  83. {
  84. writel(value, sspi->base_addr + reg);
  85. }
  86. static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
  87. {
  88. u32 reg, cnt;
  89. u8 byte;
  90. /* See how much data is available */
  91. reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  92. reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
  93. cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
  94. if (len > cnt)
  95. len = cnt;
  96. while (len--) {
  97. byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
  98. if (sspi->rx_buf)
  99. *sspi->rx_buf++ = byte;
  100. }
  101. }
  102. static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
  103. {
  104. u8 byte;
  105. if (len > sspi->len)
  106. len = sspi->len;
  107. while (len--) {
  108. byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  109. writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
  110. sspi->len--;
  111. }
  112. }
  113. static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
  114. {
  115. struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  116. u32 reg;
  117. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  118. reg &= ~SUN6I_TFR_CTL_CS_MASK;
  119. reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
  120. if (enable)
  121. reg |= SUN6I_TFR_CTL_CS_LEVEL;
  122. else
  123. reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
  124. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  125. }
  126. static int sun6i_spi_transfer_one(struct spi_master *master,
  127. struct spi_device *spi,
  128. struct spi_transfer *tfr)
  129. {
  130. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  131. unsigned int mclk_rate, div, timeout;
  132. unsigned int tx_len = 0;
  133. int ret = 0;
  134. u32 reg;
  135. /* We don't support transfer larger than the FIFO */
  136. if (tfr->len > SUN6I_FIFO_DEPTH)
  137. return -EINVAL;
  138. reinit_completion(&sspi->done);
  139. sspi->tx_buf = tfr->tx_buf;
  140. sspi->rx_buf = tfr->rx_buf;
  141. sspi->len = tfr->len;
  142. /* Clear pending interrupts */
  143. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
  144. /* Reset FIFO */
  145. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  146. SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
  147. /*
  148. * Setup the transfer control register: Chip Select,
  149. * polarities, etc.
  150. */
  151. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  152. if (spi->mode & SPI_CPOL)
  153. reg |= SUN6I_TFR_CTL_CPOL;
  154. else
  155. reg &= ~SUN6I_TFR_CTL_CPOL;
  156. if (spi->mode & SPI_CPHA)
  157. reg |= SUN6I_TFR_CTL_CPHA;
  158. else
  159. reg &= ~SUN6I_TFR_CTL_CPHA;
  160. if (spi->mode & SPI_LSB_FIRST)
  161. reg |= SUN6I_TFR_CTL_FBS;
  162. else
  163. reg &= ~SUN6I_TFR_CTL_FBS;
  164. /*
  165. * If it's a TX only transfer, we don't want to fill the RX
  166. * FIFO with bogus data
  167. */
  168. if (sspi->rx_buf)
  169. reg &= ~SUN6I_TFR_CTL_DHB;
  170. else
  171. reg |= SUN6I_TFR_CTL_DHB;
  172. /* We want to control the chip select manually */
  173. reg |= SUN6I_TFR_CTL_CS_MANUAL;
  174. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  175. /* Ensure that we have a parent clock fast enough */
  176. mclk_rate = clk_get_rate(sspi->mclk);
  177. if (mclk_rate < (2 * spi->max_speed_hz)) {
  178. clk_set_rate(sspi->mclk, 2 * spi->max_speed_hz);
  179. mclk_rate = clk_get_rate(sspi->mclk);
  180. }
  181. /*
  182. * Setup clock divider.
  183. *
  184. * We have two choices there. Either we can use the clock
  185. * divide rate 1, which is calculated thanks to this formula:
  186. * SPI_CLK = MOD_CLK / (2 ^ cdr)
  187. * Or we can use CDR2, which is calculated with the formula:
  188. * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  189. * Wether we use the former or the latter is set through the
  190. * DRS bit.
  191. *
  192. * First try CDR2, and if we can't reach the expected
  193. * frequency, fall back to CDR1.
  194. */
  195. div = mclk_rate / (2 * spi->max_speed_hz);
  196. if (div <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
  197. if (div > 0)
  198. div--;
  199. reg = SUN6I_CLK_CTL_CDR2(div) | SUN6I_CLK_CTL_DRS;
  200. } else {
  201. div = ilog2(mclk_rate) - ilog2(spi->max_speed_hz);
  202. reg = SUN6I_CLK_CTL_CDR1(div);
  203. }
  204. sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
  205. /* Setup the transfer now... */
  206. if (sspi->tx_buf)
  207. tx_len = tfr->len;
  208. /* Setup the counters */
  209. sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
  210. sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
  211. sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
  212. SUN6I_BURST_CTL_CNT_STC(tx_len));
  213. /* Fill the TX FIFO */
  214. sun6i_spi_fill_fifo(sspi, SUN6I_FIFO_DEPTH);
  215. /* Enable the interrupts */
  216. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
  217. /* Start the transfer */
  218. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  219. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
  220. timeout = wait_for_completion_timeout(&sspi->done,
  221. msecs_to_jiffies(1000));
  222. if (!timeout) {
  223. ret = -ETIMEDOUT;
  224. goto out;
  225. }
  226. sun6i_spi_drain_fifo(sspi, SUN6I_FIFO_DEPTH);
  227. out:
  228. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
  229. return ret;
  230. }
  231. static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
  232. {
  233. struct sun6i_spi *sspi = dev_id;
  234. u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
  235. /* Transfer complete */
  236. if (status & SUN6I_INT_CTL_TC) {
  237. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
  238. complete(&sspi->done);
  239. return IRQ_HANDLED;
  240. }
  241. return IRQ_NONE;
  242. }
  243. static int sun6i_spi_runtime_resume(struct device *dev)
  244. {
  245. struct spi_master *master = dev_get_drvdata(dev);
  246. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  247. int ret;
  248. ret = clk_prepare_enable(sspi->hclk);
  249. if (ret) {
  250. dev_err(dev, "Couldn't enable AHB clock\n");
  251. goto out;
  252. }
  253. ret = clk_prepare_enable(sspi->mclk);
  254. if (ret) {
  255. dev_err(dev, "Couldn't enable module clock\n");
  256. goto err;
  257. }
  258. ret = reset_control_deassert(sspi->rstc);
  259. if (ret) {
  260. dev_err(dev, "Couldn't deassert the device from reset\n");
  261. goto err2;
  262. }
  263. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
  264. SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
  265. return 0;
  266. err2:
  267. clk_disable_unprepare(sspi->mclk);
  268. err:
  269. clk_disable_unprepare(sspi->hclk);
  270. out:
  271. return ret;
  272. }
  273. static int sun6i_spi_runtime_suspend(struct device *dev)
  274. {
  275. struct spi_master *master = dev_get_drvdata(dev);
  276. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  277. reset_control_assert(sspi->rstc);
  278. clk_disable_unprepare(sspi->mclk);
  279. clk_disable_unprepare(sspi->hclk);
  280. return 0;
  281. }
  282. static int sun6i_spi_probe(struct platform_device *pdev)
  283. {
  284. struct spi_master *master;
  285. struct sun6i_spi *sspi;
  286. struct resource *res;
  287. int ret = 0, irq;
  288. master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
  289. if (!master) {
  290. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  291. return -ENOMEM;
  292. }
  293. platform_set_drvdata(pdev, master);
  294. sspi = spi_master_get_devdata(master);
  295. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  296. sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  297. if (IS_ERR(sspi->base_addr)) {
  298. ret = PTR_ERR(sspi->base_addr);
  299. goto err_free_master;
  300. }
  301. irq = platform_get_irq(pdev, 0);
  302. if (irq < 0) {
  303. dev_err(&pdev->dev, "No spi IRQ specified\n");
  304. ret = -ENXIO;
  305. goto err_free_master;
  306. }
  307. ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
  308. 0, "sun6i-spi", sspi);
  309. if (ret) {
  310. dev_err(&pdev->dev, "Cannot request IRQ\n");
  311. goto err_free_master;
  312. }
  313. sspi->master = master;
  314. master->set_cs = sun6i_spi_set_cs;
  315. master->transfer_one = sun6i_spi_transfer_one;
  316. master->num_chipselect = 4;
  317. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  318. master->bits_per_word_mask = SPI_BPW_MASK(8);
  319. master->dev.of_node = pdev->dev.of_node;
  320. master->auto_runtime_pm = true;
  321. sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  322. if (IS_ERR(sspi->hclk)) {
  323. dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  324. ret = PTR_ERR(sspi->hclk);
  325. goto err_free_master;
  326. }
  327. sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  328. if (IS_ERR(sspi->mclk)) {
  329. dev_err(&pdev->dev, "Unable to acquire module clock\n");
  330. ret = PTR_ERR(sspi->mclk);
  331. goto err_free_master;
  332. }
  333. init_completion(&sspi->done);
  334. sspi->rstc = devm_reset_control_get(&pdev->dev, NULL);
  335. if (IS_ERR(sspi->rstc)) {
  336. dev_err(&pdev->dev, "Couldn't get reset controller\n");
  337. ret = PTR_ERR(sspi->rstc);
  338. goto err_free_master;
  339. }
  340. /*
  341. * This wake-up/shutdown pattern is to be able to have the
  342. * device woken up, even if runtime_pm is disabled
  343. */
  344. ret = sun6i_spi_runtime_resume(&pdev->dev);
  345. if (ret) {
  346. dev_err(&pdev->dev, "Couldn't resume the device\n");
  347. goto err_free_master;
  348. }
  349. pm_runtime_set_active(&pdev->dev);
  350. pm_runtime_enable(&pdev->dev);
  351. pm_runtime_idle(&pdev->dev);
  352. ret = devm_spi_register_master(&pdev->dev, master);
  353. if (ret) {
  354. dev_err(&pdev->dev, "cannot register SPI master\n");
  355. goto err_pm_disable;
  356. }
  357. return 0;
  358. err_pm_disable:
  359. pm_runtime_disable(&pdev->dev);
  360. sun6i_spi_runtime_suspend(&pdev->dev);
  361. err_free_master:
  362. spi_master_put(master);
  363. return ret;
  364. }
  365. static int sun6i_spi_remove(struct platform_device *pdev)
  366. {
  367. pm_runtime_disable(&pdev->dev);
  368. return 0;
  369. }
  370. static const struct of_device_id sun6i_spi_match[] = {
  371. { .compatible = "allwinner,sun6i-a31-spi", },
  372. {}
  373. };
  374. MODULE_DEVICE_TABLE(of, sun6i_spi_match);
  375. static const struct dev_pm_ops sun6i_spi_pm_ops = {
  376. .runtime_resume = sun6i_spi_runtime_resume,
  377. .runtime_suspend = sun6i_spi_runtime_suspend,
  378. };
  379. static struct platform_driver sun6i_spi_driver = {
  380. .probe = sun6i_spi_probe,
  381. .remove = sun6i_spi_remove,
  382. .driver = {
  383. .name = "sun6i-spi",
  384. .owner = THIS_MODULE,
  385. .of_match_table = sun6i_spi_match,
  386. .pm = &sun6i_spi_pm_ops,
  387. },
  388. };
  389. module_platform_driver(sun6i_spi_driver);
  390. MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  391. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  392. MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
  393. MODULE_LICENSE("GPL");