Skip to contentSkip to conversation

RPi: Properly handle dynamic serial configuration

PATCH Superseded Alexander Graf 3 participants 22 messages Jan 17, 2018

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover RPi: Properly handle dynamic serial configuration

The RPi has proprietary firmware that can be configured (using config.txt) to expose either the PL11, Mini-UART or no serial device to the UART pins on the GPIO pin bar of the RPi.

So far we've only half-heartedly dealt with that, with lost of heuristics that ended up falling apart at times. For example the RPi3 CM uses PL011 when serial is enabled in config.txt, but we disabled PL11 support for BCM2837 because the RPi3 uses the Mini-UART with enable_uart=1 is set in config.txt.

This patch set always enables both serial outputs and determines at probe time whether a serial device is actually muxed to the UART pins on the board. Only in that case, it will be probed and thus used for in- and output in U-Boot.

With this patch set applied, I have successfully used the same U-Boot binary with CONFIG_OF_BOARD=y and a RPi firmware provided device tree with both enable_uart=1 and without on both a RPi3 and RPi3 CM.

This patch set depends on the patch set "Rpi: Add support for second sd host controller".

v1 -> v2:

- Make search logic easier to follow

Alexander Graf (7):
serial: Use next serial device if probing fails
serial: Allow boards to determine whether a serial device is usable
rpi: Remove runtime disabling support for serial
serial: bcm283x_mu: Remove support for post-init disabling
rpi: Properly detect which serial device is active
rpi: Determine PL011/Mini-UART availability at runtime
rpi: Force skip_init on serial devices

 arch/arm/mach-bcm283x/include/mach/gpio.h    |  1 -
 board/raspberrypi/rpi/rpi.c                  | 91 +++++++++++++++++++---------
 drivers/gpio/bcm2835_gpio.c                  |  2 +-
 drivers/serial/serial-uclass.c               | 36 +++++++++--
 drivers/serial/serial_bcm283x_mu.c           | 20 +-----
 drivers/serial/serial_pl01x.c                |  2 +-
 include/configs/rpi.h                        |  5 +-
 include/dm/platform_data/serial_bcm283x_mu.h |  1 -
 include/serial.h                             | 11 ++++
 9 files changed, 108 insertions(+), 61 deletions(-)
Signature · 1 line
2.12.3
#1 lore raw

PATCH v2 1/7 serial: Use next serial device if probing fails

Currently our serial device search chokes on the fact that the serial probe function could fail. If it does, instead of searching for the next usable serial device, it just quits.

This patch changes the fallback logic so that even when a serial device was not probed correctly, we just try the next ones until we find one that works.

---

v1 -> v2:

- Make search logic easier to follow

---
 drivers/serial/serial-uclass.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
drivers/serial/serial-uclass.c +19 -6
index 2e5116f7ce..68ca2d09d1 100644
@@ -74,6 +74,7 @@ static void serial_find_console_or_panic(void)
 {
 	const void *blob = gd->fdt_blob;
 	struct udevice *dev;
+	int ret;
 
 	if (CONFIG_IS_ENABLED(OF_PLATDATA)) {
 		uclass_first_device(UCLASS_SERIAL, &dev);
@@ -104,8 +105,8 @@ static void serial_find_console_or_panic(void)
 		 * from 1!).
 		 *
 		 * Failing that, get the device with sequence number 0, or in
-		 * extremis just the first serial device we can find. But we
-		 * insist on having a console (even if it is silent).
+		 * extremis just the first working serial device we can find.
+		 * But we insist on having a console (even if it is silent).
 		 */
 #ifdef CONFIG_CONS_INDEX
 #define INDEX (CONFIG_CONS_INDEX - 1)
@@ -113,10 +114,22 @@ static void serial_find_console_or_panic(void)
 #define INDEX 0
 #endif
 		if (!uclass_get_device_by_seq(UCLASS_SERIAL, INDEX, &dev) ||
-		    !uclass_get_device(UCLASS_SERIAL, INDEX, &dev) ||
-		    (!uclass_first_device(UCLASS_SERIAL, &dev) && dev)) {
-			gd->cur_serial_dev = dev;
-			return;
+		    !uclass_get_device(UCLASS_SERIAL, INDEX, &dev)) {
+			if (dev->flags & DM_FLAG_ACTIVATED) {
+				gd->cur_serial_dev = dev;
+				return;
+			}
+		}
+
+		/* Search for any working device */
+		for (ret = uclass_first_device_check(UCLASS_SERIAL, &dev);
+		     dev;
+		     ret = uclass_next_device_check(&dev)) {
+			if (!ret) {
+				/* Device did succeed probing */
+				gd->cur_serial_dev = dev;
+				return;
+			}
 		}
 #undef INDEX
 	}
