Blame view

drivers/mtd/devices/st_spi_fsm.c 55 KB
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1
2
3
4
5
  /*
   * st_spi_fsm.c	- ST Fast Sequence Mode (FSM) Serial Flash Controller
   *
   * Author: Angus Clark <angus.clark@st.com>
   *
f1919cb85   Lee Jones   mtd: st_spi_fsm: ...
6
   * Copyright (C) 2010-2014 STMicroelectronics Limited
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
7
8
9
10
11
12
13
14
15
16
   *
   * JEDEC probe based on drivers/mtd/devices/m25p80.c
   *
   * This code is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   */
  #include <linux/kernel.h>
  #include <linux/module.h>
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
17
  #include <linux/regmap.h>
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
18
  #include <linux/platform_device.h>
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
19
  #include <linux/mfd/syscon.h>
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
20
  #include <linux/mtd/mtd.h>
221cff13c   Lee Jones   mtd: st_spi_fsm: ...
21
  #include <linux/mtd/partitions.h>
6c8e1b33a   Brian Norris   mtd: st_spi_fsm: ...
22
  #include <linux/mtd/spi-nor.h>
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
23
24
25
26
  #include <linux/sched.h>
  #include <linux/delay.h>
  #include <linux/io.h>
  #include <linux/of.h>
5549fbd51   Lee Jones   mtd: devices: Pro...
27
  #include "serial_flash_cmds.h"
bc09fb575   Lee Jones   mtd: st_spi_fsm: ...
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  /*
   * FSM SPI Controller Registers
   */
  #define SPI_CLOCKDIV			0x0010
  #define SPI_MODESELECT			0x0018
  #define SPI_CONFIGDATA			0x0020
  #define SPI_STA_MODE_CHANGE		0x0028
  #define SPI_FAST_SEQ_TRANSFER_SIZE	0x0100
  #define SPI_FAST_SEQ_ADD1		0x0104
  #define SPI_FAST_SEQ_ADD2		0x0108
  #define SPI_FAST_SEQ_ADD_CFG		0x010c
  #define SPI_FAST_SEQ_OPC1		0x0110
  #define SPI_FAST_SEQ_OPC2		0x0114
  #define SPI_FAST_SEQ_OPC3		0x0118
  #define SPI_FAST_SEQ_OPC4		0x011c
  #define SPI_FAST_SEQ_OPC5		0x0120
  #define SPI_MODE_BITS			0x0124
  #define SPI_DUMMY_BITS			0x0128
  #define SPI_FAST_SEQ_FLASH_STA_DATA	0x012c
  #define SPI_FAST_SEQ_1			0x0130
  #define SPI_FAST_SEQ_2			0x0134
  #define SPI_FAST_SEQ_3			0x0138
  #define SPI_FAST_SEQ_4			0x013c
  #define SPI_FAST_SEQ_CFG		0x0140
  #define SPI_FAST_SEQ_STA		0x0144
  #define SPI_QUAD_BOOT_SEQ_INIT_1	0x0148
  #define SPI_QUAD_BOOT_SEQ_INIT_2	0x014c
  #define SPI_QUAD_BOOT_READ_SEQ_1	0x0150
  #define SPI_QUAD_BOOT_READ_SEQ_2	0x0154
  #define SPI_PROGRAM_ERASE_TIME		0x0158
  #define SPI_MULT_PAGE_REPEAT_SEQ_1	0x015c
  #define SPI_MULT_PAGE_REPEAT_SEQ_2	0x0160
  #define SPI_STATUS_WR_TIME_REG		0x0164
  #define SPI_FAST_SEQ_DATA_REG		0x0300
  
  /*
   * Register: SPI_MODESELECT
   */
  #define SPI_MODESELECT_CONTIG		0x01
  #define SPI_MODESELECT_FASTREAD		0x02
  #define SPI_MODESELECT_DUALIO		0x04
  #define SPI_MODESELECT_FSM		0x08
  #define SPI_MODESELECT_QUADBOOT		0x10
  
  /*
   * Register: SPI_CONFIGDATA
   */
  #define SPI_CFG_DEVICE_ST		0x1
  #define SPI_CFG_DEVICE_ATMEL		0x4
  #define SPI_CFG_MIN_CS_HIGH(x)		(((x) & 0xfff) << 4)
  #define SPI_CFG_CS_SETUPHOLD(x)		(((x) & 0xff) << 16)
  #define SPI_CFG_DATA_HOLD(x)		(((x) & 0xff) << 24)
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
80
81
82
  #define SPI_CFG_DEFAULT_MIN_CS_HIGH    SPI_CFG_MIN_CS_HIGH(0x0AA)
  #define SPI_CFG_DEFAULT_CS_SETUPHOLD   SPI_CFG_CS_SETUPHOLD(0xA0)
  #define SPI_CFG_DEFAULT_DATA_HOLD      SPI_CFG_DATA_HOLD(0x00)
bc09fb575   Lee Jones   mtd: st_spi_fsm: ...
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  /*
   * Register: SPI_FAST_SEQ_TRANSFER_SIZE
   */
  #define TRANSFER_SIZE(x)		((x) * 8)
  
  /*
   * Register: SPI_FAST_SEQ_ADD_CFG
   */
  #define ADR_CFG_CYCLES_ADD1(x)		((x) << 0)
  #define ADR_CFG_PADS_1_ADD1		(0x0 << 6)
  #define ADR_CFG_PADS_2_ADD1		(0x1 << 6)
  #define ADR_CFG_PADS_4_ADD1		(0x3 << 6)
  #define ADR_CFG_CSDEASSERT_ADD1		(1   << 8)
  #define ADR_CFG_CYCLES_ADD2(x)		((x) << (0+16))
  #define ADR_CFG_PADS_1_ADD2		(0x0 << (6+16))
  #define ADR_CFG_PADS_2_ADD2		(0x1 << (6+16))
  #define ADR_CFG_PADS_4_ADD2		(0x3 << (6+16))
  #define ADR_CFG_CSDEASSERT_ADD2		(1   << (8+16))
  
  /*
   * Register: SPI_FAST_SEQ_n
   */
  #define SEQ_OPC_OPCODE(x)		((x) << 0)
  #define SEQ_OPC_CYCLES(x)		((x) << 8)
  #define SEQ_OPC_PADS_1			(0x0 << 14)
  #define SEQ_OPC_PADS_2			(0x1 << 14)
  #define SEQ_OPC_PADS_4			(0x3 << 14)
  #define SEQ_OPC_CSDEASSERT		(1   << 16)
  
  /*
   * Register: SPI_FAST_SEQ_CFG
   */
  #define SEQ_CFG_STARTSEQ		(1 << 0)
  #define SEQ_CFG_SWRESET			(1 << 5)
  #define SEQ_CFG_CSDEASSERT		(1 << 6)
  #define SEQ_CFG_READNOTWRITE		(1 << 7)
  #define SEQ_CFG_ERASE			(1 << 8)
  #define SEQ_CFG_PADS_1			(0x0 << 16)
  #define SEQ_CFG_PADS_2			(0x1 << 16)
  #define SEQ_CFG_PADS_4			(0x3 << 16)
  
  /*
   * Register: SPI_MODE_BITS
   */
  #define MODE_DATA(x)			(x & 0xff)
  #define MODE_CYCLES(x)			((x & 0x3f) << 16)
  #define MODE_PADS_1			(0x0 << 22)
  #define MODE_PADS_2			(0x1 << 22)
  #define MODE_PADS_4			(0x3 << 22)
  #define DUMMY_CSDEASSERT		(1   << 24)
  
  /*
   * Register: SPI_DUMMY_BITS
   */
  #define DUMMY_CYCLES(x)			((x & 0x3f) << 16)
  #define DUMMY_PADS_1			(0x0 << 22)
  #define DUMMY_PADS_2			(0x1 << 22)
  #define DUMMY_PADS_4			(0x3 << 22)
  #define DUMMY_CSDEASSERT		(1   << 24)
  
  /*
   * Register: SPI_FAST_SEQ_FLASH_STA_DATA
   */
  #define STA_DATA_BYTE1(x)		((x & 0xff) << 0)
  #define STA_DATA_BYTE2(x)		((x & 0xff) << 8)
  #define STA_PADS_1			(0x0 << 16)
  #define STA_PADS_2			(0x1 << 16)
  #define STA_PADS_4			(0x3 << 16)
  #define STA_CSDEASSERT			(0x1 << 20)
  #define STA_RDNOTWR			(0x1 << 21)
  
  /*
   * FSM SPI Instruction Opcodes
   */
  #define STFSM_OPC_CMD			0x1
  #define STFSM_OPC_ADD			0x2
  #define STFSM_OPC_STA			0x3
  #define STFSM_OPC_MODE			0x4
  #define STFSM_OPC_DUMMY		0x5
  #define STFSM_OPC_DATA			0x6
  #define STFSM_OPC_WAIT			0x7
  #define STFSM_OPC_JUMP			0x8
  #define STFSM_OPC_GOTO			0x9
  #define STFSM_OPC_STOP			0xF
  
  /*
   * FSM SPI Instructions (== opcode + operand).
   */
  #define STFSM_INSTR(cmd, op)		((cmd) | ((op) << 4))
  
  #define STFSM_INST_CMD1			STFSM_INSTR(STFSM_OPC_CMD,	1)
  #define STFSM_INST_CMD2			STFSM_INSTR(STFSM_OPC_CMD,	2)
  #define STFSM_INST_CMD3			STFSM_INSTR(STFSM_OPC_CMD,	3)
  #define STFSM_INST_CMD4			STFSM_INSTR(STFSM_OPC_CMD,	4)
  #define STFSM_INST_CMD5			STFSM_INSTR(STFSM_OPC_CMD,	5)
  #define STFSM_INST_ADD1			STFSM_INSTR(STFSM_OPC_ADD,	1)
  #define STFSM_INST_ADD2			STFSM_INSTR(STFSM_OPC_ADD,	2)
  
  #define STFSM_INST_DATA_WRITE		STFSM_INSTR(STFSM_OPC_DATA,	1)
  #define STFSM_INST_DATA_READ		STFSM_INSTR(STFSM_OPC_DATA,	2)
  
  #define STFSM_INST_STA_RD1		STFSM_INSTR(STFSM_OPC_STA,	0x1)
  #define STFSM_INST_STA_WR1		STFSM_INSTR(STFSM_OPC_STA,	0x1)
  #define STFSM_INST_STA_RD2		STFSM_INSTR(STFSM_OPC_STA,	0x2)
  #define STFSM_INST_STA_WR1_2		STFSM_INSTR(STFSM_OPC_STA,	0x3)
  
  #define STFSM_INST_MODE			STFSM_INSTR(STFSM_OPC_MODE,	0)
  #define STFSM_INST_DUMMY		STFSM_INSTR(STFSM_OPC_DUMMY,	0)
  #define STFSM_INST_WAIT			STFSM_INSTR(STFSM_OPC_WAIT,	0)
  #define STFSM_INST_STOP			STFSM_INSTR(STFSM_OPC_STOP,	0)
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
193
194
195
196
  #define STFSM_DEFAULT_EMI_FREQ 100000000UL                        /* 100 MHz */
  #define STFSM_DEFAULT_WR_TIME  (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
  
  #define STFSM_FLASH_SAFE_FREQ  10000000UL                         /* 10 MHz */
