mtd: st_spi_fsm: begin using spi-nor.h opcodes
[linux/fpc-iii.git] / drivers / mtd / devices / st_spi_fsm.c
blob7cc49ba78f6881978dae659ef4dcd9c6cc4545aa
1 /*
2 * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
4 * Author: Angus Clark <angus.clark@st.com>
6 * Copyright (C) 2010-2014 STMicroelectronics Limited
8 * JEDEC probe based on drivers/mtd/devices/m25p80.c
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.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/regmap.h>
18 #include <linux/platform_device.h>
19 #include <linux/mfd/syscon.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/spi-nor.h>
23 #include <linux/sched.h>
24 #include <linux/delay.h>
25 #include <linux/io.h>
26 #include <linux/of.h>
28 #include "serial_flash_cmds.h"
31 * FSM SPI Controller Registers
33 #define SPI_CLOCKDIV 0x0010
34 #define SPI_MODESELECT 0x0018
35 #define SPI_CONFIGDATA 0x0020
36 #define SPI_STA_MODE_CHANGE 0x0028
37 #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
38 #define SPI_FAST_SEQ_ADD1 0x0104
39 #define SPI_FAST_SEQ_ADD2 0x0108
40 #define SPI_FAST_SEQ_ADD_CFG 0x010c
41 #define SPI_FAST_SEQ_OPC1 0x0110
42 #define SPI_FAST_SEQ_OPC2 0x0114
43 #define SPI_FAST_SEQ_OPC3 0x0118
44 #define SPI_FAST_SEQ_OPC4 0x011c
45 #define SPI_FAST_SEQ_OPC5 0x0120
46 #define SPI_MODE_BITS 0x0124
47 #define SPI_DUMMY_BITS 0x0128
48 #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
49 #define SPI_FAST_SEQ_1 0x0130
50 #define SPI_FAST_SEQ_2 0x0134
51 #define SPI_FAST_SEQ_3 0x0138
52 #define SPI_FAST_SEQ_4 0x013c
53 #define SPI_FAST_SEQ_CFG 0x0140
54 #define SPI_FAST_SEQ_STA 0x0144
55 #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
56 #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
57 #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
58 #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
59 #define SPI_PROGRAM_ERASE_TIME 0x0158
60 #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
61 #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
62 #define SPI_STATUS_WR_TIME_REG 0x0164
63 #define SPI_FAST_SEQ_DATA_REG 0x0300
66 * Register: SPI_MODESELECT
68 #define SPI_MODESELECT_CONTIG 0x01
69 #define SPI_MODESELECT_FASTREAD 0x02
70 #define SPI_MODESELECT_DUALIO 0x04
71 #define SPI_MODESELECT_FSM 0x08
72 #define SPI_MODESELECT_QUADBOOT 0x10
75 * Register: SPI_CONFIGDATA
77 #define SPI_CFG_DEVICE_ST 0x1
78 #define SPI_CFG_DEVICE_ATMEL 0x4
79 #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
80 #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
81 #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
83 #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
84 #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
85 #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
88 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
90 #define TRANSFER_SIZE(x) ((x) * 8)
93 * Register: SPI_FAST_SEQ_ADD_CFG
95 #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
96 #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
97 #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
98 #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
99 #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
100 #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
101 #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
102 #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
103 #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
104 #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
107 * Register: SPI_FAST_SEQ_n
109 #define SEQ_OPC_OPCODE(x) ((x) << 0)
110 #define SEQ_OPC_CYCLES(x) ((x) << 8)
111 #define SEQ_OPC_PADS_1 (0x0 << 14)
112 #define SEQ_OPC_PADS_2 (0x1 << 14)
113 #define SEQ_OPC_PADS_4 (0x3 << 14)
114 #define SEQ_OPC_CSDEASSERT (1 << 16)
117 * Register: SPI_FAST_SEQ_CFG
119 #define SEQ_CFG_STARTSEQ (1 << 0)
120 #define SEQ_CFG_SWRESET (1 << 5)
121 #define SEQ_CFG_CSDEASSERT (1 << 6)
122 #define SEQ_CFG_READNOTWRITE (1 << 7)
123 #define SEQ_CFG_ERASE (1 << 8)
124 #define SEQ_CFG_PADS_1 (0x0 << 16)
125 #define SEQ_CFG_PADS_2 (0x1 << 16)
126 #define SEQ_CFG_PADS_4 (0x3 << 16)
129 * Register: SPI_MODE_BITS
131 #define MODE_DATA(x) (x & 0xff)
132 #define MODE_CYCLES(x) ((x & 0x3f) << 16)
133 #define MODE_PADS_1 (0x0 << 22)
134 #define MODE_PADS_2 (0x1 << 22)
135 #define MODE_PADS_4 (0x3 << 22)
136 #define DUMMY_CSDEASSERT (1 << 24)
139 * Register: SPI_DUMMY_BITS
141 #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
142 #define DUMMY_PADS_1 (0x0 << 22)
143 #define DUMMY_PADS_2 (0x1 << 22)
144 #define DUMMY_PADS_4 (0x3 << 22)
145 #define DUMMY_CSDEASSERT (1 << 24)
148 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
150 #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
151 #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
152 #define STA_PADS_1 (0x0 << 16)
153 #define STA_PADS_2 (0x1 << 16)
154 #define STA_PADS_4 (0x3 << 16)
155 #define STA_CSDEASSERT (0x1 << 20)
156 #define STA_RDNOTWR (0x1 << 21)
159 * FSM SPI Instruction Opcodes
161 #define STFSM_OPC_CMD 0x1
162 #define STFSM_OPC_ADD 0x2
163 #define STFSM_OPC_STA 0x3
164 #define STFSM_OPC_MODE 0x4
165 #define STFSM_OPC_DUMMY 0x5
166 #define STFSM_OPC_DATA 0x6
167 #define STFSM_OPC_WAIT 0x7
168 #define STFSM_OPC_JUMP 0x8
169 #define STFSM_OPC_GOTO 0x9
170 #define STFSM_OPC_STOP 0xF
173 * FSM SPI Instructions (== opcode + operand).
175 #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
177 #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
178 #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
179 #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
180 #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
181 #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
182 #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
183 #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
185 #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
186 #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
188 #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
189 #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
190 #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
191 #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
193 #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
194 #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
195 #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
196 #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
198 #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
199 #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
201 #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
203 #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
205 /* S25FLxxxS commands */
206 #define S25FL_CMD_WRITE4_1_1_4 0x34
207 #define S25FL_CMD_SE4 0xdc
208 #define S25FL_CMD_CLSR 0x30
209 #define S25FL_CMD_DYBWR 0xe1
210 #define S25FL_CMD_DYBRD 0xe0
211 #define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
212 * 'SPINOR_OP_WRITE_1_4_4'
213 * as found on N25Qxxx devices! */
215 /* Status register */
216 #define FLASH_STATUS_BUSY 0x01
217 #define FLASH_STATUS_WEL 0x02
218 #define FLASH_STATUS_BP0 0x04
219 #define FLASH_STATUS_BP1 0x08
220 #define FLASH_STATUS_BP2 0x10
221 #define FLASH_STATUS_SRWP0 0x80
222 #define FLASH_STATUS_TIMEOUT 0xff
223 /* S25FL Error Flags */
224 #define S25FL_STATUS_E_ERR 0x20
225 #define S25FL_STATUS_P_ERR 0x40
227 #define N25Q_CMD_WRVCR 0x81
228 #define N25Q_CMD_RDVCR 0x85
229 #define N25Q_CMD_RDVECR 0x65
230 #define N25Q_CMD_RDNVCR 0xb5
231 #define N25Q_CMD_WRNVCR 0xb1
233 #define FLASH_PAGESIZE 256 /* In Bytes */
234 #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
235 #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
238 * Flags to tweak operation of default read/write/erase routines
240 #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
241 #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
242 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
243 #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
245 struct stfsm_seq {
246 uint32_t data_size;
247 uint32_t addr1;
248 uint32_t addr2;
249 uint32_t addr_cfg;
250 uint32_t seq_opc[5];
251 uint32_t mode;
252 uint32_t dummy;
253 uint32_t status;
254 uint8_t seq[16];
255 uint32_t seq_cfg;
256 } __packed __aligned(4);
258 struct stfsm {
259 struct device *dev;
260 void __iomem *base;
261 struct resource *region;
262 struct mtd_info mtd;
263 struct mutex lock;
264 struct flash_info *info;
266 uint32_t configuration;
267 uint32_t fifo_dir_delay;
268 bool booted_from_spi;
269 bool reset_signal;
270 bool reset_por;
272 struct stfsm_seq stfsm_seq_read;
273 struct stfsm_seq stfsm_seq_write;
274 struct stfsm_seq stfsm_seq_en_32bit_addr;
277 /* Parameters to configure a READ or WRITE FSM sequence */
278 struct seq_rw_config {
279 uint32_t flags; /* flags to support config */
280 uint8_t cmd; /* FLASH command */
281 int write; /* Write Sequence */
282 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
283 uint8_t data_pads; /* No. of data pads */
284 uint8_t mode_data; /* MODE data */
285 uint8_t mode_cycles; /* No. of MODE cycles */
286 uint8_t dummy_cycles; /* No. of DUMMY cycles */
289 /* SPI Flash Device Table */
290 struct flash_info {
291 char *name;
293 * JEDEC id zero means "no ID" (most older chips); otherwise it has
294 * a high byte of zero plus three data bytes: the manufacturer id,
295 * then a two byte device id.
297 u32 jedec_id;
298 u16 ext_id;
300 * The size listed here is what works with SPINOR_OP_SE, which isn't
301 * necessarily called a "sector" by the vendor.
303 unsigned sector_size;
304 u16 n_sectors;
305 u32 flags;
307 * Note, where FAST_READ is supported, freq_max specifies the
308 * FAST_READ frequency, not the READ frequency.
310 u32 max_freq;
311 int (*config)(struct stfsm *);
314 static int stfsm_n25q_config(struct stfsm *fsm);
315 static int stfsm_mx25_config(struct stfsm *fsm);
316 static int stfsm_s25fl_config(struct stfsm *fsm);
317 static int stfsm_w25q_config(struct stfsm *fsm);
319 static struct flash_info flash_types[] = {
321 * ST Microelectronics/Numonyx --
322 * (newer production versions may have feature updates
323 * (eg faster operating frequency)
325 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
326 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
327 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
328 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
329 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
330 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
331 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
333 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
334 FLASH_FLAG_READ_FAST | \
335 FLASH_FLAG_READ_1_1_2 | \
336 FLASH_FLAG_WRITE_1_1_2)
337 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
338 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
340 /* Macronix MX25xxx
341 * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
342 * where operating frequency must be reduced.
344 #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
345 FLASH_FLAG_READ_FAST | \
346 FLASH_FLAG_READ_1_1_2 | \
347 FLASH_FLAG_READ_1_2_2 | \
348 FLASH_FLAG_READ_1_1_4 | \
349 FLASH_FLAG_SE_4K | \
350 FLASH_FLAG_SE_32K)
351 { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
352 (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
353 stfsm_mx25_config},
354 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
355 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
356 stfsm_mx25_config },
357 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
358 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
359 stfsm_mx25_config},
361 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
362 FLASH_FLAG_READ_FAST | \
363 FLASH_FLAG_READ_1_1_2 | \
364 FLASH_FLAG_READ_1_2_2 | \
365 FLASH_FLAG_READ_1_1_4 | \
366 FLASH_FLAG_READ_1_4_4 | \
367 FLASH_FLAG_WRITE_1_1_2 | \
368 FLASH_FLAG_WRITE_1_2_2 | \
369 FLASH_FLAG_WRITE_1_1_4 | \
370 FLASH_FLAG_WRITE_1_4_4)
371 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
372 stfsm_n25q_config },
373 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
374 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
377 * Spansion S25FLxxxP
378 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
380 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
381 FLASH_FLAG_READ_1_1_2 | \
382 FLASH_FLAG_READ_1_2_2 | \
383 FLASH_FLAG_READ_1_1_4 | \
384 FLASH_FLAG_READ_1_4_4 | \
385 FLASH_FLAG_WRITE_1_1_4 | \
386 FLASH_FLAG_READ_FAST)
387 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
388 stfsm_s25fl_config},
389 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
390 stfsm_s25fl_config },
391 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
392 stfsm_s25fl_config },
395 * Spansion S25FLxxxS
396 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
397 * - RESET# signal supported by die but not bristled out on all
398 * package types. The package type is a function of board design,
399 * so this information is captured in the board's flags.
400 * - Supports 'DYB' sector protection. Depending on variant, sectors
401 * may default to locked state on power-on.
403 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
404 FLASH_FLAG_RESET | \
405 FLASH_FLAG_DYB_LOCKING)
406 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
407 stfsm_s25fl_config },
408 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
409 stfsm_s25fl_config },
410 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
411 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
412 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
413 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
415 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
416 #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
417 FLASH_FLAG_READ_FAST | \
418 FLASH_FLAG_READ_1_1_2 | \
419 FLASH_FLAG_WRITE_1_1_2)
420 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
421 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
422 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
423 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
424 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
426 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
427 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
428 FLASH_FLAG_READ_FAST | \
429 FLASH_FLAG_READ_1_1_2 | \
430 FLASH_FLAG_READ_1_2_2 | \
431 FLASH_FLAG_READ_1_1_4 | \
432 FLASH_FLAG_READ_1_4_4 | \
433 FLASH_FLAG_WRITE_1_1_4)
434 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
435 stfsm_w25q_config },
436 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
437 stfsm_w25q_config },
438 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
439 stfsm_w25q_config },
440 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
441 stfsm_w25q_config },
443 /* Sentinel */
444 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
448 * FSM message sequence configurations:
450 * All configs are presented in order of preference
453 /* Default READ configurations, in order of preference */
454 static struct seq_rw_config default_read_configs[] = {
455 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
456 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
457 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
458 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
459 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
460 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
461 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
464 /* Default WRITE configurations */
465 static struct seq_rw_config default_write_configs[] = {
466 {FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
467 {FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
468 {FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
469 {FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
470 {FLASH_FLAG_READ_WRITE, SPINOR_OP_WRITE, 1, 1, 1, 0x00, 0, 0},
471 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
475 * [N25Qxxx] Configuration
477 #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
478 #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
479 #define N25Q_VCR_WRAP_CONT 0x3
481 /* N25Q 3-byte Address READ configurations
482 * - 'FAST' variants configured for 8 dummy cycles.
484 * Note, the number of dummy cycles used for 'FAST' READ operations is
485 * configurable and would normally be tuned according to the READ command and
486 * operating frequency. However, this applies universally to all 'FAST' READ
487 * commands, including those used by the SPIBoot controller, and remains in
488 * force until the device is power-cycled. Since the SPIBoot controller is
489 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
490 * cycles.
492 static struct seq_rw_config n25q_read3_configs[] = {
493 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
494 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
495 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
496 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
497 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
498 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
499 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
502 /* N25Q 4-byte Address READ configurations
503 * - use special 4-byte address READ commands (reduces overheads, and
504 * reduces risk of hitting watchdog reset issues).
505 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
507 static struct seq_rw_config n25q_read4_configs[] = {
508 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
509 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
510 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
511 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
512 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
513 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
514 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
518 * [MX25xxx] Configuration
520 #define MX25_STATUS_QE (0x1 << 6)
522 static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
524 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
525 SEQ_OPC_CYCLES(8) |
526 SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
527 SEQ_OPC_CSDEASSERT);
529 seq->seq[0] = STFSM_INST_CMD1;
530 seq->seq[1] = STFSM_INST_WAIT;
531 seq->seq[2] = STFSM_INST_STOP;
533 seq->seq_cfg = (SEQ_CFG_PADS_1 |
534 SEQ_CFG_ERASE |
535 SEQ_CFG_READNOTWRITE |
536 SEQ_CFG_CSDEASSERT |
537 SEQ_CFG_STARTSEQ);
539 return 0;
543 * [S25FLxxx] Configuration
545 #define STFSM_S25FL_CONFIG_QE (0x1 << 1)
548 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
549 * Register, Extended Address Modes, and a 32-bit address command set. The
550 * 32-bit address command set is used here, since it avoids any problems with
551 * entering a state that is incompatible with the SPIBoot Controller.
553 static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
554 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
555 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
556 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
557 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
558 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
559 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
560 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
563 static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
564 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
565 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
566 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
570 * [W25Qxxx] Configuration
572 #define W25Q_STATUS_QE (0x1 << 1)
574 static struct stfsm_seq stfsm_seq_read_jedec = {
575 .data_size = TRANSFER_SIZE(8),
576 .seq_opc[0] = (SEQ_OPC_PADS_1 |
577 SEQ_OPC_CYCLES(8) |
578 SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
579 .seq = {
580 STFSM_INST_CMD1,
581 STFSM_INST_DATA_READ,
582 STFSM_INST_STOP,
584 .seq_cfg = (SEQ_CFG_PADS_1 |
585 SEQ_CFG_READNOTWRITE |
586 SEQ_CFG_CSDEASSERT |
587 SEQ_CFG_STARTSEQ),
590 static struct stfsm_seq stfsm_seq_read_status_fifo = {
591 .data_size = TRANSFER_SIZE(4),
592 .seq_opc[0] = (SEQ_OPC_PADS_1 |
593 SEQ_OPC_CYCLES(8) |
594 SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
595 .seq = {
596 STFSM_INST_CMD1,
597 STFSM_INST_DATA_READ,
598 STFSM_INST_STOP,
600 .seq_cfg = (SEQ_CFG_PADS_1 |
601 SEQ_CFG_READNOTWRITE |
602 SEQ_CFG_CSDEASSERT |
603 SEQ_CFG_STARTSEQ),
606 static struct stfsm_seq stfsm_seq_erase_sector = {
607 /* 'addr_cfg' configured during initialisation */
608 .seq_opc = {
609 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
610 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
612 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
613 SEQ_OPC_OPCODE(SPINOR_OP_SE)),
615 .seq = {
616 STFSM_INST_CMD1,
617 STFSM_INST_CMD2,
618 STFSM_INST_ADD1,
619 STFSM_INST_ADD2,
620 STFSM_INST_STOP,
622 .seq_cfg = (SEQ_CFG_PADS_1 |
623 SEQ_CFG_READNOTWRITE |
624 SEQ_CFG_CSDEASSERT |
625 SEQ_CFG_STARTSEQ),
628 static struct stfsm_seq stfsm_seq_erase_chip = {
629 .seq_opc = {
630 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
631 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
633 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
634 SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
636 .seq = {
637 STFSM_INST_CMD1,
638 STFSM_INST_CMD2,
639 STFSM_INST_WAIT,
640 STFSM_INST_STOP,
642 .seq_cfg = (SEQ_CFG_PADS_1 |
643 SEQ_CFG_ERASE |
644 SEQ_CFG_READNOTWRITE |
645 SEQ_CFG_CSDEASSERT |
646 SEQ_CFG_STARTSEQ),
649 static struct stfsm_seq stfsm_seq_write_status = {
650 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
651 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
652 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
653 SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
654 .seq = {
655 STFSM_INST_CMD1,
656 STFSM_INST_CMD2,
657 STFSM_INST_STA_WR1,
658 STFSM_INST_STOP,
660 .seq_cfg = (SEQ_CFG_PADS_1 |
661 SEQ_CFG_READNOTWRITE |
662 SEQ_CFG_CSDEASSERT |
663 SEQ_CFG_STARTSEQ),
666 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
668 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
669 SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
670 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
671 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
672 SEQ_OPC_CSDEASSERT);
674 seq->seq[0] = STFSM_INST_CMD2;
675 seq->seq[1] = STFSM_INST_CMD1;
676 seq->seq[2] = STFSM_INST_WAIT;
677 seq->seq[3] = STFSM_INST_STOP;
679 seq->seq_cfg = (SEQ_CFG_PADS_1 |
680 SEQ_CFG_ERASE |
681 SEQ_CFG_READNOTWRITE |
682 SEQ_CFG_CSDEASSERT |
683 SEQ_CFG_STARTSEQ);
685 return 0;
688 static inline int stfsm_is_idle(struct stfsm *fsm)
690 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
693 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
695 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
698 static void stfsm_clear_fifo(struct stfsm *fsm)
700 uint32_t avail;
702 for (;;) {
703 avail = stfsm_fifo_available(fsm);
704 if (!avail)
705 break;
707 while (avail) {
708 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
709 avail--;
714 static inline void stfsm_load_seq(struct stfsm *fsm,
715 const struct stfsm_seq *seq)
717 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
718 const uint32_t *src = (const uint32_t *)seq;
719 int words = sizeof(*seq) / sizeof(*src);
721 BUG_ON(!stfsm_is_idle(fsm));
723 while (words--) {
724 writel(*src, dst);
725 src++;
726 dst += 4;
730 static void stfsm_wait_seq(struct stfsm *fsm)
732 unsigned long deadline;
733 int timeout = 0;
735 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
737 while (!timeout) {
738 if (time_after_eq(jiffies, deadline))
739 timeout = 1;
741 if (stfsm_is_idle(fsm))
742 return;
744 cond_resched();
747 dev_err(fsm->dev, "timeout on sequence completion\n");
750 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
752 uint32_t remaining = size >> 2;
753 uint32_t avail;
754 uint32_t words;
756 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
758 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
760 while (remaining) {
761 for (;;) {
762 avail = stfsm_fifo_available(fsm);
763 if (avail)
764 break;
765 udelay(1);
767 words = min(avail, remaining);
768 remaining -= words;
770 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
771 buf += words;
775 static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
776 uint32_t size)
778 uint32_t words = size >> 2;
780 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
782 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
784 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
786 return size;
789 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
791 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
792 uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
794 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
795 SEQ_OPC_CYCLES(8) |
796 SEQ_OPC_OPCODE(cmd) |
797 SEQ_OPC_CSDEASSERT);
799 stfsm_load_seq(fsm, seq);
801 stfsm_wait_seq(fsm);
803 return 0;
806 static uint8_t stfsm_wait_busy(struct stfsm *fsm)
808 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
809 unsigned long deadline;
810 uint32_t status;
811 int timeout = 0;
813 /* Use RDRS1 */
814 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
815 SEQ_OPC_CYCLES(8) |
816 SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
818 /* Load read_status sequence */
819 stfsm_load_seq(fsm, seq);
822 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
824 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
825 while (!timeout) {
826 if (time_after_eq(jiffies, deadline))
827 timeout = 1;
829 stfsm_wait_seq(fsm);
831 stfsm_read_fifo(fsm, &status, 4);
833 if ((status & FLASH_STATUS_BUSY) == 0)
834 return 0;
836 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
837 ((status & S25FL_STATUS_P_ERR) ||
838 (status & S25FL_STATUS_E_ERR)))
839 return (uint8_t)(status & 0xff);
841 if (!timeout)
842 /* Restart */
843 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
845 cond_resched();
848 dev_err(fsm->dev, "timeout on wait_busy\n");
850 return FLASH_STATUS_TIMEOUT;
853 static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
854 uint8_t *data, int bytes)
856 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
857 uint32_t tmp;
858 uint8_t *t = (uint8_t *)&tmp;
859 int i;
861 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
862 cmd, bytes);
864 BUG_ON(bytes != 1 && bytes != 2);
866 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
867 SEQ_OPC_OPCODE(cmd)),
869 stfsm_load_seq(fsm, seq);
871 stfsm_read_fifo(fsm, &tmp, 4);
873 for (i = 0; i < bytes; i++)
874 data[i] = t[i];
876 stfsm_wait_seq(fsm);
878 return 0;
881 static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
882 uint16_t data, int bytes, int wait_busy)
884 struct stfsm_seq *seq = &stfsm_seq_write_status;
886 dev_dbg(fsm->dev,
887 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
888 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
890 BUG_ON(bytes != 1 && bytes != 2);
892 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
893 SEQ_OPC_OPCODE(cmd));
895 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
896 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
898 stfsm_load_seq(fsm, seq);
900 stfsm_wait_seq(fsm);
902 if (wait_busy)
903 stfsm_wait_busy(fsm);
905 return 0;
909 * SoC reset on 'boot-from-spi' systems
911 * Certain modes of operation cause the Flash device to enter a particular state
912 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
913 * Addr' commands). On boot-from-spi systems, it is important to consider what
914 * happens if a warm reset occurs during this period. The SPIBoot controller
915 * assumes that Flash device is in its default reset state, 24-bit address mode,
916 * and ready to accept commands. This can be achieved using some form of
917 * on-board logic/controller to force a device POR in response to a SoC-level
918 * reset or by making use of the device reset signal if available (limited
919 * number of devices only).
921 * Failure to take such precautions can cause problems following a warm reset.
922 * For some operations (e.g. ERASE), there is little that can be done. For
923 * other modes of operation (e.g. 32-bit addressing), options are often
924 * available that can help minimise the window in which a reset could cause a
925 * problem.
928 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
930 /* Reset signal is available on the board and supported by the device */
931 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
932 return true;
934 /* Board-level logic forces a power-on-reset */
935 if (fsm->reset_por)
936 return true;
938 /* Reset is not properly handled and may result in failure to reboot */
939 return false;
942 /* Configure 'addr_cfg' according to addressing mode */
943 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
944 struct stfsm_seq *seq)
946 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
948 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
949 ADR_CFG_PADS_1_ADD1 |
950 ADR_CFG_CYCLES_ADD2(16) |
951 ADR_CFG_PADS_1_ADD2 |
952 ADR_CFG_CSDEASSERT_ADD2);
955 /* Search for preferred configuration based on available flags */
956 static struct seq_rw_config *
957 stfsm_search_seq_rw_configs(struct stfsm *fsm,
958 struct seq_rw_config cfgs[])
960 struct seq_rw_config *config;
961 int flags = fsm->info->flags;
963 for (config = cfgs; config->cmd != 0; config++)
964 if ((config->flags & flags) == config->flags)
965 return config;
967 return NULL;
970 /* Prepare a READ/WRITE sequence according to configuration parameters */
971 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
972 struct stfsm_seq *seq,
973 struct seq_rw_config *cfg)
975 int addr1_cycles, addr2_cycles;
976 int i = 0;
978 memset(seq, 0, sizeof(*seq));
980 /* Add READ/WRITE OPC */
981 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
982 SEQ_OPC_CYCLES(8) |
983 SEQ_OPC_OPCODE(cfg->cmd));
985 /* Add WREN OPC for a WRITE sequence */
986 if (cfg->write)
987 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
988 SEQ_OPC_CYCLES(8) |
989 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
990 SEQ_OPC_CSDEASSERT);
992 /* Address configuration (24 or 32-bit addresses) */
993 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
994 addr1_cycles /= cfg->addr_pads;
995 addr2_cycles = 16 / cfg->addr_pads;
996 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
997 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
998 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
999 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1001 /* Data/Sequence configuration */
1002 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1003 SEQ_CFG_STARTSEQ |
1004 SEQ_CFG_CSDEASSERT);
1005 if (!cfg->write)
1006 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1008 /* Mode configuration (no. of pads taken from addr cfg) */
1009 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1010 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1011 (cfg->addr_pads - 1) << 22); /* pads */
1013 /* Dummy configuration (no. of pads taken from addr cfg) */
1014 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1015 (cfg->addr_pads - 1) << 22); /* pads */
1018 /* Instruction sequence */
1019 i = 0;
1020 if (cfg->write)
1021 seq->seq[i++] = STFSM_INST_CMD2;
1023 seq->seq[i++] = STFSM_INST_CMD1;
1025 seq->seq[i++] = STFSM_INST_ADD1;
1026 seq->seq[i++] = STFSM_INST_ADD2;
1028 if (cfg->mode_cycles)
1029 seq->seq[i++] = STFSM_INST_MODE;
1031 if (cfg->dummy_cycles)
1032 seq->seq[i++] = STFSM_INST_DUMMY;
1034 seq->seq[i++] =
1035 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1036 seq->seq[i++] = STFSM_INST_STOP;
1039 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1040 struct stfsm_seq *seq,
1041 struct seq_rw_config *cfgs)
1043 struct seq_rw_config *config;
1045 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1046 if (!config) {
1047 dev_err(fsm->dev, "failed to find suitable config\n");
1048 return -EINVAL;
1051 stfsm_prepare_rw_seq(fsm, seq, config);
1053 return 0;
1056 /* Prepare a READ/WRITE/ERASE 'default' sequences */
1057 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1059 uint32_t flags = fsm->info->flags;
1060 int ret;
1062 /* Configure 'READ' sequence */
1063 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1064 default_read_configs);
1065 if (ret) {
1066 dev_err(fsm->dev,
1067 "failed to prep READ sequence with flags [0x%08x]\n",
1068 flags);
1069 return ret;
1072 /* Configure 'WRITE' sequence */
1073 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1074 default_write_configs);
1075 if (ret) {
1076 dev_err(fsm->dev,
1077 "failed to prep WRITE sequence with flags [0x%08x]\n",
1078 flags);
1079 return ret;
1082 /* Configure 'ERASE_SECTOR' sequence */
1083 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1085 return 0;
1088 static int stfsm_mx25_config(struct stfsm *fsm)
1090 uint32_t flags = fsm->info->flags;
1091 uint32_t data_pads;
1092 uint8_t sta;
1093 int ret;
1094 bool soc_reset;
1097 * Use default READ/WRITE sequences
1099 ret = stfsm_prepare_rwe_seqs_default(fsm);
1100 if (ret)
1101 return ret;
1104 * Configure 32-bit Address Support
1106 if (flags & FLASH_FLAG_32BIT_ADDR) {
1107 /* Configure 'enter_32bitaddr' FSM sequence */
1108 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1110 soc_reset = stfsm_can_handle_soc_reset(fsm);
1111 if (soc_reset || !fsm->booted_from_spi)
1112 /* If we can handle SoC resets, we enable 32-bit address
1113 * mode pervasively */
1114 stfsm_enter_32bit_addr(fsm, 1);
1116 else
1117 /* Else, enable/disable 32-bit addressing before/after
1118 * each operation */
1119 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1120 CFG_WRITE_TOGGLE_32BIT_ADDR |
1121 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1124 /* Check status of 'QE' bit, update if required. */
1125 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
1126 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1127 if (data_pads == 4) {
1128 if (!(sta & MX25_STATUS_QE)) {
1129 /* Set 'QE' */
1130 sta |= MX25_STATUS_QE;
1132 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1134 } else {
1135 if (sta & MX25_STATUS_QE) {
1136 /* Clear 'QE' */
1137 sta &= ~MX25_STATUS_QE;
1139 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1143 return 0;
1146 static int stfsm_n25q_config(struct stfsm *fsm)
1148 uint32_t flags = fsm->info->flags;
1149 uint8_t vcr;
1150 int ret = 0;
1151 bool soc_reset;
1153 /* Configure 'READ' sequence */
1154 if (flags & FLASH_FLAG_32BIT_ADDR)
1155 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1156 n25q_read4_configs);
1157 else
1158 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1159 n25q_read3_configs);
1160 if (ret) {
1161 dev_err(fsm->dev,
1162 "failed to prepare READ sequence with flags [0x%08x]\n",
1163 flags);
1164 return ret;
1167 /* Configure 'WRITE' sequence (default configs) */
1168 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1169 default_write_configs);
1170 if (ret) {
1171 dev_err(fsm->dev,
1172 "preparing WRITE sequence using flags [0x%08x] failed\n",
1173 flags);
1174 return ret;
1177 /* * Configure 'ERASE_SECTOR' sequence */
1178 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1180 /* Configure 32-bit address support */
1181 if (flags & FLASH_FLAG_32BIT_ADDR) {
1182 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1184 soc_reset = stfsm_can_handle_soc_reset(fsm);
1185 if (soc_reset || !fsm->booted_from_spi) {
1187 * If we can handle SoC resets, we enable 32-bit
1188 * address mode pervasively
1190 stfsm_enter_32bit_addr(fsm, 1);
1191 } else {
1193 * If not, enable/disable for WRITE and ERASE
1194 * operations (READ uses special commands)
1196 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1197 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1202 * Configure device to use 8 dummy cycles
1204 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1205 N25Q_VCR_WRAP_CONT);
1206 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
1208 return 0;
1211 static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1213 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1214 SEQ_OPC_CYCLES(8) |
1215 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1217 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1218 ADR_CFG_PADS_1_ADD1 |
1219 ADR_CFG_CYCLES_ADD2(16) |
1220 ADR_CFG_PADS_1_ADD2 |
1221 ADR_CFG_CSDEASSERT_ADD2);
1224 static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1226 uint32_t tmp;
1227 struct stfsm_seq seq = {
1228 .data_size = TRANSFER_SIZE(4),
1229 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1230 SEQ_OPC_CYCLES(8) |
1231 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1232 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1233 ADR_CFG_PADS_1_ADD1 |
1234 ADR_CFG_CYCLES_ADD2(16) |
1235 ADR_CFG_PADS_1_ADD2),
1236 .addr1 = (offs >> 16) & 0xffff,
1237 .addr2 = offs & 0xffff,
1238 .seq = {
1239 STFSM_INST_CMD1,
1240 STFSM_INST_ADD1,
1241 STFSM_INST_ADD2,
1242 STFSM_INST_DATA_READ,
1243 STFSM_INST_STOP,
1245 .seq_cfg = (SEQ_CFG_PADS_1 |
1246 SEQ_CFG_READNOTWRITE |
1247 SEQ_CFG_CSDEASSERT |
1248 SEQ_CFG_STARTSEQ),
1251 stfsm_load_seq(fsm, &seq);
1253 stfsm_read_fifo(fsm, &tmp, 4);
1255 *dby = (uint8_t)(tmp >> 24);
1257 stfsm_wait_seq(fsm);
1260 static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1262 struct stfsm_seq seq = {
1263 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1264 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
1265 SEQ_OPC_CSDEASSERT),
1266 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1267 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1268 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1269 ADR_CFG_PADS_1_ADD1 |
1270 ADR_CFG_CYCLES_ADD2(16) |
1271 ADR_CFG_PADS_1_ADD2),
1272 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1273 .addr1 = (offs >> 16) & 0xffff,
1274 .addr2 = offs & 0xffff,
1275 .seq = {
1276 STFSM_INST_CMD1,
1277 STFSM_INST_CMD2,
1278 STFSM_INST_ADD1,
1279 STFSM_INST_ADD2,
1280 STFSM_INST_STA_WR1,
1281 STFSM_INST_STOP,
1283 .seq_cfg = (SEQ_CFG_PADS_1 |
1284 SEQ_CFG_READNOTWRITE |
1285 SEQ_CFG_CSDEASSERT |
1286 SEQ_CFG_STARTSEQ),
1289 stfsm_load_seq(fsm, &seq);
1290 stfsm_wait_seq(fsm);
1292 stfsm_wait_busy(fsm);
1295 static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1297 struct stfsm_seq seq = {
1298 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1299 SEQ_OPC_CYCLES(8) |
1300 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1301 SEQ_OPC_CSDEASSERT),
1302 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1303 SEQ_OPC_CYCLES(8) |
1304 SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
1305 SEQ_OPC_CSDEASSERT),
1306 .seq = {
1307 STFSM_INST_CMD1,
1308 STFSM_INST_CMD2,
1309 STFSM_INST_WAIT,
1310 STFSM_INST_STOP,
1312 .seq_cfg = (SEQ_CFG_PADS_1 |
1313 SEQ_CFG_ERASE |
1314 SEQ_CFG_READNOTWRITE |
1315 SEQ_CFG_CSDEASSERT |
1316 SEQ_CFG_STARTSEQ),
1319 stfsm_load_seq(fsm, &seq);
1321 stfsm_wait_seq(fsm);
1323 return 0;
1326 static int stfsm_s25fl_config(struct stfsm *fsm)
1328 struct flash_info *info = fsm->info;
1329 uint32_t flags = info->flags;
1330 uint32_t data_pads;
1331 uint32_t offs;
1332 uint16_t sta_wr;
1333 uint8_t sr1, cr1, dyb;
1334 int update_sr = 0;
1335 int ret;
1337 if (flags & FLASH_FLAG_32BIT_ADDR) {
1339 * Prepare Read/Write/Erase sequences according to S25FLxxx
1340 * 32-bit address command set
1342 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1343 stfsm_s25fl_read4_configs);
1344 if (ret)
1345 return ret;
1347 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1348 stfsm_s25fl_write4_configs);
1349 if (ret)
1350 return ret;
1352 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1354 } else {
1355 /* Use default configurations for 24-bit addressing */
1356 ret = stfsm_prepare_rwe_seqs_default(fsm);
1357 if (ret)
1358 return ret;
1362 * For devices that support 'DYB' sector locking, check lock status and
1363 * unlock sectors if necessary (some variants power-on with sectors
1364 * locked by default)
1366 if (flags & FLASH_FLAG_DYB_LOCKING) {
1367 offs = 0;
1368 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1369 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1370 if (dyb == 0x00)
1371 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1373 /* Handle bottom/top 4KiB parameter sectors */
1374 if ((offs < info->sector_size * 2) ||
1375 (offs >= (info->sector_size - info->n_sectors * 4)))
1376 offs += 0x1000;
1377 else
1378 offs += 0x10000;
1382 /* Check status of 'QE' bit, update if required. */
1383 stfsm_read_status(fsm, SPINOR_OP_RDSR2, &cr1, 1);
1384 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1385 if (data_pads == 4) {
1386 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1387 /* Set 'QE' */
1388 cr1 |= STFSM_S25FL_CONFIG_QE;
1390 update_sr = 1;
1392 } else {
1393 if (cr1 & STFSM_S25FL_CONFIG_QE) {
1394 /* Clear 'QE' */
1395 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1397 update_sr = 1;
1400 if (update_sr) {
1401 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
1402 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1403 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
1407 * S25FLxxx devices support Program and Error error flags.
1408 * Configure driver to check flags and clear if necessary.
1410 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1412 return 0;
1415 static int stfsm_w25q_config(struct stfsm *fsm)
1417 uint32_t data_pads;
1418 uint8_t sr1, sr2;
1419 uint16_t sr_wr;
1420 int update_sr = 0;
1421 int ret;
1423 ret = stfsm_prepare_rwe_seqs_default(fsm);
1424 if (ret)
1425 return ret;
1427 /* Check status of 'QE' bit, update if required. */
1428 stfsm_read_status(fsm, SPINOR_OP_RDSR2, &sr2, 1);
1429 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1430 if (data_pads == 4) {
1431 if (!(sr2 & W25Q_STATUS_QE)) {
1432 /* Set 'QE' */
1433 sr2 |= W25Q_STATUS_QE;
1434 update_sr = 1;
1436 } else {
1437 if (sr2 & W25Q_STATUS_QE) {
1438 /* Clear 'QE' */
1439 sr2 &= ~W25Q_STATUS_QE;
1440 update_sr = 1;
1443 if (update_sr) {
1444 /* Write status register */
1445 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
1446 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1447 stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
1450 return 0;
1453 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1454 uint32_t offset)
1456 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
1457 uint32_t data_pads;
1458 uint32_t read_mask;
1459 uint32_t size_ub;
1460 uint32_t size_lb;
1461 uint32_t size_mop;
1462 uint32_t tmp[4];
1463 uint32_t page_buf[FLASH_PAGESIZE_32];
1464 uint8_t *p;
1466 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1468 /* Enter 32-bit address mode, if required */
1469 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1470 stfsm_enter_32bit_addr(fsm, 1);
1472 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1473 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1474 read_mask = (data_pads << 2) - 1;
1476 /* Handle non-aligned buf */
1477 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1479 /* Handle non-aligned size */
1480 size_ub = (size + read_mask) & ~read_mask;
1481 size_lb = size & ~read_mask;
1482 size_mop = size & read_mask;
1484 seq->data_size = TRANSFER_SIZE(size_ub);
1485 seq->addr1 = (offset >> 16) & 0xffff;
1486 seq->addr2 = offset & 0xffff;
1488 stfsm_load_seq(fsm, seq);
1490 if (size_lb)
1491 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1493 if (size_mop) {
1494 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1495 memcpy(p + size_lb, &tmp, size_mop);
1498 /* Handle non-aligned buf */
1499 if ((uint32_t)buf & 0x3)
1500 memcpy(buf, page_buf, size);
1502 /* Wait for sequence to finish */
1503 stfsm_wait_seq(fsm);
1505 stfsm_clear_fifo(fsm);
1507 /* Exit 32-bit address mode, if required */
1508 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1509 stfsm_enter_32bit_addr(fsm, 0);
1511 return 0;
1514 static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1515 uint32_t size, uint32_t offset)
1517 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
1518 uint32_t data_pads;
1519 uint32_t write_mask;
1520 uint32_t size_ub;
1521 uint32_t size_lb;
1522 uint32_t size_mop;
1523 uint32_t tmp[4];
1524 uint32_t page_buf[FLASH_PAGESIZE_32];
1525 uint8_t *t = (uint8_t *)&tmp;
1526 const uint8_t *p;
1527 int ret;
1528 int i;
1530 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1532 /* Enter 32-bit address mode, if required */
1533 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1534 stfsm_enter_32bit_addr(fsm, 1);
1536 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1537 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1538 write_mask = (data_pads << 2) - 1;
1540 /* Handle non-aligned buf */
1541 if ((uint32_t)buf & 0x3) {
1542 memcpy(page_buf, buf, size);
1543 p = (uint8_t *)page_buf;
1544 } else {
1545 p = buf;
1548 /* Handle non-aligned size */
1549 size_ub = (size + write_mask) & ~write_mask;
1550 size_lb = size & ~write_mask;
1551 size_mop = size & write_mask;
1553 seq->data_size = TRANSFER_SIZE(size_ub);
1554 seq->addr1 = (offset >> 16) & 0xffff;
1555 seq->addr2 = offset & 0xffff;
1557 /* Need to set FIFO to write mode, before writing data to FIFO (see
1558 * GNBvb79594)
1560 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1563 * Before writing data to the FIFO, apply a small delay to allow a
1564 * potential change of FIFO direction to complete.
1566 if (fsm->fifo_dir_delay == 0)
1567 readl(fsm->base + SPI_FAST_SEQ_CFG);
1568 else
1569 udelay(fsm->fifo_dir_delay);
1572 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1573 if (size_lb) {
1574 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1575 p += size_lb;
1578 /* Handle non-aligned size */
1579 if (size_mop) {
1580 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1581 for (i = 0; i < size_mop; i++)
1582 t[i] = *p++;
1584 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1587 /* Start sequence */
1588 stfsm_load_seq(fsm, seq);
1590 /* Wait for sequence to finish */
1591 stfsm_wait_seq(fsm);
1593 /* Wait for completion */
1594 ret = stfsm_wait_busy(fsm);
1595 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1596 stfsm_s25fl_clear_status_reg(fsm);
1598 /* Exit 32-bit address mode, if required */
1599 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1600 stfsm_enter_32bit_addr(fsm, 0);
1602 return 0;
1606 * Read an address range from the flash chip. The address range
1607 * may be any size provided it is within the physical boundaries.
1609 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1610 size_t *retlen, u_char *buf)
1612 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1613 uint32_t bytes;
1615 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1616 __func__, (u32)from, len);
1618 mutex_lock(&fsm->lock);
1620 while (len > 0) {
1621 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1623 stfsm_read(fsm, buf, bytes, from);
1625 buf += bytes;
1626 from += bytes;
1627 len -= bytes;
1629 *retlen += bytes;
1632 mutex_unlock(&fsm->lock);
1634 return 0;
1637 static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
1639 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1640 int ret;
1642 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1644 /* Enter 32-bit address mode, if required */
1645 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1646 stfsm_enter_32bit_addr(fsm, 1);
1648 seq->addr1 = (offset >> 16) & 0xffff;
1649 seq->addr2 = offset & 0xffff;
1651 stfsm_load_seq(fsm, seq);
1653 stfsm_wait_seq(fsm);
1655 /* Wait for completion */
1656 ret = stfsm_wait_busy(fsm);
1657 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1658 stfsm_s25fl_clear_status_reg(fsm);
1660 /* Exit 32-bit address mode, if required */
1661 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1662 stfsm_enter_32bit_addr(fsm, 0);
1664 return ret;
1667 static int stfsm_erase_chip(struct stfsm *fsm)
1669 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1671 dev_dbg(fsm->dev, "erasing chip\n");
1673 stfsm_load_seq(fsm, seq);
1675 stfsm_wait_seq(fsm);
1677 return stfsm_wait_busy(fsm);
1681 * Write an address range to the flash chip. Data must be written in
1682 * FLASH_PAGESIZE chunks. The address range may be any size provided
1683 * it is within the physical boundaries.
1685 static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1686 size_t *retlen, const u_char *buf)
1688 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1690 u32 page_offs;
1691 u32 bytes;
1692 uint8_t *b = (uint8_t *)buf;
1693 int ret = 0;
1695 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1697 /* Offset within page */
1698 page_offs = to % FLASH_PAGESIZE;
1700 mutex_lock(&fsm->lock);
1702 while (len) {
1703 /* Write up to page boundary */
1704 bytes = min(FLASH_PAGESIZE - page_offs, len);
1706 ret = stfsm_write(fsm, b, bytes, to);
1707 if (ret)
1708 goto out1;
1710 b += bytes;
1711 len -= bytes;
1712 to += bytes;
1714 /* We are now page-aligned */
1715 page_offs = 0;
1717 *retlen += bytes;
1721 out1:
1722 mutex_unlock(&fsm->lock);
1724 return ret;
1728 * Erase an address range on the flash chip. The address range may extend
1729 * one or more erase sectors. Return an error is there is a problem erasing.
1731 static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1733 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1734 u32 addr, len;
1735 int ret;
1737 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1738 (long long)instr->addr, (long long)instr->len);
1740 addr = instr->addr;
1741 len = instr->len;
1743 mutex_lock(&fsm->lock);
1745 /* Whole-chip erase? */
1746 if (len == mtd->size) {
1747 ret = stfsm_erase_chip(fsm);
1748 if (ret)
1749 goto out1;
1750 } else {
1751 while (len) {
1752 ret = stfsm_erase_sector(fsm, addr);
1753 if (ret)
1754 goto out1;
1756 addr += mtd->erasesize;
1757 len -= mtd->erasesize;
1761 mutex_unlock(&fsm->lock);
1763 instr->state = MTD_ERASE_DONE;
1764 mtd_erase_callback(instr);
1766 return 0;
1768 out1:
1769 instr->state = MTD_ERASE_FAILED;
1770 mutex_unlock(&fsm->lock);
1772 return ret;
1775 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
1777 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1778 uint32_t tmp[2];
1780 stfsm_load_seq(fsm, seq);
1782 stfsm_read_fifo(fsm, tmp, 8);
1784 memcpy(jedec, tmp, 5);
1786 stfsm_wait_seq(fsm);
1789 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1791 struct flash_info *info;
1792 u16 ext_jedec;
1793 u32 jedec;
1794 u8 id[5];
1796 stfsm_read_jedec(fsm, id);
1798 jedec = id[0] << 16 | id[1] << 8 | id[2];
1800 * JEDEC also defines an optional "extended device information"
1801 * string for after vendor-specific data, after the three bytes
1802 * we use here. Supporting some chips might require using it.
1804 ext_jedec = id[3] << 8 | id[4];
1806 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1807 jedec, id[0], id[1], id[2], id[3], id[4]);
1809 for (info = flash_types; info->name; info++) {
1810 if (info->jedec_id == jedec) {
1811 if (info->ext_id && info->ext_id != ext_jedec)
1812 continue;
1813 return info;
1816 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1818 return NULL;
1821 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1823 int ret, timeout = 10;
1825 /* Wait for controller to accept mode change */
1826 while (--timeout) {
1827 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1828 if (ret & 0x1)
1829 break;
1830 udelay(1);
1833 if (!timeout)
1834 return -EBUSY;
1836 writel(mode, fsm->base + SPI_MODESELECT);
1838 return 0;
1841 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1843 uint32_t emi_freq;
1844 uint32_t clk_div;
1846 /* TODO: Make this dynamic */
1847 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1850 * Calculate clk_div - values between 2 and 128
1851 * Multiple of 2, rounded up
1853 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1854 if (clk_div < 2)
1855 clk_div = 2;
1856 else if (clk_div > 128)
1857 clk_div = 128;
1860 * Determine a suitable delay for the IP to complete a change of
1861 * direction of the FIFO. The required delay is related to the clock
1862 * divider used. The following heuristics are based on empirical tests,
1863 * using a 100MHz EMI clock.
1865 if (clk_div <= 4)
1866 fsm->fifo_dir_delay = 0;
1867 else if (clk_div <= 10)
1868 fsm->fifo_dir_delay = 1;
1869 else
1870 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1872 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1873 emi_freq, spi_freq, clk_div);
1875 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1878 static int stfsm_init(struct stfsm *fsm)
1880 int ret;
1882 /* Perform a soft reset of the FSM controller */
1883 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1884 udelay(1);
1885 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1887 /* Set clock to 'safe' frequency initially */
1888 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1890 /* Switch to FSM */
1891 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1892 if (ret)
1893 return ret;
1895 /* Set timing parameters */
1896 writel(SPI_CFG_DEVICE_ST |
1897 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1898 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1899 SPI_CFG_DEFAULT_DATA_HOLD,
1900 fsm->base + SPI_CONFIGDATA);
1901 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1904 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1905 * our purposes, the WAIT instruction is used purely to achieve
1906 * "sequence validity" rather than actually implement a delay.
1908 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1910 /* Clear FIFO, just in case */
1911 stfsm_clear_fifo(fsm);
1913 return 0;
1916 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1918 struct stfsm *fsm = platform_get_drvdata(pdev);
1919 struct device_node *np = pdev->dev.of_node;
1920 struct regmap *regmap;
1921 uint32_t boot_device_reg;
1922 uint32_t boot_device_spi;
1923 uint32_t boot_device; /* Value we read from *boot_device_reg */
1924 int ret;
1926 /* Booting from SPI NOR Flash is the default */
1927 fsm->booted_from_spi = true;
1929 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1930 if (IS_ERR(regmap))
1931 goto boot_device_fail;
1933 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1935 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1937 /* Where in the syscon the boot device information lives */
1938 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1939 if (ret)
1940 goto boot_device_fail;
1942 /* Boot device value when booted from SPI NOR */
1943 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1944 if (ret)
1945 goto boot_device_fail;
1947 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1948 if (ret)
1949 goto boot_device_fail;
1951 if (boot_device != boot_device_spi)
1952 fsm->booted_from_spi = false;
1954 return;
1956 boot_device_fail:
1957 dev_warn(&pdev->dev,
1958 "failed to fetch boot device, assuming boot from SPI\n");
1961 static int stfsm_probe(struct platform_device *pdev)
1963 struct device_node *np = pdev->dev.of_node;
1964 struct mtd_part_parser_data ppdata;
1965 struct flash_info *info;
1966 struct resource *res;
1967 struct stfsm *fsm;
1968 int ret;
1970 if (!np) {
1971 dev_err(&pdev->dev, "No DT found\n");
1972 return -EINVAL;
1974 ppdata.of_node = np;
1976 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1977 if (!fsm)
1978 return -ENOMEM;
1980 fsm->dev = &pdev->dev;
1982 platform_set_drvdata(pdev, fsm);
1984 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1985 if (!res) {
1986 dev_err(&pdev->dev, "Resource not found\n");
1987 return -ENODEV;
1990 fsm->base = devm_ioremap_resource(&pdev->dev, res);
1991 if (IS_ERR(fsm->base)) {
1992 dev_err(&pdev->dev,
1993 "Failed to reserve memory region %pR\n", res);
1994 return PTR_ERR(fsm->base);
1997 mutex_init(&fsm->lock);
1999 ret = stfsm_init(fsm);
2000 if (ret) {
2001 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2002 return ret;
2005 stfsm_fetch_platform_configs(pdev);
2007 /* Detect SPI FLASH device */
2008 info = stfsm_jedec_probe(fsm);
2009 if (!info)
2010 return -ENODEV;
2011 fsm->info = info;
2013 /* Use device size to determine address width */
2014 if (info->sector_size * info->n_sectors > 0x1000000)
2015 info->flags |= FLASH_FLAG_32BIT_ADDR;
2018 * Configure READ/WRITE/ERASE sequences according to platform and
2019 * device flags.
2021 if (info->config) {
2022 ret = info->config(fsm);
2023 if (ret)
2024 return ret;
2025 } else {
2026 ret = stfsm_prepare_rwe_seqs_default(fsm);
2027 if (ret)
2028 return ret;
2031 fsm->mtd.name = info->name;
2032 fsm->mtd.dev.parent = &pdev->dev;
2033 fsm->mtd.type = MTD_NORFLASH;
2034 fsm->mtd.writesize = 4;
2035 fsm->mtd.writebufsize = fsm->mtd.writesize;
2036 fsm->mtd.flags = MTD_CAP_NORFLASH;
2037 fsm->mtd.size = info->sector_size * info->n_sectors;
2038 fsm->mtd.erasesize = info->sector_size;
2040 fsm->mtd._read = stfsm_mtd_read;
2041 fsm->mtd._write = stfsm_mtd_write;
2042 fsm->mtd._erase = stfsm_mtd_erase;
2044 dev_info(&pdev->dev,
2045 "Found serial flash device: %s\n"
2046 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2047 info->name,
2048 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2049 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
2051 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
2054 static int stfsm_remove(struct platform_device *pdev)
2056 struct stfsm *fsm = platform_get_drvdata(pdev);
2058 return mtd_device_unregister(&fsm->mtd);
2061 static struct of_device_id stfsm_match[] = {
2062 { .compatible = "st,spi-fsm", },
2065 MODULE_DEVICE_TABLE(of, stfsm_match);
2067 static struct platform_driver stfsm_driver = {
2068 .probe = stfsm_probe,
2069 .remove = stfsm_remove,
2070 .driver = {
2071 .name = "st-spi-fsm",
2072 .owner = THIS_MODULE,
2073 .of_match_table = stfsm_match,
2076 module_platform_driver(stfsm_driver);
2078 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2079 MODULE_DESCRIPTION("ST SPI FSM driver");
2080 MODULE_LICENSE("GPL");