ohci_pci.c revision 190581
1139826Simp/*-
253541Sshin * Copyright (c) 1998 The NetBSD Foundation, Inc.
353541Sshin * All rights reserved.
453541Sshin *
553541Sshin * This code is derived from software contributed to The NetBSD Foundation
653541Sshin * by Lennart Augustsson (augustss@carlstedt.se) at
753541Sshin * Carlstedt Research & Technology.
853541Sshin *
953541Sshin * Redistribution and use in source and binary forms, with or without
1053541Sshin * modification, are permitted provided that the following conditions
1153541Sshin * are met:
1253541Sshin * 1. Redistributions of source code must retain the above copyright
1353541Sshin *    notice, this list of conditions and the following disclaimer.
1453541Sshin * 2. Redistributions in binary form must reproduce the above copyright
1553541Sshin *    notice, this list of conditions and the following disclaimer in the
1653541Sshin *    documentation and/or other materials provided with the distribution.
1753541Sshin * 3. All advertising materials mentioning features or use of this software
1853541Sshin *    must display the following acknowledgement:
1953541Sshin *        This product includes software developed by the NetBSD
2053541Sshin *        Foundation, Inc. and its contributors.
2153541Sshin * 4. Neither the name of The NetBSD Foundation nor the names of its
2253541Sshin *    contributors may be used to endorse or promote products derived
2353541Sshin *    from this software without specific prior written permission.
2453541Sshin *
2553541Sshin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2653541Sshin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2753541Sshin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2853541Sshin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
29174510Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3053541Sshin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3153541Sshin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32139826Simp * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3353541Sshin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3453541Sshin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3553541Sshin * POSSIBILITY OF SUCH DAMAGE.
3653541Sshin */
3753541Sshin
3853541Sshin#include <sys/cdefs.h>
3953541Sshin__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ohci_pci.c 190581 2009-03-30 22:18:38Z mav $");
4053541Sshin
4153541Sshin/*
4253541Sshin * USB Open Host Controller driver.
4353541Sshin *
4453541Sshin * OHCI spec: http://www.intel.com/design/usb/ohci11d.pdf
4553541Sshin */
4653541Sshin
4753541Sshin/* The low level controller code for OHCI has been split into
4853541Sshin * PCI probes and OHCI specific code. This was done to facilitate the
4953541Sshin * sharing of code between *BSD's
5053541Sshin */
5153541Sshin
5253541Sshin#include <dev/usb/usb.h>
5353541Sshin#include <dev/usb/usb_mfunc.h>
5453541Sshin#include <dev/usb/usb_error.h>
5553541Sshin
5653541Sshin#include <dev/usb/usb_core.h>
5753541Sshin#include <dev/usb/usb_busdma.h>
5853541Sshin#include <dev/usb/usb_process.h>
5953541Sshin#include <dev/usb/usb_sw_transfer.h>
6053541Sshin#include <dev/usb/usb_util.h>
61174510Sobrien
6253541Sshin#include <dev/usb/usb_controller.h>
6353541Sshin#include <dev/usb/usb_bus.h>
6453541Sshin#include <dev/usb/usb_pci.h>
6553541Sshin#include <dev/usb/controller/ohci.h>
6653541Sshin
6755205Speter#define	PCI_OHCI_VENDORID_ACERLABS	0x10b9
6853541Sshin#define	PCI_OHCI_VENDORID_AMD		0x1022
6953541Sshin#define	PCI_OHCI_VENDORID_APPLE		0x106b
7053541Sshin#define	PCI_OHCI_VENDORID_ATI		0x1002
7153541Sshin#define	PCI_OHCI_VENDORID_CMDTECH	0x1095
72157978Srwatson#define	PCI_OHCI_VENDORID_NEC		0x1033
7353541Sshin#define	PCI_OHCI_VENDORID_NVIDIA	0x12D2
74127505Spjd#define	PCI_OHCI_VENDORID_NVIDIA2	0x10DE
75127505Spjd#define	PCI_OHCI_VENDORID_OPTI		0x1045
7653541Sshin#define	PCI_OHCI_VENDORID_SIS		0x1039
7753541Sshin#define	PCI_OHCI_VENDORID_SUN		0x108e
78157373Srwatson
7953541Sshin#define	PCI_OHCI_BASE_REG	0x10
8053541Sshin
8153541Sshinstatic device_probe_t ohci_pci_probe;
8253541Sshinstatic device_attach_t ohci_pci_attach;
8353541Sshinstatic device_detach_t ohci_pci_detach;
8453541Sshinstatic device_suspend_t ohci_pci_suspend;
8553541Sshinstatic device_resume_t ohci_pci_resume;
8653541Sshin
8753541Sshinstatic int
88133192Srwatsonohci_pci_suspend(device_t self)
89125776Sume{
9098211Shsu	ohci_softc_t *sc = device_get_softc(self);
9198211Shsu	int err;
9298211Shsu
93102218Struckman	err = bus_generic_suspend(self);
94102218Struckman	if (err) {
95102218Struckman		return (err);
96102218Struckman	}
97169462Srwatson	ohci_suspend(sc);
98169462Srwatson	return (0);
9953541Sshin}
10053541Sshin
10162587Sitojunstatic int
102127505Spjdohci_pci_resume(device_t self)
10362587Sitojun{
10455205Speter	ohci_softc_t *sc = device_get_softc(self);
10553541Sshin	uint32_t reg, int_line;
10653541Sshin
107	if (pci_get_powerstate(self) != PCI_POWERSTATE_D0) {
108		device_printf(self, "chip is in D%d mode "
109		    "-- setting to D0\n", pci_get_powerstate(self));
110		reg = pci_read_config(self, PCI_CBMEM, 4);
111		int_line = pci_read_config(self, PCIR_INTLINE, 4);
112		pci_set_powerstate(self, PCI_POWERSTATE_D0);
113		pci_write_config(self, PCI_CBMEM, reg, 4);
114		pci_write_config(self, PCIR_INTLINE, int_line, 4);
115	}
116	ohci_resume(sc);
117
118	bus_generic_resume(self);
119	return (0);
120}
121
122static const char *
123ohci_pci_match(device_t self)
124{
125	uint32_t device_id = pci_get_devid(self);
126
127	switch (device_id) {
128	case 0x523710b9:
129		return ("AcerLabs M5237 (Aladdin-V) USB controller");
130
131	case 0x740c1022:
132		return ("AMD-756 USB Controller");
133
134	case 0x74141022:
135		return ("AMD-766 USB Controller");
136
137	case 0x43741002:
138		return "ATI SB400 USB Controller";
139	case 0x43751002:
140		return "ATI SB400 USB Controller";
141
142	case 0x06701095:
143		return ("CMD Tech 670 (USB0670) USB controller");
144
145	case 0x06731095:
146		return ("CMD Tech 673 (USB0673) USB controller");
147
148	case 0xc8611045:
149		return ("OPTi 82C861 (FireLink) USB controller");
150
151	case 0x00351033:
152		return ("NEC uPD 9210 USB controller");
153
154	case 0x00d710de:
155		return ("nVidia nForce3 USB Controller");
156
157	case 0x70011039:
158		return ("SiS 5571 USB controller");
159
160	case 0x1103108e:
161		return "Sun PCIO-2 USB controller";
162
163	case 0x0019106b:
164		return ("Apple KeyLargo USB controller");
165
166	default:
167		break;
168	}
169	if ((pci_get_class(self) == PCIC_SERIALBUS) &&
170	    (pci_get_subclass(self) == PCIS_SERIALBUS_USB) &&
171	    (pci_get_progif(self) == PCI_INTERFACE_OHCI)) {
172		return ("OHCI (generic) USB controller");
173	}
174	return (NULL);
175}
176
177static int
178ohci_pci_probe(device_t self)
179{
180	const char *desc = ohci_pci_match(self);
181
182	if (desc) {
183		device_set_desc(self, desc);
184		return (0);
185	} else {
186		return (ENXIO);
187	}
188}
189
190static int
191ohci_pci_attach(device_t self)
192{
193	ohci_softc_t *sc = device_get_softc(self);
194	int rid;
195	int err;
196
197	/* initialise some bus fields */
198	sc->sc_bus.parent = self;
199	sc->sc_bus.devices = sc->sc_devices;
200	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
201
202	/* get all DMA memory */
203	if (usb2_bus_mem_alloc_all(&sc->sc_bus, USB_GET_DMA_TAG(self),
204	    &ohci_iterate_hw_softc)) {
205		return (ENOMEM);
206	}
207	sc->sc_dev = self;
208
209	pci_enable_busmaster(self);
210
211	/*
212	 * Some Sun PCIO-2 USB controllers have their intpin register
213	 * bogusly set to 0, although it should be 4.  Correct that.
214	 */
215	if (pci_get_devid(self) == 0x1103108e && pci_get_intpin(self) == 0)
216		pci_set_intpin(self, 4);
217
218	rid = PCI_CBMEM;
219	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
220	    RF_ACTIVE);
221	if (!sc->sc_io_res) {
222		device_printf(self, "Could not map memory\n");
223		goto error;
224	}
225	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
226	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
227	sc->sc_io_size = rman_get_size(sc->sc_io_res);
228
229	rid = 0;
230	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
231	    RF_SHAREABLE | RF_ACTIVE);
232	if (sc->sc_irq_res == NULL) {
233		device_printf(self, "Could not allocate irq\n");
234		goto error;
235	}
236	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
237	if (!sc->sc_bus.bdev) {
238		device_printf(self, "Could not add USB device\n");
239		goto error;
240	}
241	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
242
243	/*
244	 * ohci_pci_match will never return NULL if ohci_pci_probe
245	 * succeeded
246	 */
247	device_set_desc(sc->sc_bus.bdev, ohci_pci_match(self));
248	switch (pci_get_vendor(self)) {
249	case PCI_OHCI_VENDORID_ACERLABS:
250		sprintf(sc->sc_vendor, "AcerLabs");
251		break;
252	case PCI_OHCI_VENDORID_AMD:
253		sprintf(sc->sc_vendor, "AMD");
254		break;
255	case PCI_OHCI_VENDORID_APPLE:
256		sprintf(sc->sc_vendor, "Apple");
257		break;
258	case PCI_OHCI_VENDORID_ATI:
259		sprintf(sc->sc_vendor, "ATI");
260		break;
261	case PCI_OHCI_VENDORID_CMDTECH:
262		sprintf(sc->sc_vendor, "CMDTECH");
263		break;
264	case PCI_OHCI_VENDORID_NEC:
265		sprintf(sc->sc_vendor, "NEC");
266		break;
267	case PCI_OHCI_VENDORID_NVIDIA:
268	case PCI_OHCI_VENDORID_NVIDIA2:
269		sprintf(sc->sc_vendor, "nVidia");
270		break;
271	case PCI_OHCI_VENDORID_OPTI:
272		sprintf(sc->sc_vendor, "OPTi");
273		break;
274	case PCI_OHCI_VENDORID_SIS:
275		sprintf(sc->sc_vendor, "SiS");
276		break;
277	case PCI_OHCI_VENDORID_SUN:
278		sprintf(sc->sc_vendor, "SUN");
279		break;
280	default:
281		if (bootverbose) {
282			device_printf(self, "(New OHCI DeviceId=0x%08x)\n",
283			    pci_get_devid(self));
284		}
285		sprintf(sc->sc_vendor, "(0x%04x)", pci_get_vendor(self));
286	}
287
288	/* sc->sc_bus.usbrev; set by ohci_init() */
289
290#if (__FreeBSD_version >= 700031)
291	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
292	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
293#else
294	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
295	    (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
296#endif
297	if (err) {
298		device_printf(self, "Could not setup irq, %d\n", err);
299		sc->sc_intr_hdl = NULL;
300		goto error;
301	}
302	err = ohci_init(sc);
303	if (!err) {
304		err = device_probe_and_attach(sc->sc_bus.bdev);
305	}
306	if (err) {
307		device_printf(self, "USB init failed\n");
308		goto error;
309	}
310	return (0);
311
312error:
313	ohci_pci_detach(self);
314	return (ENXIO);
315}
316
317static int
318ohci_pci_detach(device_t self)
319{
320	ohci_softc_t *sc = device_get_softc(self);
321	device_t bdev;
322
323	if (sc->sc_bus.bdev) {
324		bdev = sc->sc_bus.bdev;
325		device_detach(bdev);
326		device_delete_child(self, bdev);
327	}
328	/* during module unload there are lots of children leftover */
329	device_delete_all_children(self);
330
331	pci_disable_busmaster(self);
332
333	if (sc->sc_irq_res && sc->sc_intr_hdl) {
334		/*
335		 * only call ohci_detach() after ohci_init()
336		 */
337		ohci_detach(sc);
338
339		int err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
340
341		if (err) {
342			/* XXX or should we panic? */
343			device_printf(self, "Could not tear down irq, %d\n",
344			    err);
345		}
346		sc->sc_intr_hdl = NULL;
347	}
348	if (sc->sc_irq_res) {
349		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
350		sc->sc_irq_res = NULL;
351	}
352	if (sc->sc_io_res) {
353		bus_release_resource(self, SYS_RES_MEMORY, PCI_CBMEM,
354		    sc->sc_io_res);
355		sc->sc_io_res = NULL;
356	}
357	usb2_bus_mem_free_all(&sc->sc_bus, &ohci_iterate_hw_softc);
358
359	return (0);
360}
361
362static driver_t ohci_driver =
363{
364	.name = "ohci",
365	.methods = (device_method_t[]){
366		/* device interface */
367		DEVMETHOD(device_probe, ohci_pci_probe),
368		DEVMETHOD(device_attach, ohci_pci_attach),
369		DEVMETHOD(device_detach, ohci_pci_detach),
370		DEVMETHOD(device_suspend, ohci_pci_suspend),
371		DEVMETHOD(device_resume, ohci_pci_resume),
372		DEVMETHOD(device_shutdown, bus_generic_shutdown),
373
374		/* bus interface */
375		DEVMETHOD(bus_print_child, bus_generic_print_child),
376
377		{0, 0}
378	},
379	.size = sizeof(struct ohci_softc),
380};
381
382static devclass_t ohci_devclass;
383
384DRIVER_MODULE(ohci, pci, ohci_driver, ohci_devclass, 0, 0);
385MODULE_DEPEND(ohci, usb, 1, 1, 1);
386