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