[U-Boot] [PATCH v2] serial: bcm283x_mu: Detect disabled serial device

Alexander Graf agraf at suse.de
Thu Aug 4 09:11:22 CEST 2016


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.

Signed-off-by: Alexander Graf <agraf at suse.de>

---

I'm currently on the road and verified that the non-uart case works properly
with this patch and that the uart case doesn't do anything stupid, but please
double check it before applying :).

---
 board/raspberrypi/rpi/rpi.c                  |  1 +
 drivers/serial/serial_bcm283x_mu.c           | 33 ++++++++++++++++++++++++++++
 include/dm/platform_data/serial_bcm283x_mu.h |  1 +
 3 files changed, 35 insertions(+)

diff --git a/board/raspberrypi/rpi/rpi.c b/board/raspberrypi/rpi/rpi.c
index 4c8253d..9c52c91 100644
--- a/board/raspberrypi/rpi/rpi.c
+++ b/board/raspberrypi/rpi/rpi.c
@@ -53,6 +53,7 @@ U_BOOT_DEVICE(bcm2835_serials) = {
 #else
 static const struct bcm283x_mu_serial_platdata serial_platdata = {
 	.base = 0x3f215040,
+	.gpio = 0x3f200000,
 	.clock = 250000000,
 	.skip_init = true,
 };
diff --git a/drivers/serial/serial_bcm283x_mu.c b/drivers/serial/serial_bcm283x_mu.c
index 7357bbf..02d5f39 100644
--- a/drivers/serial/serial_bcm283x_mu.c
+++ b/drivers/serial/serial_bcm283x_mu.c
@@ -39,6 +39,20 @@ struct bcm283x_mu_regs {
 	u32 baud;
 };
 
+struct bcm283x_gpio_regs {
+	u32 gpfsel0;
+	u32 gpfsel1;
+	u32 gpfsel2;
+	u32 gpfsel3;
+	u32 gpfsel4;
+	u32 gpfsel5;
+	/* ... */
+};
+
+#define BCM283X_GPIO_GPFSEL1_F15_SHIFT	15
+#define BCM283X_GPIO_ALTFUNC_MASK	0x7
+#define BCM283X_GPIO_ALTFUNC_5		0x2
+
 #define BCM283X_MU_LCR_DATA_SIZE_8	3
 
 #define BCM283X_MU_LSR_TX_IDLE		BIT(6)
@@ -48,6 +62,7 @@ struct bcm283x_mu_regs {
 
 struct bcm283x_mu_priv {
 	struct bcm283x_mu_regs *regs;
+	bool disabled;
 };
 
 static int bcm283x_mu_serial_setbrg(struct udevice *dev, int baudrate)
@@ -72,9 +87,18 @@ 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);
+	struct bcm283x_gpio_regs *gpio = (struct bcm283x_gpio_regs *)plat->gpio;
 
 	priv->regs = (struct bcm283x_mu_regs *)plat->base;
 
+	/*
+	 * 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 (((readl(&gpio->gpfsel1) >> BCM283X_GPIO_GPFSEL1_F15_SHIFT) &
+	    BCM283X_GPIO_ALTFUNC_MASK) != BCM283X_GPIO_ALTFUNC_5)
+		priv->disabled = true;
+
 	return 0;
 }
 
@@ -84,6 +108,9 @@ static int bcm283x_mu_serial_getc(struct udevice *dev)
 	struct bcm283x_mu_regs *regs = priv->regs;
 	u32 data;
 
+	if (priv->disabled)
+		return -EAGAIN;
+
 	/* Wait until there is data in the FIFO */
 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_RX_READY))
 		return -EAGAIN;
@@ -98,6 +125,9 @@ static int bcm283x_mu_serial_putc(struct udevice *dev, const char data)
 	struct bcm283x_mu_priv *priv = dev_get_priv(dev);
 	struct bcm283x_mu_regs *regs = priv->regs;
 
+	if (priv->disabled)
+		return 0;
+
 	/* Wait until there is space in the FIFO */
 	if (!(readl(&regs->lsr) & BCM283X_MU_LSR_TX_EMPTY))
 		return -EAGAIN;
@@ -114,6 +144,9 @@ static int bcm283x_mu_serial_pending(struct udevice *dev, bool input)
 	struct bcm283x_mu_regs *regs = priv->regs;
 	unsigned int lsr = readl(&regs->lsr);
 
+	if (priv->disabled)
+		return 0;
+
 	if (input) {
 		WATCHDOG_RESET();
 		return (lsr & BCM283X_MU_LSR_RX_READY) ? 1 : 0;
diff --git a/include/dm/platform_data/serial_bcm283x_mu.h b/include/dm/platform_data/serial_bcm283x_mu.h
index 57ae6ad..b2e32d3 100644
--- a/include/dm/platform_data/serial_bcm283x_mu.h
+++ b/include/dm/platform_data/serial_bcm283x_mu.h
@@ -17,6 +17,7 @@
  */
 struct bcm283x_mu_serial_platdata {
 	unsigned long base;
+	unsigned long gpio;
 	unsigned int clock;
 	bool skip_init;
 };
-- 
2.6.6



More information about the U-Boot mailing list