hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/*
 * (C) Copyright 2019 Rockchip Electronics Co., Ltd
 *
 * SPDX-License-Identifier:    GPL-2.0+
 */
 
#include <common.h>
#include <blk.h>
#include <boot_rkimg.h>
#include <dm.h>
#include <errno.h>
#include <image.h>
#include <linux/log2.h>
#include <malloc.h>
#include <nand.h>
#include <part.h>
#include <spi.h>
#include <dm/device-internal.h>
#include <linux/mtd/spi-nor.h>
#ifdef CONFIG_NAND
#include <linux/mtd/nand.h>
#endif
 
// #define MTD_BLK_VERBOSE
 
#define MTD_PART_NAND_HEAD        "mtdparts="
#define MTD_PART_INFO_MAX_SIZE        512
#define MTD_SINGLE_PART_INFO_MAX_SIZE    40
 
#define MTD_BLK_TABLE_BLOCK_UNKNOWN    (-2)
#define MTD_BLK_TABLE_BLOCK_SHIFT    (-1)
 
static int *mtd_map_blk_table;
 
int mtd_blk_map_table_init(struct blk_desc *desc,
              loff_t offset,
              size_t length)
{
   u32 blk_total, blk_begin, blk_cnt;
   struct mtd_info *mtd = NULL;
   int i, j;
 
   if (!desc)
       return -ENODEV;
 
   switch (desc->devnum) {
   case BLK_MTD_NAND:
   case BLK_MTD_SPI_NAND:
       mtd = desc->bdev->priv;
       break;
   default:
       break;
   }
 
   if (!mtd) {
       return -ENODEV;
   } else {
       blk_total = (mtd->size + mtd->erasesize - 1) >> mtd->erasesize_shift;
       if (!mtd_map_blk_table) {
           mtd_map_blk_table = (int *)malloc(blk_total * sizeof(int));
           if (!mtd_map_blk_table)
               return -ENOMEM;
           for (i = 0; i < blk_total; i++)
               mtd_map_blk_table[i] = MTD_BLK_TABLE_BLOCK_UNKNOWN;
       }
 
       blk_begin = (u32)offset >> mtd->erasesize_shift;
       blk_cnt = ((u32)((offset & mtd->erasesize_mask) + length + \
           mtd->erasesize - 1) >> mtd->erasesize_shift);
       if (blk_begin >= blk_total) {
           pr_err("map table blk begin[%d] overflow\n", blk_begin);
           return -EINVAL;
       }
       if ((blk_begin + blk_cnt) > blk_total)
           blk_cnt = blk_total - blk_begin;
 
       if (mtd_map_blk_table[blk_begin] != MTD_BLK_TABLE_BLOCK_UNKNOWN)
           return 0;
 
       j = 0;
        /* should not across blk_cnt */
       for (i = 0; i < blk_cnt; i++) {
           if (j >= blk_cnt)
               mtd_map_blk_table[blk_begin + i] = MTD_BLK_TABLE_BLOCK_SHIFT;
           for (; j < blk_cnt; j++) {
               if (!mtd_block_isbad(mtd, (blk_begin + j) << mtd->erasesize_shift)) {
                   mtd_map_blk_table[blk_begin + i] = blk_begin + j;
                   j++;
                   if (j == blk_cnt)
                       j++;
                   break;
               }
           }
       }
 
       return 0;
   }
}
 
static bool get_mtd_blk_map_address(struct mtd_info *mtd, loff_t *off)
{
   bool mapped;
   loff_t offset = *off;
   size_t block_offset = offset & (mtd->erasesize - 1);
 
   mapped = false;
   if (!mtd_map_blk_table ||
       mtd_map_blk_table[(u64)offset >> mtd->erasesize_shift] ==
       MTD_BLK_TABLE_BLOCK_UNKNOWN ||
       mtd_map_blk_table[(u64)offset >> mtd->erasesize_shift] ==
       0xffffffff)
       return mapped;
 
   mapped = true;
   *off = (loff_t)(((u32)mtd_map_blk_table[(u64)offset >>
       mtd->erasesize_shift] << mtd->erasesize_shift) + block_offset);
 
   return mapped;
}
 
