summaryrefslogtreecommitdiff
path: root/arch/arm/imx-common/cmd_writebcb_mx7.c
blob: 6dfe7a22e7ac30240bf4cdd2bd1df80f8a7268c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
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
/*
 * Copyright (c) 2013, Toradex AG.  All rights reserved.
 *
 * Derived from downstream U-Boot (drivers/mtd/nand/fsl_nfc.c)
 * and mxsboot
 *
 * SPDX-License-Identifier:	GPL-2.0+
 */

/* NAND LAYOUT
 *
 * Content	FCB CFG_BLOCK .. FCB     CFG_BLOCK  DBBT .. DBBT
 * Block/Page	0/0 0/1       .. 0/(n-1) 1/(n-1)    0/n  .. 0/n + (n-1)
 *
 * With n = BOOT_SEARCH_COUNT
 *
 * With n = 2 the NAND is used starting at 0 with a length of 0x80000
 *
 * Usually one will use fw_start1 at 0x80000 and fw_start2 at 0x200000
 * allowing for at least 6 bad blocks in either bootloader
 */

#include <common.h>
#include <linux/bch.h>
#include <malloc.h>
#include <nand.h>

#ifdef CONFIG_MX7
/*
 * compare with v5_rom_mtd_commit_structures vs. v6_rom_mtd_commit_structures
 * in imx-kobs/src/mtd.c
 */
#define USE_RANDOMIZER
#define USE_62_BIT_ECC
#endif

#ifdef USE_RANDOMIZER
#include "rand.h"
#define RAND_16K		(16 * 1024)
#endif

#define PAGES_PER_STRIDE	64

#define BOOT_SEARCH_COUNT	2 /* match with BOOT_CFG_FUSES [6:5] */

extern int raw_access(struct mtd_info *info, ulong addr, loff_t off,
		      ulong count, int read);

struct mxs_nand_fcb {
	uint32_t		checksum;
	uint32_t		fingerprint;
	uint32_t		version;
	struct {
		uint8_t			data_setup;
		uint8_t			data_hold;
		uint8_t			address_setup;
		uint8_t			dsample_time;
		uint8_t			nand_timing_state;
		uint8_t			rea;
		uint8_t			rloh;
		uint8_t			rhoh;
	}			timing;
	uint32_t		page_data_size;
	uint32_t		total_page_size;
	uint32_t		sectors_per_block;
	uint32_t		number_of_nands;		/* Ignored */
	uint32_t		total_internal_die;		/* Ignored */
	uint32_t		cell_type;			/* Ignored */
	uint32_t		ecc_block_n_ecc_type;
	uint32_t		ecc_block_0_size;
	uint32_t		ecc_block_n_size;
	uint32_t		ecc_block_0_ecc_type;
	uint32_t		metadata_bytes;
	uint32_t		num_ecc_blocks_per_page;
	uint32_t		ecc_block_n_ecc_level_sdk;	/* Ignored */
	uint32_t		ecc_block_0_size_sdk;		/* Ignored */
	uint32_t		ecc_block_n_size_sdk;		/* Ignored */
	uint32_t		ecc_block_0_ecc_level_sdk;	/* Ignored */
	uint32_t		num_ecc_blocks_per_page_sdk;	/* Ignored */
	uint32_t		metadata_bytes_sdk;		/* Ignored */
	uint32_t		erase_threshold;
	uint32_t		boot_patch;			/* not iMX7 */
	uint32_t		patch_sectors;			/* not iMX7 */
	uint32_t		firmware1_starting_sector;
	uint32_t		firmware2_starting_sector;
	uint32_t		sectors_in_firmware1;
	uint32_t		sectors_in_firmware2;
	uint32_t		dbbt_search_area_start_address;
	uint32_t		badblock_marker_byte;
	uint32_t		badblock_marker_start_bit;
	uint32_t		bb_marker_physical_offset;
	uint32_t		bch_type;			/* only iMX7 */
	uint32_t		tm_timing2_read_latency;	/* only iMX7 */
	uint32_t		tm_timing2_preambel_delay;	/* only iMX7 */
	uint32_t		tm_timing2_ce_delay;		/* only iMX7 */
	uint32_t		tm_timing2_postamble_delay;	/* only iMX7 */
	uint32_t		tm_timing2_cmd_add_pause;	/* only iMX7 */
	uint32_t		tm_timing2_data_pause;		/* only iMX7 */
	uint32_t		tm_speed;			/* only iMX7 */
	uint32_t		tm_timing1_busy_timeout;	/* only iMX7 */
	uint32_t		disbbm;
	uint32_t		bbmark_spare_offset;		/* only iMX7 */
	uint32_t		onfi_sync_enable;		/* only iMX7 */
	uint32_t		onfi_sync_speed;		/* only iMX7 */
	uint32_t		onfi_sync_nand_data;		/* only iMX7 */
	uint32_t		reserved2[6];
	uint32_t		disbbm_search;
	uint32_t		disbbm_search_limit;
	uint32_t		reserved3[15];			/* only iMX7 */
	uint32_t		read_retry_enable;		/* only iMX7 */
	uint32_t		reserved4[1];			/* only iMX7 */
	uint32_t		fill_to_1024[183];
};

