Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6
[safe/jmp/linux-2.6] / drivers / net / irda / pxaficp_ir.c
index b3e1107..31794c2 100644 (file)
  *
  */
 #include <linux/module.h>
-#include <linux/types.h>
-#include <linux/init.h>
-#include <linux/errno.h>
 #include <linux/netdevice.h>
-#include <linux/slab.h>
-#include <linux/rtnetlink.h>
-#include <linux/interrupt.h>
-#include <linux/dma-mapping.h>
 #include <linux/platform_device.h>
-#include <linux/pm.h>
+#include <linux/clk.h>
 
 #include <net/irda/irda.h>
 #include <net/irda/irmod.h>
 #include <net/irda/wrapper.h>
 #include <net/irda/irda_device.h>
 
-#include <asm/irq.h>
-#include <asm/dma.h>
-#include <asm/delay.h>
-#include <asm/hardware.h>
-#include <asm/arch/irda.h>
-#include <asm/arch/pxa-regs.h>
-
-#ifdef CONFIG_MACH_MAINSTONE
-#include <asm/arch/mainstone.h>
+#include <mach/dma.h>
+#include <mach/irda.h>
+#include <mach/hardware.h>
+#include <mach/pxa-regs.h>
+#include <mach/regs-uart.h>
+
+#define FICP           __REG(0x40800000)  /* Start of FICP area */
+#define ICCR0          __REG(0x40800000)  /* ICP Control Register 0 */
+#define ICCR1          __REG(0x40800004)  /* ICP Control Register 1 */
+#define ICCR2          __REG(0x40800008)  /* ICP Control Register 2 */
+#define ICDR           __REG(0x4080000c)  /* ICP Data Register */
+#define ICSR0          __REG(0x40800014)  /* ICP Status Register 0 */
+#define ICSR1          __REG(0x40800018)  /* ICP Status Register 1 */
+
+#define ICCR0_AME      (1 << 7)        /* Address match enable */
+#define ICCR0_TIE      (1 << 6)        /* Transmit FIFO interrupt enable */
+#define ICCR0_RIE      (1 << 5)        /* Recieve FIFO interrupt enable */
+#define ICCR0_RXE      (1 << 4)        /* Receive enable */
+#define ICCR0_TXE      (1 << 3)        /* Transmit enable */
+#define ICCR0_TUS      (1 << 2)        /* Transmit FIFO underrun select */
+#define ICCR0_LBM      (1 << 1)        /* Loopback mode */
+#define ICCR0_ITR      (1 << 0)        /* IrDA transmission */
+
+#define ICCR2_RXP       (1 << 3)       /* Receive Pin Polarity select */
+#define ICCR2_TXP       (1 << 2)       /* Transmit Pin Polarity select */
+#define ICCR2_TRIG     (3 << 0)        /* Receive FIFO Trigger threshold */
+#define ICCR2_TRIG_8    (0 << 0)       /*      >= 8 bytes */
+#define ICCR2_TRIG_16   (1 << 0)       /*      >= 16 bytes */
+#define ICCR2_TRIG_32   (2 << 0)       /*      >= 32 bytes */
+
+#ifdef CONFIG_PXA27x
+#define ICSR0_EOC      (1 << 6)        /* DMA End of Descriptor Chain */
 #endif
+#define ICSR0_FRE      (1 << 5)        /* Framing error */
+#define ICSR0_RFS      (1 << 4)        /* Receive FIFO service request */
+#define ICSR0_TFS      (1 << 3)        /* Transnit FIFO service request */
+#define ICSR0_RAB      (1 << 2)        /* Receiver abort */
+#define ICSR0_TUR      (1 << 1)        /* Trunsmit FIFO underun */
+#define ICSR0_EIF      (1 << 0)        /* End/Error in FIFO */
+
+#define ICSR1_ROR      (1 << 6)        /* Receiver FIFO underrun  */
+#define ICSR1_CRE      (1 << 5)        /* CRC error */
+#define ICSR1_EOF      (1 << 4)        /* End of frame */
+#define ICSR1_TNF      (1 << 3)        /* Transmit FIFO not full */
+#define ICSR1_RNE      (1 << 2)        /* Receive FIFO not empty */
+#define ICSR1_TBY      (1 << 1)        /* Tramsmiter busy flag */
+#define ICSR1_RSY      (1 << 0)        /* Recevier synchronized flag */
 
 #define IrSR_RXPL_NEG_IS_ZERO (1<<4)
 #define IrSR_RXPL_POS_IS_ZERO 0x0
