Re: [linux-sunxi] Allwinner A40i USB-OTG host GPIO pin ID interrupt doesn't run handler

2024-01-11 Thread Сергей Мосолов
Hi Andre!

Thanks for sharing that info.

Actually the problem was that the GPIO interrupts not working, there just 
no irq 60 (PIO) generated for some reason at all, at any EXTI PIN.

After conforming to recommendation in A40i datasheet:
"(24).A 10MΩ resister is connected in parallel between X32KOUT and X32KIN, 
the resistor can create negative feedback in an inverter to ensure 
amplifier in linear amplifier region."
GPIO interrupts start working and, in particular, USB OTG PIN correctly 
detects Host or Client connected. I've used mentioned above:
https://github.com/wirenboard/linux/blob/dev/v5.10.y/drivers/phy/allwinner/phy-sun4i-usb.c
and during quick test it looks to me that USB OTG is fully worked now.

Bye, wish you all the best! Thanks again!

On Thursday, December 21, 2023 at 9:14:29 PM UTC+4 Andre Przywara wrote:

> On Thu, 21 Dec 2023 11:24:57 +0400
> Сергей Мосолов  wrote:
>
> (adding the proper sunxi mailing list for a wider audience)
>
> Hi Sergej,
>
> > I kindly ask for an advice about interrupt working with USB-OTG usb0 on
> > Allwinner A40i.
> > I need to make work usb0 as USB-OTG.
> > 
> > Looks like it's possible somehow:
> > 
> https://lore.kernel.org/linux-arm-kernel/daf5c543-a1d1-04d2...@sholland.org/T/#md5e9ce01feb201041467c136f09e80628f414557
>  
> 
> > 
> > I've used usb-phy driver with support of muxing ohci0(host) and
> > usb0(client) on usb0 pins, mentioned in the link above:
> > 
> https://github.com/wirenboard/linux/blob/dev/v5.10.y/drivers/phy/allwinner/phy-sun4i-usb.c
>
> some good checking and detective work!
>
> I am afraid the problem is somewhere deeper, though, in the way our sunxi
> PHY interacts with the OTG framework. I actually believe it's broken
> for every Allwinner system at the moment. Some months ago I spent some
> time in the code, and it looks like there is some race between the
> OHCI/EHCI controllers and the MUSB controller for the PHY. Both the MUSB
> and the HCI drivers claim the PHY and set their preferred mode (OTG and
> host, respectively), but if I remember correctly, the HCI driver always
> wins, regardless of the order. I can't remember exactly, but I think the
> problem was in sun4i_usb_phy_set_mode. You can try to insert debug
> messages there to see what's going on.
> The short summary at the moment seems to be: forcing the mode (peripheral
> or host) works, but the dynamic switching does not.
>
> You might want to try disabling the ehci0/ohci0 nodes, as a quick check, to
> see if that improves thing. I haven't tried that, though, as doing so is
> not correct and breaks U-Boot.
> Also keep an eye on the interrupt counter in /proc/interrupts, you should
> see some non-zero number if you connect and disconnect the cable a few
> times. But if the PHY is in host mode, then it won't activate the IRQ, I
> believe.
>
> Пока,
> Andre
>
> > In my case I have pin ID USB OTG = PI11.
> > 
> > Some excerpts from my dts file:
> > [quote]
> > / {
> > ...
> > /* ohci0 and ehci0 need to be included to work with USB OTG */
> > ohci0: usb@1c14400 {
> > compatible = "allwinner,sun8i-r40-ohci", "generic-ohci";
> > reg = <0x01c14400 0x100>;
> > interrupts = ;
> > clocks = <&ccu CLK_BUS_EHCI0>, <&ccu CLK_BUS_OHCI0>,
> > <&ccu CLK_USB_OHCI0>;
> > resets = <&ccu RST_BUS_EHCI0>, <&ccu RST_BUS_OHCI0>;
> > status = "okay";
> > };
> > 
> > ehci0: usb@1c14000 {
> > compatible = "allwinner,sun8i-r40-ehci", "generic-ehci";
> > reg = <0x01c14000 0x100>;
> > interrupts = ;
> > clocks = <&ccu CLK_BUS_EHCI0>, <&ccu CLK_BUS_OHCI0>;
> > resets = <&ccu RST_BUS_EHCI0>, <&ccu RST_BUS_OHCI0>;
> > status = "okay";
> > };
> > ...
> > }
> > ...
> > &usb_otg {
> > /* в dtsi defines of usb_otg as dr_mode = "peripheral"; */
> > status = "okay";
> > };
> > 
> > &usbphy {
> > dr_mode = "otg";
> > usb0_id_det-gpios = ;
> > usb0_vbus-supply = <®_vcc5v0>;
> > status = "okay";
> > };
> > [/quote]
> > 
> > USB-OTG works, but the problem is USB-OTG reads pin ID and choose client 
> or
> > host only on OS startup once.
> > 
> > After adding many printk and -DDEBUG for several subsystems it seems to 
> me
> > that the problem is PI11 GPIO irq just doesn't run it's handler requested
> > here:
> > 
> https://github.com/wirenboard/linux/blob/dev/v5.10.y/drivers/phy/allwinner/phy-sun4i-usb.c#L951
> > 
> > I observed EINT23 registration in pinctrl sunxi driver:
> > [quote]
> > [ 11.578919] sun4i-pinctrl 1c20800.pinctrl: 1c20800.pinctrl: request IRQ
> > for GPIO 267, return 23
> > [ 11.587657] devm_request_irq data->id_det_irq: 74, ret: 0
> > 
> > 74: 0 0 0 0 sunxi_pio_edge 23 Edge
> > usb0-id-det
> > 
> > # cat /proc/interrupts
> > CPU0 CPU1 CPU2 CPU3
> > 26: 0 0 0 0 GICv2 54 Level
> > timer@1c20c00
> > 27: 0 0 0 0 GICv2 29 Level
> > arch_timer
> > 28: 198416 45783 21540 10341 GICv2 30 Level
> > arch_timer
> > 31: 0 0 0 0 GICv2 152 Level
> > arm-pmu
> > 32: 0 0 0 0 GICv2 1

