blob: 098298336da31dd50a8b580a97e42d37dc577d19 [file] [log] [blame]
Tudor Ambarus24c8ff42019-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 Glass336d4612020-02-03 07:36:16 -070012#include <malloc.h>
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000013#include <asm/io.h>
14#include <clk.h>
15#include <common.h>
16#include <dm.h>
17#include <errno.h>
18#include <fdtdec.h>
Simon Glass336d4612020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020020#include <linux/bitfield.h>
Simon Glasscd93d622020-05-10 11:40:13 -060021#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070022#include <linux/err.h>
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000023#include <linux/io.h>
24#include <linux/iopoll.h>
25#include <linux/ioport.h>
26#include <mach/clk.h>
27#include <spi.h>
28#include <spi-mem.h>
29
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 */
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020036#define QSPI_SR2 0x0024 /* SAMA7G5 Status Register */
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000037#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 */
41
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 */
47
48#define QSPI_SMR 0x0040 /* Scrambling Mode Register */
49#define QSPI_SKR 0x0044 /* Scrambling Key Register */
50
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020051#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 */
57
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000058#define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
59#define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
60
61#define QSPI_VERSION 0x00FC /* Version Register */
62
63/* Bitfields in QSPI_CR (Control Register) */
64#define QSPI_CR_QSPIEN BIT(0)
65#define QSPI_CR_QSPIDIS BIT(1)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020066#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)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000070#define QSPI_CR_SWRST BIT(7)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020071#define QSPI_CR_UPDCFG BIT(8)
72#define QSPI_CR_STTFR BIT(9)
73#define QSPI_CR_RTOUT BIT(10)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000074#define QSPI_CR_LASTXFER BIT(24)
75
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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020081#define QSPI_MR_DQSDLYEN BIT(3)
82
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000083#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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +020089#define QSPI_MR_OENSD BIT(15)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +000090#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)
94
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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200103#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)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000110#define QSPI_SR_QSPIENS BIT(24)
111
112#define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
113
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)
121
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200122/* 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)
130
131/* Bitfields in QSPI_IAR (Instruction Address Register) */
132#define QSPI_IAR_ADDR GENMASK(31, 0)
133
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000134/* 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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200137#define QSPI_ICR_INST_MASK_SAMA7G5 GENMASK(15, 0)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000138#define QSPI_ICR_OPT_MASK GENMASK(23, 16)
139#define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
140
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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200150#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)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000153#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)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200163#define QSPI_IFR_ADDRL_SAMA7G5 GENMASK(11, 10)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000164#define QSPI_IFR_TFRTYP_MEM BIT(12)
165#define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13)
166#define QSPI_IFR_CRM BIT(14)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200167#define QSPI_IFR_DDREN BIT(15)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000168#define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
169#define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200170#define QSPI_IFR_END BIT(22)
171#define QSPI_IFR_SMRM BIT(23)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000172#define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200173#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
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000181
182/* Bitfields in QSPI_SMR (Scrambling Mode Register) */
183#define QSPI_SMR_SCREN BIT(0)
184#define QSPI_SMR_RVDIS BIT(1)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200185#define QSPI_SMR_SCRKL BIT(2)
186
187/* Bitfields in QSPI_REFRESH (Refresh Register) */
188#define QSPI_REFRESH_DELAY_COUNTER GENMASK(31, 0)
189
190/* Bitfields in QSPI_WRACNT (Write Access Counter Register) */
191#define QSPI_WRACNT_NBWRA GENMASK(31, 0)
192
193/* Bitfields in QSPI_DLLCFG (DLL Configuration Register) */
194#define QSPI_DLLCFG_RANGE BIT(0)
195
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)
204
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)
209
210/* Bitfields in QSPI_TOUT (Timeout Register) */
211#define QSPI_TOUT_TCNTM GENMASK(15, 0)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000212
213/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
214#define QSPI_WPMR_WPEN BIT(0)
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200215#define QSPI_WPMR_WPITEN BIT(1)
216#define QSPI_WPMR_WPCREN BIT(2)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000217#define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
218#define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
219
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)
224
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200225#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
229
230/**
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.
235 */
236struct atmel_qspi_pcal {
237 u32 pclk_rate;
238 u8 pclk_div;
239};
240
241#define ATMEL_QSPI_PCAL_ARRAY_SIZE 8
242static const struct atmel_qspi_pcal pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE] = {
243 {25000000, 0},
244 {50000000, 1},
245 {75000000, 2},
246 {100000000, 3},
247 {125000000, 4},
248 {150000000, 5},
249 {175000000, 6},
250 {200000000, 7},
251};
252
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000253struct atmel_qspi_caps {
254 bool has_qspick;
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200255 bool has_gclk;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000256 bool has_ricr;
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200257 bool octal;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000258};
259
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200260struct atmel_qspi_priv_ops;
261
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000262struct atmel_qspi {
263 void __iomem *regs;
264 void __iomem *mem;
Tudor Ambarusba03a6c2020-03-20 09:37:59 +0000265 resource_size_t mmap_size;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000266 const struct atmel_qspi_caps *caps;
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200267 const struct atmel_qspi_priv_ops *ops;
Tudor Ambarus52e25652020-03-20 09:37:59 +0000268 struct udevice *dev;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000269 ulong bus_clk_rate;
270 u32 mr;
271};
272
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200273struct atmel_qspi_priv_ops {
274 int (*set_cfg)(struct atmel_qspi *aq, const struct spi_mem_op *op,
275 u32 *offset);
276 int (*transfer)(struct atmel_qspi *aq, const struct spi_mem_op *op,
277 u32 offset);
278};
279
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000280struct atmel_qspi_mode {
281 u8 cmd_buswidth;
282 u8 addr_buswidth;
283 u8 data_buswidth;
284 u32 config;
285};
286
287static 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 },
295};
296
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200297static 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 },
308};
309
Tudor Ambarus52e25652020-03-20 09:37:59 +0000310#ifdef VERBOSE_DEBUG
311static const char *atmel_qspi_reg_name(u32 offset, char *tmp, size_t sz)
312{
313 switch (offset) {
314 case QSPI_CR:
315 return "CR";
316 case QSPI_MR:
317 return "MR";
318 case QSPI_RD:
Tudor Ambarus7dc48b42021-11-03 18:45:42 +0200319 return "RD";
Tudor Ambarus52e25652020-03-20 09:37:59 +0000320 case QSPI_TD:
321 return "TD";
322 case QSPI_SR:
323 return "SR";
324 case QSPI_IER:
325 return "IER";
326 case QSPI_IDR:
327 return "IDR";
328 case QSPI_IMR:
329 return "IMR";
330 case QSPI_SCR:
331 return "SCR";
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200332 case QSPI_SR2:
333 return "SR2";
Tudor Ambarus52e25652020-03-20 09:37:59 +0000334 case QSPI_IAR:
335 return "IAR";
336 case QSPI_ICR:
337 return "ICR/WICR";
338 case QSPI_IFR:
339 return "IFR";
340 case QSPI_RICR:
341 return "RICR";
342 case QSPI_SMR:
343 return "SMR";
344 case QSPI_SKR:
345 return "SKR";
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200346 case QSPI_REFRESH:
347 return "REFRESH";
348 case QSPI_WRACNT:
349 return "WRACNT";
350 case QSPI_DLLCFG:
351 return "DLLCFG";
352 case QSPI_PCALCFG:
353 return "PCALCFG";
354 case QSPI_PCALBP:
355 return "PCALBP";
356 case QSPI_TOUT:
357 return "TOUT";
Tudor Ambarus52e25652020-03-20 09:37:59 +0000358 case QSPI_WPMR:
359 return "WPMR";
360 case QSPI_WPSR:
361 return "WPSR";
362 case QSPI_VERSION:
363 return "VERSION";
364 default:
365 snprintf(tmp, sz, "0x%02x", offset);
366 break;
367 }
368
369 return tmp;
370}
371#endif /* VERBOSE_DEBUG */
372
373static u32 atmel_qspi_read(struct atmel_qspi *aq, u32 offset)
374{
375 u32 value = readl(aq->regs + offset);
376
377#ifdef VERBOSE_DEBUG
378 char tmp[16];
379
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 */
383
384 return value;
385}
386
387static void atmel_qspi_write(u32 value, struct atmel_qspi *aq, u32 offset)
388{
389#ifdef VERBOSE_DEBUG
390 char tmp[16];
391
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 */
395
396 writel(value, aq->regs + offset);
397}
398
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000399static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
400 const struct atmel_qspi_mode *mode)
401{
402 if (op->cmd.buswidth != mode->cmd_buswidth)
403 return false;
404
405 if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
406 return false;
407
408 if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
409 return false;
410
411 return true;
412}
413
414static int atmel_qspi_find_mode(const struct spi_mem_op *op)
415{
416 u32 i;
417
418 for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
419 if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
420 return i;
421
422 return -ENOTSUPP;
423}
424
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200425static int atmel_qspi_sama7g5_find_mode(const struct spi_mem_op *op)
426{
427 u32 i;
428
429 for (i = 0; i < ARRAY_SIZE(atmel_qspi_sama7g5_modes); i++)
430 if (atmel_qspi_is_compatible(op, &atmel_qspi_sama7g5_modes[i]))
431 return i;
432
433 return -EOPNOTSUPP;
434}
435
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000436static bool atmel_qspi_supports_op(struct spi_slave *slave,
437 const struct spi_mem_op *op)
438{
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200439 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
440
441 if (aq->caps->octal) {
442 if (atmel_qspi_sama7g5_find_mode(op) < 0)
443 return false;
444 else
445 return true;
446 }
447
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000448 if (atmel_qspi_find_mode(op) < 0)
449 return false;
450
451 /* special case not supported by hardware */
452 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
453 op->dummy.nbytes == 0)
454 return false;
455
456 return true;
457}
458
459static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
460 const struct spi_mem_op *op, u32 *offset)
461{
462 u32 iar, icr, ifr;
463 u32 dummy_cycles = 0;
464 int mode;
465
466 iar = 0;
467 icr = QSPI_ICR_INST(op->cmd.opcode);
468 ifr = QSPI_IFR_INSTEN;
469
470 mode = atmel_qspi_find_mode(op);
471 if (mode < 0)
472 return mode;
473 ifr |= atmel_qspi_modes[mode].config;
474
475 if (op->dummy.buswidth && op->dummy.nbytes)
476 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
477
478 /*
479 * The controller allows 24 and 32-bit addressing while NAND-flash
480 * requires 16-bit long. Handling 8-bit long addresses is done using
481 * the option field. For the 16-bit addresses, the workaround depends
482 * of the number of requested dummy bits. If there are 8 or more dummy
483 * cycles, the address is shifted and sent with the first dummy byte.
484 * Otherwise opcode is disabled and the first byte of the address
485 * contains the command opcode (works only if the opcode and address
486 * use the same buswidth). The limitation is when the 16-bit address is
487 * used without enough dummy cycles and the opcode is using a different
488 * buswidth than the address.
489 */
490 if (op->addr.buswidth) {
491 switch (op->addr.nbytes) {
492 case 0:
493 break;
494 case 1:
495 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
496 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
497 break;
498 case 2:
499 if (dummy_cycles < 8 / op->addr.buswidth) {
500 ifr &= ~QSPI_IFR_INSTEN;
501 ifr |= QSPI_IFR_ADDREN;
502 iar = (op->cmd.opcode << 16) |
503 (op->addr.val & 0xffff);
504 } else {
505 ifr |= QSPI_IFR_ADDREN;
506 iar = (op->addr.val << 8) & 0xffffff;
507 dummy_cycles -= 8 / op->addr.buswidth;
508 }
509 break;
510 case 3:
511 ifr |= QSPI_IFR_ADDREN;
512 iar = op->addr.val & 0xffffff;
513 break;
514 case 4:
515 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
516 iar = op->addr.val & 0x7ffffff;
517 break;
518 default:
519 return -ENOTSUPP;
520 }
521 }
522
523 /* offset of the data access in the QSPI memory space */
524 *offset = iar;
525
526 /* Set number of dummy cycles */
527 if (dummy_cycles)
528 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
529
530 /* Set data enable */
531 if (op->data.nbytes)
532 ifr |= QSPI_IFR_DATAEN;
533
534 /*
535 * If the QSPI controller is set in regular SPI mode, set it in
536 * Serial Memory Mode (SMM).
537 */
538 if (aq->mr != QSPI_MR_SMM) {
Tudor Ambarus52e25652020-03-20 09:37:59 +0000539 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000540 aq->mr = QSPI_MR_SMM;
541 }
542
543 /* Clear pending interrupts */
Tudor Ambarus52e25652020-03-20 09:37:59 +0000544 (void)atmel_qspi_read(aq, QSPI_SR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000545
546 if (aq->caps->has_ricr) {
547 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
548 ifr |= QSPI_IFR_APBTFRTYP_READ;
549
550 /* Set QSPI Instruction Frame registers */
Tudor Ambarus52e25652020-03-20 09:37:59 +0000551 atmel_qspi_write(iar, aq, QSPI_IAR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000552 if (op->data.dir == SPI_MEM_DATA_IN)
Tudor Ambarus52e25652020-03-20 09:37:59 +0000553 atmel_qspi_write(icr, aq, QSPI_RICR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000554 else
Tudor Ambarus52e25652020-03-20 09:37:59 +0000555 atmel_qspi_write(icr, aq, QSPI_WICR);
556 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000557 } else {
558 if (op->data.dir == SPI_MEM_DATA_OUT)
559 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
560
561 /* Set QSPI Instruction Frame registers */
Tudor Ambarus52e25652020-03-20 09:37:59 +0000562 atmel_qspi_write(iar, aq, QSPI_IAR);
563 atmel_qspi_write(icr, aq, QSPI_ICR);
564 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000565 }
566
567 return 0;
568}
569
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200570static int atmel_qspi_transfer(struct atmel_qspi *aq,
571 const struct spi_mem_op *op, u32 offset)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000572{
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200573 u32 sr, imr;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000574
575 /* Skip to the final steps if there is no data */
576 if (op->data.nbytes) {
577 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
Tudor Ambarus52e25652020-03-20 09:37:59 +0000578 (void)atmel_qspi_read(aq, QSPI_IFR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000579
580 /* Send/Receive data */
581 if (op->data.dir == SPI_MEM_DATA_IN)
582 memcpy_fromio(op->data.buf.in, aq->mem + offset,
583 op->data.nbytes);
584 else
585 memcpy_toio(aq->mem + offset, op->data.buf.out,
586 op->data.nbytes);
587
588 /* Release the chip-select */
Tudor Ambarus52e25652020-03-20 09:37:59 +0000589 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000590 }
591
592 /* Poll INSTruction End and Chip Select Rise flags. */
593 imr = QSPI_SR_INSTRE | QSPI_SR_CSR;
594 return readl_poll_timeout(aq->regs + QSPI_SR, sr, (sr & imr) == imr,
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200595 ATMEL_QSPI_TIMEOUT);
596}
597
598static int atmel_qspi_reg_sync(struct atmel_qspi *aq)
599{
600 u32 val;
601
602 return readl_poll_timeout(aq->regs + QSPI_SR2, val,
603 !(val & QSPI_SR2_SYNCBSY),
604 ATMEL_QSPI_SYNC_TIMEOUT);
605}
606
607static int atmel_qspi_update_config(struct atmel_qspi *aq)
608{
609 int ret;
610
611 ret = atmel_qspi_reg_sync(aq);
612 if (ret)
613 return ret;
614 atmel_qspi_write(QSPI_CR_UPDCFG, aq, QSPI_CR);
615 return atmel_qspi_reg_sync(aq);
616}
617
618static int atmel_qspi_sama7g5_set_cfg(struct atmel_qspi *aq,
619 const struct spi_mem_op *op, u32 *offset)
620{
621 u32 iar, icr, ifr;
622 int mode, ret;
623
624 iar = 0;
625 icr = FIELD_PREP(QSPI_ICR_INST_MASK_SAMA7G5, op->cmd.opcode);
626 ifr = QSPI_IFR_INSTEN;
627
628 mode = atmel_qspi_sama7g5_find_mode(op);
629 if (mode < 0)
630 return mode;
631 ifr |= atmel_qspi_sama7g5_modes[mode].config;
632
633 if (op->dummy.buswidth && op->dummy.nbytes) {
634 if (op->addr.dtr && op->dummy.dtr && op->data.dtr)
635 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
636 (2 * op->dummy.buswidth));
637 else
638 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
639 op->dummy.buswidth);
640 }
641
642 if (op->addr.buswidth && op->addr.nbytes) {
643 ifr |= FIELD_PREP(QSPI_IFR_ADDRL_SAMA7G5, op->addr.nbytes - 1) |
644 QSPI_IFR_ADDREN;
645 iar = FIELD_PREP(QSPI_IAR_ADDR, op->addr.val);
646 }
647
648 if (op->addr.dtr && op->dummy.dtr && op->data.dtr) {
649 ifr |= QSPI_IFR_DDREN;
650 if (op->cmd.dtr)
651 ifr |= QSPI_IFR_DDRCMDEN;
652 ifr |= QSPI_IFR_DQSEN;
653 }
654
655 if (op->cmd.buswidth == 8 || op->addr.buswidth == 8 ||
656 op->data.buswidth == 8)
657 ifr |= FIELD_PREP(QSPI_IFR_PROTTYP, QSPI_IFR_PROTTYP_OCTAFLASH);
658
659 /* offset of the data access in the QSPI memory space */
660 *offset = iar;
661
662 /* Set data enable */
663 if (op->data.nbytes) {
664 ifr |= QSPI_IFR_DATAEN;
665 if (op->addr.nbytes)
666 ifr |= QSPI_IFR_TFRTYP_MEM;
667 }
668
669 /*
670 * If the QSPI controller is set in regular SPI mode, set it in
671 * Serial Memory Mode (SMM).
672 */
673 if (aq->mr != QSPI_MR_SMM) {
674 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
675 ret = atmel_qspi_update_config(aq);
676 if (ret)
677 return ret;
678 aq->mr = QSPI_MR_SMM;
679 }
680
681 /* Clear pending interrupts */
682 (void)atmel_qspi_read(aq, QSPI_SR);
683
684 /* Set QSPI Instruction Frame registers */
685 if (op->addr.nbytes && !op->data.nbytes)
686 atmel_qspi_write(iar, aq, QSPI_IAR);
687
688 if (op->data.dir == SPI_MEM_DATA_IN) {
689 atmel_qspi_write(icr, aq, QSPI_RICR);
690 } else {
691 atmel_qspi_write(icr, aq, QSPI_WICR);
692 if (op->data.nbytes)
693 atmel_qspi_write(FIELD_PREP(QSPI_WRACNT_NBWRA,
694 op->data.nbytes),
695 aq, QSPI_WRACNT);
696 }
697
698 atmel_qspi_write(ifr, aq, QSPI_IFR);
699
700 return atmel_qspi_update_config(aq);
701}
702
703static int atmel_qspi_sama7g5_transfer(struct atmel_qspi *aq,
704 const struct spi_mem_op *op, u32 offset)
705{
706 int err;
707 u32 val;
708
709 if (!op->data.nbytes) {
710 /* Start the transfer. */
711 err = atmel_qspi_reg_sync(aq);
712 if (err)
713 return err;
714 atmel_qspi_write(QSPI_CR_STTFR, aq, QSPI_CR);
715
716 return readl_poll_timeout(aq->regs + QSPI_SR, val,
717 val & QSPI_SR_CSRA,
718 ATMEL_QSPI_TIMEOUT);
719 }
720
721 /* Send/Receive data. */
722 if (op->data.dir == SPI_MEM_DATA_IN) {
723 memcpy_fromio(op->data.buf.in, aq->mem + offset,
724 op->data.nbytes);
725
726 if (op->addr.nbytes) {
727 err = readl_poll_timeout(aq->regs + QSPI_SR2, val,
728 !(val & QSPI_SR2_RBUSY),
729 ATMEL_QSPI_SYNC_TIMEOUT);
730 if (err)
731 return err;
732 }
733 } else {
734 memcpy_toio(aq->mem + offset, op->data.buf.out,
735 op->data.nbytes);
736
737 err = readl_poll_timeout(aq->regs + QSPI_SR, val,
738 val & QSPI_SR_LWRA,
739 ATMEL_QSPI_TIMEOUT);
740 if (err)
741 return err;
742 }
743
744 /* Release the chip-select. */
745 err = atmel_qspi_reg_sync(aq);
746 if (err)
747 return err;
748 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
749
750 return readl_poll_timeout(aq->regs + QSPI_SR, val, val & QSPI_SR_CSRA,
751 ATMEL_QSPI_TIMEOUT);
752}
753
754static int atmel_qspi_exec_op(struct spi_slave *slave,
755 const struct spi_mem_op *op)
756{
757 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
758 u32 offset;
759 int err;
760
761 /*
762 * Check if the address exceeds the MMIO window size. An improvement
763 * would be to add support for regular SPI mode and fall back to it
764 * when the flash memories overrun the controller's memory space.
765 */
766 if (op->addr.val + op->data.nbytes > aq->mmap_size)
767 return -ENOTSUPP;
768
769 if (op->addr.nbytes > 4)
770 return -EOPNOTSUPP;
771
772 err = aq->ops->set_cfg(aq, op, &offset);
773 if (err)
774 return err;
775
776 return aq->ops->transfer(aq, op, offset);
777}
778
779static int atmel_qspi_set_pad_calibration(struct udevice *bus, uint hz)
780{
781 struct atmel_qspi *aq = dev_get_priv(bus);
782 u32 status, val;
783 int i, ret;
784 u8 pclk_div = 0;
785
786 for (i = 0; i < ATMEL_QSPI_PCAL_ARRAY_SIZE; i++) {
787 if (aq->bus_clk_rate <= pcal[i].pclk_rate) {
788 pclk_div = pcal[i].pclk_div;
789 break;
790 }
791 }
792
793 /*
794 * Use the biggest divider in case the peripheral clock exceeds
795 * 200MHZ.
796 */
797 if (aq->bus_clk_rate > pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_rate)
798 pclk_div = pcal[ATMEL_QSPI_PCAL_ARRAY_SIZE - 1].pclk_div;
799
800 /* Disable QSPI while configuring the pad calibration. */
801 status = atmel_qspi_read(aq, QSPI_SR2);
802 if (status & QSPI_SR2_QSPIENS) {
803 ret = atmel_qspi_reg_sync(aq);
804 if (ret)
805 return ret;
806 atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
807 }
808
809 /*
810 * The analog circuitry is not shut down at the end of the calibration
811 * and the start-up time is only required for the first calibration
812 * sequence, thus increasing performance. Set the delay between the Pad
813 * calibration analog circuitry and the calibration request to 2us.
814 */
815 atmel_qspi_write(QSPI_PCALCFG_AAON |
816 FIELD_PREP(QSPI_PCALCFG_CLKDIV, pclk_div) |
817 FIELD_PREP(QSPI_PCALCFG_CALCNT,
818 2 * (aq->bus_clk_rate / 1000000)),
819 aq, QSPI_PCALCFG);
820
821 /* DLL On + start calibration. */
822 atmel_qspi_write(QSPI_CR_DLLON | QSPI_CR_STPCAL, aq, QSPI_CR);
823 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
824 (val & QSPI_SR2_DLOCK) &&
825 !(val & QSPI_SR2_CALBSY),
826 ATMEL_QSPI_TIMEOUT);
827
828 /* Refresh analogic blocks every 1 ms.*/
829 atmel_qspi_write(FIELD_PREP(QSPI_REFRESH_DELAY_COUNTER, hz / 1000),
830 aq, QSPI_REFRESH);
831
832 return ret;
833}
834
835static int atmel_qspi_set_gclk(struct udevice *bus, uint hz)
836{
837 struct atmel_qspi *aq = dev_get_priv(bus);
838 struct clk gclk;
839 u32 status, val;
840 int ret;
841
842 /* Disable DLL before setting GCLK */
843 status = atmel_qspi_read(aq, QSPI_SR2);
844 if (status & QSPI_SR2_DLOCK) {
845 atmel_qspi_write(QSPI_CR_DLLOFF, aq, QSPI_CR);
846 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
847 !(val & QSPI_SR2_DLOCK),
848 ATMEL_QSPI_TIMEOUT);
849 if (ret)
850 return ret;
851 }
852
853 if (hz > QSPI_DLLCFG_THRESHOLD_FREQ)
854 atmel_qspi_write(QSPI_DLLCFG_RANGE, aq, QSPI_DLLCFG);
855 else
856 atmel_qspi_write(0, aq, QSPI_DLLCFG);
857
858 ret = clk_get_by_name(bus, "gclk", &gclk);
859 if (ret) {
860 dev_err(bus, "Missing QSPI generic clock\n");
861 return ret;
862 }
863
864 ret = clk_disable(&gclk);
865 if (ret)
866 dev_err(bus, "Failed to disable QSPI generic clock\n");
867
868 ret = clk_set_rate(&gclk, hz);
869 if (ret < 0) {
870 dev_err(bus, "Failed to set generic clock rate.\n");
871 return ret;
872 }
873
874 ret = clk_enable(&gclk);
875 if (ret)
876 dev_err(bus, "Failed to enable QSPI generic clock\n");
877 clk_free(&gclk);
878
879 return ret;
880}
881
882static int atmel_qspi_sama7g5_set_speed(struct udevice *bus, uint hz)
883{
884 struct atmel_qspi *aq = dev_get_priv(bus);
885 u32 val;
886 int ret;
887
888 ret = atmel_qspi_set_gclk(bus, hz);
889 if (ret)
890 return ret;
891
892 if (aq->caps->octal) {
893 ret = atmel_qspi_set_pad_calibration(bus, hz);
894 if (ret)
895 return ret;
896 } else {
897 atmel_qspi_write(QSPI_CR_DLLON, aq, QSPI_CR);
898 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
899 val & QSPI_SR2_DLOCK,
900 ATMEL_QSPI_TIMEOUT);
901 }
902
903 /* Set the QSPI controller by default in Serial Memory Mode */
904 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
905 ret = atmel_qspi_update_config(aq);
906 if (ret)
907 return ret;
908 aq->mr = QSPI_MR_SMM;
909
910 /* Enable the QSPI controller. */
911 ret = atmel_qspi_reg_sync(aq);
912 if (ret)
913 return ret;
914 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
915 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
916 val & QSPI_SR2_QSPIENS,
917 ATMEL_QSPI_SYNC_TIMEOUT);
918 if (ret)
919 return ret;
920
921 if (aq->caps->octal)
922 ret = readl_poll_timeout(aq->regs + QSPI_SR, val,
923 val & QSPI_SR_RFRSHD,
924 ATMEL_QSPI_TIMEOUT);
925
926 atmel_qspi_write(FIELD_PREP(QSPI_TOUT_TCNTM, QSPI_TOUT_MAX),
927 aq, QSPI_TOUT);
928
929 return ret;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000930}
931
932static int atmel_qspi_set_speed(struct udevice *bus, uint hz)
933{
934 struct atmel_qspi *aq = dev_get_priv(bus);
935 u32 scr, scbr, mask, new_value;
936
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200937 if (aq->caps->has_gclk)
938 return atmel_qspi_sama7g5_set_speed(bus, hz);
939
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000940 /* Compute the QSPI baudrate */
941 scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz);
942 if (scbr > 0)
943 scbr--;
944
945 new_value = QSPI_SCR_SCBR(scbr);
946 mask = QSPI_SCR_SCBR_MASK;
947
Tudor Ambarus52e25652020-03-20 09:37:59 +0000948 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000949 if ((scr & mask) == new_value)
950 return 0;
951
952 scr = (scr & ~mask) | new_value;
Tudor Ambarus52e25652020-03-20 09:37:59 +0000953 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000954
955 return 0;
956}
957
958static int atmel_qspi_set_mode(struct udevice *bus, uint mode)
959{
960 struct atmel_qspi *aq = dev_get_priv(bus);
961 u32 scr, mask, new_value = 0;
962
963 if (mode & SPI_CPOL)
964 new_value = QSPI_SCR_CPOL;
965 if (mode & SPI_CPHA)
966 new_value = QSPI_SCR_CPHA;
967
968 mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA;
969
Tudor Ambarus52e25652020-03-20 09:37:59 +0000970 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000971 if ((scr & mask) == new_value)
972 return 0;
973
974 scr = (scr & ~mask) | new_value;
Tudor Ambarus52e25652020-03-20 09:37:59 +0000975 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200976 if (aq->caps->has_gclk)
977 return atmel_qspi_update_config(aq);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000978
979 return 0;
980}
981
982static int atmel_qspi_enable_clk(struct udevice *dev)
983{
984 struct atmel_qspi *aq = dev_get_priv(dev);
Tudor Ambarus2fd1b972021-11-03 18:47:10 +0200985 struct clk pclk, qspick, gclk;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +0000986 int ret;
987
988 ret = clk_get_by_name(dev, "pclk", &pclk);
989 if (ret)
990 ret = clk_get_by_index(dev, 0, &pclk);
991
992 if (ret) {
993 dev_err(dev, "Missing QSPI peripheral clock\n");
994 return ret;
995 }
996
997 ret = clk_enable(&pclk);
998 if (ret) {
999 dev_err(dev, "Failed to enable QSPI peripheral clock\n");
1000 goto free_pclk;
1001 }
1002
1003 if (aq->caps->has_qspick) {
1004 /* Get the QSPI system clock */
1005 ret = clk_get_by_name(dev, "qspick", &qspick);
1006 if (ret) {
1007 dev_err(dev, "Missing QSPI peripheral clock\n");
1008 goto free_pclk;
1009 }
1010
1011 ret = clk_enable(&qspick);
1012 if (ret)
1013 dev_err(dev, "Failed to enable QSPI system clock\n");
1014 clk_free(&qspick);
Tudor Ambarus2fd1b972021-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");
1019 goto free_pclk;
1020 }
1021
1022 ret = clk_enable(&gclk);
1023 if (ret)
1024 dev_err(dev, "Failed to enable QSPI system clock\n");
1025 clk_free(&gclk);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001026 }
1027
1028 aq->bus_clk_rate = clk_get_rate(&pclk);
1029 if (!aq->bus_clk_rate)
1030 ret = -EINVAL;
1031
1032free_pclk:
1033 clk_free(&pclk);
1034
1035 return ret;
1036}
1037
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001038static int atmel_qspi_init(struct atmel_qspi *aq)
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001039{
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001040 int ret;
1041
1042 if (aq->caps->has_gclk) {
1043 ret = atmel_qspi_reg_sync(aq);
1044 if (ret)
1045 return ret;
1046 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
1047 return 0;
1048 }
1049
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001050 /* Reset the QSPI controller */
Tudor Ambarus52e25652020-03-20 09:37:59 +00001051 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001052
1053 /* Set the QSPI controller by default in Serial Memory Mode */
Tudor Ambarus52e25652020-03-20 09:37:59 +00001054 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001055 aq->mr = QSPI_MR_SMM;
1056
1057 /* Enable the QSPI controller */
Tudor Ambarus52e25652020-03-20 09:37:59 +00001058 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001059
1060 return 0;
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001061}
1062
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001063static const struct atmel_qspi_priv_ops atmel_qspi_priv_ops = {
1064 .set_cfg = atmel_qspi_set_cfg,
1065 .transfer = atmel_qspi_transfer,
1066};
1067
1068static const struct atmel_qspi_priv_ops atmel_qspi_sama7g5_priv_ops = {
1069 .set_cfg = atmel_qspi_sama7g5_set_cfg,
1070 .transfer = atmel_qspi_sama7g5_transfer,
1071};
1072
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001073static int atmel_qspi_probe(struct udevice *dev)
1074{
1075 struct atmel_qspi *aq = dev_get_priv(dev);
1076 struct resource res;
1077 int ret;
1078
1079 aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev);
1080 if (!aq->caps) {
1081 dev_err(dev, "Could not retrieve QSPI caps\n");
1082 return -EINVAL;
1083 };
1084
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001085 if (aq->caps->has_gclk)
1086 aq->ops = &atmel_qspi_sama7g5_priv_ops;
1087 else
1088 aq->ops = &atmel_qspi_priv_ops;
1089
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001090 /* Map the registers */
1091 ret = dev_read_resource_byname(dev, "qspi_base", &res);
1092 if (ret) {
1093 dev_err(dev, "missing registers\n");
1094 return ret;
1095 }
1096
1097 aq->regs = devm_ioremap(dev, res.start, resource_size(&res));
1098 if (IS_ERR(aq->regs))
1099 return PTR_ERR(aq->regs);
1100
1101 /* Map the AHB memory */
1102 ret = dev_read_resource_byname(dev, "qspi_mmap", &res);
1103 if (ret) {
1104 dev_err(dev, "missing AHB memory\n");
1105 return ret;
1106 }
1107
1108 aq->mem = devm_ioremap(dev, res.start, resource_size(&res));
1109 if (IS_ERR(aq->mem))
1110 return PTR_ERR(aq->mem);
1111
Tudor Ambarusba03a6c2020-03-20 09:37:59 +00001112 aq->mmap_size = resource_size(&res);
1113
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001114 ret = atmel_qspi_enable_clk(dev);
1115 if (ret)
1116 return ret;
1117
Tudor Ambarus52e25652020-03-20 09:37:59 +00001118 aq->dev = dev;
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001119 return atmel_qspi_init(aq);
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001120}
1121
1122static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
1123 .supports_op = atmel_qspi_supports_op,
1124 .exec_op = atmel_qspi_exec_op,
1125};
1126
1127static const struct dm_spi_ops atmel_qspi_ops = {
1128 .set_speed = atmel_qspi_set_speed,
1129 .set_mode = atmel_qspi_set_mode,
1130 .mem_ops = &atmel_qspi_mem_ops,
1131};
1132
1133static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
1134
1135static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
1136 .has_qspick = true,
1137 .has_ricr = true,
1138};
1139
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001140static const struct atmel_qspi_caps atmel_sama7g5_ospi_caps = {
1141 .has_gclk = true,
1142 .octal = true,
1143};
1144
1145static const struct atmel_qspi_caps atmel_sama7g5_qspi_caps = {
1146 .has_gclk = true,
1147};
1148
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001149static const struct udevice_id atmel_qspi_ids[] = {
1150 {
1151 .compatible = "atmel,sama5d2-qspi",
1152 .data = (ulong)&atmel_sama5d2_qspi_caps,
1153 },
1154 {
1155 .compatible = "microchip,sam9x60-qspi",
1156 .data = (ulong)&atmel_sam9x60_qspi_caps,
1157 },
Tudor Ambarus2fd1b972021-11-03 18:47:10 +02001158 {
1159 .compatible = "microchip,sama7g5-ospi",
1160 .data = (ulong)&atmel_sama7g5_ospi_caps,
1161 },
1162 {
1163 .compatible = "microchip,sama7g5-qspi",
1164 .data = (ulong)&atmel_sama7g5_qspi_caps,
1165 },
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001166 { /* sentinel */ }
1167};
1168
1169U_BOOT_DRIVER(atmel_qspi) = {
1170 .name = "atmel_qspi",
1171 .id = UCLASS_SPI,
1172 .of_match = atmel_qspi_ids,
1173 .ops = &atmel_qspi_ops,
Simon Glass41575d82020-12-03 16:55:17 -07001174 .priv_auto = sizeof(struct atmel_qspi),
Tudor Ambarus24c8ff42019-06-18 08:51:50 +00001175 .probe = atmel_qspi_probe,
1176};