Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 1 | /* |
| 2 | * i.MX6 nand boot control block(bcb). |
| 3 | * |
| 4 | * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng |
| 5 | * |
| 6 | * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com> |
| 7 | * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <nand.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame^] | 14 | #include <dm/devres.h> |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 15 | |
| 16 | #include <asm/io.h> |
| 17 | #include <jffs2/jffs2.h> |
Parthiban Nallathambi | 6aa8749 | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 18 | #include <linux/bch.h> |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 19 | #include <linux/mtd/mtd.h> |
| 20 | |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 21 | #include <asm/arch/sys_proto.h> |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 22 | #include <asm/mach-imx/imx-nandbcb.h> |
| 23 | #include <asm/mach-imx/imximage.cfg> |
| 24 | #include <mxs_nand.h> |
| 25 | #include <linux/mtd/mtd.h> |
| 26 | #include <nand.h> |
| 27 | |
Miquel Raynal | eb446ef | 2019-10-25 19:39:29 +0200 | [diff] [blame] | 28 | #include "../../../cmd/legacy-mtd-utils.h" |
| 29 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 30 | #define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET) |
| 31 | #define GETBIT(v, n) (((v) >> (n)) & 0x1) |
| 32 | |
Parthiban Nallathambi | 6aa8749 | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 33 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 34 | static uint8_t reverse_bit(uint8_t b) |
| 35 | { |
| 36 | b = (b & 0xf0) >> 4 | (b & 0x0f) << 4; |
| 37 | b = (b & 0xcc) >> 2 | (b & 0x33) << 2; |
| 38 | b = (b & 0xaa) >> 1 | (b & 0x55) << 1; |
| 39 | |
| 40 | return b; |
| 41 | } |
| 42 | |
| 43 | static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits) |
| 44 | { |
| 45 | int i, j, m = 13; |
| 46 | int blocksize = 128; |
| 47 | int numblocks = 8; |
| 48 | int ecc_buf_size = (m * eccbits + 7) / 8; |
| 49 | struct bch_control *bch = init_bch(m, eccbits, 0); |
| 50 | u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL); |
| 51 | u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL); |
| 52 | u8 *psrc, *pdst; |
| 53 | |
| 54 | /* |
| 55 | * The blocks here are bit aligned. If eccbits is a multiple of 8, |
| 56 | * we just can copy bytes. Otherwiese we must move the blocks to |
| 57 | * the next free bit position. |
| 58 | */ |
| 59 | WARN_ON(eccbits % 8); |
| 60 | |
| 61 | memcpy(tmp_buf, fcb, sizeof(*fcb)); |
| 62 | |
| 63 | for (i = 0; i < numblocks; i++) { |
| 64 | memset(ecc_buf, 0, ecc_buf_size); |
| 65 | psrc = tmp_buf + i * blocksize; |
| 66 | pdst = buf + i * (blocksize + ecc_buf_size); |
| 67 | |
| 68 | /* copy data byte aligned to destination buf */ |
| 69 | memcpy(pdst, psrc, blocksize); |
| 70 | |
| 71 | /* |
| 72 | * imx-kobs use a modified encode_bch which reverse the |
| 73 | * bit order of the data before calculating bch. |
| 74 | * Do this in the buffer and use the bch lib here. |
| 75 | */ |
| 76 | for (j = 0; j < blocksize; j++) |
| 77 | psrc[j] = reverse_bit(psrc[j]); |
| 78 | |
| 79 | encode_bch(bch, psrc, blocksize, ecc_buf); |
| 80 | |
| 81 | /* reverse ecc bit */ |
| 82 | for (j = 0; j < ecc_buf_size; j++) |
| 83 | ecc_buf[j] = reverse_bit(ecc_buf[j]); |
| 84 | |
| 85 | /* Here eccbuf is byte aligned and we can just copy it */ |
| 86 | memcpy(pdst + blocksize, ecc_buf, ecc_buf_size); |
| 87 | } |
| 88 | |
| 89 | kfree(ecc_buf); |
| 90 | kfree(tmp_buf); |
| 91 | free_bch(bch); |
| 92 | } |
| 93 | #else |
| 94 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 95 | static u8 calculate_parity_13_8(u8 d) |
| 96 | { |
| 97 | u8 p = 0; |
| 98 | |
| 99 | p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0; |
| 100 | p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^ |
| 101 | GETBIT(d, 1)) << 1; |
| 102 | p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^ |
| 103 | GETBIT(d, 0)) << 2; |
| 104 | p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3; |
| 105 | p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^ |
| 106 | GETBIT(d, 1) ^ GETBIT(d, 0)) << 4; |
| 107 | |
| 108 | return p; |
| 109 | } |
| 110 | |
| 111 | static void encode_hamming_13_8(void *_src, void *_ecc, size_t size) |
| 112 | { |
| 113 | int i; |
| 114 | u8 *src = _src; |
| 115 | u8 *ecc = _ecc; |
| 116 | |
| 117 | for (i = 0; i < size; i++) |
| 118 | ecc[i] = calculate_parity_13_8(src[i]); |
| 119 | } |
Parthiban Nallathambi | 6aa8749 | 2019-10-18 11:46:19 +0200 | [diff] [blame] | 120 | #endif |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 121 | |
| 122 | static u32 calc_chksum(void *buf, size_t size) |
| 123 | { |
| 124 | u32 chksum = 0; |
| 125 | u8 *bp = buf; |
| 126 | size_t i; |
| 127 | |
| 128 | for (i = 0; i < size; i++) |
| 129 | chksum += bp[i]; |
| 130 | |
| 131 | return ~chksum; |
| 132 | } |
| 133 | |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 134 | static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd, |
| 135 | u32 fw1_start, u32 fw2_start, u32 fw_pages) |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 136 | { |
| 137 | struct nand_chip *chip = mtd_to_nand(mtd); |
| 138 | struct mxs_nand_info *nand_info = nand_get_controller_data(chip); |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 139 | struct mxs_nand_layout l; |
| 140 | |
| 141 | mxs_nand_get_layout(mtd, &l); |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 142 | |
| 143 | fcb->fingerprint = FCB_FINGERPRINT; |
| 144 | fcb->version = FCB_VERSION_1; |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 145 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 146 | fcb->pagesize = mtd->writesize; |
| 147 | fcb->oob_pagesize = mtd->writesize + mtd->oobsize; |
| 148 | fcb->sectors = mtd->erasesize / mtd->writesize; |
| 149 | |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 150 | fcb->meta_size = l.meta_size; |
| 151 | fcb->nr_blocks = l.nblocks; |
| 152 | fcb->ecc_nr = l.data0_size; |
| 153 | fcb->ecc_level = l.ecc0; |
| 154 | fcb->ecc_size = l.datan_size; |
| 155 | fcb->ecc_type = l.eccn; |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 156 | |
| 157 | /* Also hardcoded in kobs-ng */ |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 158 | if (is_mx6()) { |
| 159 | fcb->datasetup = 80; |
| 160 | fcb->datahold = 60; |
| 161 | fcb->addr_setup = 25; |
| 162 | fcb->dsample_time = 6; |
| 163 | } else if (is_mx7()) { |
| 164 | fcb->datasetup = 10; |
| 165 | fcb->datahold = 7; |
| 166 | fcb->addr_setup = 15; |
| 167 | fcb->dsample_time = 6; |
| 168 | } |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 169 | |
| 170 | /* DBBT search area starts at second page on first block */ |
| 171 | fcb->dbbt_start = 1; |
| 172 | |
| 173 | fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset; |
| 174 | fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset; |
| 175 | |
| 176 | fcb->phy_offset = mtd->writesize; |
| 177 | |
| 178 | fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1; |
| 179 | |
Igor Opaniuk | dad30dd | 2019-11-03 16:49:44 +0100 | [diff] [blame] | 180 | fcb->disbbm = 0; |
| 181 | fcb->disbbm_search = 0; |
| 182 | |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 183 | fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */ |
| 184 | fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */ |
| 185 | fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */ |
| 186 | fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */ |
| 187 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 188 | fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4); |
| 189 | } |
| 190 | |
| 191 | static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks) |
| 192 | { |
| 193 | int n, n_bad_blocks = 0; |
| 194 | u32 *bb = buf + 0x8; |
| 195 | u32 *n_bad_blocksp = buf + 0x4; |
| 196 | |
| 197 | for (n = 0; n < num_blocks; n++) { |
| 198 | loff_t offset = n * mtd->erasesize; |
| 199 | if (mtd_block_isbad(mtd, offset)) { |
| 200 | n_bad_blocks++; |
| 201 | *bb = n; |
| 202 | bb++; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | *n_bad_blocksp = n_bad_blocks; |
| 207 | |
| 208 | return n_bad_blocks; |
| 209 | } |
| 210 | |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 211 | static int write_fcb_dbbt(struct mtd_info *mtd, struct fcb_block *fcb, |
| 212 | struct dbbt_block *dbbt, void *dbbt_data_page, |
| 213 | loff_t off) |
| 214 | { |
| 215 | void *fcb_raw_page = 0; |
| 216 | int i, ret; |
| 217 | size_t dummy; |
| 218 | |
| 219 | /* |
| 220 | * We prepare raw page only for i.MX6, for i.MX7 we |
| 221 | * leverage BCH hw module instead |
| 222 | */ |
| 223 | if (is_mx6()) { |
| 224 | /* write fcb/dbbt */ |
| 225 | fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize, |
| 226 | GFP_KERNEL); |
| 227 | if (!fcb_raw_page) { |
| 228 | debug("failed to allocate fcb_raw_page\n"); |
| 229 | ret = -ENOMEM; |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL) |
| 234 | /* 40 bit BCH, for i.MX6UL(L) */ |
| 235 | encode_bch_ecc(fcb_raw_page + 32, fcb, 40); |
| 236 | #else |
| 237 | memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block)); |
| 238 | encode_hamming_13_8(fcb_raw_page + 12, |
| 239 | fcb_raw_page + 12 + 512, 512); |
| 240 | #endif |
| 241 | /* |
| 242 | * Set the first and second byte of OOB data to 0xFF, |
| 243 | * not 0x00. These bytes are used as the Manufacturers Bad |
| 244 | * Block Marker (MBBM). Since the FCB is mostly written to |
| 245 | * the first page in a block, a scan for |
| 246 | * factory bad blocks will detect these blocks as bad, e.g. |
| 247 | * when function nand_scan_bbt() is executed to build a new |
| 248 | * bad block table. |
| 249 | */ |
| 250 | memset(fcb_raw_page + mtd->writesize, 0xFF, 2); |
| 251 | } |
| 252 | for (i = 0; i < 2; i++) { |
| 253 | if (mtd_block_isbad(mtd, off)) { |
| 254 | printf("Block %d is bad, skipped\n", i); |
| 255 | continue; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * User BCH ECC hardware module for i.MX7 |
| 260 | */ |
| 261 | if (is_mx7()) { |
| 262 | u32 off = i * mtd->erasesize; |
| 263 | size_t rwsize = sizeof(*fcb); |
| 264 | |
| 265 | printf("Writing %d bytes to 0x%x: ", rwsize, off); |
| 266 | |
| 267 | /* switch nand BCH to FCB compatible settings */ |
| 268 | mxs_nand_mode_fcb(mtd); |
| 269 | ret = nand_write(mtd, off, &rwsize, |
| 270 | (unsigned char *)fcb); |
| 271 | mxs_nand_mode_normal(mtd); |
| 272 | |
| 273 | printf("%s\n", ret ? "ERROR" : "OK"); |
| 274 | } else if (is_mx6()) { |
| 275 | /* raw write */ |
| 276 | mtd_oob_ops_t ops = { |
| 277 | .datbuf = (u8 *)fcb_raw_page, |
| 278 | .oobbuf = ((u8 *)fcb_raw_page) + |
| 279 | mtd->writesize, |
| 280 | .len = mtd->writesize, |
| 281 | .ooblen = mtd->oobsize, |
| 282 | .mode = MTD_OPS_RAW |
| 283 | }; |
| 284 | |
| 285 | ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops); |
| 286 | if (ret) |
| 287 | goto fcb_raw_page_err; |
| 288 | debug("NAND fcb write: 0x%x offset 0x%x written: %s\n", |
| 289 | mtd->erasesize * i, ops.len, ret ? |
| 290 | "ERROR" : "OK"); |
| 291 | } |
| 292 | |
| 293 | ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize, |
| 294 | mtd->writesize, &dummy, (void *)dbbt); |
| 295 | if (ret) |
| 296 | goto fcb_raw_page_err; |
| 297 | debug("NAND dbbt write: 0x%x offset, 0x%x bytes written: %s\n", |
| 298 | mtd->erasesize * i + mtd->writesize, dummy, |
| 299 | ret ? "ERROR" : "OK"); |
| 300 | |
| 301 | /* dbbtpages == 0 if no bad blocks */ |
| 302 | if (dbbt->dbbtpages > 0) { |
| 303 | loff_t to = (mtd->erasesize * i + mtd->writesize * 5); |
| 304 | |
| 305 | ret = mtd_write(mtd, to, mtd->writesize, &dummy, |
| 306 | dbbt_data_page); |
| 307 | if (ret) |
| 308 | goto fcb_raw_page_err; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | fcb_raw_page_err: |
| 313 | if (is_mx6()) |
| 314 | kfree(fcb_raw_page); |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 319 | static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size, |
| 320 | size_t maxsize, const u_char *buf) |
| 321 | { |
| 322 | nand_erase_options_t opts; |
| 323 | struct fcb_block *fcb; |
| 324 | struct dbbt_block *dbbt; |
| 325 | loff_t fw1_off; |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 326 | void *fwbuf, *dbbt_page, *dbbt_data_page; |
| 327 | u32 fw1_start, fw1_pages; |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 328 | int nr_blks, nr_blks_fcb, fw1_blk; |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 329 | size_t fwsize; |
| 330 | int ret; |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 331 | |
| 332 | /* erase */ |
| 333 | memset(&opts, 0, sizeof(opts)); |
| 334 | opts.offset = off; |
| 335 | opts.length = maxsize - 1; |
| 336 | ret = nand_erase_opts(mtd, &opts); |
| 337 | if (ret) { |
| 338 | printf("%s: erase failed (ret = %d)\n", __func__, ret); |
| 339 | return ret; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Reference documentation from i.MX6DQRM section 8.5.2.2 |
| 344 | * |
| 345 | * Nand Boot Control Block(BCB) contains two data structures, |
| 346 | * - Firmware Configuration Block(FCB) |
| 347 | * - Discovered Bad Block Table(DBBT) |
| 348 | * |
| 349 | * FCB contains, |
| 350 | * - nand timings |
| 351 | * - DBBT search page address, |
| 352 | * - start page address of primary firmware |
| 353 | * - start page address of secondary firmware |
| 354 | * |
| 355 | * setup fcb: |
| 356 | * - number of blocks = mtd partition size / mtd erasesize |
| 357 | * - two firmware blocks, primary and secondary |
| 358 | * - first 4 block for FCB/DBBT |
| 359 | * - rest split in half for primary and secondary firmware |
| 360 | * - same firmware will write two times |
| 361 | */ |
| 362 | nr_blks_fcb = 2; |
| 363 | nr_blks = maxsize / mtd->erasesize; |
| 364 | fw1_blk = nr_blks_fcb; |
| 365 | |
| 366 | /* write fw */ |
| 367 | fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize, |
| 368 | mtd->writesize); |
| 369 | fwbuf = kzalloc(fwsize, GFP_KERNEL); |
| 370 | if (!fwbuf) { |
| 371 | debug("failed to allocate fwbuf\n"); |
| 372 | ret = -ENOMEM; |
| 373 | goto err; |
| 374 | } |
| 375 | |
| 376 | memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size); |
| 377 | fw1_off = fw1_blk * mtd->erasesize; |
| 378 | ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize, |
| 379 | (u_char *)fwbuf, WITH_WR_VERIFY); |
| 380 | printf("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n", |
| 381 | fw1_off, fwsize, ret ? "ERROR" : "OK"); |
| 382 | if (ret) |
| 383 | goto fwbuf_err; |
| 384 | |
| 385 | /* fill fcb */ |
| 386 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 387 | if (!fcb) { |
| 388 | debug("failed to allocate fcb\n"); |
| 389 | ret = -ENOMEM; |
| 390 | goto fwbuf_err; |
| 391 | } |
| 392 | |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 393 | fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize; |
| 394 | fw1_pages = size / mtd->writesize + 1; |
| 395 | fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages); |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 396 | |
| 397 | /* fill dbbt */ |
| 398 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 399 | if (!dbbt_page) { |
| 400 | debug("failed to allocate dbbt_page\n"); |
| 401 | ret = -ENOMEM; |
| 402 | goto fcb_err; |
| 403 | } |
| 404 | |
| 405 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 406 | if (!dbbt_data_page) { |
| 407 | debug("failed to allocate dbbt_data_page\n"); |
| 408 | ret = -ENOMEM; |
| 409 | goto dbbt_page_err; |
| 410 | } |
| 411 | |
| 412 | dbbt = dbbt_page; |
| 413 | dbbt->checksum = 0; |
| 414 | dbbt->fingerprint = DBBT_FINGERPRINT2; |
| 415 | dbbt->version = DBBT_VERSION_1; |
| 416 | ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks); |
| 417 | if (ret < 0) |
| 418 | goto dbbt_data_page_err; |
| 419 | else if (ret > 0) |
| 420 | dbbt->dbbtpages = 1; |
| 421 | |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 422 | /* write fcb and dbbt to nand */ |
| 423 | ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, off); |
| 424 | if (ret < 0) |
| 425 | printf("failed to write FCB/DBBT\n"); |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 426 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 427 | dbbt_data_page_err: |
| 428 | kfree(dbbt_data_page); |
| 429 | dbbt_page_err: |
| 430 | kfree(dbbt_page); |
| 431 | fcb_err: |
| 432 | kfree(fcb); |
| 433 | fwbuf_err: |
| 434 | kfree(fwbuf); |
| 435 | err: |
| 436 | return ret; |
| 437 | } |
| 438 | |
Igor Opaniuk | ae8a53e | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 439 | static int do_nandbcb_bcbonly(int argc, char * const argv[]) |
| 440 | { |
| 441 | struct fcb_block *fcb; |
| 442 | struct dbbt_block *dbbt; |
| 443 | u32 fw_len, fw1_off, fw2_off; |
| 444 | struct mtd_info *mtd; |
| 445 | void *dbbt_page, *dbbt_data_page; |
| 446 | int dev, ret; |
| 447 | |
| 448 | dev = nand_curr_device; |
| 449 | if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) || |
| 450 | (!get_nand_dev_by_index(dev))) { |
| 451 | puts("No devices available\n"); |
| 452 | return CMD_RET_FAILURE; |
| 453 | } |
| 454 | |
| 455 | mtd = get_nand_dev_by_index(dev); |
| 456 | |
| 457 | if (argc < 3) |
| 458 | return CMD_RET_FAILURE; |
| 459 | |
| 460 | fw_len = simple_strtoul(argv[1], NULL, 16); |
| 461 | fw1_off = simple_strtoul(argv[2], NULL, 16); |
| 462 | |
| 463 | if (argc > 3) |
| 464 | fw2_off = simple_strtoul(argv[3], NULL, 16); |
| 465 | else |
| 466 | fw2_off = fw1_off; |
| 467 | |
| 468 | /* fill fcb */ |
| 469 | fcb = kzalloc(sizeof(*fcb), GFP_KERNEL); |
| 470 | if (!fcb) { |
| 471 | debug("failed to allocate fcb\n"); |
| 472 | ret = -ENOMEM; |
| 473 | return CMD_RET_FAILURE; |
| 474 | } |
| 475 | |
| 476 | fill_fcb(fcb, mtd, fw1_off / mtd->writesize, |
| 477 | fw2_off / mtd->writesize, fw_len / mtd->writesize); |
| 478 | |
| 479 | /* fill dbbt */ |
| 480 | dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 481 | if (!dbbt_page) { |
| 482 | debug("failed to allocate dbbt_page\n"); |
| 483 | ret = -ENOMEM; |
| 484 | goto fcb_err; |
| 485 | } |
| 486 | |
| 487 | dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL); |
| 488 | if (!dbbt_data_page) { |
| 489 | debug("failed to allocate dbbt_data_page\n"); |
| 490 | ret = -ENOMEM; |
| 491 | goto dbbt_page_err; |
| 492 | } |
| 493 | |
| 494 | dbbt = dbbt_page; |
| 495 | dbbt->checksum = 0; |
| 496 | dbbt->fingerprint = DBBT_FINGERPRINT2; |
| 497 | dbbt->version = DBBT_VERSION_1; |
| 498 | ret = dbbt_fill_data(mtd, dbbt_data_page, 0); |
| 499 | if (ret < 0) |
| 500 | goto dbbt_data_page_err; |
| 501 | else if (ret > 0) |
| 502 | dbbt->dbbtpages = 1; |
| 503 | |
| 504 | /* write fcb and dbbt to nand */ |
| 505 | ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, 0); |
| 506 | dbbt_data_page_err: |
| 507 | kfree(dbbt_data_page); |
| 508 | dbbt_page_err: |
| 509 | kfree(dbbt_page); |
| 510 | fcb_err: |
| 511 | kfree(fcb); |
| 512 | |
| 513 | if (ret < 0) { |
| 514 | printf("failed to write FCB/DBBT\n"); |
| 515 | return CMD_RET_FAILURE; |
| 516 | } |
| 517 | |
| 518 | return CMD_RET_SUCCESS; |
| 519 | } |
| 520 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 521 | static int do_nandbcb_update(int argc, char * const argv[]) |
| 522 | { |
| 523 | struct mtd_info *mtd; |
| 524 | loff_t addr, offset, size, maxsize; |
| 525 | char *endp; |
| 526 | u_char *buf; |
| 527 | int dev; |
| 528 | int ret; |
| 529 | |
| 530 | if (argc != 4) |
| 531 | return CMD_RET_USAGE; |
| 532 | |
| 533 | dev = nand_curr_device; |
| 534 | if (dev < 0) { |
Igor Opaniuk | 1b899a8 | 2019-11-03 16:49:45 +0100 | [diff] [blame] | 535 | printf("failed to get nand_curr_device, run nand device\n"); |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 536 | return CMD_RET_FAILURE; |
| 537 | } |
| 538 | |
| 539 | addr = simple_strtoul(argv[1], &endp, 16); |
| 540 | if (*argv[1] == 0 || *endp != 0) |
| 541 | return CMD_RET_FAILURE; |
| 542 | |
| 543 | mtd = get_nand_dev_by_index(dev); |
| 544 | if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size, |
| 545 | &maxsize, MTD_DEV_TYPE_NAND, mtd->size)) |
| 546 | return CMD_RET_FAILURE; |
| 547 | |
| 548 | buf = map_physmem(addr, size, MAP_WRBACK); |
| 549 | if (!buf) { |
| 550 | puts("failed to map physical memory\n"); |
| 551 | return CMD_RET_FAILURE; |
| 552 | } |
| 553 | |
| 554 | ret = nandbcb_update(mtd, offset, size, maxsize, buf); |
| 555 | |
| 556 | return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE; |
| 557 | } |
| 558 | |
| 559 | static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc, |
| 560 | char * const argv[]) |
| 561 | { |
| 562 | const char *cmd; |
| 563 | int ret = 0; |
| 564 | |
| 565 | if (argc < 5) |
| 566 | goto usage; |
| 567 | |
| 568 | cmd = argv[1]; |
| 569 | --argc; |
| 570 | ++argv; |
| 571 | |
| 572 | if (strcmp(cmd, "update") == 0) { |
| 573 | ret = do_nandbcb_update(argc, argv); |
| 574 | goto done; |
| 575 | } |
| 576 | |
Igor Opaniuk | ae8a53e | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 577 | if (strcmp(cmd, "bcbonly") == 0) { |
| 578 | ret = do_nandbcb_bcbonly(argc, argv); |
| 579 | goto done; |
| 580 | } |
| 581 | |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 582 | done: |
| 583 | if (ret != -1) |
| 584 | return ret; |
| 585 | usage: |
| 586 | return CMD_RET_USAGE; |
| 587 | } |
| 588 | |
Parthiban Nallathambi | 4ee0ff1 | 2019-08-23 18:35:10 +0200 | [diff] [blame] | 589 | #ifdef CONFIG_SYS_LONGHELP |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 590 | static char nandbcb_help_text[] = |
| 591 | "update addr off|partition len - update 'len' bytes starting at\n" |
Igor Opaniuk | ae8a53e | 2019-11-03 16:49:46 +0100 | [diff] [blame] | 592 | " 'off|part' to memory address 'addr', skipping bad blocks\n" |
| 593 | "bcbonly fw-size fw1-off [fw2-off] - write only BCB (FCB and DBBT)\n" |
Igor Opaniuk | 061b63b | 2019-12-16 14:06:44 +0200 | [diff] [blame] | 594 | " where `fw-size` is fw sizes in bytes, `fw1-off`\n" |
| 595 | " and `fw2-off` - firmware offsets\n" |
| 596 | " FIY, BCB isn't erased automatically, so mtd erase should\n" |
| 597 | " be called in advance before writing new BCB:\n" |
| 598 | " > mtd erase mx7-bcb"; |
Parthiban Nallathambi | 4ee0ff1 | 2019-08-23 18:35:10 +0200 | [diff] [blame] | 599 | #endif |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 600 | |
| 601 | U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb, |
Igor Opaniuk | 061b63b | 2019-12-16 14:06:44 +0200 | [diff] [blame] | 602 | "i.MX6/i.MX7 NAND Boot Control Blocks write", |
Shyam Saini | 1d43e24 | 2019-06-14 13:05:33 +0530 | [diff] [blame] | 603 | nandbcb_help_text |
| 604 | ); |