blob: 34aac3f49ba9a8f40cbaad6b014d5e7e25ca1c00 [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 *
6 * Copyright (C) 2010-2014 STicroelectronics Limited
7 *
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>
21#include <linux/sched.h>
22#include <linux/delay.h>
23#include <linux/io.h>
24#include <linux/of.h>
25
Lee Jones5549fbd2014-03-20 09:20:39 +000026#include "serial_flash_cmds.h"
27
Lee Jonesbc09fb52014-03-20 09:20:34 +000028/*
29 * FSM SPI Controller Registers
30 */
31#define SPI_CLOCKDIV 0x0010
32#define SPI_MODESELECT 0x0018
33#define SPI_CONFIGDATA 0x0020
34#define SPI_STA_MODE_CHANGE 0x0028
35#define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
36#define SPI_FAST_SEQ_ADD1 0x0104
37#define SPI_FAST_SEQ_ADD2 0x0108
38#define SPI_FAST_SEQ_ADD_CFG 0x010c
39#define SPI_FAST_SEQ_OPC1 0x0110
40#define SPI_FAST_SEQ_OPC2 0x0114
41#define SPI_FAST_SEQ_OPC3 0x0118
42#define SPI_FAST_SEQ_OPC4 0x011c
43#define SPI_FAST_SEQ_OPC5 0x0120
44#define SPI_MODE_BITS 0x0124
45#define SPI_DUMMY_BITS 0x0128
46#define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
47#define SPI_FAST_SEQ_1 0x0130
48#define SPI_FAST_SEQ_2 0x0134
49#define SPI_FAST_SEQ_3 0x0138
50#define SPI_FAST_SEQ_4 0x013c
51#define SPI_FAST_SEQ_CFG 0x0140
52#define SPI_FAST_SEQ_STA 0x0144
53#define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
54#define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
55#define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
56#define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
57#define SPI_PROGRAM_ERASE_TIME 0x0158
58#define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
59#define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
60#define SPI_STATUS_WR_TIME_REG 0x0164
61#define SPI_FAST_SEQ_DATA_REG 0x0300
62
63/*
64 * Register: SPI_MODESELECT
65 */
66#define SPI_MODESELECT_CONTIG 0x01
67#define SPI_MODESELECT_FASTREAD 0x02
68#define SPI_MODESELECT_DUALIO 0x04
69#define SPI_MODESELECT_FSM 0x08
70#define SPI_MODESELECT_QUADBOOT 0x10
71
72/*
73 * Register: SPI_CONFIGDATA
74 */
75#define SPI_CFG_DEVICE_ST 0x1
76#define SPI_CFG_DEVICE_ATMEL 0x4
77#define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
78#define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
79#define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
80
Lee Jones86f309fd2014-03-20 09:20:35 +000081#define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
82#define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
83#define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
84
Lee Jonesbc09fb52014-03-20 09:20:34 +000085/*
86 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
87 */
88#define TRANSFER_SIZE(x) ((x) * 8)
89
90/*
91 * Register: SPI_FAST_SEQ_ADD_CFG
92 */
93#define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
94#define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
95#define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
96#define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
97#define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
98#define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
99#define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
100#define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
101#define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
102#define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
103
104/*
105 * Register: SPI_FAST_SEQ_n
106 */
107#define SEQ_OPC_OPCODE(x) ((x) << 0)
108#define SEQ_OPC_CYCLES(x) ((x) << 8)
109#define SEQ_OPC_PADS_1 (0x0 << 14)
110#define SEQ_OPC_PADS_2 (0x1 << 14)
111#define SEQ_OPC_PADS_4 (0x3 << 14)
112#define SEQ_OPC_CSDEASSERT (1 << 16)
113
114/*
115 * Register: SPI_FAST_SEQ_CFG
116 */
117#define SEQ_CFG_STARTSEQ (1 << 0)
118#define SEQ_CFG_SWRESET (1 << 5)
119#define SEQ_CFG_CSDEASSERT (1 << 6)
120#define SEQ_CFG_READNOTWRITE (1 << 7)
121#define SEQ_CFG_ERASE (1 << 8)
122#define SEQ_CFG_PADS_1 (0x0 << 16)
123#define SEQ_CFG_PADS_2 (0x1 << 16)
124#define SEQ_CFG_PADS_4 (0x3 << 16)
125
126/*
127 * Register: SPI_MODE_BITS
128 */
129#define MODE_DATA(x) (x & 0xff)
130#define MODE_CYCLES(x) ((x & 0x3f) << 16)
131#define MODE_PADS_1 (0x0 << 22)
132#define MODE_PADS_2 (0x1 << 22)
133#define MODE_PADS_4 (0x3 << 22)
134#define DUMMY_CSDEASSERT (1 << 24)
135
136/*
137 * Register: SPI_DUMMY_BITS
138 */
139#define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
140#define DUMMY_PADS_1 (0x0 << 22)
141#define DUMMY_PADS_2 (0x1 << 22)
142#define DUMMY_PADS_4 (0x3 << 22)
143#define DUMMY_CSDEASSERT (1 << 24)
144
145/*
146 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
147 */
148#define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
149#define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
150#define STA_PADS_1 (0x0 << 16)
151#define STA_PADS_2 (0x1 << 16)
152#define STA_PADS_4 (0x3 << 16)
153#define STA_CSDEASSERT (0x1 << 20)
154#define STA_RDNOTWR (0x1 << 21)
155
156/*
157 * FSM SPI Instruction Opcodes
158 */
159#define STFSM_OPC_CMD 0x1
160#define STFSM_OPC_ADD 0x2
161#define STFSM_OPC_STA 0x3
162#define STFSM_OPC_MODE 0x4
163#define STFSM_OPC_DUMMY 0x5
164#define STFSM_OPC_DATA 0x6
165#define STFSM_OPC_WAIT 0x7
166#define STFSM_OPC_JUMP 0x8
167#define STFSM_OPC_GOTO 0x9
168#define STFSM_OPC_STOP 0xF
169
170/*
171 * FSM SPI Instructions (== opcode + operand).
172 */
173#define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
174
175#define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
176#define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
177#define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
178#define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
179#define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
180#define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
181#define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
182
183#define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
184#define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
185
186#define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
187#define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
188#define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
189#define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
190
191#define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
192#define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
193#define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
194#define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
195
Lee Jones86f309fd2014-03-20 09:20:35 +0000196#define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
197#define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
198
199#define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
200
Lee Jones3c8b85b2014-03-20 09:20:36 +0000201#define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
202
Lee Jonese85a6192014-03-20 09:20:54 +0000203/* Flash Commands */
204#define FLASH_CMD_WREN 0x06
205#define FLASH_CMD_WRDI 0x04
206#define FLASH_CMD_RDID 0x9f
207#define FLASH_CMD_RDSR 0x05
208#define FLASH_CMD_RDSR2 0x35
209#define FLASH_CMD_WRSR 0x01
210#define FLASH_CMD_SE_4K 0x20
211#define FLASH_CMD_SE_32K 0x52
212#define FLASH_CMD_SE 0xd8
213#define FLASH_CMD_CHIPERASE 0xc7
214#define FLASH_CMD_WRVCR 0x81
215#define FLASH_CMD_RDVCR 0x85
216
217#define FLASH_CMD_READ 0x03 /* READ */
218#define FLASH_CMD_READ_FAST 0x0b /* FAST READ */
219#define FLASH_CMD_READ_1_1_2 0x3b /* DUAL OUTPUT READ */
220#define FLASH_CMD_READ_1_2_2 0xbb /* DUAL I/O READ */
221#define FLASH_CMD_READ_1_1_4 0x6b /* QUAD OUTPUT READ */
222#define FLASH_CMD_READ_1_4_4 0xeb /* QUAD I/O READ */
223
224#define FLASH_CMD_WRITE 0x02 /* PAGE PROGRAM */
225#define FLASH_CMD_WRITE_1_1_2 0xa2 /* DUAL INPUT PROGRAM */
226#define FLASH_CMD_WRITE_1_2_2 0xd2 /* DUAL INPUT EXT PROGRAM */
227#define FLASH_CMD_WRITE_1_1_4 0x32 /* QUAD INPUT PROGRAM */
228#define FLASH_CMD_WRITE_1_4_4 0x12 /* QUAD INPUT EXT PROGRAM */
229
230#define FLASH_CMD_EN4B_ADDR 0xb7 /* Enter 4-byte address mode */
231#define FLASH_CMD_EX4B_ADDR 0xe9 /* Exit 4-byte address mode */
232
233/* READ commands with 32-bit addressing (N25Q256 and S25FLxxxS) */
234#define FLASH_CMD_READ4 0x13
235#define FLASH_CMD_READ4_FAST 0x0c
236#define FLASH_CMD_READ4_1_1_2 0x3c
237#define FLASH_CMD_READ4_1_2_2 0xbc
238#define FLASH_CMD_READ4_1_1_4 0x6c
239#define FLASH_CMD_READ4_1_4_4 0xec
240
Lee Jones176b4372014-03-20 09:20:59 +0000241/* Status register */
242#define FLASH_STATUS_BUSY 0x01
243#define FLASH_STATUS_WEL 0x02
244#define FLASH_STATUS_BP0 0x04
245#define FLASH_STATUS_BP1 0x08
246#define FLASH_STATUS_BP2 0x10
247#define FLASH_STATUS_SRWP0 0x80
248#define FLASH_STATUS_TIMEOUT 0xff
249
Lee Jonese514f102014-03-20 09:20:57 +0000250#define FLASH_PAGESIZE 256 /* In Bytes */
251#define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
Lee Jones176b4372014-03-20 09:20:59 +0000252#define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
Lee Jonese514f102014-03-20 09:20:57 +0000253
Lee Jonese85a6192014-03-20 09:20:54 +0000254/*
255 * Flags to tweak operation of default read/write/erase routines
256 */
257#define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
258#define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
259#define CFG_WRITE_EX_32BIT_ADDR_DELAY 0x00000004
260#define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
261#define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
262
Lee Jonesd90db4a2014-03-20 09:20:33 +0000263struct stfsm {
264 struct device *dev;
265 void __iomem *base;
266 struct resource *region;
267 struct mtd_info mtd;
268 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000269 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000270
Lee Jonese85a6192014-03-20 09:20:54 +0000271 uint32_t configuration;
Lee Jones86f309fd2014-03-20 09:20:35 +0000272 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000273 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000274 bool reset_signal;
275 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000276};
277
Lee Jones3c8b85b2014-03-20 09:20:36 +0000278struct stfsm_seq {
279 uint32_t data_size;
280 uint32_t addr1;
281 uint32_t addr2;
282 uint32_t addr_cfg;
283 uint32_t seq_opc[5];
284 uint32_t mode;
285 uint32_t dummy;
286 uint32_t status;
287 uint8_t seq[16];
288 uint32_t seq_cfg;
289} __packed __aligned(4);
290
Lee Jones08981272014-03-20 09:20:42 +0000291/* Parameters to configure a READ or WRITE FSM sequence */
292struct seq_rw_config {
293 uint32_t flags; /* flags to support config */
294 uint8_t cmd; /* FLASH command */
295 int write; /* Write Sequence */
296 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
297 uint8_t data_pads; /* No. of data pads */
298 uint8_t mode_data; /* MODE data */
299 uint8_t mode_cycles; /* No. of MODE cycles */
300 uint8_t dummy_cycles; /* No. of DUMMY cycles */
301};
302
Lee Jones11d7f822014-03-20 09:20:40 +0000303/* SPI Flash Device Table */
304struct flash_info {
305 char *name;
306 /*
307 * JEDEC id zero means "no ID" (most older chips); otherwise it has
308 * a high byte of zero plus three data bytes: the manufacturer id,
309 * then a two byte device id.
310 */
311 u32 jedec_id;
312 u16 ext_id;
313 /*
314 * The size listed here is what works with FLASH_CMD_SE, which isn't
315 * necessarily called a "sector" by the vendor.
316 */
317 unsigned sector_size;
318 u16 n_sectors;
319 u32 flags;
320 /*
321 * Note, where FAST_READ is supported, freq_max specifies the
322 * FAST_READ frequency, not the READ frequency.
323 */
324 u32 max_freq;
325 int (*config)(struct stfsm *);
326};
327
Lee Jones218b8702014-03-20 09:20:55 +0000328static int stfsm_n25q_config(struct stfsm *fsm);
329
Lee Jones11d7f822014-03-20 09:20:40 +0000330static struct flash_info flash_types[] = {
331 /*
332 * ST Microelectronics/Numonyx --
333 * (newer production versions may have feature updates
334 * (eg faster operating frequency)
335 */
336#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
337 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
338 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
339 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
340 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
341 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
342 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
343
344#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
345 FLASH_FLAG_READ_FAST | \
346 FLASH_FLAG_READ_1_1_2 | \
347 FLASH_FLAG_WRITE_1_1_2)
348 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
349 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
350
351#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
352 FLASH_FLAG_READ_FAST | \
353 FLASH_FLAG_READ_1_1_2 | \
354 FLASH_FLAG_READ_1_2_2 | \
355 FLASH_FLAG_READ_1_1_4 | \
356 FLASH_FLAG_READ_1_4_4 | \
357 FLASH_FLAG_SE_4K | \
358 FLASH_FLAG_SE_32K)
359 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
360 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
361
362#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
363 FLASH_FLAG_READ_FAST | \
364 FLASH_FLAG_READ_1_1_2 | \
365 FLASH_FLAG_READ_1_2_2 | \
366 FLASH_FLAG_READ_1_1_4 | \
367 FLASH_FLAG_READ_1_4_4 | \
368 FLASH_FLAG_WRITE_1_1_2 | \
369 FLASH_FLAG_WRITE_1_2_2 | \
370 FLASH_FLAG_WRITE_1_1_4 | \
371 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000372 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
373 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000374 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000375 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000376
377 /*
378 * Spansion S25FLxxxP
379 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
380 */
381#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
382 FLASH_FLAG_READ_1_1_2 | \
383 FLASH_FLAG_READ_1_2_2 | \
384 FLASH_FLAG_READ_1_1_4 | \
385 FLASH_FLAG_READ_1_4_4 | \
386 FLASH_FLAG_WRITE_1_1_4 | \
387 FLASH_FLAG_READ_FAST)
388 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
389 NULL },
390 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
391 NULL },
392
393 /*
394 * Spansion S25FLxxxS
395 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
396 * - RESET# signal supported by die but not bristled out on all
397 * package types. The package type is a function of board design,
398 * so this information is captured in the board's flags.
399 * - Supports 'DYB' sector protection. Depending on variant, sectors
400 * may default to locked state on power-on.
401 */
402#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
403 FLASH_FLAG_RESET | \
404 FLASH_FLAG_DYB_LOCKING)
405 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
406 NULL },
407 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
408 NULL },
409 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
410 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
411 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
412 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
413
414 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
415#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
416 FLASH_FLAG_READ_FAST | \
417 FLASH_FLAG_READ_1_1_2 | \
418 FLASH_FLAG_WRITE_1_1_2)
419 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
420 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
421 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
422 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
423 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
424
425 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
426#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
427 FLASH_FLAG_READ_FAST | \
428 FLASH_FLAG_READ_1_1_2 | \
429 FLASH_FLAG_READ_1_2_2 | \
430 FLASH_FLAG_READ_1_1_4 | \
431 FLASH_FLAG_READ_1_4_4 | \
432 FLASH_FLAG_WRITE_1_1_4)
433 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
434 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
435 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
436 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
437
438 /* Sentinel */
439 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
440};
441
Lee Jonesa37b2f52014-03-20 09:20:53 +0000442/*
443 * FSM message sequence configurations:
444 *
445 * All configs are presented in order of preference
446 */
447
448/* Default READ configurations, in order of preference */
449static struct seq_rw_config default_read_configs[] = {
450 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
451 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
452 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
453 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
454 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
455 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
456 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
457};
458
459/* Default WRITE configurations */
460static struct seq_rw_config default_write_configs[] = {
461 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
462 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
463 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
464 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
465 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
466 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
467};
468
Lee Jonese85a6192014-03-20 09:20:54 +0000469/*
470 * [N25Qxxx] Configuration
471 */
472#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
473#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
474#define N25Q_VCR_WRAP_CONT 0x3
475
476/* N25Q 3-byte Address READ configurations
477 * - 'FAST' variants configured for 8 dummy cycles.
478 *
479 * Note, the number of dummy cycles used for 'FAST' READ operations is
480 * configurable and would normally be tuned according to the READ command and
481 * operating frequency. However, this applies universally to all 'FAST' READ
482 * commands, including those used by the SPIBoot controller, and remains in
483 * force until the device is power-cycled. Since the SPIBoot controller is
484 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
485 * cycles.
486 */
487static struct seq_rw_config n25q_read3_configs[] = {
488 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
489 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
490 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
491 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
492 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
493 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
494 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
495};
496
497/* N25Q 4-byte Address READ configurations
498 * - use special 4-byte address READ commands (reduces overheads, and
499 * reduces risk of hitting watchdog reset issues).
500 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
501 */
502static struct seq_rw_config n25q_read4_configs[] = {
503 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
504 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
505 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
506 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
507 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
508 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
509 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
510};
511
Lee Jones218b8702014-03-20 09:20:55 +0000512static struct stfsm_seq stfsm_seq_read; /* Dynamically populated */
513static struct stfsm_seq stfsm_seq_write; /* Dynamically populated */
Lee Jones0de08e42014-03-20 09:20:51 +0000514static struct stfsm_seq stfsm_seq_en_32bit_addr;/* Dynamically populated */
515
Lee Jones1bd512b2014-03-20 09:20:38 +0000516static struct stfsm_seq stfsm_seq_read_jedec = {
517 .data_size = TRANSFER_SIZE(8),
518 .seq_opc[0] = (SEQ_OPC_PADS_1 |
519 SEQ_OPC_CYCLES(8) |
520 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
521 .seq = {
522 STFSM_INST_CMD1,
523 STFSM_INST_DATA_READ,
524 STFSM_INST_STOP,
525 },
526 .seq_cfg = (SEQ_CFG_PADS_1 |
527 SEQ_CFG_READNOTWRITE |
528 SEQ_CFG_CSDEASSERT |
529 SEQ_CFG_STARTSEQ),
530};
531
Lee Jones176b4372014-03-20 09:20:59 +0000532static struct stfsm_seq stfsm_seq_read_status_fifo = {
533 .data_size = TRANSFER_SIZE(4),
534 .seq_opc[0] = (SEQ_OPC_PADS_1 |
535 SEQ_OPC_CYCLES(8) |
536 SEQ_OPC_OPCODE(FLASH_CMD_RDSR)),
537 .seq = {
538 STFSM_INST_CMD1,
539 STFSM_INST_DATA_READ,
540 STFSM_INST_STOP,
541 },
542 .seq_cfg = (SEQ_CFG_PADS_1 |
543 SEQ_CFG_READNOTWRITE |
544 SEQ_CFG_CSDEASSERT |
545 SEQ_CFG_STARTSEQ),
546};
547
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000548static struct stfsm_seq stfsm_seq_erase_sector = {
549 /* 'addr_cfg' configured during initialisation */
550 .seq_opc = {
551 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
552 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
553
554 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
555 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
556 },
557 .seq = {
558 STFSM_INST_CMD1,
559 STFSM_INST_CMD2,
560 STFSM_INST_ADD1,
561 STFSM_INST_ADD2,
562 STFSM_INST_STOP,
563 },
564 .seq_cfg = (SEQ_CFG_PADS_1 |
565 SEQ_CFG_READNOTWRITE |
566 SEQ_CFG_CSDEASSERT |
567 SEQ_CFG_STARTSEQ),
568};
569
Lee Jones4a341fe2014-03-20 09:21:00 +0000570static struct stfsm_seq stfsm_seq_erase_chip = {
571 .seq_opc = {
572 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
573 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
574
575 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
576 SEQ_OPC_OPCODE(FLASH_CMD_CHIPERASE) | SEQ_OPC_CSDEASSERT),
577 },
578 .seq = {
579 STFSM_INST_CMD1,
580 STFSM_INST_CMD2,
581 STFSM_INST_WAIT,
582 STFSM_INST_STOP,
583 },
584 .seq_cfg = (SEQ_CFG_PADS_1 |
585 SEQ_CFG_ERASE |
586 SEQ_CFG_READNOTWRITE |
587 SEQ_CFG_CSDEASSERT |
588 SEQ_CFG_STARTSEQ),
589};
590
Lee Jones249516c2014-03-20 09:20:52 +0000591static struct stfsm_seq stfsm_seq_wrvcr = {
592 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
593 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
594 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
595 SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
596 .seq = {
597 STFSM_INST_CMD1,
598 STFSM_INST_CMD2,
599 STFSM_INST_STA_WR1,
600 STFSM_INST_STOP,
601 },
602 .seq_cfg = (SEQ_CFG_PADS_1 |
603 SEQ_CFG_READNOTWRITE |
604 SEQ_CFG_CSDEASSERT |
605 SEQ_CFG_STARTSEQ),
606};
607
Lee Jones6bd29602014-03-20 09:20:48 +0000608static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
609{
610 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
611 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
612 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
613 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
614 SEQ_OPC_CSDEASSERT);
615
616 seq->seq[0] = STFSM_INST_CMD2;
617 seq->seq[1] = STFSM_INST_CMD1;
618 seq->seq[2] = STFSM_INST_WAIT;
619 seq->seq[3] = STFSM_INST_STOP;
620
621 seq->seq_cfg = (SEQ_CFG_PADS_1 |
622 SEQ_CFG_ERASE |
623 SEQ_CFG_READNOTWRITE |
624 SEQ_CFG_CSDEASSERT |
625 SEQ_CFG_STARTSEQ);
626
627 return 0;
628}
629
Lee Jones3c8b85b2014-03-20 09:20:36 +0000630static inline int stfsm_is_idle(struct stfsm *fsm)
631{
632 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
633}
634
Lee Jones86f309fd2014-03-20 09:20:35 +0000635static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
636{
637 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
638}
639
640static void stfsm_clear_fifo(struct stfsm *fsm)
641{
642 uint32_t avail;
643
644 for (;;) {
645 avail = stfsm_fifo_available(fsm);
646 if (!avail)
647 break;
648
649 while (avail) {
650 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
651 avail--;
652 }
653 }
654}
655
Lee Jones3c8b85b2014-03-20 09:20:36 +0000656static inline void stfsm_load_seq(struct stfsm *fsm,
657 const struct stfsm_seq *seq)
658{
659 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
660 const uint32_t *src = (const uint32_t *)seq;
661 int words = sizeof(*seq) / sizeof(*src);
662
663 BUG_ON(!stfsm_is_idle(fsm));
664
665 while (words--) {
666 writel(*src, dst);
667 src++;
668 dst += 4;
669 }
670}
671
672static void stfsm_wait_seq(struct stfsm *fsm)
673{
674 unsigned long deadline;
675 int timeout = 0;
676
677 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
678
679 while (!timeout) {
680 if (time_after_eq(jiffies, deadline))
681 timeout = 1;
682
683 if (stfsm_is_idle(fsm))
684 return;
685
686 cond_resched();
687 }
688
689 dev_err(fsm->dev, "timeout on sequence completion\n");
690}
691
Lee Jones030e82d2014-03-20 09:20:37 +0000692static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
693 const uint32_t size)
694{
695 uint32_t remaining = size >> 2;
696 uint32_t avail;
697 uint32_t words;
698
699 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
700
701 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
702
703 while (remaining) {
704 for (;;) {
705 avail = stfsm_fifo_available(fsm);
706 if (avail)
707 break;
708 udelay(1);
709 }
710 words = min(avail, remaining);
711 remaining -= words;
712
713 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
714 buf += words;
715 }
716}
717
Lee Jones30ca64f2014-03-20 09:20:58 +0000718static int stfsm_write_fifo(struct stfsm *fsm,
719 const uint32_t *buf, const uint32_t size)
720{
721 uint32_t words = size >> 2;
722
723 dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
724
725 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
726
727 writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
728
729 return size;
730}
731
Lee Jones0de08e42014-03-20 09:20:51 +0000732static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
733{
734 struct stfsm_seq *seq = &stfsm_seq_en_32bit_addr;
735 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
736
737 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
738 SEQ_OPC_CYCLES(8) |
739 SEQ_OPC_OPCODE(cmd) |
740 SEQ_OPC_CSDEASSERT);
741
742 stfsm_load_seq(fsm, seq);
743
744 stfsm_wait_seq(fsm);
745
746 return 0;
747}
748
Lee Jones176b4372014-03-20 09:20:59 +0000749static uint8_t stfsm_wait_busy(struct stfsm *fsm)
750{
751 struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
752 unsigned long deadline;
753 uint32_t status;
754 int timeout = 0;
755
756 /* Use RDRS1 */
757 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
758 SEQ_OPC_CYCLES(8) |
759 SEQ_OPC_OPCODE(FLASH_CMD_RDSR));
760
761 /* Load read_status sequence */
762 stfsm_load_seq(fsm, seq);
763
764 /*
765 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
766 */
767 deadline = jiffies + FLASH_MAX_BUSY_WAIT;
768 while (!timeout) {
769 cond_resched();
770
771 if (time_after_eq(jiffies, deadline))
772 timeout = 1;
773
774 stfsm_wait_seq(fsm);
775
776 stfsm_read_fifo(fsm, &status, 4);
777
778 if ((status & FLASH_STATUS_BUSY) == 0)
779 return 0;
780
781 if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
782 ((status & S25FL_STATUS_P_ERR) ||
783 (status & S25FL_STATUS_E_ERR)))
784 return (uint8_t)(status & 0xff);
785
786 if (!timeout)
787 /* Restart */
788 writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
789 }
790
791 dev_err(fsm->dev, "timeout on wait_busy\n");
792
793 return FLASH_STATUS_TIMEOUT;
794}
795
Lee Jones249516c2014-03-20 09:20:52 +0000796static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
797{
798 struct stfsm_seq *seq = &stfsm_seq_wrvcr;
799
800 dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
801
802 seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
803
804 stfsm_load_seq(fsm, seq);
805
806 stfsm_wait_seq(fsm);
807
808 return 0;
809}
810
Lee Jones0ea7d702014-03-20 09:20:50 +0000811/*
812 * SoC reset on 'boot-from-spi' systems
813 *
814 * Certain modes of operation cause the Flash device to enter a particular state
815 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
816 * Addr' commands). On boot-from-spi systems, it is important to consider what
817 * happens if a warm reset occurs during this period. The SPIBoot controller
818 * assumes that Flash device is in its default reset state, 24-bit address mode,
819 * and ready to accept commands. This can be achieved using some form of
820 * on-board logic/controller to force a device POR in response to a SoC-level
821 * reset or by making use of the device reset signal if available (limited
822 * number of devices only).
823 *
824 * Failure to take such precautions can cause problems following a warm reset.
825 * For some operations (e.g. ERASE), there is little that can be done. For
826 * other modes of operation (e.g. 32-bit addressing), options are often
827 * available that can help minimise the window in which a reset could cause a
828 * problem.
829 *
830 */
831static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
832{
833 /* Reset signal is available on the board and supported by the device */
834 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
835 return true;
836
837 /* Board-level logic forces a power-on-reset */
838 if (fsm->reset_por)
839 return true;
840
841 /* Reset is not properly handled and may result in failure to reboot */
842 return false;
843}
844
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000845/* Configure 'addr_cfg' according to addressing mode */
846static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
847 struct stfsm_seq *seq)
848{
849 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
850
851 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
852 ADR_CFG_PADS_1_ADD1 |
853 ADR_CFG_CYCLES_ADD2(16) |
854 ADR_CFG_PADS_1_ADD2 |
855 ADR_CFG_CSDEASSERT_ADD2);
856}
857
Lee Jones08981272014-03-20 09:20:42 +0000858/* Search for preferred configuration based on available flags */
859static struct seq_rw_config *
860stfsm_search_seq_rw_configs(struct stfsm *fsm,
861 struct seq_rw_config cfgs[])
862{
863 struct seq_rw_config *config;
864 int flags = fsm->info->flags;
865
866 for (config = cfgs; config->cmd != 0; config++)
867 if ((config->flags & flags) == config->flags)
868 return config;
869
870 return NULL;
871}
872
Lee Jones97ccf2d2014-03-20 09:20:44 +0000873/* Prepare a READ/WRITE sequence according to configuration parameters */
874static void stfsm_prepare_rw_seq(struct stfsm *fsm,
875 struct stfsm_seq *seq,
876 struct seq_rw_config *cfg)
877{
878 int addr1_cycles, addr2_cycles;
879 int i = 0;
880
881 memset(seq, 0, sizeof(*seq));
882
883 /* Add READ/WRITE OPC */
884 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
885 SEQ_OPC_CYCLES(8) |
886 SEQ_OPC_OPCODE(cfg->cmd));
887
888 /* Add WREN OPC for a WRITE sequence */
889 if (cfg->write)
890 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
891 SEQ_OPC_CYCLES(8) |
892 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
893 SEQ_OPC_CSDEASSERT);
894
895 /* Address configuration (24 or 32-bit addresses) */
896 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
897 addr1_cycles /= cfg->addr_pads;
898 addr2_cycles = 16 / cfg->addr_pads;
899 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
900 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
901 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
902 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
903
904 /* Data/Sequence configuration */
905 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
906 SEQ_CFG_STARTSEQ |
907 SEQ_CFG_CSDEASSERT);
908 if (!cfg->write)
909 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
910
911 /* Mode configuration (no. of pads taken from addr cfg) */
912 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
913 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
914 (cfg->addr_pads - 1) << 22); /* pads */
915
916 /* Dummy configuration (no. of pads taken from addr cfg) */
917 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
918 (cfg->addr_pads - 1) << 22); /* pads */
919
920
921 /* Instruction sequence */
922 i = 0;
923 if (cfg->write)
924 seq->seq[i++] = STFSM_INST_CMD2;
925
926 seq->seq[i++] = STFSM_INST_CMD1;
927
928 seq->seq[i++] = STFSM_INST_ADD1;
929 seq->seq[i++] = STFSM_INST_ADD2;
930
931 if (cfg->mode_cycles)
932 seq->seq[i++] = STFSM_INST_MODE;
933
934 if (cfg->dummy_cycles)
935 seq->seq[i++] = STFSM_INST_DUMMY;
936
937 seq->seq[i++] =
938 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
939 seq->seq[i++] = STFSM_INST_STOP;
940}
941
Lee Jones88cccb82014-03-20 09:20:49 +0000942static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
943 struct stfsm_seq *seq,
944 struct seq_rw_config *cfgs)
945{
946 struct seq_rw_config *config;
947
948 config = stfsm_search_seq_rw_configs(fsm, cfgs);
949 if (!config) {
950 dev_err(fsm->dev, "failed to find suitable config\n");
951 return -EINVAL;
952 }
953
954 stfsm_prepare_rw_seq(fsm, seq, config);
955
956 return 0;
957}
958
Lee Jones4eb3f0d82014-03-20 09:20:56 +0000959/* Prepare a READ/WRITE/ERASE 'default' sequences */
960static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
961{
962 uint32_t flags = fsm->info->flags;
963 int ret;
964
965 /* Configure 'READ' sequence */
966 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
967 default_read_configs);
968 if (ret) {
969 dev_err(fsm->dev,
970 "failed to prep READ sequence with flags [0x%08x]\n",
971 flags);
972 return ret;
973 }
974
975 /* Configure 'WRITE' sequence */
976 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
977 default_write_configs);
978 if (ret) {
979 dev_err(fsm->dev,
980 "failed to prep WRITE sequence with flags [0x%08x]\n",
981 flags);
982 return ret;
983 }
984
985 /* Configure 'ERASE_SECTOR' sequence */
986 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
987
988 return 0;
989}
990
Lee Jones218b8702014-03-20 09:20:55 +0000991static int stfsm_n25q_config(struct stfsm *fsm)
992{
993 uint32_t flags = fsm->info->flags;
994 uint8_t vcr;
995 int ret = 0;
996 bool soc_reset;
997
998 /* Configure 'READ' sequence */
999 if (flags & FLASH_FLAG_32BIT_ADDR)
1000 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
1001 n25q_read4_configs);
1002 else
1003 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
1004 n25q_read3_configs);
1005 if (ret) {
1006 dev_err(fsm->dev,
1007 "failed to prepare READ sequence with flags [0x%08x]\n",
1008 flags);
1009 return ret;
1010 }
1011
1012 /* Configure 'WRITE' sequence (default configs) */
1013 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
1014 default_write_configs);
1015 if (ret) {
1016 dev_err(fsm->dev,
1017 "preparing WRITE sequence using flags [0x%08x] failed\n",
1018 flags);
1019 return ret;
1020 }
1021
1022 /* * Configure 'ERASE_SECTOR' sequence */
1023 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1024
1025 /* Configure 32-bit address support */
1026 if (flags & FLASH_FLAG_32BIT_ADDR) {
1027 stfsm_n25q_en_32bit_addr_seq(&stfsm_seq_en_32bit_addr);
1028
1029 soc_reset = stfsm_can_handle_soc_reset(fsm);
1030 if (soc_reset || !fsm->booted_from_spi) {
1031 /*
1032 * If we can handle SoC resets, we enable 32-bit
1033 * address mode pervasively
1034 */
1035 stfsm_enter_32bit_addr(fsm, 1);
1036 } else {
1037 /*
1038 * If not, enable/disable for WRITE and ERASE
1039 * operations (READ uses special commands)
1040 */
1041 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1042 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1043 }
1044 }
1045
1046 /*
1047 * Configure device to use 8 dummy cycles
1048 */
1049 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1050 N25Q_VCR_WRAP_CONT);
1051 stfsm_wrvcr(fsm, vcr);
1052
1053 return 0;
1054}
1055
Lee Jonese514f102014-03-20 09:20:57 +00001056static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1057 uint32_t offset)
1058{
1059 struct stfsm_seq *seq = &stfsm_seq_read;
1060 uint32_t data_pads;
1061 uint32_t read_mask;
1062 uint32_t size_ub;
1063 uint32_t size_lb;
1064 uint32_t size_mop;
1065 uint32_t tmp[4];
1066 uint32_t page_buf[FLASH_PAGESIZE_32];
1067 uint8_t *p;
1068
1069 dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1070
1071 /* Enter 32-bit address mode, if required */
1072 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1073 stfsm_enter_32bit_addr(fsm, 1);
1074
1075 /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1076 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1077 read_mask = (data_pads << 2) - 1;
1078
1079 /* Handle non-aligned buf */
1080 p = ((uint32_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1081
1082 /* Handle non-aligned size */
1083 size_ub = (size + read_mask) & ~read_mask;
1084 size_lb = size & ~read_mask;
1085 size_mop = size & read_mask;
1086
1087 seq->data_size = TRANSFER_SIZE(size_ub);
1088 seq->addr1 = (offset >> 16) & 0xffff;
1089 seq->addr2 = offset & 0xffff;
1090
1091 stfsm_load_seq(fsm, seq);
1092
1093 if (size_lb)
1094 stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1095
1096 if (size_mop) {
1097 stfsm_read_fifo(fsm, tmp, read_mask + 1);
1098 memcpy(p + size_lb, &tmp, size_mop);
1099 }
1100
1101 /* Handle non-aligned buf */
1102 if ((uint32_t)buf & 0x3)
1103 memcpy(buf, page_buf, size);
1104
1105 /* Wait for sequence to finish */
1106 stfsm_wait_seq(fsm);
1107
1108 stfsm_clear_fifo(fsm);
1109
1110 /* Exit 32-bit address mode, if required */
1111 if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1112 stfsm_enter_32bit_addr(fsm, 0);
1113
1114 return 0;
1115}
1116
Lee Jones176b4372014-03-20 09:20:59 +00001117static int stfsm_write(struct stfsm *fsm, const uint8_t *const buf,
1118 const uint32_t size, const uint32_t offset)
1119{
1120 struct stfsm_seq *seq = &stfsm_seq_write;
1121 uint32_t data_pads;
1122 uint32_t write_mask;
1123 uint32_t size_ub;
1124 uint32_t size_lb;
1125 uint32_t size_mop;
1126 uint32_t tmp[4];
1127 uint32_t page_buf[FLASH_PAGESIZE_32];
1128 uint8_t *t = (uint8_t *)&tmp;
1129 const uint8_t *p;
1130 int ret;
1131 int i;
1132
1133 dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1134
1135 /* Enter 32-bit address mode, if required */
1136 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1137 stfsm_enter_32bit_addr(fsm, 1);
1138
1139 /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1140 data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1141 write_mask = (data_pads << 2) - 1;
1142
1143 /* Handle non-aligned buf */
1144 if ((uint32_t)buf & 0x3) {
1145 memcpy(page_buf, buf, size);
1146 p = (uint8_t *)page_buf;
1147 } else {
1148 p = buf;
1149 }
1150
1151 /* Handle non-aligned size */
1152 size_ub = (size + write_mask) & ~write_mask;
1153 size_lb = size & ~write_mask;
1154 size_mop = size & write_mask;
1155
1156 seq->data_size = TRANSFER_SIZE(size_ub);
1157 seq->addr1 = (offset >> 16) & 0xffff;
1158 seq->addr2 = offset & 0xffff;
1159
1160 /* Need to set FIFO to write mode, before writing data to FIFO (see
1161 * GNBvb79594)
1162 */
1163 writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1164
1165 /*
1166 * Before writing data to the FIFO, apply a small delay to allow a
1167 * potential change of FIFO direction to complete.
1168 */
1169 if (fsm->fifo_dir_delay == 0)
1170 readl(fsm->base + SPI_FAST_SEQ_CFG);
1171 else
1172 udelay(fsm->fifo_dir_delay);
1173
1174
1175 /* Write data to FIFO, before starting sequence (see GNBvd79593) */
1176 if (size_lb) {
1177 stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1178 p += size_lb;
1179 }
1180
1181 /* Handle non-aligned size */
1182 if (size_mop) {
1183 memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
1184 for (i = 0; i < size_mop; i++)
1185 t[i] = *p++;
1186
1187 stfsm_write_fifo(fsm, tmp, write_mask + 1);
1188 }
1189
1190 /* Start sequence */
1191 stfsm_load_seq(fsm, seq);
1192
1193 /* Wait for sequence to finish */
1194 stfsm_wait_seq(fsm);
1195
1196 /* Wait for completion */
1197 ret = stfsm_wait_busy(fsm);
1198
1199 /* Exit 32-bit address mode, if required */
1200 if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR) {
1201 stfsm_enter_32bit_addr(fsm, 0);
1202 if (fsm->configuration & CFG_WRITE_EX_32BIT_ADDR_DELAY)
1203 udelay(1);
1204 }
1205
1206 return 0;
1207}
1208
Lee Jonese514f102014-03-20 09:20:57 +00001209/*
1210 * Read an address range from the flash chip. The address range
1211 * may be any size provided it is within the physical boundaries.
1212 */
1213static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1214 size_t *retlen, u_char *buf)
1215{
1216 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1217 uint32_t bytes;
1218
1219 dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1220 __func__, (u32)from, len);
1221
1222 mutex_lock(&fsm->lock);
1223
1224 while (len > 0) {
1225 bytes = min_t(size_t, len, FLASH_PAGESIZE);
1226
1227 stfsm_read(fsm, buf, bytes, from);
1228
1229 buf += bytes;
1230 from += bytes;
1231 len -= bytes;
1232
1233 *retlen += bytes;
1234 }
1235
1236 mutex_unlock(&fsm->lock);
1237
1238 return 0;
1239}
1240
Lee Jones4a341fe2014-03-20 09:21:00 +00001241static int stfsm_erase_sector(struct stfsm *fsm, const uint32_t offset)
1242{
1243 struct stfsm_seq *seq = &stfsm_seq_erase_sector;
1244 int ret;
1245
1246 dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
1247
1248 /* Enter 32-bit address mode, if required */
1249 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1250 stfsm_enter_32bit_addr(fsm, 1);
1251
1252 seq->addr1 = (offset >> 16) & 0xffff;
1253 seq->addr2 = offset & 0xffff;
1254
1255 stfsm_load_seq(fsm, seq);
1256
1257 stfsm_wait_seq(fsm);
1258
1259 /* Wait for completion */
1260 ret = stfsm_wait_busy(fsm);
1261
1262 /* Exit 32-bit address mode, if required */
1263 if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
1264 stfsm_enter_32bit_addr(fsm, 0);
1265
1266 return ret;
1267}
1268
1269static int stfsm_erase_chip(struct stfsm *fsm)
1270{
1271 const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
1272
1273 dev_dbg(fsm->dev, "erasing chip\n");
1274
1275 stfsm_load_seq(fsm, seq);
1276
1277 stfsm_wait_seq(fsm);
1278
1279 return stfsm_wait_busy(fsm);
1280}
1281
Lee Jones176b4372014-03-20 09:20:59 +00001282/*
1283 * Write an address range to the flash chip. Data must be written in
1284 * FLASH_PAGESIZE chunks. The address range may be any size provided
1285 * it is within the physical boundaries.
1286 */
1287static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1288 size_t *retlen, const u_char *buf)
1289{
1290 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1291
1292 u32 page_offs;
1293 u32 bytes;
1294 uint8_t *b = (uint8_t *)buf;
1295 int ret = 0;
1296
1297 dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1298
1299 *retlen = 0;
1300
1301 if (!len)
1302 return 0;
1303
1304 if (to + len > mtd->size)
1305 return -EINVAL;
1306
1307 /* Offset within page */
1308 page_offs = to % FLASH_PAGESIZE;
1309
1310 mutex_lock(&fsm->lock);
1311
1312 while (len) {
1313 /* Write up to page boundary */
1314 bytes = min(FLASH_PAGESIZE - page_offs, len);
1315
1316 ret = stfsm_write(fsm, b, bytes, to);
1317 if (ret)
1318 goto out1;
1319
1320 b += bytes;
1321 len -= bytes;
1322 to += bytes;
1323
1324 /* We are now page-aligned */
1325 page_offs = 0;
1326
1327 *retlen += bytes;
1328
1329 }
1330
1331out1:
1332 mutex_unlock(&fsm->lock);
1333
1334 return ret;
1335}
1336
Lee Jones4a341fe2014-03-20 09:21:00 +00001337/*
1338 * Erase an address range on the flash chip. The address range may extend
1339 * one or more erase sectors. Return an error is there is a problem erasing.
1340 */
1341static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
1342{
1343 struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1344 u32 addr, len;
1345 int ret;
1346
1347 dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
1348 (long long)instr->addr, (long long)instr->len);
1349
1350 addr = instr->addr;
1351 len = instr->len;
1352
1353 mutex_lock(&fsm->lock);
1354
1355 /* Whole-chip erase? */
1356 if (len == mtd->size) {
1357 ret = stfsm_erase_chip(fsm);
1358 if (ret)
1359 goto out1;
1360 } else {
1361 while (len) {
1362 ret = stfsm_erase_sector(fsm, addr);
1363 if (ret)
1364 goto out1;
1365
1366 addr += mtd->erasesize;
1367 len -= mtd->erasesize;
1368 }
1369 }
1370
1371 mutex_unlock(&fsm->lock);
1372
1373 instr->state = MTD_ERASE_DONE;
1374 mtd_erase_callback(instr);
1375
1376 return 0;
1377
1378out1:
1379 instr->state = MTD_ERASE_FAILED;
1380 mutex_unlock(&fsm->lock);
1381
1382 return ret;
1383}
1384
Lee Jones1bd512b2014-03-20 09:20:38 +00001385static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
1386{
1387 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
1388 uint32_t tmp[2];
1389
1390 stfsm_load_seq(fsm, seq);
1391
1392 stfsm_read_fifo(fsm, tmp, 8);
1393
1394 memcpy(jedec, tmp, 5);
1395
1396 stfsm_wait_seq(fsm);
1397}
1398
1399static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
1400{
Lee Jones24fec652014-03-20 09:20:41 +00001401 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001402 u16 ext_jedec;
1403 u32 jedec;
1404 u8 id[5];
1405
1406 stfsm_read_jedec(fsm, id);
1407
1408 jedec = id[0] << 16 | id[1] << 8 | id[2];
1409 /*
1410 * JEDEC also defines an optional "extended device information"
1411 * string for after vendor-specific data, after the three bytes
1412 * we use here. Supporting some chips might require using it.
1413 */
1414 ext_jedec = id[3] << 8 | id[4];
1415
1416 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
1417 jedec, id[0], id[1], id[2], id[3], id[4]);
1418
Lee Jones24fec652014-03-20 09:20:41 +00001419 for (info = flash_types; info->name; info++) {
1420 if (info->jedec_id == jedec) {
1421 if (info->ext_id && info->ext_id != ext_jedec)
1422 continue;
1423 return info;
1424 }
1425 }
1426 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
1427
Lee Jones1bd512b2014-03-20 09:20:38 +00001428 return NULL;
1429}
1430
Lee Jones86f309fd2014-03-20 09:20:35 +00001431static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
1432{
1433 int ret, timeout = 10;
1434
1435 /* Wait for controller to accept mode change */
1436 while (--timeout) {
1437 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
1438 if (ret & 0x1)
1439 break;
1440 udelay(1);
1441 }
1442
1443 if (!timeout)
1444 return -EBUSY;
1445
1446 writel(mode, fsm->base + SPI_MODESELECT);
1447
1448 return 0;
1449}
1450
1451static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
1452{
1453 uint32_t emi_freq;
1454 uint32_t clk_div;
1455
1456 /* TODO: Make this dynamic */
1457 emi_freq = STFSM_DEFAULT_EMI_FREQ;
1458
1459 /*
1460 * Calculate clk_div - values between 2 and 128
1461 * Multiple of 2, rounded up
1462 */
1463 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
1464 if (clk_div < 2)
1465 clk_div = 2;
1466 else if (clk_div > 128)
1467 clk_div = 128;
1468
1469 /*
1470 * Determine a suitable delay for the IP to complete a change of
1471 * direction of the FIFO. The required delay is related to the clock
1472 * divider used. The following heuristics are based on empirical tests,
1473 * using a 100MHz EMI clock.
1474 */
1475 if (clk_div <= 4)
1476 fsm->fifo_dir_delay = 0;
1477 else if (clk_div <= 10)
1478 fsm->fifo_dir_delay = 1;
1479 else
1480 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1481
1482 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1483 emi_freq, spi_freq, clk_div);
1484
1485 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1486}
1487
1488static int stfsm_init(struct stfsm *fsm)
1489{
1490 int ret;
1491
1492 /* Perform a soft reset of the FSM controller */
1493 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1494 udelay(1);
1495 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1496
1497 /* Set clock to 'safe' frequency initially */
1498 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1499
1500 /* Switch to FSM */
1501 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1502 if (ret)
1503 return ret;
1504
1505 /* Set timing parameters */
1506 writel(SPI_CFG_DEVICE_ST |
1507 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1508 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1509 SPI_CFG_DEFAULT_DATA_HOLD,
1510 fsm->base + SPI_CONFIGDATA);
1511 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1512
1513 /* Clear FIFO, just in case */
1514 stfsm_clear_fifo(fsm);
1515
1516 return 0;
1517}
1518
Lee Jonesa63984c2014-03-20 09:20:46 +00001519static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1520{
1521 struct stfsm *fsm = platform_get_drvdata(pdev);
1522 struct device_node *np = pdev->dev.of_node;
1523 struct regmap *regmap;
1524 uint32_t boot_device_reg;
1525 uint32_t boot_device_spi;
1526 uint32_t boot_device; /* Value we read from *boot_device_reg */
1527 int ret;
1528
1529 /* Booting from SPI NOR Flash is the default */
1530 fsm->booted_from_spi = true;
1531
1532 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1533 if (IS_ERR(regmap))
1534 goto boot_device_fail;
1535
Lee Jones0ea7d702014-03-20 09:20:50 +00001536 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1537
1538 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1539
Lee Jonesa63984c2014-03-20 09:20:46 +00001540 /* Where in the syscon the boot device information lives */
1541 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1542 if (ret)
1543 goto boot_device_fail;
1544
1545 /* Boot device value when booted from SPI NOR */
1546 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1547 if (ret)
1548 goto boot_device_fail;
1549
1550 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1551 if (ret)
1552 goto boot_device_fail;
1553
1554 if (boot_device != boot_device_spi)
1555 fsm->booted_from_spi = false;
1556
1557 return;
1558
1559boot_device_fail:
1560 dev_warn(&pdev->dev,
1561 "failed to fetch boot device, assuming boot from SPI\n");
1562}
1563
Lee Jonesd90db4a2014-03-20 09:20:33 +00001564static int stfsm_probe(struct platform_device *pdev)
1565{
1566 struct device_node *np = pdev->dev.of_node;
Lee Jones24fec652014-03-20 09:20:41 +00001567 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001568 struct resource *res;
1569 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00001570 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001571
1572 if (!np) {
1573 dev_err(&pdev->dev, "No DT found\n");
1574 return -EINVAL;
1575 }
1576
1577 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1578 if (!fsm)
1579 return -ENOMEM;
1580
1581 fsm->dev = &pdev->dev;
1582
1583 platform_set_drvdata(pdev, fsm);
1584
1585 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1586 if (!res) {
1587 dev_err(&pdev->dev, "Resource not found\n");
1588 return -ENODEV;
1589 }
1590
1591 fsm->base = devm_ioremap_resource(&pdev->dev, res);
1592 if (IS_ERR(fsm->base)) {
1593 dev_err(&pdev->dev,
1594 "Failed to reserve memory region %pR\n", res);
1595 return PTR_ERR(fsm->base);
1596 }
1597
1598 mutex_init(&fsm->lock);
1599
Lee Jones86f309fd2014-03-20 09:20:35 +00001600 ret = stfsm_init(fsm);
1601 if (ret) {
1602 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
1603 return ret;
1604 }
1605
Lee Jonesa63984c2014-03-20 09:20:46 +00001606 stfsm_fetch_platform_configs(pdev);
1607
Lee Jones1bd512b2014-03-20 09:20:38 +00001608 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00001609 info = stfsm_jedec_probe(fsm);
1610 if (!info)
1611 return -ENODEV;
1612 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001613
Lee Jones3b5d1982014-03-20 09:20:43 +00001614 /* Use device size to determine address width */
1615 if (info->sector_size * info->n_sectors > 0x1000000)
1616 info->flags |= FLASH_FLAG_32BIT_ADDR;
1617
Lee Jones218b8702014-03-20 09:20:55 +00001618 /*
1619 * Configure READ/WRITE/ERASE sequences according to platform and
1620 * device flags.
1621 */
1622 if (info->config) {
1623 ret = info->config(fsm);
1624 if (ret)
1625 return ret;
Lee Jones4eb3f0d82014-03-20 09:20:56 +00001626 } else {
1627 ret = stfsm_prepare_rwe_seqs_default(fsm);
1628 if (ret)
1629 return ret;
Lee Jones218b8702014-03-20 09:20:55 +00001630 }
1631
Lee Jonesd90db4a2014-03-20 09:20:33 +00001632 fsm->mtd.dev.parent = &pdev->dev;
1633 fsm->mtd.type = MTD_NORFLASH;
1634 fsm->mtd.writesize = 4;
1635 fsm->mtd.writebufsize = fsm->mtd.writesize;
1636 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00001637 fsm->mtd.size = info->sector_size * info->n_sectors;
1638 fsm->mtd.erasesize = info->sector_size;
1639
Lee Jonese514f102014-03-20 09:20:57 +00001640 fsm->mtd._read = stfsm_mtd_read;
Lee Jones176b4372014-03-20 09:20:59 +00001641 fsm->mtd._write = stfsm_mtd_write;
Lee Jones4a341fe2014-03-20 09:21:00 +00001642 fsm->mtd._erase = stfsm_mtd_erase;
Lee Jonese514f102014-03-20 09:20:57 +00001643
Lee Jones4a341fe2014-03-20 09:21:00 +00001644 dev_info(&pdev->dev,
Lee Jones24fec652014-03-20 09:20:41 +00001645 "Found serial flash device: %s\n"
1646 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
1647 info->name,
1648 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
1649 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00001650
1651 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
1652}
1653
1654static int stfsm_remove(struct platform_device *pdev)
1655{
1656 struct stfsm *fsm = platform_get_drvdata(pdev);
1657 int err;
1658
1659 err = mtd_device_unregister(&fsm->mtd);
1660 if (err)
1661 return err;
1662
1663 return 0;
1664}
1665
1666static struct of_device_id stfsm_match[] = {
1667 { .compatible = "st,spi-fsm", },
1668 {},
1669};
1670MODULE_DEVICE_TABLE(of, stfsm_match);
1671
1672static struct platform_driver stfsm_driver = {
1673 .probe = stfsm_probe,
1674 .remove = stfsm_remove,
1675 .driver = {
1676 .name = "st-spi-fsm",
1677 .owner = THIS_MODULE,
1678 .of_match_table = stfsm_match,
1679 },
1680};
1681module_platform_driver(stfsm_driver);
1682
1683MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
1684MODULE_DESCRIPTION("ST SPI FSM driver");
1685MODULE_LICENSE("GPL");