18c2ecf20Sopenharmony_ci.. SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci
38c2ecf20Sopenharmony_ci=========================
48c2ecf20Sopenharmony_ciLinux and the Device Tree
58c2ecf20Sopenharmony_ci=========================
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ciThe Linux usage model for device tree data
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci:Author: Grant Likely <grant.likely@secretlab.ca>
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ciThis article describes how Linux uses the device tree.  An overview of
128c2ecf20Sopenharmony_cithe device tree data format can be found on the device tree usage page
138c2ecf20Sopenharmony_ciat devicetree.org\ [1]_.
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci.. [1] https://elinux.org/Device_Tree_Usage
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ciThe "Open Firmware Device Tree", or simply Device Tree (DT), is a data
188c2ecf20Sopenharmony_cistructure and language for describing hardware.  More specifically, it
198c2ecf20Sopenharmony_ciis a description of hardware that is readable by an operating system
208c2ecf20Sopenharmony_ciso that the operating system doesn't need to hard code details of the
218c2ecf20Sopenharmony_cimachine.
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciStructurally, the DT is a tree, or acyclic graph with named nodes, and
248c2ecf20Sopenharmony_cinodes may have an arbitrary number of named properties encapsulating
258c2ecf20Sopenharmony_ciarbitrary data.  A mechanism also exists to create arbitrary
268c2ecf20Sopenharmony_cilinks from one node to another outside of the natural tree structure.
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ciConceptually, a common set of usage conventions, called 'bindings',
298c2ecf20Sopenharmony_ciis defined for how data should appear in the tree to describe typical
308c2ecf20Sopenharmony_cihardware characteristics including data busses, interrupt lines, GPIO
318c2ecf20Sopenharmony_ciconnections, and peripheral devices.
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ciAs much as possible, hardware is described using existing bindings to
348c2ecf20Sopenharmony_cimaximize use of existing support code, but since property and node
358c2ecf20Sopenharmony_cinames are simply text strings, it is easy to extend existing bindings
368c2ecf20Sopenharmony_cior create new ones by defining new nodes and properties.  Be wary,
378c2ecf20Sopenharmony_cihowever, of creating a new binding without first doing some homework
388c2ecf20Sopenharmony_ciabout what already exists.  There are currently two different,
398c2ecf20Sopenharmony_ciincompatible, bindings for i2c busses that came about because the new
408c2ecf20Sopenharmony_cibinding was created without first investigating how i2c devices were
418c2ecf20Sopenharmony_cialready being enumerated in existing systems.
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_ci1. History
448c2ecf20Sopenharmony_ci----------
458c2ecf20Sopenharmony_ciThe DT was originally created by Open Firmware as part of the
468c2ecf20Sopenharmony_cicommunication method for passing data from Open Firmware to a client
478c2ecf20Sopenharmony_ciprogram (like to an operating system).  An operating system used the
488c2ecf20Sopenharmony_ciDevice Tree to discover the topology of the hardware at runtime, and
498c2ecf20Sopenharmony_cithereby support a majority of available hardware without hard coded
508c2ecf20Sopenharmony_ciinformation (assuming drivers were available for all devices).
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ciSince Open Firmware is commonly used on PowerPC and SPARC platforms,
538c2ecf20Sopenharmony_cithe Linux support for those architectures has for a long time used the
548c2ecf20Sopenharmony_ciDevice Tree.
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ciIn 2005, when PowerPC Linux began a major cleanup and to merge 32-bit
578c2ecf20Sopenharmony_ciand 64-bit support, the decision was made to require DT support on all
588c2ecf20Sopenharmony_cipowerpc platforms, regardless of whether or not they used Open
598c2ecf20Sopenharmony_ciFirmware.  To do this, a DT representation called the Flattened Device
608c2ecf20Sopenharmony_ciTree (FDT) was created which could be passed to the kernel as a binary
618c2ecf20Sopenharmony_ciblob without requiring a real Open Firmware implementation.  U-Boot,
628c2ecf20Sopenharmony_cikexec, and other bootloaders were modified to support both passing a
638c2ecf20Sopenharmony_ciDevice Tree Binary (dtb) and to modify a dtb at boot time.  DT was
648c2ecf20Sopenharmony_cialso added to the PowerPC boot wrapper (``arch/powerpc/boot/*``) so that
658c2ecf20Sopenharmony_cia dtb could be wrapped up with the kernel image to support booting
668c2ecf20Sopenharmony_ciexisting non-DT aware firmware.
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ciSome time later, FDT infrastructure was generalized to be usable by
698c2ecf20Sopenharmony_ciall architectures.  At the time of this writing, 6 mainlined
708c2ecf20Sopenharmony_ciarchitectures (arm, microblaze, mips, powerpc, sparc, and x86) and 1
718c2ecf20Sopenharmony_ciout of mainline (nios) have some level of DT support.
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci2. Data Model
748c2ecf20Sopenharmony_ci-------------
758c2ecf20Sopenharmony_ciIf you haven't already read the Device Tree Usage\ [1]_ page,
768c2ecf20Sopenharmony_cithen go read it now.  It's okay, I'll wait....
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci2.1 High Level View
798c2ecf20Sopenharmony_ci-------------------
808c2ecf20Sopenharmony_ciThe most important thing to understand is that the DT is simply a data
818c2ecf20Sopenharmony_cistructure that describes the hardware.  There is nothing magical about
828c2ecf20Sopenharmony_ciit, and it doesn't magically make all hardware configuration problems
838c2ecf20Sopenharmony_cigo away.  What it does do is provide a language for decoupling the
848c2ecf20Sopenharmony_cihardware configuration from the board and device driver support in the
858c2ecf20Sopenharmony_ciLinux kernel (or any other operating system for that matter).  Using
868c2ecf20Sopenharmony_ciit allows board and device support to become data driven; to make
878c2ecf20Sopenharmony_cisetup decisions based on data passed into the kernel instead of on
888c2ecf20Sopenharmony_ciper-machine hard coded selections.
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ciIdeally, data driven platform setup should result in less code
918c2ecf20Sopenharmony_ciduplication and make it easier to support a wide range of hardware
928c2ecf20Sopenharmony_ciwith a single kernel image.
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciLinux uses DT data for three major purposes:
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci1) platform identification,
978c2ecf20Sopenharmony_ci2) runtime configuration, and
988c2ecf20Sopenharmony_ci3) device population.
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci2.2 Platform Identification
1018c2ecf20Sopenharmony_ci---------------------------
1028c2ecf20Sopenharmony_ciFirst and foremost, the kernel will use data in the DT to identify the
1038c2ecf20Sopenharmony_cispecific machine.  In a perfect world, the specific platform shouldn't
1048c2ecf20Sopenharmony_cimatter to the kernel because all platform details would be described
1058c2ecf20Sopenharmony_ciperfectly by the device tree in a consistent and reliable manner.
1068c2ecf20Sopenharmony_ciHardware is not perfect though, and so the kernel must identify the
1078c2ecf20Sopenharmony_cimachine during early boot so that it has the opportunity to run
1088c2ecf20Sopenharmony_cimachine-specific fixups.
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ciIn the majority of cases, the machine identity is irrelevant, and the
1118c2ecf20Sopenharmony_cikernel will instead select setup code based on the machine's core
1128c2ecf20Sopenharmony_ciCPU or SoC.  On ARM for example, setup_arch() in
1138c2ecf20Sopenharmony_ciarch/arm/kernel/setup.c will call setup_machine_fdt() in
1148c2ecf20Sopenharmony_ciarch/arm/kernel/devtree.c which searches through the machine_desc
1158c2ecf20Sopenharmony_citable and selects the machine_desc which best matches the device tree
1168c2ecf20Sopenharmony_cidata.  It determines the best match by looking at the 'compatible'
1178c2ecf20Sopenharmony_ciproperty in the root device tree node, and comparing it with the
1188c2ecf20Sopenharmony_cidt_compat list in struct machine_desc (which is defined in
1198c2ecf20Sopenharmony_ciarch/arm/include/asm/mach/arch.h if you're curious).
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ciThe 'compatible' property contains a sorted list of strings starting
1228c2ecf20Sopenharmony_ciwith the exact name of the machine, followed by an optional list of
1238c2ecf20Sopenharmony_ciboards it is compatible with sorted from most compatible to least.  For
1248c2ecf20Sopenharmony_ciexample, the root compatible properties for the TI BeagleBoard and its
1258c2ecf20Sopenharmony_cisuccessor, the BeagleBoard xM board might look like, respectively::
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	compatible = "ti,omap3-beagleboard", "ti,omap3450", "ti,omap3";
1288c2ecf20Sopenharmony_ci	compatible = "ti,omap3-beagleboard-xm", "ti,omap3450", "ti,omap3";
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ciWhere "ti,omap3-beagleboard-xm" specifies the exact model, it also
1318c2ecf20Sopenharmony_ciclaims that it compatible with the OMAP 3450 SoC, and the omap3 family
1328c2ecf20Sopenharmony_ciof SoCs in general.  You'll notice that the list is sorted from most
1338c2ecf20Sopenharmony_cispecific (exact board) to least specific (SoC family).
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ciAstute readers might point out that the Beagle xM could also claim
1368c2ecf20Sopenharmony_cicompatibility with the original Beagle board.  However, one should be
1378c2ecf20Sopenharmony_cicautioned about doing so at the board level since there is typically a
1388c2ecf20Sopenharmony_cihigh level of change from one board to another, even within the same
1398c2ecf20Sopenharmony_ciproduct line, and it is hard to nail down exactly what is meant when one
1408c2ecf20Sopenharmony_ciboard claims to be compatible with another.  For the top level, it is
1418c2ecf20Sopenharmony_cibetter to err on the side of caution and not claim one board is
1428c2ecf20Sopenharmony_cicompatible with another.  The notable exception would be when one
1438c2ecf20Sopenharmony_ciboard is a carrier for another, such as a CPU module attached to a
1448c2ecf20Sopenharmony_cicarrier board.
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ciOne more note on compatible values.  Any string used in a compatible
1478c2ecf20Sopenharmony_ciproperty must be documented as to what it indicates.  Add
1488c2ecf20Sopenharmony_cidocumentation for compatible strings in Documentation/devicetree/bindings.
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ciAgain on ARM, for each machine_desc, the kernel looks to see if
1518c2ecf20Sopenharmony_ciany of the dt_compat list entries appear in the compatible property.
1528c2ecf20Sopenharmony_ciIf one does, then that machine_desc is a candidate for driving the
1538c2ecf20Sopenharmony_cimachine.  After searching the entire table of machine_descs,
1548c2ecf20Sopenharmony_cisetup_machine_fdt() returns the 'most compatible' machine_desc based
1558c2ecf20Sopenharmony_cion which entry in the compatible property each machine_desc matches
1568c2ecf20Sopenharmony_ciagainst.  If no matching machine_desc is found, then it returns NULL.
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ciThe reasoning behind this scheme is the observation that in the majority
1598c2ecf20Sopenharmony_ciof cases, a single machine_desc can support a large number of boards
1608c2ecf20Sopenharmony_ciif they all use the same SoC, or same family of SoCs.  However,
1618c2ecf20Sopenharmony_ciinvariably there will be some exceptions where a specific board will
1628c2ecf20Sopenharmony_cirequire special setup code that is not useful in the generic case.
1638c2ecf20Sopenharmony_ciSpecial cases could be handled by explicitly checking for the
1648c2ecf20Sopenharmony_citroublesome board(s) in generic setup code, but doing so very quickly
1658c2ecf20Sopenharmony_cibecomes ugly and/or unmaintainable if it is more than just a couple of
1668c2ecf20Sopenharmony_cicases.
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ciInstead, the compatible list allows a generic machine_desc to provide
1698c2ecf20Sopenharmony_cisupport for a wide common set of boards by specifying "less
1708c2ecf20Sopenharmony_cicompatible" values in the dt_compat list.  In the example above,
1718c2ecf20Sopenharmony_cigeneric board support can claim compatibility with "ti,omap3" or
1728c2ecf20Sopenharmony_ci"ti,omap3450".  If a bug was discovered on the original beagleboard
1738c2ecf20Sopenharmony_cithat required special workaround code during early boot, then a new
1748c2ecf20Sopenharmony_cimachine_desc could be added which implements the workarounds and only
1758c2ecf20Sopenharmony_cimatches on "ti,omap3-beagleboard".
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ciPowerPC uses a slightly different scheme where it calls the .probe()
1788c2ecf20Sopenharmony_cihook from each machine_desc, and the first one returning TRUE is used.
1798c2ecf20Sopenharmony_ciHowever, this approach does not take into account the priority of the
1808c2ecf20Sopenharmony_cicompatible list, and probably should be avoided for new architecture
1818c2ecf20Sopenharmony_cisupport.
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci2.3 Runtime configuration
1848c2ecf20Sopenharmony_ci-------------------------
1858c2ecf20Sopenharmony_ciIn most cases, a DT will be the sole method of communicating data from
1868c2ecf20Sopenharmony_cifirmware to the kernel, so also gets used to pass in runtime and
1878c2ecf20Sopenharmony_ciconfiguration data like the kernel parameters string and the location
1888c2ecf20Sopenharmony_ciof an initrd image.
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ciMost of this data is contained in the /chosen node, and when booting
1918c2ecf20Sopenharmony_ciLinux it will look something like this::
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	chosen {
1948c2ecf20Sopenharmony_ci		bootargs = "console=ttyS0,115200 loglevel=8";
1958c2ecf20Sopenharmony_ci		initrd-start = <0xc8000000>;
1968c2ecf20Sopenharmony_ci		initrd-end = <0xc8200000>;
1978c2ecf20Sopenharmony_ci	};
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ciThe bootargs property contains the kernel arguments, and the initrd-*
2008c2ecf20Sopenharmony_ciproperties define the address and size of an initrd blob.  Note that
2018c2ecf20Sopenharmony_ciinitrd-end is the first address after the initrd image, so this doesn't
2028c2ecf20Sopenharmony_cimatch the usual semantic of struct resource.  The chosen node may also
2038c2ecf20Sopenharmony_cioptionally contain an arbitrary number of additional properties for
2048c2ecf20Sopenharmony_ciplatform-specific configuration data.
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ciDuring early boot, the architecture setup code calls of_scan_flat_dt()
2078c2ecf20Sopenharmony_ciseveral times with different helper callbacks to parse device tree
2088c2ecf20Sopenharmony_cidata before paging is setup.  The of_scan_flat_dt() code scans through
2098c2ecf20Sopenharmony_cithe device tree and uses the helpers to extract information required
2108c2ecf20Sopenharmony_ciduring early boot.  Typically the early_init_dt_scan_chosen() helper
2118c2ecf20Sopenharmony_ciis used to parse the chosen node including kernel parameters,
2128c2ecf20Sopenharmony_ciearly_init_dt_scan_root() to initialize the DT address space model,
2138c2ecf20Sopenharmony_ciand early_init_dt_scan_memory() to determine the size and
2148c2ecf20Sopenharmony_cilocation of usable RAM.
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciOn ARM, the function setup_machine_fdt() is responsible for early
2178c2ecf20Sopenharmony_ciscanning of the device tree after selecting the correct machine_desc
2188c2ecf20Sopenharmony_cithat supports the board.
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci2.4 Device population
2218c2ecf20Sopenharmony_ci---------------------
2228c2ecf20Sopenharmony_ciAfter the board has been identified, and after the early configuration data
2238c2ecf20Sopenharmony_cihas been parsed, then kernel initialization can proceed in the normal
2248c2ecf20Sopenharmony_ciway.  At some point in this process, unflatten_device_tree() is called
2258c2ecf20Sopenharmony_cito convert the data into a more efficient runtime representation.
2268c2ecf20Sopenharmony_ciThis is also when machine-specific setup hooks will get called, like
2278c2ecf20Sopenharmony_cithe machine_desc .init_early(), .init_irq() and .init_machine() hooks
2288c2ecf20Sopenharmony_cion ARM.  The remainder of this section uses examples from the ARM
2298c2ecf20Sopenharmony_ciimplementation, but all architectures will do pretty much the same
2308c2ecf20Sopenharmony_cithing when using a DT.
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciAs can be guessed by the names, .init_early() is used for any machine-
2338c2ecf20Sopenharmony_cispecific setup that needs to be executed early in the boot process,
2348c2ecf20Sopenharmony_ciand .init_irq() is used to set up interrupt handling.  Using a DT
2358c2ecf20Sopenharmony_cidoesn't materially change the behaviour of either of these functions.
2368c2ecf20Sopenharmony_ciIf a DT is provided, then both .init_early() and .init_irq() are able
2378c2ecf20Sopenharmony_cito call any of the DT query functions (of_* in include/linux/of*.h) to
2388c2ecf20Sopenharmony_ciget additional data about the platform.
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ciThe most interesting hook in the DT context is .init_machine() which
2418c2ecf20Sopenharmony_ciis primarily responsible for populating the Linux device model with
2428c2ecf20Sopenharmony_cidata about the platform.  Historically this has been implemented on
2438c2ecf20Sopenharmony_ciembedded platforms by defining a set of static clock structures,
2448c2ecf20Sopenharmony_ciplatform_devices, and other data in the board support .c file, and
2458c2ecf20Sopenharmony_ciregistering it en-masse in .init_machine().  When DT is used, then
2468c2ecf20Sopenharmony_ciinstead of hard coding static devices for each platform, the list of
2478c2ecf20Sopenharmony_cidevices can be obtained by parsing the DT, and allocating device
2488c2ecf20Sopenharmony_cistructures dynamically.
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ciThe simplest case is when .init_machine() is only responsible for
2518c2ecf20Sopenharmony_ciregistering a block of platform_devices.  A platform_device is a concept
2528c2ecf20Sopenharmony_ciused by Linux for memory or I/O mapped devices which cannot be detected
2538c2ecf20Sopenharmony_ciby hardware, and for 'composite' or 'virtual' devices (more on those
2548c2ecf20Sopenharmony_cilater).  While there is no 'platform device' terminology for the DT,
2558c2ecf20Sopenharmony_ciplatform devices roughly correspond to device nodes at the root of the
2568c2ecf20Sopenharmony_citree and children of simple memory mapped bus nodes.
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ciAbout now is a good time to lay out an example.  Here is part of the
2598c2ecf20Sopenharmony_cidevice tree for the NVIDIA Tegra board::
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci  /{
2628c2ecf20Sopenharmony_ci	compatible = "nvidia,harmony", "nvidia,tegra20";
2638c2ecf20Sopenharmony_ci	#address-cells = <1>;
2648c2ecf20Sopenharmony_ci	#size-cells = <1>;
2658c2ecf20Sopenharmony_ci	interrupt-parent = <&intc>;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	chosen { };
2688c2ecf20Sopenharmony_ci	aliases { };
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	memory {
2718c2ecf20Sopenharmony_ci		device_type = "memory";
2728c2ecf20Sopenharmony_ci		reg = <0x00000000 0x40000000>;
2738c2ecf20Sopenharmony_ci	};
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	soc {
2768c2ecf20Sopenharmony_ci		compatible = "nvidia,tegra20-soc", "simple-bus";
2778c2ecf20Sopenharmony_ci		#address-cells = <1>;
2788c2ecf20Sopenharmony_ci		#size-cells = <1>;
2798c2ecf20Sopenharmony_ci		ranges;
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci		intc: interrupt-controller@50041000 {
2828c2ecf20Sopenharmony_ci			compatible = "nvidia,tegra20-gic";
2838c2ecf20Sopenharmony_ci			interrupt-controller;
2848c2ecf20Sopenharmony_ci			#interrupt-cells = <1>;
2858c2ecf20Sopenharmony_ci			reg = <0x50041000 0x1000>, < 0x50040100 0x0100 >;
2868c2ecf20Sopenharmony_ci		};
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		serial@70006300 {
2898c2ecf20Sopenharmony_ci			compatible = "nvidia,tegra20-uart";
2908c2ecf20Sopenharmony_ci			reg = <0x70006300 0x100>;
2918c2ecf20Sopenharmony_ci			interrupts = <122>;
2928c2ecf20Sopenharmony_ci		};
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci		i2s1: i2s@70002800 {
2958c2ecf20Sopenharmony_ci			compatible = "nvidia,tegra20-i2s";
2968c2ecf20Sopenharmony_ci			reg = <0x70002800 0x100>;
2978c2ecf20Sopenharmony_ci			interrupts = <77>;
2988c2ecf20Sopenharmony_ci			codec = <&wm8903>;
2998c2ecf20Sopenharmony_ci		};
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		i2c@7000c000 {
3028c2ecf20Sopenharmony_ci			compatible = "nvidia,tegra20-i2c";
3038c2ecf20Sopenharmony_ci			#address-cells = <1>;
3048c2ecf20Sopenharmony_ci			#size-cells = <0>;
3058c2ecf20Sopenharmony_ci			reg = <0x7000c000 0x100>;
3068c2ecf20Sopenharmony_ci			interrupts = <70>;
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci			wm8903: codec@1a {
3098c2ecf20Sopenharmony_ci				compatible = "wlf,wm8903";
3108c2ecf20Sopenharmony_ci				reg = <0x1a>;
3118c2ecf20Sopenharmony_ci				interrupts = <347>;
3128c2ecf20Sopenharmony_ci			};
3138c2ecf20Sopenharmony_ci		};
3148c2ecf20Sopenharmony_ci	};
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	sound {
3178c2ecf20Sopenharmony_ci		compatible = "nvidia,harmony-sound";
3188c2ecf20Sopenharmony_ci		i2s-controller = <&i2s1>;
3198c2ecf20Sopenharmony_ci		i2s-codec = <&wm8903>;
3208c2ecf20Sopenharmony_ci	};
3218c2ecf20Sopenharmony_ci  };
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ciAt .init_machine() time, Tegra board support code will need to look at
3248c2ecf20Sopenharmony_cithis DT and decide which nodes to create platform_devices for.
3258c2ecf20Sopenharmony_ciHowever, looking at the tree, it is not immediately obvious what kind
3268c2ecf20Sopenharmony_ciof device each node represents, or even if a node represents a device
3278c2ecf20Sopenharmony_ciat all.  The /chosen, /aliases, and /memory nodes are informational
3288c2ecf20Sopenharmony_cinodes that don't describe devices (although arguably memory could be
3298c2ecf20Sopenharmony_ciconsidered a device).  The children of the /soc node are memory mapped
3308c2ecf20Sopenharmony_cidevices, but the codec@1a is an i2c device, and the sound node
3318c2ecf20Sopenharmony_cirepresents not a device, but rather how other devices are connected
3328c2ecf20Sopenharmony_citogether to create the audio subsystem.  I know what each device is
3338c2ecf20Sopenharmony_cibecause I'm familiar with the board design, but how does the kernel
3348c2ecf20Sopenharmony_ciknow what to do with each node?
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ciThe trick is that the kernel starts at the root of the tree and looks
3378c2ecf20Sopenharmony_cifor nodes that have a 'compatible' property.  First, it is generally
3388c2ecf20Sopenharmony_ciassumed that any node with a 'compatible' property represents a device
3398c2ecf20Sopenharmony_ciof some kind, and second, it can be assumed that any node at the root
3408c2ecf20Sopenharmony_ciof the tree is either directly attached to the processor bus, or is a
3418c2ecf20Sopenharmony_cimiscellaneous system device that cannot be described any other way.
3428c2ecf20Sopenharmony_ciFor each of these nodes, Linux allocates and registers a
3438c2ecf20Sopenharmony_ciplatform_device, which in turn may get bound to a platform_driver.
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ciWhy is using a platform_device for these nodes a safe assumption?
3468c2ecf20Sopenharmony_ciWell, for the way that Linux models devices, just about all bus_types
3478c2ecf20Sopenharmony_ciassume that its devices are children of a bus controller.  For
3488c2ecf20Sopenharmony_ciexample, each i2c_client is a child of an i2c_master.  Each spi_device
3498c2ecf20Sopenharmony_ciis a child of an SPI bus.  Similarly for USB, PCI, MDIO, etc.  The
3508c2ecf20Sopenharmony_cisame hierarchy is also found in the DT, where I2C device nodes only
3518c2ecf20Sopenharmony_ciever appear as children of an I2C bus node.  Ditto for SPI, MDIO, USB,
3528c2ecf20Sopenharmony_cietc.  The only devices which do not require a specific type of parent
3538c2ecf20Sopenharmony_cidevice are platform_devices (and amba_devices, but more on that
3548c2ecf20Sopenharmony_cilater), which will happily live at the base of the Linux /sys/devices
3558c2ecf20Sopenharmony_citree.  Therefore, if a DT node is at the root of the tree, then it
3568c2ecf20Sopenharmony_cireally probably is best registered as a platform_device.
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ciLinux board support code calls of_platform_populate(NULL, NULL, NULL, NULL)
3598c2ecf20Sopenharmony_cito kick off discovery of devices at the root of the tree.  The
3608c2ecf20Sopenharmony_ciparameters are all NULL because when starting from the root of the
3618c2ecf20Sopenharmony_citree, there is no need to provide a starting node (the first NULL), a
3628c2ecf20Sopenharmony_ciparent struct device (the last NULL), and we're not using a match
3638c2ecf20Sopenharmony_citable (yet).  For a board that only needs to register devices,
3648c2ecf20Sopenharmony_ci.init_machine() can be completely empty except for the
3658c2ecf20Sopenharmony_ciof_platform_populate() call.
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ciIn the Tegra example, this accounts for the /soc and /sound nodes, but
3688c2ecf20Sopenharmony_ciwhat about the children of the SoC node?  Shouldn't they be registered
3698c2ecf20Sopenharmony_cias platform devices too?  For Linux DT support, the generic behaviour
3708c2ecf20Sopenharmony_ciis for child devices to be registered by the parent's device driver at
3718c2ecf20Sopenharmony_cidriver .probe() time.  So, an i2c bus device driver will register a
3728c2ecf20Sopenharmony_cii2c_client for each child node, an SPI bus driver will register
3738c2ecf20Sopenharmony_ciits spi_device children, and similarly for other bus_types.
3748c2ecf20Sopenharmony_ciAccording to that model, a driver could be written that binds to the
3758c2ecf20Sopenharmony_ciSoC node and simply registers platform_devices for each of its
3768c2ecf20Sopenharmony_cichildren.  The board support code would allocate and register an SoC
3778c2ecf20Sopenharmony_cidevice, a (theoretical) SoC device driver could bind to the SoC device,
3788c2ecf20Sopenharmony_ciand register platform_devices for /soc/interrupt-controller, /soc/serial,
3798c2ecf20Sopenharmony_ci/soc/i2s, and /soc/i2c in its .probe() hook.  Easy, right?
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ciActually, it turns out that registering children of some
3828c2ecf20Sopenharmony_ciplatform_devices as more platform_devices is a common pattern, and the
3838c2ecf20Sopenharmony_cidevice tree support code reflects that and makes the above example
3848c2ecf20Sopenharmony_cisimpler.  The second argument to of_platform_populate() is an
3858c2ecf20Sopenharmony_ciof_device_id table, and any node that matches an entry in that table
3868c2ecf20Sopenharmony_ciwill also get its child nodes registered.  In the Tegra case, the code
3878c2ecf20Sopenharmony_cican look something like this::
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci  static void __init harmony_init_machine(void)
3908c2ecf20Sopenharmony_ci  {
3918c2ecf20Sopenharmony_ci	/* ... */
3928c2ecf20Sopenharmony_ci	of_platform_populate(NULL, of_default_bus_match_table, NULL, NULL);
3938c2ecf20Sopenharmony_ci  }
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci"simple-bus" is defined in the Devicetree Specification as a property
3968c2ecf20Sopenharmony_cimeaning a simple memory mapped bus, so the of_platform_populate() code
3978c2ecf20Sopenharmony_cicould be written to just assume simple-bus compatible nodes will
3988c2ecf20Sopenharmony_cialways be traversed.  However, we pass it in as an argument so that
3998c2ecf20Sopenharmony_ciboard support code can always override the default behaviour.
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci[Need to add discussion of adding i2c/spi/etc child devices]
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ciAppendix A: AMBA devices
4048c2ecf20Sopenharmony_ci------------------------
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ciARM Primecells are a certain kind of device attached to the ARM AMBA
4078c2ecf20Sopenharmony_cibus which include some support for hardware detection and power
4088c2ecf20Sopenharmony_cimanagement.  In Linux, struct amba_device and the amba_bus_type is
4098c2ecf20Sopenharmony_ciused to represent Primecell devices.  However, the fiddly bit is that
4108c2ecf20Sopenharmony_cinot all devices on an AMBA bus are Primecells, and for Linux it is
4118c2ecf20Sopenharmony_citypical for both amba_device and platform_device instances to be
4128c2ecf20Sopenharmony_cisiblings of the same bus segment.
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ciWhen using the DT, this creates problems for of_platform_populate()
4158c2ecf20Sopenharmony_cibecause it must decide whether to register each node as either a
4168c2ecf20Sopenharmony_ciplatform_device or an amba_device.  This unfortunately complicates the
4178c2ecf20Sopenharmony_cidevice creation model a little bit, but the solution turns out not to
4188c2ecf20Sopenharmony_cibe too invasive.  If a node is compatible with "arm,amba-primecell", then
4198c2ecf20Sopenharmony_ciof_platform_populate() will register it as an amba_device instead of a
4208c2ecf20Sopenharmony_ciplatform_device.
421