xref: /freebsd/share/man/man9/OF_device_from_xref.9 (revision 944eda42fa6e449ec802958d41302e71a125ca5f)
1ac32b99cSOleksandr Tymoshenko.\"
2ac32b99cSOleksandr Tymoshenko.\" Copyright (c) 2018 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
3ac32b99cSOleksandr Tymoshenko.\"
4ac32b99cSOleksandr Tymoshenko.\" All rights reserved.
5ac32b99cSOleksandr Tymoshenko.\"
6ac32b99cSOleksandr Tymoshenko.\" Redistribution and use in source and binary forms, with or without
7ac32b99cSOleksandr Tymoshenko.\" modification, are permitted provided that the following conditions
8ac32b99cSOleksandr Tymoshenko.\" are met:
9ac32b99cSOleksandr Tymoshenko.\" 1. Redistributions of source code must retain the above copyright
10ac32b99cSOleksandr Tymoshenko.\"    notice, this list of conditions and the following disclaimer.
11ac32b99cSOleksandr Tymoshenko.\" 2. Redistributions in binary form must reproduce the above copyright
12ac32b99cSOleksandr Tymoshenko.\"    notice, this list of conditions and the following disclaimer in the
13ac32b99cSOleksandr Tymoshenko.\"    documentation and/or other materials provided with the distribution.
14ac32b99cSOleksandr Tymoshenko.\"
15ac32b99cSOleksandr Tymoshenko.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
16ac32b99cSOleksandr Tymoshenko.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17ac32b99cSOleksandr Tymoshenko.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18ac32b99cSOleksandr Tymoshenko.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
19ac32b99cSOleksandr Tymoshenko.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20ac32b99cSOleksandr Tymoshenko.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21ac32b99cSOleksandr Tymoshenko.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22ac32b99cSOleksandr Tymoshenko.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23ac32b99cSOleksandr Tymoshenko.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24ac32b99cSOleksandr Tymoshenko.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ac32b99cSOleksandr Tymoshenko.\"
26*944eda42SEmannuel Vadot.Dd April 3, 2025
27ac32b99cSOleksandr Tymoshenko.Dt OF_DEVICE_FROM_XREF 9
28ac32b99cSOleksandr Tymoshenko.Os
29ac32b99cSOleksandr Tymoshenko.Sh NAME
30ac32b99cSOleksandr Tymoshenko.Nm OF_device_from_xref ,
31ac32b99cSOleksandr Tymoshenko.Nm OF_xref_from_device ,
32ac32b99cSOleksandr Tymoshenko.Nm OF_device_register_xref
33*944eda42SEmannuel Vadot.Nm OF_device_unregister_xref
34ac32b99cSOleksandr Tymoshenko.Nd "manage mappings between xrefs and devices"
35ac32b99cSOleksandr Tymoshenko.Sh SYNOPSIS
36ac32b99cSOleksandr Tymoshenko.In dev/ofw/ofw_bus.h
37ac32b99cSOleksandr Tymoshenko.In dev/ofw/ofw_bus_subr.h
38ac32b99cSOleksandr Tymoshenko.Ft int
39ac32b99cSOleksandr Tymoshenko.Fn OF_device_register_xref "phandle_t xref" "device_t dev"
40*944eda42SEmannuel Vadot.Ft void
41*944eda42SEmannuel Vadot.Fn OF_device_unregister_xref "phandle_t xref" "device_t dev"
42ac32b99cSOleksandr Tymoshenko.Ft device_t
43ac32b99cSOleksandr Tymoshenko.Fn OF_device_from_xref "phandle_t xref"
44ac32b99cSOleksandr Tymoshenko.Ft phandle_t
45ac32b99cSOleksandr Tymoshenko.Fn OF_xref_from_device "device_t dev"
46ac32b99cSOleksandr Tymoshenko.Sh DESCRIPTION
47ac32b99cSOleksandr TymoshenkoWhen a device tree node references another node, the driver may
48ac32b99cSOleksandr Tymoshenkoneed to get a device_t instance associated with the referenced node.
49ac32b99cSOleksandr TymoshenkoFor instance, an Ethernet driver accessing a PHY device.
50ac32b99cSOleksandr TymoshenkoTo make this possible, the kernel maintains a table that
51ac32b99cSOleksandr Tymoshenkomaps effective handles to device_t instances.
52ac32b99cSOleksandr Tymoshenko.Pp
53ac32b99cSOleksandr Tymoshenko.Fn OF_device_register_xref
54ac32b99cSOleksandr Tymoshenkoadds a map entry from the effective phandle
55ac32b99cSOleksandr Tymoshenko.Fa xref
56ac32b99cSOleksandr Tymoshenkoto device
57ac32b99cSOleksandr Tymoshenko.Fa dev .
58ac32b99cSOleksandr TymoshenkoIf a mapping entry for
59ac32b99cSOleksandr Tymoshenko.Fa xref
60ac32b99cSOleksandr Tymoshenkoalready exists, it is replaced with the new one.
61ac32b99cSOleksandr TymoshenkoThe function always returns 0.
62ac32b99cSOleksandr Tymoshenko.Pp
63*944eda42SEmannuel Vadot.Fn OF_device_unregister_xref
64*944eda42SEmannuel Vadotremoves a map entry from the effective phandle
65*944eda42SEmannuel Vadot.Fa xref
66*944eda42SEmannuel Vadotto device
67*944eda42SEmannuel Vadot.Fa dev .
68*944eda42SEmannuel VadotIf a mapping entry for
69*944eda42SEmannuel Vadot.Fa xref
70*944eda42SEmannuel Vadotdoes not exists, it silently returns.
71*944eda42SEmannuel Vadot.Pp
72ac32b99cSOleksandr Tymoshenko.Fn OF_device_from_xref
73ac32b99cSOleksandr Tymoshenkoreturns a device_t instance associated with the effective phandle
74ac32b99cSOleksandr Tymoshenko.Fa xref .
75ac32b99cSOleksandr TymoshenkoIf no such mapping exists, the function returns NULL.
76ac32b99cSOleksandr Tymoshenko.Pp
77ac32b99cSOleksandr Tymoshenko.Fn OF_xref_from_device
78ac32b99cSOleksandr Tymoshenkoreturns the effective phandle associated with the device
79ac32b99cSOleksandr Tymoshenko.Fa dev .
80ac32b99cSOleksandr TymoshenkoIf no such mapping exists, the function returns 0.
81ac32b99cSOleksandr Tymoshenko.Sh EXAMPLES
82ac32b99cSOleksandr Tymoshenko.Bd -literal
83ac32b99cSOleksandr Tymoshenko    static int
84ac32b99cSOleksandr Tymoshenko    acmephy_attach(device_t dev)
85ac32b99cSOleksandr Tymoshenko    {
86ac32b99cSOleksandr Tymoshenko        phandle_t node;
87ac32b99cSOleksandr Tymoshenko
88ac32b99cSOleksandr Tymoshenko	/* PHY node is referenced from eth device, register it */
89ac32b99cSOleksandr Tymoshenko        node = ofw_bus_get_node(dev);
90ac32b99cSOleksandr Tymoshenko        OF_device_register_xref(OF_xref_from_node(node), dev);
91ac32b99cSOleksandr Tymoshenko
92ac32b99cSOleksandr Tymoshenko        return (0);
93ac32b99cSOleksandr Tymoshenko    }
94ac32b99cSOleksandr Tymoshenko.Ed
95ac32b99cSOleksandr Tymoshenko.Sh SEE ALSO
96ac32b99cSOleksandr Tymoshenko.Xr OF_node_to_xref 9
97ac32b99cSOleksandr Tymoshenko.Sh AUTHORS
98ac32b99cSOleksandr Tymoshenko.An -nosplit
99ac32b99cSOleksandr TymoshenkoThis manual page was written by
100ac32b99cSOleksandr Tymoshenko.An Oleksandr Tymoshenko Aq Mt gonzo@FreeBSD.org .
101