physmap.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 * const rom_probe_types[] = {
  75. "cfi_probe", "jedec_probe", "qinfo_probe", "map_rom", NULL };
  76. static const char * const part_probe_types[] = {
  77. "cmdlinepart", "RedBoot", "afs", NULL };
  78. static int physmap_flash_probe(struct platform_device *dev)
  79. {
  80. struct physmap_flash_data *physmap_data;
  81. struct physmap_flash_info *info;
  82. const char * const *probe_type;
  83. const char * const *part_types;
  84. int err = 0;
  85. int i;
  86. int devices_found = 0;
  87. physmap_data = dev->dev.platform_data;
  88. if (physmap_data == NULL)
  89. return -ENODEV;
  90. info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
  91. GFP_KERNEL);
  92. if (info == NULL) {
  93. err = -ENOMEM;
  94. goto err_out;
  95. }
  96. if (physmap_data->init) {
  97. err = physmap_data->init(dev);
  98. if (err)
  99. goto err_out;
  100. }
  101. platform_set_drvdata(dev, info);
  102. for (i = 0; i < dev->num_resources; i++) {
  103. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  104. (unsigned long long)resource_size(&dev->resource[i]),
  105. (unsigned long long)dev->resource[i].start);
  106. if (!devm_request_mem_region(&dev->dev,
  107. dev->resource[i].start,
  108. resource_size(&dev->resource[i]),
  109. dev_name(&dev->dev))) {
  110. dev_err(&dev->dev, "Could not reserve memory region\n");
  111. err = -ENOMEM;
  112. goto err_out;
  113. }
  114. info->map[i].name = dev_name(&dev->dev);
  115. info->map[i].phys = dev->resource[i].start;
  116. info->map[i].size = resource_size(&dev->resource[i]);
  117. info->map[i].bankwidth = physmap_data->width;
  118. info->map[i].set_vpp = physmap_set_vpp;
  119. info->map[i].pfow_base = physmap_data->pfow_base;
  120. info->map[i].map_priv_1 = (unsigned long)dev;
  121. info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
  122. info->map[i].size);
  123. if (info->map[i].virt == NULL) {
  124. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  125. err = -EIO;
  126. goto err_out;
  127. }
  128. simple_map_init(&info->map[i]);
  129. probe_type = rom_probe_types;
  130. if (physmap_data->probe_type == NULL) {
  131. for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
  132. info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
  133. } else
  134. info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
  135. if (info->mtd[i] == NULL) {
  136. dev_err(&dev->dev, "map_probe failed\n");
  137. err = -ENXIO;
  138. goto err_out;
  139. } else {
  140. devices_found++;
  141. }
  142. info->mtd[i]->owner = THIS_MODULE;
  143. info->mtd[i]->dev.parent = &dev->dev;
  144. }
  145. if (devices_found == 1) {
  146. info->cmtd = info->mtd[0];
  147. } else if (devices_found > 1) {
  148. /*
  149. * We detected multiple devices. Concatenate them together.
  150. */
  151. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
  152. if (info->cmtd == NULL)
  153. err = -ENXIO;
  154. }
  155. if (err)
  156. goto err_out;
  157. spin_lock_init(&info->vpp_lock);
  158. part_types = physmap_data->part_probe_types ? : part_probe_types;
  159. mtd_device_parse_register(info->cmtd, part_types, NULL,
  160. physmap_data->parts, physmap_data->nr_parts);
  161. return 0;
  162. err_out:
  163. physmap_flash_remove(dev);
  164. return err;
  165. }
  166. #ifdef CONFIG_PM
  167. static void physmap_flash_shutdown(struct platform_device *dev)
  168. {
  169. struct physmap_flash_info *info = platform_get_drvdata(dev);
  170. int i;
  171. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  172. if (mtd_suspend(info->mtd[i]) == 0)
  173. mtd_resume(info->mtd[i]);
  174. }
  175. #else
  176. #define physmap_flash_shutdown NULL
  177. #endif
  178. static struct platform_driver physmap_flash_driver = {
  179. .probe = physmap_flash_probe,
  180. .remove = physmap_flash_remove,
  181. .shutdown = physmap_flash_shutdown,
  182. .driver = {
  183. .name = "physmap-flash",
  184. .owner = THIS_MODULE,
  185. },
  186. };
  187. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  188. static struct physmap_flash_data physmap_flash_data = {
  189. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  190. };
  191. static struct resource physmap_flash_resource = {
  192. .start = CONFIG_MTD_PHYSMAP_START,
  193. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  194. .flags = IORESOURCE_MEM,
  195. };
  196. static struct platform_device physmap_flash = {
  197. .name = "physmap-flash",
  198. .id = 0,
  199. .dev = {
  200. .platform_data = &physmap_flash_data,
  201. },
  202. .num_resources = 1,
  203. .resource = &physmap_flash_resource,
  204. };
  205. #endif
  206. static int __init physmap_init(void)
  207. {
  208. int err;
  209. err = platform_driver_register(&physmap_flash_driver);
  210. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  211. if (err == 0) {
  212. err = platform_device_register(&physmap_flash);
  213. if (err)
  214. platform_driver_unregister(&physmap_flash_driver);
  215. }
  216. #endif
  217. return err;
  218. }
  219. static void __exit physmap_exit(void)
  220. {
  221. #ifdef CONFIG_MTD_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");
  231. /* legacy platform drivers can't hotplug or coldplg */
  232. #ifndef CONFIG_MTD_PHYSMAP_COMPAT
  233. /* work with hotplug and coldplug */
  234. MODULE_ALIAS("platform:physmap-flash");
  235. #endif