Skip to contentSkip to conversation

Add UART driver for Suplus SP7021 SoC

PATCH Superseded Hammer Hsieh 3 participants 5 messages Dec 1, 2021 – Dec 3, 2021

Subsystems (3)

v4 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Hammer Hsieh

author maintainer
#0 lore raw

v4 cover Add UART driver for Suplus SP7021 SoC

This is a patch series for UART driver for Suplus SP7021 SoC.

Sunplus SP7021 is an ARM Cortex A7 (4 cores) based SoC. It integrates many peripherals (ex: UART. I2C, SPI, SDIO, eMMC, USB, SD card and etc.) into a single chip. It is designed for industrial control.

Refer to:
https://sunplus-tibbo.atlassian.net/wiki/spaces/doc/overview
https://tibbo.com/store/plus1.html

Hammer Hsieh (2):
dt-bindings:serial:Add bindings doc for Sunplus SoC UART Driver serial:sunplus-uart:Add Sunplus SoC UART Driver

 .../bindings/serial/sunplus,sp7021-uart.yaml       |  58 ++
 MAINTAINERS                                        |   6 +
 drivers/tty/serial/Kconfig                         |  23 +
 drivers/tty/serial/Makefile                        |   1 +
 drivers/tty/serial/sunplus-uart.c                  | 848 +++++++++++++++++++++
 5 files changed, 936 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/sunplus,sp7021-uart.yaml
 create mode 100644 drivers/tty/serial/sunplus-uart.c
Signature · 1 line
2.7.4
author maintainer
#1 lore raw

PATCH v4 1/2 dt-bindings:serial:Add bindings doc for Sunplus SoC UART Driver

Add bindings doc for Sunplus SoC UART Driver

---
Changes in v4:
- no change.
- Reviewed-by : Rob Herring <[email protected]> in v3.

 .../bindings/serial/sunplus,sp7021-uart.yaml       | 58 ++++++++++++++++++++++
 MAINTAINERS                                        |  5 ++
 2 files changed, 63 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/serial/sunplus,sp7021-uart.yaml
Documentation/devicetree/bindings/serial/sunplus,sp7021-uart.yaml new file +58 -0
new file mode 100644
index 0000000..df11074
@@ -0,0 +1,58 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+# Copyright (C) Sunplus Co., Ltd. 2021
+%YAML 1.2
+---
+$id: "http://devicetree.org/schemas/serial/sunplus,sp7021-uart.yaml#"
+$schema: "http://devicetree.org/meta-schemas/core.yaml#"
+
+title: Sunplus SoC SP7021 UART Controller Device Tree Bindings
+
+maintainers:
+  - Tony Huang <[email protected]>
+  - Hammer Hsieh <[email protected]>
+  - Wells Lu <[email protected]>
+
+allOf:
+  - $ref: serial.yaml#
+
+properties:
+  compatible:
+    const: sunplus,sp7021-uart
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+  resets:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+  - resets
+
+additionalProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/irq.h>
+    aliases {
+            serial0 = &uart0;
+    };
+
+    uart0: serial@9c000900 {
+        compatible = "sunplus,sp7021-uart";
+        reg = <0x9c000900 0x80>;
+        interrupt-parent = <&intc>;
+        interrupts = <53 IRQ_TYPE_LEVEL_HIGH>;
+        clocks = <&clkc 0x28>;
+        resets = <&rstc 0x18>;
+    };
+...
MAINTAINERS +5 -0
index 3b79fd4..f2ee40c 100644
@@ -17945,6 +17945,11 @@ L:	[email protected]
 S:	Maintained
 F:	drivers/net/ethernet/dlink/sundance.c
 
+SUNPLUS UART DRIVER
+M:	Hammer Hsieh <[email protected]>
+S:	Maintained
+F:	Documentation/devicetree/bindings/serial/sunplus,sp7021-uart.yaml
+
 SUPERH
 M:	Yoshinori Sato <[email protected]>
 M:	Rich Felker <[email protected]>
Signature · 1 line
2.7.4
author maintainer
#2 lore raw

PATCH v4 2/2 serial:sunplus-uart:Add Sunplus SoC UART Driver

In reply to Hammer Hsieh

Add Sunplus SoC UART Driver

