Lines Matching +full:clock +full:- +full:presc
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) STMicroelectronics 2018 - All Rights Reserved
9 #include <linux/dma-mapping.h>
23 #include <linux/spi/spi-mem.h>
91 #define STM32_AUTOSUSPEND_DELAY -1
96 u32 presc; member
131 sr = readl_relaxed(qspi->io_base + QSPI_SR); in stm32_qspi_irq()
135 cr = readl_relaxed(qspi->io_base + QSPI_CR); in stm32_qspi_irq()
137 writel_relaxed(cr, qspi->io_base + QSPI_CR); in stm32_qspi_irq()
138 complete(&qspi->data_completion); in stm32_qspi_irq()
158 u32 len = op->data.nbytes, sr; in stm32_qspi_tx_poll()
162 if (op->data.dir == SPI_MEM_DATA_IN) { in stm32_qspi_tx_poll()
164 buf = op->data.buf.in; in stm32_qspi_tx_poll()
168 buf = (u8 *)op->data.buf.out; in stm32_qspi_tx_poll()
171 while (len--) { in stm32_qspi_tx_poll()
172 ret = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, in stm32_qspi_tx_poll()
176 dev_err(qspi->dev, "fifo timeout (len:%d stat:%#x)\n", in stm32_qspi_tx_poll()
180 tx_fifo(buf++, qspi->io_base + QSPI_DR); in stm32_qspi_tx_poll()
189 memcpy_fromio(op->data.buf.in, qspi->mm_base + op->addr.val, in stm32_qspi_tx_mm()
190 op->data.nbytes); in stm32_qspi_tx_mm()
212 if (op->data.dir == SPI_MEM_DATA_IN) { in stm32_qspi_tx_dma()
214 dma_ch = qspi->dma_chrx; in stm32_qspi_tx_dma()
217 dma_ch = qspi->dma_chtx; in stm32_qspi_tx_dma()
221 * spi_map_buf return -EINVAL if the buffer is not DMA-able in stm32_qspi_tx_dma()
222 * (DMA-able: in vmalloc | kmap | virt_addr_valid) in stm32_qspi_tx_dma()
224 err = spi_controller_dma_map_mem_op_data(qspi->ctrl, op, &sgt); in stm32_qspi_tx_dma()
231 err = -ENOMEM; in stm32_qspi_tx_dma()
235 cr = readl_relaxed(qspi->io_base + QSPI_CR); in stm32_qspi_tx_dma()
237 reinit_completion(&qspi->dma_completion); in stm32_qspi_tx_dma()
238 desc->callback = stm32_qspi_dma_callback; in stm32_qspi_tx_dma()
239 desc->callback_param = &qspi->dma_completion; in stm32_qspi_tx_dma()
247 writel_relaxed(cr | CR_DMAEN, qspi->io_base + QSPI_CR); in stm32_qspi_tx_dma()
250 if (!wait_for_completion_timeout(&qspi->dma_completion, in stm32_qspi_tx_dma()
252 err = -ETIMEDOUT; in stm32_qspi_tx_dma()
258 writel_relaxed(cr & ~CR_DMAEN, qspi->io_base + QSPI_CR); in stm32_qspi_tx_dma()
260 spi_controller_dma_unmap_mem_op_data(qspi->ctrl, op, &sgt); in stm32_qspi_tx_dma()
267 if (!op->data.nbytes) in stm32_qspi_tx()
270 if (qspi->fmode == CCR_FMODE_MM) in stm32_qspi_tx()
272 else if ((op->data.dir == SPI_MEM_DATA_IN && qspi->dma_chrx) || in stm32_qspi_tx()
273 (op->data.dir == SPI_MEM_DATA_OUT && qspi->dma_chtx)) in stm32_qspi_tx()
284 return readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_SR, sr, in stm32_qspi_wait_nobusy()
295 if (!op->data.nbytes) in stm32_qspi_wait_cmd()
298 if (readl_relaxed(qspi->io_base + QSPI_SR) & SR_TCF) in stm32_qspi_wait_cmd()
301 reinit_completion(&qspi->data_completion); in stm32_qspi_wait_cmd()
302 cr = readl_relaxed(qspi->io_base + QSPI_CR); in stm32_qspi_wait_cmd()
303 writel_relaxed(cr | CR_TCIE | CR_TEIE, qspi->io_base + QSPI_CR); in stm32_qspi_wait_cmd()
305 if (!wait_for_completion_timeout(&qspi->data_completion, in stm32_qspi_wait_cmd()
307 err = -ETIMEDOUT; in stm32_qspi_wait_cmd()
309 sr = readl_relaxed(qspi->io_base + QSPI_SR); in stm32_qspi_wait_cmd()
311 err = -EIO; in stm32_qspi_wait_cmd()
316 writel_relaxed(FCR_CTCF | FCR_CTEF, qspi->io_base + QSPI_FCR); in stm32_qspi_wait_cmd()
331 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master); in stm32_qspi_send()
332 struct stm32_qspi_flash *flash = &qspi->flash[mem->spi->chip_select]; in stm32_qspi_send()
336 dev_dbg(qspi->dev, "cmd:%#x mode:%d.%d.%d.%d addr:%#llx len:%#x\n", in stm32_qspi_send()
337 op->cmd.opcode, op->cmd.buswidth, op->addr.buswidth, in stm32_qspi_send()
338 op->dummy.buswidth, op->data.buswidth, in stm32_qspi_send()
339 op->addr.val, op->data.nbytes); in stm32_qspi_send()
345 addr_max = op->addr.val + op->data.nbytes + 1; in stm32_qspi_send()
347 if (op->data.dir == SPI_MEM_DATA_IN) { in stm32_qspi_send()
348 if (addr_max < qspi->mm_size && in stm32_qspi_send()
349 op->addr.buswidth) in stm32_qspi_send()
350 qspi->fmode = CCR_FMODE_MM; in stm32_qspi_send()
352 qspi->fmode = CCR_FMODE_INDR; in stm32_qspi_send()
354 qspi->fmode = CCR_FMODE_INDW; in stm32_qspi_send()
357 cr = readl_relaxed(qspi->io_base + QSPI_CR); in stm32_qspi_send()
359 cr |= FIELD_PREP(CR_PRESC_MASK, flash->presc); in stm32_qspi_send()
360 cr |= FIELD_PREP(CR_FSEL, flash->cs); in stm32_qspi_send()
361 writel_relaxed(cr, qspi->io_base + QSPI_CR); in stm32_qspi_send()
363 if (op->data.nbytes) in stm32_qspi_send()
364 writel_relaxed(op->data.nbytes - 1, in stm32_qspi_send()
365 qspi->io_base + QSPI_DLR); in stm32_qspi_send()
367 qspi->fmode = CCR_FMODE_INDW; in stm32_qspi_send()
369 ccr = qspi->fmode; in stm32_qspi_send()
370 ccr |= FIELD_PREP(CCR_INST_MASK, op->cmd.opcode); in stm32_qspi_send()
372 stm32_qspi_get_mode(qspi, op->cmd.buswidth)); in stm32_qspi_send()
374 if (op->addr.nbytes) { in stm32_qspi_send()
376 stm32_qspi_get_mode(qspi, op->addr.buswidth)); in stm32_qspi_send()
377 ccr |= FIELD_PREP(CCR_ADSIZE_MASK, op->addr.nbytes - 1); in stm32_qspi_send()
380 if (op->dummy.buswidth && op->dummy.nbytes) in stm32_qspi_send()
382 op->dummy.nbytes * 8 / op->dummy.buswidth); in stm32_qspi_send()
384 if (op->data.nbytes) { in stm32_qspi_send()
386 stm32_qspi_get_mode(qspi, op->data.buswidth)); in stm32_qspi_send()
389 writel_relaxed(ccr, qspi->io_base + QSPI_CCR); in stm32_qspi_send()
391 if (op->addr.nbytes && qspi->fmode != CCR_FMODE_MM) in stm32_qspi_send()
392 writel_relaxed(op->addr.val, qspi->io_base + QSPI_AR); in stm32_qspi_send()
398 * -error case in stm32_qspi_send()
399 * -read memory map: prefetching must be stopped if we read the last in stm32_qspi_send()
400 * byte of device (device size - fifo size). like device size is not in stm32_qspi_send()
403 if (err || qspi->fmode == CCR_FMODE_MM) in stm32_qspi_send()
414 cr = readl_relaxed(qspi->io_base + QSPI_CR) | CR_ABORT; in stm32_qspi_send()
415 writel_relaxed(cr, qspi->io_base + QSPI_CR); in stm32_qspi_send()
418 timeout = readl_relaxed_poll_timeout_atomic(qspi->io_base + QSPI_CR, in stm32_qspi_send()
422 writel_relaxed(FCR_CTCF, qspi->io_base + QSPI_FCR); in stm32_qspi_send()
425 dev_err(qspi->dev, "%s err:%d abort timeout:%d\n", in stm32_qspi_send()
433 struct stm32_qspi *qspi = spi_controller_get_devdata(mem->spi->master); in stm32_qspi_exec_op()
436 ret = pm_runtime_get_sync(qspi->dev); in stm32_qspi_exec_op()
440 mutex_lock(&qspi->lock); in stm32_qspi_exec_op()
442 mutex_unlock(&qspi->lock); in stm32_qspi_exec_op()
444 pm_runtime_mark_last_busy(qspi->dev); in stm32_qspi_exec_op()
445 pm_runtime_put_autosuspend(qspi->dev); in stm32_qspi_exec_op()
452 struct spi_controller *ctrl = spi->master; in stm32_qspi_setup()
455 u32 presc; in stm32_qspi_setup() local
458 if (ctrl->busy) in stm32_qspi_setup()
459 return -EBUSY; in stm32_qspi_setup()
461 if (!spi->max_speed_hz) in stm32_qspi_setup()
462 return -EINVAL; in stm32_qspi_setup()
464 ret = pm_runtime_get_sync(qspi->dev); in stm32_qspi_setup()
468 presc = DIV_ROUND_UP(qspi->clk_rate, spi->max_speed_hz) - 1; in stm32_qspi_setup()
470 flash = &qspi->flash[spi->chip_select]; in stm32_qspi_setup()
471 flash->qspi = qspi; in stm32_qspi_setup()
472 flash->cs = spi->chip_select; in stm32_qspi_setup()
473 flash->presc = presc; in stm32_qspi_setup()
475 mutex_lock(&qspi->lock); in stm32_qspi_setup()
476 qspi->cr_reg = 3 << CR_FTHRES_SHIFT | CR_SSHIFT | CR_EN; in stm32_qspi_setup()
477 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR); in stm32_qspi_setup()
480 qspi->dcr_reg = DCR_FSIZE_MASK; in stm32_qspi_setup()
481 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR); in stm32_qspi_setup()
482 mutex_unlock(&qspi->lock); in stm32_qspi_setup()
484 pm_runtime_mark_last_busy(qspi->dev); in stm32_qspi_setup()
485 pm_runtime_put_autosuspend(qspi->dev); in stm32_qspi_setup()
493 struct device *dev = qspi->dev; in stm32_qspi_dma_setup()
500 dma_cfg.src_addr = qspi->phys_base + QSPI_DR; in stm32_qspi_dma_setup()
501 dma_cfg.dst_addr = qspi->phys_base + QSPI_DR; in stm32_qspi_dma_setup()
505 qspi->dma_chrx = dma_request_chan(dev, "rx"); in stm32_qspi_dma_setup()
506 if (IS_ERR(qspi->dma_chrx)) { in stm32_qspi_dma_setup()
507 ret = PTR_ERR(qspi->dma_chrx); in stm32_qspi_dma_setup()
508 qspi->dma_chrx = NULL; in stm32_qspi_dma_setup()
509 if (ret == -EPROBE_DEFER) in stm32_qspi_dma_setup()
512 if (dmaengine_slave_config(qspi->dma_chrx, &dma_cfg)) { in stm32_qspi_dma_setup()
514 dma_release_channel(qspi->dma_chrx); in stm32_qspi_dma_setup()
515 qspi->dma_chrx = NULL; in stm32_qspi_dma_setup()
519 qspi->dma_chtx = dma_request_chan(dev, "tx"); in stm32_qspi_dma_setup()
520 if (IS_ERR(qspi->dma_chtx)) { in stm32_qspi_dma_setup()
521 ret = PTR_ERR(qspi->dma_chtx); in stm32_qspi_dma_setup()
522 qspi->dma_chtx = NULL; in stm32_qspi_dma_setup()
524 if (dmaengine_slave_config(qspi->dma_chtx, &dma_cfg)) { in stm32_qspi_dma_setup()
526 dma_release_channel(qspi->dma_chtx); in stm32_qspi_dma_setup()
527 qspi->dma_chtx = NULL; in stm32_qspi_dma_setup()
532 init_completion(&qspi->dma_completion); in stm32_qspi_dma_setup()
534 if (ret != -EPROBE_DEFER) in stm32_qspi_dma_setup()
542 if (qspi->dma_chtx) in stm32_qspi_dma_free()
543 dma_release_channel(qspi->dma_chtx); in stm32_qspi_dma_free()
544 if (qspi->dma_chrx) in stm32_qspi_dma_free()
545 dma_release_channel(qspi->dma_chrx); in stm32_qspi_dma_free()
558 struct device *dev = &pdev->dev; in stm32_qspi_probe()
567 return -ENOMEM; in stm32_qspi_probe()
570 qspi->ctrl = ctrl; in stm32_qspi_probe()
573 qspi->io_base = devm_ioremap_resource(dev, res); in stm32_qspi_probe()
574 if (IS_ERR(qspi->io_base)) { in stm32_qspi_probe()
575 ret = PTR_ERR(qspi->io_base); in stm32_qspi_probe()
579 qspi->phys_base = res->start; in stm32_qspi_probe()
582 qspi->mm_base = devm_ioremap_resource(dev, res); in stm32_qspi_probe()
583 if (IS_ERR(qspi->mm_base)) { in stm32_qspi_probe()
584 ret = PTR_ERR(qspi->mm_base); in stm32_qspi_probe()
588 qspi->mm_size = resource_size(res); in stm32_qspi_probe()
589 if (qspi->mm_size > STM32_QSPI_MAX_MMAP_SZ) { in stm32_qspi_probe()
590 ret = -EINVAL; in stm32_qspi_probe()
607 init_completion(&qspi->data_completion); in stm32_qspi_probe()
609 qspi->clk = devm_clk_get(dev, NULL); in stm32_qspi_probe()
610 if (IS_ERR(qspi->clk)) { in stm32_qspi_probe()
611 ret = PTR_ERR(qspi->clk); in stm32_qspi_probe()
615 qspi->clk_rate = clk_get_rate(qspi->clk); in stm32_qspi_probe()
616 if (!qspi->clk_rate) { in stm32_qspi_probe()
617 ret = -EINVAL; in stm32_qspi_probe()
621 ret = clk_prepare_enable(qspi->clk); in stm32_qspi_probe()
623 dev_err(dev, "can not enable the clock\n"); in stm32_qspi_probe()
630 if (ret == -EPROBE_DEFER) in stm32_qspi_probe()
638 qspi->dev = dev; in stm32_qspi_probe()
644 mutex_init(&qspi->lock); in stm32_qspi_probe()
646 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD in stm32_qspi_probe()
648 ctrl->setup = stm32_qspi_setup; in stm32_qspi_probe()
649 ctrl->bus_num = -1; in stm32_qspi_probe()
650 ctrl->mem_ops = &stm32_qspi_mem_ops; in stm32_qspi_probe()
651 ctrl->num_chipselect = STM32_QSPI_MAX_NORCHIP; in stm32_qspi_probe()
652 ctrl->dev.of_node = dev->of_node; in stm32_qspi_probe()
670 pm_runtime_get_sync(qspi->dev); in stm32_qspi_probe()
672 writel_relaxed(0, qspi->io_base + QSPI_CR); in stm32_qspi_probe()
673 mutex_destroy(&qspi->lock); in stm32_qspi_probe()
674 pm_runtime_put_noidle(qspi->dev); in stm32_qspi_probe()
675 pm_runtime_disable(qspi->dev); in stm32_qspi_probe()
676 pm_runtime_set_suspended(qspi->dev); in stm32_qspi_probe()
677 pm_runtime_dont_use_autosuspend(qspi->dev); in stm32_qspi_probe()
681 clk_disable_unprepare(qspi->clk); in stm32_qspi_probe()
683 spi_master_put(qspi->ctrl); in stm32_qspi_probe()
692 pm_runtime_get_sync(qspi->dev); in stm32_qspi_remove()
694 writel_relaxed(0, qspi->io_base + QSPI_CR); in stm32_qspi_remove()
696 mutex_destroy(&qspi->lock); in stm32_qspi_remove()
697 pm_runtime_put_noidle(qspi->dev); in stm32_qspi_remove()
698 pm_runtime_disable(qspi->dev); in stm32_qspi_remove()
699 pm_runtime_set_suspended(qspi->dev); in stm32_qspi_remove()
700 pm_runtime_dont_use_autosuspend(qspi->dev); in stm32_qspi_remove()
701 clk_disable_unprepare(qspi->clk); in stm32_qspi_remove()
710 clk_disable_unprepare(qspi->clk); in stm32_qspi_runtime_suspend()
719 return clk_prepare_enable(qspi->clk); in stm32_qspi_runtime_resume()
734 clk_prepare_enable(qspi->clk); in stm32_qspi_resume()
736 writel_relaxed(qspi->cr_reg, qspi->io_base + QSPI_CR); in stm32_qspi_resume()
737 writel_relaxed(qspi->dcr_reg, qspi->io_base + QSPI_DCR); in stm32_qspi_resume()
739 pm_runtime_mark_last_busy(qspi->dev); in stm32_qspi_resume()
740 pm_runtime_put_autosuspend(qspi->dev); in stm32_qspi_resume()
752 {.compatible = "st,stm32f469-qspi"},
761 .name = "stm32-qspi",