mtd: st_spi_fsm: Add the ability to read from a Serial Flash device
[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 #define FLASH_PAGESIZE         256                      /* In Bytes    */
242 #define FLASH_PAGESIZE_32      (FLASH_PAGESIZE / 4)     /* In uint32_t */
243
244 /*
245  * Flags to tweak operation of default read/write/erase routines
246  */
247 #define CFG_READ_TOGGLE_32BIT_ADDR     0x00000001
248 #define CFG_WRITE_TOGGLE_32BIT_ADDR    0x00000002
249 #define CFG_WRITE_EX_32BIT_ADDR_DELAY  0x00000004
250 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
251 #define CFG_S25FL_CHECK_ERROR_FLAGS    0x00000010
252
253 struct stfsm {
254         struct device           *dev;
255         void __iomem            *base;
256         struct resource         *region;
257         struct mtd_info         mtd;
258         struct mutex            lock;
259         struct flash_info       *info;
260
261         uint32_t                configuration;
262         uint32_t                fifo_dir_delay;
263         bool                    booted_from_spi;
264         bool                    reset_signal;
265         bool                    reset_por;
266 };
267
268 struct stfsm_seq {
269         uint32_t data_size;
270         uint32_t addr1;
271         uint32_t addr2;
272         uint32_t addr_cfg;
273         uint32_t seq_opc[5];
274         uint32_t mode;
275         uint32_t dummy;
276         uint32_t status;
277         uint8_t  seq[16];
278         uint32_t seq_cfg;
279 } __packed __aligned(4);
280
281 /* Parameters to configure a READ or WRITE FSM sequence */
282 struct seq_rw_config {
283         uint32_t        flags;          /* flags to support config */
284         uint8_t         cmd;            /* FLASH command */
285         int             write;          /* Write Sequence */
286         uint8_t         addr_pads;      /* No. of addr pads (MODE & DUMMY) */
287         uint8_t         data_pads;      /* No. of data pads */
288         uint8_t         mode_data;      /* MODE data */
289         uint8_t         mode_cycles;    /* No. of MODE cycles */
290         uint8_t         dummy_cycles;   /* No. of DUMMY cycles */
291 };
292
293 /* SPI Flash Device Table */
294 struct flash_info {
295         char            *name;
296         /*
297          * JEDEC id zero means "no ID" (most older chips); otherwise it has
298          * a high byte of zero plus three data bytes: the manufacturer id,
299          * then a two byte device id.
300          */
301         u32             jedec_id;
302         u16             ext_id;
303         /*
304          * The size listed here is what works with FLASH_CMD_SE, which isn't
305          * necessarily called a "sector" by the vendor.
306          */
307         unsigned        sector_size;
308         u16             n_sectors;
309         u32             flags;
310         /*
311          * Note, where FAST_READ is supported, freq_max specifies the
312          * FAST_READ frequency, not the READ frequency.
313          */
314         u32             max_freq;
315         int             (*config)(struct stfsm *);
316 };
317
318 static int stfsm_n25q_config(struct stfsm *fsm);
319
320 static struct flash_info flash_types[] = {
321         /*
322          * ST Microelectronics/Numonyx --
323          * (newer production versions may have feature updates
324          * (eg faster operating frequency)
325          */
326 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
327         { "m25p40",  0x202013, 0,  64 * 1024,   8, M25P_FLAG, 25, NULL },
328         { "m25p80",  0x202014, 0,  64 * 1024,  16, M25P_FLAG, 25, NULL },
329         { "m25p16",  0x202015, 0,  64 * 1024,  32, M25P_FLAG, 25, NULL },
330         { "m25p32",  0x202016, 0,  64 * 1024,  64, M25P_FLAG, 50, NULL },
331         { "m25p64",  0x202017, 0,  64 * 1024, 128, M25P_FLAG, 50, NULL },
332         { "m25p128", 0x202018, 0, 256 * 1024,  64, M25P_FLAG, 50, NULL },
333
334 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE      |        \
335                     FLASH_FLAG_READ_FAST        |       \
336                     FLASH_FLAG_READ_1_1_2       |       \
337                     FLASH_FLAG_WRITE_1_1_2)
338         { "m25px32", 0x207116, 0,  64 * 1024,  64, M25PX_FLAG, 75, NULL },
339         { "m25px64", 0x207117, 0,  64 * 1024, 128, M25PX_FLAG, 75, NULL },
340
341 #define MX25_FLAG (FLASH_FLAG_READ_WRITE       |        \
342                    FLASH_FLAG_READ_FAST         |       \
343                    FLASH_FLAG_READ_1_1_2        |       \
344                    FLASH_FLAG_READ_1_2_2        |       \
345                    FLASH_FLAG_READ_1_1_4        |       \
346                    FLASH_FLAG_READ_1_4_4        |       \
347                    FLASH_FLAG_SE_4K             |       \
348                    FLASH_FLAG_SE_32K)
349         { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
350           (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
351
352 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE       |        \
353                    FLASH_FLAG_READ_FAST         |       \
354                    FLASH_FLAG_READ_1_1_2        |       \
355                    FLASH_FLAG_READ_1_2_2        |       \
356                    FLASH_FLAG_READ_1_1_4        |       \
357                    FLASH_FLAG_READ_1_4_4        |       \
358                    FLASH_FLAG_WRITE_1_1_2       |       \
359                    FLASH_FLAG_WRITE_1_2_2       |       \
360                    FLASH_FLAG_WRITE_1_1_4       |       \
361                    FLASH_FLAG_WRITE_1_4_4)
362         { "n25q128", 0x20ba18, 0, 64 * 1024,  256, N25Q_FLAG, 108,
363           stfsm_n25q_config },
364         { "n25q256", 0x20ba19, 0, 64 * 1024,  512,
365           N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
366
367         /*
368          * Spansion S25FLxxxP
369          *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
370          */
371 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE  |        \
372                         FLASH_FLAG_READ_1_1_2   |       \
373                         FLASH_FLAG_READ_1_2_2   |       \
374                         FLASH_FLAG_READ_1_1_4   |       \
375                         FLASH_FLAG_READ_1_4_4   |       \
376                         FLASH_FLAG_WRITE_1_1_4  |       \
377                         FLASH_FLAG_READ_FAST)
378         { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024,  64, S25FLXXXP_FLAG, 80,
379           NULL },
380         { "s25fl129p1", 0x012018, 0x4d01,  64 * 1024, 256, S25FLXXXP_FLAG, 80,
381           NULL },
382
383         /*
384          * Spansion S25FLxxxS
385          *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
386          *     - RESET# signal supported by die but not bristled out on all
387          *       package types.  The package type is a function of board design,
388          *       so this information is captured in the board's flags.
389          *     - Supports 'DYB' sector protection. Depending on variant, sectors
390          *       may default to locked state on power-on.
391          */
392 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG         |        \
393                         FLASH_FLAG_RESET        |       \
394                         FLASH_FLAG_DYB_LOCKING)
395         { "s25fl128s0", 0x012018, 0x0300,  256 * 1024, 64, S25FLXXXS_FLAG, 80,
396           NULL },
397         { "s25fl128s1", 0x012018, 0x0301,  64 * 1024, 256, S25FLXXXS_FLAG, 80,
398           NULL },
399         { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
400           S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
401         { "s25fl256s1", 0x010219, 0x4d01,  64 * 1024, 512,
402           S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
403
404         /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
405 #define W25X_FLAG (FLASH_FLAG_READ_WRITE       |        \
406                    FLASH_FLAG_READ_FAST         |       \
407                    FLASH_FLAG_READ_1_1_2        |       \
408                    FLASH_FLAG_WRITE_1_1_2)
409         { "w25x40",  0xef3013, 0,  64 * 1024,   8, W25X_FLAG, 75, NULL },
410         { "w25x80",  0xef3014, 0,  64 * 1024,  16, W25X_FLAG, 75, NULL },
411         { "w25x16",  0xef3015, 0,  64 * 1024,  32, W25X_FLAG, 75, NULL },
412         { "w25x32",  0xef3016, 0,  64 * 1024,  64, W25X_FLAG, 75, NULL },
413         { "w25x64",  0xef3017, 0,  64 * 1024, 128, W25X_FLAG, 75, NULL },
414
415         /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
416 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE       |        \
417                    FLASH_FLAG_READ_FAST         |       \
418                    FLASH_FLAG_READ_1_1_2        |       \
419                    FLASH_FLAG_READ_1_2_2        |       \
420                    FLASH_FLAG_READ_1_1_4        |       \
421                    FLASH_FLAG_READ_1_4_4        |       \
422                    FLASH_FLAG_WRITE_1_1_4)
423         { "w25q80",  0xef4014, 0,  64 * 1024,  16, W25Q_FLAG, 80, NULL },
424         { "w25q16",  0xef4015, 0,  64 * 1024,  32, W25Q_FLAG, 80, NULL },
425         { "w25q32",  0xef4016, 0,  64 * 1024,  64, W25Q_FLAG, 80, NULL },
426         { "w25q64",  0xef4017, 0,  64 * 1024, 128, W25Q_FLAG, 80, NULL },
427
428         /* Sentinel */
429         { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
430 };
431
432 /*
433  * FSM message sequence configurations:
434  *
435  * All configs are presented in order of preference
436  */
437
438 /* Default READ configurations, in order of preference */
439 static struct seq_rw_config default_read_configs[] = {
440         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4,   0, 4, 4, 0x00, 2, 4},
441         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4,   0, 1, 4, 0x00, 4, 0},
442         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2,   0, 2, 2, 0x00, 4, 0},
443         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2,   0, 1, 2, 0x00, 0, 8},
444         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ_FAST,    0, 1, 1, 0x00, 0, 8},
445         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ,         0, 1, 1, 0x00, 0, 0},
446         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
447 };
448
449 /* Default WRITE configurations */
450 static struct seq_rw_config default_write_configs[] = {
451         {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
452         {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
453         {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
454         {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
455         {FLASH_FLAG_READ_WRITE,  FLASH_CMD_WRITE,       1, 1, 1, 0x00, 0, 0},
456         {0x00,                   0,                     0, 0, 0, 0x00, 0, 0},
457 };
458
459 /*
460  * [N25Qxxx] Configuration
461  */
462 #define N25Q_VCR_DUMMY_CYCLES(x)        (((x) & 0xf) << 4)
463 #define N25Q_VCR_XIP_DISABLED           ((uint8_t)0x1 << 3)
464 #define N25Q_VCR_WRAP_CONT              0x3
465
466 /* N25Q 3-byte Address READ configurations
467  *      - 'FAST' variants configured for 8 dummy cycles.
468  *
469  * Note, the number of dummy cycles used for 'FAST' READ operations is
470  * configurable and would normally be tuned according to the READ command and
471  * operating frequency.  However, this applies universally to all 'FAST' READ
472  * commands, including those used by the SPIBoot controller, and remains in
473  * force until the device is power-cycled.  Since the SPIBoot controller is
474  * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
475  * cycles.
476  */
477 static struct seq_rw_config n25q_read3_configs[] = {
478         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4,   0, 4, 4, 0x00, 0, 8},
479         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4,   0, 1, 4, 0x00, 0, 8},
480         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2,   0, 2, 2, 0x00, 0, 8},
481         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2,   0, 1, 2, 0x00, 0, 8},
482         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ_FAST,    0, 1, 1, 0x00, 0, 8},
483         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ,         0, 1, 1, 0x00, 0, 0},
484         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
485 };
486
487 /* N25Q 4-byte Address READ configurations
488  *      - use special 4-byte address READ commands (reduces overheads, and
489  *        reduces risk of hitting watchdog reset issues).
490  *      - 'FAST' variants configured for 8 dummy cycles (see note above.)
491  */
492 static struct seq_rw_config n25q_read4_configs[] = {
493         {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4,  0, 4, 4, 0x00, 0, 8},
494         {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4,  0, 1, 4, 0x00, 0, 8},
495         {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2,  0, 2, 2, 0x00, 0, 8},
496         {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2,  0, 1, 2, 0x00, 0, 8},
497         {FLASH_FLAG_READ_FAST,  FLASH_CMD_READ4_FAST,   0, 1, 1, 0x00, 0, 8},
498         {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4,        0, 1, 1, 0x00, 0, 0},
499         {0x00,                  0,                      0, 0, 0, 0x00, 0, 0},
500 };
501
502 static struct stfsm_seq stfsm_seq_read;         /* Dynamically populated */
503 static struct stfsm_seq stfsm_seq_write;        /* Dynamically populated */
504 static struct stfsm_seq stfsm_seq_en_32bit_addr;/* Dynamically populated */
505
506 static struct stfsm_seq stfsm_seq_read_jedec = {
507         .data_size = TRANSFER_SIZE(8),
508         .seq_opc[0] = (SEQ_OPC_PADS_1 |
509                        SEQ_OPC_CYCLES(8) |
510                        SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
511         .seq = {
512                 STFSM_INST_CMD1,
513                 STFSM_INST_DATA_READ,
514                 STFSM_INST_STOP,
515         },
516         .seq_cfg = (SEQ_CFG_PADS_1 |
517                     SEQ_CFG_READNOTWRITE |
518                     SEQ_CFG_CSDEASSERT |
519                     SEQ_CFG_STARTSEQ),
520 };
521
522 static struct stfsm_seq stfsm_seq_erase_sector = {
523         /* 'addr_cfg' configured during initialisation */
524         .seq_opc = {
525                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
526                  SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
527
528                 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
529                  SEQ_OPC_OPCODE(FLASH_CMD_SE)),
530         },
531         .seq = {
532                 STFSM_INST_CMD1,
533                 STFSM_INST_CMD2,
534                 STFSM_INST_ADD1,
535                 STFSM_INST_ADD2,
536                 STFSM_INST_STOP,
537         },
538         .seq_cfg = (SEQ_CFG_PADS_1 |
539                     SEQ_CFG_READNOTWRITE |
540                     SEQ_CFG_CSDEASSERT |
541                     SEQ_CFG_STARTSEQ),
542 };
543
544 static struct stfsm_seq stfsm_seq_wrvcr = {
545         .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
546                        SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
547         .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
548                        SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
549         .seq = {
550                 STFSM_INST_CMD1,
551                 STFSM_INST_CMD2,
552                 STFSM_INST_STA_WR1,
553                 STFSM_INST_STOP,
554         },
555         .seq_cfg = (SEQ_CFG_PADS_1 |
556                     SEQ_CFG_READNOTWRITE |
557                     SEQ_CFG_CSDEASSERT |
558                     SEQ_CFG_STARTSEQ),
559 };
560
561 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
562 {
563         seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
564                            SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
565         seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
566                            SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
567                            SEQ_OPC_CSDEASSERT);
568
569         seq->seq[0] = STFSM_INST_CMD2;
570         seq->seq[1] = STFSM_INST_CMD1;
571         seq->seq[2] = STFSM_INST_WAIT;
572         seq->seq[3] = STFSM_INST_STOP;
573
574         seq->seq_cfg = (SEQ_CFG_PADS_1 |
575                         SEQ_CFG_ERASE |
576                         SEQ_CFG_READNOTWRITE |
577                         SEQ_CFG_CSDEASSERT |
578                         SEQ_CFG_STARTSEQ);
579
580         return 0;
581 }
582
583 static inline int stfsm_is_idle(struct stfsm *fsm)
584 {
585         return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
586 }
587
588 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
589 {
590         return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
591 }
592
593 static void stfsm_clear_fifo(struct stfsm *fsm)
594 {
595         uint32_t avail;
596
597         for (;;) {
598                 avail = stfsm_fifo_available(fsm);
599                 if (!avail)
600                         break;
601
602                 while (avail) {
603                         readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
604                         avail--;
605                 }
606         }
607 }
608
609 static inline void stfsm_load_seq(struct stfsm *fsm,
610                                   const struct stfsm_seq *seq)
611 {
612         void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
613         const uint32_t *src = (const uint32_t *)seq;
614         int words = sizeof(*seq) / sizeof(*src);
615
616         BUG_ON(!stfsm_is_idle(fsm));
617
618         while (words--) {
619                 writel(*src, dst);
620                 src++;
621                 dst += 4;
622         }
623 }
624
625 static void stfsm_wait_seq(struct stfsm *fsm)
626 {
627         unsigned long deadline;
628         int timeout = 0;
629
630         deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
631
632         while (!timeout) {
633                 if (time_after_eq(jiffies, deadline))
634                         timeout = 1;
635
636                 if (stfsm_is_idle(fsm))
637                         return;
638
639                 cond_resched();
640         }
641
642         dev_err(fsm->dev, "timeout on sequence completion\n");
643 }
644
645 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
646                             const uint32_t size)
647 {
648         uint32_t remaining = size >> 2;
649         uint32_t avail;
650         uint32_t words;
651
652         dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
653
654         BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
655
656         while (remaining) {
657                 for (;;) {
658                         avail = stfsm_fifo_available(fsm);
659                         if (avail)
660                                 break;
661                         udelay(1);
662                 }
663                 words = min(avail, remaining);
664                 remaining -= words;
665
666                 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
667                 buf += words;
668         }
669 }
670
671 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
672 {
673         struct stfsm_seq *seq = &stfsm_seq_en_32bit_addr;
674         uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
675
676         seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
677                            SEQ_OPC_CYCLES(8) |
678                            SEQ_OPC_OPCODE(cmd) |
679                            SEQ_OPC_CSDEASSERT);
680
681         stfsm_load_seq(fsm, seq);
682
683         stfsm_wait_seq(fsm);
684
685         return 0;
686 }
687
688 static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
689 {
690         struct stfsm_seq *seq = &stfsm_seq_wrvcr;
691
692         dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
693
694         seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
695
696         stfsm_load_seq(fsm, seq);
697
698         stfsm_wait_seq(fsm);
699
700         return 0;
701 }
702
703 /*
704  * SoC reset on 'boot-from-spi' systems
705  *
706  * Certain modes of operation cause the Flash device to enter a particular state
707  * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
708  * Addr' commands).  On boot-from-spi systems, it is important to consider what
709  * happens if a warm reset occurs during this period.  The SPIBoot controller
710  * assumes that Flash device is in its default reset state, 24-bit address mode,
711  * and ready to accept commands.  This can be achieved using some form of
712  * on-board logic/controller to force a device POR in response to a SoC-level
713  * reset or by making use of the device reset signal if available (limited
714  * number of devices only).
715  *
716  * Failure to take such precautions can cause problems following a warm reset.
717  * For some operations (e.g. ERASE), there is little that can be done.  For
718  * other modes of operation (e.g. 32-bit addressing), options are often
719  * available that can help minimise the window in which a reset could cause a
720  * problem.
721  *
722  */
723 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
724 {
725         /* Reset signal is available on the board and supported by the device */
726         if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
727                 return true;
728
729         /* Board-level logic forces a power-on-reset */
730         if (fsm->reset_por)
731                 return true;
732
733         /* Reset is not properly handled and may result in failure to reboot */
734         return false;
735 }
736
737 /* Configure 'addr_cfg' according to addressing mode */
738 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
739                                        struct stfsm_seq *seq)
740 {
741         int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
742
743         seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
744                          ADR_CFG_PADS_1_ADD1 |
745                          ADR_CFG_CYCLES_ADD2(16) |
746                          ADR_CFG_PADS_1_ADD2 |
747                          ADR_CFG_CSDEASSERT_ADD2);
748 }
749
750 /* Search for preferred configuration based on available flags */
751 static struct seq_rw_config *
752 stfsm_search_seq_rw_configs(struct stfsm *fsm,
753                             struct seq_rw_config cfgs[])
754 {
755         struct seq_rw_config *config;
756         int flags = fsm->info->flags;
757
758         for (config = cfgs; config->cmd != 0; config++)
759                 if ((config->flags & flags) == config->flags)
760                         return config;
761
762         return NULL;
763 }
764
765 /* Prepare a READ/WRITE sequence according to configuration parameters */
766 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
767                                  struct stfsm_seq *seq,
768                                  struct seq_rw_config *cfg)
769 {
770         int addr1_cycles, addr2_cycles;
771         int i = 0;
772
773         memset(seq, 0, sizeof(*seq));
774
775         /* Add READ/WRITE OPC  */
776         seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
777                              SEQ_OPC_CYCLES(8) |
778                              SEQ_OPC_OPCODE(cfg->cmd));
779
780         /* Add WREN OPC for a WRITE sequence */
781         if (cfg->write)
782                 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
783                                      SEQ_OPC_CYCLES(8) |
784                                      SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
785                                      SEQ_OPC_CSDEASSERT);
786
787         /* Address configuration (24 or 32-bit addresses) */
788         addr1_cycles  = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
789         addr1_cycles /= cfg->addr_pads;
790         addr2_cycles  = 16 / cfg->addr_pads;
791         seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 |   /* ADD1 cycles */
792                          (cfg->addr_pads - 1) << 6 |    /* ADD1 pads */
793                          (addr2_cycles & 0x3f) << 16 |  /* ADD2 cycles */
794                          ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
795
796         /* Data/Sequence configuration */
797         seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
798                         SEQ_CFG_STARTSEQ |
799                         SEQ_CFG_CSDEASSERT);
800         if (!cfg->write)
801                 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
802
803         /* Mode configuration (no. of pads taken from addr cfg) */
804         seq->mode = ((cfg->mode_data & 0xff) << 0 |     /* data */
805                      (cfg->mode_cycles & 0x3f) << 16 |  /* cycles */
806                      (cfg->addr_pads - 1) << 22);       /* pads */
807
808         /* Dummy configuration (no. of pads taken from addr cfg) */
809         seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 |        /* cycles */
810                       (cfg->addr_pads - 1) << 22);              /* pads */
811
812
813         /* Instruction sequence */
814         i = 0;
815         if (cfg->write)
816                 seq->seq[i++] = STFSM_INST_CMD2;
817
818         seq->seq[i++] = STFSM_INST_CMD1;
819
820         seq->seq[i++] = STFSM_INST_ADD1;
821         seq->seq[i++] = STFSM_INST_ADD2;
822
823         if (cfg->mode_cycles)
824                 seq->seq[i++] = STFSM_INST_MODE;
825
826         if (cfg->dummy_cycles)
827                 seq->seq[i++] = STFSM_INST_DUMMY;
828
829         seq->seq[i++] =
830                 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
831         seq->seq[i++] = STFSM_INST_STOP;
832 }
833
834 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
835                                        struct stfsm_seq *seq,
836                                        struct seq_rw_config *cfgs)
837 {
838         struct seq_rw_config *config;
839
840         config = stfsm_search_seq_rw_configs(fsm, cfgs);
841         if (!config) {
842                 dev_err(fsm->dev, "failed to find suitable config\n");
843                 return -EINVAL;
844         }
845
846         stfsm_prepare_rw_seq(fsm, seq, config);
847
848         return 0;
849 }
850
851 /* Prepare a READ/WRITE/ERASE 'default' sequences */
852 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
853 {
854         uint32_t flags = fsm->info->flags;
855         int ret;
856
857         /* Configure 'READ' sequence */
858         ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
859                                           default_read_configs);
860         if (ret) {
861                 dev_err(fsm->dev,
862                         "failed to prep READ sequence with flags [0x%08x]\n",
863                         flags);
864                 return ret;
865         }
866
867         /* Configure 'WRITE' sequence */
868         ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
869                                           default_write_configs);
870         if (ret) {
871                 dev_err(fsm->dev,
872                         "failed to prep WRITE sequence with flags [0x%08x]\n",
873                         flags);
874                 return ret;
875         }
876
877         /* Configure 'ERASE_SECTOR' sequence */
878         stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
879
880         return 0;
881 }
882
883 static int stfsm_n25q_config(struct stfsm *fsm)
884 {
885         uint32_t flags = fsm->info->flags;
886         uint8_t vcr;
887         int ret = 0;
888         bool soc_reset;
889
890         /* Configure 'READ' sequence */
891         if (flags & FLASH_FLAG_32BIT_ADDR)
892                 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
893                                                   n25q_read4_configs);
894         else
895                 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
896                                                   n25q_read3_configs);
897         if (ret) {
898                 dev_err(fsm->dev,
899                         "failed to prepare READ sequence with flags [0x%08x]\n",
900                         flags);
901                 return ret;
902         }
903
904         /* Configure 'WRITE' sequence (default configs) */
905         ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
906                                           default_write_configs);
907         if (ret) {
908                 dev_err(fsm->dev,
909                         "preparing WRITE sequence using flags [0x%08x] failed\n",
910                         flags);
911                 return ret;
912         }
913
914         /* * Configure 'ERASE_SECTOR' sequence */
915         stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
916
917         /* Configure 32-bit address support */
918         if (flags & FLASH_FLAG_32BIT_ADDR) {
919                 stfsm_n25q_en_32bit_addr_seq(&stfsm_seq_en_32bit_addr);
920
921                 soc_reset = stfsm_can_handle_soc_reset(fsm);
922                 if (soc_reset || !fsm->booted_from_spi) {
923                         /*
924                          * If we can handle SoC resets, we enable 32-bit
925                          * address mode pervasively
926                          */
927                         stfsm_enter_32bit_addr(fsm, 1);
928                 } else {
929                         /*
930                          * If not, enable/disable for WRITE and ERASE
931                          * operations (READ uses special commands)
932                          */
933                         fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
934                                               CFG_ERASESEC_TOGGLE_32BIT_ADDR);
935                 }
936         }
937
938         /*
939          * Configure device to use 8 dummy cycles
940          */
941         vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
942                N25Q_VCR_WRAP_CONT);
943         stfsm_wrvcr(fsm, vcr);
944
945         return 0;
946 }
947
948 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
949                       uint32_t offset)
950 {
951         struct stfsm_seq *seq = &stfsm_seq_read;
952         uint32_t data_pads;
953         uint32_t read_mask;
954         uint32_t size_ub;
955         uint32_t size_lb;
956         uint32_t size_mop;
957         uint32_t tmp[4];
958         uint32_t page_buf[FLASH_PAGESIZE_32];
959         uint8_t *p;
960
961         dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
962
963         /* Enter 32-bit address mode, if required */
964         if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
965                 stfsm_enter_32bit_addr(fsm, 1);
966
967         /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
968         data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
969         read_mask = (data_pads << 2) - 1;
970
971         /* Handle non-aligned buf */
972         p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
973
974         /* Handle non-aligned size */
975         size_ub = (size + read_mask) & ~read_mask;
976         size_lb = size & ~read_mask;
977         size_mop = size & read_mask;
978
979         seq->data_size = TRANSFER_SIZE(size_ub);
980         seq->addr1 = (offset >> 16) & 0xffff;
981         seq->addr2 = offset & 0xffff;
982
983         stfsm_load_seq(fsm, seq);
984
985         if (size_lb)
986                 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
987
988         if (size_mop) {
989                 stfsm_read_fifo(fsm, tmp, read_mask + 1);
990                 memcpy(p + size_lb, &tmp, size_mop);
991         }
992
993         /* Handle non-aligned buf */
994         if ((uint32_t)buf & 0x3)
995                 memcpy(buf, page_buf, size);
996
997         /* Wait for sequence to finish */
998         stfsm_wait_seq(fsm);
999
1000         stfsm_clear_fifo(fsm);
1001
1002         /* Exit 32-bit address mode, if required */
1003         if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1004                 stfsm_enter_32bit_addr(fsm, 0);
1005
1006         return 0;
1007 }
1008
1009 /*
1010  * Read an address range from the flash chip. The address range
1011  * may be any size provided it is within the physical boundaries.
1012  */
1013 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1014                           size_t *retlen, u_char *buf)
1015 {
1016         struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1017         uint32_t bytes;
1018
1019         dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1020                 __func__, (u32)from, len);
1021
1022         mutex_lock(&fsm->lock);
1023
1024         while (len > 0) {
1025                 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1026
1027                 stfsm_read(fsm, buf, bytes, from);
1028
1029                 buf += bytes;
1030                 from += bytes;
1031                 len -= bytes;
1032
1033                 *retlen += bytes;
1034         }
1035
1036         mutex_unlock(&fsm->lock);
1037
1038         return 0;
1039 }
1040
1041 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
1042 {
1043         const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1044         uint32_t tmp[2];
1045
1046         stfsm_load_seq(fsm, seq);
1047
1048         stfsm_read_fifo(fsm, tmp, 8);
1049
1050         memcpy(jedec, tmp, 5);
1051
1052         stfsm_wait_seq(fsm);
1053 }
1054
1055 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1056 {
1057         struct flash_info       *info;
1058         u16                     ext_jedec;
1059         u32                     jedec;
1060         u8                      id[5];
1061
1062         stfsm_read_jedec(fsm, id);
1063
1064         jedec     = id[0] << 16 | id[1] << 8 | id[2];
1065         /*
1066          * JEDEC also defines an optional "extended device information"
1067          * string for after vendor-specific data, after the three bytes
1068          * we use here. Supporting some chips might require using it.
1069          */
1070         ext_jedec = id[3] << 8  | id[4];
1071
1072         dev_dbg(fsm->dev, "JEDEC =  0x%08x [%02x %02x %02x %02x %02x]\n",
1073                 jedec, id[0], id[1], id[2], id[3], id[4]);
1074
1075         for (info = flash_types; info->name; info++) {
1076                 if (info->jedec_id == jedec) {
1077                         if (info->ext_id && info->ext_id != ext_jedec)
1078                                 continue;
1079                         return info;
1080                 }
1081         }
1082         dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1083
1084         return NULL;
1085 }
1086
1087 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1088 {
1089         int ret, timeout = 10;
1090
1091         /* Wait for controller to accept mode change */
1092         while (--timeout) {
1093                 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1094                 if (ret & 0x1)
1095                         break;
1096                 udelay(1);
1097         }
1098
1099         if (!timeout)
1100                 return -EBUSY;
1101
1102         writel(mode, fsm->base + SPI_MODESELECT);
1103
1104         return 0;
1105 }
1106
1107 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1108 {
1109         uint32_t emi_freq;
1110         uint32_t clk_div;
1111
1112         /* TODO: Make this dynamic */
1113         emi_freq = STFSM_DEFAULT_EMI_FREQ;
1114
1115         /*
1116          * Calculate clk_div - values between 2 and 128
1117          * Multiple of 2, rounded up
1118          */
1119         clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1120         if (clk_div < 2)
1121                 clk_div = 2;
1122         else if (clk_div > 128)
1123                 clk_div = 128;
1124
1125         /*
1126          * Determine a suitable delay for the IP to complete a change of
1127          * direction of the FIFO. The required delay is related to the clock
1128          * divider used. The following heuristics are based on empirical tests,
1129          * using a 100MHz EMI clock.
1130          */
1131         if (clk_div <= 4)
1132                 fsm->fifo_dir_delay = 0;
1133         else if (clk_div <= 10)
1134                 fsm->fifo_dir_delay = 1;
1135         else
1136                 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1137
1138         dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1139                 emi_freq, spi_freq, clk_div);
1140
1141         writel(clk_div, fsm->base + SPI_CLOCKDIV);
1142 }
1143
1144 static int stfsm_init(struct stfsm *fsm)
1145 {
1146         int ret;
1147
1148         /* Perform a soft reset of the FSM controller */
1149         writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1150         udelay(1);
1151         writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1152
1153         /* Set clock to 'safe' frequency initially */
1154         stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1155
1156         /* Switch to FSM */
1157         ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1158         if (ret)
1159                 return ret;
1160
1161         /* Set timing parameters */
1162         writel(SPI_CFG_DEVICE_ST            |
1163                SPI_CFG_DEFAULT_MIN_CS_HIGH  |
1164                SPI_CFG_DEFAULT_CS_SETUPHOLD |
1165                SPI_CFG_DEFAULT_DATA_HOLD,
1166                fsm->base + SPI_CONFIGDATA);
1167         writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1168
1169         /* Clear FIFO, just in case */
1170         stfsm_clear_fifo(fsm);
1171
1172         return 0;
1173 }
1174
1175 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1176 {
1177         struct stfsm *fsm = platform_get_drvdata(pdev);
1178         struct device_node *np = pdev->dev.of_node;
1179         struct regmap *regmap;
1180         uint32_t boot_device_reg;
1181         uint32_t boot_device_spi;
1182         uint32_t boot_device;     /* Value we read from *boot_device_reg */
1183         int ret;
1184
1185         /* Booting from SPI NOR Flash is the default */
1186         fsm->booted_from_spi = true;
1187
1188         regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1189         if (IS_ERR(regmap))
1190                 goto boot_device_fail;
1191
1192         fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1193
1194         fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1195
1196         /* Where in the syscon the boot device information lives */
1197         ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1198         if (ret)
1199                 goto boot_device_fail;
1200
1201         /* Boot device value when booted from SPI NOR */
1202         ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1203         if (ret)
1204                 goto boot_device_fail;
1205
1206         ret = regmap_read(regmap, boot_device_reg, &boot_device);
1207         if (ret)
1208                 goto boot_device_fail;
1209
1210         if (boot_device != boot_device_spi)
1211                 fsm->booted_from_spi = false;
1212
1213         return;
1214
1215 boot_device_fail:
1216         dev_warn(&pdev->dev,
1217                  "failed to fetch boot device, assuming boot from SPI\n");
1218 }
1219
1220 static int stfsm_probe(struct platform_device *pdev)
1221 {
1222         struct device_node *np = pdev->dev.of_node;
1223         struct flash_info *info;
1224         struct resource *res;
1225         struct stfsm *fsm;
1226         int ret;
1227
1228         if (!np) {
1229                 dev_err(&pdev->dev, "No DT found\n");
1230                 return -EINVAL;
1231         }
1232
1233         fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1234         if (!fsm)
1235                 return -ENOMEM;
1236
1237         fsm->dev = &pdev->dev;
1238
1239         platform_set_drvdata(pdev, fsm);
1240
1241         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1242         if (!res) {
1243                 dev_err(&pdev->dev, "Resource not found\n");
1244                 return -ENODEV;
1245         }
1246
1247         fsm->base = devm_ioremap_resource(&pdev->dev, res);
1248         if (IS_ERR(fsm->base)) {
1249                 dev_err(&pdev->dev,
1250                         "Failed to reserve memory region %pR\n", res);
1251                 return PTR_ERR(fsm->base);
1252         }
1253
1254         mutex_init(&fsm->lock);
1255
1256         ret = stfsm_init(fsm);
1257         if (ret) {
1258                 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
1259                 return ret;
1260         }
1261
1262         stfsm_fetch_platform_configs(pdev);
1263
1264         /* Detect SPI FLASH device */
1265         info = stfsm_jedec_probe(fsm);
1266         if (!info)
1267                 return -ENODEV;
1268         fsm->info = info;
1269
1270         /* Use device size to determine address width */
1271         if (info->sector_size * info->n_sectors > 0x1000000)
1272                 info->flags |= FLASH_FLAG_32BIT_ADDR;
1273
1274         /*
1275          * Configure READ/WRITE/ERASE sequences according to platform and
1276          * device flags.
1277          */
1278         if (info->config) {
1279                 ret = info->config(fsm);
1280                 if (ret)
1281                         return ret;
1282         } else {
1283                 ret = stfsm_prepare_rwe_seqs_default(fsm);
1284                 if (ret)
1285                         return ret;
1286         }
1287
1288         fsm->mtd.dev.parent     = &pdev->dev;
1289         fsm->mtd.type           = MTD_NORFLASH;
1290         fsm->mtd.writesize      = 4;
1291         fsm->mtd.writebufsize   = fsm->mtd.writesize;
1292         fsm->mtd.flags          = MTD_CAP_NORFLASH;
1293         fsm->mtd.size           = info->sector_size * info->n_sectors;
1294         fsm->mtd.erasesize      = info->sector_size;
1295
1296         fsm->mtd._read  = stfsm_mtd_read;
1297
1298         dev_err(&pdev->dev,
1299                 "Found serial flash device: %s\n"
1300                 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
1301                 info->name,
1302                 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
1303                 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
1304
1305         return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
1306 }
1307
1308 static int stfsm_remove(struct platform_device *pdev)
1309 {
1310         struct stfsm *fsm = platform_get_drvdata(pdev);
1311         int err;
1312
1313         err = mtd_device_unregister(&fsm->mtd);
1314         if (err)
1315                 return err;
1316
1317         return 0;
1318 }
1319
1320 static struct of_device_id stfsm_match[] = {
1321         { .compatible = "st,spi-fsm", },
1322         {},
1323 };
1324 MODULE_DEVICE_TABLE(of, stfsm_match);
1325
1326 static struct platform_driver stfsm_driver = {
1327         .probe          = stfsm_probe,
1328         .remove         = stfsm_remove,
1329         .driver         = {
1330                 .name   = "st-spi-fsm",
1331                 .owner  = THIS_MODULE,
1332                 .of_match_table = stfsm_match,
1333         },
1334 };
1335 module_platform_driver(stfsm_driver);
1336
1337 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
1338 MODULE_DESCRIPTION("ST SPI FSM driver");
1339 MODULE_LICENSE("GPL");