Build Linux Kernel
To build the linux kernel of the Raspberry Pi you need a linux-development-system like Ubuntu as virtual machine or desktop machine.
The recommended specs are:
- 2 x 1,5GHz cores, more are recommended
- 2GB RAM, more are recommended
- More than 20GB of free disk space
Kernel building changed for newer kernels
For newer kernel versions the Raspberry Pi Foundation no longer uses their own "compile tools" for cross-compiling, but now uses the "crossbuild-essential-armhf" toolchain to do so. Some steps below are splitted into the old and the new procedure. Keep that in mind when following the steps.
The RTC Kernel module is now included in Raspberry Pi OS
Starting from the May 2023 releases of the Raspberry Pi OS the needed RV8803 RTC Kernel module is included in the OS by default. Users no longer need to compile the module themselves, adding one overlay to the config.txt is now enough to activate the RTC. Head over to the Onboard RTC section for more information.
Needed software:
Install the following on your linux-development-system
sudo apt-get install git bc
sudo apt-get install libncurses5-dev
sudo apt-get install make
sudo apt-get install gcc
sudo apt-get install bison flex libssl-dev libc6-dev
Only install one cross-compile toolchain, if in doubt try the crossbuild-essential-armhf toolchain (a) first.
a) Cross compile the Raspberry Pi kernel (for newer kernels)
sudo apt install crossbuild-essential-armhf
b) Cross compile the Raspberry Pi kernel (for older kernels)
git clone https://github.com/raspberrypi/tools ~/tools
echo PATH=\$PATH:~/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin >> ~/.bashrc
source ~/.bashrc
git clone --branch rpi-4.14.y https://github.com/raspberrypi/linux
cd linux
Note: In the above example the kernel 4.14 is used, you must adjust the branch part to an available branch at the Raspberry Pi firmware repository or which ever one you need.
If you want to compile modules etc. for your existing system you have to know the exact kernel version.
For this you should first update your Raspberry Pi system:
sudo apt-get update && sudo apt-get upgrade -y
To find out the right kernel version you have to type the following on your Raspberry Pi:
Look at the kernel version on your Pi-Tron/Software
uname -r
On your Linux development system:
Go to the website:
https://www.github.com/raspberrypi/linux
Pick the right branch by choosing the version you found out with 'uname -r'.
Look into the 'Makefile' There you can find the Version, Patchlevel and the Sublevel information. In the most cases the Sublevel is higher than the sublevel on your Raspberry Pi system. But to compile the right modules etc., you have to compile the right sublevel!
Therefore you must go to 'History' of the Makefile and search for the exact kernel version of your Raspberry Pi system.
If you found the right kernel version, you can look at the Makefile to make sure you have the right one:
On the top you can choose the marked link to navigate your final kernel version:
On the left side you can choose the corresponding commit id:
The complete commit id you need is on the right side:
→ Copy this commit id
Here are some commit id's to give you a starting point:
Linux 4.14.79-V7+
git checkout 36612d5d7a88672a3e7dd6cb458dbbbca0d75efe
Linux 4.14.98-v7+
git checkout 5d63a4595d32a8505590d5fea5c4ec1ca79fd49d
Linux 4.19.75-v7+
git checkout 6d8bf28fa4b1ca0a35c0cd1dcb267fb216daf720
Linux 4.19.97-v7+
git checkout f16e91dad2af9d57aef477cc1f522040353849f5
Linux 5.4.51-v7+
git checkout 3e92327867b6b40fed95ed36bb20f56d6e0cc879
Linux 5.4.72-v7+
git checkout ff93994fb3f92070d8521d709ad04675ecaa5817
To check that the kernel version is right, type in:
nano Makefile
Depending on your Raspberry Pi, enter the correct kernel parameter at the console and then start make.
For Pi1, PiZero, PiZeroW or CM1
KERNEL=kernel
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcmrpi_defconfig
For Pi2, Pi3, Pi3+, CM3 or CM3+
KERNEL=kernel7
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- bcm2709_defconfig
To configure the menuconfig and activate the RTC, type the following:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- menuconfig
Navigate to Device Driver --> Real Time Clock --> RTC-RV8803, Epson RX8900 and set the module to "M". Then Save all
Modify dtoverlay :
nano ~/linux/arch/arm/boot/dts/overlays/i2c-rtc-overlay.dts
supplement the following:
fragment@10 {
target = <&i2c_arm>;
__dormant__ {
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
rx8900: rx8900@32 {
compatible = "epson,rx8900";
reg = <0x32>;
status = "okay";
};
};
};
__overrides__ {
abx80x = <0>,"+0";
ds1307 = <0>,"+1";
ds1339 = <0>,"+2";
ds3231 = <0>,"+3";
mcp7940x = <0>,"+4";
mcp7941x = <0>,"+5";
pcf2127 = <0>,"+6";
pcf8523 = <0>,"+7";
pcf8563 = <0>,"+8";
m41t62 = <0>,"+9";
rx8900 = <0>,"+10";
trickle-diode-type = <&abx80x>,"abracon,tc-diode";
trickle-resistor-ohms = <&ds1339>,"trickle-resistor-ohms:0",
<&abx80x>,"abracon,tc-resistor";
wakeup-source = <&ds1339>,"wakeup-source?",
<&ds3231>,"wakeup-source?",
<&mcp7940x>,"wakeup-source?",
<&mcp7941x>,"wakeup-source?",
<&m41t62>,"wakeup-source?";
};
Note: The number behind fragment@, in this example 10, needs to be adapted to the current number found already in the file and increased by 1 and then the changes have to be made accordingly. This file is evolving and growing over time which means the maximum number of fragments in the file will be different as well. This is just an example of the needed changes.
Build the kernel:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- zImage modules dtbs
Note: This will take a while, consider using the -j parameter to make the compile process multi-threaded.
The compiled kernel module can be found in ~/linux/drivers/rtc/
and is named rtc-rv8803.ko. The corresponding new overlay file, i2c-rtc.dtbo, can be found under ~/linux/arch/arm/boot/dts/.
Copy the created driver and the overlay file on to your Raspberry Pi, e.g. into the pi user's home folder, and then copy the rtc driver to the rtc driver folder of your current kernel and the overlay file into the overlays folder on the boot partition.
#Copy kernel module into the RTC driver folder
cd ~
sudo cp rtc-rv8803.ko /lib/modules/{KERNELVERSION}/kernel/drivers/rtc
#Example for kernel 5.4.51-v7+
cd ~
sudo cp rtc-rv8803.ko /lib/modules/5.4.51-v7+/kernel/drivers/rtc
#Copy the new overlay file to overlays on the boot partition
cd ~
sudo cp -f i2c-rtc.dtbo /boot/overlays/
Also check the Onboard RTC section on how to configure the Raspberry Pi to use the onboard RTC module.