Skip to contentSkip to conversation

bcm2835_gpio: Implement GPIOF_FUNC

PATCH New Alexander Graf 4 participants 12 messages Aug 11, 2016 – Sep 7, 2016

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bcm2835_gpio: Implement GPIOF_FUNC

So far we could only tell the gpio framework that a GPIO was mapped as input or output, not as alternative function.

This patch adds support for determining whether a function is mapped as alternative.

---
 arch/arm/mach-bcm283x/include/mach/gpio.h |  2 ++
 drivers/gpio/bcm2835_gpio.c               | 30 +++++++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
arch/arm/mach-bcm283x/include/mach/gpio.h +2 -0
index e6e5d16..b2df75a 100644
@@ -66,4 +66,6 @@ struct bcm2835_gpio_platdata {
 	unsigned long base;
 };
 
+int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio);
+
 #endif /* _BCM2835_GPIO_H_ */
drivers/gpio/bcm2835_gpio.c +17 -13
index fbc641d..8b88d79 100644
@@ -44,15 +44,6 @@ static int bcm2835_gpio_direction_output(struct udevice *dev, unsigned gpio,
 	return 0;
 }
 
-static bool bcm2835_gpio_is_output(const struct bcm2835_gpios *gpios, int gpio)
-{
-	u32 val;
-
-	val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
-	val &= BCM2835_GPIO_FSEL_MASK << BCM2835_GPIO_FSEL_SHIFT(gpio);
-	return val ? true : false;
-}
-
 static int bcm2835_get_value(const struct bcm2835_gpios *gpios, unsigned gpio)
 {
 	unsigned val;
@@ -81,15 +72,28 @@ static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
 	return 0;
 }
 
-static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
+int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
 {
 	struct bcm2835_gpios *gpios = dev_get_priv(dev);
+	u32 val;
+
+	val = readl(&gpios->reg->gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+
+	return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
+}
+
+static int bcm2835_gpio_get_function(struct udevice *dev, unsigned offset)
+{
+	int funcid = bcm2835_gpio_get_func_id(dev, offset);
 
-	/* GPIOF_FUNC is not implemented yet */
-	if (bcm2835_gpio_is_output(gpios, offset))
+	switch (funcid) {
+	case BCM2835_GPIO_OUTPUT:
 		return GPIOF_OUTPUT;
-	else
+	case BCM2835_GPIO_INPUT:
 		return GPIOF_INPUT;
+	default:
+		return GPIOF_FUNC;
+	}
 }
 
 
Signature · 1 line
1.8.5.6
#1 lore raw

PATCH 2/2 serial: bcm283x_mu: Detect disabled serial device

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

---

v2 -> v3:

- Disable and detect pinmux in board file

---
 board/raspberrypi/rpi/rpi.c | 29 +++++++++++++++++++++++++++++
 configs/rpi_3_32b_defconfig |  1 +
 configs/rpi_3_defconfig     |  1 +
 include/configs/rpi.h       |  1 +
 4 files changed, 32 insertions(+)
board/raspberrypi/rpi/rpi.c +29 -0
index 4c8253d..20b0d1b 100644
@@ -453,6 +453,35 @@ int board_init(void)
 	return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 }
 
+static bool rpi_is_serial_active(void)
+{
+#ifndef CONFIG_PL01X_SERIAL
+	int serial_gpio = 15;
+	struct udevice *dev;
+
+	/*
+	 * The RPi3 disables the mini uart by default. The easiest way to find
+	 * out whether it is available is to check if the pin is muxed.
+	 */
+	if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
+		return true;
+
+	if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
+		return false;
+#endif
+
+	return true;
+}
+
+int board_late_init(void)
+{
+	/* Disable mini-UART I/O if it's not pinmuxed to our pins */
+	if (!rpi_is_serial_active())
+		gd->cur_serial_dev = NULL;
+
+	return 0;
+}
+
 int board_mmc_init(bd_t *bis)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
configs/rpi_3_32b_defconfig +1 -0
index 922e01b..4c2f106 100644
@@ -20,3 +20,4 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
configs/rpi_3_defconfig +1 -0
index bff92df..288214c 100644
@@ -19,3 +19,4 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
include/configs/rpi.h +1 -0
index b5543f4..e3b890a 100644
@@ -22,6 +22,7 @@
 
 /* Architecture, CPU, etc.*/
 #define CONFIG_ARCH_CPU_INIT
+#define CONFIG_BOARD_LATE_INIT
 
 /* Use SoC timer for AArch32, but architected timer for AArch64 */
 #ifndef CONFIG_ARM64
