xref: /OK3568_Linux_fs/kernel/Documentation/devicetree/usage-model.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun.. SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun
3*4882a593Smuzhiyun=========================
4*4882a593SmuzhiyunLinux and the Device Tree
5*4882a593Smuzhiyun=========================
6*4882a593Smuzhiyun
7*4882a593SmuzhiyunThe Linux usage model for device tree data
8*4882a593Smuzhiyun
9*4882a593Smuzhiyun:Author: Grant Likely <grant.likely@secretlab.ca>
10*4882a593Smuzhiyun
11*4882a593SmuzhiyunThis article describes how Linux uses the device tree.  An overview of
12*4882a593Smuzhiyunthe device tree data format can be found on the device tree usage page
13*4882a593Smuzhiyunat devicetree.org\ [1]_.
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun.. [1] https://elinux.org/Device_Tree_Usage
16*4882a593Smuzhiyun
17*4882a593SmuzhiyunThe "Open Firmware Device Tree", or simply Device Tree (DT), is a data
18*4882a593Smuzhiyunstructure and language for describing hardware.  More specifically, it
19*4882a593Smuzhiyunis a description of hardware that is readable by an operating system
20*4882a593Smuzhiyunso that the operating system doesn't need to hard code details of the
21*4882a593Smuzhiyunmachine.
22*4882a593Smuzhiyun
23*4882a593SmuzhiyunStructurally, the DT is a tree, or acyclic graph with named nodes, and
24*4882a593Smuzhiyunnodes may have an arbitrary number of named properties encapsulating
25*4882a593Smuzhiyunarbitrary data.  A mechanism also exists to create arbitrary
26*4882a593Smuzhiyunlinks from one node to another outside of the natural tree structure.
27*4882a593Smuzhiyun
28*4882a593SmuzhiyunConceptually, a common set of usage conventions, called 'bindings',
29*4882a593Smuzhiyunis defined for how data should appear in the tree to describe typical
30*4882a593Smuzhiyunhardware characteristics including data busses, interrupt lines, GPIO
31*4882a593Smuzhiyunconnections, and peripheral devices.
32*4882a593Smuzhiyun
33*4882a593SmuzhiyunAs much as possible, hardware is described using existing bindings to
34*4882a593Smuzhiyunmaximize use of existing support code, but since property and node
35*4882a593Smuzhiyunnames are simply text strings, it is easy to extend existing bindings
36*4882a593Smuzhiyunor create new ones by defining new nodes and properties.  Be wary,
37*4882a593Smuzhiyunhowever, of creating a new binding without first doing some homework
38*4882a593Smuzhiyunabout what already exists.  There are currently two different,
39*4882a593Smuzhiyunincompatible, bindings for i2c busses that came about because the new
40*4882a593Smuzhiyunbinding was created without first investigating how i2c devices were
41*4882a593Smuzhiyunalready being enumerated in existing systems.
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun1. History
44*4882a593Smuzhiyun----------
45*4882a593SmuzhiyunThe DT was originally created by Open Firmware as part of the
46*4882a593Smuzhiyuncommunication method for passing data from Open Firmware to a client
47*4882a593Smuzhiyunprogram (like to an operating system).  An operating system used the
48*4882a593SmuzhiyunDevice Tree to discover the topology of the hardware at runtime, and
49*4882a593Smuzhiyunthereby support a majority of available hardware without hard coded
50*4882a593Smuzhiyuninformation (assuming drivers were available for all devices).
51*4882a593Smuzhiyun
52*4882a593SmuzhiyunSince Open Firmware is commonly used on PowerPC and SPARC platforms,
53*4882a593Smuzhiyunthe Linux support for those architectures has for a long time used the
54*4882a593SmuzhiyunDevice Tree.
55*4882a593Smuzhiyun
56*4882a593SmuzhiyunIn 2005, when PowerPC Linux began a major cleanup and to merge 32-bit
57*4882a593Smuzhiyunand 64-bit support, the decision was made to require DT support on all
58*4882a593Smuzhiyunpowerpc platforms, regardless of whether or not they used Open
59*4882a593SmuzhiyunFirmware.  To do this, a DT representation called the Flattened Device
60*4882a593SmuzhiyunTree (FDT) was created which could be passed to the kernel as a binary
61*4882a593Smuzhiyunblob without requiring a real Open Firmware implementation.  U-Boot,
62*4882a593Smuzhiyunkexec, and other bootloaders were modified to support both passing a
63*4882a593SmuzhiyunDevice Tree Binary (dtb) and to modify a dtb at boot time.  DT was
64*4882a593Smuzhiyunalso added to the PowerPC boot wrapper (``arch/powerpc/boot/*``) so that
65*4882a593Smuzhiyuna dtb could be wrapped up with the kernel image to support booting
66*4882a593Smuzhiyunexisting non-DT aware firmware.
67*4882a593Smuzhiyun
68*4882a593SmuzhiyunSome time later, FDT infrastructure was generalized to be usable by
69*4882a593Smuzhiyunall architectures.  At the time of this writing, 6 mainlined
70*4882a593Smuzhiyunarchitectures (arm, microblaze, mips, powerpc, sparc, and x86) and 1
71*4882a593Smuzhiyunout of mainline (nios) have some level of DT support.
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun2. Data Model
74*4882a593Smuzhiyun-------------
75*4882a593SmuzhiyunIf you haven't already read the Device Tree Usage\ [1]_ page,
76*4882a593Smuzhiyunthen go read it now.  It's okay, I'll wait....
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun2.1 High Level View
79*4882a593Smuzhiyun-------------------
80*4882a593SmuzhiyunThe most important thing to understand is that the DT is simply a data
81*4882a593Smuzhiyunstructure that describes the hardware.  There is nothing magical about
82*4882a593Smuzhiyunit, and it doesn't magically make all hardware configuration problems
83*4882a593Smuzhiyungo away.  What it does do is provide a language for decoupling the
84*4882a593Smuzhiyunhardware configuration from the board and device driver support in the
85*4882a593SmuzhiyunLinux kernel (or any other operating system for that matter).  Using
86*4882a593Smuzhiyunit allows board and device support to become data driven; to make
87*4882a593Smuzhiyunsetup decisions based on data passed into the kernel instead of on
88*4882a593Smuzhiyunper-machine hard coded selections.
89*4882a593Smuzhiyun
90*4882a593SmuzhiyunIdeally, data driven platform setup should result in less code
91*4882a593Smuzhiyunduplication and make it easier to support a wide range of hardware
92*4882a593Smuzhiyunwith a single kernel image.
93*4882a593Smuzhiyun
94*4882a593SmuzhiyunLinux uses DT data for three major purposes:
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun1) platform identification,
97*4882a593Smuzhiyun2) runtime configuration, and
98*4882a593Smuzhiyun3) device population.
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun2.2 Platform Identification
101*4882a593Smuzhiyun---------------------------
102*4882a593SmuzhiyunFirst and foremost, the kernel will use data in the DT to identify the
103*4882a593Smuzhiyunspecific machine.  In a perfect world, the specific platform shouldn't
104*4882a593Smuzhiyunmatter to the kernel because all platform details would be described
105*4882a593Smuzhiyunperfectly by the device tree in a consistent and reliable manner.
106*4882a593SmuzhiyunHardware is not perfect though, and so the kernel must identify the
107*4882a593Smuzhiyunmachine during early boot so that it has the opportunity to run
108*4882a593Smuzhiyunmachine-specific fixups.
109*4882a593Smuzhiyun
110*4882a593SmuzhiyunIn the majority of cases, the machine identity is irrelevant, and the
111*4882a593Smuzhiyunkernel will instead select setup code based on the machine's core
112*4882a593SmuzhiyunCPU or SoC.  On ARM for example, setup_arch() in
113*4882a593Smuzhiyunarch/arm/kernel/setup.c will call setup_machine_fdt() in
114*4882a593Smuzhiyunarch/arm/kernel/devtree.c which searches through the machine_desc
115*4882a593Smuzhiyuntable and selects the machine_desc which best matches the device tree
116*4882a593Smuzhiyundata.  It determines the best match by looking at the 'compatible'
117*4882a593Smuzhiyunproperty in the root device tree node, and comparing it with the
118*4882a593Smuzhiyundt_compat list in struct machine_desc (which is defined in
119*4882a593Smuzhiyunarch/arm/include/asm/mach/arch.h if you're curious).
120*4882a593Smuzhiyun
121*4882a593SmuzhiyunThe 'compatible' property contains a sorted list of strings starting
122*4882a593Smuzhiyunwith the exact name of the machine, followed by an optional list of
123*4882a593Smuzhiyunboards it is compatible with sorted from most compatible to least.  For
124*4882a593Smuzhiyunexample, the root compatible properties for the TI BeagleBoard and its
125*4882a593Smuzhiyunsuccessor, the BeagleBoard xM board might look like, respectively::
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun	compatible = "ti,omap3-beagleboard", "ti,omap3450", "ti,omap3";
128*4882a593Smuzhiyun	compatible = "ti,omap3-beagleboard-xm", "ti,omap3450", "ti,omap3";
129*4882a593Smuzhiyun
130*4882a593SmuzhiyunWhere "ti,omap3-beagleboard-xm" specifies the exact model, it also
131*4882a593Smuzhiyunclaims that it compatible with the OMAP 3450 SoC, and the omap3 family
132*4882a593Smuzhiyunof SoCs in general.  You'll notice that the list is sorted from most
133*4882a593Smuzhiyunspecific (exact board) to least specific (SoC family).
134*4882a593Smuzhiyun
135*4882a593SmuzhiyunAstute readers might point out that the Beagle xM could also claim
136*4882a593Smuzhiyuncompatibility with the original Beagle board.  However, one should be
137*4882a593Smuzhiyuncautioned about doing so at the board level since there is typically a
138*4882a593Smuzhiyunhigh level of change from one board to another, even within the same
139*4882a593Smuzhiyunproduct line, and it is hard to nail down exactly what is meant when one
140*4882a593Smuzhiyunboard claims to be compatible with another.  For the top level, it is
141*4882a593Smuzhiyunbetter to err on the side of caution and not claim one board is
142*4882a593Smuzhiyuncompatible with another.  The notable exception would be when one
143*4882a593Smuzhiyunboard is a carrier for another, such as a CPU module attached to a
144*4882a593Smuzhiyuncarrier board.
145*4882a593Smuzhiyun
146*4882a593SmuzhiyunOne more note on compatible values.  Any string used in a compatible
147*4882a593Smuzhiyunproperty must be documented as to what it indicates.  Add
148*4882a593Smuzhiyundocumentation for compatible strings in Documentation/devicetree/bindings.
149*4882a593Smuzhiyun
150*4882a593SmuzhiyunAgain on ARM, for each machine_desc, the kernel looks to see if
151*4882a593Smuzhiyunany of the dt_compat list entries appear in the compatible property.
152*4882a593SmuzhiyunIf one does, then that machine_desc is a candidate for driving the
153*4882a593Smuzhiyunmachine.  After searching the entire table of machine_descs,
154*4882a593Smuzhiyunsetup_machine_fdt() returns the 'most compatible' machine_desc based
155*4882a593Smuzhiyunon which entry in the compatible property each machine_desc matches
156*4882a593Smuzhiyunagainst.  If no matching machine_desc is found, then it returns NULL.
157*4882a593Smuzhiyun
158*4882a593SmuzhiyunThe reasoning behind this scheme is the observation that in the majority
159*4882a593Smuzhiyunof cases, a single machine_desc can support a large number of boards
160*4882a593Smuzhiyunif they all use the same SoC, or same family of SoCs.  However,
161*4882a593Smuzhiyuninvariably there will be some exceptions where a specific board will
162*4882a593Smuzhiyunrequire special setup code that is not useful in the generic case.
163*4882a593SmuzhiyunSpecial cases could be handled by explicitly checking for the
164*4882a593Smuzhiyuntroublesome board(s) in generic setup code, but doing so very quickly
165*4882a593Smuzhiyunbecomes ugly and/or unmaintainable if it is more than just a couple of
166*4882a593Smuzhiyuncases.
167*4882a593Smuzhiyun
168*4882a593SmuzhiyunInstead, the compatible list allows a generic machine_desc to provide
169*4882a593Smuzhiyunsupport for a wide common set of boards by specifying "less
170*4882a593Smuzhiyuncompatible" values in the dt_compat list.  In the example above,
171*4882a593Smuzhiyungeneric board support can claim compatibility with "ti,omap3" or
172*4882a593Smuzhiyun"ti,omap3450".  If a bug was discovered on the original beagleboard
173*4882a593Smuzhiyunthat required special workaround code during early boot, then a new
174*4882a593Smuzhiyunmachine_desc could be added which implements the workarounds and only
175*4882a593Smuzhiyunmatches on "ti,omap3-beagleboard".
176*4882a593Smuzhiyun
177*4882a593SmuzhiyunPowerPC uses a slightly different scheme where it calls the .probe()
178*4882a593Smuzhiyunhook from each machine_desc, and the first one returning TRUE is used.
179*4882a593SmuzhiyunHowever, this approach does not take into account the priority of the
180*4882a593Smuzhiyuncompatible list, and probably should be avoided for new architecture
181*4882a593Smuzhiyunsupport.
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun2.3 Runtime configuration
184*4882a593Smuzhiyun-------------------------
185*4882a593SmuzhiyunIn most cases, a DT will be the sole method of communicating data from
186*4882a593Smuzhiyunfirmware to the kernel, so also gets used to pass in runtime and
187*4882a593Smuzhiyunconfiguration data like the kernel parameters string and the location
188*4882a593Smuzhiyunof an initrd image.
189*4882a593Smuzhiyun
190*4882a593SmuzhiyunMost of this data is contained in the /chosen node, and when booting
191*4882a593SmuzhiyunLinux it will look something like this::
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun	chosen {
194*4882a593Smuzhiyun		bootargs = "console=ttyS0,115200 loglevel=8";
195*4882a593Smuzhiyun		initrd-start = <0xc8000000>;
196*4882a593Smuzhiyun		initrd-end = <0xc8200000>;
197*4882a593Smuzhiyun	};
198*4882a593Smuzhiyun
199*4882a593SmuzhiyunThe bootargs property contains the kernel arguments, and the initrd-*
200*4882a593Smuzhiyunproperties define the address and size of an initrd blob.  Note that
201*4882a593Smuzhiyuninitrd-end is the first address after the initrd image, so this doesn't
202*4882a593Smuzhiyunmatch the usual semantic of struct resource.  The chosen node may also
203*4882a593Smuzhiyunoptionally contain an arbitrary number of additional properties for
204*4882a593Smuzhiyunplatform-specific configuration data.
205*4882a593Smuzhiyun
206*4882a593SmuzhiyunDuring early boot, the architecture setup code calls of_scan_flat_dt()
207*4882a593Smuzhiyunseveral times with different helper callbacks to parse device tree
208*4882a593Smuzhiyundata before paging is setup.  The of_scan_flat_dt() code scans through
209*4882a593Smuzhiyunthe device tree and uses the helpers to extract information required
210*4882a593Smuzhiyunduring early boot.  Typically the early_init_dt_scan_chosen() helper
211*4882a593Smuzhiyunis used to parse the chosen node including kernel parameters,
212*4882a593Smuzhiyunearly_init_dt_scan_root() to initialize the DT address space model,
213*4882a593Smuzhiyunand early_init_dt_scan_memory() to determine the size and
214*4882a593Smuzhiyunlocation of usable RAM.
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunOn ARM, the function setup_machine_fdt() is responsible for early
217*4882a593Smuzhiyunscanning of the device tree after selecting the correct machine_desc
218*4882a593Smuzhiyunthat supports the board.
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun2.4 Device population
221*4882a593Smuzhiyun---------------------
222*4882a593SmuzhiyunAfter the board has been identified, and after the early configuration data
223*4882a593Smuzhiyunhas been parsed, then kernel initialization can proceed in the normal
224*4882a593Smuzhiyunway.  At some point in this process, unflatten_device_tree() is called
225*4882a593Smuzhiyunto convert the data into a more efficient runtime representation.
226*4882a593SmuzhiyunThis is also when machine-specific setup hooks will get called, like
227*4882a593Smuzhiyunthe machine_desc .init_early(), .init_irq() and .init_machine() hooks
228*4882a593Smuzhiyunon ARM.  The remainder of this section uses examples from the ARM
229*4882a593Smuzhiyunimplementation, but all architectures will do pretty much the same
230*4882a593Smuzhiyunthing when using a DT.
231*4882a593Smuzhiyun
232*4882a593SmuzhiyunAs can be guessed by the names, .init_early() is used for any machine-
233*4882a593Smuzhiyunspecific setup that needs to be executed early in the boot process,
234*4882a593Smuzhiyunand .init_irq() is used to set up interrupt handling.  Using a DT
235*4882a593Smuzhiyundoesn't materially change the behaviour of either of these functions.
236*4882a593SmuzhiyunIf a DT is provided, then both .init_early() and .init_irq() are able
237*4882a593Smuzhiyunto call any of the DT query functions (of_* in include/linux/of*.h) to
238*4882a593Smuzhiyunget additional data about the platform.
239*4882a593Smuzhiyun
240*4882a593SmuzhiyunThe most interesting hook in the DT context is .init_machine() which
241*4882a593Smuzhiyunis primarily responsible for populating the Linux device model with
242*4882a593Smuzhiyundata about the platform.  Historically this has been implemented on
243*4882a593Smuzhiyunembedded platforms by defining a set of static clock structures,
244*4882a593Smuzhiyunplatform_devices, and other data in the board support .c file, and
245*4882a593Smuzhiyunregistering it en-masse in .init_machine().  When DT is used, then
246*4882a593Smuzhiyuninstead of hard coding static devices for each platform, the list of
247*4882a593Smuzhiyundevices can be obtained by parsing the DT, and allocating device
248*4882a593Smuzhiyunstructures dynamically.
249*4882a593Smuzhiyun
250*4882a593SmuzhiyunThe simplest case is when .init_machine() is only responsible for
251*4882a593Smuzhiyunregistering a block of platform_devices.  A platform_device is a concept
252*4882a593Smuzhiyunused by Linux for memory or I/O mapped devices which cannot be detected
253*4882a593Smuzhiyunby hardware, and for 'composite' or 'virtual' devices (more on those
254*4882a593Smuzhiyunlater).  While there is no 'platform device' terminology for the DT,
255*4882a593Smuzhiyunplatform devices roughly correspond to device nodes at the root of the
256*4882a593Smuzhiyuntree and children of simple memory mapped bus nodes.
257*4882a593Smuzhiyun
258*4882a593SmuzhiyunAbout now is a good time to lay out an example.  Here is part of the
259*4882a593Smuzhiyundevice tree for the NVIDIA Tegra board::
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun  /{
262*4882a593Smuzhiyun	compatible = "nvidia,harmony", "nvidia,tegra20";
263*4882a593Smuzhiyun	#address-cells = <1>;
264*4882a593Smuzhiyun	#size-cells = <1>;
265*4882a593Smuzhiyun	interrupt-parent = <&intc>;
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun	chosen { };
268*4882a593Smuzhiyun	aliases { };
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun	memory {
271*4882a593Smuzhiyun		device_type = "memory";
272*4882a593Smuzhiyun		reg = <0x00000000 0x40000000>;
273*4882a593Smuzhiyun	};
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun	soc {
276*4882a593Smuzhiyun		compatible = "nvidia,tegra20-soc", "simple-bus";
277*4882a593Smuzhiyun		#address-cells = <1>;
278*4882a593Smuzhiyun		#size-cells = <1>;
279*4882a593Smuzhiyun		ranges;
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun		intc: interrupt-controller@50041000 {
282*4882a593Smuzhiyun			compatible = "nvidia,tegra20-gic";
283*4882a593Smuzhiyun			interrupt-controller;
284*4882a593Smuzhiyun			#interrupt-cells = <1>;
285*4882a593Smuzhiyun			reg = <0x50041000 0x1000>, < 0x50040100 0x0100 >;
286*4882a593Smuzhiyun		};
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun		serial@70006300 {
289*4882a593Smuzhiyun			compatible = "nvidia,tegra20-uart";
290*4882a593Smuzhiyun			reg = <0x70006300 0x100>;
291*4882a593Smuzhiyun			interrupts = <122>;
292*4882a593Smuzhiyun		};
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun		i2s1: i2s@70002800 {
295*4882a593Smuzhiyun			compatible = "nvidia,tegra20-i2s";
296*4882a593Smuzhiyun			reg = <0x70002800 0x100>;
297*4882a593Smuzhiyun			interrupts = <77>;
298*4882a593Smuzhiyun			codec = <&wm8903>;
299*4882a593Smuzhiyun		};
300*4882a593Smuzhiyun
301*4882a593Smuzhiyun		i2c@7000c000 {
302*4882a593Smuzhiyun			compatible = "nvidia,tegra20-i2c";
303*4882a593Smuzhiyun			#address-cells = <1>;
304*4882a593Smuzhiyun			#size-cells = <0>;
305*4882a593Smuzhiyun			reg = <0x7000c000 0x100>;
306*4882a593Smuzhiyun			interrupts = <70>;
307*4882a593Smuzhiyun
308*4882a593Smuzhiyun			wm8903: codec@1a {
309*4882a593Smuzhiyun				compatible = "wlf,wm8903";
310*4882a593Smuzhiyun				reg = <0x1a>;
311*4882a593Smuzhiyun				interrupts = <347>;
312*4882a593Smuzhiyun			};
313*4882a593Smuzhiyun		};
314*4882a593Smuzhiyun	};
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun	sound {
317*4882a593Smuzhiyun		compatible = "nvidia,harmony-sound";
318*4882a593Smuzhiyun		i2s-controller = <&i2s1>;
319*4882a593Smuzhiyun		i2s-codec = <&wm8903>;
320*4882a593Smuzhiyun	};
321*4882a593Smuzhiyun  };
322*4882a593Smuzhiyun
323*4882a593SmuzhiyunAt .init_machine() time, Tegra board support code will need to look at
324*4882a593Smuzhiyunthis DT and decide which nodes to create platform_devices for.
325*4882a593SmuzhiyunHowever, looking at the tree, it is not immediately obvious what kind
326*4882a593Smuzhiyunof device each node represents, or even if a node represents a device
327*4882a593Smuzhiyunat all.  The /chosen, /aliases, and /memory nodes are informational
328*4882a593Smuzhiyunnodes that don't describe devices (although arguably memory could be
329*4882a593Smuzhiyunconsidered a device).  The children of the /soc node are memory mapped
330*4882a593Smuzhiyundevices, but the codec@1a is an i2c device, and the sound node
331*4882a593Smuzhiyunrepresents not a device, but rather how other devices are connected
332*4882a593Smuzhiyuntogether to create the audio subsystem.  I know what each device is
333*4882a593Smuzhiyunbecause I'm familiar with the board design, but how does the kernel
334*4882a593Smuzhiyunknow what to do with each node?
335*4882a593Smuzhiyun
336*4882a593SmuzhiyunThe trick is that the kernel starts at the root of the tree and looks
337*4882a593Smuzhiyunfor nodes that have a 'compatible' property.  First, it is generally
338*4882a593Smuzhiyunassumed that any node with a 'compatible' property represents a device
339*4882a593Smuzhiyunof some kind, and second, it can be assumed that any node at the root
340*4882a593Smuzhiyunof the tree is either directly attached to the processor bus, or is a
341*4882a593Smuzhiyunmiscellaneous system device that cannot be described any other way.
342*4882a593SmuzhiyunFor each of these nodes, Linux allocates and registers a
343*4882a593Smuzhiyunplatform_device, which in turn may get bound to a platform_driver.
344*4882a593Smuzhiyun
345*4882a593SmuzhiyunWhy is using a platform_device for these nodes a safe assumption?
346*4882a593SmuzhiyunWell, for the way that Linux models devices, just about all bus_types
347*4882a593Smuzhiyunassume that its devices are children of a bus controller.  For
348*4882a593Smuzhiyunexample, each i2c_client is a child of an i2c_master.  Each spi_device
349*4882a593Smuzhiyunis a child of an SPI bus.  Similarly for USB, PCI, MDIO, etc.  The
350*4882a593Smuzhiyunsame hierarchy is also found in the DT, where I2C device nodes only
351*4882a593Smuzhiyunever appear as children of an I2C bus node.  Ditto for SPI, MDIO, USB,
352*4882a593Smuzhiyunetc.  The only devices which do not require a specific type of parent
353*4882a593Smuzhiyundevice are platform_devices (and amba_devices, but more on that
354*4882a593Smuzhiyunlater), which will happily live at the base of the Linux /sys/devices
355*4882a593Smuzhiyuntree.  Therefore, if a DT node is at the root of the tree, then it
356*4882a593Smuzhiyunreally probably is best registered as a platform_device.
357*4882a593Smuzhiyun
358*4882a593SmuzhiyunLinux board support code calls of_platform_populate(NULL, NULL, NULL, NULL)
359*4882a593Smuzhiyunto kick off discovery of devices at the root of the tree.  The
360*4882a593Smuzhiyunparameters are all NULL because when starting from the root of the
361*4882a593Smuzhiyuntree, there is no need to provide a starting node (the first NULL), a
362*4882a593Smuzhiyunparent struct device (the last NULL), and we're not using a match
363*4882a593Smuzhiyuntable (yet).  For a board that only needs to register devices,
364*4882a593Smuzhiyun.init_machine() can be completely empty except for the
365*4882a593Smuzhiyunof_platform_populate() call.
366*4882a593Smuzhiyun
367*4882a593SmuzhiyunIn the Tegra example, this accounts for the /soc and /sound nodes, but
368*4882a593Smuzhiyunwhat about the children of the SoC node?  Shouldn't they be registered
369*4882a593Smuzhiyunas platform devices too?  For Linux DT support, the generic behaviour
370*4882a593Smuzhiyunis for child devices to be registered by the parent's device driver at
371*4882a593Smuzhiyundriver .probe() time.  So, an i2c bus device driver will register a
372*4882a593Smuzhiyuni2c_client for each child node, an SPI bus driver will register
373*4882a593Smuzhiyunits spi_device children, and similarly for other bus_types.
374*4882a593SmuzhiyunAccording to that model, a driver could be written that binds to the
375*4882a593SmuzhiyunSoC node and simply registers platform_devices for each of its
376*4882a593Smuzhiyunchildren.  The board support code would allocate and register an SoC
377*4882a593Smuzhiyundevice, a (theoretical) SoC device driver could bind to the SoC device,
378*4882a593Smuzhiyunand register platform_devices for /soc/interrupt-controller, /soc/serial,
379*4882a593Smuzhiyun/soc/i2s, and /soc/i2c in its .probe() hook.  Easy, right?
380*4882a593Smuzhiyun
381*4882a593SmuzhiyunActually, it turns out that registering children of some
382*4882a593Smuzhiyunplatform_devices as more platform_devices is a common pattern, and the
383*4882a593Smuzhiyundevice tree support code reflects that and makes the above example
384*4882a593Smuzhiyunsimpler.  The second argument to of_platform_populate() is an
385*4882a593Smuzhiyunof_device_id table, and any node that matches an entry in that table
386*4882a593Smuzhiyunwill also get its child nodes registered.  In the Tegra case, the code
387*4882a593Smuzhiyuncan look something like this::
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun  static void __init harmony_init_machine(void)
390*4882a593Smuzhiyun  {
391*4882a593Smuzhiyun	/* ... */
392*4882a593Smuzhiyun	of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
393*4882a593Smuzhiyun  }
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun"simple-bus" is defined in the Devicetree Specification as a property
396*4882a593Smuzhiyunmeaning a simple memory mapped bus, so the of_platform_populate() code
397*4882a593Smuzhiyuncould be written to just assume simple-bus compatible nodes will
398*4882a593Smuzhiyunalways be traversed.  However, we pass it in as an argument so that
399*4882a593Smuzhiyunboard support code can always override the default behaviour.
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun[Need to add discussion of adding i2c/spi/etc child devices]
402*4882a593Smuzhiyun
403*4882a593SmuzhiyunAppendix A: AMBA devices
404*4882a593Smuzhiyun------------------------
405*4882a593Smuzhiyun
406*4882a593SmuzhiyunARM Primecells are a certain kind of device attached to the ARM AMBA
407*4882a593Smuzhiyunbus which include some support for hardware detection and power
408*4882a593Smuzhiyunmanagement.  In Linux, struct amba_device and the amba_bus_type is
409*4882a593Smuzhiyunused to represent Primecell devices.  However, the fiddly bit is that
410*4882a593Smuzhiyunnot all devices on an AMBA bus are Primecells, and for Linux it is
411*4882a593Smuzhiyuntypical for both amba_device and platform_device instances to be
412*4882a593Smuzhiyunsiblings of the same bus segment.
413*4882a593Smuzhiyun
414*4882a593SmuzhiyunWhen using the DT, this creates problems for of_platform_populate()
415*4882a593Smuzhiyunbecause it must decide whether to register each node as either a
416*4882a593Smuzhiyunplatform_device or an amba_device.  This unfortunately complicates the
417*4882a593Smuzhiyundevice creation model a little bit, but the solution turns out not to
418*4882a593Smuzhiyunbe too invasive.  If a node is compatible with "arm,amba-primecell", then
419*4882a593Smuzhiyunof_platform_populate() will register it as an amba_device instead of a
420*4882a593Smuzhiyunplatform_device.
421