Skip to contentSkip to conversation

serial: ns16550: Fix ordering of getting base address

PATCH In review Bin Meng 3 participants 6 messages Apr 3, 2020

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH 1/1 serial: ns16550: Fix ordering of getting base address

Currently the driver gets ns16550 base address in the driver probe() routine, which may potentially break any ns16550 wrapper driver that does additional initialization before calling ns16550_serial_probe().

Things are complicated that we need consider ns16550 devices on both simple-bus and PCI bus. To fix the issue we move the base address assignment for simple-bus ns16550 device back to the ofdata_to_platdata(), and assign base address for PCI ns16550 device in ns16550_serial_probe().

This is still not perfect. Ideally if any PCI bus based ns16550 wrapper driver tries to access plat->base before calling probe(), it is subject to break.

Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

---

 drivers/serial/ns16550.c | 51 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 27 deletions(-)
drivers/serial/ns16550.c +24 -27
index c1b303f..5e3cd1c 100644
@@ -479,39 +479,24 @@ static int ns16550_serial_getinfo(struct udevice *dev,
 	return 0;
 }
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-static int ns1655_serial_set_base_addr(struct udevice *dev)
-{
-	fdt_addr_t addr;
-	struct ns16550_platdata *plat;
-
-	plat = dev_get_platdata(dev);
-
-	addr = dev_read_addr_pci(dev);
-	if (addr == FDT_ADDR_T_NONE)
-		return -EINVAL;
-
-#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
-	plat->base = addr;
-#else
-	plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
-#endif
-
-	return 0;
-}
-#endif
-
 int ns16550_serial_probe(struct udevice *dev)
 {
+	struct ns16550_platdata *plat = dev->platdata;
 	struct NS16550 *const com_port = dev_get_priv(dev);
 	struct reset_ctl_bulk reset_bulk;
+	fdt_addr_t addr;
 	int ret;
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-	ret = ns1655_serial_set_base_addr(dev);
-	if (ret)
-		return ret;
-#endif
+	/*
+	 * If we are on PCI bus, either directly attached to a PCI root port,
+	 * or via a PCI bridge, assign platdata->base before probing hardware.
+	 */
+	if (device_is_on_pci_bus(dev)) {
+		addr = devfdt_get_addr_pci(dev);
+		if (addr == FDT_ADDR_T_NONE)
+			return -EINVAL;
+		plat->base = addr;
+	}
 
 	ret = reset_get_bulk(dev, &reset_bulk);
 	if (!ret)
@@ -535,9 +520,21 @@ int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
 {
 	struct ns16550_platdata *plat = dev->platdata;
 	const u32 port_type = dev_get_driver_data(dev);
+	fdt_addr_t addr;
 	struct clk clk;
 	int err;
 
+	addr = dev_read_addr(dev);
+	if (addr != FDT_ADDR_T_NONE) {
+#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
+		plat->base = addr;
+#else
+		plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
+#endif
+	} else if (!device_is_on_pci_bus(dev)) {
+		return -EINVAL;
+	}
+
 	plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
 	plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
 	plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Signature · 1 line
2.7.4
#1 lore raw

PATCH 1/1 serial: ns16550: Fix ordering of getting base address

Quoted 11 lines · Bin Meng

Currently the driver gets ns16550 base address in the driver probe() routine, which may potentially break any ns16550 wrapper driver that does additional initialization before calling ns16550_serial_probe().

Things are complicated that we need consider ns16550 devices on both simple-bus and PCI bus. To fix the issue we move the base address assignment for simple-bus ns16550 device back to the ofdata_to_platdata(), and assign base address for PCI ns16550 device in ns16550_serial_probe().

Quoted 3 lines

This is still not perfect. Ideally if any PCI bus based ns16550 wrapper driver tries to access plat->base before calling probe(), it is subject to break.

Thank you.
I have tested it and it fixes my case.

Quoted 2 lines
Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

I believe this should be one line.

Quoted 89 lines

---

 drivers/serial/ns16550.c | 51 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 27 deletions(-)
drivers/serial/ns16550.c +24 -27
index c1b303f..5e3cd1c 100644
@@ -479,39 +479,24 @@ static int ns16550_serial_getinfo(struct udevice *dev,
 	return 0;
 }
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-static int ns1655_serial_set_base_addr(struct udevice *dev)
-{
-	fdt_addr_t addr;
-	struct ns16550_platdata *plat;
-
-	plat = dev_get_platdata(dev);
-
-	addr = dev_read_addr_pci(dev);
-	if (addr == FDT_ADDR_T_NONE)
-		return -EINVAL;
-
-#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
-	plat->base = addr;
-#else
-	plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
-#endif
-
-	return 0;
-}
-#endif
-
 int ns16550_serial_probe(struct udevice *dev)
 {
+	struct ns16550_platdata *plat = dev->platdata;
 	struct NS16550 *const com_port = dev_get_priv(dev);
 	struct reset_ctl_bulk reset_bulk;
+	fdt_addr_t addr;
 	int ret;
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-	ret = ns1655_serial_set_base_addr(dev);
-	if (ret)
-		return ret;
-#endif
+	/*
+	 * If we are on PCI bus, either directly attached to a PCI root port,
+	 * or via a PCI bridge, assign platdata->base before probing hardware.
+	 */
+	if (device_is_on_pci_bus(dev)) {
+		addr = devfdt_get_addr_pci(dev);
+		if (addr == FDT_ADDR_T_NONE)
+			return -EINVAL;
+		plat->base = addr;
+	}
 
 	ret = reset_get_bulk(dev, &reset_bulk);
 	if (!ret)
@@ -535,9 +520,21 @@ int ns16550_serial_ofdata_to_platdata(struct udevice *dev)
 {
 	struct ns16550_platdata *plat = dev->platdata;
 	const u32 port_type = dev_get_driver_data(dev);
+	fdt_addr_t addr;
 	struct clk clk;
 	int err;
 
+	addr = dev_read_addr(dev);
+	if (addr != FDT_ADDR_T_NONE) {
+#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
+		plat->base = addr;
+#else
+		plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
+#endif
+	} else if (!device_is_on_pci_bus(dev)) {
+		return -EINVAL;
+	}
+
 	plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
 	plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
 	plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Signature · 1 line
2.7.4
Signature · 2 lines
With Best Regards,
Andy Shevchenko
author
#2 lore raw

PATCH 1/1 serial: ns16550: Fix ordering of getting base address

Hi Andy,

Quoted 13 lines · Andy Shevchenko
Quoted 11 lines · Bin Meng

Currently the driver gets ns16550 base address in the driver probe() routine, which may potentially break any ns16550 wrapper driver that does additional initialization before calling ns16550_serial_probe().

Things are complicated that we need consider ns16550 devices on both simple-bus and PCI bus. To fix the issue we move the base address assignment for simple-bus ns16550 device back to the ofdata_to_platdata(), and assign base address for PCI ns16550 device in ns16550_serial_probe().

Quoted 8 lines
Quoted 3 lines

This is still not perfect. Ideally if any PCI bus based ns16550 wrapper driver tries to access plat->base before calling probe(), it is subject to break.

Thank you.
I have tested it and it fixes my case.

Thanks for testing!

Quoted 6 lines
Quoted 4 lines
Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

I believe this should be one line.

I thought putting it in one line violates the 80 characters per column rule. And checkpatch.pl does not complain about this, so I think it's fine :)

Quoted 9 lines
Quoted 9 lines

---

 drivers/serial/ns16550.c | 51 +++++++++++++++++++++++-------------------------
 1 file changed, 24 insertions(+), 27 deletions(-)

Regards,
Bin

#3 lore raw

PATCH 1/1 serial: ns16550: Fix ordering of getting base address

Quoted 3 lines · Bin Meng
Quoted 1 line · Andy Shevchenko

On Fri, Apr 03, 2020 at 02:58:08AM -0700, Bin Meng wrote:

...

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

I believe this should be one line.

I thought putting it in one line violates the 80 characters per column rule. And checkpatch.pl does not complain about this, so I think it's fine :)

Breaking tags to two or more lines actually breaks the scripts around this stuff.

JFYI, in Linux kernel it was now in documentation (not to break Fixes, etc to two or more lines).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Tested-by Reviewed-by on v1 1/1
#4 lore raw

Antwort: [PATCH] serial: ns16550: Fix ordering of getting base address

In reply to Bin Meng

Hi Bin,

Thanks for taking care of this!

-----"Bin Meng" <[email protected]> schrieb: -----

Quoted 86 lines

An: "Simon Glass" <[email protected]>, "Tom Rini" <[email protected]>, "Andy Shevchenko" <[email protected]>, "Wolfgang Wallner" <[email protected]>, "Chee Hong Ang" <[email protected]>, "U-Boot Mailing List" <[email protected]>
Von: "Bin Meng" <[email protected]>
Datum: 03.04.2020 11:58
Betreff: [PATCH] serial: ns16550: Fix ordering of getting base address

Currently the driver gets ns16550 base address in the driver probe() routine, which may potentially break any ns16550 wrapper driver that does additional initialization before calling ns16550_serial_probe().

Things are complicated that we need consider ns16550 devices on both simple-bus and PCI bus. To fix the issue we move the base address assignment for simple-bus ns16550 device back to the ofdata_to_platdata(), and assign base address for PCI ns16550 device in ns16550_serial_probe().

This is still not perfect. Ideally if any PCI bus based ns16550 wrapper driver tries to access plat->base before calling probe(), it is subject to break.

Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

---

drivers/serial/ns16550.c | 51
+++++++++++++++++++++++-------------------------

1 file changed, 24 insertions(+), 27 deletions(-)
drivers/serial/ns16550.c +5 -27
index c1b303f..5e3cd1c 100644
@@ -479,39 +479,24 @@ static int ns16550_serial_getinfo(struct
 udevice *dev,
 	return 0;
 }
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-static int ns1655_serial_set_base_addr(struct udevice *dev)
-{
-	fdt_addr_t addr;
-	struct ns16550_platdata *plat;
-
-	plat = dev_get_platdata(dev);
-
-	addr = dev_read_addr_pci(dev);
-	if (addr == FDT_ADDR_T_NONE)
-		return -EINVAL;
-
-#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
-	plat->base = addr;
-#else
-	plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
-#endif
-
-	return 0;
-}
-#endif
-
 int ns16550_serial_probe(struct udevice *dev)
 {
+	struct ns16550_platdata *plat = dev->platdata;
 	struct NS16550 *const com_port = dev_get_priv(dev);
 	struct reset_ctl_bulk reset_bulk;
+	fdt_addr_t addr;
 	int ret;
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-	ret = ns1655_serial_set_base_addr(dev);
-	if (ret)
-		return ret;
-#endif
+	/*
+	 * If we are on PCI bus, either directly attached to a PCI root
 port,
+	 * or via a PCI bridge, assign platdata->base before probing

hardware.

drivers/serial/ns16550.c static int ns16550_serial_getinfo(struct udevice *dev, · line 498

Quoted 6 lines
drivers/serial/ns16550.c +6 -0
+	 */
+	if (device_is_on_pci_bus(dev)) {
+		addr = devfdt_get_addr_pci(dev);
+		if (addr == FDT_ADDR_T_NONE)
+			return -EINVAL;
+		plat->base = addr;

The assignment here to plat->base does not distinguish any more based on CONFIG_SYS_NS16550_PORT_MAPPED. Is it not necessary any more in this case?

Quoted 26 lines
drivers/serial/ns16550.c +1 -0
+	}
	ret = reset_get_bulk(dev, &reset_bulk);
	if (!ret)
drivers/serial/ns16550.c +12 -0
@@ -535,9 +520,21 @@ int ns16550_serial_ofdata_to_platdata(struct
 udevice *dev)
 {
 	struct ns16550_platdata *plat = dev->platdata;
 	const u32 port_type = dev_get_driver_data(dev);
+	fdt_addr_t addr;
 	struct clk clk;
 	int err;
 
+	addr = dev_read_addr(dev);
+	if (addr != FDT_ADDR_T_NONE) {
+#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
+		plat->base = addr;
+#else
+		plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
+#endif
+	} else if (!device_is_on_pci_bus(dev)) {
+		return -EINVAL;
+	}
+
 	plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
 	plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
Quoted 5 lines
	plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Signature · 1 line
2.7.4

Tested on an Intel Apollo Lake based board booting with Coreboot and using U-Boot as payload.

author
#5 lore raw

PATCH 1/1 serial: ns16550: Fix ordering of getting base address

Hi Wolfgang,

Quoted 94 lines · Wolfgang Wallner

Hi Bin,

Thanks for taking care of this!

-----"Bin Meng" <[email protected]> schrieb: -----

Quoted 87 lines

An: "Simon Glass" <[email protected]>, "Tom Rini" <[email protected]>, "Andy Shevchenko" <[email protected]>, "Wolfgang Wallner" <[email protected]>, "Chee Hong Ang" <[email protected]>, "U-Boot Mailing List" <[email protected]>
Von: "Bin Meng" <[email protected]>
Datum: 03.04.2020 11:58
Betreff: [PATCH] serial: ns16550: Fix ordering of getting base address

Currently the driver gets ns16550 base address in the driver probe() routine, which may potentially break any ns16550 wrapper driver that does additional initialization before calling ns16550_serial_probe().

Things are complicated that we need consider ns16550 devices on both simple-bus and PCI bus. To fix the issue we move the base address assignment for simple-bus ns16550 device back to the ofdata_to_platdata(), and assign base address for PCI ns16550 device in ns16550_serial_probe().

This is still not perfect. Ideally if any PCI bus based ns16550 wrapper driver tries to access plat->base before calling probe(), it is subject to break.

Fixes 720f9e1fdb0c9 ("serial: ns16550: Move PCI access from

ofdata_to_platdata() to probe()")

---

drivers/serial/ns16550.c | 51
+++++++++++++++++++++++-------------------------

1 file changed, 24 insertions(+), 27 deletions(-)
drivers/serial/ns16550.c +5 -27
index c1b303f..5e3cd1c 100644
@@ -479,39 +479,24 @@ static int ns16550_serial_getinfo(struct
 udevice *dev,
      return 0;
 }
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-static int ns1655_serial_set_base_addr(struct udevice *dev)
-{
-      fdt_addr_t addr;
-      struct ns16550_platdata *plat;
-
-      plat = dev_get_platdata(dev);
-
-      addr = dev_read_addr_pci(dev);
-      if (addr == FDT_ADDR_T_NONE)
-              return -EINVAL;
-
-#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
-      plat->base = addr;
-#else
-      plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
-#endif
-
-      return 0;
-}
-#endif
-
 int ns16550_serial_probe(struct udevice *dev)
 {
+      struct ns16550_platdata *plat = dev->platdata;
      struct NS16550 *const com_port = dev_get_priv(dev);
      struct reset_ctl_bulk reset_bulk;
+      fdt_addr_t addr;
      int ret;
 
-#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
-      ret = ns1655_serial_set_base_addr(dev);
-      if (ret)
-              return ret;
-#endif
+      /*
+       * If we are on PCI bus, either directly attached to a PCI root
 port,
+       * or via a PCI bridge, assign platdata->base before probing

hardware.
+ */

Quoted 9 lines

drivers/serial/ns16550.c static int ns16550_serial_getinfo(struct udevice *dev, · line 498

Quoted 5 lines
drivers/serial/ns16550.c +5 -0
+      if (device_is_on_pci_bus(dev)) {
+              addr = devfdt_get_addr_pci(dev);
+              if (addr == FDT_ADDR_T_NONE)
+                      return -EINVAL;
+              plat->base = addr;

The assignment here to plat->base does not distinguish any more based on CONFIG_SYS_NS16550_PORT_MAPPED. Is it not necessary any more in this case?

Yep, you are right. Both two cases should be handled consistently. I will spin a v2.

Quoted 33 lines
Quoted 27 lines
drivers/serial/ns16550.c +13 -0
+      }
 
      ret = reset_get_bulk(dev, &reset_bulk);
      if (!ret)
@@ -535,9 +520,21 @@ int ns16550_serial_ofdata_to_platdata(struct
 udevice *dev)
 {
      struct ns16550_platdata *plat = dev->platdata;
      const u32 port_type = dev_get_driver_data(dev);
+      fdt_addr_t addr;
      struct clk clk;
      int err;
 
+      addr = dev_read_addr(dev);
+      if (addr != FDT_ADDR_T_NONE) {
+#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
+              plat->base = addr;
+#else
+              plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
+#endif
+      } else if (!device_is_on_pci_bus(dev)) {
+              return -EINVAL;
+      }
+
      plat->reg_offset = dev_read_u32_default(dev, "reg-offset", 0);
      plat->reg_shift = dev_read_u32_default(dev, "reg-shift", 0);
      plat->reg_width = dev_read_u32_default(dev, "reg-io-width", 1);
Quoted 4 lines

--
2.7.4

Quoted 3 lines

Tested on an Intel Apollo Lake based board booting with Coreboot and using U-Boot as payload.

Thanks for testing!

Quoted 2 lines

Regards,
Bin