---
Changes in v4:
- Addressed all comments from Andy Shevchenko except pm runtime function.

 MAINTAINERS                       |   1 +
 drivers/tty/serial/Kconfig        |  23 ++
 drivers/tty/serial/Makefile       |   1 +
 drivers/tty/serial/sunplus-uart.c | 848 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 873 insertions(+)
 create mode 100644 drivers/tty/serial/sunplus-uart.c
MAINTAINERS +1 -0
index f2ee40c..65681ff 100644
@@ -17949,6 +17949,7 @@ SUNPLUS UART DRIVER
 M:	Hammer Hsieh <[email protected]>
 S:	Maintained
 F:	Documentation/devicetree/bindings/serial/sunplus,sp7021-uart.yaml
+F:	drivers/tty/serial/sunplus-uart.c
 
 SUPERH
 M:	Yoshinori Sato <[email protected]>
drivers/tty/serial/Kconfig +23 -0
index 131a6a5..ffdcf40 100644
@@ -1561,6 +1561,29 @@ config SERIAL_LITEUART_CONSOLE
 	  and warnings and which allows logins in single user mode).
 	  Otherwise, say 'N'.
 
+config SERIAL_SUNPLUS
+	tristate "Sunplus UART support"
+	depends on OF || COMPILE_TEST
+	select SERIAL_CORE
+	help
+	  Select this option if you would like to use Sunplus serial port on
+	  Sunplus SoC SP7021.
+	  If you enable this option, Sunplus serial ports in the system will
+	  be registered as ttySUPx.
+
+config SERIAL_SUNPLUS_CONSOLE
+	bool "Console on Sunplus UART"
+	depends on SERIAL_SUNPLUS
+	select SERIAL_CORE_CONSOLE
+	select SERIAL_EARLYCON
+	help
+	  Select this option if you would like to use a Sunplus UART as the
+	  system console.
+	  Even if you say Y here, the currently visible virtual console
+	  (/dev/tty0) will still be used as the system console by default, but
+	  you can alter that using a kernel command line option such as
+	  "console=ttySUPx".
+
 endmenu
 
 config SERIAL_MCTRL_GPIO
drivers/tty/serial/Makefile +1 -0
index 7da0856..61cc8de 100644
@@ -87,6 +87,7 @@ obj-$(CONFIG_SERIAL_RDA)	+= rda-uart.o
 obj-$(CONFIG_SERIAL_MILBEAUT_USIO) += milbeaut_usio.o
 obj-$(CONFIG_SERIAL_SIFIVE)	+= sifive.o
 obj-$(CONFIG_SERIAL_LITEUART) += liteuart.o
+obj-$(CONFIG_SERIAL_SUNPLUS)	+= sunplus-uart.o
 
 # GPIOLIB helpers for modem control lines
 obj-$(CONFIG_SERIAL_MCTRL_GPIO)	+= serial_mctrl_gpio.o
drivers/tty/serial/sunplus-uart.c new file +848 -0

848 lines not on this page · raw message

Signature · 1 line
2.7.4
#3 lore raw

Re: [PATCH v4 2/2] serial:sunplus-uart:Add Sunplus SoC UART Driver

Quoted 2 lines · Hammer Hsieh

Add Sunplus SoC UART Driver

...

drivers/tty/serial/Kconfig config SERIAL_LITEUART_CONSOLE · line 1572

Quoted 9 lines
drivers/tty/serial/Kconfig +9 -0
+config SERIAL_SUNPLUS
+       tristate "Sunplus UART support"
+       depends on OF || COMPILE_TEST
+       select SERIAL_CORE
+       help
+         Select this option if you would like to use Sunplus serial port on
+         Sunplus SoC SP7021.
+         If you enable this option, Sunplus serial ports in the system will
+         be registered as ttySUPx.

What will be the module name in case of module build?

...

Quoted 4 lines

+/*
+ * Sunplus SoC UART driver
+ *
+ * Author: Hammer Hsieh <[email protected]>

Authors:

Quoted 2 lines

+ * Tony Huang <[email protected]>
+ * Wells Lu <[email protected]>

And please indent names to be on the same column.

Quoted 1 line

+ */

...

Quoted 1 line

+#define UART_AUTOSUSPEND_TIMEOUT 3000

Add units to the name.

...