Re: [linux-sunxi] Allwinner A40i USB-OTG host GPIO pin ID interrupt doesn't run handler

2023-12-21 Thread Andre Przywara
On Thu, 21 Dec 2023 11:24:57 +0400
Сергей Мосолов  wrote:

(adding the proper sunxi mailing list for a wider audience)

Hi Sergej,

> I kindly ask for an advice about interrupt working with USB-OTG usb0 on
> Allwinner A40i.
> I need to make work usb0 as USB-OTG.
> 
> Looks like it's possible somehow:
> https://lore.kernel.org/linux-arm-kernel/daf5c543-a1d1-04d2-6486-6cc9cd72d...@sholland.org/T/#md5e9ce01feb201041467c136f09e80628f414557
> 
> I've used usb-phy driver with support of muxing ohci0(host) and
> usb0(client) on usb0 pins, mentioned in the link above:
> https://github.com/wirenboard/linux/blob/dev/v5.10.y/drivers/phy/allwinner/phy-sun4i-usb.c

some good checking and detective work!

I am afraid the problem is somewhere deeper, though, in the way our sunxi
PHY interacts with the OTG framework. I actually believe it's broken
for every Allwinner system at the moment. Some months ago I spent some
time in the code, and it looks like there is some race between the
OHCI/EHCI controllers and the MUSB controller for the PHY. Both the MUSB
and the HCI drivers claim the PHY and set their preferred mode (OTG and
host, respectively), but if I remember correctly, the HCI driver always
wins, regardless of the order. I can't remember exactly, but I think the
problem was in sun4i_usb_phy_set_mode. You can try to insert debug
messages there to see what's going on.
The short summary at the moment seems to be: forcing the mode (peripheral
or host) works, but the dynamic switching does not.

