################################################################################
# Helper script: /load_kernel_modules.scr/platform/aarch64/bcm2711
# Purpose......: Set the Kernel modules for Hardware Models that use the BCM2711
#                SoC within the Slackware initial RAM disk ('OS initrd')
#                and the Slackware installer.
#                Currently supported Hardware Models:
#                * Raspberry Pi 4
#                This script is sourced from '/load_kernel_modules'
# Author.......: Stuart Winter <mozes@slackware.com>
# Date.........: 30-Mar-2021
# Maintainer...: Stuart Winter, Brent Earl.
#
# Please use this as an reference and example of how to add support for
# a new Hardware Model to Slackware ARM/AArch64.
#
# Important Note:
# * You must _append_ to the module lists (as this script does)
#   otherwise the base set of modules will not be loaded and would result in
#   a Kernel panic.
# * The initrd uses the 'bash' shell, rather than 'busybox'
#   (as in upstream/x86 Slackware). This allows you (for example)
#   to use 'fallthrough' (case statements terminated with ';&'
#   rather than ';;') within case statements and regular expression
#   matches within 'if' statements.
#   This permits the 'c'-style switch statements where you can
#   'inherit' the previous matches as you move down the cases.
#   This enables you to match on varients of boards, and inherit
#   a 'baseline' of platform modules for that board.
#
# The 'PLATWALK' match is to enable build scripts to process these
# scripts outside of the initrd environment and determine which
# modules will be loaded by a particular Hardware Model.  This must remain
# in place for all scripts.
################################################################################

# $HWM is set by the caller ('/load_kernel_modules') of this helper script
# and the value is taken from /proc/device-tree/model.
# If your Hardware Model does not have a /proc/device-tree/model interface
# (such as QEMU), see the 'virt-qemu' loader script for the other Hardware
# Model detection method.
case $HWM in
   "Raspberry Pi 4"*|PLATWALK)
      platform_detected=1
      SOC_NAME=bcm2711
      # Set the short name that is used by /load_kernel_modules to install
      # the appropriate configuration for modprobe for this Hardware Model.
      # These files are stored within the source tree:
      # source/k/SlkOS-initrd-overlay/usr/share/hwm-configure/platform/aarch64/modprobe.d/
      #
      # Note: Typically these are only used to blacklist particular modules from loading
      # within the OS InitRD or Installer.  Within the OS the regular location of
      # /lib/modprobe.d/ is used and has no connection to the content of the
      # OS InitRD/Installer.
      HWM_SHORTNAME=rpi4

      echo "Architecture: ${ARCH}, Hardware model: ${HWM}, SoC: ${SOC_NAME}"
      # If one of the modules within the base list is causing problems on
      # your platform, here are the options laid out as examples:
      # USB="${USB/ehci_orion/differentmodule}" # Substitute module 'ehci_orion' with 'differentmodule'
      # USB="${USB/ehci_orion/}" # Remove the 'ehci_orion' module from the list
      MOD_GPIO+="   "
      # Drivers for PCI and other core sub systems:
      MOD_PHY+="    bcm_phy_lib phy_generic"
      # MFD (Multi Functional Devices) drivers:
      MOD_MFD+="    "
      # Video drivers:
      MOD_VIDEO+="  simpledrm v3d"
      # sdhci-iproc caused crash in 5.19 taking down the SD subsystem:
      # Seems to work in 6.1.
      MOD_CARDS+="  sdhci-iproc"
      MOD_USB+="    dwc2 dwc3"
      MOD_NET+="    "
      MOD_CMP+="    "
      MOD_CRYPTO+=" "
      # Example to detect hardware at runtime:
      #{ lspci 2>/dev/null | grep -q 'SATA cont.*ATA' ;} && MOD_XX+=" yyy"
      # The following modules do not inherit:
      MOD_RTC="rtc-ds1307 rtc-pcf8523"
      # Modules for the peripherals on the Hardware Model's main board
      # (outside of the SoC itself)
      # Note: the distinction between the SoC and HWM is blurred presently
      #       This needs organising according to the hardware - TODO!
      MOD_HWM="pcie_brcmstb mdio_bcm_unimac iproc_rng200 pwrseq_simple crct10dif_ce"
      # Modules for the IP blocks/peripherals embedded within the SoC:
      MOD_SOC="broadcom clk_raspberrypi pwm-bcm2835 raspberrypi-hwmon i2c-bcm2835 i2c_brcmstb"

      # /load_kernel_modules provides the ability to run a defined function
      # just prior to loading the Linux Kernel module lists that have been
      # defined in the base set + directly above these lines.
      # There's no current use case for this - it's here to expose it as
      # an option should it be useful at some future time.
      #function hwm_hook_pre-modload() {
      #}
      #
      # And the same functionality once the Kernel modules have been loaded.
      # This allows us to set up the RTC, which we cannot do at the time this
      # script is loaded initially, as the RTC device is only addressable upon
      # having loaded the driver into the Kernel.
      # To achieve this effect locally, users should look at
      # /boot/local/README.txt
      # and create /boot/local/load_kernel_modules.post from the sample file.
      # This is a shell script in which you can 'modprobe' additional modules,
      # and run shell code as we do below to write a value into the RTC's
      # Kernel interface to initialise it.
      #
      # This function is here because this particular RTC is officially
      # supported by Slackware AArch64, as it's connected to this author's
      # RPi4.
      #
      function hwm_hook_post-modload() {
         # Initialise a DS1307 RTC without Device Tree Overlay.
         #
         # When booting Slackware directly from the RPi's native Boot Loader,
         # we add a Kernel command line operator 'slkrpinbl' which enables
         # these types of conditional actions:
         grep -iq "slkrpinbl" /proc/cmdline 2> /dev/null || {
            # Silently detect peripherals on bus 1.
            # Something like this, self-contained would be ideal:
            #sensors-detect --auto > /dev/null 2>&1
            i2cdetect -y 1 > /dev/null 2>&1
            # Unless you change this setting, it'll appear as position 68.
            [ -f /sys/class/i2c-adapter/i2c-1/new_device ] && \
               echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device
         }
      }

      # Define a function to run from the OS InitRD's '/init' immediately prior
      # to switching into the Slackware OS proper.
      # This is after any software RAID arrays et al have been initialised.
      # There's no current use case for this, but it may be useful in the future.
      #
      # At this stage, the following paths for Slackware OS are mounted:
      # /proc, /sys, /run, /dev
      # The root file system ('/') is mounted under /mnt
      #
      #function hwm_hook_pre_switch_root() {
      #  echo "Just about to switch into the Slackware OS proper, leaving the OS InitRD"
      #  sleep 4
      #}

     # End of stanza - the ';;' below must be the last line:
     ;;
esac

# The '/load_kernel_modules' script will load the modules
# set above.
