spi-fsl-dspi.c 27 KB

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