blob: 2786470a52011f1e60ac9de3979059b81149ce65 [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)
39#define CR_PRESC_MASK GENMASK(31, 24)
40
41#define QSPI_DCR 0x04
42#define DCR_FSIZE_MASK GENMASK(20, 16)
43
44#define QSPI_SR 0x08
45#define SR_TEF BIT(0)
46#define SR_TCF BIT(1)
47#define SR_FTF BIT(2)
48#define SR_SMF BIT(3)
49#define SR_TOF BIT(4)
50#define SR_BUSY BIT(5)
51#define SR_FLEVEL_MASK GENMASK(13, 8)
52
53#define QSPI_FCR 0x0c
54#define FCR_CTEF BIT(0)
55#define FCR_CTCF BIT(1)
56
57#define QSPI_DLR 0x10
58
59#define QSPI_CCR 0x14
60#define CCR_INST_MASK GENMASK(7, 0)
61#define CCR_IMODE_MASK GENMASK(9, 8)
62#define CCR_ADMODE_MASK GENMASK(11, 10)
63#define CCR_ADSIZE_MASK GENMASK(13, 12)
64#define CCR_DCYC_MASK GENMASK(22, 18)
65#define CCR_DMODE_MASK GENMASK(25, 24)
66#define CCR_FMODE_MASK GENMASK(27, 26)
67#define CCR_FMODE_INDW (0U << 26)
68#define CCR_FMODE_INDR (1U << 26)
69#define CCR_FMODE_APM (2U << 26)
70#define CCR_FMODE_MM (3U << 26)
71#define CCR_BUSWIDTH_0 0x0
72#define CCR_BUSWIDTH_1 0x1
73#define CCR_BUSWIDTH_2 0x2
74#define CCR_BUSWIDTH_4 0x3
75
76#define QSPI_AR 0x18
77#define QSPI_ABR 0x1c
78#define QSPI_DR 0x20
79#define QSPI_PSMKR 0x24
80#define QSPI_PSMAR 0x28
81#define QSPI_PIR 0x2c
82#define QSPI_LPTR 0x30
Ludovic Barrec530cd12018-10-05 09:43:03 +020083
84#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
85#define STM32_QSPI_MAX_NORCHIP 2
86
87#define STM32_FIFO_TIMEOUT_US 30000
88#define STM32_BUSY_TIMEOUT_US 100000
89#define STM32_ABT_TIMEOUT_US 100000
Ludovic Barre245308c2019-03-25 18:01:40 +010090#define STM32_COMP_TIMEOUT_MS 1000
Patrice Chotard9d282c12020-04-17 14:12:41 +020091#define STM32_AUTOSUSPEND_DELAY -1
Ludovic Barrec530cd12018-10-05 09:43:03 +020092
93struct stm32_qspi_flash {
94 struct stm32_qspi *qspi;
95 u32 cs;
96 u32 presc;
97};
98
99struct stm32_qspi {
100 struct device *dev;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100101 struct spi_controller *ctrl;
Ludovic Barre245308c2019-03-25 18:01:40 +0100102 phys_addr_t phys_base;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200103 void __iomem *io_base;
104 void __iomem *mm_base;
105 resource_size_t mm_size;
106 struct clk *clk;
107 u32 clk_rate;
108 struct stm32_qspi_flash flash[STM32_QSPI_MAX_NORCHIP];
109 struct completion data_completion;
110 u32 fmode;
111
Ludovic Barre245308c2019-03-25 18:01:40 +0100112 struct dma_chan *dma_chtx;
113 struct dma_chan *dma_chrx;
114 struct completion dma_completion;
115
Ludovic Barre2e541b62019-03-08 14:12:21 +0100116 u32 cr_reg;
117 u32 dcr_reg;
118
Ludovic Barrec530cd12018-10-05 09:43:03 +0200119 /*
120 * to protect device configuration, could be different between
121 * 2 flash access (bk1, bk2)
122 */
123 struct mutex lock;
124};
125
126static irqreturn_t stm32_qspi_irq(int irq, void *dev_id)
127{
128 struct stm32_qspi *qspi = (struct stm32_qspi *)dev_id;
129 u32 cr, sr;
130
131 sr = readl_relaxed(qspi->io_base + QSPI_SR);
132
133 if (sr & (SR_TEF | SR_TCF)) {
134 /* disable irq */
135 cr = readl_relaxed(qspi->io_base + QSPI_CR);
136 cr &= ~CR_TCIE & ~CR_TEIE;
137 writel_relaxed(cr, qspi->io_base + QSPI_CR);
138 complete(&qspi->data_completion);
139 }
140
141 return IRQ_HANDLED;
142}
143
144static void stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
145{
146 *val = readb_relaxed(addr);
147}
148
149static void stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
150{
151 writeb_relaxed(*val, addr);
152}
153
154static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
155 const struct spi_mem_op *op)
156{
157 void (*tx_fifo)(u8 *val, void __iomem *addr);
158 u32 len = op->data.nbytes, sr;
159 u8 *buf;
160 int ret;
161
162 if (op->data.dir == SPI_MEM_DATA_IN) {
163 tx_fifo = stm32_qspi_read_fifo;
164 buf = op->data.buf.in;
165
166 } else {
167 tx_fifo = stm32_qspi_write_fifo;
168 buf = (u8 *)op->data.buf.out;
169 }
170
171 while (len--) {
172 ret = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR,
173 sr, (sr & SR_FTF), 1,
174 STM32_FIFO_TIMEOUT_US);
175 if (ret) {
176 dev_err(qspi->dev, "fifo timeout (len:%d stat:%#x)\n",
177 len, sr);
178 return ret;
179 }
180 tx_fifo(buf++, qspi->io_base + QSPI_DR);
181 }
182
183 return 0;
184}
185
186static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
187 const struct spi_mem_op *op)
188{
189 memcpy_fromio(op->data.buf.in, qspi->mm_base + op->addr.val,
190 op->data.nbytes);
191 return 0;
192}
193
Ludovic Barre245308c2019-03-25 18:01:40 +0100194static void stm32_qspi_dma_callback(void *arg)
195{
196 struct completion *dma_completion = arg;
197
198 complete(dma_completion);
199}
200
201static int stm32_qspi_tx_dma(struct stm32_qspi *qspi,
202 const struct spi_mem_op *op)
203{
204 struct dma_async_tx_descriptor *desc;
205 enum dma_transfer_direction dma_dir;
206 struct dma_chan *dma_ch;
207 struct sg_table sgt;
208 dma_cookie_t cookie;
209 u32 cr, t_out;
210 int err;
211
212 if (op->data.dir == SPI_MEM_DATA_IN) {
213 dma_dir = DMA_DEV_TO_MEM;
214 dma_ch = qspi->dma_chrx;
215 } else {
216 dma_dir = DMA_MEM_TO_DEV;
217 dma_ch = qspi->dma_chtx;
218 }
219
220 /*
221 * spi_map_buf return -EINVAL if the buffer is not DMA-able
222 * (DMA-able: in vmalloc | kmap | virt_addr_valid)
223 */
224 err = spi_controller_dma_map_mem_op_data(qspi->ctrl, op, &sgt);
225 if (err)
226 return err;
227
228 desc = dmaengine_prep_slave_sg(dma_ch, sgt.sgl, sgt.nents,
229 dma_dir, DMA_PREP_INTERRUPT);
230 if (!desc) {
231 err = -ENOMEM;
232 goto out_unmap;
233 }
234
235 cr = readl_relaxed(qspi->io_base + QSPI_CR);
236
237 reinit_completion(&qspi->dma_completion);
238 desc->callback = stm32_qspi_dma_callback;
239 desc->callback_param = &qspi->dma_completion;
240 cookie = dmaengine_submit(desc);
241 err = dma_submit_error(cookie);
242 if (err)
243 goto out;
244
245 dma_async_issue_pending(dma_ch);
246
247 writel_relaxed(cr | CR_DMAEN, qspi->io_base + QSPI_CR);
248
249 t_out = sgt.nents * STM32_COMP_TIMEOUT_MS;
Ludovic Barre775c4c02019-06-27 09:43:59 +0200250 if (!wait_for_completion_timeout(&qspi->dma_completion,
251 msecs_to_jiffies(t_out)))
Ludovic Barre245308c2019-03-25 18:01:40 +0100252 err = -ETIMEDOUT;
253
254 if (err)
255 dmaengine_terminate_all(dma_ch);
256
257out:
258 writel_relaxed(cr & ~CR_DMAEN, qspi->io_base + QSPI_CR);
259out_unmap:
260 spi_controller_dma_unmap_mem_op_data(qspi->ctrl, op, &sgt);
261
262 return err;
263}
264
Ludovic Barrec530cd12018-10-05 09:43:03 +0200265static int stm32_qspi_tx(struct stm32_qspi *qspi, const struct spi_mem_op *op)
266{
267 if (!op->data.nbytes)
268 return 0;
269
270 if (qspi->fmode == CCR_FMODE_MM)
271 return stm32_qspi_tx_mm(qspi, op);
Ludovic Barre245308c2019-03-25 18:01:40 +0100272 else if ((op->data.dir == SPI_MEM_DATA_IN && qspi->dma_chrx) ||
273 (op->data.dir == SPI_MEM_DATA_OUT && qspi->dma_chtx))
274 if (!stm32_qspi_tx_dma(qspi, op))
275 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200276
277 return stm32_qspi_tx_poll(qspi, op);
278}
279
280static int stm32_qspi_wait_nobusy(struct stm32_qspi *qspi)
281{
282 u32 sr;
283
284 return readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, sr,
285 !(sr & SR_BUSY), 1,
286 STM32_BUSY_TIMEOUT_US);
287}
288
289static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi,
290 const struct spi_mem_op *op)
291{
292 u32 cr, sr;
293 int err = 0;
294
295 if (!op->data.nbytes)
296 return stm32_qspi_wait_nobusy(qspi);
297
298 if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF)
299 goto out;
300
301 reinit_completion(&qspi->data_completion);
302 cr = readl_relaxed(qspi->io_base + QSPI_CR);
303 writel_relaxed(cr | CR_TCIE | CR_TEIE, qspi->io_base + QSPI_CR);
304
Ludovic Barre775c4c02019-06-27 09:43:59 +0200305 if (!wait_for_completion_timeout(&qspi->data_completion,
Ludovic Barre245308c2019-03-25 18:01:40 +0100306 msecs_to_jiffies(STM32_COMP_TIMEOUT_MS))) {
Ludovic Barrec530cd12018-10-05 09:43:03 +0200307 err = -ETIMEDOUT;
308 } else {
309 sr = readl_relaxed(qspi->io_base + QSPI_SR);
310 if (sr & SR_TEF)
311 err = -EIO;
312 }
313
314out:
315 /* clear flags */
316 writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR);
317
318 return err;
319}
320
321static int stm32_qspi_get_mode(struct stm32_qspi *qspi, u8 buswidth)
322{
323 if (buswidth == 4)
324 return CCR_BUSWIDTH_4;
325
326 return buswidth;
327}
328
329static int stm32_qspi_send(struct spi_mem *mem, const struct spi_mem_op *op)
330{
331 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
332 struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select];
333 u32 ccr, cr, addr_max;
334 int timeout, err = 0;
335
336 dev_dbg(qspi->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
337 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
338 op->dummy.buswidth, op->data.buswidth,
339 op->addr.val, op->data.nbytes);
340
341 err = stm32_qspi_wait_nobusy(qspi);
342 if (err)
343 goto abort;
344
345 addr_max = op->addr.val + op->data.nbytes + 1;
346
347 if (op->data.dir == SPI_MEM_DATA_IN) {
348 if (addr_max < qspi->mm_size &&
349 op->addr.buswidth)
350 qspi->fmode = CCR_FMODE_MM;
351 else
352 qspi->fmode = CCR_FMODE_INDR;
353 } else {
354 qspi->fmode = CCR_FMODE_INDW;
355 }
356
357 cr = readl_relaxed(qspi->io_base + QSPI_CR);
358 cr &= ~CR_PRESC_MASK & ~CR_FSEL;
359 cr |= FIELD_PREP(CR_PRESC_MASK, flash->presc);
360 cr |= FIELD_PREP(CR_FSEL, flash->cs);
361 writel_relaxed(cr, qspi->io_base + QSPI_CR);
362
363 if (op->data.nbytes)
364 writel_relaxed(op->data.nbytes - 1,
365 qspi->io_base + QSPI_DLR);
366 else
367 qspi->fmode = CCR_FMODE_INDW;
368
369 ccr = qspi->fmode;
370 ccr |= FIELD_PREP(CCR_INST_MASK, op->cmd.opcode);
371 ccr |= FIELD_PREP(CCR_IMODE_MASK,
372 stm32_qspi_get_mode(qspi, op->cmd.buswidth));
373
374 if (op->addr.nbytes) {
375 ccr |= FIELD_PREP(CCR_ADMODE_MASK,
376 stm32_qspi_get_mode(qspi, op->addr.buswidth));
377 ccr |= FIELD_PREP(CCR_ADSIZE_MASK, op->addr.nbytes - 1);
378 }
379
380 if (op->dummy.buswidth && op->dummy.nbytes)
381 ccr |= FIELD_PREP(CCR_DCYC_MASK,
382 op->dummy.nbytes * 8 / op->dummy.buswidth);
383
384 if (op->data.nbytes) {
385 ccr |= FIELD_PREP(CCR_DMODE_MASK,
386 stm32_qspi_get_mode(qspi, op->data.buswidth));
387 }
388
389 writel_relaxed(ccr, qspi->io_base + QSPI_CCR);
390
391 if (op->addr.nbytes && qspi->fmode != CCR_FMODE_MM)
392 writel_relaxed(op->addr.val, qspi->io_base + QSPI_AR);
393
394 err = stm32_qspi_tx(qspi, op);
395
396 /*
397 * Abort in:
398 * -error case
399 * -read memory map: prefetching must be stopped if we read the last
400 * byte of device (device size - fifo size). like device size is not
401 * knows, the prefetching is always stop.
402 */
403 if (err || qspi->fmode == CCR_FMODE_MM)
404 goto abort;
405
406 /* wait end of tx in indirect mode */
407 err = stm32_qspi_wait_cmd(qspi, op);
408 if (err)
409 goto abort;
410
411 return 0;
412
413abort:
414 cr = readl_relaxed(qspi->io_base + QSPI_CR) | CR_ABORT;
415 writel_relaxed(cr, qspi->io_base + QSPI_CR);
416
417 /* wait clear of abort bit by hw */
418 timeout = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_CR,
419 cr, !(cr & CR_ABORT), 1,
420 STM32_ABT_TIMEOUT_US);
421
422 writel_relaxed(FCR_CTCF, qspi->io_base + QSPI_FCR);
423
424 if (err || timeout)
425 dev_err(qspi->dev, "%s err:%d abort timeout:%d\n",
426 __func__, err, timeout);
427
428 return err;
429}
430
431static int stm32_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
432{
433 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
434 int ret;
435
Patrice Chotard9d282c12020-04-17 14:12:41 +0200436 ret = pm_runtime_get_sync(qspi->dev);
Zhang Qilong88e14192020-11-06 09:53:57 +0800437 if (ret < 0) {
438 pm_runtime_put_noidle(qspi->dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200439 return ret;
Zhang Qilong88e14192020-11-06 09:53:57 +0800440 }
Patrice Chotard9d282c12020-04-17 14:12:41 +0200441
Ludovic Barrec530cd12018-10-05 09:43:03 +0200442 mutex_lock(&qspi->lock);
443 ret = stm32_qspi_send(mem, op);
444 mutex_unlock(&qspi->lock);
445
Patrice Chotard9d282c12020-04-17 14:12:41 +0200446 pm_runtime_mark_last_busy(qspi->dev);
447 pm_runtime_put_autosuspend(qspi->dev);
448
Ludovic Barrec530cd12018-10-05 09:43:03 +0200449 return ret;
450}
451
452static int stm32_qspi_setup(struct spi_device *spi)
453{
454 struct spi_controller *ctrl = spi->master;
455 struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl);
456 struct stm32_qspi_flash *flash;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100457 u32 presc;
Patrice Chotard9d282c12020-04-17 14:12:41 +0200458 int ret;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200459
460 if (ctrl->busy)
461 return -EBUSY;
462
463 if (!spi->max_speed_hz)
464 return -EINVAL;
465
Patrice Chotard9d282c12020-04-17 14:12:41 +0200466 ret = pm_runtime_get_sync(qspi->dev);
Zhang Qilong88e14192020-11-06 09:53:57 +0800467 if (ret < 0) {
468 pm_runtime_put_noidle(qspi->dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200469 return ret;
Zhang Qilong88e14192020-11-06 09:53:57 +0800470 }
Patrice Chotard9d282c12020-04-17 14:12:41 +0200471
Ludovic Barrec530cd12018-10-05 09:43:03 +0200472 presc = DIV_ROUND_UP(qspi->clk_rate, spi->max_speed_hz) - 1;
473
474 flash = &qspi->flash[spi->chip_select];
475 flash->qspi = qspi;
476 flash->cs = spi->chip_select;
477 flash->presc = presc;
478
479 mutex_lock(&qspi->lock);
Patrice Chotard94613d52019-06-20 15:13:23 +0200480 qspi->cr_reg = 3 << CR_FTHRES_SHIFT | CR_SSHIFT | CR_EN;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100481 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200482
483 /* set dcr fsize to max address */
Ludovic Barre2e541b62019-03-08 14:12:21 +0100484 qspi->dcr_reg = DCR_FSIZE_MASK;
485 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200486 mutex_unlock(&qspi->lock);
487
Patrice Chotard9d282c12020-04-17 14:12:41 +0200488 pm_runtime_mark_last_busy(qspi->dev);
489 pm_runtime_put_autosuspend(qspi->dev);
490
Ludovic Barrec530cd12018-10-05 09:43:03 +0200491 return 0;
492}
493
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200494static int stm32_qspi_dma_setup(struct stm32_qspi *qspi)
Ludovic Barre245308c2019-03-25 18:01:40 +0100495{
496 struct dma_slave_config dma_cfg;
497 struct device *dev = qspi->dev;
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200498 int ret = 0;
Ludovic Barre245308c2019-03-25 18:01:40 +0100499
500 memset(&dma_cfg, 0, sizeof(dma_cfg));
501
502 dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
503 dma_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
504 dma_cfg.src_addr = qspi->phys_base + QSPI_DR;
505 dma_cfg.dst_addr = qspi->phys_base + QSPI_DR;
506 dma_cfg.src_maxburst = 4;
507 dma_cfg.dst_maxburst = 4;
508
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200509 qspi->dma_chrx = dma_request_chan(dev, "rx");
510 if (IS_ERR(qspi->dma_chrx)) {
511 ret = PTR_ERR(qspi->dma_chrx);
512 qspi->dma_chrx = NULL;
513 if (ret == -EPROBE_DEFER)
514 goto out;
515 } else {
Ludovic Barre245308c2019-03-25 18:01:40 +0100516 if (dmaengine_slave_config(qspi->dma_chrx, &dma_cfg)) {
517 dev_err(dev, "dma rx config failed\n");
518 dma_release_channel(qspi->dma_chrx);
519 qspi->dma_chrx = NULL;
520 }
521 }
522
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200523 qspi->dma_chtx = dma_request_chan(dev, "tx");
524 if (IS_ERR(qspi->dma_chtx)) {
525 ret = PTR_ERR(qspi->dma_chtx);
526 qspi->dma_chtx = NULL;
527 } else {
Ludovic Barre245308c2019-03-25 18:01:40 +0100528 if (dmaengine_slave_config(qspi->dma_chtx, &dma_cfg)) {
529 dev_err(dev, "dma tx config failed\n");
530 dma_release_channel(qspi->dma_chtx);
531 qspi->dma_chtx = NULL;
532 }
533 }
534
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200535out:
Ludovic Barre245308c2019-03-25 18:01:40 +0100536 init_completion(&qspi->dma_completion);
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200537
538 if (ret != -EPROBE_DEFER)
539 ret = 0;
540
541 return ret;
Ludovic Barre245308c2019-03-25 18:01:40 +0100542}
543
544static void stm32_qspi_dma_free(struct stm32_qspi *qspi)
545{
546 if (qspi->dma_chtx)
547 dma_release_channel(qspi->dma_chtx);
548 if (qspi->dma_chrx)
549 dma_release_channel(qspi->dma_chrx);
550}
551
Ludovic Barrec530cd12018-10-05 09:43:03 +0200552/*
553 * no special host constraint, so use default spi_mem_default_supports_op
554 * to check supported mode.
555 */
556static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
557 .exec_op = stm32_qspi_exec_op,
558};
559
Ludovic Barrec530cd12018-10-05 09:43:03 +0200560static int stm32_qspi_probe(struct platform_device *pdev)
561{
562 struct device *dev = &pdev->dev;
563 struct spi_controller *ctrl;
564 struct reset_control *rstc;
565 struct stm32_qspi *qspi;
566 struct resource *res;
567 int ret, irq;
568
569 ctrl = spi_alloc_master(dev, sizeof(*qspi));
570 if (!ctrl)
571 return -ENOMEM;
572
573 qspi = spi_controller_get_devdata(ctrl);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100574 qspi->ctrl = ctrl;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200575
576 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi");
577 qspi->io_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100578 if (IS_ERR(qspi->io_base)) {
579 ret = PTR_ERR(qspi->io_base);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100580 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100581 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200582
Ludovic Barre245308c2019-03-25 18:01:40 +0100583 qspi->phys_base = res->start;
584
Ludovic Barrec530cd12018-10-05 09:43:03 +0200585 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm");
586 qspi->mm_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100587 if (IS_ERR(qspi->mm_base)) {
588 ret = PTR_ERR(qspi->mm_base);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100589 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100590 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200591
592 qspi->mm_size = resource_size(res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100593 if (qspi->mm_size > STM32_QSPI_MAX_MMAP_SZ) {
594 ret = -EINVAL;
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100595 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100596 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200597
598 irq = platform_get_irq(pdev, 0);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100599 if (irq < 0) {
600 ret = irq;
601 goto err_master_put;
602 }
Fabien Dessenne4b562de2019-04-24 17:19:00 +0200603
Ludovic Barrec530cd12018-10-05 09:43:03 +0200604 ret = devm_request_irq(dev, irq, stm32_qspi_irq, 0,
605 dev_name(dev), qspi);
606 if (ret) {
607 dev_err(dev, "failed to request irq\n");
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100608 goto err_master_put;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200609 }
610
611 init_completion(&qspi->data_completion);
612
613 qspi->clk = devm_clk_get(dev, NULL);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100614 if (IS_ERR(qspi->clk)) {
615 ret = PTR_ERR(qspi->clk);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100616 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100617 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200618
619 qspi->clk_rate = clk_get_rate(qspi->clk);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100620 if (!qspi->clk_rate) {
621 ret = -EINVAL;
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100622 goto err_master_put;
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100623 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200624
625 ret = clk_prepare_enable(qspi->clk);
626 if (ret) {
627 dev_err(dev, "can not enable the clock\n");
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100628 goto err_master_put;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200629 }
630
631 rstc = devm_reset_control_get_exclusive(dev, NULL);
Etienne Carriere8196f7b2020-02-03 14:50:47 +0100632 if (IS_ERR(rstc)) {
633 ret = PTR_ERR(rstc);
634 if (ret == -EPROBE_DEFER)
Patrice Chotard35700e22020-06-16 13:30:35 +0200635 goto err_clk_disable;
Etienne Carriere8196f7b2020-02-03 14:50:47 +0100636 } else {
Ludovic Barrec530cd12018-10-05 09:43:03 +0200637 reset_control_assert(rstc);
638 udelay(2);
639 reset_control_deassert(rstc);
640 }
641
642 qspi->dev = dev;
643 platform_set_drvdata(pdev, qspi);
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200644 ret = stm32_qspi_dma_setup(qspi);
645 if (ret)
Patrice Chotard35700e22020-06-16 13:30:35 +0200646 goto err_dma_free;
Peter Ujfalusi658606f2019-12-12 15:55:49 +0200647
Ludovic Barrec530cd12018-10-05 09:43:03 +0200648 mutex_init(&qspi->lock);
649
650 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD
651 | SPI_TX_DUAL | SPI_TX_QUAD;
652 ctrl->setup = stm32_qspi_setup;
653 ctrl->bus_num = -1;
654 ctrl->mem_ops = &stm32_qspi_mem_ops;
655 ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP;
656 ctrl->dev.of_node = dev->of_node;
657
Patrice Chotard9d282c12020-04-17 14:12:41 +0200658 pm_runtime_set_autosuspend_delay(dev, STM32_AUTOSUSPEND_DELAY);
659 pm_runtime_use_autosuspend(dev);
660 pm_runtime_set_active(dev);
661 pm_runtime_enable(dev);
662 pm_runtime_get_noresume(dev);
663
Ludovic Barrec530cd12018-10-05 09:43:03 +0200664 ret = devm_spi_register_master(dev, ctrl);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200665 if (ret)
Patrice Chotard35700e22020-06-16 13:30:35 +0200666 goto err_pm_runtime_free;
Patrice Chotard9d282c12020-04-17 14:12:41 +0200667
668 pm_runtime_mark_last_busy(dev);
669 pm_runtime_put_autosuspend(dev);
670
671 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200672
Patrice Chotard35700e22020-06-16 13:30:35 +0200673err_pm_runtime_free:
674 pm_runtime_get_sync(qspi->dev);
675 /* disable qspi */
676 writel_relaxed(0, qspi->io_base + QSPI_CR);
677 mutex_destroy(&qspi->lock);
678 pm_runtime_put_noidle(qspi->dev);
679 pm_runtime_disable(qspi->dev);
680 pm_runtime_set_suspended(qspi->dev);
681 pm_runtime_dont_use_autosuspend(qspi->dev);
682err_dma_free:
683 stm32_qspi_dma_free(qspi);
684err_clk_disable:
685 clk_disable_unprepare(qspi->clk);
Lionel Debieve4a08d6c2020-02-03 14:50:48 +0100686err_master_put:
Patrice Chotard3c0af1d2019-10-04 14:36:06 +0200687 spi_master_put(qspi->ctrl);
688
Ludovic Barrec530cd12018-10-05 09:43:03 +0200689 return ret;
690}
691
692static int stm32_qspi_remove(struct platform_device *pdev)
693{
694 struct stm32_qspi *qspi = platform_get_drvdata(pdev);
695
Patrice Chotard35700e22020-06-16 13:30:35 +0200696 pm_runtime_get_sync(qspi->dev);
697 /* disable qspi */
698 writel_relaxed(0, qspi->io_base + QSPI_CR);
699 stm32_qspi_dma_free(qspi);
700 mutex_destroy(&qspi->lock);
701 pm_runtime_put_noidle(qspi->dev);
702 pm_runtime_disable(qspi->dev);
703 pm_runtime_set_suspended(qspi->dev);
704 pm_runtime_dont_use_autosuspend(qspi->dev);
705 clk_disable_unprepare(qspi->clk);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200706
Ludovic Barrec530cd12018-10-05 09:43:03 +0200707 return 0;
708}
709
Patrice Chotard9d282c12020-04-17 14:12:41 +0200710static int __maybe_unused stm32_qspi_runtime_suspend(struct device *dev)
Ludovic Barre2e541b62019-03-08 14:12:21 +0100711{
712 struct stm32_qspi *qspi = dev_get_drvdata(dev);
713
714 clk_disable_unprepare(qspi->clk);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200715
716 return 0;
717}
718
719static int __maybe_unused stm32_qspi_runtime_resume(struct device *dev)
720{
721 struct stm32_qspi *qspi = dev_get_drvdata(dev);
722
723 return clk_prepare_enable(qspi->clk);
724}
725
726static int __maybe_unused stm32_qspi_suspend(struct device *dev)
727{
Ludovic Barre2e541b62019-03-08 14:12:21 +0100728 pinctrl_pm_select_sleep_state(dev);
729
Christophe Kerello102e9d12021-04-19 14:15:39 +0200730 return pm_runtime_force_suspend(dev);
Ludovic Barre2e541b62019-03-08 14:12:21 +0100731}
732
733static int __maybe_unused stm32_qspi_resume(struct device *dev)
734{
735 struct stm32_qspi *qspi = dev_get_drvdata(dev);
Christophe Kerello102e9d12021-04-19 14:15:39 +0200736 int ret;
737
738 ret = pm_runtime_force_resume(dev);
739 if (ret < 0)
740 return ret;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100741
742 pinctrl_pm_select_default_state(dev);
Christophe Kerello102e9d12021-04-19 14:15:39 +0200743
744 ret = pm_runtime_get_sync(dev);
745 if (ret < 0) {
746 pm_runtime_put_noidle(dev);
747 return ret;
748 }
Ludovic Barre2e541b62019-03-08 14:12:21 +0100749
750 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
751 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
752
Christophe Kerello102e9d12021-04-19 14:15:39 +0200753 pm_runtime_mark_last_busy(dev);
754 pm_runtime_put_autosuspend(dev);
Patrice Chotard9d282c12020-04-17 14:12:41 +0200755
Ludovic Barre2e541b62019-03-08 14:12:21 +0100756 return 0;
757}
758
Patrice Chotard9d282c12020-04-17 14:12:41 +0200759static const struct dev_pm_ops stm32_qspi_pm_ops = {
760 SET_RUNTIME_PM_OPS(stm32_qspi_runtime_suspend,
761 stm32_qspi_runtime_resume, NULL)
762 SET_SYSTEM_SLEEP_PM_OPS(stm32_qspi_suspend, stm32_qspi_resume)
763};
Ludovic Barre2e541b62019-03-08 14:12:21 +0100764
Ludovic Barrec530cd12018-10-05 09:43:03 +0200765static const struct of_device_id stm32_qspi_match[] = {
766 {.compatible = "st,stm32f469-qspi"},
767 {}
768};
769MODULE_DEVICE_TABLE(of, stm32_qspi_match);
770
771static struct platform_driver stm32_qspi_driver = {
772 .probe = stm32_qspi_probe,
773 .remove = stm32_qspi_remove,
774 .driver = {
775 .name = "stm32-qspi",
776 .of_match_table = stm32_qspi_match,
Ludovic Barre2e541b62019-03-08 14:12:21 +0100777 .pm = &stm32_qspi_pm_ops,
Ludovic Barrec530cd12018-10-05 09:43:03 +0200778 },
779};
780module_platform_driver(stm32_qspi_driver);
781
782MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
783MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver");
784MODULE_LICENSE("GPL v2");