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
7 Signed-off-by: Axel Gembe <ago@bastart.eu.org>
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(-)
14 --- a/drivers/mtd/maps/Kconfig
15 +++ b/drivers/mtd/maps/Kconfig
16 @@ -257,6 +257,13 @@ config MTD_ALCHEMY
18 Flash memory access on AMD Alchemy Pb/Db/RDK Reference Boards
21 + tristate "BCM963xx Flash device"
22 + depends on MIPS && BCM63XX
24 + Flash memory access on BCM963xx boards. Currently only works with
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
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)
41 @@ -162,6 +162,14 @@ static int parse_redboot_partitions(stru
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);
53 for (i = 0; i < numslots; i++) {
54 struct fis_list *new_fl, **prev;
56 @@ -184,9 +192,8 @@ static int parse_redboot_partitions(stru
57 new_fl->img = &buf[i];
59 buf[i].flash_base -= fis_origin;
61 - buf[i].flash_base &= master->size-1;
63 + buf[i].flash_base &= (master->size << 1) - 1;
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 @@ -61,3 +61,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
75 +++ b/drivers/mtd/maps/bcm963xx-flash.c
78 + * Copyright (C) 2006-2008 Florian Fainelli <florian@openwrt.org>
79 + * Mike Albon <malbon@openwrt.org>
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.
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.
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
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/vmalloc.h>
102 +#include <linux/platform_device.h>
104 +#include <bcm_tag.h>
107 +#define BUSWIDTH 2 /* Buswidth */
108 +#define EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
110 +#define PFX KBUILD_MODNAME ": "
112 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
113 +static struct mtd_partition *parsed_parts;
115 +static struct mtd_info *bcm963xx_mtd_info;
117 +static struct map_info bcm963xx_map = {
118 + .name = "bcm963xx",
119 + .bankwidth = BUSWIDTH,
123 +static int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
125 + int nrparts = 3, curpart = 0; /* CFE,NVRAM and global LINUX are always present. */
126 + struct bcm_tag *buf;
127 + struct mtd_partition *parts;
130 + unsigned int rootfsaddr, kerneladdr, spareaddr;
131 + unsigned int rootfslen, kernellen, sparelen, totallen;
137 + /* Allocate memory for buffer */
138 + buf = vmalloc(sizeof(struct bcm_tag));
143 + ret = master->read(master,0x10000,sizeof(struct bcm_tag), &retlen, (void *)buf);
144 + if (retlen != sizeof(struct bcm_tag)){
149 + sscanf(buf->kernelAddress, "%u", &kerneladdr);
150 + sscanf(buf->kernelLength, "%u", &kernellen);
151 + sscanf(buf->totalLength, "%u", &totallen);
152 + tagversion = &(buf->tagVersion[0]);
153 + boardid = &(buf->boardid[0]);
155 + printk(KERN_INFO PFX "CFE boot tag found with version %s and board type %s\n",tagversion, boardid);
157 + kerneladdr = kerneladdr - EXTENDED_SIZE;
158 + rootfsaddr = kerneladdr + kernellen;
159 + spareaddr = roundup(totallen, master->erasesize) + master->erasesize;
160 + sparelen = master->size - spareaddr - master->erasesize;
161 + rootfslen = spareaddr - rootfsaddr;
163 + /* Determine number of partitions */
165 + if (rootfslen > 0){
169 + if (kernellen > 0) {
174 + /* Ask kernel for more memory */
175 + parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
181 + /* Start building partition list */
182 + parts[curpart].name = "CFE";
183 + parts[curpart].offset = 0;
184 + parts[curpart].size = master->erasesize;
187 + if (kernellen > 0) {
188 + parts[curpart].name = "kernel";
189 + parts[curpart].offset = kerneladdr;
190 + parts[curpart].size = kernellen;
194 + if (rootfslen > 0) {
195 + parts[curpart].name = "rootfs";
196 + parts[curpart].offset = rootfsaddr;
197 + parts[curpart].size = rootfslen;
199 + parts[curpart].size += sparelen;
203 + parts[curpart].name = "nvram";
204 + parts[curpart].offset = master->size - master->erasesize;
205 + parts[curpart].size = master->erasesize;
207 + /* Global partition "linux" to make easy firmware upgrade */
209 + parts[curpart].name = "linux";
210 + parts[curpart].offset = parts[0].size;
211 + parts[curpart].size = master->size - parts[0].size - parts[3].size;
213 + for (i = 0; i < nrparts; i++)
214 + 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));
216 + printk(KERN_INFO PFX "Spare partition is %x offset and length %x\n", spareaddr, sparelen);
223 +static int bcm963xx_detect_cfe(struct mtd_info *master)
225 + int idoffset = 0x4e0;
226 + static char idstring[8] = "CFE1CFE1";
231 + ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
233 + printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
235 + return strncmp(idstring, buf, 8);
238 +static int bcm963xx_probe(struct platform_device *pdev)
241 + int parsed_nr_parts = 0;
243 + struct resource *r;
245 + r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
246 + bcm963xx_map.phys = r->start;
247 + bcm963xx_map.size = (r->end - r->start) + 1;
248 + bcm963xx_map.virt = ioremap(r->start, r->end - r->start + 1);
250 + if (!bcm963xx_map.virt) {
251 + printk(KERN_ERR PFX "Failed to ioremap\n");
254 + printk(KERN_INFO PFX "0x%08lx at 0x%08x\n", bcm963xx_map.size, bcm963xx_map.phys);
256 + simple_map_init(&bcm963xx_map);
258 + bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
259 + if (!bcm963xx_mtd_info) {
260 + printk(KERN_ERR PFX "Failed to probe using CFI\n");
265 + bcm963xx_mtd_info->owner = THIS_MODULE;
267 + /* This is mutually exclusive */
268 + if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
269 + printk(KERN_INFO PFX "CFE bootloader detected\n");
270 + if (parsed_nr_parts == 0) {
271 + int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
274 + parsed_nr_parts = ret;
278 + printk(KERN_INFO PFX "assuming RedBoot bootloader\n");
279 + if (bcm963xx_mtd_info->size > 0x00400000) {
280 + printk(KERN_INFO PFX "Support for extended flash memory size : 0x%lx ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
281 + bcm963xx_map.virt = (u32)(EXTENDED_SIZE);
284 +#ifdef CONFIG_MTD_REDBOOT_PARTS
285 + if (parsed_nr_parts == 0) {
286 + int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
288 + part_type = "RedBoot";
289 + parsed_nr_parts = ret;
295 + return add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
298 + iounmap(bcm963xx_map.virt);
302 +static int bcm963xx_remove(struct platform_device *pdev)
304 + if (bcm963xx_mtd_info) {
305 + del_mtd_partitions(bcm963xx_mtd_info);
306 + map_destroy(bcm963xx_mtd_info);
309 + if (bcm963xx_map.virt) {
310 + iounmap(bcm963xx_map.virt);
311 + bcm963xx_map.virt = 0;
317 +static struct platform_driver bcm63xx_mtd_dev = {
318 + .probe = bcm963xx_probe,
319 + .remove = bcm963xx_remove,
321 + .name = "bcm963xx-flash",
322 + .owner = THIS_MODULE,
326 +static int __init bcm963xx_mtd_init(void)
328 + return platform_driver_register(&bcm63xx_mtd_dev);
331 +static void __exit bcm963xx_mtd_exit(void)
333 + platform_driver_unregister(&bcm63xx_mtd_dev);
336 +module_init(bcm963xx_mtd_init);
337 +module_exit(bcm963xx_mtd_exit);
339 +MODULE_LICENSE("GPL");
340 +MODULE_DESCRIPTION("Broadcom BCM63xx MTD partition parser/mapping for CFE and RedBoot");
341 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
342 +MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
343 --- a/arch/mips/bcm63xx/boards/board_bcm963xx.c
344 +++ b/arch/mips/bcm63xx/boards/board_bcm963xx.c
345 @@ -721,20 +721,6 @@ static int board_get_mac_address(u8 *mac
349 -static struct mtd_partition mtd_partitions[] = {
357 -static struct physmap_flash_data flash_data = {
359 - .nr_parts = ARRAY_SIZE(mtd_partitions),
360 - .parts = mtd_partitions,
363 static struct resource mtd_resources[] = {
365 .start = 0, /* filled at runtime */
366 @@ -744,12 +730,9 @@ static struct resource mtd_resources[] =
369 static struct platform_device mtd_dev = {
370 - .name = "physmap-flash",
371 + .name = "bcm963xx-flash",
372 .resource = mtd_resources,
373 .num_resources = ARRAY_SIZE(mtd_resources),
375 - .platform_data = &flash_data,