spi-rockchip.c 22 KB

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