plat-ram.c 5.8 KB

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