2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_platform.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/errno.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/ofw_graph.h>
44 #include <dev/ofw/openfirm.h>
46 #include "ofw_bus_if.h"
48 #define PORT_MAX_NAME 8
51 ofw_graph_get_port_by_idx(phandle_t node, uint32_t idx)
53 phandle_t ports, child;
55 char portnode[PORT_MAX_NAME];
57 /* First try to find a port@<idx> node */
58 snprintf(portnode, sizeof(portnode), "port@%d", idx);
59 child = ofw_bus_find_child(node, portnode);
63 /* Next try to look under ports */
64 ports = ofw_bus_find_child(node, "ports");
68 for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
69 if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 ||
80 ofw_graph_port_get_num_endpoints(phandle_t port)
87 for (num = 0, child = OF_child(port); child != 0;
88 child = OF_peer(child)) {
89 ret = OF_getprop_alloc(child, "name", (void **)&name);
92 if (strcmp(name, "endpoint") == 0)
94 else if (strncmp(name, "endpoint@", 9) == 0)
96 free(name, M_OFWPROP);
103 ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx)
105 phandle_t endpoint, child;
108 /* First test if we have only one endpoint */
109 endpoint = ofw_bus_find_child(port, "endpoint");
113 /* Then test all childs based on the reg property */
114 for (child = OF_child(port); child != 0; child = OF_peer(child)) {
115 if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 ||
126 ofw_graph_get_remote_endpoint(phandle_t endpoint)
130 if (OF_getencprop(endpoint, "remote-endpoint", &remote,
131 sizeof(phandle_t)) <= 0)
138 ofw_graph_get_remote_parent(phandle_t remote)
144 /* get the endpoint node */
145 node = OF_node_from_xref(remote);
147 /* go to the port@X node */
148 node = OF_parent(node);
149 /* go to the ports node or parent */
150 node = OF_parent(node);
152 /* if the node name is 'ports' we need to go up one last time */
153 ret = OF_getprop_alloc(node, "name", (void **)&name);
155 printf("%s: Node %x don't have a name, abort\n", __func__, node);
159 if (strcmp("ports", name) == 0)
160 node = OF_parent(node);
163 free(name, M_OFWPROP);
168 ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id)
170 phandle_t outport, port, endpoint, remote;
172 port = ofw_graph_get_port_by_idx(node, port_id);
175 endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id);
178 remote = ofw_graph_get_remote_endpoint(endpoint);
181 outport = ofw_graph_get_remote_parent(remote);
185 return (OF_device_from_xref(OF_xref_from_node(outport)));