blob: d1b3808c4db9ec60690bb19ecbc9cc84c910ed6d [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese10e8bf82014-11-07 12:37:49 +01002/*
3 * Copyright (C) 2012
4 * Altera Corporation <www.altera.com>
Stefan Roese10e8bf82014-11-07 12:37:49 +01005 */
6
7#include <common.h>
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +01008#include <clk.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Vignesh Raghavendra0f247842019-12-05 15:46:06 +053010#include <asm-generic/io.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010011#include <dm.h>
12#include <fdtdec.h>
13#include <malloc.h>
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +010014#include <reset.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010015#include <spi.h>
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053016#include <spi-mem.h>
Simon Glass336d4612020-02-03 07:36:16 -070017#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <linux/err.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090019#include <linux/errno.h>
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053020#include <linux/sizes.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010021#include "cadence_qspi.h"
22
Pratyush Yadava6903aa2021-06-26 00:47:08 +053023#define NSEC_PER_SEC 1000000000L
24
Stefan Roese10e8bf82014-11-07 12:37:49 +010025#define CQSPI_STIG_READ 0
26#define CQSPI_STIG_WRITE 1
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053027#define CQSPI_READ 2
28#define CQSPI_WRITE 3
Stefan Roese10e8bf82014-11-07 12:37:49 +010029
Stefan Roese10e8bf82014-11-07 12:37:49 +010030static int cadence_spi_write_speed(struct udevice *bus, uint hz)
31{
Simon Glass0fd3d912020-12-22 19:30:28 -070032 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +010033 struct cadence_spi_priv *priv = dev_get_priv(bus);
34
35 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010036 plat->ref_clk_hz, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010037
38 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010039 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese10e8bf82014-11-07 12:37:49 +010040 plat->tshsl_ns, plat->tsd2d_ns,
41 plat->tchsh_ns, plat->tslch_ns);
42
43 return 0;
44}
45
Pratyush Yadav38b08522021-06-26 00:47:09 +053046static int cadence_spi_read_id(struct cadence_spi_plat *plat, u8 len,
47 u8 *idcode)
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053048{
49 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
50 SPI_MEM_OP_NO_ADDR,
51 SPI_MEM_OP_NO_DUMMY,
52 SPI_MEM_OP_DATA_IN(len, idcode, 1));
53
Pratyush Yadav38b08522021-06-26 00:47:09 +053054 return cadence_qspi_apb_command_read(plat, &op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053055}
56
Stefan Roese10e8bf82014-11-07 12:37:49 +010057/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050058static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010059{
Stefan Roese10e8bf82014-11-07 12:37:49 +010060 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav38b08522021-06-26 00:47:09 +053061 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +010062 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +010063 unsigned int idcode = 0, temp = 0;
64 int err = 0, i, range_lo = -1, range_hi = -1;
65
66 /* start with slowest clock (1 MHz) */
67 cadence_spi_write_speed(bus, 1000000);
68
69 /* configure the read data capture delay register to 0 */
70 cadence_qspi_apb_readdata_capture(base, 1, 0);
71
72 /* Enable QSPI */
73 cadence_qspi_apb_controller_enable(base);
74
75 /* read the ID which will be our golden value */
Pratyush Yadav38b08522021-06-26 00:47:09 +053076 err = cadence_spi_read_id(plat, 3, (u8 *)&idcode);
Stefan Roese10e8bf82014-11-07 12:37:49 +010077 if (err) {
78 puts("SF: Calibration failed (read)\n");
79 return err;
80 }
81
82 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050083 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010084 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
85 /* Disable QSPI */
86 cadence_qspi_apb_controller_disable(base);
87
88 /* reconfigure the read data capture delay register */
89 cadence_qspi_apb_readdata_capture(base, 1, i);
90
91 /* Enable back QSPI */
92 cadence_qspi_apb_controller_enable(base);
93
94 /* issue a RDID to get the ID value */
Pratyush Yadav38b08522021-06-26 00:47:09 +053095 err = cadence_spi_read_id(plat, 3, (u8 *)&temp);
Stefan Roese10e8bf82014-11-07 12:37:49 +010096 if (err) {
97 puts("SF: Calibration failed (read)\n");
98 return err;
99 }
100
101 /* search for range lo */
102 if (range_lo == -1 && temp == idcode) {
103 range_lo = i;
104 continue;
105 }
106
107 /* search for range hi */
108 if (range_lo != -1 && temp != idcode) {
109 range_hi = i - 1;
110 break;
111 }
112 range_hi = i;
113 }
114
115 if (range_lo == -1) {
116 puts("SF: Calibration failed (low range)\n");
117 return err;
118 }
119
120 /* Disable QSPI for subsequent initialization */
121 cadence_qspi_apb_controller_disable(base);
122
123 /* configure the final value for read data capture delay register */
124 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
125 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
126 (range_hi + range_lo) / 2, range_lo, range_hi);
127
128 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500129 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100130 priv->qspi_calibrated_cs = spi_chip_select(bus);
131
132 return 0;
133}
134
135static int cadence_spi_set_speed(struct udevice *bus, uint hz)
136{
Simon Glass0fd3d912020-12-22 19:30:28 -0700137 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100138 struct cadence_spi_priv *priv = dev_get_priv(bus);
139 int err;
140
Chin Liang See4e609b62015-10-17 08:32:38 -0500141 if (hz > plat->max_hz)
142 hz = plat->max_hz;
143
Stefan Roese10e8bf82014-11-07 12:37:49 +0100144 /* Disable QSPI */
145 cadence_qspi_apb_controller_disable(priv->regbase);
146
Chin Liang See98fbd712015-10-17 08:31:55 -0500147 /*
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530148 * If the device tree already provides a read delay value, use that
149 * instead of calibrating.
Chin Liang See98fbd712015-10-17 08:31:55 -0500150 */
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530151 if (plat->read_delay >= 0) {
152 cadence_spi_write_speed(bus, hz);
153 cadence_qspi_apb_readdata_capture(priv->regbase, 1,
154 plat->read_delay);
155 } else if (priv->previous_hz != hz ||
156 priv->qspi_calibrated_hz != hz ||
157 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
158 /*
159 * Calibration required for different current SCLK speed,
160 * requested SCLK speed or chip select
161 */
Chin Liang See98fbd712015-10-17 08:31:55 -0500162 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100163 if (err)
164 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500165
166 /* prevent calibration run when same as previous request */
167 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100168 }
169
170 /* Enable QSPI */
171 cadence_qspi_apb_controller_enable(priv->regbase);
172
173 debug("%s: speed=%d\n", __func__, hz);
174
175 return 0;
176}
177
178static int cadence_spi_probe(struct udevice *bus)
179{
Simon Glass0fd3d912020-12-22 19:30:28 -0700180 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100181 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav0a9c2872020-02-24 12:40:51 +0530182 struct clk clk;
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100183 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100184
185 priv->regbase = plat->regbase;
186 priv->ahbbase = plat->ahbbase;
187
Pratyush Yadav0a9c2872020-02-24 12:40:51 +0530188 if (plat->ref_clk_hz == 0) {
189 ret = clk_get_by_index(bus, 0, &clk);
190 if (ret) {
191#ifdef CONFIG_CQSPI_REF_CLK
192 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
193#else
194 return ret;
195#endif
196 } else {
197 plat->ref_clk_hz = clk_get_rate(&clk);
198 clk_free(&clk);
199 if (IS_ERR_VALUE(plat->ref_clk_hz))
200 return plat->ref_clk_hz;
201 }
202 }
203
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100204 ret = reset_get_bulk(bus, &priv->resets);
205 if (ret)
206 dev_warn(bus, "Can't get reset: %d\n", ret);
207 else
208 reset_deassert_bulk(&priv->resets);
209
Stefan Roese10e8bf82014-11-07 12:37:49 +0100210 if (!priv->qspi_is_init) {
211 cadence_qspi_apb_controller_init(plat);
212 priv->qspi_is_init = 1;
213 }
214
Pratyush Yadava6903aa2021-06-26 00:47:08 +0530215 plat->wr_delay = 50 * DIV_ROUND_UP(NSEC_PER_SEC, plat->ref_clk_hz);
216
Stefan Roese10e8bf82014-11-07 12:37:49 +0100217 return 0;
218}
219
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100220static int cadence_spi_remove(struct udevice *dev)
221{
222 struct cadence_spi_priv *priv = dev_get_priv(dev);
223
224 return reset_release_bulk(&priv->resets);
225}
226
Stefan Roese10e8bf82014-11-07 12:37:49 +0100227static int cadence_spi_set_mode(struct udevice *bus, uint mode)
228{
Simon Glass0fd3d912020-12-22 19:30:28 -0700229 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100230 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100231
232 /* Disable QSPI */
233 cadence_qspi_apb_controller_disable(priv->regbase);
234
235 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000236 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100237
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530238 /* Enable Direct Access Controller */
239 if (plat->use_dac_mode)
240 cadence_qspi_apb_dac_mode_enable(priv->regbase);
241
Stefan Roese10e8bf82014-11-07 12:37:49 +0100242 /* Enable QSPI */
243 cadence_qspi_apb_controller_enable(priv->regbase);
244
245 return 0;
246}
247
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530248static int cadence_spi_mem_exec_op(struct spi_slave *spi,
249 const struct spi_mem_op *op)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100250{
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530251 struct udevice *bus = spi->dev->parent;
Simon Glass0fd3d912020-12-22 19:30:28 -0700252 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100253 struct cadence_spi_priv *priv = dev_get_priv(bus);
254 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100255 int err = 0;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530256 u32 mode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100257
258 /* Set Chip select */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530259 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600260 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100261
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530262 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
263 if (!op->addr.nbytes)
264 mode = CQSPI_STIG_READ;
265 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530266 mode = CQSPI_READ;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530267 } else {
268 if (!op->addr.nbytes || !op->data.buf.out)
269 mode = CQSPI_STIG_WRITE;
270 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530271 mode = CQSPI_WRITE;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530272 }
Stefan Roese10e8bf82014-11-07 12:37:49 +0100273
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530274 switch (mode) {
275 case CQSPI_STIG_READ:
Pratyush Yadav38b08522021-06-26 00:47:09 +0530276 err = cadence_qspi_apb_command_read_setup(plat, op);
277 if (!err)
278 err = cadence_qspi_apb_command_read(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530279 break;
280 case CQSPI_STIG_WRITE:
Pratyush Yadav38b08522021-06-26 00:47:09 +0530281 err = cadence_qspi_apb_command_write_setup(plat, op);
282 if (!err)
283 err = cadence_qspi_apb_command_write(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530284 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530285 case CQSPI_READ:
286 err = cadence_qspi_apb_read_setup(plat, op);
287 if (!err)
288 err = cadence_qspi_apb_read_execute(plat, op);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100289 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530290 case CQSPI_WRITE:
291 err = cadence_qspi_apb_write_setup(plat, op);
292 if (!err)
293 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530294 break;
295 default:
296 err = -1;
297 break;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100298 }
299
300 return err;
301}
302
Pratyush Yadav38b08522021-06-26 00:47:09 +0530303static bool cadence_spi_mem_supports_op(struct spi_slave *slave,
304 const struct spi_mem_op *op)
305{
306 bool all_true, all_false;
307
308 all_true = op->cmd.dtr && op->addr.dtr && op->dummy.dtr &&
309 op->data.dtr;
310 all_false = !op->cmd.dtr && !op->addr.dtr && !op->dummy.dtr &&
311 !op->data.dtr;
312
313 /* Mixed DTR modes not supported. */
314 if (!(all_true || all_false))
315 return false;
316
317 if (all_true)
318 return spi_mem_dtr_supports_op(slave, op);
319 else
320 return spi_mem_default_supports_op(slave, op);
321}
322
Simon Glassd1998a92020-12-03 16:55:21 -0700323static int cadence_spi_of_to_plat(struct udevice *bus)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100324{
Simon Glass0fd3d912020-12-22 19:30:28 -0700325 struct cadence_spi_plat *plat = dev_get_plat(bus);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200326 ofnode subnode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100327
Ley Foon Tan6c353672018-05-07 17:42:55 +0800328 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530329 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
330 &plat->ahbsize);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200331 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
332 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
333 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
334 plat->trigger_address = dev_read_u32_default(bus,
335 "cdns,trigger-address",
336 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530337 /* Use DAC mode only when MMIO window is at least 8M wide */
338 if (plat->ahbsize >= SZ_8M)
339 plat->use_dac_mode = true;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100340
Stefan Roese10e8bf82014-11-07 12:37:49 +0100341 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200342 subnode = dev_read_first_subnode(bus);
343 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100344 printf("Error: subnode with SPI flash config missing!\n");
345 return -ENODEV;
346 }
347
Chin Liang See040f4ba2015-10-17 08:32:14 -0500348 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200349 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
350 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500351
Stefan Roese10e8bf82014-11-07 12:37:49 +0100352 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200353 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
354 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
355 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
356 200);
357 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
358 255);
359 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
360 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530361 /*
362 * Read delay should be an unsigned value but we use a signed integer
363 * so that negative values can indicate that the device tree did not
364 * specify any signed values and we need to perform the calibration
365 * sequence to find it out.
366 */
367 plat->read_delay = ofnode_read_s32_default(subnode, "cdns,read-delay",
368 -1);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100369
370 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
371 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
372 plat->page_size);
373
374 return 0;
375}
376
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530377static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
378 .exec_op = cadence_spi_mem_exec_op,
Pratyush Yadav38b08522021-06-26 00:47:09 +0530379 .supports_op = cadence_spi_mem_supports_op,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530380};
381
Stefan Roese10e8bf82014-11-07 12:37:49 +0100382static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100383 .set_speed = cadence_spi_set_speed,
384 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530385 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100386 /*
387 * cs_info is not needed, since we require all chip selects to be
388 * in the device tree explicitly
389 */
390};
391
392static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100393 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendradaa94052019-12-05 15:46:07 +0530394 { .compatible = "ti,am654-ospi" },
Stefan Roese10e8bf82014-11-07 12:37:49 +0100395 { }
396};
397
398U_BOOT_DRIVER(cadence_spi) = {
399 .name = "cadence_spi",
400 .id = UCLASS_SPI,
401 .of_match = cadence_spi_ids,
402 .ops = &cadence_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700403 .of_to_plat = cadence_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700404 .plat_auto = sizeof(struct cadence_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700405 .priv_auto = sizeof(struct cadence_spi_priv),
Stefan Roese10e8bf82014-11-07 12:37:49 +0100406 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100407 .remove = cadence_spi_remove,
408 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100409};