plat-ram.c 5.3 KB

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