@@ -78,7 +108,6 @@ struct pxa_irda {
        int                     txdma;
        int                     rxdma;
 
-       struct net_device_stats stats;
        struct irlap_cb         *irlap;
        struct qos_info         qos;
 
@@ -87,8 +116,30 @@ struct pxa_irda {
 
        struct device           *dev;
        struct pxaficp_platform_data *pdata;
+       struct clk              *fir_clk;
+       struct clk              *sir_clk;
+       struct clk              *cur_clk;
 };
 
+static inline void pxa_irda_disable_clk(struct pxa_irda *si)
+{
+       if (si->cur_clk)
+               clk_disable(si->cur_clk);
+       si->cur_clk = NULL;
+}
+
+static inline void pxa_irda_enable_firclk(struct pxa_irda *si)
+{
+       si->cur_clk = si->fir_clk;
+       clk_enable(si->fir_clk);
+}
+
+static inline void pxa_irda_enable_sirclk(struct pxa_irda *si)
+{
+       si->cur_clk = si->sir_clk;
+       clk_enable(si->sir_clk);
+}
+
 
 #define IS_FIR(si)             ((si)->speed >= 4000000)
 #define IRDA_FRAME_SIZE_LIMIT  2047
@@ -134,17 +185,13 @@ static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
                        DCSR(si->rxdma) &= ~DCSR_RUN;
                        /* disable FICP */
                        ICCR0 = 0;
-                       pxa_set_cken(CKEN13_FICP, 0);
+                       pxa_irda_disable_clk(si);
 
                        /* set board transceiver to SIR mode */
                        si->pdata->transceiver_mode(si->dev, IR_SIRMODE);
 
-                       /* configure GPIO46/47 */
-                       pxa_gpio_mode(GPIO46_STRXD_MD);
-                       pxa_gpio_mode(GPIO47_STTXD_MD);
-
                        /* enable the STUART clock */
-                       pxa_set_cken(CKEN5_STUART, 1);
+                       pxa_irda_enable_sirclk(si);
                }
 
                /* disable STUART first */
@@ -169,7 +216,7 @@ static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
                /* disable STUART */
                STIER = 0;
                STISR = 0;
-               pxa_set_cken(CKEN5_STUART, 0);
+               pxa_irda_disable_clk(si);
 
                /* disable FICP first */
                ICCR0 = 0;
@@ -177,12 +224,8 @@ static int pxa_irda_set_speed(struct pxa_irda *si, int speed)
                /* set board transceiver to FIR mode */
                si->pdata->transceiver_mode(si->dev, IR_FIRMODE);
 
-               /* configure GPIO46/47 */
-               pxa_gpio_mode(GPIO46_ICPRXD_MD);
-               pxa_gpio_mode(GPIO47_ICPTXD_MD);
-
                /* enable the FICP clock */
-               pxa_set_cken(CKEN13_FICP, 1);
+               pxa_irda_enable_firclk(si);
 
                si->speed = speed;
                pxa_irda_fir_dma_rx_start(si);
@@ -214,18 +257,18 @@ static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
                        data = STRBR;
                        if (lsr & (LSR_OE | LSR_PE | LSR_FE | LSR_BI)) {
                                printk(KERN_DEBUG "pxa_ir: sir receiving error\n");
-                               si->stats.rx_errors++;
+                               dev->stats.rx_errors++;
                                if (lsr & LSR_FE)
-                                       si->stats.rx_frame_errors++;
+                                       dev->stats.rx_frame_errors++;
                                if (lsr & LSR_OE)
-                                       si->stats.rx_fifo_errors++;
+                                       dev->stats.rx_fifo_errors++;
                        } else {
-                               si->stats.rx_bytes++;
-                               async_unwrap_char(dev, &si->stats, &si->rx_buff, data);
+                               dev->stats.rx_bytes++;
+                               async_unwrap_char(dev, &dev->stats,
+                                                 &si->rx_buff, data);
                        }
                        lsr = STLSR;
                }
-               dev->last_rx = jiffies;
                si->last_oscr = OSCR;
                break;
 
@@ -234,10 +277,9 @@ static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
 
        case 0x0C: /* Character Timeout Indication */
                do  {
-                   si->stats.rx_bytes++;
-                   async_unwrap_char(dev, &si->stats, &si->rx_buff, STRBR);
+                   dev->stats.rx_bytes++;
+                   async_unwrap_char(dev, &dev->stats, &si->rx_buff, STRBR);
                } while (STLSR & LSR_DR);
-               dev->last_rx = jiffies;
                si->last_oscr = OSCR;
                break;
 
