ehci_imx.c revision 259345
1/*-
2 * Copyright (c) 2010-2012 Semihalf
3 * Copyright (c) 2012 The FreeBSD Foundation
4 * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
5 * All rights reserved.
6 *
7 * Portions of this software were developed by Oleksandr Rybalko
8 * under sponsorship from the FreeBSD Foundation.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: stable/10/sys/dev/usb/controller/ehci_imx.c 259345 2013-12-13 22:19:21Z ian $");
34
35/*
36 * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller.
37 */
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/module.h>
43#include <sys/bus.h>
44#include <sys/condvar.h>
45#include <sys/rman.h>
46
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.h>
49
50#include <dev/usb/usb.h>
51#include <dev/usb/usbdi.h>
52#include <dev/usb/usb_busdma.h>
53#include <dev/usb/usb_process.h>
54#include <dev/usb/usb_controller.h>
55#include <dev/usb/usb_bus.h>
56#include <dev/usb/controller/ehci.h>
57#include <dev/usb/controller/ehcireg.h>
58#include "usbdevs.h"
59
60#include <machine/bus.h>
61#include <machine/resource.h>
62
63#include <arm/freescale/imx/imx_machdep.h>
64
65#include "opt_platform.h"
66
67/*
68 * Notes on the hardware and related FDT data seen in the wild.
69 *
70 * There are two sets of registers in the USBOH3 implementation; documentation
71 * refers to them as "core" and "non-core" registers.  A set of core register
72 * exists for each OTG or EHCI device.  There is a single set of non-core
73 * registers per USBOH3, and they control aspects of operation not directly
74 * related to the USB specs, such as whether interrupts from each of the core
75 * devices are able to generate a SoC wakeup event.
76 *
77 * In the FreeBSD universe we might be inclined to describe the core and
78 * non-core registers by using a pair of resource address/size values (two
79 * entries in the reg property for each core).  However, we have to work with
80 * existing FDT data (which mostly comes from the linux universe), and the way
81 * they've chosen to represent this is with an entry for a "usbmisc" device
82 * whose reg property describes the non-core registers. The way we handle FDT
83 * data, this means that the resources (memory-mapped register range) for the
84 * non-core registers belongs to a device other than the echi devices.
85 *
86 * At the moment we have no need to access the non-core registers, so all of
87 * this amounts to documenting what's known.  The following compat strings have
88 * been seen in existing FDT data:
89 *   - "fsl,imx25-usbmisc"
90 *   - "fsl,imx51-usbmisc";
91 *   - "fsl,imx6q-usbmisc";
92 *
93 * In addition to the single usbmisc device, the existing FDT data defines a
94 * separate device for each of the OTG or EHCI cores within the USBOH3.  Each of
95 * those devices has a set of core registers described by the reg property.
96 *
97 * The core registers for each of the four cores in the USBOH3 are divided into
98 * two parts: a set of imx-specific registers at an offset of 0 from the
99 * beginning of the register range, and the standard USB (EHCI or OTG) registers
100 * at an offset of 0x100 from the beginning of the register range.  The FreeBSD
101 * way of dealing with this might be to map out two ranges in the reg property,
102 * but that's not what the alternate universe has done.  To work with existing
103 * FDT data, we acquire the resource that maps all the core registers, then use
104 * bus_space_subregion() to create another resource that maps just the standard
105 * USB registers, which we provide to the standard USB code in the ehci_softc.
106 *
107 * The following compat strings have been seen for the OTG and EHCI cores.  The
108 * FDT compat table in this driver contains all these strings, but as of this
109 * writing, not all of these SoCs have been tested with the driver.  The fact
110 * that imx27 is common to all of them gives some hope that the driver will work
111 * on all these SoCs.
112 *   - "fsl,imx23-usb", "fsl,imx27-usb";
113 *   - "fsl,imx25-usb", "fsl,imx27-usb";
114 *   - "fsl,imx28-usb", "fsl,imx27-usb";
115 *   - "fsl,imx51-usb", "fsl,imx27-usb";
116 *   - "fsl,imx53-usb", "fsl,imx27-usb";
117 *   - "fsl,imx6q-usb", "fsl,imx27-usb";
118 *
119 * The FDT data for some SoCs contains the following properties, which we don't
120 * currently do anything with:
121 *   - fsl,usbmisc = <&usbmisc 0>;
122 *   - fsl,usbphy = <&usbphy0>;
123 *
124 * Some imx SoCs have FDT data related to USB PHY, some don't.  We have separate
125 * usbphy drivers where needed; this data is mentioned here just to keep all the
126 * imx-FDT-usb-related info in one place.  Here are the usbphy compat strings
127 * known to exist:
128 *   - "nop-usbphy"
129 *   - "usb-nop-xceiv";
130 *   - "fsl,imx23-usbphy"
131 *   - "fsl,imx28-usbphy", "fsl,imx23-usbphy";
132 *   - "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
133 *
134 */
135
136static struct ofw_compat_data compat_data[] = {
137	{"fsl,imx6q-usb",	1},
138	{"fsl,imx53-usb",	1},
139	{"fsl,imx51-usb",	1},
140	{"fsl,imx28-usb",	1},
141	{"fsl,imx27-usb",	1},
142	{"fsl,imx25-usb",	1},
143	{"fsl,imx23-usb",	1},
144	{NULL,		 	0},
145};
146
147/*
148 * Each EHCI device in the SoC has some SoC-specific per-device registers at an
149 * offset of 0, then the standard EHCI registers begin at an offset of 0x100.
150 */
151#define	IMX_EHCI_REG_OFF	0x100
152#define	IMX_EHCI_REG_SIZE	0x100
153
154struct imx_ehci_softc {
155	ehci_softc_t	ehci_softc;
156	struct resource	*ehci_mem_res;	/* EHCI core regs. */
157	struct resource	*ehci_irq_res;	/* EHCI core IRQ. */
158};
159
160static int
161imx_ehci_probe(device_t dev)
162{
163
164	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
165		device_set_desc(dev, "Freescale i.MX integrated USB controller");
166		return (BUS_PROBE_DEFAULT);
167	}
168	return (ENXIO);
169}
170
171static int
172imx_ehci_detach(device_t dev)
173{
174	struct imx_ehci_softc *sc;
175	ehci_softc_t *esc;
176
177	sc = device_get_softc(dev);
178
179	esc = &sc->ehci_softc;
180
181	if (esc->sc_bus.bdev != NULL)
182		device_delete_child(dev, esc->sc_bus.bdev);
183	if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
184		ehci_detach(esc);
185	if (esc->sc_intr_hdl != NULL)
186		bus_teardown_intr(dev, esc->sc_irq_res,
187		    esc->sc_intr_hdl);
188	if (sc->ehci_irq_res != NULL)
189		bus_release_resource(dev, SYS_RES_IRQ, 0,
190		    sc->ehci_irq_res);
191	if (sc->ehci_mem_res != NULL)
192		bus_release_resource(dev, SYS_RES_MEMORY, 0,
193		    sc->ehci_mem_res);
194
195	usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
196
197	/* During module unload there are lots of children leftover */
198	device_delete_children(dev);
199
200	return (0);
201}
202
203static int
204imx_ehci_attach(device_t dev)
205{
206	struct imx_ehci_softc *sc;
207	ehci_softc_t *esc;
208	int err, rid;
209
210	sc = device_get_softc(dev);
211	esc = &sc->ehci_softc;
212	err = 0;
213
214	/* Allocate bus_space resources. */
215	rid = 0;
216	sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
217	    RF_ACTIVE);
218	if (sc->ehci_mem_res == NULL) {
219		device_printf(dev, "Cannot allocate memory resources\n");
220		err = ENXIO;
221		goto out;
222	}
223
224	rid = 0;
225	sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
226	    RF_ACTIVE);
227	if (sc->ehci_irq_res == NULL) {
228		device_printf(dev, "Cannot allocate IRQ resources\n");
229		err = ENXIO;
230		goto out;
231	}
232
233	esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
234	esc->sc_bus.parent = dev;
235	esc->sc_bus.devices = esc->sc_devices;
236	esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
237
238	if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
239	    &ehci_iterate_hw_softc) != 0) {
240		device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
241		err = ENOMEM;
242		goto out;
243	}
244
245	/*
246	 * Set handle to USB related registers subregion used by
247	 * generic EHCI driver.
248	 */
249	err = bus_space_subregion(esc->sc_io_tag,
250	    rman_get_bushandle(sc->ehci_mem_res),
251	    IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl);
252	if (err != 0) {
253		device_printf(dev, "bus_space_subregion() failed\n");
254		err = ENXIO;
255		goto out;
256	}
257
258	/* Setup interrupt handler. */
259	err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO, NULL,
260	    (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
261	if (err != 0) {
262		device_printf(dev, "Could not setup IRQ\n");
263		goto out;
264	}
265
266	/* Turn on clocks. */
267	imx_ccm_usb_enable(dev);
268
269	/* Add USB bus device. */
270	esc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
271	if (esc->sc_bus.bdev == NULL) {
272		device_printf(dev, "Could not add USB device\n");
273		goto out;
274	}
275	device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
276
277	esc->sc_id_vendor = USB_VENDOR_FREESCALE;
278	strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor));
279
280	/* Set flags that affect ehci_init() behavior. */
281	esc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
282	err = ehci_init(esc);
283	if (err != 0) {
284		device_printf(dev, "USB init failed, usb_err_t=%d\n",
285		    err);
286		goto out;
287	}
288	esc->sc_flags |= EHCI_SCFLG_DONEINIT;
289
290	/* Probe the bus. */
291	err = device_probe_and_attach(esc->sc_bus.bdev);
292	if (err != 0) {
293		device_printf(dev,
294		    "device_probe_and_attach() failed\n");
295		goto out;
296	}
297
298	err = 0;
299
300out:
301
302	if (err != 0)
303		imx_ehci_detach(dev);
304
305	return (err);
306}
307
308static device_method_t ehci_methods[] = {
309	/* Device interface */
310	DEVMETHOD(device_probe, imx_ehci_probe),
311	DEVMETHOD(device_attach, imx_ehci_attach),
312	DEVMETHOD(device_detach, imx_ehci_detach),
313	DEVMETHOD(device_suspend, bus_generic_suspend),
314	DEVMETHOD(device_resume, bus_generic_resume),
315	DEVMETHOD(device_shutdown, bus_generic_shutdown),
316
317	/* Bus interface */
318	DEVMETHOD(bus_print_child, bus_generic_print_child),
319
320	DEVMETHOD_END
321};
322
323static driver_t ehci_driver = {
324	"ehci",
325	ehci_methods,
326	sizeof(struct imx_ehci_softc)
327};
328
329static devclass_t ehci_devclass;
330
331DRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
332MODULE_DEPEND(ehci, usb, 1, 1, 1);
333