50fff896f171a6981fa077bfd5594dc584b901f8
[librecmc/librecmc.git] / target / linux / brcm47xx-2.6 / 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/wait.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/map.h>
46 #ifdef CONFIG_MTD_PARTITIONS
47 #include <linux/mtd/partitions.h>
48 #endif
49 #include <linux/squashfs_fs.h>
50 #include <linux/jffs2.h>
51 #include <linux/crc32.h>
52 #include <linux/ssb/ssb.h>
53 #include <asm/io.h>
54
55
56 #define TRX_MAGIC       0x30524448      /* "HDR0" */
57 #define TRX_VERSION     1
58 #define TRX_MAX_LEN     0x3A0000
59 #define TRX_NO_HEADER   1               /* Do not write TRX header */   
60 #define TRX_GZ_FILES    0x2     /* Contains up to TRX_MAX_OFFSET individual gzip files */
61 #define TRX_MAX_OFFSET  3
62
63 struct trx_header {
64         u32 magic;              /* "HDR0" */
65         u32 len;                /* Length of file including header */
66         u32 crc32;              /* 32-bit CRC from flag_version to end of file */
67         u32 flag_version;       /* 0:15 flags, 16:31 version */
68         u32 offsets[TRX_MAX_OFFSET];    /* Offsets of partitions from start of header */
69 };
70
71 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
72 #define NVRAM_SPACE 0x8000
73 #define WINDOW_ADDR 0x1fc00000
74 #define WINDOW_SIZE 0x400000
75 #define BUSWIDTH 2
76
77 extern struct ssb_bus ssb;
78 static struct mtd_info *bcm947xx_mtd;
79
80 static struct map_info bcm947xx_map = {
81         name: "Physically mapped flash",
82         size: WINDOW_SIZE,
83         bankwidth: BUSWIDTH,
84         phys: WINDOW_ADDR,
85 };
86
87 #ifdef CONFIG_MTD_PARTITIONS
88
89 static struct mtd_partition bcm947xx_parts[] = {
90         { name: "cfe",  offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
91         { name: "linux", offset: 0, size: 0, },
92         { name: "rootfs", offset: 0, size: 0, },
93         { name: "nvram", offset: 0, size: 0, },
94         { name: "OpenWrt", offset: 0, size: 0, },
95         { name: NULL, },
96 };
97
98 static int __init
99 find_cfe_size(struct mtd_info *mtd, size_t size)
100 {
101         struct trx_header *trx;
102         unsigned char buf[512];
103         int off;
104         size_t len;
105         int blocksize;
106
107         trx = (struct trx_header *) buf;
108
109         blocksize = mtd->erasesize;
110         if (blocksize < 0x10000)
111                 blocksize = 0x10000;
112
113         for (off = (128*1024); off < size; off += blocksize) {
114                 memset(buf, 0xe5, sizeof(buf));
115
116                 /*
117                  * Read into buffer 
118                  */
119                 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
120                     len != sizeof(buf))
121                         continue;
122
123                 /* found a TRX header */
124                 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
125                         goto found;
126                 }
127         }
128
129         printk(KERN_NOTICE
130                "%s: Couldn't find bootloader size\n",
131                mtd->name);
132         return -1;
133
134  found:
135         printk(KERN_NOTICE "bootloader size: %d\n", off);
136         return off;
137
138 }
139
140 /*
141  * Copied from mtdblock.c
142  *
143  * Cache stuff...
144  * 
145  * Since typical flash erasable sectors are much larger than what Linux's
146  * buffer cache can handle, we must implement read-modify-write on flash
147  * sectors for each block write requests.  To avoid over-erasing flash sectors
148  * and to speed things up, we locally cache a whole flash sector while it is
149  * being written to until a different sector is required.
150  */
151
152 static void erase_callback(struct erase_info *done)
153 {
154         wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
155         wake_up(wait_q);
156 }
157
158 static int erase_write (struct mtd_info *mtd, unsigned long pos, 
159                         int len, const char *buf)
160 {
161         struct erase_info erase;
162         DECLARE_WAITQUEUE(wait, current);
163         wait_queue_head_t wait_q;
164         size_t retlen;
165         int ret;
166
167         /*
168          * First, let's erase the flash block.
169          */
170
171         init_waitqueue_head(&wait_q);
172         erase.mtd = mtd;
173         erase.callback = erase_callback;
174         erase.addr = pos;
175         erase.len = len;
176         erase.priv = (u_long)&wait_q;
177
178         set_current_state(TASK_INTERRUPTIBLE);
179         add_wait_queue(&wait_q, &wait);
180
181         ret = mtd->erase(mtd, &erase);
182         if (ret) {
183                 set_current_state(TASK_RUNNING);
184                 remove_wait_queue(&wait_q, &wait);
185                 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
186                                      "on \"%s\" failed\n",
187                         pos, len, mtd->name);
188                 return ret;
189         }
190
191         schedule();  /* Wait for erase to finish. */
192         remove_wait_queue(&wait_q, &wait);
193
194         /*
195          * Next, writhe data to flash.
196          */
197
198         ret = mtd->write (mtd, pos, len, &retlen, buf);
199         if (ret)
200                 return ret;
201         if (retlen != len)
202                 return -EIO;
203         return 0;
204 }
205
206
207
208
209 static int __init
210 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
211 {
212         struct trx_header trx, *trx2;
213         unsigned char buf[512], *block;
214         int off, blocksize;
215         u32 i, crc = ~0;
216         size_t len;
217         struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
218
219         blocksize = mtd->erasesize;
220         if (blocksize < 0x10000)
221                 blocksize = 0x10000;
222
223         for (off = (128*1024); off < size; off += blocksize) {
224                 memset(&trx, 0xe5, sizeof(trx));
225
226                 /*
227                  * Read into buffer 
228                  */
229                 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
230                     len != sizeof(trx))
231                         continue;
232
233                 /* found a TRX header */
234                 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
235                         part->offset = le32_to_cpu(trx.offsets[2]) ? : 
236                                 le32_to_cpu(trx.offsets[1]);
237                         part->size = le32_to_cpu(trx.len); 
238
239                         part->size -= part->offset;
240                         part->offset += off;
241
242                         goto found;
243                 }
244         }
245
246         printk(KERN_NOTICE
247                "%s: Couldn't find root filesystem\n",
248                mtd->name);
249         return -1;
250
251  found:
252         if (part->size == 0)
253                 return 0;
254         
255         if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
256                 return 0;
257
258         if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
259                 printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
260
261                 /* Update the squashfs partition size based on the superblock info */
262                 part->size = sb->bytes_used;
263                 len = part->offset + part->size;
264                 len +=  (mtd->erasesize - 1);
265                 len &= ~(mtd->erasesize - 1);
266                 part->size = len - part->offset;
267         } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
268                 printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
269
270                 /* Move the squashfs outside of the trx */
271                 part->size = 0;
272         } else {
273                 printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
274                 return 0;
275         }
276
277         if (trx.len != part->offset + part->size - off) {
278                 /* Update the trx offsets and length */
279                 trx.len = part->offset + part->size - off;
280         
281                 /* Update the trx crc32 */
282                 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
283                         if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
284                                 return 0;
285                         crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
286                 }
287                 trx.crc32 = crc;
288
289                 /* read first eraseblock from the trx */
290                 block = kmalloc(mtd->erasesize, GFP_KERNEL);
291                 trx2 = (struct trx_header *) block;
292                 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
293                         printk("Error accessing the first trx eraseblock\n");
294                         return 0;
295                 }
296                 
297                 printk("Updating TRX offsets and length:\n");
298                 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);
299                 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);
300
301                 /* Write updated trx header to the flash */
302                 memcpy(block, &trx, sizeof(trx));
303                 if (mtd->unlock)
304                         mtd->unlock(mtd, off, mtd->erasesize);
305                 erase_write(mtd, off, mtd->erasesize, block);
306                 if (mtd->sync)
307                         mtd->sync(mtd);
308                 kfree(block);
309                 printk("Done\n");
310         }
311         
312         return part->size;
313 }
314
315 struct mtd_partition * __init
316 init_mtd_partitions(struct mtd_info *mtd, size_t size)
317 {
318         int cfe_size;
319
320         if ((cfe_size = find_cfe_size(mtd,size)) < 0)
321                 return NULL;
322
323         /* boot loader */
324         bcm947xx_parts[0].offset = 0;
325         bcm947xx_parts[0].size   = cfe_size;
326
327         /* nvram */
328         if (cfe_size != 384 * 1024) {
329                 bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
330                 bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
331         } else {
332                 /* nvram (old 128kb config partition on netgear wgt634u) */
333                 bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
334                 bcm947xx_parts[3].size   = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
335         }
336
337         /* linux (kernel and rootfs) */
338         if (cfe_size != 384 * 1024) {
339                 bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
340                 bcm947xx_parts[1].size   = bcm947xx_parts[3].offset - 
341                         bcm947xx_parts[1].offset;
342         } else {
343                 /* do not count the elf loader, which is on one block */
344                 bcm947xx_parts[1].offset = bcm947xx_parts[0].size + 
345                         bcm947xx_parts[3].size + mtd->erasesize;
346                 bcm947xx_parts[1].size   = size - 
347                         bcm947xx_parts[0].size - 
348                         (2*bcm947xx_parts[3].size) - 
349                         mtd->erasesize;
350         }
351
352         /* find and size rootfs */
353         if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
354                 /* entirely jffs2 */
355                 bcm947xx_parts[4].name = NULL;
356                 bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - 
357                                 bcm947xx_parts[3].size;
358         } else {
359                 /* legacy setup */
360                 /* calculate leftover flash, and assign it to the jffs2 partition */
361                 if (cfe_size != 384 * 1024) {
362                         bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
363                                 bcm947xx_parts[2].size;
364                         if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
365                                 bcm947xx_parts[4].offset += mtd->erasesize - 
366                                         (bcm947xx_parts[4].offset % mtd->erasesize);
367                         }
368                         bcm947xx_parts[4].size = bcm947xx_parts[3].offset - 
369                                 bcm947xx_parts[4].offset;
370                 } else {
371                         bcm947xx_parts[4].offset = bcm947xx_parts[2].offset + 
372                                 bcm947xx_parts[2].size;
373                         if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
374                                 bcm947xx_parts[4].offset += mtd->erasesize - 
375                                         (bcm947xx_parts[4].offset % mtd->erasesize);
376                         }
377                         bcm947xx_parts[4].size = size - bcm947xx_parts[3].size - 
378                                 bcm947xx_parts[4].offset;
379                 }
380         }
381
382         return bcm947xx_parts;
383 }
384 #endif
385
386 int __init init_bcm947xx_map(void)
387 {
388         struct ssb_mipscore *mcore = &ssb.mipscore;
389         size_t size;
390         int ret = 0;
391 #ifdef CONFIG_MTD_PARTITIONS
392         struct mtd_partition *parts;
393         int i;
394 #endif
395         u32 window = mcore->flash_window;
396         u32 window_size = mcore->flash_window_size;
397
398         printk("flash init: 0x%08x 0x%08x\n", window, window_size);
399         bcm947xx_map.phys = window;
400         bcm947xx_map.size = window_size;
401         bcm947xx_map.virt = ioremap_nocache(window, window_size);
402
403         if (!bcm947xx_map.virt) {
404                 printk("Failed to ioremap\n");
405                 return -EIO;
406         }
407         simple_map_init(&bcm947xx_map);
408         
409         if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
410                 printk("Failed to do_map_probe\n");
411                 iounmap((void *)bcm947xx_map.virt);
412                 return -ENXIO;
413         }
414
415         bcm947xx_mtd->owner = THIS_MODULE;
416
417         size = bcm947xx_mtd->size;
418
419         printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
420
421 #ifdef CONFIG_MTD_PARTITIONS
422         parts = init_mtd_partitions(bcm947xx_mtd, size);
423         for (i = 0; parts[i].name; i++);
424         ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
425         if (ret) {
426                 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
427                 goto fail;
428         }
429 #endif
430         return 0;
431
432  fail:
433         if (bcm947xx_mtd)
434                 map_destroy(bcm947xx_mtd);
435         if (bcm947xx_map.virt)
436                 iounmap((void *)bcm947xx_map.virt);
437         bcm947xx_map.virt = 0;
438         return ret;
439 }
440
441 void __exit cleanup_bcm947xx_map(void)
442 {
443 #ifdef CONFIG_MTD_PARTITIONS
444         del_mtd_partitions(bcm947xx_mtd);
445 #endif
446         map_destroy(bcm947xx_mtd);
447         iounmap((void *)bcm947xx_map.virt);
448 }
449
450 module_init(init_bcm947xx_map);
451 module_exit(cleanup_bcm947xx_map);