diff --git a/CODEOWNERS b/CODEOWNERS index f0a1b5a4..6ccf8247 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -8,6 +8,7 @@ pcengines/apu @yegortimoshenko pine64/rockpro64 @tomfitzhenry pine64/star64 @fgaz purism/librem/13v3 @yegortimoshenko +raspberry-pi @doronbehar system76/darp6 @khumba framework @emiller88 @johnazoidberg tuxedo/pulse/15/gen2 @trueNAHO diff --git a/README.md b/README.md index 4103ad59..f7f3ac5d 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,7 @@ See code for all available configurations. | [Gigabyte B550](gigabyte/b550) | `` | `gigabyte-b550` | | [Gigabyte B650](gigabyte/b650) | `` | `gigabyte-b650` | | [GMKtec NucBox G3 Plus](gmktec/nucbox/g3-plus) | `` | `gmktec-nucbox-g3-plus` | +| [GPD Duo](gpd/duo) | `` | `gpd-duo` | | [GPD MicroPC](gpd/micropc) | `` | `gpd-micropc` | | [GPD P2 Max](gpd/p2-max) | `` | `gpd-p2-max` | | [GPD Pocket 3](gpd/pocket-3) | `` | `gpd-pocket-3` | @@ -306,6 +307,8 @@ See code for all available configurations. | [Lenovo ThinkPad L14 (Intel)](lenovo/thinkpad/l14/intel) | `` | `lenovo-thinkpad-l14-intel` | | [Lenovo ThinkPad L480](lenovo/thinkpad/l480) | `` | `lenovo-thinkpad-l480` | | [Lenovo ThinkPad P1 Gen 3](lenovo/thinkpad/p1/3th-gen) | `` | `lenovo-thinkpad-p1-gen3` | +| [Lenovo ThinkPad P1 Gen 5](lenovo/thinkpad/p1/gen5) | `` | `lenovo-thinkpad-p1-gen5` | +| [Lenovo ThinkPad P1 Gen 5 (Nvidia)](lenovo/thinkpad/p1/gen5/nvidia) | `` | `lenovo-thinkpad-p1-gen5-nvidia` | | [Lenovo ThinkPad P14s AMD Gen 1](lenovo/thinkpad/p14s/amd/gen1) | `` | `lenovo-thinkpad-p14s-amd-gen1` | | [Lenovo ThinkPad P14s AMD Gen 2](lenovo/thinkpad/p14s/amd/gen2) | `` | `lenovo-thinkpad-p14s-amd-gen2` | | [Lenovo ThinkPad P14s AMD Gen 3](lenovo/thinkpad/p14s/amd/gen3) | `` | `lenovo-thinkpad-p14s-amd-gen3` | @@ -361,6 +364,7 @@ See code for all available configurations. | [Lenovo ThinkPad X1 Yoga](lenovo/thinkpad/x1/yoga) | `` | `lenovo-thinkpad-x1-yoga` | | [Lenovo ThinkPad X1 Yoga Gen 7](lenovo/thinkpad/x1/yoga/7th-gen/) | `` | `lenovo-thinkpad-x1-yoga-7th-gen` | | [Lenovo ThinkPad X1 Yoga Gen 8](lenovo/thinkpad/x1/yoga/8th-gen/) | `` | `lenovo-thinkpad-x1-yoga-8th-gen` | +| [Lenovo ThinkPad X1 Yoga Gen 8 (IPU6)](lenovo/thinkpad/x1/yoga/8th-gen/ipu6) | `` | `lenovo-thinkpad-x1-yoga-8th-gen-ipu6` | | [Lenovo ThinkPad X1 (2nd Gen)](lenovo/thinkpad/x1/2nd-gen) | `` | `lenovo-thinkpad-x1-2nd-gen` | | [Lenovo ThinkPad X1 (6th Gen)](lenovo/thinkpad/x1/6th-gen) | `` | `lenovo-thinkpad-x1-6th-gen` | | [Lenovo ThinkPad X1 (7th Gen)](lenovo/thinkpad/x1/7th-gen) | `` | `lenovo-thinkpad-x1-7th-gen` | diff --git a/apple/t2/pkgs/brcm-firmware/default.nix b/apple/t2/pkgs/brcm-firmware/default.nix index 742d6ff6..e444e4f6 100644 --- a/apple/t2/pkgs/brcm-firmware/default.nix +++ b/apple/t2/pkgs/brcm-firmware/default.nix @@ -2,10 +2,7 @@ lib, stdenvNoCC, callPackage, - vmTools, - util-linux, - linux, - kmod, + _7zz, version, }: @@ -38,52 +35,44 @@ let }; in -vmTools.runInLinuxVM ( - stdenvNoCC.mkDerivation { - pname = "brcm-firmware"; - inherit version; +stdenvNoCC.mkDerivation { + pname = "brcm-firmware"; + inherit version; - src = fetchmacos { - name = version; - inherit (boards.${version}) - boardId - mlb - osType - hash - ; - }; - dontUnpack = true; + src = fetchmacos { + name = version; + inherit (boards.${version}) + boardId + mlb + osType + hash + ; + }; + dontUnpack = true; - nativeBuildInputs = [ - util-linux - get-firmware - ]; - buildPhase = '' - ln -s ${linux}/lib /lib - ${kmod}/bin/modprobe loop - ${kmod}/bin/modprobe hfsplus + nativeBuildInputs = [ + _7zz + get-firmware + ]; + buildPhase = '' + 7zz x -bd -bso0 -bsp0 $src \ + "macOS Base System/usr/share/firmware/bluetooth/*" \ + "macOS Base System/usr/share/firmware/wifi/*" - imgdir=$(mktemp -d) - loopdev=$(losetup -f | cut -d "/" -f 3) - losetup -P $loopdev $src - loopdev_partition=/dev/$(lsblk -o KNAME,TYPE,MOUNTPOINT -n | grep $loopdev | tail -1 | awk '{print $1}') - mount $loopdev_partition $imgdir + get-bluetooth "macOS Base System/usr/share/firmware/bluetooth" bluetooth/ + get-wifi "macOS Base System/usr/share/firmware/wifi" wifi/ + ''; - get-bluetooth $imgdir/usr/share/firmware/bluetooth bluetooth/ - get-wifi $imgdir/usr/share/firmware/wifi wifi/ - ''; + installPhase = '' + mkdir -p $out/lib/firmware/brcm + cp bluetooth/brcm/* $out/lib/firmware/brcm/ + cp wifi/brcm/* $out/lib/firmware/brcm/ + ''; - installPhase = '' - mkdir -p $out/lib/firmware/brcm - cp bluetooth/brcm/* $out/lib/firmware/brcm/ - cp wifi/brcm/* $out/lib/firmware/brcm/ - ''; - - meta = with lib; { - description = "Wi-Fi and Bluetooth firmware for T2 Macs"; - license = licenses.unfree; - maintainers = with maintainers; [ mkorje ]; - platforms = platforms.linux; - }; - } -) + meta = with lib; { + description = "Wi-Fi and Bluetooth firmware for T2 Macs"; + license = licenses.unfree; + maintainers = with maintainers; [ mkorje ]; + platforms = platforms.linux; + }; +} diff --git a/common/gpu/amd/dc-debug-mask.nix b/common/gpu/amd/dc-debug-mask.nix new file mode 100644 index 00000000..5c73dfc7 --- /dev/null +++ b/common/gpu/amd/dc-debug-mask.nix @@ -0,0 +1,36 @@ +# Flag values from drivers/gpu/drm/amd/include/amd_shared.h +{ config, lib, ... }: +let + flags = { + disablePipeSplit = 1; + disableStutter = 2; + disableDsc = 4; + disableClockGating = 8; + disablePsr = 16; + disableMpo = 64; + disablePsrSu = 512; + disableReplay = 1024; + disableIps = 2048; + }; + + cfg = config.hardware.amdgpu.dcDebugMask; + + mask = lib.foldlAttrs ( + acc: name: bit: + if cfg.${name} then lib.bitOr acc bit else acc + ) 0 flags; +in +{ + options.hardware.amdgpu.dcDebugMask = lib.mapAttrs ( + _name: bit: + lib.mkOption { + type = lib.types.bool; + default = false; + description = "Set flag 0x${lib.toHexString bit} in amdgpu.dcdebugmask."; + } + ) flags; + + config = lib.mkIf (mask != 0) { + boot.kernelParams = [ "amdgpu.dcdebugmask=0x${lib.toHexString mask}" ]; + }; +} diff --git a/common/gpu/amd/default.nix b/common/gpu/amd/default.nix index f6903d9e..5bb4546e 100644 --- a/common/gpu/amd/default.nix +++ b/common/gpu/amd/default.nix @@ -1,6 +1,8 @@ { lib, ... }: { + imports = [ ./dc-debug-mask.nix ]; + config = { services.xserver.videoDrivers = lib.mkDefault [ "modesetting" ]; diff --git a/common/pc/laptop/default.nix b/common/pc/laptop/default.nix index 5711e531..5ad4b777 100644 --- a/common/pc/laptop/default.nix +++ b/common/pc/laptop/default.nix @@ -3,5 +3,7 @@ { imports = [ ../. ]; - services.tlp.enable = lib.mkDefault (!config.services.power-profiles-daemon.enable); + services.tlp.enable = lib.mkDefault ( + !config.services.power-profiles-daemon.enable && !config.services.tuned.enable + ); } diff --git a/flake.lock b/flake.lock index 67a1708d..1187da68 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1767892417, - "narHash": "sha256-8bW3q88CEg2u4hSP66Vf4lpbLonHz7hqDNBMcCY7E9U=", - "rev": "3497aa5c9457a9d88d71fa93a4a8368816fbeeba", + "lastModified": 1789546076, + "narHash": "sha256-vWkSk5bbfTqdtMoSgD9FshACO8JCvXTFi+3cqEp0mH0=", + "rev": "b1b875982b17dabde9b4a37f3e229e74913e6db3", "type": "tarball", - "url": "https://releases.nixos.org/nixos/unstable/nixos-26.05pre924538.3497aa5c9457/nixexprs.tar.xz" + "url": "https://releases.nixos.org/nixos/unstable/nixos-26.11pre1074753.b1b875982b17/nixexprs.tar.xz" }, "original": { "type": "tarball", diff --git a/flake.nix b/flake.nix index f4a8c3c6..380a4b1c 100644 --- a/flake.nix +++ b/flake.nix @@ -194,6 +194,7 @@ google-pixelbook = import ./google/pixelbook; google-brya = import ./google/brya; google-rex = import ./google/rex; + gpd-duo = import ./gpd/duo; gpd-micropc = import ./gpd/micropc; gpd-p2-max = import ./gpd/p2-max; gpd-pocket-3 = import ./gpd/pocket-3; @@ -265,6 +266,8 @@ lenovo-thinkpad-l480 = import ./lenovo/thinkpad/l480; lenovo-thinkpad-p1 = import ./lenovo/thinkpad/p1; lenovo-thinkpad-p1-gen3 = import ./lenovo/thinkpad/p1/3th-gen; + lenovo-thinkpad-p1-gen5 = import ./lenovo/thinkpad/p1/gen5; + lenovo-thinkpad-p1-gen5-nvidia = import ./lenovo/thinkpad/p1/gen5/nvidia; lenovo-thinkpad-p14s-amd-gen1 = import ./lenovo/thinkpad/p14s/amd/gen1; lenovo-thinkpad-p14s-amd-gen2 = import ./lenovo/thinkpad/p14s/amd/gen2; lenovo-thinkpad-p14s-amd-gen3 = import ./lenovo/thinkpad/p14s/amd/gen3; @@ -320,6 +323,7 @@ lenovo-thinkpad-x1-yoga = import ./lenovo/thinkpad/x1/yoga; lenovo-thinkpad-x1-yoga-7th-gen = import ./lenovo/thinkpad/x1/yoga/7th-gen; lenovo-thinkpad-x1-yoga-8th-gen = import ./lenovo/thinkpad/x1/yoga/8th-gen; + lenovo-thinkpad-x1-yoga-8th-gen-ipu6 = import ./lenovo/thinkpad/x1/yoga/8th-gen/ipu6; lenovo-thinkpad-x1-2nd-gen = import ./lenovo/thinkpad/x1/2nd-gen; lenovo-thinkpad-x1-6th-gen = import ./lenovo/thinkpad/x1/6th-gen; lenovo-thinkpad-x1-7th-gen = import ./lenovo/thinkpad/x1/7th-gen; @@ -553,6 +557,9 @@ pkgs.lib.optionalAttrs (self.formatter ? ${system}) { formatting = treefmtEval.config.build.check self; } + // { + raspberry-pi-config-txt = pkgs.callPackage ./tests/config-txt-tests.nix { }; + } // nixosTests ); }; diff --git a/framework/13-inch/common/amd.nix b/framework/13-inch/common/amd.nix index 4a89f8da..fc34d3dc 100644 --- a/framework/13-inch/common/amd.nix +++ b/framework/13-inch/common/amd.nix @@ -6,18 +6,18 @@ ../../../common/gpu/amd ]; - boot.kernelParams = [ - # There seems to be an issue with panel self-refresh (PSR) that - # causes hangs for users. - # - # https://community.frame.work/t/fedora-kde-becomes-suddenly-slow/58459 - # https://gitlab.freedesktop.org/drm/amd/-/issues/3647 - "amdgpu.dcdebugmask=0x10" - ] - # Workaround for SuspendThenHibernate: https://lore.kernel.org/linux-kernel/20231106162310.85711-1-mario.limonciello@amd.com/ - ++ lib.optionals (lib.versionOlder config.boot.kernelPackages.kernel.version "6.8") [ - "rtc_cmos.use_acpi_alarm=1" - ]; + # There seems to be an issue with panel self-refresh (PSR) that + # causes hangs for users. + # + # https://community.frame.work/t/fedora-kde-becomes-suddenly-slow/58459 + # https://gitlab.freedesktop.org/drm/amd/-/issues/3647 + hardware.amdgpu.dcDebugMask.disablePsr = lib.mkDefault true; + + boot.kernelParams = + # Workaround for SuspendThenHibernate: https://lore.kernel.org/linux-kernel/20231106162310.85711-1-mario.limonciello@amd.com/ + lib.optionals (lib.versionOlder config.boot.kernelPackages.kernel.version "6.8") [ + "rtc_cmos.use_acpi_alarm=1" + ]; # AMD has better battery life with PPD over TLP: # https://community.frame.work/t/responded-amd-7040-sleep-states/38101/13 diff --git a/framework/13-inch/common/classic-audio.nix b/framework/13-inch/common/classic-audio.nix index db60c52b..e0d41bdc 100644 --- a/framework/13-inch/common/classic-audio.nix +++ b/framework/13-inch/common/classic-audio.nix @@ -315,12 +315,11 @@ in "device.api" = "dsp"; "node.virtual" = "false"; - # Lower seems to mean "more preferred", - # bluetooth devices seem to be ~1000, speakers seem to be ~2000 - # since this is between the two, bluetooth devices take over when they connect, - # and hand over to this instead of the speakers when they disconnect. - "priority.session" = 1500; - "priority.driver" = 1500; + # 1009 is the default priority of the "first" analog audio device. + # Higher priorities (e.g., 1010 for bluetooth and 1100 for usb audio) + # are preferred. + "priority.session" = 1009; + "priority.driver" = 1009; "state.default-volume" = 0.343; "device.icon-name" = "audio-card-analog-pci"; }; diff --git a/framework/16-inch/amd-ai-300-series/default.nix b/framework/16-inch/amd-ai-300-series/default.nix index 1e42fff1..c4d4e911 100644 --- a/framework/16-inch/amd-ai-300-series/default.nix +++ b/framework/16-inch/amd-ai-300-series/default.nix @@ -21,8 +21,9 @@ # The following mitigations fix various graphics issues # See https://gist.github.com/lbrame/f9034b1a9fe4fc2d2835c5542acb170a#user-content-quick-version-apply-the-mitigations-i-am-personally-using + hardware.amdgpu.dcDebugMask.disableReplay = lib.mkDefault true; + boot.kernelParams = [ - "amdgpu.dcdebugmask=0x410" "amdgpu.sg_display=0" "amdgpu.abmlevel=0" ]; diff --git a/framework/16-inch/common/amd.nix b/framework/16-inch/common/amd.nix index 4a89f8da..fc34d3dc 100644 --- a/framework/16-inch/common/amd.nix +++ b/framework/16-inch/common/amd.nix @@ -6,18 +6,18 @@ ../../../common/gpu/amd ]; - boot.kernelParams = [ - # There seems to be an issue with panel self-refresh (PSR) that - # causes hangs for users. - # - # https://community.frame.work/t/fedora-kde-becomes-suddenly-slow/58459 - # https://gitlab.freedesktop.org/drm/amd/-/issues/3647 - "amdgpu.dcdebugmask=0x10" - ] - # Workaround for SuspendThenHibernate: https://lore.kernel.org/linux-kernel/20231106162310.85711-1-mario.limonciello@amd.com/ - ++ lib.optionals (lib.versionOlder config.boot.kernelPackages.kernel.version "6.8") [ - "rtc_cmos.use_acpi_alarm=1" - ]; + # There seems to be an issue with panel self-refresh (PSR) that + # causes hangs for users. + # + # https://community.frame.work/t/fedora-kde-becomes-suddenly-slow/58459 + # https://gitlab.freedesktop.org/drm/amd/-/issues/3647 + hardware.amdgpu.dcDebugMask.disablePsr = lib.mkDefault true; + + boot.kernelParams = + # Workaround for SuspendThenHibernate: https://lore.kernel.org/linux-kernel/20231106162310.85711-1-mario.limonciello@amd.com/ + lib.optionals (lib.versionOlder config.boot.kernelPackages.kernel.version "6.8") [ + "rtc_cmos.use_acpi_alarm=1" + ]; # AMD has better battery life with PPD over TLP: # https://community.frame.work/t/responded-amd-7040-sleep-states/38101/13 diff --git a/framework/16-inch/common/default.nix b/framework/16-inch/common/default.nix index 44f867f9..0b5150be 100644 --- a/framework/16-inch/common/default.nix +++ b/framework/16-inch/common/default.nix @@ -28,22 +28,6 @@ # Enable keyboard customization hardware.keyboard.qmk.enable = lib.mkDefault true; - # Allow `services.libinput.touchpad.disableWhileTyping` to work correctly. - # Set unconditionally because libinput can also be configured dynamically via - # gsettings. - # - # This is extracted from the quirks file that is in the upstream libinput - # source. Once we can assume everyone is on at least libinput 1.26.0, this - # local override file can be removed. - # https://gitlab.freedesktop.org/libinput/libinput/-/commit/566857bd98131009699c9ab6efc7af37afd43fd0 - environment.etc."libinput/local-overrides.quirks".text = '' - [Framework Laptop 16 Keyboard Module] - MatchName=Framework Laptop 16 Keyboard Module* - MatchUdevType=keyboard - MatchDMIModalias=dmi:*svnFramework:pnLaptop16* - AttrKeyboardIntegration=internal - ''; - # Everything is updateable through fwupd services.fwupd.enable = true; } diff --git a/gpd/duo/default.nix b/gpd/duo/default.nix new file mode 100644 index 00000000..a7d8cb75 --- /dev/null +++ b/gpd/duo/default.nix @@ -0,0 +1,60 @@ +{ + config, + lib, + pkgs, + ... +}: + +let + cfg = config.hardware.gpd.duo; +in +with lib; +{ + imports = [ + ./duo-specific + ../../common/pc/laptop + ../../common/pc/ssd + ../../common/hidpi.nix + ]; + + options = { + hardware.gpd.duo.preventWakeOnAC = mkOption { + type = types.bool; + default = false; + description = '' + Stop the system waking from suspend when the AC is plugged + in. The catch: it also disables waking from the keyboard. + + See: + https://community.frame.work/t/tracking-framework-amd-ryzen-7040-series-lid-wakeup-behavior-feedback/39128/45 + ''; + }; + }; + + config = { + # Workaround applied upstream in Linux >=6.7 (on BIOS 03.03) + # https://github.com/torvalds/linux/commit/a55bdad5dfd1efd4ed9ffe518897a21ca8e4e193 + services.udev.extraRules = + mkIf (versionOlder config.boot.kernelPackages.kernel.version "6.7" && cfg.preventWakeOnAC) + '' + # Prevent wake when plugging in AC during suspend. Trade-off: keyboard wake disabled. See: + # https://community.frame.work/t/tracking-framework-amd-ryzen-7040-series-lid-wakeup-behavior-feedback/39128/45 + ACTION=="add", SUBSYSTEM=="serio", DRIVERS=="atkbd", ATTR{power/wakeup}="disabled" + ''; + + # Replace 'left' with 'right' or 'inverted' as needed + # Fixes DUO stupid inverted display at boot + # Enable kernel module for your graphics (adjust if needed) + boot.kernelModules = [ "amdgpu" ]; + + # Set the eDP-1 panel video parameters for display rotation + boot.kernelParams = mkBefore [ + "video=eDP-1:2880x1800,panel_orientation=upside_down" + "video=DP-3:2880x1800,panel_orientation=normal" + "i2c_touchscreen_props=GXTP7380:touchscreen-inverted-x:touchscreen-inverted-y" + ]; + + hardware.gpd.duo.audioEnhancement.rawDeviceName = + mkDefault "alsa_output.pci-0000_c1_00.6.analog-stereo"; + }; +} diff --git a/gpd/duo/duo-specific/amd.nix b/gpd/duo/duo-specific/amd.nix new file mode 100644 index 00000000..c2f36a60 --- /dev/null +++ b/gpd/duo/duo-specific/amd.nix @@ -0,0 +1,27 @@ +{ lib, config, ... }: +with lib; +{ + imports = [ + ../../../common/cpu/amd + ../../../common/cpu/amd/pstate.nix + ../../../common/gpu/amd + ]; + + boot.kernelParams = [ + # There seems to be an issue with panel self-refresh (PSR) that + # causes hangs for users. + # + # https://community.frame.work/t/fedora-kde-becomes-suddenly-slow/58459 + # https://gitlab.freedesktop.org/drm/amd/-/issues/3647 + "amdgpu.dcdebugmask=0x210" + ] + # Workaround for SuspendThenHibernate: https://lore.kernel.org/linux-kernel/20231106162310.85711-1-mario.limonciello@amd.com/ + ++ optionals (versionOlder config.boot.kernelPackages.kernel.version "6.8") [ + "rtc_cmos.use_acpi_alarm=1" + ]; + + # AMD has better battery life with PPD over TLP: + # https://community.frame.work/t/responded-amd-7040-sleep-states/38101/13 + services.power-profiles-daemon.enable = mkDefault true; + services.tlp.enable = mkDefault false; +} diff --git a/gpd/duo/duo-specific/audio.nix b/gpd/duo/duo-specific/audio.nix new file mode 100644 index 00000000..d384c273 --- /dev/null +++ b/gpd/duo/duo-specific/audio.nix @@ -0,0 +1,391 @@ +{ + config, + lib, + pkgs, + ... +}: +let + cfg = config.hardware.gpd.duo.audioEnhancement; +in +with lib; +{ + options = { + hardware.gpd.duo.audioEnhancement = { + enable = mkOption { + type = types.bool; + default = false; + description = '' + Create a new audio device called "DUO Speakers", + which applies sound tuning before sending the audio out to the speakers. + This option requires PipeWire and WirePlumber. + + The filter chain includes the following: + - Pyschoacoustic bass enhancement + - Loudness compensation + - Equalizer + - Slight compression + + This option has been optimised for the Framework Laptop 13 AMD 7040 series, but should work on all models. + + Before applying, ensure the speakers are set to 100%, + because the volumes compound and the raw speaker device will be hidden by default. + + You might also need to re-select the default output device. + + In some cases, the added bass will vibrate the keyboard cable leading to a rattling sound, + a piece of foam can be used to mitigate this. + ''; + }; + + hideRawDevice = mkOption { + type = types.bool; + default = true; + description = '' + Hide the raw speaker device. + This option is enabled by default, because keeping the raw speaker device can lead to volume conflicts. + ''; + }; + + rawDeviceName = mkOption { + type = types.str; + example = "alsa_output.pci-0000_c1_00.6.analog-stereo"; + description = '' + The name of the raw speaker device. This will vary by device. + You can get this by running `pw-dump | grep -C 20 pci-0000`. + ''; + }; + }; + }; + + config = mkIf cfg.enable ( + let + outputName = cfg.rawDeviceName; + prettyName = "Duo Speakers"; + + # These are pre-made decibel to linear value conversions, since Nix doesn't have pow(). + # Use the formula `10 ** (db / 20)` to calculate. + db = { + "-18.1" = 0.1244514611771385; + "-5.48" = 0.5321082592667942; + "-4.76" = 0.5780960474057181; + "8.1" = 2.5409727055493048; + "-36" = 1.5848931924611134e-2; + }; + + json = pkgs.formats.json { }; + + # The filter chain, heavily inspired by the asahi-audio project: https://github.com/AsahiLinux/asahi-audio + filter-chain = json.generate "filter-chain.json" { + "node.description" = prettyName; + "media.name" = prettyName; + "filter.graph" = { + nodes = [ + # Psychoacoustic bass extension, + # it creates harmonics of the missing bass to fool our ears into hearing it. + { + type = "lv2"; + plugin = "https://chadmed.au/bankstown"; + name = "bassex"; + control = { + bypass = 0; + amt = 1.2; + sat_second = 1.3; + sat_third = 2.5; + blend = 1.0; + ceil = 200.0; + floor = 20.0; + }; + } + # Loudness compensation, + # it ensures that the sound profile stays consistent across different volumes. + { + type = "lv2"; + plugin = "http://lsp-plug.in/plugins/lv2/loud_comp_stereo"; + name = "el"; + control = { + enabled = 1; + input = 1.0; + fft = 4; + }; + } + # 8-band equalizer, + # it tries to lessen frequencies where the laptop might resonate, + # and tries to make the frequency curve more pleasing; + # this is the "Lappy McTopface" profile (https://github.com/ceiphr/ee-framework-presets) + # further tuned for the Framework Laptop 13 AMD 7040 series + # and might need some tuning on other models. + { + type = "lv2"; + plugin = "http://lsp-plug.in/plugins/lv2/para_equalizer_x8_lr"; + name = "fw13eq"; + control = { + mode = 0; + react = 0.2; + zoom = db."-36"; + + fl_0 = 101.0; + fml_0 = 0; + ftl_0 = 5; + gl_0 = db."-18.1"; + huel_0 = 0.0; + ql_0 = 4.36; + sl_0 = 0; + wl_0 = 4.0; + + fl_1 = 451.0; + fml_1 = 0; + ftl_1 = 1; + gl_1 = db."-5.48"; + huel_1 = 3.125e-2; + ql_1 = 2.46; + sl_1 = 0; + wl_1 = 4.0; + + fl_2 = 918.0; + fml_2 = 0; + ftl_2 = 1; + gl_2 = db."-4.76"; + huel_2 = 6.25e-2; + ql_2 = 2.44; + sl_2 = 0; + wl_2 = 4.0; + + fl_3 = 9700.0; + fml_3 = 0; + ftl_3 = 1; + gl_3 = db."8.1"; + huel_3 = 9.375e-2; + ql_3 = 2.0; + sl_3 = 0; + wl_3 = 4.0; + + fr_0 = 101.0; + fmr_0 = 0; + ftr_0 = 5; + gr_0 = db."-18.1"; + huer_0 = 0.0; + qr_0 = 4.36; + sr_0 = 0; + wr_0 = 4.0; + + fr_1 = 451.0; + fmr_1 = 0; + ftr_1 = 1; + gr_1 = db."-5.48"; + huer_1 = 3.125e-2; + qr_1 = 2.46; + sr_1 = 0; + wr_1 = 4.0; + + fr_2 = 918.0; + fmr_2 = 0; + ftr_2 = 1; + gr_2 = db."-4.76"; + huer_2 = 6.25e-2; + qr_2 = 2.44; + sr_2 = 0; + wr_2 = 4.0; + + fr_3 = 9700.0; + fmr_3 = 0; + ftr_3 = 1; + gr_3 = db."8.1"; + huer_3 = 9.375e-2; + qr_3 = 2.0; + sr_3 = 0; + wr_3 = 4.0; + }; + } + # Compressors. The settings were taken from the asahi-audio project. + { + type = "lv2"; + plugin = "http://lsp-plug.in/plugins/lv2/mb_compressor_stereo"; + name = "woofer_bp"; + control = { + mode = 0; + ce_0 = 1; + sla_0 = 5.0; + cr_0 = 1.75; + al_0 = 0.725; + at_0 = 1.0; + rt_0 = 100; + kn_0 = 0.125; + cbe_1 = 1; + sf_1 = 200.0; + ce_1 = 0; + cbe_2 = 0; + ce_2 = 0; + cbe_3 = 0; + ce_3 = 0; + cbe_4 = 0; + ce_4 = 0; + cbe_5 = 0; + ce_5 = 0; + cbe_6 = 0; + ce_6 = 0; + }; + } + { + type = "lv2"; + plugin = "http://lsp-plug.in/plugins/lv2/compressor_stereo"; + name = "woofer_lim"; + control = { + sla = 5.0; + al = 1.0; + at = 1.0; + rt = 100.0; + cr = 15.0; + kn = 0.5; + }; + } + ]; + + # Now, we're chaining together the modules instantiated above. + links = [ + { + output = "bassex:out_l"; + input = "el:in_l"; + } + { + output = "bassex:out_r"; + input = "el:in_r"; + } + + { + output = "el:out_l"; + input = "fw13eq:in_l"; + } + { + output = "el:out_r"; + input = "fw13eq:in_r"; + } + { + output = "fw13eq:out_l"; + input = "woofer_bp:in_l"; + } + { + output = "fw13eq:out_r"; + input = "woofer_bp:in_r"; + } + { + output = "woofer_bp:out_l"; + input = "woofer_lim:in_l"; + } + { + output = "woofer_bp:out_r"; + input = "woofer_lim:in_r"; + } + ]; + + inputs = [ + "bassex:in_l" + "bassex:in_r" + ]; + outputs = [ + "woofer_lim:out_l" + "woofer_lim:out_r" + ]; + + # This makes pipewire's volume control actually control the loudness comp module + "capture.volumes" = [ + { + control = "el:volume"; + min = -47.5; + max = 0.0; + scale = "cubic"; + } + ]; + }; + "capture.props" = { + "node.name" = "audio_effect.laptop-convolver"; + "media.class" = "Audio/Sink"; + "audio.channels" = "2"; + "audio.position" = [ + "FL" + "FR" + ]; + "audio.allowed-rates" = [ + 44100 + 48000 + 88200 + 96000 + 176400 + 192000 + ]; + "device.api" = "dsp"; + "node.virtual" = "false"; + + # Lower seems to mean "more preferred", + # bluetooth devices seem to be ~1000, speakers seem to be ~2000 + # since this is between the two, bluetooth devices take over when they connect, + # and hand over to this instead of the speakers when they disconnect. + "priority.session" = 1500; + "priority.driver" = 1500; + "state.default-volume" = 0.343; + "device.icon-name" = "audio-card-analog-pci"; + }; + "playback.props" = { + "node.name" = "audio_effect.laptop-convolver"; + "target.object" = outputName; + "node.passive" = "true"; + "audio.channels" = "2"; + "audio.allowed-rates" = [ + 44100 + 48000 + 88200 + 96000 + 176400 + 192000 + ]; + "audio.position" = [ + "FL" + "FR" + ]; + "device.icon-name" = "audio-card-analog-pci"; + }; + }; + + configPackage = + (pkgs.writeTextDir "share/wireplumber/wireplumber.conf.d/99-laptop.conf" '' + monitor.alsa.rules = [ + { + matches = [{ node.name = "${outputName}" }] + actions = { + update-props = { + audio.allowed-rates = [44100, 48000, 88200, 96000, 176400, 192000] + } + } + } + ] + + node.software-dsp.rules = [ + { + matches = [{ node.name = "${outputName}" }] + actions = { + create-filter = { + filter-path = "${filter-chain}" + hide-parent = ${boolToString cfg.hideRawDevice} + } + } + } + ] + + wireplumber.profiles = { + main = { node.software-dsp = "required" } + } + '') + // { + passthru.requiredLv2Packages = with pkgs; [ + lsp-plugins + bankstown-lv2 + ]; + }; + in + { + services.pipewire.wireplumber.configPackages = [ configPackage ]; + + # Pipewire is needed for this. + services.pipewire.enable = mkDefault true; + } + ); +} diff --git a/gpd/duo/duo-specific/bluetooth.nix b/gpd/duo/duo-specific/bluetooth.nix new file mode 100644 index 00000000..bdc530b9 --- /dev/null +++ b/gpd/duo/duo-specific/bluetooth.nix @@ -0,0 +1,56 @@ +# There is apparently a bug that affects Framework computers that causes black +# screen on resume from sleep or hibernate with kernel version 6.11. Framework +# have published a workaround; this applies that workaround. +# +# https://fosstodon.org/@frameworkcomputer/113406887743149089 +# https://github.com/FrameworkComputer/linux-docs/blob/main/hibernation/kernel-6-11-workarounds/suspend-hibernate-bluetooth-workaround.md#workaround-for-suspendhibernate-black-screen-on-resume-kernel-611 +{ + config, + lib, + pkgs, + ... + # TODO: drop this if linux 6.11 goes EOL +}: +with lib; +mkIf + ( + (config.boot.kernelPackages.kernelAtLeast "6.11") && (config.boot.kernelPackages.kernelOlder "6.12") + ) + { + systemd.services = { + bluetooth-rfkill-suspend = { + description = "Soft block Bluetooth on suspend/hibernate"; + before = [ "sleep.target" ]; + unitConfig.StopWhenUnneeded = true; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.util-linux}/bin/rfkill block bluetooth"; + ExecStartPost = "${pkgs.coreutils}/bin/sleep 3"; + RemainAfterExit = true; + }; + wantedBy = [ + "suspend.target" + "hibernate.target" + "suspend-then-hibernate.target" + ]; + }; + + bluetooth-rfkill-resume = { + description = "Unblock Bluetooth on resume"; + after = [ + "suspend.target" + "hibernate.target" + "suspend-then-hibernate.target" + ]; + serviceConfig = { + Type = "oneshot"; + ExecStart = "${pkgs.util-linux}/bin/rfkill unblock bluetooth"; + }; + wantedBy = [ + "suspend.target" + "hibernate.target" + "suspend-then-hibernate.target" + ]; + }; + }; + } diff --git a/gpd/duo/duo-specific/default.nix b/gpd/duo/duo-specific/default.nix new file mode 100644 index 00000000..2b5eaef3 --- /dev/null +++ b/gpd/duo/duo-specific/default.nix @@ -0,0 +1,20 @@ +{ lib, pkgs, ... }: +with lib; +{ + imports = [ + ../../../common/cpu/amd/raphael/igpu.nix + ./bluetooth.nix + ./amd.nix + ./audio.nix + ./power + ]; + + # Fix TRRS headphones missing a mic + # https://community.frame.work/t/headset-microphone-on-linux/12387/3 + boot.extraModprobeConfig = mkIf (versionOlder pkgs.linux.version "6.6.8") '' + options snd-hda-intel model=dell-headset-multi + ''; + + # Needed for desktop environments to detect/manage display brightness + hardware.sensor.iio.enable = mkDefault true; +} diff --git a/gpd/duo/duo-specific/power/default.nix b/gpd/duo/duo-specific/power/default.nix new file mode 100644 index 00000000..814ae900 --- /dev/null +++ b/gpd/duo/duo-specific/power/default.nix @@ -0,0 +1,6 @@ +{ + imports = [ + ../../../ppt.nix + ./power-saving.nix + ]; +} diff --git a/gpd/duo/duo-specific/power/power-saving.nix b/gpd/duo/duo-specific/power/power-saving.nix new file mode 100644 index 00000000..9c534040 --- /dev/null +++ b/gpd/duo/duo-specific/power/power-saving.nix @@ -0,0 +1,18 @@ +{ lib, config, ... }: +with lib; +let + cfg = config.hardware.gpd.duo.powerManagement; +in +{ + options.hardware.gpd.duo.powerManagement = { + enable = mkEnableOption "Enable power-profiles-daemon and disable TLP for the GPD Duo" // { + # Default increase PPT to the BIOS default when power adapter plugin to increase performance. + default = true; + }; + }; + + config = mkIf cfg.enable { + services.power-profiles-daemon.enable = mkDefault true; + services.tlp.enable = mkForce false; + }; +} diff --git a/gpd/pocket-4/default.nix b/gpd/pocket-4/default.nix index cb44fe65..fbd5abee 100644 --- a/gpd/pocket-4/default.nix +++ b/gpd/pocket-4/default.nix @@ -10,6 +10,7 @@ in ../../common/cpu/amd/pstate.nix ../../common/gpu/amd ../../common/hidpi.nix + ../ppt.nix ]; boot = { diff --git a/gpd/ppt.nix b/gpd/ppt.nix new file mode 100644 index 00000000..ffbf2a56 --- /dev/null +++ b/gpd/ppt.nix @@ -0,0 +1,66 @@ +{ + config, + lib, + pkgs, + ... +}: +with lib; +let + cfg = config.hardware.gpd.ppt; +in +{ + # Linux default PPT is 24-22-22, BIOS default PPT is 35-32-28. It can be controlled by ryzenadj. + + # NOTICE: Whenever you can limit PPT to 15W by pressing Fn + Shift to enter quiet mode. + + options.hardware.gpd.ppt = { + enable = mkEnableOption "Enable PPT control for device by ryzenadj." // { + # Default increase PPT to the BIOS default when power adapter plugin to increase performance. + default = true; + }; + + adapter = { + fast-limit = mkOption { + description = "Fast PTT Limit(milliwatt) when power adapter plugin."; + default = 35000; + type = types.ints.unsigned; + }; + slow-limit = mkOption { + description = "Slow PTT Limit(milliwatt) when power adapter plugin."; + default = 32000; + type = types.ints.unsigned; + }; + stapm-limit = mkOption { + description = "Stapm PTT Limit(milliwatt) when power adapter plugin."; + default = 28000; + type = types.ints.unsigned; + }; + }; + + battery = { + fast-limit = mkOption { + description = "Fast PTT Limit(milliwatt) when using battery."; + default = 24000; + type = types.ints.unsigned; + }; + slow-limit = mkOption { + description = "Slow PTT Limit(milliwatt) when using battery."; + default = 22000; + type = types.ints.unsigned; + }; + stapm-limit = mkOption { + description = "Stapm PTT Limit(milliwatt) when using battery."; + default = 22000; + type = types.ints.unsigned; + }; + }; + }; + + config = mkIf cfg.enable { + environment.systemPackages = [ pkgs.ryzenadj ]; + services.udev.extraRules = '' + SUBSYSTEM=="power_supply", KERNEL=="ADP1", ATTR{online}=="1", RUN+="${getExe pkgs.ryzenadj} --stapm-limit ${toString cfg.adapter.stapm-limit} --fast-limit ${toString cfg.adapter.fast-limit} --slow-limit ${toString cfg.adapter.slow-limit}" + SUBSYSTEM=="power_supply", KERNEL=="ADP1", ATTR{online}=="0", RUN+="${getExe pkgs.ryzenadj} --stapm-limit ${toString cfg.battery.stapm-limit} --fast-limit ${toString cfg.battery.fast-limit} --slow-limit ${toString cfg.battery.slow-limit}" + ''; + }; +} diff --git a/gpd/win-max-2/default.nix b/gpd/win-max-2/default.nix index c9762cd6..47e2dbe6 100644 --- a/gpd/win-max-2/default.nix +++ b/gpd/win-max-2/default.nix @@ -1,72 +1,8 @@ -{ - config, - lib, - pkgs, - ... -}: -with lib; -let - cfg = config.hardware.gpd.ppt; -in -{ +{ ... }: { imports = [ ../../common/pc/laptop ../../common/pc/ssd ../../common/hidpi.nix + ../ppt.nix ]; - - # Linux default PPT is 24-22-22, BIOS default PPT is 35-32-28. It can be controlled by ryzenadj. - - # NOTICE: Whenever you can limit PPT to 15W by pressing Fn + Shift to enter quiet mode. - - options.hardware.gpd.ppt = { - enable = mkEnableOption "Enable PPT control for device by ryzenadj." // { - # Default increase PPT to the BIOS default when power adapter plugin to increase performance. - default = true; - }; - - adapter = { - fast-limit = mkOption { - description = "Fast PTT Limit(milliwatt) when power adapter plugin."; - default = 35000; - type = types.ints.unsigned; - }; - slow-limit = mkOption { - description = "Slow PTT Limit(milliwatt) when power adapter plugin."; - default = 32000; - type = types.ints.unsigned; - }; - stapm-limit = mkOption { - description = "Stapm PTT Limit(milliwatt) when power adapter plugin."; - default = 28000; - type = types.ints.unsigned; - }; - }; - - battery = { - fast-limit = mkOption { - description = "Fast PTT Limit(milliwatt) when using battery."; - default = 24000; - type = types.ints.unsigned; - }; - slow-limit = mkOption { - description = "Slow PTT Limit(milliwatt) when using battery."; - default = 22000; - type = types.ints.unsigned; - }; - stapm-limit = mkOption { - description = "Stapm PTT Limit(milliwatt) when using battery."; - default = 22000; - type = types.ints.unsigned; - }; - }; - }; - - config = mkIf cfg.enable { - environment.systemPackages = [ pkgs.ryzenadj ]; - services.udev.extraRules = '' - SUBSYSTEM=="power_supply", KERNEL=="ADP1", ATTR{online}=="1", RUN+="${pkgs.ryzenadj}/bin/ryzenadj --stapm-limit ${toString cfg.adapter.stapm-limit} --fast-limit ${toString cfg.adapter.fast-limit} --slow-limit ${toString cfg.adapter.slow-limit}" - SUBSYSTEM=="power_supply", KERNEL=="ADP1", ATTR{online}=="0", RUN+="${pkgs.ryzenadj}/bin/ryzenadj --stapm-limit ${toString cfg.battery.stapm-limit} --fast-limit ${toString cfg.battery.fast-limit} --slow-limit ${toString cfg.battery.slow-limit}" - ''; - }; } diff --git a/gpd/win-mini/2024/default.nix b/gpd/win-mini/2024/default.nix index c8eb1bfd..6f93e46c 100644 --- a/gpd/win-mini/2024/default.nix +++ b/gpd/win-mini/2024/default.nix @@ -6,6 +6,7 @@ with lib; ../../../common/cpu/amd ../../../common/cpu/amd/pstate.nix ../../../common/gpu/amd + ../../ppt.nix ]; hardware.bluetooth.enable = lib.mkDefault true; diff --git a/intel/nuc/8i7beh/default.nix b/intel/nuc/8i7beh/default.nix index 5dfed4ef..22b15b6a 100644 --- a/intel/nuc/8i7beh/default.nix +++ b/intel/nuc/8i7beh/default.nix @@ -2,7 +2,9 @@ { imports = [ - ../../../common/cpu/intel + ../../../common/cpu/intel/coffee-lake + ../../../common/pc + ../../../common/pc/ssd ]; services.thermald.enable = lib.mkDefault true; diff --git a/lenovo/thinkpad/p1/default.nix b/lenovo/thinkpad/p1/default.nix index 197bfab2..75c0abee 100644 --- a/lenovo/thinkpad/p1/default.nix +++ b/lenovo/thinkpad/p1/default.nix @@ -1,9 +1,6 @@ { imports = [ ../../../common/cpu/intel - # might need nvidia module but we don't know the PCI ids: - # https://github.com/NixOS/nixos-hardware/pull/274#discussion_r650483740 - #../../../common/gpu/nvidia/prime.nix ../../../common/pc/ssd ]; diff --git a/lenovo/thinkpad/p1/gen5/README.md b/lenovo/thinkpad/p1/gen5/README.md new file mode 100644 index 00000000..41d2aaf4 --- /dev/null +++ b/lenovo/thinkpad/p1/gen5/README.md @@ -0,0 +1,68 @@ +# Lenovo ThinkPad P1 Gen 5 + +https://www.lenovo.com/us/en/p/laptops/thinkpad/thinkpadp/thinkpad-p1-gen-5-16-inch-intel/len101t0033 + +Tested on a P1 Gen 5 with Intel Iris Xe (Alder Lake) graphics and an +NVIDIA GeForce RTX 3080 Ti Mobile (Ampere) discrete GPU. + +- `lenovo-thinkpad-p1-gen5` — Alder Lake base; use on iGPU-only units. +- `lenovo-thinkpad-p1-gen5-nvidia` — adds NVIDIA PRIME **offload**. Run a + program on the discrete GPU with `nvidia-offload `. + +## Power management + +The `-nvidia` profile enables PRIME **offload** but does not turn on fine-grained +runtime power management, so the discrete GPU stays powered even when idle — +noticeable battery drain on a laptop. That setting is experimental and depends on +the GPU + firmware supporting PCIe Runtime D3 (RTD3), so it is left to the user. +If your unit supports it, let the dGPU suspend when unused by adding to your own +configuration: + +```nix +hardware.nvidia.powerManagement.finegrained = true; +``` + +## Tested Hardware + +```shell +lspci -nn +00:00.0 Host bridge [0600]: Intel Corporation 12th Gen Core Processor Host Bridge/DRAM Registers [8086:4641] (rev 02) +00:01.0 PCI bridge [0604]: Intel Corporation 12th Gen Core Processor PCI Express x16 Controller #1 [8086:460d] (rev 02) +00:02.0 VGA compatible controller [0300]: Intel Corporation Alder Lake-P GT2 [Iris Xe Graphics] [8086:46a6] (rev 0c) +00:04.0 Signal processing controller [1180]: Intel Corporation Alder Lake Innovation Platform Framework Processor Participant [8086:461d] (rev 02) +00:06.0 PCI bridge [0604]: Intel Corporation 12th Gen Core Processor PCI Express x4 Controller #0 [8086:464d] (rev 02) +00:08.0 System peripheral [0880]: Intel Corporation 12th Gen Core Processor Gaussian & Neural Accelerator [8086:464f] (rev 02) +00:0a.0 Signal processing controller [1180]: Intel Corporation Platform Monitoring Technology [8086:467d] (rev 01) +00:14.0 USB controller [0c03]: Intel Corporation Alder Lake PCH USB 3.2 xHCI Host Controller [8086:51ed] (rev 01) +00:14.2 RAM memory [0500]: Intel Corporation Alder Lake PCH Shared SRAM [8086:51ef] (rev 01) +00:14.3 Network controller [0280]: Intel Corporation Alder Lake-P PCH CNVi WiFi [8086:51f0] (rev 01) +00:15.0 Serial bus controller [0c80]: Intel Corporation Alder Lake PCH Serial IO I2C Controller #0 [8086:51e8] (rev 01) +00:16.0 Communication controller [0780]: Intel Corporation Alder Lake PCH HECI Controller [8086:51e0] (rev 01) +00:16.3 Serial controller [0700]: Intel Corporation Alder Lake AMT SOL Redirection [8086:51e3] (rev 01) +00:1c.0 PCI bridge [0604]: Intel Corporation Device [8086:51b8] (rev 01) +00:1c.7 PCI bridge [0604]: Intel Corporation Alder Lake PCH-P PCI Express Root Port #9 [8086:51bf] (rev 01) +00:1d.0 PCI bridge [0604]: Intel Corporation Alder Lake PCI Express Root Port #9 [8086:51b0] (rev 01) +00:1f.0 ISA bridge [0601]: Intel Corporation Alder Lake PCH eSPI Controller [8086:5182] (rev 01) +00:1f.3 Multimedia audio controller [0401]: Intel Corporation Alder Lake PCH-P High Definition Audio Controller [8086:51c8] (rev 01) +00:1f.4 SMBus [0c05]: Intel Corporation Alder Lake PCH-P SMBus Host Controller [8086:51a3] (rev 01) +00:1f.5 Serial bus controller [0c80]: Intel Corporation Alder Lake-P PCH SPI Controller [8086:51a4] (rev 01) +01:00.0 VGA compatible controller [0300]: NVIDIA Corporation GA103M [GeForce RTX 3080 Ti Mobile] [10de:2420] (rev a1) +01:00.1 Audio device [0403]: NVIDIA Corporation Device [10de:2288] (rev a1) +04:00.0 Non-Volatile memory controller [0108]: SK hynix Gold P31/BC711/PC711 NVMe Solid State Drive [1c5c:174a] +0a:00.0 Unassigned class [ff00]: Realtek Semiconductor Co., Ltd. RTS5261 PCIe SD Express Card Reader controller [10ec:5261] (rev 01) +20:00.0 PCI bridge [0604]: Intel Corporation Thunderbolt 4 Bridge [Maple Ridge 4C 2020] [8086:1136] (rev 02) +21:00.0 PCI bridge [0604]: Intel Corporation Thunderbolt 4 Bridge [Maple Ridge 4C 2020] [8086:1136] (rev 02) +21:01.0 PCI bridge [0604]: Intel Corporation Thunderbolt 4 Bridge [Maple Ridge 4C 2020] [8086:1136] (rev 02) +21:02.0 PCI bridge [0604]: Intel Corporation Thunderbolt 4 Bridge [Maple Ridge 4C 2020] [8086:1136] (rev 02) +21:03.0 PCI bridge [0604]: Intel Corporation Thunderbolt 4 Bridge [Maple Ridge 4C 2020] [8086:1136] (rev 02) +22:00.0 USB controller [0c03]: Intel Corporation Thunderbolt 4 NHI [Maple Ridge 4C 2020] [8086:1137] +56:00.0 USB controller [0c03]: Intel Corporation Thunderbolt 4 USB Controller [Maple Ridge 4C 2020] [8086:1138] + +nix-info -m + - system: `"x86_64-linux"` + - host os: `Linux 7.0.11, NixOS, 26.05 (Yarara), 26.05.20260603.6b31628` + - multi-user?: `yes` + - sandbox: `yes` + - version: `nix-env (Nix) 2.34.7` + - nixpkgs: `/nix/store/snzg7swkp0vsd2qch9izsbmxnyb13ca0-source` +``` diff --git a/lenovo/thinkpad/p1/gen5/default.nix b/lenovo/thinkpad/p1/gen5/default.nix new file mode 100644 index 00000000..1c9661fc --- /dev/null +++ b/lenovo/thinkpad/p1/gen5/default.nix @@ -0,0 +1,7 @@ +{ ... }: +{ + imports = [ + ../. + ../../../../common/cpu/intel/alder-lake # P1 Gen 5 is 12th-gen Alder Lake + ]; +} diff --git a/lenovo/thinkpad/p1/gen5/nvidia/default.nix b/lenovo/thinkpad/p1/gen5/nvidia/default.nix new file mode 100644 index 00000000..4b588292 --- /dev/null +++ b/lenovo/thinkpad/p1/gen5/nvidia/default.nix @@ -0,0 +1,26 @@ +{ lib, ... }: +{ + imports = [ + ../. + ../../../../../common/gpu/nvidia/ampere + ../../../../../common/gpu/nvidia/prime.nix + ]; + + hardware = { + graphics = { + enable = lib.mkDefault true; + enable32Bit = lib.mkDefault true; + }; + nvidia = { + # "Wayland requires kernel mode setting (KMS) to be enabled (Highly + # Recommended)" per https://wiki.nixos.org/wiki/NVIDIA#Requirements. + modesetting.enable = lib.mkDefault true; + prime = { + # Bus ID of the Intel GPU. + intelBusId = lib.mkDefault "PCI:0:2:0"; + # Bus ID of the NVIDIA GPU. + nvidiaBusId = lib.mkDefault "PCI:1:0:0"; + }; + }; + }; +} diff --git a/lenovo/thinkpad/p14s/amd/gen6/default.nix b/lenovo/thinkpad/p14s/amd/gen6/default.nix index f417797b..4d6ba518 100644 --- a/lenovo/thinkpad/p14s/amd/gen6/default.nix +++ b/lenovo/thinkpad/p14s/amd/gen6/default.nix @@ -5,4 +5,8 @@ ../. ../../../../../common/cpu/amd/pstate.nix ]; + + # Prevent the integrated RGB camera from dying during suspend. + # This issue was observed on Linux 6.18.38. + boot.kernelParams = [ "usbcore.quirks=30c9:00f4:b" ]; } diff --git a/lenovo/thinkpad/x1-extreme/gen2/default.nix b/lenovo/thinkpad/x1-extreme/gen2/default.nix index 17147b53..25023300 100644 --- a/lenovo/thinkpad/x1-extreme/gen2/default.nix +++ b/lenovo/thinkpad/x1-extreme/gen2/default.nix @@ -5,6 +5,7 @@ with lib; { imports = [ ../. + ../../../../common/cpu/intel/coffee-lake ]; # Fixes an issue with incorrect battery reporting. See @@ -15,6 +16,9 @@ with lib; # See also https://certification.ubuntu.com/catalog/component/input/5313/input%3ATPPS/2ElanTrackPoint/ hardware.trackpoint.device = "TPPS/2 Elan TrackPoint"; + # Fix clickpad (clicking by depressing the touchpad). + boot.kernelParams = [ "psmouse.synaptics_intertouch=0" ]; + # Since the HDMI port is connected to the NVIDIA card. hardware.bumblebee.connectDisplay = true; diff --git a/lenovo/thinkpad/x1/yoga/8th-gen/README.md b/lenovo/thinkpad/x1/yoga/8th-gen/README.md new file mode 100644 index 00000000..db725aa6 --- /dev/null +++ b/lenovo/thinkpad/x1/yoga/8th-gen/README.md @@ -0,0 +1,20 @@ +# Lenovo ThinkPad X1 Yoga Gen 8 + +## Camera + +The X1 Yoga Gen 8 ships with one of two cameras, and each needs a +different profile. + +| Camera | Check | Profile | +|-------------------------|-----------------------------------|----------------------------------------| +| USB (UVC) | `lsusb` lists "Integrated Camera" | `lenovo-thinkpad-x1-yoga-8th-gen` | +| MIPI sensor behind IPU6 | `lsmod` lists `intel_ipu6` | `lenovo-thinkpad-x1-yoga-8th-gen-ipu6` | + +The USB camera works out of the box. + +The MIPI camera works through libcamera without any extra configuration, +so browsers and GNOME can already use it through PipeWire. Applications +that speak plain V4L2, such as Zoom, can only see raw Bayer nodes and +report that there's no camera. The `ipu6` profile enables +`hardware.ipu6`, which relays the sensor to a regular V4L2 device. That +pulls in Intel's proprietary camera HAL and firmware. diff --git a/lenovo/thinkpad/x1/yoga/8th-gen/ipu6/default.nix b/lenovo/thinkpad/x1/yoga/8th-gen/ipu6/default.nix new file mode 100644 index 00000000..81658c2d --- /dev/null +++ b/lenovo/thinkpad/x1/yoga/8th-gen/ipu6/default.nix @@ -0,0 +1,9 @@ +{ lib, ... }: +{ + imports = [ ../. ]; + + hardware.ipu6 = { + enable = lib.mkDefault true; + platform = lib.mkDefault "ipu6ep"; + }; +} diff --git a/mnt/reform/updateKernelPatches.nix b/mnt/reform/updateKernelPatches.nix index e30bd6f9..2bffbec6 100644 --- a/mnt/reform/updateKernelPatches.nix +++ b/mnt/reform/updateKernelPatches.nix @@ -1,4 +1,5 @@ { + runCommand, lib, fetchFromGitLab, }: @@ -7,6 +8,8 @@ let sources = lib.importJSON ./sources.json; reformDebianPackages = fetchFromGitLab sources.reformDebianPackages; in -map (lib.removePrefix "${reformDebianPackages}/") ( - lib.filesystem.listFilesRecursive "${reformDebianPackages}/linux/patches${lib.versions.majorMinor sources.modDirVersion}" -) +runCommand "mnt-reform-kernel-patches" { } '' + shopt -s globstar + cd ${reformDebianPackages} + echo -e "[\n$(printf ' "%s"\n' linux/patches${lib.versions.majorMinor sources.modDirVersion}/**/*.patch)\n]"> $out +'' diff --git a/mnt/reform/updateKernelPatches.sh b/mnt/reform/updateKernelPatches.sh index 79581c7d..8475acc0 100755 --- a/mnt/reform/updateKernelPatches.sh +++ b/mnt/reform/updateKernelPatches.sh @@ -1,4 +1,4 @@ #!/usr/bin/env sh -nix eval .#mnt-reform-kernel-patches > kernelPatches.nix -nix fmt kernelPatches.nix +nix build .#mnt-reform-kernel-patches +cp result kernelPatches.nix diff --git a/raspberry-pi/2/README.org b/raspberry-pi/2/README.org deleted file mode 100644 index 56384537..00000000 --- a/raspberry-pi/2/README.org +++ /dev/null @@ -1,10 +0,0 @@ -** Status - -The code in master as of June 2015 should be able to prepare a bootable NixOS -for Raspberry PI 2. - -There are still some drawbacks: - -NixOS does not provide a /boot/config.txt (the FAT32 partition). Making NixOS -work in the Raspberry PI 2 is mainly the result of the recent work of ambro718, -Dezgeg and viric (#nixos@irc.freenode.net). diff --git a/raspberry-pi/3/default.nix b/raspberry-pi/3/default.nix index 15dfee8d..271bc59f 100644 --- a/raspberry-pi/3/default.nix +++ b/raspberry-pi/3/default.nix @@ -35,4 +35,5 @@ "console=tty0" ]; + hardware.firmware = [ (pkgs.callPackage ../common/raspberry-pi-wireless-firmware.nix { }) ]; } diff --git a/raspberry-pi/4/digi-amp-plus.nix b/raspberry-pi/4/digi-amp-plus.nix index f9a3a5b3..d2d37f23 100644 --- a/raspberry-pi/4/digi-amp-plus.nix +++ b/raspberry-pi/4/digi-amp-plus.nix @@ -1,91 +1,42 @@ -{ config, lib, ... }: +{ lib, ... }: -let - cfg = config.hardware.raspberry-pi."4".digi-amp-plus; -in { - options.hardware = { - raspberry-pi."4".digi-amp-plus = { - enable = lib.mkEnableOption "support for the IQaudIO DigiAMP+ Hat"; + imports = [ + (lib.mkRemovedOptionModule + [ + "hardware" + "raspberry-pi" + "4" + "digi-amp-plus" + ] + '' + Use the stock iqaudio-dacplus firmware overlay instead: - unmuteAmp = lib.mkOption { - type = lib.types.bool; - default = false; - description = '' - Whether to "one-shot" unmute when kernel module first loads. - ''; - }; + { + boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = + false; - autoMuteAmp = lib.mkOption { - type = lib.types.bool; - default = true; - description = '' - Unmute the amp when an ALSA device is opened by a client. Mute, with a five-second delay when the ALSA device is closed. - (Reopening the device within the five-second close window will cancel mute.) - ''; - }; - }; - }; - - config = lib.mkIf cfg.enable { - hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; - - hardware.deviceTree = { - overlays = [ - # Adapted from to: https://github.com/raspberrypi/linux/blob/3ed6d34d53e94ecbebc64c8fa3d1b6d3c41db8fb/arch/arm/boot/dts/overlays/iqaudio-dacplus-overlay.dts - # changes: - # - modified top-level "compatible" field from bcm2835 to bcm2711 - # - s/i2s_clk_producer/i2s/ (name on bcm2711 platform) - { - name = "iqaudio-digiampplus-overlay"; - dtsText = '' - /dts-v1/; - /plugin/; - - / { - compatible = "brcm,bcm2711"; - - fragment@0 { - target = <&i2s>; - __overlay__ { - status = "okay"; + hardware.raspberry-pi.configtxt.deviceTreeOverlays.pi4 = [ + { + iqaudio-dacplus = { + auto_mute_amp = true; + unmute_amp = false; }; - }; + } + ]; + } - fragment@1 { - target = <&i2c1>; - __overlay__ { - #address-cells = <1>; - #size-cells = <0>; - status = "okay"; + Keep the old boolean values. Map autoMuteAmp to auto_mute_amp and + unmuteAmp to unmute_amp. - pcm5122@4c { - #sound-dai-cells = <0>; - compatible = "ti,pcm5122"; - reg = <0x4c>; - AVDD-supply = <&vdd_3v3_reg>; - DVDD-supply = <&vdd_3v3_reg>; - CPVDD-supply = <&vdd_3v3_reg>; - status = "okay"; - }; - }; - }; + On a running system, set hardware.raspberry-pi.firmware.enable to true. - fragment@2 { - target = <&sound>; - iqaudio_dac: __overlay__ { - compatible = "iqaudio,iqaudio-dac"; - i2s-controller = <&i2s>; - mute-gpios = <&gpio 22 0>; - status = "okay"; - ${lib.optionalString cfg.unmuteAmp "iqaudio-dac,unmute-amp;"} - ${lib.optionalString cfg.autoMuteAmp "iqaudio-dac,auto-mute-amp;"} - }; - }; - }; - ''; - } - ]; - }; - }; + For NixOS-Hardware specific firmware setup, read: + raspberry-pi/README.md. + + For digiamp specific details, see: + https://www.raspberrypi.com/documentation/accessories/audio.html#raspberry-pi-digiamp + '' + ) + ]; } diff --git a/raspberry-pi/4/dwc2.nix b/raspberry-pi/4/dwc2.nix index 5d49f9a1..b0e1d49b 100644 --- a/raspberry-pi/4/dwc2.nix +++ b/raspberry-pi/4/dwc2.nix @@ -1,86 +1,34 @@ -{ config, lib, ... }: +{ lib, ... }: -let - cfg = config.hardware.raspberry-pi."4".dwc2; -in { - options.hardware = { - raspberry-pi."4".dwc2 = { - enable = lib.mkOption { - type = lib.types.bool; - default = false; - example = true; - description = '' - Enable the UDC controller to support USB OTG gadget functions. + imports = [ + (lib.mkRemovedOptionModule + [ + "hardware" + "raspberry-pi" + "4" + "dwc2" + ] + '' + Use the stock dwc2 firmware overlay instead: - In order to verify that this works, connect the Raspberry Pi with - another computer via the USB C cable, and then do one of: + { + boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = + false; - - `modprobe g_serial` - - `modprobe g_mass_storage file=/path/to/some/iso-file.iso` + hardware.raspberry-pi.configtxt.deviceTreeOverlays.pi4 = [ + { dwc2 = { }; } + ]; + } - On the Raspberry Pi, `dmesg` should then show success-indicating output - that is related to the dwc2 and g_serial/g_mass_storage modules. - On the other computer, a serial/mass-storage device should pop up in - the system logs. + If you set dr_mode on the old option, add it to the overlay. For + example: - For more information about what gadget functions exist along with handy - guides on how to test them, please refer to: - https://www.kernel.org/doc/Documentation/usb/gadget-testing.txt - ''; - }; - dr_mode = lib.mkOption { - type = lib.types.enum [ - "host" - "peripheral" - "otg" - ]; - default = "otg"; - description = '' - Dual role mode setting for the dwc2 USB controller driver. - ''; - }; - }; - }; + { dwc2.dr_mode = "host"; } - config = lib.mkIf cfg.enable { - # Configure for modesetting in the device tree - hardware.deviceTree = { - overlays = [ - # this *should* be equivalent to (which doesn't work): - # https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/dwc2-overlay.dts - # but actually it's obtained using - # dtc -I dtb -O dts ${config.hardware.deviceTree.kernelPackage}/dtbs/overlays/dwc2.dtbo - # (changes: modified top-level "compatible" field) - # which is slightly different and works - { - name = "dwc2-overlay"; - dtsText = '' - /dts-v1/; - /plugin/; - - / { - compatible = "brcm,bcm2711"; - - fragment@0 { - target = <&usb>; - #address-cells = <0x01>; - #size-cells = <0x01>; - - __overlay__ { - compatible = "brcm,bcm2835-usb"; - dr_mode = "${cfg.dr_mode}"; - g-np-tx-fifo-size = <0x20>; - g-rx-fifo-size = <0x22e>; - g-tx-fifo-size = <0x200 0x200 0x200 0x200 0x200 0x100 0x100>; - status = "okay"; - phandle = <0x01>; - }; - }; - }; - ''; - } - ]; - }; - }; + For CM4 and firmware-partition setup, see "DWC2 USB controller" in + raspberry-pi/README.md. + '' + ) + ]; } diff --git a/raspberry-pi/4/poe-hat.nix b/raspberry-pi/4/poe-hat.nix index ac2381e1..c40de143 100644 --- a/raspberry-pi/4/poe-hat.nix +++ b/raspberry-pi/4/poe-hat.nix @@ -1,232 +1,35 @@ -{ config, lib, ... }: +{ lib, ... }: -let - cfg = config.hardware.raspberry-pi."4".poe-hat; -in { - options.hardware = { - raspberry-pi."4".poe-hat = { - enable = lib.mkEnableOption "support for the Raspberry Pi POE Hat"; - fan = lib.mkOption { - type = lib.types.submodule { - options = { - temperature0 = lib.mkOption { - type = lib.types.int; - default = 40000; - description = "Temperature threshold 0."; - }; - hysteresis0 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 0."; - }; - temperature1 = lib.mkOption { - type = lib.types.int; - default = 45000; - description = "Temperature threshold 1."; - }; - hysteresis1 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 1."; - }; - temperature2 = lib.mkOption { - type = lib.types.int; - default = 50000; - description = "Temperature threshold 2."; - }; - hysteresis2 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 2."; - }; - temperature3 = lib.mkOption { - type = lib.types.int; - default = 55000; - description = "Temperature threshold 3."; - }; - hysteresis3 = lib.mkOption { - type = lib.types.int; - default = 5000; - description = "Temperature hysteresis 3."; - }; - }; - }; + imports = [ + (lib.mkRemovedOptionModule + [ + "hardware" + "raspberry-pi" + "4" + "poe-hat" + ] + '' + Use the stock rpi-poe firmware overlay instead: - default = { }; - description = "Fan configuration."; - }; - }; - }; - - config = lib.mkIf cfg.enable { - hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; - # doesn't work for the CM module, so we exclude e.g. bcm2711-rpi-cm4.dts - hardware.deviceTree.filter = "bcm2711-rpi-4*.dtb"; - - hardware.deviceTree = { - overlays = [ - # Equivalent to: https://github.com/raspberrypi/linux/blob/rpi-6.6.y/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts - { - name = "rpi-poe-overlay"; - dtsText = '' - /* - * Overlay for the Raspberry Pi POE HAT. - */ - /dts-v1/; - /plugin/; - - / { - compatible = "brcm,bcm2711"; - - fragment@0 { - target-path = "/"; - __overlay__ { - fan: pwm-fan { - compatible = "pwm-fan"; - cooling-levels = <0 1 10 100 255>; - #cooling-cells = <2>; - pwms = <&fwpwm 0 80000 0>; - }; - }; + hardware.raspberry-pi.configtxt.deviceTreeOverlays."board-type=0x11" = [ + { + rpi-poe = { + poe_fan_temp0 = 40000; + poe_fan_temp0_hyst = 2000; + poe_fan_temp1 = 45000; + poe_fan_temp1_hyst = 2000; + poe_fan_temp2 = 50000; + poe_fan_temp2_hyst = 2000; + poe_fan_temp3 = 55000; + poe_fan_temp3_hyst = 5000; }; + } + ]; - fragment@1 { - target = <&cpu_thermal>; - __overlay__ { - polling-delay = <2000>; /* milliseconds */ - }; - }; - - fragment@2 { - target = <&thermal_trips>; - __overlay__ { - trip0: trip0 { - temperature = <${toString cfg.fan.temperature0}>; - hysteresis = <${toString cfg.fan.hysteresis0}>; - type = "active"; - }; - trip1: trip1 { - temperature = <${toString cfg.fan.temperature1}>; - hysteresis = <${toString cfg.fan.hysteresis1}>; - type = "active"; - }; - trip2: trip2 { - temperature = <${toString cfg.fan.temperature2}>; - hysteresis = <${toString cfg.fan.hysteresis2}>; - type = "active"; - }; - trip3: trip3 { - temperature = <${toString cfg.fan.temperature3}>; - hysteresis = <${toString cfg.fan.hysteresis3}>; - type = "active"; - }; - }; - }; - - fragment@3 { - target = <&cooling_maps>; - __overlay__ { - map0 { - trip = <&trip0>; - cooling-device = <&fan 0 1>; - }; - map1 { - trip = <&trip1>; - cooling-device = <&fan 1 2>; - }; - map2 { - trip = <&trip2>; - cooling-device = <&fan 2 3>; - }; - map3 { - trip = <&trip3>; - cooling-device = <&fan 3 4>; - }; - }; - }; - - fragment@4 { - target-path = "/__overrides__"; - params: __overlay__ { - poe_fan_temp0 = <&trip0>,"temperature:0"; - poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; - poe_fan_temp1 = <&trip1>,"temperature:0"; - poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; - poe_fan_temp2 = <&trip2>,"temperature:0"; - poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; - poe_fan_temp3 = <&trip3>,"temperature:0"; - poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; - poe_fan_i2c = <&fwpwm>,"status=disabled", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - }; - - fragment@5 { - target = <&firmware>; - __overlay__ { - fwpwm: pwm { - compatible = "raspberrypi,firmware-poe-pwm"; - #pwm-cells = <2>; - }; - }; - }; - - fragment@6 { - target = <&i2c0>; - i2c_bus: __overlay__ { - #address-cells = <1>; - #size-cells = <0>; - - poe_mfd: poe@51 { - compatible = "raspberrypi,poe-core"; - reg = <0x51>; - status = "disabled"; - - poe_mfd_pwm: poe_pwm@f0 { - compatible = "raspberrypi,poe-pwm"; - reg = <0xf0>; - status = "okay"; - #pwm-cells = <2>; - }; - }; - }; - }; - - fragment@7 { - target = <&i2c0if>; - __dormant__ { - status = "okay"; - }; - }; - - fragment@8 { - target = <&i2c0mux>; - __dormant__ { - status = "okay"; - }; - }; - - __overrides__ { - poe_fan_temp0 = <&trip0>,"temperature:0"; - poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; - poe_fan_temp1 = <&trip1>,"temperature:0"; - poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; - poe_fan_temp2 = <&trip2>,"temperature:0"; - poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; - poe_fan_temp3 = <&trip3>,"temperature:0"; - poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; - i2c = <0>, "+5+6", - <&fwpwm>,"status=disabled", - <&i2c_bus>,"status=okay", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - }; - ''; - } - ]; - }; - }; + These are the overlay defaults. Parameters that were not customized can + be omitted. + '' + ) + ]; } diff --git a/raspberry-pi/4/poe-plus-hat.nix b/raspberry-pi/4/poe-plus-hat.nix index 59f91608..4403510e 100644 --- a/raspberry-pi/4/poe-plus-hat.nix +++ b/raspberry-pi/4/poe-plus-hat.nix @@ -1,282 +1,35 @@ -{ config, lib, ... }: +{ lib, ... }: -let - cfg = config.hardware.raspberry-pi."4".poe-plus-hat; -in { - options.hardware = { - raspberry-pi."4".poe-plus-hat = { - enable = lib.mkEnableOption "support for the Raspberry Pi PoE+ HAT"; - fan = lib.mkOption { - type = lib.types.submodule { - options = { - temperature0 = lib.mkOption { - type = lib.types.int; - default = 40000; - description = "Temperature threshold 0."; - }; - hysteresis0 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 0."; - }; - temperature1 = lib.mkOption { - type = lib.types.int; - default = 45000; - description = "Temperature threshold 1."; - }; - hysteresis1 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 1."; - }; - temperature2 = lib.mkOption { - type = lib.types.int; - default = 50000; - description = "Temperature threshold 2."; - }; - hysteresis2 = lib.mkOption { - type = lib.types.int; - default = 2000; - description = "Temperature hysteresis 2."; - }; - temperature3 = lib.mkOption { - type = lib.types.int; - default = 55000; - description = "Temperature threshold 3."; - }; - hysteresis3 = lib.mkOption { - type = lib.types.int; - default = 5000; - description = "Temperature hysteresis 3."; - }; - }; - }; + imports = [ + (lib.mkRemovedOptionModule + [ + "hardware" + "raspberry-pi" + "4" + "poe-plus-hat" + ] + '' + Use the stock rpi-poe-plus firmware overlay instead: - default = { }; - description = "Fan configuration."; - }; - }; - }; - - config = lib.mkIf cfg.enable { - hardware.raspberry-pi."4".apply-overlays-dtmerge.enable = lib.mkDefault true; - # doesn't work for the CM module, so we exclude e.g. bcm2711-rpi-cm4.dts - hardware.deviceTree.filter = "bcm2711-rpi-4*.dtb"; - - hardware.deviceTree = { - overlays = [ - # Combined equivalent to: - # * https://github.com/raspberrypi/linux/blob/rpi-6.6.y/arch/arm/boot/dts/overlays/rpi-poe-overlay.dts - # * https://github.com/raspberrypi/linux/blob/rpi-6.6.y/arch/arm/boot/dts/overlays/rpi-poe-plus-overlay.dts - { - name = "rpi-poe-plus-overlay"; - dtsText = '' - /* - * Overlay for the Raspberry Pi POE HAT. - */ - /dts-v1/; - /plugin/; - - / { - compatible = "brcm,bcm2711"; - - fragment@0 { - target-path = "/"; - __overlay__ { - fan: pwm-fan { - compatible = "pwm-fan"; - cooling-levels = <0 1 10 100 255>; - #cooling-cells = <2>; - pwms = <&fwpwm 0 80000 0>; - }; - }; + hardware.raspberry-pi.configtxt.deviceTreeOverlays."board-type=0x11" = [ + { + rpi-poe-plus = { + poe_fan_temp0 = 40000; + poe_fan_temp0_hyst = 2000; + poe_fan_temp1 = 45000; + poe_fan_temp1_hyst = 2000; + poe_fan_temp2 = 50000; + poe_fan_temp2_hyst = 2000; + poe_fan_temp3 = 55000; + poe_fan_temp3_hyst = 5000; }; + } + ]; - fragment@1 { - target = <&cpu_thermal>; - __overlay__ { - polling-delay = <2000>; /* milliseconds */ - }; - }; - - fragment@2 { - target = <&thermal_trips>; - __overlay__ { - trip0: trip0 { - temperature = <${toString cfg.fan.temperature0}>; - hysteresis = <${toString cfg.fan.hysteresis0}>; - type = "active"; - }; - trip1: trip1 { - temperature = <${toString cfg.fan.temperature1}>; - hysteresis = <${toString cfg.fan.hysteresis1}>; - type = "active"; - }; - trip2: trip2 { - temperature = <${toString cfg.fan.temperature2}>; - hysteresis = <${toString cfg.fan.hysteresis2}>; - type = "active"; - }; - trip3: trip3 { - temperature = <${toString cfg.fan.temperature3}>; - hysteresis = <${toString cfg.fan.hysteresis3}>; - type = "active"; - }; - }; - }; - - fragment@3 { - target = <&cooling_maps>; - __overlay__ { - map0 { - trip = <&trip0>; - cooling-device = <&fan 0 1>; - }; - map1 { - trip = <&trip1>; - cooling-device = <&fan 1 2>; - }; - map2 { - trip = <&trip2>; - cooling-device = <&fan 2 3>; - }; - map3 { - trip = <&trip3>; - cooling-device = <&fan 3 4>; - }; - }; - }; - - fragment@4 { - target-path = "/__overrides__"; - params: __overlay__ { - poe_fan_temp0 = <&trip0>,"temperature:0"; - poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; - poe_fan_temp1 = <&trip1>,"temperature:0"; - poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; - poe_fan_temp2 = <&trip2>,"temperature:0"; - poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; - poe_fan_temp3 = <&trip3>,"temperature:0"; - poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; - poe_fan_i2c = <&fwpwm>,"status=disabled", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - }; - - fragment@5 { - target = <&firmware>; - __overlay__ { - fwpwm: pwm { - compatible = "raspberrypi,firmware-poe-pwm"; - #pwm-cells = <2>; - }; - }; - }; - - fragment@6 { - target = <&i2c0>; - i2c_bus: __overlay__ { - #address-cells = <1>; - #size-cells = <0>; - - poe_mfd: poe@51 { - compatible = "raspberrypi,poe-core"; - reg = <0x51>; - status = "disabled"; - - poe_mfd_pwm: poe_pwm@f0 { - compatible = "raspberrypi,poe-pwm"; - reg = <0xf0>; - status = "okay"; - #pwm-cells = <2>; - }; - }; - }; - }; - - fragment@7 { - target = <&i2c0if>; - __dormant__ { - status = "okay"; - }; - }; - - fragment@8 { - target = <&i2c0mux>; - __dormant__ { - status = "okay"; - }; - }; - - __overrides__ { - poe_fan_temp0 = <&trip0>,"temperature:0"; - poe_fan_temp0_hyst = <&trip0>,"hysteresis:0"; - poe_fan_temp1 = <&trip1>,"temperature:0"; - poe_fan_temp1_hyst = <&trip1>,"hysteresis:0"; - poe_fan_temp2 = <&trip2>,"temperature:0"; - poe_fan_temp2_hyst = <&trip2>,"hysteresis:0"; - poe_fan_temp3 = <&trip3>,"temperature:0"; - poe_fan_temp3_hyst = <&trip3>,"hysteresis:0"; - i2c = <0>, "+5+6", - <&fwpwm>,"status=disabled", - <&i2c_bus>,"status=okay", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - }; - - // SPDX-License-Identifier: (GPL-2.0 OR MIT) - // Overlay for the Raspberry Pi PoE+ HAT. - - / { - compatible = "brcm,bcm2711"; - - fragment@10 { - target-path = "/"; - __overlay__ { - rpi_poe_power_supply: rpi-poe-power-supply { - compatible = "raspberrypi,rpi-poe-power-supply"; - firmware = <&firmware>; - status = "okay"; - }; - }; - }; - fragment@11 { - target = <&poe_mfd>; - __overlay__ { - rpi-poe-power-supply@f2 { - compatible = "raspberrypi,rpi-poe-power-supply"; - reg = <0xf2>; - status = "okay"; - }; - }; - }; - - __overrides__ { - i2c = <0>, "+5+6", - <&fwpwm>,"status=disabled", - <&rpi_poe_power_supply>,"status=disabled", - <&i2c_bus>,"status=okay", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - }; - - &fan { - cooling-levels = <0 32 64 128 255>; - }; - - ¶ms { - poe_fan_i2c = <&fwpwm>,"status=disabled", - <&rpi_poe_power_supply>,"status=disabled", - <&poe_mfd>,"status=okay", - <&fan>,"pwms:0=",<&poe_mfd_pwm>; - }; - ''; - } - ]; - }; - }; + These are the overlay defaults. Parameters that were not customized can + be omitted. + '' + ) + ]; } diff --git a/raspberry-pi/5/default.nix b/raspberry-pi/5/default.nix index c9d44bd0..435179df 100644 --- a/raspberry-pi/5/default.nix +++ b/raspberry-pi/5/default.nix @@ -49,4 +49,6 @@ in message = "The Raspberry Pi 5 requires a newer kernel version (>=6.1.54). Please upgrade nixpkgs for this system."; } ]; + + hardware.firmware = [ (pkgs.callPackage ../common/raspberry-pi-wireless-firmware.nix { }) ]; } diff --git a/raspberry-pi/README.md b/raspberry-pi/README.md index bf96213b..4d894b18 100644 --- a/raspberry-pi/README.md +++ b/raspberry-pi/README.md @@ -6,7 +6,7 @@ NixOS profiles and modules for Raspberry Pi boards. - `common/` has the shared bits: the `linux-rpi` kernel build (vendor defconfig, matching firmware), the `config.txt` generation module, a pinned wireless firmware, and the firmware-partition install module. - `2/`, `3/`, `4/`, `5/` are the board profiles. Each one picks the right kernel and kernel params. Pi 4 and 5 also set DT filters and the initrd modules they need. -- The extra files under `4/` are opt-in toggles for Pi 4 hardware: audio, dwc2, GPIO, I2C, LEDs, the PoE HATs, touchscreens, and so on. +- The extra files under `4/` are opt-in toggles for Pi 4 hardware: audio, GPIO, I2C, LEDs, touchscreens, and so on. ## Using a board profile @@ -49,11 +49,11 @@ Board profiles import `hardware.raspberry-pi.configtxt`, which renders `config.t { hardware.raspberry-pi.configtxt.settings = { all = { - dtparam = [ "audio=on" ]; - dtoverlay = [ - "vc4-kms-v3d" - "disable-bt" + dtparam = [ + "audio=on" + "i2c_arm=on" ]; + disable_overscan = true; }; pi5.arm_freq = 2400; cm4.otg_mode = true; @@ -61,14 +61,114 @@ Board profiles import `hardware.raspberry-pi.configtxt`, which renders `config.t } ``` -List values become repeated keys in the rendered file, so the `dtoverlay` above expands to: +List values become repeated keys in the rendered file, so the `dtparam` above expands to: -``` -dtoverlay=vc4-kms-v3d -dtoverlay=disable-bt +```ini +dtparam=audio=on +dtparam=i2c_arm=on ``` -Top-level attrs are conditional sections (`all`, `pi4`, `pi5`, `cm4`, and so on). Nesting stacks filters. To drop a default, set the key to `null` with `mkForce`. +Top-level attrs are conditional sections (`all`, `pi4`, `pi5`, `cm4`, and so on). Nesting stacks filters. Set a board profile's `mkDefault` value to `null` to remove it. Use `mkForce null` when overriding a value with normal priority. + +Every rendered group opens with `[all]` and then its own filters. Filters of different types stack rather than replace, so `[all]` is what clears the previous group before the next one starts. + +The firmware applies these filters at boot, not Nix at evaluation time. This matters in two cases. One image can boot on more than one board, such as a CM4 and a CM5 swapped into the same carrier. Filters like `[EDID=...]`, `[gpio4=1]`, `[tryboot]` and `[bootvar0=42]` also depend on state that Nix cannot see: the attached monitor, a jumper, an EEPROM value. + +### Device tree overlays + +Overlays go in `configtxt.deviceTreeOverlays`, not in a `dtoverlay` key under `settings`. Filters nest the same way, and the leaf is an ordered list where each element names one overlay and its parameters: + +```nix +{ + boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = false; + + hardware.raspberry-pi.configtxt.deviceTreeOverlays.pi4 = [ + { dwc2.dr_mode = "host"; } + { + gpio-fan = { + gpiopin = 14; + temp = 80000; + }; + } + ]; +} +``` + +This renders after `configtxt.settings` as: + +```ini +[all] +[pi4] +dtoverlay=dwc2 +dtparam=dr_mode=host +dtoverlay= +dtoverlay=gpio-fan +dtparam=gpiopin=14 +dtparam=temp=80000 +dtoverlay= +``` + +A separate option is necessary because `settings` groups values by key. That grouping cannot keep an overlay next to the `dtparam` lines that belong to it. The order is functional. A `dtparam` applies to the overlay that loaded last, and the parameters of an overlay stay in scope only until the next overlay loads. The bare `dtoverlay=` line closes that scope, so later parameters apply to the base device tree. The order between overlays can also matter, because one overlay can build on another. + +Overlays render after `settings`. This order keeps base parameters such as `dtparam=audio=on` out of the scope of an overlay. An overlay can export a parameter with the same name as a base parameter. While that overlay is in scope, the firmware uses the parameter of the overlay. + +Each parameter becomes its own `dtparam` line rather than an addition to the `dtoverlay` line, which keeps them clear of the 98-character line limit. Booleans render as `on` or `off`. Use `null` with `mkForce` to remove a parameter. + +The module concatenates lists from separate modules, but the order is not the order of definition. If one overlay must load before another, set the order with `mkBefore` or `mkAfter`. + +The Raspberry Pi firmware applies these overlays before U-Boot starts. Set `boot.loader.generic-extlinux-compatible.useGenerationDeviceTree = false` so U-Boot keeps that device tree instead of loading one from the NixOS generation. Enabling `hardware.raspberry-pi.firmware.uboot.enable` sets this automatically. + +The firmware partition must contain the generated `config.txt` and stock overlays. SD image builds populate it automatically. On a running system, set `hardware.raspberry-pi.firmware.enable = true`. + +#### DWC2 USB controller + +Use the stock `dwc2` overlay to enable the USB 2.0 controller on the Pi 4B USB-C connector: + +```nix +{ + hardware.raspberry-pi.configtxt.deviceTreeOverlays."board-type=0x11" = [ + { dwc2 = { }; } + ]; +} +``` + +`board-type=0x11` matches the Pi 4B. The broader `pi4` filter also matches Pi 400, CM4, and CM4S. + +[Raspberry Pi OS sets `otg_mode=1` on CM4](https://github.com/RPi-Distro/pi-gen/blob/master/stage1/00-boot-files/files/config.txt#L39-L43), and [nixos-hardware sets the same default](./common/config-txt-defaults.nix). Set it to `null` before loading DWC2: + +```nix +{ + hardware.raspberry-pi.configtxt = { + settings.cm4.otg_mode = null; + deviceTreeOverlays.cm4 = [ + { dwc2 = { }; } + ]; + }; +} +``` + +The `cm4` filter matches CM4 only. Set `dwc2.dr_mode` to `host`, `peripheral`, or `otg` to override the overlay default. The overlay also accepts `g-rx-fifo-size` and `g-np-tx-fifo-size`. + +#### PoE HATs + +The original [PoE HAT](https://www.raspberrypi.com/products/poe-hat/) and the [PoE+ HAT](https://www.raspberrypi.com/products/poe-plus-hat/) use the stock `rpi-poe` and `rpi-poe-plus` overlays. Both HATs support the Pi 3B+ and Pi 4B. + +```nix +{ + hardware.raspberry-pi.configtxt.deviceTreeOverlays."board-type=0x11" = [ + { + rpi-poe = { + poe_fan_temp0 = 50000; + poe_fan_temp0_hyst = 2000; + }; + } + ]; +} +``` + +Use `board-type=0x0d` for the Pi 3B+ and `board-type=0x11` for the Pi 4B. Replace `rpi-poe` with `rpi-poe-plus` for the PoE+ HAT. All overlay parameters are optional. + +To supply your own file, set `configtxt.file`. The module then ignores `settings` and `deviceTreeOverlays`. ## Current limits diff --git a/raspberry-pi/common/config-txt-defaults.nix b/raspberry-pi/common/config-txt-defaults.nix index d52af85f..a184547c 100644 --- a/raspberry-pi/common/config-txt-defaults.nix +++ b/raspberry-pi/common/config-txt-defaults.nix @@ -8,30 +8,37 @@ { lib, ... }: { - hardware.raspberry-pi.configtxt.settings = { - all = { - camera_auto_detect = lib.mkDefault true; - display_auto_detect = lib.mkDefault true; - max_framebuffers = lib.mkDefault 2; - disable_fw_kms_setup = lib.mkDefault true; - disable_overscan = lib.mkDefault true; - arm_boost = lib.mkDefault true; - dtparam = lib.mkDefault [ "audio=on" ]; - dtoverlay = lib.mkDefault [ "vc4-kms-v3d" ]; - # U-Boot needs the UART enabled to boot (see arch/arm/mach-bcm283x/Kconfig - # in the U-Boot tree). Without it U-Boot hangs on the Pi 4. - enable_uart = lib.mkDefault true; + hardware.raspberry-pi.configtxt = { + settings = { + all = { + camera_auto_detect = lib.mkDefault true; + display_auto_detect = lib.mkDefault true; + max_framebuffers = lib.mkDefault 2; + disable_fw_kms_setup = lib.mkDefault true; + disable_overscan = lib.mkDefault true; + arm_boost = lib.mkDefault true; + # A base DTB parameter rather than an overlay one, so it stays here. It + # has to apply before any overlay loads: vc4-kms-v3d exports its own + # `audio` parameter, which would otherwise capture this line and set + # HDMI audio instead of enabling the onboard audio. + dtparam = lib.mkDefault [ "audio=on" ]; + # U-Boot needs the UART enabled to boot (see arch/arm/mach-bcm283x/Kconfig + # in the U-Boot tree). Without it U-Boot hangs on the Pi 4. + enable_uart = lib.mkDefault true; + }; + # The Pi 5 has a dedicated debug UART. Leaving the mini UART on feeds ghost + # input into boot, so turn it back off. + pi5 = { + enable_uart = lib.mkDefault false; + }; + cm4 = { + otg_mode = lib.mkDefault true; + }; }; - # The Pi 5 has a dedicated debug UART. Leaving the mini UART on feeds ghost - # input into boot, so turn it back off. - pi5 = { - enable_uart = lib.mkDefault false; - }; - cm4 = { - otg_mode = lib.mkDefault true; - }; - cm5 = { - dtoverlay = lib.mkDefault [ "dwc2,dr_mode=host" ]; + + deviceTreeOverlays = { + all = lib.mkDefault [ { vc4-kms-v3d = { }; } ]; + cm5 = lib.mkDefault [ { dwc2.dr_mode = "host"; } ]; }; }; } diff --git a/raspberry-pi/common/config-txt.nix b/raspberry-pi/common/config-txt.nix index a3b5569e..66501bf2 100644 --- a/raspberry-pi/common/config-txt.nix +++ b/raspberry-pi/common/config-txt.nix @@ -1,9 +1,13 @@ # config.txt generation module for Raspberry Pi # -# Generates config.txt from structured Nix options. The type system models -# config.txt as nested attrs: each nesting level adds a conditional filter, -# and leaves are config values. Repeated keys (dtparam, dtoverlay, gpio) -# are supported via lists. +# This module renders config.txt from structured Nix options. The type models +# config.txt as nested attrs: each level of nesting adds a conditional filter, +# and the leaves are config values. Lists produce repeated keys for dtparam, +# dtoverlay and gpio. +# +# Device tree overlays have their own option. A dtparam applies to the overlay +# that loaded last, and values grouped by key cannot express that order. +# Overlays render after settings, so base parameters apply first. # # Based on work from nvmd/nixos-raspberrypi (MIT License) and rendering # approach suggested by @quentinmit. Follows RFC 42. @@ -31,59 +35,175 @@ let else if false == v then "0" else - builtins.abort "config.txt: unsupported value type: ${builtins.typeOf v}"; + abort "config.txt: unsupported value type: ${builtins.typeOf v}"; mkKeyValue = lib.generators.mkKeyValueDefault { inherit mkValueString; } "="; - # Recursively flatten nested attrs into a list of { conditionals, name, value } records. - # Each nesting level (except the leaf) is a conditional filter. - # "all" filters are omitted from the conditionals list since they reset state. - # null values are filtered out, allowing `mkForce null` to remove defaults. - recurse = + # Overlay parameters use on/off for booleans. This follows the Raspberry Pi + # documentation and the overlay README. Plain settings keep 1/0. + mkParamValue = v: if builtins.isBool v then (if v then "on" else "off") else mkValueString v; + + # An "all" filter resets state rather than narrowing it, so it never has to be + # recorded. The rest are sorted, which makes two paths that stack the same + # filters in a different order compare equal. + mkConditionals = + path: + lib.pipe path [ + (lib.filter (k: k != "all")) + (lib.sort builtins.lessThan) + ]; + + # Flatten settings into { conditionals, items } groups, one group per leaf. + # Every level above the leaf is a conditional filter. A null value drops out, + # so `mkForce null` removes a default. + recurseSettings = path: value: if value == null then [ ] - else if builtins.isAttrs value && !(builtins.isList value) && !(value ? _type) then - lib.flatten (lib.mapAttrsToList (name: recurse ([ name ] ++ path)) value) + else if !(builtins.isAttrs value) then + [ + { + conditionals = mkConditionals (lib.init path); + items = [ + { + name = lib.last path; + inherit value; + } + ]; + } + ] else - { - conditionals = lib.sort builtins.lessThan (lib.filter (k: k != "all") (lib.tail path)); - name = lib.head path; - inherit value; - }; + lib.pipe value [ + (lib.mapAttrsToList (name: recurseSettings (path ++ [ name ]))) + lib.concatLists + ]; - # Group flattened items by their conditional filter set, then render. - groupItems = - items: - lib.mapAttrsToList (groupJSON: groupItems: { - conditionals = builtins.fromJSON groupJSON; - items = builtins.listToAttrs groupItems; - }) (builtins.groupBy (x: builtins.toJSON x.conditionals) items); + # The same flattening for overlays. Here the leaf is already the ordered + # overlay list for that filter set, so every level of nesting is a filter. + recurseOverlays = + path: value: + if value == null then + [ ] + else if builtins.isList value then + [ + { + conditionals = mkConditionals path; + items = value; + } + ] + else + lib.pipe value [ + (lib.mapAttrsToList (name: recurseOverlays (path ++ [ name ]))) + lib.concatLists + ]; - mkGroup = + # Merge groups that share a filter set. Two paths can reduce to the same set + # (all.pi4 and pi4.all both yield [ "pi4" ]), so merging is needed even though + # each recursion returns one group per leaf. + # + # Sorting first puts equal sets next to each other, so a single pass coalesces + # them. Folding from the right lets each step prepend, which keeps the result + # in order without a second pass. builtins.sort is stable, so items keep their + # original relative order, which overlays depend on. + # + # The sort is not redundant with mkConditionals, which only orders the filters + # within one group. Groups arrive in attrs walk order, driven by the raw names + # before "all" is stripped, so all.z.foo walks before b.bar. + groupByConditionals = + groups: + lib.pipe groups [ + (lib.sort (a: b: a.conditionals < b.conditionals)) + (lib.foldr ( + group: merged: + let + next = lib.head merged; + in + if merged != [ ] && next.conditionals == group.conditionals then + [ (group // { items = group.items ++ next.items; }) ] ++ lib.tail merged + else + [ group ] ++ merged + ) [ ]) + ]; + + # Every group opens with [all] plus its own filters. For settings that leading + # [all] is only a reset. For overlays it is required: a bare `dtoverlay=` ends + # overlay scope but leaves the conditional filters set, and only [all] clears + # them. + mkFilterHeader = conditionals: lib.concatMapStrings (f: "[${f}]\n") ([ "all" ] ++ conditionals); + + renderSettingsGroup = group: - lib.concatMapStrings (k: "[${k}]\n") group.conditionals - + lib.generators.toKeyValue { - inherit mkKeyValue; - listsAsDuplicateKeys = true; - } group.items; + mkFilterHeader group.conditionals + + lib.pipe group.items [ + builtins.listToAttrs + (lib.generators.toKeyValue { + inherit mkKeyValue; + listsAsDuplicateKeys = true; + }) + ]; - toConfigTxt = - attrs: - let - groups = lib.sort (a: b: a.conditionals < b.conditionals) ( - groupItems (lib.flatten (recurse [ ] attrs)) - ); - in - lib.concatMapStringsSep "\n[all]\n" mkGroup groups; + # Each entry ends with a bare `dtoverlay=`. This terminator closes the parameter + # scope of that overlay, so a later dtparam applies to the base DTB instead. A + # terminator must never come first in the file, where it means "load no HAT + # overlay". Settings render first, and every terminator here follows a + # dtoverlay=, so no terminator comes first today. A change to the section + # order can break this guarantee. + renderOverlayGroup = + group: + mkFilterHeader group.conditionals + + lib.concatMapStrings ( + entry: + let + name = lib.head (lib.attrNames entry); + in + lib.concatMapStrings (line: line + "\n") ( + [ "dtoverlay=${name}" ] + ++ lib.pipe entry.${name} [ + (lib.filterAttrs (_: v: v != null)) + (lib.mapAttrsToList (p: v: "dtparam=${p}=${mkParamValue v}")) + ] + ++ [ "dtoverlay=" ] + ) + ) group.items; + render = + conf: renderSectionFunc: recurseFunc: + lib.pipe conf [ + (recurseFunc [ ]) + groupByConditionals + (map renderSectionFunc) + (lib.concatStringsSep "\n") + ]; + + # Settings render first, so base DT parameters apply before any overlay loads. + # The overlay groups follow. A blank line separates the two sections, and the + # filter keeps that separator from becoming a stray leading or trailing line + # when one of the two is empty. + generatedConfig = + lib.pipe + [ + (render cfg.settings renderSettingsGroup recurseSettings) + (render cfg.deviceTreeOverlays renderOverlayGroup recurseOverlays) + ] + [ + (lib.filter (section: section != "")) + (lib.concatStringsSep "\n") + ]; in { options.hardware.raspberry-pi.configtxt = { settings = lib.mkOption { type = - with lib.types; let + inherit (lib.types) + attrsOf + bool + int + listOf + nullOr + oneOf + str + ; atom = nullOr (oneOf [ str int @@ -107,6 +227,10 @@ in Booleans render as `0`/`1`. Use `null` with `mkForce` to remove a default. + Set device tree overlays through {option}`deviceTreeOverlays` rather + than a `dtoverlay` key here. A `dtoverlay` written here gets no + terminator, so it captures every `dtparam` that follows it. + See ''; example = lib.literalExpression '' @@ -123,13 +247,95 @@ in ''; }; + deviceTreeOverlays = lib.mkOption { + type = + let + inherit (lib.types) + addCheck + attrsOf + bool + int + listOf + nullOr + oneOf + str + ; + params = attrsOf ( + nullOr (oneOf [ + str + int + bool + ]) + ); + # Each list element names one overlay. Without this check one element + # can name two overlays, as in [ { a = { }; b = { }; } ]. Nix sorts + # attribute names, so the order between them is lost. + entry = addCheck (attrsOf params) (e: builtins.length (builtins.attrNames e) == 1) // { + description = "attribute set naming exactly one overlay, mapped to its parameters"; + }; + node = oneOf [ + (listOf entry) + (attrsOf node) + ]; + in + attrsOf node // { description = "config.txt device tree overlay type"; }; + default = { }; + description = '' + Device tree overlays for the Raspberry Pi firmware, rendered in list + order. + + Keys are conditional filter sections and nest exactly like + {option}`settings`. The leaf is an ordered list, and each element names + one overlay mapped to its parameters. + + Use this instead of a `dtoverlay` key in {option}`settings` when an + overlay has parameters. `settings` groups values by key and cannot keep + an overlay next to the `dtparam` lines that belong to it. A parameter + stays in scope only until the next overlay loads. + + Each parameter becomes its own `dtparam` line rather than an addition to + the `dtoverlay` line, which keeps them clear of the 98-character limit + on `config.txt` lines. Booleans render as `on` or `off`. Use `null` with + `mkForce` to remove a default. + + Overlays render after {option}`settings`, so base device tree parameters + set there apply before any overlay loads. The example renders as: + + ```ini + [all] + [pi4] + dtoverlay=dwc2 + dtparam=dr_mode=host + dtoverlay= + dtoverlay=gpio-fan + dtparam=gpiopin=14 + dtparam=temp=80000 + dtoverlay= + ``` + ''; + example = lib.literalExpression '' + { + pi4 = [ + { dwc2.dr_mode = "host"; } + { + gpio-fan = { + gpiopin = 14; + temp = 80000; + }; + } + ]; + } + ''; + }; + file = lib.mkOption { type = lib.types.path; - default = pkgs.writeText "config.txt" (toConfigTxt cfg.settings); - defaultText = lib.literalExpression ''pkgs.writeText "config.txt" (generated from settings)''; + default = pkgs.writeText "config.txt" generatedConfig; + defaultText = lib.literalExpression ''pkgs.writeText "config.txt" (generated from settings and deviceTreeOverlays)''; description = '' - Path to the generated config.txt file. Defaults to the rendered output - of `settings`, but can be overridden to supply a custom config.txt. + Path to the config.txt file to install. Defaults to the rendered output + of {option}`settings` and {option}`deviceTreeOverlays`. A custom file + replaces that output, so the module then ignores both options. ''; }; }; diff --git a/raspberry-pi/common/firmware.nix b/raspberry-pi/common/firmware.nix index f3631ff2..2f7baf8d 100644 --- a/raspberry-pi/common/firmware.nix +++ b/raspberry-pi/common/firmware.nix @@ -70,14 +70,32 @@ let kept[$dst]=1 done - if [ -d "$dtbSrc/overlays" ]; then - for ovr in "$dtbSrc"/overlays/*; do + # hardware.raspberry-pi.configtxt.deviceTreeOverlays entries are + # matched by filename against the vendor .dtbo set the Raspberry Pi + # firmware ships, regardless of whether the *base* dtb above came + # from the generation or from firmwareBoot. Stage those + # unconditionally so config.txt's "dtoverlay=" lines have a target + # to apply, even when useGenerationDeviceTree = true. + if [ -d "$firmwareBoot/overlays" ]; then + for ovr in "$firmwareBoot"/overlays/*; do dst="$target/overlays/$(basename "$ovr")" copyForced "$ovr" "$dst" kept[$dst]=1 done fi + # When useGenerationDeviceTree also produced its own overlays + # (built via hardware.deviceTree.overlays), stage those too, + # letting them take precedence over same-named vendor overlays. + if [ "$dtbSrc" != "$firmwareBoot" ] && [ -d "$dtbSrc/overlays" ]; then + for ovr in "$dtbSrc"/overlays/*; do + dst="$target/overlays/$(basename "$ovr")" + copyForced "$ovr" "$dst" + kept[$dst]=1 + done + fi + + # Prune stale device trees / overlays. for fn in "$target"/*.dtb "$target"/overlays/*; do if [ "''${kept[$fn]:-}" != 1 ]; then diff --git a/raspberry-pi/common/kernel.nix b/raspberry-pi/common/kernel.nix index ec95e822..92b73547 100644 --- a/raspberry-pi/common/kernel.nix +++ b/raspberry-pi/common/kernel.nix @@ -10,9 +10,9 @@ let # NOTE: raspberryPiWirelessFirmware should be updated with this - modDirVersion = "6.18.34"; - tag = "stable_20260609"; - hash = "sha256-ok++36dh9o4e7AC5RErW00/r23rGxufe0PYXz5Dzy5U="; + modDirVersion = "6.18.50"; + tag = "stable_20260911"; + hash = "sha256-rIYYt4AzTZnZO1v8Gv6byJFXm4gQDlColgRSu09/9F4="; inherit (lib.kernel) freeform yes no; in (buildLinux ( diff --git a/raspberry-pi/common/raspberry-pi-wireless-firmware.nix b/raspberry-pi/common/raspberry-pi-wireless-firmware.nix index 17ef3fda..e5b20784 100644 --- a/raspberry-pi/common/raspberry-pi-wireless-firmware.nix +++ b/raspberry-pi/common/raspberry-pi-wireless-firmware.nix @@ -6,7 +6,7 @@ stdenvNoCC.mkDerivation { pname = "raspberrypi-wireless-firmware"; - version = "0-unstable-2026-03-21"; + version = "0-unstable-2026-07-10"; srcs = [ (fetchFromGitHub { @@ -20,8 +20,8 @@ stdenvNoCC.mkDerivation { name = "firmware-nonfree"; owner = "RPi-Distro"; repo = "firmware-nonfree"; - rev = "9794282eb9f4a2de1f23b41a738926740e975d83"; - hash = "sha256-OtA8yHvfusGP/ucf8Exzi+nSUmNoYp10u+luC2gbNZc="; + rev = "3bab0f823f5b53150b76aab77093adef6655b920"; + hash = "sha256-2DXj2esIix8YQwBf3IuzHwnUJ+yCANbRdpyS/DeB4XE="; }) ]; @@ -36,7 +36,8 @@ stdenvNoCC.mkDerivation { mkdir -p "$out/lib/firmware/brcm" # Wifi firmware - cp -r "firmware-nonfree/debian/config/brcm80211/." "$out/lib/firmware/" + cp -r "firmware-nonfree/debian/added-firmware/brcm" "$out/lib/firmware/" + cp -r "firmware-nonfree/debian/added-firmware/cypress" "$out/lib/firmware/" # Bluetooth firmware cp -r "bluez-firmware/debian/firmware/broadcom/." "$out/lib/firmware/brcm" diff --git a/spacemit/k3-pico-itx/README.md b/spacemit/k3-pico-itx/README.md index 0ca17733..eefa7b6e 100644 --- a/spacemit/k3-pico-itx/README.md +++ b/spacemit/k3-pico-itx/README.md @@ -79,44 +79,16 @@ hardware. I can't guarantee its availability, nor that everything is already built on it. The Hydra infrastructure is still being deployed, so please be lenient in that regard. -## HMP configuration +## X100 vs A100 -The X100 and A100 clusters have different vector widths (`VLENB=32` vs `VLENB=128`). +Here are the notable differences between the X100 and the A100. -You can configure HMP at runtime: - -`echo strict > /sys/kernel/spacemit_hmp/mode` (default) - -or - -`echo permissive > /sys/kernel/spacemit_hmp/mode` - -Permissive mode lets you use the 8 A100 cores in addition to the X100 cores. -However, I deliberately apply a segmentation between the two clusters because of a -VLEN issue. - -If you force tasks across both clusters anyway (e.g. via `taskset`), it will cause -crypto corruption. - -I also made a small module for this: - -```nix -hardware.spacemit.hmp = { - enable = true; - mode = "permissive"; # or "strict" -}; -``` - -- **`strict`** (default): uses the X100 cores only. -- **`permissive`**: uses both the X100 and A100 cores, but with a segmentation - between the clusters. - -An alternative is still available to use all 16 cores without segmentation, with -this: - -```nix -environment.variables.OPENSSL_riscvcap = "RV64GC"; -``` +| | X100 (cpu 0-7) | A100 (cpu 8-15) | +|---|---|---| +| Hypervisor extension (`h`) | yes (`rv64imafdcvh`) | no (`rv64imafdcv`) | +| Vector width | `VLENB=32` (VLEN 256-bit) | `VLENB=128` (VLEN 1024-bit) | +| L2 cache | 4 MB per cluster (8 MB total) | 1 MB per cluster (2 MB total) | +| L1 cache | 64 KB I + 64 KB D per core | identical | ## UEFI diff --git a/spacemit/k3-pico-itx/default.nix b/spacemit/k3-pico-itx/default.nix index e5b5c7de..be80d0e8 100644 --- a/spacemit/k3-pico-itx/default.nix +++ b/spacemit/k3-pico-itx/default.nix @@ -21,12 +21,14 @@ kernelParams = [ "earlycon=sbi" + "console=ttyS0,115200" "keep_bootcon" "systemd.journald.forward_to_console=1" "systemd.log_target=kmsg" "systemd.log_level=info" "clk_ignore_unused" "pd_ignore_unused" + "regulator_ignore_unused" "root=PARTLABEL=nixos-rootfs" "rootwait" "rootfstype=ext4" @@ -62,6 +64,10 @@ name = lib.mkDefault "spacemit/k3-pico-itx.dtb"; }; + boot.supportedFilesystems = lib.mkForce [ + "ext4" + "vfat" + ]; boot.initrd.systemd.enable = lib.mkDefault true; system.nixos-init.enable = lib.mkDefault true; system.etc.overlay.enable = lib.mkDefault true; diff --git a/spacemit/k3-pico-itx/linux.nix b/spacemit/k3-pico-itx/linux.nix index bc742988..5046f9e5 100644 --- a/spacemit/k3-pico-itx/linux.nix +++ b/spacemit/k3-pico-itx/linux.nix @@ -1,98 +1,222 @@ { buildLinux, fetchFromGitHub, - kernelPatches, lib, ... -} - -@args: +}@args: let - modDirVersion = "6.18.3"; + modDirVersion = "7.3.0"; in - buildLinux ( args // { - inherit kernelPatches modDirVersion; - version = "${modDirVersion}-spacemit-k3"; + inherit modDirVersion; + version = "7.3-spacemit-k3"; src = fetchFromGitHub { - owner = "liberodark"; - repo = "spacemit-linux-6.18"; - rev = "3709ed51967962fd02d6b4e5a80234719693931f"; - hash = "sha256-itqxXJCogLHbGucVHCDVwGkoqunQjJs0i1ZfvkuUtP4="; + owner = "torvalds"; + repo = "linux"; + tag = "v7.3"; + hash = "sha256-ca8CMAWKe1yzkQIn8sasUQxMLqdtfvC+wBFtX3819Zk="; }; - defconfig = "k3_bianbu_defconfig"; + defconfig = "defconfig"; - postPatch = '' - sed -i '/CONFIG_INITRAMFS_SOURCE=/d' arch/riscv/configs/k3_bianbu_defconfig - ''; - - structuredExtraConfig = - let - inherit (lib.kernel) yes no; - in + kernelPatches = [ { - CGROUPS = yes; - MEMCG = yes; - CGROUP_BPF = yes; - BPF = yes; - BPF_SYSCALL = yes; - BPF_JIT = yes; - NAMESPACES = yes; - USER_NS = yes; - SECCOMP = yes; - SECCOMP_FILTER = yes; - DEVTMPFS = yes; - DEVTMPFS_MOUNT = yes; - AUTOFS_FS = yes; - FANOTIFY = yes; - TMPFS_POSIX_ACL = yes; - TMPFS_XATTR = yes; - MODULES = yes; - MODULE_UNLOAD = yes; - RD_GZIP = yes; - RD_BZIP2 = yes; - RD_LZMA = yes; - RD_XZ = yes; - RD_LZO = yes; - RD_LZ4 = yes; - RD_ZSTD = yes; - # Disable - PSTORE_BLK = no; - PSTORE_BLK_BLKDEV = no; - RTL8852BS = no; - CORESIGHT = no; - ETHERCAT = no; - HWSPINLOCK_SPACEMIT = no; - DRM_AMDGPU = no; - DRM_RADEON = no; - DRM_NOUVEAU = no; - SPACEMIT_K1_CCU = no; - NVMEM_LAYOUT_ONIE_TLV = no; - DEBUG_VM = no; - DEBUG_VM_PGFLAGS = no; - DEBUG_VM_PGTABLE = no; - DEBUG_PAGEALLOC = no; - DEBUG_PAGEALLOC_ENABLE_DEFAULT = no; - PAGE_EXTENSION = lib.mkForce no; - PAGE_OWNER = no; - KASAN = no; - KMEMLEAK = no; - DEBUG_KMEMLEAK = no; - SLUB_DEBUG_ON = no; - MEM_ALLOC_PROFILING = lib.mkForce no; - MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT = lib.mkForce no; - NO_HZ_FULL = lib.mkForce no; - SLAB_FREELIST_HARDENED = lib.mkForce no; - SLAB_FREELIST_RANDOM = lib.mkForce no; - KFENCE = lib.mkForce no; - }; + name = "k3-pcie-03-pci-k1-device-data"; + patch = ./patches/0003-pci-k1-device-data.patch; + } + { + name = "k3-pcie-04-pci-k1-multiple-phy-handles"; + patch = ./patches/0004-pci-k1-multiple-phy-handles.patch; + } + { + name = "k3-pcie-04b-pci-k1-device-id-helper"; + patch = ./patches/0004b-pci-k1-device-id-helper.patch; + } + { + name = "k3-pcie-05-dt-bindings-dw-pcie-msi-parent"; + patch = ./patches/0005-dt-bindings-dw-pcie-msi-parent.patch; + } + { + name = "k3-pcie-06-dt-bindings-k3-pcie-host"; + patch = ./patches/0006-dt-bindings-k3-pcie-host.patch; + } + { + name = "k3-pcie-07-pci-k3-host-controller"; + patch = ./patches/0007-pci-k3-host-controller.patch; + } + { + name = "k3-08-serial-8250-gate-clock"; + patch = ./patches/0008-serial-8250_of-gate-clock.patch; + } + { + name = "k3-09-dts-uart0-gate-clock"; + patch = ./patches/0009-dts-k3-uart0-gate-clock.patch; + } + { + name = "k3-10-dts-k3-add-pcie"; + patch = ./patches/0010-dts-k3-add-pcie-combphy.patch; + } + { + name = "k3-11-dts-reserved-memory"; + patch = ./patches/0011-dts-k3-reserved-memory.patch; + } + { + name = "k3-12-thermal-spacemit-k3"; + patch = ./patches/0012-thermal-spacemit-k3.patch; + } + { + name = "k3-13-riscv-cpuinfo-model-name"; + patch = ./patches/0013-riscv-cpuinfo-model-name.patch; + } + { + name = "k3-14-cpufreq-spacemit-k3"; + patch = ./patches/0014-cpufreq-spacemit-k3.patch; + } + { + name = "k3-15-dts-com260-pcie"; + patch = ./patches/0015-dts-k3-com260-pcie.patch; + } + { + name = "k3-15-dt-bindings-vendor-prefixes-sensylink"; + patch = ./patches/0015-dt-bindings-vendor-prefixes-sensylink.patch; + } + { + name = "k3-16-dt-bindings-hwmon-lm63-family"; + patch = ./patches/0016-dt-bindings-hwmon-lm63-family.patch; + } + { + name = "k3-17-dt-bindings-hwmon-ctf2301"; + patch = ./patches/0017-dt-bindings-hwmon-ctf2301.patch; + } + { + name = "k3-18-hwmon-lm63-ctf2301"; + patch = ./patches/0018-hwmon-lm63-ctf2301.patch; + } + { + name = "k3-19-dts-i2c6-pinctrl"; + patch = ./patches/0019-dts-k3-i2c6-pinctrl.patch; + } + { + name = "k3-20-dts-com260-ctf2301"; + patch = ./patches/0020-dts-k3-com260-ctf2301.patch; + } + { + name = "k3-21-dts-com260-fan-cooling"; + patch = ./patches/0021-dts-k3-com260-fan-cooling.patch; + } + { + name = "k3-22-dts-com260-qspi-nor"; + patch = ./patches/0022-dts-k3-com260-qspi-nor.patch; + } + ]; - extraMeta.branch = "k3-br-v1.0.y"; + structuredExtraConfig = with lib.kernel; { + ARCH_SPACEMIT = option yes; + CPU_FREQ = option yes; + CPU_FREQ_GOV_PERFORMANCE = option yes; + CPU_FREQ_GOV_SCHEDUTIL = option yes; + CPU_FREQ_DEFAULT_GOV_SCHEDUTIL = option yes; + CPUFREQ_DT = option yes; + CPUFREQ_DT_PLATDEV = option yes; + SPACEMIT_K3_CPUFREQ = option yes; + PM_OPP = option yes; + SPACEMIT_K3_THERMAL = option yes; + THERMAL = option yes; + THERMAL_HWMON = option yes; + HWMON = option yes; + SENSORS_LM63 = option yes; + SMP = option yes; + SPACEMIT_K3_CCU = option yes; + GPIO_SPACEMIT_K1 = option yes; + CMA = option yes; + DMA_CMA = option yes; + SPARSEMEM_MANUAL = option yes; + PCI = option yes; + PCIE_DW = option yes; + PCIE_DW_HOST = option yes; + PCIE_SPACEMIT_K1 = option yes; + PHY_SPACEMIT_K3_COMBO_PHY = option yes; + PHY_SPACEMIT_K3_COMMON_OPS = option yes; + NVME_CORE = option yes; + BLK_DEV_NVME = option yes; + MMC = option yes; + MMC_SDHCI = option yes; + MMC_SDHCI_PLTFM = option yes; + MMC_SDHCI_OF_K1 = option yes; + SCSI = option yes; + STMMAC_ETH = option module; + USB = option yes; + USB_XHCI_HCD = option yes; + USB_EHCI_HCD = option yes; + SERIAL_8250 = option yes; + SERIAL_8250_CONSOLE = option yes; + SERIAL_OF_PLATFORM = option yes; + EFI = option yes; + EFI_STUB = option yes; + EFIVAR_FS = option yes; + CGROUPS = option yes; + MEMCG = option yes; + CGROUP_BPF = option yes; + BPF = option yes; + BPF_SYSCALL = option yes; + BPF_JIT = option yes; + NAMESPACES = option yes; + USER_NS = option yes; + SECCOMP = option yes; + SECCOMP_FILTER = option yes; + DEVTMPFS = option yes; + DEVTMPFS_MOUNT = option yes; + AUTOFS_FS = option yes; + FANOTIFY = option yes; + TMPFS_POSIX_ACL = option yes; + TMPFS_XATTR = option yes; + MODULES = option yes; + MODULE_UNLOAD = option yes; + RD_GZIP = option yes; + RD_BZIP2 = option yes; + RD_LZMA = option yes; + RD_XZ = option yes; + RD_LZO = option yes; + RD_LZ4 = option yes; + RD_ZSTD = option yes; + # Disable + PSTORE_BLK = no; + PSTORE_BLK_BLKDEV = no; + RTL8852BS = no; + CORESIGHT = no; + ETHERCAT = no; + HWSPINLOCK_SPACEMIT = no; + DRM_AMDGPU = no; + DRM_RADEON = no; + DRM_NOUVEAU = no; + SPACEMIT_K1_CCU = no; + NVMEM_LAYOUT_ONIE_TLV = no; + DEBUG_VM = no; + DEBUG_VM_PGFLAGS = no; + DEBUG_VM_PGTABLE = no; + DEBUG_PAGEALLOC = no; + DEBUG_PAGEALLOC_ENABLE_DEFAULT = no; + PAGE_EXTENSION = lib.mkForce no; + PAGE_OWNER = no; + KASAN = no; + KMEMLEAK = no; + DEBUG_KMEMLEAK = no; + SLUB_DEBUG_ON = no; + MEM_ALLOC_PROFILING = lib.mkForce no; + MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT = lib.mkForce no; + NO_HZ_FULL = lib.mkForce no; + SLAB_FREELIST_HARDENED = lib.mkForce no; + SLAB_FREELIST_RANDOM = lib.mkForce no; + KFENCE = lib.mkForce no; + DEBUG_INFO_BTF = lib.mkForce no; + DEBUG_INFO_BTF_MODULES = lib.mkForce no; + }; + + extraMeta.branch = "7.3"; } // (args.argsOverride or { }) ) diff --git a/spacemit/k3-pico-itx/patches/0001-phy-dt-bindings-k3-comb-phy.patch b/spacemit/k3-pico-itx/patches/0001-phy-dt-bindings-k3-comb-phy.patch new file mode 100644 index 00000000..83487aac --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0001-phy-dt-bindings-k3-comb-phy.patch @@ -0,0 +1,92 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH 1/2] dt-bindings: phy: Add Spacemit K3 USB3/PCIe comb phy + support +From: Inochi Amaoto +Date: Thu, 30 Apr 2026 10:28:40 +0800 +Message-Id: <20260430022843.1090138-2-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +The USB3/PCIe comb PHY on the K3 is a complex PHY group that +can provide multiple phy for both PCIe and USB controller. +Its mux configuration is controlled by the APMU syscon device. + +Signed-off-by: Inochi Amaoto +--- + .../bindings/phy/spacemit,k3-comb-phy.yaml | 63 +++++++++++++++++++ + 1 file changed, 63 insertions(+) + create mode 100644 Documentation/devicetree/bindings/phy/spacemit,k3-comb-phy.yaml + +diff --git a/Documentation/devicetree/bindings/phy/spacemit,k3-comb-phy.yaml b/Documentation/devicetree/bindings/phy/spacemit,k3-comb-phy.yaml +new file mode 100644 +index 000000000000..7aa2cf9301b7 +--- /dev/null ++++ b/Documentation/devicetree/bindings/phy/spacemit,k3-comb-phy.yaml +@@ -0,0 +1,63 @@ ++# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/phy/spacemit,k3-comb-phy.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: Spacemit K3 PCIE/USB3 Comb PHY ++ ++maintainers: ++ - Inochi Amaoto ++ ++properties: ++ compatible: ++ const: spacemit,k3-comb-phy ++ ++ reg: ++ maxItems: 1 ++ ++ "#phy-cells": ++ const: 2 ++ description: ++ The first one is phy id, the second one is phy type. ++ ++ spacemit,apb-spare: ++ $ref: /schemas/types.yaml#/definitions/phandle ++ description: ++ Phandle to APB SPARE system controller interface, used for ++ PHY calibration. ++ ++ spacemit,apmu: ++ $ref: /schemas/types.yaml#/definitions/phandle-array ++ items: ++ - items: ++ - description: phandle of APMU syscon ++ - description: configuration of the PHY lanes ++ description: | ++ Phandle to control PHY mux configuration. The configuration ++ is described as follows: ++ bit 4: 0 - PCIe A x8 mode, 1 - PCIe lane share mode ++ bit 3: 0 - PCIe A x4 mode, 1 - PCIe A x2 and PCIe B x2 mode ++ bit 2: 0 - PCIe C lane 0 is PCIe mode , 1 - USB mode ++ bit 1: 0 - PCIe C lane 1 is PCIe mode , 1 - USB mode ++ bit 0: 0 - PCIe D lane is PCIe mode , 1 - USB mode ++ ++ The bit[3:0] is only valid when bit 4 is 1. ++ ++required: ++ - compatible ++ - "#phy-cells" ++ - spacemit,apb-spare ++ - spacemit,apmu ++ ++additionalProperties: false ++ ++examples: ++ - | ++ phy@81d00000 { ++ compatible = "spacemit,k3-comb-phy"; ++ reg = <0x81d00000 0x600000>; ++ #phy-cells = <2>; ++ spacemit,apb-spare = <&apb_spare>; ++ spacemit,apmu = <&apmu 0x00>; ++ }; +-- +2.54.0 + diff --git a/spacemit/k3-pico-itx/patches/0002-phy-k3-combphy-driver.patch b/spacemit/k3-pico-itx/patches/0002-phy-k3-combphy-driver.patch new file mode 100644 index 00000000..1407562b --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0002-phy-k3-combphy-driver.patch @@ -0,0 +1,758 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH 2/2] phy: spacemit: Add USB3/PCIe comb PHY driver for + Spacemit K3 +From: Inochi Amaoto +Date: Thu, 30 Apr 2026 10:28:41 +0800 +Message-Id: <20260430022843.1090138-3-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 8bit + +The comb PHY on K3 requires to configure a syscon device for the +right mux configuration. And it requires calibration before any +usage. + +Add USB3/PCIe comb PHY driver for Spacemit K3. + +Signed-off-by: Inochi Amaoto +--- + drivers/phy/spacemit/Kconfig | 16 ++ + drivers/phy/spacemit/Makefile | 2 + + drivers/phy/spacemit/phy-k3-combphy.c | 250 ++++++++++++++++ + drivers/phy/spacemit/phy-k3-common.c | 398 ++++++++++++++++++++++++++ + drivers/phy/spacemit/phy-k3-common.h | 27 ++ + 5 files changed, 693 insertions(+) + create mode 100644 drivers/phy/spacemit/phy-k3-combphy.c + create mode 100644 drivers/phy/spacemit/phy-k3-common.c + create mode 100644 drivers/phy/spacemit/phy-k3-common.h + +diff --git a/drivers/phy/spacemit/Kconfig b/drivers/phy/spacemit/Kconfig +index 50b0005acf66..5fdf18fce499 100644 +--- a/drivers/phy/spacemit/Kconfig ++++ b/drivers/phy/spacemit/Kconfig +@@ -23,3 +23,19 @@ config PHY_SPACEMIT_K1_USB2 + help + Enable this to support K1 USB 2.0 PHY driver. This driver takes care of + enabling and clock setup and will be used by K1 udc/ehci/otg/xhci driver. ++ ++config PHY_SPACEMIT_K3_COMMON_OPS ++ tristate ++ select MFD_SYSCON ++ select GENERIC_PHY ++ ++config PHY_SPACEMIT_K3_COMBO_PHY ++ tristate "SpacemiT K3 USB3/PCIe PHY support" ++ depends on (ARCH_SPACEMIT || COMPILE_TEST) && OF ++ depends on COMMON_CLK ++ select PHY_SPACEMIT_K3_COMMON_OPS ++ help ++ Enable this to support K3 USB3/PCIe combo PHY driver. This ++ driver takes care of enabling and clock setup and will be used ++ by K3 dwc3 driver. ++ If unsure, say N. +diff --git a/drivers/phy/spacemit/Makefile b/drivers/phy/spacemit/Makefile +index a821a21d6142..41be7b0388da 100644 +--- a/drivers/phy/spacemit/Makefile ++++ b/drivers/phy/spacemit/Makefile +@@ -1,3 +1,5 @@ + # SPDX-License-Identifier: GPL-2.0-only + obj-$(CONFIG_PHY_SPACEMIT_K1_PCIE) += phy-k1-pcie.o + obj-$(CONFIG_PHY_SPACEMIT_K1_USB2) += phy-k1-usb2.o ++obj-$(CONFIG_PHY_SPACEMIT_K3_COMBO_PHY) += phy-k3-combphy.o ++obj-$(CONFIG_PHY_SPACEMIT_K3_COMMON_OPS) += phy-k3-common.o +diff --git a/drivers/phy/spacemit/phy-k3-combphy.c b/drivers/phy/spacemit/phy-k3-combphy.c +new file mode 100644 +index 000000000000..66fa6330ad6e +--- /dev/null ++++ b/drivers/phy/spacemit/phy-k3-combphy.c +@@ -0,0 +1,250 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++/* ++ * phy-k3-usb3.c - SpacemiT K3 Type-C Orientation Switch Driver ++ * ++ * Copyright (c) 2025 SpacemiT Technology Co. Ltd ++ */ ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++ ++#include "phy-k3-common.h" ++ ++/* ++ * The PCIE/USB Subsystem on SpacemiT K3 have 3 single lane PIPE3 PHYs ++ * (PHY2/3/4) shared by PCIE PortC/D and USB3 PortB/C/D. ++ * ++ * PMUA_PCIE_SUBSYS_MGMT[4:0] ++ * ++ * bit4 = 0 : PCIe A X8 mode, all 8 lanes dedicated to PCIe Port A ++ * 1 : PHY lanes shared between PCIe or USB according to [3:0] ++ * ++ * All PHY matrix combinations according to [4:0]: ++ * ++ * 0x0X : PCIe-A X8 ++ * 0x10 : PCIe-C x2 (PHY2+PHY3) + PCIe-D x1 (PHY4) ++ * 0x11 : PCIe-C x2 (PHY2+PHY3) + USB-D (PHY4) ++ * 0x12 : PCIe-C x1 (PHY2) + USB-C (PHY3) ++ * 0x13 : PCIe-C x1 (PHY2) + USB-C (PHY3) + USB-D (PHY4) ++ * 0x14 : PCIe-C x1 (PHY3) + USB-B (PHY2) ++ * 0x15 : PCIe-C x1 (PHY3) + USB-B (PHY2) + USB-D (PHY4) ++ * 0x16 : USB-B (PHY2) + USB-C (PHY3) + PCIe D x1 (PHY4) ++ * 0x17 : USB-B (PHY2) + USB-C (PHY3) + USB-D (PHY4) ++ * ++ * So any USB Port B/C/D operation requires PCIe A X8 mode to be disabled. ++ */ ++#define PMUA_PCIE_SUBSYS_MGMT 0x1d8 ++#define PU_MATRIX_CONF_MASK GENMASK(4, 0) ++ ++#define COMBPHY_MAX_SUBPHYS 6 ++ ++struct k3_comb_phy { ++ struct device *dev; ++ struct k3_lane_group groups[COMBPHY_MAX_SUBPHYS]; ++ void __iomem *base; ++ struct regmap *apb_spare; ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group0 = { ++ .lanes = 2, ++ .config = 0x00, ++ .mask = 0xff, ++ .offsets = { ++ 0x0, 0x400 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group1 = { ++ .lanes = 2, ++ .config = 0x00, ++ .mask = 0xff, ++ .offsets = { ++ 0x100000, 0x100400 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group2 = { ++ .lanes = 1, ++ .config = 0x14, ++ .mask = 0x14, ++ .offsets = { ++ 0x200000 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group3 = { ++ .lanes = 1, ++ .config = 0x12, ++ .mask = 0x12, ++ .offsets = { ++ 0x300000 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group4 = { ++ .lanes = 1, ++ .config = 0x11, ++ .mask = 0x11, ++ .offsets = { ++ 0x400000 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data k3_combphy_lane_group5 = { ++ .lanes = 1, ++ .config = 0x00, ++ .mask = 0xff, ++ .offsets = { ++ 0x500000 ++ }, ++}; ++ ++static const struct k3_phy_lane_group_data *k3_combphy_lane_datas[] = { ++ &k3_combphy_lane_group0, ++ &k3_combphy_lane_group1, ++ &k3_combphy_lane_group2, ++ &k3_combphy_lane_group3, ++ &k3_combphy_lane_group4, ++ &k3_combphy_lane_group5, ++}; ++ ++static int k3_comb_phy_init_lanes(struct k3_comb_phy *phy, unsigned int config) ++{ ++ int i; ++ ++ for (i = 0; i < ARRAY_SIZE(k3_combphy_lane_datas); i++) { ++ const struct k3_phy_lane_group_data *data = k3_combphy_lane_datas[i]; ++ struct k3_lane_group *lg = &phy->groups[i]; ++ const struct phy_ops *ops; ++ bool is_usb; ++ ++ is_usb = (data->mask & config) == data->config; ++ if (is_usb) ++ ops = &k3_usb3_phy_ops; ++ else ++ ops = &k3_pcie_phy_ops; ++ ++ lg->phy = devm_phy_create(phy->dev, NULL, ops); ++ if (IS_ERR(lg->phy)) ++ return PTR_ERR(lg->phy); ++ ++ lg->is_pcie = !is_usb; ++ lg->data = data; ++ lg->base = phy->base; ++ phy_set_drvdata(lg->phy, lg); ++ } ++ ++ return 0; ++} ++ ++static int k3_comb_phy_update_config(struct regmap *apmu, unsigned int config) ++{ ++ if (config & ~PU_MATRIX_CONF_MASK) ++ return -EINVAL; ++ ++ return regmap_update_bits(apmu, PMUA_PCIE_SUBSYS_MGMT, PU_MATRIX_CONF_MASK, config); ++} ++ ++static struct phy *k3_comb_phy_xlate(struct device *dev, const struct of_phandle_args *args) ++{ ++ struct k3_comb_phy *phy = dev_get_drvdata(dev); ++ struct k3_lane_group *lg; ++ ++ if (args->args_count != 2) { ++ dev_err(dev, "Invalid number of arguments\n"); ++ return ERR_PTR(-EINVAL); ++ } ++ ++ if (args->args[0] >= ARRAY_SIZE(k3_combphy_lane_datas)) { ++ dev_err(dev, "Invalid PHY id\n"); ++ return ERR_PTR(-EINVAL); ++ } ++ ++ lg = &phy->groups[args->args[0]]; ++ ++ if ((lg->is_pcie && args->args[1] != PHY_TYPE_PCIE) || ++ (!lg->is_pcie && args->args[1] != PHY_TYPE_USB3)) { ++ dev_err(dev, "Invalid PHY mode\n"); ++ return ERR_PTR(-EINVAL); ++ } ++ ++ return lg->phy; ++} ++ ++static int k3_comb_phy_probe(struct platform_device *pdev) ++{ ++ struct device *dev = &pdev->dev; ++ struct device_node *node = dev->of_node; ++ struct phy_provider *provider; ++ struct k3_comb_phy *phy; ++ struct regmap *apmu; ++ u32 config = 0; ++ int ret; ++ ++ phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); ++ if (!phy) ++ return -ENOMEM; ++ ++ phy->base = devm_platform_ioremap_resource(pdev, 0); ++ if (IS_ERR(phy->base)) ++ return PTR_ERR(phy->base); ++ ++ phy->apb_spare = syscon_regmap_lookup_by_phandle(node, "spacemit,apb-spare"); ++ if (IS_ERR(phy->apb_spare)) ++ return dev_err_probe(dev, PTR_ERR(phy->apb_spare), ++ "Failed to fine APB SPARE syscon"); ++ ++ apmu = syscon_regmap_lookup_by_phandle_args(node, "spacemit,apmu", 1, &config); ++ if (IS_ERR(apmu)) ++ return dev_err_probe(dev, PTR_ERR(phy->apb_spare), ++ "Failed to fine APMU syscon"); ++ ++ ret = k3_comb_phy_update_config(apmu, config); ++ if (ret < 0) ++ return dev_err_probe(dev, ret, "Failed to set lane configuration"); ++ ++ phy->dev = dev; ++ platform_set_drvdata(pdev, phy); ++ ++ ret = k3_phy_calibrate(phy->apb_spare); ++ if (ret < 0) ++ return dev_err_probe(dev, ret, "Failed to calibrate phy"); ++ ++ ret = k3_comb_phy_init_lanes(phy, config); ++ if (ret < 0) ++ return dev_err_probe(dev, ret, "Failed to init lanes"); ++ ++ provider = devm_of_phy_provider_register(dev, k3_comb_phy_xlate); ++ if (IS_ERR(provider)) ++ return dev_err_probe(dev, PTR_ERR(provider), ++ "Failed to register provider\n"); ++ ++ return 0; ++} ++ ++static const struct of_device_id k3_comb_phy_of_match[] = { ++ { .compatible = "spacemit,k3-comb-phy" }, ++ { }, ++}; ++MODULE_DEVICE_TABLE(of, k3_comb_phy_of_match); ++ ++static struct platform_driver k3_comb_phy_driver = { ++ .probe = k3_comb_phy_probe, ++ .driver = { ++ .name = "spacemit,k3-comb-phy", ++ .of_match_table = k3_comb_phy_of_match, ++ }, ++}; ++module_platform_driver(k3_comb_phy_driver); ++ ++MODULE_DESCRIPTION("SpacemiT K3 USB3/PCIe comb PHY driver"); ++MODULE_LICENSE("GPL"); +diff --git a/drivers/phy/spacemit/phy-k3-common.c b/drivers/phy/spacemit/phy-k3-common.c +new file mode 100644 +index 000000000000..77c4b4073b96 +--- /dev/null ++++ b/drivers/phy/spacemit/phy-k3-common.c +@@ -0,0 +1,398 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++#include ++ ++#include "phy-k3-common.h" ++ ++/* PHY Registers */ ++#define PHY_VERSION 0x0 ++ ++#define PHY_RESET_CFG 0x04 ++ ++#define PHY_RESET_RXBUF_RST BIT(0) ++#define PHY_RESET_SOFT_RST_PCS BIT(1) ++#define PHY_RESET_SOFT_RST_AHB BIT(2) ++#define PHY_RESET_EN_SD_AFTER_LOCK BIT(6) ++ ++#define PHY_CLK_CFG 0x08 ++ ++#define PHY_CLK_PLL_READY BIT(0) ++#define PHY_CLK_TXCLK_INV BIT(2) ++#define PHY_CLK_RXCLK_EN BIT(3) ++#define PHY_CLK_TXCLK_EN BIT(4) ++#define PHY_CLK_PCLK_EN BIT(5) ++#define PHY_CLK_PIPE_PCLK_EN BIT(6) ++#define PHY_CLK_REFCLK_FREQ GENMASK(10, 7) ++#define PHY_CLK_REFCLK_24M 2 ++#define PHY_CLK_SW_INIT_DONE BIT(11) ++#define PHY_CLK_PU_SSC_OUT BIT(23) ++ ++#define PHY_MODE_CFG 0x0C ++ ++#define PHY_MODE_PCIE_INT_EN BIT(0) ++#define PHY_MODE_LFPS_TPERIOD GENMASK(9, 8) ++#define PHY_MODE_LFPS_TPERIOD_USB 3 ++ ++#define PHY_PU_SEL 0x40 ++ ++#define PHY_PU_CFG_STATUS BIT(9) ++#define PHY_PU_OVRD_STATUS BIT(10) ++ ++#define PHY_PU_CK_REG 0x54 ++ ++#define PHY_PU_REFCLK_100 BIT(25) ++ ++#define PHY_PLL_REG1 0x58 ++ ++#define PHY_PLL_FREF_SEL GENMASK(15, 13) ++#define PHY_PLL_FREF_24M 0x1 ++#define PHY_PLL_SSC_DEP_SEL GENMASK(27, 24) ++#define PHY_PLL_SSC_5000PPM 0xa ++#define PHY_PLL_SSC_MODE GENMASK(29, 28) ++#define PHY_PLL_SSC_MODE_CENTER_SPREAD 0 ++#define PHY_PLL_SSC_MODE_UP_SPREAD 1 ++#define PHY_PLL_SSC_MODE_DOWN_SPREAD 2 ++#define PHY_PLL_SSC_MODE_DOWN_SPREAD1 3 ++ ++#define PHY_PLL_REG2 0x5c ++ ++#define PHY_PLL_SEL_REF100 BIT(21) ++ ++/* PHY RX Register Definitions */ ++#define PHY_RX_REG_A 0x60 ++ ++#define PHY_RX_REG0_RLOAD BIT(4) ++#define PHY_RX_REG1_RTERM GENMASK(11, 8) ++#define PHY_RX_REG1_RC_CALI GENMASK(15, 12) ++#define PHY_RX_REG2_CSEL GENMASK(19, 16) ++#define PHY_RX_REG2_FORCE_CSEL BIT(20) ++#define PHY_RX_REG2_PSEL GENMASK(23, 21) ++#define PHY_RX_REG3_I_LOAD GENMASK(26, 24) ++#define PHY_RX_REG3_SEL_CBOOST_CODE BIT(27) ++#define PHY_RX_REG3_ADJ_BIAS GENMASK(29, 28) ++#define PHY_RX_REG3_RDEG1 GENMASK(31, 30) ++ ++#define PHY_RX_REG_B 0x64 ++ ++#define PHY_RX_REGB_MASK GENMASK(23, 0) ++ ++#define PHY_RX_REG4_RDEG2 GENMASK(2, 1) ++#define PHY_RX_REG4_ENVOS BIT(4) ++#define PHY_RX_REG4_RTERM_SEL BIT(5) ++#define PHY_RX_REG4_MANUAL_CFG BIT(7) ++#define PHY_RX_REG5_RCELL_VCM GENMASK(11, 8) ++#define PHY_RX_REG5_RCELL_BIAS GENMASK(15, 12) ++#define PHY_RX_REG6_H1_REG GENMASK(19, 16) ++#define PHY_RX_REG6_ADAPT_GAIN GENMASK(21, 20) ++#define PHY_RX_REG6_BYPASS_ADPT BIT(22) ++ ++#define PHY_ADPT_CFG0 0x140 ++#define PHY_ADPT_AFE_RST_OVRD_EN BIT(1) ++#define PHY_ADPT_AFE_RST_OVRD_VAL BIT(4) ++ ++#define PHY_RXEQ_TIME 0xb4 ++#define PHY_RXEQ_TIME_OVRD_POST_C_SOC BIT(21) ++#define PHY_RXEQ_TIME_CFG_AMP_SOC GENMASK(23, 22) ++#define PHY_RXEQ_TIME_AMP_SOC_650M 0 ++#define PHY_RXEQ_TIME_AMP_SOC_800M 1 ++#define PHY_RXEQ_TIME_AMP_SOC_870M 2 ++#define PHY_RXEQ_TIME_AMP_SOC_900M 3 ++#define PHY_RXEQ_TIME_OVRD_AMP_SOC BIT(24) ++ ++#define PCIE_PU_ADDR_CLK_CFG 0x0008 ++#define PHY_CLK_PLL_READY BIT(0) ++#define PCIE_INITAL_TIMER GENMASK(6, 3) ++#define CFG_INTERNAL_TIMER_ADJ GENMASK(10, 7) ++#define CFG_SW_PHY_INIT_DONE BIT(11) ++ ++/* Lane RX/TX configuration (per‑lane, at lane_base) */ ++#define PCIE_RX_REG1 0x050 ++#define PCIE_TX_REG1 0x064 ++ ++#define PCIE_PLL_TIMEOUT 500000 ++#define PCIE_POLL_DELAY 500 ++ ++static int k3_usb3phy_init_single(struct k3_lane_group *lg, void __iomem *base) ++{ ++ struct phy *phy = lg->phy; ++ u32 val, tmp; ++ int ret; ++ ++ val = readl(base + PHY_PU_SEL); ++ val = u32_replace_bits(val, 0, PHY_PU_CFG_STATUS); ++ val |= PHY_PU_OVRD_STATUS; ++ writel(val, base + PHY_PU_SEL); ++ ++ udelay(200); ++ ++ /* Do not wait CDR lock before sampling data */ ++ val = readl(base + PHY_RESET_CFG); ++ val = u32_replace_bits(val, 0, PHY_RESET_EN_SD_AFTER_LOCK); ++ writel(val, base + PHY_RESET_CFG); ++ ++ /* Power down 100MHz refclk buffer */ ++ val = readl(base + PHY_PU_CK_REG); ++ val = u32_replace_bits(val, 0, PHY_PU_REFCLK_100); ++ writel(val, base + PHY_PU_CK_REG); ++ ++ /* Program PLL REG1 configure the SSC */ ++ val = FIELD_PREP(PHY_PLL_SSC_MODE, PHY_PLL_SSC_MODE_DOWN_SPREAD1) | ++ FIELD_PREP(PHY_PLL_SSC_DEP_SEL, PHY_PLL_SSC_5000PPM) | ++ FIELD_PREP(PHY_PLL_FREF_SEL, PHY_PLL_FREF_24M); ++ writel(val, base + PHY_PLL_REG1); ++ ++ /* Un-select 100MHz PLL reference */ ++ val = readl(base + PHY_PLL_REG2); ++ val = u32_replace_bits(val, 0, PHY_PLL_SEL_REF100); ++ writel(val, base + PHY_PLL_REG2); ++ ++ /* USB LFPS period configuration */ ++ val = readl(base + PHY_MODE_CFG); ++ val = u32_replace_bits(val, PHY_MODE_LFPS_TPERIOD_USB, PHY_MODE_LFPS_TPERIOD); ++ writel(val, base + PHY_MODE_CFG); ++ ++ /* Force AFE adaptation reset */ ++ val = readl(base + PHY_ADPT_CFG0); ++ val |= PHY_ADPT_AFE_RST_OVRD_EN | PHY_ADPT_AFE_RST_OVRD_VAL; ++ writel(val, base + PHY_ADPT_CFG0); ++ ++ /* Override driver amplitude value to 900m */ ++ val = readl(base + PHY_RXEQ_TIME); ++ val |= PHY_RXEQ_TIME_OVRD_AMP_SOC; ++ val = u32_replace_bits(val, PHY_RXEQ_TIME_AMP_SOC_900M, PHY_RXEQ_TIME_CFG_AMP_SOC); ++ writel(val, base + PHY_RXEQ_TIME); ++ ++ /* Configure RX parameters */ ++ val = PHY_RX_REG0_RLOAD | ++ FIELD_PREP(PHY_RX_REG1_RTERM, 0x8) | ++ FIELD_PREP(PHY_RX_REG1_RC_CALI, 0x7) | ++ FIELD_PREP(PHY_RX_REG2_CSEL, 0x8) | ++ PHY_RX_REG2_FORCE_CSEL | ++ FIELD_PREP(PHY_RX_REG2_PSEL, 0x4) | ++ FIELD_PREP(PHY_RX_REG3_I_LOAD, 0x7) | ++ PHY_RX_REG3_SEL_CBOOST_CODE | ++ FIELD_PREP(PHY_RX_REG3_ADJ_BIAS, 0x1) | ++ FIELD_PREP(PHY_RX_REG3_RDEG1, 0x3); ++ writel(val, base + PHY_RX_REG_A); ++ ++ val = readl(base + PHY_RX_REG_B); ++ tmp = FIELD_PREP(PHY_RX_REG4_RDEG2, 0x2) | ++ PHY_RX_REG4_ENVOS | PHY_RX_REG4_RTERM_SEL | PHY_RX_REG4_MANUAL_CFG | ++ FIELD_PREP(PHY_RX_REG5_RCELL_VCM, 0x8) | ++ FIELD_PREP(PHY_RX_REG5_RCELL_BIAS, 0x8) | ++ FIELD_PREP(PHY_RX_REG6_H1_REG, 0x8) | ++ FIELD_PREP(PHY_RX_REG6_ADAPT_GAIN, 0x2); ++ val = u32_replace_bits(val, tmp, PHY_RX_REGB_MASK); ++ writel(val, base + PHY_RX_REG_B); ++ ++ /* ++ * Inform PHY that all PLL-related configuration is done. ++ * PLL will not start locking until PHY_CLK_SW_INIT_DONE is set. ++ */ ++ val = PHY_CLK_SW_INIT_DONE | PHY_CLK_PU_SSC_OUT | ++ FIELD_PREP(PHY_CLK_REFCLK_FREQ, PHY_CLK_REFCLK_24M) | ++ PHY_CLK_RXCLK_EN | PHY_CLK_TXCLK_EN | ++ PHY_CLK_PCLK_EN | PHY_CLK_PIPE_PCLK_EN; ++ writel(val, base + PHY_CLK_CFG); ++ ++ ret = readl_poll_timeout(base + PHY_CLK_CFG, val, ++ (val & PHY_CLK_PLL_READY), ++ PCIE_POLL_DELAY, PCIE_PLL_TIMEOUT); ++ if (ret) { ++ dev_err(&phy->dev, "PHY PLL polling timeout\n"); ++ return ret; ++ } ++ ++ return 0; ++} ++ ++static int k3_usb3phy_init(struct phy *phy) ++{ ++ struct k3_lane_group *lg = phy_get_drvdata(phy); ++ int ret, i; ++ ++ for (i = 0; i < lg->data->lanes; i++) { ++ ret = k3_usb3phy_init_single(lg, lg->base + lg->data->offsets[i]); ++ if (ret < 0) ++ return ret; ++ } ++ ++ return 0; ++} ++ ++static int k3_usb3phy_set_speed(struct phy *phy, int speed) ++{ ++ struct k3_lane_group *lg = phy_get_drvdata(phy); ++ void __iomem *base = lg->base + lg->data->offsets[0]; ++ u32 val; ++ ++ if (speed == USB_SPEED_HIGH) { ++ val = readl(base + PHY_PU_SEL); ++ val = u32_replace_bits(val, 0, PHY_PU_CFG_STATUS); ++ val |= PHY_PU_OVRD_STATUS; ++ writel(val, base + PHY_PU_SEL); ++ ++ udelay(200); ++ } ++ ++ return 0; ++} ++ ++const struct phy_ops k3_usb3_phy_ops = { ++ .init = k3_usb3phy_init, ++ .set_speed = k3_usb3phy_set_speed, ++ .owner = THIS_MODULE, ++}; ++EXPORT_SYMBOL_GPL(k3_usb3_phy_ops); ++ ++static int k3_pcie_phy_init(struct phy *phy) ++{ ++ struct k3_lane_group *lg = phy_get_drvdata(phy); ++ void __iomem *phy_base = lg->base + lg->data->offsets[0]; ++ u32 val; ++ int ret; ++ int i; ++ ++ val = readl(phy_base + PHY_PLL_REG1); ++ val = u32_replace_bits(val, 0x2, GENMASK(15, 12)); ++ writel(val, phy_base + PHY_PLL_REG1); ++ ++ val = readl(phy_base + PHY_PLL_REG2); ++ val = u32_replace_bits(val, 0, BIT(21)); ++ writel(val, phy_base + PHY_PLL_REG2); ++ ++ for (i = 0; i < lg->data->lanes; i++) { ++ void __iomem *lane_base = lg->base + lg->data->offsets[i]; ++ ++ val = readl(lane_base + PCIE_RX_REG1); ++ val = u32_replace_bits(val, 0, 0x3); ++ writel(val, phy_base + PCIE_RX_REG1); ++ } ++ ++ val = readl(phy_base + PHY_PLL_REG2); ++ val |= BIT(20); ++ writel(val, phy_base + PHY_PLL_REG2); ++ ++ writel(0x00006505, phy_base + PCIE_RX_REG1); ++ ++ /* pll_reg1 of lane0, disable SSC: pll_reg4[3:0] = 0 */ ++ val = readl(phy_base + PHY_PLL_REG1); ++ val = u32_replace_bits(val, 0, GENMASK(27, 24)); ++ writel(val, phy_base + PHY_PLL_REG1); ++ ++ for (i = 0; i < lg->data->lanes; i++) { ++ void __iomem *lane_base = lg->base + lg->data->offsets[i]; ++ ++ /* set cfg_tx_send_dummy_data to be 1'b1 for disable dash data */ ++ val = readl(lane_base + PHY_PU_SEL); ++ val = u32_replace_bits(val, 1, BIT(13)); ++ writel(val, lane_base + PHY_PU_SEL); ++ ++ /* disable en_sample_data_after_cdr_locked */ ++ val = readl(lane_base + PHY_RESET_CFG); ++ val = u32_replace_bits(val, 0, BIT(6)); ++ writel(val, lane_base + PHY_RESET_CFG); ++ ++ /* Dynamic Lock */ ++ val = readl(lane_base + PHY_MODE_CFG); ++ val = u32_replace_bits(val, 1, BIT(2)); ++ writel(val, lane_base + PHY_MODE_CFG); ++ ++ val = FIELD_PREP(GENMASK(7, 0), 0x10) | ++ FIELD_PREP(GENMASK(15, 8), 0x78) | ++ FIELD_PREP(GENMASK(23, 16), 0x98) | ++ FIELD_PREP(GENMASK(31, 24), 0xdf); ++ writel(val, lane_base + PHY_RX_REG_A); ++ ++ val = readl(lane_base + PHY_RX_REG_B); ++ val &= ~PHY_RX_REGB_MASK; ++ val |= FIELD_PREP(GENMASK(7, 0), 0xb4) | ++ FIELD_PREP(GENMASK(15, 8), 0x88) | ++ FIELD_PREP(GENMASK(23, 16), 0x28); ++ writel(val, lane_base + PHY_RX_REG_B); ++ ++ /* Set init done */ ++ val = readl(lane_base + PCIE_PU_ADDR_CLK_CFG); ++ val = u32_replace_bits(val, 1, CFG_SW_PHY_INIT_DONE); ++ writel(val, lane_base + PCIE_PU_ADDR_CLK_CFG); ++ } ++ ++ ret = readl_poll_timeout(phy_base + PCIE_PU_ADDR_CLK_CFG, val, ++ (val & PHY_CLK_PLL_READY), PCIE_POLL_DELAY, ++ PCIE_PLL_TIMEOUT); ++ if (ret) { ++ dev_err(&lg->phy->dev, "PHY PLL lock timeout\n"); ++ return ret; ++ } ++ ++ return 0; ++} ++ ++const struct phy_ops k3_pcie_phy_ops = { ++ .init = k3_pcie_phy_init, ++ .owner = THIS_MODULE, ++}; ++EXPORT_SYMBOL_GPL(k3_pcie_phy_ops); ++ ++/* PHY rcal init requires APB_SPARE regmap access */ ++ ++#define APB_SPARE_PU_CAL 0x178 ++#define PU_CAL BIT(17) ++ ++#define APB_SPARE_RCAL_HSIO 0x17c ++#define APB_SPARE_PU_CAL_DONE BIT(8) ++#define RCAL_OVRD_PTRIM GENMASK(23, 20) ++#define RCAL_OVRD_NTRIM GENMASK(27, 24) ++#define RCAL_OVRD_PTRIM_EN BIT(28) ++#define RCAL_OVRD_NTRIM_EN BIT(29) ++#define RCAL_OVRD_STABLE_VAL BIT(30) ++#define RCAL_OVRD_STABLE_EN BIT(31) ++ ++#define RCAL_OVRD_TRIM_EN (RCAL_OVRD_NTRIM_EN | RCAL_OVRD_PTRIM_EN) ++#define RCAL_OVRD_TRIM_MASK (RCAL_OVRD_NTRIM | RCAL_OVRD_PTRIM) ++ ++#define PU_CAL_TIMEOUT 2000000 ++ ++static DEFINE_MUTEX(calibrate_lock); ++ ++int k3_phy_calibrate(struct regmap *apb_spare) ++{ ++ unsigned int val; ++ int ret; ++ ++ guard(mutex)(&calibrate_lock); ++ ++ regmap_read(apb_spare, APB_SPARE_RCAL_HSIO, &val); ++ if (val & APB_SPARE_PU_CAL_DONE) ++ return 0; ++ ++ regmap_update_bits(apb_spare, APB_SPARE_PU_CAL, PU_CAL, ++ PU_CAL); ++ ++ ret = regmap_read_poll_timeout(apb_spare, APB_SPARE_RCAL_HSIO, ++ val, (val & APB_SPARE_PU_CAL_DONE), PCIE_POLL_DELAY, ++ PU_CAL_TIMEOUT); ++ ++ if (ret) ++ regmap_update_bits(apb_spare, APB_SPARE_RCAL_HSIO, ++ RCAL_OVRD_TRIM_EN | RCAL_OVRD_STABLE_VAL | ++ RCAL_OVRD_TRIM_MASK | RCAL_OVRD_STABLE_EN, ++ RCAL_OVRD_TRIM_EN | RCAL_OVRD_STABLE_VAL | ++ FIELD_PREP(RCAL_OVRD_NTRIM, 0x6) | ++ FIELD_PREP(RCAL_OVRD_PTRIM, 0xa) | ++ RCAL_OVRD_STABLE_EN); ++ ++ return ret; ++} ++EXPORT_SYMBOL_GPL(k3_phy_calibrate); ++ ++MODULE_DESCRIPTION("SpacemiT K3 PHY common ops"); ++MODULE_LICENSE("GPL"); +diff --git a/drivers/phy/spacemit/phy-k3-common.h b/drivers/phy/spacemit/phy-k3-common.h +new file mode 100644 +index 000000000000..49009c3c313a +--- /dev/null ++++ b/drivers/phy/spacemit/phy-k3-common.h +@@ -0,0 +1,27 @@ ++/* SPDX-License-Identifier: GPL-2.0-only */ ++ ++#ifndef _PHY_K3_COMMON_H ++#define _PHY_K3_COMMON_H ++ ++#include ++ ++struct k3_phy_lane_group_data { ++ u32 lanes; ++ u8 config; ++ u8 mask; ++ u32 offsets[] __counted_by(lanes); ++}; ++ ++struct k3_lane_group { ++ const struct k3_phy_lane_group_data *data; ++ void __iomem *base; ++ struct phy *phy; ++ bool is_pcie; ++}; ++ ++extern const struct phy_ops k3_pcie_phy_ops; ++extern const struct phy_ops k3_usb3_phy_ops; ++ ++int k3_phy_calibrate(struct regmap *apb_spare); ++ ++#endif +-- +2.54.0 + diff --git a/spacemit/k3-pico-itx/patches/0003-pci-k1-device-data.patch b/spacemit/k3-pico-itx/patches/0003-pci-k1-device-data.patch new file mode 100644 index 00000000..49b3ff3e --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0003-pci-k1-device-data.patch @@ -0,0 +1,105 @@ +From From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 1/6] PCI: spacemit-k1: Add device data support +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:21 +0800 +Message-Id: <20260709040027.958400-2-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +To reuse the K1 PCIe driver logic for K3 PCIe controller, add device +data to handle the K1 specific logic and make room for the incoming +logic for K3. + +Signed-off-by: Inochi Amaoto +Reviewed-by: Alex Elder +Tested-by: Aurelien Jarno +--- + drivers/pci/controller/dwc/pcie-spacemit-k1.c | 30 ++++++++++++++++--- + 1 file changed, 26 insertions(+), 4 deletions(-) + +diff --git a/drivers/pci/controller/dwc/pcie-spacemit-k1.c b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +index be20a520255b..f6ae8ff3589a 100644 +--- a/drivers/pci/controller/dwc/pcie-spacemit-k1.c ++++ b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +@@ -49,8 +49,17 @@ + #define PCIE_CONTROL_LOGIC 0x0004 + #define PCIE_SOFT_RESET BIT(0) + ++struct k1_pcie; ++ ++struct k1_pcie_device_data { ++ const struct dw_pcie_host_ops *host_ops; ++ const struct dw_pcie_ops *ops; ++ int (*parse_port)(struct k1_pcie *k1); ++}; ++ + struct k1_pcie { + struct dw_pcie pci; ++ const struct k1_pcie_device_data *data; + struct phy *phy; + void __iomem *link; + struct regmap *pmu; /* Errors ignored; MMIO-backed regmap */ +@@ -278,14 +287,21 @@ static int k1_pcie_parse_port(struct k1_pcie *k1) + + static int k1_pcie_probe(struct platform_device *pdev) + { ++ const struct k1_pcie_device_data *data; + struct device *dev = &pdev->dev; + struct k1_pcie *k1; + int ret; + ++ data = device_get_match_data(dev); ++ if (!data) ++ return -ENODEV; ++ + k1 = devm_kzalloc(dev, sizeof(*k1), GFP_KERNEL); + if (!k1) + return -ENOMEM; + ++ k1->data = data; ++ + k1->pmu = syscon_regmap_lookup_by_phandle_args(dev_of_node(dev), + SYSCON_APMU, 1, + &k1->pmu_off); +@@ -299,11 +315,11 @@ static int k1_pcie_probe(struct platform_device *pdev) + "failed to map \"link\" registers\n"); + + k1->pci.dev = dev; +- k1->pci.ops = &k1_pcie_ops; ++ k1->pci.ops = data->ops; + k1->pci.pp.num_vectors = MAX_MSI_IRQS; + dw_pcie_cap_set(&k1->pci, REQ_RES); + +- k1->pci.pp.ops = &k1_pcie_host_ops; ++ k1->pci.pp.ops = data->host_ops; + + /* Hold the PHY in reset until we start the link */ + regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL, +@@ -320,7 +336,7 @@ static int k1_pcie_probe(struct platform_device *pdev) + + platform_set_drvdata(pdev, k1); + +- ret = k1_pcie_parse_port(k1); ++ ret = data->parse_port(k1); + if (ret) + return dev_err_probe(dev, ret, "failed to parse root port\n"); + +@@ -338,8 +354,14 @@ static void k1_pcie_remove(struct platform_device *pdev) + dw_pcie_host_deinit(&k1->pci.pp); + } + ++static const struct k1_pcie_device_data k1_pcie_device_data = { ++ .host_ops = &k1_pcie_host_ops, ++ .ops = &k1_pcie_ops, ++ .parse_port = k1_pcie_parse_port, ++}; ++ + static const struct of_device_id k1_pcie_of_match_table[] = { +- { .compatible = "spacemit,k1-pcie", }, ++ { .compatible = "spacemit,k1-pcie", .data = &k1_pcie_device_data}, + { } + }; + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0004-pci-k1-multiple-phy-handles.patch b/spacemit/k3-pico-itx/patches/0004-pci-k1-multiple-phy-handles.patch new file mode 100644 index 00000000..bf379b40 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0004-pci-k1-multiple-phy-handles.patch @@ -0,0 +1,156 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 2/6] PCI: spacemit-k1: Add multiple PHY handles support +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:22 +0800 +Message-Id: <20260709040027.958400-3-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +The PCIe controller on Spacemit K3 may use multiple PHYs at the +same time. The feature is not support by the current driver. +So extend the PHY definition to support multiple PHY handles. + +Signed-off-by: Inochi Amaoto +Tested-by: Aurelien Jarno +--- + drivers/pci/controller/dwc/pcie-spacemit-k1.c | 70 ++++++++++++++++--- + 1 file changed, 59 insertions(+), 11 deletions(-) + +diff --git a/drivers/pci/controller/dwc/pcie-spacemit-k1.c b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +index f6ae8ff3589a..e22ecbd09579 100644 +--- a/drivers/pci/controller/dwc/pcie-spacemit-k1.c ++++ b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +@@ -55,12 +55,14 @@ struct k1_pcie_device_data { + const struct dw_pcie_host_ops *host_ops; + const struct dw_pcie_ops *ops; + int (*parse_port)(struct k1_pcie *k1); ++ unsigned int max_phy_count; + }; + + struct k1_pcie { + struct dw_pcie pci; + const struct k1_pcie_device_data *data; +- struct phy *phy; ++ struct phy **phy; ++ unsigned int phy_count; + void __iomem *link; + struct regmap *pmu; /* Errors ignored; MMIO-backed regmap */ + u32 pmu_off; +@@ -119,6 +121,54 @@ static void k1_pcie_disable_resources(struct k1_pcie *k1) + clk_bulk_disable_unprepare(ARRAY_SIZE(pci->app_clks), pci->app_clks); + } + ++static int k1_pcie_get_phy_handle(struct k1_pcie *k1, struct device_node *node) ++{ ++ const struct k1_pcie_device_data *data = k1->data; ++ struct device *dev = k1->pci.dev; ++ unsigned int i; ++ ++ k1->phy = devm_kmalloc_array(dev, data->max_phy_count, ++ sizeof(*k1->phy), GFP_KERNEL); ++ if (!k1->phy) ++ return -ENOMEM; ++ ++ for (i = 0; i < data->max_phy_count; i++) { ++ k1->phy[i] = devm_of_phy_get_by_index(dev, node, i); ++ if (IS_ERR(k1->phy[i])) { ++ if (PTR_ERR(k1->phy[i]) == -ENODEV) ++ break; ++ ++ return PTR_ERR(k1->phy[i]); ++ } ++ } ++ ++ k1->phy_count = i; ++ if (k1->phy_count == 0) ++ return -EINVAL; ++ ++ return 0; ++} ++ ++static int k1_pcie_enable_phy(struct k1_pcie *k1) ++{ ++ unsigned int i; ++ int ret; ++ ++ for (i = 0; i < k1->phy_count; i++) { ++ ret = phy_init(k1->phy[i]); ++ if (ret) ++ goto err_phy; ++ } ++ ++ return 0; ++ ++err_phy: ++ while (i--) ++ phy_exit(k1->phy[i]); ++ ++ return ret; ++} ++ + /* FIXME: Disable ASPM L1 to avoid errors reported on some NVMe drives */ + static void k1_pcie_disable_aspm_l1(struct k1_pcie *k1) + { +@@ -174,7 +224,7 @@ static int k1_pcie_init(struct dw_pcie_rp *pp) + */ + regmap_set_bits(k1->pmu, reset_ctrl, DEVICE_TYPE_RC | PCIE_AUX_PWR_DET); + +- ret = phy_init(k1->phy); ++ ret = k1_pcie_enable_phy(k1); + if (ret) { + k1_pcie_disable_resources(k1); + +@@ -194,12 +244,14 @@ static void k1_pcie_deinit(struct dw_pcie_rp *pp) + { + struct dw_pcie *pci = to_dw_pcie_from_pp(pp); + struct k1_pcie *k1 = to_k1_pcie(pci); ++ int i; + + /* Assert fundamental reset (drive PERST# low) */ + regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CLK_RESET_CONTROL, + PCIE_RC_PERST); + +- phy_exit(k1->phy); ++ for (i = 0; i < k1->phy_count; i++) ++ phy_exit(k1->phy[i]); + + k1_pcie_disable_resources(k1); + } +@@ -266,23 +318,18 @@ static int k1_pcie_parse_port(struct k1_pcie *k1) + { + struct device *dev = k1->pci.dev; + struct device_node *root_port; +- struct phy *phy; ++ int ret; + + /* We assume only one root port */ + root_port = of_get_next_available_child(dev_of_node(dev), NULL); + if (!root_port) + return -EINVAL; + +- phy = devm_of_phy_get(dev, root_port, NULL); ++ ret = k1_pcie_get_phy_handle(k1, root_port); + + of_node_put(root_port); + +- if (IS_ERR(phy)) +- return PTR_ERR(phy); +- +- k1->phy = phy; +- +- return 0; ++ return ret; + } + + static int k1_pcie_probe(struct platform_device *pdev) +@@ -358,6 +405,7 @@ static const struct k1_pcie_device_data k1_pcie_device_data = { + .host_ops = &k1_pcie_host_ops, + .ops = &k1_pcie_ops, + .parse_port = k1_pcie_parse_port, ++ .max_phy_count = 1, + }; + + static const struct of_device_id k1_pcie_of_match_table[] = { +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0004b-pci-k1-device-id-helper.patch b/spacemit/k3-pico-itx/patches/0004b-pci-k1-device-id-helper.patch new file mode 100644 index 00000000..5d8295e7 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0004b-pci-k1-device-id-helper.patch @@ -0,0 +1,70 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 3/6] PCI: spacemit-k1: Add device id update helper +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:23 +0800 +Message-Id: <20260709040027.958400-4-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Both K1 and K3 needs to set vendor id and device id, add a helper function +to simplify this. + +Signed-off-by: Inochi Amaoto +Reviewed-by: Alex Elder +Tested-by: Aurelien Jarno +--- + drivers/pci/controller/dwc/pcie-spacemit-k1.c | 17 +++++++++++++---- + 1 file changed, 13 insertions(+), 4 deletions(-) + +diff --git a/drivers/pci/controller/dwc/pcie-spacemit-k1.c b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +index e22ecbd09579..31aac056b68e 100644 +--- a/drivers/pci/controller/dwc/pcie-spacemit-k1.c ++++ b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +@@ -56,6 +56,7 @@ struct k1_pcie_device_data { + const struct dw_pcie_ops *ops; + int (*parse_port)(struct k1_pcie *k1); + unsigned int max_phy_count; ++ unsigned int device_id; + }; + + struct k1_pcie { +@@ -186,6 +187,16 @@ static void k1_pcie_disable_aspm_l1(struct k1_pcie *k1) + dw_pcie_dbi_ro_wr_dis(pci); + } + ++static void k1_pcie_set_device_id(struct k1_pcie *k1) ++{ ++ struct dw_pcie *pci = &k1->pci; ++ ++ dw_pcie_dbi_ro_wr_en(pci); ++ dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, PCI_VENDOR_ID_SPACEMIT); ++ dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, k1->data->device_id); ++ dw_pcie_dbi_ro_wr_dis(pci); ++} ++ + static int k1_pcie_init(struct dw_pcie_rp *pp) + { + struct dw_pcie *pci = to_dw_pcie_from_pp(pp); +@@ -201,10 +212,7 @@ static int k1_pcie_init(struct dw_pcie_rp *pp) + return ret; + + /* Set the PCI vendor and device ID */ +- dw_pcie_dbi_ro_wr_en(pci); +- dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, PCI_VENDOR_ID_SPACEMIT); +- dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, PCI_DEVICE_ID_SPACEMIT_K1); +- dw_pcie_dbi_ro_wr_dis(pci); ++ k1_pcie_set_device_id(k1); + + /* + * Start by asserting fundamental reset (drive PERST# low). The +@@ -406,6 +414,7 @@ static const struct k1_pcie_device_data k1_pcie_device_data = { + .ops = &k1_pcie_ops, + .parse_port = k1_pcie_parse_port, + .max_phy_count = 1, ++ .device_id = PCI_DEVICE_ID_SPACEMIT_K1, + }; + + static const struct of_device_id k1_pcie_of_match_table[] = { +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0005-dt-bindings-dw-pcie-msi-parent.patch b/spacemit/k3-pico-itx/patches/0005-dt-bindings-dw-pcie-msi-parent.patch new file mode 100644 index 00000000..26a18dc2 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0005-dt-bindings-dw-pcie-msi-parent.patch @@ -0,0 +1,44 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 4/6] dt-bindings: PCI: snps,dw-pcie: Add msi-parent for + MSI handle check +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:24 +0800 +Message-Id: <20260709040027.958400-5-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +The IMSIC device on RISC-V based system does not require ID +remapping for MSI. So this device only needs "msi-parent" +property for IMSIC-based SoC, and the "msi-map" is not a +necessary property. + +Add new condition for MSI handling on IMSIC based SoC. + +Signed-off-by: Inochi Amaoto +Acked-by: Rob Herring (Arm) +Tested-by: Aurelien Jarno +--- + Documentation/devicetree/bindings/pci/snps,dw-pcie.yaml | 7 +++++-- + 1 file changed, 5 insertions(+), 2 deletions(-) + +diff --git a/Documentation/devicetree/bindings/pci/snps,dw-pcie.yaml b/Documentation/devicetree/bindings/pci/snps,dw-pcie.yaml +index b3216141881c..91bbbc8924f6 100644 +--- a/Documentation/devicetree/bindings/pci/snps,dw-pcie.yaml ++++ b/Documentation/devicetree/bindings/pci/snps,dw-pcie.yaml +@@ -27,8 +27,11 @@ allOf: + - $ref: /schemas/pci/snps,dw-pcie-common.yaml# + - if: + not: +- required: +- - msi-map ++ anyOf: ++ - required: ++ - msi-map ++ - required: ++ - msi-parent + then: + properties: + interrupt-names: +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0006-dt-bindings-k3-pcie-host.patch b/spacemit/k3-pico-itx/patches/0006-dt-bindings-k3-pcie-host.patch new file mode 100644 index 00000000..389ef022 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0006-dt-bindings-k3-pcie-host.patch @@ -0,0 +1,124 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 5/6] dt-bindings: PCI: spacemit: Introduce Spacemit K3 + PCIe host controller +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:25 +0800 +Message-Id: <20260709040027.958400-6-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add binding support for the PCIe controller on the SpacemiT K3 SoC. +This controller is almost a standard Synopsys DesignWare PCIe IP, +with some extra link and reset state control. + +Signed-off-by: Inochi Amaoto +--- + .../bindings/pci/spacemit,k1-pcie-host.yaml | 50 ++++++++++++++++--- + 1 file changed, 43 insertions(+), 7 deletions(-) + +diff --git a/Documentation/devicetree/bindings/pci/spacemit,k1-pcie-host.yaml b/Documentation/devicetree/bindings/pci/spacemit,k1-pcie-host.yaml +index c4c00b5fcdc0..54817d6fd9af 100644 +--- a/Documentation/devicetree/bindings/pci/spacemit,k1-pcie-host.yaml ++++ b/Documentation/devicetree/bindings/pci/spacemit,k1-pcie-host.yaml +@@ -14,26 +14,29 @@ description: > + PCIe IP. The controller uses the DesignWare built-in MSI interrupt + controller, and supports 256 MSIs. + +-allOf: +- - $ref: /schemas/pci/snps,dw-pcie.yaml# +- + properties: + compatible: +- const: spacemit,k1-pcie ++ enum: ++ - spacemit,k1-pcie ++ - spacemit,k3-pcie + + reg: ++ minItems: 4 + items: + - description: DesignWare PCIe registers + - description: ATU address space + - description: PCIe configuration space + - description: Link control registers ++ - description: Data Bus Interface (DBI) shadow registers. + + reg-names: ++ minItems: 4 + items: + - const: dbi + - const: atu + - const: config + - const: link ++ - const: dbi2 + + clocks: + items: +@@ -66,6 +69,8 @@ properties: + interrupt-names: + const: msi + ++ msi-parent: true ++ + spacemit,apmu: + $ref: /schemas/types.yaml#/definitions/phandle-array + description: +@@ -84,7 +89,8 @@ patternProperties: + + properties: + phys: +- maxItems: 1 ++ minItems: 1 ++ maxItems: 6 + + vpcie3v3-supply: + description: +@@ -96,13 +102,43 @@ patternProperties: + + unevaluatedProperties: false + ++allOf: ++ - $ref: /schemas/pci/snps,dw-pcie.yaml# ++ - if: ++ properties: ++ compatible: ++ contains: ++ const: spacemit,k1-pcie ++ then: ++ properties: ++ reg: ++ maxItems: 4 ++ ++ reg-names: ++ maxItems: 4 ++ ++ patternProperties: ++ '^pcie@': ++ properties: ++ phys: ++ maxItems: 1 ++ ++ required: ++ - interrupts ++ - interrupt-names ++ else: ++ properties: ++ reg: ++ minItems: 5 ++ ++ reg-names: ++ minItems: 5 ++ + required: + - clocks + - clock-names + - resets + - reset-names +- - interrupts +- - interrupt-names + - spacemit,apmu + + unevaluatedProperties: false +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0007-pci-k3-host-controller.patch b/spacemit/k3-pico-itx/patches/0007-pci-k3-host-controller.patch new file mode 100644 index 00000000..a4d98327 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0007-pci-k3-host-controller.patch @@ -0,0 +1,210 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 6/6] PCI: spacemit-k1: Add Spacemit K3 PCIe host + controller support +From: Inochi Amaoto +Date: Thu, 09 Jul 2026 12:00:26 +0800 +Message-Id: <20260709040027.958400-7-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +The PCIe controller on Spacemit K3 is almost a standard Synopsys +DesignWare PCIe IP with extra link and reset control. Unlike +the PCIe controller on K1, this controller supports external MSI +interrupt controller and can use multiple PHYs at the same time. + +Add driver to support PCIe controller on Spacemit K3 PCIe. + +Signed-off-by: Inochi Amaoto +Tested-by: Aurelien Jarno +--- + drivers/pci/controller/dwc/Kconfig | 4 +- + drivers/pci/controller/dwc/pcie-spacemit-k1.c | 126 ++++++++++++++++++ + 2 files changed, 128 insertions(+), 2 deletions(-) + +diff --git a/drivers/pci/controller/dwc/Kconfig b/drivers/pci/controller/dwc/Kconfig +index aa0b784c85b4..dacbac5cc35c 100644 +--- a/drivers/pci/controller/dwc/Kconfig ++++ b/drivers/pci/controller/dwc/Kconfig +@@ -440,7 +440,7 @@ config PCIE_SOPHGO_DW + Sophgo SoCs. + + config PCIE_SPACEMIT_K1 +- tristate "SpacemiT K1 PCIe controller (host mode)" ++ tristate "SpacemiT PCIe controller (host mode)" + depends on ARCH_SPACEMIT || COMPILE_TEST + depends on HAS_IOMEM + select PCIE_DW_HOST +@@ -448,7 +448,7 @@ config PCIE_SPACEMIT_K1 + default ARCH_SPACEMIT + help + Enables support for the DesignWare based PCIe controller in +- the SpacemiT K1 SoC operating in host mode. Three controllers ++ the SpacemiT SoC operating in host mode. Three controllers + are available on the K1 SoC; the first of these shares a PHY + with a USB 3.0 host controller (one or the other can be used). + +diff --git a/drivers/pci/controller/dwc/pcie-spacemit-k1.c b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +index 31aac056b68e..680acc93f539 100644 +--- a/drivers/pci/controller/dwc/pcie-spacemit-k1.c ++++ b/drivers/pci/controller/dwc/pcie-spacemit-k1.c +@@ -32,8 +33,18 @@ + #define SMLH_LINK_UP BIT(1) + #define RDLH_LINK_UP BIT(12) + ++#define INTR_STATUS 0x0010 ++ + #define INTR_ENABLE 0x0014 + #define MSI_CTRL_INT BIT(11) ++#define RDLH_LINK_UP_INT BIT(20) ++ ++#define K3_PHY_AHB_IRQSTATUS_INTX 0x0008 ++ ++#define K3_ADDR_INTR_STATUS1 0x0018 ++ ++#define K3_CACHE_MSTR_AWCACHE_MODE GENMASK(14, 11) ++#define K3_CACHE_MSTR_AWCACHE_BEHAVIOR 0xf + + /* Some controls require APMU regmap access */ + #define SYSCON_APMU "spacemit,apmu" +@@ -48,6 +59,9 @@ + + #define PCIE_CONTROL_LOGIC 0x0004 + #define PCIE_SOFT_RESET BIT(0) ++#define PCIE_PERSTN_OE BIT(24) ++#define PCIE_PERSTN_OUT BIT(25) ++#define PCIE_IGNORE_PERSTN BIT(31) + + struct k1_pcie; + +@@ -340,6 +354,109 @@ static int k1_pcie_parse_port(struct k1_pcie *k1) + return ret; + } + ++static int k3_pcie_init(struct dw_pcie_rp *pp) ++{ ++ struct dw_pcie *pci = to_dw_pcie_from_pp(pp); ++ struct k1_pcie *k1 = to_k1_pcie(pci); ++ u32 reset_ctrl = k1->pmu_off + PCIE_CLK_RESET_CONTROL; ++ u32 val; ++ int ret; ++ ++ regmap_clear_bits(k1->pmu, reset_ctrl, LTSSM_EN); ++ ++ k1_pcie_toggle_soft_reset(k1); ++ ++ ret = k1_pcie_enable_resources(k1); ++ if (ret) ++ return ret; ++ ++ regmap_set_bits(k1->pmu, reset_ctrl, PCIE_AUX_PWR_DET); ++ regmap_clear_bits(k1->pmu, reset_ctrl, APP_HOLD_PHY_RST); ++ ++ ret = k1_pcie_enable_phy(k1); ++ if (ret) { ++ k1_pcie_disable_resources(k1); ++ return ret; ++ } ++ ++ /* K3: Set IGNORE_PERSTN and drive PERSTN_OE high (assert reset) */ ++ regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CONTROL_LOGIC, ++ PCIE_IGNORE_PERSTN | PCIE_PERSTN_OE | PCIE_PERSTN_OUT); ++ usleep_range(1000, 2000); ++ regmap_clear_bits(k1->pmu, k1->pmu_off + PCIE_CONTROL_LOGIC, PCIE_PERSTN_OUT); ++ ++ msleep(PCIE_T_PVPERL_MS); ++ ++ /* ++ * Put the controller in root complex mode, and indicate that ++ * Vaux (3.3v) is present. ++ */ ++ regmap_set_bits(k1->pmu, k1->pmu_off + PCIE_CONTROL_LOGIC, ++ PCIE_PERSTN_OUT | PCIE_PERSTN_OE); ++ ++ val = dw_pcie_readl_dbi(pci, GEN3_EQ_CONTROL_OFF); ++ val = u32_replace_bits(val, BIT(7), ++ GEN3_EQ_CONTROL_OFF_PSET_REQ_VEC); ++ dw_pcie_writel_dbi(pci, GEN3_EQ_CONTROL_OFF, val); ++ ++ k1_pcie_set_device_id(k1); ++ ++ /* Finally, as a workaround, disable ASPM L1 */ ++ k1_pcie_disable_aspm_l1(k1); ++ ++ return 0; ++} ++ ++static int k3_pcie_msi_host_init(struct dw_pcie_rp *pp) ++{ ++ struct dw_pcie *pci = to_dw_pcie_from_pp(pp); ++ u32 val; ++ ++ dw_pcie_dbi_ro_wr_en(pci); ++ ++ val = dw_pcie_readl_dbi(pci, COHERENCY_CONTROL_3_OFF); ++ val = u32_replace_bits(val, K3_CACHE_MSTR_AWCACHE_BEHAVIOR, ++ K3_CACHE_MSTR_AWCACHE_MODE); ++ dw_pcie_writel_dbi(pci, COHERENCY_CONTROL_3_OFF, val); ++ ++ dw_pcie_dbi_ro_wr_dis(pci); ++ ++ return 0; ++} ++ ++static const struct dw_pcie_host_ops k3_pcie_host_ops = { ++ .init = k3_pcie_init, ++ .deinit = k1_pcie_deinit, ++ .msi_init = k3_pcie_msi_host_init, ++}; ++ ++static const struct dw_pcie_ops k3_pcie_ops = { ++ .link_up = k1_pcie_link_up, ++ .start_link = k1_pcie_start_link, ++ .stop_link = k1_pcie_stop_link, ++}; ++ ++static void k3_pcie_clear_irq_status(struct k1_pcie *k1, ++ u32 *status0, u32 *status1, u32 *status2) ++{ ++ *status0 = readl_relaxed(k1->link + K3_PHY_AHB_IRQSTATUS_INTX); ++ *status1 = readl_relaxed(k1->link + INTR_STATUS); ++ *status2 = readl_relaxed(k1->link + K3_ADDR_INTR_STATUS1); ++ ++ writel_relaxed(*status0, k1->link + K3_PHY_AHB_IRQSTATUS_INTX); ++ writel_relaxed(*status1, k1->link + INTR_STATUS); ++ writel_relaxed(*status2, k1->link + K3_ADDR_INTR_STATUS1); ++} ++ ++static int k3_pcie_parse_port(struct k1_pcie *k1) ++{ ++ u32 status0, status1, status2; ++ ++ k3_pcie_clear_irq_status(k1, &status0, &status1, &status2); ++ ++ return k1_pcie_parse_port(k1); ++} ++ + static int k1_pcie_probe(struct platform_device *pdev) + { + const struct k1_pcie_device_data *data; +@@ -417,8 +534,17 @@ static const struct k1_pcie_device_data k1_pcie_device_data = { + .device_id = PCI_DEVICE_ID_SPACEMIT_K1, + }; + ++static const struct k1_pcie_device_data k3_pcie_device_data = { ++ .host_ops = &k3_pcie_host_ops, ++ .ops = &k3_pcie_ops, ++ .parse_port = k3_pcie_parse_port, ++ .max_phy_count = 6, ++ .device_id = PCI_DEVICE_ID_SPACEMIT_K3, ++}; ++ + static const struct of_device_id k1_pcie_of_match_table[] = { + { .compatible = "spacemit,k1-pcie", .data = &k1_pcie_device_data}, ++ { .compatible = "spacemit,k3-pcie", .data = &k3_pcie_device_data}, + { } + }; + +-- +2.55.0 + diff --git a/spacemit/k3-pico-itx/patches/0008-serial-8250_of-gate-clock.patch b/spacemit/k3-pico-itx/patches/0008-serial-8250_of-gate-clock.patch new file mode 100644 index 00000000..4adbcb4d --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0008-serial-8250_of-gate-clock.patch @@ -0,0 +1,38 @@ +From: liberodark +Subject: [PATCH] serial: 8250_of: support an optional gate clock + +Enable an optional "gate" clock so UARTs whose register access is +gated by a separate clock (SpacemiT K3 CLK_MPMU_SLOW_UART) work. + +Signed-off-by: liberodark +--- +diff --git a/drivers/tty/serial/8250/8250_of.c b/drivers/tty/serial/8250/8250_of.c +index 81644d4..a288d2b 100644 +--- a/drivers/tty/serial/8250/8250_of.c ++++ b/drivers/tty/serial/8250/8250_of.c +@@ -25,6 +25,7 @@ + struct of_serial_info { + struct clk *clk; + struct clk *bus_clk; ++ struct clk *gate_clk; /* Gate clock for register access (SpacemiT) */ + struct reset_control *rst; + int type; + int line; +@@ -132,6 +133,17 @@ static int of_platform_serial_setup(struct platform_device *ofdev, + goto err_pmruntime; + } + ++ /* ++ * Some platforms (e.g. SpacemiT K3) gate register access behind ++ * an additional "gate" clock. Without it, MMIO access to the ++ * UART stalls during port registration. Enable it if present. ++ */ ++ info->gate_clk = devm_clk_get_optional_enabled(dev, "gate"); ++ if (IS_ERR(info->gate_clk)) { ++ ret = dev_err_probe(dev, PTR_ERR(info->gate_clk), "failed to get gate clock\n"); ++ goto err_pmruntime; ++ } ++ + /* If the bus clock is required, core clock must be named */ + info->clk = devm_clk_get_enabled(dev, bus_clk ? "core" : NULL); + if (IS_ERR(info->clk)) { diff --git a/spacemit/k3-pico-itx/patches/0009-dts-k3-uart0-gate-clock.patch b/spacemit/k3-pico-itx/patches/0009-dts-k3-uart0-gate-clock.patch new file mode 100644 index 00000000..c72efeac --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0009-dts-k3-uart0-gate-clock.patch @@ -0,0 +1,21 @@ +From: liberodark +Subject: [PATCH] riscv: dts: spacemit: k3: wire uart0 gate clock + +Signed-off-by: liberodark +--- +diff --git a/arch/riscv/boot/dts/spacemit/k3.dtsi b/arch/riscv/boot/dts/spacemit/k3.dtsi +index 19fc9b4..b0d5a6a 100644 +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -682,8 +682,9 @@ uart0: serial@d4017000 { + reg-shift = <2>; + reg-io-width = <4>; + clocks = <&syscon_apbc CLK_APBC_UART0>, +- <&syscon_apbc CLK_APBC_UART0_BUS>; +- clock-names = "core", "bus"; ++ <&syscon_apbc CLK_APBC_UART0_BUS>, ++ <&syscon_mpmu CLK_MPMU_SLOW_UART>; ++ clock-names = "core", "bus", "gate"; + resets = <&syscon_apbc RESET_APBC_UART0>; + interrupts = <42 IRQ_TYPE_LEVEL_HIGH>; + status = "disabled"; diff --git a/spacemit/k3-pico-itx/patches/0010-dts-k3-add-pcie-combphy.patch b/spacemit/k3-pico-itx/patches/0010-dts-k3-add-pcie-combphy.patch new file mode 100644 index 00000000..205ea35e --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0010-dts-k3-add-pcie-combphy.patch @@ -0,0 +1,318 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v2 2/2] riscv: dts: spacemit: k3: Add PCIe device node +From: Inochi Amaoto +Date: Mon, 27 Jul 2026 17:47:24 +0800 +Message-Id: <20260727094726.890179-3-inochiama@gmail.com> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add all PCIe device node for Spacemit K3. + +Signed-off-by: Inochi Amaoto +--- + arch/riscv/boot/dts/spacemit/k3-pico-itx.dts | 38 ++++ + arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi | 33 ++++ + arch/riscv/boot/dts/spacemit/k3.dtsi | 195 +++++++++++++++++++ + 3 files changed, 266 insertions(+) + +diff --git a/arch/riscv/boot/dts/spacemit/k3-pico-itx.dts b/arch/riscv/boot/dts/spacemit/k3-pico-itx.dts +index 2a6d35a64d5c..1eb5abbc61f9 100644 +--- a/arch/riscv/boot/dts/spacemit/k3-pico-itx.dts ++++ b/arch/riscv/boot/dts/spacemit/k3-pico-itx.dts +@@ -205,6 +205,44 @@ phy0: phy@1 { + }; + }; + ++&pcie0_rc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&pcie0_0_cfg>; ++ num-lanes = <4>; ++ status = "okay"; ++ ++ pcie@0 { ++ vpcie3v3-supply = <®_aux_vcc3v3>; ++ phys = <&combophy 0 PHY_TYPE_PCIE>, ++ <&combophy 1 PHY_TYPE_PCIE>; ++ }; ++}; ++ ++&pcie2_rc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&pcie2_0_cfg>; ++ num-lanes = <2>; ++ status = "okay"; ++ ++ pcie@0 { ++ vpcie3v3-supply = <®_aux_vcc3v3>; ++ phys = <&combophy 2 PHY_TYPE_PCIE>, ++ <&combophy 3 PHY_TYPE_PCIE>; ++ }; ++}; ++ ++&pcie4_rc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&pcie4_0_cfg>; ++ num-lanes = <1>; ++ status = "okay"; ++ ++ pcie@0 { ++ vpcie3v3-supply = <®_aux_vcc3v3>; ++ phys = <&combophy 5 PHY_TYPE_PCIE>; ++ }; ++}; ++ + &uart0 { + pinctrl-names = "default"; + pinctrl-0 = <&uart0_0_cfg>; +diff --git a/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi b/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi +index 3ee1471f3798..68aa21a5279f 100644 +--- a/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi +@@ -689,4 +689,37 @@ uart0-0-pins { + drive-strength = <25>; + }; + }; ++ ++ pcie0_0_cfg: pcie0-0-cfg { ++ pcie0-0-pins { ++ pinmux = , /* pcie0 perst */ ++ ; /* pcie0 clkreq */ ++ ++ bias-pull-up = <1>; ++ drive-strength = <33>; ++ power-source = <1800>; ++ }; ++ }; ++ ++ pcie2_0_cfg: pcie2-0-cfg { ++ pcie2-0-pins { ++ pinmux = , /* pcie2 perst */ ++ ; /* pcie2 clkreq */ ++ ++ drive-strength = <38>; ++ power-source = <3300>; ++ }; ++ }; ++ ++ pcie4_0_cfg: pcie4-0-cfg { ++ pcie4-0-pins { ++ pinmux = , /* pcie4 perst */ ++ , /* pcie4 wake */ ++ ; /* pcie4 clkreq */ ++ ++ bias-pull-up = <1>; ++ drive-strength = <33>; ++ power-source = <1800>; ++ }; ++ }; + }; +diff --git a/arch/riscv/boot/dts/spacemit/k3.dtsi b/arch/riscv/boot/dts/spacemit/k3.dtsi +index 7863ab7dd308..314dea88b4a4 100644 +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -439,6 +439,201 @@ soc: soc { + dma-noncoherent; + ranges; + ++ pcie0_rc: pcie@80000000 { ++ compatible = "spacemit,k3-pcie"; ++ reg = <0x0 0x80000000 0x0 0x00001000>, ++ <0x0 0x80300000 0x0 0x00003f20>, ++ <0x11 0x00000000 0x0 0x00010000>, ++ <0x0 0x82900000 0x0 0x00001000>, ++ <0x0 0x80100000 0x0 0x00001000>; ++ reg-names = "dbi", "atu", "config", "link", "dbi2"; ++ device_type = "pci"; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ clocks = <&syscon_apmu CLK_APMU_PCIE_PORTA_DBI>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTA_MSTE>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTA_SLV>; ++ clock-names = "dbi", "mstr", "slv"; ++ msi-parent = <&simsic>; ++ ranges = <0x01000000 0x00 0x00010000 0x11 0x00010000 0x0 0x00100000>, ++ <0x02000000 0x0 0x00110000 0x11 0x00110000 0x0 0x7fef0000>, ++ <0x43000000 0x18 0x00000000 0x18 0x00000000 0x1 0x00000000>; ++ resets = <&syscon_apmu RESET_APMU_PCIE_A_DBI>, ++ <&syscon_apmu RESET_APMU_PCIE_A_MASTER>, ++ <&syscon_apmu RESET_APMU_PCIE_A_SLAVE>; ++ reset-names = "dbi", "mstr", "slv"; ++ max-link-speed = <3>; ++ linux,pci-domain = <0>; ++ spacemit,apmu = <&syscon_apmu 0x1f0>; ++ status = "disabled"; ++ ++ pcie0_port: pcie@0 { ++ device_type = "pci"; ++ compatible = "pciclass,0604"; ++ reg = <0x0 0x0 0x0 0x0 0x0>; ++ bus-range = <0x01 0xff>; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ ranges; ++ }; ++ }; ++ ++ pcie1_rc: pcie@80400000 { ++ compatible = "spacemit,k3-pcie"; ++ reg = <0x0 0x80400000 0x0 0x00001000>, ++ <0x0 0x80700000 0x0 0x00003f20>, ++ <0x11 0x80000000 0x0 0x00010000>, ++ <0x0 0x82c00000 0x0 0x00001000>, ++ <0x0 0x80500000 0x0 0x00001000>; ++ reg-names = "dbi", "atu", "config", "link", "dbi2"; ++ device_type = "pci"; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ clocks = <&syscon_apmu CLK_APMU_PCIE_PORTB_DBI>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTB_MSTE>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTB_SLV>; ++ clock-names = "dbi", "mstr", "slv"; ++ msi-parent = <&simsic>; ++ ranges = <0x01000000 0x0 0x00010000 0x11 0x80010000 0x0 0x00100000>, ++ <0x02000000 0x0 0x80110000 0x11 0x80110000 0x0 0x7fef0000>, ++ <0x43000000 0x16 0x00000000 0x16 0x00000000 0x1 0x00000000>; ++ resets = <&syscon_apmu RESET_APMU_PCIE_B_DBI>, ++ <&syscon_apmu RESET_APMU_PCIE_B_MASTER>, ++ <&syscon_apmu RESET_APMU_PCIE_B_SLAVE>; ++ reset-names = "dbi", "mstr", "slv"; ++ max-link-speed = <3>; ++ linux,pci-domain = <1>; ++ spacemit,apmu = <&syscon_apmu 0x1d0>; ++ status = "disabled"; ++ ++ pcie1_port: pcie@0 { ++ device_type = "pci"; ++ compatible = "pciclass,0604"; ++ reg = <0x0 0x0 0x0 0x0 0x0>; ++ bus-range = <0x01 0xff>; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ ranges; ++ }; ++ }; ++ ++ pcie2_rc: pcie@80800000 { ++ compatible = "spacemit,k3-pcie"; ++ reg = <0x0 0x80800000 0x0 0x00001000>, ++ <0x0 0x80b00000 0x0 0x00003f20>, ++ <0x12 0x00000000 0x0 0x00010000>, ++ <0x0 0x82d00000 0x0 0x00001000>, ++ <0x0 0x80900000 0x0 0x00001000>; ++ reg-names = "dbi", "atu", "config", "link", "dbi2"; ++ device_type = "pci"; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ clocks = <&syscon_apmu CLK_APMU_PCIE_PORTC_DBI>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTC_MSTE>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTC_SLV>; ++ clock-names = "dbi", "mstr", "slv"; ++ msi-parent = <&simsic>; ++ ranges = <0x01000000 0x00 0x00000000 0x12 0x00010000 0x0 0x00100000>, ++ <0x02000000 0x0 0x00110000 0x12 0x00110000 0x0 0x7fef0000>, ++ <0x43000000 0x15 0x00000000 0x15 0x00000000 0x1 0x00000000>; ++ resets = <&syscon_apmu RESET_APMU_PCIE_C_DBI>, ++ <&syscon_apmu RESET_APMU_PCIE_C_MASTER>, ++ <&syscon_apmu RESET_APMU_PCIE_C_SLAVE>; ++ reset-names = "dbi", "mstr", "slv"; ++ linux,pci-domain = <2>; ++ max-link-speed = <3>; ++ spacemit,apmu = <&syscon_apmu 0x1c8>; ++ status = "disabled"; ++ ++ pcie2_port: pcie@0 { ++ device_type = "pci"; ++ compatible = "pciclass,0604"; ++ reg = <0x0 0x0 0x0 0x0 0x0>; ++ bus-range = <0x01 0xff>; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ ranges; ++ }; ++ }; ++ ++ pcie3_rc: pcie@80c00000 { ++ compatible = "spacemit,k3-pcie"; ++ reg = <0x0 0x80c00000 0x0 0x00001000>, ++ <0x0 0x80f00000 0x0 0x00003f20>, ++ <0x12 0x80000000 0x0 0x00010000>, ++ <0x0 0x82a00000 0x0 0x00001000>, ++ <0x0 0x80d00000 0x0 0x00001000>; ++ reg-names = "dbi", "atu", "config", "link", "dbi2"; ++ device_type = "pci"; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ clocks = <&syscon_apmu CLK_APMU_PCIE_PORTD_DBI>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTD_MSTE>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTD_SLV>; ++ clock-names = "dbi", "mstr", "slv"; ++ msi-parent = <&simsic>; ++ ranges = <0x01000000 0x0 0x00010000 0x12 0x80010000 0x0 0x100000>, ++ <0x02000000 0x0 0x80110000 0x12 0x80110000 0x0 0x3fef0000>, ++ <0x43000000 0x14 0x00000000 0x14 0x00000000 0x1 0x00000000>; ++ resets = <&syscon_apmu RESET_APMU_PCIE_D_DBI>, ++ <&syscon_apmu RESET_APMU_PCIE_D_MASTER>, ++ <&syscon_apmu RESET_APMU_PCIE_D_SLAVE>; ++ reset-names = "dbi", "mstr", "slv"; ++ linux,pci-domain = <3>; ++ bus-range = <0x00 0xff>; ++ max-link-speed = <3>; ++ spacemit,apmu = <&syscon_apmu 0x1e0>; ++ status = "disabled"; ++ ++ pcie3_port: pcie@0 { ++ device_type = "pci"; ++ compatible = "pciclass,0604"; ++ reg = <0x0 0x0 0x0 0x0 0x0>; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ ranges; ++ }; ++ }; ++ ++ pcie4_rc: pcie@81000000 { ++ compatible = "spacemit,k3-pcie"; ++ reg = <0x0 0x81000000 0x0 0x00001000>, ++ <0x0 0x81300000 0x0 0x00003f20>, ++ <0x12 0xc0000000 0x0 0x00010000>, ++ <0x0 0x82b00000 0x0 0x00001000>, ++ <0x0 0x81100000 0x0 0x00001000>; ++ reg-names = "dbi", "atu", "config", "link", "dbi2"; ++ device_type = "pci"; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ clocks = <&syscon_apmu CLK_APMU_PCIE_PORTE_DBI>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTE_MSTE>, ++ <&syscon_apmu CLK_APMU_PCIE_PORTE_SLV>; ++ clock-names = "dbi", "mstr", "slv"; ++ msi-parent = <&simsic>; ++ ranges = <0x01000000 0x0 0x00000000 0x12 0xc0010000 0x0 0x100000>, ++ <0x02000000 0x0 0xc0110000 0x12 0xc0110000 0x0 0x3fef0000>, ++ <0x43000000 0x13 0x00000000 0x13 0x00000000 0x1 0x00000000>; ++ resets = <&syscon_apmu RESET_APMU_PCIE_E_DBI>, ++ <&syscon_apmu RESET_APMU_PCIE_E_MASTER>, ++ <&syscon_apmu RESET_APMU_PCIE_E_SLAVE>; ++ reset-names = "dbi", "mstr", "slv"; ++ linux,pci-domain = <4>; ++ max-link-speed = <3>; ++ spacemit,apmu = <&syscon_apmu 0x1e8>; ++ status = "disabled"; ++ ++ pcie4_port: pcie@0 { ++ device_type = "pci"; ++ compatible = "pciclass,0604"; ++ reg = <0x0 0x0 0x0 0x0 0x0>; ++ bus-range = <0x01 0xff>; ++ #address-cells = <3>; ++ #size-cells = <2>; ++ ranges; ++ }; ++ }; ++ + usb3d: usb@81a00000 { + compatible = "spacemit,k3-dwc3"; + reg = <0x0 0x81a00000 0x0 0x10000>; +-- +2.55.0 + diff --git a/spacemit/k3-pico-itx/patches/0011-dts-k3-reserved-memory.patch b/spacemit/k3-pico-itx/patches/0011-dts-k3-reserved-memory.patch new file mode 100644 index 00000000..0189db80 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0011-dts-k3-reserved-memory.patch @@ -0,0 +1,129 @@ +From: liberodark +Subject: [PATCH] riscv: dts: spacemit: k3: add reserved-memory regions + +The RCPUs and RPMI/DPU firmware use low RAM (0x100200000-0x101ffffff). +Without these no-map reservations the kernel allocates there and the +RCPUs corrupt it (random 0xdeadbeef crashes in early boot). Regions +match the vendor 6.18 device tree. + +Signed-off-by: liberodark +--- +diff --git a/arch/riscv/boot/dts/spacemit/k3.dtsi b/arch/riscv/boot/dts/spacemit/k3.dtsi +index 19fc9b4..e41a6be 100644 +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -16,6 +16,114 @@ / { + model = "SpacemiT K3"; + compatible = "spacemit,k3"; + ++ reserved-memory { ++ #address-cells = <2>; ++ #size-cells = <2>; ++ ranges; ++ ++ /* RCPU0 (management core) code, data and bss */ ++ rcpu0_pwr: rcpu0-pwr@100200000 { ++ reg = <0x1 0x200000 0x0 0x300000>; ++ no-map; ++ }; ++ ++ /* RCPU0 heap */ ++ rcpu0_heap: rcpu0-heap@100500000 { ++ reg = <0x1 0x500000 0x0 0x200000>; ++ no-map; ++ }; ++ ++ /* RCPU0 vrings */ ++ rcpu0_vdev0vring0: vdev0vring0@c0876000 { ++ reg = <0x0 0xc0876000 0x0 0x3000>; ++ no-map; ++ }; ++ ++ rcpu0_vdev0vring1: vdev0vring1@c0879000 { ++ reg = <0x0 0xc0879000 0x0 0x3000>; ++ no-map; ++ }; ++ ++ rcpu0_vdev0vring2: vdev0vring2@c087c000 { ++ reg = <0x0 0xc087c000 0x0 0x1000>; ++ no-map; ++ }; ++ ++ /* RCPU0 shared memory buffer */ ++ rcpu0_vdev0buffer: vdev0buffer@100700000 { ++ compatible = "shared-dma-pool"; ++ reg = <0x1 0x700000 0x0 0xfc000>; ++ no-map; ++ }; ++ ++ /* RCPU0 resource table */ ++ rcpu0_rsc_table: rcpu0-rsc-table@1007fc000 { ++ reg = <0x1 0x7fc000 0x0 0x4000>; ++ no-map; ++ }; ++ ++ /* RPMI shared memory (mailbox with platform firmware) */ ++ rpmi0: rpmi0@100800000 { ++ reg = <0x1 0x00800000 0x0 0x4000>; ++ no-map; ++ }; ++ ++ /* RCPU1 code, data and bss */ ++ rcpu1_runtime: rcpu1-runtime@100804000 { ++ reg = <0x1 0x804000 0x0 0x300000>; ++ no-map; ++ }; ++ ++ /* RCPU1 heap */ ++ rcpu1_heap: rcpu1-heap@100b04000 { ++ reg = <0x1 0xb04000 0x0 0x200000>; ++ no-map; ++ }; ++ ++ /* RCPU1 vrings */ ++ rcpu1_vdev0vring0: vdev0vring0@c086c000 { ++ reg = <0x0 0xc086c000 0x0 0x3000>; ++ no-map; ++ }; ++ ++ rcpu1_vdev0vring1: vdev0vring1@c086f000 { ++ reg = <0x0 0xc086f000 0x0 0x3000>; ++ no-map; ++ }; ++ ++ /* RCPU1 shared memory buffer */ ++ rcpu1_vdev0buffer: vdev0buffer@100d04000 { ++ compatible = "shared-dma-pool"; ++ reg = <0x1 0xd04000 0x0 0xfc000>; ++ no-map; ++ }; ++ ++ /* RCPU1 resource table */ ++ rcpu1_rsc_table: rcpu1-rsc-table@100e00000 { ++ reg = <0x1 0xe00000 0x0 0x4000>; ++ no-map; ++ }; ++ ++ /* RCPU DTB space */ ++ rcpu_dtb_table: rcpu-dtb-table@100e04000 { ++ reg = <0x1 0xe04000 0x0 0x300000>; ++ no-map; ++ }; ++ ++ /* DPU: MMU table and command list */ ++ dpu_resv0: dpu-reserved@101b00000 { ++ compatible = "shared-dma-pool"; ++ reg = <0x1 0x01b00000 0x0 0x00280000>; ++ no-map; ++ }; ++ ++ dpu_resv1: dpu-reserved1@101d80000 { ++ compatible = "shared-dma-pool"; ++ reg = <0x1 0x01d80000 0x0 0x00280000>; ++ no-map; ++ }; ++ }; ++ + cpus: cpus { + #address-cells = <1>; + #size-cells = <0>; diff --git a/spacemit/k3-pico-itx/patches/0012-thermal-spacemit-k3.patch b/spacemit/k3-pico-itx/patches/0012-thermal-spacemit-k3.patch new file mode 100644 index 00000000..e4abd30d --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0012-thermal-spacemit-k3.patch @@ -0,0 +1,470 @@ +From: liberodark +Subject: [PATCH] thermal: spacemit: k3: tsensor support (v2) + +Port of the SpacemiT K3 thermal sensor driver with devicetree zones +(top/vpu/gpu/cluster, critical trip 115C). + +v2: review fixes : +- switch both clocks to devm_clk_get_enabled() (enable results were + ignored and the clocks leaked on every probe error path) +- check the sensor descriptor allocation and the init_sensors() + return (required DT properties: on failure the zone loop iterated + over an undefined sensor range) +- check reset_control_deassert() and assert the reset through a devm + action so teardown runs in the correct order (zones unregistered + before clocks/reset), replacing the mis-ordered remove() callback +- drop the commented-out IRQ block and reduce the debug bit-toggle + sequence to the plain register clear it amounted to + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -16,6 +16,64 @@ + model = "SpacemiT K3"; + compatible = "spacemit,k3"; + ++ thermal-zones { ++ top-thermal { ++ polling-delay = <1000>; ++ polling-delay-passive = <500>; ++ thermal-sensors = <&tsensor 0>; ++ ++ trips { ++ top-crit { ++ temperature = <115000>; ++ hysteresis = <2000>; ++ type = "critical"; ++ }; ++ }; ++ }; ++ ++ vpu-thermal { ++ polling-delay = <1000>; ++ polling-delay-passive = <500>; ++ thermal-sensors = <&tsensor 2>; ++ ++ trips { ++ vpu-crit { ++ temperature = <115000>; ++ hysteresis = <2000>; ++ type = "critical"; ++ }; ++ }; ++ }; ++ ++ gpu-thermal { ++ polling-delay = <1000>; ++ polling-delay-passive = <500>; ++ thermal-sensors = <&tsensor 3>; ++ ++ trips { ++ gpu-crit { ++ temperature = <115000>; ++ hysteresis = <2000>; ++ type = "critical"; ++ }; ++ }; ++ }; ++ ++ cluster-thermal { ++ polling-delay = <1000>; ++ polling-delay-passive = <500>; ++ thermal-sensors = <&tsensor 4>; ++ ++ trips { ++ cluster-crit { ++ temperature = <115000>; ++ hysteresis = <2000>; ++ type = "critical"; ++ }; ++ }; ++ }; ++ }; ++ + reserved-memory { + #address-cells = <2>; + #size-cells = <2>; +@@ -784,6 +842,22 @@ + #reset-cells = <1>; + }; + ++ tsensor: thermal-sensor@d4018000 { ++ compatible = "spacemit,k3-tsensor"; ++ reg = <0x0 0xd4018000 0x0 0x100>; ++ interrupt-parent = <&saplic>; ++ interrupts = <61 IRQ_TYPE_LEVEL_HIGH>; ++ interrupt-names = "tsensor"; ++ clocks = <&syscon_apbc CLK_APBC_TSEN>, ++ <&syscon_apbc CLK_APBC_TSEN_BUS>; ++ clock-names = "func", "bus"; ++ resets = <&syscon_apbc RESET_APBC_TSEN>; ++ sensor_range = <0x0 0x7>; ++ tsensor_map = <1 0 1 1 1 1 1 1>; ++ temperature_offset = <274>; ++ #thermal-sensor-cells = <1>; ++ }; ++ + uart0: serial@d4017000 { + compatible = "spacemit,k3-uart", "intel,xscale-uart"; + reg = <0x0 0xd4017000 0x0 0x100>; +--- a/drivers/thermal/Kconfig ++++ b/drivers/thermal/Kconfig +@@ -306,6 +306,15 @@ + threshold values. The direction of the comparison is configurable + (greater / lesser than). + ++config SPACEMIT_K3_THERMAL ++ tristate "SpacemiT K3 thermal sensor support" ++ depends on ARCH_SPACEMIT || COMPILE_TEST ++ depends on OF && HAS_IOMEM ++ help ++ Enable this to support the thermal sensor (tsensor) block on ++ the SpacemiT K3 SoC. It exposes on-die temperature sensors as ++ thermal zones and hwmon devices. ++ + config K3_THERMAL + tristate "Texas Instruments K3 thermal support" + depends on ARCH_K3 || COMPILE_TEST +--- a/drivers/thermal/Makefile ++++ b/drivers/thermal/Makefile +@@ -35,6 +35,7 @@ + thermal_sys-$(CONFIG_PCIE_THERMAL) += pcie_cooling.o + + obj-$(CONFIG_K3_THERMAL) += k3_bandgap.o k3_j72xx_bandgap.o ++obj-$(CONFIG_SPACEMIT_K3_THERMAL) += spacemit-k3-thermal.o + # platform thermal drivers + obj-y += broadcom/ + obj-$(CONFIG_THERMAL_MMIO) += thermal_mmio.o +--- /dev/null ++++ b/drivers/thermal/spacemit-k3-thermal.c +@@ -0,0 +1,268 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "thermal_hwmon.h" ++#include "thermal_core.h" ++#include "spacemit-k3-thermal.h" ++ ++static int init_sensors(struct platform_device *pdev) ++{ ++ int ret; ++ unsigned int val; ++ struct k3_thermal_sensor *s = platform_get_drvdata(pdev); ++ ++ /* read the sensor range */ ++ ret = of_property_read_u32_array(pdev->dev.of_node, "sensor_range", s->sr, 2); ++ if (ret < 0) { ++ dev_err(&pdev->dev, "get sensor range error\n"); ++ return ret; ++ } ++ ++ if (s->sr[1] >= MAX_SENSOR_NUMBER) { ++ dev_err(&pdev->dev, "un-fitable sensor range\n"); ++ return -EINVAL; ++ } ++ ++ ret = of_property_read_u32_array(pdev->dev.of_node, "tsensor_map", ++ s->tsen_enable_map, s->sr[1] - s->sr[0] + 1); ++ if (ret) { ++ dev_err(&pdev->dev, "Failed to get definition of tsensor_map\n"); ++ return -EINVAL; ++ } ++ ++ /* first: disable all the interrupts */ ++ writel(0xffffffff, s->base + REG_TSEN_LITE_INT_CLR); ++ writel(0xffffffff, s->base + REG_TSEN_LITE_INT_ENB); ++ ++ /* select clk div 26M/4 */ ++ val = readl(s->base + REG_TSEN_LITE_CFG); ++ val &= ~BITS_D_CK_DIV_SEL; ++ val |= BITS_CK_DIV_SEL_DIV4; ++ ++ /* vref calibration */ ++ val &= ~BITS_D_REG_VREF_CTRL; ++ val |= ((CALIB_VREF_DEFAULT & 0xff) << BITS_D_REG_VREF_OFFSET); ++ ++ writel(val, s->base + REG_TSEN_LITE_CFG); ++ ++ return 0; ++} ++ ++static void enable_sensors(struct platform_device *pdev) ++{ ++ struct k3_thermal_sensor *s = platform_get_drvdata(pdev); ++ ++ writel(readl(s->base + REG_TSEN_LITE_CFG) | BIT_TSEN_EN, ++ s->base + REG_TSEN_LITE_CFG); ++} ++ ++static int k3_thermal_get_temp(struct thermal_zone_device *tz, int *temp) ++{ ++ struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)tz->devdata; ++ ++ mutex_lock(&desc->ks->lock); ++ /* select which sensor */ ++ writel(desc->index, desc->base + REG_TSEN_LITE_CFG2); ++ msleep(1); ++ *temp = readl(desc->base + REG_TSEN_LITE_TEMP_DATA); ++ mutex_unlock(&desc->ks->lock); ++ ++ *temp &= BITS_TEMP_DATA; ++ *temp /= TEMP_RAW_DATA_DIV; ++ ++ *temp -= desc->temp_offset; ++ ++ *temp *= 1000; ++ ++ return 0; ++} ++ ++/** ++ * static int k3_thermal_set_trips(struct thermal_zone_device *tz, int low, int high) ++ * { ++ * int index; ++ * unsigned int temp; ++ * int over_thrsh = high; ++ * int under_thrsh = low; ++ * struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)tz->devdata; ++ * ++ * index = desc - desc; ++ * writel(index, desc->base + REG_TSEN_LITE_CFG2); ++ * ++ * over_thrsh /= 1000; ++ * over_thrsh += desc->temp_offset; ++ * over_thrsh *= TEMP_RAW_DATA_DIV; ++ * ++ * temp = readl(desc->base + REG_TSEN_LITE_TEMP_THRESH); ++ * temp &= ~0xffff0000; ++ * temp |= (over_thrsh << TSEN_HIGH_THRESH_OFFSET); ++ * writel(temp, desc->base + REG_TSEN_LITE_TEMP_THRESH); ++ * ++ * if (low < 0) ++ * under_thrsh = 0; ++ * ++ * under_thrsh /= 1000; ++ * under_thrsh += desc->temp_offset; ++ * under_thrsh *= TEMP_RAW_DATA_DIV; ++ * temp = readl(desc->base + REG_TSEN_LITE_TEMP_THRESH); ++ * temp &= ~0xffff; ++ * temp |= (under_thrsh << TSEN_LOW_THRESH_OFFSET); ++ * writel(temp, desc->base + REG_TSEN_LITE_TEMP_THRESH); ++ * ++ * return 0; ++ * } ++ */ ++static const struct thermal_zone_device_ops k3_of_thermal_ops = { ++ .get_temp = k3_thermal_get_temp, ++ /* .set_trips = k3_thermal_set_trips, */ ++}; ++ ++/** ++ * static irqreturn_t k3_thermal_irq_thread(int irq, void *data) ++ * { ++ * struct k3_thermal_sensor_desc *desc = (struct k3_thermal_sensor_desc *)data; ++ * ++ * thermal_zone_device_update(desc->tzd, THERMAL_EVENT_UNSPECIFIED); ++ * ++ * return IRQ_HANDLED; ++ * } ++ */ ++ ++static void k3_thermal_reset_assert(void *data) ++{ ++ reset_control_assert(data); ++} ++ ++static int k3_thermal_probe(struct platform_device *pdev) ++{ ++ unsigned int value; ++ int ret, i; ++ struct resource *res; ++ struct k3_thermal_sensor *s; ++ struct device *dev = &pdev->dev; ++ ++ s = devm_kzalloc(dev, sizeof(*s), GFP_KERNEL); ++ if (!s) ++ return -ENOMEM; ++ ++ s->dev = dev; ++ ++ res = platform_get_resource(pdev, IORESOURCE_MEM, 0); ++ s->base = devm_ioremap_resource(dev, res); ++ if (IS_ERR(s->base)) ++ return PTR_ERR(s->base); ++ ++ ret = of_property_read_u32(dev->of_node, "temperature_offset", &s->temp_offset); ++ if (ret) { ++ dev_err(dev, "Get temperature_offset failed\n"); ++ return -EINVAL; ++ } ++ ++ s->resets = devm_reset_control_get_optional(&pdev->dev, NULL); ++ if (IS_ERR(s->resets)) ++ return PTR_ERR(s->resets); ++ ++ ret = reset_control_deassert(s->resets); ++ if (ret) ++ return ret; ++ ++ ret = devm_add_action_or_reset(dev, k3_thermal_reset_assert, s->resets); ++ if (ret) ++ return ret; ++ ++ /* ++ * devm teardown runs in reverse order: the thermal zones are ++ * unregistered before the clocks are disabled and the reset is ++ * asserted, so a late poll can never hit dead hardware. ++ */ ++ s->fclk = devm_clk_get_enabled(dev, "func"); ++ if (IS_ERR(s->fclk)) ++ return PTR_ERR(s->fclk); ++ ++ s->bclk = devm_clk_get_enabled(dev, "bus"); ++ if (IS_ERR(s->bclk)) ++ return PTR_ERR(s->bclk); ++ ++ mutex_init(&s->lock); ++ ++ s->sdesc = devm_kcalloc(dev, MAX_SENSOR_NUMBER, ++ sizeof(struct k3_thermal_sensor_desc), ++ GFP_KERNEL); ++ if (!s->sdesc) ++ return -ENOMEM; ++ ++ platform_set_drvdata(pdev, s); ++ ++ /* initialize the sensors */ ++ ret = init_sensors(pdev); ++ if (ret) ++ return ret; ++ ++ /* enable the sensors & using auto mode */ ++ enable_sensors(pdev); ++ ++ /* clear d_en_autozero[25] and d_out_sel[26] */ ++ value = readl(s->base); ++ value &= ~((1 << 25) | (1 << 26)); ++ writel(value, s->base); ++ ++ /* then register the thermal zone */ ++ for (i = s->sr[0]; i <= s->sr[1]; ++i) { ++ if (s->tsen_enable_map[i] == 0) ++ continue; ++ ++ s->sdesc[i].base = s->base; ++ s->sdesc[i].index = i; ++ s->sdesc[i].ks = s; ++ s->sdesc[i].temp_offset = s->temp_offset; ++ s->sdesc[i].tzd = devm_thermal_of_zone_register(dev, ++ i, s->sdesc + i, &k3_of_thermal_ops); ++ if (IS_ERR(s->sdesc[i].tzd)) { ++ ret = PTR_ERR(s->sdesc[i].tzd); ++ /* ++ * No thermal zone described in DT for this sensor: ++ * skip it instead of aborting the whole probe. ++ */ ++ if (ret == -ENODEV) { ++ s->sdesc[i].tzd = NULL; ++ continue; ++ } ++ dev_err(dev, "failed to register sensor id %d: %d\n", ++ i, ret); ++ return ret; ++ } ++ ++ /* register the thermal sensor to hwmon */ ++ if (devm_thermal_add_hwmon_sysfs(dev, s->sdesc[i].tzd)) ++ dev_warn(dev, "Failed to add hwmon sysfs attributes\n"); ++ } ++ ++ return 0; ++} ++ ++static const struct of_device_id of_k3_thermal_match[] = { ++ { ++ .compatible = "spacemit,k3-tsensor", ++ }, ++ { /* end */ } ++}; ++ ++MODULE_DEVICE_TABLE(of, of_k3_thermal_match); ++ ++static struct platform_driver k3_thermal_driver = { ++ .driver = { ++ .name = "k3_thermal", ++ .of_match_table = of_k3_thermal_match, ++ }, ++ .probe = k3_thermal_probe, ++}; ++ ++module_platform_driver(k3_thermal_driver); +--- /dev/null ++++ b/drivers/thermal/spacemit-k3-thermal.h +@@ -0,0 +1,58 @@ ++#ifndef __k3_THERMAL_H__ ++#define __k3_THERMAL_H__ ++ ++#define MAX_SENSOR_NUMBER 8 ++#define CALIB_VREF_DEFAULT (0x9A) ++#define BITS_D_REG_VREF_CTRL BITS(7, 14) ++#define BITS_D_REG_VREF_OFFSET (7) ++ ++#define BITS(_start, _end) ((BIT(_end) - BIT(_start)) + BIT(_end)) ++ ++#define MAX_SENSOR_NUMBER 8 ++#define TEMPERATURE_OFFSET 278 ++ ++#define REG_TSEN_LITE_CFG 0x00 ++#define REG_TSEN_LITE_CFG2 0x04 ++#define REG_TSEN_LITE_INT_ENB 0x08 ++#define REG_TSEN_LITE_INT_ST 0x0C ++#define REG_TSEN_LITE_INT_CLR 0x10 ++#define REG_TSEN_REBOOT_THRESH 0x14 ++#define REG_TSEN_LITE_TEMP_DATA 0x18 ++#define REG_TSEN_LITE_VREF_EFFUSE 0x1C ++#define REG_TSEN_LITE_TEMP_THRESH 0x20 ++#define REG_TSEN_LITE_ECO_INTVL 0x24 ++#define REG_TSEN_LITE_LP_FIX_BYPASS 0x28 ++ ++#define BITS_D_CK_DIV_SEL BITS(2, 3) ++#define BITS_TEMP_DATA BITS(0, 11) ++#define BITS_CK_DIV_SEL_DIV4 (0 << 2) ++#define BIT_TSEN_EN BIT(0) ++ ++#define TSEN_HIGH_THRESH_OFFSET (0) ++#define TSEN_LOW_THRESH_OFFSET (16) ++ ++#define TEMP_RAW_DATA_DIV (8) ++ ++struct k3_thermal_sensor_desc { ++ void __iomem *base; ++ int temp_offset; ++ int index; ++ struct k3_thermal_sensor *ks; ++ struct thermal_zone_device *tzd; ++}; ++ ++struct k3_thermal_sensor { ++ int irq; ++ int temp_offset; ++ void __iomem *base; ++ struct clk *fclk, *bclk; ++ struct reset_control *resets; ++ struct device *dev; ++ struct mutex lock; ++ /* sensor range */ ++ unsigned int sr[2]; ++ struct k3_thermal_sensor_desc *sdesc; ++ unsigned int tsen_enable_map[MAX_SENSOR_NUMBER]; ++}; ++ ++#endif diff --git a/spacemit/k3-pico-itx/patches/0013-riscv-cpuinfo-model-name.patch b/spacemit/k3-pico-itx/patches/0013-riscv-cpuinfo-model-name.patch new file mode 100644 index 00000000..c7fecc66 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0013-riscv-cpuinfo-model-name.patch @@ -0,0 +1,93 @@ +From: liberodark +Subject: [PATCH] riscv: cpuinfo: expose DT cpu model as model name + +Lets tools like btop show "Spacemit(R) X100". + +Signed-off-by: liberodark +--- +diff --git a/arch/riscv/boot/dts/spacemit/k3.dtsi b/arch/riscv/boot/dts/spacemit/k3.dtsi +index 19fc9b4..f776f49 100644 +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -23,6 +23,7 @@ cpus: cpus { + + cpu_0: cpu@0 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <0>; + riscv,isa-base = "rv64i"; +@@ -64,6 +65,7 @@ cpu0_intc: interrupt-controller { + + cpu_1: cpu@1 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <1>; + riscv,isa-base = "rv64i"; +@@ -105,6 +107,7 @@ cpu1_intc: interrupt-controller { + + cpu_2: cpu@2 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <2>; + riscv,isa-base = "rv64i"; +@@ -146,6 +149,7 @@ cpu2_intc: interrupt-controller { + + cpu_3: cpu@3 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <3>; + riscv,isa-base = "rv64i"; +@@ -187,6 +191,7 @@ cpu3_intc: interrupt-controller { + + cpu_4: cpu@4 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <4>; + riscv,isa-base = "rv64i"; +@@ -228,6 +233,7 @@ cpu4_intc: interrupt-controller { + + cpu_5: cpu@5 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <5>; + riscv,isa-base = "rv64i"; +@@ -269,6 +275,7 @@ cpu5_intc: interrupt-controller { + + cpu_6: cpu@6 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <6>; + riscv,isa-base = "rv64i"; +@@ -310,6 +317,7 @@ cpu6_intc: interrupt-controller { + + cpu_7: cpu@7 { + compatible = "spacemit,x100", "riscv"; ++ model = "Spacemit(R) X100"; + device_type = "cpu"; + reg = <7>; + riscv,isa-base = "rv64i"; +diff --git a/arch/riscv/kernel/cpu.c b/arch/riscv/kernel/cpu.c +index 3dbc8cc..ed0a429 100644 +--- a/arch/riscv/kernel/cpu.c ++++ b/arch/riscv/kernel/cpu.c +@@ -347,8 +347,13 @@ static int c_show(struct seq_file *m, void *v) + print_mmu(m); + + if (acpi_disabled) { ++ const char *model; ++ + node = of_get_cpu_node(cpu_id, NULL); + ++ if (!of_property_read_string(node, "model", &model)) ++ seq_printf(m, "model name\t: %s\n", model); ++ + if (!of_property_read_string(node, "compatible", &compat) && + strcmp(compat, "riscv")) + seq_printf(m, "uarch\t\t: %s\n", compat); diff --git a/spacemit/k3-pico-itx/patches/0014-cpufreq-spacemit-k3.patch b/spacemit/k3-pico-itx/patches/0014-cpufreq-spacemit-k3.patch new file mode 100644 index 00000000..ba7582d0 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0014-cpufreq-spacemit-k3.patch @@ -0,0 +1,686 @@ +From: liberodark +Subject: [PATCH] cpufreq: spacemit: k3: per-cluster DVFS via cpufreq-dt (v4) + +Multi-clock OPP support for the K3 X100 clusters (PLL3/PLL4 switching, +614 MHz - 2.4 GHz) hooked into cpufreq-dt. + +v4: review fixes : +- cache the PLL clock handles at first transition instead of + of_clk_get_by_name() on every frequency change (reference + + memory leak in the hot path), drop the dead opp-hz/microvolt scan + and its leaked of_node reference +- serialize the cross-cluster PLL manipulation with a mutex (two + policies can transition concurrently) +- policy notifier: only act on CPUFREQ_CREATE_POLICY, check + _find_opp_table() result, drop the opp_table reference +- fold the redundant nested turbo condition + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -187,9 +187,135 @@ + #size-cells = <0>; + timebase-frequency = <24000000>; + ++ clst_core_opp_table0_x100: opp_table0_x100 { ++ compatible = "operating-points-v2"; ++ opp-shared; ++ ++ clocks = <&pll CLK_PLL3>, <&pll CLK_PLL4>, <&pll CLK_PLL3_D1>; ++ clock-names = "pll_clst0", "pll_clst1", "pll_src"; ++ ++ opp2400000000 { ++ opp-hz = /bits/ 64 <2400000000>, /bits/ 64 <2400000000>; ++ opp-microvolt = <1000000 1000000 1004000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp2300000000 { ++ opp-hz = /bits/ 64 <2300000000>, /bits/ 64 <2300000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp2200000000 { ++ opp-hz = /bits/ 64 <2200000000>, /bits/ 64 <2200000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp2150000000 { ++ opp-hz = /bits/ 64 <2150000000>, /bits/ 64 <2150000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp2100000000 { ++ opp-hz = /bits/ 64 <2100000000>, /bits/ 64 <2100000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp2000000000 { ++ opp-hz = /bits/ 64 <2000000000>, /bits/ 64 <2000000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1900000000 { ++ opp-hz = /bits/ 64 <1900000000>, /bits/ 64 <1900000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1850000000 { ++ opp-hz = /bits/ 64 <1850000000>, /bits/ 64 <1850000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1800000000 { ++ opp-hz = /bits/ 64 <1800000000>, /bits/ 64 <1800000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1700000000 { ++ opp-hz = /bits/ 64 <1700000000>, /bits/ 64 <1700000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1600000000 { ++ opp-hz = /bits/ 64 <1600000000>, /bits/ 64 <1600000000>; ++ opp-microvolt = <880000 880000 884000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1500000000 { ++ opp-hz = /bits/ 64 <1500000000>, /bits/ 64 <1500000000>; ++ opp-microvolt = <850000 850000 854000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1400000000 { ++ opp-hz = /bits/ 64 <1400000000>, /bits/ 64 <1400000000>; ++ opp-microvolt = <850000 850000 854000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1300000000 { ++ opp-hz = /bits/ 64 <1300000000>, /bits/ 64 <1300000000>; ++ opp-microvolt = <850000 850000 854000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1200000000 { ++ opp-hz = /bits/ 64 <1200000000>, /bits/ 64 <1200000000>; ++ opp-microvolt = <850000 850000 854000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1100000000 { ++ opp-hz = /bits/ 64 <1100000000>, /bits/ 64 <1100000000>; ++ opp-microvolt = <850000 850000 854000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp1000000000 { ++ opp-hz = /bits/ 64 <1000000000>, /bits/ 64 <1000000000>; ++ opp-microvolt = <800000 800000 804000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp819000000 { ++ opp-hz = /bits/ 64 <819200000>, /bits/ 64 <819200000>; ++ opp-microvolt = <800000 800000 804000>; ++ clock-latency-ns = <200000>; ++ }; ++ ++ opp614400000 { ++ opp-hz = /bits/ 64 <614400000>, /bits/ 64 <614400000>; ++ opp-microvolt = <800000 800000 804000>; ++ clock-latency-ns = <200000>; ++ }; ++ }; ++ + cpu_0: cpu@0 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <0>; + riscv,isa-base = "rv64i"; +@@ -232,6 +358,10 @@ + cpu_1: cpu@1 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <1>; + riscv,isa-base = "rv64i"; +@@ -274,6 +404,10 @@ + cpu_2: cpu@2 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <2>; + riscv,isa-base = "rv64i"; +@@ -316,6 +450,10 @@ + cpu_3: cpu@3 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <3>; + riscv,isa-base = "rv64i"; +@@ -358,6 +496,10 @@ + cpu_4: cpu@4 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <4>; + riscv,isa-base = "rv64i"; +@@ -400,6 +542,10 @@ + cpu_5: cpu@5 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <5>; + riscv,isa-base = "rv64i"; +@@ -442,6 +588,10 @@ + cpu_6: cpu@6 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <6>; + riscv,isa-base = "rv64i"; +@@ -484,6 +634,10 @@ + cpu_7: cpu@7 { + compatible = "spacemit,x100", "riscv"; + model = "Spacemit(R) X100"; ++ clocks = <&syscon_apmu CLK_APMU_CPU_C0_CORE>, ++ <&syscon_apmu CLK_APMU_CPU_C1_CORE>; ++ clock-names = "cls0", "cls1"; ++ operating-points-v2 = <&clst_core_opp_table0_x100>; + device_type = "cpu"; + reg = <7>; + riscv,isa-base = "rv64i"; +--- a/drivers/cpufreq/cpufreq-dt.c ++++ b/drivers/cpufreq/cpufreq-dt.c +@@ -24,6 +24,7 @@ + + #include "cpufreq-dt.h" + ++#ifndef CONFIG_SPACEMIT_K3_CPUFREQ + struct private_data { + struct list_head node; + +@@ -33,9 +34,11 @@ + bool have_static_opps; + int opp_token; + }; ++#endif + + static LIST_HEAD(priv_list); + ++#ifndef CONFIG_SPACEMIT_K3_CPUFREQ + static struct private_data *cpufreq_dt_find_data(int cpu) + { + struct private_data *priv; +@@ -47,6 +50,24 @@ + + return NULL; + } ++#else ++struct private_data *cpufreq_dt_find_data(int cpu) ++{ ++ struct private_data *priv; ++ ++ list_for_each_entry(priv, &priv_list, node) { ++ if (cpumask_test_cpu(cpu, priv->cpus)) ++ return priv; ++ } ++ ++ return NULL; ++} ++ ++void cpufreq_dt_add_data(struct private_data *priv) ++{ ++ list_add(&priv->node, &priv_list); ++} ++#endif + + static int set_target(struct cpufreq_policy *policy, unsigned int index) + { +--- a/drivers/cpufreq/cpufreq-dt.h ++++ b/drivers/cpufreq/cpufreq-dt.h +@@ -24,4 +24,20 @@ + + struct platform_device *cpufreq_dt_pdev_register(struct device *dev); + ++ ++#ifdef CONFIG_SPACEMIT_K3_CPUFREQ ++struct private_data { ++ struct list_head node; ++ ++ cpumask_var_t cpus; ++ struct device *cpu_dev; ++ struct cpufreq_frequency_table *freq_table; ++ bool have_static_opps; ++ int opp_token; ++}; ++ ++struct private_data *cpufreq_dt_find_data(int cpu); ++void cpufreq_dt_add_data(struct private_data *priv); ++#endif ++ + #endif /* __CPUFREQ_DT_H__ */ +--- a/drivers/cpufreq/Kconfig ++++ b/drivers/cpufreq/Kconfig +@@ -204,6 +204,18 @@ + + comment "CPU frequency scaling drivers" + ++config SPACEMIT_K3_CPUFREQ ++ bool "SpacemiT K3 CPU frequency scaling" ++ depends on ARCH_SPACEMIT && OF && COMMON_CLK ++ select CPUFREQ_DT ++ select CPUFREQ_DT_PLATDEV ++ select PM_OPP ++ help ++ CPU frequency scaling support for the SpacemiT K3 SoC. Works ++ with cpufreq-dt; a transition notifier reprograms the cluster ++ PLLs for turbo frequencies. Voltage is left at the firmware ++ defaults (no regulator scaling). ++ + config CPUFREQ_DT + tristate "Generic DT based cpufreq driver" + depends on HAVE_CLK && OF +--- a/drivers/cpufreq/Makefile ++++ b/drivers/cpufreq/Makefile +@@ -15,6 +15,7 @@ + obj-$(CONFIG_CPU_FREQ_GOV_ATTR_SET) += cpufreq_governor_attr_set.o + + obj-$(CONFIG_CPUFREQ_DT) += cpufreq-dt.o ++obj-$(CONFIG_SPACEMIT_K3_CPUFREQ) += spacemit-k3-cpufreq.o + obj-$(CONFIG_CPUFREQ_DT_RUST) += rcpufreq_dt.o + obj-$(CONFIG_CPUFREQ_DT_PLATDEV) += cpufreq-dt-platdev.o + obj-$(CONFIG_CPUFREQ_VIRT) += virtual-cpufreq.o +--- a/drivers/opp/core.c ++++ b/drivers/opp/core.c +@@ -94,6 +94,16 @@ + static bool assert_single_clk(struct opp_table *opp_table, + unsigned int __always_unused index) + { ++ /* ++ * Multi-clock OPP tables driven by dev_pm_opp_config_clks_simple ++ * keep every clock at the rate of the same OPP entry, so ++ * frequency-based lookups can safely use the first rate. ++ */ ++ if (IS_ENABLED(CONFIG_SPACEMIT_K3_CPUFREQ) && ++ opp_table->clk_count > 1 && ++ opp_table->config_clks == dev_pm_opp_config_clks_simple) ++ return true; ++ + return !WARN_ON(opp_table->clk_count > 1); + } + +--- /dev/null ++++ b/drivers/cpufreq/spacemit-k3-cpufreq.c +@@ -0,0 +1,329 @@ ++// SPDX-License-Identifier: GPL-2.0-only ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "../opp/opp.h" ++#include "cpufreq-dt.h" ++ ++#define TURBO0_FREQUENCY (1000000000) ++#define STABLE_FREQUENCY (819200000) ++ ++/* PLL handles cached at first transition (see spacemit_processor_notifier) */ ++static DEFINE_MUTEX(spacemit_pll_lock); ++static bool spacemit_plls_cached; ++static struct clk *spacemit_pll_clst0, *spacemit_pll_clst1; ++static struct clk *spacemit_pll_src, *spacemit_clt_pll_src; ++ ++static int spacemit_processor_notifier(struct notifier_block *nb, ++ unsigned long event, void *data) ++{ ++ int cpu; ++ struct device *cpu_dev; ++ struct cpufreq_freqs *freqs = (struct cpufreq_freqs *)data; ++ struct cpufreq_policy *policy = ( struct cpufreq_policy *)freqs->policy; ++ struct opp_table *opp_table; ++ struct clk *pll_clst0, *pll_clst1, *pll_src, *clt_pll_src; ++ int i; ++ ++ cpu = cpumask_first(policy->related_cpus); ++ cpu_dev = get_cpu_device(cpu); ++ opp_table = _find_opp_table(cpu_dev); ++ if (IS_ERR_OR_NULL(opp_table)) ++ return NOTIFY_DONE; ++ ++ /* ++ * Get the pll clk handles once and cache them: this notifier runs ++ * on every frequency transition, per-call of_clk_get_by_name() ++ * would leak a reference each time. ++ */ ++ mutex_lock(&spacemit_pll_lock); ++ if (!spacemit_plls_cached) { ++ spacemit_pll_clst0 = of_clk_get_by_name(opp_table->np, "pll_clst0"); ++ spacemit_pll_clst1 = of_clk_get_by_name(opp_table->np, "pll_clst1"); ++ spacemit_pll_src = of_clk_get_by_name(opp_table->np, "pll_src"); ++ spacemit_clt_pll_src = of_clk_get_by_name(opp_table->np, "clt_pll_src"); ++ spacemit_plls_cached = true; ++ } ++ pll_clst0 = spacemit_pll_clst0; ++ pll_clst1 = spacemit_pll_clst1; ++ pll_src = spacemit_pll_src; ++ clt_pll_src = spacemit_clt_pll_src; ++ ++ if (event == CPUFREQ_PRECHANGE) { ++ ++ if (freqs->new * 1000 > TURBO0_FREQUENCY) { ++ if (freqs->old * 1000 > TURBO0_FREQUENCY) { ++ for (i = 0; i < opp_table->clk_count; ++i) ++ clk_set_rate(opp_table->clks[i], STABLE_FREQUENCY); ++ } ++ ++ /* 2.4G */ ++ if (!IS_ERR(pll_clst0)) ++ clk_set_rate(pll_clst0, freqs->new * 1000); ++ if (!IS_ERR(pll_clst1)) ++ clk_set_rate(pll_clst1, freqs->new * 1000); ++ } ++ } ++ ++ if (!IS_ERR(clt_pll_src) && !IS_ERR(pll_src)) ++ clk_set_parent(clt_pll_src, pll_src); ++ ++ if (event == CPUFREQ_POSTCHANGE) { ++ /* TODO */ ++ } ++ ++ mutex_unlock(&spacemit_pll_lock); ++ dev_pm_opp_put_opp_table(opp_table); ++ ++ return 0; ++} ++static struct notifier_block spacemit_processor_notifier_block = { ++ .notifier_call = spacemit_processor_notifier, ++}; ++ ++static int spacemit_policy_notifier(struct notifier_block *nb, ++ unsigned long event, void *data) ++{ ++ int cpu; ++ struct device *cpu_dev; ++ struct cpufreq_policy *policy = data; ++ struct opp_table *opp_table; ++ ++ if (event != CPUFREQ_CREATE_POLICY) ++ return NOTIFY_DONE; ++ ++ cpu = cpumask_first(policy->related_cpus); ++ cpu_dev = get_cpu_device(cpu); ++ opp_table = _find_opp_table(cpu_dev); ++ if (IS_ERR_OR_NULL(opp_table)) ++ return NOTIFY_DONE; ++ ++ if (policy->clk) ++ clk_put(policy->clk); ++ ++ /* cover the policy->clk & opp_table->clk which has been set before */ ++ policy->clk = opp_table->clks[0]; ++ opp_table->clk = opp_table->clks[0]; ++ ++ dev_pm_opp_put_opp_table(opp_table); ++ ++ return 0; ++} ++ ++static struct notifier_block spacemit_policy_notifier_block = { ++ .notifier_call = spacemit_policy_notifier, ++}; ++ ++static int spacemit_init_cpufreq_table(struct device *dev, ++ struct cpufreq_frequency_table **table) ++{ ++ struct cpufreq_frequency_table *freq_table; ++ struct dev_pm_opp *opp; ++ unsigned long freq; ++ int i, count; ++ ++ count = dev_pm_opp_get_opp_count(dev); ++ if (count <= 0) ++ return count ? count : -ENOENT; ++ ++ freq_table = kcalloc(count + 1, sizeof(*freq_table), GFP_KERNEL); ++ if (!freq_table) ++ return -ENOMEM; ++ ++ for (i = 0, freq = 0; i < count; i++, freq++) { ++ opp = dev_pm_opp_find_freq_ceil_indexed(dev, &freq, 0); ++ if (IS_ERR(opp)) { ++ kfree(freq_table); ++ return PTR_ERR(opp); ++ } ++ freq_table[i].driver_data = i; ++ freq_table[i].frequency = freq / 1000; ++ dev_pm_opp_put(opp); ++ } ++ ++ freq_table[i].driver_data = i; ++ freq_table[i].frequency = CPUFREQ_TABLE_END; ++ ++ *table = freq_table; ++ return 0; ++} ++ ++static int spacemit_dt_cpufreq_pre_early_init(struct device *dev, int cpu) ++{ ++ struct private_data *priv; ++ struct device *cpu_dev; ++ const char *reg_name[] = { "clst", NULL }; ++ const char *clk_name[] = { "cls0", "cls1", NULL }; ++ struct dev_pm_opp_config config = { ++ .regulator_names = reg_name, ++ .clk_names = clk_name, ++ .config_clks = dev_pm_opp_config_clks_simple, ++ }; ++ int ret; ++ ++ /* Check if this CPU is already covered by some other policy */ ++ if (cpufreq_dt_find_data(cpu)) ++ return 0; ++ ++ cpu_dev = get_cpu_device(cpu); ++ if (!cpu_dev) ++ return -EPROBE_DEFER; ++ ++ priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); ++ if (!priv) ++ return -ENOMEM; ++ ++ if (!alloc_cpumask_var(&priv->cpus, GFP_KERNEL)) ++ return -ENOMEM; ++ ++ cpumask_set_cpu(cpu, priv->cpus); ++ priv->cpu_dev = cpu_dev; ++ ++ /* ++ * Mainline DT does not wire the cluster regulator (clst-supply): ++ * keep the firmware-set voltage and only scale frequency. This is ++ * safe (never under-volted for the max OPP set by firmware). ++ */ ++ config.regulator_names = NULL; ++ /* ++ * OPP layer will be taking care of regulators now, but it needs to know ++ * the name of the regulator first. ++ */ ++ priv->opp_token = dev_pm_opp_set_config(cpu_dev, &config); ++ if (priv->opp_token < 0) { ++ ret = -EPROBE_DEFER; ++ goto free_cpumask; ++ } ++ ++ /* Get OPP-sharing information from "operating-points-v2" bindings */ ++ ret = dev_pm_opp_of_get_sharing_cpus(cpu_dev, priv->cpus); ++ if (ret) ++ goto out; ++ ++ /* ++ * Initialize OPP tables for all priv->cpus. They will be shared by ++ * all CPUs which have marked their CPUs shared with OPP bindings. ++ * ++ * For platforms not using operating-points-v2 bindings, we do this ++ * before updating priv->cpus. Otherwise, we will end up creating ++ * duplicate OPPs for the CPUs. ++ * ++ * OPPs might be populated at runtime, don't fail for error here unless ++ * it is -EPROBE_DEFER. ++ */ ++ ret = dev_pm_opp_of_cpumask_add_table(priv->cpus); ++ if (!ret) { ++ priv->have_static_opps = true; ++ } else if (ret == -EPROBE_DEFER) { ++ goto out; ++ } ++ ++ /* ++ * The OPP table must be initialized, statically or dynamically, by this ++ * point. ++ */ ++ ret = dev_pm_opp_get_opp_count(cpu_dev); ++ if (ret <= 0) { ++ dev_err(cpu_dev, "OPP table can't be empty\n"); ++ ret = -ENODEV; ++ goto out; ++ } ++ ++ /* ++ * dev_pm_opp_init_cpufreq_table() asserts a single clock per OPP ++ * table; build the table manually using the indexed helpers which ++ * support multi-clock OPP entries (cls0 + cls1). ++ */ ++ ret = spacemit_init_cpufreq_table(cpu_dev, &priv->freq_table); ++ if (ret) { ++ dev_err(cpu_dev, "failed to init cpufreq table: %d\n", ret); ++ goto out; ++ } ++ ++ cpufreq_dt_add_data(priv); ++ ++ return 0; ++ ++out: ++ if (priv->have_static_opps) ++ dev_pm_opp_of_cpumask_remove_table(priv->cpus); ++ dev_pm_opp_put_regulators(priv->opp_token); ++free_cpumask: ++ free_cpumask_var(priv->cpus); ++ return ret; ++} ++ ++static int spacemit_dt_cpufreq_pre_probe(struct platform_device *pdev) ++{ ++ int cpu; ++ ++ if (strncmp(pdev->name, "cpufreq-dt", 10) != 0) ++ return 0; ++ ++ for_each_possible_cpu(cpu) ++ spacemit_dt_cpufreq_pre_early_init(&pdev->dev, cpu); ++ ++ return 0; ++} ++ ++static int __device_notifier_call(struct notifier_block *nb, ++ unsigned long event, void *dev) ++{ ++ struct platform_device *pdev = to_platform_device(dev); ++ ++ switch (event) { ++ case BUS_NOTIFY_REMOVED_DEVICE: ++ break; ++ case BUS_NOTIFY_UNBOUND_DRIVER: ++ break; ++ case BUS_NOTIFY_BIND_DRIVER: ++ /* here */ ++ spacemit_dt_cpufreq_pre_probe(pdev); ++ break; ++ case BUS_NOTIFY_ADD_DEVICE: ++ break; ++ default: ++ break; ++ } ++ ++ return NOTIFY_DONE; ++} ++ ++static struct notifier_block spacemit_platform_nb = { ++ .notifier_call = __device_notifier_call, ++}; ++ ++static int __init spacemit_processor_driver_init(void) ++{ ++ int ret; ++ ++ ret = cpufreq_register_notifier(&spacemit_processor_notifier_block, CPUFREQ_TRANSITION_NOTIFIER); ++ if (ret) { ++ pr_err("register cpufreq notifier failed\n"); ++ return -EINVAL; ++ } ++ ++ ret = cpufreq_register_notifier(&spacemit_policy_notifier_block, CPUFREQ_POLICY_NOTIFIER); ++ if (ret) { ++ pr_err("register cpufreq notifier failed\n"); ++ return -EINVAL; ++ } ++ ++ bus_register_notifier(&platform_bus_type, &spacemit_platform_nb); ++ ++ return 0; ++} ++arch_initcall(spacemit_processor_driver_init); diff --git a/spacemit/k3-pico-itx/patches/0015-dt-bindings-vendor-prefixes-sensylink.patch b/spacemit/k3-pico-itx/patches/0015-dt-bindings-vendor-prefixes-sensylink.patch new file mode 100644 index 00000000..1ed1dc24 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0015-dt-bindings-vendor-prefixes-sensylink.patch @@ -0,0 +1,34 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 1/6] dt-bindings: vendor-prefixes: Add Sensylink +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:35 +0800 +Message-Id: <20260831-ctl2301-v4-1-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add the vendor prefix for Sensylink Microelectronics. + +Link: https://en.sensylink.com/ +Acked-by: Krzysztof Kozlowski +Signed-off-by: Troy Mitchell +--- + Documentation/devicetree/bindings/vendor-prefixes.yaml | 2 ++ + 1 file changed, 2 insertions(+) + +diff --git a/Documentation/devicetree/bindings/vendor-prefixes.yaml b/Documentation/devicetree/bindings/vendor-prefixes.yaml +index ba2002969373..4a5cf436eb14 100644 +--- a/Documentation/devicetree/bindings/vendor-prefixes.yaml ++++ b/Documentation/devicetree/bindings/vendor-prefixes.yaml +@@ -1495,6 +1495,8 @@ patternProperties: + description: Sensirion AG + "^sensortek,.*": + description: Sensortek Technology Corporation ++ "^sensylink,.*": ++ description: Sensylink Microelectronics Technology Co., Ltd. + "^sercomm,.*": + description: Sercomm (Suzhou) Corporation + "^sff,.*": + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0015-dts-k3-com260-pcie.patch b/spacemit/k3-pico-itx/patches/0015-dts-k3-com260-pcie.patch new file mode 100644 index 00000000..22e2af71 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0015-dts-k3-com260-pcie.patch @@ -0,0 +1,44 @@ +From: liberodark +Subject: [PATCH] riscv: dts: spacemit: k3: enable PCIe on CoM260-IFX + +The CoM260-IFX (BPI-SM10) boots from an NVMe drive on PCIe port A, with +the same x4 wiring as the Pico-ITX. Enable the combo PHY and the first +PCIe root complex on this board. + +Rebased on v7.3-rc1: the PHY node and the PCIe host nodes now come from +upstream, so only the board level enablement is left here. + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3-com260-ifx.dts ++++ b/arch/riscv/boot/dts/spacemit/k3-com260-ifx.dts +@@ -4,6 +4,8 @@ + * Copyright (c) 2026 Yixun Lan + */ + ++#include ++ + #include "k3-com260.dtsi" + + / { +@@ -52,3 +54,20 @@ + sd-uhs-sdr104; + status = "okay"; + }; ++ ++&combophy { ++ spacemit,apmu = <&syscon_apmu 0x11>; ++ status = "okay"; ++}; ++ ++&pcie0_rc { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&pcie0_0_cfg>; ++ num-lanes = <4>; ++ status = "okay"; ++ ++ pcie@0 { ++ phys = <&combophy 0 PHY_TYPE_PCIE>, ++ <&combophy 1 PHY_TYPE_PCIE>; ++ }; ++}; diff --git a/spacemit/k3-pico-itx/patches/0016-dt-bindings-hwmon-lm63-family.patch b/spacemit/k3-pico-itx/patches/0016-dt-bindings-hwmon-lm63-family.patch new file mode 100644 index 00000000..1e66960d --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0016-dt-bindings-hwmon-lm63-family.patch @@ -0,0 +1,105 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 2/6] dt-bindings: hwmon: Move LM63 family to a dedicated + binding +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:36 +0800 +Message-Id: <20260831-ctl2301-v4-2-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Move the National Semiconductor LM63, LM64, and LM96163 compatibles out +of trivial-devices.yaml and into a dedicated binding. Describe their +integrated fan control and preserve the optional interrupt used by the +ALERT output. Permit use as thermal cooling devices via #cooling-cells. + +Reviewed-by: Krzysztof Kozlowski +Signed-off-by: Troy Mitchell +--- + .../devicetree/bindings/hwmon/national,lm63.yaml | 53 ++++++++++++++++++++++ + .../devicetree/bindings/trivial-devices.yaml | 6 --- + 2 files changed, 53 insertions(+), 6 deletions(-) + +diff --git a/Documentation/devicetree/bindings/hwmon/national,lm63.yaml b/Documentation/devicetree/bindings/hwmon/national,lm63.yaml +new file mode 100644 +index 000000000000..f2e0993cfbef +--- /dev/null ++++ b/Documentation/devicetree/bindings/hwmon/national,lm63.yaml +@@ -0,0 +1,53 @@ ++# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause ++%YAML 1.2 ++--- ++$id: http://devicetree.org/schemas/hwmon/national,lm63.yaml# ++$schema: http://devicetree.org/meta-schemas/core.yaml# ++ ++title: LM63-compatible temperature sensors with integrated fan control ++ ++maintainers: ++ - Guenter Roeck ++ ++description: | ++ The LM63 family provides local and remote temperature monitoring together ++ with fan speed monitoring and PWM fan control. Supported devices include ++ the National Semiconductor LM63, LM64 and LM96163. ++ ++allOf: ++ - $ref: hwmon-common.yaml# ++ ++properties: ++ compatible: ++ enum: ++ - national,lm63 ++ - national,lm64 ++ - national,lm96163 ++ ++ reg: ++ maxItems: 1 ++ ++ interrupts: ++ maxItems: 1 ++ ++ "#cooling-cells": ++ const: 2 ++ ++required: ++ - compatible ++ - reg ++ ++unevaluatedProperties: false ++ ++examples: ++ - | ++ i2c { ++ #address-cells = <1>; ++ #size-cells = <0>; ++ ++ temperature-sensor@4c { ++ compatible = "national,lm63"; ++ reg = <0x4c>; ++ #cooling-cells = <2>; ++ }; ++ }; +diff --git a/Documentation/devicetree/bindings/trivial-devices.yaml b/Documentation/devicetree/bindings/trivial-devices.yaml +index 2de8eb09cb7d..e46912ba2fd6 100644 +--- a/Documentation/devicetree/bindings/trivial-devices.yaml ++++ b/Documentation/devicetree/bindings/trivial-devices.yaml +@@ -368,16 +368,10 @@ properties: + - mps,mpq82d00 + # Murata D1U74T-W power supply unit + - murata,d1u74t +- # Temperature sensor with integrated fan control +- - national,lm63 +- # Temperature sensor with integrated fan control +- - national,lm64 + # Temperature sensor + - national,lm95235 + # Temperature sensor + - national,lm95245 +- # Temperature sensor with integrated fan control +- - national,lm96163 + # Serial Interface ACPI-Compatible Microprocessor System Hardware Monitor + - national,lm80 + # Serial Interface ACPI-Compatible Microprocessor System Hardware Monitor + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0016-remoteproc-spacemit-k3-rcpu.patch b/spacemit/k3-pico-itx/patches/0016-remoteproc-spacemit-k3-rcpu.patch new file mode 100644 index 00000000..fa516b22 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0016-remoteproc-spacemit-k3-rcpu.patch @@ -0,0 +1,952 @@ +From: liberodark +Subject: [PATCH] remoteproc: spacemit: attach to K3 RCPU (mailbox + rproc) + +Port the vendor K3 mailbox and remoteproc drivers (renamed to avoid +the TI K3 namespace). The RCPU management cores are started by the +platform firmware; the kernel attaches to RCPU1 and brings up the +rpmsg link over the vrings already reserved in the device tree. The +RCPU thermal/fan service only activates once this link is up - without +it the fan header (FAN_PWM) stays at 0% duty and the fan never spins. + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3.dtsi +@@ -1004,6 +1004,24 @@ + #reset-cells = <1>; + }; + ++ mailbox4: mailbox@cac91000 { ++ compatible = "spacemit,k3-mailbox"; ++ reg = <0x0 0xcac91000 0x0 0x400>; ++ interrupt-parent = <&saplic>; ++ interrupts = <217 IRQ_TYPE_LEVEL_HIGH>; ++ #mbox-cells = <1>; ++ }; ++ ++ rcpu1: remoteproc-rcpu1 { ++ compatible = "spacemit,k3-rproc"; ++ mboxes = <&mailbox4 0>, <&mailbox4 1>; ++ mbox-names = "vq0", "vq1"; ++ firmware-name = "rt24_os1_rcpu.elf"; ++ memory-region = <&rcpu1_runtime>, <&rcpu1_heap>, ++ <&rcpu1_vdev0vring0>, <&rcpu1_vdev0buffer>, ++ <&rcpu1_vdev0vring1>, <&rcpu1_rsc_table>; ++ }; ++ + tsensor: thermal-sensor@d4018000 { + compatible = "spacemit,k3-tsensor"; + reg = <0x0 0xd4018000 0x0 0x100>; +--- a/drivers/mailbox/Kconfig ++++ b/drivers/mailbox/Kconfig +@@ -221,6 +221,13 @@ + + If unsure, say N. + ++config SPACEMIT_K3_MAILBOX ++ tristate "SpacemiT K3 mailbox support" ++ depends on ARCH_SPACEMIT || COMPILE_TEST ++ help ++ Inter-processor mailbox used to communicate with the K3 RCPU ++ management cores (remoteproc/rpmsg transport). ++ + config QCOM_APCS_IPC + tristate "Qualcomm APCS IPC driver" + depends on ARCH_QCOM || COMPILE_TEST +--- a/drivers/mailbox/Makefile ++++ b/drivers/mailbox/Makefile +@@ -84,3 +84,5 @@ + obj-$(CONFIG_BCM74110_MAILBOX) += bcm74110-mailbox.o + + obj-$(CONFIG_RISCV_SBI_MPXY_MBOX) += riscv-sbi-mpxy-mbox.o ++ ++obj-$(CONFIG_SPACEMIT_K3_MAILBOX) += spacemit-k3-mailbox.o +--- a/drivers/remoteproc/Kconfig ++++ b/drivers/remoteproc/Kconfig +@@ -379,6 +379,18 @@ + + It's safe to say N if not interested in using RPU r5f cores. + ++config SPACEMIT_K3_RPROC ++ tristate "SpacemiT K3 RCPU remoteproc support" ++ depends on ARCH_SPACEMIT || COMPILE_TEST ++ depends on REMOTEPROC ++ select MAILBOX ++ select SPACEMIT_K3_MAILBOX ++ select RPMSG_VIRTIO ++ help ++ Attach to the K3 RCPU management cores (started by platform ++ firmware) and establish the rpmsg link. The RCPU thermal/fan ++ service activates once the link is up. ++ + endif # REMOTEPROC + + endmenu +--- a/drivers/remoteproc/Makefile ++++ b/drivers/remoteproc/Makefile +@@ -40,3 +40,5 @@ + obj-$(CONFIG_TI_K3_M4_REMOTEPROC) += ti_k3_m4_remoteproc.o ti_k3_common.o + obj-$(CONFIG_TI_K3_R5_REMOTEPROC) += ti_k3_r5_remoteproc.o ti_k3_common.o + obj-$(CONFIG_XLNX_R5_REMOTEPROC) += xlnx_r5_remoteproc.o ++ ++obj-$(CONFIG_SPACEMIT_K3_RPROC) += spacemit-k3-rproc.o +--- /dev/null ++++ b/drivers/mailbox/spacemit-k3-mailbox.c +@@ -0,0 +1,298 @@ ++// SPDX-License-Identifier: GPL-2.0 ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "spacemit-k3-mailbox.h" ++ ++#define mbox_dbg(mbox, ...) dev_dbg((mbox)->controller.dev, __VA_ARGS__) ++ ++static irqreturn_t spacemit_mbox_irq(int irq, void *dev_id) ++{ ++ struct spacemit_mailbox *mbox = dev_id; ++ struct mbox_chan *chan; ++ u32 status, msg; ++ int i, j; ++ mbox_msg_status_t mstatus; ++ unsigned long flags; ++ ++ spin_lock_irqsave(&mbox->lock, flags); ++ ++ status = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status) & ++ readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_set); ++ ++ if (!(status & 0xff)) { ++ spin_unlock_irqrestore(&mbox->lock, flags); ++ return IRQ_HANDLED; ++ } ++ ++ for (i = 0; i < SPACEMIT_NUM_CHANNELS; ++i) { ++ chan = &mbox->controller.chans[i]; ++ ++ /* not full irq */ ++ if (status & (1 << (i * 2 + 1))) { ++ /* disable not full irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++ j |= (1 << (i * 2 + 1)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++ j |= (1 << (i * 2 + 1)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++ ++ if (chan->txdone_method & MBOX_TXDONE_BY_IRQ) ++ mbox_chan_txdone(chan, 0); ++ } ++ ++ /* new msg irq */ ++ if (status & (1 << (i * 2))) { ++ ++ /* clear the fifo */ ++ while (1) { ++ msg = readl((void *)&mbox->regs->mbox_msg[i]); ++ mstatus.val = readl((void *)&mbox->regs->msg_status[i]); ++ if (mstatus.bits.num_msg == 0) ++ break; ++ } ++ mbox_chan_received_data(chan, &msg); ++ ++#if 0 ++ /* disable the new msg irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++ j |= (1 << (i * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++#endif ++ /* clear the irq pending */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++ j |= (1 << (i * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++ } ++ } ++ ++ spin_unlock_irqrestore(&mbox->lock, flags); ++ ++ return IRQ_HANDLED; ++} ++ ++static int spacemit_chan_send_data(struct mbox_chan *chan, void *data) ++{ ++ int j; ++ unsigned long flags; ++ struct spacemit_mailbox *mbox = chan->con_priv; ++ u32 chan_num = chan - mbox->controller.chans; ++ ++ spin_lock_irqsave(&mbox->lock, flags); ++ ++#if 0 ++ /* disable new msg irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++ j |= (1 << (chan_num * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_clr); ++ ++ /* clear pending */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++ j |= (1 << (chan_num * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_status_clr); ++#endif ++ ++ /* enable the other end new msg irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_en_set); ++ j |= (1 << (chan_num * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_en_set); ++ ++ /* send data */ ++ writel('c', (void *)&mbox->regs->mbox_msg[chan_num]); ++#if 0 ++ /* set other end new msg irq thresh */ ++ j = readl((void *)&mbox->regs->mbox_thresh[USER1_MBOX_OFFSET].thresh0); ++ j |= 7 << (chan_num * 8); ++ writel(j, (void *)&mbox->regs->mbox_thresh[USER1_MBOX_OFFSET].thresh0); ++#endif ++ /* set not full thresh */ ++ j = readl((void *)&mbox->regs->mbox_thresh[USER0_MBOX_OFFSET].thresh0); ++ j |= 1 << (chan_num * 8 + 4); ++ writel(j, (void *)&mbox->regs->mbox_thresh[USER0_MBOX_OFFSET].thresh0); ++ ++ /* enable not full irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_set); ++ j |= (1 << (chan_num * 2 + 1)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER0_MBOX_OFFSET].irq_en_set); ++ ++ spin_unlock_irqrestore(&mbox->lock, flags); ++ ++ return 0; ++} ++ ++static int spacemit_chan_startup(struct mbox_chan *chan) ++{ ++ struct spacemit_mailbox *mbox = chan->con_priv; ++ u32 chan_num = chan - mbox->controller.chans; ++ u32 msg; ++ mbox_msg_status_t mstatus; ++ unsigned long flags; ++ ++ spin_lock_irqsave(&mbox->lock, flags); ++ ++ /* clear the fifo */ ++ while (1) { ++ mstatus.val = readl((void *)&mbox->regs->msg_status[chan_num]); ++ msg = readl((void *)&mbox->regs->mbox_msg[chan_num]); ++ if (mstatus.bits.num_msg == 0) ++ break; ++ } ++ ++ spin_unlock_irqrestore(&mbox->lock, flags); ++ ++ return 0; ++} ++ ++static void spacemit_chan_shutdown(struct mbox_chan *chan) ++{ ++ struct spacemit_mailbox *mbox = chan->con_priv; ++ u32 chan_num = chan - mbox->controller.chans; ++ u32 msg, j; ++ mbox_msg_status_t mstatus; ++ unsigned long flags; ++ ++ if (chan->cl->tx_prepare != NULL) ++ return; ++ ++ spin_lock_irqsave(&mbox->lock, flags); ++ ++ /* disable new msg irq */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_en_clr); ++ j |= (1 << (chan_num * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_en_clr); ++ ++ /* flush the fifo */ ++ while (1) { ++ mstatus.val = readl((void *)&mbox->regs->msg_status[chan_num]); ++ msg = readl((void *)&mbox->regs->mbox_msg[chan_num]); ++ if (mstatus.bits.num_msg == 0) ++ break; ++ } ++ ++ /* clear pending */ ++ j = readl((void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_status_clr); ++ j |= (1 << (chan_num * 2)); ++ writel(j, (void *)&mbox->regs->mbox_irq[USER1_MBOX_OFFSET].irq_status_clr); ++ ++ spin_unlock_irqrestore(&mbox->lock, flags); ++} ++ ++static bool spacemit_chan_last_tx_done(struct mbox_chan *chan) ++{ ++ /* TODO */ ++ return true; ++} ++ ++static bool spacemit_chan_peek_data(struct mbox_chan *chan) ++{ ++ struct spacemit_mailbox *mbox = chan->con_priv; ++ u32 chan_num = chan - mbox->controller.chans; ++ ++ return readl((void *)&mbox->regs->msg_status[chan_num]); ++} ++ ++static const struct mbox_chan_ops spacemit_chan_ops = { ++ .send_data = spacemit_chan_send_data, ++ .startup = spacemit_chan_startup, ++ .shutdown = spacemit_chan_shutdown, ++ .last_tx_done = spacemit_chan_last_tx_done, ++ .peek_data = spacemit_chan_peek_data, ++}; ++ ++static int spacemit_mailbox_probe(struct platform_device *pdev) ++{ ++ struct device *dev = &pdev->dev; ++ struct mbox_chan *chans; ++ struct spacemit_mailbox *mbox; ++ int i, ret; ++ ++ mbox = devm_kzalloc(dev, sizeof(*mbox), GFP_KERNEL); ++ if (!mbox) ++ return -ENOMEM; ++ ++ chans = devm_kcalloc(dev, SPACEMIT_NUM_CHANNELS, sizeof(*chans), GFP_KERNEL); ++ if (!chans) ++ return -ENOMEM; ++ ++ for (i = 0; i < SPACEMIT_NUM_CHANNELS; ++i) ++ chans[i].con_priv = mbox; ++ ++ mbox->regs = (mbox_reg_desc_t *)of_iomap(pdev->dev.of_node, 0); ++ if (IS_ERR(mbox->regs)) { ++ ret = PTR_ERR(mbox->regs); ++ dev_err(dev, "Failed to map MMIO resource: %d\n", ret); ++ return -EINVAL; ++ } ++ ++ mbox->ap_communicate = of_property_read_bool(pdev->dev.of_node, "ap-communicate"); ++ ++ /* request irq */ ++ ret = devm_request_irq(dev, platform_get_irq(pdev, 0), ++ spacemit_mbox_irq, 0, dev_name(dev), mbox); ++ if (ret) { ++ dev_err(dev, "Failed to register IRQ handler: %d\n", ret); ++ return ret; ++ } ++ ++ /* register the mailbox controller */ ++ mbox->controller.dev = dev; ++ mbox->controller.ops = &spacemit_chan_ops; ++ mbox->controller.chans = chans; ++ mbox->controller.num_chans = SPACEMIT_NUM_CHANNELS; ++ mbox->controller.txdone_irq = true; ++ mbox->controller.txdone_poll = false; ++ mbox->controller.txpoll_period = 5; ++ ++ spin_lock_init(&mbox->lock); ++ platform_set_drvdata(pdev, mbox); ++ ++ ret = mbox_controller_register(&mbox->controller); ++ if (ret) { ++ dev_err(dev, "Failed to register controller: %d\n", ret); ++ return ret; ++ } ++ ++ return 0; ++} ++ ++static void spacemit_mailbox_remove(struct platform_device *pdev) ++{ ++ struct spacemit_mailbox *mbox = platform_get_drvdata(pdev); ++ ++ mbox_controller_unregister(&mbox->controller); ++} ++ ++static const struct of_device_id spacemit_mailbox_of_match[] = { ++ { .compatible = "spacemit,k3-mailbox", }, ++ {}, ++}; ++MODULE_DEVICE_TABLE(of, spacemit_mailbox_of_match); ++ ++static struct platform_driver spacemit_mailbox_driver = { ++ .driver = { ++ .name = "spacemit-mailbox", ++ .of_match_table = spacemit_mailbox_of_match, ++ }, ++ .probe = spacemit_mailbox_probe, ++ .remove = spacemit_mailbox_remove, ++}; ++module_platform_driver(spacemit_mailbox_driver); ++ ++MODULE_DESCRIPTION("spacemit Message Box driver"); ++MODULE_LICENSE("GPL v2"); +--- /dev/null ++++ b/drivers/mailbox/spacemit-k3-mailbox.h +@@ -0,0 +1,154 @@ ++#ifndef __SPACEMIT_MAILBOX_H__ ++#define __SPACEMIT_MAILBOX_H__ ++ ++#include ++#include ++#include ++ ++#define SPACEMIT_NUM_CHANNELS 4 ++ ++/* mailbox register description */ ++typedef union mbox_msg { ++ u32 val; ++ struct { ++ u32 msg:32; ++ } bits; ++} mbox_msg_t; ++ ++typedef union mbox_fifo_status { ++ u32 val; ++ struct { ++ u32 is_full:1; ++ u32 is_empty:1; ++ u32 reserved0:26; ++ u32 writefull:1; ++ u32 readempty:1; ++ u32 reserved1:2; ++ } bits; ++} mbox_fifo_status_t; ++ ++ ++typedef union mbox_msg_status { ++ u32 val; ++ struct { ++ u32 num_msg:4; ++ u32 reserved0:24; ++ u32 writefull:1; ++ u32 readempty:1; ++ u32 reserved1:2; ++ } bits; ++} mbox_msg_status_t; ++ ++typedef union mbox_irq_status { ++ u32 val; ++ struct { ++ u32 new_msg0_status:1; ++ u32 not_msg0_full:1; ++ u32 new_msg1_status:1; ++ u32 not_msg1_full:1; ++ u32 new_msg2_status:1; ++ u32 not_msg2_full:1; ++ u32 new_msg3_status:1; ++ u32 not_msg3_full:1; ++ u32 reserved:24; ++ } bits; ++} mbox_irq_status_t; ++ ++typedef union mbox_irq_status_clr { ++ u32 val; ++ struct { ++ u32 new_msg0_clr:1; ++ u32 not_msg0_full_clr:1; ++ u32 new_msg1_clr:1; ++ u32 not_msg1_full_clr:1; ++ u32 new_msg2_clr:1; ++ u32 not_msg2_full_clr:1; ++ u32 new_msg3_clr:1; ++ u32 not_msg3_full_clr:1; ++ u32 reserved:24; ++ } bits; ++} mbox_irq_status_clr_t; ++ ++typedef union mbox_irq_enable_set { ++ u32 val; ++ struct { ++ u32 new_msg0_irq_en:1; ++ u32 not_msg0_full_irq_en:1; ++ u32 new_msg1_irq_en:1; ++ u32 not_msg1_full_irq_en:1; ++ u32 new_msg2_irq_en:1; ++ u32 not_msg2_full_irq_en:1; ++ u32 new_msg3_irq_en:1; ++ u32 not_msg3_full_irq_en:1; ++ u32 reserved:24; ++ } bits; ++} mbox_irq_enable_set_t; ++ ++typedef union mbox_irq_enable_clr { ++ u32 val; ++ struct { ++ u32 new_msg0_irq_clr:1; ++ u32 not_msg0_full_irq_clr:1; ++ u32 new_msg1_irq_clr:1; ++ u32 not_msg1_full_irq_clr:1; ++ u32 new_msg2_irq_clr:1; ++ u32 not_msg2_full_irq_clr:1; ++ u32 new_msg3_irq_clr:1; ++ u32 not_msg3_full_irq_clr:1; ++ u32 reserved:24; ++ } bits; ++} mbox_irq_enable_clr_t; ++ ++typedef struct mbox_irqthresh { ++ u32 val; ++ struct { ++ u32 mbox0_new_msg_thresh:4; ++ u32 mbox0_not_full_thresh:4; ++ u32 mbox1_new_msg_thresh:4; ++ u32 mbox1_not_full_thresh:4; ++ u32 mbox2_new_msg_thresh:4; ++ u32 mbox2_not_full_thresh:4; ++ u32 mbox3_new_msg_thresh:4; ++ u32 mbox3_not_full_thresh:4; ++ } bits; ++} mbox_irqthresh_t; ++ ++typedef struct mbox_thresh { ++ mbox_irqthresh_t thresh0; ++ u32 reserved[3]; ++} mbox_thresh_t; ++ ++typedef struct mbox_irq { ++ mbox_irq_status_t irq_status; ++ mbox_irq_status_clr_t irq_status_clr; ++ mbox_irq_enable_set_t irq_en_set; ++ mbox_irq_enable_clr_t irq_en_clr; ++} mbox_irq_t; ++ ++typedef struct mbox_reg_desc { ++ u32 mbox_version; /* 0x00 */ ++ u32 reseved0[3]; /* 0x04 ~ 0x0c */ ++ u32 mbox_sysconfig; /* 0x10 */ ++ u32 reserved1[11]; /* 0x14 ~ 0x3c */ ++ mbox_msg_t mbox_msg[4]; /* 0x40 ~ 0x4c */ ++ u32 reserved2[12]; /* 0x50 ~ 0x7c */ ++ mbox_fifo_status_t fifo_status[4]; /* 0x80 ~ 0x8c */ ++ u32 reserved3[12]; /* 0x90 ~ 0xbc */ ++ mbox_msg_status_t msg_status[4]; /* 0xc0 ~ 0xcc */ ++ u32 reserved4[12]; /* 0xd0 ~ 0xfc */ ++ mbox_irq_t mbox_irq[2]; /* 0x100 ~ 0x11c */ ++ u32 reserved5[24]; /* 0x120 ~ 0x17c */ ++ mbox_thresh_t mbox_thresh[2]; /* 0x180 */ ++} mbox_reg_desc_t; ++ ++struct spacemit_mailbox { ++ struct mbox_controller controller; ++ mbox_reg_desc_t *regs; ++ spinlock_t lock; ++ bool ap_communicate; ++}; ++ ++#define USER0_MBOX_OFFSET ((mbox->ap_communicate) ? 1 : 0) ++#define USER1_MBOX_OFFSET ((mbox->ap_communicate) ? 0 : 1) ++ ++#endif /* __SPACEMIT_MAILBOX_H__ */ +--- /dev/null ++++ b/drivers/remoteproc/spacemit-k3-rproc.c +@@ -0,0 +1,399 @@ ++// SPDX-License-Identifier: GPL-2.0 ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++#include "remoteproc_internal.h" ++#include "remoteproc_elf_helpers.h" ++ ++#define MAX_MEM_BASE 2 ++#define MAX_MBOX 2 ++ ++#define K3_MBOX_VQ0_ID 0 ++#define K3_MBOX_VQ1_ID 1 ++ ++struct spacemit_mbox { ++ const char *name; ++ struct mbox_chan *chan; ++ struct mbox_client client; ++ struct task_struct *mb_thread; ++ bool kthread_running; ++ struct completion mb_comp; ++ int vq_id; ++}; ++ ++struct spacemit_rproc { ++ struct device *dev; ++ struct spacemit_mbox mb[MAX_MBOX]; ++ unsigned int size; ++}; ++ ++static int spacemit_rproc_mem_alloc(struct rproc *rproc, struct rproc_mem_entry *mem) ++{ ++ void __iomem *va = NULL; ++ ++ dev_dbg(&rproc->dev, "map memory: %pa+%zx\n", &mem->dma, mem->len); ++ va = ioremap(mem->dma, mem->len); ++ if (!va) { ++ dev_err(&rproc->dev, "Unable to map memory region: %pa+%zx\n", ++ &mem->dma, mem->len); ++ return -ENOMEM; ++ } ++ ++ /* Update memory entry va */ ++ mem->va = va; ++ ++ return 0; ++} ++ ++static int spacemit_rproc_mem_release(struct rproc *rproc, struct rproc_mem_entry *mem) ++{ ++ dev_dbg(&rproc->dev, "unmap memory: %pa\n", &mem->dma); ++ ++ iounmap(mem->va); ++ ++ return 0; ++} ++ ++static int spacemit_rproc_prepare(struct rproc *rproc) ++{ ++ struct device *dev = rproc->dev.parent; ++ struct device_node *np = dev->of_node; ++ struct of_phandle_iterator it; ++ struct rproc_mem_entry *mem; ++ struct reserved_mem *rmem; ++ int index = 0; ++ ++ /* Register associated reserved memory regions */ ++ of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); ++ while (of_phandle_iterator_next(&it) == 0) { ++ rmem = of_reserved_mem_lookup(it.node); ++ if (!rmem) { ++ dev_err(&rproc->dev, "unable to acquire memory-region\n"); ++ return -EINVAL; ++ } ++ ++ if (rmem->base > U64_MAX) { ++ dev_err(&rproc->dev, "the rmem base is overflow\n"); ++ return -EINVAL; ++ } ++ ++ mem = rproc_mem_entry_init(dev, NULL, ++ rmem->base, ++ rmem->size, rmem->base, ++ spacemit_rproc_mem_alloc, ++ spacemit_rproc_mem_release, ++ it.node->name); ++ if (!mem) ++ return -ENOMEM; ++ ++ rproc_add_carveout(rproc, mem); ++ index++; ++ } ++ ++ return 0; ++} ++ ++static int spacemit_rproc_start(struct rproc *rproc) ++{ ++ /* Do nothing: has been latched from spl */ ++ return 0; ++} ++ ++static int spacemit_rproc_stop(struct rproc *rproc) ++{ ++ /* TODO */ ++ ++ return 0; ++} ++ ++static void spacemit_rproc_kick(struct rproc *rproc, int vqid) ++{ ++ struct spacemit_rproc *ddata = rproc->priv; ++ unsigned int i; ++ int err; ++ ++ if (WARN_ON(vqid >= MAX_MBOX)) ++ return; ++ ++ for (i = 0; i < MAX_MBOX; i++) { ++ if (vqid != ddata->mb[i].vq_id) ++ continue; ++ if (!ddata->mb[i].chan) ++ return; ++ err = mbox_send_message(ddata->mb[i].chan, "kick"); ++ if (err < 0) ++ dev_err(&rproc->dev, "%s: failed (%s, err:%d)\n", ++ __func__, ddata->mb[i].name, err); ++ return; ++ } ++} ++ ++static int spacemit_rproc_attach(struct rproc *rproc) ++{ ++ return 0; ++} ++ ++static int spacemit_rproc_detach(struct rproc *rproc) ++{ ++ return 0; ++} ++ ++static struct resource_table* spacemit_get_loaded_rsc_table( ++ struct rproc *rproc, size_t *size) ++{ ++ struct device *dev = rproc->dev.parent; ++ struct device_node *np = dev->of_node; ++ struct of_phandle_iterator it; ++ struct reserved_mem *rmem; ++ ++ /* Register associated reserved memory regions */ ++ of_phandle_iterator_init(&it, np, "memory-region", NULL, 0); ++ while (of_phandle_iterator_next(&it) == 0) { ++ rmem = of_reserved_mem_lookup(it.node); ++ if (!rmem) { ++ dev_err(&rproc->dev, "unable to acquire memory-region\n"); ++ return NULL; ++ } ++ ++ if (rmem->base > U64_MAX) { ++ dev_err(&rproc->dev, "the rmem base is overflow\n"); ++ return NULL; ++ } ++ ++ if (!strcmp(it.node->name, "rcpu0-rsc-table")) { ++ *size = rmem->size; ++ return (struct resource_table *)ioremap(rmem->base, rmem->size); ++ } else if (!strcmp(it.node->name, "rcpu1-rsc-table")) { ++ *size = rmem->size; ++ return (struct resource_table *)ioremap(rmem->base, rmem->size); ++ } ++ } ++ ++ return NULL; ++} ++ ++static struct rproc_ops spacemit_rproc_ops = { ++ .prepare = spacemit_rproc_prepare, ++ .start = spacemit_rproc_start, ++ .stop = spacemit_rproc_stop, ++ .attach = spacemit_rproc_attach, ++ .detach = spacemit_rproc_detach, ++ .kick = spacemit_rproc_kick, ++ .get_loaded_rsc_table = spacemit_get_loaded_rsc_table, ++}; ++ ++static int __process_theread(void *arg) ++{ ++ int ret; ++ struct mbox_client *cl = arg; ++ struct rproc *rproc = dev_get_drvdata(cl->dev); ++ struct spacemit_mbox *mb = container_of(cl, struct spacemit_mbox, client); ++ struct sched_param param = {.sched_priority = 0 }; ++ ++ mb->kthread_running = true; ++ ret = sched_setscheduler(current, SCHED_FIFO, ¶m); ++ set_freezable(); ++ ++ do { ++ try_to_freeze(); ++ wait_for_completion_timeout(&mb->mb_comp, 10); ++ if (rproc_vq_interrupt(rproc, mb->vq_id) == IRQ_NONE) ++ dev_dbg(&rproc->dev, "no message found in vq%d\n", mb->vq_id); ++ } while (!kthread_should_stop()); ++ ++ mb->kthread_running = false; ++ ++ return 0; ++} ++static void k3_rproc_mb_callback(struct mbox_client *cl, void *data) ++{ ++ struct spacemit_mbox *mb = container_of(cl, struct spacemit_mbox, client); ++ ++ complete(&mb->mb_comp); ++} ++ ++static int spacemit_rproc_probe(struct platform_device *pdev) ++{ ++ int ret, i; ++ const char *name; ++ struct device *dev = &pdev->dev; ++ struct device_node *np = dev->of_node; ++ const char *fw_name; ++ struct spacemit_rproc *priv; ++ struct mbox_client *cl; ++ struct rproc *rproc; ++ ++ ret = rproc_of_parse_firmware(dev, 0, &fw_name); ++ if (ret < 0 && ret != -EINVAL) ++ return ret; ++ ++ rproc = devm_rproc_alloc(dev, np->name, &spacemit_rproc_ops, ++ fw_name, sizeof(*priv)); ++ if (!rproc) ++ return -ENOMEM; ++ ++ priv = rproc->priv; ++ priv->dev = dev; ++ ++ platform_set_drvdata(pdev, rproc); ++ ++ /* tx */ ++ priv->mb[0].name = "vq0"; ++ priv->mb[0].vq_id = K3_MBOX_VQ0_ID; ++ priv->mb[0].client.rx_callback = k3_rproc_mb_callback, ++ priv->mb[0].client.tx_block = true, ++ ++ /* rx */ ++ priv->mb[1].name = "vq1"; ++ priv->mb[1].vq_id = K3_MBOX_VQ1_ID; ++ priv->mb[1].client.rx_callback = k3_rproc_mb_callback, ++ priv->mb[1].client.tx_block = true; ++ ++ for (i = 0; i < MAX_MBOX; ++i) { ++ name = priv->mb[i].name; ++ ++ cl = &priv->mb[i].client; ++ cl->dev = dev; ++ init_completion(&priv->mb[i].mb_comp); ++ ++ priv->mb[i].chan = mbox_request_channel_byname(cl, name); ++ if (IS_ERR(priv->mb[i].chan)) { ++ dev_err(dev, "failed to request mbox channel\n"); ++ ret = -EINVAL; ++ goto err_0; ++ } ++ ++ if (priv->mb[i].vq_id >= 0) { ++ priv->mb[i].mb_thread = kthread_run(__process_theread, (void *)cl, name); ++ if (IS_ERR(priv->mb[i].mb_thread)) { ++ ret = PTR_ERR(priv->mb[i].mb_thread); ++ goto err_0; ++ } ++ } ++ } ++ ++ rproc->auto_boot = true; ++ rproc->state = RPROC_DETACHED; ++ ret = devm_rproc_add(dev, rproc); ++ if (ret) { ++ dev_err(dev, "rproc_add failed\n"); ++ ret = -EINVAL; ++ goto err_0; ++ } ++ ++ return 0; ++ ++err_0: ++ while (--i >= 0) { ++ if (priv->mb[i].chan) ++ mbox_free_channel(priv->mb[i].chan); ++ if (priv->mb[i].mb_thread) ++ kthread_stop(priv->mb[i].mb_thread); ++ } ++ ++ rproc_free(rproc); ++ ++ return ret; ++} ++ ++static void k3_rproc_free_mbox(struct rproc *rproc) ++{ ++ struct spacemit_rproc *ddata = rproc->priv; ++ unsigned int i; ++ ++ for (i = 0; i < MAX_MBOX; i++) { ++ if (ddata->mb[i].chan) ++ mbox_free_channel(ddata->mb[i].chan); ++ ddata->mb[i].chan = NULL; ++ } ++} ++ ++static void spacemit_rproc_remove(struct platform_device *pdev) ++{ ++ int i = 0; ++ struct rproc *rproc = platform_get_drvdata(pdev); ++ struct spacemit_rproc *ddata = rproc->priv; ++ ++ for (i = 0; i < MAX_MBOX; ++i) ++ if (ddata->mb[i].kthread_running) ++ kthread_stop(ddata->mb[i].mb_thread); ++ ++ rproc_del(rproc); ++ k3_rproc_free_mbox(rproc); ++ rproc_free(rproc); ++} ++ ++static const struct of_device_id spacemit_rproc_of_match[] = { ++ { .compatible = "spacemit,k3-rproc" }, ++ {}, ++}; ++ ++MODULE_DEVICE_TABLE(of, spacemit_rproc_of_match); ++ ++static void spacemit_rproc_shutdown(struct platform_device *pdev) ++{ ++ int i; ++ struct rproc *rproc; ++ struct spacemit_rproc *priv; ++ ++ rproc = dev_get_drvdata(&pdev->dev); ++ priv = rproc->priv; ++ ++ for (i = 0; i < MAX_MBOX; ++i) { ++ /* release the resource of rt thread */ ++ if (priv->mb[i].kthread_running) { ++ if (!frozen((priv->mb[i].mb_thread))) ++ kthread_stop(priv->mb[i].mb_thread); ++ } ++ /* mbox_free_channel(priv->mb[i].chan); */ ++ } ++} ++ ++static struct platform_driver spacemit_rproc_driver = { ++ .probe = spacemit_rproc_probe, ++ .remove = spacemit_rproc_remove, ++ .shutdown = spacemit_rproc_shutdown, ++ .driver = { ++ .name = "spacemit-rproc", ++ .of_match_table = spacemit_rproc_of_match, ++ }, ++}; ++ ++static __init int spacemit_rproc_driver_init(void) ++{ ++ return platform_driver_register(&spacemit_rproc_driver); ++} ++device_initcall(spacemit_rproc_driver_init); ++ ++MODULE_LICENSE("GPL v2"); ++MODULE_DESCRIPTION("sapcemit remote processor control driver"); diff --git a/spacemit/k3-pico-itx/patches/0017-dt-bindings-hwmon-ctf2301.patch b/spacemit/k3-pico-itx/patches/0017-dt-bindings-hwmon-ctf2301.patch new file mode 100644 index 00000000..25cfef3c --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0017-dt-bindings-hwmon-ctf2301.patch @@ -0,0 +1,50 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 3/6] dt-bindings: hwmon: Add Sensylink CTF2301 +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:37 +0800 +Message-Id: <20260831-ctl2301-v4-3-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add the Sensylink CTF2301 temperature sensor and fan controller to the +LM63 family binding. The chip follows the LM63 register model and can +provide a thermal cooling device when #cooling-cells is present. + +Do not provide a national,lm63 fallback because the CTF2301 is not fully +compatible with LM63. It uses different conversion-rate encodings, +extended local-temperature registers, additional fan lookup table +entries, and device-specific PWM and ALERT/TACH configuration. An older +LM63 driver would therefore not operate the device correctly. + +Acked-by: Krzysztof Kozlowski +Signed-off-by: Troy Mitchell +--- + Documentation/devicetree/bindings/hwmon/national,lm63.yaml | 4 +++- + 1 file changed, 3 insertions(+), 1 deletion(-) + +diff --git a/Documentation/devicetree/bindings/hwmon/national,lm63.yaml b/Documentation/devicetree/bindings/hwmon/national,lm63.yaml +index f2e0993cfbef..4c8d0a15fbd1 100644 +--- a/Documentation/devicetree/bindings/hwmon/national,lm63.yaml ++++ b/Documentation/devicetree/bindings/hwmon/national,lm63.yaml +@@ -12,7 +12,8 @@ maintainers: + description: | + The LM63 family provides local and remote temperature monitoring together + with fan speed monitoring and PWM fan control. Supported devices include +- the National Semiconductor LM63, LM64 and LM96163. ++ the National Semiconductor LM63, LM64 and LM96163, and the Sensylink ++ CTF2301. + + allOf: + - $ref: hwmon-common.yaml# +@@ -23,6 +24,7 @@ properties: + - national,lm63 + - national,lm64 + - national,lm96163 ++ - sensylink,ctf2301 + + reg: + maxItems: 1 + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0018-hwmon-lm63-ctf2301.patch b/spacemit/k3-pico-itx/patches/0018-hwmon-lm63-ctf2301.patch new file mode 100644 index 00000000..b6a296d7 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0018-hwmon-lm63-ctf2301.patch @@ -0,0 +1,804 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 4/6] hwmon: (lm63) Add Sensylink CTF2301 support +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:38 +0800 +Message-Id: <20260831-ctl2301-v4-4-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add Sensylink CTF2301 support to the LM63 driver. CTF2301 follows +the LM63-style temperature, tachometer, and PWM register layout, +while using a 12-bit local temperature register and requiring the +ALERT/TACH pin to be configured for tachometer input. + +Register an optional thermal cooling device when the device tree +node provides #cooling-cells, allowing thermal zones to control +the fan through the existing PWM path without changing behavior +for existing LM63-family users. + +Signed-off-by: Troy Mitchell +--- + Documentation/hwmon/lm63.rst | 22 +++ + drivers/hwmon/Kconfig | 8 +- + drivers/hwmon/lm63.c | 426 +++++++++++++++++++++++++++++++++++-------- + 3 files changed, 377 insertions(+), 79 deletions(-) + +diff --git a/Documentation/hwmon/lm63.rst b/Documentation/hwmon/lm63.rst +index 9e27367d7405..c40bd0dfe591 100644 +--- a/Documentation/hwmon/lm63.rst ++++ b/Documentation/hwmon/lm63.rst +@@ -33,6 +33,16 @@ Supported chips: + + http://www.national.com/pf/LM/LM96163.html + ++ * Sensylink CTF2301 ++ ++ Prefix: 'ctf2301' ++ ++ Addresses scanned: none ++ ++ Datasheet: https://www.sensylink.com/upload/1/net.sensylink.portal/1689557281035.pdf ++ ++ Register description: https://github.com/TroyMitchell911/ctf2301-datasheet ++ + + Author: Jean Delvare + +@@ -62,6 +72,8 @@ value have to be masked out. The value is still 16 bit in width. + + All temperature values are given in degrees Celsius. Resolution is 1.0 + degree for the local temperature, 0.125 degree for the remote temperature. ++The CTF2301 local temperature input and limit have a resolution of 0.0625 ++degree. + + The fan speed is measured using a tachometer. Contrary to most chips which + store the value in an 8-bit register and have a selectable clock divider +@@ -93,3 +105,13 @@ support these GPIO lines at present. + The LM96163 is an enhanced version of LM63 with improved temperature accuracy + and better PWM resolution. For LM96163, the external temperature sensor type is + configurable as CPU embedded diode(1) or 3904 transistor(2). ++ ++The CTF2301 is register-compatible with the LM63 family and provides 12 fan ++control lookup table entries. It supports 8-bit PWM resolution when configured ++for a 22.5 kHz PWM frequency. ++ ++Device tree nodes may declare ``#cooling-cells`` to register the fan controller ++with the thermal framework. The cooling state is mapped to the existing ++``pwm1`` range from 0 to 255. The driver keeps the current automatic or manual ++fan control mode during probe and switches to manual control when the thermal ++framework first requests a cooling state. +diff --git a/drivers/hwmon/Kconfig b/drivers/hwmon/Kconfig +index fecff8610ea8..5d87ad3e7a20 100644 +--- a/drivers/hwmon/Kconfig ++++ b/drivers/hwmon/Kconfig +@@ -1521,10 +1521,10 @@ config SENSORS_LM63 + depends on I2C + help + If you say yes here you get support for the National +- Semiconductor LM63, LM64, and LM96163 remote diode digital temperature +- sensors with integrated fan control. Such chips are found +- on the Tyan S4882 (Thunder K8QS Pro) motherboard, among +- others. ++ Semiconductor LM63, LM64, LM96163, and Sensylink CTF2301 ++ remote diode digital temperature sensors with integrated fan ++ control. Such chips are found on the Tyan S4882 (Thunder K8QS ++ Pro) motherboard, among others. + + This driver can also be built as a module. If so, the module + will be called lm63. +diff --git a/drivers/hwmon/lm63.c b/drivers/hwmon/lm63.c +index da09770c05e7..5a94895242ed 100644 +--- a/drivers/hwmon/lm63.c ++++ b/drivers/hwmon/lm63.c +@@ -35,13 +35,14 @@ + #include + #include + #include ++#include + #include + + /* + * Addresses to scan + * Address is fully defined internally and cannot be changed except for + * LM64 which has one pin dedicated to address selection. +- * LM63 and LM96163 have address 0x4c. ++ * LM63, LM96163, and CTF2301 have address 0x4c. + * LM64 can have address 0x18 or 0x4e. + */ + +@@ -70,6 +71,9 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; + #define LM63_REG_LOCAL_TEMP 0x00 + #define LM63_REG_LOCAL_HIGH 0x05 + ++#define CTF2301_REG_LOCAL_TEMP_LSB 0x15 ++#define CTF2301_REG_LOCAL_HIGH_LSB 0x06 ++ + #define LM63_REG_REMOTE_TEMP_MSB 0x01 + #define LM63_REG_REMOTE_TEMP_LSB 0x10 + #define LM63_REG_REMOTE_OFFSET_MSB 0x11 +@@ -94,6 +98,7 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; + + #define LM63_PWM_BASE_FAST_HZ 180000 + #define LM63_PWM_BASE_SLOW_HZ 700 ++#define LM63_MAX_PWM 255 + + #define LM63_MAX_CONVRATE 9 + +@@ -121,6 +126,9 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; + 127000), 1000) + #define TEMP8U_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), 0, \ + 255000), 1000) ++#define TEMP12_FROM_REG(reg) DIV_ROUND_CLOSEST((s16)(reg) * 1000, 256) ++#define TEMP12_TO_REG(val) DIV_ROUND_CLOSEST(clamp_val((val), -128000, \ ++ 127937) * 256, 1000) + #define TEMP11_FROM_REG(reg) ((reg) / 32 * 125) + #define TEMP11_TO_REG(val) (DIV_ROUND_CLOSEST(clamp_val((val), -128000, \ + 127875), 125) * 32) +@@ -132,7 +140,11 @@ static const unsigned short normal_i2c[] = { 0x18, 0x4c, 0x4e, I2C_CLIENT_END }; + #define UPDATE_INTERVAL(max, rate) \ + ((1000 << (LM63_MAX_CONVRATE - (rate))) / (max)) + +-enum chips { lm63, lm64, lm96163 }; ++enum chips { ctf2301, lm63, lm64, lm96163 }; ++ ++static const unsigned int ctf2301_update_intervals[] = { ++ 20000, 10000, 4902, 2463, 1000, 615, 307, 154, 107 ++}; + + /* + * Client data (each client gets its own) +@@ -164,6 +176,8 @@ struct lm63_data { + 1: local high limit + 2: remote critical limit + 3-14: lookup table */ ++ s16 temp1_input; /* local input for chips with extended resolution */ ++ s16 temp1_max; /* local limit for chips with extended resolution */ + s16 temp11[4]; /* 0: remote input + 1: remote low limit + 2: remote high limit +@@ -229,6 +243,7 @@ static struct lm63_data *lm63_update_device(struct device *dev) + struct lm63_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; + unsigned long next_update; ++ int msb, lsb; + + mutex_lock(&data->update_lock); + +@@ -253,10 +268,26 @@ static struct lm63_data *lm63_update_device(struct device *dev) + data->pwm1[0] = i2c_smbus_read_byte_data(client, + LM63_REG_PWM_VALUE); + +- data->temp8[0] = i2c_smbus_read_byte_data(client, +- LM63_REG_LOCAL_TEMP); +- data->temp8[1] = i2c_smbus_read_byte_data(client, +- LM63_REG_LOCAL_HIGH); ++ if (data->kind == ctf2301) { ++ msb = i2c_smbus_read_byte_data(client, ++ LM63_REG_LOCAL_TEMP); ++ lsb = i2c_smbus_read_byte_data(client, ++ CTF2301_REG_LOCAL_TEMP_LSB); ++ if (msb >= 0 && lsb >= 0) ++ data->temp1_input = ((u16)(u8)msb << 8) | lsb; ++ ++ msb = i2c_smbus_read_byte_data(client, ++ LM63_REG_LOCAL_HIGH); ++ lsb = i2c_smbus_read_byte_data(client, ++ CTF2301_REG_LOCAL_HIGH_LSB); ++ if (msb >= 0 && lsb >= 0) ++ data->temp1_max = ((u16)(u8)msb << 8) | lsb; ++ } else { ++ data->temp8[0] = i2c_smbus_read_byte_data(client, ++ LM63_REG_LOCAL_TEMP); ++ data->temp8[1] = i2c_smbus_read_byte_data(client, ++ LM63_REG_LOCAL_HIGH); ++ } + + /* order matters for temp2_input */ + data->temp11[0] = i2c_smbus_read_byte_data(client, +@@ -276,7 +307,7 @@ static struct lm63_data *lm63_update_device(struct device *dev) + | i2c_smbus_read_byte_data(client, + LM63_REG_REMOTE_OFFSET_LSB); + +- if (data->kind == lm96163) ++ if (data->kind == lm96163 || data->kind == ctf2301) + data->temp11u = (i2c_smbus_read_byte_data(client, + LM96163_REG_REMOTE_TEMP_U_MSB) << 8) + | i2c_smbus_read_byte_data(client, +@@ -366,6 +397,49 @@ static ssize_t set_fan(struct device *dev, struct device_attribute *dummy, + return count; + } + ++static int lm63_pwm_from_reg(struct lm63_data *data, u8 reg) ++{ ++ if (data->pwm_highres) ++ return reg; ++ ++ return reg >= 2 * data->pwm1_freq ? ++ LM63_MAX_PWM : ++ (reg * LM63_MAX_PWM + data->pwm1_freq) / (2 * data->pwm1_freq); ++} ++ ++static u8 lm63_pwm_to_reg(struct lm63_data *data, unsigned long val) ++{ ++ val = clamp_val(val, 0, LM63_MAX_PWM); ++ ++ if (data->pwm_highres) ++ return val; ++ ++ return (val * data->pwm1_freq * 2 + LM63_MAX_PWM / 2) / LM63_MAX_PWM; ++} ++ ++static void lm63_update_pwm_highres(struct lm63_data *data, ++ int config_enhanced) ++{ ++ data->pwm_highres = !(data->config_fan & 0x08) && ++ data->pwm1_freq == 8 && ++ (config_enhanced & 0x10); ++} ++ ++static int lm63_write_pwm(struct lm63_data *data, int nr, unsigned long val) ++{ ++ struct i2c_client *client = data->client; ++ u8 reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE; ++ u8 regval = lm63_pwm_to_reg(data, val); ++ int ret; ++ ++ ret = i2c_smbus_write_byte_data(client, reg, regval); ++ if (ret) ++ return ret; ++ data->pwm1[nr] = regval; ++ ++ return 0; ++} ++ + static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr, + char *buf) + { +@@ -375,12 +449,7 @@ static ssize_t show_pwm1(struct device *dev, struct device_attribute *devattr, + int pwm; + + mutex_lock(&data->update_lock); +- if (data->pwm_highres) +- pwm = data->pwm1[nr]; +- else +- pwm = data->pwm1[nr] >= 2 * data->pwm1_freq ? +- 255 : (data->pwm1[nr] * 255 + data->pwm1_freq) / +- (2 * data->pwm1_freq); ++ pwm = lm63_pwm_from_reg(data, data->pwm1[nr]); + mutex_unlock(&data->update_lock); + + return sprintf(buf, "%d\n", pwm); +@@ -391,11 +460,9 @@ static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr, + { + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); + struct lm63_data *data = dev_get_drvdata(dev); +- struct i2c_client *client = data->client; + int nr = attr->index; + unsigned long val; + int err; +- u8 reg; + + if (!(data->config_fan & 0x20)) /* register is read-only */ + return -EPERM; +@@ -404,15 +471,11 @@ static ssize_t set_pwm1(struct device *dev, struct device_attribute *devattr, + if (err) + return err; + +- reg = nr ? LM63_REG_LUT_PWM(nr - 1) : LM63_REG_PWM_VALUE; +- val = clamp_val(val, 0, 255); +- + mutex_lock(&data->update_lock); +- data->pwm1[nr] = data->pwm_highres ? val : +- (val * data->pwm1_freq * 2 + 127) / 255; +- i2c_smbus_write_byte_data(client, reg, data->pwm1[nr]); ++ err = lm63_write_pwm(data, nr, val); + mutex_unlock(&data->update_lock); +- return count; ++ ++ return err ? err : count; + } + + static ssize_t pwm1_enable_show(struct device *dev, +@@ -485,7 +548,9 @@ static ssize_t pwm1_freq_store(struct device *dev, + struct lm63_data *data = dev_get_drvdata(dev); + struct i2c_client *client = data->client; + unsigned long val, pfr_fast, pfr_slow, err_fast, err_slow, pfr; ++ u8 config_fan; + bool slow_clock; ++ int config_enhanced = 0; + int ret; + + ret = kstrtoul(buf, 10, &val); +@@ -511,35 +576,58 @@ static ssize_t pwm1_freq_store(struct device *dev, + + mutex_lock(&data->update_lock); + ret = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG_FAN); +- if (ret < 0) { +- mutex_unlock(&data->update_lock); +- return ret; +- } +- data->config_fan = ret; ++ if (ret < 0) ++ goto unlock; ++ config_fan = ret; ++ data->config_fan = config_fan; + +- if (!(data->config_fan & 0x20)) { /* register is read-only */ +- mutex_unlock(&data->update_lock); +- return -EPERM; ++ if (!(config_fan & 0x20)) { /* register is read-only */ ++ ret = -EPERM; ++ goto unlock; + } + +- if (data->kind == lm96163) { +- ret = i2c_smbus_read_byte_data(client, LM96163_REG_CONFIG_ENHANCED); +- if (ret < 0) { +- mutex_unlock(&data->update_lock); +- return ret; ++ if (data->kind == lm96163 || data->kind == ctf2301) { ++ ret = i2c_smbus_read_byte_data(client, ++ LM96163_REG_CONFIG_ENHANCED); ++ if (ret < 0) ++ goto unlock; ++ config_enhanced = ret; ++ lm63_update_pwm_highres(data, config_enhanced); ++ ++ if (data->kind == ctf2301) { ++ if (!slow_clock && pfr == 8) ++ config_enhanced |= 0x10; ++ else ++ config_enhanced &= ~0x10; ++ ret = i2c_smbus_write_byte_data(client, ++ LM96163_REG_CONFIG_ENHANCED, ++ config_enhanced); ++ if (ret) ++ goto unlock; ++ lm63_update_pwm_highres(data, config_enhanced); + } +- data->pwm_highres = !slow_clock && pfr == 8 && (ret & 0x10); + } + + if (slow_clock) +- data->config_fan |= 0x08; ++ config_fan |= 0x08; + else +- data->config_fan &= ~0x08; +- i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, data->config_fan); +- i2c_smbus_write_byte_data(client, LM63_REG_PWM_FREQ, pfr); ++ config_fan &= ~0x08; ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_PWM_FREQ, pfr); ++ if (ret) ++ goto unlock; + data->pwm1_freq = pfr; ++ lm63_update_pwm_highres(data, config_enhanced); ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_CONFIG_FAN, ++ config_fan); ++ if (ret) ++ goto unlock; ++ ++ data->config_fan = config_fan; ++ lm63_update_pwm_highres(data, config_enhanced); ++unlock: + mutex_unlock(&data->update_lock); +- return count; ++ ++ return ret ? ret : count; + } + + /* +@@ -554,7 +642,17 @@ static ssize_t show_local_temp8(struct device *dev, + { + struct sensor_device_attribute *attr = to_sensor_dev_attr(devattr); + struct lm63_data *data = lm63_update_device(dev); +- return sprintf(buf, "%d\n", TEMP8_FROM_REG(data->temp8[attr->index])); ++ int temp; ++ ++ mutex_lock(&data->update_lock); ++ if (data->kind == ctf2301) ++ temp = TEMP12_FROM_REG(attr->index ? data->temp1_max : ++ data->temp1_input); ++ else ++ temp = TEMP8_FROM_REG(data->temp8[attr->index]); ++ mutex_unlock(&data->update_lock); ++ ++ return sprintf(buf, "%d\n", temp); + } + + static ssize_t show_remote_temp8(struct device *dev, +@@ -594,6 +692,21 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, + return err; + + mutex_lock(&data->update_lock); ++ if (data->kind == ctf2301 && nr == 1) { ++ temp = TEMP12_TO_REG(val); ++ err = i2c_smbus_write_byte_data(client, LM63_REG_LOCAL_HIGH, ++ temp >> 8); ++ if (!err) ++ err = i2c_smbus_write_byte_data(client, ++ CTF2301_REG_LOCAL_HIGH_LSB, ++ temp & 0xf0); ++ if (!err) ++ data->temp1_max = temp; ++ mutex_unlock(&data->update_lock); ++ ++ return err ? err : count; ++ } ++ + switch (nr) { + case 2: + reg = LM63_REG_REMOTE_TCRIT; +@@ -610,10 +723,12 @@ static ssize_t set_temp8(struct device *dev, struct device_attribute *devattr, + reg = LM63_REG_LUT_TEMP(nr - 3); + temp = lut_temp_to_reg(data, val); + } +- data->temp8[nr] = temp; +- i2c_smbus_write_byte_data(client, reg, temp); ++ err = i2c_smbus_write_byte_data(client, reg, temp); ++ if (!err) ++ data->temp8[nr] = temp; + mutex_unlock(&data->update_lock); +- return count; ++ ++ return err ? err : count; + } + + static ssize_t show_temp11(struct device *dev, struct device_attribute *devattr, +@@ -787,11 +902,40 @@ static ssize_t temp2_crit_hyst_store(struct device *dev, + * Set conversion rate. + * client->update_lock must be held when calling this function. + */ +-static void lm63_set_convrate(struct lm63_data *data, unsigned int interval) ++static unsigned int lm63_update_interval(struct lm63_data *data, u8 rate) ++{ ++ if (data->kind == ctf2301) ++ return ctf2301_update_intervals[min_t(unsigned int, rate, ++ ARRAY_SIZE(ctf2301_update_intervals) - 1)]; ++ ++ return UPDATE_INTERVAL(data->max_convrate_hz, rate); ++} ++ ++static int lm63_set_convrate(struct lm63_data *data, unsigned int interval) + { + struct i2c_client *client = data->client; +- unsigned int update_interval; +- int i; ++ unsigned int update_interval, error, best_error = ~0U; ++ unsigned int best = 0; ++ unsigned int i; ++ int ret; ++ ++ if (data->kind == ctf2301) { ++ for (i = 0; i < ARRAY_SIZE(ctf2301_update_intervals); i++) { ++ error = abs_diff(interval, ctf2301_update_intervals[i]); ++ if (error < best_error) { ++ best_error = error; ++ best = i; ++ } ++ } ++ ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, ++ best); ++ if (ret) ++ return ret; ++ data->update_interval = ctf2301_update_intervals[best]; ++ ++ return 0; ++ } + + /* Shift calculations to avoid rounding errors */ + interval <<= 6; +@@ -803,8 +947,12 @@ static void lm63_set_convrate(struct lm63_data *data, unsigned int interval) + if (interval >= update_interval * 3 / 4) + break; + +- i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_CONVRATE, i); ++ if (ret) ++ return ret; + data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, i); ++ ++ return 0; + } + + static ssize_t update_interval_show(struct device *dev, +@@ -828,10 +976,10 @@ static ssize_t update_interval_store(struct device *dev, + return err; + + mutex_lock(&data->update_lock); +- lm63_set_convrate(data, clamp_val(val, 0, 100000)); ++ err = lm63_set_convrate(data, clamp_val(val, 0, 100000)); + mutex_unlock(&data->update_lock); + +- return count; ++ return err ? err : count; + } + + static ssize_t temp2_type_show(struct device *dev, +@@ -996,6 +1144,75 @@ static DEVICE_ATTR_RO(alarms); + + static DEVICE_ATTR_RW(update_interval); + ++static int lm63_get_max_state(struct thermal_cooling_device *cdev, ++ unsigned long *state) ++{ ++ *state = LM63_MAX_PWM; ++ ++ return 0; ++} ++ ++static int lm63_get_cur_state(struct thermal_cooling_device *cdev, ++ unsigned long *state) ++{ ++ struct lm63_data *data = cdev->devdata; ++ int ret; ++ ++ mutex_lock(&data->update_lock); ++ ret = i2c_smbus_read_byte_data(data->client, LM63_REG_PWM_VALUE); ++ if (ret < 0) ++ goto unlock; ++ ++ data->pwm1[0] = ret; ++ *state = lm63_pwm_from_reg(data, data->pwm1[0]); ++unlock: ++ mutex_unlock(&data->update_lock); ++ ++ return ret < 0 ? ret : 0; ++} ++ ++static int lm63_set_cur_state(struct thermal_cooling_device *cdev, ++ unsigned long state) ++{ ++ struct lm63_data *data = cdev->devdata; ++ int config_fan; ++ int ret; ++ ++ if (state > LM63_MAX_PWM) ++ return -EINVAL; ++ ++ mutex_lock(&data->update_lock); ++ config_fan = i2c_smbus_read_byte_data(data->client, ++ LM63_REG_CONFIG_FAN); ++ if (config_fan < 0) { ++ ret = config_fan; ++ goto unlock; ++ } ++ data->config_fan = config_fan; ++ ++ if (!(config_fan & 0x20)) { ++ config_fan |= 0x20; ++ ret = i2c_smbus_write_byte_data(data->client, ++ LM63_REG_CONFIG_FAN, ++ config_fan); ++ if (ret) ++ goto unlock; ++ data->config_fan = config_fan; ++ } ++ ++ ret = lm63_write_pwm(data, 0, state); ++unlock: ++ mutex_unlock(&data->update_lock); ++ ++ return ret; ++} ++ ++static const struct thermal_cooling_device_ops lm63_cooling_ops = { ++ .get_max_state = lm63_get_max_state, ++ .get_cur_state = lm63_get_cur_state, ++ .set_cur_state = lm63_set_cur_state, ++}; ++ + static struct attribute *lm63_attributes[] = { + &sensor_dev_attr_pwm1.dev_attr.attr, + &dev_attr_pwm1_enable.attr, +@@ -1078,8 +1295,8 @@ static const struct attribute_group lm63_group_extra_lut = { + * On LM63, temp2_crit can be set only once, which should be job + * of the bootloader. + * On LM64, temp2_crit can always be set. +- * On LM96163, temp2_crit can be set if bit 1 of the configuration +- * register is true. ++ * On LM96163 and CTF2301, temp2_crit can be set if bit 1 of the ++ * configuration register is true. + */ + static umode_t lm63_attribute_mode(struct kobject *kobj, + struct attribute *attr, int index) +@@ -1089,7 +1306,8 @@ static umode_t lm63_attribute_mode(struct kobject *kobj, + + if (attr == &sensor_dev_attr_temp2_crit.dev_attr.attr + && (data->kind == lm64 || +- (data->kind == lm96163 && (data->config & 0x02)))) ++ ((data->kind == lm96163 || data->kind == ctf2301) && ++ (data->config & 0x02)))) + return attr->mode | S_IWUSR; + + return attr->mode; +@@ -1164,29 +1382,48 @@ static int lm63_detect(struct i2c_client *client, + * Ideally we shouldn't have to initialize anything, since the BIOS + * should have taken care of everything + */ +-static void lm63_init_client(struct lm63_data *data) ++static int lm63_init_client(struct lm63_data *data) + { + struct i2c_client *client = data->client; + struct device *dev = &client->dev; + u8 convrate; ++ int ret; + +- data->config = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); +- data->config_fan = i2c_smbus_read_byte_data(client, +- LM63_REG_CONFIG_FAN); ++ ret = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG1); ++ if (ret < 0) ++ return ret; ++ data->config = ret; ++ ret = i2c_smbus_read_byte_data(client, LM63_REG_CONFIG_FAN); ++ if (ret < 0) ++ return ret; ++ data->config_fan = ret; + + /* Start converting if needed */ + if (data->config & 0x40) { /* standby */ + dev_dbg(dev, "Switching to operational mode\n"); + data->config &= 0xA7; +- i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, +- data->config); ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, ++ data->config); ++ if (ret) ++ return ret; + } + /* Tachometer is always enabled on LM64 */ + if (data->kind == lm64) + data->config |= 0x04; ++ if (data->kind == ctf2301 && !client->irq && !(data->config & 0x04)) { ++ dev_dbg(dev, "Configuring ALERT/TACH pin for tachometer input\n"); ++ data->config |= 0x04; ++ ret = i2c_smbus_write_byte_data(client, LM63_REG_CONFIG1, ++ data->config); ++ if (ret) ++ return ret; ++ } + + /* We may need pwm1_freq before ever updating the client data */ +- data->pwm1_freq = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ) & 0x1f; ++ ret = i2c_smbus_read_byte_data(client, LM63_REG_PWM_FREQ); ++ if (ret < 0) ++ return ret; ++ data->pwm1_freq = ret & 0x1f; + if (data->pwm1_freq == 0) + data->pwm1_freq = 1; + +@@ -1199,25 +1436,38 @@ static void lm63_init_client(struct lm63_data *data) + case lm96163: + data->max_convrate_hz = LM96163_MAX_CONVRATE_HZ; + data->lut_size = 12; +- data->trutherm +- = i2c_smbus_read_byte_data(client, +- LM96163_REG_TRUTHERM) & 0x02; ++ ret = i2c_smbus_read_byte_data(client, LM96163_REG_TRUTHERM); ++ if (ret < 0) ++ return ret; ++ data->trutherm = ret & 0x02; ++ break; ++ case ctf2301: ++ data->lut_size = 12; + break; + } +- convrate = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); +- if (unlikely(convrate > LM63_MAX_CONVRATE)) ++ ret = i2c_smbus_read_byte_data(client, LM63_REG_CONVRATE); ++ if (ret < 0) ++ return ret; ++ convrate = ret; ++ if (data->kind == ctf2301 && ++ convrate >= ARRAY_SIZE(ctf2301_update_intervals)) ++ convrate = ARRAY_SIZE(ctf2301_update_intervals) - 1; ++ else if (unlikely(convrate > LM63_MAX_CONVRATE)) + convrate = LM63_MAX_CONVRATE; +- data->update_interval = UPDATE_INTERVAL(data->max_convrate_hz, +- convrate); ++ data->update_interval = lm63_update_interval(data, convrate); + + /* +- * For LM96163, check if high resolution PWM ++ * For LM96163 and CTF2301, check if high resolution PWM + * and unsigned temperature format is enabled. + */ +- if (data->kind == lm96163) { +- u8 config_enhanced +- = i2c_smbus_read_byte_data(client, +- LM96163_REG_CONFIG_ENHANCED); ++ if (data->kind == lm96163 || data->kind == ctf2301) { ++ u8 config_enhanced; ++ ++ ret = i2c_smbus_read_byte_data(client, ++ LM96163_REG_CONFIG_ENHANCED); ++ if (ret < 0) ++ return ret; ++ config_enhanced = ret; + if (config_enhanced & 0x20) + data->lut_temp_highres = true; + if ((config_enhanced & 0x10) +@@ -1238,6 +1488,8 @@ static void lm63_init_client(struct lm63_data *data) + dev_dbg(dev, "PWM output active %s, %s mode\n", + (data->config_fan & 0x10) ? "low" : "high", + (data->config_fan & 0x20) ? "manual" : "auto"); ++ ++ return 0; + } + + static const struct i2c_device_id lm63_id[]; +@@ -1245,9 +1497,11 @@ static const struct i2c_device_id lm63_id[]; + static int lm63_probe(struct i2c_client *client) + { + struct device *dev = &client->dev; ++ struct thermal_cooling_device *cdev; + struct device *hwmon_dev; + struct lm63_data *data; + int groups = 0; ++ int ret; + + data = devm_kzalloc(dev, sizeof(struct lm63_data), GFP_KERNEL); + if (!data) +@@ -1262,7 +1516,9 @@ static int lm63_probe(struct i2c_client *client) + data->temp2_offset = 16000; + + /* Initialize chip */ +- lm63_init_client(data); ++ ret = lm63_init_client(data); ++ if (ret) ++ return dev_err_probe(dev, ret, "failed to initialize device\n"); + + /* Register sysfs hooks */ + data->groups[groups++] = &lm63_group; +@@ -1272,11 +1528,26 @@ static int lm63_probe(struct i2c_client *client) + if (data->kind == lm96163) { + data->groups[groups++] = &lm63_group_temp2_type; + data->groups[groups++] = &lm63_group_extra_lut; ++ } else if (data->kind == ctf2301) { ++ data->groups[groups++] = &lm63_group_extra_lut; + } + + hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, + data, data->groups); +- return PTR_ERR_OR_ZERO(hwmon_dev); ++ if (IS_ERR(hwmon_dev)) ++ return PTR_ERR(hwmon_dev); ++ ++ if (IS_ENABLED(CONFIG_THERMAL_OF) && ++ of_property_present(dev->of_node, "#cooling-cells")) { ++ cdev = devm_thermal_of_cooling_device_register(dev, 0, ++ client->name, ++ data, ++ &lm63_cooling_ops); ++ if (IS_ERR(cdev)) ++ return PTR_ERR(cdev); ++ } ++ ++ return 0; + } + + /* +@@ -1287,6 +1558,7 @@ static const struct i2c_device_id lm63_id[] = { + { .name = "lm63", .driver_data = lm63 }, + { .name = "lm64", .driver_data = lm64 }, + { .name = "lm96163", .driver_data = lm96163 }, ++ { .name = "ctf2301", .driver_data = ctf2301 }, + { } + }; + MODULE_DEVICE_TABLE(i2c, lm63_id); +@@ -1304,6 +1576,10 @@ static const struct of_device_id __maybe_unused lm63_of_match[] = { + .compatible = "national,lm96163", + .data = (void *)lm96163 + }, ++ { ++ .compatible = "sensylink,ctf2301", ++ .data = (void *)ctf2301 ++ }, + { }, + }; + MODULE_DEVICE_TABLE(of, lm63_of_match); + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0019-dts-k3-i2c6-pinctrl.patch b/spacemit/k3-pico-itx/patches/0019-dts-k3-i2c6-pinctrl.patch new file mode 100644 index 00000000..1b87e8a4 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0019-dts-k3-i2c6-pinctrl.patch @@ -0,0 +1,44 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 5/6] riscv: dts: spacemit: Add K3 I2C6 pinctrl state +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:39 +0800 +Message-Id: <20260831-ctl2301-v4-5-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Add the K3 I2C6 pinctrl state used by boards that route I2C6 to pads 7 +and 8. Use a 25 mA drive strength, which is supported for both K3 IO +voltage domains, and leave board-specific IO voltage selection to board +files. + +Signed-off-by: Troy Mitchell +--- + arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi | 11 +++++++++++ + 1 file changed, 11 insertions(+) + +diff --git a/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi b/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi +index 6482d24be139..cb4f4f4e33af 100644 +--- a/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3-pinctrl.dtsi +@@ -101,6 +101,17 @@ i2c2-1-pins { + }; + }; + ++ /omit-if-no-ref/ ++ i2c6_1_cfg: i2c6-1-cfg { ++ i2c6-1-pins { ++ pinmux = , /* i2c6 scl */ ++ ; /* i2c6 sda */ ++ ++ bias-pull-up = <0>; ++ drive-strength = <25>; ++ }; ++ }; ++ + /omit-if-no-ref/ + i2c8_cfg: i2c8-cfg { + i2c8-pins { + +-- +2.55.0 diff --git a/spacemit/k3-pico-itx/patches/0020-dts-k3-com260-ctf2301.patch b/spacemit/k3-pico-itx/patches/0020-dts-k3-com260-ctf2301.patch new file mode 100644 index 00000000..c6d4533d --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0020-dts-k3-com260-ctf2301.patch @@ -0,0 +1,52 @@ +From git@z Thu Jan 1 00:00:00 1970 +Subject: [PATCH v4 6/6] riscv: dts: spacemit: Add CTF2301 on K3 CoM260 +From: Troy Mitchell +Date: Mon, 31 Aug 2026 21:45:40 +0800 +Message-Id: <20260831-ctl2301-v4-6-b335d52a0570@linux.dev> +MIME-Version: 1.0 +Content-Type: text/plain; charset="utf-8" +Content-Transfer-Encoding: 7bit + +Enable I2C6 on the K3 CoM260 module and describe the Sensylink +CTF2301 temperature sensor and fan controller at address 0x4c. + +Override the I2C6 pin power source at board level because the IO rail +voltage is board-specific. + +Signed-off-by: Troy Mitchell +--- + arch/riscv/boot/dts/spacemit/k3-com260.dtsi | 17 +++++++++++++++++ + 1 file changed, 17 insertions(+) + +diff --git a/arch/riscv/boot/dts/spacemit/k3-com260.dtsi b/arch/riscv/boot/dts/spacemit/k3-com260.dtsi +index 37627963a649..92dea8639779 100644 +--- a/arch/riscv/boot/dts/spacemit/k3-com260.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3-com260.dtsi +@@ -58,6 +58,23 @@ serial-number { + }; + }; + ++&i2c6_1_cfg { ++ i2c6-1-pins { ++ power-source = <3300>; ++ }; ++}; ++ ++&i2c6 { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&i2c6_1_cfg>; ++ status = "okay"; ++ ++ temperature-sensor@4c { ++ compatible = "sensylink,ctf2301"; ++ reg = <0x4c>; ++ }; ++}; ++ + &i2c8 { + pinctrl-names = "default"; + pinctrl-0 = <&i2c8_cfg>; + +-- +2.55.0 + diff --git a/spacemit/k3-pico-itx/patches/0021-dts-k3-com260-fan-cooling.patch b/spacemit/k3-pico-itx/patches/0021-dts-k3-com260-fan-cooling.patch new file mode 100644 index 00000000..5eea7040 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0021-dts-k3-com260-fan-cooling.patch @@ -0,0 +1,67 @@ +From: liberodark +Subject: [PATCH] riscv: dts: spacemit: k3: wire the CoM260 fan to the thermal zones + +The CTF2301 comes up in manual mode with a zero duty cycle and an +auto-temp lookup table still at its reset values, so the fan never spins +on its own. Register it as a thermal cooling device and let the CPU +cluster thermal zone drive it. + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3-com260.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3-com260.dtsi +@@ -69,9 +69,10 @@ + pinctrl-0 = <&i2c6_1_cfg>; + status = "okay"; + +- temperature-sensor@4c { ++ fan: temperature-sensor@4c { + compatible = "sensylink,ctf2301"; + reg = <0x4c>; ++ #cooling-cells = <2>; + }; + }; + +@@ -237,3 +238,42 @@ + pinctrl-0 = <&uart0_0_cfg>; + status = "okay"; + }; ++ ++&{/thermal-zones/cluster-thermal} { ++ trips { ++ fan_low: fan-low { ++ temperature = <55000>; ++ hysteresis = <5000>; ++ type = "active"; ++ }; ++ ++ fan_mid: fan-mid { ++ temperature = <65000>; ++ hysteresis = <5000>; ++ type = "active"; ++ }; ++ ++ fan_high: fan-high { ++ temperature = <75000>; ++ hysteresis = <5000>; ++ type = "active"; ++ }; ++ }; ++ ++ cooling-maps { ++ map0 { ++ trip = <&fan_low>; ++ cooling-device = <&fan 60 120>; ++ }; ++ ++ map1 { ++ trip = <&fan_mid>; ++ cooling-device = <&fan 120 190>; ++ }; ++ ++ map2 { ++ trip = <&fan_high>; ++ cooling-device = <&fan 190 255>; ++ }; ++ }; ++}; diff --git a/spacemit/k3-pico-itx/patches/0022-dts-k3-com260-qspi-nor.patch b/spacemit/k3-pico-itx/patches/0022-dts-k3-com260-qspi-nor.patch new file mode 100644 index 00000000..97112779 --- /dev/null +++ b/spacemit/k3-pico-itx/patches/0022-dts-k3-com260-qspi-nor.patch @@ -0,0 +1,70 @@ +From: liberodark +Subject: [PATCH] riscv: dts: spacemit: k3: describe the CoM260 boot NOR flash + +Same QSPI NOR and partition layout as the Pico-ITX, so the firmware +partitions show up as MTD devices and can be updated from Linux. + +Signed-off-by: liberodark +--- +--- a/arch/riscv/boot/dts/spacemit/k3-com260.dtsi ++++ b/arch/riscv/boot/dts/spacemit/k3-com260.dtsi +@@ -277,3 +277,59 @@ + }; + }; + }; ++ ++&qspi_cfg { ++ qspi-pins { ++ power-source = <1800>; ++ }; ++ ++ qspi-cs0-pins { ++ power-source = <1800>; ++ }; ++}; ++ ++&qspi { ++ pinctrl-names = "default"; ++ pinctrl-0 = <&qspi_cfg>; ++ status = "okay"; ++ ++ flash@0 { ++ compatible = "jedec,spi-nor"; ++ reg = <0>; ++ spi-max-frequency = <26500000>; ++ spi-rx-bus-width = <4>; ++ spi-tx-bus-width = <4>; ++ vcc-supply = <&aldo2>; /* PMIC_VCC1V8_QSPI */ ++ m25p,fast-read; ++ ++ partitions { ++ compatible = "fixed-partitions"; ++ #address-cells = <1>; ++ #size-cells = <1>; ++ ++ bootinfo@0 { ++ reg = <0x0 0x20000>; ++ }; ++ ++ fsbl@20000 { ++ reg = <0x20000 0x80000>; ++ }; ++ ++ env@a0000 { ++ reg = <0xa0000 0x10000>; ++ }; ++ ++ esos@b0000 { ++ reg = <0xb0000 0x100000>; ++ }; ++ ++ opensbi@1b0000 { ++ reg = <0x1b0000 0x60000>; ++ }; ++ ++ uboot@210000 { ++ reg = <0x210000 0x5f0000>; ++ }; ++ }; ++ }; ++}; diff --git a/spacemit/k3-pico-itx/sd-image.nix b/spacemit/k3-pico-itx/sd-image.nix index b09b0c3a..d2c47c5c 100644 --- a/spacemit/k3-pico-itx/sd-image.nix +++ b/spacemit/k3-pico-itx/sd-image.nix @@ -33,7 +33,7 @@ image.fileName = "${config.image.baseName}-${config.system.nixos.label}-${pkgs.stdenv.hostPlatform.system}-k3-pico-itx.img"; sdImage = { - expandOnBoot = false; + expandOnBoot = true; firmwarePartitionOffset = 12; firmwarePartitionName = "ESP"; firmwareSize = 256; @@ -84,13 +84,13 @@ populateRootCommands = ""; postBuildCommands = '' - eval $(${pkgs.util-linux}/bin/partx $img -o START,SECTORS --nr 2 --pairs) + eval $(${pkgs.buildPackages.util-linux}/bin/partx $img -o START,SECTORS --nr 2 --pairs) ROOTFS_START=$START ROOTFS_SECTORS=$SECTORS truncate -s '+2M' $img - ${pkgs.util-linux}/bin/sfdisk $img <.raspberry-pi-config-txt + +{ + lib, + pkgs, + runCommand, + writeText, +}: + +let + configtxt = ../raspberry-pi/common/config-txt.nix; + + # Render without a full NixOS evaluation. + render = + module: + (lib.evalModules { + modules = [ + configtxt + { _module.args = { inherit pkgs; }; } + module + ]; + }).config.hardware.raspberry-pi.configtxt.file; + + cases = { + empty = { + module = { }; + expected = ""; + }; + + settingsOnly = { + module = { + hardware.raspberry-pi.configtxt.settings.all.arm_boost = true; + }; + expected = '' + [all] + arm_boost=1 + ''; + }; + + settingsValueTypes = { + module = { + hardware.raspberry-pi.configtxt.settings.all = { + yes = true; + no = false; + num = 2; + text = "abc"; + }; + }; + expected = '' + [all] + no=0 + num=2 + text=abc + yes=1 + ''; + }; + + stackedFilters = { + module = { + hardware.raspberry-pi.configtxt.settings.pi4."gpio4=1".arm_freq = 1000; + }; + expected = '' + [all] + [gpio4=1] + [pi4] + arm_freq=1000 + ''; + }; + + # "all" is stripped before sorting, so all.z lands after b. + groupSortOrder = { + module = { + hardware.raspberry-pi.configtxt.settings = { + all.z.foo = 1; + b.bar = 2; + }; + }; + expected = '' + [all] + [b] + bar=2 + + [all] + [z] + foo=1 + ''; + }; + + # This is what makes mkForce null remove a default. + nullDropsOut = { + module = { + hardware.raspberry-pi.configtxt.settings.all = { + kept = true; + dropped = null; + }; + }; + expected = '' + [all] + kept=1 + ''; + }; + + listsRepeatKeys = { + module = { + hardware.raspberry-pi.configtxt.settings.all.dtparam = [ + "audio=on" + "i2c_arm=on" + ]; + }; + expected = '' + [all] + dtparam=audio=on + dtparam=i2c_arm=on + ''; + }; + + overlayWithParams = { + module = { + hardware.raspberry-pi.configtxt.deviceTreeOverlays.all = [ + { dwc2.dr_mode = "host"; } + ]; + }; + expected = '' + [all] + dtoverlay=dwc2 + dtparam=dr_mode=host + dtoverlay= + ''; + }; + + # Overlay parameters use on/off, unlike settings. + overlayBooleans = { + module = { + hardware.raspberry-pi.configtxt.deviceTreeOverlays.all = [ + { + example = { + yes = true; + no = false; + dropped = null; + }; + } + ]; + }; + expected = '' + [all] + dtoverlay=example + dtparam=no=off + dtparam=yes=on + dtoverlay= + ''; + }; + + duplicateOverlays = { + module = { + hardware.raspberry-pi.configtxt.deviceTreeOverlays.all = [ + { mcp251xfd.interrupt = 25; } + { mcp251xfd.interrupt = 24; } + ]; + }; + expected = '' + [all] + dtoverlay=mcp251xfd + dtparam=interrupt=25 + dtoverlay= + dtoverlay=mcp251xfd + dtparam=interrupt=24 + dtoverlay= + ''; + }; + + overlayOrder = { + module = { + hardware.raspberry-pi.configtxt.deviceTreeOverlays.all = [ + { first = { }; } + { second = { }; } + ]; + }; + expected = '' + [all] + dtoverlay=first + dtoverlay= + dtoverlay=second + dtoverlay= + ''; + }; + + # all.pi4 and pi4.all are the same filter set, so they merge. + mergedFilterPaths = { + module = { + hardware.raspberry-pi.configtxt.deviceTreeOverlays = { + all.pi4 = [ { first = { }; } ]; + pi4.all = [ { second = { }; } ]; + }; + }; + expected = '' + [all] + [pi4] + dtoverlay=first + dtoverlay= + dtoverlay=second + dtoverlay= + ''; + }; + + # dtoverlay sorts before dtparam, so rendering settings first is what keeps a + # base parameter out of an overlay's scope. + settingsRenderBeforeOverlays = { + module = { + hardware.raspberry-pi.configtxt = { + settings.all.dtparam = [ "audio=on" ]; + deviceTreeOverlays.all = [ { vc4-kms-v3d = { }; } ]; + }; + }; + expected = '' + [all] + dtparam=audio=on + + [all] + dtoverlay=vc4-kms-v3d + dtoverlay= + ''; + }; + }; + + # One derivation per case, so a single test can be built on its own. + tests = lib.mapAttrs ( + name: case: + runCommand "config-txt-${name}" { } '' + diff -u ${writeText "${name}-expected.txt" case.expected} ${render case.module} + touch $out + '' + ) cases; +in +# Building this builds every case, because naming them here makes them inputs. +# passthru re-exposes each one, so a single case can be built on its own with +# `nix build .#checks..raspberry-pi-config-txt.`. +runCommand "raspberry-pi-config-txt-tests" { + cases = lib.attrValues tests; + passthru = tests; +} "touch $out" diff --git a/tests/flake.lock b/tests/flake.lock index 09bc7112..b2249ce8 100644 --- a/tests/flake.lock +++ b/tests/flake.lock @@ -2,11 +2,11 @@ "nodes": { "nixos-stable": { "locked": { - "lastModified": 1784011430, - "narHash": "sha256-LX2eBS9gFbXL5xUNBwp5mZ0tjnNvgiXoDnhz3ee1LzY=", - "rev": "8eeec934ae0dbeca3d7868c059568a65c08b2fc3", + "lastModified": 1789542786, + "narHash": "sha256-BGtBoaZNpOPDEBLTiWNFdYppnDjGhwrUq5/P15QwrrM=", + "rev": "4c7870105e7f1fdf9c48688c8d7efc21abf0688a", "type": "tarball", - "url": "https://releases.nixos.org/nixos/26.05/nixos-26.05.4937.8eeec934ae0d/nixexprs.tar.xz" + "url": "https://releases.nixos.org/nixos/26.05/nixos-26.05.9904.4c7870105e7f/nixexprs.tar.xz" }, "original": { "type": "tarball", @@ -15,11 +15,11 @@ }, "nixos-unstable-small": { "locked": { - "lastModified": 1784157011, - "narHash": "sha256-k3oN75PtDT9xJfc4zqgah7ulSaw1Bv/XDuMrXaHupEE=", - "rev": "d4a57f162b50db6d7e49580a772ccbdb3a1605a4", + "lastModified": 1789590615, + "narHash": "sha256-Kdzc9cA73knnnxYsWcfV1fL1Ru4sCYSjRvIpZBFKrL8=", + "rev": "bc071daf4c377dd821398672fafe269d5c654ce8", "type": "tarball", - "url": "https://releases.nixos.org/nixos/unstable-small/nixos-26.11pre1035646.d4a57f162b50/nixexprs.tar.xz" + "url": "https://releases.nixos.org/nixos/unstable-small/nixos-26.11pre1075076.bc071daf4c37/nixexprs.tar.xz" }, "original": { "type": "tarball",