physmap.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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((void *)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 = kmalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  81. if (info == NULL) {
  82. err = -ENOMEM;
  83. goto err_out;
  84. }
  85. memset(info, 0, sizeof(*info));
  86. platform_set_drvdata(dev, info);
  87. info->res = request_mem_region(dev->resource->start,
  88. dev->resource->end - dev->resource->start + 1,
  89. dev->dev.bus_id);
  90. if (info->res == NULL) {
  91. dev_err(&dev->dev, "Could not reserve memory region\n");
  92. err = -ENOMEM;
  93. goto err_out;
  94. }
  95. info->map.name = dev->dev.bus_id;
  96. info->map.phys = dev->resource->start;
  97. info->map.size = dev->resource->end - dev->resource->start + 1;
  98. info->map.bankwidth = physmap_data->width;
  99. info->map.set_vpp = physmap_data->set_vpp;
  100. info->map.virt = ioremap(info->map.phys, info->map.size);
  101. if (info->map.virt == NULL) {
  102. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  103. err = EIO;
  104. goto err_out;
  105. }
  106. simple_map_init(&info->map);
  107. probe_type = rom_probe_types;
  108. for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
  109. info->mtd = do_map_probe(*probe_type, &info->map);
  110. if (info->mtd == NULL) {
  111. dev_err(&dev->dev, "map_probe failed\n");
  112. err = -ENXIO;
  113. goto err_out;
  114. }
  115. info->mtd->owner = THIS_MODULE;
  116. #ifdef CONFIG_MTD_PARTITIONS
  117. err = parse_mtd_partitions(info->mtd, part_probe_types, &info->parts, 0);
  118. if (err > 0) {
  119. add_mtd_partitions(info->mtd, info->parts, err);
  120. return 0;
  121. }
  122. if (physmap_data->nr_parts) {
  123. printk(KERN_NOTICE "Using physmap partition information\n");
  124. add_mtd_partitions(info->mtd, physmap_data->parts,
  125. physmap_data->nr_parts);
  126. return 0;
  127. }
  128. #endif
  129. add_mtd_device(info->mtd);
  130. return 0;
  131. err_out:
  132. physmap_flash_remove(dev);
  133. return err;
  134. }
  135. #ifdef CONFIG_PM
  136. static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
  137. {
  138. struct physmap_flash_info *info = platform_get_drvdata(dev);
  139. int ret = 0;
  140. if (info)
  141. ret = info->mtd->suspend(info->mtd);
  142. return ret;
  143. }
  144. static int physmap_flash_resume(struct platform_device *dev)
  145. {
  146. struct physmap_flash_info *info = platform_get_drvdata(dev);
  147. if (info)
  148. info->mtd->resume(info->mtd);
  149. return 0;
  150. }
  151. static void physmap_flash_shutdown(struct platform_device *dev)
  152. {
  153. struct physmap_flash_info *info = platform_get_drvdata(dev);
  154. if (info && info->mtd->suspend(info->mtd) == 0)
  155. info->mtd->resume(info->mtd);
  156. }
  157. #endif
  158. static struct platform_driver physmap_flash_driver = {
  159. .probe = physmap_flash_probe,
  160. .remove = physmap_flash_remove,
  161. #ifdef CONFIG_PM
  162. .suspend = physmap_flash_suspend,
  163. .resume = physmap_flash_resume,
  164. .shutdown = physmap_flash_shutdown,
  165. #endif
  166. .driver = {
  167. .name = "physmap-flash",
  168. },
  169. };
  170. #ifdef CONFIG_MTD_PHYSMAP_LEN
  171. #if CONFIG_MTD_PHYSMAP_LEN != 0
  172. #warning using PHYSMAP compat code
  173. #define PHYSMAP_COMPAT
  174. #endif
  175. #endif
  176. #ifdef PHYSMAP_COMPAT
  177. static struct physmap_flash_data physmap_flash_data = {
  178. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  179. };
  180. static struct resource physmap_flash_resource = {
  181. .start = CONFIG_MTD_PHYSMAP_START,
  182. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  183. .flags = IORESOURCE_MEM,
  184. };
  185. static struct platform_device physmap_flash = {
  186. .name = "physmap-flash",
  187. .id = 0,
  188. .dev = {
  189. .platform_data = &physmap_flash_data,
  190. },
  191. .num_resources = 1,
  192. .resource = &physmap_flash_resource,
  193. };
  194. void physmap_configure(unsigned long addr, unsigned long size,
  195. int bankwidth, void (*set_vpp)(struct map_info *, int))
  196. {
  197. physmap_flash_resource.start = addr;
  198. physmap_flash_resource.end = addr + size - 1;
  199. physmap_flash_data.width = bankwidth;
  200. physmap_flash_data.set_vpp = set_vpp;
  201. }
  202. #ifdef CONFIG_MTD_PARTITIONS
  203. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  204. {
  205. physmap_flash_data.nr_parts = num_parts;
  206. physmap_flash_data.parts = parts;
  207. }
  208. #endif
  209. #endif
  210. static int __init physmap_init(void)
  211. {
  212. int err;
  213. err = platform_driver_register(&physmap_flash_driver);
  214. #ifdef PHYSMAP_COMPAT
  215. if (err == 0)
  216. platform_device_register(&physmap_flash);
  217. #endif
  218. return err;
  219. }
  220. static void __exit physmap_exit(void)
  221. {
  222. #ifdef PHYSMAP_COMPAT
  223. platform_device_unregister(&physmap_flash);
  224. #endif
  225. platform_driver_unregister(&physmap_flash_driver);
  226. }
  227. module_init(physmap_init);
  228. module_exit(physmap_exit);
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  231. MODULE_DESCRIPTION("Generic configurable MTD map driver");