warn1="WARNING: do NOT power-down while saving/copying!"
img="[kernel+initrd]"

# ----------------------------------------------------------
# Display the given number in binary format
printbin() {
# NOTE: assume only 7 digits, since getdio() only seem to give 7
#       will only print the first 7 digits either way

	digits="1 2 3 4 5 6 7"
	# Loop thru the given hex digits
	for digit in $digits; do
		num=`echo $1 | cut -c${digit}`
		if 	[ "$num" = "a" ]; then num="10"
		elif 	[ "$num" = "b" ]; then num="11"
		elif 	[ "$num" = "c" ]; then num="12"
		elif 	[ "$num" = "d" ]; then num="13"
		elif 	[ "$num" = "e" ]; then num="14"
		elif 	[ "$num" = "f" ]; then num="15"
		fi

		# Loop thru the 4 binary digits:
		# AND with 0x8 to get the MSB, display it
		# SHIFT left, do the next digit
		loops="1 2 3 4 "
		for loop in $loops; do
			bin=$((num&0x8));
			if [ "$bin" = "0" ]; then 
				echo -n "0"
			else 
				echo -n "1" 
			fi
			num=$((num<<1))
		done
		echo -n " "	
	done	
	# echo	
}


#cvtime() {
#	local x sec tenths
#	let x=`devmem 0x79000040`
#	sec=$((x / 100000))
#	tenths=$((x % 100000 / 1000))
#	printf "%d.%02d" $sec $tenths
	
# Return the value read from mem
#return $x
#}

usbload() {                                
        mount -t tmpfs tmpfs /lib/modules
        tar -x -z -f /modules.tar.gz -C /                                       
        modprobe scsi_mod                   
        modprobe sd_mod                       
        modprobe usb_storage                           
	modprobe smsc95xx
        umount /lib/modules                            
}

save() {

# Determine the boot device used:
	# 	media		dev
	#--------------------
	# NAND	1		
	# mSD	0
	#--------------------
	# 
	local dev=`ts4700ctl -i | grep bootdev | cut -d= -f2 | cut -c3-`
	if [ $dev -eq 0 ]; then
		sdsave
	else	nandsave
	fi
}


sdsave() {
	echo "$warn1"
	echo -n "Saving [initrd] to mSD ... "
	#local sz=`fdisk -lu /dev/mmcblk0 | awk '/mmcblk0p3/ { print 1+$3-$2; }'`
	mount -o remount,ro /
	dd if=/dev/ram0 of=/dev/mmcblk0p3 conv=fsync bs=131072 count=20 >/dev/null 2>&1
	#dd if=/dev/ram0 of=/dev/mmcblk0p3 count=$sz >/dev/null 2>&1
	mount -o remount,rw /
	#mount -oro /dev/mmcblk0p4 /mnt/root
	echo "done"
}


nandsave() {
	echo "$warn1"
	echo -n "Saving [initrd] to NAND ... "
	mount -o remount,ro /
	sync
	dd if=/dev/ram0 of=/dev/nbd2 bs=131072 count=20 conv=fsync >/dev/null 2>&1
	mount -o remount,rw /
	sync
	echo "done"
}


sd2nand() {
	echo "$warn1"
	echo -n "Copying $img mSD --> NAND ... "
	
	dd if=/dev/mmcblk0p2 of=/tmp/image bs=512 count=5119 >/dev/null 2>&1
	dd if=/tmp/image count=512 bs=5119 of=/dev/nbd1 conv=fsync >/dev/null 2>&1
	rm /tmp/image
	wait
	
	dd if=/dev/mmcblk0p3 of=/tmp/image bs=131072 count=20 >/dev/null 2>&1
	dd if=/tmp/image of=/dev/nbd2 bs=131072 count=20 conv=fsync >/dev/null 2>&1
	rm /tmp/image
	echo "done"
}

nand2sd() {
	echo "$warn1"
	echo -n "Copying $img $flash NAND --> mSD ... "

	dd if=/dev/nbd1 bs=5119 count=512 of=/tmp/image >/dev/null 2>&1
	dd of=/dev/mmcblk0p2 if=/tmp/image bs=512 count=5119 conv=fsync >/dev/null 2>&1
	rm /tmp/image
	wait

	dd if=/dev/nbd2 bs=131072 count=20 of=/tmp/image >/dev/null 2>&1
	dd of=/dev/mmcblk0p3 if=/tmp/image bs=131072 count=20 conv=fsync >/dev/null 2>&1
	rm /tmp/image
	echo "done"
}


#Usage: setdiopin <pin> <1,0,Z> <b>
# Second arg:
#	1= ON, 0= OFF, z= input mode
# Third arg:
#	if present, print DIO reg in binary
setdiopin() {
	if [ -z "$dio_out" ]; then
		eval `ts4700ctl --getdioreg`
	fi
	case $2 in
	0)
		let diodir="diodir | (1<<$1)"
		let dio_out="dio_out & ~(1<<$1)"
		;;
	1)
		let diodir="diodir | (1<<$1)"
		let dio_out="dio_out | (1<<$1)" 
		;;
	z|Z)
		let diodir="diodir & ~(1<<$1)"
		;;
	esac
	ts4700ctl --setdio $dio_out --setdiodir $diodir
	
	if [ "$3" = "b" ]; then
		local dir=`ts4700ctl --getdioreg | grep diodir | cut -dx -f2`
		echo -n "DIR= "
		printbin $dir
		echo
		local out=`ts4700ctl --getdioreg | grep dio_out | cut -dx -f2`
		echo -n "OUT= "
		printbin $out
		echo
	fi
	#return $dionum
}


