# 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
}

# dio_dir_get DIO_NUM(0..15)
dio_dir_get() {
  local TMP ADRS

  if test $1 -gt 7; then
    let TMP=$1-8
    let ADRS=0x12C00001
  else
    let TMP=$1
    let ADRS=0x12C00000
  fi

  bit_get 8 $ADRS $TMP
}

# dio_dir_set DIO_NUM(0..15) DIR(0=input,1=output)
dio_dir_set() {
  local TMP ADRS

  if test $1 -gt 7; then
    let TMP=$1-8
    let ADRS=0x12C00001
  else
    let TMP=$1
    let ADRS=0x12C00000
  fi

  if test $2 -eq 0; then
    bit_clr 8 $ADRS $TMP
  else
    bit_set 8 $ADRS $TMP
  fi
}

# dio_data_get DIO_NUM(0..15)
dio_data_get() {
  local ADRS TMP VAL

  if test $1 -gt 7; then
    let TMP=$1-8
    let ADRS=0x12C00003
  else
    let TMP=$1
    let ADRS=0x12C00002
  fi
  bit_get 8 $ADRS $TMP
}

# dio_data_set DIO_NUM(0..15) 0|1
dio_data_set() {
  local ADRS TMP VAL

  if test $1 -gt 7; then
    let TMP=$1-8
    let ADRS=0x12C00003
  else
    let TMP=$1
    let ADRS=0x12C00002
  fi
  if test "$2" = "1"; then
    bit_set 8 $ADRS $TMP
  else
    bit_clr 8 $ADRS $TMP
  fi
}

# usb_init
usb_init() {
  bit_set 32 0x80930004 28 > /dev/null
  bit_clr 32 0x80020004 6 > /dev/null
  bit_set 32 0x80020004 7 > /dev/null
}

# usb_off
usb_off() {
  bit_clr 32 0x80930004 28 > /dev/null
}

# usb_numports
usb_numports() {
  local NUM

  let NUM=`peekpoke 32 0x80020048`
  let NUM=NUM\&255
  echo $NUM
}

# usb_port_devexists PORTNUM(zero indexed)
usb_port_devexists() {
  local ADDR

  let ADDR=0x80020054
  let ADDR=$ADDR+4*$1
  bit_get 32 $ADDR 0
}

eth_off() {
  bit_set 32 0x80840044 2 > /dev/null
  bit_clr 32 0x80840040 2 > /dev/null
}

eth_on() {
  bit_set 32 0x80840040 2 > /dev/null
}

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

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

# 200/100
cpu_speed_max() {
  peekpoke 32 0x80930020 0x02a4bb36 > /dev/null
  peekpoke 32 0x80060008 0x30D > /dev/null
}

# 166/66
cpu_speed_166() {
  peekpoke 32 0x80930020 0x02b4fa5a > /dev/null
  peekpoke 32 0x80060008 0x203 > /dev/null
}

# 41.5Mhz
cpu_speed_42() {
  peekpoke 32 0x80930020 0x0296fa5a > /dev/null
  peekpoke 32 0x80060008 0x144 > /dev/null
}

# 14.7456Mhz
cpu_speed_min() {
  peekpoke 32 0x80930020 0 > /dev/null
  peekpoke 32 0x80060008 0x73 > /dev/null
}

pc104on() {
  (
  bit_set  16 0x600ff0d4 11
  peekpoke 16 0x600ff098 0x5555
  peekpoke 16 0x600ff09a 0x5555
  peekpoke 16 0x600ff09c 0x5555
  peekpoke 16 0x600ff09e 0x5555
  ) > /dev/null 2>&1
}
                                                                                                                      
pc104off() {
  (
  peekpoke 16 0x600ff098 0x0
  peekpoke 16 0x600ff09a 0x0
  peekpoke 16 0x600ff09c 0x0
  peekpoke 16 0x600ff09e 0x0
  ) > /dev/null 2>&1
}

nand_on() {
  bit_set 16 0x600ff086 12 > /dev/null
}

nand_off() {
  bit_clr 16 0x600ff086 12 > /dev/null
}

lcd_on() {
  bit_set 16 0x600ff086 14 > /dev/null
}

lcd_off() {
  bit_clr 16 0x600ff086 14 > /dev/null
}

speaker_on() {
  bit_set 16 0x600ff086 11 > /dev/null
}
                                                                                                                                    
speaker_off() {
  bit_clr 16 0x600ff086 11 > /dev/null
}

wdt_on_338ms() {
  peekpoke 16 0x600ff0d6 0x0 > /dev/null 2>&1
}

wdt_on_2706ms() {
  peekpoke 16 0x600ff0d6 0x1 > /dev/null 2>&1
}

wdt_on_10824ms() {
  peekpoke 16 0x600ff0d6 0x2 > /dev/null 2>&1
}

wdt_on() {
  wdt_on_10824ms 
}

wdt_off() {
  peekpoke 16 0x600ff0d6 0x3 > /dev/null 2>&1
}

is_ts7390 () {
  local VAL
  let VAL=`peekpoke 16 0x600ff080`
  let VAL=$VAL\&0xfff0
  if test $VAL -eq 52704; then
    echo "1"
  else
    echo "0"
  fi
}

tshelp() {
  echo "Technologic Systems TS-7000 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 "'createmtdboot' - create bootable flash image from sdcard"
  echo "'createmtdroot' - create bootable flash filesystem from /mnt/root and sdcard"
  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/tssdcarda4 /mnt/root -o ro
  fsck /dev/tssdcarda4
  mount -o remount,rw /dev/tssdcarda4 /mnt/root
  ) > /dev/null 2>&1
}

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

help() {
  /bin/busybox
}

