1
2
3
4
5
6
7
8
9
10#ifndef __LINUX_MTD_NAND_H
11#define __LINUX_MTD_NAND_H
12
13#include <linux/mtd/mtd.h>
14
15struct nand_device;
16
17
18
19
20
21
22
23
24
25
26
27
28
29struct 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
56
57
58
59
60
61struct nand_row_converter {
62 unsigned int lun_addr_shift;
63 unsigned int eraseblock_addr_shift;
64};
65
66
67
68
69
70
71
72
73
74
75
76
77struct 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101struct nand_page_io_req {
102 struct nand_pos pos;
103 unsigned int dataoffs;
104 unsigned int datalen;
105 union {
106 const void *out;
107 void *in;
108 } databuf;
109 unsigned int ooboffs;
110 unsigned int ooblen;
111 union {
112 const void *out;
113 void *in;
114 } oobbuf;
115 int mode;
116};
117
118
119
120
121
122
123struct nand_ecc_props {
124 unsigned int strength;
125 unsigned int step_size;
126};
127
128#define NAND_ECCREQ(str, stp) { .strength = (str), .step_size = (stp) }
129
130
131
132
133
134struct nand_bbt {
135 unsigned long *cache;
136};
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154struct nand_ops {
155 int (*erase)(struct nand_device *nand, const struct nand_pos *pos);
156 int (*markbad)(struct nand_device *nand, const struct nand_pos *pos);
157 bool (*isbad)(struct nand_device *nand, const struct nand_pos *pos);
158};
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179struct nand_device {
180 struct mtd_info mtd;
181 struct nand_memory_organization memorg;
182 struct nand_ecc_props eccreq;
183 struct nand_row_converter rowconv;
184 struct nand_bbt bbt;
185 const struct nand_ops *ops;
186};
187
188
189
190
191
192
193
194
195
196
197
198
199struct nand_io_iter {
200 struct nand_page_io_req req;
201 unsigned int oobbytes_per_page;
202 unsigned int dataleft;
203 unsigned int oobleft;
204};
205
206
207
208
209
210
211
212static inline struct nand_device *mtd_to_nanddev(struct mtd_info *mtd)
213{
214 return container_of(mtd, struct nand_device, mtd);
215}
216
217
218
219
220
221
222
223static inline struct mtd_info *nanddev_to_mtd(struct nand_device *nand)
224{
225 return &nand->mtd;
226}
227
228
229
230
231
232
233
234static inline unsigned int nanddev_bits_per_cell(const struct nand_device *nand)
235{
236 return nand->memorg.bits_per_cell;
237}
238
239
240
241
242
243
244
245static inline size_t nanddev_page_size(const struct nand_device *nand)
246{
247 return nand->memorg.pagesize;
248}
249
250
251
252
253
254
255
256static inline unsigned int
257nanddev_per_page_oobsize(const struct nand_device *nand)
258{
259 return nand->memorg.oobsize;
260}
261
262
263
264
265
266
267
268static inline unsigned int
269nanddev_pages_per_eraseblock(const struct nand_device *nand)
270{
271 return nand->memorg.pages_per_eraseblock;
272}
273
274
275
276
277
278
279
280static inline unsigned int
281nanddev_pages_per_target(const struct nand_device *nand)
282{
283 return nand->memorg.pages_per_eraseblock *
284 nand->memorg.eraseblocks_per_lun *
285 nand->memorg.luns_per_target;
286}
287
288
289
290
291
292
293
294static inline size_t nanddev_eraseblock_size(const struct nand_device *nand)
295{
296 return nand->memorg.pagesize * nand->memorg.pages_per_eraseblock;
297}
298
299
300
301
302
303
304
305static inline unsigned int
306nanddev_eraseblocks_per_lun(const struct nand_device *nand)
307{
308 return nand->memorg.eraseblocks_per_lun;
309}
310
311
312
313
314
315
316
317static inline unsigned int
318nanddev_eraseblocks_per_target(const struct nand_device *nand)
319{
320 return nand->memorg.eraseblocks_per_lun * nand->memorg.luns_per_target;
321}
322
323
324
325
326
327
328
329static inline u64 nanddev_target_size(const struct nand_device *nand)
330{
331 return (u64)nand->memorg.luns_per_target *
332 nand->memorg.eraseblocks_per_lun *
333 nand->memorg.pages_per_eraseblock *
334 nand->memorg.pagesize;
335}
336
337
338
339
340
341
342
343static inline unsigned int nanddev_ntargets(const struct nand_device *nand)
344{
345 return nand->memorg.ntargets;
346}
347
348
349
350
351
352
353
354static inline unsigned int nanddev_neraseblocks(const struct nand_device *nand)
355{
356 return nand->memorg.ntargets * nand->memorg.luns_per_target *
357 nand->memorg.eraseblocks_per_lun;
358}
359
360
361
362
363
364
365
366static inline u64 nanddev_size(const struct nand_device *nand)
367{
368 return nanddev_target_size(nand) * nanddev_ntargets(nand);
369}
370
371
372
373
374
375
376
377
378
379
380static inline struct nand_memory_organization *
381nanddev_get_memorg(struct nand_device *nand)
382{
383 return &nand->memorg;
384}
385
386int nanddev_init(struct nand_device *nand, const struct nand_ops *ops,
387 struct module *owner);
388void nanddev_cleanup(struct nand_device *nand);
389
390
391
392
393
394
395
396
397
398
399
400static inline int nanddev_register(struct nand_device *nand)
401{
402 return mtd_device_register(&nand->mtd, NULL, 0);
403}
404
405
406
407
408
409
410
411
412
413
414
415static inline int nanddev_unregister(struct nand_device *nand)
416{
417 return mtd_device_unregister(&nand->mtd);
418}
419
420
421
422
423
424
425
426
427static inline void nanddev_set_of_node(struct nand_device *nand,
428 struct device_node *np)
429{
430 mtd_set_of_node(&nand->mtd, np);
431}
432
433
434
435
436
437
438
439static inline struct device_node *nanddev_get_of_node(struct nand_device *nand)
440{
441 return mtd_get_of_node(&nand->mtd);
442}
443
444
445
446
447
448
449
450
451
452
453
454static inline unsigned int nanddev_offs_to_pos(struct nand_device *nand,
455 loff_t offs,
456 struct nand_pos *pos)
457{
458 unsigned int pageoffs;
459 u64 tmp = offs;
460
461 pageoffs = do_div(tmp, nand->memorg.pagesize);
462 pos->page = do_div(tmp, nand->memorg.pages_per_eraseblock);
463 pos->eraseblock = do_div(tmp, nand->memorg.eraseblocks_per_lun);
464 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
465 pos->lun = do_div(tmp, nand->memorg.luns_per_target);
466 pos->target = tmp;
467
468 return pageoffs;
469}
470
471
472
473
474
475
476
477
478
479
480static inline int nanddev_pos_cmp(const struct nand_pos *a,
481 const struct nand_pos *b)
482{
483 if (a->target != b->target)
484 return a->target < b->target ? -1 : 1;
485
486 if (a->lun != b->lun)
487 return a->lun < b->lun ? -1 : 1;
488
489 if (a->eraseblock != b->eraseblock)
490 return a->eraseblock < b->eraseblock ? -1 : 1;
491
492 if (a->page != b->page)
493 return a->page < b->page ? -1 : 1;
494
495 return 0;
496}
497
498
499
500
501
502
503
504
505
506
507
508
509static inline loff_t nanddev_pos_to_offs(struct nand_device *nand,
510 const struct nand_pos *pos)
511{
512 unsigned int npages;
513
514 npages = pos->page +
515 ((pos->eraseblock +
516 (pos->lun +
517 (pos->target * nand->memorg.luns_per_target)) *
518 nand->memorg.eraseblocks_per_lun) *
519 nand->memorg.pages_per_eraseblock);
520
521 return (loff_t)npages * nand->memorg.pagesize;
522}
523
524
525
526
527
528
529
530
531
532
533
534static inline unsigned int nanddev_pos_to_row(struct nand_device *nand,
535 const struct nand_pos *pos)
536{
537 return (pos->lun << nand->rowconv.lun_addr_shift) |
538 (pos->eraseblock << nand->rowconv.eraseblock_addr_shift) |
539 pos->page;
540}
541
542
543
544
545
546
547
548
549
550static inline void nanddev_pos_next_target(struct nand_device *nand,
551 struct nand_pos *pos)
552{
553 pos->page = 0;
554 pos->plane = 0;
555 pos->eraseblock = 0;
556 pos->lun = 0;
557 pos->target++;
558}
559
560
561
562
563
564
565
566
567
568static inline void nanddev_pos_next_lun(struct nand_device *nand,
569 struct nand_pos *pos)
570{
571 if (pos->lun >= nand->memorg.luns_per_target - 1)
572 return nanddev_pos_next_target(nand, pos);
573
574 pos->lun++;
575 pos->page = 0;
576 pos->plane = 0;
577 pos->eraseblock = 0;
578}
579
580
581
582
583
584
585
586
587
588static inline void nanddev_pos_next_eraseblock(struct nand_device *nand,
589 struct nand_pos *pos)
590{
591 if (pos->eraseblock >= nand->memorg.eraseblocks_per_lun - 1)
592 return nanddev_pos_next_lun(nand, pos);
593
594 pos->eraseblock++;
595 pos->page = 0;
596 pos->plane = pos->eraseblock % nand->memorg.planes_per_lun;
597}
598
599
600
601
602
603
604
605
606
607static inline void nanddev_pos_next_page(struct nand_device *nand,
608 struct nand_pos *pos)
609{
610 if (pos->page >= nand->memorg.pages_per_eraseblock - 1)
611 return nanddev_pos_next_eraseblock(nand, pos);
612
613 pos->page++;
614}
615
616
617
618
619
620
621
622
623
624
625
626static inline void nanddev_io_iter_init(struct nand_device *nand,
627 loff_t offs, struct mtd_oob_ops *req,
628 struct nand_io_iter *iter)
629{
630 struct mtd_info *mtd = nanddev_to_mtd(nand);
631
632 iter->req.mode = req->mode;
633 iter->req.dataoffs = nanddev_offs_to_pos(nand, offs, &iter->req.pos);
634 iter->req.ooboffs = req->ooboffs;
635 iter->oobbytes_per_page = mtd_oobavail(mtd, req);
636 iter->dataleft = req->len;
637 iter->oobleft = req->ooblen;
638 iter->req.databuf.in = req->datbuf;
639 iter->req.datalen = min_t(unsigned int,
640 nand->memorg.pagesize - iter->req.dataoffs,
641 iter->dataleft);
642 iter->req.oobbuf.in = req->oobbuf;
643 iter->req.ooblen = min_t(unsigned int,
644 iter->oobbytes_per_page - iter->req.ooboffs,
645 iter->oobleft);
646}
647
648
649
650
651
652
653
654
655static inline void nanddev_io_iter_next_page(struct nand_device *nand,
656 struct nand_io_iter *iter)
657{
658 nanddev_pos_next_page(nand, &iter->req.pos);
659 iter->dataleft -= iter->req.datalen;
660 iter->req.databuf.in += iter->req.datalen;
661 iter->oobleft -= iter->req.ooblen;
662 iter->req.oobbuf.in += iter->req.ooblen;
663 iter->req.dataoffs = 0;
664 iter->req.ooboffs = 0;
665 iter->req.datalen = min_t(unsigned int, nand->memorg.pagesize,
666 iter->dataleft);
667 iter->req.ooblen = min_t(unsigned int, iter->oobbytes_per_page,
668 iter->oobleft);
669}
670
671
672
673
674
675
676
677
678
679
680
681
682static inline bool nanddev_io_iter_end(struct nand_device *nand,
683 const struct nand_io_iter *iter)
684{
685 if (iter->dataleft || iter->oobleft)
686 return false;
687
688 return true;
689}
690
691
692
693
694
695
696
697
698
699
700
701#define nanddev_io_for_each_page(nand, start, req, iter) \
702 for (nanddev_io_iter_init(nand, start, req, iter); \
703 !nanddev_io_iter_end(nand, iter); \
704 nanddev_io_iter_next_page(nand, iter))
705
706bool nanddev_isbad(struct nand_device *nand, const struct nand_pos *pos);
707bool nanddev_isreserved(struct nand_device *nand, const struct nand_pos *pos);
708int nanddev_erase(struct nand_device *nand, const struct nand_pos *pos);
709int nanddev_markbad(struct nand_device *nand, const struct nand_pos *pos);
710
711
712enum nand_bbt_block_status {
713 NAND_BBT_BLOCK_STATUS_UNKNOWN,
714 NAND_BBT_BLOCK_GOOD,
715 NAND_BBT_BLOCK_WORN,
716 NAND_BBT_BLOCK_RESERVED,
717 NAND_BBT_BLOCK_FACTORY_BAD,
718 NAND_BBT_BLOCK_NUM_STATUS,
719};
720
721int nanddev_bbt_init(struct nand_device *nand);
722void nanddev_bbt_cleanup(struct nand_device *nand);
723int nanddev_bbt_update(struct nand_device *nand);
724int nanddev_bbt_get_block_status(const struct nand_device *nand,
725 unsigned int entry);
726int nanddev_bbt_set_block_status(struct nand_device *nand, unsigned int entry,
727 enum nand_bbt_block_status status);
728int nanddev_bbt_markbad(struct nand_device *nand, unsigned int block);
729
730
731
732
733
734
735
736
737
738
739
740static inline unsigned int nanddev_bbt_pos_to_entry(struct nand_device *nand,
741 const struct nand_pos *pos)
742{
743 return pos->eraseblock +
744 ((pos->lun + (pos->target * nand->memorg.luns_per_target)) *
745 nand->memorg.eraseblocks_per_lun);
746}
747
748
749
750
751
752
753
754static inline bool nanddev_bbt_is_initialized(struct nand_device *nand)
755{
756 return !!nand->bbt.cache;
757}
758
759
760int nanddev_mtd_erase(struct mtd_info *mtd, struct erase_info *einfo);
761int nanddev_mtd_max_bad_blocks(struct mtd_info *mtd, loff_t offs, size_t len);
762
763#endif
764