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>
57 #define TRX_MAGIC 0x30524448 /* "HDR0" */
59 #define TRX_MAX_LEN 0x3A0000
60 #define TRX_NO_HEADER 1 /* Do not write TRX header */
61 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
62 #define TRX_MAX_OFFSET 3
65 u32 magic; /* "HDR0" */
66 u32 len; /* Length of file including header */
67 u32 crc32; /* 32-bit CRC from flag_version to end of file */
68 u32 flag_version; /* 0:15 flags, 16:31 version */
69 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
72 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
73 #define NVRAM_SPACE 0x8000
74 #define WINDOW_ADDR 0x1fc00000
75 #define WINDOW_SIZE 0x400000
79 extern struct ssb_bus ssb_bcm47xx;
81 static struct mtd_info *bcm47xx_mtd;
83 static void bcm47xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
86 memcpy_fromio(to, map->virt + from, len);
89 u16 *dest = (u16 *) to;
90 u16 *src = (u16 *) (map->virt + from);
91 for (i = 0; i < (len / 2); i++) {
95 *((u8 *)dest+len-1) = src[i] & 0xff;
99 static struct map_info bcm47xx_map = {
100 name: "Physically mapped flash",
106 #ifdef CONFIG_MTD_PARTITIONS
108 static struct mtd_partition bcm47xx_parts[] = {
109 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
110 { name: "linux", offset: 0, size: 0, },
111 { name: "rootfs", offset: 0, size: 0, },
112 { name: "nvram", offset: 0, size: 0, },
117 find_cfe_size(struct mtd_info *mtd, size_t size)
119 struct trx_header *trx;
120 unsigned char buf[512];
125 trx = (struct trx_header *) buf;
127 blocksize = mtd->erasesize;
128 if (blocksize < 0x10000)
131 for (off = (128*1024); off < size; off += blocksize) {
132 memset(buf, 0xe5, sizeof(buf));
137 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
141 /* found a TRX header */
142 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
148 "%s: Couldn't find bootloader size\n",
153 printk(KERN_NOTICE "bootloader size: %d\n", off);
159 * Copied from mtdblock.c
163 * Since typical flash erasable sectors are much larger than what Linux's
164 * buffer cache can handle, we must implement read-modify-write on flash
165 * sectors for each block write requests. To avoid over-erasing flash sectors
166 * and to speed things up, we locally cache a whole flash sector while it is
167 * being written to until a different sector is required.
170 static void erase_callback(struct erase_info *done)
172 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
176 static int erase_write (struct mtd_info *mtd, unsigned long pos,
177 int len, const char *buf)
179 struct erase_info erase;
180 DECLARE_WAITQUEUE(wait, current);
181 wait_queue_head_t wait_q;
186 * First, let's erase the flash block.
189 init_waitqueue_head(&wait_q);
191 erase.callback = erase_callback;
194 erase.priv = (u_long)&wait_q;
196 set_current_state(TASK_INTERRUPTIBLE);
197 add_wait_queue(&wait_q, &wait);
199 ret = mtd->erase(mtd, &erase);
201 set_current_state(TASK_RUNNING);
202 remove_wait_queue(&wait_q, &wait);
203 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
204 "on \"%s\" failed\n",
205 pos, len, mtd->name);
209 schedule(); /* Wait for erase to finish. */
210 remove_wait_queue(&wait_q, &wait);
213 * Next, writhe data to flash.
216 ret = mtd->write (mtd, pos, len, &retlen, buf);
228 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
230 struct trx_header trx, *trx2;
231 unsigned char buf[512], *block;
235 struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
237 blocksize = mtd->erasesize;
238 if (blocksize < 0x10000)
241 for (off = (128*1024); off < size; off += blocksize) {
242 memset(&trx, 0xe5, sizeof(trx));
247 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
251 /* found a TRX header */
252 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
253 part->offset = le32_to_cpu(trx.offsets[2]) ? :
254 le32_to_cpu(trx.offsets[1]);
255 part->size = le32_to_cpu(trx.len);
257 part->size -= part->offset;
265 "%s: Couldn't find root filesystem\n",
273 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
276 /* Move the fs outside of the trx */
279 if (trx.len != part->offset + part->size - off) {
280 /* Update the trx offsets and length */
281 trx.len = part->offset + part->size - off;
283 /* Update the trx crc32 */
284 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
285 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
287 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
291 /* read first eraseblock from the trx */
292 block = kmalloc(mtd->erasesize, GFP_KERNEL);
293 trx2 = (struct trx_header *) block;
294 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
295 printk("Error accessing the first trx eraseblock\n");
299 printk("Updating TRX offsets and length:\n");
300 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);
301 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);
303 /* Write updated trx header to the flash */
304 memcpy(block, &trx, sizeof(trx));
306 mtd->unlock(mtd, off, mtd->erasesize);
307 erase_write(mtd, off, mtd->erasesize, block);
317 struct mtd_partition * __init
318 init_mtd_partitions(struct mtd_info *mtd, size_t size)
322 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
326 bcm47xx_parts[0].offset = 0;
327 bcm47xx_parts[0].size = cfe_size;
330 if (cfe_size != 384 * 1024) {
331 bcm47xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
332 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
334 /* nvram (old 128kb config partition on netgear wgt634u) */
335 bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
336 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
339 /* linux (kernel and rootfs) */
340 if (cfe_size != 384 * 1024) {
341 bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
342 bcm47xx_parts[1].size = bcm47xx_parts[3].offset -
343 bcm47xx_parts[1].offset;
345 /* do not count the elf loader, which is on one block */
346 bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
347 bcm47xx_parts[3].size + mtd->erasesize;
348 bcm47xx_parts[1].size = size -
349 bcm47xx_parts[0].size -
350 (2*bcm47xx_parts[3].size) -
354 /* find and size rootfs */
355 find_root(mtd,size,&bcm47xx_parts[2]);
356 bcm47xx_parts[2].size = size - bcm47xx_parts[2].offset - bcm47xx_parts[3].size;
358 return bcm47xx_parts;
362 int __init init_bcm47xx_map(void)
365 struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
369 #ifdef CONFIG_MTD_PARTITIONS
370 struct mtd_partition *parts;
375 u32 window = mcore->flash_window;
376 u32 window_size = mcore->flash_window_size;
378 printk("flash init: 0x%08x 0x%08x\n", window, window_size);
379 bcm47xx_map.phys = window;
380 bcm47xx_map.size = window_size;
381 bcm47xx_map.bankwidth = mcore->flash_buswidth;
382 bcm47xx_map.virt = ioremap_nocache(window, window_size);
384 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
385 bcm47xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
388 if (!bcm47xx_map.virt) {
389 printk("Failed to ioremap\n");
393 simple_map_init(&bcm47xx_map);
395 if (!(bcm47xx_mtd = do_map_probe("cfi_probe", &bcm47xx_map))) {
396 printk("Failed to do_map_probe\n");
397 iounmap((void *)bcm47xx_map.virt);
401 /* override copy_from routine */
402 bcm47xx_map.copy_from = bcm47xx_map_copy_from;
404 bcm47xx_mtd->owner = THIS_MODULE;
406 size = bcm47xx_mtd->size;
408 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
410 #ifdef CONFIG_MTD_PARTITIONS
411 parts = init_mtd_partitions(bcm47xx_mtd, size);
412 for (i = 0; parts[i].name; i++);
413 ret = add_mtd_partitions(bcm47xx_mtd, parts, i);
415 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
423 map_destroy(bcm47xx_mtd);
424 if (bcm47xx_map.virt)
425 iounmap((void *)bcm47xx_map.virt);
426 bcm47xx_map.virt = 0;
430 void __exit cleanup_bcm47xx_map(void)
432 #ifdef CONFIG_MTD_PARTITIONS
433 del_mtd_partitions(bcm47xx_mtd);
435 map_destroy(bcm47xx_mtd);
436 iounmap((void *)bcm47xx_map.virt);
439 module_init(init_bcm47xx_map);
440 module_exit(cleanup_bcm47xx_map);