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