serial: liteuart: add IRQ support
Subsystems (2)
v4 cover serial: liteuart: add IRQ support
Add IRQ support to the LiteX LiteUART serial interface
Changes from v3:
- picked up r/b Ilpo Järvinen on select subset of commits
- rebased entire series on top of tty-next tree
- 2/14 ("use bit number macros"): explicitly include <linux/bits.h>
- 3/14 ("remove unused uart_ops stubs"): don't add gratuitous comment removed later on in the series
- 12/14 ("add IRQ support for the RX path"): add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies both liteuart_interrupt() and liteuart_startup()
- 13/14 ("add IRQ support for the TX path"):
- removed superfluous curly braces from liteuart_interrupt()
- simplified [start|stop]_tx() by using shadow irq register from 12/14
- simplified liteuart_tx_chars() by rebasing on top of tty-next tree
Quoted 20 lines
Changes from v2:
- further split out "separate RX loop from poll timer" into dedicated patches highlighting additional changes explicitly:
- factoring outtty_flip_buffer_push()(6/14)
- ack only RX events in RX loop (7/14)
- pass constant flag touart_insert_char()directly (8/14)
- fix variable types in rx loop (9/14)
- separating RX loop from poll timer (10/14)
- added patch (11/14) to move function definitions to a more convenient location, making subsequent changes easier to read in diff patch form.
- fixes and clarifications for RX path IRQ support patch (now 12/14):
- only returnIRQ_HANDLEDfor RX events
- usepr_fmt()to improve style of irq setup error message
- remove unnecessary locking from around single-register access
- modify TX path to support both IRQ-mode and polling (13/14)
- move polling-onlyliteuart_putchar()behind same conditional (CONFIG_SERIAL_LITEUART_CONSOLE) as the rest of the code that's still using it.
Quoted 4 lines
Changes from v1:
- split minor cosmetic changes out into individual patches(1/3 became 1..5/7)- patches 6/7 and 7/7 unchanged (used to be 2/3 and 3/3)
Gabriel Somlo (14):
serial: liteuart: use KBUILD_MODNAME as driver name
serial: liteuart: use bit number macros
serial: liteuart: remove unused uart_ops stubs
serial: liteuart: don't set unused port fields
serial: liteuart: minor style fix in liteuart_init()
serial: liteuart: move tty_flip_buffer_push() out of rx loop
serial: liteuart: rx loop should only ack rx events
serial: liteuart: simplify passing of uart_insert_char() flag
serial: liteuart: fix rx loop variable types
serial: liteuart: separate rx loop from poll timer
serial: liteuart: move function definitions
serial: liteuart: add IRQ support for the RX path
serial: liteuart: add IRQ support for the TX path
serial: liteuart: move polling putchar() function
drivers/tty/serial/liteuart.c | 210 ++++++++++++++++++++-------------- 1 file changed, 126 insertions(+), 84 deletions(-)
Signature · 1 line
2.38.1
PATCH v4 1/14 serial: liteuart: use KBUILD_MODNAME as driver name
Replace hard-coded instances of "liteuart" with KBUILD_MODNAME.
--- drivers/tty/serial/liteuart.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/tty/serial/liteuart.c +3 -3
@@ -57,7 +57,7 @@ static struct console liteuart_console;static struct uart_driver liteuart_driver = { .owner =THIS_MODULE , - .driver_name ="liteuart" , + .driver_name =KBUILD_MODNAME , .dev_name ="ttyLXU" , .major =0 , .minor =0 ,
@@ -321,7 +321,7 @@ static struct platform_driver liteuart_platform_driver = { .probe = liteuart_probe, .remove = liteuart_remove, .driver = { - .name = "liteuart", + .name = KBUILD_MODNAME, .of_match_table = liteuart_of_match, }, };
@@ -367,7 +367,7 @@ static int liteuart_console_setup(struct console *co, char *options) }static struct console liteuart_console = { - .name ="liteuart" , + .name =KBUILD_MODNAME , .write = liteuart_console_write, .device = uart_console_device, .setup = liteuart_console_setup,
Signature · 1 line
2.38.1
PATCH v4 2/14 serial: liteuart: use bit number macros
In reply to Gabriel SomloReplace magic bit constants (e.g., 1, 2, 4) with BIT(x) expressions.
---
New in v4:
- explicitly include <linux/bits.h>
drivers/tty/serial/liteuart.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/tty/serial/liteuart.c +3 -2
@@ -5,6 +5,7 @@* Copyright (C) 2019-2020 Antmicro <www.antmicro.com> */ +#include <linux/bits.h> #include <linux/console.h> #include <linux/litex.h> #include <linux/module.h>
@@ -38,8 +39,8 @@#define OFF_EV_ENABLE 0x14/* events */ -#define EV_TX 0x1 -#define EV_RX 0x2 +#define EV_TX BIT(0) +#define EV_RX BIT(1)struct liteuart_port {struct uart_port port;
Signature · 1 line
2.38.1
PATCH v4 3/14 serial: liteuart: remove unused uart_ops stubs
In reply to Gabriel SomloRemove stub uart_ops methods that are not called unconditionally from serial_core.
---
Changes since v3:
- no longer adding gratuitous comment to liteuart_stop_tx, removed later on in the series
drivers/tty/serial/liteuart.c | 17 ----------------- 1 file changed, 17 deletions(-)
drivers/tty/serial/liteuart.c +0 -17
@@ -154,11 +154,6 @@ static void liteuart_stop_rx(struct uart_port *port)del_timer (&uart->timer); } -static void liteuart_break_ctl (struct uart_port *port,int break_state) -{ -/* LiteUART doesn't support sending break signal */ -} -static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port);
@@ -197,15 +192,6 @@ static const char *liteuart_type(struct uart_port *port)return "liteuart" ; } -static void liteuart_release_port (struct uart_port *port) -{ -} - -static int liteuart_request_port (struct uart_port *port) -{ -return 0 ; -} -static void liteuart_config_port (struct uart_port *port,int flags) {/*
@@ -232,13 +218,10 @@ static const struct uart_ops liteuart_ops = { .stop_tx = liteuart_stop_tx, .start_tx = liteuart_start_tx, .stop_rx = liteuart_stop_rx, - .break_ctl = liteuart_break_ctl, .startup = liteuart_startup, .shutdown = liteuart_shutdown, .set_termios = liteuart_set_termios, .type = liteuart_type, - .release_port = liteuart_release_port, - .request_port = liteuart_request_port, .config_port = liteuart_config_port, .verify_port = liteuart_verify_port, };
Signature · 1 line
2.38.1
PATCH v4 4/14 serial: liteuart: don't set unused port fields
In reply to Gabriel SomloRemove regshift and iobase port fields, since they are unused by the driver.
--- drivers/tty/serial/liteuart.c | 2 -- 1 file changed, 2 deletions(-)
drivers/tty/serial/liteuart.c +0 -2
@@ -263,9 +263,7 @@ static int liteuart_probe(struct platform_device *pdev) port->iotype =UPIO_MEM ; port->flags =UPF_BOOT_AUTOCONF ; port->ops = &liteuart_ops; - port->regshift =2 ; port->fifosize =16 ; - port->iobase =1 ; port->type =PORT_UNKNOWN ; port->line = dev_id;spin_lock_init (&port->lock);
Signature · 1 line
2.38.1
PATCH v4 5/14 serial: liteuart: minor style fix in liteuart_init()
In reply to Gabriel Somlo--- drivers/tty/serial/liteuart.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/tty/serial/liteuart.c +2 -4
@@ -397,12 +397,10 @@ static int __init liteuart_init(void)return res; res =platform_driver_register (&liteuart_platform_driver); -if (res) { +if (res)uart_unregister_driver (&liteuart_driver); -return res; - } -return 0 ; +return res; } static void __exit liteuart_exit(void)
Signature · 1 line
2.38.1
PATCH v4 6/14 serial: liteuart: move tty_flip_buffer_push() out of rx loop
In reply to Gabriel SomloCalling tty_flip_buffer_push() for each individual received character is overkill. Move it out of the rx loop, and only call it once per set of characters received together.
--- drivers/tty/serial/liteuart.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/serial/liteuart.c +2 -2
@@ -87,10 +87,10 @@ static void liteuart_timer(struct timer_list *t)/* no overflow bits in status */ if (!(uart_handle_sysrq_char (port, ch)))uart_insert_char (port, status,0 , ch, flg); - -tty_flip_buffer_push (&port->state->port); } +tty_flip_buffer_push (&port->state->port); +mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }
Signature · 1 line
2.38.1
PATCH v4 7/14 serial: liteuart: rx loop should only ack rx events
In reply to Gabriel SomloWhile receiving characters, it is necessary to acknowledge each one by writing to the EV_PENDING register's EV_RX bit. Ensure we do not also gratuitously set the EV_TX bit in the process.
--- drivers/tty/serial/liteuart.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/serial/liteuart.c +1 -1
@@ -82,7 +82,7 @@ static void liteuart_timer(struct timer_list *t) port->icount.rx++;/* necessary for RXEMPTY to refresh its value */ -litex_write8 (membase +OFF_EV_PENDING ,EV_TX |EV_RX ); +litex_write8 (membase +OFF_EV_PENDING ,EV_RX );/* no overflow bits in status */ if (!(uart_handle_sysrq_char (port, ch)))
Signature · 1 line
2.38.1
PATCH v4 8/14 serial: liteuart: simplify passing of uart_insert_char() flag
In reply to Gabriel SomloSimply provide the hard-coded TTY_NORMAL flag to uart_insert_char() directly -- no need to dedicate a variable for that exclusive purpose.
--- drivers/tty/serial/liteuart.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/tty/serial/liteuart.c +1 -2
@@ -73,7 +73,6 @@ static void liteuart_timer(struct timer_list *t)struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port;unsigned char __iomem *membase = port->membase; -unsigned int flg =TTY_NORMAL ;int ch;unsigned long status;
@@ -86,7 +85,7 @@ static void liteuart_timer(struct timer_list *t)/* no overflow bits in status */ if (!(uart_handle_sysrq_char (port, ch))) -uart_insert_char (port, status,0 , ch, flg); +uart_insert_char (port, status,0 , ch,TTY_NORMAL ); }tty_flip_buffer_push (&port->state->port);
Signature · 1 line
2.38.1
PATCH v4 9/14 serial: liteuart: fix rx loop variable types
In reply to Gabriel SomloUpdate variable types to match the signature of uart_insert_char() which consumes them.
--- drivers/tty/serial/liteuart.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/tty/serial/liteuart.c +1 -2
@@ -73,8 +73,7 @@ static void liteuart_timer(struct timer_list *t)struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port;unsigned char __iomem *membase = port->membase; -int ch; -unsigned long status; +unsigned int status, ch;while ((status = !litex_read8 (membase +OFF_RXEMPTY )) ==1 ) { ch =litex_read8 (membase +OFF_RXTX );
Signature · 1 line
2.38.1
PATCH v4 10/14 serial: liteuart: separate rx loop from poll timer
In reply to Gabriel SomloConvert the rx loop into its own dedicated function, and (for now) call it from the poll timer. This is in preparation for adding irq support to the receive path.
--- drivers/tty/serial/liteuart.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-)
drivers/tty/serial/liteuart.c +9 -3
@@ -68,10 +68,8 @@ static struct uart_driver liteuart_driver = {#endif }; -static void liteuart_timer (struct timer_list *t) +static void liteuart_rx_chars (struct uart_port *port) { -struct liteuart_port *uart =from_timer (uart, t, timer); -struct uart_port *port = &uart->port;unsigned char __iomem *membase = port->membase;unsigned int status, ch;
@@ -88,6 +86,14 @@ static void liteuart_timer(struct timer_list *t) }tty_flip_buffer_push (&port->state->port); +} + +static void liteuart_timer (struct timer_list *t) +{ +struct liteuart_port *uart =from_timer (uart, t, timer); +struct uart_port *port = &uart->port; + +liteuart_rx_chars (port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }
Signature · 1 line
2.38.1
PATCH v4 11/14 serial: liteuart: move function definitions
In reply to Gabriel SomloMove definitions for liteuart_[stop|start]_tx(), liteuart_stop_rx(), and liteuart_putchar() to a more convenient location in preparation for adding IRQ support. This patch contains no functional changes.
--- drivers/tty/serial/liteuart.c | 82 +++++++++++++++++------------------ 1 file changed, 41 insertions(+), 41 deletions(-)
drivers/tty/serial/liteuart.c +41 -41
@@ -68,6 +68,47 @@ static struct uart_driver liteuart_driver = {#endif }; +static void liteuart_putchar (struct uart_port *port,unsigned char ch) +{ +while (litex_read8 (port->membase +OFF_TXFULL )) +cpu_relax (); + +litex_write8 (port->membase +OFF_RXTX , ch); +} + +static void liteuart_stop_tx (struct uart_port *port) +{ +} + +static void liteuart_start_tx (struct uart_port *port) +{ +struct circ_buf *xmit = &port->state->xmit; +unsigned char ch; + +if (unlikely (port->x_char)) { +litex_write8 (port->membase +OFF_RXTX , port->x_char); + port->icount.tx++; + port->x_char =0 ; + }else if (!uart_circ_empty (xmit)) { +while (xmit->head != xmit->tail) { + ch = xmit->buf[xmit->tail]; +uart_xmit_advance (port,1 ); +liteuart_putchar (port, ch); + } + } + +if (uart_circ_chars_pending (xmit) <WAKEUP_CHARS ) +uart_write_wakeup (port); +} + +static void liteuart_stop_rx (struct uart_port *port) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +/* just delete timer */ +del_timer (&uart->timer); +} +static void liteuart_rx_chars (struct uart_port *port) {unsigned char __iomem *membase = port->membase;
@@ -98,14 +139,6 @@ static void liteuart_timer(struct timer_list *t)mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); } -static void liteuart_putchar (struct uart_port *port,unsigned char ch) -{ -while (litex_read8 (port->membase +OFF_TXFULL )) -cpu_relax (); - -litex_write8 (port->membase +OFF_RXTX , ch); -} -static unsigned int liteuart_tx_empty (struct uart_port *port) {/* not really tx empty, just checking if tx is not full */
@@ -125,39 +158,6 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)return TIOCM_CTS |TIOCM_DSR |TIOCM_CAR ; } -static void liteuart_stop_tx (struct uart_port *port) -{ -} - -static void liteuart_start_tx (struct uart_port *port) -{ -struct circ_buf *xmit = &port->state->xmit; -unsigned char ch; - -if (unlikely (port->x_char)) { -litex_write8 (port->membase +OFF_RXTX , port->x_char); - port->icount.tx++; - port->x_char =0 ; - }else if (!uart_circ_empty (xmit)) { -while (xmit->head != xmit->tail) { - ch = xmit->buf[xmit->tail]; -uart_xmit_advance (port,1 ); -liteuart_putchar (port, ch); - } - } - -if (uart_circ_chars_pending (xmit) <WAKEUP_CHARS ) -uart_write_wakeup (port); -} - -static void liteuart_stop_rx (struct uart_port *port) -{ -struct liteuart_port *uart =to_liteuart_port (port); - -/* just delete timer */ -del_timer (&uart->timer); -} -static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port);
Signature · 1 line
2.38.1
PATCH v4 12/14 serial: liteuart: add IRQ support for the RX path
In reply to Gabriel SomloAdd support for IRQ-driven RX. Support for the TX path will be added in a separate commit.
---
Changes from v3:
- add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies both liteuart_interrupt() and liteuart_startup()
drivers/tty/serial/liteuart.c | 76 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)
drivers/tty/serial/liteuart.c +69 -7
@@ -7,6 +7,7 @@#include <linux/bits.h> #include <linux/console.h> +#include <linux/interrupt.h> #include <linux/litex.h> #include <linux/module.h> #include <linux/of.h>
@@ -46,6 +47,7 @@ struct liteuart_port {struct uart_port port;struct timer_list timer;u32 id; +u8 irq_reg; };#define to_liteuart_port (port) container_of(port, struct liteuart_port, port)
@@ -76,6 +78,19 @@ static void liteuart_putchar(struct uart_port *port, unsigned char ch)litex_write8 (port->membase +OFF_RXTX , ch); } +static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +if (set) + uart->irq_reg |= mask; +else + uart->irq_reg &= ~mask; + +if (port->irq) +litex_write8 (port->membase +OFF_EV_ENABLE , uart->irq_reg); +} +static void liteuart_stop_tx (struct uart_port *port) { }
@@ -129,13 +144,27 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static irqreturn_t liteuart_interrupt (int irq,void *data) +{ +struct liteuart_port *uart = data; +struct uart_port *port = &uart->port; +u8 isr; + +spin_lock (&port->lock); + isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg; +if (isr &EV_RX ) +liteuart_rx_chars (port); +spin_unlock (&port->lock); + +return IRQ_RETVAL (isr); +} +static void liteuart_timer (struct timer_list *t) {struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port; -liteuart_rx_chars (port); - +liteuart_interrupt (0 , port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }
@@ -161,19 +190,45 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; +int ret; -/* disable events */ -litex_write8 (port->membase +OFF_EV_ENABLE ,0 ); +if (port->irq) { + ret =request_irq (port->irq, liteuart_interrupt,0 , +KBUILD_MODNAME , uart); +if (ret) { +pr_err (pr_fmt ("line %d irq %d failed: using polling\n" ), + port->line, port->irq); + port->irq =0 ; + } + } -/* prepare timer for polling */ -timer_setup (&uart->timer, liteuart_timer,0 ); -mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); +spin_lock_irqsave (&port->lock, flags); +/* only enabling rx irqs during startup */ +liteuart_update_irq_reg (port, true,EV_RX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (!port->irq) { +timer_setup (&uart->timer, liteuart_timer,0 ); +mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); + }return 0 ; }static void liteuart_shutdown (struct uart_port *port) { +struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; + +spin_lock_irqsave (&port->lock, flags); +liteuart_update_irq_reg (port, false,EV_RX |EV_TX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (port->irq) +free_irq (port->irq, port); +else +del_timer_sync (&uart->timer); } static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
@@ -262,6 +317,13 @@ static int liteuart_probe(struct platform_device *pdev) goto err_erase_id; } +/* get irq */ + ret =platform_get_irq_optional (pdev,0 ); +if (ret <0 && ret != -ENXIO ) +return ret; +if (ret >0 ) + port->irq = ret; +/* values not from device tree */ port->dev = &pdev->dev; port->iotype =UPIO_MEM ;
Signature · 1 line
2.38.1
Re: [PATCH v4 12/14] serial: liteuart: add IRQ support for the RX path
Quoted 89 lines · Gabriel Somlo
Add support for IRQ-driven RX. Support for the TX path will be added in a separate commit.
Signed-off-by Gabriel Somlo <[email protected]>---
Changes from v3:
- add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies bothliteuart_interrupt()andliteuart_startup()drivers/tty/serial/liteuart.c | 76 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)drivers/tty/serial/liteuart.c +32
-2@@ -7,6 +7,7 @@#include <linux/bits.h> #include <linux/console.h> +#include <linux/interrupt.h> #include <linux/litex.h> #include <linux/module.h> #include <linux/of.h> @@ -46,6 +47,7 @@ struct liteuart_port {struct uart_port port;struct timer_list timer;u32 id; +u8 irq_reg; };#define to_liteuart_port (port) container_of(port, struct liteuart_port, port)@@ -76,6 +78,19 @@ static void liteuart_putchar(struct uart_port *port, unsigned char ch)litex_write8 (port->membase +OFF_RXTX , ch); } +static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +if (set) + uart->irq_reg |= mask; +else + uart->irq_reg &= ~mask; + +if (port->irq) +litex_write8 (port->membase +OFF_EV_ENABLE , uart->irq_reg); +} +static void liteuart_stop_tx (struct uart_port *port) { }@@ -129,13 +144,27 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static irqreturn_t liteuart_interrupt (int irq,void *data) +{ +struct liteuart_port *uart = data; +struct uart_port *port = &uart->port; +u8 isr; + +spin_lock (&port->lock); + isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg; +if (isr &EV_RX ) +liteuart_rx_chars (port); +spin_unlock (&port->lock); + +return IRQ_RETVAL (isr); +} +static void liteuart_timer (struct timer_list *t) {struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port; -liteuart_rx_chars (port); - +liteuart_interrupt (0 , port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }@@ -161,19 +190,45 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags;
drivers/tty/serial/liteuart.c static unsigned int liteuart_get_mctrl(struct uart_port *port) · line 201
Quoted 10 lines
drivers/tty/serial/liteuart.c +7
-2+int ret; -/* disable events */ -litex_write8 (port->membase +OFF_EV_ENABLE ,0 ); +if (port->irq) { + ret =request_irq (port->irq, liteuart_interrupt,0 , +KBUILD_MODNAME , uart); +if (ret) { +pr_err (pr_fmt ("line %d irq %d failed: using polling\n" ), + port->line, port->irq);
dev_err() seems more appropriate here.
Quoted 37 lines
drivers/tty/serial/liteuart.c +23
-3+ port->irq =0 ; + } + } -/* prepare timer for polling */ -timer_setup (&uart->timer, liteuart_timer,0 ); -mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); +spin_lock_irqsave (&port->lock, flags); +/* only enabling rx irqs during startup */ +liteuart_update_irq_reg (port, true,EV_RX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (!port->irq) { +timer_setup (&uart->timer, liteuart_timer,0 ); +mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); + }return 0 ; }static void liteuart_shutdown (struct uart_port *port) { +struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; + +spin_lock_irqsave (&port->lock, flags); +liteuart_update_irq_reg (port, false,EV_RX |EV_TX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (port->irq) +free_irq (port->irq, port); +else +del_timer_sync (&uart->timer); } static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
drivers/tty/serial/liteuart.c static int liteuart_probe(struct platform_device *pdev) · line 320
Quoted 4 lines
drivers/tty/serial/liteuart.c +1
-0@@ -262,6 +317,13 @@ static int liteuart_probe(struct platform_device *pdev) goto err_erase_id; } +/* get irq */
This comment is unnecessary.
Other than that,
Signature · 1 line
i.
Quoted 10 lines
drivers/tty/serial/liteuart.c +6
-0+ ret =platform_get_irq_optional (pdev,0 ); +if (ret <0 && ret != -ENXIO ) +return ret; +if (ret >0 ) + port->irq = ret; +/* values not from device tree */ port->dev = &pdev->dev; port->iotype =UPIO_MEM ;
Re: [PATCH v4 12/14] serial: liteuart: add IRQ support for the RX path
Quoted 93 lines · Ilpo Järvinen
Quoted 91 lines · Gabriel Somlo
Add support for IRQ-driven RX. Support for the TX path will be added in a separate commit.
Signed-off-by Gabriel Somlo <[email protected]>---
Changes from v3:
- add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies bothliteuart_interrupt()andliteuart_startup()drivers/tty/serial/liteuart.c | 76 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)drivers/tty/serial/liteuart.c +33
-2@@ -7,6 +7,7 @@#include <linux/bits.h> #include <linux/console.h> +#include <linux/interrupt.h> #include <linux/litex.h> #include <linux/module.h> #include <linux/of.h> @@ -46,6 +47,7 @@ struct liteuart_port {struct uart_port port;struct timer_list timer;u32 id; +u8 irq_reg; };#define to_liteuart_port (port) container_of(port, struct liteuart_port, port)@@ -76,6 +78,19 @@ static void liteuart_putchar(struct uart_port *port, unsigned char ch)litex_write8 (port->membase +OFF_RXTX , ch); } +static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +if (set) + uart->irq_reg |= mask; +else + uart->irq_reg &= ~mask; + +if (port->irq) +litex_write8 (port->membase +OFF_EV_ENABLE , uart->irq_reg); +} +static void liteuart_stop_tx (struct uart_port *port) { }@@ -129,13 +144,27 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static irqreturn_t liteuart_interrupt (int irq,void *data) +{ +struct liteuart_port *uart = data; +struct uart_port *port = &uart->port; +u8 isr; + +spin_lock (&port->lock); + isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg; +if (isr &EV_RX ) +liteuart_rx_chars (port); +spin_unlock (&port->lock); + +return IRQ_RETVAL (isr); +} +static void liteuart_timer (struct timer_list *t) {struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port; -liteuart_rx_chars (port); - +liteuart_interrupt (0 , port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }@@ -161,19 +190,45 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; +int ret;
Quoted 10 lines
drivers/tty/serial/liteuart.c static unsigned int liteuart_get_mctrl(struct uart_port *port) · line 201
Quoted 8 lines
drivers/tty/serial/liteuart.c +6
-2-/* disable events */ -litex_write8 (port->membase +OFF_EV_ENABLE ,0 ); +if (port->irq) { + ret =request_irq (port->irq, liteuart_interrupt,0 , +KBUILD_MODNAME , uart); +if (ret) { +pr_err (pr_fmt ("line %d irq %d failed: using polling\n" ), + port->line, port->irq);
dev_err()seems more appropriate here.
Makes sense, good thing struct uart_port has a dev field :) I've made the change, and lined it up for the upcoming v5 of the series.
Quoted 35 lines
Quoted 35 lines
drivers/tty/serial/liteuart.c +23
-3+ port->irq =0 ; + } + } -/* prepare timer for polling */ -timer_setup (&uart->timer, liteuart_timer,0 ); -mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); +spin_lock_irqsave (&port->lock, flags); +/* only enabling rx irqs during startup */ +liteuart_update_irq_reg (port, true,EV_RX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (!port->irq) { +timer_setup (&uart->timer, liteuart_timer,0 ); +mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); + }return 0 ; }static void liteuart_shutdown (struct uart_port *port) { +struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; + +spin_lock_irqsave (&port->lock, flags); +liteuart_update_irq_reg (port, false,EV_RX |EV_TX ); +spin_unlock_irqrestore (&port->lock, flags); + +if (port->irq) +free_irq (port->irq, port); +else +del_timer_sync (&uart->timer); }
Quoted 8 lines
drivers/tty/serial/liteuart.c static int liteuart_probe(struct platform_device *pdev) · line 320
Quoted 6 lines
drivers/tty/serial/liteuart.c +1
-0static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,@@ -262,6 +317,13 @@ static int liteuart_probe(struct platform_device *pdev) goto err_erase_id; } +/* get irq */ This comment is unnecessary.
Removed in v5.
Quoted 3 lines
Other than that,
Reviewed-by Ilpo Järvinen <[email protected]>
Thanks much, I'll send out v5 a bit later today.
--Gabriel
Quoted 14 lines
Signature · 1 line
i.Quoted 10 lines
drivers/tty/serial/liteuart.c +6
-0+ ret =platform_get_irq_optional (pdev,0 ); +if (ret <0 && ret != -ENXIO ) +return ret; +if (ret >0 ) + port->irq = ret; +/* values not from device tree */ port->dev = &pdev->dev; port->iotype =UPIO_MEM ;
Re: [PATCH v4 12/14] serial: liteuart: add IRQ support for the RX path
Quoted 105 lines · Gabriel L. Somlo
Quoted 93 lines · Ilpo Järvinen
Quoted 91 lines · Gabriel Somlo
Add support for IRQ-driven RX. Support for the TX path will be added in a separate commit.
Signed-off-by Gabriel Somlo <[email protected]>---
Changes from v3:
- add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies bothliteuart_interrupt()andliteuart_startup()drivers/tty/serial/liteuart.c | 76 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)drivers/tty/serial/liteuart.c +33
-2@@ -7,6 +7,7 @@#include <linux/bits.h> #include <linux/console.h> +#include <linux/interrupt.h> #include <linux/litex.h> #include <linux/module.h> #include <linux/of.h> @@ -46,6 +47,7 @@ struct liteuart_port {struct uart_port port;struct timer_list timer;u32 id; +u8 irq_reg; };#define to_liteuart_port (port) container_of(port, struct liteuart_port, port)@@ -76,6 +78,19 @@ static void liteuart_putchar(struct uart_port *port, unsigned char ch)litex_write8 (port->membase +OFF_RXTX , ch); } +static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +if (set) + uart->irq_reg |= mask; +else + uart->irq_reg &= ~mask; + +if (port->irq) +litex_write8 (port->membase +OFF_EV_ENABLE , uart->irq_reg); +} +static void liteuart_stop_tx (struct uart_port *port) { }@@ -129,13 +144,27 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static irqreturn_t liteuart_interrupt (int irq,void *data) +{ +struct liteuart_port *uart = data; +struct uart_port *port = &uart->port; +u8 isr; + +spin_lock (&port->lock); + isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg; +if (isr &EV_RX ) +liteuart_rx_chars (port); +spin_unlock (&port->lock); + +return IRQ_RETVAL (isr); +} +static void liteuart_timer (struct timer_list *t) {struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port; -liteuart_rx_chars (port); - +liteuart_interrupt (0 , port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }@@ -161,19 +190,45 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags; +int ret;Quoted 10 lines
drivers/tty/serial/liteuart.c static unsigned int liteuart_get_mctrl(struct uart_port *port) · line 201
Quoted 8 lines
drivers/tty/serial/liteuart.c +6
-2-/* disable events */ -litex_write8 (port->membase +OFF_EV_ENABLE ,0 ); +if (port->irq) { + ret =request_irq (port->irq, liteuart_interrupt,0 , +KBUILD_MODNAME , uart); +if (ret) { +pr_err (pr_fmt ("line %d irq %d failed: using polling\n" ), + port->line, port->irq);
dev_err()seems more appropriate here.
Quoted 3 lines
Makes sense, good thing
struct uart_porthas adevfield :) I've made the change, and lined it up for the upcoming v5 of the series.
Also that pr_fmt() should be dropped (in case you didn't already). It was in my mind ealier but should have stated it explicitly.
Signature · 1 line
i.
Re: [PATCH v4 12/14] serial: liteuart: add IRQ support for the RX path
Quoted 107 lines · Ilpo Järvinen
Quoted 105 lines · Gabriel L. Somlo
Quoted 103 lines · Ilpo Järvinen
Quoted 89 lines · Gabriel Somlo
Add support for IRQ-driven RX. Support for the TX path will be added in a separate commit.
Signed-off-by Gabriel Somlo <[email protected]>---
Changes from v3:
- add shadow irq register to support polling mode and avoid reading hardware mmio irq register to learn which irq flags are enabled
- this also simplifies bothliteuart_interrupt()andliteuart_startup()drivers/tty/serial/liteuart.c | 76 +++++++++++++++++++++++++++++++---- 1 file changed, 69 insertions(+), 7 deletions(-)drivers/tty/serial/liteuart.c +32
-2@@ -7,6 +7,7 @@#include <linux/bits.h> #include <linux/console.h> +#include <linux/interrupt.h> #include <linux/litex.h> #include <linux/module.h> #include <linux/of.h> @@ -46,6 +47,7 @@ struct liteuart_port {struct uart_port port;struct timer_list timer;u32 id; +u8 irq_reg; };#define to_liteuart_port (port) container_of(port, struct liteuart_port, port)@@ -76,6 +78,19 @@ static void liteuart_putchar(struct uart_port *port, unsigned char ch)litex_write8 (port->membase +OFF_RXTX , ch); } +static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) +{ +struct liteuart_port *uart =to_liteuart_port (port); + +if (set) + uart->irq_reg |= mask; +else + uart->irq_reg &= ~mask; + +if (port->irq) +litex_write8 (port->membase +OFF_EV_ENABLE , uart->irq_reg); +} +static void liteuart_stop_tx (struct uart_port *port) { }@@ -129,13 +144,27 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static irqreturn_t liteuart_interrupt (int irq,void *data) +{ +struct liteuart_port *uart = data; +struct uart_port *port = &uart->port; +u8 isr; + +spin_lock (&port->lock); + isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg; +if (isr &EV_RX ) +liteuart_rx_chars (port); +spin_unlock (&port->lock); + +return IRQ_RETVAL (isr); +} +static void liteuart_timer (struct timer_list *t) {struct liteuart_port *uart =from_timer (uart, t, timer);struct uart_port *port = &uart->port; -liteuart_rx_chars (port); - +liteuart_interrupt (0 , port);mod_timer (&uart->timer, jiffies +uart_poll_timeout (port)); }@@ -161,19 +190,45 @@ static unsigned int liteuart_get_mctrl(struct uart_port *port)static int liteuart_startup (struct uart_port *port) {struct liteuart_port *uart =to_liteuart_port (port); +unsigned long flags;drivers/tty/serial/liteuart.c static unsigned int liteuart_get_mctrl(struct uart_port *port) · line 201
Quoted 10 lines
drivers/tty/serial/liteuart.c +7
-2+int ret; -/* disable events */ -litex_write8 (port->membase +OFF_EV_ENABLE ,0 ); +if (port->irq) { + ret =request_irq (port->irq, liteuart_interrupt,0 , +KBUILD_MODNAME , uart); +if (ret) { +pr_err (pr_fmt ("line %d irq %d failed: using polling\n" ), + port->line, port->irq);
dev_err()seems more appropriate here.
Quoted 6 lines
Quoted 3 lines
Makes sense, good thing
struct uart_porthas adevfield :) I've made the change, and lined it up for the upcoming v5 of the series.Also that
pr_fmt()should be dropped (in case you didn't already). It was in my mind ealier but should have stated it explicitly.
Understood. I've used dev_err() before, it just slipped my mind to check for the availability of a dev field in struct uart_port... :)
Thanks again for the review!
Best,
--Gabriel
PATCH v4 13/14 serial: liteuart: add IRQ support for the TX path
In reply to Gabriel SomloSwitch the TX path to IRQ-driven operation, while maintaining support for polling mode via the poll timer.
---
Changes from v3:
- remove superfluous curly braces from liteuart_interrupt()
- simplified [start|stop]_tx() by using shadow irq register from
patch 12/14
- simplified liteuart_tx_chars() by rebasing on top of tty-next tree
drivers/tty/serial/liteuart.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)
drivers/tty/serial/liteuart.c +13 -17
@@ -93,27 +93,12 @@ static void liteuart_update_irq_reg(struct uart_port *port, bool set, u8 mask)static void liteuart_stop_tx (struct uart_port *port) { +liteuart_update_irq_reg (port, false,EV_TX ); }static void liteuart_start_tx (struct uart_port *port) { -struct circ_buf *xmit = &port->state->xmit; -unsigned char ch; - -if (unlikely (port->x_char)) { -litex_write8 (port->membase +OFF_RXTX , port->x_char); - port->icount.tx++; - port->x_char =0 ; - }else if (!uart_circ_empty (xmit)) { -while (xmit->head != xmit->tail) { - ch = xmit->buf[xmit->tail]; -uart_xmit_advance (port,1 ); -liteuart_putchar (port, ch); - } - } - -if (uart_circ_chars_pending (xmit) <WAKEUP_CHARS ) -uart_write_wakeup (port); +liteuart_update_irq_reg (port, true,EV_TX ); }static void liteuart_stop_rx (struct uart_port *port)
@@ -144,6 +129,15 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static void liteuart_tx_chars (struct uart_port *port) +{ +u8 ch; + +uart_port_tx (port, ch, + !litex_read8 (port->membase +OFF_TXFULL ), +litex_write8 (port->membase +OFF_RXTX , ch)); +} +static irqreturn_t liteuart_interrupt (int irq,void *data) {struct liteuart_port *uart = data;
@@ -154,6 +148,8 @@ static irqreturn_t liteuart_interrupt(int irq, void *data) isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg;if (isr &EV_RX )liteuart_rx_chars (port); +if (isr &EV_TX ) +liteuart_tx_chars (port);spin_unlock (&port->lock);return IRQ_RETVAL (isr);
Signature · 1 line
2.38.1
Re: [PATCH v4 13/14] serial: liteuart: add IRQ support for the TX path
Quoted 66 lines · Gabriel Somlo
Switch the TX path to IRQ-driven operation, while maintaining support for polling mode via the poll timer.
Signed-off-by Gabriel Somlo <[email protected]>---
Changes from v3:
- remove superfluous curly braces fromliteuart_interrupt()
- simplified [start|stop]_tx() by using shadow irq register frompatch 12/14- simplified
liteuart_tx_chars()by rebasing on top of tty-next treedrivers/tty/serial/liteuart.c | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-)drivers/tty/serial/liteuart.c +11
-17@@ -93,27 +93,12 @@ static void liteuart_update_irq_reg(struct uart_port *port, bool set, u8 mask)static void liteuart_stop_tx (struct uart_port *port) { +liteuart_update_irq_reg (port, false,EV_TX ); }static void liteuart_start_tx (struct uart_port *port) { -struct circ_buf *xmit = &port->state->xmit; -unsigned char ch; - -if (unlikely (port->x_char)) { -litex_write8 (port->membase +OFF_RXTX , port->x_char); - port->icount.tx++; - port->x_char =0 ; - }else if (!uart_circ_empty (xmit)) { -while (xmit->head != xmit->tail) { - ch = xmit->buf[xmit->tail]; -uart_xmit_advance (port,1 ); -liteuart_putchar (port, ch); - } - } - -if (uart_circ_chars_pending (xmit) <WAKEUP_CHARS ) -uart_write_wakeup (port); +liteuart_update_irq_reg (port, true,EV_TX ); }static void liteuart_stop_rx (struct uart_port *port)@@ -144,6 +129,15 @@ static void liteuart_rx_chars(struct uart_port *port)tty_flip_buffer_push (&port->state->port); } +static void liteuart_tx_chars (struct uart_port *port) +{ +u8 ch; + +uart_port_tx (port, ch, + !litex_read8 (port->membase +OFF_TXFULL ), +litex_write8 (port->membase +OFF_RXTX , ch)); +} +static irqreturn_t liteuart_interrupt (int irq,void *data) {struct liteuart_port *uart = data;
drivers/tty/serial/liteuart.c static irqreturn_t liteuart_interrupt(int irq, void *data) · line 155
Quoted 8 lines
drivers/tty/serial/liteuart.c +2
-0@@ -154,6 +148,8 @@ static irqreturn_t liteuart_interrupt(int irq, void *data) isr =litex_read8 (port->membase +OFF_EV_PENDING ) & uart->irq_reg;if (isr &EV_RX )liteuart_rx_chars (port); +if (isr &EV_TX ) +liteuart_tx_chars (port);spin_unlock (&port->lock);return IRQ_RETVAL (isr);
Looks very clean now with uart_port_tx(), thanks.
Signature · 1 line
i.
PATCH v4 14/14 serial: liteuart: move polling putchar() function
In reply to Gabriel SomloThe polling liteuart_putchar() function is only called from methods conditionally enabled by CONFIG_SERIAL_LITEUART_CONSOLE. Move its definition closer to the console code where it is dependent on the same config option.
--- drivers/tty/serial/liteuart.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/tty/serial/liteuart.c +8 -8
@@ -70,14 +70,6 @@ static struct uart_driver liteuart_driver = {#endif }; -static void liteuart_putchar (struct uart_port *port,unsigned char ch) -{ -while (litex_read8 (port->membase +OFF_TXFULL )) -cpu_relax (); - -litex_write8 (port->membase +OFF_RXTX , ch); -} -static void liteuart_update_irq_reg (struct uart_port *port,bool set,u8 mask) {struct liteuart_port *uart =to_liteuart_port (port);
@@ -372,6 +364,14 @@ static struct platform_driver liteuart_platform_driver = {#ifdef CONFIG_SERIAL_LITEUART_CONSOLE +static void liteuart_putchar (struct uart_port *port,unsigned char ch) +{ +while (litex_read8 (port->membase +OFF_TXFULL )) +cpu_relax (); + +litex_write8 (port->membase +OFF_RXTX , ch); +} +static void liteuart_console_write (struct console *co,const char *s,unsigned int count) {
Signature · 1 line
2.38.1