update work in progress rewritten bcm947xx code. wifi and usb seem to be working...
[openwrt/openwrt.git] / target / linux / brcm47xx-2.6 / files / drivers / mtd / maps / bcm47xx-flash.c
1 /*
2 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
4 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
5 *
6 * original functions for finding root filesystem from Mike Baker
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version.
12 *
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
14 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
16 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
19 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 675 Mass Ave, Cambridge, MA 02139, USA.
27 *
28 * Copyright 2001-2003, Broadcom Corporation
29 * All Rights Reserved.
30 *
31 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
32 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
33 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
34 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
35 *
36 * Flash mapping for BCM947XX boards
37 */
38
39 #include <linux/init.h>
40 #include <linux/module.h>
41 #include <linux/types.h>
42 #include <linux/kernel.h>
43 #include <linux/wait.h>
44 #include <linux/mtd/mtd.h>
45 #include <linux/mtd/map.h>
46 #ifdef CONFIG_MTD_PARTITIONS
47 #include <linux/mtd/partitions.h>
48 #endif
49 #include <linux/squashfs_fs.h>
50 #include <linux/jffs2.h>
51 #include <linux/crc32.h>
52 #include <linux/ssb/ssb.h>
53 #include <asm/io.h>
54
55
56 #define TRX_MAGIC 0x30524448 /* "HDR0" */
57 #define TRX_VERSION 1
58 #define TRX_MAX_LEN 0x3A0000
59 #define TRX_NO_HEADER 1 /* Do not write TRX header */
60 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
61 #define TRX_MAX_OFFSET 3
62
63 struct trx_header {
64 u32 magic; /* "HDR0" */
65 u32 len; /* Length of file including header */
66 u32 crc32; /* 32-bit CRC from flag_version to end of file */
67 u32 flag_version; /* 0:15 flags, 16:31 version */
68 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
69 };
70
71 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
72 #define NVRAM_SPACE 0x8000
73 #define WINDOW_ADDR 0x1fc00000
74 #define WINDOW_SIZE 0x400000
75 #define BUSWIDTH 2
76
77 extern struct ssb_bus ssb;
78 static struct mtd_info *bcm947xx_mtd;
79
80 static void bcm947xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
81 {
82 #define MIPS_MEMCPY_ALIGN 4
83 map_word ret;
84 ssize_t transfer;
85 ssize_t done = 0;
86 if ((len >= MIPS_MEMCPY_ALIGN) && (!(from & (MIPS_MEMCPY_ALIGN - 1))) && (!(((unsigned int)to & (MIPS_MEMCPY_ALIGN - 1))))) {
87 done = len & ~(MIPS_MEMCPY_ALIGN - 1);
88 memcpy_fromio(to, map->virt + from, done);
89 }
90 while (done < len) {
91 ret = map->read(map, from + done);
92 transfer = len - done;
93 if (transfer > map->bankwidth)
94 transfer = map->bankwidth;
95 memcpy((void *)((unsigned long)to + done), &ret.x[0], transfer);
96 done += transfer;
97 }
98 }
99
100 static struct map_info bcm947xx_map = {
101 name: "Physically mapped flash",
102 size: WINDOW_SIZE,
103 bankwidth: BUSWIDTH,
104 phys: WINDOW_ADDR,
105 };
106
107 #ifdef CONFIG_MTD_PARTITIONS
108
109 static struct mtd_partition bcm947xx_parts[] = {
110 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
111 { name: "linux", offset: 0, size: 0, },
112 { name: "rootfs", offset: 0, size: 0, },
113 { name: "nvram", offset: 0, size: 0, },
114 { name: "OpenWrt", offset: 0, size: 0, },
115 { name: NULL, },
116 };
117
118 static int __init
119 find_cfe_size(struct mtd_info *mtd, size_t size)
120 {
121 struct trx_header *trx;
122 unsigned char buf[512];
123 int off;
124 size_t len;
125 int blocksize;
126
127 trx = (struct trx_header *) buf;
128
129 blocksize = mtd->erasesize;
130 if (blocksize < 0x10000)
131 blocksize = 0x10000;
132
133 for (off = (128*1024); off < size; off += blocksize) {
134 memset(buf, 0xe5, sizeof(buf));
135
136 /*
137 * Read into buffer
138 */
139 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
140 len != sizeof(buf))
141 continue;
142
143 /* found a TRX header */
144 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
145 goto found;
146 }
147 }
148
149 printk(KERN_NOTICE
150 "%s: Couldn't find bootloader size\n",
151 mtd->name);
152 return -1;
153
154 found:
155 printk(KERN_NOTICE "bootloader size: %d\n", off);
156 return off;
157
158 }
159
160 /*
161 * Copied from mtdblock.c
162 *
163 * Cache stuff...
164 *
165 * Since typical flash erasable sectors are much larger than what Linux's
166 * buffer cache can handle, we must implement read-modify-write on flash
167 * sectors for each block write requests. To avoid over-erasing flash sectors
168 * and to speed things up, we locally cache a whole flash sector while it is
169 * being written to until a different sector is required.
170 */
171
172 static void erase_callback(struct erase_info *done)
173 {
174 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
175 wake_up(wait_q);
176 }
177
178 static int erase_write (struct mtd_info *mtd, unsigned long pos,
179 int len, const char *buf)
180 {
181 struct erase_info erase;
182 DECLARE_WAITQUEUE(wait, current);
183 wait_queue_head_t wait_q;
184 size_t retlen;
185 int ret;
186
187 /*
188 * First, let's erase the flash block.
189 */
190
191 init_waitqueue_head(&wait_q);
192 erase.mtd = mtd;
193 erase.callback = erase_callback;
194 erase.addr = pos;
195 erase.len = len;
196 erase.priv = (u_long)&wait_q;
197
198 set_current_state(TASK_INTERRUPTIBLE);
199 add_wait_queue(&wait_q, &wait);
200
201 ret = mtd->erase(mtd, &erase);
202 if (ret) {
203 set_current_state(TASK_RUNNING);
204 remove_wait_queue(&wait_q, &wait);
205 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
206 "on \"%s\" failed\n",
207 pos, len, mtd->name);
208 return ret;
209 }
210
211 schedule(); /* Wait for erase to finish. */
212 remove_wait_queue(&wait_q, &wait);
213
214 /*
215 * Next, writhe data to flash.
216 */
217
218 ret = mtd->write (mtd, pos, len, &retlen, buf);
219 if (ret)
220 return ret;
221 if (retlen != len)
222 return -EIO;
223 return 0;
224 }
225
226
227
228
229 static int __init
230 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
231 {
232 struct trx_header trx, *trx2;
233 unsigned char buf[512], *block;
234 int off, blocksize;
235 u32 i, crc = ~0;
236 size_t len;
237 struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
238
239 blocksize = mtd->erasesize;
240 if (blocksize < 0x10000)
241 blocksize = 0x10000;
242
243 for (off = (128*1024); off < size; off += blocksize) {
244 memset(&trx, 0xe5, sizeof(trx));
245
246 /*
247 * Read into buffer
248 */
249 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
250 len != sizeof(trx))
251 continue;
252
253 /* found a TRX header */
254 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
255 part->offset = le32_to_cpu(trx.offsets[2]) ? :
256 le32_to_cpu(trx.offsets[1]);
257 part->size = le32_to_cpu(trx.len);
258
259 part->size -= part->offset;
260 part->offset += off;
261
262 goto found;
263 }
264 }
265
266 printk(KERN_NOTICE
267 "%s: Couldn't find root filesystem\n",
268 mtd->name);
269 return -1;
270
271 found:
272 if (part->size == 0)
273 return 0;
274
275 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
276 return 0;
277
278 if (*((__u32 *) buf) == SQUASHFS_MAGIC) {
279 printk(KERN_INFO "%s: Filesystem type: squashfs, size=0x%x\n", mtd->name, (u32) sb->bytes_used);
280
281 /* Update the squashfs partition size based on the superblock info */
282 part->size = sb->bytes_used;
283 len = part->offset + part->size;
284 len += (mtd->erasesize - 1);
285 len &= ~(mtd->erasesize - 1);
286 part->size = len - part->offset;
287 } else if (*((__u16 *) buf) == JFFS2_MAGIC_BITMASK) {
288 printk(KERN_INFO "%s: Filesystem type: jffs2\n", mtd->name);
289
290 /* Move the squashfs outside of the trx */
291 part->size = 0;
292 } else {
293 printk(KERN_INFO "%s: Filesystem type: unknown\n", mtd->name);
294 return 0;
295 }
296
297 if (trx.len != part->offset + part->size - off) {
298 /* Update the trx offsets and length */
299 trx.len = part->offset + part->size - off;
300
301 /* Update the trx crc32 */
302 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
303 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
304 return 0;
305 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
306 }
307 trx.crc32 = crc;
308
309 /* read first eraseblock from the trx */
310 block = kmalloc(mtd->erasesize, GFP_KERNEL);
311 trx2 = (struct trx_header *) block;
312 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
313 printk("Error accessing the first trx eraseblock\n");
314 return 0;
315 }
316
317 printk("Updating TRX offsets and length:\n");
318 printk("old trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx2->offsets[0], trx2->offsets[1], trx2->offsets[2], trx2->len, trx2->crc32);
319 printk("new trx = [0x%08x, 0x%08x, 0x%08x], len=0x%08x crc32=0x%08x\n", trx.offsets[0], trx.offsets[1], trx.offsets[2], trx.len, trx.crc32);
320
321 /* Write updated trx header to the flash */
322 memcpy(block, &trx, sizeof(trx));
323 if (mtd->unlock)
324 mtd->unlock(mtd, off, mtd->erasesize);
325 erase_write(mtd, off, mtd->erasesize, block);
326 if (mtd->sync)
327 mtd->sync(mtd);
328 kfree(block);
329 printk("Done\n");
330 }
331
332 return part->size;
333 }
334
335 struct mtd_partition * __init
336 init_mtd_partitions(struct mtd_info *mtd, size_t size)
337 {
338 int cfe_size;
339
340 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
341 return NULL;
342
343 /* boot loader */
344 bcm947xx_parts[0].offset = 0;
345 bcm947xx_parts[0].size = cfe_size;
346
347 /* nvram */
348 if (cfe_size != 384 * 1024) {
349 bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
350 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
351 } else {
352 /* nvram (old 128kb config partition on netgear wgt634u) */
353 bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
354 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
355 }
356
357 /* linux (kernel and rootfs) */
358 if (cfe_size != 384 * 1024) {
359 bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
360 bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
361 bcm947xx_parts[1].offset;
362 } else {
363 /* do not count the elf loader, which is on one block */
364 bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
365 bcm947xx_parts[3].size + mtd->erasesize;
366 bcm947xx_parts[1].size = size -
367 bcm947xx_parts[0].size -
368 (2*bcm947xx_parts[3].size) -
369 mtd->erasesize;
370 }
371
372 /* find and size rootfs */
373 if (find_root(mtd,size,&bcm947xx_parts[2])==0) {
374 /* entirely jffs2 */
375 bcm947xx_parts[4].name = NULL;
376 bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset -
377 bcm947xx_parts[3].size;
378 } else {
379 /* legacy setup */
380 /* calculate leftover flash, and assign it to the jffs2 partition */
381 if (cfe_size != 384 * 1024) {
382 bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
383 bcm947xx_parts[2].size;
384 if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
385 bcm947xx_parts[4].offset += mtd->erasesize -
386 (bcm947xx_parts[4].offset % mtd->erasesize);
387 }
388 bcm947xx_parts[4].size = bcm947xx_parts[3].offset -
389 bcm947xx_parts[4].offset;
390 } else {
391 bcm947xx_parts[4].offset = bcm947xx_parts[2].offset +
392 bcm947xx_parts[2].size;
393 if ((bcm947xx_parts[4].offset % mtd->erasesize) > 0) {
394 bcm947xx_parts[4].offset += mtd->erasesize -
395 (bcm947xx_parts[4].offset % mtd->erasesize);
396 }
397 bcm947xx_parts[4].size = size - bcm947xx_parts[3].size -
398 bcm947xx_parts[4].offset;
399 }
400 }
401
402 return bcm947xx_parts;
403 }
404 #endif
405
406 int __init init_bcm947xx_map(void)
407 {
408 struct ssb_mipscore *mcore = &ssb.mipscore;
409 size_t size;
410 int ret = 0;
411 #ifdef CONFIG_MTD_PARTITIONS
412 struct mtd_partition *parts;
413 int i;
414 #endif
415 u32 window = mcore->flash_window;
416 u32 window_size = mcore->flash_window_size;
417
418 printk("flash init: 0x%08x 0x%08x\n", window, window_size);
419 bcm947xx_map.phys = window;
420 bcm947xx_map.size = window_size;
421 bcm947xx_map.virt = ioremap_nocache(window, window_size);
422
423 if (!bcm947xx_map.virt) {
424 printk("Failed to ioremap\n");
425 return -EIO;
426 }
427 simple_map_init(&bcm947xx_map);
428
429 bcm947xx_map.copy_from = bcm947xx_map_copy_from;
430
431 if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
432 printk("Failed to do_map_probe\n");
433 iounmap((void *)bcm947xx_map.virt);
434 return -ENXIO;
435 }
436
437 bcm947xx_mtd->owner = THIS_MODULE;
438
439 size = bcm947xx_mtd->size;
440
441 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
442
443 #ifdef CONFIG_MTD_PARTITIONS
444 parts = init_mtd_partitions(bcm947xx_mtd, size);
445 for (i = 0; parts[i].name; i++);
446 ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
447 if (ret) {
448 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
449 goto fail;
450 }
451 #endif
452 return 0;
453
454 fail:
455 if (bcm947xx_mtd)
456 map_destroy(bcm947xx_mtd);
457 if (bcm947xx_map.virt)
458 iounmap((void *)bcm947xx_map.virt);
459 bcm947xx_map.virt = 0;
460 return ret;
461 }
462
463 void __exit cleanup_bcm947xx_map(void)
464 {
465 #ifdef CONFIG_MTD_PARTITIONS
466 del_mtd_partitions(bcm947xx_mtd);
467 #endif
468 map_destroy(bcm947xx_mtd);
469 iounmap((void *)bcm947xx_map.virt);
470 }
471
472 module_init(init_bcm947xx_map);
473 module_exit(cleanup_bcm947xx_map);