xref: /linux/drivers/mtd/devices/st_spi_fsm.c (revision 0ea5c948cb64bab5bc7a5516774eb8536f05aa0d)
18d36fe1eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2d90db4a0SLee Jones /*
3d90db4a0SLee Jones  * st_spi_fsm.c	- ST Fast Sequence Mode (FSM) Serial Flash Controller
4d90db4a0SLee Jones  *
5d90db4a0SLee Jones  * Author: Angus Clark <angus.clark@st.com>
6d90db4a0SLee Jones  *
7f1919cb8SLee Jones  * Copyright (C) 2010-2014 STMicroelectronics Limited
8d90db4a0SLee Jones  *
9d90db4a0SLee Jones  * JEDEC probe based on drivers/mtd/devices/m25p80.c
10d90db4a0SLee Jones  */
11d90db4a0SLee Jones #include <linux/kernel.h>
12d90db4a0SLee Jones #include <linux/module.h>
13a63984c1SLee Jones #include <linux/regmap.h>
14d90db4a0SLee Jones #include <linux/platform_device.h>
15a63984c1SLee Jones #include <linux/mfd/syscon.h>
16d90db4a0SLee Jones #include <linux/mtd/mtd.h>
17221cff13SLee Jones #include <linux/mtd/partitions.h>
186c8e1b33SBrian Norris #include <linux/mtd/spi-nor.h>
19d90db4a0SLee Jones #include <linux/sched.h>
20d90db4a0SLee Jones #include <linux/delay.h>
21d90db4a0SLee Jones #include <linux/io.h>
22d90db4a0SLee Jones #include <linux/of.h>
2369d5af8dSLee Jones #include <linux/clk.h>
24d90db4a0SLee Jones 
255549fbd5SLee Jones #include "serial_flash_cmds.h"
265549fbd5SLee Jones 
27bc09fb57SLee Jones /*
28bc09fb57SLee Jones  * FSM SPI Controller Registers
29bc09fb57SLee Jones  */
30bc09fb57SLee Jones #define SPI_CLOCKDIV			0x0010
31bc09fb57SLee Jones #define SPI_MODESELECT			0x0018
32bc09fb57SLee Jones #define SPI_CONFIGDATA			0x0020
33bc09fb57SLee Jones #define SPI_STA_MODE_CHANGE		0x0028
34bc09fb57SLee Jones #define SPI_FAST_SEQ_TRANSFER_SIZE	0x0100
35bc09fb57SLee Jones #define SPI_FAST_SEQ_ADD1		0x0104
36bc09fb57SLee Jones #define SPI_FAST_SEQ_ADD2		0x0108
37bc09fb57SLee Jones #define SPI_FAST_SEQ_ADD_CFG		0x010c
38bc09fb57SLee Jones #define SPI_FAST_SEQ_OPC1		0x0110
39bc09fb57SLee Jones #define SPI_FAST_SEQ_OPC2		0x0114
40bc09fb57SLee Jones #define SPI_FAST_SEQ_OPC3		0x0118
41bc09fb57SLee Jones #define SPI_FAST_SEQ_OPC4		0x011c
42bc09fb57SLee Jones #define SPI_FAST_SEQ_OPC5		0x0120
43bc09fb57SLee Jones #define SPI_MODE_BITS			0x0124
44bc09fb57SLee Jones #define SPI_DUMMY_BITS			0x0128
45bc09fb57SLee Jones #define SPI_FAST_SEQ_FLASH_STA_DATA	0x012c
46bc09fb57SLee Jones #define SPI_FAST_SEQ_1			0x0130
47bc09fb57SLee Jones #define SPI_FAST_SEQ_2			0x0134
48bc09fb57SLee Jones #define SPI_FAST_SEQ_3			0x0138
49bc09fb57SLee Jones #define SPI_FAST_SEQ_4			0x013c
50bc09fb57SLee Jones #define SPI_FAST_SEQ_CFG		0x0140
51bc09fb57SLee Jones #define SPI_FAST_SEQ_STA		0x0144
52bc09fb57SLee Jones #define SPI_QUAD_BOOT_SEQ_INIT_1	0x0148
53bc09fb57SLee Jones #define SPI_QUAD_BOOT_SEQ_INIT_2	0x014c
54bc09fb57SLee Jones #define SPI_QUAD_BOOT_READ_SEQ_1	0x0150
55bc09fb57SLee Jones #define SPI_QUAD_BOOT_READ_SEQ_2	0x0154
56bc09fb57SLee Jones #define SPI_PROGRAM_ERASE_TIME		0x0158
57bc09fb57SLee Jones #define SPI_MULT_PAGE_REPEAT_SEQ_1	0x015c
58bc09fb57SLee Jones #define SPI_MULT_PAGE_REPEAT_SEQ_2	0x0160
59bc09fb57SLee Jones #define SPI_STATUS_WR_TIME_REG		0x0164
60bc09fb57SLee Jones #define SPI_FAST_SEQ_DATA_REG		0x0300
61bc09fb57SLee Jones 
62bc09fb57SLee Jones /*
63bc09fb57SLee Jones  * Register: SPI_MODESELECT
64bc09fb57SLee Jones  */
65bc09fb57SLee Jones #define SPI_MODESELECT_CONTIG		0x01
66bc09fb57SLee Jones #define SPI_MODESELECT_FASTREAD		0x02
67bc09fb57SLee Jones #define SPI_MODESELECT_DUALIO		0x04
68bc09fb57SLee Jones #define SPI_MODESELECT_FSM		0x08
69bc09fb57SLee Jones #define SPI_MODESELECT_QUADBOOT		0x10
70bc09fb57SLee Jones 
71bc09fb57SLee Jones /*
72bc09fb57SLee Jones  * Register: SPI_CONFIGDATA
73bc09fb57SLee Jones  */
74bc09fb57SLee Jones #define SPI_CFG_DEVICE_ST		0x1
75bc09fb57SLee Jones #define SPI_CFG_DEVICE_ATMEL		0x4
76bc09fb57SLee Jones #define SPI_CFG_MIN_CS_HIGH(x)		(((x) & 0xfff) << 4)
77bc09fb57SLee Jones #define SPI_CFG_CS_SETUPHOLD(x)		(((x) & 0xff) << 16)
78bc09fb57SLee Jones #define SPI_CFG_DATA_HOLD(x)		(((x) & 0xff) << 24)
79bc09fb57SLee Jones 
8086f309fdSLee Jones #define SPI_CFG_DEFAULT_MIN_CS_HIGH    SPI_CFG_MIN_CS_HIGH(0x0AA)
8186f309fdSLee Jones #define SPI_CFG_DEFAULT_CS_SETUPHOLD   SPI_CFG_CS_SETUPHOLD(0xA0)
8286f309fdSLee Jones #define SPI_CFG_DEFAULT_DATA_HOLD      SPI_CFG_DATA_HOLD(0x00)
8386f309fdSLee Jones 
84bc09fb57SLee Jones /*
85bc09fb57SLee Jones  * Register: SPI_FAST_SEQ_TRANSFER_SIZE
86bc09fb57SLee Jones  */
87bc09fb57SLee Jones #define TRANSFER_SIZE(x)		((x) * 8)
88bc09fb57SLee Jones 
89bc09fb57SLee Jones /*
90bc09fb57SLee Jones  * Register: SPI_FAST_SEQ_ADD_CFG
91bc09fb57SLee Jones  */
92bc09fb57SLee Jones #define ADR_CFG_CYCLES_ADD1(x)		((x) << 0)
93bc09fb57SLee Jones #define ADR_CFG_PADS_1_ADD1		(0x0 << 6)
94bc09fb57SLee Jones #define ADR_CFG_PADS_2_ADD1		(0x1 << 6)
95bc09fb57SLee Jones #define ADR_CFG_PADS_4_ADD1		(0x3 << 6)
96bc09fb57SLee Jones #define ADR_CFG_CSDEASSERT_ADD1		(1   << 8)
97bc09fb57SLee Jones #define ADR_CFG_CYCLES_ADD2(x)		((x) << (0+16))
98bc09fb57SLee Jones #define ADR_CFG_PADS_1_ADD2		(0x0 << (6+16))
99bc09fb57SLee Jones #define ADR_CFG_PADS_2_ADD2		(0x1 << (6+16))
100bc09fb57SLee Jones #define ADR_CFG_PADS_4_ADD2		(0x3 << (6+16))
101bc09fb57SLee Jones #define ADR_CFG_CSDEASSERT_ADD2		(1   << (8+16))
102bc09fb57SLee Jones 
103bc09fb57SLee Jones /*
104bc09fb57SLee Jones  * Register: SPI_FAST_SEQ_n
105bc09fb57SLee Jones  */
106bc09fb57SLee Jones #define SEQ_OPC_OPCODE(x)		((x) << 0)
107bc09fb57SLee Jones #define SEQ_OPC_CYCLES(x)		((x) << 8)
108bc09fb57SLee Jones #define SEQ_OPC_PADS_1			(0x0 << 14)
109bc09fb57SLee Jones #define SEQ_OPC_PADS_2			(0x1 << 14)
110bc09fb57SLee Jones #define SEQ_OPC_PADS_4			(0x3 << 14)
111bc09fb57SLee Jones #define SEQ_OPC_CSDEASSERT		(1   << 16)
112bc09fb57SLee Jones 
113bc09fb57SLee Jones /*
114bc09fb57SLee Jones  * Register: SPI_FAST_SEQ_CFG
115bc09fb57SLee Jones  */
116bc09fb57SLee Jones #define SEQ_CFG_STARTSEQ		(1 << 0)
117bc09fb57SLee Jones #define SEQ_CFG_SWRESET			(1 << 5)
118bc09fb57SLee Jones #define SEQ_CFG_CSDEASSERT		(1 << 6)
119bc09fb57SLee Jones #define SEQ_CFG_READNOTWRITE		(1 << 7)
120bc09fb57SLee Jones #define SEQ_CFG_ERASE			(1 << 8)
121bc09fb57SLee Jones #define SEQ_CFG_PADS_1			(0x0 << 16)
122bc09fb57SLee Jones #define SEQ_CFG_PADS_2			(0x1 << 16)
123bc09fb57SLee Jones #define SEQ_CFG_PADS_4			(0x3 << 16)
124bc09fb57SLee Jones 
125bc09fb57SLee Jones /*
126bc09fb57SLee Jones  * Register: SPI_MODE_BITS
127bc09fb57SLee Jones  */
128bc09fb57SLee Jones #define MODE_DATA(x)			(x & 0xff)
129bc09fb57SLee Jones #define MODE_CYCLES(x)			((x & 0x3f) << 16)
130bc09fb57SLee Jones #define MODE_PADS_1			(0x0 << 22)
131bc09fb57SLee Jones #define MODE_PADS_2			(0x1 << 22)
132bc09fb57SLee Jones #define MODE_PADS_4			(0x3 << 22)
133bc09fb57SLee Jones #define DUMMY_CSDEASSERT		(1   << 24)
134bc09fb57SLee Jones 
135bc09fb57SLee Jones /*
136bc09fb57SLee Jones  * Register: SPI_DUMMY_BITS
137bc09fb57SLee Jones  */
138bc09fb57SLee Jones #define DUMMY_CYCLES(x)			((x & 0x3f) << 16)
139bc09fb57SLee Jones #define DUMMY_PADS_1			(0x0 << 22)
140bc09fb57SLee Jones #define DUMMY_PADS_2			(0x1 << 22)
141bc09fb57SLee Jones #define DUMMY_PADS_4			(0x3 << 22)
142bc09fb57SLee Jones #define DUMMY_CSDEASSERT		(1   << 24)
143bc09fb57SLee Jones 
144bc09fb57SLee Jones /*
145bc09fb57SLee Jones  * Register: SPI_FAST_SEQ_FLASH_STA_DATA
146bc09fb57SLee Jones  */
147bc09fb57SLee Jones #define STA_DATA_BYTE1(x)		((x & 0xff) << 0)
148bc09fb57SLee Jones #define STA_DATA_BYTE2(x)		((x & 0xff) << 8)
149bc09fb57SLee Jones #define STA_PADS_1			(0x0 << 16)
150bc09fb57SLee Jones #define STA_PADS_2			(0x1 << 16)
151bc09fb57SLee Jones #define STA_PADS_4			(0x3 << 16)
152bc09fb57SLee Jones #define STA_CSDEASSERT			(0x1 << 20)
153bc09fb57SLee Jones #define STA_RDNOTWR			(0x1 << 21)
154bc09fb57SLee Jones 
155bc09fb57SLee Jones /*
156bc09fb57SLee Jones  * FSM SPI Instruction Opcodes
157bc09fb57SLee Jones  */
158bc09fb57SLee Jones #define STFSM_OPC_CMD			0x1
159bc09fb57SLee Jones #define STFSM_OPC_ADD			0x2
160bc09fb57SLee Jones #define STFSM_OPC_STA			0x3
161bc09fb57SLee Jones #define STFSM_OPC_MODE			0x4
162bc09fb57SLee Jones #define STFSM_OPC_DUMMY		0x5
163bc09fb57SLee Jones #define STFSM_OPC_DATA			0x6
164bc09fb57SLee Jones #define STFSM_OPC_WAIT			0x7
165bc09fb57SLee Jones #define STFSM_OPC_JUMP			0x8
166bc09fb57SLee Jones #define STFSM_OPC_GOTO			0x9
167bc09fb57SLee Jones #define STFSM_OPC_STOP			0xF
168bc09fb57SLee Jones 
169bc09fb57SLee Jones /*
170bc09fb57SLee Jones  * FSM SPI Instructions (== opcode + operand).
171bc09fb57SLee Jones  */
172bc09fb57SLee Jones #define STFSM_INSTR(cmd, op)		((cmd) | ((op) << 4))
173bc09fb57SLee Jones 
174bc09fb57SLee Jones #define STFSM_INST_CMD1			STFSM_INSTR(STFSM_OPC_CMD,	1)
175bc09fb57SLee Jones #define STFSM_INST_CMD2			STFSM_INSTR(STFSM_OPC_CMD,	2)
176bc09fb57SLee Jones #define STFSM_INST_CMD3			STFSM_INSTR(STFSM_OPC_CMD,	3)
177bc09fb57SLee Jones #define STFSM_INST_CMD4			STFSM_INSTR(STFSM_OPC_CMD,	4)
178bc09fb57SLee Jones #define STFSM_INST_CMD5			STFSM_INSTR(STFSM_OPC_CMD,	5)
179bc09fb57SLee Jones #define STFSM_INST_ADD1			STFSM_INSTR(STFSM_OPC_ADD,	1)
180bc09fb57SLee Jones #define STFSM_INST_ADD2			STFSM_INSTR(STFSM_OPC_ADD,	2)
181bc09fb57SLee Jones 
182bc09fb57SLee Jones #define STFSM_INST_DATA_WRITE		STFSM_INSTR(STFSM_OPC_DATA,	1)
183bc09fb57SLee Jones #define STFSM_INST_DATA_READ		STFSM_INSTR(STFSM_OPC_DATA,	2)
184bc09fb57SLee Jones 
185bc09fb57SLee Jones #define STFSM_INST_STA_RD1		STFSM_INSTR(STFSM_OPC_STA,	0x1)
186bc09fb57SLee Jones #define STFSM_INST_STA_WR1		STFSM_INSTR(STFSM_OPC_STA,	0x1)
187bc09fb57SLee Jones #define STFSM_INST_STA_RD2		STFSM_INSTR(STFSM_OPC_STA,	0x2)
188bc09fb57SLee Jones #define STFSM_INST_STA_WR1_2		STFSM_INSTR(STFSM_OPC_STA,	0x3)
189bc09fb57SLee Jones 
190bc09fb57SLee Jones #define STFSM_INST_MODE			STFSM_INSTR(STFSM_OPC_MODE,	0)
191bc09fb57SLee Jones #define STFSM_INST_DUMMY		STFSM_INSTR(STFSM_OPC_DUMMY,	0)
192bc09fb57SLee Jones #define STFSM_INST_WAIT			STFSM_INSTR(STFSM_OPC_WAIT,	0)
193bc09fb57SLee Jones #define STFSM_INST_STOP			STFSM_INSTR(STFSM_OPC_STOP,	0)
194bc09fb57SLee Jones 
19586f309fdSLee Jones #define STFSM_DEFAULT_EMI_FREQ 100000000UL                        /* 100 MHz */
19686f309fdSLee Jones #define STFSM_DEFAULT_WR_TIME  (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
19786f309fdSLee Jones 
19886f309fdSLee Jones #define STFSM_FLASH_SAFE_FREQ  10000000UL                         /* 10 MHz */
19986f309fdSLee Jones 
2003c8b85b3SLee Jones #define STFSM_MAX_WAIT_SEQ_MS  1000     /* FSM execution time */
2013c8b85b3SLee Jones 
2025343a123SLee Jones /* S25FLxxxS commands */
2035343a123SLee Jones #define S25FL_CMD_WRITE4_1_1_4 0x34
2045343a123SLee Jones #define S25FL_CMD_SE4          0xdc
2055343a123SLee Jones #define S25FL_CMD_CLSR         0x30
2065343a123SLee Jones #define S25FL_CMD_DYBWR                0xe1
2075343a123SLee Jones #define S25FL_CMD_DYBRD                0xe0
2085343a123SLee Jones #define S25FL_CMD_WRITE4       0x12    /* Note, opcode clashes with
20992d3af9aSBrian Norris 					* 'SPINOR_OP_WRITE_1_4_4'
2105343a123SLee Jones 					* as found on N25Qxxx devices! */
2115343a123SLee Jones 
212176b4377SLee Jones /* Status register */
213176b4377SLee Jones #define FLASH_STATUS_BUSY      0x01
214176b4377SLee Jones #define FLASH_STATUS_WEL       0x02
215176b4377SLee Jones #define FLASH_STATUS_BP0       0x04
216176b4377SLee Jones #define FLASH_STATUS_BP1       0x08
217176b4377SLee Jones #define FLASH_STATUS_BP2       0x10
218176b4377SLee Jones #define FLASH_STATUS_SRWP0     0x80
219176b4377SLee Jones #define FLASH_STATUS_TIMEOUT   0xff
2205343a123SLee Jones /* S25FL Error Flags */
2215343a123SLee Jones #define S25FL_STATUS_E_ERR     0x20
2225343a123SLee Jones #define S25FL_STATUS_P_ERR     0x40
223176b4377SLee Jones 
2245d0bddabSAngus Clark #define N25Q_CMD_WRVCR         0x81
2255d0bddabSAngus Clark #define N25Q_CMD_RDVCR         0x85
2265d0bddabSAngus Clark #define N25Q_CMD_RDVECR        0x65
2275d0bddabSAngus Clark #define N25Q_CMD_RDNVCR        0xb5
2285d0bddabSAngus Clark #define N25Q_CMD_WRNVCR        0xb1
2295d0bddabSAngus Clark 
230e514f105SLee Jones #define FLASH_PAGESIZE         256			/* In Bytes    */
231e514f105SLee Jones #define FLASH_PAGESIZE_32      (FLASH_PAGESIZE / 4)	/* In uint32_t */
232176b4377SLee Jones #define FLASH_MAX_BUSY_WAIT    (300 * HZ)	/* Maximum 'CHIPERASE' time */
233e514f105SLee Jones 
234e85a6196SLee Jones /*
235e85a6196SLee Jones  * Flags to tweak operation of default read/write/erase routines
236e85a6196SLee Jones  */
237e85a6196SLee Jones #define CFG_READ_TOGGLE_32BIT_ADDR     0x00000001
238e85a6196SLee Jones #define CFG_WRITE_TOGGLE_32BIT_ADDR    0x00000002
239e85a6196SLee Jones #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
240e85a6196SLee Jones #define CFG_S25FL_CHECK_ERROR_FLAGS    0x00000010
241e85a6196SLee Jones 
242e6b1bb4eSLee Jones struct stfsm_seq {
243e6b1bb4eSLee Jones 	uint32_t data_size;
244e6b1bb4eSLee Jones 	uint32_t addr1;
245e6b1bb4eSLee Jones 	uint32_t addr2;
246e6b1bb4eSLee Jones 	uint32_t addr_cfg;
247e6b1bb4eSLee Jones 	uint32_t seq_opc[5];
248e6b1bb4eSLee Jones 	uint32_t mode;
249e6b1bb4eSLee Jones 	uint32_t dummy;
250e6b1bb4eSLee Jones 	uint32_t status;
251e6b1bb4eSLee Jones 	uint8_t  seq[16];
252e6b1bb4eSLee Jones 	uint32_t seq_cfg;
253e6b1bb4eSLee Jones } __packed __aligned(4);
254e6b1bb4eSLee Jones 
255d90db4a0SLee Jones struct stfsm {
256d90db4a0SLee Jones 	struct device		*dev;
257d90db4a0SLee Jones 	void __iomem		*base;
258d90db4a0SLee Jones 	struct mtd_info		mtd;
259d90db4a0SLee Jones 	struct mutex		lock;
26024fec651SLee Jones 	struct flash_info       *info;
26169d5af8dSLee Jones 	struct clk              *clk;
26286f309fdSLee Jones 
263e85a6196SLee Jones 	uint32_t                configuration;
26486f309fdSLee Jones 	uint32_t                fifo_dir_delay;
265a63984c1SLee Jones 	bool                    booted_from_spi;
2660ea7d706SLee Jones 	bool                    reset_signal;
2670ea7d706SLee Jones 	bool                    reset_por;
268d90db4a0SLee Jones 
269e6b1bb4eSLee Jones 	struct stfsm_seq stfsm_seq_read;
270e6b1bb4eSLee Jones 	struct stfsm_seq stfsm_seq_write;
271e6b1bb4eSLee Jones 	struct stfsm_seq stfsm_seq_en_32bit_addr;
272e6b1bb4eSLee Jones };
2733c8b85b3SLee Jones 
27408981274SLee Jones /* Parameters to configure a READ or WRITE FSM sequence */
27508981274SLee Jones struct seq_rw_config {
27608981274SLee Jones 	uint32_t        flags;          /* flags to support config */
27708981274SLee Jones 	uint8_t         cmd;            /* FLASH command */
27808981274SLee Jones 	int             write;          /* Write Sequence */
27908981274SLee Jones 	uint8_t         addr_pads;      /* No. of addr pads (MODE & DUMMY) */
28008981274SLee Jones 	uint8_t         data_pads;      /* No. of data pads */
28108981274SLee Jones 	uint8_t         mode_data;      /* MODE data */
28208981274SLee Jones 	uint8_t         mode_cycles;    /* No. of MODE cycles */
28308981274SLee Jones 	uint8_t         dummy_cycles;   /* No. of DUMMY cycles */
28408981274SLee Jones };
28508981274SLee Jones 
28611d7f826SLee Jones /* SPI Flash Device Table */
28711d7f826SLee Jones struct flash_info {
28811d7f826SLee Jones 	char            *name;
28911d7f826SLee Jones 	/*
29011d7f826SLee Jones 	 * JEDEC id zero means "no ID" (most older chips); otherwise it has
29111d7f826SLee Jones 	 * a high byte of zero plus three data bytes: the manufacturer id,
29211d7f826SLee Jones 	 * then a two byte device id.
29311d7f826SLee Jones 	 */
29411d7f826SLee Jones 	u32             jedec_id;
29511d7f826SLee Jones 	u16             ext_id;
29611d7f826SLee Jones 	/*
29792d3af9aSBrian Norris 	 * The size listed here is what works with SPINOR_OP_SE, which isn't
29811d7f826SLee Jones 	 * necessarily called a "sector" by the vendor.
29911d7f826SLee Jones 	 */
30011d7f826SLee Jones 	unsigned        sector_size;
30111d7f826SLee Jones 	u16             n_sectors;
30211d7f826SLee Jones 	u32             flags;
30311d7f826SLee Jones 	/*
30411d7f826SLee Jones 	 * Note, where FAST_READ is supported, freq_max specifies the
30511d7f826SLee Jones 	 * FAST_READ frequency, not the READ frequency.
30611d7f826SLee Jones 	 */
30711d7f826SLee Jones 	u32             max_freq;
30811d7f826SLee Jones 	int             (*config)(struct stfsm *);
30911d7f826SLee Jones };
31011d7f826SLee Jones 
311218b870fSLee Jones static int stfsm_n25q_config(struct stfsm *fsm);
31289818066SLee Jones static int stfsm_mx25_config(struct stfsm *fsm);
3135343a123SLee Jones static int stfsm_s25fl_config(struct stfsm *fsm);
314cd7cac9eSLee Jones static int stfsm_w25q_config(struct stfsm *fsm);
315218b870fSLee Jones 
31611d7f826SLee Jones static struct flash_info flash_types[] = {
31711d7f826SLee Jones 	/*
31811d7f826SLee Jones 	 * ST Microelectronics/Numonyx --
31911d7f826SLee Jones 	 * (newer production versions may have feature updates
32011d7f826SLee Jones 	 * (eg faster operating frequency)
32111d7f826SLee Jones 	 */
32211d7f826SLee Jones #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
32311d7f826SLee Jones 	{ "m25p40",  0x202013, 0,  64 * 1024,   8, M25P_FLAG, 25, NULL },
32411d7f826SLee Jones 	{ "m25p80",  0x202014, 0,  64 * 1024,  16, M25P_FLAG, 25, NULL },
32511d7f826SLee Jones 	{ "m25p16",  0x202015, 0,  64 * 1024,  32, M25P_FLAG, 25, NULL },
32611d7f826SLee Jones 	{ "m25p32",  0x202016, 0,  64 * 1024,  64, M25P_FLAG, 50, NULL },
32711d7f826SLee Jones 	{ "m25p64",  0x202017, 0,  64 * 1024, 128, M25P_FLAG, 50, NULL },
32811d7f826SLee Jones 	{ "m25p128", 0x202018, 0, 256 * 1024,  64, M25P_FLAG, 50, NULL },
32911d7f826SLee Jones 
33011d7f826SLee Jones #define M25PX_FLAG (FLASH_FLAG_READ_WRITE      |	\
33111d7f826SLee Jones 		    FLASH_FLAG_READ_FAST        |	\
33211d7f826SLee Jones 		    FLASH_FLAG_READ_1_1_2       |	\
33311d7f826SLee Jones 		    FLASH_FLAG_WRITE_1_1_2)
33411d7f826SLee Jones 	{ "m25px32", 0x207116, 0,  64 * 1024,  64, M25PX_FLAG, 75, NULL },
33511d7f826SLee Jones 	{ "m25px64", 0x207117, 0,  64 * 1024, 128, M25PX_FLAG, 75, NULL },
33611d7f826SLee Jones 
3376b6d3737SAngus Clark 	/* Macronix MX25xxx
3386b6d3737SAngus Clark 	 *     - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
3396b6d3737SAngus Clark 	 *       where operating frequency must be reduced.
3406b6d3737SAngus Clark 	 */
34111d7f826SLee Jones #define MX25_FLAG (FLASH_FLAG_READ_WRITE       |	\
34211d7f826SLee Jones 		   FLASH_FLAG_READ_FAST         |	\
34311d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_2        |	\
34411d7f826SLee Jones 		   FLASH_FLAG_READ_1_2_2        |	\
34511d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_4        |	\
34611d7f826SLee Jones 		   FLASH_FLAG_SE_4K             |	\
34711d7f826SLee Jones 		   FLASH_FLAG_SE_32K)
3486b6d3737SAngus Clark 	{ "mx25l3255e",  0xc29e16, 0, 64 * 1024, 64,
3496b6d3737SAngus Clark 	  (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
3506b6d3737SAngus Clark 	  stfsm_mx25_config},
35111d7f826SLee Jones 	{ "mx25l25635e", 0xc22019, 0, 64*1024, 512,
35289818066SLee Jones 	  (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
35389818066SLee Jones 	  stfsm_mx25_config },
3545fa98069SAngus Clark 	{ "mx25l25655e", 0xc22619, 0, 64*1024, 512,
3555fa98069SAngus Clark 	  (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
3565fa98069SAngus Clark 	  stfsm_mx25_config},
35711d7f826SLee Jones 
35811d7f826SLee Jones #define N25Q_FLAG (FLASH_FLAG_READ_WRITE       |	\
35911d7f826SLee Jones 		   FLASH_FLAG_READ_FAST         |	\
36011d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_2        |	\
36111d7f826SLee Jones 		   FLASH_FLAG_READ_1_2_2        |	\
36211d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_4        |	\
36311d7f826SLee Jones 		   FLASH_FLAG_READ_1_4_4        |	\
36411d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_1_2       |	\
36511d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_2_2       |	\
36611d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_1_4       |	\
36711d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_4_4)
368218b870fSLee Jones 	{ "n25q128", 0x20ba18, 0, 64 * 1024,  256, N25Q_FLAG, 108,
369218b870fSLee Jones 	  stfsm_n25q_config },
37011d7f826SLee Jones 	{ "n25q256", 0x20ba19, 0, 64 * 1024,  512,
371218b870fSLee Jones 	  N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
37211d7f826SLee Jones 
37311d7f826SLee Jones 	/*
37411d7f826SLee Jones 	 * Spansion S25FLxxxP
37511d7f826SLee Jones 	 *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
37611d7f826SLee Jones 	 */
37711d7f826SLee Jones #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE  |	\
37811d7f826SLee Jones 			FLASH_FLAG_READ_1_1_2   |	\
37911d7f826SLee Jones 			FLASH_FLAG_READ_1_2_2   |	\
38011d7f826SLee Jones 			FLASH_FLAG_READ_1_1_4   |	\
38111d7f826SLee Jones 			FLASH_FLAG_READ_1_4_4   |	\
38211d7f826SLee Jones 			FLASH_FLAG_WRITE_1_1_4  |	\
38311d7f826SLee Jones 			FLASH_FLAG_READ_FAST)
38485bdcf6bSAngus Clark 	{ "s25fl032p",  0x010215, 0x4d00,  64 * 1024,  64, S25FLXXXP_FLAG, 80,
38585bdcf6bSAngus Clark 	  stfsm_s25fl_config},
38611d7f826SLee Jones 	{ "s25fl129p0", 0x012018, 0x4d00, 256 * 1024,  64, S25FLXXXP_FLAG, 80,
3875343a123SLee Jones 	  stfsm_s25fl_config },
38811d7f826SLee Jones 	{ "s25fl129p1", 0x012018, 0x4d01,  64 * 1024, 256, S25FLXXXP_FLAG, 80,
3895343a123SLee Jones 	  stfsm_s25fl_config },
39011d7f826SLee Jones 
39111d7f826SLee Jones 	/*
39211d7f826SLee Jones 	 * Spansion S25FLxxxS
39311d7f826SLee Jones 	 *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
39411d7f826SLee Jones 	 *     - RESET# signal supported by die but not bristled out on all
39511d7f826SLee Jones 	 *       package types.  The package type is a function of board design,
39611d7f826SLee Jones 	 *       so this information is captured in the board's flags.
39711d7f826SLee Jones 	 *     - Supports 'DYB' sector protection. Depending on variant, sectors
39811d7f826SLee Jones 	 *       may default to locked state on power-on.
39911d7f826SLee Jones 	 */
40011d7f826SLee Jones #define S25FLXXXS_FLAG (S25FLXXXP_FLAG         |	\
40111d7f826SLee Jones 			FLASH_FLAG_RESET        |	\
40211d7f826SLee Jones 			FLASH_FLAG_DYB_LOCKING)
40311d7f826SLee Jones 	{ "s25fl128s0", 0x012018, 0x0300,  256 * 1024, 64, S25FLXXXS_FLAG, 80,
4045343a123SLee Jones 	  stfsm_s25fl_config },
40511d7f826SLee Jones 	{ "s25fl128s1", 0x012018, 0x0301,  64 * 1024, 256, S25FLXXXS_FLAG, 80,
4065343a123SLee Jones 	  stfsm_s25fl_config },
40711d7f826SLee Jones 	{ "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
4085343a123SLee Jones 	  S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
40911d7f826SLee Jones 	{ "s25fl256s1", 0x010219, 0x4d01,  64 * 1024, 512,
4105343a123SLee Jones 	  S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
41111d7f826SLee Jones 
41211d7f826SLee Jones 	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
41311d7f826SLee Jones #define W25X_FLAG (FLASH_FLAG_READ_WRITE       |	\
41411d7f826SLee Jones 		   FLASH_FLAG_READ_FAST         |	\
41511d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_2        |	\
41611d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_1_2)
41711d7f826SLee Jones 	{ "w25x40",  0xef3013, 0,  64 * 1024,   8, W25X_FLAG, 75, NULL },
41811d7f826SLee Jones 	{ "w25x80",  0xef3014, 0,  64 * 1024,  16, W25X_FLAG, 75, NULL },
41911d7f826SLee Jones 	{ "w25x16",  0xef3015, 0,  64 * 1024,  32, W25X_FLAG, 75, NULL },
42011d7f826SLee Jones 	{ "w25x32",  0xef3016, 0,  64 * 1024,  64, W25X_FLAG, 75, NULL },
42111d7f826SLee Jones 	{ "w25x64",  0xef3017, 0,  64 * 1024, 128, W25X_FLAG, 75, NULL },
42211d7f826SLee Jones 
42311d7f826SLee Jones 	/* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
42411d7f826SLee Jones #define W25Q_FLAG (FLASH_FLAG_READ_WRITE       |	\
42511d7f826SLee Jones 		   FLASH_FLAG_READ_FAST         |	\
42611d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_2        |	\
42711d7f826SLee Jones 		   FLASH_FLAG_READ_1_2_2        |	\
42811d7f826SLee Jones 		   FLASH_FLAG_READ_1_1_4        |	\
42911d7f826SLee Jones 		   FLASH_FLAG_READ_1_4_4        |	\
43011d7f826SLee Jones 		   FLASH_FLAG_WRITE_1_1_4)
431cd7cac9eSLee Jones 	{ "w25q80",  0xef4014, 0,  64 * 1024,  16, W25Q_FLAG, 80,
432cd7cac9eSLee Jones 	  stfsm_w25q_config },
433cd7cac9eSLee Jones 	{ "w25q16",  0xef4015, 0,  64 * 1024,  32, W25Q_FLAG, 80,
434cd7cac9eSLee Jones 	  stfsm_w25q_config },
435cd7cac9eSLee Jones 	{ "w25q32",  0xef4016, 0,  64 * 1024,  64, W25Q_FLAG, 80,
436cd7cac9eSLee Jones 	  stfsm_w25q_config },
437cd7cac9eSLee Jones 	{ "w25q64",  0xef4017, 0,  64 * 1024, 128, W25Q_FLAG, 80,
438cd7cac9eSLee Jones 	  stfsm_w25q_config },
43911d7f826SLee Jones 
44011d7f826SLee Jones 	/* Sentinel */
44111d7f826SLee Jones 	{ NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
44211d7f826SLee Jones };
44311d7f826SLee Jones 
444a37b2f5aSLee Jones /*
445a37b2f5aSLee Jones  * FSM message sequence configurations:
446a37b2f5aSLee Jones  *
447a37b2f5aSLee Jones  * All configs are presented in order of preference
448a37b2f5aSLee Jones  */
449a37b2f5aSLee Jones 
450a37b2f5aSLee Jones /* Default READ configurations, in order of preference */
451a37b2f5aSLee Jones static struct seq_rw_config default_read_configs[] = {
45292d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4,	0, 4, 4, 0x00, 2, 4},
45392d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4,	0, 1, 4, 0x00, 4, 0},
45492d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2,	0, 2, 2, 0x00, 4, 0},
45592d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2,	0, 1, 2, 0x00, 0, 8},
45692d3af9aSBrian Norris 	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ_FAST,	0, 1, 1, 0x00, 0, 8},
45792d3af9aSBrian Norris 	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ,		0, 1, 1, 0x00, 0, 0},
458a37b2f5aSLee Jones 	{0x00,			0,			0, 0, 0, 0x00, 0, 0},
459a37b2f5aSLee Jones };
460a37b2f5aSLee Jones 
461a37b2f5aSLee Jones /* Default WRITE configurations */
462a37b2f5aSLee Jones static struct seq_rw_config default_write_configs[] = {
46392d3af9aSBrian Norris 	{FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
46492d3af9aSBrian Norris 	{FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
46592d3af9aSBrian Norris 	{FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
46692d3af9aSBrian Norris 	{FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
46792d3af9aSBrian Norris 	{FLASH_FLAG_READ_WRITE,  SPINOR_OP_WRITE,       1, 1, 1, 0x00, 0, 0},
468a37b2f5aSLee Jones 	{0x00,			 0,			0, 0, 0, 0x00, 0, 0},
469a37b2f5aSLee Jones };
470a37b2f5aSLee Jones 
471e85a6196SLee Jones /*
472e85a6196SLee Jones  * [N25Qxxx] Configuration
473e85a6196SLee Jones  */
474e85a6196SLee Jones #define N25Q_VCR_DUMMY_CYCLES(x)	(((x) & 0xf) << 4)
475e85a6196SLee Jones #define N25Q_VCR_XIP_DISABLED		((uint8_t)0x1 << 3)
476e85a6196SLee Jones #define N25Q_VCR_WRAP_CONT		0x3
477e85a6196SLee Jones 
478e85a6196SLee Jones /* N25Q 3-byte Address READ configurations
479e85a6196SLee Jones  *	- 'FAST' variants configured for 8 dummy cycles.
480e85a6196SLee Jones  *
481e85a6196SLee Jones  * Note, the number of dummy cycles used for 'FAST' READ operations is
482e85a6196SLee Jones  * configurable and would normally be tuned according to the READ command and
483e85a6196SLee Jones  * operating frequency.  However, this applies universally to all 'FAST' READ
484e85a6196SLee Jones  * commands, including those used by the SPIBoot controller, and remains in
485e85a6196SLee Jones  * force until the device is power-cycled.  Since the SPIBoot controller is
486e85a6196SLee Jones  * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
487e85a6196SLee Jones  * cycles.
488e85a6196SLee Jones  */
489e85a6196SLee Jones static struct seq_rw_config n25q_read3_configs[] = {
49092d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4,	0, 4, 4, 0x00, 0, 8},
49192d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4,	0, 1, 4, 0x00, 0, 8},
49292d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2,	0, 2, 2, 0x00, 0, 8},
49392d3af9aSBrian Norris 	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2,	0, 1, 2, 0x00, 0, 8},
49492d3af9aSBrian Norris 	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ_FAST,	0, 1, 1, 0x00, 0, 8},
49592d3af9aSBrian Norris 	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ,	        0, 1, 1, 0x00, 0, 0},
496e85a6196SLee Jones 	{0x00,			0,			0, 0, 0, 0x00, 0, 0},
497e85a6196SLee Jones };
498e85a6196SLee Jones 
499e85a6196SLee Jones /* N25Q 4-byte Address READ configurations
500e85a6196SLee Jones  *	- use special 4-byte address READ commands (reduces overheads, and
501e85a6196SLee Jones  *        reduces risk of hitting watchdog reset issues).
502e85a6196SLee Jones  *	- 'FAST' variants configured for 8 dummy cycles (see note above.)
503e85a6196SLee Jones  */
504e85a6196SLee Jones static struct seq_rw_config n25q_read4_configs[] = {
505902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4_4B, 0, 4, 4, 0x00, 0, 8},
506902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4_4B, 0, 1, 4, 0x00, 0, 8},
507902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2_4B, 0, 2, 2, 0x00, 0, 8},
508902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2_4B, 0, 1, 2, 0x00, 0, 8},
509902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ_FAST_4B,  0, 1, 1, 0x00, 0, 8},
510902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ_4B,       0, 1, 1, 0x00, 0, 0},
511e85a6196SLee Jones 	{0x00,			0,                       0, 0, 0, 0x00, 0, 0},
512e85a6196SLee Jones };
513e85a6196SLee Jones 
51489818066SLee Jones /*
51589818066SLee Jones  * [MX25xxx] Configuration
51689818066SLee Jones  */
51789818066SLee Jones #define MX25_STATUS_QE			(0x1 << 6)
51889818066SLee Jones 
stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq * seq)51989818066SLee Jones static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
52089818066SLee Jones {
52189818066SLee Jones 	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
52289818066SLee Jones 			   SEQ_OPC_CYCLES(8) |
5236c8e1b33SBrian Norris 			   SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
52489818066SLee Jones 			   SEQ_OPC_CSDEASSERT);
52589818066SLee Jones 
52689818066SLee Jones 	seq->seq[0] = STFSM_INST_CMD1;
52789818066SLee Jones 	seq->seq[1] = STFSM_INST_WAIT;
52889818066SLee Jones 	seq->seq[2] = STFSM_INST_STOP;
52989818066SLee Jones 
53089818066SLee Jones 	seq->seq_cfg = (SEQ_CFG_PADS_1 |
53189818066SLee Jones 			SEQ_CFG_ERASE |
53289818066SLee Jones 			SEQ_CFG_READNOTWRITE |
53389818066SLee Jones 			SEQ_CFG_CSDEASSERT |
53489818066SLee Jones 			SEQ_CFG_STARTSEQ);
53589818066SLee Jones 
53689818066SLee Jones 	return 0;
53789818066SLee Jones }
53889818066SLee Jones 
5395343a123SLee Jones /*
5405343a123SLee Jones  * [S25FLxxx] Configuration
5415343a123SLee Jones  */
5425343a123SLee Jones #define STFSM_S25FL_CONFIG_QE		(0x1 << 1)
5435343a123SLee Jones 
5445343a123SLee Jones /*
5455343a123SLee Jones  * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
5465343a123SLee Jones  * Register, Extended Address Modes, and a 32-bit address command set.  The
5475343a123SLee Jones  * 32-bit address command set is used here, since it avoids any problems with
5485343a123SLee Jones  * entering a state that is incompatible with the SPIBoot Controller.
5495343a123SLee Jones  */
5505343a123SLee Jones static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
551902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_4_4,  SPINOR_OP_READ_1_4_4_4B,  0, 4, 4, 0x00, 2, 4},
552902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_1_4,  SPINOR_OP_READ_1_1_4_4B,  0, 1, 4, 0x00, 0, 8},
553902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_2_2,  SPINOR_OP_READ_1_2_2_4B,  0, 2, 2, 0x00, 4, 0},
554902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_1_1_2,  SPINOR_OP_READ_1_1_2_4B,  0, 1, 2, 0x00, 0, 8},
555902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_FAST,   SPINOR_OP_READ_FAST_4B,   0, 1, 1, 0x00, 0, 8},
556902cc69aSCyrille Pitchen 	{FLASH_FLAG_READ_WRITE,  SPINOR_OP_READ_4B,        0, 1, 1, 0x00, 0, 0},
5575343a123SLee Jones 	{0x00,                   0,                        0, 0, 0, 0x00, 0, 0},
5585343a123SLee Jones };
5595343a123SLee Jones 
5605343a123SLee Jones static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
5615343a123SLee Jones 	{FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
5625343a123SLee Jones 	{FLASH_FLAG_READ_WRITE,  S25FL_CMD_WRITE4,       1, 1, 1, 0x00, 0, 0},
5635343a123SLee Jones 	{0x00,                   0,                      0, 0, 0, 0x00, 0, 0},
5645343a123SLee Jones };
5655343a123SLee Jones 
566cd7cac9eSLee Jones /*
567cd7cac9eSLee Jones  * [W25Qxxx] Configuration
568cd7cac9eSLee Jones  */
5695d0bddabSAngus Clark #define W25Q_STATUS_QE			(0x1 << 1)
570cd7cac9eSLee Jones 
5711bd512b5SLee Jones static struct stfsm_seq stfsm_seq_read_jedec = {
5721bd512b5SLee Jones 	.data_size = TRANSFER_SIZE(8),
5731bd512b5SLee Jones 	.seq_opc[0] = (SEQ_OPC_PADS_1 |
5741bd512b5SLee Jones 		       SEQ_OPC_CYCLES(8) |
57592d3af9aSBrian Norris 		       SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
5761bd512b5SLee Jones 	.seq = {
5771bd512b5SLee Jones 		STFSM_INST_CMD1,
5781bd512b5SLee Jones 		STFSM_INST_DATA_READ,
5791bd512b5SLee Jones 		STFSM_INST_STOP,
5801bd512b5SLee Jones 	},
5811bd512b5SLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
5821bd512b5SLee Jones 		    SEQ_CFG_READNOTWRITE |
5831bd512b5SLee Jones 		    SEQ_CFG_CSDEASSERT |
5841bd512b5SLee Jones 		    SEQ_CFG_STARTSEQ),
5851bd512b5SLee Jones };
5861bd512b5SLee Jones 
587176b4377SLee Jones static struct stfsm_seq stfsm_seq_read_status_fifo = {
588176b4377SLee Jones 	.data_size = TRANSFER_SIZE(4),
589176b4377SLee Jones 	.seq_opc[0] = (SEQ_OPC_PADS_1 |
590176b4377SLee Jones 		       SEQ_OPC_CYCLES(8) |
59192d3af9aSBrian Norris 		       SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
592176b4377SLee Jones 	.seq = {
593176b4377SLee Jones 		STFSM_INST_CMD1,
594176b4377SLee Jones 		STFSM_INST_DATA_READ,
595176b4377SLee Jones 		STFSM_INST_STOP,
596176b4377SLee Jones 	},
597176b4377SLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
598176b4377SLee Jones 		    SEQ_CFG_READNOTWRITE |
599176b4377SLee Jones 		    SEQ_CFG_CSDEASSERT |
600176b4377SLee Jones 		    SEQ_CFG_STARTSEQ),
601176b4377SLee Jones };
602176b4377SLee Jones 
603fa5ba3afSLee Jones static struct stfsm_seq stfsm_seq_erase_sector = {
604fa5ba3afSLee Jones 	/* 'addr_cfg' configured during initialisation */
605fa5ba3afSLee Jones 	.seq_opc = {
606fa5ba3afSLee Jones 		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
60792d3af9aSBrian Norris 		 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
608fa5ba3afSLee Jones 
609fa5ba3afSLee Jones 		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
61092d3af9aSBrian Norris 		 SEQ_OPC_OPCODE(SPINOR_OP_SE)),
611fa5ba3afSLee Jones 	},
612fa5ba3afSLee Jones 	.seq = {
613fa5ba3afSLee Jones 		STFSM_INST_CMD1,
614fa5ba3afSLee Jones 		STFSM_INST_CMD2,
615fa5ba3afSLee Jones 		STFSM_INST_ADD1,
616fa5ba3afSLee Jones 		STFSM_INST_ADD2,
617fa5ba3afSLee Jones 		STFSM_INST_STOP,
618fa5ba3afSLee Jones 	},
619fa5ba3afSLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
620fa5ba3afSLee Jones 		    SEQ_CFG_READNOTWRITE |
621fa5ba3afSLee Jones 		    SEQ_CFG_CSDEASSERT |
622fa5ba3afSLee Jones 		    SEQ_CFG_STARTSEQ),
623fa5ba3afSLee Jones };
624fa5ba3afSLee Jones 
6254a341fe7SLee Jones static struct stfsm_seq stfsm_seq_erase_chip = {
6264a341fe7SLee Jones 	.seq_opc = {
6274a341fe7SLee Jones 		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
62892d3af9aSBrian Norris 		 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
6294a341fe7SLee Jones 
6304a341fe7SLee Jones 		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
6316c8e1b33SBrian Norris 		 SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
6324a341fe7SLee Jones 	},
6334a341fe7SLee Jones 	.seq = {
6344a341fe7SLee Jones 		STFSM_INST_CMD1,
6354a341fe7SLee Jones 		STFSM_INST_CMD2,
6364a341fe7SLee Jones 		STFSM_INST_WAIT,
6374a341fe7SLee Jones 		STFSM_INST_STOP,
6384a341fe7SLee Jones 	},
6394a341fe7SLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
6404a341fe7SLee Jones 		    SEQ_CFG_ERASE |
6414a341fe7SLee Jones 		    SEQ_CFG_READNOTWRITE |
6424a341fe7SLee Jones 		    SEQ_CFG_CSDEASSERT |
6434a341fe7SLee Jones 		    SEQ_CFG_STARTSEQ),
6444a341fe7SLee Jones };
6454a341fe7SLee Jones 
646150571b7SLee Jones static struct stfsm_seq stfsm_seq_write_status = {
647150571b7SLee Jones 	.seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
64892d3af9aSBrian Norris 		       SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
649150571b7SLee Jones 	.seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
65092d3af9aSBrian Norris 		       SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
651150571b7SLee Jones 	.seq = {
652150571b7SLee Jones 		STFSM_INST_CMD1,
653150571b7SLee Jones 		STFSM_INST_CMD2,
654150571b7SLee Jones 		STFSM_INST_STA_WR1,
655150571b7SLee Jones 		STFSM_INST_STOP,
656150571b7SLee Jones 	},
657150571b7SLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
658150571b7SLee Jones 		    SEQ_CFG_READNOTWRITE |
659150571b7SLee Jones 		    SEQ_CFG_CSDEASSERT |
660150571b7SLee Jones 		    SEQ_CFG_STARTSEQ),
661150571b7SLee Jones };
662150571b7SLee Jones 
6635ecd3ea1SLee Jones /* Dummy sequence to read one byte of data from flash into the FIFO */
6645ecd3ea1SLee Jones static const struct stfsm_seq stfsm_seq_load_fifo_byte = {
6655ecd3ea1SLee Jones 	.data_size = TRANSFER_SIZE(1),
6665ecd3ea1SLee Jones 	.seq_opc[0] = (SEQ_OPC_PADS_1 |
6675ecd3ea1SLee Jones 		       SEQ_OPC_CYCLES(8) |
6685ecd3ea1SLee Jones 		       SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
6695ecd3ea1SLee Jones 	.seq = {
6705ecd3ea1SLee Jones 		STFSM_INST_CMD1,
6715ecd3ea1SLee Jones 		STFSM_INST_DATA_READ,
6725ecd3ea1SLee Jones 		STFSM_INST_STOP,
6735ecd3ea1SLee Jones 	},
6745ecd3ea1SLee Jones 	.seq_cfg = (SEQ_CFG_PADS_1 |
6755ecd3ea1SLee Jones 		    SEQ_CFG_READNOTWRITE |
6765ecd3ea1SLee Jones 		    SEQ_CFG_CSDEASSERT |
6775ecd3ea1SLee Jones 		    SEQ_CFG_STARTSEQ),
6785ecd3ea1SLee Jones };
6795ecd3ea1SLee Jones 
stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq * seq)6806bd29600SLee Jones static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
6816bd29600SLee Jones {
6826bd29600SLee Jones 	seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
6836c8e1b33SBrian Norris 			   SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
6846bd29600SLee Jones 	seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
68592d3af9aSBrian Norris 			   SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
6866bd29600SLee Jones 			   SEQ_OPC_CSDEASSERT);
6876bd29600SLee Jones 
6886bd29600SLee Jones 	seq->seq[0] = STFSM_INST_CMD2;
6896bd29600SLee Jones 	seq->seq[1] = STFSM_INST_CMD1;
6906bd29600SLee Jones 	seq->seq[2] = STFSM_INST_WAIT;
6916bd29600SLee Jones 	seq->seq[3] = STFSM_INST_STOP;
6926bd29600SLee Jones 
6936bd29600SLee Jones 	seq->seq_cfg = (SEQ_CFG_PADS_1 |
6946bd29600SLee Jones 			SEQ_CFG_ERASE |
6956bd29600SLee Jones 			SEQ_CFG_READNOTWRITE |
6966bd29600SLee Jones 			SEQ_CFG_CSDEASSERT |
6976bd29600SLee Jones 			SEQ_CFG_STARTSEQ);
6986bd29600SLee Jones 
6996bd29600SLee Jones 	return 0;
7006bd29600SLee Jones }
7016bd29600SLee Jones 
stfsm_is_idle(struct stfsm * fsm)7023c8b85b3SLee Jones static inline int stfsm_is_idle(struct stfsm *fsm)
7033c8b85b3SLee Jones {
7043c8b85b3SLee Jones 	return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
7053c8b85b3SLee Jones }
7063c8b85b3SLee Jones 
stfsm_fifo_available(struct stfsm * fsm)70786f309fdSLee Jones static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
70886f309fdSLee Jones {
70986f309fdSLee Jones 	return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
71086f309fdSLee Jones }
71186f309fdSLee Jones 
stfsm_load_seq(struct stfsm * fsm,const struct stfsm_seq * seq)7123c8b85b3SLee Jones static inline void stfsm_load_seq(struct stfsm *fsm,
7133c8b85b3SLee Jones 				  const struct stfsm_seq *seq)
7143c8b85b3SLee Jones {
7153c8b85b3SLee Jones 	void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
7163c8b85b3SLee Jones 	const uint32_t *src = (const uint32_t *)seq;
7173c8b85b3SLee Jones 	int words = sizeof(*seq) / sizeof(*src);
7183c8b85b3SLee Jones 
7193c8b85b3SLee Jones 	BUG_ON(!stfsm_is_idle(fsm));
7203c8b85b3SLee Jones 
7213c8b85b3SLee Jones 	while (words--) {
7223c8b85b3SLee Jones 		writel(*src, dst);
7233c8b85b3SLee Jones 		src++;
7243c8b85b3SLee Jones 		dst += 4;
7253c8b85b3SLee Jones 	}
7263c8b85b3SLee Jones }
7273c8b85b3SLee Jones 
stfsm_wait_seq(struct stfsm * fsm)7283c8b85b3SLee Jones static void stfsm_wait_seq(struct stfsm *fsm)
7293c8b85b3SLee Jones {
7303c8b85b3SLee Jones 	unsigned long deadline;
7313c8b85b3SLee Jones 	int timeout = 0;
7323c8b85b3SLee Jones 
7333c8b85b3SLee Jones 	deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
7343c8b85b3SLee Jones 
7353c8b85b3SLee Jones 	while (!timeout) {
7363c8b85b3SLee Jones 		if (time_after_eq(jiffies, deadline))
7373c8b85b3SLee Jones 			timeout = 1;
7383c8b85b3SLee Jones 
7393c8b85b3SLee Jones 		if (stfsm_is_idle(fsm))
7403c8b85b3SLee Jones 			return;
7413c8b85b3SLee Jones 
7423c8b85b3SLee Jones 		cond_resched();
7433c8b85b3SLee Jones 	}
7443c8b85b3SLee Jones 
7453c8b85b3SLee Jones 	dev_err(fsm->dev, "timeout on sequence completion\n");
7463c8b85b3SLee Jones }
7473c8b85b3SLee Jones 
stfsm_read_fifo(struct stfsm * fsm,uint32_t * buf,uint32_t size)7483f9d720aSLee Jones static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
749030e82dcSLee Jones {
750030e82dcSLee Jones 	uint32_t remaining = size >> 2;
751030e82dcSLee Jones 	uint32_t avail;
752030e82dcSLee Jones 	uint32_t words;
753030e82dcSLee Jones 
754030e82dcSLee Jones 	dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
755030e82dcSLee Jones 
75638e2eee9SBrian Norris 	BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
757030e82dcSLee Jones 
758030e82dcSLee Jones 	while (remaining) {
759030e82dcSLee Jones 		for (;;) {
760030e82dcSLee Jones 			avail = stfsm_fifo_available(fsm);
761030e82dcSLee Jones 			if (avail)
762030e82dcSLee Jones 				break;
763030e82dcSLee Jones 			udelay(1);
764030e82dcSLee Jones 		}
765030e82dcSLee Jones 		words = min(avail, remaining);
766030e82dcSLee Jones 		remaining -= words;
767030e82dcSLee Jones 
768030e82dcSLee Jones 		readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
769030e82dcSLee Jones 		buf += words;
770030e82dcSLee Jones 	}
771030e82dcSLee Jones }
772030e82dcSLee Jones 
7735ecd3ea1SLee Jones /*
7745ecd3ea1SLee Jones  * Clear the data FIFO
7755ecd3ea1SLee Jones  *
7765ecd3ea1SLee Jones  * Typically, this is only required during driver initialisation, where no
7775ecd3ea1SLee Jones  * assumptions can be made regarding the state of the FIFO.
7785ecd3ea1SLee Jones  *
7795ecd3ea1SLee Jones  * The process of clearing the FIFO is complicated by fact that while it is
7805ecd3ea1SLee Jones  * possible for the FIFO to contain an arbitrary number of bytes [1], the
7815ecd3ea1SLee Jones  * SPI_FAST_SEQ_STA register only reports the number of complete 32-bit words
7825ecd3ea1SLee Jones  * present.  Furthermore, data can only be drained from the FIFO by reading
7835ecd3ea1SLee Jones  * complete 32-bit words.
7845ecd3ea1SLee Jones  *
7855ecd3ea1SLee Jones  * With this in mind, a two stage process is used to the clear the FIFO:
7865ecd3ea1SLee Jones  *
7875ecd3ea1SLee Jones  *     1. Read any complete 32-bit words from the FIFO, as reported by the
7885ecd3ea1SLee Jones  *        SPI_FAST_SEQ_STA register.
7895ecd3ea1SLee Jones  *
7905ecd3ea1SLee Jones  *     2. Mop up any remaining bytes.  At this point, it is not known if there
7915ecd3ea1SLee Jones  *        are 0, 1, 2, or 3 bytes in the FIFO.  To handle all cases, a dummy FSM
7925ecd3ea1SLee Jones  *        sequence is used to load one byte at a time, until a complete 32-bit
7935ecd3ea1SLee Jones  *        word is formed; at most, 4 bytes will need to be loaded.
7945ecd3ea1SLee Jones  *
7955ecd3ea1SLee Jones  * [1] It is theoretically possible for the FIFO to contain an arbitrary number
7965ecd3ea1SLee Jones  *     of bits.  However, since there are no known use-cases that leave
7975ecd3ea1SLee Jones  *     incomplete bytes in the FIFO, only words and bytes are considered here.
7985ecd3ea1SLee Jones  */
stfsm_clear_fifo(struct stfsm * fsm)7995ecd3ea1SLee Jones static void stfsm_clear_fifo(struct stfsm *fsm)
8005ecd3ea1SLee Jones {
8015ecd3ea1SLee Jones 	const struct stfsm_seq *seq = &stfsm_seq_load_fifo_byte;
8025ecd3ea1SLee Jones 	uint32_t words, i;
8035ecd3ea1SLee Jones 
8045ecd3ea1SLee Jones 	/* 1. Clear any 32-bit words */
8055ecd3ea1SLee Jones 	words = stfsm_fifo_available(fsm);
8065ecd3ea1SLee Jones 	if (words) {
8075ecd3ea1SLee Jones 		for (i = 0; i < words; i++)
8085ecd3ea1SLee Jones 			readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
8095ecd3ea1SLee Jones 		dev_dbg(fsm->dev, "cleared %d words from FIFO\n", words);
8105ecd3ea1SLee Jones 	}
8115ecd3ea1SLee Jones 
8125ecd3ea1SLee Jones 	/*
8135ecd3ea1SLee Jones 	 * 2. Clear any remaining bytes
8145ecd3ea1SLee Jones 	 *    - Load the FIFO, one byte at a time, until a complete 32-bit word
8155ecd3ea1SLee Jones 	 *      is available.
8165ecd3ea1SLee Jones 	 */
8175ecd3ea1SLee Jones 	for (i = 0, words = 0; i < 4 && !words; i++) {
8185ecd3ea1SLee Jones 		stfsm_load_seq(fsm, seq);
8195ecd3ea1SLee Jones 		stfsm_wait_seq(fsm);
8205ecd3ea1SLee Jones 		words = stfsm_fifo_available(fsm);
8215ecd3ea1SLee Jones 	}
8225ecd3ea1SLee Jones 
8235ecd3ea1SLee Jones 	/*    - A single word must be available now */
8245ecd3ea1SLee Jones 	if (words != 1) {
8255ecd3ea1SLee Jones 		dev_err(fsm->dev, "failed to clear bytes from the data FIFO\n");
8265ecd3ea1SLee Jones 		return;
8275ecd3ea1SLee Jones 	}
8285ecd3ea1SLee Jones 
8295ecd3ea1SLee Jones 	/*    - Read the 32-bit word */
8305ecd3ea1SLee Jones 	readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
8315ecd3ea1SLee Jones 
8325ecd3ea1SLee Jones 	dev_dbg(fsm->dev, "cleared %d byte(s) from the data FIFO\n", 4 - i);
8335ecd3ea1SLee Jones }
8345ecd3ea1SLee Jones 
stfsm_write_fifo(struct stfsm * fsm,const uint32_t * buf,uint32_t size)8353f9d720aSLee Jones static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
8363f9d720aSLee Jones 			    uint32_t size)
83730ca64f9SLee Jones {
83830ca64f9SLee Jones 	uint32_t words = size >> 2;
83930ca64f9SLee Jones 
84030ca64f9SLee Jones 	dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
84130ca64f9SLee Jones 
84238e2eee9SBrian Norris 	BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
84330ca64f9SLee Jones 
84430ca64f9SLee Jones 	writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
84530ca64f9SLee Jones 
84630ca64f9SLee Jones 	return size;
84730ca64f9SLee Jones }
84830ca64f9SLee Jones 
stfsm_enter_32bit_addr(struct stfsm * fsm,int enter)8490de08e43SLee Jones static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
8500de08e43SLee Jones {
851e6b1bb4eSLee Jones 	struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
8526c8e1b33SBrian Norris 	uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
8530de08e43SLee Jones 
8540de08e43SLee Jones 	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
8550de08e43SLee Jones 			   SEQ_OPC_CYCLES(8) |
8560de08e43SLee Jones 			   SEQ_OPC_OPCODE(cmd) |
8570de08e43SLee Jones 			   SEQ_OPC_CSDEASSERT);
8580de08e43SLee Jones 
8590de08e43SLee Jones 	stfsm_load_seq(fsm, seq);
8600de08e43SLee Jones 
8610de08e43SLee Jones 	stfsm_wait_seq(fsm);
8620de08e43SLee Jones 
8630de08e43SLee Jones 	return 0;
8640de08e43SLee Jones }
8650de08e43SLee Jones 
stfsm_wait_busy(struct stfsm * fsm)866176b4377SLee Jones static uint8_t stfsm_wait_busy(struct stfsm *fsm)
867176b4377SLee Jones {
868176b4377SLee Jones 	struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
869176b4377SLee Jones 	unsigned long deadline;
870176b4377SLee Jones 	uint32_t status;
871176b4377SLee Jones 	int timeout = 0;
872176b4377SLee Jones 
873176b4377SLee Jones 	/* Use RDRS1 */
874176b4377SLee Jones 	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
875176b4377SLee Jones 			   SEQ_OPC_CYCLES(8) |
87692d3af9aSBrian Norris 			   SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
877176b4377SLee Jones 
878176b4377SLee Jones 	/* Load read_status sequence */
879176b4377SLee Jones 	stfsm_load_seq(fsm, seq);
880176b4377SLee Jones 
881176b4377SLee Jones 	/*
882176b4377SLee Jones 	 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
883176b4377SLee Jones 	 */
884176b4377SLee Jones 	deadline = jiffies + FLASH_MAX_BUSY_WAIT;
885176b4377SLee Jones 	while (!timeout) {
886176b4377SLee Jones 		if (time_after_eq(jiffies, deadline))
887176b4377SLee Jones 			timeout = 1;
888176b4377SLee Jones 
889176b4377SLee Jones 		stfsm_wait_seq(fsm);
890176b4377SLee Jones 
891176b4377SLee Jones 		stfsm_read_fifo(fsm, &status, 4);
892176b4377SLee Jones 
893176b4377SLee Jones 		if ((status & FLASH_STATUS_BUSY) == 0)
894176b4377SLee Jones 			return 0;
895176b4377SLee Jones 
896176b4377SLee Jones 		if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
897176b4377SLee Jones 		    ((status & S25FL_STATUS_P_ERR) ||
898176b4377SLee Jones 		     (status & S25FL_STATUS_E_ERR)))
899176b4377SLee Jones 			return (uint8_t)(status & 0xff);
900176b4377SLee Jones 
901176b4377SLee Jones 		if (!timeout)
902176b4377SLee Jones 			/* Restart */
903176b4377SLee Jones 			writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
904ea7864bfSLee Jones 
905ea7864bfSLee Jones 		cond_resched();
906176b4377SLee Jones 	}
907176b4377SLee Jones 
908176b4377SLee Jones 	dev_err(fsm->dev, "timeout on wait_busy\n");
909176b4377SLee Jones 
910176b4377SLee Jones 	return FLASH_STATUS_TIMEOUT;
911176b4377SLee Jones }
912176b4377SLee Jones 
stfsm_read_status(struct stfsm * fsm,uint8_t cmd,uint8_t * data,int bytes)913ac94dbcbSLee Jones static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
9145d0bddabSAngus Clark 			     uint8_t *data, int bytes)
915ac94dbcbSLee Jones {
916ac94dbcbSLee Jones 	struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
917ac94dbcbSLee Jones 	uint32_t tmp;
9185d0bddabSAngus Clark 	uint8_t *t = (uint8_t *)&tmp;
9195d0bddabSAngus Clark 	int i;
920ac94dbcbSLee Jones 
9215d0bddabSAngus Clark 	dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
9225d0bddabSAngus Clark 		cmd, bytes);
923ac94dbcbSLee Jones 
9245d0bddabSAngus Clark 	BUG_ON(bytes != 1 && bytes != 2);
9255d0bddabSAngus Clark 
9265d0bddabSAngus Clark 	seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
927106a3ec3SZheng Yongjun 			   SEQ_OPC_OPCODE(cmd));
928ac94dbcbSLee Jones 
929ac94dbcbSLee Jones 	stfsm_load_seq(fsm, seq);
930ac94dbcbSLee Jones 
931ac94dbcbSLee Jones 	stfsm_read_fifo(fsm, &tmp, 4);
932ac94dbcbSLee Jones 
9335d0bddabSAngus Clark 	for (i = 0; i < bytes; i++)
9345d0bddabSAngus Clark 		data[i] = t[i];
935ac94dbcbSLee Jones 
936ac94dbcbSLee Jones 	stfsm_wait_seq(fsm);
937ac94dbcbSLee Jones 
938ac94dbcbSLee Jones 	return 0;
939ac94dbcbSLee Jones }
940ac94dbcbSLee Jones 
stfsm_write_status(struct stfsm * fsm,uint8_t cmd,uint16_t data,int bytes,int wait_busy)9415d0bddabSAngus Clark static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
9425d0bddabSAngus Clark 			    uint16_t data, int bytes, int wait_busy)
943150571b7SLee Jones {
944150571b7SLee Jones 	struct stfsm_seq *seq = &stfsm_seq_write_status;
945150571b7SLee Jones 
9465d0bddabSAngus Clark 	dev_dbg(fsm->dev,
9475d0bddabSAngus Clark 		"write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
9485d0bddabSAngus Clark 		" %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
949150571b7SLee Jones 
9505d0bddabSAngus Clark 	BUG_ON(bytes != 1 && bytes != 2);
9515d0bddabSAngus Clark 
9525d0bddabSAngus Clark 	seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
9535d0bddabSAngus Clark 			   SEQ_OPC_OPCODE(cmd));
9545d0bddabSAngus Clark 
9555d0bddabSAngus Clark 	seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
9565d0bddabSAngus Clark 	seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
957150571b7SLee Jones 
958150571b7SLee Jones 	stfsm_load_seq(fsm, seq);
959150571b7SLee Jones 
960150571b7SLee Jones 	stfsm_wait_seq(fsm);
961150571b7SLee Jones 
9625d0bddabSAngus Clark 	if (wait_busy)
9635d0bddabSAngus Clark 		stfsm_wait_busy(fsm);
964249516c9SLee Jones 
965249516c9SLee Jones 	return 0;
966249516c9SLee Jones }
967249516c9SLee Jones 
9680ea7d706SLee Jones /*
9690ea7d706SLee Jones  * SoC reset on 'boot-from-spi' systems
9700ea7d706SLee Jones  *
9710ea7d706SLee Jones  * Certain modes of operation cause the Flash device to enter a particular state
9720ea7d706SLee Jones  * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
9730ea7d706SLee Jones  * Addr' commands).  On boot-from-spi systems, it is important to consider what
9740ea7d706SLee Jones  * happens if a warm reset occurs during this period.  The SPIBoot controller
9750ea7d706SLee Jones  * assumes that Flash device is in its default reset state, 24-bit address mode,
9760ea7d706SLee Jones  * and ready to accept commands.  This can be achieved using some form of
9770ea7d706SLee Jones  * on-board logic/controller to force a device POR in response to a SoC-level
9780ea7d706SLee Jones  * reset or by making use of the device reset signal if available (limited
9790ea7d706SLee Jones  * number of devices only).
9800ea7d706SLee Jones  *
9810ea7d706SLee Jones  * Failure to take such precautions can cause problems following a warm reset.
9820ea7d706SLee Jones  * For some operations (e.g. ERASE), there is little that can be done.  For
9830ea7d706SLee Jones  * other modes of operation (e.g. 32-bit addressing), options are often
9840ea7d706SLee Jones  * available that can help minimise the window in which a reset could cause a
9850ea7d706SLee Jones  * problem.
9860ea7d706SLee Jones  *
9870ea7d706SLee Jones  */
stfsm_can_handle_soc_reset(struct stfsm * fsm)9880ea7d706SLee Jones static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
9890ea7d706SLee Jones {
9900ea7d706SLee Jones 	/* Reset signal is available on the board and supported by the device */
9910ea7d706SLee Jones 	if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
9920ea7d706SLee Jones 		return true;
9930ea7d706SLee Jones 
9940ea7d706SLee Jones 	/* Board-level logic forces a power-on-reset */
9950ea7d706SLee Jones 	if (fsm->reset_por)
9960ea7d706SLee Jones 		return true;
9970ea7d706SLee Jones 
9980ea7d706SLee Jones 	/* Reset is not properly handled and may result in failure to reboot */
9990ea7d706SLee Jones 	return false;
10000ea7d706SLee Jones }
10010ea7d706SLee Jones 
1002fa5ba3afSLee Jones /* Configure 'addr_cfg' according to addressing mode */
stfsm_prepare_erasesec_seq(struct stfsm * fsm,struct stfsm_seq * seq)1003fa5ba3afSLee Jones static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
1004fa5ba3afSLee Jones 				       struct stfsm_seq *seq)
1005fa5ba3afSLee Jones {
1006fa5ba3afSLee Jones 	int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
1007fa5ba3afSLee Jones 
1008fa5ba3afSLee Jones 	seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
1009fa5ba3afSLee Jones 			 ADR_CFG_PADS_1_ADD1 |
1010fa5ba3afSLee Jones 			 ADR_CFG_CYCLES_ADD2(16) |
1011fa5ba3afSLee Jones 			 ADR_CFG_PADS_1_ADD2 |
1012fa5ba3afSLee Jones 			 ADR_CFG_CSDEASSERT_ADD2);
1013fa5ba3afSLee Jones }
1014fa5ba3afSLee Jones 
101508981274SLee Jones /* Search for preferred configuration based on available flags */
101608981274SLee Jones static struct seq_rw_config *
stfsm_search_seq_rw_configs(struct stfsm * fsm,struct seq_rw_config cfgs[])101708981274SLee Jones stfsm_search_seq_rw_configs(struct stfsm *fsm,
101808981274SLee Jones 			    struct seq_rw_config cfgs[])
101908981274SLee Jones {
102008981274SLee Jones 	struct seq_rw_config *config;
102108981274SLee Jones 	int flags = fsm->info->flags;
102208981274SLee Jones 
102308981274SLee Jones 	for (config = cfgs; config->cmd != 0; config++)
102408981274SLee Jones 		if ((config->flags & flags) == config->flags)
102508981274SLee Jones 			return config;
102608981274SLee Jones 
102708981274SLee Jones 	return NULL;
102808981274SLee Jones }
102908981274SLee Jones 
103097ccf2d2SLee Jones /* Prepare a READ/WRITE sequence according to configuration parameters */
stfsm_prepare_rw_seq(struct stfsm * fsm,struct stfsm_seq * seq,struct seq_rw_config * cfg)103197ccf2d2SLee Jones static void stfsm_prepare_rw_seq(struct stfsm *fsm,
103297ccf2d2SLee Jones 				 struct stfsm_seq *seq,
103397ccf2d2SLee Jones 				 struct seq_rw_config *cfg)
103497ccf2d2SLee Jones {
103597ccf2d2SLee Jones 	int addr1_cycles, addr2_cycles;
103697ccf2d2SLee Jones 	int i = 0;
103797ccf2d2SLee Jones 
103897ccf2d2SLee Jones 	memset(seq, 0, sizeof(*seq));
103997ccf2d2SLee Jones 
104097ccf2d2SLee Jones 	/* Add READ/WRITE OPC  */
104197ccf2d2SLee Jones 	seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
104297ccf2d2SLee Jones 			     SEQ_OPC_CYCLES(8) |
104397ccf2d2SLee Jones 			     SEQ_OPC_OPCODE(cfg->cmd));
104497ccf2d2SLee Jones 
104597ccf2d2SLee Jones 	/* Add WREN OPC for a WRITE sequence */
104697ccf2d2SLee Jones 	if (cfg->write)
104797ccf2d2SLee Jones 		seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
104897ccf2d2SLee Jones 				     SEQ_OPC_CYCLES(8) |
104992d3af9aSBrian Norris 				     SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
105097ccf2d2SLee Jones 				     SEQ_OPC_CSDEASSERT);
105197ccf2d2SLee Jones 
105297ccf2d2SLee Jones 	/* Address configuration (24 or 32-bit addresses) */
105397ccf2d2SLee Jones 	addr1_cycles  = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
105497ccf2d2SLee Jones 	addr1_cycles /= cfg->addr_pads;
105597ccf2d2SLee Jones 	addr2_cycles  = 16 / cfg->addr_pads;
105697ccf2d2SLee Jones 	seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 |	/* ADD1 cycles */
105797ccf2d2SLee Jones 			 (cfg->addr_pads - 1) << 6 |	/* ADD1 pads */
105897ccf2d2SLee Jones 			 (addr2_cycles & 0x3f) << 16 |	/* ADD2 cycles */
105997ccf2d2SLee Jones 			 ((cfg->addr_pads - 1) << 22));	/* ADD2 pads */
106097ccf2d2SLee Jones 
106197ccf2d2SLee Jones 	/* Data/Sequence configuration */
106297ccf2d2SLee Jones 	seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
106397ccf2d2SLee Jones 			SEQ_CFG_STARTSEQ |
106497ccf2d2SLee Jones 			SEQ_CFG_CSDEASSERT);
106597ccf2d2SLee Jones 	if (!cfg->write)
106697ccf2d2SLee Jones 		seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
106797ccf2d2SLee Jones 
106897ccf2d2SLee Jones 	/* Mode configuration (no. of pads taken from addr cfg) */
106997ccf2d2SLee Jones 	seq->mode = ((cfg->mode_data & 0xff) << 0 |	/* data */
107097ccf2d2SLee Jones 		     (cfg->mode_cycles & 0x3f) << 16 |	/* cycles */
107197ccf2d2SLee Jones 		     (cfg->addr_pads - 1) << 22);	/* pads */
107297ccf2d2SLee Jones 
107397ccf2d2SLee Jones 	/* Dummy configuration (no. of pads taken from addr cfg) */
107497ccf2d2SLee Jones 	seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 |	/* cycles */
107597ccf2d2SLee Jones 		      (cfg->addr_pads - 1) << 22);		/* pads */
107697ccf2d2SLee Jones 
107797ccf2d2SLee Jones 
107897ccf2d2SLee Jones 	/* Instruction sequence */
107997ccf2d2SLee Jones 	i = 0;
108097ccf2d2SLee Jones 	if (cfg->write)
108197ccf2d2SLee Jones 		seq->seq[i++] = STFSM_INST_CMD2;
108297ccf2d2SLee Jones 
108397ccf2d2SLee Jones 	seq->seq[i++] = STFSM_INST_CMD1;
108497ccf2d2SLee Jones 
108597ccf2d2SLee Jones 	seq->seq[i++] = STFSM_INST_ADD1;
108697ccf2d2SLee Jones 	seq->seq[i++] = STFSM_INST_ADD2;
108797ccf2d2SLee Jones 
108897ccf2d2SLee Jones 	if (cfg->mode_cycles)
108997ccf2d2SLee Jones 		seq->seq[i++] = STFSM_INST_MODE;
109097ccf2d2SLee Jones 
109197ccf2d2SLee Jones 	if (cfg->dummy_cycles)
109297ccf2d2SLee Jones 		seq->seq[i++] = STFSM_INST_DUMMY;
109397ccf2d2SLee Jones 
109497ccf2d2SLee Jones 	seq->seq[i++] =
109597ccf2d2SLee Jones 		cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
109697ccf2d2SLee Jones 	seq->seq[i++] = STFSM_INST_STOP;
109797ccf2d2SLee Jones }
109897ccf2d2SLee Jones 
stfsm_search_prepare_rw_seq(struct stfsm * fsm,struct stfsm_seq * seq,struct seq_rw_config * cfgs)109988cccb89SLee Jones static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
110088cccb89SLee Jones 				       struct stfsm_seq *seq,
110188cccb89SLee Jones 				       struct seq_rw_config *cfgs)
110288cccb89SLee Jones {
110388cccb89SLee Jones 	struct seq_rw_config *config;
110488cccb89SLee Jones 
110588cccb89SLee Jones 	config = stfsm_search_seq_rw_configs(fsm, cfgs);
110688cccb89SLee Jones 	if (!config) {
110788cccb89SLee Jones 		dev_err(fsm->dev, "failed to find suitable config\n");
110888cccb89SLee Jones 		return -EINVAL;
110988cccb89SLee Jones 	}
111088cccb89SLee Jones 
111188cccb89SLee Jones 	stfsm_prepare_rw_seq(fsm, seq, config);
111288cccb89SLee Jones 
111388cccb89SLee Jones 	return 0;
111488cccb89SLee Jones }
111588cccb89SLee Jones 
11164eb3f0d8SLee Jones /* Prepare a READ/WRITE/ERASE 'default' sequences */
stfsm_prepare_rwe_seqs_default(struct stfsm * fsm)11174eb3f0d8SLee Jones static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
11184eb3f0d8SLee Jones {
11194eb3f0d8SLee Jones 	uint32_t flags = fsm->info->flags;
11204eb3f0d8SLee Jones 	int ret;
11214eb3f0d8SLee Jones 
11224eb3f0d8SLee Jones 	/* Configure 'READ' sequence */
1123e6b1bb4eSLee Jones 	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
11244eb3f0d8SLee Jones 					  default_read_configs);
11254eb3f0d8SLee Jones 	if (ret) {
11264eb3f0d8SLee Jones 		dev_err(fsm->dev,
11274eb3f0d8SLee Jones 			"failed to prep READ sequence with flags [0x%08x]\n",
11284eb3f0d8SLee Jones 			flags);
11294eb3f0d8SLee Jones 		return ret;
11304eb3f0d8SLee Jones 	}
11314eb3f0d8SLee Jones 
11324eb3f0d8SLee Jones 	/* Configure 'WRITE' sequence */
1133e6b1bb4eSLee Jones 	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
11344eb3f0d8SLee Jones 					  default_write_configs);
11354eb3f0d8SLee Jones 	if (ret) {
11364eb3f0d8SLee Jones 		dev_err(fsm->dev,
11374eb3f0d8SLee Jones 			"failed to prep WRITE sequence with flags [0x%08x]\n",
11384eb3f0d8SLee Jones 			flags);
11394eb3f0d8SLee Jones 		return ret;
11404eb3f0d8SLee Jones 	}
11414eb3f0d8SLee Jones 
11424eb3f0d8SLee Jones 	/* Configure 'ERASE_SECTOR' sequence */
11434eb3f0d8SLee Jones 	stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
11444eb3f0d8SLee Jones 
11454eb3f0d8SLee Jones 	return 0;
11464eb3f0d8SLee Jones }
11474eb3f0d8SLee Jones 
stfsm_mx25_config(struct stfsm * fsm)114889818066SLee Jones static int stfsm_mx25_config(struct stfsm *fsm)
114989818066SLee Jones {
115089818066SLee Jones 	uint32_t flags = fsm->info->flags;
115189818066SLee Jones 	uint32_t data_pads;
115289818066SLee Jones 	uint8_t sta;
115389818066SLee Jones 	int ret;
115489818066SLee Jones 	bool soc_reset;
115589818066SLee Jones 
115689818066SLee Jones 	/*
115789818066SLee Jones 	 * Use default READ/WRITE sequences
115889818066SLee Jones 	 */
115989818066SLee Jones 	ret = stfsm_prepare_rwe_seqs_default(fsm);
116089818066SLee Jones 	if (ret)
116189818066SLee Jones 		return ret;
116289818066SLee Jones 
116389818066SLee Jones 	/*
116489818066SLee Jones 	 * Configure 32-bit Address Support
116589818066SLee Jones 	 */
116689818066SLee Jones 	if (flags & FLASH_FLAG_32BIT_ADDR) {
116789818066SLee Jones 		/* Configure 'enter_32bitaddr' FSM sequence */
1168e6b1bb4eSLee Jones 		stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
116989818066SLee Jones 
117089818066SLee Jones 		soc_reset = stfsm_can_handle_soc_reset(fsm);
1171009e7e61SAngus Clark 		if (soc_reset || !fsm->booted_from_spi)
117289818066SLee Jones 			/* If we can handle SoC resets, we enable 32-bit address
117389818066SLee Jones 			 * mode pervasively */
117489818066SLee Jones 			stfsm_enter_32bit_addr(fsm, 1);
117589818066SLee Jones 
1176009e7e61SAngus Clark 		else
117789818066SLee Jones 			/* Else, enable/disable 32-bit addressing before/after
117889818066SLee Jones 			 * each operation */
117989818066SLee Jones 			fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
118089818066SLee Jones 					      CFG_WRITE_TOGGLE_32BIT_ADDR |
118189818066SLee Jones 					      CFG_ERASESEC_TOGGLE_32BIT_ADDR);
118289818066SLee Jones 	}
118389818066SLee Jones 
11845d0bddabSAngus Clark 	/* Check status of 'QE' bit, update if required. */
118592d3af9aSBrian Norris 	stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
1186e6b1bb4eSLee Jones 	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
118789818066SLee Jones 	if (data_pads == 4) {
1188cc666863SAngus Clark 		if (!(sta & MX25_STATUS_QE)) {
1189cc666863SAngus Clark 			/* Set 'QE' */
119089818066SLee Jones 			sta |= MX25_STATUS_QE;
1191cc666863SAngus Clark 
119292d3af9aSBrian Norris 			stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1193cc666863SAngus Clark 		}
1194cc666863SAngus Clark 	} else {
1195cc666863SAngus Clark 		if (sta & MX25_STATUS_QE) {
1196cc666863SAngus Clark 			/* Clear 'QE' */
1197cc666863SAngus Clark 			sta &= ~MX25_STATUS_QE;
1198cc666863SAngus Clark 
119992d3af9aSBrian Norris 			stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
1200cc666863SAngus Clark 		}
120189818066SLee Jones 	}
120289818066SLee Jones 
120389818066SLee Jones 	return 0;
120489818066SLee Jones }
120589818066SLee Jones 
stfsm_n25q_config(struct stfsm * fsm)1206218b870fSLee Jones static int stfsm_n25q_config(struct stfsm *fsm)
1207218b870fSLee Jones {
1208218b870fSLee Jones 	uint32_t flags = fsm->info->flags;
1209218b870fSLee Jones 	uint8_t vcr;
1210218b870fSLee Jones 	int ret = 0;
1211218b870fSLee Jones 	bool soc_reset;
1212218b870fSLee Jones 
1213218b870fSLee Jones 	/* Configure 'READ' sequence */
1214218b870fSLee Jones 	if (flags & FLASH_FLAG_32BIT_ADDR)
1215e6b1bb4eSLee Jones 		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1216218b870fSLee Jones 						  n25q_read4_configs);
1217218b870fSLee Jones 	else
1218e6b1bb4eSLee Jones 		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
1219218b870fSLee Jones 						  n25q_read3_configs);
1220218b870fSLee Jones 	if (ret) {
1221218b870fSLee Jones 		dev_err(fsm->dev,
1222218b870fSLee Jones 			"failed to prepare READ sequence with flags [0x%08x]\n",
1223218b870fSLee Jones 			flags);
1224218b870fSLee Jones 		return ret;
1225218b870fSLee Jones 	}
1226218b870fSLee Jones 
1227218b870fSLee Jones 	/* Configure 'WRITE' sequence (default configs) */
1228e6b1bb4eSLee Jones 	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
1229218b870fSLee Jones 					  default_write_configs);
1230218b870fSLee Jones 	if (ret) {
1231218b870fSLee Jones 		dev_err(fsm->dev,
1232218b870fSLee Jones 			"preparing WRITE sequence using flags [0x%08x] failed\n",
1233218b870fSLee Jones 			flags);
1234218b870fSLee Jones 		return ret;
1235218b870fSLee Jones 	}
1236218b870fSLee Jones 
1237218b870fSLee Jones 	/* * Configure 'ERASE_SECTOR' sequence */
1238218b870fSLee Jones 	stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
1239218b870fSLee Jones 
1240218b870fSLee Jones 	/* Configure 32-bit address support */
1241218b870fSLee Jones 	if (flags & FLASH_FLAG_32BIT_ADDR) {
1242e6b1bb4eSLee Jones 		stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
1243218b870fSLee Jones 
1244218b870fSLee Jones 		soc_reset = stfsm_can_handle_soc_reset(fsm);
1245218b870fSLee Jones 		if (soc_reset || !fsm->booted_from_spi) {
1246218b870fSLee Jones 			/*
1247218b870fSLee Jones 			 * If we can handle SoC resets, we enable 32-bit
1248218b870fSLee Jones 			 * address mode pervasively
1249218b870fSLee Jones 			 */
1250218b870fSLee Jones 			stfsm_enter_32bit_addr(fsm, 1);
1251218b870fSLee Jones 		} else {
1252218b870fSLee Jones 			/*
1253218b870fSLee Jones 			 * If not, enable/disable for WRITE and ERASE
1254218b870fSLee Jones 			 * operations (READ uses special commands)
1255218b870fSLee Jones 			 */
1256218b870fSLee Jones 			fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
1257218b870fSLee Jones 					      CFG_ERASESEC_TOGGLE_32BIT_ADDR);
1258218b870fSLee Jones 		}
1259218b870fSLee Jones 	}
1260218b870fSLee Jones 
1261218b870fSLee Jones 	/*
1262218b870fSLee Jones 	 * Configure device to use 8 dummy cycles
1263218b870fSLee Jones 	 */
1264218b870fSLee Jones 	vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
1265218b870fSLee Jones 	       N25Q_VCR_WRAP_CONT);
12665d0bddabSAngus Clark 	stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
1267218b870fSLee Jones 
1268218b870fSLee Jones 	return 0;
1269218b870fSLee Jones }
1270218b870fSLee Jones 
stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq * seq)12715343a123SLee Jones static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
12725343a123SLee Jones {
12735343a123SLee Jones 	seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
12745343a123SLee Jones 			   SEQ_OPC_CYCLES(8) |
12755343a123SLee Jones 			   SEQ_OPC_OPCODE(S25FL_CMD_SE4));
12765343a123SLee Jones 
12775343a123SLee Jones 	seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
12785343a123SLee Jones 			 ADR_CFG_PADS_1_ADD1 |
12795343a123SLee Jones 			 ADR_CFG_CYCLES_ADD2(16) |
12805343a123SLee Jones 			 ADR_CFG_PADS_1_ADD2 |
12815343a123SLee Jones 			 ADR_CFG_CSDEASSERT_ADD2);
12825343a123SLee Jones }
12835343a123SLee Jones 
stfsm_s25fl_read_dyb(struct stfsm * fsm,uint32_t offs,uint8_t * dby)12845343a123SLee Jones static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
12855343a123SLee Jones {
12865343a123SLee Jones 	uint32_t tmp;
12875343a123SLee Jones 	struct stfsm_seq seq = {
12885343a123SLee Jones 		.data_size = TRANSFER_SIZE(4),
12895343a123SLee Jones 		.seq_opc[0] = (SEQ_OPC_PADS_1 |
12905343a123SLee Jones 			       SEQ_OPC_CYCLES(8) |
12915343a123SLee Jones 			       SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
12925343a123SLee Jones 		.addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
12935343a123SLee Jones 			     ADR_CFG_PADS_1_ADD1 |
12945343a123SLee Jones 			     ADR_CFG_CYCLES_ADD2(16) |
12955343a123SLee Jones 			     ADR_CFG_PADS_1_ADD2),
12965343a123SLee Jones 		.addr1 = (offs >> 16) & 0xffff,
12975343a123SLee Jones 		.addr2 = offs & 0xffff,
12985343a123SLee Jones 		.seq = {
12995343a123SLee Jones 			STFSM_INST_CMD1,
13005343a123SLee Jones 			STFSM_INST_ADD1,
13015343a123SLee Jones 			STFSM_INST_ADD2,
13025343a123SLee Jones 			STFSM_INST_DATA_READ,
13035343a123SLee Jones 			STFSM_INST_STOP,
13045343a123SLee Jones 		},
13055343a123SLee Jones 		.seq_cfg = (SEQ_CFG_PADS_1 |
13065343a123SLee Jones 			    SEQ_CFG_READNOTWRITE |
13075343a123SLee Jones 			    SEQ_CFG_CSDEASSERT |
13085343a123SLee Jones 			    SEQ_CFG_STARTSEQ),
13095343a123SLee Jones 	};
13105343a123SLee Jones 
13115343a123SLee Jones 	stfsm_load_seq(fsm, &seq);
13125343a123SLee Jones 
13135343a123SLee Jones 	stfsm_read_fifo(fsm, &tmp, 4);
13145343a123SLee Jones 
13155343a123SLee Jones 	*dby = (uint8_t)(tmp >> 24);
13165343a123SLee Jones 
13175343a123SLee Jones 	stfsm_wait_seq(fsm);
13185343a123SLee Jones }
13195343a123SLee Jones 
stfsm_s25fl_write_dyb(struct stfsm * fsm,uint32_t offs,uint8_t dby)13205343a123SLee Jones static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
13215343a123SLee Jones {
13225343a123SLee Jones 	struct stfsm_seq seq = {
13235343a123SLee Jones 		.seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
132492d3af9aSBrian Norris 			       SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
13255343a123SLee Jones 			       SEQ_OPC_CSDEASSERT),
13265343a123SLee Jones 		.seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
13275343a123SLee Jones 			       SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
13285343a123SLee Jones 		.addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
13295343a123SLee Jones 			     ADR_CFG_PADS_1_ADD1 |
13305343a123SLee Jones 			     ADR_CFG_CYCLES_ADD2(16) |
13315343a123SLee Jones 			     ADR_CFG_PADS_1_ADD2),
13325343a123SLee Jones 		.status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
13335343a123SLee Jones 		.addr1 = (offs >> 16) & 0xffff,
13345343a123SLee Jones 		.addr2 = offs & 0xffff,
13355343a123SLee Jones 		.seq = {
13365343a123SLee Jones 			STFSM_INST_CMD1,
13375343a123SLee Jones 			STFSM_INST_CMD2,
13385343a123SLee Jones 			STFSM_INST_ADD1,
13395343a123SLee Jones 			STFSM_INST_ADD2,
13405343a123SLee Jones 			STFSM_INST_STA_WR1,
13415343a123SLee Jones 			STFSM_INST_STOP,
13425343a123SLee Jones 		},
13435343a123SLee Jones 		.seq_cfg = (SEQ_CFG_PADS_1 |
13445343a123SLee Jones 			    SEQ_CFG_READNOTWRITE |
13455343a123SLee Jones 			    SEQ_CFG_CSDEASSERT |
13465343a123SLee Jones 			    SEQ_CFG_STARTSEQ),
13475343a123SLee Jones 	};
13485343a123SLee Jones 
13495343a123SLee Jones 	stfsm_load_seq(fsm, &seq);
13505343a123SLee Jones 	stfsm_wait_seq(fsm);
13515343a123SLee Jones 
13525343a123SLee Jones 	stfsm_wait_busy(fsm);
13535343a123SLee Jones }
13545343a123SLee Jones 
stfsm_s25fl_clear_status_reg(struct stfsm * fsm)13555343a123SLee Jones static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
13565343a123SLee Jones {
13575343a123SLee Jones 	struct stfsm_seq seq = {
13585343a123SLee Jones 		.seq_opc[0] = (SEQ_OPC_PADS_1 |
13595343a123SLee Jones 			       SEQ_OPC_CYCLES(8) |
13605343a123SLee Jones 			       SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
13615343a123SLee Jones 			       SEQ_OPC_CSDEASSERT),
13625343a123SLee Jones 		.seq_opc[1] = (SEQ_OPC_PADS_1 |
13635343a123SLee Jones 			       SEQ_OPC_CYCLES(8) |
136492d3af9aSBrian Norris 			       SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
13655343a123SLee Jones 			       SEQ_OPC_CSDEASSERT),
13665343a123SLee Jones 		.seq = {
13675343a123SLee Jones 			STFSM_INST_CMD1,
13685343a123SLee Jones 			STFSM_INST_CMD2,
13695343a123SLee Jones 			STFSM_INST_WAIT,
13705343a123SLee Jones 			STFSM_INST_STOP,
13715343a123SLee Jones 		},
13725343a123SLee Jones 		.seq_cfg = (SEQ_CFG_PADS_1 |
13735343a123SLee Jones 			    SEQ_CFG_ERASE |
13745343a123SLee Jones 			    SEQ_CFG_READNOTWRITE |
13755343a123SLee Jones 			    SEQ_CFG_CSDEASSERT |
13765343a123SLee Jones 			    SEQ_CFG_STARTSEQ),
13775343a123SLee Jones 	};
13785343a123SLee Jones 
13795343a123SLee Jones 	stfsm_load_seq(fsm, &seq);
13805343a123SLee Jones 
13815343a123SLee Jones 	stfsm_wait_seq(fsm);
13825343a123SLee Jones 
13835343a123SLee Jones 	return 0;
13845343a123SLee Jones }
13855343a123SLee Jones 
stfsm_s25fl_config(struct stfsm * fsm)13865343a123SLee Jones static int stfsm_s25fl_config(struct stfsm *fsm)
13875343a123SLee Jones {
13885343a123SLee Jones 	struct flash_info *info = fsm->info;
13895343a123SLee Jones 	uint32_t flags = info->flags;
13905343a123SLee Jones 	uint32_t data_pads;
13915343a123SLee Jones 	uint32_t offs;
13925343a123SLee Jones 	uint16_t sta_wr;
13935343a123SLee Jones 	uint8_t sr1, cr1, dyb;
13945d0bddabSAngus Clark 	int update_sr = 0;
13955343a123SLee Jones 	int ret;
13965343a123SLee Jones 
13975343a123SLee Jones 	if (flags & FLASH_FLAG_32BIT_ADDR) {
13985343a123SLee Jones 		/*
13995343a123SLee Jones 		 * Prepare Read/Write/Erase sequences according to S25FLxxx
14005343a123SLee Jones 		 * 32-bit address command set
14015343a123SLee Jones 		 */
1402e6b1bb4eSLee Jones 		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
14035343a123SLee Jones 						  stfsm_s25fl_read4_configs);
14045343a123SLee Jones 		if (ret)
14055343a123SLee Jones 			return ret;
14065343a123SLee Jones 
1407e6b1bb4eSLee Jones 		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
14085343a123SLee Jones 						  stfsm_s25fl_write4_configs);
14095343a123SLee Jones 		if (ret)
14105343a123SLee Jones 			return ret;
14115343a123SLee Jones 
14125343a123SLee Jones 		stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
14135343a123SLee Jones 
14145343a123SLee Jones 	} else {
14155343a123SLee Jones 		/* Use default configurations for 24-bit addressing */
14165343a123SLee Jones 		ret = stfsm_prepare_rwe_seqs_default(fsm);
14175343a123SLee Jones 		if (ret)
14185343a123SLee Jones 			return ret;
14195343a123SLee Jones 	}
14205343a123SLee Jones 
14215343a123SLee Jones 	/*
14225343a123SLee Jones 	 * For devices that support 'DYB' sector locking, check lock status and
14235343a123SLee Jones 	 * unlock sectors if necessary (some variants power-on with sectors
14245343a123SLee Jones 	 * locked by default)
14255343a123SLee Jones 	 */
14265343a123SLee Jones 	if (flags & FLASH_FLAG_DYB_LOCKING) {
14275343a123SLee Jones 		offs = 0;
14285343a123SLee Jones 		for (offs = 0; offs < info->sector_size * info->n_sectors;) {
14295343a123SLee Jones 			stfsm_s25fl_read_dyb(fsm, offs, &dyb);
14305343a123SLee Jones 			if (dyb == 0x00)
14315343a123SLee Jones 				stfsm_s25fl_write_dyb(fsm, offs, 0xff);
14325343a123SLee Jones 
14335343a123SLee Jones 			/* Handle bottom/top 4KiB parameter sectors */
14345343a123SLee Jones 			if ((offs < info->sector_size * 2) ||
14355343a123SLee Jones 			    (offs >= (info->sector_size - info->n_sectors * 4)))
14365343a123SLee Jones 				offs += 0x1000;
14375343a123SLee Jones 			else
14385343a123SLee Jones 				offs += 0x10000;
14395343a123SLee Jones 		}
14405343a123SLee Jones 	}
14415343a123SLee Jones 
14425d0bddabSAngus Clark 	/* Check status of 'QE' bit, update if required. */
14439447332fSCyrille Pitchen 	stfsm_read_status(fsm, SPINOR_OP_RDCR, &cr1, 1);
1444e6b1bb4eSLee Jones 	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
14455343a123SLee Jones 	if (data_pads == 4) {
14465343a123SLee Jones 		if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
14475343a123SLee Jones 			/* Set 'QE' */
14485343a123SLee Jones 			cr1 |= STFSM_S25FL_CONFIG_QE;
14495343a123SLee Jones 
14505d0bddabSAngus Clark 			update_sr = 1;
14515343a123SLee Jones 		}
14525343a123SLee Jones 	} else {
14535d0bddabSAngus Clark 		if (cr1 & STFSM_S25FL_CONFIG_QE) {
14545343a123SLee Jones 			/* Clear 'QE' */
14555343a123SLee Jones 			cr1 &= ~STFSM_S25FL_CONFIG_QE;
14565343a123SLee Jones 
14575d0bddabSAngus Clark 			update_sr = 1;
14585343a123SLee Jones 		}
14595d0bddabSAngus Clark 	}
14605d0bddabSAngus Clark 	if (update_sr) {
146192d3af9aSBrian Norris 		stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
14625d0bddabSAngus Clark 		sta_wr = ((uint16_t)cr1  << 8) | sr1;
146392d3af9aSBrian Norris 		stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
14645343a123SLee Jones 	}
14655343a123SLee Jones 
14665343a123SLee Jones 	/*
14675343a123SLee Jones 	 * S25FLxxx devices support Program and Error error flags.
14685343a123SLee Jones 	 * Configure driver to check flags and clear if necessary.
14695343a123SLee Jones 	 */
14705343a123SLee Jones 	fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
14715343a123SLee Jones 
14725343a123SLee Jones 	return 0;
14735343a123SLee Jones }
14745343a123SLee Jones 
stfsm_w25q_config(struct stfsm * fsm)1475cd7cac9eSLee Jones static int stfsm_w25q_config(struct stfsm *fsm)
1476cd7cac9eSLee Jones {
1477cd7cac9eSLee Jones 	uint32_t data_pads;
14785d0bddabSAngus Clark 	uint8_t sr1, sr2;
14795d0bddabSAngus Clark 	uint16_t sr_wr;
14805d0bddabSAngus Clark 	int update_sr = 0;
1481cd7cac9eSLee Jones 	int ret;
1482cd7cac9eSLee Jones 
1483cd7cac9eSLee Jones 	ret = stfsm_prepare_rwe_seqs_default(fsm);
1484cd7cac9eSLee Jones 	if (ret)
1485cd7cac9eSLee Jones 		return ret;
1486cd7cac9eSLee Jones 
14875d0bddabSAngus Clark 	/* Check status of 'QE' bit, update if required. */
14889447332fSCyrille Pitchen 	stfsm_read_status(fsm, SPINOR_OP_RDCR, &sr2, 1);
1489e6b1bb4eSLee Jones 	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
1490cd7cac9eSLee Jones 	if (data_pads == 4) {
14915d0bddabSAngus Clark 		if (!(sr2 & W25Q_STATUS_QE)) {
14925d0bddabSAngus Clark 			/* Set 'QE' */
14935d0bddabSAngus Clark 			sr2 |= W25Q_STATUS_QE;
14945d0bddabSAngus Clark 			update_sr = 1;
14955d0bddabSAngus Clark 		}
14965d0bddabSAngus Clark 	} else {
14975d0bddabSAngus Clark 		if (sr2 & W25Q_STATUS_QE) {
14985d0bddabSAngus Clark 			/* Clear 'QE' */
14995d0bddabSAngus Clark 			sr2 &= ~W25Q_STATUS_QE;
15005d0bddabSAngus Clark 			update_sr = 1;
15015d0bddabSAngus Clark 		}
15025d0bddabSAngus Clark 	}
15035d0bddabSAngus Clark 	if (update_sr) {
15045d0bddabSAngus Clark 		/* Write status register */
150592d3af9aSBrian Norris 		stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
15065d0bddabSAngus Clark 		sr_wr = ((uint16_t)sr2 << 8) | sr1;
150792d3af9aSBrian Norris 		stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
1508cd7cac9eSLee Jones 	}
1509cd7cac9eSLee Jones 
1510cd7cac9eSLee Jones 	return 0;
1511cd7cac9eSLee Jones }
1512cd7cac9eSLee Jones 
stfsm_read(struct stfsm * fsm,uint8_t * buf,uint32_t size,uint32_t offset)1513e514f105SLee Jones static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
1514e514f105SLee Jones 		      uint32_t offset)
1515e514f105SLee Jones {
1516e6b1bb4eSLee Jones 	struct stfsm_seq *seq = &fsm->stfsm_seq_read;
1517e514f105SLee Jones 	uint32_t data_pads;
1518e514f105SLee Jones 	uint32_t read_mask;
1519e514f105SLee Jones 	uint32_t size_ub;
1520e514f105SLee Jones 	uint32_t size_lb;
1521e514f105SLee Jones 	uint32_t size_mop;
1522e514f105SLee Jones 	uint32_t tmp[4];
1523e514f105SLee Jones 	uint32_t page_buf[FLASH_PAGESIZE_32];
1524e514f105SLee Jones 	uint8_t *p;
1525e514f105SLee Jones 
1526e514f105SLee Jones 	dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
1527e514f105SLee Jones 
1528e514f105SLee Jones 	/* Enter 32-bit address mode, if required */
1529e514f105SLee Jones 	if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1530e514f105SLee Jones 		stfsm_enter_32bit_addr(fsm, 1);
1531e514f105SLee Jones 
1532e514f105SLee Jones 	/* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
1533e514f105SLee Jones 	data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1534e514f105SLee Jones 	read_mask = (data_pads << 2) - 1;
1535e514f105SLee Jones 
1536e514f105SLee Jones 	/* Handle non-aligned buf */
153738e2eee9SBrian Norris 	p = ((uintptr_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
1538e514f105SLee Jones 
1539e514f105SLee Jones 	/* Handle non-aligned size */
1540e514f105SLee Jones 	size_ub = (size + read_mask) & ~read_mask;
1541e514f105SLee Jones 	size_lb = size & ~read_mask;
1542e514f105SLee Jones 	size_mop = size & read_mask;
1543e514f105SLee Jones 
1544e514f105SLee Jones 	seq->data_size = TRANSFER_SIZE(size_ub);
1545e514f105SLee Jones 	seq->addr1 = (offset >> 16) & 0xffff;
1546e514f105SLee Jones 	seq->addr2 = offset & 0xffff;
1547e514f105SLee Jones 
1548e514f105SLee Jones 	stfsm_load_seq(fsm, seq);
1549e514f105SLee Jones 
1550e514f105SLee Jones 	if (size_lb)
1551e514f105SLee Jones 		stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
1552e514f105SLee Jones 
1553e514f105SLee Jones 	if (size_mop) {
1554e514f105SLee Jones 		stfsm_read_fifo(fsm, tmp, read_mask + 1);
1555e514f105SLee Jones 		memcpy(p + size_lb, &tmp, size_mop);
1556e514f105SLee Jones 	}
1557e514f105SLee Jones 
1558e514f105SLee Jones 	/* Handle non-aligned buf */
155938e2eee9SBrian Norris 	if ((uintptr_t)buf & 0x3)
1560e514f105SLee Jones 		memcpy(buf, page_buf, size);
1561e514f105SLee Jones 
1562e514f105SLee Jones 	/* Wait for sequence to finish */
1563e514f105SLee Jones 	stfsm_wait_seq(fsm);
1564e514f105SLee Jones 
1565e514f105SLee Jones 	stfsm_clear_fifo(fsm);
1566e514f105SLee Jones 
1567e514f105SLee Jones 	/* Exit 32-bit address mode, if required */
1568e514f105SLee Jones 	if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
1569e514f105SLee Jones 		stfsm_enter_32bit_addr(fsm, 0);
1570e514f105SLee Jones 
1571e514f105SLee Jones 	return 0;
1572e514f105SLee Jones }
1573e514f105SLee Jones 
stfsm_write(struct stfsm * fsm,const uint8_t * buf,uint32_t size,uint32_t offset)15743f9d720aSLee Jones static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
15753f9d720aSLee Jones 		       uint32_t size, uint32_t offset)
1576176b4377SLee Jones {
1577e6b1bb4eSLee Jones 	struct stfsm_seq *seq = &fsm->stfsm_seq_write;
1578176b4377SLee Jones 	uint32_t data_pads;
1579176b4377SLee Jones 	uint32_t write_mask;
1580176b4377SLee Jones 	uint32_t size_ub;
1581176b4377SLee Jones 	uint32_t size_lb;
1582176b4377SLee Jones 	uint32_t size_mop;
1583176b4377SLee Jones 	uint32_t tmp[4];
1584a9b679bfSLee Jones 	uint32_t i;
1585176b4377SLee Jones 	uint32_t page_buf[FLASH_PAGESIZE_32];
1586176b4377SLee Jones 	uint8_t *t = (uint8_t *)&tmp;
1587176b4377SLee Jones 	const uint8_t *p;
1588176b4377SLee Jones 	int ret;
1589176b4377SLee Jones 
1590176b4377SLee Jones 	dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
1591176b4377SLee Jones 
1592176b4377SLee Jones 	/* Enter 32-bit address mode, if required */
1593176b4377SLee Jones 	if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1594176b4377SLee Jones 		stfsm_enter_32bit_addr(fsm, 1);
1595176b4377SLee Jones 
1596176b4377SLee Jones 	/* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
1597176b4377SLee Jones 	data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
1598176b4377SLee Jones 	write_mask = (data_pads << 2) - 1;
1599176b4377SLee Jones 
1600176b4377SLee Jones 	/* Handle non-aligned buf */
160138e2eee9SBrian Norris 	if ((uintptr_t)buf & 0x3) {
1602176b4377SLee Jones 		memcpy(page_buf, buf, size);
1603176b4377SLee Jones 		p = (uint8_t *)page_buf;
1604176b4377SLee Jones 	} else {
1605176b4377SLee Jones 		p = buf;
1606176b4377SLee Jones 	}
1607176b4377SLee Jones 
1608176b4377SLee Jones 	/* Handle non-aligned size */
1609176b4377SLee Jones 	size_ub = (size + write_mask) & ~write_mask;
1610176b4377SLee Jones 	size_lb = size & ~write_mask;
1611176b4377SLee Jones 	size_mop = size & write_mask;
1612176b4377SLee Jones 
1613176b4377SLee Jones 	seq->data_size = TRANSFER_SIZE(size_ub);
1614176b4377SLee Jones 	seq->addr1 = (offset >> 16) & 0xffff;
1615176b4377SLee Jones 	seq->addr2 = offset & 0xffff;
1616176b4377SLee Jones 
1617176b4377SLee Jones 	/* Need to set FIFO to write mode, before writing data to FIFO (see
1618176b4377SLee Jones 	 * GNBvb79594)
1619176b4377SLee Jones 	 */
1620176b4377SLee Jones 	writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
1621176b4377SLee Jones 
1622176b4377SLee Jones 	/*
1623176b4377SLee Jones 	 * Before writing data to the FIFO, apply a small delay to allow a
1624176b4377SLee Jones 	 * potential change of FIFO direction to complete.
1625176b4377SLee Jones 	 */
1626176b4377SLee Jones 	if (fsm->fifo_dir_delay == 0)
1627176b4377SLee Jones 		readl(fsm->base + SPI_FAST_SEQ_CFG);
1628176b4377SLee Jones 	else
1629176b4377SLee Jones 		udelay(fsm->fifo_dir_delay);
1630176b4377SLee Jones 
1631176b4377SLee Jones 
1632176b4377SLee Jones 	/* Write data to FIFO, before starting sequence (see GNBvd79593) */
1633176b4377SLee Jones 	if (size_lb) {
1634176b4377SLee Jones 		stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
1635176b4377SLee Jones 		p += size_lb;
1636176b4377SLee Jones 	}
1637176b4377SLee Jones 
1638176b4377SLee Jones 	/* Handle non-aligned size */
1639176b4377SLee Jones 	if (size_mop) {
1640176b4377SLee Jones 		memset(t, 0xff, write_mask + 1);	/* fill with 0xff's */
1641176b4377SLee Jones 		for (i = 0; i < size_mop; i++)
1642176b4377SLee Jones 			t[i] = *p++;
1643176b4377SLee Jones 
1644176b4377SLee Jones 		stfsm_write_fifo(fsm, tmp, write_mask + 1);
1645176b4377SLee Jones 	}
1646176b4377SLee Jones 
1647176b4377SLee Jones 	/* Start sequence */
1648176b4377SLee Jones 	stfsm_load_seq(fsm, seq);
1649176b4377SLee Jones 
1650176b4377SLee Jones 	/* Wait for sequence to finish */
1651176b4377SLee Jones 	stfsm_wait_seq(fsm);
1652176b4377SLee Jones 
1653176b4377SLee Jones 	/* Wait for completion */
1654176b4377SLee Jones 	ret = stfsm_wait_busy(fsm);
16555343a123SLee Jones 	if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
16565343a123SLee Jones 		stfsm_s25fl_clear_status_reg(fsm);
1657176b4377SLee Jones 
1658176b4377SLee Jones 	/* Exit 32-bit address mode, if required */
1659009e7e61SAngus Clark 	if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
1660176b4377SLee Jones 		stfsm_enter_32bit_addr(fsm, 0);
1661176b4377SLee Jones 
1662176b4377SLee Jones 	return 0;
1663176b4377SLee Jones }
1664176b4377SLee Jones 
1665e514f105SLee Jones /*
1666e514f105SLee Jones  * Read an address range from the flash chip. The address range
1667e514f105SLee Jones  * may be any size provided it is within the physical boundaries.
1668e514f105SLee Jones  */
stfsm_mtd_read(struct mtd_info * mtd,loff_t from,size_t len,size_t * retlen,u_char * buf)1669e514f105SLee Jones static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
1670e514f105SLee Jones 			  size_t *retlen, u_char *buf)
1671e514f105SLee Jones {
1672e514f105SLee Jones 	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1673e514f105SLee Jones 	uint32_t bytes;
1674e514f105SLee Jones 
1675e514f105SLee Jones 	dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
1676e514f105SLee Jones 		__func__, (u32)from, len);
1677e514f105SLee Jones 
1678e514f105SLee Jones 	mutex_lock(&fsm->lock);
1679e514f105SLee Jones 
1680e514f105SLee Jones 	while (len > 0) {
1681e514f105SLee Jones 		bytes = min_t(size_t, len, FLASH_PAGESIZE);
1682e514f105SLee Jones 
1683e514f105SLee Jones 		stfsm_read(fsm, buf, bytes, from);
1684e514f105SLee Jones 
1685e514f105SLee Jones 		buf += bytes;
1686e514f105SLee Jones 		from += bytes;
1687e514f105SLee Jones 		len -= bytes;
1688e514f105SLee Jones 
1689e514f105SLee Jones 		*retlen += bytes;
1690e514f105SLee Jones 	}
1691e514f105SLee Jones 
1692e514f105SLee Jones 	mutex_unlock(&fsm->lock);
1693e514f105SLee Jones 
1694e514f105SLee Jones 	return 0;
1695e514f105SLee Jones }
1696e514f105SLee Jones 
stfsm_erase_sector(struct stfsm * fsm,uint32_t offset)16973f9d720aSLee Jones static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
16984a341fe7SLee Jones {
16994a341fe7SLee Jones 	struct stfsm_seq *seq = &stfsm_seq_erase_sector;
17004a341fe7SLee Jones 	int ret;
17014a341fe7SLee Jones 
17024a341fe7SLee Jones 	dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
17034a341fe7SLee Jones 
17044a341fe7SLee Jones 	/* Enter 32-bit address mode, if required */
17054a341fe7SLee Jones 	if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
17064a341fe7SLee Jones 		stfsm_enter_32bit_addr(fsm, 1);
17074a341fe7SLee Jones 
17084a341fe7SLee Jones 	seq->addr1 = (offset >> 16) & 0xffff;
17094a341fe7SLee Jones 	seq->addr2 = offset & 0xffff;
17104a341fe7SLee Jones 
17114a341fe7SLee Jones 	stfsm_load_seq(fsm, seq);
17124a341fe7SLee Jones 
17134a341fe7SLee Jones 	stfsm_wait_seq(fsm);
17144a341fe7SLee Jones 
17154a341fe7SLee Jones 	/* Wait for completion */
17164a341fe7SLee Jones 	ret = stfsm_wait_busy(fsm);
17175343a123SLee Jones 	if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
17185343a123SLee Jones 		stfsm_s25fl_clear_status_reg(fsm);
17194a341fe7SLee Jones 
17204a341fe7SLee Jones 	/* Exit 32-bit address mode, if required */
17214a341fe7SLee Jones 	if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
17224a341fe7SLee Jones 		stfsm_enter_32bit_addr(fsm, 0);
17234a341fe7SLee Jones 
17244a341fe7SLee Jones 	return ret;
17254a341fe7SLee Jones }
17264a341fe7SLee Jones 
stfsm_erase_chip(struct stfsm * fsm)17274a341fe7SLee Jones static int stfsm_erase_chip(struct stfsm *fsm)
17284a341fe7SLee Jones {
17294a341fe7SLee Jones 	const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
17304a341fe7SLee Jones 
17314a341fe7SLee Jones 	dev_dbg(fsm->dev, "erasing chip\n");
17324a341fe7SLee Jones 
17334a341fe7SLee Jones 	stfsm_load_seq(fsm, seq);
17344a341fe7SLee Jones 
17354a341fe7SLee Jones 	stfsm_wait_seq(fsm);
17364a341fe7SLee Jones 
17374a341fe7SLee Jones 	return stfsm_wait_busy(fsm);
17384a341fe7SLee Jones }
17394a341fe7SLee Jones 
1740176b4377SLee Jones /*
1741176b4377SLee Jones  * Write an address range to the flash chip.  Data must be written in
1742176b4377SLee Jones  * FLASH_PAGESIZE chunks.  The address range may be any size provided
1743176b4377SLee Jones  * it is within the physical boundaries.
1744176b4377SLee Jones  */
stfsm_mtd_write(struct mtd_info * mtd,loff_t to,size_t len,size_t * retlen,const u_char * buf)1745176b4377SLee Jones static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
1746176b4377SLee Jones 			   size_t *retlen, const u_char *buf)
1747176b4377SLee Jones {
1748176b4377SLee Jones 	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
1749176b4377SLee Jones 
1750176b4377SLee Jones 	u32 page_offs;
1751176b4377SLee Jones 	u32 bytes;
1752176b4377SLee Jones 	uint8_t *b = (uint8_t *)buf;
1753176b4377SLee Jones 	int ret = 0;
1754176b4377SLee Jones 
1755176b4377SLee Jones 	dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
1756176b4377SLee Jones 
1757176b4377SLee Jones 	/* Offset within page */
1758176b4377SLee Jones 	page_offs = to % FLASH_PAGESIZE;
1759176b4377SLee Jones 
1760176b4377SLee Jones 	mutex_lock(&fsm->lock);
1761176b4377SLee Jones 
1762176b4377SLee Jones 	while (len) {
1763176b4377SLee Jones 		/* Write up to page boundary */
176438e2eee9SBrian Norris 		bytes = min_t(size_t, FLASH_PAGESIZE - page_offs, len);
1765176b4377SLee Jones 
1766176b4377SLee Jones 		ret = stfsm_write(fsm, b, bytes, to);
1767176b4377SLee Jones 		if (ret)
1768176b4377SLee Jones 			goto out1;
1769176b4377SLee Jones 
1770176b4377SLee Jones 		b += bytes;
1771176b4377SLee Jones 		len -= bytes;
1772176b4377SLee Jones 		to += bytes;
1773176b4377SLee Jones 
1774176b4377SLee Jones 		/* We are now page-aligned */
1775176b4377SLee Jones 		page_offs = 0;
1776176b4377SLee Jones 
1777176b4377SLee Jones 		*retlen += bytes;
1778176b4377SLee Jones 
1779176b4377SLee Jones 	}
1780176b4377SLee Jones 
1781176b4377SLee Jones out1:
1782176b4377SLee Jones 	mutex_unlock(&fsm->lock);
1783176b4377SLee Jones 
1784176b4377SLee Jones 	return ret;
1785176b4377SLee Jones }
1786176b4377SLee Jones 
17874a341fe7SLee Jones /*
17884a341fe7SLee Jones  * Erase an address range on the flash chip. The address range may extend
17894a341fe7SLee Jones  * one or more erase sectors.  Return an error is there is a problem erasing.
17904a341fe7SLee Jones  */
stfsm_mtd_erase(struct mtd_info * mtd,struct erase_info * instr)17914a341fe7SLee Jones static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
17924a341fe7SLee Jones {
17934a341fe7SLee Jones 	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
17944a341fe7SLee Jones 	u32 addr, len;
17954a341fe7SLee Jones 	int ret;
17964a341fe7SLee Jones 
17974a341fe7SLee Jones 	dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
17984a341fe7SLee Jones 		(long long)instr->addr, (long long)instr->len);
17994a341fe7SLee Jones 
18004a341fe7SLee Jones 	addr = instr->addr;
18014a341fe7SLee Jones 	len = instr->len;
18024a341fe7SLee Jones 
18034a341fe7SLee Jones 	mutex_lock(&fsm->lock);
18044a341fe7SLee Jones 
18054a341fe7SLee Jones 	/* Whole-chip erase? */
18064a341fe7SLee Jones 	if (len == mtd->size) {
18074a341fe7SLee Jones 		ret = stfsm_erase_chip(fsm);
18084a341fe7SLee Jones 		if (ret)
18094a341fe7SLee Jones 			goto out1;
18104a341fe7SLee Jones 	} else {
18114a341fe7SLee Jones 		while (len) {
18124a341fe7SLee Jones 			ret = stfsm_erase_sector(fsm, addr);
18134a341fe7SLee Jones 			if (ret)
18144a341fe7SLee Jones 				goto out1;
18154a341fe7SLee Jones 
18164a341fe7SLee Jones 			addr += mtd->erasesize;
18174a341fe7SLee Jones 			len -= mtd->erasesize;
18184a341fe7SLee Jones 		}
18194a341fe7SLee Jones 	}
18204a341fe7SLee Jones 
18214a341fe7SLee Jones 	mutex_unlock(&fsm->lock);
18224a341fe7SLee Jones 
18234a341fe7SLee Jones 	return 0;
18244a341fe7SLee Jones 
18254a341fe7SLee Jones out1:
18264a341fe7SLee Jones 	mutex_unlock(&fsm->lock);
18274a341fe7SLee Jones 
18284a341fe7SLee Jones 	return ret;
18294a341fe7SLee Jones }
18304a341fe7SLee Jones 
stfsm_read_jedec(struct stfsm * fsm,uint8_t * jedec)18313f9d720aSLee Jones static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
18321bd512b5SLee Jones {
18331bd512b5SLee Jones 	const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
18341bd512b5SLee Jones 	uint32_t tmp[2];
18351bd512b5SLee Jones 
18361bd512b5SLee Jones 	stfsm_load_seq(fsm, seq);
18371bd512b5SLee Jones 
18381bd512b5SLee Jones 	stfsm_read_fifo(fsm, tmp, 8);
18391bd512b5SLee Jones 
18401bd512b5SLee Jones 	memcpy(jedec, tmp, 5);
18411bd512b5SLee Jones 
18421bd512b5SLee Jones 	stfsm_wait_seq(fsm);
18431bd512b5SLee Jones }
18441bd512b5SLee Jones 
stfsm_jedec_probe(struct stfsm * fsm)18451bd512b5SLee Jones static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
18461bd512b5SLee Jones {
184724fec651SLee Jones 	struct flash_info	*info;
18481bd512b5SLee Jones 	u16                     ext_jedec;
18491bd512b5SLee Jones 	u32			jedec;
18501bd512b5SLee Jones 	u8			id[5];
18511bd512b5SLee Jones 
18521bd512b5SLee Jones 	stfsm_read_jedec(fsm, id);
18531bd512b5SLee Jones 
18541bd512b5SLee Jones 	jedec     = id[0] << 16 | id[1] << 8 | id[2];
18551bd512b5SLee Jones 	/*
18561bd512b5SLee Jones 	 * JEDEC also defines an optional "extended device information"
18571bd512b5SLee Jones 	 * string for after vendor-specific data, after the three bytes
18581bd512b5SLee Jones 	 * we use here. Supporting some chips might require using it.
18591bd512b5SLee Jones 	 */
18601bd512b5SLee Jones 	ext_jedec = id[3] << 8  | id[4];
18611bd512b5SLee Jones 
1862f0a37a8dSAntonio Cardace 	dev_dbg(fsm->dev, "JEDEC =  0x%08x [%5ph]\n", jedec, id);
18631bd512b5SLee Jones 
186424fec651SLee Jones 	for (info = flash_types; info->name; info++) {
186524fec651SLee Jones 		if (info->jedec_id == jedec) {
186624fec651SLee Jones 			if (info->ext_id && info->ext_id != ext_jedec)
186724fec651SLee Jones 				continue;
186824fec651SLee Jones 			return info;
186924fec651SLee Jones 		}
187024fec651SLee Jones 	}
187124fec651SLee Jones 	dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
187224fec651SLee Jones 
18731bd512b5SLee Jones 	return NULL;
18741bd512b5SLee Jones }
18751bd512b5SLee Jones 
stfsm_set_mode(struct stfsm * fsm,uint32_t mode)187686f309fdSLee Jones static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
187786f309fdSLee Jones {
187886f309fdSLee Jones 	int ret, timeout = 10;
187986f309fdSLee Jones 
188086f309fdSLee Jones 	/* Wait for controller to accept mode change */
188186f309fdSLee Jones 	while (--timeout) {
188286f309fdSLee Jones 		ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
188386f309fdSLee Jones 		if (ret & 0x1)
188486f309fdSLee Jones 			break;
188586f309fdSLee Jones 		udelay(1);
188686f309fdSLee Jones 	}
188786f309fdSLee Jones 
188886f309fdSLee Jones 	if (!timeout)
188986f309fdSLee Jones 		return -EBUSY;
189086f309fdSLee Jones 
189186f309fdSLee Jones 	writel(mode, fsm->base + SPI_MODESELECT);
189286f309fdSLee Jones 
189386f309fdSLee Jones 	return 0;
189486f309fdSLee Jones }
189586f309fdSLee Jones 
stfsm_set_freq(struct stfsm * fsm,uint32_t spi_freq)189686f309fdSLee Jones static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
189786f309fdSLee Jones {
189886f309fdSLee Jones 	uint32_t emi_freq;
189986f309fdSLee Jones 	uint32_t clk_div;
190086f309fdSLee Jones 
190169d5af8dSLee Jones 	emi_freq = clk_get_rate(fsm->clk);
190286f309fdSLee Jones 
190386f309fdSLee Jones 	/*
190486f309fdSLee Jones 	 * Calculate clk_div - values between 2 and 128
190586f309fdSLee Jones 	 * Multiple of 2, rounded up
190686f309fdSLee Jones 	 */
190786f309fdSLee Jones 	clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
190886f309fdSLee Jones 	if (clk_div < 2)
190986f309fdSLee Jones 		clk_div = 2;
191086f309fdSLee Jones 	else if (clk_div > 128)
191186f309fdSLee Jones 		clk_div = 128;
191286f309fdSLee Jones 
191386f309fdSLee Jones 	/*
191486f309fdSLee Jones 	 * Determine a suitable delay for the IP to complete a change of
191586f309fdSLee Jones 	 * direction of the FIFO. The required delay is related to the clock
191686f309fdSLee Jones 	 * divider used. The following heuristics are based on empirical tests,
191786f309fdSLee Jones 	 * using a 100MHz EMI clock.
191886f309fdSLee Jones 	 */
191986f309fdSLee Jones 	if (clk_div <= 4)
192086f309fdSLee Jones 		fsm->fifo_dir_delay = 0;
192186f309fdSLee Jones 	else if (clk_div <= 10)
192286f309fdSLee Jones 		fsm->fifo_dir_delay = 1;
192386f309fdSLee Jones 	else
192486f309fdSLee Jones 		fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
192586f309fdSLee Jones 
192686f309fdSLee Jones 	dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
192786f309fdSLee Jones 		emi_freq, spi_freq, clk_div);
192886f309fdSLee Jones 
192986f309fdSLee Jones 	writel(clk_div, fsm->base + SPI_CLOCKDIV);
193086f309fdSLee Jones }
193186f309fdSLee Jones 
stfsm_init(struct stfsm * fsm)193286f309fdSLee Jones static int stfsm_init(struct stfsm *fsm)
193386f309fdSLee Jones {
193486f309fdSLee Jones 	int ret;
193586f309fdSLee Jones 
193686f309fdSLee Jones 	/* Perform a soft reset of the FSM controller */
193786f309fdSLee Jones 	writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
193886f309fdSLee Jones 	udelay(1);
193986f309fdSLee Jones 	writel(0, fsm->base + SPI_FAST_SEQ_CFG);
194086f309fdSLee Jones 
194186f309fdSLee Jones 	/* Set clock to 'safe' frequency initially */
194286f309fdSLee Jones 	stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
194386f309fdSLee Jones 
194486f309fdSLee Jones 	/* Switch to FSM */
194586f309fdSLee Jones 	ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
194686f309fdSLee Jones 	if (ret)
194786f309fdSLee Jones 		return ret;
194886f309fdSLee Jones 
194986f309fdSLee Jones 	/* Set timing parameters */
195086f309fdSLee Jones 	writel(SPI_CFG_DEVICE_ST            |
195186f309fdSLee Jones 	       SPI_CFG_DEFAULT_MIN_CS_HIGH  |
195286f309fdSLee Jones 	       SPI_CFG_DEFAULT_CS_SETUPHOLD |
195386f309fdSLee Jones 	       SPI_CFG_DEFAULT_DATA_HOLD,
195486f309fdSLee Jones 	       fsm->base + SPI_CONFIGDATA);
195586f309fdSLee Jones 	writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
195686f309fdSLee Jones 
1957009e7e61SAngus Clark 	/*
1958009e7e61SAngus Clark 	 * Set the FSM 'WAIT' delay to the minimum workable value.  Note, for
1959009e7e61SAngus Clark 	 * our purposes, the WAIT instruction is used purely to achieve
1960009e7e61SAngus Clark 	 * "sequence validity" rather than actually implement a delay.
1961009e7e61SAngus Clark 	 */
1962009e7e61SAngus Clark 	writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
1963009e7e61SAngus Clark 
196486f309fdSLee Jones 	/* Clear FIFO, just in case */
196586f309fdSLee Jones 	stfsm_clear_fifo(fsm);
196686f309fdSLee Jones 
196786f309fdSLee Jones 	return 0;
196886f309fdSLee Jones }
196986f309fdSLee Jones 
stfsm_fetch_platform_configs(struct platform_device * pdev)1970a63984c1SLee Jones static void stfsm_fetch_platform_configs(struct platform_device *pdev)
1971a63984c1SLee Jones {
1972a63984c1SLee Jones 	struct stfsm *fsm = platform_get_drvdata(pdev);
1973a63984c1SLee Jones 	struct device_node *np = pdev->dev.of_node;
1974a63984c1SLee Jones 	struct regmap *regmap;
1975a63984c1SLee Jones 	uint32_t boot_device_reg;
1976a63984c1SLee Jones 	uint32_t boot_device_spi;
1977a63984c1SLee Jones 	uint32_t boot_device;     /* Value we read from *boot_device_reg */
1978a63984c1SLee Jones 	int ret;
1979a63984c1SLee Jones 
1980a63984c1SLee Jones 	/* Booting from SPI NOR Flash is the default */
1981a63984c1SLee Jones 	fsm->booted_from_spi = true;
1982a63984c1SLee Jones 
1983a63984c1SLee Jones 	regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
1984a63984c1SLee Jones 	if (IS_ERR(regmap))
1985a63984c1SLee Jones 		goto boot_device_fail;
1986a63984c1SLee Jones 
19870ea7d706SLee Jones 	fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
19880ea7d706SLee Jones 
19890ea7d706SLee Jones 	fsm->reset_por = of_property_read_bool(np, "st,reset-por");
19900ea7d706SLee Jones 
1991a63984c1SLee Jones 	/* Where in the syscon the boot device information lives */
1992a63984c1SLee Jones 	ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
1993a63984c1SLee Jones 	if (ret)
1994a63984c1SLee Jones 		goto boot_device_fail;
1995a63984c1SLee Jones 
1996a63984c1SLee Jones 	/* Boot device value when booted from SPI NOR */
1997a63984c1SLee Jones 	ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
1998a63984c1SLee Jones 	if (ret)
1999a63984c1SLee Jones 		goto boot_device_fail;
2000a63984c1SLee Jones 
2001a63984c1SLee Jones 	ret = regmap_read(regmap, boot_device_reg, &boot_device);
2002a63984c1SLee Jones 	if (ret)
2003a63984c1SLee Jones 		goto boot_device_fail;
2004a63984c1SLee Jones 
2005a63984c1SLee Jones 	if (boot_device != boot_device_spi)
2006a63984c1SLee Jones 		fsm->booted_from_spi = false;
2007a63984c1SLee Jones 
2008a63984c1SLee Jones 	return;
2009a63984c1SLee Jones 
2010a63984c1SLee Jones boot_device_fail:
2011a63984c1SLee Jones 	dev_warn(&pdev->dev,
2012a63984c1SLee Jones 		 "failed to fetch boot device, assuming boot from SPI\n");
2013a63984c1SLee Jones }
2014a63984c1SLee Jones 
stfsm_probe(struct platform_device * pdev)2015d90db4a0SLee Jones static int stfsm_probe(struct platform_device *pdev)
2016d90db4a0SLee Jones {
2017d90db4a0SLee Jones 	struct device_node *np = pdev->dev.of_node;
201824fec651SLee Jones 	struct flash_info *info;
2019d90db4a0SLee Jones 	struct stfsm *fsm;
202086f309fdSLee Jones 	int ret;
2021d90db4a0SLee Jones 
2022d90db4a0SLee Jones 	if (!np) {
2023d90db4a0SLee Jones 		dev_err(&pdev->dev, "No DT found\n");
2024d90db4a0SLee Jones 		return -EINVAL;
2025d90db4a0SLee Jones 	}
2026d90db4a0SLee Jones 
2027d90db4a0SLee Jones 	fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
2028d90db4a0SLee Jones 	if (!fsm)
2029d90db4a0SLee Jones 		return -ENOMEM;
2030d90db4a0SLee Jones 
2031d90db4a0SLee Jones 	fsm->dev = &pdev->dev;
2032d90db4a0SLee Jones 
2033d90db4a0SLee Jones 	platform_set_drvdata(pdev, fsm);
2034d90db4a0SLee Jones 
20351726813cSYangtao Li 	fsm->base = devm_platform_ioremap_resource(pdev, 0);
20361726813cSYangtao Li 	if (IS_ERR(fsm->base))
2037d90db4a0SLee Jones 		return PTR_ERR(fsm->base);
2038d90db4a0SLee Jones 
2039a2c2690fSChristophe JAILLET 	fsm->clk = devm_clk_get_enabled(&pdev->dev, NULL);
204069d5af8dSLee Jones 	if (IS_ERR(fsm->clk)) {
204169d5af8dSLee Jones 		dev_err(fsm->dev, "Couldn't find EMI clock.\n");
204269d5af8dSLee Jones 		return PTR_ERR(fsm->clk);
204369d5af8dSLee Jones 	}
204469d5af8dSLee Jones 
2045d90db4a0SLee Jones 	mutex_init(&fsm->lock);
2046d90db4a0SLee Jones 
204786f309fdSLee Jones 	ret = stfsm_init(fsm);
204886f309fdSLee Jones 	if (ret) {
204986f309fdSLee Jones 		dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
2050a2c2690fSChristophe JAILLET 		return ret;
205186f309fdSLee Jones 	}
205286f309fdSLee Jones 
2053a63984c1SLee Jones 	stfsm_fetch_platform_configs(pdev);
2054a63984c1SLee Jones 
20551bd512b5SLee Jones 	/* Detect SPI FLASH device */
205624fec651SLee Jones 	info = stfsm_jedec_probe(fsm);
2057a2c2690fSChristophe JAILLET 	if (!info)
2058a2c2690fSChristophe JAILLET 		return -ENODEV;
205924fec651SLee Jones 	fsm->info = info;
20601bd512b5SLee Jones 
20613b5d1981SLee Jones 	/* Use device size to determine address width */
20623b5d1981SLee Jones 	if (info->sector_size * info->n_sectors > 0x1000000)
20633b5d1981SLee Jones 		info->flags |= FLASH_FLAG_32BIT_ADDR;
20643b5d1981SLee Jones 
2065218b870fSLee Jones 	/*
2066218b870fSLee Jones 	 * Configure READ/WRITE/ERASE sequences according to platform and
2067218b870fSLee Jones 	 * device flags.
2068218b870fSLee Jones 	 */
20696f6536a0SUwe Kleine-König 	if (info->config)
2070218b870fSLee Jones 		ret = info->config(fsm);
20716f6536a0SUwe Kleine-König 	else
20724eb3f0d8SLee Jones 		ret = stfsm_prepare_rwe_seqs_default(fsm);
20734eb3f0d8SLee Jones 	if (ret)
2074a2c2690fSChristophe JAILLET 		return ret;
2075218b870fSLee Jones 
2076221cff13SLee Jones 	fsm->mtd.name		= info->name;
2077d90db4a0SLee Jones 	fsm->mtd.dev.parent	= &pdev->dev;
2078004b5e60SBrian Norris 	mtd_set_of_node(&fsm->mtd, np);
2079d90db4a0SLee Jones 	fsm->mtd.type		= MTD_NORFLASH;
2080d90db4a0SLee Jones 	fsm->mtd.writesize	= 4;
2081d90db4a0SLee Jones 	fsm->mtd.writebufsize	= fsm->mtd.writesize;
2082d90db4a0SLee Jones 	fsm->mtd.flags		= MTD_CAP_NORFLASH;
208324fec651SLee Jones 	fsm->mtd.size		= info->sector_size * info->n_sectors;
208424fec651SLee Jones 	fsm->mtd.erasesize	= info->sector_size;
208524fec651SLee Jones 
2086e514f105SLee Jones 	fsm->mtd._read  = stfsm_mtd_read;
2087176b4377SLee Jones 	fsm->mtd._write = stfsm_mtd_write;
20884a341fe7SLee Jones 	fsm->mtd._erase = stfsm_mtd_erase;
2089e514f105SLee Jones 
20904a341fe7SLee Jones 	dev_info(&pdev->dev,
209124fec651SLee Jones 		"Found serial flash device: %s\n"
209224fec651SLee Jones 		" size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
209324fec651SLee Jones 		info->name,
209424fec651SLee Jones 		(long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
209524fec651SLee Jones 		fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
2096d90db4a0SLee Jones 
2097a2c2690fSChristophe JAILLET 	return mtd_device_register(&fsm->mtd, NULL, 0);
2098d90db4a0SLee Jones }
2099d90db4a0SLee Jones 
stfsm_remove(struct platform_device * pdev)2100*326563fdSUwe Kleine-König static void stfsm_remove(struct platform_device *pdev)
2101d90db4a0SLee Jones {
2102d90db4a0SLee Jones 	struct stfsm *fsm = platform_get_drvdata(pdev);
2103d90db4a0SLee Jones 
2104cfa7847fSUwe Kleine-König 	WARN_ON(mtd_device_unregister(&fsm->mtd));
2105d90db4a0SLee Jones }
2106d90db4a0SLee Jones 
210769d5af8dSLee Jones #ifdef CONFIG_PM_SLEEP
stfsmfsm_suspend(struct device * dev)210869d5af8dSLee Jones static int stfsmfsm_suspend(struct device *dev)
210969d5af8dSLee Jones {
211069d5af8dSLee Jones 	struct stfsm *fsm = dev_get_drvdata(dev);
211169d5af8dSLee Jones 
211269d5af8dSLee Jones 	clk_disable_unprepare(fsm->clk);
211369d5af8dSLee Jones 
211469d5af8dSLee Jones 	return 0;
211569d5af8dSLee Jones }
211669d5af8dSLee Jones 
stfsmfsm_resume(struct device * dev)211769d5af8dSLee Jones static int stfsmfsm_resume(struct device *dev)
211869d5af8dSLee Jones {
211969d5af8dSLee Jones 	struct stfsm *fsm = dev_get_drvdata(dev);
212069d5af8dSLee Jones 
2121481815a6SArvind Yadav 	return clk_prepare_enable(fsm->clk);
212269d5af8dSLee Jones }
212369d5af8dSLee Jones #endif
212469d5af8dSLee Jones 
212569d5af8dSLee Jones static SIMPLE_DEV_PM_OPS(stfsm_pm_ops, stfsmfsm_suspend, stfsmfsm_resume);
212669d5af8dSLee Jones 
2127aeea6eb4SJingoo Han static const struct of_device_id stfsm_match[] = {
2128d90db4a0SLee Jones 	{ .compatible = "st,spi-fsm", },
2129d90db4a0SLee Jones 	{},
2130d90db4a0SLee Jones };
2131d90db4a0SLee Jones MODULE_DEVICE_TABLE(of, stfsm_match);
2132d90db4a0SLee Jones 
2133d90db4a0SLee Jones static struct platform_driver stfsm_driver = {
2134d90db4a0SLee Jones 	.probe		= stfsm_probe,
2135*326563fdSUwe Kleine-König 	.remove_new	= stfsm_remove,
2136d90db4a0SLee Jones 	.driver		= {
2137d90db4a0SLee Jones 		.name	= "st-spi-fsm",
2138d90db4a0SLee Jones 		.of_match_table = stfsm_match,
213969d5af8dSLee Jones 		.pm     = &stfsm_pm_ops,
2140d90db4a0SLee Jones 	},
2141d90db4a0SLee Jones };
2142d90db4a0SLee Jones module_platform_driver(stfsm_driver);
2143d90db4a0SLee Jones 
2144d90db4a0SLee Jones MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
2145d90db4a0SLee Jones MODULE_DESCRIPTION("ST SPI FSM driver");
2146d90db4a0SLee Jones MODULE_LICENSE("GPL");
2147