prom_common.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* prom_common.c: OF device tree support common code.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc by David S. Miller [email protected]
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/export.h>
  14. #include <linux/errno.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/of_pdt.h>
  19. #include <asm/prom.h>
  20. #include <asm/oplib.h>
  21. #include "prom.h"
  22. struct device_node *of_console_device;
  23. EXPORT_SYMBOL(of_console_device);
  24. char *of_console_path;
  25. EXPORT_SYMBOL(of_console_path);
  26. char *of_console_options;
  27. EXPORT_SYMBOL(of_console_options);
  28. int of_getintprop_default(struct device_node *np, const char *name, int def)
  29. {
  30. struct property *prop;
  31. int len;
  32. prop = of_find_property(np, name, &len);
  33. if (!prop || len != 4)
  34. return def;
  35. return *(int *) prop->value;
  36. }
  37. EXPORT_SYMBOL(of_getintprop_default);
  38. DEFINE_MUTEX(of_set_property_mutex);
  39. EXPORT_SYMBOL(of_set_property_mutex);
  40. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  41. {
  42. struct property **prevp;
  43. unsigned long flags;
  44. void *new_val;
  45. int err;
  46. new_val = kmemdup(val, len, GFP_KERNEL);
  47. if (!new_val)
  48. return -ENOMEM;
  49. err = -ENODEV;
  50. mutex_lock(&of_set_property_mutex);
  51. raw_spin_lock_irqsave(&devtree_lock, flags);
  52. prevp = &dp->properties;
  53. while (*prevp) {
  54. struct property *prop = *prevp;
  55. if (!strcasecmp(prop->name, name)) {
  56. void *old_val = prop->value;
  57. int ret;
  58. ret = prom_setprop(dp->phandle, name, val, len);
  59. err = -EINVAL;
  60. if (ret >= 0) {
  61. prop->value = new_val;
  62. prop->length = len;
  63. if (OF_IS_DYNAMIC(prop))
  64. kfree(old_val);
  65. OF_MARK_DYNAMIC(prop);
  66. err = 0;
  67. }
  68. break;
  69. }
  70. prevp = &(*prevp)->next;
  71. }
  72. raw_spin_unlock_irqrestore(&devtree_lock, flags);
  73. mutex_unlock(&of_set_property_mutex);
  74. /* XXX Upate procfs if necessary... */
  75. return err;
  76. }
  77. EXPORT_SYMBOL(of_set_property);
  78. int of_find_in_proplist(const char *list, const char *match, int len)
  79. {
  80. while (len > 0) {
  81. int l;
  82. if (!strcmp(list, match))
  83. return 1;
  84. l = strlen(list) + 1;
  85. list += l;
  86. len -= l;
  87. }
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(of_find_in_proplist);
  91. /*
  92. * SPARC32 and SPARC64's prom_nextprop() do things differently
  93. * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf',
  94. * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an
  95. * empty string upon error.
  96. */
  97. static int __init handle_nextprop_quirks(char *buf, const char *name)
  98. {
  99. if (!name || strlen(name) == 0)
  100. return -1;
  101. #ifdef CONFIG_SPARC32
  102. strcpy(buf, name);
  103. #endif
  104. return 0;
  105. }
  106. static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
  107. {
  108. const char *name;
  109. buf[0] = '\0';
  110. name = prom_nextprop(node, prev, buf);
  111. return handle_nextprop_quirks(buf, name);
  112. }
  113. unsigned int prom_early_allocated __initdata;
  114. static struct of_pdt_ops prom_sparc_ops __initdata = {
  115. .nextprop = prom_common_nextprop,
  116. .getproplen = prom_getproplen,
  117. .getproperty = prom_getproperty,
  118. .getchild = prom_getchild,
  119. .getsibling = prom_getsibling,
  120. };
  121. void __init prom_build_devicetree(void)
  122. {
  123. of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
  124. of_console_init();
  125. pr_info("PROM: Built device tree with %u bytes of memory.\n",
  126. prom_early_allocated);
  127. }