fix flash map driver for squashfs images for wrt54gs
[openwrt/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-29 12:50:51.416656000 +0100
4 @@ -0,0 +1,333 @@
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 block */
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 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
252 + bcm947xx_parts[4].offset += mtd->erasesize -
253 + (bcm947xx_parts[4].offset % mtd->erasesize);
254 + }
255 + bcm947xx_parts[4].size = bcm947xx_parts[3].offset -
256 + bcm947xx_parts[4].offset;
257 + } else {
258 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
259 + bcm947xx_parts[2].size;
260 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
261 + bcm947xx_parts[4].offset += mtd->erasesize -
262 + (bcm947xx_parts[4].offset % mtd->erasesize);
263 + }
264 + bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
265 + bcm947xx_parts[4].offset;
266 + }
267 + }
268 +
269 + return bcm947xx_parts;
270 +}
271 +
272 +EXPORT_SYMBOL(init_mtd_partitions);
273 +#endif
274 +
275 +int __init init_bcm947xx_map(void)
276 +{
277 + size_t size;
278 + int ret = 0;
279 +#ifdef CONFIG_MTD_PARTITIONS
280 + struct mtd_partition *parts;
281 + int i;
282 +#endif
283 +
284 + bcm947xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
285 +
286 + if (!bcm947xx_map.virt) {
287 + printk("Failed to ioremap\n");
288 + return -EIO;
289 + }
290 + simple_map_init(&bcm947xx_map);
291 +
292 + bcm947xx_map.copy_from = bcm947xx_map_copy_from;
293 +
294 + if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
295 + printk("Failed to do_map_probe\n");
296 + iounmap((void *)bcm947xx_map.virt);
297 + return -ENXIO;
298 + }
299 +
300 + bcm947xx_mtd->owner = THIS_MODULE;
301 +
302 + size = bcm947xx_mtd->size;
303 +
304 + printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", bcm947xx_mtd->size, WINDOW_ADDR);
305 +
306 +#ifdef CONFIG_MTD_PARTITIONS
307 + parts = init_mtd_partitions(bcm947xx_mtd, size);
308 + for (i = 0; parts[i].name; i++);
309 + ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
310 + if (ret) {
311 + printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
312 + goto fail;
313 + }
314 +#endif
315 +
316 + return 0;
317 +
318 + fail:
319 + if (bcm947xx_mtd)
320 + map_destroy(bcm947xx_mtd);
321 + if (bcm947xx_map.map_priv_1)
322 + iounmap((void *) bcm947xx_map.map_priv_1);
323 + bcm947xx_map.map_priv_1 = 0;
324 + return ret;
325 +}
326 +
327 +void __exit cleanup_bcm947xx_map(void)
328 +{
329 +#ifdef CONFIG_MTD_PARTITIONS
330 + del_mtd_partitions(bcm947xx_mtd);
331 +#endif
332 + map_destroy(bcm947xx_mtd);
333 + iounmap((void *)bcm947xx_map.virt);
334 +}
335 +
336 +module_init(init_bcm947xx_map);
337 +module_exit(cleanup_bcm947xx_map);
338 diff -Nur linux-2.6.12.5/drivers/mtd/maps/Kconfig linux-2.6.12.5-flash/drivers/mtd/maps/Kconfig
339 --- linux-2.6.12.5/drivers/mtd/maps/Kconfig 2005-08-15 02:20:18.000000000 +0200
340 +++ linux-2.6.12.5-flash/drivers/mtd/maps/Kconfig 2005-09-16 22:27:36.000000000 +0200
341 @@ -357,6 +357,12 @@
342 Mapping for the Flaga digital module. If you don't have one, ignore
343 this setting.
344
345 +config MTD_BCM47XX
346 + tristate "BCM47xx flash device"
347 + depends on MIPS && MTD_CFI && BCM947XX
348 + help
349 + Support for the flash chips on the BCM947xx board.
350 +
351 config MTD_BEECH
352 tristate "CFI Flash device mapped on IBM 405LP Beech"
353 depends on MTD_CFI && PPC32 && 40x && BEECH
354 diff -Nur linux-2.6.12.5/drivers/mtd/maps/Makefile linux-2.6.12.5-flash/drivers/mtd/maps/Makefile
355 --- linux-2.6.12.5/drivers/mtd/maps/Makefile 2005-08-15 02:20:18.000000000 +0200
356 +++ linux-2.6.12.5-flash/drivers/mtd/maps/Makefile 2005-09-16 22:27:01.000000000 +0200
357 @@ -31,6 +31,7 @@
358 obj-$(CONFIG_MTD_PCMCIA) += pcmciamtd.o
359 obj-$(CONFIG_MTD_RPXLITE) += rpxlite.o
360 obj-$(CONFIG_MTD_TQM8XXL) += tqm8xxl.o
361 +obj-$(CONFIG_MTD_BCM47XX) += bcm47xx-flash.o
362 obj-$(CONFIG_MTD_SA1100) += sa1100-flash.o
363 obj-$(CONFIG_MTD_IPAQ) += ipaq-flash.o
364 obj-$(CONFIG_MTD_SBC_GXX) += sbc_gxx.o