spi-sun6i.c 12 KB

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