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/dma-mapping.h>
22 #include <linux/dmaengine.h>
23 #include <linux/err.h>
24 #include <linux/errno.h>
25 #include <linux/interrupt.h>
27 #include <linux/jiffies.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/mtd/spi-nor.h>
33 #include <linux/of_device.h>
35 #include <linux/platform_device.h>
36 #include <linux/pm_runtime.h>
37 #include <linux/sched.h>
38 #include <linux/spi/spi.h>
39 #include <linux/timer.h>
41 #define CQSPI_NAME "cadence-qspi"
42 #define CQSPI_MAX_CHIPSELECT 16
45 #define CQSPI_NEEDS_WR_DELAY BIT(0)
47 /* Capabilities mask */
48 #define CQSPI_BASE_HWCAPS_MASK \
49 (SNOR_HWCAPS_READ | SNOR_HWCAPS_READ_FAST | \
50 SNOR_HWCAPS_READ_1_1_2 | SNOR_HWCAPS_READ_1_1_4 | \
55 struct cqspi_flash_pdata {
57 struct cqspi_st *cqspi;
73 struct platform_device *pdev;
79 void __iomem *ahb_base;
80 resource_size_t ahb_size;
81 struct completion transfer_complete;
82 struct mutex bus_mutex;
84 struct dma_chan *rx_chan;
85 struct completion rx_dma_complete;
86 dma_addr_t mmap_phys_base;
89 int current_page_size;
90 int current_erase_size;
91 int current_addr_width;
92 unsigned long master_ref_clk_hz;
99 struct cqspi_flash_pdata f_pdata[CQSPI_MAX_CHIPSELECT];
102 struct cqspi_driver_platdata {
107 /* Operation timeout value */
108 #define CQSPI_TIMEOUT_MS 500
109 #define CQSPI_READ_TIMEOUT_MS 10
111 /* Instruction type */
112 #define CQSPI_INST_TYPE_SINGLE 0
113 #define CQSPI_INST_TYPE_DUAL 1
114 #define CQSPI_INST_TYPE_QUAD 2
115 #define CQSPI_INST_TYPE_OCTAL 3
117 #define CQSPI_DUMMY_CLKS_PER_BYTE 8
118 #define CQSPI_DUMMY_BYTES_MAX 4
119 #define CQSPI_DUMMY_CLKS_MAX 31
121 #define CQSPI_STIG_DATA_LEN_MAX 8
124 #define CQSPI_REG_CONFIG 0x00
125 #define CQSPI_REG_CONFIG_ENABLE_MASK BIT(0)
126 #define CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL BIT(7)
127 #define CQSPI_REG_CONFIG_DECODE_MASK BIT(9)
128 #define CQSPI_REG_CONFIG_CHIPSELECT_LSB 10
129 #define CQSPI_REG_CONFIG_DMA_MASK BIT(15)
130 #define CQSPI_REG_CONFIG_BAUD_LSB 19
131 #define CQSPI_REG_CONFIG_IDLE_LSB 31
132 #define CQSPI_REG_CONFIG_CHIPSELECT_MASK 0xF
133 #define CQSPI_REG_CONFIG_BAUD_MASK 0xF
135 #define CQSPI_REG_RD_INSTR 0x04
136 #define CQSPI_REG_RD_INSTR_OPCODE_LSB 0
137 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB 8
138 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB 12
139 #define CQSPI_REG_RD_INSTR_TYPE_DATA_LSB 16
140 #define CQSPI_REG_RD_INSTR_MODE_EN_LSB 20
141 #define CQSPI_REG_RD_INSTR_DUMMY_LSB 24
142 #define CQSPI_REG_RD_INSTR_TYPE_INSTR_MASK 0x3
143 #define CQSPI_REG_RD_INSTR_TYPE_ADDR_MASK 0x3
144 #define CQSPI_REG_RD_INSTR_TYPE_DATA_MASK 0x3
145 #define CQSPI_REG_RD_INSTR_DUMMY_MASK 0x1F
147 #define CQSPI_REG_WR_INSTR 0x08
148 #define CQSPI_REG_WR_INSTR_OPCODE_LSB 0
149 #define CQSPI_REG_WR_INSTR_TYPE_ADDR_LSB 12
150 #define CQSPI_REG_WR_INSTR_TYPE_DATA_LSB 16
152 #define CQSPI_REG_DELAY 0x0C
153 #define CQSPI_REG_DELAY_TSLCH_LSB 0
154 #define CQSPI_REG_DELAY_TCHSH_LSB 8
155 #define CQSPI_REG_DELAY_TSD2D_LSB 16
156 #define CQSPI_REG_DELAY_TSHSL_LSB 24
157 #define CQSPI_REG_DELAY_TSLCH_MASK 0xFF
158 #define CQSPI_REG_DELAY_TCHSH_MASK 0xFF
159 #define CQSPI_REG_DELAY_TSD2D_MASK 0xFF
160 #define CQSPI_REG_DELAY_TSHSL_MASK 0xFF
162 #define CQSPI_REG_READCAPTURE 0x10
163 #define CQSPI_REG_READCAPTURE_BYPASS_LSB 0
164 #define CQSPI_REG_READCAPTURE_DELAY_LSB 1
165 #define CQSPI_REG_READCAPTURE_DELAY_MASK 0xF
167 #define CQSPI_REG_SIZE 0x14
168 #define CQSPI_REG_SIZE_ADDRESS_LSB 0
169 #define CQSPI_REG_SIZE_PAGE_LSB 4
170 #define CQSPI_REG_SIZE_BLOCK_LSB 16
171 #define CQSPI_REG_SIZE_ADDRESS_MASK 0xF
172 #define CQSPI_REG_SIZE_PAGE_MASK 0xFFF
173 #define CQSPI_REG_SIZE_BLOCK_MASK 0x3F
175 #define CQSPI_REG_SRAMPARTITION 0x18
176 #define CQSPI_REG_INDIRECTTRIGGER 0x1C
178 #define CQSPI_REG_DMA 0x20
179 #define CQSPI_REG_DMA_SINGLE_LSB 0
180 #define CQSPI_REG_DMA_BURST_LSB 8
181 #define CQSPI_REG_DMA_SINGLE_MASK 0xFF
182 #define CQSPI_REG_DMA_BURST_MASK 0xFF
184 #define CQSPI_REG_REMAP 0x24
185 #define CQSPI_REG_MODE_BIT 0x28
187 #define CQSPI_REG_SDRAMLEVEL 0x2C
188 #define CQSPI_REG_SDRAMLEVEL_RD_LSB 0
189 #define CQSPI_REG_SDRAMLEVEL_WR_LSB 16
190 #define CQSPI_REG_SDRAMLEVEL_RD_MASK 0xFFFF
191 #define CQSPI_REG_SDRAMLEVEL_WR_MASK 0xFFFF
193 #define CQSPI_REG_IRQSTATUS 0x40
194 #define CQSPI_REG_IRQMASK 0x44
196 #define CQSPI_REG_INDIRECTRD 0x60
197 #define CQSPI_REG_INDIRECTRD_START_MASK BIT(0)
198 #define CQSPI_REG_INDIRECTRD_CANCEL_MASK BIT(1)
199 #define CQSPI_REG_INDIRECTRD_DONE_MASK BIT(5)
201 #define CQSPI_REG_INDIRECTRDWATERMARK 0x64
202 #define CQSPI_REG_INDIRECTRDSTARTADDR 0x68
203 #define CQSPI_REG_INDIRECTRDBYTES 0x6C
205 #define CQSPI_REG_CMDCTRL 0x90
206 #define CQSPI_REG_CMDCTRL_EXECUTE_MASK BIT(0)
207 #define CQSPI_REG_CMDCTRL_INPROGRESS_MASK BIT(1)
208 #define CQSPI_REG_CMDCTRL_WR_BYTES_LSB 12
209 #define CQSPI_REG_CMDCTRL_WR_EN_LSB 15
210 #define CQSPI_REG_CMDCTRL_ADD_BYTES_LSB 16
211 #define CQSPI_REG_CMDCTRL_ADDR_EN_LSB 19
212 #define CQSPI_REG_CMDCTRL_RD_BYTES_LSB 20
213 #define CQSPI_REG_CMDCTRL_RD_EN_LSB 23
214 #define CQSPI_REG_CMDCTRL_OPCODE_LSB 24
215 #define CQSPI_REG_CMDCTRL_WR_BYTES_MASK 0x7
216 #define CQSPI_REG_CMDCTRL_ADD_BYTES_MASK 0x3
217 #define CQSPI_REG_CMDCTRL_RD_BYTES_MASK 0x7
219 #define CQSPI_REG_INDIRECTWR 0x70
220 #define CQSPI_REG_INDIRECTWR_START_MASK BIT(0)
221 #define CQSPI_REG_INDIRECTWR_CANCEL_MASK BIT(1)
222 #define CQSPI_REG_INDIRECTWR_DONE_MASK BIT(5)
224 #define CQSPI_REG_INDIRECTWRWATERMARK 0x74
225 #define CQSPI_REG_INDIRECTWRSTARTADDR 0x78
226 #define CQSPI_REG_INDIRECTWRBYTES 0x7C
228 #define CQSPI_REG_CMDADDRESS 0x94
229 #define CQSPI_REG_CMDREADDATALOWER 0xA0
230 #define CQSPI_REG_CMDREADDATAUPPER 0xA4
231 #define CQSPI_REG_CMDWRITEDATALOWER 0xA8
232 #define CQSPI_REG_CMDWRITEDATAUPPER 0xAC
234 /* Interrupt status bits */
235 #define CQSPI_REG_IRQ_MODE_ERR BIT(0)
236 #define CQSPI_REG_IRQ_UNDERFLOW BIT(1)
237 #define CQSPI_REG_IRQ_IND_COMP BIT(2)
238 #define CQSPI_REG_IRQ_IND_RD_REJECT BIT(3)
239 #define CQSPI_REG_IRQ_WR_PROTECTED_ERR BIT(4)
240 #define CQSPI_REG_IRQ_ILLEGAL_AHB_ERR BIT(5)
241 #define CQSPI_REG_IRQ_WATERMARK BIT(6)
242 #define CQSPI_REG_IRQ_IND_SRAM_FULL BIT(12)
244 #define CQSPI_IRQ_MASK_RD (CQSPI_REG_IRQ_WATERMARK | \
245 CQSPI_REG_IRQ_IND_SRAM_FULL | \
246 CQSPI_REG_IRQ_IND_COMP)
248 #define CQSPI_IRQ_MASK_WR (CQSPI_REG_IRQ_IND_COMP | \
249 CQSPI_REG_IRQ_WATERMARK | \
250 CQSPI_REG_IRQ_UNDERFLOW)
252 #define CQSPI_IRQ_STATUS_MASK 0x1FFFF
254 static int cqspi_wait_for_bit(void __iomem *reg, const u32 mask, bool clear)
256 unsigned long end = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
268 if (time_after(jiffies, end))
273 static bool cqspi_is_idle(struct cqspi_st *cqspi)
275 u32 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
277 return reg & (1 << CQSPI_REG_CONFIG_IDLE_LSB);
280 static u32 cqspi_get_rd_sram_level(struct cqspi_st *cqspi)
282 u32 reg = readl(cqspi->iobase + CQSPI_REG_SDRAMLEVEL);
284 reg >>= CQSPI_REG_SDRAMLEVEL_RD_LSB;
285 return reg & CQSPI_REG_SDRAMLEVEL_RD_MASK;
288 static irqreturn_t cqspi_irq_handler(int this_irq, void *dev)
290 struct cqspi_st *cqspi = dev;
291 unsigned int irq_status;
293 /* Read interrupt status */
294 irq_status = readl(cqspi->iobase + CQSPI_REG_IRQSTATUS);
296 /* Clear interrupt */
297 writel(irq_status, cqspi->iobase + CQSPI_REG_IRQSTATUS);
299 irq_status &= CQSPI_IRQ_MASK_RD | CQSPI_IRQ_MASK_WR;
302 complete(&cqspi->transfer_complete);
307 static unsigned int cqspi_calc_rdreg(struct spi_nor *nor, const u8 opcode)
309 struct cqspi_flash_pdata *f_pdata = nor->priv;
312 rdreg |= f_pdata->inst_width << CQSPI_REG_RD_INSTR_TYPE_INSTR_LSB;
313 rdreg |= f_pdata->addr_width << CQSPI_REG_RD_INSTR_TYPE_ADDR_LSB;
314 rdreg |= f_pdata->data_width << CQSPI_REG_RD_INSTR_TYPE_DATA_LSB;
319 static int cqspi_wait_idle(struct cqspi_st *cqspi)
321 const unsigned int poll_idle_retry = 3;
322 unsigned int count = 0;
323 unsigned long timeout;
325 timeout = jiffies + msecs_to_jiffies(CQSPI_TIMEOUT_MS);
328 * Read few times in succession to ensure the controller
329 * is indeed idle, that is, the bit does not transition
332 if (cqspi_is_idle(cqspi))
337 if (count >= poll_idle_retry)
340 if (time_after(jiffies, timeout)) {
341 /* Timeout, in busy mode. */
342 dev_err(&cqspi->pdev->dev,
343 "QSPI is still busy after %dms timeout.\n",
352 static int cqspi_exec_flash_cmd(struct cqspi_st *cqspi, unsigned int reg)
354 void __iomem *reg_base = cqspi->iobase;
357 /* Write the CMDCTRL without start execution. */
358 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
360 reg |= CQSPI_REG_CMDCTRL_EXECUTE_MASK;
361 writel(reg, reg_base + CQSPI_REG_CMDCTRL);
363 /* Polling for completion. */
364 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_CMDCTRL,
365 CQSPI_REG_CMDCTRL_INPROGRESS_MASK, 1);
367 dev_err(&cqspi->pdev->dev,
368 "Flash command execution timed out.\n");
372 /* Polling QSPI idle status. */
373 return cqspi_wait_idle(cqspi);
376 static int cqspi_command_read(struct spi_nor *nor,
377 const u8 *txbuf, const unsigned n_tx,
378 u8 *rxbuf, const unsigned n_rx)
380 struct cqspi_flash_pdata *f_pdata = nor->priv;
381 struct cqspi_st *cqspi = f_pdata->cqspi;
382 void __iomem *reg_base = cqspi->iobase;
385 unsigned int read_len;
388 if (!n_rx || n_rx > CQSPI_STIG_DATA_LEN_MAX || !rxbuf) {
389 dev_err(nor->dev, "Invalid input argument, len %d rxbuf 0x%p\n",
394 reg = txbuf[0] << CQSPI_REG_CMDCTRL_OPCODE_LSB;
396 rdreg = cqspi_calc_rdreg(nor, txbuf[0]);
397 writel(rdreg, reg_base + CQSPI_REG_RD_INSTR);
399 reg |= (0x1 << CQSPI_REG_CMDCTRL_RD_EN_LSB);
401 /* 0 means 1 byte. */
402 reg |= (((n_rx - 1) & CQSPI_REG_CMDCTRL_RD_BYTES_MASK)
403 << CQSPI_REG_CMDCTRL_RD_BYTES_LSB);
404 status = cqspi_exec_flash_cmd(cqspi, reg);
408 reg = readl(reg_base + CQSPI_REG_CMDREADDATALOWER);
410 /* Put the read value into rx_buf */
411 read_len = (n_rx > 4) ? 4 : n_rx;
412 memcpy(rxbuf, ®, read_len);
416 reg = readl(reg_base + CQSPI_REG_CMDREADDATAUPPER);
418 read_len = n_rx - read_len;
419 memcpy(rxbuf, ®, read_len);
425 static int cqspi_command_write(struct spi_nor *nor, const u8 opcode,
426 const u8 *txbuf, const unsigned n_tx)
428 struct cqspi_flash_pdata *f_pdata = nor->priv;
429 struct cqspi_st *cqspi = f_pdata->cqspi;
430 void __iomem *reg_base = cqspi->iobase;
436 if (n_tx > CQSPI_STIG_DATA_LEN_MAX || (n_tx && !txbuf)) {
438 "Invalid input argument, cmdlen %d txbuf 0x%p\n",
443 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
445 reg |= (0x1 << CQSPI_REG_CMDCTRL_WR_EN_LSB);
446 reg |= ((n_tx - 1) & CQSPI_REG_CMDCTRL_WR_BYTES_MASK)
447 << CQSPI_REG_CMDCTRL_WR_BYTES_LSB;
449 write_len = (n_tx > 4) ? 4 : n_tx;
450 memcpy(&data, txbuf, write_len);
452 writel(data, reg_base + CQSPI_REG_CMDWRITEDATALOWER);
456 write_len = n_tx - 4;
457 memcpy(&data, txbuf, write_len);
458 writel(data, reg_base + CQSPI_REG_CMDWRITEDATAUPPER);
461 ret = cqspi_exec_flash_cmd(cqspi, reg);
465 static int cqspi_command_write_addr(struct spi_nor *nor,
466 const u8 opcode, const unsigned int addr)
468 struct cqspi_flash_pdata *f_pdata = nor->priv;
469 struct cqspi_st *cqspi = f_pdata->cqspi;
470 void __iomem *reg_base = cqspi->iobase;
473 reg = opcode << CQSPI_REG_CMDCTRL_OPCODE_LSB;
474 reg |= (0x1 << CQSPI_REG_CMDCTRL_ADDR_EN_LSB);
475 reg |= ((nor->addr_width - 1) & CQSPI_REG_CMDCTRL_ADD_BYTES_MASK)
476 << CQSPI_REG_CMDCTRL_ADD_BYTES_LSB;
478 writel(addr, reg_base + CQSPI_REG_CMDADDRESS);
480 return cqspi_exec_flash_cmd(cqspi, reg);
483 static int cqspi_read_setup(struct spi_nor *nor)
485 struct cqspi_flash_pdata *f_pdata = nor->priv;
486 struct cqspi_st *cqspi = f_pdata->cqspi;
487 void __iomem *reg_base = cqspi->iobase;
488 unsigned int dummy_clk = 0;
491 reg = nor->read_opcode << CQSPI_REG_RD_INSTR_OPCODE_LSB;
492 reg |= cqspi_calc_rdreg(nor, nor->read_opcode);
494 /* Setup dummy clock cycles */
495 dummy_clk = nor->read_dummy;
496 if (dummy_clk > CQSPI_DUMMY_CLKS_MAX)
497 dummy_clk = CQSPI_DUMMY_CLKS_MAX;
500 reg |= (1 << CQSPI_REG_RD_INSTR_MODE_EN_LSB);
501 /* Set mode bits high to ensure chip doesn't enter XIP */
502 writel(0xFF, reg_base + CQSPI_REG_MODE_BIT);
504 /* Need to subtract the mode byte (8 clocks). */
505 if (f_pdata->inst_width != CQSPI_INST_TYPE_QUAD)
509 reg |= (dummy_clk & CQSPI_REG_RD_INSTR_DUMMY_MASK)
510 << CQSPI_REG_RD_INSTR_DUMMY_LSB;
513 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
515 /* Set address width */
516 reg = readl(reg_base + CQSPI_REG_SIZE);
517 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
518 reg |= (nor->addr_width - 1);
519 writel(reg, reg_base + CQSPI_REG_SIZE);
523 static int cqspi_indirect_read_execute(struct spi_nor *nor, u8 *rxbuf,
524 loff_t from_addr, const size_t n_rx)
526 struct cqspi_flash_pdata *f_pdata = nor->priv;
527 struct cqspi_st *cqspi = f_pdata->cqspi;
528 void __iomem *reg_base = cqspi->iobase;
529 void __iomem *ahb_base = cqspi->ahb_base;
530 unsigned int remaining = n_rx;
531 unsigned int mod_bytes = n_rx % 4;
532 unsigned int bytes_to_read = 0;
533 u8 *rxbuf_end = rxbuf + n_rx;
536 writel(from_addr, reg_base + CQSPI_REG_INDIRECTRDSTARTADDR);
537 writel(remaining, reg_base + CQSPI_REG_INDIRECTRDBYTES);
539 /* Clear all interrupts. */
540 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
542 writel(CQSPI_IRQ_MASK_RD, reg_base + CQSPI_REG_IRQMASK);
544 reinit_completion(&cqspi->transfer_complete);
545 writel(CQSPI_REG_INDIRECTRD_START_MASK,
546 reg_base + CQSPI_REG_INDIRECTRD);
548 while (remaining > 0) {
549 if (!wait_for_completion_timeout(&cqspi->transfer_complete,
550 msecs_to_jiffies(CQSPI_READ_TIMEOUT_MS)))
553 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
555 if (ret && bytes_to_read == 0) {
556 dev_err(nor->dev, "Indirect read timeout, no bytes\n");
560 while (bytes_to_read != 0) {
561 unsigned int word_remain = round_down(remaining, 4);
563 bytes_to_read *= cqspi->fifo_width;
564 bytes_to_read = bytes_to_read > remaining ?
565 remaining : bytes_to_read;
566 bytes_to_read = round_down(bytes_to_read, 4);
567 /* Read 4 byte word chunks then single bytes */
569 ioread32_rep(ahb_base, rxbuf,
570 (bytes_to_read / 4));
571 } else if (!word_remain && mod_bytes) {
572 unsigned int temp = ioread32(ahb_base);
574 bytes_to_read = mod_bytes;
575 memcpy(rxbuf, &temp, min((unsigned int)
579 rxbuf += bytes_to_read;
580 remaining -= bytes_to_read;
581 bytes_to_read = cqspi_get_rd_sram_level(cqspi);
585 reinit_completion(&cqspi->transfer_complete);
588 /* Check indirect done status */
589 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTRD,
590 CQSPI_REG_INDIRECTRD_DONE_MASK, 0);
593 "Indirect read completion error (%i)\n", ret);
597 /* Disable interrupt */
598 writel(0, reg_base + CQSPI_REG_IRQMASK);
600 /* Clear indirect completion status */
601 writel(CQSPI_REG_INDIRECTRD_DONE_MASK, reg_base + CQSPI_REG_INDIRECTRD);
606 /* Disable interrupt */
607 writel(0, reg_base + CQSPI_REG_IRQMASK);
609 /* Cancel the indirect read */
610 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
611 reg_base + CQSPI_REG_INDIRECTRD);
615 static int cqspi_write_setup(struct spi_nor *nor)
618 struct cqspi_flash_pdata *f_pdata = nor->priv;
619 struct cqspi_st *cqspi = f_pdata->cqspi;
620 void __iomem *reg_base = cqspi->iobase;
623 reg = nor->program_opcode << CQSPI_REG_WR_INSTR_OPCODE_LSB;
624 writel(reg, reg_base + CQSPI_REG_WR_INSTR);
625 reg = cqspi_calc_rdreg(nor, nor->program_opcode);
626 writel(reg, reg_base + CQSPI_REG_RD_INSTR);
628 reg = readl(reg_base + CQSPI_REG_SIZE);
629 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
630 reg |= (nor->addr_width - 1);
631 writel(reg, reg_base + CQSPI_REG_SIZE);
635 static int cqspi_indirect_write_execute(struct spi_nor *nor, loff_t to_addr,
636 const u8 *txbuf, const size_t n_tx)
638 const unsigned int page_size = nor->page_size;
639 struct cqspi_flash_pdata *f_pdata = nor->priv;
640 struct cqspi_st *cqspi = f_pdata->cqspi;
641 void __iomem *reg_base = cqspi->iobase;
642 unsigned int remaining = n_tx;
643 unsigned int write_bytes;
646 writel(to_addr, reg_base + CQSPI_REG_INDIRECTWRSTARTADDR);
647 writel(remaining, reg_base + CQSPI_REG_INDIRECTWRBYTES);
649 /* Clear all interrupts. */
650 writel(CQSPI_IRQ_STATUS_MASK, reg_base + CQSPI_REG_IRQSTATUS);
652 writel(CQSPI_IRQ_MASK_WR, reg_base + CQSPI_REG_IRQMASK);
654 reinit_completion(&cqspi->transfer_complete);
655 writel(CQSPI_REG_INDIRECTWR_START_MASK,
656 reg_base + CQSPI_REG_INDIRECTWR);
658 * As per 66AK2G02 TRM SPRUHY8F section 11.15.5.3 Indirect Access
659 * Controller programming sequence, couple of cycles of
660 * QSPI_REF_CLK delay is required for the above bit to
661 * be internally synchronized by the QSPI module. Provide 5
665 ndelay(cqspi->wr_delay);
667 while (remaining > 0) {
668 size_t write_words, mod_bytes;
670 write_bytes = remaining > page_size ? page_size : remaining;
671 write_words = write_bytes / 4;
672 mod_bytes = write_bytes % 4;
673 /* Write 4 bytes at a time then single bytes. */
675 iowrite32_rep(cqspi->ahb_base, txbuf, write_words);
676 txbuf += (write_words * 4);
679 unsigned int temp = 0xFFFFFFFF;
681 memcpy(&temp, txbuf, mod_bytes);
682 iowrite32(temp, cqspi->ahb_base);
686 if (!wait_for_completion_timeout(&cqspi->transfer_complete,
687 msecs_to_jiffies(CQSPI_TIMEOUT_MS))) {
688 dev_err(nor->dev, "Indirect write timeout\n");
693 remaining -= write_bytes;
696 reinit_completion(&cqspi->transfer_complete);
699 /* Check indirect done status */
700 ret = cqspi_wait_for_bit(reg_base + CQSPI_REG_INDIRECTWR,
701 CQSPI_REG_INDIRECTWR_DONE_MASK, 0);
704 "Indirect write completion error (%i)\n", ret);
708 /* Disable interrupt. */
709 writel(0, reg_base + CQSPI_REG_IRQMASK);
711 /* Clear indirect completion status */
712 writel(CQSPI_REG_INDIRECTWR_DONE_MASK, reg_base + CQSPI_REG_INDIRECTWR);
714 cqspi_wait_idle(cqspi);
719 /* Disable interrupt. */
720 writel(0, reg_base + CQSPI_REG_IRQMASK);
722 /* Cancel the indirect write */
723 writel(CQSPI_REG_INDIRECTWR_CANCEL_MASK,
724 reg_base + CQSPI_REG_INDIRECTWR);
728 static void cqspi_chipselect(struct spi_nor *nor)
730 struct cqspi_flash_pdata *f_pdata = nor->priv;
731 struct cqspi_st *cqspi = f_pdata->cqspi;
732 void __iomem *reg_base = cqspi->iobase;
733 unsigned int chip_select = f_pdata->cs;
736 reg = readl(reg_base + CQSPI_REG_CONFIG);
737 if (cqspi->is_decoded_cs) {
738 reg |= CQSPI_REG_CONFIG_DECODE_MASK;
740 reg &= ~CQSPI_REG_CONFIG_DECODE_MASK;
742 /* Convert CS if without decoder.
748 chip_select = 0xF & ~(1 << chip_select);
751 reg &= ~(CQSPI_REG_CONFIG_CHIPSELECT_MASK
752 << CQSPI_REG_CONFIG_CHIPSELECT_LSB);
753 reg |= (chip_select & CQSPI_REG_CONFIG_CHIPSELECT_MASK)
754 << CQSPI_REG_CONFIG_CHIPSELECT_LSB;
755 writel(reg, reg_base + CQSPI_REG_CONFIG);
758 static void cqspi_configure_cs_and_sizes(struct spi_nor *nor)
760 struct cqspi_flash_pdata *f_pdata = nor->priv;
761 struct cqspi_st *cqspi = f_pdata->cqspi;
762 void __iomem *iobase = cqspi->iobase;
765 /* configure page size and block size. */
766 reg = readl(iobase + CQSPI_REG_SIZE);
767 reg &= ~(CQSPI_REG_SIZE_PAGE_MASK << CQSPI_REG_SIZE_PAGE_LSB);
768 reg &= ~(CQSPI_REG_SIZE_BLOCK_MASK << CQSPI_REG_SIZE_BLOCK_LSB);
769 reg &= ~CQSPI_REG_SIZE_ADDRESS_MASK;
770 reg |= (nor->page_size << CQSPI_REG_SIZE_PAGE_LSB);
771 reg |= (ilog2(nor->mtd.erasesize) << CQSPI_REG_SIZE_BLOCK_LSB);
772 reg |= (nor->addr_width - 1);
773 writel(reg, iobase + CQSPI_REG_SIZE);
775 /* configure the chip select */
776 cqspi_chipselect(nor);
778 /* Store the new configuration of the controller */
779 cqspi->current_page_size = nor->page_size;
780 cqspi->current_erase_size = nor->mtd.erasesize;
781 cqspi->current_addr_width = nor->addr_width;
784 static unsigned int calculate_ticks_for_ns(const unsigned int ref_clk_hz,
785 const unsigned int ns_val)
789 ticks = ref_clk_hz / 1000; /* kHz */
790 ticks = DIV_ROUND_UP(ticks * ns_val, 1000000);
795 static void cqspi_delay(struct spi_nor *nor)
797 struct cqspi_flash_pdata *f_pdata = nor->priv;
798 struct cqspi_st *cqspi = f_pdata->cqspi;
799 void __iomem *iobase = cqspi->iobase;
800 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
801 unsigned int tshsl, tchsh, tslch, tsd2d;
805 /* calculate the number of ref ticks for one sclk tick */
806 tsclk = DIV_ROUND_UP(ref_clk_hz, cqspi->sclk);
808 tshsl = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tshsl_ns);
809 /* this particular value must be at least one sclk */
813 tchsh = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tchsh_ns);
814 tslch = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tslch_ns);
815 tsd2d = calculate_ticks_for_ns(ref_clk_hz, f_pdata->tsd2d_ns);
817 reg = (tshsl & CQSPI_REG_DELAY_TSHSL_MASK)
818 << CQSPI_REG_DELAY_TSHSL_LSB;
819 reg |= (tchsh & CQSPI_REG_DELAY_TCHSH_MASK)
820 << CQSPI_REG_DELAY_TCHSH_LSB;
821 reg |= (tslch & CQSPI_REG_DELAY_TSLCH_MASK)
822 << CQSPI_REG_DELAY_TSLCH_LSB;
823 reg |= (tsd2d & CQSPI_REG_DELAY_TSD2D_MASK)
824 << CQSPI_REG_DELAY_TSD2D_LSB;
825 writel(reg, iobase + CQSPI_REG_DELAY);
828 static void cqspi_config_baudrate_div(struct cqspi_st *cqspi)
830 const unsigned int ref_clk_hz = cqspi->master_ref_clk_hz;
831 void __iomem *reg_base = cqspi->iobase;
834 /* Recalculate the baudrate divisor based on QSPI specification. */
835 div = DIV_ROUND_UP(ref_clk_hz, 2 * cqspi->sclk) - 1;
837 reg = readl(reg_base + CQSPI_REG_CONFIG);
838 reg &= ~(CQSPI_REG_CONFIG_BAUD_MASK << CQSPI_REG_CONFIG_BAUD_LSB);
839 reg |= (div & CQSPI_REG_CONFIG_BAUD_MASK) << CQSPI_REG_CONFIG_BAUD_LSB;
840 writel(reg, reg_base + CQSPI_REG_CONFIG);
843 static void cqspi_readdata_capture(struct cqspi_st *cqspi,
845 const unsigned int delay)
847 void __iomem *reg_base = cqspi->iobase;
850 reg = readl(reg_base + CQSPI_REG_READCAPTURE);
853 reg |= (1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
855 reg &= ~(1 << CQSPI_REG_READCAPTURE_BYPASS_LSB);
857 reg &= ~(CQSPI_REG_READCAPTURE_DELAY_MASK
858 << CQSPI_REG_READCAPTURE_DELAY_LSB);
860 reg |= (delay & CQSPI_REG_READCAPTURE_DELAY_MASK)
861 << CQSPI_REG_READCAPTURE_DELAY_LSB;
863 writel(reg, reg_base + CQSPI_REG_READCAPTURE);
866 static void cqspi_controller_enable(struct cqspi_st *cqspi, bool enable)
868 void __iomem *reg_base = cqspi->iobase;
871 reg = readl(reg_base + CQSPI_REG_CONFIG);
874 reg |= CQSPI_REG_CONFIG_ENABLE_MASK;
876 reg &= ~CQSPI_REG_CONFIG_ENABLE_MASK;
878 writel(reg, reg_base + CQSPI_REG_CONFIG);
881 static void cqspi_configure(struct spi_nor *nor)
883 struct cqspi_flash_pdata *f_pdata = nor->priv;
884 struct cqspi_st *cqspi = f_pdata->cqspi;
885 const unsigned int sclk = f_pdata->clk_rate;
886 int switch_cs = (cqspi->current_cs != f_pdata->cs);
887 int switch_ck = (cqspi->sclk != sclk);
889 if ((cqspi->current_page_size != nor->page_size) ||
890 (cqspi->current_erase_size != nor->mtd.erasesize) ||
891 (cqspi->current_addr_width != nor->addr_width))
894 if (switch_cs || switch_ck)
895 cqspi_controller_enable(cqspi, 0);
897 /* Switch chip select. */
899 cqspi->current_cs = f_pdata->cs;
900 cqspi_configure_cs_and_sizes(nor);
903 /* Setup baudrate divisor and delays */
906 cqspi_config_baudrate_div(cqspi);
908 cqspi_readdata_capture(cqspi, !cqspi->rclk_en,
909 f_pdata->read_delay);
912 if (switch_cs || switch_ck)
913 cqspi_controller_enable(cqspi, 1);
916 static int cqspi_set_protocol(struct spi_nor *nor, const int read)
918 struct cqspi_flash_pdata *f_pdata = nor->priv;
920 f_pdata->inst_width = CQSPI_INST_TYPE_SINGLE;
921 f_pdata->addr_width = CQSPI_INST_TYPE_SINGLE;
922 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
925 switch (nor->read_proto) {
926 case SNOR_PROTO_1_1_1:
927 f_pdata->data_width = CQSPI_INST_TYPE_SINGLE;
929 case SNOR_PROTO_1_1_2:
930 f_pdata->data_width = CQSPI_INST_TYPE_DUAL;
932 case SNOR_PROTO_1_1_4:
933 f_pdata->data_width = CQSPI_INST_TYPE_QUAD;
935 case SNOR_PROTO_1_1_8:
936 f_pdata->data_width = CQSPI_INST_TYPE_OCTAL;
943 cqspi_configure(nor);
948 static ssize_t cqspi_write(struct spi_nor *nor, loff_t to,
949 size_t len, const u_char *buf)
951 struct cqspi_flash_pdata *f_pdata = nor->priv;
952 struct cqspi_st *cqspi = f_pdata->cqspi;
955 ret = cqspi_set_protocol(nor, 0);
959 ret = cqspi_write_setup(nor);
963 if (f_pdata->use_direct_mode) {
964 memcpy_toio(cqspi->ahb_base + to, buf, len);
965 ret = cqspi_wait_idle(cqspi);
967 ret = cqspi_indirect_write_execute(nor, to, buf, len);
975 static void cqspi_rx_dma_callback(void *param)
977 struct cqspi_st *cqspi = param;
979 complete(&cqspi->rx_dma_complete);
982 static int cqspi_direct_read_execute(struct spi_nor *nor, u_char *buf,
983 loff_t from, size_t len)
985 struct cqspi_flash_pdata *f_pdata = nor->priv;
986 struct cqspi_st *cqspi = f_pdata->cqspi;
987 enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT;
988 dma_addr_t dma_src = (dma_addr_t)cqspi->mmap_phys_base + from;
990 struct dma_async_tx_descriptor *tx;
994 if (!cqspi->rx_chan || !virt_addr_valid(buf)) {
995 memcpy_fromio(buf, cqspi->ahb_base + from, len);
999 dma_dst = dma_map_single(nor->dev, buf, len, DMA_FROM_DEVICE);
1000 if (dma_mapping_error(nor->dev, dma_dst)) {
1001 dev_err(nor->dev, "dma mapping failed\n");
1004 tx = dmaengine_prep_dma_memcpy(cqspi->rx_chan, dma_dst, dma_src,
1007 dev_err(nor->dev, "device_prep_dma_memcpy error\n");
1012 tx->callback = cqspi_rx_dma_callback;
1013 tx->callback_param = cqspi;
1014 cookie = tx->tx_submit(tx);
1015 reinit_completion(&cqspi->rx_dma_complete);
1017 ret = dma_submit_error(cookie);
1019 dev_err(nor->dev, "dma_submit_error %d\n", cookie);
1024 dma_async_issue_pending(cqspi->rx_chan);
1025 if (!wait_for_completion_timeout(&cqspi->rx_dma_complete,
1026 msecs_to_jiffies(len))) {
1027 dmaengine_terminate_sync(cqspi->rx_chan);
1028 dev_err(nor->dev, "DMA wait_for_completion_timeout\n");
1034 dma_unmap_single(nor->dev, dma_dst, len, DMA_FROM_DEVICE);
1039 static ssize_t cqspi_read(struct spi_nor *nor, loff_t from,
1040 size_t len, u_char *buf)
1042 struct cqspi_flash_pdata *f_pdata = nor->priv;
1045 ret = cqspi_set_protocol(nor, 1);
1049 ret = cqspi_read_setup(nor);
1053 if (f_pdata->use_direct_mode)
1054 ret = cqspi_direct_read_execute(nor, buf, from, len);
1056 ret = cqspi_indirect_read_execute(nor, buf, from, len);
1063 static int cqspi_erase(struct spi_nor *nor, loff_t offs)
1067 ret = cqspi_set_protocol(nor, 0);
1071 /* Send write enable, then erase commands. */
1072 ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0);
1076 /* Set up command buffer. */
1077 ret = cqspi_command_write_addr(nor, nor->erase_opcode, offs);
1084 static int cqspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
1086 struct cqspi_flash_pdata *f_pdata = nor->priv;
1087 struct cqspi_st *cqspi = f_pdata->cqspi;
1089 mutex_lock(&cqspi->bus_mutex);
1094 static void cqspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
1096 struct cqspi_flash_pdata *f_pdata = nor->priv;
1097 struct cqspi_st *cqspi = f_pdata->cqspi;
1099 mutex_unlock(&cqspi->bus_mutex);
1102 static int cqspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
1106 ret = cqspi_set_protocol(nor, 0);
1108 ret = cqspi_command_read(nor, &opcode, 1, buf, len);
1113 static int cqspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
1117 ret = cqspi_set_protocol(nor, 0);
1119 ret = cqspi_command_write(nor, opcode, buf, len);
1124 static int cqspi_of_get_flash_pdata(struct platform_device *pdev,
1125 struct cqspi_flash_pdata *f_pdata,
1126 struct device_node *np)
1128 if (of_property_read_u32(np, "cdns,read-delay", &f_pdata->read_delay)) {
1129 dev_err(&pdev->dev, "couldn't determine read-delay\n");
1133 if (of_property_read_u32(np, "cdns,tshsl-ns", &f_pdata->tshsl_ns)) {
1134 dev_err(&pdev->dev, "couldn't determine tshsl-ns\n");
1138 if (of_property_read_u32(np, "cdns,tsd2d-ns", &f_pdata->tsd2d_ns)) {
1139 dev_err(&pdev->dev, "couldn't determine tsd2d-ns\n");
1143 if (of_property_read_u32(np, "cdns,tchsh-ns", &f_pdata->tchsh_ns)) {
1144 dev_err(&pdev->dev, "couldn't determine tchsh-ns\n");
1148 if (of_property_read_u32(np, "cdns,tslch-ns", &f_pdata->tslch_ns)) {
1149 dev_err(&pdev->dev, "couldn't determine tslch-ns\n");
1153 if (of_property_read_u32(np, "spi-max-frequency", &f_pdata->clk_rate)) {
1154 dev_err(&pdev->dev, "couldn't determine spi-max-frequency\n");
1161 static int cqspi_of_get_pdata(struct platform_device *pdev)
1163 struct device_node *np = pdev->dev.of_node;
1164 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1166 cqspi->is_decoded_cs = of_property_read_bool(np, "cdns,is-decoded-cs");
1168 if (of_property_read_u32(np, "cdns,fifo-depth", &cqspi->fifo_depth)) {
1169 dev_err(&pdev->dev, "couldn't determine fifo-depth\n");
1173 if (of_property_read_u32(np, "cdns,fifo-width", &cqspi->fifo_width)) {
1174 dev_err(&pdev->dev, "couldn't determine fifo-width\n");
1178 if (of_property_read_u32(np, "cdns,trigger-address",
1179 &cqspi->trigger_address)) {
1180 dev_err(&pdev->dev, "couldn't determine trigger-address\n");
1184 cqspi->rclk_en = of_property_read_bool(np, "cdns,rclk-en");
1189 static void cqspi_controller_init(struct cqspi_st *cqspi)
1193 cqspi_controller_enable(cqspi, 0);
1195 /* Configure the remap address register, no remap */
1196 writel(0, cqspi->iobase + CQSPI_REG_REMAP);
1198 /* Disable all interrupts. */
1199 writel(0, cqspi->iobase + CQSPI_REG_IRQMASK);
1201 /* Configure the SRAM split to 1:1 . */
1202 writel(cqspi->fifo_depth / 2, cqspi->iobase + CQSPI_REG_SRAMPARTITION);
1204 /* Load indirect trigger address. */
1205 writel(cqspi->trigger_address,
1206 cqspi->iobase + CQSPI_REG_INDIRECTTRIGGER);
1208 /* Program read watermark -- 1/2 of the FIFO. */
1209 writel(cqspi->fifo_depth * cqspi->fifo_width / 2,
1210 cqspi->iobase + CQSPI_REG_INDIRECTRDWATERMARK);
1211 /* Program write watermark -- 1/8 of the FIFO. */
1212 writel(cqspi->fifo_depth * cqspi->fifo_width / 8,
1213 cqspi->iobase + CQSPI_REG_INDIRECTWRWATERMARK);
1215 /* Enable Direct Access Controller */
1216 reg = readl(cqspi->iobase + CQSPI_REG_CONFIG);
1217 reg |= CQSPI_REG_CONFIG_ENB_DIR_ACC_CTRL;
1218 writel(reg, cqspi->iobase + CQSPI_REG_CONFIG);
1220 cqspi_controller_enable(cqspi, 1);
1223 static void cqspi_request_mmap_dma(struct cqspi_st *cqspi)
1225 dma_cap_mask_t mask;
1228 dma_cap_set(DMA_MEMCPY, mask);
1230 cqspi->rx_chan = dma_request_chan_by_mask(&mask);
1231 if (IS_ERR(cqspi->rx_chan)) {
1232 dev_err(&cqspi->pdev->dev, "No Rx DMA available\n");
1233 cqspi->rx_chan = NULL;
1235 init_completion(&cqspi->rx_dma_complete);
1238 static int cqspi_setup_flash(struct cqspi_st *cqspi, struct device_node *np)
1240 struct platform_device *pdev = cqspi->pdev;
1241 struct device *dev = &pdev->dev;
1242 const struct cqspi_driver_platdata *ddata;
1243 struct spi_nor_hwcaps hwcaps;
1244 struct cqspi_flash_pdata *f_pdata;
1245 struct spi_nor *nor;
1246 struct mtd_info *mtd;
1250 ddata = of_device_get_match_data(dev);
1252 dev_err(dev, "Couldn't find driver data\n");
1255 hwcaps.mask = ddata->hwcaps_mask;
1257 /* Get flash device data */
1258 for_each_available_child_of_node(dev->of_node, np) {
1259 ret = of_property_read_u32(np, "reg", &cs);
1261 dev_err(dev, "Couldn't determine chip select.\n");
1265 if (cs >= CQSPI_MAX_CHIPSELECT) {
1267 dev_err(dev, "Chip select %d out of range.\n", cs);
1271 f_pdata = &cqspi->f_pdata[cs];
1272 f_pdata->cqspi = cqspi;
1275 ret = cqspi_of_get_flash_pdata(pdev, f_pdata, np);
1279 nor = &f_pdata->nor;
1285 spi_nor_set_flash_node(nor, np);
1286 nor->priv = f_pdata;
1288 nor->read_reg = cqspi_read_reg;
1289 nor->write_reg = cqspi_write_reg;
1290 nor->read = cqspi_read;
1291 nor->write = cqspi_write;
1292 nor->erase = cqspi_erase;
1293 nor->prepare = cqspi_prep;
1294 nor->unprepare = cqspi_unprep;
1296 mtd->name = devm_kasprintf(dev, GFP_KERNEL, "%s.%d",
1303 ret = spi_nor_scan(nor, NULL, &hwcaps);
1307 ret = mtd_device_register(mtd, NULL, 0);
1311 f_pdata->registered = true;
1313 if (mtd->size <= cqspi->ahb_size) {
1314 f_pdata->use_direct_mode = true;
1315 dev_dbg(nor->dev, "using direct mode for %s\n",
1318 if (!cqspi->rx_chan)
1319 cqspi_request_mmap_dma(cqspi);
1326 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1327 if (cqspi->f_pdata[i].registered)
1328 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1332 static int cqspi_probe(struct platform_device *pdev)
1334 struct device_node *np = pdev->dev.of_node;
1335 struct device *dev = &pdev->dev;
1336 struct cqspi_st *cqspi;
1337 struct resource *res;
1338 struct resource *res_ahb;
1339 const struct cqspi_driver_platdata *ddata;
1343 cqspi = devm_kzalloc(dev, sizeof(*cqspi), GFP_KERNEL);
1347 mutex_init(&cqspi->bus_mutex);
1349 platform_set_drvdata(pdev, cqspi);
1351 /* Obtain configuration from OF. */
1352 ret = cqspi_of_get_pdata(pdev);
1354 dev_err(dev, "Cannot get mandatory OF data.\n");
1358 /* Obtain QSPI clock. */
1359 cqspi->clk = devm_clk_get(dev, NULL);
1360 if (IS_ERR(cqspi->clk)) {
1361 dev_err(dev, "Cannot claim QSPI clock.\n");
1362 return PTR_ERR(cqspi->clk);
1365 /* Obtain and remap controller address. */
1366 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1367 cqspi->iobase = devm_ioremap_resource(dev, res);
1368 if (IS_ERR(cqspi->iobase)) {
1369 dev_err(dev, "Cannot remap controller address.\n");
1370 return PTR_ERR(cqspi->iobase);
1373 /* Obtain and remap AHB address. */
1374 res_ahb = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1375 cqspi->ahb_base = devm_ioremap_resource(dev, res_ahb);
1376 if (IS_ERR(cqspi->ahb_base)) {
1377 dev_err(dev, "Cannot remap AHB address.\n");
1378 return PTR_ERR(cqspi->ahb_base);
1380 cqspi->mmap_phys_base = (dma_addr_t)res_ahb->start;
1381 cqspi->ahb_size = resource_size(res_ahb);
1383 init_completion(&cqspi->transfer_complete);
1385 /* Obtain IRQ line. */
1386 irq = platform_get_irq(pdev, 0);
1388 dev_err(dev, "Cannot obtain IRQ.\n");
1392 pm_runtime_enable(dev);
1393 ret = pm_runtime_get_sync(dev);
1395 pm_runtime_put_noidle(dev);
1399 ret = clk_prepare_enable(cqspi->clk);
1401 dev_err(dev, "Cannot enable QSPI clock.\n");
1402 goto probe_clk_failed;
1405 cqspi->master_ref_clk_hz = clk_get_rate(cqspi->clk);
1406 ddata = of_device_get_match_data(dev);
1407 if (ddata && (ddata->quirks & CQSPI_NEEDS_WR_DELAY))
1408 cqspi->wr_delay = 5 * DIV_ROUND_UP(NSEC_PER_SEC,
1409 cqspi->master_ref_clk_hz);
1411 ret = devm_request_irq(dev, irq, cqspi_irq_handler, 0,
1414 dev_err(dev, "Cannot request IRQ.\n");
1415 goto probe_irq_failed;
1418 cqspi_wait_idle(cqspi);
1419 cqspi_controller_init(cqspi);
1420 cqspi->current_cs = -1;
1423 ret = cqspi_setup_flash(cqspi, np);
1425 dev_err(dev, "Cadence QSPI NOR probe failed %d\n", ret);
1426 goto probe_setup_failed;
1431 cqspi_controller_enable(cqspi, 0);
1433 clk_disable_unprepare(cqspi->clk);
1435 pm_runtime_put_sync(dev);
1436 pm_runtime_disable(dev);
1440 static int cqspi_remove(struct platform_device *pdev)
1442 struct cqspi_st *cqspi = platform_get_drvdata(pdev);
1445 for (i = 0; i < CQSPI_MAX_CHIPSELECT; i++)
1446 if (cqspi->f_pdata[i].registered)
1447 mtd_device_unregister(&cqspi->f_pdata[i].nor.mtd);
1449 cqspi_controller_enable(cqspi, 0);
1452 dma_release_channel(cqspi->rx_chan);
1454 clk_disable_unprepare(cqspi->clk);
1456 pm_runtime_put_sync(&pdev->dev);
1457 pm_runtime_disable(&pdev->dev);
1462 #ifdef CONFIG_PM_SLEEP
1463 static int cqspi_suspend(struct device *dev)
1465 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1467 cqspi_controller_enable(cqspi, 0);
1471 static int cqspi_resume(struct device *dev)
1473 struct cqspi_st *cqspi = dev_get_drvdata(dev);
1475 cqspi_controller_enable(cqspi, 1);
1479 static const struct dev_pm_ops cqspi__dev_pm_ops = {
1480 .suspend = cqspi_suspend,
1481 .resume = cqspi_resume,
1484 #define CQSPI_DEV_PM_OPS (&cqspi__dev_pm_ops)
1486 #define CQSPI_DEV_PM_OPS NULL
1489 static const struct cqspi_driver_platdata cdns_qspi = {
1490 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK,
1493 static const struct cqspi_driver_platdata k2g_qspi = {
1494 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK,
1495 .quirks = CQSPI_NEEDS_WR_DELAY,
1498 static const struct cqspi_driver_platdata am654_ospi = {
1499 .hwcaps_mask = CQSPI_BASE_HWCAPS_MASK | SNOR_HWCAPS_READ_1_1_8,
1500 .quirks = CQSPI_NEEDS_WR_DELAY,
1503 static const struct of_device_id cqspi_dt_ids[] = {
1505 .compatible = "cdns,qspi-nor",
1509 .compatible = "ti,k2g-qspi",
1513 .compatible = "ti,am654-ospi",
1514 .data = &am654_ospi,
1516 { /* end of table */ }
1519 MODULE_DEVICE_TABLE(of, cqspi_dt_ids);
1521 static struct platform_driver cqspi_platform_driver = {
1522 .probe = cqspi_probe,
1523 .remove = cqspi_remove,
1526 .pm = CQSPI_DEV_PM_OPS,
1527 .of_match_table = cqspi_dt_ids,
1531 module_platform_driver(cqspi_platform_driver);
1533 MODULE_DESCRIPTION("Cadence QSPI Controller Driver");
1534 MODULE_LICENSE("GPL v2");
1535 MODULE_ALIAS("platform:" CQSPI_NAME);
1536 MODULE_AUTHOR("Ley Foon Tan <lftan@altera.com>");
1537 MODULE_AUTHOR("Graham Moore <grmoore@opensource.altera.com>");