Signature · 1 line
2.12.3
#2 lore raw

PATCH v2 1/7 serial: Use next serial device if probing fails

Quoted 8 lines · Alexander Graf

Currently our serial device search chokes on the fact that the serial probe function could fail. If it does, instead of searching for the next usable serial device, it just quits.

This patch changes the fallback logic so that even when a serial device was not probed correctly, we just try the next ones until we find one that works.

Quoted 7 lines

---

v1 -> v2:

- Make search logic easier to follow

#3 lore raw

PATCH v2 1/7 serial: Use next serial device if probing fails

In reply to Alexander Graf
Quoted 10 lines · Alexander Graf

Currently our serial device search chokes on the fact that the serial probe function could fail. If it does, instead of searching for the next usable serial device, it just quits.

This patch changes the fallback logic so that even when a serial device was not probed correctly, we just try the next ones until we find one that works.

Quoted 8 lines

---

v1 -> v2:

- Make search logic easier to follow

---
 drivers/serial/serial-uclass.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

The fallback logic is getting more complicated. How come the DT does not specify the correct console?

Regards,
Simon

#4 lore raw

PATCH v2 1/7 serial: Use next serial device if probing fails

Quoted 20 lines · Simon Glass
Quoted 10 lines · Alexander Graf

Currently our serial device search chokes on the fact that the serial probe function could fail. If it does, instead of searching for the next usable serial device, it just quits.

This patch changes the fallback logic so that even when a serial device was not probed correctly, we just try the next ones until we find one that works.

Quoted 8 lines

---

v1 -> v2:

- Make search logic easier to follow

---
 drivers/serial/serial-uclass.c | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)
Quoted 4 lines

The fallback logic is getting more complicated. How come the DT does not specify the correct console?

By default the DT on the RPi does not specify any serial console. However, users have grown accustomed to the fact that there is indeed one :).

I guess it all boils down to the fact that Linux is terrible when it comes to console selection. 99.9% of users that want a serial console and have a graphical screen want to have *both* available as first class citizen consoles. But Linux just can't do that, so we have a lot of crude hacks all over the place.

Alex

#5 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

In reply to Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)
drivers/serial/serial-uclass.c +11 -0
index 68ca2d09d1..ecd64f8e86 100644
@@ -366,6 +366,16 @@ static int on_baudrate(const char *name, const char *value, enum env_op op,
 U_BOOT_ENV_CALLBACK(baudrate, on_baudrate);
 
 #if CONFIG_IS_ENABLED(SERIAL_PRESENT)
+__weak int board_check_serial(struct udevice *dev)
+{
+	return 0;
+}
+
+static int serial_pre_probe(struct udevice *dev)
+{
+	return board_check_serial(dev);
+}
+
 static int serial_post_probe(struct udevice *dev)
 {
 	struct dm_serial_ops *ops = serial_get_ops(dev);
@@ -438,6 +448,7 @@ UCLASS_DRIVER(serial) = {
 	.name		= "serial",
 	.flags		= DM_UC_FLAG_SEQ_ALIAS,
 	.post_probe	= serial_post_probe,
+	.pre_probe	= serial_pre_probe,
 	.pre_remove	= serial_pre_remove,
 	.per_device_auto_alloc_size = sizeof(struct serial_dev_priv),
 };
include/serial.h +11 -0
index d87f01082a..221b3e1402 100644
@@ -207,4 +207,15 @@ void sh_serial_initialize(void);
 void uartlite_serial_initialize(void);
 void zynq_serial_initialize(void);
 
+/**
+ * board_check_serial() - Determine whether a serial device works
+ *
+ * This is a board callback that allows boards to override whether a serial
+ * device is usable. By default, all devices are declared usable.
+ *
+ * @dev: Device pointer
+ * @return 0 if the device is usable, !0 otherwise
+ */
+int board_check_serial(struct udevice *dev);
+
 #endif
Signature · 1 line
2.12.3
#6 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

Hi Alex,

Quoted 9 lines · Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

Quoted 6 lines
---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)

Can we please figure out how handle this in the serial driver / driver model itself? I want to avoid weak functions with driver model.

Regards,
Simon

#7 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

Quoted 19 lines · Simon Glass

Hi Alex,

Quoted 9 lines · Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

Quoted 6 lines
---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)
Quoted 2 lines

