2 * Copyright (C) 2011-2014 Lothar Waßmann <LW@KARO-electronics.de>
4 * See file CREDITS for list of people who contributed to this
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.
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.
23 #include <linux/err.h>
24 #include <jffs2/load_kernel.h>
27 #include <asm/sizes.h>
28 #include <asm/arch/regs-base.h>
29 #include <asm/imx-common/regs-gpmi.h>
30 #include <asm/imx-common/regs-bch.h>
32 struct mx28_nand_timing {
47 struct mx28_nand_timing timing;
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 */
59 u32 ecc_blocks_per_page;
60 u32 rsrvd[6]; /* not used by ROM code */
71 u32 bb_mark_phys_offset;
74 #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
76 static nand_info_t *mtd = &nand_info[0];
80 #define BIT(v,n) (((v) >> (n)) & 0x1)
82 static u8 calculate_parity_13_8(u8 d)
86 p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2)) << 0;
87 p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
88 p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
89 p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0)) << 3;
90 p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
94 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
100 for (i = 0; i < size; i++)
101 ecc[i] = calculate_parity_13_8(src[i]);
104 static u32 calc_chksum(void *buf, size_t size)
110 for (i = 0; i < size; i++) {
117 Physical organisation of data in NAND flash:
119 payload chunk 0 (may be empty)
120 ecc for metadata + payload chunk 0
122 ecc for payload chunk 1
125 ecc for payload chunk n
128 static int calc_bb_offset(nand_info_t *mtd, struct mx28_fcb *fcb)
131 int chunk_data_size = fcb->ecc_blockn_size * 8;
132 int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
133 int chunk_total_size = chunk_data_size + chunk_ecc_size;
134 int bb_mark_chunk, bb_mark_chunk_offs;
136 bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
137 if (fcb->ecc_block0_size == 0)
138 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
140 bb_mark_chunk = bb_mark_offset / chunk_total_size;
141 bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
142 if (bb_mark_chunk_offs > chunk_data_size) {
143 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
147 bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
148 return bb_mark_offset;
152 * return number of blocks to skip for a contiguous partition
155 static int find_contig_space(int block, int num_blocks, int max_blocks)
159 int last = block + max_blocks;
161 debug("Searching %u contiguous blocks from %d..%d\n",
162 num_blocks, block, block + max_blocks - 1);
163 for (; block < last; block++) {
164 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
167 debug("Skipping %u blocks to %u\n",
171 if (found >= num_blocks) {
172 debug("Found %u good blocks from %d..%d\n",
173 found, block - found + 1, block);
181 #define pr_fcb_val(p, n) debug("%s=%08x(%d)\n", #n, (p)->n, (p)->n)
183 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block,
184 int fw2_start_block, int fw_num_blocks)
186 struct gpmi_regs *gpmi_base = (void *)GPMI_BASE_ADDRESS;
187 struct bch_regs *bch_base = (void *)BCH_BASE_ADDRESS;
191 int bb_mark_bit_offs;
192 struct mx28_fcb *fcb;
195 if (gpmi_base == NULL || bch_base == NULL) {
196 return ERR_PTR(-ENOMEM);
199 fl0 = readl(&bch_base->hw_bch_flash0layout0);
200 fl1 = readl(&bch_base->hw_bch_flash0layout1);
201 t0 = readl(&gpmi_base->hw_gpmi_timing0);
203 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
205 fcb = buf + ALIGN(metadata_size, 4);
206 fcb_offs = (void *)fcb - buf;
208 memset(buf, 0xff, fcb_offs);
209 memset(fcb, 0x00, sizeof(*fcb));
210 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
212 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
213 fcb->version = cpu_to_be32(1);
215 /* ROM code assumes GPMI clock of 25 MHz */
216 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP) * 40;
217 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD) * 40;
218 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP) * 40;
220 fcb->page_data_size = mtd->writesize;
221 fcb->total_page_size = mtd->writesize + mtd->oobsize;
222 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
224 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
225 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE);
226 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
227 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE);
229 pr_fcb_val(fcb, ecc_block0_type);
230 pr_fcb_val(fcb, ecc_blockn_type);
231 pr_fcb_val(fcb, ecc_block0_size);
232 pr_fcb_val(fcb, ecc_blockn_size);
234 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
235 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
236 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
239 fcb->patch_sectors = 0;
241 fcb->fw1_start_page = fw1_start_block * fcb->sectors_per_block;
242 fcb->fw1_sectors = fw_num_blocks * fcb->sectors_per_block;
243 pr_fcb_val(fcb, fw1_start_page);
244 pr_fcb_val(fcb, fw1_sectors);
246 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
247 fcb->fw2_start_page = fw2_start_block * fcb->sectors_per_block;
248 fcb->fw2_sectors = fcb->fw1_sectors;
249 pr_fcb_val(fcb, fw2_start_page);
250 pr_fcb_val(fcb, fw2_sectors);
253 fcb->dbbt_search_area = 1;
255 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
256 if (bb_mark_bit_offs < 0)
257 return ERR_PTR(bb_mark_bit_offs);
258 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
259 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
260 fcb->bb_mark_phys_offset = mtd->writesize;
262 pr_fcb_val(fcb, bb_mark_byte);
263 pr_fcb_val(fcb, bb_mark_startbit);
264 pr_fcb_val(fcb, bb_mark_phys_offset);
266 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
270 static int find_fcb(void *ref, int page)
273 struct nand_chip *chip = mtd->priv;
274 void *buf = malloc(mtd->erasesize);
279 chip->select_chip(mtd, 0);
280 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
281 ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
283 printf("Failed to read FCB from page %u: %d\n", page, ret);
286 if (memcmp(buf, ref, mtd->writesize) == 0) {
287 debug("Found FCB in page %u (%08x)\n",
288 page, page * mtd->writesize);
292 chip->select_chip(mtd, -1);
297 static int write_fcb(void *buf, int block)
300 struct nand_chip *chip = mtd->priv;
301 int page = block * mtd->erasesize / mtd->writesize;
303 ret = find_fcb(buf, page);
305 printf("FCB at block %d is up to date\n", block);
309 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
311 printf("Failed to erase FCB block %u\n", block);
315 printf("Writing FCB to block %d @ %08llx\n", block,
316 (u64)block * mtd->erasesize);
317 chip->select_chip(mtd, 0);
318 ret = chip->write_page(mtd, chip, buf, 1, page, 0, 1);
320 printf("Failed to write FCB to block %u: %d\n", block, ret);
322 chip->select_chip(mtd, -1);
326 #define chk_overlap(a,b) \
327 ((a##_start_block <= b##_end_block && \
328 a##_end_block >= b##_start_block) || \
329 (b##_start_block <= a##_end_block && \
330 b##_end_block >= a##_start_block))
332 #define fail_if_overlap(a,b,m1,m2) do { \
333 if (chk_overlap(a, b)) { \
334 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
335 m1, a##_start_block, a##_end_block, \
336 m2, b##_start_block, b##_end_block); \
341 static int tx28_prog_uboot(void *addr, int start_block, int skip,
342 size_t size, size_t max_len)
345 nand_erase_options_t erase_opts = { 0, };
348 erase_opts.offset = start_block * mtd->erasesize;
349 erase_opts.length = max_len;
350 erase_opts.quiet = 1;
352 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
353 erase_opts.offset + erase_opts.length - 1);
354 ret = nand_erase_opts(mtd, &erase_opts);
356 printf("Failed to erase flash: %d\n", ret);
360 printf("Programming flash @ %08llx..%08llx from %p\n",
361 (u64)start_block * mtd->erasesize,
362 (u64)start_block * mtd->erasesize + size - 1, addr);
364 ret = nand_write_skip_bad(mtd, start_block * mtd->erasesize,
365 &actual, NULL, erase_opts.length, addr,
368 printf("Failed to program flash: %d\n", ret);
372 printf("Could only write %u of %u bytes\n", actual, size);
378 #ifdef CONFIG_ENV_IS_IN_NAND
379 #ifndef CONFIG_ENV_OFFSET_REDUND
380 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
382 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
386 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
389 const unsigned long fcb_start_block = 0, fcb_end_block = 0;
390 int erase_size = mtd->erasesize;
391 int page_size = mtd->writesize;
397 struct mx28_fcb *fcb;
398 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
399 #ifdef CONFIG_ENV_IS_IN_NAND
400 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
401 unsigned long env_end_block = env_start_block +
402 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
406 unsigned long fw1_start_block = 0, fw1_end_block;
407 unsigned long fw2_start_block = 0, fw2_end_block;
408 unsigned long fw_num_blocks;
409 int fw1_skip, fw2_skip;
410 unsigned long extra_blocks = 0;
411 size_t max_len1, max_len2;
412 struct mtd_device *dev;
413 struct part_info *part_info;
414 struct part_info *redund_part_info;
415 const char *uboot_part = "u-boot";
416 const char *redund_part = NULL;
420 ret = mtdparts_init();
424 for (optind = 1; optind < argc; optind++) {
427 if (strcmp(argv[optind], "-f") == 0) {
428 if (optind >= argc - 1) {
429 printf("Option %s requires an argument\n",
434 fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
436 uboot_part = argv[optind];
440 if (fw1_start_block >= mtd_num_blocks) {
441 printf("Block number %lu is out of range: 0..%lu\n",
442 fw1_start_block, mtd_num_blocks - 1);
445 } else if (strcmp(argv[optind], "-r") == 0) {
447 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
449 fw2_start_block = simple_strtoul(argv[optind],
452 redund_part = argv[optind];
455 if (fw2_start_block >= mtd_num_blocks) {
456 printf("Block number %lu is out of range: 0..%lu\n",
462 } else if (strcmp(argv[optind], "-e") == 0) {
463 if (optind >= argc - 1) {
464 printf("Option %s requires an argument\n",
469 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
470 if (extra_blocks >= mtd_num_blocks) {
471 printf("Extra block count %lu is out of range: 0..%lu\n",
476 } else if (argv[optind][0] == '-') {
477 printf("Unrecognized option %s\n", argv[optind]);
484 load_addr = getenv("fileaddr");
485 file_size = getenv("filesize");
487 if (argc - optind < 1 && load_addr == NULL) {
488 printf("Load address not specified\n");
491 if (argc - optind < 2 && file_size == NULL) {
492 printf("WARNING: Image size not specified; overwriting whole uboot partition\n");
496 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
501 size = simple_strtoul(argv[optind], NULL, 16);
504 if (load_addr != NULL) {
505 addr = (void *)simple_strtoul(load_addr, NULL, 16);
506 printf("Using default load address %p\n", addr);
508 if (file_size != NULL) {
509 size = simple_strtoul(file_size, NULL, 16);
510 printf("Using default file size %08x\n", size);
513 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
515 fw_num_blocks = part_info->size / mtd->erasesize -
517 size = fw_num_blocks * mtd->erasesize;
521 ret = find_dev_and_part(uboot_part, &dev, &part_num,
524 printf("Failed to find '%s' partition: %d\n",
528 fw1_start_block = part_info->offset / mtd->erasesize;
529 max_len1 = part_info->size;
531 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
535 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
538 printf("Failed to find '%s' partition: %d\n",
542 fw2_start_block = redund_part_info->offset / mtd->erasesize;
543 max_len2 = redund_part_info->size;
544 } else if (fw2_set) {
545 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
550 fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
551 max_len1 / mtd->erasesize);
553 printf("Could not find %lu contiguous good blocks for fw image\n",
556 #ifdef CONFIG_ENV_IS_IN_NAND
557 if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
558 printf("Use a different partition\n");
560 printf("Increase the size of the '%s' partition\n",
564 printf("Increase the size of the '%s' partition\n",
568 printf("Increase the number of spare blocks to use with the '-e' option\n");
572 fw1_end_block = fw1_start_block + fw1_skip + fw_num_blocks - 1;
574 if (fw2_set && fw2_start_block == 0)
575 fw2_start_block = fw1_end_block + 1;
576 if (fw2_start_block > 0) {
577 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
578 max_len2 / mtd->erasesize);
580 printf("Could not find %lu contiguous good blocks for redundant fw image\n",
583 printf("Increase the size of the '%s' partition or use a different partition\n",
586 printf("Increase the number of spare blocks to use with the '-e' option\n");
593 fw2_end_block = fw2_start_block + fw2_skip + fw_num_blocks - 1;
595 #ifdef CONFIG_ENV_IS_IN_NAND
596 fail_if_overlap(fcb, env, "FCB", "Environment");
597 fail_if_overlap(fw1, env, "FW1", "Environment");
599 fail_if_overlap(fcb, fw1, "FCB", "FW1");
601 fail_if_overlap(fcb, fw2, "FCB", "FW2");
602 #ifdef CONFIG_ENV_IS_IN_NAND
603 fail_if_overlap(fw2, env, "FW2", "Environment");
605 fail_if_overlap(fw1, fw2, "FW1", "FW2");
608 buf = malloc(erase_size);
610 printf("Failed to allocate buffer\n");
614 fcb = create_fcb(buf, fw1_start_block + fw1_skip,
615 fw2_start_block + fw2_skip, fw_num_blocks);
617 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
621 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
623 ret = write_fcb(buf, fcb_start_block);
626 printf("Failed to write FCB to block %lu\n", fcb_start_block);
630 if (size & (page_size - 1)) {
631 memset(addr + size, 0xff, size & (page_size - 1));
632 size = ALIGN(size, page_size);
635 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
636 addr, fw1_start_block + fw1_skip,
637 (u64)(fw1_start_block + fw1_skip) * mtd->erasesize);
638 ret = tx28_prog_uboot(addr, fw1_start_block, fw1_skip, size,
641 if (fw2_start_block == 0) {
645 printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
646 fw2_start_block + fw2_skip,
647 (u64)(fw2_start_block + fw2_skip) * mtd->erasesize);
648 ret = tx28_prog_uboot(addr, fw2_start_block, fw2_skip, fw_num_blocks,
653 U_BOOT_CMD(romupdate, 11, 0, do_update,
654 "Creates an FCB data structure and writes an U-Boot image to flash",
655 "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
656 "\t-f <part>\twrite bootloader image to partition <part>\n"
657 "\t-f #\twrite bootloader image at block # (decimal)\n"
658 "\t-r\twrite redundant bootloader image at next free block after first image\n"
659 "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
660 "\t-r #\twrite redundant bootloader image at block # (decimal)\n"
661 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
662 "\t\tonly valid if -f or -r specify a flash address rather than a partition name\n"
663 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
664 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"