2 * Driver for Cadence QSPI Controller
4 * Copyright Altera Corporation (C) 2012-2014. All rights reserved.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/clk.h>
19 #include <linux/completion.h>
20 #include <linux/delay.h>
21 #include <linux/err.h>
22 #include <linux/errno.h>
23 #include <linux/interrupt.h>
25 #include <linux/jiffies.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/partitions.h>
30 #include <linux/mtd/spi-nor.h>
31 #include <linux/of_device.h>
33 #include <linux/platform_device.h>
34 #include <linux/sched.h>
35 #include <linux/spi/spi.h>
36 #include <linux/timer.h>
38 #define CQSPI_NAME "cadence-qspi"
39 #define CQSPI_MAX_CHIPSELECT 16
43 struct cqspi_flash_pdata
{
45 struct cqspi_st
*cqspi
;
60 struct platform_device
*pdev
;
66 void __iomem
*ahb_base
;
67 struct completion transfer_complete
;
68 struct mutex bus_mutex
;
71 int current_page_size
;
72 int current_erase_size
;
73 int current_addr_width
;
74 unsigned long master_ref_clk_hz
;
79 struct cqspi_flash_pdata f_pdata
[CQSPI_MAX_CHIPSELECT
];
82 /* Operation timeout value */
83 #define CQSPI_TIMEOUT_MS 500
84 #define CQSPI_READ_TIMEOUT_MS 10
86 /* Instruction type */
87 #define CQSPI_INST_TYPE_SINGLE 0
88 #define CQSPI_INST_TYPE_DUAL 1
89 #define CQSPI_INST_TYPE_QUAD 2
91 #define CQSPI_DUMMY_CLKS_PER_BYTE 8
92 #define CQSPI_DUMMY_BYTES_MAX 4
93 #define CQSPI_DUMMY_CLKS_MAX 31
95 #define CQSPI_STIG_DATA_LEN_MAX 8
98 #define CQSPI_REG_CONFIG 0x00
99 #define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
100 #define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
101 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
102 #define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
103 #define CQSPI_REG_CONFIG_BAUD_LSB 19
104 #define CQSPI_REG_CONFIG_IDLE_LSB 31
105 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
106 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
108 #define CQSPI_REG_RD_INSTR 0x04
109 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
110 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
111 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
112 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
113 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
114 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
115 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
116 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
117 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
118 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
120 #define CQSPI_REG_WR_INSTR 0x08
121 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
122 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
123 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
125 #define CQSPI_REG_DELAY 0x0C
126 #define CQSPI_REG_DELAY_TSLCH_LSB 0
127 #define CQSPI_REG_DELAY_TCHSH_LSB 8
128 #define CQSPI_REG_DELAY_TSD2D_LSB 16
129 #define CQSPI_REG_DELAY_TSHSL_LSB 24
130 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
131 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
132 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
133 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
135 #define CQSPI_REG_READCAPTURE 0x10
136 #define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
137 #define CQSPI_REG_READCAPTURE_DELAY_LSB 1
138 #define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
140 #define CQSPI_REG_SIZE 0x14
141 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
142 #define CQSPI_REG_SIZE_PAGE_LSB 4
143 #define CQSPI_REG_SIZE_BLOCK_LSB 16
144 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
145 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
146 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
148 #define CQSPI_REG_SRAMPARTITION 0x18
149 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
151 #define CQSPI_REG_DMA 0x20
152 #define CQSPI_REG_DMA_SINGLE_LSB 0
153 #define CQSPI_REG_DMA_BURST_LSB 8
154 #define CQSPI_REG_DMA_SINGLE_MASK 0xFF
155 #define CQSPI_REG_DMA_BURST_MASK 0xFF
157 #define CQSPI_REG_REMAP 0x24
158 #define CQSPI_REG_MODE_BIT 0x28
160 #define CQSPI_REG_SDRAMLEVEL 0x2C
161 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
162 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
163 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
164 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
166 #define CQSPI_REG_IRQSTATUS 0x40
167 #define CQSPI_REG_IRQMASK 0x44
169 #define CQSPI_REG_INDIRECTRD 0x60
170 #define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
171 #define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
172 #define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
174 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
175 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
176 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
178 #define CQSPI_REG_CMDCTRL 0x90
179 #define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
180 #define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
181 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
182 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
183 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
184 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
185 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
186 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
187 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
188 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
189 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
190 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
192 #define CQSPI_REG_INDIRECTWR 0x70
193 #define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
194 #define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
195 #define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
197 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
198 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
199 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
201 #define CQSPI_REG_CMDADDRESS 0x94
202 #define CQSPI_REG_CMDREADDATALOWER 0xA0
203 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
204 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
205 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
207 /* Interrupt status bits */
208 #define CQSPI_REG_IRQ_MODE_ERR BIT(0)
209 #define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
210 #define CQSPI_REG_IRQ_IND_COMP BIT(2)
211 #define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
212 #define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
213 #define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
214 #define CQSPI_REG_IRQ_WATERMARK BIT(6)
215 #define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
217 #define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
218 CQSPI_REG_IRQ_IND_SRAM_FULL | \
219 CQSPI_REG_IRQ_IND_COMP)
221 #define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
222 CQSPI_REG_IRQ_WATERMARK | \
223 CQSPI_REG_IRQ_UNDERFLOW)
225 #define CQSPI_IRQ_STATUS_MASK 0x1FFFF
227 static int cqspi_wait_for_bit(void __iomem
*reg
, const u32 mask
, bool clear
)
229 unsigned long end
= jiffies
+ msecs_to_jiffies(CQSPI_TIMEOUT_MS
);
241 if (time_after(jiffies
, end
))
246 static bool cqspi_is_idle(struct cqspi_st
*cqspi
)
248 u32 reg
= readl(cqspi
->iobase
+ CQSPI_REG_CONFIG
);
250 return reg
& (1 << CQSPI_REG_CONFIG_IDLE_LSB
);
253 static u32
cqspi_get_rd_sram_level(struct cqspi_st
*cqspi
)
255 u32 reg
= readl(cqspi
->iobase
+ CQSPI_REG_SDRAMLEVEL
);
257 reg
>>= CQSPI_REG_SDRAMLEVEL_RD_LSB
;
258 return reg
& CQSPI_REG_SDRAMLEVEL_RD_MASK
;
261 static irqreturn_t
cqspi_irq_handler(int this_irq
, void *dev
)
263 struct cqspi_st
*cqspi
= dev
;
264 unsigned int irq_status
;
266 /* Read interrupt status */
267 irq_status
= readl(cqspi
->iobase
+ CQSPI_REG_IRQSTATUS
);
269 /* Clear interrupt */
270 writel(irq_status
, cqspi
->iobase
+ CQSPI_REG_IRQSTATUS
);
272 irq_status
&= CQSPI_IRQ_MASK_RD
| CQSPI_IRQ_MASK_WR
;
275 complete(&cqspi
->transfer_complete
);
280 static unsigned int cqspi_calc_rdreg(struct spi_nor
*nor
, const u8 opcode
)
282 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
285 rdreg
|= f_pdata
->inst_width
<< CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB
;
286 rdreg
|= f_pdata
->addr_width
<< CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB
;
287 rdreg
|= f_pdata
->data_width
<< CQSPI_REG_RD_INSTR_TYPE_DATA_LSB
;
292 static int cqspi_wait_idle(struct cqspi_st
*cqspi
)
294 const unsigned int poll_idle_retry
= 3;
295 unsigned int count
= 0;
296 unsigned long timeout
;
298 timeout
= jiffies
+ msecs_to_jiffies(CQSPI_TIMEOUT_MS
);
301 * Read few times in succession to ensure the controller
302 * is indeed idle, that is, the bit does not transition
305 if (cqspi_is_idle(cqspi
))
310 if (count
>= poll_idle_retry
)
313 if (time_after(jiffies
, timeout
)) {
314 /* Timeout, in busy mode. */
315 dev_err(&cqspi
->pdev
->dev
,
316 "QSPI is still busy after %dms timeout.\n",
325 static int cqspi_exec_flash_cmd(struct cqspi_st
*cqspi
, unsigned int reg
)
327 void __iomem
*reg_base
= cqspi
->iobase
;
330 /* Write the CMDCTRL without start execution. */
331 writel(reg
, reg_base
+ CQSPI_REG_CMDCTRL
);
333 reg
|= CQSPI_REG_CMDCTRL_EXECUTE_MASK
;
334 writel(reg
, reg_base
+ CQSPI_REG_CMDCTRL
);
336 /* Polling for completion. */
337 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_CMDCTRL
,
338 CQSPI_REG_CMDCTRL_INPROGRESS_MASK
, 1);
340 dev_err(&cqspi
->pdev
->dev
,
341 "Flash command execution timed out.\n");
345 /* Polling QSPI idle status. */
346 return cqspi_wait_idle(cqspi
);
349 static int cqspi_command_read(struct spi_nor
*nor
,
350 const u8
*txbuf
, const unsigned n_tx
,
351 u8
*rxbuf
, const unsigned n_rx
)
353 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
354 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
355 void __iomem
*reg_base
= cqspi
->iobase
;
358 unsigned int read_len
;
361 if (!n_rx
|| n_rx
> CQSPI_STIG_DATA_LEN_MAX
|| !rxbuf
) {
362 dev_err(nor
->dev
, "Invalid input argument, len %d rxbuf 0x%p\n",
367 reg
= txbuf
[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB
;
369 rdreg
= cqspi_calc_rdreg(nor
, txbuf
[0]);
370 writel(rdreg
, reg_base
+ CQSPI_REG_RD_INSTR
);
372 reg
|= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB
);
374 /* 0 means 1 byte. */
375 reg
|= (((n_rx
- 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK
)
376 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB
);
377 status
= cqspi_exec_flash_cmd(cqspi
, reg
);
381 reg
= readl(reg_base
+ CQSPI_REG_CMDREADDATALOWER
);
383 /* Put the read value into rx_buf */
384 read_len
= (n_rx
> 4) ? 4 : n_rx
;
385 memcpy(rxbuf
, ®
, read_len
);
389 reg
= readl(reg_base
+ CQSPI_REG_CMDREADDATAUPPER
);
391 read_len
= n_rx
- read_len
;
392 memcpy(rxbuf
, ®
, read_len
);
398 static int cqspi_command_write(struct spi_nor
*nor
, const u8 opcode
,
399 const u8
*txbuf
, const unsigned n_tx
)
401 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
402 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
403 void __iomem
*reg_base
= cqspi
->iobase
;
408 if (n_tx
> 4 || (n_tx
&& !txbuf
)) {
410 "Invalid input argument, cmdlen %d txbuf 0x%p\n",
415 reg
= opcode
<< CQSPI_REG_CMDCTRL_OPCODE_LSB
;
417 reg
|= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB
);
418 reg
|= ((n_tx
- 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK
)
419 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB
;
421 memcpy(&data
, txbuf
, n_tx
);
422 writel(data
, reg_base
+ CQSPI_REG_CMDWRITEDATALOWER
);
425 ret
= cqspi_exec_flash_cmd(cqspi
, reg
);
429 static int cqspi_command_write_addr(struct spi_nor
*nor
,
430 const u8 opcode
, const unsigned int addr
)
432 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
433 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
434 void __iomem
*reg_base
= cqspi
->iobase
;
437 reg
= opcode
<< CQSPI_REG_CMDCTRL_OPCODE_LSB
;
438 reg
|= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB
);
439 reg
|= ((nor
->addr_width
- 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK
)
440 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB
;
442 writel(addr
, reg_base
+ CQSPI_REG_CMDADDRESS
);
444 return cqspi_exec_flash_cmd(cqspi
, reg
);
447 static int cqspi_indirect_read_setup(struct spi_nor
*nor
,
448 const unsigned int from_addr
)
450 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
451 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
452 void __iomem
*reg_base
= cqspi
->iobase
;
453 unsigned int dummy_clk
= 0;
456 writel(from_addr
, reg_base
+ CQSPI_REG_INDIRECTRDSTARTADDR
);
458 reg
= nor
->read_opcode
<< CQSPI_REG_RD_INSTR_OPCODE_LSB
;
459 reg
|= cqspi_calc_rdreg(nor
, nor
->read_opcode
);
461 /* Setup dummy clock cycles */
462 dummy_clk
= nor
->read_dummy
;
463 if (dummy_clk
> CQSPI_DUMMY_CLKS_MAX
)
464 dummy_clk
= CQSPI_DUMMY_CLKS_MAX
;
467 reg
|= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB
);
468 /* Set mode bits high to ensure chip doesn't enter XIP */
469 writel(0xFF, reg_base
+ CQSPI_REG_MODE_BIT
);
471 /* Need to subtract the mode byte (8 clocks). */
472 if (f_pdata
->inst_width
!= CQSPI_INST_TYPE_QUAD
)
476 reg
|= (dummy_clk
& CQSPI_REG_RD_INSTR_DUMMY_MASK
)
477 << CQSPI_REG_RD_INSTR_DUMMY_LSB
;
480 writel(reg
, reg_base
+ CQSPI_REG_RD_INSTR
);
482 /* Set address width */
483 reg
= readl(reg_base
+ CQSPI_REG_SIZE
);
484 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
485 reg
|= (nor
->addr_width
- 1);
486 writel(reg
, reg_base
+ CQSPI_REG_SIZE
);
490 static int cqspi_indirect_read_execute(struct spi_nor
*nor
,
491 u8
*rxbuf
, const unsigned n_rx
)
493 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
494 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
495 void __iomem
*reg_base
= cqspi
->iobase
;
496 void __iomem
*ahb_base
= cqspi
->ahb_base
;
497 unsigned int remaining
= n_rx
;
498 unsigned int bytes_to_read
= 0;
501 writel(remaining
, reg_base
+ CQSPI_REG_INDIRECTRDBYTES
);
503 /* Clear all interrupts. */
504 writel(CQSPI_IRQ_STATUS_MASK
, reg_base
+ CQSPI_REG_IRQSTATUS
);
506 writel(CQSPI_IRQ_MASK_RD
, reg_base
+ CQSPI_REG_IRQMASK
);
508 reinit_completion(&cqspi
->transfer_complete
);
509 writel(CQSPI_REG_INDIRECTRD_START_MASK
,
510 reg_base
+ CQSPI_REG_INDIRECTRD
);
512 while (remaining
> 0) {
513 ret
= wait_for_completion_timeout(&cqspi
->transfer_complete
,
515 (CQSPI_READ_TIMEOUT_MS
));
517 bytes_to_read
= cqspi_get_rd_sram_level(cqspi
);
519 if (!ret
&& bytes_to_read
== 0) {
520 dev_err(nor
->dev
, "Indirect read timeout, no bytes\n");
525 while (bytes_to_read
!= 0) {
526 bytes_to_read
*= cqspi
->fifo_width
;
527 bytes_to_read
= bytes_to_read
> remaining
?
528 remaining
: bytes_to_read
;
529 readsl(ahb_base
, rxbuf
, DIV_ROUND_UP(bytes_to_read
, 4));
530 rxbuf
+= bytes_to_read
;
531 remaining
-= bytes_to_read
;
532 bytes_to_read
= cqspi_get_rd_sram_level(cqspi
);
536 reinit_completion(&cqspi
->transfer_complete
);
539 /* Check indirect done status */
540 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_INDIRECTRD
,
541 CQSPI_REG_INDIRECTRD_DONE_MASK
, 0);
544 "Indirect read completion error (%i)\n", ret
);
548 /* Disable interrupt */
549 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
551 /* Clear indirect completion status */
552 writel(CQSPI_REG_INDIRECTRD_DONE_MASK
, reg_base
+ CQSPI_REG_INDIRECTRD
);
557 /* Disable interrupt */
558 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
560 /* Cancel the indirect read */
561 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK
,
562 reg_base
+ CQSPI_REG_INDIRECTRD
);
566 static int cqspi_indirect_write_setup(struct spi_nor
*nor
,
567 const unsigned int to_addr
)
570 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
571 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
572 void __iomem
*reg_base
= cqspi
->iobase
;
575 reg
= nor
->program_opcode
<< CQSPI_REG_WR_INSTR_OPCODE_LSB
;
576 writel(reg
, reg_base
+ CQSPI_REG_WR_INSTR
);
577 reg
= cqspi_calc_rdreg(nor
, nor
->program_opcode
);
578 writel(reg
, reg_base
+ CQSPI_REG_RD_INSTR
);
580 writel(to_addr
, reg_base
+ CQSPI_REG_INDIRECTWRSTARTADDR
);
582 reg
= readl(reg_base
+ CQSPI_REG_SIZE
);
583 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
584 reg
|= (nor
->addr_width
- 1);
585 writel(reg
, reg_base
+ CQSPI_REG_SIZE
);
589 static int cqspi_indirect_write_execute(struct spi_nor
*nor
,
590 const u8
*txbuf
, const unsigned n_tx
)
592 const unsigned int page_size
= nor
->page_size
;
593 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
594 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
595 void __iomem
*reg_base
= cqspi
->iobase
;
596 unsigned int remaining
= n_tx
;
597 unsigned int write_bytes
;
600 writel(remaining
, reg_base
+ CQSPI_REG_INDIRECTWRBYTES
);
602 /* Clear all interrupts. */
603 writel(CQSPI_IRQ_STATUS_MASK
, reg_base
+ CQSPI_REG_IRQSTATUS
);
605 writel(CQSPI_IRQ_MASK_WR
, reg_base
+ CQSPI_REG_IRQMASK
);
607 reinit_completion(&cqspi
->transfer_complete
);
608 writel(CQSPI_REG_INDIRECTWR_START_MASK
,
609 reg_base
+ CQSPI_REG_INDIRECTWR
);
611 while (remaining
> 0) {
612 write_bytes
= remaining
> page_size
? page_size
: remaining
;
613 writesl(cqspi
->ahb_base
, txbuf
, DIV_ROUND_UP(write_bytes
, 4));
615 ret
= wait_for_completion_timeout(&cqspi
->transfer_complete
,
619 dev_err(nor
->dev
, "Indirect write timeout\n");
624 txbuf
+= write_bytes
;
625 remaining
-= write_bytes
;
628 reinit_completion(&cqspi
->transfer_complete
);
631 /* Check indirect done status */
632 ret
= cqspi_wait_for_bit(reg_base
+ CQSPI_REG_INDIRECTWR
,
633 CQSPI_REG_INDIRECTWR_DONE_MASK
, 0);
636 "Indirect write completion error (%i)\n", ret
);
640 /* Disable interrupt. */
641 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
643 /* Clear indirect completion status */
644 writel(CQSPI_REG_INDIRECTWR_DONE_MASK
, reg_base
+ CQSPI_REG_INDIRECTWR
);
646 cqspi_wait_idle(cqspi
);
651 /* Disable interrupt. */
652 writel(0, reg_base
+ CQSPI_REG_IRQMASK
);
654 /* Cancel the indirect write */
655 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK
,
656 reg_base
+ CQSPI_REG_INDIRECTWR
);
660 static void cqspi_chipselect(struct spi_nor
*nor
)
662 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
663 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
664 void __iomem
*reg_base
= cqspi
->iobase
;
665 unsigned int chip_select
= f_pdata
->cs
;
668 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
669 if (cqspi
->is_decoded_cs
) {
670 reg
|= CQSPI_REG_CONFIG_DECODE_MASK
;
672 reg
&= ~CQSPI_REG_CONFIG_DECODE_MASK
;
674 /* Convert CS if without decoder.
680 chip_select
= 0xF & ~(1 << chip_select
);
683 reg
&= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
684 << CQSPI_REG_CONFIG_CHIPSELECT_LSB
);
685 reg
|= (chip_select
& CQSPI_REG_CONFIG_CHIPSELECT_MASK
)
686 << CQSPI_REG_CONFIG_CHIPSELECT_LSB
;
687 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
690 static void cqspi_configure_cs_and_sizes(struct spi_nor
*nor
)
692 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
693 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
694 void __iomem
*iobase
= cqspi
->iobase
;
697 /* configure page size and block size. */
698 reg
= readl(iobase
+ CQSPI_REG_SIZE
);
699 reg
&= ~(CQSPI_REG_SIZE_PAGE_MASK
<< CQSPI_REG_SIZE_PAGE_LSB
);
700 reg
&= ~(CQSPI_REG_SIZE_BLOCK_MASK
<< CQSPI_REG_SIZE_BLOCK_LSB
);
701 reg
&= ~CQSPI_REG_SIZE_ADDRESS_MASK
;
702 reg
|= (nor
->page_size
<< CQSPI_REG_SIZE_PAGE_LSB
);
703 reg
|= (ilog2(nor
->mtd
.erasesize
) << CQSPI_REG_SIZE_BLOCK_LSB
);
704 reg
|= (nor
->addr_width
- 1);
705 writel(reg
, iobase
+ CQSPI_REG_SIZE
);
707 /* configure the chip select */
708 cqspi_chipselect(nor
);
710 /* Store the new configuration of the controller */
711 cqspi
->current_page_size
= nor
->page_size
;
712 cqspi
->current_erase_size
= nor
->mtd
.erasesize
;
713 cqspi
->current_addr_width
= nor
->addr_width
;
716 static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz
,
717 const unsigned int ns_val
)
721 ticks
= ref_clk_hz
/ 1000; /* kHz */
722 ticks
= DIV_ROUND_UP(ticks
* ns_val
, 1000000);
727 static void cqspi_delay(struct spi_nor
*nor
)
729 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
730 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
731 void __iomem
*iobase
= cqspi
->iobase
;
732 const unsigned int ref_clk_hz
= cqspi
->master_ref_clk_hz
;
733 unsigned int tshsl
, tchsh
, tslch
, tsd2d
;
737 /* calculate the number of ref ticks for one sclk tick */
738 tsclk
= DIV_ROUND_UP(ref_clk_hz
, cqspi
->sclk
);
740 tshsl
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tshsl_ns
);
741 /* this particular value must be at least one sclk */
745 tchsh
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tchsh_ns
);
746 tslch
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tslch_ns
);
747 tsd2d
= calculate_ticks_for_ns(ref_clk_hz
, f_pdata
->tsd2d_ns
);
749 reg
= (tshsl
& CQSPI_REG_DELAY_TSHSL_MASK
)
750 << CQSPI_REG_DELAY_TSHSL_LSB
;
751 reg
|= (tchsh
& CQSPI_REG_DELAY_TCHSH_MASK
)
752 << CQSPI_REG_DELAY_TCHSH_LSB
;
753 reg
|= (tslch
& CQSPI_REG_DELAY_TSLCH_MASK
)
754 << CQSPI_REG_DELAY_TSLCH_LSB
;
755 reg
|= (tsd2d
& CQSPI_REG_DELAY_TSD2D_MASK
)
756 << CQSPI_REG_DELAY_TSD2D_LSB
;
757 writel(reg
, iobase
+ CQSPI_REG_DELAY
);
760 static void cqspi_config_baudrate_div(struct cqspi_st
*cqspi
)
762 const unsigned int ref_clk_hz
= cqspi
->master_ref_clk_hz
;
763 void __iomem
*reg_base
= cqspi
->iobase
;
766 /* Recalculate the baudrate divisor based on QSPI specification. */
767 div
= DIV_ROUND_UP(ref_clk_hz
, 2 * cqspi
->sclk
) - 1;
769 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
770 reg
&= ~(CQSPI_REG_CONFIG_BAUD_MASK
<< CQSPI_REG_CONFIG_BAUD_LSB
);
771 reg
|= (div
& CQSPI_REG_CONFIG_BAUD_MASK
) << CQSPI_REG_CONFIG_BAUD_LSB
;
772 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
775 static void cqspi_readdata_capture(struct cqspi_st
*cqspi
,
776 const unsigned int bypass
,
777 const unsigned int delay
)
779 void __iomem
*reg_base
= cqspi
->iobase
;
782 reg
= readl(reg_base
+ CQSPI_REG_READCAPTURE
);
785 reg
|= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB
);
787 reg
&= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB
);
789 reg
&= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
790 << CQSPI_REG_READCAPTURE_DELAY_LSB
);
792 reg
|= (delay
& CQSPI_REG_READCAPTURE_DELAY_MASK
)
793 << CQSPI_REG_READCAPTURE_DELAY_LSB
;
795 writel(reg
, reg_base
+ CQSPI_REG_READCAPTURE
);
798 static void cqspi_controller_enable(struct cqspi_st
*cqspi
, bool enable
)
800 void __iomem
*reg_base
= cqspi
->iobase
;
803 reg
= readl(reg_base
+ CQSPI_REG_CONFIG
);
806 reg
|= CQSPI_REG_CONFIG_ENABLE_MASK
;
808 reg
&= ~CQSPI_REG_CONFIG_ENABLE_MASK
;
810 writel(reg
, reg_base
+ CQSPI_REG_CONFIG
);
813 static void cqspi_configure(struct spi_nor
*nor
)
815 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
816 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
817 const unsigned int sclk
= f_pdata
->clk_rate
;
818 int switch_cs
= (cqspi
->current_cs
!= f_pdata
->cs
);
819 int switch_ck
= (cqspi
->sclk
!= sclk
);
821 if ((cqspi
->current_page_size
!= nor
->page_size
) ||
822 (cqspi
->current_erase_size
!= nor
->mtd
.erasesize
) ||
823 (cqspi
->current_addr_width
!= nor
->addr_width
))
826 if (switch_cs
|| switch_ck
)
827 cqspi_controller_enable(cqspi
, 0);
829 /* Switch chip select. */
831 cqspi
->current_cs
= f_pdata
->cs
;
832 cqspi_configure_cs_and_sizes(nor
);
835 /* Setup baudrate divisor and delays */
838 cqspi_config_baudrate_div(cqspi
);
840 cqspi_readdata_capture(cqspi
, 1, f_pdata
->read_delay
);
843 if (switch_cs
|| switch_ck
)
844 cqspi_controller_enable(cqspi
, 1);
847 static int cqspi_set_protocol(struct spi_nor
*nor
, const int read
)
849 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
851 f_pdata
->inst_width
= CQSPI_INST_TYPE_SINGLE
;
852 f_pdata
->addr_width
= CQSPI_INST_TYPE_SINGLE
;
853 f_pdata
->data_width
= CQSPI_INST_TYPE_SINGLE
;
856 switch (nor
->flash_read
) {
859 f_pdata
->data_width
= CQSPI_INST_TYPE_SINGLE
;
862 f_pdata
->data_width
= CQSPI_INST_TYPE_DUAL
;
865 f_pdata
->data_width
= CQSPI_INST_TYPE_QUAD
;
872 cqspi_configure(nor
);
877 static ssize_t
cqspi_write(struct spi_nor
*nor
, loff_t to
,
878 size_t len
, const u_char
*buf
)
882 ret
= cqspi_set_protocol(nor
, 0);
886 ret
= cqspi_indirect_write_setup(nor
, to
);
890 ret
= cqspi_indirect_write_execute(nor
, buf
, len
);
894 return (ret
< 0) ? ret
: len
;
897 static ssize_t
cqspi_read(struct spi_nor
*nor
, loff_t from
,
898 size_t len
, u_char
*buf
)
902 ret
= cqspi_set_protocol(nor
, 1);
906 ret
= cqspi_indirect_read_setup(nor
, from
);
910 ret
= cqspi_indirect_read_execute(nor
, buf
, len
);
914 return (ret
< 0) ? ret
: len
;
917 static int cqspi_erase(struct spi_nor
*nor
, loff_t offs
)
921 ret
= cqspi_set_protocol(nor
, 0);
925 /* Send write enable, then erase commands. */
926 ret
= nor
->write_reg(nor
, SPINOR_OP_WREN
, NULL
, 0);
930 /* Set up command buffer. */
931 ret
= cqspi_command_write_addr(nor
, nor
->erase_opcode
, offs
);
938 static int cqspi_prep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
940 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
941 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
943 mutex_lock(&cqspi
->bus_mutex
);
948 static void cqspi_unprep(struct spi_nor
*nor
, enum spi_nor_ops ops
)
950 struct cqspi_flash_pdata
*f_pdata
= nor
->priv
;
951 struct cqspi_st
*cqspi
= f_pdata
->cqspi
;
953 mutex_unlock(&cqspi
->bus_mutex
);
956 static int cqspi_read_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
960 ret
= cqspi_set_protocol(nor
, 0);
962 ret
= cqspi_command_read(nor
, &opcode
, 1, buf
, len
);
967 static int cqspi_write_reg(struct spi_nor
*nor
, u8 opcode
, u8
*buf
, int len
)
971 ret
= cqspi_set_protocol(nor
, 0);
973 ret
= cqspi_command_write(nor
, opcode
, buf
, len
);
978 static int cqspi_of_get_flash_pdata(struct platform_device
*pdev
,
979 struct cqspi_flash_pdata
*f_pdata
,
980 struct device_node
*np
)
982 if (of_property_read_u32(np
, "cdns,read-delay", &f_pdata
->read_delay
)) {
983 dev_err(&pdev
->dev
, "couldn't determine read-delay\n");
987 if (of_property_read_u32(np
, "cdns,tshsl-ns", &f_pdata
->tshsl_ns
)) {
988 dev_err(&pdev
->dev
, "couldn't determine tshsl-ns\n");
992 if (of_property_read_u32(np
, "cdns,tsd2d-ns", &f_pdata
->tsd2d_ns
)) {
993 dev_err(&pdev
->dev
, "couldn't determine tsd2d-ns\n");
997 if (of_property_read_u32(np
, "cdns,tchsh-ns", &f_pdata
->tchsh_ns
)) {
998 dev_err(&pdev
->dev
, "couldn't determine tchsh-ns\n");
1002 if (of_property_read_u32(np
, "cdns,tslch-ns", &f_pdata
->tslch_ns
)) {
1003 dev_err(&pdev
->dev
, "couldn't determine tslch-ns\n");
1007 if (of_property_read_u32(np
, "spi-max-frequency", &f_pdata
->clk_rate
)) {
1008 dev_err(&pdev
->dev
, "couldn't determine spi-max-frequency\n");
1015 static int cqspi_of_get_pdata(struct platform_device
*pdev
)
1017 struct device_node
*np
= pdev
->dev
.of_node
;
1018 struct cqspi_st
*cqspi
= platform_get_drvdata(pdev
);
1020 cqspi
->is_decoded_cs
= of_property_read_bool(np
, "cdns,is-decoded-cs");
1022 if (of_property_read_u32(np
, "cdns,fifo-depth", &cqspi
->fifo_depth
)) {
1023 dev_err(&pdev
->dev
, "couldn't determine fifo-depth\n");
1027 if (of_property_read_u32(np
, "cdns,fifo-width", &cqspi
->fifo_width
)) {
1028 dev_err(&pdev
->dev
, "couldn't determine fifo-width\n");
1032 if (of_property_read_u32(np
, "cdns,trigger-address",
1033 &cqspi
->trigger_address
)) {
1034 dev_err(&pdev
->dev
, "couldn't determine trigger-address\n");
1041 static void cqspi_controller_init(struct cqspi_st
*cqspi
)
1043 cqspi_controller_enable(cqspi
, 0);
1045 /* Configure the remap address register, no remap */
1046 writel(0, cqspi
->iobase
+ CQSPI_REG_REMAP
);
1048 /* Disable all interrupts. */
1049 writel(0, cqspi
->iobase
+ CQSPI_REG_IRQMASK
);
1051 /* Configure the SRAM split to 1:1 . */
1052 writel(cqspi
->fifo_depth
/ 2, cqspi
->iobase
+ CQSPI_REG_SRAMPARTITION
);
1054 /* Load indirect trigger address. */
1055 writel(cqspi
->trigger_address
,
1056 cqspi
->iobase
+ CQSPI_REG_INDIRECTTRIGGER
);
1058 /* Program read watermark -- 1/2 of the FIFO. */
1059 writel(cqspi
->fifo_depth
* cqspi
->fifo_width
/ 2,
1060 cqspi
->iobase
+ CQSPI_REG_INDIRECTRDWATERMARK
);
1061 /* Program write watermark -- 1/8 of the FIFO. */
1062 writel(cqspi
->fifo_depth
* cqspi
->fifo_width
/ 8,
1063 cqspi
->iobase
+ CQSPI_REG_INDIRECTWRWATERMARK
);
1065 cqspi_controller_enable(cqspi
, 1);
1068 static int cqspi_setup_flash(struct cqspi_st
*cqspi
, struct device_node
*np
)
1070 struct platform_device
*pdev
= cqspi
->pdev
;
1071 struct device
*dev
= &pdev
->dev
;
1072 struct cqspi_flash_pdata
*f_pdata
;
1073 struct spi_nor
*nor
;
1074 struct mtd_info
*mtd
;
1078 /* Get flash device data */
1079 for_each_available_child_of_node(dev
->of_node
, np
) {
1080 if (of_property_read_u32(np
, "reg", &cs
)) {
1081 dev_err(dev
, "Couldn't determine chip select.\n");
1085 if (cs
> CQSPI_MAX_CHIPSELECT
) {
1086 dev_err(dev
, "Chip select %d out of range.\n", cs
);
1090 f_pdata
= &cqspi
->f_pdata
[cs
];
1091 f_pdata
->cqspi
= cqspi
;
1094 ret
= cqspi_of_get_flash_pdata(pdev
, f_pdata
, np
);
1098 nor
= &f_pdata
->nor
;
1104 spi_nor_set_flash_node(nor
, np
);
1105 nor
->priv
= f_pdata
;
1107 nor
->read_reg
= cqspi_read_reg
;
1108 nor
->write_reg
= cqspi_write_reg
;
1109 nor
->read
= cqspi_read
;
1110 nor
->write
= cqspi_write
;
1111 nor
->erase
= cqspi_erase
;
1112 nor
->prepare
= cqspi_prep
;
1113 nor
->unprepare
= cqspi_unprep
;
1115 mtd
->name
= devm_kasprintf(dev
, GFP_KERNEL
, "%s.%d",
1122 ret
= spi_nor_scan(nor
, NULL
, SPI_NOR_QUAD
);
1126 ret
= mtd_device_register(mtd
, NULL
, 0);
1130 f_pdata
->registered
= true;
1136 for (i
= 0; i
< CQSPI_MAX_CHIPSELECT
; i
++)
1137 if (cqspi
->f_pdata
[i
].registered
)
1138 mtd_device_unregister(&cqspi
->f_pdata
[i
].nor
.mtd
);
1142 static int cqspi_probe(struct platform_device
*pdev
)
1144 struct device_node
*np
= pdev
->dev
.of_node
;
1145 struct device
*dev
= &pdev
->dev
;
1146 struct cqspi_st
*cqspi
;
1147 struct resource
*res
;
1148 struct resource
*res_ahb
;
1152 cqspi
= devm_kzalloc(dev
, sizeof(*cqspi
), GFP_KERNEL
);
1156 mutex_init(&cqspi
->bus_mutex
);
1158 platform_set_drvdata(pdev
, cqspi
);
1160 /* Obtain configuration from OF. */
1161 ret
= cqspi_of_get_pdata(pdev
);
1163 dev_err(dev
, "Cannot get mandatory OF data.\n");
1167 /* Obtain QSPI clock. */
1168 cqspi
->clk
= devm_clk_get(dev
, NULL
);
1169 if (IS_ERR(cqspi
->clk
)) {
1170 dev_err(dev
, "Cannot claim QSPI clock.\n");
1171 return PTR_ERR(cqspi
->clk
);
1174 /* Obtain and remap controller address. */
1175 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1176 cqspi
->iobase
= devm_ioremap_resource(dev
, res
);
1177 if (IS_ERR(cqspi
->iobase
)) {
1178 dev_err(dev
, "Cannot remap controller address.\n");
1179 return PTR_ERR(cqspi
->iobase
);
1182 /* Obtain and remap AHB address. */
1183 res_ahb
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1184 cqspi
->ahb_base
= devm_ioremap_resource(dev
, res_ahb
);
1185 if (IS_ERR(cqspi
->ahb_base
)) {
1186 dev_err(dev
, "Cannot remap AHB address.\n");
1187 return PTR_ERR(cqspi
->ahb_base
);
1190 init_completion(&cqspi
->transfer_complete
);
1192 /* Obtain IRQ line. */
1193 irq
= platform_get_irq(pdev
, 0);
1195 dev_err(dev
, "Cannot obtain IRQ.\n");
1199 ret
= clk_prepare_enable(cqspi
->clk
);
1201 dev_err(dev
, "Cannot enable QSPI clock.\n");
1205 cqspi
->master_ref_clk_hz
= clk_get_rate(cqspi
->clk
);
1207 ret
= devm_request_irq(dev
, irq
, cqspi_irq_handler
, 0,
1210 dev_err(dev
, "Cannot request IRQ.\n");
1211 goto probe_irq_failed
;
1214 cqspi_wait_idle(cqspi
);
1215 cqspi_controller_init(cqspi
);
1216 cqspi
->current_cs
= -1;
1219 ret
= cqspi_setup_flash(cqspi
, np
);
1221 dev_err(dev
, "Cadence QSPI NOR probe failed %d\n", ret
);
1222 goto probe_setup_failed
;
1227 cqspi_controller_enable(cqspi
, 0);
1229 clk_disable_unprepare(cqspi
->clk
);
1233 static int cqspi_remove(struct platform_device
*pdev
)
1235 struct cqspi_st
*cqspi
= platform_get_drvdata(pdev
);
1238 for (i
= 0; i
< CQSPI_MAX_CHIPSELECT
; i
++)
1239 if (cqspi
->f_pdata
[i
].registered
)
1240 mtd_device_unregister(&cqspi
->f_pdata
[i
].nor
.mtd
);
1242 cqspi_controller_enable(cqspi
, 0);
1244 clk_disable_unprepare(cqspi
->clk
);
1249 #ifdef CONFIG_PM_SLEEP
1250 static int cqspi_suspend(struct device
*dev
)
1252 struct cqspi_st
*cqspi
= dev_get_drvdata(dev
);
1254 cqspi_controller_enable(cqspi
, 0);
1258 static int cqspi_resume(struct device
*dev
)
1260 struct cqspi_st
*cqspi
= dev_get_drvdata(dev
);
1262 cqspi_controller_enable(cqspi
, 1);
1266 static const struct dev_pm_ops cqspi__dev_pm_ops
= {
1267 .suspend
= cqspi_suspend
,
1268 .resume
= cqspi_resume
,
1271 #define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
1273 #define CQSPI_DEV_PM_OPS NULL
1276 static struct of_device_id
const cqspi_dt_ids
[] = {
1277 {.compatible
= "cdns,qspi-nor",},
1278 { /* end of table */ }
1281 MODULE_DEVICE_TABLE(of
, cqspi_dt_ids
);
1283 static struct platform_driver cqspi_platform_driver
= {
1284 .probe
= cqspi_probe
,
1285 .remove
= cqspi_remove
,
1288 .pm
= CQSPI_DEV_PM_OPS
,
1289 .of_match_table
= cqspi_dt_ids
,
1293 module_platform_driver(cqspi_platform_driver
);
1295 MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
1296 MODULE_LICENSE("GPL v2");
1297 MODULE_ALIAS("platform:" CQSPI_NAME
);
1298 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
1299 MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");