Can we please figure out how handle this in the serial driver / driver model itself? I want to avoid weak functions with driver model.

I'm very happy to see suggestions :). The reason I went with the weak function is really because I couldn't think of anything better.

Alex

#8 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

Hi Alex,

Quoted 22 lines · Alexander Graf
Quoted 19 lines · Simon Glass

Hi Alex,

Quoted 15 lines · Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)
Quoted 5 lines
Quoted 2 lines

Can we please figure out how handle this in the serial driver / driver model itself? I want to avoid weak functions with driver model.

I'm very happy to see suggestions :). The reason I went with the weak function is really because I couldn't think of anything better.

The serial driver is proprietary so you should be able to put a call into the pinctrl driver from that. The pinctrl driver can return the state of a pin - maybe get_gpio_mux() or a new pinmux_get()?

Regards,
Simon

#9 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

Quoted 28 lines · Simon Glass

Hi Alex,

Quoted 25 lines · Alexander Graf
Quoted 21 lines · Simon Glass

Hi Alex,

Quoted 9 lines · Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

Quoted 6 lines
---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)

Can we please figure out how handle this in the serial driver / driver model itself? I want to avoid weak functions with driver model.

Quoted 6 lines
Quoted 2 lines

I'm very happy to see suggestions :). The reason I went with the weak function is really because I couldn't think of anything better.

The serial driver is proprietary so you should be able to put a call into the pinctrl driver from that. The pinctrl driver can return the state of a pin - maybe get_gpio_mux() or a new pinmux_get()?

The SoC has 2 serial drivers: a proprietary one and a pl011. Would you think it's ok to put an architecture specific hack into the generic pl011 code with an #ifdef?

Alex

#10 lore raw

PATCH v2 2/7 serial: Allow boards to determine whether a serial device is usable

Hi Alex,

Quoted 34 lines · Alexander Graf
Quoted 31 lines · Simon Glass

Hi Alex,

Quoted 27 lines · Alexander Graf
Quoted 19 lines · Simon Glass

Hi Alex,

Quoted 9 lines · Alexander Graf

On some boards, serial devices may or may not be muxed properly to actual pins, depending on firmware configuration. To determine whether we should use a serial device for U-Boot in-/output, we need to check whether it is muxed properly.

This is something only the board file can do, so let's expose a weak function that a board can override to explicitly allow or disallow usage of certain serial devices.

Quoted 6 lines
---
 drivers/serial/serial-uclass.c | 11 +++++++++++
 include/serial.h               | 11 +++++++++++
 2 files changed, 22 insertions(+)
Quoted 2 lines

Can we please figure out how handle this in the serial driver / driver model itself? I want to avoid weak functions with driver model.

I'm very happy to see suggestions :). The reason I went with the weak function is really because I couldn't think of anything better.

Quoted 7 lines
Quoted 3 lines

The serial driver is proprietary so you should be able to put a call into the pinctrl driver from that. The pinctrl driver can return the state of a pin - maybe get_gpio_mux() or a new pinmux_get()?

The SoC has 2 serial drivers: a proprietary one and a pl011. Would you think it's ok to put an architecture specific hack into the generic pl011 code with an #ifdef?

Normally in this situation we create an outer driver which uses the other one - see serial_rockchip.c for example. That way the base driver doesn't have to know about arch-specific things.

Regards,
Simon

#11 lore raw

PATCH v2 3/7 rpi: Remove runtime disabling support for serial

In reply to Alexander Graf

We are switching to a model where our board file can directly fail probing of serial devices when they're not usable, so remove the current runtime hack we have.

---
 arch/arm/mach-bcm283x/include/mach/gpio.h |  1 -
 board/raspberrypi/rpi/rpi.c               | 43 -------------------------------
 drivers/gpio/bcm2835_gpio.c               |  2 +-
 3 files changed, 1 insertion(+), 45 deletions(-)
