spi-fsl-dspi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. /*
  2. * drivers/spi/spi-fsl-dspi.c
  3. *
  4. * Copyright 2013 Freescale Semiconductor, Inc.
  5. *
  6. * Freescale DSPI driver
  7. * This file contains a driver for the Freescale DSPI
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/err.h>
  20. #include <linux/errno.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/kernel.h>
  24. #include <linux/math64.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/pinctrl/consumer.h>
  29. #include <linux/platform_device.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/regmap.h>
  32. #include <linux/sched.h>
  33. #include <linux/spi/spi.h>
  34. #include <linux/spi/spi_bitbang.h>
  35. #include <linux/time.h>
  36. #define DRIVER_NAME "fsl-dspi"
  37. #define TRAN_STATE_RX_VOID 0x01
  38. #define TRAN_STATE_TX_VOID 0x02
  39. #define TRAN_STATE_WORD_ODD_NUM 0x04
  40. #define DSPI_FIFO_SIZE 4
  41. #define DSPI_DMA_BUFSIZE (DSPI_FIFO_SIZE * 1024)
  42. #define SPI_MCR 0x00
  43. #define SPI_MCR_MASTER (1 << 31)
  44. #define SPI_MCR_PCSIS (0x3F << 16)
  45. #define SPI_MCR_CLR_TXF (1 << 11)
  46. #define SPI_MCR_CLR_RXF (1 << 10)
  47. #define SPI_TCR 0x08
  48. #define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
  49. #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
  50. #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
  51. #define SPI_CTAR_CPOL(x) ((x) << 26)
  52. #define SPI_CTAR_CPHA(x) ((x) << 25)
  53. #define SPI_CTAR_LSBFE(x) ((x) << 24)
  54. #define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
  55. #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
  56. #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
  57. #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
  58. #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
  59. #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
  60. #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
  61. #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
  62. #define SPI_CTAR_SCALE_BITS 0xf
  63. #define SPI_CTAR0_SLAVE 0x0c
  64. #define SPI_SR 0x2c
  65. #define SPI_SR_EOQF 0x10000000
  66. #define SPI_SR_TCFQF 0x80000000
  67. #define SPI_RSER_TFFFE BIT(25)
  68. #define SPI_RSER_TFFFD BIT(24)
  69. #define SPI_RSER_RFDFE BIT(17)
  70. #define SPI_RSER_RFDFD BIT(16)
  71. #define SPI_RSER 0x30
  72. #define SPI_RSER_EOQFE 0x10000000
  73. #define SPI_RSER_TCFQE 0x80000000
  74. #define SPI_PUSHR 0x34
  75. #define SPI_PUSHR_CONT (1 << 31)
  76. #define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
  77. #define SPI_PUSHR_EOQ (1 << 27)
  78. #define SPI_PUSHR_CTCNT (1 << 26)
  79. #define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
  80. #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
  81. #define SPI_PUSHR_SLAVE 0x34
  82. #define SPI_POPR 0x38
  83. #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
  84. #define SPI_TXFR0 0x3c
  85. #define SPI_TXFR1 0x40
  86. #define SPI_TXFR2 0x44
  87. #define SPI_TXFR3 0x48
  88. #define SPI_RXFR0 0x7c
  89. #define SPI_RXFR1 0x80
  90. #define SPI_RXFR2 0x84
  91. #define SPI_RXFR3 0x88
  92. #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
  93. #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
  94. #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
  95. #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
  96. #define SPI_CS_INIT 0x01
  97. #define SPI_CS_ASSERT 0x02
  98. #define SPI_CS_DROP 0x04
  99. #define SPI_TCR_TCNT_MAX 0x10000
  100. #define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
  101. struct chip_data {
  102. u32 mcr_val;
  103. u32 ctar_val;
  104. u16 void_write_data;
  105. };
  106. enum dspi_trans_mode {
  107. DSPI_EOQ_MODE = 0,
  108. DSPI_TCFQ_MODE,
  109. DSPI_DMA_MODE,
  110. };
  111. struct fsl_dspi_devtype_data {
  112. enum dspi_trans_mode trans_mode;
  113. u8 max_clock_factor;
  114. };
  115. static const struct fsl_dspi_devtype_data vf610_data = {
  116. .trans_mode = DSPI_DMA_MODE,
  117. .max_clock_factor = 2,
  118. };
  119. static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
  120. .trans_mode = DSPI_TCFQ_MODE,
  121. .max_clock_factor = 8,
  122. };
  123. static const struct fsl_dspi_devtype_data ls2085a_data = {
  124. .trans_mode = DSPI_TCFQ_MODE,
  125. .max_clock_factor = 8,
  126. };
  127. struct fsl_dspi_dma {
  128. /* Length of transfer in words of DSPI_FIFO_SIZE */
  129. u32 curr_xfer_len;
  130. u32 *tx_dma_buf;
  131. struct dma_chan *chan_tx;
  132. dma_addr_t tx_dma_phys;
  133. struct completion cmd_tx_complete;
  134. struct dma_async_tx_descriptor *tx_desc;
  135. u32 *rx_dma_buf;
  136. struct dma_chan *chan_rx;
  137. dma_addr_t rx_dma_phys;
  138. struct completion cmd_rx_complete;
  139. struct dma_async_tx_descriptor *rx_desc;
  140. };
  141. struct fsl_dspi {
  142. struct spi_master *master;
  143. struct platform_device *pdev;
  144. struct regmap *regmap;
  145. int irq;
  146. struct clk *clk;
  147. struct spi_transfer *cur_transfer;
  148. struct spi_message *cur_msg;
  149. struct chip_data *cur_chip;
  150. size_t len;
  151. void *tx;
  152. void *tx_end;
  153. void *rx;
  154. void *rx_end;
  155. char dataflags;
  156. u8 cs;
  157. u16 void_write_data;
  158. u32 cs_change;
  159. const struct fsl_dspi_devtype_data *devtype_data;
  160. wait_queue_head_t waitq;
  161. u32 waitflags;
  162. u32 spi_tcnt;
  163. struct fsl_dspi_dma *dma;
  164. };
  165. static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word);
  166. static inline int is_double_byte_mode(struct fsl_dspi *dspi)
  167. {
  168. unsigned int val;
  169. regmap_read(dspi->regmap, SPI_CTAR(0), &val);
  170. return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
  171. }
  172. static void dspi_tx_dma_callback(void *arg)
  173. {
  174. struct fsl_dspi *dspi = arg;
  175. struct fsl_dspi_dma *dma = dspi->dma;
  176. complete(&dma->cmd_tx_complete);
  177. }
  178. static void dspi_rx_dma_callback(void *arg)
  179. {
  180. struct fsl_dspi *dspi = arg;
  181. struct fsl_dspi_dma *dma = dspi->dma;
  182. int rx_word;
  183. int i;
  184. u16 d;
  185. rx_word = is_double_byte_mode(dspi);
  186. if (!(dspi->dataflags & TRAN_STATE_RX_VOID)) {
  187. for (i = 0; i < dma->curr_xfer_len; i++) {
  188. d = dspi->dma->rx_dma_buf[i];
  189. rx_word ? (*(u16 *)dspi->rx = d) :
  190. (*(u8 *)dspi->rx = d);
  191. dspi->rx += rx_word + 1;
  192. }
  193. }
  194. complete(&dma->cmd_rx_complete);
  195. }
  196. static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
  197. {
  198. struct fsl_dspi_dma *dma = dspi->dma;
  199. struct device *dev = &dspi->pdev->dev;
  200. int time_left;
  201. int tx_word;
  202. int i;
  203. tx_word = is_double_byte_mode(dspi);
  204. for (i = 0; i < dma->curr_xfer_len; i++) {
  205. dspi->dma->tx_dma_buf[i] = dspi_data_to_pushr(dspi, tx_word);
  206. if ((dspi->cs_change) && (!dspi->len))
  207. dspi->dma->tx_dma_buf[i] &= ~SPI_PUSHR_CONT;
  208. }
  209. dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
  210. dma->tx_dma_phys,
  211. dma->curr_xfer_len *
  212. DMA_SLAVE_BUSWIDTH_4_BYTES,
  213. DMA_MEM_TO_DEV,
  214. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  215. if (!dma->tx_desc) {
  216. dev_err(dev, "Not able to get desc for DMA xfer\n");
  217. return -EIO;
  218. }
  219. dma->tx_desc->callback = dspi_tx_dma_callback;
  220. dma->tx_desc->callback_param = dspi;
  221. if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
  222. dev_err(dev, "DMA submit failed\n");
  223. return -EINVAL;
  224. }
  225. dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
  226. dma->rx_dma_phys,
  227. dma->curr_xfer_len *
  228. DMA_SLAVE_BUSWIDTH_4_BYTES,
  229. DMA_DEV_TO_MEM,
  230. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  231. if (!dma->rx_desc) {
  232. dev_err(dev, "Not able to get desc for DMA xfer\n");
  233. return -EIO;
  234. }
  235. dma->rx_desc->callback = dspi_rx_dma_callback;
  236. dma->rx_desc->callback_param = dspi;
  237. if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
  238. dev_err(dev, "DMA submit failed\n");
  239. return -EINVAL;
  240. }
  241. reinit_completion(&dspi->dma->cmd_rx_complete);
  242. reinit_completion(&dspi->dma->cmd_tx_complete);
  243. dma_async_issue_pending(dma->chan_rx);
  244. dma_async_issue_pending(dma->chan_tx);
  245. time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
  246. DMA_COMPLETION_TIMEOUT);
  247. if (time_left == 0) {
  248. dev_err(dev, "DMA tx timeout\n");
  249. dmaengine_terminate_all(dma->chan_tx);
  250. dmaengine_terminate_all(dma->chan_rx);
  251. return -ETIMEDOUT;
  252. }
  253. time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
  254. DMA_COMPLETION_TIMEOUT);
  255. if (time_left == 0) {
  256. dev_err(dev, "DMA rx timeout\n");
  257. dmaengine_terminate_all(dma->chan_tx);
  258. dmaengine_terminate_all(dma->chan_rx);
  259. return -ETIMEDOUT;
  260. }
  261. return 0;
  262. }
  263. static int dspi_dma_xfer(struct fsl_dspi *dspi)
  264. {
  265. struct fsl_dspi_dma *dma = dspi->dma;
  266. struct device *dev = &dspi->pdev->dev;
  267. int curr_remaining_bytes;
  268. int bytes_per_buffer;
  269. int word = 1;
  270. int ret = 0;
  271. if (is_double_byte_mode(dspi))
  272. word = 2;
  273. curr_remaining_bytes = dspi->len;
  274. bytes_per_buffer = DSPI_DMA_BUFSIZE / DSPI_FIFO_SIZE;
  275. while (curr_remaining_bytes) {
  276. /* Check if current transfer fits the DMA buffer */
  277. dma->curr_xfer_len = curr_remaining_bytes / word;
  278. if (dma->curr_xfer_len > bytes_per_buffer)
  279. dma->curr_xfer_len = bytes_per_buffer;
  280. ret = dspi_next_xfer_dma_submit(dspi);
  281. if (ret) {
  282. dev_err(dev, "DMA transfer failed\n");
  283. goto exit;
  284. } else {
  285. curr_remaining_bytes -= dma->curr_xfer_len * word;
  286. if (curr_remaining_bytes < 0)
  287. curr_remaining_bytes = 0;
  288. }
  289. }
  290. exit:
  291. return ret;
  292. }
  293. static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
  294. {
  295. struct fsl_dspi_dma *dma;
  296. struct dma_slave_config cfg;
  297. struct device *dev = &dspi->pdev->dev;
  298. int ret;
  299. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  300. if (!dma)
  301. return -ENOMEM;
  302. dma->chan_rx = dma_request_slave_channel(dev, "rx");
  303. if (!dma->chan_rx) {
  304. dev_err(dev, "rx dma channel not available\n");
  305. ret = -ENODEV;
  306. return ret;
  307. }
  308. dma->chan_tx = dma_request_slave_channel(dev, "tx");
  309. if (!dma->chan_tx) {
  310. dev_err(dev, "tx dma channel not available\n");
  311. ret = -ENODEV;
  312. goto err_tx_channel;
  313. }
  314. dma->tx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
  315. &dma->tx_dma_phys, GFP_KERNEL);
  316. if (!dma->tx_dma_buf) {
  317. ret = -ENOMEM;
  318. goto err_tx_dma_buf;
  319. }
  320. dma->rx_dma_buf = dma_alloc_coherent(dev, DSPI_DMA_BUFSIZE,
  321. &dma->rx_dma_phys, GFP_KERNEL);
  322. if (!dma->rx_dma_buf) {
  323. ret = -ENOMEM;
  324. goto err_rx_dma_buf;
  325. }
  326. cfg.src_addr = phy_addr + SPI_POPR;
  327. cfg.dst_addr = phy_addr + SPI_PUSHR;
  328. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  329. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  330. cfg.src_maxburst = 1;
  331. cfg.dst_maxburst = 1;
  332. cfg.direction = DMA_DEV_TO_MEM;
  333. ret = dmaengine_slave_config(dma->chan_rx, &cfg);
  334. if (ret) {
  335. dev_err(dev, "can't configure rx dma channel\n");
  336. ret = -EINVAL;
  337. goto err_slave_config;
  338. }
  339. cfg.direction = DMA_MEM_TO_DEV;
  340. ret = dmaengine_slave_config(dma->chan_tx, &cfg);
  341. if (ret) {
  342. dev_err(dev, "can't configure tx dma channel\n");
  343. ret = -EINVAL;
  344. goto err_slave_config;
  345. }
  346. dspi->dma = dma;
  347. init_completion(&dma->cmd_tx_complete);
  348. init_completion(&dma->cmd_rx_complete);
  349. return 0;
  350. err_slave_config:
  351. dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
  352. dma->rx_dma_buf, dma->rx_dma_phys);
  353. err_rx_dma_buf:
  354. dma_free_coherent(dev, DSPI_DMA_BUFSIZE,
  355. dma->tx_dma_buf, dma->tx_dma_phys);
  356. err_tx_dma_buf:
  357. dma_release_channel(dma->chan_tx);
  358. err_tx_channel:
  359. dma_release_channel(dma->chan_rx);
  360. devm_kfree(dev, dma);
  361. dspi->dma = NULL;
  362. return ret;
  363. }
  364. static void dspi_release_dma(struct fsl_dspi *dspi)
  365. {
  366. struct fsl_dspi_dma *dma = dspi->dma;
  367. struct device *dev = &dspi->pdev->dev;
  368. if (dma) {
  369. if (dma->chan_tx) {
  370. dma_unmap_single(dev, dma->tx_dma_phys,
  371. DSPI_DMA_BUFSIZE, DMA_TO_DEVICE);
  372. dma_release_channel(dma->chan_tx);
  373. }
  374. if (dma->chan_rx) {
  375. dma_unmap_single(dev, dma->rx_dma_phys,
  376. DSPI_DMA_BUFSIZE, DMA_FROM_DEVICE);
  377. dma_release_channel(dma->chan_rx);
  378. }
  379. }
  380. }
  381. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  382. unsigned long clkrate)
  383. {
  384. /* Valid baud rate pre-scaler values */
  385. int pbr_tbl[4] = {2, 3, 5, 7};
  386. int brs[16] = { 2, 4, 6, 8,
  387. 16, 32, 64, 128,
  388. 256, 512, 1024, 2048,
  389. 4096, 8192, 16384, 32768 };
  390. int scale_needed, scale, minscale = INT_MAX;
  391. int i, j;
  392. scale_needed = clkrate / speed_hz;
  393. if (clkrate % speed_hz)
  394. scale_needed++;
  395. for (i = 0; i < ARRAY_SIZE(brs); i++)
  396. for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
  397. scale = brs[i] * pbr_tbl[j];
  398. if (scale >= scale_needed) {
  399. if (scale < minscale) {
  400. minscale = scale;
  401. *br = i;
  402. *pbr = j;
  403. }
  404. break;
  405. }
  406. }
  407. if (minscale == INT_MAX) {
  408. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
  409. speed_hz, clkrate);
  410. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  411. *br = ARRAY_SIZE(brs) - 1;
  412. }
  413. }
  414. static void ns_delay_scale(char *psc, char *sc, int delay_ns,
  415. unsigned long clkrate)
  416. {
  417. int pscale_tbl[4] = {1, 3, 5, 7};
  418. int scale_needed, scale, minscale = INT_MAX;
  419. int i, j;
  420. u32 remainder;
  421. scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
  422. &remainder);
  423. if (remainder)
  424. scale_needed++;
  425. for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
  426. for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
  427. scale = pscale_tbl[i] * (2 << j);
  428. if (scale >= scale_needed) {
  429. if (scale < minscale) {
  430. minscale = scale;
  431. *psc = i;
  432. *sc = j;
  433. }
  434. break;
  435. }
  436. }
  437. if (minscale == INT_MAX) {
  438. pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
  439. delay_ns, clkrate);
  440. *psc = ARRAY_SIZE(pscale_tbl) - 1;
  441. *sc = SPI_CTAR_SCALE_BITS;
  442. }
  443. }
  444. static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
  445. {
  446. u16 d16;
  447. if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
  448. d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
  449. else
  450. d16 = dspi->void_write_data;
  451. dspi->tx += tx_word + 1;
  452. dspi->len -= tx_word + 1;
  453. return SPI_PUSHR_TXDATA(d16) |
  454. SPI_PUSHR_PCS(dspi->cs) |
  455. SPI_PUSHR_CTAS(0) |
  456. SPI_PUSHR_CONT;
  457. }
  458. static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
  459. {
  460. u16 d;
  461. unsigned int val;
  462. regmap_read(dspi->regmap, SPI_POPR, &val);
  463. d = SPI_POPR_RXDATA(val);
  464. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  465. rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
  466. dspi->rx += rx_word + 1;
  467. }
  468. static int dspi_eoq_write(struct fsl_dspi *dspi)
  469. {
  470. int tx_count = 0;
  471. int tx_word;
  472. u32 dspi_pushr = 0;
  473. tx_word = is_double_byte_mode(dspi);
  474. while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
  475. /* If we are in word mode, only have a single byte to transfer
  476. * switch to byte mode temporarily. Will switch back at the
  477. * end of the transfer.
  478. */
  479. if (tx_word && (dspi->len == 1)) {
  480. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  481. regmap_update_bits(dspi->regmap, SPI_CTAR(0),
  482. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  483. tx_word = 0;
  484. }
  485. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  486. if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
  487. /* last transfer in the transfer */
  488. dspi_pushr |= SPI_PUSHR_EOQ;
  489. if ((dspi->cs_change) && (!dspi->len))
  490. dspi_pushr &= ~SPI_PUSHR_CONT;
  491. } else if (tx_word && (dspi->len == 1))
  492. dspi_pushr |= SPI_PUSHR_EOQ;
  493. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  494. tx_count++;
  495. }
  496. return tx_count * (tx_word + 1);
  497. }
  498. static int dspi_eoq_read(struct fsl_dspi *dspi)
  499. {
  500. int rx_count = 0;
  501. int rx_word = is_double_byte_mode(dspi);
  502. while ((dspi->rx < dspi->rx_end)
  503. && (rx_count < DSPI_FIFO_SIZE)) {
  504. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  505. rx_word = 0;
  506. dspi_data_from_popr(dspi, rx_word);
  507. rx_count++;
  508. }
  509. return rx_count;
  510. }
  511. static int dspi_tcfq_write(struct fsl_dspi *dspi)
  512. {
  513. int tx_word;
  514. u32 dspi_pushr = 0;
  515. tx_word = is_double_byte_mode(dspi);
  516. if (tx_word && (dspi->len == 1)) {
  517. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  518. regmap_update_bits(dspi->regmap, SPI_CTAR(0),
  519. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  520. tx_word = 0;
  521. }
  522. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  523. if ((dspi->cs_change) && (!dspi->len))
  524. dspi_pushr &= ~SPI_PUSHR_CONT;
  525. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  526. return tx_word + 1;
  527. }
  528. static void dspi_tcfq_read(struct fsl_dspi *dspi)
  529. {
  530. int rx_word = is_double_byte_mode(dspi);
  531. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  532. rx_word = 0;
  533. dspi_data_from_popr(dspi, rx_word);
  534. }
  535. static int dspi_transfer_one_message(struct spi_master *master,
  536. struct spi_message *message)
  537. {
  538. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  539. struct spi_device *spi = message->spi;
  540. struct spi_transfer *transfer;
  541. int status = 0;
  542. enum dspi_trans_mode trans_mode;
  543. u32 spi_tcr;
  544. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  545. dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  546. message->actual_length = 0;
  547. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  548. dspi->cur_transfer = transfer;
  549. dspi->cur_msg = message;
  550. dspi->cur_chip = spi_get_ctldata(spi);
  551. dspi->cs = spi->chip_select;
  552. dspi->cs_change = 0;
  553. if (list_is_last(&dspi->cur_transfer->transfer_list,
  554. &dspi->cur_msg->transfers) || transfer->cs_change)
  555. dspi->cs_change = 1;
  556. dspi->void_write_data = dspi->cur_chip->void_write_data;
  557. dspi->dataflags = 0;
  558. dspi->tx = (void *)transfer->tx_buf;
  559. dspi->tx_end = dspi->tx + transfer->len;
  560. dspi->rx = transfer->rx_buf;
  561. dspi->rx_end = dspi->rx + transfer->len;
  562. dspi->len = transfer->len;
  563. if (!dspi->rx)
  564. dspi->dataflags |= TRAN_STATE_RX_VOID;
  565. if (!dspi->tx)
  566. dspi->dataflags |= TRAN_STATE_TX_VOID;
  567. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  568. regmap_update_bits(dspi->regmap, SPI_MCR,
  569. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  570. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  571. regmap_write(dspi->regmap, SPI_CTAR(0),
  572. dspi->cur_chip->ctar_val);
  573. trans_mode = dspi->devtype_data->trans_mode;
  574. switch (trans_mode) {
  575. case DSPI_EOQ_MODE:
  576. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  577. dspi_eoq_write(dspi);
  578. break;
  579. case DSPI_TCFQ_MODE:
  580. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
  581. dspi_tcfq_write(dspi);
  582. break;
  583. case DSPI_DMA_MODE:
  584. regmap_write(dspi->regmap, SPI_RSER,
  585. SPI_RSER_TFFFE | SPI_RSER_TFFFD |
  586. SPI_RSER_RFDFE | SPI_RSER_RFDFD);
  587. status = dspi_dma_xfer(dspi);
  588. break;
  589. default:
  590. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  591. trans_mode);
  592. status = -EINVAL;
  593. goto out;
  594. }
  595. if (trans_mode != DSPI_DMA_MODE) {
  596. if (wait_event_interruptible(dspi->waitq,
  597. dspi->waitflags))
  598. dev_err(&dspi->pdev->dev,
  599. "wait transfer complete fail!\n");
  600. dspi->waitflags = 0;
  601. }
  602. if (transfer->delay_usecs)
  603. udelay(transfer->delay_usecs);
  604. }
  605. out:
  606. message->status = status;
  607. spi_finalize_current_message(master);
  608. return status;
  609. }
  610. static int dspi_setup(struct spi_device *spi)
  611. {
  612. struct chip_data *chip;
  613. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  614. u32 cs_sck_delay = 0, sck_cs_delay = 0;
  615. unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
  616. unsigned char pasc = 0, asc = 0, fmsz = 0;
  617. unsigned long clkrate;
  618. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  619. fmsz = spi->bits_per_word - 1;
  620. } else {
  621. pr_err("Invalid wordsize\n");
  622. return -ENODEV;
  623. }
  624. /* Only alloc on first setup */
  625. chip = spi_get_ctldata(spi);
  626. if (chip == NULL) {
  627. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  628. if (!chip)
  629. return -ENOMEM;
  630. }
  631. of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
  632. &cs_sck_delay);
  633. of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
  634. &sck_cs_delay);
  635. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  636. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  637. chip->void_write_data = 0;
  638. clkrate = clk_get_rate(dspi->clk);
  639. hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
  640. /* Set PCS to SCK delay scale values */
  641. ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
  642. /* Set After SCK delay scale values */
  643. ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
  644. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  645. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  646. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  647. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  648. | SPI_CTAR_PCSSCK(pcssck)
  649. | SPI_CTAR_CSSCK(cssck)
  650. | SPI_CTAR_PASC(pasc)
  651. | SPI_CTAR_ASC(asc)
  652. | SPI_CTAR_PBR(pbr)
  653. | SPI_CTAR_BR(br);
  654. spi_set_ctldata(spi, chip);
  655. return 0;
  656. }
  657. static void dspi_cleanup(struct spi_device *spi)
  658. {
  659. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  660. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  661. spi->master->bus_num, spi->chip_select);
  662. kfree(chip);
  663. }
  664. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  665. {
  666. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  667. struct spi_message *msg = dspi->cur_msg;
  668. enum dspi_trans_mode trans_mode;
  669. u32 spi_sr, spi_tcr;
  670. u32 spi_tcnt, tcnt_diff;
  671. int tx_word;
  672. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  673. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  674. if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
  675. tx_word = is_double_byte_mode(dspi);
  676. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  677. spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  678. /*
  679. * The width of SPI Transfer Counter in SPI_TCR is 16bits,
  680. * so the max couner is 65535. When the counter reach 65535,
  681. * it will wrap around, counter reset to zero.
  682. * spi_tcnt my be less than dspi->spi_tcnt, it means the
  683. * counter already wrapped around.
  684. * SPI Transfer Counter is a counter of transmitted frames.
  685. * The size of frame maybe two bytes.
  686. */
  687. tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
  688. % SPI_TCR_TCNT_MAX;
  689. tcnt_diff *= (tx_word + 1);
  690. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
  691. tcnt_diff--;
  692. msg->actual_length += tcnt_diff;
  693. dspi->spi_tcnt = spi_tcnt;
  694. trans_mode = dspi->devtype_data->trans_mode;
  695. switch (trans_mode) {
  696. case DSPI_EOQ_MODE:
  697. dspi_eoq_read(dspi);
  698. break;
  699. case DSPI_TCFQ_MODE:
  700. dspi_tcfq_read(dspi);
  701. break;
  702. default:
  703. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  704. trans_mode);
  705. return IRQ_HANDLED;
  706. }
  707. if (!dspi->len) {
  708. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
  709. regmap_update_bits(dspi->regmap,
  710. SPI_CTAR(0),
  711. SPI_FRAME_BITS_MASK,
  712. SPI_FRAME_BITS(16));
  713. dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
  714. }
  715. dspi->waitflags = 1;
  716. wake_up_interruptible(&dspi->waitq);
  717. } else {
  718. switch (trans_mode) {
  719. case DSPI_EOQ_MODE:
  720. dspi_eoq_write(dspi);
  721. break;
  722. case DSPI_TCFQ_MODE:
  723. dspi_tcfq_write(dspi);
  724. break;
  725. default:
  726. dev_err(&dspi->pdev->dev,
  727. "unsupported trans_mode %u\n",
  728. trans_mode);
  729. }
  730. }
  731. }
  732. return IRQ_HANDLED;
  733. }
  734. static const struct of_device_id fsl_dspi_dt_ids[] = {
  735. { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
  736. { .compatible = "fsl,ls1021a-v1.0-dspi",
  737. .data = (void *)&ls1021a_v1_data, },
  738. { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
  739. { /* sentinel */ }
  740. };
  741. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  742. #ifdef CONFIG_PM_SLEEP
  743. static int dspi_suspend(struct device *dev)
  744. {
  745. struct spi_master *master = dev_get_drvdata(dev);
  746. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  747. spi_master_suspend(master);
  748. clk_disable_unprepare(dspi->clk);
  749. pinctrl_pm_select_sleep_state(dev);
  750. return 0;
  751. }
  752. static int dspi_resume(struct device *dev)
  753. {
  754. struct spi_master *master = dev_get_drvdata(dev);
  755. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  756. int ret;
  757. pinctrl_pm_select_default_state(dev);
  758. ret = clk_prepare_enable(dspi->clk);
  759. if (ret)
  760. return ret;
  761. spi_master_resume(master);
  762. return 0;
  763. }
  764. #endif /* CONFIG_PM_SLEEP */
  765. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  766. static const struct regmap_config dspi_regmap_config = {
  767. .reg_bits = 32,
  768. .val_bits = 32,
  769. .reg_stride = 4,
  770. .max_register = 0x88,
  771. };
  772. static int dspi_probe(struct platform_device *pdev)
  773. {
  774. struct device_node *np = pdev->dev.of_node;
  775. struct spi_master *master;
  776. struct fsl_dspi *dspi;
  777. struct resource *res;
  778. void __iomem *base;
  779. int ret = 0, cs_num, bus_num;
  780. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  781. if (!master)
  782. return -ENOMEM;
  783. dspi = spi_master_get_devdata(master);
  784. dspi->pdev = pdev;
  785. dspi->master = master;
  786. master->transfer = NULL;
  787. master->setup = dspi_setup;
  788. master->transfer_one_message = dspi_transfer_one_message;
  789. master->dev.of_node = pdev->dev.of_node;
  790. master->cleanup = dspi_cleanup;
  791. master->mode_bits = SPI_CPOL | SPI_CPHA;
  792. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  793. SPI_BPW_MASK(16);
  794. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  795. if (ret < 0) {
  796. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  797. goto out_master_put;
  798. }
  799. master->num_chipselect = cs_num;
  800. ret = of_property_read_u32(np, "bus-num", &bus_num);
  801. if (ret < 0) {
  802. dev_err(&pdev->dev, "can't get bus-num\n");
  803. goto out_master_put;
  804. }
  805. master->bus_num = bus_num;
  806. dspi->devtype_data = of_device_get_match_data(&pdev->dev);
  807. if (!dspi->devtype_data) {
  808. dev_err(&pdev->dev, "can't get devtype_data\n");
  809. ret = -EFAULT;
  810. goto out_master_put;
  811. }
  812. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  813. base = devm_ioremap_resource(&pdev->dev, res);
  814. if (IS_ERR(base)) {
  815. ret = PTR_ERR(base);
  816. goto out_master_put;
  817. }
  818. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  819. &dspi_regmap_config);
  820. if (IS_ERR(dspi->regmap)) {
  821. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  822. PTR_ERR(dspi->regmap));
  823. return PTR_ERR(dspi->regmap);
  824. }
  825. dspi->irq = platform_get_irq(pdev, 0);
  826. if (dspi->irq < 0) {
  827. dev_err(&pdev->dev, "can't get platform irq\n");
  828. ret = dspi->irq;
  829. goto out_master_put;
  830. }
  831. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  832. pdev->name, dspi);
  833. if (ret < 0) {
  834. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  835. goto out_master_put;
  836. }
  837. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  838. if (IS_ERR(dspi->clk)) {
  839. ret = PTR_ERR(dspi->clk);
  840. dev_err(&pdev->dev, "unable to get clock\n");
  841. goto out_master_put;
  842. }
  843. ret = clk_prepare_enable(dspi->clk);
  844. if (ret)
  845. goto out_master_put;
  846. if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
  847. if (dspi_request_dma(dspi, res->start)) {
  848. dev_err(&pdev->dev, "can't get dma channels\n");
  849. goto out_clk_put;
  850. }
  851. }
  852. master->max_speed_hz =
  853. clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
  854. init_waitqueue_head(&dspi->waitq);
  855. platform_set_drvdata(pdev, master);
  856. ret = spi_register_master(master);
  857. if (ret != 0) {
  858. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  859. goto out_clk_put;
  860. }
  861. return ret;
  862. out_clk_put:
  863. clk_disable_unprepare(dspi->clk);
  864. out_master_put:
  865. spi_master_put(master);
  866. return ret;
  867. }
  868. static int dspi_remove(struct platform_device *pdev)
  869. {
  870. struct spi_master *master = platform_get_drvdata(pdev);
  871. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  872. /* Disconnect from the SPI framework */
  873. dspi_release_dma(dspi);
  874. clk_disable_unprepare(dspi->clk);
  875. spi_unregister_master(dspi->master);
  876. return 0;
  877. }
  878. static struct platform_driver fsl_dspi_driver = {
  879. .driver.name = DRIVER_NAME,
  880. .driver.of_match_table = fsl_dspi_dt_ids,
  881. .driver.owner = THIS_MODULE,
  882. .driver.pm = &dspi_pm,
  883. .probe = dspi_probe,
  884. .remove = dspi_remove,
  885. };
  886. module_platform_driver(fsl_dspi_driver);
  887. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  888. MODULE_LICENSE("GPL");
  889. MODULE_ALIAS("platform:" DRIVER_NAME);