malbon has contributed to flash map driver as well, update the driver description
[openwrt/openwrt.git] / target / linux / brcm63xx-2.6 / patches / 040-bcm963xx_flashmap.patch
1 diff -urN linux-2.6.19/drivers/mtd/maps/Kconfig linux-2.6.19.new/drivers/mtd/maps/Kconfig
2 --- linux-2.6.19/drivers/mtd/maps/Kconfig 2006-11-29 22:57:37.000000000 +0100
3 +++ linux-2.6.19.new/drivers/mtd/maps/Kconfig 2006-12-18 17:21:07.000000000 +0100
4 @@ -224,6 +224,13 @@
5 Flash memory access on 4G Systems MTX-1 Board. If you have one of
6 these boards and would like to use the flash chips on it, say 'Y'.
7
8 +config MTD_BCM963XX
9 + tristate "BCM963xx Flash device"
10 + depends on MIPS && BCM963XX
11 + help
12 + Flash memory access on BCM963xx boards. Currently only works with
13 + RedBoot and CFE.
14 +
15 config MTD_DILNETPC
16 tristate "CFI Flash device mapped on DIL/Net PC"
17 depends on X86 && MTD_CONCAT && MTD_PARTITIONS && MTD_CFI_INTELEXT
18 diff -urN linux-2.6.19/drivers/mtd/maps/Makefile linux-2.6.19.new/drivers/mtd/maps/Makefile
19 --- linux-2.6.19/drivers/mtd/maps/Makefile 2006-11-29 22:57:37.000000000 +0100
20 +++ linux-2.6.19.new/drivers/mtd/maps/Makefile 2006-12-18 17:21:07.000000000 +0100
21 @@ -70,3 +70,4 @@
22 obj-$(CONFIG_MTD_OMAP_NOR) += omap_nor.o
23 obj-$(CONFIG_MTD_MTX1) += mtx-1_flash.o
24 obj-$(CONFIG_MTD_TQM834x) += tqm834x.o
25 +obj-$(CONFIG_MTD_BCM963XX) += bcm963xx-flash.o
26 diff -urN linux-2.6.19/drivers/mtd/maps/bcm963xx-flash.c linux-2.6.19.new/drivers/mtd/maps/bcm963xx-flash.c
27 --- linux-2.6.19/drivers/mtd/maps/bcm963xx-flash.c 1970-01-01 01:00:00.000000000 +0100
28 +++ linux-2.6.19.new/drivers/mtd/maps/bcm963xx-flash.c 2006-12-18 17:21:07.000000000 +0100
29 @@ -0,0 +1,277 @@
30 +/*
31 + * $Id$
32 + * Copyright (C) 2006 Florian Fainelli <florian@openwrt.org>
33 + * Mike Albon <malbon@openwrt.org>
34 + * Copyright (C) $Date$ $Author$
35 + *
36 + * This program is free software; you can redistribute it and/or modify
37 + * it under the terms of the GNU General Public License as published by
38 + * the Free Software Foundation; either version 2 of the License, or
39 + * (at your option) any later version.
40 + *
41 + * This program is distributed in the hope that it will be useful,
42 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
43 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
44 + * GNU General Public License for more details.
45 + *
46 + * You should have received a copy of the GNU General Public License
47 + * along with this program; if not, write to the Free Software
48 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
49 + */
50 +
51 +/* This is the BCM963xx flash map driver, in its actual state it only supports BCM96348 devices
52 + * this driver is able to manage both bootloader we found on these boards : CFE and RedBoot
53 + *
54 + * RedBoot :
55 + * - this bootloader allows us to parse partitions and therefore deduce the MTD partition table
56 + *
57 + * CFE :
58 + * - CFE partitionning can be detected as for BCM947xx devices
59 + *
60 + */
61 +
62 +#include <asm/io.h>
63 +#include <linux/init.h>
64 +#include <linux/kernel.h>
65 +#include <linux/mtd/map.h>
66 +#include <linux/mtd/mtd.h>
67 +#include <linux/mtd/partitions.h>
68 +#include <linux/vmalloc.h>
69 +#include <board.h>
70 +
71 +#define WINDOW_ADDR 0x1FC00000 /* Real address of the flash */
72 +#define WINDOW_SIZE 0x400000 /* Size of flash */
73 +#define BUSWIDTH 2 /* Buswidth */
74 +#define EXTENDED_SIZE 0xBFC00000 /* Extended flash address */
75 +#define IMAGE_LEN 10 /* Length of Length Field */
76 +#define ADDRESS_LEN 12 /* Length of Address field */
77 +#define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
78 +
79 +extern int boot_loader_type; /* For RedBoot / CFE detection */
80 +extern int parse_redboot_partitions(struct mtd_info *master, struct mtd_partition **pparts, unsigned long fis_origin);
81 +static struct mtd_partition *parsed_parts;
82 +
83 +static void __exit bcm963xx_mtd_cleanup(void);
84 +
85 +static struct mtd_info *bcm963xx_mtd_info;
86 +
87 +static struct map_info bcm963xx_map = {
88 + .name = "bcm963xx",
89 + .size = WINDOW_SIZE,
90 + .bankwidth = BUSWIDTH,
91 + .phys = WINDOW_ADDR,
92 +};
93 +
94 +
95 +int parse_cfe_partitions( struct mtd_info *master, struct mtd_partition **pparts)
96 +{
97 + int nrparts = 2, curpart = 0; // CFE and NVRAM always present.
98 + struct bcm963xx_cfe_map {
99 + unsigned char tagVersion[4]; // Version of the image tag
100 + unsigned char sig_1[20]; // Company Line 1
101 + unsigned char sig_2[14]; // Company Line 2
102 + unsigned char chipid[6]; // Chip this image is for
103 + unsigned char boardid[16]; // Board name
104 + unsigned char bigEndian[2]; // Map endianness -- 1 BE 0 LE
105 + unsigned char totalLength[IMAGE_LEN]; //Total length of image
106 + unsigned char cfeAddress[ADDRESS_LEN]; // Address in memory of CFE
107 + unsigned char cfeLength[IMAGE_LEN]; // Size of CFE
108 + unsigned char rootAddress[ADDRESS_LEN]; // Address in memory of rootfs
109 + unsigned char rootLength[IMAGE_LEN]; // Size of rootfs
110 + unsigned char kernelAddress[ADDRESS_LEN]; // Address in memory of kernel
111 + unsigned char kernelLength[IMAGE_LEN]; // Size of kernel
112 + unsigned char dualImage[2]; // Unused at present
113 + unsigned char inactiveFlag[2]; // Unused at present
114 + unsigned char reserved1[74]; // Reserved area not in use
115 + unsigned char imageCRC[4]; // CRC32 of images
116 + unsigned char reserved2[16]; // Unused at present
117 + unsigned char headerCRC[4]; // CRC32 of header excluding tagVersion
118 + unsigned char reserved3[16]; // Unused at present
119 + } *buf;
120 + struct mtd_partition *parts;
121 + int ret;
122 + size_t retlen;
123 + unsigned int rootfsaddr, kerneladdr, spareaddr;
124 + unsigned int rootfslen, kernellen, sparelen, totallen;
125 + int namelen = 0;
126 + int i;
127 + // Allocate memory for buffer
128 + buf = vmalloc(sizeof(struct bcm963xx_cfe_map));
129 +
130 + if (!buf)
131 + return -ENOMEM;
132 +
133 + // Get the tag
134 + ret = master->read(master,master->erasesize,sizeof(struct bcm963xx_cfe_map), &retlen, (void *)buf);
135 + if (retlen != sizeof(struct bcm963xx_cfe_map)){
136 + vfree(buf);
137 + return -EIO;
138 + };
139 + printk("bcm963xx: CFE boot tag found with version %s and board type %s.\n",buf->tagVersion,buf->boardid);
140 + // Get the values and calculate
141 + sscanf(buf->rootAddress,"%u", &rootfsaddr);
142 + rootfsaddr = rootfsaddr - EXTENDED_SIZE;
143 + sscanf(buf->rootLength, "%u", &rootfslen);
144 + sscanf(buf->kernelAddress, "%u", &kerneladdr);
145 + kerneladdr = kerneladdr - EXTENDED_SIZE;
146 + sscanf(buf->kernelLength, "%u", &kernellen);
147 + sscanf(buf->totalLength, "%u", &totallen);
148 + spareaddr = ROUNDUP(totallen,master->erasesize) + master->erasesize;
149 + sparelen = master->size - spareaddr - master->erasesize;
150 + // Determine number of partitions
151 + namelen = 8;
152 + if (rootfslen > 0){
153 + nrparts++;
154 + namelen =+ 6;
155 + };
156 + if (kernellen > 0){
157 + nrparts++;
158 + namelen =+ 6;
159 + };
160 + if (sparelen > 0){
161 + nrparts++;
162 + namelen =+ 6;
163 + };
164 + // Ask kernel for more memory.
165 + parts = kmalloc(sizeof(*parts)*nrparts+10*nrparts, GFP_KERNEL);
166 + if (!parts){
167 + vfree(buf);
168 + return -ENOMEM;
169 + };
170 + memset(parts,0,sizeof(*parts)*nrparts+10*nrparts);
171 + // Start building partition list
172 + parts[curpart].name = "CFE";
173 + parts[curpart].offset = 0;
174 + parts[curpart].size = master->erasesize;
175 + curpart++;
176 + if (kernellen > 0){
177 + parts[curpart].name = "Kernel";
178 + parts[curpart].offset = kerneladdr;
179 + parts[curpart].size = kernellen;
180 + curpart++;
181 + };
182 + if (rootfslen > 0){
183 + parts[curpart].name = "Rootfs";
184 + parts[curpart].offset = rootfsaddr;
185 + parts[curpart].size = rootfslen;
186 + curpart++;
187 + };
188 + if (sparelen > 0){
189 + parts[curpart].name = "OpenWrt";
190 + parts[curpart].offset = spareaddr;
191 + parts[curpart].size = sparelen;
192 + curpart++;
193 + };
194 + parts[curpart].name = "NVRAM";
195 + parts[curpart].offset = master->size - master->erasesize;
196 + parts[curpart].size = master->erasesize;
197 + for (i = 0; i < nrparts; i++) {
198 + printk("bcm963xx: Partition %d is %s offset %x and length %x\n", i, parts[i].name, parts[i].offset, parts[i].size);
199 + }
200 + *pparts = parts;
201 + vfree(buf);
202 + return nrparts;
203 +};
204 +
205 +static struct mtd_partition bcm963xx_parts[] = {
206 + { name: "bootloader", size: 0, offset: 0, mask_flags: MTD_WRITEABLE },
207 + { name: "rootfs", size: 0, offset: 0},
208 + { name: "jffs2", size: 5 * 0x10000, offset: 57*0x10000}
209 +};
210 +
211 +static int bcm963xx_parts_size = sizeof(bcm963xx_parts) / sizeof(bcm963xx_parts[0]);
212 +
213 +static int bcm963xx_detect_cfe(struct mtd_info *master)
214 +{
215 + int idoffset = 0x4e0;
216 + static char idstring[8] = "CFE1CFE1";
217 + char buf[8];
218 + int ret;
219 + size_t retlen;
220 +
221 + ret = master->read(master, idoffset, 8, &retlen, (void *)buf);
222 + printk("bcm963xx: Read Signature value of %s\n", buf);
223 + return strcmp(idstring,buf);
224 +}
225 +
226 +static int __init bcm963xx_mtd_init(void)
227 +{
228 + printk("bcm963xx: 0x%08x at 0x%08x\n", WINDOW_SIZE, WINDOW_ADDR);
229 + bcm963xx_map.virt = ioremap(WINDOW_ADDR, WINDOW_SIZE);
230 +
231 + if (!bcm963xx_map.virt) {
232 + printk("bcm963xx: Failed to ioremap\n");
233 + return -EIO;
234 + }
235 +
236 + simple_map_init(&bcm963xx_map);
237 +
238 + bcm963xx_mtd_info = do_map_probe("cfi_probe", &bcm963xx_map);
239 +
240 + if (bcm963xx_mtd_info) {
241 + bcm963xx_mtd_info->owner = THIS_MODULE;
242 +
243 + //if (boot_loader_type == BOOT_CFE)
244 + if (bcm963xx_detect_cfe(bcm963xx_mtd_info) == 0)
245 + {
246 + int parsed_nr_parts = 0;
247 + char * part_type;
248 + printk("bcm963xx: CFE bootloader detected\n");
249 + //add_mtd_device(bcm963xx_mtd_info);
250 + //add_mtd_partitions(bcm963xx_mtd_info, bcm963xx_parts, bcm963xx_parts_size);
251 + if (parsed_nr_parts == 0) {
252 + int ret = parse_cfe_partitions(bcm963xx_mtd_info, &parsed_parts);
253 + if (ret > 0) {
254 + part_type = "CFE";
255 + parsed_nr_parts = ret;
256 + }
257 + }
258 + add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
259 + return 0;
260 + }
261 + else
262 + {
263 + int parsed_nr_parts = 0;
264 + char * part_type;
265 +
266 + if (bcm963xx_mtd_info->size > 0x00400000) {
267 + printk("Support for extended flash memory size : 0x%08X ; ONLY 64MBIT SUPPORT\n", bcm963xx_mtd_info->size);
268 + bcm963xx_map.virt = (unsigned long)(EXTENDED_SIZE);
269 + }
270 +
271 +#ifdef CONFIG_MTD_REDBOOT_PARTS
272 + if (parsed_nr_parts == 0) {
273 + int ret = parse_redboot_partitions(bcm963xx_mtd_info, &parsed_parts, 0);
274 + if (ret > 0) {
275 + part_type = "RedBoot";
276 + parsed_nr_parts = ret;
277 + }
278 + }
279 +#endif
280 + add_mtd_partitions(bcm963xx_mtd_info, parsed_parts, parsed_nr_parts);
281 +
282 + return 0;
283 + }
284 + }
285 + iounmap(bcm963xx_map.virt);
286 + return -ENXIO;
287 +}
288 +
289 +static void __exit bcm963xx_mtd_cleanup(void)
290 +{
291 + if (bcm963xx_mtd_info) {
292 + del_mtd_partitions(bcm963xx_mtd_info);
293 + map_destroy(bcm963xx_mtd_info);
294 + }
295 +
296 + if (bcm963xx_map.virt) {
297 + iounmap(bcm963xx_map.virt);
298 + bcm963xx_map.virt = 0;
299 + }
300 +}
301 +
302 +module_init(bcm963xx_mtd_init);
303 +module_exit(bcm963xx_mtd_cleanup);
304 +
305 +MODULE_LICENSE("GPL");
306 +MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org> Mike Albon <malbon@openwrt.org>");
307 diff -urN linux-2.6.19/drivers/mtd/redboot.c linux-2.6.19.new/drivers/mtd/redboot.c
308 --- linux-2.6.19/drivers/mtd/redboot.c 2006-12-18 17:09:14.000000000 +0100
309 +++ linux-2.6.19.new/drivers/mtd/redboot.c 2006-12-18 17:14:26.000000000 +0100
310 @@ -39,7 +39,7 @@
311 return 1;
312 }
313
314 -static int parse_redboot_partitions(struct mtd_info *master,
315 +int parse_redboot_partitions(struct mtd_info *master,
316 struct mtd_partition **pparts,
317 unsigned long fis_origin)
318 {
319 @@ -132,6 +132,14 @@
320 goto out;
321 }
322
323 + if (!fis_origin) {
324 + for (i = 0; i < numslots; i++) {
325 + if (!strncmp(buf[i].name, "RedBoot", 8)) {
326 + fis_origin = (buf[i].flash_base & (master->size << 1) - 1);
327 + }
328 + }
329 + }
330 +
331 for (i = 0; i < numslots; i++) {
332 struct fis_list *new_fl, **prev;
333
334 @@ -154,9 +162,8 @@
335 new_fl->img = &buf[i];
336 if (fis_origin) {
337 buf[i].flash_base -= fis_origin;
338 - } else {
339 - buf[i].flash_base &= master->size-1;
340 }
341 + buf[i].flash_base &= (master->size << 1) - 1;
342
343 /* I'm sure the JFFS2 code has done me permanent damage.
344 * I now think the following is _normal_