1 // SPDX-License-Identifier: GPL-2.0+
4 * Altera Corporation <www.altera.com>
13 #include <linux/errno.h>
14 #include "cadence_qspi.h"
16 #define CQSPI_STIG_READ 0
17 #define CQSPI_STIG_WRITE 1
18 #define CQSPI_INDIRECT_READ 2
19 #define CQSPI_INDIRECT_WRITE 3
21 DECLARE_GLOBAL_DATA_PTR;
23 static int cadence_spi_write_speed(struct udevice *bus, uint hz)
25 struct cadence_spi_platdata *plat = bus->platdata;
26 struct cadence_spi_priv *priv = dev_get_priv(bus);
28 cadence_qspi_apb_config_baudrate_div(priv->regbase,
29 CONFIG_CQSPI_REF_CLK, hz);
31 /* Reconfigure delay timing if speed is changed. */
32 cadence_qspi_apb_delay(priv->regbase, CONFIG_CQSPI_REF_CLK, hz,
33 plat->tshsl_ns, plat->tsd2d_ns,
34 plat->tchsh_ns, plat->tslch_ns);
39 /* Calibration sequence to determine the read data capture delay register */
40 static int spi_calibration(struct udevice *bus, uint hz)
42 struct cadence_spi_priv *priv = dev_get_priv(bus);
43 void *base = priv->regbase;
44 u8 opcode_rdid = 0x9F;
45 unsigned int idcode = 0, temp = 0;
46 int err = 0, i, range_lo = -1, range_hi = -1;
48 /* start with slowest clock (1 MHz) */
49 cadence_spi_write_speed(bus, 1000000);
51 /* configure the read data capture delay register to 0 */
52 cadence_qspi_apb_readdata_capture(base, 1, 0);
55 cadence_qspi_apb_controller_enable(base);
57 /* read the ID which will be our golden value */
58 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
61 puts("SF: Calibration failed (read)\n");
65 /* use back the intended clock and find low range */
66 cadence_spi_write_speed(bus, hz);
67 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
69 cadence_qspi_apb_controller_disable(base);
71 /* reconfigure the read data capture delay register */
72 cadence_qspi_apb_readdata_capture(base, 1, i);
74 /* Enable back QSPI */
75 cadence_qspi_apb_controller_enable(base);
77 /* issue a RDID to get the ID value */
78 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
81 puts("SF: Calibration failed (read)\n");
85 /* search for range lo */
86 if (range_lo == -1 && temp == idcode) {
91 /* search for range hi */
92 if (range_lo != -1 && temp != idcode) {
100 puts("SF: Calibration failed (low range)\n");
104 /* Disable QSPI for subsequent initialization */
105 cadence_qspi_apb_controller_disable(base);
107 /* configure the final value for read data capture delay register */
108 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
109 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
110 (range_hi + range_lo) / 2, range_lo, range_hi);
112 /* just to ensure we do once only when speed or chip select change */
113 priv->qspi_calibrated_hz = hz;
114 priv->qspi_calibrated_cs = spi_chip_select(bus);
119 static int cadence_spi_set_speed(struct udevice *bus, uint hz)
121 struct cadence_spi_platdata *plat = bus->platdata;
122 struct cadence_spi_priv *priv = dev_get_priv(bus);
125 if (hz > plat->max_hz)
129 cadence_qspi_apb_controller_disable(priv->regbase);
132 * Calibration required for different current SCLK speed, requested
133 * SCLK speed or chip select
135 if (priv->previous_hz != hz ||
136 priv->qspi_calibrated_hz != hz ||
137 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
138 err = spi_calibration(bus, hz);
142 /* prevent calibration run when same as previous request */
143 priv->previous_hz = hz;
147 cadence_qspi_apb_controller_enable(priv->regbase);
149 debug("%s: speed=%d\n", __func__, hz);
154 static int cadence_spi_probe(struct udevice *bus)
156 struct cadence_spi_platdata *plat = bus->platdata;
157 struct cadence_spi_priv *priv = dev_get_priv(bus);
160 priv->regbase = plat->regbase;
161 priv->ahbbase = plat->ahbbase;
163 ret = reset_get_bulk(bus, &priv->resets);
165 dev_warn(bus, "Can't get reset: %d\n", ret);
167 reset_deassert_bulk(&priv->resets);
169 if (!priv->qspi_is_init) {
170 cadence_qspi_apb_controller_init(plat);
171 priv->qspi_is_init = 1;
177 static int cadence_spi_remove(struct udevice *dev)
179 struct cadence_spi_priv *priv = dev_get_priv(dev);
181 return reset_release_bulk(&priv->resets);
184 static int cadence_spi_set_mode(struct udevice *bus, uint mode)
186 struct cadence_spi_priv *priv = dev_get_priv(bus);
189 cadence_qspi_apb_controller_disable(priv->regbase);
192 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
195 cadence_qspi_apb_controller_enable(priv->regbase);
200 static int cadence_spi_xfer(struct udevice *dev, unsigned int bitlen,
201 const void *dout, void *din, unsigned long flags)
203 struct udevice *bus = dev->parent;
204 struct cadence_spi_platdata *plat = bus->platdata;
205 struct cadence_spi_priv *priv = dev_get_priv(bus);
206 struct dm_spi_slave_platdata *dm_plat = dev_get_parent_platdata(dev);
207 void *base = priv->regbase;
208 u8 *cmd_buf = priv->cmd_buf;
211 u32 mode = CQSPI_STIG_WRITE;
213 if (flags & SPI_XFER_BEGIN) {
214 /* copy command to local buffer */
215 priv->cmd_len = bitlen / 8;
216 memcpy(cmd_buf, dout, priv->cmd_len);
219 if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
220 /* if start and end bit are set, the data bytes is 0. */
223 data_bytes = bitlen / 8;
225 debug("%s: len=%zu [bytes]\n", __func__, data_bytes);
227 /* Set Chip select */
228 cadence_qspi_apb_chipselect(base, spi_chip_select(dev),
229 plat->is_decoded_cs);
231 if ((flags & SPI_XFER_END) || (flags == 0)) {
232 if (priv->cmd_len == 0) {
233 printf("QSPI: Error, command is empty.\n");
237 if (din && data_bytes) {
239 /* Use STIG if no address. */
240 if (!CQSPI_IS_ADDR(priv->cmd_len))
241 mode = CQSPI_STIG_READ;
243 mode = CQSPI_INDIRECT_READ;
244 } else if (dout && !(flags & SPI_XFER_BEGIN)) {
246 if (!CQSPI_IS_ADDR(priv->cmd_len))
247 mode = CQSPI_STIG_WRITE;
249 mode = CQSPI_INDIRECT_WRITE;
253 case CQSPI_STIG_READ:
254 err = cadence_qspi_apb_command_read(
255 base, priv->cmd_len, cmd_buf,
259 case CQSPI_STIG_WRITE:
260 err = cadence_qspi_apb_command_write(base,
261 priv->cmd_len, cmd_buf,
264 case CQSPI_INDIRECT_READ:
265 err = cadence_qspi_apb_indirect_read_setup(plat,
266 priv->cmd_len, dm_plat->mode, cmd_buf);
268 err = cadence_qspi_apb_indirect_read_execute
269 (plat, data_bytes, din);
272 case CQSPI_INDIRECT_WRITE:
273 err = cadence_qspi_apb_indirect_write_setup
274 (plat, priv->cmd_len, dm_plat->mode, cmd_buf);
276 err = cadence_qspi_apb_indirect_write_execute
277 (plat, data_bytes, dout);
285 if (flags & SPI_XFER_END) {
286 /* clear command buffer */
287 memset(cmd_buf, 0, sizeof(priv->cmd_buf));
295 static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
297 struct cadence_spi_platdata *plat = bus->platdata;
298 const void *blob = gd->fdt_blob;
299 int node = dev_of_offset(bus);
302 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
303 plat->ahbbase = (void *)devfdt_get_addr_index(bus, 1);
304 plat->is_decoded_cs = fdtdec_get_bool(blob, node, "cdns,is-decoded-cs");
305 plat->fifo_depth = fdtdec_get_uint(blob, node, "cdns,fifo-depth", 128);
306 plat->fifo_width = fdtdec_get_uint(blob, node, "cdns,fifo-width", 4);
307 plat->trigger_address = fdtdec_get_uint(blob, node,
308 "cdns,trigger-address", 0);
310 /* All other paramters are embedded in the child node */
311 subnode = fdt_first_subnode(blob, node);
313 printf("Error: subnode with SPI flash config missing!\n");
317 /* Use 500 KHz as a suitable default */
318 plat->max_hz = fdtdec_get_uint(blob, subnode, "spi-max-frequency",
321 /* Read other parameters from DT */
322 plat->page_size = fdtdec_get_uint(blob, subnode, "page-size", 256);
323 plat->block_size = fdtdec_get_uint(blob, subnode, "block-size", 16);
324 plat->tshsl_ns = fdtdec_get_uint(blob, subnode, "cdns,tshsl-ns", 200);
325 plat->tsd2d_ns = fdtdec_get_uint(blob, subnode, "cdns,tsd2d-ns", 255);
326 plat->tchsh_ns = fdtdec_get_uint(blob, subnode, "cdns,tchsh-ns", 20);
327 plat->tslch_ns = fdtdec_get_uint(blob, subnode, "cdns,tslch-ns", 20);
329 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
330 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
336 static const struct dm_spi_ops cadence_spi_ops = {
337 .xfer = cadence_spi_xfer,
338 .set_speed = cadence_spi_set_speed,
339 .set_mode = cadence_spi_set_mode,
341 * cs_info is not needed, since we require all chip selects to be
342 * in the device tree explicitly
346 static const struct udevice_id cadence_spi_ids[] = {
347 { .compatible = "cdns,qspi-nor" },
351 U_BOOT_DRIVER(cadence_spi) = {
352 .name = "cadence_spi",
354 .of_match = cadence_spi_ids,
355 .ops = &cadence_spi_ops,
356 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
357 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
358 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
359 .probe = cadence_spi_probe,
360 .remove = cadence_spi_remove,
361 .flags = DM_FLAG_OS_PREPARE,