Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /* prom_common.c: OF device tree support common code.
0003  *
0004  * Paul Mackerras   August 1996.
0005  * Copyright (C) 1996-2005 Paul Mackerras.
0006  *
0007  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
0008  *    {engebret|bergner}@us.ibm.com
0009  *
0010  *  Adapted for sparc by David S. Miller davem@davemloft.net
0011  */
0012 
0013 #include <linux/kernel.h>
0014 #include <linux/export.h>
0015 #include <linux/errno.h>
0016 #include <linux/mutex.h>
0017 #include <linux/slab.h>
0018 #include <linux/of.h>
0019 #include <linux/of_pdt.h>
0020 #include <asm/prom.h>
0021 #include <asm/oplib.h>
0022 
0023 #include "prom.h"
0024 
0025 struct device_node *of_console_device;
0026 EXPORT_SYMBOL(of_console_device);
0027 
0028 char *of_console_path;
0029 EXPORT_SYMBOL(of_console_path);
0030 
0031 char *of_console_options;
0032 EXPORT_SYMBOL(of_console_options);
0033 
0034 int of_getintprop_default(struct device_node *np, const char *name, int def)
0035 {
0036     struct property *prop;
0037     int len;
0038 
0039     prop = of_find_property(np, name, &len);
0040     if (!prop || len != 4)
0041         return def;
0042 
0043     return *(int *) prop->value;
0044 }
0045 EXPORT_SYMBOL(of_getintprop_default);
0046 
0047 DEFINE_MUTEX(of_set_property_mutex);
0048 EXPORT_SYMBOL(of_set_property_mutex);
0049 
0050 int of_set_property(struct device_node *dp, const char *name, void *val, int len)
0051 {
0052     struct property **prevp;
0053     unsigned long flags;
0054     void *new_val;
0055     int err;
0056 
0057     new_val = kmemdup(val, len, GFP_KERNEL);
0058     if (!new_val)
0059         return -ENOMEM;
0060 
0061     err = -ENODEV;
0062 
0063     mutex_lock(&of_set_property_mutex);
0064     raw_spin_lock_irqsave(&devtree_lock, flags);
0065     prevp = &dp->properties;
0066     while (*prevp) {
0067         struct property *prop = *prevp;
0068 
0069         if (!strcasecmp(prop->name, name)) {
0070             void *old_val = prop->value;
0071             int ret;
0072 
0073             ret = prom_setprop(dp->phandle, name, val, len);
0074 
0075             err = -EINVAL;
0076             if (ret >= 0) {
0077                 prop->value = new_val;
0078                 prop->length = len;
0079 
0080                 if (OF_IS_DYNAMIC(prop))
0081                     kfree(old_val);
0082 
0083                 OF_MARK_DYNAMIC(prop);
0084 
0085                 err = 0;
0086             }
0087             break;
0088         }
0089         prevp = &(*prevp)->next;
0090     }
0091     raw_spin_unlock_irqrestore(&devtree_lock, flags);
0092     mutex_unlock(&of_set_property_mutex);
0093 
0094     /* XXX Upate procfs if necessary... */
0095 
0096     return err;
0097 }
0098 EXPORT_SYMBOL(of_set_property);
0099 
0100 int of_find_in_proplist(const char *list, const char *match, int len)
0101 {
0102     while (len > 0) {
0103         int l;
0104 
0105         if (!strcmp(list, match))
0106             return 1;
0107         l = strlen(list) + 1;
0108         list += l;
0109         len -= l;
0110     }
0111     return 0;
0112 }
0113 EXPORT_SYMBOL(of_find_in_proplist);
0114 
0115 /*
0116  * SPARC32 and SPARC64's prom_nextprop() do things differently
0117  * here, despite sharing the same interface.  SPARC32 doesn't fill in 'buf',
0118  * returning NULL on an error.  SPARC64 fills in 'buf', but sets it to an
0119  * empty string upon error.
0120  */
0121 static int __init handle_nextprop_quirks(char *buf, const char *name)
0122 {
0123     if (!name || strlen(name) == 0)
0124         return -1;
0125 
0126 #ifdef CONFIG_SPARC32
0127     strcpy(buf, name);
0128 #endif
0129     return 0;
0130 }
0131 
0132 static int __init prom_common_nextprop(phandle node, char *prev, char *buf)
0133 {
0134     const char *name;
0135 
0136     buf[0] = '\0';
0137     name = prom_nextprop(node, prev, buf);
0138     return handle_nextprop_quirks(buf, name);
0139 }
0140 
0141 unsigned int prom_early_allocated __initdata;
0142 
0143 static struct of_pdt_ops prom_sparc_ops __initdata = {
0144     .nextprop = prom_common_nextprop,
0145     .getproplen = prom_getproplen,
0146     .getproperty = prom_getproperty,
0147     .getchild = prom_getchild,
0148     .getsibling = prom_getsibling,
0149 };
0150 
0151 void __init prom_build_devicetree(void)
0152 {
0153     of_pdt_build_devicetree(prom_root_node, &prom_sparc_ops);
0154     of_console_init();
0155 
0156     pr_info("PROM: Built device tree with %u bytes of memory.\n",
0157             prom_early_allocated);
0158 }