1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
11 #include <linux/types.h>
12 #include <linux/uio.h>
13 #include <linux/notifier.h>
14 #include <linux/device.h>
16 #include <mtd/mtd-abi.h>
18 #include <asm/div64.h>
20 #include <linux/compat.h>
21 #include <mtd/mtd-abi.h>
22 #include <linux/errno.h>
25 #define MAX_MTD_DEVICES 32
28 #define MTD_ERASE_PENDING 0x01
29 #define MTD_ERASING 0x02
30 #define MTD_ERASE_SUSPEND 0x04
31 #define MTD_ERASE_DONE 0x08
32 #define MTD_ERASE_FAILED 0x10
34 #define MTD_FAIL_ADDR_UNKNOWN -1LL
37 * If the erase fails, fail_addr might indicate exactly which block failed. If
38 * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
39 * or was not specific to any particular block.
50 void (*callback) (struct erase_info *self);
53 struct erase_info *next;
57 struct mtd_erase_region_info {
58 uint64_t offset; /* At which this region starts, from the beginning of the MTD */
59 uint32_t erasesize; /* For this region */
60 uint32_t numblocks; /* Number of blocks of erasesize in this region */
61 unsigned long *lockmap; /* If keeping bitmap of locks */
65 * struct mtd_oob_ops - oob operation operands
66 * @mode: operation mode
68 * @len: number of data bytes to write/read
70 * @retlen: number of data bytes written/read
72 * @ooblen: number of oob bytes to write/read
73 * @oobretlen: number of oob bytes written/read
74 * @ooboffs: offset of oob data in the oob area (only relevant when
75 * mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
76 * @datbuf: data buffer - if NULL only oob data are read/written
77 * @oobbuf: oob data buffer
90 #ifdef CONFIG_SYS_NAND_MAX_OOBFREE
91 #define MTD_MAX_OOBFREE_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_OOBFREE
93 #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32
96 #ifdef CONFIG_SYS_NAND_MAX_ECCPOS
97 #define MTD_MAX_ECCPOS_ENTRIES_LARGE CONFIG_SYS_NAND_MAX_ECCPOS
99 #define MTD_MAX_ECCPOS_ENTRIES_LARGE 680
102 * struct mtd_oob_region - oob region definition
103 * @offset: region offset
104 * @length: region length
106 * This structure describes a region of the OOB area, and is used
107 * to retrieve ECC or free bytes sections.
108 * Each section is defined by an offset within the OOB area and a
111 struct mtd_oob_region {
117 * struct mtd_ooblayout_ops - NAND OOB layout operations
118 * @ecc: function returning an ECC region in the OOB area.
119 * Should return -ERANGE if %section exceeds the total number of
121 * @free: function returning a free region in the OOB area.
122 * Should return -ERANGE if %section exceeds the total number of
125 struct mtd_ooblayout_ops {
126 int (*ecc)(struct mtd_info *mtd, int section,
127 struct mtd_oob_region *oobecc);
128 int (*free)(struct mtd_info *mtd, int section,
129 struct mtd_oob_region *oobfree);
133 * Internal ECC layout control structure. For historical reasons, there is a
134 * similar, smaller struct nand_ecclayout_user (in mtd-abi.h) that is retained
135 * for export to user-space via the ECCGETLAYOUT ioctl.
136 * nand_ecclayout should be expandable in the future simply by the above macros.
138 struct nand_ecclayout {
140 __u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
142 struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
145 struct module; /* only needed for owner field in mtd_info */
150 uint64_t size; // Total size of the MTD
152 /* "Major" erase size for the device. Naïve users may take this
153 * to be the only erase size available, or may use the more detailed
154 * information below if they desire
157 /* Minimal writable flash unit size. In case of NOR flash it is 1 (even
158 * though individual bits can be cleared), in case of NAND flash it is
159 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
160 * it is of ECC block size, etc. It is illegal to have writesize = 0.
161 * Any driver registering a struct mtd_info must ensure a writesize of
167 * Size of the write buffer used by the MTD. MTD devices having a write
168 * buffer can write multiple writesize chunks at a time. E.g. while
169 * writing 4 * writesize bytes to a device with 2 * writesize bytes
170 * buffer the MTD driver can (but doesn't have to) do 2 writesize
171 * operations, but not 4. Currently, all NANDs have writebufsize
172 * equivalent to writesize (NAND page size). Some NOR flashes do have
173 * writebufsize greater than writesize.
175 uint32_t writebufsize;
177 uint32_t oobsize; // Amount of OOB data per block (e.g. 16)
178 uint32_t oobavail; // Available OOB bytes per block
181 * If erasesize is a power of 2 then the shift is stored in
182 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
184 unsigned int erasesize_shift;
185 unsigned int writesize_shift;
186 /* Masks based on erasesize_shift and writesize_shift */
187 unsigned int erasesize_mask;
188 unsigned int writesize_mask;
191 * read ops return -EUCLEAN if max number of bitflips corrected on any
192 * one region comprising an ecc step equals or exceeds this value.
193 * Settable by driver, else defaults to ecc_strength. User can override
194 * in sysfs. N.B. The meaning of the -EUCLEAN return code has changed;
195 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
197 unsigned int bitflip_threshold;
199 // Kernel-only stuff starts here.
207 /* OOB layout description */
208 const struct mtd_ooblayout_ops *ooblayout;
210 /* ECC layout structure pointer - read only! */
211 struct nand_ecclayout *ecclayout;
213 /* the ecc step size. */
214 unsigned int ecc_step_size;
216 /* max number of correctible bit errors per ecc step */
217 unsigned int ecc_strength;
219 /* Data for variable erase regions. If numeraseregions is zero,
220 * it means that the whole device has erasesize as given above.
223 struct mtd_erase_region_info *eraseregions;
226 * Do not call via these pointers, use corresponding mtd_*()
229 int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
231 int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
232 size_t *retlen, void **virt, resource_size_t *phys);
233 int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
235 unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
237 unsigned long offset,
238 unsigned long flags);
239 int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
240 size_t *retlen, u_char *buf);
241 int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
242 size_t *retlen, const u_char *buf);
243 int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
244 size_t *retlen, const u_char *buf);
245 int (*_read_oob) (struct mtd_info *mtd, loff_t from,
246 struct mtd_oob_ops *ops);
247 int (*_write_oob) (struct mtd_info *mtd, loff_t to,
248 struct mtd_oob_ops *ops);
249 int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
250 size_t *retlen, struct otp_info *buf);
251 int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
252 size_t len, size_t *retlen, u_char *buf);
253 int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
254 size_t *retlen, struct otp_info *buf);
255 int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
256 size_t len, size_t *retlen, u_char *buf);
257 int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
258 size_t len, size_t *retlen, u_char *buf);
259 int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
262 int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
263 unsigned long count, loff_t to, size_t *retlen);
265 void (*_sync) (struct mtd_info *mtd);
266 int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
267 int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
268 int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
269 int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
270 int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
271 int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
273 int (*_suspend) (struct mtd_info *mtd);
274 void (*_resume) (struct mtd_info *mtd);
275 void (*_reboot) (struct mtd_info *mtd);
278 * If the driver is something smart, like UBI, it may need to maintain
279 * its own reference counting. The below functions are only for driver.
281 int (*_get_device) (struct mtd_info *mtd);
282 void (*_put_device) (struct mtd_info *mtd);
285 /* Backing device capabilities for this device
286 * - provides mmap capabilities
288 struct backing_dev_info *backing_dev_info;
290 struct notifier_block reboot_notifier; /* default mode before reboot */
293 /* ECC status information */
294 struct mtd_ecc_stats ecc_stats;
295 /* Subpage shift (NAND) */
300 struct module *owner;
309 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
310 struct mtd_oob_region *oobecc);
311 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
313 struct mtd_oob_region *oobregion);
314 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
315 const u8 *oobbuf, int start, int nbytes);
316 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
317 u8 *oobbuf, int start, int nbytes);
318 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
319 struct mtd_oob_region *oobfree);
320 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
321 const u8 *oobbuf, int start, int nbytes);
322 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
323 u8 *oobbuf, int start, int nbytes);
324 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
325 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
327 static inline void mtd_set_ooblayout(struct mtd_info *mtd,
328 const struct mtd_ooblayout_ops *ooblayout)
330 mtd->ooblayout = ooblayout;
333 static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
335 return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
338 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
340 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
341 void **virt, resource_size_t *phys);
342 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
344 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
345 unsigned long offset, unsigned long flags);
346 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
348 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
350 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
353 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
354 int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
356 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
357 struct otp_info *buf);
358 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
359 size_t *retlen, u_char *buf);
360 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
361 struct otp_info *buf);
362 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
363 size_t *retlen, u_char *buf);
364 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
365 size_t *retlen, u_char *buf);
366 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
369 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
370 unsigned long count, loff_t to, size_t *retlen);
373 static inline void mtd_sync(struct mtd_info *mtd)
379 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
380 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
381 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
382 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
383 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
384 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
387 static inline int mtd_suspend(struct mtd_info *mtd)
389 return mtd->_suspend ? mtd->_suspend(mtd) : 0;
392 static inline void mtd_resume(struct mtd_info *mtd)
399 static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
401 if (mtd->erasesize_shift)
402 return sz >> mtd->erasesize_shift;
403 do_div(sz, mtd->erasesize);
407 static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
409 if (mtd->erasesize_shift)
410 return sz & mtd->erasesize_mask;
411 return do_div(sz, mtd->erasesize);
414 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
416 if (mtd->writesize_shift)
417 return sz >> mtd->writesize_shift;
418 do_div(sz, mtd->writesize);
422 static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
424 if (mtd->writesize_shift)
425 return sz & mtd->writesize_mask;
426 return do_div(sz, mtd->writesize);
429 static inline int mtd_has_oob(const struct mtd_info *mtd)
431 return mtd->_read_oob && mtd->_write_oob;
434 static inline int mtd_type_is_nand(const struct mtd_info *mtd)
436 return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
439 static inline int mtd_can_have_bb(const struct mtd_info *mtd)
441 return !!mtd->_block_isbad;
444 /* Kernel-side ioctl definitions */
446 struct mtd_partition;
447 struct mtd_part_parser_data;
449 extern int mtd_device_parse_register(struct mtd_info *mtd,
450 const char * const *part_probe_types,
451 struct mtd_part_parser_data *parser_data,
452 const struct mtd_partition *defparts,
454 #define mtd_device_register(master, parts, nr_parts) \
455 mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
456 extern int mtd_device_unregister(struct mtd_info *master);
457 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
458 extern int __get_mtd_device(struct mtd_info *mtd);
459 extern void __put_mtd_device(struct mtd_info *mtd);
460 extern struct mtd_info *get_mtd_device_nm(const char *name);
461 extern void put_mtd_device(struct mtd_info *mtd);
465 struct mtd_notifier {
466 void (*add)(struct mtd_info *mtd);
467 void (*remove)(struct mtd_info *mtd);
468 struct list_head list;
472 extern void register_mtd_user (struct mtd_notifier *new);
473 extern int unregister_mtd_user (struct mtd_notifier *old);
475 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
477 #ifdef CONFIG_MTD_PARTITIONS
478 void mtd_erase_callback(struct erase_info *instr);
480 static inline void mtd_erase_callback(struct erase_info *instr)
483 instr->callback(instr);
487 static inline int mtd_is_bitflip(int err) {
488 return err == -EUCLEAN;
491 static inline int mtd_is_eccerr(int err) {
492 return err == -EBADMSG;
495 static inline int mtd_is_bitflip_or_eccerr(int err) {
496 return mtd_is_bitflip(err) || mtd_is_eccerr(err);
499 unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
502 /* drivers/mtd/mtdcore.h */
503 int add_mtd_device(struct mtd_info *mtd);
504 int del_mtd_device(struct mtd_info *mtd);
505 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
506 int del_mtd_partitions(struct mtd_info *);
508 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
509 loff_t *maxsize, int devtype, uint64_t chipsize);
510 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
511 loff_t *size, loff_t *maxsize, int devtype,
514 /* drivers/mtd/mtdcore.c */
515 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
516 const uint64_t length, uint64_t *len_incl_bad,
519 #endif /* __MTD_MTD_H__ */