dm: core: Require users of devres to include the header
[platform/kernel/u-boot.git] / drivers / spi / atmel-quadspi.c
1 // 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
12 #include <asm/io.h>
13 #include <clk.h>
14 #include <common.h>
15 #include <dm.h>
16 #include <errno.h>
17 #include <fdtdec.h>
18 #include <linux/err.h>
19 #include <linux/io.h>
20 #include <linux/iopoll.h>
21 #include <linux/ioport.h>
22 #include <mach/clk.h>
23 #include <spi.h>
24 #include <spi-mem.h>
25
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 */
39 #define QSPI_WICR    0x0034  /* Write Instruction Code Register */
40 #define QSPI_IFR     0x0038  /* Instruction Frame Register */
41 #define QSPI_RICR    0x003C  /* Read Instruction Code Register */
42
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 /* Bitfields in QSPI_CR (Control Register) */
52 #define QSPI_CR_QSPIEN                  BIT(0)
53 #define QSPI_CR_QSPIDIS                 BIT(1)
54 #define QSPI_CR_SWRST                   BIT(7)
55 #define QSPI_CR_LASTXFER                BIT(24)
56
57 /* Bitfields in QSPI_MR (Mode Register) */
58 #define QSPI_MR_SMM                     BIT(0)
59 #define QSPI_MR_LLB                     BIT(1)
60 #define QSPI_MR_WDRBT                   BIT(2)
61 #define QSPI_MR_SMRM                    BIT(3)
62 #define QSPI_MR_CSMODE_MASK             GENMASK(5, 4)
63 #define QSPI_MR_CSMODE_NOT_RELOADED     (0 << 4)
64 #define QSPI_MR_CSMODE_LASTXFER         (1 << 4)
65 #define QSPI_MR_CSMODE_SYSTEMATICALLY   (2 << 4)
66 #define QSPI_MR_NBBITS_MASK             GENMASK(11, 8)
67 #define QSPI_MR_NBBITS(n)               ((((n) - 8) << 8) & QSPI_MR_NBBITS_MASK)
68 #define QSPI_MR_DLYBCT_MASK             GENMASK(23, 16)
69 #define QSPI_MR_DLYBCT(n)               (((n) << 16) & QSPI_MR_DLYBCT_MASK)
70 #define QSPI_MR_DLYCS_MASK              GENMASK(31, 24)
71 #define QSPI_MR_DLYCS(n)                (((n) << 24) & QSPI_MR_DLYCS_MASK)
72
73 /* Bitfields in QSPI_SR/QSPI_IER/QSPI_IDR/QSPI_IMR  */
74 #define QSPI_SR_RDRF                    BIT(0)
75 #define QSPI_SR_TDRE                    BIT(1)
76 #define QSPI_SR_TXEMPTY                 BIT(2)
77 #define QSPI_SR_OVRES                   BIT(3)
78 #define QSPI_SR_CSR                     BIT(8)
79 #define QSPI_SR_CSS                     BIT(9)
80 #define QSPI_SR_INSTRE                  BIT(10)
81 #define QSPI_SR_QSPIENS                 BIT(24)
82
83 #define QSPI_SR_CMD_COMPLETED   (QSPI_SR_INSTRE | QSPI_SR_CSR)
84
85 /* Bitfields in QSPI_SCR (Serial Clock Register) */
86 #define QSPI_SCR_CPOL                   BIT(0)
87 #define QSPI_SCR_CPHA                   BIT(1)
88 #define QSPI_SCR_SCBR_MASK              GENMASK(15, 8)
89 #define QSPI_SCR_SCBR(n)                (((n) << 8) & QSPI_SCR_SCBR_MASK)
90 #define QSPI_SCR_DLYBS_MASK             GENMASK(23, 16)
91 #define QSPI_SCR_DLYBS(n)               (((n) << 16) & QSPI_SCR_DLYBS_MASK)
92
93 /* Bitfields in QSPI_ICR (Read/Write Instruction Code Register) */
94 #define QSPI_ICR_INST_MASK              GENMASK(7, 0)
95 #define QSPI_ICR_INST(inst)             (((inst) << 0) & QSPI_ICR_INST_MASK)
96 #define QSPI_ICR_OPT_MASK               GENMASK(23, 16)
97 #define QSPI_ICR_OPT(opt)               (((opt) << 16) & QSPI_ICR_OPT_MASK)
98
99 /* Bitfields in QSPI_IFR (Instruction Frame Register) */
100 #define QSPI_IFR_WIDTH_MASK             GENMASK(2, 0)
101 #define QSPI_IFR_WIDTH_SINGLE_BIT_SPI   (0 << 0)
102 #define QSPI_IFR_WIDTH_DUAL_OUTPUT      (1 << 0)
103 #define QSPI_IFR_WIDTH_QUAD_OUTPUT      (2 << 0)
104 #define QSPI_IFR_WIDTH_DUAL_IO          (3 << 0)
105 #define QSPI_IFR_WIDTH_QUAD_IO          (4 << 0)
106 #define QSPI_IFR_WIDTH_DUAL_CMD         (5 << 0)
107 #define QSPI_IFR_WIDTH_QUAD_CMD         (6 << 0)
108 #define QSPI_IFR_INSTEN                 BIT(4)
109 #define QSPI_IFR_ADDREN                 BIT(5)
110 #define QSPI_IFR_OPTEN                  BIT(6)
111 #define QSPI_IFR_DATAEN                 BIT(7)
112 #define QSPI_IFR_OPTL_MASK              GENMASK(9, 8)
113 #define QSPI_IFR_OPTL_1BIT              (0 << 8)
114 #define QSPI_IFR_OPTL_2BIT              (1 << 8)
115 #define QSPI_IFR_OPTL_4BIT              (2 << 8)
116 #define QSPI_IFR_OPTL_8BIT              (3 << 8)
117 #define QSPI_IFR_ADDRL                  BIT(10)
118 #define QSPI_IFR_TFRTYP_MEM             BIT(12)
119 #define QSPI_IFR_SAMA5D2_WRITE_TRSFR    BIT(13)
120 #define QSPI_IFR_CRM                    BIT(14)
121 #define QSPI_IFR_NBDUM_MASK             GENMASK(20, 16)
122 #define QSPI_IFR_NBDUM(n)               (((n) << 16) & QSPI_IFR_NBDUM_MASK)
123 #define QSPI_IFR_APBTFRTYP_READ         BIT(24) /* Defined in SAM9X60 */
124
125 /* Bitfields in QSPI_SMR (Scrambling Mode Register) */
126 #define QSPI_SMR_SCREN                  BIT(0)
127 #define QSPI_SMR_RVDIS                  BIT(1)
128
129 /* Bitfields in QSPI_WPMR (Write Protection Mode Register) */
130 #define QSPI_WPMR_WPEN                  BIT(0)
131 #define QSPI_WPMR_WPKEY_MASK            GENMASK(31, 8)
132 #define QSPI_WPMR_WPKEY(wpkey)          (((wpkey) << 8) & QSPI_WPMR_WPKEY_MASK)
133
134 /* Bitfields in QSPI_WPSR (Write Protection Status Register) */
135 #define QSPI_WPSR_WPVS                  BIT(0)
136 #define QSPI_WPSR_WPVSRC_MASK           GENMASK(15, 8)
137 #define QSPI_WPSR_WPVSRC(src)           (((src) << 8) & QSPI_WPSR_WPVSRC)
138
139 struct atmel_qspi_caps {
140         bool has_qspick;
141         bool has_ricr;
142 };
143
144 struct atmel_qspi {
145         void __iomem *regs;
146         void __iomem *mem;
147         const struct atmel_qspi_caps *caps;
148         ulong bus_clk_rate;
149         u32 mr;
150 };
151
152 struct atmel_qspi_mode {
153         u8 cmd_buswidth;
154         u8 addr_buswidth;
155         u8 data_buswidth;
156         u32 config;
157 };
158
159 static const struct atmel_qspi_mode atmel_qspi_modes[] = {
160         { 1, 1, 1, QSPI_IFR_WIDTH_SINGLE_BIT_SPI },
161         { 1, 1, 2, QSPI_IFR_WIDTH_DUAL_OUTPUT },
162         { 1, 1, 4, QSPI_IFR_WIDTH_QUAD_OUTPUT },
163         { 1, 2, 2, QSPI_IFR_WIDTH_DUAL_IO },
164         { 1, 4, 4, QSPI_IFR_WIDTH_QUAD_IO },
165         { 2, 2, 2, QSPI_IFR_WIDTH_DUAL_CMD },
166         { 4, 4, 4, QSPI_IFR_WIDTH_QUAD_CMD },
167 };
168
169 static inline bool atmel_qspi_is_compatible(const struct spi_mem_op *op,
170                                             const struct atmel_qspi_mode *mode)
171 {
172         if (op->cmd.buswidth != mode->cmd_buswidth)
173                 return false;
174
175         if (op->addr.nbytes && op->addr.buswidth != mode->addr_buswidth)
176                 return false;
177
178         if (op->data.nbytes && op->data.buswidth != mode->data_buswidth)
179                 return false;
180
181         return true;
182 }
183
184 static int atmel_qspi_find_mode(const struct spi_mem_op *op)
185 {
186         u32 i;
187
188         for (i = 0; i < ARRAY_SIZE(atmel_qspi_modes); i++)
189                 if (atmel_qspi_is_compatible(op, &atmel_qspi_modes[i]))
190                         return i;
191
192         return -ENOTSUPP;
193 }
194
195 static bool atmel_qspi_supports_op(struct spi_slave *slave,
196                                    const struct spi_mem_op *op)
197 {
198         if (atmel_qspi_find_mode(op) < 0)
199                 return false;
200
201         /* special case not supported by hardware */
202         if (op->addr.nbytes == 2 && op->cmd.buswidth != op->addr.buswidth &&
203             op->dummy.nbytes == 0)
204                 return false;
205
206         return true;
207 }
208
209 static int atmel_qspi_set_cfg(struct atmel_qspi *aq,
210                               const struct spi_mem_op *op, u32 *offset)
211 {
212         u32 iar, icr, ifr;
213         u32 dummy_cycles = 0;
214         int mode;
215
216         iar = 0;
217         icr = QSPI_ICR_INST(op->cmd.opcode);
218         ifr = QSPI_IFR_INSTEN;
219
220         mode = atmel_qspi_find_mode(op);
221         if (mode < 0)
222                 return mode;
223         ifr |= atmel_qspi_modes[mode].config;
224
225         if (op->dummy.buswidth && op->dummy.nbytes)
226                 dummy_cycles = op->dummy.nbytes * 8 / op->dummy.buswidth;
227
228         /*
229          * The controller allows 24 and 32-bit addressing while NAND-flash
230          * requires 16-bit long. Handling 8-bit long addresses is done using
231          * the option field. For the 16-bit addresses, the workaround depends
232          * of the number of requested dummy bits. If there are 8 or more dummy
233          * cycles, the address is shifted and sent with the first dummy byte.
234          * Otherwise opcode is disabled and the first byte of the address
235          * contains the command opcode (works only if the opcode and address
236          * use the same buswidth). The limitation is when the 16-bit address is
237          * used without enough dummy cycles and the opcode is using a different
238          * buswidth than the address.
239          */
240         if (op->addr.buswidth) {
241                 switch (op->addr.nbytes) {
242                 case 0:
243                         break;
244                 case 1:
245                         ifr |= QSPI_IFR_OPTEN | QSPI_IFR_OPTL_8BIT;
246                         icr |= QSPI_ICR_OPT(op->addr.val & 0xff);
247                         break;
248                 case 2:
249                         if (dummy_cycles < 8 / op->addr.buswidth) {
250                                 ifr &= ~QSPI_IFR_INSTEN;
251                                 ifr |= QSPI_IFR_ADDREN;
252                                 iar = (op->cmd.opcode << 16) |
253                                         (op->addr.val & 0xffff);
254                         } else {
255                                 ifr |= QSPI_IFR_ADDREN;
256                                 iar = (op->addr.val << 8) & 0xffffff;
257                                 dummy_cycles -= 8 / op->addr.buswidth;
258                         }
259                         break;
260                 case 3:
261                         ifr |= QSPI_IFR_ADDREN;
262                         iar = op->addr.val & 0xffffff;
263                         break;
264                 case 4:
265                         ifr |= QSPI_IFR_ADDREN | QSPI_IFR_ADDRL;
266                         iar = op->addr.val & 0x7ffffff;
267                         break;
268                 default:
269                         return -ENOTSUPP;
270                 }
271         }
272
273         /* offset of the data access in the QSPI memory space */
274         *offset = iar;
275
276         /* Set number of dummy cycles */
277         if (dummy_cycles)
278                 ifr |= QSPI_IFR_NBDUM(dummy_cycles);
279
280         /* Set data enable */
281         if (op->data.nbytes)
282                 ifr |= QSPI_IFR_DATAEN;
283
284         /*
285          * If the QSPI controller is set in regular SPI mode, set it in
286          * Serial Memory Mode (SMM).
287          */
288         if (aq->mr != QSPI_MR_SMM) {
289                 writel(QSPI_MR_SMM, aq->regs + QSPI_MR);
290                 aq->mr = QSPI_MR_SMM;
291         }
292
293         /* Clear pending interrupts */
294         (void)readl(aq->regs + QSPI_SR);
295
296         if (aq->caps->has_ricr) {
297                 if (!op->addr.nbytes && op->data.dir == SPI_MEM_DATA_IN)
298                         ifr |= QSPI_IFR_APBTFRTYP_READ;
299
300                 /* Set QSPI Instruction Frame registers */
301                 writel(iar, aq->regs + QSPI_IAR);
302                 if (op->data.dir == SPI_MEM_DATA_IN)
303                         writel(icr, aq->regs + QSPI_RICR);
304                 else
305                         writel(icr, aq->regs + QSPI_WICR);
306                 writel(ifr, aq->regs + QSPI_IFR);
307         } else {
308                 if (op->data.dir == SPI_MEM_DATA_OUT)
309                         ifr |= QSPI_IFR_SAMA5D2_WRITE_TRSFR;
310
311                 /* Set QSPI Instruction Frame registers */
312                 writel(iar, aq->regs + QSPI_IAR);
313                 writel(icr, aq->regs + QSPI_ICR);
314                 writel(ifr, aq->regs + QSPI_IFR);
315         }
316
317         return 0;
318 }
319
320 static int atmel_qspi_exec_op(struct spi_slave *slave,
321                               const struct spi_mem_op *op)
322 {
323         struct atmel_qspi *aq = dev_get_priv(slave->dev->parent);
324         u32 sr, imr, offset;
325         int err;
326
327         err = atmel_qspi_set_cfg(aq, op, &offset);
328         if (err)
329                 return err;
330
331         /* Skip to the final steps if there is no data */
332         if (op->data.nbytes) {
333                 /* Dummy read of QSPI_IFR to synchronize APB and AHB accesses */
334                 (void)readl(aq->regs + QSPI_IFR);
335
336                 /* Send/Receive data */
337                 if (op->data.dir == SPI_MEM_DATA_IN)
338                         memcpy_fromio(op->data.buf.in, aq->mem + offset,
339                                       op->data.nbytes);
340                 else
341                         memcpy_toio(aq->mem + offset, op->data.buf.out,
342                                     op->data.nbytes);
343
344                 /* Release the chip-select */
345                 writel(QSPI_CR_LASTXFER, aq->regs + QSPI_CR);
346         }
347
348         /* Poll INSTruction End and Chip Select Rise flags. */
349         imr = QSPI_SR_INSTRE | QSPI_SR_CSR;
350         return readl_poll_timeout(aq->regs + QSPI_SR, sr, (sr & imr) == imr,
351                                   1000000);
352 }
353
354 static int atmel_qspi_set_speed(struct udevice *bus, uint hz)
355 {
356         struct atmel_qspi *aq = dev_get_priv(bus);
357         u32 scr, scbr, mask, new_value;
358
359         /* Compute the QSPI baudrate */
360         scbr = DIV_ROUND_UP(aq->bus_clk_rate, hz);
361         if (scbr > 0)
362                 scbr--;
363
364         new_value = QSPI_SCR_SCBR(scbr);
365         mask = QSPI_SCR_SCBR_MASK;
366
367         scr = readl(aq->regs + QSPI_SCR);
368         if ((scr & mask) == new_value)
369                 return 0;
370
371         scr = (scr & ~mask) | new_value;
372         writel(scr, aq->regs + QSPI_SCR);
373
374         return 0;
375 }
376
377 static int atmel_qspi_set_mode(struct udevice *bus, uint mode)
378 {
379         struct atmel_qspi *aq = dev_get_priv(bus);
380         u32 scr, mask, new_value = 0;
381
382         if (mode & SPI_CPOL)
383                 new_value = QSPI_SCR_CPOL;
384         if (mode & SPI_CPHA)
385                 new_value = QSPI_SCR_CPHA;
386
387         mask = QSPI_SCR_CPOL | QSPI_SCR_CPHA;
388
389         scr = readl(aq->regs + QSPI_SCR);
390         if ((scr & mask) == new_value)
391                 return 0;
392
393         scr = (scr & ~mask) | new_value;
394         writel(scr, aq->regs + QSPI_SCR);
395
396         return 0;
397 }
398
399 static int atmel_qspi_enable_clk(struct udevice *dev)
400 {
401         struct atmel_qspi *aq = dev_get_priv(dev);
402         struct clk pclk, qspick;
403         int ret;
404
405         ret = clk_get_by_name(dev, "pclk", &pclk);
406         if (ret)
407                 ret = clk_get_by_index(dev, 0, &pclk);
408
409         if (ret) {
410                 dev_err(dev, "Missing QSPI peripheral clock\n");
411                 return ret;
412         }
413
414         ret = clk_enable(&pclk);
415         if (ret) {
416                 dev_err(dev, "Failed to enable QSPI peripheral clock\n");
417                 goto free_pclk;
418         }
419
420         if (aq->caps->has_qspick) {
421                 /* Get the QSPI system clock */
422                 ret = clk_get_by_name(dev, "qspick", &qspick);
423                 if (ret) {
424                         dev_err(dev, "Missing QSPI peripheral clock\n");
425                         goto free_pclk;
426                 }
427
428                 ret = clk_enable(&qspick);
429                 if (ret)
430                         dev_err(dev, "Failed to enable QSPI system clock\n");
431                 clk_free(&qspick);
432         }
433
434         aq->bus_clk_rate = clk_get_rate(&pclk);
435         if (!aq->bus_clk_rate)
436                 ret = -EINVAL;
437
438 free_pclk:
439         clk_free(&pclk);
440
441         return ret;
442 }
443
444 static void atmel_qspi_init(struct atmel_qspi *aq)
445 {
446         /* Reset the QSPI controller */
447         writel(QSPI_CR_SWRST, aq->regs + QSPI_CR);
448
449         /* Set the QSPI controller by default in Serial Memory Mode */
450         writel(QSPI_MR_SMM, aq->regs + QSPI_MR);
451         aq->mr = QSPI_MR_SMM;
452
453         /* Enable the QSPI controller */
454         writel(QSPI_CR_QSPIEN, aq->regs + QSPI_CR);
455 }
456
457 static int atmel_qspi_probe(struct udevice *dev)
458 {
459         struct atmel_qspi *aq = dev_get_priv(dev);
460         struct resource res;
461         int ret;
462
463         aq->caps = (struct atmel_qspi_caps *)dev_get_driver_data(dev);
464         if (!aq->caps) {
465                 dev_err(dev, "Could not retrieve QSPI caps\n");
466                 return -EINVAL;
467         };
468
469         /* Map the registers */
470         ret = dev_read_resource_byname(dev, "qspi_base", &res);
471         if (ret) {
472                 dev_err(dev, "missing registers\n");
473                 return ret;
474         }
475
476         aq->regs = devm_ioremap(dev, res.start, resource_size(&res));
477         if (IS_ERR(aq->regs))
478                 return PTR_ERR(aq->regs);
479
480         /* Map the AHB memory */
481         ret = dev_read_resource_byname(dev, "qspi_mmap", &res);
482         if (ret) {
483                 dev_err(dev, "missing AHB memory\n");
484                 return ret;
485         }
486
487         aq->mem = devm_ioremap(dev, res.start, resource_size(&res));
488         if (IS_ERR(aq->mem))
489                 return PTR_ERR(aq->mem);
490
491         ret = atmel_qspi_enable_clk(dev);
492         if (ret)
493                 return ret;
494
495         atmel_qspi_init(aq);
496
497         return 0;
498 }
499
500 static const struct spi_controller_mem_ops atmel_qspi_mem_ops = {
501         .supports_op = atmel_qspi_supports_op,
502         .exec_op = atmel_qspi_exec_op,
503 };
504
505 static const struct dm_spi_ops atmel_qspi_ops = {
506         .set_speed = atmel_qspi_set_speed,
507         .set_mode = atmel_qspi_set_mode,
508         .mem_ops = &atmel_qspi_mem_ops,
509 };
510
511 static const struct atmel_qspi_caps atmel_sama5d2_qspi_caps = {};
512
513 static const struct atmel_qspi_caps atmel_sam9x60_qspi_caps = {
514         .has_qspick = true,
515         .has_ricr = true,
516 };
517
518 static const struct udevice_id atmel_qspi_ids[] = {
519         {
520                 .compatible = "atmel,sama5d2-qspi",
521                 .data = (ulong)&atmel_sama5d2_qspi_caps,
522         },
523         {
524                 .compatible = "microchip,sam9x60-qspi",
525                 .data = (ulong)&atmel_sam9x60_qspi_caps,
526         },
527         { /* sentinel */ }
528 };
529
530 U_BOOT_DRIVER(atmel_qspi) = {
531         .name           = "atmel_qspi",
532         .id             = UCLASS_SPI,
533         .of_match       = atmel_qspi_ids,
534         .ops            = &atmel_qspi_ops,
535         .priv_auto_alloc_size = sizeof(struct atmel_qspi),
536         .probe          = atmel_qspi_probe,
537 };