blob: 5a1b470ce9087017eeaa678c7e062a8af5787a2e [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>
17#include <linux/platform_device.h>
18#include <linux/mtd/mtd.h>
19#include <linux/sched.h>
20#include <linux/delay.h>
21#include <linux/io.h>
22#include <linux/of.h>
23
Lee Jones5549fbd2014-03-20 09:20:39 +000024#include "serial_flash_cmds.h"
25
Lee Jonesbc09fb52014-03-20 09:20:34 +000026/*
27 * FSM SPI Controller Registers
28 */
29#define SPI_CLOCKDIV 0x0010
30#define SPI_MODESELECT 0x0018
31#define SPI_CONFIGDATA 0x0020
32#define SPI_STA_MODE_CHANGE 0x0028
33#define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
34#define SPI_FAST_SEQ_ADD1 0x0104
35#define SPI_FAST_SEQ_ADD2 0x0108
36#define SPI_FAST_SEQ_ADD_CFG 0x010c
37#define SPI_FAST_SEQ_OPC1 0x0110
38#define SPI_FAST_SEQ_OPC2 0x0114
39#define SPI_FAST_SEQ_OPC3 0x0118
40#define SPI_FAST_SEQ_OPC4 0x011c
41#define SPI_FAST_SEQ_OPC5 0x0120
42#define SPI_MODE_BITS 0x0124
43#define SPI_DUMMY_BITS 0x0128
44#define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
45#define SPI_FAST_SEQ_1 0x0130
46#define SPI_FAST_SEQ_2 0x0134
47#define SPI_FAST_SEQ_3 0x0138
48#define SPI_FAST_SEQ_4 0x013c
49#define SPI_FAST_SEQ_CFG 0x0140
50#define SPI_FAST_SEQ_STA 0x0144
51#define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
52#define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
53#define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
54#define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
55#define SPI_PROGRAM_ERASE_TIME 0x0158
56#define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
57#define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
58#define SPI_STATUS_WR_TIME_REG 0x0164
59#define SPI_FAST_SEQ_DATA_REG 0x0300
60
61/*
62 * Register: SPI_MODESELECT
63 */
64#define SPI_MODESELECT_CONTIG 0x01
65#define SPI_MODESELECT_FASTREAD 0x02
66#define SPI_MODESELECT_DUALIO 0x04
67#define SPI_MODESELECT_FSM 0x08
68#define SPI_MODESELECT_QUADBOOT 0x10
69
70/*
71 * Register: SPI_CONFIGDATA
72 */
73#define SPI_CFG_DEVICE_ST 0x1
74#define SPI_CFG_DEVICE_ATMEL 0x4
75#define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
76#define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
77#define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
78
Lee Jones86f309fd2014-03-20 09:20:35 +000079#define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
80#define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
81#define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
82
Lee Jonesbc09fb52014-03-20 09:20:34 +000083/*
84 * Register: SPI_FAST_SEQ_TRANSFER_SIZE
85 */
86#define TRANSFER_SIZE(x) ((x) * 8)
87
88/*
89 * Register: SPI_FAST_SEQ_ADD_CFG
90 */
91#define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
92#define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
93#define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
94#define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
95#define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
96#define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
97#define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
98#define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
99#define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
100#define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
101
102/*
103 * Register: SPI_FAST_SEQ_n
104 */
105#define SEQ_OPC_OPCODE(x) ((x) << 0)
106#define SEQ_OPC_CYCLES(x) ((x) << 8)
107#define SEQ_OPC_PADS_1 (0x0 << 14)
108#define SEQ_OPC_PADS_2 (0x1 << 14)
109#define SEQ_OPC_PADS_4 (0x3 << 14)
110#define SEQ_OPC_CSDEASSERT (1 << 16)
111
112/*
113 * Register: SPI_FAST_SEQ_CFG
114 */
115#define SEQ_CFG_STARTSEQ (1 << 0)
116#define SEQ_CFG_SWRESET (1 << 5)
117#define SEQ_CFG_CSDEASSERT (1 << 6)
118#define SEQ_CFG_READNOTWRITE (1 << 7)
119#define SEQ_CFG_ERASE (1 << 8)
120#define SEQ_CFG_PADS_1 (0x0 << 16)
121#define SEQ_CFG_PADS_2 (0x1 << 16)
122#define SEQ_CFG_PADS_4 (0x3 << 16)
123
124/*
125 * Register: SPI_MODE_BITS
126 */
127#define MODE_DATA(x) (x & 0xff)
128#define MODE_CYCLES(x) ((x & 0x3f) << 16)
129#define MODE_PADS_1 (0x0 << 22)
130#define MODE_PADS_2 (0x1 << 22)
131#define MODE_PADS_4 (0x3 << 22)
132#define DUMMY_CSDEASSERT (1 << 24)
133
134/*
135 * Register: SPI_DUMMY_BITS
136 */
137#define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
138#define DUMMY_PADS_1 (0x0 << 22)
139#define DUMMY_PADS_2 (0x1 << 22)
140#define DUMMY_PADS_4 (0x3 << 22)
141#define DUMMY_CSDEASSERT (1 << 24)
142
143/*
144 * Register: SPI_FAST_SEQ_FLASH_STA_DATA
145 */
146#define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
147#define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
148#define STA_PADS_1 (0x0 << 16)
149#define STA_PADS_2 (0x1 << 16)
150#define STA_PADS_4 (0x3 << 16)
151#define STA_CSDEASSERT (0x1 << 20)
152#define STA_RDNOTWR (0x1 << 21)
153
154/*
155 * FSM SPI Instruction Opcodes
156 */
157#define STFSM_OPC_CMD 0x1
158#define STFSM_OPC_ADD 0x2
159#define STFSM_OPC_STA 0x3
160#define STFSM_OPC_MODE 0x4
161#define STFSM_OPC_DUMMY 0x5
162#define STFSM_OPC_DATA 0x6
163#define STFSM_OPC_WAIT 0x7
164#define STFSM_OPC_JUMP 0x8
165#define STFSM_OPC_GOTO 0x9
166#define STFSM_OPC_STOP 0xF
167
168/*
169 * FSM SPI Instructions (== opcode + operand).
170 */
171#define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
172
173#define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
174#define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
175#define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
176#define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
177#define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
178#define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
179#define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
180
181#define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
182#define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
183
184#define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
185#define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
186#define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
187#define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
188
189#define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
190#define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
191#define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
192#define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
193
Lee Jones86f309fd2014-03-20 09:20:35 +0000194#define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
195#define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
196
197#define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
198
Lee Jones3c8b85b2014-03-20 09:20:36 +0000199#define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
200
Lee Jonesd90db4a2014-03-20 09:20:33 +0000201struct stfsm {
202 struct device *dev;
203 void __iomem *base;
204 struct resource *region;
205 struct mtd_info mtd;
206 struct mutex lock;
Lee Jones24fec652014-03-20 09:20:41 +0000207 struct flash_info *info;
Lee Jones86f309fd2014-03-20 09:20:35 +0000208
209 uint32_t fifo_dir_delay;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000210};
211
Lee Jones3c8b85b2014-03-20 09:20:36 +0000212struct stfsm_seq {
213 uint32_t data_size;
214 uint32_t addr1;
215 uint32_t addr2;
216 uint32_t addr_cfg;
217 uint32_t seq_opc[5];
218 uint32_t mode;
219 uint32_t dummy;
220 uint32_t status;
221 uint8_t seq[16];
222 uint32_t seq_cfg;
223} __packed __aligned(4);
224
Lee Jones08981272014-03-20 09:20:42 +0000225/* Parameters to configure a READ or WRITE FSM sequence */
226struct seq_rw_config {
227 uint32_t flags; /* flags to support config */
228 uint8_t cmd; /* FLASH command */
229 int write; /* Write Sequence */
230 uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
231 uint8_t data_pads; /* No. of data pads */
232 uint8_t mode_data; /* MODE data */
233 uint8_t mode_cycles; /* No. of MODE cycles */
234 uint8_t dummy_cycles; /* No. of DUMMY cycles */
235};
236
Lee Jones11d7f822014-03-20 09:20:40 +0000237/* SPI Flash Device Table */
238struct flash_info {
239 char *name;
240 /*
241 * JEDEC id zero means "no ID" (most older chips); otherwise it has
242 * a high byte of zero plus three data bytes: the manufacturer id,
243 * then a two byte device id.
244 */
245 u32 jedec_id;
246 u16 ext_id;
247 /*
248 * The size listed here is what works with FLASH_CMD_SE, which isn't
249 * necessarily called a "sector" by the vendor.
250 */
251 unsigned sector_size;
252 u16 n_sectors;
253 u32 flags;
254 /*
255 * Note, where FAST_READ is supported, freq_max specifies the
256 * FAST_READ frequency, not the READ frequency.
257 */
258 u32 max_freq;
259 int (*config)(struct stfsm *);
260};
261
262static struct flash_info flash_types[] = {
263 /*
264 * ST Microelectronics/Numonyx --
265 * (newer production versions may have feature updates
266 * (eg faster operating frequency)
267 */
268#define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
269 { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
270 { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
271 { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
272 { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
273 { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
274 { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
275
276#define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
277 FLASH_FLAG_READ_FAST | \
278 FLASH_FLAG_READ_1_1_2 | \
279 FLASH_FLAG_WRITE_1_1_2)
280 { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
281 { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
282
283#define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
284 FLASH_FLAG_READ_FAST | \
285 FLASH_FLAG_READ_1_1_2 | \
286 FLASH_FLAG_READ_1_2_2 | \
287 FLASH_FLAG_READ_1_1_4 | \
288 FLASH_FLAG_READ_1_4_4 | \
289 FLASH_FLAG_SE_4K | \
290 FLASH_FLAG_SE_32K)
291 { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
292 (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70, NULL }
293
294#define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
295 FLASH_FLAG_READ_FAST | \
296 FLASH_FLAG_READ_1_1_2 | \
297 FLASH_FLAG_READ_1_2_2 | \
298 FLASH_FLAG_READ_1_1_4 | \
299 FLASH_FLAG_READ_1_4_4 | \
300 FLASH_FLAG_WRITE_1_1_2 | \
301 FLASH_FLAG_WRITE_1_2_2 | \
302 FLASH_FLAG_WRITE_1_1_4 | \
303 FLASH_FLAG_WRITE_1_4_4)
304 { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108, NULL },
305 { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
306 N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, NULL },
307
308 /*
309 * Spansion S25FLxxxP
310 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
311 */
312#define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
313 FLASH_FLAG_READ_1_1_2 | \
314 FLASH_FLAG_READ_1_2_2 | \
315 FLASH_FLAG_READ_1_1_4 | \
316 FLASH_FLAG_READ_1_4_4 | \
317 FLASH_FLAG_WRITE_1_1_4 | \
318 FLASH_FLAG_READ_FAST)
319 { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
320 NULL },
321 { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
322 NULL },
323
324 /*
325 * Spansion S25FLxxxS
326 * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
327 * - RESET# signal supported by die but not bristled out on all
328 * package types. The package type is a function of board design,
329 * so this information is captured in the board's flags.
330 * - Supports 'DYB' sector protection. Depending on variant, sectors
331 * may default to locked state on power-on.
332 */
333#define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
334 FLASH_FLAG_RESET | \
335 FLASH_FLAG_DYB_LOCKING)
336 { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
337 NULL },
338 { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
339 NULL },
340 { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
341 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
342 { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
343 S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, NULL },
344
345 /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
346#define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
347 FLASH_FLAG_READ_FAST | \
348 FLASH_FLAG_READ_1_1_2 | \
349 FLASH_FLAG_WRITE_1_1_2)
350 { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
351 { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
352 { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
353 { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
354 { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
355
356 /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
357#define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
358 FLASH_FLAG_READ_FAST | \
359 FLASH_FLAG_READ_1_1_2 | \
360 FLASH_FLAG_READ_1_2_2 | \
361 FLASH_FLAG_READ_1_1_4 | \
362 FLASH_FLAG_READ_1_4_4 | \
363 FLASH_FLAG_WRITE_1_1_4)
364 { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80, NULL },
365 { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80, NULL },
366 { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80, NULL },
367 { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80, NULL },
368
369 /* Sentinel */
370 { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
371};
372
Lee Jones1bd512b2014-03-20 09:20:38 +0000373static struct stfsm_seq stfsm_seq_read_jedec = {
374 .data_size = TRANSFER_SIZE(8),
375 .seq_opc[0] = (SEQ_OPC_PADS_1 |
376 SEQ_OPC_CYCLES(8) |
377 SEQ_OPC_OPCODE(FLASH_CMD_RDID)),
378 .seq = {
379 STFSM_INST_CMD1,
380 STFSM_INST_DATA_READ,
381 STFSM_INST_STOP,
382 },
383 .seq_cfg = (SEQ_CFG_PADS_1 |
384 SEQ_CFG_READNOTWRITE |
385 SEQ_CFG_CSDEASSERT |
386 SEQ_CFG_STARTSEQ),
387};
388
Lee Jones3c8b85b2014-03-20 09:20:36 +0000389static inline int stfsm_is_idle(struct stfsm *fsm)
390{
391 return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
392}
393
Lee Jones86f309fd2014-03-20 09:20:35 +0000394static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
395{
396 return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
397}
398
399static void stfsm_clear_fifo(struct stfsm *fsm)
400{
401 uint32_t avail;
402
403 for (;;) {
404 avail = stfsm_fifo_available(fsm);
405 if (!avail)
406 break;
407
408 while (avail) {
409 readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
410 avail--;
411 }
412 }
413}
414
Lee Jones3c8b85b2014-03-20 09:20:36 +0000415static inline void stfsm_load_seq(struct stfsm *fsm,
416 const struct stfsm_seq *seq)
417{
418 void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
419 const uint32_t *src = (const uint32_t *)seq;
420 int words = sizeof(*seq) / sizeof(*src);
421
422 BUG_ON(!stfsm_is_idle(fsm));
423
424 while (words--) {
425 writel(*src, dst);
426 src++;
427 dst += 4;
428 }
429}
430
431static void stfsm_wait_seq(struct stfsm *fsm)
432{
433 unsigned long deadline;
434 int timeout = 0;
435
436 deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
437
438 while (!timeout) {
439 if (time_after_eq(jiffies, deadline))
440 timeout = 1;
441
442 if (stfsm_is_idle(fsm))
443 return;
444
445 cond_resched();
446 }
447
448 dev_err(fsm->dev, "timeout on sequence completion\n");
449}
450
Lee Jones030e82d2014-03-20 09:20:37 +0000451static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf,
452 const uint32_t size)
453{
454 uint32_t remaining = size >> 2;
455 uint32_t avail;
456 uint32_t words;
457
458 dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
459
460 BUG_ON((((uint32_t)buf) & 0x3) || (size & 0x3));
461
462 while (remaining) {
463 for (;;) {
464 avail = stfsm_fifo_available(fsm);
465 if (avail)
466 break;
467 udelay(1);
468 }
469 words = min(avail, remaining);
470 remaining -= words;
471
472 readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
473 buf += words;
474 }
475}
476
Lee Jones08981272014-03-20 09:20:42 +0000477/* Search for preferred configuration based on available flags */
478static struct seq_rw_config *
479stfsm_search_seq_rw_configs(struct stfsm *fsm,
480 struct seq_rw_config cfgs[])
481{
482 struct seq_rw_config *config;
483 int flags = fsm->info->flags;
484
485 for (config = cfgs; config->cmd != 0; config++)
486 if ((config->flags & flags) == config->flags)
487 return config;
488
489 return NULL;
490}
491
Lee Jones1bd512b2014-03-20 09:20:38 +0000492static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *const jedec)
493{
494 const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
495 uint32_t tmp[2];
496
497 stfsm_load_seq(fsm, seq);
498
499 stfsm_read_fifo(fsm, tmp, 8);
500
501 memcpy(jedec, tmp, 5);
502
503 stfsm_wait_seq(fsm);
504}
505
506static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
507{
Lee Jones24fec652014-03-20 09:20:41 +0000508 struct flash_info *info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000509 u16 ext_jedec;
510 u32 jedec;
511 u8 id[5];
512
513 stfsm_read_jedec(fsm, id);
514
515 jedec = id[0] << 16 | id[1] << 8 | id[2];
516 /*
517 * JEDEC also defines an optional "extended device information"
518 * string for after vendor-specific data, after the three bytes
519 * we use here. Supporting some chips might require using it.
520 */
521 ext_jedec = id[3] << 8 | id[4];
522
523 dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
524 jedec, id[0], id[1], id[2], id[3], id[4]);
525
Lee Jones24fec652014-03-20 09:20:41 +0000526 for (info = flash_types; info->name; info++) {
527 if (info->jedec_id == jedec) {
528 if (info->ext_id && info->ext_id != ext_jedec)
529 continue;
530 return info;
531 }
532 }
533 dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
534
Lee Jones1bd512b2014-03-20 09:20:38 +0000535 return NULL;
536}
537
Lee Jones86f309fd2014-03-20 09:20:35 +0000538static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
539{
540 int ret, timeout = 10;
541
542 /* Wait for controller to accept mode change */
543 while (--timeout) {
544 ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
545 if (ret & 0x1)
546 break;
547 udelay(1);
548 }
549
550 if (!timeout)
551 return -EBUSY;
552
553 writel(mode, fsm->base + SPI_MODESELECT);
554
555 return 0;
556}
557
558static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
559{
560 uint32_t emi_freq;
561 uint32_t clk_div;
562
563 /* TODO: Make this dynamic */
564 emi_freq = STFSM_DEFAULT_EMI_FREQ;
565
566 /*
567 * Calculate clk_div - values between 2 and 128
568 * Multiple of 2, rounded up
569 */
570 clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
571 if (clk_div < 2)
572 clk_div = 2;
573 else if (clk_div > 128)
574 clk_div = 128;
575
576 /*
577 * Determine a suitable delay for the IP to complete a change of
578 * direction of the FIFO. The required delay is related to the clock
579 * divider used. The following heuristics are based on empirical tests,
580 * using a 100MHz EMI clock.
581 */
582 if (clk_div <= 4)
583 fsm->fifo_dir_delay = 0;
584 else if (clk_div <= 10)
585 fsm->fifo_dir_delay = 1;
586 else
587 fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
588
589 dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
590 emi_freq, spi_freq, clk_div);
591
592 writel(clk_div, fsm->base + SPI_CLOCKDIV);
593}
594
595static int stfsm_init(struct stfsm *fsm)
596{
597 int ret;
598
599 /* Perform a soft reset of the FSM controller */
600 writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
601 udelay(1);
602 writel(0, fsm->base + SPI_FAST_SEQ_CFG);
603
604 /* Set clock to 'safe' frequency initially */
605 stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
606
607 /* Switch to FSM */
608 ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
609 if (ret)
610 return ret;
611
612 /* Set timing parameters */
613 writel(SPI_CFG_DEVICE_ST |
614 SPI_CFG_DEFAULT_MIN_CS_HIGH |
615 SPI_CFG_DEFAULT_CS_SETUPHOLD |
616 SPI_CFG_DEFAULT_DATA_HOLD,
617 fsm->base + SPI_CONFIGDATA);
618 writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
619
620 /* Clear FIFO, just in case */
621 stfsm_clear_fifo(fsm);
622
623 return 0;
624}
625
Lee Jonesd90db4a2014-03-20 09:20:33 +0000626static int stfsm_probe(struct platform_device *pdev)
627{
628 struct device_node *np = pdev->dev.of_node;
Lee Jones24fec652014-03-20 09:20:41 +0000629 struct flash_info *info;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000630 struct resource *res;
631 struct stfsm *fsm;
Lee Jones86f309fd2014-03-20 09:20:35 +0000632 int ret;
Lee Jonesd90db4a2014-03-20 09:20:33 +0000633
634 if (!np) {
635 dev_err(&pdev->dev, "No DT found\n");
636 return -EINVAL;
637 }
638
639 fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
640 if (!fsm)
641 return -ENOMEM;
642
643 fsm->dev = &pdev->dev;
644
645 platform_set_drvdata(pdev, fsm);
646
647 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
648 if (!res) {
649 dev_err(&pdev->dev, "Resource not found\n");
650 return -ENODEV;
651 }
652
653 fsm->base = devm_ioremap_resource(&pdev->dev, res);
654 if (IS_ERR(fsm->base)) {
655 dev_err(&pdev->dev,
656 "Failed to reserve memory region %pR\n", res);
657 return PTR_ERR(fsm->base);
658 }
659
660 mutex_init(&fsm->lock);
661
Lee Jones86f309fd2014-03-20 09:20:35 +0000662 ret = stfsm_init(fsm);
663 if (ret) {
664 dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
665 return ret;
666 }
667
Lee Jones1bd512b2014-03-20 09:20:38 +0000668 /* Detect SPI FLASH device */
Lee Jones24fec652014-03-20 09:20:41 +0000669 info = stfsm_jedec_probe(fsm);
670 if (!info)
671 return -ENODEV;
672 fsm->info = info;
Lee Jones1bd512b2014-03-20 09:20:38 +0000673
Lee Jones3b5d1982014-03-20 09:20:43 +0000674 /* Use device size to determine address width */
675 if (info->sector_size * info->n_sectors > 0x1000000)
676 info->flags |= FLASH_FLAG_32BIT_ADDR;
677
Lee Jonesd90db4a2014-03-20 09:20:33 +0000678 fsm->mtd.dev.parent = &pdev->dev;
679 fsm->mtd.type = MTD_NORFLASH;
680 fsm->mtd.writesize = 4;
681 fsm->mtd.writebufsize = fsm->mtd.writesize;
682 fsm->mtd.flags = MTD_CAP_NORFLASH;
Lee Jones24fec652014-03-20 09:20:41 +0000683 fsm->mtd.size = info->sector_size * info->n_sectors;
684 fsm->mtd.erasesize = info->sector_size;
685
686 dev_err(&pdev->dev,
687 "Found serial flash device: %s\n"
688 " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
689 info->name,
690 (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
691 fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
Lee Jonesd90db4a2014-03-20 09:20:33 +0000692
693 return mtd_device_parse_register(&fsm->mtd, NULL, NULL, NULL, 0);
694}
695
696static int stfsm_remove(struct platform_device *pdev)
697{
698 struct stfsm *fsm = platform_get_drvdata(pdev);
699 int err;
700
701 err = mtd_device_unregister(&fsm->mtd);
702 if (err)
703 return err;
704
705 return 0;
706}
707
708static struct of_device_id stfsm_match[] = {
709 { .compatible = "st,spi-fsm", },
710 {},
711};
712MODULE_DEVICE_TABLE(of, stfsm_match);
713
714static struct platform_driver stfsm_driver = {
715 .probe = stfsm_probe,
716 .remove = stfsm_remove,
717 .driver = {
718 .name = "st-spi-fsm",
719 .owner = THIS_MODULE,
720 .of_match_table = stfsm_match,
721 },
722};
723module_platform_driver(stfsm_driver);
724
725MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
726MODULE_DESCRIPTION("ST SPI FSM driver");
727MODULE_LICENSE("GPL");