76e994cd954254e0c0f6ad0b5022f63aca84d19c
[karo-tx-uboot.git] / board / karo / tx53 / flash.c
1 /*
2  * Copyright (C) 2011-2015 Lothar WaƟmann <LW@KARO-electronics.de>
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  */
17
18 #include <common.h>
19 #include <malloc.h>
20 #include <nand.h>
21 #include <errno.h>
22
23 #include <linux/err.h>
24 #include <jffs2/load_kernel.h>
25
26 struct mx53_fcb {
27         u32 rsrvd0;
28         u32 fingerprint;
29         u32 version;
30         u32 rsrvd1[23];
31         u32 fw1_start_page;
32         u32 fw2_start_page;
33         u32 rsrvd2[2];
34         u32 dbbt_search_area;
35         u32 bb_mark_phys_offset;
36         u32 rsrvd3[11];
37         u32 bb_swap;
38         u32 bb_mark_byte;
39         u32 rsrvd4[83];
40 };
41
42 static nand_info_t *mtd = &nand_info[0];
43 static bool doit;
44
45 static inline int calc_bb_offset(struct mx53_fcb *fcb)
46 {
47         int ecc_block_size = 512;
48         int ecc_size = mtd->oobsize / (mtd->writesize / ecc_block_size);
49         int bb_mark_chunk, bb_mark_chunk_offs;
50
51         bb_mark_chunk = mtd->writesize / (ecc_block_size + ecc_size);
52         bb_mark_chunk_offs = mtd->writesize % (ecc_block_size + ecc_size);
53         if (bb_mark_chunk_offs > ecc_block_size) {
54                 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
55                         bb_mark_chunk_offs);
56                 return -EINVAL;
57         }
58         printf("BB mark is in block %d offset %d\n",
59                 bb_mark_chunk, bb_mark_chunk_offs);
60
61         return bb_mark_chunk * ecc_block_size + bb_mark_chunk_offs;
62 }
63
64 /*
65  * return number of blocks to skip for a contiguous partition
66  * of given # blocks
67  */
68 static int find_contig_space(int block, int num_blocks, int max_blocks)
69 {
70         int skip = 0;
71         int found = 0;
72         int last = block + max_blocks;
73
74         debug("Searching %u contiguous blocks from %d..%d\n",
75                 num_blocks, block, block + max_blocks - 1);
76         for (; block < last; block++) {
77                 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
78                         skip += found + 1;
79                         found = 0;
80                         debug("Skipping %u blocks to %u\n",
81                                 skip, block + 1);
82                 } else {
83                         found++;
84                         if (found >= num_blocks) {
85                                 debug("Found %u good blocks from %d..%d\n",
86                                         found, block - found + 1, block);
87                                 return skip;
88                         }
89                 }
90         }
91         return -ENOSPC;
92 }
93
94 #define offset_of(p, m)         ((void *)&(p)->m - (void *)(p))
95 #define pr_fcb_val(p, n)        debug("%-24s[%02x]=%08x(%d)\n", #n, offset_of(p, n), (p)->n, (p)->n)
96
97 static struct mx53_fcb *create_fcb(void *buf, int fw1_start_block,
98                                 int fw2_start_block, int fw_num_blocks)
99 {
100         struct mx53_fcb *fcb;
101         u32 sectors_per_block = mtd->erasesize / mtd->writesize;
102
103         fcb = buf;
104
105         memset(fcb, 0x00, sizeof(*fcb));
106         memset(fcb + 1, 0xff, SZ_1K - sizeof(*fcb));
107
108         strncpy((char *)&fcb->fingerprint, "FCB ", 4);
109         fcb->version = 1;
110
111         fcb->fw1_start_page = fw1_start_block * sectors_per_block;
112         pr_fcb_val(fcb, fw1_start_page);
113
114         if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
115                 fcb->fw2_start_page = fw2_start_block * sectors_per_block;
116                 pr_fcb_val(fcb, fw2_start_page);
117         }
118
119         return fcb;
120 }
121
122 static int find_fcb(void *ref, int page)
123 {
124         int ret = 0;
125         struct nand_chip *chip = mtd->priv;
126         void *buf = malloc(mtd->erasesize);
127
128         if (buf == NULL) {
129                 return -ENOMEM;
130         }
131         chip->select_chip(mtd, 0);
132         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
133         ret = chip->ecc.read_page_raw(mtd, chip, buf, 1, page);
134         if (ret) {
135                 printf("Failed to read FCB from page %u: %d\n", page, ret);
136                 goto out;
137         }
138         if (memcmp(buf, ref, mtd->writesize) == 0) {
139                 debug("Found FCB in page %u (%08x)\n",
140                         page, page * mtd->writesize);
141                 ret = 1;
142         }
143 out:
144         chip->select_chip(mtd, -1);
145         free(buf);
146         return ret;
147 }
148
149 static int write_fcb(void *buf, int block)
150 {
151         int ret;
152         struct nand_chip *chip = mtd->priv;
153         int page = block * mtd->erasesize / mtd->writesize;
154
155         ret = find_fcb(buf, page);
156         if (ret > 0) {
157                 printf("FCB at block %d is up to date\n", block);
158                 return 0;
159         }
160
161         printf("Erasing FCB block %08x..%08x\n", block * mtd->erasesize,
162                 block * mtd->erasesize + mtd->erasesize - 1);
163         if (doit) {
164                 ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
165                 if (ret) {
166                         printf("Failed to erase FCB block %u\n", block);
167                         return ret;
168                 }
169         }
170
171         printf("Writing FCB to block %d @ %08llx\n", block,
172                 (u64)block * mtd->erasesize);
173         if (doit) {
174                 chip->select_chip(mtd, 0);
175                 ret = chip->write_page(mtd, chip, 0, mtd->writesize,
176                                 buf, 1, page, 0, 0);
177                 if (ret) {
178                         printf("Failed to write FCB to block %u: %d\n", block, ret);
179                 }
180                 chip->select_chip(mtd, -1);
181         }
182         return ret;
183 }
184
185 #define chk_overlap(a,b)                                \
186         ((a##_start_block <= b##_end_block &&           \
187                 a##_end_block >= b##_start_block) ||    \
188         (b##_start_block <= a##_end_block &&            \
189                 b##_end_block >= a##_start_block))
190
191 #define fail_if_overlap(a,b,m1,m2) do {                                 \
192         if (!doit)                                                      \
193                 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n",         \
194                         m1, a##_start_block, a##_end_block,             \
195                         m2, b##_start_block, b##_end_block);            \
196         if (a##_end_block < a##_start_block)                            \
197                 printf("Invalid start/end block # for %s\n", m1);       \
198         if (b##_end_block < b##_start_block)                            \
199                 printf("Invalid start/end block # for %s\n", m2);       \
200         if (chk_overlap(a, b)) {                                        \
201                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
202                         m1, a##_start_block, a##_end_block,             \
203                         m2, b##_start_block, b##_end_block);            \
204                 return -EINVAL;                                         \
205         }                                                               \
206 } while (0)
207
208 static int tx53_prog_uboot(void *addr, int start_block, int skip,
209                         size_t size, size_t max_len)
210 {
211         int ret;
212         nand_erase_options_t erase_opts = { 0, };
213         size_t actual;
214         size_t prg_length = max_len - skip * mtd->erasesize;
215         int prg_start = (start_block + skip) * mtd->erasesize;
216
217         erase_opts.offset = start_block * mtd->erasesize;
218         erase_opts.length = max_len;
219         erase_opts.quiet = 1;
220
221         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
222                 erase_opts.offset + erase_opts.length - 1);
223         if (doit) {
224                 ret = nand_erase_opts(mtd, &erase_opts);
225                 if (ret) {
226                         printf("Failed to erase flash: %d\n", ret);
227                         return ret;
228                 }
229         }
230
231         printf("Programming flash @ %08x..%08x from %p\n",
232                 prg_start, prg_start + size - 1, addr);
233         if (doit) {
234                 actual = size;
235                 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
236                                         prg_length, addr, WITH_DROP_FFS);
237                 if (ret) {
238                         printf("Failed to program flash: %d\n", ret);
239                         return ret;
240                 }
241                 if (actual < size) {
242                         printf("Could only write %u of %u bytes\n", actual, size);
243                         return -EIO;
244                 }
245         }
246         return 0;
247 }
248
249 #ifdef CONFIG_ENV_IS_IN_NAND
250 #ifndef CONFIG_ENV_OFFSET_REDUND
251 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
252 #else
253 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
254 #endif
255 #endif
256
257 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
258 {
259         int ret;
260         const unsigned long fcb_start_block = 0, fcb_end_block = 0;
261         int erase_size = mtd->erasesize;
262         void *buf;
263         size_t buf_size;
264         char *load_addr;
265         char *file_size;
266         size_t size = 0;
267         void *addr = NULL;
268         struct mx53_fcb *fcb;
269         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
270 #ifdef CONFIG_ENV_IS_IN_NAND
271         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
272         unsigned long env_end_block = env_start_block +
273                 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
274 #endif
275         int optind;
276         int fw2_set = 0;
277         unsigned long fw1_start_block = 0, fw1_end_block;
278         unsigned long fw2_start_block = 0, fw2_end_block;
279         unsigned long fw_num_blocks;
280         int fw1_skip, fw2_skip;
281         unsigned long extra_blocks = 0;
282         size_t max_len1, max_len2;
283         struct mtd_device *dev;
284         struct part_info *part_info;
285         struct part_info *redund_part_info;
286         const char *uboot_part = "u-boot";
287         const char *redund_part = NULL;
288         u8 part_num;
289         u8 redund_part_num;
290
291         ret = mtdparts_init();
292         if (ret)
293                 return ret;
294
295         doit = true;
296         for (optind = 1; optind < argc; optind++) {
297                 char *endp;
298
299                 if (strcmp(argv[optind], "-f") == 0) {
300                         if (optind >= argc - 1) {
301                                 printf("Option %s requires an argument\n",
302                                         argv[optind]);
303                                 return -EINVAL;
304                         }
305                         optind++;
306                         fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
307                         if (*endp != '\0') {
308                                 uboot_part = argv[optind];
309                                 continue;
310                         }
311                         uboot_part = NULL;
312                         if (fw1_start_block >= mtd_num_blocks) {
313                                 printf("Block number %lu is out of range: 0..%lu\n",
314                                         fw1_start_block, mtd_num_blocks - 1);
315                                 return -EINVAL;
316                         }
317                 } else if (strcmp(argv[optind], "-r") == 0) {
318                         fw2_set = 1;
319                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
320                                 optind++;
321                                 fw2_start_block = simple_strtoul(argv[optind],
322                                                                 &endp, 0);
323                                 if (*endp != '\0') {
324                                         redund_part = argv[optind];
325                                         continue;
326                                 }
327                                 if (fw2_start_block >= mtd_num_blocks) {
328                                         printf("Block number %lu is out of range: 0..%lu\n",
329                                                 fw2_start_block,
330                                                 mtd_num_blocks - 1);
331                                         return -EINVAL;
332                                 }
333                         }
334                 } else if (strcmp(argv[optind], "-e") == 0) {
335                         if (optind >= argc - 1) {
336                                 printf("Option %s requires an argument\n",
337                                         argv[optind]);
338                                 return -EINVAL;
339                         }
340                         optind++;
341                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
342                         if (extra_blocks >= mtd_num_blocks) {
343                                 printf("Extra block count %lu is out of range: 0..%lu\n",
344                                         extra_blocks,
345                                         mtd_num_blocks - 1);
346                                 return -EINVAL;
347                         }
348                 } else if (strcmp(argv[optind], "-n") == 0) {
349                         doit = false;
350                 } else if (argv[optind][0] == '-') {
351                         printf("Unrecognized option %s\n", argv[optind]);
352                         return -EINVAL;
353                 } else {
354                         break;
355                 }
356         }
357
358         load_addr = getenv("fileaddr");
359         file_size = getenv("filesize");
360
361         if (argc - optind < 1 && load_addr == NULL) {
362                 printf("Load address not specified\n");
363                 return -EINVAL;
364         }
365         if (argc - optind < 2 && file_size == NULL) {
366                 if (uboot_part) {
367                         printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
368                                 uboot_part);
369                         printf("This will only work, if there are no bad blocks inside this partition!\n");
370                 } else {
371                         printf("ERROR: Image size must be specified\n");
372                         return -EINVAL;
373                 }
374         }
375         if (argc > optind) {
376                 load_addr = NULL;
377                 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
378                 optind++;
379         }
380         if (argc > optind) {
381                 file_size = NULL;
382                 size = simple_strtoul(argv[optind], NULL, 16);
383                 optind++;
384         }
385         if (load_addr != NULL) {
386                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
387                 printf("Using default load address %p\n", addr);
388         }
389         if (file_size != NULL) {
390                 size = simple_strtoul(file_size, NULL, 16);
391                 printf("Using default file size %08x\n", size);
392         }
393         if (size > 0)
394                 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
395         else
396                 fw_num_blocks = 0;
397
398         if (uboot_part) {
399                 ret = find_dev_and_part(uboot_part, &dev, &part_num,
400                                         &part_info);
401                 if (ret) {
402                         printf("Failed to find '%s' partition: %d\n",
403                                 uboot_part, ret);
404                         return ret;
405                 }
406                 fw1_start_block = part_info->offset / mtd->erasesize;
407                 max_len1 = part_info->size;
408                 if (size == 0)
409                         fw_num_blocks = max_len1 / mtd->erasesize;
410         } else {
411                 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
412         }
413
414         if (redund_part) {
415                 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
416                                         &redund_part_info);
417                 if (ret) {
418                         printf("Failed to find '%s' partition: %d\n",
419                                 redund_part, ret);
420                         return ret;
421                 }
422                 fw2_start_block = redund_part_info->offset / mtd->erasesize;
423                 max_len2 = redund_part_info->size;
424                 if (fw2_start_block == fcb_start_block) {
425                         fw2_start_block++;
426                         max_len2 -= mtd->erasesize;
427                 }
428                 if (size == 0)
429                         fw_num_blocks = max_len2 / mtd->erasesize;
430         } else if (fw2_set) {
431                 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
432         } else {
433                 max_len2 = 0;
434         }
435
436         fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
437                                 max_len1 / mtd->erasesize);
438         if (fw1_skip < 0) {
439                 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%lu\n",
440                         fw_num_blocks, fw1_start_block,
441                         fw1_start_block + max_len1 / mtd->erasesize - 1);
442                 if (uboot_part) {
443 #ifdef CONFIG_ENV_IS_IN_NAND
444                         if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
445                                 printf("Use a different partition\n");
446                         } else {
447                                 printf("Increase the size of the '%s' partition\n",
448                                         uboot_part);
449                         }
450 #else
451                         printf("Increase the size of the '%s' partition\n",
452                                 uboot_part);
453 #endif
454                 } else {
455                         printf("Increase the number of spare blocks to use with the '-e' option\n");
456                 }
457                 return -ENOSPC;
458         }
459         if (extra_blocks)
460                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
461         else
462                 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
463
464         if (fw2_set && fw2_start_block == 0)
465                 fw2_start_block = fw1_end_block + 1;
466         if (fw2_start_block > 0) {
467                 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
468                                         max_len2 / mtd->erasesize);
469                 if (fw2_skip < 0) {
470                         printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%lu\n",
471                                 fw_num_blocks, fw2_start_block,
472                                 fw2_start_block + max_len2 / mtd->erasesize - 1);
473                         if (redund_part) {
474                                 printf("Increase the size of the '%s' partition or use a different partition\n",
475                                         redund_part);
476                         } else {
477                                 printf("Increase the number of spare blocks to use with the '-e' option\n");
478                         }
479                         return -ENOSPC;
480                 }
481         } else {
482                 fw2_skip = 0;
483         }
484         if (extra_blocks)
485                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
486         else
487                 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
488
489 #ifdef CONFIG_ENV_IS_IN_NAND
490         fail_if_overlap(fcb, env, "FCB", "Environment");
491         fail_if_overlap(fw1, env, "FW1", "Environment");
492 #endif
493         if (fw2_set) {
494                 fail_if_overlap(fw2, fcb, "FW2", "FCB");
495 #ifdef CONFIG_ENV_IS_IN_NAND
496                 fail_if_overlap(fw2, env, "FW2", "Environment");
497 #endif
498                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
499         }
500         fw1_start_block += fw1_skip;
501         fw2_start_block += fw2_skip;
502
503         buf_size = fw_num_blocks * erase_size;
504         buf = memalign(erase_size, buf_size);
505         if (buf == NULL) {
506                 printf("Failed to allocate buffer\n");
507                 return -ENOMEM;
508         }
509
510         /* copy U-Boot image to buffer */
511         memcpy(buf, addr, size);
512         memset(buf + size, 0xff, buf_size - size);
513
514         fcb = create_fcb(buf, fw1_start_block,
515                         fw2_start_block, fw_num_blocks);
516         if (IS_ERR(fcb)) {
517                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
518                 ret = PTR_ERR(fcb);
519                 goto out;
520         }
521
522         if (fw1_start_block == fcb_start_block) {
523                 printf("Programming FCB + U-Boot image from %p to block %lu @ %08llx\n",
524                         buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
525                 ret = tx53_prog_uboot(buf, fw1_start_block, fw1_skip, size,
526                                 max_len1);
527         } else {
528                 printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
529                         buf, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
530                 ret = tx53_prog_uboot(buf, fw1_start_block, fw1_skip, size,
531                                 max_len1);
532                 if (ret)
533                         goto out;
534
535                 memset(buf + sizeof(*fcb), 0xff,
536                         mtd->writesize - sizeof(*fcb));
537
538                 ret = write_fcb(buf, fcb_start_block);
539                 if (ret) {
540                         printf("Failed to write FCB to block %lu\n", fcb_start_block);
541                         return ret;
542                 }
543                 memset(buf, 0xff, SZ_1K);
544         }
545         if (ret || fw2_start_block == 0)
546                 goto out;
547
548         printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
549                 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
550         ret = tx53_prog_uboot(buf, fw2_start_block, fw2_skip, size,
551                         max_len2);
552 out:
553         free(buf);
554         return ret;
555 }
556
557 U_BOOT_CMD(romupdate, 11, 0, do_update,
558         "Creates an FCB data structure and writes an U-Boot image to flash",
559         "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
560         "\t-f <part>\twrite bootloader image to partition <part>\n"
561         "\t-f #\t\twrite bootloader image at block # (decimal)\n"
562         "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
563         "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
564         "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
565         "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
566         "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
567         "\t-n\t\tshow what would be done without actually updating the flash\n"
568         "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
569         "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"
570         );