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