]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx6/flash.c
karo: tx6 remove bogus '\n' from cmd descriptions
[karo-tx-uboot.git] / board / karo / tx6 / flash.c
1 #include <common.h>
2 #include <malloc.h>
3 #include <nand.h>
4 #include <errno.h>
5
6 #include <linux/err.h>
7
8 #include <asm/io.h>
9 #include <asm/sizes.h>
10 #include <asm/arch/imx-regs.h>
11 #include <asm/imx-common/regs-gpmi.h>
12 #include <asm/imx-common/regs-bch.h>
13
14 #if CONFIG_SYS_NAND_U_BOOT_OFFS < 0x20000
15 #error CONFIG_SYS_NAND_U_BOOT_OFFS must be >= 128kIB
16 #endif
17
18 struct mx6_nand_timing {
19         u8 data_setup;
20         u8 data_hold;
21         u8 address_setup;
22         u8 dsample_time;
23         u8 nand_timing_state;
24         u8 tREA;
25         u8 tRLOH;
26         u8 tRHOH;
27 };
28
29 struct mx6_fcb {
30         u32 checksum;
31         u32 fingerprint;
32         u32 version;
33         struct mx6_nand_timing timing;
34         u32 page_data_size;
35         u32 total_page_size;
36         u32 sectors_per_block;
37         u32 number_of_nands;    /* not used by ROM code */
38         u32 total_internal_die; /* not used by ROM code */
39         u32 cell_type;          /* not used by ROM code */
40         u32 ecc_blockn_type;
41         u32 ecc_block0_size;
42         u32 ecc_blockn_size;
43         u32 ecc_block0_type;
44         u32 metadata_size;
45         u32 ecc_blocks_per_page;
46         u32 rsrvd1[6];          /* not used by ROM code */
47         u32 bch_mode;           /* erase_threshold */
48         u32 rsrvd2[2];
49         u32 fw1_start_page;
50         u32 fw2_start_page;
51         u32 fw1_sectors;
52         u32 fw2_sectors;
53         u32 dbbt_search_area;
54         u32 bb_mark_byte;
55         u32 bb_mark_startbit;
56         u32 bb_mark_phys_offset;
57         u32 bch_type;
58         u32 rsrvd3[8]; /* Toggle NAND timing parameters */
59         u32 disbbm;
60         u32 bb_mark_spare_offset;
61         u32 rsrvd4[9]; /* ONFI NAND parameters */
62         u32 disbb_search;
63 };
64
65 struct mx6_dbbt_header {
66         u32 checksum;
67         u32 fingerprint;
68         u32 version;
69         u32 number_bb;
70         u32 number_pages;
71         u8 spare[492];
72 };
73
74 struct mx6_dbbt {
75         u32 nand_number;
76         u32 number_bb;
77         u32 bb_num[2040 / 4];
78 };
79
80 #define BF_VAL(v, bf)           (((v) & bf##_MASK) >> bf##_OFFSET)
81
82 static nand_info_t *mtd = &nand_info[0];
83
84 extern void *_start;
85
86 #define BIT(v,n)        (((v) >> (n)) & 0x1)
87
88 static inline void memdump(const void *addr, size_t len)
89 {
90         const char *buf = addr;
91         int i;
92
93         for (i = 0; i < len; i++) {
94                 if (i % 16 == 0) {
95                         if (i > 0)
96                                 printf("\n");
97                         printf("%p:", &buf[i]);
98                 }
99                 printf(" %02x", buf[i]);
100         }
101         printf("\n");
102 }
103
104 static u8 calculate_parity_13_8(u8 d)
105 {
106         u8 p = 0;
107
108         p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2))             << 0;
109         p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
110         p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
111         p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0))             << 3;
112         p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
113         return p;
114 }
115
116 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
117 {
118         int i;
119         u8 *src = _src;
120         u8 *ecc = _ecc;
121
122         for (i = 0; i < size; i++)
123                 ecc[i] = calculate_parity_13_8(src[i]);
124 }
125
126 static u32 calc_chksum(void *buf, size_t size)
127 {
128         u32 chksum = 0;
129         u8 *bp = buf;
130         size_t i;
131
132         for (i = 0; i < size; i++) {
133                 chksum += bp[i];
134         }
135         return ~chksum;
136 }
137
138 /*
139   Physical organisation of data in NAND flash:
140   metadata
141   payload chunk 0 (may be empty)
142   ecc for metadata + payload chunk 0
143   payload chunk 1
144   ecc for payload chunk 1
145 ...
146   payload chunk n
147   ecc for payload chunk n
148  */
149
150 static int calc_bb_offset(nand_info_t *mtd, struct mx6_fcb *fcb)
151 {
152         int bb_mark_offset;
153         int chunk_data_size = fcb->ecc_blockn_size * 8;
154         int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
155         int chunk_total_size = chunk_data_size + chunk_ecc_size;
156         int bb_mark_chunk, bb_mark_chunk_offs;
157
158         bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
159         if (fcb->ecc_block0_size == 0)
160                 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
161
162         bb_mark_chunk = bb_mark_offset / chunk_total_size;
163         bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
164         if (bb_mark_chunk_offs > chunk_data_size) {
165                 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
166                         bb_mark_chunk_offs);
167                 return -EINVAL;
168         }
169         bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
170         return bb_mark_offset;
171 }
172
173 #define pr_fcb_val(p, n)        debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
174
175 static struct mx6_fcb *create_fcb(void *buf, int fw1_start_block,
176                                 int fw2_start_block, size_t fw_size)
177 {
178         struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
179         struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
180         u32 fl0, fl1;
181         u32 t0;
182         int metadata_size;
183         int bb_mark_bit_offs;
184         struct mx6_fcb *fcb;
185         int fcb_offs;
186
187         if (gpmi_base == NULL || bch_base == NULL) {
188                 return ERR_PTR(-ENOMEM);
189         }
190
191         fl0 = readl(&bch_base->hw_bch_flash0layout0);
192         fl1 = readl(&bch_base->hw_bch_flash0layout1);
193         t0 = readl(&gpmi_base->hw_gpmi_timing0);
194
195         metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
196
197         fcb = buf + ALIGN(metadata_size, 4);
198         fcb_offs = (void *)fcb - buf;
199
200         memset(buf, 0xff, fcb_offs);
201         memset(fcb, 0x00, sizeof(*fcb));
202         memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
203
204         strncpy((char *)&fcb->fingerprint, "FCB ", 4);
205         fcb->version = cpu_to_be32(1);
206
207         fcb->disbb_search = 1;
208         fcb->disbbm = 1;
209
210         /* ROM code assumes GPMI clock of 25 MHz */
211         fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
212         fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
213         fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
214
215         fcb->page_data_size = mtd->writesize;
216         fcb->total_page_size = mtd->writesize + mtd->oobsize;
217         fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
218
219         fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
220         fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE) * 4;
221         fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
222         fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE) * 4;
223
224         pr_fcb_val(fcb, ecc_block0_type);
225         pr_fcb_val(fcb, ecc_blockn_type);
226         pr_fcb_val(fcb, ecc_block0_size);
227         pr_fcb_val(fcb, ecc_blockn_size);
228
229         fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
230         fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
231         fcb->bch_mode = readl(&bch_base->hw_bch_mode);
232         fcb->bch_type = 0; /* BCH20 */
233
234         fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
235         fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
236
237         if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
238                 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
239                 fcb->fw2_sectors = fcb->fw1_sectors;
240         }
241
242         fcb->dbbt_search_area = 1;
243
244         bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
245         if (bb_mark_bit_offs < 0)
246                 return ERR_PTR(bb_mark_bit_offs);
247         fcb->bb_mark_byte = bb_mark_bit_offs / 8;
248         fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
249         fcb->bb_mark_phys_offset = mtd->writesize;
250
251         pr_fcb_val(fcb, bb_mark_byte);
252         pr_fcb_val(fcb, bb_mark_startbit);
253         pr_fcb_val(fcb, bb_mark_phys_offset);
254
255         fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
256         return fcb;
257 }
258
259 static int find_fcb(void *ref, int page)
260 {
261         int ret = 0;
262         struct nand_chip *chip = mtd->priv;
263         void *buf = malloc(mtd->erasesize);
264
265         if (buf == NULL) {
266                 return -ENOMEM;
267         }
268         chip->select_chip(mtd, 0);
269         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
270         ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
271         if (ret) {
272                 printf("Failed to read FCB from page %u: %d\n", page, ret);
273                 return ret;
274         }
275         chip->select_chip(mtd, -1);
276         if (memcmp(buf, ref, mtd->writesize) == 0) {
277                 debug("Found FCB in page %u (%08x)\n",
278                         page, page * mtd->writesize);
279                 ret = 1;
280         }
281         free(buf);
282         return ret;
283 }
284
285 static int write_fcb(void *buf, int block)
286 {
287         int ret;
288         struct nand_chip *chip = mtd->priv;
289         int page = block * mtd->erasesize / mtd->writesize;
290
291         ret = find_fcb(buf, page);
292         if (ret > 0) {
293                 printf("FCB at block %d is up to date\n", block);
294                 return 0;
295         }
296
297         ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
298         if (ret) {
299                 printf("Failed to erase FCB block %u\n", block);
300                 return ret;
301         }
302
303         printf("Writing FCB to block %d @ %08x\n", block,
304                 block * mtd->erasesize);
305         chip->select_chip(mtd, 0);
306         ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
307         if (ret) {
308                 printf("Failed to write FCB to block %u: %d\n", block, ret);
309         }
310         chip->select_chip(mtd, -1);
311         return ret;
312 }
313
314 struct mx6_ivt {
315         u32 magic;
316         u32 entry;
317         u32 rsrvd1;
318         void *dcd;
319         void *boot_data;
320         void *self;
321         void *csf;
322         u32 rsrvd2;
323 };
324
325 struct mx6_boot_data {
326         u32 start;
327         u32 length;
328         u32 plugin;
329 };
330
331 static size_t count_good_blocks(int start, int end)
332 {
333         size_t max_len = (end - start + 1);
334         int block;
335
336         for (block = start; block <= end; block++) {
337                 if (nand_block_isbad(mtd, block * mtd->erasesize))
338                         max_len--;
339         }
340         return max_len;
341 }
342
343 static int find_ivt(void *buf)
344 {
345         struct mx6_ivt *ivt_hdr = buf + 0x400;
346
347         if ((ivt_hdr->magic & 0xff0000ff) != 0x400000d1)
348                 return 0;
349
350         return 1;
351 }
352
353 static inline void *reloc(void *dst, void *base, void *ptr)
354 {
355         return dst + (ptr - base);
356 }
357
358 static int patch_ivt(void *buf, size_t fsize)
359 {
360         struct mx6_ivt *ivt_hdr = buf + 0x400;
361         struct mx6_boot_data *boot_data;
362
363         if (!find_ivt(buf)) {
364                 printf("No IVT found in image at %p\n", buf);
365                 return -EINVAL;
366         }
367         boot_data = reloc(ivt_hdr, ivt_hdr->self, ivt_hdr->boot_data);
368         boot_data->length = fsize;
369
370         return 0;
371 }
372
373 #define chk_overlap(a,b)                                \
374         ((a##_start_block <= b##_end_block &&           \
375                 a##_end_block >= b##_start_block) ||    \
376         (b##_start_block <= a##_end_block &&            \
377                 b##_end_block >= a##_start_block))
378
379 #define fail_if_overlap(a,b,m1,m2) do {                         \
380         if (chk_overlap(a, b)) {                                \
381                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
382                         m1, a##_start_block, a##_end_block,     \
383                         m2, b##_start_block, b##_end_block);    \
384                 return -EINVAL;                                 \
385         }                                                       \
386 } while (0)
387
388 #ifdef CONFIG_ENV_IS_IN_NAND
389 #ifndef CONFIG_ENV_OFFSET_REDUND
390 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
391 #else
392 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
393 #endif
394 #endif
395
396 #define pr_fcb_offset(n)        printf("%s: %04x (%d)\n", #n, \
397                 offsetof(struct mx6_fcb, n), offsetof(struct mx6_fcb, n))
398
399 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
400 {
401         int ret;
402         const unsigned long fcb_start_block = 0, fcb_end_block = 0;
403         int erase_size = mtd->erasesize;
404         int page_size = mtd->writesize;
405         void *buf;
406         char *load_addr;
407         char *file_size;
408         size_t size = 0;
409         void *addr = NULL;
410         struct mx6_fcb *fcb;
411         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
412 #ifdef CONFIG_ENV_IS_IN_NAND
413         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
414         unsigned long env_end_block = env_start_block +
415                 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
416 #endif
417         int optind;
418         int fw1_set = 0;
419         int fw2_set = 0;
420         unsigned long fw1_start_block = 0, fw1_end_block;
421         unsigned long fw2_start_block = 0, fw2_end_block;
422         unsigned long fw_num_blocks;
423         unsigned long extra_blocks = 2;
424         nand_erase_options_t erase_opts = { 0, };
425         size_t max_len1, max_len2;
426         size_t actual;
427
428         for (optind = 1; optind < argc; optind++) {
429                 if (strcmp(argv[optind], "-f") == 0) {
430                         if (optind >= argc - 1) {
431                                 printf("Option %s requires an argument\n",
432                                         argv[optind]);
433                                 return -EINVAL;
434                         }
435                         optind++;
436                         fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
437                         if (fw1_start_block >= mtd_num_blocks) {
438                                 printf("Block number %lu is out of range: 0..%lu\n",
439                                         fw1_start_block, mtd_num_blocks - 1);
440                                 return -EINVAL;
441                         }
442                         fw1_set = 1;
443                 } else if (strcmp(argv[optind], "-r") == 0) {
444                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
445                                 optind++;
446                                 fw2_start_block = simple_strtoul(argv[optind],
447                                                                 NULL, 0);
448                                 if (fw2_start_block >= mtd_num_blocks) {
449                                         printf("Block number %lu is out of range: 0..%lu\n",
450                                                 fw2_start_block,
451                                                 mtd_num_blocks - 1);
452                                         return -EINVAL;
453                                 }
454                         }
455                         fw2_set = 1;
456                 } else if (strcmp(argv[optind], "-e") == 0) {
457                         if (optind >= argc - 1) {
458                                 printf("Option %s requires an argument\n",
459                                         argv[optind]);
460                                 return -EINVAL;
461                         }
462                         optind++;
463                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
464                         if (extra_blocks >= mtd_num_blocks) {
465                                 printf("Extra block count %lu is out of range: 0..%lu\n",
466                                         extra_blocks,
467                                         mtd_num_blocks - 1);
468                                 return -EINVAL;
469                         }
470                 } else if (argv[optind][0] == '-') {
471                         printf("Unrecognized option %s\n", argv[optind]);
472                         return -EINVAL;
473                 } else {
474                         break;
475                 }
476         }
477
478         load_addr = getenv("fileaddr");
479         file_size = getenv("filesize");
480
481         if (argc - optind < 1 && load_addr == NULL) {
482                 printf("Load address not specified\n");
483                 return -EINVAL;
484         }
485         if (argc - optind < 2 && file_size == NULL) {
486                 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
487         }
488         if (argc > optind) {
489                 load_addr = NULL;
490                 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
491                 optind++;
492         }
493         if (argc > optind) {
494                 file_size = NULL;
495                 size = simple_strtoul(argv[optind], NULL, 16);
496                 optind++;
497         }
498         if (load_addr != NULL) {
499                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
500                 printf("Using default load address %p\n", addr);
501         }
502         if (file_size != NULL) {
503                 size = simple_strtoul(file_size, NULL, 16);
504                 printf("Using default file size %08x\n", size);
505         }
506         if (size > 0)
507                 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
508         else
509                 fw_num_blocks = CONFIG_U_BOOT_IMG_SIZE / mtd->erasesize - extra_blocks;
510
511         if (!fw1_set) {
512                 fw1_start_block = CONFIG_SYS_NAND_U_BOOT_OFFS / mtd->erasesize;
513                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
514         } else {
515                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
516         }
517
518         if (fw2_set && fw2_start_block == 0) {
519                 fw2_start_block = fw1_end_block + 1;
520                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
521         } else {
522                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
523         }
524
525 #ifdef CONFIG_ENV_IS_IN_NAND
526         fail_if_overlap(fcb, env, "FCB", "Environment");
527         fail_if_overlap(fw1, env, "FW1", "Environment");
528 #endif
529         fail_if_overlap(fcb, fw1, "FCB", "FW1");
530         if (fw2_set) {
531                 fail_if_overlap(fcb, fw2, "FCB", "FW2");
532 #ifdef CONFIG_ENV_IS_IN_NAND
533                 fail_if_overlap(fw2, env, "FW2", "Environment");
534 #endif
535                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
536         }
537
538         buf = malloc(erase_size);
539         if (buf == NULL) {
540                 printf("Failed to allocate buffer\n");
541                 return -ENOMEM;
542         }
543
544         /* search for bad blocks in FW1 block range */
545         max_len1 = count_good_blocks(fw1_start_block, fw1_end_block);
546         printf("%u good blocks in %lu..%lu\n",
547                 max_len1, fw1_start_block, fw1_end_block);
548         if (fw_num_blocks > max_len1) {
549                 printf("Too many bad blocks in FW1 block range: %lu..%lu; max blocks: %u\n",
550                         fw1_end_block + 1 - fw_num_blocks - extra_blocks,
551                         fw1_end_block, max_len1);
552                 return -EINVAL;
553         }
554
555         /* search for bad blocks in FW2 block range */
556         max_len2 = count_good_blocks(fw2_start_block, fw2_end_block);
557         if (fw2_start_block > 0 && fw_num_blocks > max_len2) {
558                 printf("Too many bad blocks in FW2 block range: %lu..%lu\n",
559                         fw2_end_block + 1 - fw_num_blocks - extra_blocks,
560                         fw2_end_block);
561                 return -EINVAL;
562         }
563
564         fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
565                         ALIGN(fw_num_blocks * mtd->erasesize, mtd->writesize));
566         if (IS_ERR(fcb)) {
567                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
568                 return PTR_ERR(fcb);
569         }
570         encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
571
572         ret = write_fcb(buf, fcb_start_block);
573         if (ret) {
574                 printf("Failed to write FCB to block %lu\n", fcb_start_block);
575                 return ret;
576         }
577
578         ret = patch_ivt(addr, size ?: fw_num_blocks * mtd->erasesize);
579         if (ret) {
580                 return ret;
581         }
582
583         printf("Programming U-Boot image from %p to block %lu\n",
584                 addr, fw1_start_block);
585         if (size & (page_size - 1)) {
586                 memset(addr + size, 0xff, size & (page_size - 1));
587                 size = ALIGN(size, page_size);
588         }
589
590         erase_opts.offset = fcb->fw1_start_page * page_size;
591         erase_opts.length = (fw1_end_block - fw1_start_block + 1) *
592                 mtd->erasesize;
593         erase_opts.quiet = 1;
594
595         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
596                 erase_opts.offset + erase_opts.length - 1);
597
598         ret = nand_erase_opts(mtd, &erase_opts);
599         if (ret) {
600                 printf("Failed to erase flash: %d\n", ret);
601                 return ret;
602         }
603         if (size == 0)
604                 max_len1 *= mtd->erasesize;
605         else
606                 max_len1 = size;
607
608         printf("Programming flash @ %08x..%08x from %p\n",
609                 fcb->fw1_start_page * page_size,
610                 fcb->fw1_start_page * page_size + max_len1 - 1, addr);
611         ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
612                                 &max_len1, &actual, erase_opts.length, addr,
613                                 WITH_DROP_FFS);
614         if (ret || actual < size) {
615                 printf("Failed to program flash: %d\n", ret);
616                 return ret ?: -EIO;
617         }
618         if (fw2_start_block == 0) {
619                 return ret;
620         }
621
622         printf("Programming redundant U-Boot image to block %lu\n",
623                 fw2_start_block);
624         erase_opts.offset = fcb->fw2_start_page * page_size;
625         erase_opts.length = (fw2_end_block - fw2_start_block + 1) *
626                 mtd->erasesize;
627         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
628                 erase_opts.offset + erase_opts.length - 1);
629
630         ret = nand_erase_opts(mtd, &erase_opts);
631         if (ret) {
632                 printf("Failed to erase flash: %d\n", ret);
633                 return ret;
634         }
635         if (size == 0)
636                 max_len2 *= mtd->erasesize;
637         else
638                 max_len2 = size;
639         printf("Programming flash @ %08x..%08x from %p\n",
640                 fcb->fw2_start_page * page_size,
641                 fcb->fw2_start_page * page_size + max_len2 - 1, addr);
642         ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
643                                 &max_len2, &actual, erase_opts.length, addr,
644                                 WITH_DROP_FFS);
645         if (ret || actual < size) {
646                 printf("Failed to program flash: %d\n", ret);
647                 return ret ?: -EIO;
648         }
649         return ret;
650 }
651
652 U_BOOT_CMD(romupdate, 11, 0, do_update,
653         "Creates an FCB data structure and writes an U-Boot image to flash",
654         "[-f #] [-r [#]] [-e #] [<address>] [<length>]\n"
655         "\t-f #\twrite bootloader image at block #\n"
656         "\t-r\twrite redundant bootloader image at next free block after first image\n"
657         "\t-r #\twrite redundant bootloader image at block #\n"
658         "\t-e #\tspecify number of redundant blocks per boot loader image (default 2)\n"
659         "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
660         "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"
661         );