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>
65 /* Global SB handle */
66 extern void *bcm947xx_sbh
;
67 extern spinlock_t bcm947xx_sbh_lock
;
70 #define sbh bcm947xx_sbh
71 #define sbh_lock bcm947xx_sbh_lock
73 #define WINDOW_ADDR 0x1fc00000
74 #define WINDOW_SIZE 0x400000
77 static struct mtd_info
*bcm947xx_mtd
;
79 __u8
bcm947xx_map_read8(struct map_info
*map
, unsigned long ofs
)
81 if (map
->map_priv_2
== 1)
82 return __raw_readb(map
->map_priv_1
+ ofs
);
84 u16 val
= __raw_readw(map
->map_priv_1
+ (ofs
& ~1));
86 return ((val
>> 8) & 0xff);
91 __u16
bcm947xx_map_read16(struct map_info
*map
, unsigned long ofs
)
93 return __raw_readw(map
->map_priv_1
+ ofs
);
96 __u32
bcm947xx_map_read32(struct map_info
*map
, unsigned long ofs
)
98 return __raw_readl(map
->map_priv_1
+ ofs
);
101 void bcm947xx_map_copy_from(struct map_info
*map
, void *to
, unsigned long from
, ssize_t len
)
104 memcpy_fromio(to
, map
->map_priv_1
+ from
, len
);
107 u16
*dest
= (u16
*) to
;
108 u16
*src
= (u16
*) (map
->map_priv_1
+ from
);
109 for (i
= 0; i
< (len
/ 2); i
++) {
113 *((u8
*)dest
+len
-1) = src
[i
] & 0xff;
117 void bcm947xx_map_write8(struct map_info
*map
, __u8 d
, unsigned long adr
)
119 __raw_writeb(d
, map
->map_priv_1
+ adr
);
123 void bcm947xx_map_write16(struct map_info
*map
, __u16 d
, unsigned long adr
)
125 __raw_writew(d
, map
->map_priv_1
+ adr
);
129 void bcm947xx_map_write32(struct map_info
*map
, __u32 d
, unsigned long adr
)
131 __raw_writel(d
, map
->map_priv_1
+ adr
);
135 void bcm947xx_map_copy_to(struct map_info
*map
, unsigned long to
, const void *from
, ssize_t len
)
137 memcpy_toio(map
->map_priv_1
+ to
, from
, len
);
140 struct map_info bcm947xx_map
= {
141 name
: "Physically mapped flash",
144 read8
: bcm947xx_map_read8
,
145 read16
: bcm947xx_map_read16
,
146 read32
: bcm947xx_map_read32
,
147 copy_from
: bcm947xx_map_copy_from
,
148 write8
: bcm947xx_map_write8
,
149 write16
: bcm947xx_map_write16
,
150 write32
: bcm947xx_map_write32
,
151 copy_to
: bcm947xx_map_copy_to
154 #ifdef CONFIG_MTD_PARTITIONS
156 static struct mtd_partition bcm947xx_parts
[] = {
157 { name
: "cfe", offset
: 0, size
: 0, mask_flags
: MTD_WRITEABLE
, },
158 { name
: "linux", offset
: 0, size
: 0, },
159 { name
: "rootfs", offset
: 0, size
: 0, },
160 { name
: "nvram", offset
: 0, size
: 0, },
161 { name
: "OpenWrt", offset
: 0, size
: 0, },
166 find_cfe_size(struct mtd_info
*mtd
, size_t size
)
168 struct trx_header
*trx
;
169 unsigned char buf
[512];
174 trx
= (struct trx_header
*) buf
;
176 blocksize
= mtd
->erasesize
;
177 if (blocksize
< 0x10000)
180 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
181 memset(buf
, 0xe5, sizeof(buf
));
186 if (MTD_READ(mtd
, off
, sizeof(buf
), &len
, buf
) ||
190 /* found a TRX header */
191 if (le32_to_cpu(trx
->magic
) == TRX_MAGIC
) {
197 "%s: Couldn't find bootloader size\n",
202 printk(KERN_NOTICE
"bootloader size: %d\n", off
);
208 * Copied from mtdblock.c
212 * Since typical flash erasable sectors are much larger than what Linux's
213 * buffer cache can handle, we must implement read-modify-write on flash
214 * sectors for each block write requests. To avoid over-erasing flash sectors
215 * and to speed things up, we locally cache a whole flash sector while it is
216 * being written to until a different sector is required.
219 static void erase_callback(struct erase_info
*done
)
221 wait_queue_head_t
*wait_q
= (wait_queue_head_t
*)done
->priv
;
225 static int erase_write (struct mtd_info
*mtd
, unsigned long pos
,
226 int len
, const char *buf
)
228 struct erase_info erase
;
229 DECLARE_WAITQUEUE(wait
, current
);
230 wait_queue_head_t wait_q
;
235 * First, let's erase the flash block.
238 init_waitqueue_head(&wait_q
);
240 erase
.callback
= erase_callback
;
243 erase
.priv
= (u_long
)&wait_q
;
245 set_current_state(TASK_INTERRUPTIBLE
);
246 add_wait_queue(&wait_q
, &wait
);
248 ret
= MTD_ERASE(mtd
, &erase
);
250 set_current_state(TASK_RUNNING
);
251 remove_wait_queue(&wait_q
, &wait
);
252 printk (KERN_WARNING
"erase of region [0x%lx, 0x%x] "
253 "on \"%s\" failed\n",
254 pos
, len
, mtd
->name
);
258 schedule(); /* Wait for erase to finish. */
259 remove_wait_queue(&wait_q
, &wait
);
262 * Next, writhe data to flash.
265 ret
= MTD_WRITE (mtd
, pos
, len
, &retlen
, buf
);
277 find_root(struct mtd_info
*mtd
, size_t size
, struct mtd_partition
*part
)
279 struct trx_header trx
, *trx2
;
280 unsigned char buf
[512], *block
;
284 struct squashfs_super_block
*sb
= (struct squashfs_super_block
*) buf
;
286 blocksize
= mtd
->erasesize
;
287 if (blocksize
< 0x10000)
290 for (off
= (128*1024); off
< size
; off
+= blocksize
) {
291 memset(&trx
, 0xe5, sizeof(trx
));
296 if (MTD_READ(mtd
, off
, sizeof(trx
), &len
, (char *) &trx
) ||
300 /* found a TRX header */
301 if (le32_to_cpu(trx
.magic
) == TRX_MAGIC
) {
302 part
->offset
= le32_to_cpu(trx
.offsets
[2]) ? :
303 le32_to_cpu(trx
.offsets
[1]);
304 part
->size
= le32_to_cpu(trx
.len
);
306 part
->size
-= part
->offset
;
314 "%s: Couldn't find root filesystem\n",
322 if (MTD_READ(mtd
, part
->offset
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
325 if (*((__u32
*) buf
) == SQUASHFS_MAGIC
) {
326 printk(KERN_INFO
"%s: Filesystem type: squashfs, size=0x%x\n", mtd
->name
, (u32
) sb
->bytes_used
);
328 /* Update the squashfs partition size based on the superblock info */
329 part
->size
= sb
->bytes_used
;
330 len
= part
->offset
+ part
->size
;
331 len
+= (mtd
->erasesize
- 1);
332 len
&= ~(mtd
->erasesize
- 1);
333 part
->size
= len
- part
->offset
;
334 } else if (*((__u16
*) buf
) == JFFS2_MAGIC_BITMASK
) {
335 printk(KERN_INFO
"%s: Filesystem type: jffs2\n", mtd
->name
);
337 /* Move the squashfs outside of the trx */
340 printk(KERN_INFO
"%s: Filesystem type: unknown\n", mtd
->name
);
344 if (trx
.len
!= part
->offset
+ part
->size
- off
) {
345 /* Update the trx offsets and length */
346 trx
.len
= part
->offset
+ part
->size
- off
;
348 /* Update the trx crc32 */
349 for (i
= (u32
) &(((struct trx_header
*)NULL
)->flag_version
); i
<= trx
.len
; i
+= sizeof(buf
)) {
350 if (MTD_READ(mtd
, off
+ i
, sizeof(buf
), &len
, buf
) || len
!= sizeof(buf
))
352 crc
= crc32_le(crc
, buf
, min(sizeof(buf
), trx
.len
- i
));
356 /* read first eraseblock from the trx */
357 trx2
= block
= kmalloc(mtd
->erasesize
, GFP_KERNEL
);
358 if (MTD_READ(mtd
, off
, mtd
->erasesize
, &len
, block
) || len
!= mtd
->erasesize
) {
359 printk("Error accessing the first trx eraseblock\n");
363 printk("Updating TRX offsets and length:\n");
364 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
);
365 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
);
367 /* Write updated trx header to the flash */
368 memcpy(block
, &trx
, sizeof(trx
));
370 mtd
->unlock(mtd
, off
, mtd
->erasesize
);
371 erase_write(mtd
, off
, mtd
->erasesize
, block
);
381 struct mtd_partition
* __init
382 init_mtd_partitions(struct mtd_info
*mtd
, size_t size
)
386 if ((cfe_size
= find_cfe_size(mtd
,size
)) < 0)
390 bcm947xx_parts
[0].offset
= 0;
391 bcm947xx_parts
[0].size
= cfe_size
;
394 if (cfe_size
!= 384 * 1024) {
395 bcm947xx_parts
[3].offset
= size
- ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
396 bcm947xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
398 /* nvram (old 128kb config partition on netgear wgt634u) */
399 bcm947xx_parts
[3].offset
= bcm947xx_parts
[0].size
;
400 bcm947xx_parts
[3].size
= ROUNDUP(NVRAM_SPACE
, mtd
->erasesize
);
403 /* linux (kernel and rootfs) */
404 if (cfe_size
!= 384 * 1024) {
405 bcm947xx_parts
[1].offset
= bcm947xx_parts
[0].size
;
406 bcm947xx_parts
[1].size
= bcm947xx_parts
[3].offset
-
407 bcm947xx_parts
[1].offset
;
409 /* do not count the elf loader, which is on one block */
410 bcm947xx_parts
[1].offset
= bcm947xx_parts
[0].size
+
411 bcm947xx_parts
[3].size
+ mtd
->erasesize
;
412 bcm947xx_parts
[1].size
= size
-
413 bcm947xx_parts
[0].size
-
414 (2*bcm947xx_parts
[3].size
) -
418 /* find and size rootfs */
419 if (find_root(mtd
,size
,&bcm947xx_parts
[2])==0) {
421 bcm947xx_parts
[4].name
= NULL
;
422 bcm947xx_parts
[2].size
= size
- bcm947xx_parts
[2].offset
-
423 bcm947xx_parts
[3].size
;
426 /* calculate leftover flash, and assign it to the jffs2 partition */
427 if (cfe_size
!= 384 * 1024) {
428 bcm947xx_parts
[4].offset
= bcm947xx_parts
[2].offset
+
429 bcm947xx_parts
[2].size
;
430 if ((bcm947xx_parts
[4].offset
% mtd
->erasesize
) > 0) {
431 bcm947xx_parts
[4].offset
+= mtd
->erasesize
-
432 (bcm947xx_parts
[4].offset
% mtd
->erasesize
);
434 bcm947xx_parts
[4].size
= bcm947xx_parts
[3].offset
-
435 bcm947xx_parts
[4].offset
;
437 bcm947xx_parts
[4].offset
= bcm947xx_parts
[2].offset
+
438 bcm947xx_parts
[2].size
;
439 if ((bcm947xx_parts
[4].offset
% mtd
->erasesize
) > 0) {
440 bcm947xx_parts
[4].offset
+= mtd
->erasesize
-
441 (bcm947xx_parts
[4].offset
% mtd
->erasesize
);
443 bcm947xx_parts
[4].size
= size
- bcm947xx_parts
[3].size
-
444 bcm947xx_parts
[4].offset
;
448 return bcm947xx_parts
;
454 mod_init_t
init_bcm947xx_map(void)
460 uint window_addr
= 0, window_size
= 0;
463 #ifdef CONFIG_MTD_PARTITIONS
464 struct mtd_partition
*parts
;
468 spin_lock_irqsave(&sbh_lock
, flags
);
469 coreidx
= sb_coreidx(sbh
);
471 /* Check strapping option if chipcommon exists */
472 if ((cc
= sb_setcore(sbh
, SB_CC
, 0))) {
473 fltype
= readl(&cc
->capabilities
) & CAP_FLASH_MASK
;
474 if (fltype
== PFLASH
) {
475 bcm947xx_map
.map_priv_2
= 1;
476 window_addr
= 0x1c000000;
477 bcm947xx_map
.size
= window_size
= 32 * 1024 * 1024;
478 if ((readl(&cc
->flash_config
) & CC_CFG_DS
) == 0)
479 bcm947xx_map
.buswidth
= 1;
483 bcm947xx_map
.map_priv_2
= 0;
484 window_addr
= WINDOW_ADDR
;
485 window_size
= WINDOW_SIZE
;
488 sb_setcoreidx(sbh
, coreidx
);
489 spin_unlock_irqrestore(&sbh_lock
, flags
);
491 if (fltype
!= PFLASH
) {
492 printk(KERN_ERR
"pflash: found no supported devices\n");
497 bcm947xx_map
.map_priv_1
= (unsigned long) ioremap(window_addr
, window_size
);
499 if (!bcm947xx_map
.map_priv_1
) {
500 printk(KERN_ERR
"Failed to ioremap\n");
504 if (!(bcm947xx_mtd
= do_map_probe("cfi_probe", &bcm947xx_map
))) {
505 printk(KERN_ERR
"pflash: cfi_probe failed\n");
506 iounmap((void *)bcm947xx_map
.map_priv_1
);
510 bcm947xx_mtd
->module
= THIS_MODULE
;
512 size
= bcm947xx_mtd
->size
;
514 printk(KERN_NOTICE
"Flash device: 0x%x at 0x%x\n", size
, window_addr
);
516 #ifdef CONFIG_MTD_PARTITIONS
517 parts
= init_mtd_partitions(bcm947xx_mtd
, size
);
518 for (i
= 0; parts
[i
].name
; i
++);
519 ret
= add_mtd_partitions(bcm947xx_mtd
, parts
, i
);
521 printk(KERN_ERR
"Flash: add_mtd_partitions failed\n");
530 map_destroy(bcm947xx_mtd
);
531 if (bcm947xx_map
.map_priv_1
)
532 iounmap((void *) bcm947xx_map
.map_priv_1
);
533 bcm947xx_map
.map_priv_1
= 0;
537 mod_exit_t
cleanup_bcm947xx_map(void)
539 #ifdef CONFIG_MTD_PARTITIONS
540 del_mtd_partitions(bcm947xx_mtd
);
542 map_destroy(bcm947xx_mtd
);
543 iounmap((void *) bcm947xx_map
.map_priv_1
);
544 bcm947xx_map
.map_priv_1
= 0;
547 module_init(init_bcm947xx_map
);
548 module_exit(cleanup_bcm947xx_map
);