1 // SPDX-License-Identifier: GPL-2.0+
3 * Freescale i.MX28 image generator
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
11 #include <sys/types.h>
16 /* Taken from <linux/kernel.h> */
17 #define __round_mask(x, y) ((__typeof__(x))((y)-1))
18 #define round_down(x, y) ((x) & ~__round_mask(x, y))
23 * TWEAK this if you have blown any OCOTP fuses.
25 #define STRIDE_PAGES 64
26 #define STRIDE_COUNT 4
29 * Layout for 256Mb big NAND with 2048b page size, 64b OOB size and
32 * TWEAK this if you have different kind of NAND chip.
34 static uint32_t nand_writesize = 2048;
35 static uint32_t nand_oobsize = 64;
36 static uint32_t nand_erasesize = 128 * 1024;
39 * Sector on which the SigmaTel boot partition (0x53) starts.
41 static uint32_t sd_sector = 2048;
44 * Each of the U-Boot bootstreams is at maximum 1MB big.
46 * TWEAK this if, for some wild reason, you need to boot bigger image.
48 #define MAX_BOOTSTREAM_SIZE (1 * 1024 * 1024)
50 /* i.MX28 NAND controller-specific constants. DO NOT TWEAK! */
51 #define MXS_NAND_DMA_DESCRIPTOR_COUNT 4
52 #define MXS_NAND_CHUNK_DATA_CHUNK_SIZE 512
53 #define MXS_NAND_METADATA_SIZE 10
54 #define MXS_NAND_BITS_PER_ECC_LEVEL 13
55 #define MXS_NAND_COMMAND_BUFFER_SIZE 32
57 struct mx28_nand_fcb {
64 uint8_t address_setup;
66 uint8_t nand_timing_state;
71 uint32_t page_data_size;
72 uint32_t total_page_size;
73 uint32_t sectors_per_block;
74 uint32_t number_of_nands; /* Ignored */
75 uint32_t total_internal_die; /* Ignored */
76 uint32_t cell_type; /* Ignored */
77 uint32_t ecc_block_n_ecc_type;
78 uint32_t ecc_block_0_size;
79 uint32_t ecc_block_n_size;
80 uint32_t ecc_block_0_ecc_type;
81 uint32_t metadata_bytes;
82 uint32_t num_ecc_blocks_per_page;
83 uint32_t ecc_block_n_ecc_level_sdk; /* Ignored */
84 uint32_t ecc_block_0_size_sdk; /* Ignored */
85 uint32_t ecc_block_n_size_sdk; /* Ignored */
86 uint32_t ecc_block_0_ecc_level_sdk; /* Ignored */
87 uint32_t num_ecc_blocks_per_page_sdk; /* Ignored */
88 uint32_t metadata_bytes_sdk; /* Ignored */
89 uint32_t erase_threshold;
91 uint32_t patch_sectors;
92 uint32_t firmware1_starting_sector;
93 uint32_t firmware2_starting_sector;
94 uint32_t sectors_in_firmware1;
95 uint32_t sectors_in_firmware2;
96 uint32_t dbbt_search_area_start_address;
97 uint32_t badblock_marker_byte;
98 uint32_t badblock_marker_start_bit;
99 uint32_t bb_marker_physical_offset;
102 struct mx28_nand_dbbt {
104 uint32_t fingerprint;
107 uint32_t number_2k_pages_bb;
110 struct mx28_nand_bbt {
113 uint32_t badblock[510];
116 struct mx28_sd_drive_info {
120 uint32_t first_sector_number;
121 uint32_t sector_count;
124 struct mx28_sd_config_block {
126 uint32_t primary_boot_tag;
127 uint32_t secondary_boot_tag;
129 struct mx28_sd_drive_info drv_info[1];
132 static inline uint32_t mx28_nand_ecc_chunk_cnt(uint32_t page_data_size)
134 return page_data_size / MXS_NAND_CHUNK_DATA_CHUNK_SIZE;
137 static inline uint32_t mx28_nand_ecc_size_in_bits(uint32_t ecc_strength)
139 return ecc_strength * MXS_NAND_BITS_PER_ECC_LEVEL;
142 static inline uint32_t mx28_nand_get_ecc_strength(uint32_t page_data_size,
143 uint32_t page_oob_size)
148 * Determine the ECC layout with the formula:
149 * ECC bits per chunk = (total page spare data bits) /
150 * (bits per ECC level) / (chunks per page)
152 * total page spare data bits =
153 * (page oob size - meta data size) * (bits per byte)
155 ecc_strength = ((page_oob_size - MXS_NAND_METADATA_SIZE) * 8)
156 / (MXS_NAND_BITS_PER_ECC_LEVEL *
157 mx28_nand_ecc_chunk_cnt(page_data_size));
159 return round_down(ecc_strength, 2);
162 static inline uint32_t mx28_nand_get_mark_offset(uint32_t page_data_size,
163 uint32_t ecc_strength)
165 uint32_t chunk_data_size_in_bits;
166 uint32_t chunk_ecc_size_in_bits;
167 uint32_t chunk_total_size_in_bits;
168 uint32_t block_mark_chunk_number;
169 uint32_t block_mark_chunk_bit_offset;
170 uint32_t block_mark_bit_offset;
172 chunk_data_size_in_bits = MXS_NAND_CHUNK_DATA_CHUNK_SIZE * 8;
173 chunk_ecc_size_in_bits = mx28_nand_ecc_size_in_bits(ecc_strength);
175 chunk_total_size_in_bits =
176 chunk_data_size_in_bits + chunk_ecc_size_in_bits;
178 /* Compute the bit offset of the block mark within the physical page. */
179 block_mark_bit_offset = page_data_size * 8;
181 /* Subtract the metadata bits. */
182 block_mark_bit_offset -= MXS_NAND_METADATA_SIZE * 8;
185 * Compute the chunk number (starting at zero) in which the block mark
188 block_mark_chunk_number =
189 block_mark_bit_offset / chunk_total_size_in_bits;
192 * Compute the bit offset of the block mark within its chunk, and
195 block_mark_chunk_bit_offset = block_mark_bit_offset -
196 (block_mark_chunk_number * chunk_total_size_in_bits);
198 if (block_mark_chunk_bit_offset > chunk_data_size_in_bits)
202 * Now that we know the chunk number in which the block mark appears,
203 * we can subtract all the ECC bits that appear before it.
205 block_mark_bit_offset -=
206 block_mark_chunk_number * chunk_ecc_size_in_bits;
208 return block_mark_bit_offset;
211 static inline uint32_t mx28_nand_mark_byte_offset(void)
213 uint32_t ecc_strength;
214 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
215 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) >> 3;
218 static inline uint32_t mx28_nand_mark_bit_offset(void)
220 uint32_t ecc_strength;
221 ecc_strength = mx28_nand_get_ecc_strength(nand_writesize, nand_oobsize);
222 return mx28_nand_get_mark_offset(nand_writesize, ecc_strength) & 0x7;
225 static uint32_t mx28_nand_block_csum(uint8_t *block, uint32_t size)
230 for (i = 0; i < size; i++)
233 return csum ^ 0xffffffff;
236 static struct mx28_nand_fcb *mx28_nand_get_fcb(uint32_t size)
238 struct mx28_nand_fcb *fcb;
239 uint32_t bcb_size_bytes;
240 uint32_t stride_size_bytes;
241 uint32_t bootstream_size_pages;
242 uint32_t fw1_start_page;
243 uint32_t fw2_start_page;
245 fcb = malloc(nand_writesize);
247 printf("MX28 NAND: Unable to allocate FCB\n");
251 memset(fcb, 0, nand_writesize);
253 fcb->fingerprint = 0x20424346;
254 fcb->version = 0x01000000;
257 * FIXME: These here are default values as found in kobs-ng. We should
258 * probably retrieve the data from NAND or something.
260 fcb->timing.data_setup = 80;
261 fcb->timing.data_hold = 60;
262 fcb->timing.address_setup = 25;
263 fcb->timing.dsample_time = 6;
265 fcb->page_data_size = nand_writesize;
266 fcb->total_page_size = nand_writesize + nand_oobsize;
267 fcb->sectors_per_block = nand_erasesize / nand_writesize;
269 fcb->num_ecc_blocks_per_page = (nand_writesize / 512) - 1;
270 fcb->ecc_block_0_size = 512;
271 fcb->ecc_block_n_size = 512;
272 fcb->metadata_bytes = 10;
273 fcb->ecc_block_n_ecc_type = mx28_nand_get_ecc_strength(
274 nand_writesize, nand_oobsize) >> 1;
275 fcb->ecc_block_0_ecc_type = mx28_nand_get_ecc_strength(
276 nand_writesize, nand_oobsize) >> 1;
277 if (fcb->ecc_block_n_ecc_type == 0) {
278 printf("MX28 NAND: Unsupported NAND geometry\n");
283 fcb->patch_sectors = 0;
285 fcb->badblock_marker_byte = mx28_nand_mark_byte_offset();
286 fcb->badblock_marker_start_bit = mx28_nand_mark_bit_offset();
287 fcb->bb_marker_physical_offset = nand_writesize;
289 stride_size_bytes = STRIDE_PAGES * nand_writesize;
290 bcb_size_bytes = stride_size_bytes * STRIDE_COUNT;
292 bootstream_size_pages = (size + (nand_writesize - 1)) /
295 fw1_start_page = 2 * bcb_size_bytes / nand_writesize;
296 fw2_start_page = (2 * bcb_size_bytes + MAX_BOOTSTREAM_SIZE) /
299 fcb->firmware1_starting_sector = fw1_start_page;
300 fcb->firmware2_starting_sector = fw2_start_page;
301 fcb->sectors_in_firmware1 = bootstream_size_pages;
302 fcb->sectors_in_firmware2 = bootstream_size_pages;
304 fcb->dbbt_search_area_start_address = STRIDE_PAGES * STRIDE_COUNT;
313 static struct mx28_nand_dbbt *mx28_nand_get_dbbt(void)
315 struct mx28_nand_dbbt *dbbt;
317 dbbt = malloc(nand_writesize);
319 printf("MX28 NAND: Unable to allocate DBBT\n");
323 memset(dbbt, 0, nand_writesize);
325 dbbt->fingerprint = 0x54424244;
331 static inline uint8_t mx28_nand_parity_13_8(const uint8_t b)
333 uint32_t parity = 0, tmp;
335 tmp = ((b >> 6) ^ (b >> 5) ^ (b >> 3) ^ (b >> 2)) & 1;
338 tmp = ((b >> 7) ^ (b >> 5) ^ (b >> 4) ^ (b >> 2) ^ (b >> 1)) & 1;
341 tmp = ((b >> 7) ^ (b >> 6) ^ (b >> 5) ^ (b >> 1) ^ (b >> 0)) & 1;
344 tmp = ((b >> 7) ^ (b >> 4) ^ (b >> 3) ^ (b >> 0)) & 1;
347 tmp = ((b >> 6) ^ (b >> 4) ^ (b >> 3) ^
348 (b >> 2) ^ (b >> 1) ^ (b >> 0)) & 1;
354 static uint8_t *mx28_nand_fcb_block(struct mx28_nand_fcb *fcb)
360 block = malloc(nand_writesize + nand_oobsize);
362 printf("MX28 NAND: Unable to allocate FCB block\n");
366 memset(block, 0, nand_writesize + nand_oobsize);
368 /* Update the FCB checksum */
369 fcb->checksum = mx28_nand_block_csum(((uint8_t *)fcb) + 4, 508);
371 /* Figure 12-11. in iMX28RM, rev. 1, says FCB is at offset 12 */
372 memcpy(block + 12, fcb, sizeof(struct mx28_nand_fcb));
374 /* ECC is at offset 12 + 512 */
375 ecc = block + 12 + 512;
377 /* Compute the ECC parity */
378 for (i = 0; i < sizeof(struct mx28_nand_fcb); i++)
379 ecc[i] = mx28_nand_parity_13_8(block[i + 12]);
384 static int mx28_nand_write_fcb(struct mx28_nand_fcb *fcb, uint8_t *buf)
391 fcbblock = mx28_nand_fcb_block(fcb);
395 for (i = 0; i < STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
396 offset = i * nand_writesize;
397 memcpy(buf + offset, fcbblock, nand_writesize + nand_oobsize);
398 /* Mark the NAND page is OK. */
399 buf[offset + nand_writesize] = 0xff;
406 static int mx28_nand_write_dbbt(struct mx28_nand_dbbt *dbbt, uint8_t *buf)
409 int i = STRIDE_PAGES * STRIDE_COUNT;
411 for (; i < 2 * STRIDE_PAGES * STRIDE_COUNT; i += STRIDE_PAGES) {
412 offset = i * nand_writesize;
413 memcpy(buf + offset, dbbt, sizeof(struct mx28_nand_dbbt));
419 static int mx28_nand_write_firmware(struct mx28_nand_fcb *fcb, int infd,
424 uint32_t offset1, offset2;
426 size = lseek(infd, 0, SEEK_END);
427 lseek(infd, 0, SEEK_SET);
429 offset1 = fcb->firmware1_starting_sector * nand_writesize;
430 offset2 = fcb->firmware2_starting_sector * nand_writesize;
432 ret = read(infd, buf + offset1, size);
436 memcpy(buf + offset2, buf + offset1, size);
441 static void usage(void)
444 "Usage: mxsboot [ops] <type> <infile> <outfile>\n"
445 "Augment BootStream file with a proper header for i.MX28 boot\n"
447 " <type> type of image:\n"
448 " \"nand\" for NAND image\n"
449 " \"sd\" for SD image\n"
450 " <infile> input file, the u-boot.sb bootstream\n"
451 " <outfile> output file, the bootable image\n"
454 "For NAND boot, these options are accepted:\n"
455 " -w <size> NAND page size\n"
456 " -o <size> NAND OOB size\n"
457 " -e <size> NAND erase size\n"
459 "For SD boot, these options are accepted:\n"
460 " -p <sector> Sector where the SGTL partition starts\n"
464 static int mx28_create_nand_image(int infd, int outfd)
466 struct mx28_nand_fcb *fcb;
467 struct mx28_nand_dbbt *dbbt;
473 size = nand_writesize * 512 + 2 * MAX_BOOTSTREAM_SIZE;
477 printf("Can not allocate output buffer of %d bytes\n", size);
481 memset(buf, 0, size);
483 fcb = mx28_nand_get_fcb(MAX_BOOTSTREAM_SIZE);
485 printf("Unable to compile FCB\n");
489 dbbt = mx28_nand_get_dbbt();
491 printf("Unable to compile DBBT\n");
495 ret = mx28_nand_write_fcb(fcb, buf);
497 printf("Unable to write FCB to buffer\n");
501 ret = mx28_nand_write_dbbt(dbbt, buf);
503 printf("Unable to write DBBT to buffer\n");
507 ret = mx28_nand_write_firmware(fcb, infd, buf);
509 printf("Unable to write firmware to buffer\n");
513 wr_size = write(outfd, buf, size);
514 if (wr_size != size) {
531 static int mx28_create_sd_image(int infd, int outfd)
538 struct mx28_sd_config_block *cb;
540 fsize = lseek(infd, 0, SEEK_END);
541 lseek(infd, 0, SEEK_SET);
542 size = fsize + 4 * 512;
546 printf("Can not allocate output buffer of %d bytes\n", size);
550 ret = read(infd, (uint8_t *)buf + 4 * 512, fsize);
556 cb = (struct mx28_sd_config_block *)buf;
558 cb->signature = cpu_to_le32(0x00112233);
559 cb->primary_boot_tag = cpu_to_le32(0x1);
560 cb->secondary_boot_tag = cpu_to_le32(0x1);
561 cb->num_copies = cpu_to_le32(1);
562 cb->drv_info[0].chip_num = cpu_to_le32(0x0);
563 cb->drv_info[0].drive_type = cpu_to_le32(0x0);
564 cb->drv_info[0].tag = cpu_to_le32(0x1);
565 cb->drv_info[0].first_sector_number = cpu_to_le32(sd_sector + 4);
566 cb->drv_info[0].sector_count = cpu_to_le32((size - 4) / 512);
568 wr_size = write(outfd, buf, size);
569 if (wr_size != size) {
582 static int parse_ops(int argc, char **argv)
600 for (i = 1; i < argc; i++) {
601 if (!strncmp(argv[i], "-w", 2))
603 else if (!strncmp(argv[i], "-o", 2))
605 else if (!strncmp(argv[i], "-e", 2))
607 else if (!strncmp(argv[i], "-p", 2))
612 tmp = strtol(argv[++i], &end, 10);
618 if (type == PARAM_WRITE)
619 nand_writesize = tmp;
620 if (type == PARAM_OOB)
622 if (type == PARAM_ERASE)
623 nand_erasesize = tmp;
624 if (type == PARAM_PART)
628 if (strcmp(argv[i], "sd") && strcmp(argv[i], "nand"))
637 int main(int argc, char **argv)
643 offset = parse_ops(argc, argv);
650 infd = open(argv[offset + 1], O_RDONLY);
652 printf("Input BootStream file can not be opened\n");
657 outfd = open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY,
660 printf("Output file can not be created\n");
665 if (!strcmp(argv[offset], "sd"))
666 ret = mx28_create_sd_image(infd, outfd);
667 else if (!strcmp(argv[offset], "nand"))
668 ret = mx28_create_nand_image(infd, outfd);