1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/slab.h>
20
21#include <linux/mtd/concat.h>
22#include <linux/mtd/map.h>
23#include <linux/mtd/mtd.h>
24#include <linux/mtd/mtdram.h>
25#include <linux/mtd/partitions.h>
26
27#include <asm/axisflashmap.h>
28#include <asm/mmu.h>
29#include <arch/sv_addr_ag.h>
30
31#ifdef CONFIG_CRIS_LOW_MAP
32#define FLASH_UNCACHED_ADDR KSEG_8
33#define FLASH_CACHED_ADDR KSEG_5
34#else
35#define FLASH_UNCACHED_ADDR KSEG_E
36#define FLASH_CACHED_ADDR KSEG_F
37#endif
38
39#if CONFIG_ETRAX_FLASH_BUSWIDTH==1
40#define flash_data __u8
41#elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
42#define flash_data __u16
43#elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
44#define flash_data __u32
45#endif
46
47
48extern unsigned long romfs_start, romfs_length, romfs_in_flash;
49
50
51struct mtd_info* axisflash_mtd = NULL;
52
53
54
55static map_word flash_read(struct map_info *map, unsigned long ofs)
56{
57 map_word tmp;
58 tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
59 return tmp;
60}
61
62static void flash_copy_from(struct map_info *map, void *to,
63 unsigned long from, ssize_t len)
64{
65 memcpy(to, (void *)(map->map_priv_1 + from), len);
66}
67
68static void flash_write(struct map_info *map, map_word d, unsigned long adr)
69{
70 *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
71}
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88static struct map_info map_cse0 = {
89 .name = "cse0",
90 .size = MEM_CSE0_SIZE,
91 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
92 .read = flash_read,
93 .copy_from = flash_copy_from,
94 .write = flash_write,
95 .map_priv_1 = FLASH_UNCACHED_ADDR
96};
97
98
99
100
101
102
103
104static struct map_info map_cse1 = {
105 .name = "cse1",
106 .size = MEM_CSE1_SIZE,
107 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
108 .read = flash_read,
109 .copy_from = flash_copy_from,
110 .write = flash_write,
111 .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
112};
113
114
115#define MAX_PARTITIONS 7
116#define NUM_DEFAULT_PARTITIONS 3
117
118
119
120
121
122
123static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
124 {
125 .name = "boot firmware",
126 .size = CONFIG_ETRAX_PTABLE_SECTOR,
127 .offset = 0
128 },
129 {
130 .name = "kernel",
131 .size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),
132 .offset = CONFIG_ETRAX_PTABLE_SECTOR
133 },
134 {
135 .name = "filesystem",
136 .size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,
137 .offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)
138 }
139};
140
141
142static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
143 {
144 .name = "part0",
145 .size = CONFIG_ETRAX_PTABLE_SECTOR,
146 .offset = 0
147 },
148 {
149 .name = "part1",
150 .size = 0,
151 .offset = 0
152 },
153 {
154 .name = "part2",
155 .size = 0,
156 .offset = 0
157 },
158 {
159 .name = "part3",
160 .size = 0,
161 .offset = 0
162 },
163 {
164 .name = "part4",
165 .size = 0,
166 .offset = 0
167 },
168 {
169 .name = "part5",
170 .size = 0,
171 .offset = 0
172 },
173 {
174 .name = "part6",
175 .size = 0,
176 .offset = 0
177 },
178};
179
180#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
181
182static struct mtd_partition main_partition = {
183 .name = "main",
184 .size = 0,
185 .offset = 0
186};
187#endif
188
189
190
191
192
193static struct mtd_info *probe_cs(struct map_info *map_cs)
194{
195 struct mtd_info *mtd_cs = NULL;
196
197 printk(KERN_INFO
198 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
199 map_cs->name, map_cs->size, map_cs->map_priv_1);
200
201#ifdef CONFIG_MTD_CFI
202 mtd_cs = do_map_probe("cfi_probe", map_cs);
203#endif
204#ifdef CONFIG_MTD_JEDECPROBE
205 if (!mtd_cs)
206 mtd_cs = do_map_probe("jedec_probe", map_cs);
207#endif
208
209 return mtd_cs;
210}
211
212
213
214
215
216
217
218
219
220
221
222static struct mtd_info *flash_probe(void)
223{
224 struct mtd_info *mtd_cse0;
225 struct mtd_info *mtd_cse1;
226 struct mtd_info *mtd_cse;
227
228 mtd_cse0 = probe_cs(&map_cse0);
229 mtd_cse1 = probe_cs(&map_cse1);
230
231 if (!mtd_cse0 && !mtd_cse1) {
232
233 return NULL;
234 }
235
236 if (mtd_cse0 && mtd_cse1) {
237 struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };
238
239
240
241
242
243
244
245
246 mtd_cse = mtd_concat_create(mtds, ARRAY_SIZE(mtds),
247 "cse0+cse1");
248 if (!mtd_cse) {
249 printk(KERN_ERR "%s and %s: Concatenation failed!\n",
250 map_cse0.name, map_cse1.name);
251
252
253
254
255 mtd_cse = mtd_cse0;
256 map_destroy(mtd_cse1);
257 }
258 } else {
259 mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;
260 }
261
262 return mtd_cse;
263}
264
265
266
267
268
269static int __init init_axis_flash(void)
270{
271 struct mtd_info *mymtd;
272 int err = 0;
273 int pidx = 0;
274 struct partitiontable_head *ptable_head = NULL;
275 struct partitiontable_entry *ptable;
276 int use_default_ptable = 1;
277 const char pmsg[] = " /dev/flash%d at 0x%08x, size 0x%08x\n";
278
279 if (!(mymtd = flash_probe())) {
280
281
282
283 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
284 } else {
285 printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",
286 mymtd->name, mymtd->size);
287 axisflash_mtd = mymtd;
288 }
289
290 if (mymtd) {
291 mymtd->owner = THIS_MODULE;
292 ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +
293 CONFIG_ETRAX_PTABLE_SECTOR +
294 PARTITION_TABLE_OFFSET);
295 }
296 pidx++;
297
298 if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
299 && (ptable_head->size <
300 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
301 PARTITIONTABLE_END_MARKER_SIZE))
302 && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
303 ptable_head->size -
304 PARTITIONTABLE_END_MARKER_SIZE)
305 == PARTITIONTABLE_END_MARKER)) {
306
307
308
309 int ptable_ok = 0;
310 struct partitiontable_entry *max_addr =
311 (struct partitiontable_entry *)
312 ((unsigned long)ptable_head + sizeof(*ptable_head) +
313 ptable_head->size);
314 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
315 unsigned char *p;
316 unsigned long csum = 0;
317
318 ptable = (struct partitiontable_entry *)
319 ((unsigned long)ptable_head + sizeof(*ptable_head));
320
321
322 p = (unsigned char*) ptable;
323
324 while (p <= (unsigned char*)max_addr) {
325 csum += *p++;
326 csum += *p++;
327 csum += *p++;
328 csum += *p++;
329 }
330 ptable_ok = (csum == ptable_head->checksum);
331
332
333 printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",
334 (ptable_ok ? " valid" : "n invalid"), ptable_head,
335 max_addr);
336
337
338
339
340
341 while (ptable_ok
342 && ptable->offset != 0xffffffff
343 && ptable < max_addr
344 && pidx < MAX_PARTITIONS) {
345
346 axis_partitions[pidx].offset = offset + ptable->offset;
347 axis_partitions[pidx].size = ptable->size;
348
349 printk(pmsg, pidx, axis_partitions[pidx].offset,
350 axis_partitions[pidx].size);
351 pidx++;
352 ptable++;
353 }
354 use_default_ptable = !ptable_ok;
355 }
356
357 if (romfs_in_flash) {
358
359
360 axis_partitions[pidx].name = "romfs";
361 axis_partitions[pidx].size = romfs_length;
362 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
363 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
364
365 printk(KERN_INFO
366 " Adding readonly flash partition for romfs image:\n");
367 printk(pmsg, pidx, axis_partitions[pidx].offset,
368 axis_partitions[pidx].size);
369 pidx++;
370 }
371
372#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
373 if (mymtd) {
374 main_partition.size = mymtd->size;
375 err = mtd_device_register(mymtd, &main_partition, 1);
376 if (err)
377 panic("axisflashmap: Could not initialize "
378 "partition for whole main mtd device!\n");
379 }
380#endif
381
382 if (mymtd) {
383 if (use_default_ptable) {
384 printk(KERN_INFO " Using default partition table.\n");
385 err = mtd_device_register(mymtd,
386 axis_default_partitions,
387 NUM_DEFAULT_PARTITIONS);
388 } else {
389 err = mtd_device_register(mymtd, axis_partitions,
390 pidx);
391 }
392
393 if (err)
394 panic("axisflashmap could not add MTD partitions!\n");
395 }
396
397 if (!romfs_in_flash) {
398
399
400#if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
401
402 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
403 "device due to kernel (mis)configuration!\n");
404 panic("This kernel cannot boot from RAM!\n");
405#else
406 struct mtd_info *mtd_ram;
407
408 mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
409 if (!mtd_ram)
410 panic("axisflashmap couldn't allocate memory for "
411 "mtd_info!\n");
412
413 printk(KERN_INFO " Adding RAM partition for romfs image:\n");
414 printk(pmsg, pidx, (unsigned)romfs_start,
415 (unsigned)romfs_length);
416
417 err = mtdram_init_device(mtd_ram,
418 (void *)romfs_start,
419 romfs_length,
420 "romfs");
421 if (err)
422 panic("axisflashmap could not initialize MTD RAM "
423 "device!\n");
424#endif
425 }
426 return err;
427}
428
429
430module_init(init_axis_flash);
431
432EXPORT_SYMBOL(axisflash_mtd);
433