From 57f5952f8ab446416cd7f8efa47986431267b8f2 Mon Sep 17 00:00:00 2001 From: coolneng Date: Sat, 15 May 2021 16:51:46 +0200 Subject: [PATCH] Initialize the SATA HAT on startup --- configuration.nix | 2 ++ modules/periodic.nix | 17 +++++++++++++- scripts/SATA-hat.sh | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100755 scripts/SATA-hat.sh diff --git a/configuration.nix b/configuration.nix index c50f6c1..132f80d 100644 --- a/configuration.nix +++ b/configuration.nix @@ -9,6 +9,8 @@ "console=TTYAMA0,115200" "console=tty1" "8250.nr_uarts=1" + "iomem=relaxed" + "strict-devmem=0" ]; boot.loader.raspberryPi = { enable = true; diff --git a/modules/periodic.nix b/modules/periodic.nix index a36715a..92060dd 100644 --- a/modules/periodic.nix +++ b/modules/periodic.nix @@ -28,7 +28,22 @@ in { startAt = "02:00:00"; }; - wantedBy = [ "default.target" ]; + # Enable SATA HAT + systemd.services.sata-hat = { + description = "Enable software support for SATA Hat"; + wantedBy = [ "zfs-import.target" ]; + script = '' + ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh on" + ''; + serviceConfig = { + Type = "oneshot"; + RemainAfterExit = "yes"; + ExecStop = '' + ${pkgs.bash}/bin/bash -c "/etc/nixos/scripts/SATA-hat.sh off" + ''; }; + before = [ "zfs-import.target" "zfs-import-vault.service" ]; + requires = [ "systemd-udev-settle.service" ]; + after = [ "systemd-udev-settle.service" ]; }; } diff --git a/scripts/SATA-hat.sh b/scripts/SATA-hat.sh new file mode 100755 index 0000000..ac721b9 --- /dev/null +++ b/scripts/SATA-hat.sh @@ -0,0 +1,56 @@ +#!/bin/sh + +BASE_PATH=/sys/class/gpio + +# GPIO pins +SATA0=26 +SATA1=25 +CPU_FAN=12 + +# Values +LOW=0 +HIGH=1 + +export_pin() { + if [ ! -e $BASE_PATH/gpio"$1" ]; then + echo "$1" >$BASE_PATH/export + fi +} + +unexport_pin() { + if [ -e $BASE_PATH/gpio"$1" ]; then + echo "$1" >$BASE_PATH/unexport + fi +} + +set_mode() { + export_pin "$1" + echo "out" >$BASE_PATH/gpio"$1"/direction + echo "$2" >$BASE_PATH/gpio"$1"/value + if [ "$3" = "clean" ]; then + unexport_pin "$1" + fi +} + +turn_on() { + set_mode $SATA0 $HIGH + sleep 1 + set_mode $SATA1 $HIGH + set_mode $CPU_FAN $LOW +} + +turn_off() { + set_mode $SATA0 $LOW clean + set_mode $SATA1 $LOW clean + set_mode $CPU_FAN $LOW clean +} + +trap turn_off INT + +if [ "$1" = "on" ]; then + turn_on +else + turn_off +fi + +exit 0