convert brcm-2.4 to the new target structure
[openwrt/staging/yousong.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: 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 /* Move the filesystem outside of the trx */
325 part->size = 0;
326
327 if (trx.len != part->offset + part->size - off) {
328 /* Update the trx offsets and length */
329 trx.len = part->offset + part->size - off;
330
331 /* Update the trx crc32 */
332 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
333 if (MTD_READ(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
334 return 0;
335 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
336 }
337 trx.crc32 = crc;
338
339 /* read first eraseblock from the trx */
340 trx2 = block = kmalloc(mtd->erasesize, GFP_KERNEL);
341 if (MTD_READ(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
342 printk("Error accessing the first trx eraseblock\n");
343 return 0;
344 }
345
346 printk("Updating TRX offsets and length:\n");
347 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);
348 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);
349
350 /* Write updated trx header to the flash */
351 memcpy(block, &trx, sizeof(trx));
352 if (mtd->unlock)
353 mtd->unlock(mtd, off, mtd->erasesize);
354 erase_write(mtd, off, mtd->erasesize, block);
355 if (mtd->sync)
356 mtd->sync(mtd);
357 kfree(block);
358 printk("Done\n");
359 }
360
361 return part->size;
362 }
363
364 struct mtd_partition * __init
365 init_mtd_partitions(struct mtd_info *mtd, size_t size)
366 {
367 int cfe_size;
368
369 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
370 return NULL;
371
372 /* boot loader */
373 bcm947xx_parts[0].offset = 0;
374 bcm947xx_parts[0].size = cfe_size;
375
376 /* nvram */
377 if (cfe_size != 384 * 1024) {
378 bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
379 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
380 } else {
381 /* nvram (old 128kb config partition on netgear wgt634u) */
382 bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
383 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
384 }
385
386 /* linux (kernel and rootfs) */
387 if (cfe_size != 384 * 1024) {
388 bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
389 bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
390 bcm947xx_parts[1].offset;
391 } else {
392 /* do not count the elf loader, which is on one block */
393 bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
394 bcm947xx_parts[3].size + mtd->erasesize;
395 bcm947xx_parts[1].size = size -
396 bcm947xx_parts[0].size -
397 (2*bcm947xx_parts[3].size) -
398 mtd->erasesize;
399 }
400
401 find_root(mtd,size,&bcm947xx_parts[2]);
402 bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - bcm947xx_parts[3].size;
403
404 return bcm947xx_parts;
405 }
406
407 #endif
408
409
410 mod_init_t init_bcm947xx_map(void)
411 {
412 ulong flags;
413 uint coreidx;
414 chipcregs_t *cc;
415 uint32 fltype;
416 uint window_addr = 0, window_size = 0;
417 size_t size;
418 int ret = 0;
419 #ifdef CONFIG_MTD_PARTITIONS
420 struct mtd_partition *parts;
421 int i;
422 #endif
423
424 spin_lock_irqsave(&sbh_lock, flags);
425 coreidx = sb_coreidx(sbh);
426
427 /* Check strapping option if chipcommon exists */
428 if ((cc = sb_setcore(sbh, SB_CC, 0))) {
429 fltype = readl(&cc->capabilities) & CAP_FLASH_MASK;
430 if (fltype == PFLASH) {
431 bcm947xx_map.map_priv_2 = 1;
432 window_addr = 0x1c000000;
433 bcm947xx_map.size = window_size = 32 * 1024 * 1024;
434 if ((readl(&cc->flash_config) & CC_CFG_DS) == 0)
435 bcm947xx_map.buswidth = 1;
436 }
437 } else {
438 fltype = PFLASH;
439 bcm947xx_map.map_priv_2 = 0;
440 window_addr = WINDOW_ADDR;
441 window_size = WINDOW_SIZE;
442 }
443
444 sb_setcoreidx(sbh, coreidx);
445 spin_unlock_irqrestore(&sbh_lock, flags);
446
447 if (fltype != PFLASH) {
448 printk(KERN_ERR "pflash: found no supported devices\n");
449 ret = -ENODEV;
450 goto fail;
451 }
452
453 bcm947xx_map.map_priv_1 = (unsigned long) ioremap(window_addr, window_size);
454
455 if (!bcm947xx_map.map_priv_1) {
456 printk(KERN_ERR "Failed to ioremap\n");
457 return -EIO;
458 }
459
460 if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
461 printk(KERN_ERR "pflash: cfi_probe failed\n");
462 iounmap((void *)bcm947xx_map.map_priv_1);
463 return -ENXIO;
464 }
465
466 bcm947xx_mtd->module = THIS_MODULE;
467
468 size = bcm947xx_mtd->size;
469
470 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, window_addr);
471
472 #ifdef CONFIG_MTD_PARTITIONS
473 parts = init_mtd_partitions(bcm947xx_mtd, size);
474 for (i = 0; parts[i].name; i++);
475 ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
476 if (ret) {
477 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
478 goto fail;
479 }
480 #endif
481
482 return 0;
483
484 fail:
485 if (bcm947xx_mtd)
486 map_destroy(bcm947xx_mtd);
487 if (bcm947xx_map.map_priv_1)
488 iounmap((void *) bcm947xx_map.map_priv_1);
489 bcm947xx_map.map_priv_1 = 0;
490 return ret;
491 }
492
493 mod_exit_t cleanup_bcm947xx_map(void)
494 {
495 #ifdef CONFIG_MTD_PARTITIONS
496 del_mtd_partitions(bcm947xx_mtd);
497 #endif
498 map_destroy(bcm947xx_mtd);
499 iounmap((void *) bcm947xx_map.map_priv_1);
500 bcm947xx_map.map_priv_1 = 0;
501 }
502
503 module_init(init_bcm947xx_map);
504 module_exit(cleanup_bcm947xx_map);