spi-fsl-dspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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_bitbang bitbang;
  92. struct platform_device *pdev;
  93. struct regmap *regmap;
  94. int irq;
  95. struct clk *clk;
  96. struct spi_transfer *cur_transfer;
  97. struct chip_data *cur_chip;
  98. size_t len;
  99. void *tx;
  100. void *tx_end;
  101. void *rx;
  102. void *rx_end;
  103. char dataflags;
  104. u8 cs;
  105. u16 void_write_data;
  106. wait_queue_head_t waitq;
  107. u32 waitflags;
  108. };
  109. static inline int is_double_byte_mode(struct fsl_dspi *dspi)
  110. {
  111. unsigned int val;
  112. regmap_read(dspi->regmap, SPI_CTAR(dspi->cs), &val);
  113. return ((val & SPI_FRAME_BITS_MASK) == SPI_FRAME_BITS(8)) ? 0 : 1;
  114. }
  115. static void hz_to_spi_baud(char *pbr, char *br, int speed_hz,
  116. unsigned long clkrate)
  117. {
  118. /* Valid baud rate pre-scaler values */
  119. int pbr_tbl[4] = {2, 3, 5, 7};
  120. int brs[16] = { 2, 4, 6, 8,
  121. 16, 32, 64, 128,
  122. 256, 512, 1024, 2048,
  123. 4096, 8192, 16384, 32768 };
  124. int temp, i = 0, j = 0;
  125. temp = clkrate / 2 / speed_hz;
  126. for (i = 0; i < ARRAY_SIZE(pbr_tbl); i++)
  127. for (j = 0; j < ARRAY_SIZE(brs); j++) {
  128. if (pbr_tbl[i] * brs[j] >= temp) {
  129. *pbr = i;
  130. *br = j;
  131. return;
  132. }
  133. }
  134. pr_warn("Can not find valid baud rate,speed_hz is %d,clkrate is %ld\
  135. ,we use the max prescaler value.\n", speed_hz, clkrate);
  136. *pbr = ARRAY_SIZE(pbr_tbl) - 1;
  137. *br = ARRAY_SIZE(brs) - 1;
  138. }
  139. static int dspi_transfer_write(struct fsl_dspi *dspi)
  140. {
  141. int tx_count = 0;
  142. int tx_word;
  143. u16 d16;
  144. u8 d8;
  145. u32 dspi_pushr = 0;
  146. int first = 1;
  147. tx_word = is_double_byte_mode(dspi);
  148. /* If we are in word mode, but only have a single byte to transfer
  149. * then switch to byte mode temporarily. Will switch back at the
  150. * end of the transfer.
  151. */
  152. if (tx_word && (dspi->len == 1)) {
  153. dspi->dataflags |= TRAN_STATE_WORD_ODD_NUM;
  154. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  155. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(8));
  156. tx_word = 0;
  157. }
  158. while (dspi->len && (tx_count < DSPI_FIFO_SIZE)) {
  159. if (tx_word) {
  160. if (dspi->len == 1)
  161. break;
  162. if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
  163. d16 = *(u16 *)dspi->tx;
  164. dspi->tx += 2;
  165. } else {
  166. d16 = dspi->void_write_data;
  167. }
  168. dspi_pushr = SPI_PUSHR_TXDATA(d16) |
  169. SPI_PUSHR_PCS(dspi->cs) |
  170. SPI_PUSHR_CTAS(dspi->cs) |
  171. SPI_PUSHR_CONT;
  172. dspi->len -= 2;
  173. } else {
  174. if (!(dspi->dataflags & TRAN_STATE_TX_VOID)) {
  175. d8 = *(u8 *)dspi->tx;
  176. dspi->tx++;
  177. } else {
  178. d8 = (u8)dspi->void_write_data;
  179. }
  180. dspi_pushr = SPI_PUSHR_TXDATA(d8) |
  181. SPI_PUSHR_PCS(dspi->cs) |
  182. SPI_PUSHR_CTAS(dspi->cs) |
  183. SPI_PUSHR_CONT;
  184. dspi->len--;
  185. }
  186. if (dspi->len == 0 || tx_count == DSPI_FIFO_SIZE - 1) {
  187. /* last transfer in the transfer */
  188. dspi_pushr |= SPI_PUSHR_EOQ;
  189. } else if (tx_word && (dspi->len == 1))
  190. dspi_pushr |= SPI_PUSHR_EOQ;
  191. if (first) {
  192. first = 0;
  193. dspi_pushr |= SPI_PUSHR_CTCNT; /* clear counter */
  194. }
  195. regmap_write(dspi->regmap, SPI_PUSHR, dspi_pushr);
  196. tx_count++;
  197. }
  198. return tx_count * (tx_word + 1);
  199. }
  200. static int dspi_transfer_read(struct fsl_dspi *dspi)
  201. {
  202. int rx_count = 0;
  203. int rx_word = is_double_byte_mode(dspi);
  204. u16 d;
  205. while ((dspi->rx < dspi->rx_end)
  206. && (rx_count < DSPI_FIFO_SIZE)) {
  207. if (rx_word) {
  208. unsigned int val;
  209. if ((dspi->rx_end - dspi->rx) == 1)
  210. break;
  211. regmap_read(dspi->regmap, SPI_POPR, &val);
  212. d = SPI_POPR_RXDATA(val);
  213. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  214. *(u16 *)dspi->rx = d;
  215. dspi->rx += 2;
  216. } else {
  217. unsigned int val;
  218. regmap_read(dspi->regmap, SPI_POPR, &val);
  219. d = SPI_POPR_RXDATA(val);
  220. if (!(dspi->dataflags & TRAN_STATE_RX_VOID))
  221. *(u8 *)dspi->rx = d;
  222. dspi->rx++;
  223. }
  224. rx_count++;
  225. }
  226. return rx_count;
  227. }
  228. static int dspi_txrx_transfer(struct spi_device *spi, struct spi_transfer *t)
  229. {
  230. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  231. dspi->cur_transfer = t;
  232. dspi->cur_chip = spi_get_ctldata(spi);
  233. dspi->cs = spi->chip_select;
  234. dspi->void_write_data = dspi->cur_chip->void_write_data;
  235. dspi->dataflags = 0;
  236. dspi->tx = (void *)t->tx_buf;
  237. dspi->tx_end = dspi->tx + t->len;
  238. dspi->rx = t->rx_buf;
  239. dspi->rx_end = dspi->rx + t->len;
  240. dspi->len = t->len;
  241. if (!dspi->rx)
  242. dspi->dataflags |= TRAN_STATE_RX_VOID;
  243. if (!dspi->tx)
  244. dspi->dataflags |= TRAN_STATE_TX_VOID;
  245. regmap_write(dspi->regmap, SPI_MCR, dspi->cur_chip->mcr_val);
  246. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs), dspi->cur_chip->ctar_val);
  247. regmap_write(dspi->regmap, SPI_RSER, SPI_RSER_EOQFE);
  248. if (t->speed_hz)
  249. regmap_write(dspi->regmap, SPI_CTAR(dspi->cs),
  250. dspi->cur_chip->ctar_val);
  251. dspi_transfer_write(dspi);
  252. if (wait_event_interruptible(dspi->waitq, dspi->waitflags))
  253. dev_err(&dspi->pdev->dev, "wait transfer complete fail!\n");
  254. dspi->waitflags = 0;
  255. return t->len - dspi->len;
  256. }
  257. static void dspi_chipselect(struct spi_device *spi, int value)
  258. {
  259. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  260. unsigned int pushr;
  261. regmap_read(dspi->regmap, SPI_PUSHR, &pushr);
  262. switch (value) {
  263. case BITBANG_CS_ACTIVE:
  264. pushr |= SPI_PUSHR_CONT;
  265. break;
  266. case BITBANG_CS_INACTIVE:
  267. pushr &= ~SPI_PUSHR_CONT;
  268. break;
  269. }
  270. regmap_write(dspi->regmap, SPI_PUSHR, pushr);
  271. }
  272. static int dspi_setup_transfer(struct spi_device *spi, struct spi_transfer *t)
  273. {
  274. struct chip_data *chip;
  275. struct fsl_dspi *dspi = spi_master_get_devdata(spi->master);
  276. unsigned char br = 0, pbr = 0, fmsz = 0;
  277. /* Only alloc on first setup */
  278. chip = spi_get_ctldata(spi);
  279. if (chip == NULL) {
  280. chip = devm_kzalloc(&spi->dev, sizeof(struct chip_data),
  281. GFP_KERNEL);
  282. if (!chip)
  283. return -ENOMEM;
  284. }
  285. chip->mcr_val = SPI_MCR_MASTER | SPI_MCR_PCSIS |
  286. SPI_MCR_CLR_TXF | SPI_MCR_CLR_RXF;
  287. if ((spi->bits_per_word >= 4) && (spi->bits_per_word <= 16)) {
  288. fmsz = spi->bits_per_word - 1;
  289. } else {
  290. pr_err("Invalid wordsize\n");
  291. return -ENODEV;
  292. }
  293. chip->void_write_data = 0;
  294. hz_to_spi_baud(&pbr, &br,
  295. spi->max_speed_hz, clk_get_rate(dspi->clk));
  296. chip->ctar_val = SPI_CTAR_FMSZ(fmsz)
  297. | SPI_CTAR_CPOL(spi->mode & SPI_CPOL ? 1 : 0)
  298. | SPI_CTAR_CPHA(spi->mode & SPI_CPHA ? 1 : 0)
  299. | SPI_CTAR_LSBFE(spi->mode & SPI_LSB_FIRST ? 1 : 0)
  300. | SPI_CTAR_PBR(pbr)
  301. | SPI_CTAR_BR(br);
  302. spi_set_ctldata(spi, chip);
  303. return 0;
  304. }
  305. static int dspi_setup(struct spi_device *spi)
  306. {
  307. if (!spi->max_speed_hz)
  308. return -EINVAL;
  309. return dspi_setup_transfer(spi, NULL);
  310. }
  311. static irqreturn_t dspi_interrupt(int irq, void *dev_id)
  312. {
  313. struct fsl_dspi *dspi = (struct fsl_dspi *)dev_id;
  314. regmap_write(dspi->regmap, SPI_SR, SPI_SR_EOQF);
  315. dspi_transfer_read(dspi);
  316. if (!dspi->len) {
  317. if (dspi->dataflags & TRAN_STATE_WORD_ODD_NUM)
  318. regmap_update_bits(dspi->regmap, SPI_CTAR(dspi->cs),
  319. SPI_FRAME_BITS_MASK, SPI_FRAME_BITS(16));
  320. dspi->waitflags = 1;
  321. wake_up_interruptible(&dspi->waitq);
  322. } else {
  323. dspi_transfer_write(dspi);
  324. return IRQ_HANDLED;
  325. }
  326. return IRQ_HANDLED;
  327. }
  328. static const struct of_device_id fsl_dspi_dt_ids[] = {
  329. { .compatible = "fsl,vf610-dspi", .data = NULL, },
  330. { /* sentinel */ }
  331. };
  332. MODULE_DEVICE_TABLE(of, fsl_dspi_dt_ids);
  333. #ifdef CONFIG_PM_SLEEP
  334. static int dspi_suspend(struct device *dev)
  335. {
  336. struct spi_master *master = dev_get_drvdata(dev);
  337. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  338. spi_master_suspend(master);
  339. clk_disable_unprepare(dspi->clk);
  340. return 0;
  341. }
  342. static int dspi_resume(struct device *dev)
  343. {
  344. struct spi_master *master = dev_get_drvdata(dev);
  345. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  346. clk_prepare_enable(dspi->clk);
  347. spi_master_resume(master);
  348. return 0;
  349. }
  350. #endif /* CONFIG_PM_SLEEP */
  351. static SIMPLE_DEV_PM_OPS(dspi_pm, dspi_suspend, dspi_resume);
  352. static const struct regmap_config dspi_regmap_config = {
  353. .reg_bits = 32,
  354. .val_bits = 32,
  355. .reg_stride = 4,
  356. .max_register = 0x88,
  357. };
  358. static int dspi_probe(struct platform_device *pdev)
  359. {
  360. struct device_node *np = pdev->dev.of_node;
  361. struct spi_master *master;
  362. struct fsl_dspi *dspi;
  363. struct resource *res;
  364. void __iomem *base;
  365. int ret = 0, cs_num, bus_num;
  366. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_dspi));
  367. if (!master)
  368. return -ENOMEM;
  369. dspi = spi_master_get_devdata(master);
  370. dspi->pdev = pdev;
  371. dspi->bitbang.master = master;
  372. dspi->bitbang.chipselect = dspi_chipselect;
  373. dspi->bitbang.setup_transfer = dspi_setup_transfer;
  374. dspi->bitbang.txrx_bufs = dspi_txrx_transfer;
  375. dspi->bitbang.master->setup = dspi_setup;
  376. dspi->bitbang.master->dev.of_node = pdev->dev.of_node;
  377. master->mode_bits = SPI_CPOL | SPI_CPHA;
  378. master->bits_per_word_mask = SPI_BPW_MASK(4) | SPI_BPW_MASK(8) |
  379. SPI_BPW_MASK(16);
  380. ret = of_property_read_u32(np, "spi-num-chipselects", &cs_num);
  381. if (ret < 0) {
  382. dev_err(&pdev->dev, "can't get spi-num-chipselects\n");
  383. goto out_master_put;
  384. }
  385. master->num_chipselect = cs_num;
  386. ret = of_property_read_u32(np, "bus-num", &bus_num);
  387. if (ret < 0) {
  388. dev_err(&pdev->dev, "can't get bus-num\n");
  389. goto out_master_put;
  390. }
  391. master->bus_num = bus_num;
  392. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  393. base = devm_ioremap_resource(&pdev->dev, res);
  394. if (IS_ERR(base)) {
  395. ret = PTR_ERR(base);
  396. goto out_master_put;
  397. }
  398. dspi->regmap = devm_regmap_init_mmio_clk(&pdev->dev, "dspi", base,
  399. &dspi_regmap_config);
  400. if (IS_ERR(dspi->regmap)) {
  401. dev_err(&pdev->dev, "failed to init regmap: %ld\n",
  402. PTR_ERR(dspi->regmap));
  403. return PTR_ERR(dspi->regmap);
  404. }
  405. dspi->irq = platform_get_irq(pdev, 0);
  406. if (dspi->irq < 0) {
  407. dev_err(&pdev->dev, "can't get platform irq\n");
  408. ret = dspi->irq;
  409. goto out_master_put;
  410. }
  411. ret = devm_request_irq(&pdev->dev, dspi->irq, dspi_interrupt, 0,
  412. pdev->name, dspi);
  413. if (ret < 0) {
  414. dev_err(&pdev->dev, "Unable to attach DSPI interrupt\n");
  415. goto out_master_put;
  416. }
  417. dspi->clk = devm_clk_get(&pdev->dev, "dspi");
  418. if (IS_ERR(dspi->clk)) {
  419. ret = PTR_ERR(dspi->clk);
  420. dev_err(&pdev->dev, "unable to get clock\n");
  421. goto out_master_put;
  422. }
  423. clk_prepare_enable(dspi->clk);
  424. init_waitqueue_head(&dspi->waitq);
  425. platform_set_drvdata(pdev, master);
  426. ret = spi_bitbang_start(&dspi->bitbang);
  427. if (ret != 0) {
  428. dev_err(&pdev->dev, "Problem registering DSPI master\n");
  429. goto out_clk_put;
  430. }
  431. return ret;
  432. out_clk_put:
  433. clk_disable_unprepare(dspi->clk);
  434. out_master_put:
  435. spi_master_put(master);
  436. return ret;
  437. }
  438. static int dspi_remove(struct platform_device *pdev)
  439. {
  440. struct spi_master *master = platform_get_drvdata(pdev);
  441. struct fsl_dspi *dspi = spi_master_get_devdata(master);
  442. /* Disconnect from the SPI framework */
  443. spi_bitbang_stop(&dspi->bitbang);
  444. clk_disable_unprepare(dspi->clk);
  445. spi_master_put(dspi->bitbang.master);
  446. return 0;
  447. }
  448. static struct platform_driver fsl_dspi_driver = {
  449. .driver.name = DRIVER_NAME,
  450. .driver.of_match_table = fsl_dspi_dt_ids,
  451. .driver.owner = THIS_MODULE,
  452. .driver.pm = &dspi_pm,
  453. .probe = dspi_probe,
  454. .remove = dspi_remove,
  455. };
  456. module_platform_driver(fsl_dspi_driver);
  457. MODULE_DESCRIPTION("Freescale DSPI Controller Driver");
  458. MODULE_LICENSE("GPL");
  459. MODULE_ALIAS("platform:" DRIVER_NAME);