Home | History | Annotate | Line # | Download | only in cardbus
ohci_cardbus.c revision 1.40.2.5
      1  1.40.2.5     skrll /*	$NetBSD: ohci_cardbus.c,v 1.40.2.5 2016/10/05 20:55:40 skrll Exp $	*/
      2       1.1  augustss 
      3       1.1  augustss /*
      4       1.1  augustss  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5       1.1  augustss  * All rights reserved.
      6       1.1  augustss  *
      7       1.1  augustss  * This code is derived from software contributed to The NetBSD Foundation
      8       1.7  augustss  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9       1.1  augustss  * Carlstedt Research & Technology.
     10       1.1  augustss  *
     11       1.1  augustss  * Redistribution and use in source and binary forms, with or without
     12       1.1  augustss  * modification, are permitted provided that the following conditions
     13       1.1  augustss  * are met:
     14       1.1  augustss  * 1. Redistributions of source code must retain the above copyright
     15       1.1  augustss  *    notice, this list of conditions and the following disclaimer.
     16       1.1  augustss  * 2. Redistributions in binary form must reproduce the above copyright
     17       1.1  augustss  *    notice, this list of conditions and the following disclaimer in the
     18       1.1  augustss  *    documentation and/or other materials provided with the distribution.
     19       1.1  augustss  *
     20       1.1  augustss  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21       1.1  augustss  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22       1.1  augustss  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23       1.1  augustss  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24       1.1  augustss  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25       1.1  augustss  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26       1.1  augustss  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27       1.1  augustss  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28       1.1  augustss  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29       1.1  augustss  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30       1.1  augustss  * POSSIBILITY OF SUCH DAMAGE.
     31       1.1  augustss  */
     32       1.1  augustss 
     33       1.1  augustss /*
     34       1.1  augustss  * USB Open Host Controller driver.
     35       1.1  augustss  *
     36       1.1  augustss  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
     37       1.1  augustss  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
     38       1.1  augustss  */
     39      1.11     lukem 
     40      1.11     lukem #include <sys/cdefs.h>
     41  1.40.2.5     skrll __KERNEL_RCSID(0, "$NetBSD: ohci_cardbus.c,v 1.40.2.5 2016/10/05 20:55:40 skrll Exp $");
     42       1.1  augustss 
     43      1.15  drochner #include "ehci_cardbus.h"
     44      1.10  augustss 
     45       1.1  augustss #include <sys/param.h>
     46       1.1  augustss #include <sys/systm.h>
     47       1.1  augustss #include <sys/kernel.h>
     48       1.1  augustss #include <sys/device.h>
     49       1.1  augustss #include <sys/proc.h>
     50       1.1  augustss 
     51      1.24        ad #include <sys/bus.h>
     52       1.1  augustss 
     53       1.1  augustss #if defined pciinc
     54       1.1  augustss #include <dev/pci/pcidevs.h>
     55       1.1  augustss #endif
     56       1.2      haya 
     57       1.1  augustss #include <dev/cardbus/cardbusvar.h>
     58      1.19   mycroft #include <dev/pci/pcidevs.h>
     59       1.1  augustss 
     60      1.10  augustss #include <dev/cardbus/usb_cardbus.h>
     61      1.10  augustss 
     62       1.1  augustss #include <dev/usb/usb.h>
     63       1.1  augustss #include <dev/usb/usbdi.h>
     64       1.1  augustss #include <dev/usb/usbdivar.h>
     65       1.1  augustss #include <dev/usb/usb_mem.h>
     66       1.1  augustss 
     67       1.1  augustss #include <dev/usb/ohcireg.h>
     68       1.1  augustss #include <dev/usb/ohcivar.h>
     69       1.1  augustss 
     70      1.32    cegger int	ohci_cardbus_match(device_t, cfdata_t, void *);
     71      1.26    dyoung void	ohci_cardbus_attach(device_t, device_t, void *);
     72      1.26    dyoung int	ohci_cardbus_detach(device_t, int);
     73       1.1  augustss 
     74       1.1  augustss struct ohci_cardbus_softc {
     75       1.1  augustss 	ohci_softc_t		sc;
     76      1.15  drochner #if NEHCI_CARDBUS > 0
     77      1.10  augustss 	struct usb_cardbus	sc_cardbus;
     78      1.10  augustss #endif
     79       1.1  augustss 	cardbus_chipset_tag_t	sc_cc;
     80       1.1  augustss 	cardbus_function_tag_t	sc_cf;
     81       1.3  augustss 	cardbus_devfunc_t	sc_ct;
     82       1.1  augustss 	void 			*sc_ih;		/* interrupt vectoring */
     83       1.1  augustss };
     84       1.1  augustss 
     85      1.27  drochner CFATTACH_DECL_NEW(ohci_cardbus, sizeof(struct ohci_cardbus_softc),
     86  1.40.2.4     skrll     ohci_cardbus_match, ohci_cardbus_attach, ohci_cardbus_detach,
     87  1.40.2.4     skrll     ohci_activate);
     88       1.1  augustss 
     89       1.1  augustss int
     90      1.32    cegger ohci_cardbus_match(device_t parent, cfdata_t match, void *aux)
     91       1.1  augustss {
     92       1.8  augustss 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
     93       1.1  augustss 
     94      1.35    dyoung 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
     95      1.35    dyoung 	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     96      1.35    dyoung 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_OHCI)
     97  1.40.2.2     skrll 		return 1;
     98      1.20     perry 
     99  1.40.2.2     skrll 	return 0;
    100       1.1  augustss }
    101       1.1  augustss 
    102       1.1  augustss void
    103      1.26    dyoung ohci_cardbus_attach(device_t parent, device_t self, void *aux)
    104       1.1  augustss {
    105      1.26    dyoung 	struct ohci_cardbus_softc *sc = device_private(self);
    106       1.1  augustss 	struct cardbus_attach_args *ca = aux;
    107       1.1  augustss 	cardbus_devfunc_t ct = ca->ca_ct;
    108       1.1  augustss 	cardbus_chipset_tag_t cc = ct->ct_cc;
    109       1.1  augustss 	cardbus_function_tag_t cf = ct->ct_cf;
    110      1.33    dyoung 	pcireg_t csr;
    111       1.1  augustss 	char devinfo[256];
    112      1.27  drochner 	const char *devname = device_xname(self);
    113      1.27  drochner 
    114      1.27  drochner 	sc->sc.sc_dev = self;
    115  1.40.2.1     skrll 	sc->sc.sc_bus.ub_hcpriv = sc;
    116       1.1  augustss 
    117      1.35    dyoung 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    118       1.1  augustss 	printf(": %s (rev. 0x%02x)\n", devinfo,
    119      1.35    dyoung 	       PCI_REVISION(ca->ca_class));
    120       1.1  augustss 
    121       1.1  augustss 	/* Map I/O registers */
    122      1.35    dyoung 	if (Cardbus_mapreg_map(ct, PCI_CBMEM, PCI_MAPREG_TYPE_MEM, 0,
    123       1.6  augustss 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    124  1.40.2.5     skrll 		aprint_error("%s: can't map mem space\n", devname);
    125       1.1  augustss 		return;
    126       1.1  augustss 	}
    127       1.1  augustss 
    128       1.1  augustss 	sc->sc_cc = cc;
    129       1.1  augustss 	sc->sc_cf = cf;
    130       1.3  augustss 	sc->sc_ct = ct;
    131  1.40.2.1     skrll 	sc->sc.sc_bus.ub_dmatag = ca->ca_dmat;
    132       1.1  augustss 
    133       1.1  augustss 	/* Enable the device. */
    134  1.40.2.5     skrll 	csr = Cardbus_conf_read(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG);
    135      1.35    dyoung 	Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG,
    136      1.35    dyoung 		       csr | PCI_COMMAND_MASTER_ENABLE
    137      1.35    dyoung 			   | PCI_COMMAND_MEM_ENABLE);
    138       1.1  augustss 
    139      1.31    dyoung 	/* Disable interrupts, so we don't can any spurious ones. */
    140      1.31    dyoung 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    141      1.31    dyoung 			  OHCI_ALL_INTRS);
    142      1.31    dyoung 
    143      1.39  drochner 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_USB, ohci_intr, sc);
    144       1.1  augustss 	if (sc->sc_ih == NULL) {
    145  1.40.2.5     skrll 		aprint_error("%s: couldn't establish interrupt\n", devname);
    146       1.1  augustss 		return;
    147       1.1  augustss 	}
    148       1.1  augustss 
    149       1.1  augustss 	/* Figure out vendor for root hub descriptor. */
    150      1.35    dyoung 	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
    151      1.40  christos 	pci_findvendor(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    152      1.40  christos 	    sc->sc.sc_id_vendor);
    153      1.20     perry 
    154  1.40.2.3     skrll 	int err = ohci_init(&sc->sc);
    155  1.40.2.3     skrll 	if (err) {
    156  1.40.2.5     skrll 		aprint_error("%s: init failed, error=%d\n", devname, err);
    157       1.1  augustss 
    158       1.1  augustss 		/* Avoid spurious interrupts. */
    159      1.36    dyoung 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    160       1.1  augustss 		sc->sc_ih = 0;
    161       1.1  augustss 
    162       1.1  augustss 		return;
    163       1.1  augustss 	}
    164       1.1  augustss 
    165      1.15  drochner #if NEHCI_CARDBUS > 0
    166      1.27  drochner 	usb_cardbus_add(&sc->sc_cardbus, ca, self);
    167      1.10  augustss #endif
    168      1.10  augustss 
    169      1.25    dyoung 	if (!pmf_device_register1(self, ohci_suspend, ohci_resume,
    170      1.25    dyoung 	                          ohci_shutdown))
    171      1.25    dyoung 		aprint_error_dev(self, "couldn't establish power handler\n");
    172      1.25    dyoung 
    173       1.1  augustss 	/* Attach usb device. */
    174      1.26    dyoung 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    175       1.1  augustss }
    176       1.1  augustss 
    177       1.1  augustss int
    178      1.26    dyoung ohci_cardbus_detach(device_t self, int flags)
    179       1.1  augustss {
    180      1.26    dyoung 	struct ohci_cardbus_softc *sc = device_private(self);
    181       1.3  augustss 	struct cardbus_devfunc *ct = sc->sc_ct;
    182       1.1  augustss 	int rv;
    183       1.1  augustss 
    184       1.1  augustss 	rv = ohci_detach(&sc->sc, flags);
    185       1.1  augustss 	if (rv)
    186  1.40.2.2     skrll 		return rv;
    187       1.3  augustss 	if (sc->sc_ih != NULL) {
    188      1.36    dyoung 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    189       1.3  augustss 		sc->sc_ih = NULL;
    190       1.1  augustss 	}
    191       1.6  augustss 	if (sc->sc.sc_size) {
    192      1.35    dyoung 		Cardbus_mapreg_unmap(ct, PCI_CBMEM, sc->sc.iot,
    193       1.6  augustss 		    sc->sc.ioh, sc->sc.sc_size);
    194       1.6  augustss 		sc->sc.sc_size = 0;
    195       1.1  augustss 	}
    196      1.15  drochner #if NEHCI_CARDBUS > 0
    197      1.10  augustss 	usb_cardbus_rem(&sc->sc_cardbus);
    198      1.10  augustss #endif
    199  1.40.2.2     skrll 	return 0;
    200       1.1  augustss }
    201