10 #include <asm/arch/regs-base.h>
11 #include <asm/arch/regs-gpmi.h>
12 #include <asm/arch/regs-bch.h>
14 #define FCB_START_BLOCK 0
15 #define NUM_FCB_BLOCKS 1
16 #define MAX_FCB_BLOCKS 32768
18 struct mx28_nand_timing {
33 struct mx28_nand_timing timing;
36 u32 sectors_per_block;
37 u32 number_of_nands; /* not used by ROM code */
38 u32 total_internal_die; /* not used by ROM code */
39 u32 cell_type; /* not used by ROM code */
45 u32 ecc_blocks_per_page;
46 u32 rsrvd[6]; /* not used by ROM code */
57 u32 bb_mark_phys_offset;
60 struct mx28_dbbt_header {
75 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
77 static nand_info_t *mtd = &nand_info[0];
81 #define BIT(v,n) (((v) >> (n)) & 0x1)
83 static u8 calculate_parity_13_8(u8 d)
87 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
88 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
89 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
90 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
91 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
95 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
101 for (i = 0; i < size; i++)
102 ecc[i] = calculate_parity_13_8(src[i]);
105 static u32 calc_chksum(void *buf, size_t size)
111 for (i = 0; i < size; i++) {
118 Physical organisation of data in NAND flash:
120 payload chunk 0 (may be empty)
121 ecc for metadata + payload chunk 0
123 ecc for payload chunk 1
126 ecc for payload chunk n
129 static int calc_bb_offset(nand_info_t *mtd, struct mx28_fcb *fcb)
132 int chunk_data_size = fcb->ecc_blockn_size * 8;
133 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
134 int chunk_total_size = chunk_data_size + chunk_ecc_size;
135 int bb_mark_chunk, bb_mark_chunk_offs;
137 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
138 if (fcb->ecc_block0_size == 0)
139 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
141 bb_mark_chunk = bb_mark_offset / chunk_total_size;
142 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
143 if (bb_mark_chunk_offs > chunk_data_size) {
144 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
148 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
149 return bb_mark_offset;
152 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block,
153 int fw2_start_block, size_t fw_size)
155 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
156 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
160 int bb_mark_bit_offs;
161 struct mx28_fcb *fcb;
164 if (gpmi_base == NULL || bch_base == NULL) {
165 return ERR_PTR(-ENOMEM);
168 fl0 = readl(&bch_base->hw_bch_flash0layout0);
169 fl1 = readl(&bch_base->hw_bch_flash0layout1);
170 t0 = readl(&gpmi_base->hw_gpmi_timing0);
171 t1 = readl(&gpmi_base->hw_gpmi_timing1);
173 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
175 fcb = buf + ALIGN(metadata_size, 4);
176 fcb_offs = (void *)fcb - buf;
178 memset(buf, 0xff, fcb_offs);
179 memset(fcb, 0x00, sizeof(*fcb));
180 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
182 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
183 fcb->version = cpu_to_be32(1);
185 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
186 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
187 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
189 fcb->page_data_size = mtd->writesize;
190 fcb->total_page_size = mtd->writesize + mtd->oobsize;
191 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
193 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
194 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE);
195 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
196 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE);
198 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
199 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
200 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
203 fcb->patch_sectors = 0;
205 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
206 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
208 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
209 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
210 fcb->fw2_sectors = fcb->fw1_sectors;
213 fcb->dbbt_search_area = 1;
215 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
216 if (bb_mark_bit_offs < 0)
217 return ERR_PTR(bb_mark_bit_offs);
218 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
219 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
220 fcb->bb_mark_phys_offset = mtd->writesize;
222 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
226 static int find_fcb(void *ref, int page)
229 struct nand_chip *chip = mtd->priv;
230 void *buf = malloc(mtd->erasesize);
235 chip->select_chip(mtd, 0);
236 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
237 ret = chip->ecc.read_page_raw(mtd, chip, buf, page);
239 printf("Failed to read FCB from page %u: %d\n", page, ret);
242 chip->select_chip(mtd, -1);
243 if (memcmp(buf, ref, mtd->writesize) == 0) {
244 printf("%s: Found FCB in page %u (%08x)\n", __func__,
245 page, page * mtd->writesize);
252 static int write_fcb(void *buf, int block)
255 struct nand_chip *chip = mtd->priv;
256 int page = block * mtd->erasesize / mtd->writesize;
258 ret = find_fcb(buf, page);
260 printf("FCB at block %d is up to date\n", block);
264 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
266 printf("Failed to erase FCB block %u\n", block);
270 printf("Writing FCB to block %d @ %08x\n", block,
271 block * mtd->erasesize);
272 chip->select_chip(mtd, 0);
273 ret = chip->write_page(mtd, chip, buf, page, 0, 1);
275 printf("Failed to write FCB to block %u: %d\n", block, ret);
277 chip->select_chip(mtd, -1);
281 #define chk_overlap(a,b) \
282 ((a##_start_block <= b##_end_block && \
283 a##_end_block >= b##_start_block) || \
284 (b##_start_block <= a##_end_block && \
285 b##_end_block >= a##_start_block))
287 #define fail_if_overlap(a,b,m1,m2) do { \
288 if (chk_overlap(a, b)) { \
289 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
290 m1, a##_start_block, a##_end_block, \
291 m2, b##_start_block, b##_end_block); \
296 #ifndef CONFIG_ENV_OFFSET_REDUND
297 #define TOTAL_ENV_SIZE CONFIG_ENV_SIZE
299 #define TOTAL_ENV_SIZE (CONFIG_ENV_SIZE * 2)
302 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
306 int erase_size = mtd->erasesize;
307 int page_size = mtd->writesize;
313 struct mx28_fcb *fcb;
314 unsigned long fcb_start_block = FCB_START_BLOCK;
315 unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
316 unsigned long fcb_end_block;
317 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
318 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
319 unsigned long env_end_block = env_start_block +
320 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
324 unsigned long fw1_start_block = 0, fw1_end_block;
325 unsigned long fw2_start_block = 0, fw2_end_block;
326 unsigned long fw_num_blocks;
327 unsigned long extra_blocks = 2;
328 nand_erase_options_t erase_opts = { 0, };
331 load_addr = getenv("fileaddr");
332 file_size = getenv("filesize");
334 if (argc < 2 && load_addr == NULL) {
335 printf("Load address not specified\n");
338 if (argc < 3 && file_size == NULL) {
339 printf("Image size not specified\n");
343 for (optind = 1; optind < argc; optind++) {
344 if (strcmp(argv[optind], "-b") == 0) {
345 if (optind >= argc - 1) {
346 printf("Option %s requires an argument\n", argv[optind]);
350 fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
351 if (fcb_start_block >= mtd_num_blocks) {
352 printf("Block number %lu is out of range: 0..%lu\n",
353 fcb_start_block, mtd_num_blocks - 1);
356 } else if (strcmp(argv[optind], "-n") == 0) {
357 if (optind >= argc - 1) {
358 printf("Option %s requires an argument\n", argv[optind]);
362 num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
363 if (num_fcb_blocks > MAX_FCB_BLOCKS) {
364 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
368 } else if (strcmp(argv[optind], "-f") == 0) {
369 if (optind >= argc - 1) {
370 printf("Option %s requires an argument\n", argv[optind]);
374 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
375 if (fw1_start_block >= mtd_num_blocks) {
376 printf("Block number %lu is out of range: 0..%lu\n",
382 } else if (strcmp(argv[optind], "-r") == 0) {
383 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
385 fw2_start_block = simple_strtoul(argv[optind], NULL, 0);
386 if (fw2_start_block >= mtd_num_blocks) {
387 printf("Block number %lu is out of range: 0..%lu\n",
394 } else if (strcmp(argv[optind], "-e") == 0) {
395 if (optind >= argc - 1) {
396 printf("Option %s requires an argument\n", argv[optind]);
400 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
401 if (extra_blocks >= mtd_num_blocks) {
402 printf("Extra block count %lu is out of range: 0..%lu\n",
407 } else if (argv[optind][0] == '-') {
408 printf("Unrecognized option %s\n", argv[optind]);
414 addr = (void *)simple_strtoul(argv[optind], NULL, 0);
419 size = simple_strtoul(argv[optind], NULL, 0);
422 if (load_addr != NULL) {
423 addr = (void *)simple_strtoul(load_addr, NULL, 16);
424 printf("Using default load address %p\n", addr);
426 if (file_size != NULL) {
427 size = simple_strtoul(file_size, NULL, 16);
428 printf("Using default file size %08x\n", size);
430 fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
431 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
434 fw1_start_block = fcb_end_block + 1;
435 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
436 if (chk_overlap(fw1, env)) {
437 fw1_start_block = env_end_block + 1;
438 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
441 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
444 if (fw2_set && fw2_start_block == 0) {
445 fw2_start_block = fw1_end_block + 1;
446 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
447 if (chk_overlap(fw2, env)) {
448 fw2_start_block = env_end_block + 1;
449 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
452 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
455 fail_if_overlap(fcb, env, "FCB", "Environment");
456 fail_if_overlap(fcb, fw1, "FCB", "FW1");
457 fail_if_overlap(fw1, env, "FW1", "Environment");
459 fail_if_overlap(fcb, fw2, "FCB", "FW2");
460 fail_if_overlap(fw2, env, "FW2", "Environment");
461 fail_if_overlap(fw1, fw2, "FW1", "FW2");
464 buf = malloc(erase_size);
466 printf("Failed to allocate buffer\n");
469 /* search for first non-bad block in FW1 block range */
470 while (fw1_start_block <= fw1_end_block) {
471 if (!nand_block_isbad(mtd, fw1_start_block * mtd->erasesize))
475 if (fw1_end_block - fw1_start_block + 1 < fw_num_blocks) {
476 printf("Too many bad blocks in FW1 block range: %lu..%lu\n",
477 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
482 /* search for first non-bad block in FW2 block range */
483 while (fw2_set && fw2_start_block <= fw2_end_block) {
484 if (!nand_block_isbad(mtd, fw2_start_block * mtd->erasesize))
488 if (fw2_end_block - fw2_start_block + 1 < fw_num_blocks) {
489 printf("Too many bad blocks in FW2 area %08lx..%08lx\n",
490 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
495 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
496 (fw_num_blocks + extra_blocks) * mtd->erasesize);
498 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
501 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
503 for (block = fcb_start_block; block < fcb_start_block + num_fcb_blocks;
505 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
506 if (block == fcb_start_block)
510 ret = write_fcb(buf, block);
512 printf("Failed to write FCB to block %u\n", block);
519 printf("Could not write FCB to flash\n");
523 printf("Programming U-Boot image from %p to block %lu\n",
524 addr, fw1_start_block);
525 if (size & (page_size - 1)) {
526 memset(addr + size, 0xff, size & (page_size - 1));
527 size = ALIGN(size, page_size);
530 erase_opts.offset = fcb->fw1_start_page * page_size;
531 erase_opts.length = ALIGN(size, erase_size) +
532 extra_blocks * mtd->erasesize;
533 erase_opts.quiet = 1;
535 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
536 erase_opts.offset + erase_opts.length - 1);
538 ret = nand_erase_opts(mtd, &erase_opts);
540 printf("Failed to erase flash: %d\n", ret);
543 printf("Programming flash @ %08x..%08x from %p\n",
544 fcb->fw1_start_page * page_size,
545 fcb->fw1_start_page * page_size + size, addr);
546 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
547 &size, addr, WITH_DROP_FFS);
549 printf("Failed to program flash: %d\n", ret);
552 if (fw2_start_block == 0) {
556 printf("Programming redundant U-Boot image to block %lu\n",
558 erase_opts.offset = fcb->fw2_start_page * page_size;
559 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
560 erase_opts.offset + erase_opts.length - 1);
562 ret = nand_erase_opts(mtd, &erase_opts);
564 printf("Failed to erase flash: %d\n", ret);
567 printf("Programming flash @ %08x..%08x from %p\n",
568 fcb->fw2_start_page * page_size,
569 fcb->fw2_start_page * page_size + size, addr);
570 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
571 &size, addr, WITH_DROP_FFS);
573 printf("Failed to program flash: %d\n", ret);
579 U_BOOT_CMD(romupdate, 11, 0, do_update,
580 "Creates an FCB data structure and writes an U-Boot image to flash\n",
581 "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
582 "\t-b #\tfirst FCB block number (default 0)\n"
583 "\t-n #\ttotal number of FCB blocks (default 1)\n"
584 "\t-f #\twrite bootloader image at block #\n"
585 "\t-r\twrite redundant bootloader image at next free block after first image\n"
586 "\t-r #\twrite redundant bootloader image at block #\n"
587 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
588 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
589 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"