struct mxs_nand_dbbt {
	uint32_t		checksum;
	uint32_t		fingerprint;
	uint32_t		version;
	uint32_t		number_bb;
	uint32_t		number_2k_pages_bb;
};

struct mxs_nand_bbt {
	uint32_t		nand;
	uint32_t		number_bb;
	uint32_t		badblock[510];
};


#ifdef DEBUG
#include <linux/ctype.h>
/* static */
void dump(const void *data, int size)
{
	int i, j;
	const uint8_t *s;

	s = data;
	for (i = j = 0; i < size; i += 16) {
		if (i)
			printf("\n");
		printf("[%04x]", i);

		for (j = i; j < i + 16; j++) {
			if (j < size)
				printf(" %02x", s[j]);
			else
				printf("   ");
			if (j == i + 7)
				printf(" ");
		}


		printf(" | ");

		for (j = i; j < i + 16; j ++) {
			if (j < size)
				printf("%c", isprint(s[j]) ? s[j] : '.');
			else
				printf(" ");
			if (j == i + 7)
				printf("-");
		}
	}
	printf("\n");
}
#endif

/*
 * reverse bit for byte
 */
static uint8_t reverse_bit(uint8_t in_byte)
{
	int i;
	uint8_t out_byte = 0;

	for (i = 0; i < 8; i++) {
		if (in_byte & ((0x80) >> i)) {
			out_byte |= 1 << i;
		}
	}

	return out_byte;
}

