-
Notifications
You must be signed in to change notification settings - Fork 3k
Add support for GPIO internal pull up/down configuration on RPi4 family. #746
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reference: NetBSD/src@bb88cfa
BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUDCLK(bank), BCM_GPIO_MASK(pin)); | ||
BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUD(0), 0); | ||
BCM_GPIO_WRITE(sc, BCM_GPIO_GPPUDCLK(bank), 0); | ||
if (sc->sc_is2711 == false) { /* BCM2835 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'sc_is2711' is boolean so no need to compare it to false.
if (!sc->sc_is2711) {} is more readable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or put the true case first
if (sc->sc_is2711) {
u_int mask = BCM2711_GPIO_MASK(pin);
u_int regid = BCM2711_GPIO_REGID(pin);
uint32_t reg;
...
} else { /* BCM2835 */
...
}
/* Find our node. */ | ||
gpio = ofw_bus_get_node(sc->sc_dev); | ||
if (!OF_hasprop(gpio, "gpio-controller")) | ||
/* Node is not a GPIO controller. */ | ||
goto fail; | ||
/* Guess I'm BCM2711 or not. */ | ||
sc->sc_is2711 = ofw_bus_node_is_compatible(gpio, "brcm,bcm2711-gpio") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dropping the ? true : false
is clearer IMO
sc->sc_is2711 = ofw_bus_node_is_compatible(gpio, "brcm,bcm2711-gpio");
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, almost there.
#define BCM2835_GPIO_GPPUDCLK(_bank) (0x98 + _bank * 4) /* Pin Pull up clock */ | ||
|
||
#define BCM2711_GPIO_GPPUD(x) (0x0e4 + (x) * sizeof(uint32_t)) /* Pin Pull up/down */ | ||
#define BCM2711_GPIO_MASK(n) (0x3 << ((n) % 16)*2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you add:
#define BCM2711_GPIO_PIN_SHIFT(n) (((n) % 16) * 2)
Then you can use it as part of this definition, and below (see my other comment).
#define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask)) | ||
#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask)) | ||
reg |= __SHIFTIN(state, mask); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't love these defines, they over-complicate what you are trying to do, which is shift the state
value the appropriate number of bits for pin
. You have already selected the correct register. It could just be:
reg |= state << BCM2711_GPIO_PIN_SHIFT(pin);
|
||
bank = BCM_GPIO_BANK(pin); | ||
BCM_GPIO_WRITE(sc, BCM2835_GPIO_GPPUD(0), state); | ||
BCM_GPIO_WRITE(sc, BCM2835_GPIO_GPPUDCLK(bank), BCM_GPIO_MASK(pin)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should BCM_GPIO_MASK()
be named BCM2835_GPIO_MASK()
now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, two more small things to fix that I should have noticed before. After that, I will be able to merge this.
@@ -109,6 +115,8 @@ struct bcm_gpio_softc { | |||
device_t sc_busdev; | |||
struct mtx sc_mtx; | |||
struct resource * sc_res[BCM_GPIO_IRQS + 1]; | |||
bool sc_is2711; | |||
u_int sc_maxpins; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sc_res
and sc_bst
/sc_bsh
are functionally related, so we shouldn't insert the new struct members between them.
I would suggest placing the new ones right above sc_gpio_npins
.
switch (state) { | ||
case BCM2835_PUD_OFF: | ||
state = BCM2711_PUD_OFF; | ||
break; | ||
case BCM2835_PUD_DOWN: | ||
state = BCM2711_PUD_DOWN; | ||
break; | ||
case BCM2835_PUD_UP: | ||
state = BCM2711_PUD_UP; | ||
break; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have an unusual style for switch
statements in FreeBSD, in which the case
lines should not be indented. Meaning:
switch (state) {
case BCM2835_PUD_OFF:
state = BCM2711_PUD_OFF;
break;
case BCM2835_PUD_DOWN:
...
Style(9) conformance.
Add support for GPIO internal pull up/down configuration on RPi4 family. BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reviewed by: mhorne Obtained from: NetBSD bb88cfa64ad8 Pull Request: freebsd#746
I have merged the change, so this PR can be closed. It does not seem like I am able to do it myself. |
Thanks! Changes had been merged and this request should be closed. |
Add support for GPIO internal pull up/down configuration on RPi4 family. BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reviewed by: mhorne Obtained from: NetBSD bb88cfa64ad8 Pull Request: freebsd#746
Add support for GPIO internal pull up/down configuration on RPi4 family. BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reviewed by: mhorne Obtained from: NetBSD bb88cfa64ad8 Pull Request: freebsd#746 (cherry picked from commit 9d35469)
Add support for GPIO internal pull up/down configuration on RPi4 family. BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reviewed by: mhorne Obtained from: NetBSD bb88cfa64ad8 Pull Request: #746 (cherry picked from commit 9d35469)
Add support for GPIO internal pull up/down configuration on RPi4 family. BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial. This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status. PR: 256372 Reviewed by: mhorne Obtained from: NetBSD bb88cfa64ad8 Pull Request: freebsd/freebsd-src#746
BCM2711 SoC on 4th generation Raspberry Pi changed the way to configure its GPIO pins' internal pull up/down resistors. NetBSD already have support for this change, and port it to FreeBSD is trivial.
This patch, based on the NetBSD commit adds the appropriate method for BCM2711 and now we can properly configure the GPIO pins' pull status.
PR: 256372
Reference: NetBSD/src@bb88cfa