@@ -248,9 +290,8 @@ static irqreturn_t pxa_irda_sir_irq(int irq, void *dev_id)
                }
 
                if (si->tx_buff.len == 0) {
-                       si->stats.tx_packets++;
-                       si->stats.tx_bytes += si->tx_buff.data -
-                                             si->tx_buff.head;
+                       dev->stats.tx_packets++;
+                       dev->stats.tx_bytes += si->tx_buff.data - si->tx_buff.head;
 
                         /* We need to ensure that the transmitter has finished. */
                        while ((STLSR & LSR_TEMT) == 0)
@@ -301,10 +342,10 @@ static void pxa_irda_fir_dma_tx_irq(int channel, void *data)
        DCSR(channel) = dcsr & ~DCSR_RUN;
 
        if (dcsr & DCSR_ENDINTR)  {
-               si->stats.tx_packets++;
-               si->stats.tx_bytes += si->dma_tx_buff_len;
+               dev->stats.tx_packets++;
+               dev->stats.tx_bytes += si->dma_tx_buff_len;
        } else {
-               si->stats.tx_errors++;
+               dev->stats.tx_errors++;
        }
 
        while (ICSR1 & ICSR1_TBY)
@@ -350,14 +391,14 @@ static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, in
                data = ICDR;
 
                if (stat & (ICSR1_CRE | ICSR1_ROR)) {
-                       si->stats.rx_errors++;
+                       dev->stats.rx_errors++;
                        if (stat & ICSR1_CRE) {
                                printk(KERN_DEBUG "pxa_ir: fir receive CRC error\n");
-                               si->stats.rx_crc_errors++;
+                               dev->stats.rx_crc_errors++;
                        }
                        if (stat & ICSR1_ROR) {
                                printk(KERN_DEBUG "pxa_ir: fir receive overrun\n");
-                               si->stats.rx_over_errors++;
+                               dev->stats.rx_over_errors++;
                        }
                } else  {
                        si->dma_rx_buff[len++] = data;
@@ -373,20 +414,20 @@ static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, in
 
                if (icsr0 & ICSR0_FRE) {
                        printk(KERN_ERR "pxa_ir: dropping erroneous frame\n");
-                       si->stats.rx_dropped++;
+                       dev->stats.rx_dropped++;
                        return;
                }
 
                skb = alloc_skb(len+1,GFP_ATOMIC);
                if (!skb)  {
                        printk(KERN_ERR "pxa_ir: fir out of memory for receive skb\n");
-                       si->stats.rx_dropped++;
+                       dev->stats.rx_dropped++;
                        return;
                }
 
                /* Align IP header to 20 bytes  */
                skb_reserve(skb, 1);
-               memcpy(skb->data, si->dma_rx_buff, len);
+               skb_copy_to_linear_data(skb, si->dma_rx_buff, len);
                skb_put(skb, len);
 
                /* Feed it to IrLAP  */
@@ -395,10 +436,8 @@ static void pxa_irda_fir_irq_eif(struct pxa_irda *si, struct net_device *dev, in
                skb->protocol = htons(ETH_P_IRDA);
                netif_rx(skb);
 
-               si->stats.rx_packets++;
-               si->stats.rx_bytes += len;
-
-               dev->last_rx = jiffies;
+               dev->stats.rx_packets++;
+               dev->stats.rx_bytes += len;
        }
 }
 
@@ -417,10 +456,10 @@ static irqreturn_t pxa_irda_fir_irq(int irq, void *dev_id)
        if (icsr0 & (ICSR0_FRE | ICSR0_RAB)) {
                if (icsr0 & ICSR0_FRE) {
                        printk(KERN_DEBUG "pxa_ir: fir receive frame error\n");
-                       si->stats.rx_frame_errors++;
+                       dev->stats.rx_frame_errors++;
                } else {
                        printk(KERN_DEBUG "pxa_ir: fir receive abort\n");
-                       si->stats.rx_errors++;
+                       dev->stats.rx_errors++;
                }
                ICSR0 = icsr0 & (ICSR0_FRE | ICSR0_RAB);
        }
@@ -549,12 +588,6 @@ static int pxa_irda_ioctl(struct net_device *dev, struct ifreq *ifreq, int cmd)
        return ret;
 }
 
