Fix brcm63xx support. Now a kernel is booting, detecting the flash, and can probably...
[openwrt.git] / target / linux / brcm-2.6 / patches / 002-flash-map.patch
1 diff -urN linux.old/drivers/mtd/maps/bcm47xx-flash.c linux.dev/drivers/mtd/maps/bcm47xx-flash.c
2 --- linux.old/drivers/mtd/maps/bcm47xx-flash.c 1970-01-01 01:00:00.000000000 +0100
3 +++ linux.dev/drivers/mtd/maps/bcm47xx-flash.c 2006-06-23 19:54:06.000000000 +0200
4 @@ -0,0 +1,451 @@
5 +/*
6 + * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
7 + * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
8 + * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
9 + *
10 + * original functions for finding root filesystem from Mike Baker
11 + *
12 + * This program is free software; you can redistribute it and/or modify it
13 + * under the terms of the GNU General Public License as published by the
14 + * Free Software Foundation; either version 2 of the License, or (at your
15 + * option) any later version.
16 + *
17 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
20 + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24 + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 + *
28 + * You should have received a copy of the GNU General Public License along
29 + * with this program; if not, write to the Free Software Foundation, Inc.,
30 + * 675 Mass Ave, Cambridge, MA 02139, USA.
31 + *
32 + * Copyright 2001-2003, Broadcom Corporation
33 + * All Rights Reserved.
34 + *
35 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
36 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
37 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
38 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
39 + *
40 + * Flash mapping for BCM947XX boards
41 + */
42 +
43 +#include <linux/init.h>
44 +#include <linux/module.h>
45 +#include <linux/types.h>
46 +#include <linux/kernel.h>
47 +#include <linux/wait.h>
48 +#include <linux/config.h>
49 +#include <linux/mtd/mtd.h>
50 +#include <linux/mtd/map.h>
51 +#ifdef CONFIG_MTD_PARTITIONS
52 +#include <linux/mtd/partitions.h>
53 +#endif
54 +#include <linux/squashfs_fs.h>
55 +#include <linux/jffs2.h>
56 +#include <linux/crc32.h>
57 +#include <asm/io.h>
58 +
59 +#include <trxhdr.h>
60 +
61 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
62 +#define NVRAM_SPACE 0x8000
63 +#define WINDOW_ADDR 0x1c000000
64 +#define WINDOW_SIZE 0x800000
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 blocksize;
115 +
116 + trx = (struct trx_header *) buf;
117 +
118 + blocksize = mtd->erasesize;
119 + if (blocksize < 0x10000)
120 + blocksize = 0x10000;
121 +
122 + for (off = (128*1024); off < size; off += blocksize) {
123 + memset(buf, 0xe5, sizeof(buf));
124 +
125 + /*
126 + * Read into buffer
127 + */
128 + if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
129 + len != sizeof(buf))
130 + continue;
131 +
132 + /* found a TRX header */
133 + if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
134 + goto found;
135 + }
136 + }
137 +
138 + printk(KERN_NOTICE
139 + "%s: Couldn't find bootloader size\n",
140 + mtd->name);
141 + return -1;
142 +
143 + found:
144 + printk(KERN_NOTICE "bootloader size: %d\n", off);
145 + return off;
146 +
147 +}
148 +
149 +/*
150 + * Copied from mtdblock.c
151 + *
152 + * Cache stuff...
153 + *
154 + * Since typical flash erasable sectors are much larger than what Linux's
155 + * buffer cache can handle, we must implement read-modify-write on flash
156 + * sectors for each block write requests. To avoid over-erasing flash sectors
157 + * and to speed things up, we locally cache a whole flash sector while it is
158 + * being written to until a different sector is required.
159 + */
160 +
161 +static void erase_callback(struct erase_info *done)
162 +{
163 + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
164 + wake_up(wait_q);
165 +}
166 +
167 +static int erase_write (struct mtd_info *mtd, unsigned long pos,
168 + int len, const char *buf)
169 +{
170 + struct erase_info erase;
171 + DECLARE_WAITQUEUE(wait, current);
172 + wait_queue_head_t wait_q;
173 + size_t retlen;
174 + int ret;
175 +
176 + /*
177 + * First, let's erase the flash block.
178 + */
179 +
180 + init_waitqueue_head(&wait_q);
181 + erase.mtd = mtd;
182 + erase.callback = erase_callback;
183 + erase.addr = pos;
184 + erase.len = len;
185 + erase.priv = (u_long)&wait_q;
186 +
187 + set_current_state(TASK_INTERRUPTIBLE);
188 + add_wait_queue(&wait_q, &wait);
189 +
190 + ret = MTD_ERASE(mtd, &erase);
191 + if (ret) {
192 + set_current_state(TASK_RUNNING);
193 + remove_wait_queue(&wait_q, &wait);
194 + printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
195 + "on \"%s\" failed\n",
196 + pos, len, mtd->name);
197 + return ret;
198 + }
199 +
200 + schedule(); /* Wait for erase to finish. */
201 + remove_wait_queue(&wait_q, &wait);
202 +
203 + /*
204 + * Next, writhe data to flash.
205 + */
206 +
207 + ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
208 + if (ret)
209 + return ret;
210 + if (retlen != len)
211 + return -EIO;
212 + return 0;
213 +}
214 +
215 +
216 +
217 +
218 +static int __init
219 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
220 +{
221 + struct trx_header trx, *trx2;
222 + unsigned char buf[512], *block;
223 + int off, blocksize;
224 + u32 i, crc = ~0;
225 + size_t len;
226 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
227 +
228 + blocksize = mtd->erasesize;
229 + if (blocksize < 0x10000)
230 + blocksize = 0x10000;
231 +
232 + for (off = (128*1024); off < size; off += blocksize) {
233 + memset(&trx, 0xe5, sizeof(trx));
234 +
235 + /*
236 + * Read into buffer
237 + */
238 + if (MTD_READ(mtd, off, sizeof(trx), &len, (char *) &trx) ||
239 + len != sizeof(trx))
240 + continue;
241 +
242 + /* found a TRX header */
243 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
244 + part->offset = le32_to_cpu(trx.offsets[2]) ? :
245 + le32_to_cpu(trx.offsets[1]);
246 + part->size = le32_to_cpu(trx.len);
247 +
248 + part->size -= part->offset;
249 + part->offset += off;
250 +
251 + goto found;
252 + }
253 + }
254 +
255 + printk(KERN_NOTICE
256 + "%s: Couldn't find root filesystem\n",
257 + mtd->name);
258 + return -1;
259 +
260 + found:
261 + if (part->size == 0)
262 + return 0;
263 +
264 + if (MTD_READ(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
265 + return 0;
266 +
267 + if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
268 + printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
269 +
270 + /* Update the squashfs partition size based on the superblock info */
271 + part->size = sb->bytes_used;
272 + len = part->offset + part->size;
273 + len += (mtd->erasesize - 1);
274 + len &= ~(mtd->erasesize - 1);
275 + part->size = len - part->offset;
276 + } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
277 + printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
278 +
279 + /* Move the squashfs outside of the trx */
280 + part->size = 0;
281 + } else {
282 + printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
283 + return 0;
284 + }
285 +
286 + if (trx.len != part->offset + part->size - off) {
287 + /* Update the trx offsets and length */
288 + trx.len = part->offset + part->size - off;
289 +
290 + /* Update the trx crc32 */
291 + for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
292 + if (MTD_READ(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
293 + return 0;
294 + crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
295 + }
296 + trx.crc32 = crc;
297 +
298 + /* read first eraseblock from the trx */
299 + block = kmalloc(mtd->erasesize, GFP_KERNEL);
300 + trx2 = (struct trx_header *) block;
301 + if (MTD_READ(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
302 + printk("Error accessing the first trx eraseblock\n");
303 + return 0;
304 + }
305 +
306 + printk("Updating TRX offsets and length:\n");
307 + printk("old trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx2->offsets[0], trx2->offsets[1], trx2->offsets[2], trx2->len, trx2->crc32);
308 + printk("new trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx.offsets[0], trx.offsets[1], trx.offsets[2], trx.len, trx.crc32);
309 +
310 + /* Write updated trx header to the flash */
311 + memcpy(block, &trx, sizeof(trx));
312 + if (mtd->unlock)
313 + mtd->unlock(mtd, off, mtd->erasesize);
314 + erase_write(mtd, off, mtd->erasesize, block);
315 + if (mtd->sync)
316 + mtd->sync(mtd);
317 + kfree(block);
318 + printk("Done\n");
319 + }
320 +
321 + return part->size;
322 +}
323 +
324 +struct mtd_partition * __init
325 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
326 +{
327 + int cfe_size;
328 +
329 + cfe_size = find_cfe_size(mtd,size);
330 +
331 + /* boot loader */
332 + bcm947xx_parts[0].offset = 0;
333 + bcm947xx_parts[0].size = cfe_size;
334 +
335 + /* nvram */
336 + if (cfe_size != 384 * 1024) {
337 + bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
338 + bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
339 + } else {
340 + /* nvram (old 128kb config partition on netgear wgt634u) */
341 + bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
342 + bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
343 + }
344 +
345 + /* linux (kernel and rootfs) */
346 + if (cfe_size != 384 * 1024) {
347 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
348 + bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
349 + bcm947xx_parts[1].offset;
350 + } else {
351 + /* do not count the elf loader, which is on one block */
352 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
353 + bcm947xx_parts[3].size + mtd->erasesize;
354 + bcm947xx_parts[1].size = size -
355 + bcm947xx_parts[0].size -
356 + (2*bcm947xx_parts[3].size) -
357 + mtd->erasesize;
358 + }
359 +
360 + /* find and size rootfs */
361 + if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
362 + /* entirely jffs2 */
363 + bcm947xx_parts[4].name = NULL;
364 + bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset -
365 + bcm947xx_parts[3].size;
366 + } else {
367 + /* legacy setup */
368 + /* calculate leftover flash, and assign it to the jffs2 partition */
369 + if (cfe_size != 384 * 1024) {
370 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
371 + bcm947xx_parts[2].size;
372 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
373 + bcm947xx_parts[4].offset += mtd->erasesize -
374 + (bcm947xx_parts[4].offset % mtd->erasesize);
375 + }
376 + bcm947xx_parts[4].size = bcm947xx_parts[3].offset -
377 + bcm947xx_parts[4].offset;
378 + } else {
379 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
380 + bcm947xx_parts[2].size;
381 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
382 + bcm947xx_parts[4].offset += mtd->erasesize -
383 + (bcm947xx_parts[4].offset % mtd->erasesize);
384 + }
385 + bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
386 + bcm947xx_parts[4].offset;
387 + }
388 + }
389 +
390 + return bcm947xx_parts;
391 +}
392 +#endif
393 +
394 +int __init init_bcm947xx_map(void)
395 +{
396 + size_t size;
397 + int ret = 0;
398 +#ifdef CONFIG_MTD_PARTITIONS
399 + struct mtd_partition *parts;
400 + int i;
401 +#endif
402 +
403 + bcm947xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
404 +
405 + if (!bcm947xx_map.virt) {
406 + printk("Failed to ioremap\n");
407 + return -EIO;
408 + }
409 + simple_map_init(&bcm947xx_map);
410 +
411 + bcm947xx_map.copy_from = bcm947xx_map_copy_from;
412 +
413 + if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
414 + printk("Failed to do_map_probe\n");
415 + iounmap((void *)bcm947xx_map.virt);
416 + return -ENXIO;
417 + }
418 +
419 + bcm947xx_mtd->owner = THIS_MODULE;
420 +
421 + size = bcm947xx_mtd->size;
422 +
423 + printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
424 +
425 +#ifdef CONFIG_MTD_PARTITIONS
426 + parts = init_mtd_partitions(bcm947xx_mtd, size);
427 + for (i = 0; parts[i].name; i++);
428 + ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
429 + if (ret) {
430 + printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
431 + goto fail;
432 + }
433 +#endif
434 + return 0;
435 +
436 + fail:
437 + if (bcm947xx_mtd)
438 + map_destroy(bcm947xx_mtd);
439 + if (bcm947xx_map.virt)
440 + iounmap((void *)bcm947xx_map.virt);
441 + bcm947xx_map.virt = 0;
442 + return ret;
443 +}
444 +
445 +void __exit cleanup_bcm947xx_map(void)
446 +{
447 +#ifdef CONFIG_MTD_PARTITIONS
448 + del_mtd_partitions(bcm947xx_mtd);
449 +#endif
450 + map_destroy(bcm947xx_mtd);
451 + iounmap((void *)bcm947xx_map.virt);
452 +}
453 +
454 +module_init(init_bcm947xx_map);
455 +module_exit(cleanup_bcm947xx_map);
456 diff -urN linux.old/drivers/mtd/maps/Kconfig linux.dev/drivers/mtd/maps/Kconfig
457 --- linux.old/drivers/mtd/maps/Kconfig 2006-06-23 19:13:51.000000000 +0200
458 +++ linux.dev/drivers/mtd/maps/Kconfig 2006-06-23 18:47:58.000000000 +0200
459 @@ -299,6 +299,12 @@
460 Mapping for the Flaga digital module. If you don't have one, ignore
461 this setting.
462
463 +config MTD_BCM47XX
464 + tristate "BCM47xx flash device"
465 + depends on MIPS && MTD_CFI && BCM947XX
466 + help
467 + Support for the flash chips on the BCM947xx board.
468 +
469 config MTD_BEECH
470 tristate "CFI Flash device mapped on IBM 405LP Beech"
471 depends on MTD_CFI && BEECH
472 diff -urN linux.old/drivers/mtd/maps/Makefile linux.dev/drivers/mtd/maps/Makefile
473 --- linux.old/drivers/mtd/maps/Makefile 2006-06-23 19:13:51.000000000 +0200
474 +++ linux.dev/drivers/mtd/maps/Makefile 2006-06-23 18:47:58.000000000 +0200
475 @@ -30,6 +30,7 @@
476 obj-$(CONFIG_MTD_PCMCIA) += pcmciamtd.o
477 obj-$(CONFIG_MTD_RPXLITE) += rpxlite.o
478 obj-$(CONFIG_MTD_TQM8XXL) += tqm8xxl.o
479 +obj-$(CONFIG_MTD_BCM47XX) += bcm47xx-flash.o
480 obj-$(CONFIG_MTD_SA1100) += sa1100-flash.o
481 obj-$(CONFIG_MTD_IPAQ) += ipaq-flash.o
482 obj-$(CONFIG_MTD_SBC_GXX) += sbc_gxx.o
This page took 0.066098 seconds and 5 git commands to generate.