David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 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 | |
| 17 | #include <linux/kernel.h> |
Paul Gortmaker | 066bcac | 2011-07-22 13:18:16 -0400 | [diff] [blame] | 18 | #include <linux/export.h> |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 19 | #include <linux/errno.h> |
| 20 | #include <linux/mutex.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/of.h> |
Andres Salomon | 3cfc535 | 2010-10-10 21:42:33 -0600 | [diff] [blame] | 23 | #include <linux/of_pdt.h> |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 24 | #include <asm/prom.h> |
| 25 | #include <asm/oplib.h> |
| 26 | |
| 27 | #include "prom.h" |
| 28 | |
David S. Miller | ad07aed | 2008-12-05 15:20:26 -0800 | [diff] [blame] | 29 | struct device_node *of_console_device; |
| 30 | EXPORT_SYMBOL(of_console_device); |
| 31 | |
| 32 | char *of_console_path; |
| 33 | EXPORT_SYMBOL(of_console_path); |
| 34 | |
| 35 | char *of_console_options; |
| 36 | EXPORT_SYMBOL(of_console_options); |
| 37 | |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 38 | int of_getintprop_default(struct device_node *np, const char *name, int def) |
| 39 | { |
| 40 | struct property *prop; |
| 41 | int len; |
| 42 | |
| 43 | prop = of_find_property(np, name, &len); |
| 44 | if (!prop || len != 4) |
| 45 | return def; |
| 46 | |
| 47 | return *(int *) prop->value; |
| 48 | } |
| 49 | EXPORT_SYMBOL(of_getintprop_default); |
| 50 | |
| 51 | DEFINE_MUTEX(of_set_property_mutex); |
| 52 | EXPORT_SYMBOL(of_set_property_mutex); |
| 53 | |
| 54 | int of_set_property(struct device_node *dp, const char *name, void *val, int len) |
| 55 | { |
| 56 | struct property **prevp; |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 57 | unsigned long flags; |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 58 | void *new_val; |
| 59 | int err; |
| 60 | |
Thomas Meyer | f0a4cf3 | 2011-11-17 12:43:40 +0000 | [diff] [blame] | 61 | new_val = kmemdup(val, len, GFP_KERNEL); |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 62 | if (!new_val) |
| 63 | return -ENOMEM; |
| 64 | |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 65 | err = -ENODEV; |
| 66 | |
David S. Miller | 1c9d80d | 2009-11-08 17:41:20 -0800 | [diff] [blame] | 67 | mutex_lock(&of_set_property_mutex); |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 68 | raw_spin_lock_irqsave(&devtree_lock, flags); |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 69 | prevp = &dp->properties; |
| 70 | while (*prevp) { |
| 71 | struct property *prop = *prevp; |
| 72 | |
| 73 | if (!strcasecmp(prop->name, name)) { |
| 74 | void *old_val = prop->value; |
| 75 | int ret; |
| 76 | |
Grant Likely | 6016a36 | 2010-01-28 14:06:53 -0700 | [diff] [blame] | 77 | ret = prom_setprop(dp->phandle, name, val, len); |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 78 | |
| 79 | err = -EINVAL; |
| 80 | if (ret >= 0) { |
| 81 | prop->value = new_val; |
| 82 | prop->length = len; |
| 83 | |
| 84 | if (OF_IS_DYNAMIC(prop)) |
| 85 | kfree(old_val); |
| 86 | |
| 87 | OF_MARK_DYNAMIC(prop); |
| 88 | |
| 89 | err = 0; |
| 90 | } |
| 91 | break; |
| 92 | } |
| 93 | prevp = &(*prevp)->next; |
| 94 | } |
Benjamin Herrenschmidt | d25d869 | 2013-06-12 15:39:04 +1000 | [diff] [blame] | 95 | raw_spin_unlock_irqrestore(&devtree_lock, flags); |
David S. Miller | 1c9d80d | 2009-11-08 17:41:20 -0800 | [diff] [blame] | 96 | mutex_unlock(&of_set_property_mutex); |
David S. Miller | dfa7606 | 2008-12-04 20:28:22 -0800 | [diff] [blame] | 97 | |
| 98 | /* XXX Upate procfs if necessary... */ |
| 99 | |
| 100 | return err; |
| 101 | } |
| 102 | EXPORT_SYMBOL(of_set_property); |
| 103 | |
| 104 | int of_find_in_proplist(const char *list, const char *match, int len) |
| 105 | { |
| 106 | while (len > 0) { |
| 107 | int l; |
| 108 | |
| 109 | if (!strcmp(list, match)) |
| 110 | return 1; |
| 111 | l = strlen(list) + 1; |
| 112 | list += l; |
| 113 | len -= l; |
| 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | EXPORT_SYMBOL(of_find_in_proplist); |
| 118 | |
Andres Salomon | f90c34bd | 2010-10-10 21:49:45 -0600 | [diff] [blame] | 119 | /* |
| 120 | * SPARC32 and SPARC64's prom_nextprop() do things differently |
| 121 | * here, despite sharing the same interface. SPARC32 doesn't fill in 'buf', |
| 122 | * returning NULL on an error. SPARC64 fills in 'buf', but sets it to an |
| 123 | * empty string upon error. |
| 124 | */ |
| 125 | static int __init handle_nextprop_quirks(char *buf, const char *name) |
| 126 | { |
| 127 | if (!name || strlen(name) == 0) |
| 128 | return -1; |
| 129 | |
| 130 | #ifdef CONFIG_SPARC32 |
| 131 | strcpy(buf, name); |
| 132 | #endif |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | static int __init prom_common_nextprop(phandle node, char *prev, char *buf) |
| 137 | { |
| 138 | const char *name; |
| 139 | |
| 140 | buf[0] = '\0'; |
| 141 | name = prom_nextprop(node, prev, buf); |
| 142 | return handle_nextprop_quirks(buf, name); |
| 143 | } |
| 144 | |
David S. Miller | 23dc758 | 2008-12-05 18:16:48 -0800 | [diff] [blame] | 145 | unsigned int prom_early_allocated __initdata; |
| 146 | |
Andres Salomon | f90c34bd | 2010-10-10 21:49:45 -0600 | [diff] [blame] | 147 | static struct of_pdt_ops prom_sparc_ops __initdata = { |
| 148 | .nextprop = prom_common_nextprop, |
| 149 | .getproplen = prom_getproplen, |
| 150 | .getproperty = prom_getproperty, |
| 151 | .getchild = prom_getchild, |
| 152 | .getsibling = prom_getsibling, |
| 153 | }; |
| 154 | |
Andres Salomon | 3cfc535 | 2010-10-10 21:42:33 -0600 | [diff] [blame] | 155 | void __init prom_build_devicetree(void) |
| 156 | { |
Andres Salomon | f90c34bd | 2010-10-10 21:49:45 -0600 | [diff] [blame] | 157 | of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops); |
Andres Salomon | 3cfc535 | 2010-10-10 21:42:33 -0600 | [diff] [blame] | 158 | of_console_init(); |
| 159 | |
| 160 | pr_info("PROM: Built device tree with %u bytes of memory.\n", |
| 161 | prom_early_allocated); |
| 162 | } |