xref: /freebsd/sys/dev/usb/controller/ehci_fsl.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
1 /*-
2  * Copyright (c) 2010-2012 Semihalf
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_bus.h"
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/queue.h>
38 #include <sys/lock.h>
39 #include <sys/lockmgr.h>
40 #include <sys/condvar.h>
41 #include <sys/rman.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usb_core.h>
49 #include <dev/usb/usb_busdma.h>
50 #include <dev/usb/usb_process.h>
51 #include <dev/usb/usb_util.h>
52 #include <dev/usb/usb_controller.h>
53 #include <dev/usb/usb_bus.h>
54 #include <dev/usb/controller/ehci.h>
55 #include <dev/usb/controller/ehcireg.h>
56 
57 #include <machine/bus.h>
58 #include <machine/clock.h>
59 #include <machine/resource.h>
60 
61 #include <powerpc/include/tlb.h>
62 
63 #include "opt_platform.h"
64 
65 /*
66  * Register the driver
67  */
68 /* Forward declarations */
69 static int	fsl_ehci_attach(device_t self);
70 static int	fsl_ehci_detach(device_t self);
71 static int	fsl_ehci_probe(device_t self);
72 
73 static device_method_t ehci_methods[] = {
74 	/* Device interface */
75 	DEVMETHOD(device_probe, fsl_ehci_probe),
76 	DEVMETHOD(device_attach, fsl_ehci_attach),
77 	DEVMETHOD(device_detach, fsl_ehci_detach),
78 	DEVMETHOD(device_suspend, bus_generic_suspend),
79 	DEVMETHOD(device_resume, bus_generic_resume),
80 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
81 
82 	/* Bus interface */
83 	DEVMETHOD(bus_print_child, bus_generic_print_child),
84 
85 	{ 0, 0 }
86 };
87 
88 /* kobj_class definition */
89 static driver_t ehci_driver = {
90 	"ehci",
91 	ehci_methods,
92 	sizeof(struct ehci_softc)
93 };
94 
95 static devclass_t ehci_devclass;
96 
97 DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
98 MODULE_DEPEND(ehci, usb, 1, 1, 1);
99 
100 /*
101  * Private defines
102  */
103 #define FSL_EHCI_REG_OFF	0x100
104 #define FSL_EHCI_REG_SIZE	0x300
105 
106 /*
107  * Internal interface registers' offsets.
108  * Offsets from 0x000 ehci dev space, big-endian access.
109  */
110 enum internal_reg {
111 	SNOOP1		= 0x400,
112 	SNOOP2		= 0x404,
113 	AGE_CNT_THRESH	= 0x408,
114 	SI_CTRL		= 0x410,
115 	CONTROL		= 0x500
116 };
117 
118 /* CONTROL register bit flags */
119 enum control_flags {
120 	USB_EN		= 0x00000004,
121 	UTMI_PHY_EN	= 0x00000200,
122 	ULPI_INT_EN	= 0x00000001
123 };
124 
125 /* SI_CTRL register bit flags */
126 enum si_ctrl_flags {
127 	FETCH_32	= 1,
128 	FETCH_64	= 0
129 };
130 
131 #define SNOOP_RANGE_2GB	0x1E
132 
133 /*
134  * Operational registers' offsets.
135  * Offsets from USBCMD register, little-endian access.
136  */
137 enum special_op_reg {
138 	USBMODE		= 0x0A8,
139 	PORTSC		= 0x084,
140 	ULPI_VIEWPORT	= 0x70
141 };
142 
143 /* USBMODE register bit flags */
144 enum usbmode_flags {
145 	HOST_MODE	= 0x3,
146 	DEVICE_MODE	= 0x2
147 };
148 
149 #define	PORT_POWER_MASK	0x00001000
150 
151 /*
152  * Private methods
153  */
154 
155 static void
156 set_to_host_mode(ehci_softc_t *sc)
157 {
158 	int tmp;
159 
160 	tmp = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, USBMODE);
161 	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, USBMODE, tmp | HOST_MODE);
162 }
163 
164 static void
165 enable_usb(device_t dev, bus_space_tag_t iot, bus_space_handle_t ioh)
166 {
167 	int tmp;
168 	phandle_t node;
169 	char *phy_type;
170 
171 	phy_type = NULL;
172 	tmp = bus_space_read_4(iot, ioh, CONTROL) | USB_EN;
173 
174 	node = ofw_bus_get_node(dev);
175 	if ((node != 0) &&
176 	    (OF_getprop_alloc(node, "phy_type", 1, (void **)&phy_type) > 0)) {
177 		if (strncasecmp(phy_type, "utmi", strlen("utmi")) == 0)
178 			tmp |= UTMI_PHY_EN;
179 		free(phy_type, M_OFWPROP);
180 	}
181 	bus_space_write_4(iot, ioh, CONTROL, tmp);
182 }
183 
184 static void
185 set_32b_prefetch(bus_space_tag_t iot, bus_space_handle_t ioh)
186 {
187 
188 	bus_space_write_4(iot, ioh, SI_CTRL, FETCH_32);
189 }
190 
191 static void
192 set_snooping(bus_space_tag_t iot, bus_space_handle_t ioh)
193 {
194 
195 	bus_space_write_4(iot, ioh, SNOOP1, SNOOP_RANGE_2GB);
196 	bus_space_write_4(iot, ioh, SNOOP2, 0x80000000 | SNOOP_RANGE_2GB);
197 }
198 
199 static void
200 clear_port_power(ehci_softc_t *sc)
201 {
202 	int tmp;
203 
204 	tmp = bus_space_read_4(sc->sc_io_tag, sc->sc_io_hdl, PORTSC);
205 	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, PORTSC, tmp & ~PORT_POWER_MASK);
206 }
207 
208 /*
209  * Public methods
210  */
211 static int
212 fsl_ehci_probe(device_t dev)
213 {
214 
215 	if (!ofw_bus_status_okay(dev))
216 		return (ENXIO);
217 
218 	if (((ofw_bus_is_compatible(dev, "fsl-usb2-dr")) == 0) &&
219 	    ((ofw_bus_is_compatible(dev, "fsl-usb2-mph")) == 0))
220 		return (ENXIO);
221 
222 	device_set_desc(dev, "Freescale integrated EHCI controller");
223 
224 	return (BUS_PROBE_DEFAULT);
225 }
226 
227 static int
228 fsl_ehci_attach(device_t self)
229 {
230 	ehci_softc_t *sc;
231 	int rid;
232 	int err;
233 	bus_space_handle_t ioh;
234 	bus_space_tag_t iot;
235 
236 	sc = device_get_softc(self);
237 	rid = 0;
238 
239 	sc->sc_bus.parent = self;
240 	sc->sc_bus.devices = sc->sc_devices;
241 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
242 	sc->sc_bus.dma_bits = 32;
243 
244 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
245 	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc))
246 		return (ENOMEM);
247 
248 	/* Allocate io resource for EHCI */
249 	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
250 	    RF_ACTIVE);
251 	if (sc->sc_io_res == NULL) {
252 		err = fsl_ehci_detach(self);
253 		if (err) {
254 			device_printf(self,
255 			    "Detach of the driver failed with error %d\n",
256 			    err);
257 		}
258 		return (ENXIO);
259 	}
260 	iot = rman_get_bustag(sc->sc_io_res);
261 
262 	/*
263 	 * Set handle to USB related registers subregion used by generic
264 	 * EHCI driver
265 	 */
266 	ioh = rman_get_bushandle(sc->sc_io_res);
267 
268 	err = bus_space_subregion(iot, ioh, FSL_EHCI_REG_OFF, FSL_EHCI_REG_SIZE,
269 	    &sc->sc_io_hdl);
270 	if (err != 0) {
271 		err = fsl_ehci_detach(self);
272 		if (err) {
273 			device_printf(self,
274 			    "Detach of the driver failed with error %d\n",
275 			    err);
276 		}
277 		return (ENXIO);
278 	}
279 
280 	/* Set little-endian tag for use by the generic EHCI driver */
281 	sc->sc_io_tag = &bs_le_tag;
282 
283 	/* Allocate irq */
284 	sc->sc_irq_res = bus_alloc_resource_any(self, SYS_RES_IRQ, &rid,
285 	    RF_ACTIVE);
286 	if (sc->sc_irq_res == NULL) {
287 		err = fsl_ehci_detach(self);
288 		if (err) {
289 			device_printf(self,
290 			    "Detach of the driver failed with error %d\n",
291 			    err);
292 		}
293 		return (ENXIO);
294 	}
295 
296 	/* Setup interrupt handler */
297 	err = bus_setup_intr(self, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
298 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
299 	if (err) {
300 		device_printf(self, "Could not setup irq, %d\n", err);
301 		sc->sc_intr_hdl = NULL;
302 		err = fsl_ehci_detach(self);
303 		if (err) {
304 			device_printf(self,
305 			    "Detach of the driver failed with error %d\n",
306 			    err);
307 		}
308 		return (ENXIO);
309 	}
310 
311 	/* Add USB device */
312 	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
313 	if (!sc->sc_bus.bdev) {
314 		device_printf(self, "Could not add USB device\n");
315 		err = fsl_ehci_detach(self);
316 		if (err) {
317 			device_printf(self,
318 			    "Detach of the driver failed with error %d\n",
319 			    err);
320 		}
321 		return (ENOMEM);
322 	}
323 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
324 
325 	sc->sc_id_vendor = 0x1234;
326 	strlcpy(sc->sc_vendor, "Freescale", sizeof(sc->sc_vendor));
327 
328 	/* Enable USB */
329 	err = ehci_reset(sc);
330 	if (err) {
331 		device_printf(self, "Could not reset the controller\n");
332 		err = fsl_ehci_detach(self);
333 		if (err) {
334 			device_printf(self,
335 			    "Detach of the driver failed with error %d\n",
336 			    err);
337 		}
338 		return (ENXIO);
339 	}
340 
341 	enable_usb(self, iot, ioh);
342 	set_snooping(iot, ioh);
343 	set_to_host_mode(sc);
344 	set_32b_prefetch(iot, ioh);
345 
346 	/*
347 	 * If usb subsystem is enabled in U-Boot, port power has to be turned
348 	 * off to allow proper discovery of devices during boot up.
349 	 */
350 	clear_port_power(sc);
351 
352 	/* Set flags */
353 	sc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
354 
355 	err = ehci_init(sc);
356 	if (!err) {
357 		sc->sc_flags |= EHCI_SCFLG_DONEINIT;
358 		err = device_probe_and_attach(sc->sc_bus.bdev);
359 	}
360 
361 	if (err) {
362 		device_printf(self, "USB init failed err=%d\n", err);
363 		err = fsl_ehci_detach(self);
364 		if (err) {
365 			device_printf(self,
366 			    "Detach of the driver failed with error %d\n",
367 			    err);
368 		}
369 		return (EIO);
370 	}
371 
372 	return (0);
373 }
374 
375 static int
376 fsl_ehci_detach(device_t self)
377 {
378 
379 	int err;
380 	ehci_softc_t *sc;
381 
382 	sc = device_get_softc(self);
383 	/*
384 	 * only call ehci_detach() after ehci_init()
385 	 */
386 	if (sc->sc_flags & EHCI_SCFLG_DONEINIT) {
387 		ehci_detach(sc);
388 		sc->sc_flags &= ~EHCI_SCFLG_DONEINIT;
389 	}
390 
391 	/* Disable interrupts that might have been switched on in ehci_init */
392 	if (sc->sc_io_tag && sc->sc_io_hdl)
393 		bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl, EHCI_USBINTR, 0);
394 
395 	if (sc->sc_irq_res && sc->sc_intr_hdl) {
396 		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
397 		if (err) {
398 			device_printf(self, "Could not tear down irq, %d\n",
399 			    err);
400 			return (err);
401 		}
402 		sc->sc_intr_hdl = NULL;
403 	}
404 
405 	if (sc->sc_bus.bdev) {
406 		device_delete_child(self, sc->sc_bus.bdev);
407 		sc->sc_bus.bdev = NULL;
408 	}
409 
410 	/* During module unload there are lots of children leftover */
411 	device_delete_children(self);
412 
413 	if (sc->sc_irq_res) {
414 		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
415 		sc->sc_irq_res = NULL;
416 	}
417 
418 	if (sc->sc_io_res) {
419 		bus_release_resource(self, SYS_RES_MEMORY, 0, sc->sc_io_res);
420 		sc->sc_io_res = NULL;
421 		sc->sc_io_tag = 0;
422 		sc->sc_io_hdl = 0;
423 	}
424 
425 	return (0);
426 }
427 
428