add new flash map driver, some minor system code cleanup
[oweals/openwrt.git] / openwrt / target / linux / linux-2.6 / patches / brcm / 002-flash-map.patch
1 diff -Nur linux-2.6.12.5/drivers/mtd/maps/bcm47xx-flash.c linux-2.6.12.5-flash/drivers/mtd/maps/bcm47xx-flash.c
2 --- linux-2.6.12.5/drivers/mtd/maps/bcm47xx-flash.c     1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.12.5-flash/drivers/mtd/maps/bcm47xx-flash.c       2005-11-06 20:36:42.553198500 +0100
4 @@ -0,0 +1,329 @@
5 +/*
6 + *  Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
7 + *  Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
8 + *
9 + *  original functions for finding root filesystem from Mike Baker 
10 + *
11 + *  This program is free software; you can redistribute  it and/or modify it
12 + *  under  the terms of  the GNU General  Public License as published by the
13 + *  Free Software Foundation;  either version 2 of the  License, or (at your
14 + *  option) any later version.
15 + *
16 + *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
17 + *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
18 + *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
19 + *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
20 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 + *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
22 + *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 + *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
24 + *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 + *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 + *
27 + *  You should have received a copy of the  GNU General Public License along
28 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
29 + *  675 Mass Ave, Cambridge, MA 02139, USA.
30 + * 
31 + *  Copyright 2001-2003, Broadcom Corporation
32 + *  All Rights Reserved.
33 + * 
34 + *  THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
35 + *  KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
36 + *  SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
37 + *  FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
38 + *
39 + *  $Id: bcm47xx-flash.c,v 1.1 2004/10/21 07:18:31 jolt Exp $
40 + *
41 + *  Flash mapping for BCM947XX boards
42 + */
43 +
44 +#include <linux/init.h>
45 +#include <linux/module.h>
46 +#include <linux/types.h>
47 +#include <linux/kernel.h>
48 +#include <asm/io.h>
49 +#include <linux/mtd/mtd.h>
50 +#include <linux/mtd/map.h>
51 +#include <linux/mtd/partitions.h>
52 +#include <linux/config.h>
53 +#include <typedefs.h>
54 +#include <bcmutils.h>
55 +#include <bcmnvram.h>
56 +#include <trxhdr.h>
57 +
58 +
59 +#ifdef CONFIG_MTD_PARTITIONS
60 +extern struct mtd_partition * init_mtd_partitions(struct mtd_info *mtd, size_t size);
61 +#endif
62 +
63 +#define WINDOW_ADDR 0x1c000000
64 +#define WINDOW_SIZE (0x400000*2)
65 +#define BUSWIDTH 2
66 +
67 +static struct mtd_info *bcm947xx_mtd;
68 +
69 +static void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
70 +{
71 +#define MIPS_MEMCPY_ALIGN 4
72 +       map_word ret;
73 +       ssize_t transfer;
74 +       ssize_t done = 0;
75 +       if ((len >= MIPS_MEMCPY_ALIGN) && (!(from & (MIPS_MEMCPY_ALIGN - 1))) && (!(((unsigned int)to & (MIPS_MEMCPY_ALIGN - 1))))) {
76 +               done = len & ~(MIPS_MEMCPY_ALIGN - 1);
77 +               memcpy_fromio(to, map->virt + from, done);
78 +       }
79 +       while (done < len) {
80 +               ret = map->read(map, from + done);
81 +               transfer = len - done;
82 +               if (transfer > map->bankwidth)
83 +                       transfer = map->bankwidth;
84 +               memcpy((void *)((unsigned long)to + done), &ret.x[0], transfer);
85 +               done += transfer;
86 +       }
87 +}
88 +
89 +static struct map_info bcm947xx_map = {
90 +       name: "Physically mapped flash",
91 +       size: WINDOW_SIZE,
92 +       bankwidth: BUSWIDTH,
93 +       phys: WINDOW_ADDR,
94 +};
95 +
96 +#ifdef CONFIG_MTD_PARTITIONS
97 +
98 +static struct mtd_partition bcm947xx_parts[] = {
99 +       { name: "cfe",  offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
100 +       { name: "linux", offset: 0, size: 0, },
101 +       { name: "rootfs", offset: 0, size: 0, },
102 +       { name: "nvram", offset: 0, size: 0, },
103 +       { name: "OpenWrt", offset: 0, size: 0, },
104 +       { name: NULL, },
105 +};
106 +
107 +static int __init
108 +find_cfe_size(struct mtd_info *mtd, size_t size)
109 +{
110 +       struct trx_header *trx;
111 +       unsigned char buf[512];
112 +       int off;
113 +       size_t len;
114 +       int cfe_size_flag;
115 +
116 +       trx = (struct trx_header *) buf;
117 +
118 +       cfe_size_flag=0;
119 +
120 +       for (off = (256*1024); off < size; off += mtd->erasesize) {
121 +               memset(buf, 0xe5, sizeof(buf));
122 +
123 +               /*
124 +                * Read into buffer 
125 +                */
126 +               if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
127 +                   len != sizeof(buf))
128 +                       continue;
129 +
130 +               /* found a TRX header */
131 +               if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
132 +                       goto done;
133 +               }
134 +               cfe_size_flag += 1;
135 +       }
136 +
137 +       printk(KERN_NOTICE
138 +              "%s: Couldn't find bootloader size\n",
139 +              mtd->name);
140 +       return -1;
141 +
142 + done:
143 +       printk(KERN_NOTICE "bootloader size flag: %d\n", cfe_size_flag);
144 +       return cfe_size_flag;
145 +
146 +}
147 +
148 +static int __init
149 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
150 +{
151 +       struct trx_header *trx;
152 +       unsigned char buf[512];
153 +       int off;
154 +       size_t len;
155 +
156 +       trx = (struct trx_header *) buf;
157 +
158 +       for (off = (256*1024); off < size; off += mtd->erasesize) {
159 +               memset(buf, 0xe5, sizeof(buf));
160 +
161 +               /*
162 +                * Read into buffer 
163 +                */
164 +               if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
165 +                   len != sizeof(buf))
166 +                       continue;
167 +
168 +               /* found a TRX header */
169 +               if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
170 +                       part->offset = le32_to_cpu(trx->offsets[2]) ? : 
171 +                               le32_to_cpu(trx->offsets[1]);
172 +                       part->size = le32_to_cpu(trx->len); 
173 +
174 +                       part->size -= part->offset;
175 +                       part->offset += off;
176 +
177 +                       goto done;
178 +               }
179 +       }
180 +
181 +       printk(KERN_NOTICE
182 +              "%s: Couldn't find root filesystem\n",
183 +              mtd->name);
184 +       return -1;
185 +
186 + done:
187 +       return part->size;
188 +}
189 +
190 +struct mtd_partition * __init
191 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
192 +{
193 +
194 +       int cfe_size_flag;
195 +
196 +       cfe_size_flag = find_cfe_size(mtd,size); 
197 +
198 +       /* if cfe_size_flag=0, cfe size is 256 kb, else 384 kb */
199 +
200 +       /* boot loader */
201 +       bcm947xx_parts[0].offset = 0;
202 +       if (cfe_size_flag == 0) {
203 +               bcm947xx_parts[0].size   = 1024*256;
204 +       } else {
205 +               /* netgear wgt634u has 384 kb bootloader */
206 +               bcm947xx_parts[0].size   = 1024*384;
207 +       }
208 +
209 +       /* nvram */
210 +       if (cfe_size_flag == 0) {
211 +               bcm947xx_parts[3].offset = size - mtd->erasesize;
212 +               bcm947xx_parts[3].size   = mtd->erasesize;
213 +       } else {
214 +               /* nvram (old 128kb config partition on netgear wgt634u) */
215 +               bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
216 +               bcm947xx_parts[3].size   = 1024*128;
217 +       }
218 +
219 +       /* linux (kernel and rootfs) */
220 +       if (cfe_size_flag == 0) {
221 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
222 +               bcm947xx_parts[1].size   = bcm947xx_parts[3].offset - 
223 +                       bcm947xx_parts[1].offset;
224 +       } else {
225 +               /* do not count the elf loader, which is on one sector */
226 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size + 
227 +                       bcm947xx_parts[3].size + mtd->erasesize;
228 +               bcm947xx_parts[1].size   = size - 
229 +                       bcm947xx_parts[0].size - 
230 +                       (2*bcm947xx_parts[3].size) - 
231 +                       mtd->erasesize;
232 +       }
233 +
234 +       /* find and size rootfs */
235 +       if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
236 +               /* entirely jffs2 */
237 +               bcm947xx_parts[4].name = NULL;
238 +               if (cfe_size_flag == 0) {
239 +                       bcm947xx_parts[2].size = bcm947xx_parts[3].offset - 
240 +                               bcm947xx_parts[2].offset;
241 +               } else {
242 +                       bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - 
243 +                               bcm947xx_parts[3].size; 
244 +               }
245 +       } else {
246 +               /* legacy setup */
247 +               /* calculate leftover flash, and assign it to the jffs2 partition */
248 +               if (cfe_size_flag == 0) {
249 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
250 +                               bcm947xx_parts[2].size;
251 +                       bcm947xx_parts[4].size = bcm947xx_parts[3].offset - 
252 +                               bcm947xx_parts[4].offset;
253 +               } else {
254 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
255 +                               bcm947xx_parts[2].size;
256 +                       if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
257 +                               bcm947xx_parts[4].offset += mtd->erasesize - 
258 +                                       (bcm947xx_parts[4].offset % mtd->erasesize);
259 +                       }
260 +                       bcm947xx_parts[4].size = size - bcm947xx_parts[3].size - 
261 +                               bcm947xx_parts[4].offset;
262 +               }
263 +       }
264 +
265 +       return bcm947xx_parts;
266 +}
267 +
268 +EXPORT_SYMBOL(init_mtd_partitions);
269 +#endif
270 +
271 +int __init init_bcm947xx_map(void)
272 +{
273 +       size_t size;
274 +       int ret = 0;
275 +#ifdef CONFIG_MTD_PARTITIONS
276 +       struct mtd_partition *parts;
277 +       int i;
278 +#endif
279 +
280 +       bcm947xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
281 +
282 +       if (!bcm947xx_map.virt) {
283 +               printk("Failed to ioremap\n");
284 +               return -EIO;
285 +       }
286 +       simple_map_init(&bcm947xx_map);
287 +       
288 +       bcm947xx_map.copy_from = bcm947xx_map_copy_from;
289 +       
290 +       if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
291 +               printk("Failed to do_map_probe\n");
292 +               iounmap((void *)bcm947xx_map.virt);
293 +               return -ENXIO;
294 +       }
295 +
296 +       bcm947xx_mtd->owner = THIS_MODULE;
297 +
298 +       size = bcm947xx_mtd->size;
299 +
300 +       printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", bcm947xx_mtd->size, WINDOW_ADDR);
301 +
302 +#ifdef CONFIG_MTD_PARTITIONS
303 +       parts = init_mtd_partitions(bcm947xx_mtd, size);
304 +       for (i = 0; parts[i].name; i++);
305 +       ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
306 +       if (ret) {
307 +               printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
308 +               goto fail;
309 +       }
310 +#endif
311 +
312 +       return 0;
313 +
314 + fail:
315 +       if (bcm947xx_mtd)
316 +               map_destroy(bcm947xx_mtd);
317 +       if (bcm947xx_map.map_priv_1)
318 +               iounmap((void *) bcm947xx_map.map_priv_1);
319 +       bcm947xx_map.map_priv_1 = 0;
320 +       return ret;
321 +}
322 +
323 +void __exit cleanup_bcm947xx_map(void)
324 +{
325 +#ifdef CONFIG_MTD_PARTITIONS
326 +       del_mtd_partitions(bcm947xx_mtd);
327 +#endif
328 +       map_destroy(bcm947xx_mtd);
329 +       iounmap((void *)bcm947xx_map.virt);
330 +}
331 +
332 +module_init(init_bcm947xx_map);
333 +module_exit(cleanup_bcm947xx_map);
334 diff -Nur linux-2.6.12.5/drivers/mtd/maps/Kconfig linux-2.6.12.5-flash/drivers/mtd/maps/Kconfig
335 --- linux-2.6.12.5/drivers/mtd/maps/Kconfig     2005-08-15 02:20:18.000000000 +0200
336 +++ linux-2.6.12.5-flash/drivers/mtd/maps/Kconfig       2005-09-16 22:27:36.000000000 +0200
337 @@ -357,6 +357,12 @@
338           Mapping for the Flaga digital module. If you don't have one, ignore
339           this setting.
340  
341 +config MTD_BCM47XX
342 +       tristate "BCM47xx flash device"
343 +       depends on MIPS && MTD_CFI && BCM947XX
344 +       help
345 +         Support for the flash chips on the BCM947xx board.
346 +         
347  config MTD_BEECH
348         tristate "CFI Flash device mapped on IBM 405LP Beech"
349         depends on MTD_CFI && PPC32 && 40x && BEECH
350 diff -Nur linux-2.6.12.5/drivers/mtd/maps/Makefile linux-2.6.12.5-flash/drivers/mtd/maps/Makefile
351 --- linux-2.6.12.5/drivers/mtd/maps/Makefile    2005-08-15 02:20:18.000000000 +0200
352 +++ linux-2.6.12.5-flash/drivers/mtd/maps/Makefile      2005-09-16 22:27:01.000000000 +0200
353 @@ -31,6 +31,7 @@
354  obj-$(CONFIG_MTD_PCMCIA)       += pcmciamtd.o
355  obj-$(CONFIG_MTD_RPXLITE)      += rpxlite.o
356  obj-$(CONFIG_MTD_TQM8XXL)      += tqm8xxl.o
357 +obj-$(CONFIG_MTD_BCM47XX)      += bcm47xx-flash.o
358  obj-$(CONFIG_MTD_SA1100)       += sa1100-flash.o
359  obj-$(CONFIG_MTD_IPAQ)         += ipaq-flash.o
360  obj-$(CONFIG_MTD_SBC_GXX)      += sbc_gxx.o