blob: a3a44bc284695ad8a7ce9d0d93167156eaaef73b [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>
13#include <nand.h>
14
15#include <asm/io.h>
16#include <jffs2/jffs2.h>
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +020017#include <linux/bch.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053018#include <linux/mtd/mtd.h>
19
Igor Opaniukdad30dd2019-11-03 16:49:44 +010020#include <asm/arch/sys_proto.h>
Shyam Saini1d43e242019-06-14 13:05:33 +053021#include <asm/mach-imx/imx-nandbcb.h>
22#include <asm/mach-imx/imximage.cfg>
23#include <mxs_nand.h>
24#include <linux/mtd/mtd.h>
25#include <nand.h>
26
27#define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
28#define GETBIT(v, n) (((v) >> (n)) & 0x1)
29
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +020030#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
31static uint8_t reverse_bit(uint8_t b)
32{
33 b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
34 b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
35 b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
36
37 return b;
38}
39
40static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
41{
42 int i, j, m = 13;
43 int blocksize = 128;
44 int numblocks = 8;
45 int ecc_buf_size = (m * eccbits + 7) / 8;
46 struct bch_control *bch = init_bch(m, eccbits, 0);
47 u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
48 u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
49 u8 *psrc, *pdst;
50
51 /*
52 * The blocks here are bit aligned. If eccbits is a multiple of 8,
53 * we just can copy bytes. Otherwiese we must move the blocks to
54 * the next free bit position.
55 */
56 WARN_ON(eccbits % 8);
57
58 memcpy(tmp_buf, fcb, sizeof(*fcb));
59
60 for (i = 0; i < numblocks; i++) {
61 memset(ecc_buf, 0, ecc_buf_size);
62 psrc = tmp_buf + i * blocksize;
63 pdst = buf + i * (blocksize + ecc_buf_size);
64
65 /* copy data byte aligned to destination buf */
66 memcpy(pdst, psrc, blocksize);
67
68 /*
69 * imx-kobs use a modified encode_bch which reverse the
70 * bit order of the data before calculating bch.
71 * Do this in the buffer and use the bch lib here.
72 */
73 for (j = 0; j < blocksize; j++)
74 psrc[j] = reverse_bit(psrc[j]);
75
76 encode_bch(bch, psrc, blocksize, ecc_buf);
77
78 /* reverse ecc bit */
79 for (j = 0; j < ecc_buf_size; j++)
80 ecc_buf[j] = reverse_bit(ecc_buf[j]);
81
82 /* Here eccbuf is byte aligned and we can just copy it */
83 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
84 }
85
86 kfree(ecc_buf);
87 kfree(tmp_buf);
88 free_bch(bch);
89}
90#else
91
Shyam Saini1d43e242019-06-14 13:05:33 +053092static u8 calculate_parity_13_8(u8 d)
93{
94 u8 p = 0;
95
96 p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
97 p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
98 GETBIT(d, 1)) << 1;
99 p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
100 GETBIT(d, 0)) << 2;
101 p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
102 p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
103 GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
104
105 return p;
106}
107
108static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
109{
110 int i;
111 u8 *src = _src;
112 u8 *ecc = _ecc;
113
114 for (i = 0; i < size; i++)
115 ecc[i] = calculate_parity_13_8(src[i]);
116}
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200117#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530118
119static u32 calc_chksum(void *buf, size_t size)
120{
121 u32 chksum = 0;
122 u8 *bp = buf;
123 size_t i;
124
125 for (i = 0; i < size; i++)
126 chksum += bp[i];
127
128 return ~chksum;
129}
130
131static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd)
132{
133 struct nand_chip *chip = mtd_to_nand(mtd);
134 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100135 struct mxs_nand_layout l;
136
137 mxs_nand_get_layout(mtd, &l);
Shyam Saini1d43e242019-06-14 13:05:33 +0530138
139 fcb->fingerprint = FCB_FINGERPRINT;
140 fcb->version = FCB_VERSION_1;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100141
Shyam Saini1d43e242019-06-14 13:05:33 +0530142 fcb->pagesize = mtd->writesize;
143 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
144 fcb->sectors = mtd->erasesize / mtd->writesize;
145
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100146 fcb->meta_size = l.meta_size;
147 fcb->nr_blocks = l.nblocks;
148 fcb->ecc_nr = l.data0_size;
149 fcb->ecc_level = l.ecc0;
150 fcb->ecc_size = l.datan_size;
151 fcb->ecc_type = l.eccn;
Shyam Saini1d43e242019-06-14 13:05:33 +0530152
153 /* Also hardcoded in kobs-ng */
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100154 if (is_mx6()) {
155 fcb->datasetup = 80;
156 fcb->datahold = 60;
157 fcb->addr_setup = 25;
158 fcb->dsample_time = 6;
159 } else if (is_mx7()) {
160 fcb->datasetup = 10;
161 fcb->datahold = 7;
162 fcb->addr_setup = 15;
163 fcb->dsample_time = 6;
164 }
Shyam Saini1d43e242019-06-14 13:05:33 +0530165
166 /* DBBT search area starts at second page on first block */
167 fcb->dbbt_start = 1;
168
169 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
170 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
171
172 fcb->phy_offset = mtd->writesize;
173
174 fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
175
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100176 fcb->disbbm = 0;
177 fcb->disbbm_search = 0;
178
Shyam Saini1d43e242019-06-14 13:05:33 +0530179 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
180}
181
182static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
183{
184 int n, n_bad_blocks = 0;
185 u32 *bb = buf + 0x8;
186 u32 *n_bad_blocksp = buf + 0x4;
187
188 for (n = 0; n < num_blocks; n++) {
189 loff_t offset = n * mtd->erasesize;
190 if (mtd_block_isbad(mtd, offset)) {
191 n_bad_blocks++;
192 *bb = n;
193 bb++;
194 }
195 }
196
197 *n_bad_blocksp = n_bad_blocks;
198
199 return n_bad_blocks;
200}
201
202static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
203 size_t maxsize, const u_char *buf)
204{
205 nand_erase_options_t opts;
206 struct fcb_block *fcb;
207 struct dbbt_block *dbbt;
208 loff_t fw1_off;
209 void *fwbuf, *fcb_raw_page, *dbbt_page, *dbbt_data_page;
210 int nr_blks, nr_blks_fcb, fw1_blk;
211 size_t fwsize, dummy;
212 int i, ret;
213
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100214 fcb_raw_page = 0;
Shyam Saini1d43e242019-06-14 13:05:33 +0530215 /* erase */
216 memset(&opts, 0, sizeof(opts));
217 opts.offset = off;
218 opts.length = maxsize - 1;
219 ret = nand_erase_opts(mtd, &opts);
220 if (ret) {
221 printf("%s: erase failed (ret = %d)\n", __func__, ret);
222 return ret;
223 }
224
225 /*
226 * Reference documentation from i.MX6DQRM section 8.5.2.2
227 *
228 * Nand Boot Control Block(BCB) contains two data structures,
229 * - Firmware Configuration Block(FCB)
230 * - Discovered Bad Block Table(DBBT)
231 *
232 * FCB contains,
233 * - nand timings
234 * - DBBT search page address,
235 * - start page address of primary firmware
236 * - start page address of secondary firmware
237 *
238 * setup fcb:
239 * - number of blocks = mtd partition size / mtd erasesize
240 * - two firmware blocks, primary and secondary
241 * - first 4 block for FCB/DBBT
242 * - rest split in half for primary and secondary firmware
243 * - same firmware will write two times
244 */
245 nr_blks_fcb = 2;
246 nr_blks = maxsize / mtd->erasesize;
247 fw1_blk = nr_blks_fcb;
248
249 /* write fw */
250 fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
251 mtd->writesize);
252 fwbuf = kzalloc(fwsize, GFP_KERNEL);
253 if (!fwbuf) {
254 debug("failed to allocate fwbuf\n");
255 ret = -ENOMEM;
256 goto err;
257 }
258
259 memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
260 fw1_off = fw1_blk * mtd->erasesize;
261 ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
262 (u_char *)fwbuf, WITH_WR_VERIFY);
263 printf("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n",
264 fw1_off, fwsize, ret ? "ERROR" : "OK");
265 if (ret)
266 goto fwbuf_err;
267
268 /* fill fcb */
269 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
270 if (!fcb) {
271 debug("failed to allocate fcb\n");
272 ret = -ENOMEM;
273 goto fwbuf_err;
274 }
275
276 fcb->fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
277 fcb->fw1_pages = size / mtd->writesize + 1;
278 fill_fcb(fcb, mtd);
279
280 /* fill dbbt */
281 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
282 if (!dbbt_page) {
283 debug("failed to allocate dbbt_page\n");
284 ret = -ENOMEM;
285 goto fcb_err;
286 }
287
288 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
289 if (!dbbt_data_page) {
290 debug("failed to allocate dbbt_data_page\n");
291 ret = -ENOMEM;
292 goto dbbt_page_err;
293 }
294
295 dbbt = dbbt_page;
296 dbbt->checksum = 0;
297 dbbt->fingerprint = DBBT_FINGERPRINT2;
298 dbbt->version = DBBT_VERSION_1;
299 ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
300 if (ret < 0)
301 goto dbbt_data_page_err;
302 else if (ret > 0)
303 dbbt->dbbtpages = 1;
304
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100305 /*
306 * We prepare raw page only for i.MX6, for i.MX7 we
307 * leverage BCH hw module instead
308 */
309 if (is_mx6()) {
310 /* write fcb/dbbt */
311 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
312 GFP_KERNEL);
313 if (!fcb_raw_page) {
314 debug("failed to allocate fcb_raw_page\n");
315 ret = -ENOMEM;
316 goto dbbt_data_page_err;
317 }
Shyam Saini1d43e242019-06-14 13:05:33 +0530318
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200319#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100320 /* 40 bit BCH, for i.MX6UL(L) */
321 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200322#else
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100323 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
324 encode_hamming_13_8(fcb_raw_page + 12,
325 fcb_raw_page + 12 + 512, 512);
Parthiban Nallathambi6aa87492019-10-18 11:46:19 +0200326#endif
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100327 /*
328 * Set the first and second byte of OOB data to 0xFF,
329 * not 0x00. These bytes are used as the Manufacturers Bad
330 * Block Marker (MBBM). Since the FCB is mostly written to
331 * the first page in a block, a scan for
332 * factory bad blocks will detect these blocks as bad, e.g.
333 * when function nand_scan_bbt() is executed to build a new
334 * bad block table.
335 */
336 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
337 }
Shyam Saini1d43e242019-06-14 13:05:33 +0530338 for (i = 0; i < nr_blks_fcb; i++) {
339 if (mtd_block_isbad(mtd, off)) {
340 printf("Block %d is bad, skipped\n", i);
341 continue;
342 }
343
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100344 /*
345 * User BCH ECC hardware module for i.MX7
346 */
347 if (is_mx7()) {
348 u32 off = i * mtd->erasesize;
349 size_t rwsize = sizeof(*fcb);
Shyam Saini1d43e242019-06-14 13:05:33 +0530350
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100351 printf("Writing %d bytes to 0x%x: ", rwsize, off);
352
353 /* switch nand BCH to FCB compatible settings */
354 mxs_nand_mode_fcb(mtd);
355 ret = nand_write(mtd, off, &rwsize,
356 (unsigned char *)fcb);
357 mxs_nand_mode_normal(mtd);
358
359 printf("%s\n", ret ? "ERROR" : "OK");
360 } else if (is_mx6()) {
361 /* raw write */
362 mtd_oob_ops_t ops = {
363 .datbuf = (u8 *)fcb_raw_page,
364 .oobbuf = ((u8 *)fcb_raw_page) +
365 mtd->writesize,
366 .len = mtd->writesize,
367 .ooblen = mtd->oobsize,
368 .mode = MTD_OPS_RAW
369 };
370
371 ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
372 if (ret)
373 goto fcb_raw_page_err;
374 debug("NAND fcb write: 0x%x offset 0x%x written: %s\n",
375 mtd->erasesize * i, ops.len, ret ?
376 "ERROR" : "OK");
377 }
Shyam Saini1d43e242019-06-14 13:05:33 +0530378
379 ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
380 mtd->writesize, &dummy, dbbt_page);
381 if (ret)
382 goto fcb_raw_page_err;
383 debug("NAND dbbt write: 0x%x offset, 0x%x bytes written: %s\n",
384 mtd->erasesize * i + mtd->writesize, dummy,
385 ret ? "ERROR" : "OK");
386
387 /* dbbtpages == 0 if no bad blocks */
388 if (dbbt->dbbtpages > 0) {
389 loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
390
391 ret = mtd_write(mtd, to, mtd->writesize, &dummy,
392 dbbt_data_page);
393 if (ret)
394 goto fcb_raw_page_err;
395 }
396 }
397
398fcb_raw_page_err:
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100399 if (is_mx6())
400 kfree(fcb_raw_page);
Shyam Saini1d43e242019-06-14 13:05:33 +0530401dbbt_data_page_err:
402 kfree(dbbt_data_page);
403dbbt_page_err:
404 kfree(dbbt_page);
405fcb_err:
406 kfree(fcb);
407fwbuf_err:
408 kfree(fwbuf);
409err:
410 return ret;
411}
412
413static int do_nandbcb_update(int argc, char * const argv[])
414{
415 struct mtd_info *mtd;
416 loff_t addr, offset, size, maxsize;
417 char *endp;
418 u_char *buf;
419 int dev;
420 int ret;
421
422 if (argc != 4)
423 return CMD_RET_USAGE;
424
425 dev = nand_curr_device;
426 if (dev < 0) {
427 printf("failed to get nand_curr_device, run nand device");
428 return CMD_RET_FAILURE;
429 }
430
431 addr = simple_strtoul(argv[1], &endp, 16);
432 if (*argv[1] == 0 || *endp != 0)
433 return CMD_RET_FAILURE;
434
435 mtd = get_nand_dev_by_index(dev);
436 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
437 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
438 return CMD_RET_FAILURE;
439
440 buf = map_physmem(addr, size, MAP_WRBACK);
441 if (!buf) {
442 puts("failed to map physical memory\n");
443 return CMD_RET_FAILURE;
444 }
445
446 ret = nandbcb_update(mtd, offset, size, maxsize, buf);
447
448 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
449}
450
451static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
452 char * const argv[])
453{
454 const char *cmd;
455 int ret = 0;
456
457 if (argc < 5)
458 goto usage;
459
460 cmd = argv[1];
461 --argc;
462 ++argv;
463
464 if (strcmp(cmd, "update") == 0) {
465 ret = do_nandbcb_update(argc, argv);
466 goto done;
467 }
468
469done:
470 if (ret != -1)
471 return ret;
472usage:
473 return CMD_RET_USAGE;
474}
475
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +0200476#ifdef CONFIG_SYS_LONGHELP
Shyam Saini1d43e242019-06-14 13:05:33 +0530477static char nandbcb_help_text[] =
478 "update addr off|partition len - update 'len' bytes starting at\n"
479 " 'off|part' to memory address 'addr', skipping bad blocks";
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +0200480#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530481
482U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
483 "i.MX6 Nand BCB",
484 nandbcb_help_text
485);