blob: 6be2672777561247cf7646223eda62c9069cd3d2 [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 Jonesd90db4a2014-03-20 09:20:33 +0000213};
214
Lee Jones3c8b85b2014-03-20 09:20:36 +0000215struct stfsm_seq {
216 uint32_t data_size;
217 uint32_t addr1;
218 uint32_t addr2;
219 uint32_t addr_cfg;
220 uint32_t seq_opc[5];
221 uint32_t mode;
222 uint32_t dummy;
223 uint32_t status;
224 uint8_t seq[16];
225 uint32_t seq_cfg;
226} __packed __aligned(4);
227
Lee Jones08981272014-03-20 09:20:42 +0000228/* Parameters to configure a READ or WRITE FSM sequence */
229struct seq_rw_config {
230 uint32_t flags; /* flags to support config */
231 uint8_t cmd; /* FLASH command */
232 int write; /* Write Sequence */
233 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
234 uint8_t data_pads; /* No. of data pads */
235 uint8_t mode_data; /* MODE data */
236 uint8_t mode_cycles; /* No. of MODE cycles */
237 uint8_t dummy_cycles; /* No. of DUMMY cycles */
238};
239
Lee Jones11d7f822014-03-20 09:20:40 +0000240/* SPI Flash Device Table */
241struct flash_info {
242 char *name;
243 /*
244 * JEDEC id zero means "no ID" (most older chips); otherwise it has
245 * a high byte of zero plus three data bytes: the manufacturer id,
246 * then a two byte device id.
247 */
248 u32 jedec_id;
249 u16 ext_id;
250 /*
251 * The size listed here is what works with FLASH_CMD_SE, which isn't
252 * necessarily called a "sector" by the vendor.
253 */
254 unsigned sector_size;
255 u16 n_sectors;
256 u32 flags;
257 /*
258 * Note, where FAST_READ is supported, freq_max specifies the
259 * FAST_READ frequency, not the READ frequency.
260 */
261 u32 max_freq;
262 int (*config)(struct stfsm *);
263};
264
265static struct flash_info flash_types[] = {
266 /*
267 * ST Microelectronics/Numonyx --
268 * (newer production versions may have feature updates
269 * (eg faster operating frequency)
270 */
271#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
272 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
273 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
274 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
275 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
276 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
277 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
278
279#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
280 FLASH_FLAG_READ_FAST | \
281 FLASH_FLAG_READ_1_1_2 | \
282 FLASH_FLAG_WRITE_1_1_2)
283 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
284 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
285
286#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
287 FLASH_FLAG_READ_FAST | \
288 FLASH_FLAG_READ_1_1_2 | \
289 FLASH_FLAG_READ_1_2_2 | \
290 FLASH_FLAG_READ_1_1_4 | \
291 FLASH_FLAG_READ_1_4_4 | \
292 FLASH_FLAG_SE_4K | \
293 FLASH_FLAG_SE_32K)
294 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
295 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
296
297#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
298 FLASH_FLAG_READ_FAST | \
299 FLASH_FLAG_READ_1_1_2 | \
300 FLASH_FLAG_READ_1_2_2 | \
301 FLASH_FLAG_READ_1_1_4 | \
302 FLASH_FLAG_READ_1_4_4 | \
303 FLASH_FLAG_WRITE_1_1_2 | \
304 FLASH_FLAG_WRITE_1_2_2 | \
305 FLASH_FLAG_WRITE_1_1_4 | \
306 FLASH_FLAG_WRITE_1_4_4)
307 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108, NULL },
308 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
309 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, NULL },
310
311 /*
312 * Spansion S25FLxxxP
313 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
314 */
315#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
316 FLASH_FLAG_READ_1_1_2 | \
317 FLASH_FLAG_READ_1_2_2 | \
318 FLASH_FLAG_READ_1_1_4 | \
319 FLASH_FLAG_READ_1_4_4 | \
320 FLASH_FLAG_WRITE_1_1_4 | \
321 FLASH_FLAG_READ_FAST)
322 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
323 NULL },
324 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
325 NULL },
326
327 /*
328 * Spansion S25FLxxxS
329 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
330 * - RESET# signal supported by die but not bristled out on all
331 * package types. The package type is a function of board design,
332 * so this information is captured in the board's flags.
333 * - Supports 'DYB' sector protection. Depending on variant, sectors
334 * may default to locked state on power-on.
335 */
336#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
337 FLASH_FLAG_RESET | \
338 FLASH_FLAG_DYB_LOCKING)
339 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
340 NULL },
341 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
342 NULL },
343 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
344 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
345 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
346 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
347
348 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
349#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
350 FLASH_FLAG_READ_FAST | \
351 FLASH_FLAG_READ_1_1_2 | \
352 FLASH_FLAG_WRITE_1_1_2)
353 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
354 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
355 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
356 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
357 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
358
359 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
360#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
361 FLASH_FLAG_READ_FAST | \
362 FLASH_FLAG_READ_1_1_2 | \
363 FLASH_FLAG_READ_1_2_2 | \
364 FLASH_FLAG_READ_1_1_4 | \
365 FLASH_FLAG_READ_1_4_4 | \
366 FLASH_FLAG_WRITE_1_1_4)
367 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
368 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
369 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
370 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
371
372 /* Sentinel */
373 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
374};
375
Lee Jones1bd512b2014-03-20 09:20:38 +0000376static struct stfsm_seq stfsm_seq_read_jedec = {
377 .data_size = TRANSFER_SIZE(8),
378 .seq_opc[0] = (SEQ_OPC_PADS_1 |
379 SEQ_OPC_CYCLES(8) |
380 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
381 .seq = {
382 STFSM_INST_CMD1,
383 STFSM_INST_DATA_READ,
384 STFSM_INST_STOP,
385 },
386 .seq_cfg = (SEQ_CFG_PADS_1 |
387 SEQ_CFG_READNOTWRITE |
388 SEQ_CFG_CSDEASSERT |
389 SEQ_CFG_STARTSEQ),
390};
391
Lee Jones3c8b85b2014-03-20 09:20:36 +0000392static inline int stfsm_is_idle(struct stfsm *fsm)
393{
394 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
395}
396
Lee Jones86f309fd2014-03-20 09:20:35 +0000397static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
398{
399 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
400}
401
402static void stfsm_clear_fifo(struct stfsm *fsm)
403{
404 uint32_t avail;
405
406 for (;;) {
407 avail = stfsm_fifo_available(fsm);
408 if (!avail)
409 break;
410
411 while (avail) {
412 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
413 avail--;
414 }
415 }
416}
417
Lee Jones3c8b85b2014-03-20 09:20:36 +0000418static inline void stfsm_load_seq(struct stfsm *fsm,
419 const struct stfsm_seq *seq)
420{
421 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
422 const uint32_t *src = (const uint32_t *)seq;
423 int words = sizeof(*seq) / sizeof(*src);
424
425 BUG_ON(!stfsm_is_idle(fsm));
426
427 while (words--) {
428 writel(*src, dst);
429 src++;
430 dst += 4;
431 }
432}
433
434static void stfsm_wait_seq(struct stfsm *fsm)
435{
436 unsigned long deadline;
437 int timeout = 0;
438
439 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
440
441 while (!timeout) {
442 if (time_after_eq(jiffies, deadline))
443 timeout = 1;
444
445 if (stfsm_is_idle(fsm))
446 return;
447
448 cond_resched();
449 }
450
451 dev_err(fsm->dev, "timeout on sequence completion\n");
452}
453
Lee Jones030e82d2014-03-20 09:20:37 +0000454static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
455 const uint32_t size)
456{
457 uint32_t remaining = size >> 2;
458 uint32_t avail;
459 uint32_t words;
460
461 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
462
463 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
464
465 while (remaining) {
466 for (;;) {
467 avail = stfsm_fifo_available(fsm);
468 if (avail)
469 break;
470 udelay(1);
471 }
472 words = min(avail, remaining);
473 remaining -= words;
474
475 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
476 buf += words;
477 }
478}
479
Lee Jones08981272014-03-20 09:20:42 +0000480/* Search for preferred configuration based on available flags */
481static struct seq_rw_config *
482stfsm_search_seq_rw_configs(struct stfsm *fsm,
483 struct seq_rw_config cfgs[])
484{
485 struct seq_rw_config *config;
486 int flags = fsm->info->flags;
487
488 for (config = cfgs; config->cmd != 0; config++)
489 if ((config->flags & flags) == config->flags)
490 return config;
491
492 return NULL;
493}
494
Lee Jones97ccf2d2014-03-20 09:20:44 +0000495/* Prepare a READ/WRITE sequence according to configuration parameters */
496static void stfsm_prepare_rw_seq(struct stfsm *fsm,
497 struct stfsm_seq *seq,
498 struct seq_rw_config *cfg)
499{
500 int addr1_cycles, addr2_cycles;
501 int i = 0;
502
503 memset(seq, 0, sizeof(*seq));
504
505 /* Add READ/WRITE OPC */
506 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
507 SEQ_OPC_CYCLES(8) |
508 SEQ_OPC_OPCODE(cfg->cmd));
509
510 /* Add WREN OPC for a WRITE sequence */
511 if (cfg->write)
512 seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
513 SEQ_OPC_CYCLES(8) |
514 SEQ_OPC_OPCODE(FLASH_CMD_WREN) |
515 SEQ_OPC_CSDEASSERT);
516
517 /* Address configuration (24 or 32-bit addresses) */
518 addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
519 addr1_cycles /= cfg->addr_pads;
520 addr2_cycles = 16 / cfg->addr_pads;
521 seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
522 (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
523 (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
524 ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
525
526 /* Data/Sequence configuration */
527 seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
528 SEQ_CFG_STARTSEQ |
529 SEQ_CFG_CSDEASSERT);
530 if (!cfg->write)
531 seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
532
533 /* Mode configuration (no. of pads taken from addr cfg) */
534 seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
535 (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
536 (cfg->addr_pads - 1) << 22); /* pads */
537
538 /* Dummy configuration (no. of pads taken from addr cfg) */
539 seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
540 (cfg->addr_pads - 1) << 22); /* pads */
541
542
543 /* Instruction sequence */
544 i = 0;
545 if (cfg->write)
546 seq->seq[i++] = STFSM_INST_CMD2;
547
548 seq->seq[i++] = STFSM_INST_CMD1;
549
550 seq->seq[i++] = STFSM_INST_ADD1;
551 seq->seq[i++] = STFSM_INST_ADD2;
552
553 if (cfg->mode_cycles)
554 seq->seq[i++] = STFSM_INST_MODE;
555
556 if (cfg->dummy_cycles)
557 seq->seq[i++] = STFSM_INST_DUMMY;
558
559 seq->seq[i++] =
560 cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
561 seq->seq[i++] = STFSM_INST_STOP;
562}
563
Lee Jones1bd512b2014-03-20 09:20:38 +0000564static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
565{
566 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
567 uint32_t tmp[2];
568
569 stfsm_load_seq(fsm, seq);
570
571 stfsm_read_fifo(fsm, tmp, 8);
572
573 memcpy(jedec, tmp, 5);
574
575 stfsm_wait_seq(fsm);
576}
577
578static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
579{
Lee Jones24fec652014-03-20 09:20:41 +0000580 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000581 u16 ext_jedec;
582 u32 jedec;
583 u8 id[5];
584
585 stfsm_read_jedec(fsm, id);
586
587 jedec = id[0] << 16 | id[1] << 8 | id[2];
588 /*
589 * JEDEC also defines an optional "extended device information"
590 * string for after vendor-specific data, after the three bytes
591 * we use here. Supporting some chips might require using it.
592 */
593 ext_jedec = id[3] << 8 | id[4];
594
595 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
596 jedec, id[0], id[1], id[2], id[3], id[4]);
597
Lee Jones24fec652014-03-20 09:20:41 +0000598 for (info = flash_types; info->name; info++) {
599 if (info->jedec_id == jedec) {
600 if (info->ext_id && info->ext_id != ext_jedec)
601 continue;
602 return info;
603 }
604 }
605 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
606
Lee Jones1bd512b2014-03-20 09:20:38 +0000607 return NULL;
608}
609
Lee Jones86f309fd2014-03-20 09:20:35 +0000610static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
611{
612 int ret, timeout = 10;
613
614 /* Wait for controller to accept mode change */
615 while (--timeout) {
616 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
617 if (ret & 0x1)
618 break;
619 udelay(1);
620 }
621
622 if (!timeout)
623 return -EBUSY;
624
625 writel(mode, fsm->base + SPI_MODESELECT);
626
627 return 0;
628}
629
630static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
631{
632 uint32_t emi_freq;
633 uint32_t clk_div;
634
635 /* TODO: Make this dynamic */
636 emi_freq = STFSM_DEFAULT_EMI_FREQ;
637
638 /*
639 * Calculate clk_div - values between 2 and 128
640 * Multiple of 2, rounded up
641 */
642 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
643 if (clk_div < 2)
644 clk_div = 2;
645 else if (clk_div > 128)
646 clk_div = 128;
647
648 /*
649 * Determine a suitable delay for the IP to complete a change of
650 * direction of the FIFO. The required delay is related to the clock
651 * divider used. The following heuristics are based on empirical tests,
652 * using a 100MHz EMI clock.
653 */
654 if (clk_div <= 4)
655 fsm->fifo_dir_delay = 0;
656 else if (clk_div <= 10)
657 fsm->fifo_dir_delay = 1;
658 else
659 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
660
661 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
662 emi_freq, spi_freq, clk_div);
663
664 writel(clk_div, fsm->base + SPI_CLOCKDIV);
665}
666
667static int stfsm_init(struct stfsm *fsm)
668{
669 int ret;
670
671 /* Perform a soft reset of the FSM controller */
672 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
673 udelay(1);
674 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
675
676 /* Set clock to 'safe' frequency initially */
677 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
678
679 /* Switch to FSM */
680 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
681 if (ret)
682 return ret;
683
684 /* Set timing parameters */
685 writel(SPI_CFG_DEVICE_ST |
686 SPI_CFG_DEFAULT_MIN_CS_HIGH |
687 SPI_CFG_DEFAULT_CS_SETUPHOLD |
688 SPI_CFG_DEFAULT_DATA_HOLD,
689 fsm->base + SPI_CONFIGDATA);
690 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
691
692 /* Clear FIFO, just in case */
693 stfsm_clear_fifo(fsm);
694
695 return 0;
696}
697
Lee Jonesa63984c2014-03-20 09:20:46 +0000698static void stfsm_fetch_platform_configs(struct platform_device *pdev)
699{
700 struct stfsm *fsm = platform_get_drvdata(pdev);
701 struct device_node *np = pdev->dev.of_node;
702 struct regmap *regmap;
703 uint32_t boot_device_reg;
704 uint32_t boot_device_spi;
705 uint32_t boot_device; /* Value we read from *boot_device_reg */
706 int ret;
707
708 /* Booting from SPI NOR Flash is the default */
709 fsm->booted_from_spi = true;
710
711 regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
712 if (IS_ERR(regmap))
713 goto boot_device_fail;
714
715 /* Where in the syscon the boot device information lives */
716 ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
717 if (ret)
718 goto boot_device_fail;
719
720 /* Boot device value when booted from SPI NOR */
721 ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
722 if (ret)
723 goto boot_device_fail;
724
725 ret = regmap_read(regmap, boot_device_reg, &boot_device);
726 if (ret)
727 goto boot_device_fail;
728
729 if (boot_device != boot_device_spi)
730 fsm->booted_from_spi = false;
731
732 return;
733
734boot_device_fail:
735 dev_warn(&pdev->dev,
736 "failed to fetch boot device, assuming boot from SPI\n");
737}
738
Lee Jonesd90db4a2014-03-20 09:20:33 +0000739static int stfsm_probe(struct platform_device *pdev)
740{
741 struct device_node *np = pdev->dev.of_node;
Lee Jones24fec652014-03-20 09:20:41 +0000742 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000743 struct resource *res;
744 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +0000745 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000746
747 if (!np) {
748 dev_err(&pdev->dev, "No DT found\n");
749 return -EINVAL;
750 }
751
752 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
753 if (!fsm)
754 return -ENOMEM;
755
756 fsm->dev = &pdev->dev;
757
758 platform_set_drvdata(pdev, fsm);
759
760 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
761 if (!res) {
762 dev_err(&pdev->dev, "Resource not found\n");
763 return -ENODEV;
764 }
765
766 fsm->base = devm_ioremap_resource(&pdev->dev, res);
767 if (IS_ERR(fsm->base)) {
768 dev_err(&pdev->dev,
769 "Failed to reserve memory region %pR\n", res);
770 return PTR_ERR(fsm->base);
771 }
772
773 mutex_init(&fsm->lock);
774
Lee Jones86f309fd2014-03-20 09:20:35 +0000775 ret = stfsm_init(fsm);
776 if (ret) {
777 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
778 return ret;
779 }
780
Lee Jonesa63984c2014-03-20 09:20:46 +0000781 stfsm_fetch_platform_configs(pdev);
782
Lee Jones1bd512b2014-03-20 09:20:38 +0000783 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +0000784 info = stfsm_jedec_probe(fsm);
785 if (!info)
786 return -ENODEV;
787 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000788
Lee Jones3b5d1982014-03-20 09:20:43 +0000789 /* Use device size to determine address width */
790 if (info->sector_size * info->n_sectors > 0x1000000)
791 info->flags |= FLASH_FLAG_32BIT_ADDR;
792
Lee Jonesd90db4a2014-03-20 09:20:33 +0000793 fsm->mtd.dev.parent = &pdev->dev;
794 fsm->mtd.type = MTD_NORFLASH;
795 fsm->mtd.writesize = 4;
796 fsm->mtd.writebufsize = fsm->mtd.writesize;
797 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +0000798 fsm->mtd.size = info->sector_size * info->n_sectors;
799 fsm->mtd.erasesize = info->sector_size;
800
801 dev_err(&pdev->dev,
802 "Found serial flash device: %s\n"
803 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
804 info->name,
805 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
806 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +0000807
808 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
809}
810
811static int stfsm_remove(struct platform_device *pdev)
812{
813 struct stfsm *fsm = platform_get_drvdata(pdev);
814 int err;
815
816 err = mtd_device_unregister(&fsm->mtd);
817 if (err)
818 return err;
819
820 return 0;
821}
822
823static struct of_device_id stfsm_match[] = {
824 { .compatible = "st,spi-fsm", },
825 {},
826};
827MODULE_DEVICE_TABLE(of, stfsm_match);
828
829static struct platform_driver stfsm_driver = {
830 .probe = stfsm_probe,
831 .remove = stfsm_remove,
832 .driver = {
833 .name = "st-spi-fsm",
834 .owner = THIS_MODULE,
835 .of_match_table = stfsm_match,
836 },
837};
838module_platform_driver(stfsm_driver);
839
840MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
841MODULE_DESCRIPTION("ST SPI FSM driver");
842MODULE_LICENSE("GPL");