3c8b85b34   Lee Jones   mtd: st_spi_fsm: ...
197
  #define STFSM_MAX_WAIT_SEQ_MS  1000     /* FSM execution time */
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
198
199
200
201
202
203
204
  /* S25FLxxxS commands */
  #define S25FL_CMD_WRITE4_1_1_4 0x34
  #define S25FL_CMD_SE4          0xdc
  #define S25FL_CMD_CLSR         0x30
  #define S25FL_CMD_DYBWR                0xe1
  #define S25FL_CMD_DYBRD                0xe0
  #define S25FL_CMD_WRITE4       0x12    /* Note, opcode clashes with
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
205
  					* 'SPINOR_OP_WRITE_1_4_4'
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
206
  					* as found on N25Qxxx devices! */
176b43776   Lee Jones   mtd: st_spi_fsm: ...
207
208
209
210
211
212
213
214
  /* Status register */
  #define FLASH_STATUS_BUSY      0x01
  #define FLASH_STATUS_WEL       0x02
  #define FLASH_STATUS_BP0       0x04
  #define FLASH_STATUS_BP1       0x08
  #define FLASH_STATUS_BP2       0x10
  #define FLASH_STATUS_SRWP0     0x80
  #define FLASH_STATUS_TIMEOUT   0xff
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
215
216
217
  /* S25FL Error Flags */
  #define S25FL_STATUS_E_ERR     0x20
  #define S25FL_STATUS_P_ERR     0x40
176b43776   Lee Jones   mtd: st_spi_fsm: ...
218

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
219
220
221
222
223
  #define N25Q_CMD_WRVCR         0x81
  #define N25Q_CMD_RDVCR         0x85
  #define N25Q_CMD_RDVECR        0x65
  #define N25Q_CMD_RDNVCR        0xb5
  #define N25Q_CMD_WRNVCR        0xb1
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
224
225
  #define FLASH_PAGESIZE         256			/* In Bytes    */
  #define FLASH_PAGESIZE_32      (FLASH_PAGESIZE / 4)	/* In uint32_t */
176b43776   Lee Jones   mtd: st_spi_fsm: ...
226
  #define FLASH_MAX_BUSY_WAIT    (300 * HZ)	/* Maximum 'CHIPERASE' time */
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
227

e85a61967   Lee Jones   mtd: st_spi_fsm: ...
228
229
230
231
232
  /*
   * Flags to tweak operation of default read/write/erase routines
   */
  #define CFG_READ_TOGGLE_32BIT_ADDR     0x00000001
  #define CFG_WRITE_TOGGLE_32BIT_ADDR    0x00000002
e85a61967   Lee Jones   mtd: st_spi_fsm: ...
233
234
  #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
  #define CFG_S25FL_CHECK_ERROR_FLAGS    0x00000010
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
235
236
237
238
239
240
241
242
243
244
245
246
  struct stfsm_seq {
  	uint32_t data_size;
  	uint32_t addr1;
  	uint32_t addr2;
  	uint32_t addr_cfg;
  	uint32_t seq_opc[5];
  	uint32_t mode;
  	uint32_t dummy;
  	uint32_t status;
  	uint8_t  seq[16];
  	uint32_t seq_cfg;
  } __packed __aligned(4);
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
247
248
249
250
251
252
  struct stfsm {
  	struct device		*dev;
  	void __iomem		*base;
  	struct resource		*region;
  	struct mtd_info		mtd;
  	struct mutex		lock;
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
253
  	struct flash_info       *info;
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
254

e85a61967   Lee Jones   mtd: st_spi_fsm: ...
255
  	uint32_t                configuration;
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
256
  	uint32_t                fifo_dir_delay;
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
257
  	bool                    booted_from_spi;
0ea7d7069   Lee Jones   mtd: st_spi_fsm: ...
258
259
  	bool                    reset_signal;
  	bool                    reset_por;
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
260

e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
261
262
263
264
  	struct stfsm_seq stfsm_seq_read;
  	struct stfsm_seq stfsm_seq_write;
  	struct stfsm_seq stfsm_seq_en_32bit_addr;
  };
3c8b85b34   Lee Jones   mtd: st_spi_fsm: ...
265

089812740   Lee Jones   mtd: st_spi_fsm: ...
266
267
268
269
270
271
272
273
274
275
276
  /* Parameters to configure a READ or WRITE FSM sequence */
  struct seq_rw_config {
  	uint32_t        flags;          /* flags to support config */
  	uint8_t         cmd;            /* FLASH command */
  	int             write;          /* Write Sequence */
  	uint8_t         addr_pads;      /* No. of addr pads (MODE & DUMMY) */
  	uint8_t         data_pads;      /* No. of data pads */
  	uint8_t         mode_data;      /* MODE data */
  	uint8_t         mode_cycles;    /* No. of MODE cycles */
  	uint8_t         dummy_cycles;   /* No. of DUMMY cycles */
  };
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
277
278
279
280
281
282
283
284
285
286
287
  /* SPI Flash Device Table */
  struct flash_info {
  	char            *name;
  	/*
  	 * JEDEC id zero means "no ID" (most older chips); otherwise it has
  	 * a high byte of zero plus three data bytes: the manufacturer id,
  	 * then a two byte device id.
  	 */
  	u32             jedec_id;
  	u16             ext_id;
  	/*
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
288
  	 * The size listed here is what works with SPINOR_OP_SE, which isn't
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
289
290
291
292
293
294
295
296
297
298
299
300
  	 * necessarily called a "sector" by the vendor.
  	 */
  	unsigned        sector_size;
  	u16             n_sectors;
  	u32             flags;
  	/*
  	 * Note, where FAST_READ is supported, freq_max specifies the
  	 * FAST_READ frequency, not the READ frequency.
  	 */
  	u32             max_freq;
  	int             (*config)(struct stfsm *);
  };
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
301
  static int stfsm_n25q_config(struct stfsm *fsm);
898180665   Lee Jones   mtd: st_spi_fsm: ...
302
  static int stfsm_mx25_config(struct stfsm *fsm);
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
303
  static int stfsm_s25fl_config(struct stfsm *fsm);
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
304
  static int stfsm_w25q_config(struct stfsm *fsm);
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
305

11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
  static struct flash_info flash_types[] = {
  	/*
  	 * ST Microelectronics/Numonyx --
  	 * (newer production versions may have feature updates
  	 * (eg faster operating frequency)
  	 */
  #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
  	{ "m25p40",  0x202013, 0,  64 * 1024,   8, M25P_FLAG, 25, NULL },
  	{ "m25p80",  0x202014, 0,  64 * 1024,  16, M25P_FLAG, 25, NULL },
  	{ "m25p16",  0x202015, 0,  64 * 1024,  32, M25P_FLAG, 25, NULL },
  	{ "m25p32",  0x202016, 0,  64 * 1024,  64, M25P_FLAG, 50, NULL },
  	{ "m25p64",  0x202017, 0,  64 * 1024, 128, M25P_FLAG, 50, NULL },
  	{ "m25p128", 0x202018, 0, 256 * 1024,  64, M25P_FLAG, 50, NULL },
  
  #define M25PX_FLAG (FLASH_FLAG_READ_WRITE      |	\
  		    FLASH_FLAG_READ_FAST        |	\
  		    FLASH_FLAG_READ_1_1_2       |	\
  		    FLASH_FLAG_WRITE_1_1_2)
  	{ "m25px32", 0x207116, 0,  64 * 1024,  64, M25PX_FLAG, 75, NULL },
  	{ "m25px64", 0x207117, 0,  64 * 1024, 128, M25PX_FLAG, 75, NULL },
6b6d37377   Angus Clark   mtd: st_spi_fsm: ...
326
327
328
329
  	/* Macronix MX25xxx
  	 *     - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
  	 *       where operating frequency must be reduced.
  	 */
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
330
331
332
333
334
  #define MX25_FLAG (FLASH_FLAG_READ_WRITE       |	\
  		   FLASH_FLAG_READ_FAST         |	\
  		   FLASH_FLAG_READ_1_1_2        |	\
  		   FLASH_FLAG_READ_1_2_2        |	\
  		   FLASH_FLAG_READ_1_1_4        |	\
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
335
336
  		   FLASH_FLAG_SE_4K             |	\
  		   FLASH_FLAG_SE_32K)
