1 // SPDX-License-Identifier: GPL-2.0+
4 * Altera Corporation <www.altera.com>
14 #include <linux/errno.h>
15 #include "cadence_qspi.h"
17 #define CQSPI_STIG_READ 0
18 #define CQSPI_STIG_WRITE 1
19 #define CQSPI_INDIRECT_READ 2
20 #define CQSPI_INDIRECT_WRITE 3
22 static int cadence_spi_write_speed(struct udevice *bus, uint hz)
24 struct cadence_spi_platdata *plat = bus->platdata;
25 struct cadence_spi_priv *priv = dev_get_priv(bus);
27 cadence_qspi_apb_config_baudrate_div(priv->regbase,
28 plat->ref_clk_hz, hz);
30 /* Reconfigure delay timing if speed is changed. */
31 cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz,
32 plat->tshsl_ns, plat->tsd2d_ns,
33 plat->tchsh_ns, plat->tslch_ns);
38 /* Calibration sequence to determine the read data capture delay register */
39 static int spi_calibration(struct udevice *bus, uint hz)
41 struct cadence_spi_priv *priv = dev_get_priv(bus);
42 void *base = priv->regbase;
43 u8 opcode_rdid = 0x9F;
44 unsigned int idcode = 0, temp = 0;
45 int err = 0, i, range_lo = -1, range_hi = -1;
47 /* start with slowest clock (1 MHz) */
48 cadence_spi_write_speed(bus, 1000000);
50 /* configure the read data capture delay register to 0 */
51 cadence_qspi_apb_readdata_capture(base, 1, 0);
54 cadence_qspi_apb_controller_enable(base);
56 /* read the ID which will be our golden value */
57 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
60 puts("SF: Calibration failed (read)\n");
64 /* use back the intended clock and find low range */
65 cadence_spi_write_speed(bus, hz);
66 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
68 cadence_qspi_apb_controller_disable(base);
70 /* reconfigure the read data capture delay register */
71 cadence_qspi_apb_readdata_capture(base, 1, i);
73 /* Enable back QSPI */
74 cadence_qspi_apb_controller_enable(base);
76 /* issue a RDID to get the ID value */
77 err = cadence_qspi_apb_command_read(base, 1, &opcode_rdid,
80 puts("SF: Calibration failed (read)\n");
84 /* search for range lo */
85 if (range_lo == -1 && temp == idcode) {
90 /* search for range hi */
91 if (range_lo != -1 && temp != idcode) {
99 puts("SF: Calibration failed (low range)\n");
103 /* Disable QSPI for subsequent initialization */
104 cadence_qspi_apb_controller_disable(base);
106 /* configure the final value for read data capture delay register */
107 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
108 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
109 (range_hi + range_lo) / 2, range_lo, range_hi);
111 /* just to ensure we do once only when speed or chip select change */
112 priv->qspi_calibrated_hz = hz;
113 priv->qspi_calibrated_cs = spi_chip_select(bus);
118 static int cadence_spi_set_speed(struct udevice *bus, uint hz)
120 struct cadence_spi_platdata *plat = bus->platdata;
121 struct cadence_spi_priv *priv = dev_get_priv(bus);
124 if (hz > plat->max_hz)
128 cadence_qspi_apb_controller_disable(priv->regbase);
131 * Calibration required for different current SCLK speed, requested
132 * SCLK speed or chip select
134 if (priv->previous_hz != hz ||
135 priv->qspi_calibrated_hz != hz ||
136 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
137 err = spi_calibration(bus, hz);
141 /* prevent calibration run when same as previous request */
142 priv->previous_hz = hz;
146 cadence_qspi_apb_controller_enable(priv->regbase);
148 debug("%s: speed=%d\n", __func__, hz);
153 static int cadence_spi_probe(struct udevice *bus)
155 struct cadence_spi_platdata *plat = bus->platdata;
156 struct cadence_spi_priv *priv = dev_get_priv(bus);
159 priv->regbase = plat->regbase;
160 priv->ahbbase = plat->ahbbase;
162 ret = reset_get_bulk(bus, &priv->resets);
164 dev_warn(bus, "Can't get reset: %d\n", ret);
166 reset_deassert_bulk(&priv->resets);
168 if (!priv->qspi_is_init) {
169 cadence_qspi_apb_controller_init(plat);
170 priv->qspi_is_init = 1;
176 static int cadence_spi_remove(struct udevice *dev)
178 struct cadence_spi_priv *priv = dev_get_priv(dev);
180 return reset_release_bulk(&priv->resets);
183 static int cadence_spi_set_mode(struct udevice *bus, uint mode)
185 struct cadence_spi_priv *priv = dev_get_priv(bus);
188 cadence_qspi_apb_controller_disable(priv->regbase);
191 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
194 cadence_qspi_apb_controller_enable(priv->regbase);
199 static int cadence_spi_xfer(struct udevice *dev, unsigned int bitlen,
200 const void *dout, void *din, unsigned long flags)
202 struct udevice *bus = dev->parent;
203 struct cadence_spi_platdata *plat = bus->platdata;
204 struct cadence_spi_priv *priv = dev_get_priv(bus);
205 struct dm_spi_slave_platdata *dm_plat = dev_get_parent_platdata(dev);
206 void *base = priv->regbase;
207 u8 *cmd_buf = priv->cmd_buf;
210 u32 mode = CQSPI_STIG_WRITE;
212 if (flags & SPI_XFER_BEGIN) {
213 /* copy command to local buffer */
214 priv->cmd_len = bitlen / 8;
215 memcpy(cmd_buf, dout, priv->cmd_len);
218 if (flags == (SPI_XFER_BEGIN | SPI_XFER_END)) {
219 /* if start and end bit are set, the data bytes is 0. */
222 data_bytes = bitlen / 8;
224 debug("%s: len=%zu [bytes]\n", __func__, data_bytes);
226 /* Set Chip select */
227 cadence_qspi_apb_chipselect(base, spi_chip_select(dev),
228 plat->is_decoded_cs);
230 if ((flags & SPI_XFER_END) || (flags == 0)) {
231 if (priv->cmd_len == 0) {
232 printf("QSPI: Error, command is empty.\n");
236 if (din && data_bytes) {
238 /* Use STIG if no address. */
239 if (!CQSPI_IS_ADDR(priv->cmd_len))
240 mode = CQSPI_STIG_READ;
242 mode = CQSPI_INDIRECT_READ;
243 } else if (dout && !(flags & SPI_XFER_BEGIN)) {
245 if (!CQSPI_IS_ADDR(priv->cmd_len))
246 mode = CQSPI_STIG_WRITE;
248 mode = CQSPI_INDIRECT_WRITE;
252 case CQSPI_STIG_READ:
253 err = cadence_qspi_apb_command_read(
254 base, priv->cmd_len, cmd_buf,
258 case CQSPI_STIG_WRITE:
259 err = cadence_qspi_apb_command_write(base,
260 priv->cmd_len, cmd_buf,
263 case CQSPI_INDIRECT_READ:
264 err = cadence_qspi_apb_indirect_read_setup(plat,
265 priv->cmd_len, dm_plat->mode, cmd_buf);
267 err = cadence_qspi_apb_indirect_read_execute
268 (plat, data_bytes, din);
271 case CQSPI_INDIRECT_WRITE:
272 err = cadence_qspi_apb_indirect_write_setup
273 (plat, priv->cmd_len, dm_plat->mode, cmd_buf);
275 err = cadence_qspi_apb_indirect_write_execute
276 (plat, data_bytes, dout);
284 if (flags & SPI_XFER_END) {
285 /* clear command buffer */
286 memset(cmd_buf, 0, sizeof(priv->cmd_buf));
294 static int cadence_spi_ofdata_to_platdata(struct udevice *bus)
296 struct cadence_spi_platdata *plat = bus->platdata;
301 plat->regbase = (void *)devfdt_get_addr_index(bus, 0);
302 plat->ahbbase = (void *)devfdt_get_addr_index(bus, 1);
303 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
304 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
305 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
306 plat->trigger_address = dev_read_u32_default(bus,
307 "cdns,trigger-address",
310 /* All other paramters are embedded in the child node */
311 subnode = dev_read_first_subnode(bus);
312 if (!ofnode_valid(subnode)) {
313 printf("Error: subnode with SPI flash config missing!\n");
317 /* Use 500 KHz as a suitable default */
318 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
321 /* Read other parameters from DT */
322 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
323 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
324 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
326 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
328 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
329 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
331 ret = clk_get_by_index(bus, 0, &clk);
333 #ifdef CONFIG_CQSPI_REF_CLK
334 plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
339 plat->ref_clk_hz = clk_get_rate(&clk);
341 if (IS_ERR_VALUE(plat->ref_clk_hz))
342 return plat->ref_clk_hz;
345 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
346 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
352 static const struct dm_spi_ops cadence_spi_ops = {
353 .xfer = cadence_spi_xfer,
354 .set_speed = cadence_spi_set_speed,
355 .set_mode = cadence_spi_set_mode,
357 * cs_info is not needed, since we require all chip selects to be
358 * in the device tree explicitly
362 static const struct udevice_id cadence_spi_ids[] = {
363 { .compatible = "cdns,qspi-nor" },
367 U_BOOT_DRIVER(cadence_spi) = {
368 .name = "cadence_spi",
370 .of_match = cadence_spi_ids,
371 .ops = &cadence_spi_ops,
372 .ofdata_to_platdata = cadence_spi_ofdata_to_platdata,
373 .platdata_auto_alloc_size = sizeof(struct cadence_spi_platdata),
374 .priv_auto_alloc_size = sizeof(struct cadence_spi_priv),
375 .probe = cadence_spi_probe,
376 .remove = cadence_spi_remove,
377 .flags = DM_FLAG_OS_PREPARE,