spi-fsl-dspi.c 19 KB

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