DeviceTree For Hackers: Intro

Raspberry Pi has just leaked information on the Pi Zero 3W. They did it by publishing a DeviceTree file that describes the hardware of the Pi Zero. This is how we know it has the BCM2711 (Pi 4 CPU), makes use of PCIe (maybe it’s even exposed!), has an onboard power button, an extra power LED, and an eMMC option. If I were to guess, this will be the first dual-sided Pi Zero board, and if I were to have hope, it finally has USB-C.

Don’t believe it? Go read the DeviceTree file for yourself — it’s exceptionally easy to read it between the lines. DeviceTree files tell the Linux kernel, or other OS kernels, what CPU to expect, what drivers to load, which interfaces are exposed on your board, and the devices connected to those interfaces. Sometimes a proper DeviceTree file is the only thing you need to make a modern Linux OS run on a neglected SBC you own.

There are DeviceTree files powering technology all around you. Your phone has one, so does your router, your ARM laptop, and not even to speak of your SBCs. Some DeviceTree files are evil, being merely a thin layer between the Linux kernel and proprietary drivers; you will find these in phones with CPUs made by evil corporations. Most DeviceTree files are simply there to do their job.

What To Know

DeviceTree files describe hardware like HTML describes elements of a website. They are not Turing complete, and sadly, you cannot run Doom on DeviceTree alone – unless you find one of those evil implementations, I suppose. A DeviceTree source file (extensions .dts, .dtsi) gets compiled into a binary file (.dtb, .dtbo) able to read by your kernel. It’s relatively easy to decompile it and change things even if you don’t have the source!

Got a DS1307 IC on address 0x68, bus I2C1? A DeviceTree overlay like this will tell your kernel about it.

You can handle DeviceTree files pretty easily. Compile/decompile commands are dead simple and easy to find; the compiler is also a decompiler, a tiny utility that’s one sudo apt install device-tree-compiler away. For the Blepis PDA I’ve worked on, we have a script to compile a bunch of overlays for different displays that the Blepis can have, and it’s one of the most straightforward things ever.

There are a few types of DT files, like includes, base files, overlays. Includes (.dtsi) tend to hold information about the CPU, mostly the boring stuff you’d never touch, like some clock definitions. Base files describe the board fully, the kernel loads them and knows what to do with the board. Overlays are typically optional, they’re how you “add” features on top of the base DeviceTree, either describing extra devices or modifying the devices/interfaces already described. You’ve seen overlays mentioned in Raspberry Pi config.txt, without a doubt!

In fact, if you wanted to learn DeviceTree files by reading, you should check out the Raspberry Pi overlay source directory – could start from the README. There’s a ton in there and most of it is boring, but you will find gold if you look. By the way, you can also check out the base files in the broadcom/ DeviceTree base file directory to learn a lot about Raspberry Pi hardware as-is!

Learn By Numerous Examples

You will mostly want to write overlays, not base files. There isn’t much you need to learn to write overlays, because there are just so many places to learn from. In this intro, I will mostly describe the bare minimum you need to know to write an overlay that works. And 99% of time that will involve taking an existing overlay and slightly modifying it.

Here is a simple and exceptionally verbose LED overlay from Raspberry Pi, more comments than code, as if meant to be a training overlay! You describe an LED connected to a specific GPIO, its default state, its label, and even overrides so you can replace those parameters on the fly. Then, you can load the overlay with a single command line on a running system (this part is exclusive to Raspberry Pi), poke at the LED, turn it into heartbeat LED or a CPU activity signal, and load it permanently as a config.txt entry if you so wish. The file is nicely commented and explains basically everything.

Here’s an overlay that describes an I2C touchscreen connected to your Pi. Tweak the pin numbers to match, compile it, load it, and it should be automagically picked up by the kernel. Here is an overlay that disables WiFi on your Pi. Doesn’t this look super easy, just setting two variables to “disabled”? Here’s an overlay that describes an SPI Ethernet controller. It defines the pins used, and, it also takes care to set the specific SPI interface in use to “disabled”, so you can’t accidentally write into it from userspace.

At times Raspberry Pi overlays are too involved for their own good and hard to learn from, you can see just how many there are. Worry not, there are many other places to get examples from. The core Linux repository at large has no shortage, then there’s the Radxa overlay repository and Armbian’s everything-Rockchip repository, Armbian’s sunxi overlay (Allwinner) repository and its examples directory in particular… If you need some specific feature or device, someone already likely implemented it in an overlay and put it online. When just researching or even at your wits’ end, you can always search GitHub for .dts files containing your keywords of choice, too.

