d17baf2b055887d03ddfc8a0f2f1c21409481f83
[openwrt.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 = 2, curpart = 0; /* CFE and NVRAM 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 parts[curpart].name = "nvram";
122 parts[curpart].offset = master->size - master->erasesize;
123 parts[curpart].size = master->erasesize;
124
125 for (i = 0; i < nrparts; i++)
126 printk(KERN_INFO PFX "Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
127
128 *pparts = parts;
129 vfree(buf);
130
131 return nrparts;
132 };
133
134 static int bcm963xx_detect_cfe(struct mtd_info *master)
135 {
136 int idoffset = 0x4e0;
137 static char idstring[8] = "CFE1CFE1";
138 char buf[9];
139 int ret;
140 size_t retlen;
141
142 ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
143 buf[retlen] = 0;
144 printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
145
146 return strncmp(idstring, buf, 8);
147 }
148
149 static int bcm963xx_probe(struct platform_device *pdev)
150 {
151 int err = 0;
152 int parsed_nr_parts = 0;
153 char *part_type;
154 struct resource *r;
155
156 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
157 bcm963xx_map.phys = r->start;
158 bcm963xx_map.size = (r->end - r->start) + 1;
159 bcm963xx_map.virt = ioremap(r->start, r->end - r->start + 1);
160
161 if (!bcm963xx_map.virt) {
162 printk(KERN_ERR PFX "Failed to ioremap\n");
163 return -EIO;
164 }
165 printk(KERN_INFO PFX "0x%08lx at 0x%08x\n", bcm963xx_map.size, bcm963xx_map.phys);
166
167 simple_map_init(&bcm963xx_map);
168
169 bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
170 if (!bcm963xx_mtd_info) {
171 printk(KERN_ERR PFX "Failed to probe using CFI\n");
172 err = -EIO;
173 goto err_probe;
174 }
175
176 bcm963xx_mtd_info->owner = THIS_MODULE;
177
178 /* This is mutually exclusive */
179 if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
180 printk(KERN_INFO PFX "CFE bootloader detected\n");
181 if (parsed_nr_parts == 0) {
182 int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
183 if (ret > 0) {
184 part_type = "CFE";
185 parsed_nr_parts = ret;
186 }
187 }
188 } else {
189 printk(KERN_INFO PFX "assuming RedBoot bootloader\n");
190 if (bcm963xx_mtd_info->size > 0x00400000) {
191 printk(KERN_INFO PFX "Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
192 bcm963xx_map.virt = (u32)(EXTENDED_SIZE);
193 }
194
195 #ifdef CONFIG_MTD_REDBOOT_PARTS
196 if (parsed_nr_parts == 0) {
197 int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
198 if (ret > 0) {
199 part_type = "RedBoot";
200 parsed_nr_parts = ret;
201 }
202 }
203 #endif
204 }
205
206 return add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
207
208 err_probe:
209 iounmap(bcm963xx_map.virt);
210 return err;
211 }
212
213 static int bcm963xx_remove(struct platform_device *pdev)
214 {
215 if (bcm963xx_mtd_info) {
216 del_mtd_partitions(bcm963xx_mtd_info);
217 map_destroy(bcm963xx_mtd_info);
218 }
219
220 if (bcm963xx_map.virt) {
221 iounmap(bcm963xx_map.virt);
222 bcm963xx_map.virt = 0;
223 }
224
225 return 0;
226 }
227
228 static struct platform_driver bcm63xx_mtd_dev = {
229 .probe = bcm963xx_probe,
230 .remove = bcm963xx_remove,
231 .driver = {
232 .name = "bcm963xx-flash",
233 .owner = THIS_MODULE,
234 },
235 };
236
237 static int __init bcm963xx_mtd_init(void)
238 {
239 return platform_driver_register(&bcm63xx_mtd_dev);
240 }
241
242 static void __exit bcm963xx_mtd_exit(void)
243 {
244 platform_driver_unregister(&bcm63xx_mtd_dev);
245 }
246
247 module_init(bcm963xx_mtd_init);
248 module_exit(bcm963xx_mtd_exit);
249
250 MODULE_LICENSE("GPL");
251 MODULE_DESCRIPTION("Broadcom BCM63xx MTD partition parser/mapping for CFE and RedBoot");
252 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
253 MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
This page took 0.052164 seconds and 3 git commands to generate.