Path: blob/master/arch/cris/arch-v32/drivers/axisflashmap.c
15126 views
/*1* Physical mapping layer for MTD using the Axis partitiontable format2*3* Copyright (c) 2001-2007 Axis Communications AB4*5* This file is under the GPL.6*7* First partition is always sector 0 regardless of if we find a partitiontable8* or not. In the start of the next sector, there can be a partitiontable that9* tells us what other partitions to define. If there isn't, we use a default10* partition split defined below.11*12*/1314#include <linux/module.h>15#include <linux/types.h>16#include <linux/kernel.h>17#include <linux/init.h>18#include <linux/slab.h>1920#include <linux/mtd/concat.h>21#include <linux/mtd/map.h>22#include <linux/mtd/mtd.h>23#include <linux/mtd/mtdram.h>24#include <linux/mtd/partitions.h>2526#include <linux/cramfs_fs.h>2728#include <asm/axisflashmap.h>29#include <asm/mmu.h>3031#define MEM_CSE0_SIZE (0x04000000)32#define MEM_CSE1_SIZE (0x04000000)3334#define FLASH_UNCACHED_ADDR KSEG_E35#define FLASH_CACHED_ADDR KSEG_F3637#define PAGESIZE (512)3839#if CONFIG_ETRAX_FLASH_BUSWIDTH==140#define flash_data __u841#elif CONFIG_ETRAX_FLASH_BUSWIDTH==242#define flash_data __u1643#elif CONFIG_ETRAX_FLASH_BUSWIDTH==444#define flash_data __u3245#endif4647/* From head.S */48extern unsigned long romfs_in_flash; /* 1 when romfs_start, _length in flash */49extern unsigned long romfs_start, romfs_length;50extern unsigned long nand_boot; /* 1 when booted from nand flash */5152struct partition_name {53char name[6];54};5556/* The master mtd for the entire flash. */57struct mtd_info* axisflash_mtd = NULL;5859/* Map driver functions. */6061static map_word flash_read(struct map_info *map, unsigned long ofs)62{63map_word tmp;64tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);65return tmp;66}6768static void flash_copy_from(struct map_info *map, void *to,69unsigned long from, ssize_t len)70{71memcpy(to, (void *)(map->map_priv_1 + from), len);72}7374static void flash_write(struct map_info *map, map_word d, unsigned long adr)75{76*(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];77}7879/*80* The map for chip select e0.81*82* We run into tricky coherence situations if we mix cached with uncached83* accesses to we only use the uncached version here.84*85* The size field is the total size where the flash chips may be mapped on the86* chip select. MTD probes should find all devices there and it does not matter87* if there are unmapped gaps or aliases (mirrors of flash devices). The MTD88* probes will ignore them.89*90* The start address in map_priv_1 is in virtual memory so we cannot use91* MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start92* address of cse0.93*/94static struct map_info map_cse0 = {95.name = "cse0",96.size = MEM_CSE0_SIZE,97.bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,98.read = flash_read,99.copy_from = flash_copy_from,100.write = flash_write,101.map_priv_1 = FLASH_UNCACHED_ADDR102};103104/*105* The map for chip select e1.106*107* If there was a gap between cse0 and cse1, map_priv_1 would get the wrong108* address, but there isn't.109*/110static struct map_info map_cse1 = {111.name = "cse1",112.size = MEM_CSE1_SIZE,113.bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,114.read = flash_read,115.copy_from = flash_copy_from,116.write = flash_write,117.map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE118};119120#define MAX_PARTITIONS 7121#ifdef CONFIG_ETRAX_NANDBOOT122#define NUM_DEFAULT_PARTITIONS 4123#define DEFAULT_ROOTFS_PARTITION_NO 2124#define DEFAULT_MEDIA_SIZE 0x2000000 /* 32 megs */125#else126#define NUM_DEFAULT_PARTITIONS 3127#define DEFAULT_ROOTFS_PARTITION_NO (-1)128#define DEFAULT_MEDIA_SIZE 0x800000 /* 8 megs */129#endif130131#if (MAX_PARTITIONS < NUM_DEFAULT_PARTITIONS)132#error MAX_PARTITIONS must be >= than NUM_DEFAULT_PARTITIONS133#endif134135/* Initialize the ones normally used. */136static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {137{138.name = "part0",139.size = CONFIG_ETRAX_PTABLE_SECTOR,140.offset = 0141},142{143.name = "part1",144.size = 0,145.offset = 0146},147{148.name = "part2",149.size = 0,150.offset = 0151},152{153.name = "part3",154.size = 0,155.offset = 0156},157{158.name = "part4",159.size = 0,160.offset = 0161},162{163.name = "part5",164.size = 0,165.offset = 0166},167{168.name = "part6",169.size = 0,170.offset = 0171},172};173174175/* If no partition-table was found, we use this default-set.176* Default flash size is 8MB (NOR). CONFIG_ETRAX_PTABLE_SECTOR is most177* likely the size of one flash block and "filesystem"-partition needs178* to be >=5 blocks to be able to use JFFS.179*/180static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {181{182.name = "boot firmware",183.size = CONFIG_ETRAX_PTABLE_SECTOR,184.offset = 0185},186{187.name = "kernel",188.size = 10 * CONFIG_ETRAX_PTABLE_SECTOR,189.offset = CONFIG_ETRAX_PTABLE_SECTOR190},191#define FILESYSTEM_SECTOR (11 * CONFIG_ETRAX_PTABLE_SECTOR)192#ifdef CONFIG_ETRAX_NANDBOOT193{194.name = "rootfs",195.size = 10 * CONFIG_ETRAX_PTABLE_SECTOR,196.offset = FILESYSTEM_SECTOR197},198#undef FILESYSTEM_SECTOR199#define FILESYSTEM_SECTOR (21 * CONFIG_ETRAX_PTABLE_SECTOR)200#endif201{202.name = "rwfs",203.size = DEFAULT_MEDIA_SIZE - FILESYSTEM_SECTOR,204.offset = FILESYSTEM_SECTOR205}206};207208#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE209/* Main flash device */210static struct mtd_partition main_partition = {211.name = "main",212.size = 0,213.offset = 0214};215#endif216217/* Auxiliary partition if we find another flash */218static struct mtd_partition aux_partition = {219.name = "aux",220.size = 0,221.offset = 0222};223224/*225* Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash226* chips in that order (because the amd_flash-driver is faster).227*/228static struct mtd_info *probe_cs(struct map_info *map_cs)229{230struct mtd_info *mtd_cs = NULL;231232printk(KERN_INFO233"%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",234map_cs->name, map_cs->size, map_cs->map_priv_1);235236#ifdef CONFIG_MTD_CFI237mtd_cs = do_map_probe("cfi_probe", map_cs);238#endif239#ifdef CONFIG_MTD_JEDECPROBE240if (!mtd_cs)241mtd_cs = do_map_probe("jedec_probe", map_cs);242#endif243244return mtd_cs;245}246247/*248* Probe each chip select individually for flash chips. If there are chips on249* both cse0 and cse1, the mtd_info structs will be concatenated to one struct250* so that MTD partitions can cross chip boundries.251*252* The only known restriction to how you can mount your chips is that each253* chip select must hold similar flash chips. But you need external hardware254* to do that anyway and you can put totally different chips on cse0 and cse1255* so it isn't really much of a restriction.256*/257extern struct mtd_info* __init crisv32_nand_flash_probe (void);258static struct mtd_info *flash_probe(void)259{260struct mtd_info *mtd_cse0;261struct mtd_info *mtd_cse1;262struct mtd_info *mtd_total;263struct mtd_info *mtds[2];264int count = 0;265266if ((mtd_cse0 = probe_cs(&map_cse0)) != NULL)267mtds[count++] = mtd_cse0;268if ((mtd_cse1 = probe_cs(&map_cse1)) != NULL)269mtds[count++] = mtd_cse1;270271if (!mtd_cse0 && !mtd_cse1) {272/* No chip found. */273return NULL;274}275276if (count > 1) {277/* Since the concatenation layer adds a small overhead we278* could try to figure out if the chips in cse0 and cse1 are279* identical and reprobe the whole cse0+cse1 window. But since280* flash chips are slow, the overhead is relatively small.281* So we use the MTD concatenation layer instead of further282* complicating the probing procedure.283*/284mtd_total = mtd_concat_create(mtds, count, "cse0+cse1");285if (!mtd_total) {286printk(KERN_ERR "%s and %s: Concatenation failed!\n",287map_cse0.name, map_cse1.name);288289/* The best we can do now is to only use what we found290* at cse0. */291mtd_total = mtd_cse0;292map_destroy(mtd_cse1);293}294} else295mtd_total = mtd_cse0 ? mtd_cse0 : mtd_cse1;296297return mtd_total;298}299300/*301* Probe the flash chip(s) and, if it succeeds, read the partition-table302* and register the partitions with MTD.303*/304static int __init init_axis_flash(void)305{306struct mtd_info *main_mtd;307struct mtd_info *aux_mtd = NULL;308int err = 0;309int pidx = 0;310struct partitiontable_head *ptable_head = NULL;311struct partitiontable_entry *ptable;312int ptable_ok = 0;313static char page[PAGESIZE];314size_t len;315int ram_rootfs_partition = -1; /* -1 => no RAM rootfs partition */316int part;317318/* We need a root fs. If it resides in RAM, we need to use an319* MTDRAM device, so it must be enabled in the kernel config,320* but its size must be configured as 0 so as not to conflict321* with our usage.322*/323#if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)324if (!romfs_in_flash && !nand_boot) {325printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "326"device; configure CONFIG_MTD_MTDRAM with size = 0!\n");327panic("This kernel cannot boot from RAM!\n");328}329#endif330331#ifndef CONFIG_ETRAX_VCS_SIM332main_mtd = flash_probe();333if (main_mtd)334printk(KERN_INFO "%s: 0x%08x bytes of NOR flash memory.\n",335main_mtd->name, main_mtd->size);336337#ifdef CONFIG_ETRAX_NANDFLASH338aux_mtd = crisv32_nand_flash_probe();339if (aux_mtd)340printk(KERN_INFO "%s: 0x%08x bytes of NAND flash memory.\n",341aux_mtd->name, aux_mtd->size);342343#ifdef CONFIG_ETRAX_NANDBOOT344{345struct mtd_info *tmp_mtd;346347printk(KERN_INFO "axisflashmap: Set to boot from NAND flash, "348"making NAND flash primary device.\n");349tmp_mtd = main_mtd;350main_mtd = aux_mtd;351aux_mtd = tmp_mtd;352}353#endif /* CONFIG_ETRAX_NANDBOOT */354#endif /* CONFIG_ETRAX_NANDFLASH */355356if (!main_mtd && !aux_mtd) {357/* There's no reason to use this module if no flash chip can358* be identified. Make sure that's understood.359*/360printk(KERN_INFO "axisflashmap: Found no flash chip.\n");361}362363#if 0 /* Dump flash memory so we can see what is going on */364if (main_mtd) {365int sectoraddr, i;366for (sectoraddr = 0; sectoraddr < 2*65536+4096;367sectoraddr += PAGESIZE) {368main_mtd->read(main_mtd, sectoraddr, PAGESIZE, &len,369page);370printk(KERN_INFO371"Sector at %d (length %d):\n",372sectoraddr, len);373for (i = 0; i < PAGESIZE; i += 16) {374printk(KERN_INFO375"%02x %02x %02x %02x "376"%02x %02x %02x %02x "377"%02x %02x %02x %02x "378"%02x %02x %02x %02x\n",379page[i] & 255, page[i+1] & 255,380page[i+2] & 255, page[i+3] & 255,381page[i+4] & 255, page[i+5] & 255,382page[i+6] & 255, page[i+7] & 255,383page[i+8] & 255, page[i+9] & 255,384page[i+10] & 255, page[i+11] & 255,385page[i+12] & 255, page[i+13] & 255,386page[i+14] & 255, page[i+15] & 255);387}388}389}390#endif391392if (main_mtd) {393main_mtd->owner = THIS_MODULE;394axisflash_mtd = main_mtd;395396loff_t ptable_sector = CONFIG_ETRAX_PTABLE_SECTOR;397398/* First partition (rescue) is always set to the default. */399pidx++;400#ifdef CONFIG_ETRAX_NANDBOOT401/* We know where the partition table should be located,402* it will be in first good block after that.403*/404int blockstat;405do {406blockstat = main_mtd->block_isbad(main_mtd,407ptable_sector);408if (blockstat < 0)409ptable_sector = 0; /* read error */410else if (blockstat)411ptable_sector += main_mtd->erasesize;412} while (blockstat && ptable_sector);413#endif414if (ptable_sector) {415main_mtd->read(main_mtd, ptable_sector, PAGESIZE,416&len, page);417ptable_head = &((struct partitiontable *) page)->head;418}419420#if 0 /* Dump partition table so we can see what is going on */421printk(KERN_INFO422"axisflashmap: flash read %d bytes at 0x%08x, data: "423"%02x %02x %02x %02x %02x %02x %02x %02x\n",424len, CONFIG_ETRAX_PTABLE_SECTOR,425page[0] & 255, page[1] & 255,426page[2] & 255, page[3] & 255,427page[4] & 255, page[5] & 255,428page[6] & 255, page[7] & 255);429printk(KERN_INFO430"axisflashmap: partition table offset %d, data: "431"%02x %02x %02x %02x %02x %02x %02x %02x\n",432PARTITION_TABLE_OFFSET,433page[PARTITION_TABLE_OFFSET+0] & 255,434page[PARTITION_TABLE_OFFSET+1] & 255,435page[PARTITION_TABLE_OFFSET+2] & 255,436page[PARTITION_TABLE_OFFSET+3] & 255,437page[PARTITION_TABLE_OFFSET+4] & 255,438page[PARTITION_TABLE_OFFSET+5] & 255,439page[PARTITION_TABLE_OFFSET+6] & 255,440page[PARTITION_TABLE_OFFSET+7] & 255);441#endif442}443444if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)445&& (ptable_head->size <446(MAX_PARTITIONS * sizeof(struct partitiontable_entry) +447PARTITIONTABLE_END_MARKER_SIZE))448&& (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +449ptable_head->size -450PARTITIONTABLE_END_MARKER_SIZE)451== PARTITIONTABLE_END_MARKER)) {452/* Looks like a start, sane length and end of a453* partition table, lets check csum etc.454*/455struct partitiontable_entry *max_addr =456(struct partitiontable_entry *)457((unsigned long)ptable_head + sizeof(*ptable_head) +458ptable_head->size);459unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;460unsigned char *p;461unsigned long csum = 0;462463ptable = (struct partitiontable_entry *)464((unsigned long)ptable_head + sizeof(*ptable_head));465466/* Lets be PARANOID, and check the checksum. */467p = (unsigned char*) ptable;468469while (p <= (unsigned char*)max_addr) {470csum += *p++;471csum += *p++;472csum += *p++;473csum += *p++;474}475ptable_ok = (csum == ptable_head->checksum);476477/* Read the entries and use/show the info. */478printk(KERN_INFO "axisflashmap: "479"Found a%s partition table at 0x%p-0x%p.\n",480(ptable_ok ? " valid" : "n invalid"), ptable_head,481max_addr);482483/* We have found a working bootblock. Now read the484* partition table. Scan the table. It ends with 0xffffffff.485*/486while (ptable_ok487&& ptable->offset != PARTITIONTABLE_END_MARKER488&& ptable < max_addr489&& pidx < MAX_PARTITIONS - 1) {490491axis_partitions[pidx].offset = offset + ptable->offset;492#ifdef CONFIG_ETRAX_NANDFLASH493if (main_mtd->type == MTD_NANDFLASH) {494axis_partitions[pidx].size =495(((ptable+1)->offset ==496PARTITIONTABLE_END_MARKER) ?497main_mtd->size :498((ptable+1)->offset + offset)) -499(ptable->offset + offset);500501} else502#endif /* CONFIG_ETRAX_NANDFLASH */503axis_partitions[pidx].size = ptable->size;504#ifdef CONFIG_ETRAX_NANDBOOT505/* Save partition number of jffs2 ro partition.506* Needed if RAM booting or root file system in RAM.507*/508if (!nand_boot &&509ram_rootfs_partition < 0 && /* not already set */510ptable->type == PARTITION_TYPE_JFFS2 &&511(ptable->flags & PARTITION_FLAGS_READONLY_MASK) ==512PARTITION_FLAGS_READONLY)513ram_rootfs_partition = pidx;514#endif /* CONFIG_ETRAX_NANDBOOT */515pidx++;516ptable++;517}518}519520/* Decide whether to use default partition table. */521/* Only use default table if we actually have a device (main_mtd) */522523struct mtd_partition *partition = &axis_partitions[0];524if (main_mtd && !ptable_ok) {525memcpy(axis_partitions, axis_default_partitions,526sizeof(axis_default_partitions));527pidx = NUM_DEFAULT_PARTITIONS;528ram_rootfs_partition = DEFAULT_ROOTFS_PARTITION_NO;529}530531/* Add artificial partitions for rootfs if necessary */532if (romfs_in_flash) {533/* rootfs is in directly accessible flash memory = NOR flash.534Add an overlapping device for the rootfs partition. */535printk(KERN_INFO "axisflashmap: Adding partition for "536"overlapping root file system image\n");537axis_partitions[pidx].size = romfs_length;538axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;539axis_partitions[pidx].name = "romfs";540axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;541ram_rootfs_partition = -1;542pidx++;543} else if (romfs_length && !nand_boot) {544/* romfs exists in memory, but not in flash, so must be in RAM.545* Configure an MTDRAM partition. */546if (ram_rootfs_partition < 0) {547/* None set yet, put it at the end */548ram_rootfs_partition = pidx;549pidx++;550}551printk(KERN_INFO "axisflashmap: Adding partition for "552"root file system image in RAM\n");553axis_partitions[ram_rootfs_partition].size = romfs_length;554axis_partitions[ram_rootfs_partition].offset = romfs_start;555axis_partitions[ram_rootfs_partition].name = "romfs";556axis_partitions[ram_rootfs_partition].mask_flags |=557MTD_WRITEABLE;558}559560#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE561if (main_mtd) {562main_partition.size = main_mtd->size;563err = mtd_device_register(main_mtd, &main_partition, 1);564if (err)565panic("axisflashmap: Could not initialize "566"partition for whole main mtd device!\n");567}568#endif569570/* Now, register all partitions with mtd.571* We do this one at a time so we can slip in an MTDRAM device572* in the proper place if required. */573574for (part = 0; part < pidx; part++) {575if (part == ram_rootfs_partition) {576/* add MTDRAM partition here */577struct mtd_info *mtd_ram;578579mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);580if (!mtd_ram)581panic("axisflashmap: Couldn't allocate memory "582"for mtd_info!\n");583printk(KERN_INFO "axisflashmap: Adding RAM partition "584"for rootfs image.\n");585err = mtdram_init_device(mtd_ram,586(void *)partition[part].offset,587partition[part].size,588partition[part].name);589if (err)590panic("axisflashmap: Could not initialize "591"MTD RAM device!\n");592/* JFFS2 likes to have an erasesize. Keep potential593* JFFS2 rootfs happy by providing one. Since image594* was most likely created for main mtd, use that595* erasesize, if available. Otherwise, make a guess. */596mtd_ram->erasesize = (main_mtd ? main_mtd->erasesize :597CONFIG_ETRAX_PTABLE_SECTOR);598} else {599err = mtd_device_register(main_mtd, &partition[part],6001);601if (err)602panic("axisflashmap: Could not add mtd "603"partition %d\n", part);604}605}606#endif /* CONFIG_EXTRAX_VCS_SIM */607608#ifdef CONFIG_ETRAX_VCS_SIM609/* 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*/613struct mtd_info *mtd_ram;614615mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);616if (!mtd_ram) {617panic("axisflashmap: Couldn't allocate memory for "618"mtd_info!\n");619}620621printk(KERN_INFO "axisflashmap: Adding RAM partition for romfs, "622"at %u, size %u\n",623(unsigned) romfs_start, (unsigned) romfs_length);624625err = mtdram_init_device(mtd_ram, (void *)romfs_start,626romfs_length, "romfs");627if (err) {628panic("axisflashmap: Could not initialize MTD RAM "629"device!\n");630}631#endif /* CONFIG_EXTRAX_VCS_SIM */632633#ifndef CONFIG_ETRAX_VCS_SIM634if (aux_mtd) {635aux_partition.size = aux_mtd->size;636err = mtd_device_register(aux_mtd, &aux_partition, 1);637if (err)638panic("axisflashmap: Could not initialize "639"aux mtd device!\n");640641}642#endif /* CONFIG_EXTRAX_VCS_SIM */643644return err;645}646647/* This adds the above to the kernels init-call chain. */648module_init(init_axis_flash);649650EXPORT_SYMBOL(axisflash_mtd);651652653