]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blobdiff - drivers/usb/gadget/omap_udc.c
h63xx: usb gadget driver support for allowing network over usb.
[linux-2.6-omap-h63xx.git] / drivers / usb / gadget / omap_udc.c
index 387692a3611e7119560b606611e75d1ac881285f..975a54fb0dc475ff1ad3a1a71801490b53981fa1 100644 (file)
@@ -43,6 +43,7 @@
 #include <linux/usb_gadget.h>
 #include <linux/usb_otg.h>
 #include <linux/dma-mapping.h>
+#include <linux/clk.h>
 
 #include <asm/byteorder.h>
 #include <asm/io.h>
 #undef USB_TRACE
 
 /* bulk DMA seems to be behaving for both IN and OUT */
+#ifdef CONFIG_MACH_OMAP_H6300
+#undef USE_DMA
+#else
 #define        USE_DMA
+#endif
 
 /* ISO too */
 #define        USE_ISO
@@ -544,9 +549,9 @@ static inline dma_addr_t dma_csac(unsigned lch)
        /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
         * read before the DMA controller finished disabling the channel.
         */
-       csac = omap_readw(OMAP_DMA_CSAC(lch));
+       csac = OMAP_DMA_CSAC_REG(lch);
        if (csac == 0)
-               csac = omap_readw(OMAP_DMA_CSAC(lch));
+               csac = OMAP_DMA_CSAC_REG(lch);
        return csac;
 }
 
@@ -557,9 +562,9 @@ static inline dma_addr_t dma_cdac(unsigned lch)
        /* omap 3.2/3.3 erratum: sometimes 0 is returned if CSAC/CDAC is
         * read before the DMA controller finished disabling the channel.
         */
-       cdac = omap_readw(OMAP_DMA_CDAC(lch));
+       cdac = OMAP_DMA_CDAC_REG(lch);
        if (cdac == 0)
-               cdac = omap_readw(OMAP_DMA_CDAC(lch));
+               cdac = OMAP_DMA_CDAC_REG(lch);
        return cdac;
 }
 
@@ -584,7 +589,7 @@ static u16 dma_src_len(struct omap_ep *ep, dma_addr_t start)
 }
 
 #define DMA_DEST_LAST(x) (cpu_is_omap15xx() \
-               ? omap_readw(OMAP_DMA_CSAC(x)) /* really: CPC */ \
+               ? OMAP_DMA_CSAC_REG(x) /* really: CPC */ \
                : dma_cdac(x))
 
 static u16 dma_dest_len(struct omap_ep *ep, dma_addr_t start)
@@ -622,17 +627,19 @@ static void next_in_dma(struct omap_ep *ep, struct omap_req *req)
                        || (cpu_is_omap15xx() && length < ep->maxpacket)) {
                txdma_ctrl = UDC_TXN_EOT | length;
                omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S8,
-                               length, 1, sync_mode);
+                               length, 1, sync_mode, 0, 0);
        } else {
                length = min(length / ep->maxpacket,
                                (unsigned) UDC_TXN_TSC + 1);
                txdma_ctrl = length;
                omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
-                               ep->ep.maxpacket >> 1, length, sync_mode);
+                               ep->ep.maxpacket >> 1, length, sync_mode,
+                               0, 0);
                length *= ep->maxpacket;
        }
        omap_set_dma_src_params(ep->lch, OMAP_DMA_PORT_EMIFF,
-               OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
+               OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
+               0, 0);
 
        omap_start_dma(ep->lch);
        ep->dma_counter = dma_csac(ep->lch);
@@ -677,9 +684,11 @@ static void next_out_dma(struct omap_ep *ep, struct omap_req *req)
        req->dma_bytes = packets * ep->ep.maxpacket;
        omap_set_dma_transfer_params(ep->lch, OMAP_DMA_DATA_TYPE_S16,
                        ep->ep.maxpacket >> 1, packets,
-                       OMAP_DMA_SYNC_ELEMENT);
+                       OMAP_DMA_SYNC_ELEMENT,
+                       0, 0);
        omap_set_dma_dest_params(ep->lch, OMAP_DMA_PORT_EMIFF,
-               OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual);
+               OMAP_DMA_AMODE_POST_INC, req->req.dma + req->req.actual,
+               0, 0);
        ep->dma_counter = DMA_DEST_LAST(ep->lch);
 
        UDC_RXDMA_REG(ep->dma_channel) = UDC_RXN_STOP | (packets - 1);