arch/arm/mach-bcm283x/include/mach/gpio.h +0 -1
index 751594d09f..1bcb5846ca 100644
@@ -61,7 +61,6 @@ struct bcm2835_gpio_platdata {
 	unsigned long base;
 };
 
-int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio);
 void bcm2835_gpio_set_pinmux(struct udevice *dev, int handle);
 
 #endif /* _BCM2835_GPIO_H_ */
board/raspberrypi/rpi/rpi.c +0 -43

54 lines not on this page · raw message

drivers/gpio/bcm2835_gpio.c +1 -1
index 209cbed9e6..3edd90ea97 100644
@@ -73,7 +73,7 @@ static int bcm2835_gpio_set_value(struct udevice *dev, unsigned gpio,
 	return 0;
 }
 
-int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
+static int bcm2835_gpio_get_func_id(struct udevice *dev, unsigned gpio)
 {
 	struct bcm2835_gpios *gpios = dev_get_priv(dev);
 	u32 val;
Signature · 1 line
2.12.3
#12 lore raw

PATCH v2 4/7 serial: bcm283x_mu: Remove support for post-init disabling

In reply to Alexander Graf

We are switching to a model where a serial device doesn't even get probed when it's not muxed properly, so we don't need device specific disabling functionality anymore.

---
 drivers/serial/serial_bcm283x_mu.c           | 18 +-----------------
 include/dm/platform_data/serial_bcm283x_mu.h |  1 -
 2 files changed, 1 insertion(+), 18 deletions(-)
drivers/serial/serial_bcm283x_mu.c +1 -17
index 41c26b3d93..7ce990b9b8 100644
@@ -59,7 +59,7 @@ static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
 	struct bcm283x_mu_regs *regs = priv->regs;
 	u32 divider;
 
-	if (plat->disabled || plat->skip_init)
+	if (plat->skip_init)
 		return 0;
 
 	divider = plat->clock / (baudrate * 8);
@@ -75,9 +75,6 @@ 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;
@@ -85,14 +82,10 @@ static int bcm283x_mu_serial_probe(struct udevice *dev)
 
 static int bcm283x_mu_serial_getc(struct udevice *dev)
 {
-	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
 	struct bcm283x_mu_regs *regs = priv->regs;
 	u32 data;
 
-	if (plat->disabled)
-		return -EAGAIN;
-
 	/* Wait until there is data in the FIFO */
 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
 		return -EAGAIN;
@@ -104,13 +97,9 @@ static int bcm283x_mu_serial_getc(struct udevice *dev)
 
 static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
 {
-	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
 	struct bcm283x_mu_regs *regs = priv->regs;
 
-	if (plat->disabled)
-		return 0;
-
 	/* Wait until there is space in the FIFO */
 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
 		return -EAGAIN;
@@ -123,14 +112,10 @@ static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
 
 static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
 {
-	struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
 	struct bcm283x_mu_regs *regs = priv->regs;
 	unsigned int lsr;
 
-	if (plat->disabled)
-		return 0;
-
 	lsr = readl(&regs->lsr);
 
 	if (input) {
@@ -168,7 +153,6 @@ static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
 				     1);
 	plat->skip_init = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
 	                                  "skip-init");
-	plat->disabled = false;
 	return 0;
 }
 #endif
include/dm/platform_data/serial_bcm283x_mu.h +0 -1
index c47d3c0e60..57ae6adc05 100644
@@ -19,7 +19,6 @@ struct bcm283x_mu_serial_platdata {
 	unsigned long base;
 	unsigned int clock;
 	bool skip_init;
-	bool disabled;
 };
 
 #endif
Signature · 1 line
2.12.3
#13 lore raw

PATCH v2 5/7 rpi: Properly detect which serial device is active

In reply to Alexander Graf

Now that we have all infrastructure in place to dynamically determine whether a serial device is actually usable (read: routed to user accessible pins), we can wire it up to the board.

This patch adds support to determine whether the pl011 or mini-uart or no serial is routed to the UART RX/TX pins on the Raspberry Pi family of boards.

---
 board/raspberrypi/rpi/rpi.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
board/raspberrypi/rpi/rpi.c +69 -0
index a96d5d8952..b0cdad70f7 100644
@@ -24,9 +24,16 @@
 #include <asm/armv8/mmu.h>
 #endif
 #include <watchdog.h>
+#include <asm/io.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * This is the GPIO pin that the user facing UART RX line is attached to.
+ * We use this pin to determine which serial device is available.
+ */
+#define BCM2835_GPIO_RX		15
+
 /* From lowlevel_init.S */
 extern unsigned long fw_dtb_pointer;
 
@@ -419,6 +426,68 @@ static void get_board_rev(void)
 	printf("RPI %s (0x%x)\n", model->name, revision);
 }
 
+/*
+ * We may get called before the device model is initialized, so we can not
+ * rely on the GPIO driver.
+ */
+int get_func_id(unsigned gpio)
+{
+	u32 val;
+	u32 node;
+	u32 *gpfsel;
+	fdt_addr_t addr;
+	fdt_size_t size;
+
+	node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2835-gpio");
+	if (node < 0)
+		return -EINVAL;
+
+	addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg",
+						  0, &size, true);
+	gpfsel = (void*)addr;
+
+	val = readl(&gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+
+	return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
+}
+
+
+/*
+ * The RPi has 2 serial ports: A PL011 based one and the mini-uart.
+ * Depending on firmware configuration, either can be configured to either
+ * nothing, the wifi adapter or serial output.
+ *
+ * We only want to use the serial port that is user facing to not
+ * end up with a potentially unresponsive serial port. Due to this
+ * we need to check whether the serial device is actually connected
+ * to the UART RX/TX pins on the RPi GPIO pin bar.
+ *
+ * We only allow U-Boot to instantiate the serial driver for the serial
+ * device that is muxed correctly.
+ */
+int board_check_serial(struct udevice *dev)
+{
+	int func;
+
+	printf("Checking serial %s\n", dev->name);
+
+	if (device_is_compatible(dev, "arm,pl011")) {
+		func = BCM2835_GPIO_ALT0;
+	} else if (device_is_compatible(dev, "brcm,bcm2835-aux-uart")) {
+		func = BCM2835_GPIO_ALT5;
+	} else {
+		return 0;
+	}
+
+	if (get_func_id(BCM2835_GPIO_RX) != func) {
+		printf("Disabling serial %s\n", dev->name);
+		return -ENODEV;
+	}
+
+	printf("Enabling serial %s\n", dev->name);
+	return 0;
+}
+
 int board_init(void)
 {
 #ifdef CONFIG_HW_WATCHDOG
Signature · 1 line
2.12.3
#14 lore raw

PATCH v2 5/7 rpi: Properly detect which serial device is active

Hi Alex,

Quoted 51 lines · Alexander Graf

Now that we have all infrastructure in place to dynamically determine whether a serial device is actually usable (read: routed to user accessible pins), we can wire it up to the board.

This patch adds support to determine whether the pl011 or mini-uart or no serial is routed to the UART RX/TX pins on the Raspberry Pi family of boards.

---
 board/raspberrypi/rpi/rpi.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
board/raspberrypi/rpi/rpi.c +21 -0
index a96d5d8952..b0cdad70f7 100644
@@ -24,9 +24,16 @@
 #include <asm/armv8/mmu.h>
 #endif
 #include <watchdog.h>
+#include <asm/io.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * This is the GPIO pin that the user facing UART RX line is attached to.
+ * We use this pin to determine which serial device is available.
+ */
+#define BCM2835_GPIO_RX                15
+
 /* From lowlevel_init.S */
 extern unsigned long fw_dtb_pointer;
 
@@ -419,6 +426,68 @@ static void get_board_rev(void)
        printf("RPI %s (0x%x)\n", model->name, revision);
 }
 
+/*
+ * We may get called before the device model is initialized, so we can not
+ * rely on the GPIO driver.
+ */
+int get_func_id(unsigned gpio)
+{
+       u32 val;
+       u32 node;
+       u32 *gpfsel;
+       fdt_addr_t addr;
+       fdt_size_t size;
+
+       node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2835-gpio");
+       if (node < 0)

board/raspberrypi/rpi/rpi.c static void get_board_rev(void) · line 452

Quoted 10 lines
board/raspberrypi/rpi/rpi.c +10 -0
+               return -EINVAL;
+
+       addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg",
+                                                 0, &size, true);
+       gpfsel = (void*)addr;
+
+       val = readl(&gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+
+       return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
+}

Ick, this should be done in the GPIO driver and use gpio_get_function().

Regards,
Simon

#15 lore raw

PATCH v2 5/7 rpi: Properly detect which serial device is active

Quoted 58 lines · Simon Glass

Hi Alex,

Quoted 55 lines · Alexander Graf

Now that we have all infrastructure in place to dynamically determine whether a serial device is actually usable (read: routed to user accessible pins), we can wire it up to the board.

This patch adds support to determine whether the pl011 or mini-uart or no serial is routed to the UART RX/TX pins on the Raspberry Pi family of boards.

---
 board/raspberrypi/rpi/rpi.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
board/raspberrypi/rpi/rpi.c +25 -0
index a96d5d8952..b0cdad70f7 100644
@@ -24,9 +24,16 @@
 #include <asm/armv8/mmu.h>
 #endif
 #include <watchdog.h>
+#include <asm/io.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * This is the GPIO pin that the user facing UART RX line is attached to.
+ * We use this pin to determine which serial device is available.
+ */
+#define BCM2835_GPIO_RX                15
+
 /* From lowlevel_init.S */
 extern unsigned long fw_dtb_pointer;
 
@@ -419,6 +426,68 @@ static void get_board_rev(void)
        printf("RPI %s (0x%x)\n", model->name, revision);
 }
 
