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/sched.h>
23 #include <linux/delay.h>
27 #include "serial_flash_cmds.h"
30 * FSM SPI Controller Registers
32 #define SPI_CLOCKDIV 0x0010
33 #define SPI_MODESELECT 0x0018
34 #define SPI_CONFIGDATA 0x0020
35 #define SPI_STA_MODE_CHANGE 0x0028
36 #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
37 #define SPI_FAST_SEQ_ADD1 0x0104
38 #define SPI_FAST_SEQ_ADD2 0x0108
39 #define SPI_FAST_SEQ_ADD_CFG 0x010c
40 #define SPI_FAST_SEQ_OPC1 0x0110
41 #define SPI_FAST_SEQ_OPC2 0x0114
42 #define SPI_FAST_SEQ_OPC3 0x0118
43 #define SPI_FAST_SEQ_OPC4 0x011c
44 #define SPI_FAST_SEQ_OPC5 0x0120
45 #define SPI_MODE_BITS 0x0124
46 #define SPI_DUMMY_BITS 0x0128
47 #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
48 #define SPI_FAST_SEQ_1 0x0130
49 #define SPI_FAST_SEQ_2 0x0134
50 #define SPI_FAST_SEQ_3 0x0138
51 #define SPI_FAST_SEQ_4 0x013c
52 #define SPI_FAST_SEQ_CFG 0x0140
53 #define SPI_FAST_SEQ_STA 0x0144
54 #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
55 #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
56 #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
57 #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
58 #define SPI_PROGRAM_ERASE_TIME 0x0158
59 #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
60 #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
61 #define SPI_STATUS_WR_TIME_REG 0x0164
62 #define SPI_FAST_SEQ_DATA_REG 0x0300
65 * Register: SPI_MODESELECT
67 #define SPI_MODESELECT_CONTIG 0x01
68 #define SPI_MODESELECT_FASTREAD 0x02
69 #define SPI_MODESELECT_DUALIO 0x04
70 #define SPI_MODESELECT_FSM 0x08
71 #define SPI_MODESELECT_QUADBOOT 0x10
74 * Register: SPI_CONFIGDATA
76 #define SPI_CFG_DEVICE_ST 0x1
77 #define SPI_CFG_DEVICE_ATMEL 0x4
78 #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
79 #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
80 #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
82 #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
83 #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
84 #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
87 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
89 #define TRANSFER_SIZE(x) ((x) * 8)
92 * Register: SPI_FAST_SEQ_ADD_CFG
94 #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
95 #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
96 #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
97 #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
98 #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
99 #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
100 #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
101 #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
102 #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
103 #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
106 * Register: SPI_FAST_SEQ_n
108 #define SEQ_OPC_OPCODE(x) ((x) << 0)
109 #define SEQ_OPC_CYCLES(x) ((x) << 8)
110 #define SEQ_OPC_PADS_1 (0x0 << 14)
111 #define SEQ_OPC_PADS_2 (0x1 << 14)
112 #define SEQ_OPC_PADS_4 (0x3 << 14)
113 #define SEQ_OPC_CSDEASSERT (1 << 16)
116 * Register: SPI_FAST_SEQ_CFG
118 #define SEQ_CFG_STARTSEQ (1 << 0)
119 #define SEQ_CFG_SWRESET (1 << 5)
120 #define SEQ_CFG_CSDEASSERT (1 << 6)
121 #define SEQ_CFG_READNOTWRITE (1 << 7)
122 #define SEQ_CFG_ERASE (1 << 8)
123 #define SEQ_CFG_PADS_1 (0x0 << 16)
124 #define SEQ_CFG_PADS_2 (0x1 << 16)
125 #define SEQ_CFG_PADS_4 (0x3 << 16)
128 * Register: SPI_MODE_BITS
130 #define MODE_DATA(x) (x & 0xff)
131 #define MODE_CYCLES(x) ((x & 0x3f) << 16)
132 #define MODE_PADS_1 (0x0 << 22)
133 #define MODE_PADS_2 (0x1 << 22)
134 #define MODE_PADS_4 (0x3 << 22)
135 #define DUMMY_CSDEASSERT (1 << 24)
138 * Register: SPI_DUMMY_BITS
140 #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
141 #define DUMMY_PADS_1 (0x0 << 22)
142 #define DUMMY_PADS_2 (0x1 << 22)
143 #define DUMMY_PADS_4 (0x3 << 22)
144 #define DUMMY_CSDEASSERT (1 << 24)
147 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
149 #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
150 #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
151 #define STA_PADS_1 (0x0 << 16)
152 #define STA_PADS_2 (0x1 << 16)
153 #define STA_PADS_4 (0x3 << 16)
154 #define STA_CSDEASSERT (0x1 << 20)
155 #define STA_RDNOTWR (0x1 << 21)
158 * FSM SPI Instruction Opcodes
160 #define STFSM_OPC_CMD 0x1
161 #define STFSM_OPC_ADD 0x2
162 #define STFSM_OPC_STA 0x3
163 #define STFSM_OPC_MODE 0x4
164 #define STFSM_OPC_DUMMY 0x5
165 #define STFSM_OPC_DATA 0x6
166 #define STFSM_OPC_WAIT 0x7
167 #define STFSM_OPC_JUMP 0x8
168 #define STFSM_OPC_GOTO 0x9
169 #define STFSM_OPC_STOP 0xF
172 * FSM SPI Instructions (== opcode + operand).
174 #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
176 #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
177 #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
178 #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
179 #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
180 #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
181 #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
182 #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
184 #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
185 #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
187 #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
188 #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
189 #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
190 #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
192 #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
193 #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
194 #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
195 #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
197 #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
198 #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
200 #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
202 #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
205 #define FLASH_CMD_WREN 0x06
206 #define FLASH_CMD_WRDI 0x04
207 #define FLASH_CMD_RDID 0x9f
208 #define FLASH_CMD_RDSR 0x05
209 #define FLASH_CMD_RDSR2 0x35
210 #define FLASH_CMD_WRSR 0x01
211 #define FLASH_CMD_SE_4K 0x20
212 #define FLASH_CMD_SE_32K 0x52
213 #define FLASH_CMD_SE 0xd8
214 #define FLASH_CMD_CHIPERASE 0xc7
216 #define FLASH_CMD_READ 0x03 /* READ */
217 #define FLASH_CMD_READ_FAST 0x0b /* FAST READ */
218 #define FLASH_CMD_READ_1_1_2 0x3b /* DUAL OUTPUT READ */
219 #define FLASH_CMD_READ_1_2_2 0xbb /* DUAL I/O READ */
220 #define FLASH_CMD_READ_1_1_4 0x6b /* QUAD OUTPUT READ */
221 #define FLASH_CMD_READ_1_4_4 0xeb /* QUAD I/O READ */
223 #define FLASH_CMD_WRITE 0x02 /* PAGE PROGRAM */
224 #define FLASH_CMD_WRITE_1_1_2 0xa2 /* DUAL INPUT PROGRAM */
225 #define FLASH_CMD_WRITE_1_2_2 0xd2 /* DUAL INPUT EXT PROGRAM */
226 #define FLASH_CMD_WRITE_1_1_4 0x32 /* QUAD INPUT PROGRAM */
227 #define FLASH_CMD_WRITE_1_4_4 0x12 /* QUAD INPUT EXT PROGRAM */
229 #define FLASH_CMD_EN4B_ADDR 0xb7 /* Enter 4-byte address mode */
230 #define FLASH_CMD_EX4B_ADDR 0xe9 /* Exit 4-byte address mode */
232 /* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
233 #define FLASH_CMD_READ4 0x13
234 #define FLASH_CMD_READ4_FAST 0x0c
235 #define FLASH_CMD_READ4_1_1_2 0x3c
236 #define FLASH_CMD_READ4_1_2_2 0xbc
237 #define FLASH_CMD_READ4_1_1_4 0x6c
238 #define FLASH_CMD_READ4_1_4_4 0xec
240 /* S25FLxxxS commands */
241 #define S25FL_CMD_WRITE4_1_1_4 0x34
242 #define S25FL_CMD_SE4 0xdc
243 #define S25FL_CMD_CLSR 0x30
244 #define S25FL_CMD_DYBWR 0xe1
245 #define S25FL_CMD_DYBRD 0xe0
246 #define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
247 * 'FLASH_CMD_WRITE_1_4_4'
248 * as found on N25Qxxx devices! */
250 /* Status register */
251 #define FLASH_STATUS_BUSY 0x01
252 #define FLASH_STATUS_WEL 0x02
253 #define FLASH_STATUS_BP0 0x04
254 #define FLASH_STATUS_BP1 0x08
255 #define FLASH_STATUS_BP2 0x10
256 #define FLASH_STATUS_SRWP0 0x80
257 #define FLASH_STATUS_TIMEOUT 0xff
258 /* S25FL Error Flags */
259 #define S25FL_STATUS_E_ERR 0x20
260 #define S25FL_STATUS_P_ERR 0x40
262 #define N25Q_CMD_WRVCR 0x81
263 #define N25Q_CMD_RDVCR 0x85
264 #define N25Q_CMD_RDVECR 0x65
265 #define N25Q_CMD_RDNVCR 0xb5
266 #define N25Q_CMD_WRNVCR 0xb1
268 #define FLASH_PAGESIZE 256 /* In Bytes */
269 #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
270 #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
273 * Flags to tweak operation of default read/write/erase routines
275 #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
276 #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
277 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
278 #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
291 } __packed __aligned(4);
296 struct resource *region;
299 struct flash_info *info;
301 uint32_t configuration;
302 uint32_t fifo_dir_delay;
303 bool booted_from_spi;
307 struct stfsm_seq stfsm_seq_read;
308 struct stfsm_seq stfsm_seq_write;
309 struct stfsm_seq stfsm_seq_en_32bit_addr;
312 /* Parameters to configure a READ or WRITE FSM sequence */
313 struct seq_rw_config {
314 uint32_t flags; /* flags to support config */
315 uint8_t cmd; /* FLASH command */
316 int write; /* Write Sequence */
317 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
318 uint8_t data_pads; /* No. of data pads */
319 uint8_t mode_data; /* MODE data */
320 uint8_t mode_cycles; /* No. of MODE cycles */
321 uint8_t dummy_cycles; /* No. of DUMMY cycles */
324 /* SPI Flash Device Table */
328 * JEDEC id zero means "no ID" (most older chips); otherwise it has
329 * a high byte of zero plus three data bytes: the manufacturer id,
330 * then a two byte device id.
335 * The size listed here is what works with FLASH_CMD_SE, which isn't
336 * necessarily called a "sector" by the vendor.
338 unsigned sector_size;
342 * Note, where FAST_READ is supported, freq_max specifies the
343 * FAST_READ frequency, not the READ frequency.
346 int (*config)(struct stfsm *);
349 static int stfsm_n25q_config(struct stfsm *fsm);
350 static int stfsm_mx25_config(struct stfsm *fsm);
351 static int stfsm_s25fl_config(struct stfsm *fsm);
352 static int stfsm_w25q_config(struct stfsm *fsm);
354 static struct flash_info flash_types[] = {
356 * ST Microelectronics/Numonyx --
357 * (newer production versions may have feature updates
358 * (eg faster operating frequency)
360 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
361 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
362 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
363 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
364 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
365 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
366 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
368 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
369 FLASH_FLAG_READ_FAST | \
370 FLASH_FLAG_READ_1_1_2 | \
371 FLASH_FLAG_WRITE_1_1_2)
372 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
373 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
375 #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
376 FLASH_FLAG_READ_FAST | \
377 FLASH_FLAG_READ_1_1_2 | \
378 FLASH_FLAG_READ_1_2_2 | \
379 FLASH_FLAG_READ_1_1_4 | \
380 FLASH_FLAG_READ_1_4_4 | \
383 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
384 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
386 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
387 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
390 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
391 FLASH_FLAG_READ_FAST | \
392 FLASH_FLAG_READ_1_1_2 | \
393 FLASH_FLAG_READ_1_2_2 | \
394 FLASH_FLAG_READ_1_1_4 | \
395 FLASH_FLAG_READ_1_4_4 | \
396 FLASH_FLAG_WRITE_1_1_2 | \
397 FLASH_FLAG_WRITE_1_2_2 | \
398 FLASH_FLAG_WRITE_1_1_4 | \
399 FLASH_FLAG_WRITE_1_4_4)
400 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
402 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
403 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
407 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
409 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
410 FLASH_FLAG_READ_1_1_2 | \
411 FLASH_FLAG_READ_1_2_2 | \
412 FLASH_FLAG_READ_1_1_4 | \
413 FLASH_FLAG_READ_1_4_4 | \
414 FLASH_FLAG_WRITE_1_1_4 | \
415 FLASH_FLAG_READ_FAST)
416 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
418 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
419 stfsm_s25fl_config },
420 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
421 stfsm_s25fl_config },
425 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
426 * - RESET# signal supported by die but not bristled out on all
427 * package types. The package type is a function of board design,
428 * so this information is captured in the board's flags.
429 * - Supports 'DYB' sector protection. Depending on variant, sectors
430 * may default to locked state on power-on.
432 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
434 FLASH_FLAG_DYB_LOCKING)
435 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
436 stfsm_s25fl_config },
437 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
438 stfsm_s25fl_config },
439 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
440 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
441 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
442 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
444 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
445 #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
446 FLASH_FLAG_READ_FAST | \
447 FLASH_FLAG_READ_1_1_2 | \
448 FLASH_FLAG_WRITE_1_1_2)
449 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
450 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
451 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
452 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
453 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
455 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
456 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
457 FLASH_FLAG_READ_FAST | \
458 FLASH_FLAG_READ_1_1_2 | \
459 FLASH_FLAG_READ_1_2_2 | \
460 FLASH_FLAG_READ_1_1_4 | \
461 FLASH_FLAG_READ_1_4_4 | \
462 FLASH_FLAG_WRITE_1_1_4)
463 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
465 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
467 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
469 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
473 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
477 * FSM message sequence configurations:
479 * All configs are presented in order of preference
482 /* Default READ configurations, in order of preference */
483 static struct seq_rw_config default_read_configs[] = {
484 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
485 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
486 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
487 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
488 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
489 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
490 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
493 /* Default WRITE configurations */
494 static struct seq_rw_config default_write_configs[] = {
495 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
496 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
497 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
498 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
499 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
500 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
504 * [N25Qxxx] Configuration
506 #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
507 #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
508 #define N25Q_VCR_WRAP_CONT 0x3
510 /* N25Q 3-byte Address READ configurations
511 * - 'FAST' variants configured for 8 dummy cycles.
513 * Note, the number of dummy cycles used for 'FAST' READ operations is
514 * configurable and would normally be tuned according to the READ command and
515 * operating frequency. However, this applies universally to all 'FAST' READ
516 * commands, including those used by the SPIBoot controller, and remains in
517 * force until the device is power-cycled. Since the SPIBoot controller is
518 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
521 static struct seq_rw_config n25q_read3_configs[] = {
522 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
523 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
524 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
525 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
526 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
527 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
528 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
531 /* N25Q 4-byte Address READ configurations
532 * - use special 4-byte address READ commands (reduces overheads, and
533 * reduces risk of hitting watchdog reset issues).
534 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
536 static struct seq_rw_config n25q_read4_configs[] = {
537 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
538 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
539 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
540 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
541 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
542 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
543 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
547 * [MX25xxx] Configuration
549 #define MX25_STATUS_QE (0x1 << 6)
551 static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
553 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
555 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR) |
558 seq->seq[0] = STFSM_INST_CMD1;
559 seq->seq[1] = STFSM_INST_WAIT;
560 seq->seq[2] = STFSM_INST_STOP;
562 seq->seq_cfg = (SEQ_CFG_PADS_1 |
564 SEQ_CFG_READNOTWRITE |
572 * [S25FLxxx] Configuration
574 #define STFSM_S25FL_CONFIG_QE (0x1 << 1)
577 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
578 * Register, Extended Address Modes, and a 32-bit address command set. The
579 * 32-bit address command set is used here, since it avoids any problems with
580 * entering a state that is incompatible with the SPIBoot Controller.
582 static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
583 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
584 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
585 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
586 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
587 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
588 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
589 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
592 static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
593 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
594 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
595 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
599 * [W25Qxxx] Configuration
601 #define W25Q_STATUS_QE (0x1 << 1)
603 static struct stfsm_seq stfsm_seq_read_jedec = {
604 .data_size = TRANSFER_SIZE(8),
605 .seq_opc[0] = (SEQ_OPC_PADS_1 |
607 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
610 STFSM_INST_DATA_READ,
613 .seq_cfg = (SEQ_CFG_PADS_1 |
614 SEQ_CFG_READNOTWRITE |
619 static struct stfsm_seq stfsm_seq_read_status_fifo = {
620 .data_size = TRANSFER_SIZE(4),
621 .seq_opc[0] = (SEQ_OPC_PADS_1 |
623 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
626 STFSM_INST_DATA_READ,
629 .seq_cfg = (SEQ_CFG_PADS_1 |
630 SEQ_CFG_READNOTWRITE |
635 static struct stfsm_seq stfsm_seq_erase_sector = {
636 /* 'addr_cfg' configured during initialisation */
638 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
639 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
641 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
642 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
651 .seq_cfg = (SEQ_CFG_PADS_1 |
652 SEQ_CFG_READNOTWRITE |
657 static struct stfsm_seq stfsm_seq_erase_chip = {
659 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
660 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
662 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
663 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
671 .seq_cfg = (SEQ_CFG_PADS_1 |
673 SEQ_CFG_READNOTWRITE |
678 static struct stfsm_seq stfsm_seq_write_status = {
679 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
680 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
681 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
682 SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
689 .seq_cfg = (SEQ_CFG_PADS_1 |
690 SEQ_CFG_READNOTWRITE |
695 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
697 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
698 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
699 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
700 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
703 seq->seq[0] = STFSM_INST_CMD2;
704 seq->seq[1] = STFSM_INST_CMD1;
705 seq->seq[2] = STFSM_INST_WAIT;
706 seq->seq[3] = STFSM_INST_STOP;
708 seq->seq_cfg = (SEQ_CFG_PADS_1 |
710 SEQ_CFG_READNOTWRITE |
717 static inline int stfsm_is_idle(struct stfsm *fsm)
719 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
722 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
724 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
727 static void stfsm_clear_fifo(struct stfsm *fsm)
732 avail = stfsm_fifo_available(fsm);
737 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
743 static inline void stfsm_load_seq(struct stfsm *fsm,
744 const struct stfsm_seq *seq)
746 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
747 const uint32_t *src = (const uint32_t *)seq;
748 int words = sizeof(*seq) / sizeof(*src);
750 BUG_ON(!stfsm_is_idle(fsm));
759 static void stfsm_wait_seq(struct stfsm *fsm)
761 unsigned long deadline;
764 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
767 if (time_after_eq(jiffies, deadline))
770 if (stfsm_is_idle(fsm))
776 dev_err(fsm->dev, "timeout on sequence completion\n");
779 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
781 uint32_t remaining = size >> 2;
785 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
787 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
791 avail = stfsm_fifo_available(fsm);
796 words = min(avail, remaining);
799 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
804 static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
807 uint32_t words = size >> 2;
809 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
811 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
813 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
818 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
820 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
821 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
823 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
825 SEQ_OPC_OPCODE(cmd) |
828 stfsm_load_seq(fsm, seq);
835 static uint8_t stfsm_wait_busy(struct stfsm *fsm)
837 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
838 unsigned long deadline;
843 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
845 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
847 /* Load read_status sequence */
848 stfsm_load_seq(fsm, seq);
851 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
853 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
855 if (time_after_eq(jiffies, deadline))
860 stfsm_read_fifo(fsm, &status, 4);
862 if ((status & FLASH_STATUS_BUSY) == 0)
865 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
866 ((status & S25FL_STATUS_P_ERR) ||
867 (status & S25FL_STATUS_E_ERR)))
868 return (uint8_t)(status & 0xff);
872 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
877 dev_err(fsm->dev, "timeout on wait_busy\n");
879 return FLASH_STATUS_TIMEOUT;
882 static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
883 uint8_t *data, int bytes)
885 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
887 uint8_t *t = (uint8_t *)&tmp;
890 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
893 BUG_ON(bytes != 1 && bytes != 2);
895 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
896 SEQ_OPC_OPCODE(cmd)),
898 stfsm_load_seq(fsm, seq);
900 stfsm_read_fifo(fsm, &tmp, 4);
902 for (i = 0; i < bytes; i++)
910 static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
911 uint16_t data, int bytes, int wait_busy)
913 struct stfsm_seq *seq = &stfsm_seq_write_status;
916 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
917 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
919 BUG_ON(bytes != 1 && bytes != 2);
921 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
922 SEQ_OPC_OPCODE(cmd));
924 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
925 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
927 stfsm_load_seq(fsm, seq);
932 stfsm_wait_busy(fsm);
938 * SoC reset on 'boot-from-spi' systems
940 * Certain modes of operation cause the Flash device to enter a particular state
941 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
942 * Addr' commands). On boot-from-spi systems, it is important to consider what
943 * happens if a warm reset occurs during this period. The SPIBoot controller
944 * assumes that Flash device is in its default reset state, 24-bit address mode,
945 * and ready to accept commands. This can be achieved using some form of
946 * on-board logic/controller to force a device POR in response to a SoC-level
947 * reset or by making use of the device reset signal if available (limited
948 * number of devices only).
950 * Failure to take such precautions can cause problems following a warm reset.
951 * For some operations (e.g. ERASE), there is little that can be done. For
952 * other modes of operation (e.g. 32-bit addressing), options are often
953 * available that can help minimise the window in which a reset could cause a
957 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
959 /* Reset signal is available on the board and supported by the device */
960 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
963 /* Board-level logic forces a power-on-reset */
967 /* Reset is not properly handled and may result in failure to reboot */
971 /* Configure 'addr_cfg' according to addressing mode */
972 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
973 struct stfsm_seq *seq)
975 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
977 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
978 ADR_CFG_PADS_1_ADD1 |
979 ADR_CFG_CYCLES_ADD2(16) |
980 ADR_CFG_PADS_1_ADD2 |
981 ADR_CFG_CSDEASSERT_ADD2);
984 /* Search for preferred configuration based on available flags */
985 static struct seq_rw_config *
986 stfsm_search_seq_rw_configs(struct stfsm *fsm,
987 struct seq_rw_config cfgs[])
989 struct seq_rw_config *config;
990 int flags = fsm->info->flags;
992 for (config = cfgs; config->cmd != 0; config++)
993 if ((config->flags & flags) == config->flags)
999 /* Prepare a READ/WRITE sequence according to configuration parameters */
1000 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1001 struct stfsm_seq *seq,
1002 struct seq_rw_config *cfg)
1004 int addr1_cycles, addr2_cycles;
1007 memset(seq, 0, sizeof(*seq));
1009 /* Add READ/WRITE OPC */
1010 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1012 SEQ_OPC_OPCODE(cfg->cmd));
1014 /* Add WREN OPC for a WRITE sequence */
1016 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1018 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1019 SEQ_OPC_CSDEASSERT);
1021 /* Address configuration (24 or 32-bit addresses) */
1022 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1023 addr1_cycles /= cfg->addr_pads;
1024 addr2_cycles = 16 / cfg->addr_pads;
1025 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
1026 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
1027 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
1028 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1030 /* Data/Sequence configuration */
1031 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1033 SEQ_CFG_CSDEASSERT);
1035 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1037 /* Mode configuration (no. of pads taken from addr cfg) */
1038 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1039 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1040 (cfg->addr_pads - 1) << 22); /* pads */
1042 /* Dummy configuration (no. of pads taken from addr cfg) */
1043 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1044 (cfg->addr_pads - 1) << 22); /* pads */
1047 /* Instruction sequence */
1050 seq->seq[i++] = STFSM_INST_CMD2;
1052 seq->seq[i++] = STFSM_INST_CMD1;
1054 seq->seq[i++] = STFSM_INST_ADD1;
1055 seq->seq[i++] = STFSM_INST_ADD2;
1057 if (cfg->mode_cycles)
1058 seq->seq[i++] = STFSM_INST_MODE;
1060 if (cfg->dummy_cycles)
1061 seq->seq[i++] = STFSM_INST_DUMMY;
1064 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1065 seq->seq[i++] = STFSM_INST_STOP;
1068 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1069 struct stfsm_seq *seq,
1070 struct seq_rw_config *cfgs)
1072 struct seq_rw_config *config;
1074 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1076 dev_err(fsm->dev, "failed to find suitable config\n");
1080 stfsm_prepare_rw_seq(fsm, seq, config);
1085 /* Prepare a READ/WRITE/ERASE 'default' sequences */
1086 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1088 uint32_t flags = fsm->info->flags;
1091 /* Configure 'READ' sequence */
1092 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1093 default_read_configs);
1096 "failed to prep READ sequence with flags [0x%08x]\n",
1101 /* Configure 'WRITE' sequence */
1102 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1103 default_write_configs);
1106 "failed to prep WRITE sequence with flags [0x%08x]\n",
1111 /* Configure 'ERASE_SECTOR' sequence */
1112 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1117 static int stfsm_mx25_config(struct stfsm *fsm)
1119 uint32_t flags = fsm->info->flags;
1126 * Use default READ/WRITE sequences
1128 ret = stfsm_prepare_rwe_seqs_default(fsm);
1133 * Configure 32-bit Address Support
1135 if (flags & FLASH_FLAG_32BIT_ADDR) {
1136 /* Configure 'enter_32bitaddr' FSM sequence */
1137 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1139 soc_reset = stfsm_can_handle_soc_reset(fsm);
1140 if (soc_reset || !fsm->booted_from_spi)
1141 /* If we can handle SoC resets, we enable 32-bit address
1142 * mode pervasively */
1143 stfsm_enter_32bit_addr(fsm, 1);
1146 /* Else, enable/disable 32-bit addressing before/after
1148 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1149 CFG_WRITE_TOGGLE_32BIT_ADDR |
1150 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1153 /* Check status of 'QE' bit, update if required. */
1154 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta, 1);
1155 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1156 if (data_pads == 4) {
1157 if (!(sta & MX25_STATUS_QE)) {
1159 sta |= MX25_STATUS_QE;
1161 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
1164 if (sta & MX25_STATUS_QE) {
1166 sta &= ~MX25_STATUS_QE;
1168 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
1175 static int stfsm_n25q_config(struct stfsm *fsm)
1177 uint32_t flags = fsm->info->flags;
1182 /* Configure 'READ' sequence */
1183 if (flags & FLASH_FLAG_32BIT_ADDR)
1184 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1185 n25q_read4_configs);
1187 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1188 n25q_read3_configs);
1191 "failed to prepare READ sequence with flags [0x%08x]\n",
1196 /* Configure 'WRITE' sequence (default configs) */
1197 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1198 default_write_configs);
1201 "preparing WRITE sequence using flags [0x%08x] failed\n",
1206 /* * Configure 'ERASE_SECTOR' sequence */
1207 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1209 /* Configure 32-bit address support */
1210 if (flags & FLASH_FLAG_32BIT_ADDR) {
1211 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1213 soc_reset = stfsm_can_handle_soc_reset(fsm);
1214 if (soc_reset || !fsm->booted_from_spi) {
1216 * If we can handle SoC resets, we enable 32-bit
1217 * address mode pervasively
1219 stfsm_enter_32bit_addr(fsm, 1);
1222 * If not, enable/disable for WRITE and ERASE
1223 * operations (READ uses special commands)
1225 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1226 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1231 * Configure device to use 8 dummy cycles
1233 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1234 N25Q_VCR_WRAP_CONT);
1235 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
1240 static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1242 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1244 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1246 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1247 ADR_CFG_PADS_1_ADD1 |
1248 ADR_CFG_CYCLES_ADD2(16) |
1249 ADR_CFG_PADS_1_ADD2 |
1250 ADR_CFG_CSDEASSERT_ADD2);
1253 static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1256 struct stfsm_seq seq = {
1257 .data_size = TRANSFER_SIZE(4),
1258 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1260 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1261 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1262 ADR_CFG_PADS_1_ADD1 |
1263 ADR_CFG_CYCLES_ADD2(16) |
1264 ADR_CFG_PADS_1_ADD2),
1265 .addr1 = (offs >> 16) & 0xffff,
1266 .addr2 = offs & 0xffff,
1271 STFSM_INST_DATA_READ,
1274 .seq_cfg = (SEQ_CFG_PADS_1 |
1275 SEQ_CFG_READNOTWRITE |
1276 SEQ_CFG_CSDEASSERT |
1280 stfsm_load_seq(fsm, &seq);
1282 stfsm_read_fifo(fsm, &tmp, 4);
1284 *dby = (uint8_t)(tmp >> 24);
1286 stfsm_wait_seq(fsm);
1289 static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1291 struct stfsm_seq seq = {
1292 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1293 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1294 SEQ_OPC_CSDEASSERT),
1295 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1296 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1297 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1298 ADR_CFG_PADS_1_ADD1 |
1299 ADR_CFG_CYCLES_ADD2(16) |
1300 ADR_CFG_PADS_1_ADD2),
1301 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1302 .addr1 = (offs >> 16) & 0xffff,
1303 .addr2 = offs & 0xffff,
1312 .seq_cfg = (SEQ_CFG_PADS_1 |
1313 SEQ_CFG_READNOTWRITE |
1314 SEQ_CFG_CSDEASSERT |
1318 stfsm_load_seq(fsm, &seq);
1319 stfsm_wait_seq(fsm);
1321 stfsm_wait_busy(fsm);
1324 static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1326 struct stfsm_seq seq = {
1327 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1329 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1330 SEQ_OPC_CSDEASSERT),
1331 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1333 SEQ_OPC_OPCODE(FLASH_CMD_WRDI) |
1334 SEQ_OPC_CSDEASSERT),
1341 .seq_cfg = (SEQ_CFG_PADS_1 |
1343 SEQ_CFG_READNOTWRITE |
1344 SEQ_CFG_CSDEASSERT |
1348 stfsm_load_seq(fsm, &seq);
1350 stfsm_wait_seq(fsm);
1355 static int stfsm_s25fl_config(struct stfsm *fsm)
1357 struct flash_info *info = fsm->info;
1358 uint32_t flags = info->flags;
1362 uint8_t sr1, cr1, dyb;
1366 if (flags & FLASH_FLAG_32BIT_ADDR) {
1368 * Prepare Read/Write/Erase sequences according to S25FLxxx
1369 * 32-bit address command set
1371 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1372 stfsm_s25fl_read4_configs);
1376 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1377 stfsm_s25fl_write4_configs);
1381 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1384 /* Use default configurations for 24-bit addressing */
1385 ret = stfsm_prepare_rwe_seqs_default(fsm);
1391 * For devices that support 'DYB' sector locking, check lock status and
1392 * unlock sectors if necessary (some variants power-on with sectors
1393 * locked by default)
1395 if (flags & FLASH_FLAG_DYB_LOCKING) {
1397 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1398 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1400 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1402 /* Handle bottom/top 4KiB parameter sectors */
1403 if ((offs < info->sector_size * 2) ||
1404 (offs >= (info->sector_size - info->n_sectors * 4)))
1411 /* Check status of 'QE' bit, update if required. */
1412 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &cr1, 1);
1413 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1414 if (data_pads == 4) {
1415 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1417 cr1 |= STFSM_S25FL_CONFIG_QE;
1422 if (cr1 & STFSM_S25FL_CONFIG_QE) {
1424 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1430 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1431 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1432 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta_wr, 2, 1);
1436 * S25FLxxx devices support Program and Error error flags.
1437 * Configure driver to check flags and clear if necessary.
1439 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1444 static int stfsm_w25q_config(struct stfsm *fsm)
1452 ret = stfsm_prepare_rwe_seqs_default(fsm);
1456 /* Check status of 'QE' bit, update if required. */
1457 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &sr2, 1);
1458 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1459 if (data_pads == 4) {
1460 if (!(sr2 & W25Q_STATUS_QE)) {
1462 sr2 |= W25Q_STATUS_QE;
1466 if (sr2 & W25Q_STATUS_QE) {
1468 sr2 &= ~W25Q_STATUS_QE;
1473 /* Write status register */
1474 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1475 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1476 stfsm_write_status(fsm, FLASH_CMD_WRSR, sr_wr, 2, 1);
1482 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1485 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
1492 uint32_t page_buf[FLASH_PAGESIZE_32];
1495 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1497 /* Enter 32-bit address mode, if required */
1498 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1499 stfsm_enter_32bit_addr(fsm, 1);
1501 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1502 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1503 read_mask = (data_pads << 2) - 1;
1505 /* Handle non-aligned buf */
1506 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1508 /* Handle non-aligned size */
1509 size_ub = (size + read_mask) & ~read_mask;
1510 size_lb = size & ~read_mask;
1511 size_mop = size & read_mask;
1513 seq->data_size = TRANSFER_SIZE(size_ub);
1514 seq->addr1 = (offset >> 16) & 0xffff;
1515 seq->addr2 = offset & 0xffff;
1517 stfsm_load_seq(fsm, seq);
1520 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1523 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1524 memcpy(p + size_lb, &tmp, size_mop);
1527 /* Handle non-aligned buf */
1528 if ((uint32_t)buf & 0x3)
1529 memcpy(buf, page_buf, size);
1531 /* Wait for sequence to finish */
1532 stfsm_wait_seq(fsm);
1534 stfsm_clear_fifo(fsm);
1536 /* Exit 32-bit address mode, if required */
1537 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1538 stfsm_enter_32bit_addr(fsm, 0);
1543 static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1544 uint32_t size, uint32_t offset)
1546 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
1548 uint32_t write_mask;
1553 uint32_t page_buf[FLASH_PAGESIZE_32];
1554 uint8_t *t = (uint8_t *)&tmp;
1559 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1561 /* Enter 32-bit address mode, if required */
1562 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1563 stfsm_enter_32bit_addr(fsm, 1);
1565 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1566 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1567 write_mask = (data_pads << 2) - 1;
1569 /* Handle non-aligned buf */
1570 if ((uint32_t)buf & 0x3) {
1571 memcpy(page_buf, buf, size);
1572 p = (uint8_t *)page_buf;
1577 /* Handle non-aligned size */
1578 size_ub = (size + write_mask) & ~write_mask;
1579 size_lb = size & ~write_mask;
1580 size_mop = size & write_mask;
1582 seq->data_size = TRANSFER_SIZE(size_ub);
1583 seq->addr1 = (offset >> 16) & 0xffff;
1584 seq->addr2 = offset & 0xffff;
1586 /* Need to set FIFO to write mode, before writing data to FIFO (see
1589 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1592 * Before writing data to the FIFO, apply a small delay to allow a
1593 * potential change of FIFO direction to complete.
1595 if (fsm->fifo_dir_delay == 0)
1596 readl(fsm->base + SPI_FAST_SEQ_CFG);
1598 udelay(fsm->fifo_dir_delay);
1601 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1603 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1607 /* Handle non-aligned size */
1609 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1610 for (i = 0; i < size_mop; i++)
1613 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1616 /* Start sequence */
1617 stfsm_load_seq(fsm, seq);
1619 /* Wait for sequence to finish */
1620 stfsm_wait_seq(fsm);
1622 /* Wait for completion */
1623 ret = stfsm_wait_busy(fsm);
1624 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1625 stfsm_s25fl_clear_status_reg(fsm);
1627 /* Exit 32-bit address mode, if required */
1628 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1629 stfsm_enter_32bit_addr(fsm, 0);
1635 * Read an address range from the flash chip. The address range
1636 * may be any size provided it is within the physical boundaries.
1638 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1639 size_t *retlen, u_char *buf)
1641 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1644 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1645 __func__, (u32)from, len);
1647 mutex_lock(&fsm->lock);
1650 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1652 stfsm_read(fsm, buf, bytes, from);
1661 mutex_unlock(&fsm->lock);
1666 static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
1668 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1671 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1673 /* Enter 32-bit address mode, if required */
1674 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1675 stfsm_enter_32bit_addr(fsm, 1);
1677 seq->addr1 = (offset >> 16) & 0xffff;
1678 seq->addr2 = offset & 0xffff;
1680 stfsm_load_seq(fsm, seq);
1682 stfsm_wait_seq(fsm);
1684 /* Wait for completion */
1685 ret = stfsm_wait_busy(fsm);
1686 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1687 stfsm_s25fl_clear_status_reg(fsm);
1689 /* Exit 32-bit address mode, if required */
1690 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1691 stfsm_enter_32bit_addr(fsm, 0);
1696 static int stfsm_erase_chip(struct stfsm *fsm)
1698 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1700 dev_dbg(fsm->dev, "erasing chip\n");
1702 stfsm_load_seq(fsm, seq);
1704 stfsm_wait_seq(fsm);
1706 return stfsm_wait_busy(fsm);
1710 * Write an address range to the flash chip. Data must be written in
1711 * FLASH_PAGESIZE chunks. The address range may be any size provided
1712 * it is within the physical boundaries.
1714 static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1715 size_t *retlen, const u_char *buf)
1717 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1721 uint8_t *b = (uint8_t *)buf;
1724 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1726 /* Offset within page */
1727 page_offs = to % FLASH_PAGESIZE;
1729 mutex_lock(&fsm->lock);
1732 /* Write up to page boundary */
1733 bytes = min(FLASH_PAGESIZE - page_offs, len);
1735 ret = stfsm_write(fsm, b, bytes, to);
1743 /* We are now page-aligned */
1751 mutex_unlock(&fsm->lock);
1757 * Erase an address range on the flash chip. The address range may extend
1758 * one or more erase sectors. Return an error is there is a problem erasing.
1760 static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1762 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1766 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1767 (long long)instr->addr, (long long)instr->len);
1772 mutex_lock(&fsm->lock);
1774 /* Whole-chip erase? */
1775 if (len == mtd->size) {
1776 ret = stfsm_erase_chip(fsm);
1781 ret = stfsm_erase_sector(fsm, addr);
1785 addr += mtd->erasesize;
1786 len -= mtd->erasesize;
1790 mutex_unlock(&fsm->lock);
1792 instr->state = MTD_ERASE_DONE;
1793 mtd_erase_callback(instr);
1798 instr->state = MTD_ERASE_FAILED;
1799 mutex_unlock(&fsm->lock);
1804 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
1806 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1809 stfsm_load_seq(fsm, seq);
1811 stfsm_read_fifo(fsm, tmp, 8);
1813 memcpy(jedec, tmp, 5);
1815 stfsm_wait_seq(fsm);
1818 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1820 struct flash_info *info;
1825 stfsm_read_jedec(fsm, id);
1827 jedec = id[0] << 16 | id[1] << 8 | id[2];
1829 * JEDEC also defines an optional "extended device information"
1830 * string for after vendor-specific data, after the three bytes
1831 * we use here. Supporting some chips might require using it.
1833 ext_jedec = id[3] << 8 | id[4];
1835 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1836 jedec, id[0], id[1], id[2], id[3], id[4]);
1838 for (info = flash_types; info->name; info++) {
1839 if (info->jedec_id == jedec) {
1840 if (info->ext_id && info->ext_id != ext_jedec)
1845 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1850 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1852 int ret, timeout = 10;
1854 /* Wait for controller to accept mode change */
1856 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1865 writel(mode, fsm->base + SPI_MODESELECT);
1870 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1875 /* TODO: Make this dynamic */
1876 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1879 * Calculate clk_div - values between 2 and 128
1880 * Multiple of 2, rounded up
1882 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1885 else if (clk_div > 128)
1889 * Determine a suitable delay for the IP to complete a change of
1890 * direction of the FIFO. The required delay is related to the clock
1891 * divider used. The following heuristics are based on empirical tests,
1892 * using a 100MHz EMI clock.
1895 fsm->fifo_dir_delay = 0;
1896 else if (clk_div <= 10)
1897 fsm->fifo_dir_delay = 1;
1899 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1901 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1902 emi_freq, spi_freq, clk_div);
1904 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1907 static int stfsm_init(struct stfsm *fsm)
1911 /* Perform a soft reset of the FSM controller */
1912 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1914 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1916 /* Set clock to 'safe' frequency initially */
1917 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1920 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1924 /* Set timing parameters */
1925 writel(SPI_CFG_DEVICE_ST |
1926 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1927 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1928 SPI_CFG_DEFAULT_DATA_HOLD,
1929 fsm->base + SPI_CONFIGDATA);
1930 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1933 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1934 * our purposes, the WAIT instruction is used purely to achieve
1935 * "sequence validity" rather than actually implement a delay.
1937 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1939 /* Clear FIFO, just in case */
1940 stfsm_clear_fifo(fsm);
1945 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1947 struct stfsm *fsm = platform_get_drvdata(pdev);
1948 struct device_node *np = pdev->dev.of_node;
1949 struct regmap *regmap;
1950 uint32_t boot_device_reg;
1951 uint32_t boot_device_spi;
1952 uint32_t boot_device; /* Value we read from *boot_device_reg */
1955 /* Booting from SPI NOR Flash is the default */
1956 fsm->booted_from_spi = true;
1958 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1960 goto boot_device_fail;
1962 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1964 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1966 /* Where in the syscon the boot device information lives */
1967 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1969 goto boot_device_fail;
1971 /* Boot device value when booted from SPI NOR */
1972 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1974 goto boot_device_fail;
1976 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1978 goto boot_device_fail;
1980 if (boot_device != boot_device_spi)
1981 fsm->booted_from_spi = false;
1986 dev_warn(&pdev->dev,
1987 "failed to fetch boot device, assuming boot from SPI\n");
1990 static int stfsm_probe(struct platform_device *pdev)
1992 struct device_node *np = pdev->dev.of_node;
1993 struct mtd_part_parser_data ppdata;
1994 struct flash_info *info;
1995 struct resource *res;
2000 dev_err(&pdev->dev, "No DT found\n");
2003 ppdata.of_node = np;
2005 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2009 fsm->dev = &pdev->dev;
2011 platform_set_drvdata(pdev, fsm);
2013 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2015 dev_err(&pdev->dev, "Resource not found\n");
2019 fsm->base = devm_ioremap_resource(&pdev->dev, res);
2020 if (IS_ERR(fsm->base)) {
2022 "Failed to reserve memory region %pR\n", res);
2023 return PTR_ERR(fsm->base);
2026 mutex_init(&fsm->lock);
2028 ret = stfsm_init(fsm);
2030 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2034 stfsm_fetch_platform_configs(pdev);
2036 /* Detect SPI FLASH device */
2037 info = stfsm_jedec_probe(fsm);
2042 /* Use device size to determine address width */
2043 if (info->sector_size * info->n_sectors > 0x1000000)
2044 info->flags |= FLASH_FLAG_32BIT_ADDR;
2047 * Configure READ/WRITE/ERASE sequences according to platform and
2051 ret = info->config(fsm);
2055 ret = stfsm_prepare_rwe_seqs_default(fsm);
2060 fsm->mtd.name = info->name;
2061 fsm->mtd.dev.parent = &pdev->dev;
2062 fsm->mtd.type = MTD_NORFLASH;
2063 fsm->mtd.writesize = 4;
2064 fsm->mtd.writebufsize = fsm->mtd.writesize;
2065 fsm->mtd.flags = MTD_CAP_NORFLASH;
2066 fsm->mtd.size = info->sector_size * info->n_sectors;
2067 fsm->mtd.erasesize = info->sector_size;
2069 fsm->mtd._read = stfsm_mtd_read;
2070 fsm->mtd._write = stfsm_mtd_write;
2071 fsm->mtd._erase = stfsm_mtd_erase;
2073 dev_info(&pdev->dev,
2074 "Found serial flash device: %s\n"
2075 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2077 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2078 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
2080 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
2083 static int stfsm_remove(struct platform_device *pdev)
2085 struct stfsm *fsm = platform_get_drvdata(pdev);
2087 return mtd_device_unregister(&fsm->mtd);
2090 static struct of_device_id stfsm_match[] = {
2091 { .compatible = "st,spi-fsm", },
2094 MODULE_DEVICE_TABLE(of, stfsm_match);
2096 static struct platform_driver stfsm_driver = {
2097 .probe = stfsm_probe,
2098 .remove = stfsm_remove,
2100 .name = "st-spi-fsm",
2101 .owner = THIS_MODULE,
2102 .of_match_table = stfsm_match,
2105 module_platform_driver(stfsm_driver);
2107 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2108 MODULE_DESCRIPTION("ST SPI FSM driver");
2109 MODULE_LICENSE("GPL");