spi-fsl-dspi.c 14 KB

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