blob: 30082a09f68b5e43a9fd7ec6096dd5944523ca9f [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
Angus Clark6b6d3732014-03-26 16:39:20 +0000375 /* Macronix MX25xxx
376 * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
377 * where operating frequency must be reduced.
378 */
Lee Jones11d7f822014-03-20 09:20:40 +0000379#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
380 FLASH_FLAG_READ_FAST | \
381 FLASH_FLAG_READ_1_1_2 | \
382 FLASH_FLAG_READ_1_2_2 | \
383 FLASH_FLAG_READ_1_1_4 | \
Lee Jones11d7f822014-03-20 09:20:40 +0000384 FLASH_FLAG_SE_4K | \
385 FLASH_FLAG_SE_32K)
Angus Clark6b6d3732014-03-26 16:39:20 +0000386 { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
387 (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
388 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000389 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
Lee Jones898180662014-03-20 09:21:03 +0000390 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
391 stfsm_mx25_config },
Angus Clark5fa98062014-03-26 16:39:15 +0000392 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
393 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
394 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000395
396#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
397 FLASH_FLAG_READ_FAST | \
398 FLASH_FLAG_READ_1_1_2 | \
399 FLASH_FLAG_READ_1_2_2 | \
400 FLASH_FLAG_READ_1_1_4 | \
401 FLASH_FLAG_READ_1_4_4 | \
402 FLASH_FLAG_WRITE_1_1_2 | \
403 FLASH_FLAG_WRITE_1_2_2 | \
404 FLASH_FLAG_WRITE_1_1_4 | \
405 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000406 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
407 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000408 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000409 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000410
411 /*
412 * Spansion S25FLxxxP
413 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
414 */
415#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
416 FLASH_FLAG_READ_1_1_2 | \
417 FLASH_FLAG_READ_1_2_2 | \
418 FLASH_FLAG_READ_1_1_4 | \
419 FLASH_FLAG_READ_1_4_4 | \
420 FLASH_FLAG_WRITE_1_1_4 | \
421 FLASH_FLAG_READ_FAST)
Angus Clark85bdcf62014-03-26 16:39:19 +0000422 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
423 stfsm_s25fl_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000424 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000425 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000426 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000427 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000428
429 /*
430 * Spansion S25FLxxxS
431 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
432 * - RESET# signal supported by die but not bristled out on all
433 * package types. The package type is a function of board design,
434 * so this information is captured in the board's flags.
435 * - Supports 'DYB' sector protection. Depending on variant, sectors
436 * may default to locked state on power-on.
437 */
438#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
439 FLASH_FLAG_RESET | \
440 FLASH_FLAG_DYB_LOCKING)
441 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000442 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000443 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000444 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000445 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
Lee Jones5343a122014-03-20 09:21:04 +0000446 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000447 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
Lee Jones5343a122014-03-20 09:21:04 +0000448 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000449
450 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
451#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
452 FLASH_FLAG_READ_FAST | \
453 FLASH_FLAG_READ_1_1_2 | \
454 FLASH_FLAG_WRITE_1_1_2)
455 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
456 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
457 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
458 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
459 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
460
461 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
462#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
463 FLASH_FLAG_READ_FAST | \
464 FLASH_FLAG_READ_1_1_2 | \
465 FLASH_FLAG_READ_1_2_2 | \
466 FLASH_FLAG_READ_1_1_4 | \
467 FLASH_FLAG_READ_1_4_4 | \
468 FLASH_FLAG_WRITE_1_1_4)
Lee Jonescd7cac92014-03-20 09:21:05 +0000469 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
470 stfsm_w25q_config },
471 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
472 stfsm_w25q_config },
473 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
474 stfsm_w25q_config },
475 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
476 stfsm_w25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000477
478 /* Sentinel */
479 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
480};
481
Lee Jonesa37b2f52014-03-20 09:20:53 +0000482/*
483 * FSM message sequence configurations:
484 *
485 * All configs are presented in order of preference
486 */
487
488/* Default READ configurations, in order of preference */
489static struct seq_rw_config default_read_configs[] = {
490 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
491 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
492 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
493 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
494 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
495 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
496 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
497};
498
499/* Default WRITE configurations */
500static struct seq_rw_config default_write_configs[] = {
501 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
502 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
503 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
504 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
505 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
506 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
507};
508
Lee Jonese85a6192014-03-20 09:20:54 +0000509/*
510 * [N25Qxxx] Configuration
511 */
512#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
513#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
514#define N25Q_VCR_WRAP_CONT 0x3
515
516/* N25Q 3-byte Address READ configurations
517 * - 'FAST' variants configured for 8 dummy cycles.
518 *
519 * Note, the number of dummy cycles used for 'FAST' READ operations is
520 * configurable and would normally be tuned according to the READ command and
521 * operating frequency. However, this applies universally to all 'FAST' READ
522 * commands, including those used by the SPIBoot controller, and remains in
523 * force until the device is power-cycled. Since the SPIBoot controller is
524 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
525 * cycles.
526 */
527static struct seq_rw_config n25q_read3_configs[] = {
528 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
529 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
530 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
531 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
532 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
533 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
534 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
535};
536
537/* N25Q 4-byte Address READ configurations
538 * - use special 4-byte address READ commands (reduces overheads, and
539 * reduces risk of hitting watchdog reset issues).
540 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
541 */
542static struct seq_rw_config n25q_read4_configs[] = {
543 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
544 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
545 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
546 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
547 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
548 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
549 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
550};
551
Lee Jones898180662014-03-20 09:21:03 +0000552/*
553 * [MX25xxx] Configuration
554 */
555#define MX25_STATUS_QE (0x1 << 6)
556
557static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
558{
559 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
560 SEQ_OPC_CYCLES(8) |
561 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR) |
562 SEQ_OPC_CSDEASSERT);
563
564 seq->seq[0] = STFSM_INST_CMD1;
565 seq->seq[1] = STFSM_INST_WAIT;
566 seq->seq[2] = STFSM_INST_STOP;
567
568 seq->seq_cfg = (SEQ_CFG_PADS_1 |
569 SEQ_CFG_ERASE |
570 SEQ_CFG_READNOTWRITE |
571 SEQ_CFG_CSDEASSERT |
572 SEQ_CFG_STARTSEQ);
573
574 return 0;
575}
576
Lee Jones5343a122014-03-20 09:21:04 +0000577/*
578 * [S25FLxxx] Configuration
579 */
580#define STFSM_S25FL_CONFIG_QE (0x1 << 1)
581
582/*
583 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
584 * Register, Extended Address Modes, and a 32-bit address command set. The
585 * 32-bit address command set is used here, since it avoids any problems with
586 * entering a state that is incompatible with the SPIBoot Controller.
587 */
588static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
589 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
590 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
591 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
592 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
593 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
594 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
595 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
596};
597
598static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
599 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
600 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
601 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
602};
603
Lee Jonescd7cac92014-03-20 09:21:05 +0000604/*
605 * [W25Qxxx] Configuration
606 */
Angus Clark5d0bdda2014-03-26 16:39:18 +0000607#define W25Q_STATUS_QE (0x1 << 1)
Lee Jonescd7cac92014-03-20 09:21:05 +0000608
Lee Jones1bd512b2014-03-20 09:20:38 +0000609static struct stfsm_seq stfsm_seq_read_jedec = {
610 .data_size = TRANSFER_SIZE(8),
611 .seq_opc[0] = (SEQ_OPC_PADS_1 |
612 SEQ_OPC_CYCLES(8) |
613 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
614 .seq = {
615 STFSM_INST_CMD1,
616 STFSM_INST_DATA_READ,
617 STFSM_INST_STOP,
618 },
619 .seq_cfg = (SEQ_CFG_PADS_1 |
620 SEQ_CFG_READNOTWRITE |
621 SEQ_CFG_CSDEASSERT |
622 SEQ_CFG_STARTSEQ),
623};
624
Lee Jones176b4372014-03-20 09:20:59 +0000625static struct stfsm_seq stfsm_seq_read_status_fifo = {
626 .data_size = TRANSFER_SIZE(4),
627 .seq_opc[0] = (SEQ_OPC_PADS_1 |
628 SEQ_OPC_CYCLES(8) |
629 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
630 .seq = {
631 STFSM_INST_CMD1,
632 STFSM_INST_DATA_READ,
633 STFSM_INST_STOP,
634 },
635 .seq_cfg = (SEQ_CFG_PADS_1 |
636 SEQ_CFG_READNOTWRITE |
637 SEQ_CFG_CSDEASSERT |
638 SEQ_CFG_STARTSEQ),
639};
640
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000641static struct stfsm_seq stfsm_seq_erase_sector = {
642 /* 'addr_cfg' configured during initialisation */
643 .seq_opc = {
644 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
645 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
646
647 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
648 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
649 },
650 .seq = {
651 STFSM_INST_CMD1,
652 STFSM_INST_CMD2,
653 STFSM_INST_ADD1,
654 STFSM_INST_ADD2,
655 STFSM_INST_STOP,
656 },
657 .seq_cfg = (SEQ_CFG_PADS_1 |
658 SEQ_CFG_READNOTWRITE |
659 SEQ_CFG_CSDEASSERT |
660 SEQ_CFG_STARTSEQ),
661};
662
Lee Jones4a341fe2014-03-20 09:21:00 +0000663static struct stfsm_seq stfsm_seq_erase_chip = {
664 .seq_opc = {
665 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
666 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
667
668 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
669 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
670 },
671 .seq = {
672 STFSM_INST_CMD1,
673 STFSM_INST_CMD2,
674 STFSM_INST_WAIT,
675 STFSM_INST_STOP,
676 },
677 .seq_cfg = (SEQ_CFG_PADS_1 |
678 SEQ_CFG_ERASE |
679 SEQ_CFG_READNOTWRITE |
680 SEQ_CFG_CSDEASSERT |
681 SEQ_CFG_STARTSEQ),
682};
683
Lee Jones150571b2014-03-20 09:21:02 +0000684static struct stfsm_seq stfsm_seq_write_status = {
685 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
686 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
687 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
688 SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
689 .seq = {
690 STFSM_INST_CMD1,
691 STFSM_INST_CMD2,
692 STFSM_INST_STA_WR1,
693 STFSM_INST_STOP,
694 },
695 .seq_cfg = (SEQ_CFG_PADS_1 |
696 SEQ_CFG_READNOTWRITE |
697 SEQ_CFG_CSDEASSERT |
698 SEQ_CFG_STARTSEQ),
699};
700
Lee Jones6bd29602014-03-20 09:20:48 +0000701static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
702{
703 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
704 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
705 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
706 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
707 SEQ_OPC_CSDEASSERT);
708
709 seq->seq[0] = STFSM_INST_CMD2;
710 seq->seq[1] = STFSM_INST_CMD1;
711 seq->seq[2] = STFSM_INST_WAIT;
712 seq->seq[3] = STFSM_INST_STOP;
713
714 seq->seq_cfg = (SEQ_CFG_PADS_1 |
715 SEQ_CFG_ERASE |
716 SEQ_CFG_READNOTWRITE |
717 SEQ_CFG_CSDEASSERT |
718 SEQ_CFG_STARTSEQ);
719
720 return 0;
721}
722
Lee Jones3c8b85b2014-03-20 09:20:36 +0000723static inline int stfsm_is_idle(struct stfsm *fsm)
724{
725 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
726}
727
Lee Jones86f309fd2014-03-20 09:20:35 +0000728static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
729{
730 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
731}
732
733static void stfsm_clear_fifo(struct stfsm *fsm)
734{
735 uint32_t avail;
736
737 for (;;) {
738 avail = stfsm_fifo_available(fsm);
739 if (!avail)
740 break;
741
742 while (avail) {
743 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
744 avail--;
745 }
746 }
747}
748
Lee Jones3c8b85b2014-03-20 09:20:36 +0000749static inline void stfsm_load_seq(struct stfsm *fsm,
750 const struct stfsm_seq *seq)
751{
752 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
753 const uint32_t *src = (const uint32_t *)seq;
754 int words = sizeof(*seq) / sizeof(*src);
755
756 BUG_ON(!stfsm_is_idle(fsm));
757
758 while (words--) {
759 writel(*src, dst);
760 src++;
761 dst += 4;
762 }
763}
764
765static void stfsm_wait_seq(struct stfsm *fsm)
766{
767 unsigned long deadline;
768 int timeout = 0;
769
770 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
771
772 while (!timeout) {
773 if (time_after_eq(jiffies, deadline))
774 timeout = 1;
775
776 if (stfsm_is_idle(fsm))
777 return;
778
779 cond_resched();
780 }
781
782 dev_err(fsm->dev, "timeout on sequence completion\n");
783}
784
Lee Jones3f9d7202014-03-20 11:11:43 +0000785static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
Lee Jones030e82d2014-03-20 09:20:37 +0000786{
787 uint32_t remaining = size >> 2;
788 uint32_t avail;
789 uint32_t words;
790
791 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
792
793 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
794
795 while (remaining) {
796 for (;;) {
797 avail = stfsm_fifo_available(fsm);
798 if (avail)
799 break;
800 udelay(1);
801 }
802 words = min(avail, remaining);
803 remaining -= words;
804
805 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
806 buf += words;
807 }
808}
809
Lee Jones3f9d7202014-03-20 11:11:43 +0000810static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
811 uint32_t size)
Lee Jones30ca64f2014-03-20 09:20:58 +0000812{
813 uint32_t words = size >> 2;
814
815 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
816
817 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
818
819 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
820
821 return size;
822}
823
Lee Jones0de08e42014-03-20 09:20:51 +0000824static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
825{
Lee Jonese6b1bb42014-03-20 09:21:06 +0000826 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
Lee Jones0de08e42014-03-20 09:20:51 +0000827 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
828
829 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
830 SEQ_OPC_CYCLES(8) |
831 SEQ_OPC_OPCODE(cmd) |
832 SEQ_OPC_CSDEASSERT);
833
834 stfsm_load_seq(fsm, seq);
835
836 stfsm_wait_seq(fsm);
837
838 return 0;
839}
840
Lee Jones176b4372014-03-20 09:20:59 +0000841static uint8_t stfsm_wait_busy(struct stfsm *fsm)
842{
843 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
844 unsigned long deadline;
845 uint32_t status;
846 int timeout = 0;
847
848 /* Use RDRS1 */
849 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
850 SEQ_OPC_CYCLES(8) |
851 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
852
853 /* Load read_status sequence */
854 stfsm_load_seq(fsm, seq);
855
856 /*
857 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
858 */
859 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
860 while (!timeout) {
Lee Jones176b4372014-03-20 09:20:59 +0000861 if (time_after_eq(jiffies, deadline))
862 timeout = 1;
863
864 stfsm_wait_seq(fsm);
865
866 stfsm_read_fifo(fsm, &status, 4);
867
868 if ((status & FLASH_STATUS_BUSY) == 0)
869 return 0;
870
871 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
872 ((status & S25FL_STATUS_P_ERR) ||
873 (status & S25FL_STATUS_E_ERR)))
874 return (uint8_t)(status & 0xff);
875
876 if (!timeout)
877 /* Restart */
878 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
Lee Jonesea7864b2014-03-20 11:11:46 +0000879
880 cond_resched();
Lee Jones176b4372014-03-20 09:20:59 +0000881 }
882
883 dev_err(fsm->dev, "timeout on wait_busy\n");
884
885 return FLASH_STATUS_TIMEOUT;
886}
887
Lee Jonesac94dbc2014-03-20 09:21:01 +0000888static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
Angus Clark5d0bdda2014-03-26 16:39:18 +0000889 uint8_t *data, int bytes)
Lee Jonesac94dbc2014-03-20 09:21:01 +0000890{
891 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
892 uint32_t tmp;
Angus Clark5d0bdda2014-03-26 16:39:18 +0000893 uint8_t *t = (uint8_t *)&tmp;
894 int i;
Lee Jonesac94dbc2014-03-20 09:21:01 +0000895
Angus Clark5d0bdda2014-03-26 16:39:18 +0000896 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
897 cmd, bytes);
Lee Jonesac94dbc2014-03-20 09:21:01 +0000898
Angus Clark5d0bdda2014-03-26 16:39:18 +0000899 BUG_ON(bytes != 1 && bytes != 2);
900
901 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Lee Jonesac94dbc2014-03-20 09:21:01 +0000902 SEQ_OPC_OPCODE(cmd)),
903
904 stfsm_load_seq(fsm, seq);
905
906 stfsm_read_fifo(fsm, &tmp, 4);
907
Angus Clark5d0bdda2014-03-26 16:39:18 +0000908 for (i = 0; i < bytes; i++)
909 data[i] = t[i];
Lee Jonesac94dbc2014-03-20 09:21:01 +0000910
911 stfsm_wait_seq(fsm);
912
913 return 0;
914}
915
Angus Clark5d0bdda2014-03-26 16:39:18 +0000916static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
917 uint16_t data, int bytes, int wait_busy)
Lee Jones150571b2014-03-20 09:21:02 +0000918{
919 struct stfsm_seq *seq = &stfsm_seq_write_status;
920
Angus Clark5d0bdda2014-03-26 16:39:18 +0000921 dev_dbg(fsm->dev,
922 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
923 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
Lee Jones150571b2014-03-20 09:21:02 +0000924
Angus Clark5d0bdda2014-03-26 16:39:18 +0000925 BUG_ON(bytes != 1 && bytes != 2);
926
927 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
928 SEQ_OPC_OPCODE(cmd));
929
930 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
931 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
Lee Jones150571b2014-03-20 09:21:02 +0000932
933 stfsm_load_seq(fsm, seq);
934
935 stfsm_wait_seq(fsm);
936
Angus Clark5d0bdda2014-03-26 16:39:18 +0000937 if (wait_busy)
938 stfsm_wait_busy(fsm);
Lee Jones249516c2014-03-20 09:20:52 +0000939
940 return 0;
941}
942
Lee Jones0ea7d702014-03-20 09:20:50 +0000943/*
944 * SoC reset on 'boot-from-spi' systems
945 *
946 * Certain modes of operation cause the Flash device to enter a particular state
947 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
948 * Addr' commands). On boot-from-spi systems, it is important to consider what
949 * happens if a warm reset occurs during this period. The SPIBoot controller
950 * assumes that Flash device is in its default reset state, 24-bit address mode,
951 * and ready to accept commands. This can be achieved using some form of
952 * on-board logic/controller to force a device POR in response to a SoC-level
953 * reset or by making use of the device reset signal if available (limited
954 * number of devices only).
955 *
956 * Failure to take such precautions can cause problems following a warm reset.
957 * For some operations (e.g. ERASE), there is little that can be done. For
958 * other modes of operation (e.g. 32-bit addressing), options are often
959 * available that can help minimise the window in which a reset could cause a
960 * problem.
961 *
962 */
963static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
964{
965 /* Reset signal is available on the board and supported by the device */
966 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
967 return true;
968
969 /* Board-level logic forces a power-on-reset */
970 if (fsm->reset_por)
971 return true;
972
973 /* Reset is not properly handled and may result in failure to reboot */
974 return false;
975}
976
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000977/* Configure 'addr_cfg' according to addressing mode */
978static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
979 struct stfsm_seq *seq)
980{
981 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
982
983 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
984 ADR_CFG_PADS_1_ADD1 |
985 ADR_CFG_CYCLES_ADD2(16) |
986 ADR_CFG_PADS_1_ADD2 |
987 ADR_CFG_CSDEASSERT_ADD2);
988}
989
Lee Jones08981272014-03-20 09:20:42 +0000990/* Search for preferred configuration based on available flags */
991static struct seq_rw_config *
992stfsm_search_seq_rw_configs(struct stfsm *fsm,
993 struct seq_rw_config cfgs[])
994{
995 struct seq_rw_config *config;
996 int flags = fsm->info->flags;
997
998 for (config = cfgs; config->cmd != 0; config++)
999 if ((config->flags & flags) == config->flags)
1000 return config;
1001
1002 return NULL;
1003}
1004
Lee Jones97ccf2d2014-03-20 09:20:44 +00001005/* Prepare a READ/WRITE sequence according to configuration parameters */
1006static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1007 struct stfsm_seq *seq,
1008 struct seq_rw_config *cfg)
1009{
1010 int addr1_cycles, addr2_cycles;
1011 int i = 0;
1012
1013 memset(seq, 0, sizeof(*seq));
1014
1015 /* Add READ/WRITE OPC */
1016 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1017 SEQ_OPC_CYCLES(8) |
1018 SEQ_OPC_OPCODE(cfg->cmd));
1019
1020 /* Add WREN OPC for a WRITE sequence */
1021 if (cfg->write)
1022 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1023 SEQ_OPC_CYCLES(8) |
1024 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1025 SEQ_OPC_CSDEASSERT);
1026
1027 /* Address configuration (24 or 32-bit addresses) */
1028 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1029 addr1_cycles /= cfg->addr_pads;
1030 addr2_cycles = 16 / cfg->addr_pads;
1031 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
1032 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
1033 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
1034 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1035
1036 /* Data/Sequence configuration */
1037 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1038 SEQ_CFG_STARTSEQ |
1039 SEQ_CFG_CSDEASSERT);
1040 if (!cfg->write)
1041 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1042
1043 /* Mode configuration (no. of pads taken from addr cfg) */
1044 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1045 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1046 (cfg->addr_pads - 1) << 22); /* pads */
1047
1048 /* Dummy configuration (no. of pads taken from addr cfg) */
1049 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1050 (cfg->addr_pads - 1) << 22); /* pads */
1051
1052
1053 /* Instruction sequence */
1054 i = 0;
1055 if (cfg->write)
1056 seq->seq[i++] = STFSM_INST_CMD2;
1057
1058 seq->seq[i++] = STFSM_INST_CMD1;
1059
1060 seq->seq[i++] = STFSM_INST_ADD1;
1061 seq->seq[i++] = STFSM_INST_ADD2;
1062
1063 if (cfg->mode_cycles)
1064 seq->seq[i++] = STFSM_INST_MODE;
1065
1066 if (cfg->dummy_cycles)
1067 seq->seq[i++] = STFSM_INST_DUMMY;
1068
1069 seq->seq[i++] =
1070 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1071 seq->seq[i++] = STFSM_INST_STOP;
1072}
1073
Lee Jones88cccb82014-03-20 09:20:49 +00001074static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1075 struct stfsm_seq *seq,
1076 struct seq_rw_config *cfgs)
1077{
1078 struct seq_rw_config *config;
1079
1080 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1081 if (!config) {
1082 dev_err(fsm->dev, "failed to find suitable config\n");
1083 return -EINVAL;
1084 }
1085
1086 stfsm_prepare_rw_seq(fsm, seq, config);
1087
1088 return 0;
1089}
1090
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001091/* Prepare a READ/WRITE/ERASE 'default' sequences */
1092static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1093{
1094 uint32_t flags = fsm->info->flags;
1095 int ret;
1096
1097 /* Configure 'READ' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001098 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001099 default_read_configs);
1100 if (ret) {
1101 dev_err(fsm->dev,
1102 "failed to prep READ sequence with flags [0x%08x]\n",
1103 flags);
1104 return ret;
1105 }
1106
1107 /* Configure 'WRITE' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001108 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001109 default_write_configs);
1110 if (ret) {
1111 dev_err(fsm->dev,
1112 "failed to prep WRITE sequence with flags [0x%08x]\n",
1113 flags);
1114 return ret;
1115 }
1116
1117 /* Configure 'ERASE_SECTOR' sequence */
1118 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1119
1120 return 0;
1121}
1122
Lee Jones898180662014-03-20 09:21:03 +00001123static int stfsm_mx25_config(struct stfsm *fsm)
1124{
1125 uint32_t flags = fsm->info->flags;
1126 uint32_t data_pads;
1127 uint8_t sta;
1128 int ret;
1129 bool soc_reset;
1130
1131 /*
1132 * Use default READ/WRITE sequences
1133 */
1134 ret = stfsm_prepare_rwe_seqs_default(fsm);
1135 if (ret)
1136 return ret;
1137
1138 /*
1139 * Configure 32-bit Address Support
1140 */
1141 if (flags & FLASH_FLAG_32BIT_ADDR) {
1142 /* Configure 'enter_32bitaddr' FSM sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001143 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones898180662014-03-20 09:21:03 +00001144
1145 soc_reset = stfsm_can_handle_soc_reset(fsm);
Angus Clark009e7e62014-03-26 16:39:16 +00001146 if (soc_reset || !fsm->booted_from_spi)
Lee Jones898180662014-03-20 09:21:03 +00001147 /* If we can handle SoC resets, we enable 32-bit address
1148 * mode pervasively */
1149 stfsm_enter_32bit_addr(fsm, 1);
1150
Angus Clark009e7e62014-03-26 16:39:16 +00001151 else
Lee Jones898180662014-03-20 09:21:03 +00001152 /* Else, enable/disable 32-bit addressing before/after
1153 * each operation */
1154 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1155 CFG_WRITE_TOGGLE_32BIT_ADDR |
1156 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
Lee Jones898180662014-03-20 09:21:03 +00001157 }
1158
Angus Clark5d0bdda2014-03-26 16:39:18 +00001159 /* Check status of 'QE' bit, update if required. */
1160 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001161 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones898180662014-03-20 09:21:03 +00001162 if (data_pads == 4) {
Angus Clarkcc666862014-03-26 16:39:17 +00001163 if (!(sta & MX25_STATUS_QE)) {
1164 /* Set 'QE' */
1165 sta |= MX25_STATUS_QE;
1166
Angus Clark5d0bdda2014-03-26 16:39:18 +00001167 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001168 }
1169 } else {
1170 if (sta & MX25_STATUS_QE) {
1171 /* Clear 'QE' */
1172 sta &= ~MX25_STATUS_QE;
1173
Angus Clark5d0bdda2014-03-26 16:39:18 +00001174 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001175 }
Lee Jones898180662014-03-20 09:21:03 +00001176 }
1177
1178 return 0;
1179}
1180
Lee Jones218b8702014-03-20 09:20:55 +00001181static int stfsm_n25q_config(struct stfsm *fsm)
1182{
1183 uint32_t flags = fsm->info->flags;
1184 uint8_t vcr;
1185 int ret = 0;
1186 bool soc_reset;
1187
1188 /* Configure 'READ' sequence */
1189 if (flags & FLASH_FLAG_32BIT_ADDR)
Lee Jonese6b1bb42014-03-20 09:21:06 +00001190 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001191 n25q_read4_configs);
1192 else
Lee Jonese6b1bb42014-03-20 09:21:06 +00001193 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001194 n25q_read3_configs);
1195 if (ret) {
1196 dev_err(fsm->dev,
1197 "failed to prepare READ sequence with flags [0x%08x]\n",
1198 flags);
1199 return ret;
1200 }
1201
1202 /* Configure 'WRITE' sequence (default configs) */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001203 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones218b8702014-03-20 09:20:55 +00001204 default_write_configs);
1205 if (ret) {
1206 dev_err(fsm->dev,
1207 "preparing WRITE sequence using flags [0x%08x] failed\n",
1208 flags);
1209 return ret;
1210 }
1211
1212 /* * Configure 'ERASE_SECTOR' sequence */
1213 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1214
1215 /* Configure 32-bit address support */
1216 if (flags & FLASH_FLAG_32BIT_ADDR) {
Lee Jonese6b1bb42014-03-20 09:21:06 +00001217 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones218b8702014-03-20 09:20:55 +00001218
1219 soc_reset = stfsm_can_handle_soc_reset(fsm);
1220 if (soc_reset || !fsm->booted_from_spi) {
1221 /*
1222 * If we can handle SoC resets, we enable 32-bit
1223 * address mode pervasively
1224 */
1225 stfsm_enter_32bit_addr(fsm, 1);
1226 } else {
1227 /*
1228 * If not, enable/disable for WRITE and ERASE
1229 * operations (READ uses special commands)
1230 */
1231 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1232 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1233 }
1234 }
1235
1236 /*
1237 * Configure device to use 8 dummy cycles
1238 */
1239 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1240 N25Q_VCR_WRAP_CONT);
Angus Clark5d0bdda2014-03-26 16:39:18 +00001241 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
Lee Jones218b8702014-03-20 09:20:55 +00001242
1243 return 0;
1244}
1245
Lee Jones5343a122014-03-20 09:21:04 +00001246static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1247{
1248 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1249 SEQ_OPC_CYCLES(8) |
1250 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1251
1252 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1253 ADR_CFG_PADS_1_ADD1 |
1254 ADR_CFG_CYCLES_ADD2(16) |
1255 ADR_CFG_PADS_1_ADD2 |
1256 ADR_CFG_CSDEASSERT_ADD2);
1257}
1258
1259static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1260{
1261 uint32_t tmp;
1262 struct stfsm_seq seq = {
1263 .data_size = TRANSFER_SIZE(4),
1264 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1265 SEQ_OPC_CYCLES(8) |
1266 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1267 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1268 ADR_CFG_PADS_1_ADD1 |
1269 ADR_CFG_CYCLES_ADD2(16) |
1270 ADR_CFG_PADS_1_ADD2),
1271 .addr1 = (offs >> 16) & 0xffff,
1272 .addr2 = offs & 0xffff,
1273 .seq = {
1274 STFSM_INST_CMD1,
1275 STFSM_INST_ADD1,
1276 STFSM_INST_ADD2,
1277 STFSM_INST_DATA_READ,
1278 STFSM_INST_STOP,
1279 },
1280 .seq_cfg = (SEQ_CFG_PADS_1 |
1281 SEQ_CFG_READNOTWRITE |
1282 SEQ_CFG_CSDEASSERT |
1283 SEQ_CFG_STARTSEQ),
1284 };
1285
1286 stfsm_load_seq(fsm, &seq);
1287
1288 stfsm_read_fifo(fsm, &tmp, 4);
1289
1290 *dby = (uint8_t)(tmp >> 24);
1291
1292 stfsm_wait_seq(fsm);
1293}
1294
1295static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1296{
1297 struct stfsm_seq seq = {
1298 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1299 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1300 SEQ_OPC_CSDEASSERT),
1301 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1302 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1303 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1304 ADR_CFG_PADS_1_ADD1 |
1305 ADR_CFG_CYCLES_ADD2(16) |
1306 ADR_CFG_PADS_1_ADD2),
1307 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1308 .addr1 = (offs >> 16) & 0xffff,
1309 .addr2 = offs & 0xffff,
1310 .seq = {
1311 STFSM_INST_CMD1,
1312 STFSM_INST_CMD2,
1313 STFSM_INST_ADD1,
1314 STFSM_INST_ADD2,
1315 STFSM_INST_STA_WR1,
1316 STFSM_INST_STOP,
1317 },
1318 .seq_cfg = (SEQ_CFG_PADS_1 |
1319 SEQ_CFG_READNOTWRITE |
1320 SEQ_CFG_CSDEASSERT |
1321 SEQ_CFG_STARTSEQ),
1322 };
1323
1324 stfsm_load_seq(fsm, &seq);
1325 stfsm_wait_seq(fsm);
1326
1327 stfsm_wait_busy(fsm);
1328}
1329
1330static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1331{
1332 struct stfsm_seq seq = {
1333 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1334 SEQ_OPC_CYCLES(8) |
1335 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1336 SEQ_OPC_CSDEASSERT),
1337 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1338 SEQ_OPC_CYCLES(8) |
1339 SEQ_OPC_OPCODE(FLASH_CMD_WRDI) |
1340 SEQ_OPC_CSDEASSERT),
1341 .seq = {
1342 STFSM_INST_CMD1,
1343 STFSM_INST_CMD2,
1344 STFSM_INST_WAIT,
1345 STFSM_INST_STOP,
1346 },
1347 .seq_cfg = (SEQ_CFG_PADS_1 |
1348 SEQ_CFG_ERASE |
1349 SEQ_CFG_READNOTWRITE |
1350 SEQ_CFG_CSDEASSERT |
1351 SEQ_CFG_STARTSEQ),
1352 };
1353
1354 stfsm_load_seq(fsm, &seq);
1355
1356 stfsm_wait_seq(fsm);
1357
1358 return 0;
1359}
1360
1361static int stfsm_s25fl_config(struct stfsm *fsm)
1362{
1363 struct flash_info *info = fsm->info;
1364 uint32_t flags = info->flags;
1365 uint32_t data_pads;
1366 uint32_t offs;
1367 uint16_t sta_wr;
1368 uint8_t sr1, cr1, dyb;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001369 int update_sr = 0;
Lee Jones5343a122014-03-20 09:21:04 +00001370 int ret;
1371
1372 if (flags & FLASH_FLAG_32BIT_ADDR) {
1373 /*
1374 * Prepare Read/Write/Erase sequences according to S25FLxxx
1375 * 32-bit address command set
1376 */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001377 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones5343a122014-03-20 09:21:04 +00001378 stfsm_s25fl_read4_configs);
1379 if (ret)
1380 return ret;
1381
Lee Jonese6b1bb42014-03-20 09:21:06 +00001382 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones5343a122014-03-20 09:21:04 +00001383 stfsm_s25fl_write4_configs);
1384 if (ret)
1385 return ret;
1386
1387 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1388
1389 } else {
1390 /* Use default configurations for 24-bit addressing */
1391 ret = stfsm_prepare_rwe_seqs_default(fsm);
1392 if (ret)
1393 return ret;
1394 }
1395
1396 /*
1397 * For devices that support 'DYB' sector locking, check lock status and
1398 * unlock sectors if necessary (some variants power-on with sectors
1399 * locked by default)
1400 */
1401 if (flags & FLASH_FLAG_DYB_LOCKING) {
1402 offs = 0;
1403 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1404 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1405 if (dyb == 0x00)
1406 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1407
1408 /* Handle bottom/top 4KiB parameter sectors */
1409 if ((offs < info->sector_size * 2) ||
1410 (offs >= (info->sector_size - info->n_sectors * 4)))
1411 offs += 0x1000;
1412 else
1413 offs += 0x10000;
1414 }
1415 }
1416
Angus Clark5d0bdda2014-03-26 16:39:18 +00001417 /* Check status of 'QE' bit, update if required. */
1418 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &cr1, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001419 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones5343a122014-03-20 09:21:04 +00001420 if (data_pads == 4) {
1421 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1422 /* Set 'QE' */
1423 cr1 |= STFSM_S25FL_CONFIG_QE;
1424
Angus Clark5d0bdda2014-03-26 16:39:18 +00001425 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001426 }
1427 } else {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001428 if (cr1 & STFSM_S25FL_CONFIG_QE) {
Lee Jones5343a122014-03-20 09:21:04 +00001429 /* Clear 'QE' */
1430 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1431
Angus Clark5d0bdda2014-03-26 16:39:18 +00001432 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001433 }
Angus Clark5d0bdda2014-03-26 16:39:18 +00001434 }
1435 if (update_sr) {
1436 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1437 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1438 stfsm_write_status(fsm, FLASH_CMD_WRSR, sta_wr, 2, 1);
Lee Jones5343a122014-03-20 09:21:04 +00001439 }
1440
1441 /*
1442 * S25FLxxx devices support Program and Error error flags.
1443 * Configure driver to check flags and clear if necessary.
1444 */
1445 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1446
1447 return 0;
1448}
1449
Lee Jonescd7cac92014-03-20 09:21:05 +00001450static int stfsm_w25q_config(struct stfsm *fsm)
1451{
1452 uint32_t data_pads;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001453 uint8_t sr1, sr2;
1454 uint16_t sr_wr;
1455 int update_sr = 0;
Lee Jonescd7cac92014-03-20 09:21:05 +00001456 int ret;
1457
1458 ret = stfsm_prepare_rwe_seqs_default(fsm);
1459 if (ret)
1460 return ret;
1461
Angus Clark5d0bdda2014-03-26 16:39:18 +00001462 /* Check status of 'QE' bit, update if required. */
1463 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &sr2, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001464 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jonescd7cac92014-03-20 09:21:05 +00001465 if (data_pads == 4) {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001466 if (!(sr2 & W25Q_STATUS_QE)) {
1467 /* Set 'QE' */
1468 sr2 |= W25Q_STATUS_QE;
1469 update_sr = 1;
1470 }
1471 } else {
1472 if (sr2 & W25Q_STATUS_QE) {
1473 /* Clear 'QE' */
1474 sr2 &= ~W25Q_STATUS_QE;
1475 update_sr = 1;
1476 }
1477 }
1478 if (update_sr) {
1479 /* Write status register */
1480 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1, 1);
1481 sr_wr = ((uint16_t)sr2 << 8) | sr1;
1482 stfsm_write_status(fsm, FLASH_CMD_WRSR, sr_wr, 2, 1);
Lee Jonescd7cac92014-03-20 09:21:05 +00001483 }
1484
1485 return 0;
1486}
1487
Lee Jonese514f102014-03-20 09:20:57 +00001488static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1489 uint32_t offset)
1490{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001491 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
Lee Jonese514f102014-03-20 09:20:57 +00001492 uint32_t data_pads;
1493 uint32_t read_mask;
1494 uint32_t size_ub;
1495 uint32_t size_lb;
1496 uint32_t size_mop;
1497 uint32_t tmp[4];
1498 uint32_t page_buf[FLASH_PAGESIZE_32];
1499 uint8_t *p;
1500
1501 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1502
1503 /* Enter 32-bit address mode, if required */
1504 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1505 stfsm_enter_32bit_addr(fsm, 1);
1506
1507 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1508 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1509 read_mask = (data_pads << 2) - 1;
1510
1511 /* Handle non-aligned buf */
1512 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1513
1514 /* Handle non-aligned size */
1515 size_ub = (size + read_mask) & ~read_mask;
1516 size_lb = size & ~read_mask;
1517 size_mop = size & read_mask;
1518
1519 seq->data_size = TRANSFER_SIZE(size_ub);
1520 seq->addr1 = (offset >> 16) & 0xffff;
1521 seq->addr2 = offset & 0xffff;
1522
1523 stfsm_load_seq(fsm, seq);
1524
1525 if (size_lb)
1526 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1527
1528 if (size_mop) {
1529 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1530 memcpy(p + size_lb, &tmp, size_mop);
1531 }
1532
1533 /* Handle non-aligned buf */
1534 if ((uint32_t)buf & 0x3)
1535 memcpy(buf, page_buf, size);
1536
1537 /* Wait for sequence to finish */
1538 stfsm_wait_seq(fsm);
1539
1540 stfsm_clear_fifo(fsm);
1541
1542 /* Exit 32-bit address mode, if required */
1543 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1544 stfsm_enter_32bit_addr(fsm, 0);
1545
1546 return 0;
1547}
1548
Lee Jones3f9d7202014-03-20 11:11:43 +00001549static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1550 uint32_t size, uint32_t offset)
Lee Jones176b4372014-03-20 09:20:59 +00001551{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001552 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
Lee Jones176b4372014-03-20 09:20:59 +00001553 uint32_t data_pads;
1554 uint32_t write_mask;
1555 uint32_t size_ub;
1556 uint32_t size_lb;
1557 uint32_t size_mop;
1558 uint32_t tmp[4];
1559 uint32_t page_buf[FLASH_PAGESIZE_32];
1560 uint8_t *t = (uint8_t *)&tmp;
1561 const uint8_t *p;
1562 int ret;
1563 int i;
1564
1565 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1566
1567 /* Enter 32-bit address mode, if required */
1568 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1569 stfsm_enter_32bit_addr(fsm, 1);
1570
1571 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1572 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1573 write_mask = (data_pads << 2) - 1;
1574
1575 /* Handle non-aligned buf */
1576 if ((uint32_t)buf & 0x3) {
1577 memcpy(page_buf, buf, size);
1578 p = (uint8_t *)page_buf;
1579 } else {
1580 p = buf;
1581 }
1582
1583 /* Handle non-aligned size */
1584 size_ub = (size + write_mask) & ~write_mask;
1585 size_lb = size & ~write_mask;
1586 size_mop = size & write_mask;
1587
1588 seq->data_size = TRANSFER_SIZE(size_ub);
1589 seq->addr1 = (offset >> 16) & 0xffff;
1590 seq->addr2 = offset & 0xffff;
1591
1592 /* Need to set FIFO to write mode, before writing data to FIFO (see
1593 * GNBvb79594)
1594 */
1595 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1596
1597 /*
1598 * Before writing data to the FIFO, apply a small delay to allow a
1599 * potential change of FIFO direction to complete.
1600 */
1601 if (fsm->fifo_dir_delay == 0)
1602 readl(fsm->base + SPI_FAST_SEQ_CFG);
1603 else
1604 udelay(fsm->fifo_dir_delay);
1605
1606
1607 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1608 if (size_lb) {
1609 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1610 p += size_lb;
1611 }
1612
1613 /* Handle non-aligned size */
1614 if (size_mop) {
1615 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1616 for (i = 0; i < size_mop; i++)
1617 t[i] = *p++;
1618
1619 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1620 }
1621
1622 /* Start sequence */
1623 stfsm_load_seq(fsm, seq);
1624
1625 /* Wait for sequence to finish */
1626 stfsm_wait_seq(fsm);
1627
1628 /* Wait for completion */
1629 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001630 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1631 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones176b4372014-03-20 09:20:59 +00001632
1633 /* Exit 32-bit address mode, if required */
Angus Clark009e7e62014-03-26 16:39:16 +00001634 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
Lee Jones176b4372014-03-20 09:20:59 +00001635 stfsm_enter_32bit_addr(fsm, 0);
Lee Jones176b4372014-03-20 09:20:59 +00001636
1637 return 0;
1638}
1639
Lee Jonese514f102014-03-20 09:20:57 +00001640/*
1641 * Read an address range from the flash chip. The address range
1642 * may be any size provided it is within the physical boundaries.
1643 */
1644static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1645 size_t *retlen, u_char *buf)
1646{
1647 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1648 uint32_t bytes;
1649
1650 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1651 __func__, (u32)from, len);
1652
1653 mutex_lock(&fsm->lock);
1654
1655 while (len > 0) {
1656 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1657
1658 stfsm_read(fsm, buf, bytes, from);
1659
1660 buf += bytes;
1661 from += bytes;
1662 len -= bytes;
1663
1664 *retlen += bytes;
1665 }
1666
1667 mutex_unlock(&fsm->lock);
1668
1669 return 0;
1670}
1671
Lee Jones3f9d7202014-03-20 11:11:43 +00001672static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
Lee Jones4a341fe2014-03-20 09:21:00 +00001673{
1674 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1675 int ret;
1676
1677 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1678
1679 /* Enter 32-bit address mode, if required */
1680 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1681 stfsm_enter_32bit_addr(fsm, 1);
1682
1683 seq->addr1 = (offset >> 16) & 0xffff;
1684 seq->addr2 = offset & 0xffff;
1685
1686 stfsm_load_seq(fsm, seq);
1687
1688 stfsm_wait_seq(fsm);
1689
1690 /* Wait for completion */
1691 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001692 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1693 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones4a341fe2014-03-20 09:21:00 +00001694
1695 /* Exit 32-bit address mode, if required */
1696 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1697 stfsm_enter_32bit_addr(fsm, 0);
1698
1699 return ret;
1700}
1701
1702static int stfsm_erase_chip(struct stfsm *fsm)
1703{
1704 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1705
1706 dev_dbg(fsm->dev, "erasing chip\n");
1707
1708 stfsm_load_seq(fsm, seq);
1709
1710 stfsm_wait_seq(fsm);
1711
1712 return stfsm_wait_busy(fsm);
1713}
1714
Lee Jones176b4372014-03-20 09:20:59 +00001715/*
1716 * Write an address range to the flash chip. Data must be written in
1717 * FLASH_PAGESIZE chunks. The address range may be any size provided
1718 * it is within the physical boundaries.
1719 */
1720static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1721 size_t *retlen, const u_char *buf)
1722{
1723 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1724
1725 u32 page_offs;
1726 u32 bytes;
1727 uint8_t *b = (uint8_t *)buf;
1728 int ret = 0;
1729
1730 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1731
Lee Jones176b4372014-03-20 09:20:59 +00001732 /* Offset within page */
1733 page_offs = to % FLASH_PAGESIZE;
1734
1735 mutex_lock(&fsm->lock);
1736
1737 while (len) {
1738 /* Write up to page boundary */
1739 bytes = min(FLASH_PAGESIZE - page_offs, len);
1740
1741 ret = stfsm_write(fsm, b, bytes, to);
1742 if (ret)
1743 goto out1;
1744
1745 b += bytes;
1746 len -= bytes;
1747 to += bytes;
1748
1749 /* We are now page-aligned */
1750 page_offs = 0;
1751
1752 *retlen += bytes;
1753
1754 }
1755
1756out1:
1757 mutex_unlock(&fsm->lock);
1758
1759 return ret;
1760}
1761
Lee Jones4a341fe2014-03-20 09:21:00 +00001762/*
1763 * Erase an address range on the flash chip. The address range may extend
1764 * one or more erase sectors. Return an error is there is a problem erasing.
1765 */
1766static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1767{
1768 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1769 u32 addr, len;
1770 int ret;
1771
1772 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1773 (long long)instr->addr, (long long)instr->len);
1774
1775 addr = instr->addr;
1776 len = instr->len;
1777
1778 mutex_lock(&fsm->lock);
1779
1780 /* Whole-chip erase? */
1781 if (len == mtd->size) {
1782 ret = stfsm_erase_chip(fsm);
1783 if (ret)
1784 goto out1;
1785 } else {
1786 while (len) {
1787 ret = stfsm_erase_sector(fsm, addr);
1788 if (ret)
1789 goto out1;
1790
1791 addr += mtd->erasesize;
1792 len -= mtd->erasesize;
1793 }
1794 }
1795
1796 mutex_unlock(&fsm->lock);
1797
1798 instr->state = MTD_ERASE_DONE;
1799 mtd_erase_callback(instr);
1800
1801 return 0;
1802
1803out1:
1804 instr->state = MTD_ERASE_FAILED;
1805 mutex_unlock(&fsm->lock);
1806
1807 return ret;
1808}
1809
Lee Jones3f9d7202014-03-20 11:11:43 +00001810static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
Lee Jones1bd512b2014-03-20 09:20:38 +00001811{
1812 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1813 uint32_t tmp[2];
1814
1815 stfsm_load_seq(fsm, seq);
1816
1817 stfsm_read_fifo(fsm, tmp, 8);
1818
1819 memcpy(jedec, tmp, 5);
1820
1821 stfsm_wait_seq(fsm);
1822}
1823
1824static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1825{
Lee Jones24fec652014-03-20 09:20:41 +00001826 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001827 u16 ext_jedec;
1828 u32 jedec;
1829 u8 id[5];
1830
1831 stfsm_read_jedec(fsm, id);
1832
1833 jedec = id[0] << 16 | id[1] << 8 | id[2];
1834 /*
1835 * JEDEC also defines an optional "extended device information"
1836 * string for after vendor-specific data, after the three bytes
1837 * we use here. Supporting some chips might require using it.
1838 */
1839 ext_jedec = id[3] << 8 | id[4];
1840
1841 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1842 jedec, id[0], id[1], id[2], id[3], id[4]);
1843
Lee Jones24fec652014-03-20 09:20:41 +00001844 for (info = flash_types; info->name; info++) {
1845 if (info->jedec_id == jedec) {
1846 if (info->ext_id && info->ext_id != ext_jedec)
1847 continue;
1848 return info;
1849 }
1850 }
1851 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1852
Lee Jones1bd512b2014-03-20 09:20:38 +00001853 return NULL;
1854}
1855
Lee Jones86f309fd2014-03-20 09:20:35 +00001856static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1857{
1858 int ret, timeout = 10;
1859
1860 /* Wait for controller to accept mode change */
1861 while (--timeout) {
1862 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1863 if (ret & 0x1)
1864 break;
1865 udelay(1);
1866 }
1867
1868 if (!timeout)
1869 return -EBUSY;
1870
1871 writel(mode, fsm->base + SPI_MODESELECT);
1872
1873 return 0;
1874}
1875
1876static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1877{
1878 uint32_t emi_freq;
1879 uint32_t clk_div;
1880
1881 /* TODO: Make this dynamic */
1882 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1883
1884 /*
1885 * Calculate clk_div - values between 2 and 128
1886 * Multiple of 2, rounded up
1887 */
1888 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1889 if (clk_div < 2)
1890 clk_div = 2;
1891 else if (clk_div > 128)
1892 clk_div = 128;
1893
1894 /*
1895 * Determine a suitable delay for the IP to complete a change of
1896 * direction of the FIFO. The required delay is related to the clock
1897 * divider used. The following heuristics are based on empirical tests,
1898 * using a 100MHz EMI clock.
1899 */
1900 if (clk_div <= 4)
1901 fsm->fifo_dir_delay = 0;
1902 else if (clk_div <= 10)
1903 fsm->fifo_dir_delay = 1;
1904 else
1905 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1906
1907 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1908 emi_freq, spi_freq, clk_div);
1909
1910 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1911}
1912
1913static int stfsm_init(struct stfsm *fsm)
1914{
1915 int ret;
1916
1917 /* Perform a soft reset of the FSM controller */
1918 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1919 udelay(1);
1920 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1921
1922 /* Set clock to 'safe' frequency initially */
1923 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1924
1925 /* Switch to FSM */
1926 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1927 if (ret)
1928 return ret;
1929
1930 /* Set timing parameters */
1931 writel(SPI_CFG_DEVICE_ST |
1932 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1933 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1934 SPI_CFG_DEFAULT_DATA_HOLD,
1935 fsm->base + SPI_CONFIGDATA);
1936 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1937
Angus Clark009e7e62014-03-26 16:39:16 +00001938 /*
1939 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1940 * our purposes, the WAIT instruction is used purely to achieve
1941 * "sequence validity" rather than actually implement a delay.
1942 */
1943 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1944
Lee Jones86f309fd2014-03-20 09:20:35 +00001945 /* Clear FIFO, just in case */
1946 stfsm_clear_fifo(fsm);
1947
1948 return 0;
1949}
1950
Lee Jonesa63984c2014-03-20 09:20:46 +00001951static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1952{
1953 struct stfsm *fsm = platform_get_drvdata(pdev);
1954 struct device_node *np = pdev->dev.of_node;
1955 struct regmap *regmap;
1956 uint32_t boot_device_reg;
1957 uint32_t boot_device_spi;
1958 uint32_t boot_device; /* Value we read from *boot_device_reg */
1959 int ret;
1960
1961 /* Booting from SPI NOR Flash is the default */
1962 fsm->booted_from_spi = true;
1963
1964 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1965 if (IS_ERR(regmap))
1966 goto boot_device_fail;
1967
Lee Jones0ea7d702014-03-20 09:20:50 +00001968 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1969
1970 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1971
Lee Jonesa63984c2014-03-20 09:20:46 +00001972 /* Where in the syscon the boot device information lives */
1973 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1974 if (ret)
1975 goto boot_device_fail;
1976
1977 /* Boot device value when booted from SPI NOR */
1978 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1979 if (ret)
1980 goto boot_device_fail;
1981
1982 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1983 if (ret)
1984 goto boot_device_fail;
1985
1986 if (boot_device != boot_device_spi)
1987 fsm->booted_from_spi = false;
1988
1989 return;
1990
1991boot_device_fail:
1992 dev_warn(&pdev->dev,
1993 "failed to fetch boot device, assuming boot from SPI\n");
1994}
1995
Lee Jonesd90db4a2014-03-20 09:20:33 +00001996static int stfsm_probe(struct platform_device *pdev)
1997{
1998 struct device_node *np = pdev->dev.of_node;
Lee Jones221cff12014-03-20 09:21:07 +00001999 struct mtd_part_parser_data ppdata;
Lee Jones24fec652014-03-20 09:20:41 +00002000 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002001 struct resource *res;
2002 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00002003 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002004
2005 if (!np) {
2006 dev_err(&pdev->dev, "No DT found\n");
2007 return -EINVAL;
2008 }
Lee Jones221cff12014-03-20 09:21:07 +00002009 ppdata.of_node = np;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002010
2011 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2012 if (!fsm)
2013 return -ENOMEM;
2014
2015 fsm->dev = &pdev->dev;
2016
2017 platform_set_drvdata(pdev, fsm);
2018
2019 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2020 if (!res) {
2021 dev_err(&pdev->dev, "Resource not found\n");
2022 return -ENODEV;
2023 }
2024
2025 fsm->base = devm_ioremap_resource(&pdev->dev, res);
2026 if (IS_ERR(fsm->base)) {
2027 dev_err(&pdev->dev,
2028 "Failed to reserve memory region %pR\n", res);
2029 return PTR_ERR(fsm->base);
2030 }
2031
2032 mutex_init(&fsm->lock);
2033
Lee Jones86f309fd2014-03-20 09:20:35 +00002034 ret = stfsm_init(fsm);
2035 if (ret) {
2036 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2037 return ret;
2038 }
2039
Lee Jonesa63984c2014-03-20 09:20:46 +00002040 stfsm_fetch_platform_configs(pdev);
2041
Lee Jones1bd512b2014-03-20 09:20:38 +00002042 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00002043 info = stfsm_jedec_probe(fsm);
2044 if (!info)
2045 return -ENODEV;
2046 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00002047
Lee Jones3b5d1982014-03-20 09:20:43 +00002048 /* Use device size to determine address width */
2049 if (info->sector_size * info->n_sectors > 0x1000000)
2050 info->flags |= FLASH_FLAG_32BIT_ADDR;
2051
Lee Jones218b8702014-03-20 09:20:55 +00002052 /*
2053 * Configure READ/WRITE/ERASE sequences according to platform and
2054 * device flags.
2055 */
2056 if (info->config) {
2057 ret = info->config(fsm);
2058 if (ret)
2059 return ret;
Lee Jones4eb3f0d82014-03-20 09:20:56 +00002060 } else {
2061 ret = stfsm_prepare_rwe_seqs_default(fsm);
2062 if (ret)
2063 return ret;
Lee Jones218b8702014-03-20 09:20:55 +00002064 }
2065
Lee Jones221cff12014-03-20 09:21:07 +00002066 fsm->mtd.name = info->name;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002067 fsm->mtd.dev.parent = &pdev->dev;
2068 fsm->mtd.type = MTD_NORFLASH;
2069 fsm->mtd.writesize = 4;
2070 fsm->mtd.writebufsize = fsm->mtd.writesize;
2071 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00002072 fsm->mtd.size = info->sector_size * info->n_sectors;
2073 fsm->mtd.erasesize = info->sector_size;
2074
Lee Jonese514f102014-03-20 09:20:57 +00002075 fsm->mtd._read = stfsm_mtd_read;
Lee Jones176b4372014-03-20 09:20:59 +00002076 fsm->mtd._write = stfsm_mtd_write;
Lee Jones4a341fe2014-03-20 09:21:00 +00002077 fsm->mtd._erase = stfsm_mtd_erase;
Lee Jonese514f102014-03-20 09:20:57 +00002078
Lee Jones4a341fe2014-03-20 09:21:00 +00002079 dev_info(&pdev->dev,
Lee Jones24fec652014-03-20 09:20:41 +00002080 "Found serial flash device: %s\n"
2081 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2082 info->name,
2083 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2084 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00002085
Lee Jones221cff12014-03-20 09:21:07 +00002086 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002087}
2088
2089static int stfsm_remove(struct platform_device *pdev)
2090{
2091 struct stfsm *fsm = platform_get_drvdata(pdev);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002092
Lee Jonesd9ba46d2014-03-20 11:11:47 +00002093 return mtd_device_unregister(&fsm->mtd);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002094}
2095
2096static struct of_device_id stfsm_match[] = {
2097 { .compatible = "st,spi-fsm", },
2098 {},
2099};
2100MODULE_DEVICE_TABLE(of, stfsm_match);
2101
2102static struct platform_driver stfsm_driver = {
2103 .probe = stfsm_probe,
2104 .remove = stfsm_remove,
2105 .driver = {
2106 .name = "st-spi-fsm",
2107 .owner = THIS_MODULE,
2108 .of_match_table = stfsm_match,
2109 },
2110};
2111module_platform_driver(stfsm_driver);
2112
2113MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2114MODULE_DESCRIPTION("ST SPI FSM driver");
2115MODULE_LICENSE("GPL");