brcm47xx: prepare brcm47xx patches for sending to mainline.
[openwrt/svn-archive/archive.git] / target / linux / brcm47xx / files-2.6.35 / 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/sched.h>
44 #include <linux/wait.h>
45 #include <linux/mtd/mtd.h>
46 #include <linux/mtd/map.h>
47 #ifdef CONFIG_MTD_PARTITIONS
48 #include <linux/mtd/partitions.h>
49 #endif
50 #include <linux/crc32.h>
51 #ifdef CONFIG_SSB
52 #include <linux/ssb/ssb.h>
53 #endif
54 #include <asm/io.h>
55
56
57 #define TRX_MAGIC 0x30524448 /* "HDR0" */
58 #define TRX_VERSION 1
59 #define TRX_MAX_LEN 0x3A0000
60 #define TRX_NO_HEADER 1 /* Do not write TRX header */
61 #define TRX_GZ_FILES 0x2 /* Contains up to TRX_MAX_OFFSET individual gzip files */
62 #define TRX_MAX_OFFSET 3
63
64 struct trx_header {
65 u32 magic; /* "HDR0" */
66 u32 len; /* Length of file including header */
67 u32 crc32; /* 32-bit CRC from flag_version to end of file */
68 u32 flag_version; /* 0:15 flags, 16:31 version */
69 u32 offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
70 };
71
72 #define ROUNDUP(x, y) ((((x)+((y)-1))/(y))*(y))
73 #define NVRAM_SPACE 0x8000
74 #define WINDOW_ADDR 0x1fc00000
75 #define WINDOW_SIZE 0x400000
76 #define BUSWIDTH 2
77
78 #ifdef CONFIG_SSB
79 extern struct ssb_bus ssb_bcm47xx;
80 #endif
81 static struct mtd_info *bcm47xx_mtd;
82
83 static void bcm47xx_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
84 {
85 if (len==1) {
86 memcpy_fromio(to, map->virt + from, len);
87 } else {
88 int i;
89 u16 *dest = (u16 *) to;
90 u16 *src = (u16 *) (map->virt + from);
91 for (i = 0; i < (len / 2); i++) {
92 dest[i] = src[i];
93 }
94 if (len & 1)
95 *((u8 *)dest+len-1) = src[i] & 0xff;
96 }
97 }
98
99 static struct map_info bcm47xx_map = {
100 name: "Physically mapped flash",
101 size: WINDOW_SIZE,
102 bankwidth: BUSWIDTH,
103 phys: WINDOW_ADDR,
104 };
105
106 #ifdef CONFIG_MTD_PARTITIONS
107
108 static struct mtd_partition bcm47xx_parts[] = {
109 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
110 { name: "linux", offset: 0, size: 0, },
111 { name: "rootfs", offset: 0, size: 0, },
112 { name: "nvram", offset: 0, size: 0, },
113 { name: NULL, },
114 };
115
116 static int __init
117 find_cfe_size(struct mtd_info *mtd, size_t size)
118 {
119 struct trx_header *trx;
120 unsigned char buf[512];
121 int off;
122 size_t len;
123 int blocksize;
124
125 trx = (struct trx_header *) buf;
126
127 blocksize = mtd->erasesize;
128 if (blocksize < 0x10000)
129 blocksize = 0x10000;
130
131 for (off = (128*1024); off < size; off += blocksize) {
132 memset(buf, 0xe5, sizeof(buf));
133
134 /*
135 * Read into buffer
136 */
137 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
138 len != sizeof(buf))
139 continue;
140
141 /* found a TRX header */
142 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
143 goto found;
144 }
145 }
146
147 printk(KERN_NOTICE
148 "%s: Couldn't find bootloader size\n",
149 mtd->name);
150 return -1;
151
152 found:
153 printk(KERN_NOTICE "bootloader size: %d\n", off);
154 return off;
155
156 }
157
158 /*
159 * Copied from mtdblock.c
160 *
161 * Cache stuff...
162 *
163 * Since typical flash erasable sectors are much larger than what Linux's
164 * buffer cache can handle, we must implement read-modify-write on flash
165 * sectors for each block write requests. To avoid over-erasing flash sectors
166 * and to speed things up, we locally cache a whole flash sector while it is
167 * being written to until a different sector is required.
168 */
169
170 static void erase_callback(struct erase_info *done)
171 {
172 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
173 wake_up(wait_q);
174 }
175
176 static int erase_write (struct mtd_info *mtd, unsigned long pos,
177 int len, const char *buf)
178 {
179 struct erase_info erase;
180 DECLARE_WAITQUEUE(wait, current);
181 wait_queue_head_t wait_q;
182 size_t retlen;
183 int ret;
184
185 /*
186 * First, let's erase the flash block.
187 */
188
189 init_waitqueue_head(&wait_q);
190 erase.mtd = mtd;
191 erase.callback = erase_callback;
192 erase.addr = pos;
193 erase.len = len;
194 erase.priv = (u_long)&wait_q;
195
196 set_current_state(TASK_INTERRUPTIBLE);
197 add_wait_queue(&wait_q, &wait);
198
199 ret = mtd->erase(mtd, &erase);
200 if (ret) {
201 set_current_state(TASK_RUNNING);
202 remove_wait_queue(&wait_q, &wait);
203 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
204 "on \"%s\" failed\n",
205 pos, len, mtd->name);
206 return ret;
207 }
208
209 schedule(); /* Wait for erase to finish. */
210 remove_wait_queue(&wait_q, &wait);
211
212 /*
213 * Next, writhe data to flash.
214 */
215
216 ret = mtd->write (mtd, pos, len, &retlen, buf);
217 if (ret)
218 return ret;
219 if (retlen != len)
220 return -EIO;
221 return 0;
222 }
223
224
225 static int __init
226 find_dual_image_off (struct mtd_info *mtd, size_t size)
227 {
228 struct trx_header trx;
229 int off, blocksize;
230 size_t len;
231
232 blocksize = mtd->erasesize;
233 if (blocksize < 0x10000)
234 blocksize = 0x10000;
235
236 for (off = (128*1024); off < size; off += blocksize) {
237 memset(&trx, 0xe5, sizeof(trx));
238 /*
239 * Read into buffer
240 */
241 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
242 len != sizeof(trx))
243 continue;
244 /* found last TRX header */
245 if (le32_to_cpu(trx.magic) == TRX_MAGIC){
246 if (le32_to_cpu(trx.flag_version >> 16)==2){
247 printk("dual image TRX header found\n");
248 return size/2;
249 } else {
250 return 0;
251 }
252 }
253 }
254 return 0;
255 }
256
257
258 static int __init
259 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
260 {
261 struct trx_header trx, *trx2;
262 unsigned char buf[512], *block;
263 int off, blocksize;
264 u32 i, crc = ~0;
265 size_t len;
266
267 blocksize = mtd->erasesize;
268 if (blocksize < 0x10000)
269 blocksize = 0x10000;
270
271 for (off = (128*1024); off < size; off += blocksize) {
272 memset(&trx, 0xe5, sizeof(trx));
273
274 /*
275 * Read into buffer
276 */
277 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
278 len != sizeof(trx))
279 continue;
280
281 /* found a TRX header */
282 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
283 part->offset = le32_to_cpu(trx.offsets[2]) ? :
284 le32_to_cpu(trx.offsets[1]);
285 part->size = le32_to_cpu(trx.len);
286
287 part->size -= part->offset;
288 part->offset += off;
289
290 goto found;
291 }
292 }
293
294 printk(KERN_NOTICE
295 "%s: Couldn't find root filesystem\n",
296 mtd->name);
297 return -1;
298
299 found:
300 if (part->size == 0)
301 return 0;
302
303 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
304 return 0;
305
306 /* Move the fs outside of the trx */
307 part->size = 0;
308
309 if (trx.len != part->offset + part->size - off) {
310 /* Update the trx offsets and length */
311 trx.len = part->offset + part->size - off;
312
313 /* Update the trx crc32 */
314 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
315 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
316 return 0;
317 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
318 }
319 trx.crc32 = crc;
320
321 /* read first eraseblock from the trx */
322 block = kmalloc(mtd->erasesize, GFP_KERNEL);
323 trx2 = (struct trx_header *) block;
324 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
325 printk("Error accessing the first trx eraseblock\n");
326 return 0;
327 }
328
329 printk("Updating TRX offsets and length:\n");
330 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);
331 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);
332
333 /* Write updated trx header to the flash */
334 memcpy(block, &trx, sizeof(trx));
335 if (mtd->unlock)
336 mtd->unlock(mtd, off, mtd->erasesize);
337 erase_write(mtd, off, mtd->erasesize, block);
338 if (mtd->sync)
339 mtd->sync(mtd);
340 kfree(block);
341 printk("Done\n");
342 }
343
344 return part->size;
345 }
346
347 struct mtd_partition * __init
348 init_mtd_partitions(struct mtd_info *mtd, size_t size)
349 {
350 int cfe_size;
351 int dual_image_offset = 0;
352
353 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
354 return NULL;
355
356 /* boot loader */
357 bcm47xx_parts[0].offset = 0;
358 bcm47xx_parts[0].size = cfe_size;
359
360 /* nvram */
361 if (cfe_size != 384 * 1024) {
362 bcm47xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
363 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
364 } else {
365 /* nvram (old 128kb config partition on netgear wgt634u) */
366 bcm47xx_parts[3].offset = bcm47xx_parts[0].size;
367 bcm47xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
368 }
369
370 /* dual image offset*/
371 printk("Looking for dual image\n");
372 dual_image_offset=find_dual_image_off(mtd,size);
373 /* linux (kernel and rootfs) */
374 if (cfe_size != 384 * 1024) {
375 bcm47xx_parts[1].offset = bcm47xx_parts[0].size;
376 bcm47xx_parts[1].size = bcm47xx_parts[3].offset - dual_image_offset -
377 bcm47xx_parts[1].offset;
378 } else {
379 /* do not count the elf loader, which is on one block */
380 bcm47xx_parts[1].offset = bcm47xx_parts[0].size +
381 bcm47xx_parts[3].size + mtd->erasesize;
382 bcm47xx_parts[1].size = size -
383 bcm47xx_parts[0].size -
384 (2*bcm47xx_parts[3].size) -
385 mtd->erasesize;
386 }
387
388 /* find and size rootfs */
389 find_root(mtd,size,&bcm47xx_parts[2]);
390 bcm47xx_parts[2].size = size - dual_image_offset - bcm47xx_parts[2].offset - bcm47xx_parts[3].size;
391
392 return bcm47xx_parts;
393 }
394 #endif
395
396 int __init init_bcm47xx_map(void)
397 {
398 #ifdef CONFIG_SSB
399 struct ssb_mipscore *mcore = &ssb_bcm47xx.mipscore;
400 #endif
401 size_t size;
402 int ret = 0;
403 #ifdef CONFIG_MTD_PARTITIONS
404 struct mtd_partition *parts;
405 int i;
406 #endif
407
408 #ifdef CONFIG_SSB
409 u32 window = mcore->flash_window;
410 u32 window_size = mcore->flash_window_size;
411
412 printk("flash init: 0x%08x 0x%08x\n", window, window_size);
413 bcm47xx_map.phys = window;
414 bcm47xx_map.size = window_size;
415 bcm47xx_map.bankwidth = mcore->flash_buswidth;
416 bcm47xx_map.virt = ioremap_nocache(window, window_size);
417 #else
418 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
419 bcm47xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
420 #endif
421
422 if (!bcm47xx_map.virt) {
423 printk("Failed to ioremap\n");
424 return -EIO;
425 }
426
427 simple_map_init(&bcm47xx_map);
428
429 if (!(bcm47xx_mtd = do_map_probe("cfi_probe", &bcm47xx_map))) {
430 printk("Failed to do_map_probe\n");
431 iounmap((void *)bcm47xx_map.virt);
432 return -ENXIO;
433 }
434
435 /* override copy_from routine */
436 bcm47xx_map.copy_from = bcm47xx_map_copy_from;
437
438 bcm47xx_mtd->owner = THIS_MODULE;
439
440 size = bcm47xx_mtd->size;
441
442 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
443
444 #ifdef CONFIG_MTD_PARTITIONS
445 parts = init_mtd_partitions(bcm47xx_mtd, size);
446 for (i = 0; parts[i].name; i++);
447 ret = add_mtd_partitions(bcm47xx_mtd, parts, i);
448 if (ret) {
449 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
450 goto fail;
451 }
452 #endif
453 return 0;
454
455 fail:
456 if (bcm47xx_mtd)
457 map_destroy(bcm47xx_mtd);
458 if (bcm47xx_map.virt)
459 iounmap((void *)bcm47xx_map.virt);
460 bcm47xx_map.virt = 0;
461 return ret;
462 }
463
464 void __exit cleanup_bcm47xx_map(void)
465 {
466 #ifdef CONFIG_MTD_PARTITIONS
467 del_mtd_partitions(bcm47xx_mtd);
468 #endif
469 map_destroy(bcm47xx_mtd);
470 iounmap((void *)bcm47xx_map.virt);
471 }
472
473 module_init(init_bcm47xx_map);
474 module_exit(cleanup_bcm47xx_map);