6b6d37377   Angus Clark   mtd: st_spi_fsm: ...
337
338
339
  	{ "mx25l3255e",  0xc29e16, 0, 64 * 1024, 64,
  	  (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
  	  stfsm_mx25_config},
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
340
  	{ "mx25l25635e", 0xc22019, 0, 64*1024, 512,
898180665   Lee Jones   mtd: st_spi_fsm: ...
341
342
  	  (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  	  stfsm_mx25_config },
5fa980691   Angus Clark   mtd: st_spi_fsm: ...
343
344
345
  	{ "mx25l25655e", 0xc22619, 0, 64*1024, 512,
  	  (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  	  stfsm_mx25_config},
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
346
347
348
349
350
351
352
353
354
355
356
  
  #define N25Q_FLAG (FLASH_FLAG_READ_WRITE       |	\
  		   FLASH_FLAG_READ_FAST         |	\
  		   FLASH_FLAG_READ_1_1_2        |	\
  		   FLASH_FLAG_READ_1_2_2        |	\
  		   FLASH_FLAG_READ_1_1_4        |	\
  		   FLASH_FLAG_READ_1_4_4        |	\
  		   FLASH_FLAG_WRITE_1_1_2       |	\
  		   FLASH_FLAG_WRITE_1_2_2       |	\
  		   FLASH_FLAG_WRITE_1_1_4       |	\
  		   FLASH_FLAG_WRITE_1_4_4)
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
357
358
  	{ "n25q128", 0x20ba18, 0, 64 * 1024,  256, N25Q_FLAG, 108,
  	  stfsm_n25q_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
359
  	{ "n25q256", 0x20ba19, 0, 64 * 1024,  512,
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
360
  	  N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
361
362
363
364
365
366
367
368
369
370
371
372
  
  	/*
  	 * Spansion S25FLxxxP
  	 *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  	 */
  #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE  |	\
  			FLASH_FLAG_READ_1_1_2   |	\
  			FLASH_FLAG_READ_1_2_2   |	\
  			FLASH_FLAG_READ_1_1_4   |	\
  			FLASH_FLAG_READ_1_4_4   |	\
  			FLASH_FLAG_WRITE_1_1_4  |	\
  			FLASH_FLAG_READ_FAST)
85bdcf6b2   Angus Clark   mtd: st_spi_fsm: ...
373
374
  	{ "s25fl032p",  0x010215, 0x4d00,  64 * 1024,  64, S25FLXXXP_FLAG, 80,
  	  stfsm_s25fl_config},
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
375
  	{ "s25fl129p0", 0x012018, 0x4d00, 256 * 1024,  64, S25FLXXXP_FLAG, 80,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
376
  	  stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
377
  	{ "s25fl129p1", 0x012018, 0x4d01,  64 * 1024, 256, S25FLXXXP_FLAG, 80,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
378
  	  stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
379
380
381
382
383
384
385
386
387
388
389
390
391
392
  
  	/*
  	 * Spansion S25FLxxxS
  	 *     - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  	 *     - RESET# signal supported by die but not bristled out on all
  	 *       package types.  The package type is a function of board design,
  	 *       so this information is captured in the board's flags.
  	 *     - Supports 'DYB' sector protection. Depending on variant, sectors
  	 *       may default to locked state on power-on.
  	 */
  #define S25FLXXXS_FLAG (S25FLXXXP_FLAG         |	\
  			FLASH_FLAG_RESET        |	\
  			FLASH_FLAG_DYB_LOCKING)
  	{ "s25fl128s0", 0x012018, 0x0300,  256 * 1024, 64, S25FLXXXS_FLAG, 80,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
393
  	  stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
394
  	{ "s25fl128s1", 0x012018, 0x0301,  64 * 1024, 256, S25FLXXXS_FLAG, 80,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
395
  	  stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
396
  	{ "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
397
  	  S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
398
  	{ "s25fl256s1", 0x010219, 0x4d01,  64 * 1024, 512,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
399
  	  S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  
  	/* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  #define W25X_FLAG (FLASH_FLAG_READ_WRITE       |	\
  		   FLASH_FLAG_READ_FAST         |	\
  		   FLASH_FLAG_READ_1_1_2        |	\
  		   FLASH_FLAG_WRITE_1_1_2)
  	{ "w25x40",  0xef3013, 0,  64 * 1024,   8, W25X_FLAG, 75, NULL },
  	{ "w25x80",  0xef3014, 0,  64 * 1024,  16, W25X_FLAG, 75, NULL },
  	{ "w25x16",  0xef3015, 0,  64 * 1024,  32, W25X_FLAG, 75, NULL },
  	{ "w25x32",  0xef3016, 0,  64 * 1024,  64, W25X_FLAG, 75, NULL },
  	{ "w25x64",  0xef3017, 0,  64 * 1024, 128, W25X_FLAG, 75, NULL },
  
  	/* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
  #define W25Q_FLAG (FLASH_FLAG_READ_WRITE       |	\
  		   FLASH_FLAG_READ_FAST         |	\
  		   FLASH_FLAG_READ_1_1_2        |	\
  		   FLASH_FLAG_READ_1_2_2        |	\
  		   FLASH_FLAG_READ_1_1_4        |	\
  		   FLASH_FLAG_READ_1_4_4        |	\
  		   FLASH_FLAG_WRITE_1_1_4)
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
420
421
422
423
424
425
426
427
  	{ "w25q80",  0xef4014, 0,  64 * 1024,  16, W25Q_FLAG, 80,
  	  stfsm_w25q_config },
  	{ "w25q16",  0xef4015, 0,  64 * 1024,  32, W25Q_FLAG, 80,
  	  stfsm_w25q_config },
  	{ "w25q32",  0xef4016, 0,  64 * 1024,  64, W25Q_FLAG, 80,
  	  stfsm_w25q_config },
  	{ "w25q64",  0xef4017, 0,  64 * 1024, 128, W25Q_FLAG, 80,
  	  stfsm_w25q_config },
11d7f8266   Lee Jones   mtd: st_spi_fsm: ...
428
429
430
431
  
  	/* Sentinel */
  	{ NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
  };
a37b2f5ae   Lee Jones   mtd: st_spi_fsm: ...
432
433
434
435
436
437
438
439
  /*
   * FSM message sequence configurations:
   *
   * All configs are presented in order of preference
   */
  
  /* Default READ configurations, in order of preference */
  static struct seq_rw_config default_read_configs[] = {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
440
441
442
443
444
445
  	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4,	0, 4, 4, 0x00, 2, 4},
  	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4,	0, 1, 4, 0x00, 4, 0},
  	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2,	0, 2, 2, 0x00, 4, 0},
  	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2,	0, 1, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ_FAST,	0, 1, 1, 0x00, 0, 8},
  	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ,		0, 1, 1, 0x00, 0, 0},
a37b2f5ae   Lee Jones   mtd: st_spi_fsm: ...
446
447
448
449
450
  	{0x00,			0,			0, 0, 0, 0x00, 0, 0},
  };
  
  /* Default WRITE configurations */
  static struct seq_rw_config default_write_configs[] = {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
451
452
453
454
455
  	{FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
  	{FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
  	{FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
  	{FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
  	{FLASH_FLAG_READ_WRITE,  SPINOR_OP_WRITE,       1, 1, 1, 0x00, 0, 0},
a37b2f5ae   Lee Jones   mtd: st_spi_fsm: ...
456
457
  	{0x00,			 0,			0, 0, 0, 0x00, 0, 0},
  };
e85a61967   Lee Jones   mtd: st_spi_fsm: ...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
  /*
   * [N25Qxxx] Configuration
   */
  #define N25Q_VCR_DUMMY_CYCLES(x)	(((x) & 0xf) << 4)
  #define N25Q_VCR_XIP_DISABLED		((uint8_t)0x1 << 3)
  #define N25Q_VCR_WRAP_CONT		0x3
  
  /* N25Q 3-byte Address READ configurations
   *	- 'FAST' variants configured for 8 dummy cycles.
   *
   * Note, the number of dummy cycles used for 'FAST' READ operations is
   * configurable and would normally be tuned according to the READ command and
   * operating frequency.  However, this applies universally to all 'FAST' READ
   * commands, including those used by the SPIBoot controller, and remains in
   * force until the device is power-cycled.  Since the SPIBoot controller is
   * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
   * cycles.
   */
  static struct seq_rw_config n25q_read3_configs[] = {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
477
478
479
480
481
482
  	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4,	0, 4, 4, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4,	0, 1, 4, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2,	0, 2, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2,	0, 1, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ_FAST,	0, 1, 1, 0x00, 0, 8},
  	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ,	        0, 1, 1, 0x00, 0, 0},
e85a61967   Lee Jones   mtd: st_spi_fsm: ...
483
484
485
486
487
488
489
490
491
  	{0x00,			0,			0, 0, 0, 0x00, 0, 0},
  };
  
  /* N25Q 4-byte Address READ configurations
   *	- use special 4-byte address READ commands (reduces overheads, and
   *        reduces risk of hitting watchdog reset issues).
   *	- 'FAST' variants configured for 8 dummy cycles (see note above.)
   */
  static struct seq_rw_config n25q_read4_configs[] = {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
492
493
494
495
496
497
  	{FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4,	0, 4, 4, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4,	0, 1, 4, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2,	0, 2, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2,	0, 1, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_FAST,	SPINOR_OP_READ4_FAST,	0, 1, 1, 0x00, 0, 8},
  	{FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4,	0, 1, 1, 0x00, 0, 0},
e85a61967   Lee Jones   mtd: st_spi_fsm: ...
498
499
  	{0x00,			0,			0, 0, 0, 0x00, 0, 0},
  };
898180665   Lee Jones   mtd: st_spi_fsm: ...
500
501
502
503
504
505
506
507
508
  /*
   * [MX25xxx] Configuration
   */
  #define MX25_STATUS_QE			(0x1 << 6)
  
  static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
  {
  	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  			   SEQ_OPC_CYCLES(8) |
6c8e1b33a   Brian Norris   mtd: st_spi_fsm: ...
509
  			   SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
898180665   Lee Jones   mtd: st_spi_fsm: ...
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  			   SEQ_OPC_CSDEASSERT);
  
  	seq->seq[0] = STFSM_INST_CMD1;
  	seq->seq[1] = STFSM_INST_WAIT;
  	seq->seq[2] = STFSM_INST_STOP;
  
  	seq->seq_cfg = (SEQ_CFG_PADS_1 |
  			SEQ_CFG_ERASE |
  			SEQ_CFG_READNOTWRITE |
  			SEQ_CFG_CSDEASSERT |
  			SEQ_CFG_STARTSEQ);
  
  	return 0;
  }
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
524
525
526
527
528
529
530
531
532
533
534
535
  /*
   * [S25FLxxx] Configuration
   */
  #define STFSM_S25FL_CONFIG_QE		(0x1 << 1)
  
  /*
   * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
   * Register, Extended Address Modes, and a 32-bit address command set.  The
   * 32-bit address command set is used here, since it avoids any problems with
   * entering a state that is incompatible with the SPIBoot Controller.
   */
  static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
536
537
538
539
540
541
  	{FLASH_FLAG_READ_1_4_4,  SPINOR_OP_READ4_1_4_4,  0, 4, 4, 0x00, 2, 4},
  	{FLASH_FLAG_READ_1_1_4,  SPINOR_OP_READ4_1_1_4,  0, 1, 4, 0x00, 0, 8},
  	{FLASH_FLAG_READ_1_2_2,  SPINOR_OP_READ4_1_2_2,  0, 2, 2, 0x00, 4, 0},
  	{FLASH_FLAG_READ_1_1_2,  SPINOR_OP_READ4_1_1_2,  0, 1, 2, 0x00, 0, 8},
  	{FLASH_FLAG_READ_FAST,   SPINOR_OP_READ4_FAST,   0, 1, 1, 0x00, 0, 8},
  	{FLASH_FLAG_READ_WRITE,  SPINOR_OP_READ4,        0, 1, 1, 0x00, 0, 0},
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
542
543
544
545
546
547
548
549
  	{0x00,                   0,                      0, 0, 0, 0x00, 0, 0},
  };
  
  static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
  	{FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
  	{FLASH_FLAG_READ_WRITE,  S25FL_CMD_WRITE4,       1, 1, 1, 0x00, 0, 0},
  	{0x00,                   0,                      0, 0, 0, 0x00, 0, 0},
  };
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
550
551
552
  /*
   * [W25Qxxx] Configuration
   */
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
553
  #define W25Q_STATUS_QE			(0x1 << 1)
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
554

1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
555
556
557
558
  static struct stfsm_seq stfsm_seq_read_jedec = {
  	.data_size = TRANSFER_SIZE(8),
  	.seq_opc[0] = (SEQ_OPC_PADS_1 |
  		       SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
559
  		       SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
560
561
562
563
564
565
566
567
568
569
  	.seq = {
  		STFSM_INST_CMD1,
  		STFSM_INST_DATA_READ,
  		STFSM_INST_STOP,
  	},
  	.seq_cfg = (SEQ_CFG_PADS_1 |
  		    SEQ_CFG_READNOTWRITE |
  		    SEQ_CFG_CSDEASSERT |
  		    SEQ_CFG_STARTSEQ),
  };
176b43776   Lee Jones   mtd: st_spi_fsm: ...
570
571
572
573
  static struct stfsm_seq stfsm_seq_read_status_fifo = {
  	.data_size = TRANSFER_SIZE(4),
  	.seq_opc[0] = (SEQ_OPC_PADS_1 |
  		       SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
574
  		       SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
176b43776   Lee Jones   mtd: st_spi_fsm: ...
575
576
577
578
579
580
581
582
583
584
  	.seq = {
  		STFSM_INST_CMD1,
  		STFSM_INST_DATA_READ,
  		STFSM_INST_STOP,
  	},
  	.seq_cfg = (SEQ_CFG_PADS_1 |
  		    SEQ_CFG_READNOTWRITE |
  		    SEQ_CFG_CSDEASSERT |
  		    SEQ_CFG_STARTSEQ),
  };
fa5ba3af2   Lee Jones   mtd: st_spi_fsm: ...
585
586
587
588
  static struct stfsm_seq stfsm_seq_erase_sector = {
  	/* 'addr_cfg' configured during initialisation */
  	.seq_opc = {
  		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
589
  		 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
fa5ba3af2   Lee Jones   mtd: st_spi_fsm: ...
590
591
  
  		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
592
  		 SEQ_OPC_OPCODE(SPINOR_OP_SE)),
fa5ba3af2   Lee Jones   mtd: st_spi_fsm: ...
593
594
595
596
597
598
599
600
601
602
603
604
605
  	},
  	.seq = {
  		STFSM_INST_CMD1,
  		STFSM_INST_CMD2,
  		STFSM_INST_ADD1,
  		STFSM_INST_ADD2,
  		STFSM_INST_STOP,
  	},
  	.seq_cfg = (SEQ_CFG_PADS_1 |
  		    SEQ_CFG_READNOTWRITE |
  		    SEQ_CFG_CSDEASSERT |
  		    SEQ_CFG_STARTSEQ),
  };
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
606
607
608
  static struct stfsm_seq stfsm_seq_erase_chip = {
  	.seq_opc = {
  		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
609
  		 SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
610
611
  
  		(SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
6c8e1b33a   Brian Norris   mtd: st_spi_fsm: ...
612
  		 SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
613
614
615
616
617
618
619
620
621
622
623
624
625
  	},
  	.seq = {
  		STFSM_INST_CMD1,
  		STFSM_INST_CMD2,
  		STFSM_INST_WAIT,
  		STFSM_INST_STOP,
  	},
  	.seq_cfg = (SEQ_CFG_PADS_1 |
  		    SEQ_CFG_ERASE |
  		    SEQ_CFG_READNOTWRITE |
  		    SEQ_CFG_CSDEASSERT |
  		    SEQ_CFG_STARTSEQ),
  };
150571b74   Lee Jones   mtd: st_spi_fsm: ...
626
627
  static struct stfsm_seq stfsm_seq_write_status = {
  	.seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
628
  		       SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
150571b74   Lee Jones   mtd: st_spi_fsm: ...
629
  	.seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
630
  		       SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
150571b74   Lee Jones   mtd: st_spi_fsm: ...
631
632
633
634
635
636
637
638
639
640
641
  	.seq = {
  		STFSM_INST_CMD1,
  		STFSM_INST_CMD2,
  		STFSM_INST_STA_WR1,
  		STFSM_INST_STOP,
  	},
  	.seq_cfg = (SEQ_CFG_PADS_1 |
  		    SEQ_CFG_READNOTWRITE |
  		    SEQ_CFG_CSDEASSERT |
  		    SEQ_CFG_STARTSEQ),
  };
6bd296008   Lee Jones   mtd: st_spi_fsm: ...
642
643
644
  static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
  {
  	seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
6c8e1b33a   Brian Norris   mtd: st_spi_fsm: ...
645
  			   SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
6bd296008   Lee Jones   mtd: st_spi_fsm: ...
646
  	seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
647
  			   SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
6bd296008   Lee Jones   mtd: st_spi_fsm: ...
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
  			   SEQ_OPC_CSDEASSERT);
  
  	seq->seq[0] = STFSM_INST_CMD2;
  	seq->seq[1] = STFSM_INST_CMD1;
  	seq->seq[2] = STFSM_INST_WAIT;
  	seq->seq[3] = STFSM_INST_STOP;
  
  	seq->seq_cfg = (SEQ_CFG_PADS_1 |
  			SEQ_CFG_ERASE |
  			SEQ_CFG_READNOTWRITE |
  			SEQ_CFG_CSDEASSERT |
  			SEQ_CFG_STARTSEQ);
  
  	return 0;
  }
3c8b85b34   Lee Jones   mtd: st_spi_fsm: ...
663
664
665
666
  static inline int stfsm_is_idle(struct stfsm *fsm)
  {
  	return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
  }
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
  {
  	return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
  }
  
  static void stfsm_clear_fifo(struct stfsm *fsm)
  {
  	uint32_t avail;
  
  	for (;;) {
  		avail = stfsm_fifo_available(fsm);
  		if (!avail)
  			break;
  
  		while (avail) {
  			readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
  			avail--;
  		}
  	}
  }
3c8b85b34   Lee Jones   mtd: st_spi_fsm: ...
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
  static inline void stfsm_load_seq(struct stfsm *fsm,
  				  const struct stfsm_seq *seq)
  {
  	void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
  	const uint32_t *src = (const uint32_t *)seq;
  	int words = sizeof(*seq) / sizeof(*src);
  
  	BUG_ON(!stfsm_is_idle(fsm));
  
  	while (words--) {
  		writel(*src, dst);
  		src++;
  		dst += 4;
  	}
  }
  
  static void stfsm_wait_seq(struct stfsm *fsm)
  {
  	unsigned long deadline;
  	int timeout = 0;
  
  	deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
  
  	while (!timeout) {
  		if (time_after_eq(jiffies, deadline))
  			timeout = 1;
  
  		if (stfsm_is_idle(fsm))
  			return;
  
  		cond_resched();
  	}
  
  	dev_err(fsm->dev, "timeout on sequence completion
  ");
  }
3f9d720a4   Lee Jones   mtd: st_spi_fsm: ...
723
  static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
030e82dc6   Lee Jones   mtd: st_spi_fsm: ...
724
725
726
727
728
729
730
  {
  	uint32_t remaining = size >> 2;
  	uint32_t avail;
  	uint32_t words;
  
  	dev_dbg(fsm->dev, "Reading %d bytes from FIFO
  ", size);
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
731
  	BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
030e82dc6   Lee Jones   mtd: st_spi_fsm: ...
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
  
  	while (remaining) {
  		for (;;) {
  			avail = stfsm_fifo_available(fsm);
  			if (avail)
  				break;
  			udelay(1);
  		}
  		words = min(avail, remaining);
  		remaining -= words;
  
  		readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  		buf += words;
  	}
  }
3f9d720a4   Lee Jones   mtd: st_spi_fsm: ...
747
748
  static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
  			    uint32_t size)
30ca64f9f   Lee Jones   mtd: st_spi_fsm: ...
749
750
751
752
753
  {
  	uint32_t words = size >> 2;
  
  	dev_dbg(fsm->dev, "writing %d bytes to FIFO
  ", size);
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
754
  	BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
30ca64f9f   Lee Jones   mtd: st_spi_fsm: ...
755
756
757
758
759
  
  	writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  
  	return size;
  }
0de08e43c   Lee Jones   mtd: st_spi_fsm: ...
760
761
  static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
  {
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
762
  	struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
6c8e1b33a   Brian Norris   mtd: st_spi_fsm: ...
763
  	uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
0de08e43c   Lee Jones   mtd: st_spi_fsm: ...
764
765
766
767
768
769
770
771
772
773
774
775
  
  	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  			   SEQ_OPC_CYCLES(8) |
  			   SEQ_OPC_OPCODE(cmd) |
  			   SEQ_OPC_CSDEASSERT);
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_wait_seq(fsm);
  
  	return 0;
  }
176b43776   Lee Jones   mtd: st_spi_fsm: ...
776
777
778
779
780
781
782
783
784
785
  static uint8_t stfsm_wait_busy(struct stfsm *fsm)
  {
  	struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  	unsigned long deadline;
  	uint32_t status;
  	int timeout = 0;
  
  	/* Use RDRS1 */
  	seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  			   SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
786
  			   SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
176b43776   Lee Jones   mtd: st_spi_fsm: ...
787
788
789
790
791
792
793
794
795
  
  	/* Load read_status sequence */
  	stfsm_load_seq(fsm, seq);
  
  	/*
  	 * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
  	 */
  	deadline = jiffies + FLASH_MAX_BUSY_WAIT;
  	while (!timeout) {
176b43776   Lee Jones   mtd: st_spi_fsm: ...
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  		if (time_after_eq(jiffies, deadline))
  			timeout = 1;
  
  		stfsm_wait_seq(fsm);
  
  		stfsm_read_fifo(fsm, &status, 4);
  
  		if ((status & FLASH_STATUS_BUSY) == 0)
  			return 0;
  
  		if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
  		    ((status & S25FL_STATUS_P_ERR) ||
  		     (status & S25FL_STATUS_E_ERR)))
  			return (uint8_t)(status & 0xff);
  
  		if (!timeout)
  			/* Restart */
  			writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
ea7864bf4   Lee Jones   mtd: st_spi_fsm: ...
814
815
  
  		cond_resched();
176b43776   Lee Jones   mtd: st_spi_fsm: ...
816
817
818
819
820
821
822
  	}
  
  	dev_err(fsm->dev, "timeout on wait_busy
  ");
  
  	return FLASH_STATUS_TIMEOUT;
  }
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
823
  static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
824
  			     uint8_t *data, int bytes)
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
825
826
827
  {
  	struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  	uint32_t tmp;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
828
829
  	uint8_t *t = (uint8_t *)&tmp;
  	int i;
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
830

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
831
832
833
  	dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)
  ",
  		cmd, bytes);
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
834

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
835
836
837
  	BUG_ON(bytes != 1 && bytes != 2);
  
  	seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
838
839
840
841
842
  			   SEQ_OPC_OPCODE(cmd)),
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_read_fifo(fsm, &tmp, 4);
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
843
844
  	for (i = 0; i < bytes; i++)
  		data[i] = t[i];
ac94dbcb6   Lee Jones   mtd: st_spi_fsm: ...
845
846
847
848
849
  
  	stfsm_wait_seq(fsm);
  
  	return 0;
  }
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
850
851
  static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
  			    uint16_t data, int bytes, int wait_busy)
