From 6fe6d017a468560c396da7120d00977037ae971e Mon Sep 17 00:00:00 2001 From: Sebastian Huber Date: Tue, 26 Nov 2013 08:56:08 +0100 Subject: bsp/leon3: Avoid copy and paste in console driver --- c/src/lib/libbsp/sparc/leon3/console/console.c | 62 ++++++++++---------------- 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/c/src/lib/libbsp/sparc/leon3/console/console.c b/c/src/lib/libbsp/sparc/leon3/console/console.c index 0b30369592..1860e5ce0f 100644 --- a/c/src/lib/libbsp/sparc/leon3/console/console.c +++ b/c/src/lib/libbsp/sparc/leon3/console/console.c @@ -82,6 +82,18 @@ struct apbuart_priv { static struct apbuart_priv apbuarts[BSP_NUMBER_OF_TERMIOS_PORTS]; static int uarts = 0; +static struct apbuart_priv *leon3_console_get_uart(int minor) +{ + struct apbuart_priv *uart; + + if (minor == 0) + uart = &apbuarts[syscon_uart_index]; + else + uart = &apbuarts[minor - 1]; + + return uart; +} + #if CONSOLE_USE_INTERRUPTS /* Handle UART interrupts */ @@ -115,14 +127,9 @@ static void leon3_console_isr(void *arg) */ static int leon3_console_write_support(int minor, const char *buf, size_t len) { - struct apbuart_priv *uart; + struct apbuart_priv *uart = leon3_console_get_uart(minor); int sending; - if (minor == 0) - uart = &apbuarts[syscon_uart_index]; - else - uart = &apbuarts[minor - 1]; - if (len > 0) { /* Enable TX interrupt (interrupt is edge-triggered) */ uart->regs->ctrl |= LEON_REG_UART_CTRL_TI; @@ -157,15 +164,11 @@ int console_pollRead(int minor); */ ssize_t console_write_polled(int minor, const char *buf, size_t len) { - int nwrite = 0, port; - - if (minor == 0) - port = syscon_uart_index; - else - port = minor - 1; + struct apbuart_priv *uart = leon3_console_get_uart(minor); + int nwrite = 0; while (nwrite < len) { - apbuart_outbyte_polled(apbuarts[port].regs, *buf++, 1, 0); + apbuart_outbyte_polled(uart->regs, *buf++, 1, 0); nwrite++; } return nwrite; @@ -173,14 +176,9 @@ ssize_t console_write_polled(int minor, const char *buf, size_t len) int console_pollRead(int minor) { - int port; - - if (minor == 0) - port = syscon_uart_index; - else - port = minor - 1; + struct apbuart_priv *uart = leon3_console_get_uart(minor); - return apbuart_inbyte_nonblocking(apbuarts[port].regs); + return apbuart_inbyte_nonblocking(uart->regs); } #endif @@ -195,10 +193,10 @@ int console_scan_uarts(void); int console_set_attributes(int minor, const struct termios *t) { + struct apbuart_priv *uart = leon3_console_get_uart(minor); unsigned int scaler; unsigned int ctrl; int baud; - struct apbuart_priv *uart; switch (t->c_cflag & CSIZE) { default: @@ -211,11 +209,6 @@ int console_set_attributes(int minor, const struct termios *t) break; } - if (minor == 0) - uart = &apbuarts[syscon_uart_index]; - else - uart = &apbuarts[minor - 1]; - /* * FIXME: This read-modify-write sequence is broken since interrupts may * interfere. @@ -371,15 +364,11 @@ static struct rtems_termios_tty *leon3_console_get_tty( static int leon3_console_first_open(int major, int minor, void *arg) { - struct apbuart_priv *uart; - rtems_status_code sc; - - if (minor == 0) - uart = &apbuarts[syscon_uart_index]; - else - uart = &apbuarts[minor - 1]; + struct apbuart_priv *uart = leon3_console_get_uart(minor); #if CONSOLE_USE_INTERRUPTS + rtems_status_code sc; + uart->cookie = leon3_console_get_tty(arg); /* Register Interrupt handler */ @@ -407,14 +396,9 @@ static int leon3_console_first_open(int major, int minor, void *arg) static int leon3_console_last_close(int major, int minor, void *arg) { struct rtems_termios_tty *tty = leon3_console_get_tty(arg); - struct apbuart_priv *uart; + struct apbuart_priv *uart = leon3_console_get_uart(minor); rtems_interrupt_level level; - if (minor == 0) - uart = &apbuarts[syscon_uart_index]; - else - uart = &apbuarts[minor - 1]; - /* Turn off RX interrupts */ rtems_termios_interrupt_lock_acquire(tty, level); uart->regs->ctrl &= ~(LEON_REG_UART_CTRL_RI); -- cgit v1.2.3