spi-ti-qspi.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867
  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/mfd/syscon.h>
  34. #include <linux/regmap.h>
  35. #include <linux/sizes.h>
  36. #include <linux/spi/spi.h>
  37. #include <linux/spi/spi-mem.h>
  38. struct ti_qspi_regs {
  39. u32 clkctrl;
  40. };
  41. struct ti_qspi {
  42. struct completion transfer_complete;
  43. /* list synchronization */
  44. struct mutex list_lock;
  45. struct spi_master *master;
  46. void __iomem *base;
  47. void __iomem *mmap_base;
  48. size_t mmap_size;
  49. struct regmap *ctrl_base;
  50. unsigned int ctrl_reg;
  51. struct clk *fclk;
  52. struct device *dev;
  53. struct ti_qspi_regs ctx_reg;
  54. dma_addr_t mmap_phys_base;
  55. dma_addr_t rx_bb_dma_addr;
  56. void *rx_bb_addr;
  57. struct dma_chan *rx_chan;
  58. u32 spi_max_frequency;
  59. u32 cmd;
  60. u32 dc;
  61. bool mmap_enabled;
  62. int current_cs;
  63. };
  64. #define QSPI_PID (0x0)
  65. #define QSPI_SYSCONFIG (0x10)
  66. #define QSPI_SPI_CLOCK_CNTRL_REG (0x40)
  67. #define QSPI_SPI_DC_REG (0x44)
  68. #define QSPI_SPI_CMD_REG (0x48)
  69. #define QSPI_SPI_STATUS_REG (0x4c)
  70. #define QSPI_SPI_DATA_REG (0x50)
  71. #define QSPI_SPI_SETUP_REG(n) ((0x54 + 4 * n))
  72. #define QSPI_SPI_SWITCH_REG (0x64)
  73. #define QSPI_SPI_DATA_REG_1 (0x68)
  74. #define QSPI_SPI_DATA_REG_2 (0x6c)
  75. #define QSPI_SPI_DATA_REG_3 (0x70)
  76. #define QSPI_COMPLETION_TIMEOUT msecs_to_jiffies(2000)
  77. #define QSPI_FCLK 192000000
  78. /* Clock Control */
  79. #define QSPI_CLK_EN (1 << 31)
  80. #define QSPI_CLK_DIV_MAX 0xffff
  81. /* Command */
  82. #define QSPI_EN_CS(n) (n << 28)
  83. #define QSPI_WLEN(n) ((n - 1) << 19)
  84. #define QSPI_3_PIN (1 << 18)
  85. #define QSPI_RD_SNGL (1 << 16)
  86. #define QSPI_WR_SNGL (2 << 16)
  87. #define QSPI_RD_DUAL (3 << 16)
  88. #define QSPI_RD_QUAD (7 << 16)
  89. #define QSPI_INVAL (4 << 16)
  90. #define QSPI_FLEN(n) ((n - 1) << 0)
  91. #define QSPI_WLEN_MAX_BITS 128
  92. #define QSPI_WLEN_MAX_BYTES 16
  93. #define QSPI_WLEN_MASK QSPI_WLEN(QSPI_WLEN_MAX_BITS)
  94. /* STATUS REGISTER */
  95. #define BUSY 0x01
  96. #define WC 0x02
  97. /* Device Control */
  98. #define QSPI_DD(m, n) (m << (3 + n * 8))
  99. #define QSPI_CKPHA(n) (1 << (2 + n * 8))
  100. #define QSPI_CSPOL(n) (1 << (1 + n * 8))
  101. #define QSPI_CKPOL(n) (1 << (n * 8))
  102. #define QSPI_FRAME 4096
  103. #define QSPI_AUTOSUSPEND_TIMEOUT 2000
  104. #define MEM_CS_EN(n) ((n + 1) << 8)
  105. #define MEM_CS_MASK (7 << 8)
  106. #define MM_SWITCH 0x1
  107. #define QSPI_SETUP_RD_NORMAL (0x0 << 12)
  108. #define QSPI_SETUP_RD_DUAL (0x1 << 12)
  109. #define QSPI_SETUP_RD_QUAD (0x3 << 12)
  110. #define QSPI_SETUP_ADDR_SHIFT 8
  111. #define QSPI_SETUP_DUMMY_SHIFT 10
  112. #define QSPI_DMA_BUFFER_SIZE SZ_64K
  113. static inline unsigned long ti_qspi_read(struct ti_qspi *qspi,
  114. unsigned long reg)
  115. {
  116. return readl(qspi->base + reg);
  117. }
  118. static inline void ti_qspi_write(struct ti_qspi *qspi,
  119. unsigned long val, unsigned long reg)
  120. {
  121. writel(val, qspi->base + reg);
  122. }
  123. static int ti_qspi_setup(struct spi_device *spi)
  124. {
  125. struct ti_qspi *qspi = spi_master_get_devdata(spi->master);
  126. struct ti_qspi_regs *ctx_reg = &qspi->ctx_reg;
  127. int clk_div = 0, ret;
  128. u32 clk_ctrl_reg, clk_rate, clk_mask;
  129. if (spi->master->busy) {
  130. dev_dbg(qspi->dev, "master busy doing other transfers\n");
  131. return -EBUSY;
  132. }
  133. if (!qspi->spi_max_frequency) {
  134. dev_err(qspi->dev, "spi max frequency not defined\n");
  135. return -EINVAL;
  136. }
  137. clk_rate = clk_get_rate(qspi->fclk);
  138. clk_div = DIV_ROUND_UP(clk_rate, qspi->spi_max_frequency) - 1;
  139. if (clk_div < 0) {
  140. dev_dbg(qspi->dev, "clock divider < 0, using /1 divider\n");
  141. return -EINVAL;
  142. }
  143. if (clk_div > QSPI_CLK_DIV_MAX) {
  144. dev_dbg(qspi->dev, "clock divider >%d , using /%d divider\n",
  145. QSPI_CLK_DIV_MAX, QSPI_CLK_DIV_MAX + 1);
  146. return -EINVAL;
  147. }
  148. dev_dbg(qspi->dev, "hz: %d, clock divider %d\n",
  149. qspi->spi_max_frequency, clk_div);
  150. ret = pm_runtime_get_sync(qspi->dev);
  151. if (ret < 0) {
  152. dev_err(qspi->dev, "pm_runtime_get_sync() failed\n");
  153. return ret;
  154. }
  155. clk_ctrl_reg = ti_qspi_read(qspi, QSPI_SPI_CLOCK_CNTRL_REG);
  156. clk_ctrl_reg &= ~QSPI_CLK_EN;
  157. /* disable SCLK */
  158. ti_qspi_write(qspi, clk_ctrl_reg, QSPI_SPI_CLOCK_CNTRL_REG);
  159. /* enable SCLK */
  160. clk_mask = QSPI_CLK_EN | clk_div;
  161. ti_qspi_write(qspi, clk_mask, QSPI_SPI_CLOCK_CNTRL_REG);
  162. ctx_reg->clkctrl = clk_mask;
  163. pm_runtime_mark_last_busy(qspi->dev);
  164. ret = pm_runtime_put_autosuspend(qspi->dev);
  165. if (ret < 0) {
  166. dev_err(qspi->dev, "pm_runtime_put_autosuspend() failed\n");
  167. return ret;
  168. }
  169. return 0;
  170. }
  171. static void ti_qspi_restore_ctx(struct ti_qspi *qspi)
  172. {
  173. struct ti_qspi_regs *ctx_reg = &qspi->ctx_reg;
  174. ti_qspi_write(qspi, ctx_reg->clkctrl, QSPI_SPI_CLOCK_CNTRL_REG);
  175. }
  176. static inline u32 qspi_is_busy(struct ti_qspi *qspi)
  177. {
  178. u32 stat;
  179. unsigned long timeout = jiffies + QSPI_COMPLETION_TIMEOUT;
  180. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  181. while ((stat & BUSY) && time_after(timeout, jiffies)) {
  182. cpu_relax();
  183. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  184. }
  185. WARN(stat & BUSY, "qspi busy\n");
  186. return stat & BUSY;
  187. }
  188. static inline int ti_qspi_poll_wc(struct ti_qspi *qspi)
  189. {
  190. u32 stat;
  191. unsigned long timeout = jiffies + QSPI_COMPLETION_TIMEOUT;
  192. do {
  193. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  194. if (stat & WC)
  195. return 0;
  196. cpu_relax();
  197. } while (time_after(timeout, jiffies));
  198. stat = ti_qspi_read(qspi, QSPI_SPI_STATUS_REG);
  199. if (stat & WC)
  200. return 0;
  201. return -ETIMEDOUT;
  202. }
  203. static int qspi_write_msg(struct ti_qspi *qspi, struct spi_transfer *t,
  204. int count)
  205. {
  206. int wlen, xfer_len;
  207. unsigned int cmd;
  208. const u8 *txbuf;
  209. u32 data;
  210. txbuf = t->tx_buf;
  211. cmd = qspi->cmd | QSPI_WR_SNGL;
  212. wlen = t->bits_per_word >> 3; /* in bytes */
  213. xfer_len = wlen;
  214. while (count) {
  215. if (qspi_is_busy(qspi))
  216. return -EBUSY;
  217. switch (wlen) {
  218. case 1:
  219. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %02x\n",
  220. cmd, qspi->dc, *txbuf);
  221. if (count >= QSPI_WLEN_MAX_BYTES) {
  222. u32 *txp = (u32 *)txbuf;
  223. data = cpu_to_be32(*txp++);
  224. writel(data, qspi->base +
  225. QSPI_SPI_DATA_REG_3);
  226. data = cpu_to_be32(*txp++);
  227. writel(data, qspi->base +
  228. QSPI_SPI_DATA_REG_2);
  229. data = cpu_to_be32(*txp++);
  230. writel(data, qspi->base +
  231. QSPI_SPI_DATA_REG_1);
  232. data = cpu_to_be32(*txp++);
  233. writel(data, qspi->base +
  234. QSPI_SPI_DATA_REG);
  235. xfer_len = QSPI_WLEN_MAX_BYTES;
  236. cmd |= QSPI_WLEN(QSPI_WLEN_MAX_BITS);
  237. } else {
  238. writeb(*txbuf, qspi->base + QSPI_SPI_DATA_REG);
  239. cmd = qspi->cmd | QSPI_WR_SNGL;
  240. xfer_len = wlen;
  241. cmd |= QSPI_WLEN(wlen);
  242. }
  243. break;
  244. case 2:
  245. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %04x\n",
  246. cmd, qspi->dc, *txbuf);
  247. writew(*((u16 *)txbuf), qspi->base + QSPI_SPI_DATA_REG);
  248. break;
  249. case 4:
  250. dev_dbg(qspi->dev, "tx cmd %08x dc %08x data %08x\n",
  251. cmd, qspi->dc, *txbuf);
  252. writel(*((u32 *)txbuf), qspi->base + QSPI_SPI_DATA_REG);
  253. break;
  254. }
  255. ti_qspi_write(qspi, cmd, QSPI_SPI_CMD_REG);
  256. if (ti_qspi_poll_wc(qspi)) {
  257. dev_err(qspi->dev, "write timed out\n");
  258. return -ETIMEDOUT;
  259. }
  260. txbuf += xfer_len;
  261. count -= xfer_len;
  262. }
  263. return 0;
  264. }
  265. static int qspi_read_msg(struct ti_qspi *qspi, struct spi_transfer *t,
  266. int count)
  267. {
  268. int wlen;
  269. unsigned int cmd;
  270. u8 *rxbuf;
  271. rxbuf = t->rx_buf;
  272. cmd = qspi->cmd;
  273. switch (t->rx_nbits) {
  274. case SPI_NBITS_DUAL:
  275. cmd |= QSPI_RD_DUAL;
  276. break;
  277. case SPI_NBITS_QUAD:
  278. cmd |= QSPI_RD_QUAD;
  279. break;
  280. default:
  281. cmd |= QSPI_RD_SNGL;
  282. break;
  283. }
  284. wlen = t->bits_per_word >> 3; /* in bytes */
  285. while (count) {
  286. dev_dbg(qspi->dev, "rx cmd %08x dc %08x\n", cmd, qspi->dc);
  287. if (qspi_is_busy(qspi))
  288. return -EBUSY;
  289. ti_qspi_write(qspi, cmd, QSPI_SPI_CMD_REG);
  290. if (ti_qspi_poll_wc(qspi)) {
  291. dev_err(qspi->dev, "read timed out\n");
  292. return -ETIMEDOUT;
  293. }
  294. switch (wlen) {
  295. case 1:
  296. *rxbuf = readb(qspi->base + QSPI_SPI_DATA_REG);
  297. break;
  298. case 2:
  299. *((u16 *)rxbuf) = readw(qspi->base + QSPI_SPI_DATA_REG);
  300. break;
  301. case 4:
  302. *((u32 *)rxbuf) = readl(qspi->base + QSPI_SPI_DATA_REG);
  303. break;
  304. }
  305. rxbuf += wlen;
  306. count -= wlen;
  307. }
  308. return 0;
  309. }
  310. static int qspi_transfer_msg(struct ti_qspi *qspi, struct spi_transfer *t,
  311. int count)
  312. {
  313. int ret;
  314. if (t->tx_buf) {
  315. ret = qspi_write_msg(qspi, t, count);
  316. if (ret) {
  317. dev_dbg(qspi->dev, "Error while writing\n");
  318. return ret;
  319. }
  320. }
  321. if (t->rx_buf) {
  322. ret = qspi_read_msg(qspi, t, count);
  323. if (ret) {
  324. dev_dbg(qspi->dev, "Error while reading\n");
  325. return ret;
  326. }
  327. }
  328. return 0;
  329. }
  330. static void ti_qspi_dma_callback(void *param)
  331. {
  332. struct ti_qspi *qspi = param;
  333. complete(&qspi->transfer_complete);
  334. }
  335. static int ti_qspi_dma_xfer(struct ti_qspi *qspi, dma_addr_t dma_dst,
  336. dma_addr_t dma_src, size_t len)
  337. {
  338. struct dma_chan *chan = qspi->rx_chan;
  339. dma_cookie_t cookie;
  340. enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
  341. struct dma_async_tx_descriptor *tx;
  342. int ret;
  343. tx = dmaengine_prep_dma_memcpy(chan, dma_dst, dma_src, len, flags);
  344. if (!tx) {
  345. dev_err(qspi->dev, "device_prep_dma_memcpy error\n");
  346. return -EIO;
  347. }
  348. tx->callback = ti_qspi_dma_callback;
  349. tx->callback_param = qspi;
  350. cookie = tx->tx_submit(tx);
  351. reinit_completion(&qspi->transfer_complete);
  352. ret = dma_submit_error(cookie);
  353. if (ret) {
  354. dev_err(qspi->dev, "dma_submit_error %d\n", cookie);
  355. return -EIO;
  356. }
  357. dma_async_issue_pending(chan);
  358. ret = wait_for_completion_timeout(&qspi->transfer_complete,
  359. msecs_to_jiffies(len));
  360. if (ret <= 0) {
  361. dmaengine_terminate_sync(chan);
  362. dev_err(qspi->dev, "DMA wait_for_completion_timeout\n");
  363. return -ETIMEDOUT;
  364. }
  365. return 0;
  366. }
  367. static int ti_qspi_dma_bounce_buffer(struct ti_qspi *qspi, loff_t offs,
  368. void *to, size_t readsize)
  369. {
  370. dma_addr_t dma_src = qspi->mmap_phys_base + offs;
  371. int ret = 0;
  372. /*
  373. * Use bounce buffer as FS like jffs2, ubifs may pass
  374. * buffers that does not belong to kernel lowmem region.
  375. */
  376. while (readsize != 0) {
  377. size_t xfer_len = min_t(size_t, QSPI_DMA_BUFFER_SIZE,
  378. readsize);
  379. ret = ti_qspi_dma_xfer(qspi, qspi->rx_bb_dma_addr,
  380. dma_src, xfer_len);
  381. if (ret != 0)
  382. return ret;
  383. memcpy(to, qspi->rx_bb_addr, xfer_len);
  384. readsize -= xfer_len;
  385. dma_src += xfer_len;
  386. to += xfer_len;
  387. }
  388. return ret;
  389. }
  390. static int ti_qspi_dma_xfer_sg(struct ti_qspi *qspi, struct sg_table rx_sg,
  391. loff_t from)
  392. {
  393. struct scatterlist *sg;
  394. dma_addr_t dma_src = qspi->mmap_phys_base + from;
  395. dma_addr_t dma_dst;
  396. int i, len, ret;
  397. for_each_sg(rx_sg.sgl, sg, rx_sg.nents, i) {
  398. dma_dst = sg_dma_address(sg);
  399. len = sg_dma_len(sg);
  400. ret = ti_qspi_dma_xfer(qspi, dma_dst, dma_src, len);
  401. if (ret)
  402. return ret;
  403. dma_src += len;
  404. }
  405. return 0;
  406. }
  407. static void ti_qspi_enable_memory_map(struct spi_device *spi)
  408. {
  409. struct ti_qspi *qspi = spi_master_get_devdata(spi->master);
  410. ti_qspi_write(qspi, MM_SWITCH, QSPI_SPI_SWITCH_REG);
  411. if (qspi->ctrl_base) {
  412. regmap_update_bits(qspi->ctrl_base, qspi->ctrl_reg,
  413. MEM_CS_MASK,
  414. MEM_CS_EN(spi->chip_select));
  415. }
  416. qspi->mmap_enabled = true;
  417. qspi->current_cs = spi->chip_select;
  418. }
  419. static void ti_qspi_disable_memory_map(struct spi_device *spi)
  420. {
  421. struct ti_qspi *qspi = spi_master_get_devdata(spi->master);
  422. ti_qspi_write(qspi, 0, QSPI_SPI_SWITCH_REG);
  423. if (qspi->ctrl_base)
  424. regmap_update_bits(qspi->ctrl_base, qspi->ctrl_reg,
  425. MEM_CS_MASK, 0);
  426. qspi->mmap_enabled = false;
  427. qspi->current_cs = -1;
  428. }
  429. static void ti_qspi_setup_mmap_read(struct spi_device *spi, u8 opcode,
  430. u8 data_nbits, u8 addr_width,
  431. u8 dummy_bytes)
  432. {
  433. struct ti_qspi *qspi = spi_master_get_devdata(spi->master);
  434. u32 memval = opcode;
  435. switch (data_nbits) {
  436. case SPI_NBITS_QUAD:
  437. memval |= QSPI_SETUP_RD_QUAD;
  438. break;
  439. case SPI_NBITS_DUAL:
  440. memval |= QSPI_SETUP_RD_DUAL;
  441. break;
  442. default:
  443. memval |= QSPI_SETUP_RD_NORMAL;
  444. break;
  445. }
  446. memval |= ((addr_width - 1) << QSPI_SETUP_ADDR_SHIFT |
  447. dummy_bytes << QSPI_SETUP_DUMMY_SHIFT);
  448. ti_qspi_write(qspi, memval,
  449. QSPI_SPI_SETUP_REG(spi->chip_select));
  450. }
  451. static int ti_qspi_exec_mem_op(struct spi_mem *mem,
  452. const struct spi_mem_op *op)
  453. {
  454. struct ti_qspi *qspi = spi_master_get_devdata(mem->spi->master);
  455. u32 from = 0;
  456. int ret = 0;
  457. /* Only optimize read path. */
  458. if (!op->data.nbytes || op->data.dir != SPI_MEM_DATA_IN ||
  459. !op->addr.nbytes || op->addr.nbytes > 4)
  460. return -ENOTSUPP;
  461. /* Address exceeds MMIO window size, fall back to regular mode. */
  462. from = op->addr.val;
  463. if (from + op->data.nbytes > qspi->mmap_size)
  464. return -ENOTSUPP;
  465. mutex_lock(&qspi->list_lock);
  466. if (!qspi->mmap_enabled || qspi->current_cs != mem->spi->chip_select)
  467. ti_qspi_enable_memory_map(mem->spi);
  468. ti_qspi_setup_mmap_read(mem->spi, op->cmd.opcode, op->data.buswidth,
  469. op->addr.nbytes, op->dummy.nbytes);
  470. if (qspi->rx_chan) {
  471. struct sg_table sgt;
  472. if (virt_addr_valid(op->data.buf.in) &&
  473. !spi_controller_dma_map_mem_op_data(mem->spi->master, op,
  474. &sgt)) {
  475. ret = ti_qspi_dma_xfer_sg(qspi, sgt, from);
  476. spi_controller_dma_unmap_mem_op_data(mem->spi->master,
  477. op, &sgt);
  478. } else {
  479. ret = ti_qspi_dma_bounce_buffer(qspi, from,
  480. op->data.buf.in,
  481. op->data.nbytes);
  482. }
  483. } else {
  484. memcpy_fromio(op->data.buf.in, qspi->mmap_base + from,
  485. op->data.nbytes);
  486. }
  487. mutex_unlock(&qspi->list_lock);
  488. return ret;
  489. }
  490. static const struct spi_controller_mem_ops ti_qspi_mem_ops = {
  491. .exec_op = ti_qspi_exec_mem_op,
  492. };
  493. static int ti_qspi_start_transfer_one(struct spi_master *master,
  494. struct spi_message *m)
  495. {
  496. struct ti_qspi *qspi = spi_master_get_devdata(master);
  497. struct spi_device *spi = m->spi;
  498. struct spi_transfer *t;
  499. int status = 0, ret;
  500. unsigned int frame_len_words, transfer_len_words;
  501. int wlen;
  502. /* setup device control reg */
  503. qspi->dc = 0;
  504. if (spi->mode & SPI_CPHA)
  505. qspi->dc |= QSPI_CKPHA(spi->chip_select);
  506. if (spi->mode & SPI_CPOL)
  507. qspi->dc |= QSPI_CKPOL(spi->chip_select);
  508. if (spi->mode & SPI_CS_HIGH)
  509. qspi->dc |= QSPI_CSPOL(spi->chip_select);
  510. frame_len_words = 0;
  511. list_for_each_entry(t, &m->transfers, transfer_list)
  512. frame_len_words += t->len / (t->bits_per_word >> 3);
  513. frame_len_words = min_t(unsigned int, frame_len_words, QSPI_FRAME);
  514. /* setup command reg */
  515. qspi->cmd = 0;
  516. qspi->cmd |= QSPI_EN_CS(spi->chip_select);
  517. qspi->cmd |= QSPI_FLEN(frame_len_words);
  518. ti_qspi_write(qspi, qspi->dc, QSPI_SPI_DC_REG);
  519. mutex_lock(&qspi->list_lock);
  520. if (qspi->mmap_enabled)
  521. ti_qspi_disable_memory_map(spi);
  522. list_for_each_entry(t, &m->transfers, transfer_list) {
  523. qspi->cmd = ((qspi->cmd & ~QSPI_WLEN_MASK) |
  524. QSPI_WLEN(t->bits_per_word));
  525. wlen = t->bits_per_word >> 3;
  526. transfer_len_words = min(t->len / wlen, frame_len_words);
  527. ret = qspi_transfer_msg(qspi, t, transfer_len_words * wlen);
  528. if (ret) {
  529. dev_dbg(qspi->dev, "transfer message failed\n");
  530. mutex_unlock(&qspi->list_lock);
  531. return -EINVAL;
  532. }
  533. m->actual_length += transfer_len_words * wlen;
  534. frame_len_words -= transfer_len_words;
  535. if (frame_len_words == 0)
  536. break;
  537. }
  538. mutex_unlock(&qspi->list_lock);
  539. ti_qspi_write(qspi, qspi->cmd | QSPI_INVAL, QSPI_SPI_CMD_REG);
  540. m->status = status;
  541. spi_finalize_current_message(master);
  542. return status;
  543. }
  544. static int ti_qspi_runtime_resume(struct device *dev)
  545. {
  546. struct ti_qspi *qspi;
  547. qspi = dev_get_drvdata(dev);
  548. ti_qspi_restore_ctx(qspi);
  549. return 0;
  550. }
  551. static const struct of_device_id ti_qspi_match[] = {
  552. {.compatible = "ti,dra7xxx-qspi" },
  553. {.compatible = "ti,am4372-qspi" },
  554. {},
  555. };
  556. MODULE_DEVICE_TABLE(of, ti_qspi_match);
  557. static int ti_qspi_probe(struct platform_device *pdev)
  558. {
  559. struct ti_qspi *qspi;
  560. struct spi_master *master;
  561. struct resource *r, *res_mmap;
  562. struct device_node *np = pdev->dev.of_node;
  563. u32 max_freq;
  564. int ret = 0, num_cs, irq;
  565. dma_cap_mask_t mask;
  566. master = spi_alloc_master(&pdev->dev, sizeof(*qspi));
  567. if (!master)
  568. return -ENOMEM;
  569. master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_RX_DUAL | SPI_RX_QUAD;
  570. master->flags = SPI_MASTER_HALF_DUPLEX;
  571. master->setup = ti_qspi_setup;
  572. master->auto_runtime_pm = true;
  573. master->transfer_one_message = ti_qspi_start_transfer_one;
  574. master->dev.of_node = pdev->dev.of_node;
  575. master->bits_per_word_mask = SPI_BPW_MASK(32) | SPI_BPW_MASK(16) |
  576. SPI_BPW_MASK(8);
  577. master->mem_ops = &ti_qspi_mem_ops;
  578. if (!of_property_read_u32(np, "num-cs", &num_cs))
  579. master->num_chipselect = num_cs;
  580. qspi = spi_master_get_devdata(master);
  581. qspi->master = master;
  582. qspi->dev = &pdev->dev;
  583. platform_set_drvdata(pdev, qspi);
  584. r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
  585. if (r == NULL) {
  586. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  587. if (r == NULL) {
  588. dev_err(&pdev->dev, "missing platform data\n");
  589. ret = -ENODEV;
  590. goto free_master;
  591. }
  592. }
  593. res_mmap = platform_get_resource_byname(pdev,
  594. IORESOURCE_MEM, "qspi_mmap");
  595. if (res_mmap == NULL) {
  596. res_mmap = platform_get_resource(pdev, IORESOURCE_MEM, 1);
  597. if (res_mmap == NULL) {
  598. dev_err(&pdev->dev,
  599. "memory mapped resource not required\n");
  600. }
  601. }
  602. if (res_mmap)
  603. qspi->mmap_size = resource_size(res_mmap);
  604. irq = platform_get_irq(pdev, 0);
  605. if (irq < 0) {
  606. dev_err(&pdev->dev, "no irq resource?\n");
  607. ret = irq;
  608. goto free_master;
  609. }
  610. mutex_init(&qspi->list_lock);
  611. qspi->base = devm_ioremap_resource(&pdev->dev, r);
  612. if (IS_ERR(qspi->base)) {
  613. ret = PTR_ERR(qspi->base);
  614. goto free_master;
  615. }
  616. if (of_property_read_bool(np, "syscon-chipselects")) {
  617. qspi->ctrl_base =
  618. syscon_regmap_lookup_by_phandle(np,
  619. "syscon-chipselects");
  620. if (IS_ERR(qspi->ctrl_base)) {
  621. ret = PTR_ERR(qspi->ctrl_base);
  622. goto free_master;
  623. }
  624. ret = of_property_read_u32_index(np,
  625. "syscon-chipselects",
  626. 1, &qspi->ctrl_reg);
  627. if (ret) {
  628. dev_err(&pdev->dev,
  629. "couldn't get ctrl_mod reg index\n");
  630. goto free_master;
  631. }
  632. }
  633. qspi->fclk = devm_clk_get(&pdev->dev, "fck");
  634. if (IS_ERR(qspi->fclk)) {
  635. ret = PTR_ERR(qspi->fclk);
  636. dev_err(&pdev->dev, "could not get clk: %d\n", ret);
  637. }
  638. pm_runtime_use_autosuspend(&pdev->dev);
  639. pm_runtime_set_autosuspend_delay(&pdev->dev, QSPI_AUTOSUSPEND_TIMEOUT);
  640. pm_runtime_enable(&pdev->dev);
  641. if (!of_property_read_u32(np, "spi-max-frequency", &max_freq))
  642. qspi->spi_max_frequency = max_freq;
  643. dma_cap_zero(mask);
  644. dma_cap_set(DMA_MEMCPY, mask);
  645. qspi->rx_chan = dma_request_chan_by_mask(&mask);
  646. if (IS_ERR(qspi->rx_chan)) {
  647. dev_err(qspi->dev,
  648. "No Rx DMA available, trying mmap mode\n");
  649. qspi->rx_chan = NULL;
  650. ret = 0;
  651. goto no_dma;
  652. }
  653. qspi->rx_bb_addr = dma_alloc_coherent(qspi->dev,
  654. QSPI_DMA_BUFFER_SIZE,
  655. &qspi->rx_bb_dma_addr,
  656. GFP_KERNEL | GFP_DMA);
  657. if (!qspi->rx_bb_addr) {
  658. dev_err(qspi->dev,
  659. "dma_alloc_coherent failed, using PIO mode\n");
  660. dma_release_channel(qspi->rx_chan);
  661. goto no_dma;
  662. }
  663. master->dma_rx = qspi->rx_chan;
  664. init_completion(&qspi->transfer_complete);
  665. if (res_mmap)
  666. qspi->mmap_phys_base = (dma_addr_t)res_mmap->start;
  667. no_dma:
  668. if (!qspi->rx_chan && res_mmap) {
  669. qspi->mmap_base = devm_ioremap_resource(&pdev->dev, res_mmap);
  670. if (IS_ERR(qspi->mmap_base)) {
  671. dev_info(&pdev->dev,
  672. "mmap failed with error %ld using PIO mode\n",
  673. PTR_ERR(qspi->mmap_base));
  674. qspi->mmap_base = NULL;
  675. master->mem_ops = NULL;
  676. }
  677. }
  678. qspi->mmap_enabled = false;
  679. qspi->current_cs = -1;
  680. ret = devm_spi_register_master(&pdev->dev, master);
  681. if (!ret)
  682. return 0;
  683. pm_runtime_disable(&pdev->dev);
  684. free_master:
  685. spi_master_put(master);
  686. return ret;
  687. }
  688. static int ti_qspi_remove(struct platform_device *pdev)
  689. {
  690. struct ti_qspi *qspi = platform_get_drvdata(pdev);
  691. int rc;
  692. rc = spi_master_suspend(qspi->master);
  693. if (rc)
  694. return rc;
  695. pm_runtime_put_sync(&pdev->dev);
  696. pm_runtime_disable(&pdev->dev);
  697. if (qspi->rx_bb_addr)
  698. dma_free_coherent(qspi->dev, QSPI_DMA_BUFFER_SIZE,
  699. qspi->rx_bb_addr,
  700. qspi->rx_bb_dma_addr);
  701. if (qspi->rx_chan)
  702. dma_release_channel(qspi->rx_chan);
  703. return 0;
  704. }
  705. static const struct dev_pm_ops ti_qspi_pm_ops = {
  706. .runtime_resume = ti_qspi_runtime_resume,
  707. };
  708. static struct platform_driver ti_qspi_driver = {
  709. .probe = ti_qspi_probe,
  710. .remove = ti_qspi_remove,
  711. .driver = {
  712. .name = "ti-qspi",
  713. .pm = &ti_qspi_pm_ops,
  714. .of_match_table = ti_qspi_match,
  715. }
  716. };
  717. module_platform_driver(ti_qspi_driver);
  718. MODULE_AUTHOR("Sourav Poddar <sourav.poddar@ti.com>");
  719. MODULE_LICENSE("GPL v2");
  720. MODULE_DESCRIPTION("TI QSPI controller driver");
  721. MODULE_ALIAS("platform:ti-qspi");