Home | History | Annotate | Line # | Download | only in cardbus
uhci_cardbus.c revision 1.9.4.5
      1  1.9.4.5      yamt /*	$NetBSD: uhci_cardbus.c,v 1.9.4.5 2010/08/11 22:53:20 yamt Exp $	*/
      2      1.1  augustss 
      3      1.1  augustss /*
      4      1.1  augustss  * Copyright (c) 1998-2005 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.1  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 #include <sys/cdefs.h>
     34  1.9.4.5      yamt __KERNEL_RCSID(0, "$NetBSD: uhci_cardbus.c,v 1.9.4.5 2010/08/11 22:53:20 yamt Exp $");
     35      1.1  augustss 
     36      1.1  augustss #include "ehci_cardbus.h"
     37      1.1  augustss 
     38      1.1  augustss #include <sys/param.h>
     39      1.1  augustss #include <sys/systm.h>
     40      1.1  augustss #include <sys/kernel.h>
     41      1.1  augustss #include <sys/device.h>
     42      1.1  augustss #include <sys/proc.h>
     43      1.1  augustss #include <sys/queue.h>
     44      1.1  augustss 
     45      1.6        ad #include <sys/bus.h>
     46      1.1  augustss 
     47      1.1  augustss #include <dev/cardbus/cardbusvar.h>
     48      1.1  augustss #include <dev/cardbus/usb_cardbus.h>
     49      1.1  augustss 
     50      1.1  augustss #include <dev/usb/usb.h>
     51      1.1  augustss #include <dev/usb/usbdi.h>
     52      1.1  augustss #include <dev/usb/usbdivar.h>
     53      1.1  augustss #include <dev/usb/usb_mem.h>
     54      1.1  augustss 
     55      1.1  augustss #include <dev/usb/uhcireg.h>
     56      1.1  augustss #include <dev/usb/uhcivar.h>
     57      1.1  augustss 
     58      1.1  augustss struct uhci_cardbus_softc {
     59      1.1  augustss 	uhci_softc_t		sc;
     60      1.1  augustss #if NEHCI_CARDBUS > 0
     61      1.1  augustss 	struct usb_cardbus	sc_cardbus;
     62      1.1  augustss #endif
     63      1.1  augustss 	cardbus_chipset_tag_t	sc_cc;
     64      1.1  augustss 	cardbus_function_tag_t	sc_cf;
     65      1.1  augustss 	cardbus_devfunc_t	sc_ct;
     66  1.9.4.4      yamt 	pcitag_t		sc_tag;
     67      1.1  augustss 	void 			*sc_ih;		/* interrupt vectoring */
     68      1.1  augustss };
     69      1.1  augustss 
     70  1.9.4.3      yamt static int	uhci_cardbus_match(device_t, cfdata_t, void *);
     71      1.7    dyoung static void	uhci_cardbus_attach(device_t, device_t, void *);
     72      1.7    dyoung static int	uhci_cardbus_detach(device_t, int);
     73      1.1  augustss 
     74      1.8  drochner CFATTACH_DECL_NEW(uhci_cardbus, sizeof(struct uhci_cardbus_softc),
     75      1.1  augustss     uhci_cardbus_match, uhci_cardbus_attach, uhci_cardbus_detach, uhci_activate);
     76      1.1  augustss 
     77      1.1  augustss static int
     78  1.9.4.3      yamt uhci_cardbus_match(device_t parent, cfdata_t match, void *aux)
     79      1.1  augustss {
     80      1.1  augustss 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
     81      1.1  augustss 
     82  1.9.4.4      yamt 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
     83  1.9.4.4      yamt 	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
     84  1.9.4.4      yamt 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_UHCI)
     85      1.1  augustss 		return (1);
     86      1.1  augustss 
     87      1.1  augustss 	return (0);
     88      1.1  augustss }
     89      1.1  augustss 
     90      1.1  augustss static void
     91      1.7    dyoung uhci_cardbus_attach(device_t parent, device_t self,
     92      1.4  christos     void *aux)
     93      1.1  augustss {
     94      1.3   thorpej 	struct uhci_cardbus_softc *sc = device_private(self);
     95      1.1  augustss 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
     96      1.1  augustss 	cardbus_devfunc_t ct = ca->ca_ct;
     97      1.1  augustss 	cardbus_chipset_tag_t cc = ct->ct_cc;
     98      1.1  augustss 	cardbus_function_tag_t cf = ct->ct_cf;
     99  1.9.4.4      yamt 	pcitag_t tag = ca->ca_tag;
    100  1.9.4.4      yamt 	pcireg_t csr;
    101      1.1  augustss 	const char *vendor;
    102      1.8  drochner 	const char *devname = device_xname(self);
    103      1.1  augustss 	char devinfo[256];
    104      1.1  augustss 	usbd_status r;
    105      1.1  augustss 
    106      1.8  drochner 	sc->sc.sc_dev = self;
    107      1.9       skd 	sc->sc.sc_bus.hci_private = sc;
    108      1.8  drochner 
    109  1.9.4.4      yamt 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
    110  1.9.4.4      yamt 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(ca->ca_class));
    111      1.1  augustss 
    112      1.1  augustss 	/* Map I/O registers */
    113  1.9.4.4      yamt 	if (Cardbus_mapreg_map(ct, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
    114      1.1  augustss 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
    115      1.1  augustss 		printf("%s: can't map i/o space\n", devname);
    116      1.1  augustss 		return;
    117      1.1  augustss 	}
    118      1.1  augustss 
    119      1.1  augustss 	sc->sc_cc = cc;
    120      1.1  augustss 	sc->sc_cf = cf;
    121      1.1  augustss 	sc->sc_ct = ct;
    122      1.1  augustss 	sc->sc_tag = tag;
    123      1.1  augustss 	sc->sc.sc_bus.dmatag = ca->ca_dmat;
    124      1.1  augustss 
    125      1.1  augustss 	/* Enable the device. */
    126  1.9.4.4      yamt 	csr = Cardbus_conf_read(ct, tag, PCI_COMMAND_STATUS_REG);
    127  1.9.4.4      yamt 	Cardbus_conf_write(ct, tag, PCI_COMMAND_STATUS_REG,
    128  1.9.4.4      yamt 		       csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
    129      1.1  augustss 
    130  1.9.4.2      yamt 	/* Disable interrupts, so we don't get any spurious ones. */
    131  1.9.4.2      yamt 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
    132  1.9.4.2      yamt 
    133      1.1  augustss 	/* Map and establish the interrupt. */
    134  1.9.4.5      yamt 	sc->sc_ih = Cardbus_intr_establish(ct, ca->ca_intrline,
    135      1.1  augustss 					   IPL_USB, uhci_intr, sc);
    136      1.1  augustss 	if (sc->sc_ih == NULL) {
    137      1.1  augustss 		printf("%s: couldn't establish interrupt\n", devname);
    138      1.1  augustss 		return;
    139      1.1  augustss 	}
    140      1.1  augustss 
    141      1.1  augustss 	/* Set LEGSUP register to its default value. */
    142  1.9.4.4      yamt 	Cardbus_conf_write(ct, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
    143      1.1  augustss 
    144  1.9.4.4      yamt 	switch(Cardbus_conf_read(ct, tag, PCI_USBREV) & PCI_USBREV_MASK) {
    145      1.1  augustss 	case PCI_USBREV_PRE_1_0:
    146      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
    147      1.1  augustss 		break;
    148      1.1  augustss 	case PCI_USBREV_1_0:
    149      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_1_0;
    150      1.1  augustss 		break;
    151      1.1  augustss 	case PCI_USBREV_1_1:
    152      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_1_1;
    153      1.1  augustss 		break;
    154      1.1  augustss 	default:
    155      1.1  augustss 		sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
    156      1.1  augustss 		break;
    157      1.1  augustss 	}
    158      1.1  augustss 
    159      1.1  augustss 	/* Figure out vendor for root hub descriptor. */
    160  1.9.4.4      yamt 	vendor = pci_findvendor(ca->ca_id);
    161  1.9.4.4      yamt 	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
    162      1.1  augustss 	if (vendor)
    163      1.1  augustss 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
    164      1.1  augustss 	else
    165      1.1  augustss 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
    166  1.9.4.4      yamt 		    "vendor 0x%04x", PCI_VENDOR(ca->ca_id));
    167      1.1  augustss 
    168      1.1  augustss 	r = uhci_init(&sc->sc);
    169      1.1  augustss 	if (r != USBD_NORMAL_COMPLETION) {
    170      1.1  augustss 		printf("%s: init failed, error=%d\n", devname, r);
    171      1.1  augustss 
    172      1.1  augustss 		/* Avoid spurious interrupts. */
    173  1.9.4.5      yamt 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    174      1.1  augustss 		sc->sc_ih = NULL;
    175      1.1  augustss 
    176      1.1  augustss 		return;
    177      1.1  augustss 	}
    178      1.1  augustss 
    179      1.1  augustss #if NEHCI_CARDBUS > 0
    180      1.8  drochner 	usb_cardbus_add(&sc->sc_cardbus, ca, self);
    181      1.1  augustss #endif
    182      1.1  augustss 
    183      1.1  augustss 	/* Attach usb device. */
    184      1.7    dyoung 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
    185      1.1  augustss }
    186      1.1  augustss 
    187      1.1  augustss static int
    188      1.7    dyoung uhci_cardbus_detach(device_t self, int flags)
    189      1.1  augustss {
    190      1.3   thorpej 	struct uhci_cardbus_softc *sc = device_private(self);
    191      1.1  augustss 	struct cardbus_devfunc *ct = sc->sc_ct;
    192      1.1  augustss 	int rv;
    193      1.1  augustss 
    194      1.1  augustss 	rv = uhci_detach(&sc->sc, flags);
    195      1.1  augustss 	if (rv)
    196      1.1  augustss 		return (rv);
    197      1.1  augustss 	if (sc->sc_ih != NULL) {
    198  1.9.4.5      yamt 		Cardbus_intr_disestablish(ct, sc->sc_ih);
    199      1.1  augustss 		sc->sc_ih = NULL;
    200      1.1  augustss 	}
    201      1.1  augustss 	if (sc->sc.sc_size) {
    202  1.9.4.4      yamt 		Cardbus_mapreg_unmap(ct, PCI_CBIO, sc->sc.iot,
    203      1.1  augustss 		    sc->sc.ioh, sc->sc.sc_size);
    204      1.1  augustss 		sc->sc.sc_size = 0;
    205      1.1  augustss 	}
    206      1.1  augustss #if NEHCI_CARDBUS > 0
    207      1.1  augustss 	usb_cardbus_rem(&sc->sc_cardbus);
    208      1.1  augustss #endif
    209      1.1  augustss 	return (0);
    210      1.1  augustss }
    211