How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build



  • This is a problem arising only because we don't yet have the exact sources for the Omega2 firmware :-(

    A number of pins of the MT7688, among them GPIO18 and 19, can be used either as digital I/Os (GPIOs, PWM etc.) or as "analog" pins for connecting up to 5 Ethernet ports.

    When using the original Omega2 FW, it comes up with these pins in GPIO mode.

    However, with a LEDE image built from https://github.com/OnionIoT/source, these pins are switched to Ethernet port mode at some point, meaning GPIO18 and 19 don't work.

    The AGPIO vs. normal GPIO mode can be switched by bits 20..17 in the AGPIO_CFG register (address 0x1000003C) of the MT7688, see page 59 of the datasheet.

    Using devmem command line tool, it is possible to switch AGPIO mode on an off (be careful, writing directly to physical memory is dangerous!!):

    Switch to normal GPIO mode:

    devmem 0x1000003C 32 0x00FE01FF
    

    Switch to AGPIO (Ethernet port) mode:

    devmem 0x1000003C 32 0x00E001FF
    

    This works, but is super ugly. I'd like to understand which part of the FW is responsible for switching AGPIO on or off, and how to correctly configure a LEDE build such that AGPIOs are off (so GPIO18/19 work normally). Any Ideas?



  • @luz This is excellent work you did there. :fishing_pole_and_fish: :thumbsup:

    If you keep the LEDE firmware as is (until the right solution is found), could these commands be run at the start of the boot script instead? I mean, is there a danger to equipment connected to these GPIOs if the pins are in Ethernet Mode during a very short period of time?



  • @luz What DTS-file are you using? GPIO18 and GPIO19 work just fine via both the /sys/class/gpio-interface and fast-gpio with my DTS.



  • @WereCatf said in How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build:

    @luz What DTS-file are you using?

    The OMEGA2.dts from the onion's LEDE fork on github, hoping they actually use exactly that file.

    GPIO18 and GPIO19 work just fine via both the /sys/class/gpio-interface and fast-gpio with my DTS.

    I assume you checked the output levels in hardware. Just asking - because even if the AGPIO switch is set the wrong way, you can claim those gpios via /sys/class or fast-gpio fine, just the signals don't get through to the pins.

    As AGPIO is related to the built-in ethernet switch, my next guess is that it is somehow related to network and switch config. I tried to enable/disable switch ports using swconfig, but that makes no difference. AGPIO state does not change.



  • @fossette said in How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build:

    If you keep the LEDE firmware as is (until the right solution is found), could these commands be run at the start of the boot script instead? I mean, is there a danger to equipment connected to these GPIOs if the pins are in Ethernet Mode during a very short period of time?

    That's exactly how I'm doing it right now - as a workaround ;-)

    For my specific application it is no problem, neither for connected equipment nor the pin drivers when the pins are in ethernet mode for a short period.

    But it's a uuuuugly solution, so I want to understand why Onion's build is behaving different despite the same dts and fix it the right way eventually!

    One curious detail - omega2-ctrl (of which we sadly don't have the source code) does have a "ephy" mux option that I can't relate to any of the actual gpio muxes of the MT7688. There is a "ephy" mux in the 7620, but not the 76x8. The available muxes are set up in linux-4.4.39/arch/mips/ralink/mt7620.c, but nothing in that file has to do with the AGPIO switch either.



  • @luz said in How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build:

    I assume you checked the output levels in hardware. Just asking - because even if the AGPIO switch is set the wrong way, you can claim those gpios via /sys/class or fast-gpio fine, just the signals don't get through to the pins.

    I'm sorry, I didn't actually check that. I just simply checked whether exporting the pin works in /sys/class/gpio and assumed it meant it was working. You're entirely correct, I should have checked it properly. Nevermind, though, I'm gonna push a fix for it on my github-repo in a minute!



  • There, fix pushed, push fixed.


  • administrators

    @luz sorry for the delayed response! GPIOs 18 and 19 are set to the PWM function by default. The Onion DTS file changes them to GPIO functionality.

    Also, we've open-sourced omega2-ctrl, check it out here: https://github.com/OnionIoT/omega2-ctrl



  • @WereCatf The fix just makes "pwm0" group GPIO by default (which I agree should be the case, too). But this does not help with AGPIO mode set.

    @Lazar-Demin yes, I realized that GPIO18/19 were in PWM mode by default. I fixed this in my experimental DTS already.

    However that's not what I was talking about. There is a global switch for all EPHY port1..4 pins to make them either digital I/O functions (GPIOs, PWM, etc.) or ethernet differential "analog" TX/RX pins.

    The bits called EPHY_GPIO_AIO_EN, bit 20..17 in AGPIO_CFG (0x1000003C) control that mode, as I explained in the original post. After a hardware reset, these bits are set to 1, so the MT7688 starts up in digital (GPIO) mode for these pins.

    My problem is that I can't figure out why my LEDE build at some point switches these pins to Ethernet.

    As it's not in the device tree, it must be some network related driver/package doing this, only I can't find it. If I had the omega2 .config, I would diff it with mine and probably be able to spot the difference.



  • @luz I don't understand the problem. GPIO18 and GPIO19 work fine with my latest change, I can see the pin-state change when I connect the pin to HIGH or LOW.



  • @Lazar-Demin said in How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build:

    Also, we've open-sourced omega2-ctrl, check it out here: https://github.com/OnionIoT/omega2-ctrl

    Great! Thanks a lot!

    This allowed me to figure out what the "ephy" setting that confused me actually is.

    It controls EPHY_LED0_N_JTDO pin to be either GPIO43 or the Ethernet LED for Port 0 (P0_LED_AN_MODE, Bits 3..2 in GPIO2_MODE). Thus, it should probably be called "eled". On the other hand, as that pin seems not connected in the Omega2, maybe it should be removed from omega2-ctrl, together with "wled" which seems not connected, too.



  • @WereCatf said in How to switch AGPIO (GPIO 18/19) - Omega2 FW vs LEDE build:

    @luz I don't understand the problem. GPIO18 and GPIO19 work fine with my latest change, I can see the pin-state change when I connect the pin to HIGH or LOW.

    It's definitely not a .dts thing. If I manually disable the AGPIO (see devmem command in the original post), GPIO18/19 work fine.

    But some software piece in my LEDE build does switch this entire pin group (all 16 bins related to ethernet switch port 1..4) into AGPIO/ethernet mode.

    In the meantime, I have a suspect: it could be the mere presence of the "swconfig" package. This is a utility to control built-in switch hardware, but it also pulls in a kernel module and maybe this module enables the switch hardware. I'm now building a new image without swconfig, let's see if that helps...

    Could you please check in your .config, do you have "swconfig" package selected?



  • @luz I have CONFIG_PACKAGE_swconfig=y but CONFIG_PACKAGE_kmod-swconfig=m -- perhaps that's the difference? You could also try if just disabling the switch in the DTS-file works, e.g. add the following to the DTS:

    &esw {
      status = "disabled";
    };
    


  • @WereCatf disabling the switch in the device tree kills ethernet functionality entirely :-(

    So this is no option for me - I need the ethernet port.

    Maybe its using the ethernet port that causes AGPIO to get enabled (and GPIO18/19 disabled)?

    Do you have ethernet in use on your omega2?

    BTW: CONFIG_PACKAGE_swconfig=y and CONFIG_PACKAGE_kmod-swconfig=m didn't help either.



  • @luz No, I don't have any of the various docks, including the Ethernet-dock.



  • @WereCatf I mean, is the eth0 interface enabled (in software)? It does not matter whether you have a ethernet dock or not.
    But if it isn't enabled in your configuration, then that could be the reason why GPIO18/19 work for you, and don't work in my case.



  • @luz It is enabled and all, yes. At least I haven't done anything to disable it.


Log in to reply
 

Looks like your connection to Onion Community was lost, please wait while we try to reconnect.