prom_common.c 3.6 KB

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