1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright 2017 - Free Electrons
6 * Boris Brezillon <boris.brezillon@free-electrons.com>
7 * Peter Pan <peterpandong@micron.com>
10 #ifndef __LINUX_MTD_NAND_H
11 #define __LINUX_MTD_NAND_H
13 #include <linux/mtd/mtd.h>
18 * struct nand_memory_organization - Memory organization structure
19 * @bits_per_cell: number of bits per NAND cell
20 * @pagesize: page size
21 * @oobsize: OOB area size
22 * @pages_per_eraseblock: number of pages per eraseblock
23 * @eraseblocks_per_lun: number of eraseblocks per LUN (Logical Unit Number)
24 * @max_bad_eraseblocks_per_lun: maximum number of eraseblocks per LUN
25 * @planes_per_lun: number of planes per LUN
26 * @luns_per_target: number of LUN per target (target is a synonym for die)
27 * @ntargets: total number of targets exposed by the NAND device
29 struct nand_memory_organization {
30 unsigned int bits_per_cell;
31 unsigned int pagesize;
33 unsigned int pages_per_eraseblock;
34 unsigned int eraseblocks_per_lun;
35 unsigned int max_bad_eraseblocks_per_lun;
36 unsigned int planes_per_lun;
37 unsigned int luns_per_target;
38 unsigned int ntargets;
41 #define NAND_MEMORG(bpc, ps, os, ppe, epl, mbb, ppl, lpt, nt) \
43 .bits_per_cell = (bpc), \
46 .pages_per_eraseblock = (ppe), \
47 .eraseblocks_per_lun = (epl), \
48 .max_bad_eraseblocks_per_lun = (mbb), \
49 .planes_per_lun = (ppl), \
50 .luns_per_target = (lpt), \
55 * struct nand_row_converter - Information needed to convert an absolute offset
57 * @lun_addr_shift: position of the LUN identifier in the row address
58 * @eraseblock_addr_shift: position of the eraseblock identifier in the row
61 struct nand_row_converter {
62 unsigned int lun_addr_shift;
63 unsigned int eraseblock_addr_shift;
67 * struct nand_pos - NAND position object
68 * @target: the NAND target/die
69 * @lun: the LUN identifier
70 * @plane: the plane within the LUN
71 * @eraseblock: the eraseblock within the LUN
72 * @page: the page within the LUN
74 * These information are usually used by specific sub-layers to select the
75 * appropriate target/die and generate a row address to pass to the device.
81 unsigned int eraseblock;
86 * enum nand_page_io_req_type - Direction of an I/O request
87 * @NAND_PAGE_READ: from the chip, to the controller
88 * @NAND_PAGE_WRITE: from the controller, to the chip
90 enum nand_page_io_req_type {
96 * struct nand_page_io_req - NAND I/O request object
97 * @type: the type of page I/O: read or write
98 * @pos: the position this I/O request is targeting
99 * @dataoffs: the offset within the page
100 * @datalen: number of data bytes to read from/write to this page
101 * @databuf: buffer to store data in or get data from
102 * @ooboffs: the OOB offset within the page
103 * @ooblen: the number of OOB bytes to read from/write to this page
104 * @oobbuf: buffer to store OOB data in or get OOB data from
105 * @mode: one of the %MTD_OPS_XXX mode
107 * This object is used to pass per-page I/O requests to NAND sub-layers. This
108 * way all useful information are already formatted in a useful way and
109 * specific NAND layers can focus on translating these information into
110 * specific commands/operations.
112 struct nand_page_io_req {
113 enum nand_page_io_req_type type;
115 unsigned int dataoffs;
116 unsigned int datalen;
121 unsigned int ooboffs;
130 const struct mtd_ooblayout_ops *nand_get_small_page_ooblayout(void);
131 const struct mtd_ooblayout_ops *nand_get_large_page_ooblayout(void);
132 const struct mtd_ooblayout_ops *nand_get_large_page_hamming_ooblayout(void);
135 * enum nand_ecc_engine_type - NAND ECC engine type
136 * @NAND_ECC_ENGINE_TYPE_INVALID: Invalid value
137 * @NAND_ECC_ENGINE_TYPE_NONE: No ECC correction
138 * @NAND_ECC_ENGINE_TYPE_SOFT: Software ECC correction
139 * @NAND_ECC_ENGINE_TYPE_ON_HOST: On host hardware ECC correction
140 * @NAND_ECC_ENGINE_TYPE_ON_DIE: On chip hardware ECC correction
142 enum nand_ecc_engine_type {
143 NAND_ECC_ENGINE_TYPE_INVALID,
144 NAND_ECC_ENGINE_TYPE_NONE,
145 NAND_ECC_ENGINE_TYPE_SOFT,
146 NAND_ECC_ENGINE_TYPE_ON_HOST,
147 NAND_ECC_ENGINE_TYPE_ON_DIE,
151 * enum nand_ecc_placement - NAND ECC bytes placement
152 * @NAND_ECC_PLACEMENT_UNKNOWN: The actual position of the ECC bytes is unknown
153 * @NAND_ECC_PLACEMENT_OOB: The ECC bytes are located in the OOB area
154 * @NAND_ECC_PLACEMENT_INTERLEAVED: Syndrome layout, there are ECC bytes
155 * interleaved with regular data in the main
158 enum nand_ecc_placement {
159 NAND_ECC_PLACEMENT_UNKNOWN,
160 NAND_ECC_PLACEMENT_OOB,
161 NAND_ECC_PLACEMENT_INTERLEAVED,
165 * enum nand_ecc_algo - NAND ECC algorithm
166 * @NAND_ECC_ALGO_UNKNOWN: Unknown algorithm
167 * @NAND_ECC_ALGO_HAMMING: Hamming algorithm
168 * @NAND_ECC_ALGO_BCH: Bose-Chaudhuri-Hocquenghem algorithm
169 * @NAND_ECC_ALGO_RS: Reed-Solomon algorithm
172 NAND_ECC_ALGO_UNKNOWN,
173 NAND_ECC_ALGO_HAMMING,
179 * struct nand_ecc_props - NAND ECC properties
180 * @engine_type: ECC engine type
181 * @placement: OOB placement (if relevant)
182 * @algo: ECC algorithm (if relevant)
183 * @strength: ECC strength
184 * @step_size: Number of bytes per step
185 * @flags: Misc properties
187 struct nand_ecc_props {
188 enum nand_ecc_engine_type engine_type;
189 enum nand_ecc_placement placement;
190 enum nand_ecc_algo algo;
191 unsigned int strength;
192 unsigned int step_size;
196 #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
198 /* NAND ECC misc flags */
199 #define NAND_ECC_MAXIMIZE_STRENGTH BIT(0)
202 * struct nand_bbt - bad block table object
203 * @cache: in memory BBT cache
206 unsigned long *cache;
210 * struct nand_ops - NAND operations
211 * @erase: erase a specific block. No need to check if the block is bad before
212 * erasing, this has been taken care of by the generic NAND layer
213 * @markbad: mark a specific block bad. No need to check if the block is
214 * already marked bad, this has been taken care of by the generic
215 * NAND layer. This method should just write the BBM (Bad Block
216 * Marker) so that future call to struct_nand_ops->isbad() return
218 * @isbad: check whether a block is bad or not. This method should just read
219 * the BBM and return whether the block is bad or not based on what it
222 * These are all low level operations that should be implemented by specialized
223 * NAND layers (SPI NAND, raw NAND, ...).
226 int (*erase)(struct nand_device *nand, const struct nand_pos *pos);
227 int (*markbad)(struct nand_device *nand, const struct nand_pos *pos);
228 bool (*isbad)(struct nand_device *nand, const struct nand_pos *pos);
232 * struct nand_ecc_context - Context for the ECC engine
233 * @conf: basic ECC engine parameters
234 * @total: total number of bytes used for storing ECC codes, this is used by
235 * generic OOB layouts
236 * @priv: ECC engine driver private data
238 struct nand_ecc_context {
239 struct nand_ecc_props conf;
245 * struct nand_ecc_engine_ops - ECC engine operations
246 * @init_ctx: given a desired user configuration for the pointed NAND device,
247 * requests the ECC engine driver to setup a configuration with
248 * values it supports.
249 * @cleanup_ctx: clean the context initialized by @init_ctx.
250 * @prepare_io_req: is called before reading/writing a page to prepare the I/O
251 * request to be performed with ECC correction.
252 * @finish_io_req: is called after reading/writing a page to terminate the I/O
253 * request and ensure proper ECC correction.
255 struct nand_ecc_engine_ops {
256 int (*init_ctx)(struct nand_device *nand);
257 void (*cleanup_ctx)(struct nand_device *nand);
258 int (*prepare_io_req)(struct nand_device *nand,
259 struct nand_page_io_req *req);
260 int (*finish_io_req)(struct nand_device *nand,
261 struct nand_page_io_req *req);
265 * struct nand_ecc_engine - ECC engine abstraction for NAND devices
266 * @ops: ECC engine operations
268 struct nand_ecc_engine {
269 struct nand_ecc_engine_ops *ops;
272 void of_get_nand_ecc_user_config(struct nand_device *nand);
273 int nand_ecc_init_ctx(struct nand_device *nand);
274 void nand_ecc_cleanup_ctx(struct nand_device *nand);
275 int nand_ecc_prepare_io_req(struct nand_device *nand,
276 struct nand_page_io_req *req);
277 int nand_ecc_finish_io_req(struct nand_device *nand,
278 struct nand_page_io_req *req);
279 bool nand_ecc_is_strong_enough(struct nand_device *nand);
280 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand);
281 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand);
283 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
284 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);
286 static inline struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void)
290 #endif /* CONFIG_MTD_NAND_ECC_SW_HAMMING */
292 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)
293 struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void);
295 static inline struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void)
299 #endif /* CONFIG_MTD_NAND_ECC_SW_BCH */
302 * struct nand_ecc_req_tweak_ctx - Help for automatically tweaking requests
303 * @orig_req: Pointer to the original IO request
304 * @nand: Related NAND device, to have access to its memory organization
305 * @page_buffer_size: Real size of the page buffer to use (can be set by the
306 * user before the tweaking mechanism initialization)
307 * @oob_buffer_size: Real size of the OOB buffer to use (can be set by the
308 * user before the tweaking mechanism initialization)
309 * @spare_databuf: Data bounce buffer
310 * @spare_oobbuf: OOB bounce buffer
311 * @bounce_data: Flag indicating a data bounce buffer is used
312 * @bounce_oob: Flag indicating an OOB bounce buffer is used
314 struct nand_ecc_req_tweak_ctx {
315 struct nand_page_io_req orig_req;
316 struct nand_device *nand;
317 unsigned int page_buffer_size;
318 unsigned int oob_buffer_size;
325 int nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx,
326 struct nand_device *nand);
327 void nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx);
328 void nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx *ctx,
329 struct nand_page_io_req *req);
330 void nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx *ctx,
331 struct nand_page_io_req *req);
334 * struct nand_ecc - Information relative to the ECC
335 * @defaults: Default values, depend on the underlying subsystem
336 * @requirements: ECC requirements from the NAND chip perspective
337 * @user_conf: User desires in terms of ECC parameters
338 * @ctx: ECC context for the ECC engine, derived from the device @requirements
339 * the @user_conf and the @defaults
340 * @ondie_engine: On-die ECC engine reference, if any
341 * @engine: ECC engine actually bound
344 struct nand_ecc_props defaults;
345 struct nand_ecc_props requirements;
346 struct nand_ecc_props user_conf;
347 struct nand_ecc_context ctx;
348 struct nand_ecc_engine *ondie_engine;
349 struct nand_ecc_engine *engine;
353 * struct nand_device - NAND device
354 * @mtd: MTD instance attached to the NAND device
355 * @memorg: memory layout
356 * @ecc: NAND ECC object attached to the NAND device
357 * @rowconv: position to row address converter
358 * @bbt: bad block table info
359 * @ops: NAND operations attached to the NAND device
361 * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND)
362 * should declare their own NAND object embedding a nand_device struct (that's
363 * how inheritance is done).
364 * struct_nand_device->memorg and struct_nand_device->ecc.requirements should
365 * be filled at device detection time to reflect the NAND device
366 * capabilities/requirements. Once this is done nanddev_init() can be called.
367 * It will take care of converting NAND information into MTD ones, which means
368 * the specialized NAND layers should never manually tweak
369 * struct_nand_device->mtd except for the ->_read/write() hooks.
373 struct nand_memory_organization memorg;
375 struct nand_row_converter rowconv;
377 const struct nand_ops *ops;
381 * struct nand_io_iter - NAND I/O iterator
382 * @req: current I/O request
383 * @oobbytes_per_page: maximum number of OOB bytes per page
384 * @dataleft: remaining number of data bytes to read/write
385 * @oobleft: remaining number of OOB bytes to read/write
387 * Can be used by specialized NAND layers to iterate over all pages covered
388 * by an MTD I/O request, which should greatly simplifies the boiler-plate
389 * code needed to read/write data from/to a NAND device.
391 struct nand_io_iter {
392 struct nand_page_io_req req;
393 unsigned int oobbytes_per_page;
394 unsigned int dataleft;
395 unsigned int oobleft;
399 * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
402 * Return: the NAND device embedding @mtd.
404 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
406 return container_of(mtd, struct nand_device, mtd);
410 * nanddev_to_mtd() - Get the MTD device attached to a NAND device
413 * Return: the MTD device embedded in @nand.
415 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
421 * nanddev_bits_per_cell() - Get the number of bits per cell
424 * Return: the number of bits per cell.
426 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
428 return nand->memorg.bits_per_cell;
432 * nanddev_page_size() - Get NAND page size
435 * Return: the page size.
437 static inline size_t nanddev_page_size(const struct nand_device *nand)
439 return nand->memorg.pagesize;
443 * nanddev_per_page_oobsize() - Get NAND OOB size
446 * Return: the OOB size.
448 static inline unsigned int
449 nanddev_per_page_oobsize(const struct nand_device *nand)
451 return nand->memorg.oobsize;
455 * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
458 * Return: the number of pages per eraseblock.
460 static inline unsigned int
461 nanddev_pages_per_eraseblock(const struct nand_device *nand)
463 return nand->memorg.pages_per_eraseblock;
467 * nanddev_pages_per_target() - Get the number of pages per target
470 * Return: the number of pages per target.
472 static inline unsigned int
473 nanddev_pages_per_target(const struct nand_device *nand)
475 return nand->memorg.pages_per_eraseblock *
476 nand->memorg.eraseblocks_per_lun *
477 nand->memorg.luns_per_target;
481 * nanddev_per_page_oobsize() - Get NAND erase block size
484 * Return: the eraseblock size.
486 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
488 return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
492 * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
495 * Return: the number of eraseblocks per LUN.
497 static inline unsigned int
498 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
500 return nand->memorg.eraseblocks_per_lun;
504 * nanddev_eraseblocks_per_target() - Get the number of eraseblocks per target
507 * Return: the number of eraseblocks per target.
509 static inline unsigned int
510 nanddev_eraseblocks_per_target(const struct nand_device *nand)
512 return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
516 * nanddev_target_size() - Get the total size provided by a single target/die
519 * Return: the total size exposed by a single target/die in bytes.
521 static inline u64 nanddev_target_size(const struct nand_device *nand)
523 return (u64)nand->memorg.luns_per_target *
524 nand->memorg.eraseblocks_per_lun *
525 nand->memorg.pages_per_eraseblock *
526 nand->memorg.pagesize;
530 * nanddev_ntarget() - Get the total of targets
533 * Return: the number of targets/dies exposed by @nand.
535 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
537 return nand->memorg.ntargets;
541 * nanddev_neraseblocks() - Get the total number of eraseblocks
544 * Return: the total number of eraseblocks exposed by @nand.
546 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
548 return nand->memorg.ntargets * nand->memorg.luns_per_target *
549 nand->memorg.eraseblocks_per_lun;
553 * nanddev_size() - Get NAND size
556 * Return: the total size (in bytes) exposed by @nand.
558 static inline u64 nanddev_size(const struct nand_device *nand)
560 return nanddev_target_size(nand) * nanddev_ntargets(nand);
564 * nanddev_get_memorg() - Extract memory organization info from a NAND device
567 * This can be used by the upper layer to fill the memorg info before calling
570 * Return: the memorg object embedded in the NAND device.
572 static inline struct nand_memory_organization *
573 nanddev_get_memorg(struct nand_device *nand)
575 return &nand->memorg;
579 * nanddev_get_ecc_conf() - Extract the ECC configuration from a NAND device
582 static inline const struct nand_ecc_props *
583 nanddev_get_ecc_conf(struct nand_device *nand)
585 return &nand->ecc.ctx.conf;
589 * nanddev_get_ecc_requirements() - Extract the ECC requirements from a NAND
593 static inline const struct nand_ecc_props *
594 nanddev_get_ecc_requirements(struct nand_device *nand)
596 return &nand->ecc.requirements;
600 * nanddev_set_ecc_requirements() - Assign the ECC requirements of a NAND
603 * @reqs: Requirements
606 nanddev_set_ecc_requirements(struct nand_device *nand,
607 const struct nand_ecc_props *reqs)
609 nand->ecc.requirements = *reqs;
612 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
613 struct module *owner);
614 void nanddev_cleanup(struct nand_device *nand);
617 * nanddev_register() - Register a NAND device
620 * Register a NAND device.
621 * This function is just a wrapper around mtd_device_register()
622 * registering the MTD device embedded in @nand.
624 * Return: 0 in case of success, a negative error code otherwise.
626 static inline int nanddev_register(struct nand_device *nand)
628 return mtd_device_register(&nand->mtd, NULL, 0);
632 * nanddev_unregister() - Unregister a NAND device
635 * Unregister a NAND device.
636 * This function is just a wrapper around mtd_device_unregister()
637 * unregistering the MTD device embedded in @nand.
639 * Return: 0 in case of success, a negative error code otherwise.
641 static inline int nanddev_unregister(struct nand_device *nand)
643 return mtd_device_unregister(&nand->mtd);
647 * nanddev_set_of_node() - Attach a DT node to a NAND device
651 * Attach a DT node to a NAND device.
653 static inline void nanddev_set_of_node(struct nand_device *nand,
654 struct device_node *np)
656 mtd_set_of_node(&nand->mtd, np);
660 * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
663 * Return: the DT node attached to @nand.
665 static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
667 return mtd_get_of_node(&nand->mtd);
671 * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
673 * @offs: absolute NAND offset (usually passed by the MTD layer)
674 * @pos: a NAND position object to fill in
676 * Converts @offs into a nand_pos representation.
678 * Return: the offset within the NAND page pointed by @pos.
680 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
682 struct nand_pos *pos)
684 unsigned int pageoffs;
687 pageoffs = do_div(tmp, nand->memorg.pagesize);
688 pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
689 pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
690 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
691 pos->lun = do_div(tmp, nand->memorg.luns_per_target);
698 * nanddev_pos_cmp() - Compare two NAND positions
699 * @a: First NAND position
700 * @b: Second NAND position
702 * Compares two NAND positions.
704 * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
706 static inline int nanddev_pos_cmp(const struct nand_pos *a,
707 const struct nand_pos *b)
709 if (a->target != b->target)
710 return a->target < b->target ? -1 : 1;
712 if (a->lun != b->lun)
713 return a->lun < b->lun ? -1 : 1;
715 if (a->eraseblock != b->eraseblock)
716 return a->eraseblock < b->eraseblock ? -1 : 1;
718 if (a->page != b->page)
719 return a->page < b->page ? -1 : 1;
725 * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
727 * @pos: the NAND position to convert
729 * Converts @pos NAND position into an absolute offset.
731 * Return: the absolute offset. Note that @pos points to the beginning of a
732 * page, if one wants to point to a specific offset within this page
733 * the returned offset has to be adjusted manually.
735 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
736 const struct nand_pos *pos)
743 (pos->target * nand->memorg.luns_per_target)) *
744 nand->memorg.eraseblocks_per_lun) *
745 nand->memorg.pages_per_eraseblock);
747 return (loff_t)npages * nand->memorg.pagesize;
751 * nanddev_pos_to_row() - Extract a row address from a NAND position
753 * @pos: the position to convert
755 * Converts a NAND position into a row address that can then be passed to the
758 * Return: the row address extracted from @pos.
760 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
761 const struct nand_pos *pos)
763 return (pos->lun << nand->rowconv.lun_addr_shift) |
764 (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
769 * nanddev_pos_next_target() - Move a position to the next target/die
771 * @pos: the position to update
773 * Updates @pos to point to the start of the next target/die. Useful when you
774 * want to iterate over all targets/dies of a NAND device.
776 static inline void nanddev_pos_next_target(struct nand_device *nand,
777 struct nand_pos *pos)
787 * nanddev_pos_next_lun() - Move a position to the next LUN
789 * @pos: the position to update
791 * Updates @pos to point to the start of the next LUN. Useful when you want to
792 * iterate over all LUNs of a NAND device.
794 static inline void nanddev_pos_next_lun(struct nand_device *nand,
795 struct nand_pos *pos)
797 if (pos->lun >= nand->memorg.luns_per_target - 1)
798 return nanddev_pos_next_target(nand, pos);
807 * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
809 * @pos: the position to update
811 * Updates @pos to point to the start of the next eraseblock. Useful when you
812 * want to iterate over all eraseblocks of a NAND device.
814 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
815 struct nand_pos *pos)
817 if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
818 return nanddev_pos_next_lun(nand, pos);
822 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
826 * nanddev_pos_next_page() - Move a position to the next page
828 * @pos: the position to update
830 * Updates @pos to point to the start of the next page. Useful when you want to
831 * iterate over all pages of a NAND device.
833 static inline void nanddev_pos_next_page(struct nand_device *nand,
834 struct nand_pos *pos)
836 if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
837 return nanddev_pos_next_eraseblock(nand, pos);
843 * nand_io_iter_init - Initialize a NAND I/O iterator
845 * @offs: absolute offset
847 * @iter: NAND I/O iterator
849 * Initializes a NAND iterator based on the information passed by the MTD
852 static inline void nanddev_io_iter_init(struct nand_device *nand,
853 enum nand_page_io_req_type reqtype,
854 loff_t offs, struct mtd_oob_ops *req,
855 struct nand_io_iter *iter)
857 struct mtd_info *mtd = nanddev_to_mtd(nand);
859 iter->req.type = reqtype;
860 iter->req.mode = req->mode;
861 iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
862 iter->req.ooboffs = req->ooboffs;
863 iter->oobbytes_per_page = mtd_oobavail(mtd, req);
864 iter->dataleft = req->len;
865 iter->oobleft = req->ooblen;
866 iter->req.databuf.in = req->datbuf;
867 iter->req.datalen = min_t(unsigned int,
868 nand->memorg.pagesize - iter->req.dataoffs,
870 iter->req.oobbuf.in = req->oobbuf;
871 iter->req.ooblen = min_t(unsigned int,
872 iter->oobbytes_per_page - iter->req.ooboffs,
877 * nand_io_iter_next_page - Move to the next page
879 * @iter: NAND I/O iterator
881 * Updates the @iter to point to the next page.
883 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
884 struct nand_io_iter *iter)
886 nanddev_pos_next_page(nand, &iter->req.pos);
887 iter->dataleft -= iter->req.datalen;
888 iter->req.databuf.in += iter->req.datalen;
889 iter->oobleft -= iter->req.ooblen;
890 iter->req.oobbuf.in += iter->req.ooblen;
891 iter->req.dataoffs = 0;
892 iter->req.ooboffs = 0;
893 iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
895 iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
900 * nand_io_iter_end - Should end iteration or not
902 * @iter: NAND I/O iterator
904 * Check whether @iter has reached the end of the NAND portion it was asked to
907 * Return: true if @iter has reached the end of the iteration request, false
910 static inline bool nanddev_io_iter_end(struct nand_device *nand,
911 const struct nand_io_iter *iter)
913 if (iter->dataleft || iter->oobleft)
920 * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
923 * @start: start address to read/write from
924 * @req: MTD I/O request
925 * @iter: NAND I/O iterator
927 * Should be used for iterate over pages that are contained in an MTD request.
929 #define nanddev_io_for_each_page(nand, type, start, req, iter) \
930 for (nanddev_io_iter_init(nand, type, start, req, iter); \
931 !nanddev_io_iter_end(nand, iter); \
932 nanddev_io_iter_next_page(nand, iter))
934 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
935 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
936 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos);
937 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
939 /* ECC related functions */
940 int nanddev_ecc_engine_init(struct nand_device *nand);
941 void nanddev_ecc_engine_cleanup(struct nand_device *nand);
943 /* BBT related functions */
944 enum nand_bbt_block_status {
945 NAND_BBT_BLOCK_STATUS_UNKNOWN,
948 NAND_BBT_BLOCK_RESERVED,
949 NAND_BBT_BLOCK_FACTORY_BAD,
950 NAND_BBT_BLOCK_NUM_STATUS,
953 int nanddev_bbt_init(struct nand_device *nand);
954 void nanddev_bbt_cleanup(struct nand_device *nand);
955 int nanddev_bbt_update(struct nand_device *nand);
956 int nanddev_bbt_get_block_status(const struct nand_device *nand,
958 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
959 enum nand_bbt_block_status status);
960 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
963 * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
965 * @pos: the NAND position we want to get BBT entry for
967 * Return the BBT entry used to store information about the eraseblock pointed
970 * Return: the BBT entry storing information about eraseblock pointed by @pos.
972 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
973 const struct nand_pos *pos)
975 return pos->eraseblock +
976 ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
977 nand->memorg.eraseblocks_per_lun);
981 * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
984 * Return: true if the BBT has been initialized, false otherwise.
986 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
988 return !!nand->bbt.cache;
991 /* MTD -> NAND helper functions. */
992 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
993 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
995 #endif /* __LINUX_MTD_NAND_H */