blob: 3efb661803b6c333cc10664811573aaaa0fbda2e [file] [log] [blame]
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Driver for Atmel QSPI Controller
4 *
5 * Copyright (C) 2015 Atmel Corporation
6 * Copyright (C) 2018 Cryptera A/S
7 *
8 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
9 * Author: Piotr Bugalski <bugalski.piotr@gmail.com>
10 */
11
Simon Glass9bc15642020-02-03 07:36:16 -070012#include <malloc.h>
Tudor Ambarus88151bb2019-06-18 08:51:50 +000013#include <asm/io.h>
14#include <clk.h>
Tudor Ambarus88151bb2019-06-18 08:51:50 +000015#include <dm.h>
16#include <errno.h>
17#include <fdtdec.h>
Simon Glass9bc15642020-02-03 07:36:16 -070018#include <dm/device_compat.h>
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020019#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <linux/err.h>
Tudor Ambarus88151bb2019-06-18 08:51:50 +000022#include <linux/io.h>
23#include <linux/iopoll.h>
24#include <linux/ioport.h>
25#include <mach/clk.h>
26#include <spi.h>
27#include <spi-mem.h>
28
29/* QSPI register offsets */
30#define QSPI_CR 0x0000 /* Control Register */
31#define QSPI_MR 0x0004 /* Mode Register */
32#define QSPI_RD 0x0008 /* Receive Data Register */
33#define QSPI_TD 0x000c /* Transmit Data Register */
34#define QSPI_SR 0x0010 /* Status Register */
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020035#define QSPI_SR2 0x0024 /* SAMA7G5 Status Register */
Tudor Ambarus88151bb2019-06-18 08:51:50 +000036#define QSPI_IER 0x0014 /* Interrupt Enable Register */
37#define QSPI_IDR 0x0018 /* Interrupt Disable Register */
38#define QSPI_IMR 0x001c /* Interrupt Mask Register */
39#define QSPI_SCR 0x0020 /* Serial Clock Register */
40
41#define QSPI_IAR 0x0030 /* Instruction Address Register */
42#define QSPI_ICR 0x0034 /* Instruction Code Register */
43#define QSPI_WICR 0x0034 /* Write Instruction Code Register */
44#define QSPI_IFR 0x0038 /* Instruction Frame Register */
45#define QSPI_RICR 0x003C /* Read Instruction Code Register */
46
47#define QSPI_SMR 0x0040 /* Scrambling Mode Register */
48#define QSPI_SKR 0x0044 /* Scrambling Key Register */
49
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020050#define QSPI_REFRESH 0x0050 /* Refresh Register */
51#define QSPI_WRACNT 0x0054 /* Write Access Counter Register */
52#define QSPI_DLLCFG 0x0058 /* DLL Configuration Register */
53#define QSPI_PCALCFG 0x005C /* Pad Calibration Configuration Register */
54#define QSPI_PCALBP 0x0060 /* Pad Calibration Bypass Register */
55#define QSPI_TOUT 0x0064 /* Timeout Register */
56
Tudor Ambarus88151bb2019-06-18 08:51:50 +000057#define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
58#define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
59
60#define QSPI_VERSION 0x00FC /* Version Register */
61
62/* Bitfields in QSPI_CR (Control Register) */
63#define QSPI_CR_QSPIEN BIT(0)
64#define QSPI_CR_QSPIDIS BIT(1)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020065#define QSPI_CR_DLLON BIT(2)
66#define QSPI_CR_DLLOFF BIT(3)
67#define QSPI_CR_STPCAL BIT(4)
68#define QSPI_CR_SRFRSH BIT(5)
Tudor Ambarus88151bb2019-06-18 08:51:50 +000069#define QSPI_CR_SWRST BIT(7)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020070#define QSPI_CR_UPDCFG BIT(8)
71#define QSPI_CR_STTFR BIT(9)
72#define QSPI_CR_RTOUT BIT(10)
Tudor Ambarus88151bb2019-06-18 08:51:50 +000073#define QSPI_CR_LASTXFER BIT(24)
74
75/* Bitfields in QSPI_MR (Mode Register) */
76#define QSPI_MR_SMM BIT(0)
77#define QSPI_MR_LLB BIT(1)
78#define QSPI_MR_WDRBT BIT(2)
79#define QSPI_MR_SMRM BIT(3)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020080#define QSPI_MR_DQSDLYEN BIT(3)
81
Tudor Ambarus88151bb2019-06-18 08:51:50 +000082#define QSPI_MR_CSMODE_MASK GENMASK(5, 4)
83#define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4)
84#define QSPI_MR_CSMODE_LASTXFER (1 << 4)
85#define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4)
86#define QSPI_MR_NBBITS_MASK GENMASK(11, 8)
87#define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020088#define QSPI_MR_OENSD BIT(15)
Tudor Ambarus88151bb2019-06-18 08:51:50 +000089#define QSPI_MR_DLYBCT_MASK GENMASK(23, 16)
90#define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK)
91#define QSPI_MR_DLYCS_MASK GENMASK(31, 24)
92#define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK)
93
94/* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */
95#define QSPI_SR_RDRF BIT(0)
96#define QSPI_SR_TDRE BIT(1)
97#define QSPI_SR_TXEMPTY BIT(2)
98#define QSPI_SR_OVRES BIT(3)
99#define QSPI_SR_CSR BIT(8)
100#define QSPI_SR_CSS BIT(9)
101#define QSPI_SR_INSTRE BIT(10)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200102#define QSPI_SR_LWRA BIT(11)
103#define QSPI_SR_QITF BIT(12)
104#define QSPI_SR_QITR BIT(13)
105#define QSPI_SR_CSFA BIT(14)
106#define QSPI_SR_CSRA BIT(15)
107#define QSPI_SR_RFRSHD BIT(16)
108#define QSPI_SR_TOUT BIT(17)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000109#define QSPI_SR_QSPIENS BIT(24)
110
111#define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
112
113/* Bitfields in QSPI_SCR (Serial Clock Register) */
114#define QSPI_SCR_CPOL BIT(0)
115#define QSPI_SCR_CPHA BIT(1)
116#define QSPI_SCR_SCBR_MASK GENMASK(15, 8)
117#define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK)
118#define QSPI_SCR_DLYBS_MASK GENMASK(23, 16)
119#define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK)
120
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200121/* Bitfields in QSPI_SR2 (SAMA7G5 Status Register) */
122#define QSPI_SR2_SYNCBSY BIT(0)
123#define QSPI_SR2_QSPIENS BIT(1)
124#define QSPI_SR2_CSS BIT(2)
125#define QSPI_SR2_RBUSY BIT(3)
126#define QSPI_SR2_HIDLE BIT(4)
127#define QSPI_SR2_DLOCK BIT(5)
128#define QSPI_SR2_CALBSY BIT(6)
129
130/* Bitfields in QSPI_IAR (Instruction Address Register) */
131#define QSPI_IAR_ADDR GENMASK(31, 0)
132
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000133/* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */
134#define QSPI_ICR_INST_MASK GENMASK(7, 0)
135#define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200136#define QSPI_ICR_INST_MASK_SAMA7G5 GENMASK(15, 0)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000137#define QSPI_ICR_OPT_MASK GENMASK(23, 16)
138#define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
139
140/* Bitfields in QSPI_IFR (Instruction Frame Register) */
141#define QSPI_IFR_WIDTH_MASK GENMASK(2, 0)
142#define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0)
143#define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0)
144#define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0)
145#define QSPI_IFR_WIDTH_DUAL_IO (3 << 0)
146#define QSPI_IFR_WIDTH_QUAD_IO (4 << 0)
147#define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0)
148#define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200149#define QSPI_IFR_WIDTH_OCT_OUTPUT (7 << 0)
150#define QSPI_IFR_WIDTH_OCT_IO (8 << 0)
151#define QSPI_IFR_WIDTH_OCT_CMD (9 << 0)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000152#define QSPI_IFR_INSTEN BIT(4)
153#define QSPI_IFR_ADDREN BIT(5)
154#define QSPI_IFR_OPTEN BIT(6)
155#define QSPI_IFR_DATAEN BIT(7)
156#define QSPI_IFR_OPTL_MASK GENMASK(9, 8)
157#define QSPI_IFR_OPTL_1BIT (0 << 8)
158#define QSPI_IFR_OPTL_2BIT (1 << 8)
159#define QSPI_IFR_OPTL_4BIT (2 << 8)
160#define QSPI_IFR_OPTL_8BIT (3 << 8)
161#define QSPI_IFR_ADDRL BIT(10)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200162#define QSPI_IFR_ADDRL_SAMA7G5 GENMASK(11, 10)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000163#define QSPI_IFR_TFRTYP_MEM BIT(12)
164#define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13)
165#define QSPI_IFR_CRM BIT(14)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200166#define QSPI_IFR_DDREN BIT(15)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000167#define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
168#define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200169#define QSPI_IFR_END BIT(22)
170#define QSPI_IFR_SMRM BIT(23)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000171#define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200172#define QSPI_IFR_DQSEN BIT(25)
173#define QSPI_IFR_DDRCMDEN BIT(26)
174#define QSPI_IFR_HFWBEN BIT(27)
175#define QSPI_IFR_PROTTYP GENMASK(29, 28)
176#define QSPI_IFR_PROTTYP_STD_SPI 0
177#define QSPI_IFR_PROTTYP_TWIN_QUAD 1
178#define QSPI_IFR_PROTTYP_OCTAFLASH 2
179#define QSPI_IFR_PROTTYP_HYPERFLASH 3
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000180
181/* Bitfields in QSPI_SMR (Scrambling Mode Register) */
182#define QSPI_SMR_SCREN BIT(0)
183#define QSPI_SMR_RVDIS BIT(1)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200184#define QSPI_SMR_SCRKL BIT(2)
185
186/* Bitfields in QSPI_REFRESH (Refresh Register) */
187#define QSPI_REFRESH_DELAY_COUNTER GENMASK(31, 0)
188
189/* Bitfields in QSPI_WRACNT (Write Access Counter Register) */
190#define QSPI_WRACNT_NBWRA GENMASK(31, 0)
191
192/* Bitfields in QSPI_DLLCFG (DLL Configuration Register) */
193#define QSPI_DLLCFG_RANGE BIT(0)
194
195/* Bitfields in QSPI_PCALCFG (DLL Pad Calibration Configuration Register) */
196#define QSPI_PCALCFG_AAON BIT(0)
197#define QSPI_PCALCFG_DAPCAL BIT(1)
198#define QSPI_PCALCFG_DIFFPM BIT(2)
199#define QSPI_PCALCFG_CLKDIV GENMASK(6, 4)
200#define QSPI_PCALCFG_CALCNT GENMASK(16, 8)
201#define QSPI_PCALCFG_CALP GENMASK(27, 24)
202#define QSPI_PCALCFG_CALN GENMASK(31, 28)
203
204/* Bitfields in QSPI_PCALBP (DLL Pad Calibration Bypass Register) */
205#define QSPI_PCALBP_BPEN BIT(0)
206#define QSPI_PCALBP_CALPBP GENMASK(11, 8)
207#define QSPI_PCALBP_CALNBP GENMASK(19, 16)
208
209/* Bitfields in QSPI_TOUT (Timeout Register) */
210#define QSPI_TOUT_TCNTM GENMASK(15, 0)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000211
212/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
213#define QSPI_WPMR_WPEN BIT(0)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200214#define QSPI_WPMR_WPITEN BIT(1)
215#define QSPI_WPMR_WPCREN BIT(2)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000216#define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
217#define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
218
219/* Bitfields in QSPI_WPSR (Write Protection Status Register) */
220#define QSPI_WPSR_WPVS BIT(0)
221#define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8)
222#define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC)
223
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200224#define ATMEL_QSPI_TIMEOUT 1000000 /* us */
225#define ATMEL_QSPI_SYNC_TIMEOUT 300000 /* us */
226#define QSPI_DLLCFG_THRESHOLD_FREQ 90000000U
227#define QSPI_TOUT_MAX 0xffff
228
229/**
230 * struct atmel_qspi_pcal - Pad Calibration Clock Division
231 * @pclk_rate: peripheral clock rate.
232 * @pclkdiv: calibration clock division. The clock applied to the calibration
233 * cell is divided by pclkdiv + 1.
234 */
235struct atmel_qspi_pcal {
236 u32 pclk_rate;
237 u8 pclk_div;
238};
239
240#define ATMEL_QSPI_PCAL_ARRAY_SIZE 8
241static const struct atmel_qspi_pcal pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE] = {
242 {25000000, 0},
243 {50000000, 1},
244 {75000000, 2},
245 {100000000, 3},
246 {125000000, 4},
247 {150000000, 5},
248 {175000000, 6},
249 {200000000, 7},
250};
251
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000252struct atmel_qspi_caps {
253 bool has_qspick;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200254 bool has_gclk;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000255 bool has_ricr;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200256 bool octal;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000257};
258
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200259struct atmel_qspi_priv_ops;
260
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000261struct atmel_qspi {
262 void __iomem *regs;
263 void __iomem *mem;
Tudor Ambarus678b8932020-03-20 09:37:59 +0000264 resource_size_t mmap_size;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000265 const struct atmel_qspi_caps *caps;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200266 const struct atmel_qspi_priv_ops *ops;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000267 struct udevice *dev;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000268 ulong bus_clk_rate;
269 u32 mr;
270};
271
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200272struct atmel_qspi_priv_ops {
273 int (*set_cfg)(struct atmel_qspi *aq, const struct spi_mem_op *op,
274 u32 *offset);
275 int (*transfer)(struct atmel_qspi *aq, const struct spi_mem_op *op,
276 u32 offset);
277};
278
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000279struct atmel_qspi_mode {
280 u8 cmd_buswidth;
281 u8 addr_buswidth;
282 u8 data_buswidth;
283 u32 config;
284};
285
286static const struct atmel_qspi_mode atmel_qspi_modes[] = {
287 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
288 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
289 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
290 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
291 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
292 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
293 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
294};
295
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200296static const struct atmel_qspi_mode atmel_qspi_sama7g5_modes[] = {
297 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
298 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
299 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
300 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
301 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
302 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
303 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
304 { 1, 1, 8, QSPI_IFR_WIDTH_OCT_OUTPUT },
305 { 1, 8, 8, QSPI_IFR_WIDTH_OCT_IO },
306 { 8, 8, 8, QSPI_IFR_WIDTH_OCT_CMD },
307};
308
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000309#ifdef VERBOSE_DEBUG
310static const char *atmel_qspi_reg_name(u32 offset, char *tmp, size_t sz)
311{
312 switch (offset) {
313 case QSPI_CR:
314 return "CR";
315 case QSPI_MR:
316 return "MR";
317 case QSPI_RD:
Tudor Ambarus7e022332021-11-03 18:45:42 +0200318 return "RD";
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000319 case QSPI_TD:
320 return "TD";
321 case QSPI_SR:
322 return "SR";
323 case QSPI_IER:
324 return "IER";
325 case QSPI_IDR:
326 return "IDR";
327 case QSPI_IMR:
328 return "IMR";
329 case QSPI_SCR:
330 return "SCR";
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200331 case QSPI_SR2:
332 return "SR2";
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000333 case QSPI_IAR:
334 return "IAR";
335 case QSPI_ICR:
336 return "ICR/WICR";
337 case QSPI_IFR:
338 return "IFR";
339 case QSPI_RICR:
340 return "RICR";
341 case QSPI_SMR:
342 return "SMR";
343 case QSPI_SKR:
344 return "SKR";
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200345 case QSPI_REFRESH:
346 return "REFRESH";
347 case QSPI_WRACNT:
348 return "WRACNT";
349 case QSPI_DLLCFG:
350 return "DLLCFG";
351 case QSPI_PCALCFG:
352 return "PCALCFG";
353 case QSPI_PCALBP:
354 return "PCALBP";
355 case QSPI_TOUT:
356 return "TOUT";
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000357 case QSPI_WPMR:
358 return "WPMR";
359 case QSPI_WPSR:
360 return "WPSR";
361 case QSPI_VERSION:
362 return "VERSION";
363 default:
364 snprintf(tmp, sz, "0x%02x", offset);
365 break;
366 }
367
368 return tmp;
369}
370#endif /* VERBOSE_DEBUG */
371
372static u32 atmel_qspi_read(struct atmel_qspi *aq, u32 offset)
373{
374 u32 value = readl(aq->regs + offset);
375
376#ifdef VERBOSE_DEBUG
377 char tmp[16];
378
379 dev_vdbg(aq->dev, "read 0x%08x from %s\n", value,
380 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
381#endif /* VERBOSE_DEBUG */
382
383 return value;
384}
385
386static void atmel_qspi_write(u32 value, struct atmel_qspi *aq, u32 offset)
387{
388#ifdef VERBOSE_DEBUG
389 char tmp[16];
390
391 dev_vdbg(aq->dev, "write 0x%08x into %s\n", value,
392 atmel_qspi_reg_name(offset, tmp, sizeof(tmp)));
393#endif /* VERBOSE_DEBUG */
394
395 writel(value, aq->regs + offset);
396}
397
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000398static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
399 const struct atmel_qspi_mode *mode)
400{
401 if (op->cmd.buswidth != mode->cmd_buswidth)
402 return false;
403
404 if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
405 return false;
406
407 if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
408 return false;
409
410 return true;
411}
412
413static int atmel_qspi_find_mode(const struct spi_mem_op *op)
414{
415 u32 i;
416
417 for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
418 if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
419 return i;
420
421 return -ENOTSUPP;
422}
423
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200424static int atmel_qspi_sama7g5_find_mode(const struct spi_mem_op *op)
425{
426 u32 i;
427
428 for (i = 0; i < ARRAY_SIZE(atmel_qspi_sama7g5_modes); i++)
429 if (atmel_qspi_is_compatible(op, &atmel_qspi_sama7g5_modes[i]))
430 return i;
431
432 return -EOPNOTSUPP;
433}
434
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000435static bool atmel_qspi_supports_op(struct spi_slave *slave,
436 const struct spi_mem_op *op)
437{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200438 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
439
Tudor Ambarus938a6c92022-04-08 11:40:26 +0300440 if (!spi_mem_default_supports_op(slave, op))
441 return false;
442
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200443 if (aq->caps->octal) {
444 if (atmel_qspi_sama7g5_find_mode(op) < 0)
445 return false;
446 else
447 return true;
448 }
449
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000450 if (atmel_qspi_find_mode(op) < 0)
451 return false;
452
453 /* special case not supported by hardware */
454 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
455 op->dummy.nbytes == 0)
456 return false;
457
458 return true;
459}
460
461static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
462 const struct spi_mem_op *op, u32 *offset)
463{
464 u32 iar, icr, ifr;
465 u32 dummy_cycles = 0;
466 int mode;
467
468 iar = 0;
469 icr = QSPI_ICR_INST(op->cmd.opcode);
470 ifr = QSPI_IFR_INSTEN;
471
472 mode = atmel_qspi_find_mode(op);
473 if (mode < 0)
474 return mode;
475 ifr |= atmel_qspi_modes[mode].config;
476
477 if (op->dummy.buswidth && op->dummy.nbytes)
478 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
479
480 /*
481 * The controller allows 24 and 32-bit addressing while NAND-flash
482 * requires 16-bit long. Handling 8-bit long addresses is done using
483 * the option field. For the 16-bit addresses, the workaround depends
484 * of the number of requested dummy bits. If there are 8 or more dummy
485 * cycles, the address is shifted and sent with the first dummy byte.
486 * Otherwise opcode is disabled and the first byte of the address
487 * contains the command opcode (works only if the opcode and address
488 * use the same buswidth). The limitation is when the 16-bit address is
489 * used without enough dummy cycles and the opcode is using a different
490 * buswidth than the address.
491 */
492 if (op->addr.buswidth) {
493 switch (op->addr.nbytes) {
494 case 0:
495 break;
496 case 1:
497 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
498 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
499 break;
500 case 2:
501 if (dummy_cycles < 8 / op->addr.buswidth) {
502 ifr &= ~QSPI_IFR_INSTEN;
503 ifr |= QSPI_IFR_ADDREN;
504 iar = (op->cmd.opcode << 16) |
505 (op->addr.val & 0xffff);
506 } else {
507 ifr |= QSPI_IFR_ADDREN;
508 iar = (op->addr.val << 8) & 0xffffff;
509 dummy_cycles -= 8 / op->addr.buswidth;
510 }
511 break;
512 case 3:
513 ifr |= QSPI_IFR_ADDREN;
514 iar = op->addr.val & 0xffffff;
515 break;
516 case 4:
517 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
518 iar = op->addr.val & 0x7ffffff;
519 break;
520 default:
521 return -ENOTSUPP;
522 }
523 }
524
525 /* offset of the data access in the QSPI memory space */
526 *offset = iar;
527
528 /* Set number of dummy cycles */
529 if (dummy_cycles)
530 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
531
532 /* Set data enable */
533 if (op->data.nbytes)
534 ifr |= QSPI_IFR_DATAEN;
535
536 /*
537 * If the QSPI controller is set in regular SPI mode, set it in
538 * Serial Memory Mode (SMM).
539 */
540 if (aq->mr != QSPI_MR_SMM) {
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000541 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000542 aq->mr = QSPI_MR_SMM;
543 }
544
545 /* Clear pending interrupts */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000546 (void)atmel_qspi_read(aq, QSPI_SR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000547
548 if (aq->caps->has_ricr) {
549 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
550 ifr |= QSPI_IFR_APBTFRTYP_READ;
551
552 /* Set QSPI Instruction Frame registers */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000553 atmel_qspi_write(iar, aq, QSPI_IAR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000554 if (op->data.dir == SPI_MEM_DATA_IN)
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000555 atmel_qspi_write(icr, aq, QSPI_RICR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000556 else
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000557 atmel_qspi_write(icr, aq, QSPI_WICR);
558 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000559 } else {
560 if (op->data.dir == SPI_MEM_DATA_OUT)
561 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
562
563 /* Set QSPI Instruction Frame registers */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000564 atmel_qspi_write(iar, aq, QSPI_IAR);
565 atmel_qspi_write(icr, aq, QSPI_ICR);
566 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000567 }
568
569 return 0;
570}
571
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200572static int atmel_qspi_transfer(struct atmel_qspi *aq,
573 const struct spi_mem_op *op, u32 offset)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000574{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200575 u32 sr, imr;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000576
577 /* Skip to the final steps if there is no data */
578 if (op->data.nbytes) {
579 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000580 (void)atmel_qspi_read(aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000581
582 /* Send/Receive data */
583 if (op->data.dir == SPI_MEM_DATA_IN)
584 memcpy_fromio(op->data.buf.in, aq->mem + offset,
585 op->data.nbytes);
586 else
587 memcpy_toio(aq->mem + offset, op->data.buf.out,
588 op->data.nbytes);
589
590 /* Release the chip-select */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000591 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000592 }
593
594 /* Poll INSTruction End and Chip Select Rise flags. */
595 imr = QSPI_SR_INSTRE | QSPI_SR_CSR;
596 return readl_poll_timeout(aq->regs + QSPI_SR, sr, (sr & imr) == imr,
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200597 ATMEL_QSPI_TIMEOUT);
598}
599
600static int atmel_qspi_reg_sync(struct atmel_qspi *aq)
601{
602 u32 val;
603
604 return readl_poll_timeout(aq->regs + QSPI_SR2, val,
605 !(val & QSPI_SR2_SYNCBSY),
606 ATMEL_QSPI_SYNC_TIMEOUT);
607}
608
609static int atmel_qspi_update_config(struct atmel_qspi *aq)
610{
611 int ret;
612
613 ret = atmel_qspi_reg_sync(aq);
614 if (ret)
615 return ret;
616 atmel_qspi_write(QSPI_CR_UPDCFG, aq, QSPI_CR);
617 return atmel_qspi_reg_sync(aq);
618}
619
620static int atmel_qspi_sama7g5_set_cfg(struct atmel_qspi *aq,
621 const struct spi_mem_op *op, u32 *offset)
622{
623 u32 iar, icr, ifr;
624 int mode, ret;
625
626 iar = 0;
627 icr = FIELD_PREP(QSPI_ICR_INST_MASK_SAMA7G5, op->cmd.opcode);
628 ifr = QSPI_IFR_INSTEN;
629
630 mode = atmel_qspi_sama7g5_find_mode(op);
631 if (mode < 0)
632 return mode;
633 ifr |= atmel_qspi_sama7g5_modes[mode].config;
634
635 if (op->dummy.buswidth && op->dummy.nbytes) {
636 if (op->addr.dtr && op->dummy.dtr && op->data.dtr)
637 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
638 (2 * op->dummy.buswidth));
639 else
640 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
641 op->dummy.buswidth);
642 }
643
644 if (op->addr.buswidth && op->addr.nbytes) {
645 ifr |= FIELD_PREP(QSPI_IFR_ADDRL_SAMA7G5, op->addr.nbytes - 1) |
646 QSPI_IFR_ADDREN;
647 iar = FIELD_PREP(QSPI_IAR_ADDR, op->addr.val);
648 }
649
650 if (op->addr.dtr && op->dummy.dtr && op->data.dtr) {
651 ifr |= QSPI_IFR_DDREN;
652 if (op->cmd.dtr)
653 ifr |= QSPI_IFR_DDRCMDEN;
654 ifr |= QSPI_IFR_DQSEN;
655 }
656
657 if (op->cmd.buswidth == 8 || op->addr.buswidth == 8 ||
658 op->data.buswidth == 8)
659 ifr |= FIELD_PREP(QSPI_IFR_PROTTYP, QSPI_IFR_PROTTYP_OCTAFLASH);
660
661 /* offset of the data access in the QSPI memory space */
662 *offset = iar;
663
664 /* Set data enable */
665 if (op->data.nbytes) {
666 ifr |= QSPI_IFR_DATAEN;
667 if (op->addr.nbytes)
668 ifr |= QSPI_IFR_TFRTYP_MEM;
669 }
670
671 /*
672 * If the QSPI controller is set in regular SPI mode, set it in
673 * Serial Memory Mode (SMM).
674 */
675 if (aq->mr != QSPI_MR_SMM) {
676 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
677 ret = atmel_qspi_update_config(aq);
678 if (ret)
679 return ret;
680 aq->mr = QSPI_MR_SMM;
681 }
682
683 /* Clear pending interrupts */
684 (void)atmel_qspi_read(aq, QSPI_SR);
685
686 /* Set QSPI Instruction Frame registers */
687 if (op->addr.nbytes && !op->data.nbytes)
688 atmel_qspi_write(iar, aq, QSPI_IAR);
689
690 if (op->data.dir == SPI_MEM_DATA_IN) {
691 atmel_qspi_write(icr, aq, QSPI_RICR);
692 } else {
693 atmel_qspi_write(icr, aq, QSPI_WICR);
694 if (op->data.nbytes)
695 atmel_qspi_write(FIELD_PREP(QSPI_WRACNT_NBWRA,
696 op->data.nbytes),
697 aq, QSPI_WRACNT);
698 }
699
700 atmel_qspi_write(ifr, aq, QSPI_IFR);
701
702 return atmel_qspi_update_config(aq);
703}
704
705static int atmel_qspi_sama7g5_transfer(struct atmel_qspi *aq,
706 const struct spi_mem_op *op, u32 offset)
707{
708 int err;
709 u32 val;
710
711 if (!op->data.nbytes) {
712 /* Start the transfer. */
713 err = atmel_qspi_reg_sync(aq);
714 if (err)
715 return err;
716 atmel_qspi_write(QSPI_CR_STTFR, aq, QSPI_CR);
717
718 return readl_poll_timeout(aq->regs + QSPI_SR, val,
719 val & QSPI_SR_CSRA,
720 ATMEL_QSPI_TIMEOUT);
721 }
722
723 /* Send/Receive data. */
724 if (op->data.dir == SPI_MEM_DATA_IN) {
725 memcpy_fromio(op->data.buf.in, aq->mem + offset,
726 op->data.nbytes);
727
728 if (op->addr.nbytes) {
729 err = readl_poll_timeout(aq->regs + QSPI_SR2, val,
730 !(val & QSPI_SR2_RBUSY),
731 ATMEL_QSPI_SYNC_TIMEOUT);
732 if (err)
733 return err;
734 }
735 } else {
736 memcpy_toio(aq->mem + offset, op->data.buf.out,
737 op->data.nbytes);
738
739 err = readl_poll_timeout(aq->regs + QSPI_SR, val,
740 val & QSPI_SR_LWRA,
741 ATMEL_QSPI_TIMEOUT);
742 if (err)
743 return err;
744 }
745
746 /* Release the chip-select. */
747 err = atmel_qspi_reg_sync(aq);
748 if (err)
749 return err;
750 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
751
752 return readl_poll_timeout(aq->regs + QSPI_SR, val, val & QSPI_SR_CSRA,
753 ATMEL_QSPI_TIMEOUT);
754}
755
756static int atmel_qspi_exec_op(struct spi_slave *slave,
757 const struct spi_mem_op *op)
758{
759 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
760 u32 offset;
761 int err;
762
763 /*
764 * Check if the address exceeds the MMIO window size. An improvement
765 * would be to add support for regular SPI mode and fall back to it
766 * when the flash memories overrun the controller's memory space.
767 */
768 if (op->addr.val + op->data.nbytes > aq->mmap_size)
769 return -ENOTSUPP;
770
771 if (op->addr.nbytes > 4)
772 return -EOPNOTSUPP;
773
774 err = aq->ops->set_cfg(aq, op, &offset);
775 if (err)
776 return err;
777
778 return aq->ops->transfer(aq, op, offset);
779}
780
781static int atmel_qspi_set_pad_calibration(struct udevice *bus, uint hz)
782{
783 struct atmel_qspi *aq = dev_get_priv(bus);
784 u32 status, val;
785 int i, ret;
786 u8 pclk_div = 0;
787
788 for (i = 0; i < ATMEL_QSPI_PCAL_ARRAY_SIZE; i++) {
789 if (aq->bus_clk_rate <= pcal[i].pclk_rate) {
790 pclk_div = pcal[i].pclk_div;
791 break;
792 }
793 }
794
795 /*
796 * Use the biggest divider in case the peripheral clock exceeds
797 * 200MHZ.
798 */
799 if (aq->bus_clk_rate > pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_rate)
800 pclk_div = pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_div;
801
802 /* Disable QSPI while configuring the pad calibration. */
803 status = atmel_qspi_read(aq, QSPI_SR2);
804 if (status & QSPI_SR2_QSPIENS) {
805 ret = atmel_qspi_reg_sync(aq);
806 if (ret)
807 return ret;
808 atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
809 }
810
811 /*
812 * The analog circuitry is not shut down at the end of the calibration
813 * and the start-up time is only required for the first calibration
814 * sequence, thus increasing performance. Set the delay between the Pad
815 * calibration analog circuitry and the calibration request to 2us.
816 */
817 atmel_qspi_write(QSPI_PCALCFG_AAON |
818 FIELD_PREP(QSPI_PCALCFG_CLKDIV, pclk_div) |
819 FIELD_PREP(QSPI_PCALCFG_CALCNT,
820 2 * (aq->bus_clk_rate / 1000000)),
821 aq, QSPI_PCALCFG);
822
823 /* DLL On + start calibration. */
824 atmel_qspi_write(QSPI_CR_DLLON | QSPI_CR_STPCAL, aq, QSPI_CR);
825 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
826 (val & QSPI_SR2_DLOCK) &&
827 !(val & QSPI_SR2_CALBSY),
828 ATMEL_QSPI_TIMEOUT);
829
830 /* Refresh analogic blocks every 1 ms.*/
831 atmel_qspi_write(FIELD_PREP(QSPI_REFRESH_DELAY_COUNTER, hz / 1000),
832 aq, QSPI_REFRESH);
833
834 return ret;
835}
836
837static int atmel_qspi_set_gclk(struct udevice *bus, uint hz)
838{
839 struct atmel_qspi *aq = dev_get_priv(bus);
840 struct clk gclk;
841 u32 status, val;
842 int ret;
843
844 /* Disable DLL before setting GCLK */
845 status = atmel_qspi_read(aq, QSPI_SR2);
846 if (status & QSPI_SR2_DLOCK) {
847 atmel_qspi_write(QSPI_CR_DLLOFF, aq, QSPI_CR);
848 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
849 !(val & QSPI_SR2_DLOCK),
850 ATMEL_QSPI_TIMEOUT);
851 if (ret)
852 return ret;
853 }
854
855 if (hz > QSPI_DLLCFG_THRESHOLD_FREQ)
856 atmel_qspi_write(QSPI_DLLCFG_RANGE, aq, QSPI_DLLCFG);
857 else
858 atmel_qspi_write(0, aq, QSPI_DLLCFG);
859
860 ret = clk_get_by_name(bus, "gclk", &gclk);
861 if (ret) {
862 dev_err(bus, "Missing QSPI generic clock\n");
863 return ret;
864 }
865
866 ret = clk_disable(&gclk);
867 if (ret)
868 dev_err(bus, "Failed to disable QSPI generic clock\n");
869
870 ret = clk_set_rate(&gclk, hz);
871 if (ret < 0) {
872 dev_err(bus, "Failed to set generic clock rate.\n");
873 return ret;
874 }
875
876 ret = clk_enable(&gclk);
877 if (ret)
878 dev_err(bus, "Failed to enable QSPI generic clock\n");
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200879
880 return ret;
881}
882
883static int atmel_qspi_sama7g5_set_speed(struct udevice *bus, uint hz)
884{
885 struct atmel_qspi *aq = dev_get_priv(bus);
886 u32 val;
887 int ret;
888
889 ret = atmel_qspi_set_gclk(bus, hz);
890 if (ret)
891 return ret;
892
893 if (aq->caps->octal) {
894 ret = atmel_qspi_set_pad_calibration(bus, hz);
895 if (ret)
896 return ret;
897 } else {
898 atmel_qspi_write(QSPI_CR_DLLON, aq, QSPI_CR);
899 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
900 val & QSPI_SR2_DLOCK,
901 ATMEL_QSPI_TIMEOUT);
902 }
903
904 /* Set the QSPI controller by default in Serial Memory Mode */
905 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
906 ret = atmel_qspi_update_config(aq);
907 if (ret)
908 return ret;
909 aq->mr = QSPI_MR_SMM;
910
911 /* Enable the QSPI controller. */
912 ret = atmel_qspi_reg_sync(aq);
913 if (ret)
914 return ret;
915 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
916 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
917 val & QSPI_SR2_QSPIENS,
918 ATMEL_QSPI_SYNC_TIMEOUT);
919 if (ret)
920 return ret;
921
922 if (aq->caps->octal)
923 ret = readl_poll_timeout(aq->regs + QSPI_SR, val,
924 val & QSPI_SR_RFRSHD,
925 ATMEL_QSPI_TIMEOUT);
926
927 atmel_qspi_write(FIELD_PREP(QSPI_TOUT_TCNTM, QSPI_TOUT_MAX),
928 aq, QSPI_TOUT);
929
930 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000931}
932
933static int atmel_qspi_set_speed(struct udevice *bus, uint hz)
934{
935 struct atmel_qspi *aq = dev_get_priv(bus);
936 u32 scr, scbr, mask, new_value;
937
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200938 if (aq->caps->has_gclk)
939 return atmel_qspi_sama7g5_set_speed(bus, hz);
940
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000941 /* Compute the QSPI baudrate */
942 scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz);
943 if (scbr > 0)
944 scbr--;
945
946 new_value = QSPI_SCR_SCBR(scbr);
947 mask = QSPI_SCR_SCBR_MASK;
948
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000949 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000950 if ((scr & mask) == new_value)
951 return 0;
952
953 scr = (scr & ~mask) | new_value;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000954 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000955
956 return 0;
957}
958
959static int atmel_qspi_set_mode(struct udevice *bus, uint mode)
960{
961 struct atmel_qspi *aq = dev_get_priv(bus);
962 u32 scr, mask, new_value = 0;
963
964 if (mode & SPI_CPOL)
965 new_value = QSPI_SCR_CPOL;
966 if (mode & SPI_CPHA)
967 new_value = QSPI_SCR_CPHA;
968
969 mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA;
970
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000971 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000972 if ((scr & mask) == new_value)
973 return 0;
974
975 scr = (scr & ~mask) | new_value;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000976 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200977 if (aq->caps->has_gclk)
978 return atmel_qspi_update_config(aq);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000979
980 return 0;
981}
982
983static int atmel_qspi_enable_clk(struct udevice *dev)
984{
985 struct atmel_qspi *aq = dev_get_priv(dev);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200986 struct clk pclk, qspick, gclk;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000987 int ret;
988
989 ret = clk_get_by_name(dev, "pclk", &pclk);
990 if (ret)
991 ret = clk_get_by_index(dev, 0, &pclk);
992
993 if (ret) {
994 dev_err(dev, "Missing QSPI peripheral clock\n");
995 return ret;
996 }
997
998 ret = clk_enable(&pclk);
999 if (ret) {
1000 dev_err(dev, "Failed to enable QSPI peripheral clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001001 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001002 }
1003
1004 if (aq->caps->has_qspick) {
1005 /* Get the QSPI system clock */
1006 ret = clk_get_by_name(dev, "qspick", &qspick);
1007 if (ret) {
1008 dev_err(dev, "Missing QSPI peripheral clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001009 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001010 }
1011
1012 ret = clk_enable(&qspick);
1013 if (ret)
1014 dev_err(dev, "Failed to enable QSPI system clock\n");
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001015 } else if (aq->caps->has_gclk) {
1016 ret = clk_get_by_name(dev, "gclk", &gclk);
1017 if (ret) {
1018 dev_err(dev, "Missing QSPI generic clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001019 return ret;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001020 }
1021
1022 ret = clk_enable(&gclk);
1023 if (ret)
1024 dev_err(dev, "Failed to enable QSPI system clock\n");
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001025 }
1026
1027 aq->bus_clk_rate = clk_get_rate(&pclk);
1028 if (!aq->bus_clk_rate)
Sean Andersond318eb32023-12-16 14:38:42 -05001029 return -EINVAL;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001030
1031 return ret;
1032}
1033
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001034static int atmel_qspi_init(struct atmel_qspi *aq)
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001035{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001036 int ret;
1037
1038 if (aq->caps->has_gclk) {
1039 ret = atmel_qspi_reg_sync(aq);
1040 if (ret)
1041 return ret;
1042 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
1043 return 0;
1044 }
1045
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001046 /* Reset the QSPI controller */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001047 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001048
1049 /* Set the QSPI controller by default in Serial Memory Mode */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001050 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001051 aq->mr = QSPI_MR_SMM;
1052
1053 /* Enable the QSPI controller */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001054 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001055
1056 return 0;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001057}
1058
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001059static const struct atmel_qspi_priv_ops atmel_qspi_priv_ops = {
1060 .set_cfg = atmel_qspi_set_cfg,
1061 .transfer = atmel_qspi_transfer,
1062};
1063
1064static const struct atmel_qspi_priv_ops atmel_qspi_sama7g5_priv_ops = {
1065 .set_cfg = atmel_qspi_sama7g5_set_cfg,
1066 .transfer = atmel_qspi_sama7g5_transfer,
1067};
1068
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001069static int atmel_qspi_probe(struct udevice *dev)
1070{
1071 struct atmel_qspi *aq = dev_get_priv(dev);
1072 struct resource res;
1073 int ret;
1074
1075 aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev);
1076 if (!aq->caps) {
1077 dev_err(dev, "Could not retrieve QSPI caps\n");
1078 return -EINVAL;
1079 };
1080
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001081 if (aq->caps->has_gclk)
1082 aq->ops = &atmel_qspi_sama7g5_priv_ops;
1083 else
1084 aq->ops = &atmel_qspi_priv_ops;
1085
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001086 /* Map the registers */
1087 ret = dev_read_resource_byname(dev, "qspi_base", &res);
1088 if (ret) {
1089 dev_err(dev, "missing registers\n");
1090 return ret;
1091 }
1092
1093 aq->regs = devm_ioremap(dev, res.start, resource_size(&res));
1094 if (IS_ERR(aq->regs))
1095 return PTR_ERR(aq->regs);
1096
1097 /* Map the AHB memory */
1098 ret = dev_read_resource_byname(dev, "qspi_mmap", &res);
1099 if (ret) {
1100 dev_err(dev, "missing AHB memory\n");
1101 return ret;
1102 }
1103
1104 aq->mem = devm_ioremap(dev, res.start, resource_size(&res));
1105 if (IS_ERR(aq->mem))
1106 return PTR_ERR(aq->mem);
1107
Tudor Ambarus678b8932020-03-20 09:37:59 +00001108 aq->mmap_size = resource_size(&res);
1109
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001110 ret = atmel_qspi_enable_clk(dev);
1111 if (ret)
1112 return ret;
1113
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001114 aq->dev = dev;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001115 return atmel_qspi_init(aq);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001116}
1117
1118static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
1119 .supports_op = atmel_qspi_supports_op,
1120 .exec_op = atmel_qspi_exec_op,
1121};
1122
1123static const struct dm_spi_ops atmel_qspi_ops = {
1124 .set_speed = atmel_qspi_set_speed,
1125 .set_mode = atmel_qspi_set_mode,
1126 .mem_ops = &atmel_qspi_mem_ops,
1127};
1128
1129static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
1130
1131static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
1132 .has_qspick = true,
1133 .has_ricr = true,
1134};
1135
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001136static const struct atmel_qspi_caps atmel_sama7g5_ospi_caps = {
1137 .has_gclk = true,
1138 .octal = true,
1139};
1140
1141static const struct atmel_qspi_caps atmel_sama7g5_qspi_caps = {
1142 .has_gclk = true,
1143};
1144
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001145static const struct udevice_id atmel_qspi_ids[] = {
1146 {
1147 .compatible = "atmel,sama5d2-qspi",
1148 .data = (ulong)&atmel_sama5d2_qspi_caps,
1149 },
1150 {
1151 .compatible = "microchip,sam9x60-qspi",
1152 .data = (ulong)&atmel_sam9x60_qspi_caps,
1153 },
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001154 {
1155 .compatible = "microchip,sama7g5-ospi",
1156 .data = (ulong)&atmel_sama7g5_ospi_caps,
1157 },
1158 {
1159 .compatible = "microchip,sama7g5-qspi",
1160 .data = (ulong)&atmel_sama7g5_qspi_caps,
1161 },
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001162 { /* sentinel */ }
1163};
1164
1165U_BOOT_DRIVER(atmel_qspi) = {
1166 .name = "atmel_qspi",
1167 .id = UCLASS_SPI,
1168 .of_match = atmel_qspi_ids,
1169 .ops = &atmel_qspi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001170 .priv_auto = sizeof(struct atmel_qspi),
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001171 .probe = atmel_qspi_probe,
1172};