int encode_bch_ecc(void *source_block, size_t source_size, void *target_block,
		   size_t target_size)
{
	struct bch_control *bch;
	unsigned char *ecc_buf;
	int ecc_buf_size;
	unsigned char *tmp_buf;
	int tmp_buf_size;
	int real_buf_size;
	int i, j;
	int ecc_bit_off;
	int data_ecc_blk_size;
	int low_byte_off, low_bit_off;
	int high_byte_off, high_bit_off;
	unsigned char byte_low, byte_high;

	/* define the variables for bch algorithm*/
	/* m:  METADATABYTE */
	/* b0: BLOCK0BYTE */
	/* e0: BLOCK0ECC */
	/* bn: BLOCKNBYTE */
	/* en: BLOCKNECC */
	/* n : NUMOFBLOCKN */
	/* gf: FCB_GF */
	int m, b0, e0, bn, en, n, gf;

#ifdef USE_62_BIT_ECC
	/* 62 bit BCH, for i.MX6SX and i.MX7D */
	m = 32;
	b0 = 128;
	e0 = 62;
	bn = 128;
	en = 62;
	n = 7;
	gf = 13;
#else
	/* 40 bit BCH, for i.MX6UL */
	m  = 32;
	b0 = 128;
	e0 = 40;
	bn = 128;
	en = 40;
	n  = 7;
	gf = 13;
#endif

	/* sanity check */
	/* nand data block must be large enough for FCB structure */
	if(source_size > b0 + n * bn)
		return -EINVAL;
	/* nand page need to be large enough to contain Meta, FCB and ECC */
	if(target_size < m + b0 + e0 * gf / 8 + n * bn + n * en * gf / 8)
		return -EINVAL;

	/* init bch, using default polynomial */
	bch = init_bch(gf, en, 0);
	if(!bch)
		return -EINVAL;

	/* buffer for ecc */
	ecc_buf_size = (gf * en + 7) / 8;
	ecc_buf = malloc(ecc_buf_size);
	if(!ecc_buf)
		return -EINVAL;

	/* temp buffer to store data and ecc */
	tmp_buf_size = b0 + (e0 * gf + 7) / 8 + (bn + (en * gf + 7) / 8) * n;
	tmp_buf = malloc(tmp_buf_size);
	if(!tmp_buf)
		return -EINVAL;
	memset(tmp_buf, 0, tmp_buf_size);

	/* generate ecc code for each data block and store in temp buffer */

	for(i = 0; i < n + 1; i++) {
		memset(ecc_buf, 0, ecc_buf_size);
		encode_bch(bch, source_block + i * bn, bn, ecc_buf);

		memcpy(tmp_buf + i * (bn + ecc_buf_size), source_block + i * bn,
				bn);

		/* reverse ecc bit */
		for(j = 0; j < ecc_buf_size; j++) {
			ecc_buf[j] = reverse_bit(ecc_buf[j]);
		}

		memcpy(tmp_buf + (i + 1) * bn + i * ecc_buf_size, ecc_buf,
				ecc_buf_size);
	}

	/* store Metadata for taget block with randomizer*/
	/*memcpy(target_block, RandData, m);*/
	memset(target_block, 0, m);

	/* shift the bit to combine the source data and ecc */
	real_buf_size = (b0 * 8 + gf * e0 + (bn * 8 + gf * en) * n) / 8;

	if(!((gf * en) % 8)) {
		/* ecc data is byte aligned, just copy it. */
		memcpy(target_block + m, tmp_buf, real_buf_size);
	} else {
		/* bit offset for each ecc block */
		ecc_bit_off = 8 - (gf * en) % 8;
		/* size of a data block plus ecc block */
		data_ecc_blk_size = bn + (gf * en + 7) / 8;

		for(i = 0; i < real_buf_size; i++) {
			low_bit_off = ((i / data_ecc_blk_size) * ecc_bit_off)
					% 8;
			low_byte_off = ((i / data_ecc_blk_size) * ecc_bit_off)
					/ 8;
			high_bit_off = (((i + 1) / data_ecc_blk_size)
					* ecc_bit_off) % 8;
			high_byte_off = (((i + 1) / data_ecc_blk_size)
					* ecc_bit_off) / 8;

			byte_low = tmp_buf[i + low_byte_off] >> low_bit_off;
			byte_high = tmp_buf[i + 1 + high_byte_off]
					<< (8 - high_bit_off);

			*(uint8_t *)(target_block + i + m) = (byte_low
					| byte_high);
		}
	}

	free(ecc_buf);
	free(tmp_buf);
	return 0;
}

static void create_fcb(struct mtd_info *nand, uint8_t *buf,
		       int fw1_start_address, int fw2_start_address)
{
	int i;
	struct mxs_nand_fcb _fcb;
	struct mxs_nand_fcb *fcb = &_fcb;
	memset (fcb, 0, sizeof(struct mxs_nand_fcb));

	fcb->fingerprint =		0x20424346;
	fcb->version =			0x01000000;
	fcb->timing.data_setup =	10;
	fcb->timing.data_hold =		7;
	fcb->timing.address_setup =	15;
	fcb->timing.dsample_time =	6;
	
	fcb->page_data_size =		nand->writesize;
	fcb->total_page_size =		nand->writesize + nand->oobsize;
	fcb->sectors_per_block =	nand->erasesize / nand->writesize;

	fcb->ecc_block_n_ecc_type =	4;
	fcb->ecc_block_0_size =		512;
	fcb->ecc_block_n_size =		512;
	fcb->ecc_block_0_ecc_type =	4;
	fcb->metadata_bytes =		10;
	fcb->num_ecc_blocks_per_page =	3;

	fcb->firmware1_starting_sector = fw1_start_address / nand->writesize;
	fcb->firmware2_starting_sector = fw2_start_address / nand->writesize;

	fcb->dbbt_search_area_start_address =	256;
	fcb->badblock_marker_byte =	1999;

	/* This is typically the first byte of the pages OOB area */
	fcb->bb_marker_physical_offset = nand->writesize;

	/* Required for successful factory bad block detection */
	fcb->disbbm = 0;
	fcb->disbbm_search = 0;

	/* compute checksum, ~(sum of bytes starting with offset 4) */
	for (i = 4; i < sizeof(struct mxs_nand_fcb); i++)
		fcb->checksum += *((char *) fcb + i);
	fcb->checksum ^= 0xffffffff;
	encode_bch_ecc(fcb, sizeof(struct mxs_nand_fcb), buf,
		       fcb->total_page_size);
#ifdef DEBUG
	printf("FCB\n"); dump(fcb, sizeof(struct mxs_nand_fcb));
	printf("Encoded\n"); dump(buf, 512);
#endif
}

