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