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 | |
| 379 | return 0; |
| 380 | } |
| 381 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 382 | static int cadence_qspi_setup_opcode_ext(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 383 | const struct spi_mem_op *op, |
| 384 | unsigned int shift) |
| 385 | { |
| 386 | unsigned int reg; |
| 387 | u8 ext; |
| 388 | |
| 389 | if (op->cmd.nbytes != 2) |
| 390 | return -EINVAL; |
| 391 | |
| 392 | /* Opcode extension is the LSB. */ |
| 393 | ext = op->cmd.opcode & 0xff; |
| 394 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 395 | reg = readl(priv->regbase + CQSPI_REG_OP_EXT_LOWER); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 396 | reg &= ~(0xff << shift); |
| 397 | reg |= ext << shift; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 398 | writel(reg, priv->regbase + CQSPI_REG_OP_EXT_LOWER); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 403 | static int cadence_qspi_enable_dtr(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 404 | const struct spi_mem_op *op, |
| 405 | unsigned int shift, |
| 406 | bool enable) |
| 407 | { |
| 408 | unsigned int reg; |
| 409 | int ret; |
| 410 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 411 | reg = readl(priv->regbase + CQSPI_REG_CONFIG); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 412 | |
| 413 | if (enable) { |
| 414 | reg |= CQSPI_REG_CONFIG_DTR_PROTO; |
| 415 | reg |= CQSPI_REG_CONFIG_DUAL_OPCODE; |
| 416 | |
| 417 | /* Set up command opcode extension. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 418 | ret = cadence_qspi_setup_opcode_ext(priv, op, shift); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 419 | if (ret) |
| 420 | return ret; |
| 421 | } else { |
| 422 | reg &= ~CQSPI_REG_CONFIG_DTR_PROTO; |
| 423 | reg &= ~CQSPI_REG_CONFIG_DUAL_OPCODE; |
| 424 | } |
| 425 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 426 | writel(reg, priv->regbase + CQSPI_REG_CONFIG); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 431 | int cadence_qspi_apb_command_read_setup(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 432 | const struct spi_mem_op *op) |
| 433 | { |
| 434 | int ret; |
| 435 | unsigned int reg; |
| 436 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 437 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 438 | if (ret) |
| 439 | return ret; |
| 440 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 441 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_STIG_LSB, |
| 442 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 443 | if (ret) |
| 444 | return ret; |
| 445 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 446 | reg = cadence_qspi_calc_rdreg(priv); |
| 447 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 448 | |
| 449 | return 0; |
| 450 | } |
| 451 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 452 | /* For command RDID, RDSR. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 453 | int cadence_qspi_apb_command_read(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 454 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 455 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 456 | void *reg_base = priv->regbase; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 457 | unsigned int reg; |
| 458 | unsigned int read_len; |
| 459 | int status; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 460 | unsigned int rxlen = op->data.nbytes; |
| 461 | void *rxbuf = op->data.buf.in; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 462 | unsigned int dummy_clk; |
| 463 | u8 opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 464 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 465 | if (rxlen > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) { |
| 466 | printf("QSPI: Invalid input arguments rxlen %u\n", rxlen); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 467 | return -EINVAL; |
| 468 | } |
| 469 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 470 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 471 | opcode = op->cmd.opcode >> 8; |
| 472 | else |
| 473 | opcode = op->cmd.opcode; |
| 474 | |
| 475 | reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB; |
| 476 | |
| 477 | /* Set up dummy cycles. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 478 | dummy_clk = cadence_qspi_calc_dummy(op, priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 479 | if (dummy_clk > CQSPI_DUMMY_CLKS_MAX) |
| 480 | return -ENOTSUPP; |
| 481 | |
| 482 | if (dummy_clk) |
| 483 | reg |= (dummy_clk & CQSPI_REG_CMDCTRL_DUMMY_MASK) |
| 484 | << CQSPI_REG_CMDCTRL_DUMMY_LSB; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 485 | |
| 486 | reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB); |
| 487 | |
| 488 | /* 0 means 1 byte. */ |
| 489 | reg |= (((rxlen - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK) |
| 490 | << CQSPI_REG_CMDCTRL_RD_BYTES_LSB); |
Dhruva Gole | 24d8de6 | 2023-01-03 12:01:11 +0530 | [diff] [blame] | 491 | |
| 492 | /* setup ADDR BIT field */ |
| 493 | if (op->addr.nbytes) { |
| 494 | writel(op->addr.val, priv->regbase + CQSPI_REG_CMDADDRESS); |
| 495 | /* |
| 496 | * address bytes are zero indexed |
| 497 | */ |
| 498 | reg |= (((op->addr.nbytes - 1) & |
| 499 | CQSPI_REG_CMDCTRL_ADD_BYTES_MASK) << |
| 500 | CQSPI_REG_CMDCTRL_ADD_BYTES_LSB); |
| 501 | reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB); |
| 502 | } |
| 503 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 504 | status = cadence_qspi_apb_exec_flash_cmd(reg_base, reg); |
| 505 | if (status != 0) |
| 506 | return status; |
| 507 | |
| 508 | reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER); |
| 509 | |
| 510 | /* Put the read value into rx_buf */ |
| 511 | read_len = (rxlen > 4) ? 4 : rxlen; |
| 512 | memcpy(rxbuf, ®, read_len); |
| 513 | rxbuf += read_len; |
| 514 | |
| 515 | if (rxlen > 4) { |
| 516 | reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER); |
| 517 | |
| 518 | read_len = rxlen - read_len; |
| 519 | memcpy(rxbuf, ®, read_len); |
| 520 | } |
| 521 | return 0; |
| 522 | } |
| 523 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 524 | int cadence_qspi_apb_command_write_setup(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 525 | const struct spi_mem_op *op) |
| 526 | { |
| 527 | int ret; |
| 528 | unsigned int reg; |
| 529 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 530 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 531 | if (ret) |
| 532 | return ret; |
| 533 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 534 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_STIG_LSB, |
| 535 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 536 | if (ret) |
| 537 | return ret; |
| 538 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 539 | reg = cadence_qspi_calc_rdreg(priv); |
| 540 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 545 | /* For commands: WRSR, WREN, WRDI, CHIP_ERASE, BE, etc. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 546 | int cadence_qspi_apb_command_write(struct cadence_spi_priv *priv, |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 547 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 548 | { |
| 549 | unsigned int reg = 0; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 550 | unsigned int wr_data; |
| 551 | unsigned int wr_len; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 552 | unsigned int txlen = op->data.nbytes; |
| 553 | const void *txbuf = op->data.buf.out; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 554 | void *reg_base = priv->regbase; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 555 | u32 addr; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 556 | u8 opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 557 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 558 | /* Reorder address to SPI bus order if only transferring address */ |
| 559 | if (!txlen) { |
| 560 | addr = cpu_to_be32(op->addr.val); |
| 561 | if (op->addr.nbytes == 3) |
| 562 | addr >>= 8; |
| 563 | txbuf = &addr; |
| 564 | txlen = op->addr.nbytes; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 565 | } |
| 566 | |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 567 | if (txlen > CQSPI_STIG_DATA_LEN_MAX) { |
| 568 | printf("QSPI: Invalid input arguments txlen %u\n", txlen); |
| 569 | return -EINVAL; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 570 | } |
| 571 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 572 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 573 | opcode = op->cmd.opcode >> 8; |
| 574 | else |
| 575 | opcode = op->cmd.opcode; |
| 576 | |
| 577 | reg |= opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 578 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 579 | if (txlen) { |
| 580 | /* writing data = yes */ |
| 581 | reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB); |
| 582 | reg |= ((txlen - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK) |
| 583 | << CQSPI_REG_CMDCTRL_WR_BYTES_LSB; |
| 584 | |
| 585 | wr_len = txlen > 4 ? 4 : txlen; |
| 586 | memcpy(&wr_data, txbuf, wr_len); |
| 587 | writel(wr_data, reg_base + |
| 588 | CQSPI_REG_CMDWRITEDATALOWER); |
| 589 | |
| 590 | if (txlen > 4) { |
| 591 | txbuf += wr_len; |
| 592 | wr_len = txlen - wr_len; |
| 593 | memcpy(&wr_data, txbuf, wr_len); |
| 594 | writel(wr_data, reg_base + |
| 595 | CQSPI_REG_CMDWRITEDATAUPPER); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | /* Execute the command */ |
| 600 | return cadence_qspi_apb_exec_flash_cmd(reg_base, reg); |
| 601 | } |
| 602 | |
| 603 | /* Opcode + Address (3/4 bytes) + dummy bytes (0-4 bytes) */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 604 | int cadence_qspi_apb_read_setup(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 605 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 606 | { |
| 607 | unsigned int reg; |
| 608 | unsigned int rd_reg; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 609 | unsigned int dummy_clk; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 610 | unsigned int dummy_bytes = op->dummy.nbytes; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 611 | int ret; |
| 612 | u8 opcode; |
| 613 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 614 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 615 | if (ret) |
| 616 | return ret; |
| 617 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 618 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_READ_LSB, |
| 619 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 620 | if (ret) |
| 621 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 622 | |
| 623 | /* Setup the indirect trigger address */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 624 | writel(priv->trigger_address, |
| 625 | priv->regbase + CQSPI_REG_INDIRECTTRIGGER); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 626 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 627 | /* Configure the opcode */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 628 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 629 | opcode = op->cmd.opcode >> 8; |
| 630 | else |
| 631 | opcode = op->cmd.opcode; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 632 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 633 | rd_reg = opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 634 | rd_reg |= cadence_qspi_calc_rdreg(priv); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 635 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 636 | writel(op->addr.val, priv->regbase + CQSPI_REG_INDIRECTRDSTARTADDR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 637 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 638 | if (dummy_bytes) { |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 639 | /* Convert to clock cycles. */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 640 | dummy_clk = cadence_qspi_calc_dummy(op, priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 641 | |
| 642 | if (dummy_clk > CQSPI_DUMMY_CLKS_MAX) |
| 643 | return -ENOTSUPP; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 644 | |
| 645 | if (dummy_clk) |
| 646 | rd_reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK) |
| 647 | << CQSPI_REG_RD_INSTR_DUMMY_LSB; |
| 648 | } |
| 649 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 650 | writel(rd_reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 651 | |
| 652 | /* set device size */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 653 | reg = readl(priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 654 | reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 655 | reg |= (op->addr.nbytes - 1); |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 656 | writel(reg, priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 660 | 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] | 661 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 662 | u32 reg = readl(priv->regbase + CQSPI_REG_SDRAMLEVEL); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 663 | reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB; |
| 664 | return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK; |
| 665 | } |
| 666 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 667 | static int cadence_qspi_wait_for_data(struct cadence_spi_priv *priv) |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 668 | { |
| 669 | unsigned int timeout = 10000; |
| 670 | u32 reg; |
| 671 | |
| 672 | while (timeout--) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 673 | reg = cadence_qspi_get_rd_sram_level(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 674 | if (reg) |
| 675 | return reg; |
| 676 | udelay(1); |
| 677 | } |
| 678 | |
| 679 | return -ETIMEDOUT; |
| 680 | } |
| 681 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 682 | static int |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 683 | cadence_qspi_apb_indirect_read_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 684 | unsigned int n_rx, u8 *rxbuf) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 685 | { |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 686 | unsigned int remaining = n_rx; |
| 687 | unsigned int bytes_to_read = 0; |
| 688 | int ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 689 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 690 | writel(n_rx, priv->regbase + CQSPI_REG_INDIRECTRDBYTES); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 691 | |
| 692 | /* Start the indirect read transfer */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 693 | writel(CQSPI_REG_INDIRECTRD_START, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 694 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 695 | |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 696 | while (remaining > 0) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 697 | ret = cadence_qspi_wait_for_data(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 698 | if (ret < 0) { |
| 699 | printf("Indirect write timed out (%i)\n", ret); |
| 700 | goto failrd; |
| 701 | } |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 702 | |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 703 | bytes_to_read = ret; |
| 704 | |
| 705 | while (bytes_to_read != 0) { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 706 | bytes_to_read *= priv->fifo_width; |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 707 | bytes_to_read = bytes_to_read > remaining ? |
| 708 | remaining : bytes_to_read; |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 709 | /* |
| 710 | * Handle non-4-byte aligned access to avoid |
| 711 | * data abort. |
| 712 | */ |
| 713 | if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4)) |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 714 | readsb(priv->ahbbase, rxbuf, bytes_to_read); |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 715 | else |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 716 | readsl(priv->ahbbase, rxbuf, |
Goldschmidt Simon | 16cbd09 | 2018-01-24 10:44:05 +0530 | [diff] [blame] | 717 | bytes_to_read >> 2); |
| 718 | rxbuf += bytes_to_read; |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 719 | remaining -= bytes_to_read; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 720 | bytes_to_read = cadence_qspi_get_rd_sram_level(priv); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | |
| 724 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 725 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTRD, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 726 | CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 727 | if (ret) { |
| 728 | printf("Indirect read completion error (%i)\n", ret); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 729 | goto failrd; |
| 730 | } |
| 731 | |
| 732 | /* Clear indirect completion status */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 733 | writel(CQSPI_REG_INDIRECTRD_DONE, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 734 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 735 | |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 736 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 737 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTRD, |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 738 | CQSPI_REG_INDIRECTRD_DONE, 0, 10, 0); |
| 739 | if (ret) { |
| 740 | printf("Indirect read clear completion error (%i)\n", ret); |
| 741 | goto failrd; |
| 742 | } |
| 743 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 744 | return 0; |
| 745 | |
| 746 | failrd: |
| 747 | /* Cancel the indirect read */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 748 | writel(CQSPI_REG_INDIRECTRD_CANCEL, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 749 | priv->regbase + CQSPI_REG_INDIRECTRD); |
Marek Vasut | 8c17743 | 2016-04-27 23:38:05 +0200 | [diff] [blame] | 750 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 753 | int cadence_qspi_apb_read_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 754 | const struct spi_mem_op *op) |
| 755 | { |
Vignesh Raghavendra | 68f8266 | 2019-12-05 15:46:06 +0530 | [diff] [blame] | 756 | u64 from = op->addr.val; |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 757 | void *buf = op->data.buf.in; |
| 758 | size_t len = op->data.nbytes; |
| 759 | |
Ashok Reddy Soma | f63e602 | 2022-11-29 04:41:34 -0700 | [diff] [blame] | 760 | cadence_qspi_apb_enable_linear_mode(true); |
T Karthik Reddy | 3b49fbf | 2022-05-12 04:05:34 -0600 | [diff] [blame] | 761 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 762 | if (priv->use_dac_mode && (from + len < priv->ahbsize)) { |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 763 | if (len < 256 || |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 764 | dma_memcpy(buf, priv->ahbbase + from, len) < 0) { |
| 765 | memcpy_fromio(buf, priv->ahbbase + from, len); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 766 | } |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 767 | if (!cadence_qspi_wait_idle(priv->regbase)) |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 768 | return -EIO; |
| 769 | return 0; |
| 770 | } |
| 771 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 772 | return cadence_qspi_apb_indirect_read_execute(priv, len, buf); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 773 | } |
| 774 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 775 | /* Opcode + Address (3/4 bytes) */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 776 | int cadence_qspi_apb_write_setup(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 777 | const struct spi_mem_op *op) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 778 | { |
| 779 | unsigned int reg; |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 780 | int ret; |
| 781 | u8 opcode; |
| 782 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 783 | ret = cadence_qspi_set_protocol(priv, op); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 784 | if (ret) |
| 785 | return ret; |
| 786 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 787 | ret = cadence_qspi_enable_dtr(priv, op, CQSPI_REG_OP_EXT_WRITE_LSB, |
| 788 | priv->dtr); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 789 | if (ret) |
| 790 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 791 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 792 | /* Setup the indirect trigger address */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 793 | writel(priv->trigger_address, |
| 794 | priv->regbase + CQSPI_REG_INDIRECTTRIGGER); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 795 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 796 | /* Configure the opcode */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 797 | if (priv->dtr) |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 798 | opcode = op->cmd.opcode >> 8; |
| 799 | else |
| 800 | opcode = op->cmd.opcode; |
| 801 | |
| 802 | reg = opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 803 | reg |= priv->data_width << CQSPI_REG_WR_INSTR_TYPE_DATA_LSB; |
| 804 | reg |= priv->addr_width << CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB; |
| 805 | writel(reg, priv->regbase + CQSPI_REG_WR_INSTR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 806 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 807 | reg = cadence_qspi_calc_rdreg(priv); |
| 808 | writel(reg, priv->regbase + CQSPI_REG_RD_INSTR); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 809 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 810 | writel(op->addr.val, priv->regbase + CQSPI_REG_INDIRECTWRSTARTADDR); |
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 | if (priv->dtr) { |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 813 | /* |
| 814 | * Some flashes like the cypress Semper flash expect a 4-byte |
| 815 | * dummy address with the Read SR command in DTR mode, but this |
| 816 | * controller does not support sending address with the Read SR |
| 817 | * command. So, disable write completion polling on the |
| 818 | * controller's side. spi-nor will take care of polling the |
| 819 | * status register. |
| 820 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 821 | reg = readl(priv->regbase + CQSPI_REG_WR_COMPLETION_CTRL); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 822 | reg |= CQSPI_REG_WR_DISABLE_AUTO_POLL; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 823 | writel(reg, priv->regbase + CQSPI_REG_WR_COMPLETION_CTRL); |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 824 | } |
| 825 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 826 | reg = readl(priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 827 | reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK; |
Vignesh Raghavendra | 27516a3 | 2020-01-27 10:36:39 +0530 | [diff] [blame] | 828 | reg |= (op->addr.nbytes - 1); |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 829 | writel(reg, priv->regbase + CQSPI_REG_SIZE); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 830 | return 0; |
| 831 | } |
| 832 | |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 833 | static int |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 834 | cadence_qspi_apb_indirect_write_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 835 | unsigned int n_tx, const u8 *txbuf) |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 836 | { |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 837 | unsigned int page_size = priv->page_size; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 838 | unsigned int remaining = n_tx; |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 839 | const u8 *bb_txbuf = txbuf; |
| 840 | void *bounce_buf = NULL; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 841 | unsigned int write_bytes; |
| 842 | int ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 843 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 844 | /* |
| 845 | * Use bounce buffer for non 32 bit aligned txbuf to avoid data |
| 846 | * aborts |
| 847 | */ |
| 848 | if ((uintptr_t)txbuf % 4) { |
| 849 | bounce_buf = malloc(n_tx); |
| 850 | if (!bounce_buf) |
| 851 | return -ENOMEM; |
| 852 | memcpy(bounce_buf, txbuf, n_tx); |
| 853 | bb_txbuf = bounce_buf; |
| 854 | } |
| 855 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 856 | /* Configure the indirect read transfer bytes */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 857 | writel(n_tx, priv->regbase + CQSPI_REG_INDIRECTWRBYTES); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 858 | |
| 859 | /* Start the indirect write transfer */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 860 | writel(CQSPI_REG_INDIRECTWR_START, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 861 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 862 | |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 863 | /* |
| 864 | * Some delay is required for the above bit to be internally |
| 865 | * synchronized by the QSPI module. |
| 866 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 867 | ndelay(priv->wr_delay); |
Pratyush Yadav | 8dcf3e2 | 2021-06-26 00:47:08 +0530 | [diff] [blame] | 868 | |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 869 | while (remaining > 0) { |
| 870 | write_bytes = remaining > page_size ? page_size : remaining; |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 871 | writesl(priv->ahbbase, bb_txbuf, write_bytes >> 2); |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 872 | if (write_bytes % 4) |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 873 | writesb(priv->ahbbase, |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 874 | bb_txbuf + rounddown(write_bytes, 4), |
| 875 | write_bytes % 4); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 876 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 877 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_SDRAMLEVEL, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 878 | CQSPI_REG_SDRAMLEVEL_WR_MASK << |
| 879 | CQSPI_REG_SDRAMLEVEL_WR_LSB, 0, 10, 0); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 880 | if (ret) { |
| 881 | printf("Indirect write timed out (%i)\n", ret); |
| 882 | goto failwr; |
| 883 | } |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 884 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 885 | bb_txbuf += write_bytes; |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 886 | remaining -= write_bytes; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 889 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 890 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTWR, |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 891 | CQSPI_REG_INDIRECTWR_DONE, 1, 10, 0); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 892 | if (ret) { |
| 893 | printf("Indirect write completion error (%i)\n", ret); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 894 | goto failwr; |
| 895 | } |
| 896 | |
| 897 | /* Clear indirect completion status */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 898 | writel(CQSPI_REG_INDIRECTWR_DONE, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 899 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 900 | |
| 901 | /* Check indirect done status */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 902 | ret = wait_for_bit_le32(priv->regbase + CQSPI_REG_INDIRECTWR, |
Marek Vasut | 84d4f73 | 2021-09-14 05:22:31 +0200 | [diff] [blame] | 903 | CQSPI_REG_INDIRECTWR_DONE, 0, 10, 0); |
| 904 | if (ret) { |
| 905 | printf("Indirect write clear completion error (%i)\n", ret); |
| 906 | goto failwr; |
| 907 | } |
| 908 | |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 909 | if (bounce_buf) |
| 910 | free(bounce_buf); |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 911 | return 0; |
| 912 | |
| 913 | failwr: |
| 914 | /* Cancel the indirect write */ |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 915 | writel(CQSPI_REG_INDIRECTWR_CANCEL, |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 916 | priv->regbase + CQSPI_REG_INDIRECTWR); |
Vignesh R | ad4bd8a | 2018-01-24 10:44:07 +0530 | [diff] [blame] | 917 | if (bounce_buf) |
| 918 | free(bounce_buf); |
Marek Vasut | dae51dd | 2016-04-27 23:18:55 +0200 | [diff] [blame] | 919 | return ret; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 920 | } |
| 921 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 922 | int cadence_qspi_apb_write_execute(struct cadence_spi_priv *priv, |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 923 | const struct spi_mem_op *op) |
| 924 | { |
| 925 | u32 to = op->addr.val; |
| 926 | const void *buf = op->data.buf.out; |
| 927 | size_t len = op->data.nbytes; |
| 928 | |
Pratyush Yadav | e1814ad | 2021-06-26 00:47:09 +0530 | [diff] [blame] | 929 | /* |
| 930 | * Some flashes like the Cypress Semper flash expect a dummy 4-byte |
| 931 | * address (all 0s) with the read status register command in DTR mode. |
| 932 | * But this controller does not support sending dummy address bytes to |
| 933 | * the flash when it is polling the write completion register in DTR |
| 934 | * mode. So, we can not use direct mode when in DTR mode for writing |
| 935 | * data. |
| 936 | */ |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 937 | cadence_qspi_apb_enable_linear_mode(true); |
| 938 | if (!priv->dtr && priv->use_dac_mode && (to + len < priv->ahbsize)) { |
| 939 | memcpy_toio(priv->ahbbase + to, buf, len); |
| 940 | if (!cadence_qspi_wait_idle(priv->regbase)) |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 941 | return -EIO; |
| 942 | return 0; |
| 943 | } |
| 944 | |
Ashok Reddy Soma | f581765 | 2022-08-24 05:38:47 -0600 | [diff] [blame] | 945 | return cadence_qspi_apb_indirect_write_execute(priv, len, buf); |
Vignesh Raghavendra | 6b7df22 | 2020-01-27 10:36:40 +0530 | [diff] [blame] | 946 | } |
| 947 | |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 948 | void cadence_qspi_apb_enter_xip(void *reg_base, char xip_dummy) |
| 949 | { |
| 950 | unsigned int reg; |
| 951 | |
| 952 | /* enter XiP mode immediately and enable direct mode */ |
| 953 | reg = readl(reg_base + CQSPI_REG_CONFIG); |
Phil Edworthy | 3a5ae12 | 2016-11-29 12:58:30 +0000 | [diff] [blame] | 954 | reg |= CQSPI_REG_CONFIG_ENABLE; |
| 955 | reg |= CQSPI_REG_CONFIG_DIRECT; |
| 956 | reg |= CQSPI_REG_CONFIG_XIP_IMM; |
Stefan Roese | 1c60fe7 | 2014-11-07 12:37:49 +0100 | [diff] [blame] | 957 | writel(reg, reg_base + CQSPI_REG_CONFIG); |
| 958 | |
| 959 | /* keep the XiP mode */ |
| 960 | writel(xip_dummy, reg_base + CQSPI_REG_MODE_BIT); |
| 961 | |
| 962 | /* Enable mode bit at devrd */ |
| 963 | reg = readl(reg_base + CQSPI_REG_RD_INSTR); |
| 964 | reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB); |
| 965 | writel(reg, reg_base + CQSPI_REG_RD_INSTR); |
| 966 | } |