Home | History | Annotate | Line # | Download | only in fdt
ohci_fdt.c revision 1.1.8.2
      1 /* $NetBSD: ohci_fdt.c,v 1.1.8.2 2017/12/03 11:37:01 jdolecek Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015-2017 Jared McNeill <jmcneill (at) invisible.ca>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: ohci_fdt.c,v 1.1.8.2 2017/12/03 11:37:01 jdolecek Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/device.h>
     35 #include <sys/intr.h>
     36 #include <sys/systm.h>
     37 #include <sys/kernel.h>
     38 
     39 #include <dev/usb/usb.h>
     40 #include <dev/usb/usbdi.h>
     41 #include <dev/usb/usbdivar.h>
     42 #include <dev/usb/usb_mem.h>
     43 #include <dev/usb/ohcireg.h>
     44 #include <dev/usb/ohcivar.h>
     45 
     46 #include <dev/fdt/fdtvar.h>
     47 
     48 static int	ohci_fdt_match(device_t, cfdata_t, void *);
     49 static void	ohci_fdt_attach(device_t, device_t, void *);
     50 
     51 CFATTACH_DECL2_NEW(ohci_fdt, sizeof(struct ohci_softc),
     52 	ohci_fdt_match, ohci_fdt_attach, NULL,
     53 	ohci_activate, NULL, ohci_childdet);
     54 
     55 static int
     56 ohci_fdt_match(device_t parent, cfdata_t cf, void *aux)
     57 {
     58 	const char * const compatible[] = {
     59 		"generic-ohci",
     60 		NULL
     61 	};
     62 	struct fdt_attach_args * const faa = aux;
     63 
     64 	return of_match_compatible(faa->faa_phandle, compatible);
     65 }
     66 
     67 static void
     68 ohci_fdt_attach(device_t parent, device_t self, void *aux)
     69 {
     70 	struct ohci_softc * const sc = device_private(self);
     71 	struct fdt_attach_args * const faa = aux;
     72 	const int phandle = faa->faa_phandle;
     73 	struct fdtbus_reset *rst;
     74 	struct fdtbus_phy *phy;
     75 	struct clk *clk;
     76 	char intrstr[128];
     77 	bus_addr_t addr;
     78 	bus_size_t size;
     79 	int error;
     80 	void *ih;
     81 	u_int n;
     82 
     83 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
     84 		aprint_error(": couldn't get registers\n");
     85 		return;
     86 	}
     87 
     88 	/* Enable clocks */
     89 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
     90 		if (clk_enable(clk) != 0) {
     91 			aprint_error(": couldn't enable clock #%d\n", n);
     92 			return;
     93 		}
     94 	/* De-assert resets */
     95 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
     96 		if (fdtbus_reset_deassert(rst) != 0) {
     97 			aprint_error(": couldn't de-assert reset #%d\n", n);
     98 			return;
     99 		}
    100 
    101 	/* Enable optional phy */
    102 	phy = fdtbus_phy_get(phandle, "usb");
    103 	if (phy && fdtbus_phy_enable(phy, true) != 0) {
    104 		aprint_error(": couldn't enable phy\n");
    105 		return;
    106 	}
    107 
    108 	sc->sc_dev = self;
    109 	sc->sc_bus.ub_hcpriv = sc;
    110 	sc->sc_bus.ub_dmatag = faa->faa_dmat;
    111 	sc->sc_flags = 0;
    112 	sc->sc_id_vendor = 0;
    113 	strlcpy(sc->sc_vendor, "Generic", sizeof(sc->sc_vendor));
    114 	sc->sc_size = size;
    115 	sc->iot = faa->faa_bst;
    116 	if (bus_space_map(sc->iot, addr, size, 0, &sc->ioh) != 0) {
    117 		aprint_error(": couldn't map registers\n");
    118 		return;
    119 	}
    120 
    121 	aprint_naive("\n");
    122 	aprint_normal(": OHCI\n");
    123 
    124 	/* Disable interrupts */
    125 	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
    126 	    OHCI_ALL_INTRS);
    127 
    128 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    129 		aprint_error_dev(self, "failed to decode interrupt\n");
    130 		return;
    131 	}
    132 
    133 	ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
    134 	    ohci_intr, sc);
    135 	if (ih == NULL) {
    136 		aprint_error_dev(self, "couldn't establish interrupt on %s\n",
    137 		    intrstr);
    138 		return;
    139 	}
    140 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    141 
    142 	error = ohci_init(sc);
    143 	if (error) {
    144 		aprint_error_dev(self, "init failed, error = %d\n", error);
    145 		return;
    146 	}
    147 
    148 	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
    149 }
    150