lots of code cleanup for ifxmips
[openwrt.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 <asm/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 <linux/magic.h>
31 #include <linux/platform_device.h>
32
33
34 static struct map_info
35 ifxmips_map = {
36         .name = "ifxmips_mtd",
37         .bankwidth = 2,
38         .size = 0x400000,
39 };
40
41 static map_word
42 ifxmips_read16(struct map_info * map, unsigned long adr)
43 {
44         map_word temp;
45
46         adr ^= 2;
47         temp.x[0] = *((__u16 *) (map->virt + adr));
48
49         return temp;
50 }
51
52 static void
53 ifxmips_write16(struct map_info *map, map_word d, unsigned long adr)
54 {
55         adr ^= 2;
56         *((__u16 *) (map->virt + adr)) = d.x[0];
57 }
58
59 void
60 ifxmips_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
61 {
62         unsigned char *p;
63         unsigned char *to_8;
64
65         from = (unsigned long)(from + map->virt);
66         p = (unsigned char*) from;
67         to_8 = (unsigned char*) to;
68         while(len--)
69                 *to_8++ = *p++;
70 }
71
72 void
73 ifxmips_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
74 {
75         unsigned char *p =  (unsigned char*)from;
76         unsigned char *to_8;
77
78         to += (unsigned long) map->virt;
79         to_8 = (unsigned char*)to;
80         while(len--){
81                 *p++ = *to_8++;
82         }
83 }
84
85 static struct mtd_partition
86 ifxmips_partitions[4] = {
87         {
88                 name:"U-Boot",
89                 offset:0x00000000,
90                 size:0x00020000,
91         },
92         {
93                 name:"U-Boot-Env",
94                 offset:0x00020000,
95                 size:0x00010000,
96         },
97         {
98                 name:"kernel",
99                 offset:0x00030000,
100                 size:0x0,
101         },
102         {
103                 name:"rootfs",
104                 offset:0x0,
105                 size:0x0,
106         },
107 };
108
109 int
110 find_uImage_size(unsigned long start_offset){
111         unsigned long temp;
112
113         ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
114         printk(KERN_INFO "ifxmips_mtd: kernel size is %ld \n", temp + 0x40);
115         return temp + 0x40;
116 }
117
118 int
119 detect_squashfs_partition(unsigned long start_offset){
120         unsigned long temp;
121
122         ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
123
124         return (temp == SQUASHFS_MAGIC);
125 }
126
127 static int
128 ifxmips_mtd_probe(struct platform_device *dev)
129 {
130         struct mtd_info *ifxmips_mtd = NULL;
131         struct mtd_partition *parts = NULL;
132         unsigned long uimage_size;
133
134         ifxmips_w32(0x1d7ff, IFXMIPS_EBU_BUSCON0);
135
136         ifxmips_map.read = ifxmips_read16;
137         ifxmips_map.write = ifxmips_write16;
138         ifxmips_map.copy_from = ifxmips_copy_from;
139         ifxmips_map.copy_to = ifxmips_copy_to;
140
141         ifxmips_map.phys = IFXMIPS_FLASH_START;
142         ifxmips_map.virt = ioremap_nocache(IFXMIPS_FLASH_START, IFXMIPS_FLASH_MAX);
143         ifxmips_map.size = IFXMIPS_FLASH_MAX;
144         if(!ifxmips_map.virt)
145         {
146                 printk(KERN_WARNING "ifxmips_mtd: failed to ioremap!\n");
147                 return -EIO;
148         }
149
150         ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
151         if(!ifxmips_mtd)
152         {
153                 iounmap(ifxmips_map.virt);
154                 printk(KERN_WARNING "ifxmips_mtd: probing failed\n");
155                 return -ENXIO;
156         }
157
158         ifxmips_mtd->owner = THIS_MODULE;
159
160         uimage_size = find_uImage_size(ifxmips_partitions[2].offset);
161
162         if(detect_squashfs_partition(ifxmips_partitions[2].offset + uimage_size))
163         {
164                 printk(KERN_INFO "ifxmips_mtd: found a squashfs following the uImage\n");
165         } else {
166                 uimage_size &= ~0xffff;
167                 uimage_size += 0x10000;
168         }
169
170         ifxmips_partitions[2].size = uimage_size;
171         ifxmips_partitions[3].offset = ifxmips_partitions[2].offset + ifxmips_partitions[2].size;
172         ifxmips_partitions[3].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - ifxmips_partitions[3].offset;
173
174         parts = &ifxmips_partitions[0];
175         add_mtd_partitions(ifxmips_mtd, parts, 4);
176
177         printk(KERN_INFO "ifxmips_mtd: added ifxmips flash with %dMB\n", ifxmips_mtd->size >> 20);
178         return 0;
179 }
180
181 static struct
182 platform_driver ifxmips_mtd_driver = {
183         .probe = ifxmips_mtd_probe,
184         .driver = {
185                 .name = "ifxmips_mtd",
186                 .owner = THIS_MODULE,
187         },
188 };
189
190 int __init
191 init_ifxmips_mtd(void)
192 {
193         int ret = platform_driver_register(&ifxmips_mtd_driver);
194         if(ret)
195                 printk(KERN_INFO "ifxmips_mtd: error registering platfom driver!");
196         return ret;
197 }
198
199 static void
200 __exit
201 cleanup_ifxmips_mtd(void)
202 {
203         platform_driver_unregister(&ifxmips_mtd_driver);
204 }
205
206 module_init(init_ifxmips_mtd);
207 module_exit(cleanup_ifxmips_mtd);
208
209 MODULE_LICENSE("GPL");
210 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
211 MODULE_DESCRIPTION("MTD map driver for IFXMIPS boards");