serial: sh-sci: Add support for dynamic instances

This change “serial: sh-sci: Add support for dynamic instances” in Linux kernel is authored by Geert Uytterhoeven <geert+renesas [at]> on Mon Mar 5 18:17:40 2018 +0100.

serial: sh-sci: Add support for dynamic instances

On DT platforms, the sh-sci driver requires the presence of "serialN"
aliases in DT, from which instance IDs are derived.  If a DT alias is
missing, the drivers fails to probe the corresponding serial port.

This becomes cumbersome when considering DT overlays, as currently
there is no upstream support for dynamically updating the /aliases node
in DT.  Furthermore, even in the presence of such support, hardcoded
instance IDs in independent overlays are prone to conflicts.

Hence add support for dynamic instance IDs, to be used in the absence of
a DT alias.  This makes serial ports behave similar to I2C and SPI
buses, which already support dynamic instances.

Ports in use are tracked using a simple bitmask of type unsigned long,
which is sufficient to handle all current hardware (max. 18 ports).
The maximum number of serial ports is still fixed, and configurable
through Kconfig.  Range validation is done through both Kconfig and a
compile-time check.

Due to the fixed maximum number of serial ports, dynamic and static
instances share the same ID space.  Static instances added later are
rejected when conflicting with dynamic instances registered earlier.

Signed-off-by: Geert Uytterhoeven <>
Signed-off-by: Greg Kroah-Hartman <>

This Linux change may have been applied to various maintained Linux releases and you can find Linux releases including commit 7678f4c.

There are 10 lines of Linux source code added/deleted in this change. Code changes to Linux kernel are as follows.

 drivers/tty/serial/Kconfig  | 2 ++
 drivers/tty/serial/sh-sci.c | 8 ++++++++
 2 files changed, 10 insertions(+)

diff --git a/drivers/tty/serial/Kconfig b/drivers/tty/serial/Kconfig
index 0f058df..3c5dd20 100644
--- a/drivers/tty/serial/Kconfig
+++ b/drivers/tty/serial/Kconfig
@@ -676,6 +676,8 @@ config SERIAL_SH_SCI
 	int "Maximum number of SCI(F) serial ports" if EXPERT
+	range 1 64 if 64BIT
+	range 1 32 if !64BIT
 	depends on SERIAL_SH_SCI
 	default "3" if H8300
 	default "10" if SUPERH
diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c
index fdbbff5..9197069 100644
--- a/drivers/tty/serial/sh-sci.c
+++ b/drivers/tty/serial/sh-sci.c
@@ -160,6 +160,7 @@ struct sci_port {
 static struct sci_port sci_ports[SCI_NPORTS];
+static unsigned long sci_ports_in_use;
 static struct uart_driver sci_uart_driver;
 static inline struct sci_port *
@@ -3026,6 +3027,7 @@ static int sci_remove(struct platform_device *dev)
 	struct sci_port *port = platform_get_drvdata(dev);
+	sci_ports_in_use &= ~BIT(port->port.line);
 	uart_remove_one_port(&sci_uart_driver, &port->port);
@@ -3107,6 +3109,8 @@ static struct plat_sci_port *sci_parse_dt(struct platform_device *pdev,
 	/* Get the line number from the aliases node. */
 	id = of_alias_get_id(np, "serial");
+	if (id < 0 && ~sci_ports_in_use)
+		id = ffz(sci_ports_in_use);
 	if (id < 0) {
 		dev_err(&pdev->dev, "failed to get alias id (%d)n", id);
 		return NULL;
@@ -3141,6 +3145,9 @@ static int sci_probe_single(struct platform_device *dev,
 		dev_notice(&dev->dev, "Consider bumping CONFIG_SERIAL_SH_SCI_NR_UARTS!n");
 		return -EINVAL;
+	BUILD_BUG_ON(SCI_NPORTS > sizeof(sci_ports_in_use) * 8);
+	if (sci_ports_in_use & BIT(index))
+		return -EBUSY;
 	if (!sci_uart_driver.state) {
@@ -3239,6 +3246,7 @@ static int sci_probe(struct platform_device *dev)
+	sci_ports_in_use |= BIT(dev_id);
 	return 0;

The commit for this change in Linux stable tree is 7678f4c (patch).

Leave a Reply

Your email address will not be published. Required fields are marked *