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