From 1e28040cec1c28b38cebb434998f3bed3f12c831 Mon Sep 17 00:00:00 2001 From: Pat Beirne Date: Mon, 6 Sep 2021 09:26:55 -0400 Subject: [PATCH] first commit Bash version. Requires `devmem2`. Run this script at boot time to restore the gpio pins on the 40 pin connector to the correct state. --- gpio_fixup.sh | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 gpio_fixup.sh diff --git a/gpio_fixup.sh b/gpio_fixup.sh new file mode 100644 index 0000000..1229dd6 --- /dev/null +++ b/gpio_fixup.sh @@ -0,0 +1,77 @@ +#!/bin/bash +# correct the GPIO settings to match the stated values in the i96 bus spec +# +# Pat Beirne 2021 +# +# this script applies only to the OrangePi-i96, booting from u-boot 2012.04.442 + +echo "OrangePi-i96 fixup GPIO pins" +echo "Version 1.0" + +# assert the i96 GPIO pins into GPIO mode +PORTC_GPIO_MASK=0 # no pins need changing +PORTA_GPIO_MASK=0x7e508000 # rda gpioA 15,20,22,25-30 (i96 gpio group) +PORTB_GPIO_MASK=0 # no pins need changing +PORTD_GPIO_MASK=0xc # rda gpioD 2,3 (i96 gpioi group) +PORTB_GPIO_MASK_NO_CTSRTS=0x0x300 # assert B 8,9 to reuse uart2_cts,rts + +# clear the i96 GPIO pins to special function mode +PORTC_SF_MASK=0xfffffe3f # clear bits C c6,7,8 (i96 uarts) +PORTA_SF_MASK=0xffff91a0 # clear bits A 0-4,6,9-11,13,14 (i96 i2c, spi, i2s) +PORTB_SF_MASK=0xfffffc3f # clear bits B 6-9 (i96 i2c, uart) +PORTD_SF_MASK=0xFFFFFFFF # nothing to change + +PORTC_IOMUX=0x11a09008 +PORTA_IOMUX=0x11a0900c +PORTB_IOMUX=0x11a09010 +PORTD_IOMUX=0x11a09014 + +DEVMEM='' +if [[ -x /usr/bin/devmem2 ]] +then + DEVMEM='/usr/bin/devmem2' +elif [[ -x /usr/local/bin/devmem2 ]] +then + DEVMEM='/usr/localbin/devmem2' +elif [[ -x /usr/local/bin/devmem2.py ]] +then + DEVMEM='/usr/local/bin/devmem2.py' +else + echo "devmem2 not installed, no fixup done" + return 1 +fi + +get_word() { + A=`$DEVMEM $1` + A=${A#*: } + echo $A +} + +# call or_bits with +# to set the bits (change to gpio) + +or_bits() { + echo "===" $1 "===" + READ_VALUE=`get_word $2` + WRITE_VALUE=$(($READ_VALUE | $3)) + $DEVMEM $2 w $WRITE_VALUE >> /dev/null + echo "read:" $READ_VALUE " write:" `printf '0x%x' $WRITE_VALUE` "readback: " `get_word $2` +} + +or_bits "PORTC set GPIO" $PORTC_IOMUX $PORTC_GPIO_MASK +or_bits "PORTA set GPIO" $PORTA_IOMUX $PORTA_GPIO_MASK +or_bits "PORTB set GPIO" $PORTB_IOMUX $PORTB_GPIO_MASK +or_bits "PORTD set GPIO" $PORTD_IOMUX $PORTD_GPIO_MASK + +and_bits() { + echo "===" $1 "===" + READ_VALUE=`get_word $2` + WRITE_VALUE=$(($READ_VALUE & $3)) + $DEVMEM $2 w $WRITE_VALUE >> /dev/null + echo "read:" $READ_VALUE " write:" `printf '0x%x' $WRITE_VALUE` "readback: " `get_word $2` +} + +and_bits "PORTC set SPECIAL FUNCTION" $PORTC_IOMUX $PORTC_SF_MASK +and_bits "PORTA set SPECIAL FUNCTION" $PORTA_IOMUX $PORTA_SF_MASK +and_bits "PORTB set SPECIAL FUNCTION" $PORTB_IOMUX $PORTB_SF_MASK +