150571b74   Lee Jones   mtd: st_spi_fsm: ...
852
853
  {
  	struct stfsm_seq *seq = &stfsm_seq_write_status;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
854
855
856
857
858
  	dev_dbg(fsm->dev,
  		"write 'status' register [0x%02x], %d byte(s), 0x%04x
  "
  		" %s wait-busy
  ", cmd, bytes, data, wait_busy ? "with" : "no");
150571b74   Lee Jones   mtd: st_spi_fsm: ...
859

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
860
  	BUG_ON(bytes != 1 && bytes != 2);
249516c9c   Lee Jones   mtd: st_spi_fsm: ...
861

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
862
863
  	seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  			   SEQ_OPC_OPCODE(cmd));
249516c9c   Lee Jones   mtd: st_spi_fsm: ...
864

5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
865
866
  	seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
  	seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
249516c9c   Lee Jones   mtd: st_spi_fsm: ...
867
868
869
870
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_wait_seq(fsm);
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
871
872
  	if (wait_busy)
  		stfsm_wait_busy(fsm);
249516c9c   Lee Jones   mtd: st_spi_fsm: ...
873
874
  	return 0;
  }
0ea7d7069   Lee Jones   mtd: st_spi_fsm: ...
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
  /*
   * SoC reset on 'boot-from-spi' systems
   *
   * Certain modes of operation cause the Flash device to enter a particular state
   * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
   * Addr' commands).  On boot-from-spi systems, it is important to consider what
   * happens if a warm reset occurs during this period.  The SPIBoot controller
   * assumes that Flash device is in its default reset state, 24-bit address mode,
   * and ready to accept commands.  This can be achieved using some form of
   * on-board logic/controller to force a device POR in response to a SoC-level
   * reset or by making use of the device reset signal if available (limited
   * number of devices only).
   *
   * Failure to take such precautions can cause problems following a warm reset.
   * For some operations (e.g. ERASE), there is little that can be done.  For
   * other modes of operation (e.g. 32-bit addressing), options are often
   * available that can help minimise the window in which a reset could cause a
   * problem.
   *
   */
  static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
  {
  	/* Reset signal is available on the board and supported by the device */
  	if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
  		return true;
  
  	/* Board-level logic forces a power-on-reset */
  	if (fsm->reset_por)
  		return true;
  
  	/* Reset is not properly handled and may result in failure to reboot */
  	return false;
  }
