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.
29 * Copyright 2004, Broadcom Corporation
30 * All Rights Reserved.
32 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
33 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
34 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
35 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
37 * Flash mapping for BCM947XX boards
41 #include <linux/module.h>
42 #include <linux/types.h>
43 #include <linux/kernel.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/config.h>
51 #include <linux/squashfs_fs.h>
52 #include <linux/jffs2.h>
53 #include <linux/crc32.h>
64 /* Global SB handle */
65 extern void *bcm947xx_sbh
;
66 extern spinlock_t bcm947xx_sbh_lock
;
69 #define sbh bcm947xx_sbh
70 #define sbh_lock bcm947xx_sbh_lock
72 #define WINDOW_ADDR 0x1fc00000
73 #define WINDOW_SIZE 0x400000
76 static struct mtd_info
*bcm947xx_mtd
;
78 __u8
bcm947xx_map_read8(struct map_info
*map
, unsigned long ofs
)
80 if (map
->map_priv_2
== 1)
81 return __raw_readb(map
->map_priv_1
+ ofs
);
83 u16 val
= __raw_readw(map
->map_priv_1
+ (ofs
& ~1));
85 return ((val
>> 8) & 0xff);
90 __u16
bcm947xx_map_read16(struct map_info
*map
, unsigned long ofs
)
92 return __raw_readw(map
->map_priv_1
+ ofs
);
95 __u32
bcm947xx_map_read32(struct map_info
*map
, unsigned long ofs
)
97 return __raw_readl(map
->map_priv_1
+ ofs
);
100 void bcm947xx_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
103 memcpy_fromio(to
, map
->map_priv_1
+ from
, len
);
106 u16
*dest
= (u16
*) to
;
107 u16
*src
= (u16
*) (map
->map_priv_1
+ from
);
108 for (i
= 0; i
< (len
/ 2); i
++) {
112 *((u8
*)dest
+len
-1) = src
[i
] & 0xff;
116 void bcm947xx_map_write8(struct map_info
*map
, __u8 d
, unsigned long adr
)
118 __raw_writeb(d
, map
->map_priv_1
+ adr
);
122 void bcm947xx_map_write16(struct map_info
*map
, __u16 d
, unsigned long adr
)
124 __raw_writew(d
, map
->map_priv_1
+ adr
);
128 void bcm947xx_map_write32(struct map_info
*map
, __u32 d
, unsigned long adr
)
130 __raw_writel(d
, map
->map_priv_1
+ adr
);
134 void bcm947xx_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
136 memcpy_toio(map
->map_priv_1
+ to
, from
, len
);
139 struct map_info bcm947xx_map
= {
140 name
: "Physically mapped flash",
143 read8
: bcm947xx_map_read8
,
144 read16
: bcm947xx_map_read16
,
145 read32
: bcm947xx_map_read32
,
146 copy_from
: bcm947xx_map_copy_from
,
147 write8
: bcm947xx_map_write8
,
148 write16
: bcm947xx_map_write16
,
149 write32
: bcm947xx_map_write32
,
150 copy_to
: bcm947xx_map_copy_to
153 #ifdef CONFIG_MTD_PARTITIONS
155 static struct mtd_partition bcm947xx_parts
[] = {
156 { name
: "cfe", offset
: 0, size
: 0, mask_flags
: MTD_WRITEABLE
, },
157 { name
: "linux", offset
: 0, size
: 0, },
158 { name
: "rootfs", offset
: 0, size
: 0, },
159 { name
: "nvram", offset
: 0, size
: 0, },
160 { name
: "rootfs_data", offset
: 0, size
: 0, },
165 find_cfe_size(struct mtd_info
*mtd
, size_t size
)
167 struct trx_header
*trx
;
168 unsigned char buf
[512];
173 trx
= (struct trx_header
*) buf
;
175 blocksize
= mtd
->erasesize
;
176 if (blocksize
< 0x10000)
179 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
180 memset(buf
, 0xe5, sizeof(buf
));
185 if (MTD_READ(mtd
, off
, sizeof(buf
), &len
, buf
) ||
189 /* found a TRX header */
190 if (le32_to_cpu(trx
->magic
) == TRX_MAGIC
) {
196 "%s: Couldn't find bootloader size\n",
201 printk(KERN_NOTICE
"bootloader size: %d\n", off
);
207 * Copied from mtdblock.c
211 * Since typical flash erasable sectors are much larger than what Linux's
212 * buffer cache can handle, we must implement read-modify-write on flash
213 * sectors for each block write requests. To avoid over-erasing flash sectors
214 * and to speed things up, we locally cache a whole flash sector while it is
215 * being written to until a different sector is required.
218 static void erase_callback(struct erase_info
*done
)
220 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
224 static int erase_write (struct mtd_info
*mtd
, unsigned long pos
,
225 int len
, const char *buf
)
227 struct erase_info erase
;
228 DECLARE_WAITQUEUE(wait
, current
);
229 wait_queue_head_t wait_q
;
234 * First, let's erase the flash block.
237 init_waitqueue_head(&wait_q
);
239 erase
.callback
= erase_callback
;
242 erase
.priv
= (u_long
)&wait_q
;
244 set_current_state(TASK_INTERRUPTIBLE
);
245 add_wait_queue(&wait_q
, &wait
);
247 ret
= MTD_ERASE(mtd
, &erase
);
249 set_current_state(TASK_RUNNING
);
250 remove_wait_queue(&wait_q
, &wait
);
251 printk (KERN_WARNING
"erase of region [0x%lx, 0x%x] "
252 "on \"%s\" failed\n",
253 pos
, len
, mtd
->name
);
257 schedule(); /* Wait for erase to finish. */
258 remove_wait_queue(&wait_q
, &wait
);
261 * Next, writhe data to flash.
264 ret
= MTD_WRITE (mtd
, pos
, len
, &retlen
, buf
);
276 find_root(struct mtd_info
*mtd
, size_t size
, struct mtd_partition
*part
)
278 struct trx_header trx
, *trx2
;
279 unsigned char buf
[512], *block
;
283 struct squashfs_super_block
*sb
= (struct squashfs_super_block
*) buf
;
285 blocksize
= mtd
->erasesize
;
286 if (blocksize
< 0x10000)
289 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
290 memset(&trx
, 0xe5, sizeof(trx
));
295 if (MTD_READ(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
299 /* found a TRX header */
300 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
) {
301 part
->offset
= le32_to_cpu(trx
.offsets
[2]) ? :
302 le32_to_cpu(trx
.offsets
[1]);
303 part
->size
= le32_to_cpu(trx
.len
);
305 part
->size
-= part
->offset
;
313 "%s: Couldn't find root filesystem\n",
321 if (MTD_READ(mtd
, part
->offset
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
324 if (*((__u32
*) buf
) == SQUASHFS_MAGIC
) {
325 printk(KERN_INFO
"%s: Filesystem type: squashfs, size=0x%x\n", mtd
->name
, (u32
) sb
->bytes_used
);
327 /* Update the squashfs partition size based on the superblock info */
328 part
->size
= sb
->bytes_used
;
329 len
= part
->offset
+ part
->size
;
330 len
+= (mtd
->erasesize
- 1);
331 len
&= ~(mtd
->erasesize
- 1);
332 part
->size
= len
- part
->offset
;
333 } else if (*((__u16
*) buf
) == JFFS2_MAGIC_BITMASK
) {
334 printk(KERN_INFO
"%s: Filesystem type: jffs2\n", mtd
->name
);
336 /* Move the squashfs outside of the trx */
339 printk(KERN_INFO
"%s: Filesystem type: unknown\n", mtd
->name
);
343 if (trx
.len
!= part
->offset
+ part
->size
- off
) {
344 /* Update the trx offsets and length */
345 trx
.len
= part
->offset
+ part
->size
- off
;
347 /* Update the trx crc32 */
348 for (i
= (u32
) &(((struct trx_header
*)NULL
)->flag_version
); i
<= trx
.len
; i
+= sizeof(buf
)) {
349 if (MTD_READ(mtd
, off
+ i
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
351 crc
= crc32_le(crc
, buf
, min(sizeof(buf
), trx
.len
- i
));
355 /* read first eraseblock from the trx */
356 trx2
= block
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
357 if (MTD_READ(mtd
, off
, mtd
->erasesize
, &len
, block
) || len
!= mtd
->erasesize
) {
358 printk("Error accessing the first trx eraseblock\n");
362 printk("Updating TRX offsets and length:\n");
363 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
);
364 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
);
366 /* Write updated trx header to the flash */
367 memcpy(block
, &trx
, sizeof(trx
));
369 mtd
->unlock(mtd
, off
, mtd
->erasesize
);
370 erase_write(mtd
, off
, mtd
->erasesize
, block
);
380 struct mtd_partition
* __init
381 init_mtd_partitions(struct mtd_info
*mtd
, size_t size
)
385 if ((cfe_size
= find_cfe_size(mtd
,size
)) < 0)
389 bcm947xx_parts
[0].offset
= 0;
390 bcm947xx_parts
[0].size
= cfe_size
;
393 if (cfe_size
!= 384 * 1024) {
394 bcm947xx_parts
[3].offset
= size
- ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
395 bcm947xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
397 /* nvram (old 128kb config partition on netgear wgt634u) */
398 bcm947xx_parts
[3].offset
= bcm947xx_parts
[0].size
;
399 bcm947xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
402 /* linux (kernel and rootfs) */
403 if (cfe_size
!= 384 * 1024) {
404 bcm947xx_parts
[1].offset
= bcm947xx_parts
[0].size
;
405 bcm947xx_parts
[1].size
= bcm947xx_parts
[3].offset
-
406 bcm947xx_parts
[1].offset
;
408 /* do not count the elf loader, which is on one block */
409 bcm947xx_parts
[1].offset
= bcm947xx_parts
[0].size
+
410 bcm947xx_parts
[3].size
+ mtd
->erasesize
;
411 bcm947xx_parts
[1].size
= size
-
412 bcm947xx_parts
[0].size
-
413 (2*bcm947xx_parts
[3].size
) -
417 /* find and size rootfs */
418 if (find_root(mtd
,size
,&bcm947xx_parts
[2])==0) {
420 bcm947xx_parts
[4].name
= NULL
;
421 bcm947xx_parts
[2].size
= size
- bcm947xx_parts
[2].offset
-
422 bcm947xx_parts
[3].size
;
425 /* calculate leftover flash, and assign it to the jffs2 partition */
426 if (cfe_size
!= 384 * 1024) {
427 bcm947xx_parts
[4].offset
= bcm947xx_parts
[2].offset
+
428 bcm947xx_parts
[2].size
;
429 if ((bcm947xx_parts
[4].offset
% mtd
->erasesize
) > 0) {
430 bcm947xx_parts
[4].offset
+= mtd
->erasesize
-
431 (bcm947xx_parts
[4].offset
% mtd
->erasesize
);
433 bcm947xx_parts
[4].size
= bcm947xx_parts
[3].offset
-
434 bcm947xx_parts
[4].offset
;
436 bcm947xx_parts
[4].offset
= bcm947xx_parts
[2].offset
+
437 bcm947xx_parts
[2].size
;
438 if ((bcm947xx_parts
[4].offset
% mtd
->erasesize
) > 0) {
439 bcm947xx_parts
[4].offset
+= mtd
->erasesize
-
440 (bcm947xx_parts
[4].offset
% mtd
->erasesize
);
442 bcm947xx_parts
[4].size
= size
- bcm947xx_parts
[3].size
-
443 bcm947xx_parts
[4].offset
;
447 return bcm947xx_parts
;
453 mod_init_t
init_bcm947xx_map(void)
459 uint window_addr
= 0, window_size
= 0;
462 #ifdef CONFIG_MTD_PARTITIONS
463 struct mtd_partition
*parts
;
467 spin_lock_irqsave(&sbh_lock
, flags
);
468 coreidx
= sb_coreidx(sbh
);
470 /* Check strapping option if chipcommon exists */
471 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
472 fltype
= readl(&cc
->capabilities
) & CC_CAP_FLASH_MASK
;
473 if (fltype
== PFLASH
) {
474 bcm947xx_map
.map_priv_2
= 1;
475 window_addr
= 0x1c000000;
476 bcm947xx_map
.size
= window_size
= 32 * 1024 * 1024;
477 if ((readl(&cc
->flash_config
) & CC_CFG_DS
) == 0)
478 bcm947xx_map
.buswidth
= 1;
482 bcm947xx_map
.map_priv_2
= 0;
483 window_addr
= WINDOW_ADDR
;
484 window_size
= WINDOW_SIZE
;
487 sb_setcoreidx(sbh
, coreidx
);
488 spin_unlock_irqrestore(&sbh_lock
, flags
);
490 if (fltype
!= PFLASH
) {
491 printk(KERN_ERR
"pflash: found no supported devices\n");
496 bcm947xx_map
.map_priv_1
= (unsigned long) ioremap(window_addr
, window_size
);
498 if (!bcm947xx_map
.map_priv_1
) {
499 printk(KERN_ERR
"Failed to ioremap\n");
503 if (!(bcm947xx_mtd
= do_map_probe("cfi_probe", &bcm947xx_map
))) {
504 printk(KERN_ERR
"pflash: cfi_probe failed\n");
505 iounmap((void *)bcm947xx_map
.map_priv_1
);
509 bcm947xx_mtd
->module
= THIS_MODULE
;
511 size
= bcm947xx_mtd
->size
;
513 printk(KERN_NOTICE
"Flash device: 0x%x at 0x%x\n", size
, window_addr
);
515 #ifdef CONFIG_MTD_PARTITIONS
516 parts
= init_mtd_partitions(bcm947xx_mtd
, size
);
517 for (i
= 0; parts
[i
].name
; i
++);
518 ret
= add_mtd_partitions(bcm947xx_mtd
, parts
, i
);
520 printk(KERN_ERR
"Flash: add_mtd_partitions failed\n");
529 map_destroy(bcm947xx_mtd
);
530 if (bcm947xx_map
.map_priv_1
)
531 iounmap((void *) bcm947xx_map
.map_priv_1
);
532 bcm947xx_map
.map_priv_1
= 0;
536 mod_exit_t
cleanup_bcm947xx_map(void)
538 #ifdef CONFIG_MTD_PARTITIONS
539 del_mtd_partitions(bcm947xx_mtd
);
541 map_destroy(bcm947xx_mtd
);
542 iounmap((void *) bcm947xx_map
.map_priv_1
);
543 bcm947xx_map
.map_priv_1
= 0;
546 module_init(init_bcm947xx_map
);
547 module_exit(cleanup_bcm947xx_map
);