1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
4 * Author: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics.
6 #include <linux/bitfield.h>
8 #include <linux/errno.h>
10 #include <linux/iopoll.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/of_device.h>
16 #include <linux/platform_device.h>
17 #include <linux/reset.h>
18 #include <linux/sizes.h>
19 #include <linux/spi/spi-mem.h>
23 #define CR_ABORT BIT(1)
24 #define CR_DMAEN BIT(2)
25 #define CR_TCEN BIT(3)
26 #define CR_SSHIFT BIT(4)
28 #define CR_FSEL BIT(7)
29 #define CR_FTHRES_MASK GENMASK(12, 8)
30 #define CR_TEIE BIT(16)
31 #define CR_TCIE BIT(17)
32 #define CR_FTIE BIT(18)
33 #define CR_SMIE BIT(19)
34 #define CR_TOIE BIT(20)
35 #define CR_PRESC_MASK GENMASK(31, 24)
38 #define DCR_FSIZE_MASK GENMASK(20, 16)
46 #define SR_BUSY BIT(5)
47 #define SR_FLEVEL_MASK GENMASK(13, 8)
50 #define FCR_CTEF BIT(0)
51 #define FCR_CTCF BIT(1)
56 #define CCR_INST_MASK GENMASK(7, 0)
57 #define CCR_IMODE_MASK GENMASK(9, 8)
58 #define CCR_ADMODE_MASK GENMASK(11, 10)
59 #define CCR_ADSIZE_MASK GENMASK(13, 12)
60 #define CCR_DCYC_MASK GENMASK(22, 18)
61 #define CCR_DMODE_MASK GENMASK(25, 24)
62 #define CCR_FMODE_MASK GENMASK(27, 26)
63 #define CCR_FMODE_INDW (0U << 26)
64 #define CCR_FMODE_INDR (1U << 26)
65 #define CCR_FMODE_APM (2U << 26)
66 #define CCR_FMODE_MM (3U << 26)
67 #define CCR_BUSWIDTH_0 0x0
68 #define CCR_BUSWIDTH_1 0x1
69 #define CCR_BUSWIDTH_2 0x2
70 #define CCR_BUSWIDTH_4 0x3
75 #define QSPI_PSMKR 0x24
76 #define QSPI_PSMAR 0x28
78 #define QSPI_LPTR 0x30
79 #define LPTR_DFT_TIMEOUT 0x10
81 #define STM32_QSPI_MAX_MMAP_SZ SZ_256M
82 #define STM32_QSPI_MAX_NORCHIP 2
84 #define STM32_FIFO_TIMEOUT_US 30000
85 #define STM32_BUSY_TIMEOUT_US 100000
86 #define STM32_ABT_TIMEOUT_US 100000
88 struct stm32_qspi_flash
{
89 struct stm32_qspi
*qspi
;
96 void __iomem
*io_base
;
97 void __iomem
*mm_base
;
98 resource_size_t mm_size
;
101 struct stm32_qspi_flash flash
[STM32_QSPI_MAX_NORCHIP
];
102 struct completion data_completion
;
106 * to protect device configuration, could be different between
107 * 2 flash access (bk1, bk2)
112 static irqreturn_t
stm32_qspi_irq(int irq
, void *dev_id
)
114 struct stm32_qspi
*qspi
= (struct stm32_qspi
*)dev_id
;
117 sr
= readl_relaxed(qspi
->io_base
+ QSPI_SR
);
119 if (sr
& (SR_TEF
| SR_TCF
)) {
121 cr
= readl_relaxed(qspi
->io_base
+ QSPI_CR
);
122 cr
&= ~CR_TCIE
& ~CR_TEIE
;
123 writel_relaxed(cr
, qspi
->io_base
+ QSPI_CR
);
124 complete(&qspi
->data_completion
);
130 static void stm32_qspi_read_fifo(u8
*val
, void __iomem
*addr
)
132 *val
= readb_relaxed(addr
);
135 static void stm32_qspi_write_fifo(u8
*val
, void __iomem
*addr
)
137 writeb_relaxed(*val
, addr
);
140 static int stm32_qspi_tx_poll(struct stm32_qspi
*qspi
,
141 const struct spi_mem_op
*op
)
143 void (*tx_fifo
)(u8
*val
, void __iomem
*addr
);
144 u32 len
= op
->data
.nbytes
, sr
;
148 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
149 tx_fifo
= stm32_qspi_read_fifo
;
150 buf
= op
->data
.buf
.in
;
153 tx_fifo
= stm32_qspi_write_fifo
;
154 buf
= (u8
*)op
->data
.buf
.out
;
158 ret
= readl_relaxed_poll_timeout_atomic(qspi
->io_base
+ QSPI_SR
,
159 sr
, (sr
& SR_FTF
), 1,
160 STM32_FIFO_TIMEOUT_US
);
162 dev_err(qspi
->dev
, "fifo timeout (len:%d stat:%#x)\n",
166 tx_fifo(buf
++, qspi
->io_base
+ QSPI_DR
);
172 static int stm32_qspi_tx_mm(struct stm32_qspi
*qspi
,
173 const struct spi_mem_op
*op
)
175 memcpy_fromio(op
->data
.buf
.in
, qspi
->mm_base
+ op
->addr
.val
,
180 static int stm32_qspi_tx(struct stm32_qspi
*qspi
, const struct spi_mem_op
*op
)
182 if (!op
->data
.nbytes
)
185 if (qspi
->fmode
== CCR_FMODE_MM
)
186 return stm32_qspi_tx_mm(qspi
, op
);
188 return stm32_qspi_tx_poll(qspi
, op
);
191 static int stm32_qspi_wait_nobusy(struct stm32_qspi
*qspi
)
195 return readl_relaxed_poll_timeout_atomic(qspi
->io_base
+ QSPI_SR
, sr
,
197 STM32_BUSY_TIMEOUT_US
);
200 static int stm32_qspi_wait_cmd(struct stm32_qspi
*qspi
,
201 const struct spi_mem_op
*op
)
206 if (!op
->data
.nbytes
)
207 return stm32_qspi_wait_nobusy(qspi
);
209 if (readl_relaxed(qspi
->io_base
+ QSPI_SR
) & SR_TCF
)
212 reinit_completion(&qspi
->data_completion
);
213 cr
= readl_relaxed(qspi
->io_base
+ QSPI_CR
);
214 writel_relaxed(cr
| CR_TCIE
| CR_TEIE
, qspi
->io_base
+ QSPI_CR
);
216 if (!wait_for_completion_interruptible_timeout(&qspi
->data_completion
,
217 msecs_to_jiffies(1000))) {
220 sr
= readl_relaxed(qspi
->io_base
+ QSPI_SR
);
227 writel_relaxed(FCR_CTCF
| FCR_CTEF
, qspi
->io_base
+ QSPI_FCR
);
232 static int stm32_qspi_get_mode(struct stm32_qspi
*qspi
, u8 buswidth
)
235 return CCR_BUSWIDTH_4
;
240 static int stm32_qspi_send(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
242 struct stm32_qspi
*qspi
= spi_controller_get_devdata(mem
->spi
->master
);
243 struct stm32_qspi_flash
*flash
= &qspi
->flash
[mem
->spi
->chip_select
];
244 u32 ccr
, cr
, addr_max
;
245 int timeout
, err
= 0;
247 dev_dbg(qspi
->dev
, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
248 op
->cmd
.opcode
, op
->cmd
.buswidth
, op
->addr
.buswidth
,
249 op
->dummy
.buswidth
, op
->data
.buswidth
,
250 op
->addr
.val
, op
->data
.nbytes
);
252 err
= stm32_qspi_wait_nobusy(qspi
);
256 addr_max
= op
->addr
.val
+ op
->data
.nbytes
+ 1;
258 if (op
->data
.dir
== SPI_MEM_DATA_IN
) {
259 if (addr_max
< qspi
->mm_size
&&
261 qspi
->fmode
= CCR_FMODE_MM
;
263 qspi
->fmode
= CCR_FMODE_INDR
;
265 qspi
->fmode
= CCR_FMODE_INDW
;
268 cr
= readl_relaxed(qspi
->io_base
+ QSPI_CR
);
269 cr
&= ~CR_PRESC_MASK
& ~CR_FSEL
;
270 cr
|= FIELD_PREP(CR_PRESC_MASK
, flash
->presc
);
271 cr
|= FIELD_PREP(CR_FSEL
, flash
->cs
);
272 writel_relaxed(cr
, qspi
->io_base
+ QSPI_CR
);
275 writel_relaxed(op
->data
.nbytes
- 1,
276 qspi
->io_base
+ QSPI_DLR
);
278 qspi
->fmode
= CCR_FMODE_INDW
;
281 ccr
|= FIELD_PREP(CCR_INST_MASK
, op
->cmd
.opcode
);
282 ccr
|= FIELD_PREP(CCR_IMODE_MASK
,
283 stm32_qspi_get_mode(qspi
, op
->cmd
.buswidth
));
285 if (op
->addr
.nbytes
) {
286 ccr
|= FIELD_PREP(CCR_ADMODE_MASK
,
287 stm32_qspi_get_mode(qspi
, op
->addr
.buswidth
));
288 ccr
|= FIELD_PREP(CCR_ADSIZE_MASK
, op
->addr
.nbytes
- 1);
291 if (op
->dummy
.buswidth
&& op
->dummy
.nbytes
)
292 ccr
|= FIELD_PREP(CCR_DCYC_MASK
,
293 op
->dummy
.nbytes
* 8 / op
->dummy
.buswidth
);
295 if (op
->data
.nbytes
) {
296 ccr
|= FIELD_PREP(CCR_DMODE_MASK
,
297 stm32_qspi_get_mode(qspi
, op
->data
.buswidth
));
300 writel_relaxed(ccr
, qspi
->io_base
+ QSPI_CCR
);
302 if (op
->addr
.nbytes
&& qspi
->fmode
!= CCR_FMODE_MM
)
303 writel_relaxed(op
->addr
.val
, qspi
->io_base
+ QSPI_AR
);
305 err
= stm32_qspi_tx(qspi
, op
);
310 * -read memory map: prefetching must be stopped if we read the last
311 * byte of device (device size - fifo size). like device size is not
312 * knows, the prefetching is always stop.
314 if (err
|| qspi
->fmode
== CCR_FMODE_MM
)
317 /* wait end of tx in indirect mode */
318 err
= stm32_qspi_wait_cmd(qspi
, op
);
325 cr
= readl_relaxed(qspi
->io_base
+ QSPI_CR
) | CR_ABORT
;
326 writel_relaxed(cr
, qspi
->io_base
+ QSPI_CR
);
328 /* wait clear of abort bit by hw */
329 timeout
= readl_relaxed_poll_timeout_atomic(qspi
->io_base
+ QSPI_CR
,
330 cr
, !(cr
& CR_ABORT
), 1,
331 STM32_ABT_TIMEOUT_US
);
333 writel_relaxed(FCR_CTCF
, qspi
->io_base
+ QSPI_FCR
);
336 dev_err(qspi
->dev
, "%s err:%d abort timeout:%d\n",
337 __func__
, err
, timeout
);
342 static int stm32_qspi_exec_op(struct spi_mem
*mem
, const struct spi_mem_op
*op
)
344 struct stm32_qspi
*qspi
= spi_controller_get_devdata(mem
->spi
->master
);
347 mutex_lock(&qspi
->lock
);
348 ret
= stm32_qspi_send(mem
, op
);
349 mutex_unlock(&qspi
->lock
);
354 static int stm32_qspi_setup(struct spi_device
*spi
)
356 struct spi_controller
*ctrl
= spi
->master
;
357 struct stm32_qspi
*qspi
= spi_controller_get_devdata(ctrl
);
358 struct stm32_qspi_flash
*flash
;
364 if (!spi
->max_speed_hz
)
367 presc
= DIV_ROUND_UP(qspi
->clk_rate
, spi
->max_speed_hz
) - 1;
369 flash
= &qspi
->flash
[spi
->chip_select
];
371 flash
->cs
= spi
->chip_select
;
372 flash
->presc
= presc
;
374 mutex_lock(&qspi
->lock
);
375 writel_relaxed(LPTR_DFT_TIMEOUT
, qspi
->io_base
+ QSPI_LPTR
);
376 cr
= FIELD_PREP(CR_FTHRES_MASK
, 3) | CR_TCEN
| CR_SSHIFT
| CR_EN
;
377 writel_relaxed(cr
, qspi
->io_base
+ QSPI_CR
);
379 /* set dcr fsize to max address */
380 writel_relaxed(DCR_FSIZE_MASK
, qspi
->io_base
+ QSPI_DCR
);
381 mutex_unlock(&qspi
->lock
);
387 * no special host constraint, so use default spi_mem_default_supports_op
388 * to check supported mode.
390 static const struct spi_controller_mem_ops stm32_qspi_mem_ops
= {
391 .exec_op
= stm32_qspi_exec_op
,
394 static void stm32_qspi_release(struct stm32_qspi
*qspi
)
397 writel_relaxed(0, qspi
->io_base
+ QSPI_CR
);
398 mutex_destroy(&qspi
->lock
);
399 clk_disable_unprepare(qspi
->clk
);
402 static int stm32_qspi_probe(struct platform_device
*pdev
)
404 struct device
*dev
= &pdev
->dev
;
405 struct spi_controller
*ctrl
;
406 struct reset_control
*rstc
;
407 struct stm32_qspi
*qspi
;
408 struct resource
*res
;
411 ctrl
= spi_alloc_master(dev
, sizeof(*qspi
));
415 qspi
= spi_controller_get_devdata(ctrl
);
417 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "qspi");
418 qspi
->io_base
= devm_ioremap_resource(dev
, res
);
419 if (IS_ERR(qspi
->io_base
))
420 return PTR_ERR(qspi
->io_base
);
422 res
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, "qspi_mm");
423 qspi
->mm_base
= devm_ioremap_resource(dev
, res
);
424 if (IS_ERR(qspi
->mm_base
))
425 return PTR_ERR(qspi
->mm_base
);
427 qspi
->mm_size
= resource_size(res
);
428 if (qspi
->mm_size
> STM32_QSPI_MAX_MMAP_SZ
)
431 irq
= platform_get_irq(pdev
, 0);
432 ret
= devm_request_irq(dev
, irq
, stm32_qspi_irq
, 0,
433 dev_name(dev
), qspi
);
435 dev_err(dev
, "failed to request irq\n");
439 init_completion(&qspi
->data_completion
);
441 qspi
->clk
= devm_clk_get(dev
, NULL
);
442 if (IS_ERR(qspi
->clk
))
443 return PTR_ERR(qspi
->clk
);
445 qspi
->clk_rate
= clk_get_rate(qspi
->clk
);
449 ret
= clk_prepare_enable(qspi
->clk
);
451 dev_err(dev
, "can not enable the clock\n");
455 rstc
= devm_reset_control_get_exclusive(dev
, NULL
);
457 reset_control_assert(rstc
);
459 reset_control_deassert(rstc
);
463 platform_set_drvdata(pdev
, qspi
);
464 mutex_init(&qspi
->lock
);
466 ctrl
->mode_bits
= SPI_RX_DUAL
| SPI_RX_QUAD
467 | SPI_TX_DUAL
| SPI_TX_QUAD
;
468 ctrl
->setup
= stm32_qspi_setup
;
470 ctrl
->mem_ops
= &stm32_qspi_mem_ops
;
471 ctrl
->num_chipselect
= STM32_QSPI_MAX_NORCHIP
;
472 ctrl
->dev
.of_node
= dev
->of_node
;
474 ret
= devm_spi_register_master(dev
, ctrl
);
476 goto err_spi_register
;
481 stm32_qspi_release(qspi
);
486 static int stm32_qspi_remove(struct platform_device
*pdev
)
488 struct stm32_qspi
*qspi
= platform_get_drvdata(pdev
);
490 stm32_qspi_release(qspi
);
494 static const struct of_device_id stm32_qspi_match
[] = {
495 {.compatible
= "st,stm32f469-qspi"},
498 MODULE_DEVICE_TABLE(of
, stm32_qspi_match
);
500 static struct platform_driver stm32_qspi_driver
= {
501 .probe
= stm32_qspi_probe
,
502 .remove
= stm32_qspi_remove
,
504 .name
= "stm32-qspi",
505 .of_match_table
= stm32_qspi_match
,
508 module_platform_driver(stm32_qspi_driver
);
510 MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>");
511 MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver");
512 MODULE_LICENSE("GPL v2");