spi-fsl-dspi.c 26 KB

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