Commit | Line | Data |
---|---|---|
51533b61 MS |
1 | /* |
2 | * Physical mapping layer for MTD using the Axis partitiontable format | |
3 | * | |
5fc1f312 | 4 | * Copyright (c) 2001-2007 Axis Communications AB |
51533b61 MS |
5 | * |
6 | * This file is under the GPL. | |
7 | * | |
8 | * First partition is always sector 0 regardless of if we find a partitiontable | |
9 | * or not. In the start of the next sector, there can be a partitiontable that | |
10 | * tells us what other partitions to define. If there isn't, we use a default | |
11 | * partition split defined below. | |
12 | * | |
51533b61 MS |
13 | */ |
14 | ||
15 | #include <linux/module.h> | |
16 | #include <linux/types.h> | |
17 | #include <linux/kernel.h> | |
51533b61 | 18 | #include <linux/init.h> |
4e57b681 | 19 | #include <linux/slab.h> |
51533b61 MS |
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 | ||
5fc1f312 JN |
27 | #include <linux/cramfs_fs.h> |
28 | ||
51533b61 MS |
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 | ||
5fc1f312 JN |
38 | #define PAGESIZE (512) |
39 | ||
51533b61 MS |
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 | |
5fc1f312 | 45 | #define flash_data __u32 |
51533b61 MS |
46 | #endif |
47 | ||
48 | /* From head.S */ | |
5fc1f312 JN |
49 | extern unsigned long romfs_in_flash; /* 1 when romfs_start, _length in flash */ |
50 | extern unsigned long romfs_start, romfs_length; | |
51 | extern unsigned long nand_boot; /* 1 when booted from nand flash */ | |
52 | ||
53 | struct partition_name { | |
54 | char name[6]; | |
55 | }; | |
51533b61 MS |
56 | |
57 | /* The master mtd for the entire flash. */ | |
58 | struct mtd_info* axisflash_mtd = NULL; | |
59 | ||
60 | /* Map driver functions. */ | |
61 | ||
62 | static 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 | ||
69 | static 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 | ||
75 | static 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 | * The map for chip select e0. | |
82 | * | |
83 | * We run into tricky coherence situations if we mix cached with uncached | |
84 | * accesses to we only use the uncached version here. | |
85 | * | |
86 | * The size field is the total size where the flash chips may be mapped on the | |
87 | * chip select. MTD probes should find all devices there and it does not matter | |
88 | * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD | |
89 | * probes will ignore them. | |
90 | * | |
91 | * The start address in map_priv_1 is in virtual memory so we cannot use | |
92 | * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start | |
93 | * address of cse0. | |
94 | */ | |
95 | static 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 | * The map for chip select e1. | |
107 | * | |
108 | * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong | |
109 | * address, but there isn't. | |
110 | */ | |
111 | static 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 | ||
5fc1f312 JN |
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 /* 32 megs */ | |
126 | #else | |
127 | #define NUM_DEFAULT_PARTITIONS 3 | |
128 | #define DEFAULT_ROOTFS_PARTITION_NO (-1) | |
129 | #define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */ | |
130 | #endif | |
51533b61 | 131 | |
5fc1f312 JN |
132 | #if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS) |
133 | #error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS | |
134 | #endif | |
51533b61 MS |
135 | |
136 | /* Initialize the ones normally used. */ | |
137 | static 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 | ||
5fc1f312 JN |
175 | |
176 | /* If no partition-table was found, we use this default-set. | |
177 | * Default flash size is 8MB (NOR). CONFIG_ETRAX_PTABLE_SECTOR is most | |
178 | * likely the size of one flash block and "filesystem"-partition needs | |
179 | * to be >=5 blocks to be able to use JFFS. | |
180 | */ | |
181 | static 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 | /* Main flash device */ | |
211 | static struct mtd_partition main_partition = { | |
212 | .name = "main", | |
213 | .size = 0, | |
214 | .offset = 0 | |
215 | }; | |
216 | #endif | |
217 | ||
25985edc | 218 | /* Auxiliary partition if we find another flash */ |
5fc1f312 JN |
219 | static struct mtd_partition aux_partition = { |
220 | .name = "aux", | |
221 | .size = 0, | |
222 | .offset = 0 | |
223 | }; | |
224 | ||
51533b61 MS |
225 | /* |
226 | * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash | |
227 | * chips in that order (because the amd_flash-driver is faster). | |
228 | */ | |
229 | static 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 | ||
51533b61 | 237 | #ifdef CONFIG_MTD_CFI |
5fc1f312 | 238 | mtd_cs = do_map_probe("cfi_probe", map_cs); |
1b8be1d8 JN |
239 | #endif |
240 | #ifdef CONFIG_MTD_JEDECPROBE | |
241 | if (!mtd_cs) | |
242 | mtd_cs = do_map_probe("jedec_probe", map_cs); | |
51533b61 MS |
243 | #endif |
244 | ||
245 | return mtd_cs; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Probe each chip select individually for flash chips. If there are chips on | |
250 | * both cse0 and cse1, the mtd_info structs will be concatenated to one struct | |
5fc1f312 | 251 | * so that MTD partitions can cross chip boundries. |
51533b61 MS |
252 | * |
253 | * The only known restriction to how you can mount your chips is that each | |
254 | * chip select must hold similar flash chips. But you need external hardware | |
255 | * to do that anyway and you can put totally different chips on cse0 and cse1 | |
256 | * so it isn't really much of a restriction. | |
257 | */ | |
258 | extern struct mtd_info* __init crisv32_nand_flash_probe (void); | |
259 | static struct mtd_info *flash_probe(void) | |
260 | { | |
261 | struct mtd_info *mtd_cse0; | |
262 | struct mtd_info *mtd_cse1; | |
51533b61 | 263 | struct mtd_info *mtd_total; |
5fc1f312 | 264 | struct mtd_info *mtds[2]; |
51533b61 MS |
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 | ||
5fc1f312 | 272 | if (!mtd_cse0 && !mtd_cse1) { |
51533b61 MS |
273 | /* No chip found. */ |
274 | return NULL; | |
275 | } | |
276 | ||
277 | if (count > 1) { | |
51533b61 MS |
278 | /* Since the concatenation layer adds a small overhead we |
279 | * could try to figure out if the chips in cse0 and cse1 are | |
280 | * identical and reprobe the whole cse0+cse1 window. But since | |
281 | * flash chips are slow, the overhead is relatively small. | |
282 | * So we use the MTD concatenation layer instead of further | |
283 | * complicating the probing procedure. | |
284 | */ | |
5fc1f312 | 285 | mtd_total = mtd_concat_create(mtds, count, "cse0+cse1"); |
51533b61 MS |
286 | if (!mtd_total) { |
287 | printk(KERN_ERR "%s and %s: Concatenation failed!\n", | |
5fc1f312 | 288 | map_cse0.name, map_cse1.name); |
51533b61 MS |
289 | |
290 | /* The best we can do now is to only use what we found | |
5fc1f312 | 291 | * at cse0. */ |
51533b61 MS |
292 | mtd_total = mtd_cse0; |
293 | map_destroy(mtd_cse1); | |
294 | } | |
5fc1f312 JN |
295 | } else |
296 | mtd_total = mtd_cse0 ? mtd_cse0 : mtd_cse1; | |
51533b61 MS |
297 | |
298 | return mtd_total; | |
299 | } | |
300 | ||
51533b61 MS |
301 | /* |
302 | * Probe the flash chip(s) and, if it succeeds, read the partition-table | |
303 | * and register the partitions with MTD. | |
304 | */ | |
305 | static int __init init_axis_flash(void) | |
306 | { | |
5fc1f312 JN |
307 | struct mtd_info *main_mtd; |
308 | struct mtd_info *aux_mtd = NULL; | |
51533b61 MS |
309 | int err = 0; |
310 | int pidx = 0; | |
311 | struct partitiontable_head *ptable_head = NULL; | |
312 | struct partitiontable_entry *ptable; | |
5fc1f312 JN |
313 | int ptable_ok = 0; |
314 | static char page[PAGESIZE]; | |
51533b61 | 315 | size_t len; |
5fc1f312 JN |
316 | int ram_rootfs_partition = -1; /* -1 => no RAM rootfs partition */ |
317 | int part; | |
318 | ||
319 | /* We need a root fs. If it resides in RAM, we need to use an | |
320 | * MTDRAM device, so it must be enabled in the kernel config, | |
321 | * but its size must be configured as 0 so as not to conflict | |
322 | * with our usage. | |
323 | */ | |
324 | #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0) | |
325 | if (!romfs_in_flash && !nand_boot) { | |
326 | printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM " | |
327 | "device; configure CONFIG_MTD_MTDRAM with size = 0!\n"); | |
328 | panic("This kernel cannot boot from RAM!\n"); | |
329 | } | |
330 | #endif | |
331 | ||
332 | #ifndef CONFIG_ETRAX_VCS_SIM | |
333 | main_mtd = flash_probe(); | |
334 | if (main_mtd) | |
335 | printk(KERN_INFO "%s: 0x%08x bytes of NOR flash memory.\n", | |
336 | main_mtd->name, main_mtd->size); | |
337 | ||
338 | #ifdef CONFIG_ETRAX_NANDFLASH | |
339 | aux_mtd = crisv32_nand_flash_probe(); | |
340 | if (aux_mtd) | |
341 | printk(KERN_INFO "%s: 0x%08x bytes of NAND flash memory.\n", | |
342 | aux_mtd->name, aux_mtd->size); | |
343 | ||
344 | #ifdef CONFIG_ETRAX_NANDBOOT | |
345 | { | |
346 | struct mtd_info *tmp_mtd; | |
51533b61 | 347 | |
5fc1f312 JN |
348 | printk(KERN_INFO "axisflashmap: Set to boot from NAND flash, " |
349 | "making NAND flash primary device.\n"); | |
350 | tmp_mtd = main_mtd; | |
351 | main_mtd = aux_mtd; | |
352 | aux_mtd = tmp_mtd; | |
353 | } | |
354 | #endif /* CONFIG_ETRAX_NANDBOOT */ | |
355 | #endif /* CONFIG_ETRAX_NANDFLASH */ | |
51533b61 | 356 | |
5fc1f312 | 357 | if (!main_mtd && !aux_mtd) { |
51533b61 MS |
358 | /* There's no reason to use this module if no flash chip can |
359 | * be identified. Make sure that's understood. | |
360 | */ | |
361 | printk(KERN_INFO "axisflashmap: Found no flash chip.\n"); | |
51533b61 MS |
362 | } |
363 | ||
5fc1f312 JN |
364 | #if 0 /* Dump flash memory so we can see what is going on */ |
365 | if (main_mtd) { | |
366 | int sectoraddr, i; | |
367 | for (sectoraddr = 0; sectoraddr < 2*65536+4096; | |
368 | sectoraddr += PAGESIZE) { | |
369 | main_mtd->read(main_mtd, sectoraddr, PAGESIZE, &len, | |
370 | page); | |
371 | printk(KERN_INFO | |
372 | "Sector at %d (length %d):\n", | |
373 | sectoraddr, len); | |
374 | for (i = 0; i < PAGESIZE; i += 16) { | |
375 | printk(KERN_INFO | |
376 | "%02x %02x %02x %02x " | |
377 | "%02x %02x %02x %02x " | |
378 | "%02x %02x %02x %02x " | |
379 | "%02x %02x %02x %02x\n", | |
380 | page[i] & 255, page[i+1] & 255, | |
381 | page[i+2] & 255, page[i+3] & 255, | |
382 | page[i+4] & 255, page[i+5] & 255, | |
383 | page[i+6] & 255, page[i+7] & 255, | |
384 | page[i+8] & 255, page[i+9] & 255, | |
385 | page[i+10] & 255, page[i+11] & 255, | |
386 | page[i+12] & 255, page[i+13] & 255, | |
387 | page[i+14] & 255, page[i+15] & 255); | |
388 | } | |
389 | } | |
390 | } | |
391 | #endif | |
392 | ||
393 | if (main_mtd) { | |
394 | main_mtd->owner = THIS_MODULE; | |
395 | axisflash_mtd = main_mtd; | |
396 | ||
397 | loff_t ptable_sector = CONFIG_ETRAX_PTABLE_SECTOR; | |
398 | ||
399 | /* First partition (rescue) is always set to the default. */ | |
400 | pidx++; | |
401 | #ifdef CONFIG_ETRAX_NANDBOOT | |
402 | /* We know where the partition table should be located, | |
403 | * it will be in first good block after that. | |
404 | */ | |
405 | int blockstat; | |
406 | do { | |
407 | blockstat = main_mtd->block_isbad(main_mtd, | |
408 | ptable_sector); | |
409 | if (blockstat < 0) | |
410 | ptable_sector = 0; /* read error */ | |
411 | else if (blockstat) | |
412 | ptable_sector += main_mtd->erasesize; | |
413 | } while (blockstat && ptable_sector); | |
414 | #endif | |
415 | if (ptable_sector) { | |
416 | main_mtd->read(main_mtd, ptable_sector, PAGESIZE, | |
417 | &len, page); | |
418 | ptable_head = &((struct partitiontable *) page)->head; | |
419 | } | |
420 | ||
421 | #if 0 /* Dump partition table so we can see what is going on */ | |
422 | printk(KERN_INFO | |
423 | "axisflashmap: flash read %d bytes at 0x%08x, data: " | |
424 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
425 | len, CONFIG_ETRAX_PTABLE_SECTOR, | |
426 | page[0] & 255, page[1] & 255, | |
427 | page[2] & 255, page[3] & 255, | |
428 | page[4] & 255, page[5] & 255, | |
429 | page[6] & 255, page[7] & 255); | |
430 | printk(KERN_INFO | |
431 | "axisflashmap: partition table offset %d, data: " | |
432 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", | |
433 | PARTITION_TABLE_OFFSET, | |
434 | page[PARTITION_TABLE_OFFSET+0] & 255, | |
435 | page[PARTITION_TABLE_OFFSET+1] & 255, | |
436 | page[PARTITION_TABLE_OFFSET+2] & 255, | |
437 | page[PARTITION_TABLE_OFFSET+3] & 255, | |
438 | page[PARTITION_TABLE_OFFSET+4] & 255, | |
439 | page[PARTITION_TABLE_OFFSET+5] & 255, | |
440 | page[PARTITION_TABLE_OFFSET+6] & 255, | |
441 | page[PARTITION_TABLE_OFFSET+7] & 255); | |
442 | #endif | |
51533b61 | 443 | } |
51533b61 MS |
444 | |
445 | if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC) | |
446 | && (ptable_head->size < | |
447 | (MAX_PARTITIONS * sizeof(struct partitiontable_entry) + | |
448 | PARTITIONTABLE_END_MARKER_SIZE)) | |
449 | && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) + | |
450 | ptable_head->size - | |
451 | PARTITIONTABLE_END_MARKER_SIZE) | |
452 | == PARTITIONTABLE_END_MARKER)) { | |
453 | /* Looks like a start, sane length and end of a | |
454 | * partition table, lets check csum etc. | |
455 | */ | |
51533b61 MS |
456 | struct partitiontable_entry *max_addr = |
457 | (struct partitiontable_entry *) | |
458 | ((unsigned long)ptable_head + sizeof(*ptable_head) + | |
459 | ptable_head->size); | |
460 | unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR; | |
461 | unsigned char *p; | |
462 | unsigned long csum = 0; | |
463 | ||
464 | ptable = (struct partitiontable_entry *) | |
465 | ((unsigned long)ptable_head + sizeof(*ptable_head)); | |
466 | ||
467 | /* Lets be PARANOID, and check the checksum. */ | |
468 | p = (unsigned char*) ptable; | |
469 | ||
470 | while (p <= (unsigned char*)max_addr) { | |
471 | csum += *p++; | |
472 | csum += *p++; | |
473 | csum += *p++; | |
474 | csum += *p++; | |
475 | } | |
476 | ptable_ok = (csum == ptable_head->checksum); | |
477 | ||
478 | /* Read the entries and use/show the info. */ | |
5fc1f312 JN |
479 | printk(KERN_INFO "axisflashmap: " |
480 | "Found a%s partition table at 0x%p-0x%p.\n", | |
51533b61 MS |
481 | (ptable_ok ? " valid" : "n invalid"), ptable_head, |
482 | max_addr); | |
483 | ||
484 | /* We have found a working bootblock. Now read the | |
5fc1f312 | 485 | * partition table. Scan the table. It ends with 0xffffffff. |
51533b61 MS |
486 | */ |
487 | while (ptable_ok | |
5fc1f312 | 488 | && ptable->offset != PARTITIONTABLE_END_MARKER |
51533b61 | 489 | && ptable < max_addr |
5fc1f312 | 490 | && pidx < MAX_PARTITIONS - 1) { |
51533b61 | 491 | |
5fc1f312 JN |
492 | axis_partitions[pidx].offset = offset + ptable->offset; |
493 | #ifdef CONFIG_ETRAX_NANDFLASH | |
494 | if (main_mtd->type == MTD_NANDFLASH) { | |
495 | axis_partitions[pidx].size = | |
496 | (((ptable+1)->offset == | |
497 | PARTITIONTABLE_END_MARKER) ? | |
498 | main_mtd->size : | |
499 | ((ptable+1)->offset + offset)) - | |
500 | (ptable->offset + offset); | |
501 | ||
502 | } else | |
503 | #endif /* CONFIG_ETRAX_NANDFLASH */ | |
504 | axis_partitions[pidx].size = ptable->size; | |
505 | #ifdef CONFIG_ETRAX_NANDBOOT | |
506 | /* Save partition number of jffs2 ro partition. | |
507 | * Needed if RAM booting or root file system in RAM. | |
508 | */ | |
509 | if (!nand_boot && | |
510 | ram_rootfs_partition < 0 && /* not already set */ | |
511 | ptable->type == PARTITION_TYPE_JFFS2 && | |
512 | (ptable->flags & PARTITION_FLAGS_READONLY_MASK) == | |
513 | PARTITION_FLAGS_READONLY) | |
514 | ram_rootfs_partition = pidx; | |
515 | #endif /* CONFIG_ETRAX_NANDBOOT */ | |
51533b61 MS |
516 | pidx++; |
517 | ptable++; | |
518 | } | |
51533b61 MS |
519 | } |
520 | ||
5fc1f312 JN |
521 | /* Decide whether to use default partition table. */ |
522 | /* Only use default table if we actually have a device (main_mtd) */ | |
51533b61 | 523 | |
5fc1f312 JN |
524 | struct mtd_partition *partition = &axis_partitions[0]; |
525 | if (main_mtd && !ptable_ok) { | |
526 | memcpy(axis_partitions, axis_default_partitions, | |
527 | sizeof(axis_default_partitions)); | |
528 | pidx = NUM_DEFAULT_PARTITIONS; | |
529 | ram_rootfs_partition = DEFAULT_ROOTFS_PARTITION_NO; | |
530 | } | |
51533b61 | 531 | |
5fc1f312 JN |
532 | /* Add artificial partitions for rootfs if necessary */ |
533 | if (romfs_in_flash) { | |
534 | /* rootfs is in directly accessible flash memory = NOR flash. | |
535 | Add an overlapping device for the rootfs partition. */ | |
536 | printk(KERN_INFO "axisflashmap: Adding partition for " | |
537 | "overlapping root file system image\n"); | |
538 | axis_partitions[pidx].size = romfs_length; | |
539 | axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR; | |
540 | axis_partitions[pidx].name = "romfs"; | |
51533b61 | 541 | axis_partitions[pidx].mask_flags |= MTD_WRITEABLE; |
5fc1f312 | 542 | ram_rootfs_partition = -1; |
51533b61 | 543 | pidx++; |
5fc1f312 JN |
544 | } else if (romfs_length && !nand_boot) { |
545 | /* romfs exists in memory, but not in flash, so must be in RAM. | |
546 | * Configure an MTDRAM partition. */ | |
547 | if (ram_rootfs_partition < 0) { | |
548 | /* None set yet, put it at the end */ | |
549 | ram_rootfs_partition = pidx; | |
550 | pidx++; | |
51533b61 | 551 | } |
5fc1f312 JN |
552 | printk(KERN_INFO "axisflashmap: Adding partition for " |
553 | "root file system image in RAM\n"); | |
554 | axis_partitions[ram_rootfs_partition].size = romfs_length; | |
555 | axis_partitions[ram_rootfs_partition].offset = romfs_start; | |
556 | axis_partitions[ram_rootfs_partition].name = "romfs"; | |
557 | axis_partitions[ram_rootfs_partition].mask_flags |= | |
558 | MTD_WRITEABLE; | |
559 | } | |
51533b61 | 560 | |
5fc1f312 JN |
561 | #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE |
562 | if (main_mtd) { | |
563 | main_partition.size = main_mtd->size; | |
564 | err = add_mtd_partitions(main_mtd, &main_partition, 1); | |
565 | if (err) | |
566 | panic("axisflashmap: Could not initialize " | |
567 | "partition for whole main mtd device!\n"); | |
51533b61 | 568 | } |
51533b61 MS |
569 | #endif |
570 | ||
5fc1f312 JN |
571 | /* Now, register all partitions with mtd. |
572 | * We do this one at a time so we can slip in an MTDRAM device | |
573 | * in the proper place if required. */ | |
574 | ||
575 | for (part = 0; part < pidx; part++) { | |
576 | if (part == ram_rootfs_partition) { | |
577 | /* add MTDRAM partition here */ | |
578 | struct mtd_info *mtd_ram; | |
579 | ||
580 | mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL); | |
581 | if (!mtd_ram) | |
582 | panic("axisflashmap: Couldn't allocate memory " | |
583 | "for mtd_info!\n"); | |
584 | printk(KERN_INFO "axisflashmap: Adding RAM partition " | |
585 | "for rootfs image.\n"); | |
586 | err = mtdram_init_device(mtd_ram, | |
587 | (void *)partition[part].offset, | |
588 | partition[part].size, | |
589 | partition[part].name); | |
590 | if (err) | |
591 | panic("axisflashmap: Could not initialize " | |
592 | "MTD RAM device!\n"); | |
593 | /* JFFS2 likes to have an erasesize. Keep potential | |
594 | * JFFS2 rootfs happy by providing one. Since image | |
595 | * was most likely created for main mtd, use that | |
596 | * erasesize, if available. Otherwise, make a guess. */ | |
597 | mtd_ram->erasesize = (main_mtd ? main_mtd->erasesize : | |
598 | CONFIG_ETRAX_PTABLE_SECTOR); | |
599 | } else { | |
600 | err = add_mtd_partitions(main_mtd, &partition[part], 1); | |
601 | if (err) | |
602 | panic("axisflashmap: Could not add mtd " | |
603 | "partition %d\n", part); | |
604 | } | |
605 | } | |
606 | #endif /* CONFIG_EXTRAX_VCS_SIM */ | |
607 | ||
608 | #ifdef CONFIG_ETRAX_VCS_SIM | |
609 | /* For simulator, always use a RAM partition. | |
610 | * The rootfs will be found after the kernel in RAM, | |
611 | * with romfs_start and romfs_end indicating location and size. | |
612 | */ | |
613 | struct mtd_info *mtd_ram; | |
614 | ||
615 | mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL); | |
616 | if (!mtd_ram) { | |
617 | panic("axisflashmap: Couldn't allocate memory for " | |
618 | "mtd_info!\n"); | |
619 | } | |
51533b61 | 620 | |
5fc1f312 JN |
621 | printk(KERN_INFO "axisflashmap: Adding RAM partition for romfs, " |
622 | "at %u, size %u\n", | |
623 | (unsigned) romfs_start, (unsigned) romfs_length); | |
51533b61 | 624 | |
5fc1f312 JN |
625 | err = mtdram_init_device(mtd_ram, (void *)romfs_start, |
626 | romfs_length, "romfs"); | |
627 | if (err) { | |
628 | panic("axisflashmap: Could not initialize MTD RAM " | |
629 | "device!\n"); | |
630 | } | |
631 | #endif /* CONFIG_EXTRAX_VCS_SIM */ | |
51533b61 | 632 | |
5fc1f312 JN |
633 | #ifndef CONFIG_ETRAX_VCS_SIM |
634 | if (aux_mtd) { | |
635 | aux_partition.size = aux_mtd->size; | |
636 | err = add_mtd_partitions(aux_mtd, &aux_partition, 1); | |
637 | if (err) | |
638 | panic("axisflashmap: Could not initialize " | |
639 | "aux mtd device!\n"); | |
51533b61 | 640 | |
51533b61 | 641 | } |
5fc1f312 | 642 | #endif /* CONFIG_EXTRAX_VCS_SIM */ |
51533b61 MS |
643 | |
644 | return err; | |
645 | } | |
646 | ||
647 | /* This adds the above to the kernels init-call chain. */ | |
648 | module_init(init_axis_flash); | |
649 | ||
650 | EXPORT_SYMBOL(axisflash_mtd); |