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