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