blob: 75e5e840edbd9fedaa2d246fb420c823bdbc5cc1 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Michael Kurzd4363ba2017-01-22 16:04:30 +01002/*
3 * (C) Copyright 2016
4 *
5 * Michael Kurz, <michi.kurz@gmail.com>
6 *
7 * STM32 QSPI driver
Michael Kurzd4363ba2017-01-22 16:04:30 +01008 */
9
Patrick Delaunay162f5882020-11-06 19:01:53 +010010#define LOG_CATEGORY UCLASS_SPI
11
Michael Kurzd4363ba2017-01-22 16:04:30 +010012#include <common.h>
Patrice Chotard8c4592d2018-05-14 15:42:51 +020013#include <clk.h>
Simon Glass340fd102020-07-19 10:15:34 -060014#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060015#include <log.h>
Patrice Chotard5e461232018-05-14 15:42:56 +020016#include <reset.h>
Simon Glass340fd102020-07-19 10:15:34 -060017#include <spi.h>
Christophe Kerello321d1532019-04-05 11:46:50 +020018#include <spi-mem.h>
Simon Glass336d4612020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Simon Glasscd93d622020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassc05ed002020-05-10 11:40:11 -060021#include <linux/delay.h>
Christophe Kerello321d1532019-04-05 11:46:50 +020022#include <linux/iopoll.h>
Patrice Chotard2a6ca732018-05-14 15:42:55 +020023#include <linux/ioport.h>
Christophe Kerello321d1532019-04-05 11:46:50 +020024#include <linux/sizes.h>
Michael Kurzd4363ba2017-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 Kerello321d1532019-04-05 11:46:50 +020052#define STM32_QSPI_CR_FTHRES_SHIFT 8
Michael Kurzd4363ba2017-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 Kerello321d1532019-04-05 11:46:50 +020061#define STM32_QSPI_CR_PRESCALER_SHIFT 24
Michael Kurzd4363ba2017-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 Kerello321d1532019-04-05 11:46:50 +020068#define STM32_QSPI_DCR_CSHT_SHIFT 8
Michael Kurzd4363ba2017-01-22 16:04:30 +010069#define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0)
Christophe Kerello321d1532019-04-05 11:46:50 +020070#define STM32_QSPI_DCR_FSIZE_SHIFT 16
Michael Kurzd4363ba2017-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 Kurzd4363ba2017-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 Kerello321d1532019-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 Kurzd4363ba2017-01-22 16:04:30 +0100104
Christophe Kerello321d1532019-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 Kurzd4363ba2017-01-22 16:04:30 +0100108
Christophe Kerello321d1532019-04-05 11:46:50 +0200109#define STM32_QSPI_MAX_MMAP_SZ SZ_256M
110#define STM32_QSPI_MAX_CHIP 2
Michael Kurzd4363ba2017-01-22 16:04:30 +0100111
Christophe Kerello321d1532019-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 Kurzd4363ba2017-01-22 16:04:30 +0100116
Christophe Kerello321d1532019-04-05 11:46:50 +0200117struct stm32_qspi_flash {
118 u32 cr;
119 u32 dcr;
120 bool initialized;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100121};
122
123struct stm32_qspi_priv {
124 struct stm32_qspi_regs *regs;
Christophe Kerello321d1532019-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 Chotard541cd6e2017-07-18 09:29:09 +0200128 ulong clock_rate;
Christophe Kerello321d1532019-04-05 11:46:50 +0200129 int cs_used;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100130};
131
Christophe Kerello321d1532019-04-05 11:46:50 +0200132static int _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv)
Michael Kurzd4363ba2017-01-22 16:04:30 +0100133{
Christophe Kerello321d1532019-04-05 11:46:50 +0200134 u32 sr;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100135 int ret;
136
Christophe Kerello321d1532019-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 Delaunay162f5882020-11-06 19:01:53 +0100141 log_err("busy timeout (stat:%#x)\n", sr);
Christophe Kerello321d1532019-04-05 11:46:50 +0200142
143 return ret;
144}
145
146static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv,
147 const struct spi_mem_op *op)
148{
149 u32 sr;
150 int ret;
151
152 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);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100158 if (ret) {
Patrick Delaunay162f5882020-11-06 19:01:53 +0100159 log_err("cmd timeout (stat:%#x)\n", sr);
Christophe Kerello321d1532019-04-05 11:46:50 +0200160 } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) {
Patrick Delaunay162f5882020-11-06 19:01:53 +0100161 log_err("transfer error (stat:%#x)\n", sr);
Christophe Kerello321d1532019-04-05 11:46:50 +0200162 ret = -EIO;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100163 }
164
Christophe Kerello321d1532019-04-05 11:46:50 +0200165 /* clear flags */
166 writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100167
Christophe Kerello321d1532019-04-05 11:46:50 +0200168 return ret;
169}
Michael Kurzd4363ba2017-01-22 16:04:30 +0100170
Christophe Kerello321d1532019-04-05 11:46:50 +0200171static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr)
172{
173 *val = readb(addr);
174}
175
176static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr)
177{
178 writeb(*val, addr);
179}
180
181static int _stm32_qspi_poll(struct stm32_qspi_priv *priv,
182 const struct spi_mem_op *op)
183{
184 void (*fifo)(u8 *val, void __iomem *addr);
185 u32 len = op->data.nbytes, sr;
186 u8 *buf;
187 int ret;
188
189 if (op->data.dir == SPI_MEM_DATA_IN) {
190 fifo = _stm32_qspi_read_fifo;
191 buf = op->data.buf.in;
192
193 } else {
194 fifo = _stm32_qspi_write_fifo;
195 buf = (u8 *)op->data.buf.out;
196 }
197
198 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 Delaunay162f5882020-11-06 19:01:53 +0100203 log_err("fifo timeout (len:%d stat:%#x)\n", len, sr);
Christophe Kerello321d1532019-04-05 11:46:50 +0200204 return ret;
205 }
206
207 fifo(buf++, &priv->regs->dr);
208 }
Michael Kurzd4363ba2017-01-22 16:04:30 +0100209
210 return 0;
211}
212
Christophe Kerello321d1532019-04-05 11:46:50 +0200213static int stm32_qspi_mm(struct stm32_qspi_priv *priv,
214 const struct spi_mem_op *op)
215{
216 memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val,
217 op->data.nbytes);
218
219 return 0;
220}
221
222static int _stm32_qspi_tx(struct stm32_qspi_priv *priv,
223 const struct spi_mem_op *op,
224 u8 mode)
225{
226 if (!op->data.nbytes)
227 return 0;
228
229 if (mode == STM32_QSPI_CCR_MEM_MAP)
230 return stm32_qspi_mm(priv, op);
231
232 return _stm32_qspi_poll(priv, op);
233}
234
235static int _stm32_qspi_get_mode(u8 buswidth)
236{
237 if (buswidth == 4)
238 return 3;
239
240 return buswidth;
241}
242
243static int stm32_qspi_exec_op(struct spi_slave *slave,
244 const struct spi_mem_op *op)
245{
246 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;
250
Patrick Delaunay162f5882020-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);
Christophe Kerello321d1532019-04-05 11:46:50 +0200255
256 ret = _stm32_qspi_wait_for_not_busy(priv);
257 if (ret)
258 return ret;
259
260 addr_max = op->addr.val + op->data.nbytes + 1;
261
262 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;
267 }
268
269 if (op->data.nbytes)
270 writel(op->data.nbytes - 1, &priv->regs->dlr);
271
272 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);
276
277 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 }
282
283 if (op->dummy.buswidth && op->dummy.nbytes)
284 ccr |= (op->dummy.nbytes * 8 / op->dummy.buswidth
285 << STM32_QSPI_CCR_DCYC_SHIFT);
286
287 if (op->data.nbytes)
288 ccr |= (_stm32_qspi_get_mode(op->data.buswidth)
289 << STM32_QSPI_CCR_DMODE_SHIFT);
290
291 writel(ccr, &priv->regs->ccr);
292
293 if (op->addr.nbytes && mode != STM32_QSPI_CCR_MEM_MAP)
294 writel(op->addr.val, &priv->regs->ar);
295
296 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;
306
307 /* Wait end of tx in indirect mode */
308 ret = _stm32_qspi_wait_cmd(priv, op);
309 if (ret)
310 goto abort;
311
312 return 0;
313
314abort:
315 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT);
316
317 /* 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);
321
322 writel(STM32_QSPI_FCR_CTCF, &priv->regs->fcr);
323
324 if (ret || timeout)
Patrick Delaunay162f5882020-11-06 19:01:53 +0100325 dev_err(slave->dev, "ret:%d abort timeout:%d\n", ret, timeout);
Christophe Kerello321d1532019-04-05 11:46:50 +0200326
327 return ret;
328}
329
Michael Kurzd4363ba2017-01-22 16:04:30 +0100330static int stm32_qspi_probe(struct udevice *bus)
331{
Michael Kurzd4363ba2017-01-22 16:04:30 +0100332 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Christophe Kerello321d1532019-04-05 11:46:50 +0200333 struct resource res;
Patrice Chotard12e7c912018-05-14 15:42:49 +0200334 struct clk clk;
Patrice Chotard5e461232018-05-14 15:42:56 +0200335 struct reset_ctl reset_ctl;
Patrice Chotard12e7c912018-05-14 15:42:49 +0200336 int ret;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100337
Christophe Kerello321d1532019-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 Kurzd4363ba2017-01-22 16:04:30 +0100343
Christophe Kerello321d1532019-04-05 11:46:50 +0200344 priv->regs = (struct stm32_qspi_regs *)res.start;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100345
Christophe Kerello321d1532019-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 }
Michael Kurzd4363ba2017-01-22 16:04:30 +0100351
Christophe Kerello321d1532019-04-05 11:46:50 +0200352 priv->mm_base = (void __iomem *)res.start;
353
354 priv->mm_size = resource_size(&res);
355 if (priv->mm_size > STM32_QSPI_MAX_MMAP_SZ)
356 return -EINVAL;
357
Patrick Delaunay162f5882020-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 Kurzd4363ba2017-01-22 16:04:30 +0100360
Vikas Manocha890bafd2017-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 Manocha890bafd2017-04-10 15:02:50 -0700366 if (ret) {
367 dev_err(bus, "failed to enable clock\n");
368 return ret;
369 }
Patrice Chotard541cd6e2017-07-18 09:29:09 +0200370
371 priv->clock_rate = clk_get_rate(&clk);
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200372 if (!priv->clock_rate) {
Patrice Chotard541cd6e2017-07-18 09:29:09 +0200373 clk_disable(&clk);
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200374 return -EINVAL;
Patrice Chotard541cd6e2017-07-18 09:29:09 +0200375 }
376
Patrice Chotard5e461232018-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 Kurzd4363ba2017-01-22 16:04:30 +0100390
Christophe Kerello321d1532019-04-05 11:46:50 +0200391 priv->cs_used = -1;
392
Michael Kurzd4363ba2017-01-22 16:04:30 +0100393 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT);
394
Christophe Kerello321d1532019-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 Kurzd4363ba2017-01-22 16:04:30 +0100398
Michael Kurzd4363ba2017-01-22 16:04:30 +0100399 return 0;
400}
401
402static int stm32_qspi_claim_bus(struct udevice *dev)
403{
Christophe Kerello321d1532019-04-05 11:46:50 +0200404 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Simon Glass8a8d24b2020-12-03 16:55:23 -0700405 struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev);
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200406 int slave_cs = slave_plat->cs;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100407
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200408 if (slave_cs >= STM32_QSPI_MAX_CHIP)
Christophe Kerello495f3b22018-05-14 15:42:54 +0200409 return -ENODEV;
410
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200411 if (priv->cs_used != slave_cs) {
412 struct stm32_qspi_flash *flash = &priv->flash[slave_cs];
Michael Kurzd4363ba2017-01-22 16:04:30 +0100413
Patrick Delaunay1ddf5442019-06-21 15:26:55 +0200414 priv->cs_used = slave_cs;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100415
Christophe Kerello321d1532019-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);
424
425 /* Save the configuration: speed + cs */
426 flash->cr = readl(&priv->regs->cr);
427 flash->dcr = readl(&priv->regs->dcr);
428
429 flash->initialized = true;
430 }
431 }
432
433 setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100434
435 return 0;
436}
437
438static int stm32_qspi_release_bus(struct udevice *dev)
439{
Christophe Kerello321d1532019-04-05 11:46:50 +0200440 struct stm32_qspi_priv *priv = dev_get_priv(dev->parent);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100441
Christophe Kerello321d1532019-04-05 11:46:50 +0200442 clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100443
444 return 0;
445}
446
Michael Kurzd4363ba2017-01-22 16:04:30 +0100447static int stm32_qspi_set_speed(struct udevice *bus, uint speed)
448{
Michael Kurzd4363ba2017-01-22 16:04:30 +0100449 struct stm32_qspi_priv *priv = dev_get_priv(bus);
Patrick Delaunay936abad2018-05-14 15:42:50 +0200450 u32 qspi_clk = priv->clock_rate;
451 u32 prescaler = 255;
452 u32 csht;
Christophe Kerello321d1532019-04-05 11:46:50 +0200453 int ret;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100454
Michael Kurzd4363ba2017-01-22 16:04:30 +0100455 if (speed > 0) {
Patrick Delaunay1ddf5442019-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 Kurzd4363ba2017-01-22 16:04:30 +0100462 }
463
Patrick Delaunay936abad2018-05-14 15:42:50 +0200464 csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000);
Michael Kurzd4363ba2017-01-22 16:04:30 +0100465 csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK;
466
Christophe Kerello321d1532019-04-05 11:46:50 +0200467 ret = _stm32_qspi_wait_for_not_busy(priv);
468 if (ret)
469 return ret;
Michael Kurzd4363ba2017-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 Kurzd4363ba2017-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 Delaunay162f5882020-11-06 19:01:53 +0100480 dev_dbg(bus, "regs=%p, speed=%d\n", priv->regs,
481 (qspi_clk / (prescaler + 1)));
Michael Kurzd4363ba2017-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 Kerello321d1532019-04-05 11:46:50 +0200489 int ret;
Patrick Delaunay162f5882020-11-06 19:01:53 +0100490 const char *str_rx, *str_tx;
Michael Kurzd4363ba2017-01-22 16:04:30 +0100491
Christophe Kerello321d1532019-04-05 11:46:50 +0200492 ret = _stm32_qspi_wait_for_not_busy(priv);
493 if (ret)
494 return ret;
Michael Kurzd4363ba2017-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 Kurzd4363ba2017-01-22 16:04:30 +0100506 if (mode & SPI_RX_QUAD)
Patrick Delaunay162f5882020-11-06 19:01:53 +0100507 str_rx = "quad";
Michael Kurzd4363ba2017-01-22 16:04:30 +0100508 else if (mode & SPI_RX_DUAL)
Patrick Delaunay162f5882020-11-06 19:01:53 +0100509 str_rx = "dual";
Michael Kurzd4363ba2017-01-22 16:04:30 +0100510 else
Patrick Delaunay162f5882020-11-06 19:01:53 +0100511 str_rx = "single";
Michael Kurzd4363ba2017-01-22 16:04:30 +0100512
513 if (mode & SPI_TX_QUAD)
Patrick Delaunay162f5882020-11-06 19:01:53 +0100514 str_tx = "quad";
Michael Kurzd4363ba2017-01-22 16:04:30 +0100515 else if (mode & SPI_TX_DUAL)
Patrick Delaunay162f5882020-11-06 19:01:53 +0100516 str_tx = "dual";
Michael Kurzd4363ba2017-01-22 16:04:30 +0100517 else
Patrick Delaunay162f5882020-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 Kurzd4363ba2017-01-22 16:04:30 +0100522
523 return 0;
524}
525
Christophe Kerello321d1532019-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 Kurzd4363ba2017-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 Kurzd4363ba2017-01-22 16:04:30 +0100533 .set_speed = stm32_qspi_set_speed,
534 .set_mode = stm32_qspi_set_mode,
Christophe Kerello321d1532019-04-05 11:46:50 +0200535 .mem_ops = &stm32_qspi_mem_ops,
Michael Kurzd4363ba2017-01-22 16:04:30 +0100536};
537
538static const struct udevice_id stm32_qspi_ids[] = {
Christophe Kerello76afe562018-05-14 15:42:53 +0200539 { .compatible = "st,stm32f469-qspi" },
Michael Kurzd4363ba2017-01-22 16:04:30 +0100540 { }
541};
542
543U_BOOT_DRIVER(stm32_qspi) = {
Christophe Kerello321d1532019-04-05 11:46:50 +0200544 .name = "stm32_qspi",
545 .id = UCLASS_SPI,
Michael Kurzd4363ba2017-01-22 16:04:30 +0100546 .of_match = stm32_qspi_ids,
Christophe Kerello321d1532019-04-05 11:46:50 +0200547 .ops = &stm32_qspi_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700548 .priv_auto = sizeof(struct stm32_qspi_priv),
Christophe Kerello321d1532019-04-05 11:46:50 +0200549 .probe = stm32_qspi_probe,
Michael Kurzd4363ba2017-01-22 16:04:30 +0100550};