How to customize a development board
After learning about the bus, device, and driver models, we learned that devices and drivers are bound and matched through the bus; then, through an in-depth study of the device tree, we learned that the emergence of the device tree greatly increased the versatility of the driver; then we looked at the Linux boot process and the layer-by-layer expansion of the device in the kernel.
Through the previous courses, I believe that kernel beginners have a general perceptual understanding of the kernel and drivers, and those with kernel experience have also systematically sorted out. It is said that "practice is the only criterion for testing truth". In order to test the previous theory and deepen understanding, here we start from the device tree and simulate a circuit board with interrupt controllers, GPIO controllers, I2C controllers, SPI controllers, Ethernet controllers, etc., and build a DTS file from beginning to end based on this circuit board so that the kernel supports our own customized development board.
Adding a new SOC
Let's assume that csdn is a chip manufacturer that has developed a chip called gitchat, and the corresponding development board is called evb. What needs to be done to customize a development board and make Linux support our circuit board?
First, we create a mach-gitchat directory in the arch/arm directory, and then create arch/arm/machgitchat/Kconfig, Makefile, and common.c.
Among them, a fake chip called ARCH_GITCHAT is made in Kconfig, common.c is the corresponding driver, and this driver is compiled in Makefile. Let's take a brief look at this driver:
As mentioned in the previous lessons, we
define a circuit board through , which contains many callback functions. Here we simply write
and
, where dt_compat will match the fields in the device tree. If the fields are consistent, it means the match is correct. The fields here are "csdn, gitchat".
DT_MACHINE_START
init_late
dt_compat
Add the corresponding DTS
Now that we have added the soc we defined in Linux, we need to add the device tree corresponding to the soc, that is, the specific board-level file information.
Create csdn-gitchat.dtsi and csdn-gitchat-evb.dts under arch/arm/boot/dts. csdn-gitchat.dtsi is a description of the chip, and csdn-gitchat-evb.dts is an evb board description for this chip. Their contents are as follows:
/*
* DTS file for demo platform for Dec20,2017 CSDN/Gitchat course
*
* Copyright (c) 2017 xxx <xxx@gmail.com>
*
* Licensed under GPLv2.
*/
/ {
compatible = "csdn,gitchat";
#address-cells = <1>;
#size-cells = <1>;
interrupt-parent = <&intc>;
aliases {
serial0 = &uart0;
};
cpus {
#address-cells = <1>;
#size-cells = <0>;
cpu@0 {
compatible = "arm,cortex-a9";
device_type = "cpu";
reg = <0x0>;
};
cpu@1 {
compatible = "arm,cortex-a9";
device_type = "cpu";
reg = <0x1>;
};
};
axi@40000000 {
compatible = "simple-bus";
#address-cells = <1>;
#size-cells = <1>;
ranges = <0x40000000 0x40000000 0x80000000>;
l2-cache-controller@80040000 {
compatible = "arm,pl310-cache";
reg = <0x80040000 0x1000>;
interrupts = <59>;
arm,tag-latency = <1 1 1>;
arm,data-latency = <1 1 1>;
arm,filter-ranges = <0 0x40000000>;
};
intc: interrupt-controller@80020000 {
#interrupt-cells = <1>;
interrupt-controller;
compatible = "csdn,gitchat";
reg = <0x80020000 0x1000>;
};
peri-iobg@b0000000 {
compatible = "simple-bus";
#address-cells = <1>;
#size-cells = <1>;
ranges = <0x0 0xb0000000 0x180000>;
ethernet@10000 {
compatible = "davicom,dm9000";
reg = <0x10000 0x2 0x10004 0x2>;
interrupts = <7>;
davicom,no-eeprom;
};
timer@20000 {
compatible = "csdn,gitchat-tick";
reg = <0x20000 0x1000>;
interrupts = <0>;
clocks = <&clks 11>;
};
clks: clock-controller@30000 {
compatible = "csdn,gitchat-clkc";
reg = <0x30000 0x1000>;
#clock-cells = <1>;
};
gpio0: goio@40000 {
#gpio-cells = <2>;
#interrupt-cells = <2>;
compatible = "csdn,gitchat-gpio";
reg = <0x40000 0x200>;
interrupts = <43>;
gpio-controller;
interrupt-controller;
};
uart0: uart@50000 {
cell-index = <0>;
compatible = "csdn,gitchat-uart";
reg = <0x50000 0x1000>;
interrupts = <17>;
status = "disabled";
};
spi0: spi@d0000 {
cell-index = <0>;
compatible = "csdn,gitchat-spi";
reg = <0xd0000 0x10000>;
interrupts = <15>;
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
};
i2c0: i2c@e0000 {
cell-index = <0>;
compatible = "csdn,gitchat-i2c";
reg = <0xe0000 0x10000>;
interrupts = <24>;
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
};
};
};
};
/dts-v1/;
/include/ "csdn-gitchat.dtsi"
/ {
model = "Csdn Gitchat EVB Board";
compatible = "csdn,gitchat-evb", "csdn,gitchat";
memory {
device_type = "memory";
reg = <0x00000000 0x20000000>;
};
chosen {
bootargs = "console=ttyS0,115200 root=/dev/mtdblock5 rw rootfstype=ubifs";
};
axi@40000000 {
peri-iobg@b0000000 {
spi@d0000 {
status = "disabled";
};
i2c@e0000 {
pixcir_ts@5c {
compatible = "pixcir,pixcir_tangoc";
reg = <0x5c>;
interrupt-parent = <&gpio0>;
interrupts = <17 0>;
attb-gpio = <&gpio0 22 0>;
touchscreen-size-x = <1024>;
touchscreen-size-y = <600>;
};
};
};
};
};
&uart0 {
status = "okay";
};
Due to space limitations, we only write part of the code here.
DTS Compilation
Add our board in arch/arm/boot/dts/Makefile:
We have just defined MACH_GITCHAT in the platform directory, and here we will compile the device tree into a dtb file. We can also manually compile and generate the csdn-gitchat-evb.dtb file through the command:
$ dtc -I dts -O dtb csdn-gitchat-evb.dts -o csdn-gitchat-evb.dtb
So far, a customized development board has been completed. Here is a virtual development board. If you customize a real circuit board, you need to complete the information in the device tree according to the chip manual. But through this course, you can fully understand how to customize a development board from scratch.
This is all we know about the kernel. In the next course, we will learn about the common problems encountered in project development - debugging techniques.
【Recommended reading】
Exploration of Linux bus, device and driver models
In-depth understanding of Linux device tree (DTS)
Linux devices and drivers meet
Add Geek Assistant WeChat and join the technical exchange group
Long press, scan the code, and follow the official account
Featured Posts