137b08f1592d5f347c2e05d4d7a61f9a1c3ff05b
[librecmc/librecmc.git] / target / linux / brcm63xx / patches-2.6.32 / 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 Index: linux-2.6.32.10/drivers/mtd/maps/Kconfig
15 ===================================================================
16 --- linux-2.6.32.10.orig/drivers/mtd/maps/Kconfig       2010-03-29 06:35:59.987293878 -0400
17 +++ linux-2.6.32.10/drivers/mtd/maps/Kconfig    2010-03-29 06:37:14.968545954 -0400
18 @@ -259,6 +259,13 @@
19         help
20           Flash memory access on AMD Alchemy Pb/Db/RDK Reference Boards
21  
22 +config MTD_BCM963XX
23 +        tristate "BCM963xx Flash device"
24 +        depends on MIPS && BCM63XX
25 +        help
26 +         Flash memory access on BCM963xx boards. Currently only works with
27 +         RedBoot and CFE.
28 +
29  config MTD_DILNETPC
30         tristate "CFI Flash device mapped on DIL/Net PC"
31         depends on X86 && MTD_CONCAT && MTD_PARTITIONS && MTD_CFI_INTELEXT && BROKEN
32 Index: linux-2.6.32.10/drivers/mtd/redboot.c
33 ===================================================================
34 --- linux-2.6.32.10.orig/drivers/mtd/redboot.c  2010-03-29 06:36:00.017292877 -0400
35 +++ linux-2.6.32.10/drivers/mtd/redboot.c       2010-03-29 06:37:14.968545954 -0400
36 @@ -39,7 +39,7 @@
37         return 1;
38  }
39  
40 -static int parse_redboot_partitions(struct mtd_info *master,
41 +int parse_redboot_partitions(struct mtd_info *master,
42                               struct mtd_partition **pparts,
43                               unsigned long fis_origin)
44  {
45 @@ -162,6 +162,14 @@
46                 goto out;
47         }
48  
49 +       if (!fis_origin) {
50 +               for (i = 0; i < numslots; i++) {
51 +                       if (!strncmp(buf[i].name, "RedBoot", 8)) {
52 +                               fis_origin = (buf[i].flash_base & (master->size << 1) - 1);
53 +                       }
54 +               }
55 +       }
56 +
57         for (i = 0; i < numslots; i++) {
58                 struct fis_list *new_fl, **prev;
59  
60 @@ -184,9 +192,8 @@
61                 new_fl->img = &buf[i];
62                  if (fis_origin) {
63                          buf[i].flash_base -= fis_origin;
64 -                } else {
65 -                        buf[i].flash_base &= master->size-1;
66                  }
67 +               buf[i].flash_base &= (master->size << 1) - 1;
68  
69                 /* I'm sure the JFFS2 code has done me permanent damage.
70                  * I now think the following is _normal_
71 Index: linux-2.6.32.10/drivers/mtd/maps/Makefile
72 ===================================================================
73 --- linux-2.6.32.10.orig/drivers/mtd/maps/Makefile      2010-03-29 06:35:59.947294290 -0400
74 +++ linux-2.6.32.10/drivers/mtd/maps/Makefile   2010-03-29 06:37:14.968545954 -0400
75 @@ -61,3 +61,4 @@
76  obj-$(CONFIG_MTD_RBTX4939)     += rbtx4939-flash.o
77  obj-$(CONFIG_MTD_VMU)          += vmu-flash.o
78  obj-$(CONFIG_MTD_GPIO_ADDR)    += gpio-addr-flash.o
79 +obj-$(CONFIG_MTD_BCM963XX)     += bcm963xx-flash.o
80 Index: linux-2.6.32.10/drivers/mtd/maps/bcm963xx-flash.c
81 ===================================================================
82 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
83 +++ linux-2.6.32.10/drivers/mtd/maps/bcm963xx-flash.c   2010-03-29 13:33:58.797288897 -0400
84 @@ -0,0 +1,266 @@
85 +/*
86 + * Copyright (C) 2006-2008  Florian Fainelli <florian@openwrt.org>
87 + *                         Mike Albon <malbon@openwrt.org>
88 + *
89 + * This program is free software; you can redistribute it and/or modify
90 + * it under the terms of the GNU General Public License as published by
91 + * the Free Software Foundation; either version 2 of the License, or
92 + * (at your option) any later version.
93 + *
94 + * This program is distributed in the hope that it will be useful,
95 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
96 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
97 + * GNU General Public License for more details.
98 + *
99 + * You should have received a copy of the GNU General Public License
100 + * along with this program; if not, write to the Free Software
101 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
102 + */
103 +
104 +#include <linux/init.h>
105 +#include <linux/kernel.h>
106 +#include <linux/mtd/map.h>
107 +#include <linux/mtd/mtd.h>
108 +#include <linux/mtd/partitions.h>
109 +#include <linux/vmalloc.h>
110 +#include <linux/platform_device.h>
111 +
112 +#include <bcm_tag.h>
113 +#include <asm/io.h>
114 +
115 +#define BUSWIDTH 2                     /* Buswidth */
116 +#define EXTENDED_SIZE 0xBFC00000       /* Extended flash address */
117 +
118 +#define PFX KBUILD_MODNAME ": "
119 +
120 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
121 +static struct mtd_partition *parsed_parts;
122 +
123 +static struct mtd_info *bcm963xx_mtd_info;
124 +
125 +static struct map_info bcm963xx_map = {
126 +       .name           = "bcm963xx",
127 +       .bankwidth      = BUSWIDTH,
128 +};
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;
142 +       char *boardid;
143 +        char *tagversion;
144 +
145 +       /* Allocate memory for buffer */
146 +       buf = vmalloc(sizeof(struct bcm_tag));
147 +       if (!buf)
148 +               return -ENOMEM;
149 +
150 +       /* Get the tag */
151 +       ret = master->read(master,0x10000,sizeof(struct bcm_tag), &retlen, (void *)buf);
152 +       if (retlen != sizeof(struct bcm_tag)){
153 +               vfree(buf);
154 +               return -EIO;
155 +       }
156 +
157 +       sscanf(buf->kernelAddress, "%u", &kerneladdr);
158 +       sscanf(buf->kernelLength, "%u", &kernellen);
159 +       sscanf(buf->totalLength, "%u", &totallen);
160 +       tagversion = &(buf->tagVersion[0]);
161 +       boardid = &(buf->boardid[0]);
162 +
163 +       printk(KERN_INFO PFX "CFE boot tag found with version %s and board type %s\n",tagversion, boardid);
164 +
165 +       kerneladdr = kerneladdr - EXTENDED_SIZE;
166 +       rootfsaddr = kerneladdr + kernellen;
167 +       spareaddr = roundup(totallen, master->erasesize) + master->erasesize;
168 +       sparelen = master->size - spareaddr - master->erasesize;
169 +       rootfslen = spareaddr - rootfsaddr;
170 +
171 +       /* Determine number of partitions */
172 +       namelen = 8;
173 +       if (rootfslen > 0){
174 +               nrparts++;
175 +               namelen =+ 6;
176 +       };
177 +       if (kernellen > 0) {
178 +               nrparts++;
179 +               namelen =+ 6;
180 +       };
181 +
182 +       /* Ask kernel for more memory */
183 +       parts = kzalloc(sizeof(*parts) * nrparts + 10 * nrparts, GFP_KERNEL);
184 +       if (!parts) {
185 +               vfree(buf);
186 +               return -ENOMEM;
187 +       };
188 +
189 +       /* Start building partition list */
190 +       parts[curpart].name = "CFE";
191 +       parts[curpart].offset = 0;
192 +       parts[curpart].size = master->erasesize;
193 +       curpart++;
194 +
195 +       if (kernellen > 0) {
196 +               parts[curpart].name = "kernel";
197 +               parts[curpart].offset = kerneladdr;
198 +               parts[curpart].size = kernellen;
199 +               curpart++;
200 +       };
201 +
202 +       if (rootfslen > 0) {
203 +               parts[curpart].name = "rootfs";
204 +               parts[curpart].offset = rootfsaddr;
205 +               parts[curpart].size = rootfslen;
206 +               if (sparelen > 0)
207 +                       parts[curpart].size += sparelen;
208 +               curpart++;
209 +       };
210 +
211 +       parts[curpart].name = "nvram";
212 +       parts[curpart].offset = master->size - master->erasesize;
213 +       parts[curpart].size = master->erasesize;
214 +
215 +       /* Global partition "linux" to make easy firmware upgrade */
216 +       curpart++;
217 +       parts[curpart].name = "linux";
218 +       parts[curpart].offset = parts[0].size;
219 +       parts[curpart].size = master->size - parts[0].size - parts[3].size;
220 +
221 +       for (i = 0; i < nrparts; i++)
222 +               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));
223 +
224 +       printk(KERN_INFO PFX "Spare partition is %x offset and length %x\n", spareaddr, sparelen);
225 +       *pparts = parts;
226 +       vfree(buf);
227 +
228 +       return nrparts;
229 +};
230 +
231 +static int bcm963xx_detect_cfe(struct mtd_info *master)
232 +{
233 +       int idoffset = 0x4e0;
234 +       static char idstring[8] = "CFE1CFE1";
235 +       char buf[9];
236 +       int ret;
237 +       size_t retlen;
238 +
239 +       ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
240 +       buf[retlen] = 0;
241 +       printk(KERN_INFO PFX "Read Signature value of %s\n", buf);
242 +
243 +       return strncmp(idstring, buf, 8);
244 +}
245 +
246 +static int bcm963xx_probe(struct platform_device *pdev)
247 +{
248 +       int err = 0;
249 +       int parsed_nr_parts = 0;
250 +       char *part_type;
251 +       struct resource *r;
252 +
253 +       r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
254 +       bcm963xx_map.phys = r->start;
255 +       bcm963xx_map.size = (r->end - r->start) + 1;
256 +       bcm963xx_map.virt = ioremap(r->start, r->end - r->start + 1);
257 +
258 +       if (!bcm963xx_map.virt) {
259 +               printk(KERN_ERR PFX "Failed to ioremap\n");
260 +               return -EIO;
261 +       }
262 +       printk(KERN_INFO PFX "0x%08lx at 0x%08x\n", bcm963xx_map.size, bcm963xx_map.phys);
263 +
264 +       simple_map_init(&bcm963xx_map);
265 +
266 +       bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
267 +       if (!bcm963xx_mtd_info) {
268 +               printk(KERN_ERR PFX "Failed to probe using CFI\n");
269 +               err = -EIO;
270 +               goto err_probe;
271 +       }
272 +
273 +       bcm963xx_mtd_info->owner = THIS_MODULE;
274 +
275 +       /* This is mutually exclusive */
276 +       if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0) {
277 +               printk(KERN_INFO PFX "CFE bootloader detected\n");
278 +               if (parsed_nr_parts == 0) {
279 +                       int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
280 +                       if (ret > 0) {
281 +                               part_type = "CFE";
282 +                               parsed_nr_parts = ret;
283 +                       }
284 +               }
285 +       } else {
286 +               printk(KERN_INFO PFX "assuming RedBoot bootloader\n");
287 +               if (bcm963xx_mtd_info->size > 0x00400000) {
288 +                       printk(KERN_INFO PFX "Support for extended flash memory size : 0x%lx ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
289 +                       bcm963xx_map.virt = (u32)(EXTENDED_SIZE);
290 +               }
291 +
292 +#ifdef CONFIG_MTD_REDBOOT_PARTS
293 +               if (parsed_nr_parts == 0) {
294 +                       int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
295 +                       if (ret > 0) {
296 +                               part_type = "RedBoot";
297 +                               parsed_nr_parts = ret;
298 +                       }
299 +               }
300 +#endif
301 +       }
302 +
303 +       return add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
304 +
305 +err_probe:
306 +       iounmap(bcm963xx_map.virt);
307 +       return err;
308 +}
309 +
310 +static int bcm963xx_remove(struct platform_device *pdev)
311 +{
312 +       if (bcm963xx_mtd_info) {
313 +               del_mtd_partitions(bcm963xx_mtd_info);
314 +               map_destroy(bcm963xx_mtd_info);
315 +       }
316 +
317 +       if (bcm963xx_map.virt) {
318 +               iounmap(bcm963xx_map.virt);
319 +               bcm963xx_map.virt = 0;
320 +       }
321 +
322 +       return 0;
323 +}
324 +
325 +static struct platform_driver bcm63xx_mtd_dev = {
326 +       .probe  = bcm963xx_probe,
327 +       .remove = bcm963xx_remove,
328 +       .driver = {
329 +               .name   = "bcm963xx-flash",
330 +               .owner  = THIS_MODULE,
331 +       },
332 +};
333 +
334 +static int __init bcm963xx_mtd_init(void)
335 +{
336 +       return platform_driver_register(&bcm63xx_mtd_dev);
337 +}
338 +
339 +static void __exit bcm963xx_mtd_exit(void)
340 +{
341 +       platform_driver_unregister(&bcm63xx_mtd_dev);
342 +}
343 +
344 +module_init(bcm963xx_mtd_init);
345 +module_exit(bcm963xx_mtd_exit);
346 +
347 +MODULE_LICENSE("GPL");
348 +MODULE_DESCRIPTION("Broadcom BCM63xx MTD partition parser/mapping for CFE and RedBoot");
349 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
350 +MODULE_AUTHOR("Mike Albon <malbon@openwrt.org>");
351 Index: linux-2.6.32.10/arch/mips/bcm63xx/boards/board_bcm963xx.c
352 ===================================================================
353 --- linux-2.6.32.10.orig/arch/mips/bcm63xx/boards/board_bcm963xx.c      2010-03-29 06:35:59.927292275 -0400
354 +++ linux-2.6.32.10/arch/mips/bcm63xx/boards/board_bcm963xx.c   2010-03-29 06:37:35.178541753 -0400
355 @@ -722,20 +722,6 @@
356         return 0;
357  }
358  
359 -static struct mtd_partition mtd_partitions[] = {
360 -       {
361 -               .name           = "cfe",
362 -               .offset         = 0x0,
363 -               .size           = 0x40000,
364 -       }
365 -};
366 -
367 -static struct physmap_flash_data flash_data = {
368 -       .width                  = 2,
369 -       .nr_parts               = ARRAY_SIZE(mtd_partitions),
370 -       .parts                  = mtd_partitions,
371 -};
372 -
373  static struct resource mtd_resources[] = {
374         {
375                 .start          = 0,    /* filled at runtime */
376 @@ -745,12 +731,9 @@
377  };
378  
379  static struct platform_device mtd_dev = {
380 -       .name                   = "physmap-flash",
381 +       .name                   = "bcm963xx-flash",
382         .resource               = mtd_resources,
383         .num_resources          = ARRAY_SIZE(mtd_resources),
384 -       .dev                    = {
385 -               .platform_data  = &flash_data,
386 -       },
387  };
388  
389  /*