]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx53/flash.c
karo: tx6: add support for TX6 HW Rev. 3
[karo-tx-uboot.git] / board / karo / tx53 / flash.c
1 /*
2  * Copyright (C) 2011-2014 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, mtd->erasesize - 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, buf, 1, page, 0, 1);
176                 if (ret) {
177                         printf("Failed to write FCB to block %u: %d\n", block, ret);
178                 }
179                 chip->select_chip(mtd, -1);
180         }
181         return ret;
182 }
183
184 #define chk_overlap(a,b)                                \
185         ((a##_start_block <= b##_end_block &&           \
186                 a##_end_block >= b##_start_block) ||    \
187         (b##_start_block <= a##_end_block &&            \
188                 b##_end_block >= a##_start_block))
189
190 #define fail_if_overlap(a,b,m1,m2) do {                                 \
191         if (!doit)                                                      \
192                 printf("check: %s[%lu..%lu] <> %s[%lu..%lu]\n",         \
193                         m1, a##_start_block, a##_end_block,             \
194                         m2, b##_start_block, b##_end_block);            \
195         if (a##_end_block < a##_start_block)                            \
196                 printf("Invalid start/end block # for %s\n", m1);       \
197         if (b##_end_block < b##_start_block)                            \
198                 printf("Invalid start/end block # for %s\n", m2);       \
199         if (chk_overlap(a, b)) {                                        \
200                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
201                         m1, a##_start_block, a##_end_block,             \
202                         m2, b##_start_block, b##_end_block);            \
203                 return -EINVAL;                                         \
204         }                                                               \
205 } while (0)
206
207 static int tx53_prog_uboot(void *addr, int start_block, int skip,
208                         size_t size, size_t max_len)
209 {
210         int ret;
211         nand_erase_options_t erase_opts = { 0, };
212         size_t actual;
213         size_t prg_length = max_len - skip * mtd->erasesize;
214         int prg_start = start_block * mtd->erasesize;
215
216         erase_opts.offset = (start_block - skip) * mtd->erasesize;
217         erase_opts.length = max_len;
218         erase_opts.quiet = 1;
219
220         printf("Erasing flash @ %08llx..%08llx\n", erase_opts.offset,
221                 erase_opts.offset + erase_opts.length - 1);
222         if (doit) {
223                 ret = nand_erase_opts(mtd, &erase_opts);
224                 if (ret) {
225                         printf("Failed to erase flash: %d\n", ret);
226                         return ret;
227                 }
228         }
229
230         printf("Programming flash @ %08x..%08x from %p\n",
231                 prg_start, prg_start + size - 1, addr);
232         if (doit) {
233                 actual = size;
234                 ret = nand_write_skip_bad(mtd, prg_start, &actual, NULL,
235                                         prg_length, addr, WITH_DROP_FFS);
236                 if (ret) {
237                         printf("Failed to program flash: %d\n", ret);
238                         return ret;
239                 }
240                 if (actual < size) {
241                         printf("Could only write %u of %u bytes\n", actual, size);
242                         return -EIO;
243                 }
244         }
245         return 0;
246 }
247
248 #ifdef CONFIG_ENV_IS_IN_NAND
249 #ifndef CONFIG_ENV_OFFSET_REDUND
250 #define TOTAL_ENV_SIZE CONFIG_ENV_RANGE
251 #else
252 #define TOTAL_ENV_SIZE (CONFIG_ENV_RANGE * 2)
253 #endif
254 #endif
255
256 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
257 {
258         int ret;
259         const unsigned long fcb_start_block = 0, fcb_end_block = 0;
260         int erase_size = mtd->erasesize;
261         int page_size = mtd->writesize;
262         void *buf;
263         char *load_addr;
264         char *file_size;
265         size_t size = 0;
266         void *addr = NULL;
267         struct mx53_fcb *fcb;
268         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
269 #ifdef CONFIG_ENV_IS_IN_NAND
270         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
271         unsigned long env_end_block = env_start_block +
272                 DIV_ROUND_UP(TOTAL_ENV_SIZE, mtd->erasesize) - 1;
273 #endif
274         int optind;
275         int fw2_set = 0;
276         unsigned long fw1_start_block = 0, fw1_end_block;
277         unsigned long fw2_start_block = 0, fw2_end_block;
278         unsigned long fw_num_blocks;
279         int fw1_skip, fw2_skip;
280         unsigned long extra_blocks = 0;
281         size_t max_len1, max_len2;
282         struct mtd_device *dev;
283         struct part_info *part_info;
284         struct part_info *redund_part_info;
285         const char *uboot_part = "u-boot";
286         const char *redund_part = NULL;
287         u8 part_num;
288         u8 redund_part_num;
289
290         ret = mtdparts_init();
291         if (ret)
292                 return ret;
293
294         doit = true;
295         for (optind = 1; optind < argc; optind++) {
296                 char *endp;
297
298                 if (strcmp(argv[optind], "-f") == 0) {
299                         if (optind >= argc - 1) {
300                                 printf("Option %s requires an argument\n",
301                                         argv[optind]);
302                                 return -EINVAL;
303                         }
304                         optind++;
305                         fw1_start_block = simple_strtoul(argv[optind], &endp, 0);
306                         if (*endp != '\0') {
307                                 uboot_part = argv[optind];
308                                 continue;
309                         }
310                         uboot_part = NULL;
311                         if (fw1_start_block >= mtd_num_blocks) {
312                                 printf("Block number %lu is out of range: 0..%lu\n",
313                                         fw1_start_block, mtd_num_blocks - 1);
314                                 return -EINVAL;
315                         }
316                 } else if (strcmp(argv[optind], "-r") == 0) {
317                         fw2_set = 1;
318                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
319                                 optind++;
320                                 fw2_start_block = simple_strtoul(argv[optind],
321                                                                 &endp, 0);
322                                 if (*endp != '\0') {
323                                         redund_part = argv[optind];
324                                         continue;
325                                 }
326                                 if (fw2_start_block >= mtd_num_blocks) {
327                                         printf("Block number %lu is out of range: 0..%lu\n",
328                                                 fw2_start_block,
329                                                 mtd_num_blocks - 1);
330                                         return -EINVAL;
331                                 }
332                         }
333                 } else if (strcmp(argv[optind], "-e") == 0) {
334                         if (optind >= argc - 1) {
335                                 printf("Option %s requires an argument\n",
336                                         argv[optind]);
337                                 return -EINVAL;
338                         }
339                         optind++;
340                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
341                         if (extra_blocks >= mtd_num_blocks) {
342                                 printf("Extra block count %lu is out of range: 0..%lu\n",
343                                         extra_blocks,
344                                         mtd_num_blocks - 1);
345                                 return -EINVAL;
346                         }
347                 } else if (strcmp(argv[optind], "-n") == 0) {
348                         doit = false;
349                 } else if (argv[optind][0] == '-') {
350                         printf("Unrecognized option %s\n", argv[optind]);
351                         return -EINVAL;
352                 } else {
353                         break;
354                 }
355         }
356
357         load_addr = getenv("fileaddr");
358         file_size = getenv("filesize");
359
360         if (argc - optind < 1 && load_addr == NULL) {
361                 printf("Load address not specified\n");
362                 return -EINVAL;
363         }
364         if (argc - optind < 2 && file_size == NULL) {
365                 if (uboot_part) {
366                         printf("WARNING: Image size not specified; overwriting whole '%s' partition\n",
367                                 uboot_part);
368                         printf("This will only work, if there are no bad blocks inside this partition!\n");
369                 } else {
370                         printf("ERROR: Image size must be specified\n");
371                         return -EINVAL;
372                 }
373         }
374         if (argc > optind) {
375                 load_addr = NULL;
376                 addr = (void *)simple_strtoul(argv[optind], NULL, 16);
377                 optind++;
378         }
379         if (argc > optind) {
380                 file_size = NULL;
381                 size = simple_strtoul(argv[optind], NULL, 16);
382                 optind++;
383         }
384         if (load_addr != NULL) {
385                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
386                 printf("Using default load address %p\n", addr);
387         }
388         if (file_size != NULL) {
389                 size = simple_strtoul(file_size, NULL, 16);
390                 printf("Using default file size %08x\n", size);
391         }
392         if (size > 0)
393                 fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
394         else
395                 fw_num_blocks = 0;
396
397         if (uboot_part) {
398                 ret = find_dev_and_part(uboot_part, &dev, &part_num,
399                                         &part_info);
400                 if (ret) {
401                         printf("Failed to find '%s' partition: %d\n",
402                                 uboot_part, ret);
403                         return ret;
404                 }
405                 fw1_start_block = part_info->offset / mtd->erasesize;
406                 max_len1 = part_info->size;
407                 /*
408                  * Skip one block, if the U-Boot image resides in the
409                  * same partition as the FCB
410                  */
411                 if (fw1_start_block == fcb_start_block) {
412                         fw1_start_block++;
413                         max_len1 -= mtd->erasesize;
414                 }
415                 if (size == 0)
416                         fw_num_blocks = max_len1 / mtd->erasesize;
417         } else {
418                 max_len1 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
419         }
420
421         if (redund_part) {
422                 ret = find_dev_and_part(redund_part, &dev, &redund_part_num,
423                                         &redund_part_info);
424                 if (ret) {
425                         printf("Failed to find '%s' partition: %d\n",
426                                 redund_part, ret);
427                         return ret;
428                 }
429                 fw2_start_block = redund_part_info->offset / mtd->erasesize;
430                 max_len2 = redund_part_info->size;
431                 if (fw2_start_block == fcb_start_block) {
432                         fw2_start_block++;
433                         max_len2 -= mtd->erasesize;
434                 }
435                 if (size == 0)
436                         fw_num_blocks = max_len2 / mtd->erasesize;
437         } else if (fw2_set) {
438                 max_len2 = (fw_num_blocks + extra_blocks) * mtd->erasesize;
439         } else {
440                 max_len2 = 0;
441         }
442
443         fw1_skip = find_contig_space(fw1_start_block, fw_num_blocks,
444                                 max_len1 / mtd->erasesize);
445         if (fw1_skip < 0) {
446                 printf("Could not find %lu contiguous good blocks for fw image in blocks %lu..%lu\n",
447                         fw_num_blocks, fw1_start_block,
448                         fw1_start_block + max_len1 / mtd->erasesize - 1);
449                 if (uboot_part) {
450 #ifdef CONFIG_ENV_IS_IN_NAND
451                         if (part_info->offset <= CONFIG_ENV_OFFSET + TOTAL_ENV_SIZE) {
452                                 printf("Use a different partition\n");
453                         } else {
454                                 printf("Increase the size of the '%s' partition\n",
455                                         uboot_part);
456                         }
457 #else
458                         printf("Increase the size of the '%s' partition\n",
459                                 uboot_part);
460 #endif
461                 } else {
462                         printf("Increase the number of spare blocks to use with the '-e' option\n");
463                 }
464                 return -ENOSPC;
465         }
466         if (extra_blocks)
467                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
468         else
469                 fw1_end_block = fw1_start_block + fw_num_blocks + fw1_skip - 1;
470
471         if (fw2_set && fw2_start_block == 0)
472                 fw2_start_block = fw1_end_block + 1;
473         if (fw2_start_block > 0) {
474                 fw2_skip = find_contig_space(fw2_start_block, fw_num_blocks,
475                                         max_len2 / mtd->erasesize);
476                 if (fw2_skip < 0) {
477                         printf("Could not find %lu contiguous good blocks for redundant fw image in blocks %lu..%lu\n",
478                                 fw_num_blocks, fw2_start_block,
479                                 fw2_start_block + max_len2 / mtd->erasesize - 1);
480                         if (redund_part) {
481                                 printf("Increase the size of the '%s' partition or use a different partition\n",
482                                         redund_part);
483                         } else {
484                                 printf("Increase the number of spare blocks to use with the '-e' option\n");
485                         }
486                         return -ENOSPC;
487                 }
488         } else {
489                 fw2_skip = 0;
490         }
491         if (extra_blocks)
492                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
493         else
494                 fw2_end_block = fw2_start_block + fw_num_blocks + fw2_skip - 1;
495
496 #ifdef CONFIG_ENV_IS_IN_NAND
497         fail_if_overlap(fcb, env, "FCB", "Environment");
498         fail_if_overlap(fw1, env, "FW1", "Environment");
499 #endif
500         fail_if_overlap(fw1, fcb, "FW1", "FCB");
501         if (fw2_set) {
502                 fail_if_overlap(fw2, fcb, "FW2", "FCB");
503 #ifdef CONFIG_ENV_IS_IN_NAND
504                 fail_if_overlap(fw2, env, "FW2", "Environment");
505 #endif
506                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
507         }
508         fw1_start_block += fw1_skip;
509         fw2_start_block += fw2_skip;
510
511         buf = malloc(erase_size);
512         if (buf == NULL) {
513                 printf("Failed to allocate buffer\n");
514                 return -ENOMEM;
515         }
516
517         fcb = create_fcb(buf, fw1_start_block,
518                         fw2_start_block, fw_num_blocks);
519         if (IS_ERR(fcb)) {
520                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
521                 free(buf);
522                 return PTR_ERR(fcb);
523         }
524
525         ret = write_fcb(buf, fcb_start_block);
526         free(buf);
527         if (ret) {
528                 printf("Failed to write FCB to block %lu\n", fcb_start_block);
529                 return ret;
530         }
531
532         if (size & (page_size - 1)) {
533                 memset(addr + size, 0xff, size & (page_size - 1));
534                 size = ALIGN(size, page_size);
535         }
536
537         printf("Programming U-Boot image from %p to block %lu @ %08llx\n",
538                 addr, fw1_start_block, (u64)fw1_start_block * mtd->erasesize);
539         ret = tx53_prog_uboot(addr, fw1_start_block, fw1_skip, size,
540                         max_len1);
541         if (ret || fw2_start_block == 0)
542                 return ret;
543
544         printf("Programming redundant U-Boot image to block %lu @ %08llx\n",
545                 fw2_start_block, (u64)fw2_start_block * mtd->erasesize);
546         ret = tx53_prog_uboot(addr, fw2_start_block, fw2_skip, size,
547                         max_len2);
548         return ret;
549 }
550
551 U_BOOT_CMD(romupdate, 11, 0, do_update,
552         "Creates an FCB data structure and writes an U-Boot image to flash",
553         "[-f {<part>|block#}] [-r [{<part>|block#}]] [-e #] [<address>] [<length>]\n"
554         "\t-f <part>\twrite bootloader image to partition <part>\n"
555         "\t-f #\t\twrite bootloader image at block # (decimal)\n"
556         "\t-r\t\twrite redundant bootloader image at next free block after first image\n"
557         "\t-r <part>\twrite redundant bootloader image to partition <part>\n"
558         "\t-r #\t\twrite redundant bootloader image at block # (decimal)\n"
559         "\t-e #\t\tspecify number of redundant blocks per boot loader image\n"
560         "\t\t\t(only valid if -f or -r specify a flash address rather than a partition name)\n"
561         "\t-n\t\tshow what would be done without actually updating the flash\n"
562         "\t<address>\tRAM address of bootloader image (default: ${fileaddr})\n"
563         "\t<length>\tlength of bootloader image in RAM (default: ${filesize})"
564         );