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