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