mtd: st_spi_fsm: Add Spansion S25FL032P to device table
[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 STMicroelectronics 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/regmap.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/sched.h>
23 #include <linux/delay.h>
24 #include <linux/io.h>
25 #include <linux/of.h>
26
27 #include "serial_flash_cmds.h"
28
29 /*
30  * FSM SPI Controller Registers
31  */
32 #define SPI_CLOCKDIV                    0x0010
33 #define SPI_MODESELECT                  0x0018
34 #define SPI_CONFIGDATA                  0x0020
35 #define SPI_STA_MODE_CHANGE             0x0028
36 #define SPI_FAST_SEQ_TRANSFER_SIZE      0x0100
37 #define SPI_FAST_SEQ_ADD1               0x0104
38 #define SPI_FAST_SEQ_ADD2               0x0108
39 #define SPI_FAST_SEQ_ADD_CFG            0x010c
40 #define SPI_FAST_SEQ_OPC1               0x0110
41 #define SPI_FAST_SEQ_OPC2               0x0114
42 #define SPI_FAST_SEQ_OPC3               0x0118
43 #define SPI_FAST_SEQ_OPC4               0x011c
44 #define SPI_FAST_SEQ_OPC5               0x0120
45 #define SPI_MODE_BITS                   0x0124
46 #define SPI_DUMMY_BITS                  0x0128
47 #define SPI_FAST_SEQ_FLASH_STA_DATA     0x012c
48 #define SPI_FAST_SEQ_1                  0x0130
49 #define SPI_FAST_SEQ_2                  0x0134
50 #define SPI_FAST_SEQ_3                  0x0138
51 #define SPI_FAST_SEQ_4                  0x013c
52 #define SPI_FAST_SEQ_CFG                0x0140
53 #define SPI_FAST_SEQ_STA                0x0144
54 #define SPI_QUAD_BOOT_SEQ_INIT_1        0x0148
55 #define SPI_QUAD_BOOT_SEQ_INIT_2        0x014c
56 #define SPI_QUAD_BOOT_READ_SEQ_1        0x0150
57 #define SPI_QUAD_BOOT_READ_SEQ_2        0x0154
58 #define SPI_PROGRAM_ERASE_TIME          0x0158
59 #define SPI_MULT_PAGE_REPEAT_SEQ_1      0x015c
60 #define SPI_MULT_PAGE_REPEAT_SEQ_2      0x0160
61 #define SPI_STATUS_WR_TIME_REG          0x0164
62 #define SPI_FAST_SEQ_DATA_REG           0x0300
63
64 /*
65  * Register: SPI_MODESELECT
66  */
67 #define SPI_MODESELECT_CONTIG           0x01
68 #define SPI_MODESELECT_FASTREAD         0x02
69 #define SPI_MODESELECT_DUALIO           0x04
70 #define SPI_MODESELECT_FSM              0x08
71 #define SPI_MODESELECT_QUADBOOT         0x10
72
73 /*
74  * Register: SPI_CONFIGDATA
75  */
76 #define SPI_CFG_DEVICE_ST               0x1
77 #define SPI_CFG_DEVICE_ATMEL            0x4
78 #define SPI_CFG_MIN_CS_HIGH(x)          (((x) & 0xfff) << 4)
79 #define SPI_CFG_CS_SETUPHOLD(x)         (((x) & 0xff) << 16)
80 #define SPI_CFG_DATA_HOLD(x)            (((x) & 0xff) << 24)
81
82 #define SPI_CFG_DEFAULT_MIN_CS_HIGH    SPI_CFG_MIN_CS_HIGH(0x0AA)
83 #define SPI_CFG_DEFAULT_CS_SETUPHOLD   SPI_CFG_CS_SETUPHOLD(0xA0)
84 #define SPI_CFG_DEFAULT_DATA_HOLD      SPI_CFG_DATA_HOLD(0x00)
85
86 /*
87  * Register: SPI_FAST_SEQ_TRANSFER_SIZE
88  */
89 #define TRANSFER_SIZE(x)                ((x) * 8)
90
91 /*
92  * Register: SPI_FAST_SEQ_ADD_CFG
93  */
94 #define ADR_CFG_CYCLES_ADD1(x)          ((x) << 0)
95 #define ADR_CFG_PADS_1_ADD1             (0x0 << 6)
96 #define ADR_CFG_PADS_2_ADD1             (0x1 << 6)
97 #define ADR_CFG_PADS_4_ADD1             (0x3 << 6)
98 #define ADR_CFG_CSDEASSERT_ADD1         (1   << 8)
99 #define ADR_CFG_CYCLES_ADD2(x)          ((x) << (0+16))
100 #define ADR_CFG_PADS_1_ADD2             (0x0 << (6+16))
101 #define ADR_CFG_PADS_2_ADD2             (0x1 << (6+16))
102 #define ADR_CFG_PADS_4_ADD2             (0x3 << (6+16))
103 #define ADR_CFG_CSDEASSERT_ADD2         (1   << (8+16))
104
105 /*
106  * Register: SPI_FAST_SEQ_n
107  */
108 #define SEQ_OPC_OPCODE(x)               ((x) << 0)
109 #define SEQ_OPC_CYCLES(x)               ((x) << 8)
110 #define SEQ_OPC_PADS_1                  (0x0 << 14)
111 #define SEQ_OPC_PADS_2                  (0x1 << 14)
112 #define SEQ_OPC_PADS_4                  (0x3 << 14)
113 #define SEQ_OPC_CSDEASSERT              (1   << 16)
114
115 /*
116  * Register: SPI_FAST_SEQ_CFG
117  */
118 #define SEQ_CFG_STARTSEQ                (1 << 0)
119 #define SEQ_CFG_SWRESET                 (1 << 5)
120 #define SEQ_CFG_CSDEASSERT              (1 << 6)
121 #define SEQ_CFG_READNOTWRITE            (1 << 7)
122 #define SEQ_CFG_ERASE                   (1 << 8)
123 #define SEQ_CFG_PADS_1                  (0x0 << 16)
124 #define SEQ_CFG_PADS_2                  (0x1 << 16)
125 #define SEQ_CFG_PADS_4                  (0x3 << 16)
126
127 /*
128  * Register: SPI_MODE_BITS
129  */
130 #define MODE_DATA(x)                    (x & 0xff)
131 #define MODE_CYCLES(x)                  ((x & 0x3f) << 16)
132 #define MODE_PADS_1                     (0x0 << 22)
133 #define MODE_PADS_2                     (0x1 << 22)
134 #define MODE_PADS_4                     (0x3 << 22)
135 #define DUMMY_CSDEASSERT                (1   << 24)
136
137 /*
138  * Register: SPI_DUMMY_BITS
139  */
140 #define DUMMY_CYCLES(x)                 ((x & 0x3f) << 16)
141 #define DUMMY_PADS_1                    (0x0 << 22)
142 #define DUMMY_PADS_2                    (0x1 << 22)
143 #define DUMMY_PADS_4                    (0x3 << 22)
144 #define DUMMY_CSDEASSERT                (1   << 24)
145
146 /*
147  * Register: SPI_FAST_SEQ_FLASH_STA_DATA
148  */
149 #define STA_DATA_BYTE1(x)               ((x & 0xff) << 0)
150 #define STA_DATA_BYTE2(x)               ((x & 0xff) << 8)
151 #define STA_PADS_1                      (0x0 << 16)
152 #define STA_PADS_2                      (0x1 << 16)
153 #define STA_PADS_4                      (0x3 << 16)
154 #define STA_CSDEASSERT                  (0x1 << 20)
155 #define STA_RDNOTWR                     (0x1 << 21)
156
157 /*
158  * FSM SPI Instruction Opcodes
159  */
160 #define STFSM_OPC_CMD                   0x1
161 #define STFSM_OPC_ADD                   0x2
162 #define STFSM_OPC_STA                   0x3
163 #define STFSM_OPC_MODE                  0x4
164 #define STFSM_OPC_DUMMY         0x5
165 #define STFSM_OPC_DATA                  0x6
166 #define STFSM_OPC_WAIT                  0x7
167 #define STFSM_OPC_JUMP                  0x8
168 #define STFSM_OPC_GOTO                  0x9
169 #define STFSM_OPC_STOP                  0xF
170
171 /*
172  * FSM SPI Instructions (== opcode + operand).
173  */
174 #define STFSM_INSTR(cmd, op)            ((cmd) | ((op) << 4))
175
176 #define STFSM_INST_CMD1                 STFSM_INSTR(STFSM_OPC_CMD,      1)
177 #define STFSM_INST_CMD2                 STFSM_INSTR(STFSM_OPC_CMD,      2)
178 #define STFSM_INST_CMD3                 STFSM_INSTR(STFSM_OPC_CMD,      3)
179 #define STFSM_INST_CMD4                 STFSM_INSTR(STFSM_OPC_CMD,      4)
180 #define STFSM_INST_CMD5                 STFSM_INSTR(STFSM_OPC_CMD,      5)
181 #define STFSM_INST_ADD1                 STFSM_INSTR(STFSM_OPC_ADD,      1)
182 #define STFSM_INST_ADD2                 STFSM_INSTR(STFSM_OPC_ADD,      2)
183
184 #define STFSM_INST_DATA_WRITE           STFSM_INSTR(STFSM_OPC_DATA,     1)
185 #define STFSM_INST_DATA_READ            STFSM_INSTR(STFSM_OPC_DATA,     2)
186
187 #define STFSM_INST_STA_RD1              STFSM_INSTR(STFSM_OPC_STA,      0x1)
188 #define STFSM_INST_STA_WR1              STFSM_INSTR(STFSM_OPC_STA,      0x1)
189 #define STFSM_INST_STA_RD2              STFSM_INSTR(STFSM_OPC_STA,      0x2)
190 #define STFSM_INST_STA_WR1_2            STFSM_INSTR(STFSM_OPC_STA,      0x3)
191
192 #define STFSM_INST_MODE                 STFSM_INSTR(STFSM_OPC_MODE,     0)
193 #define STFSM_INST_DUMMY                STFSM_INSTR(STFSM_OPC_DUMMY,    0)
194 #define STFSM_INST_WAIT                 STFSM_INSTR(STFSM_OPC_WAIT,     0)
195 #define STFSM_INST_STOP                 STFSM_INSTR(STFSM_OPC_STOP,     0)
196
197 #define STFSM_DEFAULT_EMI_FREQ 100000000UL                        /* 100 MHz */
198 #define STFSM_DEFAULT_WR_TIME  (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
199
200 #define STFSM_FLASH_SAFE_FREQ  10000000UL                         /* 10 MHz */
201
202 #define STFSM_MAX_WAIT_SEQ_MS  1000     /* FSM execution time */
203
204 /* Flash Commands */
205 #define FLASH_CMD_WREN         0x06
206 #define FLASH_CMD_WRDI         0x04
207 #define FLASH_CMD_RDID         0x9f
208 #define FLASH_CMD_RDSR         0x05
209 #define FLASH_CMD_RDSR2                0x35
210 #define FLASH_CMD_WRSR         0x01
211 #define FLASH_CMD_SE_4K                0x20
212 #define FLASH_CMD_SE_32K       0x52
213 #define FLASH_CMD_SE           0xd8
214 #define FLASH_CMD_CHIPERASE    0xc7
215
216 #define FLASH_CMD_READ         0x03    /* READ */
217 #define FLASH_CMD_READ_FAST    0x0b    /* FAST READ */
218 #define FLASH_CMD_READ_1_1_2   0x3b    /* DUAL OUTPUT READ */
219 #define FLASH_CMD_READ_1_2_2   0xbb    /* DUAL I/O READ */
220 #define FLASH_CMD_READ_1_1_4   0x6b    /* QUAD OUTPUT READ */
221 #define FLASH_CMD_READ_1_4_4   0xeb    /* QUAD I/O READ */
222
223 #define FLASH_CMD_WRITE                0x02    /* PAGE PROGRAM */
224 #define FLASH_CMD_WRITE_1_1_2  0xa2    /* DUAL INPUT PROGRAM */
225 #define FLASH_CMD_WRITE_1_2_2  0xd2    /* DUAL INPUT EXT PROGRAM */
226 #define FLASH_CMD_WRITE_1_1_4  0x32    /* QUAD INPUT PROGRAM */
227 #define FLASH_CMD_WRITE_1_4_4  0x12    /* QUAD INPUT EXT PROGRAM */
228
229 #define FLASH_CMD_EN4B_ADDR    0xb7    /* Enter 4-byte address mode */
230 #define FLASH_CMD_EX4B_ADDR    0xe9    /* Exit 4-byte address mode */
231
232 /* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
233 #define FLASH_CMD_READ4                0x13
234 #define FLASH_CMD_READ4_FAST   0x0c
235 #define FLASH_CMD_READ4_1_1_2  0x3c
236 #define FLASH_CMD_READ4_1_2_2  0xbc
237 #define FLASH_CMD_READ4_1_1_4  0x6c
238 #define FLASH_CMD_READ4_1_4_4  0xec
239
240 /* S25FLxxxS commands */
241 #define S25FL_CMD_WRITE4_1_1_4 0x34
242 #define S25FL_CMD_SE4          0xdc
243 #define S25FL_CMD_CLSR         0x30
244 #define S25FL_CMD_DYBWR                0xe1
245 #define S25FL_CMD_DYBRD                0xe0
246 #define S25FL_CMD_WRITE4       0x12    /* Note, opcode clashes with
247                                         * 'FLASH_CMD_WRITE_1_4_4'
248                                         * as found on N25Qxxx devices! */
249
250 /* Status register */
251 #define FLASH_STATUS_BUSY      0x01
252 #define FLASH_STATUS_WEL       0x02
253 #define FLASH_STATUS_BP0       0x04
254 #define FLASH_STATUS_BP1       0x08
255 #define FLASH_STATUS_BP2       0x10
256 #define FLASH_STATUS_SRWP0     0x80
257 #define FLASH_STATUS_TIMEOUT   0xff
258 /* S25FL Error Flags */
259 #define S25FL_STATUS_E_ERR     0x20
260 #define S25FL_STATUS_P_ERR     0x40
261
262 #define N25Q_CMD_WRVCR         0x81
263 #define N25Q_CMD_RDVCR         0x85
264 #define N25Q_CMD_RDVECR        0x65
265 #define N25Q_CMD_RDNVCR        0xb5
266 #define N25Q_CMD_WRNVCR        0xb1
267
268 #define FLASH_PAGESIZE         256                      /* In Bytes    */
269 #define FLASH_PAGESIZE_32      (FLASH_PAGESIZE / 4)     /* In uint32_t */
270 #define FLASH_MAX_BUSY_WAIT    (300 * HZ)       /* Maximum 'CHIPERASE' time */
271
272 /*
273  * Flags to tweak operation of default read/write/erase routines
274  */
275 #define CFG_READ_TOGGLE_32BIT_ADDR     0x00000001
276 #define CFG_WRITE_TOGGLE_32BIT_ADDR    0x00000002
277 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
278 #define CFG_S25FL_CHECK_ERROR_FLAGS    0x00000010
279
280 struct stfsm_seq {
281         uint32_t data_size;
282         uint32_t addr1;
283         uint32_t addr2;
284         uint32_t addr_cfg;
285         uint32_t seq_opc[5];
286         uint32_t mode;
287         uint32_t dummy;
288         uint32_t status;
289         uint8_t  seq[16];
290         uint32_t seq_cfg;
291 } __packed __aligned(4);
292
293 struct stfsm {
294         struct device           *dev;
295         void __iomem            *base;
296         struct resource         *region;
297         struct mtd_info         mtd;
298         struct mutex            lock;
299         struct flash_info       *info;
300
301         uint32_t                configuration;
302         uint32_t                fifo_dir_delay;
303         bool                    booted_from_spi;
304         bool                    reset_signal;
305         bool                    reset_por;
306
307         struct stfsm_seq stfsm_seq_read;
308         struct stfsm_seq stfsm_seq_write;
309         struct stfsm_seq stfsm_seq_en_32bit_addr;
310 };
311
312 /* Parameters to configure a READ or WRITE FSM sequence */
313 struct seq_rw_config {
314         uint32_t        flags;          /* flags to support config */
315         uint8_t         cmd;            /* FLASH command */
316         int             write;          /* Write Sequence */
317         uint8_t         addr_pads;      /* No. of addr pads (MODE & DUMMY) */
318         uint8_t         data_pads;      /* No. of data pads */
319         uint8_t         mode_data;      /* MODE data */
320         uint8_t         mode_cycles;    /* No. of MODE cycles */
321         uint8_t         dummy_cycles;   /* No. of DUMMY cycles */
322 };
323
324 /* SPI Flash Device Table */
325 struct flash_info {
326         char            *name;
327         /*
328          * JEDEC id zero means "no ID" (most older chips); otherwise it has
329          * a high byte of zero plus three data bytes: the manufacturer id,
330          * then a two byte device id.
331          */
332         u32             jedec_id;
333         u16             ext_id;
334         /*
335          * The size listed here is what works with FLASH_CMD_SE, which isn't
336          * necessarily called a "sector" by the vendor.
337          */
338         unsigned        sector_size;
339         u16             n_sectors;
340         u32             flags;
341         /*
342          * Note, where FAST_READ is supported, freq_max specifies the
343          * FAST_READ frequency, not the READ frequency.
344          */
345         u32             max_freq;
346         int             (*config)(struct stfsm *);
347 };
348
349 static int stfsm_n25q_config(struct stfsm *fsm);
350 static int stfsm_mx25_config(struct stfsm *fsm);
351 static int stfsm_s25fl_config(struct stfsm *fsm);
352 static int stfsm_w25q_config(struct stfsm *fsm);
353
354 static struct flash_info flash_types[] = {
355         /*
356          * ST Microelectronics/Numonyx --
357          * (newer production versions may have feature updates
358          * (eg faster operating frequency)
359          */
360 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
361         { "m25p40",  0x202013, 0,  64 * 1024,   8, M25P_FLAG, 25, NULL },
362         { "m25p80",  0x202014, 0,  64 * 1024,  16, M25P_FLAG, 25, NULL },
363         { "m25p16",  0x202015, 0,  64 * 1024,  32, M25P_FLAG, 25, NULL },
364         { "m25p32",  0x202016, 0,  64 * 1024,  64, M25P_FLAG, 50, NULL },
365         { "m25p64",  0x202017, 0,  64 * 1024, 128, M25P_FLAG, 50, NULL },
366         { "m25p128", 0x202018, 0, 256 * 1024,  64, M25P_FLAG, 50, NULL },
367
368 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE      |        \
369                     FLASH_FLAG_READ_FAST        |       \
370                     FLASH_FLAG_READ_1_1_2       |       \
371                     FLASH_FLAG_WRITE_1_1_2)
372         { "m25px32", 0x207116, 0,  64 * 1024,  64, M25PX_FLAG, 75, NULL },
373         { "m25px64", 0x207117, 0,  64 * 1024, 128, M25PX_FLAG, 75, NULL },
374
375 #define MX25_FLAG (FLASH_FLAG_READ_WRITE       |        \
376                    FLASH_FLAG_READ_FAST         |       \
377                    FLASH_FLAG_READ_1_1_2        |       \
378                    FLASH_FLAG_READ_1_2_2        |       \
379                    FLASH_FLAG_READ_1_1_4        |       \
380                    FLASH_FLAG_READ_1_4_4        |       \
381                    FLASH_FLAG_SE_4K             |       \
382                    FLASH_FLAG_SE_32K)
383         { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
384           (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
385           stfsm_mx25_config },
386         { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
387           (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
388           stfsm_mx25_config},
389
390 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE       |        \
391                    FLASH_FLAG_READ_FAST         |       \
392                    FLASH_FLAG_READ_1_1_2        |       \
393                    FLASH_FLAG_READ_1_2_2        |       \
394                    FLASH_FLAG_READ_1_1_4        |       \
395                    FLASH_FLAG_READ_1_4_4        |       \
396                    FLASH_FLAG_WRITE_1_1_2       |       \
397                    FLASH_FLAG_WRITE_1_2_2       |       \
398                    FLASH_FLAG_WRITE_1_1_4       |       \
399                    FLASH_FLAG_WRITE_1_4_4)
400         { "n25q128", 0x20ba18, 0, 64 * 1024,  256, N25Q_FLAG, 108,
401           stfsm_n25q_config },
402         { "n25q256", 0x20ba19, 0, 64 * 1024,  512,
403           N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
404
405         /*
406          * Spansion S25FLxxxP
407          *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
408          */
409 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE  |        \
410                         FLASH_FLAG_READ_1_1_2   |       \
411                         FLASH_FLAG_READ_1_2_2   |       \
412                         FLASH_FLAG_READ_1_1_4   |       \
413                         FLASH_FLAG_READ_1_4_4   |       \
414                         FLASH_FLAG_WRITE_1_1_4  |       \
415                         FLASH_FLAG_READ_FAST)
416         { "s25fl032p",  0x010215, 0x4d00,  64 * 1024,  64, S25FLXXXP_FLAG, 80,
417           stfsm_s25fl_config},
418         { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024,  64, S25FLXXXP_FLAG, 80,
419           stfsm_s25fl_config },
420         { "s25fl129p1", 0x012018, 0x4d01,  64 * 1024, 256, S25FLXXXP_FLAG, 80,
421           stfsm_s25fl_config },
422
423         /*
424          * Spansion S25FLxxxS
425          *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
426          *     - RESET# signal supported by die but not bristled out on all
427          *       package types.  The package type is a function of board design,
428          *       so this information is captured in the board's flags.
429          *     - Supports 'DYB' sector protection. Depending on variant, sectors
430          *       may default to locked state on power-on.
431          */
432 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG         |        \
433                         FLASH_FLAG_RESET        |       \
434                         FLASH_FLAG_DYB_LOCKING)
435         { "s25fl128s0", 0x012018, 0x0300,  256 * 1024, 64, S25FLXXXS_FLAG, 80,
436           stfsm_s25fl_config },
437         { "s25fl128s1", 0x012018, 0x0301,  64 * 1024, 256, S25FLXXXS_FLAG, 80,
438           stfsm_s25fl_config },
439         { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
440           S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
441         { "s25fl256s1", 0x010219, 0x4d01,  64 * 1024, 512,
442           S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
443
444         /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
445 #define W25X_FLAG (FLASH_FLAG_READ_WRITE       |        \
446                    FLASH_FLAG_READ_FAST         |       \
447                    FLASH_FLAG_READ_1_1_2        |       \
448                    FLASH_FLAG_WRITE_1_1_2)
449         { "w25x40",  0xef3013, 0,  64 * 1024,   8, W25X_FLAG, 75, NULL },
450         { "w25x80",  0xef3014, 0,  64 * 1024,  16, W25X_FLAG, 75, NULL },
451         { "w25x16",  0xef3015, 0,  64 * 1024,  32, W25X_FLAG, 75, NULL },
452         { "w25x32",  0xef3016, 0,  64 * 1024,  64, W25X_FLAG, 75, NULL },
453         { "w25x64",  0xef3017, 0,  64 * 1024, 128, W25X_FLAG, 75, NULL },
454
455         /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
456 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE       |        \
457                    FLASH_FLAG_READ_FAST         |       \
458                    FLASH_FLAG_READ_1_1_2        |       \
459                    FLASH_FLAG_READ_1_2_2        |       \
460                    FLASH_FLAG_READ_1_1_4        |       \
461                    FLASH_FLAG_READ_1_4_4        |       \
462                    FLASH_FLAG_WRITE_1_1_4)
463         { "w25q80",  0xef4014, 0,  64 * 1024,  16, W25Q_FLAG, 80,
464           stfsm_w25q_config },
465         { "w25q16",  0xef4015, 0,  64 * 1024,  32, W25Q_FLAG, 80,
466           stfsm_w25q_config },
467         { "w25q32",  0xef4016, 0,  64 * 1024,  64, W25Q_FLAG, 80,
468           stfsm_w25q_config },
469         { "w25q64",  0xef4017, 0,  64 * 1024, 128, W25Q_FLAG, 80,
470           stfsm_w25q_config },
471
472         /* Sentinel */
473         { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
474 };
475
476 /*
477  * FSM message sequence configurations:
478  *
479  * All configs are presented in order of preference
480  */
481
482 /* Default READ configurations, in order of preference */
483 static struct seq_rw_config default_read_configs[] = {
484         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4,   0, 4, 4, 0x00, 2, 4},
485         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4,   0, 1, 4, 0x00, 4, 0},
486         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2,   0, 2, 2, 0x00, 4, 0},
487         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2,   0, 1, 2, 0x00, 0, 8},
488         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ_FAST,    0, 1, 1, 0x00, 0, 8},
489         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ,         0, 1, 1, 0x00, 0, 0},
490         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
491 };
492
493 /* Default WRITE configurations */
494 static struct seq_rw_config default_write_configs[] = {
495         {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
496         {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
497         {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
498         {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
499         {FLASH_FLAG_READ_WRITE,  FLASH_CMD_WRITE,       1, 1, 1, 0x00, 0, 0},
500         {0x00,                   0,                     0, 0, 0, 0x00, 0, 0},
501 };
502
503 /*
504  * [N25Qxxx] Configuration
505  */
506 #define N25Q_VCR_DUMMY_CYCLES(x)        (((x) & 0xf) << 4)
507 #define N25Q_VCR_XIP_DISABLED           ((uint8_t)0x1 << 3)
508 #define N25Q_VCR_WRAP_CONT              0x3
509
510 /* N25Q 3-byte Address READ configurations
511  *      - 'FAST' variants configured for 8 dummy cycles.
512  *
513  * Note, the number of dummy cycles used for 'FAST' READ operations is
514  * configurable and would normally be tuned according to the READ command and
515  * operating frequency.  However, this applies universally to all 'FAST' READ
516  * commands, including those used by the SPIBoot controller, and remains in
517  * force until the device is power-cycled.  Since the SPIBoot controller is
518  * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
519  * cycles.
520  */
521 static struct seq_rw_config n25q_read3_configs[] = {
522         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4,   0, 4, 4, 0x00, 0, 8},
523         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4,   0, 1, 4, 0x00, 0, 8},
524         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2,   0, 2, 2, 0x00, 0, 8},
525         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2,   0, 1, 2, 0x00, 0, 8},
526         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ_FAST,    0, 1, 1, 0x00, 0, 8},
527         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ,         0, 1, 1, 0x00, 0, 0},
528         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
529 };
530
531 /* N25Q 4-byte Address READ configurations
532  *      - use special 4-byte address READ commands (reduces overheads, and
533  *        reduces risk of hitting watchdog reset issues).
534  *      - 'FAST' variants configured for 8 dummy cycles (see note above.)
535  */
536 static struct seq_rw_config n25q_read4_configs[] = {
537         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4,  0, 4, 4, 0x00, 0, 8},
538         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4,  0, 1, 4, 0x00, 0, 8},
539         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2,  0, 2, 2, 0x00, 0, 8},
540         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2,  0, 1, 2, 0x00, 0, 8},
541         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ4_FAST,   0, 1, 1, 0x00, 0, 8},
542         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4,        0, 1, 1, 0x00, 0, 0},
543         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
544 };
545
546 /*
547  * [MX25xxx] Configuration
548  */
549 #define MX25_STATUS_QE                  (0x1 << 6)
550
551 static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
552 {
553         seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
554                            SEQ_OPC_CYCLES(8) |
555                            SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR) |
556                            SEQ_OPC_CSDEASSERT);
557
558         seq->seq[0] = STFSM_INST_CMD1;
559         seq->seq[1] = STFSM_INST_WAIT;
560         seq->seq[2] = STFSM_INST_STOP;
561
562         seq->seq_cfg = (SEQ_CFG_PADS_1 |
563                         SEQ_CFG_ERASE |
564                         SEQ_CFG_READNOTWRITE |
565                         SEQ_CFG_CSDEASSERT |
566                         SEQ_CFG_STARTSEQ);
567
568         return 0;
569 }
570
571 /*
572  * [S25FLxxx] Configuration
573  */
574 #define STFSM_S25FL_CONFIG_QE           (0x1 << 1)
575
576 /*
577  * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
578  * Register, Extended Address Modes, and a 32-bit address command set.  The
579  * 32-bit address command set is used here, since it avoids any problems with
580  * entering a state that is incompatible with the SPIBoot Controller.
581  */
582 static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
583         {FLASH_FLAG_READ_1_4_4,  FLASH_CMD_READ4_1_4_4,  0, 4, 4, 0x00, 2, 4},
584         {FLASH_FLAG_READ_1_1_4,  FLASH_CMD_READ4_1_1_4,  0, 1, 4, 0x00, 0, 8},
585         {FLASH_FLAG_READ_1_2_2,  FLASH_CMD_READ4_1_2_2,  0, 2, 2, 0x00, 4, 0},
586         {FLASH_FLAG_READ_1_1_2,  FLASH_CMD_READ4_1_1_2,  0, 1, 2, 0x00, 0, 8},
587         {FLASH_FLAG_READ_FAST,   FLASH_CMD_READ4_FAST,   0, 1, 1, 0x00, 0, 8},
588         {FLASH_FLAG_READ_WRITE,  FLASH_CMD_READ4,        0, 1, 1, 0x00, 0, 0},
589         {0x00,                   0,                      0, 0, 0, 0x00, 0, 0},
590 };
591
592 static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
593         {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
594         {FLASH_FLAG_READ_WRITE,  S25FL_CMD_WRITE4,       1, 1, 1, 0x00, 0, 0},
595         {0x00,                   0,                      0, 0, 0, 0x00, 0, 0},
596 };
597
598 /*
599  * [W25Qxxx] Configuration
600  */
601 #define W25Q_STATUS_QE                  (0x1 << 1)
602
603 static struct stfsm_seq stfsm_seq_read_jedec = {
604         .data_size = TRANSFER_SIZE(8),
605         .seq_opc[0] = (SEQ_OPC_PADS_1 |
606                        SEQ_OPC_CYCLES(8) |
607                        SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
608         .seq = {
609                 STFSM_INST_CMD1,
610                 STFSM_INST_DATA_READ,
611                 STFSM_INST_STOP,
612         },
613         .seq_cfg = (SEQ_CFG_PADS_1 |
614                     SEQ_CFG_READNOTWRITE |
615                     SEQ_CFG_CSDEASSERT |
616                     SEQ_CFG_STARTSEQ),
617 };
618
619 static struct stfsm_seq stfsm_seq_read_status_fifo = {
620         .data_size = TRANSFER_SIZE(4),
621         .seq_opc[0] = (SEQ_OPC_PADS_1 |
622                        SEQ_OPC_CYCLES(8) |
623                        SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
624         .seq = {
625                 STFSM_INST_CMD1,
626                 STFSM_INST_DATA_READ,
627                 STFSM_INST_STOP,
628         },
629         .seq_cfg = (SEQ_CFG_PADS_1 |
630                     SEQ_CFG_READNOTWRITE |
631                     SEQ_CFG_CSDEASSERT |
632                     SEQ_CFG_STARTSEQ),
633 };
634
635 static struct stfsm_seq stfsm_seq_erase_sector = {
636         /* 'addr_cfg' configured during initialisation */
637         .seq_opc = {
638                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
639                  SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
640
641                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
642                  SEQ_OPC_OPCODE(FLASH_CMD_SE)),
643         },
644         .seq = {
645                 STFSM_INST_CMD1,
646                 STFSM_INST_CMD2,
647                 STFSM_INST_ADD1,
648                 STFSM_INST_ADD2,
649                 STFSM_INST_STOP,
650         },
651         .seq_cfg = (SEQ_CFG_PADS_1 |
652                     SEQ_CFG_READNOTWRITE |
653                     SEQ_CFG_CSDEASSERT |
654                     SEQ_CFG_STARTSEQ),
655 };
656
657 static struct stfsm_seq stfsm_seq_erase_chip = {
658         .seq_opc = {
659                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
660                  SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
661
662                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
663                  SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
664         },
665         .seq = {
666                 STFSM_INST_CMD1,
667                 STFSM_INST_CMD2,
668                 STFSM_INST_WAIT,
669                 STFSM_INST_STOP,
670         },
671         .seq_cfg = (SEQ_CFG_PADS_1 |
672                     SEQ_CFG_ERASE |
673                     SEQ_CFG_READNOTWRITE |
674                     SEQ_CFG_CSDEASSERT |
675                     SEQ_CFG_STARTSEQ),
676 };
677
678 static struct stfsm_seq stfsm_seq_write_status = {
679         .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
680                        SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
681         .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
682                        SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
683         .seq = {
684                 STFSM_INST_CMD1,
685                 STFSM_INST_CMD2,
686                 STFSM_INST_STA_WR1,
687                 STFSM_INST_STOP,
688         },
689         .seq_cfg = (SEQ_CFG_PADS_1 |
690                     SEQ_CFG_READNOTWRITE |
691                     SEQ_CFG_CSDEASSERT |
692                     SEQ_CFG_STARTSEQ),
693 };
694
695 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
696 {
697         seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
698                            SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
699         seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
700                            SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
701                            SEQ_OPC_CSDEASSERT);
702
703         seq->seq[0] = STFSM_INST_CMD2;
704         seq->seq[1] = STFSM_INST_CMD1;
705         seq->seq[2] = STFSM_INST_WAIT;
706         seq->seq[3] = STFSM_INST_STOP;
707
708         seq->seq_cfg = (SEQ_CFG_PADS_1 |
709                         SEQ_CFG_ERASE |
710                         SEQ_CFG_READNOTWRITE |
711                         SEQ_CFG_CSDEASSERT |
712                         SEQ_CFG_STARTSEQ);
713
714         return 0;
715 }
716
717 static inline int stfsm_is_idle(struct stfsm *fsm)
718 {
719         return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
720 }
721
722 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
723 {
724         return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
725 }
726
727 static void stfsm_clear_fifo(struct stfsm *fsm)
728 {
729         uint32_t avail;
730
731         for (;;) {
732                 avail = stfsm_fifo_available(fsm);
733                 if (!avail)
734                         break;
735
736                 while (avail) {
737                         readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
738                         avail--;
739                 }
740         }
741 }
742
743 static inline void stfsm_load_seq(struct stfsm *fsm,
744                                   const struct stfsm_seq *seq)
745 {
746         void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
747         const uint32_t *src = (const uint32_t *)seq;
748         int words = sizeof(*seq) / sizeof(*src);
749
750         BUG_ON(!stfsm_is_idle(fsm));
751
752         while (words--) {
753                 writel(*src, dst);
754                 src++;
755                 dst += 4;
756         }
757 }
758
759 static void stfsm_wait_seq(struct stfsm *fsm)
760 {
761         unsigned long deadline;
762         int timeout = 0;
763
764         deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
765
766         while (!timeout) {
767                 if (time_after_eq(jiffies, deadline))
768                         timeout = 1;
769
770                 if (stfsm_is_idle(fsm))
771                         return;
772
773                 cond_resched();
774         }
775
776         dev_err(fsm->dev, "timeout on sequence completion\n");
777 }
778
779 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
780 {
781         uint32_t remaining = size >> 2;
782         uint32_t avail;
783         uint32_t words;
784
785         dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
786
787         BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
788
789         while (remaining) {
790                 for (;;) {
791                         avail = stfsm_fifo_available(fsm);
792                         if (avail)
793                                 break;
794                         udelay(1);
795                 }
796                 words = min(avail, remaining);
797                 remaining -= words;
798
799                 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
800                 buf += words;
801         }
802 }
803
804 static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
805                             uint32_t size)
806 {
807         uint32_t words = size >> 2;
808
809         dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
810
811         BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
812
813         writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
814
815         return size;
816 }
817
818 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
819 {
820         struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
821         uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
822
823         seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
824                            SEQ_OPC_CYCLES(8) |
825                            SEQ_OPC_OPCODE(cmd) |
826                            SEQ_OPC_CSDEASSERT);
827
828         stfsm_load_seq(fsm, seq);
829
830         stfsm_wait_seq(fsm);
831
832         return 0;
833 }
834
835 static uint8_t stfsm_wait_busy(struct stfsm *fsm)
836 {
837         struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
838         unsigned long deadline;
839         uint32_t status;
840         int timeout = 0;
841
842         /* Use RDRS1 */
843         seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
844                            SEQ_OPC_CYCLES(8) |
845                            SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
846
847         /* Load read_status sequence */
848         stfsm_load_seq(fsm, seq);
849
850         /*
851          * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
852          */
853         deadline = jiffies + FLASH_MAX_BUSY_WAIT;
854         while (!timeout) {
855                 if (time_after_eq(jiffies, deadline))
856                         timeout = 1;
857
858                 stfsm_wait_seq(fsm);
859
860                 stfsm_read_fifo(fsm, &status, 4);
861
862                 if ((status & FLASH_STATUS_BUSY) == 0)
863                         return 0;
864
865                 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
866                     ((status & S25FL_STATUS_P_ERR) ||
867                      (status & S25FL_STATUS_E_ERR)))
868                         return (uint8_t)(status & 0xff);
869
870                 if (!timeout)
871                         /* Restart */
872                         writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
873
874                 cond_resched();
875         }
876
877         dev_err(fsm->dev, "timeout on wait_busy\n");
878
879         return FLASH_STATUS_TIMEOUT;
880 }
881
882 static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
883                              uint8_t *data, int bytes)
884 {
885         struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
886         uint32_t tmp;
887         uint8_t *t = (uint8_t *)&tmp;
888         int i;
889
890         dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
891                 cmd, bytes);
892
893         BUG_ON(bytes != 1 && bytes != 2);
894
895         seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
896                            SEQ_OPC_OPCODE(cmd)),
897
898         stfsm_load_seq(fsm, seq);
899
900         stfsm_read_fifo(fsm, &tmp, 4);
901
902         for (i = 0; i < bytes; i++)
903                 data[i] = t[i];
904
905         stfsm_wait_seq(fsm);
906
907         return 0;
908 }
909
910 static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
911                             uint16_t data, int bytes, int wait_busy)
912 {
913         struct stfsm_seq *seq = &stfsm_seq_write_status;
914
915         dev_dbg(fsm->dev,
916                 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
917                 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
918
919         BUG_ON(bytes != 1 && bytes != 2);
920
921         seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
922                            SEQ_OPC_OPCODE(cmd));
923
924         seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
925         seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
926
927         stfsm_load_seq(fsm, seq);
928
929         stfsm_wait_seq(fsm);
930
931         if (wait_busy)
932                 stfsm_wait_busy(fsm);
933
934         return 0;
935 }
936
937 /*
938  * SoC reset on 'boot-from-spi' systems
939  *
940  * Certain modes of operation cause the Flash device to enter a particular state
941  * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
942  * Addr' commands).  On boot-from-spi systems, it is important to consider what
943  * happens if a warm reset occurs during this period.  The SPIBoot controller
944  * assumes that Flash device is in its default reset state, 24-bit address mode,
945  * and ready to accept commands.  This can be achieved using some form of
946  * on-board logic/controller to force a device POR in response to a SoC-level
947  * reset or by making use of the device reset signal if available (limited
948  * number of devices only).
949  *
950  * Failure to take such precautions can cause problems following a warm reset.
951  * For some operations (e.g. ERASE), there is little that can be done.  For
952  * other modes of operation (e.g. 32-bit addressing), options are often
953  * available that can help minimise the window in which a reset could cause a
954  * problem.
955  *
956  */
957 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
958 {
959         /* Reset signal is available on the board and supported by the device */
960         if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
961                 return true;
962
963         /* Board-level logic forces a power-on-reset */
964         if (fsm->reset_por)
965                 return true;
966
967         /* Reset is not properly handled and may result in failure to reboot */
968         return false;
969 }
970
971 /* Configure 'addr_cfg' according to addressing mode */
972 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
973                                        struct stfsm_seq *seq)
974 {
975         int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
976
977         seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
978                          ADR_CFG_PADS_1_ADD1 |
979                          ADR_CFG_CYCLES_ADD2(16) |
980                          ADR_CFG_PADS_1_ADD2 |
981                          ADR_CFG_CSDEASSERT_ADD2);
982 }
983
984 /* Search for preferred configuration based on available flags */
985 static struct seq_rw_config *
986 stfsm_search_seq_rw_configs(struct stfsm *fsm,
987                             struct seq_rw_config cfgs[])
988 {
989         struct seq_rw_config *config;
990         int flags = fsm->info->flags;
991
992         for (config = cfgs; config->cmd != 0; config++)
993                 if ((config->flags & flags) == config->flags)
994                         return config;
995
996         return NULL;
997 }
998
999 /* Prepare a READ/WRITE sequence according to configuration parameters */
1000 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1001                                  struct stfsm_seq *seq,
1002                                  struct seq_rw_config *cfg)
1003 {
1004         int addr1_cycles, addr2_cycles;
1005         int i = 0;
1006
1007         memset(seq, 0, sizeof(*seq));
1008
1009         /* Add READ/WRITE OPC  */
1010         seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1011                              SEQ_OPC_CYCLES(8) |
1012                              SEQ_OPC_OPCODE(cfg->cmd));
1013
1014         /* Add WREN OPC for a WRITE sequence */
1015         if (cfg->write)
1016                 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1017                                      SEQ_OPC_CYCLES(8) |
1018                                      SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1019                                      SEQ_OPC_CSDEASSERT);
1020
1021         /* Address configuration (24 or 32-bit addresses) */
1022         addr1_cycles  = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1023         addr1_cycles /= cfg->addr_pads;
1024         addr2_cycles  = 16 / cfg->addr_pads;
1025         seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 |   /* ADD1 cycles */
1026                          (cfg->addr_pads - 1) << 6 |    /* ADD1 pads */
1027                          (addr2_cycles & 0x3f) << 16 |  /* ADD2 cycles */
1028                          ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1029
1030         /* Data/Sequence configuration */
1031         seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1032                         SEQ_CFG_STARTSEQ |
1033                         SEQ_CFG_CSDEASSERT);
1034         if (!cfg->write)
1035                 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1036
1037         /* Mode configuration (no. of pads taken from addr cfg) */
1038         seq->mode = ((cfg->mode_data & 0xff) << 0 |     /* data */
1039                      (cfg->mode_cycles & 0x3f) << 16 |  /* cycles */
1040                      (cfg->addr_pads - 1) << 22);       /* pads */
1041
1042         /* Dummy configuration (no. of pads taken from addr cfg) */
1043         seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 |        /* cycles */
1044                       (cfg->addr_pads - 1) << 22);              /* pads */
1045
1046
1047         /* Instruction sequence */
1048         i = 0;
1049         if (cfg->write)
1050                 seq->seq[i++] = STFSM_INST_CMD2;
1051
1052         seq->seq[i++] = STFSM_INST_CMD1;
1053
1054         seq->seq[i++] = STFSM_INST_ADD1;
1055         seq->seq[i++] = STFSM_INST_ADD2;
1056
1057         if (cfg->mode_cycles)
1058                 seq->seq[i++] = STFSM_INST_MODE;
1059
1060         if (cfg->dummy_cycles)
1061                 seq->seq[i++] = STFSM_INST_DUMMY;
1062
1063         seq->seq[i++] =
1064                 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1065         seq->seq[i++] = STFSM_INST_STOP;
1066 }
1067
1068 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1069                                        struct stfsm_seq *seq,
1070                                        struct seq_rw_config *cfgs)
1071 {
1072         struct seq_rw_config *config;
1073
1074         config = stfsm_search_seq_rw_configs(fsm, cfgs);
1075         if (!config) {
1076                 dev_err(fsm->dev, "failed to find suitable config\n");
1077                 return -EINVAL;
1078         }
1079
1080         stfsm_prepare_rw_seq(fsm, seq, config);
1081
1082         return 0;
1083 }
1084
1085 /* Prepare a READ/WRITE/ERASE 'default' sequences */
1086 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1087 {
1088         uint32_t flags = fsm->info->flags;
1089         int ret;
1090
1091         /* Configure 'READ' sequence */
1092         ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1093                                           default_read_configs);
1094         if (ret) {
1095                 dev_err(fsm->dev,
1096                         "failed to prep READ sequence with flags [0x%08x]\n",
1097                         flags);
1098                 return ret;
1099         }
1100
1101         /* Configure 'WRITE' sequence */
1102         ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1103                                           default_write_configs);
1104         if (ret) {
1105                 dev_err(fsm->dev,
1106                         "failed to prep WRITE sequence with flags [0x%08x]\n",
1107                         flags);
1108                 return ret;
1109         }
1110
1111         /* Configure 'ERASE_SECTOR' sequence */
1112         stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1113
1114         return 0;
1115 }
1116
1117 static int stfsm_mx25_config(struct stfsm *fsm)
1118 {
1119         uint32_t flags = fsm->info->flags;
1120         uint32_t data_pads;
1121         uint8_t sta;
1122         int ret;
1123         bool soc_reset;
1124
1125         /*
1126          * Use default READ/WRITE sequences
1127          */
1128         ret = stfsm_prepare_rwe_seqs_default(fsm);
1129         if (ret)
1130                 return ret;
1131
1132         /*
1133          * Configure 32-bit Address Support
1134          */
1135         if (flags & FLASH_FLAG_32BIT_ADDR) {
1136                 /* Configure 'enter_32bitaddr' FSM sequence */
1137                 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1138
1139                 soc_reset = stfsm_can_handle_soc_reset(fsm);
1140                 if (soc_reset || !fsm->booted_from_spi)
1141                         /* If we can handle SoC resets, we enable 32-bit address
1142                          * mode pervasively */
1143                         stfsm_enter_32bit_addr(fsm, 1);
1144
1145                 else
1146                         /* Else, enable/disable 32-bit addressing before/after
1147                          * each operation */
1148                         fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1149                                               CFG_WRITE_TOGGLE_32BIT_ADDR |
1150                                               CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1151         }
1152
1153         /* Check status of 'QE' bit, update if required. */
1154         stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta, 1);
1155         data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1156         if (data_pads == 4) {
1157                 if (!(sta & MX25_STATUS_QE)) {
1158                         /* Set 'QE' */
1159                         sta |= MX25_STATUS_QE;
1160
1161                         stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
1162                 }
1163         } else {
1164                 if (sta & MX25_STATUS_QE) {
1165                         /* Clear 'QE' */
1166                         sta &= ~MX25_STATUS_QE;
1167
1168                         stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
1169                 }
1170         }
1171
1172         return 0;
1173 }
1174
1175 static int stfsm_n25q_config(struct stfsm *fsm)
1176 {
1177         uint32_t flags = fsm->info->flags;
1178         uint8_t vcr;
1179         int ret = 0;
1180         bool soc_reset;
1181
1182         /* Configure 'READ' sequence */
1183         if (flags & FLASH_FLAG_32BIT_ADDR)
1184                 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1185                                                   n25q_read4_configs);
1186         else
1187                 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1188                                                   n25q_read3_configs);
1189         if (ret) {
1190                 dev_err(fsm->dev,
1191                         "failed to prepare READ sequence with flags [0x%08x]\n",
1192                         flags);
1193                 return ret;
1194         }
1195
1196         /* Configure 'WRITE' sequence (default configs) */
1197         ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1198                                           default_write_configs);
1199         if (ret) {
1200                 dev_err(fsm->dev,
1201                         "preparing WRITE sequence using flags [0x%08x] failed\n",
1202                         flags);
1203                 return ret;
1204         }
1205
1206         /* * Configure 'ERASE_SECTOR' sequence */
1207         stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1208
1209         /* Configure 32-bit address support */
1210         if (flags & FLASH_FLAG_32BIT_ADDR) {
1211                 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1212
1213                 soc_reset = stfsm_can_handle_soc_reset(fsm);
1214                 if (soc_reset || !fsm->booted_from_spi) {
1215                         /*
1216                          * If we can handle SoC resets, we enable 32-bit
1217                          * address mode pervasively
1218                          */
1219                         stfsm_enter_32bit_addr(fsm, 1);
1220                 } else {
1221                         /*
1222                          * If not, enable/disable for WRITE and ERASE
1223                          * operations (READ uses special commands)
1224                          */
1225                         fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1226                                               CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1227                 }
1228         }
1229
1230         /*
1231          * Configure device to use 8 dummy cycles
1232          */
1233         vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1234                N25Q_VCR_WRAP_CONT);
1235         stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
1236
1237         return 0;
1238 }
1239
1240 static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1241 {
1242         seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1243                            SEQ_OPC_CYCLES(8) |
1244                            SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1245
1246         seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1247                          ADR_CFG_PADS_1_ADD1 |
1248                          ADR_CFG_CYCLES_ADD2(16) |
1249                          ADR_CFG_PADS_1_ADD2 |
1250                          ADR_CFG_CSDEASSERT_ADD2);
1251 }
1252
1253 static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1254 {
1255         uint32_t tmp;
1256         struct stfsm_seq seq = {
1257                 .data_size = TRANSFER_SIZE(4),
1258                 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1259                                SEQ_OPC_CYCLES(8) |
1260                                SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1261                 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1262                              ADR_CFG_PADS_1_ADD1 |
1263                              ADR_CFG_CYCLES_ADD2(16) |
1264                              ADR_CFG_PADS_1_ADD2),
1265                 .addr1 = (offs >> 16) & 0xffff,
1266                 .addr2 = offs & 0xffff,
1267                 .seq = {
1268                         STFSM_INST_CMD1,
1269                         STFSM_INST_ADD1,
1270                         STFSM_INST_ADD2,
1271                         STFSM_INST_DATA_READ,
1272                         STFSM_INST_STOP,
1273                 },
1274                 .seq_cfg = (SEQ_CFG_PADS_1 |
1275                             SEQ_CFG_READNOTWRITE |
1276                             SEQ_CFG_CSDEASSERT |
1277                             SEQ_CFG_STARTSEQ),
1278         };
1279
1280         stfsm_load_seq(fsm, &seq);
1281
1282         stfsm_read_fifo(fsm, &tmp, 4);
1283
1284         *dby = (uint8_t)(tmp >> 24);
1285
1286         stfsm_wait_seq(fsm);
1287 }
1288
1289 static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1290 {
1291         struct stfsm_seq seq = {
1292                 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1293                                SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1294                                SEQ_OPC_CSDEASSERT),
1295                 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1296                                SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1297                 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1298                              ADR_CFG_PADS_1_ADD1 |
1299                              ADR_CFG_CYCLES_ADD2(16) |
1300                              ADR_CFG_PADS_1_ADD2),
1301                 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1302                 .addr1 = (offs >> 16) & 0xffff,
1303                 .addr2 = offs & 0xffff,
1304                 .seq = {
1305                         STFSM_INST_CMD1,
1306                         STFSM_INST_CMD2,
1307                         STFSM_INST_ADD1,
1308                         STFSM_INST_ADD2,
1309                         STFSM_INST_STA_WR1,
1310                         STFSM_INST_STOP,
1311                 },
1312                 .seq_cfg = (SEQ_CFG_PADS_1 |
1313                             SEQ_CFG_READNOTWRITE |
1314                             SEQ_CFG_CSDEASSERT |
1315                             SEQ_CFG_STARTSEQ),
1316         };
1317
1318         stfsm_load_seq(fsm, &seq);
1319         stfsm_wait_seq(fsm);
1320
1321         stfsm_wait_busy(fsm);
1322 }
1323
1324 static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1325 {
1326         struct stfsm_seq seq = {
1327                 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1328                                SEQ_OPC_CYCLES(8) |
1329                                SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1330                                SEQ_OPC_CSDEASSERT),
1331                 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1332                                SEQ_OPC_CYCLES(8) |
1333                                SEQ_OPC_OPCODE(FLASH_CMD_WRDI) |
1334                                SEQ_OPC_CSDEASSERT),
1335                 .seq = {
1336                         STFSM_INST_CMD1,
1337                         STFSM_INST_CMD2,
1338                         STFSM_INST_WAIT,
1339                         STFSM_INST_STOP,
1340                 },
1341                 .seq_cfg = (SEQ_CFG_PADS_1 |
1342                             SEQ_CFG_ERASE |
1343                             SEQ_CFG_READNOTWRITE |
1344                             SEQ_CFG_CSDEASSERT |
1345                             SEQ_CFG_STARTSEQ),
1346         };
1347
1348         stfsm_load_seq(fsm, &seq);
1349
1350         stfsm_wait_seq(fsm);
1351
1352         return 0;
1353 }
1354
1355 static int stfsm_s25fl_config(struct stfsm *fsm)
1356 {
1357         struct flash_info *info = fsm->info;
1358         uint32_t flags = info->flags;
1359         uint32_t data_pads;
1360         uint32_t offs;
1361         uint16_t sta_wr;
1362         uint8_t sr1, cr1, dyb;
1363         int update_sr = 0;
1364         int ret;
1365
1366         if (flags & FLASH_FLAG_32BIT_ADDR) {
1367                 /*
1368                  * Prepare Read/Write/Erase sequences according to S25FLxxx
1369                  * 32-bit address command set
1370                  */
1371                 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1372                                                   stfsm_s25fl_read4_configs);
1373                 if (ret)
1374                         return ret;
1375
1376                 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1377                                                   stfsm_s25fl_write4_configs);
1378                 if (ret)
1379                         return ret;
1380
1381                 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1382
1383         } else {
1384                 /* Use default configurations for 24-bit addressing */
1385                 ret = stfsm_prepare_rwe_seqs_default(fsm);
1386                 if (ret)
1387                         return ret;
1388         }
1389
1390         /*
1391          * For devices that support 'DYB' sector locking, check lock status and
1392          * unlock sectors if necessary (some variants power-on with sectors
1393          * locked by default)
1394          */
1395         if (flags & FLASH_FLAG_DYB_LOCKING) {
1396                 offs = 0;
1397                 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1398                         stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1399                         if (dyb == 0x00)
1400                                 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1401
1402                         /* Handle bottom/top 4KiB parameter sectors */
1403                         if ((offs < info->sector_size * 2) ||
1404                             (offs >= (info->sector_size - info->n_sectors * 4)))
1405                                 offs += 0x1000;
1406                         else
1407                                 offs += 0x10000;
1408                 }
1409         }
1410
1411         /* Check status of 'QE' bit, update if required. */
1412         stfsm_read_status(fsm, FLASH_CMD_RDSR2, &cr1, 1);
1413         data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1414         if (data_pads == 4) {
1415                 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1416                         /* Set 'QE' */
1417                         cr1 |= STFSM_S25FL_CONFIG_QE;
1418
1419                         update_sr = 1;
1420                 }
1421         } else {
1422                 if (cr1 & STFSM_S25FL_CONFIG_QE) {
1423                         /* Clear 'QE' */
1424                         cr1 &= ~STFSM_S25FL_CONFIG_QE;
1425
1426                         update_sr = 1;
1427                 }
1428         }
1429         if (update_sr) {
1430                 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1431                 sta_wr = ((uint16_t)cr1  << 8) | sr1;
1432                 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta_wr, 2, 1);
1433         }
1434
1435         /*
1436          * S25FLxxx devices support Program and Error error flags.
1437          * Configure driver to check flags and clear if necessary.
1438          */
1439         fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1440
1441         return 0;
1442 }
1443
1444 static int stfsm_w25q_config(struct stfsm *fsm)
1445 {
1446         uint32_t data_pads;
1447         uint8_t sr1, sr2;
1448         uint16_t sr_wr;
1449         int update_sr = 0;
1450         int ret;
1451
1452         ret = stfsm_prepare_rwe_seqs_default(fsm);
1453         if (ret)
1454                 return ret;
1455
1456         /* Check status of 'QE' bit, update if required. */
1457         stfsm_read_status(fsm, FLASH_CMD_RDSR2, &sr2, 1);
1458         data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1459         if (data_pads == 4) {
1460                 if (!(sr2 & W25Q_STATUS_QE)) {
1461                         /* Set 'QE' */
1462                         sr2 |= W25Q_STATUS_QE;
1463                         update_sr = 1;
1464                 }
1465         } else {
1466                 if (sr2 & W25Q_STATUS_QE) {
1467                         /* Clear 'QE' */
1468                         sr2 &= ~W25Q_STATUS_QE;
1469                         update_sr = 1;
1470                 }
1471         }
1472         if (update_sr) {
1473                 /* Write status register */
1474                 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1475                 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1476                 stfsm_write_status(fsm, FLASH_CMD_WRSR, sr_wr, 2, 1);
1477         }
1478
1479         return 0;
1480 }
1481
1482 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1483                       uint32_t offset)
1484 {
1485         struct stfsm_seq *seq = &fsm->stfsm_seq_read;
1486         uint32_t data_pads;
1487         uint32_t read_mask;
1488         uint32_t size_ub;
1489         uint32_t size_lb;
1490         uint32_t size_mop;
1491         uint32_t tmp[4];
1492         uint32_t page_buf[FLASH_PAGESIZE_32];
1493         uint8_t *p;
1494
1495         dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1496
1497         /* Enter 32-bit address mode, if required */
1498         if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1499                 stfsm_enter_32bit_addr(fsm, 1);
1500
1501         /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1502         data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1503         read_mask = (data_pads << 2) - 1;
1504
1505         /* Handle non-aligned buf */
1506         p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1507
1508         /* Handle non-aligned size */
1509         size_ub = (size + read_mask) & ~read_mask;
1510         size_lb = size & ~read_mask;
1511         size_mop = size & read_mask;
1512
1513         seq->data_size = TRANSFER_SIZE(size_ub);
1514         seq->addr1 = (offset >> 16) & 0xffff;
1515         seq->addr2 = offset & 0xffff;
1516
1517         stfsm_load_seq(fsm, seq);
1518
1519         if (size_lb)
1520                 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1521
1522         if (size_mop) {
1523                 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1524                 memcpy(p + size_lb, &tmp, size_mop);
1525         }
1526
1527         /* Handle non-aligned buf */
1528         if ((uint32_t)buf & 0x3)
1529                 memcpy(buf, page_buf, size);
1530
1531         /* Wait for sequence to finish */
1532         stfsm_wait_seq(fsm);
1533
1534         stfsm_clear_fifo(fsm);
1535
1536         /* Exit 32-bit address mode, if required */
1537         if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1538                 stfsm_enter_32bit_addr(fsm, 0);
1539
1540         return 0;
1541 }
1542
1543 static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1544                        uint32_t size, uint32_t offset)
1545 {
1546         struct stfsm_seq *seq = &fsm->stfsm_seq_write;
1547         uint32_t data_pads;
1548         uint32_t write_mask;
1549         uint32_t size_ub;
1550         uint32_t size_lb;
1551         uint32_t size_mop;
1552         uint32_t tmp[4];
1553         uint32_t page_buf[FLASH_PAGESIZE_32];
1554         uint8_t *t = (uint8_t *)&tmp;
1555         const uint8_t *p;
1556         int ret;
1557         int i;
1558
1559         dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1560
1561         /* Enter 32-bit address mode, if required */
1562         if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1563                 stfsm_enter_32bit_addr(fsm, 1);
1564
1565         /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1566         data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1567         write_mask = (data_pads << 2) - 1;
1568
1569         /* Handle non-aligned buf */
1570         if ((uint32_t)buf & 0x3) {
1571                 memcpy(page_buf, buf, size);
1572                 p = (uint8_t *)page_buf;
1573         } else {
1574                 p = buf;
1575         }
1576
1577         /* Handle non-aligned size */
1578         size_ub = (size + write_mask) & ~write_mask;
1579         size_lb = size & ~write_mask;
1580         size_mop = size & write_mask;
1581
1582         seq->data_size = TRANSFER_SIZE(size_ub);
1583         seq->addr1 = (offset >> 16) & 0xffff;
1584         seq->addr2 = offset & 0xffff;
1585
1586         /* Need to set FIFO to write mode, before writing data to FIFO (see
1587          * GNBvb79594)
1588          */
1589         writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1590
1591         /*
1592          * Before writing data to the FIFO, apply a small delay to allow a
1593          * potential change of FIFO direction to complete.
1594          */
1595         if (fsm->fifo_dir_delay == 0)
1596                 readl(fsm->base + SPI_FAST_SEQ_CFG);
1597         else
1598                 udelay(fsm->fifo_dir_delay);
1599
1600
1601         /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1602         if (size_lb) {
1603                 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1604                 p += size_lb;
1605         }
1606
1607         /* Handle non-aligned size */
1608         if (size_mop) {
1609                 memset(t, 0xff, write_mask + 1);        /* fill with 0xff's */
1610                 for (i = 0; i < size_mop; i++)
1611                         t[i] = *p++;
1612
1613                 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1614         }
1615
1616         /* Start sequence */
1617         stfsm_load_seq(fsm, seq);
1618
1619         /* Wait for sequence to finish */
1620         stfsm_wait_seq(fsm);
1621
1622         /* Wait for completion */
1623         ret = stfsm_wait_busy(fsm);
1624         if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1625                 stfsm_s25fl_clear_status_reg(fsm);
1626
1627         /* Exit 32-bit address mode, if required */
1628         if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1629                 stfsm_enter_32bit_addr(fsm, 0);
1630
1631         return 0;
1632 }
1633
1634 /*
1635  * Read an address range from the flash chip. The address range
1636  * may be any size provided it is within the physical boundaries.
1637  */
1638 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1639                           size_t *retlen, u_char *buf)
1640 {
1641         struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1642         uint32_t bytes;
1643
1644         dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1645                 __func__, (u32)from, len);
1646
1647         mutex_lock(&fsm->lock);
1648
1649         while (len > 0) {
1650                 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1651
1652                 stfsm_read(fsm, buf, bytes, from);
1653
1654                 buf += bytes;
1655                 from += bytes;
1656                 len -= bytes;
1657
1658                 *retlen += bytes;
1659         }
1660
1661         mutex_unlock(&fsm->lock);
1662
1663         return 0;
1664 }
1665
1666 static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
1667 {
1668         struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1669         int ret;
1670
1671         dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1672
1673         /* Enter 32-bit address mode, if required */
1674         if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1675                 stfsm_enter_32bit_addr(fsm, 1);
1676
1677         seq->addr1 = (offset >> 16) & 0xffff;
1678         seq->addr2 = offset & 0xffff;
1679
1680         stfsm_load_seq(fsm, seq);
1681
1682         stfsm_wait_seq(fsm);
1683
1684         /* Wait for completion */
1685         ret = stfsm_wait_busy(fsm);
1686         if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1687                 stfsm_s25fl_clear_status_reg(fsm);
1688
1689         /* Exit 32-bit address mode, if required */
1690         if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1691                 stfsm_enter_32bit_addr(fsm, 0);
1692
1693         return ret;
1694 }
1695
1696 static int stfsm_erase_chip(struct stfsm *fsm)
1697 {
1698         const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1699
1700         dev_dbg(fsm->dev, "erasing chip\n");
1701
1702         stfsm_load_seq(fsm, seq);
1703
1704         stfsm_wait_seq(fsm);
1705
1706         return stfsm_wait_busy(fsm);
1707 }
1708
1709 /*
1710  * Write an address range to the flash chip.  Data must be written in
1711  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1712  * it is within the physical boundaries.
1713  */
1714 static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1715                            size_t *retlen, const u_char *buf)
1716 {
1717         struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1718
1719         u32 page_offs;
1720         u32 bytes;
1721         uint8_t *b = (uint8_t *)buf;
1722         int ret = 0;
1723
1724         dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1725
1726         /* Offset within page */
1727         page_offs = to % FLASH_PAGESIZE;
1728
1729         mutex_lock(&fsm->lock);
1730
1731         while (len) {
1732                 /* Write up to page boundary */
1733                 bytes = min(FLASH_PAGESIZE - page_offs, len);
1734
1735                 ret = stfsm_write(fsm, b, bytes, to);
1736                 if (ret)
1737                         goto out1;
1738
1739                 b += bytes;
1740                 len -= bytes;
1741                 to += bytes;
1742
1743                 /* We are now page-aligned */
1744                 page_offs = 0;
1745
1746                 *retlen += bytes;
1747
1748         }
1749
1750 out1:
1751         mutex_unlock(&fsm->lock);
1752
1753         return ret;
1754 }
1755
1756 /*
1757  * Erase an address range on the flash chip. The address range may extend
1758  * one or more erase sectors.  Return an error is there is a problem erasing.
1759  */
1760 static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1761 {
1762         struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1763         u32 addr, len;
1764         int ret;
1765
1766         dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1767                 (long long)instr->addr, (long long)instr->len);
1768
1769         addr = instr->addr;
1770         len = instr->len;
1771
1772         mutex_lock(&fsm->lock);
1773
1774         /* Whole-chip erase? */
1775         if (len == mtd->size) {
1776                 ret = stfsm_erase_chip(fsm);
1777                 if (ret)
1778                         goto out1;
1779         } else {
1780                 while (len) {
1781                         ret = stfsm_erase_sector(fsm, addr);
1782                         if (ret)
1783                                 goto out1;
1784
1785                         addr += mtd->erasesize;
1786                         len -= mtd->erasesize;
1787                 }
1788         }
1789
1790         mutex_unlock(&fsm->lock);
1791
1792         instr->state = MTD_ERASE_DONE;
1793         mtd_erase_callback(instr);
1794
1795         return 0;
1796
1797 out1:
1798         instr->state = MTD_ERASE_FAILED;
1799         mutex_unlock(&fsm->lock);
1800
1801         return ret;
1802 }
1803
1804 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
1805 {
1806         const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1807         uint32_t tmp[2];
1808
1809         stfsm_load_seq(fsm, seq);
1810
1811         stfsm_read_fifo(fsm, tmp, 8);
1812
1813         memcpy(jedec, tmp, 5);
1814
1815         stfsm_wait_seq(fsm);
1816 }
1817
1818 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1819 {
1820         struct flash_info       *info;
1821         u16                     ext_jedec;
1822         u32                     jedec;
1823         u8                      id[5];
1824
1825         stfsm_read_jedec(fsm, id);
1826
1827         jedec     = id[0] << 16 | id[1] << 8 | id[2];
1828         /*
1829          * JEDEC also defines an optional "extended device information"
1830          * string for after vendor-specific data, after the three bytes
1831          * we use here. Supporting some chips might require using it.
1832          */
1833         ext_jedec = id[3] << 8  | id[4];
1834
1835         dev_dbg(fsm->dev, "JEDEC =  0x%08x [%02x %02x %02x %02x %02x]\n",
1836                 jedec, id[0], id[1], id[2], id[3], id[4]);
1837
1838         for (info = flash_types; info->name; info++) {
1839                 if (info->jedec_id == jedec) {
1840                         if (info->ext_id && info->ext_id != ext_jedec)
1841                                 continue;
1842                         return info;
1843                 }
1844         }
1845         dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1846
1847         return NULL;
1848 }
1849
1850 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1851 {
1852         int ret, timeout = 10;
1853
1854         /* Wait for controller to accept mode change */
1855         while (--timeout) {
1856                 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1857                 if (ret & 0x1)
1858                         break;
1859                 udelay(1);
1860         }
1861
1862         if (!timeout)
1863                 return -EBUSY;
1864
1865         writel(mode, fsm->base + SPI_MODESELECT);
1866
1867         return 0;
1868 }
1869
1870 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1871 {
1872         uint32_t emi_freq;
1873         uint32_t clk_div;
1874
1875         /* TODO: Make this dynamic */
1876         emi_freq = STFSM_DEFAULT_EMI_FREQ;
1877
1878         /*
1879          * Calculate clk_div - values between 2 and 128
1880          * Multiple of 2, rounded up
1881          */
1882         clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1883         if (clk_div < 2)
1884                 clk_div = 2;
1885         else if (clk_div > 128)
1886                 clk_div = 128;
1887
1888         /*
1889          * Determine a suitable delay for the IP to complete a change of
1890          * direction of the FIFO. The required delay is related to the clock
1891          * divider used. The following heuristics are based on empirical tests,
1892          * using a 100MHz EMI clock.
1893          */
1894         if (clk_div <= 4)
1895                 fsm->fifo_dir_delay = 0;
1896         else if (clk_div <= 10)
1897                 fsm->fifo_dir_delay = 1;
1898         else
1899                 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1900
1901         dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1902                 emi_freq, spi_freq, clk_div);
1903
1904         writel(clk_div, fsm->base + SPI_CLOCKDIV);
1905 }
1906
1907 static int stfsm_init(struct stfsm *fsm)
1908 {
1909         int ret;
1910
1911         /* Perform a soft reset of the FSM controller */
1912         writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1913         udelay(1);
1914         writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1915
1916         /* Set clock to 'safe' frequency initially */
1917         stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1918
1919         /* Switch to FSM */
1920         ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1921         if (ret)
1922                 return ret;
1923
1924         /* Set timing parameters */
1925         writel(SPI_CFG_DEVICE_ST            |
1926                SPI_CFG_DEFAULT_MIN_CS_HIGH  |
1927                SPI_CFG_DEFAULT_CS_SETUPHOLD |
1928                SPI_CFG_DEFAULT_DATA_HOLD,
1929                fsm->base + SPI_CONFIGDATA);
1930         writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1931
1932         /*
1933          * Set the FSM 'WAIT' delay to the minimum workable value.  Note, for
1934          * our purposes, the WAIT instruction is used purely to achieve
1935          * "sequence validity" rather than actually implement a delay.
1936          */
1937         writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1938
1939         /* Clear FIFO, just in case */
1940         stfsm_clear_fifo(fsm);
1941
1942         return 0;
1943 }
1944
1945 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1946 {
1947         struct stfsm *fsm = platform_get_drvdata(pdev);
1948         struct device_node *np = pdev->dev.of_node;
1949         struct regmap *regmap;
1950         uint32_t boot_device_reg;
1951         uint32_t boot_device_spi;
1952         uint32_t boot_device;     /* Value we read from *boot_device_reg */
1953         int ret;
1954
1955         /* Booting from SPI NOR Flash is the default */
1956         fsm->booted_from_spi = true;
1957
1958         regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1959         if (IS_ERR(regmap))
1960                 goto boot_device_fail;
1961
1962         fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1963
1964         fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1965
1966         /* Where in the syscon the boot device information lives */
1967         ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1968         if (ret)
1969                 goto boot_device_fail;
1970
1971         /* Boot device value when booted from SPI NOR */
1972         ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1973         if (ret)
1974                 goto boot_device_fail;
1975
1976         ret = regmap_read(regmap, boot_device_reg, &boot_device);
1977         if (ret)
1978                 goto boot_device_fail;
1979
1980         if (boot_device != boot_device_spi)
1981                 fsm->booted_from_spi = false;
1982
1983         return;
1984
1985 boot_device_fail:
1986         dev_warn(&pdev->dev,
1987                  "failed to fetch boot device, assuming boot from SPI\n");
1988 }
1989
1990 static int stfsm_probe(struct platform_device *pdev)
1991 {
1992         struct device_node *np = pdev->dev.of_node;
1993         struct mtd_part_parser_data ppdata;
1994         struct flash_info *info;
1995         struct resource *res;
1996         struct stfsm *fsm;
1997         int ret;
1998
1999         if (!np) {
2000                 dev_err(&pdev->dev, "No DT found\n");
2001                 return -EINVAL;
2002         }
2003         ppdata.of_node = np;
2004
2005         fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2006         if (!fsm)
2007                 return -ENOMEM;
2008
2009         fsm->dev = &pdev->dev;
2010
2011         platform_set_drvdata(pdev, fsm);
2012
2013         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2014         if (!res) {
2015                 dev_err(&pdev->dev, "Resource not found\n");
2016                 return -ENODEV;
2017         }
2018
2019         fsm->base = devm_ioremap_resource(&pdev->dev, res);
2020         if (IS_ERR(fsm->base)) {
2021                 dev_err(&pdev->dev,
2022                         "Failed to reserve memory region %pR\n", res);
2023                 return PTR_ERR(fsm->base);
2024         }
2025
2026         mutex_init(&fsm->lock);
2027
2028         ret = stfsm_init(fsm);
2029         if (ret) {
2030                 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2031                 return ret;
2032         }
2033
2034         stfsm_fetch_platform_configs(pdev);
2035
2036         /* Detect SPI FLASH device */
2037         info = stfsm_jedec_probe(fsm);
2038         if (!info)
2039                 return -ENODEV;
2040         fsm->info = info;
2041
2042         /* Use device size to determine address width */
2043         if (info->sector_size * info->n_sectors > 0x1000000)
2044                 info->flags |= FLASH_FLAG_32BIT_ADDR;
2045
2046         /*
2047          * Configure READ/WRITE/ERASE sequences according to platform and
2048          * device flags.
2049          */
2050         if (info->config) {
2051                 ret = info->config(fsm);
2052                 if (ret)
2053                         return ret;
2054         } else {
2055                 ret = stfsm_prepare_rwe_seqs_default(fsm);
2056                 if (ret)
2057                         return ret;
2058         }
2059
2060         fsm->mtd.name           = info->name;
2061         fsm->mtd.dev.parent     = &pdev->dev;
2062         fsm->mtd.type           = MTD_NORFLASH;
2063         fsm->mtd.writesize      = 4;
2064         fsm->mtd.writebufsize   = fsm->mtd.writesize;
2065         fsm->mtd.flags          = MTD_CAP_NORFLASH;
2066         fsm->mtd.size           = info->sector_size * info->n_sectors;
2067         fsm->mtd.erasesize      = info->sector_size;
2068
2069         fsm->mtd._read  = stfsm_mtd_read;
2070         fsm->mtd._write = stfsm_mtd_write;
2071         fsm->mtd._erase = stfsm_mtd_erase;
2072
2073         dev_info(&pdev->dev,
2074                 "Found serial flash device: %s\n"
2075                 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2076                 info->name,
2077                 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2078                 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
2079
2080         return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
2081 }
2082
2083 static int stfsm_remove(struct platform_device *pdev)
2084 {
2085         struct stfsm *fsm = platform_get_drvdata(pdev);
2086
2087         return mtd_device_unregister(&fsm->mtd);
2088 }
2089
2090 static struct of_device_id stfsm_match[] = {
2091         { .compatible = "st,spi-fsm", },
2092         {},
2093 };
2094 MODULE_DEVICE_TABLE(of, stfsm_match);
2095
2096 static struct platform_driver stfsm_driver = {
2097         .probe          = stfsm_probe,
2098         .remove         = stfsm_remove,
2099         .driver         = {
2100                 .name   = "st-spi-fsm",
2101                 .owner  = THIS_MODULE,
2102                 .of_match_table = stfsm_match,
2103         },
2104 };
2105 module_platform_driver(stfsm_driver);
2106
2107 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2108 MODULE_DESCRIPTION("ST SPI FSM driver");
2109 MODULE_LICENSE("GPL");