blob: 6374d3976a4a4eb40a12ab1e7c5abf689ac2a6cf [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>
Vignesh Raghavendra0f247842019-12-05 15:46:06 +05309#include <asm-generic/io.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010010#include <dm.h>
11#include <fdtdec.h>
12#include <malloc.h>
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +010013#include <reset.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010014#include <spi.h>
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053015#include <spi-mem.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090016#include <linux/errno.h>
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053017#include <linux/sizes.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010018#include "cadence_qspi.h"
19
20#define CQSPI_STIG_READ 0
21#define CQSPI_STIG_WRITE 1
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053022#define CQSPI_READ 2
23#define CQSPI_WRITE 3
Stefan Roese10e8bf82014-11-07 12:37:49 +010024
Stefan Roese10e8bf82014-11-07 12:37:49 +010025static int cadence_spi_write_speed(struct udevice *bus, uint hz)
26{
27 struct cadence_spi_platdata *plat = bus->platdata;
28 struct cadence_spi_priv *priv = dev_get_priv(bus);
29
30 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010031 plat->ref_clk_hz, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010032
33 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010034 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese10e8bf82014-11-07 12:37:49 +010035 plat->tshsl_ns, plat->tsd2d_ns,
36 plat->tchsh_ns, plat->tslch_ns);
37
38 return 0;
39}
40
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053041static int cadence_spi_read_id(void *reg_base, u8 len, u8 *idcode)
42{
43 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
44 SPI_MEM_OP_NO_ADDR,
45 SPI_MEM_OP_NO_DUMMY,
46 SPI_MEM_OP_DATA_IN(len, idcode, 1));
47
48 return cadence_qspi_apb_command_read(reg_base, &op);
49}
50
Stefan Roese10e8bf82014-11-07 12:37:49 +010051/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050052static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010053{
Stefan Roese10e8bf82014-11-07 12:37:49 +010054 struct cadence_spi_priv *priv = dev_get_priv(bus);
55 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +010056 unsigned int idcode = 0, temp = 0;
57 int err = 0, i, range_lo = -1, range_hi = -1;
58
59 /* start with slowest clock (1 MHz) */
60 cadence_spi_write_speed(bus, 1000000);
61
62 /* configure the read data capture delay register to 0 */
63 cadence_qspi_apb_readdata_capture(base, 1, 0);
64
65 /* Enable QSPI */
66 cadence_qspi_apb_controller_enable(base);
67
68 /* read the ID which will be our golden value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053069 err = cadence_spi_read_id(base, 3, (u8 *)&idcode);
Stefan Roese10e8bf82014-11-07 12:37:49 +010070 if (err) {
71 puts("SF: Calibration failed (read)\n");
72 return err;
73 }
74
75 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050076 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010077 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
78 /* Disable QSPI */
79 cadence_qspi_apb_controller_disable(base);
80
81 /* reconfigure the read data capture delay register */
82 cadence_qspi_apb_readdata_capture(base, 1, i);
83
84 /* Enable back QSPI */
85 cadence_qspi_apb_controller_enable(base);
86
87 /* issue a RDID to get the ID value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053088 err = cadence_spi_read_id(base, 3, (u8 *)&temp);
Stefan Roese10e8bf82014-11-07 12:37:49 +010089 if (err) {
90 puts("SF: Calibration failed (read)\n");
91 return err;
92 }
93
94 /* search for range lo */
95 if (range_lo == -1 && temp == idcode) {
96 range_lo = i;
97 continue;
98 }
99
100 /* search for range hi */
101 if (range_lo != -1 && temp != idcode) {
102 range_hi = i - 1;
103 break;
104 }
105 range_hi = i;
106 }
107
108 if (range_lo == -1) {
109 puts("SF: Calibration failed (low range)\n");
110 return err;
111 }
112
113 /* Disable QSPI for subsequent initialization */
114 cadence_qspi_apb_controller_disable(base);
115
116 /* configure the final value for read data capture delay register */
117 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
118 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
119 (range_hi + range_lo) / 2, range_lo, range_hi);
120
121 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500122 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100123 priv->qspi_calibrated_cs = spi_chip_select(bus);
124
125 return 0;
126}
127
128static int cadence_spi_set_speed(struct udevice *bus, uint hz)
129{
130 struct cadence_spi_platdata *plat = bus->platdata;
131 struct cadence_spi_priv *priv = dev_get_priv(bus);
132 int err;
133
Chin Liang See4e609b62015-10-17 08:32:38 -0500134 if (hz > plat->max_hz)
135 hz = plat->max_hz;
136
Stefan Roese10e8bf82014-11-07 12:37:49 +0100137 /* Disable QSPI */
138 cadence_qspi_apb_controller_disable(priv->regbase);
139
Chin Liang See98fbd712015-10-17 08:31:55 -0500140 /*
141 * Calibration required for different current SCLK speed, requested
142 * SCLK speed or chip select
143 */
144 if (priv->previous_hz != hz ||
145 priv->qspi_calibrated_hz != hz ||
Stefan Roese10e8bf82014-11-07 12:37:49 +0100146 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
Chin Liang See98fbd712015-10-17 08:31:55 -0500147 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100148 if (err)
149 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500150
151 /* prevent calibration run when same as previous request */
152 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100153 }
154
155 /* Enable QSPI */
156 cadence_qspi_apb_controller_enable(priv->regbase);
157
158 debug("%s: speed=%d\n", __func__, hz);
159
160 return 0;
161}
162
163static int cadence_spi_probe(struct udevice *bus)
164{
165 struct cadence_spi_platdata *plat = bus->platdata;
166 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100167 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100168
169 priv->regbase = plat->regbase;
170 priv->ahbbase = plat->ahbbase;
171
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100172 ret = reset_get_bulk(bus, &priv->resets);
173 if (ret)
174 dev_warn(bus, "Can't get reset: %d\n", ret);
175 else
176 reset_deassert_bulk(&priv->resets);
177
Stefan Roese10e8bf82014-11-07 12:37:49 +0100178 if (!priv->qspi_is_init) {
179 cadence_qspi_apb_controller_init(plat);
180 priv->qspi_is_init = 1;
181 }
182
183 return 0;
184}
185
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100186static int cadence_spi_remove(struct udevice *dev)
187{
188 struct cadence_spi_priv *priv = dev_get_priv(dev);
189
190 return reset_release_bulk(&priv->resets);
191}
192
Stefan Roese10e8bf82014-11-07 12:37:49 +0100193static int cadence_spi_set_mode(struct udevice *bus, uint mode)
194{
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530195 struct cadence_spi_platdata *plat = bus->platdata;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100196 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100197
198 /* Disable QSPI */
199 cadence_qspi_apb_controller_disable(priv->regbase);
200
201 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000202 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100203
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530204 /* Enable Direct Access Controller */
205 if (plat->use_dac_mode)
206 cadence_qspi_apb_dac_mode_enable(priv->regbase);
207
Stefan Roese10e8bf82014-11-07 12:37:49 +0100208 /* Enable QSPI */
209 cadence_qspi_apb_controller_enable(priv->regbase);
210
211 return 0;
212}
213
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530214static int cadence_spi_mem_exec_op(struct spi_slave *spi,
215 const struct spi_mem_op *op)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100216{
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530217 struct udevice *bus = spi->dev->parent;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100218 struct cadence_spi_platdata *plat = bus->platdata;
219 struct cadence_spi_priv *priv = dev_get_priv(bus);
220 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100221 int err = 0;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530222 u32 mode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100223
224 /* Set Chip select */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530225 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600226 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100227
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530228 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
229 if (!op->addr.nbytes)
230 mode = CQSPI_STIG_READ;
231 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530232 mode = CQSPI_READ;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530233 } else {
234 if (!op->addr.nbytes || !op->data.buf.out)
235 mode = CQSPI_STIG_WRITE;
236 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530237 mode = CQSPI_WRITE;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530238 }
Stefan Roese10e8bf82014-11-07 12:37:49 +0100239
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530240 switch (mode) {
241 case CQSPI_STIG_READ:
242 err = cadence_qspi_apb_command_read(base, op);
243 break;
244 case CQSPI_STIG_WRITE:
245 err = cadence_qspi_apb_command_write(base, op);
246 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530247 case CQSPI_READ:
248 err = cadence_qspi_apb_read_setup(plat, op);
249 if (!err)
250 err = cadence_qspi_apb_read_execute(plat, op);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100251 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530252 case CQSPI_WRITE:
253 err = cadence_qspi_apb_write_setup(plat, op);
254 if (!err)
255 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530256 break;
257 default:
258 err = -1;
259 break;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100260 }
261
262 return err;
263}
264
265static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
266{
267 struct cadence_spi_platdata *plat = bus->platdata;
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200268 ofnode subnode;
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100269 struct clk clk;
270 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100271
Ley Foon Tan6c353672018-05-07 17:42:55 +0800272 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530273 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
274 &plat->ahbsize);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200275 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
276 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
277 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
278 plat->trigger_address = dev_read_u32_default(bus,
279 "cdns,trigger-address",
280 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530281 /* Use DAC mode only when MMIO window is at least 8M wide */
282 if (plat->ahbsize >= SZ_8M)
283 plat->use_dac_mode = true;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100284
Stefan Roese10e8bf82014-11-07 12:37:49 +0100285 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200286 subnode = dev_read_first_subnode(bus);
287 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100288 printf("Error: subnode with SPI flash config missing!\n");
289 return -ENODEV;
290 }
291
Chin Liang See040f4ba2015-10-17 08:32:14 -0500292 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200293 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
294 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500295
Stefan Roese10e8bf82014-11-07 12:37:49 +0100296 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200297 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
298 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
299 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
300 200);
301 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
302 255);
303 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
304 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100305
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100306 ret = clk_get_by_index(bus, 0, &clk);
307 if (ret) {
308#ifdef CONFIG_CQSPI_REF_CLK
309 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
310#else
311 return ret;
312#endif
313 } else {
314 plat->ref_clk_hz = clk_get_rate(&clk);
315 clk_free(&clk);
316 if (IS_ERR_VALUE(plat->ref_clk_hz))
317 return plat->ref_clk_hz;
318 }
319
Stefan Roese10e8bf82014-11-07 12:37:49 +0100320 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
321 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
322 plat->page_size);
323
324 return 0;
325}
326
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530327static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
328 .exec_op = cadence_spi_mem_exec_op,
329};
330
Stefan Roese10e8bf82014-11-07 12:37:49 +0100331static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100332 .set_speed = cadence_spi_set_speed,
333 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530334 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100335 /*
336 * cs_info is not needed, since we require all chip selects to be
337 * in the device tree explicitly
338 */
339};
340
341static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100342 { .compatible = "cdns,qspi-nor" },
Stefan Roese10e8bf82014-11-07 12:37:49 +0100343 { }
344};
345
346U_BOOT_DRIVER(cadence_spi) = {
347 .name = "cadence_spi",
348 .id = UCLASS_SPI,
349 .of_match = cadence_spi_ids,
350 .ops = &cadence_spi_ops,
351 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
352 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
353 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
Stefan Roese10e8bf82014-11-07 12:37:49 +0100354 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100355 .remove = cadence_spi_remove,
356 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100357};