Path: blob/master/arch/cris/arch-v10/drivers/axisflashmap.c
15126 views
/*1* Physical mapping layer for MTD using the Axis partitiontable format2*3* Copyright (c) 2001, 2002 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 <asm/axisflashmap.h>27#include <asm/mmu.h>28#include <arch/sv_addr_ag.h>2930#ifdef CONFIG_CRIS_LOW_MAP31#define FLASH_UNCACHED_ADDR KSEG_832#define FLASH_CACHED_ADDR KSEG_533#else34#define FLASH_UNCACHED_ADDR KSEG_E35#define FLASH_CACHED_ADDR KSEG_F36#endif3738#if CONFIG_ETRAX_FLASH_BUSWIDTH==139#define flash_data __u840#elif CONFIG_ETRAX_FLASH_BUSWIDTH==241#define flash_data __u1642#elif CONFIG_ETRAX_FLASH_BUSWIDTH==443#define flash_data __u3244#endif4546/* From head.S */47extern unsigned long romfs_start, romfs_length, romfs_in_flash;4849/* The master mtd for the entire flash. */50struct mtd_info* axisflash_mtd = NULL;5152/* Map driver functions. */5354static map_word flash_read(struct map_info *map, unsigned long ofs)55{56map_word tmp;57tmp.x[0] = *(flash_data *)(map->map_priv_1 + ofs);58return tmp;59}6061static void flash_copy_from(struct map_info *map, void *to,62unsigned long from, ssize_t len)63{64memcpy(to, (void *)(map->map_priv_1 + from), len);65}6667static void flash_write(struct map_info *map, map_word d, unsigned long adr)68{69*(flash_data *)(map->map_priv_1 + adr) = (flash_data)d.x[0];70}7172/*73* The map for chip select e0.74*75* We run into tricky coherence situations if we mix cached with uncached76* accesses to we only use the uncached version here.77*78* The size field is the total size where the flash chips may be mapped on the79* chip select. MTD probes should find all devices there and it does not matter80* if there are unmapped gaps or aliases (mirrors of flash devices). The MTD81* probes will ignore them.82*83* The start address in map_priv_1 is in virtual memory so we cannot use84* MEM_CSE0_START but must rely on that FLASH_UNCACHED_ADDR is the start85* address of cse0.86*/87static struct map_info map_cse0 = {88.name = "cse0",89.size = MEM_CSE0_SIZE,90.bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,91.read = flash_read,92.copy_from = flash_copy_from,93.write = flash_write,94.map_priv_1 = FLASH_UNCACHED_ADDR95};9697/*98* The map for chip select e1.99*100* If there was a gap between cse0 and cse1, map_priv_1 would get the wrong101* address, but there isn't.102*/103static struct map_info map_cse1 = {104.name = "cse1",105.size = MEM_CSE1_SIZE,106.bankwidth = CONFIG_ETRAX_FLASH_BUSWIDTH,107.read = flash_read,108.copy_from = flash_copy_from,109.write = flash_write,110.map_priv_1 = FLASH_UNCACHED_ADDR + MEM_CSE0_SIZE111};112113/* If no partition-table was found, we use this default-set. */114#define MAX_PARTITIONS 7115#define NUM_DEFAULT_PARTITIONS 3116117/*118* Default flash size is 2MB. CONFIG_ETRAX_PTABLE_SECTOR is most likely the119* size of one flash block and "filesystem"-partition needs 5 blocks to be able120* to use JFFS.121*/122static struct mtd_partition axis_default_partitions[NUM_DEFAULT_PARTITIONS] = {123{124.name = "boot firmware",125.size = CONFIG_ETRAX_PTABLE_SECTOR,126.offset = 0127},128{129.name = "kernel",130.size = 0x200000 - (6 * CONFIG_ETRAX_PTABLE_SECTOR),131.offset = CONFIG_ETRAX_PTABLE_SECTOR132},133{134.name = "filesystem",135.size = 5 * CONFIG_ETRAX_PTABLE_SECTOR,136.offset = 0x200000 - (5 * CONFIG_ETRAX_PTABLE_SECTOR)137}138};139140/* Initialize the ones normally used. */141static struct mtd_partition axis_partitions[MAX_PARTITIONS] = {142{143.name = "part0",144.size = CONFIG_ETRAX_PTABLE_SECTOR,145.offset = 0146},147{148.name = "part1",149.size = 0,150.offset = 0151},152{153.name = "part2",154.size = 0,155.offset = 0156},157{158.name = "part3",159.size = 0,160.offset = 0161},162{163.name = "part4",164.size = 0,165.offset = 0166},167{168.name = "part5",169.size = 0,170.offset = 0171},172{173.name = "part6",174.size = 0,175.offset = 0176},177};178179#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE180/* Main flash device */181static struct mtd_partition main_partition = {182.name = "main",183.size = 0,184.offset = 0185};186#endif187188/*189* Probe a chip select for AMD-compatible (JEDEC) or CFI-compatible flash190* chips in that order (because the amd_flash-driver is faster).191*/192static struct mtd_info *probe_cs(struct map_info *map_cs)193{194struct mtd_info *mtd_cs = NULL;195196printk(KERN_INFO197"%s: Probing a 0x%08lx bytes large window at 0x%08lx.\n",198map_cs->name, map_cs->size, map_cs->map_priv_1);199200#ifdef CONFIG_MTD_CFI201mtd_cs = do_map_probe("cfi_probe", map_cs);202#endif203#ifdef CONFIG_MTD_JEDECPROBE204if (!mtd_cs)205mtd_cs = do_map_probe("jedec_probe", map_cs);206#endif207208return mtd_cs;209}210211/*212* Probe each chip select individually for flash chips. If there are chips on213* both cse0 and cse1, the mtd_info structs will be concatenated to one struct214* so that MTD partitions can cross chip boundries.215*216* The only known restriction to how you can mount your chips is that each217* chip select must hold similar flash chips. But you need external hardware218* to do that anyway and you can put totally different chips on cse0 and cse1219* so it isn't really much of a restriction.220*/221static struct mtd_info *flash_probe(void)222{223struct mtd_info *mtd_cse0;224struct mtd_info *mtd_cse1;225struct mtd_info *mtd_cse;226227mtd_cse0 = probe_cs(&map_cse0);228mtd_cse1 = probe_cs(&map_cse1);229230if (!mtd_cse0 && !mtd_cse1) {231/* No chip found. */232return NULL;233}234235if (mtd_cse0 && mtd_cse1) {236struct mtd_info *mtds[] = { mtd_cse0, mtd_cse1 };237238/* Since the concatenation layer adds a small overhead we239* could try to figure out if the chips in cse0 and cse1 are240* identical and reprobe the whole cse0+cse1 window. But since241* flash chips are slow, the overhead is relatively small.242* So we use the MTD concatenation layer instead of further243* complicating the probing procedure.244*/245mtd_cse = mtd_concat_create(mtds, ARRAY_SIZE(mtds),246"cse0+cse1");247if (!mtd_cse) {248printk(KERN_ERR "%s and %s: Concatenation failed!\n",249map_cse0.name, map_cse1.name);250251/* The best we can do now is to only use what we found252* at cse0.253*/254mtd_cse = mtd_cse0;255map_destroy(mtd_cse1);256}257} else {258mtd_cse = mtd_cse0? mtd_cse0 : mtd_cse1;259}260261return mtd_cse;262}263264/*265* Probe the flash chip(s) and, if it succeeds, read the partition-table266* and register the partitions with MTD.267*/268static int __init init_axis_flash(void)269{270struct mtd_info *mymtd;271int err = 0;272int pidx = 0;273struct partitiontable_head *ptable_head = NULL;274struct partitiontable_entry *ptable;275int use_default_ptable = 1; /* Until proven otherwise. */276const char pmsg[] = " /dev/flash%d at 0x%08x, size 0x%08x\n";277278if (!(mymtd = flash_probe())) {279/* There's no reason to use this module if no flash chip can280* be identified. Make sure that's understood.281*/282printk(KERN_INFO "axisflashmap: Found no flash chip.\n");283} else {284printk(KERN_INFO "%s: 0x%08x bytes of flash memory.\n",285mymtd->name, mymtd->size);286axisflash_mtd = mymtd;287}288289if (mymtd) {290mymtd->owner = THIS_MODULE;291ptable_head = (struct partitiontable_head *)(FLASH_CACHED_ADDR +292CONFIG_ETRAX_PTABLE_SECTOR +293PARTITION_TABLE_OFFSET);294}295pidx++; /* First partition is always set to the default. */296297if (ptable_head && (ptable_head->magic == PARTITION_TABLE_MAGIC)298&& (ptable_head->size <299(MAX_PARTITIONS * sizeof(struct partitiontable_entry) +300PARTITIONTABLE_END_MARKER_SIZE))301&& (*(unsigned long*)((void*)ptable_head + sizeof(*ptable_head) +302ptable_head->size -303PARTITIONTABLE_END_MARKER_SIZE)304== PARTITIONTABLE_END_MARKER)) {305/* Looks like a start, sane length and end of a306* partition table, lets check csum etc.307*/308int ptable_ok = 0;309struct partitiontable_entry *max_addr =310(struct partitiontable_entry *)311((unsigned long)ptable_head + sizeof(*ptable_head) +312ptable_head->size);313unsigned long offset = CONFIG_ETRAX_PTABLE_SECTOR;314unsigned char *p;315unsigned long csum = 0;316317ptable = (struct partitiontable_entry *)318((unsigned long)ptable_head + sizeof(*ptable_head));319320/* Lets be PARANOID, and check the checksum. */321p = (unsigned char*) ptable;322323while (p <= (unsigned char*)max_addr) {324csum += *p++;325csum += *p++;326csum += *p++;327csum += *p++;328}329ptable_ok = (csum == ptable_head->checksum);330331/* Read the entries and use/show the info. */332printk(KERN_INFO " Found a%s partition table at 0x%p-0x%p.\n",333(ptable_ok ? " valid" : "n invalid"), ptable_head,334max_addr);335336/* We have found a working bootblock. Now read the337* partition table. Scan the table. It ends when338* there is 0xffffffff, that is, empty flash.339*/340while (ptable_ok341&& ptable->offset != 0xffffffff342&& ptable < max_addr343&& pidx < MAX_PARTITIONS) {344345axis_partitions[pidx].offset = offset + ptable->offset;346axis_partitions[pidx].size = ptable->size;347348printk(pmsg, pidx, axis_partitions[pidx].offset,349axis_partitions[pidx].size);350pidx++;351ptable++;352}353use_default_ptable = !ptable_ok;354}355356if (romfs_in_flash) {357/* Add an overlapping device for the root partition (romfs). */358359axis_partitions[pidx].name = "romfs";360axis_partitions[pidx].size = romfs_length;361axis_partitions[pidx].offset = romfs_start - FLASH_CACHED_ADDR;362axis_partitions[pidx].mask_flags |= MTD_WRITEABLE;363364printk(KERN_INFO365" Adding readonly flash partition for romfs image:\n");366printk(pmsg, pidx, axis_partitions[pidx].offset,367axis_partitions[pidx].size);368pidx++;369}370371#ifdef CONFIG_ETRAX_AXISFLASHMAP_MTD0WHOLE372if (mymtd) {373main_partition.size = mymtd->size;374err = mtd_device_register(mymtd, &main_partition, 1);375if (err)376panic("axisflashmap: Could not initialize "377"partition for whole main mtd device!\n");378}379#endif380381if (mymtd) {382if (use_default_ptable) {383printk(KERN_INFO " Using default partition table.\n");384err = mtd_device_register(mymtd,385axis_default_partitions,386NUM_DEFAULT_PARTITIONS);387} else {388err = mtd_device_register(mymtd, axis_partitions,389pidx);390}391392if (err)393panic("axisflashmap could not add MTD partitions!\n");394}395396if (!romfs_in_flash) {397/* Create an RAM device for the root partition (romfs). */398399#if !defined(CONFIG_MTD_MTDRAM) || (CONFIG_MTDRAM_TOTAL_SIZE != 0) || (CONFIG_MTDRAM_ABS_POS != 0)400/* No use trying to boot this kernel from RAM. Panic! */401printk(KERN_EMERG "axisflashmap: Cannot create an MTD RAM "402"device due to kernel (mis)configuration!\n");403panic("This kernel cannot boot from RAM!\n");404#else405struct mtd_info *mtd_ram;406407mtd_ram = kmalloc(sizeof(struct mtd_info), GFP_KERNEL);408if (!mtd_ram)409panic("axisflashmap couldn't allocate memory for "410"mtd_info!\n");411412printk(KERN_INFO " Adding RAM partition for romfs image:\n");413printk(pmsg, pidx, (unsigned)romfs_start,414(unsigned)romfs_length);415416err = mtdram_init_device(mtd_ram,417(void *)romfs_start,418romfs_length,419"romfs");420if (err)421panic("axisflashmap could not initialize MTD RAM "422"device!\n");423#endif424}425return err;426}427428/* This adds the above to the kernels init-call chain. */429module_init(init_axis_flash);430431EXPORT_SYMBOL(axisflash_mtd);432433434