physmap.c 6.8 KB

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