plat-ram.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /* drivers/mtd/maps/plat-ram.c
  2. *
  3. * (c) 2004-2005 Simtec Electronics
  4. * http://www.simtec.co.uk/products/SWLINUX/
  5. * Ben Dooks <ben@simtec.co.uk>
  6. *
  7. * Generic platfrom device based RAM map
  8. *
  9. * $Id: plat-ram.c,v 1.7 2005/11/07 11:14:28 gleixner Exp $
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <linux/module.h>
  26. #include <linux/types.h>
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/string.h>
  30. #include <linux/ioport.h>
  31. #include <linux/device.h>
  32. #include <linux/slab.h>
  33. #include <linux/platform_device.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/map.h>
  36. #include <linux/mtd/partitions.h>
  37. #include <linux/mtd/plat-ram.h>
  38. #include <asm/io.h>
  39. /* private structure for each mtd platform ram device created */
  40. struct platram_info {
  41. struct device *dev;
  42. struct mtd_info *mtd;
  43. struct map_info map;
  44. struct mtd_partition *partitions;
  45. bool free_partitions;
  46. struct resource *area;
  47. struct platdata_mtd_ram *pdata;
  48. };
  49. /* to_platram_info()
  50. *
  51. * device private data to struct platram_info conversion
  52. */
  53. static inline struct platram_info *to_platram_info(struct platform_device *dev)
  54. {
  55. return (struct platram_info *)platform_get_drvdata(dev);
  56. }
  57. /* platram_setrw
  58. *
  59. * call the platform device's set rw/ro control
  60. *
  61. * to = 0 => read-only
  62. * = 1 => read-write
  63. */
  64. static inline void platram_setrw(struct platram_info *info, int to)
  65. {
  66. if (info->pdata == NULL)
  67. return;
  68. if (info->pdata->set_rw != NULL)
  69. (info->pdata->set_rw)(info->dev, to);
  70. }
  71. /* platram_remove
  72. *
  73. * called to remove the device from the driver's control
  74. */
  75. static int platram_remove(struct platform_device *pdev)
  76. {
  77. struct platram_info *info = to_platram_info(pdev);
  78. platform_set_drvdata(pdev, NULL);
  79. dev_dbg(&pdev->dev, "removing device\n");
  80. if (info == NULL)
  81. return 0;
  82. if (info->mtd) {
  83. #ifdef CONFIG_MTD_PARTITIONS
  84. if (info->partitions) {
  85. del_mtd_partitions(info->mtd);
  86. if (info->free_partitions)
  87. kfree(info->partitions);
  88. }
  89. #endif
  90. del_mtd_device(info->mtd);
  91. map_destroy(info->mtd);
  92. }
  93. /* ensure ram is left read-only */
  94. platram_setrw(info, PLATRAM_RO);
  95. /* release resources */
  96. if (info->area) {
  97. release_resource(info->area);
  98. kfree(info->area);
  99. }
  100. if (info->map.virt != NULL)
  101. iounmap(info->map.virt);
  102. kfree(info);
  103. return 0;
  104. }
  105. /* platram_probe
  106. *
  107. * called from device drive system when a device matching our
  108. * driver is found.
  109. */
  110. static int platram_probe(struct platform_device *pdev)
  111. {
  112. struct platdata_mtd_ram *pdata;
  113. struct platram_info *info;
  114. struct resource *res;
  115. int err = 0;
  116. dev_dbg(&pdev->dev, "probe entered\n");
  117. if (pdev->dev.platform_data == NULL) {
  118. dev_err(&pdev->dev, "no platform data supplied\n");
  119. err = -ENOENT;
  120. goto exit_error;
  121. }
  122. pdata = pdev->dev.platform_data;
  123. info = kzalloc(sizeof(*info), GFP_KERNEL);
  124. if (info == NULL) {
  125. dev_err(&pdev->dev, "no memory for flash info\n");
  126. err = -ENOMEM;
  127. goto exit_error;
  128. }
  129. platform_set_drvdata(pdev, info);
  130. info->dev = &pdev->dev;
  131. info->pdata = pdata;
  132. /* get the resource for the memory mapping */
  133. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  134. if (res == NULL) {
  135. dev_err(&pdev->dev, "no memory resource specified\n");
  136. err = -ENOENT;
  137. goto exit_free;
  138. }
  139. dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
  140. (unsigned long long)res->start);
  141. /* setup map parameters */
  142. info->map.phys = res->start;
  143. info->map.size = (res->end - res->start) + 1;
  144. info->map.name = pdata->mapname != NULL ?
  145. (char *)pdata->mapname : (char *)pdev->name;
  146. info->map.bankwidth = pdata->bankwidth;
  147. /* register our usage of the memory area */
  148. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  149. if (info->area == NULL) {
  150. dev_err(&pdev->dev, "failed to request memory region\n");
  151. err = -EIO;
  152. goto exit_free;
  153. }
  154. /* remap the memory area */
  155. info->map.virt = ioremap(res->start, info->map.size);
  156. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  157. if (info->map.virt == NULL) {
  158. dev_err(&pdev->dev, "failed to ioremap() region\n");
  159. err = -EIO;
  160. goto exit_free;
  161. }
  162. simple_map_init(&info->map);
  163. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  164. /* probe for the right mtd map driver
  165. * supplied by the platform_data struct */
  166. if (pdata->map_probes) {
  167. const char **map_probes = pdata->map_probes;
  168. for ( ; !info->mtd && *map_probes; map_probes++)
  169. info->mtd = do_map_probe(*map_probes , &info->map);
  170. }
  171. /* fallback to map_ram */
  172. else
  173. info->mtd = do_map_probe("map_ram", &info->map);
  174. if (info->mtd == NULL) {
  175. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  176. err = -ENOMEM;
  177. goto exit_free;
  178. }
  179. info->mtd->owner = THIS_MODULE;
  180. platram_setrw(info, PLATRAM_RW);
  181. /* check to see if there are any available partitions, or wether
  182. * to add this device whole */
  183. #ifdef CONFIG_MTD_PARTITIONS
  184. if (!pdata->nr_partitions) {
  185. /* try to probe using the supplied probe type */
  186. if (pdata->probes) {
  187. err = parse_mtd_partitions(info->mtd, pdata->probes,
  188. &info->partitions, 0);
  189. info->free_partitions = 1;
  190. if (err > 0)
  191. err = add_mtd_partitions(info->mtd,
  192. info->partitions, err);
  193. }
  194. }
  195. /* use the static mapping */
  196. else
  197. err = add_mtd_partitions(info->mtd, pdata->partitions,
  198. pdata->nr_partitions);
  199. #endif /* CONFIG_MTD_PARTITIONS */
  200. if (add_mtd_device(info->mtd)) {
  201. dev_err(&pdev->dev, "add_mtd_device() failed\n");
  202. err = -ENOMEM;
  203. }
  204. if (!err)
  205. dev_info(&pdev->dev, "registered mtd device\n");
  206. return err;
  207. exit_free:
  208. platram_remove(pdev);
  209. exit_error:
  210. return err;
  211. }
  212. /* device driver info */
  213. /* work with hotplug and coldplug */
  214. MODULE_ALIAS("platform:mtd-ram");
  215. static struct platform_driver platram_driver = {
  216. .probe = platram_probe,
  217. .remove = platram_remove,
  218. .driver = {
  219. .name = "mtd-ram",
  220. .owner = THIS_MODULE,
  221. },
  222. };
  223. /* module init/exit */
  224. static int __init platram_init(void)
  225. {
  226. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  227. return platform_driver_register(&platram_driver);
  228. }
  229. static void __exit platram_exit(void)
  230. {
  231. platform_driver_unregister(&platram_driver);
  232. }
  233. module_init(platram_init);
  234. module_exit(platram_exit);
  235. MODULE_LICENSE("GPL");
  236. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  237. MODULE_DESCRIPTION("MTD platform RAM map driver");