1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/module.h>
24#include <linux/types.h>
25#include <linux/kernel.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28
29#include <linux/mtd/mtd.h>
30#include <linux/mtd/map.h>
31#include <linux/mtd/partitions.h>
32
33#include <asm/io.h>
34
35#define FLASH_ADDR 0x40000000
36#define FLASH_SIZE 0x00800000
37#define FLASH_BANK_MAX 4
38
39
40struct mtd_part_def
41{
42 int nums;
43 unsigned char *type;
44 struct mtd_partition* mtd_part;
45};
46
47
48static struct mtd_info* mtd_banks[FLASH_BANK_MAX];
49static struct map_info* map_banks[FLASH_BANK_MAX];
50static struct mtd_part_def part_banks[FLASH_BANK_MAX];
51static unsigned long num_banks;
52static void __iomem *start_scan_addr;
53
54
55
56
57
58
59
60
61
62
63
64
65
66static unsigned long tqm8xxl_max_flash_size = 0x00800000;
67
68
69
70
71static struct mtd_partition tqm8xxl_partitions[] = {
72 {
73 .name = "ppcboot",
74 .offset = 0x00000000,
75 .size = 0x00020000,
76 .mask_flags = MTD_WRITEABLE,
77 },
78 {
79 .name = "kernel",
80 .offset = 0x00020000,
81 .size = 0x000e0000,
82 .mask_flags = MTD_WRITEABLE,
83 },
84 {
85 .name = "user",
86 .offset = 0x00100000,
87 .size = 0x00100000,
88 },
89 {
90 .name = "initrd",
91 .offset = 0x00200000,
92 .size = 0x00200000,
93 }
94};
95
96static struct mtd_partition tqm8xxl_fs_partitions[] = {
97 {
98 .name = "cramfs",
99 .offset = 0x00000000,
100 .size = 0x00200000,
101 },
102 {
103 .name = "jffs",
104 .offset = 0x00200000,
105 .size = 0x00200000,
106
107 }
108};
109
110static int __init init_tqm_mtd(void)
111{
112 int idx = 0, ret = 0;
113 unsigned long flash_addr, flash_size, mtd_size = 0;
114
115 bd_t *bd = (bd_t *)__res;
116
117 flash_addr = bd->bi_flashstart;
118 flash_size = bd->bi_flashsize;
119
120
121 start_scan_addr = ioremap(flash_addr, flash_size);
122 if (!start_scan_addr) {
123 printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __func__, flash_addr);
124 return -EIO;
125 }
126
127 for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
128 if(mtd_size >= flash_size)
129 break;
130
131 printk(KERN_INFO "%s: chip probing count %d\n", __func__, idx);
132
133 map_banks[idx] = kzalloc(sizeof(struct map_info), GFP_KERNEL);
134 if(map_banks[idx] == NULL) {
135 ret = -ENOMEM;
136
137 goto error_mem;
138 }
139
140 map_banks[idx]->name = kmalloc(16, GFP_KERNEL);
141
142 if (!map_banks[idx]->name) {
143 ret = -ENOMEM;
144
145 goto error_mem;
146 }
147 sprintf(map_banks[idx]->name, "TQM8xxL%d", idx);
148
149 map_banks[idx]->size = flash_size;
150 map_banks[idx]->bankwidth = 4;
151
152 simple_map_init(map_banks[idx]);
153
154 map_banks[idx]->virt = start_scan_addr;
155 map_banks[idx]->phys = flash_addr;
156
157
158
159
160
161
162
163
164 if (idx && mtd_banks[idx-1]) {
165 map_banks[idx]->virt += mtd_banks[idx-1]->size;
166 map_banks[idx]->phys += mtd_banks[idx-1]->size;
167 }
168
169
170 mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]);
171
172 if (mtd_banks[idx]) {
173 mtd_banks[idx]->owner = THIS_MODULE;
174 mtd_size += mtd_banks[idx]->size;
175 num_banks++;
176
177 printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __func__, num_banks,
178 mtd_banks[idx]->name, mtd_banks[idx]->size);
179 }
180 }
181
182
183 if (!num_banks) {
184 printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n");
185 ret = -ENXIO;
186 goto error_mem;
187 }
188
189
190
191
192 part_banks[0].mtd_part = tqm8xxl_partitions;
193 part_banks[0].type = "Static image";
194 part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions);
195
196 part_banks[1].mtd_part = tqm8xxl_fs_partitions;
197 part_banks[1].type = "Static file system";
198 part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions);
199
200 for(idx = 0; idx < num_banks ; idx++) {
201 if (part_banks[idx].nums == 0)
202 printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx);
203 else
204 printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n",
205 idx, part_banks[idx].type);
206 mtd_device_register(mtd_banks[idx], part_banks[idx].mtd_part,
207 part_banks[idx].nums);
208 }
209 return 0;
210error_mem:
211 for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) {
212 if(map_banks[idx] != NULL) {
213 kfree(map_banks[idx]->name);
214 map_banks[idx]->name = NULL;
215 kfree(map_banks[idx]);
216 map_banks[idx] = NULL;
217 }
218 }
219error:
220 iounmap(start_scan_addr);
221 return ret;
222}
223
224static void __exit cleanup_tqm_mtd(void)
225{
226 unsigned int idx = 0;
227 for(idx = 0 ; idx < num_banks ; idx++) {
228
229 if (mtd_banks[idx]) {
230 mtd_device_unregister(mtd_banks[idx]);
231 map_destroy(mtd_banks[idx]);
232 }
233
234 kfree(map_banks[idx]->name);
235 kfree(map_banks[idx]);
236 }
237
238 if (start_scan_addr) {
239 iounmap(start_scan_addr);
240 start_scan_addr = 0;
241 }
242}
243
244module_init(init_tqm_mtd);
245module_exit(cleanup_tqm_mtd);
246
247MODULE_LICENSE("GPL");
248MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>");
249MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards");
250