bsp/leon3: Avoid copy and paste in console driver

This commit is contained in:
Sebastian Huber
2013-11-26 08:56:08 +01:00
parent 1e744efde9
commit 6fe6d017a4

View File

@@ -82,6 +82,18 @@ struct apbuart_priv {
static struct apbuart_priv apbuarts[BSP_NUMBER_OF_TERMIOS_PORTS]; static struct apbuart_priv apbuarts[BSP_NUMBER_OF_TERMIOS_PORTS];
static int uarts = 0; 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 #if CONSOLE_USE_INTERRUPTS
/* Handle UART 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) 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; int sending;
if (minor == 0)
uart = &apbuarts[syscon_uart_index];
else
uart = &apbuarts[minor - 1];
if (len > 0) { if (len > 0) {
/* Enable TX interrupt (interrupt is edge-triggered) */ /* Enable TX interrupt (interrupt is edge-triggered) */
uart->regs->ctrl |= LEON_REG_UART_CTRL_TI; 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) ssize_t console_write_polled(int minor, const char *buf, size_t len)
{ {
int nwrite = 0, port; struct apbuart_priv *uart = leon3_console_get_uart(minor);
int nwrite = 0;
if (minor == 0)
port = syscon_uart_index;
else
port = minor - 1;
while (nwrite < len) { while (nwrite < len) {
apbuart_outbyte_polled(apbuarts[port].regs, *buf++, 1, 0); apbuart_outbyte_polled(uart->regs, *buf++, 1, 0);
nwrite++; nwrite++;
} }
return 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 console_pollRead(int minor)
{ {
int port; struct apbuart_priv *uart = leon3_console_get_uart(minor);
if (minor == 0) return apbuart_inbyte_nonblocking(uart->regs);
port = syscon_uart_index;
else
port = minor - 1;
return apbuart_inbyte_nonblocking(apbuarts[port].regs);
} }
#endif #endif
@@ -195,10 +193,10 @@ int console_scan_uarts(void);
int console_set_attributes(int minor, const struct termios *t) int console_set_attributes(int minor, const struct termios *t)
{ {
struct apbuart_priv *uart = leon3_console_get_uart(minor);
unsigned int scaler; unsigned int scaler;
unsigned int ctrl; unsigned int ctrl;
int baud; int baud;
struct apbuart_priv *uart;
switch (t->c_cflag & CSIZE) { switch (t->c_cflag & CSIZE) {
default: default:
@@ -211,11 +209,6 @@ int console_set_attributes(int minor, const struct termios *t)
break; 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 * FIXME: This read-modify-write sequence is broken since interrupts may
* interfere. * 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) static int leon3_console_first_open(int major, int minor, void *arg)
{ {
struct apbuart_priv *uart; struct apbuart_priv *uart = leon3_console_get_uart(minor);
rtems_status_code sc;
if (minor == 0)
uart = &apbuarts[syscon_uart_index];
else
uart = &apbuarts[minor - 1];
#if CONSOLE_USE_INTERRUPTS #if CONSOLE_USE_INTERRUPTS
rtems_status_code sc;
uart->cookie = leon3_console_get_tty(arg); uart->cookie = leon3_console_get_tty(arg);
/* Register Interrupt handler */ /* 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) static int leon3_console_last_close(int major, int minor, void *arg)
{ {
struct rtems_termios_tty *tty = leon3_console_get_tty(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; rtems_interrupt_level level;
if (minor == 0)
uart = &apbuarts[syscon_uart_index];
else
uart = &apbuarts[minor - 1];
/* Turn off RX interrupts */ /* Turn off RX interrupts */
rtems_termios_interrupt_lock_acquire(tty, level); rtems_termios_interrupt_lock_acquire(tty, level);
uart->regs->ctrl &= ~(LEON_REG_UART_CTRL_RI); uart->regs->ctrl &= ~(LEON_REG_UART_CTRL_RI);