Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2012 |
| 4 | * Altera Corporation <www.altera.com> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Goldschmidt | baaa3fc | 2019-11-20 22:27:31 +0100 | [diff] [blame] | 8 | #include <clk.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 9 | #include <log.h> |
Vignesh Raghavendra | 68f8266 | 2019-12-05 15:46:06 +0530 | [diff] [blame] | 10 | #include <asm-generic/io.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <fdtdec.h> |
| 13 | #include <malloc.h> |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 14 | #include <reset.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 15 | #include <spi.h> |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 16 | #include <spi-mem.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 18 | #include <linux/err.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 19 | #include <linux/errno.h> |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 20 | #include <linux/sizes.h> |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 21 | #include <zynqmp_firmware.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 22 | #include "cadence_qspi.h" |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 23 | #include <dt-bindings/power/xlnx-versal-power.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 24 | |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 25 | #define NSEC_PER_SEC 1000000000L |
| 26 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 27 | #define CQSPI_STIG_READ 0 |
| 28 | #define CQSPI_STIG_WRITE 1 |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 29 | #define CQSPI_READ 2 |
| 30 | #define CQSPI_WRITE 3 |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 31 | |
T Karthik Reddy | 73701e7 | 2022-05-12 04:05:32 -0600 | [diff] [blame] | 32 | __weak int cadence_qspi_apb_dma_read(struct cadence_spi_plat *plat, |
| 33 | const struct spi_mem_op *op) |
| 34 | { |
| 35 | return 0; |
| 36 | } |
| 37 | |
T Karthik Reddy | 3d71b2d | 2022-05-12 04:05:33 -0600 | [diff] [blame] | 38 | __weak int cadence_qspi_versal_flash_reset(struct udevice *dev) |
| 39 | { |
| 40 | return 0; |
| 41 | } |
| 42 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 43 | static int cadence_spi_write_speed(struct udevice *bus, uint hz) |
| 44 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 45 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 46 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
| 47 | |
| 48 | cadence_qspi_apb_config_baudrate_div(priv->regbase, |
Simon Goldschmidt | baaa3fc | 2019-11-20 22:27:31 +0100 | [diff] [blame] | 49 | plat->ref_clk_hz, hz); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 50 | |
| 51 | /* Reconfigure delay timing if speed is changed. */ |
Simon Goldschmidt | baaa3fc | 2019-11-20 22:27:31 +0100 | [diff] [blame] | 52 | cadence_qspi_apb_delay(priv->regbase, plat->ref_clk_hz, hz, |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 53 | plat->tshsl_ns, plat->tsd2d_ns, |
| 54 | plat->tchsh_ns, plat->tslch_ns); |
| 55 | |
| 56 | return 0; |
| 57 | } |
| 58 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 59 | static int cadence_spi_read_id(struct cadence_spi_plat *plat, u8 len, |
| 60 | u8 *idcode) |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 61 | { |
| 62 | 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 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 67 | return cadence_qspi_apb_command_read(plat, &op); |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 68 | } |
| 69 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 70 | /* Calibration sequence to determine the read data capture delay register */ |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 71 | static int spi_calibration(struct udevice *bus, uint hz) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 72 | { |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 73 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 74 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 75 | void *base = priv->regbase; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 76 | unsigned int idcode = 0, temp = 0; |
| 77 | int err = 0, i, range_lo = -1, range_hi = -1; |
| 78 | |
| 79 | /* start with slowest clock (1 MHz) */ |
| 80 | cadence_spi_write_speed(bus, 1000000); |
| 81 | |
| 82 | /* configure the read data capture delay register to 0 */ |
| 83 | cadence_qspi_apb_readdata_capture(base, 1, 0); |
| 84 | |
| 85 | /* Enable QSPI */ |
| 86 | cadence_qspi_apb_controller_enable(base); |
| 87 | |
| 88 | /* read the ID which will be our golden value */ |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 89 | err = cadence_spi_read_id(plat, 3, (u8 *)&idcode); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 90 | if (err) { |
| 91 | puts("SF: Calibration failed (read)\n"); |
| 92 | return err; |
| 93 | } |
| 94 | |
| 95 | /* use back the intended clock and find low range */ |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 96 | cadence_spi_write_speed(bus, hz); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 97 | for (i = 0; i < CQSPI_READ_CAPTURE_MAX_DELAY; i++) { |
| 98 | /* Disable QSPI */ |
| 99 | cadence_qspi_apb_controller_disable(base); |
| 100 | |
| 101 | /* reconfigure the read data capture delay register */ |
| 102 | cadence_qspi_apb_readdata_capture(base, 1, i); |
| 103 | |
| 104 | /* Enable back QSPI */ |
| 105 | cadence_qspi_apb_controller_enable(base); |
| 106 | |
| 107 | /* issue a RDID to get the ID value */ |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 108 | err = cadence_spi_read_id(plat, 3, (u8 *)&temp); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 109 | if (err) { |
| 110 | puts("SF: Calibration failed (read)\n"); |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | /* search for range lo */ |
| 115 | if (range_lo == -1 && temp == idcode) { |
| 116 | range_lo = i; |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | /* search for range hi */ |
| 121 | if (range_lo != -1 && temp != idcode) { |
| 122 | range_hi = i - 1; |
| 123 | break; |
| 124 | } |
| 125 | range_hi = i; |
| 126 | } |
| 127 | |
| 128 | if (range_lo == -1) { |
| 129 | puts("SF: Calibration failed (low range)\n"); |
| 130 | return err; |
| 131 | } |
| 132 | |
| 133 | /* Disable QSPI for subsequent initialization */ |
| 134 | cadence_qspi_apb_controller_disable(base); |
| 135 | |
| 136 | /* configure the final value for read data capture delay register */ |
| 137 | cadence_qspi_apb_readdata_capture(base, 1, (range_hi + range_lo) / 2); |
| 138 | debug("SF: Read data capture delay calibrated to %i (%i - %i)\n", |
| 139 | (range_hi + range_lo) / 2, range_lo, range_hi); |
| 140 | |
| 141 | /* just to ensure we do once only when speed or chip select change */ |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 142 | priv->qspi_calibrated_hz = hz; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 143 | priv->qspi_calibrated_cs = spi_chip_select(bus); |
| 144 | |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | static int cadence_spi_set_speed(struct udevice *bus, uint hz) |
| 149 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 150 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 151 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
| 152 | int err; |
| 153 | |
T Karthik Reddy | b005b04 | 2022-05-12 04:05:35 -0600 | [diff] [blame] | 154 | if (!hz || hz > plat->max_hz) |
Chin Liang See | cb4ac0b | 2015-10-17 08:32:38 -0500 | [diff] [blame] | 155 | hz = plat->max_hz; |
| 156 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 157 | /* Disable QSPI */ |
| 158 | cadence_qspi_apb_controller_disable(priv->regbase); |
| 159 | |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 160 | /* |
Pratyush Yadav | 8e0be9e | 2021-06-26 00:47:07 +0530 | [diff] [blame] | 161 | * If the device tree already provides a read delay value, use that |
| 162 | * instead of calibrating. |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 163 | */ |
Pratyush Yadav | 8e0be9e | 2021-06-26 00:47:07 +0530 | [diff] [blame] | 164 | if (plat->read_delay >= 0) { |
| 165 | cadence_spi_write_speed(bus, hz); |
| 166 | cadence_qspi_apb_readdata_capture(priv->regbase, 1, |
| 167 | plat->read_delay); |
| 168 | } else if (priv->previous_hz != hz || |
| 169 | priv->qspi_calibrated_hz != hz || |
| 170 | priv->qspi_calibrated_cs != spi_chip_select(bus)) { |
| 171 | /* |
| 172 | * Calibration required for different current SCLK speed, |
| 173 | * requested SCLK speed or chip select |
| 174 | */ |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 175 | err = spi_calibration(bus, hz); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 176 | if (err) |
| 177 | return err; |
Chin Liang See | 36431f9 | 2015-10-17 08:31:55 -0500 | [diff] [blame] | 178 | |
| 179 | /* prevent calibration run when same as previous request */ |
| 180 | priv->previous_hz = hz; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /* Enable QSPI */ |
| 184 | cadence_qspi_apb_controller_enable(priv->regbase); |
| 185 | |
| 186 | debug("%s: speed=%d\n", __func__, hz); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | static int cadence_spi_probe(struct udevice *bus) |
| 192 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 193 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 194 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
Pratyush Yadav | 5d9e778 | 2020-02-24 12:40:51 +0530 | [diff] [blame] | 195 | struct clk clk; |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 196 | int ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 197 | |
| 198 | priv->regbase = plat->regbase; |
| 199 | priv->ahbbase = plat->ahbbase; |
| 200 | |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 201 | if (CONFIG_IS_ENABLED(ZYNQMP_FIRMWARE)) |
| 202 | xilinx_pm_request(PM_REQUEST_NODE, PM_DEV_OSPI, |
| 203 | ZYNQMP_PM_CAPABILITY_ACCESS, ZYNQMP_PM_MAX_QOS, |
| 204 | ZYNQMP_PM_REQUEST_ACK_NO, NULL); |
| 205 | |
Pratyush Yadav | 5d9e778 | 2020-02-24 12:40:51 +0530 | [diff] [blame] | 206 | if (plat->ref_clk_hz == 0) { |
| 207 | ret = clk_get_by_index(bus, 0, &clk); |
| 208 | if (ret) { |
Tom Rini | 3fb5b2f | 2022-03-30 18:07:23 -0400 | [diff] [blame] | 209 | #ifdef CONFIG_HAS_CQSPI_REF_CLK |
Pratyush Yadav | 5d9e778 | 2020-02-24 12:40:51 +0530 | [diff] [blame] | 210 | plat->ref_clk_hz = CONFIG_CQSPI_REF_CLK; |
Tom Rini | 3fb5b2f | 2022-03-30 18:07:23 -0400 | [diff] [blame] | 211 | #elif defined(CONFIG_ARCH_SOCFPGA) |
| 212 | plat->ref_clk_hz = cm_get_qspi_controller_clk_hz(); |
Pratyush Yadav | 5d9e778 | 2020-02-24 12:40:51 +0530 | [diff] [blame] | 213 | #else |
| 214 | return ret; |
| 215 | #endif |
| 216 | } else { |
| 217 | plat->ref_clk_hz = clk_get_rate(&clk); |
| 218 | clk_free(&clk); |
| 219 | if (IS_ERR_VALUE(plat->ref_clk_hz)) |
| 220 | return plat->ref_clk_hz; |
| 221 | } |
| 222 | } |
| 223 | |
Christian Gmeiner | d560a67 | 2022-02-22 17:23:25 +0100 | [diff] [blame] | 224 | priv->resets = devm_reset_bulk_get_optional(bus); |
| 225 | if (priv->resets) |
| 226 | reset_deassert_bulk(priv->resets); |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 227 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 228 | if (!priv->qspi_is_init) { |
| 229 | cadence_qspi_apb_controller_init(plat); |
| 230 | priv->qspi_is_init = 1; |
| 231 | } |
| 232 | |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 233 | plat->wr_delay = 50 * DIV_ROUND_UP(NSEC_PER_SEC, plat->ref_clk_hz); |
| 234 | |
T Karthik Reddy | 3d71b2d | 2022-05-12 04:05:33 -0600 | [diff] [blame] | 235 | if (CONFIG_IS_ENABLED(ARCH_VERSAL)) { |
| 236 | /* Versal platform uses spi calibration to set read delay */ |
| 237 | if (plat->read_delay >= 0) |
| 238 | plat->read_delay = -1; |
| 239 | /* Reset ospi flash device */ |
| 240 | ret = cadence_qspi_versal_flash_reset(bus); |
| 241 | if (ret) |
| 242 | return ret; |
| 243 | } |
| 244 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 245 | return 0; |
| 246 | } |
| 247 | |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 248 | static int cadence_spi_remove(struct udevice *dev) |
| 249 | { |
| 250 | struct cadence_spi_priv *priv = dev_get_priv(dev); |
Christian Gmeiner | d560a67 | 2022-02-22 17:23:25 +0100 | [diff] [blame] | 251 | int ret = 0; |
| 252 | |
| 253 | if (priv->resets) |
| 254 | ret = reset_release_bulk(priv->resets); |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 255 | |
Christian Gmeiner | d560a67 | 2022-02-22 17:23:25 +0100 | [diff] [blame] | 256 | return ret; |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 257 | } |
| 258 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 259 | static int cadence_spi_set_mode(struct udevice *bus, uint mode) |
| 260 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 261 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 262 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 263 | |
| 264 | /* Disable QSPI */ |
| 265 | cadence_qspi_apb_controller_disable(priv->regbase); |
| 266 | |
| 267 | /* Set SPI mode */ |
Phil Edworthy | eef2edc | 2016-11-29 12:58:31 +0000 | [diff] [blame] | 268 | cadence_qspi_apb_set_clk_mode(priv->regbase, mode); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 269 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 270 | /* Enable Direct Access Controller */ |
| 271 | if (plat->use_dac_mode) |
| 272 | cadence_qspi_apb_dac_mode_enable(priv->regbase); |
| 273 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 274 | /* Enable QSPI */ |
| 275 | cadence_qspi_apb_controller_enable(priv->regbase); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 280 | static int cadence_spi_mem_exec_op(struct spi_slave *spi, |
| 281 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 282 | { |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 283 | struct udevice *bus = spi->dev->parent; |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 284 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 285 | struct cadence_spi_priv *priv = dev_get_priv(bus); |
| 286 | void *base = priv->regbase; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 287 | int err = 0; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 288 | u32 mode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 289 | |
| 290 | /* Set Chip select */ |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 291 | cadence_qspi_apb_chipselect(base, spi_chip_select(spi->dev), |
Jason Rush | 1b4df5e | 2018-01-23 17:13:09 -0600 | [diff] [blame] | 292 | plat->is_decoded_cs); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 293 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 294 | if (op->data.dir == SPI_MEM_DATA_IN && op->data.buf.in) { |
| 295 | if (!op->addr.nbytes) |
| 296 | mode = CQSPI_STIG_READ; |
| 297 | else |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 298 | mode = CQSPI_READ; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 299 | } else { |
| 300 | if (!op->addr.nbytes || !op->data.buf.out) |
| 301 | mode = CQSPI_STIG_WRITE; |
| 302 | else |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 303 | mode = CQSPI_WRITE; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 304 | } |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 305 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 306 | switch (mode) { |
| 307 | case CQSPI_STIG_READ: |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 308 | err = cadence_qspi_apb_command_read_setup(plat, op); |
| 309 | if (!err) |
| 310 | err = cadence_qspi_apb_command_read(plat, op); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 311 | break; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 312 | case CQSPI_STIG_WRITE: |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 313 | err = cadence_qspi_apb_command_write_setup(plat, op); |
| 314 | if (!err) |
| 315 | err = cadence_qspi_apb_command_write(plat, op); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 316 | break; |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 317 | case CQSPI_READ: |
| 318 | err = cadence_qspi_apb_read_setup(plat, op); |
T Karthik Reddy | 73701e7 | 2022-05-12 04:05:32 -0600 | [diff] [blame] | 319 | if (!err) { |
| 320 | if (plat->is_dma) |
| 321 | err = cadence_qspi_apb_dma_read(plat, op); |
| 322 | else |
| 323 | err = cadence_qspi_apb_read_execute(plat, op); |
| 324 | } |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 325 | break; |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 326 | case CQSPI_WRITE: |
| 327 | err = cadence_qspi_apb_write_setup(plat, op); |
| 328 | if (!err) |
| 329 | err = cadence_qspi_apb_write_execute(plat, op); |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 330 | break; |
| 331 | default: |
| 332 | err = -1; |
| 333 | break; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | return err; |
| 337 | } |
| 338 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 339 | static bool cadence_spi_mem_supports_op(struct spi_slave *slave, |
| 340 | const struct spi_mem_op *op) |
| 341 | { |
| 342 | bool all_true, all_false; |
| 343 | |
| 344 | all_true = op->cmd.dtr && op->addr.dtr && op->dummy.dtr && |
| 345 | op->data.dtr; |
| 346 | all_false = !op->cmd.dtr && !op->addr.dtr && !op->dummy.dtr && |
| 347 | !op->data.dtr; |
| 348 | |
| 349 | /* Mixed DTR modes not supported. */ |
| 350 | if (!(all_true || all_false)) |
| 351 | return false; |
| 352 | |
| 353 | if (all_true) |
| 354 | return spi_mem_dtr_supports_op(slave, op); |
| 355 | else |
| 356 | return spi_mem_default_supports_op(slave, op); |
| 357 | } |
| 358 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 359 | static int cadence_spi_of_to_plat(struct udevice *bus) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 360 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 361 | struct cadence_spi_plat *plat = dev_get_plat(bus); |
Simon Goldschmidt | f9d7d3a | 2019-05-09 22:11:56 +0200 | [diff] [blame] | 362 | ofnode subnode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 363 | |
Ley Foon Tan | 3bca8f5 | 2018-05-07 17:42:55 +0800 | [diff] [blame] | 364 | plat->regbase = (void *)devfdt_get_addr_index(bus, 0); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 365 | plat->ahbbase = (void *)devfdt_get_addr_size_index(bus, 1, |
| 366 | &plat->ahbsize); |
Simon Goldschmidt | f9d7d3a | 2019-05-09 22:11:56 +0200 | [diff] [blame] | 367 | plat->is_decoded_cs = dev_read_bool(bus, "cdns,is-decoded-cs"); |
| 368 | plat->fifo_depth = dev_read_u32_default(bus, "cdns,fifo-depth", 128); |
| 369 | plat->fifo_width = dev_read_u32_default(bus, "cdns,fifo-width", 4); |
| 370 | plat->trigger_address = dev_read_u32_default(bus, |
| 371 | "cdns,trigger-address", |
| 372 | 0); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 373 | /* Use DAC mode only when MMIO window is at least 8M wide */ |
| 374 | if (plat->ahbsize >= SZ_8M) |
| 375 | plat->use_dac_mode = true; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 376 | |
T Karthik Reddy | 73701e7 | 2022-05-12 04:05:32 -0600 | [diff] [blame] | 377 | plat->is_dma = dev_read_bool(bus, "cdns,is-dma"); |
| 378 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 379 | /* All other paramters are embedded in the child node */ |
Simon Goldschmidt | f9d7d3a | 2019-05-09 22:11:56 +0200 | [diff] [blame] | 380 | subnode = dev_read_first_subnode(bus); |
| 381 | if (!ofnode_valid(subnode)) { |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 382 | printf("Error: subnode with SPI flash config missing!\n"); |
| 383 | return -ENODEV; |
| 384 | } |
| 385 | |
Chin Liang See | f1d200f | 2015-10-17 08:32:14 -0500 | [diff] [blame] | 386 | /* Use 500 KHz as a suitable default */ |
Simon Goldschmidt | f9d7d3a | 2019-05-09 22:11:56 +0200 | [diff] [blame] | 387 | plat->max_hz = ofnode_read_u32_default(subnode, "spi-max-frequency", |
| 388 | 500000); |
Chin Liang See | f1d200f | 2015-10-17 08:32:14 -0500 | [diff] [blame] | 389 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 390 | /* Read other parameters from DT */ |
Simon Goldschmidt | f9d7d3a | 2019-05-09 22:11:56 +0200 | [diff] [blame] | 391 | plat->page_size = ofnode_read_u32_default(subnode, "page-size", 256); |
| 392 | plat->block_size = ofnode_read_u32_default(subnode, "block-size", 16); |
| 393 | plat->tshsl_ns = ofnode_read_u32_default(subnode, "cdns,tshsl-ns", |
| 394 | 200); |
| 395 | plat->tsd2d_ns = ofnode_read_u32_default(subnode, "cdns,tsd2d-ns", |
| 396 | 255); |
| 397 | plat->tchsh_ns = ofnode_read_u32_default(subnode, "cdns,tchsh-ns", 20); |
| 398 | plat->tslch_ns = ofnode_read_u32_default(subnode, "cdns,tslch-ns", 20); |
Pratyush Yadav | 8e0be9e | 2021-06-26 00:47:07 +0530 | [diff] [blame] | 399 | /* |
| 400 | * Read delay should be an unsigned value but we use a signed integer |
| 401 | * so that negative values can indicate that the device tree did not |
| 402 | * specify any signed values and we need to perform the calibration |
| 403 | * sequence to find it out. |
| 404 | */ |
| 405 | plat->read_delay = ofnode_read_s32_default(subnode, "cdns,read-delay", |
| 406 | -1); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 407 | |
| 408 | debug("%s: regbase=%p ahbbase=%p max-frequency=%d page-size=%d\n", |
| 409 | __func__, plat->regbase, plat->ahbbase, plat->max_hz, |
| 410 | plat->page_size); |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 415 | static const struct spi_controller_mem_ops cadence_spi_mem_ops = { |
| 416 | .exec_op = cadence_spi_mem_exec_op, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 417 | .supports_op = cadence_spi_mem_supports_op, |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 418 | }; |
| 419 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 420 | static const struct dm_spi_ops cadence_spi_ops = { |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 421 | .set_speed = cadence_spi_set_speed, |
| 422 | .set_mode = cadence_spi_set_mode, |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 423 | .mem_ops = &cadence_spi_mem_ops, |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 424 | /* |
| 425 | * cs_info is not needed, since we require all chip selects to be |
| 426 | * in the device tree explicitly |
| 427 | */ |
| 428 | }; |
| 429 | |
| 430 | static const struct udevice_id cadence_spi_ids[] = { |
Simon Goldschmidt | 454c9b3 | 2018-11-02 11:54:51 +0100 | [diff] [blame] | 431 | { .compatible = "cdns,qspi-nor" }, |
Vignesh Raghavendra | 99276f0 | 2019-12-05 15:46:07 +0530 | [diff] [blame] | 432 | { .compatible = "ti,am654-ospi" }, |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 433 | { } |
| 434 | }; |
| 435 | |
| 436 | U_BOOT_DRIVER(cadence_spi) = { |
| 437 | .name = "cadence_spi", |
| 438 | .id = UCLASS_SPI, |
| 439 | .of_match = cadence_spi_ids, |
| 440 | .ops = &cadence_spi_ops, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 441 | .of_to_plat = cadence_spi_of_to_plat, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 442 | .plat_auto = sizeof(struct cadence_spi_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 443 | .priv_auto = sizeof(struct cadence_spi_priv), |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 444 | .probe = cadence_spi_probe, |
Simon Goldschmidt | 46e56a4 | 2019-03-01 20:12:35 +0100 | [diff] [blame] | 445 | .remove = cadence_spi_remove, |
| 446 | .flags = DM_FLAG_OS_PREPARE, |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 447 | }; |