@@ -822,7 +831,8 @@ static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
                        omap_set_dma_dest_params(ep->lch,
                                OMAP_DMA_PORT_TIPB,
                                OMAP_DMA_AMODE_CONSTANT,
-                               (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
+                               (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
+                               0, 0);
                }
        } else {
                status = omap_request_dma(OMAP_DMA_USB_W2FC_RX0 - 1 + channel,
@@ -833,7 +843,8 @@ static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
                        omap_set_dma_src_params(ep->lch,
                                OMAP_DMA_PORT_TIPB,
                                OMAP_DMA_AMODE_CONSTANT,
-                               (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG));
+                               (unsigned long) io_v2p((u32)&UDC_DATA_DMA_REG),
+                               0, 0);
                        /* EMIFF */
                        omap_set_dma_dest_burst_mode(ep->lch,
                                                OMAP_DMA_DATA_BURST_4);
@@ -848,7 +859,7 @@ static void dma_channel_claim(struct omap_ep *ep, unsigned channel)
 
                /* channel type P: hw synch (fifo) */
                if (!cpu_is_omap15xx())
-                       omap_writew(2, OMAP_DMA_LCH_CTRL(ep->lch));
+                       OMAP1_DMA_LCH_CTRL_REG(ep->lch) = 2;
        }
 
 just_restart:
@@ -895,7 +906,7 @@ static void dma_channel_release(struct omap_ep *ep)
        else
                req = NULL;
 
-       active = ((1 << 7) & omap_readl(OMAP_DMA_CCR(ep->lch))) != 0;
+       active = ((1 << 7) & OMAP_DMA_CCR_REG(ep->lch)) != 0;
 
        DBG("%s release %s %cxdma%d %p\n", ep->ep.name,
                        active ? "active" : "idle",
@@ -1300,6 +1311,23 @@ static void pullup_disable(struct omap_udc *udc)
        UDC_SYSCON1_REG &= ~UDC_PULLUP_EN;
 }
 
+static struct omap_udc *udc;
+
+static void omap_udc_enable_clock(int enable)
+{
+       if (udc == NULL || udc->dc_clk == NULL || udc->hhc_clk == NULL)
+               return;
+
+       if (enable) {
+               clk_enable(udc->dc_clk);
+               clk_enable(udc->hhc_clk);
+               udelay(100);
+       } else {
+               clk_disable(udc->hhc_clk);
+               clk_disable(udc->dc_clk);
+       }
+}
+
 /*
  * Called by whatever detects VBUS sessions:  external transceiver
  * driver, or maybe GPIO0 VBUS IRQ.  May request 48 MHz clock.
@@ -1320,10 +1348,22 @@ static int omap_vbus_session(struct usb_gadget *gadget, int is_active)
                else
                        FUNC_MUX_CTRL_0_REG &= ~VBUS_CTRL_1510;
        }
+       if (udc->dc_clk != NULL && is_active) {
+               if (!udc->clk_requested) {
+                       omap_udc_enable_clock(1);
+                       udc->clk_requested = 1;
+               }
+       }
        if (can_pullup(udc))
                pullup_enable(udc);
        else
                pullup_disable(udc);
+       if (udc->dc_clk != NULL && !is_active) {
+               if (udc->clk_requested) {
+                       omap_udc_enable_clock(0);
+                       udc->clk_requested = 0;
+               }
+       }
        spin_unlock_irqrestore(&udc->lock, flags);
        return 0;
 }
@@ -1869,7 +1909,7 @@ static void pio_out_timer(unsigned long _ep)
 
        spin_lock_irqsave(&ep->udc->lock, flags);
        if (!list_empty(&ep->queue) && ep->ackwait) {
-               use_ep(ep, 0);
+               use_ep(ep, UDC_EP_SEL);
                stat_flg = UDC_STAT_FLG_REG;
 
                if ((stat_flg & UDC_ACK) && (!(stat_flg & UDC_FIFO_EN)
@@ -1879,12 +1919,14 @@ static void pio_out_timer(unsigned long _ep)
                        VDBG("%s: lose, %04x\n", ep->ep.name, stat_flg);
                        req = container_of(ep->queue.next,
                                        struct omap_req, queue);
-                       UDC_EP_NUM_REG = ep->bEndpointAddress | UDC_EP_SEL;
                        (void) read_fifo(ep, req);
                        UDC_EP_NUM_REG = ep->bEndpointAddress;
                        UDC_CTRL_REG = UDC_SET_FIFO_EN;
                        ep->ackwait = 1 + ep->double_buf;
                }
+               else {
+                   deselect_ep();
+               }
        }
        mod_timer(&ep->timer, PIO_OUT_TIMEOUT);
        spin_unlock_irqrestore(&ep->udc->lock, flags);
@@ -2033,7 +2075,6 @@ omap_udc_iso_irq(int irq, void *_dev, struct pt_regs *r)
 
 /*-------------------------------------------------------------------------*/
 
