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