void mtd_blk_map_partitions(struct blk_desc *desc)
{
   disk_partition_t info;
   int i, ret;
 
   if (!desc)
       return;
 
   if (desc->if_type != IF_TYPE_MTD)
       return;
 
   for (i = 1; i < MAX_SEARCH_PARTITIONS; i++) {
       ret = part_get_info(desc, i, &info);
       if (ret != 0)
           break;
 
       if (mtd_blk_map_table_init(desc,
                      info.start << 9,
                      info.size << 9)) {
           pr_debug("mtd block map table fail\n");
       }
   }
}
 
void mtd_blk_map_fit(struct blk_desc *desc, ulong sector, void *fit)
{
   struct mtd_info *mtd = NULL;
   int totalsize = 0;
 
   if (desc->if_type != IF_TYPE_MTD)
       return;
 
   if (desc->devnum == BLK_MTD_NAND) {
#if defined(CONFIG_NAND)
       mtd = dev_get_priv(desc->bdev->parent);
#endif
   } else if (desc->devnum == BLK_MTD_SPI_NAND) {
#if defined(CONFIG_MTD_SPI_NAND)
       mtd = desc->bdev->priv;
#endif
   }
 
#ifdef CONFIG_SPL_FIT
   if (fit_get_totalsize(fit, &totalsize))
       debug("Can not find /totalsize node.\n");
#endif
   if (mtd && totalsize) {
       if (mtd_blk_map_table_init(desc, sector << 9, totalsize + (size_t)mtd->erasesize))
           debug("Map block table fail.\n");
   }
}
 
static __maybe_unused int mtd_map_read(struct mtd_info *mtd, loff_t offset,
                      size_t *length, size_t *actual,
                      loff_t lim, u_char *buffer)
{
   size_t left_to_read = *length;
   u_char *p_buffer = buffer;
   int rval;
 
   while (left_to_read > 0) {
       size_t block_offset = offset & (mtd->erasesize - 1);
       size_t read_length;
       loff_t mapped_offset;
 
       if (offset >= mtd->size)
           return 0;
 
       mapped_offset = offset;
       if (!get_mtd_blk_map_address(mtd, &mapped_offset)) {
           if (mtd_block_isbad(mtd, mapped_offset &
                       ~(mtd->erasesize - 1))) {
               printf("Skipping bad block 0x%08llx\n",
                      offset & ~(mtd->erasesize - 1));
               offset += mtd->erasesize - block_offset;
               continue;
           }
       }
 
       if (left_to_read < (mtd->erasesize - block_offset))
           read_length = left_to_read;
       else
           read_length = mtd->erasesize - block_offset;
 
       rval = mtd_read(mtd, mapped_offset, read_length, &read_length,
               p_buffer);
       if (rval && rval != -EUCLEAN) {
           printf("NAND read from offset %x failed %d\n",
                  (u32)offset, rval);
           *length -= left_to_read;
           return rval;
       }
 
       left_to_read -= read_length;
       offset       += read_length;
       p_buffer     += read_length;
   }
 
   return 0;
}
 
