xref: /netbsd-src/sys/arch/i386/pci/gcscehci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /* $NetBSD: gcscehci.c,v 1.1 2007/07/08 01:13:26 jmcneill Exp $ */
2 
3 /*
4  * Copyright (c) 2001, 2002, 2007 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)
9  * and Jared D. McNeill (jmcneill@invisible.ca)
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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *        This product includes software developed by the NetBSD
22  *        Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: gcscehci.c,v 1.1 2007/07/08 01:13:26 jmcneill Exp $");
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/device.h>
47 #include <sys/proc.h>
48 #include <sys/queue.h>
49 
50 #include <machine/bus.h>
51 #include <machine/cpufunc.h>
52 
53 #include <dev/pci/pcidevs.h>
54 #include <dev/pci/pcivar.h>
55 #include <dev/pci/usb_pci.h>
56 
57 #include <dev/usb/usb.h>
58 #include <dev/usb/usbdi.h>
59 #include <dev/usb/usbdivar.h>
60 #include <dev/usb/usb_mem.h>
61 
62 #include <dev/usb/ehcireg.h>
63 #include <dev/usb/ehcivar.h>
64 
65 #ifdef EHCI_DEBUG
66 #define DPRINTF(x)	if (ehcidebug) printf x
67 extern int ehcidebug;
68 #else
69 #define DPRINTF(x)
70 #endif
71 
72 #define GCSCUSB_MSR_BASE	0x51200000
73 #define GCSCUSB_MSR_EHCB	(GCSCUSB_MSR_BASE + 0x09)
74 
75 struct gcscehci_softc {
76 	ehci_softc_t		sc;
77 	pci_chipset_tag_t	sc_pc;
78 	pcitag_t		sc_tag;
79 	void 			*sc_ih;		/* interrupt vectoring */
80 };
81 
82 static int
83 gcscehci_match(struct device *parent, struct cfdata *match, void *aux)
84 {
85 	struct pci_attach_args *pa = (struct pci_attach_args *) aux;
86 
87 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_SERIALBUS &&
88 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_SERIALBUS_USB &&
89 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_EHCI &&
90 	    PCI_VENDOR(pa->pa_id) == PCI_VENDOR_AMD &&
91 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_AMD_CS5536_EHCI)
92 		return (10);	/* beat ehci_pci */
93 
94 	return (0);
95 }
96 
97 static void
98 gcscehci_attach(struct device *parent, struct device *self, void *aux)
99 {
100 	struct gcscehci_softc *sc = (struct gcscehci_softc *)self;
101 	struct pci_attach_args *pa = (struct pci_attach_args *)aux;
102 	pci_chipset_tag_t pc = pa->pa_pc;
103 	pcitag_t tag = pa->pa_tag;
104 	char const *intrstr;
105 	pci_intr_handle_t ih;
106 	const char *vendor;
107 	const char *devname = sc->sc.sc_bus.bdev.dv_xname;
108 	char devinfo[256];
109 	usbd_status r;
110 	bus_addr_t ehcibase;
111 	int ncomp;
112 	struct usb_pci *up;
113 
114 	aprint_naive(": USB controller\n");
115 
116 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
117 	aprint_normal(": %s (rev. 0x%02x)\n", devinfo,
118 	    PCI_REVISION(pa->pa_class));
119 
120 	/* Map I/O registers */
121 	ehcibase = rdmsr(GCSCUSB_MSR_EHCB) & 0xffffff00;
122 	sc->sc.iot = pa->pa_memt;
123 	sc->sc.sc_size = 256;
124 	if (bus_space_map(sc->sc.iot, ehcibase, 256, 0, &sc->sc.ioh)) {
125 		aprint_error("%s: can't map memory space\n", devname);
126 		return;
127 	}
128 
129 	sc->sc_pc = pc;
130 	sc->sc_tag = tag;
131 	sc->sc.sc_bus.dmatag = pa->pa_dmat;
132 
133 	/* Disable interrupts, so we don't get any spurious ones. */
134 	sc->sc.sc_offs = EREAD1(&sc->sc, EHCI_CAPLENGTH);
135 	DPRINTF(("%s: offs=%d\n", devname, sc->sc.sc_offs));
136 	EOWRITE2(&sc->sc, EHCI_USBINTR, 0);
137 
138 	/* Map and establish the interrupt. */
139 	if (pci_intr_map(pa, &ih)) {
140 		aprint_error("%s: couldn't map interrupt\n", devname);
141 		return;
142 	}
143 	intrstr = pci_intr_string(pc, ih);
144 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_USB, ehci_intr, sc);
145 	if (sc->sc_ih == NULL) {
146 		aprint_error("%s: couldn't establish interrupt", devname);
147 		if (intrstr != NULL)
148 			aprint_normal(" at %s", intrstr);
149 		aprint_normal("\n");
150 		return;
151 	}
152 	aprint_normal("%s: interrupting at %s\n", devname, intrstr);
153 
154 	sc->sc.sc_bus.usbrev = USBREV_2_0;
155 
156 	/* Figure out vendor for root hub descriptor. */
157 	vendor = pci_findvendor(pa->pa_id);
158 	sc->sc.sc_id_vendor = PCI_VENDOR(pa->pa_id);
159 	if (vendor)
160 		strlcpy(sc->sc.sc_vendor, vendor, sizeof(sc->sc.sc_vendor));
161 	else
162 		snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
163 		    "vendor 0x%04x", PCI_VENDOR(pa->pa_id));
164 
165 	/*
166 	 * Find companion controllers.  According to the spec they always
167 	 * have lower function numbers so they should be enumerated already.
168 	 */
169 	ncomp = 0;
170 	TAILQ_FOREACH(up, &ehci_pci_alldevs, next) {
171 		if (up->bus == pa->pa_bus && up->device == pa->pa_device) {
172 			DPRINTF(("gcscehci_attach: companion %s\n",
173 				 USBDEVNAME(up->usb->bdev)));
174 			sc->sc.sc_comps[ncomp++] = up->usb;
175 			if (ncomp >= EHCI_COMPANION_MAX)
176 				break;
177 		}
178 	}
179 	sc->sc.sc_ncomp = ncomp;
180 
181 	r = ehci_init(&sc->sc);
182 	if (r != USBD_NORMAL_COMPLETION) {
183 		aprint_error("%s: init failed, error=%d\n", devname, r);
184 		return;
185 	}
186 
187 	/* Attach usb device. */
188 	sc->sc.sc_child = config_found((void *)sc, &sc->sc.sc_bus,
189 				       usbctlprint);
190 }
191 
192 CFATTACH_DECL(gcscehci, sizeof(struct gcscehci_softc),
193     gcscehci_match, gcscehci_attach, NULL, ehci_activate);
194