1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2005, Intec Automation Inc.
4 * Copyright (C) 2014, Freescale Semiconductor, Inc.
7 #ifndef __LINUX_MTD_SPI_NOR_INTERNAL_H
8 #define __LINUX_MTD_SPI_NOR_INTERNAL_H
12 #define SPI_NOR_MAX_ID_LEN 6
14 enum 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),
34 struct spi_nor_read_command {
38 enum spi_nor_protocol proto;
41 struct spi_nor_pp_command {
43 enum spi_nor_protocol proto;
46 enum spi_nor_read_command_index {
49 SNOR_CMD_READ_1_1_1_DTR,
55 SNOR_CMD_READ_1_2_2_DTR,
61 SNOR_CMD_READ_1_4_4_DTR,
67 SNOR_CMD_READ_1_8_8_DTR,
68 SNOR_CMD_READ_8_8_8_DTR,
73 enum spi_nor_pp_command_index {
85 SNOR_CMD_PP_8_8_8_DTR,
91 * struct spi_nor_erase_type - Structure to describe a SPI NOR erase type
92 * @size: the size of the sector/block erased by the erase type.
93 * JEDEC JESD216B imposes erase sizes to be a power of 2.
94 * @size_shift: @size is a power of 2, the shift is stored in
96 * @size_mask: the size mask based on @size_shift.
97 * @opcode: the SPI command op code to erase the sector/block.
98 * @idx: Erase Type index as sorted in the Basic Flash Parameter
99 * Table. It will be used to synchronize the supported
100 * Erase Types with the ones identified in the SFDP
103 struct spi_nor_erase_type {
112 * struct spi_nor_erase_command - Used for non-uniform erases
113 * The structure is used to describe a list of erase commands to be executed
114 * once we validate that the erase can be performed. The elements in the list
115 * are run-length encoded.
116 * @list: for inclusion into the list of erase commands.
117 * @count: how many times the same erase command should be
118 * consecutively used.
119 * @size: the size of the sector/block erased by the command.
120 * @opcode: the SPI command op code to erase the sector/block.
122 struct spi_nor_erase_command {
123 struct list_head list;
130 * struct spi_nor_erase_region - Structure to describe a SPI NOR erase region
131 * @offset: the offset in the data array of erase region start.
132 * LSB bits are used as a bitmask encoding flags to
133 * determine if this region is overlaid, if this region is
134 * the last in the SPI NOR flash memory and to indicate
135 * all the supported erase commands inside this region.
136 * The erase types are sorted in ascending order with the
137 * smallest Erase Type size being at BIT(0).
138 * @size: the size of the region in bytes.
140 struct spi_nor_erase_region {
145 #define SNOR_ERASE_TYPE_MAX 4
146 #define SNOR_ERASE_TYPE_MASK GENMASK_ULL(SNOR_ERASE_TYPE_MAX - 1, 0)
148 #define SNOR_LAST_REGION BIT(4)
149 #define SNOR_OVERLAID_REGION BIT(5)
151 #define SNOR_ERASE_FLAGS_MAX 6
152 #define SNOR_ERASE_FLAGS_MASK GENMASK_ULL(SNOR_ERASE_FLAGS_MAX - 1, 0)
155 * struct spi_nor_erase_map - Structure to describe the SPI NOR erase map
156 * @regions: array of erase regions. The regions are consecutive in
157 * address space. Walking through the regions is done
159 * @uniform_region: a pre-allocated erase region for SPI NOR with a uniform
160 * sector size (legacy implementation).
161 * @erase_type: an array of erase types shared by all the regions.
162 * The erase types are sorted in ascending order, with the
163 * smallest Erase Type size being the first member in the
165 * @uniform_erase_type: bitmask encoding erase types that can erase the
166 * entire memory. This member is completed at init by
167 * uniform and non-uniform SPI NOR flash memories if they
168 * support at least one erase type that can erase the
171 struct 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;
179 * struct spi_nor_locking_ops - SPI NOR locking methods
180 * @lock: lock a region of the SPI NOR.
181 * @unlock: unlock a region of the SPI NOR.
182 * @is_locked: check if a region of the SPI NOR is completely locked
184 struct 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);
191 * struct spi_nor_otp_organization - Structure to describe the SPI NOR OTP regions
192 * @len: size of one OTP region in bytes.
193 * @base: start address of the OTP area.
194 * @offset: offset between consecutive OTP regions if there are more
196 * @n_regions: number of individual OTP regions.
198 struct spi_nor_otp_organization {
202 unsigned int n_regions;
206 * struct spi_nor_otp_ops - SPI NOR OTP methods
207 * @read: read from the SPI NOR OTP area.
208 * @write: write to the SPI NOR OTP area.
209 * @lock: lock an OTP region.
210 * @erase: erase an OTP region.
211 * @is_locked: check if an OTP region of the SPI NOR is locked.
213 struct 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,
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);
223 * struct spi_nor_otp - SPI NOR OTP grouping structure
224 * @org: OTP region organization
225 * @ops: OTP access ops
228 const struct spi_nor_otp_organization *org;
229 const struct spi_nor_otp_ops *ops;
233 * struct spi_nor_flash_parameter - SPI NOR flash parameters and settings.
234 * Includes legacy flash parameters and settings that can be overwritten
235 * by the spi_nor_fixups hooks, or dynamically when parsing the JESD216
236 * Serial Flash Discoverable Parameters (SFDP) tables.
238 * @size: the flash memory density in bytes.
239 * @writesize Minimal writable flash unit size. Defaults to 1. Set to
240 * ECC unit size for ECC-ed flashes.
241 * @page_size: the page size of the SPI NOR flash memory.
242 * @rdsr_dummy: dummy cycles needed for Read Status Register command.
243 * @rdsr_addr_nbytes: dummy address bytes needed for Read Status Register
245 * @hwcaps: describes the read and page program hardware
247 * @reads: read capabilities ordered by priority: the higher index
248 * in the array, the higher priority.
249 * @page_programs: page program capabilities ordered by priority: the
250 * higher index in the array, the higher priority.
251 * @erase_map: the erase map parsed from the SFDP Sector Map Parameter
253 * @otp_info: describes the OTP regions.
254 * @octal_dtr_enable: enables SPI NOR octal DTR mode.
255 * @quad_enable: enables SPI NOR quad mode.
256 * @set_4byte_addr_mode: puts the SPI NOR in 4 byte addressing mode.
257 * @convert_addr: converts an absolute address into something the flash
258 * will understand. Particularly useful when pagesize is
260 * @setup: configures the SPI NOR memory. Useful for SPI NOR
261 * flashes that have peculiarities to the SPI NOR standard
262 * e.g. different opcodes, specific address calculation,
264 * @locking_ops: SPI NOR locking methods.
265 * @otp: SPI NOR OTP methods.
267 struct spi_nor_flash_parameter {
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];
278 struct spi_nor_erase_map erase_map;
279 struct spi_nor_otp otp;
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);
287 const struct spi_nor_locking_ops *locking_ops;
291 * struct spi_nor_fixups - SPI NOR fixup hooks
292 * @default_init: called after default flash parameters init. Used to tweak
293 * flash parameters when information provided by the flash_info
294 * table is incomplete or wrong.
295 * @post_bfpt: called after the BFPT table has been parsed
296 * @post_sfdp: called after SFDP has been parsed (is also called for SPI NORs
297 * that do not support RDSFDP). Typically used to tweak various
298 * parameters that could not be extracted by other means (i.e.
299 * when information provided by the SFDP/flash_info tables are
300 * incomplete or wrong).
302 * Those hooks can be used to tweak the SPI NOR configuration when the SFDP
303 * table is broken or not available.
305 struct 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);
317 * This array stores the ID bytes.
318 * The first three bytes are the JEDIC ID.
319 * JEDEC ID zero means "no ID" (mostly older chips).
321 u8 id[SPI_NOR_MAX_ID_LEN];
324 /* The size listed here is what works with SPINOR_OP_SE, which isn't
325 * necessarily called a "sector" by the vendor.
327 unsigned sector_size;
334 #define SECT_4K BIT(0) /* SPINOR_OP_BE_4K works uniformly */
335 #define SPI_NOR_NO_ERASE BIT(1) /* No erase command needed */
336 #define SST_WRITE BIT(2) /* use SST byte programming */
337 #define SPI_NOR_NO_FR BIT(3) /* Can't do fastread */
338 #define SECT_4K_PMC BIT(4) /* SPINOR_OP_BE_4K_PMC works uniformly */
339 #define SPI_NOR_DUAL_READ BIT(5) /* Flash supports Dual Read */
340 #define SPI_NOR_QUAD_READ BIT(6) /* Flash supports Quad Read */
341 #define USE_FSR BIT(7) /* use flag status register */
342 #define SPI_NOR_HAS_LOCK BIT(8) /* Flash supports lock/unlock via SR */
343 #define SPI_NOR_HAS_TB BIT(9) /*
344 * Flash SR has Top/Bottom (TB) protect
345 * bit. Must be used with
348 #define SPI_NOR_XSR_RDY BIT(10) /*
349 * S3AN flashes have specific opcode to
350 * read the status register.
352 #define SPI_NOR_4B_OPCODES BIT(11) /*
353 * Use dedicated 4byte address op codes
354 * to support memory size above 128Mib.
356 #define NO_CHIP_ERASE BIT(12) /* Chip does not support chip erase */
357 #define SPI_NOR_SKIP_SFDP BIT(13) /* Skip parsing of SFDP tables */
358 #define USE_CLSR BIT(14) /* use CLSR command */
359 #define SPI_NOR_OCTAL_READ BIT(15) /* Flash supports Octal Read */
360 #define SPI_NOR_TB_SR_BIT6 BIT(16) /*
361 * Top/Bottom (TB) is bit 6 of
362 * status register. Must be used with
365 #define SPI_NOR_4BIT_BP BIT(17) /*
366 * Flash SR has 4 bit fields (BP0-3)
367 * for block protection.
369 #define SPI_NOR_BP3_SR_BIT6 BIT(18) /*
370 * BP3 is bit 6 of status register.
371 * Must be used with SPI_NOR_4BIT_BP.
373 #define SPI_NOR_OCTAL_DTR_READ BIT(19) /* Flash supports octal DTR Read. */
374 #define SPI_NOR_OCTAL_DTR_PP BIT(20) /* Flash supports Octal DTR Page Program */
375 #define SPI_NOR_IO_MODE_EN_VOLATILE BIT(21) /*
376 * Flash enables the best
377 * available I/O mode via a
380 #define SPI_NOR_SWP_IS_VOLATILE BIT(22) /*
381 * Flash has volatile software write
382 * protection bits. Usually these will
383 * power-up in a write-protected state.
386 const struct spi_nor_otp_organization otp_org;
388 /* Part specific fixup hooks. */
389 const struct spi_nor_fixups *fixups;
392 /* Used when the "_ext_id" is two bytes at most */
393 #define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
395 ((_jedec_id) >> 16) & 0xff, \
396 ((_jedec_id) >> 8) & 0xff, \
397 (_jedec_id) & 0xff, \
398 ((_ext_id) >> 8) & 0xff, \
401 .id_len = (!(_jedec_id) ? 0 : (3 + ((_ext_id) ? 2 : 0))), \
402 .sector_size = (_sector_size), \
403 .n_sectors = (_n_sectors), \
407 #define INFO6(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
409 ((_jedec_id) >> 16) & 0xff, \
410 ((_jedec_id) >> 8) & 0xff, \
411 (_jedec_id) & 0xff, \
412 ((_ext_id) >> 16) & 0xff, \
413 ((_ext_id) >> 8) & 0xff, \
417 .sector_size = (_sector_size), \
418 .n_sectors = (_n_sectors), \
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), \
429 #define S3AN_INFO(_jedec_id, _n_sectors, _page_size) \
431 ((_jedec_id) >> 16) & 0xff, \
432 ((_jedec_id) >> 8) & 0xff, \
436 .sector_size = (8*_page_size), \
437 .n_sectors = (_n_sectors), \
438 .page_size = _page_size, \
440 .flags = SPI_NOR_NO_FR | SPI_NOR_XSR_RDY,
442 #define OTP_INFO(_len, _n_regions, _base, _offset) \
446 .offset = (_offset), \
447 .n_regions = (_n_regions), \
451 * struct spi_nor_manufacturer - SPI NOR manufacturer object
452 * @name: manufacturer name
453 * @parts: array of parts supported by this manufacturer
454 * @nparts: number of entries in the parts array
455 * @fixups: hooks called at various points in time during spi_nor_scan()
457 struct spi_nor_manufacturer {
459 const struct flash_info *parts;
461 const struct spi_nor_fixups *fixups;
465 * struct sfdp - SFDP data
466 * @num_dwords: number of entries in the dwords array
467 * @dwords: array of double words of the SFDP data
474 /* Manufacturer drivers. */
475 extern const struct spi_nor_manufacturer spi_nor_atmel;
476 extern const struct spi_nor_manufacturer spi_nor_catalyst;
477 extern const struct spi_nor_manufacturer spi_nor_eon;
478 extern const struct spi_nor_manufacturer spi_nor_esmt;
479 extern const struct spi_nor_manufacturer spi_nor_everspin;
480 extern const struct spi_nor_manufacturer spi_nor_fujitsu;
481 extern const struct spi_nor_manufacturer spi_nor_gigadevice;
482 extern const struct spi_nor_manufacturer spi_nor_intel;
483 extern const struct spi_nor_manufacturer spi_nor_issi;
484 extern const struct spi_nor_manufacturer spi_nor_macronix;
485 extern const struct spi_nor_manufacturer spi_nor_micron;
486 extern const struct spi_nor_manufacturer spi_nor_st;
487 extern const struct spi_nor_manufacturer spi_nor_spansion;
488 extern const struct spi_nor_manufacturer spi_nor_sst;
489 extern const struct spi_nor_manufacturer spi_nor_winbond;
490 extern const struct spi_nor_manufacturer spi_nor_xilinx;
491 extern const struct spi_nor_manufacturer spi_nor_xmc;
493 extern const struct attribute_group *spi_nor_sysfs_groups[];
495 void spi_nor_spimem_setup_op(const struct spi_nor *nor,
496 struct spi_mem_op *op,
497 const enum spi_nor_protocol proto);
498 int spi_nor_write_enable(struct spi_nor *nor);
499 int spi_nor_write_disable(struct spi_nor *nor);
500 int spi_nor_set_4byte_addr_mode(struct spi_nor *nor, bool enable);
501 int spi_nor_write_ear(struct spi_nor *nor, u8 ear);
502 int spi_nor_wait_till_ready(struct spi_nor *nor);
503 int spi_nor_global_block_unlock(struct spi_nor *nor);
504 int spi_nor_lock_and_prep(struct spi_nor *nor);
505 void spi_nor_unlock_and_unprep(struct spi_nor *nor);
506 int spi_nor_sr1_bit6_quad_enable(struct spi_nor *nor);
507 int spi_nor_sr2_bit1_quad_enable(struct spi_nor *nor);
508 int spi_nor_sr2_bit7_quad_enable(struct spi_nor *nor);
509 int spi_nor_read_sr(struct spi_nor *nor, u8 *sr);
510 int spi_nor_read_cr(struct spi_nor *nor, u8 *cr);
511 int spi_nor_write_sr(struct spi_nor *nor, const u8 *sr, size_t len);
512 int spi_nor_write_sr_and_check(struct spi_nor *nor, u8 sr1);
513 int spi_nor_write_16bit_cr_and_check(struct spi_nor *nor, u8 cr);
515 int spi_nor_xread_sr(struct spi_nor *nor, u8 *sr);
516 ssize_t spi_nor_read_data(struct spi_nor *nor, loff_t from, size_t len,
518 ssize_t spi_nor_write_data(struct spi_nor *nor, loff_t to, size_t len,
520 int spi_nor_erase_sector(struct spi_nor *nor, u32 addr);
522 int spi_nor_otp_read_secr(struct spi_nor *nor, loff_t addr, size_t len, u8 *buf);
523 int spi_nor_otp_write_secr(struct spi_nor *nor, loff_t addr, size_t len,
525 int spi_nor_otp_erase_secr(struct spi_nor *nor, loff_t addr);
526 int spi_nor_otp_lock_sr2(struct spi_nor *nor, unsigned int region);
527 int spi_nor_otp_is_locked_sr2(struct spi_nor *nor, unsigned int region);
529 int spi_nor_hwcaps_read2cmd(u32 hwcaps);
530 u8 spi_nor_convert_3to4_read(u8 opcode);
531 void spi_nor_set_read_settings(struct spi_nor_read_command *read,
535 enum spi_nor_protocol proto);
536 void spi_nor_set_pp_settings(struct spi_nor_pp_command *pp, u8 opcode,
537 enum spi_nor_protocol proto);
539 void spi_nor_set_erase_type(struct spi_nor_erase_type *erase, u32 size,
541 struct spi_nor_erase_region *
542 spi_nor_region_next(struct spi_nor_erase_region *region);
543 void spi_nor_init_uniform_erase_map(struct spi_nor_erase_map *map,
544 u8 erase_mask, u64 flash_size);
546 int spi_nor_post_bfpt_fixups(struct spi_nor *nor,
547 const struct sfdp_parameter_header *bfpt_header,
548 const struct sfdp_bfpt *bfpt);
550 void spi_nor_init_default_locking_ops(struct spi_nor *nor);
551 void spi_nor_try_unlock_all(struct spi_nor *nor);
552 void spi_nor_register_locking_ops(struct spi_nor *nor);
553 void spi_nor_otp_init(struct spi_nor *nor);
555 static struct spi_nor __maybe_unused *mtd_to_spi_nor(struct mtd_info *mtd)
560 #endif /* __LINUX_MTD_SPI_NOR_INTERNAL_H */