plat-ram.c 6.3 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 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. dev_dbg(&pdev->dev, "removing device\n");
  75. if (info == NULL)
  76. return 0;
  77. if (info->mtd) {
  78. mtd_device_unregister(info->mtd);
  79. map_destroy(info->mtd);
  80. }
  81. /* ensure ram is left read-only */
  82. platram_setrw(info, PLATRAM_RO);
  83. /* release resources */
  84. if (info->area) {
  85. release_resource(info->area);
  86. kfree(info->area);
  87. }
  88. if (info->map.virt != NULL)
  89. iounmap(info->map.virt);
  90. kfree(info);
  91. return 0;
  92. }
  93. /* platram_probe
  94. *
  95. * called from device drive system when a device matching our
  96. * driver is found.
  97. */
  98. static int platram_probe(struct platform_device *pdev)
  99. {
  100. struct platdata_mtd_ram *pdata;
  101. struct platram_info *info;
  102. struct resource *res;
  103. int err = 0;
  104. dev_dbg(&pdev->dev, "probe entered\n");
  105. if (dev_get_platdata(&pdev->dev) == NULL) {
  106. dev_err(&pdev->dev, "no platform data supplied\n");
  107. err = -ENOENT;
  108. goto exit_error;
  109. }
  110. pdata = dev_get_platdata(&pdev->dev);
  111. info = kzalloc(sizeof(*info), GFP_KERNEL);
  112. if (info == NULL) {
  113. dev_err(&pdev->dev, "no memory for flash info\n");
  114. err = -ENOMEM;
  115. goto exit_error;
  116. }
  117. platform_set_drvdata(pdev, info);
  118. info->dev = &pdev->dev;
  119. info->pdata = pdata;
  120. /* get the resource for the memory mapping */
  121. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  122. if (res == NULL) {
  123. dev_err(&pdev->dev, "no memory resource specified\n");
  124. err = -ENOENT;
  125. goto exit_free;
  126. }
  127. dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
  128. (unsigned long long)res->start);
  129. /* setup map parameters */
  130. info->map.phys = res->start;
  131. info->map.size = resource_size(res);
  132. info->map.name = pdata->mapname != NULL ?
  133. (char *)pdata->mapname : (char *)pdev->name;
  134. info->map.bankwidth = pdata->bankwidth;
  135. /* register our usage of the memory area */
  136. info->area = request_mem_region(res->start, info->map.size, pdev->name);
  137. if (info->area == NULL) {
  138. dev_err(&pdev->dev, "failed to request memory region\n");
  139. err = -EIO;
  140. goto exit_free;
  141. }
  142. /* remap the memory area */
  143. info->map.virt = ioremap(res->start, info->map.size);
  144. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  145. if (info->map.virt == NULL) {
  146. dev_err(&pdev->dev, "failed to ioremap() region\n");
  147. err = -EIO;
  148. goto exit_free;
  149. }
  150. simple_map_init(&info->map);
  151. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  152. /* probe for the right mtd map driver
  153. * supplied by the platform_data struct */
  154. if (pdata->map_probes) {
  155. const char * const *map_probes = pdata->map_probes;
  156. for ( ; !info->mtd && *map_probes; map_probes++)
  157. info->mtd = do_map_probe(*map_probes , &info->map);
  158. }
  159. /* fallback to map_ram */
  160. else
  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. info->mtd->dev.parent = &pdev->dev;
  169. platram_setrw(info, PLATRAM_RW);
  170. /* check to see if there are any available partitions, or whether
  171. * to add this device whole */
  172. err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
  173. pdata->partitions,
  174. pdata->nr_partitions);
  175. if (!err)
  176. dev_info(&pdev->dev, "registered mtd device\n");
  177. if (pdata->nr_partitions) {
  178. /* add the whole device. */
  179. err = mtd_device_register(info->mtd, NULL, 0);
  180. if (err) {
  181. dev_err(&pdev->dev,
  182. "failed to register the entire device\n");
  183. }
  184. }
  185. return err;
  186. exit_free:
  187. platram_remove(pdev);
  188. exit_error:
  189. return err;
  190. }
  191. /* device driver info */
  192. /* work with hotplug and coldplug */
  193. MODULE_ALIAS("platform:mtd-ram");
  194. static struct platform_driver platram_driver = {
  195. .probe = platram_probe,
  196. .remove = platram_remove,
  197. .driver = {
  198. .name = "mtd-ram",
  199. .owner = THIS_MODULE,
  200. },
  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 platform_driver_register(&platram_driver);
  207. }
  208. static void __exit platram_exit(void)
  209. {
  210. platform_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");