drivers/tty/serial/sunplus-uart.c line 97

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static inline u32 sunplus_tx_buf_not_full(struct uart_port *port)
+{
+       unsigned int lsr = readl(port->membase + SUP_UART_LSR);
+
+       return ((lsr & SUP_UART_LSR_TX) ? SUP_UART_LSR_TX_NOT_FULL : 0);

Too many parentheses. Ditto for all similar cases.

Quoted 1 line

+}

...

drivers/tty/serial/sunplus-uart.c line 227

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+       do {
+               sp_uart_put_char(port, xmit->buf[xmit->tail]);
+               xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);

"% UART_XMIT_SIZE" is more accurate since it doesn't require a value to be a power of 2. In case of power of 2 it will be properly optimized by a compiler.

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+               port->icount.tx++;
+
+               if (uart_circ_empty(xmit))
+                       break;
+       } while (sunplus_tx_buf_not_full(port));

...

Quoted 1 line

+ spin_lock_irqsave(&port->lock, flags);

Why irqsave here?

...

drivers/tty/serial/sunplus-uart.c line 300

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+       if (readl(port->membase + SUP_UART_ISC) & SUP_UART_ISC_RX)
+               receive_chars(port);
+
+       if (readl(port->membase + SUP_UART_ISC) & SUP_UART_ISC_TX)
+               transmit_chars(port);

Do you really need to perform two I/O against the very same register?

...

drivers/tty/serial/sunplus-uart.c line 312

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static int sunplus_startup(struct uart_port *port)
+{
+       unsigned int isc;
+       int ret;
 
+#ifdef CONFIG_PM

Why is this ifdeffery around the driver?

Quoted 10 lines
drivers/tty/serial/sunplus-uart.c new file +10 -0
+       if (!uart_console(port)) {
+               ret = pm_runtime_get_sync(port->dev);
+               if (ret < 0)
+                       goto out;
+       }
+#endif
+       ret = request_irq(port->irq, sunplus_uart_irq, 0, "sunplus_uart", port);
+       if (ret)
+               return ret;
+

drivers/tty/serial/sunplus-uart.c line 332

Quoted 10 lines
drivers/tty/serial/sunplus-uart.c new file +9 -0
+       spin_lock_irq(&port->lock);
+
+       isc |= SUP_UART_ISC_RXM;
+       writel(isc, port->membase + SUP_UART_ISC);
+
+       spin_unlock_irq(&port->lock);
 
+#ifdef CONFIG_PM
+       if (!uart_console(port))
+               pm_runtime_put(port->dev);

Why doesn't it set autosuspend, i.o.w. Why is it different from an error case?

Quoted 9 lines
drivers/tty/serial/sunplus-uart.c new file +9 -0
+       return 0;
+out:
+       if (!uart_console(port)) {
+               pm_runtime_mark_last_busy(port->dev);
+               pm_runtime_put_autosuspend(port->dev);
+       }
+#endif
+       return 0;
+}

...

drivers/tty/serial/sunplus-uart.c line 363

Quoted 9 lines
drivers/tty/serial/sunplus-uart.c new file +8 -0
+static void sunplus_set_termios(struct uart_port *port,
+                               struct ktermios *termios,
+                               struct ktermios *oldtermios)
+{
+       u32 clk, ext, div, div_l, div_h, baud;
+       u32 lcr, val;
+       unsigned long flags;
 
+       clk = port->uartclk;

This can be done in the definition block above.

drivers/tty/serial/sunplus-uart.c line 368

Quoted 4 lines
drivers/tty/serial/sunplus-uart.c new file +4 -0
+       baud = uart_get_baud_rate(port, termios, oldtermios, 0, port->uartclk / 16);
+
+       readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
+                                 (val & SUP_UART_LSR_TXE), 1, 10000);

No error check?

drivers/tty/serial/sunplus-uart.c line 373

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+       /*
+        * baud rate = uartclk / ((16 * div[15:0] + 1) + div_ext[3:0])
+        * get target baud rate and uartclk
+        * auto calculate div and div_ext
+        * div_h = (div[15:8] >> 8); div_l = (div_ext[3:0] << 12) + div[7:0]

There is no need to explain the code, please add excerpts from the data sheet on how the divisors and baud rate are calculated, use mathematical language, and not programming in the comment.

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +7 -0
+        */
+       clk += baud >> 1;
+       div = clk / baud;
+       ext = div & 0x0F;
+       div = (div >> 4) - 1;
+       div_l = (div & 0xFF) | (ext << 12);
+       div_h = div >> 8;

...

