# Copyright (c) 2007, Technologic Systems.
# All rights reserved.

# Note: Be sure you have 64-bit arithmetic compiled into your shell, or some
# of these functions will fail (e.g. for ADDRESS > 0x7FFFFFFF)

# bit_set BIT_WIDTH ADDRESS BIT_NUMBER
bit_set() {
  local MASK VAL
  MASK=$((1<<$3))
  VAL=`peekpoke $1 $2`
  VAL=$((MASK|VAL))
  peekpoke $1 $2 $VAL
}
                                                                                                                    
# bit_clr BIT_WIDTH ADDRESS BIT_NUMBER
bit_clr() {
  local MASK VAL
  MASK=$((1<<$3))
  VAL=`peekpoke $1 $2`
  VAL=$((~MASK&VAL))
  peekpoke $1 $2 $VAL
}
                                                                                                                    
# bit_get BIT_WIDTH ADDRESS BIT_NUMBER
bit_get() {
  local MASK VAL
  MASK=$((1<<$3))
  VAL=`peekpoke $1 $2`
  VAL=$((VAL>>$3))
  VAL=$((VAL&1))
  echo $VAL
}

led0() {
  if test "$1" = "0"; then
    bit_clr 32 0xe8000008 30 > /dev/null
  else
    bit_set 32 0xe8000008 30 > /dev/null
  fi
}

pc104on() {
  (
  peekpoke 32 0xe8000030 0x55555555
  peekpoke 32 0xe8000034 0x55555555
  peekpoke 32 0xe8000038 0x55555
  peekpoke 32 0xe800003c 0x55555
  ) > /dev/null 2>&1
}

pc104off() {
  (
  peekpoke 32 0xe8000030 0x0
  peekpoke 32 0xe8000034 0x0
  peekpoke 32 0xe8000038 0x0
  peekpoke 32 0xe800003c 0x0
  ) > /dev/null 2>&1
}

tshelp() {
  echo "Technologic Systems TS-7800 Linux miniroot commands:"
  echo "===================================================="
  echo
  echo "'save' - save current miniroot initrd configuration to flash"
  echo "'exit' - start full Linux boot or exits telnet session"
  echo "'createmtdroot' - create bootable onboard flash filesystem from /mnt/root and sdcardB"
  echo "'sdmount' - makes available executables on SD card at /mnt/root"
  echo "'mtdmount' - makes available NAND flash at /onboardflash directory"
  echo "'help' - lists busybox shell built-in Linux commands"
  echo
  echo "To change the default Linux root filesystem, link a different sample"
  echo "linuxrc bootup script by typing (e.g.) the following command:"
  echo "  ln -sf /linuxrc-sdroot /linuxrc; save"
  echo "Several sample linuxrc scripts are available:"
  echo "  '/linuxrc-sdroot' - boots to SD flash filesystem on partition #4"
  echo "  '/linuxrc-mtdroot' - boots to YAFFS NAND flash"
  echo "  '/linuxrc-fastboot' - unconditionally drops to fastboot shell on bootup"
  echo "  '/linuxrc-nfsroot' - boots to network NFS root"
  echo "Auto-booting of NAND flash, SD card, or NFS will be aborted to"
  echo "the fastboot shell prompt if the presence of a file named '/fastboot'"
  echo "is present on the filesystem.  You may customize bootup behavior"
  echo "further by modifying these simple linuxrc shell script samples."
  echo
  echo "Contact support@embeddedARM.com for further questions."
}

sdmount() {
  (
  insmod /tssdcard.ko 
  mdev -s
  mount /dev/tssdcardb4 /mnt/root
  ) > /dev/null 2>&1
}

mtdmount() {
  (
  insmod /ts7800_nand.ko
  mdev -s
  mount -t yaffs2 /dev/mtdblock3 /onboardflash
  ) > /dev/null 2>&1
}

help() {
  /bin/busybox
}

