Blame view

arch/arm/mach-imx/cmd_nandbcb.c 20.8 KB
49538a259   Shyam Saini   i.MX6: nand: add ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  /*
   * i.MX6 nand boot control block(bcb).
   *
   * Based on the common/imx-bbu-nand-fcb.c from barebox and imx kobs-ng
   *
   * Copyright (C) 2017 Jagan Teki <jagan@amarulasolutions.com>
   * Copyright (C) 2016 Sergey Kubushyn <ksi@koi8.net>
   *
   * SPDX-License-Identifier:	GPL-2.0+
   */
  
  #include <common.h>
  #include <nand.h>
  
  #include <asm/io.h>
  #include <jffs2/jffs2.h>
  #include <linux/mtd/mtd.h>
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
18
  #include <asm/arch/sys_proto.h>
49538a259   Shyam Saini   i.MX6: nand: add ...
19
20
21
22
23
  #include <asm/mach-imx/imx-nandbcb.h>
  #include <asm/mach-imx/imximage.cfg>
  #include <mxs_nand.h>
  #include <linux/mtd/mtd.h>
  #include <nand.h>
1d8a50993   Alice Guo   MLK-22582-2: nand...
24
  #include <div64.h>
49538a259   Shyam Saini   i.MX6: nand: add ...
25
26
27
  
  #define BF_VAL(v, bf)		(((v) & bf##_MASK) >> bf##_OFFSET)
  #define GETBIT(v, n)		(((v) >> (n)) & 0x1)
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
28
29
  #define IMX8MQ_SPL_SZ 0x3e000
  #define IMX8MQ_HDMI_FW_SZ 0x19c00
1d8a50993   Alice Guo   MLK-22582-2: nand...
30
31
32
33
34
35
36
37
38
39
  #define BOOT_SEARCH_COUNT 2
  
  struct mtd_info *dump_mtd;
  static loff_t dump_nandboot_size;
  static struct fcb_block dump_fill_fcb;
  static struct dbbt_block dump_fill_dbbt;
  static struct fcb_block dump_nand_fcb[BOOT_SEARCH_COUNT];
  static struct dbbt_block dump_nand_dbbt[BOOT_SEARCH_COUNT];
  static u32 dump_fcb_off[BOOT_SEARCH_COUNT];
  static u32 dump_dbbt_off[BOOT_SEARCH_COUNT];
49538a259   Shyam Saini   i.MX6: nand: add ...
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  
  static u8 calculate_parity_13_8(u8 d)
  {
  	u8 p = 0;
  
  	p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
  	p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
  	      GETBIT(d, 1)) << 1;
  	p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
  	      GETBIT(d, 0)) << 2;
  	p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
  	p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
  	      GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
  
  	return p;
  }
  
  static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
  {
  	int i;
  	u8 *src = _src;
  	u8 *ecc = _ecc;
  
  	for (i = 0; i < size; i++)
  		ecc[i] = calculate_parity_13_8(src[i]);
  }
  
  static u32 calc_chksum(void *buf, size_t size)
  {
  	u32 chksum = 0;
  	u8 *bp = buf;
  	size_t i;
  
  	for (i = 0; i < size; i++)
  		chksum += bp[i];
  
  	return ~chksum;
  }
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
78
79
  static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd,
  		     u32 fw1_start, u32 fw2_start, u32 fw_pages)
