blob: 8d0d7885aa6d48c7627f694057e5f14f31aec955 [file] [log] [blame]
Simon Glass5063ced2022-07-30 15:52:06 -06001/* SPDX-License-Identifier: GPL-2.0+ */
2/*
3 * Copyright 2022 Google LLC
4 * Written by Simon Glass <sjg@chromium.org>
5 */
6
7#ifndef _DM_OFNODE_DECL_H
8#define _DM_OFNODE_DECL_H
9
10/**
11 * typedef union ofnode_union ofnode - reference to a device tree node
12 *
13 * This union can hold either a straightforward pointer to a struct device_node
14 * in the live device tree, or an offset within the flat device tree. In the
15 * latter case, the pointer value is just the integer offset within the flat DT.
16 *
17 * Thus we can reference nodes in both the live tree (once available) and the
18 * flat tree (until then). Functions are available to translate between an
19 * ofnode and either an offset or a `struct device_node *`.
20 *
21 * The reference can also hold a null offset, in which case the pointer value
22 * here is NULL. This corresponds to a struct device_node * value of
23 * NULL, or an offset of -1.
24 *
25 * There is no ambiguity as to whether ofnode holds an offset or a node
26 * pointer: when the live tree is active it holds a node pointer, otherwise it
27 * holds an offset. The value itself does not need to be unique and in theory
28 * the same value could point to a valid device node or a valid offset. We
29 * could arrange for a unique value to be used (e.g. by making the pointer
30 * point to an offset within the flat device tree in the case of an offset) but
31 * this increases code size slightly due to the subtraction. Since it offers no
32 * real benefit, the approach described here seems best.
33 *
34 * For now these points use constant types, since we don't allow writing
35 * the DT.
36 *
37 * @np: Pointer to device node, used for live tree
38 * @of_offset: Pointer into flat device tree, used for flat tree. Note that this
39 * is not a really a pointer to a node: it is an offset value. See above.
40 */
41typedef union ofnode_union {
Simon Glass98306982022-09-06 20:27:04 -060042 struct device_node *np;
Simon Glass5063ced2022-07-30 15:52:06 -060043 long of_offset;
44} ofnode;
45
46/**
47 * struct ofprop - reference to a property of a device tree node
48 *
49 * This struct hold the reference on one property of one node,
50 * using struct ofnode and an offset within the flat device tree or either
51 * a pointer to a struct property in the live device tree.
52 *
53 * Thus we can reference arguments in both the live tree and the flat tree.
54 *
55 * The property reference can also hold a null reference. This corresponds to
56 * a struct property NULL pointer or an offset of -1.
57 *
58 * @node: Pointer to device node
59 * @offset: Pointer into flat device tree, used for flat tree.
60 * @prop: Pointer to property, used for live treee.
61 */
62
63struct ofprop {
64 ofnode node;
65 union {
66 int offset;
67 const struct property *prop;
68 };
69};
70
Simon Glass33104842022-07-30 15:52:08 -060071/**
72 * union oftree_union - reference to a tree of device tree nodes
73 *
74 * One or other of the members is used, depending on of_live_active()
75 *
76 * @np: Pointer to roott device node, used for live tree
77 * @fdt: Pointer to the flat device tree, used for flat tree
78 */
79typedef union oftree_union {
80 struct device_node *np;
81 void *fdt;
82} oftree;
83
Simon Glass5063ced2022-07-30 15:52:06 -060084#endif
85