+/*
+ * We may get called before the device model is initialized, so we can not
+ * rely on the GPIO driver.
+ */
+int get_func_id(unsigned gpio)
+{
+       u32 val;
+       u32 node;
+       u32 *gpfsel;
+       fdt_addr_t addr;
+       fdt_size_t size;
+
+       node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2835-gpio");
+       if (node < 0)
+               return -EINVAL;
+
+       addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg",
+                                                 0, &size, true);
Quoted 8 lines

board/raspberrypi/rpi/rpi.c static void get_board_rev(void) · line 452

Quoted 6 lines
board/raspberrypi/rpi/rpi.c +6 -0
+       gpfsel = (void*)addr;
+
+       val = readl(&gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+
+       return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
+}

Ick, this should be done in the GPIO driver and use gpio_get_function().

Yes, but what if users specify the serial device to be pre-reloc and don't do that for the GPIO (or really pinctrl) one? Then we'd not have the driver around to determine whether serial is active, right?

Alex

#16 lore raw

PATCH v2 5/7 rpi: Properly detect which serial device is active

Hi Alex,

Quoted 70 lines · Alexander Graf
Quoted 58 lines · Simon Glass

Hi Alex,

Quoted 55 lines · Alexander Graf

Now that we have all infrastructure in place to dynamically determine whether a serial device is actually usable (read: routed to user accessible pins), we can wire it up to the board.

