[BBC_ENVCTRL]: kmalloc + memset conversion to kzalloc
[cascardo/linux.git] / drivers / serial / bfin_5xx.c
index 408390f..66c92bc 100644 (file)
@@ -6,8 +6,6 @@
  * Created:
  * Description:  Driver for blackfin 5xx serial ports
  *
- * Rev:          $Id: bfin_5xx.c,v 1.19 2006/09/24 02:33:53 aubrey Exp $
- *
  * Modified:
  *               Copyright 2006 Analog Devices Inc.
  *
 #include <linux/tty_flip.h>
 #include <linux/serial_core.h>
 
+#ifdef CONFIG_KGDB_UART
+#include <linux/kgdb.h>
+#include <asm/irq_regs.h>
+#endif
+
 #include <asm/gpio.h>
 #include <asm/mach/bfin_serial_5xx.h>
 
@@ -83,8 +86,21 @@ static void bfin_serial_stop_tx(struct uart_port *port)
 {
        struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
 
+#ifdef CONFIG_BF54x
+       while (!(UART_GET_LSR(uart) & TEMT))
+               continue;
+#endif
+
 #ifdef CONFIG_SERIAL_BFIN_DMA
        disable_dma(uart->tx_dma_channel);
+#else
+#ifdef CONFIG_BF54x
+       /* Waiting for Transmission Finished */
+       while (!(UART_GET_LSR(uart) & TFI))
+               continue;
+       /* Clear TFI bit */
+       UART_PUT_LSR(uart, TFI);
+       UART_CLEAR_IER(uart, ETBEI);
 #else
        unsigned short ier;
 
@@ -92,6 +108,7 @@ static void bfin_serial_stop_tx(struct uart_port *port)
        ier &= ~ETBEI;
        UART_PUT_IER(uart, ier);
 #endif
+#endif
 }
 
 /*
@@ -103,6 +120,9 @@ static void bfin_serial_start_tx(struct uart_port *port)
 
 #ifdef CONFIG_SERIAL_BFIN_DMA
        bfin_serial_dma_tx_chars(uart);
+#else
+#ifdef CONFIG_BF54x
+       UART_SET_IER(uart, ETBEI);
 #else
        unsigned short ier;
        ier = UART_GET_IER(uart);
@@ -110,6 +130,7 @@ static void bfin_serial_start_tx(struct uart_port *port)
        UART_PUT_IER(uart, ier);
        bfin_serial_tx_chars(uart);
 #endif
+#endif
 }
 
 /*
@@ -118,11 +139,18 @@ static void bfin_serial_start_tx(struct uart_port *port)
 static void bfin_serial_stop_rx(struct uart_port *port)
 {
        struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
+#ifdef CONFIG_BF54x
+       UART_CLEAR_IER(uart, ERBFI);
+#else
        unsigned short ier;
 
        ier = UART_GET_IER(uart);
+#ifdef CONFIG_KGDB_UART
+       if (uart->port.line != CONFIG_KGDB_UART_PORT)
+#endif
        ier &= ~ERBFI;
        UART_PUT_IER(uart, ier);
+#endif
 }
 
 /*
@@ -132,6 +160,49 @@ static void bfin_serial_enable_ms(struct uart_port *port)
 {
 }
 
+#ifdef CONFIG_KGDB_UART
+static int kgdb_entry_state;
+
+void kgdb_put_debug_char(int chr)
+{
+       struct bfin_serial_port *uart;
+       
+       if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
+               uart = &bfin_serial_ports[0];
+       else
+               uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
+       
+       while (!(UART_GET_LSR(uart) & THRE)) {
+               __builtin_bfin_ssync();
+       }
+       UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
+       __builtin_bfin_ssync();
+       UART_PUT_CHAR(uart, (unsigned char)chr);
+       __builtin_bfin_ssync();
+}
+
+int kgdb_get_debug_char(void)
+{
+       struct bfin_serial_port *uart;
+       unsigned char chr;
+
+       if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
+               uart = &bfin_serial_ports[0];
+       else
+               uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
+       
+       while(!(UART_GET_LSR(uart) & DR)) {
+               __builtin_bfin_ssync();
+       }
+       UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
+       __builtin_bfin_ssync();
+       chr = UART_GET_CHAR(uart);
+       __builtin_bfin_ssync();
+
+       return chr;
+}
+#endif
+
 #ifdef CONFIG_SERIAL_BFIN_PIO
 static void local_put_char(struct bfin_serial_port *uart, char ch)
 {
@@ -152,8 +223,11 @@ static void local_put_char(struct bfin_serial_port *uart, char ch)
 
 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
 {
-       struct tty_struct *tty = uart->port.info?uart->port.info->tty:0;
+       struct tty_struct *tty = uart->port.info->tty;
        unsigned int status, ch, flg;
+#ifdef CONFIG_KGDB_UART
+       struct pt_regs *regs = get_irq_regs();
+#endif
 #ifdef BF533_FAMILY
        static int in_break = 0;
 #endif
@@ -162,6 +236,27 @@ static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
        ch = UART_GET_CHAR(uart);
        uart->port.icount.rx++;
 
+#ifdef CONFIG_KGDB_UART
+       if (uart->port.line == CONFIG_KGDB_UART_PORT) {
+               if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
+                       kgdb_breakkey_pressed(regs);
+                       return;
+               } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
+                       kgdb_entry_state = 1;
+               } else if (kgdb_entry_state == 1 && ch == 'q') {
+                       kgdb_entry_state = 0;
+                       kgdb_breakkey_pressed(regs);
+                       return;
+               } else if (ch == 0x3) {/* Ctrl + C */
+                       kgdb_entry_state = 0;
+                       kgdb_breakkey_pressed(regs);
+                       return;
+               } else {
+                       kgdb_entry_state = 0;
+               }
+       }
+#endif
 #ifdef BF533_FAMILY
        /* The BF533 family of processors have a nice misbehavior where
         * they continuously generate characters for a "single" break.
@@ -173,8 +268,10 @@ static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
                if (ch != 0) {
                        in_break = 0;
                        ch = UART_GET_CHAR(uart);
-               }
-               return;
+                       if (bfin_revid() < 5)
+                               return;
+               } else
+                       return;
        }
 #endif
 
@@ -185,27 +282,33 @@ static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
                uart->port.icount.brk++;
                if (uart_handle_break(&uart->port))
                        goto ignore_char;
-               flg = TTY_BREAK;
-       } else if (status & PE) {
-               flg = TTY_PARITY;
+               status &= ~(PE | FE);
+       }
+       if (status & PE)
                uart->port.icount.parity++;
-       } else if (status & OE) {
-               flg = TTY_OVERRUN;
+       if (status & OE)
                uart->port.icount.overrun++;
-       } else if (status & FE) {
-               flg = TTY_FRAME;
+       if (status & FE)
                uart->port.icount.frame++;
-       } else
+
+       status &= uart->port.read_status_mask;
+
+       if (status & BI)
+               flg = TTY_BREAK;
+       else if (status & PE)
+               flg = TTY_PARITY;
+       else if (status & FE)
+               flg = TTY_FRAME;
+       else
                flg = TTY_NORMAL;
 
        if (uart_handle_sysrq_char(&uart->port, ch))
                goto ignore_char;
-       if (tty)
-               uart_insert_char(&uart->port, status, 2, ch, flg);
 
-ignore_char:
-       if (tty)
-               tty_flip_buffer_push(tty);
+       uart_insert_char(&uart->port, status, OE, ch, flg);
+
+ ignore_char:
+       tty_flip_buffer_push(tty);
 }
 
 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
@@ -240,31 +343,57 @@ static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
                bfin_serial_stop_tx(&uart->port);
 }
 
-static irqreturn_t bfin_serial_int(int irq, void *dev_id)
+static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
 {
        struct bfin_serial_port *uart = dev_id;
+
+#ifdef CONFIG_BF54x
        unsigned short status;
+       spin_lock(&uart->port.lock);
+       status = UART_GET_LSR(uart);
+       while ((UART_GET_IER(uart) & ERBFI) && (status & DR)) {
+               bfin_serial_rx_chars(uart);
+               status = UART_GET_LSR(uart);
+       }
+       spin_unlock(&uart->port.lock);
+#else
+       spin_lock(&uart->port.lock);
+       while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_RX_READY)
+               bfin_serial_rx_chars(uart);
+       spin_unlock(&uart->port.lock);
+#endif
+       return IRQ_HANDLED;
+}
+
+static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
+{
+       struct bfin_serial_port *uart = dev_id;
 
+#ifdef CONFIG_BF54x
+       unsigned short status;
        spin_lock(&uart->port.lock);
-       status = UART_GET_IIR(uart);
-       do {
-               if ((status & IIR_STATUS) == IIR_TX_READY)
-                       bfin_serial_tx_chars(uart);
-               if ((status & IIR_STATUS) == IIR_RX_READY)
-                       bfin_serial_rx_chars(uart);
-               status = UART_GET_IIR(uart);
-       } while (status & (IIR_TX_READY | IIR_RX_READY));
+       status = UART_GET_LSR(uart);
+       while ((UART_GET_IER(uart) & ETBEI) && (status & THRE)) {
+               bfin_serial_tx_chars(uart);
+               status = UART_GET_LSR(uart);
+       }
+       spin_unlock(&uart->port.lock);
+#else
+       spin_lock(&uart->port.lock);
+       while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_TX_READY)
+               bfin_serial_tx_chars(uart);
        spin_unlock(&uart->port.lock);
+#endif
        return IRQ_HANDLED;
 }
 
+
 static void bfin_serial_do_work(struct work_struct *work)
 {
        struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
 
        bfin_serial_mctrl_check(uart);
 }
-
 #endif
 
 #ifdef CONFIG_SERIAL_BFIN_DMA
@@ -313,13 +442,17 @@ static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
        set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
        set_dma_x_modify(uart->tx_dma_channel, 1);
        enable_dma(uart->tx_dma_channel);
+#ifdef CONFIG_BF54x
+       UART_SET_IER(uart, ETBEI);
+#else
        ier = UART_GET_IER(uart);
        ier |= ETBEI;
        UART_PUT_IER(uart, ier);
+#endif
        spin_unlock_irqrestore(&uart->port.lock, flags);
 }
 
-static void bfin_serial_dma_rx_chars(struct bfin_serial_port * uart)
+static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
 {
        struct tty_struct *tty = uart->port.info->tty;
        int i, flg, status;
@@ -331,25 +464,33 @@ static void bfin_serial_dma_rx_chars(struct bfin_serial_port * uart)
                uart->port.icount.brk++;
                if (uart_handle_break(&uart->port))
                        goto dma_ignore_char;
-               flg = TTY_BREAK;
-       } else if (status & PE) {
-               flg = TTY_PARITY;
+               status &= ~(PE | FE);
+       }
+       if (status & PE)
                uart->port.icount.parity++;
-       } else if (status & OE) {
-               flg = TTY_OVERRUN;
+       if (status & OE)
                uart->port.icount.overrun++;
-       } else if (status & FE) {
-               flg = TTY_FRAME;
+       if (status & FE)
                uart->port.icount.frame++;
-       } else
+
+       status &= uart->port.read_status_mask;
+
+       if (status & BI)
+               flg = TTY_BREAK;
+       else if (status & PE)
+               flg = TTY_PARITY;
+       else if (status & FE)
+               flg = TTY_FRAME;
+       else
                flg = TTY_NORMAL;
 
        for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
                if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
                        goto dma_ignore_char;
-               uart_insert_char(&uart->port, status, 2, uart->rx_dma_buf.buf[i], flg);
+               uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
        }
-dma_ignore_char:
+
+ dma_ignore_char:
        tty_flip_buffer_push(tty);
 }
 
@@ -387,9 +528,13 @@ static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
        if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
                clear_dma_irqstat(uart->tx_dma_channel);
                disable_dma(uart->tx_dma_channel);
+#ifdef CONFIG_BF54x
+               UART_CLEAR_IER(uart, ETBEI);
+#else
                ier = UART_GET_IER(uart);
                ier &= ~ETBEI;
                UART_PUT_IER(uart, ier);
+#endif
                xmit->tail = (xmit->tail+uart->tx_count) &(UART_XMIT_SIZE -1);
                uart->port.icount.tx+=uart->tx_count;
 
@@ -500,6 +645,14 @@ static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
  */
 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
 {
+       struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
+       u16 lcr = UART_GET_LCR(uart);
+       if (break_state)
+               lcr |= SB;
+       else
+               lcr &= ~SB;
+       UART_PUT_LCR(uart, lcr);
+       SSYNC();
 }
 
 static int bfin_serial_startup(struct uart_port *port)