49538a259   Shyam Saini   i.MX6: nand: add ...
80
81
82
  {
  	struct nand_chip *chip = mtd_to_nand(mtd);
  	struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
83
84
85
  	struct mxs_nand_layout l;
  
  	mxs_nand_get_layout(mtd, &l);
49538a259   Shyam Saini   i.MX6: nand: add ...
86
87
88
  
  	fcb->fingerprint = FCB_FINGERPRINT;
  	fcb->version = FCB_VERSION_1;
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
89

49538a259   Shyam Saini   i.MX6: nand: add ...
90
91
92
  	fcb->pagesize = mtd->writesize;
  	fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
  	fcb->sectors = mtd->erasesize / mtd->writesize;
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
93
94
95
96
97
98
  	fcb->meta_size = l.meta_size;
  	fcb->nr_blocks = l.nblocks;
  	fcb->ecc_nr = l.data0_size;
  	fcb->ecc_level = l.ecc0;
  	fcb->ecc_size = l.datan_size;
  	fcb->ecc_type = l.eccn;
2651e9fdb   Han Xu   MLK-22723-2: nand...
99
  	fcb->bchtype = l.gf_len;
49538a259   Shyam Saini   i.MX6: nand: add ...
100
101
  
  	/* Also hardcoded in kobs-ng */
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
102
  	if (is_mx6() || is_imx8m()) {
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
103
104
105
106
107
108
109
110
111
112
  		fcb->datasetup = 80;
  		fcb->datahold = 60;
  		fcb->addr_setup = 25;
  		fcb->dsample_time = 6;
  	} else if (is_mx7()) {
  		fcb->datasetup = 10;
  		fcb->datahold = 7;
  		fcb->addr_setup = 15;
  		fcb->dsample_time = 6;
  	}
49538a259   Shyam Saini   i.MX6: nand: add ...
113
114
115
116
117
118
119
120
121
122
  
  	/* DBBT search area starts at second page on first block */
  	fcb->dbbt_start = 1;
  
  	fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
  	fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
  
  	fcb->phy_offset = mtd->writesize;
  
  	fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
a0f6d95cb   Igor Opaniuk   MLK-22444-3: imx:...
123
124
  	fcb->disbbm = 0;
  	fcb->disbbm_search = 0;
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
125
126
127
128
  	fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */
  	fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */
  	fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */
  	fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */
49538a259   Shyam Saini   i.MX6: nand: add ...
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  	fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
  }
  
  static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
  {
  	int n, n_bad_blocks = 0;
  	u32 *bb = buf + 0x8;
  	u32 *n_bad_blocksp = buf + 0x4;
  
  	for (n = 0; n < num_blocks; n++) {
  		loff_t offset = n * mtd->erasesize;
  			if (mtd_block_isbad(mtd, offset)) {
  				n_bad_blocks++;
  				*bb = n;
  				bb++;
  		}
  	}
  
  	*n_bad_blocksp = n_bad_blocks;
  
  	return n_bad_blocks;
  }
