spi-fsl-dspi.c 27 KB

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