blob: bd73e4fddf18033b352a69d6c69ce549f4f92bad [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>
15#include <common.h>
16#include <dm.h>
17#include <errno.h>
18#include <fdtdec.h>
Simon Glass9bc15642020-02-03 07:36:16 -070019#include <dm/device_compat.h>
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +020020#include <linux/bitfield.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060021#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <linux/err.h>
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +020036#define QSPI_SR2 0x0024 /* SAMA7G5 Status Register */
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-06-18 08:51:50 +000070#define QSPI_CR_SWRST BIT(7)
Tudor Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +020081#define QSPI_MR_DQSDLYEN BIT(3)
82
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +020089#define QSPI_MR_OENSD BIT(15)
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +0200137#define QSPI_ICR_INST_MASK_SAMA7G5 GENMASK(15, 0)
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +0200163#define QSPI_IFR_ADDRL_SAMA7G5 GENMASK(11, 10)
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +0200167#define QSPI_IFR_DDREN BIT(15)
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-11-03 18:47:10 +0200170#define QSPI_IFR_END BIT(22)
171#define QSPI_IFR_SMRM BIT(23)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000172#define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */
Tudor Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-06-18 08:51:50 +0000212
213/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
214#define QSPI_WPMR_WPEN BIT(0)
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200215#define QSPI_WPMR_WPITEN BIT(1)
216#define QSPI_WPMR_WPCREN BIT(2)
Tudor Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-06-18 08:51:50 +0000253struct atmel_qspi_caps {
254 bool has_qspick;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200255 bool has_gclk;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000256 bool has_ricr;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200257 bool octal;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000258};
259
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200260struct atmel_qspi_priv_ops;
261
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000262struct atmel_qspi {
263 void __iomem *regs;
264 void __iomem *mem;
Tudor Ambarus678b8932020-03-20 09:37:59 +0000265 resource_size_t mmap_size;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000266 const struct atmel_qspi_caps *caps;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200267 const struct atmel_qspi_priv_ops *ops;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000268 struct udevice *dev;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000269 ulong bus_clk_rate;
270 u32 mr;
271};
272
Tudor Ambarus8e4f44b2021-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarusf44d2e02020-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 Ambarus7e022332021-11-03 18:45:42 +0200319 return "RD";
Tudor Ambarusf44d2e02020-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 Ambarus8e4f44b2021-11-03 18:47:10 +0200332 case QSPI_SR2:
333 return "SR2";
Tudor Ambarusf44d2e02020-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 Ambarus8e4f44b2021-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 Ambarusf44d2e02020-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 Ambarus88151bb2019-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 Ambarus8e4f44b2021-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 Ambarus88151bb2019-06-18 08:51:50 +0000436static bool atmel_qspi_supports_op(struct spi_slave *slave,
437 const struct spi_mem_op *op)
438{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200439 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
440
Tudor Ambarus938a6c92022-04-08 11:40:26 +0300441 if (!spi_mem_default_supports_op(slave, op))
442 return false;
443
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200444 if (aq->caps->octal) {
445 if (atmel_qspi_sama7g5_find_mode(op) < 0)
446 return false;
447 else
448 return true;
449 }
450
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000451 if (atmel_qspi_find_mode(op) < 0)
452 return false;
453
454 /* special case not supported by hardware */
455 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
456 op->dummy.nbytes == 0)
457 return false;
458
459 return true;
460}
461
462static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
463 const struct spi_mem_op *op, u32 *offset)
464{
465 u32 iar, icr, ifr;
466 u32 dummy_cycles = 0;
467 int mode;
468
469 iar = 0;
470 icr = QSPI_ICR_INST(op->cmd.opcode);
471 ifr = QSPI_IFR_INSTEN;
472
473 mode = atmel_qspi_find_mode(op);
474 if (mode < 0)
475 return mode;
476 ifr |= atmel_qspi_modes[mode].config;
477
478 if (op->dummy.buswidth && op->dummy.nbytes)
479 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
480
481 /*
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.
492 */
493 if (op->addr.buswidth) {
494 switch (op->addr.nbytes) {
495 case 0:
496 break;
497 case 1:
498 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
499 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
500 break;
501 case 2:
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);
507 } else {
508 ifr |= QSPI_IFR_ADDREN;
509 iar = (op->addr.val << 8) & 0xffffff;
510 dummy_cycles -= 8 / op->addr.buswidth;
511 }
512 break;
513 case 3:
514 ifr |= QSPI_IFR_ADDREN;
515 iar = op->addr.val & 0xffffff;
516 break;
517 case 4:
518 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
519 iar = op->addr.val & 0x7ffffff;
520 break;
521 default:
522 return -ENOTSUPP;
523 }
524 }
525
526 /* offset of the data access in the QSPI memory space */
527 *offset = iar;
528
529 /* Set number of dummy cycles */
530 if (dummy_cycles)
531 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
532
533 /* Set data enable */
534 if (op->data.nbytes)
535 ifr |= QSPI_IFR_DATAEN;
536
537 /*
538 * If the QSPI controller is set in regular SPI mode, set it in
539 * Serial Memory Mode (SMM).
540 */
541 if (aq->mr != QSPI_MR_SMM) {
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000542 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000543 aq->mr = QSPI_MR_SMM;
544 }
545
546 /* Clear pending interrupts */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000547 (void)atmel_qspi_read(aq, QSPI_SR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000548
549 if (aq->caps->has_ricr) {
550 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
551 ifr |= QSPI_IFR_APBTFRTYP_READ;
552
553 /* Set QSPI Instruction Frame registers */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000554 atmel_qspi_write(iar, aq, QSPI_IAR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000555 if (op->data.dir == SPI_MEM_DATA_IN)
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000556 atmel_qspi_write(icr, aq, QSPI_RICR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000557 else
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000558 atmel_qspi_write(icr, aq, QSPI_WICR);
559 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000560 } else {
561 if (op->data.dir == SPI_MEM_DATA_OUT)
562 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
563
564 /* Set QSPI Instruction Frame registers */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000565 atmel_qspi_write(iar, aq, QSPI_IAR);
566 atmel_qspi_write(icr, aq, QSPI_ICR);
567 atmel_qspi_write(ifr, aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000568 }
569
570 return 0;
571}
572
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200573static int atmel_qspi_transfer(struct atmel_qspi *aq,
574 const struct spi_mem_op *op, u32 offset)
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000575{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200576 u32 sr, imr;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000577
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 */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000581 (void)atmel_qspi_read(aq, QSPI_IFR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000582
583 /* Send/Receive data */
584 if (op->data.dir == SPI_MEM_DATA_IN)
585 memcpy_fromio(op->data.buf.in, aq->mem + offset,
586 op->data.nbytes);
587 else
588 memcpy_toio(aq->mem + offset, op->data.buf.out,
589 op->data.nbytes);
590
591 /* Release the chip-select */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000592 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000593 }
594
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,
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200598 ATMEL_QSPI_TIMEOUT);
599}
600
601static int atmel_qspi_reg_sync(struct atmel_qspi *aq)
602{
603 u32 val;
604
605 return readl_poll_timeout(aq->regs + QSPI_SR2, val,
606 !(val & QSPI_SR2_SYNCBSY),
607 ATMEL_QSPI_SYNC_TIMEOUT);
608}
609
610static int atmel_qspi_update_config(struct atmel_qspi *aq)
611{
612 int ret;
613
614 ret = atmel_qspi_reg_sync(aq);
615 if (ret)
616 return ret;
617 atmel_qspi_write(QSPI_CR_UPDCFG, aq, QSPI_CR);
618 return atmel_qspi_reg_sync(aq);
619}
620
621static int atmel_qspi_sama7g5_set_cfg(struct atmel_qspi *aq,
622 const struct spi_mem_op *op, u32 *offset)
623{
624 u32 iar, icr, ifr;
625 int mode, ret;
626
627 iar = 0;
628 icr = FIELD_PREP(QSPI_ICR_INST_MASK_SAMA7G5, op->cmd.opcode);
629 ifr = QSPI_IFR_INSTEN;
630
631 mode = atmel_qspi_sama7g5_find_mode(op);
632 if (mode < 0)
633 return mode;
634 ifr |= atmel_qspi_sama7g5_modes[mode].config;
635
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));
640 else
641 ifr |= QSPI_IFR_NBDUM(op->dummy.nbytes * 8 /
642 op->dummy.buswidth);
643 }
644
645 if (op->addr.buswidth && op->addr.nbytes) {
646 ifr |= FIELD_PREP(QSPI_IFR_ADDRL_SAMA7G5, op->addr.nbytes - 1) |
647 QSPI_IFR_ADDREN;
648 iar = FIELD_PREP(QSPI_IAR_ADDR, op->addr.val);
649 }
650
651 if (op->addr.dtr && op->dummy.dtr && op->data.dtr) {
652 ifr |= QSPI_IFR_DDREN;
653 if (op->cmd.dtr)
654 ifr |= QSPI_IFR_DDRCMDEN;
655 ifr |= QSPI_IFR_DQSEN;
656 }
657
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);
661
662 /* offset of the data access in the QSPI memory space */
663 *offset = iar;
664
665 /* Set data enable */
666 if (op->data.nbytes) {
667 ifr |= QSPI_IFR_DATAEN;
668 if (op->addr.nbytes)
669 ifr |= QSPI_IFR_TFRTYP_MEM;
670 }
671
672 /*
673 * If the QSPI controller is set in regular SPI mode, set it in
674 * Serial Memory Mode (SMM).
675 */
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);
679 if (ret)
680 return ret;
681 aq->mr = QSPI_MR_SMM;
682 }
683
684 /* Clear pending interrupts */
685 (void)atmel_qspi_read(aq, QSPI_SR);
686
687 /* Set QSPI Instruction Frame registers */
688 if (op->addr.nbytes && !op->data.nbytes)
689 atmel_qspi_write(iar, aq, QSPI_IAR);
690
691 if (op->data.dir == SPI_MEM_DATA_IN) {
692 atmel_qspi_write(icr, aq, QSPI_RICR);
693 } else {
694 atmel_qspi_write(icr, aq, QSPI_WICR);
695 if (op->data.nbytes)
696 atmel_qspi_write(FIELD_PREP(QSPI_WRACNT_NBWRA,
697 op->data.nbytes),
698 aq, QSPI_WRACNT);
699 }
700
701 atmel_qspi_write(ifr, aq, QSPI_IFR);
702
703 return atmel_qspi_update_config(aq);
704}
705
706static int atmel_qspi_sama7g5_transfer(struct atmel_qspi *aq,
707 const struct spi_mem_op *op, u32 offset)
708{
709 int err;
710 u32 val;
711
712 if (!op->data.nbytes) {
713 /* Start the transfer. */
714 err = atmel_qspi_reg_sync(aq);
715 if (err)
716 return err;
717 atmel_qspi_write(QSPI_CR_STTFR, aq, QSPI_CR);
718
719 return readl_poll_timeout(aq->regs + QSPI_SR, val,
720 val & QSPI_SR_CSRA,
721 ATMEL_QSPI_TIMEOUT);
722 }
723
724 /* Send/Receive data. */
725 if (op->data.dir == SPI_MEM_DATA_IN) {
726 memcpy_fromio(op->data.buf.in, aq->mem + offset,
727 op->data.nbytes);
728
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);
733 if (err)
734 return err;
735 }
736 } else {
737 memcpy_toio(aq->mem + offset, op->data.buf.out,
738 op->data.nbytes);
739
740 err = readl_poll_timeout(aq->regs + QSPI_SR, val,
741 val & QSPI_SR_LWRA,
742 ATMEL_QSPI_TIMEOUT);
743 if (err)
744 return err;
745 }
746
747 /* Release the chip-select. */
748 err = atmel_qspi_reg_sync(aq);
749 if (err)
750 return err;
751 atmel_qspi_write(QSPI_CR_LASTXFER, aq, QSPI_CR);
752
753 return readl_poll_timeout(aq->regs + QSPI_SR, val, val & QSPI_SR_CSRA,
754 ATMEL_QSPI_TIMEOUT);
755}
756
757static int atmel_qspi_exec_op(struct spi_slave *slave,
758 const struct spi_mem_op *op)
759{
760 struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
761 u32 offset;
762 int err;
763
764 /*
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.
768 */
769 if (op->addr.val + op->data.nbytes > aq->mmap_size)
770 return -ENOTSUPP;
771
772 if (op->addr.nbytes > 4)
773 return -EOPNOTSUPP;
774
775 err = aq->ops->set_cfg(aq, op, &offset);
776 if (err)
777 return err;
778
779 return aq->ops->transfer(aq, op, offset);
780}
781
782static int atmel_qspi_set_pad_calibration(struct udevice *bus, uint hz)
783{
784 struct atmel_qspi *aq = dev_get_priv(bus);
785 u32 status, val;
786 int i, ret;
787 u8 pclk_div = 0;
788
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;
792 break;
793 }
794 }
795
796 /*
797 * Use the biggest divider in case the peripheral clock exceeds
798 * 200MHZ.
799 */
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;
802
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);
807 if (ret)
808 return ret;
809 atmel_qspi_write(QSPI_CR_QSPIDIS, aq, QSPI_CR);
810 }
811
812 /*
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.
817 */
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)),
822 aq, QSPI_PCALCFG);
823
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),
829 ATMEL_QSPI_TIMEOUT);
830
831 /* Refresh analogic blocks every 1 ms.*/
832 atmel_qspi_write(FIELD_PREP(QSPI_REFRESH_DELAY_COUNTER, hz / 1000),
833 aq, QSPI_REFRESH);
834
835 return ret;
836}
837
838static int atmel_qspi_set_gclk(struct udevice *bus, uint hz)
839{
840 struct atmel_qspi *aq = dev_get_priv(bus);
841 struct clk gclk;
842 u32 status, val;
843 int ret;
844
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),
851 ATMEL_QSPI_TIMEOUT);
852 if (ret)
853 return ret;
854 }
855
856 if (hz > QSPI_DLLCFG_THRESHOLD_FREQ)
857 atmel_qspi_write(QSPI_DLLCFG_RANGE, aq, QSPI_DLLCFG);
858 else
859 atmel_qspi_write(0, aq, QSPI_DLLCFG);
860
861 ret = clk_get_by_name(bus, "gclk", &gclk);
862 if (ret) {
863 dev_err(bus, "Missing QSPI generic clock\n");
864 return ret;
865 }
866
867 ret = clk_disable(&gclk);
868 if (ret)
869 dev_err(bus, "Failed to disable QSPI generic clock\n");
870
871 ret = clk_set_rate(&gclk, hz);
872 if (ret < 0) {
873 dev_err(bus, "Failed to set generic clock rate.\n");
874 return ret;
875 }
876
877 ret = clk_enable(&gclk);
878 if (ret)
879 dev_err(bus, "Failed to enable QSPI generic clock\n");
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200880
881 return ret;
882}
883
884static int atmel_qspi_sama7g5_set_speed(struct udevice *bus, uint hz)
885{
886 struct atmel_qspi *aq = dev_get_priv(bus);
887 u32 val;
888 int ret;
889
890 ret = atmel_qspi_set_gclk(bus, hz);
891 if (ret)
892 return ret;
893
894 if (aq->caps->octal) {
895 ret = atmel_qspi_set_pad_calibration(bus, hz);
896 if (ret)
897 return ret;
898 } else {
899 atmel_qspi_write(QSPI_CR_DLLON, aq, QSPI_CR);
900 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
901 val & QSPI_SR2_DLOCK,
902 ATMEL_QSPI_TIMEOUT);
903 }
904
905 /* Set the QSPI controller by default in Serial Memory Mode */
906 atmel_qspi_write(QSPI_MR_SMM | QSPI_MR_DQSDLYEN, aq, QSPI_MR);
907 ret = atmel_qspi_update_config(aq);
908 if (ret)
909 return ret;
910 aq->mr = QSPI_MR_SMM;
911
912 /* Enable the QSPI controller. */
913 ret = atmel_qspi_reg_sync(aq);
914 if (ret)
915 return ret;
916 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
917 ret = readl_poll_timeout(aq->regs + QSPI_SR2, val,
918 val & QSPI_SR2_QSPIENS,
919 ATMEL_QSPI_SYNC_TIMEOUT);
920 if (ret)
921 return ret;
922
923 if (aq->caps->octal)
924 ret = readl_poll_timeout(aq->regs + QSPI_SR, val,
925 val & QSPI_SR_RFRSHD,
926 ATMEL_QSPI_TIMEOUT);
927
928 atmel_qspi_write(FIELD_PREP(QSPI_TOUT_TCNTM, QSPI_TOUT_MAX),
929 aq, QSPI_TOUT);
930
931 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000932}
933
934static int atmel_qspi_set_speed(struct udevice *bus, uint hz)
935{
936 struct atmel_qspi *aq = dev_get_priv(bus);
937 u32 scr, scbr, mask, new_value;
938
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200939 if (aq->caps->has_gclk)
940 return atmel_qspi_sama7g5_set_speed(bus, hz);
941
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000942 /* Compute the QSPI baudrate */
943 scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz);
944 if (scbr > 0)
945 scbr--;
946
947 new_value = QSPI_SCR_SCBR(scbr);
948 mask = QSPI_SCR_SCBR_MASK;
949
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000950 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000951 if ((scr & mask) == new_value)
952 return 0;
953
954 scr = (scr & ~mask) | new_value;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000955 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000956
957 return 0;
958}
959
960static int atmel_qspi_set_mode(struct udevice *bus, uint mode)
961{
962 struct atmel_qspi *aq = dev_get_priv(bus);
963 u32 scr, mask, new_value = 0;
964
965 if (mode & SPI_CPOL)
966 new_value = QSPI_SCR_CPOL;
967 if (mode & SPI_CPHA)
968 new_value = QSPI_SCR_CPHA;
969
970 mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA;
971
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000972 scr = atmel_qspi_read(aq, QSPI_SCR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000973 if ((scr & mask) == new_value)
974 return 0;
975
976 scr = (scr & ~mask) | new_value;
Tudor Ambarusf44d2e02020-03-20 09:37:59 +0000977 atmel_qspi_write(scr, aq, QSPI_SCR);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200978 if (aq->caps->has_gclk)
979 return atmel_qspi_update_config(aq);
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000980
981 return 0;
982}
983
984static int atmel_qspi_enable_clk(struct udevice *dev)
985{
986 struct atmel_qspi *aq = dev_get_priv(dev);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +0200987 struct clk pclk, qspick, gclk;
Tudor Ambarus88151bb2019-06-18 08:51:50 +0000988 int ret;
989
990 ret = clk_get_by_name(dev, "pclk", &pclk);
991 if (ret)
992 ret = clk_get_by_index(dev, 0, &pclk);
993
994 if (ret) {
995 dev_err(dev, "Missing QSPI peripheral clock\n");
996 return ret;
997 }
998
999 ret = clk_enable(&pclk);
1000 if (ret) {
1001 dev_err(dev, "Failed to enable QSPI peripheral clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001002 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001003 }
1004
1005 if (aq->caps->has_qspick) {
1006 /* Get the QSPI system clock */
1007 ret = clk_get_by_name(dev, "qspick", &qspick);
1008 if (ret) {
1009 dev_err(dev, "Missing QSPI peripheral clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001010 return ret;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001011 }
1012
1013 ret = clk_enable(&qspick);
1014 if (ret)
1015 dev_err(dev, "Failed to enable QSPI system clock\n");
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001016 } else if (aq->caps->has_gclk) {
1017 ret = clk_get_by_name(dev, "gclk", &gclk);
1018 if (ret) {
1019 dev_err(dev, "Missing QSPI generic clock\n");
Sean Andersond318eb32023-12-16 14:38:42 -05001020 return ret;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001021 }
1022
1023 ret = clk_enable(&gclk);
1024 if (ret)
1025 dev_err(dev, "Failed to enable QSPI system clock\n");
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001026 }
1027
1028 aq->bus_clk_rate = clk_get_rate(&pclk);
1029 if (!aq->bus_clk_rate)
Sean Andersond318eb32023-12-16 14:38:42 -05001030 return -EINVAL;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001031
1032 return ret;
1033}
1034
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001035static int atmel_qspi_init(struct atmel_qspi *aq)
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001036{
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001037 int ret;
1038
1039 if (aq->caps->has_gclk) {
1040 ret = atmel_qspi_reg_sync(aq);
1041 if (ret)
1042 return ret;
1043 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
1044 return 0;
1045 }
1046
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001047 /* Reset the QSPI controller */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001048 atmel_qspi_write(QSPI_CR_SWRST, aq, QSPI_CR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001049
1050 /* Set the QSPI controller by default in Serial Memory Mode */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001051 atmel_qspi_write(QSPI_MR_SMM, aq, QSPI_MR);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001052 aq->mr = QSPI_MR_SMM;
1053
1054 /* Enable the QSPI controller */
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001055 atmel_qspi_write(QSPI_CR_QSPIEN, aq, QSPI_CR);
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001056
1057 return 0;
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001058}
1059
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001060static const struct atmel_qspi_priv_ops atmel_qspi_priv_ops = {
1061 .set_cfg = atmel_qspi_set_cfg,
1062 .transfer = atmel_qspi_transfer,
1063};
1064
1065static const struct atmel_qspi_priv_ops atmel_qspi_sama7g5_priv_ops = {
1066 .set_cfg = atmel_qspi_sama7g5_set_cfg,
1067 .transfer = atmel_qspi_sama7g5_transfer,
1068};
1069
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001070static int atmel_qspi_probe(struct udevice *dev)
1071{
1072 struct atmel_qspi *aq = dev_get_priv(dev);
1073 struct resource res;
1074 int ret;
1075
1076 aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev);
1077 if (!aq->caps) {
1078 dev_err(dev, "Could not retrieve QSPI caps\n");
1079 return -EINVAL;
1080 };
1081
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001082 if (aq->caps->has_gclk)
1083 aq->ops = &atmel_qspi_sama7g5_priv_ops;
1084 else
1085 aq->ops = &atmel_qspi_priv_ops;
1086
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001087 /* Map the registers */
1088 ret = dev_read_resource_byname(dev, "qspi_base", &res);
1089 if (ret) {
1090 dev_err(dev, "missing registers\n");
1091 return ret;
1092 }
1093
1094 aq->regs = devm_ioremap(dev, res.start, resource_size(&res));
1095 if (IS_ERR(aq->regs))
1096 return PTR_ERR(aq->regs);
1097
1098 /* Map the AHB memory */
1099 ret = dev_read_resource_byname(dev, "qspi_mmap", &res);
1100 if (ret) {
1101 dev_err(dev, "missing AHB memory\n");
1102 return ret;
1103 }
1104
1105 aq->mem = devm_ioremap(dev, res.start, resource_size(&res));
1106 if (IS_ERR(aq->mem))
1107 return PTR_ERR(aq->mem);
1108
Tudor Ambarus678b8932020-03-20 09:37:59 +00001109 aq->mmap_size = resource_size(&res);
1110
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001111 ret = atmel_qspi_enable_clk(dev);
1112 if (ret)
1113 return ret;
1114
Tudor Ambarusf44d2e02020-03-20 09:37:59 +00001115 aq->dev = dev;
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001116 return atmel_qspi_init(aq);
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001117}
1118
1119static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
1120 .supports_op = atmel_qspi_supports_op,
1121 .exec_op = atmel_qspi_exec_op,
1122};
1123
1124static const struct dm_spi_ops atmel_qspi_ops = {
1125 .set_speed = atmel_qspi_set_speed,
1126 .set_mode = atmel_qspi_set_mode,
1127 .mem_ops = &atmel_qspi_mem_ops,
1128};
1129
1130static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
1131
1132static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
1133 .has_qspick = true,
1134 .has_ricr = true,
1135};
1136
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001137static const struct atmel_qspi_caps atmel_sama7g5_ospi_caps = {
1138 .has_gclk = true,
1139 .octal = true,
1140};
1141
1142static const struct atmel_qspi_caps atmel_sama7g5_qspi_caps = {
1143 .has_gclk = true,
1144};
1145
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001146static const struct udevice_id atmel_qspi_ids[] = {
1147 {
1148 .compatible = "atmel,sama5d2-qspi",
1149 .data = (ulong)&atmel_sama5d2_qspi_caps,
1150 },
1151 {
1152 .compatible = "microchip,sam9x60-qspi",
1153 .data = (ulong)&atmel_sam9x60_qspi_caps,
1154 },
Tudor Ambarus8e4f44b2021-11-03 18:47:10 +02001155 {
1156 .compatible = "microchip,sama7g5-ospi",
1157 .data = (ulong)&atmel_sama7g5_ospi_caps,
1158 },
1159 {
1160 .compatible = "microchip,sama7g5-qspi",
1161 .data = (ulong)&atmel_sama7g5_qspi_caps,
1162 },
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001163 { /* sentinel */ }
1164};
1165
1166U_BOOT_DRIVER(atmel_qspi) = {
1167 .name = "atmel_qspi",
1168 .id = UCLASS_SPI,
1169 .of_match = atmel_qspi_ids,
1170 .ops = &atmel_qspi_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001171 .priv_auto = sizeof(struct atmel_qspi),
Tudor Ambarus88151bb2019-06-18 08:51:50 +00001172 .probe = atmel_qspi_probe,
1173};