static __maybe_unused int mtd_map_write(struct mtd_info *mtd, loff_t offset,
                   size_t *length, size_t *actual,
                   loff_t lim, u_char *buffer, int flags)
{
   int rval = 0, blocksize;
   size_t left_to_write = *length;
   u_char *p_buffer = buffer;
   struct erase_info ei;
 
   blocksize = mtd->erasesize;
 
   /*
    * nand_write() handles unaligned, partial page writes.
    *
    * We allow length to be unaligned, for convenience in
    * using the $filesize variable.
    *
    * However, starting at an unaligned offset makes the
    * semantics of bad block skipping ambiguous (really,
    * you should only start a block skipping access at a
    * partition boundary).  So don't try to handle that.
    */
   if ((offset & (mtd->writesize - 1)) != 0) {
       printf("Attempt to write non page-aligned data\n");
       *length = 0;
       return -EINVAL;
   }
 
   while (left_to_write > 0) {
       size_t block_offset = offset & (mtd->erasesize - 1);
       size_t write_size, truncated_write_size;
       loff_t mapped_offset;
 
       if (offset >= mtd->size)
           return 0;
 
       mapped_offset = offset;
       if (!get_mtd_blk_map_address(mtd, &mapped_offset)) {
           if (mtd_block_isbad(mtd, mapped_offset &
                       ~(mtd->erasesize - 1))) {
               printf("Skipping bad block 0x%08llx\n",
                      offset & ~(mtd->erasesize - 1));
               offset += mtd->erasesize - block_offset;
               continue;
           }
       }
 
       if (!(mapped_offset & mtd->erasesize_mask)) {
           memset(&ei, 0, sizeof(struct erase_info));
           ei.addr = mapped_offset;
           ei.len  = mtd->erasesize;
           rval = mtd_erase(mtd, &ei);
           if (rval) {
               pr_info("error %d while erasing %llx\n", rval,
                   mapped_offset);
               return rval;
           }
       }
 
       if (left_to_write < (blocksize - block_offset))
           write_size = left_to_write;
       else
           write_size = blocksize - block_offset;
 
       truncated_write_size = write_size;
       rval = mtd_write(mtd, mapped_offset, truncated_write_size,
                (size_t *)(&truncated_write_size), p_buffer);
 
       offset += write_size;
       p_buffer += write_size;
 
       if (rval != 0) {
           printf("NAND write to offset %llx failed %d\n",
                  offset, rval);
           *length -= left_to_write;
           return rval;
       }
 
       left_to_write -= write_size;
   }
 
   return 0;
}
 
static __maybe_unused int mtd_map_erase(struct mtd_info *mtd, loff_t offset,
                   size_t length)
{
   struct erase_info ei;
   loff_t pos, len;
   int ret;
 
   pos = offset;
   len = length;
 
   if ((pos & mtd->erasesize_mask) || (len & mtd->erasesize_mask)) {
       pr_err("Attempt to erase non block-aligned data, pos= %llx, len= %llx\n",
              pos, len);
 
       return -EINVAL;
   }
 
   while (len) {
       loff_t mapped_offset;
 
       mapped_offset = pos;
       if (!get_mtd_blk_map_address(mtd, &mapped_offset)) {
           if (mtd_block_isbad(mtd, pos) || mtd_block_isreserved(mtd, pos)) {
               pr_debug("attempt to erase a bad/reserved block @%llx\n",
                    pos);
               pos += mtd->erasesize;
               continue;
           }
       }
 
       memset(&ei, 0, sizeof(struct erase_info));
       ei.addr = mapped_offset;
       ei.len  = mtd->erasesize;
       ret = mtd_erase(mtd, &ei);
       if (ret) {
           pr_err("map_erase error %d while erasing %llx\n", ret,
                  pos);
           return ret;
       }
 
       pos += mtd->erasesize;
       len -= mtd->erasesize;
   }
 
   return 0;
}
 
