physmap.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * $Id: physmap.c,v 1.39 2005/11/29 14:49:36 gleixner Exp $
  3. *
  4. * Normal mappings of chips in physical memory
  5. *
  6. * Copyright (C) 2003 MontaVista Software Inc.
  7. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  8. *
  9. * 031022 - [jsun] add run-time configure and partition setup
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/init.h>
  15. #include <linux/slab.h>
  16. #include <linux/device.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/config.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/physmap.h>
  23. #include <asm/io.h>
  24. #include <asm/mach/flash.h>
  25. struct physmap_flash_info {
  26. struct mtd_info *mtd;
  27. struct map_info map;
  28. struct resource *res;
  29. #ifdef CONFIG_MTD_PARTITIONS
  30. int nr_parts;
  31. struct mtd_partition *parts;
  32. #endif
  33. };
  34. static int physmap_flash_remove(struct platform_device *dev)
  35. {
  36. struct physmap_flash_info *info;
  37. struct physmap_flash_data *physmap_data;
  38. info = platform_get_drvdata(dev);
  39. if (info == NULL)
  40. return 0;
  41. platform_set_drvdata(dev, NULL);
  42. physmap_data = dev->dev.platform_data;
  43. if (info->mtd != NULL) {
  44. #ifdef CONFIG_MTD_PARTITIONS
  45. if (info->nr_parts) {
  46. del_mtd_partitions(info->mtd);
  47. kfree(info->parts);
  48. } else if (physmap_data->nr_parts) {
  49. del_mtd_partitions(info->mtd);
  50. } else {
  51. del_mtd_device(info->mtd);
  52. }
  53. #else
  54. del_mtd_device(info->mtd);
  55. #endif
  56. map_destroy(info->mtd);
  57. }
  58. if (info->map.virt != NULL)
  59. iounmap((void *)info->map.virt);
  60. if (info->res != NULL) {
  61. release_resource(info->res);
  62. kfree(info->res);
  63. }
  64. return 0;
  65. }
  66. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
  67. #ifdef CONFIG_MTD_PARTITIONS
  68. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  69. #endif
  70. static int physmap_flash_probe(struct platform_device *dev)
  71. {
  72. struct physmap_flash_data *physmap_data;
  73. struct physmap_flash_info *info;
  74. const char **probe_type;
  75. int err;
  76. physmap_data = dev->dev.platform_data;
  77. if (physmap_data == NULL)
  78. return -ENODEV;
  79. printk(KERN_NOTICE "physmap platform flash device: %.8lx at %.8lx\n",
  80. dev->resource->end - dev->resource->start + 1,
  81. dev->resource->start);
  82. info = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  83. if (info == NULL) {
  84. err = -ENOMEM;
  85. goto err_out;
  86. }
  87. memset(info, 0, sizeof(*info));
  88. platform_set_drvdata(dev, info);
  89. info->res = request_mem_region(dev->resource->start,
  90. dev->resource->end - dev->resource->start + 1,
  91. dev->dev.bus_id);
  92. if (info->res == NULL) {
  93. dev_err(&dev->dev, "Could not reserve memory region\n");
  94. err = -ENOMEM;
  95. goto err_out;
  96. }
  97. info->map.name = dev->dev.bus_id;
  98. info->map.phys = dev->resource->start;
  99. info->map.size = dev->resource->end - dev->resource->start + 1;
  100. info->map.bankwidth = physmap_data->width;
  101. info->map.set_vpp = physmap_data->set_vpp;
  102. info->map.virt = ioremap(info->map.phys, info->map.size);
  103. if (info->map.virt == NULL) {
  104. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  105. err = EIO;
  106. goto err_out;
  107. }
  108. simple_map_init(&info->map);
  109. probe_type = rom_probe_types;
  110. for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
  111. info->mtd = do_map_probe(*probe_type, &info->map);
  112. if (info->mtd == NULL) {
  113. dev_err(&dev->dev, "map_probe failed\n");
  114. err = -ENXIO;
  115. goto err_out;
  116. }
  117. info->mtd->owner = THIS_MODULE;
  118. #ifdef CONFIG_MTD_PARTITIONS
  119. err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
  120. if (err > 0) {
  121. add_mtd_partitions(info->mtd, info->parts, err);
  122. return 0;
  123. }
  124. if (physmap_data->nr_parts) {
  125. printk(KERN_NOTICE "Using physmap partition information\n");
  126. add_mtd_partitions(info->mtd, physmap_data->parts,
  127. physmap_data->nr_parts);
  128. return 0;
  129. }
  130. #endif
  131. add_mtd_device(info->mtd);
  132. return 0;
  133. err_out:
  134. physmap_flash_remove(dev);
  135. return err;
  136. }
  137. static struct platform_driver physmap_flash_driver = {
  138. .probe = physmap_flash_probe,
  139. .remove = physmap_flash_remove,
  140. .driver = {
  141. .name = "physmap-flash",
  142. },
  143. };
  144. #ifdef CONFIG_MTD_PHYSMAP_LEN
  145. #if CONFIG_MTD_PHYSMAP_LEN != 0
  146. #warning using PHYSMAP compat code
  147. #define PHYSMAP_COMPAT
  148. #endif
  149. #endif
  150. #ifdef PHYSMAP_COMPAT
  151. static struct physmap_flash_data physmap_flash_data = {
  152. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  153. };
  154. static struct resource physmap_flash_resource = {
  155. .start = CONFIG_MTD_PHYSMAP_START,
  156. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN,
  157. .flags = IORESOURCE_MEM,
  158. };
  159. static struct platform_device physmap_flash = {
  160. .name = "physmap-flash",
  161. .id = 0,
  162. .dev = {
  163. .platform_data = &physmap_flash_data,
  164. },
  165. .num_resources = 1,
  166. .resource = &physmap_flash_resource,
  167. };
  168. void physmap_configure(unsigned long addr, unsigned long size,
  169. int bankwidth, void (*set_vpp)(struct map_info *, int))
  170. {
  171. physmap_flash_resource.start = addr;
  172. physmap_flash_resource.end = addr + size - 1;
  173. physmap_flash_data.width = bankwidth;
  174. physmap_flash_data.set_vpp = set_vpp;
  175. }
  176. #ifdef CONFIG_MTD_PARTITIONS
  177. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  178. {
  179. physmap_flash_data.nr_parts = num_parts;
  180. physmap_flash_data.parts = parts;
  181. }
  182. #endif
  183. #endif
  184. static int __init physmap_init(void)
  185. {
  186. int err;
  187. err = platform_driver_register(&physmap_flash_driver);
  188. #ifdef PHYSMAP_COMPAT
  189. if (err == 0)
  190. platform_device_register(&physmap_flash);
  191. #endif
  192. return err;
  193. }
  194. static void __exit physmap_exit(void)
  195. {
  196. #ifdef PHYSMAP_COMPAT
  197. platform_device_unregister(&physmap_flash);
  198. #endif
  199. platform_driver_unregister(&physmap_flash_driver);
  200. }
  201. module_init(physmap_init);
  202. module_exit(physmap_exit);
  203. MODULE_LICENSE("GPL");
  204. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  205. MODULE_DESCRIPTION("Generic configurable MTD map driver");