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