@@ -544,22 +697,30 @@ static int bfin_serial_startup(struct uart_port *port)
        uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
        add_timer(&(uart->rx_dma_timer));
 #else
+# ifdef        CONFIG_KGDB_UART
+       if (uart->port.line != CONFIG_KGDB_UART_PORT && request_irq
+# else
        if (request_irq
-           (uart->port.irq, bfin_serial_int, IRQF_DISABLED,
+# endif
+           (uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
             "BFIN_UART_RX", uart)) {
                printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
                return -EBUSY;
        }
 
        if (request_irq
-           (uart->port.irq+1, bfin_serial_int, IRQF_DISABLED,
+           (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
             "BFIN_UART_TX", uart)) {
                printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
                free_irq(uart->port.irq, uart);
                return -EBUSY;
        }
 #endif
+#ifdef CONFIG_BF54x
+       UART_SET_IER(uart, ERBFI);
+#else
        UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
+#endif
        return 0;
 }
 
@@ -574,6 +735,9 @@ static void bfin_serial_shutdown(struct uart_port *port)
        free_dma(uart->rx_dma_channel);
        del_timer(&(uart->rx_dma_timer));
 #else
+#ifdef CONFIG_KGDB_UART
+       if (uart->port.line != CONFIG_KGDB_UART_PORT)
+#endif
        free_irq(uart->port.irq, uart);
        free_irq(uart->port.irq+1, uart);
 #endif
@@ -608,19 +772,34 @@ bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
 
        if (termios->c_cflag & CSTOPB)
                lcr |= STB;
-       if (termios->c_cflag & PARENB) {
+       if (termios->c_cflag & PARENB)
                lcr |= PEN;
-               if (!(termios->c_cflag & PARODD))
-                       lcr |= EPS;
-       }
+       if (!(termios->c_cflag & PARODD))
+               lcr |= EPS;
+       if (termios->c_cflag & CMSPAR)
+               lcr |= STP;
 
-       /* These controls are not implemented for this port */
-       termios->c_iflag |= INPCK | BRKINT | PARMRK;
-       termios->c_iflag &= ~(IGNPAR | IGNBRK);
+       port->read_status_mask = OE;
+       if (termios->c_iflag & INPCK)
+               port->read_status_mask |= (FE | PE);
+       if (termios->c_iflag & (BRKINT | PARMRK))
+               port->read_status_mask |= BI;
 
-       /* These controls are not implemented for this port */
-       termios->c_iflag |= INPCK | BRKINT | PARMRK;
-       termios->c_iflag &= ~(IGNPAR | IGNBRK);
+       /*
+        * Characters to ignore
+        */
+       port->ignore_status_mask = 0;
+       if (termios->c_iflag & IGNPAR)
+               port->ignore_status_mask |= FE | PE;
+       if (termios->c_iflag & IGNBRK) {
+               port->ignore_status_mask |= BI;
+               /*
+                * If we're ignoring parity and break indicators,
+                * ignore overruns too (for real raw support).
+                */
+               if (termios->c_iflag & IGNPAR)
+                       port->ignore_status_mask |= OE;
+       }
 
        baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
        quot = uart_get_divisor(port, baud);
@@ -632,29 +811,41 @@ bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
 
        /* Disable UART */
        ier = UART_GET_IER(uart);
+#ifdef CONFIG_BF54x
+       UART_CLEAR_IER(uart, 0xF);
+#else
        UART_PUT_IER(uart, 0);
+#endif
 
+#ifndef CONFIG_BF54x
        /* Set DLAB in LCR to Access DLL and DLH */
        val = UART_GET_LCR(uart);
        val |= DLAB;
        UART_PUT_LCR(uart, val);
        SSYNC();
+#endif
 
        UART_PUT_DLL(uart, quot & 0xFF);
        SSYNC();
        UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
        SSYNC();
 
+#ifndef CONFIG_BF54x
        /* Clear DLAB in LCR to Access THR RBR IER */
        val = UART_GET_LCR(uart);
        val &= ~DLAB;
        UART_PUT_LCR(uart, val);
        SSYNC();
+#endif
 
        UART_PUT_LCR(uart, lcr);
 
        /* Enable UART */
+#ifdef CONFIG_BF54x
+       UART_SET_IER(uart, ier);
+#else
        UART_PUT_IER(uart, ier);
+#endif
 
        val = UART_GET_GCTL(uart);
        val |= UCEN;
@@ -766,15 +957,15 @@ static void __init bfin_serial_init_ports(void)
                        bfin_serial_resource[i].uart_rts_pin;
 #endif
                bfin_serial_hw_init(&bfin_serial_ports[i]);
-
        }
+
 }
 
 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
 {
        struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
-       while (!(UART_GET_LSR(uart)))
+       while (!(UART_GET_LSR(uart) & THRE))
                barrier();
        UART_PUT_CHAR(uart, ch);
        SSYNC();
@@ -826,18 +1017,22 @@ bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
                        case 2: *bits = 7; break;
                        case 3: *bits = 8; break;
                }