fa5ba3af2   Lee Jones   mtd: st_spi_fsm: ...
908
909
910
911
912
913
914
915
916
917
918
919
  /* Configure 'addr_cfg' according to addressing mode */
  static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
  				       struct stfsm_seq *seq)
  {
  	int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
  
  	seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
  			 ADR_CFG_PADS_1_ADD1 |
  			 ADR_CFG_CYCLES_ADD2(16) |
  			 ADR_CFG_PADS_1_ADD2 |
  			 ADR_CFG_CSDEASSERT_ADD2);
  }
089812740   Lee Jones   mtd: st_spi_fsm: ...
920
921
922
923
924
925
926
927
928
929
930
931
932
933
  /* Search for preferred configuration based on available flags */
  static struct seq_rw_config *
  stfsm_search_seq_rw_configs(struct stfsm *fsm,
  			    struct seq_rw_config cfgs[])
  {
  	struct seq_rw_config *config;
  	int flags = fsm->info->flags;
  
  	for (config = cfgs; config->cmd != 0; config++)
  		if ((config->flags & flags) == config->flags)
  			return config;
  
  	return NULL;
  }
97ccf2d25   Lee Jones   mtd: st_spi_fsm: ...
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
  /* Prepare a READ/WRITE sequence according to configuration parameters */
  static void stfsm_prepare_rw_seq(struct stfsm *fsm,
  				 struct stfsm_seq *seq,
  				 struct seq_rw_config *cfg)
  {
  	int addr1_cycles, addr2_cycles;
  	int i = 0;
  
  	memset(seq, 0, sizeof(*seq));
  
  	/* Add READ/WRITE OPC  */
  	seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  			     SEQ_OPC_CYCLES(8) |
  			     SEQ_OPC_OPCODE(cfg->cmd));
  
  	/* Add WREN OPC for a WRITE sequence */
  	if (cfg->write)
  		seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  				     SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
953
  				     SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
97ccf2d25   Lee Jones   mtd: st_spi_fsm: ...
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
  				     SEQ_OPC_CSDEASSERT);
  
  	/* Address configuration (24 or 32-bit addresses) */
  	addr1_cycles  = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
  	addr1_cycles /= cfg->addr_pads;
  	addr2_cycles  = 16 / cfg->addr_pads;
  	seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 |	/* ADD1 cycles */
  			 (cfg->addr_pads - 1) << 6 |	/* ADD1 pads */
  			 (addr2_cycles & 0x3f) << 16 |	/* ADD2 cycles */
  			 ((cfg->addr_pads - 1) << 22));	/* ADD2 pads */
  
  	/* Data/Sequence configuration */
  	seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
  			SEQ_CFG_STARTSEQ |
  			SEQ_CFG_CSDEASSERT);
  	if (!cfg->write)
  		seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
  
  	/* Mode configuration (no. of pads taken from addr cfg) */
  	seq->mode = ((cfg->mode_data & 0xff) << 0 |	/* data */
  		     (cfg->mode_cycles & 0x3f) << 16 |	/* cycles */
  		     (cfg->addr_pads - 1) << 22);	/* pads */
  
  	/* Dummy configuration (no. of pads taken from addr cfg) */
  	seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 |	/* cycles */
  		      (cfg->addr_pads - 1) << 22);		/* pads */
  
  
  	/* Instruction sequence */
  	i = 0;
  	if (cfg->write)
  		seq->seq[i++] = STFSM_INST_CMD2;
  
  	seq->seq[i++] = STFSM_INST_CMD1;
  
  	seq->seq[i++] = STFSM_INST_ADD1;
  	seq->seq[i++] = STFSM_INST_ADD2;
  
  	if (cfg->mode_cycles)
  		seq->seq[i++] = STFSM_INST_MODE;
  
  	if (cfg->dummy_cycles)
  		seq->seq[i++] = STFSM_INST_DUMMY;
  
  	seq->seq[i++] =
  		cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
  	seq->seq[i++] = STFSM_INST_STOP;
  }
88cccb891   Lee Jones   mtd: st_spi_fsm: ...
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
  static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
  				       struct stfsm_seq *seq,
  				       struct seq_rw_config *cfgs)
  {
  	struct seq_rw_config *config;
  
  	config = stfsm_search_seq_rw_configs(fsm, cfgs);
  	if (!config) {
  		dev_err(fsm->dev, "failed to find suitable config
  ");
  		return -EINVAL;
  	}
  
  	stfsm_prepare_rw_seq(fsm, seq, config);
  
  	return 0;
  }
4eb3f0d8f   Lee Jones   mtd: st_spi_fsm: ...
1019
1020
1021
1022
1023
1024
1025
  /* Prepare a READ/WRITE/ERASE 'default' sequences */
  static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
  {
  	uint32_t flags = fsm->info->flags;
  	int ret;
  
  	/* Configure 'READ' sequence */
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1026
  	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
4eb3f0d8f   Lee Jones   mtd: st_spi_fsm: ...
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
  					  default_read_configs);
  	if (ret) {
  		dev_err(fsm->dev,
  			"failed to prep READ sequence with flags [0x%08x]
  ",
  			flags);
  		return ret;
  	}
  
  	/* Configure 'WRITE' sequence */
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1037
  	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
4eb3f0d8f   Lee Jones   mtd: st_spi_fsm: ...
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
  					  default_write_configs);
  	if (ret) {
  		dev_err(fsm->dev,
  			"failed to prep WRITE sequence with flags [0x%08x]
  ",
  			flags);
  		return ret;
  	}
  
  	/* Configure 'ERASE_SECTOR' sequence */
  	stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  
  	return 0;
  }
898180665   Lee Jones   mtd: st_spi_fsm: ...
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
  static int stfsm_mx25_config(struct stfsm *fsm)
  {
  	uint32_t flags = fsm->info->flags;
  	uint32_t data_pads;
  	uint8_t sta;
  	int ret;
  	bool soc_reset;
  
  	/*
  	 * Use default READ/WRITE sequences
  	 */
  	ret = stfsm_prepare_rwe_seqs_default(fsm);
  	if (ret)
  		return ret;
  
  	/*
  	 * Configure 32-bit Address Support
  	 */
  	if (flags & FLASH_FLAG_32BIT_ADDR) {
  		/* Configure 'enter_32bitaddr' FSM sequence */
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1072
  		stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
898180665   Lee Jones   mtd: st_spi_fsm: ...
1073
1074
  
  		soc_reset = stfsm_can_handle_soc_reset(fsm);
009e7e61b   Angus Clark   mtd: st_spi_fsm: ...
1075
  		if (soc_reset || !fsm->booted_from_spi)
898180665   Lee Jones   mtd: st_spi_fsm: ...
1076
1077
1078
  			/* If we can handle SoC resets, we enable 32-bit address
  			 * mode pervasively */
  			stfsm_enter_32bit_addr(fsm, 1);
009e7e61b   Angus Clark   mtd: st_spi_fsm: ...
1079
  		else
898180665   Lee Jones   mtd: st_spi_fsm: ...
1080
1081
1082
1083
1084
  			/* Else, enable/disable 32-bit addressing before/after
  			 * each operation */
  			fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
  					      CFG_WRITE_TOGGLE_32BIT_ADDR |
  					      CFG_ERASESEC_TOGGLE_32BIT_ADDR);
898180665   Lee Jones   mtd: st_spi_fsm: ...
1085
  	}
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1086
  	/* Check status of 'QE' bit, update if required. */
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1087
  	stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1088
  	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
