blob: 619fff70de2f3a3ef93b973609d7a445de030014 [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>
Stefan Roese10e8bf82014-11-07 12:37:49 +01009#include <dm.h>
10#include <fdtdec.h>
11#include <malloc.h>
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +010012#include <reset.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010013#include <spi.h>
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053014#include <spi-mem.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090015#include <linux/errno.h>
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053016#include <linux/sizes.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010017#include "cadence_qspi.h"
18
19#define CQSPI_STIG_READ 0
20#define CQSPI_STIG_WRITE 1
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053021#define CQSPI_READ 2
22#define CQSPI_WRITE 3
Stefan Roese10e8bf82014-11-07 12:37:49 +010023
Stefan Roese10e8bf82014-11-07 12:37:49 +010024static int cadence_spi_write_speed(struct udevice *bus, uint hz)
25{
26 struct cadence_spi_platdata *plat = bus->platdata;
27 struct cadence_spi_priv *priv = dev_get_priv(bus);
28
29 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010030 plat->ref_clk_hz, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010031
32 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010033 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese10e8bf82014-11-07 12:37:49 +010034 plat->tshsl_ns, plat->tsd2d_ns,
35 plat->tchsh_ns, plat->tslch_ns);
36
37 return 0;
38}
39
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053040static int cadence_spi_read_id(void *reg_base, u8 len, u8 *idcode)
41{
42 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
43 SPI_MEM_OP_NO_ADDR,
44 SPI_MEM_OP_NO_DUMMY,
45 SPI_MEM_OP_DATA_IN(len, idcode, 1));
46
47 return cadence_qspi_apb_command_read(reg_base, &op);
48}
49
Stefan Roese10e8bf82014-11-07 12:37:49 +010050/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050051static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010052{
Stefan Roese10e8bf82014-11-07 12:37:49 +010053 struct cadence_spi_priv *priv = dev_get_priv(bus);
54 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +010055 unsigned int idcode = 0, temp = 0;
56 int err = 0, i, range_lo = -1, range_hi = -1;
57
58 /* start with slowest clock (1 MHz) */
59 cadence_spi_write_speed(bus, 1000000);
60
61 /* configure the read data capture delay register to 0 */
62 cadence_qspi_apb_readdata_capture(base, 1, 0);
63
64 /* Enable QSPI */
65 cadence_qspi_apb_controller_enable(base);
66
67 /* read the ID which will be our golden value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053068 err = cadence_spi_read_id(base, 3, (u8 *)&idcode);
Stefan Roese10e8bf82014-11-07 12:37:49 +010069 if (err) {
70 puts("SF: Calibration failed (read)\n");
71 return err;
72 }
73
74 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050075 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010076 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
77 /* Disable QSPI */
78 cadence_qspi_apb_controller_disable(base);
79
80 /* reconfigure the read data capture delay register */
81 cadence_qspi_apb_readdata_capture(base, 1, i);
82
83 /* Enable back QSPI */
84 cadence_qspi_apb_controller_enable(base);
85
86 /* issue a RDID to get the ID value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053087 err = cadence_spi_read_id(base, 3, (u8 *)&temp);
Stefan Roese10e8bf82014-11-07 12:37:49 +010088 if (err) {
89 puts("SF: Calibration failed (read)\n");
90 return err;
91 }
92
93 /* search for range lo */
94 if (range_lo == -1 && temp == idcode) {
95 range_lo = i;
96 continue;
97 }
98
99 /* search for range hi */
100 if (range_lo != -1 && temp != idcode) {
101 range_hi = i - 1;
102 break;
103 }
104 range_hi = i;
105 }
106
107 if (range_lo == -1) {
108 puts("SF: Calibration failed (low range)\n");
109 return err;
110 }
111
112 /* Disable QSPI for subsequent initialization */
113 cadence_qspi_apb_controller_disable(base);
114
115 /* configure the final value for read data capture delay register */
116 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
117 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
118 (range_hi + range_lo) / 2, range_lo, range_hi);
119
120 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500121 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100122 priv->qspi_calibrated_cs = spi_chip_select(bus);
123
124 return 0;
125}
126
127static int cadence_spi_set_speed(struct udevice *bus, uint hz)
128{
129 struct cadence_spi_platdata *plat = bus->platdata;
130 struct cadence_spi_priv *priv = dev_get_priv(bus);
131 int err;
132
Chin Liang See4e609b62015-10-17 08:32:38 -0500133 if (hz > plat->max_hz)
134 hz = plat->max_hz;
135
Stefan Roese10e8bf82014-11-07 12:37:49 +0100136 /* Disable QSPI */
137 cadence_qspi_apb_controller_disable(priv->regbase);
138
Chin Liang See98fbd712015-10-17 08:31:55 -0500139 /*
140 * Calibration required for different current SCLK speed, requested
141 * SCLK speed or chip select
142 */
143 if (priv->previous_hz != hz ||
144 priv->qspi_calibrated_hz != hz ||
Stefan Roese10e8bf82014-11-07 12:37:49 +0100145 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
Chin Liang See98fbd712015-10-17 08:31:55 -0500146 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100147 if (err)
148 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500149
150 /* prevent calibration run when same as previous request */
151 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100152 }
153
154 /* Enable QSPI */
155 cadence_qspi_apb_controller_enable(priv->regbase);
156
157 debug("%s: speed=%d\n", __func__, hz);
158
159 return 0;
160}
161
162static int cadence_spi_probe(struct udevice *bus)
163{
164 struct cadence_spi_platdata *plat = bus->platdata;
165 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100166 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100167
168 priv->regbase = plat->regbase;
169 priv->ahbbase = plat->ahbbase;
170
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100171 ret = reset_get_bulk(bus, &priv->resets);
172 if (ret)
173 dev_warn(bus, "Can't get reset: %d\n", ret);
174 else
175 reset_deassert_bulk(&priv->resets);
176
Stefan Roese10e8bf82014-11-07 12:37:49 +0100177 if (!priv->qspi_is_init) {
178 cadence_qspi_apb_controller_init(plat);
179 priv->qspi_is_init = 1;
180 }
181
182 return 0;
183}
184
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100185static int cadence_spi_remove(struct udevice *dev)
186{
187 struct cadence_spi_priv *priv = dev_get_priv(dev);
188
189 return reset_release_bulk(&priv->resets);
190}
191
Stefan Roese10e8bf82014-11-07 12:37:49 +0100192static int cadence_spi_set_mode(struct udevice *bus, uint mode)
193{
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530194 struct cadence_spi_platdata *plat = bus->platdata;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100195 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100196
197 /* Disable QSPI */
198 cadence_qspi_apb_controller_disable(priv->regbase);
199
200 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000201 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100202
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530203 /* Enable Direct Access Controller */
204 if (plat->use_dac_mode)
205 cadence_qspi_apb_dac_mode_enable(priv->regbase);
206
Stefan Roese10e8bf82014-11-07 12:37:49 +0100207 /* Enable QSPI */
208 cadence_qspi_apb_controller_enable(priv->regbase);
209
210 return 0;
211}
212
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530213static int cadence_spi_mem_exec_op(struct spi_slave *spi,
214 const struct spi_mem_op *op)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100215{
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530216 struct udevice *bus = spi->dev->parent;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100217 struct cadence_spi_platdata *plat = bus->platdata;
218 struct cadence_spi_priv *priv = dev_get_priv(bus);
219 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100220 int err = 0;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530221 u32 mode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100222
223 /* Set Chip select */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530224 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600225 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100226
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530227 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
228 if (!op->addr.nbytes)
229 mode = CQSPI_STIG_READ;
230 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530231 mode = CQSPI_READ;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530232 } else {
233 if (!op->addr.nbytes || !op->data.buf.out)
234 mode = CQSPI_STIG_WRITE;
235 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530236 mode = CQSPI_WRITE;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530237 }
Stefan Roese10e8bf82014-11-07 12:37:49 +0100238
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530239 switch (mode) {
240 case CQSPI_STIG_READ:
241 err = cadence_qspi_apb_command_read(base, op);
242 break;
243 case CQSPI_STIG_WRITE:
244 err = cadence_qspi_apb_command_write(base, op);
245 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530246 case CQSPI_READ:
247 err = cadence_qspi_apb_read_setup(plat, op);
248 if (!err)
249 err = cadence_qspi_apb_read_execute(plat, op);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100250 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530251 case CQSPI_WRITE:
252 err = cadence_qspi_apb_write_setup(plat, op);
253 if (!err)
254 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530255 break;
256 default:
257 err = -1;
258 break;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100259 }
260
261 return err;
262}
263
264static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
265{
266 struct cadence_spi_platdata *plat = bus->platdata;
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200267 ofnode subnode;
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100268 struct clk clk;
269 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100270
Ley Foon Tan6c353672018-05-07 17:42:55 +0800271 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530272 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
273 &plat->ahbsize);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200274 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
275 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
276 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
277 plat->trigger_address = dev_read_u32_default(bus,
278 "cdns,trigger-address",
279 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530280 /* Use DAC mode only when MMIO window is at least 8M wide */
281 if (plat->ahbsize >= SZ_8M)
282 plat->use_dac_mode = true;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100283
Stefan Roese10e8bf82014-11-07 12:37:49 +0100284 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200285 subnode = dev_read_first_subnode(bus);
286 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100287 printf("Error: subnode with SPI flash config missing!\n");
288 return -ENODEV;
289 }
290
Chin Liang See040f4ba2015-10-17 08:32:14 -0500291 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200292 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
293 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500294
Stefan Roese10e8bf82014-11-07 12:37:49 +0100295 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200296 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
297 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
298 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
299 200);
300 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
301 255);
302 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
303 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100304
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100305 ret = clk_get_by_index(bus, 0, &clk);
306 if (ret) {
307#ifdef CONFIG_CQSPI_REF_CLK
308 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
309#else
310 return ret;
311#endif
312 } else {
313 plat->ref_clk_hz = clk_get_rate(&clk);
314 clk_free(&clk);
315 if (IS_ERR_VALUE(plat->ref_clk_hz))
316 return plat->ref_clk_hz;
317 }
318
Stefan Roese10e8bf82014-11-07 12:37:49 +0100319 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
320 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
321 plat->page_size);
322
323 return 0;
324}
325
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530326static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
327 .exec_op = cadence_spi_mem_exec_op,
328};
329
Stefan Roese10e8bf82014-11-07 12:37:49 +0100330static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100331 .set_speed = cadence_spi_set_speed,
332 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530333 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100334 /*
335 * cs_info is not needed, since we require all chip selects to be
336 * in the device tree explicitly
337 */
338};
339
340static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100341 { .compatible = "cdns,qspi-nor" },
Stefan Roese10e8bf82014-11-07 12:37:49 +0100342 { }
343};
344
345U_BOOT_DRIVER(cadence_spi) = {
346 .name = "cadence_spi",
347 .id = UCLASS_SPI,
348 .of_match = cadence_spi_ids,
349 .ops = &cadence_spi_ops,
350 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
351 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
352 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
Stefan Roese10e8bf82014-11-07 12:37:49 +0100353 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100354 .remove = cadence_spi_remove,
355 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100356};