* Bo Shen <voice.shen@atmel.com>
*/
+#include <clk.h>
+#include <dm.h>
#include <log.h>
#include <malloc.h>
#include <asm/gpio.h>
#include <asm/hardware.h>
+#include <dm/device_compat.h>
+#include <dm/devres.h>
#include <linux/bitops.h>
#include <linux/errno.h>
#include <linux/list.h>
#include <linux/usb/gadget.h>
#include <linux/usb/atmel_usba_udc.h>
+#if CONFIG_IS_ENABLED(DM_USB_GADGET)
+#include <mach/atmel_usba_udc.h>
+
+static int usba_udc_start(struct usb_gadget *gadget,
+ struct usb_gadget_driver *driver);
+static int usba_udc_stop(struct usb_gadget *gadget);
+#endif /* CONFIG_IS_ENABLED(DM_USB_GADGET) */
+
#include "atmel_usba_udc.h"
static int vbus_is_present(struct usba_udc *udc)
.wakeup = usba_udc_wakeup,
.set_selfpowered = usba_udc_set_selfpowered,
.pullup = usba_udc_pullup,
+#if CONFIG_IS_ENABLED(DM_USB_GADGET)
+ .udc_start = usba_udc_start,
+ .udc_stop = usba_udc_stop,
+#endif
};
static struct usb_endpoint_descriptor usba_ep0_desc = {
return eps;
}
+#if !CONFIG_IS_ENABLED(DM_USB_GADGET)
static struct usba_udc controller = {
.regs = (unsigned *)ATMEL_BASE_UDPHS,
.fifo = (unsigned *)ATMEL_BASE_UDPHS_FIFO,
return 0;
}
+
+#else /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */
+struct usba_priv_data {
+ struct clk_bulk clks;
+ struct usba_udc udc;
+};
+
+static int usba_udc_start(struct usb_gadget *gadget,
+ struct usb_gadget_driver *driver)
+{
+ struct usba_udc *udc = to_usba_udc(gadget);
+
+ usba_udc_enable(udc);
+
+ udc->driver = driver;
+ return 0;
+}
+
+static int usba_udc_stop(struct usb_gadget *gadget)
+{
+ struct usba_udc *udc = to_usba_udc(gadget);
+
+ udc->driver = NULL;
+
+ usba_udc_disable(udc);
+ return 0;
+}
+
+static int usba_udc_clk_init(struct udevice *dev, struct clk_bulk *clks)
+{
+ int ret;
+
+ ret = clk_get_bulk(dev, clks);
+ if (ret == -ENOSYS)
+ return 0;
+
+ if (ret)
+ return ret;
+
+ ret = clk_enable_bulk(clks);
+ if (ret) {
+ clk_release_bulk(clks);
+ return ret;
+ }
+
+ return 0;
+}
+
+static int usba_udc_probe(struct udevice *dev)
+{
+ struct usba_priv_data *priv = dev_get_priv(dev);
+ struct usba_udc *udc = &priv->udc;
+ int ret;
+
+ udc->fifo = (void __iomem *)dev_remap_addr_index(dev, FIFO_IOMEM_ID);
+ if (!udc->fifo)
+ return -EINVAL;
+
+ udc->regs = (void __iomem *)dev_remap_addr_index(dev, CTRL_IOMEM_ID);
+ if (!udc->regs)
+ return -EINVAL;
+
+ ret = usba_udc_clk_init(dev, &priv->clks);
+ if (ret)
+ return ret;
+
+ udc->usba_ep = usba_udc_pdata(&pdata, udc);
+
+ udc->gadget.ops = &usba_udc_ops;
+ udc->gadget.speed = USB_SPEED_HIGH,
+ udc->gadget.is_dualspeed = 1,
+ udc->gadget.name = "atmel_usba_udc",
+
+ ret = usb_add_gadget_udc((struct device *)dev, &udc->gadget);
+ if (ret)
+ goto err;
+
+ return 0;
+err:
+ free(udc->usba_ep);
+
+ clk_release_bulk(&priv->clks);
+
+ return ret;
+}
+
+static int usba_udc_remove(struct udevice *dev)
+{
+ struct usba_priv_data *priv = dev_get_priv(dev);
+
+ usb_del_gadget_udc(&priv->udc.gadget);
+
+ free(priv->udc.usba_ep);
+
+ clk_release_bulk(&priv->clks);
+
+ return dm_scan_fdt_dev(dev);
+}
+
+static int usba_udc_handle_interrupts(struct udevice *dev)
+{
+ struct usba_priv_data *priv = dev_get_priv(dev);
+
+ return usba_udc_irq(&priv->udc);
+}
+
+static const struct usb_gadget_generic_ops usba_udc_gadget_ops = {
+ .handle_interrupts = usba_udc_handle_interrupts,
+};
+
+static const struct udevice_id usba_udc_ids[] = {
+ { .compatible = "atmel,at91sam9rl-udc" },
+ { .compatible = "atmel,at91sam9g45-udc" },
+ { .compatible = "atmel,sama5d3-udc" },
+ {}
+};
+
+U_BOOT_DRIVER(atmel_usba_udc) = {
+ .name = "atmel_usba_udc",
+ .id = UCLASS_USB_GADGET_GENERIC,
+ .of_match = usba_udc_ids,
+ .ops = &usba_udc_gadget_ops,
+ .probe = usba_udc_probe,
+ .remove = usba_udc_remove,
+ .priv_auto = sizeof(struct usba_priv_data),
+};
+#endif /* !CONFIG_IS_ENABLED(DM_USB_GADGET) */