Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)

        自己做了一块Xavier载板,硬件裁剪了一些功能,导致官方镜像usb无法使用,我使用的是jetpack4.6.1(R32.7.1),所以进行驱动修改,重新编译内核。

一、虚拟机环境

在Windows下安装虚拟机Ubuntu18.04

二、官方源码下载

Jetson Linux R32.7.1 Release Page | NVIDIA Developer

下载图中三个文件

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第1张图片

 三、编译流程

1、建立工作空间

mkdir ~/xavier 

2、拷贝相关文件到此文件夹

u@u-vm:~/xavier$ ls
Jetson_Linux_R32.7.1_aarch64.tbz2 
Tegra_Linux_Sample-Root-Filesystem_R32.7.1_aarch64.tbz2 
public_sources.tbz2

3、解压固件并构建文件系统

a、解压bsp

u@u-vm:~/xavierx$ tar xf Tegra186_Linux_R32.4.2_aarch64.tbz2
u@u-vm:~/xavier$ ls
Linux_for_Tegra  
Jetson_Linux_R32.7.1_aarch64.tbz2 
Tegra_Linux_Sample-Root-Filesystem_R32.7.1_aarch64.tbz2 
public_sources.tbz2

b、解压并构建文件系统

u@u-vm:~/xavier$ cd Linux_for_Tegra/rootfs/
u@u-vm:~/xavier/Linux_for_Tegra/rootfs$ sudo tar xpf ../../Tegra_Linux_Sample-Root-Filesystem_R32.7.1_aarch64.tbz2
u@u-vm:~/xavier/Linux_for_Tegra/rootfs$ ls
bin  dev  etc  home  lib  media  mnt  opt  proc  README.txt  root  run  sbin  snap  srv  sys  tmp  usr  var

c.解压kernel源码

u@u-vm:~/xavier$ tar -xjf public_sources.tbz2

在你的source路径下,就会多出来一些源码包了:

u@u-vm:~/xavier/Linux_for_Tegra/source$ ls
nv_src_build.sh  public

进入kernel源码路径,执行解压

u@u-vm:~/xavier/Linux_for_Tegra/source/public$ tar -xjf kernel_src.tbz2

可以观察到多了kernel源码文件夹,进入路径就可以看到kernel源码结构了:

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ ls
arch                            build.config.goldfish.x86     drivers   Kconfig      NVIDIA-REVIEWERS  signing_key
block                           build.config.goldfish.x86_64  firmware  kernel       README            signing_key.x509
build.config.cuttlefish.x86_64  certs                         fs        lib          REPORTING-BUGS    sound
build.config.goldfish.arm       COPYING                       include   MAINTAINERS  rt-patches        tools
build.config.goldfish.arm64     CREDITS                       init      Makefile     samples           usr
build.config.goldfish.mips      crypto                        ipc       mm           scripts           verity_dev_keys.x509
build.config.goldfish.mips64    Documentation                 Kbuild    net          security          virt

4、工作路径和基础环境

u@u-vm:~/xavier/Linux_for_Tegra$ ls
apply_binaries.sh                  jetson-xavier-slvs-ec.conf           p2972-0000-devkit-maxn.conf
bootloader                         kernel                               p2972-0000-devkit-slvs-ec.conf
build_l4t_bup.sh                   l4t_generate_soc_bup.sh              p2972-0006-devkit.conf
flash.sh                           l4t_sign_image.sh                    p2972-as-galen-8gb.conf
jetson-tx2-4GB.conf                nvmassflashgen.sh                    p3449-0000+p3668-0000-qspi-sd.conf
jetson-tx2-as-4GB.conf             nv_tegra                             p3449-0000+p3668-0001-qspi-emmc.conf
jetson-tx2.conf                    p2771-0000-0888.conf                 p3509-0000+p3668-0000-qspi.conf
jetson-tx2i.conf                   p2771-0000-as-0888.conf              p3509-0000+p3668-0000-qspi-sd.conf
jetson-xavier-8gb.conf             p2771-0000.conf.common               p3509-0000+p3668-0001-qspi-emmc.conf
jetson-xavier-as-8gb.conf          p2771-0000-devkit.conf               p3668.conf.common
jetson-xavier-as-xavier-nx.conf    p2771-0000-dsi-hdmi-dp.conf          README_Massflash.txt
jetson-xavier.conf                 p2771-3489-ucm1.conf                 rootfs
jetson-xavier-maxn.conf            p2771-3489-ucm2.conf                 source
jetson-xavier-nx-devkit.conf       p2822+p2888-0001-as-p3668-0001.conf  source_sync.sh
jetson-xavier-nx-devkit-emmc.conf  p2972-0000.conf.common               tools
jetson-xavier-nx-devkit-qspi.conf  p2972-0000-devkit.conf

