blob: a3d7334350d97d4c413cf7b81f99519df37a82c9 [file] [log] [blame]
Lee Jonesd90db4a2014-03-20 09:20:33 +00001/*
2 * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
3 *
4 * Author: Angus Clark <angus.clark@st.com>
5 *
Lee Jonesf1919cb2014-03-20 11:11:45 +00006 * Copyright (C) 2010-2014 STMicroelectronics Limited
Lee Jonesd90db4a2014-03-20 09:20:33 +00007 *
8 * JEDEC probe based on drivers/mtd/devices/m25p80.c
9 *
10 * This code is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15#include <linux/kernel.h>
16#include <linux/module.h>
Lee Jonesa63984c2014-03-20 09:20:46 +000017#include <linux/regmap.h>
Lee Jonesd90db4a2014-03-20 09:20:33 +000018#include <linux/platform_device.h>
Lee Jonesa63984c2014-03-20 09:20:46 +000019#include <linux/mfd/syscon.h>
Lee Jonesd90db4a2014-03-20 09:20:33 +000020#include <linux/mtd/mtd.h>
Lee Jones221cff12014-03-20 09:21:07 +000021#include <linux/mtd/partitions.h>
Lee Jonesd90db4a2014-03-20 09:20:33 +000022#include <linux/sched.h>
23#include <linux/delay.h>
24#include <linux/io.h>
25#include <linux/of.h>
26
Lee Jones5549fbd2014-03-20 09:20:39 +000027#include "serial_flash_cmds.h"
28
Lee Jonesbc09fb52014-03-20 09:20:34 +000029/*
30 * FSM SPI Controller Registers
31 */
32#define SPI_CLOCKDIV 0x0010
33#define SPI_MODESELECT 0x0018
34#define SPI_CONFIGDATA 0x0020
35#define SPI_STA_MODE_CHANGE 0x0028
36#define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
37#define SPI_FAST_SEQ_ADD1 0x0104
38#define SPI_FAST_SEQ_ADD2 0x0108
39#define SPI_FAST_SEQ_ADD_CFG 0x010c
40#define SPI_FAST_SEQ_OPC1 0x0110
41#define SPI_FAST_SEQ_OPC2 0x0114
42#define SPI_FAST_SEQ_OPC3 0x0118
43#define SPI_FAST_SEQ_OPC4 0x011c
44#define SPI_FAST_SEQ_OPC5 0x0120
45#define SPI_MODE_BITS 0x0124
46#define SPI_DUMMY_BITS 0x0128
47#define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
48#define SPI_FAST_SEQ_1 0x0130
49#define SPI_FAST_SEQ_2 0x0134
50#define SPI_FAST_SEQ_3 0x0138
51#define SPI_FAST_SEQ_4 0x013c
52#define SPI_FAST_SEQ_CFG 0x0140
53#define SPI_FAST_SEQ_STA 0x0144
54#define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
55#define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
56#define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
57#define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
58#define SPI_PROGRAM_ERASE_TIME 0x0158
59#define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
60#define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
61#define SPI_STATUS_WR_TIME_REG 0x0164
62#define SPI_FAST_SEQ_DATA_REG 0x0300
63
64/*
65 * Register: SPI_MODESELECT
66 */
67#define SPI_MODESELECT_CONTIG 0x01
68#define SPI_MODESELECT_FASTREAD 0x02
69#define SPI_MODESELECT_DUALIO 0x04
70#define SPI_MODESELECT_FSM 0x08
71#define SPI_MODESELECT_QUADBOOT 0x10
72
73/*
74 * Register: SPI_CONFIGDATA
75 */
76#define SPI_CFG_DEVICE_ST 0x1
77#define SPI_CFG_DEVICE_ATMEL 0x4
78#define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
79#define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
80#define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
81
Lee Jones86f309fd2014-03-20 09:20:35 +000082#define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
83#define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
84#define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
85
Lee Jonesbc09fb52014-03-20 09:20:34 +000086/*
87 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
88 */
89#define TRANSFER_SIZE(x) ((x) * 8)
90
91/*
92 * Register: SPI_FAST_SEQ_ADD_CFG
93 */
94#define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
95#define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
96#define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
97#define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
98#define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
99#define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
100#define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
101#define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
102#define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
103#define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
104
105/*
106 * Register: SPI_FAST_SEQ_n
107 */
108#define SEQ_OPC_OPCODE(x) ((x) << 0)
109#define SEQ_OPC_CYCLES(x) ((x) << 8)
110#define SEQ_OPC_PADS_1 (0x0 << 14)
111#define SEQ_OPC_PADS_2 (0x1 << 14)
112#define SEQ_OPC_PADS_4 (0x3 << 14)
113#define SEQ_OPC_CSDEASSERT (1 << 16)
114
115/*
116 * Register: SPI_FAST_SEQ_CFG
117 */
118#define SEQ_CFG_STARTSEQ (1 << 0)
119#define SEQ_CFG_SWRESET (1 << 5)
120#define SEQ_CFG_CSDEASSERT (1 << 6)
121#define SEQ_CFG_READNOTWRITE (1 << 7)
122#define SEQ_CFG_ERASE (1 << 8)
123#define SEQ_CFG_PADS_1 (0x0 << 16)
124#define SEQ_CFG_PADS_2 (0x1 << 16)
125#define SEQ_CFG_PADS_4 (0x3 << 16)
126
127/*
128 * Register: SPI_MODE_BITS
129 */
130#define MODE_DATA(x) (x & 0xff)
131#define MODE_CYCLES(x) ((x & 0x3f) << 16)
132#define MODE_PADS_1 (0x0 << 22)
133#define MODE_PADS_2 (0x1 << 22)
134#define MODE_PADS_4 (0x3 << 22)
135#define DUMMY_CSDEASSERT (1 << 24)
136
137/*
138 * Register: SPI_DUMMY_BITS
139 */
140#define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
141#define DUMMY_PADS_1 (0x0 << 22)
142#define DUMMY_PADS_2 (0x1 << 22)
143#define DUMMY_PADS_4 (0x3 << 22)
144#define DUMMY_CSDEASSERT (1 << 24)
145
146/*
147 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
148 */
149#define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
150#define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
151#define STA_PADS_1 (0x0 << 16)
152#define STA_PADS_2 (0x1 << 16)
153#define STA_PADS_4 (0x3 << 16)
154#define STA_CSDEASSERT (0x1 << 20)
155#define STA_RDNOTWR (0x1 << 21)
156
157/*
158 * FSM SPI Instruction Opcodes
159 */
160#define STFSM_OPC_CMD 0x1
161#define STFSM_OPC_ADD 0x2
162#define STFSM_OPC_STA 0x3
163#define STFSM_OPC_MODE 0x4
164#define STFSM_OPC_DUMMY 0x5
165#define STFSM_OPC_DATA 0x6
166#define STFSM_OPC_WAIT 0x7
167#define STFSM_OPC_JUMP 0x8
168#define STFSM_OPC_GOTO 0x9
169#define STFSM_OPC_STOP 0xF
170
171/*
172 * FSM SPI Instructions (== opcode + operand).
173 */
174#define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
175
176#define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
177#define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
178#define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
179#define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
180#define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
181#define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
182#define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
183
184#define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
185#define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
186
187#define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
188#define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
189#define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
190#define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
191
192#define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
193#define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
194#define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
195#define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
196
Lee Jones86f309fd2014-03-20 09:20:35 +0000197#define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
198#define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
199
200#define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
201
Lee Jones3c8b85b2014-03-20 09:20:36 +0000202#define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
203
Lee Jonese85a6192014-03-20 09:20:54 +0000204/* Flash Commands */
205#define FLASH_CMD_WREN 0x06
206#define FLASH_CMD_WRDI 0x04
207#define FLASH_CMD_RDID 0x9f
208#define FLASH_CMD_RDSR 0x05
209#define FLASH_CMD_RDSR2 0x35
210#define FLASH_CMD_WRSR 0x01
211#define FLASH_CMD_SE_4K 0x20
212#define FLASH_CMD_SE_32K 0x52
213#define FLASH_CMD_SE 0xd8
214#define FLASH_CMD_CHIPERASE 0xc7
Lee Jonese85a6192014-03-20 09:20:54 +0000215
216#define FLASH_CMD_READ 0x03 /* READ */
217#define FLASH_CMD_READ_FAST 0x0b /* FAST READ */
218#define FLASH_CMD_READ_1_1_2 0x3b /* DUAL OUTPUT READ */
219#define FLASH_CMD_READ_1_2_2 0xbb /* DUAL I/O READ */
220#define FLASH_CMD_READ_1_1_4 0x6b /* QUAD OUTPUT READ */
221#define FLASH_CMD_READ_1_4_4 0xeb /* QUAD I/O READ */
222
223#define FLASH_CMD_WRITE 0x02 /* PAGE PROGRAM */
224#define FLASH_CMD_WRITE_1_1_2 0xa2 /* DUAL INPUT PROGRAM */
225#define FLASH_CMD_WRITE_1_2_2 0xd2 /* DUAL INPUT EXT PROGRAM */
226#define FLASH_CMD_WRITE_1_1_4 0x32 /* QUAD INPUT PROGRAM */
227#define FLASH_CMD_WRITE_1_4_4 0x12 /* QUAD INPUT EXT PROGRAM */
228
229#define FLASH_CMD_EN4B_ADDR 0xb7 /* Enter 4-byte address mode */
230#define FLASH_CMD_EX4B_ADDR 0xe9 /* Exit 4-byte address mode */
231
232/* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
233#define FLASH_CMD_READ4 0x13
234#define FLASH_CMD_READ4_FAST 0x0c
235#define FLASH_CMD_READ4_1_1_2 0x3c
236#define FLASH_CMD_READ4_1_2_2 0xbc
237#define FLASH_CMD_READ4_1_1_4 0x6c
238#define FLASH_CMD_READ4_1_4_4 0xec
239
Lee Jones5343a122014-03-20 09:21:04 +0000240/* S25FLxxxS commands */
241#define S25FL_CMD_WRITE4_1_1_4 0x34
242#define S25FL_CMD_SE4 0xdc
243#define S25FL_CMD_CLSR 0x30
244#define S25FL_CMD_DYBWR 0xe1
245#define S25FL_CMD_DYBRD 0xe0
246#define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
247 * 'FLASH_CMD_WRITE_1_4_4'
248 * as found on N25Qxxx devices! */
249
Lee Jones176b4372014-03-20 09:20:59 +0000250/* Status register */
251#define FLASH_STATUS_BUSY 0x01
252#define FLASH_STATUS_WEL 0x02
253#define FLASH_STATUS_BP0 0x04
254#define FLASH_STATUS_BP1 0x08
255#define FLASH_STATUS_BP2 0x10
256#define FLASH_STATUS_SRWP0 0x80
257#define FLASH_STATUS_TIMEOUT 0xff
Lee Jones5343a122014-03-20 09:21:04 +0000258/* S25FL Error Flags */
259#define S25FL_STATUS_E_ERR 0x20
260#define S25FL_STATUS_P_ERR 0x40
Lee Jones176b4372014-03-20 09:20:59 +0000261
Angus Clark5d0bdda2014-03-26 16:39:18 +0000262#define N25Q_CMD_WRVCR 0x81
263#define N25Q_CMD_RDVCR 0x85
264#define N25Q_CMD_RDVECR 0x65
265#define N25Q_CMD_RDNVCR 0xb5
266#define N25Q_CMD_WRNVCR 0xb1
267
Lee Jonese514f102014-03-20 09:20:57 +0000268#define FLASH_PAGESIZE 256 /* In Bytes */
269#define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
Lee Jones176b4372014-03-20 09:20:59 +0000270#define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
Lee Jonese514f102014-03-20 09:20:57 +0000271
Lee Jonese85a6192014-03-20 09:20:54 +0000272/*
273 * Flags to tweak operation of default read/write/erase routines
274 */
275#define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
276#define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
Lee Jonese85a6192014-03-20 09:20:54 +0000277#define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
278#define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
279
Lee Jonese6b1bb42014-03-20 09:21:06 +0000280struct stfsm_seq {
281 uint32_t data_size;
282 uint32_t addr1;
283 uint32_t addr2;
284 uint32_t addr_cfg;
285 uint32_t seq_opc[5];
286 uint32_t mode;
287 uint32_t dummy;
288 uint32_t status;
289 uint8_t seq[16];
290 uint32_t seq_cfg;
291} __packed __aligned(4);
292
Lee Jonesd90db4a2014-03-20 09:20:33 +0000293struct stfsm {
294 struct device *dev;
295 void __iomem *base;
296 struct resource *region;
297 struct mtd_info mtd;
298 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000299 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000300
Lee Jonese85a6192014-03-20 09:20:54 +0000301 uint32_t configuration;
Lee Jones86f309fd2014-03-20 09:20:35 +0000302 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000303 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000304 bool reset_signal;
305 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000306
Lee Jonese6b1bb42014-03-20 09:21:06 +0000307 struct stfsm_seq stfsm_seq_read;
308 struct stfsm_seq stfsm_seq_write;
309 struct stfsm_seq stfsm_seq_en_32bit_addr;
310};
Lee Jones3c8b85b2014-03-20 09:20:36 +0000311
Lee Jones08981272014-03-20 09:20:42 +0000312/* Parameters to configure a READ or WRITE FSM sequence */
313struct seq_rw_config {
314 uint32_t flags; /* flags to support config */
315 uint8_t cmd; /* FLASH command */
316 int write; /* Write Sequence */
317 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
318 uint8_t data_pads; /* No. of data pads */
319 uint8_t mode_data; /* MODE data */
320 uint8_t mode_cycles; /* No. of MODE cycles */
321 uint8_t dummy_cycles; /* No. of DUMMY cycles */
322};
323
Lee Jones11d7f822014-03-20 09:20:40 +0000324/* SPI Flash Device Table */
325struct flash_info {
326 char *name;
327 /*
328 * JEDEC id zero means "no ID" (most older chips); otherwise it has
329 * a high byte of zero plus three data bytes: the manufacturer id,
330 * then a two byte device id.
331 */
332 u32 jedec_id;
333 u16 ext_id;
334 /*
335 * The size listed here is what works with FLASH_CMD_SE, which isn't
336 * necessarily called a "sector" by the vendor.
337 */
338 unsigned sector_size;
339 u16 n_sectors;
340 u32 flags;
341 /*
342 * Note, where FAST_READ is supported, freq_max specifies the
343 * FAST_READ frequency, not the READ frequency.
344 */
345 u32 max_freq;
346 int (*config)(struct stfsm *);
347};
348
Lee Jones218b8702014-03-20 09:20:55 +0000349static int stfsm_n25q_config(struct stfsm *fsm);
Lee Jones898180662014-03-20 09:21:03 +0000350static int stfsm_mx25_config(struct stfsm *fsm);
Lee Jones5343a122014-03-20 09:21:04 +0000351static int stfsm_s25fl_config(struct stfsm *fsm);
Lee Jonescd7cac92014-03-20 09:21:05 +0000352static int stfsm_w25q_config(struct stfsm *fsm);
Lee Jones218b8702014-03-20 09:20:55 +0000353
Lee Jones11d7f822014-03-20 09:20:40 +0000354static struct flash_info flash_types[] = {
355 /*
356 * ST Microelectronics/Numonyx --
357 * (newer production versions may have feature updates
358 * (eg faster operating frequency)
359 */
360#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
361 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
362 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
363 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
364 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
365 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
366 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
367
368#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
369 FLASH_FLAG_READ_FAST | \
370 FLASH_FLAG_READ_1_1_2 | \
371 FLASH_FLAG_WRITE_1_1_2)
372 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
373 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
374
375#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
376 FLASH_FLAG_READ_FAST | \
377 FLASH_FLAG_READ_1_1_2 | \
378 FLASH_FLAG_READ_1_2_2 | \
379 FLASH_FLAG_READ_1_1_4 | \
380 FLASH_FLAG_READ_1_4_4 | \
381 FLASH_FLAG_SE_4K | \
382 FLASH_FLAG_SE_32K)
383 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
Lee Jones898180662014-03-20 09:21:03 +0000384 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
385 stfsm_mx25_config },
Angus Clark5fa98062014-03-26 16:39:15 +0000386 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
387 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
388 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000389
390#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
391 FLASH_FLAG_READ_FAST | \
392 FLASH_FLAG_READ_1_1_2 | \
393 FLASH_FLAG_READ_1_2_2 | \
394 FLASH_FLAG_READ_1_1_4 | \
395 FLASH_FLAG_READ_1_4_4 | \
396 FLASH_FLAG_WRITE_1_1_2 | \
397 FLASH_FLAG_WRITE_1_2_2 | \
398 FLASH_FLAG_WRITE_1_1_4 | \
399 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000400 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
401 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000402 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000403 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000404
405 /*
406 * Spansion S25FLxxxP
407 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
408 */
409#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
410 FLASH_FLAG_READ_1_1_2 | \
411 FLASH_FLAG_READ_1_2_2 | \
412 FLASH_FLAG_READ_1_1_4 | \
413 FLASH_FLAG_READ_1_4_4 | \
414 FLASH_FLAG_WRITE_1_1_4 | \
415 FLASH_FLAG_READ_FAST)
Angus Clark85bdcf62014-03-26 16:39:19 +0000416 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
417 stfsm_s25fl_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000418 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000419 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000420 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000421 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000422
423 /*
424 * Spansion S25FLxxxS
425 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
426 * - RESET# signal supported by die but not bristled out on all
427 * package types. The package type is a function of board design,
428 * so this information is captured in the board's flags.
429 * - Supports 'DYB' sector protection. Depending on variant, sectors
430 * may default to locked state on power-on.
431 */
432#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
433 FLASH_FLAG_RESET | \
434 FLASH_FLAG_DYB_LOCKING)
435 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000436 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000437 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000438 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000439 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
Lee Jones5343a122014-03-20 09:21:04 +0000440 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000441 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
Lee Jones5343a122014-03-20 09:21:04 +0000442 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000443
444 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
445#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
446 FLASH_FLAG_READ_FAST | \
447 FLASH_FLAG_READ_1_1_2 | \
448 FLASH_FLAG_WRITE_1_1_2)
449 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
450 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
451 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
452 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
453 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
454
455 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
456#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
457 FLASH_FLAG_READ_FAST | \
458 FLASH_FLAG_READ_1_1_2 | \
459 FLASH_FLAG_READ_1_2_2 | \
460 FLASH_FLAG_READ_1_1_4 | \
461 FLASH_FLAG_READ_1_4_4 | \
462 FLASH_FLAG_WRITE_1_1_4)
Lee Jonescd7cac92014-03-20 09:21:05 +0000463 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
464 stfsm_w25q_config },
465 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
466 stfsm_w25q_config },
467 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
468 stfsm_w25q_config },
469 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
470 stfsm_w25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000471
472 /* Sentinel */
473 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
474};
475
Lee Jonesa37b2f52014-03-20 09:20:53 +0000476/*
477 * FSM message sequence configurations:
478 *
479 * All configs are presented in order of preference
480 */
481
482/* Default READ configurations, in order of preference */
483static struct seq_rw_config default_read_configs[] = {
484 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
485 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
486 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
487 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
488 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
489 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
490 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
491};
492
493/* Default WRITE configurations */
494static struct seq_rw_config default_write_configs[] = {
495 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
496 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
497 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
498 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
499 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
500 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
501};
502
Lee Jonese85a6192014-03-20 09:20:54 +0000503/*
504 * [N25Qxxx] Configuration
505 */
506#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
507#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
508#define N25Q_VCR_WRAP_CONT 0x3
509
510/* N25Q 3-byte Address READ configurations
511 * - 'FAST' variants configured for 8 dummy cycles.
512 *
513 * Note, the number of dummy cycles used for 'FAST' READ operations is
514 * configurable and would normally be tuned according to the READ command and
515 * operating frequency. However, this applies universally to all 'FAST' READ
516 * commands, including those used by the SPIBoot controller, and remains in
517 * force until the device is power-cycled. Since the SPIBoot controller is
518 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
519 * cycles.
520 */
521static struct seq_rw_config n25q_read3_configs[] = {
522 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
523 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
524 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
525 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
526 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
527 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
528 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
529};
530
531/* N25Q 4-byte Address READ configurations
532 * - use special 4-byte address READ commands (reduces overheads, and
533 * reduces risk of hitting watchdog reset issues).
534 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
535 */
536static struct seq_rw_config n25q_read4_configs[] = {
537 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
538 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
539 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
540 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
541 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
542 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
543 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
544};
545
Lee Jones898180662014-03-20 09:21:03 +0000546/*
547 * [MX25xxx] Configuration
548 */
549#define MX25_STATUS_QE (0x1 << 6)
550
551static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
552{
553 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
554 SEQ_OPC_CYCLES(8) |
555 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR) |
556 SEQ_OPC_CSDEASSERT);
557
558 seq->seq[0] = STFSM_INST_CMD1;
559 seq->seq[1] = STFSM_INST_WAIT;
560 seq->seq[2] = STFSM_INST_STOP;
561
562 seq->seq_cfg = (SEQ_CFG_PADS_1 |
563 SEQ_CFG_ERASE |
564 SEQ_CFG_READNOTWRITE |
565 SEQ_CFG_CSDEASSERT |
566 SEQ_CFG_STARTSEQ);
567
568 return 0;
569}
570
Lee Jones5343a122014-03-20 09:21:04 +0000571/*
572 * [S25FLxxx] Configuration
573 */
574#define STFSM_S25FL_CONFIG_QE (0x1 << 1)
575
576/*
577 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
578 * Register, Extended Address Modes, and a 32-bit address command set. The
579 * 32-bit address command set is used here, since it avoids any problems with
580 * entering a state that is incompatible with the SPIBoot Controller.
581 */
582static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
583 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
584 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
585 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
586 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
587 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
588 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
589 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
590};
591
592static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
593 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
594 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
595 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
596};
597
Lee Jonescd7cac92014-03-20 09:21:05 +0000598/*
599 * [W25Qxxx] Configuration
600 */
Angus Clark5d0bdda2014-03-26 16:39:18 +0000601#define W25Q_STATUS_QE (0x1 << 1)
Lee Jonescd7cac92014-03-20 09:21:05 +0000602
Lee Jones1bd512b2014-03-20 09:20:38 +0000603static struct stfsm_seq stfsm_seq_read_jedec = {
604 .data_size = TRANSFER_SIZE(8),
605 .seq_opc[0] = (SEQ_OPC_PADS_1 |
606 SEQ_OPC_CYCLES(8) |
607 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
608 .seq = {
609 STFSM_INST_CMD1,
610 STFSM_INST_DATA_READ,
611 STFSM_INST_STOP,
612 },
613 .seq_cfg = (SEQ_CFG_PADS_1 |
614 SEQ_CFG_READNOTWRITE |
615 SEQ_CFG_CSDEASSERT |
616 SEQ_CFG_STARTSEQ),
617};
618
Lee Jones176b4372014-03-20 09:20:59 +0000619static struct stfsm_seq stfsm_seq_read_status_fifo = {
620 .data_size = TRANSFER_SIZE(4),
621 .seq_opc[0] = (SEQ_OPC_PADS_1 |
622 SEQ_OPC_CYCLES(8) |
623 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
624 .seq = {
625 STFSM_INST_CMD1,
626 STFSM_INST_DATA_READ,
627 STFSM_INST_STOP,
628 },
629 .seq_cfg = (SEQ_CFG_PADS_1 |
630 SEQ_CFG_READNOTWRITE |
631 SEQ_CFG_CSDEASSERT |
632 SEQ_CFG_STARTSEQ),
633};
634
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000635static struct stfsm_seq stfsm_seq_erase_sector = {
636 /* 'addr_cfg' configured during initialisation */
637 .seq_opc = {
638 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
639 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
640
641 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
642 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
643 },
644 .seq = {
645 STFSM_INST_CMD1,
646 STFSM_INST_CMD2,
647 STFSM_INST_ADD1,
648 STFSM_INST_ADD2,
649 STFSM_INST_STOP,
650 },
651 .seq_cfg = (SEQ_CFG_PADS_1 |
652 SEQ_CFG_READNOTWRITE |
653 SEQ_CFG_CSDEASSERT |
654 SEQ_CFG_STARTSEQ),
655};
656
Lee Jones4a341fe2014-03-20 09:21:00 +0000657static struct stfsm_seq stfsm_seq_erase_chip = {
658 .seq_opc = {
659 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
660 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
661
662 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
663 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
664 },
665 .seq = {
666 STFSM_INST_CMD1,
667 STFSM_INST_CMD2,
668 STFSM_INST_WAIT,
669 STFSM_INST_STOP,
670 },
671 .seq_cfg = (SEQ_CFG_PADS_1 |
672 SEQ_CFG_ERASE |
673 SEQ_CFG_READNOTWRITE |
674 SEQ_CFG_CSDEASSERT |
675 SEQ_CFG_STARTSEQ),
676};
677
Lee Jones150571b2014-03-20 09:21:02 +0000678static struct stfsm_seq stfsm_seq_write_status = {
679 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
680 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
681 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
682 SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
683 .seq = {
684 STFSM_INST_CMD1,
685 STFSM_INST_CMD2,
686 STFSM_INST_STA_WR1,
687 STFSM_INST_STOP,
688 },
689 .seq_cfg = (SEQ_CFG_PADS_1 |
690 SEQ_CFG_READNOTWRITE |
691 SEQ_CFG_CSDEASSERT |
692 SEQ_CFG_STARTSEQ),
693};
694
Lee Jones6bd29602014-03-20 09:20:48 +0000695static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
696{
697 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
698 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
699 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
700 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
701 SEQ_OPC_CSDEASSERT);
702
703 seq->seq[0] = STFSM_INST_CMD2;
704 seq->seq[1] = STFSM_INST_CMD1;
705 seq->seq[2] = STFSM_INST_WAIT;
706 seq->seq[3] = STFSM_INST_STOP;
707
708 seq->seq_cfg = (SEQ_CFG_PADS_1 |
709 SEQ_CFG_ERASE |
710 SEQ_CFG_READNOTWRITE |
711 SEQ_CFG_CSDEASSERT |
712 SEQ_CFG_STARTSEQ);
713
714 return 0;
715}
716
Lee Jones3c8b85b2014-03-20 09:20:36 +0000717static inline int stfsm_is_idle(struct stfsm *fsm)
718{
719 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
720}
721
Lee Jones86f309fd2014-03-20 09:20:35 +0000722static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
723{
724 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
725}
726
727static void stfsm_clear_fifo(struct stfsm *fsm)
728{
729 uint32_t avail;
730
731 for (;;) {
732 avail = stfsm_fifo_available(fsm);
733 if (!avail)
734 break;
735
736 while (avail) {
737 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
738 avail--;
739 }
740 }
741}
742
Lee Jones3c8b85b2014-03-20 09:20:36 +0000743static inline void stfsm_load_seq(struct stfsm *fsm,
744 const struct stfsm_seq *seq)
745{
746 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
747 const uint32_t *src = (const uint32_t *)seq;
748 int words = sizeof(*seq) / sizeof(*src);
749
750 BUG_ON(!stfsm_is_idle(fsm));
751
752 while (words--) {
753 writel(*src, dst);
754 src++;
755 dst += 4;
756 }
757}
758
759static void stfsm_wait_seq(struct stfsm *fsm)
760{
761 unsigned long deadline;
762 int timeout = 0;
763
764 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
765
766 while (!timeout) {
767 if (time_after_eq(jiffies, deadline))
768 timeout = 1;
769
770 if (stfsm_is_idle(fsm))
771 return;
772
773 cond_resched();
774 }
775
776 dev_err(fsm->dev, "timeout on sequence completion\n");
777}
778
Lee Jones3f9d7202014-03-20 11:11:43 +0000779static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
Lee Jones030e82d2014-03-20 09:20:37 +0000780{
781 uint32_t remaining = size >> 2;
782 uint32_t avail;
783 uint32_t words;
784
785 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
786
787 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
788
789 while (remaining) {
790 for (;;) {
791 avail = stfsm_fifo_available(fsm);
792 if (avail)
793 break;
794 udelay(1);
795 }
796 words = min(avail, remaining);
797 remaining -= words;
798
799 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
800 buf += words;
801 }
802}
803
Lee Jones3f9d7202014-03-20 11:11:43 +0000804static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
805 uint32_t size)
Lee Jones30ca64f2014-03-20 09:20:58 +0000806{
807 uint32_t words = size >> 2;
808
809 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
810
811 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
812
813 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
814
815 return size;
816}
817
Lee Jones0de08e42014-03-20 09:20:51 +0000818static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
819{
Lee Jonese6b1bb42014-03-20 09:21:06 +0000820 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
Lee Jones0de08e42014-03-20 09:20:51 +0000821 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
822
823 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
824 SEQ_OPC_CYCLES(8) |
825 SEQ_OPC_OPCODE(cmd) |
826 SEQ_OPC_CSDEASSERT);
827
828 stfsm_load_seq(fsm, seq);
829
830 stfsm_wait_seq(fsm);
831
832 return 0;
833}
834
Lee Jones176b4372014-03-20 09:20:59 +0000835static uint8_t stfsm_wait_busy(struct stfsm *fsm)
836{
837 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
838 unsigned long deadline;
839 uint32_t status;
840 int timeout = 0;
841
842 /* Use RDRS1 */
843 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
844 SEQ_OPC_CYCLES(8) |
845 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
846
847 /* Load read_status sequence */
848 stfsm_load_seq(fsm, seq);
849
850 /*
851 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
852 */
853 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
854 while (!timeout) {
Lee Jones176b4372014-03-20 09:20:59 +0000855 if (time_after_eq(jiffies, deadline))
856 timeout = 1;
857
858 stfsm_wait_seq(fsm);
859
860 stfsm_read_fifo(fsm, &status, 4);
861
862 if ((status & FLASH_STATUS_BUSY) == 0)
863 return 0;
864
865 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
866 ((status & S25FL_STATUS_P_ERR) ||
867 (status & S25FL_STATUS_E_ERR)))
868 return (uint8_t)(status & 0xff);
869
870 if (!timeout)
871 /* Restart */
872 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
Lee Jonesea7864b2014-03-20 11:11:46 +0000873
874 cond_resched();
Lee Jones176b4372014-03-20 09:20:59 +0000875 }
876
877 dev_err(fsm->dev, "timeout on wait_busy\n");
878
879 return FLASH_STATUS_TIMEOUT;
880}
881
Lee Jonesac94dbc2014-03-20 09:21:01 +0000882static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
Angus Clark5d0bdda2014-03-26 16:39:18 +0000883 uint8_t *data, int bytes)
Lee Jonesac94dbc2014-03-20 09:21:01 +0000884{
885 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
886 uint32_t tmp;
Angus Clark5d0bdda2014-03-26 16:39:18 +0000887 uint8_t *t = (uint8_t *)&tmp;
888 int i;
Lee Jonesac94dbc2014-03-20 09:21:01 +0000889
Angus Clark5d0bdda2014-03-26 16:39:18 +0000890 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
891 cmd, bytes);
Lee Jonesac94dbc2014-03-20 09:21:01 +0000892
Angus Clark5d0bdda2014-03-26 16:39:18 +0000893 BUG_ON(bytes != 1 && bytes != 2);
894
895 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Lee Jonesac94dbc2014-03-20 09:21:01 +0000896 SEQ_OPC_OPCODE(cmd)),
897
898 stfsm_load_seq(fsm, seq);
899
900 stfsm_read_fifo(fsm, &tmp, 4);
901
Angus Clark5d0bdda2014-03-26 16:39:18 +0000902 for (i = 0; i < bytes; i++)
903 data[i] = t[i];
Lee Jonesac94dbc2014-03-20 09:21:01 +0000904
905 stfsm_wait_seq(fsm);
906
907 return 0;
908}
909
Angus Clark5d0bdda2014-03-26 16:39:18 +0000910static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
911 uint16_t data, int bytes, int wait_busy)
Lee Jones150571b2014-03-20 09:21:02 +0000912{
913 struct stfsm_seq *seq = &stfsm_seq_write_status;
914
Angus Clark5d0bdda2014-03-26 16:39:18 +0000915 dev_dbg(fsm->dev,
916 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
917 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
Lee Jones150571b2014-03-20 09:21:02 +0000918
Angus Clark5d0bdda2014-03-26 16:39:18 +0000919 BUG_ON(bytes != 1 && bytes != 2);
920
921 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
922 SEQ_OPC_OPCODE(cmd));
923
924 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
925 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
Lee Jones150571b2014-03-20 09:21:02 +0000926
927 stfsm_load_seq(fsm, seq);
928
929 stfsm_wait_seq(fsm);
930
Angus Clark5d0bdda2014-03-26 16:39:18 +0000931 if (wait_busy)
932 stfsm_wait_busy(fsm);
Lee Jones249516c2014-03-20 09:20:52 +0000933
934 return 0;
935}
936
Lee Jones0ea7d702014-03-20 09:20:50 +0000937/*
938 * SoC reset on 'boot-from-spi' systems
939 *
940 * Certain modes of operation cause the Flash device to enter a particular state
941 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
942 * Addr' commands). On boot-from-spi systems, it is important to consider what
943 * happens if a warm reset occurs during this period. The SPIBoot controller
944 * assumes that Flash device is in its default reset state, 24-bit address mode,
945 * and ready to accept commands. This can be achieved using some form of
946 * on-board logic/controller to force a device POR in response to a SoC-level
947 * reset or by making use of the device reset signal if available (limited
948 * number of devices only).
949 *
950 * Failure to take such precautions can cause problems following a warm reset.
951 * For some operations (e.g. ERASE), there is little that can be done. For
952 * other modes of operation (e.g. 32-bit addressing), options are often
953 * available that can help minimise the window in which a reset could cause a
954 * problem.
955 *
956 */
957static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
958{
959 /* Reset signal is available on the board and supported by the device */
960 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
961 return true;
962
963 /* Board-level logic forces a power-on-reset */
964 if (fsm->reset_por)
965 return true;
966
967 /* Reset is not properly handled and may result in failure to reboot */
968 return false;
969}
970
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000971/* Configure 'addr_cfg' according to addressing mode */
972static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
973 struct stfsm_seq *seq)
974{
975 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
976
977 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
978 ADR_CFG_PADS_1_ADD1 |
979 ADR_CFG_CYCLES_ADD2(16) |
980 ADR_CFG_PADS_1_ADD2 |
981 ADR_CFG_CSDEASSERT_ADD2);
982}
983
Lee Jones08981272014-03-20 09:20:42 +0000984/* Search for preferred configuration based on available flags */
985static struct seq_rw_config *
986stfsm_search_seq_rw_configs(struct stfsm *fsm,
987 struct seq_rw_config cfgs[])
988{
989 struct seq_rw_config *config;
990 int flags = fsm->info->flags;
991
992 for (config = cfgs; config->cmd != 0; config++)
993 if ((config->flags & flags) == config->flags)
994 return config;
995
996 return NULL;
997}
998
Lee Jones97ccf2d2014-03-20 09:20:44 +0000999/* Prepare a READ/WRITE sequence according to configuration parameters */
1000static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1001 struct stfsm_seq *seq,
1002 struct seq_rw_config *cfg)
1003{
1004 int addr1_cycles, addr2_cycles;
1005 int i = 0;
1006
1007 memset(seq, 0, sizeof(*seq));
1008
1009 /* Add READ/WRITE OPC */
1010 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1011 SEQ_OPC_CYCLES(8) |
1012 SEQ_OPC_OPCODE(cfg->cmd));
1013
1014 /* Add WREN OPC for a WRITE sequence */
1015 if (cfg->write)
1016 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1017 SEQ_OPC_CYCLES(8) |
1018 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1019 SEQ_OPC_CSDEASSERT);
1020
1021 /* Address configuration (24 or 32-bit addresses) */
1022 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1023 addr1_cycles /= cfg->addr_pads;
1024 addr2_cycles = 16 / cfg->addr_pads;
1025 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
1026 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
1027 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
1028 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1029
1030 /* Data/Sequence configuration */
1031 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1032 SEQ_CFG_STARTSEQ |
1033 SEQ_CFG_CSDEASSERT);
1034 if (!cfg->write)
1035 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1036
1037 /* Mode configuration (no. of pads taken from addr cfg) */
1038 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1039 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1040 (cfg->addr_pads - 1) << 22); /* pads */
1041
1042 /* Dummy configuration (no. of pads taken from addr cfg) */
1043 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1044 (cfg->addr_pads - 1) << 22); /* pads */
1045
1046
1047 /* Instruction sequence */
1048 i = 0;
1049 if (cfg->write)
1050 seq->seq[i++] = STFSM_INST_CMD2;
1051
1052 seq->seq[i++] = STFSM_INST_CMD1;
1053
1054 seq->seq[i++] = STFSM_INST_ADD1;
1055 seq->seq[i++] = STFSM_INST_ADD2;
1056
1057 if (cfg->mode_cycles)
1058 seq->seq[i++] = STFSM_INST_MODE;
1059
1060 if (cfg->dummy_cycles)
1061 seq->seq[i++] = STFSM_INST_DUMMY;
1062
1063 seq->seq[i++] =
1064 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1065 seq->seq[i++] = STFSM_INST_STOP;
1066}
1067
Lee Jones88cccb82014-03-20 09:20:49 +00001068static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1069 struct stfsm_seq *seq,
1070 struct seq_rw_config *cfgs)
1071{
1072 struct seq_rw_config *config;
1073
1074 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1075 if (!config) {
1076 dev_err(fsm->dev, "failed to find suitable config\n");
1077 return -EINVAL;
1078 }
1079
1080 stfsm_prepare_rw_seq(fsm, seq, config);
1081
1082 return 0;
1083}
1084
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001085/* Prepare a READ/WRITE/ERASE 'default' sequences */
1086static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1087{
1088 uint32_t flags = fsm->info->flags;
1089 int ret;
1090
1091 /* Configure 'READ' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001092 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001093 default_read_configs);
1094 if (ret) {
1095 dev_err(fsm->dev,
1096 "failed to prep READ sequence with flags [0x%08x]\n",
1097 flags);
1098 return ret;
1099 }
1100
1101 /* Configure 'WRITE' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001102 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001103 default_write_configs);
1104 if (ret) {
1105 dev_err(fsm->dev,
1106 "failed to prep WRITE sequence with flags [0x%08x]\n",
1107 flags);
1108 return ret;
1109 }
1110
1111 /* Configure 'ERASE_SECTOR' sequence */
1112 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1113
1114 return 0;
1115}
1116
Lee Jones898180662014-03-20 09:21:03 +00001117static int stfsm_mx25_config(struct stfsm *fsm)
1118{
1119 uint32_t flags = fsm->info->flags;
1120 uint32_t data_pads;
1121 uint8_t sta;
1122 int ret;
1123 bool soc_reset;
1124
1125 /*
1126 * Use default READ/WRITE sequences
1127 */
1128 ret = stfsm_prepare_rwe_seqs_default(fsm);
1129 if (ret)
1130 return ret;
1131
1132 /*
1133 * Configure 32-bit Address Support
1134 */
1135 if (flags & FLASH_FLAG_32BIT_ADDR) {
1136 /* Configure 'enter_32bitaddr' FSM sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001137 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones898180662014-03-20 09:21:03 +00001138
1139 soc_reset = stfsm_can_handle_soc_reset(fsm);
Angus Clark009e7e62014-03-26 16:39:16 +00001140 if (soc_reset || !fsm->booted_from_spi)
Lee Jones898180662014-03-20 09:21:03 +00001141 /* If we can handle SoC resets, we enable 32-bit address
1142 * mode pervasively */
1143 stfsm_enter_32bit_addr(fsm, 1);
1144
Angus Clark009e7e62014-03-26 16:39:16 +00001145 else
Lee Jones898180662014-03-20 09:21:03 +00001146 /* Else, enable/disable 32-bit addressing before/after
1147 * each operation */
1148 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1149 CFG_WRITE_TOGGLE_32BIT_ADDR |
1150 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
Lee Jones898180662014-03-20 09:21:03 +00001151 }
1152
Angus Clark5d0bdda2014-03-26 16:39:18 +00001153 /* Check status of 'QE' bit, update if required. */
1154 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001155 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones898180662014-03-20 09:21:03 +00001156 if (data_pads == 4) {
Angus Clarkcc666862014-03-26 16:39:17 +00001157 if (!(sta & MX25_STATUS_QE)) {
1158 /* Set 'QE' */
1159 sta |= MX25_STATUS_QE;
1160
Angus Clark5d0bdda2014-03-26 16:39:18 +00001161 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001162 }
1163 } else {
1164 if (sta & MX25_STATUS_QE) {
1165 /* Clear 'QE' */
1166 sta &= ~MX25_STATUS_QE;
1167
Angus Clark5d0bdda2014-03-26 16:39:18 +00001168 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001169 }
Lee Jones898180662014-03-20 09:21:03 +00001170 }
1171
1172 return 0;
1173}
1174
Lee Jones218b8702014-03-20 09:20:55 +00001175static int stfsm_n25q_config(struct stfsm *fsm)
1176{
1177 uint32_t flags = fsm->info->flags;
1178 uint8_t vcr;
1179 int ret = 0;
1180 bool soc_reset;
1181
1182 /* Configure 'READ' sequence */
1183 if (flags & FLASH_FLAG_32BIT_ADDR)
Lee Jonese6b1bb42014-03-20 09:21:06 +00001184 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001185 n25q_read4_configs);
1186 else
Lee Jonese6b1bb42014-03-20 09:21:06 +00001187 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001188 n25q_read3_configs);
1189 if (ret) {
1190 dev_err(fsm->dev,
1191 "failed to prepare READ sequence with flags [0x%08x]\n",
1192 flags);
1193 return ret;
1194 }
1195
1196 /* Configure 'WRITE' sequence (default configs) */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001197 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones218b8702014-03-20 09:20:55 +00001198 default_write_configs);
1199 if (ret) {
1200 dev_err(fsm->dev,
1201 "preparing WRITE sequence using flags [0x%08x] failed\n",
1202 flags);
1203 return ret;
1204 }
1205
1206 /* * Configure 'ERASE_SECTOR' sequence */
1207 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1208
1209 /* Configure 32-bit address support */
1210 if (flags & FLASH_FLAG_32BIT_ADDR) {
Lee Jonese6b1bb42014-03-20 09:21:06 +00001211 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones218b8702014-03-20 09:20:55 +00001212
1213 soc_reset = stfsm_can_handle_soc_reset(fsm);
1214 if (soc_reset || !fsm->booted_from_spi) {
1215 /*
1216 * If we can handle SoC resets, we enable 32-bit
1217 * address mode pervasively
1218 */
1219 stfsm_enter_32bit_addr(fsm, 1);
1220 } else {
1221 /*
1222 * If not, enable/disable for WRITE and ERASE
1223 * operations (READ uses special commands)
1224 */
1225 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1226 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1227 }
1228 }
1229
1230 /*
1231 * Configure device to use 8 dummy cycles
1232 */
1233 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1234 N25Q_VCR_WRAP_CONT);
Angus Clark5d0bdda2014-03-26 16:39:18 +00001235 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
Lee Jones218b8702014-03-20 09:20:55 +00001236
1237 return 0;
1238}
1239
Lee Jones5343a122014-03-20 09:21:04 +00001240static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1241{
1242 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1243 SEQ_OPC_CYCLES(8) |
1244 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1245
1246 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1247 ADR_CFG_PADS_1_ADD1 |
1248 ADR_CFG_CYCLES_ADD2(16) |
1249 ADR_CFG_PADS_1_ADD2 |
1250 ADR_CFG_CSDEASSERT_ADD2);
1251}
1252
1253static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1254{
1255 uint32_t tmp;
1256 struct stfsm_seq seq = {
1257 .data_size = TRANSFER_SIZE(4),
1258 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1259 SEQ_OPC_CYCLES(8) |
1260 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1261 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1262 ADR_CFG_PADS_1_ADD1 |
1263 ADR_CFG_CYCLES_ADD2(16) |
1264 ADR_CFG_PADS_1_ADD2),
1265 .addr1 = (offs >> 16) & 0xffff,
1266 .addr2 = offs & 0xffff,
1267 .seq = {
1268 STFSM_INST_CMD1,
1269 STFSM_INST_ADD1,
1270 STFSM_INST_ADD2,
1271 STFSM_INST_DATA_READ,
1272 STFSM_INST_STOP,
1273 },
1274 .seq_cfg = (SEQ_CFG_PADS_1 |
1275 SEQ_CFG_READNOTWRITE |
1276 SEQ_CFG_CSDEASSERT |
1277 SEQ_CFG_STARTSEQ),
1278 };
1279
1280 stfsm_load_seq(fsm, &seq);
1281
1282 stfsm_read_fifo(fsm, &tmp, 4);
1283
1284 *dby = (uint8_t)(tmp >> 24);
1285
1286 stfsm_wait_seq(fsm);
1287}
1288
1289static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1290{
1291 struct stfsm_seq seq = {
1292 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1293 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1294 SEQ_OPC_CSDEASSERT),
1295 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1296 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1297 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1298 ADR_CFG_PADS_1_ADD1 |
1299 ADR_CFG_CYCLES_ADD2(16) |
1300 ADR_CFG_PADS_1_ADD2),
1301 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1302 .addr1 = (offs >> 16) & 0xffff,
1303 .addr2 = offs & 0xffff,
1304 .seq = {
1305 STFSM_INST_CMD1,
1306 STFSM_INST_CMD2,
1307 STFSM_INST_ADD1,
1308 STFSM_INST_ADD2,
1309 STFSM_INST_STA_WR1,
1310 STFSM_INST_STOP,
1311 },
1312 .seq_cfg = (SEQ_CFG_PADS_1 |
1313 SEQ_CFG_READNOTWRITE |
1314 SEQ_CFG_CSDEASSERT |
1315 SEQ_CFG_STARTSEQ),
1316 };
1317
1318 stfsm_load_seq(fsm, &seq);
1319 stfsm_wait_seq(fsm);
1320
1321 stfsm_wait_busy(fsm);
1322}
1323
1324static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1325{
1326 struct stfsm_seq seq = {
1327 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1328 SEQ_OPC_CYCLES(8) |
1329 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1330 SEQ_OPC_CSDEASSERT),
1331 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1332 SEQ_OPC_CYCLES(8) |
1333 SEQ_OPC_OPCODE(FLASH_CMD_WRDI) |
1334 SEQ_OPC_CSDEASSERT),
1335 .seq = {
1336 STFSM_INST_CMD1,
1337 STFSM_INST_CMD2,
1338 STFSM_INST_WAIT,
1339 STFSM_INST_STOP,
1340 },
1341 .seq_cfg = (SEQ_CFG_PADS_1 |
1342 SEQ_CFG_ERASE |
1343 SEQ_CFG_READNOTWRITE |
1344 SEQ_CFG_CSDEASSERT |
1345 SEQ_CFG_STARTSEQ),
1346 };
1347
1348 stfsm_load_seq(fsm, &seq);
1349
1350 stfsm_wait_seq(fsm);
1351
1352 return 0;
1353}
1354
1355static int stfsm_s25fl_config(struct stfsm *fsm)
1356{
1357 struct flash_info *info = fsm->info;
1358 uint32_t flags = info->flags;
1359 uint32_t data_pads;
1360 uint32_t offs;
1361 uint16_t sta_wr;
1362 uint8_t sr1, cr1, dyb;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001363 int update_sr = 0;
Lee Jones5343a122014-03-20 09:21:04 +00001364 int ret;
1365
1366 if (flags & FLASH_FLAG_32BIT_ADDR) {
1367 /*
1368 * Prepare Read/Write/Erase sequences according to S25FLxxx
1369 * 32-bit address command set
1370 */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001371 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones5343a122014-03-20 09:21:04 +00001372 stfsm_s25fl_read4_configs);
1373 if (ret)
1374 return ret;
1375
Lee Jonese6b1bb42014-03-20 09:21:06 +00001376 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones5343a122014-03-20 09:21:04 +00001377 stfsm_s25fl_write4_configs);
1378 if (ret)
1379 return ret;
1380
1381 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1382
1383 } else {
1384 /* Use default configurations for 24-bit addressing */
1385 ret = stfsm_prepare_rwe_seqs_default(fsm);
1386 if (ret)
1387 return ret;
1388 }
1389
1390 /*
1391 * For devices that support 'DYB' sector locking, check lock status and
1392 * unlock sectors if necessary (some variants power-on with sectors
1393 * locked by default)
1394 */
1395 if (flags & FLASH_FLAG_DYB_LOCKING) {
1396 offs = 0;
1397 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1398 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1399 if (dyb == 0x00)
1400 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1401
1402 /* Handle bottom/top 4KiB parameter sectors */
1403 if ((offs < info->sector_size * 2) ||
1404 (offs >= (info->sector_size - info->n_sectors * 4)))
1405 offs += 0x1000;
1406 else
1407 offs += 0x10000;
1408 }
1409 }
1410
Angus Clark5d0bdda2014-03-26 16:39:18 +00001411 /* Check status of 'QE' bit, update if required. */
1412 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &cr1, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001413 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones5343a122014-03-20 09:21:04 +00001414 if (data_pads == 4) {
1415 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1416 /* Set 'QE' */
1417 cr1 |= STFSM_S25FL_CONFIG_QE;
1418
Angus Clark5d0bdda2014-03-26 16:39:18 +00001419 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001420 }
1421 } else {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001422 if (cr1 & STFSM_S25FL_CONFIG_QE) {
Lee Jones5343a122014-03-20 09:21:04 +00001423 /* Clear 'QE' */
1424 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1425
Angus Clark5d0bdda2014-03-26 16:39:18 +00001426 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001427 }
Angus Clark5d0bdda2014-03-26 16:39:18 +00001428 }
1429 if (update_sr) {
1430 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1431 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1432 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta_wr, 2, 1);
Lee Jones5343a122014-03-20 09:21:04 +00001433 }
1434
1435 /*
1436 * S25FLxxx devices support Program and Error error flags.
1437 * Configure driver to check flags and clear if necessary.
1438 */
1439 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1440
1441 return 0;
1442}
1443
Lee Jonescd7cac92014-03-20 09:21:05 +00001444static int stfsm_w25q_config(struct stfsm *fsm)
1445{
1446 uint32_t data_pads;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001447 uint8_t sr1, sr2;
1448 uint16_t sr_wr;
1449 int update_sr = 0;
Lee Jonescd7cac92014-03-20 09:21:05 +00001450 int ret;
1451
1452 ret = stfsm_prepare_rwe_seqs_default(fsm);
1453 if (ret)
1454 return ret;
1455
Angus Clark5d0bdda2014-03-26 16:39:18 +00001456 /* Check status of 'QE' bit, update if required. */
1457 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &sr2, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001458 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jonescd7cac92014-03-20 09:21:05 +00001459 if (data_pads == 4) {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001460 if (!(sr2 & W25Q_STATUS_QE)) {
1461 /* Set 'QE' */
1462 sr2 |= W25Q_STATUS_QE;
1463 update_sr = 1;
1464 }
1465 } else {
1466 if (sr2 & W25Q_STATUS_QE) {
1467 /* Clear 'QE' */
1468 sr2 &= ~W25Q_STATUS_QE;
1469 update_sr = 1;
1470 }
1471 }
1472 if (update_sr) {
1473 /* Write status register */
1474 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1475 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1476 stfsm_write_status(fsm, FLASH_CMD_WRSR, sr_wr, 2, 1);
Lee Jonescd7cac92014-03-20 09:21:05 +00001477 }
1478
1479 return 0;
1480}
1481
Lee Jonese514f102014-03-20 09:20:57 +00001482static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1483 uint32_t offset)
1484{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001485 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
Lee Jonese514f102014-03-20 09:20:57 +00001486 uint32_t data_pads;
1487 uint32_t read_mask;
1488 uint32_t size_ub;
1489 uint32_t size_lb;
1490 uint32_t size_mop;
1491 uint32_t tmp[4];
1492 uint32_t page_buf[FLASH_PAGESIZE_32];
1493 uint8_t *p;
1494
1495 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1496
1497 /* Enter 32-bit address mode, if required */
1498 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1499 stfsm_enter_32bit_addr(fsm, 1);
1500
1501 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1502 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1503 read_mask = (data_pads << 2) - 1;
1504
1505 /* Handle non-aligned buf */
1506 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1507
1508 /* Handle non-aligned size */
1509 size_ub = (size + read_mask) & ~read_mask;
1510 size_lb = size & ~read_mask;
1511 size_mop = size & read_mask;
1512
1513 seq->data_size = TRANSFER_SIZE(size_ub);
1514 seq->addr1 = (offset >> 16) & 0xffff;
1515 seq->addr2 = offset & 0xffff;
1516
1517 stfsm_load_seq(fsm, seq);
1518
1519 if (size_lb)
1520 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1521
1522 if (size_mop) {
1523 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1524 memcpy(p + size_lb, &tmp, size_mop);
1525 }
1526
1527 /* Handle non-aligned buf */
1528 if ((uint32_t)buf & 0x3)
1529 memcpy(buf, page_buf, size);
1530
1531 /* Wait for sequence to finish */
1532 stfsm_wait_seq(fsm);
1533
1534 stfsm_clear_fifo(fsm);
1535
1536 /* Exit 32-bit address mode, if required */
1537 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1538 stfsm_enter_32bit_addr(fsm, 0);
1539
1540 return 0;
1541}
1542
Lee Jones3f9d7202014-03-20 11:11:43 +00001543static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1544 uint32_t size, uint32_t offset)
Lee Jones176b4372014-03-20 09:20:59 +00001545{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001546 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
Lee Jones176b4372014-03-20 09:20:59 +00001547 uint32_t data_pads;
1548 uint32_t write_mask;
1549 uint32_t size_ub;
1550 uint32_t size_lb;
1551 uint32_t size_mop;
1552 uint32_t tmp[4];
1553 uint32_t page_buf[FLASH_PAGESIZE_32];
1554 uint8_t *t = (uint8_t *)&tmp;
1555 const uint8_t *p;
1556 int ret;
1557 int i;
1558
1559 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1560
1561 /* Enter 32-bit address mode, if required */
1562 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1563 stfsm_enter_32bit_addr(fsm, 1);
1564
1565 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1566 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1567 write_mask = (data_pads << 2) - 1;
1568
1569 /* Handle non-aligned buf */
1570 if ((uint32_t)buf & 0x3) {
1571 memcpy(page_buf, buf, size);
1572 p = (uint8_t *)page_buf;
1573 } else {
1574 p = buf;
1575 }
1576
1577 /* Handle non-aligned size */
1578 size_ub = (size + write_mask) & ~write_mask;
1579 size_lb = size & ~write_mask;
1580 size_mop = size & write_mask;
1581
1582 seq->data_size = TRANSFER_SIZE(size_ub);
1583 seq->addr1 = (offset >> 16) & 0xffff;
1584 seq->addr2 = offset & 0xffff;
1585
1586 /* Need to set FIFO to write mode, before writing data to FIFO (see
1587 * GNBvb79594)
1588 */
1589 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1590
1591 /*
1592 * Before writing data to the FIFO, apply a small delay to allow a
1593 * potential change of FIFO direction to complete.
1594 */
1595 if (fsm->fifo_dir_delay == 0)
1596 readl(fsm->base + SPI_FAST_SEQ_CFG);
1597 else
1598 udelay(fsm->fifo_dir_delay);
1599
1600
1601 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1602 if (size_lb) {
1603 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1604 p += size_lb;
1605 }
1606
1607 /* Handle non-aligned size */
1608 if (size_mop) {
1609 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1610 for (i = 0; i < size_mop; i++)
1611 t[i] = *p++;
1612
1613 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1614 }
1615
1616 /* Start sequence */
1617 stfsm_load_seq(fsm, seq);
1618
1619 /* Wait for sequence to finish */
1620 stfsm_wait_seq(fsm);
1621
1622 /* Wait for completion */
1623 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001624 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1625 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones176b4372014-03-20 09:20:59 +00001626
1627 /* Exit 32-bit address mode, if required */
Angus Clark009e7e62014-03-26 16:39:16 +00001628 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
Lee Jones176b4372014-03-20 09:20:59 +00001629 stfsm_enter_32bit_addr(fsm, 0);
Lee Jones176b4372014-03-20 09:20:59 +00001630
1631 return 0;
1632}
1633
Lee Jonese514f102014-03-20 09:20:57 +00001634/*
1635 * Read an address range from the flash chip. The address range
1636 * may be any size provided it is within the physical boundaries.
1637 */
1638static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1639 size_t *retlen, u_char *buf)
1640{
1641 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1642 uint32_t bytes;
1643
1644 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1645 __func__, (u32)from, len);
1646
1647 mutex_lock(&fsm->lock);
1648
1649 while (len > 0) {
1650 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1651
1652 stfsm_read(fsm, buf, bytes, from);
1653
1654 buf += bytes;
1655 from += bytes;
1656 len -= bytes;
1657
1658 *retlen += bytes;
1659 }
1660
1661 mutex_unlock(&fsm->lock);
1662
1663 return 0;
1664}
1665
Lee Jones3f9d7202014-03-20 11:11:43 +00001666static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
Lee Jones4a341fe2014-03-20 09:21:00 +00001667{
1668 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1669 int ret;
1670
1671 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1672
1673 /* Enter 32-bit address mode, if required */
1674 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1675 stfsm_enter_32bit_addr(fsm, 1);
1676
1677 seq->addr1 = (offset >> 16) & 0xffff;
1678 seq->addr2 = offset & 0xffff;
1679
1680 stfsm_load_seq(fsm, seq);
1681
1682 stfsm_wait_seq(fsm);
1683
1684 /* Wait for completion */
1685 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001686 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1687 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones4a341fe2014-03-20 09:21:00 +00001688
1689 /* Exit 32-bit address mode, if required */
1690 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1691 stfsm_enter_32bit_addr(fsm, 0);
1692
1693 return ret;
1694}
1695
1696static int stfsm_erase_chip(struct stfsm *fsm)
1697{
1698 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1699
1700 dev_dbg(fsm->dev, "erasing chip\n");
1701
1702 stfsm_load_seq(fsm, seq);
1703
1704 stfsm_wait_seq(fsm);
1705
1706 return stfsm_wait_busy(fsm);
1707}
1708
Lee Jones176b4372014-03-20 09:20:59 +00001709/*
1710 * Write an address range to the flash chip. Data must be written in
1711 * FLASH_PAGESIZE chunks. The address range may be any size provided
1712 * it is within the physical boundaries.
1713 */
1714static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1715 size_t *retlen, const u_char *buf)
1716{
1717 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1718
1719 u32 page_offs;
1720 u32 bytes;
1721 uint8_t *b = (uint8_t *)buf;
1722 int ret = 0;
1723
1724 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1725
Lee Jones176b4372014-03-20 09:20:59 +00001726 /* Offset within page */
1727 page_offs = to % FLASH_PAGESIZE;
1728
1729 mutex_lock(&fsm->lock);
1730
1731 while (len) {
1732 /* Write up to page boundary */
1733 bytes = min(FLASH_PAGESIZE - page_offs, len);
1734
1735 ret = stfsm_write(fsm, b, bytes, to);
1736 if (ret)
1737 goto out1;
1738
1739 b += bytes;
1740 len -= bytes;
1741 to += bytes;
1742
1743 /* We are now page-aligned */
1744 page_offs = 0;
1745
1746 *retlen += bytes;
1747
1748 }
1749
1750out1:
1751 mutex_unlock(&fsm->lock);
1752
1753 return ret;
1754}
1755
Lee Jones4a341fe2014-03-20 09:21:00 +00001756/*
1757 * Erase an address range on the flash chip. The address range may extend
1758 * one or more erase sectors. Return an error is there is a problem erasing.
1759 */
1760static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1761{
1762 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1763 u32 addr, len;
1764 int ret;
1765
1766 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1767 (long long)instr->addr, (long long)instr->len);
1768
1769 addr = instr->addr;
1770 len = instr->len;
1771
1772 mutex_lock(&fsm->lock);
1773
1774 /* Whole-chip erase? */
1775 if (len == mtd->size) {
1776 ret = stfsm_erase_chip(fsm);
1777 if (ret)
1778 goto out1;
1779 } else {
1780 while (len) {
1781 ret = stfsm_erase_sector(fsm, addr);
1782 if (ret)
1783 goto out1;
1784
1785 addr += mtd->erasesize;
1786 len -= mtd->erasesize;
1787 }
1788 }
1789
1790 mutex_unlock(&fsm->lock);
1791
1792 instr->state = MTD_ERASE_DONE;
1793 mtd_erase_callback(instr);
1794
1795 return 0;
1796
1797out1:
1798 instr->state = MTD_ERASE_FAILED;
1799 mutex_unlock(&fsm->lock);
1800
1801 return ret;
1802}
1803
Lee Jones3f9d7202014-03-20 11:11:43 +00001804static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
Lee Jones1bd512b2014-03-20 09:20:38 +00001805{
1806 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1807 uint32_t tmp[2];
1808
1809 stfsm_load_seq(fsm, seq);
1810
1811 stfsm_read_fifo(fsm, tmp, 8);
1812
1813 memcpy(jedec, tmp, 5);
1814
1815 stfsm_wait_seq(fsm);
1816}
1817
1818static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1819{
Lee Jones24fec652014-03-20 09:20:41 +00001820 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001821 u16 ext_jedec;
1822 u32 jedec;
1823 u8 id[5];
1824
1825 stfsm_read_jedec(fsm, id);
1826
1827 jedec = id[0] << 16 | id[1] << 8 | id[2];
1828 /*
1829 * JEDEC also defines an optional "extended device information"
1830 * string for after vendor-specific data, after the three bytes
1831 * we use here. Supporting some chips might require using it.
1832 */
1833 ext_jedec = id[3] << 8 | id[4];
1834
1835 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1836 jedec, id[0], id[1], id[2], id[3], id[4]);
1837
Lee Jones24fec652014-03-20 09:20:41 +00001838 for (info = flash_types; info->name; info++) {
1839 if (info->jedec_id == jedec) {
1840 if (info->ext_id && info->ext_id != ext_jedec)
1841 continue;
1842 return info;
1843 }
1844 }
1845 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1846
Lee Jones1bd512b2014-03-20 09:20:38 +00001847 return NULL;
1848}
1849
Lee Jones86f309fd2014-03-20 09:20:35 +00001850static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1851{
1852 int ret, timeout = 10;
1853
1854 /* Wait for controller to accept mode change */
1855 while (--timeout) {
1856 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1857 if (ret & 0x1)
1858 break;
1859 udelay(1);
1860 }
1861
1862 if (!timeout)
1863 return -EBUSY;
1864
1865 writel(mode, fsm->base + SPI_MODESELECT);
1866
1867 return 0;
1868}
1869
1870static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1871{
1872 uint32_t emi_freq;
1873 uint32_t clk_div;
1874
1875 /* TODO: Make this dynamic */
1876 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1877
1878 /*
1879 * Calculate clk_div - values between 2 and 128
1880 * Multiple of 2, rounded up
1881 */
1882 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1883 if (clk_div < 2)
1884 clk_div = 2;
1885 else if (clk_div > 128)
1886 clk_div = 128;
1887
1888 /*
1889 * Determine a suitable delay for the IP to complete a change of
1890 * direction of the FIFO. The required delay is related to the clock
1891 * divider used. The following heuristics are based on empirical tests,
1892 * using a 100MHz EMI clock.
1893 */
1894 if (clk_div <= 4)
1895 fsm->fifo_dir_delay = 0;
1896 else if (clk_div <= 10)
1897 fsm->fifo_dir_delay = 1;
1898 else
1899 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1900
1901 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1902 emi_freq, spi_freq, clk_div);
1903
1904 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1905}
1906
1907static int stfsm_init(struct stfsm *fsm)
1908{
1909 int ret;
1910
1911 /* Perform a soft reset of the FSM controller */
1912 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1913 udelay(1);
1914 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1915
1916 /* Set clock to 'safe' frequency initially */
1917 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1918
1919 /* Switch to FSM */
1920 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1921 if (ret)
1922 return ret;
1923
1924 /* Set timing parameters */
1925 writel(SPI_CFG_DEVICE_ST |
1926 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1927 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1928 SPI_CFG_DEFAULT_DATA_HOLD,
1929 fsm->base + SPI_CONFIGDATA);
1930 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1931
Angus Clark009e7e62014-03-26 16:39:16 +00001932 /*
1933 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1934 * our purposes, the WAIT instruction is used purely to achieve
1935 * "sequence validity" rather than actually implement a delay.
1936 */
1937 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1938
Lee Jones86f309fd2014-03-20 09:20:35 +00001939 /* Clear FIFO, just in case */
1940 stfsm_clear_fifo(fsm);
1941
1942 return 0;
1943}
1944
Lee Jonesa63984c2014-03-20 09:20:46 +00001945static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1946{
1947 struct stfsm *fsm = platform_get_drvdata(pdev);
1948 struct device_node *np = pdev->dev.of_node;
1949 struct regmap *regmap;
1950 uint32_t boot_device_reg;
1951 uint32_t boot_device_spi;
1952 uint32_t boot_device; /* Value we read from *boot_device_reg */
1953 int ret;
1954
1955 /* Booting from SPI NOR Flash is the default */
1956 fsm->booted_from_spi = true;
1957
1958 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1959 if (IS_ERR(regmap))
1960 goto boot_device_fail;
1961
Lee Jones0ea7d702014-03-20 09:20:50 +00001962 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1963
1964 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1965
Lee Jonesa63984c2014-03-20 09:20:46 +00001966 /* Where in the syscon the boot device information lives */
1967 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1968 if (ret)
1969 goto boot_device_fail;
1970
1971 /* Boot device value when booted from SPI NOR */
1972 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1973 if (ret)
1974 goto boot_device_fail;
1975
1976 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1977 if (ret)
1978 goto boot_device_fail;
1979
1980 if (boot_device != boot_device_spi)
1981 fsm->booted_from_spi = false;
1982
1983 return;
1984
1985boot_device_fail:
1986 dev_warn(&pdev->dev,
1987 "failed to fetch boot device, assuming boot from SPI\n");
1988}
1989
Lee Jonesd90db4a2014-03-20 09:20:33 +00001990static int stfsm_probe(struct platform_device *pdev)
1991{
1992 struct device_node *np = pdev->dev.of_node;
Lee Jones221cff12014-03-20 09:21:07 +00001993 struct mtd_part_parser_data ppdata;
Lee Jones24fec652014-03-20 09:20:41 +00001994 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001995 struct resource *res;
1996 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00001997 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001998
1999 if (!np) {
2000 dev_err(&pdev->dev, "No DT found\n");
2001 return -EINVAL;
2002 }
Lee Jones221cff12014-03-20 09:21:07 +00002003 ppdata.of_node = np;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002004
2005 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2006 if (!fsm)
2007 return -ENOMEM;
2008
2009 fsm->dev = &pdev->dev;
2010
2011 platform_set_drvdata(pdev, fsm);
2012
2013 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2014 if (!res) {
2015 dev_err(&pdev->dev, "Resource not found\n");
2016 return -ENODEV;
2017 }
2018
2019 fsm->base = devm_ioremap_resource(&pdev->dev, res);
2020 if (IS_ERR(fsm->base)) {
2021 dev_err(&pdev->dev,
2022 "Failed to reserve memory region %pR\n", res);
2023 return PTR_ERR(fsm->base);
2024 }
2025
2026 mutex_init(&fsm->lock);
2027
Lee Jones86f309fd2014-03-20 09:20:35 +00002028 ret = stfsm_init(fsm);
2029 if (ret) {
2030 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2031 return ret;
2032 }
2033
Lee Jonesa63984c2014-03-20 09:20:46 +00002034 stfsm_fetch_platform_configs(pdev);
2035
Lee Jones1bd512b2014-03-20 09:20:38 +00002036 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00002037 info = stfsm_jedec_probe(fsm);
2038 if (!info)
2039 return -ENODEV;
2040 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00002041
Lee Jones3b5d1982014-03-20 09:20:43 +00002042 /* Use device size to determine address width */
2043 if (info->sector_size * info->n_sectors > 0x1000000)
2044 info->flags |= FLASH_FLAG_32BIT_ADDR;
2045
Lee Jones218b8702014-03-20 09:20:55 +00002046 /*
2047 * Configure READ/WRITE/ERASE sequences according to platform and
2048 * device flags.
2049 */
2050 if (info->config) {
2051 ret = info->config(fsm);
2052 if (ret)
2053 return ret;
Lee Jones4eb3f0d82014-03-20 09:20:56 +00002054 } else {
2055 ret = stfsm_prepare_rwe_seqs_default(fsm);
2056 if (ret)
2057 return ret;
Lee Jones218b8702014-03-20 09:20:55 +00002058 }
2059
Lee Jones221cff12014-03-20 09:21:07 +00002060 fsm->mtd.name = info->name;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002061 fsm->mtd.dev.parent = &pdev->dev;
2062 fsm->mtd.type = MTD_NORFLASH;
2063 fsm->mtd.writesize = 4;
2064 fsm->mtd.writebufsize = fsm->mtd.writesize;
2065 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00002066 fsm->mtd.size = info->sector_size * info->n_sectors;
2067 fsm->mtd.erasesize = info->sector_size;
2068
Lee Jonese514f102014-03-20 09:20:57 +00002069 fsm->mtd._read = stfsm_mtd_read;
Lee Jones176b4372014-03-20 09:20:59 +00002070 fsm->mtd._write = stfsm_mtd_write;
Lee Jones4a341fe2014-03-20 09:21:00 +00002071 fsm->mtd._erase = stfsm_mtd_erase;
Lee Jonese514f102014-03-20 09:20:57 +00002072
Lee Jones4a341fe2014-03-20 09:21:00 +00002073 dev_info(&pdev->dev,
Lee Jones24fec652014-03-20 09:20:41 +00002074 "Found serial flash device: %s\n"
2075 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2076 info->name,
2077 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2078 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00002079
Lee Jones221cff12014-03-20 09:21:07 +00002080 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002081}
2082
2083static int stfsm_remove(struct platform_device *pdev)
2084{
2085 struct stfsm *fsm = platform_get_drvdata(pdev);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002086
Lee Jonesd9ba46d2014-03-20 11:11:47 +00002087 return mtd_device_unregister(&fsm->mtd);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002088}
2089
2090static struct of_device_id stfsm_match[] = {
2091 { .compatible = "st,spi-fsm", },
2092 {},
2093};
2094MODULE_DEVICE_TABLE(of, stfsm_match);
2095
2096static struct platform_driver stfsm_driver = {
2097 .probe = stfsm_probe,
2098 .remove = stfsm_remove,
2099 .driver = {
2100 .name = "st-spi-fsm",
2101 .owner = THIS_MODULE,
2102 .of_match_table = stfsm_match,
2103 },
2104};
2105module_platform_driver(stfsm_driver);
2106
2107MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2108MODULE_DESCRIPTION("ST SPI FSM driver");
2109MODULE_LICENSE("GPL");