Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Copyright (C) STMicroelectronics 2018 - All Rights Reserved |
| 4 | * Author: Ludovic Barre <ludovic.barre@st.com> for STMicroelectronics. |
| 5 | */ |
| 6 | #include <linux/bitfield.h> |
| 7 | #include <linux/clk.h> |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 8 | #include <linux/dmaengine.h> |
| 9 | #include <linux/dma-mapping.h> |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 10 | #include <linux/errno.h> |
| 11 | #include <linux/io.h> |
| 12 | #include <linux/iopoll.h> |
| 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/mutex.h> |
| 16 | #include <linux/of.h> |
| 17 | #include <linux/of_device.h> |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 18 | #include <linux/pinctrl/consumer.h> |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 19 | #include <linux/pm_runtime.h> |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/reset.h> |
| 22 | #include <linux/sizes.h> |
| 23 | #include <linux/spi/spi-mem.h> |
| 24 | |
| 25 | #define QSPI_CR 0x00 |
| 26 | #define CR_EN BIT(0) |
| 27 | #define CR_ABORT BIT(1) |
| 28 | #define CR_DMAEN BIT(2) |
| 29 | #define CR_TCEN BIT(3) |
| 30 | #define CR_SSHIFT BIT(4) |
| 31 | #define CR_DFM BIT(6) |
| 32 | #define CR_FSEL BIT(7) |
Patrice Chotard | 94613d5 | 2019-06-20 15:13:23 +0200 | [diff] [blame] | 33 | #define CR_FTHRES_SHIFT 8 |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 34 | #define CR_TEIE BIT(16) |
| 35 | #define CR_TCIE BIT(17) |
| 36 | #define CR_FTIE BIT(18) |
| 37 | #define CR_SMIE BIT(19) |
| 38 | #define CR_TOIE BIT(20) |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 39 | #define CR_APMS BIT(22) |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 40 | #define CR_PRESC_MASK GENMASK(31, 24) |
| 41 | |
| 42 | #define QSPI_DCR 0x04 |
| 43 | #define DCR_FSIZE_MASK GENMASK(20, 16) |
| 44 | |
| 45 | #define QSPI_SR 0x08 |
| 46 | #define SR_TEF BIT(0) |
| 47 | #define SR_TCF BIT(1) |
| 48 | #define SR_FTF BIT(2) |
| 49 | #define SR_SMF BIT(3) |
| 50 | #define SR_TOF BIT(4) |
| 51 | #define SR_BUSY BIT(5) |
| 52 | #define SR_FLEVEL_MASK GENMASK(13, 8) |
| 53 | |
| 54 | #define QSPI_FCR 0x0c |
| 55 | #define FCR_CTEF BIT(0) |
| 56 | #define FCR_CTCF BIT(1) |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 57 | #define FCR_CSMF BIT(3) |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 58 | |
| 59 | #define QSPI_DLR 0x10 |
| 60 | |
| 61 | #define QSPI_CCR 0x14 |
| 62 | #define CCR_INST_MASK GENMASK(7, 0) |
| 63 | #define CCR_IMODE_MASK GENMASK(9, 8) |
| 64 | #define CCR_ADMODE_MASK GENMASK(11, 10) |
| 65 | #define CCR_ADSIZE_MASK GENMASK(13, 12) |
| 66 | #define CCR_DCYC_MASK GENMASK(22, 18) |
| 67 | #define CCR_DMODE_MASK GENMASK(25, 24) |
| 68 | #define CCR_FMODE_MASK GENMASK(27, 26) |
| 69 | #define CCR_FMODE_INDW (0U << 26) |
| 70 | #define CCR_FMODE_INDR (1U << 26) |
| 71 | #define CCR_FMODE_APM (2U << 26) |
| 72 | #define CCR_FMODE_MM (3U << 26) |
| 73 | #define CCR_BUSWIDTH_0 0x0 |
| 74 | #define CCR_BUSWIDTH_1 0x1 |
| 75 | #define CCR_BUSWIDTH_2 0x2 |
| 76 | #define CCR_BUSWIDTH_4 0x3 |
| 77 | |
| 78 | #define QSPI_AR 0x18 |
| 79 | #define QSPI_ABR 0x1c |
| 80 | #define QSPI_DR 0x20 |
| 81 | #define QSPI_PSMKR 0x24 |
| 82 | #define QSPI_PSMAR 0x28 |
| 83 | #define QSPI_PIR 0x2c |
| 84 | #define QSPI_LPTR 0x30 |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 85 | |
| 86 | #define STM32_QSPI_MAX_MMAP_SZ SZ_256M |
| 87 | #define STM32_QSPI_MAX_NORCHIP 2 |
| 88 | |
| 89 | #define STM32_FIFO_TIMEOUT_US 30000 |
| 90 | #define STM32_BUSY_TIMEOUT_US 100000 |
| 91 | #define STM32_ABT_TIMEOUT_US 100000 |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 92 | #define STM32_COMP_TIMEOUT_MS 1000 |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 93 | #define STM32_AUTOSUSPEND_DELAY -1 |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 94 | |
| 95 | struct stm32_qspi_flash { |
| 96 | struct stm32_qspi *qspi; |
| 97 | u32 cs; |
| 98 | u32 presc; |
| 99 | }; |
| 100 | |
| 101 | struct stm32_qspi { |
| 102 | struct device *dev; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 103 | struct spi_controller *ctrl; |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 104 | phys_addr_t phys_base; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 105 | void __iomem *io_base; |
| 106 | void __iomem *mm_base; |
| 107 | resource_size_t mm_size; |
| 108 | struct clk *clk; |
| 109 | u32 clk_rate; |
| 110 | struct stm32_qspi_flash flash[STM32_QSPI_MAX_NORCHIP]; |
| 111 | struct completion data_completion; |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 112 | struct completion match_completion; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 113 | u32 fmode; |
| 114 | |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 115 | struct dma_chan *dma_chtx; |
| 116 | struct dma_chan *dma_chrx; |
| 117 | struct completion dma_completion; |
| 118 | |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 119 | u32 cr_reg; |
| 120 | u32 dcr_reg; |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 121 | unsigned long status_timeout; |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 122 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 123 | /* |
| 124 | * to protect device configuration, could be different between |
| 125 | * 2 flash access (bk1, bk2) |
| 126 | */ |
| 127 | struct mutex lock; |
| 128 | }; |
| 129 | |
| 130 | static irqreturn_t stm32_qspi_irq(int irq, void *dev_id) |
| 131 | { |
| 132 | struct stm32_qspi *qspi = (struct stm32_qspi *)dev_id; |
| 133 | u32 cr, sr; |
| 134 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 135 | cr = readl_relaxed(qspi->io_base + QSPI_CR); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 136 | sr = readl_relaxed(qspi->io_base + QSPI_SR); |
| 137 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 138 | if (cr & CR_SMIE && sr & SR_SMF) { |
| 139 | /* disable irq */ |
| 140 | cr &= ~CR_SMIE; |
| 141 | writel_relaxed(cr, qspi->io_base + QSPI_CR); |
| 142 | complete(&qspi->match_completion); |
| 143 | |
| 144 | return IRQ_HANDLED; |
| 145 | } |
| 146 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 147 | if (sr & (SR_TEF | SR_TCF)) { |
| 148 | /* disable irq */ |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 149 | cr &= ~CR_TCIE & ~CR_TEIE; |
| 150 | writel_relaxed(cr, qspi->io_base + QSPI_CR); |
| 151 | complete(&qspi->data_completion); |
| 152 | } |
| 153 | |
| 154 | return IRQ_HANDLED; |
| 155 | } |
| 156 | |
| 157 | static void stm32_qspi_read_fifo(u8 *val, void __iomem *addr) |
| 158 | { |
| 159 | *val = readb_relaxed(addr); |
| 160 | } |
| 161 | |
| 162 | static void stm32_qspi_write_fifo(u8 *val, void __iomem *addr) |
| 163 | { |
| 164 | writeb_relaxed(*val, addr); |
| 165 | } |
| 166 | |
| 167 | static int stm32_qspi_tx_poll(struct stm32_qspi *qspi, |
| 168 | const struct spi_mem_op *op) |
| 169 | { |
| 170 | void (*tx_fifo)(u8 *val, void __iomem *addr); |
| 171 | u32 len = op->data.nbytes, sr; |
| 172 | u8 *buf; |
| 173 | int ret; |
| 174 | |
| 175 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 176 | tx_fifo = stm32_qspi_read_fifo; |
| 177 | buf = op->data.buf.in; |
| 178 | |
| 179 | } else { |
| 180 | tx_fifo = stm32_qspi_write_fifo; |
| 181 | buf = (u8 *)op->data.buf.out; |
| 182 | } |
| 183 | |
| 184 | while (len--) { |
| 185 | ret = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, |
| 186 | sr, (sr & SR_FTF), 1, |
| 187 | STM32_FIFO_TIMEOUT_US); |
| 188 | if (ret) { |
| 189 | dev_err(qspi->dev, "fifo timeout (len:%d stat:%#x)\n", |
| 190 | len, sr); |
| 191 | return ret; |
| 192 | } |
| 193 | tx_fifo(buf++, qspi->io_base + QSPI_DR); |
| 194 | } |
| 195 | |
| 196 | return 0; |
| 197 | } |
| 198 | |
| 199 | static int stm32_qspi_tx_mm(struct stm32_qspi *qspi, |
| 200 | const struct spi_mem_op *op) |
| 201 | { |
| 202 | memcpy_fromio(op->data.buf.in, qspi->mm_base + op->addr.val, |
| 203 | op->data.nbytes); |
| 204 | return 0; |
| 205 | } |
| 206 | |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 207 | static void stm32_qspi_dma_callback(void *arg) |
| 208 | { |
| 209 | struct completion *dma_completion = arg; |
| 210 | |
| 211 | complete(dma_completion); |
| 212 | } |
| 213 | |
| 214 | static int stm32_qspi_tx_dma(struct stm32_qspi *qspi, |
| 215 | const struct spi_mem_op *op) |
| 216 | { |
| 217 | struct dma_async_tx_descriptor *desc; |
| 218 | enum dma_transfer_direction dma_dir; |
| 219 | struct dma_chan *dma_ch; |
| 220 | struct sg_table sgt; |
| 221 | dma_cookie_t cookie; |
| 222 | u32 cr, t_out; |
| 223 | int err; |
| 224 | |
| 225 | if (op->data.dir == SPI_MEM_DATA_IN) { |
| 226 | dma_dir = DMA_DEV_TO_MEM; |
| 227 | dma_ch = qspi->dma_chrx; |
| 228 | } else { |
| 229 | dma_dir = DMA_MEM_TO_DEV; |
| 230 | dma_ch = qspi->dma_chtx; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * spi_map_buf return -EINVAL if the buffer is not DMA-able |
| 235 | * (DMA-able: in vmalloc | kmap | virt_addr_valid) |
| 236 | */ |
| 237 | err = spi_controller_dma_map_mem_op_data(qspi->ctrl, op, &sgt); |
| 238 | if (err) |
| 239 | return err; |
| 240 | |
| 241 | desc = dmaengine_prep_slave_sg(dma_ch, sgt.sgl, sgt.nents, |
| 242 | dma_dir, DMA_PREP_INTERRUPT); |
| 243 | if (!desc) { |
| 244 | err = -ENOMEM; |
| 245 | goto out_unmap; |
| 246 | } |
| 247 | |
| 248 | cr = readl_relaxed(qspi->io_base + QSPI_CR); |
| 249 | |
| 250 | reinit_completion(&qspi->dma_completion); |
| 251 | desc->callback = stm32_qspi_dma_callback; |
| 252 | desc->callback_param = &qspi->dma_completion; |
| 253 | cookie = dmaengine_submit(desc); |
| 254 | err = dma_submit_error(cookie); |
| 255 | if (err) |
| 256 | goto out; |
| 257 | |
| 258 | dma_async_issue_pending(dma_ch); |
| 259 | |
| 260 | writel_relaxed(cr | CR_DMAEN, qspi->io_base + QSPI_CR); |
| 261 | |
| 262 | t_out = sgt.nents * STM32_COMP_TIMEOUT_MS; |
Ludovic Barre | 775c4c0 | 2019-06-27 09:43:59 +0200 | [diff] [blame] | 263 | if (!wait_for_completion_timeout(&qspi->dma_completion, |
| 264 | msecs_to_jiffies(t_out))) |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 265 | err = -ETIMEDOUT; |
| 266 | |
| 267 | if (err) |
| 268 | dmaengine_terminate_all(dma_ch); |
| 269 | |
| 270 | out: |
| 271 | writel_relaxed(cr & ~CR_DMAEN, qspi->io_base + QSPI_CR); |
| 272 | out_unmap: |
| 273 | spi_controller_dma_unmap_mem_op_data(qspi->ctrl, op, &sgt); |
| 274 | |
| 275 | return err; |
| 276 | } |
| 277 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 278 | static int stm32_qspi_tx(struct stm32_qspi *qspi, const struct spi_mem_op *op) |
| 279 | { |
| 280 | if (!op->data.nbytes) |
| 281 | return 0; |
| 282 | |
| 283 | if (qspi->fmode == CCR_FMODE_MM) |
| 284 | return stm32_qspi_tx_mm(qspi, op); |
Patrice Chotard | f3530f2 | 2021-04-19 14:15:40 +0200 | [diff] [blame] | 285 | else if (((op->data.dir == SPI_MEM_DATA_IN && qspi->dma_chrx) || |
| 286 | (op->data.dir == SPI_MEM_DATA_OUT && qspi->dma_chtx)) && |
| 287 | op->data.nbytes > 4) |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 288 | if (!stm32_qspi_tx_dma(qspi, op)) |
| 289 | return 0; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 290 | |
| 291 | return stm32_qspi_tx_poll(qspi, op); |
| 292 | } |
| 293 | |
| 294 | static int stm32_qspi_wait_nobusy(struct stm32_qspi *qspi) |
| 295 | { |
| 296 | u32 sr; |
| 297 | |
| 298 | return readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, sr, |
| 299 | !(sr & SR_BUSY), 1, |
| 300 | STM32_BUSY_TIMEOUT_US); |
| 301 | } |
| 302 | |
| 303 | static int stm32_qspi_wait_cmd(struct stm32_qspi *qspi, |
| 304 | const struct spi_mem_op *op) |
| 305 | { |
| 306 | u32 cr, sr; |
| 307 | int err = 0; |
| 308 | |
| 309 | if (!op->data.nbytes) |
| 310 | return stm32_qspi_wait_nobusy(qspi); |
| 311 | |
| 312 | if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF) |
| 313 | goto out; |
| 314 | |
| 315 | reinit_completion(&qspi->data_completion); |
| 316 | cr = readl_relaxed(qspi->io_base + QSPI_CR); |
| 317 | writel_relaxed(cr | CR_TCIE | CR_TEIE, qspi->io_base + QSPI_CR); |
| 318 | |
Ludovic Barre | 775c4c0 | 2019-06-27 09:43:59 +0200 | [diff] [blame] | 319 | if (!wait_for_completion_timeout(&qspi->data_completion, |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 320 | msecs_to_jiffies(STM32_COMP_TIMEOUT_MS))) { |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 321 | err = -ETIMEDOUT; |
| 322 | } else { |
| 323 | sr = readl_relaxed(qspi->io_base + QSPI_SR); |
| 324 | if (sr & SR_TEF) |
| 325 | err = -EIO; |
| 326 | } |
| 327 | |
| 328 | out: |
| 329 | /* clear flags */ |
| 330 | writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR); |
| 331 | |
| 332 | return err; |
| 333 | } |
| 334 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 335 | static int stm32_qspi_wait_poll_status(struct stm32_qspi *qspi, |
| 336 | const struct spi_mem_op *op) |
| 337 | { |
| 338 | u32 cr; |
| 339 | |
| 340 | reinit_completion(&qspi->match_completion); |
| 341 | cr = readl_relaxed(qspi->io_base + QSPI_CR); |
| 342 | writel_relaxed(cr | CR_SMIE, qspi->io_base + QSPI_CR); |
| 343 | |
| 344 | if (!wait_for_completion_timeout(&qspi->match_completion, |
| 345 | msecs_to_jiffies(qspi->status_timeout))) |
| 346 | return -ETIMEDOUT; |
| 347 | |
| 348 | writel_relaxed(FCR_CSMF, qspi->io_base + QSPI_FCR); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 353 | static int stm32_qspi_get_mode(struct stm32_qspi *qspi, u8 buswidth) |
| 354 | { |
| 355 | if (buswidth == 4) |
| 356 | return CCR_BUSWIDTH_4; |
| 357 | |
| 358 | return buswidth; |
| 359 | } |
| 360 | |
| 361 | static int stm32_qspi_send(struct spi_mem *mem, const struct spi_mem_op *op) |
| 362 | { |
| 363 | struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master); |
| 364 | struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select]; |
Patrice Chotard | 18674de | 2021-04-19 14:15:41 +0200 | [diff] [blame] | 365 | u32 ccr, cr; |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 366 | int timeout, err = 0, err_poll_status = 0; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 367 | |
| 368 | dev_dbg(qspi->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n", |
| 369 | op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, |
| 370 | op->dummy.buswidth, op->data.buswidth, |
| 371 | op->addr.val, op->data.nbytes); |
| 372 | |
| 373 | err = stm32_qspi_wait_nobusy(qspi); |
| 374 | if (err) |
| 375 | goto abort; |
| 376 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 377 | cr = readl_relaxed(qspi->io_base + QSPI_CR); |
| 378 | cr &= ~CR_PRESC_MASK & ~CR_FSEL; |
| 379 | cr |= FIELD_PREP(CR_PRESC_MASK, flash->presc); |
| 380 | cr |= FIELD_PREP(CR_FSEL, flash->cs); |
| 381 | writel_relaxed(cr, qspi->io_base + QSPI_CR); |
| 382 | |
| 383 | if (op->data.nbytes) |
| 384 | writel_relaxed(op->data.nbytes - 1, |
| 385 | qspi->io_base + QSPI_DLR); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 386 | |
| 387 | ccr = qspi->fmode; |
| 388 | ccr |= FIELD_PREP(CCR_INST_MASK, op->cmd.opcode); |
| 389 | ccr |= FIELD_PREP(CCR_IMODE_MASK, |
| 390 | stm32_qspi_get_mode(qspi, op->cmd.buswidth)); |
| 391 | |
| 392 | if (op->addr.nbytes) { |
| 393 | ccr |= FIELD_PREP(CCR_ADMODE_MASK, |
| 394 | stm32_qspi_get_mode(qspi, op->addr.buswidth)); |
| 395 | ccr |= FIELD_PREP(CCR_ADSIZE_MASK, op->addr.nbytes - 1); |
| 396 | } |
| 397 | |
| 398 | if (op->dummy.buswidth && op->dummy.nbytes) |
| 399 | ccr |= FIELD_PREP(CCR_DCYC_MASK, |
| 400 | op->dummy.nbytes * 8 / op->dummy.buswidth); |
| 401 | |
| 402 | if (op->data.nbytes) { |
| 403 | ccr |= FIELD_PREP(CCR_DMODE_MASK, |
| 404 | stm32_qspi_get_mode(qspi, op->data.buswidth)); |
| 405 | } |
| 406 | |
| 407 | writel_relaxed(ccr, qspi->io_base + QSPI_CCR); |
| 408 | |
| 409 | if (op->addr.nbytes && qspi->fmode != CCR_FMODE_MM) |
| 410 | writel_relaxed(op->addr.val, qspi->io_base + QSPI_AR); |
| 411 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 412 | if (qspi->fmode == CCR_FMODE_APM) |
| 413 | err_poll_status = stm32_qspi_wait_poll_status(qspi, op); |
| 414 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 415 | err = stm32_qspi_tx(qspi, op); |
| 416 | |
| 417 | /* |
| 418 | * Abort in: |
| 419 | * -error case |
| 420 | * -read memory map: prefetching must be stopped if we read the last |
| 421 | * byte of device (device size - fifo size). like device size is not |
| 422 | * knows, the prefetching is always stop. |
| 423 | */ |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 424 | if (err || err_poll_status || qspi->fmode == CCR_FMODE_MM) |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 425 | goto abort; |
| 426 | |
| 427 | /* wait end of tx in indirect mode */ |
| 428 | err = stm32_qspi_wait_cmd(qspi, op); |
| 429 | if (err) |
| 430 | goto abort; |
| 431 | |
| 432 | return 0; |
| 433 | |
| 434 | abort: |
| 435 | cr = readl_relaxed(qspi->io_base + QSPI_CR) | CR_ABORT; |
| 436 | writel_relaxed(cr, qspi->io_base + QSPI_CR); |
| 437 | |
| 438 | /* wait clear of abort bit by hw */ |
| 439 | timeout = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_CR, |
| 440 | cr, !(cr & CR_ABORT), 1, |
| 441 | STM32_ABT_TIMEOUT_US); |
| 442 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 443 | writel_relaxed(FCR_CTCF | FCR_CSMF, qspi->io_base + QSPI_FCR); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 444 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 445 | if (err || err_poll_status || timeout) |
| 446 | dev_err(qspi->dev, "%s err:%d err_poll_status:%d abort timeout:%d\n", |
| 447 | __func__, err, err_poll_status, timeout); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 448 | |
| 449 | return err; |
| 450 | } |
| 451 | |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 452 | static int stm32_qspi_poll_status(struct spi_mem *mem, const struct spi_mem_op *op, |
| 453 | u16 mask, u16 match, |
| 454 | unsigned long initial_delay_us, |
| 455 | unsigned long polling_rate_us, |
| 456 | unsigned long timeout_ms) |
| 457 | { |
| 458 | struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master); |
| 459 | int ret; |
| 460 | |
| 461 | if (!spi_mem_supports_op(mem, op)) |
| 462 | return -EOPNOTSUPP; |
| 463 | |
| 464 | ret = pm_runtime_get_sync(qspi->dev); |
| 465 | if (ret < 0) { |
| 466 | pm_runtime_put_noidle(qspi->dev); |
| 467 | return ret; |
| 468 | } |
| 469 | |
| 470 | mutex_lock(&qspi->lock); |
| 471 | |
| 472 | writel_relaxed(mask, qspi->io_base + QSPI_PSMKR); |
| 473 | writel_relaxed(match, qspi->io_base + QSPI_PSMAR); |
| 474 | qspi->fmode = CCR_FMODE_APM; |
| 475 | qspi->status_timeout = timeout_ms; |
| 476 | |
| 477 | ret = stm32_qspi_send(mem, op); |
| 478 | mutex_unlock(&qspi->lock); |
| 479 | |
| 480 | pm_runtime_mark_last_busy(qspi->dev); |
| 481 | pm_runtime_put_autosuspend(qspi->dev); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 486 | static int stm32_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) |
| 487 | { |
| 488 | struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master); |
| 489 | int ret; |
| 490 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 491 | ret = pm_runtime_get_sync(qspi->dev); |
Zhang Qilong | 88e1419 | 2020-11-06 09:53:57 +0800 | [diff] [blame] | 492 | if (ret < 0) { |
| 493 | pm_runtime_put_noidle(qspi->dev); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 494 | return ret; |
Zhang Qilong | 88e1419 | 2020-11-06 09:53:57 +0800 | [diff] [blame] | 495 | } |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 496 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 497 | mutex_lock(&qspi->lock); |
Patrice Chotard | 18674de | 2021-04-19 14:15:41 +0200 | [diff] [blame] | 498 | if (op->data.dir == SPI_MEM_DATA_IN && op->data.nbytes) |
| 499 | qspi->fmode = CCR_FMODE_INDR; |
| 500 | else |
| 501 | qspi->fmode = CCR_FMODE_INDW; |
| 502 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 503 | ret = stm32_qspi_send(mem, op); |
| 504 | mutex_unlock(&qspi->lock); |
| 505 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 506 | pm_runtime_mark_last_busy(qspi->dev); |
| 507 | pm_runtime_put_autosuspend(qspi->dev); |
| 508 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 509 | return ret; |
| 510 | } |
| 511 | |
Patrice Chotard | 18674de | 2021-04-19 14:15:41 +0200 | [diff] [blame] | 512 | static int stm32_qspi_dirmap_create(struct spi_mem_dirmap_desc *desc) |
| 513 | { |
| 514 | struct stm32_qspi *qspi = spi_controller_get_devdata(desc->mem->spi->master); |
| 515 | |
| 516 | if (desc->info.op_tmpl.data.dir == SPI_MEM_DATA_OUT) |
| 517 | return -EOPNOTSUPP; |
| 518 | |
| 519 | /* should never happen, as mm_base == null is an error probe exit condition */ |
| 520 | if (!qspi->mm_base && desc->info.op_tmpl.data.dir == SPI_MEM_DATA_IN) |
| 521 | return -EOPNOTSUPP; |
| 522 | |
| 523 | if (!qspi->mm_size) |
| 524 | return -EOPNOTSUPP; |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | static ssize_t stm32_qspi_dirmap_read(struct spi_mem_dirmap_desc *desc, |
| 530 | u64 offs, size_t len, void *buf) |
| 531 | { |
| 532 | struct stm32_qspi *qspi = spi_controller_get_devdata(desc->mem->spi->master); |
| 533 | struct spi_mem_op op; |
| 534 | u32 addr_max; |
| 535 | int ret; |
| 536 | |
| 537 | ret = pm_runtime_get_sync(qspi->dev); |
| 538 | if (ret < 0) { |
| 539 | pm_runtime_put_noidle(qspi->dev); |
| 540 | return ret; |
| 541 | } |
| 542 | |
| 543 | mutex_lock(&qspi->lock); |
| 544 | /* make a local copy of desc op_tmpl and complete dirmap rdesc |
| 545 | * spi_mem_op template with offs, len and *buf in order to get |
| 546 | * all needed transfer information into struct spi_mem_op |
| 547 | */ |
| 548 | memcpy(&op, &desc->info.op_tmpl, sizeof(struct spi_mem_op)); |
Arnd Bergmann | 14ef64e | 2021-04-22 15:38:57 +0200 | [diff] [blame] | 549 | dev_dbg(qspi->dev, "%s len = 0x%zx offs = 0x%llx buf = 0x%p\n", __func__, len, offs, buf); |
Patrice Chotard | 18674de | 2021-04-19 14:15:41 +0200 | [diff] [blame] | 550 | |
| 551 | op.data.nbytes = len; |
| 552 | op.addr.val = desc->info.offset + offs; |
| 553 | op.data.buf.in = buf; |
| 554 | |
| 555 | addr_max = op.addr.val + op.data.nbytes + 1; |
| 556 | if (addr_max < qspi->mm_size && op.addr.buswidth) |
| 557 | qspi->fmode = CCR_FMODE_MM; |
| 558 | else |
| 559 | qspi->fmode = CCR_FMODE_INDR; |
| 560 | |
| 561 | ret = stm32_qspi_send(desc->mem, &op); |
| 562 | mutex_unlock(&qspi->lock); |
| 563 | |
| 564 | pm_runtime_mark_last_busy(qspi->dev); |
| 565 | pm_runtime_put_autosuspend(qspi->dev); |
| 566 | |
| 567 | return ret ?: len; |
| 568 | } |
| 569 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 570 | static int stm32_qspi_setup(struct spi_device *spi) |
| 571 | { |
| 572 | struct spi_controller *ctrl = spi->master; |
| 573 | struct stm32_qspi *qspi = spi_controller_get_devdata(ctrl); |
| 574 | struct stm32_qspi_flash *flash; |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 575 | u32 presc; |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 576 | int ret; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 577 | |
| 578 | if (ctrl->busy) |
| 579 | return -EBUSY; |
| 580 | |
| 581 | if (!spi->max_speed_hz) |
| 582 | return -EINVAL; |
| 583 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 584 | ret = pm_runtime_get_sync(qspi->dev); |
Zhang Qilong | 88e1419 | 2020-11-06 09:53:57 +0800 | [diff] [blame] | 585 | if (ret < 0) { |
| 586 | pm_runtime_put_noidle(qspi->dev); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 587 | return ret; |
Zhang Qilong | 88e1419 | 2020-11-06 09:53:57 +0800 | [diff] [blame] | 588 | } |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 589 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 590 | presc = DIV_ROUND_UP(qspi->clk_rate, spi->max_speed_hz) - 1; |
| 591 | |
| 592 | flash = &qspi->flash[spi->chip_select]; |
| 593 | flash->qspi = qspi; |
| 594 | flash->cs = spi->chip_select; |
| 595 | flash->presc = presc; |
| 596 | |
| 597 | mutex_lock(&qspi->lock); |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 598 | qspi->cr_reg = CR_APMS | 3 << CR_FTHRES_SHIFT | CR_SSHIFT | CR_EN; |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 599 | writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 600 | |
| 601 | /* set dcr fsize to max address */ |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 602 | qspi->dcr_reg = DCR_FSIZE_MASK; |
| 603 | writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 604 | mutex_unlock(&qspi->lock); |
| 605 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 606 | pm_runtime_mark_last_busy(qspi->dev); |
| 607 | pm_runtime_put_autosuspend(qspi->dev); |
| 608 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 609 | return 0; |
| 610 | } |
| 611 | |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 612 | static int stm32_qspi_dma_setup(struct stm32_qspi *qspi) |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 613 | { |
| 614 | struct dma_slave_config dma_cfg; |
| 615 | struct device *dev = qspi->dev; |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 616 | int ret = 0; |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 617 | |
| 618 | memset(&dma_cfg, 0, sizeof(dma_cfg)); |
| 619 | |
| 620 | dma_cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 621 | dma_cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; |
| 622 | dma_cfg.src_addr = qspi->phys_base + QSPI_DR; |
| 623 | dma_cfg.dst_addr = qspi->phys_base + QSPI_DR; |
| 624 | dma_cfg.src_maxburst = 4; |
| 625 | dma_cfg.dst_maxburst = 4; |
| 626 | |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 627 | qspi->dma_chrx = dma_request_chan(dev, "rx"); |
| 628 | if (IS_ERR(qspi->dma_chrx)) { |
| 629 | ret = PTR_ERR(qspi->dma_chrx); |
| 630 | qspi->dma_chrx = NULL; |
| 631 | if (ret == -EPROBE_DEFER) |
| 632 | goto out; |
| 633 | } else { |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 634 | if (dmaengine_slave_config(qspi->dma_chrx, &dma_cfg)) { |
| 635 | dev_err(dev, "dma rx config failed\n"); |
| 636 | dma_release_channel(qspi->dma_chrx); |
| 637 | qspi->dma_chrx = NULL; |
| 638 | } |
| 639 | } |
| 640 | |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 641 | qspi->dma_chtx = dma_request_chan(dev, "tx"); |
| 642 | if (IS_ERR(qspi->dma_chtx)) { |
| 643 | ret = PTR_ERR(qspi->dma_chtx); |
| 644 | qspi->dma_chtx = NULL; |
| 645 | } else { |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 646 | if (dmaengine_slave_config(qspi->dma_chtx, &dma_cfg)) { |
| 647 | dev_err(dev, "dma tx config failed\n"); |
| 648 | dma_release_channel(qspi->dma_chtx); |
| 649 | qspi->dma_chtx = NULL; |
| 650 | } |
| 651 | } |
| 652 | |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 653 | out: |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 654 | init_completion(&qspi->dma_completion); |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 655 | |
| 656 | if (ret != -EPROBE_DEFER) |
| 657 | ret = 0; |
| 658 | |
| 659 | return ret; |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static void stm32_qspi_dma_free(struct stm32_qspi *qspi) |
| 663 | { |
| 664 | if (qspi->dma_chtx) |
| 665 | dma_release_channel(qspi->dma_chtx); |
| 666 | if (qspi->dma_chrx) |
| 667 | dma_release_channel(qspi->dma_chrx); |
| 668 | } |
| 669 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 670 | /* |
| 671 | * no special host constraint, so use default spi_mem_default_supports_op |
| 672 | * to check supported mode. |
| 673 | */ |
| 674 | static const struct spi_controller_mem_ops stm32_qspi_mem_ops = { |
Patrice Chotard | 18674de | 2021-04-19 14:15:41 +0200 | [diff] [blame] | 675 | .exec_op = stm32_qspi_exec_op, |
| 676 | .dirmap_create = stm32_qspi_dirmap_create, |
| 677 | .dirmap_read = stm32_qspi_dirmap_read, |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 678 | .poll_status = stm32_qspi_poll_status, |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 679 | }; |
| 680 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 681 | static int stm32_qspi_probe(struct platform_device *pdev) |
| 682 | { |
| 683 | struct device *dev = &pdev->dev; |
| 684 | struct spi_controller *ctrl; |
| 685 | struct reset_control *rstc; |
| 686 | struct stm32_qspi *qspi; |
| 687 | struct resource *res; |
| 688 | int ret, irq; |
| 689 | |
| 690 | ctrl = spi_alloc_master(dev, sizeof(*qspi)); |
| 691 | if (!ctrl) |
| 692 | return -ENOMEM; |
| 693 | |
| 694 | qspi = spi_controller_get_devdata(ctrl); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 695 | qspi->ctrl = ctrl; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 696 | |
| 697 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi"); |
| 698 | qspi->io_base = devm_ioremap_resource(dev, res); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 699 | if (IS_ERR(qspi->io_base)) { |
| 700 | ret = PTR_ERR(qspi->io_base); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 701 | goto err_master_put; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 702 | } |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 703 | |
Ludovic Barre | 245308c | 2019-03-25 18:01:40 +0100 | [diff] [blame] | 704 | qspi->phys_base = res->start; |
| 705 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 706 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mm"); |
| 707 | qspi->mm_base = devm_ioremap_resource(dev, res); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 708 | if (IS_ERR(qspi->mm_base)) { |
| 709 | ret = PTR_ERR(qspi->mm_base); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 710 | goto err_master_put; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 711 | } |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 712 | |
| 713 | qspi->mm_size = resource_size(res); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 714 | if (qspi->mm_size > STM32_QSPI_MAX_MMAP_SZ) { |
| 715 | ret = -EINVAL; |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 716 | goto err_master_put; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 717 | } |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 718 | |
| 719 | irq = platform_get_irq(pdev, 0); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 720 | if (irq < 0) { |
| 721 | ret = irq; |
| 722 | goto err_master_put; |
| 723 | } |
Fabien Dessenne | 4b562de | 2019-04-24 17:19:00 +0200 | [diff] [blame] | 724 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 725 | ret = devm_request_irq(dev, irq, stm32_qspi_irq, 0, |
| 726 | dev_name(dev), qspi); |
| 727 | if (ret) { |
| 728 | dev_err(dev, "failed to request irq\n"); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 729 | goto err_master_put; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | init_completion(&qspi->data_completion); |
Patrice Chotard | 86d1c6b | 2021-05-18 18:27:54 +0200 | [diff] [blame^] | 733 | init_completion(&qspi->match_completion); |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 734 | |
| 735 | qspi->clk = devm_clk_get(dev, NULL); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 736 | if (IS_ERR(qspi->clk)) { |
| 737 | ret = PTR_ERR(qspi->clk); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 738 | goto err_master_put; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 739 | } |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 740 | |
| 741 | qspi->clk_rate = clk_get_rate(qspi->clk); |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 742 | if (!qspi->clk_rate) { |
| 743 | ret = -EINVAL; |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 744 | goto err_master_put; |
Ludovic Barre | a88eceb | 2019-03-25 18:01:39 +0100 | [diff] [blame] | 745 | } |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 746 | |
| 747 | ret = clk_prepare_enable(qspi->clk); |
| 748 | if (ret) { |
| 749 | dev_err(dev, "can not enable the clock\n"); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 750 | goto err_master_put; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | rstc = devm_reset_control_get_exclusive(dev, NULL); |
Etienne Carriere | 8196f7b | 2020-02-03 14:50:47 +0100 | [diff] [blame] | 754 | if (IS_ERR(rstc)) { |
| 755 | ret = PTR_ERR(rstc); |
| 756 | if (ret == -EPROBE_DEFER) |
Patrice Chotard | 35700e2 | 2020-06-16 13:30:35 +0200 | [diff] [blame] | 757 | goto err_clk_disable; |
Etienne Carriere | 8196f7b | 2020-02-03 14:50:47 +0100 | [diff] [blame] | 758 | } else { |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 759 | reset_control_assert(rstc); |
| 760 | udelay(2); |
| 761 | reset_control_deassert(rstc); |
| 762 | } |
| 763 | |
| 764 | qspi->dev = dev; |
| 765 | platform_set_drvdata(pdev, qspi); |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 766 | ret = stm32_qspi_dma_setup(qspi); |
| 767 | if (ret) |
Patrice Chotard | 35700e2 | 2020-06-16 13:30:35 +0200 | [diff] [blame] | 768 | goto err_dma_free; |
Peter Ujfalusi | 658606f | 2019-12-12 15:55:49 +0200 | [diff] [blame] | 769 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 770 | mutex_init(&qspi->lock); |
| 771 | |
| 772 | ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD |
| 773 | | SPI_TX_DUAL | SPI_TX_QUAD; |
| 774 | ctrl->setup = stm32_qspi_setup; |
| 775 | ctrl->bus_num = -1; |
| 776 | ctrl->mem_ops = &stm32_qspi_mem_ops; |
| 777 | ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP; |
| 778 | ctrl->dev.of_node = dev->of_node; |
| 779 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 780 | pm_runtime_set_autosuspend_delay(dev, STM32_AUTOSUSPEND_DELAY); |
| 781 | pm_runtime_use_autosuspend(dev); |
| 782 | pm_runtime_set_active(dev); |
| 783 | pm_runtime_enable(dev); |
| 784 | pm_runtime_get_noresume(dev); |
| 785 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 786 | ret = devm_spi_register_master(dev, ctrl); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 787 | if (ret) |
Patrice Chotard | 35700e2 | 2020-06-16 13:30:35 +0200 | [diff] [blame] | 788 | goto err_pm_runtime_free; |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 789 | |
| 790 | pm_runtime_mark_last_busy(dev); |
| 791 | pm_runtime_put_autosuspend(dev); |
| 792 | |
| 793 | return 0; |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 794 | |
Patrice Chotard | 35700e2 | 2020-06-16 13:30:35 +0200 | [diff] [blame] | 795 | err_pm_runtime_free: |
| 796 | pm_runtime_get_sync(qspi->dev); |
| 797 | /* disable qspi */ |
| 798 | writel_relaxed(0, qspi->io_base + QSPI_CR); |
| 799 | mutex_destroy(&qspi->lock); |
| 800 | pm_runtime_put_noidle(qspi->dev); |
| 801 | pm_runtime_disable(qspi->dev); |
| 802 | pm_runtime_set_suspended(qspi->dev); |
| 803 | pm_runtime_dont_use_autosuspend(qspi->dev); |
| 804 | err_dma_free: |
| 805 | stm32_qspi_dma_free(qspi); |
| 806 | err_clk_disable: |
| 807 | clk_disable_unprepare(qspi->clk); |
Lionel Debieve | 4a08d6c | 2020-02-03 14:50:48 +0100 | [diff] [blame] | 808 | err_master_put: |
Patrice Chotard | 3c0af1d | 2019-10-04 14:36:06 +0200 | [diff] [blame] | 809 | spi_master_put(qspi->ctrl); |
| 810 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 811 | return ret; |
| 812 | } |
| 813 | |
| 814 | static int stm32_qspi_remove(struct platform_device *pdev) |
| 815 | { |
| 816 | struct stm32_qspi *qspi = platform_get_drvdata(pdev); |
| 817 | |
Patrice Chotard | 35700e2 | 2020-06-16 13:30:35 +0200 | [diff] [blame] | 818 | pm_runtime_get_sync(qspi->dev); |
| 819 | /* disable qspi */ |
| 820 | writel_relaxed(0, qspi->io_base + QSPI_CR); |
| 821 | stm32_qspi_dma_free(qspi); |
| 822 | mutex_destroy(&qspi->lock); |
| 823 | pm_runtime_put_noidle(qspi->dev); |
| 824 | pm_runtime_disable(qspi->dev); |
| 825 | pm_runtime_set_suspended(qspi->dev); |
| 826 | pm_runtime_dont_use_autosuspend(qspi->dev); |
| 827 | clk_disable_unprepare(qspi->clk); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 828 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 829 | return 0; |
| 830 | } |
| 831 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 832 | static int __maybe_unused stm32_qspi_runtime_suspend(struct device *dev) |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 833 | { |
| 834 | struct stm32_qspi *qspi = dev_get_drvdata(dev); |
| 835 | |
| 836 | clk_disable_unprepare(qspi->clk); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 837 | |
| 838 | return 0; |
| 839 | } |
| 840 | |
| 841 | static int __maybe_unused stm32_qspi_runtime_resume(struct device *dev) |
| 842 | { |
| 843 | struct stm32_qspi *qspi = dev_get_drvdata(dev); |
| 844 | |
| 845 | return clk_prepare_enable(qspi->clk); |
| 846 | } |
| 847 | |
| 848 | static int __maybe_unused stm32_qspi_suspend(struct device *dev) |
| 849 | { |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 850 | pinctrl_pm_select_sleep_state(dev); |
| 851 | |
Christophe Kerello | 102e9d1 | 2021-04-19 14:15:39 +0200 | [diff] [blame] | 852 | return pm_runtime_force_suspend(dev); |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | static int __maybe_unused stm32_qspi_resume(struct device *dev) |
| 856 | { |
| 857 | struct stm32_qspi *qspi = dev_get_drvdata(dev); |
Christophe Kerello | 102e9d1 | 2021-04-19 14:15:39 +0200 | [diff] [blame] | 858 | int ret; |
| 859 | |
| 860 | ret = pm_runtime_force_resume(dev); |
| 861 | if (ret < 0) |
| 862 | return ret; |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 863 | |
| 864 | pinctrl_pm_select_default_state(dev); |
Christophe Kerello | 102e9d1 | 2021-04-19 14:15:39 +0200 | [diff] [blame] | 865 | |
| 866 | ret = pm_runtime_get_sync(dev); |
| 867 | if (ret < 0) { |
| 868 | pm_runtime_put_noidle(dev); |
| 869 | return ret; |
| 870 | } |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 871 | |
| 872 | writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR); |
| 873 | writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR); |
| 874 | |
Christophe Kerello | 102e9d1 | 2021-04-19 14:15:39 +0200 | [diff] [blame] | 875 | pm_runtime_mark_last_busy(dev); |
| 876 | pm_runtime_put_autosuspend(dev); |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 877 | |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Patrice Chotard | 9d282c1 | 2020-04-17 14:12:41 +0200 | [diff] [blame] | 881 | static const struct dev_pm_ops stm32_qspi_pm_ops = { |
| 882 | SET_RUNTIME_PM_OPS(stm32_qspi_runtime_suspend, |
| 883 | stm32_qspi_runtime_resume, NULL) |
| 884 | SET_SYSTEM_SLEEP_PM_OPS(stm32_qspi_suspend, stm32_qspi_resume) |
| 885 | }; |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 886 | |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 887 | static const struct of_device_id stm32_qspi_match[] = { |
| 888 | {.compatible = "st,stm32f469-qspi"}, |
| 889 | {} |
| 890 | }; |
| 891 | MODULE_DEVICE_TABLE(of, stm32_qspi_match); |
| 892 | |
| 893 | static struct platform_driver stm32_qspi_driver = { |
| 894 | .probe = stm32_qspi_probe, |
| 895 | .remove = stm32_qspi_remove, |
| 896 | .driver = { |
| 897 | .name = "stm32-qspi", |
| 898 | .of_match_table = stm32_qspi_match, |
Ludovic Barre | 2e541b6 | 2019-03-08 14:12:21 +0100 | [diff] [blame] | 899 | .pm = &stm32_qspi_pm_ops, |
Ludovic Barre | c530cd1 | 2018-10-05 09:43:03 +0200 | [diff] [blame] | 900 | }, |
| 901 | }; |
| 902 | module_platform_driver(stm32_qspi_driver); |
| 903 | |
| 904 | MODULE_AUTHOR("Ludovic Barre <ludovic.barre@st.com>"); |
| 905 | MODULE_DESCRIPTION("STMicroelectronics STM32 quad spi driver"); |
| 906 | MODULE_LICENSE("GPL v2"); |