brcm47xx: Add edimax PS1208mfg support
[15.05/openwrt.git] / target / linux / brcm47xx / files-2.6.34 / 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             cfe_getenv("boardnum", buf, sizeof(buf)) >= 0)
403                 boardnum = simple_strtoul(buf, NULL, 0);
404         if (nvram_getenv("boardtype", buf, sizeof(buf)) >= 0 ||
405             cfe_getenv("boardtype", buf, sizeof(buf)) >= 0)
406                 boardtype = simple_strtoul(buf, NULL, 0);
407         if (nvram_getenv("boardrev", buf, sizeof(buf)) >= 0 ||
408             cfe_getenv("boardrev", buf, sizeof(buf)) >= 0)
409                 boardrev = simple_strtoul(buf, NULL, 0);
410         if (nvram_getenv("boardflags", buf, sizeof(buf)) >= 0 ||
411             cfe_getenv("boardflags", buf, sizeof(buf)) >= 0)
412                 boardflags = simple_strtoul(buf, NULL, 0);
413         if (nvram_getenv("sdram_init", buf, sizeof(buf)) >= 0 ||
414             cfe_getenv("sdram_init", buf, sizeof(buf)) >= 0)
415                 sdram_init = simple_strtoul(buf, NULL, 0);
416         if (nvram_getenv("cardbus", buf, sizeof(buf)) >= 0 ||
417             cfe_getenv("cardbus", buf, sizeof(buf)) >= 0)
418                 cardbus = simple_strtoul(buf, NULL, 0);
419
420         if ((boardnum == 8 || boardnum == 01)
421           && boardtype == 0x0472 && cardbus == 1) {
422                 /* Netgear WNR834B, Netgear WNR834Bv2 */
423                 return ROUTER_NETGEAR_WNR834B;
424         }
425
426         if (boardnum == 01 && boardtype == 0x0472 && boardrev == 0x23) {
427                 /* Netgear WNDR-3300 */
428                 return ROUTER_NETGEAR_WNDR3300;
429         }
430
431         if ((boardnum == 83258 || boardnum == 01)
432           && boardtype == 0x048e
433           && (boardrev == 0x11 || boardrev == 0x10)
434           && boardflags == 0x750
435           && sdram_init == 0x000A) {
436                 /* Netgear WGR614v8/L/WW 16MB ram, cfe v1.3 or v1.5 */
437                 return ROUTER_NETGEAR_WGR614L;
438         }
439
440         if ((boardnum == 1 || boardnum == 3500)
441           && boardtype == 0x04CF
442           && (boardrev == 0x1213 || boardrev == 02)) {
443                 /* Netgear WNR3500v2/U/L */
444                 return ROUTER_NETGEAR_WNR3500L;
445         }
446
447         return 0;
448 }
449
450 struct mtd_partition * __init
451 init_mtd_partitions(struct mtd_info *mtd, size_t size)
452 {
453         int cfe_size;
454         int dual_image_offset = 0;
455         /* e.g Netgear 0x003e0000-0x003f0000 : "board_data", we exclude this
456          * part from our mapping to prevent overwriting len/checksum on e.g.
457          * Netgear WGR614v8/L/WW
458          */
459         int board_data_size = 0;
460
461         switch (get_router()) {
462         case ROUTER_NETGEAR_WGR614L:
463         case ROUTER_NETGEAR_WNR834B:
464         case ROUTER_NETGEAR_WNDR3300:
465         case ROUTER_NETGEAR_WNR3500L:
466                 /* Netgear: checksum is @ 0x003AFFF8 for 4M flash or checksum
467                  * is @ 0x007AFFF8 for 8M flash
468                  */
469                 board_data_size = 4 * mtd->erasesize;
470                 break;
471         }
472
473         if ((cfe_size = find_cfe_size(mtd,size)) < 0)
474                 return NULL;
475
476         /* boot loader */
477         bcm47xx_parts[0].offset = 0;
478         bcm47xx_parts[0].size   = cfe_size;
479
480         /* nvram */
481         if (cfe_size != 384 * 1024) {
482                 bcm47xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
483                 bcm47xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
484         } else {
485                 /* nvram (old 128kb config partition on netgear wgt634u) */
486                 bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
487                 bcm47xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
488         }
489
490         /* dual image offset*/
491         printk("Looking for dual image\n");
492         dual_image_offset=find_dual_image_off(mtd,size);
493         /* linux (kernel and rootfs) */
494         if (cfe_size != 384 * 1024) {
495                 bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
496                 bcm47xx_parts[1].size   = bcm47xx_parts[3].offset - dual_image_offset -
497                         bcm47xx_parts[1].offset - board_data_size;
498         } else {
499                 /* do not count the elf loader, which is on one block */
500                 bcm47xx_parts[1].offset = bcm47xx_parts[0].size + 
501                         bcm47xx_parts[3].size + mtd->erasesize;
502                 bcm47xx_parts[1].size   = size - 
503                         bcm47xx_parts[0].size - 
504                         (2*bcm47xx_parts[3].size) - 
505                         mtd->erasesize - board_data_size;
506         }
507
508         /* find and size rootfs */
509         find_root(mtd,size,&bcm47xx_parts[2]);
510         bcm47xx_parts[2].size = size - dual_image_offset -
511                                 bcm47xx_parts[2].offset -
512                                 bcm47xx_parts[3].size - board_data_size;
513
514         return bcm47xx_parts;
515 }
516 #endif
517
518 int __init init_bcm47xx_map(void)
519 {
520 #ifdef CONFIG_SSB
521         struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
522 #endif
523         size_t size;
524         int ret = 0;
525 #ifdef CONFIG_MTD_PARTITIONS
526         struct mtd_partition *parts;
527         int i;
528 #endif
529
530 #ifdef CONFIG_SSB
531         u32 window = mcore->flash_window;
532         u32 window_size = mcore->flash_window_size;
533
534         printk("flash init: 0x%08x 0x%08x\n", window, window_size);
535         bcm47xx_map.phys = window;
536         bcm47xx_map.size = window_size;
537         bcm47xx_map.bankwidth = mcore->flash_buswidth;
538         bcm47xx_map.virt = ioremap_nocache(window, window_size);
539 #else
540         printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
541         bcm47xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
542 #endif
543
544         if (!bcm47xx_map.virt) {
545                 printk("Failed to ioremap\n");
546                 return -EIO;
547         }
548
549         simple_map_init(&bcm47xx_map);
550         
551         if (!(bcm47xx_mtd = do_map_probe("cfi_probe", &bcm47xx_map))) {
552                 printk("Failed to do_map_probe\n");
553                 iounmap((void *)bcm47xx_map.virt);
554                 return -ENXIO;
555         }
556
557         /* override copy_from routine */
558         bcm47xx_map.copy_from = bcm47xx_map_copy_from;
559
560         bcm47xx_mtd->owner = THIS_MODULE;
561
562         size = bcm47xx_mtd->size;
563
564         printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
565
566 #ifdef CONFIG_MTD_PARTITIONS
567         parts = init_mtd_partitions(bcm47xx_mtd, size);
568         for (i = 0; parts[i].name; i++);
569         ret = add_mtd_partitions(bcm47xx_mtd, parts, i);
570         if (ret) {
571                 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
572                 goto fail;
573         }
574 #endif
575         return 0;
576
577  fail:
578         if (bcm47xx_mtd)
579                 map_destroy(bcm47xx_mtd);
580         if (bcm47xx_map.virt)
581                 iounmap((void *)bcm47xx_map.virt);
582         bcm47xx_map.virt = 0;
583         return ret;
584 }
585
586 void __exit cleanup_bcm47xx_map(void)
587 {
588 #ifdef CONFIG_MTD_PARTITIONS
589         del_mtd_partitions(bcm47xx_mtd);
590 #endif
591         map_destroy(bcm47xx_mtd);
592         iounmap((void *)bcm47xx_map.virt);
593 }
594
595 module_init(init_bcm47xx_map);
596 module_exit(cleanup_bcm47xx_map);