xref: /netbsd-src/sys/arch/mips/ralink/ralink_ehci.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: ralink_ehci.c,v 1.5 2014/04/30 00:54:47 matt Exp $	*/
2 /*-
3  * Copyright (c) 2011 CradlePoint Technology, Inc.
4  * All rights reserved.
5  *
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY CRADLEPOINT TECHNOLOGY, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /* ralink_ehci.c -- Ralink EHCI USB Driver */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: ralink_ehci.c,v 1.5 2014/04/30 00:54:47 matt Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 
37 #include <dev/usb/usb.h>
38 #include <dev/usb/usbdi.h>
39 #include <dev/usb/usbdivar.h>
40 #include <dev/usb/usb_mem.h>
41 
42 #include <dev/usb/ehcireg.h>
43 #include <dev/usb/ehcivar.h>
44 
45 #include <mips/ralink/ralink_usbhcvar.h>
46 
47 #include <mips/ralink/ralink_var.h>
48 #include <mips/ralink/ralink_reg.h>
49 
50 struct ralink_ehci_softc {
51 	struct ehci_softc	sc_ehci;
52 	void			*sc_ih;
53 };
54 
55 static int  ralink_ehci_match(device_t, cfdata_t, void *);
56 static void ralink_ehci_attach(device_t, device_t, void *);
57 static int  ralink_ehci_detach(device_t, int);
58 
59 CFATTACH_DECL2_NEW(ralink_ehci, sizeof(struct ralink_ehci_softc),
60 	ralink_ehci_match, ralink_ehci_attach, ralink_ehci_detach,
61 	ehci_activate, NULL, ehci_childdet);
62 
63 static TAILQ_HEAD(, ralink_usb_hc) ralink_usb_alldevs =
64 	TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
65 
66 #if 0
67 struct usb_hc_alldevs ralink_usb_alldevs = TAILQ_HEAD_INITIALIZER(ralink_usb_alldevs);
68 #endif
69 
70 /*
71  * ralink_ehci_match
72  */
73 static int
74 ralink_ehci_match(device_t parent, cfdata_t cf, void *aux)
75 {
76 
77 	return 1;
78 }
79 
80 /*
81  * ralink_ehci_attach
82  */
83 static void
84 ralink_ehci_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct ralink_ehci_softc * const sc = device_private(self);
87 	const struct mainbus_attach_args *ma = aux;
88 	struct ralink_usb_hc *ruh;
89 	usbd_status status;
90 	int error;
91 
92 	aprint_naive(": EHCI USB controller\n");
93 	aprint_normal(": EHCI USB controller\n");
94 
95 	sc->sc_ehci.sc_dev = self;
96 	sc->sc_ehci.sc_bus.hci_private = sc;
97 	sc->sc_ehci.iot = ma->ma_memt;
98 	sc->sc_ehci.sc_bus.dmatag = ma->ma_dmat;
99 
100 	/* Map EHCI registers */
101 	if ((error = bus_space_map(sc->sc_ehci.iot, RA_USB_EHCI_BASE,
102 	    RA_USB_BLOCK_SIZE, 0, &sc->sc_ehci.ioh)) != 0) {
103 		aprint_error_dev(self, "can't map EHCI registers, "
104 			"error=%d\n", error);
105 		return;
106 	}
107 
108 	sc->sc_ehci.sc_size = RA_USB_BLOCK_SIZE;
109 	sc->sc_ehci.sc_bus.usbrev = USBREV_2_0;
110 
111 #if defined(RALINK_EHCI_DEBUG)
112 	aprint_normal_dev(self, "sc %p ma %p\n", sc, ma);
113 	aprint_normal_dev(self, "memt %p dmat %p\n", ma->ma_memt, ma->ma_dmat);
114 	aprint_normal_dev(self, "EHCI HCCAPBASE=%#x\n",
115 		EREAD4(&sc->sc_ehci, EHCI_CAPLENGTH));
116 	aprint_normal_dev(self, "EHCI HCSPARAMS=%#x\n",
117 		EREAD4(&sc->sc_ehci, EHCI_HCSPARAMS));
118 	aprint_normal_dev(self, "EHCI HCCPARAMS=%#x\n",
119 		EREAD4(&sc->sc_ehci, EHCI_HCCPARAMS));
120 	aprint_normal_dev(self, "EHCI HCSP_PORTROUTE=%#x\n",
121 		EREAD4(&sc->sc_ehci, EHCI_HCSP_PORTROUTE));
122 #endif
123 
124 	/* Disable EHCI interrupts. */
125 	sc->sc_ehci.sc_offs = EREAD1(&sc->sc_ehci, EHCI_CAPLENGTH);
126 	EOWRITE4(&sc->sc_ehci, EHCI_USBINTR, 0);
127 
128 #if defined(RALINK_EHCI_DEBUG)
129 	aprint_normal_dev(self, "EHCI USBCMD=%#x USBSTS=%#x USBINTR=%#x\n",
130 		EOREAD4(&sc->sc_ehci, EHCI_USBCMD),
131 		EOREAD4(&sc->sc_ehci, EHCI_USBSTS),
132 		EOREAD4(&sc->sc_ehci, EHCI_USBINTR));
133 #endif
134 
135 	/* Establish the MIPS level interrupt */
136 	sc->sc_ih = ra_intr_establish(RA_IRQ_USB, ehci_intr, sc, 1);
137 	if (sc->sc_ih == NULL) {
138 		aprint_error_dev(self, "unable to establish irq %d\n",
139 			RA_IRQ_USB);
140 		goto fail_0;
141 	}
142 
143 	/*
144 	 * Find companion controllers.  According to the spec they always
145 	 * have lower function numbers so they should be enumerated already.
146 	 */
147 	int ncomp = 0;
148 	TAILQ_FOREACH(ruh, &ralink_usb_alldevs, next) {
149 		aprint_normal_dev(self, "companion %s\n",
150 			device_xname(ruh->usb));
151 		sc->sc_ehci.sc_comps[ncomp++] = ruh->usb;
152 		if (ncomp >= EHCI_COMPANION_MAX)
153 			break;
154 	}
155 	sc->sc_ehci.sc_ncomp = ncomp;
156 
157 	/* set vendor for root hub descriptor. */
158 	sc->sc_ehci.sc_id_vendor = 0x1814;	/* XXX */
159 	strlcpy(sc->sc_ehci.sc_vendor, "Ralink", sizeof(sc->sc_ehci.sc_vendor));
160 
161 	/* Initialize EHCI */
162 	status = ehci_init(&sc->sc_ehci);
163 	if (status != USBD_NORMAL_COMPLETION) {
164 		aprint_error_dev(self, "init failed, error=%d\n", status);
165 		goto fail_1;
166 	}
167 
168 	if (!pmf_device_register1(self, ehci_suspend, ehci_resume,
169 	    ehci_shutdown))
170 		aprint_error_dev(self, "couldn't establish power handler\n");
171 
172 	/* Attach usb device. */
173 	sc->sc_ehci.sc_child = config_found(self, &sc->sc_ehci.sc_bus,
174 		usbctlprint);
175 
176 	return;
177 
178  fail_1:
179 	ra_intr_disestablish(sc->sc_ih);
180 	sc->sc_ih = NULL;
181  fail_0:
182 	bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh, sc->sc_ehci.sc_size);
183 	sc->sc_ehci.sc_size = 0;
184 }
185 
186 static int
187 ralink_ehci_detach(device_t self, int flags)
188 {
189 	struct ralink_ehci_softc * const sc = device_private(self);
190 	int rv;
191 
192 	pmf_device_deregister(self);
193 
194 	rv = ehci_detach(&sc->sc_ehci, flags);
195 	if (rv)
196 		return rv;
197 
198 	if (sc->sc_ih != NULL) {
199 		ra_intr_disestablish(sc->sc_ih);
200 		sc->sc_ih = NULL;
201 	}
202 
203 	if (sc->sc_ehci.sc_size != 0) {
204 		bus_space_unmap(sc->sc_ehci.iot, sc->sc_ehci.ioh,
205 			sc->sc_ehci.sc_size);
206 		sc->sc_ehci.sc_size = 0;
207 	}
208 
209 	return 0;
210 }
211 
212 void
213 ralink_usb_hc_add(struct ralink_usb_hc *ruh, device_t usbp)
214 {
215 	TAILQ_INSERT_TAIL(&ralink_usb_alldevs, ruh, next);
216 	ruh->usb = usbp;
217 }
218 
219 void
220 ralink_usb_hc_rem(struct ralink_usb_hc *ruh)
221 {
222 	TAILQ_REMOVE(&ralink_usb_alldevs, ruh, next);
223 }
224