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