spi-ti-qspi.c 18 KB

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