1/* 2 * Flash memory access on SA11x0 based devices 3 * 4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net> 5 */ 6#include <linux/module.h> 7#include <linux/types.h> 8#include <linux/ioport.h> 9#include <linux/kernel.h> 10#include <linux/init.h> 11#include <linux/errno.h> 12#include <linux/slab.h> 13#include <linux/platform_device.h> 14#include <linux/err.h> 15#include <linux/io.h> 16 17#include <linux/mtd/mtd.h> 18#include <linux/mtd/map.h> 19#include <linux/mtd/partitions.h> 20#include <linux/mtd/concat.h> 21 22#include <mach/hardware.h> 23#include <asm/sizes.h> 24#include <asm/mach/flash.h> 25 26 27struct sa_subdev_info { 28 char name[16]; 29 struct map_info map; 30 struct mtd_info *mtd; 31 struct flash_platform_data *plat; 32}; 33 34struct sa_info { 35 struct mtd_partition *parts; 36 struct mtd_info *mtd; 37 int num_subdev; 38 unsigned int nr_parts; 39 struct sa_subdev_info subdev[0]; 40}; 41 42static void sa1100_set_vpp(struct map_info *map, int on) 43{ 44 struct sa_subdev_info *subdev = container_of(map, struct sa_subdev_info, map); 45 subdev->plat->set_vpp(on); 46} 47 48static void sa1100_destroy_subdev(struct sa_subdev_info *subdev) 49{ 50 if (subdev->mtd) 51 map_destroy(subdev->mtd); 52 if (subdev->map.virt) 53 iounmap(subdev->map.virt); 54 release_mem_region(subdev->map.phys, subdev->map.size); 55} 56 57static int sa1100_probe_subdev(struct sa_subdev_info *subdev, struct resource *res) 58{ 59 unsigned long phys; 60 unsigned int size; 61 int ret; 62 63 phys = res->start; 64 size = res->end - phys + 1; 65 66 /* 67 * Retrieve the bankwidth from the MSC registers. 68 * We currently only implement CS0 and CS1 here. 69 */ 70 switch (phys) { 71 default: 72 printk(KERN_WARNING "SA1100 flash: unknown base address " 73 "0x%08lx, assuming CS0\n", phys); 74 75 case SA1100_CS0_PHYS: 76 subdev->map.bankwidth = (MSC0 & MSC_RBW) ? 2 : 4; 77 break; 78 79 case SA1100_CS1_PHYS: 80 subdev->map.bankwidth = ((MSC0 >> 16) & MSC_RBW) ? 2 : 4; 81 break; 82 } 83 84 if (!request_mem_region(phys, size, subdev->name)) { 85 ret = -EBUSY; 86 goto out; 87 } 88 89 if (subdev->plat->set_vpp) 90 subdev->map.set_vpp = sa1100_set_vpp; 91 92 subdev->map.phys = phys; 93 subdev->map.size = size; 94 subdev->map.virt = ioremap(phys, size); 95 if (!subdev->map.virt) { 96 ret = -ENOMEM; 97 goto err; 98 } 99 100 simple_map_init(&subdev->map); 101 102 /* 103 * Now let's probe for the actual flash. Do it here since 104 * specific machine settings might have been set above. 105 */ 106 subdev->mtd = do_map_probe(subdev->plat->map_name, &subdev->map); 107 if (subdev->mtd == NULL) { 108 ret = -ENXIO; 109 goto err; 110 } 111 subdev->mtd->owner = THIS_MODULE; 112 113 printk(KERN_INFO "SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n", 114 phys, (unsigned)(subdev->mtd->size >> 20), 115 subdev->map.bankwidth * 8); 116 117 return 0; 118 119 err: 120 sa1100_destroy_subdev(subdev); 121 out: 122 return ret; 123} 124 125static void sa1100_destroy(struct sa_info *info, struct flash_platform_data *plat) 126{ 127 int i; 128 129 if (info->mtd) { 130 if (info->nr_parts == 0) 131 del_mtd_device(info->mtd); 132#ifdef CONFIG_MTD_PARTITIONS 133 else 134 del_mtd_partitions(info->mtd); 135#endif 136#ifdef CONFIG_MTD_CONCAT 137 if (info->mtd != info->subdev[0].mtd) 138 mtd_concat_destroy(info->mtd); 139#endif 140 } 141 142 kfree(info->parts); 143 144 for (i = info->num_subdev - 1; i >= 0; i--) 145 sa1100_destroy_subdev(&info->subdev[i]); 146 kfree(info); 147 148 if (plat->exit) 149 plat->exit(); 150} 151 152static struct sa_info *__devinit 153sa1100_setup_mtd(struct platform_device *pdev, struct flash_platform_data *plat) 154{ 155 struct sa_info *info; 156 int nr, size, i, ret = 0; 157 158 /* 159 * Count number of devices. 160 */ 161 for (nr = 0; ; nr++) 162 if (!platform_get_resource(pdev, IORESOURCE_MEM, nr)) 163 break; 164 165 if (nr == 0) { 166 ret = -ENODEV; 167 goto out; 168 } 169 170 size = sizeof(struct sa_info) + sizeof(struct sa_subdev_info) * nr; 171 172 /* 173 * Allocate the map_info structs in one go. 174 */ 175 info = kzalloc(size, GFP_KERNEL); 176 if (!info) { 177 ret = -ENOMEM; 178 goto out; 179 } 180 181 if (plat->init) { 182 ret = plat->init(); 183 if (ret) 184 goto err; 185 } 186 187 /* 188 * Claim and then map the memory regions. 189 */ 190 for (i = 0; i < nr; i++) { 191 struct sa_subdev_info *subdev = &info->subdev[i]; 192 struct resource *res; 193 194 res = platform_get_resource(pdev, IORESOURCE_MEM, i); 195 if (!res) 196 break; 197 198 subdev->map.name = subdev->name; 199 sprintf(subdev->name, "%s-%d", plat->name, i); 200 subdev->plat = plat; 201 202 ret = sa1100_probe_subdev(subdev, res); 203 if (ret) 204 break; 205 } 206 207 info->num_subdev = i; 208 209 /* 210 * ENXIO is special. It means we didn't find a chip when we probed. 211 */ 212 if (ret != 0 && !(ret == -ENXIO && info->num_subdev > 0)) 213 goto err; 214 215 /* 216 * If we found one device, don't bother with concat support. If 217 * we found multiple devices, use concat if we have it available, 218 * otherwise fail. Either way, it'll be called "sa1100". 219 */ 220 if (info->num_subdev == 1) { 221 strcpy(info->subdev[0].name, plat->name); 222 info->mtd = info->subdev[0].mtd; 223 ret = 0; 224 } else if (info->num_subdev > 1) { 225#ifdef CONFIG_MTD_CONCAT 226 struct mtd_info *cdev[nr]; 227 /* 228 * We detected multiple devices. Concatenate them together. 229 */ 230 for (i = 0; i < info->num_subdev; i++) 231 cdev[i] = info->subdev[i].mtd; 232 233 info->mtd = mtd_concat_create(cdev, info->num_subdev, 234 plat->name); 235 if (info->mtd == NULL) 236 ret = -ENXIO; 237#else 238 printk(KERN_ERR "SA1100 flash: multiple devices " 239 "found but MTD concat support disabled.\n"); 240 ret = -ENXIO; 241#endif 242 } 243 244 if (ret == 0) 245 return info; 246 247 err: 248 sa1100_destroy(info, plat); 249 out: 250 return ERR_PTR(ret); 251} 252 253static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL }; 254 255static int __devinit sa1100_mtd_probe(struct platform_device *pdev) 256{ 257 struct flash_platform_data *plat = pdev->dev.platform_data; 258 struct mtd_partition *parts; 259 const char *part_type = NULL; 260 struct sa_info *info; 261 int err, nr_parts = 0; 262 263 if (!plat) 264 return -ENODEV; 265 266 info = sa1100_setup_mtd(pdev, plat); 267 if (IS_ERR(info)) { 268 err = PTR_ERR(info); 269 goto out; 270 } 271 272 /* 273 * Partition selection stuff. 274 */ 275#ifdef CONFIG_MTD_PARTITIONS 276 nr_parts = parse_mtd_partitions(info->mtd, part_probes, &parts, 0); 277 if (nr_parts > 0) { 278 info->parts = parts; 279 part_type = "dynamic"; 280 } else 281#endif 282 { 283 parts = plat->parts; 284 nr_parts = plat->nr_parts; 285 part_type = "static"; 286 } 287 288 if (nr_parts == 0) { 289 printk(KERN_NOTICE "SA1100 flash: no partition info " 290 "available, registering whole flash\n"); 291 add_mtd_device(info->mtd); 292 } else { 293 printk(KERN_NOTICE "SA1100 flash: using %s partition " 294 "definition\n", part_type); 295 add_mtd_partitions(info->mtd, parts, nr_parts); 296 } 297 298 info->nr_parts = nr_parts; 299 300 platform_set_drvdata(pdev, info); 301 err = 0; 302 303 out: 304 return err; 305} 306 307static int __exit sa1100_mtd_remove(struct platform_device *pdev) 308{ 309 struct sa_info *info = platform_get_drvdata(pdev); 310 struct flash_platform_data *plat = pdev->dev.platform_data; 311 312 platform_set_drvdata(pdev, NULL); 313 sa1100_destroy(info, plat); 314 315 return 0; 316} 317 318#ifdef CONFIG_PM 319static void sa1100_mtd_shutdown(struct platform_device *dev) 320{ 321 struct sa_info *info = platform_get_drvdata(dev); 322 if (info && info->mtd->suspend(info->mtd) == 0) 323 info->mtd->resume(info->mtd); 324} 325#else 326#define sa1100_mtd_shutdown NULL 327#endif 328 329static struct platform_driver sa1100_mtd_driver = { 330 .probe = sa1100_mtd_probe, 331 .remove = __exit_p(sa1100_mtd_remove), 332 .shutdown = sa1100_mtd_shutdown, 333 .driver = { 334 .name = "sa1100-mtd", 335 .owner = THIS_MODULE, 336 }, 337}; 338 339static int __init sa1100_mtd_init(void) 340{ 341 return platform_driver_register(&sa1100_mtd_driver); 342} 343 344static void __exit sa1100_mtd_exit(void) 345{ 346 platform_driver_unregister(&sa1100_mtd_driver); 347} 348 349module_init(sa1100_mtd_init); 350module_exit(sa1100_mtd_exit); 351 352MODULE_AUTHOR("Nicolas Pitre"); 353MODULE_DESCRIPTION("SA1100 CFI map driver"); 354MODULE_LICENSE("GPL"); 355MODULE_ALIAS("platform:sa1100-mtd"); 356