blob: e2e54cd27723c313fb7b8f76f80e9e63e7b6b610 [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>
8#include <dm.h>
9#include <fdtdec.h>
10#include <malloc.h>
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +010011#include <reset.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010012#include <spi.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090013#include <linux/errno.h>
Stefan Roese10e8bf82014-11-07 12:37:49 +010014#include "cadence_qspi.h"
15
16#define CQSPI_STIG_READ 0
17#define CQSPI_STIG_WRITE 1
18#define CQSPI_INDIRECT_READ 2
19#define CQSPI_INDIRECT_WRITE 3
20
Stefan Roese10e8bf82014-11-07 12:37:49 +010021static int cadence_spi_write_speed(struct udevice *bus, uint hz)
22{
23 struct cadence_spi_platdata *plat = bus->platdata;
24 struct cadence_spi_priv *priv = dev_get_priv(bus);
25
26 cadence_qspi_apb_config_baudrate_div(priv->regbase,
27 CONFIG_CQSPI_REF_CLK, hz);
28
29 /* Reconfigure delay timing if speed is changed. */
30 cadence_qspi_apb_delay(priv->regbase, CONFIG_CQSPI_REF_CLK, hz,
31 plat->tshsl_ns, plat->tsd2d_ns,
32 plat->tchsh_ns, plat->tslch_ns);
33
34 return 0;
35}
36
37/* Calibration sequence to determine the read data capture delay register */
Chin Liang See98fbd712015-10-17 08:31:55 -050038static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese10e8bf82014-11-07 12:37:49 +010039{
Stefan Roese10e8bf82014-11-07 12:37:49 +010040 struct cadence_spi_priv *priv = dev_get_priv(bus);
41 void *base = priv->regbase;
42 u8 opcode_rdid = 0x9F;
43 unsigned int idcode = 0, temp = 0;
44 int err = 0, i, range_lo = -1, range_hi = -1;
45
46 /* start with slowest clock (1 MHz) */
47 cadence_spi_write_speed(bus, 1000000);
48
49 /* configure the read data capture delay register to 0 */
50 cadence_qspi_apb_readdata_capture(base, 1, 0);
51
52 /* Enable QSPI */
53 cadence_qspi_apb_controller_enable(base);
54
55 /* read the ID which will be our golden value */
56 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
57 3, (u8 *)&idcode);
58 if (err) {
59 puts("SF: Calibration failed (read)\n");
60 return err;
61 }
62
63 /* use back the intended clock and find low range */
Chin Liang See98fbd712015-10-17 08:31:55 -050064 cadence_spi_write_speed(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +010065 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
66 /* Disable QSPI */
67 cadence_qspi_apb_controller_disable(base);
68
69 /* reconfigure the read data capture delay register */
70 cadence_qspi_apb_readdata_capture(base, 1, i);
71
72 /* Enable back QSPI */
73 cadence_qspi_apb_controller_enable(base);
74
75 /* issue a RDID to get the ID value */
76 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
77 3, (u8 *)&temp);
78 if (err) {
79 puts("SF: Calibration failed (read)\n");
80 return err;
81 }
82
83 /* search for range lo */
84 if (range_lo == -1 && temp == idcode) {
85 range_lo = i;
86 continue;
87 }
88
89 /* search for range hi */
90 if (range_lo != -1 && temp != idcode) {
91 range_hi = i - 1;
92 break;
93 }
94 range_hi = i;
95 }
96
97 if (range_lo == -1) {
98 puts("SF: Calibration failed (low range)\n");
99 return err;
100 }
101
102 /* Disable QSPI for subsequent initialization */
103 cadence_qspi_apb_controller_disable(base);
104
105 /* configure the final value for read data capture delay register */
106 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
107 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
108 (range_hi + range_lo) / 2, range_lo, range_hi);
109
110 /* just to ensure we do once only when speed or chip select change */
Chin Liang See98fbd712015-10-17 08:31:55 -0500111 priv->qspi_calibrated_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100112 priv->qspi_calibrated_cs = spi_chip_select(bus);
113
114 return 0;
115}
116
117static int cadence_spi_set_speed(struct udevice *bus, uint hz)
118{
119 struct cadence_spi_platdata *plat = bus->platdata;
120 struct cadence_spi_priv *priv = dev_get_priv(bus);
121 int err;
122
Chin Liang See4e609b62015-10-17 08:32:38 -0500123 if (hz > plat->max_hz)
124 hz = plat->max_hz;
125
Stefan Roese10e8bf82014-11-07 12:37:49 +0100126 /* Disable QSPI */
127 cadence_qspi_apb_controller_disable(priv->regbase);
128
Chin Liang See98fbd712015-10-17 08:31:55 -0500129 /*
130 * Calibration required for different current SCLK speed, requested
131 * SCLK speed or chip select
132 */
133 if (priv->previous_hz != hz ||
134 priv->qspi_calibrated_hz != hz ||
Stefan Roese10e8bf82014-11-07 12:37:49 +0100135 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
Chin Liang See98fbd712015-10-17 08:31:55 -0500136 err = spi_calibration(bus, hz);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100137 if (err)
138 return err;
Chin Liang See98fbd712015-10-17 08:31:55 -0500139
140 /* prevent calibration run when same as previous request */
141 priv->previous_hz = hz;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100142 }
143
144 /* Enable QSPI */
145 cadence_qspi_apb_controller_enable(priv->regbase);
146
147 debug("%s: speed=%d\n", __func__, hz);
148
149 return 0;
150}
151
152static int cadence_spi_probe(struct udevice *bus)
153{
154 struct cadence_spi_platdata *plat = bus->platdata;
155 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100156 int ret;
Stefan Roese10e8bf82014-11-07 12:37:49 +0100157
158 priv->regbase = plat->regbase;
159 priv->ahbbase = plat->ahbbase;
160
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100161 ret = reset_get_bulk(bus, &priv->resets);
162 if (ret)
163 dev_warn(bus, "Can't get reset: %d\n", ret);
164 else
165 reset_deassert_bulk(&priv->resets);
166
Stefan Roese10e8bf82014-11-07 12:37:49 +0100167 if (!priv->qspi_is_init) {
168 cadence_qspi_apb_controller_init(plat);
169 priv->qspi_is_init = 1;
170 }
171
172 return 0;
173}
174
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100175static int cadence_spi_remove(struct udevice *dev)
176{
177 struct cadence_spi_priv *priv = dev_get_priv(dev);
178
179 return reset_release_bulk(&priv->resets);
180}
181
Stefan Roese10e8bf82014-11-07 12:37:49 +0100182static int cadence_spi_set_mode(struct udevice *bus, uint mode)
183{
184 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100185
186 /* Disable QSPI */
187 cadence_qspi_apb_controller_disable(priv->regbase);
188
189 /* Set SPI mode */
Phil Edworthy7d403f22016-11-29 12:58:31 +0000190 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100191
192 /* Enable QSPI */
193 cadence_qspi_apb_controller_enable(priv->regbase);
194
195 return 0;
196}
197
198static int cadence_spi_xfer(struct udevice *dev, unsigned int bitlen,
199 const void *dout, void *din, unsigned long flags)
200{
201 struct udevice *bus = dev->parent;
202 struct cadence_spi_platdata *plat = bus->platdata;
203 struct cadence_spi_priv *priv = dev_get_priv(bus);
Vignesh R2372e142016-07-06 10:20:56 +0530204 struct dm_spi_slave_platdata *dm_plat = dev_get_parent_platdata(dev);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100205 void *base = priv->regbase;
206 u8 *cmd_buf = priv->cmd_buf;
207 size_t data_bytes;
208 int err = 0;
209 u32 mode = CQSPI_STIG_WRITE;
210
211 if (flags & SPI_XFER_BEGIN) {
212 /* copy command to local buffer */
213 priv->cmd_len = bitlen / 8;
214 memcpy(cmd_buf, dout, priv->cmd_len);
215 }
216
217 if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
218 /* if start and end bit are set, the data bytes is 0. */
219 data_bytes = 0;
220 } else {
221 data_bytes = bitlen / 8;
222 }
Ley Foon Tan9bd39dd2018-07-06 10:39:14 +0800223 debug("%s: len=%zu [bytes]\n", __func__, data_bytes);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100224
225 /* Set Chip select */
226 cadence_qspi_apb_chipselect(base, spi_chip_select(dev),
Jason Rush15a70a52018-01-23 17:13:09 -0600227 plat->is_decoded_cs);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100228
229 if ((flags & SPI_XFER_END) || (flags == 0)) {
230 if (priv->cmd_len == 0) {
231 printf("QSPI: Error, command is empty.\n");
232 return -1;
233 }
234
235 if (din && data_bytes) {
236 /* read */
237 /* Use STIG if no address. */
238 if (!CQSPI_IS_ADDR(priv->cmd_len))
239 mode = CQSPI_STIG_READ;
240 else
241 mode = CQSPI_INDIRECT_READ;
242 } else if (dout && !(flags & SPI_XFER_BEGIN)) {
243 /* write */
244 if (!CQSPI_IS_ADDR(priv->cmd_len))
245 mode = CQSPI_STIG_WRITE;
246 else
247 mode = CQSPI_INDIRECT_WRITE;
248 }
249
250 switch (mode) {
251 case CQSPI_STIG_READ:
252 err = cadence_qspi_apb_command_read(
253 base, priv->cmd_len, cmd_buf,
254 data_bytes, din);
255
256 break;
257 case CQSPI_STIG_WRITE:
258 err = cadence_qspi_apb_command_write(base,
259 priv->cmd_len, cmd_buf,
260 data_bytes, dout);
261 break;
262 case CQSPI_INDIRECT_READ:
263 err = cadence_qspi_apb_indirect_read_setup(plat,
Jagan Teki08fe9c22016-08-08 17:12:12 +0530264 priv->cmd_len, dm_plat->mode, cmd_buf);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100265 if (!err) {
266 err = cadence_qspi_apb_indirect_read_execute
267 (plat, data_bytes, din);
268 }
269 break;
270 case CQSPI_INDIRECT_WRITE:
271 err = cadence_qspi_apb_indirect_write_setup
Ley Foon Tan7eece322019-02-27 13:36:14 +0800272 (plat, priv->cmd_len, dm_plat->mode, cmd_buf);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100273 if (!err) {
274 err = cadence_qspi_apb_indirect_write_execute
275 (plat, data_bytes, dout);
276 }
277 break;
278 default:
279 err = -1;
280 break;
281 }
282
283 if (flags & SPI_XFER_END) {
284 /* clear command buffer */
285 memset(cmd_buf, 0, sizeof(priv->cmd_buf));
286 priv->cmd_len = 0;
287 }
288 }
289
290 return err;
291}
292
293static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
294{
295 struct cadence_spi_platdata *plat = bus->platdata;
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);
299 plat->ahbbase = (void *)devfdt_get_addr_index(bus, 1);
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200300 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
301 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
302 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
303 plat->trigger_address = dev_read_u32_default(bus,
304 "cdns,trigger-address",
305 0);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100306
Stefan Roese10e8bf82014-11-07 12:37:49 +0100307 /* All other paramters are embedded in the child node */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200308 subnode = dev_read_first_subnode(bus);
309 if (!ofnode_valid(subnode)) {
Stefan Roese10e8bf82014-11-07 12:37:49 +0100310 printf("Error: subnode with SPI flash config missing!\n");
311 return -ENODEV;
312 }
313
Chin Liang See040f4ba2015-10-17 08:32:14 -0500314 /* Use 500 KHz as a suitable default */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200315 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
316 500000);
Chin Liang See040f4ba2015-10-17 08:32:14 -0500317
Stefan Roese10e8bf82014-11-07 12:37:49 +0100318 /* Read other parameters from DT */
Simon Goldschmidt46b633d2019-05-09 22:11:56 +0200319 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
320 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
321 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
322 200);
323 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
324 255);
325 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
326 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Stefan Roese10e8bf82014-11-07 12:37:49 +0100327
328 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
329 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
330 plat->page_size);
331
332 return 0;
333}
334
335static const struct dm_spi_ops cadence_spi_ops = {
336 .xfer = cadence_spi_xfer,
337 .set_speed = cadence_spi_set_speed,
338 .set_mode = cadence_spi_set_mode,
339 /*
340 * cs_info is not needed, since we require all chip selects to be
341 * in the device tree explicitly
342 */
343};
344
345static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt2a3a9992018-11-02 11:54:51 +0100346 { .compatible = "cdns,qspi-nor" },
Stefan Roese10e8bf82014-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 Roese10e8bf82014-11-07 12:37:49 +0100358 .probe = cadence_spi_probe,
Simon Goldschmidtac7e14a2019-03-01 20:12:35 +0100359 .remove = cadence_spi_remove,
360 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese10e8bf82014-11-07 12:37:49 +0100361};