]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx28/flash.c
Merge remote-tracking branch 'remotes/origin/tx28-bugfix'
[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->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
239         fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
240         pr_fcb_val(fcb, fw1_start_page);
241         pr_fcb_val(fcb, fw1_sectors);
242
243         if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
244                 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
245                 fcb->fw2_sectors = fcb->fw1_sectors;
246                 pr_fcb_val(fcb, fw2_start_page);
247                 pr_fcb_val(fcb, fw2_sectors);
248         }
249
250         fcb->dbbt_search_area = 1;
251
252         bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
253         if (bb_mark_bit_offs < 0)
254                 return ERR_PTR(bb_mark_bit_offs);
255         fcb->bb_mark_byte = bb_mark_bit_offs / 8;
256         fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
257         fcb->bb_mark_phys_offset = mtd->writesize;
258
259         pr_fcb_val(fcb, bb_mark_byte);
260         pr_fcb_val(fcb, bb_mark_startbit);
261         pr_fcb_val(fcb, bb_mark_phys_offset);
262
263         fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
264         return fcb;
265 }
266
267 static int find_fcb(void *ref, int page)
268 {
269         int ret = 0;
270         struct nand_chip *chip = mtd->priv;
271         void *buf = malloc(mtd->erasesize);
272
273         if (buf == NULL) {
274                 return -ENOMEM;
275         }
276         chip->select_chip(mtd, 0);
277         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
278         ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
279         if (ret) {
280                 printf("Failed to read FCB from page %u: %d\n", page, ret);
281                 goto out;
282         }
283         if (memcmp(buf, ref, mtd->writesize) == 0) {
284                 debug("Found FCB in page %u (%08x)\n",
285                         page, page * mtd->writesize);
286                 ret = 1;
287         }
288 out:
289         chip->select_chip(mtd, -1);
290         free(buf);
291         return ret;
292 }
293
294 static int write_fcb(void *buf, int block)
295 {
296         int ret;
297         struct nand_chip *chip = mtd->priv;
298         int page = block * mtd->erasesize / mtd->writesize;
299
300         ret = find_fcb(buf, page);
301         if (ret > 0) {
302                 printf("FCB at block %d is up to date\n", block);
303                 return 0;
304         }
305
306         ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
307         if (ret) {
308                 printf("Failed to erase FCB block %u\n", block);
309                 return ret;
310         }
311
312         printf("Writing FCB to block %d @ %08llx\n", block,
313                 (u64)block * mtd->erasesize);
314         chip->select_chip(mtd, 0);
315         ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
316         if (ret) {
317                 printf("Failed to write FCB to block %u: %d\n", block, ret);
318         }
319         chip->select_chip(mtd, -1);
320         return ret;
321 }
322
323 #define chk_overlap(a,b)                                \
324         ((a##_start_block <= b##_end_block &&           \
325                 a##_end_block >= b##_start_block) ||    \
326         (b##_start_block <= a##_end_block &&            \
327                 b##_end_block >= a##_start_block))
328
329 #define fail_if_overlap(a,b,m1,m2) do {                         \
330         if (chk_overlap(a, b)) {                                \
331                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
332                         m1, a##_start_block, a##_end_block,     \
333                         m2, b##_start_block, b##_end_block);    \
334                 return -EINVAL;                                 \
335         }                                                       \
336 } while (0)
337
338 static int tx28_prog_uboot(void *addr, int start_block, int skip,
339                         size_t size, size_t max_len)
340 {
341         int ret;
342         nand_erase_options_t erase_opts = { 0, };
343         size_t actual;
344         size_t prg_length = max_len - skip * mtd->erasesize;
345         int prg_start = (start_block + skip) * mtd->erasesize;
346
347         erase_opts.offset = start_block * mtd->erasesize;
348         erase_opts.length = max_len;
349         erase_opts.quiet = 1;
350
351         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
352                 erase_opts.offset + erase_opts.length - 1);
353         ret = nand_erase_opts(mtd, &erase_opts);
354         if (ret) {
355                 printf("Failed to erase flash: %d\n", ret);
356                 return ret;
357         }
358
359         printf("Programming flash @ %08llx..%08llx from %p\n",
360                 (u64)start_block * mtd->erasesize,
361                 (u64)start_block * mtd->erasesize + size - 1, addr);
362         actual = size;
363         ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
364                                 prg_length, addr, WITH_DROP_FFS);
365         if (ret) {
366                 printf("Failed to program flash: %d\n", ret);
367                 return ret;
368         }
369         if (actual < size) {
370                 printf("Could only write %u of %u bytes\n", actual, size);
371                 return -EIO;
372         }
373         return 0;
374 }
375
376 #ifdef CONFIG_ENV_IS_IN_NAND
377 #ifndef CONFIG_ENV_OFFSET_REDUND
378 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
379 #else
380 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
381 #endif
382 #endif
383
384 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
385 {
386         int ret;
387         const unsigned long fcb_start_block = 0, fcb_end_block = 0;
388         int erase_size = mtd->erasesize;
389         int page_size = mtd->writesize;
390         void *buf;
391         char *load_addr;
392         char *file_size;
393         size_t size = 0;
394         void *addr = NULL;
395         struct mx28_fcb *fcb;
396         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
397 #ifdef CONFIG_ENV_IS_IN_NAND
398         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
399         unsigned long env_end_block = env_start_block +
400                 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
401 #endif
402         int optind;
403         int fw2_set = 0;
404         unsigned long fw1_start_block = 0, fw1_end_block;
405         unsigned long fw2_start_block = 0, fw2_end_block;
406         unsigned long fw_num_blocks;
407         int fw1_skip, fw2_skip;
408         unsigned long extra_blocks = 0;
409         size_t max_len1, max_len2;
410         struct mtd_device *dev;
411         struct part_info *part_info;
412         struct part_info *redund_part_info;
413         const char *uboot_part = "u-boot";
414         const char *redund_part = NULL;
415         u8 part_num;
416         u8 redund_part_num;
417
418         ret = mtdparts_init();
419         if (ret)
420                 return ret;
421
422         for (optind = 1; optind < argc; optind++) {
423                 char *endp;
424
425                 if (strcmp(argv[optind], "-f") == 0) {
426                         if (optind >= argc - 1) {
427                                 printf("Option %s requires an argument\n",
428                                         argv[optind]);
429                                 return -EINVAL;
430                         }
431                         optind++;
432                         fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
433                         if (*endp != '\0') {
434                                 uboot_part = argv[optind];
435                                 continue;
436                         }
437                         uboot_part = NULL;
438                         if (fw1_start_block >= mtd_num_blocks) {
439                                 printf("Block number %lu is out of range: 0..%lu\n",
440                                         fw1_start_block, mtd_num_blocks - 1);
441                                 return -EINVAL;
442                         }
443                 } else if (strcmp(argv[optind], "-r") == 0) {
444                         fw2_set = 1;
445                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
446                                 optind++;
447                                 fw2_start_block = simple_strtoul(argv[optind],
448                                                                 &endp, 0);
449                                 if (*endp != '\0') {
450                                         redund_part = argv[optind];
451                                         continue;
452                                 }
453                                 if (fw2_start_block >= mtd_num_blocks) {
454                                         printf("Block number %lu is out of range: 0..%lu\n",
455                                                 fw2_start_block,
456                                                 mtd_num_blocks - 1);
457                                         return -EINVAL;
458                                 }
459                         }
460                 } else if (strcmp(argv[optind], "-e") == 0) {
461                         if (optind >= argc - 1) {
462                                 printf("Option %s requires an argument\n",
463                                         argv[optind]);
464                                 return -EINVAL;
465                         }
466                         optind++;
467                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
468                         if (extra_blocks >= mtd_num_blocks) {
469                                 printf("Extra block count %lu is out of range: 0..%lu\n",
470                                         extra_blocks,
471                                         mtd_num_blocks - 1);
472                                 return -EINVAL;
473                         }
474                 } else if (argv[optind][0] == '-') {
475                         printf("Unrecognized option %s\n", argv[optind]);
476                         return -EINVAL;
477                 } else {
478                         break;
479                 }
480         }
481
482         load_addr = getenv("fileaddr");
483         file_size = getenv("filesize");
484
485         if (argc - optind < 1 && load_addr == NULL) {
486                 printf("Load address not specified\n");
487                 return -EINVAL;
488         }
489         if (argc - optind < 2 && file_size == NULL) {
490                 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
491         }
492         if (argc > optind) {
493                 load_addr = NULL;
494                 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
495                 optind++;
496         }
497         if (argc > optind) {
498                 file_size = NULL;
499                 size = simple_strtoul(argv[optind], NULL, 16);
500                 optind++;
501         }
502         if (load_addr != NULL) {
503                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
504                 printf("Using default load address %p\n", addr);
505         }
506         if (file_size != NULL) {
507                 size = simple_strtoul(file_size, NULL, 16);
508                 printf("Using default file size %08x\n", size);
509         }
510         if (size > 0) {
511                 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
512         } else {
513                 fw_num_blocks = part_info->size / mtd->erasesize -
514                         extra_blocks;
515                 size = fw_num_blocks * mtd->erasesize;
516         }
517
518         if (uboot_part) {
519                 ret = find_dev_and_part(uboot_part, &dev, &part_num,
520                                         &part_info);
521                 if (ret) {
522                         printf("Failed to find '%s' partition: %d\n",
523                                 uboot_part, ret);
524                         return ret;
525                 }
526                 fw1_start_block = part_info->offset / mtd->erasesize;
527                 max_len1 = part_info->size;
528         } else {
529                 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
530         }
531
532         if (redund_part) {
533                 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
534                                         &redund_part_info);
535                 if (ret) {
536                         printf("Failed to find '%s' partition: %d\n",
537                                 redund_part, ret);
538                         return ret;
539                 }
540                 fw2_start_block = redund_part_info->offset / mtd->erasesize;
541                 max_len2 = redund_part_info->size;
542         } else if (fw2_set) {
543                 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
544         } else {
545                 max_len2 = 0;
546         }
547
548         fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
549                                 max_len1 / mtd->erasesize);
550         if (fw1_skip < 0) {
551                 printf("Could not find %lu contiguous good blocks for fw image\n",
552                         fw_num_blocks);
553                 if (uboot_part) {
554 #ifdef CONFIG_ENV_IS_IN_NAND
555                         if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
556                                 printf("Use a different partition\n");
557                         } else {
558                                 printf("Increase the size of the '%s' partition\n",
559                                         uboot_part);
560                         }
561 #else
562                         printf("Increase the size of the '%s' partition\n",
563                                 uboot_part);
564 #endif
565                 } else {
566                         printf("Increase the number of spare blocks to use with the '-e' option\n");
567                 }
568                 return -ENOSPC;
569         }
570         fw1_end_block = fw1_start_block + fw1_skip + fw_num_blocks - 1;
571
572         if (fw2_set && fw2_start_block == 0)
573                 fw2_start_block = fw1_end_block + 1;
574         if (fw2_start_block > 0) {
575                 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
576                                         max_len2 / mtd->erasesize);
577                 if (fw2_skip < 0) {
578                         printf("Could not find %lu contiguous good blocks for redundant fw image\n",
579                                 fw_num_blocks);
580                         if (redund_part) {
581                                 printf("Increase the size of the '%s' partition or use a different partition\n",
582                                         redund_part);
583                         } else {
584                                 printf("Increase the number of spare blocks to use with the '-e' option\n");
585                         }
586                         return -ENOSPC;
587                 }
588         } else {
589                 fw2_skip = 0;
590         }
591         fw2_end_block = fw2_start_block + fw2_skip + fw_num_blocks - 1;
592
593 #ifdef CONFIG_ENV_IS_IN_NAND
594         fail_if_overlap(fcb, env, "FCB", "Environment");
595         fail_if_overlap(fw1, env, "FW1", "Environment");
596 #endif
597         fail_if_overlap(fcb, fw1, "FCB", "FW1");
598         if (fw2_set) {
599                 fail_if_overlap(fcb, fw2, "FCB", "FW2");
600 #ifdef CONFIG_ENV_IS_IN_NAND
601                 fail_if_overlap(fw2, env, "FW2", "Environment");
602 #endif
603                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
604         }
605
606         buf = malloc(erase_size);
607         if (buf == NULL) {
608                 printf("Failed to allocate buffer\n");
609                 return -ENOMEM;
610         }
611
612         fcb = create_fcb(buf, fw1_start_block + fw1_skip,
613                         fw2_start_block + fw2_skip, fw_num_blocks);
614         if (IS_ERR(fcb)) {
615                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
616                 free(buf);
617                 return PTR_ERR(fcb);
618         }
619         encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
620
621         ret = write_fcb(buf, fcb_start_block);
622         free(buf);
623         if (ret) {
624                 printf("Failed to write FCB to block %lu\n", fcb_start_block);
625                 return ret;
626         }
627
628         if (size & (page_size - 1)) {
629                 memset(addr + size, 0xff, size & (page_size - 1));
630                 size = ALIGN(size, page_size);
631         }
632
633         printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
634                 addr, fw1_start_block + fw1_skip,
635                 (u64)(fw1_start_block + fw1_skip) * mtd->erasesize);
636         ret = tx28_prog_uboot(addr, fw1_start_block, fw1_skip, size,
637                         max_len1);
638
639         if (fw2_start_block == 0) {
640                 return ret;
641         }
642
643         printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
644                 fw2_start_block + fw2_skip,
645                 (u64)(fw2_start_block + fw2_skip) * mtd->erasesize);
646         ret = tx28_prog_uboot(addr, fw2_start_block, fw2_skip, fw_num_blocks,
647                         max_len2);
648         return ret;
649 }
650
651 U_BOOT_CMD(romupdate, 11, 0, do_update,
652         "Creates an FCB data structure and writes an U-Boot image to flash",
653         "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
654         "\t-f <part>\twrite bootloader image to partition <part>\n"
655         "\t-f #\twrite bootloader image at block # (decimal)\n"
656         "\t-r\twrite redundant bootloader image at next free block after first image\n"
657         "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
658         "\t-r #\twrite redundant bootloader image at block # (decimal)\n"
659         "\t-e #\tspecify number of redundant blocks per boot loader image\n"
660         "\t\tonly valid if -f or -r specify a flash address rather than a partition name\n"
661         "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
662         "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"
663         );