#Usage: getdiopin <pin>
getdiopin() {
	local x dio
	eval `ts4700ctl --getdio`
	let x="(dio >> $1) & 0x1"
	echo $x
	return $x
}


setout() {
	local x
	let x="$1"
	setdiopin 33 $((x & 0x1))
	setdiopin 31 $(((x >> 1) & 0x1)) 
	setdiopin 29 $(((x >> 2) & 0x1)) 
	
# Return the value given
# NOTE: maybe we can return the ACTUAL pin status?-JW
return $x
}


getin() {
	local x dio
	eval `ts4700ctl --getdio`
	let x="(dio >> 40) & 1"
	let x="x | (((dio >> 38) & 1) << 1)"
	let x="x | (((dio >> 36) & 1) << 2)"
	let x="x | (((dio >> 34) & 1) << 3)"
	let x="x | (((dio >> 32) & 1) << 4)"
	let x="x | (((dio >> 30) & 1) << 5)"
	let x="x | (((dio >> 28) & 1) << 6)"
	let x="x | (((dio >> 26) & 1) << 7)"
	if [ -z "$1" ]; then
		printf "0x%x\n" $x
		return $x
	else
		echo $(((x >> ($1 - 1)) & 1))
		return $(((x >> ($1 - 1)) & 1)) 
	fi
}


tshelp() {
	echo "usbload:"
	echo -e "\tLoad USB kernel drivers"
	echo "setdiopin <pin> <1,0,Z> <b>:"
	echo -e "\tSet DIO header pin 1-44 (b=dump binary)"
	echo "getdiopin <pin>:"
	echo -e "\tGet DIO input state"
	echo "setout <val>:"
	echo -e "\tSet 3-bit TS-752 output val"
	echo "getin:"
	echo -e "\tReturn 8 bit TS-752 inputs"
	echo "gettemp:"
	echo -e "\tReturn Temp. Sensor reading"
	echo "save:"
	echo -e "\tSave initrd back to boot device (NANS, SD or SPI flash)"
	echo "sd2nand/nand2sd, sd2flash/flash2sd, nand2flash/flash2nand, etc:"
	echo -e "\tCopy kernel+initrd images from one media to another"
	echo "exit:" 
	echo -e "\tBoot SD card (if present) or filesystem mounted at /mnt/root"
}


gettemp() {
	templ=`i2cget -y 1 0x49 0 w`
	tempu=$templ

	let templ="(templ >> 8)"
	let tempu="(tempu & ~0xff00) << 8"
	let templ="(templ+tempu)"	
	let templ="(templ >> 5)"
	let x="(templ >> 10 & 0x1)"
	if [ "$x" = "1" ]; then
	  let templ="-(((~templ) + 1) & 0x7ff)"
	fi
	let templ="(templ / 4)"
	
	
	echo $templ
}

backlight_on() {
	peekpoke 32 0xd401500c 0x3 >/dev/null
	peekpoke 32 0xd401a004 0x60 >/dev/null
	peekpoke 32 0xd401a008 0x81 >/dev/null
}

backlight_off() {
	peekpoke 32 0xd401a004 0 >/dev/null
}

backlight_low() {
	backlight_on
	peekpoke 32 0xd401a004 0x40 >/dev/null
}

backlight_medium() {
	backlight_on
	peekpoke 32 0xd401a004 0x60 >/dev/null
}

backlight_high() {
	backlight_on
	peekpoke 32 0xd401a004 0x80 >/dev/null
}


speaker() {
# The TS-8390 and TS-8900 baseboards (maybe others) have a 
# speaker-enable line (active high).  On the TS-4700, this
# line is driven by MFP84 on the Marvell.  
# The code below ensures that this MFP is configured as a 
# GPIO output, and then the 'on' and 'off' arguments set 
# or clear this pin, respectively.

	peekpoke 32 0xd401E150 0xC80 0
	peekpoke 32 0xd4019014 0x100000 0

case "$1" in
   on)
	peekpoke 32 0xd4019020 0x100000 0
	;;

   off)
	peekpoke 32 0xd401902C 0x100000 0

	;;

   *)
	echo "Usage: speaker {on|off}"
	return
esac
}

do_splash (){
  speaker on
  amixer cset numid=5 100,100 #Headphone
  amixer cset numid=4 100,100 #Playback
  modprobe snd_soc_ts4700
  aplay /power.wav&
  gunzip -c /$SPLASH > /dev/fb0
  backlight_high
}


leds() {
	while true; do 
		if [ -e /tmp/failed ]; then
			ts4700ctl --greenledon --redledoff
			sleep .2
			ts4700ctl --greenledoff --redledon 
			sleep .2
		elif [ -e /tmp/done ]; then
			ts4700ctl --greenledon --redledoff
			sleeptil even
			ts4700ctl --greenledoff 
			sleeptil odd
		elif [ -e /tmp/running ]; then
			ts4700ctl --redledon --greenledoff
			sleeptil even
			ts4700ctl --redledoff
			sleeptil odd
		else
			sleep 1
		fi
	done
}


