Home | History | Annotate | Line # | Download | only in cardbus
ohci_cardbus.c revision 1.26
      1 /*	$NetBSD: ohci_cardbus.c,v 1.26 2008/03/07 21:48:46 dyoung Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * USB Open Host Controller driver.
     42  *
     43  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
     44  * USB spec: http://www.teleport.com/cgi-bin/mailmerge.cgi/~usb/cgiform.tpl
     45  */
     46 
     47 #include <sys/cdefs.h>
     48 __KERNEL_RCSID(0, "$NetBSD: ohci_cardbus.c,v 1.26 2008/03/07 21:48:46 dyoung Exp $");
     49 
     50 #include "ehci_cardbus.h"
     51 
     52 #include <sys/param.h>
     53 #include <sys/systm.h>
     54 #include <sys/kernel.h>
     55 #include <sys/device.h>
     56 #include <sys/proc.h>
     57 
     58 #include <sys/bus.h>
     59 
     60 #if defined pciinc
     61 #include <dev/pci/pcidevs.h>
     62 #endif
     63 
     64 #include <dev/cardbus/cardbusvar.h>
     65 #include <dev/pci/pcidevs.h>
     66 
     67 #include <dev/cardbus/usb_cardbus.h>
     68 
     69 #include <dev/usb/usb.h>
     70 #include <dev/usb/usbdi.h>
     71 #include <dev/usb/usbdivar.h>
     72 #include <dev/usb/usb_mem.h>
     73 
     74 #include <dev/usb/ohcireg.h>
     75 #include <dev/usb/ohcivar.h>
     76 
     77 int	ohci_cardbus_match(device_t, struct cfdata *, void *);
     78 void	ohci_cardbus_attach(device_t, device_t, void *);
     79 int	ohci_cardbus_detach(device_t, int);
     80 
     81 struct ohci_cardbus_softc {
     82 	ohci_softc_t		sc;
     83 #if NEHCI_CARDBUS > 0
     84 	struct usb_cardbus	sc_cardbus;
     85 #endif
     86 	cardbus_chipset_tag_t	sc_cc;
     87 	cardbus_function_tag_t	sc_cf;
     88 	cardbus_devfunc_t	sc_ct;
     89 	void 			*sc_ih;		/* interrupt vectoring */
     90 };
     91 
     92 CFATTACH_DECL(ohci_cardbus, sizeof(struct ohci_cardbus_softc),
     93     ohci_cardbus_match, ohci_cardbus_attach, ohci_cardbus_detach, ohci_activate);
     94 
     95 #define CARDBUS_INTERFACE_OHCI PCI_INTERFACE_OHCI
     96 #define CARDBUS_CBMEM PCI_CBMEM
     97 #define cardbus_findvendor pci_findvendor
     98 #define cardbus_devinfo pci_devinfo
     99 
    100 int
    101 ohci_cardbus_match(device_t parent, struct cfdata *match, void *aux)
    102 {
    103 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
    104 
    105 	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
    106 	    CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
    107 	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_OHCI)
    108 		return (1);
    109 
    110 	return (0);
    111 }
    112 
    113 void
    114 ohci_cardbus_attach(device_t parent, device_t self, void *aux)
    115 {
    116 	struct ohci_cardbus_softc *sc = device_private(self);
    117 	struct cardbus_attach_args *ca = aux;
    118 	cardbus_devfunc_t ct = ca->ca_ct;
    119 	cardbus_chipset_tag_t cc = ct->ct_cc;
    120 	cardbus_function_tag_t cf = ct->ct_cf;
    121 	cardbusreg_t csr;
    122 	char devinfo[256];
    123 	usbd_status r;
    124 	const char *vendor;
    125 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
    126 
    127 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    128 	printf(": %s (rev. 0x%02x)\n", devinfo,
    129 	       CARDBUS_REVISION(ca->ca_class));
    130 
    131 	/* Map I/O registers */
    132 	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    133 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    134 		printf("%s: can't map mem space\n", devname);
    135 		return;
    136 	}
    137 
    138 	/* Disable interrupts, so we don't can any spurious ones. */
    139 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    140 			  OHCI_ALL_INTRS);
    141 
    142 	sc->sc_cc = cc;
    143 	sc->sc_cf = cf;
    144 	sc->sc_ct = ct;
    145 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    146 
    147 #if rbus
    148 #else
    149 XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
    150 #endif
    151 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    152 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
    153 
    154 	/* Enable the device. */
    155 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
    156 				CARDBUS_COMMAND_STATUS_REG);
    157 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
    158 		       csr | CARDBUS_COMMAND_MASTER_ENABLE
    159 			   | CARDBUS_COMMAND_MEM_ENABLE);
    160 
    161 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
    162 					   IPL_USB, ohci_intr, sc);
    163 	if (sc->sc_ih == NULL) {
    164 		printf("%s: couldn't establish interrupt\n", devname);
    165 		return;
    166 	}
    167 	printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
    168 
    169 	/* Figure out vendor for root hub descriptor. */
    170 	vendor = cardbus_findvendor(ca->ca_id);
    171 	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
    172 	if (vendor)
    173 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
    174 	else
    175 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    176 		    "vendor 0x%04x", CARDBUS_VENDOR(ca->ca_id));
    177 
    178 	r = ohci_init(&sc->sc);
    179 	if (r != USBD_NORMAL_COMPLETION) {
    180 		printf("%s: init failed, error=%d\n", devname, r);
    181 
    182 		/* Avoid spurious interrupts. */
    183 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    184 		sc->sc_ih = 0;
    185 
    186 		return;
    187 	}
    188 
    189 #if NEHCI_CARDBUS > 0
    190 	usb_cardbus_add(&sc->sc_cardbus, ca, &sc->sc.sc_bus);
    191 #endif
    192 
    193 	if (!pmf_device_register1(self, ohci_suspend, ohci_resume,
    194 	                          ohci_shutdown))
    195 		aprint_error_dev(self, "couldn't establish power handler\n");
    196 
    197 	/* Attach usb device. */
    198 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    199 }
    200 
    201 int
    202 ohci_cardbus_detach(device_t self, int flags)
    203 {
    204 	struct ohci_cardbus_softc *sc = device_private(self);
    205 	struct cardbus_devfunc *ct = sc->sc_ct;
    206 	int rv;
    207 
    208 	rv = ohci_detach(&sc->sc, flags);
    209 	if (rv)
    210 		return (rv);
    211 	if (sc->sc_ih != NULL) {
    212 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    213 		sc->sc_ih = NULL;
    214 	}
    215 	if (sc->sc.sc_size) {
    216 		Cardbus_mapreg_unmap(ct, CARDBUS_CBMEM, sc->sc.iot,
    217 		    sc->sc.ioh, sc->sc.sc_size);
    218 		sc->sc.sc_size = 0;
    219 	}
    220 #if NEHCI_CARDBUS > 0
    221 	usb_cardbus_rem(&sc->sc_cardbus);
    222 #endif
    223 	return (0);
    224 }
    225