pch_uart: Fix MSI setting issue
[linux-flexiantxendom0.git] / drivers / tty / serial / pch_uart.c
index 0c95051..da776a0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *Copyright (C) 2010 OKI SEMICONDUCTOR CO., LTD.
+ *Copyright (C) 2011 LAPIS Semiconductor Co., Ltd.
  *
  *This program is free software; you can redistribute it and/or modify
  *it under the terms of the GNU General Public License as published by
  *along with this program; if not, write to the Free Software
  *Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307, USA.
  */
+#include <linux/kernel.h>
 #include <linux/serial_reg.h>
-#include <linux/pci.h>
+#include <linux/slab.h>
 #include <linux/module.h>
 #include <linux/pci.h>
 #include <linux/serial_core.h>
+#include <linux/tty.h>
+#include <linux/tty_flip.h>
 #include <linux/interrupt.h>
 #include <linux/io.h>
+#include <linux/dmi.h>
 
 #include <linux/dmaengine.h>
 #include <linux/pch_dma.h>
@@ -42,7 +46,8 @@ enum {
 
 /* Set the max number of UART port
  * Intel EG20T PCH: 4 port
- * OKI SEMICONDUCTOR ML7213 IOH: 3 port
+ * LAPIS Semiconductor ML7213 IOH: 3 port
+ * LAPIS Semiconductor ML7223 IOH: 2 port
 */
 #define PCH_UART_NR    4
 
@@ -136,8 +141,6 @@ enum {
 #define PCH_UART_DLL           0x00
 #define PCH_UART_DLM           0x01
 
-#define DIV_ROUND(a, b)        (((a) + ((b)/2)) / (b))
-
 #define PCH_UART_IID_RLS       (PCH_UART_IIR_REI)
 #define PCH_UART_IID_RDR       (PCH_UART_IIR_RRI)
 #define PCH_UART_IID_RDR_TO    (PCH_UART_IIR_RRI | PCH_UART_IIR_TOI)
@@ -235,6 +238,44 @@ struct eg20t_port {
        dma_addr_t                      rx_buf_dma;
 };
 
+/**
+ * struct pch_uart_driver_data - private data structure for UART-DMA
+ * @port_type:                 The number of DMA channel
+ * @line_no:                   UART port line number (0, 1, 2...)
+ */
+struct pch_uart_driver_data {
+       int port_type;
+       int line_no;
+};
+
+enum pch_uart_num_t {
+       pch_et20t_uart0 = 0,
+       pch_et20t_uart1,
+       pch_et20t_uart2,
+       pch_et20t_uart3,
+       pch_ml7213_uart0,
+       pch_ml7213_uart1,
+       pch_ml7213_uart2,
+       pch_ml7223_uart0,
+       pch_ml7223_uart1,
+       pch_ml7831_uart0,
+       pch_ml7831_uart1,
+};
+
+static struct pch_uart_driver_data drv_dat[] = {
+       [pch_et20t_uart0] = {PCH_UART_8LINE, 0},
+       [pch_et20t_uart1] = {PCH_UART_2LINE, 1},
+       [pch_et20t_uart2] = {PCH_UART_2LINE, 2},
+       [pch_et20t_uart3] = {PCH_UART_2LINE, 3},
+       [pch_ml7213_uart0] = {PCH_UART_8LINE, 0},
+       [pch_ml7213_uart1] = {PCH_UART_2LINE, 1},
+       [pch_ml7213_uart2] = {PCH_UART_2LINE, 2},
+       [pch_ml7223_uart0] = {PCH_UART_8LINE, 0},
+       [pch_ml7223_uart1] = {PCH_UART_2LINE, 1},
+       [pch_ml7831_uart0] = {PCH_UART_8LINE, 0},
+       [pch_ml7831_uart1] = {PCH_UART_2LINE, 1},
+};
+
 static unsigned int default_baud = 9600;
 static const int trigger_level_256[4] = { 1, 64, 128, 224 };
 static const int trigger_level_64[4] = { 1, 16, 32, 56 };
@@ -281,7 +322,7 @@ static int pch_uart_hal_set_line(struct eg20t_port *priv, int baud,
        unsigned int dll, dlm, lcr;
        int div;
 
-       div = DIV_ROUND(priv->base_baud / 16, baud);
+       div = DIV_ROUND_CLOSEST(priv->base_baud / 16, baud);
        if (div < 0 || USHRT_MAX <= div) {
                dev_err(priv->port.dev, "Invalid Baud(div=0x%x)\n", div);
                return -EINVAL;
@@ -563,12 +604,14 @@ static void pch_request_dma(struct uart_port *port)
        dma_cap_zero(mask);
        dma_cap_set(DMA_SLAVE, mask);
 
-       dma_dev = pci_get_bus_and_slot(2, PCI_DEVFN(0xa, 0)); /* Get DMA's dev
+       dma_dev = pci_get_bus_and_slot(priv->pdev->bus->number,
+                                      PCI_DEVFN(0xa, 0)); /* Get DMA's dev
                                                                information */
        /* Set Tx DMA */
        param = &priv->param_tx;
        param->dma_dev = &dma_dev->dev;
-       param->chan_id = priv->port.line;
+       param->chan_id = priv->port.line * 2; /* Tx = 0, 2, 4, ... */
+
        param->tx_reg = port->mapbase + UART_TX;
        chan = dma_request_channel(mask, filter, param);
        if (!chan) {
@@ -581,13 +624,15 @@ static void pch_request_dma(struct uart_port *port)
        /* Set Rx DMA */
        param = &priv->param_rx;
        param->dma_dev = &dma_dev->dev;
-       param->chan_id = priv->port.line + 1; /* Rx = Tx + 1 */
+       param->chan_id = priv->port.line * 2 + 1; /* Rx = Tx + 1 */
+
        param->rx_reg = port->mapbase + UART_RX;
        chan = dma_request_channel(mask, filter, param);
        if (!chan) {
                dev_err(priv->port.dev, "%s:dma_request_channel FAILS(Rx)\n",
                        __func__);
                dma_release_channel(priv->chan_tx);
+               priv->chan_tx = NULL;
                return;
        }
 
@@ -636,8 +681,7 @@ static void pch_dma_tx_complete(void *arg)
        priv->tx_dma_use = 0;
        priv->nent = 0;
        kfree(priv->sg_tx_p);
-       if (uart_circ_chars_pending(xmit))
-               pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
+       pch_uart_hal_enable_interrupt(priv, PCH_UART_HAL_TX_INT);
 }
 
 static int pop_tx(struct eg20t_port *priv, int size)
@@ -793,6 +837,14 @@ static unsigned int dma_handle_tx(struct eg20t_port *priv)
                return 0;
        }
 
+       if (priv->tx_dma_use) {
+               dev_dbg(priv->port.dev, "%s:Tx is not completed. (%lu)\n",
+                       __func__, jiffies);
+               pch_uart_hal_disable_interrupt(priv, PCH_UART_HAL_TX_INT);
+               priv->tx_empty = 1;
+               return 0;
+       }
+
        fifo_size = max(priv->fifo_size, 1);
        tx_empty = 1;
        if (pop_tx_x(priv, xmit->buf)) {
@@ -1168,8 +1220,7 @@ static void pch_uart_shutdown(struct uart_port *port)
                dev_err(priv->port.dev,
                        "pch_uart_hal_set_fifo Failed(ret=%d)\n", ret);
 
-       if (priv->use_dma_flag)
-               pch_free_dma(port);
+       pch_free_dma(port);
 
        free_irq(priv->port.irq, priv);
 }
@@ -1233,6 +1284,7 @@ static void pch_uart_set_termios(struct uart_port *port,
        if (rtn)
                goto out;
 
+       pch_uart_set_mctrl(&priv->port, priv->port.mctrl);
        /* Don't rewrite B0 */
        if (tty_termios_baud_rate(termios))
                tty_termios_encode_baud_rate(termios, baud, baud);
@@ -1351,8 +1403,12 @@ static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
        unsigned int mapbase;
        unsigned char *rxbuf;
        int fifosize, base_baud;
-       static int num;
-       int port_type = id->driver_data;
+       int port_type;
+       struct pch_uart_driver_data *board;
+       const char *board_name;
+
+       board = &drv_dat[id->driver_data];
+       port_type = board->port_type;
 
        priv = kzalloc(sizeof(struct eg20t_port), GFP_KERNEL);
        if (priv == NULL)
@@ -1362,20 +1418,28 @@ static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
        if (!rxbuf)
                goto init_port_free_txbuf;
 
+       base_baud = 1843200; /* 1.8432MHz */
+
+       /* quirk for CM-iTC board */
+       board_name = dmi_get_system_info(DMI_BOARD_NAME);
+       if (board_name && strstr(board_name, "CM-iTC"))
+               base_baud = 192000000; /* 192.0MHz */
+
        switch (port_type) {
        case PORT_UNKNOWN:
                fifosize = 256; /* EG20T/ML7213: UART0 */
-               base_baud = 1843200; /* 1.8432MHz */
                break;
        case PORT_8250:
                fifosize = 64; /* EG20T:UART1~3  ML7213: UART1~2*/
-               base_baud = 1843200; /* 1.8432MHz */
                break;
        default:
                dev_err(&pdev->dev, "Invalid Port Type(=%d)\n", port_type);
                goto init_port_hal_free;
        }
 
+       pci_enable_msi(pdev);
+       pci_set_master(pdev);
+
        iobase = pci_resource_start(pdev, 0);
        mapbase = pci_resource_start(pdev, 1);
        priv->mapbase = mapbase;
@@ -1397,7 +1461,7 @@ static struct eg20t_port *pch_uart_init_port(struct pci_dev *pdev,
        priv->port.ops = &pch_uart_ops;
        priv->port.flags = UPF_BOOT_AUTOCONF;
        priv->port.fifosize = fifosize;
-       priv->port.line = num++;
+       priv->port.line = board->line_no;
        priv->trigger = PCH_UART_HAL_TRIGGER_M;
 
        spin_lock_init(&priv->port.lock);
@@ -1432,6 +1496,8 @@ static void pch_uart_pci_remove(struct pci_dev *pdev)
        struct eg20t_port *priv;
 
        priv = (struct eg20t_port *)pci_get_drvdata(pdev);
+
+       pci_disable_msi(pdev);
        pch_uart_exit_port(priv);
        pci_disable_device(pdev);
        kfree(priv);
@@ -1475,19 +1541,27 @@ static int pch_uart_pci_resume(struct pci_dev *pdev)
 
 static DEFINE_PCI_DEVICE_TABLE(pch_uart_pci_id) = {
        {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8811),
-        .driver_data = PCH_UART_8LINE},
+        .driver_data = pch_et20t_uart0},
        {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8812),
-        .driver_data = PCH_UART_2LINE},
+        .driver_data = pch_et20t_uart1},
        {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8813),
-        .driver_data = PCH_UART_2LINE},
+        .driver_data = pch_et20t_uart2},
        {PCI_DEVICE(PCI_VENDOR_ID_INTEL, 0x8814),
-        .driver_data = PCH_UART_2LINE},
+        .driver_data = pch_et20t_uart3},
        {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8027),
-        .driver_data = PCH_UART_8LINE},
+        .driver_data = pch_ml7213_uart0},
        {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8028),
-        .driver_data = PCH_UART_2LINE},
+        .driver_data = pch_ml7213_uart1},
        {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8029),
-        .driver_data = PCH_UART_2LINE},
+        .driver_data = pch_ml7213_uart2},
+       {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800C),
+        .driver_data = pch_ml7223_uart0},
+       {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x800D),
+        .driver_data = pch_ml7223_uart1},
+       {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8811),
+        .driver_data = pch_ml7831_uart0},
+       {PCI_DEVICE(PCI_VENDOR_ID_ROHM, 0x8812),
+        .driver_data = pch_ml7831_uart1},
        {0,},
 };
 
@@ -1511,6 +1585,7 @@ static int __devinit pch_uart_pci_probe(struct pci_dev *pdev,
        return ret;
 
 probe_disable_device:
+       pci_disable_msi(pdev);
        pci_disable_device(pdev);
 probe_error:
        return ret;