physmap.c 7.8 KB

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