xref: /freebsd-11-stable/sys/dev/usb/controller/ehci_ixp4xx.c (revision 4ab2e064d7950be84256d671a7ae93f87cc6aa36)
1 /*-
2  * Copyright (c) 2008 Sam Leffler.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 /*
26  * IXP435 attachment driver for the USB Enhanced Host Controller.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include "opt_bus.h"
33 
34 #include <sys/stdint.h>
35 #include <sys/stddef.h>
36 #include <sys/param.h>
37 #include <sys/queue.h>
38 #include <sys/types.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bus.h>
42 #include <sys/module.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/condvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/sx.h>
48 #include <sys/unistd.h>
49 #include <sys/callout.h>
50 #include <sys/malloc.h>
51 #include <sys/priv.h>
52 
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.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/controller/ehci.h>
64 #include <dev/usb/controller/ehcireg.h>
65 
66 #include <arm/xscale/ixp425/ixp425reg.h>
67 #include <arm/xscale/ixp425/ixp425var.h>
68 
69 #define EHCI_VENDORID_IXP4XX	0x42fa05
70 #define EHCI_HC_DEVSTR		"IXP4XX Integrated USB 2.0 controller"
71 
72 struct ixp_ehci_softc {
73 	ehci_softc_t		base;	/* storage for EHCI code */
74 	bus_space_tag_t		iot;
75 	bus_space_handle_t	ioh;
76 	struct bus_space	tag;	/* tag for private bus space ops */
77 };
78 
79 static device_attach_t ehci_ixp_attach;
80 static device_detach_t ehci_ixp_detach;
81 
82 static uint8_t ehci_bs_r_1(bus_space_tag_t tag, bus_space_handle_t, bus_size_t);
83 static void ehci_bs_w_1(bus_space_tag_t tag, bus_space_handle_t, bus_size_t, u_int8_t);
84 static uint16_t ehci_bs_r_2(bus_space_tag_t tag, bus_space_handle_t, bus_size_t);
85 static void ehci_bs_w_2(bus_space_tag_t tag, bus_space_handle_t, bus_size_t, uint16_t);
86 static uint32_t ehci_bs_r_4(bus_space_tag_t tag, bus_space_handle_t, bus_size_t);
87 static void ehci_bs_w_4(bus_space_tag_t tag, bus_space_handle_t, bus_size_t, uint32_t);
88 
89 static void
ehci_ixp_post_reset(struct ehci_softc * ehci_softc)90 ehci_ixp_post_reset(struct ehci_softc *ehci_softc)
91 {
92 	uint32_t usbmode;
93 
94 	/* Force HOST mode, select big-endian mode */
95 	usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
96 	usbmode &= ~EHCI_UM_CM;
97 	usbmode |= EHCI_UM_CM_HOST;
98 	usbmode |= EHCI_UM_ES_BE;
99 	EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
100 }
101 
102 static int
ehci_ixp_probe(device_t self)103 ehci_ixp_probe(device_t self)
104 {
105 
106 	device_set_desc(self, EHCI_HC_DEVSTR);
107 
108 	return (BUS_PROBE_DEFAULT);
109 }
110 
111 static int
ehci_ixp_attach(device_t self)112 ehci_ixp_attach(device_t self)
113 {
114 	struct ixp_ehci_softc *isc = device_get_softc(self);
115 	ehci_softc_t *sc = &isc->base;
116 	int err;
117 	int rid;
118 
119 	/* initialise some bus fields */
120 	sc->sc_bus.parent = self;
121 	sc->sc_bus.devices = sc->sc_devices;
122 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
123 	sc->sc_bus.dma_bits = 32;
124 
125 	/* get all DMA memory */
126 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
127 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
128 		return (ENOMEM);
129 	}
130 
131 	/* NB: hints fix the memory location and irq */
132 
133 	rid = 0;
134 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid, RF_ACTIVE);
135 	if (!sc->sc_io_res) {
136 		device_printf(self, "Could not map memory\n");
137 		goto error;
138 	}
139 
140 	/*
141 	 * Craft special resource for bus space ops that handle
142 	 * byte-alignment of non-word addresses.  Also, since
143 	 * we're already intercepting bus space ops we handle
144 	 * the register window offset that could otherwise be
145 	 * done with bus_space_subregion.
146 	 */
147 	isc->iot = rman_get_bustag(sc->sc_io_res);
148 	isc->tag.bs_privdata = isc->iot;
149 	/* read single */
150 	isc->tag.bs_r_1	= ehci_bs_r_1;
151 	isc->tag.bs_r_2	= ehci_bs_r_2;
152 	isc->tag.bs_r_4	= ehci_bs_r_4;
153 	/* write (single) */
154 	isc->tag.bs_w_1	= ehci_bs_w_1;
155 	isc->tag.bs_w_2	= ehci_bs_w_2;
156 	isc->tag.bs_w_4	= ehci_bs_w_4;
157 
158 	sc->sc_io_tag = &isc->tag;
159 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
160 	sc->sc_io_size = IXP435_USB1_SIZE - 0x100;
161 
162 	rid = 0;
163 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
164 	    RF_ACTIVE);
165 	if (sc->sc_irq_res == NULL) {
166 		device_printf(self, "Could not allocate irq\n");
167 		goto error;
168 	}
169 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
170 	if (!sc->sc_bus.bdev) {
171 		device_printf(self, "Could not add USB device\n");
172 		goto error;
173 	}
174 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
175 	device_set_desc(sc->sc_bus.bdev, EHCI_HC_DEVSTR);
176 
177 	sprintf(sc->sc_vendor, "Intel");
178 
179 
180 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
181 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
182 	if (err) {
183 		device_printf(self, "Could not setup irq, %d\n", err);
184 		sc->sc_intr_hdl = NULL;
185 		goto error;
186 	}
187 
188 	/*
189 	 * Select big-endian byte alignment and arrange to not terminate
190 	 * reset operations (the adapter will ignore it if we do but might
191 	 * as well save a reg write). Also, the controller has an embedded
192 	 * Transaction Translator which means port speed must be read from
193 	 * the Port Status register following a port enable.
194 	 */
195 	sc->sc_flags |= EHCI_SCFLG_TT
196 		     | EHCI_SCFLG_BIGEDESC
197 		     | EHCI_SCFLG_NORESTERM
198 		     ;
199 
200 	/* Setup callbacks. */
201 	sc->sc_vendor_post_reset = ehci_ixp_post_reset;
202 	sc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc;
203 
204 	err = ehci_init(sc);
205 	if (!err) {
206 		err = device_probe_and_attach(sc->sc_bus.bdev);
207 	}
208 	if (err) {
209 		device_printf(self, "USB init failed err=%d\n", err);
210 		goto error;
211 	}
212 	return (0);
213 
214 error:
215 	ehci_ixp_detach(self);
216 	return (ENXIO);
217 }
218 
219 static int
ehci_ixp_detach(device_t self)220 ehci_ixp_detach(device_t self)
221 {
222 	struct ixp_ehci_softc *isc = device_get_softc(self);
223 	ehci_softc_t *sc = &isc->base;
224 	int err;
225 
226 	/* during module unload there are lots of children leftover */
227 	device_delete_children(self);
228 
229  	if (sc->sc_irq_res && sc->sc_intr_hdl) {
230 		/*
231 		 * only call ehci_detach() after ehci_init()
232 		 */
233 		ehci_detach(sc);
234 
235 		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
236 
237 		if (err)
238 			/* XXX or should we panic? */
239 			device_printf(self, "Could not tear down irq, %d\n",
240 			    err);
241 		sc->sc_intr_hdl = NULL;
242 	}
243 
244  	if (sc->sc_irq_res) {
245 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
246 		sc->sc_irq_res = NULL;
247 	}
248 	if (sc->sc_io_res) {
249 		bus_release_resource(self, SYS_RES_MEMORY, 0,
250 		    sc->sc_io_res);
251 		sc->sc_io_res = NULL;
252 	}
253 	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
254 
255 	return (0);
256 }
257 
258 /*
259  * Bus space accessors for PIO operations.
260  */
261 
262 static uint8_t
ehci_bs_r_1(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o)263 ehci_bs_r_1(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o)
264 {
265 	return bus_space_read_1((bus_space_tag_t)tag->bs_privdata, h,
266 	    0x100 + (o &~ 3) + (3 - (o & 3)));
267 }
268 
269 static void
ehci_bs_w_1(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o,u_int8_t v)270 ehci_bs_w_1(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o, u_int8_t v)
271 {
272 	panic("%s", __func__);
273 }
274 
275 static uint16_t
ehci_bs_r_2(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o)276 ehci_bs_r_2(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o)
277 {
278 	return bus_space_read_2((bus_space_tag_t)tag->bs_privdata, h,
279 	    0x100 + (o &~ 3) + (2 - (o & 3)));
280 }
281 
282 static void
ehci_bs_w_2(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o,uint16_t v)283 ehci_bs_w_2(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o, uint16_t v)
284 {
285 	panic("%s", __func__);
286 }
287 
288 static uint32_t
ehci_bs_r_4(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o)289 ehci_bs_r_4(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o)
290 {
291 	return bus_space_read_4((bus_space_tag_t) tag->bs_privdata, h, 0x100 + o);
292 }
293 
294 static void
ehci_bs_w_4(bus_space_tag_t tag,bus_space_handle_t h,bus_size_t o,uint32_t v)295 ehci_bs_w_4(bus_space_tag_t tag, bus_space_handle_t h, bus_size_t o, uint32_t v)
296 {
297 	bus_space_write_4((bus_space_tag_t) tag->bs_privdata, h, 0x100 + o, v);
298 }
299 
300 static device_method_t ehci_methods[] = {
301 	/* Device interface */
302 	DEVMETHOD(device_probe, ehci_ixp_probe),
303 	DEVMETHOD(device_attach, ehci_ixp_attach),
304 	DEVMETHOD(device_detach, ehci_ixp_detach),
305 	DEVMETHOD(device_suspend, bus_generic_suspend),
306 	DEVMETHOD(device_resume, bus_generic_resume),
307 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
308 
309 	DEVMETHOD_END
310 };
311 
312 static driver_t ehci_driver = {
313 	"ehci",
314 	ehci_methods,
315 	sizeof(struct ixp_ehci_softc),
316 };
317 
318 static devclass_t ehci_devclass;
319 
320 DRIVER_MODULE(ehci, ixp, ehci_driver, ehci_devclass, 0, 0);
321 MODULE_DEPEND(ehci, usb, 1, 1, 1);
322