ehci_imx.c revision 331501
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2010-2012 Semihalf
5 * Copyright (c) 2012 The FreeBSD Foundation
6 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
7 * All rights reserved.
8 *
9 * Portions of this software were developed by Oleksandr Rybalko
10 * under sponsorship from the FreeBSD Foundation.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 *    notice, this list of conditions and the following disclaimer in the
19 *    documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: stable/11/sys/dev/usb/controller/ehci_imx.c 331501 2018-03-24 22:39:38Z ian $");
36
37/*
38 * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller.
39 */
40
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/kernel.h>
44#include <sys/module.h>
45#include <sys/bus.h>
46#include <sys/condvar.h>
47#include <sys/rman.h>
48
49#include <dev/ofw/ofw_bus.h>
50#include <dev/ofw/ofw_bus_subr.h>
51
52#include <dev/usb/usb.h>
53#include <dev/usb/usbdi.h>
54#include <dev/usb/usb_busdma.h>
55#include <dev/usb/usb_process.h>
56#include <dev/usb/usb_controller.h>
57#include <dev/usb/usb_bus.h>
58#include <dev/usb/controller/ehci.h>
59#include <dev/usb/controller/ehcireg.h>
60#include "usbdevs.h"
61
62#include <machine/bus.h>
63#include <machine/resource.h>
64
65#include <arm/freescale/imx/imx_ccmvar.h>
66
67#include "opt_platform.h"
68
69/*
70 * Notes on the hardware and related FDT data seen in the wild.
71 *
72 * There are two sets of registers in the USBOH3 implementation; documentation
73 * refers to them as "core" and "non-core" registers.  A set of core register
74 * exists for each OTG or EHCI device.  There is a single set of non-core
75 * registers per USBOH3, and they control aspects of operation not directly
76 * related to the USB specs, such as whether interrupts from each of the core
77 * devices are able to generate a SoC wakeup event.
78 *
79 * In the FreeBSD universe we might be inclined to describe the core and
80 * non-core registers by using a pair of resource address/size values (two
81 * entries in the reg property for each core).  However, we have to work with
82 * existing FDT data (which mostly comes from the linux universe), and the way
83 * they've chosen to represent this is with an entry for a "usbmisc" device
84 * whose reg property describes the non-core registers. The way we handle FDT
85 * data, this means that the resources (memory-mapped register range) for the
86 * non-core registers belongs to a device other than the echi devices.
87 *
88 * Because the main ehci device cannot access registers in a range that's
89 * defined in the fdt data as belonging to another device, we implement a teeny
90 * little "usbmisc" driver which exists only to provide access to the usbmisc
91 * control register for each of the 4 usb controller instances.  That little
92 * driver is implemented here in this file, before the main driver.
93 *
94 * In addition to the single usbmisc device, the existing FDT data defines a
95 * separate device for each of the OTG or EHCI cores within the USBOH3.  Each of
96 * those devices has a set of core registers described by the reg property.
97 *
98 * The core registers for each of the four cores in the USBOH3 are divided into
99 * two parts: a set of imx-specific registers at an offset of 0 from the
100 * beginning of the register range, and the standard USB (EHCI or OTG) registers
101 * at an offset of 0x100 from the beginning of the register range.  The FreeBSD
102 * way of dealing with this might be to map out two ranges in the reg property,
103 * but that's not what the alternate universe has done.  To work with existing
104 * FDT data, we acquire the resource that maps all the core registers, then use
105 * bus_space_subregion() to create another resource that maps just the standard
106 * USB registers, which we provide to the standard USB code in the ehci_softc.
107 *
108 * The following compat strings have been seen for the OTG and EHCI cores.  The
109 * FDT compat table in this driver contains all these strings, but as of this
110 * writing, not all of these SoCs have been tested with the driver.  The fact
111 * that imx27 is common to all of them gives some hope that the driver will work
112 * on all these SoCs.
113 *   - "fsl,imx23-usb", "fsl,imx27-usb";
114 *   - "fsl,imx25-usb", "fsl,imx27-usb";
115 *   - "fsl,imx28-usb", "fsl,imx27-usb";
116 *   - "fsl,imx51-usb", "fsl,imx27-usb";
117 *   - "fsl,imx53-usb", "fsl,imx27-usb";
118 *   - "fsl,imx6q-usb", "fsl,imx27-usb";
119 *
120 * The FDT data for some SoCs contains the following properties, which we don't
121 * currently do anything with:
122 *   - fsl,usbmisc = <&usbmisc 0>;
123 *   - fsl,usbphy = <&usbphy0>;
124 *
125 * Some imx SoCs have FDT data related to USB PHY, some don't.  We have separate
126 * usbphy drivers where needed; this data is mentioned here just to keep all the
127 * imx-FDT-usb-related info in one place.  Here are the usbphy compat strings
128 * known to exist:
129 *   - "nop-usbphy"
130 *   - "usb-nop-xceiv";
131 *   - "fsl,imx23-usbphy"
132 *   - "fsl,imx28-usbphy", "fsl,imx23-usbphy";
133 *   - "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
134 *
135 */
136
137/*-----------------------------------------------------------------------------
138 * imx_usbmisc driver
139 *---------------------------------------------------------------------------*/
140
141#define	USBNC_OVER_CUR_POL	  (1u << 8)
142#define	USBNC_OVER_CUR_DIS	  (1u << 7)
143
144struct imx_usbmisc_softc {
145	device_t	dev;
146	struct resource	*mmio;
147};
148
149static struct ofw_compat_data usbmisc_compat_data[] = {
150	{"fsl,imx6q-usbmisc",	true},
151	{"fsl,imx51-usbmisc",	true},
152	{"fsl,imx25-usbmisc",	true},
153	{NULL, 			false},
154};
155
156static void
157imx_usbmisc_set_ctrl(device_t dev, u_int index, uint32_t bits)
158{
159	struct imx_usbmisc_softc *sc;
160	uint32_t reg;
161
162	sc = device_get_softc(dev);
163	reg = bus_read_4(sc->mmio, index * sizeof(uint32_t));
164	bus_write_4(sc->mmio, index * sizeof(uint32_t), reg | bits);
165}
166
167#ifdef notyet
168static void
169imx_usbmisc_clr_ctrl(device_t dev, u_int index, uint32_t bits)
170{
171	struct imx_usbmisc_softc *sc;
172	uint32_t reg;
173
174	sc = device_get_softc(dev);
175	reg = bus_read_4(sc->mmio, index * sizeof(uint32_t));
176	bus_write_4(sc->mmio, index * sizeof(uint32_t), reg & ~bits);
177}
178#endif
179
180static int
181imx_usbmisc_probe(device_t dev)
182{
183
184	if (!ofw_bus_status_okay(dev))
185		return (ENXIO);
186
187	if (ofw_bus_search_compatible(dev, usbmisc_compat_data)->ocd_data) {
188		device_set_desc(dev, "i.MX USB Misc Control");
189		return (BUS_PROBE_DEFAULT);
190	}
191	return (ENXIO);
192}
193
194static int
195imx_usbmisc_detach(device_t dev)
196{
197	struct imx_usbmisc_softc *sc;
198
199	sc = device_get_softc(dev);
200
201	if (sc->mmio != NULL)
202		bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mmio);
203
204	return (0);
205}
206
207static int
208imx_usbmisc_attach(device_t dev)
209{
210	struct imx_usbmisc_softc *sc;
211	int err, rid;
212
213	sc = device_get_softc(dev);
214	err = 0;
215
216	/* Allocate bus_space resources. */
217	rid = 0;
218	sc->mmio = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
219	    RF_ACTIVE);
220	if (sc->mmio == NULL) {
221		device_printf(dev, "Cannot allocate memory resources\n");
222		return (ENXIO);
223	}
224
225	OF_device_register_xref(OF_xref_from_node(ofw_bus_get_node(dev)), dev);
226
227	return (0);
228}
229
230static device_method_t imx_usbmisc_methods[] = {
231	/* Device interface */
232	DEVMETHOD(device_probe, 	imx_usbmisc_probe),
233	DEVMETHOD(device_attach,	imx_usbmisc_attach),
234	DEVMETHOD(device_detach,	imx_usbmisc_detach),
235
236	DEVMETHOD_END
237};
238
239static driver_t imx_usbmisc_driver = {
240	"imx_usbmisc",
241	imx_usbmisc_methods,
242	sizeof(struct imx_usbmisc_softc)
243};
244
245static devclass_t imx_usbmisc_devclass;
246
247/*
248 * This driver needs to start before the ehci driver, but later than the usual
249 * "special" drivers like clocks and cpu.  Ehci starts at DEFAULT so
250 * DEFAULT-1000 seems good.
251 */
252EARLY_DRIVER_MODULE(imx_usbmisc, simplebus, imx_usbmisc_driver,
253    imx_usbmisc_devclass, 0, 0, BUS_PASS_DEFAULT - 1000);
254
255/*-----------------------------------------------------------------------------
256 * imx_ehci driver...
257 *---------------------------------------------------------------------------*/
258
259/*
260 * Each EHCI device in the SoC has some SoC-specific per-device registers at an
261 * offset of 0, then the standard EHCI registers begin at an offset of 0x100.
262 */
263#define	IMX_EHCI_REG_OFF	0x100
264#define	IMX_EHCI_REG_SIZE	0x100
265
266struct imx_ehci_softc {
267	ehci_softc_t	ehci_softc;
268	device_t	dev;
269	struct resource	*ehci_mem_res;	/* EHCI core regs. */
270	struct resource	*ehci_irq_res;	/* EHCI core IRQ. */
271	bool		usb_mem_allocated;
272};
273
274static struct ofw_compat_data compat_data[] = {
275	{"fsl,imx6q-usb",	1},
276	{"fsl,imx53-usb",	1},
277	{"fsl,imx51-usb",	1},
278	{"fsl,imx28-usb",	1},
279	{"fsl,imx27-usb",	1},
280	{"fsl,imx25-usb",	1},
281	{"fsl,imx23-usb",	1},
282	{NULL,		 	0},
283};
284
285static void
286imx_ehci_post_reset(struct ehci_softc *ehci_softc)
287{
288        uint32_t usbmode;
289
290        /* Force HOST mode */
291        usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
292        usbmode &= ~EHCI_UM_CM;
293        usbmode |= EHCI_UM_CM_HOST;
294        EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
295}
296
297static int
298imx_ehci_probe(device_t dev)
299{
300
301	if (!ofw_bus_status_okay(dev))
302		return (ENXIO);
303
304	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
305		device_set_desc(dev, "Freescale i.MX integrated USB controller");
306		return (BUS_PROBE_DEFAULT);
307	}
308	return (ENXIO);
309}
310
311static int
312imx_ehci_detach(device_t dev)
313{
314	struct imx_ehci_softc *sc;
315	ehci_softc_t *esc;
316
317	sc = device_get_softc(dev);
318
319	esc = &sc->ehci_softc;
320
321	if (esc->sc_bus.bdev != NULL)
322		device_delete_child(dev, esc->sc_bus.bdev);
323	if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
324		ehci_detach(esc);
325	if (esc->sc_intr_hdl != NULL)
326		bus_teardown_intr(dev, esc->sc_irq_res,
327		    esc->sc_intr_hdl);
328	if (sc->ehci_irq_res != NULL)
329		bus_release_resource(dev, SYS_RES_IRQ, 0,
330		    sc->ehci_irq_res);
331	if (sc->ehci_mem_res != NULL)
332		bus_release_resource(dev, SYS_RES_MEMORY, 0,
333		    sc->ehci_mem_res);
334
335	if (sc->usb_mem_allocated)
336		usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
337
338	/* During module unload there are lots of children leftover */
339	device_delete_children(dev);
340
341	return (0);
342}
343
344static void
345imx_ehci_disable_oc(struct imx_ehci_softc *sc)
346{
347	device_t usbmdev;
348	pcell_t usbmprops[2];
349	phandle_t node;
350	ssize_t size;
351	int index;
352
353	/* Get the reference to the usbmisc driver from the fdt data */
354	node = ofw_bus_get_node(sc->dev);
355	size = OF_getencprop(node, "fsl,usbmisc", usbmprops,
356	    sizeof(usbmprops));
357	if (size < sizeof(usbmprops)) {
358		device_printf(sc->dev, "failed to retrieve fsl,usbmisc "
359		   "property, cannot disable overcurrent protection");
360		return;
361	}
362	/* Retrieve the device_t via the xref handle. */
363	usbmdev = OF_device_from_xref(usbmprops[0]);
364	if (usbmdev == NULL) {
365		device_printf(sc->dev, "usbmisc device not found, "
366		    "cannot disable overcurrent protection");
367		return;
368	}
369	/* Call the device routine to set the overcurrent disable bit. */
370	index = usbmprops[1];
371	imx_usbmisc_set_ctrl(usbmdev, index, USBNC_OVER_CUR_DIS);
372}
373
374static int
375imx_ehci_attach(device_t dev)
376{
377	struct imx_ehci_softc *sc;
378	ehci_softc_t *esc;
379	int err, rid;
380
381	sc = device_get_softc(dev);
382	sc->dev = dev;
383	esc = &sc->ehci_softc;
384	err = 0;
385
386	/* Allocate bus_space resources. */
387	rid = 0;
388	sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
389	    RF_ACTIVE);
390	if (sc->ehci_mem_res == NULL) {
391		device_printf(dev, "Cannot allocate memory resources\n");
392		err = ENXIO;
393		goto out;
394	}
395
396	rid = 0;
397	sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
398	    RF_ACTIVE);
399	if (sc->ehci_irq_res == NULL) {
400		device_printf(dev, "Cannot allocate IRQ resources\n");
401		err = ENXIO;
402		goto out;
403	}
404
405	esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
406	esc->sc_bus.parent = dev;
407	esc->sc_bus.devices = esc->sc_devices;
408	esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
409	esc->sc_bus.dma_bits = 32;
410
411	/* allocate all DMA memory */
412	if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
413	    &ehci_iterate_hw_softc) != 0) {
414		device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
415		err = ENOMEM;
416		goto out;
417	}
418	sc->usb_mem_allocated = true;
419
420	/*
421	 * Set handle to USB related registers subregion used by
422	 * generic EHCI driver.
423	 */
424	err = bus_space_subregion(esc->sc_io_tag,
425	    rman_get_bushandle(sc->ehci_mem_res),
426	    IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl);
427	if (err != 0) {
428		device_printf(dev, "bus_space_subregion() failed\n");
429		err = ENXIO;
430		goto out;
431	}
432
433	/* Setup interrupt handler. */
434	err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
435	    NULL, (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
436	if (err != 0) {
437		device_printf(dev, "Could not setup IRQ\n");
438		goto out;
439	}
440
441	/* Turn on clocks. */
442	imx_ccm_usb_enable(dev);
443
444	/* Disable overcurrent detection, if configured to do so. */
445	if (OF_hasprop(ofw_bus_get_node(sc->dev), "disable-over-current"))
446		imx_ehci_disable_oc(sc);
447
448	/* Add USB bus device. */
449	esc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
450	if (esc->sc_bus.bdev == NULL) {
451		device_printf(dev, "Could not add USB device\n");
452		goto out;
453	}
454	device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
455
456	esc->sc_id_vendor = USB_VENDOR_FREESCALE;
457	strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor));
458
459	/*
460	 * Set flags that affect ehci_init() behavior, and hook our post-reset
461	 * code into the standard controller code.
462	 */
463	esc->sc_flags |= EHCI_SCFLG_NORESTERM | EHCI_SCFLG_TT;
464	esc->sc_vendor_post_reset = imx_ehci_post_reset;
465	esc->sc_vendor_get_port_speed = ehci_get_port_speed_portsc;
466
467	err = ehci_init(esc);
468	if (err != 0) {
469		device_printf(dev, "USB init failed, usb_err_t=%d\n",
470		    err);
471		goto out;
472	}
473	esc->sc_flags |= EHCI_SCFLG_DONEINIT;
474
475	/* Probe the bus. */
476	err = device_probe_and_attach(esc->sc_bus.bdev);
477	if (err != 0) {
478		device_printf(dev,
479		    "device_probe_and_attach() failed\n");
480		goto out;
481	}
482
483	err = 0;
484
485out:
486
487	if (err != 0)
488		imx_ehci_detach(dev);
489
490	return (err);
491}
492
493static device_method_t ehci_methods[] = {
494	/* Device interface */
495	DEVMETHOD(device_probe, imx_ehci_probe),
496	DEVMETHOD(device_attach, imx_ehci_attach),
497	DEVMETHOD(device_detach, imx_ehci_detach),
498	DEVMETHOD(device_suspend, bus_generic_suspend),
499	DEVMETHOD(device_resume, bus_generic_resume),
500	DEVMETHOD(device_shutdown, bus_generic_shutdown),
501
502	/* Bus interface */
503	DEVMETHOD(bus_print_child, bus_generic_print_child),
504
505	DEVMETHOD_END
506};
507
508static driver_t ehci_driver = {
509	"ehci",
510	ehci_methods,
511	sizeof(struct imx_ehci_softc)
512};
513
514static devclass_t ehci_devclass;
515
516DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
517MODULE_DEPEND(ehci, usb, 1, 1, 1);
518