spi-fsl-dspi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  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. } else if (tx_word && (dspi->len == 1))
  498. dspi_pushr |= SPI_PUSHR_EOQ;
  499. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  500. tx_count++;
  501. }
  502. return tx_count * (tx_word + 1);
  503. }
  504. static int dspi_eoq_read(struct fsl_dspi *dspi)
  505. {
  506. int rx_count = 0;
  507. int rx_word = is_double_byte_mode(dspi);
  508. while ((dspi->rx < dspi->rx_end)
  509. && (rx_count < DSPI_FIFO_SIZE)) {
  510. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  511. rx_word = 0;
  512. dspi_data_from_popr(dspi, rx_word);
  513. rx_count++;
  514. }
  515. return rx_count;
  516. }
  517. static int dspi_tcfq_write(struct fsl_dspi *dspi)
  518. {
  519. int tx_word;
  520. u32 dspi_pushr = 0;
  521. tx_word = is_double_byte_mode(dspi);
  522. if (tx_word && (dspi->len == 1)) {
  523. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  524. regmap_update_bits(dspi->regmap, SPI_CTAR(0),
  525. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  526. tx_word = 0;
  527. }
  528. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  529. if ((dspi->cs_change) && (!dspi->len))
  530. dspi_pushr &= ~SPI_PUSHR_CONT;
  531. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  532. return tx_word + 1;
  533. }
  534. static void dspi_tcfq_read(struct fsl_dspi *dspi)
  535. {
  536. int rx_word = is_double_byte_mode(dspi);
  537. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  538. rx_word = 0;
  539. dspi_data_from_popr(dspi, rx_word);
  540. }
  541. static int dspi_transfer_one_message(struct spi_master *master,
  542. struct spi_message *message)
  543. {
  544. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  545. struct spi_device *spi = message->spi;
  546. struct spi_transfer *transfer;
  547. int status = 0;
  548. enum dspi_trans_mode trans_mode;
  549. u32 spi_tcr;
  550. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  551. dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  552. message->actual_length = 0;
  553. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  554. dspi->cur_transfer = transfer;
  555. dspi->cur_msg = message;
  556. dspi->cur_chip = spi_get_ctldata(spi);
  557. dspi->cs = spi->chip_select;
  558. dspi->cs_change = 0;
  559. if (list_is_last(&dspi->cur_transfer->transfer_list,
  560. &dspi->cur_msg->transfers) || transfer->cs_change)
  561. dspi->cs_change = 1;
  562. dspi->void_write_data = dspi->cur_chip->void_write_data;
  563. dspi->dataflags = 0;
  564. dspi->tx = (void *)transfer->tx_buf;
  565. dspi->tx_end = dspi->tx + transfer->len;
  566. dspi->rx = transfer->rx_buf;
  567. dspi->rx_end = dspi->rx + transfer->len;
  568. dspi->len = transfer->len;
  569. if (!dspi->rx)
  570. dspi->dataflags |= TRAN_STATE_RX_VOID;
  571. if (!dspi->tx)
  572. dspi->dataflags |= TRAN_STATE_TX_VOID;
  573. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  574. regmap_update_bits(dspi->regmap, SPI_MCR,
  575. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  576. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  577. regmap_write(dspi->regmap, SPI_CTAR(0),
  578. dspi->cur_chip->ctar_val);
  579. trans_mode = dspi->devtype_data->trans_mode;
  580. switch (trans_mode) {
  581. case DSPI_EOQ_MODE:
  582. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  583. dspi_eoq_write(dspi);
  584. break;
  585. case DSPI_TCFQ_MODE:
  586. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
  587. dspi_tcfq_write(dspi);
  588. break;
  589. case DSPI_DMA_MODE:
  590. regmap_write(dspi->regmap, SPI_RSER,
  591. SPI_RSER_TFFFE | SPI_RSER_TFFFD |
  592. SPI_RSER_RFDFE | SPI_RSER_RFDFD);
  593. status = dspi_dma_xfer(dspi);
  594. break;
  595. default:
  596. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  597. trans_mode);
  598. status = -EINVAL;
  599. goto out;
  600. }
  601. if (trans_mode != DSPI_DMA_MODE) {
  602. if (wait_event_interruptible(dspi->waitq,
  603. dspi->waitflags))
  604. dev_err(&dspi->pdev->dev,
  605. "wait transfer complete fail!\n");
  606. dspi->waitflags = 0;
  607. }
  608. if (transfer->delay_usecs)
  609. udelay(transfer->delay_usecs);
  610. }
  611. out:
  612. message->status = status;
  613. spi_finalize_current_message(master);
  614. return status;
  615. }
  616. static int dspi_setup(struct spi_device *spi)
  617. {
  618. struct chip_data *chip;
  619. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  620. struct fsl_dspi_platform_data *pdata;
  621. u32 cs_sck_delay = 0, sck_cs_delay = 0;
  622. unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
  623. unsigned char pasc = 0, asc = 0, fmsz = 0;
  624. unsigned long clkrate;
  625. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  626. fmsz = spi->bits_per_word - 1;
  627. } else {
  628. pr_err("Invalid wordsize\n");
  629. return -ENODEV;
  630. }
  631. /* Only alloc on first setup */
  632. chip = spi_get_ctldata(spi);
  633. if (chip == NULL) {
  634. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  635. if (!chip)
  636. return -ENOMEM;
  637. }
  638. pdata = dev_get_platdata(&dspi->pdev->dev);
  639. if (!pdata) {
  640. of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
  641. &cs_sck_delay);
  642. of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
  643. &sck_cs_delay);
  644. } else {
  645. cs_sck_delay = pdata->cs_sck_delay;
  646. sck_cs_delay = pdata->sck_cs_delay;
  647. }
  648. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  649. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  650. chip->void_write_data = 0;
  651. clkrate = clk_get_rate(dspi->clk);
  652. hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
  653. /* Set PCS to SCK delay scale values */
  654. ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
  655. /* Set After SCK delay scale values */
  656. ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
  657. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  658. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  659. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  660. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  661. | SPI_CTAR_PCSSCK(pcssck)
  662. | SPI_CTAR_CSSCK(cssck)
  663. | SPI_CTAR_PASC(pasc)
  664. | SPI_CTAR_ASC(asc)
  665. | SPI_CTAR_PBR(pbr)
  666. | SPI_CTAR_BR(br);
  667. spi_set_ctldata(spi, chip);
  668. return 0;
  669. }
  670. static void dspi_cleanup(struct spi_device *spi)
  671. {
  672. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  673. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  674. spi->master->bus_num, spi->chip_select);
  675. kfree(chip);
  676. }
  677. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  678. {
  679. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  680. struct spi_message *msg = dspi->cur_msg;
  681. enum dspi_trans_mode trans_mode;
  682. u32 spi_sr, spi_tcr;
  683. u32 spi_tcnt, tcnt_diff;
  684. int tx_word;
  685. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  686. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  687. if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
  688. tx_word = is_double_byte_mode(dspi);
  689. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  690. spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  691. /*
  692. * The width of SPI Transfer Counter in SPI_TCR is 16bits,
  693. * so the max couner is 65535. When the counter reach 65535,
  694. * it will wrap around, counter reset to zero.
  695. * spi_tcnt my be less than dspi->spi_tcnt, it means the
  696. * counter already wrapped around.
  697. * SPI Transfer Counter is a counter of transmitted frames.
  698. * The size of frame maybe two bytes.
  699. */
  700. tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
  701. % SPI_TCR_TCNT_MAX;
  702. tcnt_diff *= (tx_word + 1);
  703. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
  704. tcnt_diff--;
  705. msg->actual_length += tcnt_diff;
  706. dspi->spi_tcnt = spi_tcnt;
  707. trans_mode = dspi->devtype_data->trans_mode;
  708. switch (trans_mode) {
  709. case DSPI_EOQ_MODE:
  710. dspi_eoq_read(dspi);
  711. break;
  712. case DSPI_TCFQ_MODE:
  713. dspi_tcfq_read(dspi);
  714. break;
  715. default:
  716. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  717. trans_mode);
  718. return IRQ_HANDLED;
  719. }
  720. if (!dspi->len) {
  721. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
  722. regmap_update_bits(dspi->regmap,
  723. SPI_CTAR(0),
  724. SPI_FRAME_BITS_MASK,
  725. SPI_FRAME_BITS(16));
  726. dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
  727. }
  728. dspi->waitflags = 1;
  729. wake_up_interruptible(&dspi->waitq);
  730. } else {
  731. switch (trans_mode) {
  732. case DSPI_EOQ_MODE:
  733. dspi_eoq_write(dspi);
  734. break;
  735. case DSPI_TCFQ_MODE:
  736. dspi_tcfq_write(dspi);
  737. break;
  738. default:
  739. dev_err(&dspi->pdev->dev,
  740. "unsupported trans_mode %u\n",
  741. trans_mode);
  742. }
  743. }
  744. }
  745. return IRQ_HANDLED;
  746. }
  747. static const struct of_device_id fsl_dspi_dt_ids[] = {
  748. { .compatible = "fsl,vf610-dspi", .data = &vf610_data, },
  749. { .compatible = "fsl,ls1021a-v1.0-dspi", .data = &ls1021a_v1_data, },
  750. { .compatible = "fsl,ls2085a-dspi", .data = &ls2085a_data, },
  751. { /* sentinel */ }
  752. };
  753. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  754. #ifdef CONFIG_PM_SLEEP
  755. static int dspi_suspend(struct device *dev)
  756. {
  757. struct spi_master *master = dev_get_drvdata(dev);
  758. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  759. spi_master_suspend(master);
  760. clk_disable_unprepare(dspi->clk);
  761. pinctrl_pm_select_sleep_state(dev);
  762. return 0;
  763. }
  764. static int dspi_resume(struct device *dev)
  765. {
  766. struct spi_master *master = dev_get_drvdata(dev);
  767. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  768. int ret;
  769. pinctrl_pm_select_default_state(dev);
  770. ret = clk_prepare_enable(dspi->clk);
  771. if (ret)
  772. return ret;
  773. spi_master_resume(master);
  774. return 0;
  775. }
  776. #endif /* CONFIG_PM_SLEEP */
  777. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  778. static const struct regmap_config dspi_regmap_config = {
  779. .reg_bits = 32,
  780. .val_bits = 32,
  781. .reg_stride = 4,
  782. .max_register = 0x88,
  783. };
  784. static void dspi_init(struct fsl_dspi *dspi)
  785. {
  786. regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
  787. }
  788. static int dspi_probe(struct platform_device *pdev)
  789. {
  790. struct device_node *np = pdev->dev.of_node;
  791. struct spi_master *master;
  792. struct fsl_dspi *dspi;
  793. struct resource *res;
  794. void __iomem *base;
  795. struct fsl_dspi_platform_data *pdata;
  796. int ret = 0, cs_num, bus_num;
  797. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  798. if (!master)
  799. return -ENOMEM;
  800. dspi = spi_master_get_devdata(master);
  801. dspi->pdev = pdev;
  802. dspi->master = master;
  803. master->transfer = NULL;
  804. master->setup = dspi_setup;
  805. master->transfer_one_message = dspi_transfer_one_message;
  806. master->dev.of_node = pdev->dev.of_node;
  807. master->cleanup = dspi_cleanup;
  808. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  809. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  810. SPI_BPW_MASK(16);
  811. pdata = dev_get_platdata(&pdev->dev);
  812. if (pdata) {
  813. master->num_chipselect = pdata->cs_num;
  814. master->bus_num = pdata->bus_num;
  815. dspi->devtype_data = &coldfire_data;
  816. } else {
  817. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  818. if (ret < 0) {
  819. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  820. goto out_master_put;
  821. }
  822. master->num_chipselect = cs_num;
  823. ret = of_property_read_u32(np, "bus-num", &bus_num);
  824. if (ret < 0) {
  825. dev_err(&pdev->dev, "can't get bus-num\n");
  826. goto out_master_put;
  827. }
  828. master->bus_num = bus_num;
  829. dspi->devtype_data = of_device_get_match_data(&pdev->dev);
  830. if (!dspi->devtype_data) {
  831. dev_err(&pdev->dev, "can't get devtype_data\n");
  832. ret = -EFAULT;
  833. goto out_master_put;
  834. }
  835. }
  836. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  837. base = devm_ioremap_resource(&pdev->dev, res);
  838. if (IS_ERR(base)) {
  839. ret = PTR_ERR(base);
  840. goto out_master_put;
  841. }
  842. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  843. &dspi_regmap_config);
  844. if (IS_ERR(dspi->regmap)) {
  845. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  846. PTR_ERR(dspi->regmap));
  847. ret = PTR_ERR(dspi->regmap);
  848. goto out_master_put;
  849. }
  850. dspi_init(dspi);
  851. dspi->irq = platform_get_irq(pdev, 0);
  852. if (dspi->irq < 0) {
  853. dev_err(&pdev->dev, "can't get platform irq\n");
  854. ret = dspi->irq;
  855. goto out_master_put;
  856. }
  857. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  858. pdev->name, dspi);
  859. if (ret < 0) {
  860. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  861. goto out_master_put;
  862. }
  863. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  864. if (IS_ERR(dspi->clk)) {
  865. ret = PTR_ERR(dspi->clk);
  866. dev_err(&pdev->dev, "unable to get clock\n");
  867. goto out_master_put;
  868. }
  869. ret = clk_prepare_enable(dspi->clk);
  870. if (ret)
  871. goto out_master_put;
  872. if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
  873. ret = dspi_request_dma(dspi, res->start);
  874. if (ret < 0) {
  875. dev_err(&pdev->dev, "can't get dma channels\n");
  876. goto out_clk_put;
  877. }
  878. }
  879. master->max_speed_hz =
  880. clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
  881. init_waitqueue_head(&dspi->waitq);
  882. platform_set_drvdata(pdev, master);
  883. ret = spi_register_master(master);
  884. if (ret != 0) {
  885. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  886. goto out_clk_put;
  887. }
  888. return ret;
  889. out_clk_put:
  890. clk_disable_unprepare(dspi->clk);
  891. out_master_put:
  892. spi_master_put(master);
  893. return ret;
  894. }
  895. static int dspi_remove(struct platform_device *pdev)
  896. {
  897. struct spi_master *master = platform_get_drvdata(pdev);
  898. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  899. /* Disconnect from the SPI framework */
  900. dspi_release_dma(dspi);
  901. clk_disable_unprepare(dspi->clk);
  902. spi_unregister_master(dspi->master);
  903. return 0;
  904. }
  905. static struct platform_driver fsl_dspi_driver = {
  906. .driver.name = DRIVER_NAME,
  907. .driver.of_match_table = fsl_dspi_dt_ids,
  908. .driver.owner = THIS_MODULE,
  909. .driver.pm = &dspi_pm,
  910. .probe = dspi_probe,
  911. .remove = dspi_remove,
  912. };
  913. module_platform_driver(fsl_dspi_driver);
  914. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  915. MODULE_LICENSE("GPL");
  916. MODULE_ALIAS("platform:" DRIVER_NAME);