Skip to contentSkip to conversation

serial: ns16550: Fix ordering of getting base address

PATCH Superseded Bin Meng 3 participants 3 messages Apr 3, 2020

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bin Meng

author
#0 lore raw

PATCH v2 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()")

---

Changes in v2:
- not to break Fixes, etc to two or more lines in the commit message
- add the same CONFIG_SYS_NS16550_PORT_MAPPED ifdefs in the PCI case

 drivers/serial/ns16550.c | 53 ++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 26 deletions(-)
drivers/serial/ns16550.c +27 -26
index c1b303f..cb464a5 100644
@@ -479,39 +479,28 @@ 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;
+	/*
+	 * 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;
+#ifdef CONFIG_SYS_NS16550_PORT_MAPPED
+		plat->base = addr;
+#else
+		plat->base = (unsigned long)map_physmem(addr, 0, MAP_NOCACHE);
 #endif
+	}
 
 	ret = reset_get_bulk(dev, &reset_bulk);
 	if (!ret)
@@ -535,9 +524,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

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

Hi Bin,

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

Quoted 28 lines

Betreff: [PATCH v2] 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()")

---

Quoted 7 lines

Changes in v2:
- not to break Fixes, etc to two or more lines in the commit message
- add the same CONFIG_SYS_NS16550_PORT_MAPPED ifdefs in the PCI case

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

1 file changed, 27 insertions(+), 26 deletions(-)

I have also tested v2, just to be sure.
Works as expected.

regards, Wolfgang

#2 lore raw

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

In reply to Bin Meng
Quoted 21 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().

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 static int ns16550_serial_getinfo(struct udevice *dev, · line 500

Quoted 10 lines
drivers/serial/ns16550.c +0 -10
-	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;

(1) for below.

...

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

Quoted 9 lines
drivers/serial/ns16550.c +7 -0
+		addr = devfdt_get_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

And now is the question why we can't use above as a helper in both cases (either with check for address correctness or without)?

Quoted 1 line
(quoted diff) +1 -0
+	}

...

drivers/serial/ns16550.c int ns16550_serial_ofdata_to_platdata(struct udevice *dev) · line 540

Quoted 10 lines
drivers/serial/ns16550.c +10 -0
+	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;
+	}

Something like

	addr = dev_read_addr(dev);
	ret = ns16550_assign_base(addr, plat);
	if (ret && !device_is_on_pci_bus(...))
		return ret;
Signature · 2 lines
With Best Regards,
Andy Shevchenko