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/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/mtd/spi-nor.h>
22 #include <linux/of_device.h>
24 #include <linux/platform_device.h>
25 #include <linux/pm_runtime.h>
26 #include <linux/sched.h>
27 #include <linux/spi/spi.h>
28 #include <linux/timer.h>
30 #define CQSPI_NAME "cadence-qspi"
31 #define CQSPI_MAX_CHIPSELECT 16
34 #define CQSPI_NEEDS_WR_DELAY BIT(0)
36 /* Capabilities mask */
37 #define CQSPI_BASE_HWCAPS_MASK \
38 (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST | \
39 SNOR_HWCAPS_READ_1_1_2 | SNOR_HWCAPS_READ_1_1_4 | \
44 struct cqspi_flash_pdata {
46 struct cqspi_st *cqspi;
62 struct platform_device *pdev;
68 void __iomem *ahb_base;
69 resource_size_t ahb_size;
70 struct completion transfer_complete;
71 struct mutex bus_mutex;
73 struct dma_chan *rx_chan;
74 struct completion rx_dma_complete;
75 dma_addr_t mmap_phys_base;
78 int current_page_size;
79 int current_erase_size;
80 int current_addr_width;
81 unsigned long master_ref_clk_hz;
88 struct cqspi_flash_pdata f_pdata[CQSPI_MAX_CHIPSELECT];
91 struct cqspi_driver_platdata {
96 /* Operation timeout value */
97 #define CQSPI_TIMEOUT_MS 500
98 #define CQSPI_READ_TIMEOUT_MS 10
100 /* Instruction type */
101 #define CQSPI_INST_TYPE_SINGLE 0
102 #define CQSPI_INST_TYPE_DUAL 1
103 #define CQSPI_INST_TYPE_QUAD 2
104 #define CQSPI_INST_TYPE_OCTAL 3
106 #define CQSPI_DUMMY_CLKS_PER_BYTE 8
107 #define CQSPI_DUMMY_BYTES_MAX 4
108 #define CQSPI_DUMMY_CLKS_MAX 31
110 #define CQSPI_STIG_DATA_LEN_MAX 8
113 #define CQSPI_REG_CONFIG 0x00
114 #define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
115 #define CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL BIT(7)
116 #define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
117 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
118 #define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
119 #define CQSPI_REG_CONFIG_BAUD_LSB 19
120 #define CQSPI_REG_CONFIG_IDLE_LSB 31
121 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
122 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
124 #define CQSPI_REG_RD_INSTR 0x04
125 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
126 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
127 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
128 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
129 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
130 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
131 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
132 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
133 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
134 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
136 #define CQSPI_REG_WR_INSTR 0x08
137 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
138 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
139 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
141 #define CQSPI_REG_DELAY 0x0C
142 #define CQSPI_REG_DELAY_TSLCH_LSB 0
143 #define CQSPI_REG_DELAY_TCHSH_LSB 8
144 #define CQSPI_REG_DELAY_TSD2D_LSB 16
145 #define CQSPI_REG_DELAY_TSHSL_LSB 24
146 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
147 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
148 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
149 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
151 #define CQSPI_REG_READCAPTURE 0x10
152 #define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
153 #define CQSPI_REG_READCAPTURE_DELAY_LSB 1
154 #define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
156 #define CQSPI_REG_SIZE 0x14
157 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
158 #define CQSPI_REG_SIZE_PAGE_LSB 4
159 #define CQSPI_REG_SIZE_BLOCK_LSB 16
160 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
161 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
162 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
164 #define CQSPI_REG_SRAMPARTITION 0x18
165 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
167 #define CQSPI_REG_DMA 0x20
168 #define CQSPI_REG_DMA_SINGLE_LSB 0
169 #define CQSPI_REG_DMA_BURST_LSB 8
170 #define CQSPI_REG_DMA_SINGLE_MASK 0xFF
171 #define CQSPI_REG_DMA_BURST_MASK 0xFF
173 #define CQSPI_REG_REMAP 0x24
174 #define CQSPI_REG_MODE_BIT 0x28
176 #define CQSPI_REG_SDRAMLEVEL 0x2C
177 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
178 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
179 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
180 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
182 #define CQSPI_REG_IRQSTATUS 0x40
183 #define CQSPI_REG_IRQMASK 0x44
185 #define CQSPI_REG_INDIRECTRD 0x60
186 #define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
187 #define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
188 #define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
190 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
191 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
192 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
194 #define CQSPI_REG_CMDCTRL 0x90
195 #define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
196 #define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
197 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
198 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
199 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
200 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
201 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
202 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
203 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
204 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
205 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
206 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
208 #define CQSPI_REG_INDIRECTWR 0x70
209 #define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
210 #define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
211 #define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
213 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
214 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
215 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
217 #define CQSPI_REG_CMDADDRESS 0x94
218 #define CQSPI_REG_CMDREADDATALOWER 0xA0
219 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
220 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
221 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
223 /* Interrupt status bits */
224 #define CQSPI_REG_IRQ_MODE_ERR BIT(0)
225 #define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
226 #define CQSPI_REG_IRQ_IND_COMP BIT(2)
227 #define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
228 #define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
229 #define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
230 #define CQSPI_REG_IRQ_WATERMARK BIT(6)
231 #define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
233 #define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
234 CQSPI_REG_IRQ_IND_SRAM_FULL | \
235 CQSPI_REG_IRQ_IND_COMP)
237 #define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
238 CQSPI_REG_IRQ_WATERMARK | \
239 CQSPI_REG_IRQ_UNDERFLOW)
241 #define CQSPI_IRQ_STATUS_MASK 0x1FFFF
243 static int cqspi_wait_for_bit(void __iomem *reg, const u32 mask, bool clear)
245 unsigned long end = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
257 if (time_after(jiffies, end))
262 static bool cqspi_is_idle(struct cqspi_st *cqspi)
264 u32 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
266 return reg & (1 << CQSPI_REG_CONFIG_IDLE_LSB);
269 static u32 cqspi_get_rd_sram_level(struct cqspi_st *cqspi)
271 u32 reg = readl(cqspi->iobase + CQSPI_REG_SDRAMLEVEL);
273 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
274 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
277 static irqreturn_t cqspi_irq_handler(int this_irq, void *dev)
279 struct cqspi_st *cqspi = dev;
280 unsigned int irq_status;
282 /* Read interrupt status */
283 irq_status = readl(cqspi->iobase + CQSPI_REG_IRQSTATUS);
285 /* Clear interrupt */
286 writel(irq_status, cqspi->iobase + CQSPI_REG_IRQSTATUS);
288 irq_status &= CQSPI_IRQ_MASK_RD | CQSPI_IRQ_MASK_WR;
291 complete(&cqspi->transfer_complete);
296 static unsigned int cqspi_calc_rdreg(struct spi_nor *nor, const u8 opcode)
298 struct cqspi_flash_pdata *f_pdata = nor->priv;
301 rdreg |= f_pdata->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB;
302 rdreg |= f_pdata->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB;
303 rdreg |= f_pdata->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
308 static int cqspi_wait_idle(struct cqspi_st *cqspi)
310 const unsigned int poll_idle_retry = 3;
311 unsigned int count = 0;
312 unsigned long timeout;
314 timeout = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
317 * Read few times in succession to ensure the controller
318 * is indeed idle, that is, the bit does not transition
321 if (cqspi_is_idle(cqspi))
326 if (count >= poll_idle_retry)
329 if (time_after(jiffies, timeout)) {
330 /* Timeout, in busy mode. */
331 dev_err(&cqspi->pdev->dev,
332 "QSPI is still busy after %dms timeout.\n",
341 static int cqspi_exec_flash_cmd(struct cqspi_st *cqspi, unsigned int reg)
343 void __iomem *reg_base = cqspi->iobase;
346 /* Write the CMDCTRL without start execution. */
347 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
349 reg |= CQSPI_REG_CMDCTRL_EXECUTE_MASK;
350 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
352 /* Polling for completion. */
353 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_CMDCTRL,
354 CQSPI_REG_CMDCTRL_INPROGRESS_MASK, 1);
356 dev_err(&cqspi->pdev->dev,
357 "Flash command execution timed out.\n");
361 /* Polling QSPI idle status. */
362 return cqspi_wait_idle(cqspi);
365 static int cqspi_command_read(struct spi_nor *nor,
366 const u8 *txbuf, const unsigned n_tx,
367 u8 *rxbuf, const unsigned n_rx)
369 struct cqspi_flash_pdata *f_pdata = nor->priv;
370 struct cqspi_st *cqspi = f_pdata->cqspi;
371 void __iomem *reg_base = cqspi->iobase;
374 unsigned int read_len;
377 if (!n_rx || n_rx > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
378 dev_err(nor->dev, "Invalid input argument, len %d rxbuf 0x%p\n",
383 reg = txbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
385 rdreg = cqspi_calc_rdreg(nor, txbuf[0]);
386 writel(rdreg, reg_base + CQSPI_REG_RD_INSTR);
388 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
390 /* 0 means 1 byte. */
391 reg |= (((n_rx - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
392 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
393 status = cqspi_exec_flash_cmd(cqspi, reg);
397 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
399 /* Put the read value into rx_buf */
400 read_len = (n_rx > 4) ? 4 : n_rx;
401 memcpy(rxbuf, ®, read_len);
405 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
407 read_len = n_rx - read_len;
408 memcpy(rxbuf, ®, read_len);
414 static int cqspi_command_write(struct spi_nor *nor, const u8 opcode,
415 const u8 *txbuf, const unsigned n_tx)
417 struct cqspi_flash_pdata *f_pdata = nor->priv;
418 struct cqspi_st *cqspi = f_pdata->cqspi;
419 void __iomem *reg_base = cqspi->iobase;
425 if (n_tx > CQSPI_STIG_DATA_LEN_MAX || (n_tx && !txbuf)) {
427 "Invalid input argument, cmdlen %d txbuf 0x%p\n",
432 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
434 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
435 reg |= ((n_tx - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
436 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
438 write_len = (n_tx > 4) ? 4 : n_tx;
439 memcpy(&data, txbuf, write_len);
441 writel(data, reg_base + CQSPI_REG_CMDWRITEDATALOWER);
445 write_len = n_tx - 4;
446 memcpy(&data, txbuf, write_len);
447 writel(data, reg_base + CQSPI_REG_CMDWRITEDATAUPPER);
450 ret = cqspi_exec_flash_cmd(cqspi, reg);
454 static int cqspi_command_write_addr(struct spi_nor *nor,
455 const u8 opcode, const unsigned int addr)
457 struct cqspi_flash_pdata *f_pdata = nor->priv;
458 struct cqspi_st *cqspi = f_pdata->cqspi;
459 void __iomem *reg_base = cqspi->iobase;
462 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
463 reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
464 reg |= ((nor->addr_width - 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
465 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
467 writel(addr, reg_base + CQSPI_REG_CMDADDRESS);
469 return cqspi_exec_flash_cmd(cqspi, reg);
472 static int cqspi_read_setup(struct spi_nor *nor)
474 struct cqspi_flash_pdata *f_pdata = nor->priv;
475 struct cqspi_st *cqspi = f_pdata->cqspi;
476 void __iomem *reg_base = cqspi->iobase;
477 unsigned int dummy_clk = 0;
480 reg = nor->read_opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
481 reg |= cqspi_calc_rdreg(nor, nor->read_opcode);
483 /* Setup dummy clock cycles */
484 dummy_clk = nor->read_dummy;
485 if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
486 dummy_clk = CQSPI_DUMMY_CLKS_MAX;
489 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
490 /* Set mode bits high to ensure chip doesn't enter XIP */
491 writel(0xFF, reg_base + CQSPI_REG_MODE_BIT);
493 /* Need to subtract the mode byte (8 clocks). */
494 if (f_pdata->inst_width != CQSPI_INST_TYPE_QUAD)
498 reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
499 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
502 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
504 /* Set address width */
505 reg = readl(reg_base + CQSPI_REG_SIZE);
506 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
507 reg |= (nor->addr_width - 1);
508 writel(reg, reg_base + CQSPI_REG_SIZE);
512 static int cqspi_indirect_read_execute(struct spi_nor *nor, u8 *rxbuf,
513 loff_t from_addr, const size_t n_rx)
515 struct cqspi_flash_pdata *f_pdata = nor->priv;
516 struct cqspi_st *cqspi = f_pdata->cqspi;
517 void __iomem *reg_base = cqspi->iobase;
518 void __iomem *ahb_base = cqspi->ahb_base;
519 unsigned int remaining = n_rx;
520 unsigned int mod_bytes = n_rx % 4;
521 unsigned int bytes_to_read = 0;
522 u8 *rxbuf_end = rxbuf + n_rx;
525 writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR);
526 writel(remaining, reg_base + CQSPI_REG_INDIRECTRDBYTES);
528 /* Clear all interrupts. */
529 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
531 writel(CQSPI_IRQ_MASK_RD, reg_base + CQSPI_REG_IRQMASK);
533 reinit_completion(&cqspi->transfer_complete);
534 writel(CQSPI_REG_INDIRECTRD_START_MASK,
535 reg_base + CQSPI_REG_INDIRECTRD);
537 while (remaining > 0) {
538 if (!wait_for_completion_timeout(&cqspi->transfer_complete,
539 msecs_to_jiffies(CQSPI_READ_TIMEOUT_MS)))
542 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
544 if (ret && bytes_to_read == 0) {
545 dev_err(nor->dev, "Indirect read timeout, no bytes\n");
549 while (bytes_to_read != 0) {
550 unsigned int word_remain = round_down(remaining, 4);
552 bytes_to_read *= cqspi->fifo_width;
553 bytes_to_read = bytes_to_read > remaining ?
554 remaining : bytes_to_read;
555 bytes_to_read = round_down(bytes_to_read, 4);
556 /* Read 4 byte word chunks then single bytes */
558 ioread32_rep(ahb_base, rxbuf,
559 (bytes_to_read / 4));
560 } else if (!word_remain && mod_bytes) {
561 unsigned int temp = ioread32(ahb_base);
563 bytes_to_read = mod_bytes;
564 memcpy(rxbuf, &temp, min((unsigned int)
568 rxbuf += bytes_to_read;
569 remaining -= bytes_to_read;
570 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
574 reinit_completion(&cqspi->transfer_complete);
577 /* Check indirect done status */
578 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTRD,
579 CQSPI_REG_INDIRECTRD_DONE_MASK, 0);
582 "Indirect read completion error (%i)\n", ret);
586 /* Disable interrupt */
587 writel(0, reg_base + CQSPI_REG_IRQMASK);
589 /* Clear indirect completion status */
590 writel(CQSPI_REG_INDIRECTRD_DONE_MASK, reg_base + CQSPI_REG_INDIRECTRD);
595 /* Disable interrupt */
596 writel(0, reg_base + CQSPI_REG_IRQMASK);
598 /* Cancel the indirect read */
599 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
600 reg_base + CQSPI_REG_INDIRECTRD);
604 static int cqspi_write_setup(struct spi_nor *nor)
607 struct cqspi_flash_pdata *f_pdata = nor->priv;
608 struct cqspi_st *cqspi = f_pdata->cqspi;
609 void __iomem *reg_base = cqspi->iobase;
612 reg = nor->program_opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
613 writel(reg, reg_base + CQSPI_REG_WR_INSTR);
614 reg = cqspi_calc_rdreg(nor, nor->program_opcode);
615 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
617 reg = readl(reg_base + CQSPI_REG_SIZE);
618 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
619 reg |= (nor->addr_width - 1);
620 writel(reg, reg_base + CQSPI_REG_SIZE);
624 static int cqspi_indirect_write_execute(struct spi_nor *nor, loff_t to_addr,
625 const u8 *txbuf, const size_t n_tx)
627 const unsigned int page_size = nor->page_size;
628 struct cqspi_flash_pdata *f_pdata = nor->priv;
629 struct cqspi_st *cqspi = f_pdata->cqspi;
630 void __iomem *reg_base = cqspi->iobase;
631 unsigned int remaining = n_tx;
632 unsigned int write_bytes;
635 writel(to_addr, reg_base + CQSPI_REG_INDIRECTWRSTARTADDR);
636 writel(remaining, reg_base + CQSPI_REG_INDIRECTWRBYTES);
638 /* Clear all interrupts. */
639 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
641 writel(CQSPI_IRQ_MASK_WR, reg_base + CQSPI_REG_IRQMASK);
643 reinit_completion(&cqspi->transfer_complete);
644 writel(CQSPI_REG_INDIRECTWR_START_MASK,
645 reg_base + CQSPI_REG_INDIRECTWR);
647 * As per 66AK2G02 TRM SPRUHY8F section 11.15.5.3 Indirect Access
648 * Controller programming sequence, couple of cycles of
649 * QSPI_REF_CLK delay is required for the above bit to
650 * be internally synchronized by the QSPI module. Provide 5
654 ndelay(cqspi->wr_delay);
656 while (remaining > 0) {
657 size_t write_words, mod_bytes;
659 write_bytes = remaining > page_size ? page_size : remaining;
660 write_words = write_bytes / 4;
661 mod_bytes = write_bytes % 4;
662 /* Write 4 bytes at a time then single bytes. */
664 iowrite32_rep(cqspi->ahb_base, txbuf, write_words);
665 txbuf += (write_words * 4);
668 unsigned int temp = 0xFFFFFFFF;
670 memcpy(&temp, txbuf, mod_bytes);
671 iowrite32(temp, cqspi->ahb_base);
675 if (!wait_for_completion_timeout(&cqspi->transfer_complete,
676 msecs_to_jiffies(CQSPI_TIMEOUT_MS))) {
677 dev_err(nor->dev, "Indirect write timeout\n");
682 remaining -= write_bytes;
685 reinit_completion(&cqspi->transfer_complete);
688 /* Check indirect done status */
689 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTWR,
690 CQSPI_REG_INDIRECTWR_DONE_MASK, 0);
693 "Indirect write completion error (%i)\n", ret);
697 /* Disable interrupt. */
698 writel(0, reg_base + CQSPI_REG_IRQMASK);
700 /* Clear indirect completion status */
701 writel(CQSPI_REG_INDIRECTWR_DONE_MASK, reg_base + CQSPI_REG_INDIRECTWR);
703 cqspi_wait_idle(cqspi);
708 /* Disable interrupt. */
709 writel(0, reg_base + CQSPI_REG_IRQMASK);
711 /* Cancel the indirect write */
712 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
713 reg_base + CQSPI_REG_INDIRECTWR);
717 static void cqspi_chipselect(struct spi_nor *nor)
719 struct cqspi_flash_pdata *f_pdata = nor->priv;
720 struct cqspi_st *cqspi = f_pdata->cqspi;
721 void __iomem *reg_base = cqspi->iobase;
722 unsigned int chip_select = f_pdata->cs;
725 reg = readl(reg_base + CQSPI_REG_CONFIG);
726 if (cqspi->is_decoded_cs) {
727 reg |= CQSPI_REG_CONFIG_DECODE_MASK;
729 reg &= ~CQSPI_REG_CONFIG_DECODE_MASK;
731 /* Convert CS if without decoder.
737 chip_select = 0xF & ~(1 << chip_select);
740 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
741 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
742 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
743 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
744 writel(reg, reg_base + CQSPI_REG_CONFIG);
747 static void cqspi_configure_cs_and_sizes(struct spi_nor *nor)
749 struct cqspi_flash_pdata *f_pdata = nor->priv;
750 struct cqspi_st *cqspi = f_pdata->cqspi;
751 void __iomem *iobase = cqspi->iobase;
754 /* configure page size and block size. */
755 reg = readl(iobase + CQSPI_REG_SIZE);
756 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
757 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
758 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
759 reg |= (nor->page_size << CQSPI_REG_SIZE_PAGE_LSB);
760 reg |= (ilog2(nor->mtd.erasesize) << CQSPI_REG_SIZE_BLOCK_LSB);
761 reg |= (nor->addr_width - 1);
762 writel(reg, iobase + CQSPI_REG_SIZE);
764 /* configure the chip select */
765 cqspi_chipselect(nor);
767 /* Store the new configuration of the controller */
768 cqspi->current_page_size = nor->page_size;
769 cqspi->current_erase_size = nor->mtd.erasesize;
770 cqspi->current_addr_width = nor->addr_width;
773 static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz,
774 const unsigned int ns_val)
778 ticks = ref_clk_hz / 1000; /* kHz */
779 ticks = DIV_ROUND_UP(ticks * ns_val, 1000000);
784 static void cqspi_delay(struct spi_nor *nor)
786 struct cqspi_flash_pdata *f_pdata = nor->priv;
787 struct cqspi_st *cqspi = f_pdata->cqspi;
788 void __iomem *iobase = cqspi->iobase;
789 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
790 unsigned int tshsl, tchsh, tslch, tsd2d;
794 /* calculate the number of ref ticks for one sclk tick */
795 tsclk = DIV_ROUND_UP(ref_clk_hz, cqspi->sclk);
797 tshsl = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tshsl_ns);
798 /* this particular value must be at least one sclk */
802 tchsh = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tchsh_ns);
803 tslch = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tslch_ns);
804 tsd2d = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tsd2d_ns);
806 reg = (tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
807 << CQSPI_REG_DELAY_TSHSL_LSB;
808 reg |= (tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
809 << CQSPI_REG_DELAY_TCHSH_LSB;
810 reg |= (tslch & CQSPI_REG_DELAY_TSLCH_MASK)
811 << CQSPI_REG_DELAY_TSLCH_LSB;
812 reg |= (tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
813 << CQSPI_REG_DELAY_TSD2D_LSB;
814 writel(reg, iobase + CQSPI_REG_DELAY);
817 static void cqspi_config_baudrate_div(struct cqspi_st *cqspi)
819 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
820 void __iomem *reg_base = cqspi->iobase;
823 /* Recalculate the baudrate divisor based on QSPI specification. */
824 div = DIV_ROUND_UP(ref_clk_hz, 2 * cqspi->sclk) - 1;
826 reg = readl(reg_base + CQSPI_REG_CONFIG);
827 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
828 reg |= (div & CQSPI_REG_CONFIG_BAUD_MASK) << CQSPI_REG_CONFIG_BAUD_LSB;
829 writel(reg, reg_base + CQSPI_REG_CONFIG);
832 static void cqspi_readdata_capture(struct cqspi_st *cqspi,
834 const unsigned int delay)
836 void __iomem *reg_base = cqspi->iobase;
839 reg = readl(reg_base + CQSPI_REG_READCAPTURE);
842 reg |= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
844 reg &= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
846 reg &= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
847 << CQSPI_REG_READCAPTURE_DELAY_LSB);
849 reg |= (delay & CQSPI_REG_READCAPTURE_DELAY_MASK)
850 << CQSPI_REG_READCAPTURE_DELAY_LSB;
852 writel(reg, reg_base + CQSPI_REG_READCAPTURE);
855 static void cqspi_controller_enable(struct cqspi_st *cqspi, bool enable)
857 void __iomem *reg_base = cqspi->iobase;
860 reg = readl(reg_base + CQSPI_REG_CONFIG);
863 reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
865 reg &= ~CQSPI_REG_CONFIG_ENABLE_MASK;
867 writel(reg, reg_base + CQSPI_REG_CONFIG);
870 static void cqspi_configure(struct spi_nor *nor)
872 struct cqspi_flash_pdata *f_pdata = nor->priv;
873 struct cqspi_st *cqspi = f_pdata->cqspi;
874 const unsigned int sclk = f_pdata->clk_rate;
875 int switch_cs = (cqspi->current_cs != f_pdata->cs);
876 int switch_ck = (cqspi->sclk != sclk);
878 if ((cqspi->current_page_size != nor->page_size) ||
879 (cqspi->current_erase_size != nor->mtd.erasesize) ||
880 (cqspi->current_addr_width != nor->addr_width))
883 if (switch_cs || switch_ck)
884 cqspi_controller_enable(cqspi, 0);
886 /* Switch chip select. */
888 cqspi->current_cs = f_pdata->cs;
889 cqspi_configure_cs_and_sizes(nor);
892 /* Setup baudrate divisor and delays */
895 cqspi_config_baudrate_div(cqspi);
897 cqspi_readdata_capture(cqspi, !cqspi->rclk_en,
898 f_pdata->read_delay);
901 if (switch_cs || switch_ck)
902 cqspi_controller_enable(cqspi, 1);
905 static int cqspi_set_protocol(struct spi_nor *nor, const int read)
907 struct cqspi_flash_pdata *f_pdata = nor->priv;
909 f_pdata->inst_width = CQSPI_INST_TYPE_SINGLE;
910 f_pdata->addr_width = CQSPI_INST_TYPE_SINGLE;
911 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
914 switch (nor->read_proto) {
915 case SNOR_PROTO_1_1_1:
916 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
918 case SNOR_PROTO_1_1_2:
919 f_pdata->data_width = CQSPI_INST_TYPE_DUAL;
921 case SNOR_PROTO_1_1_4:
922 f_pdata->data_width = CQSPI_INST_TYPE_QUAD;
924 case SNOR_PROTO_1_1_8:
925 f_pdata->data_width = CQSPI_INST_TYPE_OCTAL;
932 cqspi_configure(nor);
937 static ssize_t cqspi_write(struct spi_nor *nor, loff_t to,
938 size_t len, const u_char *buf)
940 struct cqspi_flash_pdata *f_pdata = nor->priv;
941 struct cqspi_st *cqspi = f_pdata->cqspi;
944 ret = cqspi_set_protocol(nor, 0);
948 ret = cqspi_write_setup(nor);
952 if (f_pdata->use_direct_mode) {
953 memcpy_toio(cqspi->ahb_base + to, buf, len);
954 ret = cqspi_wait_idle(cqspi);
956 ret = cqspi_indirect_write_execute(nor, to, buf, len);
964 static void cqspi_rx_dma_callback(void *param)
966 struct cqspi_st *cqspi = param;
968 complete(&cqspi->rx_dma_complete);
971 static int cqspi_direct_read_execute(struct spi_nor *nor, u_char *buf,
972 loff_t from, size_t len)
974 struct cqspi_flash_pdata *f_pdata = nor->priv;
975 struct cqspi_st *cqspi = f_pdata->cqspi;
976 enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
977 dma_addr_t dma_src = (dma_addr_t)cqspi->mmap_phys_base + from;
979 struct dma_async_tx_descriptor *tx;
983 if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
984 memcpy_fromio(buf, cqspi->ahb_base + from, len);
988 dma_dst = dma_map_single(nor->dev, buf, len, DMA_FROM_DEVICE);
989 if (dma_mapping_error(nor->dev, dma_dst)) {
990 dev_err(nor->dev, "dma mapping failed\n");
993 tx = dmaengine_prep_dma_memcpy(cqspi->rx_chan, dma_dst, dma_src,
996 dev_err(nor->dev, "device_prep_dma_memcpy error\n");
1001 tx->callback = cqspi_rx_dma_callback;
1002 tx->callback_param = cqspi;
1003 cookie = tx->tx_submit(tx);
1004 reinit_completion(&cqspi->rx_dma_complete);
1006 ret = dma_submit_error(cookie);
1008 dev_err(nor->dev, "dma_submit_error %d\n", cookie);
1013 dma_async_issue_pending(cqspi->rx_chan);
1014 if (!wait_for_completion_timeout(&cqspi->rx_dma_complete,
1015 msecs_to_jiffies(len))) {
1016 dmaengine_terminate_sync(cqspi->rx_chan);
1017 dev_err(nor->dev, "DMA wait_for_completion_timeout\n");
1023 dma_unmap_single(nor->dev, dma_dst, len, DMA_FROM_DEVICE);
1028 static ssize_t cqspi_read(struct spi_nor *nor, loff_t from,
1029 size_t len, u_char *buf)
1031 struct cqspi_flash_pdata *f_pdata = nor->priv;
1034 ret = cqspi_set_protocol(nor, 1);
1038 ret = cqspi_read_setup(nor);
1042 if (f_pdata->use_direct_mode)
1043 ret = cqspi_direct_read_execute(nor, buf, from, len);
1045 ret = cqspi_indirect_read_execute(nor, buf, from, len);
1052 static int cqspi_erase(struct spi_nor *nor, loff_t offs)
1056 ret = cqspi_set_protocol(nor, 0);
1060 /* Send write enable, then erase commands. */
1061 ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
1065 /* Set up command buffer. */
1066 ret = cqspi_command_write_addr(nor, nor->erase_opcode, offs);
1073 static int cqspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
1075 struct cqspi_flash_pdata *f_pdata = nor->priv;
1076 struct cqspi_st *cqspi = f_pdata->cqspi;
1078 mutex_lock(&cqspi->bus_mutex);
1083 static void cqspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
1085 struct cqspi_flash_pdata *f_pdata = nor->priv;
1086 struct cqspi_st *cqspi = f_pdata->cqspi;
1088 mutex_unlock(&cqspi->bus_mutex);
1091 static int cqspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
1095 ret = cqspi_set_protocol(nor, 0);
1097 ret = cqspi_command_read(nor, &opcode, 1, buf, len);
1102 static int cqspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
1106 ret = cqspi_set_protocol(nor, 0);
1108 ret = cqspi_command_write(nor, opcode, buf, len);
1113 static int cqspi_of_get_flash_pdata(struct platform_device *pdev,
1114 struct cqspi_flash_pdata *f_pdata,
1115 struct device_node *np)
1117 if (of_property_read_u32(np, "cdns,read-delay", &f_pdata->read_delay)) {
1118 dev_err(&pdev->dev, "couldn't determine read-delay\n");
1122 if (of_property_read_u32(np, "cdns,tshsl-ns", &f_pdata->tshsl_ns)) {
1123 dev_err(&pdev->dev, "couldn't determine tshsl-ns\n");
1127 if (of_property_read_u32(np, "cdns,tsd2d-ns", &f_pdata->tsd2d_ns)) {
1128 dev_err(&pdev->dev, "couldn't determine tsd2d-ns\n");
1132 if (of_property_read_u32(np, "cdns,tchsh-ns", &f_pdata->tchsh_ns)) {
1133 dev_err(&pdev->dev, "couldn't determine tchsh-ns\n");
1137 if (of_property_read_u32(np, "cdns,tslch-ns", &f_pdata->tslch_ns)) {
1138 dev_err(&pdev->dev, "couldn't determine tslch-ns\n");
1142 if (of_property_read_u32(np, "spi-max-frequency", &f_pdata->clk_rate)) {
1143 dev_err(&pdev->dev, "couldn't determine spi-max-frequency\n");
1150 static int cqspi_of_get_pdata(struct platform_device *pdev)
1152 struct device_node *np = pdev->dev.of_node;
1153 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1155 cqspi->is_decoded_cs = of_property_read_bool(np, "cdns,is-decoded-cs");
1157 if (of_property_read_u32(np, "cdns,fifo-depth", &cqspi->fifo_depth)) {
1158 dev_err(&pdev->dev, "couldn't determine fifo-depth\n");
1162 if (of_property_read_u32(np, "cdns,fifo-width", &cqspi->fifo_width)) {
1163 dev_err(&pdev->dev, "couldn't determine fifo-width\n");
1167 if (of_property_read_u32(np, "cdns,trigger-address",
1168 &cqspi->trigger_address)) {
1169 dev_err(&pdev->dev, "couldn't determine trigger-address\n");
1173 cqspi->rclk_en = of_property_read_bool(np, "cdns,rclk-en");
1178 static void cqspi_controller_init(struct cqspi_st *cqspi)
1182 cqspi_controller_enable(cqspi, 0);
1184 /* Configure the remap address register, no remap */
1185 writel(0, cqspi->iobase + CQSPI_REG_REMAP);
1187 /* Disable all interrupts. */
1188 writel(0, cqspi->iobase + CQSPI_REG_IRQMASK);
1190 /* Configure the SRAM split to 1:1 . */
1191 writel(cqspi->fifo_depth / 2, cqspi->iobase + CQSPI_REG_SRAMPARTITION);
1193 /* Load indirect trigger address. */
1194 writel(cqspi->trigger_address,
1195 cqspi->iobase + CQSPI_REG_INDIRECTTRIGGER);
1197 /* Program read watermark -- 1/2 of the FIFO. */
1198 writel(cqspi->fifo_depth * cqspi->fifo_width / 2,
1199 cqspi->iobase + CQSPI_REG_INDIRECTRDWATERMARK);
1200 /* Program write watermark -- 1/8 of the FIFO. */
1201 writel(cqspi->fifo_depth * cqspi->fifo_width / 8,
1202 cqspi->iobase + CQSPI_REG_INDIRECTWRWATERMARK);
1204 /* Enable Direct Access Controller */
1205 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
1206 reg |= CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL;
1207 writel(reg, cqspi->iobase + CQSPI_REG_CONFIG);
1209 cqspi_controller_enable(cqspi, 1);
1212 static void cqspi_request_mmap_dma(struct cqspi_st *cqspi)
1214 dma_cap_mask_t mask;
1217 dma_cap_set(DMA_MEMCPY, mask);
1219 cqspi->rx_chan = dma_request_chan_by_mask(&mask);
1220 if (IS_ERR(cqspi->rx_chan)) {
1221 dev_err(&cqspi->pdev->dev, "No Rx DMA available\n");
1222 cqspi->rx_chan = NULL;
1224 init_completion(&cqspi->rx_dma_complete);
1227 static int cqspi_setup_flash(struct cqspi_st *cqspi, struct device_node *np)
1229 struct platform_device *pdev = cqspi->pdev;
1230 struct device *dev = &pdev->dev;
1231 const struct cqspi_driver_platdata *ddata;
1232 struct spi_nor_hwcaps hwcaps;
1233 struct cqspi_flash_pdata *f_pdata;
1234 struct spi_nor *nor;
1235 struct mtd_info *mtd;
1239 ddata = of_device_get_match_data(dev);
1241 dev_err(dev, "Couldn't find driver data\n");
1244 hwcaps.mask = ddata->hwcaps_mask;
1246 /* Get flash device data */
1247 for_each_available_child_of_node(dev->of_node, np) {
1248 ret = of_property_read_u32(np, "reg", &cs);
1250 dev_err(dev, "Couldn't determine chip select.\n");
1254 if (cs >= CQSPI_MAX_CHIPSELECT) {
1256 dev_err(dev, "Chip select %d out of range.\n", cs);
1260 f_pdata = &cqspi->f_pdata[cs];
1261 f_pdata->cqspi = cqspi;
1264 ret = cqspi_of_get_flash_pdata(pdev, f_pdata, np);
1268 nor = &f_pdata->nor;
1274 spi_nor_set_flash_node(nor, np);
1275 nor->priv = f_pdata;
1277 nor->read_reg = cqspi_read_reg;
1278 nor->write_reg = cqspi_write_reg;
1279 nor->read = cqspi_read;
1280 nor->write = cqspi_write;
1281 nor->erase = cqspi_erase;
1282 nor->prepare = cqspi_prep;
1283 nor->unprepare = cqspi_unprep;
1285 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d",
1292 ret = spi_nor_scan(nor, NULL, &hwcaps);
1296 ret = mtd_device_register(mtd, NULL, 0);
1300 f_pdata->registered = true;
1302 if (mtd->size <= cqspi->ahb_size) {
1303 f_pdata->use_direct_mode = true;
1304 dev_dbg(nor->dev, "using direct mode for %s\n",
1307 if (!cqspi->rx_chan)
1308 cqspi_request_mmap_dma(cqspi);
1315 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1316 if (cqspi->f_pdata[i].registered)
1317 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1321 static int cqspi_probe(struct platform_device *pdev)
1323 struct device_node *np = pdev->dev.of_node;
1324 struct device *dev = &pdev->dev;
1325 struct cqspi_st *cqspi;
1326 struct resource *res;
1327 struct resource *res_ahb;
1328 const struct cqspi_driver_platdata *ddata;
1332 cqspi = devm_kzalloc(dev, sizeof(*cqspi), GFP_KERNEL);
1336 mutex_init(&cqspi->bus_mutex);
1338 platform_set_drvdata(pdev, cqspi);
1340 /* Obtain configuration from OF. */
1341 ret = cqspi_of_get_pdata(pdev);
1343 dev_err(dev, "Cannot get mandatory OF data.\n");
1347 /* Obtain QSPI clock. */
1348 cqspi->clk = devm_clk_get(dev, NULL);
1349 if (IS_ERR(cqspi->clk)) {
1350 dev_err(dev, "Cannot claim QSPI clock.\n");
1351 return PTR_ERR(cqspi->clk);
1354 /* Obtain and remap controller address. */
1355 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1356 cqspi->iobase = devm_ioremap_resource(dev, res);
1357 if (IS_ERR(cqspi->iobase)) {
1358 dev_err(dev, "Cannot remap controller address.\n");
1359 return PTR_ERR(cqspi->iobase);
1362 /* Obtain and remap AHB address. */
1363 res_ahb = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1364 cqspi->ahb_base = devm_ioremap_resource(dev, res_ahb);
1365 if (IS_ERR(cqspi->ahb_base)) {
1366 dev_err(dev, "Cannot remap AHB address.\n");
1367 return PTR_ERR(cqspi->ahb_base);
1369 cqspi->mmap_phys_base = (dma_addr_t)res_ahb->start;
1370 cqspi->ahb_size = resource_size(res_ahb);
1372 init_completion(&cqspi->transfer_complete);
1374 /* Obtain IRQ line. */
1375 irq = platform_get_irq(pdev, 0);
1377 dev_err(dev, "Cannot obtain IRQ.\n");
1381 pm_runtime_enable(dev);
1382 ret = pm_runtime_get_sync(dev);
1384 pm_runtime_put_noidle(dev);
1388 ret = clk_prepare_enable(cqspi->clk);
1390 dev_err(dev, "Cannot enable QSPI clock.\n");
1391 goto probe_clk_failed;
1394 cqspi->master_ref_clk_hz = clk_get_rate(cqspi->clk);
1395 ddata = of_device_get_match_data(dev);
1396 if (ddata && (ddata->quirks & CQSPI_NEEDS_WR_DELAY))
1397 cqspi->wr_delay = 5 * DIV_ROUND_UP(NSEC_PER_SEC,
1398 cqspi->master_ref_clk_hz);
1400 ret = devm_request_irq(dev, irq, cqspi_irq_handler, 0,
1403 dev_err(dev, "Cannot request IRQ.\n");
1404 goto probe_irq_failed;
1407 cqspi_wait_idle(cqspi);
1408 cqspi_controller_init(cqspi);
1409 cqspi->current_cs = -1;
1412 ret = cqspi_setup_flash(cqspi, np);
1414 dev_err(dev, "Cadence QSPI NOR probe failed %d\n", ret);
1415 goto probe_setup_failed;
1420 cqspi_controller_enable(cqspi, 0);
1422 clk_disable_unprepare(cqspi->clk);
1424 pm_runtime_put_sync(dev);
1425 pm_runtime_disable(dev);
1429 static int cqspi_remove(struct platform_device *pdev)
1431 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1434 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1435 if (cqspi->f_pdata[i].registered)
1436 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1438 cqspi_controller_enable(cqspi, 0);
1441 dma_release_channel(cqspi->rx_chan);
1443 clk_disable_unprepare(cqspi->clk);
1445 pm_runtime_put_sync(&pdev->dev);
1446 pm_runtime_disable(&pdev->dev);
1451 #ifdef CONFIG_PM_SLEEP
1452 static int cqspi_suspend(struct device *dev)
1454 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1456 cqspi_controller_enable(cqspi, 0);
1460 static int cqspi_resume(struct device *dev)
1462 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1464 cqspi_controller_enable(cqspi, 1);
1468 static const struct dev_pm_ops cqspi__dev_pm_ops = {
1469 .suspend = cqspi_suspend,
1470 .resume = cqspi_resume,
1473 #define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
1475 #define CQSPI_DEV_PM_OPS NULL
1478 static const struct cqspi_driver_platdata cdns_qspi = {
1479 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK,
1482 static const struct cqspi_driver_platdata k2g_qspi = {
1483 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK,
1484 .quirks = CQSPI_NEEDS_WR_DELAY,
1487 static const struct cqspi_driver_platdata am654_ospi = {
1488 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK | SNOR_HWCAPS_READ_1_1_8,
1489 .quirks = CQSPI_NEEDS_WR_DELAY,
1492 static const struct of_device_id cqspi_dt_ids[] = {
1494 .compatible = "cdns,qspi-nor",
1498 .compatible = "ti,k2g-qspi",
1502 .compatible = "ti,am654-ospi",
1503 .data = &am654_ospi,
1505 { /* end of table */ }
1508 MODULE_DEVICE_TABLE(of, cqspi_dt_ids);
1510 static struct platform_driver cqspi_platform_driver = {
1511 .probe = cqspi_probe,
1512 .remove = cqspi_remove,
1515 .pm = CQSPI_DEV_PM_OPS,
1516 .of_match_table = cqspi_dt_ids,
1520 module_platform_driver(cqspi_platform_driver);
1522 MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
1523 MODULE_LICENSE("GPL v2");
1524 MODULE_ALIAS("platform:" CQSPI_NAME);
1525 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
1526 MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");