xref: /freebsd/sys/dev/usb/controller/ohci_pci.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 1998 The NetBSD Foundation, Inc.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to The NetBSD Foundation
6  * by Lennart Augustsson (augustss@carlstedt.se) at
7  * Carlstedt Research & Technology.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *        This product includes software developed by the NetBSD
20  *        Foundation, Inc. and its contributors.
21  * 4. Neither the name of The NetBSD Foundation nor the names of its
22  *    contributors may be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
26  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * USB Open Host Controller driver.
43  *
44  * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
45  */
46 
47 /* The low level controller code for OHCI has been split into
48  * PCI probes and OHCI specific code. This was done to facilitate the
49  * sharing of code between *BSD's
50  */
51 
52 #include <sys/stdint.h>
53 #include <sys/stddef.h>
54 #include <sys/param.h>
55 #include <sys/queue.h>
56 #include <sys/types.h>
57 #include <sys/systm.h>
58 #include <sys/kernel.h>
59 #include <sys/bus.h>
60 #include <sys/linker_set.h>
61 #include <sys/module.h>
62 #include <sys/lock.h>
63 #include <sys/mutex.h>
64 #include <sys/condvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/sx.h>
67 #include <sys/unistd.h>
68 #include <sys/callout.h>
69 #include <sys/malloc.h>
70 #include <sys/priv.h>
71 
72 #include <dev/usb/usb.h>
73 #include <dev/usb/usbdi.h>
74 
75 #include <dev/usb/usb_core.h>
76 #include <dev/usb/usb_busdma.h>
77 #include <dev/usb/usb_process.h>
78 #include <dev/usb/usb_util.h>
79 
80 #include <dev/usb/usb_controller.h>
81 #include <dev/usb/usb_bus.h>
82 #include <dev/usb/usb_pci.h>
83 #include <dev/usb/controller/ohci.h>
84 
85 #define	PCI_OHCI_VENDORID_ACERLABS	0x10b9
86 #define	PCI_OHCI_VENDORID_AMD		0x1022
87 #define	PCI_OHCI_VENDORID_APPLE		0x106b
88 #define	PCI_OHCI_VENDORID_ATI		0x1002
89 #define	PCI_OHCI_VENDORID_CMDTECH	0x1095
90 #define	PCI_OHCI_VENDORID_NEC		0x1033
91 #define	PCI_OHCI_VENDORID_NVIDIA	0x12D2
92 #define	PCI_OHCI_VENDORID_NVIDIA2	0x10DE
93 #define	PCI_OHCI_VENDORID_OPTI		0x1045
94 #define	PCI_OHCI_VENDORID_SIS		0x1039
95 #define	PCI_OHCI_VENDORID_SUN		0x108e
96 
97 #define	PCI_OHCI_BASE_REG	0x10
98 
99 static device_probe_t ohci_pci_probe;
100 static device_attach_t ohci_pci_attach;
101 static device_detach_t ohci_pci_detach;
102 static device_suspend_t ohci_pci_suspend;
103 static device_resume_t ohci_pci_resume;
104 
105 static int
106 ohci_pci_suspend(device_t self)
107 {
108 	ohci_softc_t *sc = device_get_softc(self);
109 	int err;
110 
111 	err = bus_generic_suspend(self);
112 	if (err) {
113 		return (err);
114 	}
115 	ohci_suspend(sc);
116 	return (0);
117 }
118 
119 static int
120 ohci_pci_resume(device_t self)
121 {
122 	ohci_softc_t *sc = device_get_softc(self);
123 	uint32_t reg, int_line;
124 
125 	if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
126 		device_printf(self, "chip is in D%d mode "
127 		    "-- setting to D0\n", pci_get_powerstate(self));
128 		reg = pci_read_config(self, PCI_CBMEM, 4);
129 		int_line = pci_read_config(self, PCIR_INTLINE, 4);
130 		pci_set_powerstate(self, PCI_POWERSTATE_D0);
131 		pci_write_config(self, PCI_CBMEM, reg, 4);
132 		pci_write_config(self, PCIR_INTLINE, int_line, 4);
133 	}
134 	ohci_resume(sc);
135 
136 	bus_generic_resume(self);
137 	return (0);
138 }
139 
140 static const char *
141 ohci_pci_match(device_t self)
142 {
143 	uint32_t device_id = pci_get_devid(self);
144 
145 	switch (device_id) {
146 	case 0x523710b9:
147 		return ("AcerLabs M5237 (Aladdin-V) USB controller");
148 
149 	case 0x740c1022:
150 		return ("AMD-756 USB Controller");
151 
152 	case 0x74141022:
153 		return ("AMD-766 USB Controller");
154 
155 	case 0x43741002:
156 		return "ATI SB400 USB Controller";
157 	case 0x43751002:
158 		return "ATI SB400 USB Controller";
159 
160 	case 0x06701095:
161 		return ("CMD Tech 670 (USB0670) USB controller");
162 
163 	case 0x06731095:
164 		return ("CMD Tech 673 (USB0673) USB controller");
165 
166 	case 0xc8611045:
167 		return ("OPTi 82C861 (FireLink) USB controller");
168 
169 	case 0x00351033:
170 		return ("NEC uPD 9210 USB controller");
171 
172 	case 0x00d710de:
173 		return ("nVidia nForce3 USB Controller");
174 
175 	case 0x03f110de:
176 		return ("nVidia nForce MCP61 USB Controller");
177 
178 	case 0x70011039:
179 		return ("SiS 5571 USB controller");
180 
181 	case 0x1103108e:
182 		return "Sun PCIO-2 USB controller";
183 
184 	case 0x0019106b:
185 		return ("Apple KeyLargo USB controller");
186 
187 	default:
188 		break;
189 	}
190 	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
191 	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
192 	    (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
193 		return ("OHCI (generic) USB controller");
194 	}
195 	return (NULL);
196 }
197 
198 static int
199 ohci_pci_probe(device_t self)
200 {
201 	const char *desc = ohci_pci_match(self);
202 
203 	if (desc) {
204 		device_set_desc(self, desc);
205 		return (0);
206 	} else {
207 		return (ENXIO);
208 	}
209 }
210 
211 static int
212 ohci_pci_attach(device_t self)
213 {
214 	ohci_softc_t *sc = device_get_softc(self);
215 	int rid;
216 	int err;
217 
218 	/* initialise some bus fields */
219 	sc->sc_bus.parent = self;
220 	sc->sc_bus.devices = sc->sc_devices;
221 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
222 
223 	/* get all DMA memory */
224 	if (usb_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
225 	    &ohci_iterate_hw_softc)) {
226 		return (ENOMEM);
227 	}
228 	sc->sc_dev = self;
229 
230 	pci_enable_busmaster(self);
231 
232 	/*
233 	 * Some Sun PCIO-2 USB controllers have their intpin register
234 	 * bogusly set to 0, although it should be 4.  Correct that.
235 	 */
236 	if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
237 		pci_set_intpin(self, 4);
238 
239 	rid = PCI_CBMEM;
240 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
241 	    RF_ACTIVE);
242 	if (!sc->sc_io_res) {
243 		device_printf(self, "Could not map memory\n");
244 		goto error;
245 	}
246 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
247 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
248 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
249 
250 	rid = 0;
251 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
252 	    RF_SHAREABLE | RF_ACTIVE);
253 	if (sc->sc_irq_res == NULL) {
254 		device_printf(self, "Could not allocate irq\n");
255 		goto error;
256 	}
257 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
258 	if (!sc->sc_bus.bdev) {
259 		device_printf(self, "Could not add USB device\n");
260 		goto error;
261 	}
262 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
263 
264 	/*
265 	 * ohci_pci_match will never return NULL if ohci_pci_probe
266 	 * succeeded
267 	 */
268 	device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
269 	switch (pci_get_vendor(self)) {
270 	case PCI_OHCI_VENDORID_ACERLABS:
271 		sprintf(sc->sc_vendor, "AcerLabs");
272 		break;
273 	case PCI_OHCI_VENDORID_AMD:
274 		sprintf(sc->sc_vendor, "AMD");
275 		break;
276 	case PCI_OHCI_VENDORID_APPLE:
277 		sprintf(sc->sc_vendor, "Apple");
278 		break;
279 	case PCI_OHCI_VENDORID_ATI:
280 		sprintf(sc->sc_vendor, "ATI");
281 		break;
282 	case PCI_OHCI_VENDORID_CMDTECH:
283 		sprintf(sc->sc_vendor, "CMDTECH");
284 		break;
285 	case PCI_OHCI_VENDORID_NEC:
286 		sprintf(sc->sc_vendor, "NEC");
287 		break;
288 	case PCI_OHCI_VENDORID_NVIDIA:
289 	case PCI_OHCI_VENDORID_NVIDIA2:
290 		sprintf(sc->sc_vendor, "nVidia");
291 		break;
292 	case PCI_OHCI_VENDORID_OPTI:
293 		sprintf(sc->sc_vendor, "OPTi");
294 		break;
295 	case PCI_OHCI_VENDORID_SIS:
296 		sprintf(sc->sc_vendor, "SiS");
297 		break;
298 	case PCI_OHCI_VENDORID_SUN:
299 		sprintf(sc->sc_vendor, "SUN");
300 		break;
301 	default:
302 		if (bootverbose) {
303 			device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
304 			    pci_get_devid(self));
305 		}
306 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
307 	}
308 
309 	/* sc->sc_bus.usbrev; set by ohci_init() */
310 
311 #if (__FreeBSD_version >= 700031)
312 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
313 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
314 #else
315 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
316 	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
317 #endif
318 	if (err) {
319 		device_printf(self, "Could not setup irq, %d\n", err);
320 		sc->sc_intr_hdl = NULL;
321 		goto error;
322 	}
323 	err = ohci_init(sc);
324 	if (!err) {
325 		err = device_probe_and_attach(sc->sc_bus.bdev);
326 	}
327 	if (err) {
328 		device_printf(self, "USB init failed\n");
329 		goto error;
330 	}
331 	return (0);
332 
333 error:
334 	ohci_pci_detach(self);
335 	return (ENXIO);
336 }
337 
338 static int
339 ohci_pci_detach(device_t self)
340 {
341 	ohci_softc_t *sc = device_get_softc(self);
342 	device_t bdev;
343 
344 	if (sc->sc_bus.bdev) {
345 		bdev = sc->sc_bus.bdev;
346 		device_detach(bdev);
347 		device_delete_child(self, bdev);
348 	}
349 	/* during module unload there are lots of children leftover */
350 	device_delete_all_children(self);
351 
352 	pci_disable_busmaster(self);
353 
354 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
355 		/*
356 		 * only call ohci_detach() after ohci_init()
357 		 */
358 		ohci_detach(sc);
359 
360 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
361 
362 		if (err) {
363 			/* XXX or should we panic? */
364 			device_printf(self, "Could not tear down irq, %d\n",
365 			    err);
366 		}
367 		sc->sc_intr_hdl = NULL;
368 	}
369 	if (sc->sc_irq_res) {
370 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
371 		sc->sc_irq_res = NULL;
372 	}
373 	if (sc->sc_io_res) {
374 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
375 		    sc->sc_io_res);
376 		sc->sc_io_res = NULL;
377 	}
378 	usb_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
379 
380 	return (0);
381 }
382 
383 static driver_t ohci_driver =
384 {
385 	.name = "ohci",
386 	.methods = (device_method_t[]){
387 		/* device interface */
388 		DEVMETHOD(device_probe, ohci_pci_probe),
389 		DEVMETHOD(device_attach, ohci_pci_attach),
390 		DEVMETHOD(device_detach, ohci_pci_detach),
391 		DEVMETHOD(device_suspend, ohci_pci_suspend),
392 		DEVMETHOD(device_resume, ohci_pci_resume),
393 		DEVMETHOD(device_shutdown, bus_generic_shutdown),
394 
395 		/* bus interface */
396 		DEVMETHOD(bus_print_child, bus_generic_print_child),
397 
398 		{0, 0}
399 	},
400 	.size = sizeof(struct ohci_softc),
401 };
402 
403 static devclass_t ohci_devclass;
404 
405 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
406 MODULE_DEPEND(ohci, usb, 1, 1, 1);
407