Signature · 1 line
1.8.5.6
#9 lore raw

PATCH 1/2 bcm2835_gpio: Implement GPIOF_FUNC

In reply to Alexander Graf
Quoted 11 lines · Alexander Graf

So far we could only tell the gpio framework that a GPIO was mapped as input or output, not as alternative function.

This patch adds support for determining whether a function is mapped as alternative.

---
 arch/arm/mach-bcm283x/include/mach/gpio.h |  2 ++
 drivers/gpio/bcm2835_gpio.c               | 30 +++++++++++++++++-------------
 2 files changed, 19 insertions(+), 13 deletions(-)
#2 lore raw

PATCH 2/2 serial: bcm283x_mu: Detect disabled serial device

In reply to Alexander Graf

Hi Alex,

Quoted 41 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

---

v2 -> v3:

- Disable and detect pinmux in board file

---
 board/raspberrypi/rpi/rpi.c | 29 +++++++++++++++++++++++++++++
 configs/rpi_3_32b_defconfig |  1 +
 configs/rpi_3_defconfig     |  1 +
 include/configs/rpi.h       |  1 +
 4 files changed, 32 insertions(+)
board/raspberrypi/rpi/rpi.c +5 -0
index 4c8253d..20b0d1b 100644
@@ -453,6 +453,35 @@ int board_init(void)
        return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 }
 
+static bool rpi_is_serial_active(void)
+{
+#ifndef CONFIG_PL01X_SERIAL
+       int serial_gpio = 15;
+       struct udevice *dev;

board/raspberrypi/rpi/rpi.c int board_init(void) · line 470

Quoted 10 lines
board/raspberrypi/rpi/rpi.c +10 -0
+
+       /*
+        * The RPi3 disables the mini uart by default. The easiest way to find
+        * out whether it is available is to check if the pin is muxed.
+        */
+       if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
+               return true;
+
+       if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
+               return false;

Do you mean gpio_get_function()?

Quoted 48 lines
board/raspberrypi/rpi/rpi.c +14 -0
+#endif
+
+       return true;
+}
+
+int board_late_init(void)
+{
+       /* Disable mini-UART I/O if it's not pinmuxed to our pins */
+       if (!rpi_is_serial_active())
+               gd->cur_serial_dev = NULL;
+
+       return 0;
+}
+
 int board_mmc_init(bd_t *bis)
 {
        ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
configs/rpi_3_32b_defconfig +1 -0
index 922e01b..4c2f106 100644
@@ -20,3 +20,4 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
configs/rpi_3_defconfig +1 -0
index bff92df..288214c 100644
@@ -19,3 +19,4 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
include/configs/rpi.h +1 -1
index b5543f4..e3b890a 100644
@@ -22,6 +22,7 @@
 
 /* Architecture, CPU, etc.*/
 #define CONFIG_ARCH_CPU_INIT
+#define CONFIG_BOARD_LATE_INIT
 
 /* Use SoC timer for AArch32, but architected timer for AArch64 */
 #ifndef CONFIG_ARM64
--
Quoted 2 lines

1.8.5.6

Regards,
Simon

#3 lore raw

PATCH 2/2 serial: bcm283x_mu: Detect disabled serial device

Quoted 52 lines

Am 12.08.2016 um 00:38 schrieb Simon Glass <[email protected]>:

Hi Alex,

Quoted 46 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

---

v2 -> v3:

- Disable and detect pinmux in board file
---

board/raspberrypi/rpi/rpi.c | 29 +++++++++++++++++++++++++++++
configs/rpi_3_32b_defconfig |  1 +
configs/rpi_3_defconfig     |  1 +
include/configs/rpi.h       |  1 +
4 files changed, 32 insertions(+)
board/raspberrypi/rpi/rpi.c +9 -0
index 4c8253d..20b0d1b 100644
@@ -453,6 +453,35 @@ int board_init(void)
       return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 }
 
