broadcom 2.4: update the trx for jffs2 directly in the flash map driver
[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,544 @@
338 +/*
339 + *  Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
340 + *  Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
341 + *
342 + *  original functions for finding root filesystem from Mike Baker 
343 + *
344 + *  This program is free software; you can redistribute  it and/or modify it
345 + *  under  the terms of  the GNU General  Public License as published by the
346 + *  Free Software Foundation;  either version 2 of the  License, or (at your
347 + *  option) any later version.
348 + *
349 + *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
350 + *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
351 + *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
352 + *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
353 + *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
354 + *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
355 + *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
356 + *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
357 + *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
358 + *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
359 + *
360 + *  You should have received a copy of the  GNU General Public License along
361 + *  with this program; if not, write  to the Free Software Foundation, Inc.,
362 + *  675 Mass Ave, Cambridge, MA 02139, USA.
363 + * 
364 + *
365 + * Copyright 2004, Broadcom Corporation
366 + * All Rights Reserved.
367 + * 
368 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
369 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
370 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
371 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
372 + *
373 + * Flash mapping for BCM947XX boards
374 + *
375 + */
376 +
377 +#include <linux/module.h>
378 +#include <linux/types.h>
379 +#include <linux/kernel.h>
380 +#include <linux/wait.h>
381 +#include <linux/mtd/mtd.h>
382 +#include <linux/mtd/map.h>
383 +#ifdef CONFIG_MTD_PARTITIONS
384 +#include <linux/mtd/partitions.h>
385 +#endif
386 +#include <linux/config.h>
387 +#include <linux/squashfs_fs.h>
388 +#include <linux/jffs2.h>
389 +#include <linux/crc32.h>
390 +#include <asm/io.h>
391 +
392 +#include <typedefs.h>
393 +#include <osl.h>
394 +#include <bcmnvram.h>
395 +#include <bcmutils.h>
396 +#include <sbconfig.h>
397 +#include <sbchipc.h>
398 +#include <sbutils.h>
399 +#include <trxhdr.h>
400 +
401 +/* Global SB handle */
402 +extern void *bcm947xx_sbh;
403 +extern spinlock_t bcm947xx_sbh_lock;
404 +
405 +/* Convenience */
406 +#define sbh bcm947xx_sbh
407 +#define sbh_lock bcm947xx_sbh_lock
408 +
409 +#define WINDOW_ADDR 0x1fc00000
410 +#define WINDOW_SIZE 0x400000
411 +#define BUSWIDTH 2
412 +
413 +static struct mtd_info *bcm947xx_mtd;
414 +
415 +__u8 bcm947xx_map_read8(struct map_info *map, unsigned long ofs)
416 +{
417 +       if (map->map_priv_2 == 1)
418 +               return __raw_readb(map->map_priv_1 + ofs);
419 +
420 +       u16 val = __raw_readw(map->map_priv_1 + (ofs & ~1));
421 +       if (ofs & 1)
422 +               return ((val >> 8) & 0xff);
423 +       else
424 +               return (val & 0xff);
425 +}
426 +
427 +__u16 bcm947xx_map_read16(struct map_info *map, unsigned long ofs)
428 +{
429 +       return __raw_readw(map->map_priv_1 + ofs);
430 +}
431 +
432 +__u32 bcm947xx_map_read32(struct map_info *map, unsigned long ofs)
433 +{
434 +       return __raw_readl(map->map_priv_1 + ofs);
435 +}
436 +
437 +void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
438 +{
439 +       if (len==1) {
440 +               memcpy_fromio(to, map->map_priv_1 + from, len);
441 +       } else {
442 +               int i;
443 +               u16 *dest = (u16 *) to;
444 +               u16 *src  = (u16 *) (map->map_priv_1 + from);
445 +               for (i = 0; i < (len / 2); i++) {
446 +                       dest[i] = src[i];
447 +               }
448 +               if (len & 1)
449 +                       *((u8 *)dest+len-1) = src[i] & 0xff;
450 +       }
451 +}
452 +
453 +void bcm947xx_map_write8(struct map_info *map, __u8 d, unsigned long adr)
454 +{
455 +       __raw_writeb(d, map->map_priv_1 + adr);
456 +       mb();
457 +}
458 +
459 +void bcm947xx_map_write16(struct map_info *map, __u16 d, unsigned long adr)
460 +{
461 +       __raw_writew(d, map->map_priv_1 + adr);
462 +       mb();
463 +}
464 +
465 +void bcm947xx_map_write32(struct map_info *map, __u32 d, unsigned long adr)
466 +{
467 +       __raw_writel(d, map->map_priv_1 + adr);
468 +       mb();
469 +}
470 +
471 +void bcm947xx_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
472 +{
473 +       memcpy_toio(map->map_priv_1 + to, from, len);
474 +}
475 +
476 +struct map_info bcm947xx_map = {
477 +       name: "Physically mapped flash",
478 +       size: WINDOW_SIZE,
479 +       buswidth: BUSWIDTH,
480 +       read8: bcm947xx_map_read8,
481 +       read16: bcm947xx_map_read16,
482 +       read32: bcm947xx_map_read32,
483 +       copy_from: bcm947xx_map_copy_from,
484 +       write8: bcm947xx_map_write8,
485 +       write16: bcm947xx_map_write16,
486 +       write32: bcm947xx_map_write32,
487 +       copy_to: bcm947xx_map_copy_to
488 +};
489 +
490 +#ifdef CONFIG_MTD_PARTITIONS
491 +
492 +static struct mtd_partition bcm947xx_parts[] = {
493 +       { name: "cfe",  offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
494 +       { name: "linux", offset: 0, size: 0, },
495 +       { name: "rootfs", offset: 0, size: 0, },
496 +       { name: "nvram", offset: 0, size: 0, },
497 +       { name: "OpenWrt", offset: 0, size: 0, },
498 +       { name: NULL, },
499 +};
500 +
501 +static int __init
502 +find_cfe_size(struct mtd_info *mtd, size_t size)
503 +{
504 +       struct trx_header *trx;
505 +       unsigned char buf[512];
506 +       int off;
507 +       size_t len;
508 +       int blocksize;
509 +
510 +       trx = (struct trx_header *) buf;
511 +
512 +       blocksize = mtd->erasesize;
513 +       if (blocksize < 0x10000)
514 +               blocksize = 0x10000;
515 +
516 +       for (off = (128*1024); off < size; off += blocksize) {
517 +               memset(buf, 0xe5, sizeof(buf));
518 +
519 +               /*
520 +                * Read into buffer 
521 +                */
522 +               if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
523 +                   len != sizeof(buf))
524 +                       continue;
525 +
526 +               /* found a TRX header */
527 +               if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
528 +                       goto found;
529 +               }
530 +       }
531 +
532 +       printk(KERN_NOTICE
533 +              "%s: Couldn't find bootloader size\n",
534 +              mtd->name);
535 +       return -1;
536 +
537 + found:
538 +       printk(KERN_NOTICE "bootloader size: %d\n", off);
539 +       return off;
540 +
541 +}
542 +
543 +/*
544 + * Copied from mtdblock.c
545 + *
546 + * Cache stuff...
547 + * 
548 + * Since typical flash erasable sectors are much larger than what Linux's
549 + * buffer cache can handle, we must implement read-modify-write on flash
550 + * sectors for each block write requests.  To avoid over-erasing flash sectors
551 + * and to speed things up, we locally cache a whole flash sector while it is
552 + * being written to until a different sector is required.
553 + */
554 +
555 +static void erase_callback(struct erase_info *done)
556 +{
557 +       wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
558 +       wake_up(wait_q);
559 +}
560 +
561 +static int erase_write (struct mtd_info *mtd, unsigned long pos, 
562 +                       int len, const char *buf)
563 +{
564 +       struct erase_info erase;
565 +       DECLARE_WAITQUEUE(wait, current);
566 +       wait_queue_head_t wait_q;
567 +       size_t retlen;
568 +       int ret;
569 +
570 +       /*
571 +        * First, let's erase the flash block.
572 +        */
573 +
574 +       init_waitqueue_head(&wait_q);
575 +       erase.mtd = mtd;
576 +       erase.callback = erase_callback;
577 +       erase.addr = pos;
578 +       erase.len = len;
579 +       erase.priv = (u_long)&wait_q;
580 +
581 +       set_current_state(TASK_INTERRUPTIBLE);
582 +       add_wait_queue(&wait_q, &wait);
583 +
584 +       ret = MTD_ERASE(mtd, &erase);
585 +       if (ret) {
586 +               set_current_state(TASK_RUNNING);
587 +               remove_wait_queue(&wait_q, &wait);
588 +               printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
589 +                                    "on \"%s\" failed\n",
590 +                       pos, len, mtd->name);
591 +               return ret;
592 +       }
593 +
594 +       schedule();  /* Wait for erase to finish. */
595 +       remove_wait_queue(&wait_q, &wait);
596 +
597 +       /*
598 +        * Next, writhe data to flash.
599 +        */
600 +
601 +       ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
602 +       if (ret)
603 +               return ret;
604 +       if (retlen != len)
605 +               return -EIO;
606 +       return 0;
607 +}
608 +
609 +
610 +
611 +
612 +static int __init
613 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
614 +{
615 +       struct trx_header trx, *trx2;
616 +       unsigned char buf[512], *block;
617 +       int off, blocksize;
618 +       u32 i, crc = ~0;
619 +       size_t len;
620 +       struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
621 +
622 +       blocksize = mtd->erasesize;
623 +       if (blocksize < 0x10000)
624 +               blocksize = 0x10000;
625 +
626 +       for (off = (128*1024); off < size; off += blocksize) {
627 +               memset(&trx, 0xe5, sizeof(trx));
628 +
629 +               /*
630 +                * Read into buffer 
631 +                */
632 +               if (MTD_READ(mtd, off, sizeof(trx), &len, (char *) &trx) ||
633 +                   len != sizeof(trx))
634 +                       continue;
635 +
636 +               /* found a TRX header */
637 +               if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
638 +                       part->offset = le32_to_cpu(trx.offsets[2]) ? : 
639 +                               le32_to_cpu(trx.offsets[1]);
640 +                       part->size = le32_to_cpu(trx.len); 
641 +
642 +                       part->size -= part->offset;
643 +                       part->offset += off;
644 +
645 +                       goto found;
646 +               }
647 +       }
648 +
649 +       printk(KERN_NOTICE
650 +              "%s: Couldn't find root filesystem\n",
651 +              mtd->name);
652 +       return -1;
653 +
654 + found:
655 +       if (part->size == 0)
656 +               return 0;
657 +       
658 +       if (MTD_READ(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
659 +               return 0;
660 +
661 +       if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
662 +               printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
663 +
664 +               /* Update the squashfs partition size based on the superblock info */
665 +               part->size = sb->bytes_used;
666 +               part->size +=  (mtd->erasesize - 1);
667 +               part->size &= ~(mtd->erasesize - 1);
668 +       } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
669 +               printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
670 +
671 +               /* Move the squashfs outside of the trx */
672 +               part->size = 0;
673 +       } else {
674 +               printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
675 +               return 0;
676 +       }
677 +
678 +       if (trx.len != part->offset + part->size - off) {
679 +               /* Update the trx offsets and length */
680 +               trx.len = part->offset + part->size - off;
681 +       
682 +               /* Update the trx crc32 */
683 +               for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
684 +                       if (MTD_READ(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
685 +                               return 0;
686 +                       crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
687 +               }
688 +               trx.crc32 = crc;
689 +
690 +               /* read first eraseblock from the trx */
691 +               trx2 = block = kmalloc(mtd->erasesize, GFP_KERNEL);
692 +               if (MTD_READ(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
693 +                       printk("Error accessing the first trx eraseblock\n");
694 +                       return 0;
695 +               }
696 +               
697 +               printk("Updating TRX offsets and length:\n");
698 +               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);
699 +               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);
700 +
701 +               /* Write updated trx header to the flash */
702 +               memcpy(block, &trx, sizeof(trx));
703 +               if (mtd->unlock)
704 +                       mtd->unlock(mtd, off, mtd->erasesize);
705 +               erase_write(mtd, off, mtd->erasesize, block);
706 +               if (mtd->sync)
707 +                       mtd->sync(mtd);
708 +               kfree(block);
709 +               printk("Done\n");
710 +       }
711 +       
712 +       return part->size;
713 +}
714 +
715 +struct mtd_partition * __init
716 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
717 +{
718 +       int cfe_size;
719 +
720 +       cfe_size = find_cfe_size(mtd,size);
721 +
722 +       /* boot loader */
723 +       bcm947xx_parts[0].offset = 0;
724 +       bcm947xx_parts[0].size   = cfe_size;
725 +
726 +       /* nvram */
727 +       if (cfe_size != 384 * 1024) {
728 +               bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
729 +               bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
730 +       } else {
731 +               /* nvram (old 128kb config partition on netgear wgt634u) */
732 +               bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
733 +               bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
734 +       }
735 +
736 +       /* linux (kernel and rootfs) */
737 +       if (cfe_size != 384 * 1024) {
738 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
739 +               bcm947xx_parts[1].size   = bcm947xx_parts[3].offset - 
740 +                       bcm947xx_parts[1].offset;
741 +       } else {
742 +               /* do not count the elf loader, which is on one block */
743 +               bcm947xx_parts[1].offset = bcm947xx_parts[0].size + 
744 +                       bcm947xx_parts[3].size + mtd->erasesize;
745 +               bcm947xx_parts[1].size   = size - 
746 +                       bcm947xx_parts[0].size - 
747 +                       (2*bcm947xx_parts[3].size) - 
748 +                       mtd->erasesize;
749 +       }
750 +
751 +       /* find and size rootfs */
752 +       if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
753 +               /* entirely jffs2 */
754 +               bcm947xx_parts[4].name = NULL;
755 +               bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - 
756 +                               bcm947xx_parts[3].size;
757 +       } else {
758 +               /* legacy setup */
759 +               /* calculate leftover flash, and assign it to the jffs2 partition */
760 +               if (cfe_size != 384 * 1024) {
761 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
762 +                               bcm947xx_parts[2].size;
763 +                       if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
764 +                               bcm947xx_parts[4].offset += mtd->erasesize - 
765 +                                       (bcm947xx_parts[4].offset % mtd->erasesize);
766 +                       }
767 +                       bcm947xx_parts[4].size = bcm947xx_parts[3].offset - 
768 +                               bcm947xx_parts[4].offset;
769 +               } else {
770 +                       bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
771 +                               bcm947xx_parts[2].size;
772 +                       if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
773 +                               bcm947xx_parts[4].offset += mtd->erasesize - 
774 +                                       (bcm947xx_parts[4].offset % mtd->erasesize);
775 +                       }
776 +                       bcm947xx_parts[4].size = size - bcm947xx_parts[3].size - 
777 +                               bcm947xx_parts[4].offset;
778 +               }
779 +       }
780 +
781 +       return bcm947xx_parts;
782 +}
783 +
784 +#endif
785 +
786 +
787 +mod_init_t init_bcm947xx_map(void)
788 +{
789 +       ulong flags;
790 +       uint coreidx;
791 +       chipcregs_t *cc;
792 +       uint32 fltype;
793 +       uint window_addr = 0, window_size = 0;
794 +       size_t size;
795 +       int ret = 0;
796 +#ifdef CONFIG_MTD_PARTITIONS
797 +       struct mtd_partition *parts;
798 +       int i;
799 +#endif
800 +
801 +       spin_lock_irqsave(&sbh_lock, flags);
802 +       coreidx = sb_coreidx(sbh);
803 +
804 +       /* Check strapping option if chipcommon exists */
805 +       if ((cc = sb_setcore(sbh, SB_CC, 0))) {
806 +               fltype = readl(&cc->capabilities) & CAP_FLASH_MASK;
807 +               if (fltype == PFLASH) {
808 +                       bcm947xx_map.map_priv_2 = 1;
809 +                       window_addr = 0x1c000000;
810 +                       bcm947xx_map.size = window_size = 32 * 1024 * 1024;
811 +                       if ((readl(&cc->flash_config) & CC_CFG_DS) == 0)
812 +                               bcm947xx_map.buswidth = 1;
813 +               }
814 +       } else {
815 +               fltype = PFLASH;
816 +               bcm947xx_map.map_priv_2 = 0;
817 +               window_addr = WINDOW_ADDR;
818 +               window_size = WINDOW_SIZE;
819 +       }
820 +
821 +       sb_setcoreidx(sbh, coreidx);
822 +       spin_unlock_irqrestore(&sbh_lock, flags);
823 +
824 +       if (fltype != PFLASH) {
825 +               printk(KERN_ERR "pflash: found no supported devices\n");
826 +               ret = -ENODEV;
827 +               goto fail;
828 +       }
829 +
830 +       bcm947xx_map.map_priv_1 = (unsigned long) ioremap(window_addr, window_size);
831 +
832 +       if (!bcm947xx_map.map_priv_1) {
833 +               printk(KERN_ERR "Failed to ioremap\n");
834 +               return -EIO;
835 +       }
836 +
837 +       if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
838 +               printk(KERN_ERR "pflash: cfi_probe failed\n");
839 +               iounmap((void *)bcm947xx_map.map_priv_1);
840 +               return -ENXIO;
841 +       }
842 +
843 +       bcm947xx_mtd->module = THIS_MODULE;
844 +
845 +       size = bcm947xx_mtd->size;
846 +
847 +       printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, window_addr);
848 +
849 +#ifdef CONFIG_MTD_PARTITIONS
850 +       parts = init_mtd_partitions(bcm947xx_mtd, size);
851 +       for (i = 0; parts[i].name; i++);
852 +       ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
853 +       if (ret) {
854 +               printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
855 +               goto fail;
856 +       }
857 +#endif
858 +
859 +       return 0;
860 +
861 + fail:
862 +       if (bcm947xx_mtd)
863 +               map_destroy(bcm947xx_mtd);
864 +       if (bcm947xx_map.map_priv_1)
865 +               iounmap((void *) bcm947xx_map.map_priv_1);
866 +       bcm947xx_map.map_priv_1 = 0;
867 +       return ret;
868 +}
869 +
870 +mod_exit_t cleanup_bcm947xx_map(void)
871 +{
872 +#ifdef CONFIG_MTD_PARTITIONS
873 +       del_mtd_partitions(bcm947xx_mtd);
874 +#endif
875 +       map_destroy(bcm947xx_mtd);
876 +       iounmap((void *) bcm947xx_map.map_priv_1);
877 +       bcm947xx_map.map_priv_1 = 0;
878 +}
879 +
880 +module_init(init_bcm947xx_map);
881 +module_exit(cleanup_bcm947xx_map);
882 diff -urN linux.old/drivers/mtd/maps/Config.in linux.dev/drivers/mtd/maps/Config.in
883 --- linux.old/drivers/mtd/maps/Config.in        2006-06-22 17:35:39.000000000 +0200
884 +++ linux.dev/drivers/mtd/maps/Config.in        2006-06-21 21:41:24.000000000 +0200
885 @@ -48,6 +48,7 @@
886  fi
887  
888  if [ "$CONFIG_MIPS" = "y" ]; then
889 +   dep_tristate '  CFI Flash device mapped on Broadcom BCM947XX boards' CONFIG_MTD_BCM947XX $CONFIG_MTD_CFI
890     dep_tristate '  Pb1000 MTD support' CONFIG_MTD_PB1000 $CONFIG_MIPS_PB1000
891     dep_tristate '  Pb1500 MTD support' CONFIG_MTD_PB1500 $CONFIG_MIPS_PB1500
892     dep_tristate '  Pb1100 MTD support' CONFIG_MTD_PB1100 $CONFIG_MIPS_PB1100
893 diff -urN linux.old/drivers/mtd/maps/Makefile linux.dev/drivers/mtd/maps/Makefile
894 --- linux.old/drivers/mtd/maps/Makefile 2006-06-22 17:35:39.000000000 +0200
895 +++ linux.dev/drivers/mtd/maps/Makefile 2006-06-21 21:41:24.000000000 +0200
896 @@ -3,6 +3,8 @@
897  #
898  # $Id: Makefile,v 1.37 2003/01/24 14:26:38 dwmw2 Exp $
899  
900 +EXTRA_CFLAGS := -I$(TOPDIR)/arch/mips/bcm947xx/include
901 +
902  BELOW25                := $(shell echo $(PATCHLEVEL) | sed s/[1234]/y/)
903  
904  ifeq ($(BELOW25),y)
905 @@ -10,6 +12,7 @@
906  endif
907  
908  # Chip mappings
909 +obj-$(CONFIG_MTD_BCM947XX)     += bcm947xx-flash.o
910  obj-$(CONFIG_MTD_CDB89712)     += cdb89712.o
911  obj-$(CONFIG_MTD_ARM_INTEGRATOR)+= integrator-flash.o
912  obj-$(CONFIG_MTD_CFI_FLAGADM)  += cfi_flagadm.o