spi-fsl-dspi.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  1. // SPDX-License-Identifier: GPL-2.0+
  2. //
  3. // Copyright 2013 Freescale Semiconductor, Inc.
  4. // Copyright 2020 NXP
  5. //
  6. // Freescale DSPI driver
  7. // This file contains a driver for the Freescale DSPI
  8. #include <linux/clk.h>
  9. #include <linux/delay.h>
  10. #include <linux/dmaengine.h>
  11. #include <linux/dma-mapping.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of_device.h>
  16. #include <linux/pinctrl/consumer.h>
  17. #include <linux/regmap.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/spi-fsl-dspi.h>
  20. #define DRIVER_NAME "fsl-dspi"
  21. #define SPI_MCR 0x00
  22. #define SPI_MCR_MASTER BIT(31)
  23. #define SPI_MCR_PCSIS(x) ((x) << 16)
  24. #define SPI_MCR_CLR_TXF BIT(11)
  25. #define SPI_MCR_CLR_RXF BIT(10)
  26. #define SPI_MCR_XSPI BIT(3)
  27. #define SPI_MCR_DIS_TXF BIT(13)
  28. #define SPI_MCR_DIS_RXF BIT(12)
  29. #define SPI_MCR_HALT BIT(0)
  30. #define SPI_TCR 0x08
  31. #define SPI_TCR_GET_TCNT(x) (((x) & GENMASK(31, 16)) >> 16)
  32. #define SPI_CTAR(x) (0x0c + (((x) & GENMASK(1, 0)) * 4))
  33. #define SPI_CTAR_FMSZ(x) (((x) << 27) & GENMASK(30, 27))
  34. #define SPI_CTAR_CPOL BIT(26)
  35. #define SPI_CTAR_CPHA BIT(25)
  36. #define SPI_CTAR_LSBFE BIT(24)
  37. #define SPI_CTAR_PCSSCK(x) (((x) << 22) & GENMASK(23, 22))
  38. #define SPI_CTAR_PASC(x) (((x) << 20) & GENMASK(21, 20))
  39. #define SPI_CTAR_PDT(x) (((x) << 18) & GENMASK(19, 18))
  40. #define SPI_CTAR_PBR(x) (((x) << 16) & GENMASK(17, 16))
  41. #define SPI_CTAR_CSSCK(x) (((x) << 12) & GENMASK(15, 12))
  42. #define SPI_CTAR_ASC(x) (((x) << 8) & GENMASK(11, 8))
  43. #define SPI_CTAR_DT(x) (((x) << 4) & GENMASK(7, 4))
  44. #define SPI_CTAR_BR(x) ((x) & GENMASK(3, 0))
  45. #define SPI_CTAR_SCALE_BITS 0xf
  46. #define SPI_CTAR0_SLAVE 0x0c
  47. #define SPI_SR 0x2c
  48. #define SPI_SR_TCFQF BIT(31)
  49. #define SPI_SR_TFUF BIT(27)
  50. #define SPI_SR_TFFF BIT(25)
  51. #define SPI_SR_CMDTCF BIT(23)
  52. #define SPI_SR_SPEF BIT(21)
  53. #define SPI_SR_RFOF BIT(19)
  54. #define SPI_SR_TFIWF BIT(18)
  55. #define SPI_SR_RFDF BIT(17)
  56. #define SPI_SR_CMDFFF BIT(16)
  57. #define SPI_SR_CLEAR (SPI_SR_TCFQF | \
  58. SPI_SR_TFUF | SPI_SR_TFFF | \
  59. SPI_SR_CMDTCF | SPI_SR_SPEF | \
  60. SPI_SR_RFOF | SPI_SR_TFIWF | \
  61. SPI_SR_RFDF | SPI_SR_CMDFFF)
  62. #define SPI_RSER_TFFFE BIT(25)
  63. #define SPI_RSER_TFFFD BIT(24)
  64. #define SPI_RSER_RFDFE BIT(17)
  65. #define SPI_RSER_RFDFD BIT(16)
  66. #define SPI_RSER 0x30
  67. #define SPI_RSER_TCFQE BIT(31)
  68. #define SPI_RSER_CMDTCFE BIT(23)
  69. #define SPI_PUSHR 0x34
  70. #define SPI_PUSHR_CMD_CONT BIT(15)
  71. #define SPI_PUSHR_CMD_CTAS(x) (((x) << 12 & GENMASK(14, 12)))
  72. #define SPI_PUSHR_CMD_EOQ BIT(11)
  73. #define SPI_PUSHR_CMD_CTCNT BIT(10)
  74. #define SPI_PUSHR_CMD_PCS(x) (BIT(x) & GENMASK(5, 0))
  75. #define SPI_PUSHR_SLAVE 0x34
  76. #define SPI_POPR 0x38
  77. #define SPI_TXFR0 0x3c
  78. #define SPI_TXFR1 0x40
  79. #define SPI_TXFR2 0x44
  80. #define SPI_TXFR3 0x48
  81. #define SPI_RXFR0 0x7c
  82. #define SPI_RXFR1 0x80
  83. #define SPI_RXFR2 0x84
  84. #define SPI_RXFR3 0x88
  85. #define SPI_CTARE(x) (0x11c + (((x) & GENMASK(1, 0)) * 4))
  86. #define SPI_CTARE_FMSZE(x) (((x) & 0x1) << 16)
  87. #define SPI_CTARE_DTCP(x) ((x) & 0x7ff)
  88. #define SPI_SREX 0x13c
  89. #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
  90. #define SPI_FRAME_EBITS(bits) SPI_CTARE_FMSZE(((bits) - 1) >> 4)
  91. #define DMA_COMPLETION_TIMEOUT msecs_to_jiffies(3000)
  92. struct chip_data {
  93. u32 ctar_val;
  94. };
  95. enum dspi_trans_mode {
  96. DSPI_XSPI_MODE,
  97. DSPI_DMA_MODE,
  98. };
  99. struct fsl_dspi_devtype_data {
  100. enum dspi_trans_mode trans_mode;
  101. u8 max_clock_factor;
  102. int fifo_size;
  103. };
  104. enum {
  105. LS1021A,
  106. LS1012A,
  107. LS1028A,
  108. LS1043A,
  109. LS1046A,
  110. LS2080A,
  111. LS2085A,
  112. LX2160A,
  113. MCF5441X,
  114. VF610,
  115. };
  116. static const struct fsl_dspi_devtype_data devtype_data[] = {
  117. [VF610] = {
  118. .trans_mode = DSPI_DMA_MODE,
  119. .max_clock_factor = 2,
  120. .fifo_size = 4,
  121. },
  122. [LS1021A] = {
  123. /* Has A-011218 DMA erratum */
  124. .trans_mode = DSPI_XSPI_MODE,
  125. .max_clock_factor = 8,
  126. .fifo_size = 4,
  127. },
  128. [LS1012A] = {
  129. /* Has A-011218 DMA erratum */
  130. .trans_mode = DSPI_XSPI_MODE,
  131. .max_clock_factor = 8,
  132. .fifo_size = 16,
  133. },
  134. [LS1028A] = {
  135. .trans_mode = DSPI_XSPI_MODE,
  136. .max_clock_factor = 8,
  137. .fifo_size = 4,
  138. },
  139. [LS1043A] = {
  140. /* Has A-011218 DMA erratum */
  141. .trans_mode = DSPI_XSPI_MODE,
  142. .max_clock_factor = 8,
  143. .fifo_size = 16,
  144. },
  145. [LS1046A] = {
  146. /* Has A-011218 DMA erratum */
  147. .trans_mode = DSPI_XSPI_MODE,
  148. .max_clock_factor = 8,
  149. .fifo_size = 16,
  150. },
  151. [LS2080A] = {
  152. .trans_mode = DSPI_XSPI_MODE,
  153. .max_clock_factor = 8,
  154. .fifo_size = 4,
  155. },
  156. [LS2085A] = {
  157. .trans_mode = DSPI_XSPI_MODE,
  158. .max_clock_factor = 8,
  159. .fifo_size = 4,
  160. },
  161. [LX2160A] = {
  162. .trans_mode = DSPI_XSPI_MODE,
  163. .max_clock_factor = 8,
  164. .fifo_size = 4,
  165. },
  166. [MCF5441X] = {
  167. .trans_mode = DSPI_DMA_MODE,
  168. .max_clock_factor = 8,
  169. .fifo_size = 16,
  170. },
  171. };
  172. struct fsl_dspi_dma {
  173. u32 *tx_dma_buf;
  174. struct dma_chan *chan_tx;
  175. dma_addr_t tx_dma_phys;
  176. struct completion cmd_tx_complete;
  177. struct dma_async_tx_descriptor *tx_desc;
  178. u32 *rx_dma_buf;
  179. struct dma_chan *chan_rx;
  180. dma_addr_t rx_dma_phys;
  181. struct completion cmd_rx_complete;
  182. struct dma_async_tx_descriptor *rx_desc;
  183. };
  184. struct fsl_dspi {
  185. struct spi_controller *ctlr;
  186. struct platform_device *pdev;
  187. struct regmap *regmap;
  188. struct regmap *regmap_pushr;
  189. int irq;
  190. struct clk *clk;
  191. struct spi_transfer *cur_transfer;
  192. struct spi_message *cur_msg;
  193. struct chip_data *cur_chip;
  194. size_t progress;
  195. size_t len;
  196. const void *tx;
  197. void *rx;
  198. u16 tx_cmd;
  199. const struct fsl_dspi_devtype_data *devtype_data;
  200. struct completion xfer_done;
  201. struct fsl_dspi_dma *dma;
  202. int oper_word_size;
  203. int oper_bits_per_word;
  204. int words_in_flight;
  205. /*
  206. * Offsets for CMD and TXDATA within SPI_PUSHR when accessed
  207. * individually (in XSPI mode)
  208. */
  209. int pushr_cmd;
  210. int pushr_tx;
  211. void (*host_to_dev)(struct fsl_dspi *dspi, u32 *txdata);
  212. void (*dev_to_host)(struct fsl_dspi *dspi, u32 rxdata);
  213. };
  214. static void dspi_native_host_to_dev(struct fsl_dspi *dspi, u32 *txdata)
  215. {
  216. switch (dspi->oper_word_size) {
  217. case 1:
  218. *txdata = *(u8 *)dspi->tx;
  219. break;
  220. case 2:
  221. *txdata = *(u16 *)dspi->tx;
  222. break;
  223. case 4:
  224. *txdata = *(u32 *)dspi->tx;
  225. break;
  226. }
  227. dspi->tx += dspi->oper_word_size;
  228. }
  229. static void dspi_native_dev_to_host(struct fsl_dspi *dspi, u32 rxdata)
  230. {
  231. switch (dspi->oper_word_size) {
  232. case 1:
  233. *(u8 *)dspi->rx = rxdata;
  234. break;
  235. case 2:
  236. *(u16 *)dspi->rx = rxdata;
  237. break;
  238. case 4:
  239. *(u32 *)dspi->rx = rxdata;
  240. break;
  241. }
  242. dspi->rx += dspi->oper_word_size;
  243. }
  244. static void dspi_8on32_host_to_dev(struct fsl_dspi *dspi, u32 *txdata)
  245. {
  246. *txdata = cpu_to_be32(*(u32 *)dspi->tx);
  247. dspi->tx += sizeof(u32);
  248. }
  249. static void dspi_8on32_dev_to_host(struct fsl_dspi *dspi, u32 rxdata)
  250. {
  251. *(u32 *)dspi->rx = be32_to_cpu(rxdata);
  252. dspi->rx += sizeof(u32);
  253. }
  254. static void dspi_8on16_host_to_dev(struct fsl_dspi *dspi, u32 *txdata)
  255. {
  256. *txdata = cpu_to_be16(*(u16 *)dspi->tx);
  257. dspi->tx += sizeof(u16);
  258. }
  259. static void dspi_8on16_dev_to_host(struct fsl_dspi *dspi, u32 rxdata)
  260. {
  261. *(u16 *)dspi->rx = be16_to_cpu(rxdata);
  262. dspi->rx += sizeof(u16);
  263. }
  264. static void dspi_16on32_host_to_dev(struct fsl_dspi *dspi, u32 *txdata)
  265. {
  266. u16 hi = *(u16 *)dspi->tx;
  267. u16 lo = *(u16 *)(dspi->tx + 2);
  268. *txdata = (u32)hi << 16 | lo;
  269. dspi->tx += sizeof(u32);
  270. }
  271. static void dspi_16on32_dev_to_host(struct fsl_dspi *dspi, u32 rxdata)
  272. {
  273. u16 hi = rxdata & 0xffff;
  274. u16 lo = rxdata >> 16;
  275. *(u16 *)dspi->rx = lo;
  276. *(u16 *)(dspi->rx + 2) = hi;
  277. dspi->rx += sizeof(u32);
  278. }
  279. /*
  280. * Pop one word from the TX buffer for pushing into the
  281. * PUSHR register (TX FIFO)
  282. */
  283. static u32 dspi_pop_tx(struct fsl_dspi *dspi)
  284. {
  285. u32 txdata = 0;
  286. if (dspi->tx)
  287. dspi->host_to_dev(dspi, &txdata);
  288. dspi->len -= dspi->oper_word_size;
  289. return txdata;
  290. }
  291. /* Prepare one TX FIFO entry (txdata plus cmd) */
  292. static u32 dspi_pop_tx_pushr(struct fsl_dspi *dspi)
  293. {
  294. u16 cmd = dspi->tx_cmd, data = dspi_pop_tx(dspi);
  295. if (spi_controller_is_slave(dspi->ctlr))
  296. return data;
  297. if (dspi->len > 0)
  298. cmd |= SPI_PUSHR_CMD_CONT;
  299. return cmd << 16 | data;
  300. }
  301. /* Push one word to the RX buffer from the POPR register (RX FIFO) */
  302. static void dspi_push_rx(struct fsl_dspi *dspi, u32 rxdata)
  303. {
  304. if (!dspi->rx)
  305. return;
  306. dspi->dev_to_host(dspi, rxdata);
  307. }
  308. static void dspi_tx_dma_callback(void *arg)
  309. {
  310. struct fsl_dspi *dspi = arg;
  311. struct fsl_dspi_dma *dma = dspi->dma;
  312. complete(&dma->cmd_tx_complete);
  313. }
  314. static void dspi_rx_dma_callback(void *arg)
  315. {
  316. struct fsl_dspi *dspi = arg;
  317. struct fsl_dspi_dma *dma = dspi->dma;
  318. int i;
  319. if (dspi->rx) {
  320. for (i = 0; i < dspi->words_in_flight; i++)
  321. dspi_push_rx(dspi, dspi->dma->rx_dma_buf[i]);
  322. }
  323. complete(&dma->cmd_rx_complete);
  324. }
  325. static int dspi_next_xfer_dma_submit(struct fsl_dspi *dspi)
  326. {
  327. struct device *dev = &dspi->pdev->dev;
  328. struct fsl_dspi_dma *dma = dspi->dma;
  329. int time_left;
  330. int i;
  331. for (i = 0; i < dspi->words_in_flight; i++)
  332. dspi->dma->tx_dma_buf[i] = dspi_pop_tx_pushr(dspi);
  333. dma->tx_desc = dmaengine_prep_slave_single(dma->chan_tx,
  334. dma->tx_dma_phys,
  335. dspi->words_in_flight *
  336. DMA_SLAVE_BUSWIDTH_4_BYTES,
  337. DMA_MEM_TO_DEV,
  338. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  339. if (!dma->tx_desc) {
  340. dev_err(dev, "Not able to get desc for DMA xfer\n");
  341. return -EIO;
  342. }
  343. dma->tx_desc->callback = dspi_tx_dma_callback;
  344. dma->tx_desc->callback_param = dspi;
  345. if (dma_submit_error(dmaengine_submit(dma->tx_desc))) {
  346. dev_err(dev, "DMA submit failed\n");
  347. return -EINVAL;
  348. }
  349. dma->rx_desc = dmaengine_prep_slave_single(dma->chan_rx,
  350. dma->rx_dma_phys,
  351. dspi->words_in_flight *
  352. DMA_SLAVE_BUSWIDTH_4_BYTES,
  353. DMA_DEV_TO_MEM,
  354. DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
  355. if (!dma->rx_desc) {
  356. dev_err(dev, "Not able to get desc for DMA xfer\n");
  357. return -EIO;
  358. }
  359. dma->rx_desc->callback = dspi_rx_dma_callback;
  360. dma->rx_desc->callback_param = dspi;
  361. if (dma_submit_error(dmaengine_submit(dma->rx_desc))) {
  362. dev_err(dev, "DMA submit failed\n");
  363. return -EINVAL;
  364. }
  365. reinit_completion(&dspi->dma->cmd_rx_complete);
  366. reinit_completion(&dspi->dma->cmd_tx_complete);
  367. dma_async_issue_pending(dma->chan_rx);
  368. dma_async_issue_pending(dma->chan_tx);
  369. if (spi_controller_is_slave(dspi->ctlr)) {
  370. wait_for_completion_interruptible(&dspi->dma->cmd_rx_complete);
  371. return 0;
  372. }
  373. time_left = wait_for_completion_timeout(&dspi->dma->cmd_tx_complete,
  374. DMA_COMPLETION_TIMEOUT);
  375. if (time_left == 0) {
  376. dev_err(dev, "DMA tx timeout\n");
  377. dmaengine_terminate_all(dma->chan_tx);
  378. dmaengine_terminate_all(dma->chan_rx);
  379. return -ETIMEDOUT;
  380. }
  381. time_left = wait_for_completion_timeout(&dspi->dma->cmd_rx_complete,
  382. DMA_COMPLETION_TIMEOUT);
  383. if (time_left == 0) {
  384. dev_err(dev, "DMA rx timeout\n");
  385. dmaengine_terminate_all(dma->chan_tx);
  386. dmaengine_terminate_all(dma->chan_rx);
  387. return -ETIMEDOUT;
  388. }
  389. return 0;
  390. }
  391. static void dspi_setup_accel(struct fsl_dspi *dspi);
  392. static int dspi_dma_xfer(struct fsl_dspi *dspi)
  393. {
  394. struct spi_message *message = dspi->cur_msg;
  395. struct device *dev = &dspi->pdev->dev;
  396. int ret = 0;
  397. /*
  398. * dspi->len gets decremented by dspi_pop_tx_pushr in
  399. * dspi_next_xfer_dma_submit
  400. */
  401. while (dspi->len) {
  402. /* Figure out operational bits-per-word for this chunk */
  403. dspi_setup_accel(dspi);
  404. dspi->words_in_flight = dspi->len / dspi->oper_word_size;
  405. if (dspi->words_in_flight > dspi->devtype_data->fifo_size)
  406. dspi->words_in_flight = dspi->devtype_data->fifo_size;
  407. message->actual_length += dspi->words_in_flight *
  408. dspi->oper_word_size;
  409. ret = dspi_next_xfer_dma_submit(dspi);
  410. if (ret) {
  411. dev_err(dev, "DMA transfer failed\n");
  412. break;
  413. }
  414. }
  415. return ret;
  416. }
  417. static int dspi_request_dma(struct fsl_dspi *dspi, phys_addr_t phy_addr)
  418. {
  419. int dma_bufsize = dspi->devtype_data->fifo_size * 2;
  420. struct device *dev = &dspi->pdev->dev;
  421. struct dma_slave_config cfg;
  422. struct fsl_dspi_dma *dma;
  423. int ret;
  424. dma = devm_kzalloc(dev, sizeof(*dma), GFP_KERNEL);
  425. if (!dma)
  426. return -ENOMEM;
  427. dma->chan_rx = dma_request_chan(dev, "rx");
  428. if (IS_ERR(dma->chan_rx)) {
  429. dev_err(dev, "rx dma channel not available\n");
  430. ret = PTR_ERR(dma->chan_rx);
  431. return ret;
  432. }
  433. dma->chan_tx = dma_request_chan(dev, "tx");
  434. if (IS_ERR(dma->chan_tx)) {
  435. dev_err(dev, "tx dma channel not available\n");
  436. ret = PTR_ERR(dma->chan_tx);
  437. goto err_tx_channel;
  438. }
  439. dma->tx_dma_buf = dma_alloc_coherent(dma->chan_tx->device->dev,
  440. dma_bufsize, &dma->tx_dma_phys,
  441. GFP_KERNEL);
  442. if (!dma->tx_dma_buf) {
  443. ret = -ENOMEM;
  444. goto err_tx_dma_buf;
  445. }
  446. dma->rx_dma_buf = dma_alloc_coherent(dma->chan_rx->device->dev,
  447. dma_bufsize, &dma->rx_dma_phys,
  448. GFP_KERNEL);
  449. if (!dma->rx_dma_buf) {
  450. ret = -ENOMEM;
  451. goto err_rx_dma_buf;
  452. }
  453. memset(&cfg, 0, sizeof(cfg));
  454. cfg.src_addr = phy_addr + SPI_POPR;
  455. cfg.dst_addr = phy_addr + SPI_PUSHR;
  456. cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  457. cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
  458. cfg.src_maxburst = 1;
  459. cfg.dst_maxburst = 1;
  460. cfg.direction = DMA_DEV_TO_MEM;
  461. ret = dmaengine_slave_config(dma->chan_rx, &cfg);
  462. if (ret) {
  463. dev_err(dev, "can't configure rx dma channel\n");
  464. ret = -EINVAL;
  465. goto err_slave_config;
  466. }
  467. cfg.direction = DMA_MEM_TO_DEV;
  468. ret = dmaengine_slave_config(dma->chan_tx, &cfg);
  469. if (ret) {
  470. dev_err(dev, "can't configure tx dma channel\n");
  471. ret = -EINVAL;
  472. goto err_slave_config;
  473. }
  474. dspi->dma = dma;
  475. init_completion(&dma->cmd_tx_complete);
  476. init_completion(&dma->cmd_rx_complete);
  477. return 0;
  478. err_slave_config:
  479. dma_free_coherent(dma->chan_rx->device->dev,
  480. dma_bufsize, dma->rx_dma_buf, dma->rx_dma_phys);
  481. err_rx_dma_buf:
  482. dma_free_coherent(dma->chan_tx->device->dev,
  483. dma_bufsize, dma->tx_dma_buf, dma->tx_dma_phys);
  484. err_tx_dma_buf:
  485. dma_release_channel(dma->chan_tx);
  486. err_tx_channel:
  487. dma_release_channel(dma->chan_rx);
  488. devm_kfree(dev, dma);
  489. dspi->dma = NULL;
  490. return ret;
  491. }
  492. static void dspi_release_dma(struct fsl_dspi *dspi)
  493. {
  494. int dma_bufsize = dspi->devtype_data->fifo_size * 2;
  495. struct fsl_dspi_dma *dma = dspi->dma;
  496. if (!dma)
  497. return;
  498. if (dma->chan_tx) {
  499. dma_free_coherent(dma->chan_tx->device->dev, dma_bufsize,
  500. dma->tx_dma_buf, dma->tx_dma_phys);
  501. dma_release_channel(dma->chan_tx);
  502. }
  503. if (dma->chan_rx) {
  504. dma_free_coherent(dma->chan_rx->device->dev, dma_bufsize,
  505. dma->rx_dma_buf, dma->rx_dma_phys);
  506. dma_release_channel(dma->chan_rx);
  507. }
  508. }
  509. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  510. unsigned long clkrate)
  511. {
  512. /* Valid baud rate pre-scaler values */
  513. int pbr_tbl[4] = {2, 3, 5, 7};
  514. int brs[16] = { 2, 4, 6, 8,
  515. 16, 32, 64, 128,
  516. 256, 512, 1024, 2048,
  517. 4096, 8192, 16384, 32768 };
  518. int scale_needed, scale, minscale = INT_MAX;
  519. int i, j;
  520. scale_needed = clkrate / speed_hz;
  521. if (clkrate % speed_hz)
  522. scale_needed++;
  523. for (i = 0; i < ARRAY_SIZE(brs); i++)
  524. for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
  525. scale = brs[i] * pbr_tbl[j];
  526. if (scale >= scale_needed) {
  527. if (scale < minscale) {
  528. minscale = scale;
  529. *br = i;
  530. *pbr = j;
  531. }
  532. break;
  533. }
  534. }
  535. if (minscale == INT_MAX) {
  536. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
  537. speed_hz, clkrate);
  538. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  539. *br = ARRAY_SIZE(brs) - 1;
  540. }
  541. }
  542. static void ns_delay_scale(char *psc, char *sc, int delay_ns,
  543. unsigned long clkrate)
  544. {
  545. int scale_needed, scale, minscale = INT_MAX;
  546. int pscale_tbl[4] = {1, 3, 5, 7};
  547. u32 remainder;
  548. int i, j;
  549. scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
  550. &remainder);
  551. if (remainder)
  552. scale_needed++;
  553. for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
  554. for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
  555. scale = pscale_tbl[i] * (2 << j);
  556. if (scale >= scale_needed) {
  557. if (scale < minscale) {
  558. minscale = scale;
  559. *psc = i;
  560. *sc = j;
  561. }
  562. break;
  563. }
  564. }
  565. if (minscale == INT_MAX) {
  566. pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
  567. delay_ns, clkrate);
  568. *psc = ARRAY_SIZE(pscale_tbl) - 1;
  569. *sc = SPI_CTAR_SCALE_BITS;
  570. }
  571. }
  572. static void dspi_pushr_cmd_write(struct fsl_dspi *dspi, u16 cmd)
  573. {
  574. /*
  575. * The only time when the PCS doesn't need continuation after this word
  576. * is when it's last. We need to look ahead, because we actually call
  577. * dspi_pop_tx (the function that decrements dspi->len) _after_
  578. * dspi_pushr_cmd_write with XSPI mode. As for how much in advance? One
  579. * word is enough. If there's more to transmit than that,
  580. * dspi_xspi_write will know to split the FIFO writes in 2, and
  581. * generate a new PUSHR command with the final word that will have PCS
  582. * deasserted (not continued) here.
  583. */
  584. if (dspi->len > dspi->oper_word_size)
  585. cmd |= SPI_PUSHR_CMD_CONT;
  586. regmap_write(dspi->regmap_pushr, dspi->pushr_cmd, cmd);
  587. }
  588. static void dspi_pushr_txdata_write(struct fsl_dspi *dspi, u16 txdata)
  589. {
  590. regmap_write(dspi->regmap_pushr, dspi->pushr_tx, txdata);
  591. }
  592. static void dspi_xspi_fifo_write(struct fsl_dspi *dspi, int num_words)
  593. {
  594. int num_bytes = num_words * dspi->oper_word_size;
  595. u16 tx_cmd = dspi->tx_cmd;
  596. /*
  597. * If the PCS needs to de-assert (i.e. we're at the end of the buffer
  598. * and cs_change does not want the PCS to stay on), then we need a new
  599. * PUSHR command, since this one (for the body of the buffer)
  600. * necessarily has the CONT bit set.
  601. * So send one word less during this go, to force a split and a command
  602. * with a single word next time, when CONT will be unset.
  603. */
  604. if (!(dspi->tx_cmd & SPI_PUSHR_CMD_CONT) && num_bytes == dspi->len)
  605. tx_cmd |= SPI_PUSHR_CMD_EOQ;
  606. /* Update CTARE */
  607. regmap_write(dspi->regmap, SPI_CTARE(0),
  608. SPI_FRAME_EBITS(dspi->oper_bits_per_word) |
  609. SPI_CTARE_DTCP(num_words));
  610. /*
  611. * Write the CMD FIFO entry first, and then the two
  612. * corresponding TX FIFO entries (or one...).
  613. */
  614. dspi_pushr_cmd_write(dspi, tx_cmd);
  615. /* Fill TX FIFO with as many transfers as possible */
  616. while (num_words--) {
  617. u32 data = dspi_pop_tx(dspi);
  618. dspi_pushr_txdata_write(dspi, data & 0xFFFF);
  619. if (dspi->oper_bits_per_word > 16)
  620. dspi_pushr_txdata_write(dspi, data >> 16);
  621. }
  622. }
  623. static u32 dspi_popr_read(struct fsl_dspi *dspi)
  624. {
  625. u32 rxdata = 0;
  626. regmap_read(dspi->regmap, SPI_POPR, &rxdata);
  627. return rxdata;
  628. }
  629. static void dspi_fifo_read(struct fsl_dspi *dspi)
  630. {
  631. int num_fifo_entries = dspi->words_in_flight;
  632. /* Read one FIFO entry and push to rx buffer */
  633. while (num_fifo_entries--)
  634. dspi_push_rx(dspi, dspi_popr_read(dspi));
  635. }
  636. static void dspi_setup_accel(struct fsl_dspi *dspi)
  637. {
  638. struct spi_transfer *xfer = dspi->cur_transfer;
  639. bool odd = !!(dspi->len & 1);
  640. /* No accel for frames not multiple of 8 bits at the moment */
  641. if (xfer->bits_per_word % 8)
  642. goto no_accel;
  643. if (!odd && dspi->len <= dspi->devtype_data->fifo_size * 2) {
  644. dspi->oper_bits_per_word = 16;
  645. } else if (odd && dspi->len <= dspi->devtype_data->fifo_size) {
  646. dspi->oper_bits_per_word = 8;
  647. } else {
  648. /* Start off with maximum supported by hardware */
  649. if (dspi->devtype_data->trans_mode == DSPI_XSPI_MODE)
  650. dspi->oper_bits_per_word = 32;
  651. else
  652. dspi->oper_bits_per_word = 16;
  653. /*
  654. * And go down only if the buffer can't be sent with
  655. * words this big
  656. */
  657. do {
  658. if (dspi->len >= DIV_ROUND_UP(dspi->oper_bits_per_word, 8))
  659. break;
  660. dspi->oper_bits_per_word /= 2;
  661. } while (dspi->oper_bits_per_word > 8);
  662. }
  663. if (xfer->bits_per_word == 8 && dspi->oper_bits_per_word == 32) {
  664. dspi->dev_to_host = dspi_8on32_dev_to_host;
  665. dspi->host_to_dev = dspi_8on32_host_to_dev;
  666. } else if (xfer->bits_per_word == 8 && dspi->oper_bits_per_word == 16) {
  667. dspi->dev_to_host = dspi_8on16_dev_to_host;
  668. dspi->host_to_dev = dspi_8on16_host_to_dev;
  669. } else if (xfer->bits_per_word == 16 && dspi->oper_bits_per_word == 32) {
  670. dspi->dev_to_host = dspi_16on32_dev_to_host;
  671. dspi->host_to_dev = dspi_16on32_host_to_dev;
  672. } else {
  673. no_accel:
  674. dspi->dev_to_host = dspi_native_dev_to_host;
  675. dspi->host_to_dev = dspi_native_host_to_dev;
  676. dspi->oper_bits_per_word = xfer->bits_per_word;
  677. }
  678. dspi->oper_word_size = DIV_ROUND_UP(dspi->oper_bits_per_word, 8);
  679. /*
  680. * Update CTAR here (code is common for XSPI and DMA modes).
  681. * We will update CTARE in the portion specific to XSPI, when we
  682. * also know the preload value (DTCP).
  683. */
  684. regmap_write(dspi->regmap, SPI_CTAR(0),
  685. dspi->cur_chip->ctar_val |
  686. SPI_FRAME_BITS(dspi->oper_bits_per_word));
  687. }
  688. static void dspi_fifo_write(struct fsl_dspi *dspi)
  689. {
  690. int num_fifo_entries = dspi->devtype_data->fifo_size;
  691. struct spi_transfer *xfer = dspi->cur_transfer;
  692. struct spi_message *msg = dspi->cur_msg;
  693. int num_words, num_bytes;
  694. dspi_setup_accel(dspi);
  695. /* In XSPI mode each 32-bit word occupies 2 TX FIFO entries */
  696. if (dspi->oper_word_size == 4)
  697. num_fifo_entries /= 2;
  698. /*
  699. * Integer division intentionally trims off odd (or non-multiple of 4)
  700. * numbers of bytes at the end of the buffer, which will be sent next
  701. * time using a smaller oper_word_size.
  702. */
  703. num_words = dspi->len / dspi->oper_word_size;
  704. if (num_words > num_fifo_entries)
  705. num_words = num_fifo_entries;
  706. /* Update total number of bytes that were transferred */
  707. num_bytes = num_words * dspi->oper_word_size;
  708. msg->actual_length += num_bytes;
  709. dspi->progress += num_bytes / DIV_ROUND_UP(xfer->bits_per_word, 8);
  710. /*
  711. * Update shared variable for use in the next interrupt (both in
  712. * dspi_fifo_read and in dspi_fifo_write).
  713. */
  714. dspi->words_in_flight = num_words;
  715. spi_take_timestamp_pre(dspi->ctlr, xfer, dspi->progress, !dspi->irq);
  716. dspi_xspi_fifo_write(dspi, num_words);
  717. /*
  718. * Everything after this point is in a potential race with the next
  719. * interrupt, so we must never use dspi->words_in_flight again since it
  720. * might already be modified by the next dspi_fifo_write.
  721. */
  722. spi_take_timestamp_post(dspi->ctlr, dspi->cur_transfer,
  723. dspi->progress, !dspi->irq);
  724. }
  725. static int dspi_rxtx(struct fsl_dspi *dspi)
  726. {
  727. dspi_fifo_read(dspi);
  728. if (!dspi->len)
  729. /* Success! */
  730. return 0;
  731. dspi_fifo_write(dspi);
  732. return -EINPROGRESS;
  733. }
  734. static int dspi_poll(struct fsl_dspi *dspi)
  735. {
  736. int tries = 1000;
  737. u32 spi_sr;
  738. do {
  739. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  740. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  741. if (spi_sr & SPI_SR_CMDTCF)
  742. break;
  743. } while (--tries);
  744. if (!tries)
  745. return -ETIMEDOUT;
  746. return dspi_rxtx(dspi);
  747. }
  748. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  749. {
  750. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  751. u32 spi_sr;
  752. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  753. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  754. if (!(spi_sr & SPI_SR_CMDTCF))
  755. return IRQ_NONE;
  756. if (dspi_rxtx(dspi) == 0)
  757. complete(&dspi->xfer_done);
  758. return IRQ_HANDLED;
  759. }
  760. static int dspi_transfer_one_message(struct spi_controller *ctlr,
  761. struct spi_message *message)
  762. {
  763. struct fsl_dspi *dspi = spi_controller_get_devdata(ctlr);
  764. struct spi_device *spi = message->spi;
  765. struct spi_transfer *transfer;
  766. int status = 0;
  767. message->actual_length = 0;
  768. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  769. dspi->cur_transfer = transfer;
  770. dspi->cur_msg = message;
  771. dspi->cur_chip = spi_get_ctldata(spi);
  772. /* Prepare command word for CMD FIFO */
  773. dspi->tx_cmd = SPI_PUSHR_CMD_CTAS(0) |
  774. SPI_PUSHR_CMD_PCS(spi->chip_select);
  775. if (list_is_last(&dspi->cur_transfer->transfer_list,
  776. &dspi->cur_msg->transfers)) {
  777. /* Leave PCS activated after last transfer when
  778. * cs_change is set.
  779. */
  780. if (transfer->cs_change)
  781. dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
  782. } else {
  783. /* Keep PCS active between transfers in same message
  784. * when cs_change is not set, and de-activate PCS
  785. * between transfers in the same message when
  786. * cs_change is set.
  787. */
  788. if (!transfer->cs_change)
  789. dspi->tx_cmd |= SPI_PUSHR_CMD_CONT;
  790. }
  791. dspi->tx = transfer->tx_buf;
  792. dspi->rx = transfer->rx_buf;
  793. dspi->len = transfer->len;
  794. dspi->progress = 0;
  795. regmap_update_bits(dspi->regmap, SPI_MCR,
  796. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  797. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  798. spi_take_timestamp_pre(dspi->ctlr, dspi->cur_transfer,
  799. dspi->progress, !dspi->irq);
  800. if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
  801. status = dspi_dma_xfer(dspi);
  802. } else {
  803. dspi_fifo_write(dspi);
  804. if (dspi->irq) {
  805. wait_for_completion(&dspi->xfer_done);
  806. reinit_completion(&dspi->xfer_done);
  807. } else {
  808. do {
  809. status = dspi_poll(dspi);
  810. } while (status == -EINPROGRESS);
  811. }
  812. }
  813. if (status)
  814. break;
  815. spi_transfer_delay_exec(transfer);
  816. }
  817. message->status = status;
  818. spi_finalize_current_message(ctlr);
  819. return status;
  820. }
  821. static int dspi_setup(struct spi_device *spi)
  822. {
  823. struct fsl_dspi *dspi = spi_controller_get_devdata(spi->controller);
  824. u32 period_ns = DIV_ROUND_UP(NSEC_PER_SEC, spi->max_speed_hz);
  825. unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
  826. u32 quarter_period_ns = DIV_ROUND_UP(period_ns, 4);
  827. u32 cs_sck_delay = 0, sck_cs_delay = 0;
  828. struct fsl_dspi_platform_data *pdata;
  829. unsigned char pasc = 0, asc = 0;
  830. struct chip_data *chip;
  831. unsigned long clkrate;
  832. /* Only alloc on first setup */
  833. chip = spi_get_ctldata(spi);
  834. if (chip == NULL) {
  835. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  836. if (!chip)
  837. return -ENOMEM;
  838. }
  839. pdata = dev_get_platdata(&dspi->pdev->dev);
  840. if (!pdata) {
  841. of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
  842. &cs_sck_delay);
  843. of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
  844. &sck_cs_delay);
  845. } else {
  846. cs_sck_delay = pdata->cs_sck_delay;
  847. sck_cs_delay = pdata->sck_cs_delay;
  848. }
  849. /* Since tCSC and tASC apply to continuous transfers too, avoid SCK
  850. * glitches of half a cycle by never allowing tCSC + tASC to go below
  851. * half a SCK period.
  852. */
  853. if (cs_sck_delay < quarter_period_ns)
  854. cs_sck_delay = quarter_period_ns;
  855. if (sck_cs_delay < quarter_period_ns)
  856. sck_cs_delay = quarter_period_ns;
  857. dev_dbg(&spi->dev,
  858. "DSPI controller timing params: CS-to-SCK delay %u ns, SCK-to-CS delay %u ns\n",
  859. cs_sck_delay, sck_cs_delay);
  860. clkrate = clk_get_rate(dspi->clk);
  861. hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
  862. /* Set PCS to SCK delay scale values */
  863. ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
  864. /* Set After SCK delay scale values */
  865. ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
  866. chip->ctar_val = 0;
  867. if (spi->mode & SPI_CPOL)
  868. chip->ctar_val |= SPI_CTAR_CPOL;
  869. if (spi->mode & SPI_CPHA)
  870. chip->ctar_val |= SPI_CTAR_CPHA;
  871. if (!spi_controller_is_slave(dspi->ctlr)) {
  872. chip->ctar_val |= SPI_CTAR_PCSSCK(pcssck) |
  873. SPI_CTAR_CSSCK(cssck) |
  874. SPI_CTAR_PASC(pasc) |
  875. SPI_CTAR_ASC(asc) |
  876. SPI_CTAR_PBR(pbr) |
  877. SPI_CTAR_BR(br);
  878. if (spi->mode & SPI_LSB_FIRST)
  879. chip->ctar_val |= SPI_CTAR_LSBFE;
  880. }
  881. spi_set_ctldata(spi, chip);
  882. return 0;
  883. }
  884. static void dspi_cleanup(struct spi_device *spi)
  885. {
  886. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  887. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  888. spi->controller->bus_num, spi->chip_select);
  889. kfree(chip);
  890. }
  891. static const struct of_device_id fsl_dspi_dt_ids[] = {
  892. {
  893. .compatible = "fsl,vf610-dspi",
  894. .data = &devtype_data[VF610],
  895. }, {
  896. .compatible = "fsl,ls1021a-v1.0-dspi",
  897. .data = &devtype_data[LS1021A],
  898. }, {
  899. .compatible = "fsl,ls1012a-dspi",
  900. .data = &devtype_data[LS1012A],
  901. }, {
  902. .compatible = "fsl,ls1028a-dspi",
  903. .data = &devtype_data[LS1028A],
  904. }, {
  905. .compatible = "fsl,ls1043a-dspi",
  906. .data = &devtype_data[LS1043A],
  907. }, {
  908. .compatible = "fsl,ls1046a-dspi",
  909. .data = &devtype_data[LS1046A],
  910. }, {
  911. .compatible = "fsl,ls2080a-dspi",
  912. .data = &devtype_data[LS2080A],
  913. }, {
  914. .compatible = "fsl,ls2085a-dspi",
  915. .data = &devtype_data[LS2085A],
  916. }, {
  917. .compatible = "fsl,lx2160a-dspi",
  918. .data = &devtype_data[LX2160A],
  919. },
  920. { /* sentinel */ }
  921. };
  922. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  923. #ifdef CONFIG_PM_SLEEP
  924. static int dspi_suspend(struct device *dev)
  925. {
  926. struct fsl_dspi *dspi = dev_get_drvdata(dev);
  927. if (dspi->irq)
  928. disable_irq(dspi->irq);
  929. spi_controller_suspend(dspi->ctlr);
  930. clk_disable_unprepare(dspi->clk);
  931. pinctrl_pm_select_sleep_state(dev);
  932. return 0;
  933. }
  934. static int dspi_resume(struct device *dev)
  935. {
  936. struct fsl_dspi *dspi = dev_get_drvdata(dev);
  937. int ret;
  938. pinctrl_pm_select_default_state(dev);
  939. ret = clk_prepare_enable(dspi->clk);
  940. if (ret)
  941. return ret;
  942. spi_controller_resume(dspi->ctlr);
  943. if (dspi->irq)
  944. enable_irq(dspi->irq);
  945. return 0;
  946. }
  947. #endif /* CONFIG_PM_SLEEP */
  948. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  949. static const struct regmap_range dspi_volatile_ranges[] = {
  950. regmap_reg_range(SPI_MCR, SPI_TCR),
  951. regmap_reg_range(SPI_SR, SPI_SR),
  952. regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
  953. };
  954. static const struct regmap_access_table dspi_volatile_table = {
  955. .yes_ranges = dspi_volatile_ranges,
  956. .n_yes_ranges = ARRAY_SIZE(dspi_volatile_ranges),
  957. };
  958. static const struct regmap_config dspi_regmap_config = {
  959. .reg_bits = 32,
  960. .val_bits = 32,
  961. .reg_stride = 4,
  962. .max_register = 0x88,
  963. .volatile_table = &dspi_volatile_table,
  964. };
  965. static const struct regmap_range dspi_xspi_volatile_ranges[] = {
  966. regmap_reg_range(SPI_MCR, SPI_TCR),
  967. regmap_reg_range(SPI_SR, SPI_SR),
  968. regmap_reg_range(SPI_PUSHR, SPI_RXFR3),
  969. regmap_reg_range(SPI_SREX, SPI_SREX),
  970. };
  971. static const struct regmap_access_table dspi_xspi_volatile_table = {
  972. .yes_ranges = dspi_xspi_volatile_ranges,
  973. .n_yes_ranges = ARRAY_SIZE(dspi_xspi_volatile_ranges),
  974. };
  975. static const struct regmap_config dspi_xspi_regmap_config[] = {
  976. {
  977. .reg_bits = 32,
  978. .val_bits = 32,
  979. .reg_stride = 4,
  980. .max_register = 0x13c,
  981. .volatile_table = &dspi_xspi_volatile_table,
  982. },
  983. {
  984. .name = "pushr",
  985. .reg_bits = 16,
  986. .val_bits = 16,
  987. .reg_stride = 2,
  988. .max_register = 0x2,
  989. },
  990. };
  991. static int dspi_init(struct fsl_dspi *dspi)
  992. {
  993. unsigned int mcr;
  994. /* Set idle states for all chip select signals to high */
  995. mcr = SPI_MCR_PCSIS(GENMASK(dspi->ctlr->max_native_cs - 1, 0));
  996. if (dspi->devtype_data->trans_mode == DSPI_XSPI_MODE)
  997. mcr |= SPI_MCR_XSPI;
  998. if (!spi_controller_is_slave(dspi->ctlr))
  999. mcr |= SPI_MCR_MASTER;
  1000. regmap_write(dspi->regmap, SPI_MCR, mcr);
  1001. regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
  1002. switch (dspi->devtype_data->trans_mode) {
  1003. case DSPI_XSPI_MODE:
  1004. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_CMDTCFE);
  1005. break;
  1006. case DSPI_DMA_MODE:
  1007. regmap_write(dspi->regmap, SPI_RSER,
  1008. SPI_RSER_TFFFE | SPI_RSER_TFFFD |
  1009. SPI_RSER_RFDFE | SPI_RSER_RFDFD);
  1010. break;
  1011. default:
  1012. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  1013. dspi->devtype_data->trans_mode);
  1014. return -EINVAL;
  1015. }
  1016. return 0;
  1017. }
  1018. static int dspi_slave_abort(struct spi_master *master)
  1019. {
  1020. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  1021. /*
  1022. * Terminate all pending DMA transactions for the SPI working
  1023. * in SLAVE mode.
  1024. */
  1025. if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
  1026. dmaengine_terminate_sync(dspi->dma->chan_rx);
  1027. dmaengine_terminate_sync(dspi->dma->chan_tx);
  1028. }
  1029. /* Clear the internal DSPI RX and TX FIFO buffers */
  1030. regmap_update_bits(dspi->regmap, SPI_MCR,
  1031. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  1032. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  1033. return 0;
  1034. }
  1035. static int dspi_probe(struct platform_device *pdev)
  1036. {
  1037. struct device_node *np = pdev->dev.of_node;
  1038. const struct regmap_config *regmap_config;
  1039. struct fsl_dspi_platform_data *pdata;
  1040. struct spi_controller *ctlr;
  1041. int ret, cs_num, bus_num = -1;
  1042. struct fsl_dspi *dspi;
  1043. struct resource *res;
  1044. void __iomem *base;
  1045. bool big_endian;
  1046. dspi = devm_kzalloc(&pdev->dev, sizeof(*dspi), GFP_KERNEL);
  1047. if (!dspi)
  1048. return -ENOMEM;
  1049. ctlr = spi_alloc_master(&pdev->dev, 0);
  1050. if (!ctlr)
  1051. return -ENOMEM;
  1052. spi_controller_set_devdata(ctlr, dspi);
  1053. platform_set_drvdata(pdev, dspi);
  1054. dspi->pdev = pdev;
  1055. dspi->ctlr = ctlr;
  1056. ctlr->setup = dspi_setup;
  1057. ctlr->transfer_one_message = dspi_transfer_one_message;
  1058. ctlr->dev.of_node = pdev->dev.of_node;
  1059. ctlr->cleanup = dspi_cleanup;
  1060. ctlr->slave_abort = dspi_slave_abort;
  1061. ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST;
  1062. pdata = dev_get_platdata(&pdev->dev);
  1063. if (pdata) {
  1064. ctlr->num_chipselect = ctlr->max_native_cs = pdata->cs_num;
  1065. ctlr->bus_num = pdata->bus_num;
  1066. /* Only Coldfire uses platform data */
  1067. dspi->devtype_data = &devtype_data[MCF5441X];
  1068. big_endian = true;
  1069. } else {
  1070. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  1071. if (ret < 0) {
  1072. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  1073. goto out_ctlr_put;
  1074. }
  1075. ctlr->num_chipselect = ctlr->max_native_cs = cs_num;
  1076. of_property_read_u32(np, "bus-num", &bus_num);
  1077. ctlr->bus_num = bus_num;
  1078. if (of_property_read_bool(np, "spi-slave"))
  1079. ctlr->slave = true;
  1080. dspi->devtype_data = of_device_get_match_data(&pdev->dev);
  1081. if (!dspi->devtype_data) {
  1082. dev_err(&pdev->dev, "can't get devtype_data\n");
  1083. ret = -EFAULT;
  1084. goto out_ctlr_put;
  1085. }
  1086. big_endian = of_device_is_big_endian(np);
  1087. }
  1088. if (big_endian) {
  1089. dspi->pushr_cmd = 0;
  1090. dspi->pushr_tx = 2;
  1091. } else {
  1092. dspi->pushr_cmd = 2;
  1093. dspi->pushr_tx = 0;
  1094. }
  1095. if (dspi->devtype_data->trans_mode == DSPI_XSPI_MODE)
  1096. ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
  1097. else
  1098. ctlr->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
  1099. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1100. if (IS_ERR(base)) {
  1101. ret = PTR_ERR(base);
  1102. goto out_ctlr_put;
  1103. }
  1104. if (dspi->devtype_data->trans_mode == DSPI_XSPI_MODE)
  1105. regmap_config = &dspi_xspi_regmap_config[0];
  1106. else
  1107. regmap_config = &dspi_regmap_config;
  1108. dspi->regmap = devm_regmap_init_mmio(&pdev->dev, base, regmap_config);
  1109. if (IS_ERR(dspi->regmap)) {
  1110. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  1111. PTR_ERR(dspi->regmap));
  1112. ret = PTR_ERR(dspi->regmap);
  1113. goto out_ctlr_put;
  1114. }
  1115. if (dspi->devtype_data->trans_mode == DSPI_XSPI_MODE) {
  1116. dspi->regmap_pushr = devm_regmap_init_mmio(
  1117. &pdev->dev, base + SPI_PUSHR,
  1118. &dspi_xspi_regmap_config[1]);
  1119. if (IS_ERR(dspi->regmap_pushr)) {
  1120. dev_err(&pdev->dev,
  1121. "failed to init pushr regmap: %ld\n",
  1122. PTR_ERR(dspi->regmap_pushr));
  1123. ret = PTR_ERR(dspi->regmap_pushr);
  1124. goto out_ctlr_put;
  1125. }
  1126. }
  1127. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  1128. if (IS_ERR(dspi->clk)) {
  1129. ret = PTR_ERR(dspi->clk);
  1130. dev_err(&pdev->dev, "unable to get clock\n");
  1131. goto out_ctlr_put;
  1132. }
  1133. ret = clk_prepare_enable(dspi->clk);
  1134. if (ret)
  1135. goto out_ctlr_put;
  1136. ret = dspi_init(dspi);
  1137. if (ret)
  1138. goto out_clk_put;
  1139. dspi->irq = platform_get_irq(pdev, 0);
  1140. if (dspi->irq <= 0) {
  1141. dev_info(&pdev->dev,
  1142. "can't get platform irq, using poll mode\n");
  1143. dspi->irq = 0;
  1144. goto poll_mode;
  1145. }
  1146. init_completion(&dspi->xfer_done);
  1147. ret = request_threaded_irq(dspi->irq, dspi_interrupt, NULL,
  1148. IRQF_SHARED, pdev->name, dspi);
  1149. if (ret < 0) {
  1150. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  1151. goto out_clk_put;
  1152. }
  1153. poll_mode:
  1154. if (dspi->devtype_data->trans_mode == DSPI_DMA_MODE) {
  1155. ret = dspi_request_dma(dspi, res->start);
  1156. if (ret < 0) {
  1157. dev_err(&pdev->dev, "can't get dma channels\n");
  1158. goto out_free_irq;
  1159. }
  1160. }
  1161. ctlr->max_speed_hz =
  1162. clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
  1163. if (dspi->devtype_data->trans_mode != DSPI_DMA_MODE)
  1164. ctlr->ptp_sts_supported = true;
  1165. ret = spi_register_controller(ctlr);
  1166. if (ret != 0) {
  1167. dev_err(&pdev->dev, "Problem registering DSPI ctlr\n");
  1168. goto out_release_dma;
  1169. }
  1170. return ret;
  1171. out_release_dma:
  1172. dspi_release_dma(dspi);
  1173. out_free_irq:
  1174. if (dspi->irq)
  1175. free_irq(dspi->irq, dspi);
  1176. out_clk_put:
  1177. clk_disable_unprepare(dspi->clk);
  1178. out_ctlr_put:
  1179. spi_controller_put(ctlr);
  1180. return ret;
  1181. }
  1182. static int dspi_remove(struct platform_device *pdev)
  1183. {
  1184. struct fsl_dspi *dspi = platform_get_drvdata(pdev);
  1185. /* Disconnect from the SPI framework */
  1186. spi_unregister_controller(dspi->ctlr);
  1187. /* Disable RX and TX */
  1188. regmap_update_bits(dspi->regmap, SPI_MCR,
  1189. SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF,
  1190. SPI_MCR_DIS_TXF | SPI_MCR_DIS_RXF);
  1191. /* Stop Running */
  1192. regmap_update_bits(dspi->regmap, SPI_MCR, SPI_MCR_HALT, SPI_MCR_HALT);
  1193. dspi_release_dma(dspi);
  1194. if (dspi->irq)
  1195. free_irq(dspi->irq, dspi);
  1196. clk_disable_unprepare(dspi->clk);
  1197. return 0;
  1198. }
  1199. static void dspi_shutdown(struct platform_device *pdev)
  1200. {
  1201. dspi_remove(pdev);
  1202. }
  1203. static struct platform_driver fsl_dspi_driver = {
  1204. .driver.name = DRIVER_NAME,
  1205. .driver.of_match_table = fsl_dspi_dt_ids,
  1206. .driver.owner = THIS_MODULE,
  1207. .driver.pm = &dspi_pm,
  1208. .probe = dspi_probe,
  1209. .remove = dspi_remove,
  1210. .shutdown = dspi_shutdown,
  1211. };
  1212. module_platform_driver(fsl_dspi_driver);
  1213. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  1214. MODULE_LICENSE("GPL");
  1215. MODULE_ALIAS("platform:" DRIVER_NAME);