spi-sun6i.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012 - 2014 Allwinner Tech
  4. * Pan Nan <[email protected]>
  5. *
  6. * Copyright (C) 2014 Maxime Ripard
  7. * Maxime Ripard <[email protected]>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/delay.h>
  12. #include <linux/device.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/io.h>
  15. #include <linux/module.h>
  16. #include <linux/of_device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/pm_runtime.h>
  19. #include <linux/reset.h>
  20. #include <linux/dmaengine.h>
  21. #include <linux/spi/spi.h>
  22. #define SUN6I_AUTOSUSPEND_TIMEOUT 2000
  23. #define SUN6I_FIFO_DEPTH 128
  24. #define SUN8I_FIFO_DEPTH 64
  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_RDY BIT(0)
  43. #define SUN6I_INT_CTL_TF_ERQ BIT(4)
  44. #define SUN6I_INT_CTL_RF_OVF BIT(8)
  45. #define SUN6I_INT_CTL_TC BIT(12)
  46. #define SUN6I_INT_STA_REG 0x14
  47. #define SUN6I_FIFO_CTL_REG 0x18
  48. #define SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_MASK 0xff
  49. #define SUN6I_FIFO_CTL_RF_DRQ_EN BIT(8)
  50. #define SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_BITS 0
  51. #define SUN6I_FIFO_CTL_RF_RST BIT(15)
  52. #define SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_MASK 0xff
  53. #define SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_BITS 16
  54. #define SUN6I_FIFO_CTL_TF_DRQ_EN BIT(24)
  55. #define SUN6I_FIFO_CTL_TF_RST BIT(31)
  56. #define SUN6I_FIFO_STA_REG 0x1c
  57. #define SUN6I_FIFO_STA_RF_CNT_MASK GENMASK(7, 0)
  58. #define SUN6I_FIFO_STA_TF_CNT_MASK GENMASK(23, 16)
  59. #define SUN6I_CLK_CTL_REG 0x24
  60. #define SUN6I_CLK_CTL_CDR2_MASK 0xff
  61. #define SUN6I_CLK_CTL_CDR2(div) (((div) & SUN6I_CLK_CTL_CDR2_MASK) << 0)
  62. #define SUN6I_CLK_CTL_CDR1_MASK 0xf
  63. #define SUN6I_CLK_CTL_CDR1(div) (((div) & SUN6I_CLK_CTL_CDR1_MASK) << 8)
  64. #define SUN6I_CLK_CTL_DRS BIT(12)
  65. #define SUN6I_MAX_XFER_SIZE 0xffffff
  66. #define SUN6I_BURST_CNT_REG 0x30
  67. #define SUN6I_XMIT_CNT_REG 0x34
  68. #define SUN6I_BURST_CTL_CNT_REG 0x38
  69. #define SUN6I_TXDATA_REG 0x200
  70. #define SUN6I_RXDATA_REG 0x300
  71. struct sun6i_spi {
  72. struct spi_master *master;
  73. void __iomem *base_addr;
  74. dma_addr_t dma_addr_rx;
  75. dma_addr_t dma_addr_tx;
  76. struct clk *hclk;
  77. struct clk *mclk;
  78. struct reset_control *rstc;
  79. struct completion done;
  80. struct completion dma_rx_done;
  81. const u8 *tx_buf;
  82. u8 *rx_buf;
  83. int len;
  84. unsigned long fifo_depth;
  85. };
  86. static inline u32 sun6i_spi_read(struct sun6i_spi *sspi, u32 reg)
  87. {
  88. return readl(sspi->base_addr + reg);
  89. }
  90. static inline void sun6i_spi_write(struct sun6i_spi *sspi, u32 reg, u32 value)
  91. {
  92. writel(value, sspi->base_addr + reg);
  93. }
  94. static inline u32 sun6i_spi_get_rx_fifo_count(struct sun6i_spi *sspi)
  95. {
  96. u32 reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  97. return FIELD_GET(SUN6I_FIFO_STA_RF_CNT_MASK, reg);
  98. }
  99. static inline u32 sun6i_spi_get_tx_fifo_count(struct sun6i_spi *sspi)
  100. {
  101. u32 reg = sun6i_spi_read(sspi, SUN6I_FIFO_STA_REG);
  102. return FIELD_GET(SUN6I_FIFO_STA_TF_CNT_MASK, reg);
  103. }
  104. static inline void sun6i_spi_disable_interrupt(struct sun6i_spi *sspi, u32 mask)
  105. {
  106. u32 reg = sun6i_spi_read(sspi, SUN6I_INT_CTL_REG);
  107. reg &= ~mask;
  108. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, reg);
  109. }
  110. static inline void sun6i_spi_drain_fifo(struct sun6i_spi *sspi)
  111. {
  112. u32 len;
  113. u8 byte;
  114. /* See how much data is available */
  115. len = sun6i_spi_get_rx_fifo_count(sspi);
  116. while (len--) {
  117. byte = readb(sspi->base_addr + SUN6I_RXDATA_REG);
  118. if (sspi->rx_buf)
  119. *sspi->rx_buf++ = byte;
  120. }
  121. }
  122. static inline void sun6i_spi_fill_fifo(struct sun6i_spi *sspi)
  123. {
  124. u32 cnt;
  125. int len;
  126. u8 byte;
  127. /* See how much data we can fit */
  128. cnt = sspi->fifo_depth - sun6i_spi_get_tx_fifo_count(sspi);
  129. len = min((int)cnt, sspi->len);
  130. while (len--) {
  131. byte = sspi->tx_buf ? *sspi->tx_buf++ : 0;
  132. writeb(byte, sspi->base_addr + SUN6I_TXDATA_REG);
  133. sspi->len--;
  134. }
  135. }
  136. static void sun6i_spi_set_cs(struct spi_device *spi, bool enable)
  137. {
  138. struct sun6i_spi *sspi = spi_master_get_devdata(spi->master);
  139. u32 reg;
  140. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  141. reg &= ~SUN6I_TFR_CTL_CS_MASK;
  142. reg |= SUN6I_TFR_CTL_CS(spi->chip_select);
  143. if (enable)
  144. reg |= SUN6I_TFR_CTL_CS_LEVEL;
  145. else
  146. reg &= ~SUN6I_TFR_CTL_CS_LEVEL;
  147. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  148. }
  149. static size_t sun6i_spi_max_transfer_size(struct spi_device *spi)
  150. {
  151. return SUN6I_MAX_XFER_SIZE - 1;
  152. }
  153. static void sun6i_spi_dma_rx_cb(void *param)
  154. {
  155. struct sun6i_spi *sspi = param;
  156. complete(&sspi->dma_rx_done);
  157. }
  158. static int sun6i_spi_prepare_dma(struct sun6i_spi *sspi,
  159. struct spi_transfer *tfr)
  160. {
  161. struct dma_async_tx_descriptor *rxdesc, *txdesc;
  162. struct spi_master *master = sspi->master;
  163. rxdesc = NULL;
  164. if (tfr->rx_buf) {
  165. struct dma_slave_config rxconf = {
  166. .direction = DMA_DEV_TO_MEM,
  167. .src_addr = sspi->dma_addr_rx,
  168. .src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE,
  169. .src_maxburst = 8,
  170. };
  171. dmaengine_slave_config(master->dma_rx, &rxconf);
  172. rxdesc = dmaengine_prep_slave_sg(master->dma_rx,
  173. tfr->rx_sg.sgl,
  174. tfr->rx_sg.nents,
  175. DMA_DEV_TO_MEM,
  176. DMA_PREP_INTERRUPT);
  177. if (!rxdesc)
  178. return -EINVAL;
  179. rxdesc->callback_param = sspi;
  180. rxdesc->callback = sun6i_spi_dma_rx_cb;
  181. }
  182. txdesc = NULL;
  183. if (tfr->tx_buf) {
  184. struct dma_slave_config txconf = {
  185. .direction = DMA_MEM_TO_DEV,
  186. .dst_addr = sspi->dma_addr_tx,
  187. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  188. .dst_maxburst = 8,
  189. };
  190. dmaengine_slave_config(master->dma_tx, &txconf);
  191. txdesc = dmaengine_prep_slave_sg(master->dma_tx,
  192. tfr->tx_sg.sgl,
  193. tfr->tx_sg.nents,
  194. DMA_MEM_TO_DEV,
  195. DMA_PREP_INTERRUPT);
  196. if (!txdesc) {
  197. if (rxdesc)
  198. dmaengine_terminate_sync(master->dma_rx);
  199. return -EINVAL;
  200. }
  201. }
  202. if (tfr->rx_buf) {
  203. dmaengine_submit(rxdesc);
  204. dma_async_issue_pending(master->dma_rx);
  205. }
  206. if (tfr->tx_buf) {
  207. dmaengine_submit(txdesc);
  208. dma_async_issue_pending(master->dma_tx);
  209. }
  210. return 0;
  211. }
  212. static int sun6i_spi_transfer_one(struct spi_master *master,
  213. struct spi_device *spi,
  214. struct spi_transfer *tfr)
  215. {
  216. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  217. unsigned int mclk_rate, div, div_cdr1, div_cdr2, timeout;
  218. unsigned int start, end, tx_time;
  219. unsigned int trig_level;
  220. unsigned int tx_len = 0, rx_len = 0;
  221. bool use_dma;
  222. int ret = 0;
  223. u32 reg;
  224. if (tfr->len > SUN6I_MAX_XFER_SIZE)
  225. return -EINVAL;
  226. reinit_completion(&sspi->done);
  227. reinit_completion(&sspi->dma_rx_done);
  228. sspi->tx_buf = tfr->tx_buf;
  229. sspi->rx_buf = tfr->rx_buf;
  230. sspi->len = tfr->len;
  231. use_dma = master->can_dma ? master->can_dma(master, spi, tfr) : false;
  232. /* Clear pending interrupts */
  233. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, ~0);
  234. /* Reset FIFO */
  235. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG,
  236. SUN6I_FIFO_CTL_RF_RST | SUN6I_FIFO_CTL_TF_RST);
  237. reg = 0;
  238. if (!use_dma) {
  239. /*
  240. * Setup FIFO interrupt trigger level
  241. * Here we choose 3/4 of the full fifo depth, as it's
  242. * the hardcoded value used in old generation of Allwinner
  243. * SPI controller. (See spi-sun4i.c)
  244. */
  245. trig_level = sspi->fifo_depth / 4 * 3;
  246. } else {
  247. /*
  248. * Setup FIFO DMA request trigger level
  249. * We choose 1/2 of the full fifo depth, that value will
  250. * be used as DMA burst length.
  251. */
  252. trig_level = sspi->fifo_depth / 2;
  253. if (tfr->tx_buf)
  254. reg |= SUN6I_FIFO_CTL_TF_DRQ_EN;
  255. if (tfr->rx_buf)
  256. reg |= SUN6I_FIFO_CTL_RF_DRQ_EN;
  257. }
  258. reg |= (trig_level << SUN6I_FIFO_CTL_RF_RDY_TRIG_LEVEL_BITS) |
  259. (trig_level << SUN6I_FIFO_CTL_TF_ERQ_TRIG_LEVEL_BITS);
  260. sun6i_spi_write(sspi, SUN6I_FIFO_CTL_REG, reg);
  261. /*
  262. * Setup the transfer control register: Chip Select,
  263. * polarities, etc.
  264. */
  265. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  266. if (spi->mode & SPI_CPOL)
  267. reg |= SUN6I_TFR_CTL_CPOL;
  268. else
  269. reg &= ~SUN6I_TFR_CTL_CPOL;
  270. if (spi->mode & SPI_CPHA)
  271. reg |= SUN6I_TFR_CTL_CPHA;
  272. else
  273. reg &= ~SUN6I_TFR_CTL_CPHA;
  274. if (spi->mode & SPI_LSB_FIRST)
  275. reg |= SUN6I_TFR_CTL_FBS;
  276. else
  277. reg &= ~SUN6I_TFR_CTL_FBS;
  278. /*
  279. * If it's a TX only transfer, we don't want to fill the RX
  280. * FIFO with bogus data
  281. */
  282. if (sspi->rx_buf) {
  283. reg &= ~SUN6I_TFR_CTL_DHB;
  284. rx_len = tfr->len;
  285. } else {
  286. reg |= SUN6I_TFR_CTL_DHB;
  287. }
  288. /* We want to control the chip select manually */
  289. reg |= SUN6I_TFR_CTL_CS_MANUAL;
  290. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg);
  291. /* Ensure that we have a parent clock fast enough */
  292. mclk_rate = clk_get_rate(sspi->mclk);
  293. if (mclk_rate < (2 * tfr->speed_hz)) {
  294. clk_set_rate(sspi->mclk, 2 * tfr->speed_hz);
  295. mclk_rate = clk_get_rate(sspi->mclk);
  296. }
  297. /*
  298. * Setup clock divider.
  299. *
  300. * We have two choices there. Either we can use the clock
  301. * divide rate 1, which is calculated thanks to this formula:
  302. * SPI_CLK = MOD_CLK / (2 ^ cdr)
  303. * Or we can use CDR2, which is calculated with the formula:
  304. * SPI_CLK = MOD_CLK / (2 * (cdr + 1))
  305. * Wether we use the former or the latter is set through the
  306. * DRS bit.
  307. *
  308. * First try CDR2, and if we can't reach the expected
  309. * frequency, fall back to CDR1.
  310. */
  311. div_cdr1 = DIV_ROUND_UP(mclk_rate, tfr->speed_hz);
  312. div_cdr2 = DIV_ROUND_UP(div_cdr1, 2);
  313. if (div_cdr2 <= (SUN6I_CLK_CTL_CDR2_MASK + 1)) {
  314. reg = SUN6I_CLK_CTL_CDR2(div_cdr2 - 1) | SUN6I_CLK_CTL_DRS;
  315. tfr->effective_speed_hz = mclk_rate / (2 * div_cdr2);
  316. } else {
  317. div = min(SUN6I_CLK_CTL_CDR1_MASK, order_base_2(div_cdr1));
  318. reg = SUN6I_CLK_CTL_CDR1(div);
  319. tfr->effective_speed_hz = mclk_rate / (1 << div);
  320. }
  321. sun6i_spi_write(sspi, SUN6I_CLK_CTL_REG, reg);
  322. /* Finally enable the bus - doing so before might raise SCK to HIGH */
  323. reg = sun6i_spi_read(sspi, SUN6I_GBL_CTL_REG);
  324. reg |= SUN6I_GBL_CTL_BUS_ENABLE;
  325. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG, reg);
  326. /* Setup the transfer now... */
  327. if (sspi->tx_buf)
  328. tx_len = tfr->len;
  329. /* Setup the counters */
  330. sun6i_spi_write(sspi, SUN6I_BURST_CNT_REG, tfr->len);
  331. sun6i_spi_write(sspi, SUN6I_XMIT_CNT_REG, tx_len);
  332. sun6i_spi_write(sspi, SUN6I_BURST_CTL_CNT_REG, tx_len);
  333. if (!use_dma) {
  334. /* Fill the TX FIFO */
  335. sun6i_spi_fill_fifo(sspi);
  336. } else {
  337. ret = sun6i_spi_prepare_dma(sspi, tfr);
  338. if (ret) {
  339. dev_warn(&master->dev,
  340. "%s: prepare DMA failed, ret=%d",
  341. dev_name(&spi->dev), ret);
  342. return ret;
  343. }
  344. }
  345. /* Enable the interrupts */
  346. reg = SUN6I_INT_CTL_TC;
  347. if (!use_dma) {
  348. if (rx_len > sspi->fifo_depth)
  349. reg |= SUN6I_INT_CTL_RF_RDY;
  350. if (tx_len > sspi->fifo_depth)
  351. reg |= SUN6I_INT_CTL_TF_ERQ;
  352. }
  353. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, reg);
  354. /* Start the transfer */
  355. reg = sun6i_spi_read(sspi, SUN6I_TFR_CTL_REG);
  356. sun6i_spi_write(sspi, SUN6I_TFR_CTL_REG, reg | SUN6I_TFR_CTL_XCH);
  357. tx_time = max(tfr->len * 8 * 2 / (tfr->speed_hz / 1000), 100U);
  358. start = jiffies;
  359. timeout = wait_for_completion_timeout(&sspi->done,
  360. msecs_to_jiffies(tx_time));
  361. if (!use_dma) {
  362. sun6i_spi_drain_fifo(sspi);
  363. } else {
  364. if (timeout && rx_len) {
  365. /*
  366. * Even though RX on the peripheral side has finished
  367. * RX DMA might still be in flight
  368. */
  369. timeout = wait_for_completion_timeout(&sspi->dma_rx_done,
  370. timeout);
  371. if (!timeout)
  372. dev_warn(&master->dev, "RX DMA timeout\n");
  373. }
  374. }
  375. end = jiffies;
  376. if (!timeout) {
  377. dev_warn(&master->dev,
  378. "%s: timeout transferring %u bytes@%iHz for %i(%i)ms",
  379. dev_name(&spi->dev), tfr->len, tfr->speed_hz,
  380. jiffies_to_msecs(end - start), tx_time);
  381. ret = -ETIMEDOUT;
  382. }
  383. sun6i_spi_write(sspi, SUN6I_INT_CTL_REG, 0);
  384. if (ret && use_dma) {
  385. dmaengine_terminate_sync(master->dma_rx);
  386. dmaengine_terminate_sync(master->dma_tx);
  387. }
  388. return ret;
  389. }
  390. static irqreturn_t sun6i_spi_handler(int irq, void *dev_id)
  391. {
  392. struct sun6i_spi *sspi = dev_id;
  393. u32 status = sun6i_spi_read(sspi, SUN6I_INT_STA_REG);
  394. /* Transfer complete */
  395. if (status & SUN6I_INT_CTL_TC) {
  396. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TC);
  397. complete(&sspi->done);
  398. return IRQ_HANDLED;
  399. }
  400. /* Receive FIFO 3/4 full */
  401. if (status & SUN6I_INT_CTL_RF_RDY) {
  402. sun6i_spi_drain_fifo(sspi);
  403. /* Only clear the interrupt _after_ draining the FIFO */
  404. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_RF_RDY);
  405. return IRQ_HANDLED;
  406. }
  407. /* Transmit FIFO 3/4 empty */
  408. if (status & SUN6I_INT_CTL_TF_ERQ) {
  409. sun6i_spi_fill_fifo(sspi);
  410. if (!sspi->len)
  411. /* nothing left to transmit */
  412. sun6i_spi_disable_interrupt(sspi, SUN6I_INT_CTL_TF_ERQ);
  413. /* Only clear the interrupt _after_ re-seeding the FIFO */
  414. sun6i_spi_write(sspi, SUN6I_INT_STA_REG, SUN6I_INT_CTL_TF_ERQ);
  415. return IRQ_HANDLED;
  416. }
  417. return IRQ_NONE;
  418. }
  419. static int sun6i_spi_runtime_resume(struct device *dev)
  420. {
  421. struct spi_master *master = dev_get_drvdata(dev);
  422. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  423. int ret;
  424. ret = clk_prepare_enable(sspi->hclk);
  425. if (ret) {
  426. dev_err(dev, "Couldn't enable AHB clock\n");
  427. goto out;
  428. }
  429. ret = clk_prepare_enable(sspi->mclk);
  430. if (ret) {
  431. dev_err(dev, "Couldn't enable module clock\n");
  432. goto err;
  433. }
  434. ret = reset_control_deassert(sspi->rstc);
  435. if (ret) {
  436. dev_err(dev, "Couldn't deassert the device from reset\n");
  437. goto err2;
  438. }
  439. sun6i_spi_write(sspi, SUN6I_GBL_CTL_REG,
  440. SUN6I_GBL_CTL_MASTER | SUN6I_GBL_CTL_TP);
  441. return 0;
  442. err2:
  443. clk_disable_unprepare(sspi->mclk);
  444. err:
  445. clk_disable_unprepare(sspi->hclk);
  446. out:
  447. return ret;
  448. }
  449. static int sun6i_spi_runtime_suspend(struct device *dev)
  450. {
  451. struct spi_master *master = dev_get_drvdata(dev);
  452. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  453. reset_control_assert(sspi->rstc);
  454. clk_disable_unprepare(sspi->mclk);
  455. clk_disable_unprepare(sspi->hclk);
  456. return 0;
  457. }
  458. static bool sun6i_spi_can_dma(struct spi_master *master,
  459. struct spi_device *spi,
  460. struct spi_transfer *xfer)
  461. {
  462. struct sun6i_spi *sspi = spi_master_get_devdata(master);
  463. /*
  464. * If the number of spi words to transfer is less or equal than
  465. * the fifo length we can just fill the fifo and wait for a single
  466. * irq, so don't bother setting up dma
  467. */
  468. return xfer->len > sspi->fifo_depth;
  469. }
  470. static int sun6i_spi_probe(struct platform_device *pdev)
  471. {
  472. struct spi_master *master;
  473. struct sun6i_spi *sspi;
  474. struct resource *mem;
  475. int ret = 0, irq;
  476. master = spi_alloc_master(&pdev->dev, sizeof(struct sun6i_spi));
  477. if (!master) {
  478. dev_err(&pdev->dev, "Unable to allocate SPI Master\n");
  479. return -ENOMEM;
  480. }
  481. platform_set_drvdata(pdev, master);
  482. sspi = spi_master_get_devdata(master);
  483. sspi->base_addr = devm_platform_get_and_ioremap_resource(pdev, 0, &mem);
  484. if (IS_ERR(sspi->base_addr)) {
  485. ret = PTR_ERR(sspi->base_addr);
  486. goto err_free_master;
  487. }
  488. irq = platform_get_irq(pdev, 0);
  489. if (irq < 0) {
  490. ret = -ENXIO;
  491. goto err_free_master;
  492. }
  493. ret = devm_request_irq(&pdev->dev, irq, sun6i_spi_handler,
  494. 0, "sun6i-spi", sspi);
  495. if (ret) {
  496. dev_err(&pdev->dev, "Cannot request IRQ\n");
  497. goto err_free_master;
  498. }
  499. sspi->master = master;
  500. sspi->fifo_depth = (unsigned long)of_device_get_match_data(&pdev->dev);
  501. master->max_speed_hz = 100 * 1000 * 1000;
  502. master->min_speed_hz = 3 * 1000;
  503. master->use_gpio_descriptors = true;
  504. master->set_cs = sun6i_spi_set_cs;
  505. master->transfer_one = sun6i_spi_transfer_one;
  506. master->num_chipselect = 4;
  507. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH | SPI_LSB_FIRST;
  508. master->bits_per_word_mask = SPI_BPW_MASK(8);
  509. master->dev.of_node = pdev->dev.of_node;
  510. master->auto_runtime_pm = true;
  511. master->max_transfer_size = sun6i_spi_max_transfer_size;
  512. sspi->hclk = devm_clk_get(&pdev->dev, "ahb");
  513. if (IS_ERR(sspi->hclk)) {
  514. dev_err(&pdev->dev, "Unable to acquire AHB clock\n");
  515. ret = PTR_ERR(sspi->hclk);
  516. goto err_free_master;
  517. }
  518. sspi->mclk = devm_clk_get(&pdev->dev, "mod");
  519. if (IS_ERR(sspi->mclk)) {
  520. dev_err(&pdev->dev, "Unable to acquire module clock\n");
  521. ret = PTR_ERR(sspi->mclk);
  522. goto err_free_master;
  523. }
  524. init_completion(&sspi->done);
  525. init_completion(&sspi->dma_rx_done);
  526. sspi->rstc = devm_reset_control_get_exclusive(&pdev->dev, NULL);
  527. if (IS_ERR(sspi->rstc)) {
  528. dev_err(&pdev->dev, "Couldn't get reset controller\n");
  529. ret = PTR_ERR(sspi->rstc);
  530. goto err_free_master;
  531. }
  532. master->dma_tx = dma_request_chan(&pdev->dev, "tx");
  533. if (IS_ERR(master->dma_tx)) {
  534. /* Check tx to see if we need defer probing driver */
  535. if (PTR_ERR(master->dma_tx) == -EPROBE_DEFER) {
  536. ret = -EPROBE_DEFER;
  537. goto err_free_master;
  538. }
  539. dev_warn(&pdev->dev, "Failed to request TX DMA channel\n");
  540. master->dma_tx = NULL;
  541. }
  542. master->dma_rx = dma_request_chan(&pdev->dev, "rx");
  543. if (IS_ERR(master->dma_rx)) {
  544. if (PTR_ERR(master->dma_rx) == -EPROBE_DEFER) {
  545. ret = -EPROBE_DEFER;
  546. goto err_free_dma_tx;
  547. }
  548. dev_warn(&pdev->dev, "Failed to request RX DMA channel\n");
  549. master->dma_rx = NULL;
  550. }
  551. if (master->dma_tx && master->dma_rx) {
  552. sspi->dma_addr_tx = mem->start + SUN6I_TXDATA_REG;
  553. sspi->dma_addr_rx = mem->start + SUN6I_RXDATA_REG;
  554. master->can_dma = sun6i_spi_can_dma;
  555. }
  556. /*
  557. * This wake-up/shutdown pattern is to be able to have the
  558. * device woken up, even if runtime_pm is disabled
  559. */
  560. ret = sun6i_spi_runtime_resume(&pdev->dev);
  561. if (ret) {
  562. dev_err(&pdev->dev, "Couldn't resume the device\n");
  563. goto err_free_dma_rx;
  564. }
  565. pm_runtime_set_autosuspend_delay(&pdev->dev, SUN6I_AUTOSUSPEND_TIMEOUT);
  566. pm_runtime_use_autosuspend(&pdev->dev);
  567. pm_runtime_set_active(&pdev->dev);
  568. pm_runtime_enable(&pdev->dev);
  569. ret = devm_spi_register_master(&pdev->dev, master);
  570. if (ret) {
  571. dev_err(&pdev->dev, "cannot register SPI master\n");
  572. goto err_pm_disable;
  573. }
  574. return 0;
  575. err_pm_disable:
  576. pm_runtime_disable(&pdev->dev);
  577. sun6i_spi_runtime_suspend(&pdev->dev);
  578. err_free_dma_rx:
  579. if (master->dma_rx)
  580. dma_release_channel(master->dma_rx);
  581. err_free_dma_tx:
  582. if (master->dma_tx)
  583. dma_release_channel(master->dma_tx);
  584. err_free_master:
  585. spi_master_put(master);
  586. return ret;
  587. }
  588. static int sun6i_spi_remove(struct platform_device *pdev)
  589. {
  590. struct spi_master *master = platform_get_drvdata(pdev);
  591. pm_runtime_force_suspend(&pdev->dev);
  592. if (master->dma_tx)
  593. dma_release_channel(master->dma_tx);
  594. if (master->dma_rx)
  595. dma_release_channel(master->dma_rx);
  596. return 0;
  597. }
  598. static const struct of_device_id sun6i_spi_match[] = {
  599. { .compatible = "allwinner,sun6i-a31-spi", .data = (void *)SUN6I_FIFO_DEPTH },
  600. { .compatible = "allwinner,sun8i-h3-spi", .data = (void *)SUN8I_FIFO_DEPTH },
  601. {}
  602. };
  603. MODULE_DEVICE_TABLE(of, sun6i_spi_match);
  604. static const struct dev_pm_ops sun6i_spi_pm_ops = {
  605. .runtime_resume = sun6i_spi_runtime_resume,
  606. .runtime_suspend = sun6i_spi_runtime_suspend,
  607. };
  608. static struct platform_driver sun6i_spi_driver = {
  609. .probe = sun6i_spi_probe,
  610. .remove = sun6i_spi_remove,
  611. .driver = {
  612. .name = "sun6i-spi",
  613. .of_match_table = sun6i_spi_match,
  614. .pm = &sun6i_spi_pm_ops,
  615. },
  616. };
  617. module_platform_driver(sun6i_spi_driver);
  618. MODULE_AUTHOR("Pan Nan <[email protected]>");
  619. MODULE_AUTHOR("Maxime Ripard <[email protected]>");
  620. MODULE_DESCRIPTION("Allwinner A31 SPI controller driver");
  621. MODULE_LICENSE("GPL");