1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  Copyright 2017 - Free Electrons
4  *
5  *  Authors:
6  *	Boris Brezillon <boris.brezillon@free-electrons.com>
7  *	Peter Pan <peterpandong@micron.com>
8  */
9 
10 #ifndef __LINUX_MTD_NAND_H
11 #define __LINUX_MTD_NAND_H
12 
13 #include <linux/mtd/mtd.h>
14 
15 /**
16  * struct nand_memory_organization - Memory organization structure
17  * @bits_per_cell: number of bits per NAND cell
18  * @pagesize: page size
19  * @oobsize: OOB area size
20  * @pages_per_eraseblock: number of pages per eraseblock
21  * @eraseblocks_per_lun: number of eraseblocks per LUN (Logical Unit Number)
22  * @max_bad_eraseblocks_per_lun: maximum number of eraseblocks per LUN
23  * @planes_per_lun: number of planes per LUN
24  * @luns_per_target: number of LUN per target (target is a synonym for die)
25  * @ntargets: total number of targets exposed by the NAND device
26  */
27 struct nand_memory_organization {
28 	unsigned int bits_per_cell;
29 	unsigned int pagesize;
30 	unsigned int oobsize;
31 	unsigned int pages_per_eraseblock;
32 	unsigned int eraseblocks_per_lun;
33 	unsigned int max_bad_eraseblocks_per_lun;
34 	unsigned int planes_per_lun;
35 	unsigned int luns_per_target;
36 	unsigned int ntargets;
37 };
38 
39 #define NAND_MEMORG(bpc, ps, os, ppe, epl, mbb, ppl, lpt, nt)	\
40 	{							\
41 		.bits_per_cell = (bpc),				\
42 		.pagesize = (ps),				\
43 		.oobsize = (os),				\
44 		.pages_per_eraseblock = (ppe),			\
45 		.eraseblocks_per_lun = (epl),			\
46 		.max_bad_eraseblocks_per_lun = (mbb),		\
47 		.planes_per_lun = (ppl),			\
48 		.luns_per_target = (lpt),			\
49 		.ntargets = (nt),				\
50 	}
51 
52 /**
53  * struct nand_row_converter - Information needed to convert an absolute offset
54  *			       into a row address
55  * @lun_addr_shift: position of the LUN identifier in the row address
56  * @eraseblock_addr_shift: position of the eraseblock identifier in the row
57  *			   address
58  */
59 struct nand_row_converter {
60 	unsigned int lun_addr_shift;
61 	unsigned int eraseblock_addr_shift;
62 };
63 
64 /**
65  * struct nand_pos - NAND position object
66  * @target: the NAND target/die
67  * @lun: the LUN identifier
68  * @plane: the plane within the LUN
69  * @eraseblock: the eraseblock within the LUN
70  * @page: the page within the LUN
71  *
72  * These information are usually used by specific sub-layers to select the
73  * appropriate target/die and generate a row address to pass to the device.
74  */
75 struct nand_pos {
76 	unsigned int target;
77 	unsigned int lun;
78 	unsigned int plane;
79 	unsigned int eraseblock;
80 	unsigned int page;
81 };
82 
83 /**
84  * struct nand_page_io_req - NAND I/O request object
85  * @pos: the position this I/O request is targeting
86  * @dataoffs: the offset within the page
87  * @datalen: number of data bytes to read from/write to this page
88  * @databuf: buffer to store data in or get data from
89  * @ooboffs: the OOB offset within the page
90  * @ooblen: the number of OOB bytes to read from/write to this page
91  * @oobbuf: buffer to store OOB data in or get OOB data from
92  * @mode: one of the %MTD_OPS_XXX mode
93  *
94  * This object is used to pass per-page I/O requests to NAND sub-layers. This
95  * way all useful information are already formatted in a useful way and
96  * specific NAND layers can focus on translating these information into
97  * specific commands/operations.
98  */
99 struct nand_page_io_req {
100 	struct nand_pos pos;
101 	unsigned int dataoffs;
102 	unsigned int datalen;
103 	union {
104 		const void *out;
105 		void *in;
106 	} databuf;
107 	unsigned int ooboffs;
108 	unsigned int ooblen;
109 	union {
110 		const void *out;
111 		void *in;
112 	} oobbuf;
113 	int mode;
114 };
115 
116 /**
117  * struct nand_ecc_req - NAND ECC requirements
118  * @strength: ECC strength
119  * @step_size: ECC step/block size
120  */
121 struct nand_ecc_req {
122 	unsigned int strength;
123 	unsigned int step_size;
124 };
125 
126 #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
127 
128 /**
129  * struct nand_bbt - bad block table object
130  * @cache: in memory BBT cache
131  */
132 struct nand_bbt {
133 	unsigned long *cache;
134 };
135 
136 struct nand_device;
137 
138 /**
139  * struct nand_ops - NAND operations
140  * @erase: erase a specific block. No need to check if the block is bad before
141  *	   erasing, this has been taken care of by the generic NAND layer
142  * @markbad: mark a specific block bad. No need to check if the block is
143  *	     already marked bad, this has been taken care of by the generic
144  *	     NAND layer. This method should just write the BBM (Bad Block
145  *	     Marker) so that future call to struct_nand_ops->isbad() return
146  *	     true
147  * @isbad: check whether a block is bad or not. This method should just read
148  *	   the BBM and return whether the block is bad or not based on what it
149  *	   reads
150  *
151  * These are all low level operations that should be implemented by specialized
152  * NAND layers (SPI NAND, raw NAND, ...).
153  */
154 struct nand_ops {
155 	int (*erase)(struct nand_device *nand, const struct nand_pos *pos);
156 	int (*markbad)(struct nand_device *nand, const struct nand_pos *pos);
157 	bool (*isbad)(struct nand_device *nand, const struct nand_pos *pos);
158 };
159 
160 /**
161  * struct nand_device - NAND device
162  * @mtd: MTD instance attached to the NAND device
163  * @memorg: memory layout
164  * @eccreq: ECC requirements
165  * @rowconv: position to row address converter
166  * @bbt: bad block table info
167  * @ops: NAND operations attached to the NAND device
168  *
169  * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND)
170  * should declare their own NAND object embedding a nand_device struct (that's
171  * how inheritance is done).
172  * struct_nand_device->memorg and struct_nand_device->eccreq should be filled
173  * at device detection time to reflect the NAND device
174  * capabilities/requirements. Once this is done nanddev_init() can be called.
175  * It will take care of converting NAND information into MTD ones, which means
176  * the specialized NAND layers should never manually tweak
177  * struct_nand_device->mtd except for the ->_read/write() hooks.
178  */
179 struct nand_device {
180 	struct mtd_info *mtd;
181 	struct nand_memory_organization memorg;
182 	struct nand_ecc_req eccreq;
183 	struct nand_row_converter rowconv;
184 	struct nand_bbt bbt;
185 	const struct nand_ops *ops;
186 };
187 
188 /**
189  * struct nand_io_iter - NAND I/O iterator
190  * @req: current I/O request
191  * @oobbytes_per_page: maximum number of OOB bytes per page
192  * @dataleft: remaining number of data bytes to read/write
193  * @oobleft: remaining number of OOB bytes to read/write
194  *
195  * Can be used by specialized NAND layers to iterate over all pages covered
196  * by an MTD I/O request, which should greatly simplifies the boiler-plate
197  * code needed to read/write data from/to a NAND device.
198  */
199 struct nand_io_iter {
200 	struct nand_page_io_req req;
201 	unsigned int oobbytes_per_page;
202 	unsigned int dataleft;
203 	unsigned int oobleft;
204 };
205 
206 /**
207  * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
208  * @mtd: MTD instance
209  *
210  * Return: the NAND device embedding @mtd.
211  */
mtd_to_nanddev(struct mtd_info * mtd)212 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
213 {
214 	return mtd->priv;
215 }
216 
217 /**
218  * nanddev_to_mtd() - Get the MTD device attached to a NAND device
219  * @nand: NAND device
220  *
221  * Return: the MTD device embedded in @nand.
222  */
nanddev_to_mtd(struct nand_device * nand)223 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
224 {
225 	return nand->mtd;
226 }
227 
228 /*
229  * nanddev_bits_per_cell() - Get the number of bits per cell
230  * @nand: NAND device
231  *
232  * Return: the number of bits per cell.
233  */
nanddev_bits_per_cell(const struct nand_device * nand)234 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
235 {
236 	return nand->memorg.bits_per_cell;
237 }
238 
239 /**
240  * nanddev_page_size() - Get NAND page size
241  * @nand: NAND device
242  *
243  * Return: the page size.
244  */
nanddev_page_size(const struct nand_device * nand)245 static inline size_t nanddev_page_size(const struct nand_device *nand)
246 {
247 	return nand->memorg.pagesize;
248 }
249 
250 /**
251  * nanddev_per_page_oobsize() - Get NAND OOB size
252  * @nand: NAND device
253  *
254  * Return: the OOB size.
255  */
256 static inline unsigned int
nanddev_per_page_oobsize(const struct nand_device * nand)257 nanddev_per_page_oobsize(const struct nand_device *nand)
258 {
259 	return nand->memorg.oobsize;
260 }
261 
262 /**
263  * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
264  * @nand: NAND device
265  *
266  * Return: the number of pages per eraseblock.
267  */
268 static inline unsigned int
nanddev_pages_per_eraseblock(const struct nand_device * nand)269 nanddev_pages_per_eraseblock(const struct nand_device *nand)
270 {
271 	return nand->memorg.pages_per_eraseblock;
272 }
273 
274 /**
275  * nanddev_per_page_oobsize() - Get NAND erase block size
276  * @nand: NAND device
277  *
278  * Return: the eraseblock size.
279  */
nanddev_eraseblock_size(const struct nand_device * nand)280 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
281 {
282 	return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
283 }
284 
285 /**
286  * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
287  * @nand: NAND device
288  *
289  * Return: the number of eraseblocks per LUN.
290  */
291 static inline unsigned int
nanddev_eraseblocks_per_lun(const struct nand_device * nand)292 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
293 {
294 	return nand->memorg.eraseblocks_per_lun;
295 }
296 
297 /**
298  * nanddev_target_size() - Get the total size provided by a single target/die
299  * @nand: NAND device
300  *
301  * Return: the total size exposed by a single target/die in bytes.
302  */
nanddev_target_size(const struct nand_device * nand)303 static inline u64 nanddev_target_size(const struct nand_device *nand)
304 {
305 	return (u64)nand->memorg.luns_per_target *
306 	       nand->memorg.eraseblocks_per_lun *
307 	       nand->memorg.pages_per_eraseblock *
308 	       nand->memorg.pagesize;
309 }
310 
311 /**
312  * nanddev_ntarget() - Get the total of targets
313  * @nand: NAND device
314  *
315  * Return: the number of targets/dies exposed by @nand.
316  */
nanddev_ntargets(const struct nand_device * nand)317 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
318 {
319 	return nand->memorg.ntargets;
320 }
321 
322 /**
323  * nanddev_neraseblocks() - Get the total number of erasablocks
324  * @nand: NAND device
325  *
326  * Return: the total number of eraseblocks exposed by @nand.
327  */
nanddev_neraseblocks(const struct nand_device * nand)328 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
329 {
330 	return (u64)nand->memorg.luns_per_target *
331 	       nand->memorg.eraseblocks_per_lun *
332 	       nand->memorg.pages_per_eraseblock;
333 }
334 
335 /**
336  * nanddev_size() - Get NAND size
337  * @nand: NAND device
338  *
339  * Return: the total size (in bytes) exposed by @nand.
340  */
nanddev_size(const struct nand_device * nand)341 static inline u64 nanddev_size(const struct nand_device *nand)
342 {
343 	return nanddev_target_size(nand) * nanddev_ntargets(nand);
344 }
345 
346 /**
347  * nanddev_get_memorg() - Extract memory organization info from a NAND device
348  * @nand: NAND device
349  *
350  * This can be used by the upper layer to fill the memorg info before calling
351  * nanddev_init().
352  *
353  * Return: the memorg object embedded in the NAND device.
354  */
355 static inline struct nand_memory_organization *
nanddev_get_memorg(struct nand_device * nand)356 nanddev_get_memorg(struct nand_device *nand)
357 {
358 	return &nand->memorg;
359 }
360 
361 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
362 		 struct module *owner);
363 void nanddev_cleanup(struct nand_device *nand);
364 
365 /**
366  * nanddev_register() - Register a NAND device
367  * @nand: NAND device
368  *
369  * Register a NAND device.
370  * This function is just a wrapper around mtd_device_register()
371  * registering the MTD device embedded in @nand.
372  *
373  * Return: 0 in case of success, a negative error code otherwise.
374  */
nanddev_register(struct nand_device * nand)375 static inline int nanddev_register(struct nand_device *nand)
376 {
377 	return mtd_device_register(nand->mtd, NULL, 0);
378 }
379 
380 /**
381  * nanddev_unregister() - Unregister a NAND device
382  * @nand: NAND device
383  *
384  * Unregister a NAND device.
385  * This function is just a wrapper around mtd_device_unregister()
386  * unregistering the MTD device embedded in @nand.
387  *
388  * Return: 0 in case of success, a negative error code otherwise.
389  */
nanddev_unregister(struct nand_device * nand)390 static inline int nanddev_unregister(struct nand_device *nand)
391 {
392 	return mtd_device_unregister(nand->mtd);
393 }
394 
395 #ifndef __UBOOT__
396 /**
397  * nanddev_set_of_node() - Attach a DT node to a NAND device
398  * @nand: NAND device
399  * @np: DT node
400  *
401  * Attach a DT node to a NAND device.
402  */
nanddev_set_of_node(struct nand_device * nand,const struct device_node * np)403 static inline void nanddev_set_of_node(struct nand_device *nand,
404 				       const struct device_node *np)
405 {
406 	mtd_set_of_node(nand->mtd, np);
407 }
408 
409 /**
410  * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
411  * @nand: NAND device
412  *
413  * Return: the DT node attached to @nand.
414  */
nanddev_get_of_node(struct nand_device * nand)415 static inline const struct device_node *nanddev_get_of_node(struct nand_device *nand)
416 {
417 	return mtd_get_of_node(nand->mtd);
418 }
419 #else
420 /**
421  * nanddev_set_of_node() - Attach a DT node to a NAND device
422  * @nand: NAND device
423  * @node: ofnode
424  *
425  * Attach a DT node to a NAND device.
426  */
nanddev_set_ofnode(struct nand_device * nand,ofnode node)427 static inline void nanddev_set_ofnode(struct nand_device *nand, ofnode node)
428 {
429 	mtd_set_ofnode(nand->mtd, node);
430 }
431 #endif /* __UBOOT__ */
432 
433 /**
434  * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
435  * @nand: NAND device
436  * @offs: absolute NAND offset (usually passed by the MTD layer)
437  * @pos: a NAND position object to fill in
438  *
439  * Converts @offs into a nand_pos representation.
440  *
441  * Return: the offset within the NAND page pointed by @pos.
442  */
nanddev_offs_to_pos(struct nand_device * nand,loff_t offs,struct nand_pos * pos)443 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
444 					       loff_t offs,
445 					       struct nand_pos *pos)
446 {
447 	unsigned int pageoffs;
448 	u64 tmp = offs;
449 
450 	pageoffs = do_div(tmp, nand->memorg.pagesize);
451 	pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
452 	pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
453 	pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
454 	pos->lun = do_div(tmp, nand->memorg.luns_per_target);
455 	pos->target = tmp;
456 
457 	return pageoffs;
458 }
459 
460 /**
461  * nanddev_pos_cmp() - Compare two NAND positions
462  * @a: First NAND position
463  * @b: Second NAND position
464  *
465  * Compares two NAND positions.
466  *
467  * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
468  */
nanddev_pos_cmp(const struct nand_pos * a,const struct nand_pos * b)469 static inline int nanddev_pos_cmp(const struct nand_pos *a,
470 				  const struct nand_pos *b)
471 {
472 	if (a->target != b->target)
473 		return a->target < b->target ? -1 : 1;
474 
475 	if (a->lun != b->lun)
476 		return a->lun < b->lun ? -1 : 1;
477 
478 	if (a->eraseblock != b->eraseblock)
479 		return a->eraseblock < b->eraseblock ? -1 : 1;
480 
481 	if (a->page != b->page)
482 		return a->page < b->page ? -1 : 1;
483 
484 	return 0;
485 }
486 
487 /**
488  * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
489  * @nand: NAND device
490  * @pos: the NAND position to convert
491  *
492  * Converts @pos NAND position into an absolute offset.
493  *
494  * Return: the absolute offset. Note that @pos points to the beginning of a
495  *	   page, if one wants to point to a specific offset within this page
496  *	   the returned offset has to be adjusted manually.
497  */
nanddev_pos_to_offs(struct nand_device * nand,const struct nand_pos * pos)498 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
499 					 const struct nand_pos *pos)
500 {
501 	unsigned int npages;
502 
503 	npages = pos->page +
504 		 ((pos->eraseblock +
505 		   (pos->lun +
506 		    (pos->target * nand->memorg.luns_per_target)) *
507 		   nand->memorg.eraseblocks_per_lun) *
508 		  nand->memorg.pages_per_eraseblock);
509 
510 	return (loff_t)npages * nand->memorg.pagesize;
511 }
512 
513 /**
514  * nanddev_pos_to_row() - Extract a row address from a NAND position
515  * @nand: NAND device
516  * @pos: the position to convert
517  *
518  * Converts a NAND position into a row address that can then be passed to the
519  * device.
520  *
521  * Return: the row address extracted from @pos.
522  */
nanddev_pos_to_row(struct nand_device * nand,const struct nand_pos * pos)523 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
524 					      const struct nand_pos *pos)
525 {
526 	return (pos->lun << nand->rowconv.lun_addr_shift) |
527 	       (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
528 	       pos->page;
529 }
530 
531 /**
532  * nanddev_pos_next_target() - Move a position to the next target/die
533  * @nand: NAND device
534  * @pos: the position to update
535  *
536  * Updates @pos to point to the start of the next target/die. Useful when you
537  * want to iterate over all targets/dies of a NAND device.
538  */
nanddev_pos_next_target(struct nand_device * nand,struct nand_pos * pos)539 static inline void nanddev_pos_next_target(struct nand_device *nand,
540 					   struct nand_pos *pos)
541 {
542 	pos->page = 0;
543 	pos->plane = 0;
544 	pos->eraseblock = 0;
545 	pos->lun = 0;
546 	pos->target++;
547 }
548 
549 /**
550  * nanddev_pos_next_lun() - Move a position to the next LUN
551  * @nand: NAND device
552  * @pos: the position to update
553  *
554  * Updates @pos to point to the start of the next LUN. Useful when you want to
555  * iterate over all LUNs of a NAND device.
556  */
nanddev_pos_next_lun(struct nand_device * nand,struct nand_pos * pos)557 static inline void nanddev_pos_next_lun(struct nand_device *nand,
558 					struct nand_pos *pos)
559 {
560 	if (pos->lun >= nand->memorg.luns_per_target - 1)
561 		return nanddev_pos_next_target(nand, pos);
562 
563 	pos->lun++;
564 	pos->page = 0;
565 	pos->plane = 0;
566 	pos->eraseblock = 0;
567 }
568 
569 /**
570  * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
571  * @nand: NAND device
572  * @pos: the position to update
573  *
574  * Updates @pos to point to the start of the next eraseblock. Useful when you
575  * want to iterate over all eraseblocks of a NAND device.
576  */
nanddev_pos_next_eraseblock(struct nand_device * nand,struct nand_pos * pos)577 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
578 					       struct nand_pos *pos)
579 {
580 	if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
581 		return nanddev_pos_next_lun(nand, pos);
582 
583 	pos->eraseblock++;
584 	pos->page = 0;
585 	pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
586 }
587 
588 /**
589  * nanddev_pos_next_eraseblock() - Move a position to the next page
590  * @nand: NAND device
591  * @pos: the position to update
592  *
593  * Updates @pos to point to the start of the next page. Useful when you want to
594  * iterate over all pages of a NAND device.
595  */
nanddev_pos_next_page(struct nand_device * nand,struct nand_pos * pos)596 static inline void nanddev_pos_next_page(struct nand_device *nand,
597 					 struct nand_pos *pos)
598 {
599 	if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
600 		return nanddev_pos_next_eraseblock(nand, pos);
601 
602 	pos->page++;
603 }
604 
605 /**
606  * nand_io_iter_init - Initialize a NAND I/O iterator
607  * @nand: NAND device
608  * @offs: absolute offset
609  * @req: MTD request
610  * @iter: NAND I/O iterator
611  *
612  * Initializes a NAND iterator based on the information passed by the MTD
613  * layer.
614  */
nanddev_io_iter_init(struct nand_device * nand,loff_t offs,struct mtd_oob_ops * req,struct nand_io_iter * iter)615 static inline void nanddev_io_iter_init(struct nand_device *nand,
616 					loff_t offs, struct mtd_oob_ops *req,
617 					struct nand_io_iter *iter)
618 {
619 	struct mtd_info *mtd = nanddev_to_mtd(nand);
620 
621 	iter->req.mode = req->mode;
622 	iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
623 	iter->req.ooboffs = req->ooboffs;
624 	iter->oobbytes_per_page = mtd_oobavail(mtd, req);
625 	iter->dataleft = req->len;
626 	iter->oobleft = req->ooblen;
627 	iter->req.databuf.in = req->datbuf;
628 	iter->req.datalen = min_t(unsigned int,
629 				  nand->memorg.pagesize - iter->req.dataoffs,
630 				  iter->dataleft);
631 	iter->req.oobbuf.in = req->oobbuf;
632 	iter->req.ooblen = min_t(unsigned int,
633 				 iter->oobbytes_per_page - iter->req.ooboffs,
634 				 iter->oobleft);
635 }
636 
637 /**
638  * nand_io_iter_next_page - Move to the next page
639  * @nand: NAND device
640  * @iter: NAND I/O iterator
641  *
642  * Updates the @iter to point to the next page.
643  */
nanddev_io_iter_next_page(struct nand_device * nand,struct nand_io_iter * iter)644 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
645 					     struct nand_io_iter *iter)
646 {
647 	nanddev_pos_next_page(nand, &iter->req.pos);
648 	iter->dataleft -= iter->req.datalen;
649 	iter->req.databuf.in += iter->req.datalen;
650 	iter->oobleft -= iter->req.ooblen;
651 	iter->req.oobbuf.in += iter->req.ooblen;
652 	iter->req.dataoffs = 0;
653 	iter->req.ooboffs = 0;
654 	iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
655 				  iter->dataleft);
656 	iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
657 				 iter->oobleft);
658 }
659 
660 /**
661  * nand_io_iter_end - Should end iteration or not
662  * @nand: NAND device
663  * @iter: NAND I/O iterator
664  *
665  * Check whether @iter has reached the end of the NAND portion it was asked to
666  * iterate on or not.
667  *
668  * Return: true if @iter has reached the end of the iteration request, false
669  *	   otherwise.
670  */
nanddev_io_iter_end(struct nand_device * nand,const struct nand_io_iter * iter)671 static inline bool nanddev_io_iter_end(struct nand_device *nand,
672 				       const struct nand_io_iter *iter)
673 {
674 	if (iter->dataleft || iter->oobleft)
675 		return false;
676 
677 	return true;
678 }
679 
680 /**
681  * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
682  *			   request
683  * @nand: NAND device
684  * @start: start address to read/write from
685  * @req: MTD I/O request
686  * @iter: NAND I/O iterator
687  *
688  * Should be used for iterate over pages that are contained in an MTD request.
689  */
690 #define nanddev_io_for_each_page(nand, start, req, iter)		\
691 	for (nanddev_io_iter_init(nand, start, req, iter);		\
692 	     !nanddev_io_iter_end(nand, iter);				\
693 	     nanddev_io_iter_next_page(nand, iter))
694 
695 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
696 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
697 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
698 
699 /* BBT related functions */
700 enum nand_bbt_block_status {
701 	NAND_BBT_BLOCK_STATUS_UNKNOWN,
702 	NAND_BBT_BLOCK_GOOD,
703 	NAND_BBT_BLOCK_WORN,
704 	NAND_BBT_BLOCK_RESERVED,
705 	NAND_BBT_BLOCK_FACTORY_BAD,
706 	NAND_BBT_BLOCK_NUM_STATUS,
707 };
708 
709 int nanddev_bbt_init(struct nand_device *nand);
710 void nanddev_bbt_cleanup(struct nand_device *nand);
711 int nanddev_bbt_update(struct nand_device *nand);
712 int nanddev_bbt_get_block_status(const struct nand_device *nand,
713 				 unsigned int entry);
714 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
715 				 enum nand_bbt_block_status status);
716 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
717 
718 /**
719  * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
720  * @nand: NAND device
721  * @pos: the NAND position we want to get BBT entry for
722  *
723  * Return the BBT entry used to store information about the eraseblock pointed
724  * by @pos.
725  *
726  * Return: the BBT entry storing information about eraseblock pointed by @pos.
727  */
nanddev_bbt_pos_to_entry(struct nand_device * nand,const struct nand_pos * pos)728 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
729 						    const struct nand_pos *pos)
730 {
731 	return pos->eraseblock +
732 	       ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
733 		nand->memorg.eraseblocks_per_lun);
734 }
735 
736 /**
737  * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
738  * @nand: NAND device
739  *
740  * Return: true if the BBT has been initialized, false otherwise.
741  */
nanddev_bbt_is_initialized(struct nand_device * nand)742 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
743 {
744 	return !!nand->bbt.cache;
745 }
746 
747 /* MTD -> NAND helper functions. */
748 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
749 
750 #endif /* __LINUX_MTD_NAND_H */
751