blob: 994a5948f1ee99568d17031f1e57b7a78749a00d [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese1c60fe72014-11-07 12:37:49 +01002/*
3 * Copyright (C) 2012
4 * Altera Corporation <www.altera.com>
Stefan Roese1c60fe72014-11-07 12:37:49 +01005 */
6
7#include <common.h>
Simon Goldschmidtbaaa3fc2019-11-20 22:27:31 +01008#include <clk.h>
Vignesh Raghavendra68f82662019-12-05 15:46:06 +05309#include <asm-generic/io.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010010#include <dm.h>
11#include <fdtdec.h>
12#include <malloc.h>
Simon Goldschmidt46e56a42019-03-01 20:12:35 +010013#include <reset.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010014#include <spi.h>
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053015#include <spi-mem.h>
Simon Glass9bc15642020-02-03 07:36:16 -070016#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090018#include <linux/errno.h>
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053019#include <linux/sizes.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010020#include "cadence_qspi.h"
21
22#define CQSPI_STIG_READ 0
23#define CQSPI_STIG_WRITE 1
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053024#define CQSPI_READ 2
25#define CQSPI_WRITE 3
Stefan Roese1c60fe72014-11-07 12:37:49 +010026
Stefan Roese1c60fe72014-11-07 12:37:49 +010027static int cadence_spi_write_speed(struct udevice *bus, uint hz)
28{
29 struct cadence_spi_platdata *plat = bus->platdata;
30 struct cadence_spi_priv *priv = dev_get_priv(bus);
31
32 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Simon Goldschmidtbaaa3fc2019-11-20 22:27:31 +010033 plat->ref_clk_hz, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +010034
35 /* Reconfigure delay timing if speed is changed. */
Simon Goldschmidtbaaa3fc2019-11-20 22:27:31 +010036 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
Stefan Roese1c60fe72014-11-07 12:37:49 +010037 plat->tshsl_ns, plat->tsd2d_ns,
38 plat->tchsh_ns, plat->tslch_ns);
39
40 return 0;
41}
42
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053043static int cadence_spi_read_id(void *reg_base, u8 len, u8 *idcode)
44{
45 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
46 SPI_MEM_OP_NO_ADDR,
47 SPI_MEM_OP_NO_DUMMY,
48 SPI_MEM_OP_DATA_IN(len, idcode, 1));
49
50 return cadence_qspi_apb_command_read(reg_base, &op);
51}
52
Stefan Roese1c60fe72014-11-07 12:37:49 +010053/* Calibration sequence to determine the read data capture delay register */
Chin Liang See36431f92015-10-17 08:31:55 -050054static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese1c60fe72014-11-07 12:37:49 +010055{
Stefan Roese1c60fe72014-11-07 12:37:49 +010056 struct cadence_spi_priv *priv = dev_get_priv(bus);
57 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +010058 unsigned int idcode = 0, temp = 0;
59 int err = 0, i, range_lo = -1, range_hi = -1;
60
61 /* start with slowest clock (1 MHz) */
62 cadence_spi_write_speed(bus, 1000000);
63
64 /* configure the read data capture delay register to 0 */
65 cadence_qspi_apb_readdata_capture(base, 1, 0);
66
67 /* Enable QSPI */
68 cadence_qspi_apb_controller_enable(base);
69
70 /* read the ID which will be our golden value */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053071 err = cadence_spi_read_id(base, 3, (u8 *)&idcode);
Stefan Roese1c60fe72014-11-07 12:37:49 +010072 if (err) {
73 puts("SF: Calibration failed (read)\n");
74 return err;
75 }
76
77 /* use back the intended clock and find low range */
Chin Liang See36431f92015-10-17 08:31:55 -050078 cadence_spi_write_speed(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +010079 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
80 /* Disable QSPI */
81 cadence_qspi_apb_controller_disable(base);
82
83 /* reconfigure the read data capture delay register */
84 cadence_qspi_apb_readdata_capture(base, 1, i);
85
86 /* Enable back QSPI */
87 cadence_qspi_apb_controller_enable(base);
88
89 /* issue a RDID to get the ID value */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053090 err = cadence_spi_read_id(base, 3, (u8 *)&temp);
Stefan Roese1c60fe72014-11-07 12:37:49 +010091 if (err) {
92 puts("SF: Calibration failed (read)\n");
93 return err;
94 }
95
96 /* search for range lo */
97 if (range_lo == -1 && temp == idcode) {
98 range_lo = i;
99 continue;
100 }
101
102 /* search for range hi */
103 if (range_lo != -1 && temp != idcode) {
104 range_hi = i - 1;
105 break;
106 }
107 range_hi = i;
108 }
109
110 if (range_lo == -1) {
111 puts("SF: Calibration failed (low range)\n");
112 return err;
113 }
114
115 /* Disable QSPI for subsequent initialization */
116 cadence_qspi_apb_controller_disable(base);
117
118 /* configure the final value for read data capture delay register */
119 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
120 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
121 (range_hi + range_lo) / 2, range_lo, range_hi);
122
123 /* just to ensure we do once only when speed or chip select change */
Chin Liang See36431f92015-10-17 08:31:55 -0500124 priv->qspi_calibrated_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100125 priv->qspi_calibrated_cs = spi_chip_select(bus);
126
127 return 0;
128}
129
130static int cadence_spi_set_speed(struct udevice *bus, uint hz)
131{
132 struct cadence_spi_platdata *plat = bus->platdata;
133 struct cadence_spi_priv *priv = dev_get_priv(bus);
134 int err;
135
Chin Liang Seecb4ac0b2015-10-17 08:32:38 -0500136 if (hz > plat->max_hz)
137 hz = plat->max_hz;
138
Stefan Roese1c60fe72014-11-07 12:37:49 +0100139 /* Disable QSPI */
140 cadence_qspi_apb_controller_disable(priv->regbase);
141
Chin Liang See36431f92015-10-17 08:31:55 -0500142 /*
143 * Calibration required for different current SCLK speed, requested
144 * SCLK speed or chip select
145 */
146 if (priv->previous_hz != hz ||
147 priv->qspi_calibrated_hz != hz ||
Stefan Roese1c60fe72014-11-07 12:37:49 +0100148 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
Chin Liang See36431f92015-10-17 08:31:55 -0500149 err = spi_calibration(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100150 if (err)
151 return err;
Chin Liang See36431f92015-10-17 08:31:55 -0500152
153 /* prevent calibration run when same as previous request */
154 priv->previous_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100155 }
156
157 /* Enable QSPI */
158 cadence_qspi_apb_controller_enable(priv->regbase);
159
160 debug("%s: speed=%d\n", __func__, hz);
161
162 return 0;
163}
164
165static int cadence_spi_probe(struct udevice *bus)
166{
167 struct cadence_spi_platdata *plat = bus->platdata;
168 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530169 struct clk clk;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100170 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100171
172 priv->regbase = plat->regbase;
173 priv->ahbbase = plat->ahbbase;
174
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530175 if (plat->ref_clk_hz == 0) {
176 ret = clk_get_by_index(bus, 0, &clk);
177 if (ret) {
178#ifdef CONFIG_CQSPI_REF_CLK
179 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
180#else
181 return ret;
182#endif
183 } else {
184 plat->ref_clk_hz = clk_get_rate(&clk);
185 clk_free(&clk);
186 if (IS_ERR_VALUE(plat->ref_clk_hz))
187 return plat->ref_clk_hz;
188 }
189 }
190
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100191 ret = reset_get_bulk(bus, &priv->resets);
192 if (ret)
193 dev_warn(bus, "Can't get reset: %d\n", ret);
194 else
195 reset_deassert_bulk(&priv->resets);
196
Stefan Roese1c60fe72014-11-07 12:37:49 +0100197 if (!priv->qspi_is_init) {
198 cadence_qspi_apb_controller_init(plat);
199 priv->qspi_is_init = 1;
200 }
201
202 return 0;
203}
204
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100205static int cadence_spi_remove(struct udevice *dev)
206{
207 struct cadence_spi_priv *priv = dev_get_priv(dev);
208
209 return reset_release_bulk(&priv->resets);
210}
211
Stefan Roese1c60fe72014-11-07 12:37:49 +0100212static int cadence_spi_set_mode(struct udevice *bus, uint mode)
213{
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530214 struct cadence_spi_platdata *plat = bus->platdata;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100215 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100216
217 /* Disable QSPI */
218 cadence_qspi_apb_controller_disable(priv->regbase);
219
220 /* Set SPI mode */
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000221 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100222
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530223 /* Enable Direct Access Controller */
224 if (plat->use_dac_mode)
225 cadence_qspi_apb_dac_mode_enable(priv->regbase);
226
Stefan Roese1c60fe72014-11-07 12:37:49 +0100227 /* Enable QSPI */
228 cadence_qspi_apb_controller_enable(priv->regbase);
229
230 return 0;
231}
232
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530233static int cadence_spi_mem_exec_op(struct spi_slave *spi,
234 const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100235{
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530236 struct udevice *bus = spi->dev->parent;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100237 struct cadence_spi_platdata *plat = bus->platdata;
238 struct cadence_spi_priv *priv = dev_get_priv(bus);
239 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100240 int err = 0;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530241 u32 mode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100242
243 /* Set Chip select */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530244 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Jason Rush1b4df5e2018-01-23 17:13:09 -0600245 plat->is_decoded_cs);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100246
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530247 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
248 if (!op->addr.nbytes)
249 mode = CQSPI_STIG_READ;
250 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530251 mode = CQSPI_READ;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530252 } else {
253 if (!op->addr.nbytes || !op->data.buf.out)
254 mode = CQSPI_STIG_WRITE;
255 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530256 mode = CQSPI_WRITE;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530257 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100258
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530259 switch (mode) {
260 case CQSPI_STIG_READ:
261 err = cadence_qspi_apb_command_read(base, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100262 break;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530263 case CQSPI_STIG_WRITE:
264 err = cadence_qspi_apb_command_write(base, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100265 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530266 case CQSPI_READ:
267 err = cadence_qspi_apb_read_setup(plat, op);
268 if (!err)
269 err = cadence_qspi_apb_read_execute(plat, op);
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530270 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530271 case CQSPI_WRITE:
272 err = cadence_qspi_apb_write_setup(plat, op);
273 if (!err)
274 err = cadence_qspi_apb_write_execute(plat, op);
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530275 break;
276 default:
277 err = -1;
278 break;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100279 }
280
281 return err;
282}
283
284static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
285{
286 struct cadence_spi_platdata *plat = bus->platdata;
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200287 ofnode subnode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100288
Ley Foon Tan3bca8f52018-05-07 17:42:55 +0800289 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530290 plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1,
291 &plat->ahbsize);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200292 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
293 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
294 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
295 plat->trigger_address = dev_read_u32_default(bus,
296 "cdns,trigger-address",
297 0);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530298 /* Use DAC mode only when MMIO window is at least 8M wide */
299 if (plat->ahbsize >= SZ_8M)
300 plat->use_dac_mode = true;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100301
Stefan Roese1c60fe72014-11-07 12:37:49 +0100302 /* All other paramters are embedded in the child node */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200303 subnode = dev_read_first_subnode(bus);
304 if (!ofnode_valid(subnode)) {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100305 printf("Error: subnode with SPI flash config missing!\n");
306 return -ENODEV;
307 }
308
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500309 /* Use 500 KHz as a suitable default */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200310 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
311 500000);
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500312
Stefan Roese1c60fe72014-11-07 12:37:49 +0100313 /* Read other parameters from DT */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200314 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
315 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
316 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
317 200);
318 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
319 255);
320 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
321 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100322
323 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
324 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
325 plat->page_size);
326
327 return 0;
328}
329
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530330static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
331 .exec_op = cadence_spi_mem_exec_op,
332};
333
Stefan Roese1c60fe72014-11-07 12:37:49 +0100334static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100335 .set_speed = cadence_spi_set_speed,
336 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530337 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100338 /*
339 * cs_info is not needed, since we require all chip selects to be
340 * in the device tree explicitly
341 */
342};
343
344static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt454c9b32018-11-02 11:54:51 +0100345 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendra99276f02019-12-05 15:46:07 +0530346 { .compatible = "ti,am654-ospi" },
Stefan Roese1c60fe72014-11-07 12:37:49 +0100347 { }
348};
349
350U_BOOT_DRIVER(cadence_spi) = {
351 .name = "cadence_spi",
352 .id = UCLASS_SPI,
353 .of_match = cadence_spi_ids,
354 .ops = &cadence_spi_ops,
355 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
356 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
357 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
Stefan Roese1c60fe72014-11-07 12:37:49 +0100358 .probe = cadence_spi_probe,
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100359 .remove = cadence_spi_remove,
360 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100361};