physmap.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #ifdef CONFIG_MTD_CONCAT
  43. if (info->cmtd != info->mtd[0]) {
  44. del_mtd_device(info->cmtd);
  45. mtd_concat_destroy(info->cmtd);
  46. }
  47. #endif
  48. for (i = 0; i < MAX_RESOURCES; i++) {
  49. if (info->mtd[i] != NULL) {
  50. #ifdef CONFIG_MTD_PARTITIONS
  51. if (info->nr_parts) {
  52. del_mtd_partitions(info->mtd[i]);
  53. kfree(info->parts);
  54. } else if (physmap_data->nr_parts) {
  55. del_mtd_partitions(info->mtd[i]);
  56. } else {
  57. del_mtd_device(info->mtd[i]);
  58. }
  59. #else
  60. del_mtd_device(info->mtd[i]);
  61. #endif
  62. map_destroy(info->mtd[i]);
  63. }
  64. }
  65. return 0;
  66. }
  67. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
  68. #ifdef CONFIG_MTD_PARTITIONS
  69. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
  70. #endif
  71. static int physmap_flash_probe(struct platform_device *dev)
  72. {
  73. struct physmap_flash_data *physmap_data;
  74. struct physmap_flash_info *info;
  75. const char **probe_type;
  76. int err = 0;
  77. int i;
  78. int devices_found = 0;
  79. physmap_data = dev->dev.platform_data;
  80. if (physmap_data == NULL)
  81. return -ENODEV;
  82. info = devm_kzalloc(&dev->dev, sizeof(struct physmap_flash_info),
  83. GFP_KERNEL);
  84. if (info == NULL) {
  85. err = -ENOMEM;
  86. goto err_out;
  87. }
  88. platform_set_drvdata(dev, info);
  89. for (i = 0; i < dev->num_resources; i++) {
  90. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  91. (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1),
  92. (unsigned long long)dev->resource[i].start);
  93. if (!devm_request_mem_region(&dev->dev,
  94. dev->resource[i].start,
  95. dev->resource[i].end - dev->resource[i].start + 1,
  96. dev->dev.bus_id)) {
  97. dev_err(&dev->dev, "Could not reserve memory region\n");
  98. err = -ENOMEM;
  99. goto err_out;
  100. }
  101. info->map[i].name = dev->dev.bus_id;
  102. info->map[i].phys = dev->resource[i].start;
  103. info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1;
  104. info->map[i].bankwidth = physmap_data->width;
  105. info->map[i].set_vpp = physmap_data->set_vpp;
  106. info->map[i].virt = devm_ioremap(&dev->dev, info->map[i].phys,
  107. info->map[i].size);
  108. if (info->map[i].virt == NULL) {
  109. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  110. err = EIO;
  111. goto err_out;
  112. }
  113. simple_map_init(&info->map[i]);
  114. probe_type = rom_probe_types;
  115. for (; info->mtd[i] == NULL && *probe_type != NULL; probe_type++)
  116. info->mtd[i] = do_map_probe(*probe_type, &info->map[i]);
  117. if (info->mtd[i] == NULL) {
  118. dev_err(&dev->dev, "map_probe failed\n");
  119. err = -ENXIO;
  120. goto err_out;
  121. } else {
  122. devices_found++;
  123. }
  124. info->mtd[i]->owner = THIS_MODULE;
  125. }
  126. if (devices_found == 1) {
  127. info->cmtd = info->mtd[0];
  128. } else if (devices_found > 1) {
  129. /*
  130. * We detected multiple devices. Concatenate them together.
  131. */
  132. #ifdef CONFIG_MTD_CONCAT
  133. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev->dev.bus_id);
  134. if (info->cmtd == NULL)
  135. err = -ENXIO;
  136. #else
  137. printk(KERN_ERR "physmap-flash: multiple devices "
  138. "found but MTD concat support disabled.\n");
  139. err = -ENXIO;
  140. #endif
  141. }
  142. if (err)
  143. goto err_out;
  144. #ifdef CONFIG_MTD_PARTITIONS
  145. err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0);
  146. if (err > 0) {
  147. add_mtd_partitions(info->cmtd, info->parts, err);
  148. return 0;
  149. }
  150. if (physmap_data->nr_parts) {
  151. printk(KERN_NOTICE "Using physmap partition information\n");
  152. add_mtd_partitions(info->cmtd, physmap_data->parts,
  153. physmap_data->nr_parts);
  154. return 0;
  155. }
  156. #endif
  157. add_mtd_device(info->cmtd);
  158. return 0;
  159. err_out:
  160. physmap_flash_remove(dev);
  161. return err;
  162. }
  163. #ifdef CONFIG_PM
  164. static int physmap_flash_suspend(struct platform_device *dev, pm_message_t state)
  165. {
  166. struct physmap_flash_info *info = platform_get_drvdata(dev);
  167. int ret = 0;
  168. int i;
  169. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  170. if (info->mtd[i]->suspend) {
  171. ret = info->mtd[i]->suspend(info->mtd[i]);
  172. if (ret)
  173. goto fail;
  174. }
  175. return 0;
  176. fail:
  177. for (--i; i >= 0; --i)
  178. if (info->mtd[i]->suspend) {
  179. BUG_ON(!info->mtd[i]->resume);
  180. info->mtd[i]->resume(info->mtd[i]);
  181. }
  182. return ret;
  183. }
  184. static int physmap_flash_resume(struct platform_device *dev)
  185. {
  186. struct physmap_flash_info *info = platform_get_drvdata(dev);
  187. int i;
  188. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  189. if (info->mtd[i]->resume)
  190. info->mtd[i]->resume(info->mtd[i]);
  191. return 0;
  192. }
  193. static void physmap_flash_shutdown(struct platform_device *dev)
  194. {
  195. struct physmap_flash_info *info = platform_get_drvdata(dev);
  196. int i;
  197. for (i = 0; i < MAX_RESOURCES && info->mtd[i]; i++)
  198. if (info->mtd[i]->suspend && info->mtd[i]->resume)
  199. if (info->mtd[i]->suspend(info->mtd[i]) == 0)
  200. info->mtd[i]->resume(info->mtd[i]);
  201. }
  202. #else
  203. #define physmap_flash_suspend NULL
  204. #define physmap_flash_resume NULL
  205. #define physmap_flash_shutdown NULL
  206. #endif
  207. static struct platform_driver physmap_flash_driver = {
  208. .probe = physmap_flash_probe,
  209. .remove = physmap_flash_remove,
  210. .suspend = physmap_flash_suspend,
  211. .resume = physmap_flash_resume,
  212. .shutdown = physmap_flash_shutdown,
  213. .driver = {
  214. .name = "physmap-flash",
  215. .owner = THIS_MODULE,
  216. },
  217. };
  218. #ifdef CONFIG_MTD_PHYSMAP_LEN
  219. #if CONFIG_MTD_PHYSMAP_LEN != 0
  220. #warning using PHYSMAP compat code
  221. #define PHYSMAP_COMPAT
  222. #endif
  223. #endif
  224. #ifdef PHYSMAP_COMPAT
  225. static struct physmap_flash_data physmap_flash_data = {
  226. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  227. };
  228. static struct resource physmap_flash_resource = {
  229. .start = CONFIG_MTD_PHYSMAP_START,
  230. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  231. .flags = IORESOURCE_MEM,
  232. };
  233. static struct platform_device physmap_flash = {
  234. .name = "physmap-flash",
  235. .id = 0,
  236. .dev = {
  237. .platform_data = &physmap_flash_data,
  238. },
  239. .num_resources = 1,
  240. .resource = &physmap_flash_resource,
  241. };
  242. void physmap_configure(unsigned long addr, unsigned long size,
  243. int bankwidth, void (*set_vpp)(struct map_info *, int))
  244. {
  245. physmap_flash_resource.start = addr;
  246. physmap_flash_resource.end = addr + size - 1;
  247. physmap_flash_data.width = bankwidth;
  248. physmap_flash_data.set_vpp = set_vpp;
  249. }
  250. #ifdef CONFIG_MTD_PARTITIONS
  251. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  252. {
  253. physmap_flash_data.nr_parts = num_parts;
  254. physmap_flash_data.parts = parts;
  255. }
  256. #endif
  257. #endif
  258. static int __init physmap_init(void)
  259. {
  260. int err;
  261. err = platform_driver_register(&physmap_flash_driver);
  262. #ifdef PHYSMAP_COMPAT
  263. if (err == 0)
  264. platform_device_register(&physmap_flash);
  265. #endif
  266. return err;
  267. }
  268. static void __exit physmap_exit(void)
  269. {
  270. #ifdef PHYSMAP_COMPAT
  271. platform_device_unregister(&physmap_flash);
  272. #endif
  273. platform_driver_unregister(&physmap_flash_driver);
  274. }
  275. module_init(physmap_init);
  276. module_exit(physmap_exit);
  277. MODULE_LICENSE("GPL");
  278. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  279. MODULE_DESCRIPTION("Generic configurable MTD map driver");
  280. /* legacy platform drivers can't hotplug or coldplg */
  281. #ifndef PHYSMAP_COMPAT
  282. /* work with hotplug and coldplug */
  283. MODULE_ALIAS("platform:physmap-flash");
  284. #endif