fixes several compile errors, reserves memory for second core, adds u-boot env parsin...
[lede.git] / target / linux / ifxmips / files / drivers / mtd / maps / ifxmips.c
1 /*
2  *  Driver for IFXMIPS flashmap 
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
19  * Copyright (C) 2007 John Crispin <blogic@openwrt.org>
20  */
21
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <asm/io.h>
26 #include <linux/init.h>
27 #include <linux/mtd/mtd.h>
28 #include <linux/mtd/map.h>
29 #include <linux/mtd/partitions.h>
30 #include <linux/mtd/cfi.h>
31 #include <asm/ifxmips/ifxmips.h>
32 #include <linux/magic.h>
33 #include <linux/platform_device.h>
34
35 #define DRVNAME         "ifxmips_mtd"
36
37 static struct map_info
38 ifxmips_map = {
39         .name = DRVNAME,
40         .bankwidth = 2,
41         .size = 0x400000,
42 };
43
44 static map_word
45 ifxmips_read16 (struct map_info * map, unsigned long adr)
46 {
47         map_word temp;
48
49         adr ^= 2;
50         temp.x[0] = *((__u16 *) (map->virt + adr));
51
52         return temp;
53 }
54
55 static void
56 ifxmips_write16 (struct map_info *map, map_word d, unsigned long adr)
57 {
58         adr ^= 2;
59         *((__u16 *) (map->virt + adr)) = d.x[0];
60 }
61
62 void
63 ifxmips_copy_from (struct map_info *map, void *to, unsigned long from, ssize_t len)
64 {
65         u8 *p;
66         u8 *to_8;
67
68         from = (unsigned long) (from + map->virt);
69         p = (u8 *) from;
70         to_8 = (u8 *) to;
71         while(len--){
72                 *to_8++ = *p++;
73         }
74 }
75
76 void
77 ifxmips_copy_to (struct map_info *map, unsigned long to, const void *from, ssize_t len)
78 {
79         u8 *p =  (u8*) from;
80         u8 *to_8;
81
82         to += (unsigned long) map->virt;
83         to_8 = (u8*)to;
84         while(len--){
85                 *p++ = *to_8++;
86         }
87 }
88
89 static struct mtd_partition
90 ifxmips_partitions[4] = {
91         {
92                 name:"U-Boot",
93                 offset:0x00000000,
94                 size:0x00020000,
95         },
96         {
97                 name:"U-Boot-Env",
98                 offset:0x00020000,
99                 size:0x00010000,
100         },
101         {
102                 name:"kernel",
103                 offset:0x00030000,
104                 size:0x0,
105         },
106         {
107                 name:"rootfs",
108                 offset:0x0,
109                 size:0x0,
110         },
111 };
112
113 int
114 find_uImage_size (unsigned long start_offset){
115         unsigned long temp;
116
117         ifxmips_copy_from(&ifxmips_map, &temp, start_offset + 12, 4);
118         printk(KERN_INFO DRVNAME ": kernel size is %ld \n", temp + 0x40);
119         return temp + 0x40;
120 }
121
122 int
123 detect_squashfs_partition (unsigned long start_offset){
124         unsigned long temp;
125
126         ifxmips_copy_from(&ifxmips_map, &temp, start_offset, 4);
127
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
145         ifxmips_map.phys = IFXMIPS_FLASH_START;
146         ifxmips_map.virt = ioremap_nocache(IFXMIPS_FLASH_START, IFXMIPS_FLASH_MAX);
147         ifxmips_map.size = IFXMIPS_FLASH_MAX;
148         if (!ifxmips_map.virt) {
149                 printk(KERN_WARNING DRVNAME ": failed to ioremap!\n");
150                 return -EIO;
151         }
152
153         ifxmips_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &ifxmips_map);
154         if (!ifxmips_mtd) {
155                 iounmap(ifxmips_map.virt);
156                 printk(KERN_WARNING DRVNAME ": probing failed\n");
157                 return -ENXIO;
158         }
159
160         ifxmips_mtd->owner = THIS_MODULE;
161
162         uimage_size = find_uImage_size(ifxmips_partitions[2].offset);
163
164         if(detect_squashfs_partition(ifxmips_partitions[2].offset + uimage_size)){
165                 printk(KERN_INFO DRVNAME ": found a squashfs following the uImage\n");
166         } else {
167                 uimage_size &= ~0xffff;
168                 uimage_size += 0x10000;
169         }
170
171         ifxmips_partitions[2].size = uimage_size;
172         ifxmips_partitions[3].offset = ifxmips_partitions[2].offset + ifxmips_partitions[2].size;
173         ifxmips_partitions[3].size = ((ifxmips_mtd->size >> 20) * 1024 * 1024) - ifxmips_partitions[3].offset;
174
175         parts = &ifxmips_partitions[0];
176         add_mtd_partitions(ifxmips_mtd, parts, 4);
177
178         printk(KERN_INFO DRVNAME ": added ifxmips flash with %dMB\n", ifxmips_mtd->size >> 20);
179         return 0;
180 }
181
182 static struct
183 platform_driver ifxmips_mtd_driver = {
184         .probe = ifxmips_mtd_probe,
185         .driver = {
186                 .name = DRVNAME,
187                 .owner = THIS_MODULE,
188         },
189 };
190
191 int __init
192 init_ifxmips_mtd (void)
193 {
194         int ret = platform_driver_register(&ifxmips_mtd_driver);
195         if (ret)
196                 printk(KERN_INFO DRVNAME ": error registering platfom driver!");
197
198         return ret;
199 }
200
201 static void
202 __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");