- 7.1 Role of a Bootloader
- 7.2 Bootloader Challenges
- 7.3 A Universal Bootloader: Das U-Boot
- 7.4 Porting U-Boot
- 7.5 Device Tree Blob (Flat Device Tree)
- 7.6 Other Bootloaders
- 7.7 Summary
7.5 Device Tree Blob (Flat Device Tree)
One of the more challenging aspects of porting Linux (and U-Boot) to your new board is the recent requirement for a device tree blob (DTB). It is also referred to as a flat device tree, device tree binary, or simply device tree. Throughout this discussion, these terms are used interchangeably. The DTB is a database that represents the hardware components on a given board. It is derived from the IBM OpenFirmware specifications and has been chosen as the default mechanism to pass low-level hardware information from the bootloader to the kernel.
Prior to the requirement for a DTB, U-Boot would pass a board information structure to the kernel, which was derived from a header file in U-Boot that had to exactly match the contents of a similar header file in the kernel. It was very difficult to keep them in sync, and it didn't scale well. This was, in part, the motivation for incorporating the flat device tree as a method to communicate low-level hardware details from the bootloader to the kernel.
Similar to U-Boot or other low-level firmware, mastering the DTB requires complete knowledge of the underlying hardware. You can do an Internet search to find some introductory documents that describe the device tree. A great starting point is the Denx Software Engineering wiki page. References are provided at the end of this chapter.
To begin, let's see how the DTB is used during a typical boot sequence. Listing 7-13 shows a boot sequence on a Power Architecture target using U-Boot. The Freescale MPC8548CDS system was used for this example.
Listing 7-13. Booting Linux with the Device Tree Blob from U-Boot
=> tftp $loadaddr 8548/uImage Speed: 1000, full duplex Using eTSEC0 device TFTP from server 192.168.11.103; our IP address is 192.168.11.18 Filename '8548/uImage'. Load address: 0x600000 Loading: ##################################################### ##################################################### done Bytes transferred = 1838553 (1c0dd9 hex) => tftp $fdtaddr 8548/dtb Speed: 1000, full duplex Using eTSEC0 device TFTP from server 192.168.11.103; our IP address is 192.168.11.18 Filename '8548/dtb'. Load address: 0xc00000 Loading: ## done Bytes transferred = 16384 (4000 hex) => bootm $loadaddr - $fdtaddr ## Booting kernel from Legacy Image at 00600000 ... Image Name: MontaVista Linux 6/2.6.27/freesc Image Type: PowerPC Linux Kernel Image (gzip compressed) Data Size: 1838489 Bytes = 1.8 MB Load Address: 00000000 Entry Point: 00000000 Verifying Checksum ... OK ## Flattened Device Tree blob at 00c00000 Booting using the fdt blob at 0xc00000 Uncompressing Kernel Image ... OK Loading Device Tree to 007f9000, end 007fffff ... OK <... Linux begins booting here...> ...and away we go!!
The primary difference here is that we loaded two images. The large image (1.8MB) is the kernel image. The smaller image (16KB) is the flat device tree. Notice that we placed the kernel and DTB at addresses 0x600000 and 0xc00000, respectively. All the messages from Listing 7-13 are produced by U-Boot. When we use the bootm command to boot the kernel, we add a third parameter, which tells U-Boot where we loaded the DTB.
By now, you are probably wondering where the DTB came from. The easy answer is that it was provided as a courtesy by the board/architecture developers as part of the Linux kernel source tree. If you look at the powerpc branch of any recent Linux kernel tree, you will see a directory called .../arch/powerpc/boot/dts. This is where the "source code" for the DTB resides.
The hard answer is that you must provide a DTB for your custom board. Start with something close to your platform, and modify from there. At the risk of sounding redundant, there is no easy path. You must dive in and learn the details of your hardware platform and become proficient at writing device nodes and their respective properties. Hopefully, this section will start you on your way toward that proficiency.
7.5.1 Device Tree Source
The device tree blob is "compiled" by a special compiler that produces the binary in the proper form for U-Boot and Linux to understand. The dtc compiler usually is provided with your embedded Linux distribution, or it can be found at http://jdl.com/software. Listing 7-14 shows a snippet of the device tree source (DTS) from a recent kernel source tree.
Listing 7-14. Partial Device Tree Source Listing
/* * MPC8548 CDS Device Tree Source * * Copyright 2006, 2008 Freescale Semiconductor Inc. * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. */ /dts-v1/; / { model = "MPC8548CDS"; compatible = "MPC8548CDS", "MPC85xxCDS"; #address-cells = <1>; #size-cells = <1>; aliases { ethernet0 = &enet0; ethernet1 = &enet1; ethernet2 = &enet2; ethernet3 = &enet3; serial0 = &serial0; serial1 = &serial1; pci0 = &pci0; pci1 = &pci1; pci2 = &pci2; rapidio0 = &rio0; }; cpus { #address-cells = <1>; #size-cells = <0>; PowerPC,8548@0 { device_type = "cpu"; reg = <0x0>; d-cache-line-size = <32>; // 32 bytes i-cache-line-size = <32>; // 32 bytes d-cache-size = <0x8000>; // L1, 32K i-cache-size = <0x8000>; // L1, 32K timebase-frequency = <0>; // 33 MHz, from uboot bus-frequency = <0>; // 166 MHz clock-frequency = <0>; // 825 MHz, from uboot next-level-cache = <&L2>; }; }; memory { device_type = "memory"; reg = <0x0 0x8000000>; // 128M at 0x0 }; localbus@e0000000 { #address-cells = <2>; #size-cells = <1>; compatible = "simple-bus"; reg = <0xe0000000 0x5000>; interrupt-parent = <&mpic>; ranges = <0x0 0x0 0xff000000 0x01000000>; /*16MB Flash*/ flash@0,0 { #address-cells = <1>; #size-cells = <1>; compatible = "cfi-flash"; reg = <0x0 0x0 0x1000000>; bank-width = <2>; device-width = <2>; partition@0x0 { label = "free space"; reg = <0x00000000 0x00f80000>; }; partition@0x100000 { label = "bootloader"; reg = <0x00f80000 0x00080000>; read-only; }; }; }; <...truncated here...>
This is a long listing, but it is well worth the time spent studying it. Although it may seem obvious, it is worth noting that this device tree source is specific to the Freescale MPC8548CDS Configurable Development System. Part of your job as a custom embedded Linux developer is to adopt this DTS to your own MPC8548-based system.
Some of the data shown in Listing 7-14 is self-explanatory. The flat device tree is made up of device nodes. A device node is an entry in the device tree, usually describing a single device or bus. Each node contains a set of properties that describe it. It is, in fact, a tree structure. It can easily be represented by a familiar tree view, as shown in Listing 7-15.
Listing 7-15. Tree View of DTS
|-/ Model: model = "MPC8548CDS", etc. | |---- cpus: #address-cells = <1>, etc. | | | |---- PowerPC,8548@0, etc. | |--- Memory: device_type = "memory", etc. | |---- localbus@e0000000: #address-cells = <2>, etc. | | | |---- flash@0,0: #address-cells = <1>, etc. | <...>
In the first few lines of Listing 7-14, we see the processor model and a property indicating compatibility with other processors in the same family. The first child node describes the CPU. Many of the CPU device node properties are self-explanatory. For example, we can see that the 8548 CPU has data and instruction cache line sizes of 32 bytes and that these caches are both 32KB in size (0x8000 bytes.) We see a couple properties that show clock frequencies, such as timebase-frequency and clock-frequency, both of which indicate that they are set by U-Boot. That would be natural, because U-Boot configures the hardware clocks.
The properties called address-cells and size-cells are worth explaining. A "cell" in this context is simply a 32-bit quantity. address-cells and size-cells simply indicate the number of cells (32-bit fields) required to specify an address (or size) in the child node.
The memory device node offers no mysteries. From this node, it is obvious that this platform contains a single bank of memory starting at address 0, which is 128MB in size.
For complete details of flat device tree syntax, consult the references at the end of this chapter. One of the most useful is the document produced by Power.org, found at www.power.org/resources/downloads/Power_ePAPR_APPROVED_v1.0.pdf.
7.5.2 Device Tree Compiler
Introduced earlier, the device tree compiler (dtc) converts the human-readable device tree source into the machine-readable binary that both U-Boot and the Linux kernel understand. Although a git tree is hosted on kernel.org for dtc, the device tree source has been merged into the kernel source tree and is built along with any Power Architecture kernel from the .../arch/powerpc branch.
It is quite straightforward to use the device tree compiler. A typical command to convert source to binary looks like this:
$ dtc -O dtb -o myboard.dtb -b 0 myboard.dts
In this command, myboard.dts is the device tree human-readable source, and myboard.dtb is the binary created by this command invocation. The -O flag specifies the output format—in this case, the device tree blob binary. The -o flag names the output file, and the -b 0 parameter specifies the physical boot CPU in the multicore case.
Note that the dtc compiler allows you to go in both directions. The command example just shown performs a compile from source to device tree binary, whereas a command like this produces source from the binary:
$ dtc -I dtb -O dts mpc8548.dtb >mpc8548.dts
You can also build the DTB for many well-known reference boards directly from the kernel source. The command looks similar to the following:
$ make ARCH=powerpc mpc8548cds.dtb
This produces a binary device tree blob from a source file with the same base name (mpc8548cds) and the dts extension. These are found in .../arch/powerpc/boot/dts. A recent kernel source tree had 120 such device tree source files for a range of Power Architecture boards.
7.5.3 Alternative Kernel Images Using DTB
Entering make ARCH=powerpc help at the top-level Linux kernel source tree outputs many lines of useful help, describing the many build targets available. Several architecture-specific targets combine the device tree blob with the kernel image. One good reason to do this is if you are trying to boot a newer kernel on a target that has an older version of U-Boot that does not support the device tree blob. On a recent Linux kernel, Listing 7-16 reproduces the powerpc targets defined for the powerpc architecture.
Listing 7-16. Architecture-Specific Targets for Powerpc
* zImage - Build default images selected by kernel config zImage.* - Compressed kernel image (arch/powerpc/boot/zImage.*) uImage - U-Boot native image format cuImage.<dt> - Backwards compatible U-Boot image for older versions which do not support device trees dtbImage.<dt> - zImage with an embedded device tree blob simpleImage.<dt> - Firmware independent image. treeImage.<dt> - Support for older IBM 4xx firmware (not U-Boot) install - Install kernel using (your) ~/bin/installkernel or (distribution) /sbin/installkernel or install to $(INSTALL_PATH) and run lilo *_defconfig - Select default config from arch/powerpc/configs
The zImage is the default, but many targets use uImage. Notice that some of these targets have the device tree binary included in the composite kernel image. You need to decide which is most appropriate for your particular platform and application.