c1b424d5d8157dca26019575c8d5a96644dc38f0
[openwrt/staging/yousong.git] / target / linux / brcm63xx / patches-2.6.35 / 040-bcm963xx_flashmap.patch
1 From e734ace5baa04e0e8af1d4483475fbd6bd2b32a1 Mon Sep 17 00:00:00 2001
2 From: Axel Gembe <ago@bastart.eu.org>
3 Date: Mon, 12 May 2008 18:54:09 +0200
4 Subject: [PATCH] bcm963xx: flashmap support
5
6
7 Signed-off-by: Axel Gembe <ago@bastart.eu.org>
8 ---
9 drivers/mtd/maps/Kconfig | 7 +++++++
10 drivers/mtd/maps/Makefile | 1 +
11 drivers/mtd/redboot.c | 13 ++++++++++---
12 3 files changed, 18 insertions(+), 3 deletions(-)
13
14 --- a/drivers/mtd/maps/Kconfig
15 +++ b/drivers/mtd/maps/Kconfig
16 @@ -251,6 +251,13 @@ config MTD_NETtel
17 help
18 Support for flash chips on NETtel/SecureEdge/SnapGear boards.
19
20 +config MTD_BCM963XX
21 + tristate "BCM963xx Flash device"
22 + depends on MIPS && BCM63XX
23 + help
24 + Flash memory access on BCM963xx boards. Currently only works with
25 + RedBoot and CFE.
26 +
27 config MTD_DILNETPC
28 tristate "CFI Flash device mapped on DIL/Net PC"
29 depends on X86 && MTD_CONCAT && MTD_PARTITIONS && MTD_CFI_INTELEXT && BROKEN
30 --- a/drivers/mtd/redboot.c
31 +++ b/drivers/mtd/redboot.c
32 @@ -39,7 +39,7 @@ static inline int redboot_checksum(struc
33 return 1;
34 }
35
36 -static int parse_redboot_partitions(struct mtd_info *master,
37 +int parse_redboot_partitions(struct mtd_info *master,
38 struct mtd_partition **pparts,
39 unsigned long fis_origin)
40 {
41 @@ -162,6 +162,14 @@ static int parse_redboot_partitions(stru
42 goto out;
43 }
44
45 + if (!fis_origin) {
46 + for (i = 0; i < numslots; i++) {
47 + if (!strncmp(buf[i].name, "RedBoot", 8)) {
48 + fis_origin = (buf[i].flash_base & (master->size << 1) - 1);
49 + }
50 + }
51 + }
52 +
53 for (i = 0; i < numslots; i++) {
54 struct fis_list *new_fl, **prev;
55
56 @@ -184,9 +192,8 @@ static int parse_redboot_partitions(stru
57 new_fl->img = &buf[i];
58 if (fis_origin) {
59 buf[i].flash_base -= fis_origin;
60 - } else {
61 - buf[i].flash_base &= master->size-1;
62 }
63 + buf[i].flash_base &= (master->size << 1) - 1;
64
65 /* I'm sure the JFFS2 code has done me permanent damage.
66 * I now think the following is _normal_
67 --- a/drivers/mtd/maps/Makefile
68 +++ b/drivers/mtd/maps/Makefile
69 @@ -59,3 +59,4 @@ obj-$(CONFIG_MTD_BFIN_ASYNC) += bfin-asy
70 obj-$(CONFIG_MTD_RBTX4939) += rbtx4939-flash.o
71 obj-$(CONFIG_MTD_VMU) += vmu-flash.o
72 obj-$(CONFIG_MTD_GPIO_ADDR) += gpio-addr-flash.o
73 +obj-$(CONFIG_MTD_BCM963XX) += bcm963xx-flash.o
74 --- /dev/null
75 +++ b/drivers/mtd/maps/bcm963xx-flash.c
76 @@ -0,0 +1,314 @@
77 +/*
78 + * Copyright (C) 2006-2008 Florian Fainelli <florian@openwrt.org>
79 + * Mike Albon <malbon@openwrt.org>
80 + *
81 + * This program is free software; you can redistribute it and/or modify
82 + * it under the terms of the GNU General Public License as published by
83 + * the Free Software Foundation; either version 2 of the License, or
84 + * (at your option) any later version.
85 + *
86 + * This program is distributed in the hope that it will be useful,
87 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
88 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
89 + * GNU General Public License for more details.
90 + *
91 + * You should have received a copy of the GNU General Public License
92 + * along with this program; if not, write to the Free Software
93 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
94 + */
95 +
96 +#include <linux/init.h>
97 +#include <linux/kernel.h>
98 +#include <linux/mtd/map.h>
99 +#include <linux/mtd/mtd.h>
100 +#include <linux/mtd/partitions.h>
101 +#include <linux/slab.h>
102 +#include <linux/vmalloc.h>
103 +#include <linux/platform_device.h>
104 +#include <linux/magic.h>
105 +#include <linux/jffs2.h>
106 +
107 +#include <bcm_tag.h>
108 +#include <asm/io.h>
109 +
110 +#define BUSWIDTH 2 /* Buswidth */
111 +#define EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
112 +
113 +#define PFX KBUILD_MODNAME ": "
114 +
115 +struct squashfs_super_block {
116 + __le32 s_magic;
117 + __le32 pad0[9];
118 + __le64 bytes_used;
119 +};
120 +
121 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
122 +static struct mtd_partition *parsed_parts;
123 +
124 +static struct mtd_info *bcm963xx_mtd_info;
125 +
126 +static struct map_info bcm963xx_map = {
127 + .name = "bcm963xx",
128 + .bankwidth = BUSWIDTH,
129 +};
130 +
131 +static int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
132 +{
133 + int nrparts = 3, curpart = 0; /* CFE,NVRAM and global LINUX are always present. */
134 + struct bcm_tag *buf;
135 + struct mtd_partition *parts;
136 + int ret;
137 + size_t retlen;
138 + unsigned int rootfsaddr, kerneladdr, spareaddr;
139 + unsigned int rootfslen, kernellen, sparelen, totallen;
140 + int namelen = 0;
141 + int i, offset;
142 + char *boardid;
143 + char *tagversion;
144 + struct squashfs_super_block sb;
145 +
146 + /* Allocate memory for buffer */
147 + buf = vmalloc(sizeof(struct bcm_tag));
148 + if (!buf)
149 + return -ENOMEM;
150 +
151 + /* Get the tag */
152 + ret = master->read(master, master->erasesize, sizeof(struct bcm_tag), &retlen, (void *)buf);
153 + if (retlen != sizeof(struct bcm_tag)){
154 + vfree(buf);
155 + return -EIO;
156 + }
157 +
158 + sscanf(buf->kernelAddress, "%u", &kerneladdr);
159 + sscanf(buf->kernelLength, "%u", &kernellen);
160 + sscanf(buf->totalLength, "%u", &totallen);
161 + tagversion = &(buf->tagVersion[0]);
162 + boardid = &(buf->boardid[0]);
163 +
164 + printk(KERN_INFO PFX "CFE boot tag found with version %s and board type %s\n",tagversion, boardid);
165 +
166 + kerneladdr = kerneladdr - EXTENDED_SIZE;
167 + rootfsaddr = kerneladdr + kernellen;
168 +
169 + // offset = master->erasesize + sizeof(struct bcm_tag) + kernellen;
170 + offset = rootfsaddr;
171 + ret = master->read(master, offset, sizeof(sb), &retlen, (void *) &sb);
172 + if (ret || (retlen != sizeof(sb))) {
173 + printk(KERN_ALERT PFX "parse_cfe_partitions: error occured while reading "
174 + "from \"%s\"\n", master->name);
175 + return -EINVAL;
176 + }
177 +
178 + // if (SQUASHFS_MAGIC != le32_to_cpu(sb.s_magic) ) {
179 + printk(KERN_DEBUG PFX "Squash magic %08lx: Found: %08lx\n", (long unsigned int)SQUASHFS_MAGIC, (long unsigned int)le32_to_cpu(sb.s_magic));
180 + if ((uint32_t)SQUASHFS_MAGIC != (uint32_t)le32_to_cpu(sb.s_magic) ) {
181 + /* Not a squashfs image */
182 + printk(KERN_DEBUG PFX "No squashfs image in \"%s\"\n", master->name);
183 + printk(KERN_DEBUG PFX "Jffs magic %04x: Found: %04x\n", (uint16_t)(JFFS2_MAGIC_BITMASK), *(uint16_t *)(&sb));
184 + if (*(uint16_t *)(&sb) == JFFS2_MAGIC_BITMASK) {
185 + printk(KERN_DEBUG PFX "jffs image in \"%s\"\n", master->name);
186 + /* Is JFFS2 so have rootfslen so that true length gets calculated */
187 + rootfslen = master->size - master->erasesize - offset;
188 + } else {
189 + /* We only recognize squashfs and jffs2. If it's not either of these,
190 + don't create a rootfs partition. */
191 + printk(KERN_INFO PFX "No known root filesystem in \"%s\"\n", master->name);
192 + rootfslen = 0;
193 + }
194 + } else {
195 + /* Is a squash image so find where the squash ends */
196 + if (le64_to_cpu((sb.bytes_used)) <= 0) {
197 + printk(KERN_ALERT PFX "split_squashfs: squashfs is empty in \"%s\"\n",
198 + master->name);
199 + return 0;
200 + }
201 +
202 + rootfslen = (u32) le64_to_cpu(sb.bytes_used);
203 + }
204 +
205 + rootfslen = ( ( rootfslen % master->erasesize ) > 0 ? (((rootfslen / master->erasesize) + 1 ) * master->erasesize) : rootfslen);
206 + totallen = rootfslen + kernellen + sizeof(struct bcm_tag);
207 +
208 + spareaddr = roundup(totallen, master->erasesize) + master->erasesize;
209 + sparelen = master->size - spareaddr - master->erasesize;
210 +
211 + /* Determine number of partitions */
212 + namelen = 8;
213 + if (rootfslen > 0){
214 + nrparts++;
215 + namelen =+ 6;
216 + };
217 + if (kernellen > 0) {
218 + nrparts++;
219 + namelen =+ 6;
220 + };
221 +
222 + /* Ask kernel for more memory */
223 + parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
224 + if (!parts) {
225 + vfree(buf);
226 + return -ENOMEM;
227 + };
228 +
229 + /* Start building partition list */
230 + parts[curpart].name = "CFE";
231 + parts[curpart].offset = 0;
232 + parts[curpart].size = master->erasesize;
233 + curpart++;
234 +
235 + if (kernellen > 0) {
236 + parts[curpart].name = "kernel";
237 + parts[curpart].offset = kerneladdr;
238 + parts[curpart].size = kernellen;
239 + curpart++;
240 + };
241 +
242 + if (rootfslen > 0) {
243 + parts[curpart].name = "rootfs";
244 + parts[curpart].offset = rootfsaddr;
245 + parts[curpart].size = rootfslen;
246 + if (sparelen > 0)
247 + parts[curpart].size += sparelen;
248 + curpart++;
249 + };
250 +
251 + parts[curpart].name = "nvram";
252 + parts[curpart].offset = master->size - master->erasesize;
253 + parts[curpart].size = master->erasesize;
254 +
255 + /* Global partition "linux" to make easy firmware upgrade */
256 + curpart++;
257 + parts[curpart].name = "linux";
258 + parts[curpart].offset = parts[0].size;
259 + parts[curpart].size = master->size - parts[0].size - parts[3].size;
260 +
261 + for (i = 0; i < nrparts; i++)
262 + printk(KERN_INFO PFX "Partition %d is %s offset %lx and length %lx\n", i, parts[i].name, (long unsigned int)(parts[i].offset), (long unsigned int)(parts[i].size));
263 +
264 + printk(KERN_INFO PFX "Spare partition is %x offset and length %x\n", spareaddr, sparelen);
265 + *pparts = parts;
266 + vfree(buf);
267 +
268 + return nrparts;
269 +};
270 +
271 +static int bcm963xx_detect_cfe(struct mtd_info *master)
272 +{
273 + int idoffset = 0x4e0;
274 + static char idstring[8] = "CFE1CFE1";
275 + char buf[9];
276 + int ret;
277 + size_t retlen;
278 +
279 + ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
280 + buf[retlen] = 0;
281 + printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
282 +
283 + return strncmp(idstring, buf, 8);
284 +}
285 +
286 +static int bcm963xx_probe(struct platform_device *pdev)
287 +{
288 + int err = 0;
289 + int parsed_nr_parts = 0;
290 + char *part_type;
291 + struct resource *r;
292 +
293 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294 + bcm963xx_map.phys = r->start;
295 + bcm963xx_map.size = (r->end - r->start) + 1;
296 + bcm963xx_map.virt = ioremap(r->start, r->end - r->start + 1);
297 +
298 + if (!bcm963xx_map.virt) {
299 + printk(KERN_ERR PFX "Failed to ioremap\n");
300 + return -EIO;
301 + }
302 + printk(KERN_INFO PFX "0x%08lx at 0x%08x\n", bcm963xx_map.size, bcm963xx_map.phys);
303 +
304 + simple_map_init(&bcm963xx_map);
305 +
306 + bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
307 + if (!bcm963xx_mtd_info) {
308 + printk(KERN_ERR PFX "Failed to probe using CFI\n");
309 + err = -EIO;
310 + goto err_probe;
311 + }
312 +
313 + bcm963xx_mtd_info->owner = THIS_MODULE;
314 +
315 + /* This is mutually exclusive */
316 + if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
317 + printk(KERN_INFO PFX "CFE bootloader detected\n");
318 + if (parsed_nr_parts == 0) {
319 + int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
320 + if (ret > 0) {
321 + part_type = "CFE";
322 + parsed_nr_parts = ret;
323 + }
324 + }
325 + } else {
326 + printk(KERN_INFO PFX "assuming RedBoot bootloader\n");
327 + if (bcm963xx_mtd_info->size > 0x00400000) {
328 + printk(KERN_INFO PFX "Support for extended flash memory size : 0x%lx ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
329 + bcm963xx_map.virt = (u32)(EXTENDED_SIZE);
330 + }
331 +
332 +#ifdef CONFIG_MTD_REDBOOT_PARTS
333 + if (parsed_nr_parts == 0) {
334 + int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
335 + if (ret > 0) {
336 + part_type = "RedBoot";
337 + parsed_nr_parts = ret;
338 + }
339 + }
340 +#endif
341 + }
342 +
343 + return add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
344 +
345 +err_probe:
346 + iounmap(bcm963xx_map.virt);
347 + return err;
348 +}
349 +
350 +static int bcm963xx_remove(struct platform_device *pdev)
351 +{
352 + if (bcm963xx_mtd_info) {
353 + del_mtd_partitions(bcm963xx_mtd_info);
354 + map_destroy(bcm963xx_mtd_info);
355 + }
356 +
357 + if (bcm963xx_map.virt) {
358 + iounmap(bcm963xx_map.virt);
359 + bcm963xx_map.virt = 0;
360 + }
361 +
362 + return 0;
363 +}
364 +
365 +static struct platform_driver bcm63xx_mtd_dev = {
366 + .probe = bcm963xx_probe,
367 + .remove = bcm963xx_remove,
368 + .driver = {
369 + .name = "bcm963xx-flash",
370 + .owner = THIS_MODULE,
371 + },
372 +};
373 +
374 +static int __init bcm963xx_mtd_init(void)
375 +{
376 + return platform_driver_register(&bcm63xx_mtd_dev);
377 +}
378 +
379 +static void __exit bcm963xx_mtd_exit(void)
380 +{
381 + platform_driver_unregister(&bcm63xx_mtd_dev);
382 +}
383 +
384 +module_init(bcm963xx_mtd_init);
385 +module_exit(bcm963xx_mtd_exit);
386 +
387 +MODULE_LICENSE("GPL");
388 +MODULE_DESCRIPTION("Broadcom BCM63xx MTD partition parser/mapping for CFE and RedBoot");
389 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
390 +MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
391 --- a/arch/mips/bcm63xx/boards/board_bcm963xx.c
392 +++ b/arch/mips/bcm63xx/boards/board_bcm963xx.c
393 @@ -816,20 +816,6 @@ void __init board_setup(void)
394 panic("unexpected CPU for bcm963xx board");
395 }
396
397 -static struct mtd_partition mtd_partitions[] = {
398 - {
399 - .name = "cfe",
400 - .offset = 0x0,
401 - .size = 0x40000,
402 - }
403 -};
404 -
405 -static struct physmap_flash_data flash_data = {
406 - .width = 2,
407 - .nr_parts = ARRAY_SIZE(mtd_partitions),
408 - .parts = mtd_partitions,
409 -};
410 -
411 static struct resource mtd_resources[] = {
412 {
413 .start = 0, /* filled at runtime */
414 @@ -839,12 +825,9 @@ static struct resource mtd_resources[] =
415 };
416
417 static struct platform_device mtd_dev = {
418 - .name = "physmap-flash",
419 + .name = "bcm963xx-flash",
420 .resource = mtd_resources,
421 .num_resources = ARRAY_SIZE(mtd_resources),
422 - .dev = {
423 - .platform_data = &flash_data,
424 - },
425 };
426
427 static struct gpio_led_platform_data bcm63xx_led_data;