blob: 2efd626852e8760af5aae811f92a20ec6d16bf58 [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>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.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>
Igor Prusov89606c02023-11-14 14:02:56 +030019#include <linux/io.h>
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053020#include <linux/sizes.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030021#include <linux/time.h>
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -060022#include <zynqmp_firmware.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010023#include "cadence_qspi.h"
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -060024#include <dt-bindings/power/xlnx-versal-power.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010025
26#define CQSPI_STIG_READ 0
27#define CQSPI_STIG_WRITE 1
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053028#define CQSPI_READ 2
29#define CQSPI_WRITE 3
Stefan Roese1c60fe72014-11-07 12:37:49 +010030
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060031__weak int cadence_qspi_apb_dma_read(struct cadence_spi_priv *priv,
T Karthik Reddy73701e72022-05-12 04:05:32 -060032 const struct spi_mem_op *op)
33{
34 return 0;
35}
36
T Karthik Reddy3d71b2d2022-05-12 04:05:33 -060037__weak int cadence_qspi_versal_flash_reset(struct udevice *dev)
38{
39 return 0;
40}
41
Stefan Roese1c60fe72014-11-07 12:37:49 +010042static int cadence_spi_write_speed(struct udevice *bus, uint hz)
43{
Stefan Roese1c60fe72014-11-07 12:37:49 +010044 struct cadence_spi_priv *priv = dev_get_priv(bus);
45
46 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060047 priv->ref_clk_hz, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +010048
49 /* Reconfigure delay timing if speed is changed. */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060050 cadence_qspi_apb_delay(priv->regbase, priv->ref_clk_hz, hz,
51 priv->tshsl_ns, priv->tsd2d_ns,
52 priv->tchsh_ns, priv->tslch_ns);
Stefan Roese1c60fe72014-11-07 12:37:49 +010053
54 return 0;
55}
56
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060057static int cadence_spi_read_id(struct cadence_spi_priv *priv, u8 len,
Pratyush Yadave1814ad2021-06-26 00:47:09 +053058 u8 *idcode)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053059{
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060060 int err;
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060061
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053062 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
63 SPI_MEM_OP_NO_ADDR,
64 SPI_MEM_OP_NO_DUMMY,
65 SPI_MEM_OP_DATA_IN(len, idcode, 1));
66
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060067 err = cadence_qspi_apb_command_read_setup(priv, &op);
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060068 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060069 err = cadence_qspi_apb_command_read(priv, &op);
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060070
71 return err;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053072}
73
Stefan Roese1c60fe72014-11-07 12:37:49 +010074/* Calibration sequence to determine the read data capture delay register */
Chin Liang See36431f92015-10-17 08:31:55 -050075static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese1c60fe72014-11-07 12:37:49 +010076{
Stefan Roese1c60fe72014-11-07 12:37:49 +010077 struct cadence_spi_priv *priv = dev_get_priv(bus);
78 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +010079 unsigned int idcode = 0, temp = 0;
80 int err = 0, i, range_lo = -1, range_hi = -1;
81
82 /* start with slowest clock (1 MHz) */
83 cadence_spi_write_speed(bus, 1000000);
84
85 /* configure the read data capture delay register to 0 */
86 cadence_qspi_apb_readdata_capture(base, 1, 0);
87
88 /* Enable QSPI */
89 cadence_qspi_apb_controller_enable(base);
90
91 /* read the ID which will be our golden value */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060092 err = cadence_spi_read_id(priv, 3, (u8 *)&idcode);
Stefan Roese1c60fe72014-11-07 12:37:49 +010093 if (err) {
94 puts("SF: Calibration failed (read)\n");
95 return err;
96 }
97
98 /* use back the intended clock and find low range */
Chin Liang See36431f92015-10-17 08:31:55 -050099 cadence_spi_write_speed(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100100 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
101 /* Disable QSPI */
102 cadence_qspi_apb_controller_disable(base);
103
104 /* reconfigure the read data capture delay register */
105 cadence_qspi_apb_readdata_capture(base, 1, i);
106
107 /* Enable back QSPI */
108 cadence_qspi_apb_controller_enable(base);
109
110 /* issue a RDID to get the ID value */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600111 err = cadence_spi_read_id(priv, 3, (u8 *)&temp);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100112 if (err) {
113 puts("SF: Calibration failed (read)\n");
114 return err;
115 }
116
117 /* search for range lo */
118 if (range_lo == -1 && temp == idcode) {
119 range_lo = i;
120 continue;
121 }
122
123 /* search for range hi */
124 if (range_lo != -1 && temp != idcode) {
125 range_hi = i - 1;
126 break;
127 }
128 range_hi = i;
129 }
130
131 if (range_lo == -1) {
132 puts("SF: Calibration failed (low range)\n");
133 return err;
134 }
135
136 /* Disable QSPI for subsequent initialization */
137 cadence_qspi_apb_controller_disable(base);
138
139 /* configure the final value for read data capture delay register */
140 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
141 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
142 (range_hi + range_lo) / 2, range_lo, range_hi);
143
144 /* just to ensure we do once only when speed or chip select change */
Chin Liang See36431f92015-10-17 08:31:55 -0500145 priv->qspi_calibrated_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100146 priv->qspi_calibrated_cs = spi_chip_select(bus);
147
148 return 0;
149}
150
151static int cadence_spi_set_speed(struct udevice *bus, uint hz)
152{
Stefan Roese1c60fe72014-11-07 12:37:49 +0100153 struct cadence_spi_priv *priv = dev_get_priv(bus);
154 int err;
155
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600156 if (!hz || hz > priv->max_hz)
157 hz = priv->max_hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100158 /* Disable QSPI */
159 cadence_qspi_apb_controller_disable(priv->regbase);
160
Chin Liang See36431f92015-10-17 08:31:55 -0500161 /*
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530162 * If the device tree already provides a read delay value, use that
163 * instead of calibrating.
Chin Liang See36431f92015-10-17 08:31:55 -0500164 */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600165 if (priv->read_delay >= 0) {
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530166 cadence_spi_write_speed(bus, hz);
167 cadence_qspi_apb_readdata_capture(priv->regbase, 1,
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600168 priv->read_delay);
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530169 } else if (priv->previous_hz != hz ||
170 priv->qspi_calibrated_hz != hz ||
171 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
172 /*
173 * Calibration required for different current SCLK speed,
174 * requested SCLK speed or chip select
175 */
Chin Liang See36431f92015-10-17 08:31:55 -0500176 err = spi_calibration(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100177 if (err)
178 return err;
Chin Liang See36431f92015-10-17 08:31:55 -0500179
180 /* prevent calibration run when same as previous request */
181 priv->previous_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100182 }
183
184 /* Enable QSPI */
185 cadence_qspi_apb_controller_enable(priv->regbase);
186
187 debug("%s: speed=%d\n", __func__, hz);
188
189 return 0;
190}
191
192static int cadence_spi_probe(struct udevice *bus)
193{
Simon Glass95588622020-12-22 19:30:28 -0700194 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100195 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530196 struct clk clk;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100197 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100198
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600199 priv->regbase = plat->regbase;
200 priv->ahbbase = plat->ahbbase;
201 priv->is_dma = plat->is_dma;
202 priv->is_decoded_cs = plat->is_decoded_cs;
203 priv->fifo_depth = plat->fifo_depth;
204 priv->fifo_width = plat->fifo_width;
205 priv->trigger_address = plat->trigger_address;
206 priv->read_delay = plat->read_delay;
207 priv->ahbsize = plat->ahbsize;
208 priv->max_hz = plat->max_hz;
209
210 priv->page_size = plat->page_size;
211 priv->block_size = plat->block_size;
212 priv->tshsl_ns = plat->tshsl_ns;
213 priv->tsd2d_ns = plat->tsd2d_ns;
214 priv->tchsh_ns = plat->tchsh_ns;
215 priv->tslch_ns = plat->tslch_ns;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100216
Simon Glassf65db342023-02-05 15:44:33 -0700217 if (IS_ENABLED(CONFIG_ZYNQMP_FIRMWARE))
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -0600218 xilinx_pm_request(PM_REQUEST_NODE, PM_DEV_OSPI,
219 ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS,
220 ZYNQMP_PM_REQUEST_ACK_NO, NULL);
221
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600222 if (priv->ref_clk_hz == 0) {
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530223 ret = clk_get_by_index(bus, 0, &clk);
224 if (ret) {
Tom Rini3fb5b2f2022-03-30 18:07:23 -0400225#ifdef CONFIG_HAS_CQSPI_REF_CLK
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600226 priv->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
Tom Rini3fb5b2f2022-03-30 18:07:23 -0400227#elif defined(CONFIG_ARCH_SOCFPGA)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600228 priv->ref_clk_hz = cm_get_qspi_controller_clk_hz();
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530229#else
230 return ret;
231#endif
232 } else {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600233 priv->ref_clk_hz = clk_get_rate(&clk);
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530234 clk_free(&clk);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600235 if (IS_ERR_VALUE(priv->ref_clk_hz))
236 return priv->ref_clk_hz;
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530237 }
238 }
239
Christian Gmeinerd560a672022-02-22 17:23:25 +0100240 priv->resets = devm_reset_bulk_get_optional(bus);
241 if (priv->resets)
242 reset_deassert_bulk(priv->resets);
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100243
Stefan Roese1c60fe72014-11-07 12:37:49 +0100244 if (!priv->qspi_is_init) {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600245 cadence_qspi_apb_controller_init(priv);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100246 priv->qspi_is_init = 1;
247 }
248
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600249 priv->wr_delay = 50 * DIV_ROUND_UP(NSEC_PER_SEC, priv->ref_clk_hz);
Pratyush Yadav8dcf3e22021-06-26 00:47:08 +0530250
Ashok Reddy Somab6421112023-06-14 06:04:52 -0600251 /* Versal and Versal-NET use spi calibration to set read delay */
252 if (CONFIG_IS_ENABLED(ARCH_VERSAL) ||
253 CONFIG_IS_ENABLED(ARCH_VERSAL_NET))
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600254 if (priv->read_delay >= 0)
255 priv->read_delay = -1;
T Karthik Reddy3d71b2d2022-05-12 04:05:33 -0600256
Ashok Reddy Somab6421112023-06-14 06:04:52 -0600257 /* Reset ospi flash device */
258 return cadence_qspi_versal_flash_reset(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100259}
260
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100261static int cadence_spi_remove(struct udevice *dev)
262{
263 struct cadence_spi_priv *priv = dev_get_priv(dev);
Christian Gmeinerd560a672022-02-22 17:23:25 +0100264 int ret = 0;
265
266 if (priv->resets)
267 ret = reset_release_bulk(priv->resets);
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100268
Christian Gmeinerd560a672022-02-22 17:23:25 +0100269 return ret;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100270}
271
Stefan Roese1c60fe72014-11-07 12:37:49 +0100272static int cadence_spi_set_mode(struct udevice *bus, uint mode)
273{
274 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100275
276 /* Disable QSPI */
277 cadence_qspi_apb_controller_disable(priv->regbase);
278
279 /* Set SPI mode */
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000280 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100281
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530282 /* Enable Direct Access Controller */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600283 if (priv->use_dac_mode)
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530284 cadence_qspi_apb_dac_mode_enable(priv->regbase);
285
Stefan Roese1c60fe72014-11-07 12:37:49 +0100286 /* Enable QSPI */
287 cadence_qspi_apb_controller_enable(priv->regbase);
288
289 return 0;
290}
291
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530292static int cadence_spi_mem_exec_op(struct spi_slave *spi,
293 const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100294{
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530295 struct udevice *bus = spi->dev->parent;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100296 struct cadence_spi_priv *priv = dev_get_priv(bus);
297 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100298 int err = 0;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530299 u32 mode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100300
301 /* Set Chip select */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530302 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600303 priv->is_decoded_cs);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100304
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530305 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
Dhruva Gole64343d52023-01-03 12:01:12 +0530306 /*
307 * Performing reads in DAC mode forces to read minimum 4 bytes
308 * which is unsupported on some flash devices during register
309 * reads, prefer STIG mode for such small reads.
310 */
Apurva Nandan52ff9b92023-04-12 16:28:55 +0530311 if (op->data.nbytes <= CQSPI_STIG_DATA_LEN_MAX)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530312 mode = CQSPI_STIG_READ;
313 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530314 mode = CQSPI_READ;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530315 } else {
Apurva Nandan52ff9b92023-04-12 16:28:55 +0530316 if (op->data.nbytes <= CQSPI_STIG_DATA_LEN_MAX)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530317 mode = CQSPI_STIG_WRITE;
318 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530319 mode = CQSPI_WRITE;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530320 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100321
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530322 switch (mode) {
323 case CQSPI_STIG_READ:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600324 err = cadence_qspi_apb_command_read_setup(priv, op);
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530325 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600326 err = cadence_qspi_apb_command_read(priv, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100327 break;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530328 case CQSPI_STIG_WRITE:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600329 err = cadence_qspi_apb_command_write_setup(priv, op);
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530330 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600331 err = cadence_qspi_apb_command_write(priv, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100332 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530333 case CQSPI_READ:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600334 err = cadence_qspi_apb_read_setup(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600335 if (!err) {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600336 if (priv->is_dma)
337 err = cadence_qspi_apb_dma_read(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600338 else
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600339 err = cadence_qspi_apb_read_execute(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600340 }
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530341 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530342 case CQSPI_WRITE:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600343 err = cadence_qspi_apb_write_setup(priv, op);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530344 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600345 err = cadence_qspi_apb_write_execute(priv, op);
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530346 break;
347 default:
348 err = -1;
349 break;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100350 }
351
352 return err;
353}
354
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530355static bool cadence_spi_mem_supports_op(struct spi_slave *slave,
356 const struct spi_mem_op *op)
357{
358 bool all_true, all_false;
359
Apurva Nandanb88f55c2023-04-12 16:28:54 +0530360 /*
361 * op->dummy.dtr is required for converting nbytes into ncycles.
362 * Also, don't check the dtr field of the op phase having zero nbytes.
363 */
364 all_true = op->cmd.dtr &&
365 (!op->addr.nbytes || op->addr.dtr) &&
366 (!op->dummy.nbytes || op->dummy.dtr) &&
367 (!op->data.nbytes || op->data.dtr);
368
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530369 all_false = !op->cmd.dtr && !op->addr.dtr && !op->dummy.dtr &&
370 !op->data.dtr;
371
372 /* Mixed DTR modes not supported. */
373 if (!(all_true || all_false))
374 return false;
375
376 if (all_true)
377 return spi_mem_dtr_supports_op(slave, op);
378 else
379 return spi_mem_default_supports_op(slave, op);
380}
381
Simon Glassaad29ae2020-12-03 16:55:21 -0700382static int cadence_spi_of_to_plat(struct udevice *bus)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100383{
Simon Glass95588622020-12-22 19:30:28 -0700384 struct cadence_spi_plat *plat = dev_get_plat(bus);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600385 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200386 ofnode subnode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100387
Johan Jonkerb52189e2023-03-13 01:32:31 +0100388 plat->regbase = devfdt_get_addr_index_ptr(bus, 0);
Johan Jonker40702782023-03-13 01:32:18 +0100389 plat->ahbbase = devfdt_get_addr_size_index_ptr(bus, 1, &plat->ahbsize);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200390 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
391 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
392 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
393 plat->trigger_address = dev_read_u32_default(bus,
394 "cdns,trigger-address",
395 0);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530396 /* Use DAC mode only when MMIO window is at least 8M wide */
397 if (plat->ahbsize >= SZ_8M)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600398 priv->use_dac_mode = true;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100399
T Karthik Reddy73701e72022-05-12 04:05:32 -0600400 plat->is_dma = dev_read_bool(bus, "cdns,is-dma");
401
Pengfei Fan746271d2022-12-09 09:39:50 +0800402 /* All other parameters are embedded in the child node */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200403 subnode = dev_read_first_subnode(bus);
404 if (!ofnode_valid(subnode)) {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100405 printf("Error: subnode with SPI flash config missing!\n");
406 return -ENODEV;
407 }
408
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500409 /* Use 500 KHz as a suitable default */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200410 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
411 500000);
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500412
Stefan Roese1c60fe72014-11-07 12:37:49 +0100413 /* Read other parameters from DT */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200414 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
415 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
416 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
417 200);
418 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
419 255);
420 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
421 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530422 /*
423 * Read delay should be an unsigned value but we use a signed integer
424 * so that negative values can indicate that the device tree did not
425 * specify any signed values and we need to perform the calibration
426 * sequence to find it out.
427 */
428 plat->read_delay = ofnode_read_s32_default(subnode, "cdns,read-delay",
429 -1);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100430
431 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
432 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
433 plat->page_size);
434
435 return 0;
436}
437
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530438static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
439 .exec_op = cadence_spi_mem_exec_op,
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530440 .supports_op = cadence_spi_mem_supports_op,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530441};
442
Stefan Roese1c60fe72014-11-07 12:37:49 +0100443static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100444 .set_speed = cadence_spi_set_speed,
445 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530446 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100447 /*
448 * cs_info is not needed, since we require all chip selects to be
449 * in the device tree explicitly
450 */
451};
452
453static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt454c9b32018-11-02 11:54:51 +0100454 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendra99276f02019-12-05 15:46:07 +0530455 { .compatible = "ti,am654-ospi" },
Stefan Roese1c60fe72014-11-07 12:37:49 +0100456 { }
457};
458
459U_BOOT_DRIVER(cadence_spi) = {
460 .name = "cadence_spi",
461 .id = UCLASS_SPI,
462 .of_match = cadence_spi_ids,
463 .ops = &cadence_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700464 .of_to_plat = cadence_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700465 .plat_auto = sizeof(struct cadence_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700466 .priv_auto = sizeof(struct cadence_spi_priv),
Stefan Roese1c60fe72014-11-07 12:37:49 +0100467 .probe = cadence_spi_probe,
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100468 .remove = cadence_spi_remove,
469 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100470};