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