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 bad 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 * @continuous: no need to start over the operation at the end of each page, the
107 * NAND device will automatically prepare the next one
108 *
109 * This object is used to pass per-page I/O requests to NAND sub-layers. This
110 * way all useful information are already formatted in a useful way and
111 * specific NAND layers can focus on translating these information into
112 * specific commands/operations.
113 */
114 struct nand_page_io_req {
115 enum nand_page_io_req_type type;
116 struct nand_pos pos;
117 unsigned int dataoffs;
118 unsigned int datalen;
119 union {
120 const void *out;
121 void *in;
122 } databuf;
123 unsigned int ooboffs;
124 unsigned int ooblen;
125 union {
126 const void *out;
127 void *in;
128 } oobbuf;
129 int mode;
130 bool continuous;
131 };
132
133 const struct mtd_ooblayout_ops *nand_get_small_page_ooblayout(void);
134 const struct mtd_ooblayout_ops *nand_get_large_page_ooblayout(void);
135 const struct mtd_ooblayout_ops *nand_get_large_page_hamming_ooblayout(void);
136
137 /**
138 * enum nand_ecc_engine_type - NAND ECC engine type
139 * @NAND_ECC_ENGINE_TYPE_INVALID: Invalid value
140 * @NAND_ECC_ENGINE_TYPE_NONE: No ECC correction
141 * @NAND_ECC_ENGINE_TYPE_SOFT: Software ECC correction
142 * @NAND_ECC_ENGINE_TYPE_ON_HOST: On host hardware ECC correction
143 * @NAND_ECC_ENGINE_TYPE_ON_DIE: On chip hardware ECC correction
144 */
145 enum nand_ecc_engine_type {
146 NAND_ECC_ENGINE_TYPE_INVALID,
147 NAND_ECC_ENGINE_TYPE_NONE,
148 NAND_ECC_ENGINE_TYPE_SOFT,
149 NAND_ECC_ENGINE_TYPE_ON_HOST,
150 NAND_ECC_ENGINE_TYPE_ON_DIE,
151 };
152
153 /**
154 * enum nand_ecc_placement - NAND ECC bytes placement
155 * @NAND_ECC_PLACEMENT_UNKNOWN: The actual position of the ECC bytes is unknown
156 * @NAND_ECC_PLACEMENT_OOB: The ECC bytes are located in the OOB area
157 * @NAND_ECC_PLACEMENT_INTERLEAVED: Syndrome layout, there are ECC bytes
158 * interleaved with regular data in the main
159 * area
160 */
161 enum nand_ecc_placement {
162 NAND_ECC_PLACEMENT_UNKNOWN,
163 NAND_ECC_PLACEMENT_OOB,
164 NAND_ECC_PLACEMENT_INTERLEAVED,
165 };
166
167 /**
168 * enum nand_ecc_algo - NAND ECC algorithm
169 * @NAND_ECC_ALGO_UNKNOWN: Unknown algorithm
170 * @NAND_ECC_ALGO_HAMMING: Hamming algorithm
171 * @NAND_ECC_ALGO_BCH: Bose-Chaudhuri-Hocquenghem algorithm
172 * @NAND_ECC_ALGO_RS: Reed-Solomon algorithm
173 */
174 enum nand_ecc_algo {
175 NAND_ECC_ALGO_UNKNOWN,
176 NAND_ECC_ALGO_HAMMING,
177 NAND_ECC_ALGO_BCH,
178 NAND_ECC_ALGO_RS,
179 };
180
181 /**
182 * struct nand_ecc_props - NAND ECC properties
183 * @engine_type: ECC engine type
184 * @placement: OOB placement (if relevant)
185 * @algo: ECC algorithm (if relevant)
186 * @strength: ECC strength
187 * @step_size: Number of bytes per step
188 * @flags: Misc properties
189 */
190 struct nand_ecc_props {
191 enum nand_ecc_engine_type engine_type;
192 enum nand_ecc_placement placement;
193 enum nand_ecc_algo algo;
194 unsigned int strength;
195 unsigned int step_size;
196 unsigned int flags;
197 };
198
199 #define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
200
201 /* NAND ECC misc flags */
202 #define NAND_ECC_MAXIMIZE_STRENGTH BIT(0)
203
204 /**
205 * struct nand_bbt - bad block table object
206 * @cache: in memory BBT cache
207 */
208 struct nand_bbt {
209 unsigned long *cache;
210 };
211
212 /**
213 * struct nand_ops - NAND operations
214 * @erase: erase a specific block. No need to check if the block is bad before
215 * erasing, this has been taken care of by the generic NAND layer
216 * @markbad: mark a specific block bad. No need to check if the block is
217 * already marked bad, this has been taken care of by the generic
218 * NAND layer. This method should just write the BBM (Bad Block
219 * Marker) so that future call to struct_nand_ops->isbad() return
220 * true
221 * @isbad: check whether a block is bad or not. This method should just read
222 * the BBM and return whether the block is bad or not based on what it
223 * reads
224 *
225 * These are all low level operations that should be implemented by specialized
226 * NAND layers (SPI NAND, raw NAND, ...).
227 */
228 struct nand_ops {
229 int (*erase)(struct nand_device *nand, const struct nand_pos *pos);
230 int (*markbad)(struct nand_device *nand, const struct nand_pos *pos);
231 bool (*isbad)(struct nand_device *nand, const struct nand_pos *pos);
232 };
233
234 /**
235 * struct nand_ecc_context - Context for the ECC engine
236 * @conf: basic ECC engine parameters
237 * @nsteps: number of ECC steps
238 * @total: total number of bytes used for storing ECC codes, this is used by
239 * generic OOB layouts
240 * @priv: ECC engine driver private data
241 */
242 struct nand_ecc_context {
243 struct nand_ecc_props conf;
244 unsigned int nsteps;
245 unsigned int total;
246 void *priv;
247 };
248
249 /**
250 * struct nand_ecc_engine_ops - ECC engine operations
251 * @init_ctx: given a desired user configuration for the pointed NAND device,
252 * requests the ECC engine driver to setup a configuration with
253 * values it supports.
254 * @cleanup_ctx: clean the context initialized by @init_ctx.
255 * @prepare_io_req: is called before reading/writing a page to prepare the I/O
256 * request to be performed with ECC correction.
257 * @finish_io_req: is called after reading/writing a page to terminate the I/O
258 * request and ensure proper ECC correction.
259 */
260 struct nand_ecc_engine_ops {
261 int (*init_ctx)(struct nand_device *nand);
262 void (*cleanup_ctx)(struct nand_device *nand);
263 int (*prepare_io_req)(struct nand_device *nand,
264 struct nand_page_io_req *req);
265 int (*finish_io_req)(struct nand_device *nand,
266 struct nand_page_io_req *req);
267 };
268
269 /**
270 * enum nand_ecc_engine_integration - How the NAND ECC engine is integrated
271 * @NAND_ECC_ENGINE_INTEGRATION_INVALID: Invalid value
272 * @NAND_ECC_ENGINE_INTEGRATION_PIPELINED: Pipelined engine, performs on-the-fly
273 * correction, does not need to copy
274 * data around
275 * @NAND_ECC_ENGINE_INTEGRATION_EXTERNAL: External engine, needs to bring the
276 * data into its own area before use
277 */
278 enum nand_ecc_engine_integration {
279 NAND_ECC_ENGINE_INTEGRATION_INVALID,
280 NAND_ECC_ENGINE_INTEGRATION_PIPELINED,
281 NAND_ECC_ENGINE_INTEGRATION_EXTERNAL,
282 };
283
284 /**
285 * struct nand_ecc_engine - ECC engine abstraction for NAND devices
286 * @dev: Host device
287 * @node: Private field for registration time
288 * @ops: ECC engine operations
289 * @integration: How the engine is integrated with the host
290 * (only relevant on %NAND_ECC_ENGINE_TYPE_ON_HOST engines)
291 * @priv: Private data
292 */
293 struct nand_ecc_engine {
294 struct device *dev;
295 struct list_head node;
296 const struct nand_ecc_engine_ops *ops;
297 enum nand_ecc_engine_integration integration;
298 void *priv;
299 };
300
301 void of_get_nand_ecc_user_config(struct nand_device *nand);
302 int nand_ecc_init_ctx(struct nand_device *nand);
303 void nand_ecc_cleanup_ctx(struct nand_device *nand);
304 int nand_ecc_prepare_io_req(struct nand_device *nand,
305 struct nand_page_io_req *req);
306 int nand_ecc_finish_io_req(struct nand_device *nand,
307 struct nand_page_io_req *req);
308 bool nand_ecc_is_pipelined(const struct nand_device *nand);
309
310 bool nand_ecc_is_strong_enough(struct nand_device *nand);
311
312 #if IS_REACHABLE(CONFIG_MTD_NAND_CORE)
313 int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine);
314 int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine);
315 #else
316 static inline int
nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine * engine)317 nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine)
318 {
319 return -ENOTSUPP;
320 }
321 static inline int
nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine * engine)322 nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine)
323 {
324 return -ENOTSUPP;
325 }
326 #endif
327
328 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand);
329 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand);
330 struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand);
331 void nand_ecc_put_on_host_hw_engine(struct nand_device *nand);
332 struct device *nand_ecc_get_engine_dev(struct device *host);
333
334 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
335 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);
336 #else
nand_ecc_sw_hamming_get_engine(void)337 static inline struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void)
338 {
339 return NULL;
340 }
341 #endif /* CONFIG_MTD_NAND_ECC_SW_HAMMING */
342
343 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)
344 struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void);
345 #else
nand_ecc_sw_bch_get_engine(void)346 static inline struct nand_ecc_engine *nand_ecc_sw_bch_get_engine(void)
347 {
348 return NULL;
349 }
350 #endif /* CONFIG_MTD_NAND_ECC_SW_BCH */
351
352 /**
353 * struct nand_ecc_req_tweak_ctx - Help for automatically tweaking requests
354 * @orig_req: Pointer to the original IO request
355 * @nand: Related NAND device, to have access to its memory organization
356 * @page_buffer_size: Real size of the page buffer to use (can be set by the
357 * user before the tweaking mechanism initialization)
358 * @oob_buffer_size: Real size of the OOB buffer to use (can be set by the
359 * user before the tweaking mechanism initialization)
360 * @spare_databuf: Data bounce buffer
361 * @spare_oobbuf: OOB bounce buffer
362 * @bounce_data: Flag indicating a data bounce buffer is used
363 * @bounce_oob: Flag indicating an OOB bounce buffer is used
364 */
365 struct nand_ecc_req_tweak_ctx {
366 struct nand_page_io_req orig_req;
367 struct nand_device *nand;
368 unsigned int page_buffer_size;
369 unsigned int oob_buffer_size;
370 void *spare_databuf;
371 void *spare_oobbuf;
372 bool bounce_data;
373 bool bounce_oob;
374 };
375
376 int nand_ecc_init_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx,
377 struct nand_device *nand);
378 void nand_ecc_cleanup_req_tweaking(struct nand_ecc_req_tweak_ctx *ctx);
379 void nand_ecc_tweak_req(struct nand_ecc_req_tweak_ctx *ctx,
380 struct nand_page_io_req *req);
381 void nand_ecc_restore_req(struct nand_ecc_req_tweak_ctx *ctx,
382 struct nand_page_io_req *req);
383
384 /**
385 * struct nand_ecc - Information relative to the ECC
386 * @defaults: Default values, depend on the underlying subsystem
387 * @requirements: ECC requirements from the NAND chip perspective
388 * @user_conf: User desires in terms of ECC parameters
389 * @ctx: ECC context for the ECC engine, derived from the device @requirements
390 * the @user_conf and the @defaults
391 * @ondie_engine: On-die ECC engine reference, if any
392 * @engine: ECC engine actually bound
393 */
394 struct nand_ecc {
395 struct nand_ecc_props defaults;
396 struct nand_ecc_props requirements;
397 struct nand_ecc_props user_conf;
398 struct nand_ecc_context ctx;
399 struct nand_ecc_engine *ondie_engine;
400 struct nand_ecc_engine *engine;
401 };
402
403 /**
404 * struct nand_device - NAND device
405 * @mtd: MTD instance attached to the NAND device
406 * @memorg: memory layout
407 * @ecc: NAND ECC object attached to the NAND device
408 * @rowconv: position to row address converter
409 * @bbt: bad block table info
410 * @ops: NAND operations attached to the NAND device
411 *
412 * Generic NAND object. Specialized NAND layers (raw NAND, SPI NAND, OneNAND)
413 * should declare their own NAND object embedding a nand_device struct (that's
414 * how inheritance is done).
415 * struct_nand_device->memorg and struct_nand_device->ecc.requirements should
416 * be filled at device detection time to reflect the NAND device
417 * capabilities/requirements. Once this is done nanddev_init() can be called.
418 * It will take care of converting NAND information into MTD ones, which means
419 * the specialized NAND layers should never manually tweak
420 * struct_nand_device->mtd except for the ->_read/write() hooks.
421 */
422 struct nand_device {
423 struct mtd_info mtd;
424 struct nand_memory_organization memorg;
425 struct nand_ecc ecc;
426 struct nand_row_converter rowconv;
427 struct nand_bbt bbt;
428 const struct nand_ops *ops;
429 };
430
431 /**
432 * struct nand_io_iter - NAND I/O iterator
433 * @req: current I/O request
434 * @oobbytes_per_page: maximum number of OOB bytes per page
435 * @dataleft: remaining number of data bytes to read/write
436 * @oobleft: remaining number of OOB bytes to read/write
437 *
438 * Can be used by specialized NAND layers to iterate over all pages covered
439 * by an MTD I/O request, which should greatly simplifies the boiler-plate
440 * code needed to read/write data from/to a NAND device.
441 */
442 struct nand_io_iter {
443 struct nand_page_io_req req;
444 unsigned int oobbytes_per_page;
445 unsigned int dataleft;
446 unsigned int oobleft;
447 };
448
449 /**
450 * mtd_to_nanddev() - Get the NAND device attached to the MTD instance
451 * @mtd: MTD instance
452 *
453 * Return: the NAND device embedding @mtd.
454 */
mtd_to_nanddev(struct mtd_info * mtd)455 static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
456 {
457 return container_of(mtd, struct nand_device, mtd);
458 }
459
460 /**
461 * nanddev_to_mtd() - Get the MTD device attached to a NAND device
462 * @nand: NAND device
463 *
464 * Return: the MTD device embedded in @nand.
465 */
nanddev_to_mtd(struct nand_device * nand)466 static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
467 {
468 return &nand->mtd;
469 }
470
471 /*
472 * nanddev_bits_per_cell() - Get the number of bits per cell
473 * @nand: NAND device
474 *
475 * Return: the number of bits per cell.
476 */
nanddev_bits_per_cell(const struct nand_device * nand)477 static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
478 {
479 return nand->memorg.bits_per_cell;
480 }
481
482 /**
483 * nanddev_page_size() - Get NAND page size
484 * @nand: NAND device
485 *
486 * Return: the page size.
487 */
nanddev_page_size(const struct nand_device * nand)488 static inline size_t nanddev_page_size(const struct nand_device *nand)
489 {
490 return nand->memorg.pagesize;
491 }
492
493 /**
494 * nanddev_per_page_oobsize() - Get NAND OOB size
495 * @nand: NAND device
496 *
497 * Return: the OOB size.
498 */
499 static inline unsigned int
nanddev_per_page_oobsize(const struct nand_device * nand)500 nanddev_per_page_oobsize(const struct nand_device *nand)
501 {
502 return nand->memorg.oobsize;
503 }
504
505 /**
506 * nanddev_pages_per_eraseblock() - Get the number of pages per eraseblock
507 * @nand: NAND device
508 *
509 * Return: the number of pages per eraseblock.
510 */
511 static inline unsigned int
nanddev_pages_per_eraseblock(const struct nand_device * nand)512 nanddev_pages_per_eraseblock(const struct nand_device *nand)
513 {
514 return nand->memorg.pages_per_eraseblock;
515 }
516
517 /**
518 * nanddev_pages_per_target() - Get the number of pages per target
519 * @nand: NAND device
520 *
521 * Return: the number of pages per target.
522 */
523 static inline unsigned int
nanddev_pages_per_target(const struct nand_device * nand)524 nanddev_pages_per_target(const struct nand_device *nand)
525 {
526 return nand->memorg.pages_per_eraseblock *
527 nand->memorg.eraseblocks_per_lun *
528 nand->memorg.luns_per_target;
529 }
530
531 /**
532 * nanddev_per_page_oobsize() - Get NAND erase block size
533 * @nand: NAND device
534 *
535 * Return: the eraseblock size.
536 */
nanddev_eraseblock_size(const struct nand_device * nand)537 static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
538 {
539 return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
540 }
541
542 /**
543 * nanddev_eraseblocks_per_lun() - Get the number of eraseblocks per LUN
544 * @nand: NAND device
545 *
546 * Return: the number of eraseblocks per LUN.
547 */
548 static inline unsigned int
nanddev_eraseblocks_per_lun(const struct nand_device * nand)549 nanddev_eraseblocks_per_lun(const struct nand_device *nand)
550 {
551 return nand->memorg.eraseblocks_per_lun;
552 }
553
554 /**
555 * nanddev_eraseblocks_per_target() - Get the number of eraseblocks per target
556 * @nand: NAND device
557 *
558 * Return: the number of eraseblocks per target.
559 */
560 static inline unsigned int
nanddev_eraseblocks_per_target(const struct nand_device * nand)561 nanddev_eraseblocks_per_target(const struct nand_device *nand)
562 {
563 return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
564 }
565
566 /**
567 * nanddev_target_size() - Get the total size provided by a single target/die
568 * @nand: NAND device
569 *
570 * Return: the total size exposed by a single target/die in bytes.
571 */
nanddev_target_size(const struct nand_device * nand)572 static inline u64 nanddev_target_size(const struct nand_device *nand)
573 {
574 return (u64)nand->memorg.luns_per_target *
575 nand->memorg.eraseblocks_per_lun *
576 nand->memorg.pages_per_eraseblock *
577 nand->memorg.pagesize;
578 }
579
580 /**
581 * nanddev_ntarget() - Get the total of targets
582 * @nand: NAND device
583 *
584 * Return: the number of targets/dies exposed by @nand.
585 */
nanddev_ntargets(const struct nand_device * nand)586 static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
587 {
588 return nand->memorg.ntargets;
589 }
590
591 /**
592 * nanddev_neraseblocks() - Get the total number of eraseblocks
593 * @nand: NAND device
594 *
595 * Return: the total number of eraseblocks exposed by @nand.
596 */
nanddev_neraseblocks(const struct nand_device * nand)597 static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
598 {
599 return nand->memorg.ntargets * nand->memorg.luns_per_target *
600 nand->memorg.eraseblocks_per_lun;
601 }
602
603 /**
604 * nanddev_size() - Get NAND size
605 * @nand: NAND device
606 *
607 * Return: the total size (in bytes) exposed by @nand.
608 */
nanddev_size(const struct nand_device * nand)609 static inline u64 nanddev_size(const struct nand_device *nand)
610 {
611 return nanddev_target_size(nand) * nanddev_ntargets(nand);
612 }
613
614 /**
615 * nanddev_get_memorg() - Extract memory organization info from a NAND device
616 * @nand: NAND device
617 *
618 * This can be used by the upper layer to fill the memorg info before calling
619 * nanddev_init().
620 *
621 * Return: the memorg object embedded in the NAND device.
622 */
623 static inline struct nand_memory_organization *
nanddev_get_memorg(struct nand_device * nand)624 nanddev_get_memorg(struct nand_device *nand)
625 {
626 return &nand->memorg;
627 }
628
629 /**
630 * nanddev_get_ecc_conf() - Extract the ECC configuration from a NAND device
631 * @nand: NAND device
632 */
633 static inline const struct nand_ecc_props *
nanddev_get_ecc_conf(struct nand_device * nand)634 nanddev_get_ecc_conf(struct nand_device *nand)
635 {
636 return &nand->ecc.ctx.conf;
637 }
638
639 /**
640 * nanddev_get_ecc_nsteps() - Extract the number of ECC steps
641 * @nand: NAND device
642 */
643 static inline unsigned int
nanddev_get_ecc_nsteps(struct nand_device * nand)644 nanddev_get_ecc_nsteps(struct nand_device *nand)
645 {
646 return nand->ecc.ctx.nsteps;
647 }
648
649 /**
650 * nanddev_get_ecc_bytes_per_step() - Extract the number of ECC bytes per step
651 * @nand: NAND device
652 */
653 static inline unsigned int
nanddev_get_ecc_bytes_per_step(struct nand_device * nand)654 nanddev_get_ecc_bytes_per_step(struct nand_device *nand)
655 {
656 return nand->ecc.ctx.total / nand->ecc.ctx.nsteps;
657 }
658
659 /**
660 * nanddev_get_ecc_requirements() - Extract the ECC requirements from a NAND
661 * device
662 * @nand: NAND device
663 */
664 static inline const struct nand_ecc_props *
nanddev_get_ecc_requirements(struct nand_device * nand)665 nanddev_get_ecc_requirements(struct nand_device *nand)
666 {
667 return &nand->ecc.requirements;
668 }
669
670 /**
671 * nanddev_set_ecc_requirements() - Assign the ECC requirements of a NAND
672 * device
673 * @nand: NAND device
674 * @reqs: Requirements
675 */
676 static inline void
nanddev_set_ecc_requirements(struct nand_device * nand,const struct nand_ecc_props * reqs)677 nanddev_set_ecc_requirements(struct nand_device *nand,
678 const struct nand_ecc_props *reqs)
679 {
680 nand->ecc.requirements = *reqs;
681 }
682
683 int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
684 struct module *owner);
685 void nanddev_cleanup(struct nand_device *nand);
686
687 /**
688 * nanddev_register() - Register a NAND device
689 * @nand: NAND device
690 *
691 * Register a NAND device.
692 * This function is just a wrapper around mtd_device_register()
693 * registering the MTD device embedded in @nand.
694 *
695 * Return: 0 in case of success, a negative error code otherwise.
696 */
nanddev_register(struct nand_device * nand)697 static inline int nanddev_register(struct nand_device *nand)
698 {
699 return mtd_device_register(&nand->mtd, NULL, 0);
700 }
701
702 /**
703 * nanddev_unregister() - Unregister a NAND device
704 * @nand: NAND device
705 *
706 * Unregister a NAND device.
707 * This function is just a wrapper around mtd_device_unregister()
708 * unregistering the MTD device embedded in @nand.
709 *
710 * Return: 0 in case of success, a negative error code otherwise.
711 */
nanddev_unregister(struct nand_device * nand)712 static inline int nanddev_unregister(struct nand_device *nand)
713 {
714 return mtd_device_unregister(&nand->mtd);
715 }
716
717 /**
718 * nanddev_set_of_node() - Attach a DT node to a NAND device
719 * @nand: NAND device
720 * @np: DT node
721 *
722 * Attach a DT node to a NAND device.
723 */
nanddev_set_of_node(struct nand_device * nand,struct device_node * np)724 static inline void nanddev_set_of_node(struct nand_device *nand,
725 struct device_node *np)
726 {
727 mtd_set_of_node(&nand->mtd, np);
728 }
729
730 /**
731 * nanddev_get_of_node() - Retrieve the DT node attached to a NAND device
732 * @nand: NAND device
733 *
734 * Return: the DT node attached to @nand.
735 */
nanddev_get_of_node(struct nand_device * nand)736 static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
737 {
738 return mtd_get_of_node(&nand->mtd);
739 }
740
741 /**
742 * nanddev_offs_to_pos() - Convert an absolute NAND offset into a NAND position
743 * @nand: NAND device
744 * @offs: absolute NAND offset (usually passed by the MTD layer)
745 * @pos: a NAND position object to fill in
746 *
747 * Converts @offs into a nand_pos representation.
748 *
749 * Return: the offset within the NAND page pointed by @pos.
750 */
nanddev_offs_to_pos(struct nand_device * nand,loff_t offs,struct nand_pos * pos)751 static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
752 loff_t offs,
753 struct nand_pos *pos)
754 {
755 unsigned int pageoffs;
756 u64 tmp = offs;
757
758 pageoffs = do_div(tmp, nand->memorg.pagesize);
759 pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
760 pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
761 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
762 pos->lun = do_div(tmp, nand->memorg.luns_per_target);
763 pos->target = tmp;
764
765 return pageoffs;
766 }
767
768 /**
769 * nanddev_pos_cmp() - Compare two NAND positions
770 * @a: First NAND position
771 * @b: Second NAND position
772 *
773 * Compares two NAND positions.
774 *
775 * Return: -1 if @a < @b, 0 if @a == @b and 1 if @a > @b.
776 */
nanddev_pos_cmp(const struct nand_pos * a,const struct nand_pos * b)777 static inline int nanddev_pos_cmp(const struct nand_pos *a,
778 const struct nand_pos *b)
779 {
780 if (a->target != b->target)
781 return a->target < b->target ? -1 : 1;
782
783 if (a->lun != b->lun)
784 return a->lun < b->lun ? -1 : 1;
785
786 if (a->eraseblock != b->eraseblock)
787 return a->eraseblock < b->eraseblock ? -1 : 1;
788
789 if (a->page != b->page)
790 return a->page < b->page ? -1 : 1;
791
792 return 0;
793 }
794
795 /**
796 * nanddev_pos_to_offs() - Convert a NAND position into an absolute offset
797 * @nand: NAND device
798 * @pos: the NAND position to convert
799 *
800 * Converts @pos NAND position into an absolute offset.
801 *
802 * Return: the absolute offset. Note that @pos points to the beginning of a
803 * page, if one wants to point to a specific offset within this page
804 * the returned offset has to be adjusted manually.
805 */
nanddev_pos_to_offs(struct nand_device * nand,const struct nand_pos * pos)806 static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
807 const struct nand_pos *pos)
808 {
809 unsigned int npages;
810
811 npages = pos->page +
812 ((pos->eraseblock +
813 (pos->lun +
814 (pos->target * nand->memorg.luns_per_target)) *
815 nand->memorg.eraseblocks_per_lun) *
816 nand->memorg.pages_per_eraseblock);
817
818 return (loff_t)npages * nand->memorg.pagesize;
819 }
820
821 /**
822 * nanddev_pos_to_row() - Extract a row address from a NAND position
823 * @nand: NAND device
824 * @pos: the position to convert
825 *
826 * Converts a NAND position into a row address that can then be passed to the
827 * device.
828 *
829 * Return: the row address extracted from @pos.
830 */
nanddev_pos_to_row(struct nand_device * nand,const struct nand_pos * pos)831 static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
832 const struct nand_pos *pos)
833 {
834 return (pos->lun << nand->rowconv.lun_addr_shift) |
835 (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
836 pos->page;
837 }
838
839 /**
840 * nanddev_pos_next_target() - Move a position to the next target/die
841 * @nand: NAND device
842 * @pos: the position to update
843 *
844 * Updates @pos to point to the start of the next target/die. Useful when you
845 * want to iterate over all targets/dies of a NAND device.
846 */
nanddev_pos_next_target(struct nand_device * nand,struct nand_pos * pos)847 static inline void nanddev_pos_next_target(struct nand_device *nand,
848 struct nand_pos *pos)
849 {
850 pos->page = 0;
851 pos->plane = 0;
852 pos->eraseblock = 0;
853 pos->lun = 0;
854 pos->target++;
855 }
856
857 /**
858 * nanddev_pos_next_lun() - Move a position to the next LUN
859 * @nand: NAND device
860 * @pos: the position to update
861 *
862 * Updates @pos to point to the start of the next LUN. Useful when you want to
863 * iterate over all LUNs of a NAND device.
864 */
nanddev_pos_next_lun(struct nand_device * nand,struct nand_pos * pos)865 static inline void nanddev_pos_next_lun(struct nand_device *nand,
866 struct nand_pos *pos)
867 {
868 if (pos->lun >= nand->memorg.luns_per_target - 1)
869 return nanddev_pos_next_target(nand, pos);
870
871 pos->lun++;
872 pos->page = 0;
873 pos->plane = 0;
874 pos->eraseblock = 0;
875 }
876
877 /**
878 * nanddev_pos_next_eraseblock() - Move a position to the next eraseblock
879 * @nand: NAND device
880 * @pos: the position to update
881 *
882 * Updates @pos to point to the start of the next eraseblock. Useful when you
883 * want to iterate over all eraseblocks of a NAND device.
884 */
nanddev_pos_next_eraseblock(struct nand_device * nand,struct nand_pos * pos)885 static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
886 struct nand_pos *pos)
887 {
888 if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
889 return nanddev_pos_next_lun(nand, pos);
890
891 pos->eraseblock++;
892 pos->page = 0;
893 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
894 }
895
896 /**
897 * nanddev_pos_next_page() - Move a position to the next page
898 * @nand: NAND device
899 * @pos: the position to update
900 *
901 * Updates @pos to point to the start of the next page. Useful when you want to
902 * iterate over all pages of a NAND device.
903 */
nanddev_pos_next_page(struct nand_device * nand,struct nand_pos * pos)904 static inline void nanddev_pos_next_page(struct nand_device *nand,
905 struct nand_pos *pos)
906 {
907 if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
908 return nanddev_pos_next_eraseblock(nand, pos);
909
910 pos->page++;
911 }
912
913 /**
914 * nand_io_page_iter_init - Initialize a NAND I/O iterator
915 * @nand: NAND device
916 * @offs: absolute offset
917 * @req: MTD request
918 * @iter: NAND I/O iterator
919 *
920 * Initializes a NAND iterator based on the information passed by the MTD
921 * layer for page jumps.
922 */
nanddev_io_page_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)923 static inline void nanddev_io_page_iter_init(struct nand_device *nand,
924 enum nand_page_io_req_type reqtype,
925 loff_t offs, struct mtd_oob_ops *req,
926 struct nand_io_iter *iter)
927 {
928 struct mtd_info *mtd = nanddev_to_mtd(nand);
929
930 iter->req.type = reqtype;
931 iter->req.mode = req->mode;
932 iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
933 iter->req.ooboffs = req->ooboffs;
934 iter->oobbytes_per_page = mtd_oobavail(mtd, req);
935 iter->dataleft = req->len;
936 iter->oobleft = req->ooblen;
937 iter->req.databuf.in = req->datbuf;
938 iter->req.datalen = min_t(unsigned int,
939 nand->memorg.pagesize - iter->req.dataoffs,
940 iter->dataleft);
941 iter->req.oobbuf.in = req->oobbuf;
942 iter->req.ooblen = min_t(unsigned int,
943 iter->oobbytes_per_page - iter->req.ooboffs,
944 iter->oobleft);
945 iter->req.continuous = false;
946 }
947
948 /**
949 * nand_io_block_iter_init - Initialize a NAND I/O iterator
950 * @nand: NAND device
951 * @offs: absolute offset
952 * @req: MTD request
953 * @iter: NAND I/O iterator
954 *
955 * Initializes a NAND iterator based on the information passed by the MTD
956 * layer for block jumps (no OOB)
957 *
958 * In practice only reads may leverage this iterator.
959 */
nanddev_io_block_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)960 static inline void nanddev_io_block_iter_init(struct nand_device *nand,
961 enum nand_page_io_req_type reqtype,
962 loff_t offs, struct mtd_oob_ops *req,
963 struct nand_io_iter *iter)
964 {
965 unsigned int offs_in_eb;
966
967 iter->req.type = reqtype;
968 iter->req.mode = req->mode;
969 iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
970 iter->req.ooboffs = 0;
971 iter->oobbytes_per_page = 0;
972 iter->dataleft = req->len;
973 iter->oobleft = 0;
974 iter->req.databuf.in = req->datbuf;
975 offs_in_eb = (nand->memorg.pagesize * iter->req.pos.page) + iter->req.dataoffs;
976 iter->req.datalen = min_t(unsigned int,
977 nanddev_eraseblock_size(nand) - offs_in_eb,
978 iter->dataleft);
979 iter->req.oobbuf.in = NULL;
980 iter->req.ooblen = 0;
981 iter->req.continuous = true;
982 }
983
984 /**
985 * nand_io_iter_next_page - Move to the next page
986 * @nand: NAND device
987 * @iter: NAND I/O iterator
988 *
989 * Updates the @iter to point to the next page.
990 */
nanddev_io_iter_next_page(struct nand_device * nand,struct nand_io_iter * iter)991 static inline void nanddev_io_iter_next_page(struct nand_device *nand,
992 struct nand_io_iter *iter)
993 {
994 nanddev_pos_next_page(nand, &iter->req.pos);
995 iter->dataleft -= iter->req.datalen;
996 iter->req.databuf.in += iter->req.datalen;
997 iter->oobleft -= iter->req.ooblen;
998 iter->req.oobbuf.in += iter->req.ooblen;
999 iter->req.dataoffs = 0;
1000 iter->req.ooboffs = 0;
1001 iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
1002 iter->dataleft);
1003 iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
1004 iter->oobleft);
1005 }
1006
1007 /**
1008 * nand_io_iter_next_block - Move to the next block
1009 * @nand: NAND device
1010 * @iter: NAND I/O iterator
1011 *
1012 * Updates the @iter to point to the next block.
1013 * No OOB handling available.
1014 */
nanddev_io_iter_next_block(struct nand_device * nand,struct nand_io_iter * iter)1015 static inline void nanddev_io_iter_next_block(struct nand_device *nand,
1016 struct nand_io_iter *iter)
1017 {
1018 nanddev_pos_next_eraseblock(nand, &iter->req.pos);
1019 iter->dataleft -= iter->req.datalen;
1020 iter->req.databuf.in += iter->req.datalen;
1021 iter->req.dataoffs = 0;
1022 iter->req.datalen = min_t(unsigned int, nanddev_eraseblock_size(nand),
1023 iter->dataleft);
1024 }
1025
1026 /**
1027 * nand_io_iter_end - Should end iteration or not
1028 * @nand: NAND device
1029 * @iter: NAND I/O iterator
1030 *
1031 * Check whether @iter has reached the end of the NAND portion it was asked to
1032 * iterate on or not.
1033 *
1034 * Return: true if @iter has reached the end of the iteration request, false
1035 * otherwise.
1036 */
nanddev_io_iter_end(struct nand_device * nand,const struct nand_io_iter * iter)1037 static inline bool nanddev_io_iter_end(struct nand_device *nand,
1038 const struct nand_io_iter *iter)
1039 {
1040 if (iter->dataleft || iter->oobleft)
1041 return false;
1042
1043 return true;
1044 }
1045
1046 /**
1047 * nand_io_for_each_page - Iterate over all NAND pages contained in an MTD I/O
1048 * request
1049 * @nand: NAND device
1050 * @start: start address to read/write from
1051 * @req: MTD I/O request
1052 * @iter: NAND I/O iterator
1053 *
1054 * Should be used for iterating over pages that are contained in an MTD request.
1055 */
1056 #define nanddev_io_for_each_page(nand, type, start, req, iter) \
1057 for (nanddev_io_page_iter_init(nand, type, start, req, iter); \
1058 !nanddev_io_iter_end(nand, iter); \
1059 nanddev_io_iter_next_page(nand, iter))
1060
1061 /**
1062 * nand_io_for_each_block - Iterate over all NAND pages contained in an MTD I/O
1063 * request, one block at a time
1064 * @nand: NAND device
1065 * @start: start address to read/write from
1066 * @req: MTD I/O request
1067 * @iter: NAND I/O iterator
1068 *
1069 * Should be used for iterating over blocks that are contained in an MTD request.
1070 */
1071 #define nanddev_io_for_each_block(nand, type, start, req, iter) \
1072 for (nanddev_io_block_iter_init(nand, type, start, req, iter); \
1073 !nanddev_io_iter_end(nand, iter); \
1074 nanddev_io_iter_next_block(nand, iter))
1075
1076 bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
1077 bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
1078 int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
1079
1080 /* ECC related functions */
1081 int nanddev_ecc_engine_init(struct nand_device *nand);
1082 void nanddev_ecc_engine_cleanup(struct nand_device *nand);
1083
nand_to_ecc_ctx(struct nand_device * nand)1084 static inline void *nand_to_ecc_ctx(struct nand_device *nand)
1085 {
1086 return nand->ecc.ctx.priv;
1087 }
1088
1089 /* BBT related functions */
1090 enum nand_bbt_block_status {
1091 NAND_BBT_BLOCK_STATUS_UNKNOWN,
1092 NAND_BBT_BLOCK_GOOD,
1093 NAND_BBT_BLOCK_WORN,
1094 NAND_BBT_BLOCK_RESERVED,
1095 NAND_BBT_BLOCK_FACTORY_BAD,
1096 NAND_BBT_BLOCK_NUM_STATUS,
1097 };
1098
1099 int nanddev_bbt_init(struct nand_device *nand);
1100 void nanddev_bbt_cleanup(struct nand_device *nand);
1101 int nanddev_bbt_update(struct nand_device *nand);
1102 int nanddev_bbt_get_block_status(const struct nand_device *nand,
1103 unsigned int entry);
1104 int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
1105 enum nand_bbt_block_status status);
1106 int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
1107
1108 /**
1109 * nanddev_bbt_pos_to_entry() - Convert a NAND position into a BBT entry
1110 * @nand: NAND device
1111 * @pos: the NAND position we want to get BBT entry for
1112 *
1113 * Return the BBT entry used to store information about the eraseblock pointed
1114 * by @pos.
1115 *
1116 * Return: the BBT entry storing information about eraseblock pointed by @pos.
1117 */
nanddev_bbt_pos_to_entry(struct nand_device * nand,const struct nand_pos * pos)1118 static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
1119 const struct nand_pos *pos)
1120 {
1121 return pos->eraseblock +
1122 ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
1123 nand->memorg.eraseblocks_per_lun);
1124 }
1125
1126 /**
1127 * nanddev_bbt_is_initialized() - Check if the BBT has been initialized
1128 * @nand: NAND device
1129 *
1130 * Return: true if the BBT has been initialized, false otherwise.
1131 */
nanddev_bbt_is_initialized(struct nand_device * nand)1132 static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
1133 {
1134 return !!nand->bbt.cache;
1135 }
1136
1137 /* MTD -> NAND helper functions. */
1138 int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
1139 int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
1140
1141 int nand_check_erased_ecc_chunk(void *data, int datalen,
1142 void *ecc, int ecclen,
1143 void *extraoob, int extraooblen,
1144 int threshold);
1145
1146 #endif /* __LINUX_MTD_NAND_H */
1147