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>
33 #include <ifxmips_prom.h>
34 #include <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,
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 (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
);
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 le32_to_cpu(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 /* 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];
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
);
222 if (detect_squashfs_partition(parts
[kernel_part
].offset
+ uimage_size
)) {
223 printk(KERN_INFO
"ifxmips_mtd: found a squashfs following the uImage\n");
225 uimage_size
&= ~(ifxmips_mtd
->erasesize
-1);
226 uimage_size
+= ifxmips_mtd
->erasesize
;
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
;
233 ifxmips_meta_partition
.offset
= parts
[kernel_part
].offset
;
234 ifxmips_meta_partition
.size
= parts
[kernel_part
].size
+ parts
[rootfs_part
].size
;
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
;
248 add_mtd_partitions(ifxmips_mtd
, parts
, num_parts
);
249 add_mtd_partitions(ifxmips_mtd
, &ifxmips_meta_partition
, 1);
251 printk(KERN_INFO
"ifxmips_mtd: added %s flash with %dMB\n",
252 ifxmips_map
.name
, ((int)ifxmips_mtd
->size
) >> 20);
256 static struct platform_driver ifxmips_mtd_driver
= {
257 .probe
= ifxmips_mtd_probe
,
259 .name
= "ifxmips_mtd",
260 .owner
= THIS_MODULE
,
264 int __init
init_ifxmips_mtd(void)
266 int ret
= platform_driver_register(&ifxmips_mtd_driver
);
268 printk(KERN_INFO
"ifxmips_mtd: error registering platfom driver!");
272 static void __exit
cleanup_ifxmips_mtd(void)
274 platform_driver_unregister(&ifxmips_mtd_driver
);
277 module_init(init_ifxmips_mtd
);
278 module_exit(cleanup_ifxmips_mtd
);
280 MODULE_LICENSE("GPL");
281 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
282 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");