+#ifndef CONFIG_BF54x
                /* Set DLAB in LCR to Access DLL and DLH */
                val = UART_GET_LCR(uart);
                val |= DLAB;
                UART_PUT_LCR(uart, val);
+#endif
 
                dll = UART_GET_DLL(uart);
                dlh = UART_GET_DLH(uart);
 
+#ifndef CONFIG_BF54x
                /* Clear DLAB in LCR to Access THR RBR IER */
                val = UART_GET_LCR(uart);
                val &= ~DLAB;
                UART_PUT_LCR(uart, val);
+#endif
 
                *baud = get_sclk() / (16*(dll | dlh << 8));
        }
@@ -889,6 +1084,10 @@ static int __init bfin_serial_rs_console_init(void)
 {
        bfin_serial_init_ports();
        register_console(&bfin_serial_console);
+#ifdef CONFIG_KGDB_UART
+       kgdb_entry_state = 0;
+       init_kgdb_uart();
+#endif
        return 0;
 }
 console_initcall(bfin_serial_rs_console_init);
@@ -981,6 +1180,10 @@ static struct platform_driver bfin_serial_driver = {
 static int __init bfin_serial_init(void)
 {
        int ret;
+#ifdef CONFIG_KGDB_UART
+       struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
+       struct termios t;
+#endif
 
        pr_info("Serial: Blackfin serial driver\n");
 
@@ -994,6 +1197,21 @@ static int __init bfin_serial_init(void)
                        uart_unregister_driver(&bfin_serial_reg);
                }
        }
+#ifdef CONFIG_KGDB_UART
+       if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
+               request_irq(uart->port.irq, bfin_serial_int,
+                       IRQF_DISABLED, "BFIN_UART_RX", uart);
+               pr_info("Request irq for kgdb uart port\n");
+               UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
+               __builtin_bfin_ssync();
+               t.c_cflag = CS8|B57600;
+               t.c_iflag = 0;
+               t.c_oflag = 0;
+               t.c_lflag = ICANON;
+               t.c_line = CONFIG_KGDB_UART_PORT;
+               bfin_serial_set_termios(&uart->port, &t, &t);
+       }
+#endif
        return ret;
 }