blob: fc193a94307df0c89528efc348ea0ead20b1a73e [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 Jones5343a122014-03-20 09:21:04 +0000204/* S25FLxxxS commands */
205#define S25FL_CMD_WRITE4_1_1_4 0x34
206#define S25FL_CMD_SE4 0xdc
207#define S25FL_CMD_CLSR 0x30
208#define S25FL_CMD_DYBWR 0xe1
209#define S25FL_CMD_DYBRD 0xe0
210#define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
Brian Norris92d3af92014-04-08 18:56:06 -0700211 * 'SPINOR_OP_WRITE_1_4_4'
Lee Jones5343a122014-03-20 09:21:04 +0000212 * as found on N25Qxxx devices! */
213
Lee Jones176b4372014-03-20 09:20:59 +0000214/* Status register */
215#define FLASH_STATUS_BUSY 0x01
216#define FLASH_STATUS_WEL 0x02
217#define FLASH_STATUS_BP0 0x04
218#define FLASH_STATUS_BP1 0x08
219#define FLASH_STATUS_BP2 0x10
220#define FLASH_STATUS_SRWP0 0x80
221#define FLASH_STATUS_TIMEOUT 0xff
Lee Jones5343a122014-03-20 09:21:04 +0000222/* S25FL Error Flags */
223#define S25FL_STATUS_E_ERR 0x20
224#define S25FL_STATUS_P_ERR 0x40
Lee Jones176b4372014-03-20 09:20:59 +0000225
Angus Clark5d0bdda2014-03-26 16:39:18 +0000226#define N25Q_CMD_WRVCR 0x81
227#define N25Q_CMD_RDVCR 0x85
228#define N25Q_CMD_RDVECR 0x65
229#define N25Q_CMD_RDNVCR 0xb5
230#define N25Q_CMD_WRNVCR 0xb1
231
Lee Jonese514f102014-03-20 09:20:57 +0000232#define FLASH_PAGESIZE 256 /* In Bytes */
233#define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
Lee Jones176b4372014-03-20 09:20:59 +0000234#define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
Lee Jonese514f102014-03-20 09:20:57 +0000235
Lee Jonese85a6192014-03-20 09:20:54 +0000236/*
237 * Flags to tweak operation of default read/write/erase routines
238 */
239#define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
240#define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
Lee Jonese85a6192014-03-20 09:20:54 +0000241#define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
242#define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
243
Lee Jonese6b1bb42014-03-20 09:21:06 +0000244struct stfsm_seq {
245 uint32_t data_size;
246 uint32_t addr1;
247 uint32_t addr2;
248 uint32_t addr_cfg;
249 uint32_t seq_opc[5];
250 uint32_t mode;
251 uint32_t dummy;
252 uint32_t status;
253 uint8_t seq[16];
254 uint32_t seq_cfg;
255} __packed __aligned(4);
256
Lee Jonesd90db4a2014-03-20 09:20:33 +0000257struct stfsm {
258 struct device *dev;
259 void __iomem *base;
260 struct resource *region;
261 struct mtd_info mtd;
262 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000263 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000264
Lee Jonese85a6192014-03-20 09:20:54 +0000265 uint32_t configuration;
Lee Jones86f309fd2014-03-20 09:20:35 +0000266 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000267 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000268 bool reset_signal;
269 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000270
Lee Jonese6b1bb42014-03-20 09:21:06 +0000271 struct stfsm_seq stfsm_seq_read;
272 struct stfsm_seq stfsm_seq_write;
273 struct stfsm_seq stfsm_seq_en_32bit_addr;
274};
Lee Jones3c8b85b2014-03-20 09:20:36 +0000275
Lee Jones08981272014-03-20 09:20:42 +0000276/* Parameters to configure a READ or WRITE FSM sequence */
277struct seq_rw_config {
278 uint32_t flags; /* flags to support config */
279 uint8_t cmd; /* FLASH command */
280 int write; /* Write Sequence */
281 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
282 uint8_t data_pads; /* No. of data pads */
283 uint8_t mode_data; /* MODE data */
284 uint8_t mode_cycles; /* No. of MODE cycles */
285 uint8_t dummy_cycles; /* No. of DUMMY cycles */
286};
287
Lee Jones11d7f822014-03-20 09:20:40 +0000288/* SPI Flash Device Table */
289struct flash_info {
290 char *name;
291 /*
292 * JEDEC id zero means "no ID" (most older chips); otherwise it has
293 * a high byte of zero plus three data bytes: the manufacturer id,
294 * then a two byte device id.
295 */
296 u32 jedec_id;
297 u16 ext_id;
298 /*
Brian Norris92d3af92014-04-08 18:56:06 -0700299 * The size listed here is what works with SPINOR_OP_SE, which isn't
Lee Jones11d7f822014-03-20 09:20:40 +0000300 * necessarily called a "sector" by the vendor.
301 */
302 unsigned sector_size;
303 u16 n_sectors;
304 u32 flags;
305 /*
306 * Note, where FAST_READ is supported, freq_max specifies the
307 * FAST_READ frequency, not the READ frequency.
308 */
309 u32 max_freq;
310 int (*config)(struct stfsm *);
311};
312
Lee Jones218b8702014-03-20 09:20:55 +0000313static int stfsm_n25q_config(struct stfsm *fsm);
Lee Jones898180662014-03-20 09:21:03 +0000314static int stfsm_mx25_config(struct stfsm *fsm);
Lee Jones5343a122014-03-20 09:21:04 +0000315static int stfsm_s25fl_config(struct stfsm *fsm);
Lee Jonescd7cac92014-03-20 09:21:05 +0000316static int stfsm_w25q_config(struct stfsm *fsm);
Lee Jones218b8702014-03-20 09:20:55 +0000317
Lee Jones11d7f822014-03-20 09:20:40 +0000318static struct flash_info flash_types[] = {
319 /*
320 * ST Microelectronics/Numonyx --
321 * (newer production versions may have feature updates
322 * (eg faster operating frequency)
323 */
324#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
325 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
326 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
327 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
328 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
329 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
330 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
331
332#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
333 FLASH_FLAG_READ_FAST | \
334 FLASH_FLAG_READ_1_1_2 | \
335 FLASH_FLAG_WRITE_1_1_2)
336 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
337 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
338
Angus Clark6b6d3732014-03-26 16:39:20 +0000339 /* Macronix MX25xxx
340 * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
341 * where operating frequency must be reduced.
342 */
Lee Jones11d7f822014-03-20 09:20:40 +0000343#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
344 FLASH_FLAG_READ_FAST | \
345 FLASH_FLAG_READ_1_1_2 | \
346 FLASH_FLAG_READ_1_2_2 | \
347 FLASH_FLAG_READ_1_1_4 | \
Lee Jones11d7f822014-03-20 09:20:40 +0000348 FLASH_FLAG_SE_4K | \
349 FLASH_FLAG_SE_32K)
Angus Clark6b6d3732014-03-26 16:39:20 +0000350 { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
351 (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
352 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000353 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
Lee Jones898180662014-03-20 09:21:03 +0000354 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
355 stfsm_mx25_config },
Angus Clark5fa98062014-03-26 16:39:15 +0000356 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
357 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
358 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000359
360#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
361 FLASH_FLAG_READ_FAST | \
362 FLASH_FLAG_READ_1_1_2 | \
363 FLASH_FLAG_READ_1_2_2 | \
364 FLASH_FLAG_READ_1_1_4 | \
365 FLASH_FLAG_READ_1_4_4 | \
366 FLASH_FLAG_WRITE_1_1_2 | \
367 FLASH_FLAG_WRITE_1_2_2 | \
368 FLASH_FLAG_WRITE_1_1_4 | \
369 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000370 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
371 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000372 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000373 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000374
375 /*
376 * Spansion S25FLxxxP
377 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
378 */
379#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
380 FLASH_FLAG_READ_1_1_2 | \
381 FLASH_FLAG_READ_1_2_2 | \
382 FLASH_FLAG_READ_1_1_4 | \
383 FLASH_FLAG_READ_1_4_4 | \
384 FLASH_FLAG_WRITE_1_1_4 | \
385 FLASH_FLAG_READ_FAST)
Angus Clark85bdcf62014-03-26 16:39:19 +0000386 { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
387 stfsm_s25fl_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000388 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000389 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000390 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000391 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000392
393 /*
394 * Spansion S25FLxxxS
395 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
396 * - RESET# signal supported by die but not bristled out on all
397 * package types. The package type is a function of board design,
398 * so this information is captured in the board's flags.
399 * - Supports 'DYB' sector protection. Depending on variant, sectors
400 * may default to locked state on power-on.
401 */
402#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
403 FLASH_FLAG_RESET | \
404 FLASH_FLAG_DYB_LOCKING)
405 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000406 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000407 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000408 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000409 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
Lee Jones5343a122014-03-20 09:21:04 +0000410 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000411 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
Lee Jones5343a122014-03-20 09:21:04 +0000412 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000413
414 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
415#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
416 FLASH_FLAG_READ_FAST | \
417 FLASH_FLAG_READ_1_1_2 | \
418 FLASH_FLAG_WRITE_1_1_2)
419 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
420 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
421 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
422 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
423 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
424
425 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
426#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
427 FLASH_FLAG_READ_FAST | \
428 FLASH_FLAG_READ_1_1_2 | \
429 FLASH_FLAG_READ_1_2_2 | \
430 FLASH_FLAG_READ_1_1_4 | \
431 FLASH_FLAG_READ_1_4_4 | \
432 FLASH_FLAG_WRITE_1_1_4)
Lee Jonescd7cac92014-03-20 09:21:05 +0000433 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
434 stfsm_w25q_config },
435 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
436 stfsm_w25q_config },
437 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
438 stfsm_w25q_config },
439 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
440 stfsm_w25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000441
442 /* Sentinel */
443 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
444};
445
Lee Jonesa37b2f52014-03-20 09:20:53 +0000446/*
447 * FSM message sequence configurations:
448 *
449 * All configs are presented in order of preference
450 */
451
452/* Default READ configurations, in order of preference */
453static struct seq_rw_config default_read_configs[] = {
Brian Norris92d3af92014-04-08 18:56:06 -0700454 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
455 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
456 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
457 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
458 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
459 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
Lee Jonesa37b2f52014-03-20 09:20:53 +0000460 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
461};
462
463/* Default WRITE configurations */
464static struct seq_rw_config default_write_configs[] = {
Brian Norris92d3af92014-04-08 18:56:06 -0700465 {FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
466 {FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
467 {FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
468 {FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
469 {FLASH_FLAG_READ_WRITE, SPINOR_OP_WRITE, 1, 1, 1, 0x00, 0, 0},
Lee Jonesa37b2f52014-03-20 09:20:53 +0000470 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
471};
472
Lee Jonese85a6192014-03-20 09:20:54 +0000473/*
474 * [N25Qxxx] Configuration
475 */
476#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
477#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
478#define N25Q_VCR_WRAP_CONT 0x3
479
480/* N25Q 3-byte Address READ configurations
481 * - 'FAST' variants configured for 8 dummy cycles.
482 *
483 * Note, the number of dummy cycles used for 'FAST' READ operations is
484 * configurable and would normally be tuned according to the READ command and
485 * operating frequency. However, this applies universally to all 'FAST' READ
486 * commands, including those used by the SPIBoot controller, and remains in
487 * force until the device is power-cycled. Since the SPIBoot controller is
488 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
489 * cycles.
490 */
491static struct seq_rw_config n25q_read3_configs[] = {
Brian Norris92d3af92014-04-08 18:56:06 -0700492 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
493 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
494 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
495 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
496 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
497 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
Lee Jonese85a6192014-03-20 09:20:54 +0000498 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
499};
500
501/* N25Q 4-byte Address READ configurations
502 * - use special 4-byte address READ commands (reduces overheads, and
503 * reduces risk of hitting watchdog reset issues).
504 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
505 */
506static struct seq_rw_config n25q_read4_configs[] = {
Brian Norris92d3af92014-04-08 18:56:06 -0700507 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
508 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
509 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
510 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
511 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
512 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
Lee Jonese85a6192014-03-20 09:20:54 +0000513 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
514};
515
Lee Jones898180662014-03-20 09:21:03 +0000516/*
517 * [MX25xxx] Configuration
518 */
519#define MX25_STATUS_QE (0x1 << 6)
520
521static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
522{
523 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
524 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700525 SEQ_OPC_OPCODE(SPINOR_OP_EN4B_ADDR) |
Lee Jones898180662014-03-20 09:21:03 +0000526 SEQ_OPC_CSDEASSERT);
527
528 seq->seq[0] = STFSM_INST_CMD1;
529 seq->seq[1] = STFSM_INST_WAIT;
530 seq->seq[2] = STFSM_INST_STOP;
531
532 seq->seq_cfg = (SEQ_CFG_PADS_1 |
533 SEQ_CFG_ERASE |
534 SEQ_CFG_READNOTWRITE |
535 SEQ_CFG_CSDEASSERT |
536 SEQ_CFG_STARTSEQ);
537
538 return 0;
539}
540
Lee Jones5343a122014-03-20 09:21:04 +0000541/*
542 * [S25FLxxx] Configuration
543 */
544#define STFSM_S25FL_CONFIG_QE (0x1 << 1)
545
546/*
547 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
548 * Register, Extended Address Modes, and a 32-bit address command set. The
549 * 32-bit address command set is used here, since it avoids any problems with
550 * entering a state that is incompatible with the SPIBoot Controller.
551 */
552static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
Brian Norris92d3af92014-04-08 18:56:06 -0700553 {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
554 {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
555 {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
556 {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
557 {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
558 {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
Lee Jones5343a122014-03-20 09:21:04 +0000559 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
560};
561
562static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
563 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
564 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
565 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
566};
567
Lee Jonescd7cac92014-03-20 09:21:05 +0000568/*
569 * [W25Qxxx] Configuration
570 */
Angus Clark5d0bdda2014-03-26 16:39:18 +0000571#define W25Q_STATUS_QE (0x1 << 1)
Lee Jonescd7cac92014-03-20 09:21:05 +0000572
Lee Jones1bd512b2014-03-20 09:20:38 +0000573static struct stfsm_seq stfsm_seq_read_jedec = {
574 .data_size = TRANSFER_SIZE(8),
575 .seq_opc[0] = (SEQ_OPC_PADS_1 |
576 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700577 SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
Lee Jones1bd512b2014-03-20 09:20:38 +0000578 .seq = {
579 STFSM_INST_CMD1,
580 STFSM_INST_DATA_READ,
581 STFSM_INST_STOP,
582 },
583 .seq_cfg = (SEQ_CFG_PADS_1 |
584 SEQ_CFG_READNOTWRITE |
585 SEQ_CFG_CSDEASSERT |
586 SEQ_CFG_STARTSEQ),
587};
588
Lee Jones176b4372014-03-20 09:20:59 +0000589static struct stfsm_seq stfsm_seq_read_status_fifo = {
590 .data_size = TRANSFER_SIZE(4),
591 .seq_opc[0] = (SEQ_OPC_PADS_1 |
592 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700593 SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
Lee Jones176b4372014-03-20 09:20:59 +0000594 .seq = {
595 STFSM_INST_CMD1,
596 STFSM_INST_DATA_READ,
597 STFSM_INST_STOP,
598 },
599 .seq_cfg = (SEQ_CFG_PADS_1 |
600 SEQ_CFG_READNOTWRITE |
601 SEQ_CFG_CSDEASSERT |
602 SEQ_CFG_STARTSEQ),
603};
604
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000605static struct stfsm_seq stfsm_seq_erase_sector = {
606 /* 'addr_cfg' configured during initialisation */
607 .seq_opc = {
608 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700609 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000610
611 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700612 SEQ_OPC_OPCODE(SPINOR_OP_SE)),
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000613 },
614 .seq = {
615 STFSM_INST_CMD1,
616 STFSM_INST_CMD2,
617 STFSM_INST_ADD1,
618 STFSM_INST_ADD2,
619 STFSM_INST_STOP,
620 },
621 .seq_cfg = (SEQ_CFG_PADS_1 |
622 SEQ_CFG_READNOTWRITE |
623 SEQ_CFG_CSDEASSERT |
624 SEQ_CFG_STARTSEQ),
625};
626
Lee Jones4a341fe2014-03-20 09:21:00 +0000627static struct stfsm_seq stfsm_seq_erase_chip = {
628 .seq_opc = {
629 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700630 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
Lee Jones4a341fe2014-03-20 09:21:00 +0000631
632 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700633 SEQ_OPC_OPCODE(SPINOR_OP_CHIPERASE) | SEQ_OPC_CSDEASSERT),
Lee Jones4a341fe2014-03-20 09:21:00 +0000634 },
635 .seq = {
636 STFSM_INST_CMD1,
637 STFSM_INST_CMD2,
638 STFSM_INST_WAIT,
639 STFSM_INST_STOP,
640 },
641 .seq_cfg = (SEQ_CFG_PADS_1 |
642 SEQ_CFG_ERASE |
643 SEQ_CFG_READNOTWRITE |
644 SEQ_CFG_CSDEASSERT |
645 SEQ_CFG_STARTSEQ),
646};
647
Lee Jones150571b2014-03-20 09:21:02 +0000648static struct stfsm_seq stfsm_seq_write_status = {
649 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700650 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
Lee Jones150571b2014-03-20 09:21:02 +0000651 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700652 SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
Lee Jones150571b2014-03-20 09:21:02 +0000653 .seq = {
654 STFSM_INST_CMD1,
655 STFSM_INST_CMD2,
656 STFSM_INST_STA_WR1,
657 STFSM_INST_STOP,
658 },
659 .seq_cfg = (SEQ_CFG_PADS_1 |
660 SEQ_CFG_READNOTWRITE |
661 SEQ_CFG_CSDEASSERT |
662 SEQ_CFG_STARTSEQ),
663};
664
Lee Jones6bd29602014-03-20 09:20:48 +0000665static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
666{
667 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700668 SEQ_OPC_OPCODE(SPINOR_OP_EN4B_ADDR));
Lee Jones6bd29602014-03-20 09:20:48 +0000669 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700670 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
Lee Jones6bd29602014-03-20 09:20:48 +0000671 SEQ_OPC_CSDEASSERT);
672
673 seq->seq[0] = STFSM_INST_CMD2;
674 seq->seq[1] = STFSM_INST_CMD1;
675 seq->seq[2] = STFSM_INST_WAIT;
676 seq->seq[3] = STFSM_INST_STOP;
677
678 seq->seq_cfg = (SEQ_CFG_PADS_1 |
679 SEQ_CFG_ERASE |
680 SEQ_CFG_READNOTWRITE |
681 SEQ_CFG_CSDEASSERT |
682 SEQ_CFG_STARTSEQ);
683
684 return 0;
685}
686
Lee Jones3c8b85b2014-03-20 09:20:36 +0000687static inline int stfsm_is_idle(struct stfsm *fsm)
688{
689 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
690}
691
Lee Jones86f309fd2014-03-20 09:20:35 +0000692static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
693{
694 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
695}
696
697static void stfsm_clear_fifo(struct stfsm *fsm)
698{
699 uint32_t avail;
700
701 for (;;) {
702 avail = stfsm_fifo_available(fsm);
703 if (!avail)
704 break;
705
706 while (avail) {
707 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
708 avail--;
709 }
710 }
711}
712
Lee Jones3c8b85b2014-03-20 09:20:36 +0000713static inline void stfsm_load_seq(struct stfsm *fsm,
714 const struct stfsm_seq *seq)
715{
716 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
717 const uint32_t *src = (const uint32_t *)seq;
718 int words = sizeof(*seq) / sizeof(*src);
719
720 BUG_ON(!stfsm_is_idle(fsm));
721
722 while (words--) {
723 writel(*src, dst);
724 src++;
725 dst += 4;
726 }
727}
728
729static void stfsm_wait_seq(struct stfsm *fsm)
730{
731 unsigned long deadline;
732 int timeout = 0;
733
734 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
735
736 while (!timeout) {
737 if (time_after_eq(jiffies, deadline))
738 timeout = 1;
739
740 if (stfsm_is_idle(fsm))
741 return;
742
743 cond_resched();
744 }
745
746 dev_err(fsm->dev, "timeout on sequence completion\n");
747}
748
Lee Jones3f9d7202014-03-20 11:11:43 +0000749static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
Lee Jones030e82d2014-03-20 09:20:37 +0000750{
751 uint32_t remaining = size >> 2;
752 uint32_t avail;
753 uint32_t words;
754
755 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
756
757 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
758
759 while (remaining) {
760 for (;;) {
761 avail = stfsm_fifo_available(fsm);
762 if (avail)
763 break;
764 udelay(1);
765 }
766 words = min(avail, remaining);
767 remaining -= words;
768
769 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
770 buf += words;
771 }
772}
773
Lee Jones3f9d7202014-03-20 11:11:43 +0000774static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
775 uint32_t size)
Lee Jones30ca64f2014-03-20 09:20:58 +0000776{
777 uint32_t words = size >> 2;
778
779 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
780
781 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
782
783 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
784
785 return size;
786}
787
Lee Jones0de08e42014-03-20 09:20:51 +0000788static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
789{
Lee Jonese6b1bb42014-03-20 09:21:06 +0000790 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
Brian Norris92d3af92014-04-08 18:56:06 -0700791 uint32_t cmd = enter ? SPINOR_OP_EN4B_ADDR : SPINOR_OP_EX4B_ADDR;
Lee Jones0de08e42014-03-20 09:20:51 +0000792
793 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
794 SEQ_OPC_CYCLES(8) |
795 SEQ_OPC_OPCODE(cmd) |
796 SEQ_OPC_CSDEASSERT);
797
798 stfsm_load_seq(fsm, seq);
799
800 stfsm_wait_seq(fsm);
801
802 return 0;
803}
804
Lee Jones176b4372014-03-20 09:20:59 +0000805static uint8_t stfsm_wait_busy(struct stfsm *fsm)
806{
807 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
808 unsigned long deadline;
809 uint32_t status;
810 int timeout = 0;
811
812 /* Use RDRS1 */
813 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
814 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700815 SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
Lee Jones176b4372014-03-20 09:20:59 +0000816
817 /* Load read_status sequence */
818 stfsm_load_seq(fsm, seq);
819
820 /*
821 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
822 */
823 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
824 while (!timeout) {
Lee Jones176b4372014-03-20 09:20:59 +0000825 if (time_after_eq(jiffies, deadline))
826 timeout = 1;
827
828 stfsm_wait_seq(fsm);
829
830 stfsm_read_fifo(fsm, &status, 4);
831
832 if ((status & FLASH_STATUS_BUSY) == 0)
833 return 0;
834
835 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
836 ((status & S25FL_STATUS_P_ERR) ||
837 (status & S25FL_STATUS_E_ERR)))
838 return (uint8_t)(status & 0xff);
839
840 if (!timeout)
841 /* Restart */
842 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
Lee Jonesea7864b2014-03-20 11:11:46 +0000843
844 cond_resched();
Lee Jones176b4372014-03-20 09:20:59 +0000845 }
846
847 dev_err(fsm->dev, "timeout on wait_busy\n");
848
849 return FLASH_STATUS_TIMEOUT;
850}
851
Lee Jonesac94dbc2014-03-20 09:21:01 +0000852static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
Angus Clark5d0bdda2014-03-26 16:39:18 +0000853 uint8_t *data, int bytes)
Lee Jonesac94dbc2014-03-20 09:21:01 +0000854{
855 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
856 uint32_t tmp;
Angus Clark5d0bdda2014-03-26 16:39:18 +0000857 uint8_t *t = (uint8_t *)&tmp;
858 int i;
Lee Jonesac94dbc2014-03-20 09:21:01 +0000859
Angus Clark5d0bdda2014-03-26 16:39:18 +0000860 dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
861 cmd, bytes);
Lee Jonesac94dbc2014-03-20 09:21:01 +0000862
Angus Clark5d0bdda2014-03-26 16:39:18 +0000863 BUG_ON(bytes != 1 && bytes != 2);
864
865 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Lee Jonesac94dbc2014-03-20 09:21:01 +0000866 SEQ_OPC_OPCODE(cmd)),
867
868 stfsm_load_seq(fsm, seq);
869
870 stfsm_read_fifo(fsm, &tmp, 4);
871
Angus Clark5d0bdda2014-03-26 16:39:18 +0000872 for (i = 0; i < bytes; i++)
873 data[i] = t[i];
Lee Jonesac94dbc2014-03-20 09:21:01 +0000874
875 stfsm_wait_seq(fsm);
876
877 return 0;
878}
879
Angus Clark5d0bdda2014-03-26 16:39:18 +0000880static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
881 uint16_t data, int bytes, int wait_busy)
Lee Jones150571b2014-03-20 09:21:02 +0000882{
883 struct stfsm_seq *seq = &stfsm_seq_write_status;
884
Angus Clark5d0bdda2014-03-26 16:39:18 +0000885 dev_dbg(fsm->dev,
886 "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
887 " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
Lee Jones150571b2014-03-20 09:21:02 +0000888
Angus Clark5d0bdda2014-03-26 16:39:18 +0000889 BUG_ON(bytes != 1 && bytes != 2);
890
891 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
892 SEQ_OPC_OPCODE(cmd));
893
894 seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
895 seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
Lee Jones150571b2014-03-20 09:21:02 +0000896
897 stfsm_load_seq(fsm, seq);
898
899 stfsm_wait_seq(fsm);
900
Angus Clark5d0bdda2014-03-26 16:39:18 +0000901 if (wait_busy)
902 stfsm_wait_busy(fsm);
Lee Jones249516c2014-03-20 09:20:52 +0000903
904 return 0;
905}
906
Lee Jones0ea7d702014-03-20 09:20:50 +0000907/*
908 * SoC reset on 'boot-from-spi' systems
909 *
910 * Certain modes of operation cause the Flash device to enter a particular state
911 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
912 * Addr' commands). On boot-from-spi systems, it is important to consider what
913 * happens if a warm reset occurs during this period. The SPIBoot controller
914 * assumes that Flash device is in its default reset state, 24-bit address mode,
915 * and ready to accept commands. This can be achieved using some form of
916 * on-board logic/controller to force a device POR in response to a SoC-level
917 * reset or by making use of the device reset signal if available (limited
918 * number of devices only).
919 *
920 * Failure to take such precautions can cause problems following a warm reset.
921 * For some operations (e.g. ERASE), there is little that can be done. For
922 * other modes of operation (e.g. 32-bit addressing), options are often
923 * available that can help minimise the window in which a reset could cause a
924 * problem.
925 *
926 */
927static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
928{
929 /* Reset signal is available on the board and supported by the device */
930 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
931 return true;
932
933 /* Board-level logic forces a power-on-reset */
934 if (fsm->reset_por)
935 return true;
936
937 /* Reset is not properly handled and may result in failure to reboot */
938 return false;
939}
940
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000941/* Configure 'addr_cfg' according to addressing mode */
942static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
943 struct stfsm_seq *seq)
944{
945 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
946
947 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
948 ADR_CFG_PADS_1_ADD1 |
949 ADR_CFG_CYCLES_ADD2(16) |
950 ADR_CFG_PADS_1_ADD2 |
951 ADR_CFG_CSDEASSERT_ADD2);
952}
953
Lee Jones08981272014-03-20 09:20:42 +0000954/* Search for preferred configuration based on available flags */
955static struct seq_rw_config *
956stfsm_search_seq_rw_configs(struct stfsm *fsm,
957 struct seq_rw_config cfgs[])
958{
959 struct seq_rw_config *config;
960 int flags = fsm->info->flags;
961
962 for (config = cfgs; config->cmd != 0; config++)
963 if ((config->flags & flags) == config->flags)
964 return config;
965
966 return NULL;
967}
968
Lee Jones97ccf2d2014-03-20 09:20:44 +0000969/* Prepare a READ/WRITE sequence according to configuration parameters */
970static void stfsm_prepare_rw_seq(struct stfsm *fsm,
971 struct stfsm_seq *seq,
972 struct seq_rw_config *cfg)
973{
974 int addr1_cycles, addr2_cycles;
975 int i = 0;
976
977 memset(seq, 0, sizeof(*seq));
978
979 /* Add READ/WRITE OPC */
980 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
981 SEQ_OPC_CYCLES(8) |
982 SEQ_OPC_OPCODE(cfg->cmd));
983
984 /* Add WREN OPC for a WRITE sequence */
985 if (cfg->write)
986 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
987 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -0700988 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
Lee Jones97ccf2d2014-03-20 09:20:44 +0000989 SEQ_OPC_CSDEASSERT);
990
991 /* Address configuration (24 or 32-bit addresses) */
992 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
993 addr1_cycles /= cfg->addr_pads;
994 addr2_cycles = 16 / cfg->addr_pads;
995 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
996 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
997 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
998 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
999
1000 /* Data/Sequence configuration */
1001 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1002 SEQ_CFG_STARTSEQ |
1003 SEQ_CFG_CSDEASSERT);
1004 if (!cfg->write)
1005 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1006
1007 /* Mode configuration (no. of pads taken from addr cfg) */
1008 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1009 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1010 (cfg->addr_pads - 1) << 22); /* pads */
1011
1012 /* Dummy configuration (no. of pads taken from addr cfg) */
1013 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1014 (cfg->addr_pads - 1) << 22); /* pads */
1015
1016
1017 /* Instruction sequence */
1018 i = 0;
1019 if (cfg->write)
1020 seq->seq[i++] = STFSM_INST_CMD2;
1021
1022 seq->seq[i++] = STFSM_INST_CMD1;
1023
1024 seq->seq[i++] = STFSM_INST_ADD1;
1025 seq->seq[i++] = STFSM_INST_ADD2;
1026
1027 if (cfg->mode_cycles)
1028 seq->seq[i++] = STFSM_INST_MODE;
1029
1030 if (cfg->dummy_cycles)
1031 seq->seq[i++] = STFSM_INST_DUMMY;
1032
1033 seq->seq[i++] =
1034 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1035 seq->seq[i++] = STFSM_INST_STOP;
1036}
1037
Lee Jones88cccb82014-03-20 09:20:49 +00001038static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1039 struct stfsm_seq *seq,
1040 struct seq_rw_config *cfgs)
1041{
1042 struct seq_rw_config *config;
1043
1044 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1045 if (!config) {
1046 dev_err(fsm->dev, "failed to find suitable config\n");
1047 return -EINVAL;
1048 }
1049
1050 stfsm_prepare_rw_seq(fsm, seq, config);
1051
1052 return 0;
1053}
1054
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001055/* Prepare a READ/WRITE/ERASE 'default' sequences */
1056static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1057{
1058 uint32_t flags = fsm->info->flags;
1059 int ret;
1060
1061 /* Configure 'READ' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001062 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001063 default_read_configs);
1064 if (ret) {
1065 dev_err(fsm->dev,
1066 "failed to prep READ sequence with flags [0x%08x]\n",
1067 flags);
1068 return ret;
1069 }
1070
1071 /* Configure 'WRITE' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001072 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001073 default_write_configs);
1074 if (ret) {
1075 dev_err(fsm->dev,
1076 "failed to prep WRITE sequence with flags [0x%08x]\n",
1077 flags);
1078 return ret;
1079 }
1080
1081 /* Configure 'ERASE_SECTOR' sequence */
1082 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1083
1084 return 0;
1085}
1086
Lee Jones898180662014-03-20 09:21:03 +00001087static int stfsm_mx25_config(struct stfsm *fsm)
1088{
1089 uint32_t flags = fsm->info->flags;
1090 uint32_t data_pads;
1091 uint8_t sta;
1092 int ret;
1093 bool soc_reset;
1094
1095 /*
1096 * Use default READ/WRITE sequences
1097 */
1098 ret = stfsm_prepare_rwe_seqs_default(fsm);
1099 if (ret)
1100 return ret;
1101
1102 /*
1103 * Configure 32-bit Address Support
1104 */
1105 if (flags & FLASH_FLAG_32BIT_ADDR) {
1106 /* Configure 'enter_32bitaddr' FSM sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001107 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones898180662014-03-20 09:21:03 +00001108
1109 soc_reset = stfsm_can_handle_soc_reset(fsm);
Angus Clark009e7e62014-03-26 16:39:16 +00001110 if (soc_reset || !fsm->booted_from_spi)
Lee Jones898180662014-03-20 09:21:03 +00001111 /* If we can handle SoC resets, we enable 32-bit address
1112 * mode pervasively */
1113 stfsm_enter_32bit_addr(fsm, 1);
1114
Angus Clark009e7e62014-03-26 16:39:16 +00001115 else
Lee Jones898180662014-03-20 09:21:03 +00001116 /* Else, enable/disable 32-bit addressing before/after
1117 * each operation */
1118 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1119 CFG_WRITE_TOGGLE_32BIT_ADDR |
1120 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
Lee Jones898180662014-03-20 09:21:03 +00001121 }
1122
Angus Clark5d0bdda2014-03-26 16:39:18 +00001123 /* Check status of 'QE' bit, update if required. */
Brian Norris92d3af92014-04-08 18:56:06 -07001124 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001125 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones898180662014-03-20 09:21:03 +00001126 if (data_pads == 4) {
Angus Clarkcc666862014-03-26 16:39:17 +00001127 if (!(sta & MX25_STATUS_QE)) {
1128 /* Set 'QE' */
1129 sta |= MX25_STATUS_QE;
1130
Brian Norris92d3af92014-04-08 18:56:06 -07001131 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001132 }
1133 } else {
1134 if (sta & MX25_STATUS_QE) {
1135 /* Clear 'QE' */
1136 sta &= ~MX25_STATUS_QE;
1137
Brian Norris92d3af92014-04-08 18:56:06 -07001138 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
Angus Clarkcc666862014-03-26 16:39:17 +00001139 }
Lee Jones898180662014-03-20 09:21:03 +00001140 }
1141
1142 return 0;
1143}
1144
Lee Jones218b8702014-03-20 09:20:55 +00001145static int stfsm_n25q_config(struct stfsm *fsm)
1146{
1147 uint32_t flags = fsm->info->flags;
1148 uint8_t vcr;
1149 int ret = 0;
1150 bool soc_reset;
1151
1152 /* Configure 'READ' sequence */
1153 if (flags & FLASH_FLAG_32BIT_ADDR)
Lee Jonese6b1bb42014-03-20 09:21:06 +00001154 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001155 n25q_read4_configs);
1156 else
Lee Jonese6b1bb42014-03-20 09:21:06 +00001157 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001158 n25q_read3_configs);
1159 if (ret) {
1160 dev_err(fsm->dev,
1161 "failed to prepare READ sequence with flags [0x%08x]\n",
1162 flags);
1163 return ret;
1164 }
1165
1166 /* Configure 'WRITE' sequence (default configs) */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001167 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones218b8702014-03-20 09:20:55 +00001168 default_write_configs);
1169 if (ret) {
1170 dev_err(fsm->dev,
1171 "preparing WRITE sequence using flags [0x%08x] failed\n",
1172 flags);
1173 return ret;
1174 }
1175
1176 /* * Configure 'ERASE_SECTOR' sequence */
1177 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1178
1179 /* Configure 32-bit address support */
1180 if (flags & FLASH_FLAG_32BIT_ADDR) {
Lee Jonese6b1bb42014-03-20 09:21:06 +00001181 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones218b8702014-03-20 09:20:55 +00001182
1183 soc_reset = stfsm_can_handle_soc_reset(fsm);
1184 if (soc_reset || !fsm->booted_from_spi) {
1185 /*
1186 * If we can handle SoC resets, we enable 32-bit
1187 * address mode pervasively
1188 */
1189 stfsm_enter_32bit_addr(fsm, 1);
1190 } else {
1191 /*
1192 * If not, enable/disable for WRITE and ERASE
1193 * operations (READ uses special commands)
1194 */
1195 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1196 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1197 }
1198 }
1199
1200 /*
1201 * Configure device to use 8 dummy cycles
1202 */
1203 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1204 N25Q_VCR_WRAP_CONT);
Angus Clark5d0bdda2014-03-26 16:39:18 +00001205 stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
Lee Jones218b8702014-03-20 09:20:55 +00001206
1207 return 0;
1208}
1209
Lee Jones5343a122014-03-20 09:21:04 +00001210static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1211{
1212 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1213 SEQ_OPC_CYCLES(8) |
1214 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1215
1216 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1217 ADR_CFG_PADS_1_ADD1 |
1218 ADR_CFG_CYCLES_ADD2(16) |
1219 ADR_CFG_PADS_1_ADD2 |
1220 ADR_CFG_CSDEASSERT_ADD2);
1221}
1222
1223static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1224{
1225 uint32_t tmp;
1226 struct stfsm_seq seq = {
1227 .data_size = TRANSFER_SIZE(4),
1228 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1229 SEQ_OPC_CYCLES(8) |
1230 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1231 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1232 ADR_CFG_PADS_1_ADD1 |
1233 ADR_CFG_CYCLES_ADD2(16) |
1234 ADR_CFG_PADS_1_ADD2),
1235 .addr1 = (offs >> 16) & 0xffff,
1236 .addr2 = offs & 0xffff,
1237 .seq = {
1238 STFSM_INST_CMD1,
1239 STFSM_INST_ADD1,
1240 STFSM_INST_ADD2,
1241 STFSM_INST_DATA_READ,
1242 STFSM_INST_STOP,
1243 },
1244 .seq_cfg = (SEQ_CFG_PADS_1 |
1245 SEQ_CFG_READNOTWRITE |
1246 SEQ_CFG_CSDEASSERT |
1247 SEQ_CFG_STARTSEQ),
1248 };
1249
1250 stfsm_load_seq(fsm, &seq);
1251
1252 stfsm_read_fifo(fsm, &tmp, 4);
1253
1254 *dby = (uint8_t)(tmp >> 24);
1255
1256 stfsm_wait_seq(fsm);
1257}
1258
1259static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1260{
1261 struct stfsm_seq seq = {
1262 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -07001263 SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
Lee Jones5343a122014-03-20 09:21:04 +00001264 SEQ_OPC_CSDEASSERT),
1265 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1266 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
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 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1272 .addr1 = (offs >> 16) & 0xffff,
1273 .addr2 = offs & 0xffff,
1274 .seq = {
1275 STFSM_INST_CMD1,
1276 STFSM_INST_CMD2,
1277 STFSM_INST_ADD1,
1278 STFSM_INST_ADD2,
1279 STFSM_INST_STA_WR1,
1280 STFSM_INST_STOP,
1281 },
1282 .seq_cfg = (SEQ_CFG_PADS_1 |
1283 SEQ_CFG_READNOTWRITE |
1284 SEQ_CFG_CSDEASSERT |
1285 SEQ_CFG_STARTSEQ),
1286 };
1287
1288 stfsm_load_seq(fsm, &seq);
1289 stfsm_wait_seq(fsm);
1290
1291 stfsm_wait_busy(fsm);
1292}
1293
1294static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1295{
1296 struct stfsm_seq seq = {
1297 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1298 SEQ_OPC_CYCLES(8) |
1299 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1300 SEQ_OPC_CSDEASSERT),
1301 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1302 SEQ_OPC_CYCLES(8) |
Brian Norris92d3af92014-04-08 18:56:06 -07001303 SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
Lee Jones5343a122014-03-20 09:21:04 +00001304 SEQ_OPC_CSDEASSERT),
1305 .seq = {
1306 STFSM_INST_CMD1,
1307 STFSM_INST_CMD2,
1308 STFSM_INST_WAIT,
1309 STFSM_INST_STOP,
1310 },
1311 .seq_cfg = (SEQ_CFG_PADS_1 |
1312 SEQ_CFG_ERASE |
1313 SEQ_CFG_READNOTWRITE |
1314 SEQ_CFG_CSDEASSERT |
1315 SEQ_CFG_STARTSEQ),
1316 };
1317
1318 stfsm_load_seq(fsm, &seq);
1319
1320 stfsm_wait_seq(fsm);
1321
1322 return 0;
1323}
1324
1325static int stfsm_s25fl_config(struct stfsm *fsm)
1326{
1327 struct flash_info *info = fsm->info;
1328 uint32_t flags = info->flags;
1329 uint32_t data_pads;
1330 uint32_t offs;
1331 uint16_t sta_wr;
1332 uint8_t sr1, cr1, dyb;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001333 int update_sr = 0;
Lee Jones5343a122014-03-20 09:21:04 +00001334 int ret;
1335
1336 if (flags & FLASH_FLAG_32BIT_ADDR) {
1337 /*
1338 * Prepare Read/Write/Erase sequences according to S25FLxxx
1339 * 32-bit address command set
1340 */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001341 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones5343a122014-03-20 09:21:04 +00001342 stfsm_s25fl_read4_configs);
1343 if (ret)
1344 return ret;
1345
Lee Jonese6b1bb42014-03-20 09:21:06 +00001346 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones5343a122014-03-20 09:21:04 +00001347 stfsm_s25fl_write4_configs);
1348 if (ret)
1349 return ret;
1350
1351 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1352
1353 } else {
1354 /* Use default configurations for 24-bit addressing */
1355 ret = stfsm_prepare_rwe_seqs_default(fsm);
1356 if (ret)
1357 return ret;
1358 }
1359
1360 /*
1361 * For devices that support 'DYB' sector locking, check lock status and
1362 * unlock sectors if necessary (some variants power-on with sectors
1363 * locked by default)
1364 */
1365 if (flags & FLASH_FLAG_DYB_LOCKING) {
1366 offs = 0;
1367 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1368 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1369 if (dyb == 0x00)
1370 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1371
1372 /* Handle bottom/top 4KiB parameter sectors */
1373 if ((offs < info->sector_size * 2) ||
1374 (offs >= (info->sector_size - info->n_sectors * 4)))
1375 offs += 0x1000;
1376 else
1377 offs += 0x10000;
1378 }
1379 }
1380
Angus Clark5d0bdda2014-03-26 16:39:18 +00001381 /* Check status of 'QE' bit, update if required. */
Brian Norris92d3af92014-04-08 18:56:06 -07001382 stfsm_read_status(fsm, SPINOR_OP_RDSR2, &cr1, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001383 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones5343a122014-03-20 09:21:04 +00001384 if (data_pads == 4) {
1385 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1386 /* Set 'QE' */
1387 cr1 |= STFSM_S25FL_CONFIG_QE;
1388
Angus Clark5d0bdda2014-03-26 16:39:18 +00001389 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001390 }
1391 } else {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001392 if (cr1 & STFSM_S25FL_CONFIG_QE) {
Lee Jones5343a122014-03-20 09:21:04 +00001393 /* Clear 'QE' */
1394 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1395
Angus Clark5d0bdda2014-03-26 16:39:18 +00001396 update_sr = 1;
Lee Jones5343a122014-03-20 09:21:04 +00001397 }
Angus Clark5d0bdda2014-03-26 16:39:18 +00001398 }
1399 if (update_sr) {
Brian Norris92d3af92014-04-08 18:56:06 -07001400 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
Angus Clark5d0bdda2014-03-26 16:39:18 +00001401 sta_wr = ((uint16_t)cr1 << 8) | sr1;
Brian Norris92d3af92014-04-08 18:56:06 -07001402 stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
Lee Jones5343a122014-03-20 09:21:04 +00001403 }
1404
1405 /*
1406 * S25FLxxx devices support Program and Error error flags.
1407 * Configure driver to check flags and clear if necessary.
1408 */
1409 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1410
1411 return 0;
1412}
1413
Lee Jonescd7cac92014-03-20 09:21:05 +00001414static int stfsm_w25q_config(struct stfsm *fsm)
1415{
1416 uint32_t data_pads;
Angus Clark5d0bdda2014-03-26 16:39:18 +00001417 uint8_t sr1, sr2;
1418 uint16_t sr_wr;
1419 int update_sr = 0;
Lee Jonescd7cac92014-03-20 09:21:05 +00001420 int ret;
1421
1422 ret = stfsm_prepare_rwe_seqs_default(fsm);
1423 if (ret)
1424 return ret;
1425
Angus Clark5d0bdda2014-03-26 16:39:18 +00001426 /* Check status of 'QE' bit, update if required. */
Brian Norris92d3af92014-04-08 18:56:06 -07001427 stfsm_read_status(fsm, SPINOR_OP_RDSR2, &sr2, 1);
Lee Jonese6b1bb42014-03-20 09:21:06 +00001428 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jonescd7cac92014-03-20 09:21:05 +00001429 if (data_pads == 4) {
Angus Clark5d0bdda2014-03-26 16:39:18 +00001430 if (!(sr2 & W25Q_STATUS_QE)) {
1431 /* Set 'QE' */
1432 sr2 |= W25Q_STATUS_QE;
1433 update_sr = 1;
1434 }
1435 } else {
1436 if (sr2 & W25Q_STATUS_QE) {
1437 /* Clear 'QE' */
1438 sr2 &= ~W25Q_STATUS_QE;
1439 update_sr = 1;
1440 }
1441 }
1442 if (update_sr) {
1443 /* Write status register */
Brian Norris92d3af92014-04-08 18:56:06 -07001444 stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
Angus Clark5d0bdda2014-03-26 16:39:18 +00001445 sr_wr = ((uint16_t)sr2 << 8) | sr1;
Brian Norris92d3af92014-04-08 18:56:06 -07001446 stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
Lee Jonescd7cac92014-03-20 09:21:05 +00001447 }
1448
1449 return 0;
1450}
1451
Lee Jonese514f102014-03-20 09:20:57 +00001452static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1453 uint32_t offset)
1454{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001455 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
Lee Jonese514f102014-03-20 09:20:57 +00001456 uint32_t data_pads;
1457 uint32_t read_mask;
1458 uint32_t size_ub;
1459 uint32_t size_lb;
1460 uint32_t size_mop;
1461 uint32_t tmp[4];
1462 uint32_t page_buf[FLASH_PAGESIZE_32];
1463 uint8_t *p;
1464
1465 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1466
1467 /* Enter 32-bit address mode, if required */
1468 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1469 stfsm_enter_32bit_addr(fsm, 1);
1470
1471 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1472 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1473 read_mask = (data_pads << 2) - 1;
1474
1475 /* Handle non-aligned buf */
1476 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1477
1478 /* Handle non-aligned size */
1479 size_ub = (size + read_mask) & ~read_mask;
1480 size_lb = size & ~read_mask;
1481 size_mop = size & read_mask;
1482
1483 seq->data_size = TRANSFER_SIZE(size_ub);
1484 seq->addr1 = (offset >> 16) & 0xffff;
1485 seq->addr2 = offset & 0xffff;
1486
1487 stfsm_load_seq(fsm, seq);
1488
1489 if (size_lb)
1490 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1491
1492 if (size_mop) {
1493 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1494 memcpy(p + size_lb, &tmp, size_mop);
1495 }
1496
1497 /* Handle non-aligned buf */
1498 if ((uint32_t)buf & 0x3)
1499 memcpy(buf, page_buf, size);
1500
1501 /* Wait for sequence to finish */
1502 stfsm_wait_seq(fsm);
1503
1504 stfsm_clear_fifo(fsm);
1505
1506 /* Exit 32-bit address mode, if required */
1507 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1508 stfsm_enter_32bit_addr(fsm, 0);
1509
1510 return 0;
1511}
1512
Lee Jones3f9d7202014-03-20 11:11:43 +00001513static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1514 uint32_t size, uint32_t offset)
Lee Jones176b4372014-03-20 09:20:59 +00001515{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001516 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
Lee Jones176b4372014-03-20 09:20:59 +00001517 uint32_t data_pads;
1518 uint32_t write_mask;
1519 uint32_t size_ub;
1520 uint32_t size_lb;
1521 uint32_t size_mop;
1522 uint32_t tmp[4];
1523 uint32_t page_buf[FLASH_PAGESIZE_32];
1524 uint8_t *t = (uint8_t *)&tmp;
1525 const uint8_t *p;
1526 int ret;
1527 int i;
1528
1529 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1530
1531 /* Enter 32-bit address mode, if required */
1532 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1533 stfsm_enter_32bit_addr(fsm, 1);
1534
1535 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1536 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1537 write_mask = (data_pads << 2) - 1;
1538
1539 /* Handle non-aligned buf */
1540 if ((uint32_t)buf & 0x3) {
1541 memcpy(page_buf, buf, size);
1542 p = (uint8_t *)page_buf;
1543 } else {
1544 p = buf;
1545 }
1546
1547 /* Handle non-aligned size */
1548 size_ub = (size + write_mask) & ~write_mask;
1549 size_lb = size & ~write_mask;
1550 size_mop = size & write_mask;
1551
1552 seq->data_size = TRANSFER_SIZE(size_ub);
1553 seq->addr1 = (offset >> 16) & 0xffff;
1554 seq->addr2 = offset & 0xffff;
1555
1556 /* Need to set FIFO to write mode, before writing data to FIFO (see
1557 * GNBvb79594)
1558 */
1559 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1560
1561 /*
1562 * Before writing data to the FIFO, apply a small delay to allow a
1563 * potential change of FIFO direction to complete.
1564 */
1565 if (fsm->fifo_dir_delay == 0)
1566 readl(fsm->base + SPI_FAST_SEQ_CFG);
1567 else
1568 udelay(fsm->fifo_dir_delay);
1569
1570
1571 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1572 if (size_lb) {
1573 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1574 p += size_lb;
1575 }
1576
1577 /* Handle non-aligned size */
1578 if (size_mop) {
1579 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1580 for (i = 0; i < size_mop; i++)
1581 t[i] = *p++;
1582
1583 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1584 }
1585
1586 /* Start sequence */
1587 stfsm_load_seq(fsm, seq);
1588
1589 /* Wait for sequence to finish */
1590 stfsm_wait_seq(fsm);
1591
1592 /* Wait for completion */
1593 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001594 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1595 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones176b4372014-03-20 09:20:59 +00001596
1597 /* Exit 32-bit address mode, if required */
Angus Clark009e7e62014-03-26 16:39:16 +00001598 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
Lee Jones176b4372014-03-20 09:20:59 +00001599 stfsm_enter_32bit_addr(fsm, 0);
Lee Jones176b4372014-03-20 09:20:59 +00001600
1601 return 0;
1602}
1603
Lee Jonese514f102014-03-20 09:20:57 +00001604/*
1605 * Read an address range from the flash chip. The address range
1606 * may be any size provided it is within the physical boundaries.
1607 */
1608static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1609 size_t *retlen, u_char *buf)
1610{
1611 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1612 uint32_t bytes;
1613
1614 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1615 __func__, (u32)from, len);
1616
1617 mutex_lock(&fsm->lock);
1618
1619 while (len > 0) {
1620 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1621
1622 stfsm_read(fsm, buf, bytes, from);
1623
1624 buf += bytes;
1625 from += bytes;
1626 len -= bytes;
1627
1628 *retlen += bytes;
1629 }
1630
1631 mutex_unlock(&fsm->lock);
1632
1633 return 0;
1634}
1635
Lee Jones3f9d7202014-03-20 11:11:43 +00001636static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
Lee Jones4a341fe2014-03-20 09:21:00 +00001637{
1638 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1639 int ret;
1640
1641 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1642
1643 /* Enter 32-bit address mode, if required */
1644 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1645 stfsm_enter_32bit_addr(fsm, 1);
1646
1647 seq->addr1 = (offset >> 16) & 0xffff;
1648 seq->addr2 = offset & 0xffff;
1649
1650 stfsm_load_seq(fsm, seq);
1651
1652 stfsm_wait_seq(fsm);
1653
1654 /* Wait for completion */
1655 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001656 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1657 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones4a341fe2014-03-20 09:21:00 +00001658
1659 /* Exit 32-bit address mode, if required */
1660 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1661 stfsm_enter_32bit_addr(fsm, 0);
1662
1663 return ret;
1664}
1665
1666static int stfsm_erase_chip(struct stfsm *fsm)
1667{
1668 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1669
1670 dev_dbg(fsm->dev, "erasing chip\n");
1671
1672 stfsm_load_seq(fsm, seq);
1673
1674 stfsm_wait_seq(fsm);
1675
1676 return stfsm_wait_busy(fsm);
1677}
1678
Lee Jones176b4372014-03-20 09:20:59 +00001679/*
1680 * Write an address range to the flash chip. Data must be written in
1681 * FLASH_PAGESIZE chunks. The address range may be any size provided
1682 * it is within the physical boundaries.
1683 */
1684static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1685 size_t *retlen, const u_char *buf)
1686{
1687 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1688
1689 u32 page_offs;
1690 u32 bytes;
1691 uint8_t *b = (uint8_t *)buf;
1692 int ret = 0;
1693
1694 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1695
Lee Jones176b4372014-03-20 09:20:59 +00001696 /* Offset within page */
1697 page_offs = to % FLASH_PAGESIZE;
1698
1699 mutex_lock(&fsm->lock);
1700
1701 while (len) {
1702 /* Write up to page boundary */
1703 bytes = min(FLASH_PAGESIZE - page_offs, len);
1704
1705 ret = stfsm_write(fsm, b, bytes, to);
1706 if (ret)
1707 goto out1;
1708
1709 b += bytes;
1710 len -= bytes;
1711 to += bytes;
1712
1713 /* We are now page-aligned */
1714 page_offs = 0;
1715
1716 *retlen += bytes;
1717
1718 }
1719
1720out1:
1721 mutex_unlock(&fsm->lock);
1722
1723 return ret;
1724}
1725
Lee Jones4a341fe2014-03-20 09:21:00 +00001726/*
1727 * Erase an address range on the flash chip. The address range may extend
1728 * one or more erase sectors. Return an error is there is a problem erasing.
1729 */
1730static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1731{
1732 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1733 u32 addr, len;
1734 int ret;
1735
1736 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1737 (long long)instr->addr, (long long)instr->len);
1738
1739 addr = instr->addr;
1740 len = instr->len;
1741
1742 mutex_lock(&fsm->lock);
1743
1744 /* Whole-chip erase? */
1745 if (len == mtd->size) {
1746 ret = stfsm_erase_chip(fsm);
1747 if (ret)
1748 goto out1;
1749 } else {
1750 while (len) {
1751 ret = stfsm_erase_sector(fsm, addr);
1752 if (ret)
1753 goto out1;
1754
1755 addr += mtd->erasesize;
1756 len -= mtd->erasesize;
1757 }
1758 }
1759
1760 mutex_unlock(&fsm->lock);
1761
1762 instr->state = MTD_ERASE_DONE;
1763 mtd_erase_callback(instr);
1764
1765 return 0;
1766
1767out1:
1768 instr->state = MTD_ERASE_FAILED;
1769 mutex_unlock(&fsm->lock);
1770
1771 return ret;
1772}
1773
Lee Jones3f9d7202014-03-20 11:11:43 +00001774static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
Lee Jones1bd512b2014-03-20 09:20:38 +00001775{
1776 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1777 uint32_t tmp[2];
1778
1779 stfsm_load_seq(fsm, seq);
1780
1781 stfsm_read_fifo(fsm, tmp, 8);
1782
1783 memcpy(jedec, tmp, 5);
1784
1785 stfsm_wait_seq(fsm);
1786}
1787
1788static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1789{
Lee Jones24fec652014-03-20 09:20:41 +00001790 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001791 u16 ext_jedec;
1792 u32 jedec;
1793 u8 id[5];
1794
1795 stfsm_read_jedec(fsm, id);
1796
1797 jedec = id[0] << 16 | id[1] << 8 | id[2];
1798 /*
1799 * JEDEC also defines an optional "extended device information"
1800 * string for after vendor-specific data, after the three bytes
1801 * we use here. Supporting some chips might require using it.
1802 */
1803 ext_jedec = id[3] << 8 | id[4];
1804
1805 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1806 jedec, id[0], id[1], id[2], id[3], id[4]);
1807
Lee Jones24fec652014-03-20 09:20:41 +00001808 for (info = flash_types; info->name; info++) {
1809 if (info->jedec_id == jedec) {
1810 if (info->ext_id && info->ext_id != ext_jedec)
1811 continue;
1812 return info;
1813 }
1814 }
1815 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1816
Lee Jones1bd512b2014-03-20 09:20:38 +00001817 return NULL;
1818}
1819
Lee Jones86f309fd2014-03-20 09:20:35 +00001820static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1821{
1822 int ret, timeout = 10;
1823
1824 /* Wait for controller to accept mode change */
1825 while (--timeout) {
1826 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1827 if (ret & 0x1)
1828 break;
1829 udelay(1);
1830 }
1831
1832 if (!timeout)
1833 return -EBUSY;
1834
1835 writel(mode, fsm->base + SPI_MODESELECT);
1836
1837 return 0;
1838}
1839
1840static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1841{
1842 uint32_t emi_freq;
1843 uint32_t clk_div;
1844
1845 /* TODO: Make this dynamic */
1846 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1847
1848 /*
1849 * Calculate clk_div - values between 2 and 128
1850 * Multiple of 2, rounded up
1851 */
1852 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1853 if (clk_div < 2)
1854 clk_div = 2;
1855 else if (clk_div > 128)
1856 clk_div = 128;
1857
1858 /*
1859 * Determine a suitable delay for the IP to complete a change of
1860 * direction of the FIFO. The required delay is related to the clock
1861 * divider used. The following heuristics are based on empirical tests,
1862 * using a 100MHz EMI clock.
1863 */
1864 if (clk_div <= 4)
1865 fsm->fifo_dir_delay = 0;
1866 else if (clk_div <= 10)
1867 fsm->fifo_dir_delay = 1;
1868 else
1869 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1870
1871 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1872 emi_freq, spi_freq, clk_div);
1873
1874 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1875}
1876
1877static int stfsm_init(struct stfsm *fsm)
1878{
1879 int ret;
1880
1881 /* Perform a soft reset of the FSM controller */
1882 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1883 udelay(1);
1884 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1885
1886 /* Set clock to 'safe' frequency initially */
1887 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1888
1889 /* Switch to FSM */
1890 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1891 if (ret)
1892 return ret;
1893
1894 /* Set timing parameters */
1895 writel(SPI_CFG_DEVICE_ST |
1896 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1897 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1898 SPI_CFG_DEFAULT_DATA_HOLD,
1899 fsm->base + SPI_CONFIGDATA);
1900 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1901
Angus Clark009e7e62014-03-26 16:39:16 +00001902 /*
1903 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1904 * our purposes, the WAIT instruction is used purely to achieve
1905 * "sequence validity" rather than actually implement a delay.
1906 */
1907 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1908
Lee Jones86f309fd2014-03-20 09:20:35 +00001909 /* Clear FIFO, just in case */
1910 stfsm_clear_fifo(fsm);
1911
1912 return 0;
1913}
1914
Lee Jonesa63984c2014-03-20 09:20:46 +00001915static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1916{
1917 struct stfsm *fsm = platform_get_drvdata(pdev);
1918 struct device_node *np = pdev->dev.of_node;
1919 struct regmap *regmap;
1920 uint32_t boot_device_reg;
1921 uint32_t boot_device_spi;
1922 uint32_t boot_device; /* Value we read from *boot_device_reg */
1923 int ret;
1924
1925 /* Booting from SPI NOR Flash is the default */
1926 fsm->booted_from_spi = true;
1927
1928 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1929 if (IS_ERR(regmap))
1930 goto boot_device_fail;
1931
Lee Jones0ea7d702014-03-20 09:20:50 +00001932 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1933
1934 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1935
Lee Jonesa63984c2014-03-20 09:20:46 +00001936 /* Where in the syscon the boot device information lives */
1937 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1938 if (ret)
1939 goto boot_device_fail;
1940
1941 /* Boot device value when booted from SPI NOR */
1942 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1943 if (ret)
1944 goto boot_device_fail;
1945
1946 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1947 if (ret)
1948 goto boot_device_fail;
1949
1950 if (boot_device != boot_device_spi)
1951 fsm->booted_from_spi = false;
1952
1953 return;
1954
1955boot_device_fail:
1956 dev_warn(&pdev->dev,
1957 "failed to fetch boot device, assuming boot from SPI\n");
1958}
1959
Lee Jonesd90db4a2014-03-20 09:20:33 +00001960static int stfsm_probe(struct platform_device *pdev)
1961{
1962 struct device_node *np = pdev->dev.of_node;
Lee Jones221cff12014-03-20 09:21:07 +00001963 struct mtd_part_parser_data ppdata;
Lee Jones24fec652014-03-20 09:20:41 +00001964 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001965 struct resource *res;
1966 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00001967 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001968
1969 if (!np) {
1970 dev_err(&pdev->dev, "No DT found\n");
1971 return -EINVAL;
1972 }
Lee Jones221cff12014-03-20 09:21:07 +00001973 ppdata.of_node = np;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001974
1975 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1976 if (!fsm)
1977 return -ENOMEM;
1978
1979 fsm->dev = &pdev->dev;
1980
1981 platform_set_drvdata(pdev, fsm);
1982
1983 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1984 if (!res) {
1985 dev_err(&pdev->dev, "Resource not found\n");
1986 return -ENODEV;
1987 }
1988
1989 fsm->base = devm_ioremap_resource(&pdev->dev, res);
1990 if (IS_ERR(fsm->base)) {
1991 dev_err(&pdev->dev,
1992 "Failed to reserve memory region %pR\n", res);
1993 return PTR_ERR(fsm->base);
1994 }
1995
1996 mutex_init(&fsm->lock);
1997
Lee Jones86f309fd2014-03-20 09:20:35 +00001998 ret = stfsm_init(fsm);
1999 if (ret) {
2000 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2001 return ret;
2002 }
2003
Lee Jonesa63984c2014-03-20 09:20:46 +00002004 stfsm_fetch_platform_configs(pdev);
2005
Lee Jones1bd512b2014-03-20 09:20:38 +00002006 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00002007 info = stfsm_jedec_probe(fsm);
2008 if (!info)
2009 return -ENODEV;
2010 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00002011
Lee Jones3b5d1982014-03-20 09:20:43 +00002012 /* Use device size to determine address width */
2013 if (info->sector_size * info->n_sectors > 0x1000000)
2014 info->flags |= FLASH_FLAG_32BIT_ADDR;
2015
Lee Jones218b8702014-03-20 09:20:55 +00002016 /*
2017 * Configure READ/WRITE/ERASE sequences according to platform and
2018 * device flags.
2019 */
2020 if (info->config) {
2021 ret = info->config(fsm);
2022 if (ret)
2023 return ret;
Lee Jones4eb3f0d82014-03-20 09:20:56 +00002024 } else {
2025 ret = stfsm_prepare_rwe_seqs_default(fsm);
2026 if (ret)
2027 return ret;
Lee Jones218b8702014-03-20 09:20:55 +00002028 }
2029
Lee Jones221cff12014-03-20 09:21:07 +00002030 fsm->mtd.name = info->name;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002031 fsm->mtd.dev.parent = &pdev->dev;
2032 fsm->mtd.type = MTD_NORFLASH;
2033 fsm->mtd.writesize = 4;
2034 fsm->mtd.writebufsize = fsm->mtd.writesize;
2035 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00002036 fsm->mtd.size = info->sector_size * info->n_sectors;
2037 fsm->mtd.erasesize = info->sector_size;
2038
Lee Jonese514f102014-03-20 09:20:57 +00002039 fsm->mtd._read = stfsm_mtd_read;
Lee Jones176b4372014-03-20 09:20:59 +00002040 fsm->mtd._write = stfsm_mtd_write;
Lee Jones4a341fe2014-03-20 09:21:00 +00002041 fsm->mtd._erase = stfsm_mtd_erase;
Lee Jonese514f102014-03-20 09:20:57 +00002042
Lee Jones4a341fe2014-03-20 09:21:00 +00002043 dev_info(&pdev->dev,
Lee Jones24fec652014-03-20 09:20:41 +00002044 "Found serial flash device: %s\n"
2045 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2046 info->name,
2047 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2048 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00002049
Lee Jones221cff12014-03-20 09:21:07 +00002050 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002051}
2052
2053static int stfsm_remove(struct platform_device *pdev)
2054{
2055 struct stfsm *fsm = platform_get_drvdata(pdev);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002056
Lee Jonesd9ba46d2014-03-20 11:11:47 +00002057 return mtd_device_unregister(&fsm->mtd);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002058}
2059
2060static struct of_device_id stfsm_match[] = {
2061 { .compatible = "st,spi-fsm", },
2062 {},
2063};
2064MODULE_DEVICE_TABLE(of, stfsm_match);
2065
2066static struct platform_driver stfsm_driver = {
2067 .probe = stfsm_probe,
2068 .remove = stfsm_remove,
2069 .driver = {
2070 .name = "st-spi-fsm",
2071 .owner = THIS_MODULE,
2072 .of_match_table = stfsm_match,
2073 },
2074};
2075module_platform_driver(stfsm_driver);
2076
2077MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2078MODULE_DESCRIPTION("ST SPI FSM driver");
2079MODULE_LICENSE("GPL");