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.
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.
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
16 * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
17 * Copyright (C) 2008 John Crispin <blogic@openwrt.org>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.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>
35 #ifndef CONFIG_MTD_PARTITIONS
36 #error Please enable CONFIG_MTD_PARTITIONS
39 static struct map_info ifxmips_map = {
45 static map_word ifxmips_read16(struct map_info *map, unsigned long adr)
49 spin_lock_irqsave(&ebu_lock, flags);
51 temp.x[0] = *((__u16 *)(map->virt + adr));
52 spin_unlock_irqrestore(&ebu_lock, flags);
56 static void ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
59 spin_lock_irqsave(&ebu_lock, flags);
61 *((__u16 *)(map->virt + adr)) = d.x[0];
62 spin_unlock_irqrestore(&ebu_lock, flags);
65 void ifxmips_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
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;
76 spin_unlock_irqrestore(&ebu_lock, flags);
79 void ifxmips_copy_to(struct map_info *map,
84 unsigned char *p = (unsigned char *)from;
87 spin_lock_irqsave(&ebu_lock, flags);
88 to += (unsigned long) map->virt;
89 to_8 = (unsigned char *)to;
92 spin_unlock_irqrestore(&ebu_lock, flags);
95 static struct mtd_partition ifxmips_partitions[] = {
103 .offset = 0x00020000,
108 .offset = 0x00030000,
117 .name = "board_config",
123 static struct mtd_partition ifxmips_meta_partition = {
125 .offset = 0x00030000,
129 static const char *part_probe_types[] = { "cmdlinepart", NULL };
131 int find_uImage_size(unsigned long start_offset)
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);
140 ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
141 printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
145 int find_brn_block(unsigned long start_offset)
147 unsigned char temp[9];
148 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 8);
150 printk(KERN_INFO "data in brn block %s\n", temp);
151 if (memcmp(temp, "BRN-BOOT", 8) == 0)
157 int detect_squashfs_partition(unsigned long start_offset)
160 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
161 return temp == SQUASHFS_MAGIC;
164 static int ifxmips_mtd_probe(struct platform_device *dev)
166 struct mtd_info *ifxmips_mtd = NULL;
167 struct mtd_partition *parts = NULL;
168 unsigned long uimage_size;
170 int kernel_part = 2, rootfs_part = 3;
171 int num_parts = ARRAY_SIZE(ifxmips_partitions);
173 ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
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);
183 if (!ifxmips_map.virt) {
184 printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
188 ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
190 iounmap(ifxmips_map.virt);
191 printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
195 ifxmips_mtd->owner = THIS_MODULE;
197 err = parse_mtd_partitions(ifxmips_mtd, part_probe_types, &parts, 0);
199 printk(KERN_INFO "ifxmips_mtd: found %d partitions from cmdline\n", err);
203 for (i = 0; i < num_parts; i++) {
204 if (strcmp(parts[i].name, "kernel") == 0)
206 if (strcmp(parts[i].name, "rootfs") == 0)
210 parts = &ifxmips_partitions[0];
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);
217 if (detect_squashfs_partition(parts[kernel_part].offset + uimage_size)) {
218 printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
220 uimage_size &= ~0xffff;
221 uimage_size += 0x10000;
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;
228 ifxmips_meta_partition.offset = parts[kernel_part].offset;
229 ifxmips_meta_partition.size = parts[kernel_part].size + parts[rootfs_part].size;
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;
242 add_mtd_partitions(ifxmips_mtd, parts, num_parts);
243 add_mtd_partitions(ifxmips_mtd, &ifxmips_meta_partition, 1);
245 printk(KERN_INFO "ifxmips_mtd: added %s flash with %dMB\n",
246 ifxmips_map.name, ifxmips_mtd->size >> 20);
250 static struct platform_driver ifxmips_mtd_driver = {
251 .probe = ifxmips_mtd_probe,
253 .name = "ifxmips_mtd",
254 .owner = THIS_MODULE,
258 int __init init_ifxmips_mtd(void)
260 int ret = platform_driver_register(&ifxmips_mtd_driver);
262 printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
266 static void __exit cleanup_ifxmips_mtd(void)
268 platform_driver_unregister(&ifxmips_mtd_driver);
271 module_init(init_ifxmips_mtd);
272 module_exit(cleanup_ifxmips_mtd);
274 MODULE_LICENSE("GPL");
275 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
276 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");