1 // SPDX-License-Identifier: GPL-2.0+
5 * Michael Kurz, <michi.kurz@gmail.com>
17 #include <spi_flash.h>
19 #include <asm/arch/stm32.h>
20 #include <linux/ioport.h>
22 struct stm32_qspi_regs {
39 * QUADSPI control register
41 #define STM32_QSPI_CR_EN BIT(0)
42 #define STM32_QSPI_CR_ABORT BIT(1)
43 #define STM32_QSPI_CR_DMAEN BIT(2)
44 #define STM32_QSPI_CR_TCEN BIT(3)
45 #define STM32_QSPI_CR_SSHIFT BIT(4)
46 #define STM32_QSPI_CR_DFM BIT(6)
47 #define STM32_QSPI_CR_FSEL BIT(7)
48 #define STM32_QSPI_CR_FTHRES_MASK GENMASK(4, 0)
49 #define STM32_QSPI_CR_FTHRES_SHIFT (8)
50 #define STM32_QSPI_CR_TEIE BIT(16)
51 #define STM32_QSPI_CR_TCIE BIT(17)
52 #define STM32_QSPI_CR_FTIE BIT(18)
53 #define STM32_QSPI_CR_SMIE BIT(19)
54 #define STM32_QSPI_CR_TOIE BIT(20)
55 #define STM32_QSPI_CR_APMS BIT(22)
56 #define STM32_QSPI_CR_PMM BIT(23)
57 #define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0)
58 #define STM32_QSPI_CR_PRESCALER_SHIFT (24)
61 * QUADSPI device configuration register
63 #define STM32_QSPI_DCR_CKMODE BIT(0)
64 #define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0)
65 #define STM32_QSPI_DCR_CSHT_SHIFT (8)
66 #define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0)
67 #define STM32_QSPI_DCR_FSIZE_SHIFT (16)
70 * QUADSPI status register
72 #define STM32_QSPI_SR_TEF BIT(0)
73 #define STM32_QSPI_SR_TCF BIT(1)
74 #define STM32_QSPI_SR_FTF BIT(2)
75 #define STM32_QSPI_SR_SMF BIT(3)
76 #define STM32_QSPI_SR_TOF BIT(4)
77 #define STM32_QSPI_SR_BUSY BIT(5)
78 #define STM32_QSPI_SR_FLEVEL_MASK GENMASK(5, 0)
79 #define STM32_QSPI_SR_FLEVEL_SHIFT (8)
82 * QUADSPI flag clear register
84 #define STM32_QSPI_FCR_CTEF BIT(0)
85 #define STM32_QSPI_FCR_CTCF BIT(1)
86 #define STM32_QSPI_FCR_CSMF BIT(3)
87 #define STM32_QSPI_FCR_CTOF BIT(4)
90 * QUADSPI communication configuration register
92 #define STM32_QSPI_CCR_DDRM BIT(31)
93 #define STM32_QSPI_CCR_DHHC BIT(30)
94 #define STM32_QSPI_CCR_SIOO BIT(28)
95 #define STM32_QSPI_CCR_FMODE_SHIFT (26)
96 #define STM32_QSPI_CCR_DMODE_SHIFT (24)
97 #define STM32_QSPI_CCR_DCYC_SHIFT (18)
98 #define STM32_QSPI_CCR_DCYC_MASK GENMASK(4, 0)
99 #define STM32_QSPI_CCR_ABSIZE_SHIFT (16)
100 #define STM32_QSPI_CCR_ABMODE_SHIFT (14)
101 #define STM32_QSPI_CCR_ADSIZE_SHIFT (12)
102 #define STM32_QSPI_CCR_ADMODE_SHIFT (10)
103 #define STM32_QSPI_CCR_IMODE_SHIFT (8)
104 #define STM32_QSPI_CCR_INSTRUCTION_MASK GENMASK(7, 0)
106 enum STM32_QSPI_CCR_IMODE {
107 STM32_QSPI_CCR_IMODE_NONE = 0,
108 STM32_QSPI_CCR_IMODE_ONE_LINE = 1,
109 STM32_QSPI_CCR_IMODE_TWO_LINE = 2,
110 STM32_QSPI_CCR_IMODE_FOUR_LINE = 3,
113 enum STM32_QSPI_CCR_ADMODE {
114 STM32_QSPI_CCR_ADMODE_NONE = 0,
115 STM32_QSPI_CCR_ADMODE_ONE_LINE = 1,
116 STM32_QSPI_CCR_ADMODE_TWO_LINE = 2,
117 STM32_QSPI_CCR_ADMODE_FOUR_LINE = 3,
120 enum STM32_QSPI_CCR_ADSIZE {
121 STM32_QSPI_CCR_ADSIZE_8BIT = 0,
122 STM32_QSPI_CCR_ADSIZE_16BIT = 1,
123 STM32_QSPI_CCR_ADSIZE_24BIT = 2,
124 STM32_QSPI_CCR_ADSIZE_32BIT = 3,
127 enum STM32_QSPI_CCR_ABMODE {
128 STM32_QSPI_CCR_ABMODE_NONE = 0,
129 STM32_QSPI_CCR_ABMODE_ONE_LINE = 1,
130 STM32_QSPI_CCR_ABMODE_TWO_LINE = 2,
131 STM32_QSPI_CCR_ABMODE_FOUR_LINE = 3,
134 enum STM32_QSPI_CCR_ABSIZE {
135 STM32_QSPI_CCR_ABSIZE_8BIT = 0,
136 STM32_QSPI_CCR_ABSIZE_16BIT = 1,
137 STM32_QSPI_CCR_ABSIZE_24BIT = 2,
138 STM32_QSPI_CCR_ABSIZE_32BIT = 3,
141 enum STM32_QSPI_CCR_DMODE {
142 STM32_QSPI_CCR_DMODE_NONE = 0,
143 STM32_QSPI_CCR_DMODE_ONE_LINE = 1,
144 STM32_QSPI_CCR_DMODE_TWO_LINE = 2,
145 STM32_QSPI_CCR_DMODE_FOUR_LINE = 3,
148 enum STM32_QSPI_CCR_FMODE {
149 STM32_QSPI_CCR_IND_WRITE = 0,
150 STM32_QSPI_CCR_IND_READ = 1,
151 STM32_QSPI_CCR_AUTO_POLL = 2,
152 STM32_QSPI_CCR_MEM_MAP = 3,
155 /* default SCK frequency, unit: HZ */
156 #define STM32_QSPI_DEFAULT_SCK_FREQ 108000000
158 #define STM32_MAX_NORCHIP 2
160 struct stm32_qspi_platdata {
166 struct stm32_qspi_priv {
167 struct stm32_qspi_regs *regs;
175 #define CMD_HAS_ADR BIT(24)
176 #define CMD_HAS_DUMMY BIT(25)
177 #define CMD_HAS_DATA BIT(26)
180 static void _stm32_qspi_disable(struct stm32_qspi_priv *priv)
182 clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
185 static void _stm32_qspi_enable(struct stm32_qspi_priv *priv)
187 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
190 static void _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv)
192 while (readl(&priv->regs->sr) & STM32_QSPI_SR_BUSY)
196 static void _stm32_qspi_wait_for_complete(struct stm32_qspi_priv *priv)
198 while (!(readl(&priv->regs->sr) & STM32_QSPI_SR_TCF))
202 static void _stm32_qspi_wait_for_ftf(struct stm32_qspi_priv *priv)
204 while (!(readl(&priv->regs->sr) & STM32_QSPI_SR_FTF))
208 static void _stm32_qspi_set_flash_size(struct stm32_qspi_priv *priv, u32 size)
210 u32 fsize = fls(size) - 1;
212 clrsetbits_le32(&priv->regs->dcr,
213 STM32_QSPI_DCR_FSIZE_MASK << STM32_QSPI_DCR_FSIZE_SHIFT,
214 fsize << STM32_QSPI_DCR_FSIZE_SHIFT);
217 static void _stm32_qspi_set_cs(struct stm32_qspi_priv *priv, unsigned int cs)
219 clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL,
220 cs ? STM32_QSPI_CR_FSEL : 0);
223 static unsigned int _stm32_qspi_gen_ccr(struct stm32_qspi_priv *priv)
225 unsigned int ccr_reg = 0;
226 u8 imode, admode, dmode;
227 u32 mode = priv->mode;
228 u32 cmd = (priv->command & STM32_QSPI_CCR_INSTRUCTION_MASK);
230 imode = STM32_QSPI_CCR_IMODE_ONE_LINE;
231 admode = STM32_QSPI_CCR_ADMODE_ONE_LINE;
233 if (mode & SPI_RX_QUAD) {
234 dmode = STM32_QSPI_CCR_DMODE_FOUR_LINE;
235 if (mode & SPI_TX_QUAD) {
236 imode = STM32_QSPI_CCR_IMODE_FOUR_LINE;
237 admode = STM32_QSPI_CCR_ADMODE_FOUR_LINE;
239 } else if (mode & SPI_RX_DUAL) {
240 dmode = STM32_QSPI_CCR_DMODE_TWO_LINE;
241 if (mode & SPI_TX_DUAL) {
242 imode = STM32_QSPI_CCR_IMODE_TWO_LINE;
243 admode = STM32_QSPI_CCR_ADMODE_TWO_LINE;
246 dmode = STM32_QSPI_CCR_DMODE_ONE_LINE;
249 if (priv->command & CMD_HAS_DATA)
250 ccr_reg |= (dmode << STM32_QSPI_CCR_DMODE_SHIFT);
252 if (priv->command & CMD_HAS_DUMMY)
253 ccr_reg |= ((priv->dummycycles & STM32_QSPI_CCR_DCYC_MASK)
254 << STM32_QSPI_CCR_DCYC_SHIFT);
256 if (priv->command & CMD_HAS_ADR) {
257 ccr_reg |= (STM32_QSPI_CCR_ADSIZE_24BIT
258 << STM32_QSPI_CCR_ADSIZE_SHIFT);
259 ccr_reg |= (admode << STM32_QSPI_CCR_ADMODE_SHIFT);
261 ccr_reg |= (imode << STM32_QSPI_CCR_IMODE_SHIFT);
266 static void _stm32_qspi_enable_mmap(struct stm32_qspi_priv *priv,
267 struct spi_flash *flash)
269 unsigned int ccr_reg;
271 priv->command = flash->read_cmd | CMD_HAS_ADR | CMD_HAS_DATA
273 priv->dummycycles = flash->dummy_byte * 8;
275 ccr_reg = _stm32_qspi_gen_ccr(priv);
276 ccr_reg |= (STM32_QSPI_CCR_MEM_MAP << STM32_QSPI_CCR_FMODE_SHIFT);
278 _stm32_qspi_wait_for_not_busy(priv);
280 writel(ccr_reg, &priv->regs->ccr);
282 priv->dummycycles = 0;
285 static void _stm32_qspi_disable_mmap(struct stm32_qspi_priv *priv)
287 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT);
290 static void _stm32_qspi_set_xfer_length(struct stm32_qspi_priv *priv,
293 writel(length - 1, &priv->regs->dlr);
296 static void _stm32_qspi_start_xfer(struct stm32_qspi_priv *priv, u32 cr_reg)
298 writel(cr_reg, &priv->regs->ccr);
300 if (priv->command & CMD_HAS_ADR)
301 writel(priv->address, &priv->regs->ar);
304 static int _stm32_qspi_xfer(struct stm32_qspi_priv *priv,
305 struct spi_flash *flash, unsigned int bitlen,
306 const u8 *dout, u8 *din, unsigned long flags)
308 unsigned int words = bitlen / 8;
312 if (flags & SPI_XFER_MMAP) {
313 _stm32_qspi_enable_mmap(priv, flash);
315 } else if (flags & SPI_XFER_MMAP_END) {
316 _stm32_qspi_disable_mmap(priv);
324 debug("spi_xfer: Non byte aligned SPI transfer\n");
329 debug("spi_xfer: QSPI cannot have data in and data out set\n");
333 if (!dout && (flags & SPI_XFER_BEGIN)) {
334 debug("spi_xfer: QSPI transfer must begin with command\n");
339 if (flags & SPI_XFER_BEGIN) {
340 /* data is command */
341 priv->command = dout[0] | CMD_HAS_DATA;
343 /* address is here too */
344 priv->address = (dout[1] << 16) |
345 (dout[2] << 8) | dout[3];
346 priv->command |= CMD_HAS_ADR;
350 /* rest is dummy bytes */
351 priv->dummycycles = (words - 4) * 8;
352 priv->command |= CMD_HAS_DUMMY;
355 if (flags & SPI_XFER_END) {
356 /* command without data */
357 priv->command &= ~(CMD_HAS_DATA);
361 if (flags & SPI_XFER_END) {
362 ccr_reg = _stm32_qspi_gen_ccr(priv);
363 ccr_reg |= STM32_QSPI_CCR_IND_WRITE
364 << STM32_QSPI_CCR_FMODE_SHIFT;
366 _stm32_qspi_wait_for_not_busy(priv);
368 if (priv->command & CMD_HAS_DATA)
369 _stm32_qspi_set_xfer_length(priv, words);
371 _stm32_qspi_start_xfer(priv, ccr_reg);
373 debug("%s: write: ccr:0x%08x adr:0x%08x\n",
374 __func__, priv->regs->ccr, priv->regs->ar);
376 if (priv->command & CMD_HAS_DATA) {
377 _stm32_qspi_wait_for_ftf(priv);
379 debug("%s: words:%d data:", __func__, words);
383 writeb(dout[i], &priv->regs->dr);
384 debug("%02x ", dout[i]);
389 _stm32_qspi_wait_for_complete(priv);
391 _stm32_qspi_wait_for_not_busy(priv);
395 ccr_reg = _stm32_qspi_gen_ccr(priv);
396 ccr_reg |= STM32_QSPI_CCR_IND_READ
397 << STM32_QSPI_CCR_FMODE_SHIFT;
399 _stm32_qspi_wait_for_not_busy(priv);
401 _stm32_qspi_set_xfer_length(priv, words);
403 _stm32_qspi_start_xfer(priv, ccr_reg);
405 debug("%s: read: ccr:0x%08x adr:0x%08x len:%d\n", __func__,
406 priv->regs->ccr, priv->regs->ar, priv->regs->dlr);
408 debug("%s: data:", __func__);
412 din[i] = readb(&priv->regs->dr);
413 debug("%02x ", din[i]);
422 static int stm32_qspi_ofdata_to_platdata(struct udevice *bus)
424 struct resource res_regs, res_mem;
425 struct stm32_qspi_platdata *plat = bus->platdata;
428 ret = dev_read_resource_byname(bus, "qspi", &res_regs);
430 debug("Error: can't get regs base addresses(ret = %d)!\n", ret);
433 ret = dev_read_resource_byname(bus, "qspi_mm", &res_mem);
435 debug("Error: can't get mmap base address(ret = %d)!\n", ret);
439 plat->max_hz = dev_read_u32_default(bus, "spi-max-frequency",
440 STM32_QSPI_DEFAULT_SCK_FREQ);
442 plat->base = res_regs.start;
443 plat->memory_map = res_mem.start;
445 debug("%s: regs=<0x%x> mapped=<0x%x>, max-frequency=%d\n",
455 static int stm32_qspi_probe(struct udevice *bus)
457 struct stm32_qspi_platdata *plat = dev_get_platdata(bus);
458 struct stm32_qspi_priv *priv = dev_get_priv(bus);
459 struct dm_spi_bus *dm_spi_bus;
461 struct reset_ctl reset_ctl;
464 dm_spi_bus = bus->uclass_priv;
466 dm_spi_bus->max_hz = plat->max_hz;
468 priv->regs = (struct stm32_qspi_regs *)(uintptr_t)plat->base;
470 priv->max_hz = plat->max_hz;
472 ret = clk_get_by_index(bus, 0, &clk);
476 ret = clk_enable(&clk);
479 dev_err(bus, "failed to enable clock\n");
483 priv->clock_rate = clk_get_rate(&clk);
484 if (priv->clock_rate < 0) {
486 return priv->clock_rate;
489 ret = reset_get_by_index(bus, 0, &reset_ctl);
491 if (ret != -ENOENT) {
492 dev_err(bus, "failed to get reset\n");
497 /* Reset QSPI controller */
498 reset_assert(&reset_ctl);
500 reset_deassert(&reset_ctl);
503 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT);
508 static int stm32_qspi_remove(struct udevice *bus)
513 static int stm32_qspi_claim_bus(struct udevice *dev)
515 struct stm32_qspi_priv *priv;
517 struct spi_flash *flash;
518 struct dm_spi_slave_platdata *slave_plat;
521 priv = dev_get_priv(bus);
522 flash = dev_get_uclass_priv(dev);
523 slave_plat = dev_get_parent_platdata(dev);
525 if (slave_plat->cs >= STM32_MAX_NORCHIP)
528 _stm32_qspi_set_cs(priv, slave_plat->cs);
530 _stm32_qspi_set_flash_size(priv, flash->size);
532 _stm32_qspi_enable(priv);
537 static int stm32_qspi_release_bus(struct udevice *dev)
539 struct stm32_qspi_priv *priv;
543 priv = dev_get_priv(bus);
545 _stm32_qspi_disable(priv);
550 static int stm32_qspi_xfer(struct udevice *dev, unsigned int bitlen,
551 const void *dout, void *din, unsigned long flags)
553 struct stm32_qspi_priv *priv;
555 struct spi_flash *flash;
558 priv = dev_get_priv(bus);
559 flash = dev_get_uclass_priv(dev);
561 return _stm32_qspi_xfer(priv, flash, bitlen, (const u8 *)dout,
565 static int stm32_qspi_set_speed(struct udevice *bus, uint speed)
567 struct stm32_qspi_platdata *plat = bus->platdata;
568 struct stm32_qspi_priv *priv = dev_get_priv(bus);
569 u32 qspi_clk = priv->clock_rate;
573 if (speed > plat->max_hz)
574 speed = plat->max_hz;
577 prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1;
580 else if (prescaler < 0)
584 csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000);
585 csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK;
587 _stm32_qspi_wait_for_not_busy(priv);
589 clrsetbits_le32(&priv->regs->cr,
590 STM32_QSPI_CR_PRESCALER_MASK <<
591 STM32_QSPI_CR_PRESCALER_SHIFT,
592 prescaler << STM32_QSPI_CR_PRESCALER_SHIFT);
594 clrsetbits_le32(&priv->regs->dcr,
595 STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT,
596 csht << STM32_QSPI_DCR_CSHT_SHIFT);
598 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs,
599 (qspi_clk / (prescaler + 1)));
604 static int stm32_qspi_set_mode(struct udevice *bus, uint mode)
606 struct stm32_qspi_priv *priv = dev_get_priv(bus);
608 _stm32_qspi_wait_for_not_busy(priv);
610 if ((mode & SPI_CPHA) && (mode & SPI_CPOL))
611 setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
612 else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL))
613 clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
617 if (mode & SPI_CS_HIGH)
620 if (mode & SPI_RX_QUAD)
621 priv->mode |= SPI_RX_QUAD;
622 else if (mode & SPI_RX_DUAL)
623 priv->mode |= SPI_RX_DUAL;
625 priv->mode &= ~(SPI_RX_QUAD | SPI_RX_DUAL);
627 if (mode & SPI_TX_QUAD)
628 priv->mode |= SPI_TX_QUAD;
629 else if (mode & SPI_TX_DUAL)
630 priv->mode |= SPI_TX_DUAL;
632 priv->mode &= ~(SPI_TX_QUAD | SPI_TX_DUAL);
634 debug("%s: regs=%p, mode=%d rx: ", __func__, priv->regs, mode);
636 if (mode & SPI_RX_QUAD)
638 else if (mode & SPI_RX_DUAL)
641 debug("single, tx: ");
643 if (mode & SPI_TX_QUAD)
645 else if (mode & SPI_TX_DUAL)
653 static const struct dm_spi_ops stm32_qspi_ops = {
654 .claim_bus = stm32_qspi_claim_bus,
655 .release_bus = stm32_qspi_release_bus,
656 .xfer = stm32_qspi_xfer,
657 .set_speed = stm32_qspi_set_speed,
658 .set_mode = stm32_qspi_set_mode,
661 static const struct udevice_id stm32_qspi_ids[] = {
662 { .compatible = "st,stm32-qspi" },
663 { .compatible = "st,stm32f469-qspi" },
667 U_BOOT_DRIVER(stm32_qspi) = {
668 .name = "stm32_qspi",
670 .of_match = stm32_qspi_ids,
671 .ops = &stm32_qspi_ops,
672 .ofdata_to_platdata = stm32_qspi_ofdata_to_platdata,
673 .platdata_auto_alloc_size = sizeof(struct stm32_qspi_platdata),
674 .priv_auto_alloc_size = sizeof(struct stm32_qspi_priv),
675 .probe = stm32_qspi_probe,
676 .remove = stm32_qspi_remove,