Home | History | Annotate | Line # | Download | only in cardbus
ohci_cardbus.c revision 1.2
      1 /*	$NetBSD: ohci_cardbus.c,v 1.2 1999/10/27 10:04:41 haya 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 (augustss (at) carlstedt.se) 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/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/device.h>
     51 #include <sys/proc.h>
     52 #include <sys/queue.h>
     53 
     54 #include <machine/bus.h>
     55 
     56 #if defined pciinc
     57 #include <dev/pci/pcidevs.h>
     58 #endif
     59 
     60 #include <dev/cardbus/cardbusvar.h>
     61 #include <dev/cardbus/cardbusdevs.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdivar.h>
     66 #include <dev/usb/usb_mem.h>
     67 
     68 #include <dev/usb/ohcireg.h>
     69 #include <dev/usb/ohcivar.h>
     70 
     71 int	ohci_cardbus_match __P((struct device *, struct cfdata *, void *));
     72 void	ohci_cardbus_attach __P((struct device *, struct device *, void *));
     73 int	ohci_cardbus_detach __P((device_ptr_t, int));
     74 
     75 struct ohci_cardbus_softc {
     76 	ohci_softc_t		sc;
     77 	cardbus_chipset_tag_t	sc_cc;
     78 	cardbus_function_tag_t	sc_cf;
     79 	bus_size_t		sc_size;
     80 	void 			*sc_ih;		/* interrupt vectoring */
     81 };
     82 
     83 struct cfattach ohci_cardbus_ca = {
     84 	sizeof(struct ohci_cardbus_softc), ohci_cardbus_match,
     85 	ohci_cardbus_attach, ohci_cardbus_detach, ohci_activate
     86 };
     87 
     88 #define CARDBUS_INTERFACE_OHCI PCI_INTERFACE_OHCI
     89 #define CARDBUS_CBMEM PCI_CBMEM
     90 #define cardbus_findvendor pci_findvendor
     91 #define cardbus_devinfo pci_devinfo
     92 
     93 int
     94 ohci_cardbus_match(parent, match, aux)
     95 	struct device *parent;
     96 	struct cfdata *match;
     97 	void *aux;
     98 {
     99 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *) aux;
    100 
    101 	if (CARDBUS_CLASS(ca->ca_class) == CARDBUS_CLASS_SERIALBUS &&
    102 	    CARDBUS_SUBCLASS(ca->ca_class) == CARDBUS_SUBCLASS_SERIALBUS_USB &&
    103 	    CARDBUS_INTERFACE(ca->ca_class) == CARDBUS_INTERFACE_OHCI)
    104 		return (1);
    105 
    106 	return (0);
    107 }
    108 
    109 void
    110 ohci_cardbus_attach(parent, self, aux)
    111 	struct device *parent;
    112 	struct device *self;
    113 	void *aux;
    114 {
    115 	struct ohci_cardbus_softc *sc = (struct ohci_cardbus_softc *)self;
    116 	struct cardbus_attach_args *ca = aux;
    117 	cardbus_devfunc_t ct = ca->ca_ct;
    118 	cardbus_chipset_tag_t cc = ct->ct_cc;
    119 	cardbus_function_tag_t cf = ct->ct_cf;
    120 	cardbusreg_t csr;
    121 	char devinfo[256];
    122 	usbd_status r;
    123 	char *vendor;
    124 	char *devname = sc->sc.sc_bus.bdev.dv_xname;
    125 
    126 	cardbus_devinfo(ca->ca_id, ca->ca_class, 0, devinfo);
    127 	printf(": %s (rev. 0x%02x)\n", devinfo,
    128 	       CARDBUS_REVISION(ca->ca_class));
    129 
    130 	/* Map I/O registers */
    131 	if (Cardbus_mapreg_map(ct, CARDBUS_CBMEM, CARDBUS_MAPREG_TYPE_MEM, 0,
    132 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc_size)) {
    133 		printf("%s: can't map mem space\n", devname);
    134 		return;
    135 	}
    136 
    137 	/* Disable interrupts, so we don't can any spurious ones. */
    138 	bus_space_write_4(sc->sc.iot, sc->sc.ioh, OHCI_INTERRUPT_DISABLE,
    139 			  OHCI_ALL_INTRS);
    140 
    141 	sc->sc_cc = cc;
    142 	sc->sc_cf = cf;
    143 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    144 
    145 #if rbus
    146 #else
    147 XXX	(ct->ct_cf->cardbus_mem_open)(cc, 0, iob, iob + 0x40);
    148 #endif
    149 	(ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
    150 
    151 	/* Enable the device. */
    152 	csr = cardbus_conf_read(cc, cf, ca->ca_tag,
    153 				CARDBUS_COMMAND_STATUS_REG);
    154 	cardbus_conf_write(cc, cf, ca->ca_tag, CARDBUS_COMMAND_STATUS_REG,
    155 		       csr | CARDBUS_COMMAND_MASTER_ENABLE
    156 			   | CARDBUS_COMMAND_MEM_ENABLE);
    157 
    158 	sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
    159 					   IPL_USB, ohci_intr, sc);
    160 	if (sc->sc_ih == NULL) {
    161 		printf("%s: couldn't establish interrupt\n", devname);
    162 		return;
    163 	}
    164 	printf("%s: interrupting at %d\n", devname, ca->ca_intrline);
    165 
    166 	/* Figure out vendor for root hub descriptor. */
    167 	vendor = cardbus_findvendor(ca->ca_id);
    168 	sc->sc.sc_id_vendor = CARDBUS_VENDOR(ca->ca_id);
    169 	if (vendor)
    170 		strncpy(sc->sc.sc_vendor, vendor,
    171 			sizeof(sc->sc.sc_vendor) - 1);
    172 	else
    173 		sprintf(sc->sc.sc_vendor, "vendor 0x%04x",
    174 			CARDBUS_VENDOR(ca->ca_id));
    175 
    176 	r = ohci_init(&sc->sc);
    177 	if (r != USBD_NORMAL_COMPLETION) {
    178 		printf("%s: init failed, error=%d\n", devname, r);
    179 
    180 		/* Avoid spurious interrupts. */
    181 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    182 		sc->sc_ih = 0;
    183 
    184 		return;
    185 	}
    186 
    187 	/* Attach usb device. */
    188 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
    189 				       usbctlprint);
    190 }
    191 
    192 int
    193 ohci_cardbus_detach(self, flags)
    194 	device_ptr_t self;
    195 	int flags;
    196 {
    197 	struct ohci_cardbus_softc *sc = (struct ohci_cardbus_softc *)self;
    198 	int rv;
    199 
    200 	rv = ohci_detach(&sc->sc, flags);
    201 	if (rv)
    202 		return (rv);
    203 	if (sc->sc_ih) {
    204 		cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
    205 		sc->sc_ih = 0;
    206 	}
    207 	if (sc->sc_size) {
    208 		bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc_size);
    209 		sc->sc_size = 0;
    210 	}
    211 	return (0);
    212 }
    213