blob: de7628de276ac9458620a242944d764fbf840b27 [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
23#define CQSPI_STIG_READ 0
24#define CQSPI_STIG_WRITE 1
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053025#define CQSPI_READ 2
26#define CQSPI_WRITE 3
Stefan Roese10e8bf82014-11-07 12:37:49 +010027
Stefan Roese10e8bf82014-11-07 12:37:49 +010028static int cadence_spi_write_speed(struct udevice *bus, uint hz)
29{
Simon Glass0fd3d912020-12-22 19:30:28 -070030 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +010031 struct cadence_spi_priv *priv = dev_get_priv(bus);
32
33 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010034 plat->ref_clk_hz, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010035
36 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010037 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese10e8bf82014-11-07 12:37:49 +010038 plat->tshsl_ns, plat->tsd2d_ns,
39 plat->tchsh_ns, plat->tslch_ns);
40
41 return 0;
42}
43
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053044static int cadence_spi_read_id(void *reg_base, u8 len, u8 *idcode)
45{
46 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
47 SPI_MEM_OP_NO_ADDR,
48 SPI_MEM_OP_NO_DUMMY,
49 SPI_MEM_OP_DATA_IN(len, idcode, 1));
50
51 return cadence_qspi_apb_command_read(reg_base, &op);
52}
53
Stefan Roese10e8bf82014-11-07 12:37:49 +010054/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050055static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010056{
Stefan Roese10e8bf82014-11-07 12:37:49 +010057 struct cadence_spi_priv *priv = dev_get_priv(bus);
58 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +010059 unsigned int idcode = 0, temp = 0;
60 int err = 0, i, range_lo = -1, range_hi = -1;
61
62 /* start with slowest clock (1 MHz) */
63 cadence_spi_write_speed(bus, 1000000);
64
65 /* configure the read data capture delay register to 0 */
66 cadence_qspi_apb_readdata_capture(base, 1, 0);
67
68 /* Enable QSPI */
69 cadence_qspi_apb_controller_enable(base);
70
71 /* read the ID which will be our golden value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053072 err = cadence_spi_read_id(base, 3, (u8 *)&idcode);
Stefan Roese10e8bf82014-11-07 12:37:49 +010073 if (err) {
74 puts("SF: Calibration failed (read)\n");
75 return err;
76 }
77
78 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050079 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010080 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
81 /* Disable QSPI */
82 cadence_qspi_apb_controller_disable(base);
83
84 /* reconfigure the read data capture delay register */
85 cadence_qspi_apb_readdata_capture(base, 1, i);
86
87 /* Enable back QSPI */
88 cadence_qspi_apb_controller_enable(base);
89
90 /* issue a RDID to get the ID value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053091 err = cadence_spi_read_id(base, 3, (u8 *)&temp);
Stefan Roese10e8bf82014-11-07 12:37:49 +010092 if (err) {
93 puts("SF: Calibration failed (read)\n");
94 return err;
95 }
96
97 /* search for range lo */
98 if (range_lo == -1 && temp == idcode) {
99 range_lo = i;
100 continue;
101 }
102
103 /* search for range hi */
104 if (range_lo != -1 && temp != idcode) {
105 range_hi = i - 1;
106 break;
107 }
108 range_hi = i;
109 }
110
111 if (range_lo == -1) {
112 puts("SF: Calibration failed (low range)\n");
113 return err;
114 }
115
116 /* Disable QSPI for subsequent initialization */
117 cadence_qspi_apb_controller_disable(base);
118
119 /* configure the final value for read data capture delay register */
120 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
121 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
122 (range_hi + range_lo) / 2, range_lo, range_hi);
123
124 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500125 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100126 priv->qspi_calibrated_cs = spi_chip_select(bus);
127
128 return 0;
129}
130
131static int cadence_spi_set_speed(struct udevice *bus, uint hz)
132{
Simon Glass0fd3d912020-12-22 19:30:28 -0700133 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100134 struct cadence_spi_priv *priv = dev_get_priv(bus);
135 int err;
136
Chin Liang See4e609b62015-10-17 08:32:38 -0500137 if (hz > plat->max_hz)
138 hz = plat->max_hz;
139
Stefan Roese10e8bf82014-11-07 12:37:49 +0100140 /* Disable QSPI */
141 cadence_qspi_apb_controller_disable(priv->regbase);
142
Chin Liang See98fbd712015-10-17 08:31:55 -0500143 /*
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530144 * If the device tree already provides a read delay value, use that
145 * instead of calibrating.
Chin Liang See98fbd712015-10-17 08:31:55 -0500146 */
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530147 if (plat->read_delay >= 0) {
148 cadence_spi_write_speed(bus, hz);
149 cadence_qspi_apb_readdata_capture(priv->regbase, 1,
150 plat->read_delay);
151 } else if (priv->previous_hz != hz ||
152 priv->qspi_calibrated_hz != hz ||
153 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
154 /*
155 * Calibration required for different current SCLK speed,
156 * requested SCLK speed or chip select
157 */
Chin Liang See98fbd712015-10-17 08:31:55 -0500158 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100159 if (err)
160 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500161
162 /* prevent calibration run when same as previous request */
163 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100164 }
165
166 /* Enable QSPI */
167 cadence_qspi_apb_controller_enable(priv->regbase);
168
169 debug("%s: speed=%d\n", __func__, hz);
170
171 return 0;
172}
173
174static int cadence_spi_probe(struct udevice *bus)
175{
Simon Glass0fd3d912020-12-22 19:30:28 -0700176 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100177 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav0a9c2872020-02-24 12:40:51 +0530178 struct clk clk;
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100179 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100180
181 priv->regbase = plat->regbase;
182 priv->ahbbase = plat->ahbbase;
183
Pratyush Yadav0a9c2872020-02-24 12:40:51 +0530184 if (plat->ref_clk_hz == 0) {
185 ret = clk_get_by_index(bus, 0, &clk);
186 if (ret) {
187#ifdef CONFIG_CQSPI_REF_CLK
188 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
189#else
190 return ret;
191#endif
192 } else {
193 plat->ref_clk_hz = clk_get_rate(&clk);
194 clk_free(&clk);
195 if (IS_ERR_VALUE(plat->ref_clk_hz))
196 return plat->ref_clk_hz;
197 }
198 }
199
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100200 ret = reset_get_bulk(bus, &priv->resets);
201 if (ret)
202 dev_warn(bus, "Can't get reset: %d\n", ret);
203 else
204 reset_deassert_bulk(&priv->resets);
205
Stefan Roese10e8bf82014-11-07 12:37:49 +0100206 if (!priv->qspi_is_init) {
207 cadence_qspi_apb_controller_init(plat);
208 priv->qspi_is_init = 1;
209 }
210
211 return 0;
212}
213
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100214static int cadence_spi_remove(struct udevice *dev)
215{
216 struct cadence_spi_priv *priv = dev_get_priv(dev);
217
218 return reset_release_bulk(&priv->resets);
219}
220
Stefan Roese10e8bf82014-11-07 12:37:49 +0100221static int cadence_spi_set_mode(struct udevice *bus, uint mode)
222{
Simon Glass0fd3d912020-12-22 19:30:28 -0700223 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100224 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100225
226 /* Disable QSPI */
227 cadence_qspi_apb_controller_disable(priv->regbase);
228
229 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000230 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100231
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530232 /* Enable Direct Access Controller */
233 if (plat->use_dac_mode)
234 cadence_qspi_apb_dac_mode_enable(priv->regbase);
235
Stefan Roese10e8bf82014-11-07 12:37:49 +0100236 /* Enable QSPI */
237 cadence_qspi_apb_controller_enable(priv->regbase);
238
239 return 0;
240}
241
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530242static int cadence_spi_mem_exec_op(struct spi_slave *spi,
243 const struct spi_mem_op *op)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100244{
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530245 struct udevice *bus = spi->dev->parent;
Simon Glass0fd3d912020-12-22 19:30:28 -0700246 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100247 struct cadence_spi_priv *priv = dev_get_priv(bus);
248 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100249 int err = 0;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530250 u32 mode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100251
252 /* Set Chip select */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530253 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600254 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100255
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530256 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
257 if (!op->addr.nbytes)
258 mode = CQSPI_STIG_READ;
259 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530260 mode = CQSPI_READ;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530261 } else {
262 if (!op->addr.nbytes || !op->data.buf.out)
263 mode = CQSPI_STIG_WRITE;
264 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530265 mode = CQSPI_WRITE;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530266 }
Stefan Roese10e8bf82014-11-07 12:37:49 +0100267
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530268 switch (mode) {
269 case CQSPI_STIG_READ:
270 err = cadence_qspi_apb_command_read(base, op);
271 break;
272 case CQSPI_STIG_WRITE:
273 err = cadence_qspi_apb_command_write(base, op);
274 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530275 case CQSPI_READ:
276 err = cadence_qspi_apb_read_setup(plat, op);
277 if (!err)
278 err = cadence_qspi_apb_read_execute(plat, op);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100279 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530280 case CQSPI_WRITE:
281 err = cadence_qspi_apb_write_setup(plat, op);
282 if (!err)
283 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530284 break;
285 default:
286 err = -1;
287 break;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100288 }
289
290 return err;
291}
292
Simon Glassd1998a92020-12-03 16:55:21 -0700293static int cadence_spi_of_to_plat(struct udevice *bus)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100294{
Simon Glass0fd3d912020-12-22 19:30:28 -0700295 struct cadence_spi_plat *plat = dev_get_plat(bus);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200296 ofnode subnode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100297
Ley Foon Tan6c353672018-05-07 17:42:55 +0800298 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530299 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
300 &plat->ahbsize);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200301 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
302 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
303 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
304 plat->trigger_address = dev_read_u32_default(bus,
305 "cdns,trigger-address",
306 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530307 /* Use DAC mode only when MMIO window is at least 8M wide */
308 if (plat->ahbsize >= SZ_8M)
309 plat->use_dac_mode = true;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100310
Stefan Roese10e8bf82014-11-07 12:37:49 +0100311 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200312 subnode = dev_read_first_subnode(bus);
313 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100314 printf("Error: subnode with SPI flash config missing!\n");
315 return -ENODEV;
316 }
317
Chin Liang See040f4ba2015-10-17 08:32:14 -0500318 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200319 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
320 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500321
Stefan Roese10e8bf82014-11-07 12:37:49 +0100322 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200323 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
324 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
325 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
326 200);
327 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
328 255);
329 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
330 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Pratyush Yadavbd8c8dc2021-06-26 00:47:07 +0530331 /*
332 * Read delay should be an unsigned value but we use a signed integer
333 * so that negative values can indicate that the device tree did not
334 * specify any signed values and we need to perform the calibration
335 * sequence to find it out.
336 */
337 plat->read_delay = ofnode_read_s32_default(subnode, "cdns,read-delay",
338 -1);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100339
340 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
341 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
342 plat->page_size);
343
344 return 0;
345}
346
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530347static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
348 .exec_op = cadence_spi_mem_exec_op,
349};
350
Stefan Roese10e8bf82014-11-07 12:37:49 +0100351static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100352 .set_speed = cadence_spi_set_speed,
353 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530354 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100355 /*
356 * cs_info is not needed, since we require all chip selects to be
357 * in the device tree explicitly
358 */
359};
360
361static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100362 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendradaa94052019-12-05 15:46:07 +0530363 { .compatible = "ti,am654-ospi" },
Stefan Roese10e8bf82014-11-07 12:37:49 +0100364 { }
365};
366
367U_BOOT_DRIVER(cadence_spi) = {
368 .name = "cadence_spi",
369 .id = UCLASS_SPI,
370 .of_match = cadence_spi_ids,
371 .ops = &cadence_spi_ops,
Simon Glassd1998a92020-12-03 16:55:21 -0700372 .of_to_plat = cadence_spi_of_to_plat,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700373 .plat_auto = sizeof(struct cadence_spi_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700374 .priv_auto = sizeof(struct cadence_spi_priv),
Stefan Roese10e8bf82014-11-07 12:37:49 +0100375 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100376 .remove = cadence_spi_remove,
377 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100378};