plat-ram.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 = kzalloc(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. platform_set_drvdata(pdev, info);
  128. info->dev = &pdev->dev;
  129. info->pdata = pdata;
  130. /* get the resource for the memory mapping */
  131. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  132. if (res == NULL) {
  133. dev_err(&pdev->dev, "no memory resource specified\n");
  134. err = -ENOENT;
  135. goto exit_free;
  136. }
  137. dev_dbg(&pdev->dev, "got platform resource %p (0x%lx)\n", res, res->start);
  138. /* setup map parameters */
  139. info->map.phys = res->start;
  140. info->map.size = (res->end - res->start) + 1;
  141. info->map.name = pdata->mapname != NULL ? pdata->mapname : (char *)pdev->name;
  142. info->map.bankwidth = pdata->bankwidth;
  143. /* register our usage of the memory area */
  144. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  145. if (info->area == NULL) {
  146. dev_err(&pdev->dev, "failed to request memory region\n");
  147. err = -EIO;
  148. goto exit_free;
  149. }
  150. /* remap the memory area */
  151. info->map.virt = ioremap(res->start, info->map.size);
  152. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  153. if (info->map.virt == NULL) {
  154. dev_err(&pdev->dev, "failed to ioremap() region\n");
  155. err = -EIO;
  156. goto exit_free;
  157. }
  158. simple_map_init(&info->map);
  159. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  160. /* probe for the right mtd map driver */
  161. info->mtd = do_map_probe("map_ram" , &info->map);
  162. if (info->mtd == NULL) {
  163. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  164. err = -ENOMEM;
  165. goto exit_free;
  166. }
  167. info->mtd->owner = THIS_MODULE;
  168. platram_setrw(info, PLATRAM_RW);
  169. /* check to see if there are any available partitions, or wether
  170. * to add this device whole */
  171. #ifdef CONFIG_MTD_PARTITIONS
  172. if (pdata->nr_partitions > 0) {
  173. const char **probes = { NULL };
  174. if (pdata->probes)
  175. probes = (const char **)pdata->probes;
  176. err = parse_mtd_partitions(info->mtd, probes,
  177. &info->partitions, 0);
  178. if (err > 0) {
  179. err = add_mtd_partitions(info->mtd, info->partitions,
  180. err);
  181. }
  182. }
  183. #endif /* CONFIG_MTD_PARTITIONS */
  184. if (add_mtd_device(info->mtd)) {
  185. dev_err(&pdev->dev, "add_mtd_device() failed\n");
  186. err = -ENOMEM;
  187. }
  188. dev_info(&pdev->dev, "registered mtd device\n");
  189. return err;
  190. exit_free:
  191. platram_remove(pdev);
  192. exit_error:
  193. return err;
  194. }
  195. /* device driver info */
  196. static struct platform_driver platram_driver = {
  197. .probe = platram_probe,
  198. .remove = platram_remove,
  199. .driver = {
  200. .name = "mtd-ram",
  201. .owner = THIS_MODULE,
  202. },
  203. };
  204. /* module init/exit */
  205. static int __init platram_init(void)
  206. {
  207. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  208. return platform_driver_register(&platram_driver);
  209. }
  210. static void __exit platram_exit(void)
  211. {
  212. platform_driver_unregister(&platram_driver);
  213. }
  214. module_init(platram_init);
  215. module_exit(platram_exit);
  216. MODULE_LICENSE("GPL");
  217. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  218. MODULE_DESCRIPTION("MTD platform RAM map driver");