brcm63xx: rename mtd partitions to the ones used by other openwrt platforms - should...
[openwrt/svn-archive/archive.git] / target / linux / brcm63xx / files / drivers / mtd / maps / bcm963xx-flash.c
1 /*
2 * $Id$
3 * Copyright (C) 2006 Florian Fainelli <florian@openwrt.org>
4 * Mike Albon <malbon@openwrt.org>
5 * Copyright (C) $Date$ $Author$
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /* This is the BCM963xx flash map driver, in its actual state it only supports BCM96348 devices
23 * this driver is able to manage both bootloader we found on these boards : CFE and RedBoot
24 *
25 * RedBoot :
26 * - this bootloader allows us to parse partitions and therefore deduce the MTD partition table
27 *
28 * CFE :
29 * - CFE partitionning can be detected as for BCM947xx devices
30 *
31 */
32
33 #include <linux/init.h>
34 #include <linux/kernel.h>
35 #include <linux/mtd/map.h>
36 #include <linux/mtd/mtd.h>
37 #include <linux/mtd/partitions.h>
38 #include <linux/vmalloc.h>
39
40 #include <asm/io.h>
41
42 #define WINDOW_ADDR 0x1FC00000 /* Real address of the flash */
43 #define WINDOW_SIZE 0x400000 /* Size of flash */
44 #define BUSWIDTH 2 /* Buswidth */
45 #define EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
46 #define IMAGE_LEN 10 /* Length of Length Field */
47 #define ADDRESS_LEN 12 /* Length of Address field */
48 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
49
50 extern int boot_loader_type; /* For RedBoot / CFE detection */
51 extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
52 static struct mtd_partition *parsed_parts;
53
54 static void __exit bcm963xx_mtd_cleanup(void);
55
56 static struct mtd_info *bcm963xx_mtd_info;
57
58 static struct map_info bcm963xx_map = {
59 .name = "bcm963xx",
60 .size = WINDOW_SIZE,
61 .bankwidth = BUSWIDTH,
62 .phys = WINDOW_ADDR,
63 };
64
65
66 int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
67 {
68 int nrparts = 2, curpart = 0; // CFE and NVRAM always present.
69 struct bcm963xx_cfe_map {
70 unsigned char tagVersion[4]; // Version of the image tag
71 unsigned char sig_1[20]; // Company Line 1
72 unsigned char sig_2[14]; // Company Line 2
73 unsigned char chipid[6]; // Chip this image is for
74 unsigned char boardid[16]; // Board name
75 unsigned char bigEndian[2]; // Map endianness -- 1 BE 0 LE
76 unsigned char totalLength[IMAGE_LEN]; //Total length of image
77 unsigned char cfeAddress[ADDRESS_LEN]; // Address in memory of CFE
78 unsigned char cfeLength[IMAGE_LEN]; // Size of CFE
79 unsigned char rootAddress[ADDRESS_LEN]; // Address in memory of rootfs
80 unsigned char rootLength[IMAGE_LEN]; // Size of rootfs
81 unsigned char kernelAddress[ADDRESS_LEN]; // Address in memory of kernel
82 unsigned char kernelLength[IMAGE_LEN]; // Size of kernel
83 unsigned char dualImage[2]; // Unused at present
84 unsigned char inactiveFlag[2]; // Unused at present
85 unsigned char reserved1[74]; // Reserved area not in use
86 unsigned char imageCRC[4]; // CRC32 of images
87 unsigned char reserved2[16]; // Unused at present
88 unsigned char headerCRC[4]; // CRC32 of header excluding tagVersion
89 unsigned char reserved3[16]; // Unused at present
90 } *buf;
91 struct mtd_partition *parts;
92 int ret;
93 size_t retlen;
94 unsigned int rootfsaddr, kerneladdr, spareaddr;
95 unsigned int rootfslen, kernellen, sparelen, totallen;
96 int namelen = 0;
97 int i;
98 // Allocate memory for buffer
99 buf = vmalloc(sizeof(struct bcm963xx_cfe_map));
100
101 if (!buf)
102 return -ENOMEM;
103
104 // Get the tag
105 ret = master->read(master,master->erasesize,sizeof(struct bcm963xx_cfe_map), &retlen, (void *)buf);
106 if (retlen != sizeof(struct bcm963xx_cfe_map)){
107 vfree(buf);
108 return -EIO;
109 };
110 printk("bcm963xx: CFE boot tag found with version %s and board type %s.\n",buf->tagVersion,buf->boardid);
111 // Get the values and calculate
112 sscanf(buf->rootAddress,"%u", &rootfsaddr);
113 rootfsaddr = rootfsaddr - EXTENDED_SIZE;
114 sscanf(buf->rootLength, "%u", &rootfslen);
115 sscanf(buf->kernelAddress, "%u", &kerneladdr);
116 kerneladdr = kerneladdr - EXTENDED_SIZE;
117 sscanf(buf->kernelLength, "%u", &kernellen);
118 sscanf(buf->totalLength, "%u", &totallen);
119 spareaddr = ROUNDUP(totallen,master->erasesize) + master->erasesize;
120 sparelen = master->size - spareaddr - master->erasesize;
121 // Determine number of partitions
122 namelen = 8;
123 if (rootfslen > 0){
124 nrparts++;
125 namelen =+ 6;
126 };
127 if (kernellen > 0){
128 nrparts++;
129 namelen =+ 6;
130 };
131 // Ask kernel for more memory.
132 parts = kmalloc(sizeof(*parts)*nrparts+10*nrparts, GFP_KERNEL);
133 if (!parts){
134 vfree(buf);
135 return -ENOMEM;
136 };
137 memset(parts,0,sizeof(*parts)*nrparts+10*nrparts);
138 // Start building partition list
139 parts[curpart].name = "CFE";
140 parts[curpart].offset = 0;
141 parts[curpart].size = master->erasesize;
142 curpart++;
143 if (kernellen > 0){
144 parts[curpart].name = "kernel";
145 parts[curpart].offset = kerneladdr;
146 parts[curpart].size = kernellen;
147 curpart++;
148 };
149 if (rootfslen > 0){
150 parts[curpart].name = "rootfs";
151 parts[curpart].offset = rootfsaddr;
152 parts[curpart].size = rootfslen;
153 if (sparelen > 0)
154 parts[curpart].size += sparelen;
155 curpart++;
156 };
157 parts[curpart].name = "nvram";
158 parts[curpart].offset = master->size - master->erasesize;
159 parts[curpart].size = master->erasesize;
160 for (i = 0; i < nrparts; i++) {
161 printk("bcm963xx: Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
162 }
163 *pparts = parts;
164 vfree(buf);
165 return nrparts;
166 };
167
168 static struct mtd_partition bcm963xx_parts[] = {
169 { name: "bootloader", size: 0, offset: 0, mask_flags: MTD_WRITEABLE },
170 { name: "rootfs", size: 0, offset: 0},
171 { name: "jffs2", size: 5 * 0x10000, offset: 57*0x10000}
172 };
173
174 static int bcm963xx_parts_size = sizeof(bcm963xx_parts) / sizeof(bcm963xx_parts[0]);
175
176 static int bcm963xx_detect_cfe(struct mtd_info *master)
177 {
178 int idoffset = 0x4e0;
179 static char idstring[8] = "CFE1CFE1";
180 char buf[8];
181 int ret;
182 size_t retlen;
183
184 ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
185 printk("bcm963xx: Read Signature value of %s\n", buf);
186 return strcmp(idstring,buf);
187 }
188
189 static int __init bcm963xx_mtd_init(void)
190 {
191 printk("bcm963xx: 0x%08x at 0x%08x\n", WINDOW_SIZE, WINDOW_ADDR);
192 bcm963xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
193
194 if (!bcm963xx_map.virt) {
195 printk("bcm963xx: Failed to ioremap\n");
196 return -EIO;
197 }
198
199 simple_map_init(&bcm963xx_map);
200
201 bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
202
203 if (bcm963xx_mtd_info) {
204 bcm963xx_mtd_info->owner = THIS_MODULE;
205
206 //if (boot_loader_type == BOOT_CFE)
207 if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0)
208 {
209 int parsed_nr_parts = 0;
210 char * part_type;
211 printk("bcm963xx: CFE bootloader detected\n");
212 //add_mtd_device(bcm963xx_mtd_info);
213 //add_mtd_partitions(bcm963xx_mtd_info, bcm963xx_parts, bcm963xx_parts_size);
214 if (parsed_nr_parts == 0) {
215 int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
216 if (ret > 0) {
217 part_type = "CFE";
218 parsed_nr_parts = ret;
219 }
220 }
221 add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
222 return 0;
223 }
224 else
225 {
226 int parsed_nr_parts = 0;
227 char * part_type;
228
229 if (bcm963xx_mtd_info->size > 0x00400000) {
230 printk("Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
231 bcm963xx_map.virt = (unsigned long)(EXTENDED_SIZE);
232 }
233
234 #ifdef CONFIG_MTD_REDBOOT_PARTS
235 if (parsed_nr_parts == 0) {
236 int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
237 if (ret > 0) {
238 part_type = "RedBoot";
239 parsed_nr_parts = ret;
240 }
241 }
242 #endif
243 add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
244
245 return 0;
246 }
247 }
248 iounmap(bcm963xx_map.virt);
249 return -ENXIO;
250 }
251
252 static void __exit bcm963xx_mtd_cleanup(void)
253 {
254 if (bcm963xx_mtd_info) {
255 del_mtd_partitions(bcm963xx_mtd_info);
256 map_destroy(bcm963xx_mtd_info);
257 }
258
259 if (bcm963xx_map.virt) {
260 iounmap(bcm963xx_map.virt);
261 bcm963xx_map.virt = 0;
262 }
263 }
264
265 module_init(bcm963xx_mtd_init);
266 module_exit(bcm963xx_mtd_cleanup);
267
268 MODULE_LICENSE("GPL");
269 MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org> Mike Albon <malbon@openwrt.org>");