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 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
75 #define NVRAM_SPACE 0x8000
76 #define WINDOW_ADDR 0x1fc00000
77 #define WINDOW_SIZE 0x400000
80 #define ROUTER_NETGEAR_WGR614L 1
81 #define ROUTER_NETGEAR_WNR834B 2
82 #define ROUTER_NETGEAR_WNDR3300 3
83 #define ROUTER_NETGEAR_WNR3500L 4
86 extern struct ssb_bus ssb_bcm47xx
;
88 static struct mtd_info
*bcm47xx_mtd
;
90 static void bcm47xx_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
93 memcpy_fromio(to
, map
->virt
+ from
, len
);
96 u16
*dest
= (u16
*) to
;
97 u16
*src
= (u16
*) (map
->virt
+ from
);
98 for (i
= 0; i
< (len
/ 2); i
++) {
102 *((u8
*)dest
+len
-1) = src
[i
] & 0xff;
106 static struct map_info bcm47xx_map
= {
107 name
: "Physically mapped flash",
113 #ifdef CONFIG_MTD_PARTITIONS
115 static struct mtd_partition bcm47xx_parts
[] = {
116 { name
: "cfe", offset
: 0, size
: 0, mask_flags
: MTD_WRITEABLE
, },
117 { name
: "linux", offset
: 0, size
: 0, },
118 { name
: "rootfs", offset
: 0, size
: 0, },
119 { name
: "nvram", offset
: 0, size
: 0, },
124 find_cfe_size(struct mtd_info
*mtd
, size_t size
)
126 struct trx_header
*trx
;
127 unsigned char buf
[512];
132 trx
= (struct trx_header
*) buf
;
134 blocksize
= mtd
->erasesize
;
135 if (blocksize
< 0x10000)
138 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
139 memset(buf
, 0xe5, sizeof(buf
));
144 if (mtd
->read(mtd
, off
, sizeof(buf
), &len
, buf
) ||
148 /* found a TRX header */
149 if (le32_to_cpu(trx
->magic
) == TRX_MAGIC
) {
155 "%s: Couldn't find bootloader size\n",
160 printk(KERN_NOTICE
"bootloader size: %d\n", off
);
166 * Copied from mtdblock.c
170 * Since typical flash erasable sectors are much larger than what Linux's
171 * buffer cache can handle, we must implement read-modify-write on flash
172 * sectors for each block write requests. To avoid over-erasing flash sectors
173 * and to speed things up, we locally cache a whole flash sector while it is
174 * being written to until a different sector is required.
177 static void erase_callback(struct erase_info
*done
)
179 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
183 static int erase_write (struct mtd_info
*mtd
, unsigned long pos
,
184 int len
, const char *buf
)
186 struct erase_info erase
;
187 DECLARE_WAITQUEUE(wait
, current
);
188 wait_queue_head_t wait_q
;
193 * First, let's erase the flash block.
196 init_waitqueue_head(&wait_q
);
198 erase
.callback
= erase_callback
;
201 erase
.priv
= (u_long
)&wait_q
;
203 set_current_state(TASK_INTERRUPTIBLE
);
204 add_wait_queue(&wait_q
, &wait
);
206 ret
= mtd
->erase(mtd
, &erase
);
208 set_current_state(TASK_RUNNING
);
209 remove_wait_queue(&wait_q
, &wait
);
210 printk (KERN_WARNING
"erase of region [0x%lx, 0x%x] "
211 "on \"%s\" failed\n",
212 pos
, len
, mtd
->name
);
216 schedule(); /* Wait for erase to finish. */
217 remove_wait_queue(&wait_q
, &wait
);
220 * Next, writhe data to flash.
223 ret
= mtd
->write (mtd
, pos
, len
, &retlen
, buf
);
233 find_dual_image_off (struct mtd_info
*mtd
, size_t size
)
235 struct trx_header trx
;
239 blocksize
= mtd
->erasesize
;
240 if (blocksize
< 0x10000)
243 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
244 memset(&trx
, 0xe5, sizeof(trx
));
248 if (mtd
->read(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
251 /* found last TRX header */
252 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
){
253 if (le32_to_cpu(trx
.flag_version
>> 16)==2){
254 printk("dual image TRX header found\n");
266 find_root(struct mtd_info
*mtd
, size_t size
, struct mtd_partition
*part
)
268 struct trx_header trx
, *trx2
;
269 unsigned char buf
[512], *block
;
274 blocksize
= mtd
->erasesize
;
275 if (blocksize
< 0x10000)
278 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
279 memset(&trx
, 0xe5, sizeof(trx
));
284 if (mtd
->read(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
288 /* found a TRX header */
289 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
) {
290 part
->offset
= le32_to_cpu(trx
.offsets
[2]) ? :
291 le32_to_cpu(trx
.offsets
[1]);
292 part
->size
= le32_to_cpu(trx
.len
);
294 part
->size
-= part
->offset
;
302 "%s: Couldn't find root filesystem\n",
310 if (mtd
->read(mtd
, part
->offset
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
313 /* Move the fs outside of the trx */
316 if (trx
.len
!= part
->offset
+ part
->size
- off
) {
317 /* Update the trx offsets and length */
318 trx
.len
= part
->offset
+ part
->size
- off
;
320 /* Update the trx crc32 */
321 for (i
= (u32
) &(((struct trx_header
*)NULL
)->flag_version
); i
<= trx
.len
; i
+= sizeof(buf
)) {
322 if (mtd
->read(mtd
, off
+ i
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
324 crc
= crc32_le(crc
, buf
, min(sizeof(buf
), trx
.len
- i
));
328 /* read first eraseblock from the trx */
329 block
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
330 trx2
= (struct trx_header
*) block
;
331 if (mtd
->read(mtd
, off
, mtd
->erasesize
, &len
, block
) || len
!= mtd
->erasesize
) {
332 printk("Error accessing the first trx eraseblock\n");
336 printk("Updating TRX offsets and length:\n");
337 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
);
338 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
);
340 /* Write updated trx header to the flash */
341 memcpy(block
, &trx
, sizeof(trx
));
343 mtd
->unlock(mtd
, off
, mtd
->erasesize
);
344 erase_write(mtd
, off
, mtd
->erasesize
, block
);
354 static int get_router(void)
364 if (nvram_getenv("boardnum", buf
, sizeof(buf
)) >= 0 ||
365 cfe_getenv("boardnum", buf
, sizeof(buf
)) >= 0)
366 boardnum
= simple_strtoul(buf
, NULL
, 0);
367 if (nvram_getenv("boardtype", buf
, sizeof(buf
)) >= 0 ||
368 cfe_getenv("boardtype", buf
, sizeof(buf
)) >= 0)
369 boardtype
= simple_strtoul(buf
, NULL
, 0);
370 if (nvram_getenv("boardrev", buf
, sizeof(buf
)) >= 0 ||
371 cfe_getenv("boardrev", buf
, sizeof(buf
)) >= 0)
372 boardrev
= simple_strtoul(buf
, NULL
, 0);
373 if (nvram_getenv("boardflags", buf
, sizeof(buf
)) >= 0 ||
374 cfe_getenv("boardflags", buf
, sizeof(buf
)) >= 0)
375 boardflags
= simple_strtoul(buf
, NULL
, 0);
376 if (nvram_getenv("sdram_init", buf
, sizeof(buf
)) >= 0 ||
377 cfe_getenv("sdram_init", buf
, sizeof(buf
)) >= 0)
378 sdram_init
= simple_strtoul(buf
, NULL
, 0);
379 if (nvram_getenv("cardbus", buf
, sizeof(buf
)) >= 0 ||
380 cfe_getenv("cardbus", buf
, sizeof(buf
)) >= 0)
381 cardbus
= simple_strtoul(buf
, NULL
, 0);
383 if ((boardnum
== 8 || boardnum
== 01)
384 && boardtype
== 0x0472 && cardbus
== 1) {
385 /* Netgear WNR834B, Netgear WNR834Bv2 */
386 return ROUTER_NETGEAR_WNR834B
;
389 if (boardnum
== 01 && boardtype
== 0x0472 && boardrev
== 0x23) {
390 /* Netgear WNDR-3300 */
391 return ROUTER_NETGEAR_WNDR3300
;
394 if ((boardnum
== 83258 || boardnum
== 01)
395 && boardtype
== 0x048e
396 && (boardrev
== 0x11 || boardrev
== 0x10)
397 && boardflags
== 0x750
398 && sdram_init
== 0x000A) {
399 /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
400 return ROUTER_NETGEAR_WGR614L
;
403 if ((boardnum
== 1 || boardnum
== 3500)
404 && boardtype
== 0x04CF
405 && (boardrev
== 0x1213 || boardrev
== 02)) {
406 /* Netgear WNR3500v2/U/L */
407 return ROUTER_NETGEAR_WNR3500L
;
413 struct mtd_partition
* __init
414 init_mtd_partitions(struct mtd_info
*mtd
, size_t size
)
417 int dual_image_offset
= 0;
418 /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
419 * part from our mapping to prevent overwriting len/checksum on e.g.
420 * Netgear WGR614v8/L/WW
422 int board_data_size
= 0;
424 switch (get_router()) {
425 case ROUTER_NETGEAR_WGR614L
:
426 case ROUTER_NETGEAR_WNR834B
:
427 case ROUTER_NETGEAR_WNDR3300
:
428 case ROUTER_NETGEAR_WNR3500L
:
429 /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
430 * is @ 0x007AFFF8 for 8M flash
432 board_data_size
= 4 * mtd
->erasesize
;
436 if ((cfe_size
= find_cfe_size(mtd
,size
)) < 0)
440 bcm47xx_parts
[0].offset
= 0;
441 bcm47xx_parts
[0].size
= cfe_size
;
444 if (cfe_size
!= 384 * 1024) {
445 bcm47xx_parts
[3].offset
= size
- ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
446 bcm47xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
448 /* nvram (old 128kb config partition on netgear wgt634u) */
449 bcm47xx_parts
[3].offset
= bcm47xx_parts
[0].size
;
450 bcm47xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
453 /* dual image offset*/
454 printk("Looking for dual image\n");
455 dual_image_offset
=find_dual_image_off(mtd
,size
);
456 /* linux (kernel and rootfs) */
457 if (cfe_size
!= 384 * 1024) {
458 bcm47xx_parts
[1].offset
= bcm47xx_parts
[0].size
;
459 bcm47xx_parts
[1].size
= bcm47xx_parts
[3].offset
- dual_image_offset
-
460 bcm47xx_parts
[1].offset
- board_data_size
;
462 /* do not count the elf loader, which is on one block */
463 bcm47xx_parts
[1].offset
= bcm47xx_parts
[0].size
+
464 bcm47xx_parts
[3].size
+ mtd
->erasesize
;
465 bcm47xx_parts
[1].size
= size
-
466 bcm47xx_parts
[0].size
-
467 (2*bcm47xx_parts
[3].size
) -
468 mtd
->erasesize
- board_data_size
;
471 /* find and size rootfs */
472 find_root(mtd
,size
,&bcm47xx_parts
[2]);
473 bcm47xx_parts
[2].size
= size
- dual_image_offset
-
474 bcm47xx_parts
[2].offset
-
475 bcm47xx_parts
[3].size
- board_data_size
;
477 return bcm47xx_parts
;
481 int __init
init_bcm47xx_map(void)
484 struct ssb_mipscore
*mcore
= &ssb_bcm47xx
.mipscore
;
488 #ifdef CONFIG_MTD_PARTITIONS
489 struct mtd_partition
*parts
;
494 u32 window
= mcore
->flash_window
;
495 u32 window_size
= mcore
->flash_window_size
;
497 printk("flash init: 0x%08x 0x%08x\n", window
, window_size
);
498 bcm47xx_map
.phys
= window
;
499 bcm47xx_map
.size
= window_size
;
500 bcm47xx_map
.bankwidth
= mcore
->flash_buswidth
;
501 bcm47xx_map
.virt
= ioremap_nocache(window
, window_size
);
503 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR
, WINDOW_SIZE
);
504 bcm47xx_map
.virt
= ioremap_nocache(WINDOW_ADDR
, WINDOW_SIZE
);
507 if (!bcm47xx_map
.virt
) {
508 printk("Failed to ioremap\n");
512 simple_map_init(&bcm47xx_map
);
514 if (!(bcm47xx_mtd
= do_map_probe("cfi_probe", &bcm47xx_map
))) {
515 printk("Failed to do_map_probe\n");
516 iounmap((void *)bcm47xx_map
.virt
);
520 /* override copy_from routine */
521 bcm47xx_map
.copy_from
= bcm47xx_map_copy_from
;
523 bcm47xx_mtd
->owner
= THIS_MODULE
;
525 size
= bcm47xx_mtd
->size
;
527 printk(KERN_NOTICE
"Flash device: 0x%x at 0x%x\n", size
, WINDOW_ADDR
);
529 #ifdef CONFIG_MTD_PARTITIONS
530 parts
= init_mtd_partitions(bcm47xx_mtd
, size
);
531 for (i
= 0; parts
[i
].name
; i
++);
532 ret
= add_mtd_partitions(bcm47xx_mtd
, parts
, i
);
534 printk(KERN_ERR
"Flash: add_mtd_partitions failed\n");
542 map_destroy(bcm47xx_mtd
);
543 if (bcm47xx_map
.virt
)
544 iounmap((void *)bcm47xx_map
.virt
);
545 bcm47xx_map
.virt
= 0;
549 void __exit
cleanup_bcm47xx_map(void)
551 #ifdef CONFIG_MTD_PARTITIONS
552 del_mtd_partitions(bcm47xx_mtd
);
554 map_destroy(bcm47xx_mtd
);
555 iounmap((void *)bcm47xx_map
.virt
);
558 module_init(init_bcm47xx_map
);
559 module_exit(cleanup_bcm47xx_map
);