drivers/tty/serial/sunplus-uart.c line 460

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static const char *sunplus_type(struct uart_port *port)
+{
+       struct sunplus_uart_port *sup = to_sunplus_uart(port);
+
+       return sup->port.type == PORT_SUNPLUS ? "sunplus_uart" : NULL;
+}

What do we achieve with this? Who and how will see this information? Under which circumstances the port type is not SUNPLUS?

...

drivers/tty/serial/sunplus-uart.c line 469

Quoted 8 lines
drivers/tty/serial/sunplus-uart.c new file +8 -0
+static void sunplus_release_port(struct uart_port *port)
+{
+}
+
+static int sunplus_request_port(struct uart_port *port)
+{
+       return 0;
+}

Are these stubs mandatory?

...

drivers/tty/serial/sunplus-uart.c line 476

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static void sunplus_config_port(struct uart_port *port, int type)
+{
 
+       if (type & UART_CONFIG_TYPE) {
+               port->type = PORT_SUNPLUS;
+               sunplus_request_port(port);
+       }

if (!(type & ...))
return;

?

Quoted 1 line

+}

...

drivers/tty/serial/sunplus-uart.c line 491

Quoted 4 lines
drivers/tty/serial/sunplus-uart.c new file +4 -0
+static int sunplus_poll_init(struct uart_port *port)
+{
+       return 0;
+}

Why is this stub needed?

...

drivers/tty/serial/sunplus-uart.c line 542

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static inline void wait_for_xmitr(struct uart_port *port)
+{
+       unsigned int val;
+
+       readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
+                                 (val & SUP_UART_LSR_TX), 1, 10000);

Error handling?
Or explain why it's not needed.

Quoted 1 line

+}

...

drivers/tty/serial/sunplus-uart.c line 636

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+       sup->clk = devm_clk_get(&pdev->dev, NULL);
+       if (!IS_ERR(sup->clk)) {

Instead use devm_clk_get_optional().

drivers/tty/serial/sunplus-uart.c line 643

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+               ret = clk_prepare_enable(sup->clk);
+               if (ret)
+                       return ret;
 
+               ret = devm_add_action_or_reset(&pdev->dev,
+                                              (void(*)(void *))clk_disable_unprepare,
+                                              sup->clk);

Look at the examples of how other drivers do this (that were submitted more or less recently).

drivers/tty/serial/sunplus-uart.c line 648

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+               if (ret)
+                       return ret;
+       } else {
 
+               if (PTR_ERR(sup->clk) == -EPROBE_DEFER)
+                       return -EPROBE_DEFER;

Why?!

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+               return dev_err_probe(&pdev->dev, PTR_ERR(sup->clk), "clk not found\n");
+       }

...

drivers/tty/serial/sunplus-uart.c line 660

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       port->membase = devm_ioremap_resource(&pdev->dev, res);

We have an API that does both at once. Use it.

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+       if (IS_ERR(port->membase))
+               return dev_err_probe(&pdev->dev, PTR_ERR(port->membase), "membase not found\n");

...

drivers/tty/serial/sunplus-uart.c line 680

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+       ret = reset_control_deassert(sup->rstc);
+       if (ret)
+               return ret;

From here no reset assertion on error? Why?

...

drivers/tty/serial/sunplus-uart.c line 686

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+#ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
+       if (pdev->id == 0)
+               port->cons = &sunplus_uart_console;
+       sunplus_console_ports[sup->port.line] = sup;
+#endif

Actually why don't you use register_console() ?

...

drivers/tty/serial/sunplus-uart.c line 781

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static struct platform_driver sunplus_uart_platform_driver = {
+       .probe          = sunplus_uart_probe,
+       .remove         = sunplus_uart_remove,
+       .driver = {
+               .name   = "sunplus_uart",
 
+               .of_match_table = of_match_ptr(sp_uart_of_match),

How did you test this when OF=n and COMPILE_TEST=y?
Hint: Compiler will warn about unused variables (you need W=1).

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+               .pm     = &sunplus_uart_pm_ops,
+       }
+};

...

drivers/tty/serial/sunplus-uart.c line 810

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static void __exit sunplus_uart_exit(void)
+{
+       platform_driver_unregister(&sunplus_uart_platform_driver);
+       uart_unregister_driver(&sunplus_uart_driver);
+}
 
+

Dtop this blank line...

