Re: rk_gpio: high latency switching between input / output

From: Emmanuel Vadot <manu_at_bidouilliste.com>
Date: Thu, 23 Sep 2021 07:18:21 UTC
 Hi Andriy,

On Sat, 18 Sep 2021 11:34:05 +0300
Andriy Gapon <avg@FreeBSD.org> wrote:

> 
> I noticed that rk_gpio has a rather high overhead when switching between input 
> and output modes on a pin.  This is typically required for software I2C (bit 
> banging) and 1-wire protocols.  The overhead is so bad that 1-wire in particular 
> is very unreliable, barely working.
> 
> It seems that a lot of that overhead in rk_gpio_pin_setflags() comes from 
> FDT_PINCTRL_IS_GPIO() and FDT_PINCTRL_SET_FLAGS().  It's easy to shave off the 
> latter as the call is not necessary when only GPIO_PIN_INPUT and GPIO_PIN_OUTPUT 
> flags are changing.
> 
> Not sure what can be done about FDT_PINCTRL_IS_GPIO().
> On the one hand it's nice to have that sanity check, on the other hand it adds 
> the overhead.  Also, it might be racy as there is no common lock for gpio and 
> pinctrl.
> 
> Anyway, perhaps the overhead could be reduced if rk_pinctrl cached pin 
> configurations instead of retrieving and re-parsing them every time like 
> rk_pinctrl_is_gpio_locked() does.
> 
> -- 
> Andriy Gapon
> 

 I've made some tests on my rock64 board.
 The program I used [1] is, I think, the worst possible scenario that
can happen as it :
 - Set the pin to output
 - Toggle the pin
 - Set the pin to input
 - Read the pin
 - Set the pin to output

 With it I get a ~30khz signal produced on the pin.

 I did some change to rk_gpio [2], those changes are :

 - Cache the pin info (gpio or not) and flags (pullup/pulldown)
 - Remove useless reads for the pin direction and value when we're
setting or toggling the pin (so output mode only).

 With this and the same program I manage to have a ~50khz signal output
on the pin.
 I'm not sure I can get higher result for now.

 All test where done on main (c96da199458) with GENERIC kernel.
 Obviously you will have better result for kernel operation but I
choose to do userland to have the worst case possible.

 Let me know if this helps you in your case, otherwise I'll hookup some
1wire temp sensors and try to make them work while the scope is still
plugged on the board.

 Cheers,

 1: https://people.freebsd.org/~manu/rk_gpio/gpio_test.c
 2: https://people.freebsd.org/~manu/rk_gpio/0001-arm64-rockchip-rk_gpio-Improve-mode-switching.patch
-- 
Emmanuel Vadot <manu@bidouilliste.com> <manu@freebsd.org>