Add support for Tegra UART Trace Controller (UTC) client
v2 cover Add support for Tegra UART Trace Controller (UTC) client
The Tegra UTC (UART Trace Controller) is a hardware controller that allows multiple systems within the Tegra SoC to share a hardware UART interface. It supports up to 16 clients, with each client having its own interrupt and a FIFO buffer for both RX (receive) and TX (transmit), each capable of holding 128 characters.
The Tegra UTC uses 8-N-1 configuration and operates on a pre-configured baudrate, which is configured by the bootloader.
Kartik Rajput (2):
dt-bindings: serial: Add bindings for nvidia,tegra264-utc
serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
.../bindings/serial/nvidia,tegra264-utc.yaml | 73 ++ drivers/tty/serial/Kconfig | 23 + drivers/tty/serial/Makefile | 1 + drivers/tty/serial/tegra-utc.c | 622 ++++++++++++++++++ 4 files changed, 719 insertions(+) create mode 100644 Documentation/devicetree/bindings/serial/nvidia,tegra264-utc.yaml create mode 100644 drivers/tty/serial/tegra-utc.c
Signature · 1 line
2.43.0
PATCH v2 1/2 dt-bindings: serial: Add bindings for nvidia,tegra264-utc
The Tegra UTC (UART Trace Controller) allows multiple clients within the Tegra SoC to share a physical UART interface. It supports up to 16 clients. Each client operates as an independent UART endpoint with a dedicated interrupt and 128-character TX/RX FIFOs.
Add device tree binding documentation for the Tegra UTC client.
---
v1 -> v2:
* Removed current-speed as it might not be accurate as the Tegra UTC multiplex data from various clients.
* Use 'tx-threshold' and 'rx-threshold' properties defined in serial.yaml instead of 'nvidia,utc-fifo-threshold'.
* Add serial.yaml reference.
* Define minimum and maximum values for threshold.
* Rephrase the documentation to clarify that we are documenting the Tegra UTC clients and not the controller itself.
--- .../bindings/serial/nvidia,tegra264-utc.yaml | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 Documentation/devicetree/bindings/serial/nvidia,tegra264-utc.yaml
Documentation/devicetree/bindings/serial/nvidia,tegra264-utc.yaml new file +73 -0
@@ -0,0 +1,73 @@ +# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/serial/nvidia,tegra264-utc.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: NVIDIA Tegra UTC (UART Trace Controller) client + +maintainers: + - Kartik Rajput <[email protected]> + - Thierry Reding <[email protected]> + - Jonathan Hunter <[email protected]> + +description: + Represents a client interface of the Tegra UTC (UART Trace Controller). The + Tegra UTC allows multiple clients within the Tegra SoC to share a physical + UART interface. It supports up to 16 clients. Each client operates as an + independent UART endpoint with a dedicated interrupt and 128-character TX/RX + FIFOs. + + The Tegra UTC clients use 8-N-1 configuration and operates on a baudrate + configured by the bootloader at the controller level. + +allOf: + - $ref: serial.yaml# + +properties: + compatible: + const: nvidia,tegra264-utc + + reg: + items: + - description: TX region. + - description: RX region. + + reg-names: + items: + - const: tx + - const: rx + + interrupts: + maxItems: 1 + + tx-threshold: + minimum: 1 + maximum: 128 + + rx-threshold: + minimum: 1 + maximum: 128 + +required: + - compatible + - reg + - reg-names + - interrupts + - tx-threshold + - rx-threshold + +additionalProperties: false + +examples: + - | + #include <dt-bindings/interrupt-controller/arm-gic.h> + + tegra_utc: serial@c4e0000 { + compatible = "nvidia,tegra264-utc"; + reg = <0xc4e0000 0x8000>, <0xc4e8000 0x8000>; + reg-names = "tx", "rx"; + interrupts = <GIC_SPI 514 IRQ_TYPE_LEVEL_HIGH>; + tx-threshold = <4>; + rx-threshold = <4>; + };
Signature · 1 line
2.43.0
PATCH v2 2/2 serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
In reply to Kartik RajputThe Tegra264 SoC supports the UTC (UART Trace Controller), which allows multiple firmware clients (up to 16) to share a single physical UART. Each client is provided with its own interrupt and has access to a 128-character wide FIFO for both transmit (TX) and receive (RX) operations.
Add tegra-utc driver to support Tegra UART Trace Controller (UTC) client.
---
v1 -> v2:
* Use dev_err_probe() in tegra_utc_probe().
* Use uart_read_port_properties() instead of manually parsing the port line.
* Remove duplicate error prints if platform_get_irq() fails.
* In tegra_utc_of_match, remove , after terminator line.
* Remove current-speed, as it is not always accurate.
--- drivers/tty/serial/Kconfig | 23 ++ drivers/tty/serial/Makefile | 1 + drivers/tty/serial/tegra-utc.c | 622 +++++++++++++++++++++++++++++++++ 3 files changed, 646 insertions(+) create mode 100644 drivers/tty/serial/tegra-utc.c
drivers/tty/serial/Kconfig +23 -0
@@ -306,6 +306,29 @@ config SERIAL_TEGRA_TCU_CONSOLE If unsure, say Y. +config SERIAL_TEGRA_UTC + tristate "NVIDIA Tegra UART Trace Controller" + depends on ARCH_TEGRA || COMPILE_TEST + select SERIAL_CORE + help + Support for Tegra UTC (UART Trace controller) client serial port. + + UTC is a HW based serial port that allows multiplexing multiple data + streams of up to 16 UTC clients into a single hardware serial port. + +config SERIAL_TEGRA_UTC_CONSOLE + bool "Support for console on a Tegra UTC serial port" + depends on SERIAL_TEGRA_UTC + select SERIAL_CORE_CONSOLE + default SERIAL_TEGRA_UTC + help + If you say Y here, it will be possible to use a Tegra UTC client as + the system console (the system console is the device which receives + all kernel messages and warnings and which allows logins in single + user mode). + + If unsure, say Y. + config SERIAL_MAX3100 tristate "MAX3100/3110/3111/3222 support" depends on SPI
drivers/tty/serial/Makefile +1 -0
@@ -86,6 +86,7 @@ obj-$(CONFIG_SERIAL_STM32) += stm32-usart.o obj-$(CONFIG_SERIAL_SUNPLUS) += sunplus-uart.o obj-$(CONFIG_SERIAL_TEGRA) += serial-tegra.o obj-$(CONFIG_SERIAL_TEGRA_TCU) += tegra-tcu.o +obj-$(CONFIG_SERIAL_TEGRA_UTC) += tegra-utc.o obj-$(CONFIG_SERIAL_TIMBERDALE) += timbuart.o obj-$(CONFIG_SERIAL_TXX9) += serial_txx9.o obj-$(CONFIG_SERIAL_UARTLITE) += uartlite.o
drivers/tty/serial/tegra-utc.c new file +622 -0
622 lines not on this page · raw message
Signature · 1 line
2.43.0
Jiri Slaby
maintainerRe: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
Quoted 14 lines · Kartik Rajput
The Tegra264 SoC supports the UTC (UART Trace Controller), which allows multiple firmware clients (up to 16) to share a single physical UART. Each client is provided with its own interrupt and has access to a 128-character wide FIFO for both transmit (TX) and receive (RX) operations.
Add tegra-utc driver to support Tegra UART Trace Controller (UTC) client.
Signed-off-by Kartik Rajput <[email protected]>
drivers/tty/serial/tegra-utc.c line 8
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +8
-0@@ -0,0 +1,622 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +/* +* NVIDIA Tegra UTC (UART Trace Controller) driver. +*/ + +#include <linux/console.h> +#include <linux/kthread.h>
Do you really use kthread somewhere?
drivers/tty/serial/tegra-utc.c line 16
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +8
-0+#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/platform_device.h> +#include <linux/serial.h> +#include <linux/serial_core.h> +#include <linux/slab.h> +#include <linux/string.h>
What's the reason for string.h?
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+#include <linux/tty.h> +#include <linux/tty_flip.h> + +#define TEGRA_UTC_ENABLE 0x0 +#define TEGRA_UTC_ENABLE_CLIENT_ENABLE BIT(0) + +#define TEGRA_UTC_FIFO_THRESHOLD 0x8 + +#define TEGRA_UTC_COMMAND 0xc +#define TEGRA_UTC_COMMAND_FLUSH BIT(1)
drivers/tty/serial/tegra-utc.c line 36
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+#define TEGRA_UTC_COMMAND_RESET BIT(0) + +#define TEGRA_UTC_DATA 0x20 + +#define TEGRA_UTC_FIFO_STATUS 0x100 +#define TEGRA_UTC_FIFO_TIMEOUT BIT(4) +#define TEGRA_UTC_FIFO_OVERFLOW BIT(3) +#define TEGRA_UTC_FIFO_REQ BIT(2) +#define TEGRA_UTC_FIFO_FULL BIT(1) +#define TEGRA_UTC_FIFO_EMPTY BIT(0)
It looks a bit weird to order bits from MSB to LSB.
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+#define TEGRA_UTC_FIFO_OCCUPANCY 0x104 + +#define TEGRA_UTC_INTR_STATUS 0x108 +#define TEGRA_UTC_INTR_SET 0x10c +#define TEGRA_UTC_INTR_MASK 0x110 +#define TEGRA_UTC_INTR_CLEAR 0x114
drivers/tty/serial/tegra-utc.c line 53
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+#define TEGRA_UTC_INTR_TIMEOUT BIT(4) +#define TEGRA_UTC_INTR_OVERFLOW BIT(3) +#define TEGRA_UTC_INTR_REQ BIT(2) +#define TEGRA_UTC_INTR_FULL BIT(1) +#define TEGRA_UTC_INTR_EMPTY BIT(0) + +#define UART_NR 16 + +struct tegra_utc_soc { +unsigned int fifosize;
What is this struct good for, given you use a single value?
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+struct tegra_utc_port { +const struct tegra_utc_soc *soc; +#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) +struct console console; +#endif +struct uart_port port;
drivers/tty/serial/tegra-utc.c line 71
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+ +void __iomem *rx_base; +void __iomem *tx_base; + +u32 tx_irqmask; +u32 rx_irqmask; + +u32 tx_threshold; +u32 rx_threshold; +int irq;
Why can't uart_port::irq be used instead?
drivers/tty/serial/tegra-utc.c line 170
Quoted 11 lines
drivers/tty/serial/tegra-utc.c new file +11
-0+static bool tegra_utc_tx_char (struct tegra_utc_port *tup,u8 c) +{ +if (tegra_utc_tx_readl (tup,TEGRA_UTC_FIFO_STATUS ) &TEGRA_UTC_FIFO_FULL ) +return false; + +tegra_utc_tx_writel (tup, c,TEGRA_UTC_DATA ); + +return true; +} + +static bool tegra_utc_tx_chars (struct tegra_utc_port *tup)
To the least, you do not account TX stats. Why not to use uart_port_tx()?
Quoted 29 lines
drivers/tty/serial/tegra-utc.c new file +29
-0+{ +struct tty_port *tport = &tup->port.state->port; +u8 c; + +if (tup->port.x_char) { +if (!tegra_utc_tx_char (tup, tup->port.x_char)) +return true; + + tup->port.x_char =0 ; + } + +if (kfifo_is_empty (&tport->xmit_fifo) ||uart_tx_stopped (&tup->port)) { +tegra_utc_stop_tx (&tup->port); +return false; + } + +while (kfifo_peek (&tport->xmit_fifo, &c)) { +if (!tegra_utc_tx_char (tup, c)) +break ; + +kfifo_skip (&tport->xmit_fifo); + } + +if (kfifo_len (&tport->xmit_fifo) <WAKEUP_CHARS ) +uart_write_wakeup (&tup->port); + +if (kfifo_is_empty (&tport->xmit_fifo)) { +tegra_utc_stop_tx (&tup->port); +return false;
drivers/tty/serial/tegra-utc.c line 209
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+ } + +return true; +} + +static void tegra_utc_rx_chars (struct tegra_utc_port *tup) +{ +struct tty_port *port = &tup->port.state->port; +unsigned int max_chars =256 ; +unsigned int flag;
Useless variable.
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +8
-0+u32 status; +int sysrq; +u32 ch; + +while (--max_chars) { + status =tegra_utc_rx_readl (tup,TEGRA_UTC_FIFO_STATUS ); +if (status &TEGRA_UTC_FIFO_EMPTY ) +break ;
drivers/tty/serial/tegra-utc.c line 227
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+ + ch =tegra_utc_rx_readl (tup,TEGRA_UTC_DATA ); + flag =TTY_NORMAL ; + tup->port.icount.rx++; + +if (status &TEGRA_UTC_FIFO_OVERFLOW ) + tup->port.icount.overrun++; + +uart_port_unlock (&tup->port); + sysrq =uart_handle_sysrq_char (&tup->port, ch &0xff );
No need for "& 0xff".
drivers/tty/serial/tegra-utc.c line 231
Quoted 4 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+uart_port_lock (&tup->port); + +if (!sysrq) +tty_insert_flip_char (port, ch, flag);
You do not mask 'ch' here either. Both functions take 'u8'.
Quoted 24 lines
drivers/tty/serial/tegra-utc.c new file +24
-0+ } + +tty_flip_buffer_push (port); +} + +static irqreturn_t tegra_utc_isr (int irq,void *dev_id) +{ +struct tegra_utc_port *tup = dev_id; +unsigned long flags; +u32 status; + +uart_port_lock_irqsave (&tup->port, &flags); + +/* Process RX_REQ and RX_TIMEOUT interrupts. */ +do { + status =tegra_utc_rx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->rx_irqmask; +if (status) { +tegra_utc_rx_writel (tup, tup->rx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_rx_chars (tup); + } + }while (status); + +/* Process TX_REQ interrupt. */ + do {
drivers/tty/serial/tegra-utc.c line 265
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+ status =tegra_utc_tx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->tx_irqmask; +if (status) { +tegra_utc_tx_writel (tup, tup->tx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_tx_chars (tup); + } + }while (status); + +uart_port_unlock_irqrestore (&tup->port, flags); + +return IRQ_HANDLED ;
You do not let the irq subsystem to kill this IRQ if you do not handle it above (in case HW gets mad, triggers IRQ, but does not set rx/tx flags). That is, return IRQ_HANDLED only when you really handled it (some status above was nonzero).
drivers/tty/serial/tegra-utc.c line 314
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +9
-0+} +static int tegra_utc_startup (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); +int ret; + +tegra_utc_hw_init (tup); + + ret =request_irq (tup->irq, tegra_utc_isr,0 ,dev_name (port->dev), tup);
Just asking: so it can never be shared, right?
drivers/tty/serial/tegra-utc.c line 327
Quoted 13 lines
drivers/tty/serial/tegra-utc.c new file +13
-0+if (ret <0 ) { +dev_err (port->dev,"failed to register interrupt handler\n" ); +return ret; + } + +return 0 ; +} + +static void tegra_utc_shutdown (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); + +tegra_utc_rx_writel (tup,0x0 ,TEGRA_UTC_ENABLE );
Writes cannot be posted on this bus, right?
Quoted 2 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+free_irq (tup->irq, tup); +}
...
drivers/tty/serial/tegra-utc.c line 355
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+static int tegra_utc_get_poll_char (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); + +while (tegra_utc_rx_readl (tup,TEGRA_UTC_FIFO_STATUS ) &TEGRA_UTC_FIFO_EMPTY ) +cpu_relax ();
Hmm, there should be a timeout. Can't you use read_poll_timeout_atomic()?
drivers/tty/serial/tegra-utc.c line 365
Quoted 9 lines
drivers/tty/serial/tegra-utc.c new file +9
-0+return tegra_utc_rx_readl (tup,TEGRA_UTC_DATA ); +} + +static void tegra_utc_put_poll_char (struct uart_port *port,unsigned char ch) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); + +while (tegra_utc_tx_readl (tup,TEGRA_UTC_FIFO_STATUS ) &TEGRA_UTC_FIFO_FULL ) +cpu_relax ();
Detto.
Quoted 56 lines
drivers/tty/serial/tegra-utc.c new file +54
-0+tegra_utc_tx_writel (tup, ch,TEGRA_UTC_DATA ); +} + +#endif +static void tegra_utc_console_write (struct console *cons,const char *s,unsigned int count) +{ + struct tegra_utc_port *tup = container_of(cons, struct tegra_utc_port, console); +unsigned long flags; +int locked =1 ; + +if (tup->port.sysrq || oops_in_progress) + locked =uart_port_trylock_irqsave (&tup->port, &flags); +else +uart_port_lock_irqsave (&tup->port, &flags); + +while (count) { +u32 burst_size = tup->soc->fifosize; + + burst_size -=tegra_utc_tx_readl (tup,TEGRA_UTC_FIFO_OCCUPANCY ); +if (count < burst_size) + burst_size = count; + +uart_console_write (&tup->port, s, burst_size, tegra_utc_console_putchar); + + count -= burst_size; + s += burst_size; + }; + +if (locked) +uart_port_unlock_irqrestore (&tup->port, flags); +} + +static int tegra_utc_console_setup (struct console *cons,char *options) +{ + struct tegra_utc_port *tup = container_of(cons, struct tegra_utc_port, console); + +tegra_utc_init_tx (tup); + +return 0 ; +} +#endif + +static struct uart_driver tegra_utc_driver = { + .driver_name ="tegra-utc" , + .dev_name ="ttyUTC" , + .nr =UART_NR +}; + +static void tegra_utc_setup_port (struct device *dev,struct tegra_utc_port *tup) +{ + tup->port.dev = dev; + tup->port.fifosize = tup->soc->fifosize; + tup->port.flags =UPF_BOOT_AUTOCONF ; + tup->port.iotype =UPIO_MEM ;
drivers/tty/serial/tegra-utc.c line 508
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+ tup->port.ops = &tegra_utc_uart_ops; + tup->port.type =PORT_TEGRA_TCU ; + tup->port.private_data = tup; + +#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) +strscpy (tup->console.name,"ttyUTC" ,sizeof (tup->console.name)); + tup->console.write = tegra_utc_console_write; + tup->console.device = uart_console_device; + tup->console.setup = tegra_utc_console_setup; + tup->console.flags =CON_PRINTBUFFER |CON_CONSDEV |CON_ANYTIME ;
New code shall be CON_NBCON compatible. You should implement ::write_atomic/thread et al. instead of bare ::write.
drivers/tty/serial/tegra-utc.c line 572
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +9
-0+ tup->console.data = &tegra_utc_driver; +#endif + +uart_read_port_properties (&tup->port); +} +static void tegra_utc_remove (struct platform_device *pdev) +{ +struct tegra_utc_port *tup =platform_get_drvdata (pdev); +
No unregister_console()?
drivers/tty/serial/tegra-utc.c line 574
Quoted 2 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+uart_remove_one_port (&tegra_utc_driver, &tup->port); +}
thanks,
Signature · 2 lines
js suse labs
Re: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
Quoted 1 line · Jiri Slaby
On 11. 02. 25, 7:19, Kartik Rajput wrote:
...
Quoted 26 lines
Quoted 26 lines
drivers/tty/serial/tegra-utc.c new file +26
-0+static irqreturn_t tegra_utc_isr (int irq,void *dev_id) +{ +struct tegra_utc_port *tup = dev_id; +unsigned long flags; +u32 status; + +uart_port_lock_irqsave (&tup->port, &flags); + +/* Process RX_REQ and RX_TIMEOUT interrupts. */ +do { + status =tegra_utc_rx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->rx_irqmask; +if (status) { +tegra_utc_rx_writel (tup, tup->rx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_rx_chars (tup); + } + }while (status); + +/* Process TX_REQ interrupt. */ +do { + status =tegra_utc_tx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->tx_irqmask; +if (status) { +tegra_utc_tx_writel (tup, tup->tx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_tx_chars (tup); + } + }while (status); +
Quoted 10 lines
drivers/tty/serial/tegra-utc.c line 265
Quoted 3 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+uart_port_unlock_irqrestore (&tup->port, flags); + +return IRQ_HANDLED ;You do not let the irq subsystem to kill this IRQ if you do not handle it above (in case HW gets mad, triggers IRQ, but does not set rx/tx flags). That is, return
IRQ_HANDLEDonly when you really handled it (some status above was nonzero).Quoted 1 line
+}
I am also wondering why _irqsave / _irqrestore? Don't you have interrupts already being disabled at this point?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
In reply to Jiri SlabyThanks for reviewing the patch Jiri!
Quoted 17 lines · Jiri Slaby
External email: Use caution opening links or attachments
Quoted 12 lines · Kartik Rajput
The Tegra264 SoC supports the UTC (UART Trace Controller), which allows
multiple firmware clients (up to 16) to share a single physical UART.
Each client is provided with its own interrupt and has access to a 128-character wide FIFO for both transmit (TX) and receive (RX) operations.Add tegra-utc driver to support Tegra UART Trace Controller (UTC) client.
Signed-off-by Kartik Rajput <[email protected]>
Quoted 14 lines
drivers/tty/serial/tegra-utc.c line 8
Quoted 12 lines
drivers/tty/serial/tegra-utc.c new file +2
-0@@ -0,0 +1,622 @@ +// SPDX-License-Identifier: GPL-2.0-only +// SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
drivers/tty/serial/tegra-utc.c new file +6
-0+/* +* NVIDIA Tegra UTC (UART Trace Controller) driver. +*/ + +#include <linux/console.h> +#include <linux/kthread.h> Do you really use kthread somewhere?
Not needed, I will remove this.
Quoted 12 lines
drivers/tty/serial/tegra-utc.c line 16
Quoted 9 lines
drivers/tty/serial/tegra-utc.c new file +8
-0+#include <linux/module.h> +#include <linux/of.h> +#include <linux/of_device.h> +#include <linux/platform_device.h> +#include <linux/serial.h> +#include <linux/serial_core.h> +#include <linux/slab.h> +#include <linux/string.h> What's the reason for
string.h?
Not needed, I will remove this.
Quoted 14 lines
Quoted 14 lines
drivers/tty/serial/tegra-utc.c new file +14
-0+#include <linux/tty.h> +#include <linux/tty_flip.h> + +#define TEGRA_UTC_ENABLE 0x0 +#define TEGRA_UTC_ENABLE_CLIENT_ENABLE BIT(0) + +#define TEGRA_UTC_FIFO_THRESHOLD 0x8 + +#define TEGRA_UTC_COMMAND 0xc +#define TEGRA_UTC_COMMAND_FLUSH BIT(1) +#define TEGRA_UTC_COMMAND_RESET BIT(0) + +#define TEGRA_UTC_DATA 0x20 +
Quoted 8 lines
drivers/tty/serial/tegra-utc.c line 36
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+#define TEGRA_UTC_FIFO_STATUS 0x100 +#define TEGRA_UTC_FIFO_TIMEOUT BIT(4) +#define TEGRA_UTC_FIFO_OVERFLOW BIT(3) +#define TEGRA_UTC_FIFO_REQ BIT(2) +#define TEGRA_UTC_FIFO_FULL BIT(1) +#define TEGRA_UTC_FIFO_EMPTY BIT(0)It looks a bit weird to order bits from MSB to LSB.
I will change the ordering from LSB to MSB.
Quoted 11 lines
Quoted 11 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+#define TEGRA_UTC_FIFO_OCCUPANCY 0x104 + +#define TEGRA_UTC_INTR_STATUS 0x108 +#define TEGRA_UTC_INTR_SET 0x10c +#define TEGRA_UTC_INTR_MASK 0x110 +#define TEGRA_UTC_INTR_CLEAR 0x114 +#define TEGRA_UTC_INTR_TIMEOUT BIT(4) +#define TEGRA_UTC_INTR_OVERFLOW BIT(3) +#define TEGRA_UTC_INTR_REQ BIT(2) +#define TEGRA_UTC_INTR_FULL BIT(1)
Quoted 8 lines
drivers/tty/serial/tegra-utc.c line 53
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+#define TEGRA_UTC_INTR_EMPTY BIT(0) + +#define UART_NR 16 + +struct tegra_utc_soc { + unsigned int fifosize;What is this struct good for, given you use a single value?
The idea to add this struct was to allow for a simpler future expansion of the SoC data. But I agree, at this time, this struct can be removed.
I will update the patch and move fifosize to struct tegra_utc_port instead.
Quoted 11 lines
Quoted 11 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+struct tegra_utc_port { + const struct tegra_utc_soc *soc; +#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) + struct console console; +#endif + struct uart_port port; + + void __iomem *rx_base; + void __iomem *tx_base; +
Quoted 9 lines
Quoted 6 lines
+ u32
tx_irqmask;
+ u32rx_irqmask;
+
+ u32tx_threshold;
+ u32rx_threshold;
+ int irq;Why can't
uart_port::irqbe used instead?
Ack, this is redundant, uart_port::irq can be used instead of this. I will remove this.
Quoted 7 lines
Quoted 7 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+static bool tegra_utc_tx_char (struct tegra_utc_port *tup,u8 c) +{ + if (tegra_utc_tx_readl(tup, TEGRA_UTC_FIFO_STATUS) &
TEGRA_UTC_FIFO_FULL)
+ return false;
+
+tegra_utc_tx_writel(tup, c, TEGRA_UTC_DATA);
Quoted 9 lines
drivers/tty/serial/tegra-utc.c line 170
Quoted 5 lines
drivers/tty/serial/tegra-utc.c new file +5
-0+ + return true; +} + +static bool tegra_utc_tx_chars (struct tegra_utc_port *tup)To the least, you do not account TX stats. Why not to use
uart_port_tx()?
Ack, I will update the patch to use uart_port_tx() instead.
Quoted 34 lines
Quoted 34 lines
drivers/tty/serial/tegra-utc.c new file +12
-0+{ + struct tty_port *tport = &tup->port.state->port; + u8 c; + + if (tup->port.x_char) { + if (!tegra_utc_tx_char(tup, tup->port.x_char)) + return true; + + tup->port.x_char = 0; + } + + if (kfifo_is_empty(&tport->xmit_fifo) ||
uart_tx_stopped(&tup->port)) {
+tegra_utc_stop_tx(&tup->port);
+ return false;
+ }drivers/tty/serial/tegra-utc.c new file +18
-0+ + while (kfifo_peek(&tport->xmit_fifo, &c)) { + if (!tegra_utc_tx_char(tup, c)) + break; + + kfifo_skip(&tport->xmit_fifo); + } + + if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS) + uart_write_wakeup(&tup->port); + + if (kfifo_is_empty(&tport->xmit_fifo)) { + tegra_utc_stop_tx(&tup->port); + return false; + } + + return true; +}
Quoted 9 lines
drivers/tty/serial/tegra-utc.c line 209
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+ +static void tegra_utc_rx_chars (struct tegra_utc_port *tup) +{ + struct tty_port *port = &tup->port.state->port; + unsigned int max_chars = 256; + unsigned int flag;Useless variable.
Ack.
Quoted 14 lines
Quoted 14 lines
+ u32 status;
+ int sysrq;
+ u32 ch;
+
+ while (--max_chars) {
+ status = tegra_utc_rx_readl(tup,TEGRA_UTC_FIFO_STATUS);
+ if (status &TEGRA_UTC_FIFO_EMPTY)
+ break;drivers/tty/serial/tegra-utc.c new file +5
-0+ + ch = tegra_utc_rx_readl(tup, TEGRA_UTC_DATA); + flag = TTY_NORMAL; + tup->port.icount.rx++; +
Quoted 8 lines
Quoted 6 lines
+ if (status &
TEGRA_UTC_FIFO_OVERFLOW) + tup->port.icount.overrun++;
+
+uart_port_unlock(&tup->port);
+ sysrq =uart_handle_sysrq_char(&tup->port, ch & 0xff);No need for "&
0xff".
Ack.
Quoted 8 lines
Quoted 5 lines
+
uart_port_lock(&tup->port);
+
+ if (!sysrq)
+tty_insert_flip_char(port, ch, flag);You do not mask 'ch' here either. Both functions take 'u8'.
Ack.
Quoted 36 lines
Quoted 36 lines
+ }
drivers/tty/serial/tegra-utc.c new file +15
-0+ + tty_flip_buffer_push(port); +} + +static irqreturn_t tegra_utc_isr(int irq, void *dev_id) +{ + struct tegra_utc_port *tup = dev_id; + unsigned long flags; + u32 status; + + uart_port_lock_irqsave(&tup->port, &flags); + + /* Process RX_REQ and RX_TIMEOUT interrupts. */ + do { + status = tegra_utc_rx_readl(tup,
TEGRA_UTC_INTR_STATUS) &tup->rx_irqmask;
+ if (status) {
+tegra_utc_rx_writel(tup, tup->rx_irqmask, TEGRA_UTC_INTR_CLEAR);
+tegra_utc_rx_chars(tup);
+ }
+ } while (status);
+
+ /* ProcessTX_REQinterrupt. */
+ do {
+ status =tegra_utc_tx_readl(tup, TEGRA_UTC_INTR_STATUS)&tup->tx_irqmask;
+ if (status) {
+tegra_utc_tx_writel(tup, tup->tx_irqmask, TEGRA_UTC_INTR_CLEAR);
+tegra_utc_tx_chars(tup);
+ }
+ } while (status);
+
+uart_port_unlock_irqrestore(&tup->port, flags);
Quoted 9 lines
Quoted 2 lines
+
+ returnIRQ_HANDLED;You do not let the irq subsystem to kill this IRQ if you do not handle
it above (in case HW gets mad, triggers IRQ, but does not set rx/tx flags). That is, returnIRQ_HANDLEDonly when you really handled it (some status above was nonzero).
Makes sense, I will update this logic.
Quoted 11 lines
Quoted 11 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+} +static int tegra_utc_startup (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct
tegra_utc_port, port);
+ int ret;drivers/tty/serial/tegra-utc.c new file +4
-0+ + tegra_utc_hw_init(tup); + + ret = request_irq(tup->irq, tegra_utc_isr, 0, dev_name(port-
Quoted 3 lines
Quoted 1 line
Quoted 1 line
dev), tup);
Just asking: so it can never be shared, right?
Correct, this cannot be shared.
Quoted 10 lines
Quoted 10 lines
+ if (ret < 0) {
+dev_err(port->dev, "failed to register interrupt handler\n");
+ return ret;
+ }drivers/tty/serial/tegra-utc.c new file +4
-0+ + return 0; +} +
Quoted 9 lines
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+static void tegra_utc_shutdown (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct
tegra_utc_port, port);
+
+tegra_utc_rx_writel(tup, 0x0, TEGRA_UTC_ENABLE);Writes cannot be posted on this bus, right?
Writes are allowed for RX client, only the writes to the RX client data register are not allowed.
Quoted 14 lines
Quoted 2 lines
+
free_irq(tup->irq, tup);
+}...
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+static int tegra_utc_get_poll_char (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct
tegra_utc_port, port);
+
+ while (tegra_utc_rx_readl(tup, TEGRA_UTC_FIFO_STATUS)&TEGRA_UTC_FIFO_EMPTY)
+cpu_relax();Hmm, there should be a timeout. Can't you use
read_poll_timeout_atomic()?
Ack.
Quoted 15 lines
Quoted 5 lines
+ return
tegra_utc_rx_readl(tup, TEGRA_UTC_DATA);drivers/tty/serial/tegra-utc.c new file +3
-0+} + +static void tegra_utc_put_poll_char(struct uart_port *port,Quoted 8 lines
unsigned char ch)
+{
+ structtegra_utc_port*tup =container_of(port, struct tegra_utc_port, port);
+
+ while (tegra_utc_tx_readl(tup, TEGRA_UTC_FIFO_STATUS)&TEGRA_UTC_FIFO_FULL)
+cpu_relax();Detto.
Ack.
Quoted 72 lines
Quoted 72 lines
+
tegra_utc_tx_writel(tup, ch, TEGRA_UTC_DATA);drivers/tty/serial/tegra-utc.c new file +4
-0+} + +#endif +static void tegra_utc_console_write(struct console *cons, constchar *s, unsigned int count)
+{
+ structtegra_utc_port*tup =container_of(cons, struct tegra_utc_port, console);
+ unsigned long flags;
+ int locked = 1;
+
+ if (tup->port.sysrq||oops_in_progress)
+ locked =uart_port_trylock_irqsave(&tup->port, &flags);
+ else
+uart_port_lock_irqsave(&tup->port, &flags);drivers/tty/serial/tegra-utc.c new file +5
-0+ + while (count) { + u32 burst_size = tup->soc->fifosize; + + burst_size -= tegra_utc_tx_readl(tup,
TEGRA_UTC_FIFO_OCCUPANCY);
+ if (count <burst_size)
+burst_size= count;
+
+uart_console_write(&tup->port, s, burst_size, tegra_utc_console_putchar);drivers/tty/serial/tegra-utc.c new file +10
-0+ + count -= burst_size; + s += burst_size; + }; + + if (locked) + uart_port_unlock_irqrestore(&tup->port, flags); +} + +static int tegra_utc_console_setup(struct console *cons, char*options)
+{
+ structtegra_utc_port*tup =container_of(cons, struct tegra_utc_port, console);drivers/tty/serial/tegra-utc.c new file +14
-0+ + tegra_utc_init_tx(tup); + + return 0; +} +#endif + +static struct uart_driver tegra_utc_driver = { + .driver_name = "tegra-utc", + .dev_name = "ttyUTC", + .nr = UART_NR +}; + +static void tegra_utc_setup_port(struct device *dev, struct
tegra_utc_port*tup)drivers/tty/serial/tegra-utc.c new file +11
-0+{ + tup->port.dev = dev; + tup->port.fifosize = tup->soc->fifosize; + tup->port.flags = UPF_BOOT_AUTOCONF; + tup->port.iotype = UPIO_MEM; + tup->port.ops = &tegra_utc_uart_ops; + tup->port.type = PORT_TEGRA_TCU; + tup->port.private_data = tup; + +#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) + strscpy(tup->console.name, "ttyUTC", sizeof(tup-Quoted 1 line
console.name));
Quoted 9 lines
Quoted 5 lines
+ tup->console.write =
tegra_utc_console_write; + tup->console.device =uart_console_device; + tup->console.setup =tegra_utc_console_setup; + tup->console.flags =CON_PRINTBUFFER|CON_CONSDEV|CON_ANYTIME;New code shall be
CON_NBCONcompatible. You should implement ::write_atomic/thread et al. instead of bare ::write.
Ack.
Quoted 6 lines
Quoted 5 lines
+ tup->console.data =
&tegra_utc_driver;drivers/tty/serial/tegra-utc.c new file +4
-0+#endif + + uart_read_port_properties(&tup->port); +}
Quoted 7 lines
drivers/tty/serial/tegra-utc.c line 572
Quoted 4 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+static void tegra_utc_remove (struct platform_device *pdev) +{ + struct tegra_utc_port *tup = platform_get_drvdata(pdev); +No
unregister_console()?
Ack, I will add this in the next patch.
Quoted 7 lines
Quoted 2 lines
+
uart_remove_one_port(&tegra_utc_driver, &tup->port); +}thanks,
--
js
suse labs
Thanks,
Kartik
Re: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
In reply to Kartik RajputQuoted 8 lines · Kartik Rajput
The Tegra264 SoC supports the UTC (UART Trace Controller), which allows multiple firmware clients (up to 16) to share a single physical UART. Each client is provided with its own interrupt and has access to a 128-character wide FIFO for both transmit (TX) and receive (RX) operations.
Add tegra-utc driver to support Tegra UART Trace Controller (UTC) client.
...
+ bits.h
Quoted 1 line
+#include <
linux/console.h>
+ container_of.h
+ device.h
+ err.h
+ io.h
Quoted 1 line
+#include <
linux/kthread.h>
+ kfifo.h
Quoted 1 line
+#include <
linux/module.h>
+ mod_devicetable.h
drivers/tty/serial/tegra-utc.c line 11
Quoted 3 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+#include <linux/of.h> +#include <linux/of_device.h>
Use property.h (see also below).
drivers/tty/serial/tegra-utc.c line 18
Quoted 7 lines
drivers/tty/serial/tegra-utc.c new file +7
-0+#include <linux/platform_device.h> +#include <linux/serial.h> +#include <linux/serial_core.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/tty.h> +#include <linux/tty_flip.h>
+ types.h
...
drivers/tty/serial/tegra-utc.c line 214
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+static void tegra_utc_rx_chars (struct tegra_utc_port *tup) +{ +struct tty_port *port = &tup->port.state->port; +unsigned int max_chars =256 ; +unsigned int flag; +u32 status; +int sysrq; +u32 ch; + +while (--max_chars) {
Wouldn't while (max_chars--) { suffice?
Quoted 21 lines
drivers/tty/serial/tegra-utc.c new file +21
-0+ status =tegra_utc_rx_readl (tup,TEGRA_UTC_FIFO_STATUS ); +if (status &TEGRA_UTC_FIFO_EMPTY ) +break ; + + ch =tegra_utc_rx_readl (tup,TEGRA_UTC_DATA ); + flag =TTY_NORMAL ; + tup->port.icount.rx++; + +if (status &TEGRA_UTC_FIFO_OVERFLOW ) + tup->port.icount.overrun++; + +uart_port_unlock (&tup->port); + sysrq =uart_handle_sysrq_char (&tup->port, ch &0xff ); +uart_port_lock (&tup->port); + +if (!sysrq) +tty_insert_flip_char (port, ch, flag); + } + +tty_flip_buffer_push (port); +}
...
drivers/tty/serial/tegra-utc.c line 243
Quoted 7 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+static irqreturn_t tegra_utc_isr (int irq,void *dev_id) +{ +struct tegra_utc_port *tup = dev_id; +unsigned long flags; +u32 status; +uart_port_lock_irqsave (&tup->port, &flags);
As said previously, why _irqsave?
Quoted 22 lines
drivers/tty/serial/tegra-utc.c new file +22
-0+/* Process RX_REQ and RX_TIMEOUT interrupts. */ +do { + status =tegra_utc_rx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->rx_irqmask; +if (status) { +tegra_utc_rx_writel (tup, tup->rx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_rx_chars (tup); + } + }while (status); + +/* Process TX_REQ interrupt. */ +do { + status =tegra_utc_tx_readl (tup,TEGRA_UTC_INTR_STATUS ) & tup->tx_irqmask; +if (status) { +tegra_utc_tx_writel (tup, tup->tx_irqmask,TEGRA_UTC_INTR_CLEAR ); +tegra_utc_tx_chars (tup); + } + }while (status); + +uart_port_unlock_irqrestore (&tup->port, flags); + +return IRQ_HANDLED ; +}
...
drivers/tty/serial/tegra-utc.c line 316
Quoted 9 lines
drivers/tty/serial/tegra-utc.c new file +9
-0+{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); +int ret; + +tegra_utc_hw_init (tup); + + ret =request_irq (tup->irq, tegra_utc_isr,0 ,dev_name (port->dev), tup); +if (ret <0 ) { +dev_err (port->dev,"failed to register interrupt handler\n" );
Instead of these LoCs...
drivers/tty/serial/tegra-utc.c line 320
Quoted 4 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+return ret; + } + +return 0 ;
return ret;
Quoted 1 line
+}
...
drivers/tty/serial/tegra-utc.c line 355
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+static int tegra_utc_get_poll_char (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); + +while (tegra_utc_rx_readl (tup,TEGRA_UTC_FIFO_STATUS ) &TEGRA_UTC_FIFO_EMPTY ) +cpu_relax ();
No way out? HW might get stuck...
Quoted 2 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+return tegra_utc_rx_readl (tup,TEGRA_UTC_DATA ); +}
...
drivers/tty/serial/tegra-utc.c line 365
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +6
-0+static void tegra_utc_put_poll_char (struct uart_port *port,unsigned char ch) +{ + struct tegra_utc_port *tup = container_of(port, struct tegra_utc_port, port); + +while (tegra_utc_tx_readl (tup,TEGRA_UTC_FIFO_STATUS ) &TEGRA_UTC_FIFO_FULL ) +cpu_relax ();
Ditto.
Quoted 2 lines
(quoted diff) +2
-0+ tegra_utc_tx_writel(tup, ch, TEGRA_UTC_DATA); +}
...
drivers/tty/serial/tegra-utc.c line 490
Quoted 5 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+static struct uart_driver tegra_utc_driver = { + .driver_name ="tegra-utc" , + .dev_name ="ttyUTC" , + .nr =UART_NR
Leave trailing comma.
Quoted 1 line
+};
...
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+static void tegra_utc_setup_port (struct device *dev,struct tegra_utc_port *tup) +{ + tup->port.dev = dev; + tup->port.fifosize = tup->soc->fifosize; + tup->port.flags =UPF_BOOT_AUTOCONF ; + tup->port.iotype =UPIO_MEM ; + tup->port.ops = &tegra_utc_uart_ops; + tup->port.type =PORT_TEGRA_TCU ; + tup->port.private_data = tup; +
drivers/tty/serial/tegra-utc.c line 512
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) +strscpy (tup->console.name,"ttyUTC" ,sizeof (tup->console.name)); + tup->console.write = tegra_utc_console_write; + tup->console.device = uart_console_device; + tup->console.setup = tegra_utc_console_setup; + tup->console.flags =CON_PRINTBUFFER |CON_CONSDEV |CON_ANYTIME ; + tup->console.data = &tegra_utc_driver; +#endif + +uart_read_port_properties (&tup->port);
No failure handling? In some cases it might return an error.
Quoted 1 line
+}
...
drivers/tty/serial/tegra-utc.c line 537
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +8
-0+static int tegra_utc_probe (struct platform_device *pdev) +{ +struct device_node *np = pdev->dev.of_node; +struct device *dev = &pdev->dev; +struct tegra_utc_port *tup; +int ret; + + tup =devm_kzalloc (&pdev->dev,sizeof (struct tegra_utc_port ),GFP_KERNEL );
sizeof(*tup)
drivers/tty/serial/tegra-utc.c line 541
Quoted 4 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+if (!tup) +return -ENOMEM ; + ret =of_property_read_u32 (np,"tx-threshold" , &tup->tx_threshold);
device_property_read_u32()
drivers/tty/serial/tegra-utc.c line 545
Quoted 4 lines
drivers/tty/serial/tegra-utc.c new file +4
-0+if (ret) +return dev_err_probe (dev, ret,"missing tx-threshold device-tree property\n" ); + + ret =of_property_read_u32 (np,"rx-threshold" , &tup->rx_threshold);
Ditto.
drivers/tty/serial/tegra-utc.c line 551
Quoted 6 lines
drivers/tty/serial/tegra-utc.c new file +5
-0+if (ret) +return dev_err_probe (dev, ret,"missing rx-threshold device-tree property\n" ); + tup->irq =platform_get_irq (pdev,0 ); +if (tup->irq <0 ) +return tup->irq;
uart_read_port_properties() does this for you.
drivers/tty/serial/tegra-utc.c line 553
Quoted 1 line
drivers/tty/serial/tegra-utc.c new file +1
-0+ tup->soc =of_device_get_match_data (&pdev->dev);
device_get_match_data()
Quoted 13 lines
drivers/tty/serial/tegra-utc.c new file +13
-0+ tup->tx_base =devm_platform_ioremap_resource_byname (pdev,"tx" ); +if (IS_ERR (tup->tx_base)) +return PTR_ERR (tup->tx_base); + + tup->rx_base =devm_platform_ioremap_resource_byname (pdev,"rx" ); +if (IS_ERR (tup->rx_base)) +return PTR_ERR (tup->rx_base); + +tegra_utc_setup_port (&pdev->dev, tup); +platform_set_drvdata (pdev, tup); + +return tegra_utc_register_port (tup); +}
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
Quoted 1 line · Andy Shevchenko
On Tue, Feb 11, 2025 at 11:49:45AM +0530, Kartik Rajput wrote:
...
Quoted 5 lines
drivers/tty/serial/tegra-utc.c line 11
Quoted 3 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+#include <linux/of.h> +#include <linux/of_device.h> Use
property.h(see also below).
To be more precise. The above proposal is to _replace_ linux/of*.h with linux/property.h and use APIs from there.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v2 2/2] serial: tegra-utc: Add driver for Tegra UART Trace Controller (UTC)
In reply to Andy ShevchenkoThanks for reviewing the patch Andy!
Quoted 40 lines · Andy Shevchenko
External email: Use caution opening links or attachments
Quoted 10 lines · Kartik Rajput
The Tegra264 SoC supports the UTC (UART Trace Controller), which allows
multiple firmware clients (up to 16) to share a single physical UART.
Each client is provided with its own interrupt and has access to a 128-character wide FIFO for both transmit (TX) and receive (RX) operations.Add tegra-utc driver to support Tegra UART Trace Controller (UTC) client.
...
+
bits.hQuoted 1 line
+#include <
linux/console.h>+
container_of.h
+device.h
+err.h
+io.hQuoted 1 line
+#include <
linux/kthread.h>+
kfifo.hQuoted 1 line
+#include <
linux/module.h>+
mod_devicetable.hdrivers/tty/serial/tegra-utc.c line 11
Quoted 3 lines
drivers/tty/serial/tegra-utc.c new file +2
-0+#include <linux/of.h> +#include <linux/of_device.h> Use
property.h(see also below).
Quoted 10 lines
drivers/tty/serial/tegra-utc.c line 18
Quoted 7 lines
drivers/tty/serial/tegra-utc.c new file +7
-0+#include <linux/platform_device.h> +#include <linux/serial.h> +#include <linux/serial_core.h> +#include <linux/slab.h> +#include <linux/string.h> +#include <linux/tty.h> +#include <linux/tty_flip.h> +
types.h
Ack.
Quoted 15 lines
...
drivers/tty/serial/tegra-utc.c line 214
Quoted 10 lines
drivers/tty/serial/tegra-utc.c new file +10
-0+static void tegra_utc_rx_chars (struct tegra_utc_port *tup) +{ + struct tty_port *port = &tup->port.state->port; + unsigned int max_chars = 256; + unsigned int flag; + u32 status; + int sysrq; + u32 ch; + + while (--max_chars) {Wouldn't while (max_chars--) { suffice?
Yes, I should be using max_chars-- instead. I will update this.
Quoted 32 lines
Quoted 23 lines
+ status = tegra_utc_rx_readl(tup,
TEGRA_UTC_FIFO_STATUS);
+ if (status &TEGRA_UTC_FIFO_EMPTY)
+ break;drivers/tty/serial/tegra-utc.c new file +10
-0+ + ch = tegra_utc_rx_readl(tup, TEGRA_UTC_DATA); + flag = TTY_NORMAL; + tup->port.icount.rx++; + + if (status & TEGRA_UTC_FIFO_OVERFLOW) + tup->port.icount.overrun++; + + uart_port_unlock(&tup->port); + sysrq = uart_handle_sysrq_char(&tup->port, ch &
0xff);
+uart_port_lock(&tup->port);drivers/tty/serial/tegra-utc.c new file +7
-0+ + if (!sysrq) + tty_insert_flip_char(port, ch, flag); + } + + tty_flip_buffer_push(port); +}...
Quoted 5 lines
drivers/tty/serial/tegra-utc.c new file +5
-0+static irqreturn_t tegra_utc_isr (int irq,void *dev_id) +{ + struct tegra_utc_port *tup = dev_id; + unsigned long flags; + u32 status;
Quoted 4 lines
Quoted 1 line
+
uart_port_lock_irqsave(&tup->port, &flags);As said previously, why
_irqsave?
You're right, at this point the IRQs are already disabled so this is not really required. I will use uart_port_lock/unlock() instead.
Quoted 42 lines
Quoted 26 lines
+ /* Process
RX_REQandRX_TIMEOUTinterrupts. */ + do {
+ status =tegra_utc_rx_readl(tup, TEGRA_UTC_INTR_STATUS)&tup->rx_irqmask;
+ if (status) {
+tegra_utc_rx_writel(tup, tup->rx_irqmask, TEGRA_UTC_INTR_CLEAR);
+tegra_utc_rx_chars(tup);
+ }
+ } while (status);
+
+ /* ProcessTX_REQinterrupt. */
+ do {
+ status =tegra_utc_tx_readl(tup, TEGRA_UTC_INTR_STATUS)&tup->tx_irqmask;
+ if (status) {
+tegra_utc_tx_writel(tup, tup->tx_irqmask, TEGRA_UTC_INTR_CLEAR);
+tegra_utc_tx_chars(tup);
+ }
+ } while (status);drivers/tty/serial/tegra-utc.c new file +5
-0+ + uart_port_unlock_irqrestore(&tup->port, flags); + + return IRQ_HANDLED; +}...
Quoted 9 lines
+{
+ structtegra_utc_port*tup =container_of(port, struct tegra_utc_port, port);
+ int ret;drivers/tty/serial/tegra-utc.c new file +4
-0+ + tegra_utc_hw_init(tup); + + ret = request_irq(tup->irq, tegra_utc_isr, 0, dev_name(port-Quoted 1 line
dev), tup);
Quoted 3 lines
+ if (ret < 0) {
+dev_err(port->dev, "failed to register interrupt handler\n");
Quoted 8 lines
Instead of these LoCs...
Quoted 4 lines
+ return ret;
+ }
+
+ return 0;return ret;
Ack.
Quoted 5 lines
Quoted 2 lines
+}
...
Quoted 10 lines
Quoted 8 lines
drivers/tty/serial/tegra-utc.c new file +3
-0+static int tegra_utc_get_poll_char (struct uart_port *port) +{ + struct tegra_utc_port *tup = container_of(port, struct
tegra_utc_port, port);
+
+ while (tegra_utc_rx_readl(tup, TEGRA_UTC_FIFO_STATUS)&TEGRA_UTC_FIFO_EMPTY)
+cpu_relax();No way out? HW might get stuck...
Ack. I will update this to use read_poll_timeout_atomic.
Quoted 6 lines
Quoted 3 lines
+ return
tegra_utc_rx_readl(tup, TEGRA_UTC_DATA); +}...
Quoted 11 lines
Quoted 9 lines
+static void
tegra_utc_put_poll_char(struct uart_port *port, unsigned char ch)
+{
+ structtegra_utc_port*tup =container_of(port, struct tegra_utc_port, port);
+
+ while (tegra_utc_tx_readl(tup, TEGRA_UTC_FIFO_STATUS)&TEGRA_UTC_FIFO_FULL)
+cpu_relax();Ditto.
Ack.
Quoted 6 lines
Quoted 3 lines
+
tegra_utc_tx_writel(tup, ch, TEGRA_UTC_DATA); +}...
Quoted 7 lines
Quoted 5 lines
+static struct
uart_drivertegra_utc_driver= {
+ .driver_name = "tegra-utc",
+ .dev_name = "ttyUTC",+ .nr =
UART_NRLeave trailing comma.
Ack.
Quoted 19 lines
Quoted 2 lines
+};
...
Quoted 14 lines
+static void
tegra_utc_setup_port(struct device *dev, struct tegra_utc_port *tup)drivers/tty/serial/tegra-utc.c new file +11
-0+{ + tup->port.dev = dev; + tup->port.fifosize = tup->soc->fifosize; + tup->port.flags = UPF_BOOT_AUTOCONF; + tup->port.iotype = UPIO_MEM; + tup->port.ops = &tegra_utc_uart_ops; + tup->port.type = PORT_TEGRA_TCU; + tup->port.private_data = tup; + +#if IS_ENABLED (CONFIG_SERIAL_TEGRA_UTC_CONSOLE ) + strscpy(tup->console.name, "ttyUTC", sizeof(tup-Quoted 1 line
console.name));
Quoted 11 lines
drivers/tty/serial/tegra-utc.c line 512
Quoted 9 lines
+ tup->console.write =
tegra_utc_console_write; + tup->console.device =uart_console_device; + tup->console.setup =tegra_utc_console_setup; + tup->console.flags =CON_PRINTBUFFER|CON_CONSDEV|CON_ANYTIME;
+ tup->console.data =&tegra_utc_driver;drivers/tty/serial/tegra-utc.c new file +3
-0+#endif + + uart_read_port_properties(&tup->port);No failure handling? In some cases it might return an error.
Ack, I will propagate the error properly.
Quoted 17 lines
Quoted 2 lines
+}
...
Quoted 9 lines
drivers/tty/serial/tegra-utc.c new file +8
-0+static int tegra_utc_probe (struct platform_device *pdev) +{ + struct device_node *np = pdev->dev.of_node; + struct device *dev = &pdev->dev; + struct tegra_utc_port *tup; + int ret; + + tup = devm_kzalloc(&pdev->dev, sizeof(struct tegra_utc_port),
GFP_KERNEL);
sizeof(*tup)
Quoted 7 lines
Quoted 5 lines
+ if (!tup)
+ return-ENOMEM;+ ret = of_property_read_u32(np, "tx-threshold", &tup-
Quoted 1 line
tx_threshold);
device_property_read_u32()
Ack.
Quoted 9 lines
Quoted 7 lines
+ if (ret)
+ returndev_err_probe(dev, ret, "missing tx-threshold device-tree property\n");
+
+ ret = of_property_read_u32(np, "rx-threshold", &tup-Quoted 1 line
rx_threshold);Ditto.
Ack.
Quoted 10 lines
Quoted 8 lines
+ if (ret)
+ returndev_err_probe(dev, ret, "missing rx-threshold device-tree property\n");+
tup->irq=platform_get_irq(pdev, 0);
+ if (tup->irq< 0)
+ returntup->irq;
uart_read_port_properties()does this for you.
Ack.
Quoted 4 lines
Quoted 2 lines
+
tup->soc=of_device_get_match_data(&pdev->dev);
device_get_match_data()
Ack.
Quoted 12 lines
Quoted 12 lines
+
tup->tx_base=devm_platform_ioremap_resource_byname(pdev, "tx");
+ if (IS_ERR(tup->tx_base))
+ returnPTR_ERR(tup->tx_base);
+
+tup->rx_base=devm_platform_ioremap_resource_byname(pdev, "rx");
+ if (IS_ERR(tup->rx_base))
+ returnPTR_ERR(tup->rx_base);
+
+tegra_utc_setup_port(&pdev->dev, tup);
Quoted 10 lines
drivers/tty/serial/tegra-utc.c line 567
Quoted 4 lines
+
platform_set_drvdata(pdev, tup);drivers/tty/serial/tegra-utc.c new file +3
-0+ + return tegra_utc_register_port(tup); +}--
With Best Regards,
Andy Shevchenko
Thanks,
Kartik