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 /* Now check for 'port' without explicit index. */
65 snprintf(portnode, sizeof(portnode), "port");
66 child = ofw_bus_find_child(node, portnode);
71 /* Next try to look under ports */
72 ports = ofw_bus_find_child(node, "ports");
76 for (child = OF_child(ports); child != 0; child = OF_peer(child)) {
77 if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 ||
88 ofw_graph_port_get_num_endpoints(phandle_t port)
95 for (num = 0, child = OF_child(port); child != 0;
96 child = OF_peer(child)) {
97 ret = OF_getprop_alloc(child, "name", (void **)&name);
100 if (strcmp(name, "endpoint") == 0)
102 else if (strncmp(name, "endpoint@", 9) == 0)
104 free(name, M_OFWPROP);
111 ofw_graph_get_endpoint_by_idx(phandle_t port, uint32_t idx)
113 phandle_t endpoint, child;
116 /* First test if we have only one endpoint */
117 endpoint = ofw_bus_find_child(port, "endpoint");
121 /* Then test all childs based on the reg property */
122 for (child = OF_child(port); child != 0; child = OF_peer(child)) {
123 if (OF_getencprop(child, "reg", ®, sizeof(uint32_t)) <= 0 ||
134 ofw_graph_get_remote_endpoint(phandle_t endpoint)
138 if (OF_getencprop(endpoint, "remote-endpoint", &remote,
139 sizeof(phandle_t)) <= 0)
146 ofw_graph_get_remote_parent(phandle_t remote)
152 /* get the endpoint node */
153 node = OF_node_from_xref(remote);
155 /* go to the port@X node */
156 node = OF_parent(node);
157 /* go to the ports node or parent */
158 node = OF_parent(node);
160 /* if the node name is 'ports' we need to go up one last time */
161 ret = OF_getprop_alloc(node, "name", (void **)&name);
163 printf("%s: Node %x don't have a name, abort\n", __func__, node);
167 if (strcmp("ports", name) == 0)
168 node = OF_parent(node);
171 free(name, M_OFWPROP);
176 ofw_graph_get_device_by_port_ep(phandle_t node, uint32_t port_id, uint32_t ep_id)
178 phandle_t outport, port, endpoint, remote;
180 port = ofw_graph_get_port_by_idx(node, port_id);
183 endpoint = ofw_graph_get_endpoint_by_idx(port, ep_id);
186 remote = ofw_graph_get_remote_endpoint(endpoint);
189 outport = ofw_graph_get_remote_parent(remote);
193 return (OF_device_from_xref(OF_xref_from_node(outport)));