prom_common.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/module.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 <asm/leon.h>
  26. #include "prom.h"
  27. struct device_node *of_console_device;
  28. EXPORT_SYMBOL(of_console_device);
  29. char *of_console_path;
  30. EXPORT_SYMBOL(of_console_path);
  31. char *of_console_options;
  32. EXPORT_SYMBOL(of_console_options);
  33. int of_getintprop_default(struct device_node *np, const char *name, int def)
  34. {
  35. struct property *prop;
  36. int len;
  37. prop = of_find_property(np, name, &len);
  38. if (!prop || len != 4)
  39. return def;
  40. return *(int *) prop->value;
  41. }
  42. EXPORT_SYMBOL(of_getintprop_default);
  43. DEFINE_MUTEX(of_set_property_mutex);
  44. EXPORT_SYMBOL(of_set_property_mutex);
  45. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  46. {
  47. struct property **prevp;
  48. void *new_val;
  49. int err;
  50. new_val = kmalloc(len, GFP_KERNEL);
  51. if (!new_val)
  52. return -ENOMEM;
  53. memcpy(new_val, val, len);
  54. err = -ENODEV;
  55. mutex_lock(&of_set_property_mutex);
  56. write_lock(&devtree_lock);
  57. prevp = &dp->properties;
  58. while (*prevp) {
  59. struct property *prop = *prevp;
  60. if (!strcasecmp(prop->name, name)) {
  61. void *old_val = prop->value;
  62. int ret;
  63. ret = prom_setprop(dp->phandle, name, val, len);
  64. err = -EINVAL;
  65. if (ret >= 0) {
  66. prop->value = new_val;
  67. prop->length = len;
  68. if (OF_IS_DYNAMIC(prop))
  69. kfree(old_val);
  70. OF_MARK_DYNAMIC(prop);
  71. err = 0;
  72. }
  73. break;
  74. }
  75. prevp = &(*prevp)->next;
  76. }
  77. write_unlock(&devtree_lock);
  78. mutex_unlock(&of_set_property_mutex);
  79. /* XXX Upate procfs if necessary... */
  80. return err;
  81. }
  82. EXPORT_SYMBOL(of_set_property);
  83. int of_find_in_proplist(const char *list, const char *match, int len)
  84. {
  85. while (len > 0) {
  86. int l;
  87. if (!strcmp(list, match))
  88. return 1;
  89. l = strlen(list) + 1;
  90. list += l;
  91. len -= l;
  92. }
  93. return 0;
  94. }
  95. EXPORT_SYMBOL(of_find_in_proplist);
  96. /*
  97. * SPARC32 and SPARC64's prom_nextprop() do things differently
  98. * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf',
  99. * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an
  100. * empty string upon error.
  101. */
  102. static int __init handle_nextprop_quirks(char *buf, const char *name)
  103. {
  104. if (!name || strlen(name) == 0)
  105. return -1;
  106. #ifdef CONFIG_SPARC32
  107. strcpy(buf, name);
  108. #endif
  109. return 0;
  110. }
  111. static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
  112. {
  113. const char *name;
  114. buf[0] = '\0';
  115. name = prom_nextprop(node, prev, buf);
  116. return handle_nextprop_quirks(buf, name);
  117. }
  118. unsigned int prom_early_allocated __initdata;
  119. static struct of_pdt_ops prom_sparc_ops __initdata = {
  120. .nextprop = prom_common_nextprop,
  121. .getproplen = prom_getproplen,
  122. .getproperty = prom_getproperty,
  123. .getchild = prom_getchild,
  124. .getsibling = prom_getsibling,
  125. };
  126. void __init prom_build_devicetree(void)
  127. {
  128. of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
  129. of_console_init();
  130. pr_info("PROM: Built device tree with %u bytes of memory.\n",
  131. prom_early_allocated);
  132. }