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