xref: /openbmc/u-boot/include/linux/mtd/mtd.h (revision 725e09b8)
1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
4  *
5  */
6 
7 #ifndef __MTD_MTD_H__
8 #define __MTD_MTD_H__
9 
10 #ifndef __UBOOT__
11 #include <linux/types.h>
12 #include <linux/uio.h>
13 #include <linux/notifier.h>
14 #include <linux/device.h>
15 
16 #include <mtd/mtd-abi.h>
17 
18 #include <asm/div64.h>
19 #else
20 #include <linux/compat.h>
21 #include <mtd/mtd-abi.h>
22 #include <linux/errno.h>
23 #include <div64.h>
24 
25 #define MAX_MTD_DEVICES 32
26 #endif
27 
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
33 
34 #define MTD_FAIL_ADDR_UNKNOWN -1LL
35 
36 /*
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.
40  */
41 struct erase_info {
42 	struct mtd_info *mtd;
43 	uint64_t addr;
44 	uint64_t len;
45 	uint64_t fail_addr;
46 	u_long time;
47 	u_long retries;
48 	unsigned dev;
49 	unsigned cell;
50 	void (*callback) (struct erase_info *self);
51 	u_long priv;
52 	u_char state;
53 	struct erase_info *next;
54 	int scrub;
55 };
56 
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 */
62 };
63 
64 /**
65  * struct mtd_oob_ops - oob operation operands
66  * @mode:	operation mode
67  *
68  * @len:	number of data bytes to write/read
69  *
70  * @retlen:	number of data bytes written/read
71  *
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
78  */
79 struct mtd_oob_ops {
80 	unsigned int	mode;
81 	size_t		len;
82 	size_t		retlen;
83 	size_t		ooblen;
84 	size_t		oobretlen;
85 	uint32_t	ooboffs;
86 	uint8_t		*datbuf;
87 	uint8_t		*oobbuf;
88 };
89 
90 #ifdef CONFIG_SYS_NAND_MAX_OOBFREE
91 #define MTD_MAX_OOBFREE_ENTRIES_LARGE	CONFIG_SYS_NAND_MAX_OOBFREE
92 #else
93 #define MTD_MAX_OOBFREE_ENTRIES_LARGE	32
94 #endif
95 
96 #ifdef CONFIG_SYS_NAND_MAX_ECCPOS
97 #define MTD_MAX_ECCPOS_ENTRIES_LARGE	CONFIG_SYS_NAND_MAX_ECCPOS
98 #else
99 #define MTD_MAX_ECCPOS_ENTRIES_LARGE	680
100 #endif
101 /**
102  * struct mtd_oob_region - oob region definition
103  * @offset: region offset
104  * @length: region length
105  *
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
109  * length.
110  */
111 struct mtd_oob_region {
112 	u32 offset;
113 	u32 length;
114 };
115 
116 /*
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
120  *	 ECC sections.
121  * @free: function returning a free region in the OOB area.
122  *	  Should return -ERANGE if %section exceeds the total number of
123  *	  free sections.
124  */
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);
130 };
131 
132 /*
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.
137  */
138 struct nand_ecclayout {
139 	__u32 eccbytes;
140 	__u32 eccpos[MTD_MAX_ECCPOS_ENTRIES_LARGE];
141 	__u32 oobavail;
142 	struct nand_oobfree oobfree[MTD_MAX_OOBFREE_ENTRIES_LARGE];
143 };
144 
145 struct module;	/* only needed for owner field in mtd_info */
146 
147 struct mtd_info {
148 	u_char type;
149 	uint32_t flags;
150 	uint64_t size;	 // Total size of the MTD
151 
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
155 	 */
156 	uint32_t erasesize;
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
162 	 * 1 or larger.
163 	 */
164 	uint32_t writesize;
165 
166 	/*
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.
174 	 */
175 	uint32_t writebufsize;
176 
177 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
178 	uint32_t oobavail;  // Available OOB bytes per block
179 
180 	/*
181 	 * If erasesize is a power of 2 then the shift is stored in
182 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
183 	 */
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;
189 
190 	/*
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.
196 	 */
197 	unsigned int bitflip_threshold;
198 
199 	// Kernel-only stuff starts here.
200 #ifndef __UBOOT__
201 	const char *name;
202 #else
203 	char *name;
204 #endif
205 	int index;
206 
207 	/* OOB layout description */
208 	const struct mtd_ooblayout_ops *ooblayout;
209 
210 	/* ECC layout structure pointer - read only! */
211 	struct nand_ecclayout *ecclayout;
212 
213 	/* the ecc step size. */
214 	unsigned int ecc_step_size;
215 
216 	/* max number of correctible bit errors per ecc step */
217 	unsigned int ecc_strength;
218 
219 	/* Data for variable erase regions. If numeraseregions is zero,
220 	 * it means that the whole device has erasesize as given above.
221 	 */
222 	int numeraseregions;
223 	struct mtd_erase_region_info *eraseregions;
224 
225 	/*
226 	 * Do not call via these pointers, use corresponding mtd_*()
227 	 * wrappers instead.
228 	 */
229 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
230 #ifndef __UBOOT__
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);
234 #endif
235 	unsigned long (*_get_unmapped_area) (struct mtd_info *mtd,
236 					     unsigned long len,
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,
260 				    size_t len);
261 #ifndef __UBOOT__
262 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
263 			unsigned long count, loff_t to, size_t *retlen);
264 #endif
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);
272 #ifndef __UBOOT__
273 	int (*_suspend) (struct mtd_info *mtd);
274 	void (*_resume) (struct mtd_info *mtd);
275 	void (*_reboot) (struct mtd_info *mtd);
276 #endif
277 	/*
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.
280 	 */
281 	int (*_get_device) (struct mtd_info *mtd);
282 	void (*_put_device) (struct mtd_info *mtd);
283 
284 #ifndef __UBOOT__
285 	/* Backing device capabilities for this device
286 	 * - provides mmap capabilities
287 	 */
288 	struct backing_dev_info *backing_dev_info;
289 
290 	struct notifier_block reboot_notifier;  /* default mode before reboot */
291 #endif
292 
293 	/* ECC status information */
294 	struct mtd_ecc_stats ecc_stats;
295 	/* Subpage shift (NAND) */
296 	int subpage_sft;
297 
298 	void *priv;
299 
300 	struct module *owner;
301 #ifndef __UBOOT__
302 	struct device dev;
303 #else
304 	struct udevice *dev;
305 #endif
306 	int usecount;
307 };
308 
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,
312 				 int *section,
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);
326 
327 static inline void mtd_set_ooblayout(struct mtd_info *mtd,
328 				     const struct mtd_ooblayout_ops *ooblayout)
329 {
330 	mtd->ooblayout = ooblayout;
331 }
332 
333 static inline int mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
334 {
335 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
336 }
337 
338 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
339 #ifndef __UBOOT__
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);
343 #endif
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,
347 	     u_char *buf);
348 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
349 	      const u_char *buf);
350 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
351 		    const u_char *buf);
352 
353 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
354 
355 static inline int mtd_write_oob(struct mtd_info *mtd, loff_t to,
356 				struct mtd_oob_ops *ops)
357 {
358 	ops->retlen = ops->oobretlen = 0;
359 	if (!mtd->_write_oob)
360 		return -EOPNOTSUPP;
361 	if (!(mtd->flags & MTD_WRITEABLE))
362 		return -EROFS;
363 	return mtd->_write_oob(mtd, to, ops);
364 }
365 
366 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
367 			   struct otp_info *buf);
368 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
369 			   size_t *retlen, u_char *buf);
370 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
371 			   struct otp_info *buf);
372 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
373 			   size_t *retlen, u_char *buf);
374 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
375 			    size_t *retlen, u_char *buf);
376 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
377 
378 #ifndef __UBOOT__
379 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
380 	       unsigned long count, loff_t to, size_t *retlen);
381 #endif
382 
383 static inline void mtd_sync(struct mtd_info *mtd)
384 {
385 	if (mtd->_sync)
386 		mtd->_sync(mtd);
387 }
388 
389 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
390 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
391 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
392 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
393 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
394 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
395 
396 #ifndef __UBOOT__
397 static inline int mtd_suspend(struct mtd_info *mtd)
398 {
399 	return mtd->_suspend ? mtd->_suspend(mtd) : 0;
400 }
401 
402 static inline void mtd_resume(struct mtd_info *mtd)
403 {
404 	if (mtd->_resume)
405 		mtd->_resume(mtd);
406 }
407 #endif
408 
409 static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
410 {
411 	if (mtd->erasesize_shift)
412 		return sz >> mtd->erasesize_shift;
413 	do_div(sz, mtd->erasesize);
414 	return sz;
415 }
416 
417 static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
418 {
419 	if (mtd->erasesize_shift)
420 		return sz & mtd->erasesize_mask;
421 	return do_div(sz, mtd->erasesize);
422 }
423 
424 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
425 {
426 	if (mtd->writesize_shift)
427 		return sz >> mtd->writesize_shift;
428 	do_div(sz, mtd->writesize);
429 	return sz;
430 }
431 
432 static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
433 {
434 	if (mtd->writesize_shift)
435 		return sz & mtd->writesize_mask;
436 	return do_div(sz, mtd->writesize);
437 }
438 
439 static inline int mtd_has_oob(const struct mtd_info *mtd)
440 {
441 	return mtd->_read_oob && mtd->_write_oob;
442 }
443 
444 static inline int mtd_type_is_nand(const struct mtd_info *mtd)
445 {
446 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
447 }
448 
449 static inline int mtd_can_have_bb(const struct mtd_info *mtd)
450 {
451 	return !!mtd->_block_isbad;
452 }
453 
454 	/* Kernel-side ioctl definitions */
455 
456 struct mtd_partition;
457 struct mtd_part_parser_data;
458 
459 extern int mtd_device_parse_register(struct mtd_info *mtd,
460 				     const char * const *part_probe_types,
461 				     struct mtd_part_parser_data *parser_data,
462 				     const struct mtd_partition *defparts,
463 				     int defnr_parts);
464 #define mtd_device_register(master, parts, nr_parts)	\
465 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
466 extern int mtd_device_unregister(struct mtd_info *master);
467 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
468 extern int __get_mtd_device(struct mtd_info *mtd);
469 extern void __put_mtd_device(struct mtd_info *mtd);
470 extern struct mtd_info *get_mtd_device_nm(const char *name);
471 extern void put_mtd_device(struct mtd_info *mtd);
472 
473 
474 #ifndef __UBOOT__
475 struct mtd_notifier {
476 	void (*add)(struct mtd_info *mtd);
477 	void (*remove)(struct mtd_info *mtd);
478 	struct list_head list;
479 };
480 
481 
482 extern void register_mtd_user (struct mtd_notifier *new);
483 extern int unregister_mtd_user (struct mtd_notifier *old);
484 #endif
485 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
486 
487 #ifdef CONFIG_MTD_PARTITIONS
488 void mtd_erase_callback(struct erase_info *instr);
489 #else
490 static inline void mtd_erase_callback(struct erase_info *instr)
491 {
492 	if (instr->callback)
493 		instr->callback(instr);
494 }
495 #endif
496 
497 static inline int mtd_is_bitflip(int err) {
498 	return err == -EUCLEAN;
499 }
500 
501 static inline int mtd_is_eccerr(int err) {
502 	return err == -EBADMSG;
503 }
504 
505 static inline int mtd_is_bitflip_or_eccerr(int err) {
506 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
507 }
508 
509 unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
510 
511 #ifdef __UBOOT__
512 /* drivers/mtd/mtdcore.h */
513 int add_mtd_device(struct mtd_info *mtd);
514 int del_mtd_device(struct mtd_info *mtd);
515 int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
516 int del_mtd_partitions(struct mtd_info *);
517 
518 int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
519 		loff_t *maxsize, int devtype, uint64_t chipsize);
520 int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
521 		     loff_t *size, loff_t *maxsize, int devtype,
522 		     uint64_t chipsize);
523 
524 /* drivers/mtd/mtdcore.c */
525 void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
526 			  const uint64_t length, uint64_t *len_incl_bad,
527 			  int *truncated);
528 #endif
529 #endif /* __MTD_MTD_H__ */
530