This patch adds support to determine whether the pl011 or mini-uart or no serial is routed to the UART RX/TX pins on the Raspberry Pi family of boards.

---
 board/raspberrypi/rpi/rpi.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 69 insertions(+)
board/raspberrypi/rpi/rpi.c +25 -0
index a96d5d8952..b0cdad70f7 100644
@@ -24,9 +24,16 @@
 #include <asm/armv8/mmu.h>
 #endif
 #include <watchdog.h>
+#include <asm/io.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+/*
+ * This is the GPIO pin that the user facing UART RX line is attached to.
+ * We use this pin to determine which serial device is available.
+ */
+#define BCM2835_GPIO_RX                15
+
 /* From lowlevel_init.S */
 extern unsigned long fw_dtb_pointer;
 
@@ -419,6 +426,68 @@ static void get_board_rev(void)
        printf("RPI %s (0x%x)\n", model->name, revision);
 }
 
+/*
+ * We may get called before the device model is initialized, so we can not
+ * rely on the GPIO driver.
+ */
+int get_func_id(unsigned gpio)
+{
+       u32 val;
+       u32 node;
+       u32 *gpfsel;
+       fdt_addr_t addr;
+       fdt_size_t size;
+
+       node = fdt_node_offset_by_compatible(gd->fdt_blob, -1, "brcm,bcm2835-gpio");
+       if (node < 0)
+               return -EINVAL;
+
+       addr = fdtdec_get_addr_size_auto_noparent(gd->fdt_blob, node, "reg",
+                                                 0, &size, true);
Quoted 8 lines

board/raspberrypi/rpi/rpi.c static void get_board_rev(void) · line 452

Quoted 6 lines
board/raspberrypi/rpi/rpi.c +6 -0
+       gpfsel = (void*)addr;
+
+       val = readl(&gpfsel[BCM2835_GPIO_FSEL_BANK(gpio)]);
+
+       return (val >> BCM2835_GPIO_FSEL_SHIFT(gpio) & BCM2835_GPIO_FSEL_MASK);
+}

