brcm47xx-2.6 cleanup, fix the kernel config
[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/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;
80 #endif
81 static struct mtd_info *bcm947xx_mtd;
82
83 static struct map_info bcm947xx_map = {
84 name: "Physically mapped flash",
85 size: WINDOW_SIZE,
86 bankwidth: BUSWIDTH,
87 phys: WINDOW_ADDR,
88 };
89
90 #ifdef CONFIG_MTD_PARTITIONS
91
92 static struct mtd_partition bcm947xx_parts[] = {
93 { name: "cfe", offset: 0, size: 0, mask_flags: MTD_WRITEABLE, },
94 { name: "linux", offset: 0, size: 0, },
95 { name: "rootfs", offset: 0, size: 0, },
96 { name: "nvram", offset: 0, size: 0, },
97 { name: NULL, },
98 };
99
100 static int __init
101 find_cfe_size(struct mtd_info *mtd, size_t size)
102 {
103 struct trx_header *trx;
104 unsigned char buf[512];
105 int off;
106 size_t len;
107 int blocksize;
108
109 trx = (struct trx_header *) buf;
110
111 blocksize = mtd->erasesize;
112 if (blocksize < 0x10000)
113 blocksize = 0x10000;
114
115 for (off = (128*1024); off < size; off += blocksize) {
116 memset(buf, 0xe5, sizeof(buf));
117
118 /*
119 * Read into buffer
120 */
121 if (mtd->read(mtd, off, sizeof(buf), &len, buf) ||
122 len != sizeof(buf))
123 continue;
124
125 /* found a TRX header */
126 if (le32_to_cpu(trx->magic) == TRX_MAGIC) {
127 goto found;
128 }
129 }
130
131 printk(KERN_NOTICE
132 "%s: Couldn't find bootloader size\n",
133 mtd->name);
134 return -1;
135
136 found:
137 printk(KERN_NOTICE "bootloader size: %d\n", off);
138 return off;
139
140 }
141
142 /*
143 * Copied from mtdblock.c
144 *
145 * Cache stuff...
146 *
147 * Since typical flash erasable sectors are much larger than what Linux's
148 * buffer cache can handle, we must implement read-modify-write on flash
149 * sectors for each block write requests. To avoid over-erasing flash sectors
150 * and to speed things up, we locally cache a whole flash sector while it is
151 * being written to until a different sector is required.
152 */
153
154 static void erase_callback(struct erase_info *done)
155 {
156 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
157 wake_up(wait_q);
158 }
159
160 static int erase_write (struct mtd_info *mtd, unsigned long pos,
161 int len, const char *buf)
162 {
163 struct erase_info erase;
164 DECLARE_WAITQUEUE(wait, current);
165 wait_queue_head_t wait_q;
166 size_t retlen;
167 int ret;
168
169 /*
170 * First, let's erase the flash block.
171 */
172
173 init_waitqueue_head(&wait_q);
174 erase.mtd = mtd;
175 erase.callback = erase_callback;
176 erase.addr = pos;
177 erase.len = len;
178 erase.priv = (u_long)&wait_q;
179
180 set_current_state(TASK_INTERRUPTIBLE);
181 add_wait_queue(&wait_q, &wait);
182
183 ret = mtd->erase(mtd, &erase);
184 if (ret) {
185 set_current_state(TASK_RUNNING);
186 remove_wait_queue(&wait_q, &wait);
187 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
188 "on \"%s\" failed\n",
189 pos, len, mtd->name);
190 return ret;
191 }
192
193 schedule(); /* Wait for erase to finish. */
194 remove_wait_queue(&wait_q, &wait);
195
196 /*
197 * Next, writhe data to flash.
198 */
199
200 ret = mtd->write (mtd, pos, len, &retlen, buf);
201 if (ret)
202 return ret;
203 if (retlen != len)
204 return -EIO;
205 return 0;
206 }
207
208
209
210
211 static int __init
212 find_root(struct mtd_info *mtd, size_t size, struct mtd_partition *part)
213 {
214 struct trx_header trx, *trx2;
215 unsigned char buf[512], *block;
216 int off, blocksize;
217 u32 i, crc = ~0;
218 size_t len;
219 struct squashfs_super_block *sb = (struct squashfs_super_block *) buf;
220
221 blocksize = mtd->erasesize;
222 if (blocksize < 0x10000)
223 blocksize = 0x10000;
224
225 for (off = (128*1024); off < size; off += blocksize) {
226 memset(&trx, 0xe5, sizeof(trx));
227
228 /*
229 * Read into buffer
230 */
231 if (mtd->read(mtd, off, sizeof(trx), &len, (char *) &trx) ||
232 len != sizeof(trx))
233 continue;
234
235 /* found a TRX header */
236 if (le32_to_cpu(trx.magic) == TRX_MAGIC) {
237 part->offset = le32_to_cpu(trx.offsets[2]) ? :
238 le32_to_cpu(trx.offsets[1]);
239 part->size = le32_to_cpu(trx.len);
240
241 part->size -= part->offset;
242 part->offset += off;
243
244 goto found;
245 }
246 }
247
248 printk(KERN_NOTICE
249 "%s: Couldn't find root filesystem\n",
250 mtd->name);
251 return -1;
252
253 found:
254 if (part->size == 0)
255 return 0;
256
257 if (mtd->read(mtd, part->offset, sizeof(buf), &len, buf) || len != sizeof(buf))
258 return 0;
259
260 /* Move the fs outside of the trx */
261 part->size = 0;
262
263 if (trx.len != part->offset + part->size - off) {
264 /* Update the trx offsets and length */
265 trx.len = part->offset + part->size - off;
266
267 /* Update the trx crc32 */
268 for (i = (u32) &(((struct trx_header *)NULL)->flag_version); i <= trx.len; i += sizeof(buf)) {
269 if (mtd->read(mtd, off + i, sizeof(buf), &len, buf) || len != sizeof(buf))
270 return 0;
271 crc = crc32_le(crc, buf, min(sizeof(buf), trx.len - i));
272 }
273 trx.crc32 = crc;
274
275 /* read first eraseblock from the trx */
276 block = kmalloc(mtd->erasesize, GFP_KERNEL);
277 trx2 = (struct trx_header *) block;
278 if (mtd->read(mtd, off, mtd->erasesize, &len, block) || len != mtd->erasesize) {
279 printk("Error accessing the first trx eraseblock\n");
280 return 0;
281 }
282
283 printk("Updating TRX offsets and length:\n");
284 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);
285 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);
286
287 /* Write updated trx header to the flash */
288 memcpy(block, &trx, sizeof(trx));
289 if (mtd->unlock)
290 mtd->unlock(mtd, off, mtd->erasesize);
291 erase_write(mtd, off, mtd->erasesize, block);
292 if (mtd->sync)
293 mtd->sync(mtd);
294 kfree(block);
295 printk("Done\n");
296 }
297
298 return part->size;
299 }
300
301 struct mtd_partition * __init
302 init_mtd_partitions(struct mtd_info *mtd, size_t size)
303 {
304 int cfe_size;
305
306 if ((cfe_size = find_cfe_size(mtd,size)) < 0)
307 return NULL;
308
309 /* boot loader */
310 bcm947xx_parts[0].offset = 0;
311 bcm947xx_parts[0].size = cfe_size;
312
313 /* nvram */
314 if (cfe_size != 384 * 1024) {
315 bcm947xx_parts[3].offset = size - ROUNDUP(NVRAM_SPACE, mtd->erasesize);
316 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
317 } else {
318 /* nvram (old 128kb config partition on netgear wgt634u) */
319 bcm947xx_parts[3].offset = bcm947xx_parts[0].size;
320 bcm947xx_parts[3].size = ROUNDUP(NVRAM_SPACE, mtd->erasesize);
321 }
322
323 /* linux (kernel and rootfs) */
324 if (cfe_size != 384 * 1024) {
325 bcm947xx_parts[1].offset = bcm947xx_parts[0].size;
326 bcm947xx_parts[1].size = bcm947xx_parts[3].offset -
327 bcm947xx_parts[1].offset;
328 } else {
329 /* do not count the elf loader, which is on one block */
330 bcm947xx_parts[1].offset = bcm947xx_parts[0].size +
331 bcm947xx_parts[3].size + mtd->erasesize;
332 bcm947xx_parts[1].size = size -
333 bcm947xx_parts[0].size -
334 (2*bcm947xx_parts[3].size) -
335 mtd->erasesize;
336 }
337
338 /* find and size rootfs */
339 find_root(mtd,size,&bcm947xx_parts[2]);
340 bcm947xx_parts[2].size = size - bcm947xx_parts[2].offset - bcm947xx_parts[3].size;
341
342 return bcm947xx_parts;
343 }
344 #endif
345
346 int __init init_bcm947xx_map(void)
347 {
348 #ifdef CONFIG_SSB
349 struct ssb_mipscore *mcore = &ssb.mipscore;
350 #endif
351 size_t size;
352 int ret = 0;
353 #ifdef CONFIG_MTD_PARTITIONS
354 struct mtd_partition *parts;
355 int i;
356 #endif
357
358 #ifdef CONFIG_SSB
359 u32 window = mcore->flash_window;
360 u32 window_size = mcore->flash_window_size;
361
362 printk("flash init: 0x%08x 0x%08x\n", window, window_size);
363 bcm947xx_map.phys = window;
364 bcm947xx_map.size = window_size;
365 bcm947xx_map.virt = ioremap_nocache(window, window_size);
366 #else
367 printk("flash init: 0x%08x 0x%08x\n", WINDOW_ADDR, WINDOW_SIZE);
368 bcm947xx_map.virt = ioremap_nocache(WINDOW_ADDR, WINDOW_SIZE);
369 #endif
370
371 if (!bcm947xx_map.virt) {
372 printk("Failed to ioremap\n");
373 return -EIO;
374 }
375 simple_map_init(&bcm947xx_map);
376
377 if (!(bcm947xx_mtd = do_map_probe("cfi_probe", &bcm947xx_map))) {
378 printk("Failed to do_map_probe\n");
379 iounmap((void *)bcm947xx_map.virt);
380 return -ENXIO;
381 }
382
383 bcm947xx_mtd->owner = THIS_MODULE;
384
385 size = bcm947xx_mtd->size;
386
387 printk(KERN_NOTICE "Flash device: 0x%x at 0x%x\n", size, WINDOW_ADDR);
388
389 #ifdef CONFIG_MTD_PARTITIONS
390 parts = init_mtd_partitions(bcm947xx_mtd, size);
391 for (i = 0; parts[i].name; i++);
392 ret = add_mtd_partitions(bcm947xx_mtd, parts, i);
393 if (ret) {
394 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
395 goto fail;
396 }
397 #endif
398 return 0;
399
400 fail:
401 if (bcm947xx_mtd)
402 map_destroy(bcm947xx_mtd);
403 if (bcm947xx_map.virt)
404 iounmap((void *)bcm947xx_map.virt);
405 bcm947xx_map.virt = 0;
406 return ret;
407 }
408
409 void __exit cleanup_bcm947xx_map(void)
410 {
411 #ifdef CONFIG_MTD_PARTITIONS
412 del_mtd_partitions(bcm947xx_mtd);
413 #endif
414 map_destroy(bcm947xx_mtd);
415 iounmap((void *)bcm947xx_map.virt);
416 }
417
418 module_init(init_bcm947xx_map);
419 module_exit(cleanup_bcm947xx_map);
This page took 0.079904 seconds and 5 git commands to generate.