spi-fsl-dspi.c 19 KB

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