physmap.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 <asm/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. struct resource *res;
  28. #ifdef CONFIG_MTD_PARTITIONS
  29. int nr_parts;
  30. struct mtd_partition *parts;
  31. #endif
  32. };
  33. static int physmap_flash_remove(struct platform_device *dev)
  34. {
  35. struct physmap_flash_info *info;
  36. struct physmap_flash_data *physmap_data;
  37. int i;
  38. info = platform_get_drvdata(dev);
  39. if (info == NULL)
  40. return 0;
  41. platform_set_drvdata(dev, NULL);
  42. physmap_data = dev->dev.platform_data;
  43. #ifdef CONFIG_MTD_CONCAT
  44. if (info->cmtd != info->mtd[0]) {
  45. del_mtd_device(info->cmtd);
  46. mtd_concat_destroy(info->cmtd);
  47. }
  48. #endif
  49. for (i = 0; i < MAX_RESOURCES; i++) {
  50. if (info->mtd[i] != NULL) {
  51. #ifdef CONFIG_MTD_PARTITIONS
  52. if (info->nr_parts) {
  53. del_mtd_partitions(info->mtd[i]);
  54. kfree(info->parts);
  55. } else if (physmap_data->nr_parts) {
  56. del_mtd_partitions(info->mtd[i]);
  57. } else {
  58. del_mtd_device(info->mtd[i]);
  59. }
  60. #else
  61. del_mtd_device(info->mtd[i]);
  62. #endif
  63. map_destroy(info->mtd[i]);
  64. }
  65. if (info->map[i].virt != NULL)
  66. iounmap(info->map[i].virt);
  67. }
  68. if (info->res != NULL) {
  69. release_resource(info->res);
  70. kfree(info->res);
  71. }
  72. return 0;
  73. }
  74. static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", "map_rom", NULL };
  75. #ifdef CONFIG_MTD_PARTITIONS
  76. static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", 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 = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  90. if (info == NULL) {
  91. err = -ENOMEM;
  92. goto err_out;
  93. }
  94. platform_set_drvdata(dev, info);
  95. for (i = 0; i < dev->num_resources; i++) {
  96. printk(KERN_NOTICE "physmap platform flash device: %.8llx at %.8llx\n",
  97. (unsigned long long)(dev->resource[i].end - dev->resource[i].start + 1),
  98. (unsigned long long)dev->resource[i].start);
  99. info->res = request_mem_region(dev->resource[i].start,
  100. dev->resource[i].end - dev->resource[i].start + 1,
  101. dev->dev.bus_id);
  102. if (info->res == NULL) {
  103. dev_err(&dev->dev, "Could not reserve memory region\n");
  104. err = -ENOMEM;
  105. goto err_out;
  106. }
  107. info->map[i].name = dev->dev.bus_id;
  108. info->map[i].phys = dev->resource[i].start;
  109. info->map[i].size = dev->resource[i].end - dev->resource[i].start + 1;
  110. info->map[i].bankwidth = physmap_data->width;
  111. info->map[i].set_vpp = physmap_data->set_vpp;
  112. info->map[i].virt = ioremap(info->map[i].phys, 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. }
  131. if (devices_found == 1) {
  132. info->cmtd = info->mtd[0];
  133. } else if (devices_found > 1) {
  134. /*
  135. * We detected multiple devices. Concatenate them together.
  136. */
  137. #ifdef CONFIG_MTD_CONCAT
  138. info->cmtd = mtd_concat_create(info->mtd, devices_found, dev->dev.bus_id);
  139. if (info->cmtd == NULL)
  140. err = -ENXIO;
  141. #else
  142. printk(KERN_ERR "physmap-flash: multiple devices "
  143. "found but MTD concat support disabled.\n");
  144. err = -ENXIO;
  145. #endif
  146. }
  147. if (err)
  148. goto err_out;
  149. #ifdef CONFIG_MTD_PARTITIONS
  150. err = parse_mtd_partitions(info->cmtd, part_probe_types, &info->parts, 0);
  151. if (err > 0) {
  152. add_mtd_partitions(info->cmtd, info->parts, err);
  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_LEN
  224. #if CONFIG_MTD_PHYSMAP_LEN != 0
  225. #warning using PHYSMAP compat code
  226. #define PHYSMAP_COMPAT
  227. #endif
  228. #endif
  229. #ifdef PHYSMAP_COMPAT
  230. static struct physmap_flash_data physmap_flash_data = {
  231. .width = CONFIG_MTD_PHYSMAP_BANKWIDTH,
  232. };
  233. static struct resource physmap_flash_resource = {
  234. .start = CONFIG_MTD_PHYSMAP_START,
  235. .end = CONFIG_MTD_PHYSMAP_START + CONFIG_MTD_PHYSMAP_LEN - 1,
  236. .flags = IORESOURCE_MEM,
  237. };
  238. static struct platform_device physmap_flash = {
  239. .name = "physmap-flash",
  240. .id = 0,
  241. .dev = {
  242. .platform_data = &physmap_flash_data,
  243. },
  244. .num_resources = 1,
  245. .resource = &physmap_flash_resource,
  246. };
  247. void physmap_configure(unsigned long addr, unsigned long size,
  248. int bankwidth, void (*set_vpp)(struct map_info *, int))
  249. {
  250. physmap_flash_resource.start = addr;
  251. physmap_flash_resource.end = addr + size - 1;
  252. physmap_flash_data.width = bankwidth;
  253. physmap_flash_data.set_vpp = set_vpp;
  254. }
  255. #ifdef CONFIG_MTD_PARTITIONS
  256. void physmap_set_partitions(struct mtd_partition *parts, int num_parts)
  257. {
  258. physmap_flash_data.nr_parts = num_parts;
  259. physmap_flash_data.parts = parts;
  260. }
  261. #endif
  262. #endif
  263. static int __init physmap_init(void)
  264. {
  265. int err;
  266. err = platform_driver_register(&physmap_flash_driver);
  267. #ifdef PHYSMAP_COMPAT
  268. if (err == 0)
  269. platform_device_register(&physmap_flash);
  270. #endif
  271. return err;
  272. }
  273. static void __exit physmap_exit(void)
  274. {
  275. #ifdef PHYSMAP_COMPAT
  276. platform_device_unregister(&physmap_flash);
  277. #endif
  278. platform_driver_unregister(&physmap_flash_driver);
  279. }
  280. module_init(physmap_init);
  281. module_exit(physmap_exit);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  284. MODULE_DESCRIPTION("Generic configurable MTD map driver");
  285. /* legacy platform drivers can't hotplug or coldplg */
  286. #ifndef PHYSMAP_COMPAT
  287. /* work with hotplug and coldplug */
  288. MODULE_ALIAS("platform:physmap-flash");
  289. #endif