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