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