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