blob: 75e5e840edbd9fedaa2d246fb420c823bdbc5cc1 [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>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020022#include <linux/iopoll.h>
Patrice Chotardb8954782018-05-14 15:42:55 +020023#include <linux/ioport.h>
Christophe Kerello6958a0d2019-04-05 11:46:50 +020024#include <linux/sizes.h>
Michael Kurz337ff2a2017-01-22 16:04:30 +010025
26struct stm32_qspi_regs {
27 u32 cr; /* 0x00 */
28 u32 dcr; /* 0x04 */
29 u32 sr; /* 0x08 */
30 u32 fcr; /* 0x0C */
31 u32 dlr; /* 0x10 */
32 u32 ccr; /* 0x14 */
33 u32 ar; /* 0x18 */
34 u32 abr; /* 0x1C */
35 u32 dr; /* 0x20 */
36 u32 psmkr; /* 0x24 */
37 u32 psmar; /* 0x28 */
38 u32 pir; /* 0x2C */
39 u32 lptr; /* 0x30 */
40};
41
42/*
43 * QUADSPI control register
44 */
45#define STM32_QSPI_CR_EN BIT(0)
46#define STM32_QSPI_CR_ABORT BIT(1)
47#define STM32_QSPI_CR_DMAEN BIT(2)
48#define STM32_QSPI_CR_TCEN BIT(3)
49#define STM32_QSPI_CR_SSHIFT BIT(4)
50#define STM32_QSPI_CR_DFM BIT(6)
51#define STM32_QSPI_CR_FSEL BIT(7)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020052#define STM32_QSPI_CR_FTHRES_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010053#define STM32_QSPI_CR_TEIE BIT(16)
54#define STM32_QSPI_CR_TCIE BIT(17)
55#define STM32_QSPI_CR_FTIE BIT(18)
56#define STM32_QSPI_CR_SMIE BIT(19)
57#define STM32_QSPI_CR_TOIE BIT(20)
58#define STM32_QSPI_CR_APMS BIT(22)
59#define STM32_QSPI_CR_PMM BIT(23)
60#define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020061#define STM32_QSPI_CR_PRESCALER_SHIFT 24
Michael Kurz337ff2a2017-01-22 16:04:30 +010062
63/*
64 * QUADSPI device configuration register
65 */
66#define STM32_QSPI_DCR_CKMODE BIT(0)
67#define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020068#define STM32_QSPI_DCR_CSHT_SHIFT 8
Michael Kurz337ff2a2017-01-22 16:04:30 +010069#define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020070#define STM32_QSPI_DCR_FSIZE_SHIFT 16
Michael Kurz337ff2a2017-01-22 16:04:30 +010071
72/*
73 * QUADSPI status register
74 */
75#define STM32_QSPI_SR_TEF BIT(0)
76#define STM32_QSPI_SR_TCF BIT(1)
77#define STM32_QSPI_SR_FTF BIT(2)
78#define STM32_QSPI_SR_SMF BIT(3)
79#define STM32_QSPI_SR_TOF BIT(4)
80#define STM32_QSPI_SR_BUSY BIT(5)
Michael Kurz337ff2a2017-01-22 16:04:30 +010081
82/*
83 * QUADSPI flag clear register
84 */
85#define STM32_QSPI_FCR_CTEF BIT(0)
86#define STM32_QSPI_FCR_CTCF BIT(1)
87#define STM32_QSPI_FCR_CSMF BIT(3)
88#define STM32_QSPI_FCR_CTOF BIT(4)
89
90/*
91 * QUADSPI communication configuration register
92 */
93#define STM32_QSPI_CCR_DDRM BIT(31)
94#define STM32_QSPI_CCR_DHHC BIT(30)
95#define STM32_QSPI_CCR_SIOO BIT(28)
Christophe Kerello6958a0d2019-04-05 11:46:50 +020096#define STM32_QSPI_CCR_FMODE_SHIFT 26
97#define STM32_QSPI_CCR_DMODE_SHIFT 24
98#define STM32_QSPI_CCR_DCYC_SHIFT 18
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
Michael Kurz337ff2a2017-01-22 16:04:30 +0100104
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200105#define STM32_QSPI_CCR_IND_WRITE 0
106#define STM32_QSPI_CCR_IND_READ 1
107#define STM32_QSPI_CCR_MEM_MAP 3
Michael Kurz337ff2a2017-01-22 16:04:30 +0100108
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200109#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
110#define STM32_QSPI_MAX_CHIP 2
Michael Kurz337ff2a2017-01-22 16:04:30 +0100111
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200112#define STM32_QSPI_FIFO_TIMEOUT_US 30000
113#define STM32_QSPI_CMD_TIMEOUT_US 1000000
114#define STM32_BUSY_TIMEOUT_US 100000
115#define STM32_ABT_TIMEOUT_US 100000
Michael Kurz337ff2a2017-01-22 16:04:30 +0100116
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200117struct stm32_qspi_flash {
118 u32 cr;
119 u32 dcr;
120 bool initialized;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100121};
122
Michael Kurz337ff2a2017-01-22 16:04:30 +0100123struct stm32_qspi_priv {
124 struct stm32_qspi_regs *regs;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200125 struct stm32_qspi_flash flash[STM32_QSPI_MAX_CHIP];
126 void __iomem *mm_base;
127 resource_size_t mm_size;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200128 ulong clock_rate;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200129 int cs_used;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100130};
131
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200132static int _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100133{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200134 u32 sr;
135 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100136
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200137 ret = readl_poll_timeout(&priv->regs->sr, sr,
138 !(sr & STM32_QSPI_SR_BUSY),
139 STM32_BUSY_TIMEOUT_US);
140 if (ret)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100141 log_err("busy timeout (stat:%#x)\n", sr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100142
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200143 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100144}
145
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200146static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,
147 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100148{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200149 u32 sr;
150 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100151
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200152 if (!op->data.nbytes)
153 return _stm32_qspi_wait_for_not_busy(priv);
154
155 ret = readl_poll_timeout(&priv->regs->sr, sr,
156 sr & STM32_QSPI_SR_TCF,
157 STM32_QSPI_CMD_TIMEOUT_US);
158 if (ret) {
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100159 log_err("cmd timeout (stat:%#x)\n", sr);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200160 } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) {
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100161 log_err("transfer error (stat:%#x)\n", sr);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200162 ret = -EIO;
163 }
164
165 /* clear flags */
166 writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr);
167
168 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100169}
170
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200171static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100172{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200173 *val = readb(addr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100174}
175
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200176static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200177{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200178 writeb(*val, addr);
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200179}
180
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200181static int _stm32_qspi_poll(struct stm32_qspi_priv *priv,
182 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100183{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200184 void (*fifo)(u8 *val, void __iomem *addr);
185 u32 len = op->data.nbytes, sr;
186 u8 *buf;
187 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100188
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200189 if (op->data.dir == SPI_MEM_DATA_IN) {
190 fifo = _stm32_qspi_read_fifo;
191 buf = op->data.buf.in;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100192
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200193 } else {
194 fifo = _stm32_qspi_write_fifo;
195 buf = (u8 *)op->data.buf.out;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100196 }
197
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200198 while (len--) {
199 ret = readl_poll_timeout(&priv->regs->sr, sr,
200 sr & STM32_QSPI_SR_FTF,
201 STM32_QSPI_FIFO_TIMEOUT_US);
202 if (ret) {
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100203 log_err("fifo timeout (len:%d stat:%#x)\n", len, sr);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200204 return ret;
205 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100206
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200207 fifo(buf++, &priv->regs->dr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100208 }
Christophe Kerelloa1a725a2018-07-09 15:32:37 +0200209
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200210 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100211}
212
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200213static int stm32_qspi_mm(struct stm32_qspi_priv *priv,
214 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100215{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200216 memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val,
217 op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100218
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200219 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100220}
221
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200222static int _stm32_qspi_tx(struct stm32_qspi_priv *priv,
223 const struct spi_mem_op *op,
224 u8 mode)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100225{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200226 if (!op->data.nbytes)
227 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100228
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200229 if (mode == STM32_QSPI_CCR_MEM_MAP)
230 return stm32_qspi_mm(priv, op);
231
232 return _stm32_qspi_poll(priv, op);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100233}
234
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200235static int _stm32_qspi_get_mode(u8 buswidth)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100236{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200237 if (buswidth == 4)
238 return 3;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100239
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200240 return buswidth;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100241}
242
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200243static int stm32_qspi_exec_op(struct spi_slave *slave,
244 const struct spi_mem_op *op)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100245{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200246 struct stm32_qspi_priv *priv = dev_get_priv(slave->dev->parent);
247 u32 cr, ccr, addr_max;
248 u8 mode = STM32_QSPI_CCR_IND_WRITE;
249 int timeout, ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100250
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100251 dev_dbg(slave->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n",
252 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth,
253 op->dummy.buswidth, op->data.buswidth,
254 op->addr.val, op->data.nbytes);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100255
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200256 ret = _stm32_qspi_wait_for_not_busy(priv);
257 if (ret)
258 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100259
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200260 addr_max = op->addr.val + op->data.nbytes + 1;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100261
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200262 if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) {
263 if (addr_max < priv->mm_size && op->addr.buswidth)
264 mode = STM32_QSPI_CCR_MEM_MAP;
265 else
266 mode = STM32_QSPI_CCR_IND_READ;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100267 }
268
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200269 if (op->data.nbytes)
270 writel(op->data.nbytes - 1, &priv->regs->dlr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100271
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200272 ccr = (mode << STM32_QSPI_CCR_FMODE_SHIFT);
273 ccr |= op->cmd.opcode;
274 ccr |= (_stm32_qspi_get_mode(op->cmd.buswidth)
275 << STM32_QSPI_CCR_IMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100276
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200277 if (op->addr.nbytes) {
278 ccr |= ((op->addr.nbytes - 1) << STM32_QSPI_CCR_ADSIZE_SHIFT);
279 ccr |= (_stm32_qspi_get_mode(op->addr.buswidth)
280 << STM32_QSPI_CCR_ADMODE_SHIFT);
281 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100282
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200283 if (op->dummy.buswidth && op->dummy.nbytes)
284 ccr |= (op->dummy.nbytes * 8 / op->dummy.buswidth
285 << STM32_QSPI_CCR_DCYC_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100286
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200287 if (op->data.nbytes)
288 ccr |= (_stm32_qspi_get_mode(op->data.buswidth)
289 << STM32_QSPI_CCR_DMODE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100290
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200291 writel(ccr, &priv->regs->ccr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100292
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200293 if (op->addr.nbytes && mode != STM32_QSPI_CCR_MEM_MAP)
294 writel(op->addr.val, &priv->regs->ar);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100295
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200296 ret = _stm32_qspi_tx(priv, op, mode);
297 /*
298 * Abort in:
299 * -error case
300 * -read memory map: prefetching must be stopped if we read the last
301 * byte of device (device size - fifo size). like device size is not
302 * knows, the prefetching is always stop.
303 */
304 if (ret || mode == STM32_QSPI_CCR_MEM_MAP)
305 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100306
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200307 /* Wait end of tx in indirect mode */
308 ret = _stm32_qspi_wait_cmd(priv, op);
309 if (ret)
310 goto abort;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100311
312 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100313
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200314abort:
315 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100316
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200317 /* Wait clear of abort bit by hw */
318 timeout = readl_poll_timeout(&priv->regs->cr, cr,
319 !(cr & STM32_QSPI_CR_ABORT),
320 STM32_ABT_TIMEOUT_US);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100321
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200322 writel(STM32_QSPI_FCR_CTCF, &priv->regs->fcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100323
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200324 if (ret || timeout)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100325 dev_err(slave->dev, "ret:%d abort timeout:%d\n", ret, timeout);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100326
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200327 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100328}
329
330static int stm32_qspi_probe(struct udevice *bus)
331{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100332 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200333 struct resource res;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200334 struct clk clk;
Patrice Chotardb38c8972018-05-14 15:42:56 +0200335 struct reset_ctl reset_ctl;
Patrice Chotard8e0497a2018-05-14 15:42:49 +0200336 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100337
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200338 ret = dev_read_resource_byname(bus, "qspi", &res);
339 if (ret) {
340 dev_err(bus, "can't get regs base addresses(ret = %d)!\n", ret);
341 return ret;
342 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100343
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200344 priv->regs = (struct stm32_qspi_regs *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100345
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200346 ret = dev_read_resource_byname(bus, "qspi_mm", &res);
347 if (ret) {
348 dev_err(bus, "can't get mmap base address(ret = %d)!\n", ret);
349 return ret;
350 }
351
352 priv->mm_base = (void __iomem *)res.start;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100353
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200354 priv->mm_size = resource_size(&res);
355 if (priv->mm_size > STM32_QSPI_MAX_MMAP_SZ)
356 return -EINVAL;
357
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100358 dev_dbg(bus, "regs=<0x%p> mapped=<0x%p> mapped_size=<0x%lx>\n",
359 priv->regs, priv->mm_base, priv->mm_size);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100360
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700361 ret = clk_get_by_index(bus, 0, &clk);
362 if (ret < 0)
363 return ret;
364
365 ret = clk_enable(&clk);
Vikas Manocha9f28b5c2017-04-10 15:02:50 -0700366 if (ret) {
367 dev_err(bus, "failed to enable clock\n");
368 return ret;
369 }
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200370
371 priv->clock_rate = clk_get_rate(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200372 if (!priv->clock_rate) {
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200373 clk_disable(&clk);
Patrick Delaunay025020722019-06-21 15:26:55 +0200374 return -EINVAL;
Patrice Chotardb8442fb2017-07-18 09:29:09 +0200375 }
376
Patrice Chotardb38c8972018-05-14 15:42:56 +0200377 ret = reset_get_by_index(bus, 0, &reset_ctl);
378 if (ret) {
379 if (ret != -ENOENT) {
380 dev_err(bus, "failed to get reset\n");
381 clk_disable(&clk);
382 return ret;
383 }
384 } else {
385 /* Reset QSPI controller */
386 reset_assert(&reset_ctl);
387 udelay(2);
388 reset_deassert(&reset_ctl);
389 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100390
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200391 priv->cs_used = -1;
392
Michael Kurz337ff2a2017-01-22 16:04:30 +0100393 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT);
394
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200395 /* Set dcr fsize to max address */
396 setbits_le32(&priv->regs->dcr,
397 STM32_QSPI_DCR_FSIZE_MASK << STM32_QSPI_DCR_FSIZE_SHIFT);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100398
Michael Kurz337ff2a2017-01-22 16:04:30 +0100399 return 0;
400}
401
402static int stm32_qspi_claim_bus(struct udevice *dev)
403{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200404 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Simon Glassb75b15b2020-12-03 16:55:23 -0700405 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Patrick Delaunay025020722019-06-21 15:26:55 +0200406 int slave_cs = slave_plat->cs;
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200407
Patrick Delaunay025020722019-06-21 15:26:55 +0200408 if (slave_cs >= STM32_QSPI_MAX_CHIP)
Christophe Kerelloe3b34cb2018-05-14 15:42:54 +0200409 return -ENODEV;
410
Patrick Delaunay025020722019-06-21 15:26:55 +0200411 if (priv->cs_used != slave_cs) {
412 struct stm32_qspi_flash *flash = &priv->flash[slave_cs];
Michael Kurz337ff2a2017-01-22 16:04:30 +0100413
Patrick Delaunay025020722019-06-21 15:26:55 +0200414 priv->cs_used = slave_cs;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100415
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200416 if (flash->initialized) {
417 /* Set the configuration: speed + cs */
418 writel(flash->cr, &priv->regs->cr);
419 writel(flash->dcr, &priv->regs->dcr);
420 } else {
421 /* Set chip select */
422 clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL,
423 priv->cs_used ? STM32_QSPI_CR_FSEL : 0);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100424
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200425 /* Save the configuration: speed + cs */
426 flash->cr = readl(&priv->regs->cr);
427 flash->dcr = readl(&priv->regs->dcr);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100428
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200429 flash->initialized = true;
430 }
431 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100432
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200433 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100434
435 return 0;
436}
437
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200438static int stm32_qspi_release_bus(struct udevice *dev)
Michael Kurz337ff2a2017-01-22 16:04:30 +0100439{
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200440 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100441
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200442 clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100443
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200444 return 0;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100445}
446
447static int stm32_qspi_set_speed(struct udevice *bus, uint speed)
448{
Michael Kurz337ff2a2017-01-22 16:04:30 +0100449 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Patrick Delaunay0a861152018-05-14 15:42:50 +0200450 u32 qspi_clk = priv->clock_rate;
451 u32 prescaler = 255;
452 u32 csht;
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200453 int ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100454
Michael Kurz337ff2a2017-01-22 16:04:30 +0100455 if (speed > 0) {
Patrick Delaunay025020722019-06-21 15:26:55 +0200456 prescaler = 0;
457 if (qspi_clk) {
458 prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1;
459 if (prescaler > 255)
460 prescaler = 255;
461 }
Michael Kurz337ff2a2017-01-22 16:04:30 +0100462 }
463
Patrick Delaunay0a861152018-05-14 15:42:50 +0200464 csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100465 csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK;
466
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200467 ret = _stm32_qspi_wait_for_not_busy(priv);
468 if (ret)
469 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100470
471 clrsetbits_le32(&priv->regs->cr,
472 STM32_QSPI_CR_PRESCALER_MASK <<
473 STM32_QSPI_CR_PRESCALER_SHIFT,
474 prescaler << STM32_QSPI_CR_PRESCALER_SHIFT);
475
Michael Kurz337ff2a2017-01-22 16:04:30 +0100476 clrsetbits_le32(&priv->regs->dcr,
477 STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT,
478 csht << STM32_QSPI_DCR_CSHT_SHIFT);
479
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100480 dev_dbg(bus, "regs=%p, speed=%d\n", priv->regs,
481 (qspi_clk / (prescaler + 1)));
Michael Kurz337ff2a2017-01-22 16:04:30 +0100482
483 return 0;
484}
485
486static int stm32_qspi_set_mode(struct udevice *bus, uint mode)
487{
488 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200489 int ret;
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100490 const char *str_rx, *str_tx;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100491
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200492 ret = _stm32_qspi_wait_for_not_busy(priv);
493 if (ret)
494 return ret;
Michael Kurz337ff2a2017-01-22 16:04:30 +0100495
496 if ((mode & SPI_CPHA) && (mode & SPI_CPOL))
497 setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
498 else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL))
499 clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE);
500 else
501 return -ENODEV;
502
503 if (mode & SPI_CS_HIGH)
504 return -ENODEV;
505
Michael Kurz337ff2a2017-01-22 16:04:30 +0100506 if (mode & SPI_RX_QUAD)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100507 str_rx = "quad";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100508 else if (mode & SPI_RX_DUAL)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100509 str_rx = "dual";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100510 else
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100511 str_rx = "single";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100512
513 if (mode & SPI_TX_QUAD)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100514 str_tx = "quad";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100515 else if (mode & SPI_TX_DUAL)
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100516 str_tx = "dual";
Michael Kurz337ff2a2017-01-22 16:04:30 +0100517 else
Patrick Delaunay72e220c2020-11-06 19:01:53 +0100518 str_tx = "single";
519
520 dev_dbg(bus, "regs=%p, mode=%d rx: %s, tx: %s\n",
521 priv->regs, mode, str_rx, str_tx);
Michael Kurz337ff2a2017-01-22 16:04:30 +0100522
523 return 0;
524}
525
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200526static const struct spi_controller_mem_ops stm32_qspi_mem_ops = {
527 .exec_op = stm32_qspi_exec_op,
528};
529
Michael Kurz337ff2a2017-01-22 16:04:30 +0100530static const struct dm_spi_ops stm32_qspi_ops = {
531 .claim_bus = stm32_qspi_claim_bus,
532 .release_bus = stm32_qspi_release_bus,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100533 .set_speed = stm32_qspi_set_speed,
534 .set_mode = stm32_qspi_set_mode,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200535 .mem_ops = &stm32_qspi_mem_ops,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100536};
537
538static const struct udevice_id stm32_qspi_ids[] = {
Christophe Kerello4c00d552018-05-14 15:42:53 +0200539 { .compatible = "st,stm32f469-qspi" },
Michael Kurz337ff2a2017-01-22 16:04:30 +0100540 { }
541};
542
543U_BOOT_DRIVER(stm32_qspi) = {
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200544 .name = "stm32_qspi",
545 .id = UCLASS_SPI,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100546 .of_match = stm32_qspi_ids,
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200547 .ops = &stm32_qspi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700548 .priv_auto = sizeof(struct stm32_qspi_priv),
Christophe Kerello6958a0d2019-04-05 11:46:50 +0200549 .probe = stm32_qspi_probe,
Michael Kurz337ff2a2017-01-22 16:04:30 +0100550};