Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Altera Corporation <www.altera.com> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * - Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * - Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * - Neither the name of the Altera Corporation nor the |
| 13 | * names of its contributors may be used to endorse or promote products |
| 14 | * derived from this software without specific prior written permission. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 19 | * ARE DISCLAIMED. IN NO EVENT SHALL ALTERA CORPORATION BE LIABLE FOR ANY |
| 20 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 25 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 29 | #include <log.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 30 | #include <asm/io.h> |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 31 | #include <dma.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 32 | #include <linux/bitops.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 33 | #include <linux/delay.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 34 | #include <linux/errno.h> |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 35 | #include <wait_bit.h> |
Vignesh R | 4ca6019 | 2016-07-06 10:20:56 +0530 | [diff] [blame] | 36 | #include <spi.h> |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 37 | #include <spi-mem.h> |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 38 | #include <malloc.h> |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 39 | #include "cadence_qspi.h" |
| 40 | |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 41 | __weak void cadence_qspi_apb_enable_linear_mode(bool enable) |
| 42 | { |
| 43 | return; |
| 44 | } |
| 45 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 46 | void cadence_qspi_apb_controller_enable(void *reg_base) |
| 47 | { |
| 48 | unsigned int reg; |
| 49 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 50 | reg |= CQSPI_REG_CONFIG_ENABLE; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 51 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void cadence_qspi_apb_controller_disable(void *reg_base) |
| 55 | { |
| 56 | unsigned int reg; |
| 57 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 58 | reg &= ~CQSPI_REG_CONFIG_ENABLE; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 59 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 62 | void cadence_qspi_apb_dac_mode_enable(void *reg_base) |
| 63 | { |
| 64 | unsigned int reg; |
| 65 | |
| 66 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
| 67 | reg |= CQSPI_REG_CONFIG_DIRECT; |
| 68 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 69 | } |
| 70 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 71 | static unsigned int cadence_qspi_calc_dummy(const struct spi_mem_op *op, |
| 72 | bool dtr) |
| 73 | { |
| 74 | unsigned int dummy_clk; |
| 75 | |
Marek Vasut | 545be19 | 2021-09-14 05:21:48 +0200 | [diff] [blame] | 76 | if (!op->dummy.nbytes || !op->dummy.buswidth) |
| 77 | return 0; |
| 78 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 79 | dummy_clk = op->dummy.nbytes * (8 / op->dummy.buswidth); |
| 80 | if (dtr) |
| 81 | dummy_clk /= 2; |
| 82 | |
| 83 | return dummy_clk; |
| 84 | } |
| 85 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 86 | static u32 cadence_qspi_calc_rdreg(struct cadence_spi_priv *priv) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 87 | { |
| 88 | u32 rdreg = 0; |
| 89 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 90 | rdreg |= priv->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB; |
| 91 | rdreg |= priv->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB; |
| 92 | rdreg |= priv->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 93 | |
| 94 | return rdreg; |
| 95 | } |
| 96 | |
| 97 | static int cadence_qspi_buswidth_to_inst_type(u8 buswidth) |
| 98 | { |
| 99 | switch (buswidth) { |
| 100 | case 0: |
| 101 | case 1: |
| 102 | return CQSPI_INST_TYPE_SINGLE; |
| 103 | |
| 104 | case 2: |
| 105 | return CQSPI_INST_TYPE_DUAL; |
| 106 | |
| 107 | case 4: |
| 108 | return CQSPI_INST_TYPE_QUAD; |
| 109 | |
| 110 | case 8: |
| 111 | return CQSPI_INST_TYPE_OCTAL; |
| 112 | |
| 113 | default: |
| 114 | return -ENOTSUPP; |
| 115 | } |
| 116 | } |
| 117 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 118 | static int cadence_qspi_set_protocol(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 119 | const struct spi_mem_op *op) |
| 120 | { |
| 121 | int ret; |
| 122 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 123 | priv->dtr = op->data.dtr && op->cmd.dtr && op->addr.dtr; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 124 | |
| 125 | ret = cadence_qspi_buswidth_to_inst_type(op->cmd.buswidth); |
| 126 | if (ret < 0) |
| 127 | return ret; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 128 | priv->inst_width = ret; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 129 | |
| 130 | ret = cadence_qspi_buswidth_to_inst_type(op->addr.buswidth); |
| 131 | if (ret < 0) |
| 132 | return ret; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 133 | priv->addr_width = ret; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 134 | |
| 135 | ret = cadence_qspi_buswidth_to_inst_type(op->data.buswidth); |
| 136 | if (ret < 0) |
| 137 | return ret; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 138 | priv->data_width = ret; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 143 | /* Return 1 if idle, otherwise return 0 (busy). */ |
| 144 | static unsigned int cadence_qspi_wait_idle(void *reg_base) |
| 145 | { |
| 146 | unsigned int start, count = 0; |
| 147 | /* timeout in unit of ms */ |
| 148 | unsigned int timeout = 5000; |
| 149 | |
| 150 | start = get_timer(0); |
| 151 | for ( ; get_timer(start) < timeout ; ) { |
| 152 | if (CQSPI_REG_IS_IDLE(reg_base)) |
| 153 | count++; |
| 154 | else |
| 155 | count = 0; |
| 156 | /* |
| 157 | * Ensure the QSPI controller is in true idle state after |
| 158 | * reading back the same idle status consecutively |
| 159 | */ |
| 160 | if (count >= CQSPI_POLL_IDLE_RETRY) |
| 161 | return 1; |
| 162 | } |
| 163 | |
| 164 | /* Timeout, still in busy mode. */ |
| 165 | printf("QSPI: QSPI is still busy after poll for %d times.\n", |
| 166 | CQSPI_REG_RETRY); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | void cadence_qspi_apb_readdata_capture(void *reg_base, |
| 171 | unsigned int bypass, unsigned int delay) |
| 172 | { |
| 173 | unsigned int reg; |
| 174 | cadence_qspi_apb_controller_disable(reg_base); |
| 175 | |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 176 | reg = readl(reg_base + CQSPI_REG_RD_DATA_CAPTURE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 177 | |
| 178 | if (bypass) |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 179 | reg |= CQSPI_REG_RD_DATA_CAPTURE_BYPASS; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 180 | else |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 181 | reg &= ~CQSPI_REG_RD_DATA_CAPTURE_BYPASS; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 182 | |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 183 | reg &= ~(CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK |
| 184 | << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 185 | |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 186 | reg |= (delay & CQSPI_REG_RD_DATA_CAPTURE_DELAY_MASK) |
| 187 | << CQSPI_REG_RD_DATA_CAPTURE_DELAY_LSB; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 188 | |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 189 | writel(reg, reg_base + CQSPI_REG_RD_DATA_CAPTURE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 190 | |
| 191 | cadence_qspi_apb_controller_enable(reg_base); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | void cadence_qspi_apb_config_baudrate_div(void *reg_base, |
| 195 | unsigned int ref_clk_hz, unsigned int sclk_hz) |
| 196 | { |
| 197 | unsigned int reg; |
| 198 | unsigned int div; |
| 199 | |
| 200 | cadence_qspi_apb_controller_disable(reg_base); |
| 201 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
| 202 | reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB); |
| 203 | |
Phil Edworthy | 8f24a44 | 2016-11-29 12:58:27 +0000 | [diff] [blame] | 204 | /* |
| 205 | * The baud_div field in the config reg is 4 bits, and the ref clock is |
| 206 | * divided by 2 * (baud_div + 1). Round up the divider to ensure the |
| 207 | * SPI clock rate is less than or equal to the requested clock rate. |
| 208 | */ |
| 209 | div = DIV_ROUND_UP(ref_clk_hz, sclk_hz * 2) - 1; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 210 | |
Chin Liang See | 91b2c19 | 2016-08-07 22:50:40 +0800 | [diff] [blame] | 211 | /* ensure the baud rate doesn't exceed the max value */ |
| 212 | if (div > CQSPI_REG_CONFIG_BAUD_MASK) |
| 213 | div = CQSPI_REG_CONFIG_BAUD_MASK; |
| 214 | |
Phil Edworthy | 67824ad | 2016-11-29 12:58:28 +0000 | [diff] [blame] | 215 | debug("%s: ref_clk %dHz sclk %dHz Div 0x%x, actual %dHz\n", __func__, |
| 216 | ref_clk_hz, sclk_hz, div, ref_clk_hz / (2 * (div + 1))); |
| 217 | |
Chin Liang See | 91b2c19 | 2016-08-07 22:50:40 +0800 | [diff] [blame] | 218 | reg |= (div << CQSPI_REG_CONFIG_BAUD_LSB); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 219 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 220 | |
| 221 | cadence_qspi_apb_controller_enable(reg_base); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Phil Edworthy | eef2edc | 2016-11-29 12:58:31 +0000 | [diff] [blame] | 224 | void cadence_qspi_apb_set_clk_mode(void *reg_base, uint mode) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 225 | { |
| 226 | unsigned int reg; |
| 227 | |
| 228 | cadence_qspi_apb_controller_disable(reg_base); |
| 229 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 230 | reg &= ~(CQSPI_REG_CONFIG_CLK_POL | CQSPI_REG_CONFIG_CLK_PHA); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 231 | |
Phil Edworthy | eef2edc | 2016-11-29 12:58:31 +0000 | [diff] [blame] | 232 | if (mode & SPI_CPOL) |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 233 | reg |= CQSPI_REG_CONFIG_CLK_POL; |
Phil Edworthy | eef2edc | 2016-11-29 12:58:31 +0000 | [diff] [blame] | 234 | if (mode & SPI_CPHA) |
Phil Edworthy | dd18c6f | 2016-11-29 12:58:29 +0000 | [diff] [blame] | 235 | reg |= CQSPI_REG_CONFIG_CLK_PHA; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 236 | |
| 237 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 238 | |
| 239 | cadence_qspi_apb_controller_enable(reg_base); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void cadence_qspi_apb_chipselect(void *reg_base, |
| 243 | unsigned int chip_select, unsigned int decoder_enable) |
| 244 | { |
| 245 | unsigned int reg; |
| 246 | |
| 247 | cadence_qspi_apb_controller_disable(reg_base); |
| 248 | |
| 249 | debug("%s : chipselect %d decode %d\n", __func__, chip_select, |
| 250 | decoder_enable); |
| 251 | |
| 252 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
| 253 | /* docoder */ |
| 254 | if (decoder_enable) { |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 255 | reg |= CQSPI_REG_CONFIG_DECODE; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 256 | } else { |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 257 | reg &= ~CQSPI_REG_CONFIG_DECODE; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 258 | /* Convert CS if without decoder. |
| 259 | * CS0 to 4b'1110 |
| 260 | * CS1 to 4b'1101 |
| 261 | * CS2 to 4b'1011 |
| 262 | * CS3 to 4b'0111 |
| 263 | */ |
| 264 | chip_select = 0xF & ~(1 << chip_select); |
| 265 | } |
| 266 | |
| 267 | reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK |
| 268 | << CQSPI_REG_CONFIG_CHIPSELECT_LSB); |
| 269 | reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK) |
| 270 | << CQSPI_REG_CONFIG_CHIPSELECT_LSB; |
| 271 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 272 | |
| 273 | cadence_qspi_apb_controller_enable(reg_base); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void cadence_qspi_apb_delay(void *reg_base, |
| 277 | unsigned int ref_clk, unsigned int sclk_hz, |
| 278 | unsigned int tshsl_ns, unsigned int tsd2d_ns, |
| 279 | unsigned int tchsh_ns, unsigned int tslch_ns) |
| 280 | { |
| 281 | unsigned int ref_clk_ns; |
| 282 | unsigned int sclk_ns; |
| 283 | unsigned int tshsl, tchsh, tslch, tsd2d; |
| 284 | unsigned int reg; |
| 285 | |
| 286 | cadence_qspi_apb_controller_disable(reg_base); |
| 287 | |
| 288 | /* Convert to ns. */ |
Phil Edworthy | 1fdd923 | 2016-11-29 12:58:33 +0000 | [diff] [blame] | 289 | ref_clk_ns = DIV_ROUND_UP(1000000000, ref_clk); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 290 | |
| 291 | /* Convert to ns. */ |
Phil Edworthy | 1fdd923 | 2016-11-29 12:58:33 +0000 | [diff] [blame] | 292 | sclk_ns = DIV_ROUND_UP(1000000000, sclk_hz); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 293 | |
Phil Edworthy | 1fdd923 | 2016-11-29 12:58:33 +0000 | [diff] [blame] | 294 | /* The controller adds additional delay to that programmed in the reg */ |
| 295 | if (tshsl_ns >= sclk_ns + ref_clk_ns) |
| 296 | tshsl_ns -= sclk_ns + ref_clk_ns; |
| 297 | if (tchsh_ns >= sclk_ns + 3 * ref_clk_ns) |
| 298 | tchsh_ns -= sclk_ns + 3 * ref_clk_ns; |
| 299 | tshsl = DIV_ROUND_UP(tshsl_ns, ref_clk_ns); |
| 300 | tchsh = DIV_ROUND_UP(tchsh_ns, ref_clk_ns); |
| 301 | tslch = DIV_ROUND_UP(tslch_ns, ref_clk_ns); |
| 302 | tsd2d = DIV_ROUND_UP(tsd2d_ns, ref_clk_ns); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 303 | |
| 304 | reg = ((tshsl & CQSPI_REG_DELAY_TSHSL_MASK) |
| 305 | << CQSPI_REG_DELAY_TSHSL_LSB); |
| 306 | reg |= ((tchsh & CQSPI_REG_DELAY_TCHSH_MASK) |
| 307 | << CQSPI_REG_DELAY_TCHSH_LSB); |
| 308 | reg |= ((tslch & CQSPI_REG_DELAY_TSLCH_MASK) |
| 309 | << CQSPI_REG_DELAY_TSLCH_LSB); |
| 310 | reg |= ((tsd2d & CQSPI_REG_DELAY_TSD2D_MASK) |
| 311 | << CQSPI_REG_DELAY_TSD2D_LSB); |
| 312 | writel(reg, reg_base + CQSPI_REG_DELAY); |
| 313 | |
| 314 | cadence_qspi_apb_controller_enable(reg_base); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 315 | } |
| 316 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 317 | void cadence_qspi_apb_controller_init(struct cadence_spi_priv *priv) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 318 | { |
| 319 | unsigned reg; |
| 320 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 321 | cadence_qspi_apb_controller_disable(priv->regbase); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 322 | |
| 323 | /* Configure the device size and address bytes */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 324 | reg = readl(priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 325 | /* Clear the previous value */ |
| 326 | reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB); |
| 327 | reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB); |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 328 | reg |= (priv->page_size << CQSPI_REG_SIZE_PAGE_LSB); |
| 329 | reg |= (priv->block_size << CQSPI_REG_SIZE_BLOCK_LSB); |
| 330 | writel(reg, priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 331 | |
| 332 | /* Configure the remap address register, no remap */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 333 | writel(0, priv->regbase + CQSPI_REG_REMAP); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 334 | |
Vikas Manocha | 215cea0 | 2015-07-02 18:29:43 -0700 | [diff] [blame] | 335 | /* Indirect mode configurations */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 336 | writel(priv->fifo_depth / 2, priv->regbase + CQSPI_REG_SRAMPARTITION); |
Vikas Manocha | 215cea0 | 2015-07-02 18:29:43 -0700 | [diff] [blame] | 337 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 338 | /* Disable all interrupts */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 339 | writel(0, priv->regbase + CQSPI_REG_IRQMASK); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 340 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 341 | cadence_qspi_apb_controller_enable(priv->regbase); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 342 | } |
| 343 | |
T Karthik Reddy | 73701e7 | 2022-05-12 04:05:32 -0600 | [diff] [blame] | 344 | int cadence_qspi_apb_exec_flash_cmd(void *reg_base, unsigned int reg) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 345 | { |
| 346 | unsigned int retry = CQSPI_REG_RETRY; |
| 347 | |
| 348 | /* Write the CMDCTRL without start execution. */ |
| 349 | writel(reg, reg_base + CQSPI_REG_CMDCTRL); |
| 350 | /* Start execute */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 351 | reg |= CQSPI_REG_CMDCTRL_EXECUTE; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 352 | writel(reg, reg_base + CQSPI_REG_CMDCTRL); |
| 353 | |
| 354 | while (retry--) { |
| 355 | reg = readl(reg_base + CQSPI_REG_CMDCTRL); |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 356 | if ((reg & CQSPI_REG_CMDCTRL_INPROGRESS) == 0) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 357 | break; |
| 358 | udelay(1); |
| 359 | } |
| 360 | |
| 361 | if (!retry) { |
| 362 | printf("QSPI: flash command execution timeout\n"); |
| 363 | return -EIO; |
| 364 | } |
| 365 | |
| 366 | /* Polling QSPI idle status. */ |
| 367 | if (!cadence_qspi_wait_idle(reg_base)) |
| 368 | return -EIO; |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 373 | static int cadence_qspi_setup_opcode_ext(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 374 | const struct spi_mem_op *op, |
| 375 | unsigned int shift) |
| 376 | { |
| 377 | unsigned int reg; |
| 378 | u8 ext; |
| 379 | |
| 380 | if (op->cmd.nbytes != 2) |
| 381 | return -EINVAL; |
| 382 | |
| 383 | /* Opcode extension is the LSB. */ |
| 384 | ext = op->cmd.opcode & 0xff; |
| 385 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 386 | reg = readl(priv->regbase + CQSPI_REG_OP_EXT_LOWER); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 387 | reg &= ~(0xff << shift); |
| 388 | reg |= ext << shift; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 389 | writel(reg, priv->regbase + CQSPI_REG_OP_EXT_LOWER); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 394 | static int cadence_qspi_enable_dtr(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 395 | const struct spi_mem_op *op, |
| 396 | unsigned int shift, |
| 397 | bool enable) |
| 398 | { |
| 399 | unsigned int reg; |
| 400 | int ret; |
| 401 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 402 | reg = readl(priv->regbase + CQSPI_REG_CONFIG); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 403 | |
| 404 | if (enable) { |
| 405 | reg |= CQSPI_REG_CONFIG_DTR_PROTO; |
| 406 | reg |= CQSPI_REG_CONFIG_DUAL_OPCODE; |
| 407 | |
| 408 | /* Set up command opcode extension. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 409 | ret = cadence_qspi_setup_opcode_ext(priv, op, shift); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 410 | if (ret) |
| 411 | return ret; |
| 412 | } else { |
| 413 | reg &= ~CQSPI_REG_CONFIG_DTR_PROTO; |
| 414 | reg &= ~CQSPI_REG_CONFIG_DUAL_OPCODE; |
| 415 | } |
| 416 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 417 | writel(reg, priv->regbase + CQSPI_REG_CONFIG); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 418 | |
| 419 | return 0; |
| 420 | } |
| 421 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 422 | int cadence_qspi_apb_command_read_setup(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 423 | const struct spi_mem_op *op) |
| 424 | { |
| 425 | int ret; |
| 426 | unsigned int reg; |
| 427 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 428 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 429 | if (ret) |
| 430 | return ret; |
| 431 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 432 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_STIG_LSB, |
| 433 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 434 | if (ret) |
| 435 | return ret; |
| 436 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 437 | reg = cadence_qspi_calc_rdreg(priv); |
| 438 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 439 | |
| 440 | return 0; |
| 441 | } |
| 442 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 443 | /* For command RDID, RDSR. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 444 | int cadence_qspi_apb_command_read(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 445 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 446 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 447 | void *reg_base = priv->regbase; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 448 | unsigned int reg; |
| 449 | unsigned int read_len; |
| 450 | int status; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 451 | unsigned int rxlen = op->data.nbytes; |
| 452 | void *rxbuf = op->data.buf.in; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 453 | unsigned int dummy_clk; |
| 454 | u8 opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 455 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 456 | if (rxlen > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) { |
| 457 | printf("QSPI: Invalid input arguments rxlen %u\n", rxlen); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 458 | return -EINVAL; |
| 459 | } |
| 460 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 461 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 462 | opcode = op->cmd.opcode >> 8; |
| 463 | else |
| 464 | opcode = op->cmd.opcode; |
| 465 | |
| 466 | reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB; |
| 467 | |
| 468 | /* Set up dummy cycles. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 469 | dummy_clk = cadence_qspi_calc_dummy(op, priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 470 | if (dummy_clk > CQSPI_DUMMY_CLKS_MAX) |
| 471 | return -ENOTSUPP; |
| 472 | |
| 473 | if (dummy_clk) |
| 474 | reg |= (dummy_clk & CQSPI_REG_CMDCTRL_DUMMY_MASK) |
| 475 | << CQSPI_REG_CMDCTRL_DUMMY_LSB; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 476 | |
| 477 | reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB); |
| 478 | |
| 479 | /* 0 means 1 byte. */ |
| 480 | reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK) |
| 481 | << CQSPI_REG_CMDCTRL_RD_BYTES_LSB); |
Dhruva Gole | 24d8de6 | 2023-01-03 12:01:11 +0530 | [diff] [blame^] | 482 | |
| 483 | /* setup ADDR BIT field */ |
| 484 | if (op->addr.nbytes) { |
| 485 | writel(op->addr.val, priv->regbase + CQSPI_REG_CMDADDRESS); |
| 486 | /* |
| 487 | * address bytes are zero indexed |
| 488 | */ |
| 489 | reg |= (((op->addr.nbytes - 1) & |
| 490 | CQSPI_REG_CMDCTRL_ADD_BYTES_MASK) << |
| 491 | CQSPI_REG_CMDCTRL_ADD_BYTES_LSB); |
| 492 | reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB); |
| 493 | } |
| 494 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 495 | status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg); |
| 496 | if (status != 0) |
| 497 | return status; |
| 498 | |
| 499 | reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER); |
| 500 | |
| 501 | /* Put the read value into rx_buf */ |
| 502 | read_len = (rxlen > 4) ? 4 : rxlen; |
| 503 | memcpy(rxbuf, ®, read_len); |
| 504 | rxbuf += read_len; |
| 505 | |
| 506 | if (rxlen > 4) { |
| 507 | reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER); |
| 508 | |
| 509 | read_len = rxlen - read_len; |
| 510 | memcpy(rxbuf, ®, read_len); |
| 511 | } |
| 512 | return 0; |
| 513 | } |
| 514 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 515 | int cadence_qspi_apb_command_write_setup(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 516 | const struct spi_mem_op *op) |
| 517 | { |
| 518 | int ret; |
| 519 | unsigned int reg; |
| 520 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 521 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 522 | if (ret) |
| 523 | return ret; |
| 524 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 525 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_STIG_LSB, |
| 526 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 527 | if (ret) |
| 528 | return ret; |
| 529 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 530 | reg = cadence_qspi_calc_rdreg(priv); |
| 531 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 532 | |
| 533 | return 0; |
| 534 | } |
| 535 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 536 | /* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 537 | int cadence_qspi_apb_command_write(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 538 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 539 | { |
| 540 | unsigned int reg = 0; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 541 | unsigned int wr_data; |
| 542 | unsigned int wr_len; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 543 | unsigned int txlen = op->data.nbytes; |
| 544 | const void *txbuf = op->data.buf.out; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 545 | void *reg_base = priv->regbase; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 546 | u32 addr; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 547 | u8 opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 548 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 549 | /* Reorder address to SPI bus order if only transferring address */ |
| 550 | if (!txlen) { |
| 551 | addr = cpu_to_be32(op->addr.val); |
| 552 | if (op->addr.nbytes == 3) |
| 553 | addr >>= 8; |
| 554 | txbuf = &addr; |
| 555 | txlen = op->addr.nbytes; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 558 | if (txlen > CQSPI_STIG_DATA_LEN_MAX) { |
| 559 | printf("QSPI: Invalid input arguments txlen %u\n", txlen); |
| 560 | return -EINVAL; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 563 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 564 | opcode = op->cmd.opcode >> 8; |
| 565 | else |
| 566 | opcode = op->cmd.opcode; |
| 567 | |
| 568 | reg |= opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 569 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 570 | if (txlen) { |
| 571 | /* writing data = yes */ |
| 572 | reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB); |
| 573 | reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK) |
| 574 | << CQSPI_REG_CMDCTRL_WR_BYTES_LSB; |
| 575 | |
| 576 | wr_len = txlen > 4 ? 4 : txlen; |
| 577 | memcpy(&wr_data, txbuf, wr_len); |
| 578 | writel(wr_data, reg_base + |
| 579 | CQSPI_REG_CMDWRITEDATALOWER); |
| 580 | |
| 581 | if (txlen > 4) { |
| 582 | txbuf += wr_len; |
| 583 | wr_len = txlen - wr_len; |
| 584 | memcpy(&wr_data, txbuf, wr_len); |
| 585 | writel(wr_data, reg_base + |
| 586 | CQSPI_REG_CMDWRITEDATAUPPER); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* Execute the command */ |
| 591 | return cadence_qspi_apb_exec_flash_cmd(reg_base, reg); |
| 592 | } |
| 593 | |
| 594 | /* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 595 | int cadence_qspi_apb_read_setup(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 596 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 597 | { |
| 598 | unsigned int reg; |
| 599 | unsigned int rd_reg; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 600 | unsigned int dummy_clk; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 601 | unsigned int dummy_bytes = op->dummy.nbytes; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 602 | int ret; |
| 603 | u8 opcode; |
| 604 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 605 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 606 | if (ret) |
| 607 | return ret; |
| 608 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 609 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_READ_LSB, |
| 610 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 611 | if (ret) |
| 612 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 613 | |
| 614 | /* Setup the indirect trigger address */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 615 | writel(priv->trigger_address, |
| 616 | priv->regbase + CQSPI_REG_INDIRECTTRIGGER); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 617 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 618 | /* Configure the opcode */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 619 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 620 | opcode = op->cmd.opcode >> 8; |
| 621 | else |
| 622 | opcode = op->cmd.opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 623 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 624 | rd_reg = opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 625 | rd_reg |= cadence_qspi_calc_rdreg(priv); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 626 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 627 | writel(op->addr.val, priv->regbase + CQSPI_REG_INDIRECTRDSTARTADDR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 628 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 629 | if (dummy_bytes) { |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 630 | /* Convert to clock cycles. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 631 | dummy_clk = cadence_qspi_calc_dummy(op, priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 632 | |
| 633 | if (dummy_clk > CQSPI_DUMMY_CLKS_MAX) |
| 634 | return -ENOTSUPP; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 635 | |
| 636 | if (dummy_clk) |
| 637 | rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK) |
| 638 | << CQSPI_REG_RD_INSTR_DUMMY_LSB; |
| 639 | } |
| 640 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 641 | writel(rd_reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 642 | |
| 643 | /* set device size */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 644 | reg = readl(priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 645 | reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 646 | reg |= (op->addr.nbytes - 1); |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 647 | writel(reg, priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 648 | return 0; |
| 649 | } |
| 650 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 651 | static u32 cadence_qspi_get_rd_sram_level(struct cadence_spi_priv *priv) |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 652 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 653 | u32 reg = readl(priv->regbase + CQSPI_REG_SDRAMLEVEL); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 654 | reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB; |
| 655 | return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK; |
| 656 | } |
| 657 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 658 | static int cadence_qspi_wait_for_data(struct cadence_spi_priv *priv) |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 659 | { |
| 660 | unsigned int timeout = 10000; |
| 661 | u32 reg; |
| 662 | |
| 663 | while (timeout--) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 664 | reg = cadence_qspi_get_rd_sram_level(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 665 | if (reg) |
| 666 | return reg; |
| 667 | udelay(1); |
| 668 | } |
| 669 | |
| 670 | return -ETIMEDOUT; |
| 671 | } |
| 672 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 673 | static int |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 674 | cadence_qspi_apb_indirect_read_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 675 | unsigned int n_rx, u8 *rxbuf) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 676 | { |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 677 | unsigned int remaining = n_rx; |
| 678 | unsigned int bytes_to_read = 0; |
| 679 | int ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 680 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 681 | writel(n_rx, priv->regbase + CQSPI_REG_INDIRECTRDBYTES); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 682 | |
| 683 | /* Start the indirect read transfer */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 684 | writel(CQSPI_REG_INDIRECTRD_START, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 685 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 686 | |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 687 | while (remaining > 0) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 688 | ret = cadence_qspi_wait_for_data(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 689 | if (ret < 0) { |
| 690 | printf("Indirect write timed out (%i)\n", ret); |
| 691 | goto failrd; |
| 692 | } |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 693 | |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 694 | bytes_to_read = ret; |
| 695 | |
| 696 | while (bytes_to_read != 0) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 697 | bytes_to_read *= priv->fifo_width; |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 698 | bytes_to_read = bytes_to_read > remaining ? |
| 699 | remaining : bytes_to_read; |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 700 | /* |
| 701 | * Handle non-4-byte aligned access to avoid |
| 702 | * data abort. |
| 703 | */ |
| 704 | if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4)) |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 705 | readsb(priv->ahbbase, rxbuf, bytes_to_read); |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 706 | else |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 707 | readsl(priv->ahbbase, rxbuf, |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 708 | bytes_to_read >> 2); |
| 709 | rxbuf += bytes_to_read; |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 710 | remaining -= bytes_to_read; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 711 | bytes_to_read = cadence_qspi_get_rd_sram_level(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 712 | } |
| 713 | } |
| 714 | |
| 715 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 716 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTRD, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 717 | CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 718 | if (ret) { |
| 719 | printf("Indirect read completion error (%i)\n", ret); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 720 | goto failrd; |
| 721 | } |
| 722 | |
| 723 | /* Clear indirect completion status */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 724 | writel(CQSPI_REG_INDIRECTRD_DONE, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 725 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 726 | |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 727 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 728 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTRD, |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 729 | CQSPI_REG_INDIRECTRD_DONE, 0, 10, 0); |
| 730 | if (ret) { |
| 731 | printf("Indirect read clear completion error (%i)\n", ret); |
| 732 | goto failrd; |
| 733 | } |
| 734 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 735 | return 0; |
| 736 | |
| 737 | failrd: |
| 738 | /* Cancel the indirect read */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 739 | writel(CQSPI_REG_INDIRECTRD_CANCEL, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 740 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 741 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 742 | } |
| 743 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 744 | int cadence_qspi_apb_read_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 745 | const struct spi_mem_op *op) |
| 746 | { |
Vignesh Raghavendra | 68f8266 | 2019-12-05 15:46:06 +0530 | [diff] [blame] | 747 | u64 from = op->addr.val; |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 748 | void *buf = op->data.buf.in; |
| 749 | size_t len = op->data.nbytes; |
| 750 | |
Ashok Reddy Soma | f63e602 | 2022-11-29 04:41:34 -0700 | [diff] [blame] | 751 | cadence_qspi_apb_enable_linear_mode(true); |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 752 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 753 | if (priv->use_dac_mode && (from + len < priv->ahbsize)) { |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 754 | if (len < 256 || |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 755 | dma_memcpy(buf, priv->ahbbase + from, len) < 0) { |
| 756 | memcpy_fromio(buf, priv->ahbbase + from, len); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 757 | } |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 758 | if (!cadence_qspi_wait_idle(priv->regbase)) |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 759 | return -EIO; |
| 760 | return 0; |
| 761 | } |
| 762 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 763 | return cadence_qspi_apb_indirect_read_execute(priv, len, buf); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 764 | } |
| 765 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 766 | /* Opcode + Address (3/4 bytes) */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 767 | int cadence_qspi_apb_write_setup(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 768 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 769 | { |
| 770 | unsigned int reg; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 771 | int ret; |
| 772 | u8 opcode; |
| 773 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 774 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 775 | if (ret) |
| 776 | return ret; |
| 777 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 778 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_WRITE_LSB, |
| 779 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 780 | if (ret) |
| 781 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 782 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 783 | /* Setup the indirect trigger address */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 784 | writel(priv->trigger_address, |
| 785 | priv->regbase + CQSPI_REG_INDIRECTTRIGGER); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 786 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 787 | /* Configure the opcode */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 788 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 789 | opcode = op->cmd.opcode >> 8; |
| 790 | else |
| 791 | opcode = op->cmd.opcode; |
| 792 | |
| 793 | reg = opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 794 | reg |= priv->data_width << CQSPI_REG_WR_INSTR_TYPE_DATA_LSB; |
| 795 | reg |= priv->addr_width << CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB; |
| 796 | writel(reg, priv->regbase + CQSPI_REG_WR_INSTR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 797 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 798 | reg = cadence_qspi_calc_rdreg(priv); |
| 799 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 800 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 801 | writel(op->addr.val, priv->regbase + CQSPI_REG_INDIRECTWRSTARTADDR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 802 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 803 | if (priv->dtr) { |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 804 | /* |
| 805 | * Some flashes like the cypress Semper flash expect a 4-byte |
| 806 | * dummy address with the Read SR command in DTR mode, but this |
| 807 | * controller does not support sending address with the Read SR |
| 808 | * command. So, disable write completion polling on the |
| 809 | * controller's side. spi-nor will take care of polling the |
| 810 | * status register. |
| 811 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 812 | reg = readl(priv->regbase + CQSPI_REG_WR_COMPLETION_CTRL); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 813 | reg |= CQSPI_REG_WR_DISABLE_AUTO_POLL; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 814 | writel(reg, priv->regbase + CQSPI_REG_WR_COMPLETION_CTRL); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 815 | } |
| 816 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 817 | reg = readl(priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 818 | reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 819 | reg |= (op->addr.nbytes - 1); |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 820 | writel(reg, priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 824 | static int |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 825 | cadence_qspi_apb_indirect_write_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 826 | unsigned int n_tx, const u8 *txbuf) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 827 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 828 | unsigned int page_size = priv->page_size; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 829 | unsigned int remaining = n_tx; |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 830 | const u8 *bb_txbuf = txbuf; |
| 831 | void *bounce_buf = NULL; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 832 | unsigned int write_bytes; |
| 833 | int ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 834 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 835 | /* |
| 836 | * Use bounce buffer for non 32 bit aligned txbuf to avoid data |
| 837 | * aborts |
| 838 | */ |
| 839 | if ((uintptr_t)txbuf % 4) { |
| 840 | bounce_buf = malloc(n_tx); |
| 841 | if (!bounce_buf) |
| 842 | return -ENOMEM; |
| 843 | memcpy(bounce_buf, txbuf, n_tx); |
| 844 | bb_txbuf = bounce_buf; |
| 845 | } |
| 846 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 847 | /* Configure the indirect read transfer bytes */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 848 | writel(n_tx, priv->regbase + CQSPI_REG_INDIRECTWRBYTES); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 849 | |
| 850 | /* Start the indirect write transfer */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 851 | writel(CQSPI_REG_INDIRECTWR_START, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 852 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 853 | |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 854 | /* |
| 855 | * Some delay is required for the above bit to be internally |
| 856 | * synchronized by the QSPI module. |
| 857 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 858 | ndelay(priv->wr_delay); |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 859 | |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 860 | while (remaining > 0) { |
| 861 | write_bytes = remaining > page_size ? page_size : remaining; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 862 | writesl(priv->ahbbase, bb_txbuf, write_bytes >> 2); |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 863 | if (write_bytes % 4) |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 864 | writesb(priv->ahbbase, |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 865 | bb_txbuf + rounddown(write_bytes, 4), |
| 866 | write_bytes % 4); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 867 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 868 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_SDRAMLEVEL, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 869 | CQSPI_REG_SDRAMLEVEL_WR_MASK << |
| 870 | CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 871 | if (ret) { |
| 872 | printf("Indirect write timed out (%i)\n", ret); |
| 873 | goto failwr; |
| 874 | } |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 875 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 876 | bb_txbuf += write_bytes; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 877 | remaining -= write_bytes; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 878 | } |
| 879 | |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 880 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 881 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTWR, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 882 | CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 883 | if (ret) { |
| 884 | printf("Indirect write completion error (%i)\n", ret); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 885 | goto failwr; |
| 886 | } |
| 887 | |
| 888 | /* Clear indirect completion status */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 889 | writel(CQSPI_REG_INDIRECTWR_DONE, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 890 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 891 | |
| 892 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 893 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTWR, |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 894 | CQSPI_REG_INDIRECTWR_DONE, 0, 10, 0); |
| 895 | if (ret) { |
| 896 | printf("Indirect write clear completion error (%i)\n", ret); |
| 897 | goto failwr; |
| 898 | } |
| 899 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 900 | if (bounce_buf) |
| 901 | free(bounce_buf); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 902 | return 0; |
| 903 | |
| 904 | failwr: |
| 905 | /* Cancel the indirect write */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 906 | writel(CQSPI_REG_INDIRECTWR_CANCEL, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 907 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 908 | if (bounce_buf) |
| 909 | free(bounce_buf); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 910 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 911 | } |
| 912 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 913 | int cadence_qspi_apb_write_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 914 | const struct spi_mem_op *op) |
| 915 | { |
| 916 | u32 to = op->addr.val; |
| 917 | const void *buf = op->data.buf.out; |
| 918 | size_t len = op->data.nbytes; |
| 919 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 920 | /* |
| 921 | * Some flashes like the Cypress Semper flash expect a dummy 4-byte |
| 922 | * address (all 0s) with the read status register command in DTR mode. |
| 923 | * But this controller does not support sending dummy address bytes to |
| 924 | * the flash when it is polling the write completion register in DTR |
| 925 | * mode. So, we can not use direct mode when in DTR mode for writing |
| 926 | * data. |
| 927 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 928 | cadence_qspi_apb_enable_linear_mode(true); |
| 929 | if (!priv->dtr && priv->use_dac_mode && (to + len < priv->ahbsize)) { |
| 930 | memcpy_toio(priv->ahbbase + to, buf, len); |
| 931 | if (!cadence_qspi_wait_idle(priv->regbase)) |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 932 | return -EIO; |
| 933 | return 0; |
| 934 | } |
| 935 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 936 | return cadence_qspi_apb_indirect_write_execute(priv, len, buf); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 937 | } |
| 938 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 939 | void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy) |
| 940 | { |
| 941 | unsigned int reg; |
| 942 | |
| 943 | /* enter XiP mode immediately and enable direct mode */ |
| 944 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 945 | reg |= CQSPI_REG_CONFIG_ENABLE; |
| 946 | reg |= CQSPI_REG_CONFIG_DIRECT; |
| 947 | reg |= CQSPI_REG_CONFIG_XIP_IMM; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 948 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 949 | |
| 950 | /* keep the XiP mode */ |
| 951 | writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT); |
| 952 | |
| 953 | /* Enable mode bit at devrd */ |
| 954 | reg = readl(reg_base + CQSPI_REG_RD_INSTR); |
| 955 | reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB); |
| 956 | writel(reg, reg_base + CQSPI_REG_RD_INSTR); |
| 957 | } |