plat-ram.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 platform 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. mtd_device_unregister(info->mtd);
  82. if (info->partitions) {
  83. if (info->free_partitions)
  84. kfree(info->partitions);
  85. }
  86. map_destroy(info->mtd);
  87. }
  88. /* ensure ram is left read-only */
  89. platram_setrw(info, PLATRAM_RO);
  90. /* release resources */
  91. if (info->area) {
  92. release_resource(info->area);
  93. kfree(info->area);
  94. }
  95. if (info->map.virt != NULL)
  96. iounmap(info->map.virt);
  97. kfree(info);
  98. return 0;
  99. }
  100. /* platram_probe
  101. *
  102. * called from device drive system when a device matching our
  103. * driver is found.
  104. */
  105. static int platram_probe(struct platform_device *pdev)
  106. {
  107. struct platdata_mtd_ram *pdata;
  108. struct platram_info *info;
  109. struct resource *res;
  110. int err = 0;
  111. dev_dbg(&pdev->dev, "probe entered\n");
  112. if (pdev->dev.platform_data == NULL) {
  113. dev_err(&pdev->dev, "no platform data supplied\n");
  114. err = -ENOENT;
  115. goto exit_error;
  116. }
  117. pdata = pdev->dev.platform_data;
  118. info = kzalloc(sizeof(*info), GFP_KERNEL);
  119. if (info == NULL) {
  120. dev_err(&pdev->dev, "no memory for flash info\n");
  121. err = -ENOMEM;
  122. goto exit_error;
  123. }
  124. platform_set_drvdata(pdev, info);
  125. info->dev = &pdev->dev;
  126. info->pdata = pdata;
  127. /* get the resource for the memory mapping */
  128. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  129. if (res == NULL) {
  130. dev_err(&pdev->dev, "no memory resource specified\n");
  131. err = -ENOENT;
  132. goto exit_free;
  133. }
  134. dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
  135. (unsigned long long)res->start);
  136. /* setup map parameters */
  137. info->map.phys = res->start;
  138. info->map.size = resource_size(res);
  139. info->map.name = pdata->mapname != NULL ?
  140. (char *)pdata->mapname : (char *)pdev->name;
  141. info->map.bankwidth = pdata->bankwidth;
  142. /* register our usage of the memory area */
  143. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  144. if (info->area == NULL) {
  145. dev_err(&pdev->dev, "failed to request memory region\n");
  146. err = -EIO;
  147. goto exit_free;
  148. }
  149. /* remap the memory area */
  150. info->map.virt = ioremap(res->start, info->map.size);
  151. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  152. if (info->map.virt == NULL) {
  153. dev_err(&pdev->dev, "failed to ioremap() region\n");
  154. err = -EIO;
  155. goto exit_free;
  156. }
  157. simple_map_init(&info->map);
  158. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  159. /* probe for the right mtd map driver
  160. * supplied by the platform_data struct */
  161. if (pdata->map_probes) {
  162. const char **map_probes = pdata->map_probes;
  163. for ( ; !info->mtd && *map_probes; map_probes++)
  164. info->mtd = do_map_probe(*map_probes , &info->map);
  165. }
  166. /* fallback to map_ram */
  167. else
  168. info->mtd = do_map_probe("map_ram", &info->map);
  169. if (info->mtd == NULL) {
  170. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  171. err = -ENOMEM;
  172. goto exit_free;
  173. }
  174. info->mtd->owner = THIS_MODULE;
  175. info->mtd->dev.parent = &pdev->dev;
  176. platram_setrw(info, PLATRAM_RW);
  177. /* check to see if there are any available partitions, or wether
  178. * to add this device whole */
  179. if (!pdata->nr_partitions) {
  180. /* try to probe using the supplied probe type */
  181. if (pdata->probes) {
  182. err = parse_mtd_partitions(info->mtd, pdata->probes,
  183. &info->partitions, 0);
  184. info->free_partitions = 1;
  185. if (err > 0)
  186. err = mtd_device_register(info->mtd,
  187. info->partitions, err);
  188. }
  189. }
  190. /* use the static mapping */
  191. else
  192. err = mtd_device_register(info->mtd, pdata->partitions,
  193. pdata->nr_partitions);
  194. if (!err)
  195. dev_info(&pdev->dev, "registered mtd device\n");
  196. /* add the whole device. */
  197. err = mtd_device_register(info->mtd, NULL, 0);
  198. if (err)
  199. dev_err(&pdev->dev, "failed to register the entire device\n");
  200. return err;
  201. exit_free:
  202. platram_remove(pdev);
  203. exit_error:
  204. return err;
  205. }
  206. /* device driver info */
  207. /* work with hotplug and coldplug */
  208. MODULE_ALIAS("platform:mtd-ram");
  209. static struct platform_driver platram_driver = {
  210. .probe = platram_probe,
  211. .remove = platram_remove,
  212. .driver = {
  213. .name = "mtd-ram",
  214. .owner = THIS_MODULE,
  215. },
  216. };
  217. /* module init/exit */
  218. static int __init platram_init(void)
  219. {
  220. printk("Generic platform RAM MTD, (c) 2004 Simtec Electronics\n");
  221. return platform_driver_register(&platram_driver);
  222. }
  223. static void __exit platram_exit(void)
  224. {
  225. platform_driver_unregister(&platram_driver);
  226. }
  227. module_init(platram_init);
  228. module_exit(platram_exit);
  229. MODULE_LICENSE("GPL");
  230. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  231. MODULE_DESCRIPTION("MTD platform RAM map driver");