physmap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. spinlock_t vpp_lock;
  28. int vpp_refcnt;
  29. };
  30. static int physmap_flash_remove(struct platform_device *dev)
  31. {
  32. struct physmap_flash_info *info;
  33. struct physmap_flash_data *physmap_data;
  34. int i;
  35. info = platform_get_drvdata(dev);
  36. if (info == NULL)
  37. return 0;
  38. platform_set_drvdata(dev, NULL);
  39. physmap_data = dev->dev.platform_data;
  40. if (info->cmtd) {
  41. mtd_device_unregister(info->cmtd);
  42. if (info->cmtd != info->mtd[0])
  43. mtd_concat_destroy(info->cmtd);
  44. }
  45. for (i = 0; i < MAX_RESOURCES; i++) {
  46. if (info->mtd[i] != NULL)
  47. map_destroy(info->mtd[i]);
  48. }
  49. if (physmap_data->exit)
  50. physmap_data->exit(dev);
  51. return 0;
  52. }
  53. static void physmap_set_vpp(struct map_info *map, int state)
  54. {
  55. struct platform_device *pdev;
  56. struct physmap_flash_data *physmap_data;
  57. struct physmap_flash_info *info;
  58. unsigned long flags;
  59. pdev = (struct platform_device *)map->map_priv_1;
  60. physmap_data = pdev->dev.platform_data;
  61. if (!physmap_data->set_vpp)
  62. return;
  63. info = platform_get_drvdata(pdev);
  64. spin_lock_irqsave(&info->vpp_lock, flags);
  65. if (state) {
  66. if (++info->vpp_refcnt == 1) /* first nested 'on' */
  67. physmap_data->set_vpp(pdev, 1);
  68. } else {
  69. if (--info->vpp_refcnt == 0) /* last nested 'off' */
  70. physmap_data->set_vpp(pdev, 0);
  71. }
  72. spin_unlock_irqrestore(&info->vpp_lock, flags);
  73. }
  74. static const char *rom_probe_types[] = {
  75. "cfi_probe",
  76. "jedec_probe",
  77. "qinfo_probe",
  78. "map_rom",
  79. NULL };
  80. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", "afs",
  81. NULL };
  82. static int physmap_flash_probe(struct platform_device *dev)
  83. {
  84. struct physmap_flash_data *physmap_data;
  85. struct physmap_flash_info *info;
  86. const char **probe_type;
  87. const char **part_types;
  88. int err = 0;
  89. int i;
  90. int devices_found = 0;
  91. physmap_data = dev->dev.platform_data;
  92. if (physmap_data == NULL)
  93. return -ENODEV;
  94. info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
  95. GFP_KERNEL);
  96. if (info == NULL) {
  97. err = -ENOMEM;
  98. goto err_out;
  99. }
  100. if (physmap_data->init) {
  101. err = physmap_data->init(dev);
  102. if (err)
  103. goto err_out;
  104. }
  105. platform_set_drvdata(dev, info);
  106. for (i = 0; i < dev->num_resources; i++) {
  107. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  108. (unsigned long long)resource_size(&dev->resource[i]),
  109. (unsigned long long)dev->resource[i].start);
  110. if (!devm_request_mem_region(&dev->dev,
  111. dev->resource[i].start,
  112. resource_size(&dev->resource[i]),
  113. dev_name(&dev->dev))) {
  114. dev_err(&dev->dev, "Could not reserve memory region\n");
  115. err = -ENOMEM;
  116. goto err_out;
  117. }
  118. info->map[i].name = dev_name(&dev->dev);
  119. info->map[i].phys = dev->resource[i].start;
  120. info->map[i].size = resource_size(&dev->resource[i]);
  121. info->map[i].bankwidth = physmap_data->width;
  122. info->map[i].set_vpp = physmap_set_vpp;
  123. info->map[i].pfow_base = physmap_data->pfow_base;
  124. info->map[i].map_priv_1 = (unsigned long)dev;
  125. info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
  126. info->map[i].size);
  127. if (info->map[i].virt == NULL) {
  128. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  129. err = -EIO;
  130. goto err_out;
  131. }
  132. simple_map_init(&info->map[i]);
  133. probe_type = rom_probe_types;
  134. if (physmap_data->probe_type == NULL) {
  135. for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
  136. info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
  137. } else
  138. info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
  139. if (info->mtd[i] == NULL) {
  140. dev_err(&dev->dev, "map_probe failed\n");
  141. err = -ENXIO;
  142. goto err_out;
  143. } else {
  144. devices_found++;
  145. }
  146. info->mtd[i]->owner = THIS_MODULE;
  147. info->mtd[i]->dev.parent = &dev->dev;
  148. }
  149. if (devices_found == 1) {
  150. info->cmtd = info->mtd[0];
  151. } else if (devices_found > 1) {
  152. /*
  153. * We detected multiple devices. Concatenate them together.
  154. */
  155. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
  156. if (info->cmtd == NULL)
  157. err = -ENXIO;
  158. }
  159. if (err)
  160. goto err_out;
  161. spin_lock_init(&info->vpp_lock);
  162. part_types = physmap_data->part_probe_types ? : part_probe_types;
  163. mtd_device_parse_register(info->cmtd, part_types, NULL,
  164. physmap_data->parts, physmap_data->nr_parts);
  165. return 0;
  166. err_out:
  167. physmap_flash_remove(dev);
  168. return err;
  169. }
  170. #ifdef CONFIG_PM
  171. static void physmap_flash_shutdown(struct platform_device *dev)
  172. {
  173. struct physmap_flash_info *info = platform_get_drvdata(dev);
  174. int i;
  175. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  176. if (mtd_suspend(info->mtd[i]) == 0)
  177. mtd_resume(info->mtd[i]);
  178. }
  179. #else
  180. #define physmap_flash_shutdown NULL
  181. #endif
  182. static struct platform_driver physmap_flash_driver = {
  183. .probe = physmap_flash_probe,
  184. .remove = physmap_flash_remove,
  185. .shutdown = physmap_flash_shutdown,
  186. .driver = {
  187. .name = "physmap-flash",
  188. .owner = THIS_MODULE,
  189. },
  190. };
  191. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  192. static struct physmap_flash_data physmap_flash_data = {
  193. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  194. };
  195. static struct resource physmap_flash_resource = {
  196. .start = CONFIG_MTD_PHYSMAP_START,
  197. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  198. .flags = IORESOURCE_MEM,
  199. };
  200. static struct platform_device physmap_flash = {
  201. .name = "physmap-flash",
  202. .id = 0,
  203. .dev = {
  204. .platform_data = &physmap_flash_data,
  205. },
  206. .num_resources = 1,
  207. .resource = &physmap_flash_resource,
  208. };
  209. #endif
  210. static int __init physmap_init(void)
  211. {
  212. int err;
  213. err = platform_driver_register(&physmap_flash_driver);
  214. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  215. if (err == 0) {
  216. err = platform_device_register(&physmap_flash);
  217. if (err)
  218. platform_driver_unregister(&physmap_flash_driver);
  219. }
  220. #endif
  221. return err;
  222. }
  223. static void __exit physmap_exit(void)
  224. {
  225. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  226. platform_device_unregister(&physmap_flash);
  227. #endif
  228. platform_driver_unregister(&physmap_flash_driver);
  229. }
  230. module_init(physmap_init);
  231. module_exit(physmap_exit);
  232. MODULE_LICENSE("GPL");
  233. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  234. MODULE_DESCRIPTION("Generic configurable MTD map driver");
  235. /* legacy platform drivers can't hotplug or coldplg */
  236. #ifndef CONFIG_MTD_PHYSMAP_COMPAT
  237. /* work with hotplug and coldplug */
  238. MODULE_ALIAS("platform:physmap-flash");
  239. #endif