2 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
4 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
6 * original functions for finding root filesystem from Mike Baker
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 * Copyright 2001-2003, Broadcom Corporation
29 * All Rights Reserved.
31 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
32 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
33 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
34 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
36 * Flash mapping for BCM947XX boards
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/sched.h>
44 #include <linux/wait.h>
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/map.h>
47 #ifdef CONFIG_MTD_PARTITIONS
48 #include <linux/mtd/partitions.h>
50 #include <linux/crc32.h>
52 #include <linux/ssb/ssb.h>
55 #include <asm/mach-bcm47xx/nvram.h>
56 #include <asm/fw/cfe/cfe_api.h>
59 #define TRX_MAGIC 0x30524448 /* "HDR0" */
61 #define TRX_MAX_LEN 0x3A0000
62 #define TRX_NO_HEADER 1 /* Do not write TRX header */
63 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
64 #define TRX_MAX_OFFSET 3
67 u32 magic
; /* "HDR0" */
68 u32 len
; /* Length of file including header */
69 u32 crc32
; /* 32-bit CRC from flag_version to end of file */
70 u32 flag_version
; /* 0:15 flags, 16:31 version */
71 u32 offsets
[TRX_MAX_OFFSET
]; /* Offsets of partitions from start of header */
74 /* for Edimax Print servers which use an additional header
75 * then the firmware on flash looks like :
76 * EDIMAX HEADER | TRX HEADER
77 * As this header is 12 bytes long we have to handle it
78 * and skip it to find the TRX header
80 #define EDIMAX_PS_HEADER_MAGIC 0x36315350 /* "PS16" */
81 #define EDIMAX_PS_HEADER_LEN 0xc /* 12 bytes long for edimax header */
83 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
84 #define NVRAM_SPACE 0x8000
85 #define WINDOW_ADDR 0x1fc00000
86 #define WINDOW_SIZE 0x400000
89 #define ROUTER_NETGEAR_WGR614L 1
90 #define ROUTER_NETGEAR_WNR834B 2
91 #define ROUTER_NETGEAR_WNDR3300 3
92 #define ROUTER_NETGEAR_WNR3500L 4
95 extern struct ssb_bus ssb_bcm47xx
;
97 static struct mtd_info
*bcm47xx_mtd
;
99 static void bcm47xx_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
102 memcpy_fromio(to
, map
->virt
+ from
, len
);
105 u16
*dest
= (u16
*) to
;
106 u16
*src
= (u16
*) (map
->virt
+ from
);
107 for (i
= 0; i
< (len
/ 2); i
++) {
111 *((u8
*)dest
+len
-1) = src
[i
] & 0xff;
115 static struct map_info bcm47xx_map
= {
116 name
: "Physically mapped flash",
122 #ifdef CONFIG_MTD_PARTITIONS
124 static struct mtd_partition bcm47xx_parts
[] = {
125 { name
: "cfe", offset
: 0, size
: 0, mask_flags
: MTD_WRITEABLE
, },
126 { name
: "linux", offset
: 0, size
: 0, },
127 { name
: "rootfs", offset
: 0, size
: 0, },
128 { name
: "nvram", offset
: 0, size
: 0, },
133 find_cfe_size(struct mtd_info
*mtd
, size_t size
)
135 struct trx_header
*trx
;
136 unsigned char buf
[512];
141 trx
= (struct trx_header
*) buf
;
143 blocksize
= mtd
->erasesize
;
144 if (blocksize
< 0x10000)
147 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
148 memset(buf
, 0xe5, sizeof(buf
));
153 if (mtd
->read(mtd
, off
, sizeof(buf
), &len
, buf
) ||
157 if (le32_to_cpu(trx
->magic
) == EDIMAX_PS_HEADER_MAGIC
) {
158 if (mtd
->read(mtd
, off
+ EDIMAX_PS_HEADER_LEN
,
159 sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
)) {
162 printk(KERN_NOTICE
"Found edimax header\n");
166 /* found a TRX header */
167 if (le32_to_cpu(trx
->magic
) == TRX_MAGIC
) {
173 "%s: Couldn't find bootloader size\n",
178 printk(KERN_NOTICE
"bootloader size: %d\n", off
);
184 * Copied from mtdblock.c
188 * Since typical flash erasable sectors are much larger than what Linux's
189 * buffer cache can handle, we must implement read-modify-write on flash
190 * sectors for each block write requests. To avoid over-erasing flash sectors
191 * and to speed things up, we locally cache a whole flash sector while it is
192 * being written to until a different sector is required.
195 static void erase_callback(struct erase_info
*done
)
197 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
201 static int erase_write (struct mtd_info
*mtd
, unsigned long pos
,
202 int len
, const char *buf
)
204 struct erase_info erase
;
205 DECLARE_WAITQUEUE(wait
, current
);
206 wait_queue_head_t wait_q
;
211 * First, let's erase the flash block.
214 init_waitqueue_head(&wait_q
);
216 erase
.callback
= erase_callback
;
219 erase
.priv
= (u_long
)&wait_q
;
221 set_current_state(TASK_INTERRUPTIBLE
);
222 add_wait_queue(&wait_q
, &wait
);
224 ret
= mtd
->erase(mtd
, &erase
);
226 set_current_state(TASK_RUNNING
);
227 remove_wait_queue(&wait_q
, &wait
);
228 printk (KERN_WARNING
"erase of region [0x%lx, 0x%x] "
229 "on \"%s\" failed\n",
230 pos
, len
, mtd
->name
);
234 schedule(); /* Wait for erase to finish. */
235 remove_wait_queue(&wait_q
, &wait
);
238 * Next, write data to flash.
241 ret
= mtd
->write (mtd
, pos
, len
, &retlen
, buf
);
251 find_dual_image_off (struct mtd_info
*mtd
, size_t size
)
253 struct trx_header trx
;
257 blocksize
= mtd
->erasesize
;
258 if (blocksize
< 0x10000)
261 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
262 memset(&trx
, 0xe5, sizeof(trx
));
266 if (mtd
->read(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
269 /* found last TRX header */
270 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
){
271 if (le32_to_cpu(trx
.flag_version
>> 16)==2){
272 printk("dual image TRX header found\n");
284 find_root(struct mtd_info
*mtd
, size_t size
, struct mtd_partition
*part
)
286 struct trx_header trx
, *trx2
;
287 unsigned char buf
[512], *block
;
288 int off
, blocksize
, trxoff
= 0;
293 blocksize
= mtd
->erasesize
;
294 if (blocksize
< 0x10000)
297 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
298 memset(&trx
, 0xe5, sizeof(trx
));
303 if (mtd
->read(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
307 /* found an edimax header */
308 if (le32_to_cpu(trx
.magic
) == EDIMAX_PS_HEADER_MAGIC
) {
309 /* read the correct trx header */
310 if (mtd
->read(mtd
, off
+ EDIMAX_PS_HEADER_LEN
,
311 sizeof(trx
), &len
, (char *) &trx
) ||
312 len
!= sizeof(trx
)) {
315 printk(KERN_NOTICE
"Found an edimax ps header\n");
320 /* found a TRX header */
321 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
) {
322 part
->offset
= le32_to_cpu(trx
.offsets
[2]) ? :
323 le32_to_cpu(trx
.offsets
[1]);
324 part
->size
= le32_to_cpu(trx
.len
);
326 part
->size
-= part
->offset
;
329 off
+= EDIMAX_PS_HEADER_LEN
;
330 trxoff
= EDIMAX_PS_HEADER_LEN
;
338 "%s: Couldn't find root filesystem\n",
343 printk(KERN_NOTICE
"TRX offset : %lx\n", trxoff
);
347 if (mtd
->read(mtd
, part
->offset
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
350 /* Move the fs outside of the trx */
353 if (trx
.len
!= part
->offset
+ part
->size
- off
) {
354 /* Update the trx offsets and length */
355 trx
.len
= part
->offset
+ part
->size
- off
;
357 /* Update the trx crc32 */
358 for (i
= (u32
) &(((struct trx_header
*)NULL
)->flag_version
); i
<= trx
.len
; i
+= sizeof(buf
)) {
359 if (mtd
->read(mtd
, off
+ i
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
361 crc
= crc32_le(crc
, buf
, min(sizeof(buf
), trx
.len
- i
));
365 /* read first eraseblock from the trx */
366 block
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
367 trx2
= (struct trx_header
*) block
;
368 if (mtd
->read(mtd
, off
- trxoff
, mtd
->erasesize
, &len
, block
) || len
!= mtd
->erasesize
) {
369 printk("Error accessing the first trx eraseblock\n");
373 printk("Updating TRX offsets and length:\n");
374 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
);
375 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
);
377 /* Write updated trx header to the flash */
378 memcpy(block
+ trxoff
, &trx
, sizeof(trx
));
380 mtd
->unlock(mtd
, off
- trxoff
, mtd
->erasesize
);
381 erase_write(mtd
, off
- trxoff
, mtd
->erasesize
, block
);
391 static int get_router(void)
401 if (nvram_getenv("boardnum", buf
, sizeof(buf
)) >= 0)
402 boardnum
= simple_strtoul(buf
, NULL
, 0);
403 if (nvram_getenv("boardtype", buf
, sizeof(buf
)) >= 0)
404 boardtype
= simple_strtoul(buf
, NULL
, 0);
405 if (nvram_getenv("boardrev", buf
, sizeof(buf
)) >= 0)
406 boardrev
= simple_strtoul(buf
, NULL
, 0);
407 if (nvram_getenv("boardflags", buf
, sizeof(buf
)) >= 0)
408 boardflags
= simple_strtoul(buf
, NULL
, 0);
409 if (nvram_getenv("sdram_init", buf
, sizeof(buf
)) >= 0)
410 sdram_init
= simple_strtoul(buf
, NULL
, 0);
411 if (nvram_getenv("cardbus", buf
, sizeof(buf
)) >= 0)
412 cardbus
= simple_strtoul(buf
, NULL
, 0);
414 if ((boardnum
== 8 || boardnum
== 01)
415 && boardtype
== 0x0472 && cardbus
== 1) {
416 /* Netgear WNR834B, Netgear WNR834Bv2 */
417 return ROUTER_NETGEAR_WNR834B
;
420 if (boardnum
== 01 && boardtype
== 0x0472 && boardrev
== 0x23) {
421 /* Netgear WNDR-3300 */
422 return ROUTER_NETGEAR_WNDR3300
;
425 if ((boardnum
== 83258 || boardnum
== 01)
426 && boardtype
== 0x048e
427 && (boardrev
== 0x11 || boardrev
== 0x10)
428 && boardflags
== 0x750
429 && sdram_init
== 0x000A) {
430 /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
431 return ROUTER_NETGEAR_WGR614L
;
434 if ((boardnum
== 1 || boardnum
== 3500)
435 && boardtype
== 0x04CF
436 && (boardrev
== 0x1213 || boardrev
== 02)) {
437 /* Netgear WNR3500v2/U/L */
438 return ROUTER_NETGEAR_WNR3500L
;
444 struct mtd_partition
* __init
445 init_mtd_partitions(struct mtd_info
*mtd
, size_t size
)
448 int dual_image_offset
= 0;
449 /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
450 * part from our mapping to prevent overwriting len/checksum on e.g.
451 * Netgear WGR614v8/L/WW
453 int board_data_size
= 0;
455 switch (get_router()) {
456 case ROUTER_NETGEAR_WGR614L
:
457 case ROUTER_NETGEAR_WNR834B
:
458 case ROUTER_NETGEAR_WNDR3300
:
459 case ROUTER_NETGEAR_WNR3500L
:
460 /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
461 * is @ 0x007AFFF8 for 8M flash
463 board_data_size
= 4 * mtd
->erasesize
;
467 if ((cfe_size
= find_cfe_size(mtd
,size
)) < 0)
471 bcm47xx_parts
[0].offset
= 0;
472 bcm47xx_parts
[0].size
= cfe_size
;
475 if (cfe_size
!= 384 * 1024) {
476 bcm47xx_parts
[3].offset
= size
- ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
477 bcm47xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
479 /* nvram (old 128kb config partition on netgear wgt634u) */
480 bcm47xx_parts
[3].offset
= bcm47xx_parts
[0].size
;
481 bcm47xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
484 /* dual image offset*/
485 printk("Looking for dual image\n");
486 dual_image_offset
=find_dual_image_off(mtd
,size
);
487 /* linux (kernel and rootfs) */
488 if (cfe_size
!= 384 * 1024) {
489 bcm47xx_parts
[1].offset
= bcm47xx_parts
[0].size
;
490 bcm47xx_parts
[1].size
= bcm47xx_parts
[3].offset
- dual_image_offset
-
491 bcm47xx_parts
[1].offset
- board_data_size
;
493 /* do not count the elf loader, which is on one block */
494 bcm47xx_parts
[1].offset
= bcm47xx_parts
[0].size
+
495 bcm47xx_parts
[3].size
+ mtd
->erasesize
;
496 bcm47xx_parts
[1].size
= size
-
497 bcm47xx_parts
[0].size
-
498 (2*bcm47xx_parts
[3].size
) -
499 mtd
->erasesize
- board_data_size
;
502 /* find and size rootfs */
503 find_root(mtd
,size
,&bcm47xx_parts
[2]);
504 bcm47xx_parts
[2].size
= size
- dual_image_offset
-
505 bcm47xx_parts
[2].offset
-
506 bcm47xx_parts
[3].size
- board_data_size
;
508 return bcm47xx_parts
;
512 int __init
init_bcm47xx_map(void)
515 struct ssb_mipscore
*mcore
= &ssb_bcm47xx
.mipscore
;
519 #ifdef CONFIG_MTD_PARTITIONS
520 struct mtd_partition
*parts
;
525 u32 window
= mcore
->flash_window
;
526 u32 window_size
= mcore
->flash_window_size
;
528 printk("flash init: 0x%08x 0x%08x\n", window
, window_size
);
529 bcm47xx_map
.phys
= window
;
530 bcm47xx_map
.size
= window_size
;
531 bcm47xx_map
.bankwidth
= mcore
->flash_buswidth
;
532 bcm47xx_map
.virt
= ioremap_nocache(window
, window_size
);
534 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR
, WINDOW_SIZE
);
535 bcm47xx_map
.virt
= ioremap_nocache(WINDOW_ADDR
, WINDOW_SIZE
);
538 if (!bcm47xx_map
.virt
) {
539 printk("Failed to ioremap\n");
543 simple_map_init(&bcm47xx_map
);
545 if (!(bcm47xx_mtd
= do_map_probe("cfi_probe", &bcm47xx_map
))) {
546 printk("Failed to do_map_probe\n");
547 iounmap((void *)bcm47xx_map
.virt
);
551 /* override copy_from routine */
552 bcm47xx_map
.copy_from
= bcm47xx_map_copy_from
;
554 bcm47xx_mtd
->owner
= THIS_MODULE
;
556 size
= bcm47xx_mtd
->size
;
558 printk(KERN_NOTICE
"Flash device: 0x%x at 0x%x\n", size
, WINDOW_ADDR
);
560 #ifdef CONFIG_MTD_PARTITIONS
561 parts
= init_mtd_partitions(bcm47xx_mtd
, size
);
562 for (i
= 0; parts
[i
].name
; i
++);
563 ret
= add_mtd_partitions(bcm47xx_mtd
, parts
, i
);
565 printk(KERN_ERR
"Flash: add_mtd_partitions failed\n");
573 map_destroy(bcm47xx_mtd
);
574 if (bcm47xx_map
.virt
)
575 iounmap((void *)bcm47xx_map
.virt
);
576 bcm47xx_map
.virt
= 0;
580 void __exit
cleanup_bcm47xx_map(void)
582 #ifdef CONFIG_MTD_PARTITIONS
583 del_mtd_partitions(bcm47xx_mtd
);
585 map_destroy(bcm47xx_mtd
);
586 iounmap((void *)bcm47xx_map
.virt
);
589 module_init(init_bcm47xx_map
);
590 module_exit(cleanup_bcm47xx_map
);