spi-sun4i.c 14 KB

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