797a11cffd4688bb9b9292e879c00f34dc63c0a5
[openwrt.git] / target / linux / ifxmips / patches-2.6.30 / 140-mtd.patch
1 Index: linux-2.6.30.8/drivers/mtd/maps/Makefile
2 ===================================================================
3 --- linux-2.6.30.8.orig/drivers/mtd/maps/Makefile       2009-09-24 17:28:02.000000000 +0200
4 +++ linux-2.6.30.8/drivers/mtd/maps/Makefile    2009-10-19 21:31:32.000000000 +0200
5 @@ -62,3 +62,4 @@
6  obj-$(CONFIG_MTD_BFIN_ASYNC)   += bfin-async-flash.o
7  obj-$(CONFIG_MTD_RBTX4939)     += rbtx4939-flash.o
8  obj-$(CONFIG_MTD_VMU)          += vmu-flash.o
9 +obj-$(CONFIG_MTD_IFXMIPS)  += ifxmips.o
10 Index: linux-2.6.30.8/drivers/mtd/maps/ifxmips.c
11 ===================================================================
12 --- /dev/null   1970-01-01 00:00:00.000000000 +0000
13 +++ linux-2.6.30.8/drivers/mtd/maps/ifxmips.c   2009-10-19 21:40:44.000000000 +0200
14 @@ -0,0 +1,277 @@
15 +/*
16 + * This program is free software; you can redistribute it and/or modify
17 + * it under the terms of the GNU General Public License as published by
18 + * the Free Software Foundation; either version 2 of the License, or
19 + * (at your option) any later version.
20 + *
21 + * This program is distributed in the hope that it will be useful,
22 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 + * GNU General Public License for more details.
25 + *
26 + * You should have received a copy of the GNU General Public License
27 + * along with this program; if not, write to the Free Software
28 + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 + *
30 + * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
31 + * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
32 + */
33 +
34 +#include <linux/module.h>
35 +#include <linux/types.h>
36 +#include <linux/kernel.h>
37 +#include <linux/io.h>
38 +#include <linux/init.h>
39 +#include <linux/mtd/mtd.h>
40 +#include <linux/mtd/map.h>
41 +#include <linux/mtd/partitions.h>
42 +#include <linux/mtd/cfi.h>
43 +#include <linux/magic.h>
44 +#include <linux/platform_device.h>
45 +
46 +#include <ifxmips.h>
47 +#include <ifxmips_prom.h>
48 +#include <ifxmips_ebu.h>
49 +
50 +#ifndef CONFIG_MTD_PARTITIONS
51 +#error Please enable CONFIG_MTD_PARTITIONS
52 +#endif
53 +
54 +static struct map_info ifxmips_map = {
55 +       .name = "ifx-nor",
56 +       .bankwidth = 2,
57 +       .size = 0x400000,
58 +};
59 +
60 +static map_word ifxmips_read16(struct map_info *map, unsigned long adr)
61 +{
62 +       unsigned long flags;
63 +       map_word temp;
64 +       spin_lock_irqsave(&ebu_lock, flags);
65 +       adr ^= 2;
66 +       temp.x[0] = *((__u16 *)(map->virt + adr));
67 +       spin_unlock_irqrestore(&ebu_lock, flags);
68 +       return temp;
69 +}
70 +
71 +static void ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
72 +{
73 +       unsigned long flags;
74 +       spin_lock_irqsave(&ebu_lock, flags);
75 +       adr ^= 2;
76 +       *((__u16 *)(map->virt + adr)) = d.x[0];
77 +       spin_unlock_irqrestore(&ebu_lock, flags);
78 +}
79 +
80 +void ifxmips_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
81 +{
82 +       unsigned char *p;
83 +       unsigned char *to_8;
84 +       unsigned long flags;
85 +       spin_lock_irqsave(&ebu_lock, flags);
86 +       from = (unsigned long)(from + map->virt);
87 +       p = (unsigned char *) from;
88 +       to_8 = (unsigned char *) to;
89 +       while (len--)
90 +               *to_8++ = *p++;
91 +       spin_unlock_irqrestore(&ebu_lock, flags);
92 +}
93 +
94 +void ifxmips_copy_to(struct map_info *map,
95 +                    unsigned long to,
96 +                    const void *from,
97 +                    ssize_t len)
98 +{
99 +       unsigned char *p =  (unsigned char *)from;
100 +       unsigned char *to_8;
101 +       unsigned long flags;
102 +       spin_lock_irqsave(&ebu_lock, flags);
103 +       to += (unsigned long) map->virt;
104 +       to_8 = (unsigned char *)to;
105 +       while (len--)
106 +               *p++ = *to_8++;
107 +       spin_unlock_irqrestore(&ebu_lock, flags);
108 +}
109 +
110 +static struct mtd_partition ifxmips_partitions[] = {
111 +       {
112 +               .name = "uboot",
113 +               .offset = 0x00000000,
114 +               .size = 0x00020000,
115 +       },
116 +       {
117 +               .name = "uboot_env",
118 +               .offset = 0x00020000,
119 +               .size = 0x00010000,
120 +       },
121 +       {
122 +               .name = "kernel",
123 +               .offset = 0x00030000,
124 +               .size = 0x0,
125 +       },
126 +       {
127 +               .name = "rootfs",
128 +               .offset = 0x0,
129 +               .size = 0x0,
130 +       },
131 +       {
132 +               .name = "board_config",
133 +               .offset = 0x0,
134 +               .size = 0x0,
135 +       },
136 +};
137 +
138 +static struct mtd_partition ifxmips_meta_partition = {
139 +       .name = "linux",
140 +       .offset = 0x00030000,
141 +       .size = 0x0,
142 +};
143 +
144 +static const char *part_probe_types[] = { "cmdlinepart", NULL };
145 +
146 +int find_uImage_size(unsigned long start_offset)
147 +{
148 +       unsigned long magic;
149 +       unsigned long temp;
150 +       ifxmips_copy_from(&ifxmips_map, &magic, start_offset, 4);
151 +       if (le32_to_cpu(magic) != 0x56190527) {
152 +               printk(KERN_INFO "ifxmips_mtd: invalid magic (0x%08X) of kernel at 0x%08lx \n", le32_to_cpu(magic), start_offset);
153 +               return 0;
154 +       }
155 +       ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
156 +       printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
157 +       return temp + 0x40;
158 +}
159 +
160 +int find_brn_block(unsigned long start_offset)
161 +{
162 +       unsigned char temp[9];
163 +       ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 8);
164 +       temp[8] = '\0';
165 +       printk(KERN_INFO "data in brn block %s\n", temp);
166 +       if (memcmp(temp, "BRN-BOOT", 8) == 0)
167 +               return 1;
168 +       else
169 +               return 0;
170 +}
171 +
172 +int detect_squashfs_partition(unsigned long start_offset)
173 +{
174 +       unsigned long temp;
175 +       ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
176 +       return le32_to_cpu(temp) == SQUASHFS_MAGIC;
177 +}
178 +
179 +static int ifxmips_mtd_probe(struct platform_device *dev)
180 +{
181 +       struct mtd_info *ifxmips_mtd = NULL;
182 +       struct mtd_partition *parts = NULL;
183 +       unsigned long uimage_size;
184 +       int err, i;
185 +       int kernel_part = 2, rootfs_part = 3;
186 +       int num_parts = ARRAY_SIZE(ifxmips_partitions);
187 +
188 +       ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
189 +
190 +       ifxmips_map.read = ifxmips_read16;
191 +       ifxmips_map.write = ifxmips_write16;
192 +       ifxmips_map.copy_from = ifxmips_copy_from;
193 +       ifxmips_map.copy_to = ifxmips_copy_to;
194 +       ifxmips_map.phys = dev->resource->start;
195 +       ifxmips_map.size = dev->resource->end - ifxmips_map.phys + 1;
196 +       ifxmips_map.virt = ioremap_nocache(ifxmips_map.phys, ifxmips_map.size);
197 +
198 +       if (!ifxmips_map.virt) {
199 +               printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
200 +               return -EIO;
201 +       }
202 +
203 +       ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
204 +       if (!ifxmips_mtd) {
205 +               iounmap(ifxmips_map.virt);
206 +               printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
207 +               return -ENXIO;
208 +       }
209 +
210 +       ifxmips_mtd->owner = THIS_MODULE;
211 +
212 +       err = parse_mtd_partitions(ifxmips_mtd, part_probe_types, &parts, 0);
213 +       if (err > 0) {
214 +               printk(KERN_INFO "ifxmips_mtd: found %d partitions from cmdline\n", err);
215 +               num_parts = err;
216 +               kernel_part = 0;
217 +               rootfs_part = 0;
218 +               for (i = 0; i < num_parts; i++) {
219 +                       if (strcmp(parts[i].name, "kernel") == 0)
220 +                               kernel_part = i;
221 +                       if (strcmp(parts[i].name, "rootfs") == 0)
222 +                               rootfs_part = i;
223 +               }
224 +       } else {
225 +               parts = &ifxmips_partitions[0];
226 +       }
227 +
228 +       /* dynamic size detection only if rootfs-part follows kernel-part */
229 +       if (kernel_part+1 == rootfs_part) {
230 +               uimage_size = find_uImage_size(parts[kernel_part].offset);
231 +
232 +               if (detect_squashfs_partition(parts[kernel_part].offset + uimage_size)) {
233 +                       printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
234 +               } else {
235 +                       uimage_size &= ~0xffff;
236 +                       uimage_size += 0x10000;
237 +               }
238 +
239 +               parts[kernel_part].size = uimage_size;
240 +               parts[rootfs_part].offset = parts[kernel_part].offset + parts[kernel_part].size;
241 +               parts[rootfs_part].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - parts[rootfs_part].offset;
242 +
243 +               ifxmips_meta_partition.offset = parts[kernel_part].offset;
244 +               ifxmips_meta_partition.size = parts[kernel_part].size + parts[rootfs_part].size;
245 +       }
246 +
247 +       if (err <= 0) {
248 +               if (ifxmips_has_brn_block()) {
249 +                       parts[3].size -= ifxmips_mtd->erasesize;
250 +                       parts[4].offset = ifxmips_mtd->size - ifxmips_mtd->erasesize;
251 +                       parts[4].size = ifxmips_mtd->erasesize;
252 +               } else {
253 +                       num_parts--;
254 +               }
255 +       }
256 +
257 +       add_mtd_partitions(ifxmips_mtd, parts, num_parts);
258 +       add_mtd_partitions(ifxmips_mtd, &ifxmips_meta_partition, 1);
259 +
260 +       printk(KERN_INFO "ifxmips_mtd: added %s flash with %dMB\n",
261 +               ifxmips_map.name, ((int)ifxmips_mtd->size) >> 20);
262 +       return 0;
263 +}
264 +
265 +static struct platform_driver ifxmips_mtd_driver = {
266 +       .probe = ifxmips_mtd_probe,
267 +       .driver = {
268 +               .name = "ifxmips_mtd",
269 +               .owner = THIS_MODULE,
270 +       },
271 +};
272 +
273 +int __init init_ifxmips_mtd(void)
274 +{
275 +       int ret = platform_driver_register(&ifxmips_mtd_driver);
276 +       if (ret)
277 +               printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
278 +       return ret;
279 +}
280 +
281 +static void __exit cleanup_ifxmips_mtd(void)
282 +{
283 +       platform_driver_unregister(&ifxmips_mtd_driver);
284 +}
285 +
286 +module_init(init_ifxmips_mtd);
287 +module_exit(cleanup_ifxmips_mtd);
288 +
289 +MODULE_LICENSE("GPL");
290 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
291 +MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");