gpio_fixup.sh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. # correct the GPIO settings to match the stated values in the i96 bus spec
  3. #
  4. # Pat Beirne <patb@pbeirne> 2021
  5. #
  6. # this script applies only to the OrangePi-i96, booting from u-boot 2012.04.442
  7. echo "OrangePi-i96 fixup GPIO pins"
  8. echo "Version 1.0"
  9. # assert the i96 GPIO pins into GPIO mode
  10. PORTC_GPIO_MASK=0 # no pins need changing
  11. PORTA_GPIO_MASK=0x7e508000 # rda gpioA 15,20,22,25-30 (i96 gpio group)
  12. PORTB_GPIO_MASK=0 # no pins need changing
  13. PORTD_GPIO_MASK=0xc # rda gpioD 2,3 (i96 gpioi group)
  14. PORTB_GPIO_MASK_NO_CTSRTS=0x0x300 # assert B 8,9 to reuse uart2_cts,rts
  15. # clear the i96 GPIO pins to special function mode
  16. PORTC_SF_MASK=0xfffffe3f # clear bits C c6,7,8 (i96 uarts)
  17. PORTA_SF_MASK=0xffff91a0 # clear bits A 0-4,6,9-11,13,14 (i96 i2c, spi, i2s)
  18. PORTB_SF_MASK=0xfffffc3f # clear bits B 6-9 (i96 i2c, uart)
  19. PORTD_SF_MASK=0xFFFFFFFF # nothing to change
  20. PORTC_IOMUX=0x11a09008
  21. PORTA_IOMUX=0x11a0900c
  22. PORTB_IOMUX=0x11a09010
  23. PORTD_IOMUX=0x11a09014
  24. DEVMEM=''
  25. if [[ -x /usr/bin/devmem2 ]]
  26. then
  27. DEVMEM='/usr/bin/devmem2'
  28. elif [[ -x /usr/local/bin/devmem2 ]]
  29. then
  30. DEVMEM='/usr/localbin/devmem2'
  31. elif [[ -x /usr/local/bin/devmem2.py ]]
  32. then
  33. DEVMEM='/usr/local/bin/devmem2.py'
  34. else
  35. echo "devmem2 not installed, no fixup done"
  36. return 1
  37. fi
  38. get_word() {
  39. A=`$DEVMEM $1`
  40. A=${A#*: }
  41. echo $A
  42. }
  43. # call or_bits with <announcement> <iomux_address> <or_bitmask>
  44. # to set the bits (change to gpio)
  45. or_bits() {
  46. echo "===" $1 "==="
  47. READ_VALUE=`get_word $2`
  48. WRITE_VALUE=$(($READ_VALUE | $3))
  49. $DEVMEM $2 w $WRITE_VALUE >> /dev/null
  50. echo "read:" $READ_VALUE " write:" `printf '0x%x' $WRITE_VALUE` "readback: " `get_word $2`
  51. }
  52. or_bits "PORTC set GPIO" $PORTC_IOMUX $PORTC_GPIO_MASK
  53. or_bits "PORTA set GPIO" $PORTA_IOMUX $PORTA_GPIO_MASK
  54. or_bits "PORTB set GPIO" $PORTB_IOMUX $PORTB_GPIO_MASK
  55. or_bits "PORTD set GPIO" $PORTD_IOMUX $PORTD_GPIO_MASK
  56. and_bits() {
  57. echo "===" $1 "==="
  58. READ_VALUE=`get_word $2`
  59. WRITE_VALUE=$(($READ_VALUE & $3))
  60. $DEVMEM $2 w $WRITE_VALUE >> /dev/null
  61. echo "read:" $READ_VALUE " write:" `printf '0x%x' $WRITE_VALUE` "readback: " `get_word $2`
  62. }
  63. and_bits "PORTC set SPECIAL FUNCTION" $PORTC_IOMUX $PORTC_SF_MASK
  64. and_bits "PORTA set SPECIAL FUNCTION" $PORTA_IOMUX $PORTA_SF_MASK
  65. and_bits "PORTB set SPECIAL FUNCTION" $PORTB_IOMUX $PORTB_SF_MASK