8000 Add support for GPIO internal pull up/down configuration on RPi4 family. by tetsuya-uemura · Pull Request #746 · freebsd/freebsd-src · GitHub
[go: up one dir, main page]

Skip to content

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

Closed
wants to merge 5 commits into from
Closed

Conversation

tetsuya-uemura
Copy link
Contributor

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

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
10000
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 */
Copy link
Contributor

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.

Copy link
Member

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")
Copy link
Member

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");

Copy link
Contributor
@mhorne mhorne left a 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)
Copy link
Contributor

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).

Comment on lines 326 to 328
#define __LOWEST_SET_BIT(__mask) ((((__mask) - 1) & (__mask)) ^ (__mask))
#define __SHIFTIN(__x, __mask) ((__x) * __LOWEST_SET_BIT(__mask))
reg |= __SHIFTIN(state, mask);
Copy link
Contributor

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));
Copy link
Contributor

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?

Copy link
Contributor
@mhorne mhorne left a 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;
Copy link
Contributor

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.

Comment on lines 313 to 323
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;
}
Copy link
Contributor

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:
...

mhorne pushed a commit to mhorne/freebsd that referenced this pull request May 29, 2023
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
@mhorne
Copy link
Contributor
mhorne commented May 29, 2023

I have merged the change, so this PR can be closed. It does not seem like I am able to do it myself.

@tetsuya-uemura
Copy link
Contributor Author

Thanks! Changes had been merged and this request should be closed.

hardenedbsd-services pushed a commit to HardenedBSD/hardenedBSD that referenced this pull request May 30, 2023
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
mhorne pushed a commit to mhorne/freebsd that referenced this pull request Jun 9, 2023
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)
freebsd-git pushed a commit that referenced this pull request Jun 9, 2023
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)
@emaste emaste added the merged Closed commit that's been merged label Jun 12, 2023
bsdjhb pushed a commit to bsdjhb/cheribsd that referenced this pull request Aug 11, 2023
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged Closed commit that's been merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0