ofw_fdt.c revision 228201
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: head/sys/dev/ofw/ofw_fdt.c 228201 2011-12-02 15:24:39Z jchandra $"); 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 140226466Sjchandrastatic int 141226466Sjchandrafdt_pointer_offset(const void *ptr) 142226466Sjchandra{ 143226466Sjchandra uintptr_t dt_struct, p; 144208615Sraj int offset; 145208615Sraj 146226466Sjchandra p = (uintptr_t)ptr; 147226466Sjchandra dt_struct = (uintptr_t)fdtp + fdt_off_dt_struct(fdtp); 148208615Sraj 149226466Sjchandra if ((p < dt_struct) || 150226466Sjchandra p > (dt_struct + fdt_size_dt_struct(fdtp))) 151208615Sraj return (-1); 152208615Sraj 153226466Sjchandra offset = p - dt_struct; 154208615Sraj if (offset < 0) 155208615Sraj return (-1); 156208615Sraj 157208615Sraj return (offset); 158208615Sraj} 159208615Sraj 160208615Sraj/* Return the next sibling of this node or 0. */ 161208615Srajstatic phandle_t 162208615Srajofw_fdt_peer(ofw_t ofw, phandle_t node) 163208615Sraj{ 164208615Sraj int depth, offset; 165208615Sraj 166208615Sraj if (node == 0) { 167208615Sraj /* Find root node */ 168208615Sraj offset = fdt_path_offset(fdtp, "/"); 169208615Sraj 170226466Sjchandra return (fdt_offset_phandle(offset)); 171208615Sraj } 172208615Sraj 173208615Sraj offset = fdt_phandle_offset(node); 174208615Sraj if (offset < 0) 175208615Sraj return (0); 176208615Sraj 177208615Sraj for (depth = 1, offset = fdt_next_node(fdtp, offset, &depth); 178208615Sraj offset >= 0; 179208615Sraj offset = fdt_next_node(fdtp, offset, &depth)) { 180208615Sraj if (depth < 0) 181208615Sraj return (0); 182226466Sjchandra if (depth == 1) 183226466Sjchandra return (fdt_offset_phandle(offset)); 184208615Sraj } 185208615Sraj 186208615Sraj return (0); 187208615Sraj} 188208615Sraj 189208615Sraj/* Return the first child of this node or 0. */ 190208615Srajstatic phandle_t 191208615Srajofw_fdt_child(ofw_t ofw, phandle_t node) 192208615Sraj{ 193208615Sraj int depth, offset; 194208615Sraj 195208615Sraj offset = fdt_phandle_offset(node); 196208615Sraj if (offset < 0) 197208615Sraj return (0); 198208615Sraj 199208615Sraj for (depth = 0, offset = fdt_next_node(fdtp, offset, &depth); 200208615Sraj (offset >= 0) && (depth > 0); 201208615Sraj offset = fdt_next_node(fdtp, offset, &depth)) { 202208615Sraj if (depth < 0) 203208615Sraj return (0); 204226466Sjchandra if (depth == 1) 205226466Sjchandra return (fdt_offset_phandle(offset)); 206208615Sraj } 207208615Sraj 208208615Sraj return (0); 209208615Sraj} 210208615Sraj 211208615Sraj/* Return the parent of this node or 0. */ 212208615Srajstatic phandle_t 213208615Srajofw_fdt_parent(ofw_t ofw, phandle_t node) 214208615Sraj{ 215208615Sraj int offset, paroffset; 216208615Sraj 217208615Sraj offset = fdt_phandle_offset(node); 218208615Sraj if (offset < 0) 219208615Sraj return (0); 220208615Sraj 221208615Sraj paroffset = fdt_parent_offset(fdtp, offset); 222226466Sjchandra return (fdt_offset_phandle(paroffset)); 223208615Sraj} 224208615Sraj 225208615Sraj/* Return the package handle that corresponds to an instance handle. */ 226208615Srajstatic phandle_t 227208615Srajofw_fdt_instance_to_package(ofw_t ofw, ihandle_t instance) 228208615Sraj{ 229208615Sraj int offset; 230208615Sraj 231208615Sraj /* 232208615Sraj * Note: FDT does not have the notion of instances, but we somewhat 233208615Sraj * abuse the semantics and let treat as 'instance' the internal 234208615Sraj * 'phandle' prop, so that ofw I/F consumers have a uniform way of 235208615Sraj * translation between internal representation (which appear in some 236208615Sraj * contexts as property values) and effective phandles. 237208615Sraj */ 238208615Sraj offset = fdt_node_offset_by_phandle(fdtp, instance); 239208615Sraj if (offset < 0) 240215120Sraj return (-1); 241208615Sraj 242226466Sjchandra return (fdt_offset_phandle(offset)); 243208615Sraj} 244208615Sraj 245208615Sraj/* Get the length of a property of a package. */ 246208615Srajstatic ssize_t 247208615Srajofw_fdt_getproplen(ofw_t ofw, phandle_t package, const char *propname) 248208615Sraj{ 249208615Sraj const struct fdt_property *prop; 250208615Sraj int offset, len; 251208615Sraj 252208615Sraj offset = fdt_phandle_offset(package); 253208615Sraj if (offset < 0) 254215120Sraj return (-1); 255208615Sraj 256208615Sraj if (strcmp(propname, "name") == 0) { 257208615Sraj /* Emulate the 'name' property */ 258208615Sraj fdt_get_name(fdtp, offset, &len); 259208615Sraj return (len + 1); 260208615Sraj } 261208615Sraj 262215120Sraj len = -1; 263208615Sraj prop = fdt_get_property(fdtp, offset, propname, &len); 264208615Sraj 265208615Sraj return (len); 266208615Sraj} 267208615Sraj 268208615Sraj/* Get the value of a property of a package. */ 269208615Srajstatic ssize_t 270208615Srajofw_fdt_getprop(ofw_t ofw, phandle_t package, const char *propname, void *buf, 271208615Sraj size_t buflen) 272208615Sraj{ 273208615Sraj const void *prop; 274208615Sraj const char *name; 275208615Sraj int len, offset; 276208615Sraj 277208615Sraj offset = fdt_phandle_offset(package); 278208615Sraj if (offset < 0) 279215120Sraj return (-1); 280208615Sraj 281208615Sraj if (strcmp(propname, "name") == 0) { 282208615Sraj /* Emulate the 'name' property */ 283208615Sraj name = fdt_get_name(fdtp, offset, &len); 284208615Sraj strncpy(buf, name, buflen); 285208615Sraj if (len + 1 > buflen) 286208615Sraj len = buflen; 287208615Sraj return (len + 1); 288208615Sraj } 289208615Sraj 290208615Sraj prop = fdt_getprop(fdtp, offset, propname, &len); 291208615Sraj if (prop == NULL) 292215120Sraj return (-1); 293208615Sraj 294208615Sraj if (len > buflen) 295208615Sraj len = buflen; 296208615Sraj bcopy(prop, buf, len); 297208615Sraj return (len); 298208615Sraj} 299208615Sraj 300208615Srajstatic int 301208615Srajfdt_nextprop(int offset, char *buf, size_t size) 302208615Sraj{ 303208615Sraj const struct fdt_property *prop; 304208615Sraj const char *name; 305208615Sraj uint32_t tag; 306208615Sraj int nextoffset, depth; 307208615Sraj 308208615Sraj depth = 0; 309208615Sraj tag = fdt_next_tag(fdtp, offset, &nextoffset); 310208615Sraj 311208615Sraj /* Find the next prop */ 312208615Sraj do { 313208615Sraj offset = nextoffset; 314208615Sraj tag = fdt_next_tag(fdtp, offset, &nextoffset); 315208615Sraj 316208615Sraj if (tag == FDT_BEGIN_NODE) 317208615Sraj depth++; 318208615Sraj else if (tag == FDT_END_NODE) 319208615Sraj depth--; 320208615Sraj else if ((tag == FDT_PROP) && (depth == 0)) { 321208615Sraj prop = 322208615Sraj (const struct fdt_property *)fdt_offset_ptr(fdtp, 323208615Sraj offset, sizeof(*prop)); 324208615Sraj name = fdt_string(fdtp, 325208615Sraj fdt32_to_cpu(prop->nameoff)); 326208615Sraj strncpy(buf, name, size); 327208615Sraj return (strlen(name)); 328208615Sraj } else 329208615Sraj depth = -1; 330208615Sraj } while (depth >= 0); 331208615Sraj 332215120Sraj return (-1); 333208615Sraj} 334208615Sraj 335208615Sraj/* 336208615Sraj * Get the next property of a package. Return the actual len of retrieved 337208615Sraj * prop name. 338208615Sraj */ 339208615Srajstatic int 340208615Srajofw_fdt_nextprop(ofw_t ofw, phandle_t package, const char *previous, char *buf, 341208615Sraj size_t size) 342208615Sraj{ 343208615Sraj const struct fdt_property *prop; 344208615Sraj int offset, rv; 345208615Sraj 346208615Sraj offset = fdt_phandle_offset(package); 347208615Sraj if (offset < 0) 348215120Sraj return (-1); 349208615Sraj 350208615Sraj if (previous == NULL) 351208615Sraj /* Find the first prop in the node */ 352208615Sraj return (fdt_nextprop(offset, buf, size)); 353208615Sraj 354208615Sraj /* 355208615Sraj * Advance to the previous prop 356208615Sraj */ 357208615Sraj prop = fdt_get_property(fdtp, offset, previous, NULL); 358208615Sraj if (prop == NULL) 359215120Sraj return (-1); 360208615Sraj 361226466Sjchandra offset = fdt_pointer_offset(prop); 362208615Sraj rv = fdt_nextprop(offset, buf, size); 363208615Sraj return (rv); 364208615Sraj} 365208615Sraj 366208615Sraj/* Set the value of a property of a package. */ 367208615Srajstatic int 368208615Srajofw_fdt_setprop(ofw_t ofw, phandle_t package, const char *propname, 369208615Sraj const void *buf, size_t len) 370208615Sraj{ 371208615Sraj int offset; 372208615Sraj 373208615Sraj offset = fdt_phandle_offset(package); 374208615Sraj if (offset < 0) 375208615Sraj return (-1); 376208615Sraj 377208615Sraj return (fdt_setprop_inplace(fdtp, offset, propname, buf, len)); 378208615Sraj} 379208615Sraj 380208615Sraj/* Convert a device specifier to a fully qualified pathname. */ 381208615Srajstatic ssize_t 382208615Srajofw_fdt_canon(ofw_t ofw, const char *device, char *buf, size_t len) 383208615Sraj{ 384208615Sraj 385208615Sraj return (-1); 386208615Sraj} 387208615Sraj 388208615Sraj/* Return a package handle for the specified device. */ 389208615Srajstatic phandle_t 390208615Srajofw_fdt_finddevice(ofw_t ofw, const char *device) 391208615Sraj{ 392208615Sraj int offset; 393208615Sraj 394208615Sraj offset = fdt_path_offset(fdtp, device); 395228201Sjchandra if (offset < 0) 396228201Sjchandra return (-1); 397226466Sjchandra return (fdt_offset_phandle(offset)); 398208615Sraj} 399208615Sraj 400208615Sraj/* Return the fully qualified pathname corresponding to an instance. */ 401208615Srajstatic ssize_t 402208615Srajofw_fdt_instance_to_path(ofw_t ofw, ihandle_t instance, char *buf, size_t len) 403208615Sraj{ 404208615Sraj 405208615Sraj return (-1); 406208615Sraj} 407208615Sraj 408208615Sraj/* Return the fully qualified pathname corresponding to a package. */ 409208615Srajstatic ssize_t 410208615Srajofw_fdt_package_to_path(ofw_t ofw, phandle_t package, char *buf, size_t len) 411208615Sraj{ 412208615Sraj 413208615Sraj return (-1); 414208615Sraj} 415208615Sraj 416208615Srajstatic int 417208615Srajofw_fdt_fixup(ofw_t ofw) 418208615Sraj{ 419208615Sraj#define FDT_MODEL_LEN 80 420208615Sraj char model[FDT_MODEL_LEN]; 421208615Sraj phandle_t root; 422208615Sraj ssize_t len; 423208615Sraj int i; 424208615Sraj 425228201Sjchandra if ((root = ofw_fdt_finddevice(ofw, "/")) == -1) 426208615Sraj return (ENODEV); 427208615Sraj 428208615Sraj if ((len = ofw_fdt_getproplen(ofw, root, "model")) <= 0) 429208615Sraj return (0); 430208615Sraj 431208615Sraj bzero(model, FDT_MODEL_LEN); 432208615Sraj if (ofw_fdt_getprop(ofw, root, "model", model, FDT_MODEL_LEN) <= 0) 433208615Sraj return (0); 434208615Sraj 435208615Sraj /* 436208615Sraj * Search fixup table and call handler if appropriate. 437208615Sraj */ 438208615Sraj for (i = 0; fdt_fixup_table[i].model != NULL; i++) { 439208615Sraj if (strncmp(model, fdt_fixup_table[i].model, 440208615Sraj FDT_MODEL_LEN) != 0) 441208615Sraj continue; 442208615Sraj 443208615Sraj if (fdt_fixup_table[i].handler != NULL) 444208615Sraj (*fdt_fixup_table[i].handler)(root); 445208615Sraj } 446208615Sraj 447208615Sraj return (0); 448208615Sraj} 449208615Sraj 450208615Srajstatic int 451212477Smariusofw_fdt_interpret(ofw_t ofw, const char *cmd, int nret, cell_t *retvals) 452208615Sraj{ 453208615Sraj int rv; 454208615Sraj 455208615Sraj /* 456208615Sraj * Note: FDT does not have the possibility to 'interpret' commands, 457208615Sraj * but we abuse the interface a bit to use it for doing non-standard 458208615Sraj * operations on the device tree blob. 459208615Sraj * 460208615Sraj * Currently the only supported 'command' is to trigger performing 461208615Sraj * fixups. 462208615Sraj */ 463208615Sraj if (strncmp("perform-fixup", cmd, 13) != 0) 464208615Sraj return (0); 465208615Sraj 466208615Sraj rv = ofw_fdt_fixup(ofw); 467208615Sraj if (nret > 0) 468208615Sraj retvals[0] = rv; 469208615Sraj 470208615Sraj return (rv); 471208615Sraj} 472