Normalize patch : change MIPS_BRCM to BCM963XX
[openwrt.git] / target / linux / brcm63xx-2.6 / patches / 040-bcm963xx_flashmap.patch
1 diff -urN linux-2.6.17/drivers/mtd/maps/bcm963xx-flash.c linux-2.6.17-brcm63xx/drivers/mtd/maps/bcm963xx-flash.c
2 --- linux-2.6.17/drivers/mtd/maps/bcm963xx-flash.c      1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.17-brcm63xx/drivers/mtd/maps/bcm963xx-flash.c      2006-11-06 22:43:59.000000000 +0000
4 @@ -0,0 +1,276 @@
5 +/*
6 + * $Id$
7 + * Copyright (C) 2006  Florian Fainelli
8 + * Copyright (C) $Date$  $Author$
9 + *
10 + * This program is free software; you can redistribute it and/or modify
11 + * it under the terms of the GNU General Public License as published by
12 + * the Free Software Foundation; either version 2 of the License, or
13 + * (at your option) any later version.
14 + *
15 + * This program is distributed in the hope that it will be useful,
16 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 + * GNU General Public License for more details.
19 + *
20 + * You should have received a copy of the GNU General Public License
21 + * along with this program; if not, write to the Free Software
22 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
23 + */
24 +
25 +/* This is the BCM963xx flash map driver, in its actual state it only supports BCM96348 devices
26 + * this driver is able to manage both bootloader we found on these boards : CFE and RedBoot
27 + *
28 + * RedBoot :
29 + *  - this bootloader allows us to parse partitions and therefore deduce the MTD partition table
30 + *
31 + * CFE :
32 + *   - we have to use a "physically mapped flash" defined bellow
33 + *
34 + */
35 +
36 +#include <asm/io.h>
37 +#include <linux/init.h>
38 +#include <linux/kernel.h>
39 +#include <linux/mtd/map.h>
40 +#include <linux/mtd/mtd.h>
41 +#include <linux/mtd/partitions.h>
42 +#include <linux/vmalloc.h>
43 +#include <board.h>
44 +
45 +#define WINDOW_ADDR 0x1FC00000                 /* Real address of the flash */
46 +#define WINDOW_SIZE 0x400000           /* Size of flash */
47 +#define BUSWIDTH 2                     /* Buswidth */
48 +#define EXTENDED_SIZE 0xBFC00000       /* Extended flash address */
49 +#define IMAGE_LEN 10                   /* Length of Length Field */
50 +#define ADDRESS_LEN 12                 /* Length of Address field */
51 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
52 +
53 +extern int boot_loader_type;           /* For RedBoot / CFE detection */
54 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
55 +static struct mtd_partition *parsed_parts;
56 +
57 +static void __exit bcm963xx_mtd_cleanup(void);
58 +
59 +static struct mtd_info *bcm963xx_mtd_info;
60 +
61 +static struct map_info bcm963xx_map = {
62 +       .name = "bcm963xx",
63 +       .size = WINDOW_SIZE,
64 +       .bankwidth = BUSWIDTH,
65 +       .phys = WINDOW_ADDR,
66 +};
67 +
68 +
69 +int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
70 +{
71 +       int nrparts = 2, curpart = 0; // CFE and NVRAM always present.
72 +       struct bcm963xx_cfe_map {
73 +       unsigned char tagVersion[4];                            // Version of the image tag
74 +       unsigned char sig_1[20];                                // Company Line 1
75 +       unsigned char sig_2[14];                                // Company Line 2
76 +       unsigned char chipid[6];                                        // Chip this image is for
77 +       unsigned char boardid[16];                              // Board name
78 +       unsigned char bigEndian[2];                             // Map endianness -- 1 BE 0 LE
79 +       unsigned char totalLength[IMAGE_LEN];           //Total length of image
80 +       unsigned char cfeAddress[ADDRESS_LEN];  // Address in memory of CFE
81 +       unsigned char cfeLength[IMAGE_LEN];             // Size of CFE
82 +       unsigned char rootAddress[ADDRESS_LEN];         // Address in memory of rootfs
83 +       unsigned char rootLength[IMAGE_LEN];            // Size of rootfs
84 +       unsigned char kernelAddress[ADDRESS_LEN];       // Address in memory of kernel
85 +       unsigned char kernelLength[IMAGE_LEN];  // Size of kernel
86 +       unsigned char dualImage[2];                             // Unused at present
87 +       unsigned char inactiveFlag[2];                  // Unused at present
88 +       unsigned char reserved1[74];                            // Reserved area not in use
89 +       unsigned char imageCRC[4];                              // CRC32 of images
90 +       unsigned char reserved2[16];                            // Unused at present
91 +       unsigned char headerCRC[4];                             // CRC32 of header excluding tagVersion
92 +       unsigned char reserved3[16];                            // Unused at present
93 +       } *buf;
94 +       struct mtd_partition *parts;
95 +       int ret;
96 +       size_t retlen;
97 +       unsigned int rootfsaddr, kerneladdr, spareaddr;
98 +       unsigned int rootfslen, kernellen, sparelen, totallen;
99 +       int namelen = 0;
100 +       int i;
101 +       // Allocate memory for buffer
102 +       buf = vmalloc(sizeof(struct bcm963xx_cfe_map));
103 +
104 +       if (!buf)
105 +               return -ENOMEM;
106 +
107 +       // Get the tag
108 +       ret = master->read(master,master->erasesize,sizeof(struct bcm963xx_cfe_map), &retlen, (void *)buf);
109 +       if (retlen != sizeof(struct bcm963xx_cfe_map)){
110 +               vfree(buf);
111 +               return -EIO;
112 +       };
113 +       printk("bcm963xx: CFE boot tag found with version %s and board type %s.\n",buf->tagVersion,buf->boardid);
114 +       // Get the values and calculate
115 +       sscanf(buf->rootAddress,"%u", &rootfsaddr);
116 +       rootfsaddr = rootfsaddr - EXTENDED_SIZE;
117 +       sscanf(buf->rootLength, "%u", &rootfslen);
118 +       sscanf(buf->kernelAddress, "%u", &kerneladdr);
119 +       kerneladdr = kerneladdr - EXTENDED_SIZE;
120 +       sscanf(buf->kernelLength, "%u", &kernellen);
121 +       sscanf(buf->totalLength, "%u", &totallen);
122 +       spareaddr = ROUNDUP(totallen,master->erasesize) + master->erasesize;
123 +       sparelen = master->size - spareaddr - master->erasesize;
124 +       // Determine number of partitions
125 +       namelen = 8;
126 +       if (rootfslen > 0){
127 +               nrparts++;
128 +               namelen =+ 6;
129 +       };
130 +       if (kernellen > 0){
131 +               nrparts++;
132 +               namelen =+ 6;
133 +       };
134 +       if (sparelen > 0){
135 +               nrparts++;
136 +               namelen =+ 6;
137 +       };
138 +       // Ask kernel for more memory.
139 +       parts = kmalloc(sizeof(*parts)*nrparts+10*nrparts, GFP_KERNEL);
140 +       if (!parts){
141 +               vfree(buf);
142 +               return -ENOMEM;
143 +       };
144 +       memset(parts,0,sizeof(*parts)*nrparts+10*nrparts);
145 +       // Start building partition list
146 +       parts[curpart].name = "CFE";
147 +       parts[curpart].offset = 0;
148 +       parts[curpart].size = master->erasesize;
149 +       curpart++;
150 +       if (kernellen > 0){
151 +               parts[curpart].name = "Kernel";
152 +               parts[curpart].offset = kerneladdr;
153 +               parts[curpart].size = kernellen;
154 +               curpart++;
155 +       };
156 +       if (rootfslen > 0){
157 +               parts[curpart].name = "Rootfs";
158 +               parts[curpart].offset = rootfsaddr;
159 +               parts[curpart].size = rootfslen;
160 +               curpart++;
161 +       };
162 +       if (sparelen > 0){
163 +               parts[curpart].name = "OpenWrt";
164 +               parts[curpart].offset = spareaddr;
165 +               parts[curpart].size = sparelen;
166 +               curpart++;
167 +       };
168 +       parts[curpart].name = "NVRAM";
169 +       parts[curpart].offset = master->size - master->erasesize;
170 +       parts[curpart].size = master->erasesize;
171 +       for (i = 0; i < nrparts; i++) {
172 +          printk("bcm963xx: Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
173 +       }
174 +       *pparts = parts;
175 +       vfree(buf);
176 +       return nrparts;
177 +};
178 +
179 +static struct mtd_partition bcm963xx_parts[] = {
180 +        { name: "bootloader",  size: 0,        offset: 0,      mask_flags: MTD_WRITEABLE },
181 +        { name: "rootfs",              size: 0,        offset: 0},
182 +        { name: "jffs2",        size: 5 * 0x10000,      offset: 57*0x10000}
183 +};
184 +
185 +static int bcm963xx_parts_size = sizeof(bcm963xx_parts) / sizeof(bcm963xx_parts[0]);
186 +
187 +static int bcm963xx_detect_cfe(struct mtd_info *master)
188 +{
189 +       int idoffset = 0x4e0;
190 +       static char idstring[8] = "CFE1CFE1";
191 +       char buf[8];
192 +       int ret;
193 +       size_t retlen;
194 +
195 +       ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
196 +       printk("bcm963xx: Read Signature value of %s\n", buf);
197 +       return strcmp(idstring,buf);
198 +}
199 +
200 +static int __init bcm963xx_mtd_init(void)
201 +{
202 +       printk("bcm963xx: 0x%08x at 0x%08x\n", WINDOW_SIZE, WINDOW_ADDR);
203 +       bcm963xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
204 +
205 +       if (!bcm963xx_map.virt) {
206 +               printk("bcm963xx: Failed to ioremap\n");
207 +               return -EIO;
208 +       }
209 +
210 +       simple_map_init(&bcm963xx_map);
211 +
212 +       bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
213 +
214 +       if (bcm963xx_mtd_info) {
215 +               bcm963xx_mtd_info->owner = THIS_MODULE;
216 +
217 +               //if (boot_loader_type == BOOT_CFE)
218 +               if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0)
219 +               {
220 +                       int parsed_nr_parts = 0;
221 +                       char * part_type;
222 +                       printk("bcm963xx: CFE bootloader detected\n");
223 +                       //add_mtd_device(bcm963xx_mtd_info);
224 +                       //add_mtd_partitions(bcm963xx_mtd_info, bcm963xx_parts, bcm963xx_parts_size);
225 +                       if (parsed_nr_parts == 0) {
226 +                               int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
227 +                               if (ret > 0) {
228 +                                       part_type = "CFE";
229 +                                       parsed_nr_parts = ret;
230 +                               }
231 +                       }
232 +                       add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
233 +                       return 0;
234 +               }
235 +               else
236 +               {
237 +                       int parsed_nr_parts = 0;
238 +                       char * part_type;
239 +
240 +                       if (bcm963xx_mtd_info->size > 0x00400000) {
241 +                               printk("Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
242 +                               bcm963xx_map.virt = (unsigned long)(EXTENDED_SIZE);
243 +                       }
244 +
245 +#ifdef CONFIG_MTD_REDBOOT_PARTS
246 +                       if (parsed_nr_parts == 0) {
247 +                               int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
248 +                               if (ret > 0) {
249 +                                       part_type = "RedBoot";
250 +                                       parsed_nr_parts = ret;
251 +                               }
252 +                       }
253 +#endif
254 +                       add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
255 +
256 +                       return 0;
257 +               }
258 +       }
259 +       iounmap(bcm963xx_map.virt);
260 +       return -ENXIO;
261 +}
262 +
263 +static void __exit bcm963xx_mtd_cleanup(void)
264 +{
265 +       if (bcm963xx_mtd_info) {
266 +               del_mtd_partitions(bcm963xx_mtd_info);
267 +               map_destroy(bcm963xx_mtd_info);
268 +       }
269 +
270 +       if (bcm963xx_map.virt) {
271 +               iounmap(bcm963xx_map.virt);
272 +               bcm963xx_map.virt = 0;
273 +       }
274 +}
275 +
276 +module_init(bcm963xx_mtd_init);
277 +module_exit(bcm963xx_mtd_cleanup);
278 +
279 +MODULE_LICENSE("GPL");
280 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
281 diff -urN linux-2.6.17/drivers/mtd/maps/Kconfig linux-2.6.17-brcm63xx/drivers/mtd/maps/Kconfig
282 --- linux-2.6.17/drivers/mtd/maps/Kconfig       2006-06-18 03:49:35.000000000 +0200
283 +++ linux-2.6.17-brcm63xx/drivers/mtd/maps/Kconfig      2006-08-30 13:03:06.000000000 +0200
284 @@ -224,6 +224,13 @@
285           Flash memory access on 4G Systems MTX-1 Board. If you have one of
286           these boards and would like to use the flash chips on it, say 'Y'.
287  
288 +config MTD_BCM963XX
289 +        tristate "BCM963xx Flash device"
290 +        depends on MIPS && BCM963XX
291 +        help
292 +         Flash memory access on BCM963xx boards. Currently only works with
293 +         RedBoot, CFE support coming soon.
294 +
295  config MTD_DILNETPC
296         tristate "CFI Flash device mapped on DIL/Net PC"
297         depends on X86 && MTD_CONCAT && MTD_PARTITIONS && MTD_CFI_INTELEXT
298 diff -urN linux-2.6.17/drivers/mtd/maps/Makefile linux-2.6.17-brcm63xx/drivers/mtd/maps/Makefile
299 --- linux-2.6.17/drivers/mtd/maps/Makefile      2006-06-18 03:49:35.000000000 +0200
300 +++ linux-2.6.17-brcm63xx/drivers/mtd/maps/Makefile     2006-08-30 13:03:06.000000000 +0200
301 @@ -71,3 +71,4 @@
302  obj-$(CONFIG_MTD_OMAP_NOR)     += omap_nor.o
303  obj-$(CONFIG_MTD_MTX1)         += mtx-1_flash.o
304  obj-$(CONFIG_MTD_TQM834x)      += tqm834x.o
305 +obj-$(CONFIG_MTD_BCM963XX)      += bcm963xx-flash.o
306 diff -urN linux-2.6.17/drivers/mtd/redboot.c linux-2.6.17-brcm63xx/drivers/mtd/redboot.c
307 --- linux-2.6.17/drivers/mtd/redboot.c  2006-06-18 03:49:35.000000000 +0200
308 +++ linux-2.6.17-brcm63xx/drivers/mtd/redboot.c 2006-08-30 13:03:06.000000000 +0200
309 @@ -39,7 +39,7 @@
310         return 1;
311  }
312  
313 -static int parse_redboot_partitions(struct mtd_info *master,
314 +int parse_redboot_partitions(struct mtd_info *master,
315                               struct mtd_partition **pparts,
316                               unsigned long fis_origin)
317  {
318 @@ -120,11 +120,19 @@
319                 goto out;
320         }
321  
322 +       if (!fis_origin) {
323 +               for (i = 0; i < numslots; i++) {
324 +                       if (!strncmp(buf[i].name, "RedBoot", 8)) {
325 +                               fis_origin = (buf[i].flash_base & (master->size << 1) - 1);
326 +                       }
327 +               }
328 +       }
329 +
330         for (i = 0; i < numslots; i++) {
331                 struct fis_list *new_fl, **prev;
332  
333                 if (buf[i].name[0] == 0xff)
334 -                       continue;
335 +                       break;
336                 if (!redboot_checksum(&buf[i]))
337                         break;
338  
339 @@ -135,11 +143,10 @@
340                         goto out;
341                 }
342                 new_fl->img = &buf[i];
343 -                if (fis_origin) {
344 -                        buf[i].flash_base -= fis_origin;
345 -                } else {
346 -                        buf[i].flash_base &= master->size-1;
347 -                }
348 +               if (fis_origin) {
349 +                               buf[i].flash_base -= fis_origin;
350 +               }
351 +               buf[i].flash_base &= (master->size << 1) - 1;
352  
353                 /* I'm sure the JFFS2 code has done me permanent damage.
354                  * I now think the following is _normal_