add reboot fix from #1312
[openwrt/svn-archive/archive.git] / target / linux / brcm-2.6 / patches / 002-flash-map.patch
1 diff -urN linux-2.6.19.ref/drivers/mtd/maps/bcm47xx-flash.c linux-2.6.19/drivers/mtd/maps/bcm47xx-flash.c
2 --- linux-2.6.19.ref/drivers/mtd/maps/bcm47xx-flash.c 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.19/drivers/mtd/maps/bcm47xx-flash.c 2006-12-04 21:33:58.000000000 +0100
4 @@ -0,0 +1,490 @@
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/autoconf.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 <typedefs.h>
60 +#include <osl.h>
61 +#include <bcmnvram.h>
62 +#include <bcmutils.h>
63 +#include <sbconfig.h>
64 +#include <sbchipc.h>
65 +#include <sbutils.h>
66 +#include <trxhdr.h>
67 +
68 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
69 +#define NVRAM_SPACE 0x8000
70 +#define WINDOW_ADDR 0x1fc00000
71 +#define WINDOW_SIZE 0x400000
72 +#define BUSWIDTH 2
73 +
74 +extern sb_t *sbh;
75 +static struct mtd_info *bcm947xx_mtd;
76 +
77 +static void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
78 +{
79 +#define MIPS_MEMCPY_ALIGN 4
80 + map_word ret;
81 + ssize_t transfer;
82 + ssize_t done = 0;
83 + if ((len >= MIPS_MEMCPY_ALIGN) && (!(from & (MIPS_MEMCPY_ALIGN - 1))) && (!(((unsigned int)to & (MIPS_MEMCPY_ALIGN - 1))))) {
84 + done = len & ~(MIPS_MEMCPY_ALIGN - 1);
85 + memcpy_fromio(to, map->virt + from, done);
86 + }
87 + while (done < len) {
88 + ret = map->read(map, from + done);
89 + transfer = len - done;
90 + if (transfer > map->bankwidth)
91 + transfer = map->bankwidth;
92 + memcpy((void *)((unsigned long)to + done), &ret.x[0], transfer);
93 + done += transfer;
94 + }
95 +}
96 +
97 +static struct map_info bcm947xx_map = {
98 + name: "Physically mapped flash",
99 + size: WINDOW_SIZE,
100 + bankwidth: BUSWIDTH,
101 + phys: WINDOW_ADDR,
102 +};
103 +
104 +#ifdef CONFIG_MTD_PARTITIONS
105 +
106 +static struct mtd_partition bcm947xx_parts[] = {
107 + { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
108 + { name: "linux", offset: 0, size: 0, },
109 + { name: "rootfs", offset: 0, size: 0, },
110 + { name: "nvram", offset: 0, size: 0, },
111 + { name: "OpenWrt", offset: 0, size: 0, },
112 + { name: NULL, },
113 +};
114 +
115 +static int __init
116 +find_cfe_size(struct mtd_info *mtd, size_t size)
117 +{
118 + struct trx_header *trx;
119 + unsigned char buf[512];
120 + int off;
121 + size_t len;
122 + int blocksize;
123 +
124 + trx = (struct trx_header *) buf;
125 +
126 + blocksize = mtd->erasesize;
127 + if (blocksize < 0x10000)
128 + blocksize = 0x10000;
129 +
130 + for (off = (128*1024); off < size; off += blocksize) {
131 + memset(buf, 0xe5, sizeof(buf));
132 +
133 + /*
134 + * Read into buffer
135 + */
136 + if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
137 + len != sizeof(buf))
138 + continue;
139 +
140 + /* found a TRX header */
141 + if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
142 + goto found;
143 + }
144 + }
145 +
146 + printk(KERN_NOTICE
147 + "%s: Couldn't find bootloader size\n",
148 + mtd->name);
149 + return -1;
150 +
151 + found:
152 + printk(KERN_NOTICE "bootloader size: %d\n", off);
153 + return off;
154 +
155 +}
156 +
157 +/*
158 + * Copied from mtdblock.c
159 + *
160 + * Cache stuff...
161 + *
162 + * Since typical flash erasable sectors are much larger than what Linux's
163 + * buffer cache can handle, we must implement read-modify-write on flash
164 + * sectors for each block write requests. To avoid over-erasing flash sectors
165 + * and to speed things up, we locally cache a whole flash sector while it is
166 + * being written to until a different sector is required.
167 + */
168 +
169 +static void erase_callback(struct erase_info *done)
170 +{
171 + wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
172 + wake_up(wait_q);
173 +}
174 +
175 +static int erase_write (struct mtd_info *mtd, unsigned long pos,
176 + int len, const char *buf)
177 +{
178 + struct erase_info erase;
179 + DECLARE_WAITQUEUE(wait, current);
180 + wait_queue_head_t wait_q;
181 + size_t retlen;
182 + int ret;
183 +
184 + /*
185 + * First, let's erase the flash block.
186 + */
187 +
188 + init_waitqueue_head(&wait_q);
189 + erase.mtd = mtd;
190 + erase.callback = erase_callback;
191 + erase.addr = pos;
192 + erase.len = len;
193 + erase.priv = (u_long)&wait_q;
194 +
195 + set_current_state(TASK_INTERRUPTIBLE);
196 + add_wait_queue(&wait_q, &wait);
197 +
198 + ret = mtd->erase(mtd, &erase);
199 + if (ret) {
200 + set_current_state(TASK_RUNNING);
201 + remove_wait_queue(&wait_q, &wait);
202 + printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
203 + "on \"%s\" failed\n",
204 + pos, len, mtd->name);
205 + return ret;
206 + }
207 +
208 + schedule(); /* Wait for erase to finish. */
209 + remove_wait_queue(&wait_q, &wait);
210 +
211 + /*
212 + * Next, writhe data to flash.
213 + */
214 +
215 + ret = mtd->write(mtd, pos, len, &retlen, buf);
216 + if (ret)
217 + return ret;
218 + if (retlen != len)
219 + return -EIO;
220 + return 0;
221 +}
222 +
223 +
224 +
225 +
226 +static int __init
227 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
228 +{
229 + struct trx_header trx, *trx2;
230 + unsigned char buf[512], *block;
231 + int off, blocksize;
232 + u32 i, crc = ~0;
233 + size_t len;
234 + struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
235 +
236 + blocksize = mtd->erasesize;
237 + if (blocksize < 0x10000)
238 + blocksize = 0x10000;
239 +
240 + for (off = (128*1024); off < size; off += blocksize) {
241 + memset(&trx, 0xe5, sizeof(trx));
242 +
243 + /*
244 + * Read into buffer
245 + */
246 + if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
247 + len != sizeof(trx))
248 + continue;
249 +
250 + /* found a TRX header */
251 + if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
252 + part->offset = le32_to_cpu(trx.offsets[2]) ? :
253 + le32_to_cpu(trx.offsets[1]);
254 + part->size = le32_to_cpu(trx.len);
255 +
256 + part->size -= part->offset;
257 + part->offset += off;
258 +
259 + goto found;
260 + }
261 + }
262 +
263 + printk(KERN_NOTICE
264 + "%s: Couldn't find root filesystem\n",
265 + mtd->name);
266 + return -1;
267 +
268 + found:
269 + if (part->size == 0)
270 + return 0;
271 +
272 + if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
273 + return 0;
274 +
275 + if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
276 + printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
277 +
278 + /* Update the squashfs partition size based on the superblock info */
279 + part->size = sb->bytes_used;
280 + len = part->offset + part->size;
281 + len += (mtd->erasesize - 1);
282 + len &= ~(mtd->erasesize - 1);
283 + part->size = len - part->offset;
284 + } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
285 + printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
286 +
287 + /* Move the squashfs outside of the trx */
288 + part->size = 0;
289 + } else {
290 + printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
291 + return 0;
292 + }
293 +
294 + if (trx.len != part->offset + part->size - off) {
295 + /* Update the trx offsets and length */
296 + trx.len = part->offset + part->size - off;
297 +
298 + /* Update the trx crc32 */
299 + for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
300 + if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
301 + return 0;
302 + crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
303 + }
304 + trx.crc32 = crc;
305 +
306 + /* read first eraseblock from the trx */
307 + block = kmalloc(mtd->erasesize, GFP_KERNEL);
308 + trx2 = (struct trx_header *) block;
309 + if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
310 + printk("Error accessing the first trx eraseblock\n");
311 + return 0;
312 + }
313 +
314 + printk("Updating TRX offsets and length:\n");
315 + 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);
316 + 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);
317 +
318 + /* Write updated trx header to the flash */
319 + memcpy(block, &trx, sizeof(trx));
320 + if (mtd->unlock)
321 + mtd->unlock(mtd, off, mtd->erasesize);
322 + erase_write(mtd, off, mtd->erasesize, block);
323 + if (mtd->sync)
324 + mtd->sync(mtd);
325 + kfree(block);
326 + printk("Done\n");
327 + }
328 +
329 + return part->size;
330 +}
331 +
332 +struct mtd_partition * __init
333 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
334 +{
335 + int cfe_size;
336 +
337 + if ((cfe_size = find_cfe_size(mtd,size)) < 0)
338 + return NULL;
339 +
340 + /* boot loader */
341 + bcm947xx_parts[0].offset = 0;
342 + bcm947xx_parts[0].size = cfe_size;
343 +
344 + /* nvram */
345 + if (cfe_size != 384 * 1024) {
346 + bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
347 + bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
348 + } else {
349 + /* nvram (old 128kb config partition on netgear wgt634u) */
350 + bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
351 + bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
352 + }
353 +
354 + /* linux (kernel and rootfs) */
355 + if (cfe_size != 384 * 1024) {
356 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
357 + bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
358 + bcm947xx_parts[1].offset;
359 + } else {
360 + /* do not count the elf loader, which is on one block */
361 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
362 + bcm947xx_parts[3].size + mtd->erasesize;
363 + bcm947xx_parts[1].size = size -
364 + bcm947xx_parts[0].size -
365 + (2*bcm947xx_parts[3].size) -
366 + mtd->erasesize;
367 + }
368 +
369 + /* find and size rootfs */
370 + if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
371 + /* entirely jffs2 */
372 + bcm947xx_parts[4].name = NULL;
373 + bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset -
374 + bcm947xx_parts[3].size;
375 + } else {
376 + /* legacy setup */
377 + /* calculate leftover flash, and assign it to the jffs2 partition */
378 + if (cfe_size != 384 * 1024) {
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 = bcm947xx_parts[3].offset -
386 + bcm947xx_parts[4].offset;
387 + } else {
388 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
389 + bcm947xx_parts[2].size;
390 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
391 + bcm947xx_parts[4].offset += mtd->erasesize -
392 + (bcm947xx_parts[4].offset % mtd->erasesize);
393 + }
394 + bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
395 + bcm947xx_parts[4].offset;
396 + }
397 + }
398 +
399 + return bcm947xx_parts;
400 +}
401 +#endif
402 +
403 +int __init init_bcm947xx_map(void)
404 +{
405 + uint coreidx;
406 + chipcregs_t *cc;
407 + uint32 fltype;
408 + uint window_addr = 0, window_size = 0;
409 + size_t size;
410 + int ret = 0;
411 +#ifdef CONFIG_MTD_PARTITIONS
412 + struct mtd_partition *parts;
413 + int i;
414 +#endif
415 +
416 + coreidx = sb_coreidx(sbh);
417 +
418 + /* Check strapping option if chipcommon exists */
419 + if ((cc = sb_setcore(sbh, SB_CC, 0))) {
420 + fltype = readl(&cc->capabilities) & CAP_FLASH_MASK;
421 + if (fltype == PFLASH) {
422 + bcm947xx_map.map_priv_2 = 1;
423 + window_addr = 0x1c000000;
424 + bcm947xx_map.size = window_size = 32 * 1024 * 1024;
425 + if ((readl(&cc->flash_config) & CC_CFG_DS) == 0)
426 + bcm947xx_map.bankwidth = 1;
427 + }
428 + } else {
429 + fltype = PFLASH;
430 + bcm947xx_map.map_priv_2 = 0;
431 + window_addr = WINDOW_ADDR;
432 + window_size = WINDOW_SIZE;
433 + }
434 +
435 + sb_setcoreidx(sbh, coreidx);
436 +
437 + if (fltype != PFLASH) {
438 + printk(KERN_ERR "pflash: found no supported devices\n");
439 + ret = -ENODEV;
440 + goto fail;
441 + }
442 + bcm947xx_map.virt = ioremap(window_addr, window_size);
443 +
444 + if (!bcm947xx_map.virt) {
445 + printk("Failed to ioremap\n");
446 + return -EIO;
447 + }
448 + simple_map_init(&bcm947xx_map);
449 +
450 + bcm947xx_map.copy_from = bcm947xx_map_copy_from;
451 +
452 + if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
453 + printk("Failed to do_map_probe\n");
454 + iounmap((void *)bcm947xx_map.virt);
455 + return -ENXIO;
456 + }
457 +
458 + bcm947xx_mtd->owner = THIS_MODULE;
459 +
460 + size = bcm947xx_mtd->size;
461 +
462 + printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
463 +
464 +#ifdef CONFIG_MTD_PARTITIONS
465 + parts = init_mtd_partitions(bcm947xx_mtd, size);
466 + for (i = 0; parts[i].name; i++);
467 + ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
468 + if (ret) {
469 + printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
470 + goto fail;
471 + }
472 +#endif
473 + return 0;
474 +
475 + fail:
476 + if (bcm947xx_mtd)
477 + map_destroy(bcm947xx_mtd);
478 + if (bcm947xx_map.virt)
479 + iounmap((void *)bcm947xx_map.virt);
480 + bcm947xx_map.virt = 0;
481 + return ret;
482 +}
483 +
484 +void __exit cleanup_bcm947xx_map(void)
485 +{
486 +#ifdef CONFIG_MTD_PARTITIONS
487 + del_mtd_partitions(bcm947xx_mtd);
488 +#endif
489 + map_destroy(bcm947xx_mtd);
490 + iounmap((void *)bcm947xx_map.virt);
491 +}
492 +
493 +module_init(init_bcm947xx_map);
494 +module_exit(cleanup_bcm947xx_map);
495 diff -urN linux-2.6.19.ref/drivers/mtd/maps/Kconfig linux-2.6.19/drivers/mtd/maps/Kconfig
496 --- linux-2.6.19.ref/drivers/mtd/maps/Kconfig 2006-11-29 22:57:37.000000000 +0100
497 +++ linux-2.6.19/drivers/mtd/maps/Kconfig 2006-12-04 21:33:58.000000000 +0100
498 @@ -299,6 +299,12 @@
499 Mapping for the Flaga digital module. If you don't have one, ignore
500 this setting.
501
502 +config MTD_BCM47XX
503 + tristate "BCM47xx flash device"
504 + depends on MIPS && MTD_CFI && BCM947XX
505 + help
506 + Support for the flash chips on the BCM947xx board.
507 +
508 config MTD_BEECH
509 tristate "CFI Flash device mapped on IBM 405LP Beech"
510 depends on MTD_CFI && BEECH
511 diff -urN linux-2.6.19.ref/drivers/mtd/maps/Makefile linux-2.6.19/drivers/mtd/maps/Makefile
512 --- linux-2.6.19.ref/drivers/mtd/maps/Makefile 2006-11-29 22:57:37.000000000 +0100
513 +++ linux-2.6.19/drivers/mtd/maps/Makefile 2006-12-04 21:33:58.000000000 +0100
514 @@ -29,6 +29,7 @@
515 obj-$(CONFIG_MTD_PCMCIA) += pcmciamtd.o
516 obj-$(CONFIG_MTD_RPXLITE) += rpxlite.o
517 obj-$(CONFIG_MTD_TQM8XXL) += tqm8xxl.o
518 +obj-$(CONFIG_MTD_BCM47XX) += bcm47xx-flash.o
519 obj-$(CONFIG_MTD_SA1100) += sa1100-flash.o
520 obj-$(CONFIG_MTD_IPAQ) += ipaq-flash.o
521 obj-$(CONFIG_MTD_SBC_GXX) += sbc_gxx.o