xref: /netbsd-src/sys/dev/cardbus/uhci_cardbus.c (revision fdd524d4ccd2bb0c6f67401e938dabf773eb0372)
1 /*	$NetBSD: uhci_cardbus.c,v 1.23 2016/07/07 06:55:41 msaitoh Exp $	*/
2 
3 /*
4  * Copyright (c) 1998-2005 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@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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <sys/cdefs.h>
34 __KERNEL_RCSID(0, "$NetBSD: uhci_cardbus.c,v 1.23 2016/07/07 06:55:41 msaitoh Exp $");
35 
36 #include "ehci_cardbus.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/device.h>
42 #include <sys/proc.h>
43 #include <sys/queue.h>
44 
45 #include <sys/bus.h>
46 
47 #include <dev/cardbus/cardbusvar.h>
48 #include <dev/cardbus/usb_cardbus.h>
49 
50 #include <dev/usb/usb.h>
51 #include <dev/usb/usbdi.h>
52 #include <dev/usb/usbdivar.h>
53 #include <dev/usb/usb_mem.h>
54 
55 #include <dev/usb/uhcireg.h>
56 #include <dev/usb/uhcivar.h>
57 
58 struct uhci_cardbus_softc {
59 	uhci_softc_t		sc;
60 #if NEHCI_CARDBUS > 0
61 	struct usb_cardbus	sc_cardbus;
62 #endif
63 	cardbus_chipset_tag_t	sc_cc;
64 	cardbus_function_tag_t	sc_cf;
65 	cardbus_devfunc_t	sc_ct;
66 	pcitag_t		sc_tag;
67 	void 			*sc_ih;		/* interrupt vectoring */
68 };
69 
70 static int	uhci_cardbus_match(device_t, cfdata_t, void *);
71 static void	uhci_cardbus_attach(device_t, device_t, void *);
72 static int	uhci_cardbus_detach(device_t, int);
73 
74 CFATTACH_DECL_NEW(uhci_cardbus, sizeof(struct uhci_cardbus_softc),
75     uhci_cardbus_match, uhci_cardbus_attach, uhci_cardbus_detach,
76     uhci_activate);
77 
78 static int
79 uhci_cardbus_match(device_t parent, cfdata_t match, void *aux)
80 {
81 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
82 
83 	if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
84 	    PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
85 	    PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_UHCI)
86 		return 1;
87 
88 	return 0;
89 }
90 
91 static void
92 uhci_cardbus_attach(device_t parent, device_t self,
93     void *aux)
94 {
95 	struct uhci_cardbus_softc *sc = device_private(self);
96 	struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
97 	cardbus_devfunc_t ct = ca->ca_ct;
98 	cardbus_chipset_tag_t cc = ct->ct_cc;
99 	cardbus_function_tag_t cf = ct->ct_cf;
100 	pcitag_t tag = ca->ca_tag;
101 	pcireg_t csr;
102 	const char *devname = device_xname(self);
103 	char devinfo[256];
104 
105 	sc->sc.sc_dev = self;
106 	sc->sc.sc_bus.ub_hcpriv = sc;
107 
108 	pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo));
109 	printf(": %s (rev. 0x%02x)\n", devinfo, PCI_REVISION(ca->ca_class));
110 
111 	/* Map I/O registers */
112 	if (Cardbus_mapreg_map(ct, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
113 			   &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
114 		printf("%s: can't map i/o space\n", devname);
115 		return;
116 	}
117 
118 	sc->sc_cc = cc;
119 	sc->sc_cf = cf;
120 	sc->sc_ct = ct;
121 	sc->sc_tag = tag;
122 	sc->sc.sc_bus.ub_dmatag = ca->ca_dmat;
123 
124 	/* Enable the device. */
125 	csr = Cardbus_conf_read(ct, tag, PCI_COMMAND_STATUS_REG);
126 	Cardbus_conf_write(ct, tag, PCI_COMMAND_STATUS_REG,
127 		       csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
128 
129 	/* Disable interrupts, so we don't get any spurious ones. */
130 	bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
131 
132 	/* Map and establish the interrupt. */
133 	sc->sc_ih = Cardbus_intr_establish(ct, IPL_USB, uhci_intr, sc);
134 	if (sc->sc_ih == NULL) {
135 		printf("%s: couldn't establish interrupt\n", devname);
136 		return;
137 	}
138 
139 	/* Set LEGSUP register to its default value. */
140 	Cardbus_conf_write(ct, tag, PCI_LEGSUP, PCI_LEGSUP_USBPIRQDEN);
141 
142 	switch(Cardbus_conf_read(ct, tag, PCI_USBREV) & PCI_USBREV_MASK) {
143 	case PCI_USBREV_PRE_1_0:
144 		sc->sc.sc_bus.ub_revision = USBREV_PRE_1_0;
145 		break;
146 	case PCI_USBREV_1_0:
147 		sc->sc.sc_bus.ub_revision = USBREV_1_0;
148 		break;
149 	case PCI_USBREV_1_1:
150 		sc->sc.sc_bus.ub_revision = USBREV_1_1;
151 		break;
152 	default:
153 		sc->sc.sc_bus.ub_revision = USBREV_UNKNOWN;
154 		break;
155 	}
156 
157 	/* Figure out vendor for root hub descriptor. */
158 	sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
159 	pci_findvendor(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
160 	    sc->sc.sc_id_vendor);
161 
162 	int err = uhci_init(&sc->sc);
163 	if (err) {
164 		printf("%s: init failed, error=%d\n", devname, err);
165 
166 		/* Avoid spurious interrupts. */
167 		Cardbus_intr_disestablish(ct, sc->sc_ih);
168 		sc->sc_ih = NULL;
169 
170 		return;
171 	}
172 
173 #if NEHCI_CARDBUS > 0
174 	usb_cardbus_add(&sc->sc_cardbus, ca, self);
175 #endif
176 
177 	/* Attach usb device. */
178 	sc->sc.sc_child = config_found(self, &sc->sc.sc_bus, usbctlprint);
179 }
180 
181 static int
182 uhci_cardbus_detach(device_t self, int flags)
183 {
184 	struct uhci_cardbus_softc *sc = device_private(self);
185 	struct cardbus_devfunc *ct = sc->sc_ct;
186 	int rv;
187 
188 	rv = uhci_detach(&sc->sc, flags);
189 	if (rv)
190 		return rv;
191 	if (sc->sc_ih != NULL) {
192 		Cardbus_intr_disestablish(ct, sc->sc_ih);
193 		sc->sc_ih = NULL;
194 	}
195 	if (sc->sc.sc_size) {
196 		Cardbus_mapreg_unmap(ct, PCI_CBIO, sc->sc.iot,
197 		    sc->sc.ioh, sc->sc.sc_size);
198 		sc->sc.sc_size = 0;
199 	}
200 #if NEHCI_CARDBUS > 0
201 	usb_cardbus_rem(&sc->sc_cardbus);
202 #endif
203 	return 0;
204 }
205