The Answers You Need

Languages like the DeviceTree one are uncommon, and DeviceTree overlays are obscure-ish, so here are things that you will rarely see explained to you but you absolutely should know, from my yet basic but somewhat experienced understanding if it.

First off, DT overlay comments are C-style, with // and /* */. Things like #size-cells and #address-cells, those are not comments, they are overlay metadata on how to read the <reg> property – keep them in, they’re important!

The & character in overlays is a pointer to things in the base file, so, the .dts (compiled into .dtb) of your board. Different CPU families and even different boards/CPUs have different naming conventions – for instance, GPIO pointers in Raspberry Pi .dts-es are named &gpio, on other boards they’re &pio, and sometimes pointers will refer to different controllers (&gpio1, &gpio2, etc.) if your CPU has too many GPIOs to handle them otherwise. When in doubt, open the base .dts to your board and make sure your pointer names match. When in further doubt, compare to other overlays for your board in places where you need clarification on which specific names to use.

Some overlays are more verbose, while other overlays are less verbose, and you can learn a lot comparing different ones. For instance, compare the Raspberry Pi ENC28J60 overlay to the Allwinner CPU ENC28J60 overlay. You can see that the Allwinner one doesn’t do SPI device disabling, and it doesn’t provide overrides – because you put overlay names into a single line in a uBoot config file, and Allwinner uBoot has nowhere to specify parameters, I presume? It also does the GPIO initialization block a tiny bit differently. To be safe, try and match the conventions of your target platform, generally not excluding/including sections because some other platform has them or omits them. Yes, I am essentially telling you to copy-paste more.

If you are overlaying hard enough and looking around, you will see that some overlays are written as if in short-form, omitting spells like __overlay__, fragment0 and so on. I genuinely don’t know the difference between these two styles, they certainly compile either way and appear to be equivalent ways of writing overlays, as this StackOverflow question also indicates. I tried to explore this further, but I can’t find any references to how these styles are even named, so for now I’m merely putting this on your radar to help you avoid surprises.

To the best of my knowledge, you can’t use #includes when doing overlays if you’re a mere mortal running a bare dtc command. You need a preprocessor for that, and your humble dtc commandline does not run one. Typically, you’ll encounter this by finding an overlay with #include directives online, trying to compile it locally, and getting errors. This means you have to un-define all #define-d parameters, replacing names with numbers wherever dtc complains.

For instance, a nice readable line like rockchip,pins = <4 RK_PA7 RK_FUNC_GPIO &pcfg_pull_up>; (an example from yet another ENC28J60 overlay) won’t compile on its own, you’d need to call a preprocessor, which is a whole undertaking. Instead, find numeric values of RK_PA7 and RK_FUNC_GPIO (they’re from the dt-bindings/pinctrl/rockchip.h include), and replace them, so your line looks like rockchip,pins = <4 7 0 &pcfg_pull_up>;. Ugly, but dtc won’t compile it for you until you do this. For your information, another common spot for include-d GPIO-related values in overlays is the dt-bindings/gpio/gpio.h include.

To the best of my knowledge, .dtbo files are compiled the same exact way as .dtb, and .dtbo is simply the file extension that the Raspberry Pi bootloader expects overlays to be named as. Other platforms don’t use .dtbo and instead use .dtb, again, from what I’ve seen. Also, if you’re using Armbian, use the armbian-add-overlay tool for all your overlay addition needs.

Raspberry Pi allows you to load overlays dynamically with the dtoverlay command, but as far as I understand, this is not implemented on other SBCs. So, when testing overlays on non-Pi targets, you’ll be stuck into a modify-reboot-test cycle, and if you mis-write an overlay real bad, it might just hang your system during boot so you’ll need to unplug the SD card and comment the overlay out to boot again. I suspect that when your system is on an eMMC drive, you won’t be able to do that either, so make sure you can recover in case of boot failure. This applies to any eMMC system, frankly, but you deserve a reminder because you’re messing with the boot process when hacking on overlays.

Ready To Dive In

Want to learn more? Here’s a ZephyrOS primer on DeviceTree overlays, a joy to read. Also, the DeviceTree spec is surprisingly readable as far as descriptive languages go, especially if you learn between the lines. It’s not a tutorial for sure, but if you need to get to the bottom of what specific lines in a devicetree file mean, there might not be a better fallback.

