xref: /freebsd/sys/dev/usb/controller/ohci_pci.c (revision c0020399a650364d0134f79f3fa319f84064372d)
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 <dev/usb/usb.h>
53 #include <dev/usb/usb_mfunc.h>
54 #include <dev/usb/usb_error.h>
55 
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_util.h>
60 
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.h>
63 #include <dev/usb/usb_pci.h>
64 #include <dev/usb/controller/ohci.h>
65 
66 #define	PCI_OHCI_VENDORID_ACERLABS	0x10b9
67 #define	PCI_OHCI_VENDORID_AMD		0x1022
68 #define	PCI_OHCI_VENDORID_APPLE		0x106b
69 #define	PCI_OHCI_VENDORID_ATI		0x1002
70 #define	PCI_OHCI_VENDORID_CMDTECH	0x1095
71 #define	PCI_OHCI_VENDORID_NEC		0x1033
72 #define	PCI_OHCI_VENDORID_NVIDIA	0x12D2
73 #define	PCI_OHCI_VENDORID_NVIDIA2	0x10DE
74 #define	PCI_OHCI_VENDORID_OPTI		0x1045
75 #define	PCI_OHCI_VENDORID_SIS		0x1039
76 #define	PCI_OHCI_VENDORID_SUN		0x108e
77 
78 #define	PCI_OHCI_BASE_REG	0x10
79 
80 static device_probe_t ohci_pci_probe;
81 static device_attach_t ohci_pci_attach;
82 static device_detach_t ohci_pci_detach;
83 static device_suspend_t ohci_pci_suspend;
84 static device_resume_t ohci_pci_resume;
85 
86 static int
87 ohci_pci_suspend(device_t self)
88 {
89 	ohci_softc_t *sc = device_get_softc(self);
90 	int err;
91 
92 	err = bus_generic_suspend(self);
93 	if (err) {
94 		return (err);
95 	}
96 	ohci_suspend(sc);
97 	return (0);
98 }
99 
100 static int
101 ohci_pci_resume(device_t self)
102 {
103 	ohci_softc_t *sc = device_get_softc(self);
104 	uint32_t reg, int_line;
105 
106 	if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
107 		device_printf(self, "chip is in D%d mode "
108 		    "-- setting to D0\n", pci_get_powerstate(self));
109 		reg = pci_read_config(self, PCI_CBMEM, 4);
110 		int_line = pci_read_config(self, PCIR_INTLINE, 4);
111 		pci_set_powerstate(self, PCI_POWERSTATE_D0);
112 		pci_write_config(self, PCI_CBMEM, reg, 4);
113 		pci_write_config(self, PCIR_INTLINE, int_line, 4);
114 	}
115 	ohci_resume(sc);
116 
117 	bus_generic_resume(self);
118 	return (0);
119 }
120 
121 static const char *
122 ohci_pci_match(device_t self)
123 {
124 	uint32_t device_id = pci_get_devid(self);
125 
126 	switch (device_id) {
127 	case 0x523710b9:
128 		return ("AcerLabs M5237 (Aladdin-V) USB controller");
129 
130 	case 0x740c1022:
131 		return ("AMD-756 USB Controller");
132 
133 	case 0x74141022:
134 		return ("AMD-766 USB Controller");
135 
136 	case 0x43741002:
137 		return "ATI SB400 USB Controller";
138 	case 0x43751002:
139 		return "ATI SB400 USB Controller";
140 
141 	case 0x06701095:
142 		return ("CMD Tech 670 (USB0670) USB controller");
143 
144 	case 0x06731095:
145 		return ("CMD Tech 673 (USB0673) USB controller");
146 
147 	case 0xc8611045:
148 		return ("OPTi 82C861 (FireLink) USB controller");
149 
150 	case 0x00351033:
151 		return ("NEC uPD 9210 USB controller");
152 
153 	case 0x00d710de:
154 		return ("nVidia nForce3 USB Controller");
155 
156 	case 0x70011039:
157 		return ("SiS 5571 USB controller");
158 
159 	case 0x1103108e:
160 		return "Sun PCIO-2 USB controller";
161 
162 	case 0x0019106b:
163 		return ("Apple KeyLargo USB controller");
164 
165 	default:
166 		break;
167 	}
168 	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
169 	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
170 	    (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
171 		return ("OHCI (generic) USB controller");
172 	}
173 	return (NULL);
174 }
175 
176 static int
177 ohci_pci_probe(device_t self)
178 {
179 	const char *desc = ohci_pci_match(self);
180 
181 	if (desc) {
182 		device_set_desc(self, desc);
183 		return (0);
184 	} else {
185 		return (ENXIO);
186 	}
187 }
188 
189 static int
190 ohci_pci_attach(device_t self)
191 {
192 	ohci_softc_t *sc = device_get_softc(self);
193 	int rid;
194 	int err;
195 
196 	/* initialise some bus fields */
197 	sc->sc_bus.parent = self;
198 	sc->sc_bus.devices = sc->sc_devices;
199 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
200 
201 	/* get all DMA memory */
202 	if (usb2_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
203 	    &ohci_iterate_hw_softc)) {
204 		return (ENOMEM);
205 	}
206 	sc->sc_dev = self;
207 
208 	pci_enable_busmaster(self);
209 
210 	/*
211 	 * Some Sun PCIO-2 USB controllers have their intpin register
212 	 * bogusly set to 0, although it should be 4.  Correct that.
213 	 */
214 	if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
215 		pci_set_intpin(self, 4);
216 
217 	rid = PCI_CBMEM;
218 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
219 	    RF_ACTIVE);
220 	if (!sc->sc_io_res) {
221 		device_printf(self, "Could not map memory\n");
222 		goto error;
223 	}
224 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
225 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
226 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
227 
228 	rid = 0;
229 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
230 	    RF_SHAREABLE | RF_ACTIVE);
231 	if (sc->sc_irq_res == NULL) {
232 		device_printf(self, "Could not allocate irq\n");
233 		goto error;
234 	}
235 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
236 	if (!sc->sc_bus.bdev) {
237 		device_printf(self, "Could not add USB device\n");
238 		goto error;
239 	}
240 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
241 
242 	/*
243 	 * ohci_pci_match will never return NULL if ohci_pci_probe
244 	 * succeeded
245 	 */
246 	device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
247 	switch (pci_get_vendor(self)) {
248 	case PCI_OHCI_VENDORID_ACERLABS:
249 		sprintf(sc->sc_vendor, "AcerLabs");
250 		break;
251 	case PCI_OHCI_VENDORID_AMD:
252 		sprintf(sc->sc_vendor, "AMD");
253 		break;
254 	case PCI_OHCI_VENDORID_APPLE:
255 		sprintf(sc->sc_vendor, "Apple");
256 		break;
257 	case PCI_OHCI_VENDORID_ATI:
258 		sprintf(sc->sc_vendor, "ATI");
259 		break;
260 	case PCI_OHCI_VENDORID_CMDTECH:
261 		sprintf(sc->sc_vendor, "CMDTECH");
262 		break;
263 	case PCI_OHCI_VENDORID_NEC:
264 		sprintf(sc->sc_vendor, "NEC");
265 		break;
266 	case PCI_OHCI_VENDORID_NVIDIA:
267 	case PCI_OHCI_VENDORID_NVIDIA2:
268 		sprintf(sc->sc_vendor, "nVidia");
269 		break;
270 	case PCI_OHCI_VENDORID_OPTI:
271 		sprintf(sc->sc_vendor, "OPTi");
272 		break;
273 	case PCI_OHCI_VENDORID_SIS:
274 		sprintf(sc->sc_vendor, "SiS");
275 		break;
276 	case PCI_OHCI_VENDORID_SUN:
277 		sprintf(sc->sc_vendor, "SUN");
278 		break;
279 	default:
280 		if (bootverbose) {
281 			device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
282 			    pci_get_devid(self));
283 		}
284 		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
285 	}
286 
287 	/* sc->sc_bus.usbrev; set by ohci_init() */
288 
289 #if (__FreeBSD_version >= 700031)
290 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
291 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
292 #else
293 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
294 	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
295 #endif
296 	if (err) {
297 		device_printf(self, "Could not setup irq, %d\n", err);
298 		sc->sc_intr_hdl = NULL;
299 		goto error;
300 	}
301 	err = ohci_init(sc);
302 	if (!err) {
303 		err = device_probe_and_attach(sc->sc_bus.bdev);
304 	}
305 	if (err) {
306 		device_printf(self, "USB init failed\n");
307 		goto error;
308 	}
309 	return (0);
310 
311 error:
312 	ohci_pci_detach(self);
313 	return (ENXIO);
314 }
315 
316 static int
317 ohci_pci_detach(device_t self)
318 {
319 	ohci_softc_t *sc = device_get_softc(self);
320 	device_t bdev;
321 
322 	if (sc->sc_bus.bdev) {
323 		bdev = sc->sc_bus.bdev;
324 		device_detach(bdev);
325 		device_delete_child(self, bdev);
326 	}
327 	/* during module unload there are lots of children leftover */
328 	device_delete_all_children(self);
329 
330 	pci_disable_busmaster(self);
331 
332 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
333 		/*
334 		 * only call ohci_detach() after ohci_init()
335 		 */
336 		ohci_detach(sc);
337 
338 		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
339 
340 		if (err) {
341 			/* XXX or should we panic? */
342 			device_printf(self, "Could not tear down irq, %d\n",
343 			    err);
344 		}
345 		sc->sc_intr_hdl = NULL;
346 	}
347 	if (sc->sc_irq_res) {
348 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
349 		sc->sc_irq_res = NULL;
350 	}
351 	if (sc->sc_io_res) {
352 		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
353 		    sc->sc_io_res);
354 		sc->sc_io_res = NULL;
355 	}
356 	usb2_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
357 
358 	return (0);
359 }
360 
361 static driver_t ohci_driver =
362 {
363 	.name = "ohci",
364 	.methods = (device_method_t[]){
365 		/* device interface */
366 		DEVMETHOD(device_probe, ohci_pci_probe),
367 		DEVMETHOD(device_attach, ohci_pci_attach),
368 		DEVMETHOD(device_detach, ohci_pci_detach),
369 		DEVMETHOD(device_suspend, ohci_pci_suspend),
370 		DEVMETHOD(device_resume, ohci_pci_resume),
371 		DEVMETHOD(device_shutdown, bus_generic_shutdown),
372 
373 		/* bus interface */
374 		DEVMETHOD(bus_print_child, bus_generic_print_child),
375 
376 		{0, 0}
377 	},
378 	.size = sizeof(struct ohci_softc),
379 };
380 
381 static devclass_t ohci_devclass;
382 
383 DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
384 MODULE_DEPEND(ohci, usb, 1, 1, 1);
385