spi-fsl-lpspi.c 12 KB

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