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