blob: 69fd220a289446f4d92a1979c0a2d9f28633c02e [file] [log] [blame]
Ludovic Barrec530cd12018-10-05 09:43:03 +02001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Author: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics.
5 */
6#include <linux/bitfield.h>
7#include <linux/clk.h>
Ludovic Barre245308c2019-03-25 18:01:40 +01008#include <linux/dmaengine.h>
9#include <linux/dma-mapping.h>
Ludovic Barrec530cd12018-10-05 09:43:03 +020010#include <linux/errno.h>
11#include <linux/io.h>
12#include <linux/iopoll.h>
13#include <linux/interrupt.h>
14#include <linux/module.h>
15#include <linux/mutex.h>
16#include <linux/of.h>
17#include <linux/of_device.h>
Ludovic Barre2e541b62019-03-08 14:12:21 +010018#include <linux/pinctrl/consumer.h>
Patrice Chotard9d282c12020-04-17 14:12:41 +020019#include <linux/pm_runtime.h>
Ludovic Barrec530cd12018-10-05 09:43:03 +020020#include <linux/platform_device.h>
21#include <linux/reset.h>
22#include <linux/sizes.h>
23#include <linux/spi/spi-mem.h>
24
25#define QSPI_CR 0x00
26#define CR_EN BIT(0)
27#define CR_ABORT BIT(1)
28#define CR_DMAEN BIT(2)
29#define CR_TCEN BIT(3)
30#define CR_SSHIFT BIT(4)
31#define CR_DFM BIT(6)
32#define CR_FSEL BIT(7)
Patrice Chotard94613d52019-06-20 15:13:23 +020033#define CR_FTHRES_SHIFT 8
Ludovic Barrec530cd12018-10-05 09:43:03 +020034#define CR_TEIE BIT(16)
35#define CR_TCIE BIT(17)
36#define CR_FTIE BIT(18)
37#define CR_SMIE BIT(19)
38#define CR_TOIE BIT(20)
Patrice Chotard86d1c6b2021-05-18 18:27:54 +020039#define CR_APMS BIT(22)
Ludovic Barrec530cd12018-10-05 09:43:03 +020040#define CR_PRESC_MASK GENMASK(31, 24)
41
42#define QSPI_DCR 0x04
43#define DCR_FSIZE_MASK GENMASK(20, 16)
44
45#define QSPI_SR 0x08
46#define SR_TEF BIT(0)
47#define SR_TCF BIT(1)
48#define SR_FTF BIT(2)
49#define SR_SMF BIT(3)
50#define SR_TOF BIT(4)
51#define SR_BUSY BIT(5)
52#define SR_FLEVEL_MASK GENMASK(13, 8)
53
54#define QSPI_FCR 0x0c
55#define FCR_CTEF BIT(0)
56#define FCR_CTCF BIT(1)
Patrice Chotard86d1c6b2021-05-18 18:27:54 +020057#define FCR_CSMF BIT(3)
Ludovic Barrec530cd12018-10-05 09:43:03 +020058
59#define QSPI_DLR 0x10
60
61#define QSPI_CCR 0x14
62#define CCR_INST_MASK GENMASK(7, 0)
63#define CCR_IMODE_MASK GENMASK(9, 8)
64#define CCR_ADMODE_MASK GENMASK(11, 10)
65#define CCR_ADSIZE_MASK GENMASK(13, 12)
66#define CCR_DCYC_MASK GENMASK(22, 18)
67#define CCR_DMODE_MASK GENMASK(25, 24)
68#define CCR_FMODE_MASK GENMASK(27, 26)
69#define CCR_FMODE_INDW (0U << 26)
70#define CCR_FMODE_INDR (1U << 26)
71#define CCR_FMODE_APM (2U << 26)
72#define CCR_FMODE_MM (3U << 26)
73#define CCR_BUSWIDTH_0 0x0
74#define CCR_BUSWIDTH_1 0x1
75#define CCR_BUSWIDTH_2 0x2
76#define CCR_BUSWIDTH_4 0x3
77
78#define QSPI_AR 0x18
79#define QSPI_ABR 0x1c
80#define QSPI_DR 0x20
81#define QSPI_PSMKR 0x24
82#define QSPI_PSMAR 0x28
83#define QSPI_PIR 0x2c
84#define QSPI_LPTR 0x30
Ludovic Barrec530cd12018-10-05 09:43:03 +020085
86#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
87#define STM32_QSPI_MAX_NORCHIP 2
88
89#define STM32_FIFO_TIMEOUT_US 30000
90#define STM32_BUSY_TIMEOUT_US 100000
91#define STM32_ABT_TIMEOUT_US 100000
Ludovic Barre245308c2019-03-25 18:01:40 +010092#define STM32_COMP_TIMEOUT_MS 1000
Patrice Chotard9d282c12020-04-17 14:12:41 +020093#define STM32_AUTOSUSPEND_DELAY -1
Ludovic Barrec530cd12018-10-05 09:43:03 +020094
95struct stm32_qspi_flash {
96 struct stm32_qspi *qspi;
97 u32 cs;
98 u32 presc;
99};
100
101struct stm32_qspi {
102 struct device *dev;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100103 struct spi_controller *ctrl;
Ludovic Barre245308c2019-03-25 18:01:40 +0100104 phys_addr_t phys_base;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200105 void __iomem *io_base;
106 void __iomem *mm_base;
107 resource_size_t mm_size;
108 struct clk *clk;
109 u32 clk_rate;
110 struct stm32_qspi_flash flash[STM32_QSPI_MAX_NORCHIP];
111 struct completion data_completion;
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200112 struct completion match_completion;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200113 u32 fmode;
114
Ludovic Barre245308c2019-03-25 18:01:40 +0100115 struct dma_chan *dma_chtx;
116 struct dma_chan *dma_chrx;
117 struct completion dma_completion;
118
Ludovic Barre2e541b62019-03-08 14:12:21 +0100119 u32 cr_reg;
120 u32 dcr_reg;
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200121 unsigned long status_timeout;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100122
Ludovic Barrec530cd12018-10-05 09:43:03 +0200123 /*
124 * to protect device configuration, could be different between
125 * 2 flash access (bk1, bk2)
126 */
127 struct mutex lock;
128};
129
130static irqreturn_t stm32_qspi_irq(int irq, void *dev_id)
131{
132 struct stm32_qspi *qspi = (struct stm32_qspi *)dev_id;
133 u32 cr, sr;
134
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200135 cr = readl_relaxed(qspi->io_base + QSPI_CR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200136 sr = readl_relaxed(qspi->io_base + QSPI_SR);
137
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200138 if (cr & CR_SMIE && sr & SR_SMF) {
139 /* disable irq */
140 cr &= ~CR_SMIE;
141 writel_relaxed(cr, qspi->io_base + QSPI_CR);
142 complete(&qspi->match_completion);
143
144 return IRQ_HANDLED;
145 }
146
Ludovic Barrec530cd12018-10-05 09:43:03 +0200147 if (sr & (SR_TEF | SR_TCF)) {
148 /* disable irq */
Ludovic Barrec530cd12018-10-05 09:43:03 +0200149 cr &= ~CR_TCIE & ~CR_TEIE;
150 writel_relaxed(cr, qspi->io_base + QSPI_CR);
151 complete(&qspi->data_completion);
152 }
153
154 return IRQ_HANDLED;
155}
156
157static void stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
158{
159 *val = readb_relaxed(addr);
160}
161
162static void stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
163{
164 writeb_relaxed(*val, addr);
165}
166
167static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
168 const struct spi_mem_op *op)
169{
170 void (*tx_fifo)(u8 *val, void __iomem *addr);
171 u32 len = op->data.nbytes, sr;
172 u8 *buf;
173 int ret;
174
175 if (op->data.dir == SPI_MEM_DATA_IN) {
176 tx_fifo = stm32_qspi_read_fifo;
177 buf = op->data.buf.in;
178
179 } else {
180 tx_fifo = stm32_qspi_write_fifo;
181 buf = (u8 *)op->data.buf.out;
182 }
183
184 while (len--) {
185 ret = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR,
186 sr, (sr & SR_FTF), 1,
187 STM32_FIFO_TIMEOUT_US);
188 if (ret) {
189 dev_err(qspi->dev, "fifo timeout (len:%d stat:%#x)\n",
190 len, sr);
191 return ret;
192 }
193 tx_fifo(buf++, qspi->io_base + QSPI_DR);
194 }
195
196 return 0;
197}
198
199static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
200 const struct spi_mem_op *op)
201{
202 memcpy_fromio(op->data.buf.in, qspi->mm_base + op->addr.val,
203 op->data.nbytes);
204 return 0;
205}
206
Ludovic Barre245308c2019-03-25 18:01:40 +0100207static void stm32_qspi_dma_callback(void *arg)
208{
209 struct completion *dma_completion = arg;
210
211 complete(dma_completion);
212}
213
214static int stm32_qspi_tx_dma(struct stm32_qspi *qspi,
215 const struct spi_mem_op *op)
216{
217 struct dma_async_tx_descriptor *desc;
218 enum dma_transfer_direction dma_dir;
219 struct dma_chan *dma_ch;
220 struct sg_table sgt;
221 dma_cookie_t cookie;
222 u32 cr, t_out;
223 int err;
224
225 if (op->data.dir == SPI_MEM_DATA_IN) {
226 dma_dir = DMA_DEV_TO_MEM;
227 dma_ch = qspi->dma_chrx;
228 } else {
229 dma_dir = DMA_MEM_TO_DEV;
230 dma_ch = qspi->dma_chtx;
231 }
232
233 /*
234 * spi_map_buf return -EINVAL if the buffer is not DMA-able
235 * (DMA-able: in vmalloc | kmap | virt_addr_valid)
236 */
237 err = spi_controller_dma_map_mem_op_data(qspi->ctrl, op, &sgt);
238 if (err)
239 return err;
240
241 desc = dmaengine_prep_slave_sg(dma_ch, sgt.sgl, sgt.nents,
242 dma_dir, DMA_PREP_INTERRUPT);
243 if (!desc) {
244 err = -ENOMEM;
245 goto out_unmap;
246 }
247
248 cr = readl_relaxed(qspi->io_base + QSPI_CR);
249
250 reinit_completion(&qspi->dma_completion);
251 desc->callback = stm32_qspi_dma_callback;
252 desc->callback_param = &qspi->dma_completion;
253 cookie = dmaengine_submit(desc);
254 err = dma_submit_error(cookie);
255 if (err)
256 goto out;
257
258 dma_async_issue_pending(dma_ch);
259
260 writel_relaxed(cr | CR_DMAEN, qspi->io_base + QSPI_CR);
261
262 t_out = sgt.nents * STM32_COMP_TIMEOUT_MS;
Ludovic Barre775c4c02019-06-27 09:43:59 +0200263 if (!wait_for_completion_timeout(&qspi->dma_completion,
264 msecs_to_jiffies(t_out)))
Ludovic Barre245308c2019-03-25 18:01:40 +0100265 err = -ETIMEDOUT;
266
267 if (err)
268 dmaengine_terminate_all(dma_ch);
269
270out:
271 writel_relaxed(cr & ~CR_DMAEN, qspi->io_base + QSPI_CR);
272out_unmap:
273 spi_controller_dma_unmap_mem_op_data(qspi->ctrl, op, &sgt);
274
275 return err;
276}
277
Ludovic Barrec530cd12018-10-05 09:43:03 +0200278static int stm32_qspi_tx(struct stm32_qspi *qspi, const struct spi_mem_op *op)
279{
280 if (!op->data.nbytes)
281 return 0;
282
283 if (qspi->fmode == CCR_FMODE_MM)
284 return stm32_qspi_tx_mm(qspi, op);
Patrice Chotardf3530f22021-04-19 14:15:40 +0200285 else if (((op->data.dir == SPI_MEM_DATA_IN && qspi->dma_chrx) ||
286 (op->data.dir == SPI_MEM_DATA_OUT && qspi->dma_chtx)) &&
287 op->data.nbytes > 4)
Ludovic Barre245308c2019-03-25 18:01:40 +0100288 if (!stm32_qspi_tx_dma(qspi, op))
289 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200290
291 return stm32_qspi_tx_poll(qspi, op);
292}
293
294static int stm32_qspi_wait_nobusy(struct stm32_qspi *qspi)
295{
296 u32 sr;
297
298 return readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, sr,
299 !(sr & SR_BUSY), 1,
300 STM32_BUSY_TIMEOUT_US);
301}
302
303static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi,
304 const struct spi_mem_op *op)
305{
306 u32 cr, sr;
307 int err = 0;
308
309 if (!op->data.nbytes)
310 return stm32_qspi_wait_nobusy(qspi);
311
312 if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF)
313 goto out;
314
315 reinit_completion(&qspi->data_completion);
316 cr = readl_relaxed(qspi->io_base + QSPI_CR);
317 writel_relaxed(cr | CR_TCIE | CR_TEIE, qspi->io_base + QSPI_CR);
318
Ludovic Barre775c4c02019-06-27 09:43:59 +0200319 if (!wait_for_completion_timeout(&qspi->data_completion,
Ludovic Barre245308c2019-03-25 18:01:40 +0100320 msecs_to_jiffies(STM32_COMP_TIMEOUT_MS))) {
Ludovic Barrec530cd12018-10-05 09:43:03 +0200321 err = -ETIMEDOUT;
322 } else {
323 sr = readl_relaxed(qspi->io_base + QSPI_SR);
324 if (sr & SR_TEF)
325 err = -EIO;
326 }
327
328out:
329 /* clear flags */
330 writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR);
331
332 return err;
333}
334
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200335static int stm32_qspi_wait_poll_status(struct stm32_qspi *qspi,
336 const struct spi_mem_op *op)
337{
338 u32 cr;
339
340 reinit_completion(&qspi->match_completion);
341 cr = readl_relaxed(qspi->io_base + QSPI_CR);
342 writel_relaxed(cr | CR_SMIE, qspi->io_base + QSPI_CR);
343
344 if (!wait_for_completion_timeout(&qspi->match_completion,
345 msecs_to_jiffies(qspi->status_timeout)))
346 return -ETIMEDOUT;
347
348 writel_relaxed(FCR_CSMF, qspi->io_base + QSPI_FCR);
349
350 return 0;
351}
352
Ludovic Barrec530cd12018-10-05 09:43:03 +0200353static int stm32_qspi_get_mode(struct stm32_qspi *qspi, u8 buswidth)
354{
355 if (buswidth == 4)
356 return CCR_BUSWIDTH_4;
357
358 return buswidth;
359}
360
361static int stm32_qspi_send(struct spi_mem *mem, const struct spi_mem_op *op)
362{
363 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
364 struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select];
Patrice Chotard18674de2021-04-19 14:15:41 +0200365 u32 ccr, cr;
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200366 int timeout, err = 0, err_poll_status = 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200367
368 dev_dbg(qspi->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
369 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
370 op->dummy.buswidth, op->data.buswidth,
371 op->addr.val, op->data.nbytes);
372
373 err = stm32_qspi_wait_nobusy(qspi);
374 if (err)
375 goto abort;
376
Ludovic Barrec530cd12018-10-05 09:43:03 +0200377 cr = readl_relaxed(qspi->io_base + QSPI_CR);
378 cr &= ~CR_PRESC_MASK & ~CR_FSEL;
379 cr |= FIELD_PREP(CR_PRESC_MASK, flash->presc);
380 cr |= FIELD_PREP(CR_FSEL, flash->cs);
381 writel_relaxed(cr, qspi->io_base + QSPI_CR);
382
383 if (op->data.nbytes)
384 writel_relaxed(op->data.nbytes - 1,
385 qspi->io_base + QSPI_DLR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200386
387 ccr = qspi->fmode;
388 ccr |= FIELD_PREP(CCR_INST_MASK, op->cmd.opcode);
389 ccr |= FIELD_PREP(CCR_IMODE_MASK,
390 stm32_qspi_get_mode(qspi, op->cmd.buswidth));
391
392 if (op->addr.nbytes) {
393 ccr |= FIELD_PREP(CCR_ADMODE_MASK,
394 stm32_qspi_get_mode(qspi, op->addr.buswidth));
395 ccr |= FIELD_PREP(CCR_ADSIZE_MASK, op->addr.nbytes - 1);
396 }
397
398 if (op->dummy.buswidth && op->dummy.nbytes)
399 ccr |= FIELD_PREP(CCR_DCYC_MASK,
400 op->dummy.nbytes * 8 / op->dummy.buswidth);
401
402 if (op->data.nbytes) {
403 ccr |= FIELD_PREP(CCR_DMODE_MASK,
404 stm32_qspi_get_mode(qspi, op->data.buswidth));
405 }
406
407 writel_relaxed(ccr, qspi->io_base + QSPI_CCR);
408
409 if (op->addr.nbytes && qspi->fmode != CCR_FMODE_MM)
410 writel_relaxed(op->addr.val, qspi->io_base + QSPI_AR);
411
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200412 if (qspi->fmode == CCR_FMODE_APM)
413 err_poll_status = stm32_qspi_wait_poll_status(qspi, op);
414
Ludovic Barrec530cd12018-10-05 09:43:03 +0200415 err = stm32_qspi_tx(qspi, op);
416
417 /*
418 * Abort in:
419 * -error case
420 * -read memory map: prefetching must be stopped if we read the last
421 * byte of device (device size - fifo size). like device size is not
422 * knows, the prefetching is always stop.
423 */
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200424 if (err || err_poll_status || qspi->fmode == CCR_FMODE_MM)
Ludovic Barrec530cd12018-10-05 09:43:03 +0200425 goto abort;
426
427 /* wait end of tx in indirect mode */
428 err = stm32_qspi_wait_cmd(qspi, op);
429 if (err)
430 goto abort;
431
432 return 0;
433
434abort:
435 cr = readl_relaxed(qspi->io_base + QSPI_CR) | CR_ABORT;
436 writel_relaxed(cr, qspi->io_base + QSPI_CR);
437
438 /* wait clear of abort bit by hw */
439 timeout = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_CR,
440 cr, !(cr & CR_ABORT), 1,
441 STM32_ABT_TIMEOUT_US);
442
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200443 writel_relaxed(FCR_CTCF | FCR_CSMF, qspi->io_base + QSPI_FCR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200444
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200445 if (err || err_poll_status || timeout)
446 dev_err(qspi->dev, "%s err:%d err_poll_status:%d abort timeout:%d\n",
447 __func__, err, err_poll_status, timeout);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200448
449 return err;
450}
451
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200452static int stm32_qspi_poll_status(struct spi_mem *mem, const struct spi_mem_op *op,
453 u16 mask, u16 match,
454 unsigned long initial_delay_us,
455 unsigned long polling_rate_us,
456 unsigned long timeout_ms)
457{
458 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
459 int ret;
460
461 if (!spi_mem_supports_op(mem, op))
462 return -EOPNOTSUPP;
463
464 ret = pm_runtime_get_sync(qspi->dev);
465 if (ret < 0) {
466 pm_runtime_put_noidle(qspi->dev);
467 return ret;
468 }
469
470 mutex_lock(&qspi->lock);
471
472 writel_relaxed(mask, qspi->io_base + QSPI_PSMKR);
473 writel_relaxed(match, qspi->io_base + QSPI_PSMAR);
474 qspi->fmode = CCR_FMODE_APM;
475 qspi->status_timeout = timeout_ms;
476
477 ret = stm32_qspi_send(mem, op);
478 mutex_unlock(&qspi->lock);
479
480 pm_runtime_mark_last_busy(qspi->dev);
481 pm_runtime_put_autosuspend(qspi->dev);
482
483 return ret;
484}
485
Ludovic Barrec530cd12018-10-05 09:43:03 +0200486static int stm32_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
487{
488 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
489 int ret;
490
Patrice Chotard9d282c12020-04-17 14:12:41 +0200491 ret = pm_runtime_get_sync(qspi->dev);
Zhang Qilong88e14192020-11-06 09:53:57 +0800492 if (ret < 0) {
493 pm_runtime_put_noidle(qspi->dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200494 return ret;
Zhang Qilong88e14192020-11-06 09:53:57 +0800495 }
Patrice Chotard9d282c12020-04-17 14:12:41 +0200496
Ludovic Barrec530cd12018-10-05 09:43:03 +0200497 mutex_lock(&qspi->lock);
Patrice Chotard18674de2021-04-19 14:15:41 +0200498 if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes)
499 qspi->fmode = CCR_FMODE_INDR;
500 else
501 qspi->fmode = CCR_FMODE_INDW;
502
Ludovic Barrec530cd12018-10-05 09:43:03 +0200503 ret = stm32_qspi_send(mem, op);
504 mutex_unlock(&qspi->lock);
505
Patrice Chotard9d282c12020-04-17 14:12:41 +0200506 pm_runtime_mark_last_busy(qspi->dev);
507 pm_runtime_put_autosuspend(qspi->dev);
508
Ludovic Barrec530cd12018-10-05 09:43:03 +0200509 return ret;
510}
511
Patrice Chotard18674de2021-04-19 14:15:41 +0200512static int stm32_qspi_dirmap_create(struct spi_mem_dirmap_desc *desc)
513{
514 struct stm32_qspi *qspi = spi_controller_get_devdata(desc->mem->spi->master);
515
516 if (desc->info.op_tmpl.data.dir == SPI_MEM_DATA_OUT)
517 return -EOPNOTSUPP;
518
519 /* should never happen, as mm_base == null is an error probe exit condition */
520 if (!qspi->mm_base && desc->info.op_tmpl.data.dir == SPI_MEM_DATA_IN)
521 return -EOPNOTSUPP;
522
523 if (!qspi->mm_size)
524 return -EOPNOTSUPP;
525
526 return 0;
527}
528
529static ssize_t stm32_qspi_dirmap_read(struct spi_mem_dirmap_desc *desc,
530 u64 offs, size_t len, void *buf)
531{
532 struct stm32_qspi *qspi = spi_controller_get_devdata(desc->mem->spi->master);
533 struct spi_mem_op op;
534 u32 addr_max;
535 int ret;
536
537 ret = pm_runtime_get_sync(qspi->dev);
538 if (ret < 0) {
539 pm_runtime_put_noidle(qspi->dev);
540 return ret;
541 }
542
543 mutex_lock(&qspi->lock);
544 /* make a local copy of desc op_tmpl and complete dirmap rdesc
545 * spi_mem_op template with offs, len and *buf in order to get
546 * all needed transfer information into struct spi_mem_op
547 */
548 memcpy(&op, &desc->info.op_tmpl, sizeof(struct spi_mem_op));
Arnd Bergmann14ef64e2021-04-22 15:38:57 +0200549 dev_dbg(qspi->dev, "%s len = 0x%zx offs = 0x%llx buf = 0x%p\n", __func__, len, offs, buf);
Patrice Chotard18674de2021-04-19 14:15:41 +0200550
551 op.data.nbytes = len;
552 op.addr.val = desc->info.offset + offs;
553 op.data.buf.in = buf;
554
555 addr_max = op.addr.val + op.data.nbytes + 1;
556 if (addr_max < qspi->mm_size && op.addr.buswidth)
557 qspi->fmode = CCR_FMODE_MM;
558 else
559 qspi->fmode = CCR_FMODE_INDR;
560
561 ret = stm32_qspi_send(desc->mem, &op);
562 mutex_unlock(&qspi->lock);
563
564 pm_runtime_mark_last_busy(qspi->dev);
565 pm_runtime_put_autosuspend(qspi->dev);
566
567 return ret ?: len;
568}
569
Ludovic Barrec530cd12018-10-05 09:43:03 +0200570static int stm32_qspi_setup(struct spi_device *spi)
571{
572 struct spi_controller *ctrl = spi->master;
573 struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl);
574 struct stm32_qspi_flash *flash;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100575 u32 presc;
Patrice Chotard9d282c12020-04-17 14:12:41 +0200576 int ret;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200577
578 if (ctrl->busy)
579 return -EBUSY;
580
581 if (!spi->max_speed_hz)
582 return -EINVAL;
583
Patrice Chotard9d282c12020-04-17 14:12:41 +0200584 ret = pm_runtime_get_sync(qspi->dev);
Zhang Qilong88e14192020-11-06 09:53:57 +0800585 if (ret < 0) {
586 pm_runtime_put_noidle(qspi->dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200587 return ret;
Zhang Qilong88e14192020-11-06 09:53:57 +0800588 }
Patrice Chotard9d282c12020-04-17 14:12:41 +0200589
Ludovic Barrec530cd12018-10-05 09:43:03 +0200590 presc = DIV_ROUND_UP(qspi->clk_rate, spi->max_speed_hz) - 1;
591
592 flash = &qspi->flash[spi->chip_select];
593 flash->qspi = qspi;
594 flash->cs = spi->chip_select;
595 flash->presc = presc;
596
597 mutex_lock(&qspi->lock);
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200598 qspi->cr_reg = CR_APMS | 3 << CR_FTHRES_SHIFT | CR_SSHIFT | CR_EN;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100599 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200600
601 /* set dcr fsize to max address */
Ludovic Barre2e541b62019-03-08 14:12:21 +0100602 qspi->dcr_reg = DCR_FSIZE_MASK;
603 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200604 mutex_unlock(&qspi->lock);
605
Patrice Chotard9d282c12020-04-17 14:12:41 +0200606 pm_runtime_mark_last_busy(qspi->dev);
607 pm_runtime_put_autosuspend(qspi->dev);
608
Ludovic Barrec530cd12018-10-05 09:43:03 +0200609 return 0;
610}
611
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200612static int stm32_qspi_dma_setup(struct stm32_qspi *qspi)
Ludovic Barre245308c2019-03-25 18:01:40 +0100613{
614 struct dma_slave_config dma_cfg;
615 struct device *dev = qspi->dev;
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200616 int ret = 0;
Ludovic Barre245308c2019-03-25 18:01:40 +0100617
618 memset(&dma_cfg, 0, sizeof(dma_cfg));
619
620 dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
621 dma_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
622 dma_cfg.src_addr = qspi->phys_base + QSPI_DR;
623 dma_cfg.dst_addr = qspi->phys_base + QSPI_DR;
624 dma_cfg.src_maxburst = 4;
625 dma_cfg.dst_maxburst = 4;
626
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200627 qspi->dma_chrx = dma_request_chan(dev, "rx");
628 if (IS_ERR(qspi->dma_chrx)) {
629 ret = PTR_ERR(qspi->dma_chrx);
630 qspi->dma_chrx = NULL;
631 if (ret == -EPROBE_DEFER)
632 goto out;
633 } else {
Ludovic Barre245308c2019-03-25 18:01:40 +0100634 if (dmaengine_slave_config(qspi->dma_chrx, &dma_cfg)) {
635 dev_err(dev, "dma rx config failed\n");
636 dma_release_channel(qspi->dma_chrx);
637 qspi->dma_chrx = NULL;
638 }
639 }
640
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200641 qspi->dma_chtx = dma_request_chan(dev, "tx");
642 if (IS_ERR(qspi->dma_chtx)) {
643 ret = PTR_ERR(qspi->dma_chtx);
644 qspi->dma_chtx = NULL;
645 } else {
Ludovic Barre245308c2019-03-25 18:01:40 +0100646 if (dmaengine_slave_config(qspi->dma_chtx, &dma_cfg)) {
647 dev_err(dev, "dma tx config failed\n");
648 dma_release_channel(qspi->dma_chtx);
649 qspi->dma_chtx = NULL;
650 }
651 }
652
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200653out:
Ludovic Barre245308c2019-03-25 18:01:40 +0100654 init_completion(&qspi->dma_completion);
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200655
656 if (ret != -EPROBE_DEFER)
657 ret = 0;
658
659 return ret;
Ludovic Barre245308c2019-03-25 18:01:40 +0100660}
661
662static void stm32_qspi_dma_free(struct stm32_qspi *qspi)
663{
664 if (qspi->dma_chtx)
665 dma_release_channel(qspi->dma_chtx);
666 if (qspi->dma_chrx)
667 dma_release_channel(qspi->dma_chrx);
668}
669
Ludovic Barrec530cd12018-10-05 09:43:03 +0200670/*
671 * no special host constraint, so use default spi_mem_default_supports_op
672 * to check supported mode.
673 */
674static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
Patrice Chotard18674de2021-04-19 14:15:41 +0200675 .exec_op = stm32_qspi_exec_op,
676 .dirmap_create = stm32_qspi_dirmap_create,
677 .dirmap_read = stm32_qspi_dirmap_read,
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200678 .poll_status = stm32_qspi_poll_status,
Ludovic Barrec530cd12018-10-05 09:43:03 +0200679};
680
Ludovic Barrec530cd12018-10-05 09:43:03 +0200681static int stm32_qspi_probe(struct platform_device *pdev)
682{
683 struct device *dev = &pdev->dev;
684 struct spi_controller *ctrl;
685 struct reset_control *rstc;
686 struct stm32_qspi *qspi;
687 struct resource *res;
688 int ret, irq;
689
690 ctrl = spi_alloc_master(dev, sizeof(*qspi));
691 if (!ctrl)
692 return -ENOMEM;
693
694 qspi = spi_controller_get_devdata(ctrl);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100695 qspi->ctrl = ctrl;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200696
697 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi");
698 qspi->io_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100699 if (IS_ERR(qspi->io_base)) {
700 ret = PTR_ERR(qspi->io_base);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100701 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100702 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200703
Ludovic Barre245308c2019-03-25 18:01:40 +0100704 qspi->phys_base = res->start;
705
Ludovic Barrec530cd12018-10-05 09:43:03 +0200706 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm");
707 qspi->mm_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100708 if (IS_ERR(qspi->mm_base)) {
709 ret = PTR_ERR(qspi->mm_base);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100710 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100711 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200712
713 qspi->mm_size = resource_size(res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100714 if (qspi->mm_size > STM32_QSPI_MAX_MMAP_SZ) {
715 ret = -EINVAL;
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100716 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100717 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200718
719 irq = platform_get_irq(pdev, 0);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100720 if (irq < 0) {
721 ret = irq;
722 goto err_master_put;
723 }
Fabien Dessenne4b562de2019-04-24 17:19:00 +0200724
Ludovic Barrec530cd12018-10-05 09:43:03 +0200725 ret = devm_request_irq(dev, irq, stm32_qspi_irq, 0,
726 dev_name(dev), qspi);
727 if (ret) {
728 dev_err(dev, "failed to request irq\n");
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100729 goto err_master_put;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200730 }
731
732 init_completion(&qspi->data_completion);
Patrice Chotard86d1c6b2021-05-18 18:27:54 +0200733 init_completion(&qspi->match_completion);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200734
735 qspi->clk = devm_clk_get(dev, NULL);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100736 if (IS_ERR(qspi->clk)) {
737 ret = PTR_ERR(qspi->clk);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100738 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100739 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200740
741 qspi->clk_rate = clk_get_rate(qspi->clk);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100742 if (!qspi->clk_rate) {
743 ret = -EINVAL;
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100744 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100745 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200746
747 ret = clk_prepare_enable(qspi->clk);
748 if (ret) {
749 dev_err(dev, "can not enable the clock\n");
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100750 goto err_master_put;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200751 }
752
753 rstc = devm_reset_control_get_exclusive(dev, NULL);
Etienne Carriere8196f7b2020-02-03 14:50:47 +0100754 if (IS_ERR(rstc)) {
755 ret = PTR_ERR(rstc);
756 if (ret == -EPROBE_DEFER)
Patrice Chotard35700e22020-06-16 13:30:35 +0200757 goto err_clk_disable;
Etienne Carriere8196f7b2020-02-03 14:50:47 +0100758 } else {
Ludovic Barrec530cd12018-10-05 09:43:03 +0200759 reset_control_assert(rstc);
760 udelay(2);
761 reset_control_deassert(rstc);
762 }
763
764 qspi->dev = dev;
765 platform_set_drvdata(pdev, qspi);
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200766 ret = stm32_qspi_dma_setup(qspi);
767 if (ret)
Patrice Chotard35700e22020-06-16 13:30:35 +0200768 goto err_dma_free;
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200769
Ludovic Barrec530cd12018-10-05 09:43:03 +0200770 mutex_init(&qspi->lock);
771
772 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD
773 | SPI_TX_DUAL | SPI_TX_QUAD;
774 ctrl->setup = stm32_qspi_setup;
775 ctrl->bus_num = -1;
776 ctrl->mem_ops = &stm32_qspi_mem_ops;
777 ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP;
778 ctrl->dev.of_node = dev->of_node;
779
Patrice Chotard9d282c12020-04-17 14:12:41 +0200780 pm_runtime_set_autosuspend_delay(dev, STM32_AUTOSUSPEND_DELAY);
781 pm_runtime_use_autosuspend(dev);
782 pm_runtime_set_active(dev);
783 pm_runtime_enable(dev);
784 pm_runtime_get_noresume(dev);
785
Ludovic Barrec530cd12018-10-05 09:43:03 +0200786 ret = devm_spi_register_master(dev, ctrl);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200787 if (ret)
Patrice Chotard35700e22020-06-16 13:30:35 +0200788 goto err_pm_runtime_free;
Patrice Chotard9d282c12020-04-17 14:12:41 +0200789
790 pm_runtime_mark_last_busy(dev);
791 pm_runtime_put_autosuspend(dev);
792
793 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200794
Patrice Chotard35700e22020-06-16 13:30:35 +0200795err_pm_runtime_free:
796 pm_runtime_get_sync(qspi->dev);
797 /* disable qspi */
798 writel_relaxed(0, qspi->io_base + QSPI_CR);
799 mutex_destroy(&qspi->lock);
800 pm_runtime_put_noidle(qspi->dev);
801 pm_runtime_disable(qspi->dev);
802 pm_runtime_set_suspended(qspi->dev);
803 pm_runtime_dont_use_autosuspend(qspi->dev);
804err_dma_free:
805 stm32_qspi_dma_free(qspi);
806err_clk_disable:
807 clk_disable_unprepare(qspi->clk);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100808err_master_put:
Patrice Chotard3c0af1d2019-10-04 14:36:06 +0200809 spi_master_put(qspi->ctrl);
810
Ludovic Barrec530cd12018-10-05 09:43:03 +0200811 return ret;
812}
813
814static int stm32_qspi_remove(struct platform_device *pdev)
815{
816 struct stm32_qspi *qspi = platform_get_drvdata(pdev);
817
Patrice Chotard35700e22020-06-16 13:30:35 +0200818 pm_runtime_get_sync(qspi->dev);
819 /* disable qspi */
820 writel_relaxed(0, qspi->io_base + QSPI_CR);
821 stm32_qspi_dma_free(qspi);
822 mutex_destroy(&qspi->lock);
823 pm_runtime_put_noidle(qspi->dev);
824 pm_runtime_disable(qspi->dev);
825 pm_runtime_set_suspended(qspi->dev);
826 pm_runtime_dont_use_autosuspend(qspi->dev);
827 clk_disable_unprepare(qspi->clk);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200828
Ludovic Barrec530cd12018-10-05 09:43:03 +0200829 return 0;
830}
831
Patrice Chotard9d282c12020-04-17 14:12:41 +0200832static int __maybe_unused stm32_qspi_runtime_suspend(struct device *dev)
Ludovic Barre2e541b62019-03-08 14:12:21 +0100833{
834 struct stm32_qspi *qspi = dev_get_drvdata(dev);
835
836 clk_disable_unprepare(qspi->clk);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200837
838 return 0;
839}
840
841static int __maybe_unused stm32_qspi_runtime_resume(struct device *dev)
842{
843 struct stm32_qspi *qspi = dev_get_drvdata(dev);
844
845 return clk_prepare_enable(qspi->clk);
846}
847
848static int __maybe_unused stm32_qspi_suspend(struct device *dev)
849{
Ludovic Barre2e541b62019-03-08 14:12:21 +0100850 pinctrl_pm_select_sleep_state(dev);
851
Christophe Kerello102e9d12021-04-19 14:15:39 +0200852 return pm_runtime_force_suspend(dev);
Ludovic Barre2e541b62019-03-08 14:12:21 +0100853}
854
855static int __maybe_unused stm32_qspi_resume(struct device *dev)
856{
857 struct stm32_qspi *qspi = dev_get_drvdata(dev);
Christophe Kerello102e9d12021-04-19 14:15:39 +0200858 int ret;
859
860 ret = pm_runtime_force_resume(dev);
861 if (ret < 0)
862 return ret;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100863
864 pinctrl_pm_select_default_state(dev);
Christophe Kerello102e9d12021-04-19 14:15:39 +0200865
866 ret = pm_runtime_get_sync(dev);
867 if (ret < 0) {
868 pm_runtime_put_noidle(dev);
869 return ret;
870 }
Ludovic Barre2e541b62019-03-08 14:12:21 +0100871
872 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
873 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
874
Christophe Kerello102e9d12021-04-19 14:15:39 +0200875 pm_runtime_mark_last_busy(dev);
876 pm_runtime_put_autosuspend(dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200877
Ludovic Barre2e541b62019-03-08 14:12:21 +0100878 return 0;
879}
880
Patrice Chotard9d282c12020-04-17 14:12:41 +0200881static const struct dev_pm_ops stm32_qspi_pm_ops = {
882 SET_RUNTIME_PM_OPS(stm32_qspi_runtime_suspend,
883 stm32_qspi_runtime_resume, NULL)
884 SET_SYSTEM_SLEEP_PM_OPS(stm32_qspi_suspend, stm32_qspi_resume)
885};
Ludovic Barre2e541b62019-03-08 14:12:21 +0100886
Ludovic Barrec530cd12018-10-05 09:43:03 +0200887static const struct of_device_id stm32_qspi_match[] = {
888 {.compatible = "st,stm32f469-qspi"},
889 {}
890};
891MODULE_DEVICE_TABLE(of, stm32_qspi_match);
892
893static struct platform_driver stm32_qspi_driver = {
894 .probe = stm32_qspi_probe,
895 .remove = stm32_qspi_remove,
896 .driver = {
897 .name = "stm32-qspi",
898 .of_match_table = stm32_qspi_match,
Ludovic Barre2e541b62019-03-08 14:12:21 +0100899 .pm = &stm32_qspi_pm_ops,
Ludovic Barrec530cd12018-10-05 09:43:03 +0200900 },
901};
902module_platform_driver(stm32_qspi_driver);
903
904MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
905MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver");
906MODULE_LICENSE("GPL v2");