1 // SPDX-License-Identifier: GPL-2.0-only
3 * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
7 * Copyright (C) 2010-2014 STMicroelectronics Limited
9 * JEDEC probe based on drivers/mtd/devices/m25p80.c
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 #include <linux/platform_device.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/mtd/spi-nor.h>
19 #include <linux/sched.h>
20 #include <linux/delay.h>
23 #include <linux/clk.h>
25 #include "serial_flash_cmds.h"
28 * FSM SPI Controller Registers
30 #define SPI_CLOCKDIV 0x0010
31 #define SPI_MODESELECT 0x0018
32 #define SPI_CONFIGDATA 0x0020
33 #define SPI_STA_MODE_CHANGE 0x0028
34 #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
35 #define SPI_FAST_SEQ_ADD1 0x0104
36 #define SPI_FAST_SEQ_ADD2 0x0108
37 #define SPI_FAST_SEQ_ADD_CFG 0x010c
38 #define SPI_FAST_SEQ_OPC1 0x0110
39 #define SPI_FAST_SEQ_OPC2 0x0114
40 #define SPI_FAST_SEQ_OPC3 0x0118
41 #define SPI_FAST_SEQ_OPC4 0x011c
42 #define SPI_FAST_SEQ_OPC5 0x0120
43 #define SPI_MODE_BITS 0x0124
44 #define SPI_DUMMY_BITS 0x0128
45 #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
46 #define SPI_FAST_SEQ_1 0x0130
47 #define SPI_FAST_SEQ_2 0x0134
48 #define SPI_FAST_SEQ_3 0x0138
49 #define SPI_FAST_SEQ_4 0x013c
50 #define SPI_FAST_SEQ_CFG 0x0140
51 #define SPI_FAST_SEQ_STA 0x0144
52 #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
53 #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
54 #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
55 #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
56 #define SPI_PROGRAM_ERASE_TIME 0x0158
57 #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
58 #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
59 #define SPI_STATUS_WR_TIME_REG 0x0164
60 #define SPI_FAST_SEQ_DATA_REG 0x0300
63 * Register: SPI_MODESELECT
65 #define SPI_MODESELECT_CONTIG 0x01
66 #define SPI_MODESELECT_FASTREAD 0x02
67 #define SPI_MODESELECT_DUALIO 0x04
68 #define SPI_MODESELECT_FSM 0x08
69 #define SPI_MODESELECT_QUADBOOT 0x10
72 * Register: SPI_CONFIGDATA
74 #define SPI_CFG_DEVICE_ST 0x1
75 #define SPI_CFG_DEVICE_ATMEL 0x4
76 #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
77 #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
78 #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
80 #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
81 #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
82 #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
85 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
87 #define TRANSFER_SIZE(x) ((x) * 8)
90 * Register: SPI_FAST_SEQ_ADD_CFG
92 #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
93 #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
94 #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
95 #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
96 #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
97 #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
98 #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
99 #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
100 #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
101 #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
104 * Register: SPI_FAST_SEQ_n
106 #define SEQ_OPC_OPCODE(x) ((x) << 0)
107 #define SEQ_OPC_CYCLES(x) ((x) << 8)
108 #define SEQ_OPC_PADS_1 (0x0 << 14)
109 #define SEQ_OPC_PADS_2 (0x1 << 14)
110 #define SEQ_OPC_PADS_4 (0x3 << 14)
111 #define SEQ_OPC_CSDEASSERT (1 << 16)
114 * Register: SPI_FAST_SEQ_CFG
116 #define SEQ_CFG_STARTSEQ (1 << 0)
117 #define SEQ_CFG_SWRESET (1 << 5)
118 #define SEQ_CFG_CSDEASSERT (1 << 6)
119 #define SEQ_CFG_READNOTWRITE (1 << 7)
120 #define SEQ_CFG_ERASE (1 << 8)
121 #define SEQ_CFG_PADS_1 (0x0 << 16)
122 #define SEQ_CFG_PADS_2 (0x1 << 16)
123 #define SEQ_CFG_PADS_4 (0x3 << 16)
126 * Register: SPI_MODE_BITS
128 #define MODE_DATA(x) (x & 0xff)
129 #define MODE_CYCLES(x) ((x & 0x3f) << 16)
130 #define MODE_PADS_1 (0x0 << 22)
131 #define MODE_PADS_2 (0x1 << 22)
132 #define MODE_PADS_4 (0x3 << 22)
133 #define DUMMY_CSDEASSERT (1 << 24)
136 * Register: SPI_DUMMY_BITS
138 #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
139 #define DUMMY_PADS_1 (0x0 << 22)
140 #define DUMMY_PADS_2 (0x1 << 22)
141 #define DUMMY_PADS_4 (0x3 << 22)
142 #define DUMMY_CSDEASSERT (1 << 24)
145 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
147 #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
148 #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
149 #define STA_PADS_1 (0x0 << 16)
150 #define STA_PADS_2 (0x1 << 16)
151 #define STA_PADS_4 (0x3 << 16)
152 #define STA_CSDEASSERT (0x1 << 20)
153 #define STA_RDNOTWR (0x1 << 21)
156 * FSM SPI Instruction Opcodes
158 #define STFSM_OPC_CMD 0x1
159 #define STFSM_OPC_ADD 0x2
160 #define STFSM_OPC_STA 0x3
161 #define STFSM_OPC_MODE 0x4
162 #define STFSM_OPC_DUMMY 0x5
163 #define STFSM_OPC_DATA 0x6
164 #define STFSM_OPC_WAIT 0x7
165 #define STFSM_OPC_JUMP 0x8
166 #define STFSM_OPC_GOTO 0x9
167 #define STFSM_OPC_STOP 0xF
170 * FSM SPI Instructions (== opcode + operand).
172 #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
174 #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
175 #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
176 #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
177 #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
178 #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
179 #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
180 #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
182 #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
183 #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
185 #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
186 #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
187 #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
188 #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
190 #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
191 #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
192 #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
193 #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
195 #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
196 #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
198 #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
200 #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
202 /* S25FLxxxS commands */
203 #define S25FL_CMD_WRITE4_1_1_4 0x34
204 #define S25FL_CMD_SE4 0xdc
205 #define S25FL_CMD_CLSR 0x30
206 #define S25FL_CMD_DYBWR 0xe1
207 #define S25FL_CMD_DYBRD 0xe0
208 #define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
209 * 'SPINOR_OP_WRITE_1_4_4'
210 * as found on N25Qxxx devices! */
212 /* Status register */
213 #define FLASH_STATUS_BUSY 0x01
214 #define FLASH_STATUS_WEL 0x02
215 #define FLASH_STATUS_BP0 0x04
216 #define FLASH_STATUS_BP1 0x08
217 #define FLASH_STATUS_BP2 0x10
218 #define FLASH_STATUS_SRWP0 0x80
219 #define FLASH_STATUS_TIMEOUT 0xff
220 /* S25FL Error Flags */
221 #define S25FL_STATUS_E_ERR 0x20
222 #define S25FL_STATUS_P_ERR 0x40
224 #define N25Q_CMD_WRVCR 0x81
225 #define N25Q_CMD_RDVCR 0x85
226 #define N25Q_CMD_RDVECR 0x65
227 #define N25Q_CMD_RDNVCR 0xb5
228 #define N25Q_CMD_WRNVCR 0xb1
230 #define FLASH_PAGESIZE 256 /* In Bytes */
231 #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
232 #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
235 * Flags to tweak operation of default read/write/erase routines
237 #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
238 #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
239 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
240 #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
253 } __packed __aligned(4);
258 struct resource *region;
261 struct flash_info *info;
264 uint32_t configuration;
265 uint32_t fifo_dir_delay;
266 bool booted_from_spi;
270 struct stfsm_seq stfsm_seq_read;
271 struct stfsm_seq stfsm_seq_write;
272 struct stfsm_seq stfsm_seq_en_32bit_addr;
275 /* Parameters to configure a READ or WRITE FSM sequence */
276 struct seq_rw_config {
277 uint32_t flags; /* flags to support config */
278 uint8_t cmd; /* FLASH command */
279 int write; /* Write Sequence */
280 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
281 uint8_t data_pads; /* No. of data pads */
282 uint8_t mode_data; /* MODE data */
283 uint8_t mode_cycles; /* No. of MODE cycles */
284 uint8_t dummy_cycles; /* No. of DUMMY cycles */
287 /* SPI Flash Device Table */
291 * JEDEC id zero means "no ID" (most older chips); otherwise it has
292 * a high byte of zero plus three data bytes: the manufacturer id,
293 * then a two byte device id.
298 * The size listed here is what works with SPINOR_OP_SE, which isn't
299 * necessarily called a "sector" by the vendor.
301 unsigned sector_size;
305 * Note, where FAST_READ is supported, freq_max specifies the
306 * FAST_READ frequency, not the READ frequency.
309 int (*config)(struct stfsm *);
312 static int stfsm_n25q_config(struct stfsm *fsm);
313 static int stfsm_mx25_config(struct stfsm *fsm);
314 static int stfsm_s25fl_config(struct stfsm *fsm);
315 static int stfsm_w25q_config(struct stfsm *fsm);
317 static struct flash_info flash_types[] = {
319 * ST Microelectronics/Numonyx --
320 * (newer production versions may have feature updates
321 * (eg faster operating frequency)
323 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
324 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
325 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
326 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
327 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
328 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
329 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
331 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
332 FLASH_FLAG_READ_FAST | \
333 FLASH_FLAG_READ_1_1_2 | \
334 FLASH_FLAG_WRITE_1_1_2)
335 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
336 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
339 * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
340 * where operating frequency must be reduced.
342 #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
343 FLASH_FLAG_READ_FAST | \
344 FLASH_FLAG_READ_1_1_2 | \
345 FLASH_FLAG_READ_1_2_2 | \
346 FLASH_FLAG_READ_1_1_4 | \
349 { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
350 (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
352 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
353 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
355 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
356 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
359 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
360 FLASH_FLAG_READ_FAST | \
361 FLASH_FLAG_READ_1_1_2 | \
362 FLASH_FLAG_READ_1_2_2 | \
363 FLASH_FLAG_READ_1_1_4 | \
364 FLASH_FLAG_READ_1_4_4 | \
365 FLASH_FLAG_WRITE_1_1_2 | \
366 FLASH_FLAG_WRITE_1_2_2 | \
367 FLASH_FLAG_WRITE_1_1_4 | \
368 FLASH_FLAG_WRITE_1_4_4)
369 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
371 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
372 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
376 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
378 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
379 FLASH_FLAG_READ_1_1_2 | \
380 FLASH_FLAG_READ_1_2_2 | \
381 FLASH_FLAG_READ_1_1_4 | \
382 FLASH_FLAG_READ_1_4_4 | \
383 FLASH_FLAG_WRITE_1_1_4 | \
384 FLASH_FLAG_READ_FAST)
385 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
387 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
388 stfsm_s25fl_config },
389 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
390 stfsm_s25fl_config },
394 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
395 * - RESET# signal supported by die but not bristled out on all
396 * package types. The package type is a function of board design,
397 * so this information is captured in the board's flags.
398 * - Supports 'DYB' sector protection. Depending on variant, sectors
399 * may default to locked state on power-on.
401 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
403 FLASH_FLAG_DYB_LOCKING)
404 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
405 stfsm_s25fl_config },
406 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
407 stfsm_s25fl_config },
408 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
409 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
410 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
411 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
413 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
414 #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
415 FLASH_FLAG_READ_FAST | \
416 FLASH_FLAG_READ_1_1_2 | \
417 FLASH_FLAG_WRITE_1_1_2)
418 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
419 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
420 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
421 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
422 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
424 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
425 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
426 FLASH_FLAG_READ_FAST | \
427 FLASH_FLAG_READ_1_1_2 | \
428 FLASH_FLAG_READ_1_2_2 | \
429 FLASH_FLAG_READ_1_1_4 | \
430 FLASH_FLAG_READ_1_4_4 | \
431 FLASH_FLAG_WRITE_1_1_4)
432 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
434 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
436 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
438 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
442 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
446 * FSM message sequence configurations:
448 * All configs are presented in order of preference
451 /* Default READ configurations, in order of preference */
452 static struct seq_rw_config default_read_configs[] = {
453 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
454 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
455 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
456 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
457 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
458 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
459 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
462 /* Default WRITE configurations */
463 static struct seq_rw_config default_write_configs[] = {
464 {FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
465 {FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
466 {FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
467 {FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
468 {FLASH_FLAG_READ_WRITE, SPINOR_OP_WRITE, 1, 1, 1, 0x00, 0, 0},
469 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
473 * [N25Qxxx] Configuration
475 #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
476 #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
477 #define N25Q_VCR_WRAP_CONT 0x3
479 /* N25Q 3-byte Address READ configurations
480 * - 'FAST' variants configured for 8 dummy cycles.
482 * Note, the number of dummy cycles used for 'FAST' READ operations is
483 * configurable and would normally be tuned according to the READ command and
484 * operating frequency. However, this applies universally to all 'FAST' READ
485 * commands, including those used by the SPIBoot controller, and remains in
486 * force until the device is power-cycled. Since the SPIBoot controller is
487 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
490 static struct seq_rw_config n25q_read3_configs[] = {
491 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
492 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
493 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
494 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
495 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
496 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
497 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
500 /* N25Q 4-byte Address READ configurations
501 * - use special 4-byte address READ commands (reduces overheads, and
502 * reduces risk of hitting watchdog reset issues).
503 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
505 static struct seq_rw_config n25q_read4_configs[] = {
506 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B, 0, 4, 4, 0x00, 0, 8},
507 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B, 0, 1, 4, 0x00, 0, 8},
508 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B, 0, 2, 2, 0x00, 0, 8},
509 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B, 0, 1, 2, 0x00, 0, 8},
510 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST_4B, 0, 1, 1, 0x00, 0, 8},
511 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ_4B, 0, 1, 1, 0x00, 0, 0},
512 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
516 * [MX25xxx] Configuration
518 #define MX25_STATUS_QE (0x1 << 6)
520 static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
522 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
524 SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
527 seq->seq[0] = STFSM_INST_CMD1;
528 seq->seq[1] = STFSM_INST_WAIT;
529 seq->seq[2] = STFSM_INST_STOP;
531 seq->seq_cfg = (SEQ_CFG_PADS_1 |
533 SEQ_CFG_READNOTWRITE |
541 * [S25FLxxx] Configuration
543 #define STFSM_S25FL_CONFIG_QE (0x1 << 1)
546 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
547 * Register, Extended Address Modes, and a 32-bit address command set. The
548 * 32-bit address command set is used here, since it avoids any problems with
549 * entering a state that is incompatible with the SPIBoot Controller.
551 static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
552 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B, 0, 4, 4, 0x00, 2, 4},
553 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B, 0, 1, 4, 0x00, 0, 8},
554 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B, 0, 2, 2, 0x00, 4, 0},
555 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B, 0, 1, 2, 0x00, 0, 8},
556 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST_4B, 0, 1, 1, 0x00, 0, 8},
557 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ_4B, 0, 1, 1, 0x00, 0, 0},
558 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
561 static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
562 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
563 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
564 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
568 * [W25Qxxx] Configuration
570 #define W25Q_STATUS_QE (0x1 << 1)
572 static struct stfsm_seq stfsm_seq_read_jedec = {
573 .data_size = TRANSFER_SIZE(8),
574 .seq_opc[0] = (SEQ_OPC_PADS_1 |
576 SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
579 STFSM_INST_DATA_READ,
582 .seq_cfg = (SEQ_CFG_PADS_1 |
583 SEQ_CFG_READNOTWRITE |
588 static struct stfsm_seq stfsm_seq_read_status_fifo = {
589 .data_size = TRANSFER_SIZE(4),
590 .seq_opc[0] = (SEQ_OPC_PADS_1 |
592 SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
595 STFSM_INST_DATA_READ,
598 .seq_cfg = (SEQ_CFG_PADS_1 |
599 SEQ_CFG_READNOTWRITE |
604 static struct stfsm_seq stfsm_seq_erase_sector = {
605 /* 'addr_cfg' configured during initialisation */
607 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
608 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
610 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
611 SEQ_OPC_OPCODE(SPINOR_OP_SE)),
620 .seq_cfg = (SEQ_CFG_PADS_1 |
621 SEQ_CFG_READNOTWRITE |
626 static struct stfsm_seq stfsm_seq_erase_chip = {
628 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
629 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
631 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
632 SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
640 .seq_cfg = (SEQ_CFG_PADS_1 |
642 SEQ_CFG_READNOTWRITE |
647 static struct stfsm_seq stfsm_seq_write_status = {
648 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
649 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
650 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
651 SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
658 .seq_cfg = (SEQ_CFG_PADS_1 |
659 SEQ_CFG_READNOTWRITE |
664 /* Dummy sequence to read one byte of data from flash into the FIFO */
665 static const struct stfsm_seq stfsm_seq_load_fifo_byte = {
666 .data_size = TRANSFER_SIZE(1),
667 .seq_opc[0] = (SEQ_OPC_PADS_1 |
669 SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
672 STFSM_INST_DATA_READ,
675 .seq_cfg = (SEQ_CFG_PADS_1 |
676 SEQ_CFG_READNOTWRITE |
681 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
683 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
684 SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
685 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
686 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
689 seq->seq[0] = STFSM_INST_CMD2;
690 seq->seq[1] = STFSM_INST_CMD1;
691 seq->seq[2] = STFSM_INST_WAIT;
692 seq->seq[3] = STFSM_INST_STOP;
694 seq->seq_cfg = (SEQ_CFG_PADS_1 |
696 SEQ_CFG_READNOTWRITE |
703 static inline int stfsm_is_idle(struct stfsm *fsm)
705 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
708 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
710 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
713 static inline void stfsm_load_seq(struct stfsm *fsm,
714 const struct stfsm_seq *seq)
716 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
717 const uint32_t *src = (const uint32_t *)seq;
718 int words = sizeof(*seq) / sizeof(*src);
720 BUG_ON(!stfsm_is_idle(fsm));
729 static void stfsm_wait_seq(struct stfsm *fsm)
731 unsigned long deadline;
734 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
737 if (time_after_eq(jiffies, deadline))
740 if (stfsm_is_idle(fsm))
746 dev_err(fsm->dev, "timeout on sequence completion\n");
749 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
751 uint32_t remaining = size >> 2;
755 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
757 BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
761 avail = stfsm_fifo_available(fsm);
766 words = min(avail, remaining);
769 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
775 * Clear the data FIFO
777 * Typically, this is only required during driver initialisation, where no
778 * assumptions can be made regarding the state of the FIFO.
780 * The process of clearing the FIFO is complicated by fact that while it is
781 * possible for the FIFO to contain an arbitrary number of bytes [1], the
782 * SPI_FAST_SEQ_STA register only reports the number of complete 32-bit words
783 * present. Furthermore, data can only be drained from the FIFO by reading
784 * complete 32-bit words.
786 * With this in mind, a two stage process is used to the clear the FIFO:
788 * 1. Read any complete 32-bit words from the FIFO, as reported by the
789 * SPI_FAST_SEQ_STA register.
791 * 2. Mop up any remaining bytes. At this point, it is not known if there
792 * are 0, 1, 2, or 3 bytes in the FIFO. To handle all cases, a dummy FSM
793 * sequence is used to load one byte at a time, until a complete 32-bit
794 * word is formed; at most, 4 bytes will need to be loaded.
796 * [1] It is theoretically possible for the FIFO to contain an arbitrary number
797 * of bits. However, since there are no known use-cases that leave
798 * incomplete bytes in the FIFO, only words and bytes are considered here.
800 static void stfsm_clear_fifo(struct stfsm *fsm)
802 const struct stfsm_seq *seq = &stfsm_seq_load_fifo_byte;
805 /* 1. Clear any 32-bit words */
806 words = stfsm_fifo_available(fsm);
808 for (i = 0; i < words; i++)
809 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
810 dev_dbg(fsm->dev, "cleared %d words from FIFO\n", words);
814 * 2. Clear any remaining bytes
815 * - Load the FIFO, one byte at a time, until a complete 32-bit word
818 for (i = 0, words = 0; i < 4 && !words; i++) {
819 stfsm_load_seq(fsm, seq);
821 words = stfsm_fifo_available(fsm);
824 /* - A single word must be available now */
826 dev_err(fsm->dev, "failed to clear bytes from the data FIFO\n");
830 /* - Read the 32-bit word */
831 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
833 dev_dbg(fsm->dev, "cleared %d byte(s) from the data FIFO\n", 4 - i);
836 static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
839 uint32_t words = size >> 2;
841 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
843 BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
845 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
850 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
852 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
853 uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
855 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
857 SEQ_OPC_OPCODE(cmd) |
860 stfsm_load_seq(fsm, seq);
867 static uint8_t stfsm_wait_busy(struct stfsm *fsm)
869 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
870 unsigned long deadline;
875 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
877 SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
879 /* Load read_status sequence */
880 stfsm_load_seq(fsm, seq);
883 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
885 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
887 if (time_after_eq(jiffies, deadline))
892 stfsm_read_fifo(fsm, &status, 4);
894 if ((status & FLASH_STATUS_BUSY) == 0)
897 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
898 ((status & S25FL_STATUS_P_ERR) ||
899 (status & S25FL_STATUS_E_ERR)))
900 return (uint8_t)(status & 0xff);
904 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
909 dev_err(fsm->dev, "timeout on wait_busy\n");
911 return FLASH_STATUS_TIMEOUT;
914 static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
915 uint8_t *data, int bytes)
917 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
919 uint8_t *t = (uint8_t *)&tmp;
922 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
925 BUG_ON(bytes != 1 && bytes != 2);
927 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
928 SEQ_OPC_OPCODE(cmd)),
930 stfsm_load_seq(fsm, seq);
932 stfsm_read_fifo(fsm, &tmp, 4);
934 for (i = 0; i < bytes; i++)
942 static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
943 uint16_t data, int bytes, int wait_busy)
945 struct stfsm_seq *seq = &stfsm_seq_write_status;
948 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
949 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
951 BUG_ON(bytes != 1 && bytes != 2);
953 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
954 SEQ_OPC_OPCODE(cmd));
956 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
957 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
959 stfsm_load_seq(fsm, seq);
964 stfsm_wait_busy(fsm);
970 * SoC reset on 'boot-from-spi' systems
972 * Certain modes of operation cause the Flash device to enter a particular state
973 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
974 * Addr' commands). On boot-from-spi systems, it is important to consider what
975 * happens if a warm reset occurs during this period. The SPIBoot controller
976 * assumes that Flash device is in its default reset state, 24-bit address mode,
977 * and ready to accept commands. This can be achieved using some form of
978 * on-board logic/controller to force a device POR in response to a SoC-level
979 * reset or by making use of the device reset signal if available (limited
980 * number of devices only).
982 * Failure to take such precautions can cause problems following a warm reset.
983 * For some operations (e.g. ERASE), there is little that can be done. For
984 * other modes of operation (e.g. 32-bit addressing), options are often
985 * available that can help minimise the window in which a reset could cause a
989 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
991 /* Reset signal is available on the board and supported by the device */
992 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
995 /* Board-level logic forces a power-on-reset */
999 /* Reset is not properly handled and may result in failure to reboot */
1003 /* Configure 'addr_cfg' according to addressing mode */
1004 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
1005 struct stfsm_seq *seq)
1007 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
1009 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
1010 ADR_CFG_PADS_1_ADD1 |
1011 ADR_CFG_CYCLES_ADD2(16) |
1012 ADR_CFG_PADS_1_ADD2 |
1013 ADR_CFG_CSDEASSERT_ADD2);
1016 /* Search for preferred configuration based on available flags */
1017 static struct seq_rw_config *
1018 stfsm_search_seq_rw_configs(struct stfsm *fsm,
1019 struct seq_rw_config cfgs[])
1021 struct seq_rw_config *config;
1022 int flags = fsm->info->flags;
1024 for (config = cfgs; config->cmd != 0; config++)
1025 if ((config->flags & flags) == config->flags)
1031 /* Prepare a READ/WRITE sequence according to configuration parameters */
1032 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1033 struct stfsm_seq *seq,
1034 struct seq_rw_config *cfg)
1036 int addr1_cycles, addr2_cycles;
1039 memset(seq, 0, sizeof(*seq));
1041 /* Add READ/WRITE OPC */
1042 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1044 SEQ_OPC_OPCODE(cfg->cmd));
1046 /* Add WREN OPC for a WRITE sequence */
1048 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1050 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
1051 SEQ_OPC_CSDEASSERT);
1053 /* Address configuration (24 or 32-bit addresses) */
1054 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1055 addr1_cycles /= cfg->addr_pads;
1056 addr2_cycles = 16 / cfg->addr_pads;
1057 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
1058 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
1059 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
1060 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1062 /* Data/Sequence configuration */
1063 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1065 SEQ_CFG_CSDEASSERT);
1067 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1069 /* Mode configuration (no. of pads taken from addr cfg) */
1070 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1071 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1072 (cfg->addr_pads - 1) << 22); /* pads */
1074 /* Dummy configuration (no. of pads taken from addr cfg) */
1075 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1076 (cfg->addr_pads - 1) << 22); /* pads */
1079 /* Instruction sequence */
1082 seq->seq[i++] = STFSM_INST_CMD2;
1084 seq->seq[i++] = STFSM_INST_CMD1;
1086 seq->seq[i++] = STFSM_INST_ADD1;
1087 seq->seq[i++] = STFSM_INST_ADD2;
1089 if (cfg->mode_cycles)
1090 seq->seq[i++] = STFSM_INST_MODE;
1092 if (cfg->dummy_cycles)
1093 seq->seq[i++] = STFSM_INST_DUMMY;
1096 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1097 seq->seq[i++] = STFSM_INST_STOP;
1100 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1101 struct stfsm_seq *seq,
1102 struct seq_rw_config *cfgs)
1104 struct seq_rw_config *config;
1106 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1108 dev_err(fsm->dev, "failed to find suitable config\n");
1112 stfsm_prepare_rw_seq(fsm, seq, config);
1117 /* Prepare a READ/WRITE/ERASE 'default' sequences */
1118 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1120 uint32_t flags = fsm->info->flags;
1123 /* Configure 'READ' sequence */
1124 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1125 default_read_configs);
1128 "failed to prep READ sequence with flags [0x%08x]\n",
1133 /* Configure 'WRITE' sequence */
1134 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1135 default_write_configs);
1138 "failed to prep WRITE sequence with flags [0x%08x]\n",
1143 /* Configure 'ERASE_SECTOR' sequence */
1144 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1149 static int stfsm_mx25_config(struct stfsm *fsm)
1151 uint32_t flags = fsm->info->flags;
1158 * Use default READ/WRITE sequences
1160 ret = stfsm_prepare_rwe_seqs_default(fsm);
1165 * Configure 32-bit Address Support
1167 if (flags & FLASH_FLAG_32BIT_ADDR) {
1168 /* Configure 'enter_32bitaddr' FSM sequence */
1169 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1171 soc_reset = stfsm_can_handle_soc_reset(fsm);
1172 if (soc_reset || !fsm->booted_from_spi)
1173 /* If we can handle SoC resets, we enable 32-bit address
1174 * mode pervasively */
1175 stfsm_enter_32bit_addr(fsm, 1);
1178 /* Else, enable/disable 32-bit addressing before/after
1180 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1181 CFG_WRITE_TOGGLE_32BIT_ADDR |
1182 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1185 /* Check status of 'QE' bit, update if required. */
1186 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
1187 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1188 if (data_pads == 4) {
1189 if (!(sta & MX25_STATUS_QE)) {
1191 sta |= MX25_STATUS_QE;
1193 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1196 if (sta & MX25_STATUS_QE) {
1198 sta &= ~MX25_STATUS_QE;
1200 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1207 static int stfsm_n25q_config(struct stfsm *fsm)
1209 uint32_t flags = fsm->info->flags;
1214 /* Configure 'READ' sequence */
1215 if (flags & FLASH_FLAG_32BIT_ADDR)
1216 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1217 n25q_read4_configs);
1219 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1220 n25q_read3_configs);
1223 "failed to prepare READ sequence with flags [0x%08x]\n",
1228 /* Configure 'WRITE' sequence (default configs) */
1229 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1230 default_write_configs);
1233 "preparing WRITE sequence using flags [0x%08x] failed\n",
1238 /* * Configure 'ERASE_SECTOR' sequence */
1239 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1241 /* Configure 32-bit address support */
1242 if (flags & FLASH_FLAG_32BIT_ADDR) {
1243 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1245 soc_reset = stfsm_can_handle_soc_reset(fsm);
1246 if (soc_reset || !fsm->booted_from_spi) {
1248 * If we can handle SoC resets, we enable 32-bit
1249 * address mode pervasively
1251 stfsm_enter_32bit_addr(fsm, 1);
1254 * If not, enable/disable for WRITE and ERASE
1255 * operations (READ uses special commands)
1257 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1258 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1263 * Configure device to use 8 dummy cycles
1265 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1266 N25Q_VCR_WRAP_CONT);
1267 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
1272 static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1274 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1276 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1278 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1279 ADR_CFG_PADS_1_ADD1 |
1280 ADR_CFG_CYCLES_ADD2(16) |
1281 ADR_CFG_PADS_1_ADD2 |
1282 ADR_CFG_CSDEASSERT_ADD2);
1285 static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1288 struct stfsm_seq seq = {
1289 .data_size = TRANSFER_SIZE(4),
1290 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1292 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1293 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1294 ADR_CFG_PADS_1_ADD1 |
1295 ADR_CFG_CYCLES_ADD2(16) |
1296 ADR_CFG_PADS_1_ADD2),
1297 .addr1 = (offs >> 16) & 0xffff,
1298 .addr2 = offs & 0xffff,
1303 STFSM_INST_DATA_READ,
1306 .seq_cfg = (SEQ_CFG_PADS_1 |
1307 SEQ_CFG_READNOTWRITE |
1308 SEQ_CFG_CSDEASSERT |
1312 stfsm_load_seq(fsm, &seq);
1314 stfsm_read_fifo(fsm, &tmp, 4);
1316 *dby = (uint8_t)(tmp >> 24);
1318 stfsm_wait_seq(fsm);
1321 static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1323 struct stfsm_seq seq = {
1324 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1325 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
1326 SEQ_OPC_CSDEASSERT),
1327 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1328 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1329 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1330 ADR_CFG_PADS_1_ADD1 |
1331 ADR_CFG_CYCLES_ADD2(16) |
1332 ADR_CFG_PADS_1_ADD2),
1333 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1334 .addr1 = (offs >> 16) & 0xffff,
1335 .addr2 = offs & 0xffff,
1344 .seq_cfg = (SEQ_CFG_PADS_1 |
1345 SEQ_CFG_READNOTWRITE |
1346 SEQ_CFG_CSDEASSERT |
1350 stfsm_load_seq(fsm, &seq);
1351 stfsm_wait_seq(fsm);
1353 stfsm_wait_busy(fsm);
1356 static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1358 struct stfsm_seq seq = {
1359 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1361 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1362 SEQ_OPC_CSDEASSERT),
1363 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1365 SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
1366 SEQ_OPC_CSDEASSERT),
1373 .seq_cfg = (SEQ_CFG_PADS_1 |
1375 SEQ_CFG_READNOTWRITE |
1376 SEQ_CFG_CSDEASSERT |
1380 stfsm_load_seq(fsm, &seq);
1382 stfsm_wait_seq(fsm);
1387 static int stfsm_s25fl_config(struct stfsm *fsm)
1389 struct flash_info *info = fsm->info;
1390 uint32_t flags = info->flags;
1394 uint8_t sr1, cr1, dyb;
1398 if (flags & FLASH_FLAG_32BIT_ADDR) {
1400 * Prepare Read/Write/Erase sequences according to S25FLxxx
1401 * 32-bit address command set
1403 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1404 stfsm_s25fl_read4_configs);
1408 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1409 stfsm_s25fl_write4_configs);
1413 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1416 /* Use default configurations for 24-bit addressing */
1417 ret = stfsm_prepare_rwe_seqs_default(fsm);
1423 * For devices that support 'DYB' sector locking, check lock status and
1424 * unlock sectors if necessary (some variants power-on with sectors
1425 * locked by default)
1427 if (flags & FLASH_FLAG_DYB_LOCKING) {
1429 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1430 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1432 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1434 /* Handle bottom/top 4KiB parameter sectors */
1435 if ((offs < info->sector_size * 2) ||
1436 (offs >= (info->sector_size - info->n_sectors * 4)))
1443 /* Check status of 'QE' bit, update if required. */
1444 stfsm_read_status(fsm, SPINOR_OP_RDCR, &cr1, 1);
1445 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1446 if (data_pads == 4) {
1447 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1449 cr1 |= STFSM_S25FL_CONFIG_QE;
1454 if (cr1 & STFSM_S25FL_CONFIG_QE) {
1456 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1462 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
1463 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1464 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
1468 * S25FLxxx devices support Program and Error error flags.
1469 * Configure driver to check flags and clear if necessary.
1471 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1476 static int stfsm_w25q_config(struct stfsm *fsm)
1484 ret = stfsm_prepare_rwe_seqs_default(fsm);
1488 /* Check status of 'QE' bit, update if required. */
1489 stfsm_read_status(fsm, SPINOR_OP_RDCR, &sr2, 1);
1490 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1491 if (data_pads == 4) {
1492 if (!(sr2 & W25Q_STATUS_QE)) {
1494 sr2 |= W25Q_STATUS_QE;
1498 if (sr2 & W25Q_STATUS_QE) {
1500 sr2 &= ~W25Q_STATUS_QE;
1505 /* Write status register */
1506 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
1507 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1508 stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
1514 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1517 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
1524 uint32_t page_buf[FLASH_PAGESIZE_32];
1527 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1529 /* Enter 32-bit address mode, if required */
1530 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1531 stfsm_enter_32bit_addr(fsm, 1);
1533 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1534 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1535 read_mask = (data_pads << 2) - 1;
1537 /* Handle non-aligned buf */
1538 p = ((uintptr_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1540 /* Handle non-aligned size */
1541 size_ub = (size + read_mask) & ~read_mask;
1542 size_lb = size & ~read_mask;
1543 size_mop = size & read_mask;
1545 seq->data_size = TRANSFER_SIZE(size_ub);
1546 seq->addr1 = (offset >> 16) & 0xffff;
1547 seq->addr2 = offset & 0xffff;
1549 stfsm_load_seq(fsm, seq);
1552 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1555 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1556 memcpy(p + size_lb, &tmp, size_mop);
1559 /* Handle non-aligned buf */
1560 if ((uintptr_t)buf & 0x3)
1561 memcpy(buf, page_buf, size);
1563 /* Wait for sequence to finish */
1564 stfsm_wait_seq(fsm);
1566 stfsm_clear_fifo(fsm);
1568 /* Exit 32-bit address mode, if required */
1569 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1570 stfsm_enter_32bit_addr(fsm, 0);
1575 static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1576 uint32_t size, uint32_t offset)
1578 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
1580 uint32_t write_mask;
1586 uint32_t page_buf[FLASH_PAGESIZE_32];
1587 uint8_t *t = (uint8_t *)&tmp;
1591 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1593 /* Enter 32-bit address mode, if required */
1594 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1595 stfsm_enter_32bit_addr(fsm, 1);
1597 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1598 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1599 write_mask = (data_pads << 2) - 1;
1601 /* Handle non-aligned buf */
1602 if ((uintptr_t)buf & 0x3) {
1603 memcpy(page_buf, buf, size);
1604 p = (uint8_t *)page_buf;
1609 /* Handle non-aligned size */
1610 size_ub = (size + write_mask) & ~write_mask;
1611 size_lb = size & ~write_mask;
1612 size_mop = size & write_mask;
1614 seq->data_size = TRANSFER_SIZE(size_ub);
1615 seq->addr1 = (offset >> 16) & 0xffff;
1616 seq->addr2 = offset & 0xffff;
1618 /* Need to set FIFO to write mode, before writing data to FIFO (see
1621 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1624 * Before writing data to the FIFO, apply a small delay to allow a
1625 * potential change of FIFO direction to complete.
1627 if (fsm->fifo_dir_delay == 0)
1628 readl(fsm->base + SPI_FAST_SEQ_CFG);
1630 udelay(fsm->fifo_dir_delay);
1633 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1635 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1639 /* Handle non-aligned size */
1641 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1642 for (i = 0; i < size_mop; i++)
1645 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1648 /* Start sequence */
1649 stfsm_load_seq(fsm, seq);
1651 /* Wait for sequence to finish */
1652 stfsm_wait_seq(fsm);
1654 /* Wait for completion */
1655 ret = stfsm_wait_busy(fsm);
1656 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1657 stfsm_s25fl_clear_status_reg(fsm);
1659 /* Exit 32-bit address mode, if required */
1660 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1661 stfsm_enter_32bit_addr(fsm, 0);
1667 * Read an address range from the flash chip. The address range
1668 * may be any size provided it is within the physical boundaries.
1670 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1671 size_t *retlen, u_char *buf)
1673 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1676 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1677 __func__, (u32)from, len);
1679 mutex_lock(&fsm->lock);
1682 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1684 stfsm_read(fsm, buf, bytes, from);
1693 mutex_unlock(&fsm->lock);
1698 static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
1700 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1703 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1705 /* Enter 32-bit address mode, if required */
1706 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1707 stfsm_enter_32bit_addr(fsm, 1);
1709 seq->addr1 = (offset >> 16) & 0xffff;
1710 seq->addr2 = offset & 0xffff;
1712 stfsm_load_seq(fsm, seq);
1714 stfsm_wait_seq(fsm);
1716 /* Wait for completion */
1717 ret = stfsm_wait_busy(fsm);
1718 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1719 stfsm_s25fl_clear_status_reg(fsm);
1721 /* Exit 32-bit address mode, if required */
1722 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1723 stfsm_enter_32bit_addr(fsm, 0);
1728 static int stfsm_erase_chip(struct stfsm *fsm)
1730 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1732 dev_dbg(fsm->dev, "erasing chip\n");
1734 stfsm_load_seq(fsm, seq);
1736 stfsm_wait_seq(fsm);
1738 return stfsm_wait_busy(fsm);
1742 * Write an address range to the flash chip. Data must be written in
1743 * FLASH_PAGESIZE chunks. The address range may be any size provided
1744 * it is within the physical boundaries.
1746 static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1747 size_t *retlen, const u_char *buf)
1749 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1753 uint8_t *b = (uint8_t *)buf;
1756 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1758 /* Offset within page */
1759 page_offs = to % FLASH_PAGESIZE;
1761 mutex_lock(&fsm->lock);
1764 /* Write up to page boundary */
1765 bytes = min_t(size_t, FLASH_PAGESIZE - page_offs, len);
1767 ret = stfsm_write(fsm, b, bytes, to);
1775 /* We are now page-aligned */
1783 mutex_unlock(&fsm->lock);
1789 * Erase an address range on the flash chip. The address range may extend
1790 * one or more erase sectors. Return an error is there is a problem erasing.
1792 static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1794 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1798 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1799 (long long)instr->addr, (long long)instr->len);
1804 mutex_lock(&fsm->lock);
1806 /* Whole-chip erase? */
1807 if (len == mtd->size) {
1808 ret = stfsm_erase_chip(fsm);
1813 ret = stfsm_erase_sector(fsm, addr);
1817 addr += mtd->erasesize;
1818 len -= mtd->erasesize;
1822 mutex_unlock(&fsm->lock);
1827 mutex_unlock(&fsm->lock);
1832 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
1834 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1837 stfsm_load_seq(fsm, seq);
1839 stfsm_read_fifo(fsm, tmp, 8);
1841 memcpy(jedec, tmp, 5);
1843 stfsm_wait_seq(fsm);
1846 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1848 struct flash_info *info;
1853 stfsm_read_jedec(fsm, id);
1855 jedec = id[0] << 16 | id[1] << 8 | id[2];
1857 * JEDEC also defines an optional "extended device information"
1858 * string for after vendor-specific data, after the three bytes
1859 * we use here. Supporting some chips might require using it.
1861 ext_jedec = id[3] << 8 | id[4];
1863 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%5ph]\n", jedec, id);
1865 for (info = flash_types; info->name; info++) {
1866 if (info->jedec_id == jedec) {
1867 if (info->ext_id && info->ext_id != ext_jedec)
1872 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1877 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1879 int ret, timeout = 10;
1881 /* Wait for controller to accept mode change */
1883 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1892 writel(mode, fsm->base + SPI_MODESELECT);
1897 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1902 emi_freq = clk_get_rate(fsm->clk);
1905 * Calculate clk_div - values between 2 and 128
1906 * Multiple of 2, rounded up
1908 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1911 else if (clk_div > 128)
1915 * Determine a suitable delay for the IP to complete a change of
1916 * direction of the FIFO. The required delay is related to the clock
1917 * divider used. The following heuristics are based on empirical tests,
1918 * using a 100MHz EMI clock.
1921 fsm->fifo_dir_delay = 0;
1922 else if (clk_div <= 10)
1923 fsm->fifo_dir_delay = 1;
1925 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1927 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1928 emi_freq, spi_freq, clk_div);
1930 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1933 static int stfsm_init(struct stfsm *fsm)
1937 /* Perform a soft reset of the FSM controller */
1938 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1940 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1942 /* Set clock to 'safe' frequency initially */
1943 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1946 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1950 /* Set timing parameters */
1951 writel(SPI_CFG_DEVICE_ST |
1952 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1953 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1954 SPI_CFG_DEFAULT_DATA_HOLD,
1955 fsm->base + SPI_CONFIGDATA);
1956 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1959 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1960 * our purposes, the WAIT instruction is used purely to achieve
1961 * "sequence validity" rather than actually implement a delay.
1963 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1965 /* Clear FIFO, just in case */
1966 stfsm_clear_fifo(fsm);
1971 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1973 struct stfsm *fsm = platform_get_drvdata(pdev);
1974 struct device_node *np = pdev->dev.of_node;
1975 struct regmap *regmap;
1976 uint32_t boot_device_reg;
1977 uint32_t boot_device_spi;
1978 uint32_t boot_device; /* Value we read from *boot_device_reg */
1981 /* Booting from SPI NOR Flash is the default */
1982 fsm->booted_from_spi = true;
1984 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1986 goto boot_device_fail;
1988 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1990 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1992 /* Where in the syscon the boot device information lives */
1993 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1995 goto boot_device_fail;
1997 /* Boot device value when booted from SPI NOR */
1998 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
2000 goto boot_device_fail;
2002 ret = regmap_read(regmap, boot_device_reg, &boot_device);
2004 goto boot_device_fail;
2006 if (boot_device != boot_device_spi)
2007 fsm->booted_from_spi = false;
2012 dev_warn(&pdev->dev,
2013 "failed to fetch boot device, assuming boot from SPI\n");
2016 static int stfsm_probe(struct platform_device *pdev)
2018 struct device_node *np = pdev->dev.of_node;
2019 struct flash_info *info;
2020 struct resource *res;
2025 dev_err(&pdev->dev, "No DT found\n");
2029 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2033 fsm->dev = &pdev->dev;
2035 platform_set_drvdata(pdev, fsm);
2037 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2039 dev_err(&pdev->dev, "Resource not found\n");
2043 fsm->base = devm_ioremap_resource(&pdev->dev, res);
2044 if (IS_ERR(fsm->base)) {
2046 "Failed to reserve memory region %pR\n", res);
2047 return PTR_ERR(fsm->base);
2050 fsm->clk = devm_clk_get(&pdev->dev, NULL);
2051 if (IS_ERR(fsm->clk)) {
2052 dev_err(fsm->dev, "Couldn't find EMI clock.\n");
2053 return PTR_ERR(fsm->clk);
2056 ret = clk_prepare_enable(fsm->clk);
2058 dev_err(fsm->dev, "Failed to enable EMI clock.\n");
2062 mutex_init(&fsm->lock);
2064 ret = stfsm_init(fsm);
2066 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2067 goto err_clk_unprepare;
2070 stfsm_fetch_platform_configs(pdev);
2072 /* Detect SPI FLASH device */
2073 info = stfsm_jedec_probe(fsm);
2076 goto err_clk_unprepare;
2080 /* Use device size to determine address width */
2081 if (info->sector_size * info->n_sectors > 0x1000000)
2082 info->flags |= FLASH_FLAG_32BIT_ADDR;
2085 * Configure READ/WRITE/ERASE sequences according to platform and
2089 ret = info->config(fsm);
2091 goto err_clk_unprepare;
2093 ret = stfsm_prepare_rwe_seqs_default(fsm);
2095 goto err_clk_unprepare;
2098 fsm->mtd.name = info->name;
2099 fsm->mtd.dev.parent = &pdev->dev;
2100 mtd_set_of_node(&fsm->mtd, np);
2101 fsm->mtd.type = MTD_NORFLASH;
2102 fsm->mtd.writesize = 4;
2103 fsm->mtd.writebufsize = fsm->mtd.writesize;
2104 fsm->mtd.flags = MTD_CAP_NORFLASH;
2105 fsm->mtd.size = info->sector_size * info->n_sectors;
2106 fsm->mtd.erasesize = info->sector_size;
2108 fsm->mtd._read = stfsm_mtd_read;
2109 fsm->mtd._write = stfsm_mtd_write;
2110 fsm->mtd._erase = stfsm_mtd_erase;
2112 dev_info(&pdev->dev,
2113 "Found serial flash device: %s\n"
2114 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2116 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2117 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
2119 return mtd_device_register(&fsm->mtd, NULL, 0);
2122 clk_disable_unprepare(fsm->clk);
2126 static int stfsm_remove(struct platform_device *pdev)
2128 struct stfsm *fsm = platform_get_drvdata(pdev);
2130 return mtd_device_unregister(&fsm->mtd);
2133 #ifdef CONFIG_PM_SLEEP
2134 static int stfsmfsm_suspend(struct device *dev)
2136 struct stfsm *fsm = dev_get_drvdata(dev);
2138 clk_disable_unprepare(fsm->clk);
2143 static int stfsmfsm_resume(struct device *dev)
2145 struct stfsm *fsm = dev_get_drvdata(dev);
2147 return clk_prepare_enable(fsm->clk);
2151 static SIMPLE_DEV_PM_OPS(stfsm_pm_ops, stfsmfsm_suspend, stfsmfsm_resume);
2153 static const struct of_device_id stfsm_match[] = {
2154 { .compatible = "st,spi-fsm", },
2157 MODULE_DEVICE_TABLE(of, stfsm_match);
2159 static struct platform_driver stfsm_driver = {
2160 .probe = stfsm_probe,
2161 .remove = stfsm_remove,
2163 .name = "st-spi-fsm",
2164 .of_match_table = stfsm_match,
2165 .pm = &stfsm_pm_ops,
2168 module_platform_driver(stfsm_driver);
2171 MODULE_DESCRIPTION("ST SPI FSM driver");
2172 MODULE_LICENSE("GPL");