plat-ram.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.3 2005/03/19 22:41:27 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/mtd/mtd.h>
  33. #include <linux/mtd/map.h>
  34. #include <linux/mtd/partitions.h>
  35. #include <linux/mtd/plat-ram.h>
  36. #include <asm/io.h>
  37. /* private structure for each mtd platform ram device created */
  38. struct platram_info {
  39. struct device *dev;
  40. struct mtd_info *mtd;
  41. struct map_info map;
  42. struct mtd_partition *partitions;
  43. struct resource *area;
  44. struct platdata_mtd_ram *pdata;
  45. };
  46. /* to_platram_info()
  47. *
  48. * device private data to struct platram_info conversion
  49. */
  50. static inline struct platram_info *to_platram_info(struct device *dev)
  51. {
  52. return (struct platram_info *)dev_get_drvdata(dev);
  53. }
  54. /* platram_setrw
  55. *
  56. * call the platform device's set rw/ro control
  57. *
  58. * to = 0 => read-only
  59. * = 1 => read-write
  60. */
  61. static inline void platram_setrw(struct platram_info *info, int to)
  62. {
  63. if (info->pdata == NULL)
  64. return;
  65. if (info->pdata->set_rw != NULL)
  66. (info->pdata->set_rw)(info->dev, to);
  67. }
  68. /* platram_remove
  69. *
  70. * called to remove the device from the driver's control
  71. */
  72. static int platram_remove(struct device *dev)
  73. {
  74. struct platram_info *info = to_platram_info(dev);
  75. dev_set_drvdata(dev, NULL);
  76. dev_dbg(dev, "removing device\n");
  77. if (info == NULL)
  78. return 0;
  79. if (info->mtd) {
  80. #ifdef CONFIG_MTD_PARTITIONS
  81. if (info->partitions) {
  82. del_mtd_partitions(info->mtd);
  83. kfree(info->partitions);
  84. }
  85. #endif
  86. del_mtd_device(info->mtd);
  87. map_destroy(info->mtd);
  88. }
  89. /* ensure ram is left read-only */
  90. platram_setrw(info, PLATRAM_RO);
  91. /* release resources */
  92. if (info->area) {
  93. release_resource(info->area);
  94. kfree(info->area);
  95. }
  96. if (info->map.virt != NULL)
  97. iounmap(info->map.virt);
  98. kfree(info);
  99. return 0;
  100. }
  101. /* platram_probe
  102. *
  103. * called from device drive system when a device matching our
  104. * driver is found.
  105. */
  106. static int platram_probe(struct device *dev)
  107. {
  108. struct platform_device *pd = to_platform_device(dev);
  109. struct platdata_mtd_ram *pdata;
  110. struct platram_info *info;
  111. struct resource *res;
  112. int err = 0;
  113. dev_dbg(dev, "probe entered\n");
  114. if (dev->platform_data == NULL) {
  115. dev_err(dev, "no platform data supplied\n");
  116. err = -ENOENT;
  117. goto exit_error;
  118. }
  119. pdata = dev->platform_data;
  120. info = kmalloc(sizeof(*info), GFP_KERNEL);
  121. if (info == NULL) {
  122. dev_err(dev, "no memory for flash info\n");
  123. err = -ENOMEM;
  124. goto exit_error;
  125. }
  126. memset(info, 0, sizeof(*info));
  127. dev_set_drvdata(dev, info);
  128. info->dev = dev;
  129. info->pdata = pdata;
  130. /* get the resource for the memory mapping */
  131. res = platform_get_resource(pd, IORESOURCE_MEM, 0);
  132. if (res == NULL) {
  133. dev_err(dev, "no memory resource specified\n");
  134. err = -ENOENT;
  135. goto exit_free;
  136. }
  137. dev_dbg(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 : pd->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, pd->name);
  145. if (info->area == NULL) {
  146. dev_err(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(dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  153. if (info->map.virt == NULL) {
  154. dev_err(dev, "failed to ioremap() region\n");
  155. err = -EIO;
  156. goto exit_free;
  157. }
  158. simple_map_init(&info->map);
  159. dev_dbg(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(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(dev, "add_mtd_device() failed\n");
  186. err = -ENOMEM;
  187. }
  188. dev_info(dev, "registered mtd device\n");
  189. return err;
  190. exit_free:
  191. platram_remove(dev);
  192. exit_error:
  193. return err;
  194. }
  195. /* device driver info */
  196. static struct device_driver platram_driver = {
  197. .name = "mtd-ram",
  198. .bus = &platform_bus_type,
  199. .probe = platram_probe,
  200. .remove = platram_remove,
  201. };
  202. /* module init/exit */
  203. static int __init platram_init(void)
  204. {
  205. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  206. return driver_register(&platram_driver);
  207. }
  208. static void __exit platram_exit(void)
  209. {
  210. driver_unregister(&platram_driver);
  211. }
  212. module_init(platram_init);
  213. module_exit(platram_exit);
  214. MODULE_LICENSE("GPL");
  215. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  216. MODULE_DESCRIPTION("MTD platform RAM map driver");