#include <sys/param.h>
#include <sys/bus.h>
#include <sys/cdefs.h>
#include <sys/device.h>
#include <dev/clk/clk.h>
#include <dev/fdt/fdtvar.h>
#include <dev/usb/usb.h>
#include <dev/usb/usbdi.h>
#include <dev/usb/usbdivar.h>
#include <dev/usb/usb_mem.h>
#include <dev/usb/ohcireg.h>
#include <dev/usb/ohcivar.h>
struct am18xx_ohci_softc {
struct ohci_softc ohci_sc;
struct clk *sc_clk;
struct fdtbus_phy *sc_phy;
};
static int am18xx_ohci_match(device_t, cfdata_t, void *);
static void am18xx_ohci_attach(device_t, device_t, void *);
static int am18xx_ohci_enable_clocks(struct am18xx_ohci_softc *);
CFATTACH_DECL_NEW(am18xxohci, sizeof(struct am18xx_ohci_softc),
am18xx_ohci_match, am18xx_ohci_attach, NULL, NULL);
static const struct device_compatible_entry compat_data[] = {
{ .compat = "ti,da830-ohci" },
DEVICE_COMPAT_EOL
};
static int
am18xx_ohci_enable_clocks(struct am18xx_ohci_softc *sc)
{
int error;
error = clk_enable(sc->sc_clk);
if (error != 0) {
return error;
}
error = fdtbus_phy_enable(sc->sc_phy, true);
if (error != 0) {
return error;
}
return 0;
}
int
am18xx_ohci_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_ohci_attach(device_t parent, device_t self, void *aux)
{
struct am18xx_ohci_softc *const sc = device_private(self);
struct fdt_attach_args *const faa = aux;
const int phandle = faa->faa_phandle;
bus_addr_t addr;
bus_size_t size;
char intrstr[128];
int error;
sc->ohci_sc.iot = faa->faa_bst;
sc->ohci_sc.sc_dev = self;
sc->ohci_sc.sc_bus.ub_hcpriv = &sc->ohci_sc;
sc->ohci_sc.sc_bus.ub_dmatag = faa->faa_dmat;
sc->ohci_sc.sc_flags = 0;
sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
if (sc->sc_clk == NULL) {
aprint_error(": couldn't get usb clock\n");
return;
}
sc->sc_phy = fdtbus_phy_get(phandle, "usb-phy");
if (sc->sc_phy == NULL) {
aprint_error(": couldn't get usb phy\n");
return;
}
if (am18xx_ohci_enable_clocks(sc)) {
aprint_error(": couldn't enable clocks\n");
return;
}
if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
aprint_error(": couldn't get registers\n");
return;
}
if (bus_space_map(sc->ohci_sc.iot, addr, size, 0, &sc->ohci_sc.ioh)) {
aprint_error(": couldn't map registers\n");
return;
}
sc->ohci_sc.sc_size = size;
bus_space_write_4(sc->ohci_sc.iot, sc->ohci_sc.ioh,
OHCI_INTERRUPT_DISABLE, OHCI_ALL_INTRS);
if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
aprint_error(": failed to decode interrupt\n");
return;
}
void *ih = fdtbus_intr_establish_xname(phandle, 0, IPL_USB,
FDT_INTR_MPSAFE, ohci_intr, &sc->ohci_sc, device_xname(self));
if (ih == NULL) {
aprint_error_dev(self, ": couldn't install interrupt %s\n",
intrstr);
return;
}
aprint_normal(": ohci on %s\n", intrstr);
error = ohci_init(&sc->ohci_sc);
if (error) {
aprint_error_dev(self, ": init failed, error=%d\n", error);
return;
}
sc->ohci_sc.sc_child = config_found(self, &sc->ohci_sc.sc_bus,
usbctlprint, CFARGS_NONE);
}