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 mx28_gpmi_regs *gpmi_base =
156 (struct mx28_gpmi_regs *)MXS_GPMI_BASE;
157 struct mx28_bch_regs *bch_base =
158 (struct mx28_bch_regs *)MXS_BCH_BASE;
162 int bb_mark_bit_offs;
163 struct mx28_fcb *fcb;
166 if (gpmi_base == NULL || bch_base == NULL) {
167 return ERR_PTR(-ENOMEM);
170 fl0 = readl(&bch_base->hw_bch_flash0layout0);
171 fl1 = readl(&bch_base->hw_bch_flash0layout1);
172 t0 = readl(&gpmi_base->hw_gpmi_timing0);
173 t1 = readl(&gpmi_base->hw_gpmi_timing1);
175 metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
177 fcb = buf + ALIGN(metadata_size, 4);
178 fcb_offs = (void *)fcb - buf;
180 memset(buf, 0xff, fcb_offs);
181 memset(fcb, 0x00, sizeof(*fcb));
182 memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
184 strncpy((char *)&fcb->fingerprint, "FCB ", 4);
185 fcb->version = cpu_to_be32(1);
187 fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
188 fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
189 fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
191 fcb->page_data_size = mtd->writesize;
192 fcb->total_page_size = mtd->writesize + mtd->oobsize;
193 fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
195 fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASHLAYOUT0_ECC0);
196 fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_DATA0_SIZE);
197 fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASHLAYOUT1_ECCN);
198 fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASHLAYOUT1_DATAN_SIZE);
200 fcb->metadata_size = BF_VAL(fl0, BCH_FLASHLAYOUT0_META_SIZE);
201 fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASHLAYOUT0_NBLOCKS);
202 fcb->bch_mode = readl(&bch_base->hw_bch_mode);
205 fcb->patch_sectors = 0;
207 fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
208 fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
210 if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
211 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
212 fcb->fw2_sectors = fcb->fw1_sectors;
215 fcb->dbbt_search_area = 1;
217 bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
218 if (bb_mark_bit_offs < 0)
219 return ERR_PTR(bb_mark_bit_offs);
220 fcb->bb_mark_byte = bb_mark_bit_offs / 8;
221 fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
222 fcb->bb_mark_phys_offset = mtd->writesize;
224 fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
228 static int find_fcb(void *ref, int page)
231 struct nand_chip *chip = mtd->priv;
232 void *buf = malloc(mtd->erasesize);
237 chip->select_chip(mtd, 0);
238 chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
239 ret = chip->ecc.read_page_raw(mtd, chip, buf, page);
241 printf("Failed to read FCB from page %u: %d\n", page, ret);
244 chip->select_chip(mtd, -1);
245 if (memcmp(buf, ref, mtd->writesize) == 0) {
246 printf("%s: Found FCB in page %u (%08x)\n", __func__,
247 page, page * mtd->writesize);
254 static int write_fcb(void *buf, int block)
257 struct nand_chip *chip = mtd->priv;
258 int page = block * mtd->erasesize / mtd->writesize;
260 ret = find_fcb(buf, page);
262 printf("FCB at block %d is up to date\n", block);
266 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
268 printf("Failed to erase FCB block %u\n", block);
272 printf("Writing FCB to block %d @ %08x\n", block,
273 block * mtd->erasesize);
274 chip->select_chip(mtd, 0);
275 ret = chip->write_page(mtd, chip, buf, page, 0, 1);
277 printf("Failed to write FCB to block %u: %d\n", block, ret);
279 chip->select_chip(mtd, -1);
283 #define chk_overlap(a,b) \
284 ((a##_start_block <= b##_end_block && \
285 a##_end_block >= b##_start_block) || \
286 (b##_start_block <= a##_end_block && \
287 b##_end_block >= a##_start_block))
289 #define fail_if_overlap(a,b,m1,m2) do { \
290 if (chk_overlap(a, b)) { \
291 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
292 m1, a##_start_block, a##_end_block, \
293 m2, b##_start_block, b##_end_block); \
298 #ifndef CONFIG_ENV_OFFSET_REDUND
299 #define TOTAL_ENV_SIZE CONFIG_ENV_SIZE
301 #define TOTAL_ENV_SIZE (CONFIG_ENV_SIZE * 2)
304 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
308 int erase_size = mtd->erasesize;
309 int page_size = mtd->writesize;
315 struct mx28_fcb *fcb;
316 unsigned long fcb_start_block = FCB_START_BLOCK;
317 unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
318 unsigned long fcb_end_block;
319 unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
320 unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
321 unsigned long env_end_block = env_start_block +
322 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
326 unsigned long fw1_start_block = 0, fw1_end_block;
327 unsigned long fw2_start_block = 0, fw2_end_block;
328 unsigned long fw_num_blocks;
329 unsigned long extra_blocks = 2;
330 nand_erase_options_t erase_opts = { 0, };
333 load_addr = getenv("fileaddr");
334 file_size = getenv("filesize");
336 if (argc < 2 && load_addr == NULL) {
337 printf("Load address not specified\n");
340 if (argc < 3 && file_size == NULL) {
341 printf("Image size not specified\n");
345 for (optind = 1; optind < argc; optind++) {
346 if (strcmp(argv[optind], "-b") == 0) {
347 if (optind >= argc - 1) {
348 printf("Option %s requires an argument\n", argv[optind]);
352 fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
353 if (fcb_start_block >= mtd_num_blocks) {
354 printf("Block number %lu is out of range: 0..%lu\n",
355 fcb_start_block, mtd_num_blocks - 1);
358 } else if (strcmp(argv[optind], "-n") == 0) {
359 if (optind >= argc - 1) {
360 printf("Option %s requires an argument\n", argv[optind]);
364 num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
365 if (num_fcb_blocks > MAX_FCB_BLOCKS) {
366 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
370 } else if (strcmp(argv[optind], "-f") == 0) {
371 if (optind >= argc - 1) {
372 printf("Option %s requires an argument\n", argv[optind]);
376 fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
377 if (fw1_start_block >= mtd_num_blocks) {
378 printf("Block number %lu is out of range: 0..%lu\n",
384 } else if (strcmp(argv[optind], "-r") == 0) {
385 if (optind < argc - 1 && argv[optind + 1][0] != '-') {
387 fw2_start_block = simple_strtoul(argv[optind], NULL, 0);
388 if (fw2_start_block >= mtd_num_blocks) {
389 printf("Block number %lu is out of range: 0..%lu\n",
396 } else if (strcmp(argv[optind], "-e") == 0) {
397 if (optind >= argc - 1) {
398 printf("Option %s requires an argument\n", argv[optind]);
402 extra_blocks = simple_strtoul(argv[optind], NULL, 0);
403 if (extra_blocks >= mtd_num_blocks) {
404 printf("Extra block count %lu is out of range: 0..%lu\n",
409 } else if (argv[optind][0] == '-') {
410 printf("Unrecognized option %s\n", argv[optind]);
416 addr = (void *)simple_strtoul(argv[optind], NULL, 0);
421 size = simple_strtoul(argv[optind], NULL, 0);
424 if (load_addr != NULL) {
425 addr = (void *)simple_strtoul(load_addr, NULL, 16);
426 printf("Using default load address %p\n", addr);
428 if (file_size != NULL) {
429 size = simple_strtoul(file_size, NULL, 16);
430 printf("Using default file size %08x\n", size);
432 fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
433 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
436 fw1_start_block = fcb_end_block + 1;
437 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
438 if (chk_overlap(fw1, env)) {
439 fw1_start_block = env_end_block + 1;
440 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
443 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
446 if (fw2_set && fw2_start_block == 0) {
447 fw2_start_block = fw1_end_block + 1;
448 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
449 if (chk_overlap(fw2, env)) {
450 fw2_start_block = env_end_block + 1;
451 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
454 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
457 fail_if_overlap(fcb, env, "FCB", "Environment");
458 fail_if_overlap(fcb, fw1, "FCB", "FW1");
459 fail_if_overlap(fw1, env, "FW1", "Environment");
461 fail_if_overlap(fcb, fw2, "FCB", "FW2");
462 fail_if_overlap(fw2, env, "FW2", "Environment");
463 fail_if_overlap(fw1, fw2, "FW1", "FW2");
466 buf = malloc(erase_size);
468 printf("Failed to allocate buffer\n");
471 /* search for first non-bad block in FW1 block range */
472 while (fw1_start_block <= fw1_end_block) {
473 if (!nand_block_isbad(mtd, fw1_start_block * mtd->erasesize))
477 if (fw1_end_block - fw1_start_block + 1 < fw_num_blocks) {
478 printf("Too many bad blocks in FW1 block range: %lu..%lu\n",
479 fw1_end_block + 1 - fw_num_blocks - extra_blocks,
484 /* search for first non-bad block in FW2 block range */
485 while (fw2_set && fw2_start_block <= fw2_end_block) {
486 if (!nand_block_isbad(mtd, fw2_start_block * mtd->erasesize))
490 if (fw2_end_block - fw2_start_block + 1 < fw_num_blocks) {
491 printf("Too many bad blocks in FW2 area %08lx..%08lx\n",
492 fw2_end_block + 1 - fw_num_blocks - extra_blocks,
497 fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
498 (fw_num_blocks + extra_blocks) * mtd->erasesize);
500 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
503 encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
505 for (block = fcb_start_block; block < fcb_start_block + num_fcb_blocks;
507 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
508 if (block == fcb_start_block)
512 ret = write_fcb(buf, block);
514 printf("Failed to write FCB to block %u\n", block);
521 printf("Could not write FCB to flash\n");
525 printf("Programming U-Boot image from %p to block %lu\n",
526 addr, fw1_start_block);
527 if (size & (page_size - 1)) {
528 memset(addr + size, 0xff, size & (page_size - 1));
529 size = ALIGN(size, page_size);
532 erase_opts.offset = fcb->fw1_start_page * page_size;
533 erase_opts.length = ALIGN(size, erase_size) +
534 extra_blocks * mtd->erasesize;
535 erase_opts.quiet = 1;
537 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
538 erase_opts.offset + erase_opts.length - 1);
540 ret = nand_erase_opts(mtd, &erase_opts);
542 printf("Failed to erase flash: %d\n", ret);
545 printf("Programming flash @ %08x..%08x from %p\n",
546 fcb->fw1_start_page * page_size,
547 fcb->fw1_start_page * page_size + size, addr);
548 ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
549 &size, addr, WITH_DROP_FFS);
551 printf("Failed to program flash: %d\n", ret);
554 if (fw2_start_block == 0) {
558 printf("Programming redundant U-Boot image to block %lu\n",
560 erase_opts.offset = fcb->fw2_start_page * page_size;
561 printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
562 erase_opts.offset + erase_opts.length - 1);
564 ret = nand_erase_opts(mtd, &erase_opts);
566 printf("Failed to erase flash: %d\n", ret);
569 printf("Programming flash @ %08x..%08x from %p\n",
570 fcb->fw2_start_page * page_size,
571 fcb->fw2_start_page * page_size + size, addr);
572 ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
573 &size, addr, WITH_DROP_FFS);
575 printf("Failed to program flash: %d\n", ret);
581 U_BOOT_CMD(romupdate, 11, 0, do_update,
582 "Creates an FCB data structure and writes an U-Boot image to flash\n",
583 "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
584 "\t-b #\tfirst FCB block number (default 0)\n"
585 "\t-n #\ttotal number of FCB blocks (default 1)\n"
586 "\t-f #\twrite bootloader image at block #\n"
587 "\t-r\twrite redundant bootloader image at next free block after first image\n"
588 "\t-r #\twrite redundant bootloader image at block #\n"
589 "\t-e #\tspecify number of redundant blocks per boot loader image\n"
590 "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
591 "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"