backport flash-map driver and serial setup from 2.6 for netgear wgt634, split flash...
[openwrt/svn-archive/archive.git] / openwrt / target / linux / linux-2.4 / patches / brcm / 004-flash-map.patch
1 diff -Nur linux-2.4.32/drivers/mtd/maps/bcm947xx-flash.c linux-2.4.32-flash/drivers/mtd/maps/bcm947xx-flash.c
2 --- linux-2.4.32/drivers/mtd/maps/bcm947xx-flash.c 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.4.32-flash/drivers/mtd/maps/bcm947xx-flash.c 2005-12-19 01:29:52.464670750 +0100
4 @@ -0,0 +1,366 @@
5 +/*
6 + * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
7 + * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
8 + *
9 + * original functions for finding root filesystem from Mike Baker
10 + *
11 + * This program is free software; you can redistribute it and/or modify it
12 + * under the terms of the GNU General Public License as published by the
13 + * Free Software Foundation; either version 2 of the License, or (at your
14 + * option) any later version.
15 + *
16 + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 + * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
19 + * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
22 + * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
23 + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 + *
27 + * You should have received a copy of the GNU General Public License along
28 + * with this program; if not, write to the Free Software Foundation, Inc.,
29 + * 675 Mass Ave, Cambridge, MA 02139, USA.
30 + *
31 + *
32 + * Copyright 2004, Broadcom Corporation
33 + * All Rights Reserved.
34 + *
35 + * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
36 + * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
37 + * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
38 + * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
39 + *
40 + * Flash mapping for BCM947XX boards
41 + *
42 + */
43 +
44 +#include <linux/module.h>
45 +#include <linux/types.h>
46 +#include <linux/kernel.h>
47 +#include <asm/io.h>
48 +#include <linux/mtd/mtd.h>
49 +#include <linux/mtd/map.h>
50 +#ifdef CONFIG_MTD_PARTITIONS
51 +#include <linux/mtd/partitions.h>
52 +#endif
53 +#include <linux/config.h>
54 +
55 +#include <trxhdr.h>
56 +
57 +#define WINDOW_ADDR 0x1c000000
58 +#define WINDOW_SIZE (0x400000*2)
59 +#define BUSWIDTH 2
60 +
61 +static struct mtd_info *bcm947xx_mtd;
62 +
63 +__u8 bcm947xx_map_read8(struct map_info *map, unsigned long ofs)
64 +{
65 + if (map->map_priv_2 == 1)
66 + return __raw_readb(map->map_priv_1 + ofs);
67 +
68 + u16 val = __raw_readw(map->map_priv_1 + (ofs & ~1));
69 + if (ofs & 1)
70 + return ((val >> 8) & 0xff);
71 + else
72 + return (val & 0xff);
73 +}
74 +
75 +__u16 bcm947xx_map_read16(struct map_info *map, unsigned long ofs)
76 +{
77 + return __raw_readw(map->map_priv_1 + ofs);
78 +}
79 +
80 +__u32 bcm947xx_map_read32(struct map_info *map, unsigned long ofs)
81 +{
82 + return __raw_readl(map->map_priv_1 + ofs);
83 +}
84 +
85 +void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
86 +{
87 + if (len==1) {
88 + memcpy_fromio(to, map->map_priv_1 + from, len);
89 + } else {
90 + int i;
91 + u16 *dest = (u16 *) to;
92 + u16 *src = (u16 *) (map->map_priv_1 + from);
93 + for (i = 0; i < (len / 2); i++) {
94 + dest[i] = src[i];
95 + }
96 + if (len & 1)
97 + *((u8 *)dest+len-1) = src[i] & 0xff;
98 + }
99 +}
100 +
101 +void bcm947xx_map_write8(struct map_info *map, __u8 d, unsigned long adr)
102 +{
103 + __raw_writeb(d, map->map_priv_1 + adr);
104 + mb();
105 +}
106 +
107 +void bcm947xx_map_write16(struct map_info *map, __u16 d, unsigned long adr)
108 +{
109 + __raw_writew(d, map->map_priv_1 + adr);
110 + mb();
111 +}
112 +
113 +void bcm947xx_map_write32(struct map_info *map, __u32 d, unsigned long adr)
114 +{
115 + __raw_writel(d, map->map_priv_1 + adr);
116 + mb();
117 +}
118 +
119 +void bcm947xx_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
120 +{
121 + memcpy_toio(map->map_priv_1 + to, from, len);
122 +}
123 +
124 +struct map_info bcm947xx_map = {
125 + name: "Physically mapped flash",
126 + size: WINDOW_SIZE,
127 + buswidth: BUSWIDTH,
128 + read8: bcm947xx_map_read8,
129 + read16: bcm947xx_map_read16,
130 + read32: bcm947xx_map_read32,
131 + copy_from: bcm947xx_map_copy_from,
132 + write8: bcm947xx_map_write8,
133 + write16: bcm947xx_map_write16,
134 + write32: bcm947xx_map_write32,
135 + copy_to: bcm947xx_map_copy_to
136 +};
137 +
138 +#ifdef CONFIG_MTD_PARTITIONS
139 +
140 +static struct mtd_partition bcm947xx_parts[] = {
141 + { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
142 + { name: "linux", offset: 0, size: 0, },
143 + { name: "rootfs", offset: 0, size: 0, },
144 + { name: "nvram", offset: 0, size: 0, },
145 + { name: "OpenWrt", offset: 0, size: 0, },
146 + { name: NULL, },
147 +};
148 +
149 +static int __init
150 +find_cfe_size(struct mtd_info *mtd, size_t size)
151 +{
152 + struct trx_header *trx;
153 + unsigned char buf[512];
154 + int off;
155 + size_t len;
156 + int cfe_size_flag;
157 +
158 + trx = (struct trx_header *) buf;
159 +
160 + cfe_size_flag=0;
161 +
162 + for (off = (256*1024); off < size; off += mtd->erasesize) {
163 + memset(buf, 0xe5, sizeof(buf));
164 +
165 + /*
166 + * Read into buffer
167 + */
168 + if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
169 + len != sizeof(buf))
170 + continue;
171 +
172 + /* found a TRX header */
173 + if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
174 + goto done;
175 + }
176 + cfe_size_flag += 1;
177 + }
178 +
179 + printk(KERN_NOTICE
180 + "%s: Couldn't find bootloader size\n",
181 + mtd->name);
182 + return -1;
183 +
184 + done:
185 + printk(KERN_NOTICE "bootloader size flag: %d\n", cfe_size_flag);
186 + return cfe_size_flag;
187 +
188 +}
189 +
190 +static int __init
191 +find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
192 +{
193 + struct trx_header *trx;
194 + unsigned char buf[512];
195 + int off;
196 + size_t len;
197 +
198 + trx = (struct trx_header *) buf;
199 +
200 + for (off = (256*1024); off < size; off += mtd->erasesize) {
201 + memset(buf, 0xe5, sizeof(buf));
202 +
203 + /*
204 + * Read into buffer
205 + */
206 + if (MTD_READ(mtd, off, sizeof(buf), &len, buf) ||
207 + len != sizeof(buf))
208 + continue;
209 +
210 + /* found a TRX header */
211 + if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
212 + part->offset = le32_to_cpu(trx->offsets[2]) ? :
213 + le32_to_cpu(trx->offsets[1]);
214 + part->size = le32_to_cpu(trx->len);
215 +
216 + part->size -= part->offset;
217 + part->offset += off;
218 +
219 + goto done;
220 + }
221 + }
222 +
223 + printk(KERN_NOTICE
224 + "%s: Couldn't find root filesystem\n",
225 + mtd->name);
226 + return -1;
227 +
228 + done:
229 + return part->size;
230 +}
231 +
232 +struct mtd_partition * __init
233 +init_mtd_partitions(struct mtd_info *mtd, size_t size)
234 +{
235 +
236 + int cfe_size_flag;
237 +
238 + /* if cfe_size_flag=0, cfe size is 256 kb, else 384 kb */
239 + cfe_size_flag = find_cfe_size(mtd,size);
240 +
241 + /* boot loader */
242 + bcm947xx_parts[0].offset = 0;
243 + if (cfe_size_flag == 0) {
244 + bcm947xx_parts[0].size = 1024*256;
245 + } else {
246 + /* netgear wgt634u has 384 kb bootloader */
247 + bcm947xx_parts[0].size = 1024*384;
248 + }
249 +
250 + /* nvram */
251 + if (cfe_size_flag == 0) {
252 + bcm947xx_parts[3].offset = size - mtd->erasesize;
253 + } else {
254 + /* nvram (old 128kb config partition on netgear wgt634u) */
255 + bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
256 + }
257 + bcm947xx_parts[3].size = mtd->erasesize;
258 +
259 + /* linux (kernel and rootfs) */
260 + if (cfe_size_flag == 0) {
261 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
262 + bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
263 + bcm947xx_parts[1].offset;
264 + } else {
265 + /* do not count the elf loader, which is on one block */
266 + bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
267 + bcm947xx_parts[3].size + mtd->erasesize;
268 + bcm947xx_parts[1].size = size -
269 + bcm947xx_parts[0].size -
270 + (2*bcm947xx_parts[3].size) -
271 + mtd->erasesize;
272 + }
273 +
274 + /* find and size rootfs */
275 + if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
276 + /* entirely jffs2 */
277 + bcm947xx_parts[4].name = NULL;
278 + bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset -
279 + bcm947xx_parts[3].size;
280 + } else {
281 + /* legacy setup */
282 + /* calculate leftover flash, and assign it to the jffs2 partition */
283 + if (cfe_size_flag == 0) {
284 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
285 + bcm947xx_parts[2].size;
286 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
287 + bcm947xx_parts[4].offset += mtd->erasesize -
288 + (bcm947xx_parts[4].offset % mtd->erasesize);
289 + }
290 + bcm947xx_parts[4].size = bcm947xx_parts[3].offset -
291 + bcm947xx_parts[4].offset;
292 + } else {
293 + bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
294 + bcm947xx_parts[2].size;
295 + if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
296 + bcm947xx_parts[4].offset += mtd->erasesize -
297 + (bcm947xx_parts[4].offset % mtd->erasesize);
298 + }
299 + bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
300 + bcm947xx_parts[4].offset;
301 + }
302 + }
303 +
304 + return bcm947xx_parts;
305 +}
306 +
307 +#endif
308 +
309 +
310 +mod_init_t init_bcm947xx_map(void)
311 +{
312 + size_t size;
313 + int ret = 0;
314 +#ifdef CONFIG_MTD_PARTITIONS
315 + struct mtd_partition *parts;
316 + int i;
317 +#endif
318 +
319 + bcm947xx_map.map_priv_1 = (unsigned long) ioremap(WINDOW_ADDR, WINDOW_SIZE);
320 +
321 + if (!bcm947xx_map.map_priv_1) {
322 + printk(KERN_ERR "Failed to ioremap\n");
323 + return -EIO;
324 + }
325 +
326 + if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
327 + printk(KERN_ERR "pflash: cfi_probe failed\n");
328 + iounmap((void *)bcm947xx_map.map_priv_1);
329 + return -ENXIO;
330 + }
331 +
332 + bcm947xx_mtd->module = THIS_MODULE;
333 +
334 + size = bcm947xx_mtd->size;
335 +
336 + printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
337 +
338 +#ifdef CONFIG_MTD_PARTITIONS
339 + parts = init_mtd_partitions(bcm947xx_mtd, size);
340 + for (i = 0; parts[i].name; i++);
341 + ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
342 + if (ret) {
343 + printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
344 + goto fail;
345 + }
346 +#endif
347 +
348 + return 0;
349 +
350 + fail:
351 + if (bcm947xx_mtd)
352 + map_destroy(bcm947xx_mtd);
353 + if (bcm947xx_map.map_priv_1)
354 + iounmap((void *) bcm947xx_map.map_priv_1);
355 + bcm947xx_map.map_priv_1 = 0;
356 + return ret;
357 +}
358 +
359 +mod_exit_t cleanup_bcm947xx_map(void)
360 +{
361 +#ifdef CONFIG_MTD_PARTITIONS
362 + del_mtd_partitions(bcm947xx_mtd);
363 +#endif
364 + map_destroy(bcm947xx_mtd);
365 + iounmap((void *) bcm947xx_map.map_priv_1);
366 + bcm947xx_map.map_priv_1 = 0;
367 +}
368 +
369 +module_init(init_bcm947xx_map);
370 +module_exit(cleanup_bcm947xx_map);
371 diff -Nur linux-2.4.32/drivers/mtd/maps/Config.in linux-2.4.32-flash/drivers/mtd/maps/Config.in
372 --- linux-2.4.32/drivers/mtd/maps/Config.in 2003-06-13 16:51:34.000000000 +0200
373 +++ linux-2.4.32-flash/drivers/mtd/maps/Config.in 2005-12-18 15:53:52.003277250 +0100
374 @@ -48,6 +48,7 @@
375 fi
376
377 if [ "$CONFIG_MIPS" = "y" ]; then
378 + dep_tristate ' CFI Flash device mapped on Broadcom BCM947XX boards' CONFIG_MTD_BCM947XX $CONFIG_MTD_CFI
379 dep_tristate ' Pb1000 MTD support' CONFIG_MTD_PB1000 $CONFIG_MIPS_PB1000
380 dep_tristate ' Pb1500 MTD support' CONFIG_MTD_PB1500 $CONFIG_MIPS_PB1500
381 dep_tristate ' Pb1100 MTD support' CONFIG_MTD_PB1100 $CONFIG_MIPS_PB1100
382 diff -Nur linux-2.4.32/drivers/mtd/maps/Makefile linux-2.4.32-flash/drivers/mtd/maps/Makefile
383 --- linux-2.4.32/drivers/mtd/maps/Makefile 2003-06-13 16:51:34.000000000 +0200
384 +++ linux-2.4.32-flash/drivers/mtd/maps/Makefile 2005-12-18 15:54:39.022215750 +0100
385 @@ -3,6 +3,8 @@
386 #
387 # $Id: Makefile,v 1.37 2003/01/24 14:26:38 dwmw2 Exp $
388
389 +EXTRA_CFLAGS := -I$(TOPDIR)/arch/mips/bcm947xx/include
390 +
391 BELOW25 := $(shell echo $(PATCHLEVEL) | sed s/[1234]/y/)
392
393 ifeq ($(BELOW25),y)
394 @@ -10,6 +12,7 @@
395 endif
396
397 # Chip mappings
398 +obj-$(CONFIG_MTD_BCM947XX) += bcm947xx-flash.o
399 obj-$(CONFIG_MTD_CDB89712) += cdb89712.o
400 obj-$(CONFIG_MTD_ARM_INTEGRATOR)+= integrator-flash.o
401 obj-$(CONFIG_MTD_CFI_FLAGADM) += cfi_flagadm.o