spi-rockchip.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  3. * Author: Addy Ke <addy.ke@rock-chips.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/clk.h>
  18. #include <linux/err.h>
  19. #include <linux/delay.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/slab.h>
  23. #include <linux/spi/spi.h>
  24. #include <linux/scatterlist.h>
  25. #include <linux/of.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/io.h>
  28. #include <linux/dmaengine.h>
  29. #define DRIVER_NAME "rockchip-spi"
  30. /* SPI register offsets */
  31. #define ROCKCHIP_SPI_CTRLR0 0x0000
  32. #define ROCKCHIP_SPI_CTRLR1 0x0004
  33. #define ROCKCHIP_SPI_SSIENR 0x0008
  34. #define ROCKCHIP_SPI_SER 0x000c
  35. #define ROCKCHIP_SPI_BAUDR 0x0010
  36. #define ROCKCHIP_SPI_TXFTLR 0x0014
  37. #define ROCKCHIP_SPI_RXFTLR 0x0018
  38. #define ROCKCHIP_SPI_TXFLR 0x001c
  39. #define ROCKCHIP_SPI_RXFLR 0x0020
  40. #define ROCKCHIP_SPI_SR 0x0024
  41. #define ROCKCHIP_SPI_IPR 0x0028
  42. #define ROCKCHIP_SPI_IMR 0x002c
  43. #define ROCKCHIP_SPI_ISR 0x0030
  44. #define ROCKCHIP_SPI_RISR 0x0034
  45. #define ROCKCHIP_SPI_ICR 0x0038
  46. #define ROCKCHIP_SPI_DMACR 0x003c
  47. #define ROCKCHIP_SPI_DMATDLR 0x0040
  48. #define ROCKCHIP_SPI_DMARDLR 0x0044
  49. #define ROCKCHIP_SPI_TXDR 0x0400
  50. #define ROCKCHIP_SPI_RXDR 0x0800
  51. /* Bit fields in CTRLR0 */
  52. #define CR0_DFS_OFFSET 0
  53. #define CR0_CFS_OFFSET 2
  54. #define CR0_SCPH_OFFSET 6
  55. #define CR0_SCPOL_OFFSET 7
  56. #define CR0_CSM_OFFSET 8
  57. #define CR0_CSM_KEEP 0x0
  58. /* ss_n be high for half sclk_out cycles */
  59. #define CR0_CSM_HALF 0X1
  60. /* ss_n be high for one sclk_out cycle */
  61. #define CR0_CSM_ONE 0x2
  62. /* ss_n to sclk_out delay */
  63. #define CR0_SSD_OFFSET 10
  64. /*
  65. * The period between ss_n active and
  66. * sclk_out active is half sclk_out cycles
  67. */
  68. #define CR0_SSD_HALF 0x0
  69. /*
  70. * The period between ss_n active and
  71. * sclk_out active is one sclk_out cycle
  72. */
  73. #define CR0_SSD_ONE 0x1
  74. #define CR0_EM_OFFSET 11
  75. #define CR0_EM_LITTLE 0x0
  76. #define CR0_EM_BIG 0x1
  77. #define CR0_FBM_OFFSET 12
  78. #define CR0_FBM_MSB 0x0
  79. #define CR0_FBM_LSB 0x1
  80. #define CR0_BHT_OFFSET 13
  81. #define CR0_BHT_16BIT 0x0
  82. #define CR0_BHT_8BIT 0x1
  83. #define CR0_RSD_OFFSET 14
  84. #define CR0_FRF_OFFSET 16
  85. #define CR0_FRF_SPI 0x0
  86. #define CR0_FRF_SSP 0x1
  87. #define CR0_FRF_MICROWIRE 0x2
  88. #define CR0_XFM_OFFSET 18
  89. #define CR0_XFM_MASK (0x03 << SPI_XFM_OFFSET)
  90. #define CR0_XFM_TR 0x0
  91. #define CR0_XFM_TO 0x1
  92. #define CR0_XFM_RO 0x2
  93. #define CR0_OPM_OFFSET 20
  94. #define CR0_OPM_MASTER 0x0
  95. #define CR0_OPM_SLAVE 0x1
  96. #define CR0_MTM_OFFSET 0x21
  97. /* Bit fields in SER, 2bit */
  98. #define SER_MASK 0x3
  99. /* Bit fields in SR, 5bit */
  100. #define SR_MASK 0x1f
  101. #define SR_BUSY (1 << 0)
  102. #define SR_TF_FULL (1 << 1)
  103. #define SR_TF_EMPTY (1 << 2)
  104. #define SR_RF_EMPTY (1 << 3)
  105. #define SR_RF_FULL (1 << 4)
  106. /* Bit fields in ISR, IMR, ISR, RISR, 5bit */
  107. #define INT_MASK 0x1f
  108. #define INT_TF_EMPTY (1 << 0)
  109. #define INT_TF_OVERFLOW (1 << 1)
  110. #define INT_RF_UNDERFLOW (1 << 2)
  111. #define INT_RF_OVERFLOW (1 << 3)
  112. #define INT_RF_FULL (1 << 4)
  113. /* Bit fields in ICR, 4bit */
  114. #define ICR_MASK 0x0f
  115. #define ICR_ALL (1 << 0)
  116. #define ICR_RF_UNDERFLOW (1 << 1)
  117. #define ICR_RF_OVERFLOW (1 << 2)
  118. #define ICR_TF_OVERFLOW (1 << 3)
  119. /* Bit fields in DMACR */
  120. #define RF_DMA_EN (1 << 0)
  121. #define TF_DMA_EN (1 << 1)
  122. #define RXBUSY (1 << 0)
  123. #define TXBUSY (1 << 1)
  124. /* sclk_out: spi master internal logic in rk3x can support 50Mhz */
  125. #define MAX_SCLK_OUT 50000000
  126. enum rockchip_ssi_type {
  127. SSI_MOTO_SPI = 0,
  128. SSI_TI_SSP,
  129. SSI_NS_MICROWIRE,
  130. };
  131. struct rockchip_spi_dma_data {
  132. struct dma_chan *ch;
  133. enum dma_transfer_direction direction;
  134. dma_addr_t addr;
  135. };
  136. struct rockchip_spi {
  137. struct device *dev;
  138. struct spi_master *master;
  139. struct clk *spiclk;
  140. struct clk *apb_pclk;
  141. void __iomem *regs;
  142. /*depth of the FIFO buffer */
  143. u32 fifo_len;
  144. /* max bus freq supported */
  145. u32 max_freq;
  146. /* supported slave numbers */
  147. enum rockchip_ssi_type type;
  148. u16 mode;
  149. u8 tmode;
  150. u8 bpw;
  151. u8 n_bytes;
  152. u8 rsd_nsecs;
  153. unsigned len;
  154. u32 speed;
  155. const void *tx;
  156. const void *tx_end;
  157. void *rx;
  158. void *rx_end;
  159. u32 state;
  160. /* protect state */
  161. spinlock_t lock;
  162. struct completion xfer_completion;
  163. u32 use_dma;
  164. struct sg_table tx_sg;
  165. struct sg_table rx_sg;
  166. struct rockchip_spi_dma_data dma_rx;
  167. struct rockchip_spi_dma_data dma_tx;
  168. };
  169. static inline void spi_enable_chip(struct rockchip_spi *rs, int enable)
  170. {
  171. writel_relaxed((enable ? 1 : 0), rs->regs + ROCKCHIP_SPI_SSIENR);
  172. }
  173. static inline void spi_set_clk(struct rockchip_spi *rs, u16 div)
  174. {
  175. writel_relaxed(div, rs->regs + ROCKCHIP_SPI_BAUDR);
  176. }
  177. static inline void flush_fifo(struct rockchip_spi *rs)
  178. {
  179. while (readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR))
  180. readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
  181. }
  182. static inline void wait_for_idle(struct rockchip_spi *rs)
  183. {
  184. unsigned long timeout = jiffies + msecs_to_jiffies(5);
  185. do {
  186. if (!(readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY))
  187. return;
  188. } while (!time_after(jiffies, timeout));
  189. dev_warn(rs->dev, "spi controller is in busy state!\n");
  190. }
  191. static u32 get_fifo_len(struct rockchip_spi *rs)
  192. {
  193. u32 fifo;
  194. for (fifo = 2; fifo < 32; fifo++) {
  195. writel_relaxed(fifo, rs->regs + ROCKCHIP_SPI_TXFTLR);
  196. if (fifo != readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFTLR))
  197. break;
  198. }
  199. writel_relaxed(0, rs->regs + ROCKCHIP_SPI_TXFTLR);
  200. return (fifo == 31) ? 0 : fifo;
  201. }
  202. static inline u32 tx_max(struct rockchip_spi *rs)
  203. {
  204. u32 tx_left, tx_room;
  205. tx_left = (rs->tx_end - rs->tx) / rs->n_bytes;
  206. tx_room = rs->fifo_len - readl_relaxed(rs->regs + ROCKCHIP_SPI_TXFLR);
  207. return min(tx_left, tx_room);
  208. }
  209. static inline u32 rx_max(struct rockchip_spi *rs)
  210. {
  211. u32 rx_left = (rs->rx_end - rs->rx) / rs->n_bytes;
  212. u32 rx_room = (u32)readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
  213. return min(rx_left, rx_room);
  214. }
  215. static void rockchip_spi_set_cs(struct spi_device *spi, bool enable)
  216. {
  217. u32 ser;
  218. struct spi_master *master = spi->master;
  219. struct rockchip_spi *rs = spi_master_get_devdata(master);
  220. pm_runtime_get_sync(rs->dev);
  221. ser = readl_relaxed(rs->regs + ROCKCHIP_SPI_SER) & SER_MASK;
  222. /*
  223. * drivers/spi/spi.c:
  224. * static void spi_set_cs(struct spi_device *spi, bool enable)
  225. * {
  226. * if (spi->mode & SPI_CS_HIGH)
  227. * enable = !enable;
  228. *
  229. * if (spi->cs_gpio >= 0)
  230. * gpio_set_value(spi->cs_gpio, !enable);
  231. * else if (spi->master->set_cs)
  232. * spi->master->set_cs(spi, !enable);
  233. * }
  234. *
  235. * Note: enable(rockchip_spi_set_cs) = !enable(spi_set_cs)
  236. */
  237. if (!enable)
  238. ser |= 1 << spi->chip_select;
  239. else
  240. ser &= ~(1 << spi->chip_select);
  241. writel_relaxed(ser, rs->regs + ROCKCHIP_SPI_SER);
  242. pm_runtime_put_sync(rs->dev);
  243. }
  244. static int rockchip_spi_prepare_message(struct spi_master *master,
  245. struct spi_message *msg)
  246. {
  247. struct rockchip_spi *rs = spi_master_get_devdata(master);
  248. struct spi_device *spi = msg->spi;
  249. rs->mode = spi->mode;
  250. return 0;
  251. }
  252. static void rockchip_spi_handle_err(struct spi_master *master,
  253. struct spi_message *msg)
  254. {
  255. unsigned long flags;
  256. struct rockchip_spi *rs = spi_master_get_devdata(master);
  257. spin_lock_irqsave(&rs->lock, flags);
  258. /*
  259. * For DMA mode, we need terminate DMA channel and flush
  260. * fifo for the next transfer if DMA thansfer timeout.
  261. * handle_err() was called by core if transfer failed.
  262. * Maybe it is reasonable for error handling here.
  263. */
  264. if (rs->use_dma) {
  265. if (rs->state & RXBUSY) {
  266. dmaengine_terminate_all(rs->dma_rx.ch);
  267. flush_fifo(rs);
  268. }
  269. if (rs->state & TXBUSY)
  270. dmaengine_terminate_all(rs->dma_tx.ch);
  271. }
  272. spin_unlock_irqrestore(&rs->lock, flags);
  273. }
  274. static int rockchip_spi_unprepare_message(struct spi_master *master,
  275. struct spi_message *msg)
  276. {
  277. struct rockchip_spi *rs = spi_master_get_devdata(master);
  278. spi_enable_chip(rs, 0);
  279. return 0;
  280. }
  281. static void rockchip_spi_pio_writer(struct rockchip_spi *rs)
  282. {
  283. u32 max = tx_max(rs);
  284. u32 txw = 0;
  285. while (max--) {
  286. if (rs->n_bytes == 1)
  287. txw = *(u8 *)(rs->tx);
  288. else
  289. txw = *(u16 *)(rs->tx);
  290. writel_relaxed(txw, rs->regs + ROCKCHIP_SPI_TXDR);
  291. rs->tx += rs->n_bytes;
  292. }
  293. }
  294. static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
  295. {
  296. u32 max = rx_max(rs);
  297. u32 rxw;
  298. while (max--) {
  299. rxw = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXDR);
  300. if (rs->n_bytes == 1)
  301. *(u8 *)(rs->rx) = (u8)rxw;
  302. else
  303. *(u16 *)(rs->rx) = (u16)rxw;
  304. rs->rx += rs->n_bytes;
  305. }
  306. }
  307. static int rockchip_spi_pio_transfer(struct rockchip_spi *rs)
  308. {
  309. int remain = 0;
  310. do {
  311. if (rs->tx) {
  312. remain = rs->tx_end - rs->tx;
  313. rockchip_spi_pio_writer(rs);
  314. }
  315. if (rs->rx) {
  316. remain = rs->rx_end - rs->rx;
  317. rockchip_spi_pio_reader(rs);
  318. }
  319. cpu_relax();
  320. } while (remain);
  321. /* If tx, wait until the FIFO data completely. */
  322. if (rs->tx)
  323. wait_for_idle(rs);
  324. spi_enable_chip(rs, 0);
  325. return 0;
  326. }
  327. static void rockchip_spi_dma_rxcb(void *data)
  328. {
  329. unsigned long flags;
  330. struct rockchip_spi *rs = data;
  331. spin_lock_irqsave(&rs->lock, flags);
  332. rs->state &= ~RXBUSY;
  333. if (!(rs->state & TXBUSY)) {
  334. spi_enable_chip(rs, 0);
  335. spi_finalize_current_transfer(rs->master);
  336. }
  337. spin_unlock_irqrestore(&rs->lock, flags);
  338. }
  339. static void rockchip_spi_dma_txcb(void *data)
  340. {
  341. unsigned long flags;
  342. struct rockchip_spi *rs = data;
  343. /* Wait until the FIFO data completely. */
  344. wait_for_idle(rs);
  345. spin_lock_irqsave(&rs->lock, flags);
  346. rs->state &= ~TXBUSY;
  347. if (!(rs->state & RXBUSY)) {
  348. spi_enable_chip(rs, 0);
  349. spi_finalize_current_transfer(rs->master);
  350. }
  351. spin_unlock_irqrestore(&rs->lock, flags);
  352. }
  353. static void rockchip_spi_prepare_dma(struct rockchip_spi *rs)
  354. {
  355. unsigned long flags;
  356. struct dma_slave_config rxconf, txconf;
  357. struct dma_async_tx_descriptor *rxdesc, *txdesc;
  358. spin_lock_irqsave(&rs->lock, flags);
  359. rs->state &= ~RXBUSY;
  360. rs->state &= ~TXBUSY;
  361. spin_unlock_irqrestore(&rs->lock, flags);
  362. rxdesc = NULL;
  363. if (rs->rx) {
  364. rxconf.direction = rs->dma_rx.direction;
  365. rxconf.src_addr = rs->dma_rx.addr;
  366. rxconf.src_addr_width = rs->n_bytes;
  367. rxconf.src_maxburst = rs->n_bytes;
  368. dmaengine_slave_config(rs->dma_rx.ch, &rxconf);
  369. rxdesc = dmaengine_prep_slave_sg(
  370. rs->dma_rx.ch,
  371. rs->rx_sg.sgl, rs->rx_sg.nents,
  372. rs->dma_rx.direction, DMA_PREP_INTERRUPT);
  373. rxdesc->callback = rockchip_spi_dma_rxcb;
  374. rxdesc->callback_param = rs;
  375. }
  376. txdesc = NULL;
  377. if (rs->tx) {
  378. txconf.direction = rs->dma_tx.direction;
  379. txconf.dst_addr = rs->dma_tx.addr;
  380. txconf.dst_addr_width = rs->n_bytes;
  381. txconf.dst_maxburst = rs->n_bytes;
  382. dmaengine_slave_config(rs->dma_tx.ch, &txconf);
  383. txdesc = dmaengine_prep_slave_sg(
  384. rs->dma_tx.ch,
  385. rs->tx_sg.sgl, rs->tx_sg.nents,
  386. rs->dma_tx.direction, DMA_PREP_INTERRUPT);
  387. txdesc->callback = rockchip_spi_dma_txcb;
  388. txdesc->callback_param = rs;
  389. }
  390. /* rx must be started before tx due to spi instinct */
  391. if (rxdesc) {
  392. spin_lock_irqsave(&rs->lock, flags);
  393. rs->state |= RXBUSY;
  394. spin_unlock_irqrestore(&rs->lock, flags);
  395. dmaengine_submit(rxdesc);
  396. dma_async_issue_pending(rs->dma_rx.ch);
  397. }
  398. if (txdesc) {
  399. spin_lock_irqsave(&rs->lock, flags);
  400. rs->state |= TXBUSY;
  401. spin_unlock_irqrestore(&rs->lock, flags);
  402. dmaengine_submit(txdesc);
  403. dma_async_issue_pending(rs->dma_tx.ch);
  404. }
  405. }
  406. static void rockchip_spi_config(struct rockchip_spi *rs)
  407. {
  408. u32 div = 0;
  409. u32 dmacr = 0;
  410. int rsd = 0;
  411. u32 cr0 = (CR0_BHT_8BIT << CR0_BHT_OFFSET)
  412. | (CR0_SSD_ONE << CR0_SSD_OFFSET);
  413. cr0 |= (rs->n_bytes << CR0_DFS_OFFSET);
  414. cr0 |= ((rs->mode & 0x3) << CR0_SCPH_OFFSET);
  415. cr0 |= (rs->tmode << CR0_XFM_OFFSET);
  416. cr0 |= (rs->type << CR0_FRF_OFFSET);
  417. if (rs->use_dma) {
  418. if (rs->tx)
  419. dmacr |= TF_DMA_EN;
  420. if (rs->rx)
  421. dmacr |= RF_DMA_EN;
  422. }
  423. if (WARN_ON(rs->speed > MAX_SCLK_OUT))
  424. rs->speed = MAX_SCLK_OUT;
  425. /* the minimum divsor is 2 */
  426. if (rs->max_freq < 2 * rs->speed) {
  427. clk_set_rate(rs->spiclk, 2 * rs->speed);
  428. rs->max_freq = clk_get_rate(rs->spiclk);
  429. }
  430. /* div doesn't support odd number */
  431. div = DIV_ROUND_UP(rs->max_freq, rs->speed);
  432. div = (div + 1) & 0xfffe;
  433. /* Rx sample delay is expressed in parent clock cycles (max 3) */
  434. rsd = DIV_ROUND_CLOSEST(rs->rsd_nsecs * (rs->max_freq >> 8),
  435. 1000000000 >> 8);
  436. if (!rsd && rs->rsd_nsecs) {
  437. pr_warn_once("rockchip-spi: %u Hz are too slow to express %u ns delay\n",
  438. rs->max_freq, rs->rsd_nsecs);
  439. } else if (rsd > 3) {
  440. rsd = 3;
  441. pr_warn_once("rockchip-spi: %u Hz are too fast to express %u ns delay, clamping at %u ns\n",
  442. rs->max_freq, rs->rsd_nsecs,
  443. rsd * 1000000000U / rs->max_freq);
  444. }
  445. cr0 |= rsd << CR0_RSD_OFFSET;
  446. writel_relaxed(cr0, rs->regs + ROCKCHIP_SPI_CTRLR0);
  447. writel_relaxed(rs->len - 1, rs->regs + ROCKCHIP_SPI_CTRLR1);
  448. writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_TXFTLR);
  449. writel_relaxed(rs->fifo_len / 2 - 1, rs->regs + ROCKCHIP_SPI_RXFTLR);
  450. writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMATDLR);
  451. writel_relaxed(0, rs->regs + ROCKCHIP_SPI_DMARDLR);
  452. writel_relaxed(dmacr, rs->regs + ROCKCHIP_SPI_DMACR);
  453. spi_set_clk(rs, div);
  454. dev_dbg(rs->dev, "cr0 0x%x, div %d\n", cr0, div);
  455. }
  456. static int rockchip_spi_transfer_one(
  457. struct spi_master *master,
  458. struct spi_device *spi,
  459. struct spi_transfer *xfer)
  460. {
  461. int ret = 1;
  462. struct rockchip_spi *rs = spi_master_get_devdata(master);
  463. WARN_ON(readl_relaxed(rs->regs + ROCKCHIP_SPI_SSIENR) &&
  464. (readl_relaxed(rs->regs + ROCKCHIP_SPI_SR) & SR_BUSY));
  465. if (!xfer->tx_buf && !xfer->rx_buf) {
  466. dev_err(rs->dev, "No buffer for transfer\n");
  467. return -EINVAL;
  468. }
  469. rs->speed = xfer->speed_hz;
  470. rs->bpw = xfer->bits_per_word;
  471. rs->n_bytes = rs->bpw >> 3;
  472. rs->tx = xfer->tx_buf;
  473. rs->tx_end = rs->tx + xfer->len;
  474. rs->rx = xfer->rx_buf;
  475. rs->rx_end = rs->rx + xfer->len;
  476. rs->len = xfer->len;
  477. rs->tx_sg = xfer->tx_sg;
  478. rs->rx_sg = xfer->rx_sg;
  479. if (rs->tx && rs->rx)
  480. rs->tmode = CR0_XFM_TR;
  481. else if (rs->tx)
  482. rs->tmode = CR0_XFM_TO;
  483. else if (rs->rx)
  484. rs->tmode = CR0_XFM_RO;
  485. /* we need prepare dma before spi was enabled */
  486. if (master->can_dma && master->can_dma(master, spi, xfer))
  487. rs->use_dma = 1;
  488. else
  489. rs->use_dma = 0;
  490. rockchip_spi_config(rs);
  491. if (rs->use_dma) {
  492. if (rs->tmode == CR0_XFM_RO) {
  493. /* rx: dma must be prepared first */
  494. rockchip_spi_prepare_dma(rs);
  495. spi_enable_chip(rs, 1);
  496. } else {
  497. /* tx or tr: spi must be enabled first */
  498. spi_enable_chip(rs, 1);
  499. rockchip_spi_prepare_dma(rs);
  500. }
  501. } else {
  502. spi_enable_chip(rs, 1);
  503. ret = rockchip_spi_pio_transfer(rs);
  504. }
  505. return ret;
  506. }
  507. static bool rockchip_spi_can_dma(struct spi_master *master,
  508. struct spi_device *spi,
  509. struct spi_transfer *xfer)
  510. {
  511. struct rockchip_spi *rs = spi_master_get_devdata(master);
  512. return (xfer->len > rs->fifo_len);
  513. }
  514. static int rockchip_spi_probe(struct platform_device *pdev)
  515. {
  516. int ret = 0;
  517. struct rockchip_spi *rs;
  518. struct spi_master *master;
  519. struct resource *mem;
  520. u32 rsd_nsecs;
  521. master = spi_alloc_master(&pdev->dev, sizeof(struct rockchip_spi));
  522. if (!master)
  523. return -ENOMEM;
  524. platform_set_drvdata(pdev, master);
  525. rs = spi_master_get_devdata(master);
  526. /* Get basic io resource and map it */
  527. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  528. rs->regs = devm_ioremap_resource(&pdev->dev, mem);
  529. if (IS_ERR(rs->regs)) {
  530. ret = PTR_ERR(rs->regs);
  531. goto err_ioremap_resource;
  532. }
  533. rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
  534. if (IS_ERR(rs->apb_pclk)) {
  535. dev_err(&pdev->dev, "Failed to get apb_pclk\n");
  536. ret = PTR_ERR(rs->apb_pclk);
  537. goto err_ioremap_resource;
  538. }
  539. rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
  540. if (IS_ERR(rs->spiclk)) {
  541. dev_err(&pdev->dev, "Failed to get spi_pclk\n");
  542. ret = PTR_ERR(rs->spiclk);
  543. goto err_ioremap_resource;
  544. }
  545. ret = clk_prepare_enable(rs->apb_pclk);
  546. if (ret) {
  547. dev_err(&pdev->dev, "Failed to enable apb_pclk\n");
  548. goto err_ioremap_resource;
  549. }
  550. ret = clk_prepare_enable(rs->spiclk);
  551. if (ret) {
  552. dev_err(&pdev->dev, "Failed to enable spi_clk\n");
  553. goto err_spiclk_enable;
  554. }
  555. spi_enable_chip(rs, 0);
  556. rs->type = SSI_MOTO_SPI;
  557. rs->master = master;
  558. rs->dev = &pdev->dev;
  559. rs->max_freq = clk_get_rate(rs->spiclk);
  560. if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
  561. &rsd_nsecs))
  562. rs->rsd_nsecs = rsd_nsecs;
  563. rs->fifo_len = get_fifo_len(rs);
  564. if (!rs->fifo_len) {
  565. dev_err(&pdev->dev, "Failed to get fifo length\n");
  566. ret = -EINVAL;
  567. goto err_get_fifo_len;
  568. }
  569. spin_lock_init(&rs->lock);
  570. pm_runtime_set_active(&pdev->dev);
  571. pm_runtime_enable(&pdev->dev);
  572. master->auto_runtime_pm = true;
  573. master->bus_num = pdev->id;
  574. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP;
  575. master->num_chipselect = 2;
  576. master->dev.of_node = pdev->dev.of_node;
  577. master->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8);
  578. master->set_cs = rockchip_spi_set_cs;
  579. master->prepare_message = rockchip_spi_prepare_message;
  580. master->unprepare_message = rockchip_spi_unprepare_message;
  581. master->transfer_one = rockchip_spi_transfer_one;
  582. master->handle_err = rockchip_spi_handle_err;
  583. rs->dma_tx.ch = dma_request_slave_channel(rs->dev, "tx");
  584. if (!rs->dma_tx.ch)
  585. dev_warn(rs->dev, "Failed to request TX DMA channel\n");
  586. rs->dma_rx.ch = dma_request_slave_channel(rs->dev, "rx");
  587. if (!rs->dma_rx.ch) {
  588. if (rs->dma_tx.ch) {
  589. dma_release_channel(rs->dma_tx.ch);
  590. rs->dma_tx.ch = NULL;
  591. }
  592. dev_warn(rs->dev, "Failed to request RX DMA channel\n");
  593. }
  594. if (rs->dma_tx.ch && rs->dma_rx.ch) {
  595. rs->dma_tx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_TXDR);
  596. rs->dma_rx.addr = (dma_addr_t)(mem->start + ROCKCHIP_SPI_RXDR);
  597. rs->dma_tx.direction = DMA_MEM_TO_DEV;
  598. rs->dma_rx.direction = DMA_DEV_TO_MEM;
  599. master->can_dma = rockchip_spi_can_dma;
  600. master->dma_tx = rs->dma_tx.ch;
  601. master->dma_rx = rs->dma_rx.ch;
  602. }
  603. ret = devm_spi_register_master(&pdev->dev, master);
  604. if (ret) {
  605. dev_err(&pdev->dev, "Failed to register master\n");
  606. goto err_register_master;
  607. }
  608. return 0;
  609. err_register_master:
  610. if (rs->dma_tx.ch)
  611. dma_release_channel(rs->dma_tx.ch);
  612. if (rs->dma_rx.ch)
  613. dma_release_channel(rs->dma_rx.ch);
  614. err_get_fifo_len:
  615. clk_disable_unprepare(rs->spiclk);
  616. err_spiclk_enable:
  617. clk_disable_unprepare(rs->apb_pclk);
  618. err_ioremap_resource:
  619. spi_master_put(master);
  620. return ret;
  621. }
  622. static int rockchip_spi_remove(struct platform_device *pdev)
  623. {
  624. struct spi_master *master = spi_master_get(platform_get_drvdata(pdev));
  625. struct rockchip_spi *rs = spi_master_get_devdata(master);
  626. pm_runtime_disable(&pdev->dev);
  627. clk_disable_unprepare(rs->spiclk);
  628. clk_disable_unprepare(rs->apb_pclk);
  629. if (rs->dma_tx.ch)
  630. dma_release_channel(rs->dma_tx.ch);
  631. if (rs->dma_rx.ch)
  632. dma_release_channel(rs->dma_rx.ch);
  633. return 0;
  634. }
  635. #ifdef CONFIG_PM_SLEEP
  636. static int rockchip_spi_suspend(struct device *dev)
  637. {
  638. int ret = 0;
  639. struct spi_master *master = dev_get_drvdata(dev);
  640. struct rockchip_spi *rs = spi_master_get_devdata(master);
  641. ret = spi_master_suspend(rs->master);
  642. if (ret)
  643. return ret;
  644. if (!pm_runtime_suspended(dev)) {
  645. clk_disable_unprepare(rs->spiclk);
  646. clk_disable_unprepare(rs->apb_pclk);
  647. }
  648. return ret;
  649. }
  650. static int rockchip_spi_resume(struct device *dev)
  651. {
  652. int ret = 0;
  653. struct spi_master *master = dev_get_drvdata(dev);
  654. struct rockchip_spi *rs = spi_master_get_devdata(master);
  655. if (!pm_runtime_suspended(dev)) {
  656. ret = clk_prepare_enable(rs->apb_pclk);
  657. if (ret < 0)
  658. return ret;
  659. ret = clk_prepare_enable(rs->spiclk);
  660. if (ret < 0) {
  661. clk_disable_unprepare(rs->apb_pclk);
  662. return ret;
  663. }
  664. }
  665. ret = spi_master_resume(rs->master);
  666. if (ret < 0) {
  667. clk_disable_unprepare(rs->spiclk);
  668. clk_disable_unprepare(rs->apb_pclk);
  669. }
  670. return ret;
  671. }
  672. #endif /* CONFIG_PM_SLEEP */
  673. #ifdef CONFIG_PM
  674. static int rockchip_spi_runtime_suspend(struct device *dev)
  675. {
  676. struct spi_master *master = dev_get_drvdata(dev);
  677. struct rockchip_spi *rs = spi_master_get_devdata(master);
  678. clk_disable_unprepare(rs->spiclk);
  679. clk_disable_unprepare(rs->apb_pclk);
  680. return 0;
  681. }
  682. static int rockchip_spi_runtime_resume(struct device *dev)
  683. {
  684. int ret;
  685. struct spi_master *master = dev_get_drvdata(dev);
  686. struct rockchip_spi *rs = spi_master_get_devdata(master);
  687. ret = clk_prepare_enable(rs->apb_pclk);
  688. if (ret)
  689. return ret;
  690. ret = clk_prepare_enable(rs->spiclk);
  691. if (ret)
  692. clk_disable_unprepare(rs->apb_pclk);
  693. return ret;
  694. }
  695. #endif /* CONFIG_PM */
  696. static const struct dev_pm_ops rockchip_spi_pm = {
  697. SET_SYSTEM_SLEEP_PM_OPS(rockchip_spi_suspend, rockchip_spi_resume)
  698. SET_RUNTIME_PM_OPS(rockchip_spi_runtime_suspend,
  699. rockchip_spi_runtime_resume, NULL)
  700. };
  701. static const struct of_device_id rockchip_spi_dt_match[] = {
  702. { .compatible = "rockchip,rk3066-spi", },
  703. { .compatible = "rockchip,rk3188-spi", },
  704. { .compatible = "rockchip,rk3288-spi", },
  705. { },
  706. };
  707. MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);
  708. static struct platform_driver rockchip_spi_driver = {
  709. .driver = {
  710. .name = DRIVER_NAME,
  711. .pm = &rockchip_spi_pm,
  712. .of_match_table = of_match_ptr(rockchip_spi_dt_match),
  713. },
  714. .probe = rockchip_spi_probe,
  715. .remove = rockchip_spi_remove,
  716. };
  717. module_platform_driver(rockchip_spi_driver);
  718. MODULE_AUTHOR("Addy Ke <addy.ke@rock-chips.com>");
  719. MODULE_DESCRIPTION("ROCKCHIP SPI Controller Driver");
  720. MODULE_LICENSE("GPL v2");