1d8a50993   Alice Guo   MLK-22582-2: nand...
151
  static int write_fcb_dbbt_and_readback(struct mtd_info *mtd, struct fcb_block *fcb,
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
  			  struct dbbt_block *dbbt, void *dbbt_data_page,
  			  loff_t off)
  {
  	void *fcb_raw_page = 0;
  	int i, ret;
  	size_t dummy;
  
  	/*
  	 * We prepare raw page only for i.MX6, for i.MX7 we
  	 * leverage BCH hw module instead
  	 */
  	if (is_mx6()) {
  		/* write fcb/dbbt */
  		fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
  				       GFP_KERNEL);
  		if (!fcb_raw_page) {
  			debug("failed to allocate fcb_raw_page
  ");
  			ret = -ENOMEM;
  			return ret;
  		}
  
  		memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
  		encode_hamming_13_8(fcb_raw_page + 12, fcb_raw_page +
  				    12 + 512, 512);
  		/*
  		 * Set the first and second byte of OOB data to 0xFF,
  		 * not 0x00. These bytes are used as the Manufacturers Bad
  		 * Block Marker (MBBM). Since the FCB is mostly written to
  		 * the first page in a block, a scan for
  		 * factory bad blocks will detect these blocks as bad, e.g.
  		 * when function nand_scan_bbt() is executed to build a new
  		 * bad block table.
  		 */
  		memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
  	}
  	for (i = 0; i < 2; i++) {
  		if (mtd_block_isbad(mtd, off)) {
  			printf("Block %d is bad, skipped
  ", i);
  			continue;
  		}
  
  		/*
  		 * User BCH ECC hardware module for i.MX7
  		 */
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
198
  		if (is_mx7() || is_imx8m()) {
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
199
200
  			u32 off = i * mtd->erasesize;
  			size_t rwsize = sizeof(*fcb);
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
201
  			printf("Writing %zd bytes to 0x%x: ", rwsize, off);
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
202
203
204
205
206
  
  			/* switch nand BCH to FCB compatible settings */
  			mxs_nand_mode_fcb(mtd);
  			ret = nand_write(mtd, off, &rwsize,
  					 (unsigned char *)fcb);
1d8a50993   Alice Guo   MLK-22582-2: nand...
207
208
209
210
  
  			dump_fcb_off[i] = off;
  			nand_read(mtd, off, &rwsize,
  				  (unsigned char *)(dump_nand_fcb + i));
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
  			mxs_nand_mode_normal(mtd);
  
  			printf("%s
  ", ret ? "ERROR" : "OK");
  		} else if (is_mx6()) {
  			/* raw write */
  			mtd_oob_ops_t ops = {
  				.datbuf = (u8 *)fcb_raw_page,
  				.oobbuf = ((u8 *)fcb_raw_page) +
  					  mtd->writesize,
  				.len = mtd->writesize,
  				.ooblen = mtd->oobsize,
  				.mode = MTD_OPS_RAW
  			};
  
  			ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
  			if (ret)
  				goto fcb_raw_page_err;
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
229
230
  			debug("NAND fcb write: 0x%x offset 0x%zx written: %s
  ",
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
231
232
233
234
235
236
237
238
  			      mtd->erasesize * i, ops.len, ret ?
  			      "ERROR" : "OK");
  		}
  
  		ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
  				mtd->writesize, &dummy, (void *)dbbt);
  		if (ret)
  			goto fcb_raw_page_err;
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
239
240
  		debug("NAND dbbt write: 0x%x offset, 0x%zx bytes written: %s
  ",
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
241
242
  		      mtd->erasesize * i + mtd->writesize, dummy,
  		      ret ? "ERROR" : "OK");
1d8a50993   Alice Guo   MLK-22582-2: nand...
243
244
245
246
  		dump_dbbt_off[i] = mtd->erasesize * i + mtd->writesize;
  		size_t rwsize = sizeof(*dbbt);
  
  		nand_read(mtd, dump_dbbt_off[i], &rwsize, (unsigned char *)(dump_nand_dbbt + i));
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  		/* dbbtpages == 0 if no bad blocks */
  		if (dbbt->dbbtpages > 0) {
  			loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
  
  			ret = mtd_write(mtd, to, mtd->writesize, &dummy,
  					dbbt_data_page);
  			if (ret)
  				goto fcb_raw_page_err;
  		}
  	}
  
  fcb_raw_page_err:
  	if (is_mx6())
  		kfree(fcb_raw_page);
  
  	return ret;
  }
49538a259   Shyam Saini   i.MX6: nand: add ...
264
265
266
267
268
269
270
  static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
  			  size_t maxsize, const u_char *buf)
  {
  	nand_erase_options_t opts;
  	struct fcb_block *fcb;
  	struct dbbt_block *dbbt;
  	loff_t fw1_off;
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
271
272
  	void *fwbuf, *dbbt_page, *dbbt_data_page;
  	u32 fw1_start, fw1_pages;
49538a259   Shyam Saini   i.MX6: nand: add ...
273
  	int nr_blks, nr_blks_fcb, fw1_blk;
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
274
275
  	size_t fwsize;
  	int ret;
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
276
277
278
  	size_t extra_fwsize;
  	void *extra_fwbuf;
  	loff_t extra_fw1_off;
49538a259   Shyam Saini   i.MX6: nand: add ...
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
  
  	/* erase */
  	memset(&opts, 0, sizeof(opts));
  	opts.offset = off;
  	opts.length = maxsize - 1;
  	ret = nand_erase_opts(mtd, &opts);
  	if (ret) {
  		printf("%s: erase failed (ret = %d)
  ", __func__, ret);
  		return ret;
  	}
  
  	/*
  	 * Reference documentation from i.MX6DQRM section 8.5.2.2
  	 *
  	 * Nand Boot Control Block(BCB) contains two data structures,
  	 * - Firmware Configuration Block(FCB)
  	 * - Discovered Bad Block Table(DBBT)
  	 *
  	 * FCB contains,
  	 * - nand timings
  	 * - DBBT search page address,
  	 * - start page address of primary firmware
  	 * - start page address of secondary firmware
  	 *
  	 * setup fcb:
  	 * - number of blocks = mtd partition size / mtd erasesize
  	 * - two firmware blocks, primary and secondary
  	 * - first 4 block for FCB/DBBT
  	 * - rest split in half for primary and secondary firmware
  	 * - same firmware will write two times
  	 */
1d8a50993   Alice Guo   MLK-22582-2: nand...
311
  	nr_blks_fcb = BOOT_SEARCH_COUNT;
49538a259   Shyam Saini   i.MX6: nand: add ...
312
313
314
315
  	nr_blks = maxsize / mtd->erasesize;
  	fw1_blk = nr_blks_fcb;
  
  	/* write fw */
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
316
317
318
319
320
321
322
323
324
325
326
  	fwbuf = NULL;
  	if (is_mx6() || is_mx7()) {
  		fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
  			       mtd->writesize);
  		fwbuf = kzalloc(fwsize, GFP_KERNEL);
  		if (!fwbuf) {
  			debug("failed to allocate fwbuf
  ");
  			ret = -ENOMEM;
  			goto err;
  		}
49538a259   Shyam Saini   i.MX6: nand: add ...
327

e3c8f8e37   Alice Guo   MLK-22580-4: nand...
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
  		memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
  		fw1_off = fw1_blk * mtd->erasesize;
  		ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
  					  (u_char *)fwbuf, WITH_WR_VERIFY);
  		printf("NAND fw write: 0x%llx offset, 0x%zx bytes written: %s
  ",
  		       fw1_off, fwsize, ret ? "ERROR" : "OK");
  		if (ret)
  			goto fwbuf_err;
  	} else if (is_imx8m()) {
  		fwsize = ALIGN(IMX8MQ_SPL_SZ + FLASH_OFFSET_STANDARD + mtd->writesize, mtd->writesize);
  		fwbuf = kzalloc(fwsize, GFP_KERNEL);
  		if (!fwbuf) {
  			debug("failed to allocate fwbuf
  ");
  			ret = -ENOMEM;
  			goto err;
  		}
  
  		memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, IMX8MQ_SPL_SZ);
  		fw1_off = fw1_blk * mtd->erasesize;
  		ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
  					  (u_char *)fwbuf, WITH_WR_VERIFY);
  		printf("NAND fw write: 0x%llx offset, 0x%zx bytes written: %s
  ",
  		       fw1_off, fwsize, ret ? "ERROR" : "OK");
  		if (ret)
  			goto fwbuf_err;
  
  		extra_fwsize = ALIGN(IMX8MQ_SPL_SZ + mtd->writesize, mtd->writesize);
  		extra_fwbuf = kzalloc(extra_fwsize, GFP_KERNEL);
  		extra_fw1_off = fw1_off + mtd->erasesize * ((IMX8MQ_SPL_SZ + mtd->erasesize - 1) / mtd->erasesize);
  		if (!extra_fwbuf) {
  			debug("failed to allocate fwbuf
  ");
  			ret = -ENOMEM;
  			goto fwbuf_err;
  		}
  
  		memcpy(extra_fwbuf, buf + IMX8MQ_HDMI_FW_SZ, IMX8MQ_SPL_SZ);
  		ret = nand_write_skip_bad(mtd, extra_fw1_off, &extra_fwsize, NULL, maxsize,
  					  (u_char *)extra_fwbuf, WITH_WR_VERIFY);
  		printf("NAND extra_fw write: 0x%llx offset, 0x%zx bytes written: %s
  ",
  		       extra_fw1_off, extra_fwsize, ret ? "ERROR" : "OK");
  		if (ret) {
  			kfree(extra_fwbuf);
  			goto fwbuf_err;
  		}
  	}
49538a259   Shyam Saini   i.MX6: nand: add ...
378
379
380
381
382
383
384
385
386
  
  	/* fill fcb */
  	fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
  	if (!fcb) {
  		debug("failed to allocate fcb
  ");
  		ret = -ENOMEM;
  		goto fwbuf_err;
  	}
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
387
388
  	fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
  	fw1_pages = size / mtd->writesize + 1;
e3c8f8e37   Alice Guo   MLK-22580-4: nand...
389
390
  	if (is_imx8m())
  		fw1_pages = (IMX8MQ_SPL_SZ + (mtd->writesize - 1)) / mtd->writesize;
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
391
  	fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages);
49538a259   Shyam Saini   i.MX6: nand: add ...
392

1d8a50993   Alice Guo   MLK-22582-2: nand...
393
  	dump_fill_fcb = *fcb;
49538a259   Shyam Saini   i.MX6: nand: add ...
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
  	/* fill dbbt */
  	dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
  	if (!dbbt_page) {
  		debug("failed to allocate dbbt_page
  ");
  		ret = -ENOMEM;
  		goto fcb_err;
  	}
  
  	dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
  	if (!dbbt_data_page) {
  		debug("failed to allocate dbbt_data_page
  ");
  		ret = -ENOMEM;
  		goto dbbt_page_err;
  	}
  
  	dbbt = dbbt_page;
  	dbbt->checksum = 0;
  	dbbt->fingerprint = DBBT_FINGERPRINT2;
  	dbbt->version = DBBT_VERSION_1;
  	ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
  	if (ret < 0)
  		goto dbbt_data_page_err;
  	else if (ret > 0)
  		dbbt->dbbtpages = 1;
1d8a50993   Alice Guo   MLK-22582-2: nand...
420
  	dump_fill_dbbt = *dbbt;
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
421
  	/* write fcb and dbbt to nand */
1d8a50993   Alice Guo   MLK-22582-2: nand...
422
  	ret = write_fcb_dbbt_and_readback(mtd, fcb, dbbt, dbbt_data_page, off);
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
423
424
425
  	if (ret < 0)
  		printf("failed to write FCB/DBBT
  ");
49538a259   Shyam Saini   i.MX6: nand: add ...
426

49538a259   Shyam Saini   i.MX6: nand: add ...
427
428
429
430
431
432
433
434
435
436
437
  dbbt_data_page_err:
  	kfree(dbbt_data_page);
  dbbt_page_err:
  	kfree(dbbt_page);
  fcb_err:
  	kfree(fcb);
  fwbuf_err:
  	kfree(fwbuf);
  err:
  	return ret;
  }
a93ccdcee   Igor Opaniuk   MLK-22444-5: imx:...
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
  static int do_nandbcb_bcbonly(int argc, char * const argv[])
  {
  	struct fcb_block *fcb;
  	struct dbbt_block *dbbt;
  	u32 fw_len, fw1_off, fw2_off;
  	struct mtd_info *mtd;
  	void *dbbt_page, *dbbt_data_page;
  	int dev, ret;
  
  	dev = nand_curr_device;
  	if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
  	    (!get_nand_dev_by_index(dev))) {
  		puts("No devices available
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	mtd = get_nand_dev_by_index(dev);
  
  	if (argc < 3)
  		return CMD_RET_FAILURE;
  
  	fw_len = simple_strtoul(argv[1], NULL, 16);
  	fw1_off = simple_strtoul(argv[2], NULL, 16);
  
  	if (argc > 3)
  		fw2_off = simple_strtoul(argv[3], NULL, 16);
  	else
  		fw2_off = fw1_off;
  
  	/* fill fcb */
  	fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
  	if (!fcb) {
  		debug("failed to allocate fcb
  ");
  		ret = -ENOMEM;
  		return CMD_RET_FAILURE;
  	}
  
  	fill_fcb(fcb, mtd, fw1_off / mtd->writesize,
  		 fw2_off / mtd->writesize, fw_len / mtd->writesize);
  
  	/* fill dbbt */
  	dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
  	if (!dbbt_page) {
  		debug("failed to allocate dbbt_page
  ");
  		ret = -ENOMEM;
  		goto fcb_err;
  	}
  
  	dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
  	if (!dbbt_data_page) {
  		debug("failed to allocate dbbt_data_page
  ");
  		ret = -ENOMEM;
  		goto dbbt_page_err;
  	}
  
  	dbbt = dbbt_page;
  	dbbt->checksum = 0;
  	dbbt->fingerprint = DBBT_FINGERPRINT2;
  	dbbt->version = DBBT_VERSION_1;
  	ret = dbbt_fill_data(mtd, dbbt_data_page, 0);
  	if (ret < 0)
  		goto dbbt_data_page_err;
  	else if (ret > 0)
  		dbbt->dbbtpages = 1;
  
  	/* write fcb and dbbt to nand */
1d8a50993   Alice Guo   MLK-22582-2: nand...
508
  	ret = write_fcb_dbbt_and_readback(mtd, fcb, dbbt, dbbt_data_page, 0);
a93ccdcee   Igor Opaniuk   MLK-22444-5: imx:...
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
  dbbt_data_page_err:
  	kfree(dbbt_data_page);
  dbbt_page_err:
  	kfree(dbbt_page);
  fcb_err:
  	kfree(fcb);
  
  	if (ret < 0) {
  		printf("failed to write FCB/DBBT
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	return CMD_RET_SUCCESS;
  }
1d8a50993   Alice Guo   MLK-22582-2: nand...
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
  /* dump data which is planned to be encoded and written to NAND chip */
  void mtd_cfg_dump(void)
  {
  	u64 blocks;
  
  	printf("MTD CONFIG:
  ");
  	printf("  %s = %d
  ", "data_setup_time", dump_fill_fcb.datasetup);
  	printf("  %s = %d
  ", "data_hold_time", dump_fill_fcb.datahold);
  	printf("  %s = %d
  ", "address_setup_time", dump_fill_fcb.addr_setup);
  	printf("  %s = %d
  ", "data_sample_time", dump_fill_fcb.dsample_time);
  
  	printf("NFC geometry :
  ");
  	printf("\tECC Strength       : %d
  ", dump_mtd->ecc_strength);
  	printf("\tPage Size in Bytes : %d
  ", dump_fill_fcb.oob_pagesize);
  	printf("\tMetadata size      : %d
  ", dump_fill_fcb.meta_size);
  	printf("\tECC Chunk Size in byte : %d
  ", dump_fill_fcb.ecc_size);
  	printf("\tECC Chunk count        : %d
  ", dump_fill_fcb.nr_blocks + 1);
  	printf("\tBlock Mark Byte Offset : %d
  ", dump_fill_fcb.bb_byte);
  	printf("\tBlock Mark Bit Offset  : %d
  ", dump_fill_fcb.bb_start_bit);
  	printf("====================================================
  ");
  
  	printf("mtd: partition #0
  ");
  	printf("  %s = %d
  ", "type", dump_mtd->type);
  	printf("  %s = %d
  ", "flags", dump_mtd->flags);
  	printf("  %s = %llu
  ", "size", dump_nandboot_size);
  	printf("  %s = %d
  ", "erasesize", dump_mtd->erasesize);
  	printf("  %s = %d
  ", "writesize", dump_mtd->writesize);
  	printf("  %s = %d
  ", "oobsize", dump_mtd->oobsize);
  	blocks = dump_nandboot_size;
  	do_div(blocks, dump_mtd->erasesize);
  	printf("  %s = %llu
  ", "blocks", blocks);
  }
  
  /* dump data which is read from NAND chip */
  void mtd_dump_structure(int i)
  {
  	#define P1(x) printf("  %s = 0x%08x
  ", #x, dump_nand_fcb[i].x)
  		printf("FCB %d:
  ", i);
  		P1(checksum);
  		P1(fingerprint);
  		P1(version);
  	#undef P1
  	#define P1(x)	printf("  %s = %d
  ", #x, dump_nand_fcb[i].x)
  		P1(datasetup);
  		P1(datahold);
  		P1(addr_setup);
  		P1(dsample_time);
  		P1(pagesize);
  		P1(oob_pagesize);
  		P1(sectors);
  		P1(nr_nand);
  		P1(nr_die);
  		P1(celltype);
  		P1(ecc_type);
  		P1(ecc_nr);
  		P1(ecc_size);
  		P1(ecc_level);
  		P1(meta_size);
  		P1(nr_blocks);
  		P1(ecc_type_sdk);
  		P1(ecc_nr_sdk);
  		P1(ecc_size_sdk);
  		P1(ecc_level_sdk);
  		P1(nr_blocks_sdk);
  		P1(meta_size_sdk);
  		P1(erase_th);
  		P1(bootpatch);
  		P1(patch_size);
  		P1(fw1_start);
  		P1(fw2_start);
  		P1(fw1_pages);
  		P1(fw2_pages);
  		P1(dbbt_start);
  		P1(bb_byte);
  		P1(bb_start_bit);
  		P1(phy_offset);
  		P1(bchtype);
  		P1(readlatency);
  		P1(predelay);
  		P1(cedelay);
  		P1(postdelay);
  		P1(cmdaddpause);
  		P1(datapause);
  		P1(tmspeed);
  		P1(busytimeout);
  		P1(disbbm);
  		P1(spare_offset);
  		P1(onfi_sync_enable);
  		P1(onfi_sync_speed);
  		P1(onfi_sync_nand_data);
  		P1(disbbm_search);
  		P1(disbbm_search_limit);
  		P1(read_retry_enable);
  	#undef P1
  	#define P1(x)	printf("  %s = 0x%08x
  ", #x, dump_nand_dbbt[i].x)
  		printf("DBBT %d:
  ", i);
  		P1(checksum);
  		P1(fingerprint);
  		P1(version);
  	#undef P1
  	#define P1(x)	printf("  %s = %d
  ", #x, dump_nand_dbbt[i].x)
  		P1(numberbb);
  	#undef P1
  
  	printf("Firmware: image #0 @ 0x%x size 0x%x - available 0x%llx
  ",
  	       dump_nand_fcb[i].fw1_start * dump_nand_fcb[i].pagesize,
  	       dump_nand_fcb[i].fw1_pages * dump_nand_fcb[i].pagesize,
  	       dump_nandboot_size - dump_nand_fcb[i].fw1_start * dump_nand_fcb[i].pagesize);
  	if (is_imx8m())
  		printf("Extra Firmware: image #0 @ 0x%x size 0x%x - available 0x%llx
  ",
  		       dump_nand_fcb[i].fw1_start * dump_nand_fcb[i].pagesize + dump_mtd->erasesize * ((IMX8MQ_SPL_SZ + dump_mtd->erasesize - 1) / dump_mtd->erasesize),
  		       dump_nand_fcb[i].fw1_pages * dump_nand_fcb[i].pagesize,
  		       dump_nandboot_size - (dump_nand_fcb[i].fw1_start * dump_nand_fcb[i].pagesize + dump_mtd->erasesize * ((IMX8MQ_SPL_SZ + dump_mtd->erasesize - 1) / dump_mtd->erasesize)));
  }
  
  static int do_nandbcb_dump(int argc, char * const argv[])
  {
  	int num;
  	int stride;
  	int search_area_sz;
  	bool bab_block_table[BOOT_SEARCH_COUNT];
  	int bab_block_flag;
  
  	if (argc != 2)
  		return CMD_RET_USAGE;
  
  	switch (argv[1][0]) {
  	case '0':
  		num = 0;
  		break;
  	case '1':
  		num = 1;
  		break;
  	default:
  		return CMD_RET_USAGE;
  	}
  
  	/* dump data which is planned to be encoded and written to NAND chip */
  	mtd_cfg_dump();
  
  	stride = dump_mtd->erasesize;
  	search_area_sz = BOOT_SEARCH_COUNT * stride;
  	printf("stride: %x, search_area_sz: %x
  ", stride, search_area_sz);
  
  	bab_block_flag = 0;
  	for (int i = 0; i < BOOT_SEARCH_COUNT; i++) {
  		if (mtd_block_isbad(dump_mtd, (loff_t)(dump_mtd->erasesize * i))) {
  			bab_block_table[i] = 1;
  			bab_block_flag = 1;
  			continue;
  		}
  		bab_block_table[i] = 0;
  		if (memcpy(dump_nand_fcb + i, &dump_fill_fcb, sizeof(dump_fill_fcb)) != 0) {
  			printf("mtd: found FCB%d candidate version %08x @%d:0x%x
  ",
  			       i, dump_nand_fcb[i].version, i, dump_fcb_off[i]);
  		} else {
  			printf("mtd: FCB%d not found
  ", i);
  		}
  	}
  
  	for (int i = 0; i < BOOT_SEARCH_COUNT; i++) {
  		if (mtd_block_isbad(dump_mtd, (loff_t)(dump_mtd->erasesize * i)))
  			continue;
  
  		if (memcpy(dump_nand_dbbt + i, &dump_fill_dbbt, sizeof(dump_fill_dbbt)) != 0) {
  			printf("mtd: DBBT%d found
  ", i);
  			printf("mtd: Valid DBBT%d found @%d:0x%x
  ", i, i, dump_dbbt_off[i]);
  
  		} else {
  			printf("mtd: DBBT%d not found
  ", i);
  		}
  	}
  	if (bab_block_flag == 0)
  		printf("no bad block found, dbbt: %08x
  ", dump_fill_dbbt.fingerprint);
  	else
  		for (int i = 0; i < BOOT_SEARCH_COUNT; i++)
  			if (bab_block_table[i] == 1)
  				printf("mtd: bad block @ 0x%llx
  ", (loff_t)(dump_mtd->erasesize * i));
  
  	/* dump data which is read from NAND chip */
  	if (num > (BOOT_SEARCH_COUNT - 1))
  		return CMD_RET_USAGE;
  
  	if (bab_block_table[num] == 1) {
  		printf("mtd: bad block @ 0x%llx (FCB - DBBT)
  ", (loff_t)(dump_mtd->erasesize * num));
  		return CMD_RET_USAGE;
  	}
  
  	mtd_dump_structure(num);
  
  	return 0;
  }
49538a259   Shyam Saini   i.MX6: nand: add ...
755
756
757
758
759
760
761
762
763
764
765
766
767
768
  static int do_nandbcb_update(int argc, char * const argv[])
  {
  	struct mtd_info *mtd;
  	loff_t addr, offset, size, maxsize;
  	char *endp;
  	u_char *buf;
  	int dev;
  	int ret;
  
  	if (argc != 4)
  		return CMD_RET_USAGE;
  
  	dev = nand_curr_device;
  	if (dev < 0) {
d0936860e   Igor Opaniuk   MLK-22444-4: imx:...
769
770
  		printf("failed to get nand_curr_device, run nand device
  ");
49538a259   Shyam Saini   i.MX6: nand: add ...
771
772
773
774
775
776
777
778
779
780
781
  		return CMD_RET_FAILURE;
  	}
  
  	addr = simple_strtoul(argv[1], &endp, 16);
  	if (*argv[1] == 0 || *endp != 0)
  		return CMD_RET_FAILURE;
  
  	mtd = get_nand_dev_by_index(dev);
  	if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
  			     &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
  		return CMD_RET_FAILURE;
1d8a50993   Alice Guo   MLK-22582-2: nand...
782
783
784
  	/* dump_mtd and dump_nandboot_size are used for "nandbcb dump [-v]" */
  	dump_mtd = mtd;
  	dump_nandboot_size = maxsize;
49538a259   Shyam Saini   i.MX6: nand: add ...
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
  	buf = map_physmem(addr, size, MAP_WRBACK);
  	if (!buf) {
  		puts("failed to map physical memory
  ");
  		return CMD_RET_FAILURE;
  	}
  
  	ret = nandbcb_update(mtd, offset, size, maxsize, buf);
  
  	return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
  }
  
  static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
  		      char * const argv[])
  {
  	const char *cmd;
  	int ret = 0;
1d8a50993   Alice Guo   MLK-22582-2: nand...
802
  	if (argc < 3)
49538a259   Shyam Saini   i.MX6: nand: add ...
803
804
805
806
807
808
809
810
811
812
  		goto usage;
  
  	cmd = argv[1];
  	--argc;
  	++argv;
  
  	if (strcmp(cmd, "update") == 0) {
  		ret = do_nandbcb_update(argc, argv);
  		goto done;
  	}
1d8a50993   Alice Guo   MLK-22582-2: nand...
813
814
815
816
  	if (strcmp(cmd, "dump") == 0) {
  		ret = do_nandbcb_dump(argc, argv);
  		goto done;
  	}
a93ccdcee   Igor Opaniuk   MLK-22444-5: imx:...
817
818
819
820
  	if (strcmp(cmd, "bcbonly") == 0) {
  		ret = do_nandbcb_bcbonly(argc, argv);
  		goto done;
  	}
49538a259   Shyam Saini   i.MX6: nand: add ...
821
822
823
824
825
826
827
828
829
830
  done:
  	if (ret != -1)
  		return ret;
  usage:
  	return CMD_RET_USAGE;
  }
  
  static char nandbcb_help_text[] =
  	"update addr off|partition len	- update 'len' bytes starting at
  "
a93ccdcee   Igor Opaniuk   MLK-22444-5: imx:...
831
832
833
834
835
836
  	"        'off|part' to memory address 'addr', skipping  bad blocks
  "
  	"bcbonly fw-size fw1-off [fw2-off] - write only BCB (FCB and DBBT)
  "
  	"        where `fw-size` is fw sizes in bytes, `fw1-off` and
  "
1d8a50993   Alice Guo   MLK-22582-2: nand...
837
838
839
840
841
  	"        and `fw2-off` - firmware offsets		
  "
  	"nandbcb dump num - verify/dump boot structures
  "
  	"	'num' can be set to 0 and 1";
49538a259   Shyam Saini   i.MX6: nand: add ...
842
843
844
845
846
  
  U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
  	   "i.MX6 Nand BCB",
  	   nandbcb_help_text
  );