1
2
3
4
5
6
7#ifndef __LINUX_MTD_SPI_NOR_INTERNAL_H
8#define __LINUX_MTD_SPI_NOR_INTERNAL_H
9
10#include "sfdp.h"
11
12#define SPI_NOR_MAX_ID_LEN 6
13
14enum spi_nor_option_flags {
15 SNOR_F_USE_FSR = BIT(0),
16 SNOR_F_HAS_SR_TB = BIT(1),
17 SNOR_F_NO_OP_CHIP_ERASE = BIT(2),
18 SNOR_F_READY_XSR_RDY = BIT(3),
19 SNOR_F_USE_CLSR = BIT(4),
20 SNOR_F_BROKEN_RESET = BIT(5),
21 SNOR_F_4B_OPCODES = BIT(6),
22 SNOR_F_HAS_4BAIT = BIT(7),
23 SNOR_F_HAS_LOCK = BIT(8),
24 SNOR_F_HAS_16BIT_SR = BIT(9),
25 SNOR_F_NO_READ_CR = BIT(10),
26 SNOR_F_HAS_SR_TB_BIT6 = BIT(11),
27 SNOR_F_HAS_4BIT_BP = BIT(12),
28 SNOR_F_HAS_SR_BP3_BIT6 = BIT(13),
29 SNOR_F_IO_MODE_EN_VOLATILE = BIT(14),
30 SNOR_F_SOFT_RESET = BIT(15),
31 SNOR_F_SWP_IS_VOLATILE = BIT(16),
32};
33
34struct spi_nor_read_command {
35 u8 num_mode_clocks;
36 u8 num_wait_states;
37 u8 opcode;
38 enum spi_nor_protocol proto;
39};
40
41struct spi_nor_pp_command {
42 u8 opcode;
43 enum spi_nor_protocol proto;
44};
45
46enum spi_nor_read_command_index {
47 SNOR_CMD_READ,
48 SNOR_CMD_READ_FAST,
49 SNOR_CMD_READ_1_1_1_DTR,
50
51
52 SNOR_CMD_READ_1_1_2,
53 SNOR_CMD_READ_1_2_2,
54 SNOR_CMD_READ_2_2_2,
55 SNOR_CMD_READ_1_2_2_DTR,
56
57
58 SNOR_CMD_READ_1_1_4,
59 SNOR_CMD_READ_1_4_4,
60 SNOR_CMD_READ_4_4_4,
61 SNOR_CMD_READ_1_4_4_DTR,
62
63
64 SNOR_CMD_READ_1_1_8,
65 SNOR_CMD_READ_1_8_8,
66 SNOR_CMD_READ_8_8_8,
67 SNOR_CMD_READ_1_8_8_DTR,
68 SNOR_CMD_READ_8_8_8_DTR,
69
70 SNOR_CMD_READ_MAX
71};
72
73enum spi_nor_pp_command_index {
74 SNOR_CMD_PP,
75
76
77 SNOR_CMD_PP_1_1_4,
78 SNOR_CMD_PP_1_4_4,
79 SNOR_CMD_PP_4_4_4,
80
81
82 SNOR_CMD_PP_1_1_8,
83 SNOR_CMD_PP_1_8_8,
84 SNOR_CMD_PP_8_8_8,
85 SNOR_CMD_PP_8_8_8_DTR,
86
87 SNOR_CMD_PP_MAX
88};
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103struct spi_nor_erase_type {
104 u32 size;
105 u32 size_shift;
106 u32 size_mask;
107 u8 opcode;
108 u8 idx;
109};
110
111
112
113
114
115
116
117
118
119
120
121
122struct spi_nor_erase_command {
123 struct list_head list;
124 u32 count;
125 u32 size;
126 u8 opcode;
127};
128
129
130
131
132
133
134
135
136
137
138
139
140struct spi_nor_erase_region {
141 u64 offset;
142 u64 size;
143};
144
145#define SNOR_ERASE_TYPE_MAX 4
146#define SNOR_ERASE_TYPE_MASK GENMASK_ULL(SNOR_ERASE_TYPE_MAX - 1, 0)
147
148#define SNOR_LAST_REGION BIT(4)
149#define SNOR_OVERLAID_REGION BIT(5)
150
151#define SNOR_ERASE_FLAGS_MAX 6
152#define SNOR_ERASE_FLAGS_MASK GENMASK_ULL(SNOR_ERASE_FLAGS_MAX - 1, 0)
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171struct spi_nor_erase_map {
172 struct spi_nor_erase_region *regions;
173 struct spi_nor_erase_region uniform_region;
174 struct spi_nor_erase_type erase_type[SNOR_ERASE_TYPE_MAX];
175 u8 uniform_erase_type;
176};
177
178
179
180
181
182
183
184struct spi_nor_locking_ops {
185 int (*lock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
186 int (*unlock)(struct spi_nor *nor, loff_t ofs, uint64_t len);
187 int (*is_locked)(struct spi_nor *nor, loff_t ofs, uint64_t len);
188};
189
190
191
192
193
194
195
196
197
198struct spi_nor_otp_organization {
199 size_t len;
200 loff_t base;
201 loff_t offset;
202 unsigned int n_regions;
203};
204
205
206
207
208
209
210
211
212
213struct spi_nor_otp_ops {
214 int (*read)(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
215 int (*write)(struct spi_nor *nor, loff_t addr, size_t len,
216 const u8 *buf);
217 int (*lock)(struct spi_nor *nor, unsigned int region);
218 int (*erase)(struct spi_nor *nor, loff_t addr);
219 int (*is_locked)(struct spi_nor *nor, unsigned int region);
220};
221
222
223
224
225
226
227struct spi_nor_otp {
228 const struct spi_nor_otp_organization *org;
229 const struct spi_nor_otp_ops *ops;
230};
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267struct spi_nor_flash_parameter {
268 u64 size;
269 u32 writesize;
270 u32 page_size;
271 u8 rdsr_dummy;
272 u8 rdsr_addr_nbytes;
273
274 struct spi_nor_hwcaps hwcaps;
275 struct spi_nor_read_command reads[SNOR_CMD_READ_MAX];
276 struct spi_nor_pp_command page_programs[SNOR_CMD_PP_MAX];
277
278 struct spi_nor_erase_map erase_map;
279 struct spi_nor_otp otp;
280
281 int (*octal_dtr_enable)(struct spi_nor *nor, bool enable);
282 int (*quad_enable)(struct spi_nor *nor);
283 int (*set_4byte_addr_mode)(struct spi_nor *nor, bool enable);
284 u32 (*convert_addr)(struct spi_nor *nor, u32 addr);
285 int (*setup)(struct spi_nor *nor, const struct spi_nor_hwcaps *hwcaps);
286
287 const struct spi_nor_locking_ops *locking_ops;
288};
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305struct spi_nor_fixups {
306 void (*default_init)(struct spi_nor *nor);
307 int (*post_bfpt)(struct spi_nor *nor,
308 const struct sfdp_parameter_header *bfpt_header,
309 const struct sfdp_bfpt *bfpt);
310 void (*post_sfdp)(struct spi_nor *nor);
311};
312
313struct flash_info {
314 char *name;
315
316
317
318
319
320
321 u8 id[SPI_NOR_MAX_ID_LEN];
322 u8 id_len;
323
324
325
326
327 unsigned sector_size;
328 u16 n_sectors;
329
330 u16 page_size;
331 u16 addr_width;
332
333 u32 flags;
334#define SECT_4K BIT(0)
335#define SPI_NOR_NO_ERASE BIT(1)
336#define SST_WRITE BIT(2)
337#define SPI_NOR_NO_FR BIT(3)
338#define SECT_4K_PMC BIT(4)
339#define SPI_NOR_DUAL_READ BIT(5)
340#define SPI_NOR_QUAD_READ BIT(6)
341#define USE_FSR BIT(7)
342#define SPI_NOR_HAS_LOCK BIT(8)
343#define SPI_NOR_HAS_TB BIT(9)
344
345
346
347
348#define SPI_NOR_XSR_RDY BIT(10)
349
350
351
352#define SPI_NOR_4B_OPCODES BIT(11)
353
354
355
356#define NO_CHIP_ERASE BIT(12)
357#define SPI_NOR_SKIP_SFDP BIT(13)
358#define USE_CLSR BIT(14)
359#define SPI_NOR_OCTAL_READ BIT(15)
360#define SPI_NOR_TB_SR_BIT6 BIT(16)
361
362
363
364
365#define SPI_NOR_4BIT_BP BIT(17)
366
367
368
369#define SPI_NOR_BP3_SR_BIT6 BIT(18)
370
371
372
373#define SPI_NOR_OCTAL_DTR_READ BIT(19)
374#define SPI_NOR_OCTAL_DTR_PP BIT(20)
375#define SPI_NOR_IO_MODE_EN_VOLATILE BIT(21)
376
377
378
379
380#define SPI_NOR_SWP_IS_VOLATILE BIT(22)
381
382
383
384
385
386 const struct spi_nor_otp_organization otp_org;
387
388
389 const struct spi_nor_fixups *fixups;
390};
391
392
393#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
394 .id = { \
395 ((_jedec_id) >> 16) & 0xff, \
396 ((_jedec_id) >> 8) & 0xff, \
397 (_jedec_id) & 0xff, \
398 ((_ext_id) >> 8) & 0xff, \
399 (_ext_id) & 0xff, \
400 }, \
401 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
402 .sector_size = (_sector_size), \
403 .n_sectors = (_n_sectors), \
404 .page_size = 256, \
405 .flags = (_flags),
406
407#define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
408 .id = { \
409 ((_jedec_id) >> 16) & 0xff, \
410 ((_jedec_id) >> 8) & 0xff, \
411 (_jedec_id) & 0xff, \
412 ((_ext_id) >> 16) & 0xff, \
413 ((_ext_id) >> 8) & 0xff, \
414 (_ext_id) & 0xff, \
415 }, \
416 .id_len = 6, \
417 .sector_size = (_sector_size), \
418 .n_sectors = (_n_sectors), \
419 .page_size = 256, \
420 .flags = (_flags),
421
422#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
423 .sector_size = (_sector_size), \
424 .n_sectors = (_n_sectors), \
425 .page_size = (_page_size), \
426 .addr_width = (_addr_width), \
427 .flags = (_flags),
428
429#define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
430 .id = { \
431 ((_jedec_id) >> 16) & 0xff, \
432 ((_jedec_id) >> 8) & 0xff, \
433 (_jedec_id) & 0xff \
434 }, \
435 .id_len = 3, \
436 .sector_size = (8*_page_size), \
437 .n_sectors = (_n_sectors), \
438 .page_size = _page_size, \
439 .addr_width = 3, \
440 .flags = SPI_NOR_NO_FR | SPI_NOR_XSR_RDY,
441
442#define OTP_INFO(_len, _n_regions, _base, _offset) \
443 .otp_org = { \
444 .len = (_len), \
445 .base = (_base), \
446 .offset = (_offset), \
447 .n_regions = (_n_regions), \
448 },
449
450
451
452
453
454
455
456
457struct spi_nor_manufacturer {
458 const char *name;
459 const struct flash_info *parts;
460 unsigned int nparts;
461 const struct spi_nor_fixups *fixups;
462};
463
464
465
466
467
468
469struct sfdp {
470 size_t num_dwords;
471 u32 *dwords;
472};
473
474
475extern const struct spi_nor_manufacturer spi_nor_atmel;
476extern const struct spi_nor_manufacturer spi_nor_catalyst;
477extern const struct spi_nor_manufacturer spi_nor_eon;
478extern const struct spi_nor_manufacturer spi_nor_esmt;
479extern const struct spi_nor_manufacturer spi_nor_everspin;
480extern const struct spi_nor_manufacturer spi_nor_fujitsu;
481extern const struct spi_nor_manufacturer spi_nor_gigadevice;
482extern const struct spi_nor_manufacturer spi_nor_intel;
483extern const struct spi_nor_manufacturer spi_nor_issi;
484extern const struct spi_nor_manufacturer spi_nor_macronix;
485extern const struct spi_nor_manufacturer spi_nor_micron;
486extern const struct spi_nor_manufacturer spi_nor_st;
487extern const struct spi_nor_manufacturer spi_nor_spansion;
488extern const struct spi_nor_manufacturer spi_nor_sst;
489extern const struct spi_nor_manufacturer spi_nor_winbond;
490extern const struct spi_nor_manufacturer spi_nor_xilinx;
491extern const struct spi_nor_manufacturer spi_nor_xmc;
492
493extern const struct attribute_group *spi_nor_sysfs_groups[];
494
495void spi_nor_spimem_setup_op(const struct spi_nor *nor,
496 struct spi_mem_op *op,
497 const enum spi_nor_protocol proto);
498int spi_nor_write_enable(struct spi_nor *nor);
499int spi_nor_write_disable(struct spi_nor *nor);
500int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);
501int spi_nor_write_ear(struct spi_nor *nor, u8 ear);
502int spi_nor_wait_till_ready(struct spi_nor *nor);
503int spi_nor_global_block_unlock(struct spi_nor *nor);
504int spi_nor_lock_and_prep(struct spi_nor *nor);
505void spi_nor_unlock_and_unprep(struct spi_nor *nor);
506int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
507int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
508int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor);
509int spi_nor_read_sr(struct spi_nor *nor, u8 *sr);
510int spi_nor_read_cr(struct spi_nor *nor, u8 *cr);
511int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len);
512int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1);
513int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr);
514
515int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr);
516ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
517 u8 *buf);
518ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
519 const u8 *buf);
520int spi_nor_erase_sector(struct spi_nor *nor, u32 addr);
521
522int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
523int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
524 const u8 *buf);
525int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr);
526int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region);
527int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region);
528
529int spi_nor_hwcaps_read2cmd(u32 hwcaps);
530u8 spi_nor_convert_3to4_read(u8 opcode);
531void spi_nor_set_read_settings(struct spi_nor_read_command *read,
532 u8 num_mode_clocks,
533 u8 num_wait_states,
534 u8 opcode,
535 enum spi_nor_protocol proto);
536void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
537 enum spi_nor_protocol proto);
538
539void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
540 u8 opcode);
541struct spi_nor_erase_region *
542spi_nor_region_next(struct spi_nor_erase_region *region);
543void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
544 u8 erase_mask, u64 flash_size);
545
546int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
547 const struct sfdp_parameter_header *bfpt_header,
548 const struct sfdp_bfpt *bfpt);
549
550void spi_nor_init_default_locking_ops(struct spi_nor *nor);
551void spi_nor_try_unlock_all(struct spi_nor *nor);
552void spi_nor_register_locking_ops(struct spi_nor *nor);
553void spi_nor_otp_init(struct spi_nor *nor);
554
555static struct spi_nor __maybe_unused *mtd_to_spi_nor(struct mtd_info *mtd)
556{
557 return mtd->priv;
558}
559
560#endif
561