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