xref: /rk3399_rockchip-uboot/doc/driver-model/livetree.txt (revision ced0fd938d37605a22b2c24a351dc454e411cfbc)
1*d944bf6bSSimon GlassDriver Model with Live Device Tree
2*d944bf6bSSimon Glass==================================
3*d944bf6bSSimon Glass
4*d944bf6bSSimon Glass
5*d944bf6bSSimon GlassIntroduction
6*d944bf6bSSimon Glass------------
7*d944bf6bSSimon Glass
8*d944bf6bSSimon GlassTraditionally U-Boot has used a 'flat' device tree. This means that it
9*d944bf6bSSimon Glassreads directly from the device tree binary structure. It is called a flat
10*d944bf6bSSimon Glassdevice tree because nodes are listed one after the other, with the
11*d944bf6bSSimon Glasshierarchy detected by tags in the format.
12*d944bf6bSSimon Glass
13*d944bf6bSSimon GlassThis document describes U-Boot's support for a 'live' device tree, meaning
14*d944bf6bSSimon Glassthat the tree is loaded into a hierarchical data structure within U-Boot.
15*d944bf6bSSimon Glass
16*d944bf6bSSimon Glass
17*d944bf6bSSimon GlassMotivation
18*d944bf6bSSimon Glass----------
19*d944bf6bSSimon Glass
20*d944bf6bSSimon GlassThe flat device tree has several advantages:
21*d944bf6bSSimon Glass
22*d944bf6bSSimon Glass- it is the format produced by the device tree compiler, so no translation
23*d944bf6bSSimon Glassis needed
24*d944bf6bSSimon Glass
25*d944bf6bSSimon Glass- it is fairly compact (e.g. there is no need for pointers)
26*d944bf6bSSimon Glass
27*d944bf6bSSimon Glass- it is accessed by the libfdt library, which is well tested and stable
28*d944bf6bSSimon Glass
29*d944bf6bSSimon Glass
30*d944bf6bSSimon GlassHowever the flat device tree does have some limitations. Adding new
31*d944bf6bSSimon Glassproperties can involve copying large amounts of data around to make room.
32*d944bf6bSSimon GlassThe overall tree has a fixed maximum size so sometimes the tree must be
33*d944bf6bSSimon Glassrebuilt in a new location to create more space. Even if not adding new
34*d944bf6bSSimon Glassproperties or nodes, scanning the tree can be slow. For example, finding
35*d944bf6bSSimon Glassthe parent of a node is a slow process. Reading from nodes involves a
36*d944bf6bSSimon Glasssmall amount parsing which takes a little time.
37*d944bf6bSSimon Glass
38*d944bf6bSSimon GlassDriver model scans the entire device tree sequentially on start-up which
39*d944bf6bSSimon Glassavoids the worst of the flat tree's limitations. But if the tree is to be
40*d944bf6bSSimon Glassmodified at run-time, a live tree is much faster. Even if no modification
41*d944bf6bSSimon Glassis necessary, parsing the tree once and using a live tree from then on
42*d944bf6bSSimon Glassseems to save a little time.
43*d944bf6bSSimon Glass
44*d944bf6bSSimon Glass
45*d944bf6bSSimon GlassImplementation
46*d944bf6bSSimon Glass--------------
47*d944bf6bSSimon Glass
48*d944bf6bSSimon GlassIn U-Boot a live device tree ('livetree') is currently supported only
49*d944bf6bSSimon Glassafter relocation. Therefore we need a mechanism to specify a device
50*d944bf6bSSimon Glasstree node regardless of whether it is in the flat tree or livetree.
51*d944bf6bSSimon Glass
52*d944bf6bSSimon GlassThe 'ofnode' type provides this. An ofnode can point to either a flat tree
53*d944bf6bSSimon Glassnode (when the live tree node is not yet set up) or a livetree node. The
54*d944bf6bSSimon Glasscaller of an ofnode function does not need to worry about these details.
55*d944bf6bSSimon Glass
56*d944bf6bSSimon GlassThe main users of the information in a device tree are  drivers. These have
57*d944bf6bSSimon Glassa 'struct udevice *' which is attached to a device tree node. Therefore it
58*d944bf6bSSimon Glassmakes sense to be able to read device tree  properties using the
59*d944bf6bSSimon Glass'struct udevice *', rather than having to obtain the ofnode first.
60*d944bf6bSSimon Glass
61*d944bf6bSSimon GlassThe 'dev_read_...()' interface provides this. It allows properties to be
62*d944bf6bSSimon Glasseasily read from the device tree using only a device pointer. Under the
63*d944bf6bSSimon Glasshood it uses ofnode so it works with both flat and live device trees.
64*d944bf6bSSimon Glass
65*d944bf6bSSimon Glass
66*d944bf6bSSimon GlassEnabling livetree
67*d944bf6bSSimon Glass-----------------
68*d944bf6bSSimon Glass
69*d944bf6bSSimon GlassCONFIG_OF_LIVE enables livetree. When this option is enabled, the flat
70*d944bf6bSSimon Glasstree will be used in SPL and before relocation in U-Boot proper. Just
71*d944bf6bSSimon Glassbefore relocation a livetree is built, and this is used for U-Boot proper
72*d944bf6bSSimon Glassafter relocation.
73*d944bf6bSSimon Glass
74*d944bf6bSSimon GlassMost checks for livetree use CONFIG_IS_ENABLED(OF_LIVE). This means that
75*d944bf6bSSimon Glassfor SPL, the CONFIG_SPL_OF_LIVE option is checked. At present this does
76*d944bf6bSSimon Glassnot exist, since SPL does not support livetree.
77*d944bf6bSSimon Glass
78*d944bf6bSSimon Glass
79*d944bf6bSSimon GlassPorting drivers
80*d944bf6bSSimon Glass---------------
81*d944bf6bSSimon Glass
82*d944bf6bSSimon GlassMany existing drivers use the fdtdec interface to read device tree
83*d944bf6bSSimon Glassproperties. This only works with a flat device tree. The drivers should be
84*d944bf6bSSimon Glassconverted to use the dev_read_() interface.
85*d944bf6bSSimon Glass
86*d944bf6bSSimon GlassFor example, the old code may be like this:
87*d944bf6bSSimon Glass
88*d944bf6bSSimon Glass    struct udevice *bus;
89*d944bf6bSSimon Glass    const void *blob = gd->fdt_blob;
90*d944bf6bSSimon Glass    int node = dev_of_offset(bus);
91*d944bf6bSSimon Glass
92*d944bf6bSSimon Glass    i2c_bus->regs = (struct i2c_ctlr *)devfdt_get_addr(dev);
93*d944bf6bSSimon Glass    plat->frequency = fdtdec_get_int(blob, node, "spi-max-frequency", 500000);
94*d944bf6bSSimon Glass
95*d944bf6bSSimon GlassThe new code is:
96*d944bf6bSSimon Glass
97*d944bf6bSSimon Glass    struct udevice *bus;
98*d944bf6bSSimon Glass
99*d944bf6bSSimon Glass    i2c_bus->regs = (struct i2c_ctlr *)dev_read_addr(dev);
100*d944bf6bSSimon Glass    plat->frequency = dev_read_u32_default(bus, "spi-max-frequency", 500000);
101*d944bf6bSSimon Glass
102*d944bf6bSSimon GlassThe dev_read_...() interface is more convenient and works with both the
103*d944bf6bSSimon Glassflat and live device trees. See include/dm/read.h for a list of functions.
104*d944bf6bSSimon Glass
105*d944bf6bSSimon GlassWhere properties must be read from sub-nodes or other nodes, you must fall
106*d944bf6bSSimon Glassback to using ofnode. For example, for old code like this:
107*d944bf6bSSimon Glass
108*d944bf6bSSimon Glass    const void *blob = gd->fdt_blob;
109*d944bf6bSSimon Glass    int subnode;
110*d944bf6bSSimon Glass
111*d944bf6bSSimon Glass    fdt_for_each_subnode(subnode, blob, dev_of_offset(dev)) {
112*d944bf6bSSimon Glass        freq = fdtdec_get_int(blob, node, "spi-max-frequency", 500000);
113*d944bf6bSSimon Glass        ...
114*d944bf6bSSimon Glass    }
115*d944bf6bSSimon Glass
116*d944bf6bSSimon Glassyou should use:
117*d944bf6bSSimon Glass
118*d944bf6bSSimon Glass    ofnode subnode;
119*d944bf6bSSimon Glass
120*d944bf6bSSimon Glass    ofnode_for_each_subnode(subnode, dev_ofnode(dev)) {
121*d944bf6bSSimon Glass        freq = ofnode_read_u32(node, "spi-max-frequency", 500000);
122*d944bf6bSSimon Glass        ...
123*d944bf6bSSimon Glass    }
124*d944bf6bSSimon Glass
125*d944bf6bSSimon Glass
126*d944bf6bSSimon GlassUseful ofnode functions
127*d944bf6bSSimon Glass-----------------------
128*d944bf6bSSimon Glass
129*d944bf6bSSimon GlassThe internal data structures of the livetree are defined in include/dm/of.h :
130*d944bf6bSSimon Glass
131*d944bf6bSSimon Glass   struct device_node - holds information about a device tree node
132*d944bf6bSSimon Glass   struct property    - holds information about a property within a node
133*d944bf6bSSimon Glass
134*d944bf6bSSimon GlassNodes have pointers to their first property, their parent, their first child
135*d944bf6bSSimon Glassand their sibling. This allows nodes to be linked together in a hierarchical
136*d944bf6bSSimon Glasstree.
137*d944bf6bSSimon Glass
138*d944bf6bSSimon GlassProperties have pointers to the next property. This allows all properties of
139*d944bf6bSSimon Glassa node to be linked together in a chain.
140*d944bf6bSSimon Glass
141*d944bf6bSSimon GlassIt should not be necessary to use these data structures in normal code. In
142*d944bf6bSSimon Glassparticular, you should refrain from using functions which access the livetree
143*d944bf6bSSimon Glassdirectly, such as of_read_u32(). Use ofnode functions instead, to allow your
144*d944bf6bSSimon Glasscode to work with a flat tree also.
145*d944bf6bSSimon Glass
146*d944bf6bSSimon GlassSome conversion functions are used internally. Generally these are not needed
147*d944bf6bSSimon Glassfor driver code. Note that they will not work if called in the wrong context.
148*d944bf6bSSimon GlassFor example it is invalid to call ofnode_to_no() when a flat tree is being
149*d944bf6bSSimon Glassused. Similarly it is not possible to call ofnode_to_offset() on a livetree
150*d944bf6bSSimon Glassnode.
151*d944bf6bSSimon Glass
152*d944bf6bSSimon Glass   ofnode_to_np() - converts ofnode to struct device_node *
153*d944bf6bSSimon Glass   ofnode_to_offset() - converts ofnode to offset
154*d944bf6bSSimon Glass
155*d944bf6bSSimon Glass   no_to_ofnode() - converts node pointer to ofnode
156*d944bf6bSSimon Glass   offset_to_ofnode() - converts offset to ofnode
157*d944bf6bSSimon Glass
158*d944bf6bSSimon Glass
159*d944bf6bSSimon GlassOther useful functions:
160*d944bf6bSSimon Glass
161*d944bf6bSSimon Glass   of_live_active() returns true if livetree is in use, false if flat tree
162*d944bf6bSSimon Glass   ofnode_valid() return true if a given node is valid
163*d944bf6bSSimon Glass   ofnode_is_np() returns true if a given node is a livetree node
164*d944bf6bSSimon Glass   ofnode_equal() compares two ofnodes
165*d944bf6bSSimon Glass   ofnode_null() returns a null ofnode (for which ofnode_valid() returns false)
166*d944bf6bSSimon Glass
167*d944bf6bSSimon Glass
168*d944bf6bSSimon GlassPhandles
169*d944bf6bSSimon Glass--------
170*d944bf6bSSimon Glass
171*d944bf6bSSimon GlassThere is full phandle support for live tree. All functions make use of
172*d944bf6bSSimon Glassstruct ofnode_phandle_args, which has an ofnode within it. This supports both
173*d944bf6bSSimon Glasslivetree and flat tree transparently. See for example
174*d944bf6bSSimon Glassofnode_parse_phandle_with_args().
175*d944bf6bSSimon Glass
176*d944bf6bSSimon Glass
177*d944bf6bSSimon GlassReading addresses
178*d944bf6bSSimon Glass-----------------
179*d944bf6bSSimon Glass
180*d944bf6bSSimon GlassYou should use dev_read_addr() and friends to read addresses from device-tree
181*d944bf6bSSimon Glassnodes.
182*d944bf6bSSimon Glass
183*d944bf6bSSimon Glass
184*d944bf6bSSimon Glassfdtdec
185*d944bf6bSSimon Glass------
186*d944bf6bSSimon Glass
187*d944bf6bSSimon GlassThe existing fdtdec interface will eventually be retired. Please try to avoid
188*d944bf6bSSimon Glassusing it in new code.
189*d944bf6bSSimon Glass
190*d944bf6bSSimon Glass
191*d944bf6bSSimon GlassModifying the livetree
192*d944bf6bSSimon Glass----------------------
193*d944bf6bSSimon Glass
194*d944bf6bSSimon GlassThis is not currently supported. Once implemented it should provide a much
195*d944bf6bSSimon Glassmore efficient implementation for modification of the device tree than using
196*d944bf6bSSimon Glassthe flat tree.
197*d944bf6bSSimon Glass
198*d944bf6bSSimon Glass
199*d944bf6bSSimon GlassInternal implementation
200*d944bf6bSSimon Glass-----------------------
201*d944bf6bSSimon Glass
202*d944bf6bSSimon GlassThe dev_read_...() functions have two implementations. When
203*d944bf6bSSimon GlassCONFIG_DM_DEV_READ_INLINE is enabled, these functions simply call the ofnode
204*d944bf6bSSimon Glassfunctions directly. This is useful when livetree is not enabled. The ofnode
205*d944bf6bSSimon Glassfunctions call ofnode_is_np(node) which will always return false if livetree
206*d944bf6bSSimon Glassis disabled, just falling back to flat tree code.
207*d944bf6bSSimon Glass
208*d944bf6bSSimon GlassThis optimisation means that without livetree enabled, the dev_read_...() and
209*d944bf6bSSimon Glassofnode interfaces do not noticeably add to code size.
210*d944bf6bSSimon Glass
211*d944bf6bSSimon GlassThe CONFIG_DM_DEV_READ_INLINE option defaults to enabled when livetree is
212*d944bf6bSSimon Glassdisabled.
213*d944bf6bSSimon Glass
214*d944bf6bSSimon GlassMost livetree code comes directly from Linux and is modified as little as
215*d944bf6bSSimon Glasspossible. This is deliberate since this code is fairly stable and does what
216*d944bf6bSSimon Glasswe want. Some features (such as get/put) are not supported. Internal macros
217*d944bf6bSSimon Glasstake care of removing these features silently.
218*d944bf6bSSimon Glass
219*d944bf6bSSimon GlassWithin the of_access.c file there are pointers to the alias node, the chosen
220*d944bf6bSSimon Glassnode and the stdout-path alias.
221*d944bf6bSSimon Glass
222*d944bf6bSSimon Glass
223*d944bf6bSSimon GlassErrors
224*d944bf6bSSimon Glass------
225*d944bf6bSSimon Glass
226*d944bf6bSSimon GlassWith a flat device tree, libfdt errors are returned (e.g. -FDT_ERR_NOTFOUND).
227*d944bf6bSSimon GlassFor livetree normal 'errno' errors are returned (e.g. -ENOTFOUND). At present
228*d944bf6bSSimon Glassthe ofnode and dev_read_...() functions return either one or other type of
229*d944bf6bSSimon Glasserror. This is clearly not desirable. Once tests are added for all the
230*d944bf6bSSimon Glassfunctions this can be tidied up.
231*d944bf6bSSimon Glass
232*d944bf6bSSimon Glass
233*d944bf6bSSimon GlassAdding new access functions
234*d944bf6bSSimon Glass---------------------------
235*d944bf6bSSimon Glass
236*d944bf6bSSimon GlassAdding a new function for device-tree access involves the following steps:
237*d944bf6bSSimon Glass
238*d944bf6bSSimon Glass   - Add two dev_read() functions:
239*d944bf6bSSimon Glass	- inline version in the read.h header file, which calls an ofnode
240*d944bf6bSSimon Glass		function
241*d944bf6bSSimon Glass	- standard version in the read.c file (or perhaps another file), which
242*d944bf6bSSimon Glass		also calls an ofnode function
243*d944bf6bSSimon Glass
244*d944bf6bSSimon Glass	The implementations of these functions can be the same. The purpose
245*d944bf6bSSimon Glass	of the inline version is purely to reduce code size impact.
246*d944bf6bSSimon Glass
247*d944bf6bSSimon Glass   - Add an ofnode function. This should call ofnode_is_np() to work out
248*d944bf6bSSimon Glass	whether a livetree or flat tree is used. For the livetree it should
249*d944bf6bSSimon Glass	call an of_...() function. For the flat tree it should call an
250*d944bf6bSSimon Glass	fdt_...() function. The livetree version will be optimised out at
251*d944bf6bSSimon Glass	compile time if livetree is not enabled.
252*d944bf6bSSimon Glass
253*d944bf6bSSimon Glass   - Add an of_...() function for the livetree implementation. If a similar
254*d944bf6bSSimon Glass	function is available in Linux, the implementation should be taken
255*d944bf6bSSimon Glass	from there and modified as little as possible (generally not at all).
256*d944bf6bSSimon Glass
257*d944bf6bSSimon Glass
258*d944bf6bSSimon GlassFuture work
259*d944bf6bSSimon Glass-----------
260*d944bf6bSSimon Glass
261*d944bf6bSSimon GlassLive tree support was introduced in U-Boot 2017.07. There is still quite a bit
262*d944bf6bSSimon Glassof work to do to flesh this out:
263*d944bf6bSSimon Glass
264*d944bf6bSSimon Glass- tests for all access functions
265*d944bf6bSSimon Glass- support for livetree modification
266*d944bf6bSSimon Glass- addition of more access functions as needed
267*d944bf6bSSimon Glass- support for livetree in SPL and before relocation (if desired)
268*d944bf6bSSimon Glass
269*d944bf6bSSimon Glass
270*d944bf6bSSimon Glass--
271*d944bf6bSSimon GlassSimon Glass <sjg@chromium.org>
272*d944bf6bSSimon Glass5-Aug-17
273