physmap.c 7.9 KB

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