blob: 13def7f78b9e99c9829af817496c794071042981 [file] [log] [blame]
Tudor Ambaruscae417b2019-02-05 17:33:22 +00001// SPDX-License-Identifier: GPL-2.0
Cyrille Pitchen161aaab2016-06-13 17:10:26 +02002/*
3 * Driver for Atmel QSPI Controller
4 *
5 * Copyright (C) 2015 Atmel Corporation
Piotr Bugalskid5433de2018-11-05 11:36:21 +01006 * Copyright (C) 2018 Cryptera A/S
Cyrille Pitchen161aaab2016-06-13 17:10:26 +02007 *
8 * Author: Cyrille Pitchen <cyrille.pitchen@atmel.com>
Piotr Bugalskid5433de2018-11-05 11:36:21 +01009 * Author: Piotr Bugalski <bugalski.piotr@gmail.com>
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020010 *
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020011 * This driver is based on drivers/mtd/spi-nor/fsl-quadspi.c from Freescale.
12 */
13
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020014#include <linux/clk.h>
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020015#include <linux/delay.h>
16#include <linux/err.h>
17#include <linux/interrupt.h>
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020018#include <linux/io.h>
Tudor Ambarus3ae012e2019-02-05 17:33:08 +000019#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/of.h>
Tudor Ambarus2e5c8882019-02-05 17:33:38 +000022#include <linux/of_platform.h>
Tudor Ambarus3ae012e2019-02-05 17:33:08 +000023#include <linux/platform_device.h>
Piotr Bugalskid5433de2018-11-05 11:36:21 +010024#include <linux/spi/spi-mem.h>
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020025
26/* QSPI register offsets */
27#define QSPI_CR 0x0000 /* Control Register */
28#define QSPI_MR 0x0004 /* Mode Register */
29#define QSPI_RD 0x0008 /* Receive Data Register */
30#define QSPI_TD 0x000c /* Transmit Data Register */
31#define QSPI_SR 0x0010 /* Status Register */
32#define QSPI_IER 0x0014 /* Interrupt Enable Register */
33#define QSPI_IDR 0x0018 /* Interrupt Disable Register */
34#define QSPI_IMR 0x001c /* Interrupt Mask Register */
35#define QSPI_SCR 0x0020 /* Serial Clock Register */
36
37#define QSPI_IAR 0x0030 /* Instruction Address Register */
38#define QSPI_ICR 0x0034 /* Instruction Code Register */
Tudor Ambarus2e5c8882019-02-05 17:33:38 +000039#define QSPI_WICR 0x0034 /* Write Instruction Code Register */
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020040#define QSPI_IFR 0x0038 /* Instruction Frame Register */
Tudor Ambarus2e5c8882019-02-05 17:33:38 +000041#define QSPI_RICR 0x003C /* Read Instruction Code Register */
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020042
43#define QSPI_SMR 0x0040 /* Scrambling Mode Register */
44#define QSPI_SKR 0x0044 /* Scrambling Key Register */
45
46#define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
47#define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
48
49#define QSPI_VERSION 0x00FC /* Version Register */
50
51
52/* Bitfields in QSPI_CR (Control Register) */
53#define QSPI_CR_QSPIEN BIT(0)
54#define QSPI_CR_QSPIDIS BIT(1)
55#define QSPI_CR_SWRST BIT(7)
56#define QSPI_CR_LASTXFER BIT(24)
57
58/* Bitfields in QSPI_MR (Mode Register) */
Piotr Bugalskib82ab1c2018-11-05 11:36:20 +010059#define QSPI_MR_SMM BIT(0)
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020060#define QSPI_MR_LLB BIT(1)
61#define QSPI_MR_WDRBT BIT(2)
62#define QSPI_MR_SMRM BIT(3)
63#define QSPI_MR_CSMODE_MASK GENMASK(5, 4)
64#define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4)
65#define QSPI_MR_CSMODE_LASTXFER (1 << 4)
66#define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4)
67#define QSPI_MR_NBBITS_MASK GENMASK(11, 8)
68#define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
69#define QSPI_MR_DLYBCT_MASK GENMASK(23, 16)
70#define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK)
71#define QSPI_MR_DLYCS_MASK GENMASK(31, 24)
72#define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK)
73
74/* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */
75#define QSPI_SR_RDRF BIT(0)
76#define QSPI_SR_TDRE BIT(1)
77#define QSPI_SR_TXEMPTY BIT(2)
78#define QSPI_SR_OVRES BIT(3)
79#define QSPI_SR_CSR BIT(8)
80#define QSPI_SR_CSS BIT(9)
81#define QSPI_SR_INSTRE BIT(10)
82#define QSPI_SR_QSPIENS BIT(24)
83
84#define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
85
86/* Bitfields in QSPI_SCR (Serial Clock Register) */
87#define QSPI_SCR_CPOL BIT(0)
88#define QSPI_SCR_CPHA BIT(1)
89#define QSPI_SCR_SCBR_MASK GENMASK(15, 8)
90#define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK)
91#define QSPI_SCR_DLYBS_MASK GENMASK(23, 16)
92#define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK)
93
Tudor Ambarus2e5c8882019-02-05 17:33:38 +000094/* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020095#define QSPI_ICR_INST_MASK GENMASK(7, 0)
96#define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK)
97#define QSPI_ICR_OPT_MASK GENMASK(23, 16)
98#define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
99
100/* Bitfields in QSPI_IFR (Instruction Frame Register) */
101#define QSPI_IFR_WIDTH_MASK GENMASK(2, 0)
102#define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0)
103#define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0)
104#define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0)
105#define QSPI_IFR_WIDTH_DUAL_IO (3 << 0)
106#define QSPI_IFR_WIDTH_QUAD_IO (4 << 0)
107#define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0)
108#define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0)
109#define QSPI_IFR_INSTEN BIT(4)
110#define QSPI_IFR_ADDREN BIT(5)
111#define QSPI_IFR_OPTEN BIT(6)
112#define QSPI_IFR_DATAEN BIT(7)
113#define QSPI_IFR_OPTL_MASK GENMASK(9, 8)
114#define QSPI_IFR_OPTL_1BIT (0 << 8)
115#define QSPI_IFR_OPTL_2BIT (1 << 8)
116#define QSPI_IFR_OPTL_4BIT (2 << 8)
117#define QSPI_IFR_OPTL_8BIT (3 << 8)
118#define QSPI_IFR_ADDRL BIT(10)
Tudor Ambarusb456fd12019-02-05 17:33:25 +0000119#define QSPI_IFR_TFRTYP_MEM BIT(12)
120#define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13)
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200121#define QSPI_IFR_CRM BIT(14)
122#define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
123#define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000124#define QSPI_IFR_APBTFRTYP_READ BIT(24) /* Defined in SAM9X60 */
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200125
126/* Bitfields in QSPI_SMR (Scrambling Mode Register) */
127#define QSPI_SMR_SCREN BIT(0)
128#define QSPI_SMR_RVDIS BIT(1)
129
130/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
131#define QSPI_WPMR_WPEN BIT(0)
132#define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
133#define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
134
135/* Bitfields in QSPI_WPSR (Write Protection Status Register) */
136#define QSPI_WPSR_WPVS BIT(0)
137#define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8)
138#define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC)
139
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000140struct atmel_qspi_caps {
141 bool has_qspick;
142 bool has_ricr;
143};
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200144
145struct atmel_qspi {
146 void __iomem *regs;
147 void __iomem *mem;
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000148 struct clk *pclk;
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000149 struct clk *qspick;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200150 struct platform_device *pdev;
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000151 const struct atmel_qspi_caps *caps;
Tudor Ambarus8e093ea2020-02-28 15:55:32 +0000152 resource_size_t mmap_size;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200153 u32 pending;
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000154 u32 mr;
Tudor Ambarusab735612019-06-28 15:30:34 +0000155 u32 scr;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200156 struct completion cmd_completion;
157};
158
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000159struct atmel_qspi_mode {
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100160 u8 cmd_buswidth;
161 u8 addr_buswidth;
162 u8 data_buswidth;
163 u32 config;
164};
165
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000166static const struct atmel_qspi_mode atmel_qspi_modes[] = {
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100167 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
168 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
169 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
170 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
171 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
172 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
173 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
174};
175
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000176static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
177 const struct atmel_qspi_mode *mode)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100178{
179 if (op->cmd.buswidth != mode->cmd_buswidth)
180 return false;
181
182 if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
183 return false;
184
185 if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
186 return false;
187
188 return true;
189}
190
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000191static int atmel_qspi_find_mode(const struct spi_mem_op *op)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100192{
193 u32 i;
194
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000195 for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
196 if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100197 return i;
198
Tudor Ambarus2aaa8dd2019-02-05 17:33:19 +0000199 return -ENOTSUPP;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100200}
201
202static bool atmel_qspi_supports_op(struct spi_mem *mem,
203 const struct spi_mem_op *op)
204{
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000205 if (atmel_qspi_find_mode(op) < 0)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100206 return false;
207
208 /* special case not supported by hardware */
209 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
210 op->dummy.nbytes == 0)
211 return false;
212
213 return true;
214}
215
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000216static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
217 const struct spi_mem_op *op, u32 *offset)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100218{
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000219 u32 iar, icr, ifr;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100220 u32 dummy_cycles = 0;
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000221 int mode;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100222
223 iar = 0;
224 icr = QSPI_ICR_INST(op->cmd.opcode);
225 ifr = QSPI_IFR_INSTEN;
226
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000227 mode = atmel_qspi_find_mode(op);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100228 if (mode < 0)
Tudor Ambarus2aaa8dd2019-02-05 17:33:19 +0000229 return mode;
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000230 ifr |= atmel_qspi_modes[mode].config;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100231
232 if (op->dummy.buswidth && op->dummy.nbytes)
233 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
234
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000235 /*
236 * The controller allows 24 and 32-bit addressing while NAND-flash
237 * requires 16-bit long. Handling 8-bit long addresses is done using
238 * the option field. For the 16-bit addresses, the workaround depends
239 * of the number of requested dummy bits. If there are 8 or more dummy
240 * cycles, the address is shifted and sent with the first dummy byte.
241 * Otherwise opcode is disabled and the first byte of the address
242 * contains the command opcode (works only if the opcode and address
243 * use the same buswidth). The limitation is when the 16-bit address is
244 * used without enough dummy cycles and the opcode is using a different
245 * buswidth than the address.
246 */
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100247 if (op->addr.buswidth) {
248 switch (op->addr.nbytes) {
249 case 0:
250 break;
251 case 1:
252 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
253 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
254 break;
255 case 2:
256 if (dummy_cycles < 8 / op->addr.buswidth) {
257 ifr &= ~QSPI_IFR_INSTEN;
258 ifr |= QSPI_IFR_ADDREN;
259 iar = (op->cmd.opcode << 16) |
260 (op->addr.val & 0xffff);
261 } else {
262 ifr |= QSPI_IFR_ADDREN;
263 iar = (op->addr.val << 8) & 0xffffff;
264 dummy_cycles -= 8 / op->addr.buswidth;
265 }
266 break;
267 case 3:
268 ifr |= QSPI_IFR_ADDREN;
269 iar = op->addr.val & 0xffffff;
270 break;
271 case 4:
272 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
273 iar = op->addr.val & 0x7ffffff;
274 break;
275 default:
276 return -ENOTSUPP;
277 }
278 }
279
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000280 /* offset of the data access in the QSPI memory space */
281 *offset = iar;
282
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100283 /* Set number of dummy cycles */
284 if (dummy_cycles)
285 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
286
287 /* Set data enable */
288 if (op->data.nbytes)
289 ifr |= QSPI_IFR_DATAEN;
290
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000291 /*
292 * If the QSPI controller is set in regular SPI mode, set it in
293 * Serial Memory Mode (SMM).
294 */
295 if (aq->mr != QSPI_MR_SMM) {
296 writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR);
297 aq->mr = QSPI_MR_SMM;
298 }
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100299
300 /* Clear pending interrupts */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000301 (void)readl_relaxed(aq->regs + QSPI_SR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100302
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000303 if (aq->caps->has_ricr) {
304 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
305 ifr |= QSPI_IFR_APBTFRTYP_READ;
306
307 /* Set QSPI Instruction Frame registers */
308 writel_relaxed(iar, aq->regs + QSPI_IAR);
309 if (op->data.dir == SPI_MEM_DATA_IN)
310 writel_relaxed(icr, aq->regs + QSPI_RICR);
311 else
312 writel_relaxed(icr, aq->regs + QSPI_WICR);
313 writel_relaxed(ifr, aq->regs + QSPI_IFR);
314 } else {
315 if (op->data.dir == SPI_MEM_DATA_OUT)
316 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
317
318 /* Set QSPI Instruction Frame registers */
319 writel_relaxed(iar, aq->regs + QSPI_IAR);
320 writel_relaxed(icr, aq->regs + QSPI_ICR);
321 writel_relaxed(ifr, aq->regs + QSPI_IFR);
322 }
323
324 return 0;
325}
326
327static int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
328{
329 struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master);
330 u32 sr, offset;
331 int err;
332
Tudor Ambarus8e093ea2020-02-28 15:55:32 +0000333 /*
334 * Check if the address exceeds the MMIO window size. An improvement
335 * would be to add support for regular SPI mode and fall back to it
336 * when the flash memories overrun the controller's memory space.
337 */
338 if (op->addr.val + op->data.nbytes > aq->mmap_size)
339 return -ENOTSUPP;
340
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000341 err = atmel_qspi_set_cfg(aq, op, &offset);
342 if (err)
343 return err;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100344
345 /* Skip to the final steps if there is no data */
346 if (op->data.nbytes) {
347 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000348 (void)readl_relaxed(aq->regs + QSPI_IFR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100349
350 /* Send/Receive data */
351 if (op->data.dir == SPI_MEM_DATA_IN)
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000352 _memcpy_fromio(op->data.buf.in, aq->mem + offset,
353 op->data.nbytes);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100354 else
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000355 _memcpy_toio(aq->mem + offset, op->data.buf.out,
356 op->data.nbytes);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100357
358 /* Release the chip-select */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000359 writel_relaxed(QSPI_CR_LASTXFER, aq->regs + QSPI_CR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100360 }
361
362 /* Poll INSTRuction End status */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000363 sr = readl_relaxed(aq->regs + QSPI_SR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100364 if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
365 return err;
366
367 /* Wait for INSTRuction End interrupt */
368 reinit_completion(&aq->cmd_completion);
369 aq->pending = sr & QSPI_SR_CMD_COMPLETED;
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000370 writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IER);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100371 if (!wait_for_completion_timeout(&aq->cmd_completion,
372 msecs_to_jiffies(1000)))
373 err = -ETIMEDOUT;
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000374 writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IDR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100375
376 return err;
377}
378
YueHaibing55e3dac2019-03-21 23:16:56 +0800379static const char *atmel_qspi_get_name(struct spi_mem *spimem)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100380{
381 return dev_name(spimem->spi->dev.parent);
382}
383
384static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
385 .supports_op = atmel_qspi_supports_op,
386 .exec_op = atmel_qspi_exec_op,
387 .get_name = atmel_qspi_get_name
388};
389
390static int atmel_qspi_setup(struct spi_device *spi)
391{
392 struct spi_controller *ctrl = spi->master;
393 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
394 unsigned long src_rate;
Tudor Ambarusab735612019-06-28 15:30:34 +0000395 u32 scbr;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100396
397 if (ctrl->busy)
398 return -EBUSY;
399
400 if (!spi->max_speed_hz)
401 return -EINVAL;
402
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000403 src_rate = clk_get_rate(aq->pclk);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100404 if (!src_rate)
405 return -EINVAL;
406
407 /* Compute the QSPI baudrate */
408 scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz);
409 if (scbr > 0)
410 scbr--;
411
Tudor Ambarusab735612019-06-28 15:30:34 +0000412 aq->scr = QSPI_SCR_SCBR(scbr);
413 writel_relaxed(aq->scr, aq->regs + QSPI_SCR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100414
415 return 0;
416}
417
Tudor Ambarus5b74e9a2019-06-28 15:30:32 +0000418static void atmel_qspi_init(struct atmel_qspi *aq)
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200419{
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200420 /* Reset the QSPI controller */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000421 writel_relaxed(QSPI_CR_SWRST, aq->regs + QSPI_CR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200422
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000423 /* Set the QSPI controller by default in Serial Memory Mode */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000424 writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR);
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000425 aq->mr = QSPI_MR_SMM;
426
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200427 /* Enable the QSPI controller */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000428 writel_relaxed(QSPI_CR_QSPIEN, aq->regs + QSPI_CR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200429}
430
431static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id)
432{
Tudor Ambarus9ce4c512019-02-05 17:33:17 +0000433 struct atmel_qspi *aq = dev_id;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200434 u32 status, mask, pending;
435
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000436 status = readl_relaxed(aq->regs + QSPI_SR);
437 mask = readl_relaxed(aq->regs + QSPI_IMR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200438 pending = status & mask;
439
440 if (!pending)
441 return IRQ_NONE;
442
443 aq->pending |= pending;
444 if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
445 complete(&aq->cmd_completion);
446
447 return IRQ_HANDLED;
448}
449
450static int atmel_qspi_probe(struct platform_device *pdev)
451{
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100452 struct spi_controller *ctrl;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200453 struct atmel_qspi *aq;
454 struct resource *res;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200455 int irq, err = 0;
456
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100457 ctrl = spi_alloc_master(&pdev->dev, sizeof(*aq));
458 if (!ctrl)
459 return -ENOMEM;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200460
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100461 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
462 ctrl->setup = atmel_qspi_setup;
463 ctrl->bus_num = -1;
464 ctrl->mem_ops = &atmel_qspi_mem_ops;
465 ctrl->num_chipselect = 1;
466 ctrl->dev.of_node = pdev->dev.of_node;
467 platform_set_drvdata(pdev, ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200468
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100469 aq = spi_controller_get_devdata(ctrl);
470
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200471 init_completion(&aq->cmd_completion);
472 aq->pdev = pdev;
473
474 /* Map the registers */
475 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
476 aq->regs = devm_ioremap_resource(&pdev->dev, res);
477 if (IS_ERR(aq->regs)) {
478 dev_err(&pdev->dev, "missing registers\n");
479 err = PTR_ERR(aq->regs);
480 goto exit;
481 }
482
483 /* Map the AHB memory */
484 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap");
485 aq->mem = devm_ioremap_resource(&pdev->dev, res);
486 if (IS_ERR(aq->mem)) {
487 dev_err(&pdev->dev, "missing AHB memory\n");
488 err = PTR_ERR(aq->mem);
489 goto exit;
490 }
491
Tudor Ambarus8e093ea2020-02-28 15:55:32 +0000492 aq->mmap_size = resource_size(res);
493
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200494 /* Get the peripheral clock */
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000495 aq->pclk = devm_clk_get(&pdev->dev, "pclk");
496 if (IS_ERR(aq->pclk))
497 aq->pclk = devm_clk_get(&pdev->dev, NULL);
498
499 if (IS_ERR(aq->pclk)) {
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200500 dev_err(&pdev->dev, "missing peripheral clock\n");
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000501 err = PTR_ERR(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200502 goto exit;
503 }
504
505 /* Enable the peripheral clock */
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000506 err = clk_prepare_enable(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200507 if (err) {
508 dev_err(&pdev->dev, "failed to enable the peripheral clock\n");
509 goto exit;
510 }
511
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000512 aq->caps = of_device_get_match_data(&pdev->dev);
513 if (!aq->caps) {
514 dev_err(&pdev->dev, "Could not retrieve QSPI caps\n");
515 err = -EINVAL;
516 goto exit;
517 }
518
519 if (aq->caps->has_qspick) {
520 /* Get the QSPI system clock */
521 aq->qspick = devm_clk_get(&pdev->dev, "qspick");
522 if (IS_ERR(aq->qspick)) {
523 dev_err(&pdev->dev, "missing system clock\n");
524 err = PTR_ERR(aq->qspick);
525 goto disable_pclk;
526 }
527
528 /* Enable the QSPI system clock */
529 err = clk_prepare_enable(aq->qspick);
530 if (err) {
531 dev_err(&pdev->dev,
532 "failed to enable the QSPI system clock\n");
533 goto disable_pclk;
534 }
535 }
536
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200537 /* Request the IRQ */
538 irq = platform_get_irq(pdev, 0);
539 if (irq < 0) {
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200540 err = irq;
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000541 goto disable_qspick;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200542 }
543 err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt,
544 0, dev_name(&pdev->dev), aq);
545 if (err)
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000546 goto disable_qspick;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200547
Tudor Ambarus5b74e9a2019-06-28 15:30:32 +0000548 atmel_qspi_init(aq);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200549
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100550 err = spi_register_controller(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200551 if (err)
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000552 goto disable_qspick;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200553
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200554 return 0;
555
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000556disable_qspick:
557 clk_disable_unprepare(aq->qspick);
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000558disable_pclk:
559 clk_disable_unprepare(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200560exit:
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100561 spi_controller_put(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200562
563 return err;
564}
565
566static int atmel_qspi_remove(struct platform_device *pdev)
567{
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100568 struct spi_controller *ctrl = platform_get_drvdata(pdev);
569 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200570
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100571 spi_unregister_controller(ctrl);
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000572 writel_relaxed(QSPI_CR_QSPIDIS, aq->regs + QSPI_CR);
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000573 clk_disable_unprepare(aq->qspick);
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000574 clk_disable_unprepare(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200575 return 0;
576}
577
Claudiu Bezneade217c12018-06-04 11:46:33 +0300578static int __maybe_unused atmel_qspi_suspend(struct device *dev)
579{
Claudiu Bezneae5c27492019-04-24 09:17:59 +0000580 struct spi_controller *ctrl = dev_get_drvdata(dev);
581 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300582
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000583 clk_disable_unprepare(aq->qspick);
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000584 clk_disable_unprepare(aq->pclk);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300585
586 return 0;
587}
588
589static int __maybe_unused atmel_qspi_resume(struct device *dev)
590{
Claudiu Bezneae5c27492019-04-24 09:17:59 +0000591 struct spi_controller *ctrl = dev_get_drvdata(dev);
592 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300593
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000594 clk_prepare_enable(aq->pclk);
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000595 clk_prepare_enable(aq->qspick);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300596
Tudor Ambarus5b74e9a2019-06-28 15:30:32 +0000597 atmel_qspi_init(aq);
Tudor Ambarusab735612019-06-28 15:30:34 +0000598
599 writel_relaxed(aq->scr, aq->regs + QSPI_SCR);
600
Tudor Ambarus5b74e9a2019-06-28 15:30:32 +0000601 return 0;
Claudiu Bezneade217c12018-06-04 11:46:33 +0300602}
603
604static SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend,
605 atmel_qspi_resume);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200606
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000607static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
608
609static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
610 .has_qspick = true,
611 .has_ricr = true,
612};
613
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200614static const struct of_device_id atmel_qspi_dt_ids[] = {
Tudor Ambarus2e5c8882019-02-05 17:33:38 +0000615 {
616 .compatible = "atmel,sama5d2-qspi",
617 .data = &atmel_sama5d2_qspi_caps,
618 },
619 {
620 .compatible = "microchip,sam9x60-qspi",
621 .data = &atmel_sam9x60_qspi_caps,
622 },
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200623 { /* sentinel */ }
624};
625
626MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids);
627
628static struct platform_driver atmel_qspi_driver = {
629 .driver = {
630 .name = "atmel_qspi",
631 .of_match_table = atmel_qspi_dt_ids,
Claudiu Bezneade217c12018-06-04 11:46:33 +0300632 .pm = &atmel_qspi_pm_ops,
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200633 },
634 .probe = atmel_qspi_probe,
635 .remove = atmel_qspi_remove,
636};
637module_platform_driver(atmel_qspi_driver);
638
639MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100640MODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com");
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200641MODULE_DESCRIPTION("Atmel QSPI Controller driver");
642MODULE_LICENSE("GPL v2");