5、usb驱动修改

修改文件home/u/xavier/Linux_for_Tegra/source/public/hardware/nvidia/platform/t19x/galen/kernel-dts/common/tegra194-p2888-0001-p2822-0000-common.dtsi

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第2张图片

 如上图所示,找到此文件,有两个地方需要修改,如下图

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第3张图片

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第4张图片此方法是裁剪掉以下不用的功能,具体项目具体对待。

6、can时钟修改

Jetpack 4.6.1 , MTTCAN 配置发生了变化, CAN时钟默认为低功耗设置(没使能PLLAON), CAN工作不正常。

修改文件home/u/xavier/Linux_for_Tegra/source/public/hardware/nvidia/platform/t19x/galen/kernel-dts/common/tegra194-p2888-0001-p2822-0000-common.dtsi

a、把pllaon时钟使能,注释掉disable内容

# Linux_for_Tegra/source/public/hardware/nvidia/platform/t19x/galen/kernel-dts/common/tegra194-p2888-0001-p2822-0000-common.dtsi
	clocks-init {
		compatible = "nvidia,clocks-config";
		status = "okay";
		disable {
			//clocks = <&aon_clks TEGRA194_CLK_PLLAON>,
				//<&bpmp_clks TEGRA194_CLK_CAN1>,
				//<&bpmp_clks TEGRA194_CLK_CAN2>;
		};
	};

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第5张图片

 b、设备树节点 mttcan@c310000, mttcan@c320000 里附上 pllaon 时钟

# Linux_for_Tegra/source/public/hardware/nvidia/platform/t19x/galen/kernel-dts/common/tegra194-p2888-0001-p2822-0000-common.dtsi
	mttcan@c310000 {
		status = "okay";
		pll_source = "pllaon";
		clocks = <&bpmp_clks TEGRA194_CLK_CAN1_CORE>,
			<&bpmp_clks TEGRA194_CLK_CAN1_HOST>,
			<&bpmp_clks TEGRA194_CLK_CAN1>,
			<&bpmp_clks TEGRA194_CLK_PLLAON>;
		clock-names = "can_core", "can_host","can","pllaon";
	};

	mttcan@c320000 {
		status = "okay";
		pll_source = "pllaon";
		clocks = <&bpmp_clks TEGRA194_CLK_CAN2_CORE>,
			<&bpmp_clks TEGRA194_CLK_CAN2_HOST>,
			<&bpmp_clks TEGRA194_CLK_CAN2>,
			<&bpmp_clks TEGRA194_CLK_PLLAON>;
		clock-names = "can_core", "can_host","can","pllaon";
	};
	
# 可参考 Linux_for_Tegra/source/public/hardware/nvidia/soc/t19x/kernel-dts/tegra194-soc/tegra194-soc-can.dtsi

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第6张图片

 修改完成,进行下步操作。

7、相关编译器工具

在linux操作系统上只使用gcc编译是不够的,还要安装编译程序必须的软件包

a.使用下面的命令

sudo apt install build-essential bc
sudo apt-get install qemu-user-static

b.下载Dragonboard410c的交叉编译器

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第7张图片

 下载完成后解压到~/xavier/tools 文件夹

8、配置编译环境变量,直接修改 “~/.bashrc”

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ vim ~/.bashrc

