]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/tx28/flash.c
applied patches from Freescale and Ka-Ro
[karo-tx-uboot.git] / board / karo / tx28 / flash.c
1 #include <common.h>
2 #include <malloc.h>
3 #include <nand.h>
4
5 #include <linux/err.h>
6
7 #include <asm/io.h>
8 #include <asm/sizes.h>
9 #include <asm/errno.h>
10 #include <asm/arch/mxs_gpmi-regs.h>
11 #include <asm/arch/mxs_gpmi-bch-regs.h>
12
13 #define FCB_START_BLOCK         0
14 #define NUM_FCB_BLOCKS          1
15 #define MAX_FCB_BLOCKS          32768
16
17 struct mx28_nand_timing {
18         u8 data_setup;
19         u8 data_hold;
20         u8 address_setup;
21         u8 dsample_time;
22         u8 nand_timing_state;
23         u8 tREA;
24         u8 tRLOH;
25         u8 tRHOH;
26 };
27
28 struct mx28_fcb {
29         u32 checksum;
30         u32 fingerprint;
31         u32 version;
32         struct mx28_nand_timing timing;
33         u32 page_data_size;
34         u32 total_page_size;
35         u32 sectors_per_block;
36         u32 number_of_nands;    /* not used by ROM code */
37         u32 total_internal_die; /* not used by ROM code */
38         u32 cell_type;          /* not used by ROM code */
39         u32 ecc_blockn_type;
40         u32 ecc_block0_size;
41         u32 ecc_blockn_size;
42         u32 ecc_block0_type;
43         u32 metadata_size;
44         u32 ecc_blocks_per_page;
45         u32 rsrvd[6];            /* not used by ROM code */
46         u32 bch_mode;
47         u32 boot_patch;
48         u32 patch_sectors;
49         u32 fw1_start_page;
50         u32 fw2_start_page;
51         u32 fw1_sectors;
52         u32 fw2_sectors;
53         u32 dbbt_search_area;
54         u32 bb_mark_byte;
55         u32 bb_mark_startbit;
56         u32 bb_mark_phys_offset;
57 };
58
59 struct mx28_dbbt_header {
60         u32 checksum;
61         u32 fingerprint;
62         u32 version;
63         u32 number_bb;
64         u32 number_pages;
65         u8 spare[492];
66 };
67
68 struct mx28_dbbt {
69         u32 nand_number;
70         u32 number_bb;
71         u32 bb_num[2040 / 4];
72 };
73
74 #define BF_VAL(v, bf)           (((v) & BM_##bf) >> BP_##bf)
75
76 static nand_info_t *mtd = &nand_info[0];
77
78 extern void *_start;
79
80 #define BIT(v,n)        (((v) >> (n)) & 0x1)
81
82 static u8 calculate_parity_13_8(u8 d)
83 {
84         u8 p = 0;
85
86         p |= (BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 3) ^ BIT(d, 2))             << 0;
87         p |= (BIT(d, 7) ^ BIT(d, 5) ^ BIT(d, 4) ^ BIT(d, 2) ^ BIT(d, 1)) << 1;
88         p |= (BIT(d, 7) ^ BIT(d, 6) ^ BIT(d, 5) ^ BIT(d, 1) ^ BIT(d, 0)) << 2;
89         p |= (BIT(d, 7) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 0))             << 3;
90         p |= (BIT(d, 6) ^ BIT(d, 4) ^ BIT(d, 3) ^ BIT(d, 2) ^ BIT(d, 1) ^ BIT(d, 0)) << 4;
91         return p;
92 }
93
94 static void encode_hamming_13_8(void *_src, void *_ecc, size_t size)
95 {
96         int i;
97         u8 *src = _src;
98         u8 *ecc = _ecc;
99
100         for (i = 0; i < size; i++)
101                 ecc[i] = calculate_parity_13_8(src[i]);
102 }
103
104 static u32 calc_chksum(void *buf, size_t size)
105 {
106         u32 chksum;
107         u8 *bp = buf;
108         size_t i;
109
110         for (i = 0; i < size; i++) {
111                 chksum += bp[i];
112         }
113         return ~chksum;
114 }
115
116 /*
117   Physical organisation of data in NAND flash:
118   metadata
119   payload chunk 0 (may be empty)
120   ecc for metadata + payload chunk 0
121   payload chunk 1
122   ecc for payload chunk 1
123 ...
124   payload chunk n
125   ecc for payload chunk n
126  */
127
128 static int calc_bb_offset(nand_info_t *mtd, struct mx28_fcb *fcb)
129 {
130         int bb_mark_offset;
131         int chunk_data_size = fcb->ecc_blockn_size * 8;
132         int chunk_ecc_size = (fcb->ecc_blockn_type << 1) * 13;
133         int chunk_total_size = chunk_data_size + chunk_ecc_size;
134         int bb_mark_chunk, bb_mark_chunk_offs;
135
136         bb_mark_offset = (mtd->writesize - fcb->metadata_size) * 8;
137         if (fcb->ecc_block0_size == 0)
138                 bb_mark_offset -= (fcb->ecc_block0_type << 1) * 13;
139
140         bb_mark_chunk = bb_mark_offset / chunk_total_size;
141         bb_mark_chunk_offs = bb_mark_offset - (bb_mark_chunk * chunk_total_size);
142         if (bb_mark_chunk_offs > chunk_data_size) {
143                 printf("Unsupported ECC layout; BB mark resides in ECC data: %u\n",
144                         bb_mark_chunk_offs);
145                 return -EINVAL;
146         }
147         bb_mark_offset -= bb_mark_chunk * chunk_ecc_size;
148         return bb_mark_offset;
149 }
150
151 static struct mx28_fcb *create_fcb(void *buf, int fw1_start_block, int fw2_start_block,
152                                 size_t fw_size)
153 {
154         volatile void *gpmi_base = __ioremap(GPMI_BASE_ADDR, SZ_4K, 1);
155         volatile void *bch_base = __ioremap(BCH_BASE_ADDR, SZ_4K, 1);
156         u32 fl0, fl1;
157         u32 t0, t1;
158         int metadata_size;
159         int bb_mark_bit_offs;
160         struct mx28_fcb *fcb;
161         int fcb_offs;
162
163         if (gpmi_base == NULL || bch_base == NULL) {
164                 return ERR_PTR(-ENOMEM);
165         }
166
167         fl0 = readl(bch_base + HW_BCH_FLASH0LAYOUT0);
168         fl1 = readl(bch_base + HW_BCH_FLASH0LAYOUT1);
169         t0 = readl(gpmi_base + HW_GPMI_TIMING0);
170         t1 = readl(gpmi_base + HW_GPMI_TIMING1);
171
172         metadata_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_META_SIZE);
173
174         fcb = buf + ALIGN(metadata_size, 4);
175         fcb_offs = (void *)fcb - buf;
176
177         memset(buf, 0xff, fcb_offs);
178         memset(fcb, 0x00, sizeof(*fcb));
179         memset(fcb + 1, 0xff, mtd->erasesize - fcb_offs - sizeof(*fcb));
180
181         strncpy((char *)&fcb->fingerprint, "FCB ", 4);
182         fcb->version = cpu_to_be32(1);
183
184         fcb->timing.data_setup = BF_VAL(t0, GPMI_TIMING0_DATA_SETUP);
185         fcb->timing.data_hold = BF_VAL(t0, GPMI_TIMING0_DATA_HOLD);
186         fcb->timing.address_setup = BF_VAL(t0, GPMI_TIMING0_ADDRESS_SETUP);
187
188         fcb->page_data_size = mtd->writesize;
189         fcb->total_page_size = mtd->writesize + mtd->oobsize;
190         fcb->sectors_per_block = mtd->erasesize / mtd->writesize;
191
192         fcb->ecc_block0_type = BF_VAL(fl0, BCH_FLASH0LAYOUT0_ECC0);
193         fcb->ecc_block0_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_DATA0_SIZE);
194         fcb->ecc_blockn_type = BF_VAL(fl1, BCH_FLASH0LAYOUT1_ECCN);
195         fcb->ecc_blockn_size = BF_VAL(fl1, BCH_FLASH0LAYOUT1_DATAN_SIZE);
196
197         fcb->metadata_size = BF_VAL(fl0, BCH_FLASH0LAYOUT0_META_SIZE);
198         fcb->ecc_blocks_per_page = BF_VAL(fl0, BCH_FLASH0LAYOUT0_NBLOCKS);
199         fcb->bch_mode = readl(bch_base + HW_BCH_MODE);
200 /*
201         fcb->boot_patch = 0;
202         fcb->patch_sectors = 0;
203 */
204         fcb->fw1_start_page = fw1_start_block * mtd->erasesize / mtd->writesize;
205         fcb->fw1_sectors = DIV_ROUND_UP(fw_size, mtd->writesize);
206
207         if (fw2_start_block != 0 && fw2_start_block < mtd->size / mtd->erasesize) {
208                 fcb->fw2_start_page = fw2_start_block * mtd->erasesize / mtd->writesize;
209                 fcb->fw2_sectors = fcb->fw1_sectors;
210         }
211
212         fcb->dbbt_search_area = 1;
213
214         bb_mark_bit_offs = calc_bb_offset(mtd, fcb);
215         if (bb_mark_bit_offs < 0)
216                 return ERR_PTR(bb_mark_bit_offs);
217         fcb->bb_mark_byte = bb_mark_bit_offs / 8;
218         fcb->bb_mark_startbit = bb_mark_bit_offs % 8;
219         fcb->bb_mark_phys_offset = mtd->writesize;
220
221         fcb->checksum = calc_chksum(&fcb->fingerprint, 512 - 4);
222         return fcb;
223 }
224
225 static int find_fcb(void *ref, int page)
226 {
227         int ret = 0;
228         struct nand_chip *chip = mtd->priv;
229         void *buf = malloc(mtd->erasesize);
230
231         if (buf == NULL) {
232                 return -ENOMEM;
233         }
234         chip->select_chip(mtd, 0);
235         chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
236         ret = chip->ecc.read_page_raw(mtd, chip, buf);
237         if (ret) {
238                 printf("Failed to read FCB from page %u: %d\n", page, ret);
239                 return ret;
240         }
241         chip->select_chip(mtd, -1);
242         if (memcmp(buf, ref, mtd->writesize) == 0) {
243                 printf("%s: Found FCB in page %u (%08x)\n", __func__,
244                         page, page * mtd->writesize);
245                 ret = 1;
246         }
247         free(buf);
248         return ret;
249 }
250
251 static int write_fcb(void *buf, int block)
252 {
253         int ret;
254         struct nand_chip *chip = mtd->priv;
255         int page = block * mtd->erasesize / mtd->writesize;
256
257         ret = find_fcb(buf, page);
258         if (ret > 0) {
259                 printf("FCB at block %d is up to date\n", block);
260                 return 0;
261         }
262
263         ret = nand_erase(mtd, block * mtd->erasesize, mtd->erasesize);
264         if (ret) {
265                 printf("Failed to erase FCB block %u\n", block);
266                 return ret;
267         }
268
269         printf("Writing FCB to block %d @ %08x\n", block,
270                 block * mtd->erasesize);
271         chip->select_chip(mtd, 0);
272         ret = chip->write_page(mtd, chip, buf, page, 0, 1);
273         if (ret) {
274                 printf("Failed to write FCB to block %u: %d\n", block, ret);
275         }
276         chip->select_chip(mtd, -1);
277         return ret;
278 }
279
280 #define chk_overlap(a,b)                                \
281         ((a##_start_block <= b##_end_block &&           \
282                 a##_end_block >= b##_start_block) ||    \
283         (b##_start_block <= a##_end_block &&            \
284                 b##_end_block >= a##_start_block))
285
286 #define fail_if_overlap(a,b,m1,m2) do {                         \
287         if (chk_overlap(a, b)) {                                \
288                 printf("%s blocks %lu..%lu overlap %s in blocks %lu..%lu!\n", \
289                         m1, a##_start_block, a##_end_block,     \
290                         m2, b##_start_block, b##_end_block);    \
291                 return -EINVAL;                                 \
292         }                                                       \
293 } while (0)
294
295 int do_update(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
296 {
297         int ret;
298         int block;
299         int erase_size = mtd->erasesize;
300         int page_size = mtd->writesize;
301         void *buf;
302         char *load_addr;
303         char *file_size;
304         size_t size = 0;
305         void *addr = NULL;
306         struct mx28_fcb *fcb;
307         unsigned long fcb_start_block = FCB_START_BLOCK;
308         unsigned long num_fcb_blocks = NUM_FCB_BLOCKS;
309         unsigned long fcb_end_block;
310         unsigned long mtd_num_blocks = mtd->size / mtd->erasesize;
311         unsigned long env_start_block = CONFIG_ENV_OFFSET / mtd->erasesize;
312         unsigned long env_end_block = env_start_block +
313                 DIV_ROUND_UP(CONFIG_ENV_SIZE, mtd->erasesize) - 1;
314         int optind;
315         int fw1_set = 0;
316         int fw2_set = 0;
317         unsigned long fw1_start_block = 0, fw1_end_block;
318         unsigned long fw2_start_block = 0, fw2_end_block;
319         unsigned long fw_num_blocks;
320         unsigned long extra_blocks = 2;
321         nand_erase_options_t erase_opts = { 0, };
322         int fcb_written = 0;
323
324         load_addr = getenv("fileaddr");
325         file_size = getenv("filesize");
326
327         if (argc < 2 && load_addr == NULL) {
328                 printf("Load address not specified\n");
329                 return -EINVAL;
330         }
331         if (argc < 3 && file_size == NULL) {
332                 printf("Image size not specified\n");
333                 return -EINVAL;
334         }
335
336         for (optind = 1; optind < argc; optind++) {
337                 if (strcmp(argv[optind], "-b") == 0) {
338                         if (optind >= argc - 1) {
339                                 printf("Option %s requires an argument\n", argv[optind]);
340                                 return -EINVAL;
341                         }
342                         optind++;
343                         fcb_start_block = simple_strtoul(argv[optind], NULL, 0);
344                         if (fcb_start_block >= mtd_num_blocks) {
345                                 printf("Block number %lu is out of range: 0..%lu\n",
346                                         fcb_start_block, mtd_num_blocks - 1);
347                                 return -EINVAL;
348                         }
349                 } else if (strcmp(argv[optind], "-n") == 0) {
350                         if (optind >= argc - 1) {
351                                 printf("Option %s requires an argument\n", argv[optind]);
352                                 return -EINVAL;
353                         }
354                         optind++;
355                         num_fcb_blocks = simple_strtoul(argv[optind], NULL, 0);
356                         if (num_fcb_blocks > MAX_FCB_BLOCKS) {
357                                 printf("Extraneous number of FCB blocks; max. allowed: %u\n",
358                                         MAX_FCB_BLOCKS);
359                                 return -EINVAL;
360                         }
361                 } else if (strcmp(argv[optind], "-f") == 0) {
362                         if (optind >= argc - 1) {
363                                 printf("Option %s requires an argument\n", argv[optind]);
364                                 return -EINVAL;
365                         }
366                         optind++;
367                         fw1_start_block = simple_strtoul(argv[optind], NULL, 0);
368                         if (fw1_start_block >= mtd_num_blocks) {
369                                 printf("Block number %lu is out of range: 0..%lu\n",
370                                         fw1_start_block,
371                                         mtd_num_blocks - 1);
372                                 return -EINVAL;
373                         }
374                         fw1_set = 1;
375                 } else if (strcmp(argv[optind], "-r") == 0) {
376                         if (optind < argc - 1 && argv[optind + 1][0] != '-') {
377                                 optind++;
378                                 fw2_start_block = simple_strtoul(argv[optind], NULL, 0);
379                                 if (fw2_start_block >= mtd_num_blocks) {
380                                         printf("Block number %lu is out of range: 0..%lu\n",
381                                                 fw2_start_block,
382                                                 mtd_num_blocks - 1);
383                                         return -EINVAL;
384                                 }
385                         }
386                         fw2_set = 1;
387                 } else if (strcmp(argv[optind], "-e") == 0) {
388                         if (optind >= argc - 1) {
389                                 printf("Option %s requires an argument\n", argv[optind]);
390                                 return -EINVAL;
391                         }
392                         optind++;
393                         extra_blocks = simple_strtoul(argv[optind], NULL, 0);
394                         if (extra_blocks >= mtd_num_blocks) {
395                                 printf("Extra block count %lu is out of range: 0..%lu\n",
396                                         extra_blocks,
397                                         mtd_num_blocks - 1);
398                                 return -EINVAL;
399                         }
400                 } else if (argv[optind][0] == '-') {
401                         printf("Unrecognized option %s\n", argv[optind]);
402                         return -EINVAL;
403                 }
404         }
405         if (argc > optind) {
406                 load_addr = NULL;
407                 addr = (void *)simple_strtoul(argv[optind], NULL, 0);
408                 optind++;
409         }
410         if (argc > optind) {
411                 file_size = NULL;
412                 size = simple_strtoul(argv[optind], NULL, 0);
413                 optind++;
414         }
415         if (load_addr != NULL) {
416                 addr = (void *)simple_strtoul(load_addr, NULL, 16);
417                 printf("Using default load address %p\n", addr);
418         }
419         if (file_size != NULL) {
420                 size = simple_strtoul(file_size, NULL, 16);
421                 printf("Using default file size %08x\n", size);
422         }
423         fcb_end_block = fcb_start_block + num_fcb_blocks - 1;
424         fw_num_blocks = DIV_ROUND_UP(size, mtd->erasesize);
425
426         if (!fw1_set) {
427                 fw1_start_block = fcb_end_block + 1;
428                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
429                 if (chk_overlap(fw1, env)) {
430                         fw1_start_block = env_end_block + 1;
431                         fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
432                 }
433         } else {
434                 fw1_end_block = fw1_start_block + fw_num_blocks + extra_blocks - 1;
435         }
436
437         if (fw2_set && fw2_start_block == 0) {
438                 fw2_start_block = fw1_end_block + 1;
439                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
440                 if (chk_overlap(fw2, env)) {
441                         fw2_start_block = env_end_block + 1;
442                         fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
443                 }
444         } else {
445                 fw2_end_block = fw2_start_block + fw_num_blocks + extra_blocks - 1;
446         }
447
448         fail_if_overlap(fcb, env, "FCB", "Environment");
449         fail_if_overlap(fcb, fw1, "FCB", "FW1");
450         fail_if_overlap(fw1, env, "FW1", "Environment");
451         if (fw2_set) {
452                 fail_if_overlap(fcb, fw2, "FCB", "FW2");
453                 fail_if_overlap(fw2, env, "FW2", "Environment");
454                 fail_if_overlap(fw1, fw2, "FW1", "FW2");
455         }
456
457         buf = malloc(erase_size);
458         if (buf == NULL) {
459                 printf("Failed to allocate buffer\n");
460                 return -ENOMEM;
461         }
462         /* search for first non-bad block in FW1 block range */
463         while (fw1_start_block <= fw1_end_block) {
464                 if (!nand_block_isbad(mtd, fw1_start_block * mtd->erasesize))
465                         break;
466                 fw1_start_block++;
467         }
468         if (fw1_end_block - fw1_start_block + 1 < fw_num_blocks) {
469                 printf("Too many bad blocks in FW1 block range: %lu..%lu\n",
470                         fw1_end_block + 1 - fw_num_blocks - extra_blocks,
471                         fw1_end_block);
472                 return -EINVAL;
473         }
474
475         /* search for first non-bad block in FW2 block range */
476         while (fw2_set && fw2_start_block <= fw2_end_block) {
477                 if (!nand_block_isbad(mtd, fw2_start_block * mtd->erasesize))
478                         break;
479                 fw2_start_block++;
480         }
481         if (fw2_end_block - fw2_start_block + 1 < fw_num_blocks) {
482                 printf("Too many bad blocks in FW2 area %08lx..%08lx\n",
483                         fw2_end_block + 1 - fw_num_blocks - extra_blocks,
484                         fw2_end_block);
485                 return -EINVAL;
486         }
487
488         fcb = create_fcb(buf, fw1_start_block, fw2_start_block,
489                         (fw_num_blocks + extra_blocks) * mtd->erasesize);
490         if (IS_ERR(fcb)) {
491                 printf("Failed to initialize FCB: %ld\n", PTR_ERR(fcb));
492                 return PTR_ERR(fcb);
493         }
494         encode_hamming_13_8(fcb, (void *)fcb + 512, 512);
495
496         for (block = fcb_start_block; block < fcb_start_block + num_fcb_blocks;
497              block++) {
498                 if (nand_block_isbad(mtd, block * mtd->erasesize)) {
499                         if (block == fcb_start_block)
500                                 fcb_start_block++;
501                         continue;
502                 }
503                 ret = write_fcb(buf, block);
504                 if (ret) {
505                         printf("Failed to write FCB to block %u\n", block);
506                         return ret;
507                 }
508                 fcb_written = 1;
509         }
510
511         if (!fcb_written) {
512                 printf("Could not write FCB to flash\n");
513                 return -EIO;
514         }
515
516         printf("Programming U-Boot image from %p to block %lu\n",
517                 addr, fw1_start_block);
518         if (size & (page_size - 1)) {
519                 memset(addr + size, 0xff, size & (page_size - 1));
520                 size = ALIGN(size, page_size);
521         }
522
523         erase_opts.offset = fcb->fw1_start_page * page_size;
524         erase_opts.length = ALIGN(size, erase_size) +
525                 extra_blocks * mtd->erasesize;
526         erase_opts.quiet = 1;
527
528         printf("Erasing flash @ %08lx..%08lx\n", erase_opts.offset,
529                 erase_opts.offset + erase_opts.length - 1);
530
531         ret = nand_erase_opts(mtd, &erase_opts);
532         if (ret) {
533                 printf("Failed to erase flash: %d\n", ret);
534                 return ret;
535         }
536         printf("Programming flash @ %08x..%08x from %p\n",
537                 fcb->fw1_start_page * page_size,
538                 fcb->fw1_start_page * page_size + size, addr);
539         ret = nand_write_skip_bad(mtd, fcb->fw1_start_page * page_size,
540                                 &size, addr);
541         if (ret) {
542                 printf("Failed to program flash: %d\n", ret);
543                 return ret;
544         }
545         if (fw2_start_block == 0) {
546                 return ret;
547         }
548
549         printf("Programming redundant U-Boot image to block %lu\n",
550                 fw2_start_block);
551         erase_opts.offset = fcb->fw2_start_page * page_size;
552         printf("Erasing flash @ %08lx..%08lx\n", erase_opts.offset,
553                 erase_opts.offset + erase_opts.length - 1);
554
555         ret = nand_erase_opts(mtd, &erase_opts);
556         if (ret) {
557                 printf("Failed to erase flash: %d\n", ret);
558                 return ret;
559         }
560         printf("Programming flash @ %08x..%08x from %p\n",
561                 fcb->fw2_start_page * page_size,
562                 fcb->fw2_start_page * page_size + size, addr);
563         ret = nand_write_skip_bad(mtd, fcb->fw2_start_page * page_size,
564                                 &size, addr);
565         if (ret) {
566                 printf("Failed to program flash: %d\n", ret);
567                 return ret;
568         }
569         return ret;
570 }
571
572 U_BOOT_CMD(romupdate, 11, 0, do_update,
573         "Creates an FCB data structure and writes an U-Boot image to flash\n",
574         "[-b #] [-n #] [-f #] [-r [#]] [<address>] [<length>]\n"
575         "\t-b #\tfirst FCB block number (default 0)\n"
576         "\t-n #\ttotal number of FCB blocks (default 1)\n"
577         "\t-f #\twrite bootloader image at block #\n"
578         "\t-r\twrite redundant bootloader image at next free block after first image\n"
579         "\t-r #\twrite redundant bootloader image at block #\n"
580         "\t-e #\tspecify number of redundant blocks per boot loader image\n"
581         "\t<address>\tRAM address of bootloader image (default: ${fileaddr}\n"
582         "\t<length>\tlength of bootloader image in RAM (default: ${filesize}"
583         );