blob: 0b503dd934ca6d692d841e7d2432969d290ff321 [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>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090017#include <linux/errno.h>
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053018#include <linux/sizes.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010019#include "cadence_qspi.h"
20
21#define CQSPI_STIG_READ 0
22#define CQSPI_STIG_WRITE 1
Vignesh Raghavendraffab2122020-01-27 10:36:40 +053023#define CQSPI_READ 2
24#define CQSPI_WRITE 3
Stefan Roese10e8bf82014-11-07 12:37:49 +010025
Stefan Roese10e8bf82014-11-07 12:37:49 +010026static int cadence_spi_write_speed(struct udevice *bus, uint hz)
27{
28 struct cadence_spi_platdata *plat = bus->platdata;
29 struct cadence_spi_priv *priv = dev_get_priv(bus);
30
31 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010032 plat->ref_clk_hz, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010033
34 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +010035 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese10e8bf82014-11-07 12:37:49 +010036 plat->tshsl_ns, plat->tsd2d_ns,
37 plat->tchsh_ns, plat->tslch_ns);
38
39 return 0;
40}
41
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053042static int cadence_spi_read_id(void *reg_base, u8 len, u8 *idcode)
43{
44 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
45 SPI_MEM_OP_NO_ADDR,
46 SPI_MEM_OP_NO_DUMMY,
47 SPI_MEM_OP_DATA_IN(len, idcode, 1));
48
49 return cadence_qspi_apb_command_read(reg_base, &op);
50}
51
Stefan Roese10e8bf82014-11-07 12:37:49 +010052/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050053static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010054{
Stefan Roese10e8bf82014-11-07 12:37:49 +010055 struct cadence_spi_priv *priv = dev_get_priv(bus);
56 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +010057 unsigned int idcode = 0, temp = 0;
58 int err = 0, i, range_lo = -1, range_hi = -1;
59
60 /* start with slowest clock (1 MHz) */
61 cadence_spi_write_speed(bus, 1000000);
62
63 /* configure the read data capture delay register to 0 */
64 cadence_qspi_apb_readdata_capture(base, 1, 0);
65
66 /* Enable QSPI */
67 cadence_qspi_apb_controller_enable(base);
68
69 /* read the ID which will be our golden value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053070 err = cadence_spi_read_id(base, 3, (u8 *)&idcode);
Stefan Roese10e8bf82014-11-07 12:37:49 +010071 if (err) {
72 puts("SF: Calibration failed (read)\n");
73 return err;
74 }
75
76 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050077 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010078 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
79 /* Disable QSPI */
80 cadence_qspi_apb_controller_disable(base);
81
82 /* reconfigure the read data capture delay register */
83 cadence_qspi_apb_readdata_capture(base, 1, i);
84
85 /* Enable back QSPI */
86 cadence_qspi_apb_controller_enable(base);
87
88 /* issue a RDID to get the ID value */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +053089 err = cadence_spi_read_id(base, 3, (u8 *)&temp);
Stefan Roese10e8bf82014-11-07 12:37:49 +010090 if (err) {
91 puts("SF: Calibration failed (read)\n");
92 return err;
93 }
94
95 /* search for range lo */
96 if (range_lo == -1 && temp == idcode) {
97 range_lo = i;
98 continue;
99 }
100
101 /* search for range hi */
102 if (range_lo != -1 && temp != idcode) {
103 range_hi = i - 1;
104 break;
105 }
106 range_hi = i;
107 }
108
109 if (range_lo == -1) {
110 puts("SF: Calibration failed (low range)\n");
111 return err;
112 }
113
114 /* Disable QSPI for subsequent initialization */
115 cadence_qspi_apb_controller_disable(base);
116
117 /* configure the final value for read data capture delay register */
118 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
119 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
120 (range_hi + range_lo) / 2, range_lo, range_hi);
121
122 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500123 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100124 priv->qspi_calibrated_cs = spi_chip_select(bus);
125
126 return 0;
127}
128
129static int cadence_spi_set_speed(struct udevice *bus, uint hz)
130{
131 struct cadence_spi_platdata *plat = bus->platdata;
132 struct cadence_spi_priv *priv = dev_get_priv(bus);
133 int err;
134
Chin Liang See4e609b62015-10-17 08:32:38 -0500135 if (hz > plat->max_hz)
136 hz = plat->max_hz;
137
Stefan Roese10e8bf82014-11-07 12:37:49 +0100138 /* Disable QSPI */
139 cadence_qspi_apb_controller_disable(priv->regbase);
140
Chin Liang See98fbd712015-10-17 08:31:55 -0500141 /*
142 * Calibration required for different current SCLK speed, requested
143 * SCLK speed or chip select
144 */
145 if (priv->previous_hz != hz ||
146 priv->qspi_calibrated_hz != hz ||
Stefan Roese10e8bf82014-11-07 12:37:49 +0100147 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
Chin Liang See98fbd712015-10-17 08:31:55 -0500148 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100149 if (err)
150 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500151
152 /* prevent calibration run when same as previous request */
153 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100154 }
155
156 /* Enable QSPI */
157 cadence_qspi_apb_controller_enable(priv->regbase);
158
159 debug("%s: speed=%d\n", __func__, hz);
160
161 return 0;
162}
163
164static int cadence_spi_probe(struct udevice *bus)
165{
166 struct cadence_spi_platdata *plat = bus->platdata;
167 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100168 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100169
170 priv->regbase = plat->regbase;
171 priv->ahbbase = plat->ahbbase;
172
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100173 ret = reset_get_bulk(bus, &priv->resets);
174 if (ret)
175 dev_warn(bus, "Can't get reset: %d\n", ret);
176 else
177 reset_deassert_bulk(&priv->resets);
178
Stefan Roese10e8bf82014-11-07 12:37:49 +0100179 if (!priv->qspi_is_init) {
180 cadence_qspi_apb_controller_init(plat);
181 priv->qspi_is_init = 1;
182 }
183
184 return 0;
185}
186
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100187static int cadence_spi_remove(struct udevice *dev)
188{
189 struct cadence_spi_priv *priv = dev_get_priv(dev);
190
191 return reset_release_bulk(&priv->resets);
192}
193
Stefan Roese10e8bf82014-11-07 12:37:49 +0100194static int cadence_spi_set_mode(struct udevice *bus, uint mode)
195{
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530196 struct cadence_spi_platdata *plat = bus->platdata;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100197 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100198
199 /* Disable QSPI */
200 cadence_qspi_apb_controller_disable(priv->regbase);
201
202 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000203 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100204
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530205 /* Enable Direct Access Controller */
206 if (plat->use_dac_mode)
207 cadence_qspi_apb_dac_mode_enable(priv->regbase);
208
Stefan Roese10e8bf82014-11-07 12:37:49 +0100209 /* Enable QSPI */
210 cadence_qspi_apb_controller_enable(priv->regbase);
211
212 return 0;
213}
214
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530215static int cadence_spi_mem_exec_op(struct spi_slave *spi,
216 const struct spi_mem_op *op)
Stefan Roese10e8bf82014-11-07 12:37:49 +0100217{
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530218 struct udevice *bus = spi->dev->parent;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100219 struct cadence_spi_platdata *plat = bus->platdata;
220 struct cadence_spi_priv *priv = dev_get_priv(bus);
221 void *base = priv->regbase;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100222 int err = 0;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530223 u32 mode;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100224
225 /* Set Chip select */
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530226 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600227 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100228
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530229 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
230 if (!op->addr.nbytes)
231 mode = CQSPI_STIG_READ;
232 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530233 mode = CQSPI_READ;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530234 } else {
235 if (!op->addr.nbytes || !op->data.buf.out)
236 mode = CQSPI_STIG_WRITE;
237 else
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530238 mode = CQSPI_WRITE;
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530239 }
Stefan Roese10e8bf82014-11-07 12:37:49 +0100240
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530241 switch (mode) {
242 case CQSPI_STIG_READ:
243 err = cadence_qspi_apb_command_read(base, op);
244 break;
245 case CQSPI_STIG_WRITE:
246 err = cadence_qspi_apb_command_write(base, op);
247 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530248 case CQSPI_READ:
249 err = cadence_qspi_apb_read_setup(plat, op);
250 if (!err)
251 err = cadence_qspi_apb_read_execute(plat, op);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100252 break;
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530253 case CQSPI_WRITE:
254 err = cadence_qspi_apb_write_setup(plat, op);
255 if (!err)
256 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530257 break;
258 default:
259 err = -1;
260 break;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100261 }
262
263 return err;
264}
265
266static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
267{
268 struct cadence_spi_platdata *plat = bus->platdata;
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200269 ofnode subnode;
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100270 struct clk clk;
271 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100272
Ley Foon Tan6c353672018-05-07 17:42:55 +0800273 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530274 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
275 &plat->ahbsize);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200276 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
277 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
278 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
279 plat->trigger_address = dev_read_u32_default(bus,
280 "cdns,trigger-address",
281 0);
Vignesh Raghavendraffab2122020-01-27 10:36:40 +0530282 /* Use DAC mode only when MMIO window is at least 8M wide */
283 if (plat->ahbsize >= SZ_8M)
284 plat->use_dac_mode = true;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100285
Stefan Roese10e8bf82014-11-07 12:37:49 +0100286 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200287 subnode = dev_read_first_subnode(bus);
288 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100289 printf("Error: subnode with SPI flash config missing!\n");
290 return -ENODEV;
291 }
292
Chin Liang See040f4ba2015-10-17 08:32:14 -0500293 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200294 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
295 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500296
Stefan Roese10e8bf82014-11-07 12:37:49 +0100297 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200298 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
299 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
300 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
301 200);
302 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
303 255);
304 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
305 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100306
Simon Goldschmidt64c7c8c2019-11-20 22:27:31 +0100307 ret = clk_get_by_index(bus, 0, &clk);
308 if (ret) {
309#ifdef CONFIG_CQSPI_REF_CLK
310 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
311#else
312 return ret;
313#endif
314 } else {
315 plat->ref_clk_hz = clk_get_rate(&clk);
316 clk_free(&clk);
317 if (IS_ERR_VALUE(plat->ref_clk_hz))
318 return plat->ref_clk_hz;
319 }
320
Stefan Roese10e8bf82014-11-07 12:37:49 +0100321 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
322 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
323 plat->page_size);
324
325 return 0;
326}
327
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530328static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
329 .exec_op = cadence_spi_mem_exec_op,
330};
331
Stefan Roese10e8bf82014-11-07 12:37:49 +0100332static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100333 .set_speed = cadence_spi_set_speed,
334 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendrad6407722020-01-27 10:36:39 +0530335 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100336 /*
337 * cs_info is not needed, since we require all chip selects to be
338 * in the device tree explicitly
339 */
340};
341
342static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100343 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendradaa94052019-12-05 15:46:07 +0530344 { .compatible = "ti,am654-ospi" },
Stefan Roese10e8bf82014-11-07 12:37:49 +0100345 { }
346};
347
348U_BOOT_DRIVER(cadence_spi) = {
349 .name = "cadence_spi",
350 .id = UCLASS_SPI,
351 .of_match = cadence_spi_ids,
352 .ops = &cadence_spi_ops,
353 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
354 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
355 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
Stefan Roese10e8bf82014-11-07 12:37:49 +0100356 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100357 .remove = cadence_spi_remove,
358 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100359};