spi-fsl-lpspi.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * Freescale i.MX7ULP LPSPI driver
  3. *
  4. * Copyright 2016 Freescale Semiconductor, Inc.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/completion.h>
  19. #include <linux/delay.h>
  20. #include <linux/err.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/io.h>
  23. #include <linux/irq.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/of.h>
  27. #include <linux/of_device.h>
  28. #include <linux/platform_device.h>
  29. #include <linux/slab.h>
  30. #include <linux/spi/spi.h>
  31. #include <linux/spi/spi_bitbang.h>
  32. #include <linux/types.h>
  33. #define DRIVER_NAME "fsl_lpspi"
  34. /* i.MX7ULP LPSPI registers */
  35. #define IMX7ULP_VERID 0x0
  36. #define IMX7ULP_PARAM 0x4
  37. #define IMX7ULP_CR 0x10
  38. #define IMX7ULP_SR 0x14
  39. #define IMX7ULP_IER 0x18
  40. #define IMX7ULP_DER 0x1c
  41. #define IMX7ULP_CFGR0 0x20
  42. #define IMX7ULP_CFGR1 0x24
  43. #define IMX7ULP_DMR0 0x30
  44. #define IMX7ULP_DMR1 0x34
  45. #define IMX7ULP_CCR 0x40
  46. #define IMX7ULP_FCR 0x58
  47. #define IMX7ULP_FSR 0x5c
  48. #define IMX7ULP_TCR 0x60
  49. #define IMX7ULP_TDR 0x64
  50. #define IMX7ULP_RSR 0x70
  51. #define IMX7ULP_RDR 0x74
  52. /* General control register field define */
  53. #define CR_RRF BIT(9)
  54. #define CR_RTF BIT(8)
  55. #define CR_RST BIT(1)
  56. #define CR_MEN BIT(0)
  57. #define SR_TCF BIT(10)
  58. #define SR_RDF BIT(1)
  59. #define SR_TDF BIT(0)
  60. #define IER_TCIE BIT(10)
  61. #define IER_RDIE BIT(1)
  62. #define IER_TDIE BIT(0)
  63. #define CFGR1_PCSCFG BIT(27)
  64. #define CFGR1_PCSPOL BIT(8)
  65. #define CFGR1_NOSTALL BIT(3)
  66. #define CFGR1_MASTER BIT(0)
  67. #define RSR_RXEMPTY BIT(1)
  68. #define TCR_CPOL BIT(31)
  69. #define TCR_CPHA BIT(30)
  70. #define TCR_CONT BIT(21)
  71. #define TCR_CONTC BIT(20)
  72. #define TCR_RXMSK BIT(19)
  73. #define TCR_TXMSK BIT(18)
  74. static int clkdivs[] = {1, 2, 4, 8, 16, 32, 64, 128};
  75. struct lpspi_config {
  76. u8 bpw;
  77. u8 chip_select;
  78. u8 prescale;
  79. u16 mode;
  80. u32 speed_hz;
  81. };
  82. struct fsl_lpspi_data {
  83. struct device *dev;
  84. void __iomem *base;
  85. struct clk *clk;
  86. void *rx_buf;
  87. const void *tx_buf;
  88. void (*tx)(struct fsl_lpspi_data *);
  89. void (*rx)(struct fsl_lpspi_data *);
  90. u32 remain;
  91. u8 txfifosize;
  92. u8 rxfifosize;
  93. struct lpspi_config config;
  94. struct completion xfer_done;
  95. };
  96. static const struct of_device_id fsl_lpspi_dt_ids[] = {
  97. { .compatible = "fsl,imx7ulp-spi", },
  98. { /* sentinel */ }
  99. };
  100. MODULE_DEVICE_TABLE(of, fsl_lpspi_dt_ids);
  101. #define LPSPI_BUF_RX(type) \
  102. static void fsl_lpspi_buf_rx_##type(struct fsl_lpspi_data *fsl_lpspi) \
  103. { \
  104. unsigned int val = readl(fsl_lpspi->base + IMX7ULP_RDR); \
  105. \
  106. if (fsl_lpspi->rx_buf) { \
  107. *(type *)fsl_lpspi->rx_buf = val; \
  108. fsl_lpspi->rx_buf += sizeof(type); \
  109. } \
  110. }
  111. #define LPSPI_BUF_TX(type) \
  112. static void fsl_lpspi_buf_tx_##type(struct fsl_lpspi_data *fsl_lpspi) \
  113. { \
  114. type val = 0; \
  115. \
  116. if (fsl_lpspi->tx_buf) { \
  117. val = *(type *)fsl_lpspi->tx_buf; \
  118. fsl_lpspi->tx_buf += sizeof(type); \
  119. } \
  120. \
  121. fsl_lpspi->remain -= sizeof(type); \
  122. writel(val, fsl_lpspi->base + IMX7ULP_TDR); \
  123. }
  124. LPSPI_BUF_RX(u8)
  125. LPSPI_BUF_TX(u8)
  126. LPSPI_BUF_RX(u16)
  127. LPSPI_BUF_TX(u16)
  128. LPSPI_BUF_RX(u32)
  129. LPSPI_BUF_TX(u32)
  130. static void fsl_lpspi_intctrl(struct fsl_lpspi_data *fsl_lpspi,
  131. unsigned int enable)
  132. {
  133. writel(enable, fsl_lpspi->base + IMX7ULP_IER);
  134. }
  135. static int lpspi_prepare_xfer_hardware(struct spi_master *master)
  136. {
  137. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(master);
  138. return clk_prepare_enable(fsl_lpspi->clk);
  139. }
  140. static int lpspi_unprepare_xfer_hardware(struct spi_master *master)
  141. {
  142. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(master);
  143. clk_disable_unprepare(fsl_lpspi->clk);
  144. return 0;
  145. }
  146. static int fsl_lpspi_txfifo_empty(struct fsl_lpspi_data *fsl_lpspi)
  147. {
  148. u32 txcnt;
  149. unsigned long orig_jiffies = jiffies;
  150. do {
  151. txcnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff;
  152. if (time_after(jiffies, orig_jiffies + msecs_to_jiffies(500))) {
  153. dev_dbg(fsl_lpspi->dev, "txfifo empty timeout\n");
  154. return -ETIMEDOUT;
  155. }
  156. cond_resched();
  157. } while (txcnt);
  158. return 0;
  159. }
  160. static void fsl_lpspi_write_tx_fifo(struct fsl_lpspi_data *fsl_lpspi)
  161. {
  162. u8 txfifo_cnt;
  163. txfifo_cnt = readl(fsl_lpspi->base + IMX7ULP_FSR) & 0xff;
  164. while (txfifo_cnt < fsl_lpspi->txfifosize) {
  165. if (!fsl_lpspi->remain)
  166. break;
  167. fsl_lpspi->tx(fsl_lpspi);
  168. txfifo_cnt++;
  169. }
  170. if (!fsl_lpspi->remain && (txfifo_cnt < fsl_lpspi->txfifosize))
  171. writel(0, fsl_lpspi->base + IMX7ULP_TDR);
  172. else
  173. fsl_lpspi_intctrl(fsl_lpspi, IER_TDIE);
  174. }
  175. static void fsl_lpspi_read_rx_fifo(struct fsl_lpspi_data *fsl_lpspi)
  176. {
  177. while (!(readl(fsl_lpspi->base + IMX7ULP_RSR) & RSR_RXEMPTY))
  178. fsl_lpspi->rx(fsl_lpspi);
  179. }
  180. static void fsl_lpspi_set_cmd(struct fsl_lpspi_data *fsl_lpspi,
  181. bool is_first_xfer)
  182. {
  183. u32 temp = 0;
  184. temp |= fsl_lpspi->config.bpw - 1;
  185. temp |= fsl_lpspi->config.prescale << 27;
  186. temp |= (fsl_lpspi->config.mode & 0x3) << 30;
  187. temp |= (fsl_lpspi->config.chip_select & 0x3) << 24;
  188. /*
  189. * Set TCR_CONT will keep SS asserted after current transfer.
  190. * For the first transfer, clear TCR_CONTC to assert SS.
  191. * For subsequent transfer, set TCR_CONTC to keep SS asserted.
  192. */
  193. temp |= TCR_CONT;
  194. if (is_first_xfer)
  195. temp &= ~TCR_CONTC;
  196. else
  197. temp |= TCR_CONTC;
  198. writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
  199. dev_dbg(fsl_lpspi->dev, "TCR=0x%x\n", temp);
  200. }
  201. static void fsl_lpspi_set_watermark(struct fsl_lpspi_data *fsl_lpspi)
  202. {
  203. u32 temp;
  204. temp = fsl_lpspi->txfifosize >> 1 | (fsl_lpspi->rxfifosize >> 1) << 16;
  205. writel(temp, fsl_lpspi->base + IMX7ULP_FCR);
  206. dev_dbg(fsl_lpspi->dev, "FCR=0x%x\n", temp);
  207. }
  208. static int fsl_lpspi_set_bitrate(struct fsl_lpspi_data *fsl_lpspi)
  209. {
  210. struct lpspi_config config = fsl_lpspi->config;
  211. unsigned int perclk_rate, scldiv;
  212. u8 prescale;
  213. perclk_rate = clk_get_rate(fsl_lpspi->clk);
  214. for (prescale = 0; prescale < 8; prescale++) {
  215. scldiv = perclk_rate /
  216. (clkdivs[prescale] * config.speed_hz) - 2;
  217. if (scldiv < 256) {
  218. fsl_lpspi->config.prescale = prescale;
  219. break;
  220. }
  221. }
  222. if (prescale == 8 && scldiv >= 256)
  223. return -EINVAL;
  224. writel(scldiv, fsl_lpspi->base + IMX7ULP_CCR);
  225. dev_dbg(fsl_lpspi->dev, "perclk=%d, speed=%d, prescale =%d, scldiv=%d\n",
  226. perclk_rate, config.speed_hz, prescale, scldiv);
  227. return 0;
  228. }
  229. static int fsl_lpspi_config(struct fsl_lpspi_data *fsl_lpspi)
  230. {
  231. u32 temp;
  232. int ret;
  233. temp = CR_RST;
  234. writel(temp, fsl_lpspi->base + IMX7ULP_CR);
  235. writel(0, fsl_lpspi->base + IMX7ULP_CR);
  236. ret = fsl_lpspi_set_bitrate(fsl_lpspi);
  237. if (ret)
  238. return ret;
  239. fsl_lpspi_set_watermark(fsl_lpspi);
  240. temp = CFGR1_PCSCFG | CFGR1_MASTER;
  241. if (fsl_lpspi->config.mode & SPI_CS_HIGH)
  242. temp |= CFGR1_PCSPOL;
  243. writel(temp, fsl_lpspi->base + IMX7ULP_CFGR1);
  244. temp = readl(fsl_lpspi->base + IMX7ULP_CR);
  245. temp |= CR_RRF | CR_RTF | CR_MEN;
  246. writel(temp, fsl_lpspi->base + IMX7ULP_CR);
  247. return 0;
  248. }
  249. static void fsl_lpspi_setup_transfer(struct spi_device *spi,
  250. struct spi_transfer *t)
  251. {
  252. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(spi->master);
  253. fsl_lpspi->config.mode = spi->mode;
  254. fsl_lpspi->config.bpw = t ? t->bits_per_word : spi->bits_per_word;
  255. fsl_lpspi->config.speed_hz = t ? t->speed_hz : spi->max_speed_hz;
  256. fsl_lpspi->config.chip_select = spi->chip_select;
  257. if (!fsl_lpspi->config.speed_hz)
  258. fsl_lpspi->config.speed_hz = spi->max_speed_hz;
  259. if (!fsl_lpspi->config.bpw)
  260. fsl_lpspi->config.bpw = spi->bits_per_word;
  261. /* Initialize the functions for transfer */
  262. if (fsl_lpspi->config.bpw <= 8) {
  263. fsl_lpspi->rx = fsl_lpspi_buf_rx_u8;
  264. fsl_lpspi->tx = fsl_lpspi_buf_tx_u8;
  265. } else if (fsl_lpspi->config.bpw <= 16) {
  266. fsl_lpspi->rx = fsl_lpspi_buf_rx_u16;
  267. fsl_lpspi->tx = fsl_lpspi_buf_tx_u16;
  268. } else {
  269. fsl_lpspi->rx = fsl_lpspi_buf_rx_u32;
  270. fsl_lpspi->tx = fsl_lpspi_buf_tx_u32;
  271. }
  272. fsl_lpspi_config(fsl_lpspi);
  273. }
  274. static int fsl_lpspi_transfer_one(struct spi_master *master,
  275. struct spi_device *spi,
  276. struct spi_transfer *t)
  277. {
  278. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(master);
  279. int ret;
  280. fsl_lpspi->tx_buf = t->tx_buf;
  281. fsl_lpspi->rx_buf = t->rx_buf;
  282. fsl_lpspi->remain = t->len;
  283. reinit_completion(&fsl_lpspi->xfer_done);
  284. fsl_lpspi_write_tx_fifo(fsl_lpspi);
  285. ret = wait_for_completion_timeout(&fsl_lpspi->xfer_done, HZ);
  286. if (!ret) {
  287. dev_dbg(fsl_lpspi->dev, "wait for completion timeout\n");
  288. return -ETIMEDOUT;
  289. }
  290. ret = fsl_lpspi_txfifo_empty(fsl_lpspi);
  291. if (ret)
  292. return ret;
  293. fsl_lpspi_read_rx_fifo(fsl_lpspi);
  294. return 0;
  295. }
  296. static int fsl_lpspi_transfer_one_msg(struct spi_master *master,
  297. struct spi_message *msg)
  298. {
  299. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(master);
  300. struct spi_device *spi = msg->spi;
  301. struct spi_transfer *xfer;
  302. bool is_first_xfer = true;
  303. u32 temp;
  304. int ret = 0;
  305. msg->status = 0;
  306. msg->actual_length = 0;
  307. list_for_each_entry(xfer, &msg->transfers, transfer_list) {
  308. fsl_lpspi_setup_transfer(spi, xfer);
  309. fsl_lpspi_set_cmd(fsl_lpspi, is_first_xfer);
  310. is_first_xfer = false;
  311. ret = fsl_lpspi_transfer_one(master, spi, xfer);
  312. if (ret < 0)
  313. goto complete;
  314. msg->actual_length += xfer->len;
  315. }
  316. complete:
  317. /* de-assert SS, then finalize current message */
  318. temp = readl(fsl_lpspi->base + IMX7ULP_TCR);
  319. temp &= ~TCR_CONTC;
  320. writel(temp, fsl_lpspi->base + IMX7ULP_TCR);
  321. msg->status = ret;
  322. spi_finalize_current_message(master);
  323. return ret;
  324. }
  325. static irqreturn_t fsl_lpspi_isr(int irq, void *dev_id)
  326. {
  327. struct fsl_lpspi_data *fsl_lpspi = dev_id;
  328. u32 temp;
  329. fsl_lpspi_intctrl(fsl_lpspi, 0);
  330. temp = readl(fsl_lpspi->base + IMX7ULP_SR);
  331. fsl_lpspi_read_rx_fifo(fsl_lpspi);
  332. if (temp & SR_TDF) {
  333. fsl_lpspi_write_tx_fifo(fsl_lpspi);
  334. if (!fsl_lpspi->remain)
  335. complete(&fsl_lpspi->xfer_done);
  336. return IRQ_HANDLED;
  337. }
  338. return IRQ_NONE;
  339. }
  340. static int fsl_lpspi_probe(struct platform_device *pdev)
  341. {
  342. struct fsl_lpspi_data *fsl_lpspi;
  343. struct spi_master *master;
  344. struct resource *res;
  345. int ret, irq;
  346. u32 temp;
  347. master = spi_alloc_master(&pdev->dev, sizeof(struct fsl_lpspi_data));
  348. if (!master)
  349. return -ENOMEM;
  350. platform_set_drvdata(pdev, master);
  351. master->bits_per_word_mask = SPI_BPW_RANGE_MASK(8, 32);
  352. master->bus_num = pdev->id;
  353. fsl_lpspi = spi_master_get_devdata(master);
  354. fsl_lpspi->dev = &pdev->dev;
  355. master->transfer_one_message = fsl_lpspi_transfer_one_msg;
  356. master->prepare_transfer_hardware = lpspi_prepare_xfer_hardware;
  357. master->unprepare_transfer_hardware = lpspi_unprepare_xfer_hardware;
  358. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
  359. master->flags = SPI_MASTER_MUST_RX | SPI_MASTER_MUST_TX;
  360. master->dev.of_node = pdev->dev.of_node;
  361. master->bus_num = pdev->id;
  362. init_completion(&fsl_lpspi->xfer_done);
  363. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  364. fsl_lpspi->base = devm_ioremap_resource(&pdev->dev, res);
  365. if (IS_ERR(fsl_lpspi->base)) {
  366. ret = PTR_ERR(fsl_lpspi->base);
  367. goto out_master_put;
  368. }
  369. irq = platform_get_irq(pdev, 0);
  370. if (irq < 0) {
  371. ret = irq;
  372. goto out_master_put;
  373. }
  374. ret = devm_request_irq(&pdev->dev, irq, fsl_lpspi_isr, 0,
  375. dev_name(&pdev->dev), fsl_lpspi);
  376. if (ret) {
  377. dev_err(&pdev->dev, "can't get irq%d: %d\n", irq, ret);
  378. goto out_master_put;
  379. }
  380. fsl_lpspi->clk = devm_clk_get(&pdev->dev, "ipg");
  381. if (IS_ERR(fsl_lpspi->clk)) {
  382. ret = PTR_ERR(fsl_lpspi->clk);
  383. goto out_master_put;
  384. }
  385. ret = clk_prepare_enable(fsl_lpspi->clk);
  386. if (ret) {
  387. dev_err(&pdev->dev, "can't enable lpspi clock, ret=%d\n", ret);
  388. goto out_master_put;
  389. }
  390. temp = readl(fsl_lpspi->base + IMX7ULP_PARAM);
  391. fsl_lpspi->txfifosize = 1 << (temp & 0x0f);
  392. fsl_lpspi->rxfifosize = 1 << ((temp >> 8) & 0x0f);
  393. clk_disable_unprepare(fsl_lpspi->clk);
  394. ret = devm_spi_register_master(&pdev->dev, master);
  395. if (ret < 0) {
  396. dev_err(&pdev->dev, "spi_register_master error.\n");
  397. goto out_master_put;
  398. }
  399. return 0;
  400. out_master_put:
  401. spi_master_put(master);
  402. return ret;
  403. }
  404. static int fsl_lpspi_remove(struct platform_device *pdev)
  405. {
  406. struct spi_master *master = platform_get_drvdata(pdev);
  407. struct fsl_lpspi_data *fsl_lpspi = spi_master_get_devdata(master);
  408. clk_disable_unprepare(fsl_lpspi->clk);
  409. return 0;
  410. }
  411. static struct platform_driver fsl_lpspi_driver = {
  412. .driver = {
  413. .name = DRIVER_NAME,
  414. .of_match_table = fsl_lpspi_dt_ids,
  415. },
  416. .probe = fsl_lpspi_probe,
  417. .remove = fsl_lpspi_remove,
  418. };
  419. module_platform_driver(fsl_lpspi_driver);
  420. MODULE_DESCRIPTION("LPSPI Master Controller driver");
  421. MODULE_AUTHOR("Gao Pan <pandy.gao@nxp.com>");
  422. MODULE_LICENSE("GPL");