632becb13b46020fff630920bc1c5200dd0c7eb0
[linux-2.6-microblaze.git] / include / linux / mtd / nand.h
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 struct nand_device;
16
17 /**
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
28  */
29 struct nand_memory_organization {
30         unsigned int bits_per_cell;
31         unsigned int pagesize;
32         unsigned int oobsize;
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;
39 };
40
41 #define NAND_MEMORG(bpc, ps, os, ppe, epl, mbb, ppl, lpt, nt)   \
42         {                                                       \
43                 .bits_per_cell = (bpc),                         \
44                 .pagesize = (ps),                               \
45                 .oobsize = (os),                                \
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),                       \
51                 .ntargets = (nt),                               \
52         }
53
54 /**
55  * struct nand_row_converter - Information needed to convert an absolute offset
56  *                             into a row address
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
59  *                         address
60  */
61 struct nand_row_converter {
62         unsigned int lun_addr_shift;
63         unsigned int eraseblock_addr_shift;
64 };
65
66 /**
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
73  *
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.
76  */
77 struct nand_pos {
78         unsigned int target;
79         unsigned int lun;
80         unsigned int plane;
81         unsigned int eraseblock;
82         unsigned int page;
83 };
84
85 /**
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
89  */
90 enum nand_page_io_req_type {
91         NAND_PAGE_READ = 0,
92         NAND_PAGE_WRITE,
93 };
94
95 /**
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
106  *
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.
111  */
112 struct nand_page_io_req {
113         enum nand_page_io_req_type type;
114         struct nand_pos pos;
115         unsigned int dataoffs;
116         unsigned int datalen;
117         union {
118                 const void *out;
119                 void *in;
120         } databuf;
121         unsigned int ooboffs;
122         unsigned int ooblen;
123         union {
124                 const void *out;
125                 void *in;
126         } oobbuf;
127         int mode;
128 };
129
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);
133
134 /**
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
141  */
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,
148 };
149
150 /**
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
156  *                                  area
157  */
158 enum nand_ecc_placement {
159         NAND_ECC_PLACEMENT_UNKNOWN,
160         NAND_ECC_PLACEMENT_OOB,
161         NAND_ECC_PLACEMENT_INTERLEAVED,
162 };
163
164 /**
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
170  */
171 enum nand_ecc_algo {
172         NAND_ECC_ALGO_UNKNOWN,
173         NAND_ECC_ALGO_HAMMING,
174         NAND_ECC_ALGO_BCH,
175         NAND_ECC_ALGO_RS,
176 };
177
178 /**
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
186  */
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;
193         unsigned int flags;
194 };
195
196 #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
197
198 /* NAND ECC misc flags */
199 #define NAND_ECC_MAXIMIZE_STRENGTH BIT(0)
200
201 /**
202  * struct nand_bbt - bad block table object
203  * @cache: in memory BBT cache
204  */
205 struct nand_bbt {
206         unsigned long *cache;
207 };
208
209 /**
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
217  *           true
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
220  *         reads
221  *
222  * These are all low level operations that should be implemented by specialized
223  * NAND layers (SPI NAND, raw NAND, ...).
224  */
225 struct nand_ops {
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);
229 };
230
231 /**
232  * struct nand_ecc_context - Context for the ECC engine
233  * @conf: basic ECC engine parameters
234  * @nsteps: number of ECC steps
235  * @total: total number of bytes used for storing ECC codes, this is used by
236  *         generic OOB layouts
237  * @priv: ECC engine driver private data
238  */
239 struct nand_ecc_context {
240         struct nand_ecc_props conf;
241         unsigned int nsteps;
242         unsigned int total;
243         void *priv;
244 };
245
246 /**
247  * struct nand_ecc_engine_ops - ECC engine operations
248  * @init_ctx: given a desired user configuration for the pointed NAND device,
249  *            requests the ECC engine driver to setup a configuration with
250  *            values it supports.
251  * @cleanup_ctx: clean the context initialized by @init_ctx.
252  * @prepare_io_req: is called before reading/writing a page to prepare the I/O
253  *                  request to be performed with ECC correction.
254  * @finish_io_req: is called after reading/writing a page to terminate the I/O
255  *                 request and ensure proper ECC correction.
256  */
257 struct nand_ecc_engine_ops {
258         int (*init_ctx)(struct nand_device *nand);
259         void (*cleanup_ctx)(struct nand_device *nand);
260         int (*prepare_io_req)(struct nand_device *nand,
261                               struct nand_page_io_req *req);
262         int (*finish_io_req)(struct nand_device *nand,
263                              struct nand_page_io_req *req);
264 };
265
266 /**
267  * struct nand_ecc_engine - ECC engine abstraction for NAND devices
268  * @ops: ECC engine operations
269  */
270 struct nand_ecc_engine {
271         struct nand_ecc_engine_ops *ops;
272 };
273
274 void of_get_nand_ecc_user_config(struct nand_device *nand);
275 int nand_ecc_init_ctx(struct nand_device *nand);
276 void nand_ecc_cleanup_ctx(struct nand_device *nand);
277 int nand_ecc_prepare_io_req(struct nand_device *nand,
278                             struct nand_page_io_req *req);
279 int nand_ecc_finish_io_req(struct nand_device *nand,
280                            struct nand_page_io_req *req);
281 bool nand_ecc_is_strong_enough(struct nand_device *nand);
282 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand);
283 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand);
284
285 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
286 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);
287 #else
288 static inline struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void)
289 {
290         return NULL;
291 }
292 #endif /* CONFIG_MTD_NAND_ECC_SW_HAMMING */
293
294 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)
295 struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void);
296 #else
297 static inline struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void)
298 {
299         return NULL;
300 }
301 #endif /* CONFIG_MTD_NAND_ECC_SW_BCH */
302
303 /**
304  * struct nand_ecc_req_tweak_ctx - Help for automatically tweaking requests
305  * @orig_req: Pointer to the original IO request
306  * @nand: Related NAND device, to have access to its memory organization
307  * @page_buffer_size: Real size of the page buffer to use (can be set by the
308  *                    user before the tweaking mechanism initialization)
309  * @oob_buffer_size: Real size of the OOB buffer to use (can be set by the
310  *                   user before the tweaking mechanism initialization)
311  * @spare_databuf: Data bounce buffer
312  * @spare_oobbuf: OOB bounce buffer
313  * @bounce_data: Flag indicating a data bounce buffer is used
314  * @bounce_oob: Flag indicating an OOB bounce buffer is used
315  */
316 struct nand_ecc_req_tweak_ctx {
317         struct nand_page_io_req orig_req;
318         struct nand_device *nand;
319         unsigned int page_buffer_size;
320         unsigned int oob_buffer_size;
321         void *spare_databuf;
322         void *spare_oobbuf;
323         bool bounce_data;
324         bool bounce_oob;
325 };
326
327 int nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx,
328                                struct nand_device *nand);
329 void nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx);
330 void nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx *ctx,
331                         struct nand_page_io_req *req);
332 void nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx *ctx,
333                           struct nand_page_io_req *req);
334
335 /**
336  * struct nand_ecc - Information relative to the ECC
337  * @defaults: Default values, depend on the underlying subsystem
338  * @requirements: ECC requirements from the NAND chip perspective
339  * @user_conf: User desires in terms of ECC parameters
340  * @ctx: ECC context for the ECC engine, derived from the device @requirements
341  *       the @user_conf and the @defaults
342  * @ondie_engine: On-die ECC engine reference, if any
343  * @engine: ECC engine actually bound
344  */
345 struct nand_ecc {
346         struct nand_ecc_props defaults;
347         struct nand_ecc_props requirements;
348         struct nand_ecc_props user_conf;
349         struct nand_ecc_context ctx;
350         struct nand_ecc_engine *ondie_engine;
351         struct nand_ecc_engine *engine;
352 };
353
354 /**
355  * struct nand_device - NAND device
356  * @mtd: MTD instance attached to the NAND device
357  * @memorg: memory layout
358  * @ecc: NAND ECC object attached to the NAND device
359  * @rowconv: position to row address converter
360  * @bbt: bad block table info
361  * @ops: NAND operations attached to the NAND device
362  *
363  * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND)
364  * should declare their own NAND object embedding a nand_device struct (that's
365  * how inheritance is done).
366  * struct_nand_device->memorg and struct_nand_device->ecc.requirements should
367  * be filled at device detection time to reflect the NAND device
368  * capabilities/requirements. Once this is done nanddev_init() can be called.
369  * It will take care of converting NAND information into MTD ones, which means
370  * the specialized NAND layers should never manually tweak
371  * struct_nand_device->mtd except for the ->_read/write() hooks.
372  */
373 struct nand_device {
374         struct mtd_info mtd;
375         struct nand_memory_organization memorg;
376         struct nand_ecc ecc;
377         struct nand_row_converter rowconv;
378         struct nand_bbt bbt;
379         const struct nand_ops *ops;
380 };
381
382 /**
383  * struct nand_io_iter - NAND I/O iterator
384  * @req: current I/O request
385  * @oobbytes_per_page: maximum number of OOB bytes per page
386  * @dataleft: remaining number of data bytes to read/write
387  * @oobleft: remaining number of OOB bytes to read/write
388  *
389  * Can be used by specialized NAND layers to iterate over all pages covered
390  * by an MTD I/O request, which should greatly simplifies the boiler-plate
391  * code needed to read/write data from/to a NAND device.
392  */
393 struct nand_io_iter {
394         struct nand_page_io_req req;
395         unsigned int oobbytes_per_page;
396         unsigned int dataleft;
397         unsigned int oobleft;
398 };
399
400 /**
401  * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
402  * @mtd: MTD instance
403  *
404  * Return: the NAND device embedding @mtd.
405  */
406 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
407 {
408         return container_of(mtd, struct nand_device, mtd);
409 }
410
411 /**
412  * nanddev_to_mtd() - Get the MTD device attached to a NAND device
413  * @nand: NAND device
414  *
415  * Return: the MTD device embedded in @nand.
416  */
417 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
418 {
419         return &nand->mtd;
420 }
421
422 /*
423  * nanddev_bits_per_cell() - Get the number of bits per cell
424  * @nand: NAND device
425  *
426  * Return: the number of bits per cell.
427  */
428 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
429 {
430         return nand->memorg.bits_per_cell;
431 }
432
433 /**
434  * nanddev_page_size() - Get NAND page size
435  * @nand: NAND device
436  *
437  * Return: the page size.
438  */
439 static inline size_t nanddev_page_size(const struct nand_device *nand)
440 {
441         return nand->memorg.pagesize;
442 }
443
444 /**
445  * nanddev_per_page_oobsize() - Get NAND OOB size
446  * @nand: NAND device
447  *
448  * Return: the OOB size.
449  */
450 static inline unsigned int
451 nanddev_per_page_oobsize(const struct nand_device *nand)
452 {
453         return nand->memorg.oobsize;
454 }
455
456 /**
457  * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
458  * @nand: NAND device
459  *
460  * Return: the number of pages per eraseblock.
461  */
462 static inline unsigned int
463 nanddev_pages_per_eraseblock(const struct nand_device *nand)
464 {
465         return nand->memorg.pages_per_eraseblock;
466 }
467
468 /**
469  * nanddev_pages_per_target() - Get the number of pages per target
470  * @nand: NAND device
471  *
472  * Return: the number of pages per target.
473  */
474 static inline unsigned int
475 nanddev_pages_per_target(const struct nand_device *nand)
476 {
477         return nand->memorg.pages_per_eraseblock *
478                nand->memorg.eraseblocks_per_lun *
479                nand->memorg.luns_per_target;
480 }
481
482 /**
483  * nanddev_per_page_oobsize() - Get NAND erase block size
484  * @nand: NAND device
485  *
486  * Return: the eraseblock size.
487  */
488 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
489 {
490         return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
491 }
492
493 /**
494  * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
495  * @nand: NAND device
496  *
497  * Return: the number of eraseblocks per LUN.
498  */
499 static inline unsigned int
500 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
501 {
502         return nand->memorg.eraseblocks_per_lun;
503 }
504
505 /**
506  * nanddev_eraseblocks_per_target() - Get the number of eraseblocks per target
507  * @nand: NAND device
508  *
509  * Return: the number of eraseblocks per target.
510  */
511 static inline unsigned int
512 nanddev_eraseblocks_per_target(const struct nand_device *nand)
513 {
514         return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
515 }
516
517 /**
518  * nanddev_target_size() - Get the total size provided by a single target/die
519  * @nand: NAND device
520  *
521  * Return: the total size exposed by a single target/die in bytes.
522  */
523 static inline u64 nanddev_target_size(const struct nand_device *nand)
524 {
525         return (u64)nand->memorg.luns_per_target *
526                nand->memorg.eraseblocks_per_lun *
527                nand->memorg.pages_per_eraseblock *
528                nand->memorg.pagesize;
529 }
530
531 /**
532  * nanddev_ntarget() - Get the total of targets
533  * @nand: NAND device
534  *
535  * Return: the number of targets/dies exposed by @nand.
536  */
537 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
538 {
539         return nand->memorg.ntargets;
540 }
541
542 /**
543  * nanddev_neraseblocks() - Get the total number of eraseblocks
544  * @nand: NAND device
545  *
546  * Return: the total number of eraseblocks exposed by @nand.
547  */
548 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
549 {
550         return nand->memorg.ntargets * nand->memorg.luns_per_target *
551                nand->memorg.eraseblocks_per_lun;
552 }
553
554 /**
555  * nanddev_size() - Get NAND size
556  * @nand: NAND device
557  *
558  * Return: the total size (in bytes) exposed by @nand.
559  */
560 static inline u64 nanddev_size(const struct nand_device *nand)
561 {
562         return nanddev_target_size(nand) * nanddev_ntargets(nand);
563 }
564
565 /**
566  * nanddev_get_memorg() - Extract memory organization info from a NAND device
567  * @nand: NAND device
568  *
569  * This can be used by the upper layer to fill the memorg info before calling
570  * nanddev_init().
571  *
572  * Return: the memorg object embedded in the NAND device.
573  */
574 static inline struct nand_memory_organization *
575 nanddev_get_memorg(struct nand_device *nand)
576 {
577         return &nand->memorg;
578 }
579
580 /**
581  * nanddev_get_ecc_conf() - Extract the ECC configuration from a NAND device
582  * @nand: NAND device
583  */
584 static inline const struct nand_ecc_props *
585 nanddev_get_ecc_conf(struct nand_device *nand)
586 {
587         return &nand->ecc.ctx.conf;
588 }
589
590 /**
591  * nanddev_get_ecc_requirements() - Extract the ECC requirements from a NAND
592  *                                  device
593  * @nand: NAND device
594  */
595 static inline const struct nand_ecc_props *
596 nanddev_get_ecc_requirements(struct nand_device *nand)
597 {
598         return &nand->ecc.requirements;
599 }
600
601 /**
602  * nanddev_set_ecc_requirements() - Assign the ECC requirements of a NAND
603  *                                  device
604  * @nand: NAND device
605  * @reqs: Requirements
606  */
607 static inline void
608 nanddev_set_ecc_requirements(struct nand_device *nand,
609                              const struct nand_ecc_props *reqs)
610 {
611         nand->ecc.requirements = *reqs;
612 }
613
614 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
615                  struct module *owner);
616 void nanddev_cleanup(struct nand_device *nand);
617
618 /**
619  * nanddev_register() - Register a NAND device
620  * @nand: NAND device
621  *
622  * Register a NAND device.
623  * This function is just a wrapper around mtd_device_register()
624  * registering the MTD device embedded in @nand.
625  *
626  * Return: 0 in case of success, a negative error code otherwise.
627  */
628 static inline int nanddev_register(struct nand_device *nand)
629 {
630         return mtd_device_register(&nand->mtd, NULL, 0);
631 }
632
633 /**
634  * nanddev_unregister() - Unregister a NAND device
635  * @nand: NAND device
636  *
637  * Unregister a NAND device.
638  * This function is just a wrapper around mtd_device_unregister()
639  * unregistering the MTD device embedded in @nand.
640  *
641  * Return: 0 in case of success, a negative error code otherwise.
642  */
643 static inline int nanddev_unregister(struct nand_device *nand)
644 {
645         return mtd_device_unregister(&nand->mtd);
646 }
647
648 /**
649  * nanddev_set_of_node() - Attach a DT node to a NAND device
650  * @nand: NAND device
651  * @np: DT node
652  *
653  * Attach a DT node to a NAND device.
654  */
655 static inline void nanddev_set_of_node(struct nand_device *nand,
656                                        struct device_node *np)
657 {
658         mtd_set_of_node(&nand->mtd, np);
659 }
660
661 /**
662  * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
663  * @nand: NAND device
664  *
665  * Return: the DT node attached to @nand.
666  */
667 static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
668 {
669         return mtd_get_of_node(&nand->mtd);
670 }
671
672 /**
673  * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
674  * @nand: NAND device
675  * @offs: absolute NAND offset (usually passed by the MTD layer)
676  * @pos: a NAND position object to fill in
677  *
678  * Converts @offs into a nand_pos representation.
679  *
680  * Return: the offset within the NAND page pointed by @pos.
681  */
682 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
683                                                loff_t offs,
684                                                struct nand_pos *pos)
685 {
686         unsigned int pageoffs;
687         u64 tmp = offs;
688
689         pageoffs = do_div(tmp, nand->memorg.pagesize);
690         pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
691         pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
692         pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
693         pos->lun = do_div(tmp, nand->memorg.luns_per_target);
694         pos->target = tmp;
695
696         return pageoffs;
697 }
698
699 /**
700  * nanddev_pos_cmp() - Compare two NAND positions
701  * @a: First NAND position
702  * @b: Second NAND position
703  *
704  * Compares two NAND positions.
705  *
706  * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
707  */
708 static inline int nanddev_pos_cmp(const struct nand_pos *a,
709                                   const struct nand_pos *b)
710 {
711         if (a->target != b->target)
712                 return a->target < b->target ? -1 : 1;
713
714         if (a->lun != b->lun)
715                 return a->lun < b->lun ? -1 : 1;
716
717         if (a->eraseblock != b->eraseblock)
718                 return a->eraseblock < b->eraseblock ? -1 : 1;
719
720         if (a->page != b->page)
721                 return a->page < b->page ? -1 : 1;
722
723         return 0;
724 }
725
726 /**
727  * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
728  * @nand: NAND device
729  * @pos: the NAND position to convert
730  *
731  * Converts @pos NAND position into an absolute offset.
732  *
733  * Return: the absolute offset. Note that @pos points to the beginning of a
734  *         page, if one wants to point to a specific offset within this page
735  *         the returned offset has to be adjusted manually.
736  */
737 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
738                                          const struct nand_pos *pos)
739 {
740         unsigned int npages;
741
742         npages = pos->page +
743                  ((pos->eraseblock +
744                    (pos->lun +
745                     (pos->target * nand->memorg.luns_per_target)) *
746                    nand->memorg.eraseblocks_per_lun) *
747                   nand->memorg.pages_per_eraseblock);
748
749         return (loff_t)npages * nand->memorg.pagesize;
750 }
751
752 /**
753  * nanddev_pos_to_row() - Extract a row address from a NAND position
754  * @nand: NAND device
755  * @pos: the position to convert
756  *
757  * Converts a NAND position into a row address that can then be passed to the
758  * device.
759  *
760  * Return: the row address extracted from @pos.
761  */
762 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
763                                               const struct nand_pos *pos)
764 {
765         return (pos->lun << nand->rowconv.lun_addr_shift) |
766                (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
767                pos->page;
768 }
769
770 /**
771  * nanddev_pos_next_target() - Move a position to the next target/die
772  * @nand: NAND device
773  * @pos: the position to update
774  *
775  * Updates @pos to point to the start of the next target/die. Useful when you
776  * want to iterate over all targets/dies of a NAND device.
777  */
778 static inline void nanddev_pos_next_target(struct nand_device *nand,
779                                            struct nand_pos *pos)
780 {
781         pos->page = 0;
782         pos->plane = 0;
783         pos->eraseblock = 0;
784         pos->lun = 0;
785         pos->target++;
786 }
787
788 /**
789  * nanddev_pos_next_lun() - Move a position to the next LUN
790  * @nand: NAND device
791  * @pos: the position to update
792  *
793  * Updates @pos to point to the start of the next LUN. Useful when you want to
794  * iterate over all LUNs of a NAND device.
795  */
796 static inline void nanddev_pos_next_lun(struct nand_device *nand,
797                                         struct nand_pos *pos)
798 {
799         if (pos->lun >= nand->memorg.luns_per_target - 1)
800                 return nanddev_pos_next_target(nand, pos);
801
802         pos->lun++;
803         pos->page = 0;
804         pos->plane = 0;
805         pos->eraseblock = 0;
806 }
807
808 /**
809  * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
810  * @nand: NAND device
811  * @pos: the position to update
812  *
813  * Updates @pos to point to the start of the next eraseblock. Useful when you
814  * want to iterate over all eraseblocks of a NAND device.
815  */
816 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
817                                                struct nand_pos *pos)
818 {
819         if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
820                 return nanddev_pos_next_lun(nand, pos);
821
822         pos->eraseblock++;
823         pos->page = 0;
824         pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
825 }
826
827 /**
828  * nanddev_pos_next_page() - Move a position to the next page
829  * @nand: NAND device
830  * @pos: the position to update
831  *
832  * Updates @pos to point to the start of the next page. Useful when you want to
833  * iterate over all pages of a NAND device.
834  */
835 static inline void nanddev_pos_next_page(struct nand_device *nand,
836                                          struct nand_pos *pos)
837 {
838         if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
839                 return nanddev_pos_next_eraseblock(nand, pos);
840
841         pos->page++;
842 }
843
844 /**
845  * nand_io_iter_init - Initialize a NAND I/O iterator
846  * @nand: NAND device
847  * @offs: absolute offset
848  * @req: MTD request
849  * @iter: NAND I/O iterator
850  *
851  * Initializes a NAND iterator based on the information passed by the MTD
852  * layer.
853  */
854 static inline void nanddev_io_iter_init(struct nand_device *nand,
855                                         enum nand_page_io_req_type reqtype,
856                                         loff_t offs, struct mtd_oob_ops *req,
857                                         struct nand_io_iter *iter)
858 {
859         struct mtd_info *mtd = nanddev_to_mtd(nand);
860
861         iter->req.type = reqtype;
862         iter->req.mode = req->mode;
863         iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
864         iter->req.ooboffs = req->ooboffs;
865         iter->oobbytes_per_page = mtd_oobavail(mtd, req);
866         iter->dataleft = req->len;
867         iter->oobleft = req->ooblen;
868         iter->req.databuf.in = req->datbuf;
869         iter->req.datalen = min_t(unsigned int,
870                                   nand->memorg.pagesize - iter->req.dataoffs,
871                                   iter->dataleft);
872         iter->req.oobbuf.in = req->oobbuf;
873         iter->req.ooblen = min_t(unsigned int,
874                                  iter->oobbytes_per_page - iter->req.ooboffs,
875                                  iter->oobleft);
876 }
877
878 /**
879  * nand_io_iter_next_page - Move to the next page
880  * @nand: NAND device
881  * @iter: NAND I/O iterator
882  *
883  * Updates the @iter to point to the next page.
884  */
885 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
886                                              struct nand_io_iter *iter)
887 {
888         nanddev_pos_next_page(nand, &iter->req.pos);
889         iter->dataleft -= iter->req.datalen;
890         iter->req.databuf.in += iter->req.datalen;
891         iter->oobleft -= iter->req.ooblen;
892         iter->req.oobbuf.in += iter->req.ooblen;
893         iter->req.dataoffs = 0;
894         iter->req.ooboffs = 0;
895         iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
896                                   iter->dataleft);
897         iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
898                                  iter->oobleft);
899 }
900
901 /**
902  * nand_io_iter_end - Should end iteration or not
903  * @nand: NAND device
904  * @iter: NAND I/O iterator
905  *
906  * Check whether @iter has reached the end of the NAND portion it was asked to
907  * iterate on or not.
908  *
909  * Return: true if @iter has reached the end of the iteration request, false
910  *         otherwise.
911  */
912 static inline bool nanddev_io_iter_end(struct nand_device *nand,
913                                        const struct nand_io_iter *iter)
914 {
915         if (iter->dataleft || iter->oobleft)
916                 return false;
917
918         return true;
919 }
920
921 /**
922  * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
923  *                         request
924  * @nand: NAND device
925  * @start: start address to read/write from
926  * @req: MTD I/O request
927  * @iter: NAND I/O iterator
928  *
929  * Should be used for iterate over pages that are contained in an MTD request.
930  */
931 #define nanddev_io_for_each_page(nand, type, start, req, iter)          \
932         for (nanddev_io_iter_init(nand, type, start, req, iter);        \
933              !nanddev_io_iter_end(nand, iter);                          \
934              nanddev_io_iter_next_page(nand, iter))
935
936 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
937 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
938 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos);
939 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
940
941 /* ECC related functions */
942 int nanddev_ecc_engine_init(struct nand_device *nand);
943 void nanddev_ecc_engine_cleanup(struct nand_device *nand);
944
945 /* BBT related functions */
946 enum nand_bbt_block_status {
947         NAND_BBT_BLOCK_STATUS_UNKNOWN,
948         NAND_BBT_BLOCK_GOOD,
949         NAND_BBT_BLOCK_WORN,
950         NAND_BBT_BLOCK_RESERVED,
951         NAND_BBT_BLOCK_FACTORY_BAD,
952         NAND_BBT_BLOCK_NUM_STATUS,
953 };
954
955 int nanddev_bbt_init(struct nand_device *nand);
956 void nanddev_bbt_cleanup(struct nand_device *nand);
957 int nanddev_bbt_update(struct nand_device *nand);
958 int nanddev_bbt_get_block_status(const struct nand_device *nand,
959                                  unsigned int entry);
960 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
961                                  enum nand_bbt_block_status status);
962 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
963
964 /**
965  * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
966  * @nand: NAND device
967  * @pos: the NAND position we want to get BBT entry for
968  *
969  * Return the BBT entry used to store information about the eraseblock pointed
970  * by @pos.
971  *
972  * Return: the BBT entry storing information about eraseblock pointed by @pos.
973  */
974 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
975                                                     const struct nand_pos *pos)
976 {
977         return pos->eraseblock +
978                ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
979                 nand->memorg.eraseblocks_per_lun);
980 }
981
982 /**
983  * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
984  * @nand: NAND device
985  *
986  * Return: true if the BBT has been initialized, false otherwise.
987  */
988 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
989 {
990         return !!nand->bbt.cache;
991 }
992
993 /* MTD -> NAND helper functions. */
994 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
995 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
996
997 #endif /* __LINUX_MTD_NAND_H */