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 STicroelectronics 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/sched.h>
22 #include <linux/delay.h>
26 #include "serial_flash_cmds.h"
29 * FSM SPI Controller Registers
31 #define SPI_CLOCKDIV 0x0010
32 #define SPI_MODESELECT 0x0018
33 #define SPI_CONFIGDATA 0x0020
34 #define SPI_STA_MODE_CHANGE 0x0028
35 #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
36 #define SPI_FAST_SEQ_ADD1 0x0104
37 #define SPI_FAST_SEQ_ADD2 0x0108
38 #define SPI_FAST_SEQ_ADD_CFG 0x010c
39 #define SPI_FAST_SEQ_OPC1 0x0110
40 #define SPI_FAST_SEQ_OPC2 0x0114
41 #define SPI_FAST_SEQ_OPC3 0x0118
42 #define SPI_FAST_SEQ_OPC4 0x011c
43 #define SPI_FAST_SEQ_OPC5 0x0120
44 #define SPI_MODE_BITS 0x0124
45 #define SPI_DUMMY_BITS 0x0128
46 #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
47 #define SPI_FAST_SEQ_1 0x0130
48 #define SPI_FAST_SEQ_2 0x0134
49 #define SPI_FAST_SEQ_3 0x0138
50 #define SPI_FAST_SEQ_4 0x013c
51 #define SPI_FAST_SEQ_CFG 0x0140
52 #define SPI_FAST_SEQ_STA 0x0144
53 #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
54 #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
55 #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
56 #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
57 #define SPI_PROGRAM_ERASE_TIME 0x0158
58 #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
59 #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
60 #define SPI_STATUS_WR_TIME_REG 0x0164
61 #define SPI_FAST_SEQ_DATA_REG 0x0300
64 * Register: SPI_MODESELECT
66 #define SPI_MODESELECT_CONTIG 0x01
67 #define SPI_MODESELECT_FASTREAD 0x02
68 #define SPI_MODESELECT_DUALIO 0x04
69 #define SPI_MODESELECT_FSM 0x08
70 #define SPI_MODESELECT_QUADBOOT 0x10
73 * Register: SPI_CONFIGDATA
75 #define SPI_CFG_DEVICE_ST 0x1
76 #define SPI_CFG_DEVICE_ATMEL 0x4
77 #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
78 #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
79 #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
81 #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
82 #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
83 #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
86 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
88 #define TRANSFER_SIZE(x) ((x) * 8)
91 * Register: SPI_FAST_SEQ_ADD_CFG
93 #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
94 #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
95 #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
96 #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
97 #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
98 #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
99 #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
100 #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
101 #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
102 #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
105 * Register: SPI_FAST_SEQ_n
107 #define SEQ_OPC_OPCODE(x) ((x) << 0)
108 #define SEQ_OPC_CYCLES(x) ((x) << 8)
109 #define SEQ_OPC_PADS_1 (0x0 << 14)
110 #define SEQ_OPC_PADS_2 (0x1 << 14)
111 #define SEQ_OPC_PADS_4 (0x3 << 14)
112 #define SEQ_OPC_CSDEASSERT (1 << 16)
115 * Register: SPI_FAST_SEQ_CFG
117 #define SEQ_CFG_STARTSEQ (1 << 0)
118 #define SEQ_CFG_SWRESET (1 << 5)
119 #define SEQ_CFG_CSDEASSERT (1 << 6)
120 #define SEQ_CFG_READNOTWRITE (1 << 7)
121 #define SEQ_CFG_ERASE (1 << 8)
122 #define SEQ_CFG_PADS_1 (0x0 << 16)
123 #define SEQ_CFG_PADS_2 (0x1 << 16)
124 #define SEQ_CFG_PADS_4 (0x3 << 16)
127 * Register: SPI_MODE_BITS
129 #define MODE_DATA(x) (x & 0xff)
130 #define MODE_CYCLES(x) ((x & 0x3f) << 16)
131 #define MODE_PADS_1 (0x0 << 22)
132 #define MODE_PADS_2 (0x1 << 22)
133 #define MODE_PADS_4 (0x3 << 22)
134 #define DUMMY_CSDEASSERT (1 << 24)
137 * Register: SPI_DUMMY_BITS
139 #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
140 #define DUMMY_PADS_1 (0x0 << 22)
141 #define DUMMY_PADS_2 (0x1 << 22)
142 #define DUMMY_PADS_4 (0x3 << 22)
143 #define DUMMY_CSDEASSERT (1 << 24)
146 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
148 #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
149 #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
150 #define STA_PADS_1 (0x0 << 16)
151 #define STA_PADS_2 (0x1 << 16)
152 #define STA_PADS_4 (0x3 << 16)
153 #define STA_CSDEASSERT (0x1 << 20)
154 #define STA_RDNOTWR (0x1 << 21)
157 * FSM SPI Instruction Opcodes
159 #define STFSM_OPC_CMD 0x1
160 #define STFSM_OPC_ADD 0x2
161 #define STFSM_OPC_STA 0x3
162 #define STFSM_OPC_MODE 0x4
163 #define STFSM_OPC_DUMMY 0x5
164 #define STFSM_OPC_DATA 0x6
165 #define STFSM_OPC_WAIT 0x7
166 #define STFSM_OPC_JUMP 0x8
167 #define STFSM_OPC_GOTO 0x9
168 #define STFSM_OPC_STOP 0xF
171 * FSM SPI Instructions (== opcode + operand).
173 #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
175 #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
176 #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
177 #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
178 #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
179 #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
180 #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
181 #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
183 #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
184 #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
186 #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
187 #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
188 #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
189 #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
191 #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
192 #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
193 #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
194 #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
196 #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
197 #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
199 #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
201 #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
204 #define FLASH_CMD_WREN 0x06
205 #define FLASH_CMD_WRDI 0x04
206 #define FLASH_CMD_RDID 0x9f
207 #define FLASH_CMD_RDSR 0x05
208 #define FLASH_CMD_RDSR2 0x35
209 #define FLASH_CMD_WRSR 0x01
210 #define FLASH_CMD_SE_4K 0x20
211 #define FLASH_CMD_SE_32K 0x52
212 #define FLASH_CMD_SE 0xd8
213 #define FLASH_CMD_CHIPERASE 0xc7
214 #define FLASH_CMD_WRVCR 0x81
215 #define FLASH_CMD_RDVCR 0x85
217 #define FLASH_CMD_READ 0x03 /* READ */
218 #define FLASH_CMD_READ_FAST 0x0b /* FAST READ */
219 #define FLASH_CMD_READ_1_1_2 0x3b /* DUAL OUTPUT READ */
220 #define FLASH_CMD_READ_1_2_2 0xbb /* DUAL I/O READ */
221 #define FLASH_CMD_READ_1_1_4 0x6b /* QUAD OUTPUT READ */
222 #define FLASH_CMD_READ_1_4_4 0xeb /* QUAD I/O READ */
224 #define FLASH_CMD_WRITE 0x02 /* PAGE PROGRAM */
225 #define FLASH_CMD_WRITE_1_1_2 0xa2 /* DUAL INPUT PROGRAM */
226 #define FLASH_CMD_WRITE_1_2_2 0xd2 /* DUAL INPUT EXT PROGRAM */
227 #define FLASH_CMD_WRITE_1_1_4 0x32 /* QUAD INPUT PROGRAM */
228 #define FLASH_CMD_WRITE_1_4_4 0x12 /* QUAD INPUT EXT PROGRAM */
230 #define FLASH_CMD_EN4B_ADDR 0xb7 /* Enter 4-byte address mode */
231 #define FLASH_CMD_EX4B_ADDR 0xe9 /* Exit 4-byte address mode */
233 /* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
234 #define FLASH_CMD_READ4 0x13
235 #define FLASH_CMD_READ4_FAST 0x0c
236 #define FLASH_CMD_READ4_1_1_2 0x3c
237 #define FLASH_CMD_READ4_1_2_2 0xbc
238 #define FLASH_CMD_READ4_1_1_4 0x6c
239 #define FLASH_CMD_READ4_1_4_4 0xec
241 /* Status register */
242 #define FLASH_STATUS_BUSY 0x01
243 #define FLASH_STATUS_WEL 0x02
244 #define FLASH_STATUS_BP0 0x04
245 #define FLASH_STATUS_BP1 0x08
246 #define FLASH_STATUS_BP2 0x10
247 #define FLASH_STATUS_SRWP0 0x80
248 #define FLASH_STATUS_TIMEOUT 0xff
250 #define FLASH_PAGESIZE 256 /* In Bytes */
251 #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
252 #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
255 * Flags to tweak operation of default read/write/erase routines
257 #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
258 #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
259 #define CFG_WRITE_EX_32BIT_ADDR_DELAY 0x00000004
260 #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
261 #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
266 struct resource *region;
269 struct flash_info *info;
271 uint32_t configuration;
272 uint32_t fifo_dir_delay;
273 bool booted_from_spi;
289 } __packed __aligned(4);
291 /* Parameters to configure a READ or WRITE FSM sequence */
292 struct seq_rw_config {
293 uint32_t flags; /* flags to support config */
294 uint8_t cmd; /* FLASH command */
295 int write; /* Write Sequence */
296 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
297 uint8_t data_pads; /* No. of data pads */
298 uint8_t mode_data; /* MODE data */
299 uint8_t mode_cycles; /* No. of MODE cycles */
300 uint8_t dummy_cycles; /* No. of DUMMY cycles */
303 /* SPI Flash Device Table */
307 * JEDEC id zero means "no ID" (most older chips); otherwise it has
308 * a high byte of zero plus three data bytes: the manufacturer id,
309 * then a two byte device id.
314 * The size listed here is what works with FLASH_CMD_SE, which isn't
315 * necessarily called a "sector" by the vendor.
317 unsigned sector_size;
321 * Note, where FAST_READ is supported, freq_max specifies the
322 * FAST_READ frequency, not the READ frequency.
325 int (*config)(struct stfsm *);
328 static int stfsm_n25q_config(struct stfsm *fsm);
330 static struct flash_info flash_types[] = {
332 * ST Microelectronics/Numonyx --
333 * (newer production versions may have feature updates
334 * (eg faster operating frequency)
336 #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
337 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
338 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
339 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
340 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
341 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
342 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
344 #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
345 FLASH_FLAG_READ_FAST | \
346 FLASH_FLAG_READ_1_1_2 | \
347 FLASH_FLAG_WRITE_1_1_2)
348 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
349 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
351 #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
352 FLASH_FLAG_READ_FAST | \
353 FLASH_FLAG_READ_1_1_2 | \
354 FLASH_FLAG_READ_1_2_2 | \
355 FLASH_FLAG_READ_1_1_4 | \
356 FLASH_FLAG_READ_1_4_4 | \
359 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
360 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
362 #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
363 FLASH_FLAG_READ_FAST | \
364 FLASH_FLAG_READ_1_1_2 | \
365 FLASH_FLAG_READ_1_2_2 | \
366 FLASH_FLAG_READ_1_1_4 | \
367 FLASH_FLAG_READ_1_4_4 | \
368 FLASH_FLAG_WRITE_1_1_2 | \
369 FLASH_FLAG_WRITE_1_2_2 | \
370 FLASH_FLAG_WRITE_1_1_4 | \
371 FLASH_FLAG_WRITE_1_4_4)
372 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
374 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
375 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
379 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
381 #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
382 FLASH_FLAG_READ_1_1_2 | \
383 FLASH_FLAG_READ_1_2_2 | \
384 FLASH_FLAG_READ_1_1_4 | \
385 FLASH_FLAG_READ_1_4_4 | \
386 FLASH_FLAG_WRITE_1_1_4 | \
387 FLASH_FLAG_READ_FAST)
388 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
390 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
395 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
396 * - RESET# signal supported by die but not bristled out on all
397 * package types. The package type is a function of board design,
398 * so this information is captured in the board's flags.
399 * - Supports 'DYB' sector protection. Depending on variant, sectors
400 * may default to locked state on power-on.
402 #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
404 FLASH_FLAG_DYB_LOCKING)
405 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
407 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
409 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
410 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
411 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
412 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
414 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
415 #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
416 FLASH_FLAG_READ_FAST | \
417 FLASH_FLAG_READ_1_1_2 | \
418 FLASH_FLAG_WRITE_1_1_2)
419 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
420 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
421 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
422 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
423 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
425 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
426 #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
427 FLASH_FLAG_READ_FAST | \
428 FLASH_FLAG_READ_1_1_2 | \
429 FLASH_FLAG_READ_1_2_2 | \
430 FLASH_FLAG_READ_1_1_4 | \
431 FLASH_FLAG_READ_1_4_4 | \
432 FLASH_FLAG_WRITE_1_1_4)
433 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
434 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
435 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
436 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
439 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
443 * FSM message sequence configurations:
445 * All configs are presented in order of preference
448 /* Default READ configurations, in order of preference */
449 static struct seq_rw_config default_read_configs[] = {
450 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
451 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
452 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
453 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
454 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
455 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
456 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
459 /* Default WRITE configurations */
460 static struct seq_rw_config default_write_configs[] = {
461 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
462 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
463 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
464 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
465 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
466 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
470 * [N25Qxxx] Configuration
472 #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
473 #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
474 #define N25Q_VCR_WRAP_CONT 0x3
476 /* N25Q 3-byte Address READ configurations
477 * - 'FAST' variants configured for 8 dummy cycles.
479 * Note, the number of dummy cycles used for 'FAST' READ operations is
480 * configurable and would normally be tuned according to the READ command and
481 * operating frequency. However, this applies universally to all 'FAST' READ
482 * commands, including those used by the SPIBoot controller, and remains in
483 * force until the device is power-cycled. Since the SPIBoot controller is
484 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
487 static struct seq_rw_config n25q_read3_configs[] = {
488 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
489 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
490 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
491 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
492 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
493 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
494 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
497 /* N25Q 4-byte Address READ configurations
498 * - use special 4-byte address READ commands (reduces overheads, and
499 * reduces risk of hitting watchdog reset issues).
500 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
502 static struct seq_rw_config n25q_read4_configs[] = {
503 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
504 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
505 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
506 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
507 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
508 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
509 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
512 static struct stfsm_seq stfsm_seq_read; /* Dynamically populated */
513 static struct stfsm_seq stfsm_seq_write; /* Dynamically populated */
514 static struct stfsm_seq stfsm_seq_en_32bit_addr;/* Dynamically populated */
516 static struct stfsm_seq stfsm_seq_read_jedec = {
517 .data_size = TRANSFER_SIZE(8),
518 .seq_opc[0] = (SEQ_OPC_PADS_1 |
520 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
523 STFSM_INST_DATA_READ,
526 .seq_cfg = (SEQ_CFG_PADS_1 |
527 SEQ_CFG_READNOTWRITE |
532 static struct stfsm_seq stfsm_seq_read_status_fifo = {
533 .data_size = TRANSFER_SIZE(4),
534 .seq_opc[0] = (SEQ_OPC_PADS_1 |
536 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
539 STFSM_INST_DATA_READ,
542 .seq_cfg = (SEQ_CFG_PADS_1 |
543 SEQ_CFG_READNOTWRITE |
548 static struct stfsm_seq stfsm_seq_erase_sector = {
549 /* 'addr_cfg' configured during initialisation */
551 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
552 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
554 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
555 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
564 .seq_cfg = (SEQ_CFG_PADS_1 |
565 SEQ_CFG_READNOTWRITE |
570 static struct stfsm_seq stfsm_seq_erase_chip = {
572 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
573 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
575 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
576 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
584 .seq_cfg = (SEQ_CFG_PADS_1 |
586 SEQ_CFG_READNOTWRITE |
591 static struct stfsm_seq stfsm_seq_write_status = {
592 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
593 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
594 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
595 SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
602 .seq_cfg = (SEQ_CFG_PADS_1 |
603 SEQ_CFG_READNOTWRITE |
608 static struct stfsm_seq stfsm_seq_wrvcr = {
609 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
610 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
611 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
612 SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
619 .seq_cfg = (SEQ_CFG_PADS_1 |
620 SEQ_CFG_READNOTWRITE |
625 static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
627 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
628 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
629 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
630 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
633 seq->seq[0] = STFSM_INST_CMD2;
634 seq->seq[1] = STFSM_INST_CMD1;
635 seq->seq[2] = STFSM_INST_WAIT;
636 seq->seq[3] = STFSM_INST_STOP;
638 seq->seq_cfg = (SEQ_CFG_PADS_1 |
640 SEQ_CFG_READNOTWRITE |
647 static inline int stfsm_is_idle(struct stfsm *fsm)
649 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
652 static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
654 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
657 static void stfsm_clear_fifo(struct stfsm *fsm)
662 avail = stfsm_fifo_available(fsm);
667 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
673 static inline void stfsm_load_seq(struct stfsm *fsm,
674 const struct stfsm_seq *seq)
676 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
677 const uint32_t *src = (const uint32_t *)seq;
678 int words = sizeof(*seq) / sizeof(*src);
680 BUG_ON(!stfsm_is_idle(fsm));
689 static void stfsm_wait_seq(struct stfsm *fsm)
691 unsigned long deadline;
694 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
697 if (time_after_eq(jiffies, deadline))
700 if (stfsm_is_idle(fsm))
706 dev_err(fsm->dev, "timeout on sequence completion\n");
709 static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
712 uint32_t remaining = size >> 2;
716 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
718 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
722 avail = stfsm_fifo_available(fsm);
727 words = min(avail, remaining);
730 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
735 static int stfsm_write_fifo(struct stfsm *fsm,
736 const uint32_t *buf, const uint32_t size)
738 uint32_t words = size >> 2;
740 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
742 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
744 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
749 static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
751 struct stfsm_seq *seq = &stfsm_seq_en_32bit_addr;
752 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
754 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
756 SEQ_OPC_OPCODE(cmd) |
759 stfsm_load_seq(fsm, seq);
766 static uint8_t stfsm_wait_busy(struct stfsm *fsm)
768 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
769 unsigned long deadline;
774 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
776 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
778 /* Load read_status sequence */
779 stfsm_load_seq(fsm, seq);
782 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
784 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
788 if (time_after_eq(jiffies, deadline))
793 stfsm_read_fifo(fsm, &status, 4);
795 if ((status & FLASH_STATUS_BUSY) == 0)
798 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
799 ((status & S25FL_STATUS_P_ERR) ||
800 (status & S25FL_STATUS_E_ERR)))
801 return (uint8_t)(status & 0xff);
805 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
808 dev_err(fsm->dev, "timeout on wait_busy\n");
810 return FLASH_STATUS_TIMEOUT;
813 static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
816 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
819 dev_dbg(fsm->dev, "reading STA[%s]\n",
820 (cmd == FLASH_CMD_RDSR) ? "1" : "2");
822 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
824 SEQ_OPC_OPCODE(cmd)),
826 stfsm_load_seq(fsm, seq);
828 stfsm_read_fifo(fsm, &tmp, 4);
830 *status = (uint8_t)(tmp >> 24);
837 static int stfsm_write_status(struct stfsm *fsm, uint16_t status,
840 struct stfsm_seq *seq = &stfsm_seq_write_status;
842 dev_dbg(fsm->dev, "writing STA[%s] 0x%04x\n",
843 (sta_bytes == 1) ? "1" : "1+2", status);
845 seq->status = (uint32_t)status | STA_PADS_1 | STA_CSDEASSERT;
846 seq->seq[2] = (sta_bytes == 1) ?
847 STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
849 stfsm_load_seq(fsm, seq);
856 static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
858 struct stfsm_seq *seq = &stfsm_seq_wrvcr;
860 dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
862 seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
864 stfsm_load_seq(fsm, seq);
872 * SoC reset on 'boot-from-spi' systems
874 * Certain modes of operation cause the Flash device to enter a particular state
875 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
876 * Addr' commands). On boot-from-spi systems, it is important to consider what
877 * happens if a warm reset occurs during this period. The SPIBoot controller
878 * assumes that Flash device is in its default reset state, 24-bit address mode,
879 * and ready to accept commands. This can be achieved using some form of
880 * on-board logic/controller to force a device POR in response to a SoC-level
881 * reset or by making use of the device reset signal if available (limited
882 * number of devices only).
884 * Failure to take such precautions can cause problems following a warm reset.
885 * For some operations (e.g. ERASE), there is little that can be done. For
886 * other modes of operation (e.g. 32-bit addressing), options are often
887 * available that can help minimise the window in which a reset could cause a
891 static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
893 /* Reset signal is available on the board and supported by the device */
894 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
897 /* Board-level logic forces a power-on-reset */
901 /* Reset is not properly handled and may result in failure to reboot */
905 /* Configure 'addr_cfg' according to addressing mode */
906 static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
907 struct stfsm_seq *seq)
909 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
911 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
912 ADR_CFG_PADS_1_ADD1 |
913 ADR_CFG_CYCLES_ADD2(16) |
914 ADR_CFG_PADS_1_ADD2 |
915 ADR_CFG_CSDEASSERT_ADD2);
918 /* Search for preferred configuration based on available flags */
919 static struct seq_rw_config *
920 stfsm_search_seq_rw_configs(struct stfsm *fsm,
921 struct seq_rw_config cfgs[])
923 struct seq_rw_config *config;
924 int flags = fsm->info->flags;
926 for (config = cfgs; config->cmd != 0; config++)
927 if ((config->flags & flags) == config->flags)
933 /* Prepare a READ/WRITE sequence according to configuration parameters */
934 static void stfsm_prepare_rw_seq(struct stfsm *fsm,
935 struct stfsm_seq *seq,
936 struct seq_rw_config *cfg)
938 int addr1_cycles, addr2_cycles;
941 memset(seq, 0, sizeof(*seq));
943 /* Add READ/WRITE OPC */
944 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
946 SEQ_OPC_OPCODE(cfg->cmd));
948 /* Add WREN OPC for a WRITE sequence */
950 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
952 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
955 /* Address configuration (24 or 32-bit addresses) */
956 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
957 addr1_cycles /= cfg->addr_pads;
958 addr2_cycles = 16 / cfg->addr_pads;
959 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
960 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
961 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
962 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
964 /* Data/Sequence configuration */
965 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
969 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
971 /* Mode configuration (no. of pads taken from addr cfg) */
972 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
973 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
974 (cfg->addr_pads - 1) << 22); /* pads */
976 /* Dummy configuration (no. of pads taken from addr cfg) */
977 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
978 (cfg->addr_pads - 1) << 22); /* pads */
981 /* Instruction sequence */
984 seq->seq[i++] = STFSM_INST_CMD2;
986 seq->seq[i++] = STFSM_INST_CMD1;
988 seq->seq[i++] = STFSM_INST_ADD1;
989 seq->seq[i++] = STFSM_INST_ADD2;
991 if (cfg->mode_cycles)
992 seq->seq[i++] = STFSM_INST_MODE;
994 if (cfg->dummy_cycles)
995 seq->seq[i++] = STFSM_INST_DUMMY;
998 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
999 seq->seq[i++] = STFSM_INST_STOP;
1002 static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1003 struct stfsm_seq *seq,
1004 struct seq_rw_config *cfgs)
1006 struct seq_rw_config *config;
1008 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1010 dev_err(fsm->dev, "failed to find suitable config\n");
1014 stfsm_prepare_rw_seq(fsm, seq, config);
1019 /* Prepare a READ/WRITE/ERASE 'default' sequences */
1020 static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1022 uint32_t flags = fsm->info->flags;
1025 /* Configure 'READ' sequence */
1026 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
1027 default_read_configs);
1030 "failed to prep READ sequence with flags [0x%08x]\n",
1035 /* Configure 'WRITE' sequence */
1036 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
1037 default_write_configs);
1040 "failed to prep WRITE sequence with flags [0x%08x]\n",
1045 /* Configure 'ERASE_SECTOR' sequence */
1046 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1051 static int stfsm_n25q_config(struct stfsm *fsm)
1053 uint32_t flags = fsm->info->flags;
1058 /* Configure 'READ' sequence */
1059 if (flags & FLASH_FLAG_32BIT_ADDR)
1060 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
1061 n25q_read4_configs);
1063 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
1064 n25q_read3_configs);
1067 "failed to prepare READ sequence with flags [0x%08x]\n",
1072 /* Configure 'WRITE' sequence (default configs) */
1073 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
1074 default_write_configs);
1077 "preparing WRITE sequence using flags [0x%08x] failed\n",
1082 /* * Configure 'ERASE_SECTOR' sequence */
1083 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1085 /* Configure 32-bit address support */
1086 if (flags & FLASH_FLAG_32BIT_ADDR) {
1087 stfsm_n25q_en_32bit_addr_seq(&stfsm_seq_en_32bit_addr);
1089 soc_reset = stfsm_can_handle_soc_reset(fsm);
1090 if (soc_reset || !fsm->booted_from_spi) {
1092 * If we can handle SoC resets, we enable 32-bit
1093 * address mode pervasively
1095 stfsm_enter_32bit_addr(fsm, 1);
1098 * If not, enable/disable for WRITE and ERASE
1099 * operations (READ uses special commands)
1101 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1102 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1107 * Configure device to use 8 dummy cycles
1109 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1110 N25Q_VCR_WRAP_CONT);
1111 stfsm_wrvcr(fsm, vcr);
1116 static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1119 struct stfsm_seq *seq = &stfsm_seq_read;
1126 uint32_t page_buf[FLASH_PAGESIZE_32];
1129 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1131 /* Enter 32-bit address mode, if required */
1132 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1133 stfsm_enter_32bit_addr(fsm, 1);
1135 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1136 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1137 read_mask = (data_pads << 2) - 1;
1139 /* Handle non-aligned buf */
1140 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1142 /* Handle non-aligned size */
1143 size_ub = (size + read_mask) & ~read_mask;
1144 size_lb = size & ~read_mask;
1145 size_mop = size & read_mask;
1147 seq->data_size = TRANSFER_SIZE(size_ub);
1148 seq->addr1 = (offset >> 16) & 0xffff;
1149 seq->addr2 = offset & 0xffff;
1151 stfsm_load_seq(fsm, seq);
1154 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1157 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1158 memcpy(p + size_lb, &tmp, size_mop);
1161 /* Handle non-aligned buf */
1162 if ((uint32_t)buf & 0x3)
1163 memcpy(buf, page_buf, size);
1165 /* Wait for sequence to finish */
1166 stfsm_wait_seq(fsm);
1168 stfsm_clear_fifo(fsm);
1170 /* Exit 32-bit address mode, if required */
1171 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1172 stfsm_enter_32bit_addr(fsm, 0);
1177 static int stfsm_write(struct stfsm *fsm, const uint8_t *const buf,
1178 const uint32_t size, const uint32_t offset)
1180 struct stfsm_seq *seq = &stfsm_seq_write;
1182 uint32_t write_mask;
1187 uint32_t page_buf[FLASH_PAGESIZE_32];
1188 uint8_t *t = (uint8_t *)&tmp;
1193 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1195 /* Enter 32-bit address mode, if required */
1196 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1197 stfsm_enter_32bit_addr(fsm, 1);
1199 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1200 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1201 write_mask = (data_pads << 2) - 1;
1203 /* Handle non-aligned buf */
1204 if ((uint32_t)buf & 0x3) {
1205 memcpy(page_buf, buf, size);
1206 p = (uint8_t *)page_buf;
1211 /* Handle non-aligned size */
1212 size_ub = (size + write_mask) & ~write_mask;
1213 size_lb = size & ~write_mask;
1214 size_mop = size & write_mask;
1216 seq->data_size = TRANSFER_SIZE(size_ub);
1217 seq->addr1 = (offset >> 16) & 0xffff;
1218 seq->addr2 = offset & 0xffff;
1220 /* Need to set FIFO to write mode, before writing data to FIFO (see
1223 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1226 * Before writing data to the FIFO, apply a small delay to allow a
1227 * potential change of FIFO direction to complete.
1229 if (fsm->fifo_dir_delay == 0)
1230 readl(fsm->base + SPI_FAST_SEQ_CFG);
1232 udelay(fsm->fifo_dir_delay);
1235 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1237 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1241 /* Handle non-aligned size */
1243 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1244 for (i = 0; i < size_mop; i++)
1247 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1250 /* Start sequence */
1251 stfsm_load_seq(fsm, seq);
1253 /* Wait for sequence to finish */
1254 stfsm_wait_seq(fsm);
1256 /* Wait for completion */
1257 ret = stfsm_wait_busy(fsm);
1259 /* Exit 32-bit address mode, if required */
1260 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR) {
1261 stfsm_enter_32bit_addr(fsm, 0);
1262 if (fsm->configuration & CFG_WRITE_EX_32BIT_ADDR_DELAY)
1270 * Read an address range from the flash chip. The address range
1271 * may be any size provided it is within the physical boundaries.
1273 static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1274 size_t *retlen, u_char *buf)
1276 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1279 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1280 __func__, (u32)from, len);
1282 mutex_lock(&fsm->lock);
1285 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1287 stfsm_read(fsm, buf, bytes, from);
1296 mutex_unlock(&fsm->lock);
1301 static int stfsm_erase_sector(struct stfsm *fsm, const uint32_t offset)
1303 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1306 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1308 /* Enter 32-bit address mode, if required */
1309 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1310 stfsm_enter_32bit_addr(fsm, 1);
1312 seq->addr1 = (offset >> 16) & 0xffff;
1313 seq->addr2 = offset & 0xffff;
1315 stfsm_load_seq(fsm, seq);
1317 stfsm_wait_seq(fsm);
1319 /* Wait for completion */
1320 ret = stfsm_wait_busy(fsm);
1322 /* Exit 32-bit address mode, if required */
1323 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1324 stfsm_enter_32bit_addr(fsm, 0);
1329 static int stfsm_erase_chip(struct stfsm *fsm)
1331 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1333 dev_dbg(fsm->dev, "erasing chip\n");
1335 stfsm_load_seq(fsm, seq);
1337 stfsm_wait_seq(fsm);
1339 return stfsm_wait_busy(fsm);
1343 * Write an address range to the flash chip. Data must be written in
1344 * FLASH_PAGESIZE chunks. The address range may be any size provided
1345 * it is within the physical boundaries.
1347 static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1348 size_t *retlen, const u_char *buf)
1350 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1354 uint8_t *b = (uint8_t *)buf;
1357 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1364 if (to + len > mtd->size)
1367 /* Offset within page */
1368 page_offs = to % FLASH_PAGESIZE;
1370 mutex_lock(&fsm->lock);
1373 /* Write up to page boundary */
1374 bytes = min(FLASH_PAGESIZE - page_offs, len);
1376 ret = stfsm_write(fsm, b, bytes, to);
1384 /* We are now page-aligned */
1392 mutex_unlock(&fsm->lock);
1398 * Erase an address range on the flash chip. The address range may extend
1399 * one or more erase sectors. Return an error is there is a problem erasing.
1401 static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1403 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1407 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1408 (long long)instr->addr, (long long)instr->len);
1413 mutex_lock(&fsm->lock);
1415 /* Whole-chip erase? */
1416 if (len == mtd->size) {
1417 ret = stfsm_erase_chip(fsm);
1422 ret = stfsm_erase_sector(fsm, addr);
1426 addr += mtd->erasesize;
1427 len -= mtd->erasesize;
1431 mutex_unlock(&fsm->lock);
1433 instr->state = MTD_ERASE_DONE;
1434 mtd_erase_callback(instr);
1439 instr->state = MTD_ERASE_FAILED;
1440 mutex_unlock(&fsm->lock);
1445 static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
1447 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1450 stfsm_load_seq(fsm, seq);
1452 stfsm_read_fifo(fsm, tmp, 8);
1454 memcpy(jedec, tmp, 5);
1456 stfsm_wait_seq(fsm);
1459 static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1461 struct flash_info *info;
1466 stfsm_read_jedec(fsm, id);
1468 jedec = id[0] << 16 | id[1] << 8 | id[2];
1470 * JEDEC also defines an optional "extended device information"
1471 * string for after vendor-specific data, after the three bytes
1472 * we use here. Supporting some chips might require using it.
1474 ext_jedec = id[3] << 8 | id[4];
1476 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1477 jedec, id[0], id[1], id[2], id[3], id[4]);
1479 for (info = flash_types; info->name; info++) {
1480 if (info->jedec_id == jedec) {
1481 if (info->ext_id && info->ext_id != ext_jedec)
1486 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1491 static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1493 int ret, timeout = 10;
1495 /* Wait for controller to accept mode change */
1497 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1506 writel(mode, fsm->base + SPI_MODESELECT);
1511 static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1516 /* TODO: Make this dynamic */
1517 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1520 * Calculate clk_div - values between 2 and 128
1521 * Multiple of 2, rounded up
1523 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1526 else if (clk_div > 128)
1530 * Determine a suitable delay for the IP to complete a change of
1531 * direction of the FIFO. The required delay is related to the clock
1532 * divider used. The following heuristics are based on empirical tests,
1533 * using a 100MHz EMI clock.
1536 fsm->fifo_dir_delay = 0;
1537 else if (clk_div <= 10)
1538 fsm->fifo_dir_delay = 1;
1540 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1542 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1543 emi_freq, spi_freq, clk_div);
1545 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1548 static int stfsm_init(struct stfsm *fsm)
1552 /* Perform a soft reset of the FSM controller */
1553 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1555 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1557 /* Set clock to 'safe' frequency initially */
1558 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1561 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1565 /* Set timing parameters */
1566 writel(SPI_CFG_DEVICE_ST |
1567 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1568 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1569 SPI_CFG_DEFAULT_DATA_HOLD,
1570 fsm->base + SPI_CONFIGDATA);
1571 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1573 /* Clear FIFO, just in case */
1574 stfsm_clear_fifo(fsm);
1579 static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1581 struct stfsm *fsm = platform_get_drvdata(pdev);
1582 struct device_node *np = pdev->dev.of_node;
1583 struct regmap *regmap;
1584 uint32_t boot_device_reg;
1585 uint32_t boot_device_spi;
1586 uint32_t boot_device; /* Value we read from *boot_device_reg */
1589 /* Booting from SPI NOR Flash is the default */
1590 fsm->booted_from_spi = true;
1592 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1594 goto boot_device_fail;
1596 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1598 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1600 /* Where in the syscon the boot device information lives */
1601 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1603 goto boot_device_fail;
1605 /* Boot device value when booted from SPI NOR */
1606 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1608 goto boot_device_fail;
1610 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1612 goto boot_device_fail;
1614 if (boot_device != boot_device_spi)
1615 fsm->booted_from_spi = false;
1620 dev_warn(&pdev->dev,
1621 "failed to fetch boot device, assuming boot from SPI\n");
1624 static int stfsm_probe(struct platform_device *pdev)
1626 struct device_node *np = pdev->dev.of_node;
1627 struct flash_info *info;
1628 struct resource *res;
1633 dev_err(&pdev->dev, "No DT found\n");
1637 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1641 fsm->dev = &pdev->dev;
1643 platform_set_drvdata(pdev, fsm);
1645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1647 dev_err(&pdev->dev, "Resource not found\n");
1651 fsm->base = devm_ioremap_resource(&pdev->dev, res);
1652 if (IS_ERR(fsm->base)) {
1654 "Failed to reserve memory region %pR\n", res);
1655 return PTR_ERR(fsm->base);
1658 mutex_init(&fsm->lock);
1660 ret = stfsm_init(fsm);
1662 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
1666 stfsm_fetch_platform_configs(pdev);
1668 /* Detect SPI FLASH device */
1669 info = stfsm_jedec_probe(fsm);
1674 /* Use device size to determine address width */
1675 if (info->sector_size * info->n_sectors > 0x1000000)
1676 info->flags |= FLASH_FLAG_32BIT_ADDR;
1679 * Configure READ/WRITE/ERASE sequences according to platform and
1683 ret = info->config(fsm);
1687 ret = stfsm_prepare_rwe_seqs_default(fsm);
1692 fsm->mtd.dev.parent = &pdev->dev;
1693 fsm->mtd.type = MTD_NORFLASH;
1694 fsm->mtd.writesize = 4;
1695 fsm->mtd.writebufsize = fsm->mtd.writesize;
1696 fsm->mtd.flags = MTD_CAP_NORFLASH;
1697 fsm->mtd.size = info->sector_size * info->n_sectors;
1698 fsm->mtd.erasesize = info->sector_size;
1700 fsm->mtd._read = stfsm_mtd_read;
1701 fsm->mtd._write = stfsm_mtd_write;
1702 fsm->mtd._erase = stfsm_mtd_erase;
1704 dev_info(&pdev->dev,
1705 "Found serial flash device: %s\n"
1706 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
1708 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
1709 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
1711 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
1714 static int stfsm_remove(struct platform_device *pdev)
1716 struct stfsm *fsm = platform_get_drvdata(pdev);
1719 err = mtd_device_unregister(&fsm->mtd);
1726 static struct of_device_id stfsm_match[] = {
1727 { .compatible = "st,spi-fsm", },
1730 MODULE_DEVICE_TABLE(of, stfsm_match);
1732 static struct platform_driver stfsm_driver = {
1733 .probe = stfsm_probe,
1734 .remove = stfsm_remove,
1736 .name = "st-spi-fsm",
1737 .owner = THIS_MODULE,
1738 .of_match_table = stfsm_match,
1741 module_platform_driver(stfsm_driver);
1743 MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
1744 MODULE_DESCRIPTION("ST SPI FSM driver");
1745 MODULE_LICENSE("GPL");