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