[brcm63xx] add support for sysupgrade (#5117)
[openwrt/svn-archive/archive.git] / target / linux / brcm63xx / files / drivers / mtd / maps / bcm963xx-flash.c
1 /*
2 * Copyright (C) 2006-2008 Florian Fainelli <florian@openwrt.org>
3 * Mike Albon <malbon@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/mtd/map.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/partitions.h>
25 #include <linux/vmalloc.h>
26 #include <linux/platform_device.h>
27
28 #include <bcm_tag.h>
29 #include <asm/io.h>
30
31 #define BUSWIDTH 2 /* Buswidth */
32 #define EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
33
34 #define PFX KBUILD_MODNAME ": "
35
36 extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
37 static struct mtd_partition *parsed_parts;
38
39 static struct mtd_info *bcm963xx_mtd_info;
40
41 static struct map_info bcm963xx_map = {
42 .name = "bcm963xx",
43 .bankwidth = BUSWIDTH,
44 };
45
46 static int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
47 {
48 int nrparts = 3, curpart = 0; /* CFE,NVRAM and global LINUX are always present. */
49 struct bcm_tag *buf;
50 struct mtd_partition *parts;
51 int ret;
52 size_t retlen;
53 unsigned int rootfsaddr, kerneladdr, spareaddr;
54 unsigned int rootfslen, kernellen, sparelen, totallen;
55 int namelen = 0;
56 int i;
57
58 /* Allocate memory for buffer */
59 buf = vmalloc(sizeof(struct bcm_tag));
60 if (!buf)
61 return -ENOMEM;
62
63 /* Get the tag */
64 ret = master->read(master,master->erasesize,sizeof(struct bcm_tag), &retlen, (void *)buf);
65 if (retlen != sizeof(struct bcm_tag)){
66 vfree(buf);
67 return -EIO;
68 }
69 printk(KERN_INFO PFX "CFE boot tag found with version %s and board type %s.\n",buf->tagVersion,buf->boardid);
70
71 /* Get the values and calculate */
72 sscanf(buf->rootAddress,"%u", &rootfsaddr);
73 rootfsaddr = rootfsaddr - EXTENDED_SIZE;
74 sscanf(buf->rootLength, "%u", &rootfslen);
75 sscanf(buf->kernelAddress, "%u", &kerneladdr);
76 kerneladdr = kerneladdr - EXTENDED_SIZE;
77 sscanf(buf->kernelLength, "%u", &kernellen);
78 sscanf(buf->totalLength, "%u", &totallen);
79 spareaddr = roundup(totallen,master->erasesize) + master->erasesize;
80 sparelen = master->size - spareaddr - master->erasesize;
81
82 /* Determine number of partitions */
83 namelen = 8;
84 if (rootfslen > 0){
85 nrparts++;
86 namelen =+ 6;
87 };
88 if (kernellen > 0) {
89 nrparts++;
90 namelen =+ 6;
91 };
92
93 /* Ask kernel for more memory */
94 parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
95 if (!parts) {
96 vfree(buf);
97 return -ENOMEM;
98 };
99
100 /* Start building partition list */
101 parts[curpart].name = "CFE";
102 parts[curpart].offset = 0;
103 parts[curpart].size = master->erasesize;
104 curpart++;
105
106 if (kernellen > 0) {
107 parts[curpart].name = "kernel";
108 parts[curpart].offset = kerneladdr;
109 parts[curpart].size = kernellen;
110 curpart++;
111 };
112
113 if (rootfslen > 0) {
114 parts[curpart].name = "rootfs";
115 parts[curpart].offset = rootfsaddr;
116 parts[curpart].size = rootfslen;
117 if (sparelen > 0)
118 parts[curpart].size += sparelen;
119 curpart++;
120 };
121
122 parts[curpart].name = "nvram";
123 parts[curpart].offset = master->size - master->erasesize;
124 parts[curpart].size = master->erasesize;
125
126 /* Global partition "linux" to make easy firmware upgrade */
127 curpart++;
128 parts[curpart].name = "linux";
129 parts[curpart].offset = parts[0].size;
130 parts[curpart].size = master->size - parts[0].size - parts[3].size;
131
132 for (i = 0; i < nrparts; i++)
133 printk(KERN_INFO PFX "Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
134
135 *pparts = parts;
136 vfree(buf);
137
138 return nrparts;
139 };
140
141 static int bcm963xx_detect_cfe(struct mtd_info *master)
142 {
143 int idoffset = 0x4e0;
144 static char idstring[8] = "CFE1CFE1";
145 char buf[9];
146 int ret;
147 size_t retlen;
148
149 ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
150 buf[retlen] = 0;
151 printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
152
153 return strncmp(idstring, buf, 8);
154 }
155
156 static int bcm963xx_probe(struct platform_device *pdev)
157 {
158 int err = 0;
159 int parsed_nr_parts = 0;
160 char *part_type;
161 struct resource *r;
162
163 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
164 bcm963xx_map.phys = r->start;
165 bcm963xx_map.size = (r->end - r->start) + 1;
166 bcm963xx_map.virt = ioremap(r->start, r->end - r->start + 1);
167
168 if (!bcm963xx_map.virt) {
169 printk(KERN_ERR PFX "Failed to ioremap\n");
170 return -EIO;
171 }
172 printk(KERN_INFO PFX "0x%08lx at 0x%08x\n", bcm963xx_map.size, bcm963xx_map.phys);
173
174 simple_map_init(&bcm963xx_map);
175
176 bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
177 if (!bcm963xx_mtd_info) {
178 printk(KERN_ERR PFX "Failed to probe using CFI\n");
179 err = -EIO;
180 goto err_probe;
181 }
182
183 bcm963xx_mtd_info->owner = THIS_MODULE;
184
185 /* This is mutually exclusive */
186 if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
187 printk(KERN_INFO PFX "CFE bootloader detected\n");
188 if (parsed_nr_parts == 0) {
189 int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
190 if (ret > 0) {
191 part_type = "CFE";
192 parsed_nr_parts = ret;
193 }
194 }
195 } else {
196 printk(KERN_INFO PFX "assuming RedBoot bootloader\n");
197 if (bcm963xx_mtd_info->size > 0x00400000) {
198 printk(KERN_INFO PFX "Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
199 bcm963xx_map.virt = (u32)(EXTENDED_SIZE);
200 }
201
202 #ifdef CONFIG_MTD_REDBOOT_PARTS
203 if (parsed_nr_parts == 0) {
204 int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
205 if (ret > 0) {
206 part_type = "RedBoot";
207 parsed_nr_parts = ret;
208 }
209 }
210 #endif
211 }
212
213 return add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
214
215 err_probe:
216 iounmap(bcm963xx_map.virt);
217 return err;
218 }
219
220 static int bcm963xx_remove(struct platform_device *pdev)
221 {
222 if (bcm963xx_mtd_info) {
223 del_mtd_partitions(bcm963xx_mtd_info);
224 map_destroy(bcm963xx_mtd_info);
225 }
226
227 if (bcm963xx_map.virt) {
228 iounmap(bcm963xx_map.virt);
229 bcm963xx_map.virt = 0;
230 }
231
232 return 0;
233 }
234
235 static struct platform_driver bcm63xx_mtd_dev = {
236 .probe = bcm963xx_probe,
237 .remove = bcm963xx_remove,
238 .driver = {
239 .name = "bcm963xx-flash",
240 .owner = THIS_MODULE,
241 },
242 };
243
244 static int __init bcm963xx_mtd_init(void)
245 {
246 return platform_driver_register(&bcm63xx_mtd_dev);
247 }
248
249 static void __exit bcm963xx_mtd_exit(void)
250 {
251 platform_driver_unregister(&bcm63xx_mtd_dev);
252 }
253
254 module_init(bcm963xx_mtd_init);
255 module_exit(bcm963xx_mtd_exit);
256
257 MODULE_LICENSE("GPL");
258 MODULE_DESCRIPTION("Broadcom BCM63xx MTD partition parser/mapping for CFE and RedBoot");
259 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
260 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");