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