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