]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/karo/common/nand.c
karo: tx6: rework PMIC code to allow for different configs for same chip
[karo-tx-uboot.git] / board / karo / common / nand.c
1 /*
2  * (C) Copyright 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 <errno.h>
20 #include <libfdt.h>
21 #include <fdt_support.h>
22 #include <nand.h>
23 #include <mxcfb.h>
24 #include <linux/list.h>
25 #include <linux/fb.h>
26 #include <jffs2/load_kernel.h>
27 #include <malloc.h>
28
29 #include "karo.h"
30
31 #ifdef CONFIG_MAX_DTB_SIZE
32 #define MAX_DTB_SIZE    CONFIG_MAX_DTB_SIZE
33 #else
34 #define MAX_DTB_SIZE    SZ_64K
35 #endif
36
37 DECLARE_GLOBAL_DATA_PTR;
38
39 int karo_load_nand_part(const char *part, void *addr, size_t len)
40 {
41         int ret;
42         struct mtd_device *dev;
43         struct part_info *part_info;
44         u8 part_num;
45
46         debug("Initializing mtd_parts\n");
47         ret = mtdparts_init();
48         if (ret)
49                 return ret;
50
51         debug("Trying to find NAND partition '%s'\n", part);
52         ret = find_dev_and_part(part, &dev, &part_num, &part_info);
53         if (ret) {
54                 printf("Failed to find flash partition '%s': %d\n",
55                         part, ret);
56
57                 return ret;
58         }
59         debug("Found partition '%s': offset=%08llx size=%08llx\n",
60                 part, (u64)part_info->offset, (u64)part_info->size);
61
62         if (part_info->size < len)
63                 len = part_info->size;
64
65         debug("Reading NAND partition '%s' to %p\n", part, addr);
66         ret = nand_read_skip_bad(&nand_info[0], part_info->offset, &len,
67                                 NULL, part_info->size, addr);
68         if (ret) {
69                 printf("Failed to load partition '%s' to %p\n", part, addr);
70                 return ret;
71         }
72
73         debug("Read %u byte from partition '%s' @ offset %08llx\n",
74                 len, part, (u64)part_info->offset);
75         return 0;
76 }
77
78 #if defined(CONFIG_SPLASH_SCREEN) && defined(CONFIG_MTDPARTS)
79 static int erase_flash(loff_t offs, size_t len)
80 {
81         nand_erase_options_t nand_erase_options;
82
83         memset(&nand_erase_options, 0, sizeof(nand_erase_options));
84         nand_erase_options.length = len;
85         nand_erase_options.offset = offs;
86
87         return nand_erase_opts(&nand_info[0], &nand_erase_options);
88 }
89
90 int do_fbdump(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
91 {
92         int ret;
93         size_t fbsize = calc_fbsize();
94         const char *part = "logo";
95         struct mtd_device *dev;
96         struct part_info *part_info;
97         u8 part_num;
98         u_char *addr = (u_char *)gd->fb_base;
99
100         if (argc > 2)
101                 return CMD_RET_USAGE;
102
103         if (argc == 2)
104                 part = argv[1];
105
106         if (!addr) {
107                 printf("fb address unknown\n");
108                 return CMD_RET_FAILURE;
109         }
110
111         debug("Initializing mtd_parts\n");
112         ret = mtdparts_init();
113         if (ret)
114                 return CMD_RET_FAILURE;
115
116         debug("Trying to find NAND partition '%s'\n", part);
117         ret = find_dev_and_part(part, &dev, &part_num,
118                                 &part_info);
119         if (ret) {
120                 printf("Failed to find flash partition '%s': %d\n",
121                         part, ret);
122                 return CMD_RET_FAILURE;
123         }
124         debug("Found partition '%s': offset=%08x size=%08x\n",
125                 part, part_info->offset, part_info->size);
126         if (part_info->size < fbsize) {
127                 printf("Error: partition '%s' smaller than frame buffer size: %u\n",
128                         part, fbsize);
129                 return CMD_RET_FAILURE;
130         }
131         debug("Writing framebuffer %p to NAND partition '%s'\n",
132                 addr, part);
133
134         ret = erase_flash(part_info->offset, fbsize);
135         if (ret) {
136                 printf("Failed to erase partition '%s'\n", part);
137                 return CMD_RET_FAILURE;
138         }
139
140         ret = nand_write_skip_bad(&nand_info[0], part_info->offset,
141                                 &fbsize, NULL, part_info->size,
142                                 addr, WITH_DROP_FFS);
143         if (ret) {
144                 printf("Failed to write partition '%s'\n", part);
145                 return CMD_RET_FAILURE;
146         }
147
148         debug("Wrote %u byte from %p to partition '%s' @ offset %08x\n",
149                 fbsize, addr, part, part_info->offset);
150
151         return CMD_RET_SUCCESS;
152 }
153
154 U_BOOT_CMD(fbdump, 2, 0, do_fbdump, "dump framebuffer contents to flash",
155         "[partition name]\n"
156         "       default partition name: 'logo'\n");
157 #endif