blob: 001f0703e3cba2be692c47c8fb1deff5c47d7346 [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
10#include <common.h>
Patrice Chotard7188fac2018-05-14 15:42:51 +020011#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Patrice Chotardb38c8972018-05-14 15:42:56 +020013#include <reset.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020014#include <spi-mem.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020018#include <linux/iopoll.h>
Patrice Chotardb8954782018-05-14 15:42:55 +020019#include <linux/ioport.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020020#include <linux/sizes.h>
Michael Kurz337ff2a2017-01-22 16:04:30 +010021
22struct stm32_qspi_regs {
23 u32 cr; /* 0x00 */
24 u32 dcr; /* 0x04 */
25 u32 sr; /* 0x08 */
26 u32 fcr; /* 0x0C */
27 u32 dlr; /* 0x10 */
28 u32 ccr; /* 0x14 */
29 u32 ar; /* 0x18 */
30 u32 abr; /* 0x1C */
31 u32 dr; /* 0x20 */
32 u32 psmkr; /* 0x24 */
33 u32 psmar; /* 0x28 */
34 u32 pir; /* 0x2C */
35 u32 lptr; /* 0x30 */
36};
37
38/*
39 * QUADSPI control register
40 */
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)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020048#define STM32_QSPI_CR_FTHRES_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010049#define STM32_QSPI_CR_TEIE BIT(16)
50#define STM32_QSPI_CR_TCIE BIT(17)
51#define STM32_QSPI_CR_FTIE BIT(18)
52#define STM32_QSPI_CR_SMIE BIT(19)
53#define STM32_QSPI_CR_TOIE BIT(20)
54#define STM32_QSPI_CR_APMS BIT(22)
55#define STM32_QSPI_CR_PMM BIT(23)
56#define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020057#define STM32_QSPI_CR_PRESCALER_SHIFT 24
Michael Kurz337ff2a2017-01-22 16:04:30 +010058
59/*
60 * QUADSPI device configuration register
61 */
62#define STM32_QSPI_DCR_CKMODE BIT(0)
63#define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020064#define STM32_QSPI_DCR_CSHT_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010065#define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020066#define STM32_QSPI_DCR_FSIZE_SHIFT 16
Michael Kurz337ff2a2017-01-22 16:04:30 +010067
68/*
69 * QUADSPI status register
70 */
71#define STM32_QSPI_SR_TEF BIT(0)
72#define STM32_QSPI_SR_TCF BIT(1)
73#define STM32_QSPI_SR_FTF BIT(2)
74#define STM32_QSPI_SR_SMF BIT(3)
75#define STM32_QSPI_SR_TOF BIT(4)
76#define STM32_QSPI_SR_BUSY BIT(5)
Michael Kurz337ff2a2017-01-22 16:04:30 +010077
78/*
79 * QUADSPI flag clear register
80 */
81#define STM32_QSPI_FCR_CTEF BIT(0)
82#define STM32_QSPI_FCR_CTCF BIT(1)
83#define STM32_QSPI_FCR_CSMF BIT(3)
84#define STM32_QSPI_FCR_CTOF BIT(4)
85
86/*
87 * QUADSPI communication configuration register
88 */
89#define STM32_QSPI_CCR_DDRM BIT(31)
90#define STM32_QSPI_CCR_DHHC BIT(30)
91#define STM32_QSPI_CCR_SIOO BIT(28)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020092#define STM32_QSPI_CCR_FMODE_SHIFT 26
93#define STM32_QSPI_CCR_DMODE_SHIFT 24
94#define STM32_QSPI_CCR_DCYC_SHIFT 18
95#define STM32_QSPI_CCR_ABSIZE_SHIFT 16
96#define STM32_QSPI_CCR_ABMODE_SHIFT 14
97#define STM32_QSPI_CCR_ADSIZE_SHIFT 12
98#define STM32_QSPI_CCR_ADMODE_SHIFT 10
99#define STM32_QSPI_CCR_IMODE_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +0100100
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200101#define STM32_QSPI_CCR_IND_WRITE 0
102#define STM32_QSPI_CCR_IND_READ 1
103#define STM32_QSPI_CCR_MEM_MAP 3
Michael Kurz337ff2a2017-01-22 16:04:30 +0100104
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200105#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
106#define STM32_QSPI_MAX_CHIP 2
Michael Kurz337ff2a2017-01-22 16:04:30 +0100107
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200108#define STM32_QSPI_FIFO_TIMEOUT_US 30000
109#define STM32_QSPI_CMD_TIMEOUT_US 1000000
110#define STM32_BUSY_TIMEOUT_US 100000
111#define STM32_ABT_TIMEOUT_US 100000
Michael Kurz337ff2a2017-01-22 16:04:30 +0100112
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200113struct stm32_qspi_flash {
114 u32 cr;
115 u32 dcr;
116 bool initialized;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100117};
118
Michael Kurz337ff2a2017-01-22 16:04:30 +0100119struct stm32_qspi_priv {
120 struct stm32_qspi_regs *regs;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200121 struct stm32_qspi_flash flash[STM32_QSPI_MAX_CHIP];
122 void __iomem *mm_base;
123 resource_size_t mm_size;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200124 ulong clock_rate;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200125 int cs_used;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100126};
127
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200128static int _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100129{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200130 u32 sr;
131 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100132
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200133 ret = readl_poll_timeout(&priv->regs->sr, sr,
134 !(sr & STM32_QSPI_SR_BUSY),
135 STM32_BUSY_TIMEOUT_US);
136 if (ret)
137 pr_err("busy timeout (stat:%#x)\n", sr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100138
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200139 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100140}
141
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200142static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,
143 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100144{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200145 u32 sr;
146 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100147
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200148 if (!op->data.nbytes)
149 return _stm32_qspi_wait_for_not_busy(priv);
150
151 ret = readl_poll_timeout(&priv->regs->sr, sr,
152 sr & STM32_QSPI_SR_TCF,
153 STM32_QSPI_CMD_TIMEOUT_US);
154 if (ret) {
155 pr_err("cmd timeout (stat:%#x)\n", sr);
156 } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) {
157 pr_err("transfer error (stat:%#x)\n", sr);
158 ret = -EIO;
159 }
160
161 /* clear flags */
162 writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr);
163
164 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100165}
166
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200167static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100168{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200169 *val = readb(addr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100170}
171
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200172static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200173{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200174 writeb(*val, addr);
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200175}
176
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200177static int _stm32_qspi_poll(struct stm32_qspi_priv *priv,
178 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100179{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200180 void (*fifo)(u8 *val, void __iomem *addr);
181 u32 len = op->data.nbytes, sr;
182 u8 *buf;
183 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100184
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200185 if (op->data.dir == SPI_MEM_DATA_IN) {
186 fifo = _stm32_qspi_read_fifo;
187 buf = op->data.buf.in;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100188
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200189 } else {
190 fifo = _stm32_qspi_write_fifo;
191 buf = (u8 *)op->data.buf.out;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100192 }
193
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200194 while (len--) {
195 ret = readl_poll_timeout(&priv->regs->sr, sr,
196 sr & STM32_QSPI_SR_FTF,
197 STM32_QSPI_FIFO_TIMEOUT_US);
198 if (ret) {
199 pr_err("fifo timeout (len:%d stat:%#x)\n", len, sr);
200 return ret;
201 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100202
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200203 fifo(buf++, &priv->regs->dr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100204 }
Christophe Kerelloa1a725a2018-07-09 15:32:37 +0200205
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200206 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100207}
208
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200209static int stm32_qspi_mm(struct stm32_qspi_priv *priv,
210 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100211{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200212 memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val,
213 op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100214
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200215 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100216}
217
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200218static int _stm32_qspi_tx(struct stm32_qspi_priv *priv,
219 const struct spi_mem_op *op,
220 u8 mode)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100221{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200222 if (!op->data.nbytes)
223 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100224
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200225 if (mode == STM32_QSPI_CCR_MEM_MAP)
226 return stm32_qspi_mm(priv, op);
227
228 return _stm32_qspi_poll(priv, op);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100229}
230
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200231static int _stm32_qspi_get_mode(u8 buswidth)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100232{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200233 if (buswidth == 4)
234 return 3;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100235
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200236 return buswidth;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100237}
238
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200239static int stm32_qspi_exec_op(struct spi_slave *slave,
240 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100241{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200242 struct stm32_qspi_priv *priv = dev_get_priv(slave->dev->parent);
243 u32 cr, ccr, addr_max;
244 u8 mode = STM32_QSPI_CCR_IND_WRITE;
245 int timeout, ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100246
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200247 debug("%s: cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
248 __func__, op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
249 op->dummy.buswidth, op->data.buswidth,
250 op->addr.val, op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100251
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200252 ret = _stm32_qspi_wait_for_not_busy(priv);
253 if (ret)
254 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100255
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200256 addr_max = op->addr.val + op->data.nbytes + 1;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100257
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200258 if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) {
259 if (addr_max < priv->mm_size && op->addr.buswidth)
260 mode = STM32_QSPI_CCR_MEM_MAP;
261 else
262 mode = STM32_QSPI_CCR_IND_READ;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100263 }
264
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200265 if (op->data.nbytes)
266 writel(op->data.nbytes - 1, &priv->regs->dlr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100267
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200268 ccr = (mode << STM32_QSPI_CCR_FMODE_SHIFT);
269 ccr |= op->cmd.opcode;
270 ccr |= (_stm32_qspi_get_mode(op->cmd.buswidth)
271 << STM32_QSPI_CCR_IMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100272
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200273 if (op->addr.nbytes) {
274 ccr |= ((op->addr.nbytes - 1) << STM32_QSPI_CCR_ADSIZE_SHIFT);
275 ccr |= (_stm32_qspi_get_mode(op->addr.buswidth)
276 << STM32_QSPI_CCR_ADMODE_SHIFT);
277 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100278
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200279 if (op->dummy.buswidth && op->dummy.nbytes)
280 ccr |= (op->dummy.nbytes * 8 / op->dummy.buswidth
281 << STM32_QSPI_CCR_DCYC_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100282
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200283 if (op->data.nbytes)
284 ccr |= (_stm32_qspi_get_mode(op->data.buswidth)
285 << STM32_QSPI_CCR_DMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100286
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200287 writel(ccr, &priv->regs->ccr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100288
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200289 if (op->addr.nbytes && mode != STM32_QSPI_CCR_MEM_MAP)
290 writel(op->addr.val, &priv->regs->ar);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100291
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200292 ret = _stm32_qspi_tx(priv, op, mode);
293 /*
294 * Abort in:
295 * -error case
296 * -read memory map: prefetching must be stopped if we read the last
297 * byte of device (device size - fifo size). like device size is not
298 * knows, the prefetching is always stop.
299 */
300 if (ret || mode == STM32_QSPI_CCR_MEM_MAP)
301 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100302
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200303 /* Wait end of tx in indirect mode */
304 ret = _stm32_qspi_wait_cmd(priv, op);
305 if (ret)
306 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100307
308 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100309
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200310abort:
311 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100312
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200313 /* Wait clear of abort bit by hw */
314 timeout = readl_poll_timeout(&priv->regs->cr, cr,
315 !(cr & STM32_QSPI_CR_ABORT),
316 STM32_ABT_TIMEOUT_US);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100317
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200318 writel(STM32_QSPI_FCR_CTCF, &priv->regs->fcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100319
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200320 if (ret || timeout)
321 pr_err("%s ret:%d abort timeout:%d\n", __func__, ret, timeout);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100322
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200323 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100324}
325
326static int stm32_qspi_probe(struct udevice *bus)
327{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100328 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200329 struct resource res;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200330 struct clk clk;
Patrice Chotardb38c8972018-05-14 15:42:56 +0200331 struct reset_ctl reset_ctl;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200332 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100333
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200334 ret = dev_read_resource_byname(bus, "qspi", &res);
335 if (ret) {
336 dev_err(bus, "can't get regs base addresses(ret = %d)!\n", ret);
337 return ret;
338 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100339
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200340 priv->regs = (struct stm32_qspi_regs *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100341
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200342 ret = dev_read_resource_byname(bus, "qspi_mm", &res);
343 if (ret) {
344 dev_err(bus, "can't get mmap base address(ret = %d)!\n", ret);
345 return ret;
346 }
347
348 priv->mm_base = (void __iomem *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100349
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200350 priv->mm_size = resource_size(&res);
351 if (priv->mm_size > STM32_QSPI_MAX_MMAP_SZ)
352 return -EINVAL;
353
354 debug("%s: regs=<0x%p> mapped=<0x%p> mapped_size=<0x%lx>\n",
355 __func__, priv->regs, priv->mm_base, priv->mm_size);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100356
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700357 ret = clk_get_by_index(bus, 0, &clk);
358 if (ret < 0)
359 return ret;
360
361 ret = clk_enable(&clk);
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700362 if (ret) {
363 dev_err(bus, "failed to enable clock\n");
364 return ret;
365 }
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200366
367 priv->clock_rate = clk_get_rate(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200368 if (!priv->clock_rate) {
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200369 clk_disable(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200370 return -EINVAL;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200371 }
372
Patrice Chotardb38c8972018-05-14 15:42:56 +0200373 ret = reset_get_by_index(bus, 0, &reset_ctl);
374 if (ret) {
375 if (ret != -ENOENT) {
376 dev_err(bus, "failed to get reset\n");
377 clk_disable(&clk);
378 return ret;
379 }
380 } else {
381 /* Reset QSPI controller */
382 reset_assert(&reset_ctl);
383 udelay(2);
384 reset_deassert(&reset_ctl);
385 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100386
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200387 priv->cs_used = -1;
388
Michael Kurz337ff2a2017-01-22 16:04:30 +0100389 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT);
390
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200391 /* Set dcr fsize to max address */
392 setbits_le32(&priv->regs->dcr,
393 STM32_QSPI_DCR_FSIZE_MASK << STM32_QSPI_DCR_FSIZE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100394
Michael Kurz337ff2a2017-01-22 16:04:30 +0100395 return 0;
396}
397
398static int stm32_qspi_claim_bus(struct udevice *dev)
399{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200400 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
401 struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev);
Patrick Delaunay025020722019-06-21 15:26:55 +0200402 int slave_cs = slave_plat->cs;
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200403
Patrick Delaunay025020722019-06-21 15:26:55 +0200404 if (slave_cs >= STM32_QSPI_MAX_CHIP)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200405 return -ENODEV;
406
Patrick Delaunay025020722019-06-21 15:26:55 +0200407 if (priv->cs_used != slave_cs) {
408 struct stm32_qspi_flash *flash = &priv->flash[slave_cs];
Michael Kurz337ff2a2017-01-22 16:04:30 +0100409
Patrick Delaunay025020722019-06-21 15:26:55 +0200410 priv->cs_used = slave_cs;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100411
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200412 if (flash->initialized) {
413 /* Set the configuration: speed + cs */
414 writel(flash->cr, &priv->regs->cr);
415 writel(flash->dcr, &priv->regs->dcr);
416 } else {
417 /* Set chip select */
418 clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL,
419 priv->cs_used ? STM32_QSPI_CR_FSEL : 0);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100420
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200421 /* Save the configuration: speed + cs */
422 flash->cr = readl(&priv->regs->cr);
423 flash->dcr = readl(&priv->regs->dcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100424
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200425 flash->initialized = true;
426 }
427 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100428
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200429 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100430
431 return 0;
432}
433
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200434static int stm32_qspi_release_bus(struct udevice *dev)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100435{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200436 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100437
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200438 clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100439
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200440 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100441}
442
443static int stm32_qspi_set_speed(struct udevice *bus, uint speed)
444{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100445 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Patrick Delaunay0a861152018-05-14 15:42:50 +0200446 u32 qspi_clk = priv->clock_rate;
447 u32 prescaler = 255;
448 u32 csht;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200449 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100450
Michael Kurz337ff2a2017-01-22 16:04:30 +0100451 if (speed > 0) {
Patrick Delaunay025020722019-06-21 15:26:55 +0200452 prescaler = 0;
453 if (qspi_clk) {
454 prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1;
455 if (prescaler > 255)
456 prescaler = 255;
457 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100458 }
459
Patrick Delaunay0a861152018-05-14 15:42:50 +0200460 csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100461 csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK;
462
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200463 ret = _stm32_qspi_wait_for_not_busy(priv);
464 if (ret)
465 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100466
467 clrsetbits_le32(&priv->regs->cr,
468 STM32_QSPI_CR_PRESCALER_MASK <<
469 STM32_QSPI_CR_PRESCALER_SHIFT,
470 prescaler << STM32_QSPI_CR_PRESCALER_SHIFT);
471
Michael Kurz337ff2a2017-01-22 16:04:30 +0100472 clrsetbits_le32(&priv->regs->dcr,
473 STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT,
474 csht << STM32_QSPI_DCR_CSHT_SHIFT);
475
476 debug("%s: regs=%p, speed=%d\n", __func__, priv->regs,
477 (qspi_clk / (prescaler + 1)));
478
479 return 0;
480}
481
482static int stm32_qspi_set_mode(struct udevice *bus, uint mode)
483{
484 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200485 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100486
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200487 ret = _stm32_qspi_wait_for_not_busy(priv);
488 if (ret)
489 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100490
491 if ((mode & SPI_CPHA) && (mode & SPI_CPOL))
492 setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
493 else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL))
494 clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
495 else
496 return -ENODEV;
497
498 if (mode & SPI_CS_HIGH)
499 return -ENODEV;
500
Michael Kurz337ff2a2017-01-22 16:04:30 +0100501 debug("%s: regs=%p, mode=%d rx: ", __func__, priv->regs, mode);
502
503 if (mode & SPI_RX_QUAD)
504 debug("quad, tx: ");
505 else if (mode & SPI_RX_DUAL)
506 debug("dual, tx: ");
507 else
508 debug("single, tx: ");
509
510 if (mode & SPI_TX_QUAD)
511 debug("quad\n");
512 else if (mode & SPI_TX_DUAL)
513 debug("dual\n");
514 else
515 debug("single\n");
516
517 return 0;
518}
519
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200520static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
521 .exec_op = stm32_qspi_exec_op,
522};
523
Michael Kurz337ff2a2017-01-22 16:04:30 +0100524static const struct dm_spi_ops stm32_qspi_ops = {
525 .claim_bus = stm32_qspi_claim_bus,
526 .release_bus = stm32_qspi_release_bus,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100527 .set_speed = stm32_qspi_set_speed,
528 .set_mode = stm32_qspi_set_mode,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200529 .mem_ops = &stm32_qspi_mem_ops,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100530};
531
532static const struct udevice_id stm32_qspi_ids[] = {
Christophe Kerello4c00d552018-05-14 15:42:53 +0200533 { .compatible = "st,stm32f469-qspi" },
Michael Kurz337ff2a2017-01-22 16:04:30 +0100534 { }
535};
536
537U_BOOT_DRIVER(stm32_qspi) = {
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200538 .name = "stm32_qspi",
539 .id = UCLASS_SPI,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100540 .of_match = stm32_qspi_ids,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200541 .ops = &stm32_qspi_ops,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100542 .priv_auto_alloc_size = sizeof(struct stm32_qspi_priv),
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200543 .probe = stm32_qspi_probe,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100544};