Ick, this should be done in the GPIO driver and use gpio_get_function().

Quoted 3 lines

Yes, but what if users specify the serial device to be pre-reloc and don't do that for the GPIO (or really pinctrl) one? Then we'd not have the driver around to determine whether serial is active, right?

We have control of that in the DT so just need to set it up correctly. I suppose worst case you could add the pre-reloc flag to each driver's decl instead of in DT. But I'm not sure why the DT would be wrong.

Regards,
Simon

#17 lore raw

PATCH v2 6/7 rpi: Determine PL011/Mini-UART availability at runtime

In reply to Alexander Graf

Firmware on the Raspberry Pi family of devices can dynamically configure either the PL011, Mini-UART or no device at all to be routed to the user accessible UART pins.

That means we need to always include both drivers, because we can never be sure which of the two serial devices firmware actually chooses to use.

---
 include/configs/rpi.h | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
include/configs/rpi.h +1 -4
index cab8661779..2c84cf9a49 100644
@@ -75,12 +75,9 @@
 #define CONFIG_MISC_INIT_R
 #endif
 
-/* Console UART */
-#if defined (CONFIG_BCM2837) || defined(CONFIG_TARGET_RPI_0_W)
+/* Console UART, can be configured dynamically in config.txt */
 #define CONFIG_BCM283X_MU_SERIAL
-#else
 #define CONFIG_PL01X_SERIAL
-#endif
 
 /* Console configuration */
 #define CONFIG_SYS_CBSIZE		1024
Signature · 1 line
2.12.3
#18 lore raw

PATCH v2 7/7 rpi: Force skip_init on serial devices

In reply to Alexander Graf

The serial devices on the raspberry pi are based on clocks we can't easily read and influence in U-Boot. However, the serial devices are always already properly set up when coming up, so all we need to do is leave them alone.

The way to do that is to specify "skip-init" in device tree usually, but if we set CONFIG_OF_BOARD to get the device tree from the RPi firmware, that does not have skip-init properly set.

So instead we just force it in board specific code. That way serial devices also work fine when skip-init is not passed explicitly in DT.

---
 board/raspberrypi/rpi/rpi.c        | 7 +++++++
 drivers/serial/serial_bcm283x_mu.c | 2 +-
 drivers/serial/serial_pl01x.c      | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)
board/raspberrypi/rpi/rpi.c +7 -0
index b0cdad70f7..ce1286a53a 100644
@@ -20,6 +20,7 @@
 #include <asm/arch/sdhci.h>
 #include <asm/global_data.h>
 #include <dm/platform_data/serial_bcm283x_mu.h>
+#include <dm/platform_data/serial_pl01x.h>
 #ifdef CONFIG_ARM64
 #include <asm/armv8/mmu.h>
 #endif
@@ -472,9 +473,15 @@ int board_check_serial(struct udevice *dev)
 	printf("Checking serial %s\n", dev->name);
 
 	if (device_is_compatible(dev, "arm,pl011")) {
+		struct pl01x_serial_platdata *plat = dev_get_platdata(dev);
+
 		func = BCM2835_GPIO_ALT0;
+		plat->skip_init = true;
 	} else if (device_is_compatible(dev, "brcm,bcm2835-aux-uart")) {
+		struct bcm283x_mu_serial_platdata *plat = dev_get_platdata(dev);
+
 		func = BCM2835_GPIO_ALT5;
+		plat->skip_init = true;
 	} else {
 		return 0;
 	}
