blob: 75e522320101a34352e06ad9c18e49b23ca353c3 [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
Simon Goldschmidtbaaa3fc2019-11-20 22:27:31 +01007#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +01009#include <dm.h>
10#include <fdtdec.h>
11#include <malloc.h>
Simon Goldschmidt46e56a42019-03-01 20:12:35 +010012#include <reset.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010013#include <spi.h>
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053014#include <spi-mem.h>
Simon Glass9bc15642020-02-03 07:36:16 -070015#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090017#include <linux/errno.h>
Igor Prusov89606c02023-11-14 14:02:56 +030018#include <linux/io.h>
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053019#include <linux/sizes.h>
Igor Prusovc3421ea2023-11-09 20:10:04 +030020#include <linux/time.h>
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -060021#include <zynqmp_firmware.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010022#include "cadence_qspi.h"
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -060023#include <dt-bindings/power/xlnx-versal-power.h>
Stefan Roese1c60fe72014-11-07 12:37:49 +010024
25#define CQSPI_STIG_READ 0
26#define CQSPI_STIG_WRITE 1
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +053027#define CQSPI_READ 2
28#define CQSPI_WRITE 3
Stefan Roese1c60fe72014-11-07 12:37:49 +010029
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060030__weak int cadence_qspi_apb_dma_read(struct cadence_spi_priv *priv,
T Karthik Reddy73701e72022-05-12 04:05:32 -060031 const struct spi_mem_op *op)
32{
33 return 0;
34}
35
T Karthik Reddy3d71b2d2022-05-12 04:05:33 -060036__weak int cadence_qspi_versal_flash_reset(struct udevice *dev)
37{
38 return 0;
39}
40
Udit Kumar88f53042023-09-12 15:20:35 +053041__weak ofnode cadence_qspi_get_subnode(struct udevice *dev)
42{
43 return dev_read_first_subnode(dev);
44}
45
Stefan Roese1c60fe72014-11-07 12:37:49 +010046static int cadence_spi_write_speed(struct udevice *bus, uint hz)
47{
Stefan Roese1c60fe72014-11-07 12:37:49 +010048 struct cadence_spi_priv *priv = dev_get_priv(bus);
49
50 cadence_qspi_apb_config_baudrate_div(priv->regbase,
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060051 priv->ref_clk_hz, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +010052
53 /* Reconfigure delay timing if speed is changed. */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060054 cadence_qspi_apb_delay(priv->regbase, priv->ref_clk_hz, hz,
55 priv->tshsl_ns, priv->tsd2d_ns,
56 priv->tchsh_ns, priv->tslch_ns);
Stefan Roese1c60fe72014-11-07 12:37:49 +010057
58 return 0;
59}
60
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060061static int cadence_spi_read_id(struct cadence_spi_priv *priv, u8 len,
Pratyush Yadave1814ad2021-06-26 00:47:09 +053062 u8 *idcode)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053063{
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060064 int err;
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060065
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053066 struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x9F, 1),
67 SPI_MEM_OP_NO_ADDR,
68 SPI_MEM_OP_NO_DUMMY,
69 SPI_MEM_OP_DATA_IN(len, idcode, 1));
70
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060071 err = cadence_qspi_apb_command_read_setup(priv, &op);
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060072 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060073 err = cadence_qspi_apb_command_read(priv, &op);
Ashok Reddy Soma6c433fd2022-08-24 05:38:46 -060074
75 return err;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +053076}
77
Stefan Roese1c60fe72014-11-07 12:37:49 +010078/* Calibration sequence to determine the read data capture delay register */
Chin Liang See36431f92015-10-17 08:31:55 -050079static int spi_calibration(struct udevice *bus, uint hz)
Stefan Roese1c60fe72014-11-07 12:37:49 +010080{
Stefan Roese1c60fe72014-11-07 12:37:49 +010081 struct cadence_spi_priv *priv = dev_get_priv(bus);
82 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +010083 unsigned int idcode = 0, temp = 0;
84 int err = 0, i, range_lo = -1, range_hi = -1;
85
86 /* start with slowest clock (1 MHz) */
87 cadence_spi_write_speed(bus, 1000000);
88
89 /* configure the read data capture delay register to 0 */
90 cadence_qspi_apb_readdata_capture(base, 1, 0);
91
92 /* Enable QSPI */
93 cadence_qspi_apb_controller_enable(base);
94
95 /* read the ID which will be our golden value */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -060096 err = cadence_spi_read_id(priv, 3, (u8 *)&idcode);
Stefan Roese1c60fe72014-11-07 12:37:49 +010097 if (err) {
98 puts("SF: Calibration failed (read)\n");
99 return err;
100 }
101
102 /* use back the intended clock and find low range */
Chin Liang See36431f92015-10-17 08:31:55 -0500103 cadence_spi_write_speed(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100104 for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) {
105 /* Disable QSPI */
106 cadence_qspi_apb_controller_disable(base);
107
108 /* reconfigure the read data capture delay register */
109 cadence_qspi_apb_readdata_capture(base, 1, i);
110
111 /* Enable back QSPI */
112 cadence_qspi_apb_controller_enable(base);
113
114 /* issue a RDID to get the ID value */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600115 err = cadence_spi_read_id(priv, 3, (u8 *)&temp);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100116 if (err) {
117 puts("SF: Calibration failed (read)\n");
118 return err;
119 }
120
121 /* search for range lo */
122 if (range_lo == -1 && temp == idcode) {
123 range_lo = i;
124 continue;
125 }
126
127 /* search for range hi */
128 if (range_lo != -1 && temp != idcode) {
129 range_hi = i - 1;
130 break;
131 }
132 range_hi = i;
133 }
134
135 if (range_lo == -1) {
136 puts("SF: Calibration failed (low range)\n");
137 return err;
138 }
139
140 /* Disable QSPI for subsequent initialization */
141 cadence_qspi_apb_controller_disable(base);
142
143 /* configure the final value for read data capture delay register */
144 cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2);
145 debug("SF: Read data capture delay calibrated to %i (%i - %i)\n",
146 (range_hi + range_lo) / 2, range_lo, range_hi);
147
148 /* just to ensure we do once only when speed or chip select change */
Chin Liang See36431f92015-10-17 08:31:55 -0500149 priv->qspi_calibrated_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100150 priv->qspi_calibrated_cs = spi_chip_select(bus);
151
152 return 0;
153}
154
155static int cadence_spi_set_speed(struct udevice *bus, uint hz)
156{
Stefan Roese1c60fe72014-11-07 12:37:49 +0100157 struct cadence_spi_priv *priv = dev_get_priv(bus);
158 int err;
159
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600160 if (!hz || hz > priv->max_hz)
161 hz = priv->max_hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100162 /* Disable QSPI */
163 cadence_qspi_apb_controller_disable(priv->regbase);
164
Chin Liang See36431f92015-10-17 08:31:55 -0500165 /*
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530166 * If the device tree already provides a read delay value, use that
167 * instead of calibrating.
Chin Liang See36431f92015-10-17 08:31:55 -0500168 */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600169 if (priv->read_delay >= 0) {
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530170 cadence_spi_write_speed(bus, hz);
171 cadence_qspi_apb_readdata_capture(priv->regbase, 1,
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600172 priv->read_delay);
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530173 } else if (priv->previous_hz != hz ||
174 priv->qspi_calibrated_hz != hz ||
175 priv->qspi_calibrated_cs != spi_chip_select(bus)) {
176 /*
177 * Calibration required for different current SCLK speed,
178 * requested SCLK speed or chip select
179 */
Chin Liang See36431f92015-10-17 08:31:55 -0500180 err = spi_calibration(bus, hz);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100181 if (err)
182 return err;
Chin Liang See36431f92015-10-17 08:31:55 -0500183
184 /* prevent calibration run when same as previous request */
185 priv->previous_hz = hz;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100186 }
187
188 /* Enable QSPI */
189 cadence_qspi_apb_controller_enable(priv->regbase);
190
191 debug("%s: speed=%d\n", __func__, hz);
192
193 return 0;
194}
195
196static int cadence_spi_probe(struct udevice *bus)
197{
Simon Glass95588622020-12-22 19:30:28 -0700198 struct cadence_spi_plat *plat = dev_get_plat(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100199 struct cadence_spi_priv *priv = dev_get_priv(bus);
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530200 struct clk clk;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100201 int ret;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100202
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600203 priv->regbase = plat->regbase;
204 priv->ahbbase = plat->ahbbase;
205 priv->is_dma = plat->is_dma;
206 priv->is_decoded_cs = plat->is_decoded_cs;
207 priv->fifo_depth = plat->fifo_depth;
208 priv->fifo_width = plat->fifo_width;
209 priv->trigger_address = plat->trigger_address;
210 priv->read_delay = plat->read_delay;
211 priv->ahbsize = plat->ahbsize;
212 priv->max_hz = plat->max_hz;
213
214 priv->page_size = plat->page_size;
215 priv->block_size = plat->block_size;
216 priv->tshsl_ns = plat->tshsl_ns;
217 priv->tsd2d_ns = plat->tsd2d_ns;
218 priv->tchsh_ns = plat->tchsh_ns;
219 priv->tslch_ns = plat->tslch_ns;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100220
Simon Glassf65db342023-02-05 15:44:33 -0700221 if (IS_ENABLED(CONFIG_ZYNQMP_FIRMWARE))
T Karthik Reddy3b49fbf2022-05-12 04:05:34 -0600222 xilinx_pm_request(PM_REQUEST_NODE, PM_DEV_OSPI,
223 ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS,
224 ZYNQMP_PM_REQUEST_ACK_NO, NULL);
225
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600226 if (priv->ref_clk_hz == 0) {
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530227 ret = clk_get_by_index(bus, 0, &clk);
228 if (ret) {
Tom Rini3fb5b2f2022-03-30 18:07:23 -0400229#ifdef CONFIG_HAS_CQSPI_REF_CLK
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600230 priv->ref_clk_hz = CONFIG_CQSPI_REF_CLK;
Tom Rini3fb5b2f2022-03-30 18:07:23 -0400231#elif defined(CONFIG_ARCH_SOCFPGA)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600232 priv->ref_clk_hz = cm_get_qspi_controller_clk_hz();
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530233#else
234 return ret;
235#endif
236 } else {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600237 priv->ref_clk_hz = clk_get_rate(&clk);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600238 if (IS_ERR_VALUE(priv->ref_clk_hz))
239 return priv->ref_clk_hz;
Pratyush Yadav5d9e7782020-02-24 12:40:51 +0530240 }
241 }
242
Christian Gmeinerd560a672022-02-22 17:23:25 +0100243 priv->resets = devm_reset_bulk_get_optional(bus);
244 if (priv->resets)
245 reset_deassert_bulk(priv->resets);
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100246
Stefan Roese1c60fe72014-11-07 12:37:49 +0100247 if (!priv->qspi_is_init) {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600248 cadence_qspi_apb_controller_init(priv);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100249 priv->qspi_is_init = 1;
250 }
251
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600252 priv->wr_delay = 50 * DIV_ROUND_UP(NSEC_PER_SEC, priv->ref_clk_hz);
Pratyush Yadav8dcf3e22021-06-26 00:47:08 +0530253
Ashok Reddy Somab6421112023-06-14 06:04:52 -0600254 /* Versal and Versal-NET use spi calibration to set read delay */
255 if (CONFIG_IS_ENABLED(ARCH_VERSAL) ||
256 CONFIG_IS_ENABLED(ARCH_VERSAL_NET))
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600257 if (priv->read_delay >= 0)
258 priv->read_delay = -1;
T Karthik Reddy3d71b2d2022-05-12 04:05:33 -0600259
Ashok Reddy Somab6421112023-06-14 06:04:52 -0600260 /* Reset ospi flash device */
261 return cadence_qspi_versal_flash_reset(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100262}
263
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100264static int cadence_spi_remove(struct udevice *dev)
265{
266 struct cadence_spi_priv *priv = dev_get_priv(dev);
Christian Gmeinerd560a672022-02-22 17:23:25 +0100267 int ret = 0;
268
269 if (priv->resets)
270 ret = reset_release_bulk(priv->resets);
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100271
Christian Gmeinerd560a672022-02-22 17:23:25 +0100272 return ret;
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100273}
274
Stefan Roese1c60fe72014-11-07 12:37:49 +0100275static int cadence_spi_set_mode(struct udevice *bus, uint mode)
276{
277 struct cadence_spi_priv *priv = dev_get_priv(bus);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100278
279 /* Disable QSPI */
280 cadence_qspi_apb_controller_disable(priv->regbase);
281
282 /* Set SPI mode */
Phil Edworthyeef2edc2016-11-29 12:58:31 +0000283 cadence_qspi_apb_set_clk_mode(priv->regbase, mode);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100284
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530285 /* Enable Direct Access Controller */
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600286 if (priv->use_dac_mode)
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530287 cadence_qspi_apb_dac_mode_enable(priv->regbase);
288
Stefan Roese1c60fe72014-11-07 12:37:49 +0100289 /* Enable QSPI */
290 cadence_qspi_apb_controller_enable(priv->regbase);
291
292 return 0;
293}
294
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530295static int cadence_spi_mem_exec_op(struct spi_slave *spi,
296 const struct spi_mem_op *op)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100297{
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530298 struct udevice *bus = spi->dev->parent;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100299 struct cadence_spi_priv *priv = dev_get_priv(bus);
300 void *base = priv->regbase;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100301 int err = 0;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530302 u32 mode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100303
304 /* Set Chip select */
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530305 cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev),
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600306 priv->is_decoded_cs);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100307
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530308 if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) {
Dhruva Gole64343d52023-01-03 12:01:12 +0530309 /*
310 * Performing reads in DAC mode forces to read minimum 4 bytes
311 * which is unsupported on some flash devices during register
312 * reads, prefer STIG mode for such small reads.
313 */
Apurva Nandan52ff9b92023-04-12 16:28:55 +0530314 if (op->data.nbytes <= CQSPI_STIG_DATA_LEN_MAX)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530315 mode = CQSPI_STIG_READ;
316 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530317 mode = CQSPI_READ;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530318 } else {
Apurva Nandan52ff9b92023-04-12 16:28:55 +0530319 if (op->data.nbytes <= CQSPI_STIG_DATA_LEN_MAX)
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530320 mode = CQSPI_STIG_WRITE;
321 else
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530322 mode = CQSPI_WRITE;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530323 }
Stefan Roese1c60fe72014-11-07 12:37:49 +0100324
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530325 switch (mode) {
326 case CQSPI_STIG_READ:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600327 err = cadence_qspi_apb_command_read_setup(priv, op);
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530328 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600329 err = cadence_qspi_apb_command_read(priv, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100330 break;
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530331 case CQSPI_STIG_WRITE:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600332 err = cadence_qspi_apb_command_write_setup(priv, op);
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530333 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600334 err = cadence_qspi_apb_command_write(priv, op);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100335 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530336 case CQSPI_READ:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600337 err = cadence_qspi_apb_read_setup(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600338 if (!err) {
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600339 if (priv->is_dma)
340 err = cadence_qspi_apb_dma_read(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600341 else
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600342 err = cadence_qspi_apb_read_execute(priv, op);
T Karthik Reddy73701e72022-05-12 04:05:32 -0600343 }
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530344 break;
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530345 case CQSPI_WRITE:
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600346 err = cadence_qspi_apb_write_setup(priv, op);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530347 if (!err)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600348 err = cadence_qspi_apb_write_execute(priv, op);
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530349 break;
350 default:
351 err = -1;
352 break;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100353 }
354
355 return err;
356}
357
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530358static bool cadence_spi_mem_supports_op(struct spi_slave *slave,
359 const struct spi_mem_op *op)
360{
361 bool all_true, all_false;
362
Apurva Nandanb88f55c2023-04-12 16:28:54 +0530363 /*
364 * op->dummy.dtr is required for converting nbytes into ncycles.
365 * Also, don't check the dtr field of the op phase having zero nbytes.
366 */
367 all_true = op->cmd.dtr &&
368 (!op->addr.nbytes || op->addr.dtr) &&
369 (!op->dummy.nbytes || op->dummy.dtr) &&
370 (!op->data.nbytes || op->data.dtr);
371
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530372 all_false = !op->cmd.dtr && !op->addr.dtr && !op->dummy.dtr &&
373 !op->data.dtr;
374
375 /* Mixed DTR modes not supported. */
376 if (!(all_true || all_false))
377 return false;
378
379 if (all_true)
380 return spi_mem_dtr_supports_op(slave, op);
381 else
382 return spi_mem_default_supports_op(slave, op);
383}
384
Simon Glassaad29ae2020-12-03 16:55:21 -0700385static int cadence_spi_of_to_plat(struct udevice *bus)
Stefan Roese1c60fe72014-11-07 12:37:49 +0100386{
Simon Glass95588622020-12-22 19:30:28 -0700387 struct cadence_spi_plat *plat = dev_get_plat(bus);
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600388 struct cadence_spi_priv *priv = dev_get_priv(bus);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200389 ofnode subnode;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100390
Johan Jonkerb52189e2023-03-13 01:32:31 +0100391 plat->regbase = devfdt_get_addr_index_ptr(bus, 0);
Johan Jonker40702782023-03-13 01:32:18 +0100392 plat->ahbbase = devfdt_get_addr_size_index_ptr(bus, 1, &plat->ahbsize);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200393 plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs");
394 plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128);
395 plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4);
396 plat->trigger_address = dev_read_u32_default(bus,
397 "cdns,trigger-address",
398 0);
Vignesh Raghavendra6b7df222020-01-27 10:36:40 +0530399 /* Use DAC mode only when MMIO window is at least 8M wide */
400 if (plat->ahbsize >= SZ_8M)
Ashok Reddy Somaf5817652022-08-24 05:38:47 -0600401 priv->use_dac_mode = true;
Stefan Roese1c60fe72014-11-07 12:37:49 +0100402
T Karthik Reddy73701e72022-05-12 04:05:32 -0600403 plat->is_dma = dev_read_bool(bus, "cdns,is-dma");
404
Pengfei Fan746271d2022-12-09 09:39:50 +0800405 /* All other parameters are embedded in the child node */
Udit Kumar88f53042023-09-12 15:20:35 +0530406 subnode = cadence_qspi_get_subnode(bus);
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200407 if (!ofnode_valid(subnode)) {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100408 printf("Error: subnode with SPI flash config missing!\n");
409 return -ENODEV;
410 }
411
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500412 /* Use 500 KHz as a suitable default */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200413 plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency",
414 500000);
Chin Liang Seef1d200f2015-10-17 08:32:14 -0500415
Stefan Roese1c60fe72014-11-07 12:37:49 +0100416 /* Read other parameters from DT */
Simon Goldschmidtf9d7d3a2019-05-09 22:11:56 +0200417 plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256);
418 plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16);
419 plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns",
420 200);
421 plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns",
422 255);
423 plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20);
424 plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20);
Pratyush Yadav8e0be9e2021-06-26 00:47:07 +0530425 /*
426 * Read delay should be an unsigned value but we use a signed integer
427 * so that negative values can indicate that the device tree did not
428 * specify any signed values and we need to perform the calibration
429 * sequence to find it out.
430 */
431 plat->read_delay = ofnode_read_s32_default(subnode, "cdns,read-delay",
432 -1);
Stefan Roese1c60fe72014-11-07 12:37:49 +0100433
434 debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n",
435 __func__, plat->regbase, plat->ahbbase, plat->max_hz,
436 plat->page_size);
437
438 return 0;
439}
440
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530441static const struct spi_controller_mem_ops cadence_spi_mem_ops = {
442 .exec_op = cadence_spi_mem_exec_op,
Pratyush Yadave1814ad2021-06-26 00:47:09 +0530443 .supports_op = cadence_spi_mem_supports_op,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530444};
445
Stefan Roese1c60fe72014-11-07 12:37:49 +0100446static const struct dm_spi_ops cadence_spi_ops = {
Stefan Roese1c60fe72014-11-07 12:37:49 +0100447 .set_speed = cadence_spi_set_speed,
448 .set_mode = cadence_spi_set_mode,
Vignesh Raghavendra27516a32020-01-27 10:36:39 +0530449 .mem_ops = &cadence_spi_mem_ops,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100450 /*
451 * cs_info is not needed, since we require all chip selects to be
452 * in the device tree explicitly
453 */
454};
455
456static const struct udevice_id cadence_spi_ids[] = {
Simon Goldschmidt454c9b32018-11-02 11:54:51 +0100457 { .compatible = "cdns,qspi-nor" },
Vignesh Raghavendra99276f02019-12-05 15:46:07 +0530458 { .compatible = "ti,am654-ospi" },
Stefan Roese1c60fe72014-11-07 12:37:49 +0100459 { }
460};
461
462U_BOOT_DRIVER(cadence_spi) = {
463 .name = "cadence_spi",
464 .id = UCLASS_SPI,
465 .of_match = cadence_spi_ids,
466 .ops = &cadence_spi_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700467 .of_to_plat = cadence_spi_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700468 .plat_auto = sizeof(struct cadence_spi_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700469 .priv_auto = sizeof(struct cadence_spi_priv),
Stefan Roese1c60fe72014-11-07 12:37:49 +0100470 .probe = cadence_spi_probe,
Simon Goldschmidt46e56a42019-03-01 20:12:35 +0100471 .remove = cadence_spi_remove,
472 .flags = DM_FLAG_OS_PREPARE,
Stefan Roese1c60fe72014-11-07 12:37:49 +0100473};