blob: 8f4aabc3d16526d8c52b69725a05030d15ad40b2 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Kurz337ff2a2017-01-22 16:04:30 +01002/*
3 * (C) Copyright 2016
4 *
5 * Michael Kurz, <michi.kurz@gmail.com>
6 *
7 * STM32 QSPI driver
Michael Kurz337ff2a2017-01-22 16:04:30 +01008 */
9
Patrick Delaunay72e220c2020-11-06 19:01:53 +010010#define LOG_CATEGORY UCLASS_SPI
11
Michael Kurz337ff2a2017-01-22 16:04:30 +010012#include <common.h>
Patrice Chotard7188fac2018-05-14 15:42:51 +020013#include <clk.h>
Simon Glassdfb7c082020-07-19 10:15:34 -060014#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Patrice Chotardb38c8972018-05-14 15:42:56 +020016#include <reset.h>
Simon Glassdfb7c082020-07-19 10:15:34 -060017#include <spi.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020018#include <spi-mem.h>
Patrice Chotard1e49e282021-01-20 14:42:02 +010019#include <watchdog.h>
Simon Glass9bc15642020-02-03 07:36:16 -070020#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060021#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020023#include <linux/iopoll.h>
Patrice Chotardb8954782018-05-14 15:42:55 +020024#include <linux/ioport.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020025#include <linux/sizes.h>
Michael Kurz337ff2a2017-01-22 16:04:30 +010026
27struct stm32_qspi_regs {
28 u32 cr; /* 0x00 */
29 u32 dcr; /* 0x04 */
30 u32 sr; /* 0x08 */
31 u32 fcr; /* 0x0C */
32 u32 dlr; /* 0x10 */
33 u32 ccr; /* 0x14 */
34 u32 ar; /* 0x18 */
35 u32 abr; /* 0x1C */
36 u32 dr; /* 0x20 */
37 u32 psmkr; /* 0x24 */
38 u32 psmar; /* 0x28 */
39 u32 pir; /* 0x2C */
40 u32 lptr; /* 0x30 */
41};
42
43/*
44 * QUADSPI control register
45 */
46#define STM32_QSPI_CR_EN BIT(0)
47#define STM32_QSPI_CR_ABORT BIT(1)
48#define STM32_QSPI_CR_DMAEN BIT(2)
49#define STM32_QSPI_CR_TCEN BIT(3)
50#define STM32_QSPI_CR_SSHIFT BIT(4)
51#define STM32_QSPI_CR_DFM BIT(6)
52#define STM32_QSPI_CR_FSEL BIT(7)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020053#define STM32_QSPI_CR_FTHRES_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010054#define STM32_QSPI_CR_TEIE BIT(16)
55#define STM32_QSPI_CR_TCIE BIT(17)
56#define STM32_QSPI_CR_FTIE BIT(18)
57#define STM32_QSPI_CR_SMIE BIT(19)
58#define STM32_QSPI_CR_TOIE BIT(20)
59#define STM32_QSPI_CR_APMS BIT(22)
60#define STM32_QSPI_CR_PMM BIT(23)
61#define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020062#define STM32_QSPI_CR_PRESCALER_SHIFT 24
Michael Kurz337ff2a2017-01-22 16:04:30 +010063
64/*
65 * QUADSPI device configuration register
66 */
67#define STM32_QSPI_DCR_CKMODE BIT(0)
68#define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020069#define STM32_QSPI_DCR_CSHT_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010070#define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020071#define STM32_QSPI_DCR_FSIZE_SHIFT 16
Michael Kurz337ff2a2017-01-22 16:04:30 +010072
73/*
74 * QUADSPI status register
75 */
76#define STM32_QSPI_SR_TEF BIT(0)
77#define STM32_QSPI_SR_TCF BIT(1)
78#define STM32_QSPI_SR_FTF BIT(2)
79#define STM32_QSPI_SR_SMF BIT(3)
80#define STM32_QSPI_SR_TOF BIT(4)
81#define STM32_QSPI_SR_BUSY BIT(5)
Michael Kurz337ff2a2017-01-22 16:04:30 +010082
83/*
84 * QUADSPI flag clear register
85 */
86#define STM32_QSPI_FCR_CTEF BIT(0)
87#define STM32_QSPI_FCR_CTCF BIT(1)
88#define STM32_QSPI_FCR_CSMF BIT(3)
89#define STM32_QSPI_FCR_CTOF BIT(4)
90
91/*
92 * QUADSPI communication configuration register
93 */
94#define STM32_QSPI_CCR_DDRM BIT(31)
95#define STM32_QSPI_CCR_DHHC BIT(30)
96#define STM32_QSPI_CCR_SIOO BIT(28)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020097#define STM32_QSPI_CCR_FMODE_SHIFT 26
98#define STM32_QSPI_CCR_DMODE_SHIFT 24
99#define STM32_QSPI_CCR_DCYC_SHIFT 18
100#define STM32_QSPI_CCR_ABSIZE_SHIFT 16
101#define STM32_QSPI_CCR_ABMODE_SHIFT 14
102#define STM32_QSPI_CCR_ADSIZE_SHIFT 12
103#define STM32_QSPI_CCR_ADMODE_SHIFT 10
104#define STM32_QSPI_CCR_IMODE_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +0100105
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200106#define STM32_QSPI_CCR_IND_WRITE 0
107#define STM32_QSPI_CCR_IND_READ 1
108#define STM32_QSPI_CCR_MEM_MAP 3
Michael Kurz337ff2a2017-01-22 16:04:30 +0100109
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200110#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
111#define STM32_QSPI_MAX_CHIP 2
Michael Kurz337ff2a2017-01-22 16:04:30 +0100112
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200113#define STM32_QSPI_FIFO_TIMEOUT_US 30000
114#define STM32_QSPI_CMD_TIMEOUT_US 1000000
115#define STM32_BUSY_TIMEOUT_US 100000
116#define STM32_ABT_TIMEOUT_US 100000
Michael Kurz337ff2a2017-01-22 16:04:30 +0100117
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200118struct stm32_qspi_flash {
119 u32 cr;
120 u32 dcr;
121 bool initialized;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100122};
123
Michael Kurz337ff2a2017-01-22 16:04:30 +0100124struct stm32_qspi_priv {
125 struct stm32_qspi_regs *regs;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200126 struct stm32_qspi_flash flash[STM32_QSPI_MAX_CHIP];
127 void __iomem *mm_base;
128 resource_size_t mm_size;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200129 ulong clock_rate;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200130 int cs_used;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100131};
132
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200133static int _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100134{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200135 u32 sr;
136 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100137
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200138 ret = readl_poll_timeout(&priv->regs->sr, sr,
139 !(sr & STM32_QSPI_SR_BUSY),
140 STM32_BUSY_TIMEOUT_US);
141 if (ret)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100142 log_err("busy timeout (stat:%#x)\n", sr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100143
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200144 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100145}
146
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200147static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,
148 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100149{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200150 u32 sr;
Daniil Stas485b4b12021-05-23 22:24:49 +0000151 int ret = 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100152
Daniil Stas485b4b12021-05-23 22:24:49 +0000153 if (op->data.nbytes) {
154 ret = readl_poll_timeout(&priv->regs->sr, sr,
155 sr & STM32_QSPI_SR_TCF,
156 STM32_QSPI_CMD_TIMEOUT_US);
157 if (ret) {
158 log_err("cmd timeout (stat:%#x)\n", sr);
159 } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) {
160 log_err("transfer error (stat:%#x)\n", sr);
161 ret = -EIO;
162 }
163 /* clear flags */
164 writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200165 }
166
Daniil Stas485b4b12021-05-23 22:24:49 +0000167 if (!ret)
168 ret = _stm32_qspi_wait_for_not_busy(priv);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200169
170 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100171}
172
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200173static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100174{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200175 *val = readb(addr);
Patrice Chotard1e49e282021-01-20 14:42:02 +0100176 WATCHDOG_RESET();
Michael Kurz337ff2a2017-01-22 16:04:30 +0100177}
178
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200179static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200180{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200181 writeb(*val, addr);
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200182}
183
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200184static int _stm32_qspi_poll(struct stm32_qspi_priv *priv,
185 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100186{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200187 void (*fifo)(u8 *val, void __iomem *addr);
188 u32 len = op->data.nbytes, sr;
189 u8 *buf;
190 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100191
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200192 if (op->data.dir == SPI_MEM_DATA_IN) {
193 fifo = _stm32_qspi_read_fifo;
194 buf = op->data.buf.in;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100195
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200196 } else {
197 fifo = _stm32_qspi_write_fifo;
198 buf = (u8 *)op->data.buf.out;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100199 }
200
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200201 while (len--) {
202 ret = readl_poll_timeout(&priv->regs->sr, sr,
203 sr & STM32_QSPI_SR_FTF,
204 STM32_QSPI_FIFO_TIMEOUT_US);
205 if (ret) {
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100206 log_err("fifo timeout (len:%d stat:%#x)\n", len, sr);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200207 return ret;
208 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100209
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200210 fifo(buf++, &priv->regs->dr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100211 }
Christophe Kerelloa1a725a2018-07-09 15:32:37 +0200212
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200213 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100214}
215
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200216static int stm32_qspi_mm(struct stm32_qspi_priv *priv,
217 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100218{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200219 memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val,
220 op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100221
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200222 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100223}
224
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200225static int _stm32_qspi_tx(struct stm32_qspi_priv *priv,
226 const struct spi_mem_op *op,
227 u8 mode)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100228{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200229 if (!op->data.nbytes)
230 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100231
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200232 if (mode == STM32_QSPI_CCR_MEM_MAP)
233 return stm32_qspi_mm(priv, op);
234
235 return _stm32_qspi_poll(priv, op);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100236}
237
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200238static int _stm32_qspi_get_mode(u8 buswidth)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100239{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200240 if (buswidth == 4)
241 return 3;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100242
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200243 return buswidth;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100244}
245
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200246static int stm32_qspi_exec_op(struct spi_slave *slave,
247 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100248{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200249 struct stm32_qspi_priv *priv = dev_get_priv(slave->dev->parent);
250 u32 cr, ccr, addr_max;
251 u8 mode = STM32_QSPI_CCR_IND_WRITE;
252 int timeout, ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100253
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100254 dev_dbg(slave->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
255 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
256 op->dummy.buswidth, op->data.buswidth,
257 op->addr.val, op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100258
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200259 ret = _stm32_qspi_wait_for_not_busy(priv);
260 if (ret)
261 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100262
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200263 addr_max = op->addr.val + op->data.nbytes + 1;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100264
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200265 if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) {
266 if (addr_max < priv->mm_size && op->addr.buswidth)
267 mode = STM32_QSPI_CCR_MEM_MAP;
268 else
269 mode = STM32_QSPI_CCR_IND_READ;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100270 }
271
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200272 if (op->data.nbytes)
273 writel(op->data.nbytes - 1, &priv->regs->dlr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100274
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200275 ccr = (mode << STM32_QSPI_CCR_FMODE_SHIFT);
276 ccr |= op->cmd.opcode;
277 ccr |= (_stm32_qspi_get_mode(op->cmd.buswidth)
278 << STM32_QSPI_CCR_IMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100279
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200280 if (op->addr.nbytes) {
281 ccr |= ((op->addr.nbytes - 1) << STM32_QSPI_CCR_ADSIZE_SHIFT);
282 ccr |= (_stm32_qspi_get_mode(op->addr.buswidth)
283 << STM32_QSPI_CCR_ADMODE_SHIFT);
284 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100285
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200286 if (op->dummy.buswidth && op->dummy.nbytes)
287 ccr |= (op->dummy.nbytes * 8 / op->dummy.buswidth
288 << STM32_QSPI_CCR_DCYC_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100289
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200290 if (op->data.nbytes)
291 ccr |= (_stm32_qspi_get_mode(op->data.buswidth)
292 << STM32_QSPI_CCR_DMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100293
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200294 writel(ccr, &priv->regs->ccr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100295
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200296 if (op->addr.nbytes && mode != STM32_QSPI_CCR_MEM_MAP)
297 writel(op->addr.val, &priv->regs->ar);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100298
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200299 ret = _stm32_qspi_tx(priv, op, mode);
300 /*
301 * Abort in:
302 * -error case
303 * -read memory map: prefetching must be stopped if we read the last
304 * byte of device (device size - fifo size). like device size is not
305 * knows, the prefetching is always stop.
306 */
307 if (ret || mode == STM32_QSPI_CCR_MEM_MAP)
308 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100309
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200310 /* Wait end of tx in indirect mode */
311 ret = _stm32_qspi_wait_cmd(priv, op);
312 if (ret)
313 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100314
315 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100316
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200317abort:
318 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100319
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200320 /* Wait clear of abort bit by hw */
321 timeout = readl_poll_timeout(&priv->regs->cr, cr,
322 !(cr & STM32_QSPI_CR_ABORT),
323 STM32_ABT_TIMEOUT_US);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100324
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200325 writel(STM32_QSPI_FCR_CTCF, &priv->regs->fcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100326
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200327 if (ret || timeout)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100328 dev_err(slave->dev, "ret:%d abort timeout:%d\n", ret, timeout);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100329
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200330 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100331}
332
333static int stm32_qspi_probe(struct udevice *bus)
334{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100335 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200336 struct resource res;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200337 struct clk clk;
Patrice Chotardb38c8972018-05-14 15:42:56 +0200338 struct reset_ctl reset_ctl;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200339 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100340
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200341 ret = dev_read_resource_byname(bus, "qspi", &res);
342 if (ret) {
343 dev_err(bus, "can't get regs base addresses(ret = %d)!\n", ret);
344 return ret;
345 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100346
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200347 priv->regs = (struct stm32_qspi_regs *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100348
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200349 ret = dev_read_resource_byname(bus, "qspi_mm", &res);
350 if (ret) {
351 dev_err(bus, "can't get mmap base address(ret = %d)!\n", ret);
352 return ret;
353 }
354
355 priv->mm_base = (void __iomem *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100356
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200357 priv->mm_size = resource_size(&res);
358 if (priv->mm_size > STM32_QSPI_MAX_MMAP_SZ)
359 return -EINVAL;
360
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100361 dev_dbg(bus, "regs=<0x%p> mapped=<0x%p> mapped_size=<0x%lx>\n",
362 priv->regs, priv->mm_base, priv->mm_size);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100363
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700364 ret = clk_get_by_index(bus, 0, &clk);
365 if (ret < 0)
366 return ret;
367
368 ret = clk_enable(&clk);
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700369 if (ret) {
370 dev_err(bus, "failed to enable clock\n");
371 return ret;
372 }
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200373
374 priv->clock_rate = clk_get_rate(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200375 if (!priv->clock_rate) {
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200376 clk_disable(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200377 return -EINVAL;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200378 }
379
Patrice Chotardb38c8972018-05-14 15:42:56 +0200380 ret = reset_get_by_index(bus, 0, &reset_ctl);
381 if (ret) {
382 if (ret != -ENOENT) {
383 dev_err(bus, "failed to get reset\n");
384 clk_disable(&clk);
385 return ret;
386 }
387 } else {
388 /* Reset QSPI controller */
389 reset_assert(&reset_ctl);
390 udelay(2);
391 reset_deassert(&reset_ctl);
392 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100393
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200394 priv->cs_used = -1;
395
Michael Kurz337ff2a2017-01-22 16:04:30 +0100396 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT);
397
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200398 /* Set dcr fsize to max address */
399 setbits_le32(&priv->regs->dcr,
400 STM32_QSPI_DCR_FSIZE_MASK << STM32_QSPI_DCR_FSIZE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100401
Michael Kurz337ff2a2017-01-22 16:04:30 +0100402 return 0;
403}
404
405static int stm32_qspi_claim_bus(struct udevice *dev)
406{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200407 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700408 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Patrick Delaunay025020722019-06-21 15:26:55 +0200409 int slave_cs = slave_plat->cs;
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200410
Patrick Delaunay025020722019-06-21 15:26:55 +0200411 if (slave_cs >= STM32_QSPI_MAX_CHIP)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200412 return -ENODEV;
413
Patrick Delaunay025020722019-06-21 15:26:55 +0200414 if (priv->cs_used != slave_cs) {
415 struct stm32_qspi_flash *flash = &priv->flash[slave_cs];
Michael Kurz337ff2a2017-01-22 16:04:30 +0100416
Patrick Delaunay025020722019-06-21 15:26:55 +0200417 priv->cs_used = slave_cs;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100418
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200419 if (flash->initialized) {
420 /* Set the configuration: speed + cs */
421 writel(flash->cr, &priv->regs->cr);
422 writel(flash->dcr, &priv->regs->dcr);
423 } else {
424 /* Set chip select */
425 clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL,
426 priv->cs_used ? STM32_QSPI_CR_FSEL : 0);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100427
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200428 /* Save the configuration: speed + cs */
429 flash->cr = readl(&priv->regs->cr);
430 flash->dcr = readl(&priv->regs->dcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100431
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200432 flash->initialized = true;
433 }
434 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100435
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200436 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100437
438 return 0;
439}
440
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200441static int stm32_qspi_release_bus(struct udevice *dev)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100442{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200443 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100444
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200445 clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100446
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200447 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100448}
449
450static int stm32_qspi_set_speed(struct udevice *bus, uint speed)
451{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100452 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Patrick Delaunay0a861152018-05-14 15:42:50 +0200453 u32 qspi_clk = priv->clock_rate;
454 u32 prescaler = 255;
455 u32 csht;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200456 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100457
Michael Kurz337ff2a2017-01-22 16:04:30 +0100458 if (speed > 0) {
Patrick Delaunay025020722019-06-21 15:26:55 +0200459 prescaler = 0;
460 if (qspi_clk) {
461 prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1;
462 if (prescaler > 255)
463 prescaler = 255;
464 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100465 }
466
Patrick Delaunay0a861152018-05-14 15:42:50 +0200467 csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100468 csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK;
469
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200470 ret = _stm32_qspi_wait_for_not_busy(priv);
471 if (ret)
472 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100473
474 clrsetbits_le32(&priv->regs->cr,
475 STM32_QSPI_CR_PRESCALER_MASK <<
476 STM32_QSPI_CR_PRESCALER_SHIFT,
477 prescaler << STM32_QSPI_CR_PRESCALER_SHIFT);
478
Michael Kurz337ff2a2017-01-22 16:04:30 +0100479 clrsetbits_le32(&priv->regs->dcr,
480 STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT,
481 csht << STM32_QSPI_DCR_CSHT_SHIFT);
482
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100483 dev_dbg(bus, "regs=%p, speed=%d\n", priv->regs,
484 (qspi_clk / (prescaler + 1)));
Michael Kurz337ff2a2017-01-22 16:04:30 +0100485
486 return 0;
487}
488
489static int stm32_qspi_set_mode(struct udevice *bus, uint mode)
490{
491 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200492 int ret;
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100493 const char *str_rx, *str_tx;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100494
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200495 ret = _stm32_qspi_wait_for_not_busy(priv);
496 if (ret)
497 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100498
499 if ((mode & SPI_CPHA) && (mode & SPI_CPOL))
500 setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
501 else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL))
502 clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
503 else
504 return -ENODEV;
505
506 if (mode & SPI_CS_HIGH)
507 return -ENODEV;
508
Michael Kurz337ff2a2017-01-22 16:04:30 +0100509 if (mode & SPI_RX_QUAD)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100510 str_rx = "quad";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100511 else if (mode & SPI_RX_DUAL)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100512 str_rx = "dual";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100513 else
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100514 str_rx = "single";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100515
516 if (mode & SPI_TX_QUAD)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100517 str_tx = "quad";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100518 else if (mode & SPI_TX_DUAL)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100519 str_tx = "dual";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100520 else
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100521 str_tx = "single";
522
523 dev_dbg(bus, "regs=%p, mode=%d rx: %s, tx: %s\n",
524 priv->regs, mode, str_rx, str_tx);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100525
526 return 0;
527}
528
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200529static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
530 .exec_op = stm32_qspi_exec_op,
531};
532
Michael Kurz337ff2a2017-01-22 16:04:30 +0100533static const struct dm_spi_ops stm32_qspi_ops = {
534 .claim_bus = stm32_qspi_claim_bus,
535 .release_bus = stm32_qspi_release_bus,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100536 .set_speed = stm32_qspi_set_speed,
537 .set_mode = stm32_qspi_set_mode,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200538 .mem_ops = &stm32_qspi_mem_ops,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100539};
540
541static const struct udevice_id stm32_qspi_ids[] = {
Christophe Kerello4c00d552018-05-14 15:42:53 +0200542 { .compatible = "st,stm32f469-qspi" },
Michael Kurz337ff2a2017-01-22 16:04:30 +0100543 { }
544};
545
546U_BOOT_DRIVER(stm32_qspi) = {
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200547 .name = "stm32_qspi",
548 .id = UCLASS_SPI,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100549 .of_match = stm32_qspi_ids,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200550 .ops = &stm32_qspi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700551 .priv_auto = sizeof(struct stm32_qspi_priv),
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200552 .probe = stm32_qspi_probe,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100553};