1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org> et al.
4  */
5 
6 #ifndef __MTD_MTD_H__
7 #define __MTD_MTD_H__
8 
9 #include <linux/types.h>
10 #include <linux/uio.h>
11 #include <linux/list.h>
12 #include <linux/notifier.h>
13 #include <linux/device.h>
14 #include <linux/of.h>
15 #include <linux/nvmem-provider.h>
16 
17 #include <mtd/mtd-abi.h>
18 
19 #include <asm/div64.h>
20 
21 #define MTD_FAIL_ADDR_UNKNOWN -1LL
22 
23 struct mtd_info;
24 
25 /*
26  * If the erase fails, fail_addr might indicate exactly which block failed. If
27  * fail_addr = MTD_FAIL_ADDR_UNKNOWN, the failure was not at the device level
28  * or was not specific to any particular block.
29  */
30 struct erase_info {
31 	uint64_t addr;
32 	uint64_t len;
33 	uint64_t fail_addr;
34 };
35 
36 struct mtd_erase_region_info {
37 	uint64_t offset;		/* At which this region starts, from the beginning of the MTD */
38 	uint32_t erasesize;		/* For this region */
39 	uint32_t numblocks;		/* Number of blocks of erasesize in this region */
40 	unsigned long *lockmap;		/* If keeping bitmap of locks */
41 };
42 
43 /**
44  * struct mtd_oob_ops - oob operation operands
45  * @mode:	operation mode
46  *
47  * @len:	number of data bytes to write/read
48  *
49  * @retlen:	number of data bytes written/read
50  *
51  * @ooblen:	number of oob bytes to write/read
52  * @oobretlen:	number of oob bytes written/read
53  * @ooboffs:	offset of oob data in the oob area (only relevant when
54  *		mode = MTD_OPS_PLACE_OOB or MTD_OPS_RAW)
55  * @datbuf:	data buffer - if NULL only oob data are read/written
56  * @oobbuf:	oob data buffer
57  *
58  * Note, some MTD drivers do not allow you to write more than one OOB area at
59  * one go. If you try to do that on such an MTD device, -EINVAL will be
60  * returned. If you want to make your implementation portable on all kind of MTD
61  * devices you should split the write request into several sub-requests when the
62  * request crosses a page boundary.
63  */
64 struct mtd_oob_ops {
65 	unsigned int	mode;
66 	size_t		len;
67 	size_t		retlen;
68 	size_t		ooblen;
69 	size_t		oobretlen;
70 	uint32_t	ooboffs;
71 	uint8_t		*datbuf;
72 	uint8_t		*oobbuf;
73 };
74 
75 /**
76  * struct mtd_oob_region - oob region definition
77  * @offset: region offset
78  * @length: region length
79  *
80  * This structure describes a region of the OOB area, and is used
81  * to retrieve ECC or free bytes sections.
82  * Each section is defined by an offset within the OOB area and a
83  * length.
84  */
85 struct mtd_oob_region {
86 	u32 offset;
87 	u32 length;
88 };
89 
90 /*
91  * struct mtd_ooblayout_ops - NAND OOB layout operations
92  * @ecc: function returning an ECC region in the OOB area.
93  *	 Should return -ERANGE if %section exceeds the total number of
94  *	 ECC sections.
95  * @free: function returning a free region in the OOB area.
96  *	  Should return -ERANGE if %section exceeds the total number of
97  *	  free sections.
98  */
99 struct mtd_ooblayout_ops {
100 	int (*ecc)(struct mtd_info *mtd, int section,
101 		   struct mtd_oob_region *oobecc);
102 	int (*free)(struct mtd_info *mtd, int section,
103 		    struct mtd_oob_region *oobfree);
104 };
105 
106 /**
107  * struct mtd_pairing_info - page pairing information
108  *
109  * @pair: pair id
110  * @group: group id
111  *
112  * The term "pair" is used here, even though TLC NANDs might group pages by 3
113  * (3 bits in a single cell). A pair should regroup all pages that are sharing
114  * the same cell. Pairs are then indexed in ascending order.
115  *
116  * @group is defining the position of a page in a given pair. It can also be
117  * seen as the bit position in the cell: page attached to bit 0 belongs to
118  * group 0, page attached to bit 1 belongs to group 1, etc.
119  *
120  * Example:
121  * The H27UCG8T2BTR-BC datasheet describes the following pairing scheme:
122  *
123  *		group-0		group-1
124  *
125  *  pair-0	page-0		page-4
126  *  pair-1	page-1		page-5
127  *  pair-2	page-2		page-8
128  *  ...
129  *  pair-127	page-251	page-255
130  *
131  *
132  * Note that the "group" and "pair" terms were extracted from Samsung and
133  * Hynix datasheets, and might be referenced under other names in other
134  * datasheets (Micron is describing this concept as "shared pages").
135  */
136 struct mtd_pairing_info {
137 	int pair;
138 	int group;
139 };
140 
141 /**
142  * struct mtd_pairing_scheme - page pairing scheme description
143  *
144  * @ngroups: number of groups. Should be related to the number of bits
145  *	     per cell.
146  * @get_info: converts a write-unit (page number within an erase block) into
147  *	      mtd_pairing information (pair + group). This function should
148  *	      fill the info parameter based on the wunit index or return
149  *	      -EINVAL if the wunit parameter is invalid.
150  * @get_wunit: converts pairing information into a write-unit (page) number.
151  *	       This function should return the wunit index pointed by the
152  *	       pairing information described in the info argument. It should
153  *	       return -EINVAL, if there's no wunit corresponding to the
154  *	       passed pairing information.
155  *
156  * See mtd_pairing_info documentation for a detailed explanation of the
157  * pair and group concepts.
158  *
159  * The mtd_pairing_scheme structure provides a generic solution to represent
160  * NAND page pairing scheme. Instead of exposing two big tables to do the
161  * write-unit <-> (pair + group) conversions, we ask the MTD drivers to
162  * implement the ->get_info() and ->get_wunit() functions.
163  *
164  * MTD users will then be able to query these information by using the
165  * mtd_pairing_info_to_wunit() and mtd_wunit_to_pairing_info() helpers.
166  *
167  * @ngroups is here to help MTD users iterating over all the pages in a
168  * given pair. This value can be retrieved by MTD users using the
169  * mtd_pairing_groups() helper.
170  *
171  * Examples are given in the mtd_pairing_info_to_wunit() and
172  * mtd_wunit_to_pairing_info() documentation.
173  */
174 struct mtd_pairing_scheme {
175 	int ngroups;
176 	int (*get_info)(struct mtd_info *mtd, int wunit,
177 			struct mtd_pairing_info *info);
178 	int (*get_wunit)(struct mtd_info *mtd,
179 			 const struct mtd_pairing_info *info);
180 };
181 
182 struct module;	/* only needed for owner field in mtd_info */
183 
184 /**
185  * struct mtd_debug_info - debugging information for an MTD device.
186  *
187  * @dfs_dir: direntry object of the MTD device debugfs directory
188  */
189 struct mtd_debug_info {
190 	struct dentry *dfs_dir;
191 };
192 
193 /**
194  * struct mtd_part - MTD partition specific fields
195  *
196  * @node: list node used to add an MTD partition to the parent partition list
197  * @offset: offset of the partition relatively to the parent offset
198  * @size: partition size. Should be equal to mtd->size unless
199  *	  MTD_SLC_ON_MLC_EMULATION is set
200  * @flags: original flags (before the mtdpart logic decided to tweak them based
201  *	   on flash constraints, like eraseblock/pagesize alignment)
202  *
203  * This struct is embedded in mtd_info and contains partition-specific
204  * properties/fields.
205  */
206 struct mtd_part {
207 	struct list_head node;
208 	u64 offset;
209 	u64 size;
210 	u32 flags;
211 };
212 
213 /**
214  * struct mtd_master - MTD master specific fields
215  *
216  * @partitions_lock: lock protecting accesses to the partition list. Protects
217  *		     not only the master partition list, but also all
218  *		     sub-partitions.
219  * @suspended: et to 1 when the device is suspended, 0 otherwise
220  *
221  * This struct is embedded in mtd_info and contains master-specific
222  * properties/fields. The master is the root MTD device from the MTD partition
223  * point of view.
224  */
225 struct mtd_master {
226 	struct mutex partitions_lock;
227 	struct mutex chrdev_lock;
228 	unsigned int suspended : 1;
229 };
230 
231 struct mtd_info {
232 	u_char type;
233 	uint32_t flags;
234 	uint64_t size;	 // Total size of the MTD
235 
236 	/* "Major" erase size for the device. Naïve users may take this
237 	 * to be the only erase size available, or may use the more detailed
238 	 * information below if they desire
239 	 */
240 	uint32_t erasesize;
241 	/* Minimal writable flash unit size. In case of NOR flash it is 1 (even
242 	 * though individual bits can be cleared), in case of NAND flash it is
243 	 * one NAND page (or half, or one-fourths of it), in case of ECC-ed NOR
244 	 * it is of ECC block size, etc. It is illegal to have writesize = 0.
245 	 * Any driver registering a struct mtd_info must ensure a writesize of
246 	 * 1 or larger.
247 	 */
248 	uint32_t writesize;
249 
250 	/*
251 	 * Size of the write buffer used by the MTD. MTD devices having a write
252 	 * buffer can write multiple writesize chunks at a time. E.g. while
253 	 * writing 4 * writesize bytes to a device with 2 * writesize bytes
254 	 * buffer the MTD driver can (but doesn't have to) do 2 writesize
255 	 * operations, but not 4. Currently, all NANDs have writebufsize
256 	 * equivalent to writesize (NAND page size). Some NOR flashes do have
257 	 * writebufsize greater than writesize.
258 	 */
259 	uint32_t writebufsize;
260 
261 	uint32_t oobsize;   // Amount of OOB data per block (e.g. 16)
262 	uint32_t oobavail;  // Available OOB bytes per block
263 
264 	/*
265 	 * If erasesize is a power of 2 then the shift is stored in
266 	 * erasesize_shift otherwise erasesize_shift is zero. Ditto writesize.
267 	 */
268 	unsigned int erasesize_shift;
269 	unsigned int writesize_shift;
270 	/* Masks based on erasesize_shift and writesize_shift */
271 	unsigned int erasesize_mask;
272 	unsigned int writesize_mask;
273 
274 	/*
275 	 * read ops return -EUCLEAN if max number of bitflips corrected on any
276 	 * one region comprising an ecc step equals or exceeds this value.
277 	 * Settable by driver, else defaults to ecc_strength.  User can override
278 	 * in sysfs.  N.B. The meaning of the -EUCLEAN return code has changed;
279 	 * see Documentation/ABI/testing/sysfs-class-mtd for more detail.
280 	 */
281 	unsigned int bitflip_threshold;
282 
283 	/* Kernel-only stuff starts here. */
284 	const char *name;
285 	int index;
286 
287 	/* OOB layout description */
288 	const struct mtd_ooblayout_ops *ooblayout;
289 
290 	/* NAND pairing scheme, only provided for MLC/TLC NANDs */
291 	const struct mtd_pairing_scheme *pairing;
292 
293 	/* the ecc step size. */
294 	unsigned int ecc_step_size;
295 
296 	/* max number of correctible bit errors per ecc step */
297 	unsigned int ecc_strength;
298 
299 	/* Data for variable erase regions. If numeraseregions is zero,
300 	 * it means that the whole device has erasesize as given above.
301 	 */
302 	int numeraseregions;
303 	struct mtd_erase_region_info *eraseregions;
304 
305 	/*
306 	 * Do not call via these pointers, use corresponding mtd_*()
307 	 * wrappers instead.
308 	 */
309 	int (*_erase) (struct mtd_info *mtd, struct erase_info *instr);
310 	int (*_point) (struct mtd_info *mtd, loff_t from, size_t len,
311 		       size_t *retlen, void **virt, resource_size_t *phys);
312 	int (*_unpoint) (struct mtd_info *mtd, loff_t from, size_t len);
313 	int (*_read) (struct mtd_info *mtd, loff_t from, size_t len,
314 		      size_t *retlen, u_char *buf);
315 	int (*_write) (struct mtd_info *mtd, loff_t to, size_t len,
316 		       size_t *retlen, const u_char *buf);
317 	int (*_panic_write) (struct mtd_info *mtd, loff_t to, size_t len,
318 			     size_t *retlen, const u_char *buf);
319 	int (*_read_oob) (struct mtd_info *mtd, loff_t from,
320 			  struct mtd_oob_ops *ops);
321 	int (*_write_oob) (struct mtd_info *mtd, loff_t to,
322 			   struct mtd_oob_ops *ops);
323 	int (*_get_fact_prot_info) (struct mtd_info *mtd, size_t len,
324 				    size_t *retlen, struct otp_info *buf);
325 	int (*_read_fact_prot_reg) (struct mtd_info *mtd, loff_t from,
326 				    size_t len, size_t *retlen, u_char *buf);
327 	int (*_get_user_prot_info) (struct mtd_info *mtd, size_t len,
328 				    size_t *retlen, struct otp_info *buf);
329 	int (*_read_user_prot_reg) (struct mtd_info *mtd, loff_t from,
330 				    size_t len, size_t *retlen, u_char *buf);
331 	int (*_write_user_prot_reg) (struct mtd_info *mtd, loff_t to,
332 				     size_t len, size_t *retlen,
333 				     const u_char *buf);
334 	int (*_lock_user_prot_reg) (struct mtd_info *mtd, loff_t from,
335 				    size_t len);
336 	int (*_erase_user_prot_reg) (struct mtd_info *mtd, loff_t from,
337 				     size_t len);
338 	int (*_writev) (struct mtd_info *mtd, const struct kvec *vecs,
339 			unsigned long count, loff_t to, size_t *retlen);
340 	void (*_sync) (struct mtd_info *mtd);
341 	int (*_lock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
342 	int (*_unlock) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
343 	int (*_is_locked) (struct mtd_info *mtd, loff_t ofs, uint64_t len);
344 	int (*_block_isreserved) (struct mtd_info *mtd, loff_t ofs);
345 	int (*_block_isbad) (struct mtd_info *mtd, loff_t ofs);
346 	int (*_block_markbad) (struct mtd_info *mtd, loff_t ofs);
347 	int (*_max_bad_blocks) (struct mtd_info *mtd, loff_t ofs, size_t len);
348 	int (*_suspend) (struct mtd_info *mtd);
349 	void (*_resume) (struct mtd_info *mtd);
350 	void (*_reboot) (struct mtd_info *mtd);
351 	/*
352 	 * If the driver is something smart, like UBI, it may need to maintain
353 	 * its own reference counting. The below functions are only for driver.
354 	 */
355 	int (*_get_device) (struct mtd_info *mtd);
356 	void (*_put_device) (struct mtd_info *mtd);
357 
358 	/*
359 	 * flag indicates a panic write, low level drivers can take appropriate
360 	 * action if required to ensure writes go through
361 	 */
362 	bool oops_panic_write;
363 
364 	struct notifier_block reboot_notifier;  /* default mode before reboot */
365 
366 	/* ECC status information */
367 	struct mtd_ecc_stats ecc_stats;
368 	/* Subpage shift (NAND) */
369 	int subpage_sft;
370 
371 	void *priv;
372 
373 	struct module *owner;
374 	struct device dev;
375 	int usecount;
376 	struct mtd_debug_info dbg;
377 	struct nvmem_device *nvmem;
378 	struct nvmem_device *otp_user_nvmem;
379 	struct nvmem_device *otp_factory_nvmem;
380 
381 	/*
382 	 * Parent device from the MTD partition point of view.
383 	 *
384 	 * MTD masters do not have any parent, MTD partitions do. The parent
385 	 * MTD device can itself be a partition.
386 	 */
387 	struct mtd_info *parent;
388 
389 	/* List of partitions attached to this MTD device */
390 	struct list_head partitions;
391 
392 	struct mtd_part part;
393 	struct mtd_master master;
394 };
395 
mtd_get_master(struct mtd_info * mtd)396 static inline struct mtd_info *mtd_get_master(struct mtd_info *mtd)
397 {
398 	while (mtd->parent)
399 		mtd = mtd->parent;
400 
401 	return mtd;
402 }
403 
mtd_get_master_ofs(struct mtd_info * mtd,u64 ofs)404 static inline u64 mtd_get_master_ofs(struct mtd_info *mtd, u64 ofs)
405 {
406 	while (mtd->parent) {
407 		ofs += mtd->part.offset;
408 		mtd = mtd->parent;
409 	}
410 
411 	return ofs;
412 }
413 
mtd_is_partition(const struct mtd_info * mtd)414 static inline bool mtd_is_partition(const struct mtd_info *mtd)
415 {
416 	return mtd->parent;
417 }
418 
mtd_has_partitions(const struct mtd_info * mtd)419 static inline bool mtd_has_partitions(const struct mtd_info *mtd)
420 {
421 	return !list_empty(&mtd->partitions);
422 }
423 
424 int mtd_ooblayout_ecc(struct mtd_info *mtd, int section,
425 		      struct mtd_oob_region *oobecc);
426 int mtd_ooblayout_find_eccregion(struct mtd_info *mtd, int eccbyte,
427 				 int *section,
428 				 struct mtd_oob_region *oobregion);
429 int mtd_ooblayout_get_eccbytes(struct mtd_info *mtd, u8 *eccbuf,
430 			       const u8 *oobbuf, int start, int nbytes);
431 int mtd_ooblayout_set_eccbytes(struct mtd_info *mtd, const u8 *eccbuf,
432 			       u8 *oobbuf, int start, int nbytes);
433 int mtd_ooblayout_free(struct mtd_info *mtd, int section,
434 		       struct mtd_oob_region *oobfree);
435 int mtd_ooblayout_get_databytes(struct mtd_info *mtd, u8 *databuf,
436 				const u8 *oobbuf, int start, int nbytes);
437 int mtd_ooblayout_set_databytes(struct mtd_info *mtd, const u8 *databuf,
438 				u8 *oobbuf, int start, int nbytes);
439 int mtd_ooblayout_count_freebytes(struct mtd_info *mtd);
440 int mtd_ooblayout_count_eccbytes(struct mtd_info *mtd);
441 
mtd_set_ooblayout(struct mtd_info * mtd,const struct mtd_ooblayout_ops * ooblayout)442 static inline void mtd_set_ooblayout(struct mtd_info *mtd,
443 				     const struct mtd_ooblayout_ops *ooblayout)
444 {
445 	mtd->ooblayout = ooblayout;
446 }
447 
mtd_set_pairing_scheme(struct mtd_info * mtd,const struct mtd_pairing_scheme * pairing)448 static inline void mtd_set_pairing_scheme(struct mtd_info *mtd,
449 				const struct mtd_pairing_scheme *pairing)
450 {
451 	mtd->pairing = pairing;
452 }
453 
mtd_set_of_node(struct mtd_info * mtd,struct device_node * np)454 static inline void mtd_set_of_node(struct mtd_info *mtd,
455 				   struct device_node *np)
456 {
457 	mtd->dev.of_node = np;
458 	if (!mtd->name)
459 		of_property_read_string(np, "label", &mtd->name);
460 }
461 
mtd_get_of_node(struct mtd_info * mtd)462 static inline struct device_node *mtd_get_of_node(struct mtd_info *mtd)
463 {
464 	return dev_of_node(&mtd->dev);
465 }
466 
mtd_oobavail(struct mtd_info * mtd,struct mtd_oob_ops * ops)467 static inline u32 mtd_oobavail(struct mtd_info *mtd, struct mtd_oob_ops *ops)
468 {
469 	return ops->mode == MTD_OPS_AUTO_OOB ? mtd->oobavail : mtd->oobsize;
470 }
471 
mtd_max_bad_blocks(struct mtd_info * mtd,loff_t ofs,size_t len)472 static inline int mtd_max_bad_blocks(struct mtd_info *mtd,
473 				     loff_t ofs, size_t len)
474 {
475 	struct mtd_info *master = mtd_get_master(mtd);
476 
477 	if (!master->_max_bad_blocks)
478 		return -ENOTSUPP;
479 
480 	if (mtd->size < (len + ofs) || ofs < 0)
481 		return -EINVAL;
482 
483 	return master->_max_bad_blocks(master, mtd_get_master_ofs(mtd, ofs),
484 				       len);
485 }
486 
487 int mtd_wunit_to_pairing_info(struct mtd_info *mtd, int wunit,
488 			      struct mtd_pairing_info *info);
489 int mtd_pairing_info_to_wunit(struct mtd_info *mtd,
490 			      const struct mtd_pairing_info *info);
491 int mtd_pairing_groups(struct mtd_info *mtd);
492 int mtd_erase(struct mtd_info *mtd, struct erase_info *instr);
493 int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
494 	      void **virt, resource_size_t *phys);
495 int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
496 unsigned long mtd_get_unmapped_area(struct mtd_info *mtd, unsigned long len,
497 				    unsigned long offset, unsigned long flags);
498 int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
499 	     u_char *buf);
500 int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
501 	      const u_char *buf);
502 int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
503 		    const u_char *buf);
504 
505 int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops);
506 int mtd_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops);
507 
508 int mtd_get_fact_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
509 			   struct otp_info *buf);
510 int mtd_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
511 			   size_t *retlen, u_char *buf);
512 int mtd_get_user_prot_info(struct mtd_info *mtd, size_t len, size_t *retlen,
513 			   struct otp_info *buf);
514 int mtd_read_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len,
515 			   size_t *retlen, u_char *buf);
516 int mtd_write_user_prot_reg(struct mtd_info *mtd, loff_t to, size_t len,
517 			    size_t *retlen, const u_char *buf);
518 int mtd_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
519 int mtd_erase_user_prot_reg(struct mtd_info *mtd, loff_t from, size_t len);
520 
521 int mtd_writev(struct mtd_info *mtd, const struct kvec *vecs,
522 	       unsigned long count, loff_t to, size_t *retlen);
523 
mtd_sync(struct mtd_info * mtd)524 static inline void mtd_sync(struct mtd_info *mtd)
525 {
526 	struct mtd_info *master = mtd_get_master(mtd);
527 
528 	if (master->_sync)
529 		master->_sync(master);
530 }
531 
532 int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
533 int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
534 int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len);
535 int mtd_block_isreserved(struct mtd_info *mtd, loff_t ofs);
536 int mtd_block_isbad(struct mtd_info *mtd, loff_t ofs);
537 int mtd_block_markbad(struct mtd_info *mtd, loff_t ofs);
538 
mtd_suspend(struct mtd_info * mtd)539 static inline int mtd_suspend(struct mtd_info *mtd)
540 {
541 	struct mtd_info *master = mtd_get_master(mtd);
542 	int ret;
543 
544 	if (master->master.suspended)
545 		return 0;
546 
547 	ret = master->_suspend ? master->_suspend(master) : 0;
548 	if (ret)
549 		return ret;
550 
551 	master->master.suspended = 1;
552 	return 0;
553 }
554 
mtd_resume(struct mtd_info * mtd)555 static inline void mtd_resume(struct mtd_info *mtd)
556 {
557 	struct mtd_info *master = mtd_get_master(mtd);
558 
559 	if (!master->master.suspended)
560 		return;
561 
562 	if (master->_resume)
563 		master->_resume(master);
564 
565 	master->master.suspended = 0;
566 }
567 
mtd_div_by_eb(uint64_t sz,struct mtd_info * mtd)568 static inline uint32_t mtd_div_by_eb(uint64_t sz, struct mtd_info *mtd)
569 {
570 	if (mtd->erasesize_shift)
571 		return sz >> mtd->erasesize_shift;
572 	do_div(sz, mtd->erasesize);
573 	return sz;
574 }
575 
mtd_mod_by_eb(uint64_t sz,struct mtd_info * mtd)576 static inline uint32_t mtd_mod_by_eb(uint64_t sz, struct mtd_info *mtd)
577 {
578 	if (mtd->erasesize_shift)
579 		return sz & mtd->erasesize_mask;
580 	return do_div(sz, mtd->erasesize);
581 }
582 
583 /**
584  * mtd_align_erase_req - Adjust an erase request to align things on eraseblock
585  *			 boundaries.
586  * @mtd: the MTD device this erase request applies on
587  * @req: the erase request to adjust
588  *
589  * This function will adjust @req->addr and @req->len to align them on
590  * @mtd->erasesize. Of course we expect @mtd->erasesize to be != 0.
591  */
mtd_align_erase_req(struct mtd_info * mtd,struct erase_info * req)592 static inline void mtd_align_erase_req(struct mtd_info *mtd,
593 				       struct erase_info *req)
594 {
595 	u32 mod;
596 
597 	if (WARN_ON(!mtd->erasesize))
598 		return;
599 
600 	mod = mtd_mod_by_eb(req->addr, mtd);
601 	if (mod) {
602 		req->addr -= mod;
603 		req->len += mod;
604 	}
605 
606 	mod = mtd_mod_by_eb(req->addr + req->len, mtd);
607 	if (mod)
608 		req->len += mtd->erasesize - mod;
609 }
610 
mtd_div_by_ws(uint64_t sz,struct mtd_info * mtd)611 static inline uint32_t mtd_div_by_ws(uint64_t sz, struct mtd_info *mtd)
612 {
613 	if (mtd->writesize_shift)
614 		return sz >> mtd->writesize_shift;
615 	do_div(sz, mtd->writesize);
616 	return sz;
617 }
618 
mtd_mod_by_ws(uint64_t sz,struct mtd_info * mtd)619 static inline uint32_t mtd_mod_by_ws(uint64_t sz, struct mtd_info *mtd)
620 {
621 	if (mtd->writesize_shift)
622 		return sz & mtd->writesize_mask;
623 	return do_div(sz, mtd->writesize);
624 }
625 
mtd_wunit_per_eb(struct mtd_info * mtd)626 static inline int mtd_wunit_per_eb(struct mtd_info *mtd)
627 {
628 	struct mtd_info *master = mtd_get_master(mtd);
629 
630 	return master->erasesize / mtd->writesize;
631 }
632 
mtd_offset_to_wunit(struct mtd_info * mtd,loff_t offs)633 static inline int mtd_offset_to_wunit(struct mtd_info *mtd, loff_t offs)
634 {
635 	return mtd_div_by_ws(mtd_mod_by_eb(offs, mtd), mtd);
636 }
637 
mtd_wunit_to_offset(struct mtd_info * mtd,loff_t base,int wunit)638 static inline loff_t mtd_wunit_to_offset(struct mtd_info *mtd, loff_t base,
639 					 int wunit)
640 {
641 	return base + (wunit * mtd->writesize);
642 }
643 
644 
mtd_has_oob(const struct mtd_info * mtd)645 static inline int mtd_has_oob(const struct mtd_info *mtd)
646 {
647 	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
648 
649 	return master->_read_oob && master->_write_oob;
650 }
651 
mtd_type_is_nand(const struct mtd_info * mtd)652 static inline int mtd_type_is_nand(const struct mtd_info *mtd)
653 {
654 	return mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH;
655 }
656 
mtd_can_have_bb(const struct mtd_info * mtd)657 static inline int mtd_can_have_bb(const struct mtd_info *mtd)
658 {
659 	struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
660 
661 	return !!master->_block_isbad;
662 }
663 
664 	/* Kernel-side ioctl definitions */
665 
666 struct mtd_partition;
667 struct mtd_part_parser_data;
668 
669 extern int mtd_device_parse_register(struct mtd_info *mtd,
670 				     const char * const *part_probe_types,
671 				     struct mtd_part_parser_data *parser_data,
672 				     const struct mtd_partition *defparts,
673 				     int defnr_parts);
674 #define mtd_device_register(master, parts, nr_parts)	\
675 	mtd_device_parse_register(master, NULL, NULL, parts, nr_parts)
676 extern int mtd_device_unregister(struct mtd_info *master);
677 extern struct mtd_info *get_mtd_device(struct mtd_info *mtd, int num);
678 extern int __get_mtd_device(struct mtd_info *mtd);
679 extern void __put_mtd_device(struct mtd_info *mtd);
680 extern struct mtd_info *get_mtd_device_nm(const char *name);
681 extern void put_mtd_device(struct mtd_info *mtd);
682 
683 
684 struct mtd_notifier {
685 	void (*add)(struct mtd_info *mtd);
686 	void (*remove)(struct mtd_info *mtd);
687 	struct list_head list;
688 };
689 
690 
691 extern void register_mtd_user (struct mtd_notifier *new);
692 extern int unregister_mtd_user (struct mtd_notifier *old);
693 void *mtd_kmalloc_up_to(const struct mtd_info *mtd, size_t *size);
694 
mtd_is_bitflip(int err)695 static inline int mtd_is_bitflip(int err) {
696 	return err == -EUCLEAN;
697 }
698 
mtd_is_eccerr(int err)699 static inline int mtd_is_eccerr(int err) {
700 	return err == -EBADMSG;
701 }
702 
mtd_is_bitflip_or_eccerr(int err)703 static inline int mtd_is_bitflip_or_eccerr(int err) {
704 	return mtd_is_bitflip(err) || mtd_is_eccerr(err);
705 }
706 
707 unsigned mtd_mmap_capabilities(struct mtd_info *mtd);
708 
709 #ifdef CONFIG_DEBUG_FS
710 bool mtd_check_expert_analysis_mode(void);
711 #else
mtd_check_expert_analysis_mode(void)712 static inline bool mtd_check_expert_analysis_mode(void) { return false; }
713 #endif
714 
715 
716 #endif /* __MTD_MTD_H__ */
717