898180665   Lee Jones   mtd: st_spi_fsm: ...
1089
  	if (data_pads == 4) {
cc6668637   Angus Clark   mtd: st_spi_fsm: ...
1090
1091
1092
  		if (!(sta & MX25_STATUS_QE)) {
  			/* Set 'QE' */
  			sta |= MX25_STATUS_QE;
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1093
  			stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
cc6668637   Angus Clark   mtd: st_spi_fsm: ...
1094
1095
1096
1097
1098
  		}
  	} else {
  		if (sta & MX25_STATUS_QE) {
  			/* Clear 'QE' */
  			sta &= ~MX25_STATUS_QE;
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1099
  			stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
cc6668637   Angus Clark   mtd: st_spi_fsm: ...
1100
  		}
898180665   Lee Jones   mtd: st_spi_fsm: ...
1101
1102
1103
1104
  	}
  
  	return 0;
  }
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1105
1106
1107
1108
1109
1110
1111
1112
1113
  static int stfsm_n25q_config(struct stfsm *fsm)
  {
  	uint32_t flags = fsm->info->flags;
  	uint8_t vcr;
  	int ret = 0;
  	bool soc_reset;
  
  	/* Configure 'READ' sequence */
  	if (flags & FLASH_FLAG_32BIT_ADDR)
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1114
  		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1115
1116
  						  n25q_read4_configs);
  	else
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1117
  		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
  						  n25q_read3_configs);
  	if (ret) {
  		dev_err(fsm->dev,
  			"failed to prepare READ sequence with flags [0x%08x]
  ",
  			flags);
  		return ret;
  	}
  
  	/* Configure 'WRITE' sequence (default configs) */
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1128
  	ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
  					  default_write_configs);
  	if (ret) {
  		dev_err(fsm->dev,
  			"preparing WRITE sequence using flags [0x%08x] failed
  ",
  			flags);
  		return ret;
  	}
  
  	/* * Configure 'ERASE_SECTOR' sequence */
  	stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  
  	/* Configure 32-bit address support */
  	if (flags & FLASH_FLAG_32BIT_ADDR) {
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1143
  		stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
  
  		soc_reset = stfsm_can_handle_soc_reset(fsm);
  		if (soc_reset || !fsm->booted_from_spi) {
  			/*
  			 * If we can handle SoC resets, we enable 32-bit
  			 * address mode pervasively
  			 */
  			stfsm_enter_32bit_addr(fsm, 1);
  		} else {
  			/*
  			 * If not, enable/disable for WRITE and ERASE
  			 * operations (READ uses special commands)
  			 */
  			fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
  					      CFG_ERASESEC_TOGGLE_32BIT_ADDR);
  		}
  	}
  
  	/*
  	 * Configure device to use 8 dummy cycles
  	 */
  	vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
  	       N25Q_VCR_WRAP_CONT);
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1167
  	stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1168
1169
1170
  
  	return 0;
  }
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
  static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
  {
  	seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
  			   SEQ_OPC_CYCLES(8) |
  			   SEQ_OPC_OPCODE(S25FL_CMD_SE4));
  
  	seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  			 ADR_CFG_PADS_1_ADD1 |
  			 ADR_CFG_CYCLES_ADD2(16) |
  			 ADR_CFG_PADS_1_ADD2 |
  			 ADR_CFG_CSDEASSERT_ADD2);
  }
  
  static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
  {
  	uint32_t tmp;
  	struct stfsm_seq seq = {
  		.data_size = TRANSFER_SIZE(4),
  		.seq_opc[0] = (SEQ_OPC_PADS_1 |
  			       SEQ_OPC_CYCLES(8) |
  			       SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
  		.addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  			     ADR_CFG_PADS_1_ADD1 |
  			     ADR_CFG_CYCLES_ADD2(16) |
  			     ADR_CFG_PADS_1_ADD2),
  		.addr1 = (offs >> 16) & 0xffff,
  		.addr2 = offs & 0xffff,
  		.seq = {
  			STFSM_INST_CMD1,
  			STFSM_INST_ADD1,
  			STFSM_INST_ADD2,
  			STFSM_INST_DATA_READ,
  			STFSM_INST_STOP,
  		},
  		.seq_cfg = (SEQ_CFG_PADS_1 |
  			    SEQ_CFG_READNOTWRITE |
  			    SEQ_CFG_CSDEASSERT |
  			    SEQ_CFG_STARTSEQ),
  	};
  
  	stfsm_load_seq(fsm, &seq);
  
  	stfsm_read_fifo(fsm, &tmp, 4);
  
  	*dby = (uint8_t)(tmp >> 24);
  
  	stfsm_wait_seq(fsm);
  }
  
  static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
  {
  	struct stfsm_seq seq = {
  		.seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1224
  			       SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
  			       SEQ_OPC_CSDEASSERT),
  		.seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  			       SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
  		.addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  			     ADR_CFG_PADS_1_ADD1 |
  			     ADR_CFG_CYCLES_ADD2(16) |
  			     ADR_CFG_PADS_1_ADD2),
  		.status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
  		.addr1 = (offs >> 16) & 0xffff,
  		.addr2 = offs & 0xffff,
  		.seq = {
  			STFSM_INST_CMD1,
  			STFSM_INST_CMD2,
  			STFSM_INST_ADD1,
  			STFSM_INST_ADD2,
  			STFSM_INST_STA_WR1,
  			STFSM_INST_STOP,
  		},
  		.seq_cfg = (SEQ_CFG_PADS_1 |
  			    SEQ_CFG_READNOTWRITE |
  			    SEQ_CFG_CSDEASSERT |
  			    SEQ_CFG_STARTSEQ),
  	};
  
  	stfsm_load_seq(fsm, &seq);
  	stfsm_wait_seq(fsm);
  
  	stfsm_wait_busy(fsm);
  }
  
  static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
  {
  	struct stfsm_seq seq = {
  		.seq_opc[0] = (SEQ_OPC_PADS_1 |
  			       SEQ_OPC_CYCLES(8) |
  			       SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
  			       SEQ_OPC_CSDEASSERT),
  		.seq_opc[1] = (SEQ_OPC_PADS_1 |
  			       SEQ_OPC_CYCLES(8) |
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1264
  			       SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
  			       SEQ_OPC_CSDEASSERT),
  		.seq = {
  			STFSM_INST_CMD1,
  			STFSM_INST_CMD2,
  			STFSM_INST_WAIT,
  			STFSM_INST_STOP,
  		},
  		.seq_cfg = (SEQ_CFG_PADS_1 |
  			    SEQ_CFG_ERASE |
  			    SEQ_CFG_READNOTWRITE |
  			    SEQ_CFG_CSDEASSERT |
  			    SEQ_CFG_STARTSEQ),
  	};
  
  	stfsm_load_seq(fsm, &seq);
  
  	stfsm_wait_seq(fsm);
  
  	return 0;
  }
  
  static int stfsm_s25fl_config(struct stfsm *fsm)
  {
  	struct flash_info *info = fsm->info;
  	uint32_t flags = info->flags;
  	uint32_t data_pads;
  	uint32_t offs;
  	uint16_t sta_wr;
  	uint8_t sr1, cr1, dyb;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1294
  	int update_sr = 0;
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1295
1296
1297
1298
1299
1300
1301
  	int ret;
  
  	if (flags & FLASH_FLAG_32BIT_ADDR) {
  		/*
  		 * Prepare Read/Write/Erase sequences according to S25FLxxx
  		 * 32-bit address command set
  		 */
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1302
  		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1303
1304
1305
  						  stfsm_s25fl_read4_configs);
  		if (ret)
  			return ret;
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1306
  		ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
  						  stfsm_s25fl_write4_configs);
  		if (ret)
  			return ret;
  
  		stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
  
  	} else {
  		/* Use default configurations for 24-bit addressing */
  		ret = stfsm_prepare_rwe_seqs_default(fsm);
  		if (ret)
  			return ret;
  	}
  
  	/*
  	 * For devices that support 'DYB' sector locking, check lock status and
  	 * unlock sectors if necessary (some variants power-on with sectors
  	 * locked by default)
  	 */
  	if (flags & FLASH_FLAG_DYB_LOCKING) {
  		offs = 0;
  		for (offs = 0; offs < info->sector_size * info->n_sectors;) {
  			stfsm_s25fl_read_dyb(fsm, offs, &dyb);
  			if (dyb == 0x00)
  				stfsm_s25fl_write_dyb(fsm, offs, 0xff);
  
  			/* Handle bottom/top 4KiB parameter sectors */
  			if ((offs < info->sector_size * 2) ||
  			    (offs >= (info->sector_size - info->n_sectors * 4)))
  				offs += 0x1000;
  			else
  				offs += 0x10000;
  		}
  	}
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1340
  	/* Check status of 'QE' bit, update if required. */
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1341
  	stfsm_read_status(fsm, SPINOR_OP_RDSR2, &cr1, 1);
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1342
  	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1343
