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