-static struct omap_udc *udc;
 
 int usb_gadget_register_driver (struct usb_gadget_driver *driver)
 {
@@ -2076,6 +2117,9 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
        udc->gadget.dev.driver = &driver->driver;
        spin_unlock_irqrestore(&udc->lock, flags);
 
+       if (udc->dc_clk != NULL)
+               omap_udc_enable_clock(1);
+
        status = driver->bind (&udc->gadget);
        if (status) {
                DBG("bind to %s --> %d\n", driver->driver.name, status);
@@ -2107,10 +2151,12 @@ int usb_gadget_register_driver (struct usb_gadget_driver *driver)
        /* boards that don't have VBUS sensing can't autogate 48MHz;
         * can't enter deep sleep while a gadget driver is active.
         */
-       if (machine_is_omap_innovator() || machine_is_omap_osk())
+       if (machine_is_omap_innovator() || machine_is_omap_osk() || machine_is_omap_h6300())
                omap_vbus_session(&udc->gadget, 1);
 
 done:
+       if (udc->dc_clk != NULL)
+               omap_udc_enable_clock(0);
        return status;
 }
 EXPORT_SYMBOL(usb_gadget_register_driver);
@@ -2125,7 +2171,10 @@ int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
        if (!driver || driver != udc->driver)
                return -EINVAL;
 
-       if (machine_is_omap_innovator() || machine_is_omap_osk())
+       if (udc->dc_clk != NULL)
+               omap_udc_enable_clock(1);
+
+       if (machine_is_omap_innovator() || machine_is_omap_osk() || machine_is_omap_h6300())
                omap_vbus_session(&udc->gadget, 0);
 
        if (udc->transceiver)
@@ -2141,6 +2190,8 @@ int usb_gadget_unregister_driver (struct usb_gadget_driver *driver)
        udc->gadget.dev.driver = NULL;
        udc->driver = NULL;
 
+       if (udc->dc_clk != NULL)
+               omap_udc_enable_clock(0);
        DBG("unregistered driver '%s'\n", driver->driver.name);
        return status;
 }
@@ -2707,23 +2758,34 @@ omap_udc_setup(struct platform_device *odev, struct otg_transceiver *xceiv)
        return 0;
 }
 