char *mtd_part_parse(struct blk_desc *dev_desc)
{
   char mtd_part_info_temp[MTD_SINGLE_PART_INFO_MAX_SIZE] = {0};
   u32 length, data_len = MTD_PART_INFO_MAX_SIZE;
   disk_partition_t info;
   char *mtd_part_info_p;
   struct mtd_info *mtd;
   char *mtd_part_info;
   int ret;
   int p;
 
#ifndef CONFIG_SPL_BUILD
   dev_desc = rockchip_get_bootdev();
#endif
   if (!dev_desc)
       return NULL;
 
   mtd = (struct mtd_info *)dev_desc->bdev->priv;
   if (!mtd)
       return NULL;
 
   mtd_part_info = (char *)calloc(MTD_PART_INFO_MAX_SIZE, sizeof(char));
   if (!mtd_part_info) {
       printf("%s: Fail to malloc!", __func__);
       return NULL;
   }
 
   mtd_part_info_p = mtd_part_info;
   snprintf(mtd_part_info_p, data_len - 1, "%s%s:",
        MTD_PART_NAND_HEAD,
        dev_desc->product);
   data_len -= strlen(mtd_part_info_p);
   mtd_part_info_p = mtd_part_info_p + strlen(mtd_part_info_p);
 
   for (p = 1; p < MAX_SEARCH_PARTITIONS; p++) {
       ret = part_get_info(dev_desc, p, &info);
       if (ret)
           break;
 
       debug("name is %s, start addr is %x\n", info.name,
             (int)(size_t)info.start);
 
       snprintf(mtd_part_info_p, data_len - 1, "0x%x@0x%x(%s)",
            (int)(size_t)info.size << 9,
            (int)(size_t)info.start << 9,
            info.name);
       snprintf(mtd_part_info_temp, MTD_SINGLE_PART_INFO_MAX_SIZE - 1,
            "0x%x@0x%x(%s)",
            (int)(size_t)info.size << 9,
            (int)(size_t)info.start << 9,
            info.name);
       strcat(mtd_part_info, ",");
       if (part_get_info(dev_desc, p + 1, &info)) {
           /* Partition with grow tag in parameter will be resized */
           if ((info.size + info.start + 64) >= dev_desc->lba) {
               if (dev_desc->devnum == BLK_MTD_SPI_NOR) {
                   /* Nor is 64KB erase block(kernel) and gpt table just
                    * resserve 33 sectors for the last partition. This
                    * will erase the backup gpt table by user program,
                    * so reserve one block.
                    */
                   snprintf(mtd_part_info_p, data_len - 1, "0x%x@0x%x(%s)",
                        (int)(size_t)(info.size -
                        (info.size - 1) %
                        (0x10000 >> 9) - 1) << 9,
                        (int)(size_t)info.start << 9,
                        info.name);
                   break;
               } else {
                   /* Nand flash is erased by block and gpt table just
                    * resserve 33 sectors for the last partition. This
                    * will erase the backup gpt table by user program,
                    * so reserve one block.
                    */
                   snprintf(mtd_part_info_p, data_len - 1, "0x%x@0x%x(%s)",
                        (int)(size_t)(info.size -
                        (info.size - 1) %
                        (mtd->erasesize >> 9) - 1) << 9,
                        (int)(size_t)info.start << 9,
                        info.name);
                   break;
               }
           } else {
               snprintf(mtd_part_info_temp, MTD_SINGLE_PART_INFO_MAX_SIZE - 1,
                    "0x%x@0x%x(%s)",
                    (int)(size_t)info.size << 9,
                    (int)(size_t)info.start << 9,
                    info.name);
               break;
           }
       }
       length = strlen(mtd_part_info_temp);
       data_len -= length;
       mtd_part_info_p = mtd_part_info_p + length + 1;
       memset(mtd_part_info_temp, 0, MTD_SINGLE_PART_INFO_MAX_SIZE);
   }
 
   return mtd_part_info;
}
 