在 “.bashrc” 末尾添加如下配置(路径注意和自己的电脑保持一致),保存退出:

export LOCALVERSION=-tegra
 
export TEGRA_KERNEL_OUT=/home/u/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel
 
export CROSS_COMPILE=/home/u/xavier/tools/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-

刷新环境变量

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ source ~/.bashrc

9、在源码路径下,执行命令:mkdir -p $TEGRA_KERNEL_OUT,建立编译输出路径

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ mkdir -p $TEGRA_KERNEL_OUT

10、make defconfig , 执行以下命令

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ make ARCH=arm64 O=$TEGRA_KERNEL_OUT tegra_defconfig

出现如下打印信息

make[1]: Entering directory '/home/u/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel'
  HOSTCC  scripts/basic/fixdep
  GEN     ./Makefile
  HOSTCC  scripts/kconfig/conf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  SHIPPED scripts/kconfig/zconf.hash.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
#
# configuration written to .config
#
make[1]: Leaving directory '/home/u/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel'u

11、执行以下命令,开始编译kernel,包含所有DTBs and modules.(-j8 是虚拟机处理器有8个核,不一样修改下,如4核 -j4 )

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ make ARCH=arm64 O=$TEGRA_KERNEL_OUT -j8

备注: 在编译过程中可能会报错:“… is not clean, please run ‘make mrproper’”,则在kernel-4.9路径下执行“make mrproper” 命令即可。

12、将新编译好的 Image 和 dtb 文件替换至烧录位置:

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel/arch/arm64/boot$ cp Image /home/u/xavier/Linux_for_Tegra/kernel/Image
 
u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9/okernel/arch/arm64/boot$ cp dts/* /home/u/xavier/Linux_for_Tegra/kernel/dtb/ -rf

13、执行以下命令来安装 kernel modules:

u@u-vm:~/xavier/Linux_for_Tegra/source/public/kernel/kernel-4.9$ sudo make ARCH=arm64 O=$TEGRA_KERNEL_OUT modules_install INSTALL_MOD_PATH=/home/u/xavier/Linux_for_Tegra/rootfs

14、在rootfs路径下

使用以下命令归档已安装的内核模块,执行完后可以看到多了“kernel_supplements.tbz2”

u@u-vm:~/xavier/Linux_for_Tegra/rootfs$ sudo tar --owner root --group root -cjf kernel_supplements.tbz2 lib/modules
 
u@u-vm:~/xavier/Linux_for_Tegra/rootfs$ ls
bin   dev  home                     lib    mnt  proc        root  sbin  srv  tmp  var
boot  etc  kernel_supplements.tbz2  media  opt  README.txt  run   snap  sys  usr

将 “kernel_supplements.tbz2”替换到“u@u-vm:~/xavier/Linux_for_Tegra/kernel$”路径下

u@u-vm:~/xavier/Linux_for_Tegra/rootfs$ sudo cp kernel_supplements.tbz2  ../kernel/kernel_supplements.tbz2

15、执行 apply_binaries.sh 脚本

u@u-vm:~/xavier/Linux_for_Tegra$ sudo ./apply_binaries.sh

至此编译完成。

四、烧写固件

不同模组对应烧写文件不同,注意选择正确的文件,我的是Xavier工业版的,如果是常规版选择jetson-agx-xavier-devkit就行了。

Jetson Agx Xavier USB驱动裁剪+can时钟修改+内核源码编译流程(jetpack4.6.1)_第8张图片

Xavier进入刷机模式,使用usb-type-c线连接虚拟机,执行烧写命令

cd ~/xavier/Linux_for_Tegra
sudo ./flash.sh jetson-agx-xavier-industrial mmcblk0p1

五、参考文章

感谢大佬

Jetson Xavier 设置 CANFD (Jetpack 4.6, 开启pllaon)_weifengdq的博客-CSDN博客

Jetson Xavier NX 内核编译、源码编译流程_知秋贺的博客-CSDN博客

Jetson Xavier NX 内核编译、源码编译流程_知秋贺的博客-CSDN博客

你可能感兴趣的:(xavier,linux,ubuntu)