spi-fsl-dspi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_device.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/regmap.h>
  28. #include <linux/sched.h>
  29. #include <linux/spi/spi.h>
  30. #include <linux/spi/spi_bitbang.h>
  31. #define DRIVER_NAME "fsl-dspi"
  32. #define TRAN_STATE_RX_VOID 0x01
  33. #define TRAN_STATE_TX_VOID 0x02
  34. #define TRAN_STATE_WORD_ODD_NUM 0x04
  35. #define DSPI_FIFO_SIZE 4
  36. #define SPI_MCR 0x00
  37. #define SPI_MCR_MASTER (1 << 31)
  38. #define SPI_MCR_PCSIS (0x3F << 16)
  39. #define SPI_MCR_CLR_TXF (1 << 11)
  40. #define SPI_MCR_CLR_RXF (1 << 10)
  41. #define SPI_TCR 0x08
  42. #define SPI_CTAR(x) (0x0c + (((x) & 0x3) * 4))
  43. #define SPI_CTAR_FMSZ(x) (((x) & 0x0000000f) << 27)
  44. #define SPI_CTAR_CPOL(x) ((x) << 26)
  45. #define SPI_CTAR_CPHA(x) ((x) << 25)
  46. #define SPI_CTAR_LSBFE(x) ((x) << 24)
  47. #define SPI_CTAR_PCSSCR(x) (((x) & 0x00000003) << 22)
  48. #define SPI_CTAR_PASC(x) (((x) & 0x00000003) << 20)
  49. #define SPI_CTAR_PDT(x) (((x) & 0x00000003) << 18)
  50. #define SPI_CTAR_PBR(x) (((x) & 0x00000003) << 16)
  51. #define SPI_CTAR_CSSCK(x) (((x) & 0x0000000f) << 12)
  52. #define SPI_CTAR_ASC(x) (((x) & 0x0000000f) << 8)
  53. #define SPI_CTAR_DT(x) (((x) & 0x0000000f) << 4)
  54. #define SPI_CTAR_BR(x) ((x) & 0x0000000f)
  55. #define SPI_CTAR0_SLAVE 0x0c
  56. #define SPI_SR 0x2c
  57. #define SPI_SR_EOQF 0x10000000
  58. #define SPI_RSER 0x30
  59. #define SPI_RSER_EOQFE 0x10000000
  60. #define SPI_PUSHR 0x34
  61. #define SPI_PUSHR_CONT (1 << 31)
  62. #define SPI_PUSHR_CTAS(x) (((x) & 0x00000003) << 28)
  63. #define SPI_PUSHR_EOQ (1 << 27)
  64. #define SPI_PUSHR_CTCNT (1 << 26)
  65. #define SPI_PUSHR_PCS(x) (((1 << x) & 0x0000003f) << 16)
  66. #define SPI_PUSHR_TXDATA(x) ((x) & 0x0000ffff)
  67. #define SPI_PUSHR_SLAVE 0x34
  68. #define SPI_POPR 0x38
  69. #define SPI_POPR_RXDATA(x) ((x) & 0x0000ffff)
  70. #define SPI_TXFR0 0x3c
  71. #define SPI_TXFR1 0x40
  72. #define SPI_TXFR2 0x44
  73. #define SPI_TXFR3 0x48
  74. #define SPI_RXFR0 0x7c
  75. #define SPI_RXFR1 0x80
  76. #define SPI_RXFR2 0x84
  77. #define SPI_RXFR3 0x88
  78. #define SPI_FRAME_BITS(bits) SPI_CTAR_FMSZ((bits) - 1)
  79. #define SPI_FRAME_BITS_MASK SPI_CTAR_FMSZ(0xf)
  80. #define SPI_FRAME_BITS_16 SPI_CTAR_FMSZ(0xf)
  81. #define SPI_FRAME_BITS_8 SPI_CTAR_FMSZ(0x7)
  82. #define SPI_CS_INIT 0x01
  83. #define SPI_CS_ASSERT 0x02
  84. #define SPI_CS_DROP 0x04
  85. struct chip_data {
  86. u32 mcr_val;
  87. u32 ctar_val;
  88. u16 void_write_data;
  89. };
  90. struct fsl_dspi {
  91. struct spi_master *master;
  92. struct platform_device *pdev;
  93. struct regmap *regmap;
  94. int irq;
  95. struct clk *clk;
  96. struct spi_transfer *cur_transfer;
  97. struct spi_message *cur_msg;
  98. struct chip_data *cur_chip;
  99. size_t len;
  100. void *tx;
  101. void *tx_end;
  102. void *rx;
  103. void *rx_end;
  104. char dataflags;
  105. u8 cs;
  106. u16 void_write_data;
  107. u32 cs_change;
  108. wait_queue_head_t waitq;
  109. u32 waitflags;
  110. };
  111. static inline int is_double_byte_mode(struct fsl_dspi *dspi)
  112. {
  113. unsigned int val;
  114. regmap_read(dspi->regmap, SPI_CTAR(dspi->cs), &val);
  115. return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
  116. }
  117. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  118. unsigned long clkrate)
  119. {
  120. /* Valid baud rate pre-scaler values */
  121. int pbr_tbl[4] = {2, 3, 5, 7};
  122. int brs[16] = { 2, 4, 6, 8,
  123. 16, 32, 64, 128,
  124. 256, 512, 1024, 2048,
  125. 4096, 8192, 16384, 32768 };
  126. int scale_needed, scale, minscale = INT_MAX;
  127. int i, j;
  128. scale_needed = clkrate / speed_hz;
  129. for (i = 0; i < ARRAY_SIZE(brs); i++)
  130. for (j = 0; j < ARRAY_SIZE(pbr_tbl); j++) {
  131. scale = brs[i] * pbr_tbl[j];
  132. if (scale >= scale_needed) {
  133. if (scale < minscale) {
  134. minscale = scale;
  135. *br = i;
  136. *pbr = j;
  137. }
  138. break;
  139. }
  140. }
  141. if (minscale == INT_MAX) {
  142. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld, we use the max prescaler value.\n",
  143. speed_hz, clkrate);
  144. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  145. *br = ARRAY_SIZE(brs) - 1;
  146. }
  147. }
  148. static int dspi_transfer_write(struct fsl_dspi *dspi)
  149. {
  150. int tx_count = 0;
  151. int tx_word;
  152. u16 d16;
  153. u8 d8;
  154. u32 dspi_pushr = 0;
  155. int first = 1;
  156. tx_word = is_double_byte_mode(dspi);
  157. /* If we are in word mode, but only have a single byte to transfer
  158. * then switch to byte mode temporarily. Will switch back at the
  159. * end of the transfer.
  160. */
  161. if (tx_word && (dspi->len == 1)) {
  162. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  163. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  164. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  165. tx_word = 0;
  166. }
  167. while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
  168. if (tx_word) {
  169. if (dspi->len == 1)
  170. break;
  171. if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
  172. d16 = *(u16 *)dspi->tx;
  173. dspi->tx += 2;
  174. } else {
  175. d16 = dspi->void_write_data;
  176. }
  177. dspi_pushr = SPI_PUSHR_TXDATA(d16) |
  178. SPI_PUSHR_PCS(dspi->cs) |
  179. SPI_PUSHR_CTAS(dspi->cs) |
  180. SPI_PUSHR_CONT;
  181. dspi->len -= 2;
  182. } else {
  183. if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
  184. d8 = *(u8 *)dspi->tx;
  185. dspi->tx++;
  186. } else {
  187. d8 = (u8)dspi->void_write_data;
  188. }
  189. dspi_pushr = SPI_PUSHR_TXDATA(d8) |
  190. SPI_PUSHR_PCS(dspi->cs) |
  191. SPI_PUSHR_CTAS(dspi->cs) |
  192. SPI_PUSHR_CONT;
  193. dspi->len--;
  194. }
  195. if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
  196. /* last transfer in the transfer */
  197. dspi_pushr |= SPI_PUSHR_EOQ;
  198. if ((dspi->cs_change) && (!dspi->len))
  199. dspi_pushr &= ~SPI_PUSHR_CONT;
  200. } else if (tx_word && (dspi->len == 1))
  201. dspi_pushr |= SPI_PUSHR_EOQ;
  202. if (first) {
  203. first = 0;
  204. dspi_pushr |= SPI_PUSHR_CTCNT; /* clear counter */
  205. }
  206. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  207. tx_count++;
  208. }
  209. return tx_count * (tx_word + 1);
  210. }
  211. static int dspi_transfer_read(struct fsl_dspi *dspi)
  212. {
  213. int rx_count = 0;
  214. int rx_word = is_double_byte_mode(dspi);
  215. u16 d;
  216. while ((dspi->rx < dspi->rx_end)
  217. && (rx_count < DSPI_FIFO_SIZE)) {
  218. if (rx_word) {
  219. unsigned int val;
  220. if ((dspi->rx_end - dspi->rx) == 1)
  221. break;
  222. regmap_read(dspi->regmap, SPI_POPR, &val);
  223. d = SPI_POPR_RXDATA(val);
  224. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  225. *(u16 *)dspi->rx = d;
  226. dspi->rx += 2;
  227. } else {
  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. *(u8 *)dspi->rx = d;
  233. dspi->rx++;
  234. }
  235. rx_count++;
  236. }
  237. return rx_count;
  238. }
  239. static int dspi_transfer_one_message(struct spi_master *master,
  240. struct spi_message *message)
  241. {
  242. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  243. struct spi_device *spi = message->spi;
  244. struct spi_transfer *transfer;
  245. int status = 0;
  246. message->actual_length = 0;
  247. list_for_each_entry(transfer, &message->transfers, transfer_list) {
  248. dspi->cur_transfer = transfer;
  249. dspi->cur_msg = message;
  250. dspi->cur_chip = spi_get_ctldata(spi);
  251. dspi->cs = spi->chip_select;
  252. if (dspi->cur_transfer->transfer_list.next
  253. == &dspi->cur_msg->transfers)
  254. transfer->cs_change = 1;
  255. dspi->cs_change = transfer->cs_change;
  256. dspi->void_write_data = dspi->cur_chip->void_write_data;
  257. dspi->dataflags = 0;
  258. dspi->tx = (void *)transfer->tx_buf;
  259. dspi->tx_end = dspi->tx + transfer->len;
  260. dspi->rx = transfer->rx_buf;
  261. dspi->rx_end = dspi->rx + transfer->len;
  262. dspi->len = transfer->len;
  263. if (!dspi->rx)
  264. dspi->dataflags |= TRAN_STATE_RX_VOID;
  265. if (!dspi->tx)
  266. dspi->dataflags |= TRAN_STATE_TX_VOID;
  267. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  268. regmap_update_bits(dspi->regmap, SPI_MCR,
  269. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF,
  270. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF);
  271. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
  272. dspi->cur_chip->ctar_val);
  273. if (transfer->speed_hz)
  274. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
  275. dspi->cur_chip->ctar_val);
  276. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  277. message->actual_length += dspi_transfer_write(dspi);
  278. if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
  279. dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
  280. dspi->waitflags = 0;
  281. if (transfer->delay_usecs)
  282. udelay(transfer->delay_usecs);
  283. }
  284. message->status = status;
  285. spi_finalize_current_message(master);
  286. return status;
  287. }
  288. static int dspi_setup(struct spi_device *spi)
  289. {
  290. struct chip_data *chip;
  291. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  292. unsigned char br = 0, pbr = 0, fmsz = 0;
  293. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  294. fmsz = spi->bits_per_word - 1;
  295. } else {
  296. pr_err("Invalid wordsize\n");
  297. return -ENODEV;
  298. }
  299. /* Only alloc on first setup */
  300. chip = spi_get_ctldata(spi);
  301. if (chip == NULL) {
  302. chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
  303. if (!chip)
  304. return -ENOMEM;
  305. }
  306. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  307. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  308. chip->void_write_data = 0;
  309. hz_to_spi_baud(&pbr, &br,
  310. spi->max_speed_hz, clk_get_rate(dspi->clk));
  311. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  312. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  313. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  314. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  315. | SPI_CTAR_PBR(pbr)
  316. | SPI_CTAR_BR(br);
  317. spi_set_ctldata(spi, chip);
  318. return 0;
  319. }
  320. static void dspi_cleanup(struct spi_device *spi)
  321. {
  322. struct chip_data *chip = spi_get_ctldata((struct spi_device *)spi);
  323. dev_dbg(&spi->dev, "spi_device %u.%u cleanup\n",
  324. spi->master->bus_num, spi->chip_select);
  325. kfree(chip);
  326. }
  327. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  328. {
  329. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  330. struct spi_message *msg = dspi->cur_msg;
  331. regmap_write(dspi->regmap, SPI_SR, SPI_SR_EOQF);
  332. dspi_transfer_read(dspi);
  333. if (!dspi->len) {
  334. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
  335. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  336. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(16));
  337. dspi->waitflags = 1;
  338. wake_up_interruptible(&dspi->waitq);
  339. } else
  340. msg->actual_length += dspi_transfer_write(dspi);
  341. return IRQ_HANDLED;
  342. }
  343. static const struct of_device_id fsl_dspi_dt_ids[] = {
  344. { .compatible = "fsl,vf610-dspi", .data = NULL, },
  345. { /* sentinel */ }
  346. };
  347. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  348. #ifdef CONFIG_PM_SLEEP
  349. static int dspi_suspend(struct device *dev)
  350. {
  351. struct spi_master *master = dev_get_drvdata(dev);
  352. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  353. spi_master_suspend(master);
  354. clk_disable_unprepare(dspi->clk);
  355. return 0;
  356. }
  357. static int dspi_resume(struct device *dev)
  358. {
  359. struct spi_master *master = dev_get_drvdata(dev);
  360. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  361. clk_prepare_enable(dspi->clk);
  362. spi_master_resume(master);
  363. return 0;
  364. }
  365. #endif /* CONFIG_PM_SLEEP */
  366. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  367. static const struct regmap_config dspi_regmap_config = {
  368. .reg_bits = 32,
  369. .val_bits = 32,
  370. .reg_stride = 4,
  371. .max_register = 0x88,
  372. };
  373. static int dspi_probe(struct platform_device *pdev)
  374. {
  375. struct device_node *np = pdev->dev.of_node;
  376. struct spi_master *master;
  377. struct fsl_dspi *dspi;
  378. struct resource *res;
  379. void __iomem *base;
  380. int ret = 0, cs_num, bus_num;
  381. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  382. if (!master)
  383. return -ENOMEM;
  384. dspi = spi_master_get_devdata(master);
  385. dspi->pdev = pdev;
  386. dspi->master = master;
  387. master->transfer = NULL;
  388. master->setup = dspi_setup;
  389. master->transfer_one_message = dspi_transfer_one_message;
  390. master->dev.of_node = pdev->dev.of_node;
  391. master->cleanup = dspi_cleanup;
  392. master->mode_bits = SPI_CPOL | SPI_CPHA;
  393. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  394. SPI_BPW_MASK(16);
  395. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  396. if (ret < 0) {
  397. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  398. goto out_master_put;
  399. }
  400. master->num_chipselect = cs_num;
  401. ret = of_property_read_u32(np, "bus-num", &bus_num);
  402. if (ret < 0) {
  403. dev_err(&pdev->dev, "can't get bus-num\n");
  404. goto out_master_put;
  405. }
  406. master->bus_num = bus_num;
  407. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  408. base = devm_ioremap_resource(&pdev->dev, res);
  409. if (IS_ERR(base)) {
  410. ret = PTR_ERR(base);
  411. goto out_master_put;
  412. }
  413. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "dspi", base,
  414. &dspi_regmap_config);
  415. if (IS_ERR(dspi->regmap)) {
  416. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  417. PTR_ERR(dspi->regmap));
  418. return PTR_ERR(dspi->regmap);
  419. }
  420. dspi->irq = platform_get_irq(pdev, 0);
  421. if (dspi->irq < 0) {
  422. dev_err(&pdev->dev, "can't get platform irq\n");
  423. ret = dspi->irq;
  424. goto out_master_put;
  425. }
  426. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  427. pdev->name, dspi);
  428. if (ret < 0) {
  429. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  430. goto out_master_put;
  431. }
  432. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  433. if (IS_ERR(dspi->clk)) {
  434. ret = PTR_ERR(dspi->clk);
  435. dev_err(&pdev->dev, "unable to get clock\n");
  436. goto out_master_put;
  437. }
  438. clk_prepare_enable(dspi->clk);
  439. init_waitqueue_head(&dspi->waitq);
  440. platform_set_drvdata(pdev, master);
  441. ret = spi_register_master(master);
  442. if (ret != 0) {
  443. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  444. goto out_clk_put;
  445. }
  446. return ret;
  447. out_clk_put:
  448. clk_disable_unprepare(dspi->clk);
  449. out_master_put:
  450. spi_master_put(master);
  451. return ret;
  452. }
  453. static int dspi_remove(struct platform_device *pdev)
  454. {
  455. struct spi_master *master = platform_get_drvdata(pdev);
  456. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  457. /* Disconnect from the SPI framework */
  458. clk_disable_unprepare(dspi->clk);
  459. spi_unregister_master(dspi->master);
  460. spi_master_put(dspi->master);
  461. return 0;
  462. }
  463. static struct platform_driver fsl_dspi_driver = {
  464. .driver.name = DRIVER_NAME,
  465. .driver.of_match_table = fsl_dspi_dt_ids,
  466. .driver.owner = THIS_MODULE,
  467. .driver.pm = &dspi_pm,
  468. .probe = dspi_probe,
  469. .remove = dspi_remove,
  470. };
  471. module_platform_driver(fsl_dspi_driver);
  472. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  473. MODULE_LICENSE("GPL");
  474. MODULE_ALIAS("platform:" DRIVER_NAME);