1208615Sraj/*- 2208615Sraj * Copyright (c) 2009-2010 The FreeBSD Foundation 3208615Sraj * All rights reserved. 4208615Sraj * 5208615Sraj * This software was developed by Semihalf under sponsorship from 6208615Sraj * the FreeBSD Foundation. 7208615Sraj * 8208615Sraj * Redistribution and use in source and binary forms, with or without 9208615Sraj * modification, are permitted provided that the following conditions 10208615Sraj * are met: 11208615Sraj * 1. Redistributions of source code must retain the above copyright 12208615Sraj * notice, this list of conditions and the following disclaimer. 13208615Sraj * 2. Redistributions in binary form must reproduce the above copyright 14208615Sraj * notice, this list of conditions and the following disclaimer in the 15208615Sraj * documentation and/or other materials provided with the distribution. 16208615Sraj * 17208615Sraj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18208615Sraj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19208615Sraj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20208615Sraj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21208615Sraj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22208615Sraj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23208615Sraj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24208615Sraj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25208615Sraj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26208615Sraj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27208615Sraj * SUCH DAMAGE. 28208615Sraj */ 29208615Sraj 30208615Sraj#include <sys/cdefs.h> 31208615Sraj__FBSDID("$FreeBSD: releng/10.2/sys/dev/ofw/ofw_fdt.c 273652 2014-10-26 01:30:46Z ian $"); 32208615Sraj 33208615Sraj#include <sys/param.h> 34208615Sraj#include <sys/kernel.h> 35208615Sraj#include <sys/malloc.h> 36208615Sraj#include <sys/systm.h> 37208615Sraj 38208615Sraj#include <contrib/libfdt/libfdt.h> 39208615Sraj 40208615Sraj#include <machine/stdarg.h> 41208615Sraj 42208615Sraj#include <dev/fdt/fdt_common.h> 43208615Sraj#include <dev/ofw/ofwvar.h> 44208615Sraj#include <dev/ofw/openfirm.h> 45208615Sraj 46208615Sraj#include "ofw_if.h" 47208615Sraj 48208615Sraj#ifdef DEBUG 49208615Sraj#define debugf(fmt, args...) do { printf("%s(): ", __func__); \ 50208615Sraj printf(fmt,##args); } while (0) 51208615Sraj#else 52208615Sraj#define debugf(fmt, args...) 53208615Sraj#endif 54208615Sraj 55208615Srajstatic int ofw_fdt_init(ofw_t, void *); 56208615Srajstatic phandle_t ofw_fdt_peer(ofw_t, phandle_t); 57208615Srajstatic phandle_t ofw_fdt_child(ofw_t, phandle_t); 58208615Srajstatic phandle_t ofw_fdt_parent(ofw_t, phandle_t); 59208615Srajstatic phandle_t ofw_fdt_instance_to_package(ofw_t, ihandle_t); 60208615Srajstatic ssize_t ofw_fdt_getproplen(ofw_t, phandle_t, const char *); 61208615Srajstatic ssize_t ofw_fdt_getprop(ofw_t, phandle_t, const char *, void *, size_t); 62208615Srajstatic int ofw_fdt_nextprop(ofw_t, phandle_t, const char *, char *, size_t); 63208615Srajstatic int ofw_fdt_setprop(ofw_t, phandle_t, const char *, const void *, 64208615Sraj size_t); 65208615Srajstatic ssize_t ofw_fdt_canon(ofw_t, const char *, char *, size_t); 66208615Srajstatic phandle_t ofw_fdt_finddevice(ofw_t, const char *); 67208615Srajstatic ssize_t ofw_fdt_instance_to_path(ofw_t, ihandle_t, char *, size_t); 68208615Srajstatic ssize_t ofw_fdt_package_to_path(ofw_t, phandle_t, char *, size_t); 69212477Smariusstatic int ofw_fdt_interpret(ofw_t, const char *, int, cell_t *); 70208615Sraj 71208615Srajstatic ofw_method_t ofw_fdt_methods[] = { 72208615Sraj OFWMETHOD(ofw_init, ofw_fdt_init), 73208615Sraj OFWMETHOD(ofw_peer, ofw_fdt_peer), 74208615Sraj OFWMETHOD(ofw_child, ofw_fdt_child), 75208615Sraj OFWMETHOD(ofw_parent, ofw_fdt_parent), 76208615Sraj OFWMETHOD(ofw_instance_to_package, ofw_fdt_instance_to_package), 77208615Sraj OFWMETHOD(ofw_getproplen, ofw_fdt_getproplen), 78208615Sraj OFWMETHOD(ofw_getprop, ofw_fdt_getprop), 79208615Sraj OFWMETHOD(ofw_nextprop, ofw_fdt_nextprop), 80208615Sraj OFWMETHOD(ofw_setprop, ofw_fdt_setprop), 81208615Sraj OFWMETHOD(ofw_canon, ofw_fdt_canon), 82208615Sraj OFWMETHOD(ofw_finddevice, ofw_fdt_finddevice), 83208615Sraj OFWMETHOD(ofw_instance_to_path, ofw_fdt_instance_to_path), 84208615Sraj OFWMETHOD(ofw_package_to_path, ofw_fdt_package_to_path), 85208615Sraj OFWMETHOD(ofw_interpret, ofw_fdt_interpret), 86208615Sraj { 0, 0 } 87208615Sraj}; 88208615Sraj 89208615Srajstatic ofw_def_t ofw_fdt = { 90208615Sraj OFW_FDT, 91208615Sraj ofw_fdt_methods, 92208615Sraj 0 93208615Sraj}; 94208615SrajOFW_DEF(ofw_fdt); 95208615Sraj 96208615Srajstatic void *fdtp = NULL; 97208615Sraj 98208615Srajstatic int 99208615Srajofw_fdt_init(ofw_t ofw, void *data) 100208615Sraj{ 101208615Sraj int err; 102208615Sraj 103208615Sraj /* Check FDT blob integrity */ 104208615Sraj if ((err = fdt_check_header(data)) != 0) 105208615Sraj return (err); 106208615Sraj 107208615Sraj fdtp = data; 108208615Sraj return (0); 109208615Sraj} 110208615Sraj 111208615Sraj/* 112226466Sjchandra * Device tree functions. 113226466Sjchandra * 114226466Sjchandra * We use the offset from fdtp to the node as the 'phandle' in OF interface. 115226466Sjchandra * 116226466Sjchandra * phandle is a u32 value, therefore we cannot use the pointer to node as 117226466Sjchandra * phandle in 64 bit. We also do not use the usual fdt offset as phandle, 118226466Sjchandra * as it can be 0, and the OF interface has special meaning for phandle 0. 119208615Sraj */ 120208615Sraj 121226466Sjchandrastatic phandle_t 122226466Sjchandrafdt_offset_phandle(int offset) 123226466Sjchandra{ 124226466Sjchandra if (offset < 0) 125226466Sjchandra return (0); 126226466Sjchandra return ((phandle_t)offset + fdt_off_dt_struct(fdtp)); 127226466Sjchandra} 128226466Sjchandra 129208615Srajstatic int 130208615Srajfdt_phandle_offset(phandle_t p) 131208615Sraj{ 132226466Sjchandra int pint = (int)p; 133226466Sjchandra int dtoff = fdt_off_dt_struct(fdtp); 134226466Sjchandra 135226466Sjchandra if (pint < dtoff) 136226466Sjchandra return (-1); 137226466Sjchandra return (pint - dtoff); 138226466Sjchandra} 139226466Sjchandra 140208615Sraj/* Return the next sibling of this node or 0. */ 141208615Srajstatic phandle_t 142208615Srajofw_fdt_peer(ofw_t ofw, phandle_t node) 143208615Sraj{ 144208615Sraj int depth, offset; 145208615Sraj 146208615Sraj if (node == 0) { 147208615Sraj /* Find root node */ 148208615Sraj offset = fdt_path_offset(fdtp, "/"); 149208615Sraj 150226466Sjchandra return (fdt_offset_phandle(offset)); 151208615Sraj } 152208615Sraj 153208615Sraj offset = fdt_phandle_offset(node); 154208615Sraj if (offset < 0) 155208615Sraj return (0); 156208615Sraj 157208615Sraj for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth); 158208615Sraj offset >= 0; 159208615Sraj offset = fdt_next_node(fdtp, offset, &depth)) { 160208615Sraj if (depth < 0) 161208615Sraj return (0); 162226466Sjchandra if (depth == 1) 163226466Sjchandra return (fdt_offset_phandle(offset)); 164208615Sraj } 165208615Sraj 166208615Sraj return (0); 167208615Sraj} 168208615Sraj 169208615Sraj/* Return the first child of this node or 0. */ 170208615Srajstatic phandle_t 171208615Srajofw_fdt_child(ofw_t ofw, phandle_t node) 172208615Sraj{ 173208615Sraj int depth, offset; 174208615Sraj 175208615Sraj offset = fdt_phandle_offset(node); 176208615Sraj if (offset < 0) 177208615Sraj return (0); 178208615Sraj 179208615Sraj for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth); 180208615Sraj (offset >= 0) && (depth > 0); 181208615Sraj offset = fdt_next_node(fdtp, offset, &depth)) { 182208615Sraj if (depth < 0) 183208615Sraj return (0); 184226466Sjchandra if (depth == 1) 185226466Sjchandra return (fdt_offset_phandle(offset)); 186208615Sraj } 187208615Sraj 188208615Sraj return (0); 189208615Sraj} 190208615Sraj 191208615Sraj/* Return the parent of this node or 0. */ 192208615Srajstatic phandle_t 193208615Srajofw_fdt_parent(ofw_t ofw, phandle_t node) 194208615Sraj{ 195208615Sraj int offset, paroffset; 196208615Sraj 197208615Sraj offset = fdt_phandle_offset(node); 198208615Sraj if (offset < 0) 199208615Sraj return (0); 200208615Sraj 201208615Sraj paroffset = fdt_parent_offset(fdtp, offset); 202226466Sjchandra return (fdt_offset_phandle(paroffset)); 203208615Sraj} 204208615Sraj 205208615Sraj/* Return the package handle that corresponds to an instance handle. */ 206208615Srajstatic phandle_t 207208615Srajofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance) 208208615Sraj{ 209208615Sraj 210266020Sian /* Where real OF uses ihandles in the tree, FDT uses xref phandles */ 211273652Sian return (OF_node_from_xref(instance)); 212208615Sraj} 213208615Sraj 214208615Sraj/* Get the length of a property of a package. */ 215208615Srajstatic ssize_t 216208615Srajofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname) 217208615Sraj{ 218208615Sraj const struct fdt_property *prop; 219208615Sraj int offset, len; 220208615Sraj 221208615Sraj offset = fdt_phandle_offset(package); 222208615Sraj if (offset < 0) 223215120Sraj return (-1); 224208615Sraj 225208615Sraj if (strcmp(propname, "name") == 0) { 226208615Sraj /* Emulate the 'name' property */ 227208615Sraj fdt_get_name(fdtp, offset, &len); 228208615Sraj return (len + 1); 229208615Sraj } 230208615Sraj 231215120Sraj len = -1; 232208615Sraj prop = fdt_get_property(fdtp, offset, propname, &len); 233208615Sraj 234208615Sraj return (len); 235208615Sraj} 236208615Sraj 237208615Sraj/* Get the value of a property of a package. */ 238208615Srajstatic ssize_t 239208615Srajofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf, 240208615Sraj size_t buflen) 241208615Sraj{ 242208615Sraj const void *prop; 243208615Sraj const char *name; 244208615Sraj int len, offset; 245208615Sraj 246208615Sraj offset = fdt_phandle_offset(package); 247208615Sraj if (offset < 0) 248215120Sraj return (-1); 249208615Sraj 250208615Sraj if (strcmp(propname, "name") == 0) { 251208615Sraj /* Emulate the 'name' property */ 252208615Sraj name = fdt_get_name(fdtp, offset, &len); 253208615Sraj strncpy(buf, name, buflen); 254208615Sraj if (len + 1 > buflen) 255208615Sraj len = buflen; 256208615Sraj return (len + 1); 257208615Sraj } 258208615Sraj 259208615Sraj prop = fdt_getprop(fdtp, offset, propname, &len); 260208615Sraj if (prop == NULL) 261215120Sraj return (-1); 262208615Sraj 263208615Sraj if (len > buflen) 264208615Sraj len = buflen; 265208615Sraj bcopy(prop, buf, len); 266208615Sraj return (len); 267208615Sraj} 268208615Sraj 269208615Sraj/* 270266020Sian * Get the next property of a package. Return values: 271266020Sian * -1: package or previous property does not exist 272266020Sian * 0: no more properties 273266020Sian * 1: success 274208615Sraj */ 275208615Srajstatic int 276208615Srajofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf, 277208615Sraj size_t size) 278208615Sraj{ 279208615Sraj const struct fdt_property *prop; 280266000Sian const char *name; 281266000Sian int offset; 282208615Sraj 283208615Sraj offset = fdt_phandle_offset(package); 284208615Sraj if (offset < 0) 285215120Sraj return (-1); 286208615Sraj 287266000Sian /* Find the first prop in the node */ 288266000Sian offset = fdt_first_property_offset(fdtp, offset); 289266000Sian if (offset < 0) 290266000Sian return (0); /* No properties */ 291208615Sraj 292266000Sian if (previous != NULL) { 293266000Sian while (offset >= 0) { 294266000Sian prop = fdt_get_property_by_offset(fdtp, offset, NULL); 295266000Sian if (prop == NULL) 296266000Sian return (-1); /* Internal error */ 297266000Sian 298266000Sian offset = fdt_next_property_offset(fdtp, offset); 299266000Sian if (offset < 0) 300266000Sian return (0); /* No more properties */ 301266000Sian 302266000Sian /* Check if the last one was the one we wanted */ 303266000Sian name = fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)); 304266000Sian if (strcmp(name, previous) == 0) 305266000Sian break; 306266000Sian } 307266000Sian } 308266000Sian 309266000Sian prop = fdt_get_property_by_offset(fdtp, offset, &offset); 310208615Sraj if (prop == NULL) 311266000Sian return (-1); /* Internal error */ 312208615Sraj 313266000Sian strncpy(buf, fdt_string(fdtp, fdt32_to_cpu(prop->nameoff)), size); 314266000Sian 315266020Sian return (1); 316208615Sraj} 317208615Sraj 318208615Sraj/* Set the value of a property of a package. */ 319208615Srajstatic int 320208615Srajofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname, 321208615Sraj const void *buf, size_t len) 322208615Sraj{ 323208615Sraj int offset; 324208615Sraj 325208615Sraj offset = fdt_phandle_offset(package); 326208615Sraj if (offset < 0) 327208615Sraj return (-1); 328208615Sraj 329208615Sraj return (fdt_setprop_inplace(fdtp, offset, propname, buf, len)); 330208615Sraj} 331208615Sraj 332208615Sraj/* Convert a device specifier to a fully qualified pathname. */ 333208615Srajstatic ssize_t 334208615Srajofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len) 335208615Sraj{ 336208615Sraj 337208615Sraj return (-1); 338208615Sraj} 339208615Sraj 340208615Sraj/* Return a package handle for the specified device. */ 341208615Srajstatic phandle_t 342208615Srajofw_fdt_finddevice(ofw_t ofw, const char *device) 343208615Sraj{ 344208615Sraj int offset; 345208615Sraj 346208615Sraj offset = fdt_path_offset(fdtp, device); 347228201Sjchandra if (offset < 0) 348228201Sjchandra return (-1); 349226466Sjchandra return (fdt_offset_phandle(offset)); 350208615Sraj} 351208615Sraj 352208615Sraj/* Return the fully qualified pathname corresponding to an instance. */ 353208615Srajstatic ssize_t 354208615Srajofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len) 355208615Sraj{ 356266020Sian phandle_t phandle; 357208615Sraj 358266020Sian phandle = OF_instance_to_package(instance); 359266020Sian if (phandle == -1) 360266020Sian return (-1); 361266020Sian 362266020Sian return (OF_package_to_path(phandle, buf, len)); 363208615Sraj} 364208615Sraj 365208615Sraj/* Return the fully qualified pathname corresponding to a package. */ 366208615Srajstatic ssize_t 367208615Srajofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len) 368208615Sraj{ 369208615Sraj 370208615Sraj return (-1); 371208615Sraj} 372208615Sraj 373208615Srajstatic int 374208615Srajofw_fdt_fixup(ofw_t ofw) 375208615Sraj{ 376208615Sraj#define FDT_MODEL_LEN 80 377208615Sraj char model[FDT_MODEL_LEN]; 378208615Sraj phandle_t root; 379208615Sraj ssize_t len; 380208615Sraj int i; 381208615Sraj 382228201Sjchandra if ((root = ofw_fdt_finddevice(ofw, "/")) == -1) 383208615Sraj return (ENODEV); 384208615Sraj 385208615Sraj if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0) 386208615Sraj return (0); 387208615Sraj 388208615Sraj bzero(model, FDT_MODEL_LEN); 389208615Sraj if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0) 390208615Sraj return (0); 391208615Sraj 392208615Sraj /* 393208615Sraj * Search fixup table and call handler if appropriate. 394208615Sraj */ 395208615Sraj for (i = 0; fdt_fixup_table[i].model != NULL; i++) { 396208615Sraj if (strncmp(model, fdt_fixup_table[i].model, 397208615Sraj FDT_MODEL_LEN) != 0) 398208615Sraj continue; 399208615Sraj 400208615Sraj if (fdt_fixup_table[i].handler != NULL) 401208615Sraj (*fdt_fixup_table[i].handler)(root); 402208615Sraj } 403208615Sraj 404208615Sraj return (0); 405208615Sraj} 406208615Sraj 407208615Srajstatic int 408212477Smariusofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals) 409208615Sraj{ 410208615Sraj int rv; 411208615Sraj 412208615Sraj /* 413208615Sraj * Note: FDT does not have the possibility to 'interpret' commands, 414208615Sraj * but we abuse the interface a bit to use it for doing non-standard 415208615Sraj * operations on the device tree blob. 416208615Sraj * 417208615Sraj * Currently the only supported 'command' is to trigger performing 418208615Sraj * fixups. 419208615Sraj */ 420208615Sraj if (strncmp("perform-fixup", cmd, 13) != 0) 421208615Sraj return (0); 422208615Sraj 423208615Sraj rv = ofw_fdt_fixup(ofw); 424208615Sraj if (nret > 0) 425208615Sraj retvals[0] = rv; 426208615Sraj 427208615Sraj return (rv); 428208615Sraj} 429