blob: b4d2a188672e8591733833bb36d6e8b72039a11d [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
241/*
242 * Flags to tweak operation of default read/write/erase routines
243 */
244#define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
245#define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
246#define CFG_WRITE_EX_32BIT_ADDR_DELAY 0x00000004
247#define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
248#define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
249
Lee Jonesd90db4a2014-03-20 09:20:33 +0000250struct stfsm {
251 struct device *dev;
252 void __iomem *base;
253 struct resource *region;
254 struct mtd_info mtd;
255 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000256 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000257
Lee Jonese85a6192014-03-20 09:20:54 +0000258 uint32_t configuration;
Lee Jones86f309fd2014-03-20 09:20:35 +0000259 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000260 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000261 bool reset_signal;
262 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000263};
264
Lee Jones3c8b85b2014-03-20 09:20:36 +0000265struct stfsm_seq {
266 uint32_t data_size;
267 uint32_t addr1;
268 uint32_t addr2;
269 uint32_t addr_cfg;
270 uint32_t seq_opc[5];
271 uint32_t mode;
272 uint32_t dummy;
273 uint32_t status;
274 uint8_t seq[16];
275 uint32_t seq_cfg;
276} __packed __aligned(4);
277
Lee Jones08981272014-03-20 09:20:42 +0000278/* Parameters to configure a READ or WRITE FSM sequence */
279struct seq_rw_config {
280 uint32_t flags; /* flags to support config */
281 uint8_t cmd; /* FLASH command */
282 int write; /* Write Sequence */
283 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
284 uint8_t data_pads; /* No. of data pads */
285 uint8_t mode_data; /* MODE data */
286 uint8_t mode_cycles; /* No. of MODE cycles */
287 uint8_t dummy_cycles; /* No. of DUMMY cycles */
288};
289
Lee Jones11d7f822014-03-20 09:20:40 +0000290/* SPI Flash Device Table */
291struct flash_info {
292 char *name;
293 /*
294 * JEDEC id zero means "no ID" (most older chips); otherwise it has
295 * a high byte of zero plus three data bytes: the manufacturer id,
296 * then a two byte device id.
297 */
298 u32 jedec_id;
299 u16 ext_id;
300 /*
301 * The size listed here is what works with FLASH_CMD_SE, which isn't
302 * necessarily called a "sector" by the vendor.
303 */
304 unsigned sector_size;
305 u16 n_sectors;
306 u32 flags;
307 /*
308 * Note, where FAST_READ is supported, freq_max specifies the
309 * FAST_READ frequency, not the READ frequency.
310 */
311 u32 max_freq;
312 int (*config)(struct stfsm *);
313};
314
Lee Jones218b8702014-03-20 09:20:55 +0000315static int stfsm_n25q_config(struct stfsm *fsm);
316
Lee Jones11d7f822014-03-20 09:20:40 +0000317static struct flash_info flash_types[] = {
318 /*
319 * ST Microelectronics/Numonyx --
320 * (newer production versions may have feature updates
321 * (eg faster operating frequency)
322 */
323#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
324 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
325 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
326 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
327 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
328 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
329 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
330
331#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
332 FLASH_FLAG_READ_FAST | \
333 FLASH_FLAG_READ_1_1_2 | \
334 FLASH_FLAG_WRITE_1_1_2)
335 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
336 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
337
338#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
339 FLASH_FLAG_READ_FAST | \
340 FLASH_FLAG_READ_1_1_2 | \
341 FLASH_FLAG_READ_1_2_2 | \
342 FLASH_FLAG_READ_1_1_4 | \
343 FLASH_FLAG_READ_1_4_4 | \
344 FLASH_FLAG_SE_4K | \
345 FLASH_FLAG_SE_32K)
346 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
347 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
348
349#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
350 FLASH_FLAG_READ_FAST | \
351 FLASH_FLAG_READ_1_1_2 | \
352 FLASH_FLAG_READ_1_2_2 | \
353 FLASH_FLAG_READ_1_1_4 | \
354 FLASH_FLAG_READ_1_4_4 | \
355 FLASH_FLAG_WRITE_1_1_2 | \
356 FLASH_FLAG_WRITE_1_2_2 | \
357 FLASH_FLAG_WRITE_1_1_4 | \
358 FLASH_FLAG_WRITE_1_4_4)
Lee Jones218b8702014-03-20 09:20:55 +0000359 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
360 stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000361 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
Lee Jones218b8702014-03-20 09:20:55 +0000362 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
Lee Jones11d7f822014-03-20 09:20:40 +0000363
364 /*
365 * Spansion S25FLxxxP
366 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
367 */
368#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
369 FLASH_FLAG_READ_1_1_2 | \
370 FLASH_FLAG_READ_1_2_2 | \
371 FLASH_FLAG_READ_1_1_4 | \
372 FLASH_FLAG_READ_1_4_4 | \
373 FLASH_FLAG_WRITE_1_1_4 | \
374 FLASH_FLAG_READ_FAST)
375 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
376 NULL },
377 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
378 NULL },
379
380 /*
381 * Spansion S25FLxxxS
382 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
383 * - RESET# signal supported by die but not bristled out on all
384 * package types. The package type is a function of board design,
385 * so this information is captured in the board's flags.
386 * - Supports 'DYB' sector protection. Depending on variant, sectors
387 * may default to locked state on power-on.
388 */
389#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
390 FLASH_FLAG_RESET | \
391 FLASH_FLAG_DYB_LOCKING)
392 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
393 NULL },
394 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
395 NULL },
396 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
397 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
398 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
399 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
400
401 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
402#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
403 FLASH_FLAG_READ_FAST | \
404 FLASH_FLAG_READ_1_1_2 | \
405 FLASH_FLAG_WRITE_1_1_2)
406 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
407 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
408 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
409 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
410 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
411
412 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
413#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
414 FLASH_FLAG_READ_FAST | \
415 FLASH_FLAG_READ_1_1_2 | \
416 FLASH_FLAG_READ_1_2_2 | \
417 FLASH_FLAG_READ_1_1_4 | \
418 FLASH_FLAG_READ_1_4_4 | \
419 FLASH_FLAG_WRITE_1_1_4)
420 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
421 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
422 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
423 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
424
425 /* Sentinel */
426 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
427};
428
Lee Jonesa37b2f52014-03-20 09:20:53 +0000429/*
430 * FSM message sequence configurations:
431 *
432 * All configs are presented in order of preference
433 */
434
435/* Default READ configurations, in order of preference */
436static struct seq_rw_config default_read_configs[] = {
437 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
438 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
439 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
440 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
441 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
442 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
443 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
444};
445
446/* Default WRITE configurations */
447static struct seq_rw_config default_write_configs[] = {
448 {FLASH_FLAG_WRITE_1_4_4, FLASH_CMD_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
449 {FLASH_FLAG_WRITE_1_1_4, FLASH_CMD_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
450 {FLASH_FLAG_WRITE_1_2_2, FLASH_CMD_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
451 {FLASH_FLAG_WRITE_1_1_2, FLASH_CMD_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
452 {FLASH_FLAG_READ_WRITE, FLASH_CMD_WRITE, 1, 1, 1, 0x00, 0, 0},
453 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
454};
455
Lee Jonese85a6192014-03-20 09:20:54 +0000456/*
457 * [N25Qxxx] Configuration
458 */
459#define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
460#define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
461#define N25Q_VCR_WRAP_CONT 0x3
462
463/* N25Q 3-byte Address READ configurations
464 * - 'FAST' variants configured for 8 dummy cycles.
465 *
466 * Note, the number of dummy cycles used for 'FAST' READ operations is
467 * configurable and would normally be tuned according to the READ command and
468 * operating frequency. However, this applies universally to all 'FAST' READ
469 * commands, including those used by the SPIBoot controller, and remains in
470 * force until the device is power-cycled. Since the SPIBoot controller is
471 * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
472 * cycles.
473 */
474static struct seq_rw_config n25q_read3_configs[] = {
475 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
476 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
477 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
478 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
479 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ_FAST, 0, 1, 1, 0x00, 0, 8},
480 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ, 0, 1, 1, 0x00, 0, 0},
481 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
482};
483
484/* N25Q 4-byte Address READ configurations
485 * - use special 4-byte address READ commands (reduces overheads, and
486 * reduces risk of hitting watchdog reset issues).
487 * - 'FAST' variants configured for 8 dummy cycles (see note above.)
488 */
489static struct seq_rw_config n25q_read4_configs[] = {
490 {FLASH_FLAG_READ_1_4_4, FLASH_CMD_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
491 {FLASH_FLAG_READ_1_1_4, FLASH_CMD_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
492 {FLASH_FLAG_READ_1_2_2, FLASH_CMD_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
493 {FLASH_FLAG_READ_1_1_2, FLASH_CMD_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
494 {FLASH_FLAG_READ_FAST, FLASH_CMD_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
495 {FLASH_FLAG_READ_WRITE, FLASH_CMD_READ4, 0, 1, 1, 0x00, 0, 0},
496 {0x00, 0, 0, 0, 0, 0x00, 0, 0},
497};
498
Lee Jones218b8702014-03-20 09:20:55 +0000499static struct stfsm_seq stfsm_seq_read; /* Dynamically populated */
500static struct stfsm_seq stfsm_seq_write; /* Dynamically populated */
Lee Jones0de08e42014-03-20 09:20:51 +0000501static struct stfsm_seq stfsm_seq_en_32bit_addr;/* Dynamically populated */
502
Lee Jones1bd512b2014-03-20 09:20:38 +0000503static struct stfsm_seq stfsm_seq_read_jedec = {
504 .data_size = TRANSFER_SIZE(8),
505 .seq_opc[0] = (SEQ_OPC_PADS_1 |
506 SEQ_OPC_CYCLES(8) |
507 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
508 .seq = {
509 STFSM_INST_CMD1,
510 STFSM_INST_DATA_READ,
511 STFSM_INST_STOP,
512 },
513 .seq_cfg = (SEQ_CFG_PADS_1 |
514 SEQ_CFG_READNOTWRITE |
515 SEQ_CFG_CSDEASSERT |
516 SEQ_CFG_STARTSEQ),
517};
518
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000519static struct stfsm_seq stfsm_seq_erase_sector = {
520 /* 'addr_cfg' configured during initialisation */
521 .seq_opc = {
522 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
523 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
524
525 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
526 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
527 },
528 .seq = {
529 STFSM_INST_CMD1,
530 STFSM_INST_CMD2,
531 STFSM_INST_ADD1,
532 STFSM_INST_ADD2,
533 STFSM_INST_STOP,
534 },
535 .seq_cfg = (SEQ_CFG_PADS_1 |
536 SEQ_CFG_READNOTWRITE |
537 SEQ_CFG_CSDEASSERT |
538 SEQ_CFG_STARTSEQ),
539};
540
Lee Jones249516c2014-03-20 09:20:52 +0000541static struct stfsm_seq stfsm_seq_wrvcr = {
542 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
543 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
544 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
545 SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
546 .seq = {
547 STFSM_INST_CMD1,
548 STFSM_INST_CMD2,
549 STFSM_INST_STA_WR1,
550 STFSM_INST_STOP,
551 },
552 .seq_cfg = (SEQ_CFG_PADS_1 |
553 SEQ_CFG_READNOTWRITE |
554 SEQ_CFG_CSDEASSERT |
555 SEQ_CFG_STARTSEQ),
556};
557
Lee Jones6bd29602014-03-20 09:20:48 +0000558static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
559{
560 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
561 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
562 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
563 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
564 SEQ_OPC_CSDEASSERT);
565
566 seq->seq[0] = STFSM_INST_CMD2;
567 seq->seq[1] = STFSM_INST_CMD1;
568 seq->seq[2] = STFSM_INST_WAIT;
569 seq->seq[3] = STFSM_INST_STOP;
570
571 seq->seq_cfg = (SEQ_CFG_PADS_1 |
572 SEQ_CFG_ERASE |
573 SEQ_CFG_READNOTWRITE |
574 SEQ_CFG_CSDEASSERT |
575 SEQ_CFG_STARTSEQ);
576
577 return 0;
578}
579
Lee Jones3c8b85b2014-03-20 09:20:36 +0000580static inline int stfsm_is_idle(struct stfsm *fsm)
581{
582 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
583}
584
Lee Jones86f309fd2014-03-20 09:20:35 +0000585static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
586{
587 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
588}
589
590static void stfsm_clear_fifo(struct stfsm *fsm)
591{
592 uint32_t avail;
593
594 for (;;) {
595 avail = stfsm_fifo_available(fsm);
596 if (!avail)
597 break;
598
599 while (avail) {
600 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
601 avail--;
602 }
603 }
604}
605
Lee Jones3c8b85b2014-03-20 09:20:36 +0000606static inline void stfsm_load_seq(struct stfsm *fsm,
607 const struct stfsm_seq *seq)
608{
609 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
610 const uint32_t *src = (const uint32_t *)seq;
611 int words = sizeof(*seq) / sizeof(*src);
612
613 BUG_ON(!stfsm_is_idle(fsm));
614
615 while (words--) {
616 writel(*src, dst);
617 src++;
618 dst += 4;
619 }
620}
621
622static void stfsm_wait_seq(struct stfsm *fsm)
623{
624 unsigned long deadline;
625 int timeout = 0;
626
627 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
628
629 while (!timeout) {
630 if (time_after_eq(jiffies, deadline))
631 timeout = 1;
632
633 if (stfsm_is_idle(fsm))
634 return;
635
636 cond_resched();
637 }
638
639 dev_err(fsm->dev, "timeout on sequence completion\n");
640}
641
Lee Jones030e82d2014-03-20 09:20:37 +0000642static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
643 const uint32_t size)
644{
645 uint32_t remaining = size >> 2;
646 uint32_t avail;
647 uint32_t words;
648
649 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
650
651 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
652
653 while (remaining) {
654 for (;;) {
655 avail = stfsm_fifo_available(fsm);
656 if (avail)
657 break;
658 udelay(1);
659 }
660 words = min(avail, remaining);
661 remaining -= words;
662
663 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
664 buf += words;
665 }
666}
667
Lee Jones0de08e42014-03-20 09:20:51 +0000668static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
669{
670 struct stfsm_seq *seq = &stfsm_seq_en_32bit_addr;
671 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
672
673 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
674 SEQ_OPC_CYCLES(8) |
675 SEQ_OPC_OPCODE(cmd) |
676 SEQ_OPC_CSDEASSERT);
677
678 stfsm_load_seq(fsm, seq);
679
680 stfsm_wait_seq(fsm);
681
682 return 0;
683}
684
Lee Jones249516c2014-03-20 09:20:52 +0000685static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
686{
687 struct stfsm_seq *seq = &stfsm_seq_wrvcr;
688
689 dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
690
691 seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
692
693 stfsm_load_seq(fsm, seq);
694
695 stfsm_wait_seq(fsm);
696
697 return 0;
698}
699
Lee Jones0ea7d702014-03-20 09:20:50 +0000700/*
701 * SoC reset on 'boot-from-spi' systems
702 *
703 * Certain modes of operation cause the Flash device to enter a particular state
704 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
705 * Addr' commands). On boot-from-spi systems, it is important to consider what
706 * happens if a warm reset occurs during this period. The SPIBoot controller
707 * assumes that Flash device is in its default reset state, 24-bit address mode,
708 * and ready to accept commands. This can be achieved using some form of
709 * on-board logic/controller to force a device POR in response to a SoC-level
710 * reset or by making use of the device reset signal if available (limited
711 * number of devices only).
712 *
713 * Failure to take such precautions can cause problems following a warm reset.
714 * For some operations (e.g. ERASE), there is little that can be done. For
715 * other modes of operation (e.g. 32-bit addressing), options are often
716 * available that can help minimise the window in which a reset could cause a
717 * problem.
718 *
719 */
720static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
721{
722 /* Reset signal is available on the board and supported by the device */
723 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
724 return true;
725
726 /* Board-level logic forces a power-on-reset */
727 if (fsm->reset_por)
728 return true;
729
730 /* Reset is not properly handled and may result in failure to reboot */
731 return false;
732}
733
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000734/* Configure 'addr_cfg' according to addressing mode */
735static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
736 struct stfsm_seq *seq)
737{
738 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
739
740 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
741 ADR_CFG_PADS_1_ADD1 |
742 ADR_CFG_CYCLES_ADD2(16) |
743 ADR_CFG_PADS_1_ADD2 |
744 ADR_CFG_CSDEASSERT_ADD2);
745}
746
Lee Jones08981272014-03-20 09:20:42 +0000747/* Search for preferred configuration based on available flags */
748static struct seq_rw_config *
749stfsm_search_seq_rw_configs(struct stfsm *fsm,
750 struct seq_rw_config cfgs[])
751{
752 struct seq_rw_config *config;
753 int flags = fsm->info->flags;
754
755 for (config = cfgs; config->cmd != 0; config++)
756 if ((config->flags & flags) == config->flags)
757 return config;
758
759 return NULL;
760}
761
Lee Jones97ccf2d2014-03-20 09:20:44 +0000762/* Prepare a READ/WRITE sequence according to configuration parameters */
763static void stfsm_prepare_rw_seq(struct stfsm *fsm,
764 struct stfsm_seq *seq,
765 struct seq_rw_config *cfg)
766{
767 int addr1_cycles, addr2_cycles;
768 int i = 0;
769
770 memset(seq, 0, sizeof(*seq));
771
772 /* Add READ/WRITE OPC */
773 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
774 SEQ_OPC_CYCLES(8) |
775 SEQ_OPC_OPCODE(cfg->cmd));
776
777 /* Add WREN OPC for a WRITE sequence */
778 if (cfg->write)
779 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
780 SEQ_OPC_CYCLES(8) |
781 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
782 SEQ_OPC_CSDEASSERT);
783
784 /* Address configuration (24 or 32-bit addresses) */
785 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
786 addr1_cycles /= cfg->addr_pads;
787 addr2_cycles = 16 / cfg->addr_pads;
788 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
789 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
790 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
791 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
792
793 /* Data/Sequence configuration */
794 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
795 SEQ_CFG_STARTSEQ |
796 SEQ_CFG_CSDEASSERT);
797 if (!cfg->write)
798 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
799
800 /* Mode configuration (no. of pads taken from addr cfg) */
801 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
802 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
803 (cfg->addr_pads - 1) << 22); /* pads */
804
805 /* Dummy configuration (no. of pads taken from addr cfg) */
806 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
807 (cfg->addr_pads - 1) << 22); /* pads */
808
809
810 /* Instruction sequence */
811 i = 0;
812 if (cfg->write)
813 seq->seq[i++] = STFSM_INST_CMD2;
814
815 seq->seq[i++] = STFSM_INST_CMD1;
816
817 seq->seq[i++] = STFSM_INST_ADD1;
818 seq->seq[i++] = STFSM_INST_ADD2;
819
820 if (cfg->mode_cycles)
821 seq->seq[i++] = STFSM_INST_MODE;
822
823 if (cfg->dummy_cycles)
824 seq->seq[i++] = STFSM_INST_DUMMY;
825
826 seq->seq[i++] =
827 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
828 seq->seq[i++] = STFSM_INST_STOP;
829}
830
Lee Jones88cccb82014-03-20 09:20:49 +0000831static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
832 struct stfsm_seq *seq,
833 struct seq_rw_config *cfgs)
834{
835 struct seq_rw_config *config;
836
837 config = stfsm_search_seq_rw_configs(fsm, cfgs);
838 if (!config) {
839 dev_err(fsm->dev, "failed to find suitable config\n");
840 return -EINVAL;
841 }
842
843 stfsm_prepare_rw_seq(fsm, seq, config);
844
845 return 0;
846}
847
Lee Jones218b8702014-03-20 09:20:55 +0000848static int stfsm_n25q_config(struct stfsm *fsm)
849{
850 uint32_t flags = fsm->info->flags;
851 uint8_t vcr;
852 int ret = 0;
853 bool soc_reset;
854
855 /* Configure 'READ' sequence */
856 if (flags & FLASH_FLAG_32BIT_ADDR)
857 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
858 n25q_read4_configs);
859 else
860 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_read,
861 n25q_read3_configs);
862 if (ret) {
863 dev_err(fsm->dev,
864 "failed to prepare READ sequence with flags [0x%08x]\n",
865 flags);
866 return ret;
867 }
868
869 /* Configure 'WRITE' sequence (default configs) */
870 ret = stfsm_search_prepare_rw_seq(fsm, &stfsm_seq_write,
871 default_write_configs);
872 if (ret) {
873 dev_err(fsm->dev,
874 "preparing WRITE sequence using flags [0x%08x] failed\n",
875 flags);
876 return ret;
877 }
878
879 /* * Configure 'ERASE_SECTOR' sequence */
880 stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
881
882 /* Configure 32-bit address support */
883 if (flags & FLASH_FLAG_32BIT_ADDR) {
884 stfsm_n25q_en_32bit_addr_seq(&stfsm_seq_en_32bit_addr);
885
886 soc_reset = stfsm_can_handle_soc_reset(fsm);
887 if (soc_reset || !fsm->booted_from_spi) {
888 /*
889 * If we can handle SoC resets, we enable 32-bit
890 * address mode pervasively
891 */
892 stfsm_enter_32bit_addr(fsm, 1);
893 } else {
894 /*
895 * If not, enable/disable for WRITE and ERASE
896 * operations (READ uses special commands)
897 */
898 fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
899 CFG_ERASESEC_TOGGLE_32BIT_ADDR);
900 }
901 }
902
903 /*
904 * Configure device to use 8 dummy cycles
905 */
906 vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
907 N25Q_VCR_WRAP_CONT);
908 stfsm_wrvcr(fsm, vcr);
909
910 return 0;
911}
912
Lee Jones1bd512b2014-03-20 09:20:38 +0000913static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
914{
915 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
916 uint32_t tmp[2];
917
918 stfsm_load_seq(fsm, seq);
919
920 stfsm_read_fifo(fsm, tmp, 8);
921
922 memcpy(jedec, tmp, 5);
923
924 stfsm_wait_seq(fsm);
925}
926
927static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
928{
Lee Jones24fec652014-03-20 09:20:41 +0000929 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000930 u16 ext_jedec;
931 u32 jedec;
932 u8 id[5];
933
934 stfsm_read_jedec(fsm, id);
935
936 jedec = id[0] << 16 | id[1] << 8 | id[2];
937 /*
938 * JEDEC also defines an optional "extended device information"
939 * string for after vendor-specific data, after the three bytes
940 * we use here. Supporting some chips might require using it.
941 */
942 ext_jedec = id[3] << 8 | id[4];
943
944 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
945 jedec, id[0], id[1], id[2], id[3], id[4]);
946
Lee Jones24fec652014-03-20 09:20:41 +0000947 for (info = flash_types; info->name; info++) {
948 if (info->jedec_id == jedec) {
949 if (info->ext_id && info->ext_id != ext_jedec)
950 continue;
951 return info;
952 }
953 }
954 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
955
Lee Jones1bd512b2014-03-20 09:20:38 +0000956 return NULL;
957}
958
Lee Jones86f309fd2014-03-20 09:20:35 +0000959static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
960{
961 int ret, timeout = 10;
962
963 /* Wait for controller to accept mode change */
964 while (--timeout) {
965 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
966 if (ret & 0x1)
967 break;
968 udelay(1);
969 }
970
971 if (!timeout)
972 return -EBUSY;
973
974 writel(mode, fsm->base + SPI_MODESELECT);
975
976 return 0;
977}
978
979static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
980{
981 uint32_t emi_freq;
982 uint32_t clk_div;
983
984 /* TODO: Make this dynamic */
985 emi_freq = STFSM_DEFAULT_EMI_FREQ;
986
987 /*
988 * Calculate clk_div - values between 2 and 128
989 * Multiple of 2, rounded up
990 */
991 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
992 if (clk_div < 2)
993 clk_div = 2;
994 else if (clk_div > 128)
995 clk_div = 128;
996
997 /*
998 * Determine a suitable delay for the IP to complete a change of
999 * direction of the FIFO. The required delay is related to the clock
1000 * divider used. The following heuristics are based on empirical tests,
1001 * using a 100MHz EMI clock.
1002 */
1003 if (clk_div <= 4)
1004 fsm->fifo_dir_delay = 0;
1005 else if (clk_div <= 10)
1006 fsm->fifo_dir_delay = 1;
1007 else
1008 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
1009
1010 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
1011 emi_freq, spi_freq, clk_div);
1012
1013 writel(clk_div, fsm->base + SPI_CLOCKDIV);
1014}
1015
1016static int stfsm_init(struct stfsm *fsm)
1017{
1018 int ret;
1019
1020 /* Perform a soft reset of the FSM controller */
1021 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
1022 udelay(1);
1023 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
1024
1025 /* Set clock to 'safe' frequency initially */
1026 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
1027
1028 /* Switch to FSM */
1029 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
1030 if (ret)
1031 return ret;
1032
1033 /* Set timing parameters */
1034 writel(SPI_CFG_DEVICE_ST |
1035 SPI_CFG_DEFAULT_MIN_CS_HIGH |
1036 SPI_CFG_DEFAULT_CS_SETUPHOLD |
1037 SPI_CFG_DEFAULT_DATA_HOLD,
1038 fsm->base + SPI_CONFIGDATA);
1039 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
1040
1041 /* Clear FIFO, just in case */
1042 stfsm_clear_fifo(fsm);
1043
1044 return 0;
1045}
1046
Lee Jonesa63984c2014-03-20 09:20:46 +00001047static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1048{
1049 struct stfsm *fsm = platform_get_drvdata(pdev);
1050 struct device_node *np = pdev->dev.of_node;
1051 struct regmap *regmap;
1052 uint32_t boot_device_reg;
1053 uint32_t boot_device_spi;
1054 uint32_t boot_device; /* Value we read from *boot_device_reg */
1055 int ret;
1056
1057 /* Booting from SPI NOR Flash is the default */
1058 fsm->booted_from_spi = true;
1059
1060 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1061 if (IS_ERR(regmap))
1062 goto boot_device_fail;
1063
Lee Jones0ea7d702014-03-20 09:20:50 +00001064 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
1065
1066 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
1067
Lee Jonesa63984c2014-03-20 09:20:46 +00001068 /* Where in the syscon the boot device information lives */
1069 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1070 if (ret)
1071 goto boot_device_fail;
1072
1073 /* Boot device value when booted from SPI NOR */
1074 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1075 if (ret)
1076 goto boot_device_fail;
1077
1078 ret = regmap_read(regmap, boot_device_reg, &boot_device);
1079 if (ret)
1080 goto boot_device_fail;
1081
1082 if (boot_device != boot_device_spi)
1083 fsm->booted_from_spi = false;
1084
1085 return;
1086
1087boot_device_fail:
1088 dev_warn(&pdev->dev,
1089 "failed to fetch boot device, assuming boot from SPI\n");
1090}
1091
Lee Jonesd90db4a2014-03-20 09:20:33 +00001092static int stfsm_probe(struct platform_device *pdev)
1093{
1094 struct device_node *np = pdev->dev.of_node;
Lee Jones24fec652014-03-20 09:20:41 +00001095 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001096 struct resource *res;
1097 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +00001098 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +00001099
1100 if (!np) {
1101 dev_err(&pdev->dev, "No DT found\n");
1102 return -EINVAL;
1103 }
1104
1105 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
1106 if (!fsm)
1107 return -ENOMEM;
1108
1109 fsm->dev = &pdev->dev;
1110
1111 platform_set_drvdata(pdev, fsm);
1112
1113 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1114 if (!res) {
1115 dev_err(&pdev->dev, "Resource not found\n");
1116 return -ENODEV;
1117 }
1118
1119 fsm->base = devm_ioremap_resource(&pdev->dev, res);
1120 if (IS_ERR(fsm->base)) {
1121 dev_err(&pdev->dev,
1122 "Failed to reserve memory region %pR\n", res);
1123 return PTR_ERR(fsm->base);
1124 }
1125
1126 mutex_init(&fsm->lock);
1127
Lee Jones86f309fd2014-03-20 09:20:35 +00001128 ret = stfsm_init(fsm);
1129 if (ret) {
1130 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
1131 return ret;
1132 }
1133
Lee Jonesa63984c2014-03-20 09:20:46 +00001134 stfsm_fetch_platform_configs(pdev);
1135
Lee Jones1bd512b2014-03-20 09:20:38 +00001136 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +00001137 info = stfsm_jedec_probe(fsm);
1138 if (!info)
1139 return -ENODEV;
1140 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +00001141
Lee Jones3b5d1982014-03-20 09:20:43 +00001142 /* Use device size to determine address width */
1143 if (info->sector_size * info->n_sectors > 0x1000000)
1144 info->flags |= FLASH_FLAG_32BIT_ADDR;
1145
Lee Jones218b8702014-03-20 09:20:55 +00001146 /*
1147 * Configure READ/WRITE/ERASE sequences according to platform and
1148 * device flags.
1149 */
1150 if (info->config) {
1151 ret = info->config(fsm);
1152 if (ret)
1153 return ret;
1154 }
1155
Lee Jonesd90db4a2014-03-20 09:20:33 +00001156 fsm->mtd.dev.parent = &pdev->dev;
1157 fsm->mtd.type = MTD_NORFLASH;
1158 fsm->mtd.writesize = 4;
1159 fsm->mtd.writebufsize = fsm->mtd.writesize;
1160 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +00001161 fsm->mtd.size = info->sector_size * info->n_sectors;
1162 fsm->mtd.erasesize = info->sector_size;
1163
1164 dev_err(&pdev->dev,
1165 "Found serial flash device: %s\n"
1166 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
1167 info->name,
1168 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
1169 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +00001170
1171 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
1172}
1173
1174static int stfsm_remove(struct platform_device *pdev)
1175{
1176 struct stfsm *fsm = platform_get_drvdata(pdev);
1177 int err;
1178
1179 err = mtd_device_unregister(&fsm->mtd);
1180 if (err)
1181 return err;
1182
1183 return 0;
1184}
1185
1186static struct of_device_id stfsm_match[] = {
1187 { .compatible = "st,spi-fsm", },
1188 {},
1189};
1190MODULE_DEVICE_TABLE(of, stfsm_match);
1191
1192static struct platform_driver stfsm_driver = {
1193 .probe = stfsm_probe,
1194 .remove = stfsm_remove,
1195 .driver = {
1196 .name = "st-spi-fsm",
1197 .owner = THIS_MODULE,
1198 .of_match_table = stfsm_match,
1199 },
1200};
1201module_platform_driver(stfsm_driver);
1202
1203MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
1204MODULE_DESCRIPTION("ST SPI FSM driver");
1205MODULE_LICENSE("GPL");