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 static struct map_info
37 .name = "ifxmips_mtd",
43 ifxmips_read16(struct map_info * map, unsigned long adr)
47 spin_lock_irqsave(&ebu_lock, flags);
49 temp.x[0] = *((__u16*)(map->virt + adr));
50 spin_unlock_irqrestore(&ebu_lock, flags);
55 ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
58 spin_lock_irqsave(&ebu_lock, flags);
60 *((__u16*)(map->virt + adr)) = d.x[0];
61 spin_unlock_irqrestore(&ebu_lock, flags);
65 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);
80 ifxmips_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
82 unsigned char *p = (unsigned char*)from;
85 spin_lock_irqsave(&ebu_lock, flags);
86 to += (unsigned long) map->virt;
87 to_8 = (unsigned char*)to;
90 spin_unlock_irqrestore(&ebu_lock, flags);
93 static struct mtd_partition
94 ifxmips_partitions[] = {
123 find_uImage_size(unsigned long start_offset){
125 ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
126 printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
131 find_brn_block(unsigned long start_offset){
132 unsigned char temp[9];
133 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 8);
135 printk(KERN_INFO "data in brn block %s\n", temp);
136 if(memcmp(temp, "BRN-BOOT", 8) == 0)
143 detect_squashfs_partition(unsigned long start_offset){
145 ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
146 return (temp == SQUASHFS_MAGIC);
150 ifxmips_mtd_probe(struct platform_device *dev)
152 struct mtd_info *ifxmips_mtd = NULL;
153 struct mtd_partition *parts = NULL;
154 unsigned long uimage_size;
156 ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
158 ifxmips_map.read = ifxmips_read16;
159 ifxmips_map.write = ifxmips_write16;
160 ifxmips_map.copy_from = ifxmips_copy_from;
161 ifxmips_map.copy_to = ifxmips_copy_to;
162 ifxmips_map.phys = dev->resource->start;
163 ifxmips_map.size = dev->resource->end - ifxmips_map.phys + 1;
164 ifxmips_map.virt = ioremap_nocache(ifxmips_map.phys, ifxmips_map.size);
166 if(!ifxmips_map.virt)
168 printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
172 ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
175 iounmap(ifxmips_map.virt);
176 printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
180 ifxmips_mtd->owner = THIS_MODULE;
181 uimage_size = find_uImage_size(ifxmips_partitions[2].offset);
183 if(detect_squashfs_partition(ifxmips_partitions[2].offset + uimage_size))
185 printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
187 uimage_size &= ~0xffff;
188 uimage_size += 0x10000;
191 parts = &ifxmips_partitions[0];
192 ifxmips_partitions[2].size = uimage_size;
193 ifxmips_partitions[3].offset = ifxmips_partitions[2].offset + ifxmips_partitions[2].size;
194 ifxmips_partitions[3].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - ifxmips_partitions[3].offset;
195 if(ifxmips_has_brn_block())
197 ifxmips_partitions[3].size -= ifxmips_mtd->erasesize;
198 ifxmips_partitions[4].offset = ifxmips_mtd->size - ifxmips_mtd->erasesize;
199 ifxmips_partitions[4].size = ifxmips_mtd->erasesize;
200 add_mtd_partitions(ifxmips_mtd, parts, 5);
202 add_mtd_partitions(ifxmips_mtd, parts, 4);
205 printk(KERN_INFO "ifxmips_mtd: added ifxmips flash with %dMB\n", ifxmips_mtd->size >> 20);
210 platform_driver ifxmips_mtd_driver = {
211 .probe = ifxmips_mtd_probe,
213 .name = "ifxmips_mtd",
214 .owner = THIS_MODULE,
219 init_ifxmips_mtd(void)
221 int ret = platform_driver_register(&ifxmips_mtd_driver);
223 printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
228 cleanup_ifxmips_mtd(void)
230 platform_driver_unregister(&ifxmips_mtd_driver);
233 module_init(init_ifxmips_mtd);
234 module_exit(cleanup_ifxmips_mtd);
236 MODULE_LICENSE("GPL");
237 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
238 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");