ar7: clocks setup (prerequisite for dsl/usb) and misc cleanups.
[openwrt/staging/florian.git] / target / linux / ar7-2.6 / files / drivers / mtd / ar7part.c
1 /*
2 * $Id$
3 *
4 * Copyright (C) 2007 OpenWrt.org
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * TI AR7 flash partition table.
21 * Based on ar7 map by Felix Fietkau.
22 *
23 */
24
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
27
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/partitions.h>
30 #include <linux/bootmem.h>
31 #include <linux/squashfs_fs.h>
32 #include <linux/root_dev.h>
33
34 struct ar7_bin_rec {
35 unsigned int checksum;
36 unsigned int length;
37 unsigned int address;
38 };
39
40 static struct mtd_partition ar7_parts[5];
41
42 static int create_mtd_partitions(struct mtd_info *master,
43 struct mtd_partition **pparts,
44 unsigned long origin)
45 {
46 char sig[8];
47 struct ar7_bin_rec header;
48 unsigned int offset, new_offset, len;
49 struct squashfs_super_block sb;
50 unsigned int block_size = 0x10000, pre_size = 0x10000,
51 post_size = 0, root_offset = 0xe0000;
52 unsigned int p = 3;
53
54 printk("Parsing AR7 partition map...\n");
55
56 ar7_parts[0].name = "loader";
57 ar7_parts[0].offset = 0;
58 ar7_parts[0].size = block_size;
59 ar7_parts[0].mask_flags = MTD_WRITEABLE;
60
61 ar7_parts[1].name = "config";
62 ar7_parts[1].size = block_size;
63 ar7_parts[1].mask_flags = 0;
64
65 master->read(master, block_size, 8, &len, sig);
66 if (strncmp(sig, "TIENV0.8", 8)) {
67 ar7_parts[1].offset = master->size - block_size;
68 post_size = block_size;
69 } else {
70 ar7_parts[1].offset = block_size;
71 pre_size = block_size * 2;
72 }
73
74 ar7_parts[2].name = "linux";
75 ar7_parts[2].offset = pre_size;
76 ar7_parts[2].size = master->size - block_size * 2;
77 ar7_parts[2].mask_flags = 0;
78
79 offset = pre_size;
80 master->read(master, offset, sizeof(header), &len, (u_char *)&header);
81 if (header.checksum != 0xfeedfa42) {
82 printk("Unknown magic: %08x\n", header.checksum);
83 } else {
84 while (header.length) {
85 offset += sizeof(header) + header.length;
86 master->read(master, offset, sizeof(header),
87 &len, (u_char *)&header);
88 }
89 root_offset = offset + sizeof(header) + 4;
90 }
91
92 ar7_parts[p].name = "rootfs";
93 ar7_parts[p].offset = root_offset;
94 ar7_parts[p].size = master->size - root_offset - post_size;
95 ar7_parts[p++].mask_flags = 0;
96
97 master->read(master, root_offset, sizeof(sb), &len, (u_char *)&sb);
98 if (sb.s_magic == SQUASHFS_MAGIC) {
99 printk("Squashfs detected (size %Ld)\n", sb.bytes_used);
100 new_offset = root_offset + sb.bytes_used;
101
102 if ((new_offset % master->erasesize) > 0)
103 new_offset += master->erasesize -
104 (new_offset % master->erasesize);
105
106 ar7_parts[p].name = "rootfs_data";
107 ar7_parts[p].offset = new_offset;
108 ar7_parts[p].size = master->size - new_offset - post_size;
109 ar7_parts[p - 1].size -= ar7_parts[p].size;
110 ar7_parts[p - 1].mask_flags |= MTD_WRITEABLE;
111 ar7_parts[p++].mask_flags = 0;
112 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, p - 1);
113 } else {
114 printk("Squashfs not found. Moving rootfs partition to next erase block\n");
115 if ((root_offset % master->erasesize) > 0)
116 root_offset += master->erasesize -
117 (root_offset % master->erasesize);
118
119 ar7_parts[p].offset = root_offset;
120 ar7_parts[p].size = master->size - root_offset - post_size;
121 ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, p);
122 }
123 *pparts = ar7_parts;
124 return p;
125 }
126
127 static struct mtd_part_parser ar7_parser = {
128 .owner = THIS_MODULE,
129 .parse_fn = create_mtd_partitions,
130 .name = "ar7part",
131 };
132
133 static int __init ar7_parser_init(void)
134 {
135 return register_mtd_parser(&ar7_parser);
136 }
137
138 module_init(ar7_parser_init);
139
140 MODULE_LICENSE("GPL");
141 MODULE_AUTHOR("Felix Fietkau, Eugene Konev");
142 MODULE_DESCRIPTION("MTD partitioning for TI AR7");