Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 2 | * Handle mapping of the flash memory access routines |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * on TQM8xxL based devices. |
| 4 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * based on rpxlite.c |
| 6 | * |
| 7 | * Copyright(C) 2001 Kirk Lee <kirk@hpc.ee.ntu.edu.tw> |
| 8 | * |
| 9 | * This code is GPLed |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 10 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 20 | * Thus, we choose CONFIG_MTD_CFI_I2 & CONFIG_MTD_CFI_B4 at |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | * kernel configuration. |
| 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/types.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 27 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #include <linux/mtd/mtd.h> |
| 30 | #include <linux/mtd/map.h> |
| 31 | #include <linux/mtd/partitions.h> |
| 32 | |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 33 | #include <asm/io.h> |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #define FLASH_ADDR 0x40000000 |
| 36 | #define FLASH_SIZE 0x00800000 |
| 37 | #define FLASH_BANK_MAX 4 |
| 38 | |
| 39 | // trivial struct to describe partition information |
| 40 | struct 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; |
| 48 | static struct mtd_info* mtd_banks[FLASH_BANK_MAX]; |
| 49 | static struct map_info* map_banks[FLASH_BANK_MAX]; |
| 50 | static struct mtd_part_def part_banks[FLASH_BANK_MAX]; |
| 51 | static unsigned long num_banks; |
| 52 | static 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. |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 58 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * The *_max_flash_size is the maximum possible mapped flash size which |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 60 | * is not necessarily the actual flash size. It must correspond to the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | * value specified in the mapping definition defined by the |
| 62 | * "struct map_desc *_io_desc" for the corresponding machine. |
| 63 | */ |
| 64 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 65 | /* Currently, TQM8xxL has up to 8MiB flash */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static unsigned long tqm8xxl_max_flash_size = 0x00800000; |
| 67 | |
| 68 | /* partition definition for first flash bank |
| 69 | * (cf. "drivers/char/flash_config.c") |
| 70 | */ |
| 71 | static struct mtd_partition tqm8xxl_partitions[] = { |
| 72 | { |
| 73 | .name = "ppcboot", |
| 74 | .offset = 0x00000000, |
| 75 | .size = 0x00020000, /* 128KB */ |
| 76 | .mask_flags = MTD_WRITEABLE, /* force read-only */ |
| 77 | }, |
| 78 | { |
| 79 | .name = "kernel", /* default kernel image */ |
| 80 | .offset = 0x00020000, |
| 81 | .size = 0x000e0000, |
| 82 | .mask_flags = MTD_WRITEABLE, /* force read-only */ |
| 83 | }, |
| 84 | { |
| 85 | .name = "user", |
| 86 | .offset = 0x00100000, |
| 87 | .size = 0x00100000, |
| 88 | }, |
| 89 | { |
| 90 | .name = "initrd", |
| 91 | .offset = 0x00200000, |
| 92 | .size = 0x00200000, |
| 93 | } |
| 94 | }; |
| 95 | /* partition definition for second flash bank */ |
| 96 | static struct mtd_partition tqm8xxl_fs_partitions[] = { |
| 97 | { |
| 98 | .name = "cramfs", |
| 99 | .offset = 0x00000000, |
| 100 | .size = 0x00200000, |
| 101 | }, |
| 102 | { |
| 103 | .name = "jffs", |
| 104 | .offset = 0x00200000, |
| 105 | .size = 0x00200000, |
| 106 | //.size = MTDPART_SIZ_FULL, |
| 107 | } |
| 108 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Dmitri Vorobiev | e30bb9c | 2008-11-25 02:55:08 +0200 | [diff] [blame] | 110 | static int __init init_tqm_mtd(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | { |
| 112 | int idx = 0, ret = 0; |
| 113 | unsigned long flash_addr, flash_size, mtd_size = 0; |
| 114 | /* pointer to TQM8xxL board info data */ |
| 115 | bd_t *bd = (bd_t *)__res; |
| 116 | |
| 117 | flash_addr = bd->bi_flashstart; |
| 118 | flash_size = bd->bi_flashsize; |
| 119 | |
| 120 | //request maximum flash size address space |
| 121 | start_scan_addr = ioremap(flash_addr, flash_size); |
| 122 | if (!start_scan_addr) { |
Harvey Harrison | cb53b3b | 2008-04-18 13:44:19 -0700 | [diff] [blame] | 123 | printk(KERN_WARNING "%s:Failed to ioremap address:0x%x\n", __func__, flash_addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | return -EIO; |
| 125 | } |
| 126 | |
| 127 | for (idx = 0 ; idx < FLASH_BANK_MAX ; idx++) { |
| 128 | if(mtd_size >= flash_size) |
| 129 | break; |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 130 | |
Harvey Harrison | cb53b3b | 2008-04-18 13:44:19 -0700 | [diff] [blame] | 131 | printk(KERN_INFO "%s: chip probing count %d\n", __func__, idx); |
Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 132 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 133 | map_banks[idx] = kzalloc(sizeof(struct map_info), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | if(map_banks[idx] == NULL) { |
| 135 | ret = -ENOMEM; |
| 136 | /* FIXME: What if some MTD devices were probed already? */ |
| 137 | goto error_mem; |
| 138 | } |
| 139 | |
Jesper Juhl | ce4a37f | 2010-11-09 00:09:02 +0100 | [diff] [blame] | 140 | map_banks[idx]->name = kmalloc(16, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | if (!map_banks[idx]->name) { |
| 143 | ret = -ENOMEM; |
| 144 | /* FIXME: What if some MTD devices were probed already? */ |
| 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 | /* FIXME: This looks utterly bogus, but I'm trying to |
| 157 | preserve the behaviour of the original (shown here)... |
| 158 | |
| 159 | map_banks[idx]->map_priv_1 = |
| 160 | start_scan_addr + ((idx > 0) ? |
| 161 | (mtd_banks[idx-1] ? mtd_banks[idx-1]->size : 0) : 0); |
| 162 | */ |
| 163 | |
| 164 | if (idx && mtd_banks[idx-1]) { |
| 165 | map_banks[idx]->virt += mtd_banks[idx-1]->size; |
| 166 | map_banks[idx]->phys += mtd_banks[idx-1]->size; |
| 167 | } |
| 168 | |
| 169 | //start to probe flash chips |
| 170 | mtd_banks[idx] = do_map_probe("cfi_probe", map_banks[idx]); |
| 171 | |
| 172 | if (mtd_banks[idx]) { |
| 173 | mtd_banks[idx]->owner = THIS_MODULE; |
| 174 | mtd_size += mtd_banks[idx]->size; |
| 175 | num_banks++; |
| 176 | |
Harvey Harrison | cb53b3b | 2008-04-18 13:44:19 -0700 | [diff] [blame] | 177 | printk(KERN_INFO "%s: bank%d, name:%s, size:%dbytes \n", __func__, num_banks, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | mtd_banks[idx]->name, mtd_banks[idx]->size); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | /* no supported flash chips found */ |
| 183 | if (!num_banks) { |
| 184 | printk(KERN_NOTICE "TQM8xxL: No support flash chips found!\n"); |
| 185 | ret = -ENXIO; |
| 186 | goto error_mem; |
| 187 | } |
| 188 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* |
| 190 | * Select Static partition definitions |
| 191 | */ |
| 192 | part_banks[0].mtd_part = tqm8xxl_partitions; |
| 193 | part_banks[0].type = "Static image"; |
| 194 | part_banks[0].nums = ARRAY_SIZE(tqm8xxl_partitions); |
| 195 | |
| 196 | part_banks[1].mtd_part = tqm8xxl_fs_partitions; |
| 197 | part_banks[1].type = "Static file system"; |
| 198 | part_banks[1].nums = ARRAY_SIZE(tqm8xxl_fs_partitions); |
| 199 | |
| 200 | for(idx = 0; idx < num_banks ; idx++) { |
Jamie Iles | b5c0a4e | 2011-05-23 10:23:11 +0100 | [diff] [blame] | 201 | if (part_banks[idx].nums == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | printk(KERN_NOTICE "TQM flash%d: no partition info available, registering whole flash at once\n", idx); |
Jamie Iles | b5c0a4e | 2011-05-23 10:23:11 +0100 | [diff] [blame] | 203 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | printk(KERN_NOTICE "TQM flash%d: Using %s partition definition\n", |
| 205 | idx, part_banks[idx].type); |
Jamie Iles | b5c0a4e | 2011-05-23 10:23:11 +0100 | [diff] [blame] | 206 | mtd_device_register(mtd_banks[idx], part_banks[idx].mtd_part, |
| 207 | part_banks[idx].nums); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | error_mem: |
| 211 | for(idx = 0 ; idx < FLASH_BANK_MAX ; idx++) { |
| 212 | if(map_banks[idx] != NULL) { |
Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 213 | kfree(map_banks[idx]->name); |
| 214 | map_banks[idx]->name = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | kfree(map_banks[idx]); |
| 216 | map_banks[idx] = NULL; |
| 217 | } |
| 218 | } |
| 219 | error: |
| 220 | iounmap(start_scan_addr); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | static void __exit cleanup_tqm_mtd(void) |
| 225 | { |
| 226 | unsigned int idx = 0; |
| 227 | for(idx = 0 ; idx < num_banks ; idx++) { |
| 228 | /* destroy mtd_info previously allocated */ |
| 229 | if (mtd_banks[idx]) { |
Jamie Iles | b5c0a4e | 2011-05-23 10:23:11 +0100 | [diff] [blame] | 230 | mtd_device_unregister(mtd_banks[idx]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | map_destroy(mtd_banks[idx]); |
| 232 | } |
| 233 | /* release map_info not used anymore */ |
| 234 | kfree(map_banks[idx]->name); |
| 235 | kfree(map_banks[idx]); |
| 236 | } |
| 237 | |
| 238 | if (start_scan_addr) { |
| 239 | iounmap(start_scan_addr); |
| 240 | start_scan_addr = 0; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | module_init(init_tqm_mtd); |
| 245 | module_exit(cleanup_tqm_mtd); |
| 246 | |
| 247 | MODULE_LICENSE("GPL"); |
| 248 | MODULE_AUTHOR("Kirk Lee <kirk@hpc.ee.ntu.edu.tw>"); |
| 249 | MODULE_DESCRIPTION("MTD map driver for TQM8xxL boards"); |