-static int __init omap_udc_probe(struct device *dev)
+static int __init omap_udc_probe(struct platform_device *pdev)
 {
-       struct platform_device  *odev = to_platform_device(dev);
        int                     status = -ENODEV;
        int                     hmc;
        struct otg_transceiver  *xceiv = NULL;
        const char              *type = NULL;
-       struct omap_usb_config  *config = dev->platform_data;
+       struct omap_usb_config  *config = pdev->dev.platform_data;
+       struct clk              *dc_clk;
+       struct clk              *hhc_clk;
 
        /* NOTE:  "knows" the order of the resources! */
-       if (!request_mem_region(odev->resource[0].start, 
-                       odev->resource[0].end - odev->resource[0].start + 1,
+       if (!request_mem_region(pdev->resource[0].start, 
+                       pdev->resource[0].end - pdev->resource[0].start + 1,
                        driver_name)) {
                DBG("request_mem_region failed\n");
                return -EBUSY;
        }
 
+       if (cpu_is_omap16xx()) {
+               dc_clk = clk_get(&pdev->dev, "usb_dc_ck");
+               hhc_clk = clk_get(&pdev->dev, "usb_hhc_ck");
+               BUG_ON(IS_ERR(dc_clk) || IS_ERR(hhc_clk));
+               /* can't use omap_udc_enable_clock yet */
+               clk_enable(dc_clk);
+               clk_enable(hhc_clk);
+               udelay(100);
+       }
+
        INFO("OMAP UDC rev %d.%d%s\n",
                UDC_REV_REG >> 4, UDC_REV_REG & 0xf,
                config->otg ? ", Mini-AB" : "");
@@ -2733,7 +2795,7 @@ static int __init omap_udc_probe(struct device *dev)
                hmc = HMC_1510;
                type = "(unknown)";
 
-               if (machine_is_omap_innovator()) {
+               if (machine_is_omap_innovator() || machine_is_omap_h6300()) {
                        /* just set up software VBUS detect, and then
                         * later rig it so we always report VBUS.
                         * FIXME without really sensing VBUS, we can't
@@ -2803,7 +2865,7 @@ bad_on_1710:
        INFO("hmc mode %d, %s transceiver\n", hmc, type);
 
        /* a "gadget" abstracts/virtualizes the controller */
-       status = omap_udc_setup(odev, xceiv);
+       status = omap_udc_setup(pdev, xceiv);
        if (status) {
                goto cleanup0;
        }
@@ -2821,31 +2883,37 @@ bad_on_1710:
                udc->clr_halt = UDC_RESET_EP;
 
        /* USB general purpose IRQ:  ep0, state changes, dma, etc */
-       status = request_irq(odev->resource[1].start, omap_udc_irq,
+       status = request_irq(pdev->resource[1].start, omap_udc_irq,
                        SA_SAMPLE_RANDOM, driver_name, udc);
        if (status != 0) {
                ERR( "can't get irq %ld, err %d\n",
-                       odev->resource[1].start, status);
+                       pdev->resource[1].start, status);
                goto cleanup1;
        }
 
        /* USB "non-iso" IRQ (PIO for all but ep0) */
-       status = request_irq(odev->resource[2].start, omap_udc_pio_irq,
+       status = request_irq(pdev->resource[2].start, omap_udc_pio_irq,
                        SA_SAMPLE_RANDOM, "omap_udc pio", udc);
        if (status != 0) {
                ERR( "can't get irq %ld, err %d\n",
-                       odev->resource[2].start, status);
+                       pdev->resource[2].start, status);
                goto cleanup2;
        }
 #ifdef USE_ISO
-       status = request_irq(odev->resource[3].start, omap_udc_iso_irq,
+       status = request_irq(pdev->resource[3].start, omap_udc_iso_irq,
                        SA_INTERRUPT, "omap_udc iso", udc);
        if (status != 0) {
                ERR("can't get irq %ld, err %d\n",
-                       odev->resource[3].start, status);
+                       pdev->resource[3].start, status);
                goto cleanup3;
        }
 #endif
+       if (cpu_is_omap16xx()) {
+               udc->dc_clk = dc_clk;
+               udc->hhc_clk = hhc_clk;
+               clk_disable(hhc_clk);
+               clk_disable(dc_clk);
+       }
 
        create_proc_file();
        device_add(&udc->gadget.dev);
@@ -2853,11 +2921,11 @@ bad_on_1710:
 
 #ifdef USE_ISO
 cleanup3:
-       free_irq(odev->resource[2].start, udc);
+       free_irq(pdev->resource[2].start, udc);
 #endif
 
 cleanup2:
-       free_irq(odev->resource[1].start, udc);
+       free_irq(pdev->resource[1].start, udc);
 
 cleanup1:
        kfree (udc);
@@ -2866,14 +2934,22 @@ cleanup1:
 cleanup0:
        if (xceiv)
                put_device(xceiv->dev);
-       release_mem_region(odev->resource[0].start,
-                       odev->resource[0].end - odev->resource[0].start + 1);
+
+       if (cpu_is_omap16xx()) {
+               clk_disable(hhc_clk);
+               clk_disable(dc_clk);
+               clk_put(hhc_clk);
+               clk_put(dc_clk);
+       }
+
+       release_mem_region(pdev->resource[0].start,
+                       pdev->resource[0].end - pdev->resource[0].start + 1);
+
        return status;
 }
 
-static int __exit omap_udc_remove(struct device *dev)
+static int __exit omap_udc_remove(struct platform_device *pdev)
 {
-       struct platform_device  *odev = to_platform_device(dev);
        DECLARE_COMPLETION(done);
 
        if (!udc)
@@ -2891,13 +2967,20 @@ static int __exit omap_udc_remove(struct device *dev)
        remove_proc_file();
 
 #ifdef USE_ISO
-       free_irq(odev->resource[3].start, udc);
+       free_irq(pdev->resource[3].start, udc);
 #endif
-       free_irq(odev->resource[2].start, udc);
-       free_irq(odev->resource[1].start, udc);
+       free_irq(pdev->resource[2].start, udc);
+       free_irq(pdev->resource[1].start, udc);
+
+       if (udc->dc_clk) {
+               if (udc->clk_requested)
+                       omap_udc_enable_clock(0);
+               clk_put(udc->hhc_clk);
+               clk_put(udc->dc_clk);
+       }
 
-       release_mem_region(odev->resource[0].start,
-                       odev->resource[0].end - odev->resource[0].start + 1);
+       release_mem_region(pdev->resource[0].start,
+                       pdev->resource[0].end - pdev->resource[0].start + 1);
 
        device_unregister(&udc->gadget.dev);
        wait_for_completion(&done);
@@ -2915,7 +2998,7 @@ static int __exit omap_udc_remove(struct device *dev)
  * may involve talking to an external transceiver (e.g. isp1301).
  */
 
-static int omap_udc_suspend(struct device *dev, pm_message_t message)
+static int omap_udc_suspend(struct platform_device *dev, pm_message_t message)
 {
        u32     devstat;
 
@@ -2935,7 +3018,7 @@ static int omap_udc_suspend(struct device *dev, pm_message_t message)
        return 0;
 }
 
-static int omap_udc_resume(struct device *dev)
+static int omap_udc_resume(struct platform_device *dev)
 {
        DBG("resume + wakeup/SRP\n");
        omap_pullup(&udc->gadget, 1);
@@ -2947,14 +3030,15 @@ static int omap_udc_resume(struct device *dev)
 
 /*-------------------------------------------------------------------------*/
 
-static struct device_driver udc_driver = {
-       .name           = (char *) driver_name,
-       .owner          = THIS_MODULE,
-       .bus            = &platform_bus_type,
+static struct platform_driver udc_driver = {
        .probe          = omap_udc_probe,
        .remove         = __exit_p(omap_udc_remove),
        .suspend        = omap_udc_suspend,
        .resume         = omap_udc_resume,
+       .driver         = {
+               .owner  = THIS_MODULE,
+               .name   = (char *) driver_name,
+       },
 };
 
 static int __init udc_init(void)
@@ -2965,13 +3049,13 @@ static int __init udc_init(void)
 #endif
                "%s\n", driver_desc,
                use_dma ?  " (dma)" : "");
-       return driver_register(&udc_driver);
+       return platform_driver_register(&udc_driver);
 }
 module_init(udc_init);
 
 static void __exit udc_exit(void)
 {
-       driver_unregister(&udc_driver);
+       platform_driver_unregister(&udc_driver);
 }
 module_exit(udc_exit);