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