#!/bin/sh

start_atg () {
	var=$1
	# read the GPIO bank2 direction register
	dirm="$(devmem 0xE000A284)"
	# read the GPIO bank2 output enable register
	oen="$(devmem 0xE000A048)"
	# read the GPIO bank2 data register
	data="$(devmem 0xE000A010)"

	val1=$dirm
	val2=$oen
	val3=$data
	if [ $var -eq 3 ]
	then
		# enable the EMIO pin6 to start the ATG3
		dirm=$((dirm|0x40))
		oen=$((oen|0x40))
		data=$((data|0x40))
	elif [ $var -eq 4 ]
	then
		# enable the EMIO pin8 to start the ATG4
		dirm=$((dirm|0x100))
		oen=$((oen|0x100))
		data=$((data|0x100))
	fi
	devmem 0xE000A284 32 $dirm
	devmem 0xE000A048 32 $oen
	devmem 0xE000A010 32 $data
	devmem 0xE000A284 32 $val1
	devmem 0xE000A048 32 $val2
	devmem 0xE000A010 32 $val3
}

stop_atg () {
	var=$1
	# read the GPIO bank2 direction register
	dirm="$(devmem 0xE000A284)"
	# read the GPIO bank2 output enable register
	oen="$(devmem 0xE000A048)"
	# read the GPIO bank2 data register
	data="$(devmem 0xE000A010)"

	val1=$dirm
	val2=$oen
	val3=$data
	if [ $var -eq 3 ]
	then
		# enable the EMIO pin7 to stop the ATG3
		dirm=$((dirm|0x80))
		oen=$((oen|0x80))
		data=$((data|0x80))
	elif [ $var -eq 4 ]
	then
		# enable the EMIO pin to stop the ATG4
		dirm=$((dirm|0x200))
		oen=$((oen|0x200))
		data=$((data|0x200))
	fi
	devmem 0xE000A284 32 $dirm
	devmem 0xE000A048 32 $oen
	devmem 0xE000A010 32 $data
	devmem 0xE000A284 32 $val1
	devmem 0xE000A048 32 $val2
	devmem 0xE000A010 32 $val3
}

val=$1
if [ $val -eq 1 ]
then
	#start the two ATG core IP's
	#echo Trigger ATG ACP controllers
	var=$((0+3))
	while [  $var -le 4 ]
	do
		#echo start Pulse to ATG$var
		#/bin/sh /media/card/bin/start_atg.sh $var
		start_atg $var
		var=$((var+1))
	done
	#echo Trigger ATG ACP controllers Done
elif [ $val -eq 0 ]
then
	#stop the two ATG core IP's
	#echo Stop ATG ACP controllers
	var=$((0+3))
	while [  $var -le 4 ]
	do
		#echo stop Pulse to ATG$var
		#/bin/sh /media/card/bin/stop_atg.sh $var
		stop_atg $var
		var=$((var+1))
	done
	#echo Stop ATG ACP controllers Done
fi