1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
5 #include <linux/dmapool.h>
6 #include <linux/dma-mapping.h>
7 #include <linux/interconnect.h>
8 #include <linux/interrupt.h>
10 #include <linux/module.h>
12 #include <linux/platform_device.h>
13 #include <linux/pinctrl/consumer.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/pm_opp.h>
16 #include <linux/spi/spi.h>
17 #include <linux/spi/spi-mem.h>
21 #define QSPI_BYTES_PER_WORD 4
23 #define MSTR_CONFIG 0x0000
24 #define FULL_CYCLE_MODE BIT(3)
25 #define FB_CLK_EN BIT(4)
26 #define PIN_HOLDN BIT(6)
27 #define PIN_WPN BIT(7)
28 #define DMA_ENABLE BIT(8)
29 #define BIG_ENDIAN_MODE BIT(9)
30 #define SPI_MODE_MSK 0xc00
31 #define SPI_MODE_SHFT 10
32 #define CHIP_SELECT_NUM BIT(12)
33 #define SBL_EN BIT(13)
34 #define LPA_BASE_MSK 0x3c000
35 #define LPA_BASE_SHFT 14
36 #define TX_DATA_DELAY_MSK 0xc0000
37 #define TX_DATA_DELAY_SHFT 18
38 #define TX_CLK_DELAY_MSK 0x300000
39 #define TX_CLK_DELAY_SHFT 20
40 #define TX_CS_N_DELAY_MSK 0xc00000
41 #define TX_CS_N_DELAY_SHFT 22
42 #define TX_DATA_OE_DELAY_MSK 0x3000000
43 #define TX_DATA_OE_DELAY_SHFT 24
45 #define AHB_MASTER_CFG 0x0004
46 #define HMEM_TYPE_START_MID_TRANS_MSK 0x7
47 #define HMEM_TYPE_START_MID_TRANS_SHFT 0
48 #define HMEM_TYPE_LAST_TRANS_MSK 0x38
49 #define HMEM_TYPE_LAST_TRANS_SHFT 3
50 #define USE_HMEMTYPE_LAST_ON_DESC_OR_CHAIN_MSK 0xc0
51 #define USE_HMEMTYPE_LAST_ON_DESC_OR_CHAIN_SHFT 6
52 #define HMEMTYPE_READ_TRANS_MSK 0x700
53 #define HMEMTYPE_READ_TRANS_SHFT 8
54 #define HSHARED BIT(11)
55 #define HINNERSHARED BIT(12)
57 #define MSTR_INT_EN 0x000C
58 #define MSTR_INT_STATUS 0x0010
59 #define RESP_FIFO_UNDERRUN BIT(0)
60 #define RESP_FIFO_NOT_EMPTY BIT(1)
61 #define RESP_FIFO_RDY BIT(2)
62 #define HRESP_FROM_NOC_ERR BIT(3)
63 #define WR_FIFO_EMPTY BIT(9)
64 #define WR_FIFO_FULL BIT(10)
65 #define WR_FIFO_OVERRUN BIT(11)
66 #define TRANSACTION_DONE BIT(16)
67 #define DMA_CHAIN_DONE BIT(31)
68 #define QSPI_ERR_IRQS (RESP_FIFO_UNDERRUN | HRESP_FROM_NOC_ERR | \
70 #define QSPI_ALL_IRQS (QSPI_ERR_IRQS | RESP_FIFO_RDY | \
71 WR_FIFO_EMPTY | WR_FIFO_FULL | \
72 TRANSACTION_DONE | DMA_CHAIN_DONE)
74 #define PIO_XFER_CTRL 0x0014
75 #define REQUEST_COUNT_MSK 0xffff
77 #define PIO_XFER_CFG 0x0018
78 #define TRANSFER_DIRECTION BIT(0)
79 #define MULTI_IO_MODE_MSK 0xe
80 #define MULTI_IO_MODE_SHFT 1
81 #define TRANSFER_FRAGMENT BIT(8)
88 #define DMA_DESC_SINGLE_SPI 1
89 #define DMA_DESC_DUAL_SPI 2
90 #define DMA_DESC_QUAD_SPI 3
92 #define PIO_XFER_STATUS 0x001c
93 #define WR_FIFO_BYTES_MSK 0xffff0000
94 #define WR_FIFO_BYTES_SHFT 16
96 #define PIO_DATAOUT_1B 0x0020
97 #define PIO_DATAOUT_4B 0x0024
99 #define RD_FIFO_CFG 0x0028
100 #define CONTINUOUS_MODE BIT(0)
102 #define RD_FIFO_STATUS 0x002c
103 #define FIFO_EMPTY BIT(11)
104 #define WR_CNTS_MSK 0x7f0
105 #define WR_CNTS_SHFT 4
106 #define RDY_64BYTE BIT(3)
107 #define RDY_32BYTE BIT(2)
108 #define RDY_16BYTE BIT(1)
109 #define FIFO_RDY BIT(0)
111 #define RD_FIFO_RESET 0x0030
112 #define RESET_FIFO BIT(0)
114 #define NEXT_DMA_DESC_ADDR 0x0040
115 #define CURRENT_DMA_DESC_ADDR 0x0044
116 #define CURRENT_MEM_ADDR 0x0048
118 #define CUR_MEM_ADDR 0x0048
119 #define HW_VERSION 0x004c
120 #define RD_FIFO 0x0050
121 #define SAMPLING_CLK_CFG 0x0090
122 #define SAMPLING_CLK_STATUS 0x0094
124 #define QSPI_ALIGN_REQ 32
131 struct qspi_cmd_desc
{
147 unsigned int rem_bytes
;
148 unsigned int buswidth
;
160 * Number of entries in sgt returned from spi framework that-
161 * will be supported. Can be modified as required.
162 * In practice, given max_dma_len is 64KB, the number of
163 * entries is not expected to exceed 1.
165 #define QSPI_MAX_SG 5
170 struct clk_bulk_data
*clks
;
171 struct qspi_xfer xfer
;
172 struct dma_pool
*dma_cmd_pool
;
173 dma_addr_t dma_cmd_desc
[QSPI_MAX_SG
];
174 void *virt_cmd_desc
[QSPI_MAX_SG
];
175 unsigned int n_cmd_desc
;
176 struct icc_path
*icc_path_cpu_to_qspi
;
177 unsigned long last_speed
;
178 /* Lock to protect data accessed by IRQs */
182 static u32
qspi_buswidth_to_iomode(struct qcom_qspi
*ctrl
,
183 unsigned int buswidth
)
193 dev_warn_once(ctrl
->dev
,
194 "Unexpected bus width: %u\n", buswidth
);
199 static void qcom_qspi_pio_xfer_cfg(struct qcom_qspi
*ctrl
)
203 const struct qspi_xfer
*xfer
;
206 pio_xfer_cfg
= readl(ctrl
->base
+ PIO_XFER_CFG
);
207 pio_xfer_cfg
&= ~TRANSFER_DIRECTION
;
208 pio_xfer_cfg
|= xfer
->dir
;
210 pio_xfer_cfg
&= ~TRANSFER_FRAGMENT
;
212 pio_xfer_cfg
|= TRANSFER_FRAGMENT
;
213 pio_xfer_cfg
&= ~MULTI_IO_MODE_MSK
;
214 iomode
= qspi_buswidth_to_iomode(ctrl
, xfer
->buswidth
);
215 pio_xfer_cfg
|= iomode
<< MULTI_IO_MODE_SHFT
;
217 writel(pio_xfer_cfg
, ctrl
->base
+ PIO_XFER_CFG
);
220 static void qcom_qspi_pio_xfer_ctrl(struct qcom_qspi
*ctrl
)
224 pio_xfer_ctrl
= readl(ctrl
->base
+ PIO_XFER_CTRL
);
225 pio_xfer_ctrl
&= ~REQUEST_COUNT_MSK
;
226 pio_xfer_ctrl
|= ctrl
->xfer
.rem_bytes
;
227 writel(pio_xfer_ctrl
, ctrl
->base
+ PIO_XFER_CTRL
);
230 static void qcom_qspi_pio_xfer(struct qcom_qspi
*ctrl
)
234 qcom_qspi_pio_xfer_cfg(ctrl
);
236 /* Ack any previous interrupts that might be hanging around */
237 writel(QSPI_ALL_IRQS
, ctrl
->base
+ MSTR_INT_STATUS
);
239 /* Setup new interrupts */
240 if (ctrl
->xfer
.dir
== QSPI_WRITE
)
241 ints
= QSPI_ERR_IRQS
| WR_FIFO_EMPTY
;
243 ints
= QSPI_ERR_IRQS
| RESP_FIFO_RDY
;
244 writel(ints
, ctrl
->base
+ MSTR_INT_EN
);
246 /* Kick off the transfer */
247 qcom_qspi_pio_xfer_ctrl(ctrl
);
250 static void qcom_qspi_handle_err(struct spi_controller
*host
,
251 struct spi_message
*msg
)
254 struct qcom_qspi
*ctrl
= spi_controller_get_devdata(host
);
258 spin_lock_irqsave(&ctrl
->lock
, flags
);
259 writel(0, ctrl
->base
+ MSTR_INT_EN
);
260 int_status
= readl(ctrl
->base
+ MSTR_INT_STATUS
);
261 writel(int_status
, ctrl
->base
+ MSTR_INT_STATUS
);
262 ctrl
->xfer
.rem_bytes
= 0;
264 /* free cmd descriptors if they are around (DMA mode) */
265 for (i
= 0; i
< ctrl
->n_cmd_desc
; i
++)
266 dma_pool_free(ctrl
->dma_cmd_pool
, ctrl
->virt_cmd_desc
[i
],
267 ctrl
->dma_cmd_desc
[i
]);
268 ctrl
->n_cmd_desc
= 0;
269 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
272 static int qcom_qspi_set_speed(struct qcom_qspi
*ctrl
, unsigned long speed_hz
)
275 unsigned int avg_bw_cpu
;
277 if (speed_hz
== ctrl
->last_speed
)
280 /* In regular operation (SBL_EN=1) core must be 4x transfer clock */
281 ret
= dev_pm_opp_set_rate(ctrl
->dev
, speed_hz
* 4);
283 dev_err(ctrl
->dev
, "Failed to set core clk %d\n", ret
);
288 * Set BW quota for CPU.
289 * We don't have explicit peak requirement so keep it equal to avg_bw.
291 avg_bw_cpu
= Bps_to_icc(speed_hz
);
292 ret
= icc_set_bw(ctrl
->icc_path_cpu_to_qspi
, avg_bw_cpu
, avg_bw_cpu
);
294 dev_err(ctrl
->dev
, "%s: ICC BW voting failed for cpu: %d\n",
299 ctrl
->last_speed
= speed_hz
;
304 static int qcom_qspi_alloc_desc(struct qcom_qspi
*ctrl
, dma_addr_t dma_ptr
,
307 struct qspi_cmd_desc
*virt_cmd_desc
, *prev
;
308 dma_addr_t dma_cmd_desc
;
310 /* allocate for dma cmd descriptor */
311 virt_cmd_desc
= dma_pool_alloc(ctrl
->dma_cmd_pool
, GFP_ATOMIC
| __GFP_ZERO
, &dma_cmd_desc
);
312 if (!virt_cmd_desc
) {
313 dev_warn_once(ctrl
->dev
, "Couldn't find memory for descriptor\n");
317 ctrl
->virt_cmd_desc
[ctrl
->n_cmd_desc
] = virt_cmd_desc
;
318 ctrl
->dma_cmd_desc
[ctrl
->n_cmd_desc
] = dma_cmd_desc
;
321 /* setup cmd descriptor */
322 virt_cmd_desc
->data_address
= dma_ptr
;
323 virt_cmd_desc
->direction
= ctrl
->xfer
.dir
;
324 virt_cmd_desc
->multi_io_mode
= qspi_buswidth_to_iomode(ctrl
, ctrl
->xfer
.buswidth
);
325 virt_cmd_desc
->fragment
= !ctrl
->xfer
.is_last
;
326 virt_cmd_desc
->length
= n_bytes
;
328 /* update previous descriptor */
329 if (ctrl
->n_cmd_desc
>= 2) {
330 prev
= (ctrl
->virt_cmd_desc
)[ctrl
->n_cmd_desc
- 2];
331 prev
->next_descriptor
= dma_cmd_desc
;
338 static int qcom_qspi_setup_dma_desc(struct qcom_qspi
*ctrl
,
339 struct spi_transfer
*xfer
)
342 struct sg_table
*sgt
;
343 dma_addr_t dma_ptr_sg
;
344 unsigned int dma_len_sg
;
347 if (ctrl
->n_cmd_desc
) {
348 dev_err(ctrl
->dev
, "Remnant dma buffers n_cmd_desc-%d\n", ctrl
->n_cmd_desc
);
352 sgt
= (ctrl
->xfer
.dir
== QSPI_READ
) ? &xfer
->rx_sg
: &xfer
->tx_sg
;
353 if (!sgt
->nents
|| sgt
->nents
> QSPI_MAX_SG
) {
354 dev_warn_once(ctrl
->dev
, "Cannot handle %d entries in scatter list\n", sgt
->nents
);
358 for (i
= 0; i
< sgt
->nents
; i
++) {
359 dma_ptr_sg
= sg_dma_address(sgt
->sgl
+ i
);
360 dma_len_sg
= sg_dma_len(sgt
->sgl
+ i
);
361 if (!IS_ALIGNED(dma_ptr_sg
, QSPI_ALIGN_REQ
)) {
362 dev_warn_once(ctrl
->dev
, "dma_address not aligned to %d\n", QSPI_ALIGN_REQ
);
366 * When reading with DMA the controller writes to memory 1 word
367 * at a time. If the length isn't a multiple of 4 bytes then
368 * the controller can clobber the things later in memory.
369 * Fallback to PIO to be safe.
371 if (ctrl
->xfer
.dir
== QSPI_READ
&& (dma_len_sg
& 0x03)) {
372 dev_warn_once(ctrl
->dev
, "fallback to PIO for read of size %#010x\n",
378 for (i
= 0; i
< sgt
->nents
; i
++) {
379 dma_ptr_sg
= sg_dma_address(sgt
->sgl
+ i
);
380 dma_len_sg
= sg_dma_len(sgt
->sgl
+ i
);
382 ret
= qcom_qspi_alloc_desc(ctrl
, dma_ptr_sg
, dma_len_sg
);
389 for (i
= 0; i
< ctrl
->n_cmd_desc
; i
++)
390 dma_pool_free(ctrl
->dma_cmd_pool
, ctrl
->virt_cmd_desc
[i
],
391 ctrl
->dma_cmd_desc
[i
]);
392 ctrl
->n_cmd_desc
= 0;
396 static void qcom_qspi_dma_xfer(struct qcom_qspi
*ctrl
)
398 /* Setup new interrupts */
399 writel(DMA_CHAIN_DONE
, ctrl
->base
+ MSTR_INT_EN
);
401 /* kick off transfer */
402 writel((u32
)((ctrl
->dma_cmd_desc
)[0]), ctrl
->base
+ NEXT_DMA_DESC_ADDR
);
405 /* Switch to DMA if transfer length exceeds this */
406 #define QSPI_MAX_BYTES_FIFO 64
408 static bool qcom_qspi_can_dma(struct spi_controller
*ctlr
,
409 struct spi_device
*slv
, struct spi_transfer
*xfer
)
411 return xfer
->len
> QSPI_MAX_BYTES_FIFO
;
414 static int qcom_qspi_transfer_one(struct spi_controller
*host
,
415 struct spi_device
*slv
,
416 struct spi_transfer
*xfer
)
418 struct qcom_qspi
*ctrl
= spi_controller_get_devdata(host
);
420 unsigned long speed_hz
;
424 speed_hz
= slv
->max_speed_hz
;
426 speed_hz
= xfer
->speed_hz
;
428 ret
= qcom_qspi_set_speed(ctrl
, speed_hz
);
432 spin_lock_irqsave(&ctrl
->lock
, flags
);
433 mstr_cfg
= readl(ctrl
->base
+ MSTR_CONFIG
);
435 /* We are half duplex, so either rx or tx will be set */
437 ctrl
->xfer
.dir
= QSPI_READ
;
438 ctrl
->xfer
.buswidth
= xfer
->rx_nbits
;
439 ctrl
->xfer
.rx_buf
= xfer
->rx_buf
;
441 ctrl
->xfer
.dir
= QSPI_WRITE
;
442 ctrl
->xfer
.buswidth
= xfer
->tx_nbits
;
443 ctrl
->xfer
.tx_buf
= xfer
->tx_buf
;
445 ctrl
->xfer
.is_last
= list_is_last(&xfer
->transfer_list
,
446 &host
->cur_msg
->transfers
);
447 ctrl
->xfer
.rem_bytes
= xfer
->len
;
449 if (xfer
->rx_sg
.nents
|| xfer
->tx_sg
.nents
) {
450 /* do DMA transfer */
451 if (!(mstr_cfg
& DMA_ENABLE
)) {
452 mstr_cfg
|= DMA_ENABLE
;
453 writel(mstr_cfg
, ctrl
->base
+ MSTR_CONFIG
);
456 ret
= qcom_qspi_setup_dma_desc(ctrl
, xfer
);
457 if (ret
!= -EAGAIN
) {
460 qcom_qspi_dma_xfer(ctrl
);
464 dev_warn_once(ctrl
->dev
, "DMA failure, falling back to PIO\n");
465 ret
= 0; /* We'll retry w/ PIO */
468 if (mstr_cfg
& DMA_ENABLE
) {
469 mstr_cfg
&= ~DMA_ENABLE
;
470 writel(mstr_cfg
, ctrl
->base
+ MSTR_CONFIG
);
472 qcom_qspi_pio_xfer(ctrl
);
475 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
480 /* We'll call spi_finalize_current_transfer() when done */
484 static int qcom_qspi_prepare_message(struct spi_controller
*host
,
485 struct spi_message
*message
)
488 struct qcom_qspi
*ctrl
;
489 int tx_data_oe_delay
= 1;
490 int tx_data_delay
= 1;
493 ctrl
= spi_controller_get_devdata(host
);
494 spin_lock_irqsave(&ctrl
->lock
, flags
);
496 mstr_cfg
= readl(ctrl
->base
+ MSTR_CONFIG
);
497 mstr_cfg
&= ~CHIP_SELECT_NUM
;
498 if (spi_get_chipselect(message
->spi
, 0))
499 mstr_cfg
|= CHIP_SELECT_NUM
;
501 mstr_cfg
|= FB_CLK_EN
| PIN_WPN
| PIN_HOLDN
| SBL_EN
| FULL_CYCLE_MODE
;
502 mstr_cfg
&= ~(SPI_MODE_MSK
| TX_DATA_OE_DELAY_MSK
| TX_DATA_DELAY_MSK
);
503 mstr_cfg
|= message
->spi
->mode
<< SPI_MODE_SHFT
;
504 mstr_cfg
|= tx_data_oe_delay
<< TX_DATA_OE_DELAY_SHFT
;
505 mstr_cfg
|= tx_data_delay
<< TX_DATA_DELAY_SHFT
;
506 mstr_cfg
&= ~DMA_ENABLE
;
508 writel(mstr_cfg
, ctrl
->base
+ MSTR_CONFIG
);
509 spin_unlock_irqrestore(&ctrl
->lock
, flags
);
514 static int qcom_qspi_alloc_dma(struct qcom_qspi
*ctrl
)
516 ctrl
->dma_cmd_pool
= dmam_pool_create("qspi cmd desc pool",
517 ctrl
->dev
, sizeof(struct qspi_cmd_desc
), 0, 0);
518 if (!ctrl
->dma_cmd_pool
)
524 static irqreturn_t
pio_read(struct qcom_qspi
*ctrl
)
528 unsigned int wr_cnts
;
529 unsigned int bytes_to_read
;
530 unsigned int words_to_read
;
535 rd_fifo_status
= readl(ctrl
->base
+ RD_FIFO_STATUS
);
537 if (!(rd_fifo_status
& FIFO_RDY
)) {
538 dev_dbg(ctrl
->dev
, "Spurious IRQ %#x\n", rd_fifo_status
);
542 wr_cnts
= (rd_fifo_status
& WR_CNTS_MSK
) >> WR_CNTS_SHFT
;
543 wr_cnts
= min(wr_cnts
, ctrl
->xfer
.rem_bytes
);
545 words_to_read
= wr_cnts
/ QSPI_BYTES_PER_WORD
;
546 bytes_to_read
= wr_cnts
% QSPI_BYTES_PER_WORD
;
549 word_buf
= ctrl
->xfer
.rx_buf
;
550 ctrl
->xfer
.rem_bytes
-= words_to_read
* QSPI_BYTES_PER_WORD
;
551 ioread32_rep(ctrl
->base
+ RD_FIFO
, word_buf
, words_to_read
);
552 ctrl
->xfer
.rx_buf
= word_buf
+ words_to_read
;
556 byte_buf
= ctrl
->xfer
.rx_buf
;
557 rd_fifo
= readl(ctrl
->base
+ RD_FIFO
);
558 ctrl
->xfer
.rem_bytes
-= bytes_to_read
;
559 for (i
= 0; i
< bytes_to_read
; i
++)
560 *byte_buf
++ = rd_fifo
>> (i
* BITS_PER_BYTE
);
561 ctrl
->xfer
.rx_buf
= byte_buf
;
567 static irqreturn_t
pio_write(struct qcom_qspi
*ctrl
)
569 const void *xfer_buf
= ctrl
->xfer
.tx_buf
;
571 const char *byte_buf
;
572 unsigned int wr_fifo_bytes
;
573 unsigned int wr_fifo_words
;
574 unsigned int wr_size
;
575 unsigned int rem_words
;
577 wr_fifo_bytes
= readl(ctrl
->base
+ PIO_XFER_STATUS
);
578 wr_fifo_bytes
>>= WR_FIFO_BYTES_SHFT
;
580 if (ctrl
->xfer
.rem_bytes
< QSPI_BYTES_PER_WORD
) {
581 /* Process the last 1-3 bytes */
582 wr_size
= min(wr_fifo_bytes
, ctrl
->xfer
.rem_bytes
);
583 ctrl
->xfer
.rem_bytes
-= wr_size
;
588 ctrl
->base
+ PIO_DATAOUT_1B
);
589 ctrl
->xfer
.tx_buf
= byte_buf
;
592 * Process all the whole words; to keep things simple we'll
593 * just wait for the next interrupt to handle the last 1-3
594 * bytes if we don't have an even number of words.
596 rem_words
= ctrl
->xfer
.rem_bytes
/ QSPI_BYTES_PER_WORD
;
597 wr_fifo_words
= wr_fifo_bytes
/ QSPI_BYTES_PER_WORD
;
599 wr_size
= min(rem_words
, wr_fifo_words
);
600 ctrl
->xfer
.rem_bytes
-= wr_size
* QSPI_BYTES_PER_WORD
;
603 iowrite32_rep(ctrl
->base
+ PIO_DATAOUT_4B
, word_buf
, wr_size
);
604 ctrl
->xfer
.tx_buf
= word_buf
+ wr_size
;
611 static irqreturn_t
qcom_qspi_irq(int irq
, void *dev_id
)
614 struct qcom_qspi
*ctrl
= dev_id
;
615 irqreturn_t ret
= IRQ_NONE
;
617 spin_lock(&ctrl
->lock
);
619 int_status
= readl(ctrl
->base
+ MSTR_INT_STATUS
);
620 writel(int_status
, ctrl
->base
+ MSTR_INT_STATUS
);
622 /* Ignore disabled interrupts */
623 int_status
&= readl(ctrl
->base
+ MSTR_INT_EN
);
625 /* PIO mode handling */
626 if (ctrl
->xfer
.dir
== QSPI_WRITE
) {
627 if (int_status
& WR_FIFO_EMPTY
)
628 ret
= pio_write(ctrl
);
630 if (int_status
& RESP_FIFO_RDY
)
631 ret
= pio_read(ctrl
);
634 if (int_status
& QSPI_ERR_IRQS
) {
635 if (int_status
& RESP_FIFO_UNDERRUN
)
636 dev_err(ctrl
->dev
, "IRQ error: FIFO underrun\n");
637 if (int_status
& WR_FIFO_OVERRUN
)
638 dev_err(ctrl
->dev
, "IRQ error: FIFO overrun\n");
639 if (int_status
& HRESP_FROM_NOC_ERR
)
640 dev_err(ctrl
->dev
, "IRQ error: NOC response error\n");
644 if (!ctrl
->xfer
.rem_bytes
) {
645 writel(0, ctrl
->base
+ MSTR_INT_EN
);
646 spi_finalize_current_transfer(dev_get_drvdata(ctrl
->dev
));
649 /* DMA mode handling */
650 if (int_status
& DMA_CHAIN_DONE
) {
653 writel(0, ctrl
->base
+ MSTR_INT_EN
);
654 ctrl
->xfer
.rem_bytes
= 0;
656 for (i
= 0; i
< ctrl
->n_cmd_desc
; i
++)
657 dma_pool_free(ctrl
->dma_cmd_pool
, ctrl
->virt_cmd_desc
[i
],
658 ctrl
->dma_cmd_desc
[i
]);
659 ctrl
->n_cmd_desc
= 0;
662 spi_finalize_current_transfer(dev_get_drvdata(ctrl
->dev
));
665 spin_unlock(&ctrl
->lock
);
669 static int qcom_qspi_adjust_op_size(struct spi_mem
*mem
, struct spi_mem_op
*op
)
672 * If qcom_qspi_can_dma() is going to return false we don't need to
675 if (op
->data
.nbytes
<= QSPI_MAX_BYTES_FIFO
)
679 * When reading, the transfer needs to be a multiple of 4 bytes so
680 * shrink the transfer if that's not true. The caller will then do a
681 * second transfer to finish things up.
683 if (op
->data
.dir
== SPI_MEM_DATA_IN
&& (op
->data
.nbytes
& 0x3))
684 op
->data
.nbytes
&= ~0x3;
689 static const struct spi_controller_mem_ops qcom_qspi_mem_ops
= {
690 .adjust_op_size
= qcom_qspi_adjust_op_size
,
693 static int qcom_qspi_probe(struct platform_device
*pdev
)
697 struct spi_controller
*host
;
698 struct qcom_qspi
*ctrl
;
702 host
= devm_spi_alloc_host(dev
, sizeof(*ctrl
));
706 platform_set_drvdata(pdev
, host
);
708 ctrl
= spi_controller_get_devdata(host
);
710 spin_lock_init(&ctrl
->lock
);
712 ctrl
->base
= devm_platform_ioremap_resource(pdev
, 0);
713 if (IS_ERR(ctrl
->base
))
714 return PTR_ERR(ctrl
->base
);
716 ctrl
->clks
= devm_kcalloc(dev
, QSPI_NUM_CLKS
,
717 sizeof(*ctrl
->clks
), GFP_KERNEL
);
721 ctrl
->clks
[QSPI_CLK_CORE
].id
= "core";
722 ctrl
->clks
[QSPI_CLK_IFACE
].id
= "iface";
723 ret
= devm_clk_bulk_get(dev
, QSPI_NUM_CLKS
, ctrl
->clks
);
727 ctrl
->icc_path_cpu_to_qspi
= devm_of_icc_get(dev
, "qspi-config");
728 if (IS_ERR(ctrl
->icc_path_cpu_to_qspi
))
729 return dev_err_probe(dev
, PTR_ERR(ctrl
->icc_path_cpu_to_qspi
),
730 "Failed to get cpu path\n");
732 /* Set BW vote for register access */
733 ret
= icc_set_bw(ctrl
->icc_path_cpu_to_qspi
, Bps_to_icc(1000),
736 dev_err(ctrl
->dev
, "%s: ICC BW voting failed for cpu: %d\n",
741 ret
= icc_disable(ctrl
->icc_path_cpu_to_qspi
);
743 dev_err(ctrl
->dev
, "%s: ICC disable failed for cpu: %d\n",
748 ret
= platform_get_irq(pdev
, 0);
751 ret
= devm_request_irq(dev
, ret
, qcom_qspi_irq
, 0, dev_name(dev
), ctrl
);
753 dev_err(dev
, "Failed to request irq %d\n", ret
);
757 ret
= dma_set_mask_and_coherent(dev
, DMA_BIT_MASK(32));
759 return dev_err_probe(dev
, ret
, "could not set DMA mask\n");
761 host
->max_speed_hz
= 300000000;
762 host
->max_dma_len
= 65536; /* as per HPG */
763 host
->dma_alignment
= QSPI_ALIGN_REQ
;
764 host
->num_chipselect
= QSPI_NUM_CS
;
766 host
->dev
.of_node
= pdev
->dev
.of_node
;
767 host
->mode_bits
= SPI_MODE_0
|
768 SPI_TX_DUAL
| SPI_RX_DUAL
|
769 SPI_TX_QUAD
| SPI_RX_QUAD
;
770 host
->flags
= SPI_CONTROLLER_HALF_DUPLEX
;
771 host
->prepare_message
= qcom_qspi_prepare_message
;
772 host
->transfer_one
= qcom_qspi_transfer_one
;
773 host
->handle_err
= qcom_qspi_handle_err
;
774 if (of_property_present(pdev
->dev
.of_node
, "iommus"))
775 host
->can_dma
= qcom_qspi_can_dma
;
776 host
->auto_runtime_pm
= true;
777 host
->mem_ops
= &qcom_qspi_mem_ops
;
779 ret
= devm_pm_opp_set_clkname(&pdev
->dev
, "core");
782 /* OPP table is optional */
783 ret
= devm_pm_opp_of_add_table(&pdev
->dev
);
784 if (ret
&& ret
!= -ENODEV
) {
785 dev_err(&pdev
->dev
, "invalid OPP table in device tree\n");
789 ret
= qcom_qspi_alloc_dma(ctrl
);
793 pm_runtime_use_autosuspend(dev
);
794 pm_runtime_set_autosuspend_delay(dev
, 250);
795 pm_runtime_enable(dev
);
797 ret
= spi_register_controller(host
);
801 pm_runtime_disable(dev
);
806 static void qcom_qspi_remove(struct platform_device
*pdev
)
808 struct spi_controller
*host
= platform_get_drvdata(pdev
);
810 /* Unregister _before_ disabling pm_runtime() so we stop transfers */
811 spi_unregister_controller(host
);
813 pm_runtime_disable(&pdev
->dev
);
816 static int __maybe_unused
qcom_qspi_runtime_suspend(struct device
*dev
)
818 struct spi_controller
*host
= dev_get_drvdata(dev
);
819 struct qcom_qspi
*ctrl
= spi_controller_get_devdata(host
);
822 /* Drop the performance state vote */
823 dev_pm_opp_set_rate(dev
, 0);
824 clk_bulk_disable_unprepare(QSPI_NUM_CLKS
, ctrl
->clks
);
826 ret
= icc_disable(ctrl
->icc_path_cpu_to_qspi
);
828 dev_err_ratelimited(ctrl
->dev
, "%s: ICC disable failed for cpu: %d\n",
833 pinctrl_pm_select_sleep_state(dev
);
838 static int __maybe_unused
qcom_qspi_runtime_resume(struct device
*dev
)
840 struct spi_controller
*host
= dev_get_drvdata(dev
);
841 struct qcom_qspi
*ctrl
= spi_controller_get_devdata(host
);
844 pinctrl_pm_select_default_state(dev
);
846 ret
= icc_enable(ctrl
->icc_path_cpu_to_qspi
);
848 dev_err_ratelimited(ctrl
->dev
, "%s: ICC enable failed for cpu: %d\n",
853 ret
= clk_bulk_prepare_enable(QSPI_NUM_CLKS
, ctrl
->clks
);
857 return dev_pm_opp_set_rate(dev
, ctrl
->last_speed
* 4);
860 static int __maybe_unused
qcom_qspi_suspend(struct device
*dev
)
862 struct spi_controller
*host
= dev_get_drvdata(dev
);
865 ret
= spi_controller_suspend(host
);
869 ret
= pm_runtime_force_suspend(dev
);
871 spi_controller_resume(host
);
876 static int __maybe_unused
qcom_qspi_resume(struct device
*dev
)
878 struct spi_controller
*host
= dev_get_drvdata(dev
);
881 ret
= pm_runtime_force_resume(dev
);
885 ret
= spi_controller_resume(host
);
887 pm_runtime_force_suspend(dev
);
892 static const struct dev_pm_ops qcom_qspi_dev_pm_ops
= {
893 SET_RUNTIME_PM_OPS(qcom_qspi_runtime_suspend
,
894 qcom_qspi_runtime_resume
, NULL
)
895 SET_SYSTEM_SLEEP_PM_OPS(qcom_qspi_suspend
, qcom_qspi_resume
)
898 static const struct of_device_id qcom_qspi_dt_match
[] = {
899 { .compatible
= "qcom,qspi-v1", },
902 MODULE_DEVICE_TABLE(of
, qcom_qspi_dt_match
);
904 static struct platform_driver qcom_qspi_driver
= {
907 .pm
= &qcom_qspi_dev_pm_ops
,
908 .of_match_table
= qcom_qspi_dt_match
,
910 .probe
= qcom_qspi_probe
,
911 .remove
= qcom_qspi_remove
,
913 module_platform_driver(qcom_qspi_driver
);
915 MODULE_DESCRIPTION("SPI driver for QSPI cores");
916 MODULE_LICENSE("GPL v2");