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