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