drivers/tty/serial/sunplus-uart.c line 812

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+module_init(sunplus_uart_init);
+module_exit(sunplus_uart_exit);

...and attach each of the calls to the implemented function.

...

drivers/tty/serial/sunplus-uart.c line 842

Quoted 12 lines
drivers/tty/serial/sunplus-uart.c new file +11 -0
+static int __init
+sunplus_uart_early_setup(struct earlycon_device *dev, const char *opt)
+{
+       if (!(dev->port.membase || dev->port.iobase))
+               return -ENODEV;
+
+       dev->con->write = sunplus_uart_early_write;
+
+       return 0;
+}
 
+

No blank line.

Quoted 1 line

+OF_EARLYCON_DECLARE(sunplus_uart, "sunplus,sp7021-uart", sunplus_uart_early_setup);

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

RE: [PATCH v4 2/2] serial:sunplus-uart:Add Sunplus SoC UART Driver

Hi, Andy Shevchenko :

Thanks for your review.
Please see my response in below mail.

Regards,
Hammer Hsieh

Quoted 31 lines

-----Original Message-----
From: Andy Shevchenko <[email protected]>
Sent: Friday, December 3, 2021 4:03 AM
To: Hammer Hsieh <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>; Rob Herring <[email protected]>; open list:SERIAL DRIVERS <[email protected]>; devicetree <[email protected]>; Linux Kernel Mailing List <[email protected]>; Jiri Slaby <[email protected]>; Philipp Zabel <[email protected]>; Tony Huang 黃懷厚 <[email protected]>; Wells Lu 呂芳騰 <[email protected]>; Hammer Hsieh 謝宏孟 <[email protected]>
Subject: Re: [PATCH v4 2/2] serial:sunplus-uart:Add Sunplus SoC UART Driver

Quoted 2 lines · Hammer Hsieh

Add Sunplus SoC UART Driver

...

drivers/tty/serial/Kconfig config SERIAL_LITEUART_CONSOLE · line 1572

Quoted 9 lines
drivers/tty/serial/Kconfig +9 -0
+config SERIAL_SUNPLUS
+       tristate "Sunplus UART support"
+       depends on OF || COMPILE_TEST
+       select SERIAL_CORE
+       help
+         Select this option if you would like to use Sunplus serial port on
+         Sunplus SoC SP7021.
+         If you enable this option, Sunplus serial ports in the system will
+         be registered as ttySUPx.
Quoted 2 lines

What will be the module name in case of module build?

will add info as below shows in Kconfig:
"This driver can also be built as a module. If so, the module will be called sunplus-uart."

Quoted 7 lines

...

Quoted 4 lines

+/*
+ * Sunplus SoC UART driver
+ *
+ * Author: Hammer Hsieh <[email protected]>

Quoted 7 lines

Authors:

Quoted 2 lines

+ * Tony Huang <[email protected]>
+ * Wells Lu <[email protected]>

And please indent names to be on the same column.

I rewrite almost all uart driver.
The other authors ask me to remove their names on this driver. Will modify it.

Quoted 8 lines
Quoted 1 line

+ */

...

Quoted 1 line

+#define UART_AUTOSUSPEND_TIMEOUT 3000

Add units to the name.

Will add it. /* units: ms */

Quoted 12 lines

...

Review

Quoted 5 lines
(quoted diff) +5 -0
+static inline u32 sunplus_tx_buf_not_full(struct uart_port *port) {
+       unsigned int lsr = readl(port->membase + SUP_UART_LSR);
+
+       return ((lsr & SUP_UART_LSR_TX) ? SUP_UART_LSR_TX_NOT_FULL :
+ 0);

Too many parentheses. Ditto for all similar cases.

Quoted 1 line

+}

ok, I have found similar case. I will modify it.

Quoted 9 lines

...

drivers/tty/serial/sunplus-uart.c line 227

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+       do {
+               sp_uart_put_char(port, xmit->buf[xmit->tail]);
+               xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);

"% UART_XMIT_SIZE" is more accurate since it doesn't require a value to be a power of 2. In case of power of 2 it will be properly optimized by a compiler.

Currently, I have found drivers/tty/serial/mps2-uart.c use it only. ok, will modify it.

Quoted 14 lines
Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+               port->icount.tx++;
+
+               if (uart_circ_empty(xmit))
+                       break;
+       } while (sunplus_tx_buf_not_full(port));

...

