brcm47xx: ignore some parts of flash on some netgear devices. This
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / files-2.6.35 / drivers / mtd / maps / bcm47xx-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 * Copyright 2001-2003, Broadcom Corporation
29 * All Rights Reserved.
30 *
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.
35 *
36 * Flash mapping for BCM947XX boards
37 */
38
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>
49 #endif
50 #include <linux/crc32.h>
51 #ifdef CONFIG_SSB
52 #include <linux/ssb/ssb.h>
53 #endif
54 #include <asm/io.h>
55 #include <asm/mach-bcm47xx/nvram.h>
56 #include <asm/fw/cfe/cfe_api.h>
57
58
59 #define TRX_MAGIC 0x30524448 /* "HDR0" */
60 #define TRX_VERSION 1
61 #define TRX_MAX_LEN 0x3A0000
62 #define TRX_NO_HEADER 1 /* Do not write TRX header */
63 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
64 #define TRX_MAX_OFFSET 3
65
66 struct trx_header {
67 u32 magic; /* "HDR0" */
68 u32 len; /* Length of file including header */
69 u32 crc32; /* 32-bit CRC from flag_version to end of file */
70 u32 flag_version; /* 0:15 flags, 16:31 version */
71 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
72 };
73
74 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
75 #define NVRAM_SPACE 0x8000
76 #define WINDOW_ADDR 0x1fc00000
77 #define WINDOW_SIZE 0x400000
78 #define BUSWIDTH 2
79
80 #define ROUTER_NETGEAR_WGR614L 1
81 #define ROUTER_NETGEAR_WNR834B 2
82 #define ROUTER_NETGEAR_WNDR3300 3
83 #define ROUTER_NETGEAR_WNR3500L 4
84
85 #ifdef CONFIG_SSB
86 extern struct ssb_bus ssb_bcm47xx;
87 #endif
88 static struct mtd_info *bcm47xx_mtd;
89
90 static void bcm47xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
91 {
92 if (len==1) {
93 memcpy_fromio(to, map->virt + from, len);
94 } else {
95 int i;
96 u16 *dest = (u16 *) to;
97 u16 *src = (u16 *) (map->virt + from);
98 for (i = 0; i < (len / 2); i++) {
99 dest[i] = src[i];
100 }
101 if (len & 1)
102 *((u8 *)dest+len-1) = src[i] & 0xff;
103 }
104 }
105
106 static struct map_info bcm47xx_map = {
107 name: "Physically mapped flash",
108 size: WINDOW_SIZE,
109 bankwidth: BUSWIDTH,
110 phys: WINDOW_ADDR,
111 };
112
113 #ifdef CONFIG_MTD_PARTITIONS
114
115 static struct mtd_partition bcm47xx_parts[] = {
116 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
117 { name: "linux", offset: 0, size: 0, },
118 { name: "rootfs", offset: 0, size: 0, },
119 { name: "nvram", offset: 0, size: 0, },
120 { name: NULL, },
121 };
122
123 static int __init
124 find_cfe_size(struct mtd_info *mtd, size_t size)
125 {
126 struct trx_header *trx;
127 unsigned char buf[512];
128 int off;
129 size_t len;
130 int blocksize;
131
132 trx = (struct trx_header *) buf;
133
134 blocksize = mtd->erasesize;
135 if (blocksize < 0x10000)
136 blocksize = 0x10000;
137
138 for (off = (128*1024); off < size; off += blocksize) {
139 memset(buf, 0xe5, sizeof(buf));
140
141 /*
142 * Read into buffer
143 */
144 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
145 len != sizeof(buf))
146 continue;
147
148 /* found a TRX header */
149 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
150 goto found;
151 }
152 }
153
154 printk(KERN_NOTICE
155 "%s: Couldn't find bootloader size\n",
156 mtd->name);
157 return -1;
158
159 found:
160 printk(KERN_NOTICE "bootloader size: %d\n", off);
161 return off;
162
163 }
164
165 /*
166 * Copied from mtdblock.c
167 *
168 * Cache stuff...
169 *
170 * Since typical flash erasable sectors are much larger than what Linux's
171 * buffer cache can handle, we must implement read-modify-write on flash
172 * sectors for each block write requests. To avoid over-erasing flash sectors
173 * and to speed things up, we locally cache a whole flash sector while it is
174 * being written to until a different sector is required.
175 */
176
177 static void erase_callback(struct erase_info *done)
178 {
179 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
180 wake_up(wait_q);
181 }
182
183 static int erase_write (struct mtd_info *mtd, unsigned long pos,
184 int len, const char *buf)
185 {
186 struct erase_info erase;
187 DECLARE_WAITQUEUE(wait, current);
188 wait_queue_head_t wait_q;
189 size_t retlen;
190 int ret;
191
192 /*
193 * First, let's erase the flash block.
194 */
195
196 init_waitqueue_head(&wait_q);
197 erase.mtd = mtd;
198 erase.callback = erase_callback;
199 erase.addr = pos;
200 erase.len = len;
201 erase.priv = (u_long)&wait_q;
202
203 set_current_state(TASK_INTERRUPTIBLE);
204 add_wait_queue(&wait_q, &wait);
205
206 ret = mtd->erase(mtd, &erase);
207 if (ret) {
208 set_current_state(TASK_RUNNING);
209 remove_wait_queue(&wait_q, &wait);
210 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
211 "on \"%s\" failed\n",
212 pos, len, mtd->name);
213 return ret;
214 }
215
216 schedule(); /* Wait for erase to finish. */
217 remove_wait_queue(&wait_q, &wait);
218
219 /*
220 * Next, writhe data to flash.
221 */
222
223 ret = mtd->write (mtd, pos, len, &retlen, buf);
224 if (ret)
225 return ret;
226 if (retlen != len)
227 return -EIO;
228 return 0;
229 }
230
231
232 static int __init
233 find_dual_image_off (struct mtd_info *mtd, size_t size)
234 {
235 struct trx_header trx;
236 int off, blocksize;
237 size_t len;
238
239 blocksize = mtd->erasesize;
240 if (blocksize < 0x10000)
241 blocksize = 0x10000;
242
243 for (off = (128*1024); off < size; off += blocksize) {
244 memset(&trx, 0xe5, sizeof(trx));
245 /*
246 * Read into buffer
247 */
248 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
249 len != sizeof(trx))
250 continue;
251 /* found last TRX header */
252 if (le32_to_cpu(trx.magic) == TRX_MAGIC){
253 if (le32_to_cpu(trx.flag_version >> 16)==2){
254 printk("dual image TRX header found\n");
255 return size/2;
256 } else {
257 return 0;
258 }
259 }
260 }
261 return 0;
262 }
263
264
265 static int __init
266 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
267 {
268 struct trx_header trx, *trx2;
269 unsigned char buf[512], *block;
270 int off, blocksize;
271 u32 i, crc = ~0;
272 size_t len;
273
274 blocksize = mtd->erasesize;
275 if (blocksize < 0x10000)
276 blocksize = 0x10000;
277
278 for (off = (128*1024); off < size; off += blocksize) {
279 memset(&trx, 0xe5, sizeof(trx));
280
281 /*
282 * Read into buffer
283 */
284 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
285 len != sizeof(trx))
286 continue;
287
288 /* found a TRX header */
289 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
290 part->offset = le32_to_cpu(trx.offsets[2]) ? :
291 le32_to_cpu(trx.offsets[1]);
292 part->size = le32_to_cpu(trx.len);
293
294 part->size -= part->offset;
295 part->offset += off;
296
297 goto found;
298 }
299 }
300
301 printk(KERN_NOTICE
302 "%s: Couldn't find root filesystem\n",
303 mtd->name);
304 return -1;
305
306 found:
307 if (part->size == 0)
308 return 0;
309
310 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
311 return 0;
312
313 /* Move the fs outside of the trx */
314 part->size = 0;
315
316 if (trx.len != part->offset + part->size - off) {
317 /* Update the trx offsets and length */
318 trx.len = part->offset + part->size - off;
319
320 /* Update the trx crc32 */
321 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
322 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
323 return 0;
324 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
325 }
326 trx.crc32 = crc;
327
328 /* read first eraseblock from the trx */
329 block = kmalloc(mtd->erasesize, GFP_KERNEL);
330 trx2 = (struct trx_header *) block;
331 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
332 printk("Error accessing the first trx eraseblock\n");
333 return 0;
334 }
335
336 printk("Updating TRX offsets and length:\n");
337 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);
338 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);
339
340 /* Write updated trx header to the flash */
341 memcpy(block, &trx, sizeof(trx));
342 if (mtd->unlock)
343 mtd->unlock(mtd, off, mtd->erasesize);
344 erase_write(mtd, off, mtd->erasesize, block);
345 if (mtd->sync)
346 mtd->sync(mtd);
347 kfree(block);
348 printk("Done\n");
349 }
350
351 return part->size;
352 }
353
354 static int get_router(void)
355 {
356 char buf[20];
357 u32 boardnum = 0;
358 u16 boardtype = 0;
359 u16 boardrev = 0;
360 u32 boardflags = 0;
361 u16 sdram_init = 0;
362 u16 cardbus = 0;
363
364 if (nvram_getenv("boardnum", buf, sizeof(buf)) >= 0 ||
365 cfe_getenv("boardnum", buf, sizeof(buf)) >= 0)
366 boardnum = simple_strtoul(buf, NULL, 0);
367 if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0 ||
368 cfe_getenv("boardtype", buf, sizeof(buf)) >= 0)
369 boardtype = simple_strtoul(buf, NULL, 0);
370 if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0 ||
371 cfe_getenv("boardrev", buf, sizeof(buf)) >= 0)
372 boardrev = simple_strtoul(buf, NULL, 0);
373 if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0 ||
374 cfe_getenv("boardflags", buf, sizeof(buf)) >= 0)
375 boardflags = simple_strtoul(buf, NULL, 0);
376 if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0 ||
377 cfe_getenv("sdram_init", buf, sizeof(buf)) >= 0)
378 sdram_init = simple_strtoul(buf, NULL, 0);
379 if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0 ||
380 cfe_getenv("cardbus", buf, sizeof(buf)) >= 0)
381 cardbus = simple_strtoul(buf, NULL, 0);
382
383 if ((boardnum == 8 || boardnum == 01)
384 && boardtype == 0x0472 && cardbus == 1) {
385 /* Netgear WNR834B, Netgear WNR834Bv2 */
386 return ROUTER_NETGEAR_WNR834B;
387 }
388
389 if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
390 /* Netgear WNDR-3300 */
391 return ROUTER_NETGEAR_WNDR3300;
392 }
393
394 if ((boardnum == 83258 || boardnum == 01)
395 && boardtype == 0x048e
396 && (boardrev == 0x11 || boardrev == 0x10)
397 && boardflags == 0x750
398 && sdram_init == 0x000A) {
399 /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
400 return ROUTER_NETGEAR_WGR614L;
401 }
402
403 if ((boardnum == 1 || boardnum == 3500)
404 && boardtype == 0x04CF
405 && (boardrev == 0x1213 || boardrev == 02)) {
406 /* Netgear WNR3500v2/U/L */
407 return ROUTER_NETGEAR_WNR3500L;
408 }
409
410 return 0;
411 }
412
413 struct mtd_partition * __init
414 init_mtd_partitions(struct mtd_info *mtd, size_t size)
415 {
416 int cfe_size;
417 int dual_image_offset = 0;
418 /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
419 * part from our mapping to prevent overwriting len/checksum on e.g.
420 * Netgear WGR614v8/L/WW
421 */
422 int board_data_size = 0;
423
424 switch (get_router()) {
425 case ROUTER_NETGEAR_WGR614L:
426 case ROUTER_NETGEAR_WNR834B:
427 case ROUTER_NETGEAR_WNDR3300:
428 case ROUTER_NETGEAR_WNR3500L:
429 /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
430 * is @ 0x007AFFF8 for 8M flash
431 */
432 board_data_size = 4 * mtd->erasesize;
433 break;
434 }
435
436 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
437 return NULL;
438
439 /* boot loader */
440 bcm47xx_parts[0].offset = 0;
441 bcm47xx_parts[0].size = cfe_size;
442
443 /* nvram */
444 if (cfe_size != 384 * 1024) {
445 bcm47xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
446 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
447 } else {
448 /* nvram (old 128kb config partition on netgear wgt634u) */
449 bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
450 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
451 }
452
453 /* dual image offset*/
454 printk("Looking for dual image\n");
455 dual_image_offset=find_dual_image_off(mtd,size);
456 /* linux (kernel and rootfs) */
457 if (cfe_size != 384 * 1024) {
458 bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
459 bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
460 bcm47xx_parts[1].offset - board_data_size;
461 } else {
462 /* do not count the elf loader, which is on one block */
463 bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
464 bcm47xx_parts[3].size + mtd->erasesize;
465 bcm47xx_parts[1].size = size -
466 bcm47xx_parts[0].size -
467 (2*bcm47xx_parts[3].size) -
468 mtd->erasesize - board_data_size;
469 }
470
471 /* find and size rootfs */
472 find_root(mtd,size,&bcm47xx_parts[2]);
473 bcm47xx_parts[2].size = size - dual_image_offset -
474 bcm47xx_parts[2].offset -
475 bcm47xx_parts[3].size - board_data_size;
476
477 return bcm47xx_parts;
478 }
479 #endif
480
481 int __init init_bcm47xx_map(void)
482 {
483 #ifdef CONFIG_SSB
484 struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
485 #endif
486 size_t size;
487 int ret = 0;
488 #ifdef CONFIG_MTD_PARTITIONS
489 struct mtd_partition *parts;
490 int i;
491 #endif
492
493 #ifdef CONFIG_SSB
494 u32 window = mcore->flash_window;
495 u32 window_size = mcore->flash_window_size;
496
497 printk("flash init: 0x%08x 0x%08x\n", window, window_size);
498 bcm47xx_map.phys = window;
499 bcm47xx_map.size = window_size;
500 bcm47xx_map.bankwidth = mcore->flash_buswidth;
501 bcm47xx_map.virt = ioremap_nocache(window, window_size);
502 #else
503 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
504 bcm47xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
505 #endif
506
507 if (!bcm47xx_map.virt) {
508 printk("Failed to ioremap\n");
509 return -EIO;
510 }
511
512 simple_map_init(&bcm47xx_map);
513
514 if (!(bcm47xx_mtd = do_map_probe("cfi_probe", &bcm47xx_map))) {
515 printk("Failed to do_map_probe\n");
516 iounmap((void *)bcm47xx_map.virt);
517 return -ENXIO;
518 }
519
520 /* override copy_from routine */
521 bcm47xx_map.copy_from = bcm47xx_map_copy_from;
522
523 bcm47xx_mtd->owner = THIS_MODULE;
524
525 size = bcm47xx_mtd->size;
526
527 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
528
529 #ifdef CONFIG_MTD_PARTITIONS
530 parts = init_mtd_partitions(bcm47xx_mtd, size);
531 for (i = 0; parts[i].name; i++);
532 ret = add_mtd_partitions(bcm47xx_mtd, parts, i);
533 if (ret) {
534 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
535 goto fail;
536 }
537 #endif
538 return 0;
539
540 fail:
541 if (bcm47xx_mtd)
542 map_destroy(bcm47xx_mtd);
543 if (bcm47xx_map.virt)
544 iounmap((void *)bcm47xx_map.virt);
545 bcm47xx_map.virt = 0;
546 return ret;
547 }
548
549 void __exit cleanup_bcm47xx_map(void)
550 {
551 #ifdef CONFIG_MTD_PARTITIONS
552 del_mtd_partitions(bcm47xx_mtd);
553 #endif
554 map_destroy(bcm47xx_mtd);
555 iounmap((void *)bcm47xx_map.virt);
556 }
557
558 module_init(init_bcm47xx_map);
559 module_exit(cleanup_bcm47xx_map);