spi-rockchip.c 23 KB

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