Merge https://gitlab.denx.de/u-boot/custodians/u-boot-marvell
[oweals/u-boot.git] / arch / arm / mach-imx / cmd_nandbcb.c
1 /*
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 <malloc.h>
14 #include <nand.h>
15 #include <dm/devres.h>
16
17 #include <asm/io.h>
18 #include <jffs2/jffs2.h>
19 #include <linux/bch.h>
20 #include <linux/mtd/mtd.h>
21
22 #include <asm/arch/sys_proto.h>
23 #include <asm/mach-imx/imx-nandbcb.h>
24 #include <asm/mach-imx/imximage.cfg>
25 #include <mxs_nand.h>
26 #include <linux/mtd/mtd.h>
27 #include <nand.h>
28
29 #include "../../../cmd/legacy-mtd-utils.h"
30
31 #define BF_VAL(v, bf)           (((v) & bf##_MASK) >> bf##_OFFSET)
32 #define GETBIT(v, n)            (((v) >> (n)) & 0x1)
33
34 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
35 static uint8_t reverse_bit(uint8_t b)
36 {
37         b = (b & 0xf0) >> 4 | (b & 0x0f) << 4;
38         b = (b & 0xcc) >> 2 | (b & 0x33) << 2;
39         b = (b & 0xaa) >> 1 | (b & 0x55) << 1;
40
41         return b;
42 }
43
44 static void encode_bch_ecc(void *buf, struct fcb_block *fcb, int eccbits)
45 {
46         int i, j, m = 13;
47         int blocksize = 128;
48         int numblocks = 8;
49         int ecc_buf_size = (m * eccbits + 7) / 8;
50         struct bch_control *bch = init_bch(m, eccbits, 0);
51         u8 *ecc_buf = kzalloc(ecc_buf_size, GFP_KERNEL);
52         u8 *tmp_buf = kzalloc(blocksize * numblocks, GFP_KERNEL);
53         u8 *psrc, *pdst;
54
55         /*
56          * The blocks here are bit aligned. If eccbits is a multiple of 8,
57          * we just can copy bytes. Otherwiese we must move the blocks to
58          * the next free bit position.
59          */
60         WARN_ON(eccbits % 8);
61
62         memcpy(tmp_buf, fcb, sizeof(*fcb));
63
64         for (i = 0; i < numblocks; i++) {
65                 memset(ecc_buf, 0, ecc_buf_size);
66                 psrc = tmp_buf + i * blocksize;
67                 pdst = buf + i * (blocksize + ecc_buf_size);
68
69                 /* copy data byte aligned to destination buf */
70                 memcpy(pdst, psrc, blocksize);
71
72                 /*
73                  * imx-kobs use a modified encode_bch which reverse the
74                  * bit order of the data before calculating bch.
75                  * Do this in the buffer and use the bch lib here.
76                  */
77                 for (j = 0; j < blocksize; j++)
78                         psrc[j] = reverse_bit(psrc[j]);
79
80                 encode_bch(bch, psrc, blocksize, ecc_buf);
81
82                 /* reverse ecc bit */
83                 for (j = 0; j < ecc_buf_size; j++)
84                         ecc_buf[j] = reverse_bit(ecc_buf[j]);
85
86                 /* Here eccbuf is byte aligned and we can just copy it */
87                 memcpy(pdst + blocksize, ecc_buf, ecc_buf_size);
88         }
89
90         kfree(ecc_buf);
91         kfree(tmp_buf);
92         free_bch(bch);
93 }
94 #else
95
96 static u8 calculate_parity_13_8(u8 d)
97 {
98         u8 p = 0;
99
100         p |= (GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 3) ^ GETBIT(d, 2)) << 0;
101         p |= (GETBIT(d, 7) ^ GETBIT(d, 5) ^ GETBIT(d, 4) ^ GETBIT(d, 2) ^
102               GETBIT(d, 1)) << 1;
103         p |= (GETBIT(d, 7) ^ GETBIT(d, 6) ^ GETBIT(d, 5) ^ GETBIT(d, 1) ^
104               GETBIT(d, 0)) << 2;
105         p |= (GETBIT(d, 7) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 0)) << 3;
106         p |= (GETBIT(d, 6) ^ GETBIT(d, 4) ^ GETBIT(d, 3) ^ GETBIT(d, 2) ^
107               GETBIT(d, 1) ^ GETBIT(d, 0)) << 4;
108
109         return p;
110 }
111
112 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
113 {
114         int i;
115         u8 *src = _src;
116         u8 *ecc = _ecc;
117
118         for (i = 0; i < size; i++)
119                 ecc[i] = calculate_parity_13_8(src[i]);
120 }
121 #endif
122
123 static u32 calc_chksum(void *buf, size_t size)
124 {
125         u32 chksum = 0;
126         u8 *bp = buf;
127         size_t i;
128
129         for (i = 0; i < size; i++)
130                 chksum += bp[i];
131
132         return ~chksum;
133 }
134
135 static void fill_fcb(struct fcb_block *fcb, struct mtd_info *mtd,
136                      u32 fw1_start, u32 fw2_start, u32 fw_pages)
137 {
138         struct nand_chip *chip = mtd_to_nand(mtd);
139         struct mxs_nand_info *nand_info = nand_get_controller_data(chip);
140         struct mxs_nand_layout l;
141
142         mxs_nand_get_layout(mtd, &l);
143
144         fcb->fingerprint = FCB_FINGERPRINT;
145         fcb->version = FCB_VERSION_1;
146
147         fcb->pagesize = mtd->writesize;
148         fcb->oob_pagesize = mtd->writesize + mtd->oobsize;
149         fcb->sectors = mtd->erasesize / mtd->writesize;
150
151         fcb->meta_size = l.meta_size;
152         fcb->nr_blocks = l.nblocks;
153         fcb->ecc_nr = l.data0_size;
154         fcb->ecc_level = l.ecc0;
155         fcb->ecc_size = l.datan_size;
156         fcb->ecc_type = l.eccn;
157
158         /* Also hardcoded in kobs-ng */
159         if (is_mx6()) {
160                 fcb->datasetup = 80;
161                 fcb->datahold = 60;
162                 fcb->addr_setup = 25;
163                 fcb->dsample_time = 6;
164         } else if (is_mx7()) {
165                 fcb->datasetup = 10;
166                 fcb->datahold = 7;
167                 fcb->addr_setup = 15;
168                 fcb->dsample_time = 6;
169         }
170
171         /* DBBT search area starts at second page on first block */
172         fcb->dbbt_start = 1;
173
174         fcb->bb_byte = nand_info->bch_geometry.block_mark_byte_offset;
175         fcb->bb_start_bit = nand_info->bch_geometry.block_mark_bit_offset;
176
177         fcb->phy_offset = mtd->writesize;
178
179         fcb->nr_blocks = mtd->writesize / fcb->ecc_nr - 1;
180
181         fcb->disbbm = 0;
182         fcb->disbbm_search = 0;
183
184         fcb->fw1_start = fw1_start; /* Firmware image starts on this sector */
185         fcb->fw2_start = fw2_start; /* Secondary FW Image starting Sector */
186         fcb->fw1_pages = fw_pages; /* Number of sectors in firmware image */
187         fcb->fw2_pages = fw_pages; /* Number of sector in secondary FW image */
188
189         fcb->checksum = calc_chksum((void *)fcb + 4, sizeof(*fcb) - 4);
190 }
191
192 static int dbbt_fill_data(struct mtd_info *mtd, void *buf, int num_blocks)
193 {
194         int n, n_bad_blocks = 0;
195         u32 *bb = buf + 0x8;
196         u32 *n_bad_blocksp = buf + 0x4;
197
198         for (n = 0; n < num_blocks; n++) {
199                 loff_t offset = n * mtd->erasesize;
200                         if (mtd_block_isbad(mtd, offset)) {
201                                 n_bad_blocks++;
202                                 *bb = n;
203                                 bb++;
204                 }
205         }
206
207         *n_bad_blocksp = n_bad_blocks;
208
209         return n_bad_blocks;
210 }
211
212 static int write_fcb_dbbt(struct mtd_info *mtd, struct fcb_block *fcb,
213                           struct dbbt_block *dbbt, void *dbbt_data_page,
214                           loff_t off)
215 {
216         void *fcb_raw_page = 0;
217         int i, ret;
218         size_t dummy;
219
220         /*
221          * We prepare raw page only for i.MX6, for i.MX7 we
222          * leverage BCH hw module instead
223          */
224         if (is_mx6()) {
225                 /* write fcb/dbbt */
226                 fcb_raw_page = kzalloc(mtd->writesize + mtd->oobsize,
227                                        GFP_KERNEL);
228                 if (!fcb_raw_page) {
229                         debug("failed to allocate fcb_raw_page\n");
230                         ret = -ENOMEM;
231                         return ret;
232                 }
233
234 #if defined(CONFIG_MX6UL) || defined(CONFIG_MX6ULL)
235                 /* 40 bit BCH, for i.MX6UL(L) */
236                 encode_bch_ecc(fcb_raw_page + 32, fcb, 40);
237 #else
238                 memcpy(fcb_raw_page + 12, fcb, sizeof(struct fcb_block));
239                 encode_hamming_13_8(fcb_raw_page + 12,
240                                     fcb_raw_page + 12 + 512, 512);
241 #endif
242                 /*
243                  * Set the first and second byte of OOB data to 0xFF,
244                  * not 0x00. These bytes are used as the Manufacturers Bad
245                  * Block Marker (MBBM). Since the FCB is mostly written to
246                  * the first page in a block, a scan for
247                  * factory bad blocks will detect these blocks as bad, e.g.
248                  * when function nand_scan_bbt() is executed to build a new
249                  * bad block table.
250                  */
251                 memset(fcb_raw_page + mtd->writesize, 0xFF, 2);
252         }
253         for (i = 0; i < 2; i++) {
254                 if (mtd_block_isbad(mtd, off)) {
255                         printf("Block %d is bad, skipped\n", i);
256                         continue;
257                 }
258
259                 /*
260                  * User BCH ECC hardware module for i.MX7
261                  */
262                 if (is_mx7()) {
263                         u32 off = i * mtd->erasesize;
264                         size_t rwsize = sizeof(*fcb);
265
266                         printf("Writing %d bytes to 0x%x: ", rwsize, off);
267
268                         /* switch nand BCH to FCB compatible settings */
269                         mxs_nand_mode_fcb(mtd);
270                         ret = nand_write(mtd, off, &rwsize,
271                                          (unsigned char *)fcb);
272                         mxs_nand_mode_normal(mtd);
273
274                         printf("%s\n", ret ? "ERROR" : "OK");
275                 } else if (is_mx6()) {
276                         /* raw write */
277                         mtd_oob_ops_t ops = {
278                                 .datbuf = (u8 *)fcb_raw_page,
279                                 .oobbuf = ((u8 *)fcb_raw_page) +
280                                           mtd->writesize,
281                                 .len = mtd->writesize,
282                                 .ooblen = mtd->oobsize,
283                                 .mode = MTD_OPS_RAW
284                         };
285
286                         ret = mtd_write_oob(mtd, mtd->erasesize * i, &ops);
287                         if (ret)
288                                 goto fcb_raw_page_err;
289                         debug("NAND fcb write: 0x%x offset 0x%x written: %s\n",
290                               mtd->erasesize * i, ops.len, ret ?
291                               "ERROR" : "OK");
292                 }
293
294                 ret = mtd_write(mtd, mtd->erasesize * i + mtd->writesize,
295                                 mtd->writesize, &dummy, (void *)dbbt);
296                 if (ret)
297                         goto fcb_raw_page_err;
298                 debug("NAND dbbt write: 0x%x offset, 0x%x bytes written: %s\n",
299                       mtd->erasesize * i + mtd->writesize, dummy,
300                       ret ? "ERROR" : "OK");
301
302                 /* dbbtpages == 0 if no bad blocks */
303                 if (dbbt->dbbtpages > 0) {
304                         loff_t to = (mtd->erasesize * i + mtd->writesize * 5);
305
306                         ret = mtd_write(mtd, to, mtd->writesize, &dummy,
307                                         dbbt_data_page);
308                         if (ret)
309                                 goto fcb_raw_page_err;
310                 }
311         }
312
313 fcb_raw_page_err:
314         if (is_mx6())
315                 kfree(fcb_raw_page);
316
317         return ret;
318 }
319
320 static int nandbcb_update(struct mtd_info *mtd, loff_t off, size_t size,
321                           size_t maxsize, const u_char *buf)
322 {
323         nand_erase_options_t opts;
324         struct fcb_block *fcb;
325         struct dbbt_block *dbbt;
326         loff_t fw1_off;
327         void *fwbuf, *dbbt_page, *dbbt_data_page;
328         u32 fw1_start, fw1_pages;
329         int nr_blks, nr_blks_fcb, fw1_blk;
330         size_t fwsize;
331         int ret;
332
333         /* erase */
334         memset(&opts, 0, sizeof(opts));
335         opts.offset = off;
336         opts.length = maxsize - 1;
337         ret = nand_erase_opts(mtd, &opts);
338         if (ret) {
339                 printf("%s: erase failed (ret = %d)\n", __func__, ret);
340                 return ret;
341         }
342
343         /*
344          * Reference documentation from i.MX6DQRM section 8.5.2.2
345          *
346          * Nand Boot Control Block(BCB) contains two data structures,
347          * - Firmware Configuration Block(FCB)
348          * - Discovered Bad Block Table(DBBT)
349          *
350          * FCB contains,
351          * - nand timings
352          * - DBBT search page address,
353          * - start page address of primary firmware
354          * - start page address of secondary firmware
355          *
356          * setup fcb:
357          * - number of blocks = mtd partition size / mtd erasesize
358          * - two firmware blocks, primary and secondary
359          * - first 4 block for FCB/DBBT
360          * - rest split in half for primary and secondary firmware
361          * - same firmware will write two times
362          */
363         nr_blks_fcb = 2;
364         nr_blks = maxsize / mtd->erasesize;
365         fw1_blk = nr_blks_fcb;
366
367         /* write fw */
368         fwsize = ALIGN(size + FLASH_OFFSET_STANDARD + mtd->writesize,
369                        mtd->writesize);
370         fwbuf = kzalloc(fwsize, GFP_KERNEL);
371         if (!fwbuf) {
372                 debug("failed to allocate fwbuf\n");
373                 ret = -ENOMEM;
374                 goto err;
375         }
376
377         memcpy(fwbuf + FLASH_OFFSET_STANDARD, buf, size);
378         fw1_off = fw1_blk * mtd->erasesize;
379         ret = nand_write_skip_bad(mtd, fw1_off, &fwsize, NULL, maxsize,
380                                   (u_char *)fwbuf, WITH_WR_VERIFY);
381         printf("NAND fw write: 0x%llx offset, 0x%x bytes written: %s\n",
382                fw1_off, fwsize, ret ? "ERROR" : "OK");
383         if (ret)
384                 goto fwbuf_err;
385
386         /* fill fcb */
387         fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
388         if (!fcb) {
389                 debug("failed to allocate fcb\n");
390                 ret = -ENOMEM;
391                 goto fwbuf_err;
392         }
393
394         fw1_start = (fw1_blk * mtd->erasesize) / mtd->writesize;
395         fw1_pages = size / mtd->writesize + 1;
396         fill_fcb(fcb, mtd, fw1_start, 0, fw1_pages);
397
398         /* fill dbbt */
399         dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
400         if (!dbbt_page) {
401                 debug("failed to allocate dbbt_page\n");
402                 ret = -ENOMEM;
403                 goto fcb_err;
404         }
405
406         dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
407         if (!dbbt_data_page) {
408                 debug("failed to allocate dbbt_data_page\n");
409                 ret = -ENOMEM;
410                 goto dbbt_page_err;
411         }
412
413         dbbt = dbbt_page;
414         dbbt->checksum = 0;
415         dbbt->fingerprint = DBBT_FINGERPRINT2;
416         dbbt->version = DBBT_VERSION_1;
417         ret = dbbt_fill_data(mtd, dbbt_data_page, nr_blks);
418         if (ret < 0)
419                 goto dbbt_data_page_err;
420         else if (ret > 0)
421                 dbbt->dbbtpages = 1;
422
423         /* write fcb and dbbt to nand */
424         ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, off);
425         if (ret < 0)
426                 printf("failed to write FCB/DBBT\n");
427
428 dbbt_data_page_err:
429         kfree(dbbt_data_page);
430 dbbt_page_err:
431         kfree(dbbt_page);
432 fcb_err:
433         kfree(fcb);
434 fwbuf_err:
435         kfree(fwbuf);
436 err:
437         return ret;
438 }
439
440 static int do_nandbcb_bcbonly(int argc, char * const argv[])
441 {
442         struct fcb_block *fcb;
443         struct dbbt_block *dbbt;
444         u32 fw_len, fw1_off, fw2_off;
445         struct mtd_info *mtd;
446         void *dbbt_page, *dbbt_data_page;
447         int dev, ret;
448
449         dev = nand_curr_device;
450         if ((dev < 0) || (dev >= CONFIG_SYS_MAX_NAND_DEVICE) ||
451             (!get_nand_dev_by_index(dev))) {
452                 puts("No devices available\n");
453                 return CMD_RET_FAILURE;
454         }
455
456         mtd = get_nand_dev_by_index(dev);
457
458         if (argc < 3)
459                 return CMD_RET_FAILURE;
460
461         fw_len = simple_strtoul(argv[1], NULL, 16);
462         fw1_off = simple_strtoul(argv[2], NULL, 16);
463
464         if (argc > 3)
465                 fw2_off = simple_strtoul(argv[3], NULL, 16);
466         else
467                 fw2_off = fw1_off;
468
469         /* fill fcb */
470         fcb = kzalloc(sizeof(*fcb), GFP_KERNEL);
471         if (!fcb) {
472                 debug("failed to allocate fcb\n");
473                 ret = -ENOMEM;
474                 return CMD_RET_FAILURE;
475         }
476
477         fill_fcb(fcb, mtd, fw1_off / mtd->writesize,
478                  fw2_off / mtd->writesize, fw_len / mtd->writesize);
479
480         /* fill dbbt */
481         dbbt_page = kzalloc(mtd->writesize, GFP_KERNEL);
482         if (!dbbt_page) {
483                 debug("failed to allocate dbbt_page\n");
484                 ret = -ENOMEM;
485                 goto fcb_err;
486         }
487
488         dbbt_data_page = kzalloc(mtd->writesize, GFP_KERNEL);
489         if (!dbbt_data_page) {
490                 debug("failed to allocate dbbt_data_page\n");
491                 ret = -ENOMEM;
492                 goto dbbt_page_err;
493         }
494
495         dbbt = dbbt_page;
496         dbbt->checksum = 0;
497         dbbt->fingerprint = DBBT_FINGERPRINT2;
498         dbbt->version = DBBT_VERSION_1;
499         ret = dbbt_fill_data(mtd, dbbt_data_page, 0);
500         if (ret < 0)
501                 goto dbbt_data_page_err;
502         else if (ret > 0)
503                 dbbt->dbbtpages = 1;
504
505         /* write fcb and dbbt to nand */
506         ret = write_fcb_dbbt(mtd, fcb, dbbt, dbbt_data_page, 0);
507 dbbt_data_page_err:
508         kfree(dbbt_data_page);
509 dbbt_page_err:
510         kfree(dbbt_page);
511 fcb_err:
512         kfree(fcb);
513
514         if (ret < 0) {
515                 printf("failed to write FCB/DBBT\n");
516                 return CMD_RET_FAILURE;
517         }
518
519         return CMD_RET_SUCCESS;
520 }
521
522 static int do_nandbcb_update(int argc, char * const argv[])
523 {
524         struct mtd_info *mtd;
525         loff_t addr, offset, size, maxsize;
526         char *endp;
527         u_char *buf;
528         int dev;
529         int ret;
530
531         if (argc != 4)
532                 return CMD_RET_USAGE;
533
534         dev = nand_curr_device;
535         if (dev < 0) {
536                 printf("failed to get nand_curr_device, run nand device\n");
537                 return CMD_RET_FAILURE;
538         }
539
540         addr = simple_strtoul(argv[1], &endp, 16);
541         if (*argv[1] == 0 || *endp != 0)
542                 return CMD_RET_FAILURE;
543
544         mtd = get_nand_dev_by_index(dev);
545         if (mtd_arg_off_size(argc - 2, argv + 2, &dev, &offset, &size,
546                              &maxsize, MTD_DEV_TYPE_NAND, mtd->size))
547                 return CMD_RET_FAILURE;
548
549         buf = map_physmem(addr, size, MAP_WRBACK);
550         if (!buf) {
551                 puts("failed to map physical memory\n");
552                 return CMD_RET_FAILURE;
553         }
554
555         ret = nandbcb_update(mtd, offset, size, maxsize, buf);
556
557         return ret == 0 ? CMD_RET_SUCCESS : CMD_RET_FAILURE;
558 }
559
560 static int do_nandbcb(cmd_tbl_t *cmdtp, int flag, int argc,
561                       char * const argv[])
562 {
563         const char *cmd;
564         int ret = 0;
565
566         if (argc < 5)
567                 goto usage;
568
569         cmd = argv[1];
570         --argc;
571         ++argv;
572
573         if (strcmp(cmd, "update") == 0) {
574                 ret = do_nandbcb_update(argc, argv);
575                 goto done;
576         }
577
578         if (strcmp(cmd, "bcbonly") == 0) {
579                 ret = do_nandbcb_bcbonly(argc, argv);
580                 goto done;
581         }
582
583 done:
584         if (ret != -1)
585                 return ret;
586 usage:
587         return CMD_RET_USAGE;
588 }
589
590 #ifdef CONFIG_SYS_LONGHELP
591 static char nandbcb_help_text[] =
592         "update addr off|partition len  - update 'len' bytes starting at\n"
593         "       'off|part' to memory address 'addr', skipping  bad blocks\n"
594         "bcbonly fw-size fw1-off [fw2-off] - write only BCB (FCB and DBBT)\n"
595         "       where `fw-size` is fw sizes in bytes, `fw1-off`\n"
596         "       and `fw2-off` - firmware offsets\n"
597         "       FIY, BCB isn't erased automatically, so mtd erase should\n"
598         "       be called in advance before writing new BCB:\n"
599         "           > mtd erase mx7-bcb";
600 #endif
601
602 U_BOOT_CMD(nandbcb, 5, 1, do_nandbcb,
603            "i.MX6/i.MX7 NAND Boot Control Blocks write",
604            nandbcb_help_text
605 );