Blame view

drivers/spi/cadence_qspi.c 9.23 KB
10e8bf88c   Stefan Roese   spi: Add Cadence ...
1
2
3
4
5
6
7
8
9
10
11
12
  /*
   * Copyright (C) 2012
   * Altera Corporation <www.altera.com>
   *
   * SPDX-License-Identifier:	GPL-2.0+
   */
  
  #include <common.h>
  #include <dm.h>
  #include <fdtdec.h>
  #include <malloc.h>
  #include <spi.h>
1221ce459   Masahiro Yamada   treewide: replace...
13
  #include <linux/errno.h>
10e8bf88c   Stefan Roese   spi: Add Cadence ...
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
  #include "cadence_qspi.h"
  
  #define CQSPI_STIG_READ			0
  #define CQSPI_STIG_WRITE		1
  #define CQSPI_INDIRECT_READ		2
  #define CQSPI_INDIRECT_WRITE		3
  
  DECLARE_GLOBAL_DATA_PTR;
  
  static int cadence_spi_write_speed(struct udevice *bus, uint hz)
  {
  	struct cadence_spi_platdata *plat = bus->platdata;
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
  
  	cadence_qspi_apb_config_baudrate_div(priv->regbase,
  					     CONFIG_CQSPI_REF_CLK, hz);
  
  	/* Reconfigure delay timing if speed is changed. */
  	cadence_qspi_apb_delay(priv->regbase, CONFIG_CQSPI_REF_CLK, hz,
  			       plat->tshsl_ns, plat->tsd2d_ns,
  			       plat->tchsh_ns, plat->tslch_ns);
  
  	return 0;
  }
  
  /* Calibration sequence to determine the read data capture delay register */
98fbd71d7   Chin Liang See   spi: cadence_qspi...
40
  static int spi_calibration(struct udevice *bus, uint hz)
