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