ulong mtd_dread(struct udevice *udev, lbaint_t start,
       lbaint_t blkcnt, void *dst)
{
   struct blk_desc *desc = dev_get_uclass_platdata(udev);
#if defined(CONFIG_NAND) || defined(CONFIG_MTD_SPI_NAND) || defined(CONFIG_SPI_FLASH_MTD)
   loff_t off = (loff_t)(start * 512);
   size_t rwsize = blkcnt * 512;
#endif
   struct mtd_info *mtd;
   int ret = 0;
#ifdef MTD_BLK_VERBOSE
   ulong us = 1;
#endif
 
   if (!desc)
       return ret;
 
   mtd = desc->bdev->priv;
   if (!mtd)
       return 0;
 
   if (blkcnt == 0)
       return 0;
 
#ifdef MTD_BLK_VERBOSE
   us = get_ticks();
#endif
   if (desc->devnum == BLK_MTD_NAND) {
       ret = mtd_map_read(mtd, off, &rwsize,
                  NULL, mtd->size,
                  (u_char *)(dst));
       if (!ret)
           ret = blkcnt;
   } else if (desc->devnum == BLK_MTD_SPI_NAND) {
       ret = mtd_map_read(mtd, off, &rwsize,
                  NULL, mtd->size,
                  (u_char *)(dst));
       if (!ret)
           ret = blkcnt;
   } else if (desc->devnum == BLK_MTD_SPI_NOR) {
#if defined(CONFIG_SPI_FLASH_MTD) || defined(CONFIG_SPL_BUILD)
       struct spi_nor *nor = (struct spi_nor *)mtd->priv;
       struct spi_slave *spi = nor->spi;
       size_t retlen_nor;
 
       if (desc->op_flag == BLK_PRE_RW)
           spi->mode |= SPI_DMA_PREPARE;
       ret = mtd_read(mtd, off, rwsize, &retlen_nor, dst);
       if (desc->op_flag == BLK_PRE_RW)
           spi->mode &= ~SPI_DMA_PREPARE;
 
       if (retlen_nor == rwsize)
           ret = blkcnt;
#endif
   }
#ifdef MTD_BLK_VERBOSE
   us = (get_ticks() - us) / 24UL;
   pr_err("mtd dread %s %lx %lx cost %ldus: %ldMB/s\n\n", mtd->name, start, blkcnt, us, (blkcnt / 2) / ((us + 999) / 1000));
#else
   pr_debug("mtd dread %s %lx %lx\n\n", mtd->name, start, blkcnt);
#endif
 
   return ret;
}
 
#if CONFIG_IS_ENABLED(MTD_WRITE)
ulong mtd_dwrite(struct udevice *udev, lbaint_t start,
        lbaint_t blkcnt, const void *src)
{
   struct blk_desc *desc = dev_get_uclass_platdata(udev);
#if defined(CONFIG_NAND) || defined(CONFIG_MTD_SPI_NAND) || defined(CONFIG_SPI_FLASH_MTD)
   loff_t off = (loff_t)(start * 512);
   size_t rwsize = blkcnt * 512;
#endif
   struct mtd_info *mtd;
   int ret = 0;
 
   if (!desc)
       return ret;
 
   mtd = desc->bdev->priv;
   if (!mtd)
       return 0;
 
   pr_debug("mtd dwrite %s %lx %lx\n", mtd->name, start, blkcnt);
 
   if (blkcnt == 0)
       return 0;
 
   if (desc->devnum == BLK_MTD_NAND ||
       desc->devnum == BLK_MTD_SPI_NAND ||
       desc->devnum == BLK_MTD_SPI_NOR) {
       if (desc->op_flag == BLK_MTD_CONT_WRITE) {
           ret = mtd_map_write(mtd, off, &rwsize,
                       NULL, mtd->size,
                       (u_char *)(src), 0);
           if (!ret)
               return blkcnt;
           else
               return 0;
       } else {
           lbaint_t off_aligned, alinged;
           size_t rwsize_aligned;
           u8 *p_buf;
 
           alinged = off & mtd->erasesize_mask;
           off_aligned = off - alinged;
           rwsize_aligned = rwsize + alinged;
           rwsize_aligned = (rwsize_aligned + mtd->erasesize - 1) &
               ~(mtd->erasesize - 1);
 
           p_buf = malloc(rwsize_aligned);
           if (!p_buf) {
               printf("%s: Fail to malloc!", __func__);
               return 0;
           }
 
           ret = mtd_map_read(mtd, off_aligned, &rwsize_aligned,
                      NULL, mtd->size,
                      (u_char *)(p_buf));
           if (ret) {
               free(p_buf);
               return 0;
           }
 
           memcpy(p_buf + alinged, src, rwsize);
 
           ret = mtd_map_write(mtd, off_aligned, &rwsize_aligned,
                       NULL, mtd->size,
                       (u_char *)(p_buf), 0);
           free(p_buf);
           if (!ret)
               return blkcnt;
           else
               return 0;
       }
   } else {
       return 0;
   }
 
   return 0;
}
 
