25e9df3de8d769fe2d14603d3dfba969bff49f1a
[openwrt.git] / target / linux / ifxmips / files / drivers / mtd / maps / danube.c
1 /*
2  *  Driver for DANUBE 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
27 #include <linux/init.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/map.h>
30 #include <linux/mtd/partitions.h>
31 #include <linux/mtd/cfi.h>
32 #include <asm/danube/danube.h>
33 #include <linux/magic.h>
34
35 static struct map_info
36 danube_map = {
37         .name = "DANUBE_FLASH",
38         .bankwidth = 2,
39         .size = 0x400000,
40 };
41
42 static map_word
43 danube_read16 (struct map_info * map, unsigned long adr)
44 {
45         map_word temp;
46
47         adr ^= 2;
48         temp.x[0] = *((__u16 *) (map->virt + adr));
49
50         return temp;
51 }
52
53 static void
54 danube_write16 (struct map_info *map, map_word d, unsigned long adr)
55 {
56         adr ^= 2;
57         *((__u16 *) (map->virt + adr)) = d.x[0];
58 }
59
60 void
61 danube_copy_from (struct map_info *map, void *to, unsigned long from, ssize_t len)
62 {
63         u8 *p;
64         u8 *to_8;
65
66         from = (unsigned long) (from + map->virt);
67         p = (u8 *) from;
68         to_8 = (u8 *) to;
69         while(len--){
70                 *to_8++ = *p++;
71         }
72 }
73
74 void
75 danube_copy_to (struct map_info *map, unsigned long to, const void *from, ssize_t len)
76 {
77         u8 *p =  (u8*) from;
78         u8 *to_8;
79
80         to += (unsigned long) map->virt;
81         to_8 = (u8*)to;
82         while(len--){
83                 *p++ = *to_8++;
84         }
85 }
86
87 static struct mtd_partition
88 danube_partitions[4] = {
89         {
90                 name:"U-Boot",
91                 offset:0x00000000,
92                 size:0x00020000,
93         },
94         {
95                 name:"U-Boot-Env",
96                 offset:0x00020000,
97                 size:0x00010000,
98         },
99         {
100                 name:"kernel",
101                 offset:0x00030000,
102                 size:0x0,
103         },
104         {
105                 name:"rootfs",
106                 offset:0x0,
107                 size:0x0,
108         },
109 };
110
111 #define DANUBE_FLASH_START              0x10000000
112 #define DANUBE_FLASH_MAX                0x2000000
113
114 int
115 find_uImage_size (unsigned long start_offset){
116         unsigned long temp;
117
118         danube_copy_from(&danube_map, &temp, start_offset + 12, 4);
119         printk("kernel size is %ld \n", temp + 0x40);
120         return temp + 0x40;
121 }
122
123 int
124 detect_squashfs_partition (unsigned long start_offset){
125         unsigned long temp;
126
127         danube_copy_from(&danube_map, &temp, start_offset, 4);
128
129         return (temp == SQUASHFS_MAGIC);
130 }
131
132 int __init
133 init_danube_mtd (void)
134 {
135         struct mtd_info *danube_mtd = NULL;
136         struct mtd_partition *parts = NULL;
137         unsigned long uimage_size;
138
139         writel(0x1d7ff, DANUBE_EBU_BUSCON0);
140
141         danube_map.read = danube_read16;
142         danube_map.write = danube_write16;
143         danube_map.copy_from = danube_copy_from;
144         danube_map.copy_to = danube_copy_to;
145
146         danube_map.phys = DANUBE_FLASH_START;
147         danube_map.virt = ioremap_nocache(DANUBE_FLASH_START, DANUBE_FLASH_MAX);
148         danube_map.size = DANUBE_FLASH_MAX;
149         if (!danube_map.virt) {
150                 printk(KERN_WARNING "Failed to ioremap!\n");
151                 return -EIO;
152         }
153
154         danube_mtd = (struct mtd_info *) do_map_probe("cfi_probe", &danube_map);
155         if (!danube_mtd) {
156                 iounmap(danube_map.virt);
157                 printk("probing failed\n");
158                 return -ENXIO;
159         }
160
161         danube_mtd->owner = THIS_MODULE;
162
163         uimage_size = find_uImage_size(danube_partitions[2].offset);
164
165         if(detect_squashfs_partition(danube_partitions[2].offset + uimage_size)){
166                 printk("Found a squashfs following the uImage\n");
167         } else {
168                 uimage_size &= ~0xffff;
169                 uimage_size += 0x10000;
170         }
171
172         danube_partitions[2].size = uimage_size;
173         danube_partitions[3].offset = danube_partitions[2].offset + danube_partitions[2].size;
174         danube_partitions[3].size = ((danube_mtd->size >> 20) * 1024 * 1024) - danube_partitions[3].offset;
175
176         parts = &danube_partitions[0];
177         add_mtd_partitions(danube_mtd, parts, 4);
178
179         printk("Added danube flash with %dMB\n", danube_mtd->size >> 20);
180         return 0;
181 }
182
183 static void
184 __exit
185 cleanup_danube_mtd (void)
186 {
187 }
188
189 module_init (init_danube_mtd);
190 module_exit (cleanup_danube_mtd);
191
192 MODULE_LICENSE ("GPL");
193 MODULE_AUTHOR ("John Crispin <blogic@openwrt.org>");
194 MODULE_DESCRIPTION ("MTD map driver for DANUBE boards");