google / periph

Older version of periph, see new version at https://github.com/periph
https://periph.io
Apache License 2.0
1.75k stars 167 forks source link

switch processor/platform detection to /proc/device-tree #25

Closed tve closed 1 year ago

tve commented 8 years ago

Currently most processors and platforms are detected using /proc/cpuinfo, which seems to vary a lot from distro to distro. The device tree seems to vary less, specifically the "model" and "compatible" info found in /proc/device-tree/{model,compatible}. The purpose of this issue is to validate this assumption and to switch the detection accordingly. The following is some data gathered in a ticket in the old dlibox repo.

Here's an ODROID-C1+:

root@oc1:/proc# head /boot/meson8b_odroidc.dts
/dts-v1/;
/{
        compatible = "AMLOGIC,8726_M8B";
        model = "AMLOGIC";
        interrupt-parent = <&gic>;
        #address-cells = <1>;
        #size-cells = <1>;

    cpus {
        #address-cells = <1>;
root@oc1:/proc# od -c /proc/device-tree/compatible
0000000   A   M   L   O   G   I   C   ,   8   7   2   6   _   M   8   B
0000020  \0
0000021
root@oc1:/proc# od -c /proc/device-tree/model
0000000   A   M   L   O   G   I   C  \0
0000010

Looking at a -C1+ running arch linux I see the same in /proc/device-tree, that's encouraging. A -C1 (non-plus) shows the same.

On the ODROID-XU4:

        model = "Hardkernel odroid-xu3 board based on EXYNOS5422";
        compatible = "Hardkernel,odroid-xu3";

Aha, the xu3 and xu4 use the same SoC so hardkernel doesn't bother producing two different device trees.

An old BBB running Ubuntu 14.04:

tve@base:~$ od -c /proc/device-tree/model
0000000   T   I       A   M   3   3   5   x       B   e   a   g   l   e
0000020   B   o   n   e  \0
0000025
tve@base:~$ od -c /proc/device-tree/compatible
0000000   t   i   ,   a   m   3   3   5   x   -   b   o   n   e  \0   t
0000020   i   ,   a   m   3   3   x   x  \0
0000031

Looks like on rPi /proc/device-tree/model should be one of:

arch/arm/boot/dts/bcm2708-rpi-b-plus.dts:       model = "Raspberry Pi Model B+";
arch/arm/boot/dts/bcm2708-rpi-b.dts:    model = "Raspberry Pi Model B";
arch/arm/boot/dts/bcm2708-rpi-cm.dts:   model = "Raspberry Pi Compute Module";
arch/arm/boot/dts/bcm2709-rpi-2-b.dts:  model = "Raspberry Pi 2 Model B";
arch/arm/boot/dts/bcm2835-rpi-b-plus.dts:       compatible = "raspberrypi,model-b-plus", "brcm,bcm2835";
arch/arm/boot/dts/bcm2835-rpi-b-plus.dts:       model = "Raspberry Pi Model B+";
arch/arm/boot/dts/bcm2835-rpi-b.dts:    compatible = "raspberrypi,model-b", "brcm,bcm2835";
arch/arm/boot/dts/bcm2835-rpi-b.dts:    model = "Raspberry Pi Model B";

All on Raspbian Jessie Lite

RPi3:

$ od -t x1z /sys/firmware/devicetree/base/compatible
0000000 62 72 63 6d 2c 62 63 6d 32 37 31 30 00 62 72 63  >brcm,bcm2710.brc<
0000020 6d 2c 62 63 6d 32 37 30 39 00                    >m,bcm2709.<
0000032
$ od -t x1z /sys/firmware/devicetree/base/model
0000000 52 61 73 70 62 65 72 72 79 20 50 69 20 33 20 4d  >Raspberry Pi 3 M<
0000020 6f 64 65 6c 20 42 20 52 65 76 20 31 2e 32 00     >odel B Rev 1.2.<
0000037

RPi2:

$ od -t x1z /sys/firmware/devicetree/base/compatible
0000000 62 72 63 6d 2c 62 63 6d 32 37 30 39 00           >brcm,bcm2709.<
0000015
$ od -t x1z /sys/firmware/devicetree/base/model
0000000 52 61 73 70 62 65 72 72 79 20 50 69 20 32 20 4d  >Raspberry Pi 2 M<
0000020 6f 64 65 6c 20 42 20 52 65 76 20 31 2e 31 00     >odel B Rev 1.1.<
0000037

RPI1:

$ od -t x1z /sys/firmware/devicetree/base/compatible
0000000 62 72 63 6d 2c 62 63 6d 32 37 30 38 00           >brcm,bcm2708.<
0000015
$ od -t x1z /sys/firmware/devicetree/base/model
0000000 52 61 73 70 62 65 72 72 79 20 50 69 20 4d 6f 64  >Raspberry Pi Mod<
0000020 65 6c 20 42 20 52 65 76 20 32 00                 >el B Rev 2.<
0000033

On Pine64 on Armbian:

$ od -t x1z /proc/device-tree/model 
0000000 73 75 6e 35 30 69 77 31 70 31 00                 >sun50iw1p1.<
0000013
$ od -t x1z /proc/device-tree/compatible 
0000000 61 72 6d 2c 73 75 6e 35 30 69 77 31 70 31 00 61  >arm,sun50iw1p1.a<
0000020 72 6d 2c 73 75 6e 35 30 69 77 31 70 31 00        >rm,sun50iw1p1.<
0000036
maruel commented 8 years ago

Was it fixed by 69e0d9cc801a539984b24dd54bad848af42e14b5?

tve commented 8 years ago

The CHIP and odroid_c1 platform detection uses the device tree. It appears more stable across OS distributions and more specific than /proc/cpuinfo and /proc/os-release.

It may be good to change the other platforms too, but perhaps "don't fix what isn't broken"? IN that case, I'd say "yes".

maruel commented 1 year ago

Some of the work was done, in particular https://pkg.go.dev/periph.io/x/host/v3/distro. I don't plan to do more work in this area so I'll close. If changes are needed, let's file an issue at https://github.com/periph/host.