1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Atmel QSPI Controller
5 * Copyright (C) 2015 Atmel Corporation
6 * Copyright (C) 2018 Cryptera A/S
8 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
9 * Author: Piotr Bugalski <bugalski.piotr@gmail.com>
19 #include <dm/device_compat.h>
20 #include <linux/bitfield.h>
21 #include <linux/bitops.h>
22 #include <linux/err.h>
24 #include <linux/iopoll.h>
25 #include <linux/ioport.h>
30 /* QSPI register offsets */
31 #define QSPI_CR 0x0000 /* Control Register */
32 #define QSPI_MR 0x0004 /* Mode Register */
33 #define QSPI_RD 0x0008 /* Receive Data Register */
34 #define QSPI_TD 0x000c /* Transmit Data Register */
35 #define QSPI_SR 0x0010 /* Status Register */
36 #define QSPI_SR2 0x0024 /* SAMA7G5 Status Register */
37 #define QSPI_IER 0x0014 /* Interrupt Enable Register */
38 #define QSPI_IDR 0x0018 /* Interrupt Disable Register */
39 #define QSPI_IMR 0x001c /* Interrupt Mask Register */
40 #define QSPI_SCR 0x0020 /* Serial Clock Register */
42 #define QSPI_IAR 0x0030 /* Instruction Address Register */
43 #define QSPI_ICR 0x0034 /* Instruction Code Register */
44 #define QSPI_WICR 0x0034 /* Write Instruction Code Register */
45 #define QSPI_IFR 0x0038 /* Instruction Frame Register */
46 #define QSPI_RICR 0x003C /* Read Instruction Code Register */
48 #define QSPI_SMR 0x0040 /* Scrambling Mode Register */
49 #define QSPI_SKR 0x0044 /* Scrambling Key Register */
51 #define QSPI_REFRESH 0x0050 /* Refresh Register */
52 #define QSPI_WRACNT 0x0054 /* Write Access Counter Register */
53 #define QSPI_DLLCFG 0x0058 /* DLL Configuration Register */
54 #define QSPI_PCALCFG 0x005C /* Pad Calibration Configuration Register */
55 #define QSPI_PCALBP 0x0060 /* Pad Calibration Bypass Register */
56 #define QSPI_TOUT 0x0064 /* Timeout Register */
58 #define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
59 #define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
61 #define QSPI_VERSION 0x00FC /* Version Register */
63 /* Bitfields in QSPI_CR (Control Register) */
64 #define QSPI_CR_QSPIEN BIT(0)
65 #define QSPI_CR_QSPIDIS BIT(1)
66 #define QSPI_CR_DLLON BIT(2)
67 #define QSPI_CR_DLLOFF BIT(3)
68 #define QSPI_CR_STPCAL BIT(4)
69 #define QSPI_CR_SRFRSH BIT(5)
70 #define QSPI_CR_SWRST BIT(7)
71 #define QSPI_CR_UPDCFG BIT(8)
72 #define QSPI_CR_STTFR BIT(9)
73 #define QSPI_CR_RTOUT BIT(10)
74 #define QSPI_CR_LASTXFER BIT(24)
76 /* Bitfields in QSPI_MR (Mode Register) */
77 #define QSPI_MR_SMM BIT(0)
78 #define QSPI_MR_LLB BIT(1)
79 #define QSPI_MR_WDRBT BIT(2)
80 #define QSPI_MR_SMRM BIT(3)
81 #define QSPI_MR_DQSDLYEN BIT(3)
83 #define QSPI_MR_CSMODE_MASK GENMASK(5, 4)
84 #define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4)
85 #define QSPI_MR_CSMODE_LASTXFER (1 << 4)
86 #define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4)
87 #define QSPI_MR_NBBITS_MASK GENMASK(11, 8)
88 #define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
89 #define QSPI_MR_OENSD BIT(15)
90 #define QSPI_MR_DLYBCT_MASK GENMASK(23, 16)
91 #define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK)
92 #define QSPI_MR_DLYCS_MASK GENMASK(31, 24)
93 #define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK)
95 /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */
96 #define QSPI_SR_RDRF BIT(0)
97 #define QSPI_SR_TDRE BIT(1)
98 #define QSPI_SR_TXEMPTY BIT(2)
99 #define QSPI_SR_OVRES BIT(3)
100 #define QSPI_SR_CSR BIT(8)
101 #define QSPI_SR_CSS BIT(9)
102 #define QSPI_SR_INSTRE BIT(10)
103 #define QSPI_SR_LWRA BIT(11)
104 #define QSPI_SR_QITF BIT(12)
105 #define QSPI_SR_QITR BIT(13)
106 #define QSPI_SR_CSFA BIT(14)
107 #define QSPI_SR_CSRA BIT(15)
108 #define QSPI_SR_RFRSHD BIT(16)
109 #define QSPI_SR_TOUT BIT(17)
110 #define QSPI_SR_QSPIENS BIT(24)
112 #define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
114 /* Bitfields in QSPI_SCR (Serial Clock Register) */
115 #define QSPI_SCR_CPOL BIT(0)
116 #define QSPI_SCR_CPHA BIT(1)
117 #define QSPI_SCR_SCBR_MASK GENMASK(15, 8)
118 #define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK)
119 #define QSPI_SCR_DLYBS_MASK GENMASK(23, 16)
120 #define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK)
122 /* Bitfields in QSPI_SR2 (SAMA7G5 Status Register) */
123 #define QSPI_SR2_SYNCBSY BIT(0)
124 #define QSPI_SR2_QSPIENS BIT(1)
125 #define QSPI_SR2_CSS BIT(2)
126 #define QSPI_SR2_RBUSY BIT(3)
127 #define QSPI_SR2_HIDLE BIT(4)
128 #define QSPI_SR2_DLOCK BIT(5)
129 #define QSPI_SR2_CALBSY BIT(6)
131 /* Bitfields in QSPI_IAR (Instruction Address Register) */
132 #define QSPI_IAR_ADDR GENMASK(31, 0)
134 /* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */
135 #define QSPI_ICR_INST_MASK GENMASK(7, 0)
136 #define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK)
137 #define QSPI_ICR_INST_MASK_SAMA7G5 GENMASK(15, 0)
138 #define QSPI_ICR_OPT_MASK GENMASK(23, 16)
139 #define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
141 /* Bitfields in QSPI_IFR (Instruction Frame Register) */
142 #define QSPI_IFR_WIDTH_MASK GENMASK(2, 0)
143 #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0)
144 #define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0)
145 #define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0)
146 #define QSPI_IFR_WIDTH_DUAL_IO (3 << 0)
147 #define QSPI_IFR_WIDTH_QUAD_IO (4 << 0)
148 #define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0)
149 #define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0)
150 #define QSPI_IFR_WIDTH_OCT_OUTPUT (7 << 0)
151 #define QSPI_IFR_WIDTH_OCT_IO (8 << 0)
152 #define QSPI_IFR_WIDTH_OCT_CMD (9 << 0)
153 #define QSPI_IFR_INSTEN BIT(4)
154 #define QSPI_IFR_ADDREN BIT(5)
155 #define QSPI_IFR_OPTEN BIT(6)
156 #define QSPI_IFR_DATAEN BIT(7)
157 #define QSPI_IFR_OPTL_MASK GENMASK(9, 8)
158 #define QSPI_IFR_OPTL_1BIT (0 << 8)
159 #define QSPI_IFR_OPTL_2BIT (1 << 8)
160 #define QSPI_IFR_OPTL_4BIT (2 << 8)
161 #define QSPI_IFR_OPTL_8BIT (3 << 8)
162 #define QSPI_IFR_ADDRL BIT(10)
163 #define QSPI_IFR_ADDRL_SAMA7G5 GENMASK(11, 10)
164 #define QSPI_IFR_TFRTYP_MEM BIT(12)
165 #define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13)
166 #define QSPI_IFR_CRM BIT(14)
167 #define QSPI_IFR_DDREN BIT(15)
168 #define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
169 #define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
170 #define QSPI_IFR_END BIT(22)
171 #define QSPI_IFR_SMRM BIT(23)
172 #define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */
173 #define QSPI_IFR_DQSEN BIT(25)
174 #define QSPI_IFR_DDRCMDEN BIT(26)
175 #define QSPI_IFR_HFWBEN BIT(27)
176 #define QSPI_IFR_PROTTYP GENMASK(29, 28)
177 #define QSPI_IFR_PROTTYP_STD_SPI 0
178 #define QSPI_IFR_PROTTYP_TWIN_QUAD 1
179 #define QSPI_IFR_PROTTYP_OCTAFLASH 2
180 #define QSPI_IFR_PROTTYP_HYPERFLASH 3
182 /* Bitfields in QSPI_SMR (Scrambling Mode Register) */
183 #define QSPI_SMR_SCREN BIT(0)
184 #define QSPI_SMR_RVDIS BIT(1)
185 #define QSPI_SMR_SCRKL BIT(2)
187 /* Bitfields in QSPI_REFRESH (Refresh Register) */
188 #define QSPI_REFRESH_DELAY_COUNTER GENMASK(31, 0)
190 /* Bitfields in QSPI_WRACNT (Write Access Counter Register) */
191 #define QSPI_WRACNT_NBWRA GENMASK(31, 0)
193 /* Bitfields in QSPI_DLLCFG (DLL Configuration Register) */
194 #define QSPI_DLLCFG_RANGE BIT(0)
196 /* Bitfields in QSPI_PCALCFG (DLL Pad Calibration Configuration Register) */
197 #define QSPI_PCALCFG_AAON BIT(0)
198 #define QSPI_PCALCFG_DAPCAL BIT(1)
199 #define QSPI_PCALCFG_DIFFPM BIT(2)
200 #define QSPI_PCALCFG_CLKDIV GENMASK(6, 4)
201 #define QSPI_PCALCFG_CALCNT GENMASK(16, 8)
202 #define QSPI_PCALCFG_CALP GENMASK(27, 24)
203 #define QSPI_PCALCFG_CALN GENMASK(31, 28)
205 /* Bitfields in QSPI_PCALBP (DLL Pad Calibration Bypass Register) */
206 #define QSPI_PCALBP_BPEN BIT(0)
207 #define QSPI_PCALBP_CALPBP GENMASK(11, 8)
208 #define QSPI_PCALBP_CALNBP GENMASK(19, 16)
210 /* Bitfields in QSPI_TOUT (Timeout Register) */
211 #define QSPI_TOUT_TCNTM GENMASK(15, 0)
213 /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
214 #define QSPI_WPMR_WPEN BIT(0)
215 #define QSPI_WPMR_WPITEN BIT(1)
216 #define QSPI_WPMR_WPCREN BIT(2)
217 #define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
218 #define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
220 /* Bitfields in QSPI_WPSR (Write Protection Status Register) */
221 #define QSPI_WPSR_WPVS BIT(0)
222 #define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8)
223 #define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC)
225 #define ATMEL_QSPI_TIMEOUT 1000000 /* us */
226 #define ATMEL_QSPI_SYNC_TIMEOUT 300000 /* us */
227 #define QSPI_DLLCFG_THRESHOLD_FREQ 90000000U
228 #define QSPI_TOUT_MAX 0xffff
231 * struct atmel_qspi_pcal - Pad Calibration Clock Division
232 * @pclk_rate: peripheral clock rate.
233 * @pclkdiv: calibration clock division. The clock applied to the calibration
234 * cell is divided by pclkdiv + 1.
236 struct atmel_qspi_pcal {
241 #define ATMEL_QSPI_PCAL_ARRAY_SIZE 8
242 static const struct atmel_qspi_pcal pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE] = {
253 struct atmel_qspi_caps {
260 struct atmel_qspi_priv_ops;
265 resource_size_t mmap_size;
266 const struct atmel_qspi_caps *caps;
267 const struct atmel_qspi_priv_ops *ops;
273 struct atmel_qspi_priv_ops {
274 int (*set_cfg)(struct atmel_qspi *aq, const struct spi_mem_op *op,
276 int (*transfer)(struct atmel_qspi *aq, const struct spi_mem_op *op,
280 struct atmel_qspi_mode {
287 static const struct atmel_qspi_mode atmel_qspi_modes[] = {
288 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
289 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
290 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
291 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
292 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
293 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
294 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
297 static const struct atmel_qspi_mode atmel_qspi_sama7g5_modes[] = {
298 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
299 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
300 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
301 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
302 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
303 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
304 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
305 { 1, 1, 8, QSPI_IFR_WIDTH_OCT_OUTPUT },
306 { 1, 8, 8, QSPI_IFR_WIDTH_OCT_IO },
307 { 8, 8, 8, QSPI_IFR_WIDTH_OCT_CMD },
311 static const char *atmel_qspi_reg_name(u32 offset, char *tmp, size_t sz)
365 snprintf(tmp, sz, "0x%02x", offset);
371 #endif /* VERBOSE_DEBUG */
373 static u32 atmel_qspi_read(struct atmel_qspi *aq, u32 offset)
375 u32 value = readl(aq->regs + offset);
380 dev_vdbg(aq->dev, "read 0x%08x from %s\n", value,
381 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
382 #endif /* VERBOSE_DEBUG */
387 static void atmel_qspi_write(u32 value, struct atmel_qspi *aq, u32 offset)
392 dev_vdbg(aq->dev, "write 0x%08x into %s\n", value,
393 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
394 #endif /* VERBOSE_DEBUG */
396 writel(value, aq->regs + offset);
399 static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
400 const struct atmel_qspi_mode *mode)
402 if (op->cmd.buswidth != mode->cmd_buswidth)
405 if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
408 if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
414 static int atmel_qspi_find_mode(const struct spi_mem_op *op)
418 for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
419 if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
425 static int atmel_qspi_sama7g5_find_mode(const struct spi_mem_op *op)
429 for (i = 0; i < ARRAY_SIZE(atmel_qspi_sama7g5_modes); i++)
430 if (atmel_qspi_is_compatible(op, &atmel_qspi_sama7g5_modes[i]))
436 static bool atmel_qspi_supports_op(struct spi_slave *slave,
437 const struct spi_mem_op *op)
439 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
441 if (!spi_mem_default_supports_op(slave, op))
444 if (aq->caps->octal) {
445 if (atmel_qspi_sama7g5_find_mode(op) < 0)
451 if (atmel_qspi_find_mode(op) < 0)
454 /* special case not supported by hardware */
455 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
456 op->dummy.nbytes == 0)
462 static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
463 const struct spi_mem_op *op, u32 *offset)
466 u32 dummy_cycles = 0;
470 icr = QSPI_ICR_INST(op->cmd.opcode);
471 ifr = QSPI_IFR_INSTEN;
473 mode = atmel_qspi_find_mode(op);
476 ifr |= atmel_qspi_modes[mode].config;
478 if (op->dummy.buswidth && op->dummy.nbytes)
479 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
482 * The controller allows 24 and 32-bit addressing while NAND-flash
483 * requires 16-bit long. Handling 8-bit long addresses is done using
484 * the option field. For the 16-bit addresses, the workaround depends
485 * of the number of requested dummy bits. If there are 8 or more dummy
486 * cycles, the address is shifted and sent with the first dummy byte.
487 * Otherwise opcode is disabled and the first byte of the address
488 * contains the command opcode (works only if the opcode and address
489 * use the same buswidth). The limitation is when the 16-bit address is
490 * used without enough dummy cycles and the opcode is using a different
491 * buswidth than the address.
493 if (op->addr.buswidth) {
494 switch (op->addr.nbytes) {
498 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
499 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
502 if (dummy_cycles < 8 / op->addr.buswidth) {
503 ifr &= ~QSPI_IFR_INSTEN;
504 ifr |= QSPI_IFR_ADDREN;
505 iar = (op->cmd.opcode << 16) |
506 (op->addr.val & 0xffff);
508 ifr |= QSPI_IFR_ADDREN;
509 iar = (op->addr.val << 8) & 0xffffff;
510 dummy_cycles -= 8 / op->addr.buswidth;
514 ifr |= QSPI_IFR_ADDREN;
515 iar = op->addr.val & 0xffffff;
518 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
519 iar = op->addr.val & 0x7ffffff;
526 /* offset of the data access in the QSPI memory space */
529 /* Set number of dummy cycles */
531 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
533 /* Set data enable */
535 ifr |= QSPI_IFR_DATAEN;
538 * If the QSPI controller is set in regular SPI mode, set it in
539 * Serial Memory Mode (SMM).
541 if (aq->mr != QSPI_MR_SMM) {
542 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
543 aq->mr = QSPI_MR_SMM;
546 /* Clear pending interrupts */
547 (void)atmel_qspi_read(aq, QSPI_SR);
549 if (aq->caps->has_ricr) {
550 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
551 ifr |= QSPI_IFR_APBTFRTYP_READ;
553 /* Set QSPI Instruction Frame registers */
554 atmel_qspi_write(iar, aq, QSPI_IAR);
555 if (op->data.dir == SPI_MEM_DATA_IN)
556 atmel_qspi_write(icr, aq, QSPI_RICR);
558 atmel_qspi_write(icr, aq, QSPI_WICR);
559 atmel_qspi_write(ifr, aq, QSPI_IFR);
561 if (op->data.dir == SPI_MEM_DATA_OUT)
562 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
564 /* Set QSPI Instruction Frame registers */
565 atmel_qspi_write(iar, aq, QSPI_IAR);
566 atmel_qspi_write(icr, aq, QSPI_ICR);
567 atmel_qspi_write(ifr, aq, QSPI_IFR);
573 static int atmel_qspi_transfer(struct atmel_qspi *aq,
574 const struct spi_mem_op *op, u32 offset)
578 /* Skip to the final steps if there is no data */
579 if (op->data.nbytes) {
580 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
581 (void)atmel_qspi_read(aq, QSPI_IFR);
583 /* Send/Receive data */
584 if (op->data.dir == SPI_MEM_DATA_IN)
585 memcpy_fromio(op->data.buf.in, aq->mem + offset,
588 memcpy_toio(aq->mem + offset, op->data.buf.out,
591 /* Release the chip-select */
592 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
595 /* Poll INSTruction End and Chip Select Rise flags. */
596 imr = QSPI_SR_INSTRE | QSPI_SR_CSR;
597 return readl_poll_timeout(aq->regs + QSPI_SR, sr, (sr & imr) == imr,
601 static int atmel_qspi_reg_sync(struct atmel_qspi *aq)
605 return readl_poll_timeout(aq->regs + QSPI_SR2, val,
606 !(val & QSPI_SR2_SYNCBSY),
607 ATMEL_QSPI_SYNC_TIMEOUT);
610 static int atmel_qspi_update_config(struct atmel_qspi *aq)
614 ret = atmel_qspi_reg_sync(aq);
617 atmel_qspi_write(QSPI_CR_UPDCFG, aq, QSPI_CR);
618 return atmel_qspi_reg_sync(aq);
621 static int atmel_qspi_sama7g5_set_cfg(struct atmel_qspi *aq,
622 const struct spi_mem_op *op, u32 *offset)
628 icr = FIELD_PREP(QSPI_ICR_INST_MASK_SAMA7G5, op->cmd.opcode);
629 ifr = QSPI_IFR_INSTEN;
631 mode = atmel_qspi_sama7g5_find_mode(op);
634 ifr |= atmel_qspi_sama7g5_modes[mode].config;
636 if (op->dummy.buswidth && op->dummy.nbytes) {
637 if (op->addr.dtr && op->dummy.dtr && op->data.dtr)
638 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
639 (2 * op->dummy.buswidth));
641 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
645 if (op->addr.buswidth && op->addr.nbytes) {
646 ifr |= FIELD_PREP(QSPI_IFR_ADDRL_SAMA7G5, op->addr.nbytes - 1) |
648 iar = FIELD_PREP(QSPI_IAR_ADDR, op->addr.val);
651 if (op->addr.dtr && op->dummy.dtr && op->data.dtr) {
652 ifr |= QSPI_IFR_DDREN;
654 ifr |= QSPI_IFR_DDRCMDEN;
655 ifr |= QSPI_IFR_DQSEN;
658 if (op->cmd.buswidth == 8 || op->addr.buswidth == 8 ||
659 op->data.buswidth == 8)
660 ifr |= FIELD_PREP(QSPI_IFR_PROTTYP, QSPI_IFR_PROTTYP_OCTAFLASH);
662 /* offset of the data access in the QSPI memory space */
665 /* Set data enable */
666 if (op->data.nbytes) {
667 ifr |= QSPI_IFR_DATAEN;
669 ifr |= QSPI_IFR_TFRTYP_MEM;
673 * If the QSPI controller is set in regular SPI mode, set it in
674 * Serial Memory Mode (SMM).
676 if (aq->mr != QSPI_MR_SMM) {
677 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
678 ret = atmel_qspi_update_config(aq);
681 aq->mr = QSPI_MR_SMM;
684 /* Clear pending interrupts */
685 (void)atmel_qspi_read(aq, QSPI_SR);
687 /* Set QSPI Instruction Frame registers */
688 if (op->addr.nbytes && !op->data.nbytes)
689 atmel_qspi_write(iar, aq, QSPI_IAR);
691 if (op->data.dir == SPI_MEM_DATA_IN) {
692 atmel_qspi_write(icr, aq, QSPI_RICR);
694 atmel_qspi_write(icr, aq, QSPI_WICR);
696 atmel_qspi_write(FIELD_PREP(QSPI_WRACNT_NBWRA,
701 atmel_qspi_write(ifr, aq, QSPI_IFR);
703 return atmel_qspi_update_config(aq);
706 static int atmel_qspi_sama7g5_transfer(struct atmel_qspi *aq,
707 const struct spi_mem_op *op, u32 offset)
712 if (!op->data.nbytes) {
713 /* Start the transfer. */
714 err = atmel_qspi_reg_sync(aq);
717 atmel_qspi_write(QSPI_CR_STTFR, aq, QSPI_CR);
719 return readl_poll_timeout(aq->regs + QSPI_SR, val,
724 /* Send/Receive data. */
725 if (op->data.dir == SPI_MEM_DATA_IN) {
726 memcpy_fromio(op->data.buf.in, aq->mem + offset,
729 if (op->addr.nbytes) {
730 err = readl_poll_timeout(aq->regs + QSPI_SR2, val,
731 !(val & QSPI_SR2_RBUSY),
732 ATMEL_QSPI_SYNC_TIMEOUT);
737 memcpy_toio(aq->mem + offset, op->data.buf.out,
740 err = readl_poll_timeout(aq->regs + QSPI_SR, val,
747 /* Release the chip-select. */
748 err = atmel_qspi_reg_sync(aq);
751 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
753 return readl_poll_timeout(aq->regs + QSPI_SR, val, val & QSPI_SR_CSRA,
757 static int atmel_qspi_exec_op(struct spi_slave *slave,
758 const struct spi_mem_op *op)
760 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
765 * Check if the address exceeds the MMIO window size. An improvement
766 * would be to add support for regular SPI mode and fall back to it
767 * when the flash memories overrun the controller's memory space.
769 if (op->addr.val + op->data.nbytes > aq->mmap_size)
772 if (op->addr.nbytes > 4)
775 err = aq->ops->set_cfg(aq, op, &offset);
779 return aq->ops->transfer(aq, op, offset);
782 static int atmel_qspi_set_pad_calibration(struct udevice *bus, uint hz)
784 struct atmel_qspi *aq = dev_get_priv(bus);
789 for (i = 0; i < ATMEL_QSPI_PCAL_ARRAY_SIZE; i++) {
790 if (aq->bus_clk_rate <= pcal[i].pclk_rate) {
791 pclk_div = pcal[i].pclk_div;
797 * Use the biggest divider in case the peripheral clock exceeds
800 if (aq->bus_clk_rate > pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_rate)
801 pclk_div = pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_div;
803 /* Disable QSPI while configuring the pad calibration. */
804 status = atmel_qspi_read(aq, QSPI_SR2);
805 if (status & QSPI_SR2_QSPIENS) {
806 ret = atmel_qspi_reg_sync(aq);
809 atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
813 * The analog circuitry is not shut down at the end of the calibration
814 * and the start-up time is only required for the first calibration
815 * sequence, thus increasing performance. Set the delay between the Pad
816 * calibration analog circuitry and the calibration request to 2us.
818 atmel_qspi_write(QSPI_PCALCFG_AAON |
819 FIELD_PREP(QSPI_PCALCFG_CLKDIV, pclk_div) |
820 FIELD_PREP(QSPI_PCALCFG_CALCNT,
821 2 * (aq->bus_clk_rate / 1000000)),
824 /* DLL On + start calibration. */
825 atmel_qspi_write(QSPI_CR_DLLON | QSPI_CR_STPCAL, aq, QSPI_CR);
826 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
827 (val & QSPI_SR2_DLOCK) &&
828 !(val & QSPI_SR2_CALBSY),
831 /* Refresh analogic blocks every 1 ms.*/
832 atmel_qspi_write(FIELD_PREP(QSPI_REFRESH_DELAY_COUNTER, hz / 1000),
838 static int atmel_qspi_set_gclk(struct udevice *bus, uint hz)
840 struct atmel_qspi *aq = dev_get_priv(bus);
845 /* Disable DLL before setting GCLK */
846 status = atmel_qspi_read(aq, QSPI_SR2);
847 if (status & QSPI_SR2_DLOCK) {
848 atmel_qspi_write(QSPI_CR_DLLOFF, aq, QSPI_CR);
849 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
850 !(val & QSPI_SR2_DLOCK),
856 if (hz > QSPI_DLLCFG_THRESHOLD_FREQ)
857 atmel_qspi_write(QSPI_DLLCFG_RANGE, aq, QSPI_DLLCFG);
859 atmel_qspi_write(0, aq, QSPI_DLLCFG);
861 ret = clk_get_by_name(bus, "gclk", &gclk);
863 dev_err(bus, "Missing QSPI generic clock\n");
867 ret = clk_disable(&gclk);
869 dev_err(bus, "Failed to disable QSPI generic clock\n");
871 ret = clk_set_rate(&gclk, hz);
873 dev_err(bus, "Failed to set generic clock rate.\n");
877 ret = clk_enable(&gclk);
879 dev_err(bus, "Failed to enable QSPI generic clock\n");
885 static int atmel_qspi_sama7g5_set_speed(struct udevice *bus, uint hz)
887 struct atmel_qspi *aq = dev_get_priv(bus);
891 ret = atmel_qspi_set_gclk(bus, hz);
895 if (aq->caps->octal) {
896 ret = atmel_qspi_set_pad_calibration(bus, hz);
900 atmel_qspi_write(QSPI_CR_DLLON, aq, QSPI_CR);
901 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
902 val & QSPI_SR2_DLOCK,
906 /* Set the QSPI controller by default in Serial Memory Mode */
907 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
908 ret = atmel_qspi_update_config(aq);
911 aq->mr = QSPI_MR_SMM;
913 /* Enable the QSPI controller. */
914 ret = atmel_qspi_reg_sync(aq);
917 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
918 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
919 val & QSPI_SR2_QSPIENS,
920 ATMEL_QSPI_SYNC_TIMEOUT);
925 ret = readl_poll_timeout(aq->regs + QSPI_SR, val,
926 val & QSPI_SR_RFRSHD,
929 atmel_qspi_write(FIELD_PREP(QSPI_TOUT_TCNTM, QSPI_TOUT_MAX),
935 static int atmel_qspi_set_speed(struct udevice *bus, uint hz)
937 struct atmel_qspi *aq = dev_get_priv(bus);
938 u32 scr, scbr, mask, new_value;
940 if (aq->caps->has_gclk)
941 return atmel_qspi_sama7g5_set_speed(bus, hz);
943 /* Compute the QSPI baudrate */
944 scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz);
948 new_value = QSPI_SCR_SCBR(scbr);
949 mask = QSPI_SCR_SCBR_MASK;
951 scr = atmel_qspi_read(aq, QSPI_SCR);
952 if ((scr & mask) == new_value)
955 scr = (scr & ~mask) | new_value;
956 atmel_qspi_write(scr, aq, QSPI_SCR);
961 static int atmel_qspi_set_mode(struct udevice *bus, uint mode)
963 struct atmel_qspi *aq = dev_get_priv(bus);
964 u32 scr, mask, new_value = 0;
967 new_value = QSPI_SCR_CPOL;
969 new_value = QSPI_SCR_CPHA;
971 mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA;
973 scr = atmel_qspi_read(aq, QSPI_SCR);
974 if ((scr & mask) == new_value)
977 scr = (scr & ~mask) | new_value;
978 atmel_qspi_write(scr, aq, QSPI_SCR);
979 if (aq->caps->has_gclk)
980 return atmel_qspi_update_config(aq);
985 static int atmel_qspi_enable_clk(struct udevice *dev)
987 struct atmel_qspi *aq = dev_get_priv(dev);
988 struct clk pclk, qspick, gclk;
991 ret = clk_get_by_name(dev, "pclk", &pclk);
993 ret = clk_get_by_index(dev, 0, &pclk);
996 dev_err(dev, "Missing QSPI peripheral clock\n");
1000 ret = clk_enable(&pclk);
1002 dev_err(dev, "Failed to enable QSPI peripheral clock\n");
1006 if (aq->caps->has_qspick) {
1007 /* Get the QSPI system clock */
1008 ret = clk_get_by_name(dev, "qspick", &qspick);
1010 dev_err(dev, "Missing QSPI peripheral clock\n");
1014 ret = clk_enable(&qspick);
1016 dev_err(dev, "Failed to enable QSPI system clock\n");
1018 } else if (aq->caps->has_gclk) {
1019 ret = clk_get_by_name(dev, "gclk", &gclk);
1021 dev_err(dev, "Missing QSPI generic clock\n");
1025 ret = clk_enable(&gclk);
1027 dev_err(dev, "Failed to enable QSPI system clock\n");
1031 aq->bus_clk_rate = clk_get_rate(&pclk);
1032 if (!aq->bus_clk_rate)
1041 static int atmel_qspi_init(struct atmel_qspi *aq)
1045 if (aq->caps->has_gclk) {
1046 ret = atmel_qspi_reg_sync(aq);
1049 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
1053 /* Reset the QSPI controller */
1054 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
1056 /* Set the QSPI controller by default in Serial Memory Mode */
1057 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
1058 aq->mr = QSPI_MR_SMM;
1060 /* Enable the QSPI controller */
1061 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
1066 static const struct atmel_qspi_priv_ops atmel_qspi_priv_ops = {
1067 .set_cfg = atmel_qspi_set_cfg,
1068 .transfer = atmel_qspi_transfer,
1071 static const struct atmel_qspi_priv_ops atmel_qspi_sama7g5_priv_ops = {
1072 .set_cfg = atmel_qspi_sama7g5_set_cfg,
1073 .transfer = atmel_qspi_sama7g5_transfer,
1076 static int atmel_qspi_probe(struct udevice *dev)
1078 struct atmel_qspi *aq = dev_get_priv(dev);
1079 struct resource res;
1082 aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev);
1084 dev_err(dev, "Could not retrieve QSPI caps\n");
1088 if (aq->caps->has_gclk)
1089 aq->ops = &atmel_qspi_sama7g5_priv_ops;
1091 aq->ops = &atmel_qspi_priv_ops;
1093 /* Map the registers */
1094 ret = dev_read_resource_byname(dev, "qspi_base", &res);
1096 dev_err(dev, "missing registers\n");
1100 aq->regs = devm_ioremap(dev, res.start, resource_size(&res));
1101 if (IS_ERR(aq->regs))
1102 return PTR_ERR(aq->regs);
1104 /* Map the AHB memory */
1105 ret = dev_read_resource_byname(dev, "qspi_mmap", &res);
1107 dev_err(dev, "missing AHB memory\n");
1111 aq->mem = devm_ioremap(dev, res.start, resource_size(&res));
1112 if (IS_ERR(aq->mem))
1113 return PTR_ERR(aq->mem);
1115 aq->mmap_size = resource_size(&res);
1117 ret = atmel_qspi_enable_clk(dev);
1122 return atmel_qspi_init(aq);
1125 static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
1126 .supports_op = atmel_qspi_supports_op,
1127 .exec_op = atmel_qspi_exec_op,
1130 static const struct dm_spi_ops atmel_qspi_ops = {
1131 .set_speed = atmel_qspi_set_speed,
1132 .set_mode = atmel_qspi_set_mode,
1133 .mem_ops = &atmel_qspi_mem_ops,
1136 static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
1138 static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
1143 static const struct atmel_qspi_caps atmel_sama7g5_ospi_caps = {
1148 static const struct atmel_qspi_caps atmel_sama7g5_qspi_caps = {
1152 static const struct udevice_id atmel_qspi_ids[] = {
1154 .compatible = "atmel,sama5d2-qspi",
1155 .data = (ulong)&atmel_sama5d2_qspi_caps,
1158 .compatible = "microchip,sam9x60-qspi",
1159 .data = (ulong)&atmel_sam9x60_qspi_caps,
1162 .compatible = "microchip,sama7g5-ospi",
1163 .data = (ulong)&atmel_sama7g5_ospi_caps,
1166 .compatible = "microchip,sama7g5-qspi",
1167 .data = (ulong)&atmel_sama7g5_qspi_caps,
1172 U_BOOT_DRIVER(atmel_qspi) = {
1173 .name = "atmel_qspi",
1175 .of_match = atmel_qspi_ids,
1176 .ops = &atmel_qspi_ops,
1177 .priv_auto = sizeof(struct atmel_qspi),
1178 .probe = atmel_qspi_probe,