Merge branch 'adm8668'
[oweals/openwrt.git] / target / linux / adm8668 / files / drivers / mtd / maps / adm8668.c
1 /*
2  * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/mtd/mtd.h>
24 #include <linux/mtd/map.h>
25 #include <linux/mtd/partitions.h>
26 #include <linux/magic.h>
27 #include <asm/io.h>
28
29 #define FLASH_SIZE      0x800000
30 #define FLASH_PHYS_ADDR 0x10000000
31
32 /* just interested in part of the full struct */
33 struct squashfs_super_block {
34         __le32  s_magic;
35         __le32  pad0[9];        /* it's not really padding */
36         __le64  bytes_used;
37 };
38
39 static struct mtd_info *mymtd;
40
41 static struct map_info nor_map = {
42         name:           "adm8668-nor",
43         size:           FLASH_SIZE,
44         phys:           FLASH_PHYS_ADDR,
45         bankwidth:      2,
46 };
47
48 static struct mtd_partition nor_parts[] = {
49         { name: "linux",  offset: 0x40000,  size: FLASH_SIZE - 0x40000, },
50         { name: "rootfs", offset: 0x200000, size: 0x400000, },
51         { name: NULL, },
52 };
53
54 int __init init_mtd_partitions(struct mtd_info *mtd, size_t size)
55 {
56         int blocksize, off;
57         size_t len;
58         struct squashfs_super_block hdr;
59
60         blocksize = mtd->erasesize;
61         if (blocksize < 0x10000)
62                 blocksize = 0x10000;
63
64         memset(&hdr, 0xe5, sizeof(hdr));
65         /* find and size squashfs */
66         for (off = (128*1024); off < size; off += blocksize) {
67                 if (mtd->read(mtd, off, sizeof(hdr), &len, (char *)&hdr) ||
68                     len != sizeof(hdr))
69                         continue;
70
71                 if (hdr.s_magic == SQUASHFS_MAGIC) {
72                         printk(KERN_INFO "%s: Filesystem type: squashfs, off=%#x size=%#x\n",
73                                 mtd->name, off, (unsigned int)hdr.bytes_used);
74                         /* Update the squashfs start address only! */
75                         nor_parts[1].offset = off;
76                         nor_parts[1].size = FLASH_SIZE - off;
77                         return 2;
78                 }
79         }
80         return 2;
81 }
82
83 int __init init_nor(void)
84 {
85         int nr_parts;
86
87         printk(KERN_NOTICE "ADM8668 NOR flash device: %#x at %#x\n",
88                         FLASH_SIZE, FLASH_PHYS_ADDR);
89
90         nor_map.virt = ioremap(FLASH_PHYS_ADDR, FLASH_SIZE);
91         if (!nor_map.virt) {
92                 printk("Failed to ioremap\n");
93                 return -EIO;
94         }
95
96         simple_map_init(&nor_map);
97         mymtd = do_map_probe("cfi_probe", &nor_map);
98         if (!mymtd) {
99                 iounmap((void *)nor_map.virt);
100                 return -ENXIO;
101         }
102         mymtd->owner = THIS_MODULE;
103         nr_parts = init_mtd_partitions(mymtd, mymtd->size);
104         add_mtd_partitions(mymtd, nor_parts, nr_parts);
105
106         return 0;
107 }
108
109 static void __exit cleanup_nor(void)
110 {
111         if (mymtd) {
112                 del_mtd_partitions(mymtd);
113                 map_destroy(mymtd);
114         }
115         if (nor_map.virt) {
116                 iounmap((void *)nor_map.virt);
117                 nor_map.virt = 0;
118         }
119 }
120
121 module_init(init_nor);
122 module_exit(cleanup_nor);
123
124 MODULE_LICENSE("GPL");
125 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
126 MODULE_DESCRIPTION("MTD map driver for ADM8668 NOR Flash");