+static bool rpi_is_serial_active(void)
+{
+#ifndef CONFIG_PL01X_SERIAL
+       int serial_gpio = 15;
+       struct udevice *dev;
+
+       /*
+        * The RPi3 disables the mini uart by default. The easiest way to find
+        * out whether it is available is to check if the pin is muxed.
Quoted 8 lines

board/raspberrypi/rpi/rpi.c int board_init(void) · line 470

Quoted 6 lines
board/raspberrypi/rpi/rpi.c +6 -0
+        */
+       if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
+               return true;
+
+       if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
+               return false;

Do you mean gpio_get_function()?

That only tells me whether it's in/out/other, but I need to know whether the pin is configured to exactly function 5.

Alex

#10 lore raw

PATCH 1/2 bcm2835_gpio: Implement GPIOF_FUNC

In reply to Alexander Graf
Quoted 5 lines · Alexander Graf

So far we could only tell the gpio framework that a GPIO was mapped as input or output, not as alternative function.

This patch adds support for determining whether a function is mapped as alternative.

The series,

In patch 2, I wonder if there shouldn't be a more "architectural" way of disabling the UART device; it seem like the method currently used could easily be broken by some change in the core code. I see you're discussing that part with Simon.

#4 lore raw

PATCH v4 2/2 serial: bcm283x_mu: Detect disabled serial device

In reply to Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

---

v2 -> v3:

- Disable and detect pinmux in board file

v3 -> v4:

- Detect in early code and disable by failing serial probe

---
 board/raspberrypi/rpi/rpi.c                  | 34 +++++++++++++++++++++++++++-
 configs/rpi_3_32b_defconfig                  |  2 ++
 configs/rpi_3_defconfig                      |  2 ++
 drivers/gpio/bcm2835_gpio.c                  |  1 +
 drivers/serial/serial_bcm283x_mu.c           |  3 +++
 include/configs/rpi.h                        |  1 +
 include/dm/platform_data/serial_bcm283x_mu.h |  1 +
 7 files changed, 43 insertions(+), 1 deletion(-)
board/raspberrypi/rpi/rpi.c +33 -1
index 4c8253d..7f057e1 100644
@@ -51,7 +51,7 @@ U_BOOT_DEVICE(bcm2835_serials) = {
 	.platdata = &serial_platdata,
 };
 #else
-static const struct bcm283x_mu_serial_platdata serial_platdata = {
+static struct bcm283x_mu_serial_platdata serial_platdata = {
 	.base = 0x3f215040,
 	.clock = 250000000,
 	.skip_init = true,
@@ -453,6 +453,38 @@ int board_init(void)
 	return power_on_module(BCM2835_MBOX_POWER_DEVID_USB_HCD);
 }
 
+#ifndef CONFIG_PL01X_SERIAL
+static bool rpi_is_serial_active(void)
+{
+	int serial_gpio = 15;
+	struct udevice *dev;
+
+	/*
+	 * The RPi3 disables the mini uart by default. The easiest way to find
+	 * out whether it is available is to check if the RX pin is muxed.
+	 */
+
+	if (uclass_first_device(UCLASS_GPIO, &dev) || !dev)
+		return true;
+
+	if (bcm2835_gpio_get_func_id(dev, serial_gpio) != BCM2835_GPIO_ALT5)
+		return false;
+
+	return true;
+}
+#endif
+
+int board_early_init_f(void)
+{
+#ifndef CONFIG_PL01X_SERIAL
+	/* Disable mini-UART I/O if it's not pinmuxed to our pins */
+	if (!rpi_is_serial_active())
+		serial_platdata.disabled = true;
+#endif
+
+	return 0;
+}
+
 int board_mmc_init(bd_t *bis)
 {
 	ALLOC_CACHE_ALIGN_BUFFER(struct msg_get_clock_rate, msg_clk, 1);
configs/rpi_3_32b_defconfig +2 -0
index 922e01b..c59474c 100644
@@ -20,3 +20,5 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
+CONFIG_SYS_MALLOC_F_LEN=0x2000
configs/rpi_3_defconfig +2 -0
index bff92df..67c4a0c 100644
@@ -19,3 +19,5 @@ CONFIG_CMD_FAT=y
 CONFIG_CMD_FS_GENERIC=y
 CONFIG_PHYS_TO_BUS=y
 CONFIG_OF_LIBFDT=y
+# CONFIG_REQUIRE_SERIAL_CONSOLE is not set
+CONFIG_SYS_MALLOC_F_LEN=0x2000
drivers/gpio/bcm2835_gpio.c +1 -0
index 8b88d79..8dd7a28 100644
@@ -123,5 +123,6 @@ U_BOOT_DRIVER(gpio_bcm2835) = {
 	.id	= UCLASS_GPIO,
 	.ops	= &gpio_bcm2835_ops,
 	.probe	= bcm2835_gpio_probe,
+	.flags	= DM_FLAG_PRE_RELOC,
 	.priv_auto_alloc_size = sizeof(struct bcm2835_gpios),
 };
drivers/serial/serial_bcm283x_mu.c +3 -0
index 7357bbf..f4e062f 100644
@@ -73,6 +73,9 @@ static int bcm283x_mu_serial_probe(struct udevice *dev)
 	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
 
+	if (plat->disabled)
+		return -ENODEV;
+
 	priv->regs = (struct bcm283x_mu_regs *)plat->base;
 
 	return 0;
include/configs/rpi.h +1 -0
index b5543f4..4e5b3f3 100644
@@ -22,6 +22,7 @@
 
 /* Architecture, CPU, etc.*/
 #define CONFIG_ARCH_CPU_INIT
+#define CONFIG_BOARD_EARLY_INIT_F
 
 /* Use SoC timer for AArch32, but architected timer for AArch64 */
 #ifndef CONFIG_ARM64
include/dm/platform_data/serial_bcm283x_mu.h +1 -0
index 57ae6ad..c47d3c0 100644
@@ -19,6 +19,7 @@ struct bcm283x_mu_serial_platdata {
 	unsigned long base;
 	unsigned int clock;
 	bool skip_init;
+	bool disabled;
 };
 
 #endif
Signature · 1 line
1.8.5.6
#5 lore raw

PATCH v4 2/2 serial: bcm283x_mu: Detect disabled serial device

Quoted 7 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

Quoted 6 lines

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

Nits:

I'd hope for a core DM feature to disable statically created devices rather than re-implementing it per driver, so we don't have to re-invent this each time we need it. Still, we can refactor this later if it turns out to be more generally useful.

Perhaps a separate patch for the raw serial driver feature (serial_bcm283x_mu.h, serial_bcm283x_mu.c), and the board-specific logic (all the other files)?

#6 lore raw

PATCH v4 2/2 serial: bcm283x_mu: Detect disabled serial device

Quoted 18 lines · Stephen Warren
Quoted 8 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

Quoted 6 lines

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

Quoted 6 lines

Nits:

I'd hope for a core DM feature to disable statically created devices rather than re-implementing it per driver, so we don't have to re-invent this each time we need it. Still, we can refactor this later if it turns out to be more generally useful.

Agreed - yes let's wait until we have another case or two.

Quoted 1 line

Perhaps a separate patch for the raw serial driver feature (serial_bcm283x_mu.h, serial_bcm283x_mu.c), and the board-specific logic (all the other files)?

- Simon

#7 lore raw

PATCH v4 2/2 serial: bcm283x_mu: Detect disabled serial device

In reply to Stephen Warren
Quoted 19 lines

Am 16.08.2016 um 05:30 schrieb Stephen Warren <[email protected]>:

Quoted 8 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

Quoted 6 lines

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

Quoted 7 lines

Nits:

I'd hope for a core DM feature to disable statically created devices rather than re-implementing it per driver, so we don't have to re-invent this each time we need it. Still, we can refactor this later if it turns out to be more generally useful.

Perhaps a separate patch for the raw serial driver feature (serial_bcm283x_mu.h, serial_bcm283x_mu.c), and the board-specific logic (all the other files)?

I was thinking about it, but figured that these two lines of code are so heavily intertwined with the fact that you want to actually set the disabled property. So I decided against splitting them out - it wouldn't benefit readability, bisectability or revertability (which are the usual reasons for splitting patches).

Alex

#11 lore raw

PATCH 1/2 bcm2835_gpio: Implement GPIOF_FUNC

In reply to Alexander Graf
Quoted 9 lines · Alexander Graf

So far we could only tell the gpio framework that a GPIO was mapped as input or output, not as alternative function.

This patch adds support for determining whether a function is mapped as alternative.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160907/54537923/attachment.sig>
#8 lore raw

PATCH v4 2/2 serial: bcm283x_mu: Detect disabled serial device

In reply to Alexander Graf
Quoted 10 lines · Alexander Graf

On the raspberry pi, you can disable the serial port to gain dynamic frequency scaling which can get handy at times.

However, in such a configuration the serial controller gets its rx queue filled up with zero bytes which then happily get transmitted on to whoever calls getc() today.

This patch adds detection logic for that case by checking whether the RX pin is mapped to GPIO15 and disables the mini uart if it is not mapped properly.

Quoted 7 lines

That way we can leave the driver enabled in the tree and can determine during runtime whether serial is usable or not, having a single binary that allows for uart and non-uart operation.

Applied to u-boot/master, thanks!

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.denx.de/pipermail/u-boot/attachments/20160907/a294e143/attachment.sig>