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