physmap.c 5.6 KB

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