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 | |
| 10 | #include <common.h> |
Patrice Chotard | 8c4592d | 2018-05-14 15:42:51 +0200 | [diff] [blame] | 11 | #include <clk.h> |
Patrice Chotard | 5e46123 | 2018-05-14 15:42:56 +0200 | [diff] [blame] | 12 | #include <reset.h> |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 13 | #include <spi-mem.h> |
| 14 | #include <linux/iopoll.h> |
Patrice Chotard | 2a6ca73 | 2018-05-14 15:42:55 +0200 | [diff] [blame] | 15 | #include <linux/ioport.h> |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 16 | #include <linux/sizes.h> |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 17 | |
| 18 | struct stm32_qspi_regs { |
| 19 | u32 cr; /* 0x00 */ |
| 20 | u32 dcr; /* 0x04 */ |
| 21 | u32 sr; /* 0x08 */ |
| 22 | u32 fcr; /* 0x0C */ |
| 23 | u32 dlr; /* 0x10 */ |
| 24 | u32 ccr; /* 0x14 */ |
| 25 | u32 ar; /* 0x18 */ |
| 26 | u32 abr; /* 0x1C */ |
| 27 | u32 dr; /* 0x20 */ |
| 28 | u32 psmkr; /* 0x24 */ |
| 29 | u32 psmar; /* 0x28 */ |
| 30 | u32 pir; /* 0x2C */ |
| 31 | u32 lptr; /* 0x30 */ |
| 32 | }; |
| 33 | |
| 34 | /* |
| 35 | * QUADSPI control register |
| 36 | */ |
| 37 | #define STM32_QSPI_CR_EN BIT(0) |
| 38 | #define STM32_QSPI_CR_ABORT BIT(1) |
| 39 | #define STM32_QSPI_CR_DMAEN BIT(2) |
| 40 | #define STM32_QSPI_CR_TCEN BIT(3) |
| 41 | #define STM32_QSPI_CR_SSHIFT BIT(4) |
| 42 | #define STM32_QSPI_CR_DFM BIT(6) |
| 43 | #define STM32_QSPI_CR_FSEL BIT(7) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 44 | #define STM32_QSPI_CR_FTHRES_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 45 | #define STM32_QSPI_CR_TEIE BIT(16) |
| 46 | #define STM32_QSPI_CR_TCIE BIT(17) |
| 47 | #define STM32_QSPI_CR_FTIE BIT(18) |
| 48 | #define STM32_QSPI_CR_SMIE BIT(19) |
| 49 | #define STM32_QSPI_CR_TOIE BIT(20) |
| 50 | #define STM32_QSPI_CR_APMS BIT(22) |
| 51 | #define STM32_QSPI_CR_PMM BIT(23) |
| 52 | #define STM32_QSPI_CR_PRESCALER_MASK GENMASK(7, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 53 | #define STM32_QSPI_CR_PRESCALER_SHIFT 24 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 54 | |
| 55 | /* |
| 56 | * QUADSPI device configuration register |
| 57 | */ |
| 58 | #define STM32_QSPI_DCR_CKMODE BIT(0) |
| 59 | #define STM32_QSPI_DCR_CSHT_MASK GENMASK(2, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 60 | #define STM32_QSPI_DCR_CSHT_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 61 | #define STM32_QSPI_DCR_FSIZE_MASK GENMASK(4, 0) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 62 | #define STM32_QSPI_DCR_FSIZE_SHIFT 16 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * QUADSPI status register |
| 66 | */ |
| 67 | #define STM32_QSPI_SR_TEF BIT(0) |
| 68 | #define STM32_QSPI_SR_TCF BIT(1) |
| 69 | #define STM32_QSPI_SR_FTF BIT(2) |
| 70 | #define STM32_QSPI_SR_SMF BIT(3) |
| 71 | #define STM32_QSPI_SR_TOF BIT(4) |
| 72 | #define STM32_QSPI_SR_BUSY BIT(5) |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * QUADSPI flag clear register |
| 76 | */ |
| 77 | #define STM32_QSPI_FCR_CTEF BIT(0) |
| 78 | #define STM32_QSPI_FCR_CTCF BIT(1) |
| 79 | #define STM32_QSPI_FCR_CSMF BIT(3) |
| 80 | #define STM32_QSPI_FCR_CTOF BIT(4) |
| 81 | |
| 82 | /* |
| 83 | * QUADSPI communication configuration register |
| 84 | */ |
| 85 | #define STM32_QSPI_CCR_DDRM BIT(31) |
| 86 | #define STM32_QSPI_CCR_DHHC BIT(30) |
| 87 | #define STM32_QSPI_CCR_SIOO BIT(28) |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 88 | #define STM32_QSPI_CCR_FMODE_SHIFT 26 |
| 89 | #define STM32_QSPI_CCR_DMODE_SHIFT 24 |
| 90 | #define STM32_QSPI_CCR_DCYC_SHIFT 18 |
| 91 | #define STM32_QSPI_CCR_ABSIZE_SHIFT 16 |
| 92 | #define STM32_QSPI_CCR_ABMODE_SHIFT 14 |
| 93 | #define STM32_QSPI_CCR_ADSIZE_SHIFT 12 |
| 94 | #define STM32_QSPI_CCR_ADMODE_SHIFT 10 |
| 95 | #define STM32_QSPI_CCR_IMODE_SHIFT 8 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 96 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 97 | #define STM32_QSPI_CCR_IND_WRITE 0 |
| 98 | #define STM32_QSPI_CCR_IND_READ 1 |
| 99 | #define STM32_QSPI_CCR_MEM_MAP 3 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 100 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 101 | #define STM32_QSPI_MAX_MMAP_SZ SZ_256M |
| 102 | #define STM32_QSPI_MAX_CHIP 2 |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 103 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 104 | #define STM32_QSPI_FIFO_TIMEOUT_US 30000 |
| 105 | #define STM32_QSPI_CMD_TIMEOUT_US 1000000 |
| 106 | #define STM32_BUSY_TIMEOUT_US 100000 |
| 107 | #define STM32_ABT_TIMEOUT_US 100000 |
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 | struct stm32_qspi_flash { |
| 110 | u32 cr; |
| 111 | u32 dcr; |
| 112 | bool initialized; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct stm32_qspi_priv { |
| 116 | struct stm32_qspi_regs *regs; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 117 | struct stm32_qspi_flash flash[STM32_QSPI_MAX_CHIP]; |
| 118 | void __iomem *mm_base; |
| 119 | resource_size_t mm_size; |
Patrice Chotard | 541cd6e | 2017-07-18 09:29:09 +0200 | [diff] [blame] | 120 | ulong clock_rate; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 121 | int cs_used; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 124 | 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] | 125 | { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 126 | u32 sr; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 127 | int ret; |
| 128 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 129 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 130 | !(sr & STM32_QSPI_SR_BUSY), |
| 131 | STM32_BUSY_TIMEOUT_US); |
| 132 | if (ret) |
| 133 | pr_err("busy timeout (stat:%#x)\n", sr); |
| 134 | |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | static int _stm32_qspi_wait_cmd(struct stm32_qspi_priv *priv, |
| 139 | const struct spi_mem_op *op) |
| 140 | { |
| 141 | u32 sr; |
| 142 | int ret; |
| 143 | |
| 144 | if (!op->data.nbytes) |
| 145 | return _stm32_qspi_wait_for_not_busy(priv); |
| 146 | |
| 147 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 148 | sr & STM32_QSPI_SR_TCF, |
| 149 | STM32_QSPI_CMD_TIMEOUT_US); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 150 | if (ret) { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 151 | pr_err("cmd timeout (stat:%#x)\n", sr); |
| 152 | } else if (readl(&priv->regs->sr) & STM32_QSPI_SR_TEF) { |
| 153 | pr_err("transfer error (stat:%#x)\n", sr); |
| 154 | ret = -EIO; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 155 | } |
| 156 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 157 | /* clear flags */ |
| 158 | writel(STM32_QSPI_FCR_CTCF | STM32_QSPI_FCR_CTEF, &priv->regs->fcr); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 159 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 160 | return ret; |
| 161 | } |
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 | static void _stm32_qspi_read_fifo(u8 *val, void __iomem *addr) |
| 164 | { |
| 165 | *val = readb(addr); |
| 166 | } |
| 167 | |
| 168 | static void _stm32_qspi_write_fifo(u8 *val, void __iomem *addr) |
| 169 | { |
| 170 | writeb(*val, addr); |
| 171 | } |
| 172 | |
| 173 | static int _stm32_qspi_poll(struct stm32_qspi_priv *priv, |
| 174 | const struct spi_mem_op *op) |
| 175 | { |
| 176 | void (*fifo)(u8 *val, void __iomem *addr); |
| 177 | u32 len = op->data.nbytes, sr; |
| 178 | u8 *buf; |
| 179 | int ret; |
| 180 | |
| 181 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 182 | fifo = _stm32_qspi_read_fifo; |
| 183 | buf = op->data.buf.in; |
| 184 | |
| 185 | } else { |
| 186 | fifo = _stm32_qspi_write_fifo; |
| 187 | buf = (u8 *)op->data.buf.out; |
| 188 | } |
| 189 | |
| 190 | while (len--) { |
| 191 | ret = readl_poll_timeout(&priv->regs->sr, sr, |
| 192 | sr & STM32_QSPI_SR_FTF, |
| 193 | STM32_QSPI_FIFO_TIMEOUT_US); |
| 194 | if (ret) { |
| 195 | pr_err("fifo timeout (len:%d stat:%#x)\n", len, sr); |
| 196 | return ret; |
| 197 | } |
| 198 | |
| 199 | fifo(buf++, &priv->regs->dr); |
| 200 | } |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 205 | static int stm32_qspi_mm(struct stm32_qspi_priv *priv, |
| 206 | const struct spi_mem_op *op) |
| 207 | { |
| 208 | memcpy_fromio(op->data.buf.in, priv->mm_base + op->addr.val, |
| 209 | op->data.nbytes); |
| 210 | |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | static int _stm32_qspi_tx(struct stm32_qspi_priv *priv, |
| 215 | const struct spi_mem_op *op, |
| 216 | u8 mode) |
| 217 | { |
| 218 | if (!op->data.nbytes) |
| 219 | return 0; |
| 220 | |
| 221 | if (mode == STM32_QSPI_CCR_MEM_MAP) |
| 222 | return stm32_qspi_mm(priv, op); |
| 223 | |
| 224 | return _stm32_qspi_poll(priv, op); |
| 225 | } |
| 226 | |
| 227 | static int _stm32_qspi_get_mode(u8 buswidth) |
| 228 | { |
| 229 | if (buswidth == 4) |
| 230 | return 3; |
| 231 | |
| 232 | return buswidth; |
| 233 | } |
| 234 | |
| 235 | static int stm32_qspi_exec_op(struct spi_slave *slave, |
| 236 | const struct spi_mem_op *op) |
| 237 | { |
| 238 | struct stm32_qspi_priv *priv = dev_get_priv(slave->dev->parent); |
| 239 | u32 cr, ccr, addr_max; |
| 240 | u8 mode = STM32_QSPI_CCR_IND_WRITE; |
| 241 | int timeout, ret; |
| 242 | |
| 243 | debug("%s: cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n", |
| 244 | __func__, op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, |
| 245 | op->dummy.buswidth, op->data.buswidth, |
| 246 | op->addr.val, op->data.nbytes); |
| 247 | |
| 248 | ret = _stm32_qspi_wait_for_not_busy(priv); |
| 249 | if (ret) |
| 250 | return ret; |
| 251 | |
| 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) |
| 317 | pr_err("%s ret:%d abort timeout:%d\n", __func__, ret, timeout); |
| 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 | |
| 350 | debug("%s: regs=<0x%p> mapped=<0x%p> mapped_size=<0x%lx>\n", |
| 351 | __func__, 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); |
| 364 | if (priv->clock_rate < 0) { |
| 365 | clk_disable(&clk); |
| 366 | return priv->clock_rate; |
| 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); |
| 397 | struct dm_spi_slave_platdata *slave_plat = dev_get_parent_platdata(dev); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 398 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 399 | if (slave_plat->cs >= STM32_QSPI_MAX_CHIP) |
Christophe Kerello | 495f3b2 | 2018-05-14 15:42:54 +0200 | [diff] [blame] | 400 | return -ENODEV; |
| 401 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 402 | if (priv->cs_used != slave_plat->cs) { |
| 403 | struct stm32_qspi_flash *flash = &priv->flash[slave_plat->cs]; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 404 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 405 | priv->cs_used = slave_plat->cs; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 406 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 407 | if (flash->initialized) { |
| 408 | /* Set the configuration: speed + cs */ |
| 409 | writel(flash->cr, &priv->regs->cr); |
| 410 | writel(flash->dcr, &priv->regs->dcr); |
| 411 | } else { |
| 412 | /* Set chip select */ |
| 413 | clrsetbits_le32(&priv->regs->cr, STM32_QSPI_CR_FSEL, |
| 414 | priv->cs_used ? STM32_QSPI_CR_FSEL : 0); |
| 415 | |
| 416 | /* Save the configuration: speed + cs */ |
| 417 | flash->cr = readl(&priv->regs->cr); |
| 418 | flash->dcr = readl(&priv->regs->dcr); |
| 419 | |
| 420 | flash->initialized = true; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | setbits_le32(&priv->regs->cr, STM32_QSPI_CR_EN); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 425 | |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | static int stm32_qspi_release_bus(struct udevice *dev) |
| 430 | { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 431 | struct stm32_qspi_priv *priv = dev_get_priv(dev->parent); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 432 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 433 | clrbits_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 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 438 | static int stm32_qspi_set_speed(struct udevice *bus, uint speed) |
| 439 | { |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 440 | struct stm32_qspi_priv *priv = dev_get_priv(bus); |
Patrick Delaunay | 936abad | 2018-05-14 15:42:50 +0200 | [diff] [blame] | 441 | u32 qspi_clk = priv->clock_rate; |
| 442 | u32 prescaler = 255; |
| 443 | u32 csht; |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 444 | int ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 445 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 446 | if (speed > 0) { |
| 447 | prescaler = DIV_ROUND_UP(qspi_clk, speed) - 1; |
| 448 | if (prescaler > 255) |
| 449 | prescaler = 255; |
| 450 | else if (prescaler < 0) |
| 451 | prescaler = 0; |
| 452 | } |
| 453 | |
Patrick Delaunay | 936abad | 2018-05-14 15:42:50 +0200 | [diff] [blame] | 454 | csht = DIV_ROUND_UP((5 * qspi_clk) / (prescaler + 1), 100000000); |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 455 | csht = (csht - 1) & STM32_QSPI_DCR_CSHT_MASK; |
| 456 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 457 | ret = _stm32_qspi_wait_for_not_busy(priv); |
| 458 | if (ret) |
| 459 | return ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 460 | |
| 461 | clrsetbits_le32(&priv->regs->cr, |
| 462 | STM32_QSPI_CR_PRESCALER_MASK << |
| 463 | STM32_QSPI_CR_PRESCALER_SHIFT, |
| 464 | prescaler << STM32_QSPI_CR_PRESCALER_SHIFT); |
| 465 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 466 | clrsetbits_le32(&priv->regs->dcr, |
| 467 | STM32_QSPI_DCR_CSHT_MASK << STM32_QSPI_DCR_CSHT_SHIFT, |
| 468 | csht << STM32_QSPI_DCR_CSHT_SHIFT); |
| 469 | |
| 470 | debug("%s: regs=%p, speed=%d\n", __func__, priv->regs, |
| 471 | (qspi_clk / (prescaler + 1))); |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
| 476 | static int stm32_qspi_set_mode(struct udevice *bus, uint mode) |
| 477 | { |
| 478 | struct stm32_qspi_priv *priv = dev_get_priv(bus); |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 479 | int ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 480 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 481 | ret = _stm32_qspi_wait_for_not_busy(priv); |
| 482 | if (ret) |
| 483 | return ret; |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 484 | |
| 485 | if ((mode & SPI_CPHA) && (mode & SPI_CPOL)) |
| 486 | setbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE); |
| 487 | else if (!(mode & SPI_CPHA) && !(mode & SPI_CPOL)) |
| 488 | clrbits_le32(&priv->regs->dcr, STM32_QSPI_DCR_CKMODE); |
| 489 | else |
| 490 | return -ENODEV; |
| 491 | |
| 492 | if (mode & SPI_CS_HIGH) |
| 493 | return -ENODEV; |
| 494 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 495 | debug("%s: regs=%p, mode=%d rx: ", __func__, priv->regs, mode); |
| 496 | |
| 497 | if (mode & SPI_RX_QUAD) |
| 498 | debug("quad, tx: "); |
| 499 | else if (mode & SPI_RX_DUAL) |
| 500 | debug("dual, tx: "); |
| 501 | else |
| 502 | debug("single, tx: "); |
| 503 | |
| 504 | if (mode & SPI_TX_QUAD) |
| 505 | debug("quad\n"); |
| 506 | else if (mode & SPI_TX_DUAL) |
| 507 | debug("dual\n"); |
| 508 | else |
| 509 | debug("single\n"); |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 514 | static const struct spi_controller_mem_ops stm32_qspi_mem_ops = { |
| 515 | .exec_op = stm32_qspi_exec_op, |
| 516 | }; |
| 517 | |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 518 | static const struct dm_spi_ops stm32_qspi_ops = { |
| 519 | .claim_bus = stm32_qspi_claim_bus, |
| 520 | .release_bus = stm32_qspi_release_bus, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 521 | .set_speed = stm32_qspi_set_speed, |
| 522 | .set_mode = stm32_qspi_set_mode, |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 523 | .mem_ops = &stm32_qspi_mem_ops, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 524 | }; |
| 525 | |
| 526 | static const struct udevice_id stm32_qspi_ids[] = { |
| 527 | { .compatible = "st,stm32-qspi" }, |
Christophe Kerello | 76afe56 | 2018-05-14 15:42:53 +0200 | [diff] [blame] | 528 | { .compatible = "st,stm32f469-qspi" }, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 529 | { } |
| 530 | }; |
| 531 | |
| 532 | U_BOOT_DRIVER(stm32_qspi) = { |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 533 | .name = "stm32_qspi", |
| 534 | .id = UCLASS_SPI, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 535 | .of_match = stm32_qspi_ids, |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 536 | .ops = &stm32_qspi_ops, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 537 | .priv_auto_alloc_size = sizeof(struct stm32_qspi_priv), |
Christophe Kerello | 321d153 | 2019-04-05 11:46:50 +0200 | [diff] [blame^] | 538 | .probe = stm32_qspi_probe, |
Michael Kurz | d4363ba | 2017-01-22 16:04:30 +0100 | [diff] [blame] | 539 | }; |