From: Sergiu Moga <sergiu.m...@microchip.com>

Register the OHCI driver into DM by properly initializing the required
clocks and pins required by the DT node of OHCI. In order for the VBUS
to stay enabled, a `child_pre_probe` method has been added to overcome
the DM core disabling it in `usb_scan_device`: when the generic
`device_probe` method is called, the pinctrl is processed once again,
undoing whatever changes have been made in our driver's probe method.

Furthermore, enable CONFIG_DM_GPIO whenever this driver and CONFIG_DM_USB
are selected.

Signed-off-by: Sergiu Moga <sergiu.m...@microchip.com>
---


v1 -> v2:
- squashed 3/4 into this patch
- removed bool clocked
- use *_blk API's
- select DM_GPIO in Kconfig if DM_USB enabled
- use dev_read_u32_default



v2 -> v3:
- check value of dev_read_addr
- clk_disable in case of failure after at91_start_hc


v3->v4:
none

 drivers/usb/host/Kconfig     |   1 +
 drivers/usb/host/ohci-at91.c | 159 +++++++++++++++++++++++++++++++++++
 2 files changed, 160 insertions(+)

diff --git a/drivers/usb/host/Kconfig b/drivers/usb/host/Kconfig
index 1a883babf4c2..e9551a74bb23 100644
--- a/drivers/usb/host/Kconfig
+++ b/drivers/usb/host/Kconfig
@@ -437,6 +437,7 @@ config USB_ATMEL
        depends on ARCH_AT91
        select SYS_USB_OHCI_CPU_INIT
        select USB_OHCI_NEW
+       select DM_GPIO if DM_USB
 
 choice
        prompt "Clock for OHCI"
diff --git a/drivers/usb/host/ohci-at91.c b/drivers/usb/host/ohci-at91.c
index 9b955c1bd678..92d0ab7184c4 100644
--- a/drivers/usb/host/ohci-at91.c
+++ b/drivers/usb/host/ohci-at91.c
@@ -5,6 +5,9 @@
  */
 
 #include <common.h>
+
+#if !(CONFIG_IS_ENABLED(DM_USB))
+
 #include <asm/arch/clk.h>
 
 int usb_cpu_init(void)
@@ -62,3 +65,159 @@ int usb_cpu_init_fail(void)
 {
        return usb_cpu_stop();
 }
+
+#else
+
+#include <clk.h>
+#include <dm.h>
+#include <asm/gpio.h>
+#include <usb.h>
+#include "ohci.h"
+
+#define AT91_MAX_USBH_PORTS        3
+
+#define at91_for_each_port(index, ports)                                       
\
+               for ((index) = 0;                                               
\
+                    (index) < min_t(u32, AT91_MAX_USBH_PORTS, (ports));        
\
+                    (index)++)
+
+struct at91_usbh_data {
+       enum usb_init_type init_type;
+       struct gpio_desc vbus_pin[AT91_MAX_USBH_PORTS];
+       u32 ports;                              /* number of ports on root hub 
*/
+};
+
+struct ohci_at91_priv {
+       ohci_t ohci;
+       struct clk_bulk clks;
+};
+
+static int at91_start_clock(struct ohci_at91_priv *ohci_at91)
+{
+       return clk_enable_bulk(&ohci_at91->clks);
+}
+
+static int at91_stop_clock(struct ohci_at91_priv *ohci_at91)
+{
+       return clk_disable_bulk(&ohci_at91->clks);
+}
+
+static void ohci_at91_set_power(struct at91_usbh_data *pdata, int port,
+                               bool enable)
+{
+       if (!dm_gpio_is_valid(&pdata->vbus_pin[port]))
+               return;
+
+       if (enable)
+               dm_gpio_set_dir_flags(&pdata->vbus_pin[port],
+                                     GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
+       else
+               dm_gpio_set_dir_flags(&pdata->vbus_pin[port], 0);
+}
+
+static int at91_start_hc(struct udevice *dev)
+{
+       struct ohci_at91_priv *ohci_at91 = dev_get_priv(dev);
+
+       return at91_start_clock(ohci_at91);
+}
+
+static int at91_stop_hc(struct udevice *dev)
+{
+       struct ohci_at91_priv *ohci_at91 = dev_get_priv(dev);
+
+       return at91_stop_clock(ohci_at91);
+}
+
+static int ohci_atmel_deregister(struct udevice *dev)
+{
+       struct at91_usbh_data *pdata = dev_get_plat(dev);
+       int ret, i;
+
+       ret = at91_stop_hc(dev);
+       if (ret)
+               return ret;
+
+       at91_for_each_port(i, pdata->ports)
+               ohci_at91_set_power(pdata, i, false);
+
+       return ohci_deregister(dev);
+}
+
+static int ohci_atmel_child_pre_probe(struct udevice *dev)
+{
+       struct udevice *ohci_controller = dev_get_parent(dev);
+       struct at91_usbh_data *pdata = dev_get_plat(ohci_controller);
+       int i;
+
+       at91_for_each_port(i, pdata->ports)
+               ohci_at91_set_power(pdata, i, true);
+
+       return 0;
+}
+
+static int ohci_atmel_probe(struct udevice *dev)
+{
+       struct ohci_at91_priv *ohci_at91 = dev_get_priv(dev);
+       struct at91_usbh_data *pdata = dev_get_plat(dev);
+       struct ohci_regs *regs;
+       int ret;
+       u32 i;
+
+       regs = (struct ohci_regs *)dev_read_addr(dev);
+       if (IS_ERR(regs)) {
+               ret = PTR_ERR(regs);
+               goto fail;
+       }
+
+       pdata->ports = dev_read_u32_default(dev, "num-ports", 3);
+
+       at91_for_each_port(i, pdata->ports)
+               gpio_request_by_name(dev, "atmel,vbus-gpio", i,
+                                    &pdata->vbus_pin[i], GPIOD_IS_OUT |
+                                    GPIOD_IS_OUT_ACTIVE);
+
+       ret = clk_get_bulk(dev, &ohci_at91->clks);
+       if (ret)
+               goto fail;
+
+       ret = clk_enable_bulk(&ohci_at91->clks);
+       if (ret)
+               goto fail;
+
+       ret = at91_start_hc(dev);
+       if (ret)
+               goto fail;
+
+       return ohci_register(dev, regs);
+
+fail:
+       at91_for_each_port(i, pdata->ports)
+               if (dm_gpio_is_valid(&pdata->vbus_pin[i]))
+                       gpio_free(pdata->vbus_pin[i].offset);
+
+       clk_disable_bulk(&ohci_at91->clks);
+
+       return ret;
+}
+
+static const struct udevice_id ohci_usb_ids[] = {
+       { .compatible = "atmel,at91rm9200-ohci", },
+       { .compatible = "microchip,sama7g5-ohci", },
+       { }
+};
+
+U_BOOT_DRIVER(ohci_atmel) = {
+       .name            = "ohci_atmel",
+       .id              = UCLASS_USB,
+       .of_match        = ohci_usb_ids,
+       .probe           = ohci_atmel_probe,
+       .remove          = ohci_atmel_deregister,
+       .child_pre_probe = ohci_atmel_child_pre_probe,
+       .ops             = &ohci_usb_ops,
+       .plat_auto       = sizeof(struct at91_usbh_data),
+       .priv_auto       = sizeof(struct ohci_at91_priv),
+       .flags           = DM_FLAG_ALLOC_PRIV_DMA,
+};
+
+#endif /* CONFIG_IS_ENABLED(DM_USB) */
-- 
2.34.1

Reply via email to