plat-ram.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/string.h>
  28. #include <linux/ioport.h>
  29. #include <linux/device.h>
  30. #include <linux/slab.h>
  31. #include <linux/platform_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. bool free_partitions;
  44. struct resource *area;
  45. struct platdata_mtd_ram *pdata;
  46. };
  47. /* to_platram_info()
  48. *
  49. * device private data to struct platram_info conversion
  50. */
  51. static inline struct platram_info *to_platram_info(struct platform_device *dev)
  52. {
  53. return (struct platram_info *)platform_get_drvdata(dev);
  54. }
  55. /* platram_setrw
  56. *
  57. * call the platform device's set rw/ro control
  58. *
  59. * to = 0 => read-only
  60. * = 1 => read-write
  61. */
  62. static inline void platram_setrw(struct platram_info *info, int to)
  63. {
  64. if (info->pdata == NULL)
  65. return;
  66. if (info->pdata->set_rw != NULL)
  67. (info->pdata->set_rw)(info->dev, to);
  68. }
  69. /* platram_remove
  70. *
  71. * called to remove the device from the driver's control
  72. */
  73. static int platram_remove(struct platform_device *pdev)
  74. {
  75. struct platram_info *info = to_platram_info(pdev);
  76. platform_set_drvdata(pdev, NULL);
  77. dev_dbg(&pdev->dev, "removing device\n");
  78. if (info == NULL)
  79. return 0;
  80. if (info->mtd) {
  81. #ifdef CONFIG_MTD_PARTITIONS
  82. if (info->partitions) {
  83. del_mtd_partitions(info->mtd);
  84. if (info->free_partitions)
  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%llx)\n", res,
  138. (unsigned long long)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 ?
  143. (char *)pdata->mapname : (char *)pdev->name;
  144. info->map.bankwidth = pdata->bankwidth;
  145. /* register our usage of the memory area */
  146. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  147. if (info->area == NULL) {
  148. dev_err(&pdev->dev, "failed to request memory region\n");
  149. err = -EIO;
  150. goto exit_free;
  151. }
  152. /* remap the memory area */
  153. info->map.virt = ioremap(res->start, info->map.size);
  154. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  155. if (info->map.virt == NULL) {
  156. dev_err(&pdev->dev, "failed to ioremap() region\n");
  157. err = -EIO;
  158. goto exit_free;
  159. }
  160. simple_map_init(&info->map);
  161. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  162. /* probe for the right mtd map driver
  163. * supplied by the platform_data struct */
  164. if (pdata->map_probes) {
  165. const char **map_probes = pdata->map_probes;
  166. for ( ; !info->mtd && *map_probes; map_probes++)
  167. info->mtd = do_map_probe(*map_probes , &info->map);
  168. }
  169. /* fallback to map_ram */
  170. else
  171. info->mtd = do_map_probe("map_ram", &info->map);
  172. if (info->mtd == NULL) {
  173. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  174. err = -ENOMEM;
  175. goto exit_free;
  176. }
  177. info->mtd->owner = THIS_MODULE;
  178. platram_setrw(info, PLATRAM_RW);
  179. /* check to see if there are any available partitions, or wether
  180. * to add this device whole */
  181. #ifdef CONFIG_MTD_PARTITIONS
  182. if (!pdata->nr_partitions) {
  183. /* try to probe using the supplied probe type */
  184. if (pdata->probes) {
  185. err = parse_mtd_partitions(info->mtd, pdata->probes,
  186. &info->partitions, 0);
  187. info->free_partitions = 1;
  188. if (err > 0)
  189. err = add_mtd_partitions(info->mtd,
  190. info->partitions, err);
  191. }
  192. }
  193. /* use the static mapping */
  194. else
  195. err = add_mtd_partitions(info->mtd, pdata->partitions,
  196. pdata->nr_partitions);
  197. #endif /* CONFIG_MTD_PARTITIONS */
  198. if (add_mtd_device(info->mtd)) {
  199. dev_err(&pdev->dev, "add_mtd_device() failed\n");
  200. err = -ENOMEM;
  201. }
  202. if (!err)
  203. dev_info(&pdev->dev, "registered mtd device\n");
  204. return err;
  205. exit_free:
  206. platram_remove(pdev);
  207. exit_error:
  208. return err;
  209. }
  210. /* device driver info */
  211. /* work with hotplug and coldplug */
  212. MODULE_ALIAS("platform:mtd-ram");
  213. static struct platform_driver platram_driver = {
  214. .probe = platram_probe,
  215. .remove = platram_remove,
  216. .driver = {
  217. .name = "mtd-ram",
  218. .owner = THIS_MODULE,
  219. },
  220. };
  221. /* module init/exit */
  222. static int __init platram_init(void)
  223. {
  224. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  225. return platform_driver_register(&platram_driver);
  226. }
  227. static void __exit platram_exit(void)
  228. {
  229. platform_driver_unregister(&platram_driver);
  230. }
  231. module_init(platram_init);
  232. module_exit(platram_exit);
  233. MODULE_LICENSE("GPL");
  234. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  235. MODULE_DESCRIPTION("MTD platform RAM map driver");