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 <linux/magic.h>
30 #include <linux/platform_device.h>
32 #include <asm/ifxmips/ifxmips.h>
33 #include <asm/ifxmips/ifxmips_prom.h>
34 #include <asm/ifxmips/ifxmips_ebu.h>
36 #ifndef CONFIG_MTD_PARTITIONS
37 #error Please enable CONFIG_MTD_PARTITIONS
40 static struct map_info ifxmips_map = {
46 static map_word ifxmips_read16(struct map_info *map, unsigned long adr)
50 spin_lock_irqsave(&ebu_lock, flags);
52 temp.x[0] = *((__u16 *)(map->virt + adr));
53 spin_unlock_irqrestore(&ebu_lock, flags);
57 static void ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
60 spin_lock_irqsave(&ebu_lock, flags);
62 *((__u16 *)(map->virt + adr)) = d.x[0];
63 spin_unlock_irqrestore(&ebu_lock, flags);
66 void ifxmips_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
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;
77 spin_unlock_irqrestore(&ebu_lock, flags);
80 void ifxmips_copy_to(struct map_info *map,
85 unsigned char *p = (unsigned char *)from;
88 spin_lock_irqsave(&ebu_lock, flags);
89 to += (unsigned long) map->virt;
90 to_8 = (unsigned char *)to;
93 spin_unlock_irqrestore(&ebu_lock, flags);
96 static struct mtd_partition ifxmips_partitions[] = {
104 .offset = 0x00020000,
109 .offset = 0x00030000,
118 .name = "board_config",
124 static struct mtd_partition ifxmips_meta_partition = {
126 .offset = 0x00030000,
130 static const char *part_probe_types[] = { "cmdlinepart", NULL };
132 int find_uImage_size(unsigned long start_offset)
136 ifxmips_copy_from(&ifxmips_map, &magic, start_offset, 4);
137 if (!(ntohl(magic) == 0x27051956)) {
138 printk(KERN_INFO "ifxmips_mtd: invalid magic (0x%08X) of kernel at 0x%08lx \n", ntohl(magic), start_offset);
141 ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
142 printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
146 int find_brn_block(unsigned long start_offset)
148 unsigned char temp[9];
149 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 8);
151 printk(KERN_INFO "data in brn block %s\n", temp);
152 if (memcmp(temp, "BRN-BOOT", 8) == 0)
158 int detect_squashfs_partition(unsigned long start_offset)
161 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
162 return temp == SQUASHFS_MAGIC;
165 static int ifxmips_mtd_probe(struct platform_device *dev)
167 struct mtd_info *ifxmips_mtd = NULL;
168 struct mtd_partition *parts = NULL;
169 unsigned long uimage_size;
171 int kernel_part = 2, rootfs_part = 3;
172 int num_parts = ARRAY_SIZE(ifxmips_partitions);
174 ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
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);
184 if (!ifxmips_map.virt) {
185 printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
189 ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
191 iounmap(ifxmips_map.virt);
192 printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
196 ifxmips_mtd->owner = THIS_MODULE;
198 err = parse_mtd_partitions(ifxmips_mtd, part_probe_types, &parts, 0);
200 printk(KERN_INFO "ifxmips_mtd: found %d partitions from cmdline\n", err);
204 for (i = 0; i < num_parts; i++) {
205 if (strcmp(parts[i].name, "kernel") == 0)
207 if (strcmp(parts[i].name, "rootfs") == 0)
211 parts = &ifxmips_partitions[0];
214 /* dynamic size detection only if rootfs-part follows kernel-part */
215 if (kernel_part+1 == rootfs_part) {
216 uimage_size = find_uImage_size(parts[kernel_part].offset);
218 if (detect_squashfs_partition(parts[kernel_part].offset + uimage_size)) {
219 printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
221 uimage_size &= ~0xffff;
222 uimage_size += 0x10000;
225 parts[kernel_part].size = uimage_size;
226 parts[rootfs_part].offset = parts[kernel_part].offset + parts[kernel_part].size;
227 parts[rootfs_part].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - parts[rootfs_part].offset;
229 ifxmips_meta_partition.offset = parts[kernel_part].offset;
230 ifxmips_meta_partition.size = parts[kernel_part].size + parts[rootfs_part].size;
234 if (ifxmips_has_brn_block()) {
235 parts[3].size -= ifxmips_mtd->erasesize;
236 parts[4].offset = ifxmips_mtd->size - ifxmips_mtd->erasesize;
237 parts[4].size = ifxmips_mtd->erasesize;
243 add_mtd_partitions(ifxmips_mtd, parts, num_parts);
244 add_mtd_partitions(ifxmips_mtd, &ifxmips_meta_partition, 1);
246 printk(KERN_INFO "ifxmips_mtd: added %s flash with %dMB\n",
247 ifxmips_map.name, ifxmips_mtd->size >> 20);
251 static struct platform_driver ifxmips_mtd_driver = {
252 .probe = ifxmips_mtd_probe,
254 .name = "ifxmips_mtd",
255 .owner = THIS_MODULE,
259 int __init init_ifxmips_mtd(void)
261 int ret = platform_driver_register(&ifxmips_mtd_driver);
263 printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
267 static void __exit cleanup_ifxmips_mtd(void)
269 platform_driver_unregister(&ifxmips_mtd_driver);
272 module_init(init_ifxmips_mtd);
273 module_exit(cleanup_ifxmips_mtd);
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
277 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");