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