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