ulong mtd_derase(struct udevice *udev, lbaint_t start,
        lbaint_t blkcnt)
{
   struct blk_desc *desc = dev_get_uclass_platdata(udev);
#if defined(CONFIG_NAND) || defined(CONFIG_MTD_SPI_NAND) || defined(CONFIG_SPI_FLASH_MTD)
   loff_t off = (loff_t)(start * 512);
   size_t len = blkcnt * 512;
#endif
   struct mtd_info *mtd;
   int ret = 0;
 
   if (!desc)
       return ret;
 
   mtd = desc->bdev->priv;
   if (!mtd)
       return 0;
 
   pr_debug("mtd derase %s %lx %lx\n", mtd->name, start, blkcnt);
 
   if (blkcnt == 0)
       return 0;
 
   if (desc->devnum == BLK_MTD_NAND ||
       desc->devnum == BLK_MTD_SPI_NAND ||
       desc->devnum == BLK_MTD_SPI_NOR) {
       ret = mtd_map_erase(mtd, off, len);
       if (ret)
           return ret;
   } else {
       return 0;
   }
 
   return blkcnt;
}
#endif
 
static int mtd_blk_probe(struct udevice *udev)
{
   struct mtd_info *mtd;
   struct blk_desc *desc = dev_get_uclass_platdata(udev);
   int ret, i = 0;
 
   mtd = dev_get_uclass_priv(udev->parent);
   if (mtd->type == MTD_NANDFLASH && desc->devnum == BLK_MTD_NAND) {
#ifndef CONFIG_SPL_BUILD
       mtd = dev_get_priv(udev->parent);
#endif
   }
 
   /* Fill mtd devices information */
   if (is_power_of_2(mtd->erasesize))
       mtd->erasesize_shift = ffs(mtd->erasesize) - 1;
   else
       mtd->erasesize_shift = 0;
 
   if (is_power_of_2(mtd->writesize))
       mtd->writesize_shift = ffs(mtd->writesize) - 1;
   else
       mtd->writesize_shift = 0;
 
   mtd->erasesize_mask = (1 << mtd->erasesize_shift) - 1;
   mtd->writesize_mask = (1 << mtd->writesize_shift) - 1;
 
   desc->bdev->priv = mtd;
   sprintf(desc->vendor, "0x%.4x", 0x2207);
   if (strncmp(mtd->name, "nand", 4) == 0)
       memcpy(desc->product, "rk-nand", strlen("rk-nand"));
   else
       memcpy(desc->product, mtd->name, strlen(mtd->name));
   memcpy(desc->revision, "V1.00", sizeof("V1.00"));
   if (mtd->type == MTD_NANDFLASH) {
#ifdef CONFIG_NAND
       if (desc->devnum == BLK_MTD_NAND)
           i = NAND_BBT_SCAN_MAXBLOCKS;
       else if (desc->devnum == BLK_MTD_SPI_NAND)
           i = NANDDEV_BBT_SCAN_MAXBLOCKS;
#endif
 
       /*
        * Find the first useful block in the end,
        * and it is the end lba of the nand storage.
        */
       for (; i < (mtd->size / mtd->erasesize); i++) {
           ret =  mtd_block_isbad(mtd,
                          mtd->size - mtd->erasesize * (i + 1));
           if (!ret) {
               desc->lba = (mtd->size >> 9) -
                   (mtd->erasesize >> 9) * i;
               break;
           }
       }
   } else {
       desc->lba = mtd->size >> 9;
   }
 
   debug("MTD: desc->lba is %lx\n", desc->lba);
 
   return 0;
}
 
static const struct blk_ops mtd_blk_ops = {
   .read    = mtd_dread,
#if CONFIG_IS_ENABLED(MTD_WRITE)
   .write    = mtd_dwrite,
   .erase    = mtd_derase,
#endif
};
 
U_BOOT_DRIVER(mtd_blk) = {
   .name        = "mtd_blk",
   .id        = UCLASS_BLK,
   .ops        = &mtd_blk_ops,
   .probe        = mtd_blk_probe,
};