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