You might want to try disabling the ehci0/ohci0 nodes, as a quick check, to
see if that improves thing. I haven't tried that, though, as doing so is
not correct and breaks U-Boot.
Also keep an eye on the interrupt counter in /proc/interrupts, you should
see some non-zero number if you connect and disconnect the cable a few
times. But if the PHY is in host mode, then it won't activate the IRQ, I
believe.

Пока,
Andre

> In my case I have pin ID USB OTG = PI11.
> 
> Some excerpts from my dts file:
> [quote]
> / {
> ...
>/* ohci0 and ehci0 need to be included to work with USB OTG */
>ohci0: usb@1c14400 {
>   compatible = "allwinner,sun8i-r40-ohci", "generic-ohci";
>   reg = <0x01c14400 0x100>;
>   interrupts = ;
>   clocks = <&ccu CLK_BUS_EHCI0>, <&ccu CLK_BUS_OHCI0>,
>   <&ccu CLK_USB_OHCI0>;
>   resets = <&ccu RST_BUS_EHCI0>, <&ccu RST_BUS_OHCI0>;
>   status = "okay";
>};
> 
>ehci0: usb@1c14000 {
>   compatible = "allwinner,sun8i-r40-ehci", "generic-ehci";
>   reg = <0x01c14000 0x100>;
>   interrupts = ;
>   clocks = <&ccu CLK_BUS_EHCI0>, <&ccu CLK_BUS_OHCI0>;
>   resets = <&ccu RST_BUS_EHCI0>, <&ccu RST_BUS_OHCI0>;
>   status = "okay";
>};
> ...
> }
> ...
> &usb_otg {
> /* в dtsi defines of usb_otg as dr_mode = "peripheral"; */
>status = "okay";
> };
> 
> &usbphy {
>dr_mode = "otg";
>usb0_id_det-gpios = ;
>usb0_vbus-supply = <®_vcc5v0>;
>status = "okay";
> };
> [/quote]
> 
> USB-OTG works, but the problem is USB-OTG reads pin ID and choose client or
> host only on OS startup once.
> 
> After adding many printk and -DDEBUG for several subsystems it seems to me
> that the problem is PI11 GPIO irq just doesn't run it's handler requested
> here:
> https://github.com/wirenboard/linux/blob/dev/v5.10.y/drivers/phy/allwinner/phy-sun4i-usb.c#L951
> 
> I observed EINT23 registration in pinctrl sunxi driver:
> [quote]
> [   11.578919] sun4i-pinctrl 1c20800.pinctrl: 1c20800.pinctrl: request IRQ
> for GPIO 267, return 23
> [   11.587657] devm_request_irq data->id_det_irq: 74, ret: 0
> 
>  74:  0  0  0  0  sunxi_pio_edge  23 Edge
>usb0-id-det
> 
> # cat /proc/interrupts
>CPU0   CPU1   CPU2   CPU3
>  26:  0  0  0  0 GICv2  54 Level
> timer@1c20c00
>  27:  0  0  0  0 GICv2  29 Level
> arch_timer
>  28: 198416  45783  21540  10341 GICv2  30 Level
> arch_timer
>  31:  0  0  0  0 GICv2 152 Level
> arm-pmu
>  32:  0  0  0  0 GICv2 153 Level
> arm-pmu
>  33:  0  0  0  0 GICv2 154 Level
> arm-pmu
>  34:  0  0  0  0 GICv2 155 Level
> arm-pmu
>  35:  0  0  0  0 GICv2  59 Level
> 1c02000.dma-controller
>  37:  0  0  0  0 GICv2 102 Level
> gpmmu
>  38:  0  0  0  0 GICv2 104 Level
> ppmmu0
>  39:  0  0  0  0 GICv2 107 Level
> ppmmu1
>  40:175  0  0  0 GICv2 101 Level gp
>  41:128  0  0  0 GICv2 103 Level pp0
>  42:128  0  0  0 GICv2 106 Level pp1
>  43:  0  0  0  0 GICv2  71 Lev