spi-sun6i.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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/of_device.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_runtime.h>
  22. #include <linux/reset.h>
  23. #include <linux/spi/spi.h>
  24. #define SUN6I_FIFO_DEPTH 128
  25. #define SUN8I_FIFO_DEPTH 64
  26. #define SUN6I_GBL_CTL_REG 0x04
  27. #define SUN6I_GBL_CTL_BUS_ENABLE BIT(0)
  28. #define SUN6I_GBL_CTL_MASTER BIT(1)
  29. #define SUN6I_GBL_CTL_TP BIT(7)
  30. #define SUN6I_GBL_CTL_RST BIT(31)
  31. #define SUN6I_TFR_CTL_REG 0x08
  32. #define SUN6I_TFR_CTL_CPHA BIT(0)
  33. #define SUN6I_TFR_CTL_CPOL BIT(1)
  34. #define SUN6I_TFR_CTL_SPOL BIT(2)
  35. #define SUN6I_TFR_CTL_CS_MASK 0x30
  36. #define SUN6I_TFR_CTL_CS(cs) (((cs) << 4) & SUN6I_TFR_CTL_CS_MASK)
  37. #define SUN6I_TFR_CTL_CS_MANUAL BIT(6)
  38. #define SUN6I_TFR_CTL_CS_LEVEL BIT(7)
  39. #define SUN6I_TFR_CTL_DHB BIT(8)
  40. #define SUN6I_TFR_CTL_FBS BIT(12)
  41. #define SUN6I_TFR_CTL_XCH BIT(31)
  42. #define SUN6I_INT_CTL_REG 0x10
  43. #define SUN6I_INT_CTL_RF_OVF BIT(8)
  44. #define SUN6I_INT_CTL_TC BIT(12)
  45. #define SUN6I_INT_STA_REG 0x14
  46. #define SUN6I_FIFO_CTL_REG 0x18
  47. #define SUN6I_FIFO_CTL_RF_RST BIT(15)
  48. #define SUN6I_FIFO_CTL_TF_RST BIT(31)
  49. #define SUN6I_FIFO_STA_REG 0x1c
  50. #define SUN6I_FIFO_STA_RF_CNT_MASK 0x7f
  51. #define SUN6I_FIFO_STA_RF_CNT_BITS 0
  52. #define SUN6I_FIFO_STA_TF_CNT_MASK 0x7f
  53. #define SUN6I_FIFO_STA_TF_CNT_BITS 16
  54. #define SUN6I_CLK_CTL_REG 0x24
  55. #define SUN6I_CLK_CTL_CDR2_MASK 0xff
  56. #define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
  57. #define SUN6I_CLK_CTL_CDR1_MASK 0xf
  58. #define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
  59. #define SUN6I_CLK_CTL_DRS BIT(12)
  60. #define SUN6I_BURST_CNT_REG 0x30
  61. #define SUN6I_BURST_CNT(cnt) ((cnt) & 0xffffff)
  62. #define SUN6I_XMIT_CNT_REG 0x34
  63. #define SUN6I_XMIT_CNT(cnt) ((cnt) & 0xffffff)
  64. #define SUN6I_BURST_CTL_CNT_REG 0x38
  65. #define SUN6I_BURST_CTL_CNT_STC(cnt) ((cnt) & 0xffffff)
  66. #define SUN6I_TXDATA_REG 0x200
  67. #define SUN6I_RXDATA_REG 0x300
  68. struct sun6i_spi {
  69. struct spi_master *master;
  70. void __iomem *base_addr;
  71. struct clk *hclk;
  72. struct clk *mclk;
  73. struct reset_control *rstc;
  74. struct completion done;
  75. const u8 *tx_buf;
  76. u8 *rx_buf;
  77. int len;
  78. unsigned long fifo_depth;
  79. };
  80. static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
  81. {
  82. return readl(sspi->base_addr + reg);
  83. }
  84. static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
  85. {
  86. writel(value, sspi->base_addr + reg);
  87. }
  88. static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi, int len)
  89. {
  90. u32 reg, cnt;
  91. u8 byte;
  92. /* See how much data is available */
  93. reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  94. reg &= SUN6I_FIFO_STA_RF_CNT_MASK;
  95. cnt = reg >> SUN6I_FIFO_STA_RF_CNT_BITS;
  96. if (len > cnt)
  97. len = cnt;
  98. while (len--) {
  99. byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
  100. if (sspi->rx_buf)
  101. *sspi->rx_buf++ = byte;
  102. }
  103. }
  104. static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi, int len)
  105. {
  106. u8 byte;
  107. if (len > sspi->len)
  108. len = sspi->len;
  109. while (len--) {
  110. byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  111. writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
  112. sspi->len--;
  113. }
  114. }
  115. static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
  116. {
  117. struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  118. u32 reg;
  119. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  120. reg &= ~SUN6I_TFR_CTL_CS_MASK;
  121. reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
  122. if (enable)
  123. reg |= SUN6I_TFR_CTL_CS_LEVEL;
  124. else
  125. reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
  126. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  127. }
  128. static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
  129. {
  130. struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  131. return sspi->fifo_depth - 1;
  132. }
  133. static int sun6i_spi_transfer_one(struct spi_master *master,
  134. struct spi_device *spi,
  135. struct spi_transfer *tfr)
  136. {
  137. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  138. unsigned int mclk_rate, div, timeout;
  139. unsigned int start, end, tx_time;
  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 > sspi->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. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
  152. /* Reset FIFO */
  153. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  154. SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
  155. /*
  156. * Setup the transfer control register: Chip Select,
  157. * polarities, etc.
  158. */
  159. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  160. if (spi->mode & SPI_CPOL)
  161. reg |= SUN6I_TFR_CTL_CPOL;
  162. else
  163. reg &= ~SUN6I_TFR_CTL_CPOL;
  164. if (spi->mode & SPI_CPHA)
  165. reg |= SUN6I_TFR_CTL_CPHA;
  166. else
  167. reg &= ~SUN6I_TFR_CTL_CPHA;
  168. if (spi->mode & SPI_LSB_FIRST)
  169. reg |= SUN6I_TFR_CTL_FBS;
  170. else
  171. reg &= ~SUN6I_TFR_CTL_FBS;
  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 &= ~SUN6I_TFR_CTL_DHB;
  178. else
  179. reg |= SUN6I_TFR_CTL_DHB;
  180. /* We want to control the chip select manually */
  181. reg |= SUN6I_TFR_CTL_CS_MANUAL;
  182. sun6i_spi_write(sspi, SUN6I_TFR_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 * tfr->speed_hz)) {
  186. clk_set_rate(sspi->mclk, 2 * tfr->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)
  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 * tfr->speed_hz);
  204. if (div <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
  205. if (div > 0)
  206. div--;
  207. reg = SUN6I_CLK_CTL_CDR2(div) | SUN6I_CLK_CTL_DRS;
  208. } else {
  209. div = ilog2(mclk_rate) - ilog2(tfr->speed_hz);
  210. reg = SUN6I_CLK_CTL_CDR1(div);
  211. }
  212. sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
  213. /* Setup the transfer now... */
  214. if (sspi->tx_buf)
  215. tx_len = tfr->len;
  216. /* Setup the counters */
  217. sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, SUN6I_BURST_CNT(tfr->len));
  218. sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, SUN6I_XMIT_CNT(tx_len));
  219. sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG,
  220. SUN6I_BURST_CTL_CNT_STC(tx_len));
  221. /* Fill the TX FIFO */
  222. sun6i_spi_fill_fifo(sspi, sspi->fifo_depth);
  223. /* Enable the interrupts */
  224. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, SUN6I_INT_CTL_TC);
  225. /* Start the transfer */
  226. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  227. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
  228. tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
  229. start = jiffies;
  230. timeout = wait_for_completion_timeout(&sspi->done,
  231. msecs_to_jiffies(tx_time));
  232. end = jiffies;
  233. if (!timeout) {
  234. dev_warn(&master->dev,
  235. "%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
  236. dev_name(&spi->dev), tfr->len, tfr->speed_hz,
  237. jiffies_to_msecs(end - start), tx_time);
  238. ret = -ETIMEDOUT;
  239. goto out;
  240. }
  241. sun6i_spi_drain_fifo(sspi, sspi->fifo_depth);
  242. out:
  243. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
  244. return ret;
  245. }
  246. static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
  247. {
  248. struct sun6i_spi *sspi = dev_id;
  249. u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
  250. /* Transfer complete */
  251. if (status & SUN6I_INT_CTL_TC) {
  252. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
  253. complete(&sspi->done);
  254. return IRQ_HANDLED;
  255. }
  256. return IRQ_NONE;
  257. }
  258. static int sun6i_spi_runtime_resume(struct device *dev)
  259. {
  260. struct spi_master *master = dev_get_drvdata(dev);
  261. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  262. int ret;
  263. ret = clk_prepare_enable(sspi->hclk);
  264. if (ret) {
  265. dev_err(dev, "Couldn't enable AHB clock\n");
  266. goto out;
  267. }
  268. ret = clk_prepare_enable(sspi->mclk);
  269. if (ret) {
  270. dev_err(dev, "Couldn't enable module clock\n");
  271. goto err;
  272. }
  273. ret = reset_control_deassert(sspi->rstc);
  274. if (ret) {
  275. dev_err(dev, "Couldn't deassert the device from reset\n");
  276. goto err2;
  277. }
  278. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
  279. SUN6I_GBL_CTL_BUS_ENABLE | SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
  280. return 0;
  281. err2:
  282. clk_disable_unprepare(sspi->mclk);
  283. err:
  284. clk_disable_unprepare(sspi->hclk);
  285. out:
  286. return ret;
  287. }
  288. static int sun6i_spi_runtime_suspend(struct device *dev)
  289. {
  290. struct spi_master *master = dev_get_drvdata(dev);
  291. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  292. reset_control_assert(sspi->rstc);
  293. clk_disable_unprepare(sspi->mclk);
  294. clk_disable_unprepare(sspi->hclk);
  295. return 0;
  296. }
  297. static int sun6i_spi_probe(struct platform_device *pdev)
  298. {
  299. struct spi_master *master;
  300. struct sun6i_spi *sspi;
  301. struct resource *res;
  302. int ret = 0, irq;
  303. master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
  304. if (!master) {
  305. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  306. return -ENOMEM;
  307. }
  308. platform_set_drvdata(pdev, master);
  309. sspi = spi_master_get_devdata(master);
  310. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  311. sspi->base_addr = devm_ioremap_resource(&pdev->dev, res);
  312. if (IS_ERR(sspi->base_addr)) {
  313. ret = PTR_ERR(sspi->base_addr);
  314. goto err_free_master;
  315. }
  316. irq = platform_get_irq(pdev, 0);
  317. if (irq < 0) {
  318. dev_err(&pdev->dev, "No spi IRQ specified\n");
  319. ret = -ENXIO;
  320. goto err_free_master;
  321. }
  322. ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
  323. 0, "sun6i-spi", sspi);
  324. if (ret) {
  325. dev_err(&pdev->dev, "Cannot request IRQ\n");
  326. goto err_free_master;
  327. }
  328. sspi->master = master;
  329. sspi->fifo_depth = (unsigned long)of_device_get_match_data(&pdev->dev);
  330. master->max_speed_hz = 100 * 1000 * 1000;
  331. master->min_speed_hz = 3 * 1000;
  332. master->set_cs = sun6i_spi_set_cs;
  333. master->transfer_one = sun6i_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 = sun6i_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. sspi->rstc = devm_reset_control_get(&pdev->dev, NULL);
  354. if (IS_ERR(sspi->rstc)) {
  355. dev_err(&pdev->dev, "Couldn't get reset controller\n");
  356. ret = PTR_ERR(sspi->rstc);
  357. goto err_free_master;
  358. }
  359. /*
  360. * This wake-up/shutdown pattern is to be able to have the
  361. * device woken up, even if runtime_pm is disabled
  362. */
  363. ret = sun6i_spi_runtime_resume(&pdev->dev);
  364. if (ret) {
  365. dev_err(&pdev->dev, "Couldn't resume the device\n");
  366. goto err_free_master;
  367. }
  368. pm_runtime_set_active(&pdev->dev);
  369. pm_runtime_enable(&pdev->dev);
  370. pm_runtime_idle(&pdev->dev);
  371. ret = devm_spi_register_master(&pdev->dev, master);
  372. if (ret) {
  373. dev_err(&pdev->dev, "cannot register SPI master\n");
  374. goto err_pm_disable;
  375. }
  376. return 0;
  377. err_pm_disable:
  378. pm_runtime_disable(&pdev->dev);
  379. sun6i_spi_runtime_suspend(&pdev->dev);
  380. err_free_master:
  381. spi_master_put(master);
  382. return ret;
  383. }
  384. static int sun6i_spi_remove(struct platform_device *pdev)
  385. {
  386. pm_runtime_disable(&pdev->dev);
  387. return 0;
  388. }
  389. static const struct of_device_id sun6i_spi_match[] = {
  390. { .compatible = "allwinner,sun6i-a31-spi", .data = (void *)SUN6I_FIFO_DEPTH },
  391. { .compatible = "allwinner,sun8i-h3-spi", .data = (void *)SUN8I_FIFO_DEPTH },
  392. {}
  393. };
  394. MODULE_DEVICE_TABLE(of, sun6i_spi_match);
  395. static const struct dev_pm_ops sun6i_spi_pm_ops = {
  396. .runtime_resume = sun6i_spi_runtime_resume,
  397. .runtime_suspend = sun6i_spi_runtime_suspend,
  398. };
  399. static struct platform_driver sun6i_spi_driver = {
  400. .probe = sun6i_spi_probe,
  401. .remove = sun6i_spi_remove,
  402. .driver = {
  403. .name = "sun6i-spi",
  404. .of_match_table = sun6i_spi_match,
  405. .pm = &sun6i_spi_pm_ops,
  406. },
  407. };
  408. module_platform_driver(sun6i_spi_driver);
  409. MODULE_AUTHOR("Pan Nan <pannan@allwinnertech.com>");
  410. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@free-electrons.com>");
  411. MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
  412. MODULE_LICENSE("GPL");