/* * Copyright (C) 2006 Texas Instruments. * Copyright (C) 2007 AVM GmbH * * ---------------------------------------------------------------------------- * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * ---------------------------------------------------------------------------- * */ /************************************************************************** * Included Files **************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #define DO_MTD #include #include #include #include #include #include #include #include #include #include /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ /*--- #define ATH_MTD_DEBUG ---*/ /*--- #define DUMMY_MAGIC(pos, end_pos) (pos > (end_pos / 2)) ---*/ #define DUMMY_MAGIC(pos, end_pos) 0 #if defined(ATH_MTD_DEBUG) #define DEBUG_MTD(fmt, arg...) printk(KERN_ERR "[%s/%d] " fmt "\n", __func__, __LINE__, ##arg); #else #define DEBUG_MTD(fmt, arg...) #endif /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct _nmi_vector_location { unsigned int firmware_length; unsigned int vector_gap; char vector_id[32]; }; struct _nmi_vector_location *nmi_vector_location = (struct _nmi_vector_location *)0xbfc00040; extern void set_nmi_vetor_gap(unsigned int start, unsigned int firmware_size, unsigned int gap_size); /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ #define MAX_FLASH_MTD 6 #define MAX_NAND_MTD 8 #define MAX_RAM_MTD 3 #define MAX_NAND_SPI_MTD (MAX_NAND_MTD - 2) /*--- URLADER und TFFS3 entfallen ---*/ #define JFFS2_MIN_SIZE 6 #define JFFS2_MAX_SIZE 16 static struct mtd_partition ath_partitions[MAX_FLASH_MTD]; /*-------------------------------------------------------------------------------------*\ * Zuerst wird das JFFS2 gesucht, dann das Squash-FS! \*-------------------------------------------------------------------------------------*/ static const char *probes[] = { "avm_jffs2", "avm_squashfs" , NULL }; static const char *ram_probes[] = {"avm_squashfs" , NULL }; static const char *nand_probes[] = { "avm_nand", NULL }; static const char *dummy_probes[] = { "avm_spi", NULL }; static unsigned int my_atoi(char *p); static unsigned int flash_erase_block_size = 64 << 10; extern int __init root_dev_setup(char *line); static struct ar7240_flash_data ath_flash_data = { .flash_size = 16, .parts = ath_partitions, .nr_parts = 0, .probes = probes }; /* NOTE: CFI probe will correctly detect flash part as 32M, but EMIF * limits addresses to 16M, so using addresses past 16M will wrap */ static struct resource ath_flash_resource[2] = { { .start = 0x1f000000, .end = 0x1f000000 + (16 << 20) - 1, /* 16 MB */ .flags = IORESOURCE_MEM, }, { /* für ins RAM geladenes Filesystem */ .start = 0x00000000, .end = 0x00000000, .flags = IORESOURCE_MEM, /*--- .parent = &iomem_resource ---*/ } }; void ath_ram_mtd_set_rw(struct device *pdev, int); struct platform_device ath_flash_device = { .name = "ath-nor", .id = 0, .dev = { .platform_data = &ath_flash_data, }, .num_resources = 1, .resource = &ath_flash_resource[0], }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static unsigned int ath_use_mtdram; static struct mtd_partition ath_ram_partitions[MAX_RAM_MTD]; static struct platdata_mtd_ram ath_ram_data = { .mapname = "ram-filesystem", .bankwidth = 4, .partitions = ath_ram_partitions, .nr_partitions = 0, // Dieser Wert muss 0 sein, denn sonst wirft er den Parser nicht an. .set_rw = ath_ram_mtd_set_rw, .probes = ram_probes }; struct platform_device ath_ram_device = { .name = "mtd-ram", .id = -1, .dev = { .platform_data = &ath_ram_data, }, .num_resources = 1, .resource = &ath_flash_resource[1], }; /*------------------------------------------------------------------------------------------*\ * NAND \*------------------------------------------------------------------------------------------*/ struct mtd_partition ath_nand_partitions[MAX_NAND_MTD]; unsigned long long ath_nand_flashsize; static loff_t tffs3_offset; static struct resource ath_nand_resource[] = { { .start = ATH_NAND_FLASH_BASE, .end = ATH_NAND_FLASH_BASE + 0x300, /*--- .end = 0 + (128 << 20), ---*/ /* 128 MB */ .flags = IORESOURCE_MEM, .parent = &nand_flash_resource }, }; static struct physmap_flash_data ath_nand_data = { .width = 2, .parts = ath_nand_partitions, .nr_parts = 0, .probes = nand_probes }; struct platform_device ath_nand_device = { .name = "ath-avmnand", .id = -1, .dev = { .platform_data = &ath_nand_data, }, .num_resources = 1, .resource = &ath_nand_resource[0], }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void ath_ram_mtd_set_rw(struct device *pdev, int mode) { if(mode == PLATRAM_RO) { printk(KERN_ERR "[ath_ram_mtd_set_rw] PLATRAM_RO\n"); } else if(mode == PLATRAM_RW) { printk(KERN_ERR "[ath_ram_mtd_set_rw] PLATRAM_RW\n"); } } enum _flash_map_enum { MAP_UNKNOWN, MAP_RAM, MAP_FLASH }; /*-------------------------------------------------------------------------------------*\ * * +---+---------------------+-----------------------+--------------------+ * | | Kernel | SquashFS | JFFS2 | * +---+---------------------+-----------------------+--------------------+ * A ^_pos E * * Zu Beginn ist das Layout obiges: * start_offset = A * MTD1 mit Kernel reicht von A bis E * MTD5 für JFFS2 kann gesetzt sein, wenn JFFS2 Parser vorher schon was gefunden hat * * Wenn SquashFS gefunden wird, wird MTD1 auf den Kernel verkleinert, * MTD0 für das FS wird von pos bis E angelegt * Wenn noch kein MTD5 mit JFFS2 existiert wird dieses innerhalb von MTD0 angelegt * \*-------------------------------------------------------------------------------------*/ static int ath_squashfs_parser_function(struct mtd_info *mtd, struct mtd_partition **p_mtd_pat, unsigned long param) { enum _flash_map_enum maptype = MAP_UNKNOWN; unsigned count = 1, maxcount = 0; printk("[%s] mtd_info->name %s mtd_info->index %u param=%lu p_mtd_pat=0x%p\n",__FUNCTION__, mtd->name, mtd->index, param, p_mtd_pat); if (!strcmp(mtd->name, "ram-filesystem")) { maptype = MAP_RAM; } else if (!strcmp(mtd->name, "ath-nor")) { maptype = MAP_FLASH; flash_erase_block_size = mtd->erasesize; } else { printk(KERN_WARNING "[%s] with unknown mtd type %s\n",__FUNCTION__, mtd->name); return 0; } if(p_mtd_pat) { unsigned int magic = 0, readlen = 0; loff_t pos, start_offset, end_offset; if(*p_mtd_pat) printk("[%s] *p_mtd_pat->name %s\n",__FUNCTION__, (*p_mtd_pat)->name); switch (maptype) { case MAP_FLASH: if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_partitions; } maxcount = ath_flash_data.nr_parts; break; case MAP_RAM: if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_ram_partitions; } maxcount = ARRAY_SIZE(ath_ram_partitions); break; default: break; } printk("[%s] try partition %s (offset 0x%lx len %lu blocksize=%x) read='%pF'\n", __FUNCTION__, (*p_mtd_pat)[count].name, (unsigned long)((*p_mtd_pat)[count].offset), (unsigned long)((*p_mtd_pat)[count].size), mtd->erasesize, mtd->read); start_offset = pos = (*p_mtd_pat)[count].offset; end_offset = (*p_mtd_pat)[1].offset + (*p_mtd_pat)[count].size; while( pos < end_offset ) { mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&magic); if(maptype == MAP_RAM) { /*--- printk("[%s] read %u bytes, magic = 0x%08x index %u pos 0x%x\n",__FUNCTION__, readlen, magic, mtd->index, (unsigned int)pos); ---*/ } if(magic == 0x73717368 || DUMMY_MAGIC(pos, end_offset)) { const char* hw_revision; (*p_mtd_pat)[0].offset = pos; (*p_mtd_pat)[0].size = (u_int32_t)start_offset + (u_int32_t)(*p_mtd_pat)[1].size - (u_int32_t)pos; if(maptype == MAP_RAM) { (*p_mtd_pat)[0].name = "rootfs_ram"; (*p_mtd_pat)[1].name = "kernel_ram"; } else { (*p_mtd_pat)[0].name = "rootfs"; (*p_mtd_pat)[1].name = "kernel"; } (*p_mtd_pat)[1].size = (u_int32_t)pos - (u_int32_t)start_offset; printk("[%s] magic found @pos 0x%x\n",__FUNCTION__, (unsigned int)pos); hw_revision = prom_getenv("HWRevision"); if ((hw_revision != NULL) && (strcmp(hw_revision, "180") == 0) && (maptype == MAP_FLASH) && (memcmp(ath_partitions[5].name, "jffs2", 4) == 0)) { /* JFFS2 vorhanden, auf 6810 prüfen ob verfügbarer Platz > 6 Blöcke und */ /* JFFS2 6 Blöcke groß. Dann haben wir eine Box mit fehlerhaft zu kleinem JFFS2. */ /* Dann neu anlegen mit min(JFFS2_MAX_SIZE, verfügb. Platz) */ u_int32_t jffs2_size, jffs2_earliest_start; struct squashfs_super_block squashfs_sb; mtd->read(mtd, (loff_t)pos, sizeof(struct squashfs_super_block), &readlen, (u_char*)&squashfs_sb); jffs2_earliest_start = (u_int32_t)pos + (u_int32_t)squashfs_sb.bytes_used; /*--- printk("squashfs pos: %x\n", (u_int32_t)pos); ---*/ /*--- printk("squashfs size: %x\n", (u_int32_t)squashfs_sb.bytes_used); ---*/ /*--- printk("jffs2_start (squashfs pos + len) = %x\n", (u_int32_t)jffs2_earliest_start); ---*/ if (jffs2_earliest_start & (mtd->erasesize-1)) { /*--- printk("align jffs: start: %x\n", jffs2_earliest_start); ---*/ jffs2_earliest_start = (jffs2_earliest_start & ~(mtd->erasesize-1)) + mtd->erasesize; } /*--- printk("jffs2_earliest_start (aligned) = %x\n", jffs2_earliest_start); ---*/ jffs2_size = ((*p_mtd_pat)[0].offset + (*p_mtd_pat)[0].size - jffs2_earliest_start) >> 16; /* jffs2_size in 64k Blöcken. Muss ggf. um 1 veringert werden für 128k Block Flash */ /*--- printk("jffs2_size = %x\n", jffs2_size); ---*/ jffs2_size = jffs2_size & ~((mtd->erasesize / 0x10000)-1); /*--- printk("jffs2_size = %x\n", jffs2_size); ---*/ if (jffs2_size > JFFS2_MAX_SIZE) jffs2_size = JFFS2_MAX_SIZE; if (jffs2_size >= (JFFS2_MIN_SIZE * (mtd->erasesize/0x10000))) { u_int32_t current_jffs2_size = ath_partitions[5].size >> 16; if ((current_jffs2_size == 6) && (jffs2_size > 6)) { printk("resize jffs2\n"); ath_partitions[5].name = (char *)"reserved"; } } } if ((maptype == MAP_FLASH) && (memcmp(ath_partitions[5].name, "jffs2", 4) != 0)) { /* JFFS2 nicht gefunden: Wenn jffs2_size gesetzt ist, ggf. verkleinern */ /* sonst anlegen mit der verbleibenden Flash Grösse nach Filesystem % 64k */ u_int32_t jffs2_size, jffs2_start, jffs2_earliest_start; struct squashfs_super_block squashfs_sb; mtd->read(mtd, (loff_t)pos, sizeof(struct squashfs_super_block), &readlen, (u_char*)&squashfs_sb); jffs2_earliest_start = (u_int32_t)pos + (u_int32_t)squashfs_sb.bytes_used; /*--- printk("squashfs pos: %x\n", (u_int32_t)pos); ---*/ /*--- printk("squashfs size: %x\n", (u_int32_t)squashfs_sb.bytes_used); ---*/ /*--- printk("jffs2_start (squashfs pos + len) = %x\n", (u_int32_t)jffs2_earliest_start); ---*/ if (jffs2_earliest_start & (mtd->erasesize-1)) { /*--- printk("align jffs: start: %x\n", jffs2_earliest_start); ---*/ jffs2_earliest_start = (jffs2_earliest_start & ~(mtd->erasesize-1)) + mtd->erasesize; } /*--- printk("jffs2_earliest_start (aligned) = %x\n", jffs2_earliest_start); ---*/ jffs2_size = ((*p_mtd_pat)[0].offset + (*p_mtd_pat)[0].size - jffs2_earliest_start) >> 16; /* jffs2_size in 64k Blöcken. Muss ggf. um 1 veringert werden für 128k Block Flash */ /*--- printk("jffs2_size = %x\n", jffs2_size); ---*/ jffs2_size = jffs2_size & ~((mtd->erasesize / 0x10000)-1); /*--- printk("jffs2_size = %x\n", jffs2_size); ---*/ if (jffs2_size < (JFFS2_MIN_SIZE * (mtd->erasesize/0x10000))) { printk(KERN_WARNING "[%s]: not enough space for JFFS2!\n",__FUNCTION__); } else { /*--- Größe auf JFFS2_MAX_SIZE begrenzen ---*/ if (jffs2_size > JFFS2_MAX_SIZE) { jffs2_start = jffs2_earliest_start + (jffs2_size - JFFS2_MAX_SIZE) * 0x10000; jffs2_size = JFFS2_MAX_SIZE; } else { jffs2_start = jffs2_earliest_start; } ath_flash_data.nr_parts += 1; /*--- eine Partition mehr ! ---*/ ath_partitions[5].offset = jffs2_start; ath_partitions[5].size = jffs2_size * 0x10000; ath_partitions[5].name = "jffs2"; printk("[%s] jffs2_start@%x size: %d\n",__FUNCTION__, jffs2_start, jffs2_size); { struct erase_info instr; int ret; /*--- printk("erasing jffs2\n"); ---*/ memset(&instr, 0, sizeof(instr)); instr.mtd = mtd; instr.addr = jffs2_start; instr.len = jffs2_size * 0x10000; instr.callback = NULL; instr.fail_addr = 0xffffffff; ret = mtd->erase(mtd, &instr); if (ret) { printk(KERN_ERR "jffs mtd erase failed %d\n", ret); } } } } return maxcount; } pos += 256; } } return maxcount; } #define JFFS_NODES ( JFFS2_NODETYPE_DIRENT | JFFS2_NODETYPE_INODE | JFFS2_NODETYPE_CLEANMARKER | JFFS2_NODETYPE_PADDING | JFFS2_NODETYPE_SUMMARY | JFFS2_NODETYPE_XATTR | JFFS2_NODETYPE_XREF) /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int ath_jffs2_parser_function(struct mtd_info *mtd, struct mtd_partition **p_mtd_pat, unsigned long param) { enum _flash_map_enum maptype = MAP_UNKNOWN; unsigned count = 1; printk("[%s] mtd_info->name %s mtd_info->index %u param=%lu p_mtd_pat=0x%p\n", __FUNCTION__, mtd->name, mtd->index, param, p_mtd_pat); if (!strcmp(mtd->name, "ram-filesystem")) { maptype = MAP_RAM; } else if (!strcmp(mtd->name, "ath-nor")) { maptype = MAP_FLASH; } else { printk(KERN_WARNING "[%s] with unknown mtd type %s\n",__FUNCTION__, mtd->name); return 0; } if(p_mtd_pat) { unsigned int magic = 0, readlen = 0; loff_t pos; if(*p_mtd_pat) printk("[%s] *p_mtd_pat->name %s\n",__FUNCTION__ , (*p_mtd_pat)->name); switch (maptype) { case MAP_FLASH: if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_partitions; } break; case MAP_RAM: count = 2; if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_ram_partitions; } /*--- return 0; ---*/ /* nicht im RAM suchen */ break; default: break; } printk("[%s] try partition %s (offset 0x%lx len %lu)\n", __FUNCTION__, (*p_mtd_pat)[count].name, (unsigned long)((*p_mtd_pat)[count].offset), (unsigned long)((*p_mtd_pat)[count].size)); pos = (*p_mtd_pat)[count].offset; while(pos < (*p_mtd_pat)[count].offset + (*p_mtd_pat)[count].size) { mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&magic); /*--- printk("[%s] read %u bytes, magic = 0x%08x index %u pos 0x%x\n",__FUNCTION__, readlen, magic, mtd->index, pos); ---*/ #ifdef __LITTLE_ENDIAN if ((((magic >> 16) & ~JFFS_NODES) == 0) && ((magic & 0xFFFF) == JFFS2_MAGIC_BITMASK)) { #else if (((magic >> 16) == JFFS2_MAGIC_BITMASK) && (((magic & 0xFFFF) & ~JFFS_NODES) == 0)) { #endif switch (maptype) { case MAP_FLASH: ath_flash_data.nr_parts += 1; /*--- eine Partition mehr ! ---*/ (*p_mtd_pat)[5].size = (*p_mtd_pat)[1].offset + (*p_mtd_pat)[1].size - pos; (*p_mtd_pat)[5].offset = pos; (*p_mtd_pat)[5].name = "jffs2"; /*--- printk("mtd1: size %d\n", (*p_mtd_pat)[1].size); ---*/ printk("[%s] magic %04x found @pos 0x%x, size %ld\n",__FUNCTION__, magic, (unsigned int)pos, (unsigned long)((*p_mtd_pat)[5].size)); break; case MAP_RAM: (*p_mtd_pat)[2].size = (*p_mtd_pat)[count].offset + (*p_mtd_pat)[count].size - pos; (*p_mtd_pat)[2].offset = pos; (*p_mtd_pat)[2].name = "ram-jffs2"; /*--- printk("mtd1: size %d\n", (*p_mtd_pat)[1].size); ---*/ printk("[%s] magic %04x found @pos 0x%x, size %ld\n",__FUNCTION__ , magic, (unsigned int)pos, (unsigned long)((*p_mtd_pat)[2].size)); break; default: break; } return 0; } pos += mtd->erasesize; } } return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int ath_spi_parser_function(struct mtd_info *mtd, struct mtd_partition **p_mtd_pat, unsigned long param) { printk("[%s] mtd_info->name %s mtd_info->index %u param=%lu p_mtd_pat=0x%p\n", __func__, mtd->name, mtd->index, param, p_mtd_pat); if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_partitions; } return ath_flash_data.nr_parts - 1; /*--- die letzte "reserved" Partition ist nicht dabei ---*/ } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int ath_nand_parser_function(struct mtd_info *mtd, struct mtd_partition **p_mtd_pat, unsigned long param) { unsigned long mtd_start, mtd_end; unsigned int config_size = 0, kernel_size = 0, filesystem_size = 0, tffs_size = 0, urlader_size = 0; char *p, *fs_start = prom_getenv("linux_fs_start"); printk("[%s] mtd_info->name %s mtd_info->index %u param=%lu p_mtd_pat=0x%p\n", __func__, mtd->name, mtd->index, param, p_mtd_pat); if ( ! strcmp(mtd->name, "ath-avmnand")) { if ( ! fs_start) fs_start = "0"; if(fs_start[0] == '1') { ath_nand_partitions[0].name = (char *)"reserved-kernel"; ath_nand_partitions[1].name = (char *)"reserved-filesystem"; ath_nand_partitions[2].name = (char *)"kernel"; ath_nand_partitions[3].name = (char *)"filesystem"; } else { ath_nand_partitions[0].name = (char *)"kernel"; ath_nand_partitions[1].name = (char *)"filesystem"; ath_nand_partitions[2].name = (char *)"reserved-kernel"; ath_nand_partitions[3].name = (char *)"reserved-filesystem"; } ath_nand_partitions[4].name = (char *)"config"; ath_nand_partitions[5].name = (char *)"nand-filesystem"; /*--------------------------------------------------------------------------------------*\ * Größen ermitteln \*--------------------------------------------------------------------------------------*/ p = prom_getenv("mtd0"); if(p) { DEBUG_MTD("mtd0 = %s", p); mtd_start = (unsigned int)simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; mtd_end = (unsigned int)simple_strtoul(p, NULL, 16); filesystem_size = mtd_end - mtd_start; ath_nand_partitions[1].size = filesystem_size; ath_nand_partitions[3].size = filesystem_size; } } p = prom_getenv("mtd1"); if(p) { DEBUG_MTD("mtd1 = %s", p); mtd_start = (unsigned int)simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; mtd_end = (unsigned int)simple_strtoul(p, NULL, 16); kernel_size = mtd_end - mtd_start; ath_nand_partitions[0].size = kernel_size; ath_nand_partitions[2].size = kernel_size; } } if (ath_nand_data.nr_parts > MAX_NAND_SPI_MTD) { /*--- kein SPI-Flash vorhanden ---*/ p = prom_getenv("mtd2"); if(p) { DEBUG_MTD("mtd2 = %s", p); simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; urlader_size = (unsigned int)simple_strtoul(p, NULL, 16); ath_nand_partitions[6].name = (char *)"urlader"; ath_nand_partitions[6].size = urlader_size; ath_nand_partitions[6].offset = 0; } } p = prom_getenv("mtd3"); if(p) { DEBUG_MTD("mtd3 = %s", p); mtd_start = (unsigned int)simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; mtd_end = (unsigned int)simple_strtoul(p, NULL, 16); tffs_size = mtd_end - mtd_start; ath_nand_partitions[7].name = (char *)"nand-tffs"; ath_nand_partitions[7].size = tffs_size; ath_nand_partitions[7].offset = ath_nand_partitions[6].size; tffs3_offset = ath_nand_partitions[7].offset; } } p = prom_getenv("mtd4"); if(p) { DEBUG_MTD("mtd4 = %s", p); mtd_start = (unsigned int)simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; mtd_end = (unsigned int)simple_strtoul(p, NULL, 16); config_size = mtd_end - mtd_start; } } } else { /*--- es gibt einen SPI-Flash, der Config-Bereich ist mtd5 ---*/ p = prom_getenv("mtd5"); if(p) { DEBUG_MTD("mtd5 = %s", p); mtd_start = (unsigned int)simple_strtoul(p, NULL, 16); p = strchr(p, ','); if(p) { p++; mtd_end = (unsigned int)simple_strtoul(p, NULL, 16); config_size = mtd_end - mtd_start; } } } /*--------------------------------------------------------------------------------------*\ * prüfen ob die einzelnen Teile in das Flash passen \*--------------------------------------------------------------------------------------*/ if(filesystem_size * 2 + kernel_size * 2 + urlader_size + config_size + tffs_size > ath_nand_flashsize) { panic("NAND device too small\n"); } /*--------------------------------------------------------------------------------------*\ * Groesen aufsetzen, wenn SPI-Flash vorhanden, ist ath_nand_partitions[7] = 0 \*--------------------------------------------------------------------------------------*/ ath_nand_flashsize -= (urlader_size + tffs_size); ath_nand_partitions[0].offset = ath_nand_partitions[7].offset + ath_nand_partitions[7].size; ath_nand_flashsize -= kernel_size; ath_nand_partitions[1].offset = ath_nand_partitions[0].offset + ath_nand_partitions[0].size; ath_nand_flashsize -= filesystem_size; ath_nand_partitions[2].offset = ath_nand_partitions[1].offset + ath_nand_partitions[1].size; ath_nand_flashsize -= kernel_size; ath_nand_partitions[3].offset = ath_nand_partitions[2].offset + ath_nand_partitions[2].size; ath_nand_flashsize -= filesystem_size; ath_nand_partitions[4].size = config_size; ath_nand_partitions[4].offset = ath_nand_partitions[3].offset + ath_nand_partitions[3].size; ath_nand_flashsize -= config_size; ath_nand_partitions[5].size = ath_nand_flashsize; /*--- der Rest ---*/ ath_nand_partitions[5].offset = ath_nand_partitions[4].offset + ath_nand_partitions[4].size; if(*p_mtd_pat == NULL) { *p_mtd_pat = ath_nand_partitions; } return ath_nand_data.nr_parts; } else printk(KERN_WARNING "[%s] with unknown mtd type %s\n", __func__, mtd->name); return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ unsigned int get_erase_block_size_on_ram_device(struct mtd_info *mtd) { unsigned int readlen = 0; unsigned int pos = 0; unsigned int value1, value2; mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&value1); if(readlen != sizeof(unsigned int)) return 0; /*--- printk("[get_erase_block_size_on_ram_device] name=%s pos=0x%x value=0x%x\n", mtd->name, pos, value1); ---*/ pos += 0x10000; mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&value2); if(readlen != sizeof(unsigned int)) return 0; /*--- printk("[get_erase_block_size_on_ram_device] name=%s pos=0x%x value2=0x%x\n", mtd->name, pos, value2); ---*/ if(value1 == value2) { pos += 0x10000; mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&value2); if(readlen != sizeof(unsigned int)) return 0; /*--- printk("[get_erase_block_size_on_ram_device] name=%s pos=0x%x value2=0x%x (check)\n", mtd->name, pos, value2); ---*/ if(value1 == value2) { printk("[get_erase_block_size_on_ram_device] eraseblocksize=0x10000\n"); return 0x10000; } return 0; } pos += 0x10000; mtd->read(mtd, (loff_t)pos, sizeof(unsigned int), &readlen, (u_char*)&value2); if(readlen != sizeof(unsigned int)) return 0; /*--- printk("[get_erase_block_size_on_ram_device] name=%s pos=0x%x value2=0x%x\n", mtd->name, pos, value2); ---*/ if(value1 == value2) { printk("[get_erase_block_size_on_ram_device] eraseblocksize=0x20000\n"); return 0x20000; } return 0; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct mtd_part_parser ath_squashfs_parser = { .name = "avm_squashfs", .parse_fn = ath_squashfs_parser_function }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct mtd_part_parser ath_jffs2_parser = { .name = "avm_jffs2", .parse_fn = ath_jffs2_parser_function }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct mtd_part_parser ath_nand_parser = { .name = "avm_nand", .parse_fn = ath_nand_parser_function }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ struct mtd_part_parser ath_spi_parser = { .name = "avm_spi", .parse_fn = ath_spi_parser_function }; /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static unsigned int my_atoi(char *p) { unsigned int base, zahl; /*--- printk("[my_atoi] %s -> ", p); ---*/ if(p[0] == '0') { if((p[1] == 'x') || (p[1] == 'X')) { base = 16; p += 2; } else { p += 1; base = 8; } } else { base = 10; } zahl = 0; while(*p) { if((*p >= '0') && (*p <= '9')) { zahl *= base; zahl += *p - '0'; } else if((*p >= 'A') && (*p <= 'F')) { zahl *= base; zahl += *p - 'A' + 10; } else if((*p >= 'a') && (*p <= 'f')) { zahl *= base; zahl += *p - 'a' + 10; } else { break; } p++; } /*--- printk(" %u(0x%x)\n", zahl, zahl); ---*/ return zahl; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int get_partition_index(struct mtd_info *mtd) { extern struct mtd_info *mtd_table[MAX_MTD_DEVICES]; unsigned int i; for(i = 0 ; i < MAX_MTD_DEVICES ; i++) { if(mtd_table[i] == mtd) { return i; } } return -1; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int found_rootfs_ram = 0; char *str_rootfs[] = { "rootfs_ram", "rootfs", "filesystem" }; struct mtd_info * ath_urlader_mtd; void ath_mtd_add_notifier(struct mtd_info *mtd) { extern int tffs_mtd[2]; extern struct mtd_info *mtd_table[MAX_MTD_DEVICES]; int i, index; if(!mtd->name) { DEBUG_MTD("Leeres MTD uebergeben!"); return; } DEBUG_MTD("name %s" , mtd->name); for (i = 0; i < sizeof(str_rootfs) / sizeof(char*) ; i++) { if (!strcmp(mtd->name, str_rootfs[i])) { if (found_rootfs_ram) { /*--- we found a rootfs in RAM and use only this ---*/ DEBUG_MTD("found %s but prefer rootfs from RAM", mtd->name); return; } else { DEBUG_MTD("found %s", mtd->name); } if (!strcmp(mtd->name, str_rootfs[0])) found_rootfs_ram = 1; /*--- signal that we found a rootfs in RAM ---*/ index = get_partition_index(mtd); DEBUG_MTD("use %s" , mtd->name); if (index >= 0) { static char root_device[64]; sprintf(root_device, "/dev/mtdblock%d", index); DEBUG_MTD("root device: %s (%s)" , root_device, mtd_table[index]->name); root_dev_setup(root_device); return; } } } if(!strcmp(mtd->name, "urlader")) { DEBUG_MTD("set urlader_mtd"); ath_urlader_mtd = mtd; #if defined(CONFIG_TFFS) } else if(!strcmp(mtd->name, "tffs (1)")) { index = get_partition_index(mtd); if (index >= 0) { tffs_mtd[0] = index; DEBUG_MTD("tffs (1) on Index %d", index); } } else if(!strcmp(mtd->name, "tffs (2)")) { index = get_partition_index(mtd); if (index >= 0) { tffs_mtd[1] = index; DEBUG_MTD("tffs (2) on Index %d", index); } #endif /*--- #if defined(CONFIG_TFFS) ---*/ #if defined(CONFIG_TFFS3) } else if(!strcmp(mtd->name, "nand-tffs")) { index = get_partition_index(mtd); if (index >= 0 && mtd->size > 0) { TFFS3_Register_NAND(index, tffs3_offset); DEBUG_MTD("tffs3 on Index %d, raw offset 0x%llx", index, tffs3_offset); } #endif /*--- #if defined(CONFIG_TFFS) ---*/ } else { DEBUG_MTD("skip %s" , mtd->name); } } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void ath_mtd_rm_notifier(struct mtd_info *mtd) { printk("[ath_mtd_rm_notifier] ignore %s\n", mtd->name); } struct mtd_notifier ath_mtd_notifier = { add: ath_mtd_add_notifier, remove: ath_mtd_rm_notifier }; /*------------------------------------------------------------------------------------------*\ * Parst die erste Größe in einem Größenangaben String vom Urlader * Format der Größenangaben: xxx_size={,KB,MB} \*------------------------------------------------------------------------------------------*/ unsigned long long parse_mtd_size(char *p) { unsigned long long size; if((p[0] == '0') && (p[1] == 'x')) { size = simple_strtoul(p, NULL, 16); } else { size = simple_strtoul(p, NULL, 10); } p = strchr(p, 'B'); if(p) { /*--- Die Größe enthält mindestens eine KB Angabe ---*/ if(p[-1] == 'K') { size *= 1024; } else if(p[-1] == 'M') { size *= (1024 * 1024); } } DEBUG_MTD("%Ld", size); return size; } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void find_nmi_vector(void) { unsigned int len; if(strcmp(nmi_vector_location->vector_id, "NMI Boot Vector")) { printk(KERN_ERR "[%s] no nmi vector found\n", __FUNCTION__); return; } len = (nmi_vector_location->firmware_length + flash_erase_block_size) & ~(flash_erase_block_size - 1); printk(KERN_ERR "[%s] nmi vector found. Firmware length 0x%x bytes (erase block align 0x%x) vector gap size 0x%x bytes.\n", __FUNCTION__, nmi_vector_location->firmware_length, len, nmi_vector_location->vector_gap); len += ath_partitions[2].size; /*--- urlader size ---*/ if (nmi_vector_location->vector_gap) { printk(KERN_ERR "[%s] add '%s' size 0x%llx to length\n", __FUNCTION__, ath_partitions[2].name, ath_partitions[2].size); set_nmi_vetor_gap(0xbfc00000, len, nmi_vector_location->vector_gap); } } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ void find_nmi_vector_gap(unsigned int base, unsigned int end) { unsigned int len; struct _nmi_vector_location *first_loc = (struct _nmi_vector_location *)(base + 0x40); struct _nmi_vector_location *last_loc = (struct _nmi_vector_location *)(base + 0xbe0040); while((unsigned long)first_loc <= (unsigned long)last_loc) { if((unsigned int)first_loc >= end) break; /*--- if(((unsigned long)first_loc > 0x87d95a00UL) && ((unsigned long)first_loc < 0x87d96000UL)) ---*/ /*--- printk(KERN_ERR "[NMI] %p => %10pB\n", first_loc, first_loc); ---*/ if(!strcmp(first_loc->vector_id, "NMI Boot Vector")) { len = end - ((unsigned int)first_loc - 0x40) + first_loc->vector_gap; printk(KERN_ERR "[%s] nmi vector found. Firmware length 0x%x bytes (move length 0x%x) vector gap size 0x%x bytes.\n", __FUNCTION__, first_loc->firmware_length, len, first_loc->vector_gap); memmove((void *)((unsigned int)first_loc - 0x40), (void *)((unsigned int)first_loc + first_loc->vector_gap - 0x40), len); return; } first_loc = (struct _nmi_vector_location *)((unsigned int)first_loc + 256); } printk(KERN_ERR "[%s] no nmi vector found at base %#x\n", __FUNCTION__, base); } /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ int __init ath_mtd_init(void) { register_mtd_parser(&ath_nand_parser); register_mtd_parser(&ath_squashfs_parser); register_mtd_parser(&ath_jffs2_parser); register_mtd_user(&ath_mtd_notifier); if ( ath_nand_flashsize ) { register_mtd_parser(&ath_spi_parser); ath_flash_data.probes = dummy_probes; /*--- keine Probes im SPI-Flash, nur urlader + tffs ---*/ printk("[%s] nand: type=%ld, start=%#x, end=%#x \n", __func__, ath_nand_device.resource->flags, ath_nand_device.resource->start, ath_nand_device.resource->end ); platform_device_register( &ath_nand_device ); } if ( ath_flash_data.nr_parts ) { ath_nand_data.nr_parts = MAX_NAND_SPI_MTD; printk("[%s] flash: type=%ld, start=%#x, end=%#x \n", __func__, ath_flash_device.resource->flags, ath_flash_device.resource->start, ath_flash_device.resource->end ); platform_device_register( &ath_flash_device ); } if ( ath_use_mtdram ) { printk("[%s] ram: type=%ld, start=%#x, end=%#x \n", __func__, ath_ram_device.resource->flags, ath_ram_device.resource->start, ath_ram_device.resource->end ); platform_device_register( &ath_ram_device ); } if ( ath_use_mtdram || ath_flash_data.nr_parts ) { find_nmi_vector(); } return 0; } subsys_initcall(ath_mtd_init); /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int __init mtdflash_setup(char *p) { unsigned long flashsize; unsigned long mtd_start, mtd_end; unsigned long flashoffset = 0; unsigned int mtd_index = 0; if(!p) return 0; flashsize = parse_mtd_size(p); if (flashsize == 0) return 0; /*--------------------------------------------------------------------------------------*\ * Größen ermitteln \*--------------------------------------------------------------------------------------*/ p = prom_getenv("mtd2"); if(p) { DEBUG_MTD("mtd2 = %s", p); flashoffset = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); } else { panic("no MTD2 found\n"); } ath_flash_resource[0].start = flashoffset; ath_flash_resource[0].end = flashoffset + flashsize; /*------------------------------------------------------------------------------------------*\ * wenn der SPI-Flash nur 1MB groß ist, ist kein KERNEL und kein Filesystem drin \*------------------------------------------------------------------------------------------*/ if (flashsize > parse_mtd_size("1024KB")) { p = prom_getenv("mtd0"); if(p) { DEBUG_MTD("mtd0 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16) - 1); ath_partitions[mtd_index].name = (char *)"filesystem"; ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; } } else { /*--- bei der 6810 gibt es u.U. kein mtd0 im Environment ---*/ ath_partitions[mtd_index].name = (char *)"filesystem"; ath_partitions[mtd_index].size = 0; ath_partitions[mtd_index].offset = 0; mtd_index++; } p = prom_getenv("mtd1"); if(p) { DEBUG_MTD("mtd1 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16) - 1); ath_partitions[mtd_index].name = (char *)"kernel"; ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; } } } p = prom_getenv("mtd2"); if(p) { DEBUG_MTD("mtd2 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16) - 1); ath_partitions[mtd_index].name = (char *)"urlader"; ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; printk(KERN_ERR "[%s] mtd_start 0x%lx mtd_end 0x%lx\n", __func__, mtd_start, mtd_end); } } p = prom_getenv("mtd3"); if(p) { DEBUG_MTD("mtd3 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16) - 1); ath_partitions[mtd_index].name = (char *)"tffs (1)"; ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; } } p = prom_getenv("mtd4"); if(p) { DEBUG_MTD("mtd4 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16) - 1); ath_partitions[mtd_index].name = (char *)"tffs (2)"; ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; } } ath_partitions[mtd_index].name = (char *)"reserved"; p = prom_getenv("mtd5"); if(p) { DEBUG_MTD("mtd5 = %s", p); mtd_start = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); p = strchr(p, ','); if(p) { p++; mtd_end = CPHYSADDR((unsigned int)simple_strtoul(p, NULL, 16)); ath_partitions[mtd_index].size = mtd_end - mtd_start + 1; ath_partitions[mtd_index].offset = mtd_start - flashoffset; mtd_index++; } } ath_flash_data.nr_parts = mtd_index; return 0; } __setup("sflash_size=", mtdflash_setup); /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int __init mtdram_setup(char *p) { char *start; DEBUG_MTD("[mtdram_setup] str=\"%s\"", p); if(p) { start = prom_getenv("linux_fs_start"); if(start && !strcmp(start, "nfs")) { DEBUG_MTD(KERN_ERR "dont use RAM filesystem, use NFS"); return 0; } DEBUG_MTD("[%s] mtdram1 %s", __FUNCTION__, p); ath_flash_resource[1].start = my_atoi(p); ath_flash_resource[1].start &= ~0xE0000000; ath_flash_resource[1].flags = IORESOURCE_MEM, p = strchr(p, ','); if(p) { p++; ath_flash_resource[1].end = my_atoi(p); ath_flash_resource[1].end &= ~0xE0000000; ath_flash_resource[1].end -= 1; } else { ath_flash_resource[1].start = 0; } DEBUG_MTD("[%s] mtdram1 0x%08x - 0x%08x", __FUNCTION__, ath_flash_resource[1].start, ath_flash_resource[1].end ); ath_ram_partitions[0].name = "rootfs_ram"; ath_ram_partitions[0].offset = 0; ath_ram_partitions[0].size = ath_flash_resource[1].end - ath_flash_resource[1].start + 1; ath_ram_partitions[0].mask_flags = MTD_ROM; ath_ram_partitions[1].name = "unused"; ath_ram_partitions[1].offset = 0; ath_ram_partitions[1].size = ath_flash_resource[1].end - ath_flash_resource[1].start + 1; ath_ram_partitions[1].mask_flags = MTD_ROM; ath_ram_partitions[2].name = "extra"; ath_ram_partitions[2].offset = 0; ath_ram_partitions[2].size = ath_flash_resource[1].end - ath_flash_resource[1].start + 1; ath_ram_partitions[2].mask_flags = MTD_ROM; } ath_use_mtdram = 1; find_nmi_vector_gap(ath_flash_resource[1].start | 0x80000000, ath_flash_resource[1].end | 0x80000000); return 0; } __setup("mtdram1=", mtdram_setup); /*------------------------------------------------------------------------------------------*\ \*------------------------------------------------------------------------------------------*/ static int __init mtdnand_setup(char *p) { if(!p) return 0; DEBUG_MTD("str=\"%s\"", p); ath_nand_flashsize = parse_mtd_size(p); return 0; } __setup("nand_size=", mtdnand_setup);