physmap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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/mtd/partitions.h>
  21. #include <linux/mtd/physmap.h>
  22. #include <asm/io.h>
  23. struct physmap_flash_info {
  24. struct mtd_info *mtd;
  25. struct map_info map;
  26. struct resource *res;
  27. #ifdef CONFIG_MTD_PARTITIONS
  28. int nr_parts;
  29. struct mtd_partition *parts;
  30. #endif
  31. };
  32. static int physmap_flash_remove(struct platform_device *dev)
  33. {
  34. struct physmap_flash_info *info;
  35. struct physmap_flash_data *physmap_data;
  36. info = platform_get_drvdata(dev);
  37. if (info == NULL)
  38. return 0;
  39. platform_set_drvdata(dev, NULL);
  40. physmap_data = dev->dev.platform_data;
  41. if (info->mtd != NULL) {
  42. #ifdef CONFIG_MTD_PARTITIONS
  43. if (info->nr_parts) {
  44. del_mtd_partitions(info->mtd);
  45. kfree(info->parts);
  46. } else if (physmap_data->nr_parts) {
  47. del_mtd_partitions(info->mtd);
  48. } else {
  49. del_mtd_device(info->mtd);
  50. }
  51. #else
  52. del_mtd_device(info->mtd);
  53. #endif
  54. map_destroy(info->mtd);
  55. }
  56. if (info->map.virt != NULL)
  57. iounmap(info->map.virt);
  58. if (info->res != NULL) {
  59. release_resource(info->res);
  60. kfree(info->res);
  61. }
  62. return 0;
  63. }
  64. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
  65. #ifdef CONFIG_MTD_PARTITIONS
  66. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  67. #endif
  68. static int physmap_flash_probe(struct platform_device *dev)
  69. {
  70. struct physmap_flash_data *physmap_data;
  71. struct physmap_flash_info *info;
  72. const char **probe_type;
  73. int err;
  74. physmap_data = dev->dev.platform_data;
  75. if (physmap_data == NULL)
  76. return -ENODEV;
  77. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  78. (unsigned long long)(dev->resource->end - dev->resource->start + 1),
  79. (unsigned long long)dev->resource->start);
  80. info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  81. if (info == NULL) {
  82. err = -ENOMEM;
  83. goto err_out;
  84. }
  85. platform_set_drvdata(dev, info);
  86. info->res = request_mem_region(dev->resource->start,
  87. dev->resource->end - dev->resource->start + 1,
  88. dev->dev.bus_id);
  89. if (info->res == NULL) {
  90. dev_err(&dev->dev, "Could not reserve memory region\n");
  91. err = -ENOMEM;
  92. goto err_out;
  93. }
  94. info->map.name = dev->dev.bus_id;
  95. info->map.phys = dev->resource->start;
  96. info->map.size = dev->resource->end - dev->resource->start + 1;
  97. info->map.bankwidth = physmap_data->width;
  98. info->map.set_vpp = physmap_data->set_vpp;
  99. info->map.virt = ioremap(info->map.phys, info->map.size);
  100. if (info->map.virt == NULL) {
  101. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  102. err = EIO;
  103. goto err_out;
  104. }
  105. simple_map_init(&info->map);
  106. probe_type = rom_probe_types;
  107. for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
  108. info->mtd = do_map_probe(*probe_type, &info->map);
  109. if (info->mtd == NULL) {
  110. dev_err(&dev->dev, "map_probe failed\n");
  111. err = -ENXIO;
  112. goto err_out;
  113. }
  114. info->mtd->owner = THIS_MODULE;
  115. #ifdef CONFIG_MTD_PARTITIONS
  116. err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
  117. if (err > 0) {
  118. add_mtd_partitions(info->mtd, info->parts, err);
  119. return 0;
  120. }
  121. if (physmap_data->nr_parts) {
  122. printk(KERN_NOTICE "Using physmap partition information\n");
  123. add_mtd_partitions(info->mtd, physmap_data->parts,
  124. physmap_data->nr_parts);
  125. return 0;
  126. }
  127. #endif
  128. add_mtd_device(info->mtd);
  129. return 0;
  130. err_out:
  131. physmap_flash_remove(dev);
  132. return err;
  133. }
  134. #ifdef CONFIG_PM
  135. static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
  136. {
  137. struct physmap_flash_info *info = platform_get_drvdata(dev);
  138. int ret = 0;
  139. if (info)
  140. ret = info->mtd->suspend(info->mtd);
  141. return ret;
  142. }
  143. static int physmap_flash_resume(struct platform_device *dev)
  144. {
  145. struct physmap_flash_info *info = platform_get_drvdata(dev);
  146. if (info)
  147. info->mtd->resume(info->mtd);
  148. return 0;
  149. }
  150. static void physmap_flash_shutdown(struct platform_device *dev)
  151. {
  152. struct physmap_flash_info *info = platform_get_drvdata(dev);
  153. if (info && info->mtd->suspend(info->mtd) == 0)
  154. info->mtd->resume(info->mtd);
  155. }
  156. #endif
  157. static struct platform_driver physmap_flash_driver = {
  158. .probe = physmap_flash_probe,
  159. .remove = physmap_flash_remove,
  160. #ifdef CONFIG_PM
  161. .suspend = physmap_flash_suspend,
  162. .resume = physmap_flash_resume,
  163. .shutdown = physmap_flash_shutdown,
  164. #endif
  165. .driver = {
  166. .name = "physmap-flash",
  167. },
  168. };
  169. #ifdef CONFIG_MTD_PHYSMAP_LEN
  170. #if CONFIG_MTD_PHYSMAP_LEN != 0
  171. #warning using PHYSMAP compat code
  172. #define PHYSMAP_COMPAT
  173. #endif
  174. #endif
  175. #ifdef PHYSMAP_COMPAT
  176. static struct physmap_flash_data physmap_flash_data = {
  177. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  178. };
  179. static struct resource physmap_flash_resource = {
  180. .start = CONFIG_MTD_PHYSMAP_START,
  181. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  182. .flags = IORESOURCE_MEM,
  183. };
  184. static struct platform_device physmap_flash = {
  185. .name = "physmap-flash",
  186. .id = 0,
  187. .dev = {
  188. .platform_data = &physmap_flash_data,
  189. },
  190. .num_resources = 1,
  191. .resource = &physmap_flash_resource,
  192. };
  193. void physmap_configure(unsigned long addr, unsigned long size,
  194. int bankwidth, void (*set_vpp)(struct map_info *, int))
  195. {
  196. physmap_flash_resource.start = addr;
  197. physmap_flash_resource.end = addr + size - 1;
  198. physmap_flash_data.width = bankwidth;
  199. physmap_flash_data.set_vpp = set_vpp;
  200. }
  201. #ifdef CONFIG_MTD_PARTITIONS
  202. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  203. {
  204. physmap_flash_data.nr_parts = num_parts;
  205. physmap_flash_data.parts = parts;
  206. }
  207. #endif
  208. #endif
  209. static int __init physmap_init(void)
  210. {
  211. int err;
  212. err = platform_driver_register(&physmap_flash_driver);
  213. #ifdef PHYSMAP_COMPAT
  214. if (err == 0)
  215. platform_device_register(&physmap_flash);
  216. #endif
  217. return err;
  218. }
  219. static void __exit physmap_exit(void)
  220. {
  221. #ifdef PHYSMAP_COMPAT
  222. platform_device_unregister(&physmap_flash);
  223. #endif
  224. platform_driver_unregister(&physmap_flash_driver);
  225. }
  226. module_init(physmap_init);
  227. module_exit(physmap_exit);
  228. MODULE_LICENSE("GPL");
  229. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  230. MODULE_DESCRIPTION("Generic configurable MTD map driver");