#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cdefs.h>
#include <sys/device.h>
#include <sys/mutex.h>
#include <dev/clk/clk.h>
#include <dev/fdt/fdtvar.h>
#include <dev/fdt/syscon.h>
struct am18xx_usbphy {
struct clk *phy_clk48;
uint32_t phy_suspend_mask;
uint32_t phy_suspend_bits;
};
struct am18xx_usbphy_softc {
struct am18xx_usbphy sc_usb11_phy;
struct am18xx_usbphy sc_usb20_phy;
kmutex_t sc_lock;
struct syscon *sc_syscon;
};
static int am18xx_usbphy_match(device_t, cfdata_t, void *);
static void am18xx_usbphy_attach(device_t, device_t, void *);
static void *am18xx_usbphy_acquire(device_t, const void *, size_t);
static void am18xx_usbphy_release(device_t, void *);
static int am18xx_usbphy_enable(device_t, void *, bool);
CFATTACH_DECL_NEW(am18xxusbphy, sizeof(struct am18xx_usbphy_softc),
am18xx_usbphy_match, am18xx_usbphy_attach, NULL, NULL);
#define AM18XX_CFGCHIP2 0x8
#define AM18XX_CFGCHIP2_USB11SUSPENDM __BIT(7)
#define AM18XX_CFGCHIP2_USB20OTGPWDN __BIT(9)
static const struct device_compatible_entry compat_data[] = {
{ .compat = "ti,da830-usb-phy"},
DEVICE_COMPAT_EOL
};
static const struct fdtbus_phy_controller_func am18xx_usbphy_funcs = {
.acquire = &am18xx_usbphy_acquire,
.release = &am18xx_usbphy_release,
.enable = &am18xx_usbphy_enable,
};
static void *
am18xx_usbphy_acquire(device_t dev, const void *data, size_t len)
{
struct am18xx_usbphy_softc *const sc = device_private(dev);
const u_int *cells = data;
if (len != 4)
return NULL;
const u_int index = be32toh(cells[0]);
if (index == 0) {
return &sc->sc_usb20_phy;
} else if (index == 1) {
return &sc->sc_usb11_phy;
}
return NULL;
}
static void
am18xx_usbphy_release(device_t dev, void *priv)
{
}
static int
am18xx_usbphy_enable(device_t dev, void *priv, bool enable)
{
struct am18xx_usbphy_softc *sc = device_private(dev);
struct am18xx_usbphy *phy = (struct am18xx_usbphy *)priv;
int error;
uint32_t val;
mutex_enter(&sc->sc_lock);
if (enable) {
error = clk_enable(phy->phy_clk48);
if (error) {
goto cleanup;
}
syscon_lock(sc->sc_syscon);
val = syscon_read_4(sc->sc_syscon, AM18XX_CFGCHIP2);
val &= (~phy->phy_suspend_mask);
val |= phy->phy_suspend_mask & (~phy->phy_suspend_bits);
syscon_write_4(sc->sc_syscon, AM18XX_CFGCHIP2, val);
syscon_unlock(sc->sc_syscon);
} else {
syscon_lock(sc->sc_syscon);
val = syscon_read_4(sc->sc_syscon, AM18XX_CFGCHIP2);
val &= (~phy->phy_suspend_mask);
val |= phy->phy_suspend_bits;
syscon_write_4(sc->sc_syscon, AM18XX_CFGCHIP2, val);
syscon_unlock(sc->sc_syscon);
error = clk_disable(phy->phy_clk48);
if (error) {
goto cleanup;
}
}
cleanup:
mutex_exit(&sc->sc_lock);
return error;
}
int
am18xx_usbphy_match(device_t parent, cfdata_t cf, void *aux)
{
struct fdt_attach_args *const faa = aux;
return of_compatible_match(faa->faa_phandle, compat_data);
}
void
am18xx_usbphy_attach(device_t parent, device_t self, void *aux)
{
struct am18xx_usbphy_softc *const sc = device_private(self);
struct fdt_attach_args *const faa = aux;
const int phandle = faa->faa_phandle;
int error;
mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
sc->sc_usb11_phy.phy_suspend_mask = AM18XX_CFGCHIP2_USB11SUSPENDM;
sc->sc_usb11_phy.phy_suspend_bits = 0;
sc->sc_usb20_phy.phy_suspend_mask = AM18XX_CFGCHIP2_USB20OTGPWDN;
sc->sc_usb20_phy.phy_suspend_bits = AM18XX_CFGCHIP2_USB20OTGPWDN;
sc->sc_usb20_phy.phy_clk48 = fdtbus_clock_get(phandle, "usb0_clk48");
if (sc->sc_usb20_phy.phy_clk48 == NULL) {
aprint_error(": failed to get usb 2.0 phy clk\n");
return;
}
sc->sc_usb11_phy.phy_clk48 = fdtbus_clock_get(phandle, "usb1_clk48");
if (sc->sc_usb11_phy.phy_clk48 == NULL) {
aprint_error(": failed to get usb 1.1 phy clk\n");
return;
}
sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(phandle));
if (sc->sc_syscon == NULL) {
aprint_error(": couldn't get syscon registers\n");
return;
}
error = fdtbus_register_phy_controller(self, phandle,
&am18xx_usbphy_funcs);
if (error) {
aprint_error(": failed to register usb phy\n");
return;
}
aprint_normal("\n");
}