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