spi-ti-qspi.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. /*
  2. * TI QSPI driver
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * Author: Sourav Poddar <sourav.poddar@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GPLv2.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR /PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/init.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/module.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/dmaengine.h>
  23. #include <linux/omap-dma.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/err.h>
  26. #include <linux/clk.h>
  27. #include <linux/io.h>
  28. #include <linux/slab.h>
  29. #include <linux/pm_runtime.h>
  30. #include <linux/of.h>
  31. #include <linux/of_device.h>
  32. #include <linux/pinctrl/consumer.h>
  33. #include <linux/spi/spi.h>
  34. struct ti_qspi_regs {
  35. u32 clkctrl;
  36. };
  37. struct ti_qspi {
  38. struct completion transfer_complete;
  39. /* list synchronization */
  40. struct mutex list_lock;
  41. struct spi_master *master;
  42. void __iomem *base;
  43. void __iomem *ctrl_base;
  44. void __iomem *mmap_base;
  45. struct clk *fclk;
  46. struct device *dev;
  47. struct ti_qspi_regs ctx_reg;
  48. u32 spi_max_frequency;
  49. u32 cmd;
  50. u32 dc;
  51. bool ctrl_mod;
  52. };
  53. #define QSPI_PID (0x0)
  54. #define QSPI_SYSCONFIG (0x10)
  55. #define QSPI_INTR_STATUS_RAW_SET (0x20)
  56. #define QSPI_INTR_STATUS_ENABLED_CLEAR (0x24)
  57. #define QSPI_INTR_ENABLE_SET_REG (0x28)
  58. #define QSPI_INTR_ENABLE_CLEAR_REG (0x2c)
  59. #define QSPI_SPI_CLOCK_CNTRL_REG (0x40)
  60. #define QSPI_SPI_DC_REG (0x44)
  61. #define QSPI_SPI_CMD_REG (0x48)
  62. #define QSPI_SPI_STATUS_REG (0x4c)
  63. #define QSPI_SPI_DATA_REG (0x50)
  64. #define QSPI_SPI_SETUP0_REG (0x54)
  65. #define QSPI_SPI_SWITCH_REG (0x64)
  66. #define QSPI_SPI_SETUP1_REG (0x58)
  67. #define QSPI_SPI_SETUP2_REG (0x5c)
  68. #define QSPI_SPI_SETUP3_REG (0x60)
  69. #define QSPI_SPI_DATA_REG_1 (0x68)
  70. #define QSPI_SPI_DATA_REG_2 (0x6c)
  71. #define QSPI_SPI_DATA_REG_3 (0x70)
  72. #define QSPI_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
  73. #define QSPI_FCLK 192000000
  74. /* Clock Control */
  75. #define QSPI_CLK_EN (1 << 31)
  76. #define QSPI_CLK_DIV_MAX 0xffff
  77. /* Command */
  78. #define QSPI_EN_CS(n) (n << 28)
  79. #define QSPI_WLEN(n) ((n - 1) << 19)
  80. #define QSPI_3_PIN (1 << 18)
  81. #define QSPI_RD_SNGL (1 << 16)
  82. #define QSPI_WR_SNGL (2 << 16)
  83. #define QSPI_RD_DUAL (3 << 16)
  84. #define QSPI_RD_QUAD (7 << 16)
  85. #define QSPI_INVAL (4 << 16)
  86. #define QSPI_WC_CMD_INT_EN (1 << 14)
  87. #define QSPI_FLEN(n) ((n - 1) << 0)
  88. /* STATUS REGISTER */
  89. #define BUSY 0x01
  90. #define WC 0x02
  91. /* INTERRUPT REGISTER */
  92. #define QSPI_WC_INT_EN (1 << 1)
  93. #define QSPI_WC_INT_DISABLE (1 << 1)
  94. /* Device Control */
  95. #define QSPI_DD(m, n) (m << (3 + n * 8))
  96. #define QSPI_CKPHA(n) (1 << (2 + n * 8))
  97. #define QSPI_CSPOL(n) (1 << (1 + n * 8))
  98. #define QSPI_CKPOL(n) (1 << (n * 8))
  99. #define QSPI_FRAME 4096
  100. #define QSPI_AUTOSUSPEND_TIMEOUT 2000
  101. static inline unsigned long ti_qspi_read(struct ti_qspi *qspi,
  102. unsigned long reg)
  103. {
  104. return readl(qspi->base + reg);
  105. }
  106. static inline void ti_qspi_write(struct ti_qspi *qspi,
  107. unsigned long val, unsigned long reg)
  108. {
  109. writel(val, qspi->base + reg);
  110. }
  111. static int ti_qspi_setup(struct spi_device *spi)
  112. {
  113. struct ti_qspi *qspi = spi_master_get_devdata(spi->master);
  114. struct ti_qspi_regs *ctx_reg = &qspi->ctx_reg;
  115. int clk_div = 0, ret;
  116. u32 clk_ctrl_reg, clk_rate, clk_mask;
  117. if (spi->master->busy) {
  118. dev_dbg(qspi->dev, "master busy doing other trasnfers\n");
  119. return -EBUSY;
  120. }
  121. if (!qspi->spi_max_frequency) {
  122. dev_err(qspi->dev, "spi max frequency not defined\n");
  123. return -EINVAL;
  124. }
  125. clk_rate = clk_get_rate(qspi->fclk);
  126. clk_div = DIV_ROUND_UP(clk_rate, qspi->spi_max_frequency) - 1;
  127. if (clk_div < 0) {
  128. dev_dbg(qspi->dev, "clock divider < 0, using /1 divider\n");
  129. return -EINVAL;
  130. }
  131. if (clk_div > QSPI_CLK_DIV_MAX) {
  132. dev_dbg(qspi->dev, "clock divider >%d , using /%d divider\n",
  133. QSPI_CLK_DIV_MAX, QSPI_CLK_DIV_MAX + 1);
  134. return -EINVAL;
  135. }
  136. dev_dbg(qspi->dev, "hz: %d, clock divider %d\n",
  137. qspi->spi_max_frequency, clk_div);
  138. ret = pm_runtime_get_sync(qspi->dev);
  139. if (ret < 0) {
  140. dev_err(qspi->dev, "pm_runtime_get_sync() failed\n");
  141. return ret;
  142. }
  143. clk_ctrl_reg = ti_qspi_read(qspi, QSPI_SPI_CLOCK_CNTRL_REG);
  144. clk_ctrl_reg &= ~QSPI_CLK_EN;
  145. /* disable SCLK */
  146. ti_qspi_write(qspi, clk_ctrl_reg, QSPI_SPI_CLOCK_CNTRL_REG);
  147. /* enable SCLK */
  148. clk_mask = QSPI_CLK_EN | clk_div;
  149. ti_qspi_write(qspi, clk_mask, QSPI_SPI_CLOCK_CNTRL_REG);
  150. ctx_reg->clkctrl = clk_mask;
  151. pm_runtime_mark_last_busy(qspi->dev);
  152. ret = pm_runtime_put_autosuspend(qspi->dev);
  153. if (ret < 0) {
  154. dev_err(qspi->dev, "pm_runtime_put_autosuspend() failed\n");
  155. return ret;
  156. }
  157. return 0;
  158. }
  159. static void ti_qspi_restore_ctx(struct ti_qspi *qspi)
  160. {
  161. struct ti_qspi_regs *ctx_reg = &qspi->ctx_reg;
  162. ti_qspi_write(qspi, ctx_reg->clkctrl, QSPI_SPI_CLOCK_CNTRL_REG);
  163. }
  164. static inline u32 qspi_is_busy(struct ti_qspi *qspi)
  165. {
  166. u32 stat;
  167. unsigned long timeout = jiffies + QSPI_COMPLETION_TIMEOUT;
  168. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  169. while ((stat & BUSY) && time_after(timeout, jiffies)) {
  170. cpu_relax();
  171. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  172. }
  173. WARN(stat & BUSY, "qspi busy\n");
  174. return stat & BUSY;
  175. }
  176. static int qspi_write_msg(struct ti_qspi *qspi, struct spi_transfer *t)
  177. {
  178. int wlen, count;
  179. unsigned int cmd;
  180. const u8 *txbuf;
  181. txbuf = t->tx_buf;
  182. cmd = qspi->cmd | QSPI_WR_SNGL;
  183. count = t->len;
  184. wlen = t->bits_per_word >> 3; /* in bytes */
  185. while (count) {
  186. if (qspi_is_busy(qspi))
  187. return -EBUSY;
  188. switch (wlen) {
  189. case 1:
  190. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %02x\n",
  191. cmd, qspi->dc, *txbuf);
  192. writeb(*txbuf, qspi->base + QSPI_SPI_DATA_REG);
  193. break;
  194. case 2:
  195. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %04x\n",
  196. cmd, qspi->dc, *txbuf);
  197. writew(*((u16 *)txbuf), qspi->base + QSPI_SPI_DATA_REG);
  198. break;
  199. case 4:
  200. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %08x\n",
  201. cmd, qspi->dc, *txbuf);
  202. writel(*((u32 *)txbuf), qspi->base + QSPI_SPI_DATA_REG);
  203. break;
  204. }
  205. ti_qspi_write(qspi, cmd, QSPI_SPI_CMD_REG);
  206. if (!wait_for_completion_timeout(&qspi->transfer_complete,
  207. QSPI_COMPLETION_TIMEOUT)) {
  208. dev_err(qspi->dev, "write timed out\n");
  209. return -ETIMEDOUT;
  210. }
  211. txbuf += wlen;
  212. count -= wlen;
  213. }
  214. return 0;
  215. }
  216. static int qspi_read_msg(struct ti_qspi *qspi, struct spi_transfer *t)
  217. {
  218. int wlen, count;
  219. unsigned int cmd;
  220. u8 *rxbuf;
  221. rxbuf = t->rx_buf;
  222. cmd = qspi->cmd;
  223. switch (t->rx_nbits) {
  224. case SPI_NBITS_DUAL:
  225. cmd |= QSPI_RD_DUAL;
  226. break;
  227. case SPI_NBITS_QUAD:
  228. cmd |= QSPI_RD_QUAD;
  229. break;
  230. default:
  231. cmd |= QSPI_RD_SNGL;
  232. break;
  233. }
  234. count = t->len;
  235. wlen = t->bits_per_word >> 3; /* in bytes */
  236. while (count) {
  237. dev_dbg(qspi->dev, "rx cmd %08x dc %08x\n", cmd, qspi->dc);
  238. if (qspi_is_busy(qspi))
  239. return -EBUSY;
  240. ti_qspi_write(qspi, cmd, QSPI_SPI_CMD_REG);
  241. if (!wait_for_completion_timeout(&qspi->transfer_complete,
  242. QSPI_COMPLETION_TIMEOUT)) {
  243. dev_err(qspi->dev, "read timed out\n");
  244. return -ETIMEDOUT;
  245. }
  246. switch (wlen) {
  247. case 1:
  248. *rxbuf = readb(qspi->base + QSPI_SPI_DATA_REG);
  249. break;
  250. case 2:
  251. *((u16 *)rxbuf) = readw(qspi->base + QSPI_SPI_DATA_REG);
  252. break;
  253. case 4:
  254. *((u32 *)rxbuf) = readl(qspi->base + QSPI_SPI_DATA_REG);
  255. break;
  256. }
  257. rxbuf += wlen;
  258. count -= wlen;
  259. }
  260. return 0;
  261. }
  262. static int qspi_transfer_msg(struct ti_qspi *qspi, struct spi_transfer *t)
  263. {
  264. int ret;
  265. if (t->tx_buf) {
  266. ret = qspi_write_msg(qspi, t);
  267. if (ret) {
  268. dev_dbg(qspi->dev, "Error while writing\n");
  269. return ret;
  270. }
  271. }
  272. if (t->rx_buf) {
  273. ret = qspi_read_msg(qspi, t);
  274. if (ret) {
  275. dev_dbg(qspi->dev, "Error while reading\n");
  276. return ret;
  277. }
  278. }
  279. return 0;
  280. }
  281. static int ti_qspi_start_transfer_one(struct spi_master *master,
  282. struct spi_message *m)
  283. {
  284. struct ti_qspi *qspi = spi_master_get_devdata(master);
  285. struct spi_device *spi = m->spi;
  286. struct spi_transfer *t;
  287. int status = 0, ret;
  288. int frame_length;
  289. /* setup device control reg */
  290. qspi->dc = 0;
  291. if (spi->mode & SPI_CPHA)
  292. qspi->dc |= QSPI_CKPHA(spi->chip_select);
  293. if (spi->mode & SPI_CPOL)
  294. qspi->dc |= QSPI_CKPOL(spi->chip_select);
  295. if (spi->mode & SPI_CS_HIGH)
  296. qspi->dc |= QSPI_CSPOL(spi->chip_select);
  297. frame_length = (m->frame_length << 3) / spi->bits_per_word;
  298. frame_length = clamp(frame_length, 0, QSPI_FRAME);
  299. /* setup command reg */
  300. qspi->cmd = 0;
  301. qspi->cmd |= QSPI_EN_CS(spi->chip_select);
  302. qspi->cmd |= QSPI_FLEN(frame_length);
  303. qspi->cmd |= QSPI_WC_CMD_INT_EN;
  304. ti_qspi_write(qspi, QSPI_WC_INT_EN, QSPI_INTR_ENABLE_SET_REG);
  305. ti_qspi_write(qspi, qspi->dc, QSPI_SPI_DC_REG);
  306. mutex_lock(&qspi->list_lock);
  307. list_for_each_entry(t, &m->transfers, transfer_list) {
  308. qspi->cmd |= QSPI_WLEN(t->bits_per_word);
  309. ret = qspi_transfer_msg(qspi, t);
  310. if (ret) {
  311. dev_dbg(qspi->dev, "transfer message failed\n");
  312. mutex_unlock(&qspi->list_lock);
  313. return -EINVAL;
  314. }
  315. m->actual_length += t->len;
  316. }
  317. mutex_unlock(&qspi->list_lock);
  318. m->status = status;
  319. spi_finalize_current_message(master);
  320. ti_qspi_write(qspi, qspi->cmd | QSPI_INVAL, QSPI_SPI_CMD_REG);
  321. return status;
  322. }
  323. static irqreturn_t ti_qspi_isr(int irq, void *dev_id)
  324. {
  325. struct ti_qspi *qspi = dev_id;
  326. u16 int_stat;
  327. u32 stat;
  328. irqreturn_t ret = IRQ_HANDLED;
  329. int_stat = ti_qspi_read(qspi, QSPI_INTR_STATUS_ENABLED_CLEAR);
  330. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  331. if (!int_stat) {
  332. dev_dbg(qspi->dev, "No IRQ triggered\n");
  333. ret = IRQ_NONE;
  334. goto out;
  335. }
  336. ti_qspi_write(qspi, QSPI_WC_INT_DISABLE,
  337. QSPI_INTR_STATUS_ENABLED_CLEAR);
  338. if (stat & WC)
  339. complete(&qspi->transfer_complete);
  340. out:
  341. return ret;
  342. }
  343. static int ti_qspi_runtime_resume(struct device *dev)
  344. {
  345. struct ti_qspi *qspi;
  346. qspi = dev_get_drvdata(dev);
  347. ti_qspi_restore_ctx(qspi);
  348. return 0;
  349. }
  350. static const struct of_device_id ti_qspi_match[] = {
  351. {.compatible = "ti,dra7xxx-qspi" },
  352. {.compatible = "ti,am4372-qspi" },
  353. {},
  354. };
  355. MODULE_DEVICE_TABLE(of, ti_qspi_match);
  356. static int ti_qspi_probe(struct platform_device *pdev)
  357. {
  358. struct ti_qspi *qspi;
  359. struct spi_master *master;
  360. struct resource *r, *res_ctrl, *res_mmap;
  361. struct device_node *np = pdev->dev.of_node;
  362. u32 max_freq;
  363. int ret = 0, num_cs, irq;
  364. master = spi_alloc_master(&pdev->dev, sizeof(*qspi));
  365. if (!master)
  366. return -ENOMEM;
  367. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD;
  368. master->flags = SPI_MASTER_HALF_DUPLEX;
  369. master->setup = ti_qspi_setup;
  370. master->auto_runtime_pm = true;
  371. master->transfer_one_message = ti_qspi_start_transfer_one;
  372. master->dev.of_node = pdev->dev.of_node;
  373. master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
  374. SPI_BPW_MASK(8);
  375. if (!of_property_read_u32(np, "num-cs", &num_cs))
  376. master->num_chipselect = num_cs;
  377. qspi = spi_master_get_devdata(master);
  378. qspi->master = master;
  379. qspi->dev = &pdev->dev;
  380. platform_set_drvdata(pdev, qspi);
  381. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
  382. if (r == NULL) {
  383. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  384. if (r == NULL) {
  385. dev_err(&pdev->dev, "missing platform data\n");
  386. return -ENODEV;
  387. }
  388. }
  389. res_mmap = platform_get_resource_byname(pdev,
  390. IORESOURCE_MEM, "qspi_mmap");
  391. if (res_mmap == NULL) {
  392. res_mmap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  393. if (res_mmap == NULL) {
  394. dev_err(&pdev->dev,
  395. "memory mapped resource not required\n");
  396. }
  397. }
  398. res_ctrl = platform_get_resource_byname(pdev,
  399. IORESOURCE_MEM, "qspi_ctrlmod");
  400. if (res_ctrl == NULL) {
  401. res_ctrl = platform_get_resource(pdev, IORESOURCE_MEM, 2);
  402. if (res_ctrl == NULL) {
  403. dev_dbg(&pdev->dev,
  404. "control module resources not required\n");
  405. }
  406. }
  407. irq = platform_get_irq(pdev, 0);
  408. if (irq < 0) {
  409. dev_err(&pdev->dev, "no irq resource?\n");
  410. return irq;
  411. }
  412. mutex_init(&qspi->list_lock);
  413. qspi->base = devm_ioremap_resource(&pdev->dev, r);
  414. if (IS_ERR(qspi->base)) {
  415. ret = PTR_ERR(qspi->base);
  416. goto free_master;
  417. }
  418. if (res_ctrl) {
  419. qspi->ctrl_mod = true;
  420. qspi->ctrl_base = devm_ioremap_resource(&pdev->dev, res_ctrl);
  421. if (IS_ERR(qspi->ctrl_base)) {
  422. ret = PTR_ERR(qspi->ctrl_base);
  423. goto free_master;
  424. }
  425. }
  426. if (res_mmap) {
  427. qspi->mmap_base = devm_ioremap_resource(&pdev->dev, res_mmap);
  428. if (IS_ERR(qspi->mmap_base)) {
  429. ret = PTR_ERR(qspi->mmap_base);
  430. goto free_master;
  431. }
  432. }
  433. ret = devm_request_irq(&pdev->dev, irq, ti_qspi_isr, 0,
  434. dev_name(&pdev->dev), qspi);
  435. if (ret < 0) {
  436. dev_err(&pdev->dev, "Failed to register ISR for IRQ %d\n",
  437. irq);
  438. goto free_master;
  439. }
  440. qspi->fclk = devm_clk_get(&pdev->dev, "fck");
  441. if (IS_ERR(qspi->fclk)) {
  442. ret = PTR_ERR(qspi->fclk);
  443. dev_err(&pdev->dev, "could not get clk: %d\n", ret);
  444. }
  445. init_completion(&qspi->transfer_complete);
  446. pm_runtime_use_autosuspend(&pdev->dev);
  447. pm_runtime_set_autosuspend_delay(&pdev->dev, QSPI_AUTOSUSPEND_TIMEOUT);
  448. pm_runtime_enable(&pdev->dev);
  449. if (!of_property_read_u32(np, "spi-max-frequency", &max_freq))
  450. qspi->spi_max_frequency = max_freq;
  451. ret = devm_spi_register_master(&pdev->dev, master);
  452. if (ret)
  453. goto free_master;
  454. return 0;
  455. free_master:
  456. spi_master_put(master);
  457. return ret;
  458. }
  459. static int ti_qspi_remove(struct platform_device *pdev)
  460. {
  461. struct ti_qspi *qspi = platform_get_drvdata(pdev);
  462. int ret;
  463. ret = pm_runtime_get_sync(qspi->dev);
  464. if (ret < 0) {
  465. dev_err(qspi->dev, "pm_runtime_get_sync() failed\n");
  466. return ret;
  467. }
  468. ti_qspi_write(qspi, QSPI_WC_INT_DISABLE, QSPI_INTR_ENABLE_CLEAR_REG);
  469. pm_runtime_put(qspi->dev);
  470. pm_runtime_disable(&pdev->dev);
  471. return 0;
  472. }
  473. static const struct dev_pm_ops ti_qspi_pm_ops = {
  474. .runtime_resume = ti_qspi_runtime_resume,
  475. };
  476. static struct platform_driver ti_qspi_driver = {
  477. .probe = ti_qspi_probe,
  478. .remove = ti_qspi_remove,
  479. .driver = {
  480. .name = "ti-qspi",
  481. .pm = &ti_qspi_pm_ops,
  482. .of_match_table = ti_qspi_match,
  483. }
  484. };
  485. module_platform_driver(ti_qspi_driver);
  486. MODULE_AUTHOR("Sourav Poddar <sourav.poddar@ti.com>");
  487. MODULE_LICENSE("GPL v2");
  488. MODULE_DESCRIPTION("TI QSPI controller driver");
  489. MODULE_ALIAS("platform:ti-qspi");