10e8bf88c   Stefan Roese   spi: Add Cadence ...
41
  {
10e8bf88c   Stefan Roese   spi: Add Cadence ...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
  	void *base = priv->regbase;
  	u8 opcode_rdid = 0x9F;
  	unsigned int idcode = 0, temp = 0;
  	int err = 0, i, range_lo = -1, range_hi = -1;
  
  	/* start with slowest clock (1 MHz) */
  	cadence_spi_write_speed(bus, 1000000);
  
  	/* configure the read data capture delay register to 0 */
  	cadence_qspi_apb_readdata_capture(base, 1, 0);
  
  	/* Enable QSPI */
  	cadence_qspi_apb_controller_enable(base);
  
  	/* read the ID which will be our golden value */
  	err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
  		3, (u8 *)&idcode);
  	if (err) {
  		puts("SF: Calibration failed (read)
  ");
  		return err;
  	}
  
  	/* use back the intended clock and find low range */
98fbd71d7   Chin Liang See   spi: cadence_qspi...
67
  	cadence_spi_write_speed(bus, hz);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
  	for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
  		/* Disable QSPI */
  		cadence_qspi_apb_controller_disable(base);
  
  		/* reconfigure the read data capture delay register */
  		cadence_qspi_apb_readdata_capture(base, 1, i);
  
  		/* Enable back QSPI */
  		cadence_qspi_apb_controller_enable(base);
  
  		/* issue a RDID to get the ID value */
  		err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
  			3, (u8 *)&temp);
  		if (err) {
  			puts("SF: Calibration failed (read)
  ");
  			return err;
  		}
  
  		/* search for range lo */
  		if (range_lo == -1 && temp == idcode) {
  			range_lo = i;
  			continue;
  		}
  
  		/* search for range hi */
  		if (range_lo != -1 && temp != idcode) {
  			range_hi = i - 1;
  			break;
  		}
  		range_hi = i;
  	}
  
  	if (range_lo == -1) {
  		puts("SF: Calibration failed (low range)
  ");
  		return err;
  	}
  
  	/* Disable QSPI for subsequent initialization */
  	cadence_qspi_apb_controller_disable(base);
  
  	/* configure the final value for read data capture delay register */
  	cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
  	debug("SF: Read data capture delay calibrated to %i (%i - %i)
  ",
  	      (range_hi + range_lo) / 2, range_lo, range_hi);
  
  	/* just to ensure we do once only when speed or chip select change */
98fbd71d7   Chin Liang See   spi: cadence_qspi...
117
  	priv->qspi_calibrated_hz = hz;
10e8bf88c   Stefan Roese   spi: Add Cadence ...
118
119
120
121
122
123
124
125
126
127
  	priv->qspi_calibrated_cs = spi_chip_select(bus);
  
  	return 0;
  }
  
  static int cadence_spi_set_speed(struct udevice *bus, uint hz)
  {
  	struct cadence_spi_platdata *plat = bus->platdata;
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
  	int err;
4e609b6cb   Chin Liang See   spi: cadence_qspi...
128
129
  	if (hz > plat->max_hz)
  		hz = plat->max_hz;
10e8bf88c   Stefan Roese   spi: Add Cadence ...
130
131
  	/* Disable QSPI */
  	cadence_qspi_apb_controller_disable(priv->regbase);
98fbd71d7   Chin Liang See   spi: cadence_qspi...
132
133
134
135
136
137
  	/*
  	 * Calibration required for different current SCLK speed, requested
  	 * SCLK speed or chip select
  	 */
  	if (priv->previous_hz != hz ||
  	    priv->qspi_calibrated_hz != hz ||
10e8bf88c   Stefan Roese   spi: Add Cadence ...
138
  	    priv->qspi_calibrated_cs != spi_chip_select(bus)) {
98fbd71d7   Chin Liang See   spi: cadence_qspi...
139
  		err = spi_calibration(bus, hz);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
140
141
  		if (err)
  			return err;
98fbd71d7   Chin Liang See   spi: cadence_qspi...
142
143
144
  
  		/* prevent calibration run when same as previous request */
  		priv->previous_hz = hz;
10e8bf88c   Stefan Roese   spi: Add Cadence ...
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  	}
  
  	/* Enable QSPI */
  	cadence_qspi_apb_controller_enable(priv->regbase);
  
  	debug("%s: speed=%d
  ", __func__, hz);
  
  	return 0;
  }
  
  static int cadence_spi_probe(struct udevice *bus)
  {
  	struct cadence_spi_platdata *plat = bus->platdata;
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
  
  	priv->regbase = plat->regbase;
  	priv->ahbbase = plat->ahbbase;
  
  	if (!priv->qspi_is_init) {
  		cadence_qspi_apb_controller_init(plat);
  		priv->qspi_is_init = 1;
  	}
  
  	return 0;
  }
  
  static int cadence_spi_set_mode(struct udevice *bus, uint mode)
  {
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
175
176
177
178
179
  
  	/* Disable QSPI */
  	cadence_qspi_apb_controller_disable(priv->regbase);
  
  	/* Set SPI mode */
7d403f284   Phil Edworthy   spi: cadence_qspi...
180
  	cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
181
182
183
184
185
186
187
188
189
190
191
192
193
  
  	/* Enable QSPI */
  	cadence_qspi_apb_controller_enable(priv->regbase);
  
  	return 0;
  }
  
  static int cadence_spi_xfer(struct udevice *dev, unsigned int bitlen,
  			    const void *dout, void *din, unsigned long flags)
  {
  	struct udevice *bus = dev->parent;
  	struct cadence_spi_platdata *plat = bus->platdata;
  	struct cadence_spi_priv *priv = dev_get_priv(bus);
2372e14f1   Vignesh R   spi: cadence_quad...
194
  	struct dm_spi_slave_platdata *dm_plat = dev_get_parent_platdata(dev);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
  	void *base = priv->regbase;
  	u8 *cmd_buf = priv->cmd_buf;
  	size_t data_bytes;
  	int err = 0;
  	u32 mode = CQSPI_STIG_WRITE;
  
  	if (flags & SPI_XFER_BEGIN) {
  		/* copy command to local buffer */
  		priv->cmd_len = bitlen / 8;
  		memcpy(cmd_buf, dout, priv->cmd_len);
  	}
  
  	if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
  		/* if start and end bit are set, the data bytes is 0. */
  		data_bytes = 0;
  	} else {
  		data_bytes = bitlen / 8;
  	}
  	debug("%s: len=%d [bytes]
  ", __func__, data_bytes);
  
  	/* Set Chip select */
  	cadence_qspi_apb_chipselect(base, spi_chip_select(dev),
15a70a5da   Jason Rush   spi: cadence_spi:...
218
  				    plat->is_decoded_cs);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
  
  	if ((flags & SPI_XFER_END) || (flags == 0)) {
  		if (priv->cmd_len == 0) {
  			printf("QSPI: Error, command is empty.
  ");
  			return -1;
  		}
  
  		if (din && data_bytes) {
  			/* read */
  			/* Use STIG if no address. */
  			if (!CQSPI_IS_ADDR(priv->cmd_len))
  				mode = CQSPI_STIG_READ;
  			else
  				mode = CQSPI_INDIRECT_READ;
  		} else if (dout && !(flags & SPI_XFER_BEGIN)) {
  			/* write */
  			if (!CQSPI_IS_ADDR(priv->cmd_len))
  				mode = CQSPI_STIG_WRITE;
  			else
  				mode = CQSPI_INDIRECT_WRITE;
  		}
  
  		switch (mode) {
  		case CQSPI_STIG_READ:
  			err = cadence_qspi_apb_command_read(
  				base, priv->cmd_len, cmd_buf,
  				data_bytes, din);
  
  		break;
  		case CQSPI_STIG_WRITE:
  			err = cadence_qspi_apb_command_write(base,
  				priv->cmd_len, cmd_buf,
  				data_bytes, dout);
  		break;
  		case CQSPI_INDIRECT_READ:
  			err = cadence_qspi_apb_indirect_read_setup(plat,
08fe9c294   Jagan Teki   spi: Use mode for...
256
  				priv->cmd_len, dm_plat->mode, cmd_buf);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
  			if (!err) {
  				err = cadence_qspi_apb_indirect_read_execute
  				(plat, data_bytes, din);
  			}
  		break;
  		case CQSPI_INDIRECT_WRITE:
  			err = cadence_qspi_apb_indirect_write_setup
  				(plat, priv->cmd_len, cmd_buf);
  			if (!err) {
  				err = cadence_qspi_apb_indirect_write_execute
  				(plat, data_bytes, dout);
  			}
  		break;
  		default:
  			err = -1;
  			break;
  		}
  
  		if (flags & SPI_XFER_END) {
  			/* clear command buffer */
  			memset(cmd_buf, 0, sizeof(priv->cmd_buf));
  			priv->cmd_len = 0;
  		}
  	}
  
  	return err;
  }
  
  static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
  {
  	struct cadence_spi_platdata *plat = bus->platdata;
  	const void *blob = gd->fdt_blob;
e160f7d43   Simon Glass   dm: core: Replace...
289
  	int node = dev_of_offset(bus);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  	int subnode;
  	u32 data[4];
  	int ret;
  
  	/* 2 base addresses are needed, lets get them from the DT */
  	ret = fdtdec_get_int_array(blob, node, "reg", data, ARRAY_SIZE(data));
  	if (ret) {
  		printf("Error: Can't get base addresses (ret=%d)!
  ", ret);
  		return -ENODEV;
  	}
  
  	plat->regbase = (void *)data[0];
  	plat->ahbbase = (void *)data[2];
15a70a5da   Jason Rush   spi: cadence_spi:...
304
305
306
307
308
  	plat->is_decoded_cs = fdtdec_get_bool(blob, node, "cdns,is-decoded-cs");
  	plat->fifo_depth = fdtdec_get_uint(blob, node, "cdns,fifo-depth", 128);
  	plat->fifo_width = fdtdec_get_uint(blob, node, "cdns,fifo-width", 4);
  	plat->trigger_address = fdtdec_get_uint(blob, node,
  						"cdns,trigger-address", 0);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
309

10e8bf88c   Stefan Roese   spi: Add Cadence ...
310
311
  	/* All other paramters are embedded in the child node */
  	subnode = fdt_first_subnode(blob, node);
1dc7d00f2   Axel Lin   spi: cadence_qspi...
312
  	if (subnode < 0) {
10e8bf88c   Stefan Roese   spi: Add Cadence ...
313
314
315
316
  		printf("Error: subnode with SPI flash config missing!
  ");
  		return -ENODEV;
  	}
040f4ba74   Chin Liang See   spi: cadence_qspi...
317
318
319
  	/* Use 500 KHz as a suitable default */
  	plat->max_hz = fdtdec_get_uint(blob, subnode, "spi-max-frequency",
  				       500000);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
320
  	/* Read other parameters from DT */
15a70a5da   Jason Rush   spi: cadence_spi:...
321
322
323
324
325
326
  	plat->page_size = fdtdec_get_uint(blob, subnode, "page-size", 256);
  	plat->block_size = fdtdec_get_uint(blob, subnode, "block-size", 16);
  	plat->tshsl_ns = fdtdec_get_uint(blob, subnode, "cdns,tshsl-ns", 200);
  	plat->tsd2d_ns = fdtdec_get_uint(blob, subnode, "cdns,tsd2d-ns", 255);
  	plat->tchsh_ns = fdtdec_get_uint(blob, subnode, "cdns,tchsh-ns", 20);
  	plat->tslch_ns = fdtdec_get_uint(blob, subnode, "cdns,tslch-ns", 20);
10e8bf88c   Stefan Roese   spi: Add Cadence ...
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
  
  	debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d
  ",
  	      __func__, plat->regbase, plat->ahbbase, plat->max_hz,
  	      plat->page_size);
  
  	return 0;
  }
  
  static const struct dm_spi_ops cadence_spi_ops = {
  	.xfer		= cadence_spi_xfer,
  	.set_speed	= cadence_spi_set_speed,
  	.set_mode	= cadence_spi_set_mode,
  	/*
  	 * cs_info is not needed, since we require all chip selects to be
  	 * in the device tree explicitly
  	 */
  };
  
  static const struct udevice_id cadence_spi_ids[] = {
  	{ .compatible = "cadence,qspi" },
  	{ }
  };
  
  U_BOOT_DRIVER(cadence_spi) = {
  	.name = "cadence_spi",
  	.id = UCLASS_SPI,
  	.of_match = cadence_spi_ids,
  	.ops = &cadence_spi_ops,
  	.ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
  	.platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
  	.priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
10e8bf88c   Stefan Roese   spi: Add Cadence ...
359
360
  	.probe = cadence_spi_probe,
  };