Quoted 1 line

+ spin_lock_irqsave(&port->lock, flags);

Why irqsave here?

...

Quoted 8 lines

drivers/tty/serial/sunplus-uart.c line 300

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+       if (readl(port->membase + SUP_UART_ISC) & SUP_UART_ISC_RX)
+               receive_chars(port);
+
+       if (readl(port->membase + SUP_UART_ISC) & SUP_UART_ISC_TX)
+               transmit_chars(port);

Do you really need to perform two I/O against the very same register?

I will rewrite as below, how do you think? Static irqreturn_t sunplus_uart_irq(int irq, void *args) {
Struct uart_port *port = (struct uart_port *)args; unsigned int isc = readl(port->membase + SUP_UART_ISC); if (isc & SUP_UART_ISC_RX)
receive_chars(port);
if (isc & SUP_UART_ISC_TX)
transmit_chars(port);
return IRQ_HANDLED;
}

Quoted 28 lines

...

drivers/tty/serial/sunplus-uart.c line 312

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +4 -0
+static int sunplus_startup(struct uart_port *port) {
+       unsigned int isc;
+       int ret;
 
+#ifdef CONFIG_PM

Why is this ifdeffery around the driver?

drivers/tty/serial/sunplus-uart.c

Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +7 -0
+       if (!uart_console(port)) {
+               ret = pm_runtime_get_sync(port->dev);
+               if (ret < 0)
+                       goto out;
+       }
+#endif
+       ret = request_irq(port->irq, sunplus_uart_irq, 0, "sunplus_uart",

port);

Quoted 9 lines
drivers/tty/serial/sunplus-uart.c new file +9 -0
+       if (ret)
+               return ret;
+
+       spin_lock_irq(&port->lock);
+
+       isc |= SUP_UART_ISC_RXM;
+       writel(isc, port->membase + SUP_UART_ISC);
+
+       spin_unlock_irq(&port->lock);
Quoted 6 lines

drivers/tty/serial/sunplus-uart.c line 332

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+#ifdef CONFIG_PM
+       if (!uart_console(port))
+               pm_runtime_put(port->dev);

Why doesn't it set autosuspend, i.o.w. Why is it different from an error case?

Autosuspend already init at probe.
I remove #ifdef CONFIG_PM code in sunplus_startup() and test runtime function. linux-serial-test -y 0x55 -z 0x30 -p /dev/ttySUP1 -b 115200 runtime_resume and runtime_suspend still work.
I will remove it.

Quoted 19 lines
Quoted 9 lines
drivers/tty/serial/sunplus-uart.c new file +9 -0
+       return 0;
+out:
+       if (!uart_console(port)) {
+               pm_runtime_mark_last_busy(port->dev);
+               pm_runtime_put_autosuspend(port->dev);
+       }
+#endif
+       return 0;
+}

...

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+static void sunplus_set_termios(struct uart_port *port,
+                               struct ktermios *termios,
+                               struct ktermios *oldtermios) {
+       u32 clk, ext, div, div_l, div_h, baud;
+       u32 lcr, val;
+       unsigned long flags;
Quoted 4 lines
Quoted 1 line

+ clk = port->uartclk;

This can be done in the definition block above.

I think you want the code like below, right ?
u32 ext, div, div_l, div_h, baud, lcr;
u32 clk = port->uartclk;
unsigned long flags;

Quoted 9 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+       baud = uart_get_baud_rate(port, termios, oldtermios, 0,
+ port->uartclk / 16);
+
+       readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
+                                 (val & SUP_UART_LSR_TXE), 1,

10000);

No error check?

remove this code in set_termios( ).
I think it is not necessary to check tx empty here.

Quoted 11 lines

drivers/tty/serial/sunplus-uart.c

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +6 -0
+       /*
+        * baud rate = uartclk / ((16 * div[15:0] + 1) + div_ext[3:0])
+        * get target baud rate and uartclk
+        * auto calculate div and div_ext
+        * div_h = (div[15:8] >> 8); div_l = (div_ext[3:0] << 12) +
+ div[7:0]

There is no need to explain the code, please add excerpts from the data sheet on how the divisors and baud rate are calculated, use mathematical language, and not programming in the comment.

Ok, will modify it. Which one is better?
/* baud rate = uartclk / ((16 * divisor + 1) + divisor_ext) */ Or
/* uartclk
* baud rate = -------------------------------------------
* (16 * divisor + 1) + divisor_ext
*/

