mtd: devices: Provide header for shared OPCODEs and SFDP commands
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / devices / st_spi_fsm.c
1 /*
2  * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
3  *
4  * Author: Angus Clark <angus.clark@st.com>
5  *
6  * Copyright (C) 2010-2014 STicroelectronics Limited
7  *
8  * JEDEC probe based on drivers/mtd/devices/m25p80.c
9  *
10  * This code is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  *
14  */
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/platform_device.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/sched.h>
20 #include <linux/delay.h>
21 #include <linux/io.h>
22 #include <linux/of.h>
23
24 #include "serial_flash_cmds.h"
25
26 /*
27  * FSM SPI Controller Registers
28  */
29 #define SPI_CLOCKDIV                    0x0010
30 #define SPI_MODESELECT                  0x0018
31 #define SPI_CONFIGDATA                  0x0020
32 #define SPI_STA_MODE_CHANGE             0x0028
33 #define SPI_FAST_SEQ_TRANSFER_SIZE      0x0100
34 #define SPI_FAST_SEQ_ADD1               0x0104
35 #define SPI_FAST_SEQ_ADD2               0x0108
36 #define SPI_FAST_SEQ_ADD_CFG            0x010c
37 #define SPI_FAST_SEQ_OPC1               0x0110
38 #define SPI_FAST_SEQ_OPC2               0x0114
39 #define SPI_FAST_SEQ_OPC3               0x0118
40 #define SPI_FAST_SEQ_OPC4               0x011c
41 #define SPI_FAST_SEQ_OPC5               0x0120
42 #define SPI_MODE_BITS                   0x0124
43 #define SPI_DUMMY_BITS                  0x0128
44 #define SPI_FAST_SEQ_FLASH_STA_DATA     0x012c
45 #define SPI_FAST_SEQ_1                  0x0130
46 #define SPI_FAST_SEQ_2                  0x0134
47 #define SPI_FAST_SEQ_3                  0x0138
48 #define SPI_FAST_SEQ_4                  0x013c
49 #define SPI_FAST_SEQ_CFG                0x0140
50 #define SPI_FAST_SEQ_STA                0x0144
51 #define SPI_QUAD_BOOT_SEQ_INIT_1        0x0148
52 #define SPI_QUAD_BOOT_SEQ_INIT_2        0x014c
53 #define SPI_QUAD_BOOT_READ_SEQ_1        0x0150
54 #define SPI_QUAD_BOOT_READ_SEQ_2        0x0154
55 #define SPI_PROGRAM_ERASE_TIME          0x0158
56 #define SPI_MULT_PAGE_REPEAT_SEQ_1      0x015c
57 #define SPI_MULT_PAGE_REPEAT_SEQ_2      0x0160
58 #define SPI_STATUS_WR_TIME_REG          0x0164
59 #define SPI_FAST_SEQ_DATA_REG           0x0300
60
61 /*
62  * Register: SPI_MODESELECT
63  */
64 #define SPI_MODESELECT_CONTIG           0x01
65 #define SPI_MODESELECT_FASTREAD         0x02
66 #define SPI_MODESELECT_DUALIO           0x04
67 #define SPI_MODESELECT_FSM              0x08
68 #define SPI_MODESELECT_QUADBOOT         0x10
69
70 /*
71  * Register: SPI_CONFIGDATA
72  */
73 #define SPI_CFG_DEVICE_ST               0x1
74 #define SPI_CFG_DEVICE_ATMEL            0x4
75 #define SPI_CFG_MIN_CS_HIGH(x)          (((x) & 0xfff) << 4)
76 #define SPI_CFG_CS_SETUPHOLD(x)         (((x) & 0xff) << 16)
77 #define SPI_CFG_DATA_HOLD(x)            (((x) & 0xff) << 24)
78
79 #define SPI_CFG_DEFAULT_MIN_CS_HIGH    SPI_CFG_MIN_CS_HIGH(0x0AA)
80 #define SPI_CFG_DEFAULT_CS_SETUPHOLD   SPI_CFG_CS_SETUPHOLD(0xA0)
81 #define SPI_CFG_DEFAULT_DATA_HOLD      SPI_CFG_DATA_HOLD(0x00)
82
83 /*
84  * Register: SPI_FAST_SEQ_TRANSFER_SIZE
85  */
86 #define TRANSFER_SIZE(x)                ((x) * 8)
87
88 /*
89  * Register: SPI_FAST_SEQ_ADD_CFG
90  */
91 #define ADR_CFG_CYCLES_ADD1(x)          ((x) << 0)
92 #define ADR_CFG_PADS_1_ADD1             (0x0 << 6)
93 #define ADR_CFG_PADS_2_ADD1             (0x1 << 6)
94 #define ADR_CFG_PADS_4_ADD1             (0x3 << 6)
95 #define ADR_CFG_CSDEASSERT_ADD1         (1   << 8)
96 #define ADR_CFG_CYCLES_ADD2(x)          ((x) << (0+16))
97 #define ADR_CFG_PADS_1_ADD2             (0x0 << (6+16))
98 #define ADR_CFG_PADS_2_ADD2             (0x1 << (6+16))
99 #define ADR_CFG_PADS_4_ADD2             (0x3 << (6+16))
100 #define ADR_CFG_CSDEASSERT_ADD2         (1   << (8+16))
101
102 /*
103  * Register: SPI_FAST_SEQ_n
104  */
105 #define SEQ_OPC_OPCODE(x)               ((x) << 0)
106 #define SEQ_OPC_CYCLES(x)               ((x) << 8)
107 #define SEQ_OPC_PADS_1                  (0x0 << 14)
108 #define SEQ_OPC_PADS_2                  (0x1 << 14)
109 #define SEQ_OPC_PADS_4                  (0x3 << 14)
110 #define SEQ_OPC_CSDEASSERT              (1   << 16)
111
112 /*
113  * Register: SPI_FAST_SEQ_CFG
114  */
115 #define SEQ_CFG_STARTSEQ                (1 << 0)
116 #define SEQ_CFG_SWRESET                 (1 << 5)
117 #define SEQ_CFG_CSDEASSERT              (1 << 6)
118 #define SEQ_CFG_READNOTWRITE            (1 << 7)
119 #define SEQ_CFG_ERASE                   (1 << 8)
120 #define SEQ_CFG_PADS_1                  (0x0 << 16)
121 #define SEQ_CFG_PADS_2                  (0x1 << 16)
122 #define SEQ_CFG_PADS_4                  (0x3 << 16)
123
124 /*
125  * Register: SPI_MODE_BITS
126  */
127 #define MODE_DATA(x)                    (x & 0xff)
128 #define MODE_CYCLES(x)                  ((x & 0x3f) << 16)
129 #define MODE_PADS_1                     (0x0 << 22)
130 #define MODE_PADS_2                     (0x1 << 22)
131 #define MODE_PADS_4                     (0x3 << 22)
132 #define DUMMY_CSDEASSERT                (1   << 24)
133
134 /*
135  * Register: SPI_DUMMY_BITS
136  */
137 #define DUMMY_CYCLES(x)                 ((x & 0x3f) << 16)
138 #define DUMMY_PADS_1                    (0x0 << 22)
139 #define DUMMY_PADS_2                    (0x1 << 22)
140 #define DUMMY_PADS_4                    (0x3 << 22)
141 #define DUMMY_CSDEASSERT                (1   << 24)
142
143 /*
144  * Register: SPI_FAST_SEQ_FLASH_STA_DATA
145  */
146 #define STA_DATA_BYTE1(x)               ((x & 0xff) << 0)
147 #define STA_DATA_BYTE2(x)               ((x & 0xff) << 8)
148 #define STA_PADS_1                      (0x0 << 16)
149 #define STA_PADS_2                      (0x1 << 16)
150 #define STA_PADS_4                      (0x3 << 16)
151 #define STA_CSDEASSERT                  (0x1 << 20)
152 #define STA_RDNOTWR                     (0x1 << 21)
153
154 /*
155  * FSM SPI Instruction Opcodes
156  */
157 #define STFSM_OPC_CMD                   0x1
158 #define STFSM_OPC_ADD                   0x2
159 #define STFSM_OPC_STA                   0x3
160 #define STFSM_OPC_MODE                  0x4
161 #define STFSM_OPC_DUMMY         0x5
162 #define STFSM_OPC_DATA                  0x6
163 #define STFSM_OPC_WAIT                  0x7
164 #define STFSM_OPC_JUMP                  0x8
165 #define STFSM_OPC_GOTO                  0x9
166 #define STFSM_OPC_STOP                  0xF
167
168 /*
169  * FSM SPI Instructions (== opcode + operand).
170  */
171 #define STFSM_INSTR(cmd, op)            ((cmd) | ((op) << 4))
172
173 #define STFSM_INST_CMD1                 STFSM_INSTR(STFSM_OPC_CMD,      1)
174 #define STFSM_INST_CMD2                 STFSM_INSTR(STFSM_OPC_CMD,      2)
175 #define STFSM_INST_CMD3                 STFSM_INSTR(STFSM_OPC_CMD,      3)
176 #define STFSM_INST_CMD4                 STFSM_INSTR(STFSM_OPC_CMD,      4)
177 #define STFSM_INST_CMD5                 STFSM_INSTR(STFSM_OPC_CMD,      5)
178 #define STFSM_INST_ADD1                 STFSM_INSTR(STFSM_OPC_ADD,      1)
179 #define STFSM_INST_ADD2                 STFSM_INSTR(STFSM_OPC_ADD,      2)
180
181 #define STFSM_INST_DATA_WRITE           STFSM_INSTR(STFSM_OPC_DATA,     1)
182 #define STFSM_INST_DATA_READ            STFSM_INSTR(STFSM_OPC_DATA,     2)
183
184 #define STFSM_INST_STA_RD1              STFSM_INSTR(STFSM_OPC_STA,      0x1)
185 #define STFSM_INST_STA_WR1              STFSM_INSTR(STFSM_OPC_STA,      0x1)
186 #define STFSM_INST_STA_RD2              STFSM_INSTR(STFSM_OPC_STA,      0x2)
187 #define STFSM_INST_STA_WR1_2            STFSM_INSTR(STFSM_OPC_STA,      0x3)
188
189 #define STFSM_INST_MODE                 STFSM_INSTR(STFSM_OPC_MODE,     0)
190 #define STFSM_INST_DUMMY                STFSM_INSTR(STFSM_OPC_DUMMY,    0)
191 #define STFSM_INST_WAIT                 STFSM_INSTR(STFSM_OPC_WAIT,     0)
192 #define STFSM_INST_STOP                 STFSM_INSTR(STFSM_OPC_STOP,     0)
193
194 #define STFSM_DEFAULT_EMI_FREQ 100000000UL                        /* 100 MHz */
195 #define STFSM_DEFAULT_WR_TIME  (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
196
197 #define STFSM_FLASH_SAFE_FREQ  10000000UL                         /* 10 MHz */
198
199 #define STFSM_MAX_WAIT_SEQ_MS  1000     /* FSM execution time */
200
201 struct stfsm {
202         struct device           *dev;
203         void __iomem            *base;
204         struct resource         *region;
205         struct mtd_info         mtd;
206         struct mutex            lock;
207
208         uint32_t                fifo_dir_delay;
209 };
210
211 struct stfsm_seq {
212         uint32_t data_size;
213         uint32_t addr1;
214         uint32_t addr2;
215         uint32_t addr_cfg;
216         uint32_t seq_opc[5];
217         uint32_t mode;
218         uint32_t dummy;
219         uint32_t status;
220         uint8_t  seq[16];
221         uint32_t seq_cfg;
222 } __packed __aligned(4);
223
224 static struct stfsm_seq stfsm_seq_read_jedec = {
225         .data_size = TRANSFER_SIZE(8),
226         .seq_opc[0] = (SEQ_OPC_PADS_1 |
227                        SEQ_OPC_CYCLES(8) |
228                        SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
229         .seq = {
230                 STFSM_INST_CMD1,
231                 STFSM_INST_DATA_READ,
232                 STFSM_INST_STOP,
233         },
234         .seq_cfg = (SEQ_CFG_PADS_1 |
235                     SEQ_CFG_READNOTWRITE |
236                     SEQ_CFG_CSDEASSERT |
237                     SEQ_CFG_STARTSEQ),
238 };
239
240 static inline int stfsm_is_idle(struct stfsm *fsm)
241 {
242         return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
243 }
244
245 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
246 {
247         return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
248 }
249
250 static void stfsm_clear_fifo(struct stfsm *fsm)
251 {
252         uint32_t avail;
253
254         for (;;) {
255                 avail = stfsm_fifo_available(fsm);
256                 if (!avail)
257                         break;
258
259                 while (avail) {
260                         readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
261                         avail--;
262                 }
263         }
264 }
265
266 static inline void stfsm_load_seq(struct stfsm *fsm,
267                                   const struct stfsm_seq *seq)
268 {
269         void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
270         const uint32_t *src = (const uint32_t *)seq;
271         int words = sizeof(*seq) / sizeof(*src);
272
273         BUG_ON(!stfsm_is_idle(fsm));
274
275         while (words--) {
276                 writel(*src, dst);
277                 src++;
278                 dst += 4;
279         }
280 }
281
282 static void stfsm_wait_seq(struct stfsm *fsm)
283 {
284         unsigned long deadline;
285         int timeout = 0;
286
287         deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
288
289         while (!timeout) {
290                 if (time_after_eq(jiffies, deadline))
291                         timeout = 1;
292
293                 if (stfsm_is_idle(fsm))
294                         return;
295
296                 cond_resched();
297         }
298
299         dev_err(fsm->dev, "timeout on sequence completion\n");
300 }
301
302 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
303                             const uint32_t size)
304 {
305         uint32_t remaining = size >> 2;
306         uint32_t avail;
307         uint32_t words;
308
309         dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
310
311         BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
312
313         while (remaining) {
314                 for (;;) {
315                         avail = stfsm_fifo_available(fsm);
316                         if (avail)
317                                 break;
318                         udelay(1);
319                 }
320                 words = min(avail, remaining);
321                 remaining -= words;
322
323                 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
324                 buf += words;
325         }
326 }
327
328 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
329 {
330         const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
331         uint32_t tmp[2];
332
333         stfsm_load_seq(fsm, seq);
334
335         stfsm_read_fifo(fsm, tmp, 8);
336
337         memcpy(jedec, tmp, 5);
338
339         stfsm_wait_seq(fsm);
340 }
341
342 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
343 {
344         u16                     ext_jedec;
345         u32                     jedec;
346         u8                      id[5];
347
348         stfsm_read_jedec(fsm, id);
349
350         jedec     = id[0] << 16 | id[1] << 8 | id[2];
351         /*
352          * JEDEC also defines an optional "extended device information"
353          * string for after vendor-specific data, after the three bytes
354          * we use here. Supporting some chips might require using it.
355          */
356         ext_jedec = id[3] << 8  | id[4];
357
358         dev_dbg(fsm->dev, "JEDEC =  0x%08x [%02x %02x %02x %02x %02x]\n",
359                 jedec, id[0], id[1], id[2], id[3], id[4]);
360
361         return NULL;
362 }
363
364 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
365 {
366         int ret, timeout = 10;
367
368         /* Wait for controller to accept mode change */
369         while (--timeout) {
370                 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
371                 if (ret & 0x1)
372                         break;
373                 udelay(1);
374         }
375
376         if (!timeout)
377                 return -EBUSY;
378
379         writel(mode, fsm->base + SPI_MODESELECT);
380
381         return 0;
382 }
383
384 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
385 {
386         uint32_t emi_freq;
387         uint32_t clk_div;
388
389         /* TODO: Make this dynamic */
390         emi_freq = STFSM_DEFAULT_EMI_FREQ;
391
392         /*
393          * Calculate clk_div - values between 2 and 128
394          * Multiple of 2, rounded up
395          */
396         clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
397         if (clk_div < 2)
398                 clk_div = 2;
399         else if (clk_div > 128)
400                 clk_div = 128;
401
402         /*
403          * Determine a suitable delay for the IP to complete a change of
404          * direction of the FIFO. The required delay is related to the clock
405          * divider used. The following heuristics are based on empirical tests,
406          * using a 100MHz EMI clock.
407          */
408         if (clk_div <= 4)
409                 fsm->fifo_dir_delay = 0;
410         else if (clk_div <= 10)
411                 fsm->fifo_dir_delay = 1;
412         else
413                 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
414
415         dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
416                 emi_freq, spi_freq, clk_div);
417
418         writel(clk_div, fsm->base + SPI_CLOCKDIV);
419 }
420
421 static int stfsm_init(struct stfsm *fsm)
422 {
423         int ret;
424
425         /* Perform a soft reset of the FSM controller */
426         writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
427         udelay(1);
428         writel(0, fsm->base + SPI_FAST_SEQ_CFG);
429
430         /* Set clock to 'safe' frequency initially */
431         stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
432
433         /* Switch to FSM */
434         ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
435         if (ret)
436                 return ret;
437
438         /* Set timing parameters */
439         writel(SPI_CFG_DEVICE_ST            |
440                SPI_CFG_DEFAULT_MIN_CS_HIGH  |
441                SPI_CFG_DEFAULT_CS_SETUPHOLD |
442                SPI_CFG_DEFAULT_DATA_HOLD,
443                fsm->base + SPI_CONFIGDATA);
444         writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
445
446         /* Clear FIFO, just in case */
447         stfsm_clear_fifo(fsm);
448
449         return 0;
450 }
451
452 static int stfsm_probe(struct platform_device *pdev)
453 {
454         struct device_node *np = pdev->dev.of_node;
455         struct resource *res;
456         struct stfsm *fsm;
457         int ret;
458
459         if (!np) {
460                 dev_err(&pdev->dev, "No DT found\n");
461                 return -EINVAL;
462         }
463
464         fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
465         if (!fsm)
466                 return -ENOMEM;
467
468         fsm->dev = &pdev->dev;
469
470         platform_set_drvdata(pdev, fsm);
471
472         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
473         if (!res) {
474                 dev_err(&pdev->dev, "Resource not found\n");
475                 return -ENODEV;
476         }
477
478         fsm->base = devm_ioremap_resource(&pdev->dev, res);
479         if (IS_ERR(fsm->base)) {
480                 dev_err(&pdev->dev,
481                         "Failed to reserve memory region %pR\n", res);
482                 return PTR_ERR(fsm->base);
483         }
484
485         mutex_init(&fsm->lock);
486
487         ret = stfsm_init(fsm);
488         if (ret) {
489                 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
490                 return ret;
491         }
492
493         /* Detect SPI FLASH device */
494         stfsm_jedec_probe(fsm);
495
496         fsm->mtd.dev.parent     = &pdev->dev;
497         fsm->mtd.type           = MTD_NORFLASH;
498         fsm->mtd.writesize      = 4;
499         fsm->mtd.writebufsize   = fsm->mtd.writesize;
500         fsm->mtd.flags          = MTD_CAP_NORFLASH;
501
502         return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
503 }
504
505 static int stfsm_remove(struct platform_device *pdev)
506 {
507         struct stfsm *fsm = platform_get_drvdata(pdev);
508         int err;
509
510         err = mtd_device_unregister(&fsm->mtd);
511         if (err)
512                 return err;
513
514         return 0;
515 }
516
517 static struct of_device_id stfsm_match[] = {
518         { .compatible = "st,spi-fsm", },
519         {},
520 };
521 MODULE_DEVICE_TABLE(of, stfsm_match);
522
523 static struct platform_driver stfsm_driver = {
524         .probe          = stfsm_probe,
525         .remove         = stfsm_remove,
526         .driver         = {
527                 .name   = "st-spi-fsm",
528                 .owner  = THIS_MODULE,
529                 .of_match_table = stfsm_match,
530         },
531 };
532 module_platform_driver(stfsm_driver);
533
534 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
535 MODULE_DESCRIPTION("ST SPI FSM driver");
536 MODULE_LICENSE("GPL");