plat-ram.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* drivers/mtd/maps/plat-ram.c
  3. *
  4. * (c) 2004-2005 Simtec Electronics
  5. * http://www.simtec.co.uk/products/SWLINUX/
  6. * Ben Dooks <ben@simtec.co.uk>
  7. *
  8. * Generic platform device based RAM map
  9. */
  10. #include <linux/module.h>
  11. #include <linux/types.h>
  12. #include <linux/kernel.h>
  13. #include <linux/string.h>
  14. #include <linux/ioport.h>
  15. #include <linux/device.h>
  16. #include <linux/slab.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/mtd/plat-ram.h>
  22. #include <asm/io.h>
  23. /* private structure for each mtd platform ram device created */
  24. struct platram_info {
  25. struct device *dev;
  26. struct mtd_info *mtd;
  27. struct map_info map;
  28. struct platdata_mtd_ram *pdata;
  29. };
  30. /* to_platram_info()
  31. *
  32. * device private data to struct platram_info conversion
  33. */
  34. static inline struct platram_info *to_platram_info(struct platform_device *dev)
  35. {
  36. return platform_get_drvdata(dev);
  37. }
  38. /* platram_setrw
  39. *
  40. * call the platform device's set rw/ro control
  41. *
  42. * to = 0 => read-only
  43. * = 1 => read-write
  44. */
  45. static inline void platram_setrw(struct platram_info *info, int to)
  46. {
  47. if (info->pdata == NULL)
  48. return;
  49. if (info->pdata->set_rw != NULL)
  50. (info->pdata->set_rw)(info->dev, to);
  51. }
  52. /* platram_remove
  53. *
  54. * called to remove the device from the driver's control
  55. */
  56. static void platram_remove(struct platform_device *pdev)
  57. {
  58. struct platram_info *info = to_platram_info(pdev);
  59. dev_dbg(&pdev->dev, "removing device\n");
  60. if (info == NULL)
  61. return;
  62. if (info->mtd) {
  63. mtd_device_unregister(info->mtd);
  64. map_destroy(info->mtd);
  65. }
  66. /* ensure ram is left read-only */
  67. platram_setrw(info, PLATRAM_RO);
  68. kfree(info);
  69. }
  70. /* platram_probe
  71. *
  72. * called from device drive system when a device matching our
  73. * driver is found.
  74. */
  75. static int platram_probe(struct platform_device *pdev)
  76. {
  77. struct platdata_mtd_ram *pdata;
  78. struct platram_info *info;
  79. struct resource *res;
  80. int err = 0;
  81. dev_dbg(&pdev->dev, "probe entered\n");
  82. if (dev_get_platdata(&pdev->dev) == NULL) {
  83. dev_err(&pdev->dev, "no platform data supplied\n");
  84. err = -ENOENT;
  85. goto exit_error;
  86. }
  87. pdata = dev_get_platdata(&pdev->dev);
  88. info = kzalloc(sizeof(*info), GFP_KERNEL);
  89. if (info == NULL) {
  90. err = -ENOMEM;
  91. goto exit_error;
  92. }
  93. platform_set_drvdata(pdev, info);
  94. info->dev = &pdev->dev;
  95. info->pdata = pdata;
  96. /* get the resource for the memory mapping */
  97. info->map.virt = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  98. if (IS_ERR(info->map.virt)) {
  99. err = PTR_ERR(info->map.virt);
  100. goto exit_free;
  101. }
  102. dev_dbg(&pdev->dev, "got platform resource %p (0x%llx)\n", res,
  103. (unsigned long long)res->start);
  104. /* setup map parameters */
  105. info->map.phys = res->start;
  106. info->map.size = resource_size(res);
  107. info->map.name = pdata->mapname != NULL ?
  108. (char *)pdata->mapname : (char *)pdev->name;
  109. info->map.bankwidth = pdata->bankwidth;
  110. dev_dbg(&pdev->dev, "virt %p, %lu bytes\n", info->map.virt, info->map.size);
  111. simple_map_init(&info->map);
  112. dev_dbg(&pdev->dev, "initialised map, probing for mtd\n");
  113. /* probe for the right mtd map driver
  114. * supplied by the platform_data struct */
  115. if (pdata->map_probes) {
  116. const char * const *map_probes = pdata->map_probes;
  117. for ( ; !info->mtd && *map_probes; map_probes++)
  118. info->mtd = do_map_probe(*map_probes , &info->map);
  119. }
  120. /* fallback to map_ram */
  121. else
  122. info->mtd = do_map_probe("map_ram", &info->map);
  123. if (info->mtd == NULL) {
  124. dev_err(&pdev->dev, "failed to probe for map_ram\n");
  125. err = -ENOMEM;
  126. goto exit_free;
  127. }
  128. info->mtd->dev.parent = &pdev->dev;
  129. platram_setrw(info, PLATRAM_RW);
  130. /* check to see if there are any available partitions, or whether
  131. * to add this device whole */
  132. err = mtd_device_parse_register(info->mtd, pdata->probes, NULL,
  133. pdata->partitions,
  134. pdata->nr_partitions);
  135. if (err) {
  136. dev_err(&pdev->dev, "failed to register mtd device\n");
  137. goto exit_free;
  138. }
  139. dev_info(&pdev->dev, "registered mtd device\n");
  140. if (pdata->nr_partitions) {
  141. /* add the whole device. */
  142. err = mtd_device_register(info->mtd, NULL, 0);
  143. if (err) {
  144. dev_err(&pdev->dev,
  145. "failed to register the entire device\n");
  146. goto exit_free;
  147. }
  148. }
  149. return 0;
  150. exit_free:
  151. platram_remove(pdev);
  152. exit_error:
  153. return err;
  154. }
  155. /* device driver info */
  156. /* work with hotplug and coldplug */
  157. MODULE_ALIAS("platform:mtd-ram");
  158. static struct platform_driver platram_driver = {
  159. .probe = platram_probe,
  160. .remove_new = platram_remove,
  161. .driver = {
  162. .name = "mtd-ram",
  163. },
  164. };
  165. module_platform_driver(platram_driver);
  166. MODULE_LICENSE("GPL");
  167. MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
  168. MODULE_DESCRIPTION("MTD platform RAM map driver");