diff options
author | Ori Bernstein <orib@fb.com> | 2015-09-03 13:06:18 -0700 |
---|---|---|
committer | Ori Bernstein <orib@fb.com> | 2015-09-23 18:10:45 -0700 |
commit | 67f44805b41a532b7b440773f7ee6d17dc0e97f4 (patch) | |
tree | c21a66b12c4d92a765e1ee0d47b96e2a2b9cd0d2 /common/recipes-utils/openbmc-utils/files/openbmc-utils.sh | |
parent | 2a51b7c1c2165ddb188c511e192b75f0aa0fbead (diff) | |
download | ast2050-yocto-openbmc-67f44805b41a532b7b440773f7ee6d17dc0e97f4.zip ast2050-yocto-openbmc-67f44805b41a532b7b440773f7ee6d17dc0e97f4.tar.gz |
Openbmc dev snapshot.
Diffstat (limited to 'common/recipes-utils/openbmc-utils/files/openbmc-utils.sh')
-rw-r--r-- | common/recipes-utils/openbmc-utils/files/openbmc-utils.sh | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/common/recipes-utils/openbmc-utils/files/openbmc-utils.sh b/common/recipes-utils/openbmc-utils/files/openbmc-utils.sh new file mode 100644 index 0000000..885a6eb --- /dev/null +++ b/common/recipes-utils/openbmc-utils/files/openbmc-utils.sh @@ -0,0 +1,133 @@ +# Copyright 2015-present Facebook. All Rights Reserved. +# +# This program file is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +# for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program in a file named COPYING; if not, write to the +# Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, +# Boston, MA 02110-1301 USA + +DEVMEM=/sbin/devmem +SHADOW_GPIO=/tmp/gpionames + +devmem_set_bit() { + local addr + local val + addr=$1 + val=$($DEVMEM $addr) + val=$((val | (0x1 << $2))) + $DEVMEM $addr 32 $val +} + +devmem_clear_bit() { + local addr + local val + addr=$1 + val=$($DEVMEM $addr) + val=$((val & ~(0x1 << $2))) + $DEVMEM $addr 32 $val +} + +GPIODIR="/sys/class/gpio" +GPIOEXPORT="$GPIODIR/export" + +gpio_dir() { + echo "$GPIODIR/gpio$1" +} + +gpio_name2value() { + local first remaining base val + remaining=$1 + base="${SHADOW_GPIO}/${remaining}" + if [ -L "${base}" ]; then + val=$(readlink -f ${base} 2>/dev/null) + if [ -n "${val}" ]; then + val=${val##*gpio} + if [ -n "${val}" ]; then + echo "$val" + return + fi + fi + fi + val=0 + while [ -n "$remaining" ]; do + first=${remaining:0:1} + case "$first" in + [[:lower:]]) + base=$(printf "%d" "'$first'") + base=$((base - 96)) + val=$((val * 26 + base)) + ;; + [[:upper:]]) + base=$(printf "%d" "'$first'") + base=$((base - 64)) + val=$((val * 26 + base)) + ;; + *) + if [ $val -gt 0 ]; then + val=$((val-1)) + fi + val=$((val * 8 + $remaining)) + break + ;; + esac + remaining=${remaining:1} + done + echo "$val" +} + +gpio_export() { + local gpio + gpio=$(gpio_name2value $1) + dir=$(gpio_dir $gpio) + if [ ! -d ${dir} ]; then + echo $gpio > $GPIOEXPORT + fi + if [ $# -gt 1 ]; then + if [ ! -d $SHADOW_GPIO ]; then + mkdir -p $SHADOW_GPIO + fi + ln -s $dir $SHADOW_GPIO/$2 + fi +} + +gpio_set() { + local gpio + local val + gpio=$(gpio_name2value $1) + val=$2 + dir=$(gpio_dir $gpio) + if [ ! -d ${dir} ]; then + echo $gpio > $GPIOEXPORT + fi + echo $val > ${dir}/value + echo out > ${dir}/direction +} + +gpio_get() { + local gpio + local val + gpio=$(gpio_name2value $1) + dir=$(gpio_dir $gpio) + if [ ! -d ${dir} ]; then + echo $gpio > $GPIOEXPORT + fi + echo in > ${dir}/direction + cat ${dir}/value +} + +if [ -f "/usr/local/bin/soc-utils.sh" ]; then + source "/usr/local/bin/soc-utils.sh" +fi + +if [ -f "/usr/local/bin/board-utils.sh" ]; then + source "/usr/local/bin/board-utils.sh" +fi |