blob: 6e7e982ee54675e46c29ccd548f2bc4a55cec44a [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
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100131static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd,
132 u32 fw1_start, u32 fw2_start, u32 fw_pages)
Shyam Saini1d43e242019-06-14 13:05:33 +0530133{
134 struct nand_chip *chip = mtd_to_nand(mtd);
135 struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100136 struct mxs_nand_layout l;
137
138 mxs_nand_get_layout(mtd, &l);
Shyam Saini1d43e242019-06-14 13:05:33 +0530139
140 fcb->fingerprint = FCB_FINGERPRINT;
141 fcb->version = FCB_VERSION_1;
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100142
Shyam Saini1d43e242019-06-14 13:05:33 +0530143 fcb->pagesize = mtd->writesize;
144 fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
145 fcb->sectors = mtd->erasesize / mtd->writesize;
146
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100147 fcb->meta_size = l.meta_size;
148 fcb->nr_blocks = l.nblocks;
149 fcb->ecc_nr = l.data0_size;
150 fcb->ecc_level = l.ecc0;
151 fcb->ecc_size = l.datan_size;
152 fcb->ecc_type = l.eccn;
Shyam Saini1d43e242019-06-14 13:05:33 +0530153
154 /* Also hardcoded in kobs-ng */
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100155 if (is_mx6()) {
156 fcb->datasetup = 80;
157 fcb->datahold = 60;
158 fcb->addr_setup = 25;
159 fcb->dsample_time = 6;
160 } else if (is_mx7()) {
161 fcb->datasetup = 10;
162 fcb->datahold = 7;
163 fcb->addr_setup = 15;
164 fcb->dsample_time = 6;
165 }
Shyam Saini1d43e242019-06-14 13:05:33 +0530166
167 /* DBBT search area starts at second page on first block */
168 fcb->dbbt_start = 1;
169
170 fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
171 fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
172
173 fcb->phy_offset = mtd->writesize;
174
175 fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
176
Igor Opaniukdad30dd2019-11-03 16:49:44 +0100177 fcb->disbbm = 0;
178 fcb->disbbm_search = 0;
179
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100180 fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */
181 fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */
182 fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */
183 fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */
184
Shyam Saini1d43e242019-06-14 13:05:33 +0530185 fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
186}
187
188static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
189{
190 int n, n_bad_blocks = 0;
191 u32 *bb = buf + 0x8;
192 u32 *n_bad_blocksp = buf + 0x4;
193
194 for (n = 0; n < num_blocks; n++) {
195 loff_t offset = n * mtd->erasesize;
196 if (mtd_block_isbad(mtd, offset)) {
197 n_bad_blocks++;
198 *bb = n;
199 bb++;
200 }
201 }
202
203 *n_bad_blocksp = n_bad_blocks;
204
205 return n_bad_blocks;
206}
207
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100208static int write_fcb_dbbt(struct mtd_info *mtd, struct fcb_block *fcb,
209 struct dbbt_block *dbbt, void *dbbt_data_page,
210 loff_t off)
211{
212 void *fcb_raw_page = 0;
213 int i, ret;
214 size_t dummy;
215
216 /*
217 * We prepare raw page only for i.MX6, for i.MX7 we
218 * leverage BCH hw module instead
219 */
220 if (is_mx6()) {
221 /* write fcb/dbbt */
222 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
223 GFP_KERNEL);
224 if (!fcb_raw_page) {
225 debug("failed to allocate fcb_raw_page\n");
226 ret = -ENOMEM;
227 return ret;
228 }
229
230#if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
231 /* 40 bit BCH, for i.MX6UL(L) */
232 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
233#else
234 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
235 encode_hamming_13_8(fcb_raw_page + 12,
236 fcb_raw_page + 12 + 512, 512);
237#endif
238 /*
239 * Set the first and second byte of OOB data to 0xFF,
240 * not 0x00. These bytes are used as the Manufacturers Bad
241 * Block Marker (MBBM). Since the FCB is mostly written to
242 * the first page in a block, a scan for
243 * factory bad blocks will detect these blocks as bad, e.g.
244 * when function nand_scan_bbt() is executed to build a new
245 * bad block table.
246 */
247 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
248 }
249 for (i = 0; i < 2; i++) {
250 if (mtd_block_isbad(mtd, off)) {
251 printf("Block %d is bad, skipped\n", i);
252 continue;
253 }
254
255 /*
256 * User BCH ECC hardware module for i.MX7
257 */
258 if (is_mx7()) {
259 u32 off = i * mtd->erasesize;
260 size_t rwsize = sizeof(*fcb);
261
262 printf("Writing %d bytes to 0x%x: ", rwsize, off);
263
264 /* switch nand BCH to FCB compatible settings */
265 mxs_nand_mode_fcb(mtd);
266 ret = nand_write(mtd, off, &rwsize,
267 (unsigned char *)fcb);
268 mxs_nand_mode_normal(mtd);
269
270 printf("%s\n", ret ? "ERROR" : "OK");
271 } else if (is_mx6()) {
272 /* raw write */
273 mtd_oob_ops_t ops = {
274 .datbuf = (u8 *)fcb_raw_page,
275 .oobbuf = ((u8 *)fcb_raw_page) +
276 mtd->writesize,
277 .len = mtd->writesize,
278 .ooblen = mtd->oobsize,
279 .mode = MTD_OPS_RAW
280 };
281
282 ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
283 if (ret)
284 goto fcb_raw_page_err;
285 debug("NAND fcb write: 0x%x offset 0x%x written: %s\n",
286 mtd->erasesize * i, ops.len, ret ?
287 "ERROR" : "OK");
288 }
289
290 ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
291 mtd->writesize, &dummy, (void *)dbbt);
292 if (ret)
293 goto fcb_raw_page_err;
294 debug("NAND dbbt write: 0x%x offset, 0x%x bytes written: %s\n",
295 mtd->erasesize * i + mtd->writesize, dummy,
296 ret ? "ERROR" : "OK");
297
298 /* dbbtpages == 0 if no bad blocks */
299 if (dbbt->dbbtpages > 0) {
300 loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
301
302 ret = mtd_write(mtd, to, mtd->writesize, &dummy,
303 dbbt_data_page);
304 if (ret)
305 goto fcb_raw_page_err;
306 }
307 }
308
309fcb_raw_page_err:
310 if (is_mx6())
311 kfree(fcb_raw_page);
312
313 return ret;
314}
315
Shyam Saini1d43e242019-06-14 13:05:33 +0530316static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
317 size_t maxsize, const u_char *buf)
318{
319 nand_erase_options_t opts;
320 struct fcb_block *fcb;
321 struct dbbt_block *dbbt;
322 loff_t fw1_off;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100323 void *fwbuf, *dbbt_page, *dbbt_data_page;
324 u32 fw1_start, fw1_pages;
Shyam Saini1d43e242019-06-14 13:05:33 +0530325 int nr_blks, nr_blks_fcb, fw1_blk;
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100326 size_t fwsize;
327 int ret;
Shyam Saini1d43e242019-06-14 13:05:33 +0530328
329 /* erase */
330 memset(&opts, 0, sizeof(opts));
331 opts.offset = off;
332 opts.length = maxsize - 1;
333 ret = nand_erase_opts(mtd, &opts);
334 if (ret) {
335 printf("%s: erase failed (ret = %d)\n", __func__, ret);
336 return ret;
337 }
338
339 /*
340 * Reference documentation from i.MX6DQRM section 8.5.2.2
341 *
342 * Nand Boot Control Block(BCB) contains two data structures,
343 * - Firmware Configuration Block(FCB)
344 * - Discovered Bad Block Table(DBBT)
345 *
346 * FCB contains,
347 * - nand timings
348 * - DBBT search page address,
349 * - start page address of primary firmware
350 * - start page address of secondary firmware
351 *
352 * setup fcb:
353 * - number of blocks = mtd partition size / mtd erasesize
354 * - two firmware blocks, primary and secondary
355 * - first 4 block for FCB/DBBT
356 * - rest split in half for primary and secondary firmware
357 * - same firmware will write two times
358 */
359 nr_blks_fcb = 2;
360 nr_blks = maxsize / mtd->erasesize;
361 fw1_blk = nr_blks_fcb;
362
363 /* write fw */
364 fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
365 mtd->writesize);
366 fwbuf = kzalloc(fwsize, GFP_KERNEL);
367 if (!fwbuf) {
368 debug("failed to allocate fwbuf\n");
369 ret = -ENOMEM;
370 goto err;
371 }
372
373 memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
374 fw1_off = fw1_blk * mtd->erasesize;
375 ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
376 (u_char *)fwbuf, WITH_WR_VERIFY);
377 printf("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n",
378 fw1_off, fwsize, ret ? "ERROR" : "OK");
379 if (ret)
380 goto fwbuf_err;
381
382 /* fill fcb */
383 fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
384 if (!fcb) {
385 debug("failed to allocate fcb\n");
386 ret = -ENOMEM;
387 goto fwbuf_err;
388 }
389
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100390 fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
391 fw1_pages = size / mtd->writesize + 1;
392 fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages);
Shyam Saini1d43e242019-06-14 13:05:33 +0530393
394 /* fill dbbt */
395 dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
396 if (!dbbt_page) {
397 debug("failed to allocate dbbt_page\n");
398 ret = -ENOMEM;
399 goto fcb_err;
400 }
401
402 dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
403 if (!dbbt_data_page) {
404 debug("failed to allocate dbbt_data_page\n");
405 ret = -ENOMEM;
406 goto dbbt_page_err;
407 }
408
409 dbbt = dbbt_page;
410 dbbt->checksum = 0;
411 dbbt->fingerprint = DBBT_FINGERPRINT2;
412 dbbt->version = DBBT_VERSION_1;
413 ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
414 if (ret < 0)
415 goto dbbt_data_page_err;
416 else if (ret > 0)
417 dbbt->dbbtpages = 1;
418
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100419 /* write fcb and dbbt to nand */
420 ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, off);
421 if (ret < 0)
422 printf("failed to write FCB/DBBT\n");
Shyam Saini1d43e242019-06-14 13:05:33 +0530423
Shyam Saini1d43e242019-06-14 13:05:33 +0530424dbbt_data_page_err:
425 kfree(dbbt_data_page);
426dbbt_page_err:
427 kfree(dbbt_page);
428fcb_err:
429 kfree(fcb);
430fwbuf_err:
431 kfree(fwbuf);
432err:
433 return ret;
434}
435
436static int do_nandbcb_update(int argc, char * const argv[])
437{
438 struct mtd_info *mtd;
439 loff_t addr, offset, size, maxsize;
440 char *endp;
441 u_char *buf;
442 int dev;
443 int ret;
444
445 if (argc != 4)
446 return CMD_RET_USAGE;
447
448 dev = nand_curr_device;
449 if (dev < 0) {
Igor Opaniuk1b899a82019-11-03 16:49:45 +0100450 printf("failed to get nand_curr_device, run nand device\n");
Shyam Saini1d43e242019-06-14 13:05:33 +0530451 return CMD_RET_FAILURE;
452 }
453
454 addr = simple_strtoul(argv[1], &endp, 16);
455 if (*argv[1] == 0 || *endp != 0)
456 return CMD_RET_FAILURE;
457
458 mtd = get_nand_dev_by_index(dev);
459 if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
460 &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
461 return CMD_RET_FAILURE;
462
463 buf = map_physmem(addr, size, MAP_WRBACK);
464 if (!buf) {
465 puts("failed to map physical memory\n");
466 return CMD_RET_FAILURE;
467 }
468
469 ret = nandbcb_update(mtd, offset, size, maxsize, buf);
470
471 return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
472}
473
474static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
475 char * const argv[])
476{
477 const char *cmd;
478 int ret = 0;
479
480 if (argc < 5)
481 goto usage;
482
483 cmd = argv[1];
484 --argc;
485 ++argv;
486
487 if (strcmp(cmd, "update") == 0) {
488 ret = do_nandbcb_update(argc, argv);
489 goto done;
490 }
491
492done:
493 if (ret != -1)
494 return ret;
495usage:
496 return CMD_RET_USAGE;
497}
498
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +0200499#ifdef CONFIG_SYS_LONGHELP
Shyam Saini1d43e242019-06-14 13:05:33 +0530500static char nandbcb_help_text[] =
501 "update addr off|partition len - update 'len' bytes starting at\n"
502 " 'off|part' to memory address 'addr', skipping bad blocks";
Parthiban Nallathambi4ee0ff12019-08-23 18:35:10 +0200503#endif
Shyam Saini1d43e242019-06-14 13:05:33 +0530504
505U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
506 "i.MX6 Nand BCB",
507 nandbcb_help_text
508);