14ba3b15f4e11fa1802fff78aeef1e563df474f9
[10.03/openwrt.git] / target / linux / brcm-2.4 / patches / 004-flash.patch
1 diff -urN linux.old/drivers/mtd/devices/Config.in linux.dev/drivers/mtd/devices/Config.in
2 --- linux.old/drivers/mtd/devices/Config.in     2006-06-22 17:35:39.000000000 +0200
3 +++ linux.dev/drivers/mtd/devices/Config.in     2006-06-21 21:41:24.000000000 +0200
4 @@ -5,6 +5,7 @@
5  mainmenu_option next_comment
6  
7  comment 'Self-contained MTD device drivers'
8 +bool '  Broadcom Chipcommon Serial Flash support' CONFIG_MTD_SFLASH
9  dep_tristate '  Ramix PMC551 PCI Mezzanine RAM card support' CONFIG_MTD_PMC551 $CONFIG_MTD $CONFIG_PCI
10  if [ "$CONFIG_MTD_PMC551" = "y" -o  "$CONFIG_MTD_PMC551" = "m" ]; then
11     bool '    PMC551 256M DRAM Bugfix' CONFIG_MTD_PMC551_BUGFIX
12 diff -urN linux.old/drivers/mtd/devices/Makefile linux.dev/drivers/mtd/devices/Makefile
13 --- linux.old/drivers/mtd/devices/Makefile      2006-06-22 17:35:39.000000000 +0200
14 +++ linux.dev/drivers/mtd/devices/Makefile      2006-06-21 21:41:24.000000000 +0200
15 @@ -3,6 +3,8 @@
16  #
17  # $Id: Makefile,v 1.4 2001/06/26 21:10:05 spse Exp $
18  
19 +EXTRA_CFLAGS := -I$(TOPDIR)/arch/mips/bcm947xx/include
20 +
21  O_TARGET       := devlink.o
22  
23  #                       *** BIG UGLY NOTE ***
24 @@ -12,6 +14,7 @@
25  # here where previously there was none.  We now have to ensure that
26  # doc200[01].o are linked before docprobe.o
27  
28 +obj-$(CONFIG_MTD_SFLASH)       += sflash.o
29  obj-$(CONFIG_MTD_DOC1000)      += doc1000.o
30  obj-$(CONFIG_MTD_DOC2000)      += doc2000.o
31  obj-$(CONFIG_MTD_DOC2001)      += doc2001.o
32 diff -urN linux.old/drivers/mtd/devices/sflash.c linux.dev/drivers/mtd/devices/sflash.c
33 --- linux.old/drivers/mtd/devices/sflash.c      1970-01-01 01:00:00.000000000 +0100
34 +++ linux.dev/drivers/mtd/devices/sflash.c      2006-06-21 21:41:24.000000000 +0200
35 @@ -0,0 +1,298 @@
36 +/*
37 + * Broadcom SiliconBackplane chipcommon serial flash interface
38 + *
39 + * Copyright 2001-2003, Broadcom Corporation   
40 + * All Rights Reserved.   
41 + *    
42 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY   
43 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM   
44 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS   
45 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.   
46 + *
47 + * $Id: sflash.c,v 1.1.1.3 2003/11/10 17:43:38 hyin Exp $
48 + */
49 +
50 +#include <linux/config.h>
51 +#include <linux/module.h>
52 +#include <linux/slab.h>
53 +#include <linux/ioport.h>
54 +#include <linux/mtd/compatmac.h>
55 +#include <linux/mtd/mtd.h>
56 +#include <linux/mtd/partitions.h>
57 +#include <linux/errno.h>
58 +#include <linux/pci.h>
59 +#include <linux/delay.h>
60 +#include <asm/io.h>
61 +
62 +#ifdef CONFIG_MTD_PARTITIONS
63 +#include <linux/mtd/mtd.h>
64 +#include <linux/mtd/partitions.h>
65 +#include <linux/minix_fs.h>
66 +#include <linux/ext2_fs.h>
67 +#include <linux/romfs_fs.h>
68 +#include <linux/cramfs_fs.h>
69 +#include <linux/jffs2.h>
70 +#endif
71 +
72 +#include <typedefs.h>
73 +#include <bcmdevs.h>
74 +#include <bcmutils.h>
75 +#include <osl.h>
76 +#include <bcmutils.h>
77 +#include <bcmnvram.h>
78 +#include <sbconfig.h>
79 +#include <sbchipc.h>
80 +#include <sflash.h>
81 +#include <trxhdr.h>
82 +
83 +#ifdef CONFIG_MTD_PARTITIONS
84 +extern struct mtd_partition * init_mtd_partitions(struct mtd_info *mtd, size_t size);
85 +#endif
86 +
87 +struct sflash_mtd {
88 +       chipcregs_t *cc;
89 +       struct semaphore lock;
90 +       struct mtd_info mtd;
91 +       struct mtd_erase_region_info regions[1];
92 +};
93 +
94 +/* Private global state */
95 +static struct sflash_mtd sflash;
96 +
97 +static int
98 +sflash_mtd_poll(struct sflash_mtd *sflash, unsigned int offset, int timeout)
99 +{
100 +       int now = jiffies;
101 +       int ret = 0;
102 +
103 +       for (;;) {
104 +               if (!sflash_poll(sflash->cc, offset)) {
105 +                       ret = 0;
106 +                       break;
107 +               }
108 +               if (time_after(jiffies, now + timeout)) {
109 +                       printk(KERN_ERR "sflash: timeout\n");
110 +                       ret = -ETIMEDOUT;
111 +                       break;
112 +               }
113 +               if (current->need_resched) {
114 +                       set_current_state(TASK_UNINTERRUPTIBLE);
115 +                       schedule_timeout(timeout / 10);
116 +               } else
117 +                       udelay(1);
118 +       }
119 +
120 +       return ret;
121 +}
122 +
123 +static int
124 +sflash_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
125 +{
126 +       struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
127 +       int bytes, ret = 0;
128 +
129 +       /* Check address range */
130 +       if (!len)
131 +               return 0;
132 +       if ((from + len) > mtd->size)
133 +               return -EINVAL;
134 +       
135 +       down(&sflash->lock);
136 +
137 +       *retlen = 0;
138 +       while (len) {
139 +               if ((bytes = sflash_read(sflash->cc, (uint) from, len, buf)) < 0) {
140 +                       ret = bytes;
141 +                       break;
142 +               }
143 +               from += (loff_t) bytes;
144 +               len -= bytes;
145 +               buf += bytes;
146 +               *retlen += bytes;
147 +       }
148 +
149 +       up(&sflash->lock);
150 +
151 +       return ret;
152 +}
153 +
154 +static int
155 +sflash_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, const u_char *buf)
156 +{
157 +       struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
158 +       int bytes, ret = 0;
159 +
160 +       /* Check address range */
161 +       if (!len)
162 +               return 0;
163 +       if ((to + len) > mtd->size)
164 +               return -EINVAL;
165 +
166 +       down(&sflash->lock);
167 +
168 +       *retlen = 0;
169 +       while (len) {
170 +               if ((bytes = sflash_write(sflash->cc, (uint) to, len, buf)) < 0) {
171 +                       ret = bytes;
172 +                       break;
173 +               }
174 +               if ((ret = sflash_mtd_poll(sflash, (unsigned int) to, HZ / 10)))
175 +                       break;
176 +               to += (loff_t) bytes;
177 +               len -= bytes;
178 +               buf += bytes;
179 +               *retlen += bytes;
180 +       }
181 +
182 +       up(&sflash->lock);
183 +
184 +       return ret;
185 +}
186 +
187 +static int
188 +sflash_mtd_erase(struct mtd_info *mtd, struct erase_info *erase)
189 +{
190 +       struct sflash_mtd *sflash = (struct sflash_mtd *) mtd->priv;
191 +       int i, j, ret = 0;
192 +       unsigned int addr, len;
193 +
194 +       /* Check address range */
195 +       if (!erase->len)
196 +               return 0;
197 +       if ((erase->addr + erase->len) > mtd->size)
198 +               return -EINVAL;
199 +
200 +       addr = erase->addr;
201 +       len = erase->len;
202 +
203 +       down(&sflash->lock);
204 +
205 +       /* Ensure that requested region is aligned */
206 +       for (i = 0; i < mtd->numeraseregions; i++) {
207 +               for (j = 0; j < mtd->eraseregions[i].numblocks; j++) {
208 +                       if (addr == mtd->eraseregions[i].offset + mtd->eraseregions[i].erasesize * j &&
209 +                           len >= mtd->eraseregions[i].erasesize) {
210 +                               if ((ret = sflash_erase(sflash->cc, addr)) < 0)
211 +                                       break;
212 +                               if ((ret = sflash_mtd_poll(sflash, addr, 10 * HZ)))
213 +                                       break;
214 +                               addr += mtd->eraseregions[i].erasesize;
215 +                               len -= mtd->eraseregions[i].erasesize;
216 +                       }
217 +               }
218 +               if (ret)
219 +                       break;
220 +       }
221 +
222 +       up(&sflash->lock);
223 +
224 +       /* Set erase status */
225 +       if (ret)
226 +               erase->state = MTD_ERASE_FAILED;
227 +       else 
228 +               erase->state = MTD_ERASE_DONE;
229 +
230 +       /* Call erase callback */
231 +       if (erase->callback)
232 +               erase->callback(erase);
233 +
234 +       return ret;
235 +}
236 +
237 +#if LINUX_VERSION_CODE < 0x20212 && defined(MODULE)
238 +#define sflash_mtd_init init_module
239 +#define sflash_mtd_exit cleanup_module
240 +#endif
241 +
242 +mod_init_t
243 +sflash_mtd_init(void)
244 +{
245 +       struct pci_dev *pdev;
246 +       int ret = 0;
247 +       struct sflash *info;
248 +       uint bank, i;
249 +#ifdef CONFIG_MTD_PARTITIONS
250 +       struct mtd_partition *parts;
251 +#endif
252 +
253 +       if (!(pdev = pci_find_device(VENDOR_BROADCOM, SB_CC, NULL))) {
254 +               printk(KERN_ERR "sflash: chipcommon not found\n");
255 +               return -ENODEV;
256 +       }
257 +
258 +       memset(&sflash, 0, sizeof(struct sflash_mtd));
259 +       init_MUTEX(&sflash.lock);
260 +
261 +       /* Map registers and flash base */
262 +       if (!(sflash.cc = ioremap_nocache(pci_resource_start(pdev, 0),
263 +                                         pci_resource_len(pdev, 0)))) {
264 +               printk(KERN_ERR "sflash: error mapping registers\n");
265 +               ret = -EIO;
266 +               goto fail;
267 +       }
268 +
269 +       /* Initialize serial flash access */
270 +       info = sflash_init(sflash.cc);
271 +
272 +       if (!info) {
273 +               printk(KERN_ERR "sflash: found no supported devices\n");
274 +               ret = -ENODEV;
275 +               goto fail;
276 +       }
277 +
278 +       /* Setup banks */
279 +       sflash.regions[0].offset = 0;
280 +       sflash.regions[0].erasesize = info->blocksize;
281 +       sflash.regions[0].numblocks = info->numblocks;
282 +       if (sflash.regions[0].erasesize > sflash.mtd.erasesize)
283 +               sflash.mtd.erasesize = sflash.regions[0].erasesize;
284 +       if (sflash.regions[0].erasesize * sflash.regions[0].numblocks) {
285 +               sflash.mtd.size += sflash.regions[0].erasesize * sflash.regions[0].numblocks;
286 +       }
287 +       sflash.mtd.numeraseregions = 1;
288 +       ASSERT(sflash.mtd.size == info->size);
289 +
290 +       /* Register with MTD */
291 +       sflash.mtd.name = "sflash";
292 +       sflash.mtd.type = MTD_NORFLASH;
293 +       sflash.mtd.flags = MTD_CAP_NORFLASH;
294 +       sflash.mtd.eraseregions = sflash.regions;
295 +       sflash.mtd.module = THIS_MODULE;
296 +       sflash.mtd.erase = sflash_mtd_erase;
297 +       sflash.mtd.read = sflash_mtd_read;
298 +       sflash.mtd.write = sflash_mtd_write;
299 +       sflash.mtd.priv = &sflash;
300 +
301 +#ifdef CONFIG_MTD_PARTITIONS
302 +       parts = init_mtd_partitions(&sflash.mtd, sflash.mtd.size);
303 +       for (i = 0; parts[i].name; i++);
304 +       ret = add_mtd_partitions(&sflash.mtd, parts, i);
305 +#else
306 +       ret = add_mtd_device(&sflash.mtd);
307 +#endif
308 +       if (ret) {
309 +               printk(KERN_ERR "sflash: add_mtd failed\n");
310 +               goto fail;
311 +       }
312 +
313 +       return 0;
314 +
315 + fail:
316 +       if (sflash.cc)
317 +               iounmap((void *) sflash.cc);
318 +       return ret;
319 +}
320 +
321 +mod_exit_t
322 +sflash_mtd_exit(void)
323 +{
324 +#ifdef CONFIG_MTD_PARTITIONS
325 +       del_mtd_partitions(&sflash.mtd);
326 +#else
327 +       del_mtd_device(&sflash.mtd);
328 +#endif
329 +       iounmap((void *) sflash.cc);
330 +}
331 +
332 +module_init(sflash_mtd_init);
333 +module_exit(sflash_mtd_exit);
334 diff -urN linux.old/drivers/mtd/maps/bcm947xx-flash.c linux.dev/drivers/mtd/maps/bcm947xx-flash.c
335 --- linux.old/drivers/mtd/maps/bcm947xx-flash.c 1970-01-01 01:00:00.000000000 +0100
336 +++ linux.dev/drivers/mtd/maps/bcm947xx-flash.c 2006-06-23 18:08:46.000000000 +0200
337 @@ -0,0 +1,547 @@
338 +/*
339 + *  Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
340 + *  Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
341 + *  Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
342 + *
343 + *  original functions for finding root filesystem from Mike Baker 
344 + *
345 + *  This program is free software; you can redistribute  it and/or modify it
346 + *  under  the terms of  the GNU General  Public License as published by the
347 + *  Free Software Foundation;  either version 2 of the  License, or (at your
348 + *  option) any later version.
349 + *
350 + *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
351 + *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
352 + *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
353 + *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
354 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
355 + *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
356 + *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
357 + *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
358 + *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
359 + *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
360 + *
361 + *  You should have received a copy of the  GNU General Public License along
362 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
363 + *  675 Mass Ave, Cambridge, MA 02139, USA.
364 + * 
365 + *
366 + * Copyright 2004, Broadcom Corporation
367 + * All Rights Reserved.
368 + * 
369 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
370 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
371 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
372 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
373 + *
374 + * Flash mapping for BCM947XX boards
375 + *
376 + */
377 +
378 +#include <linux/module.h>
379 +#include <linux/types.h>
380 +#include <linux/kernel.h>
381 +#include <linux/wait.h>
382 +#include <linux/mtd/mtd.h>
383 +#include <linux/mtd/map.h>
384 +#ifdef CONFIG_MTD_PARTITIONS
385 +#include <linux/mtd/partitions.h>
386 +#endif
387 +#include <linux/config.h>
388 +#include <linux/squashfs_fs.h>
389 +#include <linux/jffs2.h>
390 +#include <linux/crc32.h>
391 +#include <asm/io.h>
392 +
393 +#include <typedefs.h>
394 +#include <osl.h>
395 +#include <bcmnvram.h>
396 +#include <bcmutils.h>
397 +#include <sbconfig.h>
398 +#include <sbchipc.h>
399 +#include <sbutils.h>
400 +#include <trxhdr.h>
401 +
402 +/* Global SB handle */
403 +extern void *bcm947xx_sbh;
404 +extern spinlock_t bcm947xx_sbh_lock;
405 +
406 +/* Convenience */
407 +#define sbh bcm947xx_sbh
408 +#define sbh_lock bcm947xx_sbh_lock
409 +
410 +#define WINDOW_ADDR 0x1fc00000
411 +#define WINDOW_SIZE 0x400000
412 +#define BUSWIDTH 2
413 +
414 +static struct mtd_info *bcm947xx_mtd;
415 +
416 +__u8 bcm947xx_map_read8(struct map_info *map, unsigned long ofs)
417 +{
418 +       if (map->map_priv_2 == 1)
419 +               return __raw_readb(map->map_priv_1 + ofs);
420 +
421 +       u16 val = __raw_readw(map->map_priv_1 + (ofs & ~1));
422 +       if (ofs & 1)
423 +               return ((val >> 8) & 0xff);
424 +       else
425 +               return (val & 0xff);
426 +}
427 +
428 +__u16 bcm947xx_map_read16(struct map_info *map, unsigned long ofs)
429 +{
430 +       return __raw_readw(map->map_priv_1 + ofs);
431 +}
432 +
433 +__u32 bcm947xx_map_read32(struct map_info *map, unsigned long ofs)
434 +{
435 +       return __raw_readl(map->map_priv_1 + ofs);
436 +}
437 +
438 +void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
439 +{
440 +       if (len==1) {
441 +               memcpy_fromio(to, map->map_priv_1 + from, len);
442 +       } else {
443 +               int i;
444 +               u16 *dest = (u16 *) to;
445 +               u16 *src  = (u16 *) (map->map_priv_1 + from);
446 +               for (i = 0; i < (len / 2); i++) {
447 +                       dest[i] = src[i];
448 +               }
449 +               if (len & 1)
450 +                       *((u8 *)dest+len-1) = src[i] & 0xff;
451 +       }
452 +}
453 +
454 +void bcm947xx_map_write8(struct map_info *map, __u8 d, unsigned long adr)
455 +{
456 +       __raw_writeb(d, map->map_priv_1 + adr);
457 +       mb();
458 +}
459 +
460 +void bcm947xx_map_write16(struct map_info *map, __u16 d, unsigned long adr)
461 +{
462 +       __raw_writew(d, map->map_priv_1 + adr);
463 +       mb();
464 +}
465 +
466 +void bcm947xx_map_write32(struct map_info *map, __u32 d, unsigned long adr)
467 +{
468 +       __raw_writel(d, map->map_priv_1 + adr);
469 +       mb();
470 +}
471 +
472 +void bcm947xx_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
473 +{
474 +       memcpy_toio(map->map_priv_1 + to, from, len);
475 +}
476 +
477 +struct map_info bcm947xx_map = {
478 +       name: "Physically mapped flash",
479 +       size: WINDOW_SIZE,
480 +       buswidth: BUSWIDTH,
481 +       read8: bcm947xx_map_read8,
482 +       read16: bcm947xx_map_read16,
483 +       read32: bcm947xx_map_read32,
484 +       copy_from: bcm947xx_map_copy_from,
485 +       write8: bcm947xx_map_write8,
486 +       write16: bcm947xx_map_write16,
487 +       write32: bcm947xx_map_write32,
488 +       copy_to: bcm947xx_map_copy_to
489 +};
490 +
491 +#ifdef CONFIG_MTD_PARTITIONS
492 +
493 +static struct mtd_partition bcm947xx_parts[] = {
494 +       { name: "cfe",  offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
495 +       { name: "linux", offset: 0, size: 0, },
496 +       { name: "rootfs", offset: 0, size: 0, },
497 +       { name: "nvram", offset: 0, size: 0, },
498 +       { name: "OpenWrt", offset: 0, size: 0, },
499 +       { name: NULL, },
500 +};
501 +
502 +static int __init
503 +find_cfe_size(struct mtd_info *mtd, size_t size)
504 +{
505 +       struct trx_header *trx;
506 +       unsigned char buf[512];
507 +       int off;
508 +       size_t len;
509 +       int blocksize;
510 +
511 +       trx = (struct trx_header *) buf;
512 +
513 +       blocksize = mtd->erasesize;
514 +       if (blocksize < 0x10000)
515 +               blocksize = 0x10000;
516 +
517 +       for (off = (128*1024); off < size; off += blocksize) {
518 +               memset(buf, 0xe5, sizeof(buf));
519 +
520 +               /*
521 +                * Read into buffer 
522 +                */
523 +               if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
524 +                   len != sizeof(buf))
525 +                       continue;
526 +
527 +               /* found a TRX header */
528 +               if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
529 +                       goto found;
530 +               }
531 +       }
532 +
533 +       printk(KERN_NOTICE
534 +              "%s: Couldn't find bootloader size\n",
535 +              mtd->name);
536 +       return -1;
537 +
538 + found:
539 +       printk(KERN_NOTICE "bootloader size: %d\n", off);
540 +       return off;
541 +
542 +}
543 +
544 +/*
545 + * Copied from mtdblock.c
546 + *
547 + * Cache stuff...
548 + * 
549 + * Since typical flash erasable sectors are much larger than what Linux's
550 + * buffer cache can handle, we must implement read-modify-write on flash
551 + * sectors for each block write requests.  To avoid over-erasing flash sectors
552 + * and to speed things up, we locally cache a whole flash sector while it is
553 + * being written to until a different sector is required.
554 + */
555 +
556 +static void erase_callback(struct erase_info *done)
557 +{
558 +       wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
559 +       wake_up(wait_q);
560 +}
561 +
562 +static int erase_write (struct mtd_info *mtd, unsigned long pos, 
563 +                       int len, const char *buf)
564 +{
565 +       struct erase_info erase;
566 +       DECLARE_WAITQUEUE(wait, current);
567 +       wait_queue_head_t wait_q;
568 +       size_t retlen;
569 +       int ret;
570 +
571 +       /*
572 +        * First, let's erase the flash block.
573 +        */
574 +
575 +       init_waitqueue_head(&wait_q);
576 +       erase.mtd = mtd;
577 +       erase.callback = erase_callback;
578 +       erase.addr = pos;
579 +       erase.len = len;
580 +       erase.priv = (u_long)&wait_q;
581 +
582 +       set_current_state(TASK_INTERRUPTIBLE);
583 +       add_wait_queue(&wait_q, &wait);
584 +
585 +       ret = MTD_ERASE(mtd, &erase);
586 +       if (ret) {
587 +               set_current_state(TASK_RUNNING);
588 +               remove_wait_queue(&wait_q, &wait);
589 +               printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
590 +                                    "on \"%s\" failed\n",
591 +                       pos, len, mtd->name);
592 +               return ret;
593 +       }
594 +
595 +       schedule();  /* Wait for erase to finish. */
596 +       remove_wait_queue(&wait_q, &wait);
597 +
598 +       /*
599 +        * Next, writhe data to flash.
600 +        */
601 +
602 +       ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
603 +       if (ret)
604 +               return ret;
605 +       if (retlen != len)
606 +               return -EIO;
607 +       return 0;
608 +}
609 +
610 +
611 +
612 +
613 +static int __init
614 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
615 +{
616 +       struct trx_header trx, *trx2;
617 +       unsigned char buf[512], *block;
618 +       int off, blocksize;
619 +       u32 i, crc = ~0;
620 +       size_t len;
621 +       struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
622 +
623 +       blocksize = mtd->erasesize;
624 +       if (blocksize < 0x10000)
625 +               blocksize = 0x10000;
626 +
627 +       for (off = (128*1024); off < size; off += blocksize) {
628 +               memset(&trx, 0xe5, sizeof(trx));
629 +
630 +               /*
631 +                * Read into buffer 
632 +                */
633 +               if (MTD_READ(mtd, off, sizeof(trx), &len, (char *) &trx) ||
634 +                   len != sizeof(trx))
635 +                       continue;
636 +
637 +               /* found a TRX header */
638 +               if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
639 +                       part->offset = le32_to_cpu(trx.offsets[2]) ? : 
640 +                               le32_to_cpu(trx.offsets[1]);
641 +                       part->size = le32_to_cpu(trx.len); 
642 +
643 +                       part->size -= part->offset;
644 +                       part->offset += off;
645 +
646 +                       goto found;
647 +               }
648 +       }
649 +
650 +       printk(KERN_NOTICE
651 +              "%s: Couldn't find root filesystem\n",
652 +              mtd->name);
653 +       return -1;
654 +
655 + found:
656 +       if (part->size == 0)
657 +               return 0;
658 +       
659 +       if (MTD_READ(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
660 +               return 0;
661 +
662 +       if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
663 +               printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
664 +
665 +               /* Update the squashfs partition size based on the superblock info */
666 +               part->size = sb->bytes_used;
667 +               len = part->offset + part->size;
668 +               len +=  (mtd->erasesize - 1);
669 +               len &= ~(mtd->erasesize - 1);
670 +               part->size = len - part->offset;
671 +       } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
672 +               printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
673 +
674 +               /* Move the squashfs outside of the trx */
675 +               part->size = 0;
676 +       } else {
677 +               printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
678 +               return 0;
679 +       }
680 +
681 +       if (trx.len != part->offset + part->size - off) {
682 +               /* Update the trx offsets and length */
683 +               trx.len = part->offset + part->size - off;
684 +       
685 +               /* Update the trx crc32 */
686 +               for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
687 +                       if (MTD_READ(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
688 +                               return 0;
689 +                       crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
690 +               }
691 +               trx.crc32 = crc;
692 +
693 +               /* read first eraseblock from the trx */
694 +               trx2 = block = kmalloc(mtd->erasesize, GFP_KERNEL);
695 +               if (MTD_READ(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
696 +                       printk("Error accessing the first trx eraseblock\n");
697 +                       return 0;
698 +               }
699 +               
700 +               printk("Updating TRX offsets and length:\n");
701 +               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);
702 +               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);
703 +
704 +               /* Write updated trx header to the flash */
705 +               memcpy(block, &trx, sizeof(trx));
706 +               if (mtd->unlock)
707 +                       mtd->unlock(mtd, off, mtd->erasesize);
708 +               erase_write(mtd, off, mtd->erasesize, block);
709 +               if (mtd->sync)
710 +                       mtd->sync(mtd);
711 +               kfree(block);
712 +               printk("Done\n");
713 +       }
714 +       
715 +       return part->size;
716 +}
717 +
718 +struct mtd_partition * __init
719 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
720 +{
721 +       int cfe_size;
722 +
723 +       cfe_size = find_cfe_size(mtd,size);
724 +
725 +       /* boot loader */
726 +       bcm947xx_parts[0].offset = 0;
727 +       bcm947xx_parts[0].size   = cfe_size;
728 +
729 +       /* nvram */
730 +       if (cfe_size != 384 * 1024) {
731 +               bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
732 +               bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
733 +       } else {
734 +               /* nvram (old 128kb config partition on netgear wgt634u) */
735 +               bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
736 +               bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
737 +       }
738 +
739 +       /* linux (kernel and rootfs) */
740 +       if (cfe_size != 384 * 1024) {
741 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
742 +               bcm947xx_parts[1].size   = bcm947xx_parts[3].offset - 
743 +                       bcm947xx_parts[1].offset;
744 +       } else {
745 +               /* do not count the elf loader, which is on one block */
746 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size + 
747 +                       bcm947xx_parts[3].size + mtd->erasesize;
748 +               bcm947xx_parts[1].size   = size - 
749 +                       bcm947xx_parts[0].size - 
750 +                       (2*bcm947xx_parts[3].size) - 
751 +                       mtd->erasesize;
752 +       }
753 +
754 +       /* find and size rootfs */
755 +       if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
756 +               /* entirely jffs2 */
757 +               bcm947xx_parts[4].name = NULL;
758 +               bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - 
759 +                               bcm947xx_parts[3].size;
760 +       } else {
761 +               /* legacy setup */
762 +               /* calculate leftover flash, and assign it to the jffs2 partition */
763 +               if (cfe_size != 384 * 1024) {
764 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
765 +                               bcm947xx_parts[2].size;
766 +                       if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
767 +                               bcm947xx_parts[4].offset += mtd->erasesize - 
768 +                                       (bcm947xx_parts[4].offset % mtd->erasesize);
769 +                       }
770 +                       bcm947xx_parts[4].size = bcm947xx_parts[3].offset - 
771 +                               bcm947xx_parts[4].offset;
772 +               } else {
773 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
774 +                               bcm947xx_parts[2].size;
775 +                       if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
776 +                               bcm947xx_parts[4].offset += mtd->erasesize - 
777 +                                       (bcm947xx_parts[4].offset % mtd->erasesize);
778 +                       }
779 +                       bcm947xx_parts[4].size = size - bcm947xx_parts[3].size - 
780 +                               bcm947xx_parts[4].offset;
781 +               }
782 +       }
783 +
784 +       return bcm947xx_parts;
785 +}
786 +
787 +#endif
788 +
789 +
790 +mod_init_t init_bcm947xx_map(void)
791 +{
792 +       ulong flags;
793 +       uint coreidx;
794 +       chipcregs_t *cc;
795 +       uint32 fltype;
796 +       uint window_addr = 0, window_size = 0;
797 +       size_t size;
798 +       int ret = 0;
799 +#ifdef CONFIG_MTD_PARTITIONS
800 +       struct mtd_partition *parts;
801 +       int i;
802 +#endif
803 +
804 +       spin_lock_irqsave(&sbh_lock, flags);
805 +       coreidx = sb_coreidx(sbh);
806 +
807 +       /* Check strapping option if chipcommon exists */
808 +       if ((cc = sb_setcore(sbh, SB_CC, 0))) {
809 +               fltype = readl(&cc->capabilities) & CAP_FLASH_MASK;
810 +               if (fltype == PFLASH) {
811 +                       bcm947xx_map.map_priv_2 = 1;
812 +                       window_addr = 0x1c000000;
813 +                       bcm947xx_map.size = window_size = 32 * 1024 * 1024;
814 +                       if ((readl(&cc->flash_config) & CC_CFG_DS) == 0)
815 +                               bcm947xx_map.buswidth = 1;
816 +               }
817 +       } else {
818 +               fltype = PFLASH;
819 +               bcm947xx_map.map_priv_2 = 0;
820 +               window_addr = WINDOW_ADDR;
821 +               window_size = WINDOW_SIZE;
822 +       }
823 +
824 +       sb_setcoreidx(sbh, coreidx);
825 +       spin_unlock_irqrestore(&sbh_lock, flags);
826 +
827 +       if (fltype != PFLASH) {
828 +               printk(KERN_ERR "pflash: found no supported devices\n");
829 +               ret = -ENODEV;
830 +               goto fail;
831 +       }
832 +
833 +       bcm947xx_map.map_priv_1 = (unsigned long) ioremap(window_addr, window_size);
834 +
835 +       if (!bcm947xx_map.map_priv_1) {
836 +               printk(KERN_ERR "Failed to ioremap\n");
837 +               return -EIO;
838 +       }
839 +
840 +       if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
841 +               printk(KERN_ERR "pflash: cfi_probe failed\n");
842 +               iounmap((void *)bcm947xx_map.map_priv_1);
843 +               return -ENXIO;
844 +       }
845 +
846 +       bcm947xx_mtd->module = THIS_MODULE;
847 +
848 +       size = bcm947xx_mtd->size;
849 +
850 +       printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, window_addr);
851 +
852 +#ifdef CONFIG_MTD_PARTITIONS
853 +       parts = init_mtd_partitions(bcm947xx_mtd, size);
854 +       for (i = 0; parts[i].name; i++);
855 +       ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
856 +       if (ret) {
857 +               printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
858 +               goto fail;
859 +       }
860 +#endif
861 +
862 +       return 0;
863 +
864 + fail:
865 +       if (bcm947xx_mtd)
866 +               map_destroy(bcm947xx_mtd);
867 +       if (bcm947xx_map.map_priv_1)
868 +               iounmap((void *) bcm947xx_map.map_priv_1);
869 +       bcm947xx_map.map_priv_1 = 0;
870 +       return ret;
871 +}
872 +
873 +mod_exit_t cleanup_bcm947xx_map(void)
874 +{
875 +#ifdef CONFIG_MTD_PARTITIONS
876 +       del_mtd_partitions(bcm947xx_mtd);
877 +#endif
878 +       map_destroy(bcm947xx_mtd);
879 +       iounmap((void *) bcm947xx_map.map_priv_1);
880 +       bcm947xx_map.map_priv_1 = 0;
881 +}
882 +
883 +module_init(init_bcm947xx_map);
884 +module_exit(cleanup_bcm947xx_map);
885 diff -urN linux.old/drivers/mtd/maps/Config.in linux.dev/drivers/mtd/maps/Config.in
886 --- linux.old/drivers/mtd/maps/Config.in        2006-06-22 17:35:39.000000000 +0200
887 +++ linux.dev/drivers/mtd/maps/Config.in        2006-06-21 21:41:24.000000000 +0200
888 @@ -48,6 +48,7 @@
889  fi
890  
891  if [ "$CONFIG_MIPS" = "y" ]; then
892 +   dep_tristate '  CFI Flash device mapped on Broadcom BCM947XX boards' CONFIG_MTD_BCM947XX $CONFIG_MTD_CFI
893     dep_tristate '  Pb1000 MTD support' CONFIG_MTD_PB1000 $CONFIG_MIPS_PB1000
894     dep_tristate '  Pb1500 MTD support' CONFIG_MTD_PB1500 $CONFIG_MIPS_PB1500
895     dep_tristate '  Pb1100 MTD support' CONFIG_MTD_PB1100 $CONFIG_MIPS_PB1100
896 diff -urN linux.old/drivers/mtd/maps/Makefile linux.dev/drivers/mtd/maps/Makefile
897 --- linux.old/drivers/mtd/maps/Makefile 2006-06-22 17:35:39.000000000 +0200
898 +++ linux.dev/drivers/mtd/maps/Makefile 2006-06-21 21:41:24.000000000 +0200
899 @@ -3,6 +3,8 @@
900  #
901  # $Id: Makefile,v 1.37 2003/01/24 14:26:38 dwmw2 Exp $
902  
903 +EXTRA_CFLAGS := -I$(TOPDIR)/arch/mips/bcm947xx/include
904 +
905  BELOW25                := $(shell echo $(PATCHLEVEL) | sed s/[1234]/y/)
906  
907  ifeq ($(BELOW25),y)
908 @@ -10,6 +12,7 @@
909  endif
910  
911  # Chip mappings
912 +obj-$(CONFIG_MTD_BCM947XX)     += bcm947xx-flash.o
913  obj-$(CONFIG_MTD_CDB89712)     += cdb89712.o
914  obj-$(CONFIG_MTD_ARM_INTEGRATOR)+= integrator-flash.o
915  obj-$(CONFIG_MTD_CFI_FLAGADM)  += cfi_flagadm.o