Quoted 39 lines
Quoted 7 lines
drivers/tty/serial/sunplus-uart.c new file +7 -0
+        */
+       clk += baud >> 1;
+       div = clk / baud;
+       ext = div & 0x0F;
+       div = (div >> 4) - 1;
+       div_l = (div & 0xFF) | (ext << 12);
+       div_h = div >> 8;

...

drivers/tty/serial/sunplus-uart.c line 460

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static const char *sunplus_type(struct uart_port *port) {
+       struct sunplus_uart_port *sup = to_sunplus_uart(port);
+
+       return sup->port.type == PORT_SUNPLUS ? "sunplus_uart" : NULL;
+}

What do we achieve with this? Who and how will see this information? Under which circumstances the port type is not SUNPLUS?

...

drivers/tty/serial/sunplus-uart.c line 339

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static void sunplus_release_port(struct uart_port *port) { }
+
+static int sunplus_request_port(struct uart_port *port) {
+       return 0;
+}

Are these stubs mandatory?

...

drivers/tty/serial/sunplus-uart.c line 476

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static void sunplus_config_port(struct uart_port *port, int type) {
 
+       if (type & UART_CONFIG_TYPE) {
+               port->type = PORT_SUNPLUS;
+               sunplus_request_port(port);
+       }
Quoted 5 lines

if (!(type & ...))
return;

?

About these functions
sunplus_type / sunplus_release_port / sunplus_request_port / sunplus_config_port

Almost all uart driver have these function, but actually I don't know when/how to use it. I will study it.
If you have more information, please share it to me.

Quoted 10 lines
Quoted 1 line

+}

...

drivers/tty/serial/sunplus-uart.c line 339

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+static int sunplus_poll_init(struct uart_port *port) {
+       return 0;
+}

Why is this stub needed?

Will remove it.

Quoted 9 lines

...

drivers/tty/serial/sunplus-uart.c

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static inline void wait_for_xmitr(struct uart_port *port) {
+       unsigned int val;
+
+       readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,
+                                 (val & SUP_UART_LSR_TX), 1,

10000);

Quoted 5 lines

Error handling?
Or explain why it's not needed.

Quoted 1 line

+}

I refer to /drivers/tty/serial/owl-uart.c Will add error handling for it as below , is that OK?

