1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for Cadence QSPI Controller
5 * Copyright Altera Corporation (C) 2012-2014. All rights reserved.
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/dma-mapping.h>
11 #include <linux/dmaengine.h>
12 #include <linux/err.h>
13 #include <linux/errno.h>
14 #include <linux/interrupt.h>
16 #include <linux/iopoll.h>
17 #include <linux/jiffies.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/spi-nor.h>
23 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/pm_runtime.h>
27 #include <linux/reset.h>
28 #include <linux/sched.h>
29 #include <linux/spi/spi.h>
30 #include <linux/timer.h>
32 #define CQSPI_NAME "cadence-qspi"
33 #define CQSPI_MAX_CHIPSELECT 16
36 #define CQSPI_NEEDS_WR_DELAY BIT(0)
38 /* Capabilities mask */
39 #define CQSPI_BASE_HWCAPS_MASK \
40 (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST | \
41 SNOR_HWCAPS_READ_1_1_2 | SNOR_HWCAPS_READ_1_1_4 | \
46 struct cqspi_flash_pdata
{
48 struct cqspi_st
*cqspi
;
64 struct platform_device
*pdev
;
70 void __iomem
*ahb_base
;
71 resource_size_t ahb_size
;
72 struct completion transfer_complete
;
73 struct mutex bus_mutex
;
75 struct dma_chan
*rx_chan
;
76 struct completion rx_dma_complete
;
77 dma_addr_t mmap_phys_base
;
80 int current_page_size
;
81 int current_erase_size
;
82 int current_addr_width
;
83 unsigned long master_ref_clk_hz
;
90 struct cqspi_flash_pdata f_pdata
[CQSPI_MAX_CHIPSELECT
];
93 struct cqspi_driver_platdata
{
98 /* Operation timeout value */
99 #define CQSPI_TIMEOUT_MS 500
100 #define CQSPI_READ_TIMEOUT_MS 10
102 /* Instruction type */
103 #define CQSPI_INST_TYPE_SINGLE 0
104 #define CQSPI_INST_TYPE_DUAL 1
105 #define CQSPI_INST_TYPE_QUAD 2
106 #define CQSPI_INST_TYPE_OCTAL 3
108 #define CQSPI_DUMMY_CLKS_PER_BYTE 8
109 #define CQSPI_DUMMY_BYTES_MAX 4
110 #define CQSPI_DUMMY_CLKS_MAX 31
112 #define CQSPI_STIG_DATA_LEN_MAX 8
115 #define CQSPI_REG_CONFIG 0x00
116 #define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
117 #define CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL BIT(7)
118 #define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
119 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
120 #define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
121 #define CQSPI_REG_CONFIG_BAUD_LSB 19
122 #define CQSPI_REG_CONFIG_IDLE_LSB 31
123 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
124 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
126 #define CQSPI_REG_RD_INSTR 0x04
127 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
128 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
129 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
130 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
131 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
132 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
133 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
134 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
135 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
136 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
138 #define CQSPI_REG_WR_INSTR 0x08
139 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
140 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
141 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
143 #define CQSPI_REG_DELAY 0x0C
144 #define CQSPI_REG_DELAY_TSLCH_LSB 0
145 #define CQSPI_REG_DELAY_TCHSH_LSB 8
146 #define CQSPI_REG_DELAY_TSD2D_LSB 16
147 #define CQSPI_REG_DELAY_TSHSL_LSB 24
148 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
149 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
150 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
151 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
153 #define CQSPI_REG_READCAPTURE 0x10
154 #define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
155 #define CQSPI_REG_READCAPTURE_DELAY_LSB 1
156 #define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
158 #define CQSPI_REG_SIZE 0x14
159 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
160 #define CQSPI_REG_SIZE_PAGE_LSB 4
161 #define CQSPI_REG_SIZE_BLOCK_LSB 16
162 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
163 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
164 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
166 #define CQSPI_REG_SRAMPARTITION 0x18
167 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
169 #define CQSPI_REG_DMA 0x20
170 #define CQSPI_REG_DMA_SINGLE_LSB 0
171 #define CQSPI_REG_DMA_BURST_LSB 8
172 #define CQSPI_REG_DMA_SINGLE_MASK 0xFF
173 #define CQSPI_REG_DMA_BURST_MASK 0xFF
175 #define CQSPI_REG_REMAP 0x24
176 #define CQSPI_REG_MODE_BIT 0x28
178 #define CQSPI_REG_SDRAMLEVEL 0x2C
179 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
180 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
181 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
182 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
184 #define CQSPI_REG_IRQSTATUS 0x40
185 #define CQSPI_REG_IRQMASK 0x44
187 #define CQSPI_REG_INDIRECTRD 0x60
188 #define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
189 #define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
190 #define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
192 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
193 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
194 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
196 #define CQSPI_REG_CMDCTRL 0x90
197 #define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
198 #define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
199 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
200 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
201 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
202 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
203 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
204 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
205 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
206 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
207 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
208 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
210 #define CQSPI_REG_INDIRECTWR 0x70
211 #define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
212 #define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
213 #define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
215 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
216 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
217 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
219 #define CQSPI_REG_CMDADDRESS 0x94
220 #define CQSPI_REG_CMDREADDATALOWER 0xA0
221 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
222 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
223 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
225 /* Interrupt status bits */
226 #define CQSPI_REG_IRQ_MODE_ERR BIT(0)
227 #define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
228 #define CQSPI_REG_IRQ_IND_COMP BIT(2)
229 #define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
230 #define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
231 #define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
232 #define CQSPI_REG_IRQ_WATERMARK BIT(6)
233 #define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
235 #define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
236 CQSPI_REG_IRQ_IND_SRAM_FULL | \
237 CQSPI_REG_IRQ_IND_COMP)
239 #define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
240 CQSPI_REG_IRQ_WATERMARK | \
241 CQSPI_REG_IRQ_UNDERFLOW)
243 #define CQSPI_IRQ_STATUS_MASK 0x1FFFF
245 static int cqspi_wait_for_bit(void __iomem
*reg
, const u32 mask
, bool clr
)
249 return readl_relaxed_poll_timeout(reg
, val
,
250 (((clr
? ~val
: val
) & mask
) == mask
),
251 10, CQSPI_TIMEOUT_MS
* 1000);
254 static bool cqspi_is_idle(struct cqspi_st
*cqspi
)
256 u32 reg
= readl(cqspi
->iobase
+ CQSPI_REG_CONFIG
);
258 return reg
& (1 << CQSPI_REG_CONFIG_IDLE_LSB
);
261 static u32
cqspi_get_rd_sram_level(struct cqspi_st
*cqspi
)
263 u32 reg
= readl(cqspi
->iobase
+ CQSPI_REG_SDRAMLEVEL
);
265 reg
>>= CQSPI_REG_SDRAMLEVEL_RD_LSB
;
266 return reg
& CQSPI_REG_SDRAMLEVEL_RD_MASK
;
269 static irqreturn_t
cqspi_irq_handler(int this_irq
, void *dev
)
271 struct cqspi_st
*cqspi
= dev
;
272 unsigned int irq_status
;
274 /* Read interrupt status */
275 irq_status
= readl(cqspi
->iobase
+ CQSPI_REG_IRQSTATUS
);
277 /* Clear interrupt */
278 writel(irq_status
, cqspi
->iobase
+ CQSPI_REG_IRQSTATUS
);
280 irq_status
&= CQSPI_IRQ_MASK_RD
| CQSPI_IRQ_MASK_WR
;
283 complete(&cqspi
->transfer_complete
);
288 static unsigned int cqspi_calc_rdreg(struct spi_nor
*nor
)
290 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
293 rdreg
|= f_pdata
->inst_width
<< CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB
;
294 rdreg
|= f_pdata
->addr_width
<< CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB
;
295 rdreg
|= f_pdata
->data_width
<< CQSPI_REG_RD_INSTR_TYPE_DATA_LSB
;
300 static int cqspi_wait_idle(struct cqspi_st
*cqspi
)
302 const unsigned int poll_idle_retry
= 3;
303 unsigned int count
= 0;
304 unsigned long timeout
;
306 timeout
= jiffies
+ msecs_to_jiffies(CQSPI_TIMEOUT_MS
);
309 * Read few times in succession to ensure the controller
310 * is indeed idle, that is, the bit does not transition
313 if (cqspi_is_idle(cqspi
))
318 if (count
>= poll_idle_retry
)
321 if (time_after(jiffies
, timeout
)) {
322 /* Timeout, in busy mode. */
323 dev_err(&cqspi
->pdev
->dev
,
324 "QSPI is still busy after %dms timeout.\n",
333 static int cqspi_exec_flash_cmd(struct cqspi_st
*cqspi
, unsigned int reg
)
335 void __iomem
*reg_base
= cqspi
->iobase
;
338 /* Write the CMDCTRL without start execution. */
339 writel(reg
, reg_base
+ CQSPI_REG_CMDCTRL
);
341 reg
|= CQSPI_REG_CMDCTRL_EXECUTE_MASK
;
342 writel(reg
, reg_base
+ CQSPI_REG_CMDCTRL
);
344 /* Polling for completion. */
345 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_CMDCTRL
,
346 CQSPI_REG_CMDCTRL_INPROGRESS_MASK
, 1);
348 dev_err(&cqspi
->pdev
->dev
,
349 "Flash command execution timed out.\n");
353 /* Polling QSPI idle status. */
354 return cqspi_wait_idle(cqspi
);
357 static int cqspi_command_read(struct spi_nor
*nor
, u8 opcode
,
358 u8
*rxbuf
, size_t n_rx
)
360 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
361 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
362 void __iomem
*reg_base
= cqspi
->iobase
;
368 if (!n_rx
|| n_rx
> CQSPI_STIG_DATA_LEN_MAX
|| !rxbuf
) {
370 "Invalid input argument, len %zu rxbuf 0x%p\n",
375 reg
= opcode
<< CQSPI_REG_CMDCTRL_OPCODE_LSB
;
377 rdreg
= cqspi_calc_rdreg(nor
);
378 writel(rdreg
, reg_base
+ CQSPI_REG_RD_INSTR
);
380 reg
|= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB
);
382 /* 0 means 1 byte. */
383 reg
|= (((n_rx
- 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK
)
384 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB
);
385 status
= cqspi_exec_flash_cmd(cqspi
, reg
);
389 reg
= readl(reg_base
+ CQSPI_REG_CMDREADDATALOWER
);
391 /* Put the read value into rx_buf */
392 read_len
= (n_rx
> 4) ? 4 : n_rx
;
393 memcpy(rxbuf
, ®
, read_len
);
397 reg
= readl(reg_base
+ CQSPI_REG_CMDREADDATAUPPER
);
399 read_len
= n_rx
- read_len
;
400 memcpy(rxbuf
, ®
, read_len
);
406 static int cqspi_command_write(struct spi_nor
*nor
, const u8 opcode
,
407 const u8
*txbuf
, size_t n_tx
)
409 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
410 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
411 void __iomem
*reg_base
= cqspi
->iobase
;
417 if (n_tx
> CQSPI_STIG_DATA_LEN_MAX
|| (n_tx
&& !txbuf
)) {
419 "Invalid input argument, cmdlen %zu txbuf 0x%p\n",
424 reg
= opcode
<< CQSPI_REG_CMDCTRL_OPCODE_LSB
;
426 reg
|= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB
);
427 reg
|= ((n_tx
- 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK
)
428 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB
;
430 write_len
= (n_tx
> 4) ? 4 : n_tx
;
431 memcpy(&data
, txbuf
, write_len
);
433 writel(data
, reg_base
+ CQSPI_REG_CMDWRITEDATALOWER
);
437 write_len
= n_tx
- 4;
438 memcpy(&data
, txbuf
, write_len
);
439 writel(data
, reg_base
+ CQSPI_REG_CMDWRITEDATAUPPER
);
442 ret
= cqspi_exec_flash_cmd(cqspi
, reg
);
446 static int cqspi_command_write_addr(struct spi_nor
*nor
,
447 const u8 opcode
, const unsigned int addr
)
449 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
450 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
451 void __iomem
*reg_base
= cqspi
->iobase
;
454 reg
= opcode
<< CQSPI_REG_CMDCTRL_OPCODE_LSB
;
455 reg
|= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB
);
456 reg
|= ((nor
->addr_width
- 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK
)
457 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB
;
459 writel(addr
, reg_base
+ CQSPI_REG_CMDADDRESS
);
461 return cqspi_exec_flash_cmd(cqspi
, reg
);
464 static int cqspi_read_setup(struct spi_nor
*nor
)
466 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
467 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
468 void __iomem
*reg_base
= cqspi
->iobase
;
469 unsigned int dummy_clk
= 0;
472 reg
= nor
->read_opcode
<< CQSPI_REG_RD_INSTR_OPCODE_LSB
;
473 reg
|= cqspi_calc_rdreg(nor
);
475 /* Setup dummy clock cycles */
476 dummy_clk
= nor
->read_dummy
;
477 if (dummy_clk
> CQSPI_DUMMY_CLKS_MAX
)
478 dummy_clk
= CQSPI_DUMMY_CLKS_MAX
;
481 reg
|= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB
);
482 /* Set mode bits high to ensure chip doesn't enter XIP */
483 writel(0xFF, reg_base
+ CQSPI_REG_MODE_BIT
);
485 /* Need to subtract the mode byte (8 clocks). */
486 if (f_pdata
->inst_width
!= CQSPI_INST_TYPE_QUAD
)
490 reg
|= (dummy_clk
& CQSPI_REG_RD_INSTR_DUMMY_MASK
)
491 << CQSPI_REG_RD_INSTR_DUMMY_LSB
;
494 writel(reg
, reg_base
+ CQSPI_REG_RD_INSTR
);
496 /* Set address width */
497 reg
= readl(reg_base
+ CQSPI_REG_SIZE
);
498 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
499 reg
|= (nor
->addr_width
- 1);
500 writel(reg
, reg_base
+ CQSPI_REG_SIZE
);
504 static int cqspi_indirect_read_execute(struct spi_nor
*nor
, u8
*rxbuf
,
505 loff_t from_addr
, const size_t n_rx
)
507 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
508 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
509 void __iomem
*reg_base
= cqspi
->iobase
;
510 void __iomem
*ahb_base
= cqspi
->ahb_base
;
511 unsigned int remaining
= n_rx
;
512 unsigned int mod_bytes
= n_rx
% 4;
513 unsigned int bytes_to_read
= 0;
514 u8
*rxbuf_end
= rxbuf
+ n_rx
;
517 writel(from_addr
, reg_base
+ CQSPI_REG_INDIRECTRDSTARTADDR
);
518 writel(remaining
, reg_base
+ CQSPI_REG_INDIRECTRDBYTES
);
520 /* Clear all interrupts. */
521 writel(CQSPI_IRQ_STATUS_MASK
, reg_base
+ CQSPI_REG_IRQSTATUS
);
523 writel(CQSPI_IRQ_MASK_RD
, reg_base
+ CQSPI_REG_IRQMASK
);
525 reinit_completion(&cqspi
->transfer_complete
);
526 writel(CQSPI_REG_INDIRECTRD_START_MASK
,
527 reg_base
+ CQSPI_REG_INDIRECTRD
);
529 while (remaining
> 0) {
530 if (!wait_for_completion_timeout(&cqspi
->transfer_complete
,
531 msecs_to_jiffies(CQSPI_READ_TIMEOUT_MS
)))
534 bytes_to_read
= cqspi_get_rd_sram_level(cqspi
);
536 if (ret
&& bytes_to_read
== 0) {
537 dev_err(nor
->dev
, "Indirect read timeout, no bytes\n");
541 while (bytes_to_read
!= 0) {
542 unsigned int word_remain
= round_down(remaining
, 4);
544 bytes_to_read
*= cqspi
->fifo_width
;
545 bytes_to_read
= bytes_to_read
> remaining
?
546 remaining
: bytes_to_read
;
547 bytes_to_read
= round_down(bytes_to_read
, 4);
548 /* Read 4 byte word chunks then single bytes */
550 ioread32_rep(ahb_base
, rxbuf
,
551 (bytes_to_read
/ 4));
552 } else if (!word_remain
&& mod_bytes
) {
553 unsigned int temp
= ioread32(ahb_base
);
555 bytes_to_read
= mod_bytes
;
556 memcpy(rxbuf
, &temp
, min((unsigned int)
560 rxbuf
+= bytes_to_read
;
561 remaining
-= bytes_to_read
;
562 bytes_to_read
= cqspi_get_rd_sram_level(cqspi
);
566 reinit_completion(&cqspi
->transfer_complete
);
569 /* Check indirect done status */
570 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_INDIRECTRD
,
571 CQSPI_REG_INDIRECTRD_DONE_MASK
, 0);
574 "Indirect read completion error (%i)\n", ret
);
578 /* Disable interrupt */
579 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
581 /* Clear indirect completion status */
582 writel(CQSPI_REG_INDIRECTRD_DONE_MASK
, reg_base
+ CQSPI_REG_INDIRECTRD
);
587 /* Disable interrupt */
588 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
590 /* Cancel the indirect read */
591 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK
,
592 reg_base
+ CQSPI_REG_INDIRECTRD
);
596 static int cqspi_write_setup(struct spi_nor
*nor
)
599 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
600 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
601 void __iomem
*reg_base
= cqspi
->iobase
;
604 reg
= nor
->program_opcode
<< CQSPI_REG_WR_INSTR_OPCODE_LSB
;
605 writel(reg
, reg_base
+ CQSPI_REG_WR_INSTR
);
606 reg
= cqspi_calc_rdreg(nor
);
607 writel(reg
, reg_base
+ CQSPI_REG_RD_INSTR
);
609 reg
= readl(reg_base
+ CQSPI_REG_SIZE
);
610 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
611 reg
|= (nor
->addr_width
- 1);
612 writel(reg
, reg_base
+ CQSPI_REG_SIZE
);
616 static int cqspi_indirect_write_execute(struct spi_nor
*nor
, loff_t to_addr
,
617 const u8
*txbuf
, const size_t n_tx
)
619 const unsigned int page_size
= nor
->page_size
;
620 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
621 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
622 void __iomem
*reg_base
= cqspi
->iobase
;
623 unsigned int remaining
= n_tx
;
624 unsigned int write_bytes
;
627 writel(to_addr
, reg_base
+ CQSPI_REG_INDIRECTWRSTARTADDR
);
628 writel(remaining
, reg_base
+ CQSPI_REG_INDIRECTWRBYTES
);
630 /* Clear all interrupts. */
631 writel(CQSPI_IRQ_STATUS_MASK
, reg_base
+ CQSPI_REG_IRQSTATUS
);
633 writel(CQSPI_IRQ_MASK_WR
, reg_base
+ CQSPI_REG_IRQMASK
);
635 reinit_completion(&cqspi
->transfer_complete
);
636 writel(CQSPI_REG_INDIRECTWR_START_MASK
,
637 reg_base
+ CQSPI_REG_INDIRECTWR
);
639 * As per 66AK2G02 TRM SPRUHY8F section 11.15.5.3 Indirect Access
640 * Controller programming sequence, couple of cycles of
641 * QSPI_REF_CLK delay is required for the above bit to
642 * be internally synchronized by the QSPI module. Provide 5
646 ndelay(cqspi
->wr_delay
);
648 while (remaining
> 0) {
649 size_t write_words
, mod_bytes
;
651 write_bytes
= remaining
> page_size
? page_size
: remaining
;
652 write_words
= write_bytes
/ 4;
653 mod_bytes
= write_bytes
% 4;
654 /* Write 4 bytes at a time then single bytes. */
656 iowrite32_rep(cqspi
->ahb_base
, txbuf
, write_words
);
657 txbuf
+= (write_words
* 4);
660 unsigned int temp
= 0xFFFFFFFF;
662 memcpy(&temp
, txbuf
, mod_bytes
);
663 iowrite32(temp
, cqspi
->ahb_base
);
667 if (!wait_for_completion_timeout(&cqspi
->transfer_complete
,
668 msecs_to_jiffies(CQSPI_TIMEOUT_MS
))) {
669 dev_err(nor
->dev
, "Indirect write timeout\n");
674 remaining
-= write_bytes
;
677 reinit_completion(&cqspi
->transfer_complete
);
680 /* Check indirect done status */
681 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_INDIRECTWR
,
682 CQSPI_REG_INDIRECTWR_DONE_MASK
, 0);
685 "Indirect write completion error (%i)\n", ret
);
689 /* Disable interrupt. */
690 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
692 /* Clear indirect completion status */
693 writel(CQSPI_REG_INDIRECTWR_DONE_MASK
, reg_base
+ CQSPI_REG_INDIRECTWR
);
695 cqspi_wait_idle(cqspi
);
700 /* Disable interrupt. */
701 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
703 /* Cancel the indirect write */
704 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK
,
705 reg_base
+ CQSPI_REG_INDIRECTWR
);
709 static void cqspi_chipselect(struct spi_nor
*nor
)
711 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
712 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
713 void __iomem
*reg_base
= cqspi
->iobase
;
714 unsigned int chip_select
= f_pdata
->cs
;
717 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
718 if (cqspi
->is_decoded_cs
) {
719 reg
|= CQSPI_REG_CONFIG_DECODE_MASK
;
721 reg
&= ~CQSPI_REG_CONFIG_DECODE_MASK
;
723 /* Convert CS if without decoder.
729 chip_select
= 0xF & ~(1 << chip_select
);
732 reg
&= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
733 << CQSPI_REG_CONFIG_CHIPSELECT_LSB
);
734 reg
|= (chip_select
& CQSPI_REG_CONFIG_CHIPSELECT_MASK
)
735 << CQSPI_REG_CONFIG_CHIPSELECT_LSB
;
736 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
739 static void cqspi_configure_cs_and_sizes(struct spi_nor
*nor
)
741 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
742 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
743 void __iomem
*iobase
= cqspi
->iobase
;
746 /* configure page size and block size. */
747 reg
= readl(iobase
+ CQSPI_REG_SIZE
);
748 reg
&= ~(CQSPI_REG_SIZE_PAGE_MASK
<< CQSPI_REG_SIZE_PAGE_LSB
);
749 reg
&= ~(CQSPI_REG_SIZE_BLOCK_MASK
<< CQSPI_REG_SIZE_BLOCK_LSB
);
750 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
751 reg
|= (nor
->page_size
<< CQSPI_REG_SIZE_PAGE_LSB
);
752 reg
|= (ilog2(nor
->mtd
.erasesize
) << CQSPI_REG_SIZE_BLOCK_LSB
);
753 reg
|= (nor
->addr_width
- 1);
754 writel(reg
, iobase
+ CQSPI_REG_SIZE
);
756 /* configure the chip select */
757 cqspi_chipselect(nor
);
759 /* Store the new configuration of the controller */
760 cqspi
->current_page_size
= nor
->page_size
;
761 cqspi
->current_erase_size
= nor
->mtd
.erasesize
;
762 cqspi
->current_addr_width
= nor
->addr_width
;
765 static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz
,
766 const unsigned int ns_val
)
770 ticks
= ref_clk_hz
/ 1000; /* kHz */
771 ticks
= DIV_ROUND_UP(ticks
* ns_val
, 1000000);
776 static void cqspi_delay(struct spi_nor
*nor
)
778 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
779 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
780 void __iomem
*iobase
= cqspi
->iobase
;
781 const unsigned int ref_clk_hz
= cqspi
->master_ref_clk_hz
;
782 unsigned int tshsl
, tchsh
, tslch
, tsd2d
;
786 /* calculate the number of ref ticks for one sclk tick */
787 tsclk
= DIV_ROUND_UP(ref_clk_hz
, cqspi
->sclk
);
789 tshsl
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tshsl_ns
);
790 /* this particular value must be at least one sclk */
794 tchsh
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tchsh_ns
);
795 tslch
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tslch_ns
);
796 tsd2d
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tsd2d_ns
);
798 reg
= (tshsl
& CQSPI_REG_DELAY_TSHSL_MASK
)
799 << CQSPI_REG_DELAY_TSHSL_LSB
;
800 reg
|= (tchsh
& CQSPI_REG_DELAY_TCHSH_MASK
)
801 << CQSPI_REG_DELAY_TCHSH_LSB
;
802 reg
|= (tslch
& CQSPI_REG_DELAY_TSLCH_MASK
)
803 << CQSPI_REG_DELAY_TSLCH_LSB
;
804 reg
|= (tsd2d
& CQSPI_REG_DELAY_TSD2D_MASK
)
805 << CQSPI_REG_DELAY_TSD2D_LSB
;
806 writel(reg
, iobase
+ CQSPI_REG_DELAY
);
809 static void cqspi_config_baudrate_div(struct cqspi_st
*cqspi
)
811 const unsigned int ref_clk_hz
= cqspi
->master_ref_clk_hz
;
812 void __iomem
*reg_base
= cqspi
->iobase
;
815 /* Recalculate the baudrate divisor based on QSPI specification. */
816 div
= DIV_ROUND_UP(ref_clk_hz
, 2 * cqspi
->sclk
) - 1;
818 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
819 reg
&= ~(CQSPI_REG_CONFIG_BAUD_MASK
<< CQSPI_REG_CONFIG_BAUD_LSB
);
820 reg
|= (div
& CQSPI_REG_CONFIG_BAUD_MASK
) << CQSPI_REG_CONFIG_BAUD_LSB
;
821 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
824 static void cqspi_readdata_capture(struct cqspi_st
*cqspi
,
826 const unsigned int delay
)
828 void __iomem
*reg_base
= cqspi
->iobase
;
831 reg
= readl(reg_base
+ CQSPI_REG_READCAPTURE
);
834 reg
|= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB
);
836 reg
&= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB
);
838 reg
&= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
839 << CQSPI_REG_READCAPTURE_DELAY_LSB
);
841 reg
|= (delay
& CQSPI_REG_READCAPTURE_DELAY_MASK
)
842 << CQSPI_REG_READCAPTURE_DELAY_LSB
;
844 writel(reg
, reg_base
+ CQSPI_REG_READCAPTURE
);
847 static void cqspi_controller_enable(struct cqspi_st
*cqspi
, bool enable
)
849 void __iomem
*reg_base
= cqspi
->iobase
;
852 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
855 reg
|= CQSPI_REG_CONFIG_ENABLE_MASK
;
857 reg
&= ~CQSPI_REG_CONFIG_ENABLE_MASK
;
859 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
862 static void cqspi_configure(struct spi_nor
*nor
)
864 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
865 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
866 const unsigned int sclk
= f_pdata
->clk_rate
;
867 int switch_cs
= (cqspi
->current_cs
!= f_pdata
->cs
);
868 int switch_ck
= (cqspi
->sclk
!= sclk
);
870 if ((cqspi
->current_page_size
!= nor
->page_size
) ||
871 (cqspi
->current_erase_size
!= nor
->mtd
.erasesize
) ||
872 (cqspi
->current_addr_width
!= nor
->addr_width
))
875 if (switch_cs
|| switch_ck
)
876 cqspi_controller_enable(cqspi
, 0);
878 /* Switch chip select. */
880 cqspi
->current_cs
= f_pdata
->cs
;
881 cqspi_configure_cs_and_sizes(nor
);
884 /* Setup baudrate divisor and delays */
887 cqspi_config_baudrate_div(cqspi
);
889 cqspi_readdata_capture(cqspi
, !cqspi
->rclk_en
,
890 f_pdata
->read_delay
);
893 if (switch_cs
|| switch_ck
)
894 cqspi_controller_enable(cqspi
, 1);
897 static int cqspi_set_protocol(struct spi_nor
*nor
, const int read
)
899 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
901 f_pdata
->inst_width
= CQSPI_INST_TYPE_SINGLE
;
902 f_pdata
->addr_width
= CQSPI_INST_TYPE_SINGLE
;
903 f_pdata
->data_width
= CQSPI_INST_TYPE_SINGLE
;
906 switch (nor
->read_proto
) {
907 case SNOR_PROTO_1_1_1
:
908 f_pdata
->data_width
= CQSPI_INST_TYPE_SINGLE
;
910 case SNOR_PROTO_1_1_2
:
911 f_pdata
->data_width
= CQSPI_INST_TYPE_DUAL
;
913 case SNOR_PROTO_1_1_4
:
914 f_pdata
->data_width
= CQSPI_INST_TYPE_QUAD
;
916 case SNOR_PROTO_1_1_8
:
917 f_pdata
->data_width
= CQSPI_INST_TYPE_OCTAL
;
924 cqspi_configure(nor
);
929 static ssize_t
cqspi_write(struct spi_nor
*nor
, loff_t to
,
930 size_t len
, const u_char
*buf
)
932 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
933 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
936 ret
= cqspi_set_protocol(nor
, 0);
940 ret
= cqspi_write_setup(nor
);
944 if (f_pdata
->use_direct_mode
) {
945 memcpy_toio(cqspi
->ahb_base
+ to
, buf
, len
);
946 ret
= cqspi_wait_idle(cqspi
);
948 ret
= cqspi_indirect_write_execute(nor
, to
, buf
, len
);
956 static void cqspi_rx_dma_callback(void *param
)
958 struct cqspi_st
*cqspi
= param
;
960 complete(&cqspi
->rx_dma_complete
);
963 static int cqspi_direct_read_execute(struct spi_nor
*nor
, u_char
*buf
,
964 loff_t from
, size_t len
)
966 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
967 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
968 enum dma_ctrl_flags flags
= DMA_CTRL_ACK
| DMA_PREP_INTERRUPT
;
969 dma_addr_t dma_src
= (dma_addr_t
)cqspi
->mmap_phys_base
+ from
;
971 struct dma_async_tx_descriptor
*tx
;
975 if (!cqspi
->rx_chan
|| !virt_addr_valid(buf
)) {
976 memcpy_fromio(buf
, cqspi
->ahb_base
+ from
, len
);
980 dma_dst
= dma_map_single(nor
->dev
, buf
, len
, DMA_FROM_DEVICE
);
981 if (dma_mapping_error(nor
->dev
, dma_dst
)) {
982 dev_err(nor
->dev
, "dma mapping failed\n");
985 tx
= dmaengine_prep_dma_memcpy(cqspi
->rx_chan
, dma_dst
, dma_src
,
988 dev_err(nor
->dev
, "device_prep_dma_memcpy error\n");
993 tx
->callback
= cqspi_rx_dma_callback
;
994 tx
->callback_param
= cqspi
;
995 cookie
= tx
->tx_submit(tx
);
996 reinit_completion(&cqspi
->rx_dma_complete
);
998 ret
= dma_submit_error(cookie
);
1000 dev_err(nor
->dev
, "dma_submit_error %d\n", cookie
);
1005 dma_async_issue_pending(cqspi
->rx_chan
);
1006 if (!wait_for_completion_timeout(&cqspi
->rx_dma_complete
,
1007 msecs_to_jiffies(len
))) {
1008 dmaengine_terminate_sync(cqspi
->rx_chan
);
1009 dev_err(nor
->dev
, "DMA wait_for_completion_timeout\n");
1015 dma_unmap_single(nor
->dev
, dma_dst
, len
, DMA_FROM_DEVICE
);
1020 static ssize_t
cqspi_read(struct spi_nor
*nor
, loff_t from
,
1021 size_t len
, u_char
*buf
)
1023 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
1026 ret
= cqspi_set_protocol(nor
, 1);
1030 ret
= cqspi_read_setup(nor
);
1034 if (f_pdata
->use_direct_mode
)
1035 ret
= cqspi_direct_read_execute(nor
, buf
, from
, len
);
1037 ret
= cqspi_indirect_read_execute(nor
, buf
, from
, len
);
1044 static int cqspi_erase(struct spi_nor
*nor
, loff_t offs
)
1048 ret
= cqspi_set_protocol(nor
, 0);
1052 /* Send write enable, then erase commands. */
1053 ret
= nor
->controller_ops
->write_reg(nor
, SPINOR_OP_WREN
, NULL
, 0);
1057 /* Set up command buffer. */
1058 ret
= cqspi_command_write_addr(nor
, nor
->erase_opcode
, offs
);
1065 static int cqspi_prep(struct spi_nor
*nor
)
1067 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
1068 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
1070 mutex_lock(&cqspi
->bus_mutex
);
1075 static void cqspi_unprep(struct spi_nor
*nor
)
1077 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
1078 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
1080 mutex_unlock(&cqspi
->bus_mutex
);
1083 static int cqspi_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, size_t len
)
1087 ret
= cqspi_set_protocol(nor
, 0);
1089 ret
= cqspi_command_read(nor
, opcode
, buf
, len
);
1094 static int cqspi_write_reg(struct spi_nor
*nor
, u8 opcode
, const u8
*buf
,
1099 ret
= cqspi_set_protocol(nor
, 0);
1101 ret
= cqspi_command_write(nor
, opcode
, buf
, len
);
1106 static int cqspi_of_get_flash_pdata(struct platform_device
*pdev
,
1107 struct cqspi_flash_pdata
*f_pdata
,
1108 struct device_node
*np
)
1110 if (of_property_read_u32(np
, "cdns,read-delay", &f_pdata
->read_delay
)) {
1111 dev_err(&pdev
->dev
, "couldn't determine read-delay\n");
1115 if (of_property_read_u32(np
, "cdns,tshsl-ns", &f_pdata
->tshsl_ns
)) {
1116 dev_err(&pdev
->dev
, "couldn't determine tshsl-ns\n");
1120 if (of_property_read_u32(np
, "cdns,tsd2d-ns", &f_pdata
->tsd2d_ns
)) {
1121 dev_err(&pdev
->dev
, "couldn't determine tsd2d-ns\n");
1125 if (of_property_read_u32(np
, "cdns,tchsh-ns", &f_pdata
->tchsh_ns
)) {
1126 dev_err(&pdev
->dev
, "couldn't determine tchsh-ns\n");
1130 if (of_property_read_u32(np
, "cdns,tslch-ns", &f_pdata
->tslch_ns
)) {
1131 dev_err(&pdev
->dev
, "couldn't determine tslch-ns\n");
1135 if (of_property_read_u32(np
, "spi-max-frequency", &f_pdata
->clk_rate
)) {
1136 dev_err(&pdev
->dev
, "couldn't determine spi-max-frequency\n");
1143 static int cqspi_of_get_pdata(struct platform_device
*pdev
)
1145 struct device_node
*np
= pdev
->dev
.of_node
;
1146 struct cqspi_st
*cqspi
= platform_get_drvdata(pdev
);
1148 cqspi
->is_decoded_cs
= of_property_read_bool(np
, "cdns,is-decoded-cs");
1150 if (of_property_read_u32(np
, "cdns,fifo-depth", &cqspi
->fifo_depth
)) {
1151 dev_err(&pdev
->dev
, "couldn't determine fifo-depth\n");
1155 if (of_property_read_u32(np
, "cdns,fifo-width", &cqspi
->fifo_width
)) {
1156 dev_err(&pdev
->dev
, "couldn't determine fifo-width\n");
1160 if (of_property_read_u32(np
, "cdns,trigger-address",
1161 &cqspi
->trigger_address
)) {
1162 dev_err(&pdev
->dev
, "couldn't determine trigger-address\n");
1166 cqspi
->rclk_en
= of_property_read_bool(np
, "cdns,rclk-en");
1171 static void cqspi_controller_init(struct cqspi_st
*cqspi
)
1175 cqspi_controller_enable(cqspi
, 0);
1177 /* Configure the remap address register, no remap */
1178 writel(0, cqspi
->iobase
+ CQSPI_REG_REMAP
);
1180 /* Disable all interrupts. */
1181 writel(0, cqspi
->iobase
+ CQSPI_REG_IRQMASK
);
1183 /* Configure the SRAM split to 1:1 . */
1184 writel(cqspi
->fifo_depth
/ 2, cqspi
->iobase
+ CQSPI_REG_SRAMPARTITION
);
1186 /* Load indirect trigger address. */
1187 writel(cqspi
->trigger_address
,
1188 cqspi
->iobase
+ CQSPI_REG_INDIRECTTRIGGER
);
1190 /* Program read watermark -- 1/2 of the FIFO. */
1191 writel(cqspi
->fifo_depth
* cqspi
->fifo_width
/ 2,
1192 cqspi
->iobase
+ CQSPI_REG_INDIRECTRDWATERMARK
);
1193 /* Program write watermark -- 1/8 of the FIFO. */
1194 writel(cqspi
->fifo_depth
* cqspi
->fifo_width
/ 8,
1195 cqspi
->iobase
+ CQSPI_REG_INDIRECTWRWATERMARK
);
1197 /* Enable Direct Access Controller */
1198 reg
= readl(cqspi
->iobase
+ CQSPI_REG_CONFIG
);
1199 reg
|= CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL
;
1200 writel(reg
, cqspi
->iobase
+ CQSPI_REG_CONFIG
);
1202 cqspi_controller_enable(cqspi
, 1);
1205 static void cqspi_request_mmap_dma(struct cqspi_st
*cqspi
)
1207 dma_cap_mask_t mask
;
1210 dma_cap_set(DMA_MEMCPY
, mask
);
1212 cqspi
->rx_chan
= dma_request_chan_by_mask(&mask
);
1213 if (IS_ERR(cqspi
->rx_chan
)) {
1214 dev_err(&cqspi
->pdev
->dev
, "No Rx DMA available\n");
1215 cqspi
->rx_chan
= NULL
;
1217 init_completion(&cqspi
->rx_dma_complete
);
1220 static const struct spi_nor_controller_ops cqspi_controller_ops
= {
1221 .prepare
= cqspi_prep
,
1222 .unprepare
= cqspi_unprep
,
1223 .read_reg
= cqspi_read_reg
,
1224 .write_reg
= cqspi_write_reg
,
1226 .write
= cqspi_write
,
1227 .erase
= cqspi_erase
,
1230 static int cqspi_setup_flash(struct cqspi_st
*cqspi
, struct device_node
*np
)
1232 struct platform_device
*pdev
= cqspi
->pdev
;
1233 struct device
*dev
= &pdev
->dev
;
1234 const struct cqspi_driver_platdata
*ddata
;
1235 struct spi_nor_hwcaps hwcaps
;
1236 struct cqspi_flash_pdata
*f_pdata
;
1237 struct spi_nor
*nor
;
1238 struct mtd_info
*mtd
;
1242 ddata
= of_device_get_match_data(dev
);
1244 dev_err(dev
, "Couldn't find driver data\n");
1247 hwcaps
.mask
= ddata
->hwcaps_mask
;
1249 /* Get flash device data */
1250 for_each_available_child_of_node(dev
->of_node
, np
) {
1251 ret
= of_property_read_u32(np
, "reg", &cs
);
1253 dev_err(dev
, "Couldn't determine chip select.\n");
1257 if (cs
>= CQSPI_MAX_CHIPSELECT
) {
1259 dev_err(dev
, "Chip select %d out of range.\n", cs
);
1263 f_pdata
= &cqspi
->f_pdata
[cs
];
1264 f_pdata
->cqspi
= cqspi
;
1267 ret
= cqspi_of_get_flash_pdata(pdev
, f_pdata
, np
);
1271 nor
= &f_pdata
->nor
;
1277 spi_nor_set_flash_node(nor
, np
);
1278 nor
->priv
= f_pdata
;
1279 nor
->controller_ops
= &cqspi_controller_ops
;
1281 mtd
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s.%d",
1288 ret
= spi_nor_scan(nor
, NULL
, &hwcaps
);
1292 ret
= mtd_device_register(mtd
, NULL
, 0);
1296 f_pdata
->registered
= true;
1298 if (mtd
->size
<= cqspi
->ahb_size
) {
1299 f_pdata
->use_direct_mode
= true;
1300 dev_dbg(nor
->dev
, "using direct mode for %s\n",
1303 if (!cqspi
->rx_chan
)
1304 cqspi_request_mmap_dma(cqspi
);
1311 for (i
= 0; i
< CQSPI_MAX_CHIPSELECT
; i
++)
1312 if (cqspi
->f_pdata
[i
].registered
)
1313 mtd_device_unregister(&cqspi
->f_pdata
[i
].nor
.mtd
);
1317 static int cqspi_probe(struct platform_device
*pdev
)
1319 struct device_node
*np
= pdev
->dev
.of_node
;
1320 struct device
*dev
= &pdev
->dev
;
1321 struct cqspi_st
*cqspi
;
1322 struct resource
*res
;
1323 struct resource
*res_ahb
;
1324 struct reset_control
*rstc
, *rstc_ocp
;
1325 const struct cqspi_driver_platdata
*ddata
;
1329 cqspi
= devm_kzalloc(dev
, sizeof(*cqspi
), GFP_KERNEL
);
1333 mutex_init(&cqspi
->bus_mutex
);
1335 platform_set_drvdata(pdev
, cqspi
);
1337 /* Obtain configuration from OF. */
1338 ret
= cqspi_of_get_pdata(pdev
);
1340 dev_err(dev
, "Cannot get mandatory OF data.\n");
1344 /* Obtain QSPI clock. */
1345 cqspi
->clk
= devm_clk_get(dev
, NULL
);
1346 if (IS_ERR(cqspi
->clk
)) {
1347 dev_err(dev
, "Cannot claim QSPI clock.\n");
1348 return PTR_ERR(cqspi
->clk
);
1351 /* Obtain and remap controller address. */
1352 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1353 cqspi
->iobase
= devm_ioremap_resource(dev
, res
);
1354 if (IS_ERR(cqspi
->iobase
)) {
1355 dev_err(dev
, "Cannot remap controller address.\n");
1356 return PTR_ERR(cqspi
->iobase
);
1359 /* Obtain and remap AHB address. */
1360 res_ahb
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1361 cqspi
->ahb_base
= devm_ioremap_resource(dev
, res_ahb
);
1362 if (IS_ERR(cqspi
->ahb_base
)) {
1363 dev_err(dev
, "Cannot remap AHB address.\n");
1364 return PTR_ERR(cqspi
->ahb_base
);
1366 cqspi
->mmap_phys_base
= (dma_addr_t
)res_ahb
->start
;
1367 cqspi
->ahb_size
= resource_size(res_ahb
);
1369 init_completion(&cqspi
->transfer_complete
);
1371 /* Obtain IRQ line. */
1372 irq
= platform_get_irq(pdev
, 0);
1376 pm_runtime_enable(dev
);
1377 ret
= pm_runtime_get_sync(dev
);
1379 pm_runtime_put_noidle(dev
);
1383 ret
= clk_prepare_enable(cqspi
->clk
);
1385 dev_err(dev
, "Cannot enable QSPI clock.\n");
1386 goto probe_clk_failed
;
1389 /* Obtain QSPI reset control */
1390 rstc
= devm_reset_control_get_optional_exclusive(dev
, "qspi");
1392 dev_err(dev
, "Cannot get QSPI reset.\n");
1393 return PTR_ERR(rstc
);
1396 rstc_ocp
= devm_reset_control_get_optional_exclusive(dev
, "qspi-ocp");
1397 if (IS_ERR(rstc_ocp
)) {
1398 dev_err(dev
, "Cannot get QSPI OCP reset.\n");
1399 return PTR_ERR(rstc_ocp
);
1402 reset_control_assert(rstc
);
1403 reset_control_deassert(rstc
);
1405 reset_control_assert(rstc_ocp
);
1406 reset_control_deassert(rstc_ocp
);
1408 cqspi
->master_ref_clk_hz
= clk_get_rate(cqspi
->clk
);
1409 ddata
= of_device_get_match_data(dev
);
1410 if (ddata
&& (ddata
->quirks
& CQSPI_NEEDS_WR_DELAY
))
1411 cqspi
->wr_delay
= 5 * DIV_ROUND_UP(NSEC_PER_SEC
,
1412 cqspi
->master_ref_clk_hz
);
1414 ret
= devm_request_irq(dev
, irq
, cqspi_irq_handler
, 0,
1417 dev_err(dev
, "Cannot request IRQ.\n");
1418 goto probe_irq_failed
;
1421 cqspi_wait_idle(cqspi
);
1422 cqspi_controller_init(cqspi
);
1423 cqspi
->current_cs
= -1;
1426 ret
= cqspi_setup_flash(cqspi
, np
);
1428 dev_err(dev
, "Cadence QSPI NOR probe failed %d\n", ret
);
1429 goto probe_setup_failed
;
1434 cqspi_controller_enable(cqspi
, 0);
1436 clk_disable_unprepare(cqspi
->clk
);
1438 pm_runtime_put_sync(dev
);
1439 pm_runtime_disable(dev
);
1443 static int cqspi_remove(struct platform_device
*pdev
)
1445 struct cqspi_st
*cqspi
= platform_get_drvdata(pdev
);
1448 for (i
= 0; i
< CQSPI_MAX_CHIPSELECT
; i
++)
1449 if (cqspi
->f_pdata
[i
].registered
)
1450 mtd_device_unregister(&cqspi
->f_pdata
[i
].nor
.mtd
);
1452 cqspi_controller_enable(cqspi
, 0);
1455 dma_release_channel(cqspi
->rx_chan
);
1457 clk_disable_unprepare(cqspi
->clk
);
1459 pm_runtime_put_sync(&pdev
->dev
);
1460 pm_runtime_disable(&pdev
->dev
);
1465 #ifdef CONFIG_PM_SLEEP
1466 static int cqspi_suspend(struct device
*dev
)
1468 struct cqspi_st
*cqspi
= dev_get_drvdata(dev
);
1470 cqspi_controller_enable(cqspi
, 0);
1474 static int cqspi_resume(struct device
*dev
)
1476 struct cqspi_st
*cqspi
= dev_get_drvdata(dev
);
1478 cqspi_controller_enable(cqspi
, 1);
1482 static const struct dev_pm_ops cqspi__dev_pm_ops
= {
1483 .suspend
= cqspi_suspend
,
1484 .resume
= cqspi_resume
,
1487 #define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
1489 #define CQSPI_DEV_PM_OPS NULL
1492 static const struct cqspi_driver_platdata cdns_qspi
= {
1493 .hwcaps_mask
= CQSPI_BASE_HWCAPS_MASK
,
1496 static const struct cqspi_driver_platdata k2g_qspi
= {
1497 .hwcaps_mask
= CQSPI_BASE_HWCAPS_MASK
,
1498 .quirks
= CQSPI_NEEDS_WR_DELAY
,
1501 static const struct cqspi_driver_platdata am654_ospi
= {
1502 .hwcaps_mask
= CQSPI_BASE_HWCAPS_MASK
| SNOR_HWCAPS_READ_1_1_8
,
1503 .quirks
= CQSPI_NEEDS_WR_DELAY
,
1506 static const struct of_device_id cqspi_dt_ids
[] = {
1508 .compatible
= "cdns,qspi-nor",
1512 .compatible
= "ti,k2g-qspi",
1516 .compatible
= "ti,am654-ospi",
1517 .data
= &am654_ospi
,
1519 { /* end of table */ }
1522 MODULE_DEVICE_TABLE(of
, cqspi_dt_ids
);
1524 static struct platform_driver cqspi_platform_driver
= {
1525 .probe
= cqspi_probe
,
1526 .remove
= cqspi_remove
,
1529 .pm
= CQSPI_DEV_PM_OPS
,
1530 .of_match_table
= cqspi_dt_ids
,
1534 module_platform_driver(cqspi_platform_driver
);
1536 MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
1537 MODULE_LICENSE("GPL v2");
1538 MODULE_ALIAS("platform:" CQSPI_NAME
);
1539 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
1540 MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");