drivers/serial/serial_bcm283x_mu.c +1 -1
index 7ce990b9b8..8a7ca75d4a 100644
@@ -151,7 +151,7 @@ static int bcm283x_mu_serial_ofdata_to_platdata(struct udevice *dev)
 	plat->base = addr;
 	plat->clock = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "clock",
 				     1);
-	plat->skip_init = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
+	plat->skip_init |= fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
 	                                  "skip-init");
 	return 0;
 }
drivers/serial/serial_pl01x.c +1 -1
index 4ec0f29c42..f957eddc07 100644
@@ -357,7 +357,7 @@ static int pl01x_serial_ofdata_to_platdata(struct udevice *dev)
 	plat->clock = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "clock",
 				     1);
 	plat->type = dev_get_driver_data(dev);
-	plat->skip_init = fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
+	plat->skip_init |= fdtdec_get_bool(gd->fdt_blob, dev_of_offset(dev),
 	                                  "skip-init");
 	return 0;
 }
Signature · 1 line
2.12.3
#19 lore raw

PATCH v2 7/7 rpi: Force skip_init on serial devices

Hi Alex,

Quoted 11 lines · Alexander Graf

The serial devices on the raspberry pi are based on clocks we can't easily read and influence in U-Boot. However, the serial devices are always already properly set up when coming up, so all we need to do is leave them alone.

The way to do that is to specify "skip-init" in device tree usually, but if we set CONFIG_OF_BOARD to get the device tree from the RPi firmware, that does not have skip-init properly set.

So instead we just force it in board specific code. That way serial devices also work fine when skip-init is not passed explicitly in DT.

Quoted 6 lines
---
 board/raspberrypi/rpi/rpi.c        | 7 +++++++
 drivers/serial/serial_bcm283x_mu.c | 2 +-
 drivers/serial/serial_pl01x.c      | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)

Would you mind converting these drivers to livetree before adding these patches?

Regards,
Simon

#20 lore raw

PATCH v2 7/7 rpi: Force skip_init on serial devices

Quoted 14 lines · Simon Glass

Hi Alex,

Quoted 11 lines · Alexander Graf

The serial devices on the raspberry pi are based on clocks we can't easily read and influence in U-Boot. However, the serial devices are always already properly set up when coming up, so all we need to do is leave them alone.

The way to do that is to specify "skip-init" in device tree usually, but if we set CONFIG_OF_BOARD to get the device tree from the RPi firmware, that does not have skip-init properly set.

So instead we just force it in board specific code. That way serial devices also work fine when skip-init is not passed explicitly in DT.

Quoted 8 lines
Quoted 6 lines
---
 board/raspberrypi/rpi/rpi.c        | 7 +++++++
 drivers/serial/serial_bcm283x_mu.c | 2 +-
 drivers/serial/serial_pl01x.c      | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)

Would you mind converting these drivers to livetree before adding these patches?

Uh, why? I don't quite see how it's related - and I'm not sure I'll make the rc1 cut-off even with just the pile I have right now :)

Alex

#21 lore raw

PATCH v2 7/7 rpi: Force skip_init on serial devices

Hi Alex,

Quoted 24 lines · Alexander Graf
Quoted 21 lines · Simon Glass

Hi Alex,

Quoted 17 lines · Alexander Graf

The serial devices on the raspberry pi are based on clocks we can't easily read and influence in U-Boot. However, the serial devices are always already properly set up when coming up, so all we need to do is leave them alone.

The way to do that is to specify "skip-init" in device tree usually, but if we set CONFIG_OF_BOARD to get the device tree from the RPi firmware, that does not have skip-init properly set.

So instead we just force it in board specific code. That way serial devices also work fine when skip-init is not passed explicitly in DT.

---
 board/raspberrypi/rpi/rpi.c        | 7 +++++++
 drivers/serial/serial_bcm283x_mu.c | 2 +-
 drivers/serial/serial_pl01x.c      | 2 +-
 3 files changed, 9 insertions(+), 2 deletions(-)
Quoted 4 lines
Quoted 1 line

Would you mind converting these drivers to livetree before adding these patches?

Uh, why? I don't quite see how it's related - and I'm not sure I'll make the rc1 cut-off even with just the pile I have right now :)

I'm trying to make sure that people that touch drivers convert them, since otherwise it won't get done. I hope we can accept v2/v3 patches which just miss rc1.

Regards,
Simon