How GPIO is mapped in memory?
Asked Answered
E

2

6

I am recently browsing GPIO driver for pi2, I found user space pi2 GPIO lib (like RPi.GPIO 0.5.11 of python) use /dev/mem for BCM2708 (begins at 0x20000000,and GPIO begins at 0x200000 relatively) to mmap a user space memory region in order to handler GPIO. But I found drivers/gpio in linux source tree is designed to be handled by /sys/class/gpio/*. I found nothing like I/O ports mapping like request_io_region and __io_remap. My question is How GPIO for BCM2708 mapped in memory ? Is there another driver? And can I handle GPIO just by R&W to /sys/class/gpio/*?

Elevated answered 25/7, 2015 at 3:26 Comment(0)
B
5

I found nothing like I/O ports mapping like request_io_region and __io_remap.

ARM does not have an I/O port space. All peripheral registers are assigned to addresses in memory space.

How GPIO for BCM2708 mapped in memory ?

GPIOs are typically implemented as a peripheral of control registers, and the GPIOs in the BCM2835 of the RPi follows this convention. This set of control register may have a different name; for example Atmel refers to these registers as the Parallel I/O (PIO) peripheral.

Each GPIO (or more accurately each pin) will be represented by one or more bits in each control register function. The control register functions include pin assignment (aka multiplexing), set output to high, set output to low, read pin level, and level and edge detection control.

IOW there is no single bit that can be read and written that corresponds to a GPIO. For a GPIO there would be a bit in a specific register to fetch the input level. There's a bit in another register to set that GPIO output high, and bit in another register to set that GPIO output low.

Is there another driver?

Yes. The pinctrl (pin control) driver is a lower-layer (i.e. closer to the HW) than GPIO. It's the pinctrl layer that handles pin multiplexing (i.e. whether a pin is used for a peripheral function or as a GPIO).
The pinctrl driver for the SoC (e.g. drivers/pinctrl/pinctrl-bcm2835.c) is where you would find devm_ioremap_resources() (which in turn calls devm_request_mem_region() and devm_ioremap()) for the GPIO register block.

And can I handle GPIO just by R&W to /sys/class/gpio/*?

Yes. the sysfs interface is provided for accessing those pins not assigned to peripherals.

ADDENDUM
The sysfs GPIO interface has limited capabilities.
Apparently there are userspace libraries to access additional pin attributes (e.g. enabling pull-up or pull-down resistor) that are normally in the domain of the pinctrl driver. Typically such libraries access the PIO hardware registers directly through the /dev/mem psuedo-file. Be cognizant that such techniques are not secure and could interfere with other device drivers.

Burn answered 25/7, 2015 at 10:15 Comment(5)
I found this line github.com/raspberrypi/linux/blob/rpi-4.0.y/arch/arm/….Elevated
I guess this is where '/dev/mem' map address comes from. As far as I know, in this code it declares a platform device to abtain the resources. Does pinctrl play as controller to bcm2708 for bcm2835 , and GPIO driver is the upper layer exported to outside?Elevated
There seems to a controversy as to whether the RPi drivers should be referenced to the BCM2708 or BDM2835/6. Looking at a BCM2835 datasheet, the GPIO registers start at 0x7E200000. You could take a look at /proc/iomem on your RPi to see what peripherals are where in phys memory (as declared by drivers). Try inspecting the boot or system log (i.e. dmesg command) for pinctrl and GPIO driver messages. Otherwise I don't understand "Does pinctrl play as controller...". The GPIO driver is accessible by other device device drivers (e.g. an interrupt for a SPI slave) as well as sysfs.Burn
For more generic details refer to the Linux documentation for pinctrl and gpio.Burn
I know this question is pretty old, but I found this video very helpful youtube.com/watch?v=aT5XMOrid7YOperation
Z
0

This response may not be "on all fours" since it merely provides a GPIO base address for Raspberry Pi 2 vs a how.

Notwithstanding, the Raspbian OS on Raspberry Pi 2 provides a base address at 0x3f20,0000.

$ dmesg -H 
[  +0.000749] gpiomem-bcm2835 3f200000.gpiomem: Initialised: Registers at 0x3f200000

A crude ARMv7 Assembly example using the above base address, via an mmmap call, to blink the ACT LED on the Raspberry Pi 2 is indicated in the link below.

https://github.com/InfinitelyManic/Raspberry-Pi-2

Zeigler answered 11/4, 2016 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.