spi-fsl-dspi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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/err.h>
  18. #include <linux/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/io.h>
  21. #include <linux/kernel.h>
  22. #include <linux/math64.h>
  23. #include <linux/module.h>
  24. #include <linux/of.h>
  25. #include <linux/of_device.h>
  26. #include <linux/pinctrl/consumer.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm_runtime.h>
  29. #include <linux/regmap.h>
  30. #include <linux/sched.h>
  31. #include <linux/spi/spi.h>
  32. #include <linux/spi/spi_bitbang.h>
  33. #include <linux/time.h>
  34. #define DRIVER_NAME "fsl-dspi"
  35. #define TRAN_STATE_RX_VOID 0x01
  36. #define TRAN_STATE_TX_VOID 0x02
  37. #define TRAN_STATE_WORD_ODD_NUM 0x04
  38. #define DSPI_FIFO_SIZE 4
  39. #define SPI_MCR 0x00
  40. #define SPI_MCR_MASTER (1 << 31)
  41. #define SPI_MCR_PCSIS (0x3F << 16)
  42. #define SPI_MCR_CLR_TXF (1 << 11)
  43. #define SPI_MCR_CLR_RXF (1 << 10)
  44. #define SPI_TCR 0x08
  45. #define SPI_TCR_GET_TCNT(x) (((x) & 0xffff0000) >> 16)
  46. #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
  47. #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
  48. #define SPI_CTAR_CPOL(x) ((x) << 26)
  49. #define SPI_CTAR_CPHA(x) ((x) << 25)
  50. #define SPI_CTAR_LSBFE(x) ((x) << 24)
  51. #define SPI_CTAR_PCSSCK(x) (((x) & 0x00000003) << 22)
  52. #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
  53. #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
  54. #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
  55. #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
  56. #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
  57. #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
  58. #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
  59. #define SPI_CTAR_SCALE_BITS 0xf
  60. #define SPI_CTAR0_SLAVE 0x0c
  61. #define SPI_SR 0x2c
  62. #define SPI_SR_EOQF 0x10000000
  63. #define SPI_SR_TCFQF 0x80000000
  64. #define SPI_SR_CLEAR 0xdaad0000
  65. #define SPI_RSER 0x30
  66. #define SPI_RSER_EOQFE 0x10000000
  67. #define SPI_RSER_TCFQE 0x80000000
  68. #define SPI_PUSHR 0x34
  69. #define SPI_PUSHR_CONT (1 << 31)
  70. #define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
  71. #define SPI_PUSHR_EOQ (1 << 27)
  72. #define SPI_PUSHR_CTCNT (1 << 26)
  73. #define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
  74. #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
  75. #define SPI_PUSHR_SLAVE 0x34
  76. #define SPI_POPR 0x38
  77. #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
  78. #define SPI_TXFR0 0x3c
  79. #define SPI_TXFR1 0x40
  80. #define SPI_TXFR2 0x44
  81. #define SPI_TXFR3 0x48
  82. #define SPI_RXFR0 0x7c
  83. #define SPI_RXFR1 0x80
  84. #define SPI_RXFR2 0x84
  85. #define SPI_RXFR3 0x88
  86. #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
  87. #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
  88. #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
  89. #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
  90. #define SPI_CS_INIT 0x01
  91. #define SPI_CS_ASSERT 0x02
  92. #define SPI_CS_DROP 0x04
  93. #define SPI_TCR_TCNT_MAX 0x10000
  94. struct chip_data {
  95. u32 mcr_val;
  96. u32 ctar_val;
  97. u16 void_write_data;
  98. };
  99. enum dspi_trans_mode {
  100. DSPI_EOQ_MODE = 0,
  101. DSPI_TCFQ_MODE,
  102. };
  103. struct fsl_dspi_devtype_data {
  104. enum dspi_trans_mode trans_mode;
  105. u8 max_clock_factor;
  106. };
  107. static const struct fsl_dspi_devtype_data vf610_data = {
  108. .trans_mode = DSPI_EOQ_MODE,
  109. .max_clock_factor = 2,
  110. };
  111. static const struct fsl_dspi_devtype_data ls1021a_v1_data = {
  112. .trans_mode = DSPI_TCFQ_MODE,
  113. .max_clock_factor = 8,
  114. };
  115. static const struct fsl_dspi_devtype_data ls2085a_data = {
  116. .trans_mode = DSPI_TCFQ_MODE,
  117. .max_clock_factor = 8,
  118. };
  119. struct fsl_dspi {
  120. struct spi_master *master;
  121. struct platform_device *pdev;
  122. struct regmap *regmap;
  123. int irq;
  124. struct clk *clk;
  125. struct spi_transfer *cur_transfer;
  126. struct spi_message *cur_msg;
  127. struct chip_data *cur_chip;
  128. size_t len;
  129. void *tx;
  130. void *tx_end;
  131. void *rx;
  132. void *rx_end;
  133. char dataflags;
  134. u8 cs;
  135. u16 void_write_data;
  136. u32 cs_change;
  137. const struct fsl_dspi_devtype_data *devtype_data;
  138. wait_queue_head_t waitq;
  139. u32 waitflags;
  140. u32 spi_tcnt;
  141. };
  142. static inline int is_double_byte_mode(struct fsl_dspi *dspi)
  143. {
  144. unsigned int val;
  145. regmap_read(dspi->regmap, SPI_CTAR(0), &val);
  146. return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
  147. }
  148. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  149. unsigned long clkrate)
  150. {
  151. /* Valid baud rate pre-scaler values */
  152. int pbr_tbl[4] = {2, 3, 5, 7};
  153. int brs[16] = { 2, 4, 6, 8,
  154. 16, 32, 64, 128,
  155. 256, 512, 1024, 2048,
  156. 4096, 8192, 16384, 32768 };
  157. int scale_needed, scale, minscale = INT_MAX;
  158. int i, j;
  159. scale_needed = clkrate / speed_hz;
  160. if (clkrate % speed_hz)
  161. scale_needed++;
  162. for (i = 0; i < ARRAY_SIZE(brs); i++)
  163. for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
  164. scale = brs[i] * pbr_tbl[j];
  165. if (scale >= scale_needed) {
  166. if (scale < minscale) {
  167. minscale = scale;
  168. *br = i;
  169. *pbr = j;
  170. }
  171. break;
  172. }
  173. }
  174. if (minscale == INT_MAX) {
  175. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
  176. speed_hz, clkrate);
  177. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  178. *br = ARRAY_SIZE(brs) - 1;
  179. }
  180. }
  181. static void ns_delay_scale(char *psc, char *sc, int delay_ns,
  182. unsigned long clkrate)
  183. {
  184. int pscale_tbl[4] = {1, 3, 5, 7};
  185. int scale_needed, scale, minscale = INT_MAX;
  186. int i, j;
  187. u32 remainder;
  188. scale_needed = div_u64_rem((u64)delay_ns * clkrate, NSEC_PER_SEC,
  189. &remainder);
  190. if (remainder)
  191. scale_needed++;
  192. for (i = 0; i < ARRAY_SIZE(pscale_tbl); i++)
  193. for (j = 0; j <= SPI_CTAR_SCALE_BITS; j++) {
  194. scale = pscale_tbl[i] * (2 << j);
  195. if (scale >= scale_needed) {
  196. if (scale < minscale) {
  197. minscale = scale;
  198. *psc = i;
  199. *sc = j;
  200. }
  201. break;
  202. }
  203. }
  204. if (minscale == INT_MAX) {
  205. pr_warn("Cannot find correct scale values for %dns delay at clkrate %ld, using max prescaler value",
  206. delay_ns, clkrate);
  207. *psc = ARRAY_SIZE(pscale_tbl) - 1;
  208. *sc = SPI_CTAR_SCALE_BITS;
  209. }
  210. }
  211. static u32 dspi_data_to_pushr(struct fsl_dspi *dspi, int tx_word)
  212. {
  213. u16 d16;
  214. if (!(dspi->dataflags & TRAN_STATE_TX_VOID))
  215. d16 = tx_word ? *(u16 *)dspi->tx : *(u8 *)dspi->tx;
  216. else
  217. d16 = dspi->void_write_data;
  218. dspi->tx += tx_word + 1;
  219. dspi->len -= tx_word + 1;
  220. return SPI_PUSHR_TXDATA(d16) |
  221. SPI_PUSHR_PCS(dspi->cs) |
  222. SPI_PUSHR_CTAS(0) |
  223. SPI_PUSHR_CONT;
  224. }
  225. static void dspi_data_from_popr(struct fsl_dspi *dspi, int rx_word)
  226. {
  227. u16 d;
  228. unsigned int val;
  229. regmap_read(dspi->regmap, SPI_POPR, &val);
  230. d = SPI_POPR_RXDATA(val);
  231. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  232. rx_word ? (*(u16 *)dspi->rx = d) : (*(u8 *)dspi->rx = d);
  233. dspi->rx += rx_word + 1;
  234. }
  235. static int dspi_eoq_write(struct fsl_dspi *dspi)
  236. {
  237. int tx_count = 0;
  238. int tx_word;
  239. u32 dspi_pushr = 0;
  240. tx_word = is_double_byte_mode(dspi);
  241. while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
  242. /* If we are in word mode, only have a single byte to transfer
  243. * switch to byte mode temporarily. Will switch back at the
  244. * end of the transfer.
  245. */
  246. if (tx_word && (dspi->len == 1)) {
  247. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  248. regmap_update_bits(dspi->regmap, SPI_CTAR(0),
  249. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  250. tx_word = 0;
  251. }
  252. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  253. if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
  254. /* last transfer in the transfer */
  255. dspi_pushr |= SPI_PUSHR_EOQ;
  256. if ((dspi->cs_change) && (!dspi->len))
  257. dspi_pushr &= ~SPI_PUSHR_CONT;
  258. } else if (tx_word && (dspi->len == 1))
  259. dspi_pushr |= SPI_PUSHR_EOQ;
  260. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  261. tx_count++;
  262. }
  263. return tx_count * (tx_word + 1);
  264. }
  265. static int dspi_eoq_read(struct fsl_dspi *dspi)
  266. {
  267. int rx_count = 0;
  268. int rx_word = is_double_byte_mode(dspi);
  269. while ((dspi->rx < dspi->rx_end)
  270. && (rx_count < DSPI_FIFO_SIZE)) {
  271. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  272. rx_word = 0;
  273. dspi_data_from_popr(dspi, rx_word);
  274. rx_count++;
  275. }
  276. return rx_count;
  277. }
  278. static int dspi_tcfq_write(struct fsl_dspi *dspi)
  279. {
  280. int tx_word;
  281. u32 dspi_pushr = 0;
  282. tx_word = is_double_byte_mode(dspi);
  283. if (tx_word && (dspi->len == 1)) {
  284. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  285. regmap_update_bits(dspi->regmap, SPI_CTAR(0),
  286. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  287. tx_word = 0;
  288. }
  289. dspi_pushr = dspi_data_to_pushr(dspi, tx_word);
  290. if ((dspi->cs_change) && (!dspi->len))
  291. dspi_pushr &= ~SPI_PUSHR_CONT;
  292. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  293. return tx_word + 1;
  294. }
  295. static void dspi_tcfq_read(struct fsl_dspi *dspi)
  296. {
  297. int rx_word = is_double_byte_mode(dspi);
  298. if (rx_word && (dspi->rx_end - dspi->rx) == 1)
  299. rx_word = 0;
  300. dspi_data_from_popr(dspi, rx_word);
  301. }
  302. static int dspi_transfer_one_message(struct spi_master *master,
  303. struct spi_message *message)
  304. {
  305. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  306. struct spi_device *spi = message->spi;
  307. struct spi_transfer *transfer;
  308. int status = 0;
  309. enum dspi_trans_mode trans_mode;
  310. u32 spi_tcr;
  311. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  312. dspi->spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  313. message->actual_length = 0;
  314. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  315. dspi->cur_transfer = transfer;
  316. dspi->cur_msg = message;
  317. dspi->cur_chip = spi_get_ctldata(spi);
  318. dspi->cs = spi->chip_select;
  319. dspi->cs_change = 0;
  320. if (list_is_last(&dspi->cur_transfer->transfer_list,
  321. &dspi->cur_msg->transfers) || transfer->cs_change)
  322. dspi->cs_change = 1;
  323. dspi->void_write_data = dspi->cur_chip->void_write_data;
  324. dspi->dataflags = 0;
  325. dspi->tx = (void *)transfer->tx_buf;
  326. dspi->tx_end = dspi->tx + transfer->len;
  327. dspi->rx = transfer->rx_buf;
  328. dspi->rx_end = dspi->rx + transfer->len;
  329. dspi->len = transfer->len;
  330. if (!dspi->rx)
  331. dspi->dataflags |= TRAN_STATE_RX_VOID;
  332. if (!dspi->tx)
  333. dspi->dataflags |= TRAN_STATE_TX_VOID;
  334. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  335. regmap_update_bits(dspi->regmap, SPI_MCR,
  336. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  337. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  338. regmap_write(dspi->regmap, SPI_CTAR(0),
  339. dspi->cur_chip->ctar_val);
  340. trans_mode = dspi->devtype_data->trans_mode;
  341. switch (trans_mode) {
  342. case DSPI_EOQ_MODE:
  343. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  344. dspi_eoq_write(dspi);
  345. break;
  346. case DSPI_TCFQ_MODE:
  347. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_TCFQE);
  348. dspi_tcfq_write(dspi);
  349. break;
  350. default:
  351. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  352. trans_mode);
  353. status = -EINVAL;
  354. goto out;
  355. }
  356. if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
  357. dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
  358. dspi->waitflags = 0;
  359. if (transfer->delay_usecs)
  360. udelay(transfer->delay_usecs);
  361. }
  362. out:
  363. message->status = status;
  364. spi_finalize_current_message(master);
  365. return status;
  366. }
  367. static int dspi_setup(struct spi_device *spi)
  368. {
  369. struct chip_data *chip;
  370. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  371. u32 cs_sck_delay = 0, sck_cs_delay = 0;
  372. unsigned char br = 0, pbr = 0, pcssck = 0, cssck = 0;
  373. unsigned char pasc = 0, asc = 0, fmsz = 0;
  374. unsigned long clkrate;
  375. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  376. fmsz = spi->bits_per_word - 1;
  377. } else {
  378. pr_err("Invalid wordsize\n");
  379. return -ENODEV;
  380. }
  381. /* Only alloc on first setup */
  382. chip = spi_get_ctldata(spi);
  383. if (chip == NULL) {
  384. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  385. if (!chip)
  386. return -ENOMEM;
  387. }
  388. of_property_read_u32(spi->dev.of_node, "fsl,spi-cs-sck-delay",
  389. &cs_sck_delay);
  390. of_property_read_u32(spi->dev.of_node, "fsl,spi-sck-cs-delay",
  391. &sck_cs_delay);
  392. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  393. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  394. chip->void_write_data = 0;
  395. clkrate = clk_get_rate(dspi->clk);
  396. hz_to_spi_baud(&pbr, &br, spi->max_speed_hz, clkrate);
  397. /* Set PCS to SCK delay scale values */
  398. ns_delay_scale(&pcssck, &cssck, cs_sck_delay, clkrate);
  399. /* Set After SCK delay scale values */
  400. ns_delay_scale(&pasc, &asc, sck_cs_delay, clkrate);
  401. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  402. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  403. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  404. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  405. | SPI_CTAR_PCSSCK(pcssck)
  406. | SPI_CTAR_CSSCK(cssck)
  407. | SPI_CTAR_PASC(pasc)
  408. | SPI_CTAR_ASC(asc)
  409. | SPI_CTAR_PBR(pbr)
  410. | SPI_CTAR_BR(br);
  411. spi_set_ctldata(spi, chip);
  412. return 0;
  413. }
  414. static void dspi_cleanup(struct spi_device *spi)
  415. {
  416. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  417. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  418. spi->master->bus_num, spi->chip_select);
  419. kfree(chip);
  420. }
  421. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  422. {
  423. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  424. struct spi_message *msg = dspi->cur_msg;
  425. enum dspi_trans_mode trans_mode;
  426. u32 spi_sr, spi_tcr;
  427. u32 spi_tcnt, tcnt_diff;
  428. int tx_word;
  429. regmap_read(dspi->regmap, SPI_SR, &spi_sr);
  430. regmap_write(dspi->regmap, SPI_SR, spi_sr);
  431. if (spi_sr & (SPI_SR_EOQF | SPI_SR_TCFQF)) {
  432. tx_word = is_double_byte_mode(dspi);
  433. regmap_read(dspi->regmap, SPI_TCR, &spi_tcr);
  434. spi_tcnt = SPI_TCR_GET_TCNT(spi_tcr);
  435. /*
  436. * The width of SPI Transfer Counter in SPI_TCR is 16bits,
  437. * so the max couner is 65535. When the counter reach 65535,
  438. * it will wrap around, counter reset to zero.
  439. * spi_tcnt my be less than dspi->spi_tcnt, it means the
  440. * counter already wrapped around.
  441. * SPI Transfer Counter is a counter of transmitted frames.
  442. * The size of frame maybe two bytes.
  443. */
  444. tcnt_diff = ((spi_tcnt + SPI_TCR_TCNT_MAX) - dspi->spi_tcnt)
  445. % SPI_TCR_TCNT_MAX;
  446. tcnt_diff *= (tx_word + 1);
  447. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
  448. tcnt_diff--;
  449. msg->actual_length += tcnt_diff;
  450. dspi->spi_tcnt = spi_tcnt;
  451. trans_mode = dspi->devtype_data->trans_mode;
  452. switch (trans_mode) {
  453. case DSPI_EOQ_MODE:
  454. dspi_eoq_read(dspi);
  455. break;
  456. case DSPI_TCFQ_MODE:
  457. dspi_tcfq_read(dspi);
  458. break;
  459. default:
  460. dev_err(&dspi->pdev->dev, "unsupported trans_mode %u\n",
  461. trans_mode);
  462. return IRQ_HANDLED;
  463. }
  464. if (!dspi->len) {
  465. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM) {
  466. regmap_update_bits(dspi->regmap,
  467. SPI_CTAR(0),
  468. SPI_FRAME_BITS_MASK,
  469. SPI_FRAME_BITS(16));
  470. dspi->dataflags &= ~TRAN_STATE_WORD_ODD_NUM;
  471. }
  472. dspi->waitflags = 1;
  473. wake_up_interruptible(&dspi->waitq);
  474. } else {
  475. switch (trans_mode) {
  476. case DSPI_EOQ_MODE:
  477. dspi_eoq_write(dspi);
  478. break;
  479. case DSPI_TCFQ_MODE:
  480. dspi_tcfq_write(dspi);
  481. break;
  482. default:
  483. dev_err(&dspi->pdev->dev,
  484. "unsupported trans_mode %u\n",
  485. trans_mode);
  486. }
  487. }
  488. }
  489. return IRQ_HANDLED;
  490. }
  491. static const struct of_device_id fsl_dspi_dt_ids[] = {
  492. { .compatible = "fsl,vf610-dspi", .data = (void *)&vf610_data, },
  493. { .compatible = "fsl,ls1021a-v1.0-dspi",
  494. .data = (void *)&ls1021a_v1_data, },
  495. { .compatible = "fsl,ls2085a-dspi", .data = (void *)&ls2085a_data, },
  496. { /* sentinel */ }
  497. };
  498. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  499. #ifdef CONFIG_PM_SLEEP
  500. static int dspi_suspend(struct device *dev)
  501. {
  502. struct spi_master *master = dev_get_drvdata(dev);
  503. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  504. spi_master_suspend(master);
  505. clk_disable_unprepare(dspi->clk);
  506. pinctrl_pm_select_sleep_state(dev);
  507. return 0;
  508. }
  509. static int dspi_resume(struct device *dev)
  510. {
  511. struct spi_master *master = dev_get_drvdata(dev);
  512. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  513. int ret;
  514. pinctrl_pm_select_default_state(dev);
  515. ret = clk_prepare_enable(dspi->clk);
  516. if (ret)
  517. return ret;
  518. spi_master_resume(master);
  519. return 0;
  520. }
  521. #endif /* CONFIG_PM_SLEEP */
  522. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  523. static const struct regmap_config dspi_regmap_config = {
  524. .reg_bits = 32,
  525. .val_bits = 32,
  526. .reg_stride = 4,
  527. .max_register = 0x88,
  528. };
  529. static void dspi_init(struct fsl_dspi *dspi)
  530. {
  531. regmap_write(dspi->regmap, SPI_SR, SPI_SR_CLEAR);
  532. }
  533. static int dspi_probe(struct platform_device *pdev)
  534. {
  535. struct device_node *np = pdev->dev.of_node;
  536. struct spi_master *master;
  537. struct fsl_dspi *dspi;
  538. struct resource *res;
  539. void __iomem *base;
  540. int ret = 0, cs_num, bus_num;
  541. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  542. if (!master)
  543. return -ENOMEM;
  544. dspi = spi_master_get_devdata(master);
  545. dspi->pdev = pdev;
  546. dspi->master = master;
  547. master->transfer = NULL;
  548. master->setup = dspi_setup;
  549. master->transfer_one_message = dspi_transfer_one_message;
  550. master->dev.of_node = pdev->dev.of_node;
  551. master->cleanup = dspi_cleanup;
  552. master->mode_bits = SPI_CPOL | SPI_CPHA;
  553. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  554. SPI_BPW_MASK(16);
  555. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  556. if (ret < 0) {
  557. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  558. goto out_master_put;
  559. }
  560. master->num_chipselect = cs_num;
  561. ret = of_property_read_u32(np, "bus-num", &bus_num);
  562. if (ret < 0) {
  563. dev_err(&pdev->dev, "can't get bus-num\n");
  564. goto out_master_put;
  565. }
  566. master->bus_num = bus_num;
  567. dspi->devtype_data = of_device_get_match_data(&pdev->dev);
  568. if (!dspi->devtype_data) {
  569. dev_err(&pdev->dev, "can't get devtype_data\n");
  570. ret = -EFAULT;
  571. goto out_master_put;
  572. }
  573. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  574. base = devm_ioremap_resource(&pdev->dev, res);
  575. if (IS_ERR(base)) {
  576. ret = PTR_ERR(base);
  577. goto out_master_put;
  578. }
  579. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
  580. &dspi_regmap_config);
  581. if (IS_ERR(dspi->regmap)) {
  582. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  583. PTR_ERR(dspi->regmap));
  584. return PTR_ERR(dspi->regmap);
  585. }
  586. dspi_init(dspi);
  587. dspi->irq = platform_get_irq(pdev, 0);
  588. if (dspi->irq < 0) {
  589. dev_err(&pdev->dev, "can't get platform irq\n");
  590. ret = dspi->irq;
  591. goto out_master_put;
  592. }
  593. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  594. pdev->name, dspi);
  595. if (ret < 0) {
  596. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  597. goto out_master_put;
  598. }
  599. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  600. if (IS_ERR(dspi->clk)) {
  601. ret = PTR_ERR(dspi->clk);
  602. dev_err(&pdev->dev, "unable to get clock\n");
  603. goto out_master_put;
  604. }
  605. ret = clk_prepare_enable(dspi->clk);
  606. if (ret)
  607. goto out_master_put;
  608. master->max_speed_hz =
  609. clk_get_rate(dspi->clk) / dspi->devtype_data->max_clock_factor;
  610. init_waitqueue_head(&dspi->waitq);
  611. platform_set_drvdata(pdev, master);
  612. ret = spi_register_master(master);
  613. if (ret != 0) {
  614. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  615. goto out_clk_put;
  616. }
  617. return ret;
  618. out_clk_put:
  619. clk_disable_unprepare(dspi->clk);
  620. out_master_put:
  621. spi_master_put(master);
  622. return ret;
  623. }
  624. static int dspi_remove(struct platform_device *pdev)
  625. {
  626. struct spi_master *master = platform_get_drvdata(pdev);
  627. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  628. /* Disconnect from the SPI framework */
  629. clk_disable_unprepare(dspi->clk);
  630. spi_unregister_master(dspi->master);
  631. return 0;
  632. }
  633. static struct platform_driver fsl_dspi_driver = {
  634. .driver.name = DRIVER_NAME,
  635. .driver.of_match_table = fsl_dspi_dt_ids,
  636. .driver.owner = THIS_MODULE,
  637. .driver.pm = &dspi_pm,
  638. .probe = dspi_probe,
  639. .remove = dspi_remove,
  640. };
  641. module_platform_driver(fsl_dspi_driver);
  642. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  643. MODULE_LICENSE("GPL");
  644. MODULE_ALIAS("platform:" DRIVER_NAME);