ARM: OMAP: DMA: Remove __REG access
[safe/jmp/linux-2.6] / drivers / usb / gadget / atmel_usba_udc.c
index 33a797b..07e5a0b 100644 (file)
@@ -18,6 +18,7 @@
 #include <linux/platform_device.h>
 #include <linux/usb/ch9.h>
 #include <linux/usb/gadget.h>
+#include <linux/usb/atmel_usba_udc.h>
 #include <linux/delay.h>
 
 #include <asm/gpio.h>
@@ -27,6 +28,7 @@
 
 
 static struct usba_udc the_udc;
+static struct usba_ep *usba_ep;
 
 #ifdef CONFIG_USB_GADGET_DEBUG_FS
 #include <linux/debugfs.h>
@@ -324,6 +326,28 @@ static int vbus_is_present(struct usba_udc *udc)
        return 1;
 }
 
+#if defined(CONFIG_AVR32)
+
+static void toggle_bias(int is_on)
+{
+}
+
+#elif defined(CONFIG_ARCH_AT91)
+
+#include <asm/arch/at91_pmc.h>
+
+static void toggle_bias(int is_on)
+{
+       unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR);
+
+       if (is_on)
+               at91_sys_write(AT91_CKGR_UCKR, uckr | AT91_PMC_BIASEN);
+       else
+               at91_sys_write(AT91_CKGR_UCKR, uckr & ~(AT91_PMC_BIASEN));
+}
+
+#endif /* CONFIG_ARCH_AT91 */
+
 static void next_fifo_transaction(struct usba_ep *ep, struct usba_request *req)
 {
        unsigned int transaction_len;
@@ -625,7 +649,13 @@ static int usba_ep_disable(struct usb_ep *_ep)
 
        if (!ep->desc) {
                spin_unlock_irqrestore(&udc->lock, flags);
-               DBG(DBG_ERR, "ep_disable: %s not enabled\n", ep->ep.name);
+               /* REVISIT because this driver disables endpoints in
+                * reset_all_endpoints() before calling disconnect(),
+                * most gadget drivers would trigger this non-error ...
+                */
+               if (udc->gadget.speed != USB_SPEED_UNKNOWN)
+                       DBG(DBG_ERR, "ep_disable: %s not enabled\n",
+                                       ep->ep.name);
                return -EINVAL;
        }
        ep->desc = NULL;
@@ -982,33 +1012,6 @@ static const struct usb_gadget_ops usba_udc_ops = {
        .set_selfpowered        = usba_udc_set_selfpowered,
 };
 
-#define EP(nam, idx, maxpkt, maxbk, dma, isoc)                 \
-{                                                              \
-       .ep     = {                                             \
-               .ops            = &usba_ep_ops,                 \
-               .name           = nam,                          \
-               .maxpacket      = maxpkt,                       \
-       },                                                      \
-       .udc            = &the_udc,                             \
-       .queue          = LIST_HEAD_INIT(usba_ep[idx].queue),   \
-       .fifo_size      = maxpkt,                               \
-       .nr_banks       = maxbk,                                \
-       .index          = idx,                                  \
-       .can_dma        = dma,                                  \
-       .can_isoc       = isoc,                                 \
-}
-
-static struct usba_ep usba_ep[] = {
-       EP("ep0", 0, 64, 1, 0, 0),
-       EP("ep1in-bulk", 1, 512, 2, 1, 1),
-       EP("ep2out-bulk", 2, 512, 2, 1, 1),
-       EP("ep3in-int", 3, 64, 3, 1, 0),
-       EP("ep4out-int", 4, 64, 3, 1, 0),
-       EP("ep5in-iso", 5, 1024, 3, 1, 1),
-       EP("ep6out-iso", 6, 1024, 3, 1, 1),
-};
-#undef EP
-
 static struct usb_endpoint_descriptor usba_ep0_desc = {
        .bLength = USB_DT_ENDPOINT_SIZE,
        .bDescriptorType = USB_DT_ENDPOINT,
@@ -1027,7 +1030,6 @@ static void nop_release(struct device *dev)
 static struct usba_udc the_udc = {
        .gadget = {
                .ops            = &usba_udc_ops,
-               .ep0            = &usba_ep[0].ep,
                .ep_list        = LIST_HEAD_INIT(the_udc.gadget.ep_list),
                .is_dualspeed   = 1,
                .name           = "atmel_usba_udc",
@@ -1036,8 +1038,6 @@ static struct usba_udc the_udc = {
                        .release        = nop_release,
                },
        },
-
-       .lock   = SPIN_LOCK_UNLOCKED,
 };
 
 /*
@@ -1056,6 +1056,12 @@ static void reset_all_endpoints(struct usba_udc *udc)
                request_complete(ep, req, -ECONNRESET);
        }
 
+       /* NOTE:  normally, the next call to the gadget driver is in
+        * charge of disabling endpoints... usually disconnect().
+        * The exception would be entering a high speed test mode.
+        *
+        * FIXME remove this code ... and retest thoroughly.
+        */
        list_for_each_entry(ep, &udc->gadget.ep_list, ep.ep_list) {
                if (ep->desc) {
                        spin_unlock(&udc->lock);
@@ -1223,7 +1229,7 @@ static inline bool feature_is_ep_halt(struct usb_ctrlrequest *crq)
 static int handle_ep0_setup(struct usba_udc *udc, struct usba_ep *ep,
                struct usb_ctrlrequest *crq)
 {
-       int retval = 0;;
+       int retval = 0;
 
        switch (crq->bRequest) {
        case USB_REQ_GET_STATUS: {
@@ -1483,7 +1489,7 @@ restart:
                DBG(DBG_HW, "Packet length: %u\n", pkt_len);
                if (pkt_len != sizeof(crq)) {
                        pr_warning("udc: Invalid packet length %u "
-                               "(expected %lu)\n", pkt_len, sizeof(crq));
+                               "(expected %zu)\n", pkt_len, sizeof(crq));
                        set_protocol_stall(udc, ep);
                        return;
                }
@@ -1641,6 +1647,7 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
        DBG(DBG_INT, "irq, status=%#08x\n", status);
 
        if (status & USBA_DET_SUSPEND) {
+               toggle_bias(0);
                usba_writel(udc, INT_CLR, USBA_DET_SUSPEND);
                DBG(DBG_BUS, "Suspend detected\n");
                if (udc->gadget.speed != USB_SPEED_UNKNOWN
@@ -1652,6 +1659,7 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
        }
 
        if (status & USBA_WAKE_UP) {
+               toggle_bias(1);
                usba_writel(udc, INT_CLR, USBA_WAKE_UP);
                DBG(DBG_BUS, "Wake Up CPU detected\n");
        }
@@ -1695,6 +1703,14 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
                usba_writel(udc, INT_CLR, USBA_END_OF_RESET);
                reset_all_endpoints(udc);
 
+               if (udc->gadget.speed != USB_SPEED_UNKNOWN
+                               && udc->driver->disconnect) {
+                       udc->gadget.speed = USB_SPEED_UNKNOWN;
+                       spin_unlock(&udc->lock);
+                       udc->driver->disconnect(&udc->gadget);
+                       spin_lock(&udc->lock);
+               }
+
                if (status & USBA_HIGH_SPEED) {
                        DBG(DBG_BUS, "High-speed bus reset detected\n");
                        udc->gadget.speed = USB_SPEED_HIGH;
@@ -1718,9 +1734,13 @@ static irqreturn_t usba_udc_irq(int irq, void *devid)
                                | USBA_DET_SUSPEND
                                | USBA_END_OF_RESUME));
 
+               /*
+                * Unclear why we hit this irregularly, e.g. in usbtest,
+                * but it's clearly harmless...
+                */
                if (!(usba_ep_readl(ep0, CFG) & USBA_EPT_MAPPED))
-                       dev_warn(&udc->pdev->dev,
-                                "WARNING: EP0 configuration is invalid!\n");
+                       dev_dbg(&udc->pdev->dev,
+                                "ODD: EP0 configuration is invalid!\n");
        }
 
        spin_unlock(&udc->lock);
@@ -1745,15 +1765,19 @@ static irqreturn_t usba_vbus_irq(int irq, void *devid)
        vbus = gpio_get_value(udc->vbus_pin);
        if (vbus != udc->vbus_prev) {
                if (vbus) {
-                       usba_writel(udc, CTRL, USBA_EN_USBA);
+                       toggle_bias(1);
+                       usba_writel(udc, CTRL, USBA_ENABLE_MASK);
                        usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
                } else {
                        udc->gadget.speed = USB_SPEED_UNKNOWN;
                        reset_all_endpoints(udc);
-                       usba_writel(udc, CTRL, 0);
-                       spin_unlock(&udc->lock);
-                       udc->driver->disconnect(&udc->gadget);
-                       spin_lock(&udc->lock);
+                       toggle_bias(0);
+                       usba_writel(udc, CTRL, USBA_DISABLE_MASK);
+                       if (udc->driver->disconnect) {
+                               spin_unlock(&udc->lock);
+                               udc->driver->disconnect(&udc->gadget);
+                               spin_lock(&udc->lock);
+                       }
                }
                udc->vbus_prev = vbus;
        }
@@ -1803,7 +1827,8 @@ int usb_gadget_register_driver(struct usb_gadget_driver *driver)
        /* If Vbus is present, enable the controller and wait for reset */
        spin_lock_irqsave(&udc->lock, flags);
        if (vbus_is_present(udc) && udc->vbus_prev == 0) {
-               usba_writel(udc, CTRL, USBA_EN_USBA);
+               toggle_bias(1);
+               usba_writel(udc, CTRL, USBA_ENABLE_MASK);
                usba_writel(udc, INT_ENB, USBA_END_OF_RESET);
        }
        spin_unlock_irqrestore(&udc->lock, flags);
@@ -1824,7 +1849,7 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
 
        if (!udc->pdev)
                return -ENODEV;
-       if (driver != udc->driver)
+       if (driver != udc->driver || !driver->unbind)
                return -EINVAL;
 
        if (udc->vbus_pin != -1)
@@ -1836,7 +1861,11 @@ int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
        spin_unlock_irqrestore(&udc->lock, flags);
 
        /* This will also disable the DP pullup */
-       usba_writel(udc, CTRL, 0);
+       toggle_bias(0);
+       usba_writel(udc, CTRL, USBA_DISABLE_MASK);
+
+       if (udc->driver->disconnect)
+               udc->driver->disconnect(&udc->gadget);
 
        driver->unbind(&udc->gadget);
        udc->gadget.dev.driver = NULL;
@@ -1861,7 +1890,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
 
        regs = platform_get_resource(pdev, IORESOURCE_MEM, CTRL_IOMEM_ID);
        fifo = platform_get_resource(pdev, IORESOURCE_MEM, FIFO_IOMEM_ID);
-       if (!regs || !fifo)
+       if (!regs || !fifo || !pdata)
                return -ENXIO;
 
        irq = platform_get_irq(pdev, 0);
@@ -1877,6 +1906,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
                goto err_get_hclk;
        }
 
+       spin_lock_init(&udc->lock);
        udc->pdev = pdev;
        udc->pclk = pclk;
        udc->hclk = hclk;
@@ -1906,19 +1936,48 @@ static int __init usba_udc_probe(struct platform_device *pdev)
 
        /* Make sure we start from a clean slate */
        clk_enable(pclk);
-       usba_writel(udc, CTRL, 0);
+       toggle_bias(0);
+       usba_writel(udc, CTRL, USBA_DISABLE_MASK);
        clk_disable(pclk);
 
+       usba_ep = kmalloc(sizeof(struct usba_ep) * pdata->num_ep,
+                         GFP_KERNEL);
+       if (!usba_ep)
+               goto err_alloc_ep;
+
+       the_udc.gadget.ep0 = &usba_ep[0].ep;
+
        INIT_LIST_HEAD(&usba_ep[0].ep.ep_list);
        usba_ep[0].ep_regs = udc->regs + USBA_EPT_BASE(0);
        usba_ep[0].dma_regs = udc->regs + USBA_DMA_BASE(0);
        usba_ep[0].fifo = udc->fifo + USBA_FIFO_BASE(0);
-       for (i = 1; i < ARRAY_SIZE(usba_ep); i++) {
+       usba_ep[0].ep.ops = &usba_ep_ops;
+       usba_ep[0].ep.name = pdata->ep[0].name;
+       usba_ep[0].ep.maxpacket = pdata->ep[0].fifo_size;
+       usba_ep[0].udc = &the_udc;
+       INIT_LIST_HEAD(&usba_ep[0].queue);
+       usba_ep[0].fifo_size = pdata->ep[0].fifo_size;
+       usba_ep[0].nr_banks = pdata->ep[0].nr_banks;
+       usba_ep[0].index = pdata->ep[0].index;
+       usba_ep[0].can_dma = pdata->ep[0].can_dma;
+       usba_ep[0].can_isoc = pdata->ep[0].can_isoc;
+
+       for (i = 1; i < pdata->num_ep; i++) {
                struct usba_ep *ep = &usba_ep[i];
 
                ep->ep_regs = udc->regs + USBA_EPT_BASE(i);
                ep->dma_regs = udc->regs + USBA_DMA_BASE(i);
                ep->fifo = udc->fifo + USBA_FIFO_BASE(i);
+               ep->ep.ops = &usba_ep_ops;
+               ep->ep.name = pdata->ep[i].name;
+               ep->ep.maxpacket = pdata->ep[i].fifo_size;
+               ep->udc = &the_udc;
+               INIT_LIST_HEAD(&ep->queue);
+               ep->fifo_size = pdata->ep[i].fifo_size;
+               ep->nr_banks = pdata->ep[i].nr_banks;
+               ep->index = pdata->ep[i].index;
+               ep->can_dma = pdata->ep[i].can_dma;
+               ep->can_isoc = pdata->ep[i].can_isoc;
 
                list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
        }
@@ -1937,7 +1996,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
                goto err_device_add;
        }
 
-       if (pdata && pdata->vbus_pin != GPIO_PIN_NONE) {
+       if (pdata->vbus_pin >= 0) {
                if (!gpio_request(pdata->vbus_pin, "atmel_usba_udc")) {
                        udc->vbus_pin = pdata->vbus_pin;
 
@@ -1957,7 +2016,7 @@ static int __init usba_udc_probe(struct platform_device *pdev)
        }
 
        usba_init_debugfs(udc);
-       for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
+       for (i = 1; i < pdata->num_ep; i++)
                usba_ep_init_debugfs(udc, &usba_ep[i]);
 
        return 0;
@@ -1965,6 +2024,8 @@ static int __init usba_udc_probe(struct platform_device *pdev)
 err_device_add:
        free_irq(irq, udc);
 err_request_irq:
+       kfree(usba_ep);
+err_alloc_ep:
        iounmap(udc->fifo);
 err_map_fifo:
        iounmap(udc->regs);
@@ -1982,10 +2043,11 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
 {
        struct usba_udc *udc;
        int i;
+       struct usba_platform_data *pdata = pdev->dev.platform_data;
 
        udc = platform_get_drvdata(pdev);
 
-       for (i = 1; i < ARRAY_SIZE(usba_ep); i++)
+       for (i = 1; i < pdata->num_ep; i++)
                usba_ep_cleanup_debugfs(&usba_ep[i]);
        usba_cleanup_debugfs(udc);
 
@@ -1993,6 +2055,7 @@ static int __exit usba_udc_remove(struct platform_device *pdev)
                gpio_free(udc->vbus_pin);
 
        free_irq(udc->irq, udc);
+       kfree(usba_ep);
        iounmap(udc->fifo);
        iounmap(udc->regs);
        clk_put(udc->hclk);
@@ -2007,6 +2070,7 @@ static struct platform_driver udc_driver = {
        .remove         = __exit_p(usba_udc_remove),
        .driver         = {
                .name           = "atmel_usba_udc",
+               .owner          = THIS_MODULE,
        },
 };
 
@@ -2025,3 +2089,4 @@ module_exit(udc_exit);
 MODULE_DESCRIPTION("Atmel USBA UDC driver");
 MODULE_AUTHOR("Haavard Skinnemoen <hskinnemoen@atmel.com>");
 MODULE_LICENSE("GPL");
+MODULE_ALIAS("platform:atmel_usba_udc");