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>
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
27 struct nand_memory_organization {
28 unsigned int bits_per_cell;
29 unsigned int pagesize;
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;
39 #define NAND_MEMORG(bpc, ps, os, ppe, epl, mbb, ppl, lpt, nt) \
41 .bits_per_cell = (bpc), \
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), \
53 * struct nand_row_converter - Information needed to convert an absolute offset
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
59 struct nand_row_converter {
60 unsigned int lun_addr_shift;
61 unsigned int eraseblock_addr_shift;
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
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.
79 unsigned int eraseblock;
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
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.
99 struct nand_page_io_req {
101 unsigned int dataoffs;
102 unsigned int datalen;
107 unsigned int ooboffs;
117 * struct nand_ecc_req - NAND ECC requirements
118 * @strength: ECC strength
119 * @step_size: ECC step/block size
121 struct nand_ecc_req {
122 unsigned int strength;
123 unsigned int step_size;
126 #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
129 * struct nand_bbt - bad block table object
130 * @cache: in memory BBT cache
133 unsigned long *cache;
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
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
151 * These are all low level operations that should be implemented by specialized
152 * NAND layers (SPI NAND, raw NAND, ...).
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);
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
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.
181 struct nand_memory_organization memorg;
182 struct nand_ecc_req eccreq;
183 struct nand_row_converter rowconv;
185 const struct nand_ops *ops;
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
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.
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;
207 * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
210 * Return: the NAND device embedding @mtd.
212 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
214 return container_of(mtd, struct nand_device, mtd);
218 * nanddev_to_mtd() - Get the MTD device attached to a NAND device
221 * Return: the MTD device embedded in @nand.
223 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
229 * nanddev_bits_per_cell() - Get the number of bits per cell
232 * Return: the number of bits per cell.
234 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
236 return nand->memorg.bits_per_cell;
240 * nanddev_page_size() - Get NAND page size
243 * Return: the page size.
245 static inline size_t nanddev_page_size(const struct nand_device *nand)
247 return nand->memorg.pagesize;
251 * nanddev_per_page_oobsize() - Get NAND OOB size
254 * Return: the OOB size.
256 static inline unsigned int
257 nanddev_per_page_oobsize(const struct nand_device *nand)
259 return nand->memorg.oobsize;
263 * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
266 * Return: the number of pages per eraseblock.
268 static inline unsigned int
269 nanddev_pages_per_eraseblock(const struct nand_device *nand)
271 return nand->memorg.pages_per_eraseblock;
275 * nanddev_pages_per_target() - Get the number of pages per target
278 * Return: the number of pages per target.
280 static inline unsigned int
281 nanddev_pages_per_target(const struct nand_device *nand)
283 return nand->memorg.pages_per_eraseblock *
284 nand->memorg.eraseblocks_per_lun *
285 nand->memorg.luns_per_target;
289 * nanddev_per_page_oobsize() - Get NAND erase block size
292 * Return: the eraseblock size.
294 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
296 return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
300 * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
303 * Return: the number of eraseblocks per LUN.
305 static inline unsigned int
306 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
308 return nand->memorg.eraseblocks_per_lun;
312 * nanddev_eraseblocks_per_target() - Get the number of eraseblocks per target
315 * Return: the number of eraseblocks per target.
317 static inline unsigned int
318 nanddev_eraseblocks_per_target(const struct nand_device *nand)
320 return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
324 * nanddev_target_size() - Get the total size provided by a single target/die
327 * Return: the total size exposed by a single target/die in bytes.
329 static inline u64 nanddev_target_size(const struct nand_device *nand)
331 return (u64)nand->memorg.luns_per_target *
332 nand->memorg.eraseblocks_per_lun *
333 nand->memorg.pages_per_eraseblock *
334 nand->memorg.pagesize;
338 * nanddev_ntarget() - Get the total of targets
341 * Return: the number of targets/dies exposed by @nand.
343 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
345 return nand->memorg.ntargets;
349 * nanddev_neraseblocks() - Get the total number of erasablocks
352 * Return: the total number of eraseblocks exposed by @nand.
354 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
356 return nand->memorg.ntargets * nand->memorg.luns_per_target *
357 nand->memorg.eraseblocks_per_lun;
361 * nanddev_size() - Get NAND size
364 * Return: the total size (in bytes) exposed by @nand.
366 static inline u64 nanddev_size(const struct nand_device *nand)
368 return nanddev_target_size(nand) * nanddev_ntargets(nand);
372 * nanddev_get_memorg() - Extract memory organization info from a NAND device
375 * This can be used by the upper layer to fill the memorg info before calling
378 * Return: the memorg object embedded in the NAND device.
380 static inline struct nand_memory_organization *
381 nanddev_get_memorg(struct nand_device *nand)
383 return &nand->memorg;
386 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
387 struct module *owner);
388 void nanddev_cleanup(struct nand_device *nand);
391 * nanddev_register() - Register a NAND device
394 * Register a NAND device.
395 * This function is just a wrapper around mtd_device_register()
396 * registering the MTD device embedded in @nand.
398 * Return: 0 in case of success, a negative error code otherwise.
400 static inline int nanddev_register(struct nand_device *nand)
402 return mtd_device_register(&nand->mtd, NULL, 0);
406 * nanddev_unregister() - Unregister a NAND device
409 * Unregister a NAND device.
410 * This function is just a wrapper around mtd_device_unregister()
411 * unregistering the MTD device embedded in @nand.
413 * Return: 0 in case of success, a negative error code otherwise.
415 static inline int nanddev_unregister(struct nand_device *nand)
417 return mtd_device_unregister(&nand->mtd);
421 * nanddev_set_of_node() - Attach a DT node to a NAND device
425 * Attach a DT node to a NAND device.
427 static inline void nanddev_set_of_node(struct nand_device *nand,
428 struct device_node *np)
430 mtd_set_of_node(&nand->mtd, np);
434 * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
437 * Return: the DT node attached to @nand.
439 static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
441 return mtd_get_of_node(&nand->mtd);
445 * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
447 * @offs: absolute NAND offset (usually passed by the MTD layer)
448 * @pos: a NAND position object to fill in
450 * Converts @offs into a nand_pos representation.
452 * Return: the offset within the NAND page pointed by @pos.
454 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
456 struct nand_pos *pos)
458 unsigned int pageoffs;
461 pageoffs = do_div(tmp, nand->memorg.pagesize);
462 pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
463 pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
464 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
465 pos->lun = do_div(tmp, nand->memorg.luns_per_target);
472 * nanddev_pos_cmp() - Compare two NAND positions
473 * @a: First NAND position
474 * @b: Second NAND position
476 * Compares two NAND positions.
478 * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
480 static inline int nanddev_pos_cmp(const struct nand_pos *a,
481 const struct nand_pos *b)
483 if (a->target != b->target)
484 return a->target < b->target ? -1 : 1;
486 if (a->lun != b->lun)
487 return a->lun < b->lun ? -1 : 1;
489 if (a->eraseblock != b->eraseblock)
490 return a->eraseblock < b->eraseblock ? -1 : 1;
492 if (a->page != b->page)
493 return a->page < b->page ? -1 : 1;
499 * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
501 * @pos: the NAND position to convert
503 * Converts @pos NAND position into an absolute offset.
505 * Return: the absolute offset. Note that @pos points to the beginning of a
506 * page, if one wants to point to a specific offset within this page
507 * the returned offset has to be adjusted manually.
509 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
510 const struct nand_pos *pos)
517 (pos->target * nand->memorg.luns_per_target)) *
518 nand->memorg.eraseblocks_per_lun) *
519 nand->memorg.pages_per_eraseblock);
521 return (loff_t)npages * nand->memorg.pagesize;
525 * nanddev_pos_to_row() - Extract a row address from a NAND position
527 * @pos: the position to convert
529 * Converts a NAND position into a row address that can then be passed to the
532 * Return: the row address extracted from @pos.
534 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
535 const struct nand_pos *pos)
537 return (pos->lun << nand->rowconv.lun_addr_shift) |
538 (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
543 * nanddev_pos_next_target() - Move a position to the next target/die
545 * @pos: the position to update
547 * Updates @pos to point to the start of the next target/die. Useful when you
548 * want to iterate over all targets/dies of a NAND device.
550 static inline void nanddev_pos_next_target(struct nand_device *nand,
551 struct nand_pos *pos)
561 * nanddev_pos_next_lun() - Move a position to the next LUN
563 * @pos: the position to update
565 * Updates @pos to point to the start of the next LUN. Useful when you want to
566 * iterate over all LUNs of a NAND device.
568 static inline void nanddev_pos_next_lun(struct nand_device *nand,
569 struct nand_pos *pos)
571 if (pos->lun >= nand->memorg.luns_per_target - 1)
572 return nanddev_pos_next_target(nand, pos);
581 * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
583 * @pos: the position to update
585 * Updates @pos to point to the start of the next eraseblock. Useful when you
586 * want to iterate over all eraseblocks of a NAND device.
588 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
589 struct nand_pos *pos)
591 if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
592 return nanddev_pos_next_lun(nand, pos);
596 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
600 * nanddev_pos_next_page() - Move a position to the next page
602 * @pos: the position to update
604 * Updates @pos to point to the start of the next page. Useful when you want to
605 * iterate over all pages of a NAND device.
607 static inline void nanddev_pos_next_page(struct nand_device *nand,
608 struct nand_pos *pos)
610 if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
611 return nanddev_pos_next_eraseblock(nand, pos);
617 * nand_io_iter_init - Initialize a NAND I/O iterator
619 * @offs: absolute offset
621 * @iter: NAND I/O iterator
623 * Initializes a NAND iterator based on the information passed by the MTD
626 static inline void nanddev_io_iter_init(struct nand_device *nand,
627 loff_t offs, struct mtd_oob_ops *req,
628 struct nand_io_iter *iter)
630 struct mtd_info *mtd = nanddev_to_mtd(nand);
632 iter->req.mode = req->mode;
633 iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
634 iter->req.ooboffs = req->ooboffs;
635 iter->oobbytes_per_page = mtd_oobavail(mtd, req);
636 iter->dataleft = req->len;
637 iter->oobleft = req->ooblen;
638 iter->req.databuf.in = req->datbuf;
639 iter->req.datalen = min_t(unsigned int,
640 nand->memorg.pagesize - iter->req.dataoffs,
642 iter->req.oobbuf.in = req->oobbuf;
643 iter->req.ooblen = min_t(unsigned int,
644 iter->oobbytes_per_page - iter->req.ooboffs,
649 * nand_io_iter_next_page - Move to the next page
651 * @iter: NAND I/O iterator
653 * Updates the @iter to point to the next page.
655 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
656 struct nand_io_iter *iter)
658 nanddev_pos_next_page(nand, &iter->req.pos);
659 iter->dataleft -= iter->req.datalen;
660 iter->req.databuf.in += iter->req.datalen;
661 iter->oobleft -= iter->req.ooblen;
662 iter->req.oobbuf.in += iter->req.ooblen;
663 iter->req.dataoffs = 0;
664 iter->req.ooboffs = 0;
665 iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
667 iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
672 * nand_io_iter_end - Should end iteration or not
674 * @iter: NAND I/O iterator
676 * Check whether @iter has reached the end of the NAND portion it was asked to
679 * Return: true if @iter has reached the end of the iteration request, false
682 static inline bool nanddev_io_iter_end(struct nand_device *nand,
683 const struct nand_io_iter *iter)
685 if (iter->dataleft || iter->oobleft)
692 * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
695 * @start: start address to read/write from
696 * @req: MTD I/O request
697 * @iter: NAND I/O iterator
699 * Should be used for iterate over pages that are contained in an MTD request.
701 #define nanddev_io_for_each_page(nand, start, req, iter) \
702 for (nanddev_io_iter_init(nand, start, req, iter); \
703 !nanddev_io_iter_end(nand, iter); \
704 nanddev_io_iter_next_page(nand, iter))
706 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
707 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
708 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos);
709 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
711 /* BBT related functions */
712 enum nand_bbt_block_status {
713 NAND_BBT_BLOCK_STATUS_UNKNOWN,
716 NAND_BBT_BLOCK_RESERVED,
717 NAND_BBT_BLOCK_FACTORY_BAD,
718 NAND_BBT_BLOCK_NUM_STATUS,
721 int nanddev_bbt_init(struct nand_device *nand);
722 void nanddev_bbt_cleanup(struct nand_device *nand);
723 int nanddev_bbt_update(struct nand_device *nand);
724 int nanddev_bbt_get_block_status(const struct nand_device *nand,
726 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
727 enum nand_bbt_block_status status);
728 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
731 * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
733 * @pos: the NAND position we want to get BBT entry for
735 * Return the BBT entry used to store information about the eraseblock pointed
738 * Return: the BBT entry storing information about eraseblock pointed by @pos.
740 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
741 const struct nand_pos *pos)
743 return pos->eraseblock +
744 ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
745 nand->memorg.eraseblocks_per_lun);
749 * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
752 * Return: true if the BBT has been initialized, false otherwise.
754 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
756 return !!nand->bbt.cache;
759 /* MTD -> NAND helper functions. */
760 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
761 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
763 #endif /* __LINUX_MTD_NAND_H */