static void create_dbbt(struct mtd_info *nand, uint8_t *buf)
{
	int i;
	struct mxs_nand_dbbt *dbbt = (struct mxs_nand_dbbt *)buf;

	memset (buf, 0, sizeof(struct mxs_nand_dbbt));
	dbbt->fingerprint = 0x54424244;
	dbbt->version = 0x01000000;
	dbbt->number_bb = 0;
	dbbt->number_2k_pages_bb = 0;

	/* compute checksum, ~(sum of bytes starting with offset 4) */
	for (i = 4; i < sizeof(struct mxs_nand_dbbt); i++)
		dbbt->checksum += *((char *) dbbt + i);
	dbbt->checksum ^= 0xffffffff;

#ifdef DEBUG
	printf("DBBT\n"); dump(dbbt, sizeof(struct mxs_nand_dbbt));
#endif
}

static int do_write_bcb(cmd_tbl_t *cmdtp, int flag, int argc,
			char * const argv[])
{
	int j, ret;
	uint8_t *buf;
	size_t rwsize, maxsize;
	ulong fw1_off, fw2_off;
	ulong off;
	struct mtd_info *nand;

	int dev = nand_curr_device;

	if (argc < 2)
		return -1;

	fw1_off = simple_strtoul(argv[1], NULL, 16);
	if (argc > 2)
		fw2_off = simple_strtoul(argv[2], NULL, 16);
	else
		fw2_off = 0;

	nand = nand_info[dev];

	/* Allocate one page, should be enought */
	rwsize = nand->writesize;
	buf = malloc(nand->writesize + nand->oobsize);

	/* Set only the first page empty... */
	memset(buf, 0xff, nand->writesize + nand->oobsize);

	create_fcb(nand, buf, fw1_off, fw2_off);

	puts("Write FCB...\n");
	rwsize = maxsize = nand->writesize;

	off = 0;
	for (j = 0; j < BOOT_SEARCH_COUNT; j++) {
#ifdef USE_RANDOMIZER
		int k;
		/* do randomizer */
		for (k = 0; k < nand->writesize + nand->oobsize; k++) {
			*(uint8_t *)(buf + k) ^=
			RandData[k + ((j * PAGES_PER_STRIDE) % 256)
			/ 64 * RAND_16K];
		}
		/* set BBM to good so we don't need nand scrub */
		*(uint8_t *)(buf + nand->writesize) = 0xff;
#ifdef DEBUG
		printf("Randomized\n"); dump(buf, 512);
#endif
#endif /* USE_RANDOMIZER */
		ret = raw_access(nand, (ulong) buf, off, 1, 0);
#ifdef USE_RANDOMIZER
		/* revert randomizer */
		for (k = 0; k < nand->writesize + nand->oobsize; k++) {
			*(uint8_t *)(buf + k) ^=
			RandData[k + ((j * PAGES_PER_STRIDE) % 256)
			/ 64 * RAND_16K];
		}
#endif /* USE_RANDOMIZER */

		printf("FCB %d bytes written to 0x%x: %s\n", rwsize,
				(unsigned) off, ret ? "ERROR" : "OK");
		off += nand->erasesize;
	}
	memset (buf, 0xff, nand->writesize + nand->oobsize);
	create_dbbt(nand, buf);

	puts("Write DBBT...\n");
	off = BOOT_SEARCH_COUNT * nand->erasesize;
	for(j = 0; j < BOOT_SEARCH_COUNT; j++) {
		ret = nand_write(nand, off, &rwsize, (unsigned char *)buf);
		printf("DBBT %d bytes written to 0x%x: %s\n", rwsize,
				(unsigned) off, ret ? "ERROR" : "OK");
		off += nand->erasesize;
	}

	return 0;
}

U_BOOT_CMD(
	writebcb, 3, 0, do_write_bcb,
	"Write Boot Control Block (FCB and DBBT)",
	"fw1-off [fw2-off]"
);