physmap.c 7.8 KB

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