plat-ram.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. struct resource *area;
  46. struct platdata_mtd_ram *pdata;
  47. };
  48. /* to_platram_info()
  49. *
  50. * device private data to struct platram_info conversion
  51. */
  52. static inline struct platram_info *to_platram_info(struct platform_device *dev)
  53. {
  54. return (struct platram_info *)platform_get_drvdata(dev);
  55. }
  56. /* platram_setrw
  57. *
  58. * call the platform device's set rw/ro control
  59. *
  60. * to = 0 => read-only
  61. * = 1 => read-write
  62. */
  63. static inline void platram_setrw(struct platram_info *info, int to)
  64. {
  65. if (info->pdata == NULL)
  66. return;
  67. if (info->pdata->set_rw != NULL)
  68. (info->pdata->set_rw)(info->dev, to);
  69. }
  70. /* platram_remove
  71. *
  72. * called to remove the device from the driver's control
  73. */
  74. static int platram_remove(struct platform_device *pdev)
  75. {
  76. struct platram_info *info = to_platram_info(pdev);
  77. platform_set_drvdata(pdev, NULL);
  78. dev_dbg(&pdev->dev, "removing device\n");
  79. if (info == NULL)
  80. return 0;
  81. if (info->mtd) {
  82. #ifdef CONFIG_MTD_PARTITIONS
  83. if (info->partitions) {
  84. del_mtd_partitions(info->mtd);
  85. kfree(info->partitions);
  86. }
  87. #endif
  88. del_mtd_device(info->mtd);
  89. map_destroy(info->mtd);
  90. }
  91. /* ensure ram is left read-only */
  92. platram_setrw(info, PLATRAM_RO);
  93. /* release resources */
  94. if (info->area) {
  95. release_resource(info->area);
  96. kfree(info->area);
  97. }
  98. if (info->map.virt != NULL)
  99. iounmap(info->map.virt);
  100. kfree(info);
  101. return 0;
  102. }
  103. /* platram_probe
  104. *
  105. * called from device drive system when a device matching our
  106. * driver is found.
  107. */
  108. static int platram_probe(struct platform_device *pdev)
  109. {
  110. struct platdata_mtd_ram *pdata;
  111. struct platram_info *info;
  112. struct resource *res;
  113. int err = 0;
  114. dev_dbg(&pdev->dev, "probe entered\n");
  115. if (pdev->dev.platform_data == NULL) {
  116. dev_err(&pdev->dev, "no platform data supplied\n");
  117. err = -ENOENT;
  118. goto exit_error;
  119. }
  120. pdata = pdev->dev.platform_data;
  121. info = kmalloc(sizeof(*info), GFP_KERNEL);
  122. if (info == NULL) {
  123. dev_err(&pdev->dev, "no memory for flash info\n");
  124. err = -ENOMEM;
  125. goto exit_error;
  126. }
  127. memset(info, 0, sizeof(*info));
  128. platform_set_drvdata(pdev, info);
  129. info->dev = &pdev->dev;
  130. info->pdata = pdata;
  131. /* get the resource for the memory mapping */
  132. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  133. if (res == NULL) {
  134. dev_err(&pdev->dev, "no memory resource specified\n");
  135. err = -ENOENT;
  136. goto exit_free;
  137. }
  138. dev_dbg(&pdev->dev, "got platform resource %p (0x%lx)\n", res, res->start);
  139. /* setup map parameters */
  140. info->map.phys = res->start;
  141. info->map.size = (res->end - res->start) + 1;
  142. info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pdev->name;
  143. info->map.bankwidth = pdata->bankwidth;
  144. /* register our usage of the memory area */
  145. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  146. if (info->area == NULL) {
  147. dev_err(&pdev->dev, "failed to request memory region\n");
  148. err = -EIO;
  149. goto exit_free;
  150. }
  151. /* remap the memory area */
  152. info->map.virt = ioremap(res->start, info->map.size);
  153. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  154. if (info->map.virt == NULL) {
  155. dev_err(&pdev->dev, "failed to ioremap() region\n");
  156. err = -EIO;
  157. goto exit_free;
  158. }
  159. simple_map_init(&info->map);
  160. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  161. /* probe for the right mtd map driver */
  162. info->mtd = do_map_probe("map_ram" , &info->map);
  163. if (info->mtd == NULL) {
  164. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  165. err = -ENOMEM;
  166. goto exit_free;
  167. }
  168. info->mtd->owner = THIS_MODULE;
  169. platram_setrw(info, PLATRAM_RW);
  170. /* check to see if there are any available partitions, or wether
  171. * to add this device whole */
  172. #ifdef CONFIG_MTD_PARTITIONS
  173. if (pdata->nr_partitions > 0) {
  174. const char **probes = { NULL };
  175. if (pdata->probes)
  176. probes = (const char **)pdata->probes;
  177. err = parse_mtd_partitions(info->mtd, probes,
  178. &info->partitions, 0);
  179. if (err > 0) {
  180. err = add_mtd_partitions(info->mtd, info->partitions,
  181. err);
  182. }
  183. }
  184. #endif /* CONFIG_MTD_PARTITIONS */
  185. if (add_mtd_device(info->mtd)) {
  186. dev_err(&pdev->dev, "add_mtd_device() failed\n");
  187. err = -ENOMEM;
  188. }
  189. dev_info(&pdev->dev, "registered mtd device\n");
  190. return err;
  191. exit_free:
  192. platram_remove(pdev);
  193. exit_error:
  194. return err;
  195. }
  196. /* device driver info */
  197. static struct platform_driver platram_driver = {
  198. .probe = platram_probe,
  199. .remove = platram_remove,
  200. .driver = {
  201. .name = "mtd-ram",
  202. .owner = THIS_MODULE,
  203. },
  204. };
  205. /* module init/exit */
  206. static int __init platram_init(void)
  207. {
  208. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  209. return platform_driver_register(&platram_driver);
  210. }
  211. static void __exit platram_exit(void)
  212. {
  213. platform_driver_unregister(&platram_driver);
  214. }
  215. module_init(platram_init);
  216. module_exit(platram_exit);
  217. MODULE_LICENSE("GPL");
  218. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  219. MODULE_DESCRIPTION("MTD platform RAM map driver");