physmap.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Normal mappings of chips in physical memory
  3. *
  4. * Copyright (C) 2003 MontaVista Software Inc.
  5. * Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
  6. *
  7. * 031022 - [jsun] add run-time configure and partition setup
  8. */
  9. #include <linux/module.h>
  10. #include <linux/types.h>
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/device.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/map.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/mtd/physmap.h>
  20. #include <linux/mtd/concat.h>
  21. #include <linux/io.h>
  22. #define MAX_RESOURCES 4
  23. struct physmap_flash_info {
  24. struct mtd_info *mtd[MAX_RESOURCES];
  25. struct mtd_info *cmtd;
  26. struct map_info map[MAX_RESOURCES];
  27. };
  28. static int physmap_flash_remove(struct platform_device *dev)
  29. {
  30. struct physmap_flash_info *info;
  31. struct physmap_flash_data *physmap_data;
  32. int i;
  33. info = platform_get_drvdata(dev);
  34. if (info == NULL)
  35. return 0;
  36. platform_set_drvdata(dev, NULL);
  37. physmap_data = dev->dev.platform_data;
  38. if (info->cmtd) {
  39. mtd_device_unregister(info->cmtd);
  40. if (info->cmtd != info->mtd[0])
  41. mtd_concat_destroy(info->cmtd);
  42. }
  43. for (i = 0; i < MAX_RESOURCES; i++) {
  44. if (info->mtd[i] != NULL)
  45. map_destroy(info->mtd[i]);
  46. }
  47. if (physmap_data->exit)
  48. physmap_data->exit(dev);
  49. return 0;
  50. }
  51. static void physmap_set_vpp(struct map_info *map, int state)
  52. {
  53. struct platform_device *pdev;
  54. struct physmap_flash_data *physmap_data;
  55. pdev = (struct platform_device *)map->map_priv_1;
  56. physmap_data = pdev->dev.platform_data;
  57. if (physmap_data->set_vpp)
  58. physmap_data->set_vpp(pdev, state);
  59. }
  60. static const char *rom_probe_types[] = {
  61. "cfi_probe",
  62. "jedec_probe",
  63. "qinfo_probe",
  64. "map_rom",
  65. NULL };
  66. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs",
  67. NULL };
  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. const char **part_types;
  74. int err = 0;
  75. int i;
  76. int devices_found = 0;
  77. physmap_data = dev->dev.platform_data;
  78. if (physmap_data == NULL)
  79. return -ENODEV;
  80. info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
  81. GFP_KERNEL);
  82. if (info == NULL) {
  83. err = -ENOMEM;
  84. goto err_out;
  85. }
  86. if (physmap_data->init) {
  87. err = physmap_data->init(dev);
  88. if (err)
  89. goto err_out;
  90. }
  91. platform_set_drvdata(dev, info);
  92. for (i = 0; i < dev->num_resources; i++) {
  93. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  94. (unsigned long long)resource_size(&dev->resource[i]),
  95. (unsigned long long)dev->resource[i].start);
  96. if (!devm_request_mem_region(&dev->dev,
  97. dev->resource[i].start,
  98. resource_size(&dev->resource[i]),
  99. dev_name(&dev->dev))) {
  100. dev_err(&dev->dev, "Could not reserve memory region\n");
  101. err = -ENOMEM;
  102. goto err_out;
  103. }
  104. info->map[i].name = dev_name(&dev->dev);
  105. info->map[i].phys = dev->resource[i].start;
  106. info->map[i].size = resource_size(&dev->resource[i]);
  107. info->map[i].bankwidth = physmap_data->width;
  108. info->map[i].set_vpp = physmap_set_vpp;
  109. info->map[i].pfow_base = physmap_data->pfow_base;
  110. info->map[i].map_priv_1 = (unsigned long)dev;
  111. info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
  112. info->map[i].size);
  113. if (info->map[i].virt == NULL) {
  114. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  115. err = -EIO;
  116. goto err_out;
  117. }
  118. simple_map_init(&info->map[i]);
  119. probe_type = rom_probe_types;
  120. if (physmap_data->probe_type == NULL) {
  121. for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
  122. info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
  123. } else
  124. info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
  125. if (info->mtd[i] == NULL) {
  126. dev_err(&dev->dev, "map_probe failed\n");
  127. err = -ENXIO;
  128. goto err_out;
  129. } else {
  130. devices_found++;
  131. }
  132. info->mtd[i]->owner = THIS_MODULE;
  133. info->mtd[i]->dev.parent = &dev->dev;
  134. }
  135. if (devices_found == 1) {
  136. info->cmtd = info->mtd[0];
  137. } else if (devices_found > 1) {
  138. /*
  139. * We detected multiple devices. Concatenate them together.
  140. */
  141. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
  142. if (info->cmtd == NULL)
  143. err = -ENXIO;
  144. }
  145. if (err)
  146. goto err_out;
  147. part_types = physmap_data->part_probe_types ? : part_probe_types;
  148. mtd_device_parse_register(info->cmtd, part_types, 0,
  149. physmap_data->parts, physmap_data->nr_parts);
  150. return 0;
  151. err_out:
  152. physmap_flash_remove(dev);
  153. return err;
  154. }
  155. #ifdef CONFIG_PM
  156. static void physmap_flash_shutdown(struct platform_device *dev)
  157. {
  158. struct physmap_flash_info *info = platform_get_drvdata(dev);
  159. int i;
  160. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  161. if (mtd_suspend(info->mtd[i]) == 0)
  162. mtd_resume(info->mtd[i]);
  163. }
  164. #else
  165. #define physmap_flash_shutdown NULL
  166. #endif
  167. static struct platform_driver physmap_flash_driver = {
  168. .probe = physmap_flash_probe,
  169. .remove = physmap_flash_remove,
  170. .shutdown = physmap_flash_shutdown,
  171. .driver = {
  172. .name = "physmap-flash",
  173. .owner = THIS_MODULE,
  174. },
  175. };
  176. #ifdef CONFIG_MTD_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. #endif
  195. static int __init physmap_init(void)
  196. {
  197. int err;
  198. err = platform_driver_register(&physmap_flash_driver);
  199. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  200. if (err == 0) {
  201. err = platform_device_register(&physmap_flash);
  202. if (err)
  203. platform_driver_unregister(&physmap_flash_driver);
  204. }
  205. #endif
  206. return err;
  207. }
  208. static void __exit physmap_exit(void)
  209. {
  210. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  211. platform_device_unregister(&physmap_flash);
  212. #endif
  213. platform_driver_unregister(&physmap_flash_driver);
  214. }
  215. module_init(physmap_init);
  216. module_exit(physmap_exit);
  217. MODULE_LICENSE("GPL");
  218. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  219. MODULE_DESCRIPTION("Generic configurable MTD map driver");
  220. /* legacy platform drivers can't hotplug or coldplg */
  221. #ifndef CONFIG_MTD_PHYSMAP_COMPAT
  222. /* work with hotplug and coldplug */
  223. MODULE_ALIAS("platform:physmap-flash");
  224. #endif