some cleanups: - fix CPUID detection - update MTD map for checks and sysupgrade ...
[openwrt/staging/mkresin.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 <asm/ifxmips/ifxmips.h>
30 #include <asm/ifxmips/ifxmips_prom.h>
31 #include <asm/ifxmips/ifxmips_ebu.h>
32 #include <linux/magic.h>
33 #include <linux/platform_device.h>
34
35 #ifndef CONFIG_MTD_PARTITIONS
36 #error Please enable CONFIG_MTD_PARTITIONS
37 #endif
38
39 static struct map_info ifxmips_map = {
40 .name = "ifx-nor",
41 .bankwidth = 2,
42 .size = 0x400000,
43 };
44
45 static map_word ifxmips_read16(struct map_info *map, unsigned long adr)
46 {
47 unsigned long flags;
48 map_word temp;
49 spin_lock_irqsave(&ebu_lock, flags);
50 adr ^= 2;
51 temp.x[0] = *((__u16 *)(map->virt + adr));
52 spin_unlock_irqrestore(&ebu_lock, flags);
53 return temp;
54 }
55
56 static void ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
57 {
58 unsigned long flags;
59 spin_lock_irqsave(&ebu_lock, flags);
60 adr ^= 2;
61 *((__u16 *)(map->virt + adr)) = d.x[0];
62 spin_unlock_irqrestore(&ebu_lock, flags);
63 }
64
65 void ifxmips_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
66 {
67 unsigned char *p;
68 unsigned char *to_8;
69 unsigned long flags;
70 spin_lock_irqsave(&ebu_lock, flags);
71 from = (unsigned long)(from + map->virt);
72 p = (unsigned char *) from;
73 to_8 = (unsigned char *) to;
74 while (len--)
75 *to_8++ = *p++;
76 spin_unlock_irqrestore(&ebu_lock, flags);
77 }
78
79 void ifxmips_copy_to(struct map_info *map,
80 unsigned long to,
81 const void *from,
82 ssize_t len)
83 {
84 unsigned char *p = (unsigned char *)from;
85 unsigned char *to_8;
86 unsigned long flags;
87 spin_lock_irqsave(&ebu_lock, flags);
88 to += (unsigned long) map->virt;
89 to_8 = (unsigned char *)to;
90 while (len--)
91 *p++ = *to_8++;
92 spin_unlock_irqrestore(&ebu_lock, flags);
93 }
94
95 static struct mtd_partition ifxmips_partitions[] = {
96 {
97 .name = "uboot",
98 .offset = 0x00000000,
99 .size = 0x00020000,
100 },
101 {
102 .name = "uboot_env",
103 .offset = 0x00020000,
104 .size = 0x00010000,
105 },
106 {
107 .name = "kernel",
108 .offset = 0x00030000,
109 .size = 0x0,
110 },
111 {
112 .name = "rootfs",
113 .offset = 0x0,
114 .size = 0x0,
115 },
116 {
117 .name = "board_config",
118 .offset = 0x0,
119 .size = 0x0,
120 },
121 };
122
123 static struct mtd_partition ifxmips_meta_partition = {
124 .name = "linux",
125 .offset = 0x00030000,
126 .size = 0x0,
127 };
128
129 static const char *part_probe_types[] = { "cmdlinepart", NULL };
130
131 int find_uImage_size(unsigned long start_offset)
132 {
133 unsigned long magic;
134 unsigned long temp;
135 ifxmips_copy_from(&ifxmips_map, &magic, start_offset, 4);
136 if (!(ntohl(magic) == 0x27051956)) {
137 printk(KERN_INFO "ifxmips_mtd: invalid magic (0x%08X) of kernel at 0x%08lx \n", ntohl(magic), start_offset);
138 return 0;
139 }
140 ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
141 printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
142 return temp + 0x40;
143 }
144
145 int find_brn_block(unsigned long start_offset)
146 {
147 unsigned char temp[9];
148 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 8);
149 temp[8] = '\0';
150 printk(KERN_INFO "data in brn block %s\n", temp);
151 if (memcmp(temp, "BRN-BOOT", 8) == 0)
152 return 1;
153 else
154 return 0;
155 }
156
157 int detect_squashfs_partition(unsigned long start_offset)
158 {
159 unsigned long temp;
160 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
161 return temp == SQUASHFS_MAGIC;
162 }
163
164 static int ifxmips_mtd_probe(struct platform_device *dev)
165 {
166 struct mtd_info *ifxmips_mtd = NULL;
167 struct mtd_partition *parts = NULL;
168 unsigned long uimage_size;
169 int err, i;
170 int kernel_part = 2, rootfs_part = 3;
171 int num_parts = ARRAY_SIZE(ifxmips_partitions);
172
173 ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
174
175 ifxmips_map.read = ifxmips_read16;
176 ifxmips_map.write = ifxmips_write16;
177 ifxmips_map.copy_from = ifxmips_copy_from;
178 ifxmips_map.copy_to = ifxmips_copy_to;
179 ifxmips_map.phys = dev->resource->start;
180 ifxmips_map.size = dev->resource->end - ifxmips_map.phys + 1;
181 ifxmips_map.virt = ioremap_nocache(ifxmips_map.phys, ifxmips_map.size);
182
183 if (!ifxmips_map.virt) {
184 printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
185 return -EIO;
186 }
187
188 ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
189 if (!ifxmips_mtd) {
190 iounmap(ifxmips_map.virt);
191 printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
192 return -ENXIO;
193 }
194
195 ifxmips_mtd->owner = THIS_MODULE;
196
197 err = parse_mtd_partitions(ifxmips_mtd, part_probe_types, &parts, 0);
198 if (err > 0) {
199 printk(KERN_INFO "ifxmips_mtd: found %d partitions from cmdline\n", err);
200 num_parts = err;
201 kernel_part = 0;
202 rootfs_part = 0;
203 for (i = 0; i < num_parts; i++) {
204 if (strcmp(parts[i].name, "kernel") == 0)
205 kernel_part = i;
206 if (strcmp(parts[i].name, "rootfs") == 0)
207 rootfs_part = i;
208 }
209 } else {
210 parts = &ifxmips_partitions[0];
211 }
212
213 /* dynamic size detection only if rootfs-part follows kernel-part */
214 if (kernel_part+1 == rootfs_part) {
215 uimage_size = find_uImage_size(parts[kernel_part].offset);
216
217 if (detect_squashfs_partition(parts[kernel_part].offset + uimage_size)) {
218 printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
219 } else {
220 uimage_size &= ~0xffff;
221 uimage_size += 0x10000;
222 }
223
224 parts[kernel_part].size = uimage_size;
225 parts[rootfs_part].offset = parts[kernel_part].offset + parts[kernel_part].size;
226 parts[rootfs_part].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - parts[rootfs_part].offset;
227
228 ifxmips_meta_partition.offset = parts[kernel_part].offset;
229 ifxmips_meta_partition.size = parts[kernel_part].size + parts[rootfs_part].size;
230 }
231
232 if (err <= 0) {
233 if (ifxmips_has_brn_block()) {
234 parts[4].size -= ifxmips_mtd->erasesize;
235 parts[5].offset = ifxmips_mtd->size - ifxmips_mtd->erasesize;
236 parts[5].size = ifxmips_mtd->erasesize;
237 } else {
238 num_parts--;
239 }
240 }
241
242 add_mtd_partitions(ifxmips_mtd, parts, num_parts);
243 add_mtd_partitions(ifxmips_mtd, &ifxmips_meta_partition, 1);
244
245 printk(KERN_INFO "ifxmips_mtd: added %s flash with %dMB\n",
246 ifxmips_map.name, ifxmips_mtd->size >> 20);
247 return 0;
248 }
249
250 static struct platform_driver ifxmips_mtd_driver = {
251 .probe = ifxmips_mtd_probe,
252 .driver = {
253 .name = "ifxmips_mtd",
254 .owner = THIS_MODULE,
255 },
256 };
257
258 int __init init_ifxmips_mtd(void)
259 {
260 int ret = platform_driver_register(&ifxmips_mtd_driver);
261 if (ret)
262 printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
263 return ret;
264 }
265
266 static void __exit cleanup_ifxmips_mtd(void)
267 {
268 platform_driver_unregister(&ifxmips_mtd_driver);
269 }
270
271 module_init(init_ifxmips_mtd);
272 module_exit(cleanup_ifxmips_mtd);
273
274 MODULE_LICENSE("GPL");
275 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
276 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");