blob: 5683443e7261672d82242b454a89bf4b6b23deca [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 Jonesd90db4a2014-03-20 09:20:33 +0000203struct stfsm {
204 struct device *dev;
205 void __iomem *base;
206 struct resource *region;
207 struct mtd_info mtd;
208 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000209 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000210
211 uint32_t fifo_dir_delay;
Lee Jonesa63984c2014-03-20 09:20:46 +0000212 bool booted_from_spi;
Lee Jones0ea7d702014-03-20 09:20:50 +0000213 bool reset_signal;
214 bool reset_por;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000215};
216
Lee Jones3c8b85b2014-03-20 09:20:36 +0000217struct stfsm_seq {
218 uint32_t data_size;
219 uint32_t addr1;
220 uint32_t addr2;
221 uint32_t addr_cfg;
222 uint32_t seq_opc[5];
223 uint32_t mode;
224 uint32_t dummy;
225 uint32_t status;
226 uint8_t seq[16];
227 uint32_t seq_cfg;
228} __packed __aligned(4);
229
Lee Jones08981272014-03-20 09:20:42 +0000230/* Parameters to configure a READ or WRITE FSM sequence */
231struct seq_rw_config {
232 uint32_t flags; /* flags to support config */
233 uint8_t cmd; /* FLASH command */
234 int write; /* Write Sequence */
235 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
236 uint8_t data_pads; /* No. of data pads */
237 uint8_t mode_data; /* MODE data */
238 uint8_t mode_cycles; /* No. of MODE cycles */
239 uint8_t dummy_cycles; /* No. of DUMMY cycles */
240};
241
Lee Jones11d7f822014-03-20 09:20:40 +0000242/* SPI Flash Device Table */
243struct flash_info {
244 char *name;
245 /*
246 * JEDEC id zero means "no ID" (most older chips); otherwise it has
247 * a high byte of zero plus three data bytes: the manufacturer id,
248 * then a two byte device id.
249 */
250 u32 jedec_id;
251 u16 ext_id;
252 /*
253 * The size listed here is what works with FLASH_CMD_SE, which isn't
254 * necessarily called a "sector" by the vendor.
255 */
256 unsigned sector_size;
257 u16 n_sectors;
258 u32 flags;
259 /*
260 * Note, where FAST_READ is supported, freq_max specifies the
261 * FAST_READ frequency, not the READ frequency.
262 */
263 u32 max_freq;
264 int (*config)(struct stfsm *);
265};
266
267static struct flash_info flash_types[] = {
268 /*
269 * ST Microelectronics/Numonyx --
270 * (newer production versions may have feature updates
271 * (eg faster operating frequency)
272 */
273#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
274 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
275 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
276 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
277 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
278 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
279 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
280
281#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
282 FLASH_FLAG_READ_FAST | \
283 FLASH_FLAG_READ_1_1_2 | \
284 FLASH_FLAG_WRITE_1_1_2)
285 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
286 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
287
288#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
289 FLASH_FLAG_READ_FAST | \
290 FLASH_FLAG_READ_1_1_2 | \
291 FLASH_FLAG_READ_1_2_2 | \
292 FLASH_FLAG_READ_1_1_4 | \
293 FLASH_FLAG_READ_1_4_4 | \
294 FLASH_FLAG_SE_4K | \
295 FLASH_FLAG_SE_32K)
296 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
297 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
298
299#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
300 FLASH_FLAG_READ_FAST | \
301 FLASH_FLAG_READ_1_1_2 | \
302 FLASH_FLAG_READ_1_2_2 | \
303 FLASH_FLAG_READ_1_1_4 | \
304 FLASH_FLAG_READ_1_4_4 | \
305 FLASH_FLAG_WRITE_1_1_2 | \
306 FLASH_FLAG_WRITE_1_2_2 | \
307 FLASH_FLAG_WRITE_1_1_4 | \
308 FLASH_FLAG_WRITE_1_4_4)
309 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108, NULL },
310 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
311 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, NULL },
312
313 /*
314 * Spansion S25FLxxxP
315 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
316 */
317#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
318 FLASH_FLAG_READ_1_1_2 | \
319 FLASH_FLAG_READ_1_2_2 | \
320 FLASH_FLAG_READ_1_1_4 | \
321 FLASH_FLAG_READ_1_4_4 | \
322 FLASH_FLAG_WRITE_1_1_4 | \
323 FLASH_FLAG_READ_FAST)
324 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
325 NULL },
326 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
327 NULL },
328
329 /*
330 * Spansion S25FLxxxS
331 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
332 * - RESET# signal supported by die but not bristled out on all
333 * package types. The package type is a function of board design,
334 * so this information is captured in the board's flags.
335 * - Supports 'DYB' sector protection. Depending on variant, sectors
336 * may default to locked state on power-on.
337 */
338#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
339 FLASH_FLAG_RESET | \
340 FLASH_FLAG_DYB_LOCKING)
341 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
342 NULL },
343 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
344 NULL },
345 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
346 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
347 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
348 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
349
350 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
351#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
352 FLASH_FLAG_READ_FAST | \
353 FLASH_FLAG_READ_1_1_2 | \
354 FLASH_FLAG_WRITE_1_1_2)
355 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
356 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
357 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
358 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
359 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
360
361 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
362#define W25Q_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_4)
369 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
370 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
371 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
372 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
373
374 /* Sentinel */
375 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
376};
377
Lee Jones0de08e42014-03-20 09:20:51 +0000378static struct stfsm_seq stfsm_seq_en_32bit_addr;/* Dynamically populated */
379
Lee Jones1bd512b2014-03-20 09:20:38 +0000380static struct stfsm_seq stfsm_seq_read_jedec = {
381 .data_size = TRANSFER_SIZE(8),
382 .seq_opc[0] = (SEQ_OPC_PADS_1 |
383 SEQ_OPC_CYCLES(8) |
384 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
385 .seq = {
386 STFSM_INST_CMD1,
387 STFSM_INST_DATA_READ,
388 STFSM_INST_STOP,
389 },
390 .seq_cfg = (SEQ_CFG_PADS_1 |
391 SEQ_CFG_READNOTWRITE |
392 SEQ_CFG_CSDEASSERT |
393 SEQ_CFG_STARTSEQ),
394};
395
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000396static struct stfsm_seq stfsm_seq_erase_sector = {
397 /* 'addr_cfg' configured during initialisation */
398 .seq_opc = {
399 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
400 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
401
402 (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
403 SEQ_OPC_OPCODE(FLASH_CMD_SE)),
404 },
405 .seq = {
406 STFSM_INST_CMD1,
407 STFSM_INST_CMD2,
408 STFSM_INST_ADD1,
409 STFSM_INST_ADD2,
410 STFSM_INST_STOP,
411 },
412 .seq_cfg = (SEQ_CFG_PADS_1 |
413 SEQ_CFG_READNOTWRITE |
414 SEQ_CFG_CSDEASSERT |
415 SEQ_CFG_STARTSEQ),
416};
417
Lee Jones249516c2014-03-20 09:20:52 +0000418static struct stfsm_seq stfsm_seq_wrvcr = {
419 .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
420 SEQ_OPC_OPCODE(FLASH_CMD_WREN) | SEQ_OPC_CSDEASSERT),
421 .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
422 SEQ_OPC_OPCODE(FLASH_CMD_WRVCR)),
423 .seq = {
424 STFSM_INST_CMD1,
425 STFSM_INST_CMD2,
426 STFSM_INST_STA_WR1,
427 STFSM_INST_STOP,
428 },
429 .seq_cfg = (SEQ_CFG_PADS_1 |
430 SEQ_CFG_READNOTWRITE |
431 SEQ_CFG_CSDEASSERT |
432 SEQ_CFG_STARTSEQ),
433};
434
Lee Jones6bd29602014-03-20 09:20:48 +0000435static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
436{
437 seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
438 SEQ_OPC_OPCODE(FLASH_CMD_EN4B_ADDR));
439 seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
440 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
441 SEQ_OPC_CSDEASSERT);
442
443 seq->seq[0] = STFSM_INST_CMD2;
444 seq->seq[1] = STFSM_INST_CMD1;
445 seq->seq[2] = STFSM_INST_WAIT;
446 seq->seq[3] = STFSM_INST_STOP;
447
448 seq->seq_cfg = (SEQ_CFG_PADS_1 |
449 SEQ_CFG_ERASE |
450 SEQ_CFG_READNOTWRITE |
451 SEQ_CFG_CSDEASSERT |
452 SEQ_CFG_STARTSEQ);
453
454 return 0;
455}
456
Lee Jones3c8b85b2014-03-20 09:20:36 +0000457static inline int stfsm_is_idle(struct stfsm *fsm)
458{
459 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
460}
461
Lee Jones86f309fd2014-03-20 09:20:35 +0000462static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
463{
464 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
465}
466
467static void stfsm_clear_fifo(struct stfsm *fsm)
468{
469 uint32_t avail;
470
471 for (;;) {
472 avail = stfsm_fifo_available(fsm);
473 if (!avail)
474 break;
475
476 while (avail) {
477 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
478 avail--;
479 }
480 }
481}
482
Lee Jones3c8b85b2014-03-20 09:20:36 +0000483static inline void stfsm_load_seq(struct stfsm *fsm,
484 const struct stfsm_seq *seq)
485{
486 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
487 const uint32_t *src = (const uint32_t *)seq;
488 int words = sizeof(*seq) / sizeof(*src);
489
490 BUG_ON(!stfsm_is_idle(fsm));
491
492 while (words--) {
493 writel(*src, dst);
494 src++;
495 dst += 4;
496 }
497}
498
499static void stfsm_wait_seq(struct stfsm *fsm)
500{
501 unsigned long deadline;
502 int timeout = 0;
503
504 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
505
506 while (!timeout) {
507 if (time_after_eq(jiffies, deadline))
508 timeout = 1;
509
510 if (stfsm_is_idle(fsm))
511 return;
512
513 cond_resched();
514 }
515
516 dev_err(fsm->dev, "timeout on sequence completion\n");
517}
518
Lee Jones030e82d2014-03-20 09:20:37 +0000519static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
520 const uint32_t size)
521{
522 uint32_t remaining = size >> 2;
523 uint32_t avail;
524 uint32_t words;
525
526 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
527
528 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
529
530 while (remaining) {
531 for (;;) {
532 avail = stfsm_fifo_available(fsm);
533 if (avail)
534 break;
535 udelay(1);
536 }
537 words = min(avail, remaining);
538 remaining -= words;
539
540 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
541 buf += words;
542 }
543}
544
Lee Jones0de08e42014-03-20 09:20:51 +0000545static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
546{
547 struct stfsm_seq *seq = &stfsm_seq_en_32bit_addr;
548 uint32_t cmd = enter ? FLASH_CMD_EN4B_ADDR : FLASH_CMD_EX4B_ADDR;
549
550 seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
551 SEQ_OPC_CYCLES(8) |
552 SEQ_OPC_OPCODE(cmd) |
553 SEQ_OPC_CSDEASSERT);
554
555 stfsm_load_seq(fsm, seq);
556
557 stfsm_wait_seq(fsm);
558
559 return 0;
560}
561
Lee Jones249516c2014-03-20 09:20:52 +0000562static int stfsm_wrvcr(struct stfsm *fsm, uint8_t data)
563{
564 struct stfsm_seq *seq = &stfsm_seq_wrvcr;
565
566 dev_dbg(fsm->dev, "writing VCR 0x%02x\n", data);
567
568 seq->status = (STA_DATA_BYTE1(data) | STA_PADS_1 | STA_CSDEASSERT);
569
570 stfsm_load_seq(fsm, seq);
571
572 stfsm_wait_seq(fsm);
573
574 return 0;
575}
576
Lee Jones0ea7d702014-03-20 09:20:50 +0000577/*
578 * SoC reset on 'boot-from-spi' systems
579 *
580 * Certain modes of operation cause the Flash device to enter a particular state
581 * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
582 * Addr' commands). On boot-from-spi systems, it is important to consider what
583 * happens if a warm reset occurs during this period. The SPIBoot controller
584 * assumes that Flash device is in its default reset state, 24-bit address mode,
585 * and ready to accept commands. This can be achieved using some form of
586 * on-board logic/controller to force a device POR in response to a SoC-level
587 * reset or by making use of the device reset signal if available (limited
588 * number of devices only).
589 *
590 * Failure to take such precautions can cause problems following a warm reset.
591 * For some operations (e.g. ERASE), there is little that can be done. For
592 * other modes of operation (e.g. 32-bit addressing), options are often
593 * available that can help minimise the window in which a reset could cause a
594 * problem.
595 *
596 */
597static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
598{
599 /* Reset signal is available on the board and supported by the device */
600 if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
601 return true;
602
603 /* Board-level logic forces a power-on-reset */
604 if (fsm->reset_por)
605 return true;
606
607 /* Reset is not properly handled and may result in failure to reboot */
608 return false;
609}
610
Lee Jonesfa5ba3a2014-03-20 09:20:47 +0000611/* Configure 'addr_cfg' according to addressing mode */
612static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
613 struct stfsm_seq *seq)
614{
615 int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
616
617 seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
618 ADR_CFG_PADS_1_ADD1 |
619 ADR_CFG_CYCLES_ADD2(16) |
620 ADR_CFG_PADS_1_ADD2 |
621 ADR_CFG_CSDEASSERT_ADD2);
622}
623
Lee Jones08981272014-03-20 09:20:42 +0000624/* Search for preferred configuration based on available flags */
625static struct seq_rw_config *
626stfsm_search_seq_rw_configs(struct stfsm *fsm,
627 struct seq_rw_config cfgs[])
628{
629 struct seq_rw_config *config;
630 int flags = fsm->info->flags;
631
632 for (config = cfgs; config->cmd != 0; config++)
633 if ((config->flags & flags) == config->flags)
634 return config;
635
636 return NULL;
637}
638
Lee Jones97ccf2d2014-03-20 09:20:44 +0000639/* Prepare a READ/WRITE sequence according to configuration parameters */
640static void stfsm_prepare_rw_seq(struct stfsm *fsm,
641 struct stfsm_seq *seq,
642 struct seq_rw_config *cfg)
643{
644 int addr1_cycles, addr2_cycles;
645 int i = 0;
646
647 memset(seq, 0, sizeof(*seq));
648
649 /* Add READ/WRITE OPC */
650 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
651 SEQ_OPC_CYCLES(8) |
652 SEQ_OPC_OPCODE(cfg->cmd));
653
654 /* Add WREN OPC for a WRITE sequence */
655 if (cfg->write)
656 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
657 SEQ_OPC_CYCLES(8) |
658 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
659 SEQ_OPC_CSDEASSERT);
660
661 /* Address configuration (24 or 32-bit addresses) */
662 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
663 addr1_cycles /= cfg->addr_pads;
664 addr2_cycles = 16 / cfg->addr_pads;
665 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
666 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
667 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
668 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
669
670 /* Data/Sequence configuration */
671 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
672 SEQ_CFG_STARTSEQ |
673 SEQ_CFG_CSDEASSERT);
674 if (!cfg->write)
675 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
676
677 /* Mode configuration (no. of pads taken from addr cfg) */
678 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
679 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
680 (cfg->addr_pads - 1) << 22); /* pads */
681
682 /* Dummy configuration (no. of pads taken from addr cfg) */
683 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
684 (cfg->addr_pads - 1) << 22); /* pads */
685
686
687 /* Instruction sequence */
688 i = 0;
689 if (cfg->write)
690 seq->seq[i++] = STFSM_INST_CMD2;
691
692 seq->seq[i++] = STFSM_INST_CMD1;
693
694 seq->seq[i++] = STFSM_INST_ADD1;
695 seq->seq[i++] = STFSM_INST_ADD2;
696
697 if (cfg->mode_cycles)
698 seq->seq[i++] = STFSM_INST_MODE;
699
700 if (cfg->dummy_cycles)
701 seq->seq[i++] = STFSM_INST_DUMMY;
702
703 seq->seq[i++] =
704 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
705 seq->seq[i++] = STFSM_INST_STOP;
706}
707
Lee Jones88cccb82014-03-20 09:20:49 +0000708static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
709 struct stfsm_seq *seq,
710 struct seq_rw_config *cfgs)
711{
712 struct seq_rw_config *config;
713
714 config = stfsm_search_seq_rw_configs(fsm, cfgs);
715 if (!config) {
716 dev_err(fsm->dev, "failed to find suitable config\n");
717 return -EINVAL;
718 }
719
720 stfsm_prepare_rw_seq(fsm, seq, config);
721
722 return 0;
723}
724
Lee Jones1bd512b2014-03-20 09:20:38 +0000725static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
726{
727 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
728 uint32_t tmp[2];
729
730 stfsm_load_seq(fsm, seq);
731
732 stfsm_read_fifo(fsm, tmp, 8);
733
734 memcpy(jedec, tmp, 5);
735
736 stfsm_wait_seq(fsm);
737}
738
739static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
740{
Lee Jones24fec652014-03-20 09:20:41 +0000741 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000742 u16 ext_jedec;
743 u32 jedec;
744 u8 id[5];
745
746 stfsm_read_jedec(fsm, id);
747
748 jedec = id[0] << 16 | id[1] << 8 | id[2];
749 /*
750 * JEDEC also defines an optional "extended device information"
751 * string for after vendor-specific data, after the three bytes
752 * we use here. Supporting some chips might require using it.
753 */
754 ext_jedec = id[3] << 8 | id[4];
755
756 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
757 jedec, id[0], id[1], id[2], id[3], id[4]);
758
Lee Jones24fec652014-03-20 09:20:41 +0000759 for (info = flash_types; info->name; info++) {
760 if (info->jedec_id == jedec) {
761 if (info->ext_id && info->ext_id != ext_jedec)
762 continue;
763 return info;
764 }
765 }
766 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
767
Lee Jones1bd512b2014-03-20 09:20:38 +0000768 return NULL;
769}
770
Lee Jones86f309fd2014-03-20 09:20:35 +0000771static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
772{
773 int ret, timeout = 10;
774
775 /* Wait for controller to accept mode change */
776 while (--timeout) {
777 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
778 if (ret & 0x1)
779 break;
780 udelay(1);
781 }
782
783 if (!timeout)
784 return -EBUSY;
785
786 writel(mode, fsm->base + SPI_MODESELECT);
787
788 return 0;
789}
790
791static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
792{
793 uint32_t emi_freq;
794 uint32_t clk_div;
795
796 /* TODO: Make this dynamic */
797 emi_freq = STFSM_DEFAULT_EMI_FREQ;
798
799 /*
800 * Calculate clk_div - values between 2 and 128
801 * Multiple of 2, rounded up
802 */
803 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
804 if (clk_div < 2)
805 clk_div = 2;
806 else if (clk_div > 128)
807 clk_div = 128;
808
809 /*
810 * Determine a suitable delay for the IP to complete a change of
811 * direction of the FIFO. The required delay is related to the clock
812 * divider used. The following heuristics are based on empirical tests,
813 * using a 100MHz EMI clock.
814 */
815 if (clk_div <= 4)
816 fsm->fifo_dir_delay = 0;
817 else if (clk_div <= 10)
818 fsm->fifo_dir_delay = 1;
819 else
820 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
821
822 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
823 emi_freq, spi_freq, clk_div);
824
825 writel(clk_div, fsm->base + SPI_CLOCKDIV);
826}
827
828static int stfsm_init(struct stfsm *fsm)
829{
830 int ret;
831
832 /* Perform a soft reset of the FSM controller */
833 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
834 udelay(1);
835 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
836
837 /* Set clock to 'safe' frequency initially */
838 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
839
840 /* Switch to FSM */
841 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
842 if (ret)
843 return ret;
844
845 /* Set timing parameters */
846 writel(SPI_CFG_DEVICE_ST |
847 SPI_CFG_DEFAULT_MIN_CS_HIGH |
848 SPI_CFG_DEFAULT_CS_SETUPHOLD |
849 SPI_CFG_DEFAULT_DATA_HOLD,
850 fsm->base + SPI_CONFIGDATA);
851 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
852
853 /* Clear FIFO, just in case */
854 stfsm_clear_fifo(fsm);
855
856 return 0;
857}
858
Lee Jonesa63984c2014-03-20 09:20:46 +0000859static void stfsm_fetch_platform_configs(struct platform_device *pdev)
860{
861 struct stfsm *fsm = platform_get_drvdata(pdev);
862 struct device_node *np = pdev->dev.of_node;
863 struct regmap *regmap;
864 uint32_t boot_device_reg;
865 uint32_t boot_device_spi;
866 uint32_t boot_device; /* Value we read from *boot_device_reg */
867 int ret;
868
869 /* Booting from SPI NOR Flash is the default */
870 fsm->booted_from_spi = true;
871
872 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
873 if (IS_ERR(regmap))
874 goto boot_device_fail;
875
Lee Jones0ea7d702014-03-20 09:20:50 +0000876 fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
877
878 fsm->reset_por = of_property_read_bool(np, "st,reset-por");
879
Lee Jonesa63984c2014-03-20 09:20:46 +0000880 /* Where in the syscon the boot device information lives */
881 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
882 if (ret)
883 goto boot_device_fail;
884
885 /* Boot device value when booted from SPI NOR */
886 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
887 if (ret)
888 goto boot_device_fail;
889
890 ret = regmap_read(regmap, boot_device_reg, &boot_device);
891 if (ret)
892 goto boot_device_fail;
893
894 if (boot_device != boot_device_spi)
895 fsm->booted_from_spi = false;
896
897 return;
898
899boot_device_fail:
900 dev_warn(&pdev->dev,
901 "failed to fetch boot device, assuming boot from SPI\n");
902}
903
Lee Jonesd90db4a2014-03-20 09:20:33 +0000904static int stfsm_probe(struct platform_device *pdev)
905{
906 struct device_node *np = pdev->dev.of_node;
Lee Jones24fec652014-03-20 09:20:41 +0000907 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000908 struct resource *res;
909 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +0000910 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000911
912 if (!np) {
913 dev_err(&pdev->dev, "No DT found\n");
914 return -EINVAL;
915 }
916
917 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
918 if (!fsm)
919 return -ENOMEM;
920
921 fsm->dev = &pdev->dev;
922
923 platform_set_drvdata(pdev, fsm);
924
925 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
926 if (!res) {
927 dev_err(&pdev->dev, "Resource not found\n");
928 return -ENODEV;
929 }
930
931 fsm->base = devm_ioremap_resource(&pdev->dev, res);
932 if (IS_ERR(fsm->base)) {
933 dev_err(&pdev->dev,
934 "Failed to reserve memory region %pR\n", res);
935 return PTR_ERR(fsm->base);
936 }
937
938 mutex_init(&fsm->lock);
939
Lee Jones86f309fd2014-03-20 09:20:35 +0000940 ret = stfsm_init(fsm);
941 if (ret) {
942 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
943 return ret;
944 }
945
Lee Jonesa63984c2014-03-20 09:20:46 +0000946 stfsm_fetch_platform_configs(pdev);
947
Lee Jones1bd512b2014-03-20 09:20:38 +0000948 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +0000949 info = stfsm_jedec_probe(fsm);
950 if (!info)
951 return -ENODEV;
952 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000953
Lee Jones3b5d1982014-03-20 09:20:43 +0000954 /* Use device size to determine address width */
955 if (info->sector_size * info->n_sectors > 0x1000000)
956 info->flags |= FLASH_FLAG_32BIT_ADDR;
957
Lee Jonesd90db4a2014-03-20 09:20:33 +0000958 fsm->mtd.dev.parent = &pdev->dev;
959 fsm->mtd.type = MTD_NORFLASH;
960 fsm->mtd.writesize = 4;
961 fsm->mtd.writebufsize = fsm->mtd.writesize;
962 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +0000963 fsm->mtd.size = info->sector_size * info->n_sectors;
964 fsm->mtd.erasesize = info->sector_size;
965
966 dev_err(&pdev->dev,
967 "Found serial flash device: %s\n"
968 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
969 info->name,
970 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
971 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +0000972
973 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
974}
975
976static int stfsm_remove(struct platform_device *pdev)
977{
978 struct stfsm *fsm = platform_get_drvdata(pdev);
979 int err;
980
981 err = mtd_device_unregister(&fsm->mtd);
982 if (err)
983 return err;
984
985 return 0;
986}
987
988static struct of_device_id stfsm_match[] = {
989 { .compatible = "st,spi-fsm", },
990 {},
991};
992MODULE_DEVICE_TABLE(of, stfsm_match);
993
994static struct platform_driver stfsm_driver = {
995 .probe = stfsm_probe,
996 .remove = stfsm_remove,
997 .driver = {
998 .name = "st-spi-fsm",
999 .owner = THIS_MODULE,
1000 .of_match_table = stfsm_match,
1001 },
1002};
1003module_platform_driver(stfsm_driver);
1004
1005MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
1006MODULE_DESCRIPTION("ST SPI FSM driver");
1007MODULE_LICENSE("GPL");