Oops, commited just one part :/
[lede.git] / target / linux / brcm63xx-2.6 / patches / 040-bcm963xx_flashmap.patch
1 diff -urN linux-2.6.19/drivers/mtd/maps/Kconfig linux-2.6.19.new/drivers/mtd/maps/Kconfig
2 --- linux-2.6.19/drivers/mtd/maps/Kconfig       2006-11-29 22:57:37.000000000 +0100
3 +++ linux-2.6.19.new/drivers/mtd/maps/Kconfig   2006-12-18 17:21:07.000000000 +0100
4 @@ -224,6 +224,13 @@
5           Flash memory access on 4G Systems MTX-1 Board. If you have one of
6           these boards and would like to use the flash chips on it, say 'Y'.
7  
8 +config MTD_BCM963XX
9 +        tristate "BCM963xx Flash device"
10 +        depends on MIPS && BCM963XX
11 +        help
12 +         Flash memory access on BCM963xx boards. Currently only works with
13 +         RedBoot, CFE support coming soon.
14 +
15  config MTD_DILNETPC
16         tristate "CFI Flash device mapped on DIL/Net PC"
17         depends on X86 && MTD_CONCAT && MTD_PARTITIONS && MTD_CFI_INTELEXT
18 diff -urN linux-2.6.19/drivers/mtd/maps/Makefile linux-2.6.19.new/drivers/mtd/maps/Makefile
19 --- linux-2.6.19/drivers/mtd/maps/Makefile      2006-11-29 22:57:37.000000000 +0100
20 +++ linux-2.6.19.new/drivers/mtd/maps/Makefile  2006-12-18 17:21:07.000000000 +0100
21 @@ -70,3 +70,4 @@
22  obj-$(CONFIG_MTD_OMAP_NOR)     += omap_nor.o
23  obj-$(CONFIG_MTD_MTX1)         += mtx-1_flash.o
24  obj-$(CONFIG_MTD_TQM834x)      += tqm834x.o
25 +obj-$(CONFIG_MTD_BCM963XX)      += bcm963xx-flash.o
26 diff -urN linux-2.6.19/drivers/mtd/maps/Makefile.orig linux-2.6.19.new/drivers/mtd/maps/Makefile.orig
27 --- linux-2.6.19/drivers/mtd/maps/Makefile.orig 1970-01-01 01:00:00.000000000 +0100
28 +++ linux-2.6.19.new/drivers/mtd/maps/Makefile.orig     2006-11-29 22:57:37.000000000 +0100
29 @@ -0,0 +1,72 @@
30 +#
31 +# linux/drivers/maps/Makefile
32 +#
33 +# $Id: Makefile.common,v 1.34 2005/11/07 11:14:26 gleixner Exp $
34 +
35 +ifeq ($(CONFIG_MTD_COMPLEX_MAPPINGS),y)
36 +obj-$(CONFIG_MTD)              += map_funcs.o
37 +endif
38 +
39 +# Chip mappings
40 +obj-$(CONFIG_MTD_CDB89712)     += cdb89712.o
41 +obj-$(CONFIG_MTD_ARM_INTEGRATOR)+= integrator-flash.o
42 +obj-$(CONFIG_MTD_BAST)         += bast-flash.o
43 +obj-$(CONFIG_MTD_CFI_FLAGADM)  += cfi_flagadm.o
44 +obj-$(CONFIG_MTD_CSTM_MIPS_IXX)        += cstm_mips_ixx.o
45 +obj-$(CONFIG_MTD_DC21285)      += dc21285.o
46 +obj-$(CONFIG_MTD_DILNETPC)     += dilnetpc.o
47 +obj-$(CONFIG_MTD_L440GX)       += l440gx.o
48 +obj-$(CONFIG_MTD_AMD76XROM)    += amd76xrom.o
49 +obj-$(CONFIG_MTD_ICHXROM)      += ichxrom.o
50 +obj-$(CONFIG_MTD_TSUNAMI)      += tsunami_flash.o
51 +obj-$(CONFIG_MTD_LUBBOCK)      += lubbock-flash.o
52 +obj-$(CONFIG_MTD_MAINSTONE)    += mainstone-flash.o
53 +obj-$(CONFIG_MTD_MBX860)       += mbx860.o
54 +obj-$(CONFIG_MTD_CEIVA)                += ceiva.o
55 +obj-$(CONFIG_MTD_OCTAGON)      += octagon-5066.o
56 +obj-$(CONFIG_MTD_PHYSMAP)      += physmap.o
57 +obj-$(CONFIG_MTD_PNC2000)      += pnc2000.o
58 +obj-$(CONFIG_MTD_PCMCIA)       += pcmciamtd.o
59 +obj-$(CONFIG_MTD_RPXLITE)      += rpxlite.o
60 +obj-$(CONFIG_MTD_TQM8XXL)      += tqm8xxl.o
61 +obj-$(CONFIG_MTD_SA1100)       += sa1100-flash.o
62 +obj-$(CONFIG_MTD_IPAQ)         += ipaq-flash.o
63 +obj-$(CONFIG_MTD_SBC_GXX)      += sbc_gxx.o
64 +obj-$(CONFIG_MTD_SC520CDP)     += sc520cdp.o
65 +obj-$(CONFIG_MTD_NETSC520)     += netsc520.o
66 +obj-$(CONFIG_MTD_TS5500)       += ts5500_flash.o
67 +obj-$(CONFIG_MTD_SUN_UFLASH)   += sun_uflash.o
68 +obj-$(CONFIG_MTD_VMAX)         += vmax301.o
69 +obj-$(CONFIG_MTD_SCx200_DOCFLASH)+= scx200_docflash.o
70 +obj-$(CONFIG_MTD_DBOX2)                += dbox2-flash.o
71 +obj-$(CONFIG_MTD_OCELOT)       += ocelot.o
72 +obj-$(CONFIG_MTD_SOLUTIONENGINE)+= solutionengine.o
73 +obj-$(CONFIG_MTD_PCI)          += pci.o
74 +obj-$(CONFIG_MTD_ALCHEMY)       += alchemy-flash.o
75 +obj-$(CONFIG_MTD_LASAT)                += lasat.o
76 +obj-$(CONFIG_MTD_AUTCPU12)     += autcpu12-nvram.o
77 +obj-$(CONFIG_MTD_EDB7312)      += edb7312.o
78 +obj-$(CONFIG_MTD_IMPA7)                += impa7.o
79 +obj-$(CONFIG_MTD_FORTUNET)     += fortunet.o
80 +obj-$(CONFIG_MTD_REDWOOD)      += redwood.o
81 +obj-$(CONFIG_MTD_UCLINUX)      += uclinux.o
82 +obj-$(CONFIG_MTD_NETtel)       += nettel.o
83 +obj-$(CONFIG_MTD_SCB2_FLASH)   += scb2_flash.o
84 +obj-$(CONFIG_MTD_EBONY)                += ebony.o
85 +obj-$(CONFIG_MTD_OCOTEA)       += ocotea.o
86 +obj-$(CONFIG_MTD_BEECH)                += beech-mtd.o
87 +obj-$(CONFIG_MTD_ARCTIC)       += arctic-mtd.o
88 +obj-$(CONFIG_MTD_WALNUT)        += walnut.o
89 +obj-$(CONFIG_MTD_H720X)                += h720x-flash.o
90 +obj-$(CONFIG_MTD_SBC8240)      += sbc8240.o
91 +obj-$(CONFIG_MTD_NOR_TOTO)     += omap-toto-flash.o
92 +obj-$(CONFIG_MTD_MPC1211)      += mpc1211.o
93 +obj-$(CONFIG_MTD_IXP4XX)       += ixp4xx.o
94 +obj-$(CONFIG_MTD_IXP2000)      += ixp2000.o
95 +obj-$(CONFIG_MTD_WRSBC8260)    += wr_sbc82xx_flash.o
96 +obj-$(CONFIG_MTD_DMV182)       += dmv182.o
97 +obj-$(CONFIG_MTD_SHARP_SL)     += sharpsl-flash.o
98 +obj-$(CONFIG_MTD_PLATRAM)      += plat-ram.o
99 +obj-$(CONFIG_MTD_OMAP_NOR)     += omap_nor.o
100 +obj-$(CONFIG_MTD_MTX1)         += mtx-1_flash.o
101 +obj-$(CONFIG_MTD_TQM834x)      += tqm834x.o
102 diff -urN linux-2.6.19/drivers/mtd/maps/bcm963xx-flash.c linux-2.6.19.new/drivers/mtd/maps/bcm963xx-flash.c
103 --- linux-2.6.19/drivers/mtd/maps/bcm963xx-flash.c      1970-01-01 01:00:00.000000000 +0100
104 +++ linux-2.6.19.new/drivers/mtd/maps/bcm963xx-flash.c  2006-12-18 17:21:07.000000000 +0100
105 @@ -0,0 +1,276 @@
106 +/*
107 + * $Id$
108 + * Copyright (C) 2006  Florian Fainelli
109 + * Copyright (C) $Date$  $Author$
110 + *
111 + * This program is free software; you can redistribute it and/or modify
112 + * it under the terms of the GNU General Public License as published by
113 + * the Free Software Foundation; either version 2 of the License, or
114 + * (at your option) any later version.
115 + *
116 + * This program is distributed in the hope that it will be useful,
117 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
118 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
119 + * GNU General Public License for more details.
120 + *
121 + * You should have received a copy of the GNU General Public License
122 + * along with this program; if not, write to the Free Software
123 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
124 + */
125 +
126 +/* This is the BCM963xx flash map driver, in its actual state it only supports BCM96348 devices
127 + * this driver is able to manage both bootloader we found on these boards : CFE and RedBoot
128 + *
129 + * RedBoot :
130 + *  - this bootloader allows us to parse partitions and therefore deduce the MTD partition table
131 + *
132 + * CFE :
133 + *   - we have to use a "physically mapped flash" defined bellow
134 + *
135 + */
136 +
137 +#include <asm/io.h>
138 +#include <linux/init.h>
139 +#include <linux/kernel.h>
140 +#include <linux/mtd/map.h>
141 +#include <linux/mtd/mtd.h>
142 +#include <linux/mtd/partitions.h>
143 +#include <linux/vmalloc.h>
144 +#include <board.h>
145 +
146 +#define WINDOW_ADDR 0x1FC00000                 /* Real address of the flash */
147 +#define WINDOW_SIZE 0x400000           /* Size of flash */
148 +#define BUSWIDTH 2                     /* Buswidth */
149 +#define EXTENDED_SIZE 0xBFC00000       /* Extended flash address */
150 +#define IMAGE_LEN 10                   /* Length of Length Field */
151 +#define ADDRESS_LEN 12                 /* Length of Address field */
152 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
153 +
154 +extern int boot_loader_type;           /* For RedBoot / CFE detection */
155 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
156 +static struct mtd_partition *parsed_parts;
157 +
158 +static void __exit bcm963xx_mtd_cleanup(void);
159 +
160 +static struct mtd_info *bcm963xx_mtd_info;
161 +
162 +static struct map_info bcm963xx_map = {
163 +       .name = "bcm963xx",
164 +       .size = WINDOW_SIZE,
165 +       .bankwidth = BUSWIDTH,
166 +       .phys = WINDOW_ADDR,
167 +};
168 +
169 +
170 +int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
171 +{
172 +       int nrparts = 2, curpart = 0; // CFE and NVRAM always present.
173 +       struct bcm963xx_cfe_map {
174 +       unsigned char tagVersion[4];                            // Version of the image tag
175 +       unsigned char sig_1[20];                                // Company Line 1
176 +       unsigned char sig_2[14];                                // Company Line 2
177 +       unsigned char chipid[6];                                        // Chip this image is for
178 +       unsigned char boardid[16];                              // Board name
179 +       unsigned char bigEndian[2];                             // Map endianness -- 1 BE 0 LE
180 +       unsigned char totalLength[IMAGE_LEN];           //Total length of image
181 +       unsigned char cfeAddress[ADDRESS_LEN];  // Address in memory of CFE
182 +       unsigned char cfeLength[IMAGE_LEN];             // Size of CFE
183 +       unsigned char rootAddress[ADDRESS_LEN];         // Address in memory of rootfs
184 +       unsigned char rootLength[IMAGE_LEN];            // Size of rootfs
185 +       unsigned char kernelAddress[ADDRESS_LEN];       // Address in memory of kernel
186 +       unsigned char kernelLength[IMAGE_LEN];  // Size of kernel
187 +       unsigned char dualImage[2];                             // Unused at present
188 +       unsigned char inactiveFlag[2];                  // Unused at present
189 +       unsigned char reserved1[74];                            // Reserved area not in use
190 +       unsigned char imageCRC[4];                              // CRC32 of images
191 +       unsigned char reserved2[16];                            // Unused at present
192 +       unsigned char headerCRC[4];                             // CRC32 of header excluding tagVersion
193 +       unsigned char reserved3[16];                            // Unused at present
194 +       } *buf;
195 +       struct mtd_partition *parts;
196 +       int ret;
197 +       size_t retlen;
198 +       unsigned int rootfsaddr, kerneladdr, spareaddr;
199 +       unsigned int rootfslen, kernellen, sparelen, totallen;
200 +       int namelen = 0;
201 +       int i;
202 +       // Allocate memory for buffer
203 +       buf = vmalloc(sizeof(struct bcm963xx_cfe_map));
204 +
205 +       if (!buf)
206 +               return -ENOMEM;
207 +
208 +       // Get the tag
209 +       ret = master->read(master,master->erasesize,sizeof(struct bcm963xx_cfe_map), &retlen, (void *)buf);
210 +       if (retlen != sizeof(struct bcm963xx_cfe_map)){
211 +               vfree(buf);
212 +               return -EIO;
213 +       };
214 +       printk("bcm963xx: CFE boot tag found with version %s and board type %s.\n",buf->tagVersion,buf->boardid);
215 +       // Get the values and calculate
216 +       sscanf(buf->rootAddress,"%u", &rootfsaddr);
217 +       rootfsaddr = rootfsaddr - EXTENDED_SIZE;
218 +       sscanf(buf->rootLength, "%u", &rootfslen);
219 +       sscanf(buf->kernelAddress, "%u", &kerneladdr);
220 +       kerneladdr = kerneladdr - EXTENDED_SIZE;
221 +       sscanf(buf->kernelLength, "%u", &kernellen);
222 +       sscanf(buf->totalLength, "%u", &totallen);
223 +       spareaddr = ROUNDUP(totallen,master->erasesize) + master->erasesize;
224 +       sparelen = master->size - spareaddr - master->erasesize;
225 +       // Determine number of partitions
226 +       namelen = 8;
227 +       if (rootfslen > 0){
228 +               nrparts++;
229 +               namelen =+ 6;
230 +       };
231 +       if (kernellen > 0){
232 +               nrparts++;
233 +               namelen =+ 6;
234 +       };
235 +       if (sparelen > 0){
236 +               nrparts++;
237 +               namelen =+ 6;
238 +       };
239 +       // Ask kernel for more memory.
240 +       parts = kmalloc(sizeof(*parts)*nrparts+10*nrparts, GFP_KERNEL);
241 +       if (!parts){
242 +               vfree(buf);
243 +               return -ENOMEM;
244 +       };
245 +       memset(parts,0,sizeof(*parts)*nrparts+10*nrparts);
246 +       // Start building partition list
247 +       parts[curpart].name = "CFE";
248 +       parts[curpart].offset = 0;
249 +       parts[curpart].size = master->erasesize;
250 +       curpart++;
251 +       if (kernellen > 0){
252 +               parts[curpart].name = "Kernel";
253 +               parts[curpart].offset = kerneladdr;
254 +               parts[curpart].size = kernellen;
255 +               curpart++;
256 +       };
257 +       if (rootfslen > 0){
258 +               parts[curpart].name = "Rootfs";
259 +               parts[curpart].offset = rootfsaddr;
260 +               parts[curpart].size = rootfslen;
261 +               curpart++;
262 +       };
263 +       if (sparelen > 0){
264 +               parts[curpart].name = "OpenWrt";
265 +               parts[curpart].offset = spareaddr;
266 +               parts[curpart].size = sparelen;
267 +               curpart++;
268 +       };
269 +       parts[curpart].name = "NVRAM";
270 +       parts[curpart].offset = master->size - master->erasesize;
271 +       parts[curpart].size = master->erasesize;
272 +       for (i = 0; i < nrparts; i++) {
273 +          printk("bcm963xx: Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
274 +       }
275 +       *pparts = parts;
276 +       vfree(buf);
277 +       return nrparts;
278 +};
279 +
280 +static struct mtd_partition bcm963xx_parts[] = {
281 +        { name: "bootloader",  size: 0,        offset: 0,      mask_flags: MTD_WRITEABLE },
282 +        { name: "rootfs",              size: 0,        offset: 0},
283 +        { name: "jffs2",        size: 5 * 0x10000,      offset: 57*0x10000}
284 +};
285 +
286 +static int bcm963xx_parts_size = sizeof(bcm963xx_parts) / sizeof(bcm963xx_parts[0]);
287 +
288 +static int bcm963xx_detect_cfe(struct mtd_info *master)
289 +{
290 +       int idoffset = 0x4e0;
291 +       static char idstring[8] = "CFE1CFE1";
292 +       char buf[8];
293 +       int ret;
294 +       size_t retlen;
295 +
296 +       ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
297 +       printk("bcm963xx: Read Signature value of %s\n", buf);
298 +       return strcmp(idstring,buf);
299 +}
300 +
301 +static int __init bcm963xx_mtd_init(void)
302 +{
303 +       printk("bcm963xx: 0x%08x at 0x%08x\n", WINDOW_SIZE, WINDOW_ADDR);
304 +       bcm963xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
305 +
306 +       if (!bcm963xx_map.virt) {
307 +               printk("bcm963xx: Failed to ioremap\n");
308 +               return -EIO;
309 +       }
310 +
311 +       simple_map_init(&bcm963xx_map);
312 +
313 +       bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
314 +
315 +       if (bcm963xx_mtd_info) {
316 +               bcm963xx_mtd_info->owner = THIS_MODULE;
317 +
318 +               //if (boot_loader_type == BOOT_CFE)
319 +               if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0)
320 +               {
321 +                       int parsed_nr_parts = 0;
322 +                       char * part_type;
323 +                       printk("bcm963xx: CFE bootloader detected\n");
324 +                       //add_mtd_device(bcm963xx_mtd_info);
325 +                       //add_mtd_partitions(bcm963xx_mtd_info, bcm963xx_parts, bcm963xx_parts_size);
326 +                       if (parsed_nr_parts == 0) {
327 +                               int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
328 +                               if (ret > 0) {
329 +                                       part_type = "CFE";
330 +                                       parsed_nr_parts = ret;
331 +                               }
332 +                       }
333 +                       add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
334 +                       return 0;
335 +               }
336 +               else
337 +               {
338 +                       int parsed_nr_parts = 0;
339 +                       char * part_type;
340 +
341 +                       if (bcm963xx_mtd_info->size > 0x00400000) {
342 +                               printk("Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
343 +                               bcm963xx_map.virt = (unsigned long)(EXTENDED_SIZE);
344 +                       }
345 +
346 +#ifdef CONFIG_MTD_REDBOOT_PARTS
347 +                       if (parsed_nr_parts == 0) {
348 +                               int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
349 +                               if (ret > 0) {
350 +                                       part_type = "RedBoot";
351 +                                       parsed_nr_parts = ret;
352 +                               }
353 +                       }
354 +#endif
355 +                       add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
356 +
357 +                       return 0;
358 +               }
359 +       }
360 +       iounmap(bcm963xx_map.virt);
361 +       return -ENXIO;
362 +}
363 +
364 +static void __exit bcm963xx_mtd_cleanup(void)
365 +{
366 +       if (bcm963xx_mtd_info) {
367 +               del_mtd_partitions(bcm963xx_mtd_info);
368 +               map_destroy(bcm963xx_mtd_info);
369 +       }
370 +
371 +       if (bcm963xx_map.virt) {
372 +               iounmap(bcm963xx_map.virt);
373 +               bcm963xx_map.virt = 0;
374 +       }
375 +}
376 +
377 +module_init(bcm963xx_mtd_init);
378 +module_exit(bcm963xx_mtd_cleanup);
379 +
380 +MODULE_LICENSE("GPL");
381 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
382 diff -urN linux-2.6.19/drivers/mtd/redboot.c linux-2.6.19.new/drivers/mtd/redboot.c
383 --- linux-2.6.19/drivers/mtd/redboot.c  2006-12-18 17:09:14.000000000 +0100
384 +++ linux-2.6.19.new/drivers/mtd/redboot.c      2006-12-18 17:14:26.000000000 +0100
385 @@ -39,7 +39,7 @@
386         return 1;
387  }
388  
389 -static int parse_redboot_partitions(struct mtd_info *master,
390 +int parse_redboot_partitions(struct mtd_info *master,
391                               struct mtd_partition **pparts,
392                               unsigned long fis_origin)
393  {
394 @@ -132,6 +132,14 @@
395                 goto out;
396         }
397  
398 +       if (!fis_origin) {
399 +               for (i = 0; i < numslots; i++) {
400 +                       if (!strncmp(buf[i].name, "RedBoot", 8)) {
401 +                               fis_origin = (buf[i].flash_base & (master->size << 1) - 1);
402 +                       }
403 +               }
404 +       }
405 +
406         for (i = 0; i < numslots; i++) {
407                 struct fis_list *new_fl, **prev;
408  
409 @@ -154,9 +162,8 @@
410                 new_fl->img = &buf[i];
411                  if (fis_origin) {
412                          buf[i].flash_base -= fis_origin;
413 -                } else {
414 -                        buf[i].flash_base &= master->size-1;
415                  }
416 +               buf[i].flash_base &= (master->size << 1) - 1;
417  
418                 /* I'm sure the JFFS2 code has done me permanent damage.
419                  * I now think the following is _normal_