blob: 42f8e3c6aa1ff15d03a30cd6bdd040aba0f6000b [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>
Ludovic Barrec530cd12018-10-05 09:43:03 +020019#include <linux/platform_device.h>
20#include <linux/reset.h>
21#include <linux/sizes.h>
22#include <linux/spi/spi-mem.h>
23
24#define QSPI_CR 0x00
25#define CR_EN BIT(0)
26#define CR_ABORT BIT(1)
27#define CR_DMAEN BIT(2)
28#define CR_TCEN BIT(3)
29#define CR_SSHIFT BIT(4)
30#define CR_DFM BIT(6)
31#define CR_FSEL BIT(7)
32#define CR_FTHRES_MASK GENMASK(12, 8)
33#define CR_TEIE BIT(16)
34#define CR_TCIE BIT(17)
35#define CR_FTIE BIT(18)
36#define CR_SMIE BIT(19)
37#define CR_TOIE BIT(20)
38#define CR_PRESC_MASK GENMASK(31, 24)
39
40#define QSPI_DCR 0x04
41#define DCR_FSIZE_MASK GENMASK(20, 16)
42
43#define QSPI_SR 0x08
44#define SR_TEF BIT(0)
45#define SR_TCF BIT(1)
46#define SR_FTF BIT(2)
47#define SR_SMF BIT(3)
48#define SR_TOF BIT(4)
49#define SR_BUSY BIT(5)
50#define SR_FLEVEL_MASK GENMASK(13, 8)
51
52#define QSPI_FCR 0x0c
53#define FCR_CTEF BIT(0)
54#define FCR_CTCF BIT(1)
55
56#define QSPI_DLR 0x10
57
58#define QSPI_CCR 0x14
59#define CCR_INST_MASK GENMASK(7, 0)
60#define CCR_IMODE_MASK GENMASK(9, 8)
61#define CCR_ADMODE_MASK GENMASK(11, 10)
62#define CCR_ADSIZE_MASK GENMASK(13, 12)
63#define CCR_DCYC_MASK GENMASK(22, 18)
64#define CCR_DMODE_MASK GENMASK(25, 24)
65#define CCR_FMODE_MASK GENMASK(27, 26)
66#define CCR_FMODE_INDW (0U << 26)
67#define CCR_FMODE_INDR (1U << 26)
68#define CCR_FMODE_APM (2U << 26)
69#define CCR_FMODE_MM (3U << 26)
70#define CCR_BUSWIDTH_0 0x0
71#define CCR_BUSWIDTH_1 0x1
72#define CCR_BUSWIDTH_2 0x2
73#define CCR_BUSWIDTH_4 0x3
74
75#define QSPI_AR 0x18
76#define QSPI_ABR 0x1c
77#define QSPI_DR 0x20
78#define QSPI_PSMKR 0x24
79#define QSPI_PSMAR 0x28
80#define QSPI_PIR 0x2c
81#define QSPI_LPTR 0x30
Ludovic Barrec530cd12018-10-05 09:43:03 +020082
83#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
84#define STM32_QSPI_MAX_NORCHIP 2
85
86#define STM32_FIFO_TIMEOUT_US 30000
87#define STM32_BUSY_TIMEOUT_US 100000
88#define STM32_ABT_TIMEOUT_US 100000
Ludovic Barre245308c2019-03-25 18:01:40 +010089#define STM32_COMP_TIMEOUT_MS 1000
Ludovic Barrec530cd12018-10-05 09:43:03 +020090
91struct stm32_qspi_flash {
92 struct stm32_qspi *qspi;
93 u32 cs;
94 u32 presc;
95};
96
97struct stm32_qspi {
98 struct device *dev;
Ludovic Barrea88eceb2019-03-25 18:01:39 +010099 struct spi_controller *ctrl;
Ludovic Barre245308c2019-03-25 18:01:40 +0100100 phys_addr_t phys_base;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200101 void __iomem *io_base;
102 void __iomem *mm_base;
103 resource_size_t mm_size;
104 struct clk *clk;
105 u32 clk_rate;
106 struct stm32_qspi_flash flash[STM32_QSPI_MAX_NORCHIP];
107 struct completion data_completion;
108 u32 fmode;
109
Ludovic Barre245308c2019-03-25 18:01:40 +0100110 struct dma_chan *dma_chtx;
111 struct dma_chan *dma_chrx;
112 struct completion dma_completion;
113
Ludovic Barre2e541b62019-03-08 14:12:21 +0100114 u32 cr_reg;
115 u32 dcr_reg;
116
Ludovic Barrec530cd12018-10-05 09:43:03 +0200117 /*
118 * to protect device configuration, could be different between
119 * 2 flash access (bk1, bk2)
120 */
121 struct mutex lock;
122};
123
124static irqreturn_t stm32_qspi_irq(int irq, void *dev_id)
125{
126 struct stm32_qspi *qspi = (struct stm32_qspi *)dev_id;
127 u32 cr, sr;
128
129 sr = readl_relaxed(qspi->io_base + QSPI_SR);
130
131 if (sr & (SR_TEF | SR_TCF)) {
132 /* disable irq */
133 cr = readl_relaxed(qspi->io_base + QSPI_CR);
134 cr &= ~CR_TCIE & ~CR_TEIE;
135 writel_relaxed(cr, qspi->io_base + QSPI_CR);
136 complete(&qspi->data_completion);
137 }
138
139 return IRQ_HANDLED;
140}
141
142static void stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
143{
144 *val = readb_relaxed(addr);
145}
146
147static void stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
148{
149 writeb_relaxed(*val, addr);
150}
151
152static int stm32_qspi_tx_poll(struct stm32_qspi *qspi,
153 const struct spi_mem_op *op)
154{
155 void (*tx_fifo)(u8 *val, void __iomem *addr);
156 u32 len = op->data.nbytes, sr;
157 u8 *buf;
158 int ret;
159
160 if (op->data.dir == SPI_MEM_DATA_IN) {
161 tx_fifo = stm32_qspi_read_fifo;
162 buf = op->data.buf.in;
163
164 } else {
165 tx_fifo = stm32_qspi_write_fifo;
166 buf = (u8 *)op->data.buf.out;
167 }
168
169 while (len--) {
170 ret = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR,
171 sr, (sr & SR_FTF), 1,
172 STM32_FIFO_TIMEOUT_US);
173 if (ret) {
174 dev_err(qspi->dev, "fifo timeout (len:%d stat:%#x)\n",
175 len, sr);
176 return ret;
177 }
178 tx_fifo(buf++, qspi->io_base + QSPI_DR);
179 }
180
181 return 0;
182}
183
184static int stm32_qspi_tx_mm(struct stm32_qspi *qspi,
185 const struct spi_mem_op *op)
186{
187 memcpy_fromio(op->data.buf.in, qspi->mm_base + op->addr.val,
188 op->data.nbytes);
189 return 0;
190}
191
Ludovic Barre245308c2019-03-25 18:01:40 +0100192static void stm32_qspi_dma_callback(void *arg)
193{
194 struct completion *dma_completion = arg;
195
196 complete(dma_completion);
197}
198
199static int stm32_qspi_tx_dma(struct stm32_qspi *qspi,
200 const struct spi_mem_op *op)
201{
202 struct dma_async_tx_descriptor *desc;
203 enum dma_transfer_direction dma_dir;
204 struct dma_chan *dma_ch;
205 struct sg_table sgt;
206 dma_cookie_t cookie;
207 u32 cr, t_out;
208 int err;
209
210 if (op->data.dir == SPI_MEM_DATA_IN) {
211 dma_dir = DMA_DEV_TO_MEM;
212 dma_ch = qspi->dma_chrx;
213 } else {
214 dma_dir = DMA_MEM_TO_DEV;
215 dma_ch = qspi->dma_chtx;
216 }
217
218 /*
219 * spi_map_buf return -EINVAL if the buffer is not DMA-able
220 * (DMA-able: in vmalloc | kmap | virt_addr_valid)
221 */
222 err = spi_controller_dma_map_mem_op_data(qspi->ctrl, op, &sgt);
223 if (err)
224 return err;
225
226 desc = dmaengine_prep_slave_sg(dma_ch, sgt.sgl, sgt.nents,
227 dma_dir, DMA_PREP_INTERRUPT);
228 if (!desc) {
229 err = -ENOMEM;
230 goto out_unmap;
231 }
232
233 cr = readl_relaxed(qspi->io_base + QSPI_CR);
234
235 reinit_completion(&qspi->dma_completion);
236 desc->callback = stm32_qspi_dma_callback;
237 desc->callback_param = &qspi->dma_completion;
238 cookie = dmaengine_submit(desc);
239 err = dma_submit_error(cookie);
240 if (err)
241 goto out;
242
243 dma_async_issue_pending(dma_ch);
244
245 writel_relaxed(cr | CR_DMAEN, qspi->io_base + QSPI_CR);
246
247 t_out = sgt.nents * STM32_COMP_TIMEOUT_MS;
248 if (!wait_for_completion_interruptible_timeout(&qspi->dma_completion,
249 msecs_to_jiffies(t_out)))
250 err = -ETIMEDOUT;
251
252 if (dma_async_is_tx_complete(dma_ch, cookie,
253 NULL, NULL) != DMA_COMPLETE)
254 err = -ETIMEDOUT;
255
256 if (err)
257 dmaengine_terminate_all(dma_ch);
258
259out:
260 writel_relaxed(cr & ~CR_DMAEN, qspi->io_base + QSPI_CR);
261out_unmap:
262 spi_controller_dma_unmap_mem_op_data(qspi->ctrl, op, &sgt);
263
264 return err;
265}
266
Ludovic Barrec530cd12018-10-05 09:43:03 +0200267static int stm32_qspi_tx(struct stm32_qspi *qspi, const struct spi_mem_op *op)
268{
269 if (!op->data.nbytes)
270 return 0;
271
272 if (qspi->fmode == CCR_FMODE_MM)
273 return stm32_qspi_tx_mm(qspi, op);
Ludovic Barre245308c2019-03-25 18:01:40 +0100274 else if ((op->data.dir == SPI_MEM_DATA_IN && qspi->dma_chrx) ||
275 (op->data.dir == SPI_MEM_DATA_OUT && qspi->dma_chtx))
276 if (!stm32_qspi_tx_dma(qspi, op))
277 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200278
279 return stm32_qspi_tx_poll(qspi, op);
280}
281
282static int stm32_qspi_wait_nobusy(struct stm32_qspi *qspi)
283{
284 u32 sr;
285
286 return readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, sr,
287 !(sr & SR_BUSY), 1,
288 STM32_BUSY_TIMEOUT_US);
289}
290
291static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi,
292 const struct spi_mem_op *op)
293{
294 u32 cr, sr;
295 int err = 0;
296
297 if (!op->data.nbytes)
298 return stm32_qspi_wait_nobusy(qspi);
299
300 if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF)
301 goto out;
302
303 reinit_completion(&qspi->data_completion);
304 cr = readl_relaxed(qspi->io_base + QSPI_CR);
305 writel_relaxed(cr | CR_TCIE | CR_TEIE, qspi->io_base + QSPI_CR);
306
307 if (!wait_for_completion_interruptible_timeout(&qspi->data_completion,
Ludovic Barre245308c2019-03-25 18:01:40 +0100308 msecs_to_jiffies(STM32_COMP_TIMEOUT_MS))) {
Ludovic Barrec530cd12018-10-05 09:43:03 +0200309 err = -ETIMEDOUT;
310 } else {
311 sr = readl_relaxed(qspi->io_base + QSPI_SR);
312 if (sr & SR_TEF)
313 err = -EIO;
314 }
315
316out:
317 /* clear flags */
318 writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR);
319
320 return err;
321}
322
323static int stm32_qspi_get_mode(struct stm32_qspi *qspi, u8 buswidth)
324{
325 if (buswidth == 4)
326 return CCR_BUSWIDTH_4;
327
328 return buswidth;
329}
330
331static int stm32_qspi_send(struct spi_mem *mem, const struct spi_mem_op *op)
332{
333 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
334 struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select];
335 u32 ccr, cr, addr_max;
336 int timeout, err = 0;
337
338 dev_dbg(qspi->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
339 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
340 op->dummy.buswidth, op->data.buswidth,
341 op->addr.val, op->data.nbytes);
342
343 err = stm32_qspi_wait_nobusy(qspi);
344 if (err)
345 goto abort;
346
347 addr_max = op->addr.val + op->data.nbytes + 1;
348
349 if (op->data.dir == SPI_MEM_DATA_IN) {
350 if (addr_max < qspi->mm_size &&
351 op->addr.buswidth)
352 qspi->fmode = CCR_FMODE_MM;
353 else
354 qspi->fmode = CCR_FMODE_INDR;
355 } else {
356 qspi->fmode = CCR_FMODE_INDW;
357 }
358
359 cr = readl_relaxed(qspi->io_base + QSPI_CR);
360 cr &= ~CR_PRESC_MASK & ~CR_FSEL;
361 cr |= FIELD_PREP(CR_PRESC_MASK, flash->presc);
362 cr |= FIELD_PREP(CR_FSEL, flash->cs);
363 writel_relaxed(cr, qspi->io_base + QSPI_CR);
364
365 if (op->data.nbytes)
366 writel_relaxed(op->data.nbytes - 1,
367 qspi->io_base + QSPI_DLR);
368 else
369 qspi->fmode = CCR_FMODE_INDW;
370
371 ccr = qspi->fmode;
372 ccr |= FIELD_PREP(CCR_INST_MASK, op->cmd.opcode);
373 ccr |= FIELD_PREP(CCR_IMODE_MASK,
374 stm32_qspi_get_mode(qspi, op->cmd.buswidth));
375
376 if (op->addr.nbytes) {
377 ccr |= FIELD_PREP(CCR_ADMODE_MASK,
378 stm32_qspi_get_mode(qspi, op->addr.buswidth));
379 ccr |= FIELD_PREP(CCR_ADSIZE_MASK, op->addr.nbytes - 1);
380 }
381
382 if (op->dummy.buswidth && op->dummy.nbytes)
383 ccr |= FIELD_PREP(CCR_DCYC_MASK,
384 op->dummy.nbytes * 8 / op->dummy.buswidth);
385
386 if (op->data.nbytes) {
387 ccr |= FIELD_PREP(CCR_DMODE_MASK,
388 stm32_qspi_get_mode(qspi, op->data.buswidth));
389 }
390
391 writel_relaxed(ccr, qspi->io_base + QSPI_CCR);
392
393 if (op->addr.nbytes && qspi->fmode != CCR_FMODE_MM)
394 writel_relaxed(op->addr.val, qspi->io_base + QSPI_AR);
395
396 err = stm32_qspi_tx(qspi, op);
397
398 /*
399 * Abort in:
400 * -error case
401 * -read memory map: prefetching must be stopped if we read the last
402 * byte of device (device size - fifo size). like device size is not
403 * knows, the prefetching is always stop.
404 */
405 if (err || qspi->fmode == CCR_FMODE_MM)
406 goto abort;
407
408 /* wait end of tx in indirect mode */
409 err = stm32_qspi_wait_cmd(qspi, op);
410 if (err)
411 goto abort;
412
413 return 0;
414
415abort:
416 cr = readl_relaxed(qspi->io_base + QSPI_CR) | CR_ABORT;
417 writel_relaxed(cr, qspi->io_base + QSPI_CR);
418
419 /* wait clear of abort bit by hw */
420 timeout = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_CR,
421 cr, !(cr & CR_ABORT), 1,
422 STM32_ABT_TIMEOUT_US);
423
424 writel_relaxed(FCR_CTCF, qspi->io_base + QSPI_FCR);
425
426 if (err || timeout)
427 dev_err(qspi->dev, "%s err:%d abort timeout:%d\n",
428 __func__, err, timeout);
429
430 return err;
431}
432
433static int stm32_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
434{
435 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master);
436 int ret;
437
438 mutex_lock(&qspi->lock);
439 ret = stm32_qspi_send(mem, op);
440 mutex_unlock(&qspi->lock);
441
442 return ret;
443}
444
445static int stm32_qspi_setup(struct spi_device *spi)
446{
447 struct spi_controller *ctrl = spi->master;
448 struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl);
449 struct stm32_qspi_flash *flash;
Ludovic Barre2e541b62019-03-08 14:12:21 +0100450 u32 presc;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200451
452 if (ctrl->busy)
453 return -EBUSY;
454
455 if (!spi->max_speed_hz)
456 return -EINVAL;
457
458 presc = DIV_ROUND_UP(qspi->clk_rate, spi->max_speed_hz) - 1;
459
460 flash = &qspi->flash[spi->chip_select];
461 flash->qspi = qspi;
462 flash->cs = spi->chip_select;
463 flash->presc = presc;
464
465 mutex_lock(&qspi->lock);
Ludovic Barre2e541b62019-03-08 14:12:21 +0100466 qspi->cr_reg = FIELD_PREP(CR_FTHRES_MASK, 3) | CR_SSHIFT | CR_EN;
467 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200468
469 /* set dcr fsize to max address */
Ludovic Barre2e541b62019-03-08 14:12:21 +0100470 qspi->dcr_reg = DCR_FSIZE_MASK;
471 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200472 mutex_unlock(&qspi->lock);
473
474 return 0;
475}
476
Ludovic Barre245308c2019-03-25 18:01:40 +0100477static void stm32_qspi_dma_setup(struct stm32_qspi *qspi)
478{
479 struct dma_slave_config dma_cfg;
480 struct device *dev = qspi->dev;
481
482 memset(&dma_cfg, 0, sizeof(dma_cfg));
483
484 dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
485 dma_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
486 dma_cfg.src_addr = qspi->phys_base + QSPI_DR;
487 dma_cfg.dst_addr = qspi->phys_base + QSPI_DR;
488 dma_cfg.src_maxburst = 4;
489 dma_cfg.dst_maxburst = 4;
490
491 qspi->dma_chrx = dma_request_slave_channel(dev, "rx");
492 if (qspi->dma_chrx) {
493 if (dmaengine_slave_config(qspi->dma_chrx, &dma_cfg)) {
494 dev_err(dev, "dma rx config failed\n");
495 dma_release_channel(qspi->dma_chrx);
496 qspi->dma_chrx = NULL;
497 }
498 }
499
500 qspi->dma_chtx = dma_request_slave_channel(dev, "tx");
501 if (qspi->dma_chtx) {
502 if (dmaengine_slave_config(qspi->dma_chtx, &dma_cfg)) {
503 dev_err(dev, "dma tx config failed\n");
504 dma_release_channel(qspi->dma_chtx);
505 qspi->dma_chtx = NULL;
506 }
507 }
508
509 init_completion(&qspi->dma_completion);
510}
511
512static void stm32_qspi_dma_free(struct stm32_qspi *qspi)
513{
514 if (qspi->dma_chtx)
515 dma_release_channel(qspi->dma_chtx);
516 if (qspi->dma_chrx)
517 dma_release_channel(qspi->dma_chrx);
518}
519
Ludovic Barrec530cd12018-10-05 09:43:03 +0200520/*
521 * no special host constraint, so use default spi_mem_default_supports_op
522 * to check supported mode.
523 */
524static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
525 .exec_op = stm32_qspi_exec_op,
526};
527
528static void stm32_qspi_release(struct stm32_qspi *qspi)
529{
530 /* disable qspi */
531 writel_relaxed(0, qspi->io_base + QSPI_CR);
Ludovic Barre245308c2019-03-25 18:01:40 +0100532 stm32_qspi_dma_free(qspi);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200533 mutex_destroy(&qspi->lock);
534 clk_disable_unprepare(qspi->clk);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100535 spi_master_put(qspi->ctrl);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200536}
537
538static int stm32_qspi_probe(struct platform_device *pdev)
539{
540 struct device *dev = &pdev->dev;
541 struct spi_controller *ctrl;
542 struct reset_control *rstc;
543 struct stm32_qspi *qspi;
544 struct resource *res;
545 int ret, irq;
546
547 ctrl = spi_alloc_master(dev, sizeof(*qspi));
548 if (!ctrl)
549 return -ENOMEM;
550
551 qspi = spi_controller_get_devdata(ctrl);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100552 qspi->ctrl = ctrl;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200553
554 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi");
555 qspi->io_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100556 if (IS_ERR(qspi->io_base)) {
557 ret = PTR_ERR(qspi->io_base);
558 goto err;
559 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200560
Ludovic Barre245308c2019-03-25 18:01:40 +0100561 qspi->phys_base = res->start;
562
Ludovic Barrec530cd12018-10-05 09:43:03 +0200563 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm");
564 qspi->mm_base = devm_ioremap_resource(dev, res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100565 if (IS_ERR(qspi->mm_base)) {
566 ret = PTR_ERR(qspi->mm_base);
567 goto err;
568 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200569
570 qspi->mm_size = resource_size(res);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100571 if (qspi->mm_size > STM32_QSPI_MAX_MMAP_SZ) {
572 ret = -EINVAL;
573 goto err;
574 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200575
576 irq = platform_get_irq(pdev, 0);
Fabien Dessenne4b562de2019-04-24 17:19:00 +0200577 if (irq < 0) {
578 if (irq != -EPROBE_DEFER)
579 dev_err(dev, "IRQ error missing or invalid\n");
580 return irq;
581 }
582
Ludovic Barrec530cd12018-10-05 09:43:03 +0200583 ret = devm_request_irq(dev, irq, stm32_qspi_irq, 0,
584 dev_name(dev), qspi);
585 if (ret) {
586 dev_err(dev, "failed to request irq\n");
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100587 goto err;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200588 }
589
590 init_completion(&qspi->data_completion);
591
592 qspi->clk = devm_clk_get(dev, NULL);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100593 if (IS_ERR(qspi->clk)) {
594 ret = PTR_ERR(qspi->clk);
595 goto err;
596 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200597
598 qspi->clk_rate = clk_get_rate(qspi->clk);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100599 if (!qspi->clk_rate) {
600 ret = -EINVAL;
601 goto err;
602 }
Ludovic Barrec530cd12018-10-05 09:43:03 +0200603
604 ret = clk_prepare_enable(qspi->clk);
605 if (ret) {
606 dev_err(dev, "can not enable the clock\n");
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100607 goto err;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200608 }
609
610 rstc = devm_reset_control_get_exclusive(dev, NULL);
611 if (!IS_ERR(rstc)) {
612 reset_control_assert(rstc);
613 udelay(2);
614 reset_control_deassert(rstc);
615 }
616
617 qspi->dev = dev;
618 platform_set_drvdata(pdev, qspi);
Ludovic Barre245308c2019-03-25 18:01:40 +0100619 stm32_qspi_dma_setup(qspi);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200620 mutex_init(&qspi->lock);
621
622 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD
623 | SPI_TX_DUAL | SPI_TX_QUAD;
624 ctrl->setup = stm32_qspi_setup;
625 ctrl->bus_num = -1;
626 ctrl->mem_ops = &stm32_qspi_mem_ops;
627 ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP;
628 ctrl->dev.of_node = dev->of_node;
629
630 ret = devm_spi_register_master(dev, ctrl);
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100631 if (!ret)
632 return 0;
Ludovic Barrec530cd12018-10-05 09:43:03 +0200633
Ludovic Barrea88eceb2019-03-25 18:01:39 +0100634err:
Ludovic Barrec530cd12018-10-05 09:43:03 +0200635 stm32_qspi_release(qspi);
Ludovic Barrec530cd12018-10-05 09:43:03 +0200636 return ret;
637}
638
639static int stm32_qspi_remove(struct platform_device *pdev)
640{
641 struct stm32_qspi *qspi = platform_get_drvdata(pdev);
642
643 stm32_qspi_release(qspi);
644 return 0;
645}
646
Ludovic Barre2e541b62019-03-08 14:12:21 +0100647static int __maybe_unused stm32_qspi_suspend(struct device *dev)
648{
649 struct stm32_qspi *qspi = dev_get_drvdata(dev);
650
651 clk_disable_unprepare(qspi->clk);
652 pinctrl_pm_select_sleep_state(dev);
653
654 return 0;
655}
656
657static int __maybe_unused stm32_qspi_resume(struct device *dev)
658{
659 struct stm32_qspi *qspi = dev_get_drvdata(dev);
660
661 pinctrl_pm_select_default_state(dev);
662 clk_prepare_enable(qspi->clk);
663
664 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR);
665 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR);
666
667 return 0;
668}
669
kbuild test robot43a8d242019-03-16 15:33:41 +0800670static SIMPLE_DEV_PM_OPS(stm32_qspi_pm_ops, stm32_qspi_suspend, stm32_qspi_resume);
Ludovic Barre2e541b62019-03-08 14:12:21 +0100671
Ludovic Barrec530cd12018-10-05 09:43:03 +0200672static const struct of_device_id stm32_qspi_match[] = {
673 {.compatible = "st,stm32f469-qspi"},
674 {}
675};
676MODULE_DEVICE_TABLE(of, stm32_qspi_match);
677
678static struct platform_driver stm32_qspi_driver = {
679 .probe = stm32_qspi_probe,
680 .remove = stm32_qspi_remove,
681 .driver = {
682 .name = "stm32-qspi",
683 .of_match_table = stm32_qspi_match,
Ludovic Barre2e541b62019-03-08 14:12:21 +0100684 .pm = &stm32_qspi_pm_ops,
Ludovic Barrec530cd12018-10-05 09:43:03 +0200685 },
686};
687module_platform_driver(stm32_qspi_driver);
688
689MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
690MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver");
691MODULE_LICENSE("GPL v2");