blob: 40eb830eea21f37c94aae565f531b71dbfd04ae5 [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
215#define FLASH_CMD_WRVCR 0x81
216#define FLASH_CMD_RDVCR 0x85
217
218#define FLASH_CMD_READ 0x03 /* READ */
219#define FLASH_CMD_READ_FAST 0x0b /* FAST READ */
220#define FLASH_CMD_READ_1_1_2 0x3b /* DUAL OUTPUT READ */
221#define FLASH_CMD_READ_1_2_2 0xbb /* DUAL I/O READ */
222#define FLASH_CMD_READ_1_1_4 0x6b /* QUAD OUTPUT READ */
223#define FLASH_CMD_READ_1_4_4 0xeb /* QUAD I/O READ */
224
225#define FLASH_CMD_WRITE 0x02 /* PAGE PROGRAM */
226#define FLASH_CMD_WRITE_1_1_2 0xa2 /* DUAL INPUT PROGRAM */
227#define FLASH_CMD_WRITE_1_2_2 0xd2 /* DUAL INPUT EXT PROGRAM */
228#define FLASH_CMD_WRITE_1_1_4 0x32 /* QUAD INPUT PROGRAM */
229#define FLASH_CMD_WRITE_1_4_4 0x12 /* QUAD INPUT EXT PROGRAM */
230
231#define FLASH_CMD_EN4B_ADDR 0xb7 /* Enter 4-byte address mode */
232#define FLASH_CMD_EX4B_ADDR 0xe9 /* Exit 4-byte address mode */
233
234/* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
235#define FLASH_CMD_READ4 0x13
236#define FLASH_CMD_READ4_FAST 0x0c
237#define FLASH_CMD_READ4_1_1_2 0x3c
238#define FLASH_CMD_READ4_1_2_2 0xbc
239#define FLASH_CMD_READ4_1_1_4 0x6c
240#define FLASH_CMD_READ4_1_4_4 0xec
241
Lee Jones5343a122014-03-20 09:21:04 +0000242/* S25FLxxxS commands */
243#define S25FL_CMD_WRITE4_1_1_4 0x34
244#define S25FL_CMD_SE4 0xdc
245#define S25FL_CMD_CLSR 0x30
246#define S25FL_CMD_DYBWR 0xe1
247#define S25FL_CMD_DYBRD 0xe0
248#define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
249 * 'FLASH_CMD_WRITE_1_4_4'
250 * as found on N25Qxxx devices! */
251
Lee Jones176b4372014-03-20 09:20:59 +0000252/* Status register */
253#define FLASH_STATUS_BUSY 0x01
254#define FLASH_STATUS_WEL 0x02
255#define FLASH_STATUS_BP0 0x04
256#define FLASH_STATUS_BP1 0x08
257#define FLASH_STATUS_BP2 0x10
258#define FLASH_STATUS_SRWP0 0x80
259#define FLASH_STATUS_TIMEOUT 0xff
Lee Jones5343a122014-03-20 09:21:04 +0000260/* S25FL Error Flags */
261#define S25FL_STATUS_E_ERR 0x20
262#define S25FL_STATUS_P_ERR 0x40
Lee Jones176b4372014-03-20 09:20:59 +0000263
Lee Jonese514f102014-03-20 09:20:57 +0000264#define FLASH_PAGESIZE 256 /* In Bytes */
265#define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
Lee Jones176b4372014-03-20 09:20:59 +0000266#define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
Lee Jonese514f102014-03-20 09:20:57 +0000267
Lee Jonese85a6192014-03-20 09:20:54 +0000268/*
269 * Flags to tweak operation of default read/write/erase routines
270 */
271#define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
272#define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
Lee Jonese85a6192014-03-20 09:20:54 +0000273#define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
274#define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
275
Lee Jonese6b1bb42014-03-20 09:21:06 +0000276struct stfsm_seq {
277 uint32_t data_size;
278 uint32_t addr1;
279 uint32_t addr2;
280 uint32_t addr_cfg;
281 uint32_t seq_opc[5];
282 uint32_t mode;
283 uint32_t dummy;
284 uint32_t status;
285 uint8_t seq[16];
286 uint32_t seq_cfg;
287} __packed __aligned(4);
288
Lee Jonesd90db4a2014-03-20 09:20:33 +0000289struct stfsm {
290 struct device *dev;
291 void __iomem *base;
292 struct resource *region;
293 struct mtd_info mtd;
294 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000295 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000296
Lee Jonese85a6192014-03-20 09:20:54 +0000297 uint32_t configuration;
Lee Jones86f309fd2014-03-20 09:20:35 +0000298 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000299 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000300 bool reset_signal;
301 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000302
Lee Jonese6b1bb42014-03-20 09:21:06 +0000303 struct stfsm_seq stfsm_seq_read;
304 struct stfsm_seq stfsm_seq_write;
305 struct stfsm_seq stfsm_seq_en_32bit_addr;
306};
Lee Jones3c8b85b2014-03-20 09:20:36 +0000307
Lee Jones08981272014-03-20 09:20:42 +0000308/* Parameters to configure a READ or WRITE FSM sequence */
309struct seq_rw_config {
310 uint32_t flags; /* flags to support config */
311 uint8_t cmd; /* FLASH command */
312 int write; /* Write Sequence */
313 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
314 uint8_t data_pads; /* No. of data pads */
315 uint8_t mode_data; /* MODE data */
316 uint8_t mode_cycles; /* No. of MODE cycles */
317 uint8_t dummy_cycles; /* No. of DUMMY cycles */
318};
319
Lee Jones11d7f822014-03-20 09:20:40 +0000320/* SPI Flash Device Table */
321struct flash_info {
322 char *name;
323 /*
324 * JEDEC id zero means "no ID" (most older chips); otherwise it has
325 * a high byte of zero plus three data bytes: the manufacturer id,
326 * then a two byte device id.
327 */
328 u32 jedec_id;
329 u16 ext_id;
330 /*
331 * The size listed here is what works with FLASH_CMD_SE, which isn't
332 * necessarily called a "sector" by the vendor.
333 */
334 unsigned sector_size;
335 u16 n_sectors;
336 u32 flags;
337 /*
338 * Note, where FAST_READ is supported, freq_max specifies the
339 * FAST_READ frequency, not the READ frequency.
340 */
341 u32 max_freq;
342 int (*config)(struct stfsm *);
343};
344
Lee Jones218b8702014-03-20 09:20:55 +0000345static int stfsm_n25q_config(struct stfsm *fsm);
Lee Jones898180662014-03-20 09:21:03 +0000346static int stfsm_mx25_config(struct stfsm *fsm);
Lee Jones5343a122014-03-20 09:21:04 +0000347static int stfsm_s25fl_config(struct stfsm *fsm);
Lee Jonescd7cac92014-03-20 09:21:05 +0000348static int stfsm_w25q_config(struct stfsm *fsm);
Lee Jones218b8702014-03-20 09:20:55 +0000349
Lee Jones11d7f822014-03-20 09:20:40 +0000350static struct flash_info flash_types[] = {
351 /*
352 * ST Microelectronics/Numonyx --
353 * (newer production versions may have feature updates
354 * (eg faster operating frequency)
355 */
356#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
357 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
358 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
359 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
360 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
361 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
362 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
363
364#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
365 FLASH_FLAG_READ_FAST | \
366 FLASH_FLAG_READ_1_1_2 | \
367 FLASH_FLAG_WRITE_1_1_2)
368 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
369 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
370
371#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
372 FLASH_FLAG_READ_FAST | \
373 FLASH_FLAG_READ_1_1_2 | \
374 FLASH_FLAG_READ_1_2_2 | \
375 FLASH_FLAG_READ_1_1_4 | \
376 FLASH_FLAG_READ_1_4_4 | \
377 FLASH_FLAG_SE_4K | \
378 FLASH_FLAG_SE_32K)
379 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
Lee Jones898180662014-03-20 09:21:03 +0000380 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
381 stfsm_mx25_config },
Angus Clark5fa98062014-03-26 16:39:15 +0000382 { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
383 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
384 stfsm_mx25_config},
Lee Jones11d7f822014-03-20 09:20:40 +0000385
386#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
387 FLASH_FLAG_READ_FAST | \
388 FLASH_FLAG_READ_1_1_2 | \
389 FLASH_FLAG_READ_1_2_2 | \
390 FLASH_FLAG_READ_1_1_4 | \
391 FLASH_FLAG_READ_1_4_4 | \
392 FLASH_FLAG_WRITE_1_1_2 | \
393 FLASH_FLAG_WRITE_1_2_2 | \
394 FLASH_FLAG_WRITE_1_1_4 | \
395 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000396 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
397 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000398 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000399 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000400
401 /*
402 * Spansion S25FLxxxP
403 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
404 */
405#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
406 FLASH_FLAG_READ_1_1_2 | \
407 FLASH_FLAG_READ_1_2_2 | \
408 FLASH_FLAG_READ_1_1_4 | \
409 FLASH_FLAG_READ_1_4_4 | \
410 FLASH_FLAG_WRITE_1_1_4 | \
411 FLASH_FLAG_READ_FAST)
412 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000413 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000414 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000415 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000416
417 /*
418 * Spansion S25FLxxxS
419 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
420 * - RESET# signal supported by die but not bristled out on all
421 * package types. The package type is a function of board design,
422 * so this information is captured in the board's flags.
423 * - Supports 'DYB' sector protection. Depending on variant, sectors
424 * may default to locked state on power-on.
425 */
426#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
427 FLASH_FLAG_RESET | \
428 FLASH_FLAG_DYB_LOCKING)
429 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000430 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000431 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
Lee Jones5343a122014-03-20 09:21:04 +0000432 stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000433 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
Lee Jones5343a122014-03-20 09:21:04 +0000434 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000435 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
Lee Jones5343a122014-03-20 09:21:04 +0000436 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000437
438 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
439#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
440 FLASH_FLAG_READ_FAST | \
441 FLASH_FLAG_READ_1_1_2 | \
442 FLASH_FLAG_WRITE_1_1_2)
443 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
444 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
445 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
446 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
447 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
448
449 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
450#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
451 FLASH_FLAG_READ_FAST | \
452 FLASH_FLAG_READ_1_1_2 | \
453 FLASH_FLAG_READ_1_2_2 | \
454 FLASH_FLAG_READ_1_1_4 | \
455 FLASH_FLAG_READ_1_4_4 | \
456 FLASH_FLAG_WRITE_1_1_4)
Lee Jonescd7cac92014-03-20 09:21:05 +0000457 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
458 stfsm_w25q_config },
459 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
460 stfsm_w25q_config },
461 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
462 stfsm_w25q_config },
463 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
464 stfsm_w25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000465
466 /* Sentinel */
467 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
468};
469
Lee Jonesa37b2f52014-03-20 09:20:53 +0000470/*
471 * FSM message sequence configurations:
472 *
473 * All configs are presented in order of preference
474 */
475
476/* Default READ configurations, in order of preference */
477static struct seq_rw_config default_read_configs[] = {
478 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
479 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
480 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
481 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
482 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
483 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
484 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
485};
486
487/* Default WRITE configurations */
488static struct seq_rw_config default_write_configs[] = {
489 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
490 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
491 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
492 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
493 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
494 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
495};
496
Lee Jonese85a6192014-03-20 09:20:54 +0000497/*
498 * [N25Qxxx] Configuration
499 */
500#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
501#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
502#define N25Q_VCR_WRAP_CONT 0x3
503
504/* N25Q 3-byte Address READ configurations
505 * - 'FAST' variants configured for 8 dummy cycles.
506 *
507 * Note, the number of dummy cycles used for 'FAST' READ operations is
508 * configurable and would normally be tuned according to the READ command and
509 * operating frequency. However, this applies universally to all 'FAST' READ
510 * commands, including those used by the SPIBoot controller, and remains in
511 * force until the device is power-cycled. Since the SPIBoot controller is
512 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
513 * cycles.
514 */
515static struct seq_rw_config n25q_read3_configs[] = {
516 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
517 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
518 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
519 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
520 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
521 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
522 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
523};
524
525/* N25Q 4-byte Address READ configurations
526 * - use special 4-byte address READ commands (reduces overheads, and
527 * reduces risk of hitting watchdog reset issues).
528 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
529 */
530static struct seq_rw_config n25q_read4_configs[] = {
531 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
532 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
533 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
534 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
535 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
536 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
537 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
538};
539
Lee Jones898180662014-03-20 09:21:03 +0000540/*
541 * [MX25xxx] Configuration
542 */
543#define MX25_STATUS_QE (0x1 << 6)
544
545static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
546{
547 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
548 SEQ_OPC_CYCLES(8) |
549 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR) |
550 SEQ_OPC_CSDEASSERT);
551
552 seq->seq[0] = STFSM_INST_CMD1;
553 seq->seq[1] = STFSM_INST_WAIT;
554 seq->seq[2] = STFSM_INST_STOP;
555
556 seq->seq_cfg = (SEQ_CFG_PADS_1 |
557 SEQ_CFG_ERASE |
558 SEQ_CFG_READNOTWRITE |
559 SEQ_CFG_CSDEASSERT |
560 SEQ_CFG_STARTSEQ);
561
562 return 0;
563}
564
Lee Jones5343a122014-03-20 09:21:04 +0000565/*
566 * [S25FLxxx] Configuration
567 */
568#define STFSM_S25FL_CONFIG_QE (0x1 << 1)
569
570/*
571 * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
572 * Register, Extended Address Modes, and a 32-bit address command set. The
573 * 32-bit address command set is used here, since it avoids any problems with
574 * entering a state that is incompatible with the SPIBoot Controller.
575 */
576static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
577 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
578 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
579 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
580 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
581 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
582 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
583 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
584};
585
586static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
587 {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
588 {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
589 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
590};
591
Lee Jonescd7cac92014-03-20 09:21:05 +0000592/*
593 * [W25Qxxx] Configuration
594 */
595#define W25Q_STATUS_QE (0x1 << 9)
596
Lee Jones1bd512b2014-03-20 09:20:38 +0000597static struct stfsm_seq stfsm_seq_read_jedec = {
598 .data_size = TRANSFER_SIZE(8),
599 .seq_opc[0] = (SEQ_OPC_PADS_1 |
600 SEQ_OPC_CYCLES(8) |
601 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
602 .seq = {
603 STFSM_INST_CMD1,
604 STFSM_INST_DATA_READ,
605 STFSM_INST_STOP,
606 },
607 .seq_cfg = (SEQ_CFG_PADS_1 |
608 SEQ_CFG_READNOTWRITE |
609 SEQ_CFG_CSDEASSERT |
610 SEQ_CFG_STARTSEQ),
611};
612
Lee Jones176b4372014-03-20 09:20:59 +0000613static struct stfsm_seq stfsm_seq_read_status_fifo = {
614 .data_size = TRANSFER_SIZE(4),
615 .seq_opc[0] = (SEQ_OPC_PADS_1 |
616 SEQ_OPC_CYCLES(8) |
617 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
618 .seq = {
619 STFSM_INST_CMD1,
620 STFSM_INST_DATA_READ,
621 STFSM_INST_STOP,
622 },
623 .seq_cfg = (SEQ_CFG_PADS_1 |
624 SEQ_CFG_READNOTWRITE |
625 SEQ_CFG_CSDEASSERT |
626 SEQ_CFG_STARTSEQ),
627};
628
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000629static struct stfsm_seq stfsm_seq_erase_sector = {
630 /* 'addr_cfg' configured during initialisation */
631 .seq_opc = {
632 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
633 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
634
635 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
636 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
637 },
638 .seq = {
639 STFSM_INST_CMD1,
640 STFSM_INST_CMD2,
641 STFSM_INST_ADD1,
642 STFSM_INST_ADD2,
643 STFSM_INST_STOP,
644 },
645 .seq_cfg = (SEQ_CFG_PADS_1 |
646 SEQ_CFG_READNOTWRITE |
647 SEQ_CFG_CSDEASSERT |
648 SEQ_CFG_STARTSEQ),
649};
650
Lee Jones4a341fe2014-03-20 09:21:00 +0000651static struct stfsm_seq stfsm_seq_erase_chip = {
652 .seq_opc = {
653 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
654 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
655
656 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
657 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
658 },
659 .seq = {
660 STFSM_INST_CMD1,
661 STFSM_INST_CMD2,
662 STFSM_INST_WAIT,
663 STFSM_INST_STOP,
664 },
665 .seq_cfg = (SEQ_CFG_PADS_1 |
666 SEQ_CFG_ERASE |
667 SEQ_CFG_READNOTWRITE |
668 SEQ_CFG_CSDEASSERT |
669 SEQ_CFG_STARTSEQ),
670};
671
Lee Jones150571b2014-03-20 09:21:02 +0000672static struct stfsm_seq stfsm_seq_write_status = {
673 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
674 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
675 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
676 SEQ_OPC_OPCODE(FLASH_CMD_WRSR)),
677 .seq = {
678 STFSM_INST_CMD1,
679 STFSM_INST_CMD2,
680 STFSM_INST_STA_WR1,
681 STFSM_INST_STOP,
682 },
683 .seq_cfg = (SEQ_CFG_PADS_1 |
684 SEQ_CFG_READNOTWRITE |
685 SEQ_CFG_CSDEASSERT |
686 SEQ_CFG_STARTSEQ),
687};
688
Lee Jones249516c2014-03-20 09:20:52 +0000689static struct stfsm_seq stfsm_seq_wrvcr = {
690 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
691 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
692 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
693 SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
694 .seq = {
695 STFSM_INST_CMD1,
696 STFSM_INST_CMD2,
697 STFSM_INST_STA_WR1,
698 STFSM_INST_STOP,
699 },
700 .seq_cfg = (SEQ_CFG_PADS_1 |
701 SEQ_CFG_READNOTWRITE |
702 SEQ_CFG_CSDEASSERT |
703 SEQ_CFG_STARTSEQ),
704};
705
Lee Jones6bd29602014-03-20 09:20:48 +0000706static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
707{
708 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
709 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
710 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
711 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
712 SEQ_OPC_CSDEASSERT);
713
714 seq->seq[0] = STFSM_INST_CMD2;
715 seq->seq[1] = STFSM_INST_CMD1;
716 seq->seq[2] = STFSM_INST_WAIT;
717 seq->seq[3] = STFSM_INST_STOP;
718
719 seq->seq_cfg = (SEQ_CFG_PADS_1 |
720 SEQ_CFG_ERASE |
721 SEQ_CFG_READNOTWRITE |
722 SEQ_CFG_CSDEASSERT |
723 SEQ_CFG_STARTSEQ);
724
725 return 0;
726}
727
Lee Jones3c8b85b2014-03-20 09:20:36 +0000728static inline int stfsm_is_idle(struct stfsm *fsm)
729{
730 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
731}
732
Lee Jones86f309fd2014-03-20 09:20:35 +0000733static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
734{
735 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
736}
737
738static void stfsm_clear_fifo(struct stfsm *fsm)
739{
740 uint32_t avail;
741
742 for (;;) {
743 avail = stfsm_fifo_available(fsm);
744 if (!avail)
745 break;
746
747 while (avail) {
748 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
749 avail--;
750 }
751 }
752}
753
Lee Jones3c8b85b2014-03-20 09:20:36 +0000754static inline void stfsm_load_seq(struct stfsm *fsm,
755 const struct stfsm_seq *seq)
756{
757 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
758 const uint32_t *src = (const uint32_t *)seq;
759 int words = sizeof(*seq) / sizeof(*src);
760
761 BUG_ON(!stfsm_is_idle(fsm));
762
763 while (words--) {
764 writel(*src, dst);
765 src++;
766 dst += 4;
767 }
768}
769
770static void stfsm_wait_seq(struct stfsm *fsm)
771{
772 unsigned long deadline;
773 int timeout = 0;
774
775 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
776
777 while (!timeout) {
778 if (time_after_eq(jiffies, deadline))
779 timeout = 1;
780
781 if (stfsm_is_idle(fsm))
782 return;
783
784 cond_resched();
785 }
786
787 dev_err(fsm->dev, "timeout on sequence completion\n");
788}
789
Lee Jones3f9d7202014-03-20 11:11:43 +0000790static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
Lee Jones030e82d2014-03-20 09:20:37 +0000791{
792 uint32_t remaining = size >> 2;
793 uint32_t avail;
794 uint32_t words;
795
796 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
797
798 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
799
800 while (remaining) {
801 for (;;) {
802 avail = stfsm_fifo_available(fsm);
803 if (avail)
804 break;
805 udelay(1);
806 }
807 words = min(avail, remaining);
808 remaining -= words;
809
810 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
811 buf += words;
812 }
813}
814
Lee Jones3f9d7202014-03-20 11:11:43 +0000815static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
816 uint32_t size)
Lee Jones30ca64f2014-03-20 09:20:58 +0000817{
818 uint32_t words = size >> 2;
819
820 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
821
822 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
823
824 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
825
826 return size;
827}
828
Lee Jones0de08e42014-03-20 09:20:51 +0000829static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
830{
Lee Jonese6b1bb42014-03-20 09:21:06 +0000831 struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
Lee Jones0de08e42014-03-20 09:20:51 +0000832 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
833
834 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
835 SEQ_OPC_CYCLES(8) |
836 SEQ_OPC_OPCODE(cmd) |
837 SEQ_OPC_CSDEASSERT);
838
839 stfsm_load_seq(fsm, seq);
840
841 stfsm_wait_seq(fsm);
842
843 return 0;
844}
845
Lee Jones176b4372014-03-20 09:20:59 +0000846static uint8_t stfsm_wait_busy(struct stfsm *fsm)
847{
848 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
849 unsigned long deadline;
850 uint32_t status;
851 int timeout = 0;
852
853 /* Use RDRS1 */
854 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
855 SEQ_OPC_CYCLES(8) |
856 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
857
858 /* Load read_status sequence */
859 stfsm_load_seq(fsm, seq);
860
861 /*
862 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
863 */
864 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
865 while (!timeout) {
Lee Jones176b4372014-03-20 09:20:59 +0000866 if (time_after_eq(jiffies, deadline))
867 timeout = 1;
868
869 stfsm_wait_seq(fsm);
870
871 stfsm_read_fifo(fsm, &status, 4);
872
873 if ((status & FLASH_STATUS_BUSY) == 0)
874 return 0;
875
876 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
877 ((status & S25FL_STATUS_P_ERR) ||
878 (status & S25FL_STATUS_E_ERR)))
879 return (uint8_t)(status & 0xff);
880
881 if (!timeout)
882 /* Restart */
883 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
Lee Jonesea7864b2014-03-20 11:11:46 +0000884
885 cond_resched();
Lee Jones176b4372014-03-20 09:20:59 +0000886 }
887
888 dev_err(fsm->dev, "timeout on wait_busy\n");
889
890 return FLASH_STATUS_TIMEOUT;
891}
892
Lee Jonesac94dbc2014-03-20 09:21:01 +0000893static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
894 uint8_t *status)
895{
896 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
897 uint32_t tmp;
898
899 dev_dbg(fsm->dev, "reading STA[%s]\n",
900 (cmd == FLASH_CMD_RDSR) ? "1" : "2");
901
902 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
903 SEQ_OPC_CYCLES(8) |
904 SEQ_OPC_OPCODE(cmd)),
905
906 stfsm_load_seq(fsm, seq);
907
908 stfsm_read_fifo(fsm, &tmp, 4);
909
910 *status = (uint8_t)(tmp >> 24);
911
912 stfsm_wait_seq(fsm);
913
914 return 0;
915}
916
Lee Jones150571b2014-03-20 09:21:02 +0000917static int stfsm_write_status(struct stfsm *fsm, uint16_t status,
918 int sta_bytes)
919{
920 struct stfsm_seq *seq = &stfsm_seq_write_status;
921
922 dev_dbg(fsm->dev, "writing STA[%s] 0x%04x\n",
923 (sta_bytes == 1) ? "1" : "1+2", status);
924
925 seq->status = (uint32_t)status | STA_PADS_1 | STA_CSDEASSERT;
926 seq->seq[2] = (sta_bytes == 1) ?
927 STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
928
929 stfsm_load_seq(fsm, seq);
930
931 stfsm_wait_seq(fsm);
932
933 return 0;
934};
935
Lee Jones249516c2014-03-20 09:20:52 +0000936static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
937{
938 struct stfsm_seq *seq = &stfsm_seq_wrvcr;
939
940 dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
941
942 seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
943
944 stfsm_load_seq(fsm, seq);
945
946 stfsm_wait_seq(fsm);
947
948 return 0;
949}
950
Lee Jones0ea7d702014-03-20 09:20:50 +0000951/*
952 * SoC reset on 'boot-from-spi' systems
953 *
954 * Certain modes of operation cause the Flash device to enter a particular state
955 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
956 * Addr' commands). On boot-from-spi systems, it is important to consider what
957 * happens if a warm reset occurs during this period. The SPIBoot controller
958 * assumes that Flash device is in its default reset state, 24-bit address mode,
959 * and ready to accept commands. This can be achieved using some form of
960 * on-board logic/controller to force a device POR in response to a SoC-level
961 * reset or by making use of the device reset signal if available (limited
962 * number of devices only).
963 *
964 * Failure to take such precautions can cause problems following a warm reset.
965 * For some operations (e.g. ERASE), there is little that can be done. For
966 * other modes of operation (e.g. 32-bit addressing), options are often
967 * available that can help minimise the window in which a reset could cause a
968 * problem.
969 *
970 */
971static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
972{
973 /* Reset signal is available on the board and supported by the device */
974 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
975 return true;
976
977 /* Board-level logic forces a power-on-reset */
978 if (fsm->reset_por)
979 return true;
980
981 /* Reset is not properly handled and may result in failure to reboot */
982 return false;
983}
984
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000985/* Configure 'addr_cfg' according to addressing mode */
986static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
987 struct stfsm_seq *seq)
988{
989 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
990
991 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
992 ADR_CFG_PADS_1_ADD1 |
993 ADR_CFG_CYCLES_ADD2(16) |
994 ADR_CFG_PADS_1_ADD2 |
995 ADR_CFG_CSDEASSERT_ADD2);
996}
997
Lee Jones08981272014-03-20 09:20:42 +0000998/* Search for preferred configuration based on available flags */
999static struct seq_rw_config *
1000stfsm_search_seq_rw_configs(struct stfsm *fsm,
1001 struct seq_rw_config cfgs[])
1002{
1003 struct seq_rw_config *config;
1004 int flags = fsm->info->flags;
1005
1006 for (config = cfgs; config->cmd != 0; config++)
1007 if ((config->flags & flags) == config->flags)
1008 return config;
1009
1010 return NULL;
1011}
1012
Lee Jones97ccf2d2014-03-20 09:20:44 +00001013/* Prepare a READ/WRITE sequence according to configuration parameters */
1014static void stfsm_prepare_rw_seq(struct stfsm *fsm,
1015 struct stfsm_seq *seq,
1016 struct seq_rw_config *cfg)
1017{
1018 int addr1_cycles, addr2_cycles;
1019 int i = 0;
1020
1021 memset(seq, 0, sizeof(*seq));
1022
1023 /* Add READ/WRITE OPC */
1024 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1025 SEQ_OPC_CYCLES(8) |
1026 SEQ_OPC_OPCODE(cfg->cmd));
1027
1028 /* Add WREN OPC for a WRITE sequence */
1029 if (cfg->write)
1030 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
1031 SEQ_OPC_CYCLES(8) |
1032 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1033 SEQ_OPC_CSDEASSERT);
1034
1035 /* Address configuration (24 or 32-bit addresses) */
1036 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
1037 addr1_cycles /= cfg->addr_pads;
1038 addr2_cycles = 16 / cfg->addr_pads;
1039 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
1040 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
1041 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
1042 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
1043
1044 /* Data/Sequence configuration */
1045 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
1046 SEQ_CFG_STARTSEQ |
1047 SEQ_CFG_CSDEASSERT);
1048 if (!cfg->write)
1049 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
1050
1051 /* Mode configuration (no. of pads taken from addr cfg) */
1052 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
1053 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
1054 (cfg->addr_pads - 1) << 22); /* pads */
1055
1056 /* Dummy configuration (no. of pads taken from addr cfg) */
1057 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
1058 (cfg->addr_pads - 1) << 22); /* pads */
1059
1060
1061 /* Instruction sequence */
1062 i = 0;
1063 if (cfg->write)
1064 seq->seq[i++] = STFSM_INST_CMD2;
1065
1066 seq->seq[i++] = STFSM_INST_CMD1;
1067
1068 seq->seq[i++] = STFSM_INST_ADD1;
1069 seq->seq[i++] = STFSM_INST_ADD2;
1070
1071 if (cfg->mode_cycles)
1072 seq->seq[i++] = STFSM_INST_MODE;
1073
1074 if (cfg->dummy_cycles)
1075 seq->seq[i++] = STFSM_INST_DUMMY;
1076
1077 seq->seq[i++] =
1078 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
1079 seq->seq[i++] = STFSM_INST_STOP;
1080}
1081
Lee Jones88cccb82014-03-20 09:20:49 +00001082static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
1083 struct stfsm_seq *seq,
1084 struct seq_rw_config *cfgs)
1085{
1086 struct seq_rw_config *config;
1087
1088 config = stfsm_search_seq_rw_configs(fsm, cfgs);
1089 if (!config) {
1090 dev_err(fsm->dev, "failed to find suitable config\n");
1091 return -EINVAL;
1092 }
1093
1094 stfsm_prepare_rw_seq(fsm, seq, config);
1095
1096 return 0;
1097}
1098
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001099/* Prepare a READ/WRITE/ERASE 'default' sequences */
1100static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
1101{
1102 uint32_t flags = fsm->info->flags;
1103 int ret;
1104
1105 /* Configure 'READ' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001106 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001107 default_read_configs);
1108 if (ret) {
1109 dev_err(fsm->dev,
1110 "failed to prep READ sequence with flags [0x%08x]\n",
1111 flags);
1112 return ret;
1113 }
1114
1115 /* Configure 'WRITE' sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001116 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001117 default_write_configs);
1118 if (ret) {
1119 dev_err(fsm->dev,
1120 "failed to prep WRITE sequence with flags [0x%08x]\n",
1121 flags);
1122 return ret;
1123 }
1124
1125 /* Configure 'ERASE_SECTOR' sequence */
1126 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1127
1128 return 0;
1129}
1130
Lee Jones898180662014-03-20 09:21:03 +00001131static int stfsm_mx25_config(struct stfsm *fsm)
1132{
1133 uint32_t flags = fsm->info->flags;
1134 uint32_t data_pads;
1135 uint8_t sta;
1136 int ret;
1137 bool soc_reset;
1138
1139 /*
1140 * Use default READ/WRITE sequences
1141 */
1142 ret = stfsm_prepare_rwe_seqs_default(fsm);
1143 if (ret)
1144 return ret;
1145
1146 /*
1147 * Configure 32-bit Address Support
1148 */
1149 if (flags & FLASH_FLAG_32BIT_ADDR) {
1150 /* Configure 'enter_32bitaddr' FSM sequence */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001151 stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones898180662014-03-20 09:21:03 +00001152
1153 soc_reset = stfsm_can_handle_soc_reset(fsm);
Angus Clark009e7e62014-03-26 16:39:16 +00001154 if (soc_reset || !fsm->booted_from_spi)
Lee Jones898180662014-03-20 09:21:03 +00001155 /* If we can handle SoC resets, we enable 32-bit address
1156 * mode pervasively */
1157 stfsm_enter_32bit_addr(fsm, 1);
1158
Angus Clark009e7e62014-03-26 16:39:16 +00001159 else
Lee Jones898180662014-03-20 09:21:03 +00001160 /* Else, enable/disable 32-bit addressing before/after
1161 * each operation */
1162 fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
1163 CFG_WRITE_TOGGLE_32BIT_ADDR |
1164 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
Lee Jones898180662014-03-20 09:21:03 +00001165 }
1166
1167 /* For QUAD mode, set 'QE' STATUS bit */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001168 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones898180662014-03-20 09:21:03 +00001169 if (data_pads == 4) {
1170 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta);
1171 sta |= MX25_STATUS_QE;
1172 stfsm_write_status(fsm, sta, 1);
1173 }
1174
1175 return 0;
1176}
1177
Lee Jones218b8702014-03-20 09:20:55 +00001178static int stfsm_n25q_config(struct stfsm *fsm)
1179{
1180 uint32_t flags = fsm->info->flags;
1181 uint8_t vcr;
1182 int ret = 0;
1183 bool soc_reset;
1184
1185 /* Configure 'READ' sequence */
1186 if (flags & FLASH_FLAG_32BIT_ADDR)
Lee Jonese6b1bb42014-03-20 09:21:06 +00001187 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones218b8702014-03-20 09:20:55 +00001188 n25q_read4_configs);
1189 else
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_read3_configs);
1192 if (ret) {
1193 dev_err(fsm->dev,
1194 "failed to prepare READ sequence with flags [0x%08x]\n",
1195 flags);
1196 return ret;
1197 }
1198
1199 /* Configure 'WRITE' sequence (default configs) */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001200 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones218b8702014-03-20 09:20:55 +00001201 default_write_configs);
1202 if (ret) {
1203 dev_err(fsm->dev,
1204 "preparing WRITE sequence using flags [0x%08x] failed\n",
1205 flags);
1206 return ret;
1207 }
1208
1209 /* * Configure 'ERASE_SECTOR' sequence */
1210 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1211
1212 /* Configure 32-bit address support */
1213 if (flags & FLASH_FLAG_32BIT_ADDR) {
Lee Jonese6b1bb42014-03-20 09:21:06 +00001214 stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
Lee Jones218b8702014-03-20 09:20:55 +00001215
1216 soc_reset = stfsm_can_handle_soc_reset(fsm);
1217 if (soc_reset || !fsm->booted_from_spi) {
1218 /*
1219 * If we can handle SoC resets, we enable 32-bit
1220 * address mode pervasively
1221 */
1222 stfsm_enter_32bit_addr(fsm, 1);
1223 } else {
1224 /*
1225 * If not, enable/disable for WRITE and ERASE
1226 * operations (READ uses special commands)
1227 */
1228 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1229 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1230 }
1231 }
1232
1233 /*
1234 * Configure device to use 8 dummy cycles
1235 */
1236 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1237 N25Q_VCR_WRAP_CONT);
1238 stfsm_wrvcr(fsm, vcr);
1239
1240 return 0;
1241}
1242
Lee Jones5343a122014-03-20 09:21:04 +00001243static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
1244{
1245 seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
1246 SEQ_OPC_CYCLES(8) |
1247 SEQ_OPC_OPCODE(S25FL_CMD_SE4));
1248
1249 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1250 ADR_CFG_PADS_1_ADD1 |
1251 ADR_CFG_CYCLES_ADD2(16) |
1252 ADR_CFG_PADS_1_ADD2 |
1253 ADR_CFG_CSDEASSERT_ADD2);
1254}
1255
1256static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
1257{
1258 uint32_t tmp;
1259 struct stfsm_seq seq = {
1260 .data_size = TRANSFER_SIZE(4),
1261 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1262 SEQ_OPC_CYCLES(8) |
1263 SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
1264 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1265 ADR_CFG_PADS_1_ADD1 |
1266 ADR_CFG_CYCLES_ADD2(16) |
1267 ADR_CFG_PADS_1_ADD2),
1268 .addr1 = (offs >> 16) & 0xffff,
1269 .addr2 = offs & 0xffff,
1270 .seq = {
1271 STFSM_INST_CMD1,
1272 STFSM_INST_ADD1,
1273 STFSM_INST_ADD2,
1274 STFSM_INST_DATA_READ,
1275 STFSM_INST_STOP,
1276 },
1277 .seq_cfg = (SEQ_CFG_PADS_1 |
1278 SEQ_CFG_READNOTWRITE |
1279 SEQ_CFG_CSDEASSERT |
1280 SEQ_CFG_STARTSEQ),
1281 };
1282
1283 stfsm_load_seq(fsm, &seq);
1284
1285 stfsm_read_fifo(fsm, &tmp, 4);
1286
1287 *dby = (uint8_t)(tmp >> 24);
1288
1289 stfsm_wait_seq(fsm);
1290}
1291
1292static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
1293{
1294 struct stfsm_seq seq = {
1295 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1296 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
1297 SEQ_OPC_CSDEASSERT),
1298 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
1299 SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
1300 .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
1301 ADR_CFG_PADS_1_ADD1 |
1302 ADR_CFG_CYCLES_ADD2(16) |
1303 ADR_CFG_PADS_1_ADD2),
1304 .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
1305 .addr1 = (offs >> 16) & 0xffff,
1306 .addr2 = offs & 0xffff,
1307 .seq = {
1308 STFSM_INST_CMD1,
1309 STFSM_INST_CMD2,
1310 STFSM_INST_ADD1,
1311 STFSM_INST_ADD2,
1312 STFSM_INST_STA_WR1,
1313 STFSM_INST_STOP,
1314 },
1315 .seq_cfg = (SEQ_CFG_PADS_1 |
1316 SEQ_CFG_READNOTWRITE |
1317 SEQ_CFG_CSDEASSERT |
1318 SEQ_CFG_STARTSEQ),
1319 };
1320
1321 stfsm_load_seq(fsm, &seq);
1322 stfsm_wait_seq(fsm);
1323
1324 stfsm_wait_busy(fsm);
1325}
1326
1327static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
1328{
1329 struct stfsm_seq seq = {
1330 .seq_opc[0] = (SEQ_OPC_PADS_1 |
1331 SEQ_OPC_CYCLES(8) |
1332 SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
1333 SEQ_OPC_CSDEASSERT),
1334 .seq_opc[1] = (SEQ_OPC_PADS_1 |
1335 SEQ_OPC_CYCLES(8) |
1336 SEQ_OPC_OPCODE(FLASH_CMD_WRDI) |
1337 SEQ_OPC_CSDEASSERT),
1338 .seq = {
1339 STFSM_INST_CMD1,
1340 STFSM_INST_CMD2,
1341 STFSM_INST_WAIT,
1342 STFSM_INST_STOP,
1343 },
1344 .seq_cfg = (SEQ_CFG_PADS_1 |
1345 SEQ_CFG_ERASE |
1346 SEQ_CFG_READNOTWRITE |
1347 SEQ_CFG_CSDEASSERT |
1348 SEQ_CFG_STARTSEQ),
1349 };
1350
1351 stfsm_load_seq(fsm, &seq);
1352
1353 stfsm_wait_seq(fsm);
1354
1355 return 0;
1356}
1357
1358static int stfsm_s25fl_config(struct stfsm *fsm)
1359{
1360 struct flash_info *info = fsm->info;
1361 uint32_t flags = info->flags;
1362 uint32_t data_pads;
1363 uint32_t offs;
1364 uint16_t sta_wr;
1365 uint8_t sr1, cr1, dyb;
1366 int ret;
1367
1368 if (flags & FLASH_FLAG_32BIT_ADDR) {
1369 /*
1370 * Prepare Read/Write/Erase sequences according to S25FLxxx
1371 * 32-bit address command set
1372 */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001373 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
Lee Jones5343a122014-03-20 09:21:04 +00001374 stfsm_s25fl_read4_configs);
1375 if (ret)
1376 return ret;
1377
Lee Jonese6b1bb42014-03-20 09:21:06 +00001378 ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
Lee Jones5343a122014-03-20 09:21:04 +00001379 stfsm_s25fl_write4_configs);
1380 if (ret)
1381 return ret;
1382
1383 stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
1384
1385 } else {
1386 /* Use default configurations for 24-bit addressing */
1387 ret = stfsm_prepare_rwe_seqs_default(fsm);
1388 if (ret)
1389 return ret;
1390 }
1391
1392 /*
1393 * For devices that support 'DYB' sector locking, check lock status and
1394 * unlock sectors if necessary (some variants power-on with sectors
1395 * locked by default)
1396 */
1397 if (flags & FLASH_FLAG_DYB_LOCKING) {
1398 offs = 0;
1399 for (offs = 0; offs < info->sector_size * info->n_sectors;) {
1400 stfsm_s25fl_read_dyb(fsm, offs, &dyb);
1401 if (dyb == 0x00)
1402 stfsm_s25fl_write_dyb(fsm, offs, 0xff);
1403
1404 /* Handle bottom/top 4KiB parameter sectors */
1405 if ((offs < info->sector_size * 2) ||
1406 (offs >= (info->sector_size - info->n_sectors * 4)))
1407 offs += 0x1000;
1408 else
1409 offs += 0x10000;
1410 }
1411 }
1412
1413 /* Check status of 'QE' bit */
Lee Jonese6b1bb42014-03-20 09:21:06 +00001414 data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
Lee Jones5343a122014-03-20 09:21:04 +00001415 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &cr1);
1416 if (data_pads == 4) {
1417 if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
1418 /* Set 'QE' */
1419 cr1 |= STFSM_S25FL_CONFIG_QE;
1420
1421 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1);
1422 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1423
1424 stfsm_write_status(fsm, sta_wr, 2);
1425
1426 stfsm_wait_busy(fsm);
1427 }
1428 } else {
1429 if ((cr1 & STFSM_S25FL_CONFIG_QE)) {
1430 /* Clear 'QE' */
1431 cr1 &= ~STFSM_S25FL_CONFIG_QE;
1432
1433 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sr1);
1434 sta_wr = ((uint16_t)cr1 << 8) | sr1;
1435
1436 stfsm_write_status(fsm, sta_wr, 2);
1437
1438 stfsm_wait_busy(fsm);
1439 }
1440
1441 }
1442
1443 /*
1444 * S25FLxxx devices support Program and Error error flags.
1445 * Configure driver to check flags and clear if necessary.
1446 */
1447 fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
1448
1449 return 0;
1450}
1451
Lee Jonescd7cac92014-03-20 09:21:05 +00001452static int stfsm_w25q_config(struct stfsm *fsm)
1453{
1454 uint32_t data_pads;
1455 uint16_t sta_wr;
1456 uint8_t sta1, sta2;
1457 int ret;
1458
1459 ret = stfsm_prepare_rwe_seqs_default(fsm);
1460 if (ret)
1461 return ret;
1462
1463 /* If using QUAD mode, set QE STATUS bit */
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) {
1466 stfsm_read_status(fsm, FLASH_CMD_RDSR, &sta1);
1467 stfsm_read_status(fsm, FLASH_CMD_RDSR2, &sta2);
1468
1469 sta_wr = ((uint16_t)sta2 << 8) | sta1;
1470
1471 sta_wr |= W25Q_STATUS_QE;
1472
1473 stfsm_write_status(fsm, sta_wr, 2);
1474
1475 stfsm_wait_busy(fsm);
1476 }
1477
1478 return 0;
1479}
1480
Lee Jonese514f102014-03-20 09:20:57 +00001481static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1482 uint32_t offset)
1483{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001484 struct stfsm_seq *seq = &fsm->stfsm_seq_read;
Lee Jonese514f102014-03-20 09:20:57 +00001485 uint32_t data_pads;
1486 uint32_t read_mask;
1487 uint32_t size_ub;
1488 uint32_t size_lb;
1489 uint32_t size_mop;
1490 uint32_t tmp[4];
1491 uint32_t page_buf[FLASH_PAGESIZE_32];
1492 uint8_t *p;
1493
1494 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1495
1496 /* Enter 32-bit address mode, if required */
1497 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1498 stfsm_enter_32bit_addr(fsm, 1);
1499
1500 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1501 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1502 read_mask = (data_pads << 2) - 1;
1503
1504 /* Handle non-aligned buf */
1505 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1506
1507 /* Handle non-aligned size */
1508 size_ub = (size + read_mask) & ~read_mask;
1509 size_lb = size & ~read_mask;
1510 size_mop = size & read_mask;
1511
1512 seq->data_size = TRANSFER_SIZE(size_ub);
1513 seq->addr1 = (offset >> 16) & 0xffff;
1514 seq->addr2 = offset & 0xffff;
1515
1516 stfsm_load_seq(fsm, seq);
1517
1518 if (size_lb)
1519 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1520
1521 if (size_mop) {
1522 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1523 memcpy(p + size_lb, &tmp, size_mop);
1524 }
1525
1526 /* Handle non-aligned buf */
1527 if ((uint32_t)buf & 0x3)
1528 memcpy(buf, page_buf, size);
1529
1530 /* Wait for sequence to finish */
1531 stfsm_wait_seq(fsm);
1532
1533 stfsm_clear_fifo(fsm);
1534
1535 /* Exit 32-bit address mode, if required */
1536 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1537 stfsm_enter_32bit_addr(fsm, 0);
1538
1539 return 0;
1540}
1541
Lee Jones3f9d7202014-03-20 11:11:43 +00001542static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
1543 uint32_t size, uint32_t offset)
Lee Jones176b4372014-03-20 09:20:59 +00001544{
Lee Jonese6b1bb42014-03-20 09:21:06 +00001545 struct stfsm_seq *seq = &fsm->stfsm_seq_write;
Lee Jones176b4372014-03-20 09:20:59 +00001546 uint32_t data_pads;
1547 uint32_t write_mask;
1548 uint32_t size_ub;
1549 uint32_t size_lb;
1550 uint32_t size_mop;
1551 uint32_t tmp[4];
1552 uint32_t page_buf[FLASH_PAGESIZE_32];
1553 uint8_t *t = (uint8_t *)&tmp;
1554 const uint8_t *p;
1555 int ret;
1556 int i;
1557
1558 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1559
1560 /* Enter 32-bit address mode, if required */
1561 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1562 stfsm_enter_32bit_addr(fsm, 1);
1563
1564 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1565 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1566 write_mask = (data_pads << 2) - 1;
1567
1568 /* Handle non-aligned buf */
1569 if ((uint32_t)buf & 0x3) {
1570 memcpy(page_buf, buf, size);
1571 p = (uint8_t *)page_buf;
1572 } else {
1573 p = buf;
1574 }
1575
1576 /* Handle non-aligned size */
1577 size_ub = (size + write_mask) & ~write_mask;
1578 size_lb = size & ~write_mask;
1579 size_mop = size & write_mask;
1580
1581 seq->data_size = TRANSFER_SIZE(size_ub);
1582 seq->addr1 = (offset >> 16) & 0xffff;
1583 seq->addr2 = offset & 0xffff;
1584
1585 /* Need to set FIFO to write mode, before writing data to FIFO (see
1586 * GNBvb79594)
1587 */
1588 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1589
1590 /*
1591 * Before writing data to the FIFO, apply a small delay to allow a
1592 * potential change of FIFO direction to complete.
1593 */
1594 if (fsm->fifo_dir_delay == 0)
1595 readl(fsm->base + SPI_FAST_SEQ_CFG);
1596 else
1597 udelay(fsm->fifo_dir_delay);
1598
1599
1600 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1601 if (size_lb) {
1602 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1603 p += size_lb;
1604 }
1605
1606 /* Handle non-aligned size */
1607 if (size_mop) {
1608 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1609 for (i = 0; i < size_mop; i++)
1610 t[i] = *p++;
1611
1612 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1613 }
1614
1615 /* Start sequence */
1616 stfsm_load_seq(fsm, seq);
1617
1618 /* Wait for sequence to finish */
1619 stfsm_wait_seq(fsm);
1620
1621 /* Wait for completion */
1622 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001623 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1624 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones176b4372014-03-20 09:20:59 +00001625
1626 /* Exit 32-bit address mode, if required */
Angus Clark009e7e62014-03-26 16:39:16 +00001627 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
Lee Jones176b4372014-03-20 09:20:59 +00001628 stfsm_enter_32bit_addr(fsm, 0);
Lee Jones176b4372014-03-20 09:20:59 +00001629
1630 return 0;
1631}
1632
Lee Jonese514f102014-03-20 09:20:57 +00001633/*
1634 * Read an address range from the flash chip. The address range
1635 * may be any size provided it is within the physical boundaries.
1636 */
1637static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1638 size_t *retlen, u_char *buf)
1639{
1640 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1641 uint32_t bytes;
1642
1643 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1644 __func__, (u32)from, len);
1645
1646 mutex_lock(&fsm->lock);
1647
1648 while (len > 0) {
1649 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1650
1651 stfsm_read(fsm, buf, bytes, from);
1652
1653 buf += bytes;
1654 from += bytes;
1655 len -= bytes;
1656
1657 *retlen += bytes;
1658 }
1659
1660 mutex_unlock(&fsm->lock);
1661
1662 return 0;
1663}
1664
Lee Jones3f9d7202014-03-20 11:11:43 +00001665static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
Lee Jones4a341fe2014-03-20 09:21:00 +00001666{
1667 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1668 int ret;
1669
1670 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1671
1672 /* Enter 32-bit address mode, if required */
1673 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1674 stfsm_enter_32bit_addr(fsm, 1);
1675
1676 seq->addr1 = (offset >> 16) & 0xffff;
1677 seq->addr2 = offset & 0xffff;
1678
1679 stfsm_load_seq(fsm, seq);
1680
1681 stfsm_wait_seq(fsm);
1682
1683 /* Wait for completion */
1684 ret = stfsm_wait_busy(fsm);
Lee Jones5343a122014-03-20 09:21:04 +00001685 if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
1686 stfsm_s25fl_clear_status_reg(fsm);
Lee Jones4a341fe2014-03-20 09:21:00 +00001687
1688 /* Exit 32-bit address mode, if required */
1689 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1690 stfsm_enter_32bit_addr(fsm, 0);
1691
1692 return ret;
1693}
1694
1695static int stfsm_erase_chip(struct stfsm *fsm)
1696{
1697 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1698
1699 dev_dbg(fsm->dev, "erasing chip\n");
1700
1701 stfsm_load_seq(fsm, seq);
1702
1703 stfsm_wait_seq(fsm);
1704
1705 return stfsm_wait_busy(fsm);
1706}
1707
Lee Jones176b4372014-03-20 09:20:59 +00001708/*
1709 * Write an address range to the flash chip. Data must be written in
1710 * FLASH_PAGESIZE chunks. The address range may be any size provided
1711 * it is within the physical boundaries.
1712 */
1713static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1714 size_t *retlen, const u_char *buf)
1715{
1716 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1717
1718 u32 page_offs;
1719 u32 bytes;
1720 uint8_t *b = (uint8_t *)buf;
1721 int ret = 0;
1722
1723 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1724
Lee Jones176b4372014-03-20 09:20:59 +00001725 /* Offset within page */
1726 page_offs = to % FLASH_PAGESIZE;
1727
1728 mutex_lock(&fsm->lock);
1729
1730 while (len) {
1731 /* Write up to page boundary */
1732 bytes = min(FLASH_PAGESIZE - page_offs, len);
1733
1734 ret = stfsm_write(fsm, b, bytes, to);
1735 if (ret)
1736 goto out1;
1737
1738 b += bytes;
1739 len -= bytes;
1740 to += bytes;
1741
1742 /* We are now page-aligned */
1743 page_offs = 0;
1744
1745 *retlen += bytes;
1746
1747 }
1748
1749out1:
1750 mutex_unlock(&fsm->lock);
1751
1752 return ret;
1753}
1754
Lee Jones4a341fe2014-03-20 09:21:00 +00001755/*
1756 * Erase an address range on the flash chip. The address range may extend
1757 * one or more erase sectors. Return an error is there is a problem erasing.
1758 */
1759static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1760{
1761 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1762 u32 addr, len;
1763 int ret;
1764
1765 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1766 (long long)instr->addr, (long long)instr->len);
1767
1768 addr = instr->addr;
1769 len = instr->len;
1770
1771 mutex_lock(&fsm->lock);
1772
1773 /* Whole-chip erase? */
1774 if (len == mtd->size) {
1775 ret = stfsm_erase_chip(fsm);
1776 if (ret)
1777 goto out1;
1778 } else {
1779 while (len) {
1780 ret = stfsm_erase_sector(fsm, addr);
1781 if (ret)
1782 goto out1;
1783
1784 addr += mtd->erasesize;
1785 len -= mtd->erasesize;
1786 }
1787 }
1788
1789 mutex_unlock(&fsm->lock);
1790
1791 instr->state = MTD_ERASE_DONE;
1792 mtd_erase_callback(instr);
1793
1794 return 0;
1795
1796out1:
1797 instr->state = MTD_ERASE_FAILED;
1798 mutex_unlock(&fsm->lock);
1799
1800 return ret;
1801}
1802
Lee Jones3f9d7202014-03-20 11:11:43 +00001803static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
Lee Jones1bd512b2014-03-20 09:20:38 +00001804{
1805 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1806 uint32_t tmp[2];
1807
1808 stfsm_load_seq(fsm, seq);
1809
1810 stfsm_read_fifo(fsm, tmp, 8);
1811
1812 memcpy(jedec, tmp, 5);
1813
1814 stfsm_wait_seq(fsm);
1815}
1816
1817static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1818{
Lee Jones24fec652014-03-20 09:20:41 +00001819 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001820 u16 ext_jedec;
1821 u32 jedec;
1822 u8 id[5];
1823
1824 stfsm_read_jedec(fsm, id);
1825
1826 jedec = id[0] << 16 | id[1] << 8 | id[2];
1827 /*
1828 * JEDEC also defines an optional "extended device information"
1829 * string for after vendor-specific data, after the three bytes
1830 * we use here. Supporting some chips might require using it.
1831 */
1832 ext_jedec = id[3] << 8 | id[4];
1833
1834 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1835 jedec, id[0], id[1], id[2], id[3], id[4]);
1836
Lee Jones24fec652014-03-20 09:20:41 +00001837 for (info = flash_types; info->name; info++) {
1838 if (info->jedec_id == jedec) {
1839 if (info->ext_id && info->ext_id != ext_jedec)
1840 continue;
1841 return info;
1842 }
1843 }
1844 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1845
Lee Jones1bd512b2014-03-20 09:20:38 +00001846 return NULL;
1847}
1848
Lee Jones86f309fd2014-03-20 09:20:35 +00001849static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1850{
1851 int ret, timeout = 10;
1852
1853 /* Wait for controller to accept mode change */
1854 while (--timeout) {
1855 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1856 if (ret & 0x1)
1857 break;
1858 udelay(1);
1859 }
1860
1861 if (!timeout)
1862 return -EBUSY;
1863
1864 writel(mode, fsm->base + SPI_MODESELECT);
1865
1866 return 0;
1867}
1868
1869static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1870{
1871 uint32_t emi_freq;
1872 uint32_t clk_div;
1873
1874 /* TODO: Make this dynamic */
1875 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1876
1877 /*
1878 * Calculate clk_div - values between 2 and 128
1879 * Multiple of 2, rounded up
1880 */
1881 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1882 if (clk_div < 2)
1883 clk_div = 2;
1884 else if (clk_div > 128)
1885 clk_div = 128;
1886
1887 /*
1888 * Determine a suitable delay for the IP to complete a change of
1889 * direction of the FIFO. The required delay is related to the clock
1890 * divider used. The following heuristics are based on empirical tests,
1891 * using a 100MHz EMI clock.
1892 */
1893 if (clk_div <= 4)
1894 fsm->fifo_dir_delay = 0;
1895 else if (clk_div <= 10)
1896 fsm->fifo_dir_delay = 1;
1897 else
1898 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1899
1900 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1901 emi_freq, spi_freq, clk_div);
1902
1903 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1904}
1905
1906static int stfsm_init(struct stfsm *fsm)
1907{
1908 int ret;
1909
1910 /* Perform a soft reset of the FSM controller */
1911 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1912 udelay(1);
1913 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1914
1915 /* Set clock to 'safe' frequency initially */
1916 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1917
1918 /* Switch to FSM */
1919 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1920 if (ret)
1921 return ret;
1922
1923 /* Set timing parameters */
1924 writel(SPI_CFG_DEVICE_ST |
1925 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1926 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1927 SPI_CFG_DEFAULT_DATA_HOLD,
1928 fsm->base + SPI_CONFIGDATA);
1929 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1930
Angus Clark009e7e62014-03-26 16:39:16 +00001931 /*
1932 * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
1933 * our purposes, the WAIT instruction is used purely to achieve
1934 * "sequence validity" rather than actually implement a delay.
1935 */
1936 writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1937
Lee Jones86f309fd2014-03-20 09:20:35 +00001938 /* Clear FIFO, just in case */
1939 stfsm_clear_fifo(fsm);
1940
1941 return 0;
1942}
1943
Lee Jonesa63984c2014-03-20 09:20:46 +00001944static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1945{
1946 struct stfsm *fsm = platform_get_drvdata(pdev);
1947 struct device_node *np = pdev->dev.of_node;
1948 struct regmap *regmap;
1949 uint32_t boot_device_reg;
1950 uint32_t boot_device_spi;
1951 uint32_t boot_device; /* Value we read from *boot_device_reg */
1952 int ret;
1953
1954 /* Booting from SPI NOR Flash is the default */
1955 fsm->booted_from_spi = true;
1956
1957 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1958 if (IS_ERR(regmap))
1959 goto boot_device_fail;
1960
Lee Jones0ea7d702014-03-20 09:20:50 +00001961 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1962
1963 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1964
Lee Jonesa63984c2014-03-20 09:20:46 +00001965 /* Where in the syscon the boot device information lives */
1966 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1967 if (ret)
1968 goto boot_device_fail;
1969
1970 /* Boot device value when booted from SPI NOR */
1971 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1972 if (ret)
1973 goto boot_device_fail;
1974
1975 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1976 if (ret)
1977 goto boot_device_fail;
1978
1979 if (boot_device != boot_device_spi)
1980 fsm->booted_from_spi = false;
1981
1982 return;
1983
1984boot_device_fail:
1985 dev_warn(&pdev->dev,
1986 "failed to fetch boot device, assuming boot from SPI\n");
1987}
1988
Lee Jonesd90db4a2014-03-20 09:20:33 +00001989static int stfsm_probe(struct platform_device *pdev)
1990{
1991 struct device_node *np = pdev->dev.of_node;
Lee Jones221cff12014-03-20 09:21:07 +00001992 struct mtd_part_parser_data ppdata;
Lee Jones24fec652014-03-20 09:20:41 +00001993 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001994 struct resource *res;
1995 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00001996 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001997
1998 if (!np) {
1999 dev_err(&pdev->dev, "No DT found\n");
2000 return -EINVAL;
2001 }
Lee Jones221cff12014-03-20 09:21:07 +00002002 ppdata.of_node = np;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002003
2004 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2005 if (!fsm)
2006 return -ENOMEM;
2007
2008 fsm->dev = &pdev->dev;
2009
2010 platform_set_drvdata(pdev, fsm);
2011
2012 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2013 if (!res) {
2014 dev_err(&pdev->dev, "Resource not found\n");
2015 return -ENODEV;
2016 }
2017
2018 fsm->base = devm_ioremap_resource(&pdev->dev, res);
2019 if (IS_ERR(fsm->base)) {
2020 dev_err(&pdev->dev,
2021 "Failed to reserve memory region %pR\n", res);
2022 return PTR_ERR(fsm->base);
2023 }
2024
2025 mutex_init(&fsm->lock);
2026
Lee Jones86f309fd2014-03-20 09:20:35 +00002027 ret = stfsm_init(fsm);
2028 if (ret) {
2029 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2030 return ret;
2031 }
2032
Lee Jonesa63984c2014-03-20 09:20:46 +00002033 stfsm_fetch_platform_configs(pdev);
2034
Lee Jones1bd512b2014-03-20 09:20:38 +00002035 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00002036 info = stfsm_jedec_probe(fsm);
2037 if (!info)
2038 return -ENODEV;
2039 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00002040
Lee Jones3b5d1982014-03-20 09:20:43 +00002041 /* Use device size to determine address width */
2042 if (info->sector_size * info->n_sectors > 0x1000000)
2043 info->flags |= FLASH_FLAG_32BIT_ADDR;
2044
Lee Jones218b8702014-03-20 09:20:55 +00002045 /*
2046 * Configure READ/WRITE/ERASE sequences according to platform and
2047 * device flags.
2048 */
2049 if (info->config) {
2050 ret = info->config(fsm);
2051 if (ret)
2052 return ret;
Lee Jones4eb3f0d82014-03-20 09:20:56 +00002053 } else {
2054 ret = stfsm_prepare_rwe_seqs_default(fsm);
2055 if (ret)
2056 return ret;
Lee Jones218b8702014-03-20 09:20:55 +00002057 }
2058
Lee Jones221cff12014-03-20 09:21:07 +00002059 fsm->mtd.name = info->name;
Lee Jonesd90db4a2014-03-20 09:20:33 +00002060 fsm->mtd.dev.parent = &pdev->dev;
2061 fsm->mtd.type = MTD_NORFLASH;
2062 fsm->mtd.writesize = 4;
2063 fsm->mtd.writebufsize = fsm->mtd.writesize;
2064 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00002065 fsm->mtd.size = info->sector_size * info->n_sectors;
2066 fsm->mtd.erasesize = info->sector_size;
2067
Lee Jonese514f102014-03-20 09:20:57 +00002068 fsm->mtd._read = stfsm_mtd_read;
Lee Jones176b4372014-03-20 09:20:59 +00002069 fsm->mtd._write = stfsm_mtd_write;
Lee Jones4a341fe2014-03-20 09:21:00 +00002070 fsm->mtd._erase = stfsm_mtd_erase;
Lee Jonese514f102014-03-20 09:20:57 +00002071
Lee Jones4a341fe2014-03-20 09:21:00 +00002072 dev_info(&pdev->dev,
Lee Jones24fec652014-03-20 09:20:41 +00002073 "Found serial flash device: %s\n"
2074 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
2075 info->name,
2076 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
2077 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00002078
Lee Jones221cff12014-03-20 09:21:07 +00002079 return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002080}
2081
2082static int stfsm_remove(struct platform_device *pdev)
2083{
2084 struct stfsm *fsm = platform_get_drvdata(pdev);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002085
Lee Jonesd9ba46d2014-03-20 11:11:47 +00002086 return mtd_device_unregister(&fsm->mtd);
Lee Jonesd90db4a2014-03-20 09:20:33 +00002087}
2088
2089static struct of_device_id stfsm_match[] = {
2090 { .compatible = "st,spi-fsm", },
2091 {},
2092};
2093MODULE_DEVICE_TABLE(of, stfsm_match);
2094
2095static struct platform_driver stfsm_driver = {
2096 .probe = stfsm_probe,
2097 .remove = stfsm_remove,
2098 .driver = {
2099 .name = "st-spi-fsm",
2100 .owner = THIS_MODULE,
2101 .of_match_table = stfsm_match,
2102 },
2103};
2104module_platform_driver(stfsm_driver);
2105
2106MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2107MODULE_DESCRIPTION("ST SPI FSM driver");
2108MODULE_LICENSE("GPL");