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 <linux/cramfs_fs.h>
28
29#include <asm/axisflashmap.h>
30#include <asm/mmu.h>
31
32#define MEM_CSE0_SIZE (0x04000000)
33#define MEM_CSE1_SIZE (0x04000000)
34
35#define FLASH_UNCACHED_ADDR KSEG_E
36#define FLASH_CACHED_ADDR KSEG_F
37
38#define PAGESIZE (512)
39
40#if CONFIG_ETRAX_FLASH_BUSWIDTH==1
41#define flash_data __u8
42#elif CONFIG_ETRAX_FLASH_BUSWIDTH==2
43#define flash_data __u16
44#elif CONFIG_ETRAX_FLASH_BUSWIDTH==4
45#define flash_data __u32
46#endif
47
48
49extern unsigned long romfs_in_flash;
50extern unsigned long romfs_start, romfs_length;
51extern unsigned long nand_boot;
52
53struct partition_name {
54 char name[6];
55};
56
57
58struct mtd_info* axisflash_mtd = NULL;
59
60
61
62static map_word flash_read(struct map_info *map, unsigned long ofs)
63{
64 map_word tmp;
65 tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);
66 return tmp;
67}
68
69static void flash_copy_from(struct map_info *map, void *to,
70 unsigned long from, ssize_t len)
71{
72 memcpy(to, (void *)(map->map_priv_1 + from), len);
73}
74
75static void flash_write(struct map_info *map, map_word d, unsigned long adr)
76{
77 *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];
78}
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95static struct map_info map_cse0 = {
96 .name = "cse0",
97 .size = MEM_CSE0_SIZE,
98 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
99 .read = flash_read,
100 .copy_from = flash_copy_from,
101 .write = flash_write,
102 .map_priv_1 = FLASH_UNCACHED_ADDR
103};
104
105
106
107
108
109
110
111static struct map_info map_cse1 = {
112 .name = "cse1",
113 .size = MEM_CSE1_SIZE,
114 .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,
115 .read = flash_read,
116 .copy_from = flash_copy_from,
117 .write = flash_write,
118 .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE
119};
120
121#define MAX_PARTITIONS 7
122#ifdef CONFIG_ETRAX_NANDBOOT
123#define NUM_DEFAULT_PARTITIONS 4
124#define DEFAULT_ROOTFS_PARTITION_NO 2
125#define DEFAULT_MEDIA_SIZE 0x2000000
126#else
127#define NUM_DEFAULT_PARTITIONS 3
128#define DEFAULT_ROOTFS_PARTITION_NO (-1)
129#define DEFAULT_MEDIA_SIZE 0x800000
130#endif
131
132#if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS)
133#error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS
134#endif
135
136
137static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {
138 {
139 .name = "part0",
140 .size = CONFIG_ETRAX_PTABLE_SECTOR,
141 .offset = 0
142 },
143 {
144 .name = "part1",
145 .size = 0,
146 .offset = 0
147 },
148 {
149 .name = "part2",
150 .size = 0,
151 .offset = 0
152 },
153 {
154 .name = "part3",
155 .size = 0,
156 .offset = 0
157 },
158 {
159 .name = "part4",
160 .size = 0,
161 .offset = 0
162 },
163 {
164 .name = "part5",
165 .size = 0,
166 .offset = 0
167 },
168 {
169 .name = "part6",
170 .size = 0,
171 .offset = 0
172 },
173};
174
175
176
177
178
179
180
181static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {
182 {
183 .name = "boot firmware",
184 .size = CONFIG_ETRAX_PTABLE_SECTOR,
185 .offset = 0
186 },
187 {
188 .name = "kernel",
189 .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR,
190 .offset = CONFIG_ETRAX_PTABLE_SECTOR
191 },
192#define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR)
193#ifdef CONFIG_ETRAX_NANDBOOT
194 {
195 .name = "rootfs",
196 .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR,
197 .offset = FILESYSTEM_SECTOR
198 },
199#undef FILESYSTEM_SECTOR
200#define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR)
201#endif
202 {
203 .name = "rwfs",
204 .size = DEFAULT_MEDIA_SIZE - FILESYSTEM_SECTOR,
205 .offset = FILESYSTEM_SECTOR
206 }
207};
208
209#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
210
211static struct mtd_partition main_partition = {
212 .name = "main",
213 .size = 0,
214 .offset = 0
215};
216#endif
217
218
219static struct mtd_partition aux_partition = {
220 .name = "aux",
221 .size = 0,
222 .offset = 0
223};
224
225
226
227
228
229static struct mtd_info *probe_cs(struct map_info *map_cs)
230{
231 struct mtd_info *mtd_cs = NULL;
232
233 printk(KERN_INFO
234 "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",
235 map_cs->name, map_cs->size, map_cs->map_priv_1);
236
237#ifdef CONFIG_MTD_CFI
238 mtd_cs = do_map_probe("cfi_probe", map_cs);
239#endif
240#ifdef CONFIG_MTD_JEDECPROBE
241 if (!mtd_cs)
242 mtd_cs = do_map_probe("jedec_probe", map_cs);
243#endif
244
245 return mtd_cs;
246}
247
248
249
250
251
252
253
254
255
256
257
258extern struct mtd_info* __init crisv32_nand_flash_probe (void);
259static struct mtd_info *flash_probe(void)
260{
261 struct mtd_info *mtd_cse0;
262 struct mtd_info *mtd_cse1;
263 struct mtd_info *mtd_total;
264 struct mtd_info *mtds[2];
265 int count = 0;
266
267 if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)
268 mtds[count++] = mtd_cse0;
269 if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)
270 mtds[count++] = mtd_cse1;
271
272 if (!mtd_cse0 && !mtd_cse1) {
273
274 return NULL;
275 }
276
277 if (count > 1) {
278#ifdef CONFIG_MTD_CONCAT
279
280
281
282
283
284
285
286 mtd_total = mtd_concat_create(mtds, count, "cse0+cse1");
287#else
288 printk(KERN_ERR "%s and %s: Cannot concatenate due to kernel "
289 "(mis)configuration!\n", map_cse0.name, map_cse1.name);
290 mtd_toal = NULL;
291#endif
292 if (!mtd_total) {
293 printk(KERN_ERR "%s and %s: Concatenation failed!\n",
294 map_cse0.name, map_cse1.name);
295
296
297
298 mtd_total = mtd_cse0;
299 map_destroy(mtd_cse1);
300 }
301 } else
302 mtd_total = mtd_cse0 ? mtd_cse0 : mtd_cse1;
303
304 return mtd_total;
305}
306
307
308
309
310
311static int __init init_axis_flash(void)
312{
313 struct mtd_info *main_mtd;
314 struct mtd_info *aux_mtd = NULL;
315 int err = 0;
316 int pidx = 0;
317 struct partitiontable_head *ptable_head = NULL;
318 struct partitiontable_entry *ptable;
319 int ptable_ok = 0;
320 static char page[PAGESIZE];
321 size_t len;
322 int ram_rootfs_partition = -1;
323 int part;
324
325
326
327
328
329
330#if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)
331 if (!romfs_in_flash && !nand_boot) {
332 printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "
333 "device; configure CONFIG_MTD_MTDRAM with size = 0!\n");
334 panic("This kernel cannot boot from RAM!\n");
335 }
336#endif
337
338#ifndef CONFIG_ETRAX_VCS_SIM
339 main_mtd = flash_probe();
340 if (main_mtd)
341 printk(KERN_INFO "%s: 0x%08x bytes of NOR flash memory.\n",
342 main_mtd->name, main_mtd->size);
343
344#ifdef CONFIG_ETRAX_NANDFLASH
345 aux_mtd = crisv32_nand_flash_probe();
346 if (aux_mtd)
347 printk(KERN_INFO "%s: 0x%08x bytes of NAND flash memory.\n",
348 aux_mtd->name, aux_mtd->size);
349
350#ifdef CONFIG_ETRAX_NANDBOOT
351 {
352 struct mtd_info *tmp_mtd;
353
354 printk(KERN_INFO "axisflashmap: Set to boot from NAND flash, "
355 "making NAND flash primary device.\n");
356 tmp_mtd = main_mtd;
357 main_mtd = aux_mtd;
358 aux_mtd = tmp_mtd;
359 }
360#endif
361#endif
362
363 if (!main_mtd && !aux_mtd) {
364
365
366
367 printk(KERN_INFO "axisflashmap: Found no flash chip.\n");
368 }
369
370#if 0
371 if (main_mtd) {
372 int sectoraddr, i;
373 for (sectoraddr = 0; sectoraddr < 2*65536+4096;
374 sectoraddr += PAGESIZE) {
375 main_mtd->read(main_mtd, sectoraddr, PAGESIZE, &len,
376 page);
377 printk(KERN_INFO
378 "Sector at %d (length %d):\n",
379 sectoraddr, len);
380 for (i = 0; i < PAGESIZE; i += 16) {
381 printk(KERN_INFO
382 "%02x %02x %02x %02x "
383 "%02x %02x %02x %02x "
384 "%02x %02x %02x %02x "
385 "%02x %02x %02x %02x\n",
386 page[i] & 255, page[i+1] & 255,
387 page[i+2] & 255, page[i+3] & 255,
388 page[i+4] & 255, page[i+5] & 255,
389 page[i+6] & 255, page[i+7] & 255,
390 page[i+8] & 255, page[i+9] & 255,
391 page[i+10] & 255, page[i+11] & 255,
392 page[i+12] & 255, page[i+13] & 255,
393 page[i+14] & 255, page[i+15] & 255);
394 }
395 }
396 }
397#endif
398
399 if (main_mtd) {
400 main_mtd->owner = THIS_MODULE;
401 axisflash_mtd = main_mtd;
402
403 loff_t ptable_sector = CONFIG_ETRAX_PTABLE_SECTOR;
404
405
406 pidx++;
407#ifdef CONFIG_ETRAX_NANDBOOT
408
409
410
411 int blockstat;
412 do {
413 blockstat = main_mtd->block_isbad(main_mtd,
414 ptable_sector);
415 if (blockstat < 0)
416 ptable_sector = 0;
417 else if (blockstat)
418 ptable_sector += main_mtd->erasesize;
419 } while (blockstat && ptable_sector);
420#endif
421 if (ptable_sector) {
422 main_mtd->read(main_mtd, ptable_sector, PAGESIZE,
423 &len, page);
424 ptable_head = &((struct partitiontable *) page)->head;
425 }
426
427#if 0
428 printk(KERN_INFO
429 "axisflashmap: flash read %d bytes at 0x%08x, data: "
430 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
431 len, CONFIG_ETRAX_PTABLE_SECTOR,
432 page[0] & 255, page[1] & 255,
433 page[2] & 255, page[3] & 255,
434 page[4] & 255, page[5] & 255,
435 page[6] & 255, page[7] & 255);
436 printk(KERN_INFO
437 "axisflashmap: partition table offset %d, data: "
438 "%02x %02x %02x %02x %02x %02x %02x %02x\n",
439 PARTITION_TABLE_OFFSET,
440 page[PARTITION_TABLE_OFFSET+0] & 255,
441 page[PARTITION_TABLE_OFFSET+1] & 255,
442 page[PARTITION_TABLE_OFFSET+2] & 255,
443 page[PARTITION_TABLE_OFFSET+3] & 255,
444 page[PARTITION_TABLE_OFFSET+4] & 255,
445 page[PARTITION_TABLE_OFFSET+5] & 255,
446 page[PARTITION_TABLE_OFFSET+6] & 255,
447 page[PARTITION_TABLE_OFFSET+7] & 255);
448#endif
449 }
450
451 if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)
452 && (ptable_head->size <
453 (MAX_PARTITIONS * sizeof(struct partitiontable_entry) +
454 PARTITIONTABLE_END_MARKER_SIZE))
455 && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +
456 ptable_head->size -
457 PARTITIONTABLE_END_MARKER_SIZE)
458 == PARTITIONTABLE_END_MARKER)) {
459
460
461
462 struct partitiontable_entry *max_addr =
463 (struct partitiontable_entry *)
464 ((unsigned long)ptable_head + sizeof(*ptable_head) +
465 ptable_head->size);
466 unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;
467 unsigned char *p;
468 unsigned long csum = 0;
469
470 ptable = (struct partitiontable_entry *)
471 ((unsigned long)ptable_head + sizeof(*ptable_head));
472
473
474 p = (unsigned char*) ptable;
475
476 while (p <= (unsigned char*)max_addr) {
477 csum += *p++;
478 csum += *p++;
479 csum += *p++;
480 csum += *p++;
481 }
482 ptable_ok = (csum == ptable_head->checksum);
483
484
485 printk(KERN_INFO "axisflashmap: "
486 "Found a%s partition table at 0x%p-0x%p.\n",
487 (ptable_ok ? " valid" : "n invalid"), ptable_head,
488 max_addr);
489
490
491
492
493 while (ptable_ok
494 && ptable->offset != PARTITIONTABLE_END_MARKER
495 && ptable < max_addr
496 && pidx < MAX_PARTITIONS - 1) {
497
498 axis_partitions[pidx].offset = offset + ptable->offset;
499#ifdef CONFIG_ETRAX_NANDFLASH
500 if (main_mtd->type == MTD_NANDFLASH) {
501 axis_partitions[pidx].size =
502 (((ptable+1)->offset ==
503 PARTITIONTABLE_END_MARKER) ?
504 main_mtd->size :
505 ((ptable+1)->offset + offset)) -
506 (ptable->offset + offset);
507
508 } else
509#endif
510 axis_partitions[pidx].size = ptable->size;
511#ifdef CONFIG_ETRAX_NANDBOOT
512
513
514
515 if (!nand_boot &&
516 ram_rootfs_partition < 0 &&
517 ptable->type == PARTITION_TYPE_JFFS2 &&
518 (ptable->flags & PARTITION_FLAGS_READONLY_MASK) ==
519 PARTITION_FLAGS_READONLY)
520 ram_rootfs_partition = pidx;
521#endif
522 pidx++;
523 ptable++;
524 }
525 }
526
527
528
529
530 struct mtd_partition *partition = &axis_partitions[0];
531 if (main_mtd && !ptable_ok) {
532 memcpy(axis_partitions, axis_default_partitions,
533 sizeof(axis_default_partitions));
534 pidx = NUM_DEFAULT_PARTITIONS;
535 ram_rootfs_partition = DEFAULT_ROOTFS_PARTITION_NO;
536 }
537
538
539 if (romfs_in_flash) {
540
541
542 printk(KERN_INFO "axisflashmap: Adding partition for "
543 "overlapping root file system image\n");
544 axis_partitions[pidx].size = romfs_length;
545 axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;
546 axis_partitions[pidx].name = "romfs";
547 axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;
548 ram_rootfs_partition = -1;
549 pidx++;
550 } else if (romfs_length && !nand_boot) {
551
552
553 if (ram_rootfs_partition < 0) {
554
555 ram_rootfs_partition = pidx;
556 pidx++;
557 }
558 printk(KERN_INFO "axisflashmap: Adding partition for "
559 "root file system image in RAM\n");
560 axis_partitions[ram_rootfs_partition].size = romfs_length;
561 axis_partitions[ram_rootfs_partition].offset = romfs_start;
562 axis_partitions[ram_rootfs_partition].name = "romfs";
563 axis_partitions[ram_rootfs_partition].mask_flags |=
564 MTD_WRITEABLE;
565 }
566
567#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE
568 if (main_mtd) {
569 main_partition.size = main_mtd->size;
570 err = add_mtd_partitions(main_mtd, &main_partition, 1);
571 if (err)
572 panic("axisflashmap: Could not initialize "
573 "partition for whole main mtd device!\n");
574 }
575#endif
576
577
578
579
580
581 for (part = 0; part < pidx; part++) {
582 if (part == ram_rootfs_partition) {
583
584 struct mtd_info *mtd_ram;
585
586 mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
587 if (!mtd_ram)
588 panic("axisflashmap: Couldn't allocate memory "
589 "for mtd_info!\n");
590 printk(KERN_INFO "axisflashmap: Adding RAM partition "
591 "for rootfs image.\n");
592 err = mtdram_init_device(mtd_ram,
593 (void *)partition[part].offset,
594 partition[part].size,
595 partition[part].name);
596 if (err)
597 panic("axisflashmap: Could not initialize "
598 "MTD RAM device!\n");
599
600
601
602
603 mtd_ram->erasesize = (main_mtd ? main_mtd->erasesize :
604 CONFIG_ETRAX_PTABLE_SECTOR);
605 } else {
606 err = add_mtd_partitions(main_mtd, &partition[part], 1);
607 if (err)
608 panic("axisflashmap: Could not add mtd "
609 "partition %d\n", part);
610 }
611 }
612#endif
613
614#ifdef CONFIG_ETRAX_VCS_SIM
615
616
617
618
619 struct mtd_info *mtd_ram;
620
621 mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);
622 if (!mtd_ram) {
623 panic("axisflashmap: Couldn't allocate memory for "
624 "mtd_info!\n");
625 }
626
627 printk(KERN_INFO "axisflashmap: Adding RAM partition for romfs, "
628 "at %u, size %u\n",
629 (unsigned) romfs_start, (unsigned) romfs_length);
630
631 err = mtdram_init_device(mtd_ram, (void *)romfs_start,
632 romfs_length, "romfs");
633 if (err) {
634 panic("axisflashmap: Could not initialize MTD RAM "
635 "device!\n");
636 }
637#endif
638
639#ifndef CONFIG_ETRAX_VCS_SIM
640 if (aux_mtd) {
641 aux_partition.size = aux_mtd->size;
642 err = add_mtd_partitions(aux_mtd, &aux_partition, 1);
643 if (err)
644 panic("axisflashmap: Could not initialize "
645 "aux mtd device!\n");
646
647 }
648#endif
649
650 return err;
651}
652
653
654module_init(init_axis_flash);
655
656EXPORT_SYMBOL(axisflash_mtd);
657