1/* 2 * Handle mapping of the flash memory access routines 3 * on TQM8xxL based devices. 4 * 5 * based on rpxlite.c 6 * 7 * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw> 8 * 9 * This code is GPLed 10 * 11 */ 12 13/* 14 * According to TQM8xxL hardware manual, TQM8xxL series have 15 * following flash memory organisations: 16 * | capacity | | chip type | | bank0 | | bank1 | 17 * 2MiB 512Kx16 2MiB 0 18 * 4MiB 1Mx16 4MiB 0 19 * 8MiB 1Mx16 4MiB 4MiB 20 * Thus, we choose CONFIG_MTD_CFI_I2 & CONFIG_MTD_CFI_B4 at 21 * kernel configuration. 22 */ 23#include <linux/module.h> 24#include <linux/types.h> 25#include <linux/kernel.h> 26#include <linux/init.h> 27#include <linux/slab.h> 28 29#include <linux/mtd/mtd.h> 30#include <linux/mtd/map.h> 31#include <linux/mtd/partitions.h> 32 33#include <asm/io.h> 34 35#define FLASH_ADDR 0x40000000 36#define FLASH_SIZE 0x00800000 37#define FLASH_BANK_MAX 4 38 39// trivial struct to describe partition information 40struct mtd_part_def 41{ 42 int nums; 43 unsigned char *type; 44 struct mtd_partition* mtd_part; 45}; 46 47//static struct mtd_info *mymtd; 48static struct mtd_info* mtd_banks[FLASH_BANK_MAX]; 49static struct map_info* map_banks[FLASH_BANK_MAX]; 50static struct mtd_part_def part_banks[FLASH_BANK_MAX]; 51static unsigned long num_banks; 52static void __iomem *start_scan_addr; 53 54/* 55 * Here are partition information for all known TQM8xxL series devices. 56 * See include/linux/mtd/partitions.h for definition of the mtd_partition 57 * structure. 58 * 59 * The *_max_flash_size is the maximum possible mapped flash size which 60 * is not necessarily the actual flash size. It must correspond to the 61 * value specified in the mapping definition defined by the 62 * "struct map_desc *_io_desc" for the corresponding machine. 63 */ 64 65#ifdef CONFIG_MTD_PARTITIONS 66/* Currently, TQM8xxL has upto 8MiB flash */ 67static unsigned long tqm8xxl_max_flash_size = 0x00800000; 68 69/* partition definition for first flash bank 70 * (cf. "drivers/char/flash_config.c") 71 */ 72static struct mtd_partition tqm8xxl_partitions[] = { 73 { 74 .name = "ppcboot", 75 .offset = 0x00000000, 76 .size = 0x00020000, /* 128KB */ 77 .mask_flags = MTD_WRITEABLE, /* force read-only */ 78 }, 79 { 80 .name = "kernel", /* default kernel image */ 81 .offset = 0x00020000, 82 .size = 0x000e0000, 83 .mask_flags = MTD_WRITEABLE, /* force read-only */ 84 }, 85 { 86 .name = "user", 87 .offset = 0x00100000, 88 .size = 0x00100000, 89 }, 90 { 91 .name = "initrd", 92 .offset = 0x00200000, 93 .size = 0x00200000, 94 } 95}; 96/* partition definition for second flash bank */ 97static struct mtd_partition tqm8xxl_fs_partitions[] = { 98 { 99 .name = "cramfs", 100 .offset = 0x00000000, 101 .size = 0x00200000, 102 }, 103 { 104 .name = "jffs", 105 .offset = 0x00200000, 106 .size = 0x00200000, 107 //.size = MTDPART_SIZ_FULL, 108 } 109}; 110#endif 111 112static int __init init_tqm_mtd(void) 113{ 114 int idx = 0, ret = 0; 115 unsigned long flash_addr, flash_size, mtd_size = 0; 116 /* pointer to TQM8xxL board info data */ 117 bd_t *bd = (bd_t *)__res; 118 119 flash_addr = bd->bi_flashstart; 120 flash_size = bd->bi_flashsize; 121 122 //request maximum flash size address space 123 start_scan_addr = ioremap(flash_addr, flash_size); 124 if (!start_scan_addr) { 125 printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __func__, flash_addr); 126 return -EIO; 127 } 128 129 for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) { 130 if(mtd_size >= flash_size) 131 break; 132 133 printk(KERN_INFO "%s: chip probing count %d\n", __func__, idx); 134 135 map_banks[idx] = kzalloc(sizeof(struct map_info), GFP_KERNEL); 136 if(map_banks[idx] == NULL) { 137 ret = -ENOMEM; 138 goto error_mem; 139 } 140 141 map_banks[idx]->name = (char *)kmalloc(16, GFP_KERNEL); 142 143 if (!map_banks[idx]->name) { 144 ret = -ENOMEM; 145 goto error_mem; 146 } 147 sprintf(map_banks[idx]->name, "TQM8xxL%d", idx); 148 149 map_banks[idx]->size = flash_size; 150 map_banks[idx]->bankwidth = 4; 151 152 simple_map_init(map_banks[idx]); 153 154 map_banks[idx]->virt = start_scan_addr; 155 map_banks[idx]->phys = flash_addr; 156 157 if (idx && mtd_banks[idx-1]) { 158 map_banks[idx]->virt += mtd_banks[idx-1]->size; 159 map_banks[idx]->phys += mtd_banks[idx-1]->size; 160 } 161 162 //start to probe flash chips 163 mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]); 164 165 if (mtd_banks[idx]) { 166 mtd_banks[idx]->owner = THIS_MODULE; 167 mtd_size += mtd_banks[idx]->size; 168 num_banks++; 169 170 printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __func__, num_banks, 171 mtd_banks[idx]->name, mtd_banks[idx]->size); 172 } 173 } 174 175 /* no supported flash chips found */ 176 if (!num_banks) { 177 printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n"); 178 ret = -ENXIO; 179 goto error_mem; 180 } 181 182#ifdef CONFIG_MTD_PARTITIONS 183 /* 184 * Select Static partition definitions 185 */ 186 part_banks[0].mtd_part = tqm8xxl_partitions; 187 part_banks[0].type = "Static image"; 188 part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions); 189 190 part_banks[1].mtd_part = tqm8xxl_fs_partitions; 191 part_banks[1].type = "Static file system"; 192 part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions); 193 194 for(idx = 0; idx < num_banks ; idx++) { 195 if (part_banks[idx].nums == 0) { 196 printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx); 197 add_mtd_device(mtd_banks[idx]); 198 } else { 199 printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n", 200 idx, part_banks[idx].type); 201 add_mtd_partitions(mtd_banks[idx], part_banks[idx].mtd_part, 202 part_banks[idx].nums); 203 } 204 } 205#else 206 printk(KERN_NOTICE "TQM flash: registering %d whole flash banks at once\n", num_banks); 207 for(idx = 0 ; idx < num_banks ; idx++) 208 add_mtd_device(mtd_banks[idx]); 209#endif 210 return 0; 211error_mem: 212 for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) { 213 if(map_banks[idx] != NULL) { 214 kfree(map_banks[idx]->name); 215 map_banks[idx]->name = NULL; 216 kfree(map_banks[idx]); 217 map_banks[idx] = NULL; 218 } 219 } 220error: 221 iounmap(start_scan_addr); 222 return ret; 223} 224 225static void __exit cleanup_tqm_mtd(void) 226{ 227 unsigned int idx = 0; 228 for(idx = 0 ; idx < num_banks ; idx++) { 229 /* destroy mtd_info previously allocated */ 230 if (mtd_banks[idx]) { 231 del_mtd_partitions(mtd_banks[idx]); 232 map_destroy(mtd_banks[idx]); 233 } 234 /* release map_info not used anymore */ 235 kfree(map_banks[idx]->name); 236 kfree(map_banks[idx]); 237 } 238 239 if (start_scan_addr) { 240 iounmap(start_scan_addr); 241 start_scan_addr = 0; 242 } 243} 244 245module_init(init_tqm_mtd); 246module_exit(cleanup_tqm_mtd); 247 248MODULE_LICENSE("GPL"); 249MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>"); 250MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards"); 251