static inline void wait_for_xmitr(struct uart_port *port) {
unsigned int val;
int ret;
/*Wait for FIFO is full or timeout */
ret = readl_poll_timeout_atomic(port->membase + SUP_UART_LSR, val,

                      (val & SUP_UART_LSR_TX), 1, 10000);
  If (ret == -ETIMEOUT) {
    dev_err(port->dev, "Timeout waiting while UART TX FULL);
    return;

}
}

Quoted 26 lines

...

drivers/tty/serial/sunplus-uart.c line 636

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+       sup->clk = devm_clk_get(&pdev->dev, NULL);
+       if (!IS_ERR(sup->clk)) {

Instead use devm_clk_get_optional().

drivers/tty/serial/sunplus-uart.c

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+               ret = clk_prepare_enable(sup->clk);
+               if (ret)
+                       return ret;
 
+               ret = devm_add_action_or_reset(&pdev->dev,
+                                              (void(*)(void

*))clk_disable_unprepare,

Quoted 1 line

+ sup->clk);

Look at the examples of how other drivers do this (that were submitted more or less recently).

drivers/tty/serial/sunplus-uart.c line 648

Quoted 6 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+               if (ret)
+                       return ret;
+       } else {
 
+               if (PTR_ERR(sup->clk) == -EPROBE_DEFER)
+                       return -EPROBE_DEFER;
Quoted 7 lines

Why?!

Quoted 1 line

+ return dev_err_probe(&pdev->dev, PTR_ERR(sup->clk),

"clk not found\n");

Quoted 1 line

+ }

...

In case of without deferred probe.
I refer to /drivers/tty/serial/meson_uart.c And I will modify as below, is that ok?
sup->clk = devm_clk_get_optional(&pdev->dev, NULL); if (IS_ERR(sup->clk))

     return dev_err_probe(&pdev->dev, PTR_ERR(sup->clk), "clk not found\n");

ret = clk_prepare_enable(sup->clk);
if (ret)

     return ret;
   ret = devm_add_action_or_reset(&pdev->dev, (void(*)(void*))clk_disable_unprepare,
         sup->clk);
   if (ret)
     return ret;

As your comment last patch v3 , you said "think of deferred". So I refer to /drivers/tty/serial/sccnxp.c
Maybe I just need to do it without deferred probe.

Quoted 6 lines

drivers/tty/serial/sunplus-uart.c line 660

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+       res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+       port->membase = devm_ioremap_resource(&pdev->dev, res);

We have an API that does both at once. Use it.

ok, will modify for it. devm_platform_get_and_ioremap_resource(pdev, 0, &res);

Quoted 4 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+       if (IS_ERR(port->membase))
+               return dev_err_probe(&pdev->dev,
+ PTR_ERR(port->membase), "membase not found\n");
Quoted 8 lines

...

drivers/tty/serial/sunplus-uart.c line 680

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+       ret = reset_control_deassert(sup->rstc);
+       if (ret)
+               return ret;

From here no reset assertion on error? Why?

I found I didn't add devm_add_action_or_reset( ) to run reset_control_assert( ) for it. I will add it as bleow.

ret = devm_add_action_or_reset(&pdev->dev, (void(*)(void*))reset_control_assert,

         sup->rstc);
   if (ret)
     return ret;
Quoted 9 lines

...

drivers/tty/serial/sunplus-uart.c

Quoted 4 lines
drivers/tty/serial/sunplus-uart.c new file +4 -0
+#ifdef CONFIG_SERIAL_SUNPLUS_CONSOLE
+       if (pdev->id == 0)
+               port->cons = &sunplus_uart_console;
+       sunplus_console_ports[sup->port.line] = sup; #endif

Actually why don't you use register_console() ?

I will think how to do it.

Quoted 8 lines

...

Quoted 5 lines
drivers/tty/serial/sunplus-uart.c new file +5 -0
+static struct platform_driver sunplus_uart_platform_driver = {
+       .probe          = sunplus_uart_probe,
+       .remove         = sunplus_uart_remove,
+       .driver = {
+               .name   = "sunplus_uart",
Quoted 9 lines
Quoted 1 line

+ .of_match_table = of_match_ptr(sp_uart_of_match),

How did you test this when OF=n and COMPILE_TEST=y?
Hint: Compiler will warn about unused variables (you need W=1).

Quoted 3 lines
drivers/tty/serial/sunplus-uart.c new file +3 -0
+               .pm     = &sunplus_uart_pm_ops,
+       }
+};

I have found many patch drop of_match_ptr( ) cause warning unused variable. Now I know what you means at last PATCH v3 comment.
I set OF=n and COMPILE_TEST=y, but other error come out first. I didn't confirm it further.
Will remove of_match_ptr( ).

Quoted 7 lines

...

Quoted 4 lines
drivers/tty/serial/sunplus-uart.c new file +4 -0
+static void __exit sunplus_uart_exit(void) {
+       platform_driver_unregister(&sunplus_uart_platform_driver);
+       uart_unregister_driver(&sunplus_uart_driver);
+}
Quoted 9 lines
Quoted 1 line

+

Dtop this blank line...

drivers/tty/serial/sunplus-uart.c line 812

Quoted 2 lines
drivers/tty/serial/sunplus-uart.c new file +2 -0
+module_init(sunplus_uart_init);
+module_exit(sunplus_uart_exit);

...and attach each of the calls to the implemented function.

Ok , will modify it.

Quoted 13 lines

...

Quoted 10 lines
drivers/tty/serial/sunplus-uart.c new file +10 -0
+static int __init
+sunplus_uart_early_setup(struct earlycon_device *dev, const char
+*opt) {
+       if (!(dev->port.membase || dev->port.iobase))
+               return -ENODEV;
+
+       dev->con->write = sunplus_uart_early_write;
+
+       return 0;
+}
Quoted 4 lines
Quoted 1 line

+

No blank line.

Use scripts/checkpatch.pl --strict -f drivers/tty/serial/sunplus-uart.c
It will show "CHECK: Please use a blank line after function/struct/union/enum declarations". That's why I confuse it and add a blank for it.
ok, will remove the blank.

Quoted 6 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+OF_EARLYCON_DECLARE(sunplus_uart, "sunplus,sp7021-uart",
+sunplus_uart_early_setup);

--
With Best Regards,
Andy Shevchenko