1344
1345
1346
  	if (data_pads == 4) {
  		if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
  			/* Set 'QE' */
  			cr1 |= STFSM_S25FL_CONFIG_QE;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1347
  			update_sr = 1;
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1348
1349
  		}
  	} else {
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1350
  		if (cr1 & STFSM_S25FL_CONFIG_QE) {
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1351
1352
  			/* Clear 'QE' */
  			cr1 &= ~STFSM_S25FL_CONFIG_QE;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1353
  			update_sr = 1;
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1354
  		}
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1355
1356
  	}
  	if (update_sr) {
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1357
  		stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1358
  		sta_wr = ((uint16_t)cr1  << 8) | sr1;
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1359
  		stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
  	}
  
  	/*
  	 * S25FLxxx devices support Program and Error error flags.
  	 * Configure driver to check flags and clear if necessary.
  	 */
  	fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
  
  	return 0;
  }
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
1370
1371
1372
  static int stfsm_w25q_config(struct stfsm *fsm)
  {
  	uint32_t data_pads;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1373
1374
1375
  	uint8_t sr1, sr2;
  	uint16_t sr_wr;
  	int update_sr = 0;
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
1376
1377
1378
1379
1380
  	int ret;
  
  	ret = stfsm_prepare_rwe_seqs_default(fsm);
  	if (ret)
  		return ret;
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1381
  	/* Check status of 'QE' bit, update if required. */
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1382
  	stfsm_read_status(fsm, SPINOR_OP_RDSR2, &sr2, 1);
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1383
  	data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
1384
  	if (data_pads == 4) {
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
  		if (!(sr2 & W25Q_STATUS_QE)) {
  			/* Set 'QE' */
  			sr2 |= W25Q_STATUS_QE;
  			update_sr = 1;
  		}
  	} else {
  		if (sr2 & W25Q_STATUS_QE) {
  			/* Clear 'QE' */
  			sr2 &= ~W25Q_STATUS_QE;
  			update_sr = 1;
  		}
  	}
  	if (update_sr) {
  		/* Write status register */
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1399
  		stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
5d0bddab3   Angus Clark   mtd: st_spi_fsm: ...
1400
  		sr_wr = ((uint16_t)sr2 << 8) | sr1;
92d3af9ac   Brian Norris   mtd: st_spi_fsm: ...
1401
  		stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
cd7cac9ec   Lee Jones   mtd: st_spi_fsm: ...
1402
1403
1404
1405
  	}
  
  	return 0;
  }
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1406
1407
1408
  static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
  		      uint32_t offset)
  {
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1409
  	struct stfsm_seq *seq = &fsm->stfsm_seq_read;
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
  	uint32_t data_pads;
  	uint32_t read_mask;
  	uint32_t size_ub;
  	uint32_t size_lb;
  	uint32_t size_mop;
  	uint32_t tmp[4];
  	uint32_t page_buf[FLASH_PAGESIZE_32];
  	uint8_t *p;
  
  	dev_dbg(fsm->dev, "reading %d bytes from 0x%08x
  ", size, offset);
  
  	/* Enter 32-bit address mode, if required */
  	if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  		stfsm_enter_32bit_addr(fsm, 1);
  
  	/* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
  	data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  	read_mask = (data_pads << 2) - 1;
  
  	/* Handle non-aligned buf */
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
1431
  	p = ((uintptr_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
  
  	/* Handle non-aligned size */
  	size_ub = (size + read_mask) & ~read_mask;
  	size_lb = size & ~read_mask;
  	size_mop = size & read_mask;
  
  	seq->data_size = TRANSFER_SIZE(size_ub);
  	seq->addr1 = (offset >> 16) & 0xffff;
  	seq->addr2 = offset & 0xffff;
  
  	stfsm_load_seq(fsm, seq);
  
  	if (size_lb)
  		stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
  
  	if (size_mop) {
  		stfsm_read_fifo(fsm, tmp, read_mask + 1);
  		memcpy(p + size_lb, &tmp, size_mop);
  	}
  
  	/* Handle non-aligned buf */
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
1453
  	if ((uintptr_t)buf & 0x3)
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
  		memcpy(buf, page_buf, size);
  
  	/* Wait for sequence to finish */
  	stfsm_wait_seq(fsm);
  
  	stfsm_clear_fifo(fsm);
  
  	/* Exit 32-bit address mode, if required */
  	if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  		stfsm_enter_32bit_addr(fsm, 0);
  
  	return 0;
  }
3f9d720a4   Lee Jones   mtd: st_spi_fsm: ...
1467
1468
  static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
  		       uint32_t size, uint32_t offset)
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1469
  {
e6b1bb4e1   Lee Jones   mtd: st_spi_fsm: ...
1470
  	struct stfsm_seq *seq = &fsm->stfsm_seq_write;
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
  	uint32_t data_pads;
  	uint32_t write_mask;
  	uint32_t size_ub;
  	uint32_t size_lb;
  	uint32_t size_mop;
  	uint32_t tmp[4];
  	uint32_t page_buf[FLASH_PAGESIZE_32];
  	uint8_t *t = (uint8_t *)&tmp;
  	const uint8_t *p;
  	int ret;
  	int i;
  
  	dev_dbg(fsm->dev, "writing %d bytes to 0x%08x
  ", size, offset);
  
  	/* Enter 32-bit address mode, if required */
  	if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
  		stfsm_enter_32bit_addr(fsm, 1);
  
  	/* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
  	data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  	write_mask = (data_pads << 2) - 1;
  
  	/* Handle non-aligned buf */
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
1495
  	if ((uintptr_t)buf & 0x3) {
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
  		memcpy(page_buf, buf, size);
  		p = (uint8_t *)page_buf;
  	} else {
  		p = buf;
  	}
  
  	/* Handle non-aligned size */
  	size_ub = (size + write_mask) & ~write_mask;
  	size_lb = size & ~write_mask;
  	size_mop = size & write_mask;
  
  	seq->data_size = TRANSFER_SIZE(size_ub);
  	seq->addr1 = (offset >> 16) & 0xffff;
  	seq->addr2 = offset & 0xffff;
  
  	/* Need to set FIFO to write mode, before writing data to FIFO (see
  	 * GNBvb79594)
  	 */
  	writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
  
  	/*
  	 * Before writing data to the FIFO, apply a small delay to allow a
  	 * potential change of FIFO direction to complete.
  	 */
  	if (fsm->fifo_dir_delay == 0)
  		readl(fsm->base + SPI_FAST_SEQ_CFG);
  	else
  		udelay(fsm->fifo_dir_delay);
  
  
  	/* Write data to FIFO, before starting sequence (see GNBvd79593) */
  	if (size_lb) {
  		stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
  		p += size_lb;
  	}
  
  	/* Handle non-aligned size */
  	if (size_mop) {
  		memset(t, 0xff, write_mask + 1);	/* fill with 0xff's */
  		for (i = 0; i < size_mop; i++)
  			t[i] = *p++;
  
  		stfsm_write_fifo(fsm, tmp, write_mask + 1);
  	}
  
  	/* Start sequence */
  	stfsm_load_seq(fsm, seq);
  
  	/* Wait for sequence to finish */
  	stfsm_wait_seq(fsm);
  
  	/* Wait for completion */
  	ret = stfsm_wait_busy(fsm);
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1549
1550
  	if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  		stfsm_s25fl_clear_status_reg(fsm);
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1551
1552
  
  	/* Exit 32-bit address mode, if required */
009e7e61b   Angus Clark   mtd: st_spi_fsm: ...
1553
  	if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1554
  		stfsm_enter_32bit_addr(fsm, 0);
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1555
1556
1557
  
  	return 0;
  }
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
  /*
   * Read an address range from the flash chip. The address range
   * may be any size provided it is within the physical boundaries.
   */
  static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  			  size_t *retlen, u_char *buf)
  {
  	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  	uint32_t bytes;
  
  	dev_dbg(fsm->dev, "%s from 0x%08x, len %zd
  ",
  		__func__, (u32)from, len);
  
  	mutex_lock(&fsm->lock);
  
  	while (len > 0) {
  		bytes = min_t(size_t, len, FLASH_PAGESIZE);
  
  		stfsm_read(fsm, buf, bytes, from);
  
  		buf += bytes;
  		from += bytes;
  		len -= bytes;
  
  		*retlen += bytes;
  	}
  
  	mutex_unlock(&fsm->lock);
  
  	return 0;
  }
3f9d720a4   Lee Jones   mtd: st_spi_fsm: ...
1590
  static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
  {
  	struct stfsm_seq *seq = &stfsm_seq_erase_sector;
  	int ret;
  
  	dev_dbg(fsm->dev, "erasing sector at 0x%08x
  ", offset);
  
  	/* Enter 32-bit address mode, if required */
  	if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  		stfsm_enter_32bit_addr(fsm, 1);
  
  	seq->addr1 = (offset >> 16) & 0xffff;
  	seq->addr2 = offset & 0xffff;
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_wait_seq(fsm);
  
  	/* Wait for completion */
  	ret = stfsm_wait_busy(fsm);
5343a1234   Lee Jones   mtd: st_spi_fsm: ...
1611
1612
  	if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  		stfsm_s25fl_clear_status_reg(fsm);
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
  
  	/* Exit 32-bit address mode, if required */
  	if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  		stfsm_enter_32bit_addr(fsm, 0);
  
  	return ret;
  }
  
  static int stfsm_erase_chip(struct stfsm *fsm)
  {
  	const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
  
  	dev_dbg(fsm->dev, "erasing chip
  ");
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_wait_seq(fsm);
  
  	return stfsm_wait_busy(fsm);
  }
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
  /*
   * Write an address range to the flash chip.  Data must be written in
   * FLASH_PAGESIZE chunks.  The address range may be any size provided
   * it is within the physical boundaries.
   */
  static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  			   size_t *retlen, const u_char *buf)
  {
  	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  
  	u32 page_offs;
  	u32 bytes;
  	uint8_t *b = (uint8_t *)buf;
  	int ret = 0;
  
  	dev_dbg(fsm->dev, "%s to 0x%08x, len %zd
  ", __func__, (u32)to, len);
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1651
1652
1653
1654
1655
1656
1657
  	/* Offset within page */
  	page_offs = to % FLASH_PAGESIZE;
  
  	mutex_lock(&fsm->lock);
  
  	while (len) {
  		/* Write up to page boundary */
38e2eee9a   Brian Norris   mtd: st_spi_fsm: ...
1658
  		bytes = min_t(size_t, FLASH_PAGESIZE - page_offs, len);
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
  
  		ret = stfsm_write(fsm, b, bytes, to);
  		if (ret)
  			goto out1;
  
  		b += bytes;
  		len -= bytes;
  		to += bytes;
  
  		/* We are now page-aligned */
  		page_offs = 0;
  
  		*retlen += bytes;
  
  	}
  
  out1:
  	mutex_unlock(&fsm->lock);
  
  	return ret;
  }
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
  /*
   * Erase an address range on the flash chip. The address range may extend
   * one or more erase sectors.  Return an error is there is a problem erasing.
   */
  static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  {
  	struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  	u32 addr, len;
  	int ret;
  
  	dev_dbg(fsm->dev, "%s at 0x%llx, len %lld
  ", __func__,
  		(long long)instr->addr, (long long)instr->len);
  
  	addr = instr->addr;
  	len = instr->len;
  
  	mutex_lock(&fsm->lock);
  
  	/* Whole-chip erase? */
  	if (len == mtd->size) {
  		ret = stfsm_erase_chip(fsm);
  		if (ret)
  			goto out1;
  	} else {
  		while (len) {
  			ret = stfsm_erase_sector(fsm, addr);
  			if (ret)
  				goto out1;
  
  			addr += mtd->erasesize;
  			len -= mtd->erasesize;
  		}
  	}
  
  	mutex_unlock(&fsm->lock);
  
  	instr->state = MTD_ERASE_DONE;
  	mtd_erase_callback(instr);
  
  	return 0;
  
  out1:
  	instr->state = MTD_ERASE_FAILED;
  	mutex_unlock(&fsm->lock);
  
  	return ret;
  }
3f9d720a4   Lee Jones   mtd: st_spi_fsm: ...
1728
  static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
  {
  	const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
  	uint32_t tmp[2];
  
  	stfsm_load_seq(fsm, seq);
  
  	stfsm_read_fifo(fsm, tmp, 8);
  
  	memcpy(jedec, tmp, 5);
  
  	stfsm_wait_seq(fsm);
  }
  
  static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
  {
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1744
  	struct flash_info	*info;
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
  	u16                     ext_jedec;
  	u32			jedec;
  	u8			id[5];
  
  	stfsm_read_jedec(fsm, id);
  
  	jedec     = id[0] << 16 | id[1] << 8 | id[2];
  	/*
  	 * JEDEC also defines an optional "extended device information"
  	 * string for after vendor-specific data, after the three bytes
  	 * we use here. Supporting some chips might require using it.
  	 */
  	ext_jedec = id[3] << 8  | id[4];
  
  	dev_dbg(fsm->dev, "JEDEC =  0x%08x [%02x %02x %02x %02x %02x]
  ",
  		jedec, id[0], id[1], id[2], id[3], id[4]);
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1762
1763
1764
1765
1766
1767
1768
1769
1770
  	for (info = flash_types; info->name; info++) {
  		if (info->jedec_id == jedec) {
  			if (info->ext_id && info->ext_id != ext_jedec)
  				continue;
  			return info;
  		}
  	}
  	dev_err(fsm->dev, "Unrecognized JEDEC id %06x
  ", jedec);
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
1771
1772
  	return NULL;
  }
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
  static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
  {
  	int ret, timeout = 10;
  
  	/* Wait for controller to accept mode change */
  	while (--timeout) {
  		ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
  		if (ret & 0x1)
  			break;
  		udelay(1);
  	}
  
  	if (!timeout)
  		return -EBUSY;
  
  	writel(mode, fsm->base + SPI_MODESELECT);
  
  	return 0;
  }
  
  static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
  {
  	uint32_t emi_freq;
  	uint32_t clk_div;
  
  	/* TODO: Make this dynamic */
  	emi_freq = STFSM_DEFAULT_EMI_FREQ;
  
  	/*
  	 * Calculate clk_div - values between 2 and 128
  	 * Multiple of 2, rounded up
  	 */
  	clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
  	if (clk_div < 2)
  		clk_div = 2;
  	else if (clk_div > 128)
  		clk_div = 128;
  
  	/*
  	 * Determine a suitable delay for the IP to complete a change of
  	 * direction of the FIFO. The required delay is related to the clock
  	 * divider used. The following heuristics are based on empirical tests,
  	 * using a 100MHz EMI clock.
  	 */
  	if (clk_div <= 4)
  		fsm->fifo_dir_delay = 0;
  	else if (clk_div <= 10)
  		fsm->fifo_dir_delay = 1;
  	else
  		fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
  
  	dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u
  ",
  		emi_freq, spi_freq, clk_div);
  
  	writel(clk_div, fsm->base + SPI_CLOCKDIV);
  }
  
  static int stfsm_init(struct stfsm *fsm)
  {
  	int ret;
  
  	/* Perform a soft reset of the FSM controller */
  	writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
  	udelay(1);
  	writel(0, fsm->base + SPI_FAST_SEQ_CFG);
  
  	/* Set clock to 'safe' frequency initially */
  	stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
  
  	/* Switch to FSM */
  	ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
  	if (ret)
  		return ret;
  
  	/* Set timing parameters */
  	writel(SPI_CFG_DEVICE_ST            |
  	       SPI_CFG_DEFAULT_MIN_CS_HIGH  |
  	       SPI_CFG_DEFAULT_CS_SETUPHOLD |
  	       SPI_CFG_DEFAULT_DATA_HOLD,
  	       fsm->base + SPI_CONFIGDATA);
  	writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
009e7e61b   Angus Clark   mtd: st_spi_fsm: ...
1855
1856
1857
1858
1859
1860
  	/*
  	 * Set the FSM 'WAIT' delay to the minimum workable value.  Note, for
  	 * our purposes, the WAIT instruction is used purely to achieve
  	 * "sequence validity" rather than actually implement a delay.
  	 */
  	writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
1861
1862
1863
1864
1865
  	/* Clear FIFO, just in case */
  	stfsm_clear_fifo(fsm);
  
  	return 0;
  }
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
  static void stfsm_fetch_platform_configs(struct platform_device *pdev)
  {
  	struct stfsm *fsm = platform_get_drvdata(pdev);
  	struct device_node *np = pdev->dev.of_node;
  	struct regmap *regmap;
  	uint32_t boot_device_reg;
  	uint32_t boot_device_spi;
  	uint32_t boot_device;     /* Value we read from *boot_device_reg */
  	int ret;
  
  	/* Booting from SPI NOR Flash is the default */
  	fsm->booted_from_spi = true;
  
  	regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  	if (IS_ERR(regmap))
  		goto boot_device_fail;
0ea7d7069   Lee Jones   mtd: st_spi_fsm: ...
1882
1883
1884
  	fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
  
  	fsm->reset_por = of_property_read_bool(np, "st,reset-por");
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
  	/* Where in the syscon the boot device information lives */
  	ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
  	if (ret)
  		goto boot_device_fail;
  
  	/* Boot device value when booted from SPI NOR */
  	ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
  	if (ret)
  		goto boot_device_fail;
  
  	ret = regmap_read(regmap, boot_device_reg, &boot_device);
  	if (ret)
  		goto boot_device_fail;
  
  	if (boot_device != boot_device_spi)
  		fsm->booted_from_spi = false;
  
  	return;
  
  boot_device_fail:
  	dev_warn(&pdev->dev,
  		 "failed to fetch boot device, assuming boot from SPI
  ");
  }
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1909
1910
1911
  static int stfsm_probe(struct platform_device *pdev)
  {
  	struct device_node *np = pdev->dev.of_node;
221cff13c   Lee Jones   mtd: st_spi_fsm: ...
1912
  	struct mtd_part_parser_data ppdata;
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1913
  	struct flash_info *info;
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1914
1915
  	struct resource *res;
  	struct stfsm *fsm;
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
1916
  	int ret;
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1917
1918
1919
1920
1921
1922
  
  	if (!np) {
  		dev_err(&pdev->dev, "No DT found
  ");
  		return -EINVAL;
  	}
221cff13c   Lee Jones   mtd: st_spi_fsm: ...
1923
  	ppdata.of_node = np;
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
  
  	fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
  	if (!fsm)
  		return -ENOMEM;
  
  	fsm->dev = &pdev->dev;
  
  	platform_set_drvdata(pdev, fsm);
  
  	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  	if (!res) {
  		dev_err(&pdev->dev, "Resource not found
  ");
  		return -ENODEV;
  	}
  
  	fsm->base = devm_ioremap_resource(&pdev->dev, res);
  	if (IS_ERR(fsm->base)) {
  		dev_err(&pdev->dev,
  			"Failed to reserve memory region %pR
  ", res);
  		return PTR_ERR(fsm->base);
  	}
  
  	mutex_init(&fsm->lock);
86f309fd8   Lee Jones   mtd: st_spi_fsm: ...
1949
1950
1951
1952
1953
1954
  	ret = stfsm_init(fsm);
  	if (ret) {
  		dev_err(&pdev->dev, "Failed to initialise FSM Controller
  ");
  		return ret;
  	}
a63984c18   Lee Jones   mtd: st_spi_fsm: ...
1955
  	stfsm_fetch_platform_configs(pdev);
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
1956
  	/* Detect SPI FLASH device */
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1957
1958
1959
1960
  	info = stfsm_jedec_probe(fsm);
  	if (!info)
  		return -ENODEV;
  	fsm->info = info;
1bd512b56   Lee Jones   mtd: st_spi_fsm: ...
1961

3b5d19819   Lee Jones   mtd: st_spi_fsm: ...
1962
1963
1964
  	/* Use device size to determine address width */
  	if (info->sector_size * info->n_sectors > 0x1000000)
  		info->flags |= FLASH_FLAG_32BIT_ADDR;
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1965
1966
1967
1968
1969
1970
1971
1972
  	/*
  	 * Configure READ/WRITE/ERASE sequences according to platform and
  	 * device flags.
  	 */
  	if (info->config) {
  		ret = info->config(fsm);
  		if (ret)
  			return ret;
4eb3f0d8f   Lee Jones   mtd: st_spi_fsm: ...
1973
1974
1975
1976
  	} else {
  		ret = stfsm_prepare_rwe_seqs_default(fsm);
  		if (ret)
  			return ret;
218b870f9   Lee Jones   mtd: st_spi_fsm: ...
1977
  	}
221cff13c   Lee Jones   mtd: st_spi_fsm: ...
1978
  	fsm->mtd.name		= info->name;
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1979
1980
1981
1982
1983
  	fsm->mtd.dev.parent	= &pdev->dev;
  	fsm->mtd.type		= MTD_NORFLASH;
  	fsm->mtd.writesize	= 4;
  	fsm->mtd.writebufsize	= fsm->mtd.writesize;
  	fsm->mtd.flags		= MTD_CAP_NORFLASH;
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1984
1985
  	fsm->mtd.size		= info->sector_size * info->n_sectors;
  	fsm->mtd.erasesize	= info->sector_size;
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1986
  	fsm->mtd._read  = stfsm_mtd_read;
176b43776   Lee Jones   mtd: st_spi_fsm: ...
1987
  	fsm->mtd._write = stfsm_mtd_write;
4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
1988
  	fsm->mtd._erase = stfsm_mtd_erase;
e514f1057   Lee Jones   mtd: st_spi_fsm: ...
1989

4a341fe75   Lee Jones   mtd: st_spi_fsm: ...
1990
  	dev_info(&pdev->dev,
24fec651d   Lee Jones   mtd: st_spi_fsm: ...
1991
1992
1993
1994
1995
1996
1997
  		"Found serial flash device: %s
  "
  		" size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)
  ",
  		info->name,
  		(long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
  		fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
1998

221cff13c   Lee Jones   mtd: st_spi_fsm: ...
1999
  	return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
2000
2001
2002
2003
2004
  }
  
  static int stfsm_remove(struct platform_device *pdev)
  {
  	struct stfsm *fsm = platform_get_drvdata(pdev);
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
2005

d9ba46d32   Lee Jones   mtd: st_spi_fsm: ...
2006
  	return mtd_device_unregister(&fsm->mtd);
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
2007
  }
aeea6eb4c   Jingoo Han   mtd: st_spi_fsm: ...
2008
  static const struct of_device_id stfsm_match[] = {
d90db4a07   Lee Jones   mtd: st_spi_fsm: ...
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
  	{ .compatible = "st,spi-fsm", },
  	{},
  };
  MODULE_DEVICE_TABLE(of, stfsm_match);
  
  static struct platform_driver stfsm_driver = {
  	.probe		= stfsm_probe,
  	.remove		= stfsm_remove,
  	.driver		= {
  		.name	= "st-spi-fsm",
  		.owner	= THIS_MODULE,
  		.of_match_table = stfsm_match,
  	},
  };
  module_platform_driver(stfsm_driver);
  
  MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
  MODULE_DESCRIPTION("ST SPI FSM driver");
  MODULE_LICENSE("GPL");