[package] libpcap: explicitely disable libnl support to prevent different build resul...
[openwrt.git] / target / linux / adm8668 / files / drivers / mtd / maps / adm8668.c
1 /*
2 * Copyright (C) 2010 Scott Nicholas <neutronscott@scottn.us>
3 * Copyright (C) 2006 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2005 Waldemar Brodkorb <wbx@openwrt.org>
5 * Copyright (C) 2004 Florian Schirmer (jolt@tuxbox.org)
6 *
7 * original functions for finding root filesystem from Mike Baker
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2 of the License, or (at your
12 * option) any later version.
13 *
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
17 * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
20 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
21 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 * You should have received a copy of the GNU General Public License along
26 * with this program; if not, write to the Free Software Foundation, Inc.,
27 * 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 *
30 * Copyright 2004, Broadcom Corporation
31 * All Rights Reserved.
32 *
33 * THIS SOFTWARE IS OFFERED "AS IS", AND BROADCOM GRANTS NO WARRANTIES OF ANY
34 * KIND, EXPRESS OR IMPLIED, BY STATUTE, COMMUNICATION OR OTHERWISE. BROADCOM
35 * SPECIFICALLY DISCLAIMS ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
36 * FOR A SPECIFIC PURPOSE OR NONINFRINGEMENT CONCERNING THIS SOFTWARE.
37 *
38 * Flash mapping for adm8668 boards
39 *
40 */
41
42 #include <linux/module.h>
43 #include <linux/types.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
46 #include <linux/wait.h>
47 #include <linux/mtd/mtd.h>
48 #include <linux/mtd/map.h>
49 #include <linux/slab.h>
50 #ifdef CONFIG_MTD_PARTITIONS
51 #include <linux/mtd/partitions.h>
52 #endif
53 #include <linux/crc32.h>
54 #include <linux/magic.h>
55 #include <asm/io.h>
56
57 #define WINDOW_ADDR 0x10000000
58 #define WINDOW_SIZE 0x800000
59 #define BANKWIDTH 2
60
61 /* first a little bit about the headers i need.. */
62
63 /* just interested in part of the full struct */
64 struct squashfs_super_block {
65 __le32 s_magic;
66 __le32 pad0[9]; /* it's not really padding */
67 __le64 bytes_used;
68 };
69
70 #define IH_MAGIC 0x56190527 /* Image Magic Number */
71 struct uboot_header {
72 uint32_t ih_magic; /* Image Header Magic Number */
73 uint32_t ih_hcrc; /* Image Header CRC Checksum */
74 uint32_t ih_time; /* Image Creation Timestamp */
75 uint32_t ih_size; /* Image Data Size */
76 uint32_t ih_load; /* Data Load Address */
77 uint32_t ih_ep; /* Entry Point Address */
78 uint32_t ih_dcrc; /* Image Data CRC Checksum */
79 uint8_t ih_os; /* Operating System */
80 uint8_t ih_arch; /* CPU architecture */
81 uint8_t ih_type; /* Image Type */
82 uint8_t ih_comp; /* Compression Type */
83 char ih_name[32]; /* image name */
84 };
85
86 /************************************************/
87
88 static struct mtd_info *adm8668_mtd;
89
90 struct map_info adm8668_map = {
91 name: "adm8668-nor",
92 size: WINDOW_SIZE,
93 phys: WINDOW_ADDR,
94 bankwidth: BANKWIDTH,
95 };
96
97 #ifdef CONFIG_MTD_PARTITIONS
98
99 /*
100 * Copied from mtdblock.c
101 *
102 * Cache stuff...
103 *
104 * Since typical flash erasable sectors are much larger than what Linux's
105 * buffer cache can handle, we must implement read-modify-write on flash
106 * sectors for each block write requests. To avoid over-erasing flash sectors
107 * and to speed things up, we locally cache a whole flash sector while it is
108 * being written to until a different sector is required.
109 */
110
111 static void erase_callback(struct erase_info *done)
112 {
113 wait_queue_head_t *wait_q = (wait_queue_head_t *)done->priv;
114 wake_up(wait_q);
115 }
116
117 static int erase_write (struct mtd_info *mtd, unsigned long pos,
118 int len, const char *buf)
119 {
120 struct erase_info erase;
121 DECLARE_WAITQUEUE(wait, current);
122 wait_queue_head_t wait_q;
123 size_t retlen;
124 int ret;
125
126 /*
127 * First, let's erase the flash block.
128 */
129
130 init_waitqueue_head(&wait_q);
131 erase.mtd = mtd;
132 erase.callback = erase_callback;
133 erase.addr = pos;
134 erase.len = len;
135 erase.priv = (u_long)&wait_q;
136
137 set_current_state(TASK_INTERRUPTIBLE);
138 add_wait_queue(&wait_q, &wait);
139
140 ret = mtd->erase(mtd, &erase);
141 if (ret) {
142 set_current_state(TASK_RUNNING);
143 remove_wait_queue(&wait_q, &wait);
144 printk (KERN_WARNING "erase of region [0x%lx, 0x%x] "
145 "on \"%s\" failed\n",
146 pos, len, mtd->name);
147 return ret;
148 }
149
150 schedule(); /* Wait for erase to finish. */
151 remove_wait_queue(&wait_q, &wait);
152
153 /*
154 * Next, write data to flash.
155 */
156
157 ret = mtd->write (mtd, pos, len, &retlen, buf);
158 if (ret)
159 return ret;
160 if (retlen != len)
161 return -EIO;
162 return 0;
163 }
164
165 /* decent defaults in case... shrug */
166 static struct mtd_partition adm8668_parts[] = {
167 { name: "linux", offset: 0x40000, size: WINDOW_SIZE-0x40000, },
168 { name: "rootfs", offset: 0xe0000, size: 0x140000, },
169 { name: "uboot_env", offset: 0x20000, size: 0x20000, },
170 { name: NULL, },
171 };
172
173 /* in case i wanna change stuff later, and to clarify the math section... */
174 #define PART_LINUX 0
175 #define PART_ROOTFS 1
176 #define NR_PARTS 3
177
178 static int __init
179 init_mtd_partitions(struct mtd_info *mtd, size_t size)
180 {
181 struct uboot_header uhdr;
182 int off, blocksize;
183 size_t len, linux_len;
184 struct squashfs_super_block shdr;
185
186 blocksize = mtd->erasesize;
187 if (blocksize < 0x10000)
188 blocksize = 0x10000;
189
190 /* now find squashfs */
191 memset(&shdr, 0xe5, sizeof(shdr));
192 for (off = adm8668_parts[PART_LINUX].offset; off < size; off += blocksize) {
193 /*
194 * Read into buffer
195 */
196 if (mtd->read(mtd, off, sizeof(shdr), &len, (char *)&shdr) ||
197 len != sizeof(shdr))
198 continue;
199
200 if (shdr.s_magic == SQUASHFS_MAGIC) {
201 uint32_t fs_size = (uint32_t)shdr.bytes_used;
202
203 printk(KERN_INFO "%s: Filesystem type: squashfs, size=%dkB\n",
204 mtd->name, fs_size>>10);
205
206 /* Update rootfs based on the superblock info, and
207 * stretch to end of MTD. rootfs_split will split it */
208 adm8668_parts[PART_ROOTFS].offset = off;
209 adm8668_parts[PART_ROOTFS].size = mtd->size -
210 adm8668_parts[PART_ROOTFS].offset;
211
212 /* kernel ends where rootfs starts
213 * but we'll keep it full-length for upgrades */
214 linux_len = adm8668_parts[PART_LINUX+1].offset -
215 adm8668_parts[PART_LINUX].offset;
216 #if 1
217 adm8668_parts[PART_LINUX].size = mtd->size -
218 adm8668_parts[PART_LINUX].offset;
219 #else
220 adm8668_parts[PART_LINUX].size = linux_len;
221 #endif
222 goto found;
223 }
224 }
225
226 printk(KERN_NOTICE
227 "%s: Couldn't find root filesystem\n",
228 mtd->name);
229 return NR_PARTS;
230
231 found:
232 if (mtd->read(mtd, adm8668_parts[PART_LINUX].offset, sizeof(uhdr), &len, (char *)&uhdr) ||
233 len != sizeof(uhdr))
234 return NR_PARTS;
235
236 /* that's odd. how'd ya boot it then */
237 if (uhdr.ih_magic != IH_MAGIC)
238 return NR_PARTS;
239
240 if (be32_to_cpu(uhdr.ih_size) != (linux_len - sizeof(uhdr))) {
241 unsigned char *block, *data = (unsigned char *)(WINDOW_ADDR | (adm8668_parts[PART_LINUX].offset + sizeof(struct uboot_header)) | 0xA0000000);
242
243 printk(KERN_NOTICE "Updating U-boot image:\n");
244 printk(KERN_NOTICE " old: [size: %8d crc32: 0x%08x]\n",
245 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
246
247 /* Update the data length & crc32 */
248 uhdr.ih_size = cpu_to_be32(linux_len - sizeof(uhdr));
249 uhdr.ih_dcrc = crc32_le(~0, data, linux_len - sizeof(uhdr)) ^ (~0);
250 uhdr.ih_dcrc = cpu_to_be32(uhdr.ih_dcrc);
251
252 printk(KERN_NOTICE " new: [size: %8d crc32: 0x%08x]\n",
253 be32_to_cpu(uhdr.ih_size), be32_to_cpu(uhdr.ih_dcrc));
254
255 /* update header's crc... */
256 uhdr.ih_hcrc = 0;
257 uhdr.ih_hcrc = crc32_le(~0, (unsigned char *)&uhdr,
258 sizeof(uhdr)) ^ (~0);
259 uhdr.ih_hcrc = cpu_to_be32(uhdr.ih_hcrc);
260
261 /* read first eraseblock from the image */
262 block = kmalloc(mtd->erasesize, GFP_KERNEL);
263 if (mtd->read(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, &len, block) || len != mtd->erasesize) {
264 printk("Error copying first eraseblock\n");
265 return 0;
266 }
267
268 /* Write updated header to the flash */
269 memcpy(block, &uhdr, sizeof(uhdr));
270 if (mtd->unlock)
271 mtd->unlock(mtd, off, mtd->erasesize);
272 erase_write(mtd, adm8668_parts[PART_LINUX].offset, mtd->erasesize, block);
273 if (mtd->sync)
274 mtd->sync(mtd);
275 kfree(block);
276 printk(KERN_NOTICE "Done\n");
277 }
278
279 return NR_PARTS;
280 }
281
282 #endif
283
284
285 int __init init_adm8668_map(void)
286 {
287 #ifdef CONFIG_MTD_PARTITIONS
288 int nr_parts, ret;
289 #endif
290
291 adm8668_map.virt = (unsigned long)ioremap(WINDOW_ADDR, WINDOW_SIZE);
292
293 if (!adm8668_map.virt) {
294 printk(KERN_ERR "Failed to ioremap\n");
295 return -EIO;
296 }
297
298 simple_map_init(&adm8668_map);
299 if (!(adm8668_mtd = do_map_probe("cfi_probe", &adm8668_map))) {
300 printk(KERN_ERR "cfi_probe failed\n");
301 iounmap((void *)adm8668_map.virt);
302 return -ENXIO;
303 }
304
305 adm8668_mtd->owner = THIS_MODULE;
306
307 #ifdef CONFIG_MTD_PARTITIONS
308 nr_parts = init_mtd_partitions(adm8668_mtd, adm8668_mtd->size);
309 ret = add_mtd_partitions(adm8668_mtd, adm8668_parts, nr_parts);
310 if (ret) {
311 printk(KERN_ERR "Flash: add_mtd_partitions failed\n");
312 goto fail;
313 }
314 #endif
315
316 return 0;
317
318 fail:
319 if (adm8668_mtd)
320 map_destroy(adm8668_mtd);
321 if (adm8668_map.virt)
322 iounmap((void *) adm8668_map.virt);
323 adm8668_map.virt = 0;
324 return ret;
325 }
326
327 void __exit cleanup_adm8668_map(void)
328 {
329 #ifdef CONFIG_MTD_PARTITIONS
330 del_mtd_partitions(adm8668_mtd);
331 #endif
332 map_destroy(adm8668_mtd);
333 iounmap((void *) adm8668_map.virt);
334 adm8668_map.virt = 0;
335 }
336
337 module_init(init_adm8668_map);
338 module_exit(cleanup_adm8668_map);
339
340 MODULE_LICENSE("GPL");
341 MODULE_AUTHOR("Scott Nicholas <neutronscott@scottn.us>");
342 MODULE_DESCRIPTION("MTD map driver for ADM8668 NOR Flash");
This page took 0.074745 seconds and 5 git commands to generate.