As last advice, be careful using LLMs for generating DeviceTree, especially if you don’t have sufficient LLM experience to know which one will work better for it. Sadly, accidentally having stumbled on some AI-generated DeviceTree “tutorials”, I found that they will outright hallucinate overlay properties at times; looks like for now, there’s no substitute for careful reading.

I’m currently hacking away at a DT overlay template, aiming to make a generator adapting the same overlay to tons of different SBCs at once – certainly, I will have something to tell in the next installment, even if I’m not exactly sure what it would be about. What about you? What aspects of DeviceTree, overlays or not, would you like to see covered next?

21 thoughts on “DeviceTree For Hackers: Intro”

  1. How well has the Raspi Zero 2W (or normal Raspi) CPU been mapped? Do we know what each of its BGA pins are for? I have a few projects where I would like to use it. There are not a lot of options if you want even halfway decent performance or IO from a custom SBC. At an affordable price, I should add.

    There are of course Ti or ST or NXP dinosaurs from Linux 2.6 era with USB1.0 and 400MHz single core CPU and DDR2. But if you want the good stuff its just Chinese and Taiwanese fabs. AllWinner, RockChip, etc. You get 32GB (LP)DDR4/DDR5, UFS3.0, a dozen USB, SPI, I2C, PCIe, etc ports. But they’re expensive and English documentation is lacking. Using a SoM just feels wrong.

    Also Zephyr RTOS, keep that away from me. I like living my life and getting my devices to actually work, not hunting #define __MACRO__MACRO__MACRO(X) __MACRO__MACRO(X) errors down 56 layers. Devicetrees for Zephyr aren’t even worth it, even if you manage to fight it, you only get to use some ‘driver’ that’s just a thin Zephyr layer under the vendor driver layer. And 9/10 times, the driver supports one or two features out of the dozen that the chip actually supports, only the happy path that lets them claim “we support Zephyr like all the modern companies”

    I just let Claude fight the Zephyr madness these days. Just let me stay out of it. I have no desire to know what is the difference between a node or an alias. I can do it with register writes,

    1. How well has the Raspi Zero 2W (or normal Raspi) CPU been mapped? Do we know what each of its BGA pins are for?

      Here you go! You don’t need any of this to work with it, just fyi. Even if you’re doing bare metal stuff for some reason and you need to know about “hidden” (not publicly described) GPIOs used for onboard devices, you can learn about all the relevant ones from device tree overlays alone, from what I understand.

      There are not a lot of options if you want even halfway decent performance or IO from a custom SBC.

      I have just written an introductory article about using non-Pi SBCs! You should read that. I won’t summarize it here, but here’s a tip – go to Aliexpress/your hardware websites of choice and put in the following keywords: “Radxa”, “NanoPi”, “Orange Pi”, “Banana Pi”, hell, even “Walnut Pi”, those particular boards look fun and inexpensive if a bit barebones, but a H618 is still mostly acceptable today. Even the slowest of these boards are incredibly far from the 400MHz single-core dinosaurs of 2000s, they provide respectable performance even at a low price, a lot of them are well-supported by now, and they’re going to be better for many tasks than a Zero 2W, given they’re capable of carrying over 512MB of RAM as opposed to the 2W. Don’t expect 32GB, but what are you doing with 32GB on an SoM anyway, and if you really do need this much, you probably don’t need my advice.

      There are Western alternatives too, if you must, NXP hasn’t stopped hacking away at their i.MX lineup last I checked, for one. Alternatively, wait for the 3W, if we’re in luck, they will finally switch to using a separate RAM chip on the bottom layer of the board. Yes, they’re going to be expensive given 2026 realities, but so is Raspberry Pi nowadays, and you’ll find there’s a ton of viable alternatives if you look around.

      1. We are currently experimenting quite a lot while developing an innovative project and we have moved from chip to chip from various manufacturers during its development (and even back to the same chips again). In that project we’ve found it actually helped, but if you are just developing for one chip or a single chip family, I fully agree. They add a lot of trouble and don’t bring more value than some good include files.

  2. not sure i’m following the ‘evil’ distinction here. it seems like the raspi devicetrees would qualify, since so much of what they describe is just interfaces to closed garbage?

    1. Either way, referring to a small piece of helpful, free, open source code as “evil” is just unprofessional. A device tree may be “incomplete” or “disappointing”, but it is not a moral issue.

      The author’s response to your comment above was unprofessional as well.

      This is the most unnecessarily hostile situation I’ve ever seen from a Hackaday author.

      1. yeah i agree that ‘evil’ doesn’t seem like a worthwhile distinction to me…like, i guess you could say i’m on a crusade to get people to say that raspi is closed. and i don’t think that means it isn’t useful, or isn’t a good value for many. it certainly doesn’t mean it’s ‘evil’. it’s just, if you wanted to hack on it, you should expect some challenges. if you had the idea that it was a ‘darling of the open source world’, you might be surprised at those challenges. good vs evil just muddies the water.

      2. I didn’t expand what qualifies as an “evil” device tree, though, as you might guess, it’s not when a system also has proprietary components! You’re also taking “evil” way too literally.

        “Good” examples of an “evil” devicetree are vendor-provided devicetrees, which tend to do at least one if not all of the following: 1) invoke closed-source drivers, 2) depend on closed-source userspace components, in many cases implementing functionality that belongs in the kernel, 3) often don’t ship as sources (requiring decompilation and restructuring), and 4) offer little in the way of clarity, often hiding devicetree-relevant information like GPIOs and memory addresses within closed drivers/userspace instead of keeping it in DT. As you might guess, they’re popular with mass-market devices like smartphones, and they follow from the problem of closed-source drivers – which can also be well described as evil, fyi. From what I remember, Qualcomm is one of the better known offenders in this regard, but a fair few BSPs commit these sins, and devicetrees are now part and parcel of a BSP.

        1. i have specifically applauded you in the past for calling raspi closed. i’m just asking as a favor, please don’t take everything i say as an attack, because it isn’t meant as one!

          anyways, i appreciate your attempt to try to refine the term. i guess i did get the gist of it the first time. specifically, “invoke closed-source drivers”…i did click through to the raspi devicetree that you linked to, and honestly i’m not an expert on devicetree by any stretch but i checked and the devicetree does include vchiq. and it also includes hdmi settings of just “enable” and “disable.”

          from my perspective, vchiq is a closed driver. i thnk it’s what you refer to as “mailbox”. it is 3 components: an open source userland library (which is just message passing), an open source kernel device driver (which is just message passing), and a closed source firmware (which does the work). we do get access to the vendor include files — #defines and a bunch of cryptic comments (which is pretty common in closed garbage in my experience), and we’re left to reverse engineer anything we’re interested in. and a specific insult that raspberry pi adds that i’ve never met with any other closed vendor is that raspberry is always updating the closed firmware, so whatever reverse engineering you’ve done is going to be useless. i’ve worked with vchiq and i’m still mad about it!

          and i’ve also had many many questions about the hdmi interface, and “enable” / “disable” in the devicetree fits my experiece with raspi. how does the hdmi output work? no one knows. the only interface i’ve found for meaningfully configuring it is cryptic ever-changing keywords in /boot/config.txt. i consider /boot/config.txt to be counter-documented because there’s an enormous amount of documentation generated by raspberry and by users that is all of it referring to an obsolete firmware version.

          so that’s why i wanted to know if you meant the raspi devicetree as an example of an evil devicetree. like, i do appreciate that it provides gpio numbers. but it also waves in the direction of all of my personal pain points with raspi.

        2. Yes…a workable DTS, (or alternatively getting to the onboard DTB) is about the only thing that stops us from running Linux natively on any Android device.

          The obstructions put in our way to get to the onboard DTB is what drives me nuts the most…

  3. One remark that I hope will help other people when learning more about device trees (DT):

    You need to be aware that DT nodes can refer directly to other nodes by design (regardless of them being a parent or child), even if you expect that in a tree the only relation is between parent-child nodes. Hope this makes sense.

    Regards.

  4. This is how we know it has the BCM2711 (Pi 4 CPU), makes use of PCIe (maybe it’s even exposed!), has an onboard power button, an extra power LED, and an eMMC option.

    And still probably only has 512 MB of RAM.

      1. Cost, for one thing. Even before the RAMpocalypse they weren’t exactly super affordable.

        The other thing is wanting the “Zero” form factor for the small size and lower power requirements for the project I had in mind. Ended up getting a couple of the Pi clones in the Zero form factor. They’re AllWinner boards with 4 GB of RAM and 32 GB eMMC.

        One’s got a 512 GB SD card and the other has a 1 TB card for data storage, and they’re awesome little keychain travel servers.

  5. A great article, perfect for beginners to get started with Linux. I am a Chinese student, and here at Rockchip, products are more affordable while offering documentation comparable to Raspberry Pi; everyone should give it a try.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.