spi-ti-qspi.c 14 KB

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