-static struct net_device_stats *pxa_irda_stats(struct net_device *dev)
-{
-       struct pxa_irda *si = netdev_priv(dev);
-       return &si->stats;
-}
-
 static void pxa_irda_startup(struct pxa_irda *si)
 {
        /* Disable STUART interrupts */
@@ -572,8 +605,8 @@ static void pxa_irda_startup(struct pxa_irda *si)
        ICCR2 = ICCR2_TXP | ICCR2_TRIG_32;
 
        /* configure DMAC */
-       DRCMR17 = si->rxdma | DRCMR_MAPVLD;
-       DRCMR18 = si->txdma | DRCMR_MAPVLD;
+       DRCMR(17) = si->rxdma | DRCMR_MAPVLD;
+       DRCMR(18) = si->txdma | DRCMR_MAPVLD;
 
        /* force SIR reinitialization */
        si->speed = 4000000;
@@ -592,19 +625,18 @@ static void pxa_irda_shutdown(struct pxa_irda *si)
        STIER = 0;
        /* disable STUART SIR mode */
        STISR = 0;
-       /* disable the STUART clock */
-       pxa_set_cken(CKEN5_STUART, 0);
 
        /* disable DMA */
        DCSR(si->txdma) &= ~DCSR_RUN;
        DCSR(si->rxdma) &= ~DCSR_RUN;
        /* disable FICP */
        ICCR0 = 0;
-       /* disable the FICP clock */
-       pxa_set_cken(CKEN13_FICP, 0);
 
-       DRCMR17 = 0;
-       DRCMR18 = 0;
+       /* disable the STUART or FICP clocks */
+       pxa_irda_disable_clk(si);
+
+       DRCMR(17) = 0;
+       DRCMR(18) = 0;
 
        local_irq_restore(flags);
 
@@ -792,6 +824,13 @@ static int pxa_irda_probe(struct platform_device *pdev)
        si->dev = &pdev->dev;
        si->pdata = pdev->dev.platform_data;
 
+       si->sir_clk = clk_get(&pdev->dev, "UARTCLK");
+       si->fir_clk = clk_get(&pdev->dev, "FICPCLK");
+       if (IS_ERR(si->sir_clk) || IS_ERR(si->fir_clk)) {
+               err = PTR_ERR(IS_ERR(si->sir_clk) ? si->sir_clk : si->fir_clk);
+               goto err_mem_4;
+       }
+
        /*
         * Initialise the SIR buffers
         */
@@ -802,11 +841,15 @@ static int pxa_irda_probe(struct platform_device *pdev)
        if (err)
                goto err_mem_5;
 
+       if (si->pdata->startup)
+               err = si->pdata->startup(si->dev);
+       if (err)
+               goto err_startup;
+
        dev->hard_start_xmit    = pxa_irda_hard_xmit;
        dev->open               = pxa_irda_start;
        dev->stop               = pxa_irda_stop;
        dev->do_ioctl           = pxa_irda_ioctl;
-       dev->get_stats          = pxa_irda_stats;
 
        irda_init_max_qos_capabilies(&si->qos);
 
@@ -827,10 +870,17 @@ static int pxa_irda_probe(struct platform_device *pdev)
                dev_set_drvdata(&pdev->dev, dev);
 
        if (err) {
+               if (si->pdata->shutdown)
+                       si->pdata->shutdown(si->dev);
+err_startup:
                kfree(si->tx_buff.head);
 err_mem_5:
                kfree(si->rx_buff.head);
 err_mem_4:
+               if (si->sir_clk && !IS_ERR(si->sir_clk))
+                       clk_put(si->sir_clk);
+               if (si->fir_clk && !IS_ERR(si->fir_clk))
+                       clk_put(si->fir_clk);
                free_netdev(dev);
 err_mem_3:
                release_mem_region(__PREG(FICP), 0x1c);
@@ -848,8 +898,12 @@ static int pxa_irda_remove(struct platform_device *_dev)
        if (dev) {
                struct pxa_irda *si = netdev_priv(dev);
                unregister_netdev(dev);
+               if (si->pdata->shutdown)
+                       si->pdata->shutdown(si->dev);
                kfree(si->tx_buff.head);
                kfree(si->rx_buff.head);
+               clk_put(si->fir_clk);
+               clk_put(si->sir_clk);
                free_netdev(dev);
        }
 
@@ -862,6 +916,7 @@ static int pxa_irda_remove(struct platform_device *_dev)
 static struct platform_driver pxa_ir_driver = {
        .driver         = {
                .name   = "pxa2xx-ir",
+               .owner  = THIS_MODULE,
        },
        .probe          = pxa_irda_probe,
        .remove         = pxa_irda_remove,
@@ -883,3 +938,4 @@ module_init(pxa_irda_init);
 module_exit(pxa_irda_exit);
 
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:pxa2xx-ir");