• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @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
237  */
238 struct nand_ecc_context {
239 	struct nand_ecc_props conf;
240 	unsigned int total;
241 	void *priv;
242 };
243 
244 /**
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.
254  */
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);
262 };
263 
264 /**
265  * struct nand_ecc_engine - ECC engine abstraction for NAND devices
266  * @ops: ECC engine operations
267  */
268 struct nand_ecc_engine {
269 	struct nand_ecc_engine_ops *ops;
270 };
271 
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 
281 /**
282  * struct nand_ecc - Information relative to the ECC
283  * @defaults: Default values, depend on the underlying subsystem
284  * @requirements: ECC requirements from the NAND chip perspective
285  * @user_conf: User desires in terms of ECC parameters
286  * @ctx: ECC context for the ECC engine, derived from the device @requirements
287  *       the @user_conf and the @defaults
288  * @ondie_engine: On-die ECC engine reference, if any
289  * @engine: ECC engine actually bound
290  */
291 struct nand_ecc {
292 	struct nand_ecc_props defaults;
293 	struct nand_ecc_props requirements;
294 	struct nand_ecc_props user_conf;
295 	struct nand_ecc_context ctx;
296 	struct nand_ecc_engine *ondie_engine;
297 	struct nand_ecc_engine *engine;
298 };
299 
300 /**
301  * struct nand_device - NAND device
302  * @mtd: MTD instance attached to the NAND device
303  * @memorg: memory layout
304  * @ecc: NAND ECC object attached to the NAND device
305  * @rowconv: position to row address converter
306  * @bbt: bad block table info
307  * @ops: NAND operations attached to the NAND device
308  *
309  * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND)
310  * should declare their own NAND object embedding a nand_device struct (that's
311  * how inheritance is done).
312  * struct_nand_device->memorg and struct_nand_device->ecc.requirements should
313  * be filled at device detection time to reflect the NAND device
314  * capabilities/requirements. Once this is done nanddev_init() can be called.
315  * It will take care of converting NAND information into MTD ones, which means
316  * the specialized NAND layers should never manually tweak
317  * struct_nand_device->mtd except for the ->_read/write() hooks.
318  */
319 struct nand_device {
320 	struct mtd_info mtd;
321 	struct nand_memory_organization memorg;
322 	struct nand_ecc ecc;
323 	struct nand_row_converter rowconv;
324 	struct nand_bbt bbt;
325 	const struct nand_ops *ops;
326 };
327 
328 /**
329  * struct nand_io_iter - NAND I/O iterator
330  * @req: current I/O request
331  * @oobbytes_per_page: maximum number of OOB bytes per page
332  * @dataleft: remaining number of data bytes to read/write
333  * @oobleft: remaining number of OOB bytes to read/write
334  *
335  * Can be used by specialized NAND layers to iterate over all pages covered
336  * by an MTD I/O request, which should greatly simplifies the boiler-plate
337  * code needed to read/write data from/to a NAND device.
338  */
339 struct nand_io_iter {
340 	struct nand_page_io_req req;
341 	unsigned int oobbytes_per_page;
342 	unsigned int dataleft;
343 	unsigned int oobleft;
344 };
345 
346 /**
347  * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
348  * @mtd: MTD instance
349  *
350  * Return: the NAND device embedding @mtd.
351  */
mtd_to_nanddev(struct mtd_info * mtd)352 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
353 {
354 	return container_of(mtd, struct nand_device, mtd);
355 }
356 
357 /**
358  * nanddev_to_mtd() - Get the MTD device attached to a NAND device
359  * @nand: NAND device
360  *
361  * Return: the MTD device embedded in @nand.
362  */
nanddev_to_mtd(struct nand_device * nand)363 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
364 {
365 	return &nand->mtd;
366 }
367 
368 /*
369  * nanddev_bits_per_cell() - Get the number of bits per cell
370  * @nand: NAND device
371  *
372  * Return: the number of bits per cell.
373  */
nanddev_bits_per_cell(const struct nand_device * nand)374 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
375 {
376 	return nand->memorg.bits_per_cell;
377 }
378 
379 /**
380  * nanddev_page_size() - Get NAND page size
381  * @nand: NAND device
382  *
383  * Return: the page size.
384  */
nanddev_page_size(const struct nand_device * nand)385 static inline size_t nanddev_page_size(const struct nand_device *nand)
386 {
387 	return nand->memorg.pagesize;
388 }
389 
390 /**
391  * nanddev_per_page_oobsize() - Get NAND OOB size
392  * @nand: NAND device
393  *
394  * Return: the OOB size.
395  */
396 static inline unsigned int
nanddev_per_page_oobsize(const struct nand_device * nand)397 nanddev_per_page_oobsize(const struct nand_device *nand)
398 {
399 	return nand->memorg.oobsize;
400 }
401 
402 /**
403  * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
404  * @nand: NAND device
405  *
406  * Return: the number of pages per eraseblock.
407  */
408 static inline unsigned int
nanddev_pages_per_eraseblock(const struct nand_device * nand)409 nanddev_pages_per_eraseblock(const struct nand_device *nand)
410 {
411 	return nand->memorg.pages_per_eraseblock;
412 }
413 
414 /**
415  * nanddev_pages_per_target() - Get the number of pages per target
416  * @nand: NAND device
417  *
418  * Return: the number of pages per target.
419  */
420 static inline unsigned int
nanddev_pages_per_target(const struct nand_device * nand)421 nanddev_pages_per_target(const struct nand_device *nand)
422 {
423 	return nand->memorg.pages_per_eraseblock *
424 	       nand->memorg.eraseblocks_per_lun *
425 	       nand->memorg.luns_per_target;
426 }
427 
428 /**
429  * nanddev_per_page_oobsize() - Get NAND erase block size
430  * @nand: NAND device
431  *
432  * Return: the eraseblock size.
433  */
nanddev_eraseblock_size(const struct nand_device * nand)434 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
435 {
436 	return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
437 }
438 
439 /**
440  * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
441  * @nand: NAND device
442  *
443  * Return: the number of eraseblocks per LUN.
444  */
445 static inline unsigned int
nanddev_eraseblocks_per_lun(const struct nand_device * nand)446 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
447 {
448 	return nand->memorg.eraseblocks_per_lun;
449 }
450 
451 /**
452  * nanddev_eraseblocks_per_target() - Get the number of eraseblocks per target
453  * @nand: NAND device
454  *
455  * Return: the number of eraseblocks per target.
456  */
457 static inline unsigned int
nanddev_eraseblocks_per_target(const struct nand_device * nand)458 nanddev_eraseblocks_per_target(const struct nand_device *nand)
459 {
460 	return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
461 }
462 
463 /**
464  * nanddev_target_size() - Get the total size provided by a single target/die
465  * @nand: NAND device
466  *
467  * Return: the total size exposed by a single target/die in bytes.
468  */
nanddev_target_size(const struct nand_device * nand)469 static inline u64 nanddev_target_size(const struct nand_device *nand)
470 {
471 	return (u64)nand->memorg.luns_per_target *
472 	       nand->memorg.eraseblocks_per_lun *
473 	       nand->memorg.pages_per_eraseblock *
474 	       nand->memorg.pagesize;
475 }
476 
477 /**
478  * nanddev_ntarget() - Get the total of targets
479  * @nand: NAND device
480  *
481  * Return: the number of targets/dies exposed by @nand.
482  */
nanddev_ntargets(const struct nand_device * nand)483 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
484 {
485 	return nand->memorg.ntargets;
486 }
487 
488 /**
489  * nanddev_neraseblocks() - Get the total number of eraseblocks
490  * @nand: NAND device
491  *
492  * Return: the total number of eraseblocks exposed by @nand.
493  */
nanddev_neraseblocks(const struct nand_device * nand)494 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
495 {
496 	return nand->memorg.ntargets * nand->memorg.luns_per_target *
497 	       nand->memorg.eraseblocks_per_lun;
498 }
499 
500 /**
501  * nanddev_size() - Get NAND size
502  * @nand: NAND device
503  *
504  * Return: the total size (in bytes) exposed by @nand.
505  */
nanddev_size(const struct nand_device * nand)506 static inline u64 nanddev_size(const struct nand_device *nand)
507 {
508 	return nanddev_target_size(nand) * nanddev_ntargets(nand);
509 }
510 
511 /**
512  * nanddev_get_memorg() - Extract memory organization info from a NAND device
513  * @nand: NAND device
514  *
515  * This can be used by the upper layer to fill the memorg info before calling
516  * nanddev_init().
517  *
518  * Return: the memorg object embedded in the NAND device.
519  */
520 static inline struct nand_memory_organization *
nanddev_get_memorg(struct nand_device * nand)521 nanddev_get_memorg(struct nand_device *nand)
522 {
523 	return &nand->memorg;
524 }
525 
526 /**
527  * nanddev_get_ecc_conf() - Extract the ECC configuration from a NAND device
528  * @nand: NAND device
529  */
530 static inline const struct nand_ecc_props *
nanddev_get_ecc_conf(struct nand_device * nand)531 nanddev_get_ecc_conf(struct nand_device *nand)
532 {
533 	return &nand->ecc.ctx.conf;
534 }
535 
536 /**
537  * nanddev_get_ecc_requirements() - Extract the ECC requirements from a NAND
538  *                                  device
539  * @nand: NAND device
540  */
541 static inline const struct nand_ecc_props *
nanddev_get_ecc_requirements(struct nand_device * nand)542 nanddev_get_ecc_requirements(struct nand_device *nand)
543 {
544 	return &nand->ecc.requirements;
545 }
546 
547 /**
548  * nanddev_set_ecc_requirements() - Assign the ECC requirements of a NAND
549  *                                  device
550  * @nand: NAND device
551  * @reqs: Requirements
552  */
553 static inline void
nanddev_set_ecc_requirements(struct nand_device * nand,const struct nand_ecc_props * reqs)554 nanddev_set_ecc_requirements(struct nand_device *nand,
555 			     const struct nand_ecc_props *reqs)
556 {
557 	nand->ecc.requirements = *reqs;
558 }
559 
560 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
561 		 struct module *owner);
562 void nanddev_cleanup(struct nand_device *nand);
563 
564 /**
565  * nanddev_register() - Register a NAND device
566  * @nand: NAND device
567  *
568  * Register a NAND device.
569  * This function is just a wrapper around mtd_device_register()
570  * registering the MTD device embedded in @nand.
571  *
572  * Return: 0 in case of success, a negative error code otherwise.
573  */
nanddev_register(struct nand_device * nand)574 static inline int nanddev_register(struct nand_device *nand)
575 {
576 	return mtd_device_register(&nand->mtd, NULL, 0);
577 }
578 
579 /**
580  * nanddev_unregister() - Unregister a NAND device
581  * @nand: NAND device
582  *
583  * Unregister a NAND device.
584  * This function is just a wrapper around mtd_device_unregister()
585  * unregistering the MTD device embedded in @nand.
586  *
587  * Return: 0 in case of success, a negative error code otherwise.
588  */
nanddev_unregister(struct nand_device * nand)589 static inline int nanddev_unregister(struct nand_device *nand)
590 {
591 	return mtd_device_unregister(&nand->mtd);
592 }
593 
594 /**
595  * nanddev_set_of_node() - Attach a DT node to a NAND device
596  * @nand: NAND device
597  * @np: DT node
598  *
599  * Attach a DT node to a NAND device.
600  */
nanddev_set_of_node(struct nand_device * nand,struct device_node * np)601 static inline void nanddev_set_of_node(struct nand_device *nand,
602 				       struct device_node *np)
603 {
604 	mtd_set_of_node(&nand->mtd, np);
605 }
606 
607 /**
608  * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
609  * @nand: NAND device
610  *
611  * Return: the DT node attached to @nand.
612  */
nanddev_get_of_node(struct nand_device * nand)613 static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
614 {
615 	return mtd_get_of_node(&nand->mtd);
616 }
617 
618 /**
619  * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
620  * @nand: NAND device
621  * @offs: absolute NAND offset (usually passed by the MTD layer)
622  * @pos: a NAND position object to fill in
623  *
624  * Converts @offs into a nand_pos representation.
625  *
626  * Return: the offset within the NAND page pointed by @pos.
627  */
nanddev_offs_to_pos(struct nand_device * nand,loff_t offs,struct nand_pos * pos)628 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
629 					       loff_t offs,
630 					       struct nand_pos *pos)
631 {
632 	unsigned int pageoffs;
633 	u64 tmp = offs;
634 
635 	pageoffs = do_div(tmp, nand->memorg.pagesize);
636 	pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
637 	pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
638 	pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
639 	pos->lun = do_div(tmp, nand->memorg.luns_per_target);
640 	pos->target = tmp;
641 
642 	return pageoffs;
643 }
644 
645 /**
646  * nanddev_pos_cmp() - Compare two NAND positions
647  * @a: First NAND position
648  * @b: Second NAND position
649  *
650  * Compares two NAND positions.
651  *
652  * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
653  */
nanddev_pos_cmp(const struct nand_pos * a,const struct nand_pos * b)654 static inline int nanddev_pos_cmp(const struct nand_pos *a,
655 				  const struct nand_pos *b)
656 {
657 	if (a->target != b->target)
658 		return a->target < b->target ? -1 : 1;
659 
660 	if (a->lun != b->lun)
661 		return a->lun < b->lun ? -1 : 1;
662 
663 	if (a->eraseblock != b->eraseblock)
664 		return a->eraseblock < b->eraseblock ? -1 : 1;
665 
666 	if (a->page != b->page)
667 		return a->page < b->page ? -1 : 1;
668 
669 	return 0;
670 }
671 
672 /**
673  * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
674  * @nand: NAND device
675  * @pos: the NAND position to convert
676  *
677  * Converts @pos NAND position into an absolute offset.
678  *
679  * Return: the absolute offset. Note that @pos points to the beginning of a
680  *	   page, if one wants to point to a specific offset within this page
681  *	   the returned offset has to be adjusted manually.
682  */
nanddev_pos_to_offs(struct nand_device * nand,const struct nand_pos * pos)683 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
684 					 const struct nand_pos *pos)
685 {
686 	unsigned int npages;
687 
688 	npages = pos->page +
689 		 ((pos->eraseblock +
690 		   (pos->lun +
691 		    (pos->target * nand->memorg.luns_per_target)) *
692 		   nand->memorg.eraseblocks_per_lun) *
693 		  nand->memorg.pages_per_eraseblock);
694 
695 	return (loff_t)npages * nand->memorg.pagesize;
696 }
697 
698 /**
699  * nanddev_pos_to_row() - Extract a row address from a NAND position
700  * @nand: NAND device
701  * @pos: the position to convert
702  *
703  * Converts a NAND position into a row address that can then be passed to the
704  * device.
705  *
706  * Return: the row address extracted from @pos.
707  */
nanddev_pos_to_row(struct nand_device * nand,const struct nand_pos * pos)708 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
709 					      const struct nand_pos *pos)
710 {
711 	return (pos->lun << nand->rowconv.lun_addr_shift) |
712 	       (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
713 	       pos->page;
714 }
715 
716 /**
717  * nanddev_pos_next_target() - Move a position to the next target/die
718  * @nand: NAND device
719  * @pos: the position to update
720  *
721  * Updates @pos to point to the start of the next target/die. Useful when you
722  * want to iterate over all targets/dies of a NAND device.
723  */
nanddev_pos_next_target(struct nand_device * nand,struct nand_pos * pos)724 static inline void nanddev_pos_next_target(struct nand_device *nand,
725 					   struct nand_pos *pos)
726 {
727 	pos->page = 0;
728 	pos->plane = 0;
729 	pos->eraseblock = 0;
730 	pos->lun = 0;
731 	pos->target++;
732 }
733 
734 /**
735  * nanddev_pos_next_lun() - Move a position to the next LUN
736  * @nand: NAND device
737  * @pos: the position to update
738  *
739  * Updates @pos to point to the start of the next LUN. Useful when you want to
740  * iterate over all LUNs of a NAND device.
741  */
nanddev_pos_next_lun(struct nand_device * nand,struct nand_pos * pos)742 static inline void nanddev_pos_next_lun(struct nand_device *nand,
743 					struct nand_pos *pos)
744 {
745 	if (pos->lun >= nand->memorg.luns_per_target - 1)
746 		return nanddev_pos_next_target(nand, pos);
747 
748 	pos->lun++;
749 	pos->page = 0;
750 	pos->plane = 0;
751 	pos->eraseblock = 0;
752 }
753 
754 /**
755  * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
756  * @nand: NAND device
757  * @pos: the position to update
758  *
759  * Updates @pos to point to the start of the next eraseblock. Useful when you
760  * want to iterate over all eraseblocks of a NAND device.
761  */
nanddev_pos_next_eraseblock(struct nand_device * nand,struct nand_pos * pos)762 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
763 					       struct nand_pos *pos)
764 {
765 	if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
766 		return nanddev_pos_next_lun(nand, pos);
767 
768 	pos->eraseblock++;
769 	pos->page = 0;
770 	pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
771 }
772 
773 /**
774  * nanddev_pos_next_page() - Move a position to the next page
775  * @nand: NAND device
776  * @pos: the position to update
777  *
778  * Updates @pos to point to the start of the next page. Useful when you want to
779  * iterate over all pages of a NAND device.
780  */
nanddev_pos_next_page(struct nand_device * nand,struct nand_pos * pos)781 static inline void nanddev_pos_next_page(struct nand_device *nand,
782 					 struct nand_pos *pos)
783 {
784 	if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
785 		return nanddev_pos_next_eraseblock(nand, pos);
786 
787 	pos->page++;
788 }
789 
790 /**
791  * nand_io_iter_init - Initialize a NAND I/O iterator
792  * @nand: NAND device
793  * @offs: absolute offset
794  * @req: MTD request
795  * @iter: NAND I/O iterator
796  *
797  * Initializes a NAND iterator based on the information passed by the MTD
798  * layer.
799  */
nanddev_io_iter_init(struct nand_device * nand,enum nand_page_io_req_type reqtype,loff_t offs,struct mtd_oob_ops * req,struct nand_io_iter * iter)800 static inline void nanddev_io_iter_init(struct nand_device *nand,
801 					enum nand_page_io_req_type reqtype,
802 					loff_t offs, struct mtd_oob_ops *req,
803 					struct nand_io_iter *iter)
804 {
805 	struct mtd_info *mtd = nanddev_to_mtd(nand);
806 
807 	iter->req.type = reqtype;
808 	iter->req.mode = req->mode;
809 	iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
810 	iter->req.ooboffs = req->ooboffs;
811 	iter->oobbytes_per_page = mtd_oobavail(mtd, req);
812 	iter->dataleft = req->len;
813 	iter->oobleft = req->ooblen;
814 	iter->req.databuf.in = req->datbuf;
815 	iter->req.datalen = min_t(unsigned int,
816 				  nand->memorg.pagesize - iter->req.dataoffs,
817 				  iter->dataleft);
818 	iter->req.oobbuf.in = req->oobbuf;
819 	iter->req.ooblen = min_t(unsigned int,
820 				 iter->oobbytes_per_page - iter->req.ooboffs,
821 				 iter->oobleft);
822 }
823 
824 /**
825  * nand_io_iter_next_page - Move to the next page
826  * @nand: NAND device
827  * @iter: NAND I/O iterator
828  *
829  * Updates the @iter to point to the next page.
830  */
nanddev_io_iter_next_page(struct nand_device * nand,struct nand_io_iter * iter)831 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
832 					     struct nand_io_iter *iter)
833 {
834 	nanddev_pos_next_page(nand, &iter->req.pos);
835 	iter->dataleft -= iter->req.datalen;
836 	iter->req.databuf.in += iter->req.datalen;
837 	iter->oobleft -= iter->req.ooblen;
838 	iter->req.oobbuf.in += iter->req.ooblen;
839 	iter->req.dataoffs = 0;
840 	iter->req.ooboffs = 0;
841 	iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
842 				  iter->dataleft);
843 	iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
844 				 iter->oobleft);
845 }
846 
847 /**
848  * nand_io_iter_end - Should end iteration or not
849  * @nand: NAND device
850  * @iter: NAND I/O iterator
851  *
852  * Check whether @iter has reached the end of the NAND portion it was asked to
853  * iterate on or not.
854  *
855  * Return: true if @iter has reached the end of the iteration request, false
856  *	   otherwise.
857  */
nanddev_io_iter_end(struct nand_device * nand,const struct nand_io_iter * iter)858 static inline bool nanddev_io_iter_end(struct nand_device *nand,
859 				       const struct nand_io_iter *iter)
860 {
861 	if (iter->dataleft || iter->oobleft)
862 		return false;
863 
864 	return true;
865 }
866 
867 /**
868  * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
869  *			   request
870  * @nand: NAND device
871  * @start: start address to read/write from
872  * @req: MTD I/O request
873  * @iter: NAND I/O iterator
874  *
875  * Should be used for iterate over pages that are contained in an MTD request.
876  */
877 #define nanddev_io_for_each_page(nand, type, start, req, iter)		\
878 	for (nanddev_io_iter_init(nand, type, start, req, iter);	\
879 	     !nanddev_io_iter_end(nand, iter);				\
880 	     nanddev_io_iter_next_page(nand, iter))
881 
882 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
883 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
884 int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos);
885 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
886 
887 /* BBT related functions */
888 enum nand_bbt_block_status {
889 	NAND_BBT_BLOCK_STATUS_UNKNOWN,
890 	NAND_BBT_BLOCK_GOOD,
891 	NAND_BBT_BLOCK_WORN,
892 	NAND_BBT_BLOCK_RESERVED,
893 	NAND_BBT_BLOCK_FACTORY_BAD,
894 	NAND_BBT_BLOCK_NUM_STATUS,
895 };
896 
897 int nanddev_bbt_init(struct nand_device *nand);
898 void nanddev_bbt_cleanup(struct nand_device *nand);
899 int nanddev_bbt_update(struct nand_device *nand);
900 int nanddev_bbt_get_block_status(const struct nand_device *nand,
901 				 unsigned int entry);
902 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
903 				 enum nand_bbt_block_status status);
904 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
905 
906 /**
907  * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
908  * @nand: NAND device
909  * @pos: the NAND position we want to get BBT entry for
910  *
911  * Return the BBT entry used to store information about the eraseblock pointed
912  * by @pos.
913  *
914  * Return: the BBT entry storing information about eraseblock pointed by @pos.
915  */
nanddev_bbt_pos_to_entry(struct nand_device * nand,const struct nand_pos * pos)916 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
917 						    const struct nand_pos *pos)
918 {
919 	return pos->eraseblock +
920 	       ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
921 		nand->memorg.eraseblocks_per_lun);
922 }
923 
924 /**
925  * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
926  * @nand: NAND device
927  *
928  * Return: true if the BBT has been initialized, false otherwise.
929  */
nanddev_bbt_is_initialized(struct nand_device * nand)930 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
931 {
932 	return !!nand->bbt.cache;
933 }
934 
935 /* MTD -> NAND helper functions. */
936 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
937 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
938 
939 #endif /* __LINUX_MTD_NAND_H */
940