broadcom: rename the OpenWrt partition to rootfs_data
[openwrt/svn-archive/archive.git] / target / linux / brcm-2.4 / files / drivers / mtd / maps / bcm947xx-flash.c
1 /*
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)
5 *
6 * original functions for finding root filesystem from Mike Baker
7 *
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.
12 *
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.
23 *
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.
27 *
28 *
29 * Copyright 2004, Broadcom Corporation
30 * All Rights Reserved.
31 *
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.
36 *
37 * Flash mapping for BCM947XX boards
38 *
39 */
40
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>
49 #endif
50 #include <linux/config.h>
51 #include <linux/squashfs_fs.h>
52 #include <linux/jffs2.h>
53 #include <linux/crc32.h>
54 #include <asm/io.h>
55
56 #include <typedefs.h>
57 #include <osl.h>
58 #include <bcmnvram.h>
59 #include <bcmutils.h>
60 #include <sbconfig.h>
61 #include <sbchipc.h>
62 #include <sbutils.h>
63 #include <trxhdr.h>
64
65 /* Global SB handle */
66 extern void *bcm947xx_sbh;
67 extern spinlock_t bcm947xx_sbh_lock;
68
69 /* Convenience */
70 #define sbh bcm947xx_sbh
71 #define sbh_lock bcm947xx_sbh_lock
72
73 #define WINDOW_ADDR 0x1fc00000
74 #define WINDOW_SIZE 0x400000
75 #define BUSWIDTH 2
76
77 static struct mtd_info *bcm947xx_mtd;
78
79 __u8 bcm947xx_map_read8(struct map_info *map, unsigned long ofs)
80 {
81 if (map->map_priv_2 == 1)
82 return __raw_readb(map->map_priv_1 + ofs);
83
84 u16 val = __raw_readw(map->map_priv_1 + (ofs & ~1));
85 if (ofs & 1)
86 return ((val >> 8) & 0xff);
87 else
88 return (val & 0xff);
89 }
90
91 __u16 bcm947xx_map_read16(struct map_info *map, unsigned long ofs)
92 {
93 return __raw_readw(map->map_priv_1 + ofs);
94 }
95
96 __u32 bcm947xx_map_read32(struct map_info *map, unsigned long ofs)
97 {
98 return __raw_readl(map->map_priv_1 + ofs);
99 }
100
101 void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
102 {
103 if (len==1) {
104 memcpy_fromio(to, map->map_priv_1 + from, len);
105 } else {
106 int i;
107 u16 *dest = (u16 *) to;
108 u16 *src = (u16 *) (map->map_priv_1 + from);
109 for (i = 0; i < (len / 2); i++) {
110 dest[i] = src[i];
111 }
112 if (len & 1)
113 *((u8 *)dest+len-1) = src[i] & 0xff;
114 }
115 }
116
117 void bcm947xx_map_write8(struct map_info *map, __u8 d, unsigned long adr)
118 {
119 __raw_writeb(d, map->map_priv_1 + adr);
120 mb();
121 }
122
123 void bcm947xx_map_write16(struct map_info *map, __u16 d, unsigned long adr)
124 {
125 __raw_writew(d, map->map_priv_1 + adr);
126 mb();
127 }
128
129 void bcm947xx_map_write32(struct map_info *map, __u32 d, unsigned long adr)
130 {
131 __raw_writel(d, map->map_priv_1 + adr);
132 mb();
133 }
134
135 void bcm947xx_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
136 {
137 memcpy_toio(map->map_priv_1 + to, from, len);
138 }
139
140 struct map_info bcm947xx_map = {
141 name: "Physically mapped flash",
142 size: WINDOW_SIZE,
143 buswidth: BUSWIDTH,
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
152 };
153
154 #ifdef CONFIG_MTD_PARTITIONS
155
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: "rootfs_data", offset: 0, size: 0, },
162 { name: NULL, },
163 };
164
165 static int __init
166 find_cfe_size(struct mtd_info *mtd, size_t size)
167 {
168 struct trx_header *trx;
169 unsigned char buf[512];
170 int off;
171 size_t len;
172 int blocksize;
173
174 trx = (struct trx_header *) buf;
175
176 blocksize = mtd->erasesize;
177 if (blocksize < 0x10000)
178 blocksize = 0x10000;
179
180 for (off = (128*1024); off < size; off += blocksize) {
181 memset(buf, 0xe5, sizeof(buf));
182
183 /*
184 * Read into buffer
185 */
186 if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
187 len != sizeof(buf))
188 continue;
189
190 /* found a TRX header */
191 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
192 goto found;
193 }
194 }
195
196 printk(KERN_NOTICE
197 "%s: Couldn't find bootloader size\n",
198 mtd->name);
199 return -1;
200
201 found:
202 printk(KERN_NOTICE "bootloader size: %d\n", off);
203 return off;
204
205 }
206
207 /*
208 * Copied from mtdblock.c
209 *
210 * Cache stuff...
211 *
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.
217 */
218
219 static void erase_callback(struct erase_info *done)
220 {
221 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
222 wake_up(wait_q);
223 }
224
225 static int erase_write (struct mtd_info *mtd, unsigned long pos,
226 int len, const char *buf)
227 {
228 struct erase_info erase;
229 DECLARE_WAITQUEUE(wait, current);
230 wait_queue_head_t wait_q;
231 size_t retlen;
232 int ret;
233
234 /*
235 * First, let's erase the flash block.
236 */
237
238 init_waitqueue_head(&wait_q);
239 erase.mtd = mtd;
240 erase.callback = erase_callback;
241 erase.addr = pos;
242 erase.len = len;
243 erase.priv = (u_long)&wait_q;
244
245 set_current_state(TASK_INTERRUPTIBLE);
246 add_wait_queue(&wait_q, &wait);
247
248 ret = MTD_ERASE(mtd, &erase);
249 if (ret) {
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);
255 return ret;
256 }
257
258 schedule(); /* Wait for erase to finish. */
259 remove_wait_queue(&wait_q, &wait);
260
261 /*
262 * Next, writhe data to flash.
263 */
264
265 ret = MTD_WRITE (mtd, pos, len, &retlen, buf);
266 if (ret)
267 return ret;
268 if (retlen != len)
269 return -EIO;
270 return 0;
271 }
272
273
274
275
276 static int __init
277 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
278 {
279 struct trx_header trx, *trx2;
280 unsigned char buf[512], *block;
281 int off, blocksize;
282 u32 i, crc = ~0;
283 size_t len;
284 struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
285
286 blocksize = mtd->erasesize;
287 if (blocksize < 0x10000)
288 blocksize = 0x10000;
289
290 for (off = (128*1024); off < size; off += blocksize) {
291 memset(&trx, 0xe5, sizeof(trx));
292
293 /*
294 * Read into buffer
295 */
296 if (MTD_READ(mtd, off, sizeof(trx), &len, (char *) &trx) ||
297 len != sizeof(trx))
298 continue;
299
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);
305
306 part->size -= part->offset;
307 part->offset += off;
308
309 goto found;
310 }
311 }
312
313 printk(KERN_NOTICE
314 "%s: Couldn't find root filesystem\n",
315 mtd->name);
316 return -1;
317
318 found:
319 if (part->size == 0)
320 return 0;
321
322 if (MTD_READ(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
323 return 0;
324
325 if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
326 printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
327
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);
336
337 /* Move the squashfs outside of the trx */
338 part->size = 0;
339 } else {
340 printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
341 return 0;
342 }
343
344 if (trx.len != part->offset + part->size - off) {
345 /* Update the trx offsets and length */
346 trx.len = part->offset + part->size - off;
347
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))
351 return 0;
352 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
353 }
354 trx.crc32 = crc;
355
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");
360 return 0;
361 }
362
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);
366
367 /* Write updated trx header to the flash */
368 memcpy(block, &trx, sizeof(trx));
369 if (mtd->unlock)
370 mtd->unlock(mtd, off, mtd->erasesize);
371 erase_write(mtd, off, mtd->erasesize, block);
372 if (mtd->sync)
373 mtd->sync(mtd);
374 kfree(block);
375 printk("Done\n");
376 }
377
378 return part->size;
379 }
380
381 struct mtd_partition * __init
382 init_mtd_partitions(struct mtd_info *mtd, size_t size)
383 {
384 int cfe_size;
385
386 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
387 return NULL;
388
389 /* boot loader */
390 bcm947xx_parts[0].offset = 0;
391 bcm947xx_parts[0].size = cfe_size;
392
393 /* nvram */
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);
397 } else {
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);
401 }
402
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;
408 } else {
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) -
415 mtd->erasesize;
416 }
417
418 /* find and size rootfs */
419 if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
420 /* entirely jffs2 */
421 bcm947xx_parts[4].name = NULL;
422 bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset -
423 bcm947xx_parts[3].size;
424 } else {
425 /* legacy setup */
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);
433 }
434 bcm947xx_parts[4].size = bcm947xx_parts[3].offset -
435 bcm947xx_parts[4].offset;
436 } else {
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);
442 }
443 bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
444 bcm947xx_parts[4].offset;
445 }
446 }
447
448 return bcm947xx_parts;
449 }
450
451 #endif
452
453
454 mod_init_t init_bcm947xx_map(void)
455 {
456 ulong flags;
457 uint coreidx;
458 chipcregs_t *cc;
459 uint32 fltype;
460 uint window_addr = 0, window_size = 0;
461 size_t size;
462 int ret = 0;
463 #ifdef CONFIG_MTD_PARTITIONS
464 struct mtd_partition *parts;
465 int i;
466 #endif
467
468 spin_lock_irqsave(&sbh_lock, flags);
469 coreidx = sb_coreidx(sbh);
470
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;
480 }
481 } else {
482 fltype = PFLASH;
483 bcm947xx_map.map_priv_2 = 0;
484 window_addr = WINDOW_ADDR;
485 window_size = WINDOW_SIZE;
486 }
487
488 sb_setcoreidx(sbh, coreidx);
489 spin_unlock_irqrestore(&sbh_lock, flags);
490
491 if (fltype != PFLASH) {
492 printk(KERN_ERR "pflash: found no supported devices\n");
493 ret = -ENODEV;
494 goto fail;
495 }
496
497 bcm947xx_map.map_priv_1 = (unsigned long) ioremap(window_addr, window_size);
498
499 if (!bcm947xx_map.map_priv_1) {
500 printk(KERN_ERR "Failed to ioremap\n");
501 return -EIO;
502 }
503
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);
507 return -ENXIO;
508 }
509
510 bcm947xx_mtd->module = THIS_MODULE;
511
512 size = bcm947xx_mtd->size;
513
514 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, window_addr);
515
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);
520 if (ret) {
521 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
522 goto fail;
523 }
524 #endif
525
526 return 0;
527
528 fail:
529 if (bcm947xx_mtd)
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;
534 return ret;
535 }
536
537 mod_exit_t cleanup_bcm947xx_map(void)
538 {
539 #ifdef CONFIG_MTD_PARTITIONS
540 del_mtd_partitions(bcm947xx_mtd);
541 #endif
542 map_destroy(bcm947xx_mtd);
543 iounmap((void *) bcm947xx_map.map_priv_1);
544 bcm947xx_map.map_priv_1 = 0;
545 }
546
547 module_init(init_bcm947xx_map);
548 module_exit(cleanup_bcm947xx_map);