Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 |
| 4 | * |
| 5 | * Michael Kurz, <michi.kurz@gmail.com> |
| 6 | * |
| 7 | * STM32 QSPI driver |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 8 | */ |
| 9 | |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 10 | #define LOG_CATEGORY UCLASS_SPI |
| 11 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 12 | #include <common.h> |
Patrice Chotard | 8c4592d | 2018-05-14 15:42:51 +0200 | [diff] [blame] | 13 | #include <clk.h> |
Simon Glass | 340fd10 | 2020-07-19 10:15:34 -0600 | [diff] [blame] | 14 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Patrice Chotard | 5e46123 | 2018-05-14 15:42:56 +0200 | [diff] [blame] | 16 | #include <reset.h> |
Simon Glass | 340fd10 | 2020-07-19 10:15:34 -0600 | [diff] [blame] | 17 | #include <spi.h> |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 18 | #include <spi-mem.h> |
Patrice Chotard | e48ec51 | 2021-01-20 14:42:02 +0100 | [diff] [blame] | 19 | #include <watchdog.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 20 | #include <dm/device_compat.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 21 | #include <linux/bitops.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 22 | #include <linux/delay.h> |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 23 | #include <linux/iopoll.h> |
Patrice Chotard | 2a6ca73 | 2018-05-14 15:42:55 +0200 | [diff] [blame] | 24 | #include <linux/ioport.h> |
Simon Glass | 1e94b46 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 25 | #include <linux/printk.h> |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 26 | #include <linux/sizes.h> |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 27 | |
| 28 | struct stm32_qspi_regs { |
| 29 | u32 cr; /* 0x00 */ |
| 30 | u32 dcr; /* 0x04 */ |
| 31 | u32 sr; /* 0x08 */ |
| 32 | u32 fcr; /* 0x0C */ |
| 33 | u32 dlr; /* 0x10 */ |
| 34 | u32 ccr; /* 0x14 */ |
| 35 | u32 ar; /* 0x18 */ |
| 36 | u32 abr; /* 0x1C */ |
| 37 | u32 dr; /* 0x20 */ |
| 38 | u32 psmkr; /* 0x24 */ |
| 39 | u32 psmar; /* 0x28 */ |
| 40 | u32 pir; /* 0x2C */ |
| 41 | u32 lptr; /* 0x30 */ |
| 42 | }; |
| 43 | |
| 44 | /* |
| 45 | * QUADSPI control register |
| 46 | */ |
| 47 | #define STM32_QSPI_CR_EN BIT(0) |
| 48 | #define STM32_QSPI_CR_ABORT BIT(1) |
| 49 | #define STM32_QSPI_CR_DMAEN BIT(2) |
| 50 | #define STM32_QSPI_CR_TCEN BIT(3) |
| 51 | #define STM32_QSPI_CR_SSHIFT BIT(4) |
| 52 | #define STM32_QSPI_CR_DFM BIT(6) |
| 53 | #define STM32_QSPI_CR_FSEL BIT(7) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 54 | #define STM32_QSPI_CR_FTHRES_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 55 | #define STM32_QSPI_CR_TEIE BIT(16) |
| 56 | #define STM32_QSPI_CR_TCIE BIT(17) |
| 57 | #define STM32_QSPI_CR_FTIE BIT(18) |
| 58 | #define STM32_QSPI_CR_SMIE BIT(19) |
| 59 | #define STM32_QSPI_CR_TOIE BIT(20) |
| 60 | #define STM32_QSPI_CR_APMS BIT(22) |
| 61 | #define STM32_QSPI_CR_PMM BIT(23) |
| 62 | #define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 63 | #define STM32_QSPI_CR_PRESCALER_SHIFT 24 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * QUADSPI device configuration register |
| 67 | */ |
| 68 | #define STM32_QSPI_DCR_CKMODE BIT(0) |
| 69 | #define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 70 | #define STM32_QSPI_DCR_CSHT_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 71 | #define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 72 | #define STM32_QSPI_DCR_FSIZE_SHIFT 16 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * QUADSPI status register |
| 76 | */ |
| 77 | #define STM32_QSPI_SR_TEF BIT(0) |
| 78 | #define STM32_QSPI_SR_TCF BIT(1) |
| 79 | #define STM32_QSPI_SR_FTF BIT(2) |
| 80 | #define STM32_QSPI_SR_SMF BIT(3) |
| 81 | #define STM32_QSPI_SR_TOF BIT(4) |
| 82 | #define STM32_QSPI_SR_BUSY BIT(5) |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 83 | |
| 84 | /* |
| 85 | * QUADSPI flag clear register |
| 86 | */ |
| 87 | #define STM32_QSPI_FCR_CTEF BIT(0) |
| 88 | #define STM32_QSPI_FCR_CTCF BIT(1) |
| 89 | #define STM32_QSPI_FCR_CSMF BIT(3) |
| 90 | #define STM32_QSPI_FCR_CTOF BIT(4) |
| 91 | |
| 92 | /* |
| 93 | * QUADSPI communication configuration register |
| 94 | */ |
| 95 | #define STM32_QSPI_CCR_DDRM BIT(31) |
| 96 | #define STM32_QSPI_CCR_DHHC BIT(30) |
| 97 | #define STM32_QSPI_CCR_SIOO BIT(28) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 98 | #define STM32_QSPI_CCR_FMODE_SHIFT 26 |
| 99 | #define STM32_QSPI_CCR_DMODE_SHIFT 24 |
| 100 | #define STM32_QSPI_CCR_DCYC_SHIFT 18 |
| 101 | #define STM32_QSPI_CCR_ABSIZE_SHIFT 16 |
| 102 | #define STM32_QSPI_CCR_ABMODE_SHIFT 14 |
| 103 | #define STM32_QSPI_CCR_ADSIZE_SHIFT 12 |
| 104 | #define STM32_QSPI_CCR_ADMODE_SHIFT 10 |
| 105 | #define STM32_QSPI_CCR_IMODE_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 106 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 107 | #define STM32_QSPI_CCR_IND_WRITE 0 |
| 108 | #define STM32_QSPI_CCR_IND_READ 1 |
| 109 | #define STM32_QSPI_CCR_MEM_MAP 3 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 110 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 111 | #define STM32_QSPI_MAX_MMAP_SZ SZ_256M |
| 112 | #define STM32_QSPI_MAX_CHIP 2 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 113 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 114 | #define STM32_QSPI_FIFO_TIMEOUT_US 30000 |
| 115 | #define STM32_QSPI_CMD_TIMEOUT_US 1000000 |
| 116 | #define STM32_BUSY_TIMEOUT_US 100000 |
| 117 | #define STM32_ABT_TIMEOUT_US 100000 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 118 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 119 | struct stm32_qspi_priv { |
| 120 | struct stm32_qspi_regs *regs; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 121 | void __iomem *mm_base; |
| 122 | resource_size_t mm_size; |
Patrice Chotard | 541cd6e | 2017-07-18 09:29:09 +0200 | [diff] [blame] | 123 | ulong clock_rate; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 124 | int cs_used; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 125 | }; |
| 126 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 127 | static int _stm32_qspi_wait_for_not_busy(struct stm32_qspi_priv *priv) |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 128 | { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 129 | u32 sr; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 130 | int ret; |
| 131 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 132 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 133 | !(sr & STM32_QSPI_SR_BUSY), |
| 134 | STM32_BUSY_TIMEOUT_US); |
| 135 | if (ret) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 136 | log_err("busy timeout (stat:%#x)\n", sr); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 137 | |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv, |
| 142 | const struct spi_mem_op *op) |
| 143 | { |
| 144 | u32 sr; |
Daniil Stas | 88f7ca0 | 2021-05-23 22:24:49 +0000 | [diff] [blame] | 145 | int ret = 0; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 146 | |
Patrice Chotard | a6d7eeb | 2022-05-12 09:17:37 +0200 | [diff] [blame] | 147 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 148 | sr & STM32_QSPI_SR_TCF, |
| 149 | STM32_QSPI_CMD_TIMEOUT_US); |
| 150 | if (ret) { |
| 151 | log_err("cmd timeout (stat:%#x)\n", sr); |
| 152 | } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) { |
| 153 | log_err("transfer error (stat:%#x)\n", sr); |
| 154 | ret = -EIO; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Patrice Chotard | a6d7eeb | 2022-05-12 09:17:37 +0200 | [diff] [blame] | 157 | /* clear flags */ |
| 158 | writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr); |
| 159 | |
Daniil Stas | 88f7ca0 | 2021-05-23 22:24:49 +0000 | [diff] [blame] | 160 | if (!ret) |
| 161 | ret = _stm32_qspi_wait_for_not_busy(priv); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 162 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 163 | return ret; |
| 164 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 165 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 166 | static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr) |
| 167 | { |
| 168 | *val = readb(addr); |
Stefan Roese | 29caf93 | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 169 | schedule(); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr) |
| 173 | { |
| 174 | writeb(*val, addr); |
| 175 | } |
| 176 | |
| 177 | static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, |
| 178 | const struct spi_mem_op *op) |
| 179 | { |
| 180 | void (*fifo)(u8 *val, void __iomem *addr); |
| 181 | u32 len = op->data.nbytes, sr; |
| 182 | u8 *buf; |
| 183 | int ret; |
| 184 | |
| 185 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 186 | fifo = _stm32_qspi_read_fifo; |
| 187 | buf = op->data.buf.in; |
| 188 | |
| 189 | } else { |
| 190 | fifo = _stm32_qspi_write_fifo; |
| 191 | buf = (u8 *)op->data.buf.out; |
| 192 | } |
| 193 | |
| 194 | while (len--) { |
| 195 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 196 | sr & STM32_QSPI_SR_FTF, |
| 197 | STM32_QSPI_FIFO_TIMEOUT_US); |
| 198 | if (ret) { |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 199 | log_err("fifo timeout (len:%d stat:%#x)\n", len, sr); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | fifo(buf++, &priv->regs->dr); |
| 204 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 209 | static int stm32_qspi_mm(struct stm32_qspi_priv *priv, |
| 210 | const struct spi_mem_op *op) |
| 211 | { |
| 212 | memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val, |
| 213 | op->data.nbytes); |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int _stm32_qspi_tx(struct stm32_qspi_priv *priv, |
| 219 | const struct spi_mem_op *op, |
| 220 | u8 mode) |
| 221 | { |
| 222 | if (!op->data.nbytes) |
| 223 | return 0; |
| 224 | |
| 225 | if (mode == STM32_QSPI_CCR_MEM_MAP) |
| 226 | return stm32_qspi_mm(priv, op); |
| 227 | |
| 228 | return _stm32_qspi_poll(priv, op); |
| 229 | } |
| 230 | |
| 231 | static int _stm32_qspi_get_mode(u8 buswidth) |
| 232 | { |
| 233 | if (buswidth == 4) |
| 234 | return 3; |
| 235 | |
| 236 | return buswidth; |
| 237 | } |
| 238 | |
| 239 | static int stm32_qspi_exec_op(struct spi_slave *slave, |
| 240 | const struct spi_mem_op *op) |
| 241 | { |
| 242 | struct stm32_qspi_priv *priv = dev_get_priv(slave->dev->parent); |
| 243 | u32 cr, ccr, addr_max; |
| 244 | u8 mode = STM32_QSPI_CCR_IND_WRITE; |
| 245 | int timeout, ret; |
| 246 | |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 247 | dev_dbg(slave->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n", |
| 248 | op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, |
| 249 | op->dummy.buswidth, op->data.buswidth, |
| 250 | op->addr.val, op->data.nbytes); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 251 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 252 | addr_max = op->addr.val + op->data.nbytes + 1; |
| 253 | |
| 254 | if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) { |
| 255 | if (addr_max < priv->mm_size && op->addr.buswidth) |
| 256 | mode = STM32_QSPI_CCR_MEM_MAP; |
| 257 | else |
| 258 | mode = STM32_QSPI_CCR_IND_READ; |
| 259 | } |
| 260 | |
| 261 | if (op->data.nbytes) |
| 262 | writel(op->data.nbytes - 1, &priv->regs->dlr); |
| 263 | |
| 264 | ccr = (mode << STM32_QSPI_CCR_FMODE_SHIFT); |
| 265 | ccr |= op->cmd.opcode; |
| 266 | ccr |= (_stm32_qspi_get_mode(op->cmd.buswidth) |
| 267 | << STM32_QSPI_CCR_IMODE_SHIFT); |
| 268 | |
| 269 | if (op->addr.nbytes) { |
| 270 | ccr |= ((op->addr.nbytes - 1) << STM32_QSPI_CCR_ADSIZE_SHIFT); |
| 271 | ccr |= (_stm32_qspi_get_mode(op->addr.buswidth) |
| 272 | << STM32_QSPI_CCR_ADMODE_SHIFT); |
| 273 | } |
| 274 | |
| 275 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 276 | ccr |= (op->dummy.nbytes * 8 / op->dummy.buswidth |
| 277 | << STM32_QSPI_CCR_DCYC_SHIFT); |
| 278 | |
| 279 | if (op->data.nbytes) |
| 280 | ccr |= (_stm32_qspi_get_mode(op->data.buswidth) |
| 281 | << STM32_QSPI_CCR_DMODE_SHIFT); |
| 282 | |
| 283 | writel(ccr, &priv->regs->ccr); |
| 284 | |
| 285 | if (op->addr.nbytes && mode != STM32_QSPI_CCR_MEM_MAP) |
| 286 | writel(op->addr.val, &priv->regs->ar); |
| 287 | |
| 288 | ret = _stm32_qspi_tx(priv, op, mode); |
| 289 | /* |
| 290 | * Abort in: |
| 291 | * -error case |
| 292 | * -read memory map: prefetching must be stopped if we read the last |
| 293 | * byte of device (device size - fifo size). like device size is not |
| 294 | * knows, the prefetching is always stop. |
| 295 | */ |
| 296 | if (ret || mode == STM32_QSPI_CCR_MEM_MAP) |
| 297 | goto abort; |
| 298 | |
| 299 | /* Wait end of tx in indirect mode */ |
| 300 | ret = _stm32_qspi_wait_cmd(priv, op); |
| 301 | if (ret) |
| 302 | goto abort; |
| 303 | |
| 304 | return 0; |
| 305 | |
| 306 | abort: |
| 307 | setbits_le32(&priv->regs->cr, STM32_QSPI_CR_ABORT); |
| 308 | |
| 309 | /* Wait clear of abort bit by hw */ |
| 310 | timeout = readl_poll_timeout(&priv->regs->cr, cr, |
| 311 | !(cr & STM32_QSPI_CR_ABORT), |
| 312 | STM32_ABT_TIMEOUT_US); |
| 313 | |
| 314 | writel(STM32_QSPI_FCR_CTCF, &priv->regs->fcr); |
| 315 | |
| 316 | if (ret || timeout) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 317 | dev_err(slave->dev, "ret:%d abort timeout:%d\n", ret, timeout); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 318 | |
| 319 | return ret; |
| 320 | } |
| 321 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 322 | static int stm32_qspi_probe(struct udevice *bus) |
| 323 | { |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 324 | struct stm32_qspi_priv *priv = dev_get_priv(bus); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 325 | struct resource res; |
Patrice Chotard | 12e7c91 | 2018-05-14 15:42:49 +0200 | [diff] [blame] | 326 | struct clk clk; |
Patrice Chotard | 5e46123 | 2018-05-14 15:42:56 +0200 | [diff] [blame] | 327 | struct reset_ctl reset_ctl; |
Patrice Chotard | 12e7c91 | 2018-05-14 15:42:49 +0200 | [diff] [blame] | 328 | int ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 329 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 330 | ret = dev_read_resource_byname(bus, "qspi", &res); |
| 331 | if (ret) { |
| 332 | dev_err(bus, "can't get regs base addresses(ret = %d)!\n", ret); |
| 333 | return ret; |
| 334 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 335 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 336 | priv->regs = (struct stm32_qspi_regs *)res.start; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 337 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 338 | ret = dev_read_resource_byname(bus, "qspi_mm", &res); |
| 339 | if (ret) { |
| 340 | dev_err(bus, "can't get mmap base address(ret = %d)!\n", ret); |
| 341 | return ret; |
| 342 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 343 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 344 | priv->mm_base = (void __iomem *)res.start; |
| 345 | |
| 346 | priv->mm_size = resource_size(&res); |
| 347 | if (priv->mm_size > STM32_QSPI_MAX_MMAP_SZ) |
| 348 | return -EINVAL; |
| 349 | |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 350 | dev_dbg(bus, "regs=<0x%p> mapped=<0x%p> mapped_size=<0x%lx>\n", |
| 351 | priv->regs, priv->mm_base, priv->mm_size); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 352 | |
Vikas Manocha | 890bafd | 2017-04-10 15:02:50 -0700 | [diff] [blame] | 353 | ret = clk_get_by_index(bus, 0, &clk); |
| 354 | if (ret < 0) |
| 355 | return ret; |
| 356 | |
| 357 | ret = clk_enable(&clk); |
Vikas Manocha | 890bafd | 2017-04-10 15:02:50 -0700 | [diff] [blame] | 358 | if (ret) { |
| 359 | dev_err(bus, "failed to enable clock\n"); |
| 360 | return ret; |
| 361 | } |
Patrice Chotard | 541cd6e | 2017-07-18 09:29:09 +0200 | [diff] [blame] | 362 | |
| 363 | priv->clock_rate = clk_get_rate(&clk); |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 364 | if (!priv->clock_rate) { |
Patrice Chotard | 541cd6e | 2017-07-18 09:29:09 +0200 | [diff] [blame] | 365 | clk_disable(&clk); |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 366 | return -EINVAL; |
Patrice Chotard | 541cd6e | 2017-07-18 09:29:09 +0200 | [diff] [blame] | 367 | } |
| 368 | |
Patrice Chotard | 5e46123 | 2018-05-14 15:42:56 +0200 | [diff] [blame] | 369 | ret = reset_get_by_index(bus, 0, &reset_ctl); |
| 370 | if (ret) { |
| 371 | if (ret != -ENOENT) { |
| 372 | dev_err(bus, "failed to get reset\n"); |
| 373 | clk_disable(&clk); |
| 374 | return ret; |
| 375 | } |
| 376 | } else { |
| 377 | /* Reset QSPI controller */ |
| 378 | reset_assert(&reset_ctl); |
| 379 | udelay(2); |
| 380 | reset_deassert(&reset_ctl); |
| 381 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 382 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 383 | priv->cs_used = -1; |
| 384 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 385 | setbits_le32(&priv->regs->cr, STM32_QSPI_CR_SSHIFT); |
| 386 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 387 | /* Set dcr fsize to max address */ |
| 388 | setbits_le32(&priv->regs->dcr, |
| 389 | STM32_QSPI_DCR_FSIZE_MASK << STM32_QSPI_DCR_FSIZE_SHIFT); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 390 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int stm32_qspi_claim_bus(struct udevice *dev) |
| 395 | { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 396 | struct stm32_qspi_priv *priv = dev_get_priv(dev->parent); |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 397 | struct dm_spi_slave_plat *slave_plat = dev_get_parent_plat(dev); |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 398 | int slave_cs = slave_plat->cs; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 399 | |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 400 | if (slave_cs >= STM32_QSPI_MAX_CHIP) |
Christophe Kerello | 495f3b2 | 2018-05-14 15:42:54 +0200 | [diff] [blame] | 401 | return -ENODEV; |
| 402 | |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 403 | if (priv->cs_used != slave_cs) { |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 404 | priv->cs_used = slave_cs; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 405 | |
Patrice Chotard | acf9f03 | 2023-04-03 08:04:43 +0200 | [diff] [blame] | 406 | /* Set chip select */ |
| 407 | clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL, |
| 408 | priv->cs_used ? STM32_QSPI_CR_FSEL : 0); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 412 | |
| 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | static int stm32_qspi_release_bus(struct udevice *dev) |
| 417 | { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 418 | struct stm32_qspi_priv *priv = dev_get_priv(dev->parent); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 419 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 420 | clrbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 425 | static int stm32_qspi_set_speed(struct udevice *bus, uint speed) |
| 426 | { |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 427 | struct stm32_qspi_priv *priv = dev_get_priv(bus); |
Patrick Delaunay | 936abad | 2018-05-14 15:42:50 +0200 | [diff] [blame] | 428 | u32 qspi_clk = priv->clock_rate; |
| 429 | u32 prescaler = 255; |
| 430 | u32 csht; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 431 | int ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 432 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 433 | if (speed > 0) { |
Patrick Delaunay | 1ddf544 | 2019-06-21 15:26:55 +0200 | [diff] [blame] | 434 | prescaler = 0; |
| 435 | if (qspi_clk) { |
| 436 | prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1; |
| 437 | if (prescaler > 255) |
| 438 | prescaler = 255; |
| 439 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 440 | } |
| 441 | |
Patrick Delaunay | 936abad | 2018-05-14 15:42:50 +0200 | [diff] [blame] | 442 | csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 443 | csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK; |
| 444 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 445 | ret = _stm32_qspi_wait_for_not_busy(priv); |
| 446 | if (ret) |
| 447 | return ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 448 | |
| 449 | clrsetbits_le32(&priv->regs->cr, |
| 450 | STM32_QSPI_CR_PRESCALER_MASK << |
| 451 | STM32_QSPI_CR_PRESCALER_SHIFT, |
| 452 | prescaler << STM32_QSPI_CR_PRESCALER_SHIFT); |
| 453 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 454 | clrsetbits_le32(&priv->regs->dcr, |
| 455 | STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT, |
| 456 | csht << STM32_QSPI_DCR_CSHT_SHIFT); |
| 457 | |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 458 | dev_dbg(bus, "regs=%p, speed=%d\n", priv->regs, |
| 459 | (qspi_clk / (prescaler + 1))); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | static int stm32_qspi_set_mode(struct udevice *bus, uint mode) |
| 465 | { |
| 466 | struct stm32_qspi_priv *priv = dev_get_priv(bus); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 467 | int ret; |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 468 | const char *str_rx, *str_tx; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 469 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 470 | ret = _stm32_qspi_wait_for_not_busy(priv); |
| 471 | if (ret) |
| 472 | return ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 473 | |
| 474 | if ((mode & SPI_CPHA) && (mode & SPI_CPOL)) |
| 475 | setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE); |
| 476 | else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL)) |
| 477 | clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE); |
| 478 | else |
| 479 | return -ENODEV; |
| 480 | |
| 481 | if (mode & SPI_CS_HIGH) |
| 482 | return -ENODEV; |
| 483 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 484 | if (mode & SPI_RX_QUAD) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 485 | str_rx = "quad"; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 486 | else if (mode & SPI_RX_DUAL) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 487 | str_rx = "dual"; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 488 | else |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 489 | str_rx = "single"; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 490 | |
| 491 | if (mode & SPI_TX_QUAD) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 492 | str_tx = "quad"; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 493 | else if (mode & SPI_TX_DUAL) |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 494 | str_tx = "dual"; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 495 | else |
Patrick Delaunay | 162f588 | 2020-11-06 19:01:53 +0100 | [diff] [blame] | 496 | str_tx = "single"; |
| 497 | |
| 498 | dev_dbg(bus, "regs=%p, mode=%d rx: %s, tx: %s\n", |
| 499 | priv->regs, mode, str_rx, str_tx); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 504 | static const struct spi_controller_mem_ops stm32_qspi_mem_ops = { |
| 505 | .exec_op = stm32_qspi_exec_op, |
| 506 | }; |
| 507 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 508 | static const struct dm_spi_ops stm32_qspi_ops = { |
| 509 | .claim_bus = stm32_qspi_claim_bus, |
| 510 | .release_bus = stm32_qspi_release_bus, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 511 | .set_speed = stm32_qspi_set_speed, |
| 512 | .set_mode = stm32_qspi_set_mode, |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 513 | .mem_ops = &stm32_qspi_mem_ops, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 514 | }; |
| 515 | |
| 516 | static const struct udevice_id stm32_qspi_ids[] = { |
Christophe Kerello | 76afe56 | 2018-05-14 15:42:53 +0200 | [diff] [blame] | 517 | { .compatible = "st,stm32f469-qspi" }, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 518 | { } |
| 519 | }; |
| 520 | |
| 521 | U_BOOT_DRIVER(stm32_qspi) = { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 522 | .name = "stm32_qspi", |
| 523 | .id = UCLASS_SPI, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 524 | .of_match = stm32_qspi_ids, |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 525 | .ops = &stm32_qspi_ops, |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 526 | .priv_auto = sizeof(struct stm32_qspi_priv), |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame] | 527 | .probe = stm32_qspi_probe, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 528 | }; |