blob: 336501d962e5d2e4b5a8293c9a37b7b3dfc4f94f [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>
22#include <linux/platform_device.h>
Piotr Bugalskid5433de2018-11-05 11:36:21 +010023#include <linux/spi/spi-mem.h>
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020024
25/* QSPI register offsets */
26#define QSPI_CR 0x0000 /* Control Register */
27#define QSPI_MR 0x0004 /* Mode Register */
28#define QSPI_RD 0x0008 /* Receive Data Register */
29#define QSPI_TD 0x000c /* Transmit Data Register */
30#define QSPI_SR 0x0010 /* Status Register */
31#define QSPI_IER 0x0014 /* Interrupt Enable Register */
32#define QSPI_IDR 0x0018 /* Interrupt Disable Register */
33#define QSPI_IMR 0x001c /* Interrupt Mask Register */
34#define QSPI_SCR 0x0020 /* Serial Clock Register */
35
36#define QSPI_IAR 0x0030 /* Instruction Address Register */
37#define QSPI_ICR 0x0034 /* Instruction Code Register */
38#define QSPI_IFR 0x0038 /* Instruction Frame Register */
39
40#define QSPI_SMR 0x0040 /* Scrambling Mode Register */
41#define QSPI_SKR 0x0044 /* Scrambling Key Register */
42
43#define QSPI_WPMR 0x00E4 /* Write Protection Mode Register */
44#define QSPI_WPSR 0x00E8 /* Write Protection Status Register */
45
46#define QSPI_VERSION 0x00FC /* Version Register */
47
48
49/* Bitfields in QSPI_CR (Control Register) */
50#define QSPI_CR_QSPIEN BIT(0)
51#define QSPI_CR_QSPIDIS BIT(1)
52#define QSPI_CR_SWRST BIT(7)
53#define QSPI_CR_LASTXFER BIT(24)
54
55/* Bitfields in QSPI_MR (Mode Register) */
Piotr Bugalskib82ab1c2018-11-05 11:36:20 +010056#define QSPI_MR_SMM BIT(0)
Cyrille Pitchen161aaab2016-06-13 17:10:26 +020057#define QSPI_MR_LLB BIT(1)
58#define QSPI_MR_WDRBT BIT(2)
59#define QSPI_MR_SMRM BIT(3)
60#define QSPI_MR_CSMODE_MASK GENMASK(5, 4)
61#define QSPI_MR_CSMODE_NOT_RELOADED (0 << 4)
62#define QSPI_MR_CSMODE_LASTXFER (1 << 4)
63#define QSPI_MR_CSMODE_SYSTEMATICALLY (2 << 4)
64#define QSPI_MR_NBBITS_MASK GENMASK(11, 8)
65#define QSPI_MR_NBBITS(n) ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
66#define QSPI_MR_DLYBCT_MASK GENMASK(23, 16)
67#define QSPI_MR_DLYBCT(n) (((n) << 16) & QSPI_MR_DLYBCT_MASK)
68#define QSPI_MR_DLYCS_MASK GENMASK(31, 24)
69#define QSPI_MR_DLYCS(n) (((n) << 24) & QSPI_MR_DLYCS_MASK)
70
71/* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR */
72#define QSPI_SR_RDRF BIT(0)
73#define QSPI_SR_TDRE BIT(1)
74#define QSPI_SR_TXEMPTY BIT(2)
75#define QSPI_SR_OVRES BIT(3)
76#define QSPI_SR_CSR BIT(8)
77#define QSPI_SR_CSS BIT(9)
78#define QSPI_SR_INSTRE BIT(10)
79#define QSPI_SR_QSPIENS BIT(24)
80
81#define QSPI_SR_CMD_COMPLETED (QSPI_SR_INSTRE | QSPI_SR_CSR)
82
83/* Bitfields in QSPI_SCR (Serial Clock Register) */
84#define QSPI_SCR_CPOL BIT(0)
85#define QSPI_SCR_CPHA BIT(1)
86#define QSPI_SCR_SCBR_MASK GENMASK(15, 8)
87#define QSPI_SCR_SCBR(n) (((n) << 8) & QSPI_SCR_SCBR_MASK)
88#define QSPI_SCR_DLYBS_MASK GENMASK(23, 16)
89#define QSPI_SCR_DLYBS(n) (((n) << 16) & QSPI_SCR_DLYBS_MASK)
90
91/* Bitfields in QSPI_ICR (Instruction Code Register) */
92#define QSPI_ICR_INST_MASK GENMASK(7, 0)
93#define QSPI_ICR_INST(inst) (((inst) << 0) & QSPI_ICR_INST_MASK)
94#define QSPI_ICR_OPT_MASK GENMASK(23, 16)
95#define QSPI_ICR_OPT(opt) (((opt) << 16) & QSPI_ICR_OPT_MASK)
96
97/* Bitfields in QSPI_IFR (Instruction Frame Register) */
98#define QSPI_IFR_WIDTH_MASK GENMASK(2, 0)
99#define QSPI_IFR_WIDTH_SINGLE_BIT_SPI (0 << 0)
100#define QSPI_IFR_WIDTH_DUAL_OUTPUT (1 << 0)
101#define QSPI_IFR_WIDTH_QUAD_OUTPUT (2 << 0)
102#define QSPI_IFR_WIDTH_DUAL_IO (3 << 0)
103#define QSPI_IFR_WIDTH_QUAD_IO (4 << 0)
104#define QSPI_IFR_WIDTH_DUAL_CMD (5 << 0)
105#define QSPI_IFR_WIDTH_QUAD_CMD (6 << 0)
106#define QSPI_IFR_INSTEN BIT(4)
107#define QSPI_IFR_ADDREN BIT(5)
108#define QSPI_IFR_OPTEN BIT(6)
109#define QSPI_IFR_DATAEN BIT(7)
110#define QSPI_IFR_OPTL_MASK GENMASK(9, 8)
111#define QSPI_IFR_OPTL_1BIT (0 << 8)
112#define QSPI_IFR_OPTL_2BIT (1 << 8)
113#define QSPI_IFR_OPTL_4BIT (2 << 8)
114#define QSPI_IFR_OPTL_8BIT (3 << 8)
115#define QSPI_IFR_ADDRL BIT(10)
Tudor Ambarusb456fd12019-02-05 17:33:25 +0000116#define QSPI_IFR_TFRTYP_MEM BIT(12)
117#define QSPI_IFR_SAMA5D2_WRITE_TRSFR BIT(13)
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200118#define QSPI_IFR_CRM BIT(14)
119#define QSPI_IFR_NBDUM_MASK GENMASK(20, 16)
120#define QSPI_IFR_NBDUM(n) (((n) << 16) & QSPI_IFR_NBDUM_MASK)
121
122/* Bitfields in QSPI_SMR (Scrambling Mode Register) */
123#define QSPI_SMR_SCREN BIT(0)
124#define QSPI_SMR_RVDIS BIT(1)
125
126/* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
127#define QSPI_WPMR_WPEN BIT(0)
128#define QSPI_WPMR_WPKEY_MASK GENMASK(31, 8)
129#define QSPI_WPMR_WPKEY(wpkey) (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
130
131/* Bitfields in QSPI_WPSR (Write Protection Status Register) */
132#define QSPI_WPSR_WPVS BIT(0)
133#define QSPI_WPSR_WPVSRC_MASK GENMASK(15, 8)
134#define QSPI_WPSR_WPVSRC(src) (((src) << 8) & QSPI_WPSR_WPVSRC)
135
136
137struct atmel_qspi {
138 void __iomem *regs;
139 void __iomem *mem;
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000140 struct clk *pclk;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200141 struct platform_device *pdev;
142 u32 pending;
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000143 u32 mr;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200144 struct completion cmd_completion;
145};
146
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000147struct atmel_qspi_mode {
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100148 u8 cmd_buswidth;
149 u8 addr_buswidth;
150 u8 data_buswidth;
151 u32 config;
152};
153
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000154static const struct atmel_qspi_mode sama5d2_qspi_modes[] = {
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100155 { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
156 { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
157 { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
158 { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
159 { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
160 { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
161 { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
162};
163
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000164static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
165 const struct atmel_qspi_mode *mode)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100166{
167 if (op->cmd.buswidth != mode->cmd_buswidth)
168 return false;
169
170 if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
171 return false;
172
173 if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
174 return false;
175
176 return true;
177}
178
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000179static int atmel_qspi_find_mode(const struct spi_mem_op *op)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100180{
181 u32 i;
182
183 for (i = 0; i < ARRAY_SIZE(sama5d2_qspi_modes); i++)
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000184 if (atmel_qspi_is_compatible(op, &sama5d2_qspi_modes[i]))
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100185 return i;
186
Tudor Ambarus2aaa8dd2019-02-05 17:33:19 +0000187 return -ENOTSUPP;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100188}
189
190static bool atmel_qspi_supports_op(struct spi_mem *mem,
191 const struct spi_mem_op *op)
192{
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000193 if (atmel_qspi_find_mode(op) < 0)
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100194 return false;
195
196 /* special case not supported by hardware */
197 if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
198 op->dummy.nbytes == 0)
199 return false;
200
201 return true;
202}
203
204static int atmel_qspi_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
205{
206 struct atmel_qspi *aq = spi_controller_get_devdata(mem->spi->master);
207 int mode;
208 u32 dummy_cycles = 0;
209 u32 iar, icr, ifr, sr;
210 int err = 0;
211
212 iar = 0;
213 icr = QSPI_ICR_INST(op->cmd.opcode);
214 ifr = QSPI_IFR_INSTEN;
215
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000216 /*
217 * If the QSPI controller is set in regular SPI mode, set it in
218 * Serial Memory Mode (SMM).
219 */
220 if (aq->mr != QSPI_MR_SMM) {
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000221 writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR);
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000222 aq->mr = QSPI_MR_SMM;
223 }
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100224
Tudor Ambarus1db6de22019-02-05 17:33:14 +0000225 mode = atmel_qspi_find_mode(op);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100226 if (mode < 0)
Tudor Ambarus2aaa8dd2019-02-05 17:33:19 +0000227 return mode;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100228
229 ifr |= sama5d2_qspi_modes[mode].config;
230
231 if (op->dummy.buswidth && op->dummy.nbytes)
232 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
233
234 if (op->addr.buswidth) {
235 switch (op->addr.nbytes) {
236 case 0:
237 break;
238 case 1:
239 ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
240 icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
241 break;
242 case 2:
243 if (dummy_cycles < 8 / op->addr.buswidth) {
244 ifr &= ~QSPI_IFR_INSTEN;
245 ifr |= QSPI_IFR_ADDREN;
246 iar = (op->cmd.opcode << 16) |
247 (op->addr.val & 0xffff);
248 } else {
249 ifr |= QSPI_IFR_ADDREN;
250 iar = (op->addr.val << 8) & 0xffffff;
251 dummy_cycles -= 8 / op->addr.buswidth;
252 }
253 break;
254 case 3:
255 ifr |= QSPI_IFR_ADDREN;
256 iar = op->addr.val & 0xffffff;
257 break;
258 case 4:
259 ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
260 iar = op->addr.val & 0x7ffffff;
261 break;
262 default:
263 return -ENOTSUPP;
264 }
265 }
266
267 /* Set number of dummy cycles */
268 if (dummy_cycles)
269 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
270
271 /* Set data enable */
272 if (op->data.nbytes)
273 ifr |= QSPI_IFR_DATAEN;
274
Tudor Ambarusb456fd12019-02-05 17:33:25 +0000275 if (op->data.dir == SPI_MEM_DATA_OUT)
276 ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100277
278 /* Clear pending interrupts */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000279 (void)readl_relaxed(aq->regs + QSPI_SR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100280
281 /* Set QSPI Instruction Frame registers */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000282 writel_relaxed(iar, aq->regs + QSPI_IAR);
283 writel_relaxed(icr, aq->regs + QSPI_ICR);
284 writel_relaxed(ifr, aq->regs + QSPI_IFR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100285
286 /* Skip to the final steps if there is no data */
287 if (op->data.nbytes) {
288 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000289 (void)readl_relaxed(aq->regs + QSPI_IFR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100290
291 /* Send/Receive data */
292 if (op->data.dir == SPI_MEM_DATA_IN)
293 _memcpy_fromio(op->data.buf.in,
294 aq->mem + iar, op->data.nbytes);
295 else
296 _memcpy_toio(aq->mem + iar,
297 op->data.buf.out, op->data.nbytes);
298
299 /* Release the chip-select */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000300 writel_relaxed(QSPI_CR_LASTXFER, aq->regs + QSPI_CR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100301 }
302
303 /* Poll INSTRuction End status */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000304 sr = readl_relaxed(aq->regs + QSPI_SR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100305 if ((sr & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
306 return err;
307
308 /* Wait for INSTRuction End interrupt */
309 reinit_completion(&aq->cmd_completion);
310 aq->pending = sr & QSPI_SR_CMD_COMPLETED;
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000311 writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IER);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100312 if (!wait_for_completion_timeout(&aq->cmd_completion,
313 msecs_to_jiffies(1000)))
314 err = -ETIMEDOUT;
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000315 writel_relaxed(QSPI_SR_CMD_COMPLETED, aq->regs + QSPI_IDR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100316
317 return err;
318}
319
320const char *atmel_qspi_get_name(struct spi_mem *spimem)
321{
322 return dev_name(spimem->spi->dev.parent);
323}
324
325static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
326 .supports_op = atmel_qspi_supports_op,
327 .exec_op = atmel_qspi_exec_op,
328 .get_name = atmel_qspi_get_name
329};
330
331static int atmel_qspi_setup(struct spi_device *spi)
332{
333 struct spi_controller *ctrl = spi->master;
334 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
335 unsigned long src_rate;
336 u32 scr, scbr;
337
338 if (ctrl->busy)
339 return -EBUSY;
340
341 if (!spi->max_speed_hz)
342 return -EINVAL;
343
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000344 src_rate = clk_get_rate(aq->pclk);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100345 if (!src_rate)
346 return -EINVAL;
347
348 /* Compute the QSPI baudrate */
349 scbr = DIV_ROUND_UP(src_rate, spi->max_speed_hz);
350 if (scbr > 0)
351 scbr--;
352
353 scr = QSPI_SCR_SCBR(scbr);
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000354 writel_relaxed(scr, aq->regs + QSPI_SCR);
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100355
356 return 0;
357}
358
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200359static int atmel_qspi_init(struct atmel_qspi *aq)
360{
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200361 /* Reset the QSPI controller */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000362 writel_relaxed(QSPI_CR_SWRST, aq->regs + QSPI_CR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200363
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000364 /* Set the QSPI controller by default in Serial Memory Mode */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000365 writel_relaxed(QSPI_MR_SMM, aq->regs + QSPI_MR);
Tudor Ambarus9958c8c2019-02-05 17:33:06 +0000366 aq->mr = QSPI_MR_SMM;
367
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200368 /* Enable the QSPI controller */
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000369 writel_relaxed(QSPI_CR_QSPIEN, aq->regs + QSPI_CR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200370
371 return 0;
372}
373
374static irqreturn_t atmel_qspi_interrupt(int irq, void *dev_id)
375{
Tudor Ambarus9ce4c512019-02-05 17:33:17 +0000376 struct atmel_qspi *aq = dev_id;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200377 u32 status, mask, pending;
378
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000379 status = readl_relaxed(aq->regs + QSPI_SR);
380 mask = readl_relaxed(aq->regs + QSPI_IMR);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200381 pending = status & mask;
382
383 if (!pending)
384 return IRQ_NONE;
385
386 aq->pending |= pending;
387 if ((aq->pending & QSPI_SR_CMD_COMPLETED) == QSPI_SR_CMD_COMPLETED)
388 complete(&aq->cmd_completion);
389
390 return IRQ_HANDLED;
391}
392
393static int atmel_qspi_probe(struct platform_device *pdev)
394{
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100395 struct spi_controller *ctrl;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200396 struct atmel_qspi *aq;
397 struct resource *res;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200398 int irq, err = 0;
399
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100400 ctrl = spi_alloc_master(&pdev->dev, sizeof(*aq));
401 if (!ctrl)
402 return -ENOMEM;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200403
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100404 ctrl->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
405 ctrl->setup = atmel_qspi_setup;
406 ctrl->bus_num = -1;
407 ctrl->mem_ops = &atmel_qspi_mem_ops;
408 ctrl->num_chipselect = 1;
409 ctrl->dev.of_node = pdev->dev.of_node;
410 platform_set_drvdata(pdev, ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200411
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100412 aq = spi_controller_get_devdata(ctrl);
413
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200414 init_completion(&aq->cmd_completion);
415 aq->pdev = pdev;
416
417 /* Map the registers */
418 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_base");
419 aq->regs = devm_ioremap_resource(&pdev->dev, res);
420 if (IS_ERR(aq->regs)) {
421 dev_err(&pdev->dev, "missing registers\n");
422 err = PTR_ERR(aq->regs);
423 goto exit;
424 }
425
426 /* Map the AHB memory */
427 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "qspi_mmap");
428 aq->mem = devm_ioremap_resource(&pdev->dev, res);
429 if (IS_ERR(aq->mem)) {
430 dev_err(&pdev->dev, "missing AHB memory\n");
431 err = PTR_ERR(aq->mem);
432 goto exit;
433 }
434
435 /* Get the peripheral clock */
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000436 aq->pclk = devm_clk_get(&pdev->dev, "pclk");
437 if (IS_ERR(aq->pclk))
438 aq->pclk = devm_clk_get(&pdev->dev, NULL);
439
440 if (IS_ERR(aq->pclk)) {
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200441 dev_err(&pdev->dev, "missing peripheral clock\n");
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000442 err = PTR_ERR(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200443 goto exit;
444 }
445
446 /* Enable the peripheral clock */
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000447 err = clk_prepare_enable(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200448 if (err) {
449 dev_err(&pdev->dev, "failed to enable the peripheral clock\n");
450 goto exit;
451 }
452
453 /* Request the IRQ */
454 irq = platform_get_irq(pdev, 0);
455 if (irq < 0) {
456 dev_err(&pdev->dev, "missing IRQ\n");
457 err = irq;
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000458 goto disable_pclk;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200459 }
460 err = devm_request_irq(&pdev->dev, irq, atmel_qspi_interrupt,
461 0, dev_name(&pdev->dev), aq);
462 if (err)
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000463 goto disable_pclk;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200464
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200465 err = atmel_qspi_init(aq);
466 if (err)
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000467 goto disable_pclk;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200468
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100469 err = spi_register_controller(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200470 if (err)
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000471 goto disable_pclk;
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200472
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200473 return 0;
474
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000475disable_pclk:
476 clk_disable_unprepare(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200477exit:
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100478 spi_controller_put(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200479
480 return err;
481}
482
483static int atmel_qspi_remove(struct platform_device *pdev)
484{
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100485 struct spi_controller *ctrl = platform_get_drvdata(pdev);
486 struct atmel_qspi *aq = spi_controller_get_devdata(ctrl);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200487
Piotr Bugalski2d30ac52018-11-05 11:36:22 +0100488 spi_unregister_controller(ctrl);
Tudor Ambarus18b6f6e2019-02-05 17:33:11 +0000489 writel_relaxed(QSPI_CR_QSPIDIS, aq->regs + QSPI_CR);
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000490 clk_disable_unprepare(aq->pclk);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200491 return 0;
492}
493
Claudiu Bezneade217c12018-06-04 11:46:33 +0300494static int __maybe_unused atmel_qspi_suspend(struct device *dev)
495{
496 struct atmel_qspi *aq = dev_get_drvdata(dev);
497
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000498 clk_disable_unprepare(aq->pclk);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300499
500 return 0;
501}
502
503static int __maybe_unused atmel_qspi_resume(struct device *dev)
504{
505 struct atmel_qspi *aq = dev_get_drvdata(dev);
506
Tudor Ambarusbd7905e2019-02-05 17:33:33 +0000507 clk_prepare_enable(aq->pclk);
Claudiu Bezneade217c12018-06-04 11:46:33 +0300508
509 return atmel_qspi_init(aq);
510}
511
512static SIMPLE_DEV_PM_OPS(atmel_qspi_pm_ops, atmel_qspi_suspend,
513 atmel_qspi_resume);
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200514
515static const struct of_device_id atmel_qspi_dt_ids[] = {
516 { .compatible = "atmel,sama5d2-qspi" },
517 { /* sentinel */ }
518};
519
520MODULE_DEVICE_TABLE(of, atmel_qspi_dt_ids);
521
522static struct platform_driver atmel_qspi_driver = {
523 .driver = {
524 .name = "atmel_qspi",
525 .of_match_table = atmel_qspi_dt_ids,
Claudiu Bezneade217c12018-06-04 11:46:33 +0300526 .pm = &atmel_qspi_pm_ops,
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200527 },
528 .probe = atmel_qspi_probe,
529 .remove = atmel_qspi_remove,
530};
531module_platform_driver(atmel_qspi_driver);
532
533MODULE_AUTHOR("Cyrille Pitchen <cyrille.pitchen@atmel.com>");
Piotr Bugalskid5433de2018-11-05 11:36:21 +0100534MODULE_AUTHOR("Piotr Bugalski <bugalski.piotr@gmail.com");
Cyrille Pitchen161aaab2016-06-13 17:10:26 +0200535MODULE_DESCRIPTION("Atmel QSPI Controller driver");
536MODULE_LICENSE("GPL v2");