Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Physical mapping layer for MTD using the Axis partitiontable format |
| 3 | * |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 4 | * Copyright (c) 2001-2007 Axis Communications AB |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 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 | * |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/kernel.h> |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 18 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 19 | #include <linux/slab.h> |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 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 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 27 | #include <asm/axisflashmap.h> |
| 28 | #include <asm/mmu.h> |
| 29 | |
| 30 | #define MEM_CSE0_SIZE (0x04000000) |
| 31 | #define MEM_CSE1_SIZE (0x04000000) |
| 32 | |
| 33 | #define FLASH_UNCACHED_ADDR KSEG_E |
| 34 | #define FLASH_CACHED_ADDR KSEG_F |
| 35 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 36 | #define PAGESIZE (512) |
| 37 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 38 | #if CONFIG_ETRAX_FLASH_BUSWIDTH==1 |
| 39 | #define flash_data __u8 |
| 40 | #elif CONFIG_ETRAX_FLASH_BUSWIDTH==2 |
| 41 | #define flash_data __u16 |
| 42 | #elif CONFIG_ETRAX_FLASH_BUSWIDTH==4 |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 43 | #define flash_data __u32 |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 44 | #endif |
| 45 | |
| 46 | /* From head.S */ |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 47 | extern unsigned long romfs_in_flash; /* 1 when romfs_start, _length in flash */ |
| 48 | extern unsigned long romfs_start, romfs_length; |
| 49 | extern unsigned long nand_boot; /* 1 when booted from nand flash */ |
| 50 | |
| 51 | struct partition_name { |
| 52 | char name[6]; |
| 53 | }; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 54 | |
| 55 | /* The master mtd for the entire flash. */ |
| 56 | struct mtd_info* axisflash_mtd = NULL; |
| 57 | |
| 58 | /* Map driver functions. */ |
| 59 | |
| 60 | static map_word flash_read(struct map_info *map, unsigned long ofs) |
| 61 | { |
| 62 | map_word tmp; |
| 63 | tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs); |
| 64 | return tmp; |
| 65 | } |
| 66 | |
| 67 | static void flash_copy_from(struct map_info *map, void *to, |
| 68 | unsigned long from, ssize_t len) |
| 69 | { |
| 70 | memcpy(to, (void *)(map->map_priv_1 + from), len); |
| 71 | } |
| 72 | |
| 73 | static void flash_write(struct map_info *map, map_word d, unsigned long adr) |
| 74 | { |
| 75 | *(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0]; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * The map for chip select e0. |
| 80 | * |
| 81 | * We run into tricky coherence situations if we mix cached with uncached |
| 82 | * accesses to we only use the uncached version here. |
| 83 | * |
| 84 | * The size field is the total size where the flash chips may be mapped on the |
| 85 | * chip select. MTD probes should find all devices there and it does not matter |
| 86 | * if there are unmapped gaps or aliases (mirrors of flash devices). The MTD |
| 87 | * probes will ignore them. |
| 88 | * |
| 89 | * The start address in map_priv_1 is in virtual memory so we cannot use |
| 90 | * MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start |
| 91 | * address of cse0. |
| 92 | */ |
| 93 | static struct map_info map_cse0 = { |
| 94 | .name = "cse0", |
| 95 | .size = MEM_CSE0_SIZE, |
| 96 | .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH, |
| 97 | .read = flash_read, |
| 98 | .copy_from = flash_copy_from, |
| 99 | .write = flash_write, |
| 100 | .map_priv_1 = FLASH_UNCACHED_ADDR |
| 101 | }; |
| 102 | |
| 103 | /* |
| 104 | * The map for chip select e1. |
| 105 | * |
| 106 | * If there was a gap between cse0 and cse1, map_priv_1 would get the wrong |
| 107 | * address, but there isn't. |
| 108 | */ |
| 109 | static struct map_info map_cse1 = { |
| 110 | .name = "cse1", |
| 111 | .size = MEM_CSE1_SIZE, |
| 112 | .bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH, |
| 113 | .read = flash_read, |
| 114 | .copy_from = flash_copy_from, |
| 115 | .write = flash_write, |
| 116 | .map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE |
| 117 | }; |
| 118 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 119 | #define MAX_PARTITIONS 7 |
| 120 | #ifdef CONFIG_ETRAX_NANDBOOT |
| 121 | #define NUM_DEFAULT_PARTITIONS 4 |
| 122 | #define DEFAULT_ROOTFS_PARTITION_NO 2 |
| 123 | #define DEFAULT_MEDIA_SIZE 0x2000000 /* 32 megs */ |
| 124 | #else |
| 125 | #define NUM_DEFAULT_PARTITIONS 3 |
| 126 | #define DEFAULT_ROOTFS_PARTITION_NO (-1) |
| 127 | #define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */ |
| 128 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 129 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 130 | #if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS) |
| 131 | #error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS |
| 132 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 133 | |
| 134 | /* Initialize the ones normally used. */ |
| 135 | static struct mtd_partition axis_partitions[MAX_PARTITIONS] = { |
| 136 | { |
| 137 | .name = "part0", |
| 138 | .size = CONFIG_ETRAX_PTABLE_SECTOR, |
| 139 | .offset = 0 |
| 140 | }, |
| 141 | { |
| 142 | .name = "part1", |
| 143 | .size = 0, |
| 144 | .offset = 0 |
| 145 | }, |
| 146 | { |
| 147 | .name = "part2", |
| 148 | .size = 0, |
| 149 | .offset = 0 |
| 150 | }, |
| 151 | { |
| 152 | .name = "part3", |
| 153 | .size = 0, |
| 154 | .offset = 0 |
| 155 | }, |
| 156 | { |
| 157 | .name = "part4", |
| 158 | .size = 0, |
| 159 | .offset = 0 |
| 160 | }, |
| 161 | { |
| 162 | .name = "part5", |
| 163 | .size = 0, |
| 164 | .offset = 0 |
| 165 | }, |
| 166 | { |
| 167 | .name = "part6", |
| 168 | .size = 0, |
| 169 | .offset = 0 |
| 170 | }, |
| 171 | }; |
| 172 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 173 | |
| 174 | /* If no partition-table was found, we use this default-set. |
| 175 | * Default flash size is 8MB (NOR). CONFIG_ETRAX_PTABLE_SECTOR is most |
| 176 | * likely the size of one flash block and "filesystem"-partition needs |
| 177 | * to be >=5 blocks to be able to use JFFS. |
| 178 | */ |
| 179 | static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = { |
| 180 | { |
| 181 | .name = "boot firmware", |
| 182 | .size = CONFIG_ETRAX_PTABLE_SECTOR, |
| 183 | .offset = 0 |
| 184 | }, |
| 185 | { |
| 186 | .name = "kernel", |
| 187 | .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR, |
| 188 | .offset = CONFIG_ETRAX_PTABLE_SECTOR |
| 189 | }, |
| 190 | #define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR) |
| 191 | #ifdef CONFIG_ETRAX_NANDBOOT |
| 192 | { |
| 193 | .name = "rootfs", |
| 194 | .size = 10 * CONFIG_ETRAX_PTABLE_SECTOR, |
| 195 | .offset = FILESYSTEM_SECTOR |
| 196 | }, |
| 197 | #undef FILESYSTEM_SECTOR |
| 198 | #define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR) |
| 199 | #endif |
| 200 | { |
| 201 | .name = "rwfs", |
| 202 | .size = DEFAULT_MEDIA_SIZE - FILESYSTEM_SECTOR, |
| 203 | .offset = FILESYSTEM_SECTOR |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE |
| 208 | /* Main flash device */ |
| 209 | static struct mtd_partition main_partition = { |
| 210 | .name = "main", |
| 211 | .size = 0, |
| 212 | .offset = 0 |
| 213 | }; |
| 214 | #endif |
| 215 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 216 | /* Auxiliary partition if we find another flash */ |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 217 | static struct mtd_partition aux_partition = { |
| 218 | .name = "aux", |
| 219 | .size = 0, |
| 220 | .offset = 0 |
| 221 | }; |
| 222 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 223 | /* |
| 224 | * Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash |
| 225 | * chips in that order (because the amd_flash-driver is faster). |
| 226 | */ |
| 227 | static struct mtd_info *probe_cs(struct map_info *map_cs) |
| 228 | { |
| 229 | struct mtd_info *mtd_cs = NULL; |
| 230 | |
| 231 | printk(KERN_INFO |
| 232 | "%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n", |
| 233 | map_cs->name, map_cs->size, map_cs->map_priv_1); |
| 234 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 235 | #ifdef CONFIG_MTD_CFI |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 236 | mtd_cs = do_map_probe("cfi_probe", map_cs); |
Jesper Nilsson | 1b8be1d | 2007-11-14 17:01:20 -0800 | [diff] [blame] | 237 | #endif |
| 238 | #ifdef CONFIG_MTD_JEDECPROBE |
| 239 | if (!mtd_cs) |
| 240 | mtd_cs = do_map_probe("jedec_probe", map_cs); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 241 | #endif |
| 242 | |
| 243 | return mtd_cs; |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * Probe each chip select individually for flash chips. If there are chips on |
| 248 | * both cse0 and cse1, the mtd_info structs will be concatenated to one struct |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 249 | * so that MTD partitions can cross chip boundries. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 250 | * |
| 251 | * The only known restriction to how you can mount your chips is that each |
| 252 | * chip select must hold similar flash chips. But you need external hardware |
| 253 | * to do that anyway and you can put totally different chips on cse0 and cse1 |
| 254 | * so it isn't really much of a restriction. |
| 255 | */ |
| 256 | extern struct mtd_info* __init crisv32_nand_flash_probe (void); |
| 257 | static struct mtd_info *flash_probe(void) |
| 258 | { |
| 259 | struct mtd_info *mtd_cse0; |
| 260 | struct mtd_info *mtd_cse1; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 261 | struct mtd_info *mtd_total; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 262 | struct mtd_info *mtds[2]; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 263 | int count = 0; |
| 264 | |
| 265 | if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL) |
| 266 | mtds[count++] = mtd_cse0; |
| 267 | if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL) |
| 268 | mtds[count++] = mtd_cse1; |
| 269 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 270 | if (!mtd_cse0 && !mtd_cse1) { |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 271 | /* No chip found. */ |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | if (count > 1) { |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 276 | /* Since the concatenation layer adds a small overhead we |
| 277 | * could try to figure out if the chips in cse0 and cse1 are |
| 278 | * identical and reprobe the whole cse0+cse1 window. But since |
| 279 | * flash chips are slow, the overhead is relatively small. |
| 280 | * So we use the MTD concatenation layer instead of further |
| 281 | * complicating the probing procedure. |
| 282 | */ |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 283 | mtd_total = mtd_concat_create(mtds, count, "cse0+cse1"); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 284 | if (!mtd_total) { |
| 285 | printk(KERN_ERR "%s and %s: Concatenation failed!\n", |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 286 | map_cse0.name, map_cse1.name); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 287 | |
| 288 | /* The best we can do now is to only use what we found |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 289 | * at cse0. */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 290 | mtd_total = mtd_cse0; |
| 291 | map_destroy(mtd_cse1); |
| 292 | } |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 293 | } else |
| 294 | mtd_total = mtd_cse0 ? mtd_cse0 : mtd_cse1; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 295 | |
| 296 | return mtd_total; |
| 297 | } |
| 298 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 299 | /* |
| 300 | * Probe the flash chip(s) and, if it succeeds, read the partition-table |
| 301 | * and register the partitions with MTD. |
| 302 | */ |
| 303 | static int __init init_axis_flash(void) |
| 304 | { |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 305 | struct mtd_info *main_mtd; |
| 306 | struct mtd_info *aux_mtd = NULL; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 307 | int err = 0; |
| 308 | int pidx = 0; |
| 309 | struct partitiontable_head *ptable_head = NULL; |
| 310 | struct partitiontable_entry *ptable; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 311 | int ptable_ok = 0; |
| 312 | static char page[PAGESIZE]; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 313 | size_t len; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 314 | int ram_rootfs_partition = -1; /* -1 => no RAM rootfs partition */ |
| 315 | int part; |
Jesper Nilsson | 4b86715 | 2015-06-03 13:04:23 +0200 | [diff] [blame] | 316 | struct mtd_partition *partition; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 317 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 318 | /* We need a root fs. If it resides in RAM, we need to use an |
| 319 | * MTDRAM device, so it must be enabled in the kernel config, |
| 320 | * but its size must be configured as 0 so as not to conflict |
| 321 | * with our usage. |
| 322 | */ |
| 323 | #if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0) |
| 324 | if (!romfs_in_flash && !nand_boot) { |
| 325 | printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM " |
| 326 | "device; configure CONFIG_MTD_MTDRAM with size = 0!\n"); |
| 327 | panic("This kernel cannot boot from RAM!\n"); |
| 328 | } |
| 329 | #endif |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 330 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 331 | main_mtd = flash_probe(); |
| 332 | if (main_mtd) |
Jesper Nilsson | 4b86715 | 2015-06-03 13:04:23 +0200 | [diff] [blame] | 333 | printk(KERN_INFO "%s: 0x%08llx bytes of NOR flash memory.\n", |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 334 | main_mtd->name, main_mtd->size); |
| 335 | |
| 336 | #ifdef CONFIG_ETRAX_NANDFLASH |
| 337 | aux_mtd = crisv32_nand_flash_probe(); |
| 338 | if (aux_mtd) |
| 339 | printk(KERN_INFO "%s: 0x%08x bytes of NAND flash memory.\n", |
| 340 | aux_mtd->name, aux_mtd->size); |
| 341 | |
| 342 | #ifdef CONFIG_ETRAX_NANDBOOT |
| 343 | { |
| 344 | struct mtd_info *tmp_mtd; |
| 345 | |
| 346 | printk(KERN_INFO "axisflashmap: Set to boot from NAND flash, " |
| 347 | "making NAND flash primary device.\n"); |
| 348 | tmp_mtd = main_mtd; |
| 349 | main_mtd = aux_mtd; |
| 350 | aux_mtd = tmp_mtd; |
| 351 | } |
| 352 | #endif /* CONFIG_ETRAX_NANDBOOT */ |
| 353 | #endif /* CONFIG_ETRAX_NANDFLASH */ |
| 354 | |
| 355 | if (!main_mtd && !aux_mtd) { |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 356 | /* There's no reason to use this module if no flash chip can |
| 357 | * be identified. Make sure that's understood. |
| 358 | */ |
| 359 | printk(KERN_INFO "axisflashmap: Found no flash chip.\n"); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 362 | #if 0 /* Dump flash memory so we can see what is going on */ |
| 363 | if (main_mtd) { |
| 364 | int sectoraddr, i; |
| 365 | for (sectoraddr = 0; sectoraddr < 2*65536+4096; |
| 366 | sectoraddr += PAGESIZE) { |
| 367 | main_mtd->read(main_mtd, sectoraddr, PAGESIZE, &len, |
| 368 | page); |
| 369 | printk(KERN_INFO |
| 370 | "Sector at %d (length %d):\n", |
| 371 | sectoraddr, len); |
| 372 | for (i = 0; i < PAGESIZE; i += 16) { |
| 373 | printk(KERN_INFO |
| 374 | "%02x %02x %02x %02x " |
| 375 | "%02x %02x %02x %02x " |
| 376 | "%02x %02x %02x %02x " |
| 377 | "%02x %02x %02x %02x\n", |
| 378 | page[i] & 255, page[i+1] & 255, |
| 379 | page[i+2] & 255, page[i+3] & 255, |
| 380 | page[i+4] & 255, page[i+5] & 255, |
| 381 | page[i+6] & 255, page[i+7] & 255, |
| 382 | page[i+8] & 255, page[i+9] & 255, |
| 383 | page[i+10] & 255, page[i+11] & 255, |
| 384 | page[i+12] & 255, page[i+13] & 255, |
| 385 | page[i+14] & 255, page[i+15] & 255); |
| 386 | } |
| 387 | } |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 388 | } |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 389 | #endif |
| 390 | |
| 391 | if (main_mtd) { |
Jesper Nilsson | 4b86715 | 2015-06-03 13:04:23 +0200 | [diff] [blame] | 392 | loff_t ptable_sector = CONFIG_ETRAX_PTABLE_SECTOR; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 393 | main_mtd->owner = THIS_MODULE; |
| 394 | axisflash_mtd = main_mtd; |
| 395 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 396 | |
| 397 | /* First partition (rescue) is always set to the default. */ |
| 398 | pidx++; |
| 399 | #ifdef CONFIG_ETRAX_NANDBOOT |
| 400 | /* We know where the partition table should be located, |
| 401 | * it will be in first good block after that. |
| 402 | */ |
| 403 | int blockstat; |
| 404 | do { |
Artem Bityutskiy | 7086c19 | 2011-12-23 19:35:30 +0200 | [diff] [blame] | 405 | blockstat = mtd_block_isbad(main_mtd, ptable_sector); |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 406 | if (blockstat < 0) |
| 407 | ptable_sector = 0; /* read error */ |
| 408 | else if (blockstat) |
| 409 | ptable_sector += main_mtd->erasesize; |
| 410 | } while (blockstat && ptable_sector); |
| 411 | #endif |
| 412 | if (ptable_sector) { |
Artem Bityutskiy | 329ad39 | 2011-12-23 17:30:16 +0200 | [diff] [blame] | 413 | mtd_read(main_mtd, ptable_sector, PAGESIZE, &len, |
| 414 | page); |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 415 | ptable_head = &((struct partitiontable *) page)->head; |
| 416 | } |
| 417 | |
| 418 | #if 0 /* Dump partition table so we can see what is going on */ |
| 419 | printk(KERN_INFO |
| 420 | "axisflashmap: flash read %d bytes at 0x%08x, data: " |
| 421 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 422 | len, CONFIG_ETRAX_PTABLE_SECTOR, |
| 423 | page[0] & 255, page[1] & 255, |
| 424 | page[2] & 255, page[3] & 255, |
| 425 | page[4] & 255, page[5] & 255, |
| 426 | page[6] & 255, page[7] & 255); |
| 427 | printk(KERN_INFO |
| 428 | "axisflashmap: partition table offset %d, data: " |
| 429 | "%02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 430 | PARTITION_TABLE_OFFSET, |
| 431 | page[PARTITION_TABLE_OFFSET+0] & 255, |
| 432 | page[PARTITION_TABLE_OFFSET+1] & 255, |
| 433 | page[PARTITION_TABLE_OFFSET+2] & 255, |
| 434 | page[PARTITION_TABLE_OFFSET+3] & 255, |
| 435 | page[PARTITION_TABLE_OFFSET+4] & 255, |
| 436 | page[PARTITION_TABLE_OFFSET+5] & 255, |
| 437 | page[PARTITION_TABLE_OFFSET+6] & 255, |
| 438 | page[PARTITION_TABLE_OFFSET+7] & 255); |
| 439 | #endif |
| 440 | } |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 441 | |
| 442 | if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC) |
| 443 | && (ptable_head->size < |
| 444 | (MAX_PARTITIONS * sizeof(struct partitiontable_entry) + |
| 445 | PARTITIONTABLE_END_MARKER_SIZE)) |
| 446 | && (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) + |
| 447 | ptable_head->size - |
| 448 | PARTITIONTABLE_END_MARKER_SIZE) |
| 449 | == PARTITIONTABLE_END_MARKER)) { |
| 450 | /* Looks like a start, sane length and end of a |
| 451 | * partition table, lets check csum etc. |
| 452 | */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 453 | struct partitiontable_entry *max_addr = |
| 454 | (struct partitiontable_entry *) |
| 455 | ((unsigned long)ptable_head + sizeof(*ptable_head) + |
| 456 | ptable_head->size); |
| 457 | unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR; |
| 458 | unsigned char *p; |
| 459 | unsigned long csum = 0; |
| 460 | |
| 461 | ptable = (struct partitiontable_entry *) |
| 462 | ((unsigned long)ptable_head + sizeof(*ptable_head)); |
| 463 | |
| 464 | /* Lets be PARANOID, and check the checksum. */ |
| 465 | p = (unsigned char*) ptable; |
| 466 | |
| 467 | while (p <= (unsigned char*)max_addr) { |
| 468 | csum += *p++; |
| 469 | csum += *p++; |
| 470 | csum += *p++; |
| 471 | csum += *p++; |
| 472 | } |
| 473 | ptable_ok = (csum == ptable_head->checksum); |
| 474 | |
| 475 | /* Read the entries and use/show the info. */ |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 476 | printk(KERN_INFO "axisflashmap: " |
| 477 | "Found a%s partition table at 0x%p-0x%p.\n", |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 478 | (ptable_ok ? " valid" : "n invalid"), ptable_head, |
| 479 | max_addr); |
| 480 | |
| 481 | /* We have found a working bootblock. Now read the |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 482 | * partition table. Scan the table. It ends with 0xffffffff. |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 483 | */ |
| 484 | while (ptable_ok |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 485 | && ptable->offset != PARTITIONTABLE_END_MARKER |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 486 | && ptable < max_addr |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 487 | && pidx < MAX_PARTITIONS - 1) { |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 488 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 489 | axis_partitions[pidx].offset = offset + ptable->offset; |
| 490 | #ifdef CONFIG_ETRAX_NANDFLASH |
| 491 | if (main_mtd->type == MTD_NANDFLASH) { |
| 492 | axis_partitions[pidx].size = |
| 493 | (((ptable+1)->offset == |
| 494 | PARTITIONTABLE_END_MARKER) ? |
| 495 | main_mtd->size : |
| 496 | ((ptable+1)->offset + offset)) - |
| 497 | (ptable->offset + offset); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 498 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 499 | } else |
| 500 | #endif /* CONFIG_ETRAX_NANDFLASH */ |
| 501 | axis_partitions[pidx].size = ptable->size; |
| 502 | #ifdef CONFIG_ETRAX_NANDBOOT |
| 503 | /* Save partition number of jffs2 ro partition. |
| 504 | * Needed if RAM booting or root file system in RAM. |
| 505 | */ |
| 506 | if (!nand_boot && |
| 507 | ram_rootfs_partition < 0 && /* not already set */ |
| 508 | ptable->type == PARTITION_TYPE_JFFS2 && |
| 509 | (ptable->flags & PARTITION_FLAGS_READONLY_MASK) == |
| 510 | PARTITION_FLAGS_READONLY) |
| 511 | ram_rootfs_partition = pidx; |
| 512 | #endif /* CONFIG_ETRAX_NANDBOOT */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 513 | pidx++; |
| 514 | ptable++; |
| 515 | } |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 516 | } |
| 517 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 518 | /* Decide whether to use default partition table. */ |
| 519 | /* Only use default table if we actually have a device (main_mtd) */ |
| 520 | |
Jesper Nilsson | 4b86715 | 2015-06-03 13:04:23 +0200 | [diff] [blame] | 521 | partition = &axis_partitions[0]; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 522 | if (main_mtd && !ptable_ok) { |
| 523 | memcpy(axis_partitions, axis_default_partitions, |
| 524 | sizeof(axis_default_partitions)); |
| 525 | pidx = NUM_DEFAULT_PARTITIONS; |
| 526 | ram_rootfs_partition = DEFAULT_ROOTFS_PARTITION_NO; |
| 527 | } |
| 528 | |
| 529 | /* Add artificial partitions for rootfs if necessary */ |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 530 | if (romfs_in_flash) { |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 531 | /* rootfs is in directly accessible flash memory = NOR flash. |
| 532 | Add an overlapping device for the rootfs partition. */ |
| 533 | printk(KERN_INFO "axisflashmap: Adding partition for " |
| 534 | "overlapping root file system image\n"); |
| 535 | axis_partitions[pidx].size = romfs_length; |
| 536 | axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 537 | axis_partitions[pidx].name = "romfs"; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 538 | axis_partitions[pidx].mask_flags |= MTD_WRITEABLE; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 539 | ram_rootfs_partition = -1; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 540 | pidx++; |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 541 | } else if (romfs_length && !nand_boot) { |
| 542 | /* romfs exists in memory, but not in flash, so must be in RAM. |
| 543 | * Configure an MTDRAM partition. */ |
| 544 | if (ram_rootfs_partition < 0) { |
| 545 | /* None set yet, put it at the end */ |
| 546 | ram_rootfs_partition = pidx; |
| 547 | pidx++; |
| 548 | } |
| 549 | printk(KERN_INFO "axisflashmap: Adding partition for " |
| 550 | "root file system image in RAM\n"); |
| 551 | axis_partitions[ram_rootfs_partition].size = romfs_length; |
| 552 | axis_partitions[ram_rootfs_partition].offset = romfs_start; |
| 553 | axis_partitions[ram_rootfs_partition].name = "romfs"; |
| 554 | axis_partitions[ram_rootfs_partition].mask_flags |= |
| 555 | MTD_WRITEABLE; |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 558 | #ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE |
| 559 | if (main_mtd) { |
| 560 | main_partition.size = main_mtd->size; |
Jamie Iles | 36cda05 | 2011-05-23 10:22:53 +0100 | [diff] [blame] | 561 | err = mtd_device_register(main_mtd, &main_partition, 1); |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 562 | if (err) |
| 563 | panic("axisflashmap: Could not initialize " |
| 564 | "partition for whole main mtd device!\n"); |
| 565 | } |
| 566 | #endif |
| 567 | |
| 568 | /* Now, register all partitions with mtd. |
| 569 | * We do this one at a time so we can slip in an MTDRAM device |
| 570 | * in the proper place if required. */ |
| 571 | |
| 572 | for (part = 0; part < pidx; part++) { |
| 573 | if (part == ram_rootfs_partition) { |
| 574 | /* add MTDRAM partition here */ |
| 575 | struct mtd_info *mtd_ram; |
| 576 | |
| 577 | mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL); |
| 578 | if (!mtd_ram) |
| 579 | panic("axisflashmap: Couldn't allocate memory " |
| 580 | "for mtd_info!\n"); |
| 581 | printk(KERN_INFO "axisflashmap: Adding RAM partition " |
| 582 | "for rootfs image.\n"); |
| 583 | err = mtdram_init_device(mtd_ram, |
Jesper Nilsson | 4b86715 | 2015-06-03 13:04:23 +0200 | [diff] [blame] | 584 | (void *)(u_int32_t)partition[part].offset, |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 585 | partition[part].size, |
| 586 | partition[part].name); |
| 587 | if (err) |
| 588 | panic("axisflashmap: Could not initialize " |
| 589 | "MTD RAM device!\n"); |
| 590 | /* JFFS2 likes to have an erasesize. Keep potential |
| 591 | * JFFS2 rootfs happy by providing one. Since image |
| 592 | * was most likely created for main mtd, use that |
| 593 | * erasesize, if available. Otherwise, make a guess. */ |
| 594 | mtd_ram->erasesize = (main_mtd ? main_mtd->erasesize : |
| 595 | CONFIG_ETRAX_PTABLE_SECTOR); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 596 | } else { |
Jamie Iles | 36cda05 | 2011-05-23 10:22:53 +0100 | [diff] [blame] | 597 | err = mtd_device_register(main_mtd, &partition[part], |
| 598 | 1); |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 599 | if (err) |
| 600 | panic("axisflashmap: Could not add mtd " |
| 601 | "partition %d\n", part); |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 604 | |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 605 | if (aux_mtd) { |
| 606 | aux_partition.size = aux_mtd->size; |
Jamie Iles | 36cda05 | 2011-05-23 10:22:53 +0100 | [diff] [blame] | 607 | err = mtd_device_register(aux_mtd, &aux_partition, 1); |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 608 | if (err) |
| 609 | panic("axisflashmap: Could not initialize " |
| 610 | "aux mtd device!\n"); |
| 611 | |
| 612 | } |
Jesper Nilsson | 5fc1f31 | 2007-11-30 16:01:53 +0100 | [diff] [blame] | 613 | |
Mikael Starvik | 51533b6 | 2005-07-27 11:44:44 -0700 | [diff] [blame] | 614 | return err; |
| 615 | } |
| 616 | |
| 617 | /* This adds the above to the kernels init-call chain. */ |
| 618 | module_init(init_axis_flash); |
| 619 | |
| 620 | EXPORT_SYMBOL(axisflash_mtd); |