1
2
3
4
5
6
7
8
9
10
11#include <linux/kernel.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include <linux/errno.h>
15#include <linux/platform_device.h>
16#include <linux/sched.h>
17#include <linux/delay.h>
18#include <linux/io.h>
19#include <linux/clk.h>
20#include <linux/err.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/timer.h>
24#include <linux/jiffies.h>
25#include <linux/completion.h>
26#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
28#include <linux/mtd/spi-nor.h>
29#include <linux/mutex.h>
30#include <linux/pm_qos.h>
31#include <linux/sizes.h>
32
33
34#define QUADSPI_QUIRK_SWAP_ENDIAN (1 << 0)
35
36#define QUADSPI_QUIRK_4X_INT_CLK (1 << 1)
37
38
39
40
41#define QUADSPI_QUIRK_TKT253890 (1 << 2)
42
43#define QUADSPI_QUIRK_TKT245618 (1 << 3)
44
45
46#define QUADSPI_MCR 0x00
47#define QUADSPI_MCR_RESERVED_SHIFT 16
48#define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
49#define QUADSPI_MCR_MDIS_SHIFT 14
50#define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
51#define QUADSPI_MCR_CLR_TXF_SHIFT 11
52#define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
53#define QUADSPI_MCR_CLR_RXF_SHIFT 10
54#define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
55#define QUADSPI_MCR_DDR_EN_SHIFT 7
56#define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
57#define QUADSPI_MCR_END_CFG_SHIFT 2
58#define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
59#define QUADSPI_MCR_SWRSTHD_SHIFT 1
60#define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
61#define QUADSPI_MCR_SWRSTSD_SHIFT 0
62#define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
63
64#define QUADSPI_IPCR 0x08
65#define QUADSPI_IPCR_SEQID_SHIFT 24
66#define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
67
68#define QUADSPI_BUF0CR 0x10
69#define QUADSPI_BUF1CR 0x14
70#define QUADSPI_BUF2CR 0x18
71#define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
72
73#define QUADSPI_BUF3CR 0x1c
74#define QUADSPI_BUF3CR_ALLMST_SHIFT 31
75#define QUADSPI_BUF3CR_ALLMST_MASK (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
76#define QUADSPI_BUF3CR_ADATSZ_SHIFT 8
77#define QUADSPI_BUF3CR_ADATSZ_MASK (0xFF << QUADSPI_BUF3CR_ADATSZ_SHIFT)
78
79#define QUADSPI_BFGENCR 0x20
80#define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
81#define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
82#define QUADSPI_BFGENCR_SEQID_SHIFT 12
83#define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
84
85#define QUADSPI_BUF0IND 0x30
86#define QUADSPI_BUF1IND 0x34
87#define QUADSPI_BUF2IND 0x38
88#define QUADSPI_SFAR 0x100
89
90#define QUADSPI_SMPR 0x108
91#define QUADSPI_SMPR_DDRSMP_SHIFT 16
92#define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
93#define QUADSPI_SMPR_FSDLY_SHIFT 6
94#define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
95#define QUADSPI_SMPR_FSPHS_SHIFT 5
96#define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
97#define QUADSPI_SMPR_HSENA_SHIFT 0
98#define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
99
100#define QUADSPI_RBSR 0x10c
101#define QUADSPI_RBSR_RDBFL_SHIFT 8
102#define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
103
104#define QUADSPI_RBCT 0x110
105#define QUADSPI_RBCT_WMRK_MASK 0x1F
106#define QUADSPI_RBCT_RXBRD_SHIFT 8
107#define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
108
109#define QUADSPI_TBSR 0x150
110#define QUADSPI_TBDR 0x154
111#define QUADSPI_SR 0x15c
112#define QUADSPI_SR_IP_ACC_SHIFT 1
113#define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
114#define QUADSPI_SR_AHB_ACC_SHIFT 2
115#define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
116
117#define QUADSPI_FR 0x160
118#define QUADSPI_FR_TFF_MASK 0x1
119
120#define QUADSPI_SFA1AD 0x180
121#define QUADSPI_SFA2AD 0x184
122#define QUADSPI_SFB1AD 0x188
123#define QUADSPI_SFB2AD 0x18c
124#define QUADSPI_RBDR 0x200
125
126#define QUADSPI_LUTKEY 0x300
127#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
128
129#define QUADSPI_LCKCR 0x304
130#define QUADSPI_LCKER_LOCK 0x1
131#define QUADSPI_LCKER_UNLOCK 0x2
132
133#define QUADSPI_RSER 0x164
134#define QUADSPI_RSER_TFIE (0x1 << 0)
135
136#define QUADSPI_LUT_BASE 0x310
137
138
139
140
141
142
143
144
145#define OPRND0_SHIFT 0
146#define PAD0_SHIFT 8
147#define INSTR0_SHIFT 10
148#define OPRND1_SHIFT 16
149
150
151#define LUT_STOP 0
152#define LUT_CMD 1
153#define LUT_ADDR 2
154#define LUT_DUMMY 3
155#define LUT_MODE 4
156#define LUT_MODE2 5
157#define LUT_MODE4 6
158#define LUT_FSL_READ 7
159#define LUT_FSL_WRITE 8
160#define LUT_JMP_ON_CS 9
161#define LUT_ADDR_DDR 10
162#define LUT_MODE_DDR 11
163#define LUT_MODE2_DDR 12
164#define LUT_MODE4_DDR 13
165#define LUT_FSL_READ_DDR 14
166#define LUT_FSL_WRITE_DDR 15
167#define LUT_DATA_LEARN 16
168
169
170
171
172
173
174
175
176#define LUT_PAD1 0
177#define LUT_PAD2 1
178#define LUT_PAD4 2
179
180
181#define ADDR24BIT 0x18
182#define ADDR32BIT 0x20
183
184
185#define LUT0(ins, pad, opr) \
186 (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
187 ((LUT_##ins) << INSTR0_SHIFT))
188
189#define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
190
191
192#define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
193#define QUADSPI_LUT_NUM 64
194
195
196#define SEQID_READ 0
197#define SEQID_WREN 1
198#define SEQID_WRDI 2
199#define SEQID_RDSR 3
200#define SEQID_SE 4
201#define SEQID_CHIP_ERASE 5
202#define SEQID_PP 6
203#define SEQID_RDID 7
204#define SEQID_WRSR 8
205#define SEQID_RDCR 9
206#define SEQID_EN4B 10
207#define SEQID_BRWR 11
208
209#define QUADSPI_MIN_IOMAP SZ_4M
210
211enum fsl_qspi_devtype {
212 FSL_QUADSPI_VYBRID,
213 FSL_QUADSPI_IMX6SX,
214 FSL_QUADSPI_IMX7D,
215 FSL_QUADSPI_IMX6UL,
216 FSL_QUADSPI_LS1021A,
217};
218
219struct fsl_qspi_devtype_data {
220 enum fsl_qspi_devtype devtype;
221 int rxfifo;
222 int txfifo;
223 int ahb_buf_size;
224 int driver_data;
225};
226
227static const struct fsl_qspi_devtype_data vybrid_data = {
228 .devtype = FSL_QUADSPI_VYBRID,
229 .rxfifo = 128,
230 .txfifo = 64,
231 .ahb_buf_size = 1024,
232 .driver_data = QUADSPI_QUIRK_SWAP_ENDIAN,
233};
234
235static const struct fsl_qspi_devtype_data imx6sx_data = {
236 .devtype = FSL_QUADSPI_IMX6SX,
237 .rxfifo = 128,
238 .txfifo = 512,
239 .ahb_buf_size = 1024,
240 .driver_data = QUADSPI_QUIRK_4X_INT_CLK
241 | QUADSPI_QUIRK_TKT245618,
242};
243
244static const struct fsl_qspi_devtype_data imx7d_data = {
245 .devtype = FSL_QUADSPI_IMX7D,
246 .rxfifo = 512,
247 .txfifo = 512,
248 .ahb_buf_size = 1024,
249 .driver_data = QUADSPI_QUIRK_TKT253890
250 | QUADSPI_QUIRK_4X_INT_CLK,
251};
252
253static const struct fsl_qspi_devtype_data imx6ul_data = {
254 .devtype = FSL_QUADSPI_IMX6UL,
255 .rxfifo = 128,
256 .txfifo = 512,
257 .ahb_buf_size = 1024,
258 .driver_data = QUADSPI_QUIRK_TKT253890
259 | QUADSPI_QUIRK_4X_INT_CLK,
260};
261
262static struct fsl_qspi_devtype_data ls1021a_data = {
263 .devtype = FSL_QUADSPI_LS1021A,
264 .rxfifo = 128,
265 .txfifo = 64,
266 .ahb_buf_size = 1024,
267 .driver_data = 0,
268};
269
270#define FSL_QSPI_MAX_CHIP 4
271struct fsl_qspi {
272 struct spi_nor nor[FSL_QSPI_MAX_CHIP];
273 void __iomem *iobase;
274 void __iomem *ahb_addr;
275 u32 memmap_phy;
276 u32 memmap_offs;
277 u32 memmap_len;
278 struct clk *clk, *clk_en;
279 struct device *dev;
280 struct completion c;
281 const struct fsl_qspi_devtype_data *devtype_data;
282 u32 nor_size;
283 u32 nor_num;
284 u32 clk_rate;
285 unsigned int chip_base_addr;
286 bool has_second_chip;
287 bool big_endian;
288 struct mutex lock;
289 struct pm_qos_request pm_qos_req;
290};
291
292static inline int needs_swap_endian(struct fsl_qspi *q)
293{
294 return q->devtype_data->driver_data & QUADSPI_QUIRK_SWAP_ENDIAN;
295}
296
297static inline int needs_4x_clock(struct fsl_qspi *q)
298{
299 return q->devtype_data->driver_data & QUADSPI_QUIRK_4X_INT_CLK;
300}
301
302static inline int needs_fill_txfifo(struct fsl_qspi *q)
303{
304 return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT253890;
305}
306
307static inline int needs_wakeup_wait_mode(struct fsl_qspi *q)
308{
309 return q->devtype_data->driver_data & QUADSPI_QUIRK_TKT245618;
310}
311
312
313
314
315
316
317
318static void qspi_writel(struct fsl_qspi *q, u32 val, void __iomem *addr)
319{
320 if (q->big_endian)
321 iowrite32be(val, addr);
322 else
323 iowrite32(val, addr);
324}
325
326static u32 qspi_readl(struct fsl_qspi *q, void __iomem *addr)
327{
328 if (q->big_endian)
329 return ioread32be(addr);
330 else
331 return ioread32(addr);
332}
333
334
335
336
337
338static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
339{
340 return needs_swap_endian(q) ? __swab32(a) : a;
341}
342
343static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
344{
345 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
346 qspi_writel(q, QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
347}
348
349static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
350{
351 qspi_writel(q, QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
352 qspi_writel(q, QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
353}
354
355static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
356{
357 struct fsl_qspi *q = dev_id;
358 u32 reg;
359
360
361 reg = qspi_readl(q, q->iobase + QUADSPI_FR);
362 qspi_writel(q, reg, q->iobase + QUADSPI_FR);
363
364 if (reg & QUADSPI_FR_TFF_MASK)
365 complete(&q->c);
366
367 dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
368 return IRQ_HANDLED;
369}
370
371static void fsl_qspi_init_lut(struct fsl_qspi *q)
372{
373 void __iomem *base = q->iobase;
374 int rxfifo = q->devtype_data->rxfifo;
375 u32 lut_base;
376 int i;
377
378 struct spi_nor *nor = &q->nor[0];
379 u8 addrlen = (nor->addr_width == 3) ? ADDR24BIT : ADDR32BIT;
380 u8 read_op = nor->read_opcode;
381 u8 read_dm = nor->read_dummy;
382
383 fsl_qspi_unlock_lut(q);
384
385
386 for (i = 0; i < QUADSPI_LUT_NUM; i++)
387 qspi_writel(q, 0, base + QUADSPI_LUT_BASE + i * 4);
388
389
390 lut_base = SEQID_READ * 4;
391
392 qspi_writel(q, LUT0(CMD, PAD1, read_op) | LUT1(ADDR, PAD1, addrlen),
393 base + QUADSPI_LUT(lut_base));
394 qspi_writel(q, LUT0(DUMMY, PAD1, read_dm) |
395 LUT1(FSL_READ, PAD4, rxfifo),
396 base + QUADSPI_LUT(lut_base + 1));
397
398
399 lut_base = SEQID_WREN * 4;
400 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WREN),
401 base + QUADSPI_LUT(lut_base));
402
403
404 lut_base = SEQID_PP * 4;
405
406 qspi_writel(q, LUT0(CMD, PAD1, nor->program_opcode) |
407 LUT1(ADDR, PAD1, addrlen),
408 base + QUADSPI_LUT(lut_base));
409 qspi_writel(q, LUT0(FSL_WRITE, PAD1, 0),
410 base + QUADSPI_LUT(lut_base + 1));
411
412
413 lut_base = SEQID_RDSR * 4;
414 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDSR) |
415 LUT1(FSL_READ, PAD1, 0x1),
416 base + QUADSPI_LUT(lut_base));
417
418
419 lut_base = SEQID_SE * 4;
420
421 qspi_writel(q, LUT0(CMD, PAD1, nor->erase_opcode) |
422 LUT1(ADDR, PAD1, addrlen),
423 base + QUADSPI_LUT(lut_base));
424
425
426 lut_base = SEQID_CHIP_ERASE * 4;
427 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
428 base + QUADSPI_LUT(lut_base));
429
430
431 lut_base = SEQID_RDID * 4;
432 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDID) |
433 LUT1(FSL_READ, PAD1, 0x8),
434 base + QUADSPI_LUT(lut_base));
435
436
437 lut_base = SEQID_WRSR * 4;
438 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRSR) |
439 LUT1(FSL_WRITE, PAD1, 0x2),
440 base + QUADSPI_LUT(lut_base));
441
442
443 lut_base = SEQID_RDCR * 4;
444 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_RDCR) |
445 LUT1(FSL_READ, PAD1, 0x1),
446 base + QUADSPI_LUT(lut_base));
447
448
449 lut_base = SEQID_WRDI * 4;
450 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_WRDI),
451 base + QUADSPI_LUT(lut_base));
452
453
454 lut_base = SEQID_EN4B * 4;
455 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_EN4B),
456 base + QUADSPI_LUT(lut_base));
457
458
459 lut_base = SEQID_BRWR * 4;
460 qspi_writel(q, LUT0(CMD, PAD1, SPINOR_OP_BRWR),
461 base + QUADSPI_LUT(lut_base));
462
463 fsl_qspi_lock_lut(q);
464}
465
466
467static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
468{
469 switch (cmd) {
470 case SPINOR_OP_READ_1_1_4:
471 return SEQID_READ;
472 case SPINOR_OP_WREN:
473 return SEQID_WREN;
474 case SPINOR_OP_WRDI:
475 return SEQID_WRDI;
476 case SPINOR_OP_RDSR:
477 return SEQID_RDSR;
478 case SPINOR_OP_SE:
479 return SEQID_SE;
480 case SPINOR_OP_CHIP_ERASE:
481 return SEQID_CHIP_ERASE;
482 case SPINOR_OP_PP:
483 return SEQID_PP;
484 case SPINOR_OP_RDID:
485 return SEQID_RDID;
486 case SPINOR_OP_WRSR:
487 return SEQID_WRSR;
488 case SPINOR_OP_RDCR:
489 return SEQID_RDCR;
490 case SPINOR_OP_EN4B:
491 return SEQID_EN4B;
492 case SPINOR_OP_BRWR:
493 return SEQID_BRWR;
494 default:
495 if (cmd == q->nor[0].erase_opcode)
496 return SEQID_SE;
497 dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
498 break;
499 }
500 return -EINVAL;
501}
502
503static int
504fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
505{
506 void __iomem *base = q->iobase;
507 int seqid;
508 u32 reg, reg2;
509 int err;
510
511 init_completion(&q->c);
512 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
513 q->chip_base_addr, addr, len, cmd);
514
515
516 reg = qspi_readl(q, base + QUADSPI_MCR);
517
518 qspi_writel(q, q->memmap_phy + q->chip_base_addr + addr,
519 base + QUADSPI_SFAR);
520 qspi_writel(q, QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
521 base + QUADSPI_RBCT);
522 qspi_writel(q, reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
523
524 do {
525 reg2 = qspi_readl(q, base + QUADSPI_SR);
526 if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
527 udelay(1);
528 dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
529 continue;
530 }
531 break;
532 } while (1);
533
534
535 seqid = fsl_qspi_get_seqid(q, cmd);
536 qspi_writel(q, (seqid << QUADSPI_IPCR_SEQID_SHIFT) | len,
537 base + QUADSPI_IPCR);
538
539
540 if (!wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000))) {
541 dev_err(q->dev,
542 "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
543 cmd, addr, qspi_readl(q, base + QUADSPI_FR),
544 qspi_readl(q, base + QUADSPI_SR));
545 err = -ETIMEDOUT;
546 } else {
547 err = 0;
548 }
549
550
551 qspi_writel(q, reg, base + QUADSPI_MCR);
552
553 return err;
554}
555
556
557static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
558{
559 u32 tmp;
560 int i = 0;
561
562 while (len > 0) {
563 tmp = qspi_readl(q, q->iobase + QUADSPI_RBDR + i * 4);
564 tmp = fsl_qspi_endian_xchg(q, tmp);
565 dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
566 q->chip_base_addr, tmp);
567
568 if (len >= 4) {
569 *((u32 *)rxbuf) = tmp;
570 rxbuf += 4;
571 } else {
572 memcpy(rxbuf, &tmp, len);
573 break;
574 }
575
576 len -= 4;
577 i++;
578 }
579}
580
581
582
583
584
585
586
587static inline void fsl_qspi_invalid(struct fsl_qspi *q)
588{
589 u32 reg;
590
591 reg = qspi_readl(q, q->iobase + QUADSPI_MCR);
592 reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
593 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
594
595
596
597
598
599 udelay(1);
600
601 reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
602 qspi_writel(q, reg, q->iobase + QUADSPI_MCR);
603}
604
605static ssize_t fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
606 u8 opcode, unsigned int to, u32 *txbuf,
607 unsigned count)
608{
609 int ret, i, j;
610 u32 tmp;
611
612 dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
613 q->chip_base_addr, to, count);
614
615
616 tmp = qspi_readl(q, q->iobase + QUADSPI_MCR);
617 qspi_writel(q, tmp | QUADSPI_MCR_CLR_TXF_MASK, q->iobase + QUADSPI_MCR);
618
619
620 for (j = 0, i = ((count + 3) / 4); j < i; j++) {
621 tmp = fsl_qspi_endian_xchg(q, *txbuf);
622 qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
623 txbuf++;
624 }
625
626
627 if (needs_fill_txfifo(q))
628 for (; i < 4; i++)
629 qspi_writel(q, tmp, q->iobase + QUADSPI_TBDR);
630
631
632 ret = fsl_qspi_runcmd(q, opcode, to, count);
633
634 if (ret == 0)
635 return count;
636
637 return ret;
638}
639
640static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
641{
642 int nor_size = q->nor_size;
643 void __iomem *base = q->iobase;
644
645 qspi_writel(q, nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
646 qspi_writel(q, nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
647 qspi_writel(q, nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
648 qspi_writel(q, nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
649}
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
665{
666 void __iomem *base = q->iobase;
667 int seqid;
668
669
670 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
671 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
672 qspi_writel(q, QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
673
674
675
676
677 qspi_writel(q, QUADSPI_BUF3CR_ALLMST_MASK |
678 ((q->devtype_data->ahb_buf_size / 8)
679 << QUADSPI_BUF3CR_ADATSZ_SHIFT),
680 base + QUADSPI_BUF3CR);
681
682
683 qspi_writel(q, 0, base + QUADSPI_BUF0IND);
684 qspi_writel(q, 0, base + QUADSPI_BUF1IND);
685 qspi_writel(q, 0, base + QUADSPI_BUF2IND);
686
687
688 seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
689 qspi_writel(q, seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
690 q->iobase + QUADSPI_BFGENCR);
691}
692
693
694static int fsl_qspi_clk_prep_enable(struct fsl_qspi *q)
695{
696 int ret;
697
698 ret = clk_prepare_enable(q->clk_en);
699 if (ret)
700 return ret;
701
702 ret = clk_prepare_enable(q->clk);
703 if (ret) {
704 clk_disable_unprepare(q->clk_en);
705 return ret;
706 }
707
708 if (needs_wakeup_wait_mode(q))
709 pm_qos_add_request(&q->pm_qos_req, PM_QOS_CPU_DMA_LATENCY, 0);
710
711 return 0;
712}
713
714
715static void fsl_qspi_clk_disable_unprep(struct fsl_qspi *q)
716{
717 if (needs_wakeup_wait_mode(q))
718 pm_qos_remove_request(&q->pm_qos_req);
719
720 clk_disable_unprepare(q->clk);
721 clk_disable_unprepare(q->clk_en);
722
723}
724
725
726static int fsl_qspi_nor_setup(struct fsl_qspi *q)
727{
728 void __iomem *base = q->iobase;
729 u32 reg;
730 int ret;
731
732
733 fsl_qspi_clk_disable_unprep(q);
734
735
736 ret = clk_set_rate(q->clk, 66000000);
737 if (ret)
738 return ret;
739
740 ret = fsl_qspi_clk_prep_enable(q);
741 if (ret)
742 return ret;
743
744
745 qspi_writel(q, QUADSPI_MCR_SWRSTSD_MASK | QUADSPI_MCR_SWRSTHD_MASK,
746 base + QUADSPI_MCR);
747 udelay(1);
748
749
750 fsl_qspi_init_lut(q);
751
752
753 qspi_writel(q, QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
754 base + QUADSPI_MCR);
755
756 reg = qspi_readl(q, base + QUADSPI_SMPR);
757 qspi_writel(q, reg & ~(QUADSPI_SMPR_FSDLY_MASK
758 | QUADSPI_SMPR_FSPHS_MASK
759 | QUADSPI_SMPR_HSENA_MASK
760 | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
761
762
763 qspi_writel(q, QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
764 base + QUADSPI_MCR);
765
766
767 qspi_writel(q, 0xffffffff, q->iobase + QUADSPI_FR);
768
769
770 qspi_writel(q, QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
771
772 return 0;
773}
774
775static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
776{
777 unsigned long rate = q->clk_rate;
778 int ret;
779
780 if (needs_4x_clock(q))
781 rate *= 4;
782
783
784 fsl_qspi_clk_disable_unprep(q);
785
786 ret = clk_set_rate(q->clk, rate);
787 if (ret)
788 return ret;
789
790 ret = fsl_qspi_clk_prep_enable(q);
791 if (ret)
792 return ret;
793
794
795 fsl_qspi_init_lut(q);
796
797
798 fsl_qspi_init_abh_read(q);
799
800 return 0;
801}
802
803static const struct of_device_id fsl_qspi_dt_ids[] = {
804 { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
805 { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
806 { .compatible = "fsl,imx7d-qspi", .data = (void *)&imx7d_data, },
807 { .compatible = "fsl,imx6ul-qspi", .data = (void *)&imx6ul_data, },
808 { .compatible = "fsl,ls1021a-qspi", .data = (void *)&ls1021a_data, },
809 { }
810};
811MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
812
813static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
814{
815 q->chip_base_addr = q->nor_size * (nor - q->nor);
816}
817
818static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
819{
820 int ret;
821 struct fsl_qspi *q = nor->priv;
822
823 ret = fsl_qspi_runcmd(q, opcode, 0, len);
824 if (ret)
825 return ret;
826
827 fsl_qspi_read_data(q, len, buf);
828 return 0;
829}
830
831static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
832{
833 struct fsl_qspi *q = nor->priv;
834 int ret;
835
836 if (!buf) {
837 ret = fsl_qspi_runcmd(q, opcode, 0, 1);
838 if (ret)
839 return ret;
840
841 if (opcode == SPINOR_OP_CHIP_ERASE)
842 fsl_qspi_invalid(q);
843
844 } else if (len > 0) {
845 ret = fsl_qspi_nor_write(q, nor, opcode, 0,
846 (u32 *)buf, len);
847 if (ret > 0)
848 return 0;
849 } else {
850 dev_err(q->dev, "invalid cmd %d\n", opcode);
851 ret = -EINVAL;
852 }
853
854 return ret;
855}
856
857static ssize_t fsl_qspi_write(struct spi_nor *nor, loff_t to,
858 size_t len, const u_char *buf)
859{
860 struct fsl_qspi *q = nor->priv;
861 ssize_t ret = fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
862 (u32 *)buf, len);
863
864
865 fsl_qspi_invalid(q);
866 return ret;
867}
868
869static ssize_t fsl_qspi_read(struct spi_nor *nor, loff_t from,
870 size_t len, u_char *buf)
871{
872 struct fsl_qspi *q = nor->priv;
873 u8 cmd = nor->read_opcode;
874
875
876 if (!q->ahb_addr) {
877 q->memmap_offs = q->chip_base_addr + from;
878 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
879
880 q->ahb_addr = ioremap_nocache(
881 q->memmap_phy + q->memmap_offs,
882 q->memmap_len);
883 if (!q->ahb_addr) {
884 dev_err(q->dev, "ioremap failed\n");
885 return -ENOMEM;
886 }
887
888 } else if (q->chip_base_addr + from < q->memmap_offs
889 || q->chip_base_addr + from + len >
890 q->memmap_offs + q->memmap_len) {
891 iounmap(q->ahb_addr);
892
893 q->memmap_offs = q->chip_base_addr + from;
894 q->memmap_len = len > QUADSPI_MIN_IOMAP ? len : QUADSPI_MIN_IOMAP;
895 q->ahb_addr = ioremap_nocache(
896 q->memmap_phy + q->memmap_offs,
897 q->memmap_len);
898 if (!q->ahb_addr) {
899 dev_err(q->dev, "ioremap failed\n");
900 return -ENOMEM;
901 }
902 }
903
904 dev_dbg(q->dev, "cmd [%x],read from %p, len:%zd\n",
905 cmd, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
906 len);
907
908
909 memcpy(buf, q->ahb_addr + q->chip_base_addr + from - q->memmap_offs,
910 len);
911
912 return len;
913}
914
915static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
916{
917 struct fsl_qspi *q = nor->priv;
918 int ret;
919
920 dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
921 nor->mtd.erasesize / 1024, q->chip_base_addr, (u32)offs);
922
923 ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
924 if (ret)
925 return ret;
926
927 fsl_qspi_invalid(q);
928 return 0;
929}
930
931static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
932{
933 struct fsl_qspi *q = nor->priv;
934 int ret;
935
936 mutex_lock(&q->lock);
937
938 ret = fsl_qspi_clk_prep_enable(q);
939 if (ret)
940 goto err_mutex;
941
942 fsl_qspi_set_base_addr(q, nor);
943 return 0;
944
945err_mutex:
946 mutex_unlock(&q->lock);
947 return ret;
948}
949
950static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
951{
952 struct fsl_qspi *q = nor->priv;
953
954 fsl_qspi_clk_disable_unprep(q);
955 mutex_unlock(&q->lock);
956}
957
958static int fsl_qspi_probe(struct platform_device *pdev)
959{
960 const struct spi_nor_hwcaps hwcaps = {
961 .mask = SNOR_HWCAPS_READ_1_1_4 |
962 SNOR_HWCAPS_PP,
963 };
964 struct device_node *np = pdev->dev.of_node;
965 struct device *dev = &pdev->dev;
966 struct fsl_qspi *q;
967 struct resource *res;
968 struct spi_nor *nor;
969 struct mtd_info *mtd;
970 int ret, i = 0;
971
972 q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
973 if (!q)
974 return -ENOMEM;
975
976 q->nor_num = of_get_child_count(dev->of_node);
977 if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
978 return -ENODEV;
979
980 q->dev = dev;
981 q->devtype_data = of_device_get_match_data(dev);
982 if (!q->devtype_data)
983 return -ENODEV;
984 platform_set_drvdata(pdev, q);
985
986
987 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
988 q->iobase = devm_ioremap_resource(dev, res);
989 if (IS_ERR(q->iobase))
990 return PTR_ERR(q->iobase);
991
992 q->big_endian = of_property_read_bool(np, "big-endian");
993 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
994 "QuadSPI-memory");
995 if (!devm_request_mem_region(dev, res->start, resource_size(res),
996 res->name)) {
997 dev_err(dev, "can't request region for resource %pR\n", res);
998 return -EBUSY;
999 }
1000
1001 q->memmap_phy = res->start;
1002
1003
1004 q->clk_en = devm_clk_get(dev, "qspi_en");
1005 if (IS_ERR(q->clk_en))
1006 return PTR_ERR(q->clk_en);
1007
1008 q->clk = devm_clk_get(dev, "qspi");
1009 if (IS_ERR(q->clk))
1010 return PTR_ERR(q->clk);
1011
1012 ret = fsl_qspi_clk_prep_enable(q);
1013 if (ret) {
1014 dev_err(dev, "can not enable the clock\n");
1015 goto clk_failed;
1016 }
1017
1018
1019 ret = platform_get_irq(pdev, 0);
1020 if (ret < 0) {
1021 dev_err(dev, "failed to get the irq: %d\n", ret);
1022 goto irq_failed;
1023 }
1024
1025 ret = devm_request_irq(dev, ret,
1026 fsl_qspi_irq_handler, 0, pdev->name, q);
1027 if (ret) {
1028 dev_err(dev, "failed to request irq: %d\n", ret);
1029 goto irq_failed;
1030 }
1031
1032 ret = fsl_qspi_nor_setup(q);
1033 if (ret)
1034 goto irq_failed;
1035
1036 if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
1037 q->has_second_chip = true;
1038
1039 mutex_init(&q->lock);
1040
1041
1042 for_each_available_child_of_node(dev->of_node, np) {
1043
1044 if (!q->has_second_chip)
1045 i *= 2;
1046
1047 nor = &q->nor[i];
1048 mtd = &nor->mtd;
1049
1050 nor->dev = dev;
1051 spi_nor_set_flash_node(nor, np);
1052 nor->priv = q;
1053
1054
1055 nor->read_reg = fsl_qspi_read_reg;
1056 nor->write_reg = fsl_qspi_write_reg;
1057 nor->read = fsl_qspi_read;
1058 nor->write = fsl_qspi_write;
1059 nor->erase = fsl_qspi_erase;
1060
1061 nor->prepare = fsl_qspi_prep;
1062 nor->unprepare = fsl_qspi_unprep;
1063
1064 ret = of_property_read_u32(np, "spi-max-frequency",
1065 &q->clk_rate);
1066 if (ret < 0)
1067 goto mutex_failed;
1068
1069
1070 fsl_qspi_set_base_addr(q, nor);
1071
1072 ret = spi_nor_scan(nor, NULL, &hwcaps);
1073 if (ret)
1074 goto mutex_failed;
1075
1076 ret = mtd_device_register(mtd, NULL, 0);
1077 if (ret)
1078 goto mutex_failed;
1079
1080
1081 if (q->nor_size == 0) {
1082 q->nor_size = mtd->size;
1083
1084
1085 fsl_qspi_set_map_addr(q);
1086 }
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097 if (nor->page_size > q->devtype_data->txfifo)
1098 nor->page_size = q->devtype_data->txfifo;
1099
1100 i++;
1101 }
1102
1103
1104 ret = fsl_qspi_nor_setup_last(q);
1105 if (ret)
1106 goto last_init_failed;
1107
1108 fsl_qspi_clk_disable_unprep(q);
1109 return 0;
1110
1111last_init_failed:
1112 for (i = 0; i < q->nor_num; i++) {
1113
1114 if (!q->has_second_chip)
1115 i *= 2;
1116 mtd_device_unregister(&q->nor[i].mtd);
1117 }
1118mutex_failed:
1119 mutex_destroy(&q->lock);
1120irq_failed:
1121 fsl_qspi_clk_disable_unprep(q);
1122clk_failed:
1123 dev_err(dev, "Freescale QuadSPI probe failed\n");
1124 return ret;
1125}
1126
1127static int fsl_qspi_remove(struct platform_device *pdev)
1128{
1129 struct fsl_qspi *q = platform_get_drvdata(pdev);
1130 int i;
1131
1132 for (i = 0; i < q->nor_num; i++) {
1133
1134 if (!q->has_second_chip)
1135 i *= 2;
1136 mtd_device_unregister(&q->nor[i].mtd);
1137 }
1138
1139
1140 qspi_writel(q, QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
1141 qspi_writel(q, 0x0, q->iobase + QUADSPI_RSER);
1142
1143 mutex_destroy(&q->lock);
1144
1145 if (q->ahb_addr)
1146 iounmap(q->ahb_addr);
1147
1148 return 0;
1149}
1150
1151static int fsl_qspi_suspend(struct platform_device *pdev, pm_message_t state)
1152{
1153 return 0;
1154}
1155
1156static int fsl_qspi_resume(struct platform_device *pdev)
1157{
1158 int ret;
1159 struct fsl_qspi *q = platform_get_drvdata(pdev);
1160
1161 ret = fsl_qspi_clk_prep_enable(q);
1162 if (ret)
1163 return ret;
1164
1165 fsl_qspi_nor_setup(q);
1166 fsl_qspi_set_map_addr(q);
1167 fsl_qspi_nor_setup_last(q);
1168
1169 fsl_qspi_clk_disable_unprep(q);
1170
1171 return 0;
1172}
1173
1174static struct platform_driver fsl_qspi_driver = {
1175 .driver = {
1176 .name = "fsl-quadspi",
1177 .bus = &platform_bus_type,
1178 .of_match_table = fsl_qspi_dt_ids,
1179 },
1180 .probe = fsl_qspi_probe,
1181 .remove = fsl_qspi_remove,
1182 .suspend = fsl_qspi_suspend,
1183 .resume = fsl_qspi_resume,
1184};
1185module_platform_driver(fsl_qspi_driver);
1186
1187MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
1188MODULE_AUTHOR("Freescale Semiconductor Inc.");
1189MODULE_LICENSE("GPL v2");
1190