physmap.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #ifdef CONFIG_MTD_PARTITIONS
  28. int nr_parts;
  29. struct mtd_partition *parts;
  30. #endif
  31. };
  32. static int physmap_flash_remove(struct platform_device *dev)
  33. {
  34. struct physmap_flash_info *info;
  35. struct physmap_flash_data *physmap_data;
  36. int i;
  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->cmtd) {
  43. #ifdef CONFIG_MTD_PARTITIONS
  44. if (info->nr_parts || physmap_data->nr_parts) {
  45. del_mtd_partitions(info->cmtd);
  46. if (info->nr_parts)
  47. kfree(info->parts);
  48. } else {
  49. del_mtd_device(info->cmtd);
  50. }
  51. #else
  52. del_mtd_device(info->cmtd);
  53. #endif
  54. if (info->cmtd != info->mtd[0])
  55. mtd_concat_destroy(info->cmtd);
  56. }
  57. for (i = 0; i < MAX_RESOURCES; i++) {
  58. if (info->mtd[i] != NULL)
  59. map_destroy(info->mtd[i]);
  60. }
  61. if (physmap_data->exit)
  62. physmap_data->exit(dev);
  63. return 0;
  64. }
  65. static const char *rom_probe_types[] = {
  66. "cfi_probe",
  67. "jedec_probe",
  68. "qinfo_probe",
  69. "map_rom",
  70. NULL };
  71. #ifdef CONFIG_MTD_PARTITIONS
  72. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot",
  73. #ifdef CONFIG_MTD_AFS_PARTS
  74. "afs",
  75. #endif
  76. NULL };
  77. #endif
  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 **probe_type;
  83. int err = 0;
  84. int i;
  85. int devices_found = 0;
  86. physmap_data = dev->dev.platform_data;
  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_data->set_vpp;
  118. info->map[i].pfow_base = physmap_data->pfow_base;
  119. info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
  120. info->map[i].size);
  121. if (info->map[i].virt == NULL) {
  122. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  123. err = -EIO;
  124. goto err_out;
  125. }
  126. simple_map_init(&info->map[i]);
  127. probe_type = rom_probe_types;
  128. if (physmap_data->probe_type == NULL) {
  129. for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
  130. info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
  131. } else
  132. info->mtd[i] = do_map_probe(physmap_data->probe_type, &info->map[i]);
  133. if (info->mtd[i] == NULL) {
  134. dev_err(&dev->dev, "map_probe failed\n");
  135. err = -ENXIO;
  136. goto err_out;
  137. } else {
  138. devices_found++;
  139. }
  140. info->mtd[i]->owner = THIS_MODULE;
  141. info->mtd[i]->dev.parent = &dev->dev;
  142. }
  143. if (devices_found == 1) {
  144. info->cmtd = info->mtd[0];
  145. } else if (devices_found > 1) {
  146. /*
  147. * We detected multiple devices. Concatenate them together.
  148. */
  149. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev_name(&dev->dev));
  150. if (info->cmtd == NULL)
  151. err = -ENXIO;
  152. }
  153. if (err)
  154. goto err_out;
  155. #ifdef CONFIG_MTD_PARTITIONS
  156. err = parse_mtd_partitions(info->cmtd, part_probe_types,
  157. &info->parts, 0);
  158. if (err > 0) {
  159. add_mtd_partitions(info->cmtd, info->parts, err);
  160. info->nr_parts = err;
  161. return 0;
  162. }
  163. if (physmap_data->nr_parts) {
  164. printk(KERN_NOTICE "Using physmap partition information\n");
  165. add_mtd_partitions(info->cmtd, physmap_data->parts,
  166. physmap_data->nr_parts);
  167. return 0;
  168. }
  169. #endif
  170. add_mtd_device(info->cmtd);
  171. return 0;
  172. err_out:
  173. physmap_flash_remove(dev);
  174. return err;
  175. }
  176. #ifdef CONFIG_PM
  177. static void physmap_flash_shutdown(struct platform_device *dev)
  178. {
  179. struct physmap_flash_info *info = platform_get_drvdata(dev);
  180. int i;
  181. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  182. if (info->mtd[i]->suspend && info->mtd[i]->resume)
  183. if (info->mtd[i]->suspend(info->mtd[i]) == 0)
  184. info->mtd[i]->resume(info->mtd[i]);
  185. }
  186. #else
  187. #define physmap_flash_shutdown NULL
  188. #endif
  189. static struct platform_driver physmap_flash_driver = {
  190. .probe = physmap_flash_probe,
  191. .remove = physmap_flash_remove,
  192. .shutdown = physmap_flash_shutdown,
  193. .driver = {
  194. .name = "physmap-flash",
  195. .owner = THIS_MODULE,
  196. },
  197. };
  198. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  199. static struct physmap_flash_data physmap_flash_data = {
  200. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  201. };
  202. static struct resource physmap_flash_resource = {
  203. .start = CONFIG_MTD_PHYSMAP_START,
  204. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  205. .flags = IORESOURCE_MEM,
  206. };
  207. static struct platform_device physmap_flash = {
  208. .name = "physmap-flash",
  209. .id = 0,
  210. .dev = {
  211. .platform_data = &physmap_flash_data,
  212. },
  213. .num_resources = 1,
  214. .resource = &physmap_flash_resource,
  215. };
  216. void physmap_configure(unsigned long addr, unsigned long size,
  217. int bankwidth, void (*set_vpp)(struct map_info *, int))
  218. {
  219. physmap_flash_resource.start = addr;
  220. physmap_flash_resource.end = addr + size - 1;
  221. physmap_flash_data.width = bankwidth;
  222. physmap_flash_data.set_vpp = set_vpp;
  223. }
  224. #ifdef CONFIG_MTD_PARTITIONS
  225. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  226. {
  227. physmap_flash_data.nr_parts = num_parts;
  228. physmap_flash_data.parts = parts;
  229. }
  230. #endif
  231. #endif
  232. static int __init physmap_init(void)
  233. {
  234. int err;
  235. err = platform_driver_register(&physmap_flash_driver);
  236. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  237. if (err == 0) {
  238. err = platform_device_register(&physmap_flash);
  239. if (err)
  240. platform_driver_unregister(&physmap_flash_driver);
  241. }
  242. #endif
  243. return err;
  244. }
  245. static void __exit physmap_exit(void)
  246. {
  247. #ifdef CONFIG_MTD_PHYSMAP_COMPAT
  248. platform_device_unregister(&physmap_flash);
  249. #endif
  250. platform_driver_unregister(&physmap_flash_driver);
  251. }
  252. module_init(physmap_init);
  253. module_exit(physmap_exit);
  254. MODULE_LICENSE("GPL");
  255. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  256. MODULE_DESCRIPTION("Generic configurable MTD map driver");
  257. /* legacy platform drivers can't hotplug or coldplg */
  258. #ifndef CONFIG_MTD_PHYSMAP_COMPAT
  259. /* work with hotplug and coldplug */
  260. MODULE_ALIAS("platform:physmap-flash");
  261. #endif