ehci_imx.c revision 257393
1248557Sray/*-
2248557Sray * Copyright (c) 2010-2012 Semihalf
3248557Sray * Copyright (c) 2012 The FreeBSD Foundation
4257393Sian * Copyright (c) 2013 Ian Lepore <ian@freebsd.org>
5248557Sray * All rights reserved.
6248557Sray *
7248557Sray * Portions of this software were developed by Oleksandr Rybalko
8248557Sray * under sponsorship from the FreeBSD Foundation.
9248557Sray *
10248557Sray * Redistribution and use in source and binary forms, with or without
11248557Sray * modification, are permitted provided that the following conditions
12248557Sray * are met:
13248557Sray * 1. Redistributions of source code must retain the above copyright
14248557Sray *    notice, this list of conditions and the following disclaimer.
15248557Sray * 2. Redistributions in binary form must reproduce the above copyright
16248557Sray *    notice, this list of conditions and the following disclaimer in the
17248557Sray *    documentation and/or other materials provided with the distribution.
18248557Sray *
19248557Sray * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20248557Sray * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21248557Sray * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22248557Sray * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23248557Sray * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24248557Sray * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25248557Sray * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26248557Sray * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27248557Sray * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28248557Sray * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29248557Sray * SUCH DAMAGE.
30248557Sray */
31248557Sray
32248557Sray#include <sys/cdefs.h>
33248557Sray__FBSDID("$FreeBSD: head/sys/dev/usb/controller/ehci_imx.c 257393 2013-10-30 18:26:18Z ian $");
34248557Sray
35257393Sian/*
36257393Sian * EHCI driver for Freescale i.MX SoCs which incorporate the USBOH3 controller.
37257393Sian */
38248557Sray
39248557Sray#include <sys/param.h>
40248557Sray#include <sys/systm.h>
41248557Sray#include <sys/kernel.h>
42248557Sray#include <sys/module.h>
43248557Sray#include <sys/bus.h>
44248557Sray#include <sys/condvar.h>
45248557Sray#include <sys/rman.h>
46248557Sray
47248557Sray#include <dev/ofw/ofw_bus.h>
48248557Sray#include <dev/ofw/ofw_bus_subr.h>
49248557Sray
50248557Sray#include <dev/usb/usb.h>
51248557Sray#include <dev/usb/usbdi.h>
52248557Sray#include <dev/usb/usb_busdma.h>
53248557Sray#include <dev/usb/usb_process.h>
54248557Sray#include <dev/usb/usb_controller.h>
55248557Sray#include <dev/usb/usb_bus.h>
56248557Sray#include <dev/usb/controller/ehci.h>
57248557Sray#include <dev/usb/controller/ehcireg.h>
58257393Sian#include "usbdevs.h"
59248557Sray
60248557Sray#include <machine/bus.h>
61248557Sray#include <machine/resource.h>
62248557Sray
63257393Sian#include <arm/freescale/imx/imx_machdep.h>
64257393Sian
65248557Sray#include "opt_platform.h"
66248557Sray
67257393Sian/*
68257393Sian * Notes on the hardware and related FDT data seen in the wild.
69257393Sian *
70257393Sian * There are two sets of registers in the USBOH3 implementation; documentation
71257393Sian * refers to them as "core" and "non-core" registers.  A set of core register
72257393Sian * exists for each OTG or EHCI device.  There is a single set of non-core
73257393Sian * registers per USBOH3, and they control aspects of operation not directly
74257393Sian * related to the USB specs, such as whether interrupts from each of the core
75257393Sian * devices are able to generate a SoC wakeup event.
76257393Sian *
77257393Sian * In the FreeBSD universe we might be inclined to describe the core and
78257393Sian * non-core registers by using a pair of resource address/size values (two
79257393Sian * entries in the reg property for each core).  However, we have to work with
80257393Sian * existing FDT data (which mostly comes from the linux universe), and the way
81257393Sian * they've chosen to represent this is with an entry for a "usbmisc" device
82257393Sian * whose reg property describes the non-core registers. The way we handle FDT
83257393Sian * data, this means that the resources (memory-mapped register range) for the
84257393Sian * non-core registers belongs to a device other than the echi devices.
85257393Sian *
86257393Sian * At the moment we have no need to access the non-core registers, so all of
87257393Sian * this amounts to documenting what's known.  The following compat strings have
88257393Sian * been seen in existing FDT data:
89257393Sian *   - "fsl,imx25-usbmisc"
90257393Sian *   - "fsl,imx51-usbmisc";
91257393Sian *   - "fsl,imx6q-usbmisc";
92257393Sian *
93257393Sian * In addition to the single usbmisc device, the existing FDT data defines a
94257393Sian * separate device for each of the OTG or EHCI cores within the USBOH3.  Each of
95257393Sian * those devices has a set of core registers described by the reg property.
96257393Sian *
97257393Sian * The core registers for each of the four cores in the USBOH3 are divided into
98257393Sian * two parts: a set of imx-specific registers at an offset of 0 from the
99257393Sian * beginning of the register range, and the standard USB (EHCI or OTG) registers
100257393Sian * at an offset of 0x100 from the beginning of the register range.  The FreeBSD
101257393Sian * way of dealing with this might be to map out two ranges in the reg property,
102257393Sian * but that's not what the alternate universe has done.  To work with existing
103257393Sian * FDT data, we acquire the resource that maps all the core registers, then use
104257393Sian * bus_space_subregion() to create another resource that maps just the standard
105257393Sian * USB registers, which we provide to the standard USB code in the ehci_softc.
106257393Sian *
107257393Sian * The following compat strings have been seen for the OTG and EHCI cores.  The
108257393Sian * FDT compat table in this driver contains all these strings, but as of this
109257393Sian * writing, not all of these SoCs have been tested with the driver.  The fact
110257393Sian * that imx27 is common to all of them gives some hope that the driver will work
111257393Sian * on all these SoCs.
112257393Sian *   - "fsl,imx23-usb", "fsl,imx27-usb";
113257393Sian *   - "fsl,imx25-usb", "fsl,imx27-usb";
114257393Sian *   - "fsl,imx28-usb", "fsl,imx27-usb";
115257393Sian *   - "fsl,imx51-usb", "fsl,imx27-usb";
116257393Sian *   - "fsl,imx53-usb", "fsl,imx27-usb";
117257393Sian *   - "fsl,imx6q-usb", "fsl,imx27-usb";
118257393Sian *
119257393Sian * The FDT data for some SoCs contains the following properties, which we don't
120257393Sian * currently do anything with:
121257393Sian *   - fsl,usbmisc = <&usbmisc 0>;
122257393Sian *   - fsl,usbphy = <&usbphy0>;
123257393Sian *
124257393Sian * Some imx SoCs have FDT data related to USB PHY, some don't.  We have separate
125257393Sian * usbphy drivers where needed; this data is mentioned here just to keep all the
126257393Sian * imx-FDT-usb-related info in one place.  Here are the usbphy compat strings
127257393Sian * known to exist:
128257393Sian *   - "nop-usbphy"
129257393Sian *   - "usb-nop-xceiv";
130257393Sian *   - "fsl,imx23-usbphy"
131257393Sian *   - "fsl,imx28-usbphy", "fsl,imx23-usbphy";
132257393Sian *   - "fsl,imx6q-usbphy", "fsl,imx23-usbphy";
133257393Sian *
134257393Sian */
135248557Sray
136257393Sianstatic struct ofw_compat_data compat_data[] = {
137257393Sian	{"fsl,imx6q-usb",	1},
138257393Sian	{"fsl,imx53-usb",	1},
139257393Sian	{"fsl,imx51-usb",	1},
140257393Sian	{"fsl,imx28-usb",	1},
141257393Sian	{"fsl,imx27-usb",	1},
142257393Sian	{"fsl,imx25-usb",	1},
143257393Sian	{"fsl,imx23-usb",	1},
144257393Sian	{NULL,		 	0},
145248557Sray};
146248557Sray
147257393Sian/*
148257393Sian * Each EHCI device in the SoC has some SoC-specific per-device registers at an
149257393Sian * offset of 0, then the standard EHCI registers begin at an offset of 0x100.
150257393Sian */
151257393Sian#define	IMX_EHCI_REG_OFF	0x100
152257393Sian#define	IMX_EHCI_REG_SIZE	0x100
153257393Sian
154257393Sianstruct imx_ehci_softc {
155257393Sian	ehci_softc_t	ehci_softc;
156257393Sian	struct resource	*ehci_mem_res;	/* EHCI core regs. */
157257393Sian	struct resource	*ehci_irq_res;	/* EHCI core IRQ. */
158248557Sray};
159248557Sray
160257393Sianstatic int
161257393Sianimx_ehci_probe(device_t dev)
162257393Sian{
163248557Sray
164257393Sian	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
165257393Sian		device_set_desc(dev, "Freescale i.MX integrated USB controller");
166257393Sian		return (BUS_PROBE_DEFAULT);
167257393Sian	}
168257393Sian	return (ENXIO);
169257393Sian}
170248557Sray
171257393Sianstatic int
172257393Sianimx_ehci_detach(device_t dev)
173257393Sian{
174257393Sian	struct imx_ehci_softc *sc;
175257393Sian	ehci_softc_t *esc;
176248557Sray
177257393Sian	sc = device_get_softc(dev);
178248557Sray
179257393Sian	esc = &sc->ehci_softc;
180248557Sray
181257393Sian	if (esc->sc_bus.bdev != NULL)
182257393Sian		device_delete_child(dev, esc->sc_bus.bdev);
183257393Sian	if (esc->sc_flags & EHCI_SCFLG_DONEINIT)
184257393Sian		ehci_detach(esc);
185257393Sian	if (esc->sc_intr_hdl != NULL)
186257393Sian		bus_teardown_intr(dev, esc->sc_irq_res,
187257393Sian		    esc->sc_intr_hdl);
188257393Sian	if (sc->ehci_irq_res != NULL)
189257393Sian		bus_release_resource(dev, SYS_RES_IRQ, 0,
190257393Sian		    sc->ehci_irq_res);
191257393Sian	if (sc->ehci_mem_res != NULL)
192257393Sian		bus_release_resource(dev, SYS_RES_MEMORY, 0,
193257393Sian		    sc->ehci_mem_res);
194248557Sray
195257393Sian	usb_bus_mem_free_all(&esc->sc_bus, &ehci_iterate_hw_softc);
196248557Sray
197257393Sian	/* During module unload there are lots of children leftover */
198257393Sian	device_delete_children(dev);
199248557Sray
200257393Sian	return (0);
201248557Sray}
202248557Sray
203248557Sraystatic int
204257393Sianimx_ehci_attach(device_t dev)
205248557Sray{
206248557Sray	struct imx_ehci_softc *sc;
207248557Sray	ehci_softc_t *esc;
208257393Sian	int err, rid;
209248557Sray
210257393Sian	sc = device_get_softc(dev);
211257393Sian	esc = &sc->ehci_softc;
212257393Sian	err = 0;
213257393Sian
214257393Sian	/* Allocate bus_space resources. */
215248557Sray	rid = 0;
216257393Sian	sc->ehci_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
217257393Sian	    RF_ACTIVE);
218257393Sian	if (sc->ehci_mem_res == NULL) {
219257393Sian		device_printf(dev, "Cannot allocate memory resources\n");
220257393Sian		err = ENXIO;
221257393Sian		goto out;
222257393Sian	}
223248557Sray
224257393Sian	rid = 0;
225257393Sian	sc->ehci_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
226257393Sian	    RF_ACTIVE);
227257393Sian	if (sc->ehci_irq_res == NULL) {
228257393Sian		device_printf(dev, "Cannot allocate IRQ resources\n");
229257393Sian		err = ENXIO;
230257393Sian		goto out;
231248557Sray	}
232248557Sray
233257393Sian	esc->sc_io_tag = rman_get_bustag(sc->ehci_mem_res);
234257393Sian	esc->sc_bus.parent = dev;
235257393Sian	esc->sc_bus.devices = esc->sc_devices;
236257393Sian	esc->sc_bus.devices_max = EHCI_MAX_DEVICES;
237248557Sray
238257393Sian	if (usb_bus_mem_alloc_all(&esc->sc_bus, USB_GET_DMA_TAG(dev),
239257393Sian	    &ehci_iterate_hw_softc) != 0) {
240257393Sian		device_printf(dev, "usb_bus_mem_alloc_all() failed\n");
241257393Sian		err = ENOMEM;
242257393Sian		goto out;
243257393Sian	}
244248557Sray
245257393Sian	/*
246257393Sian	 * Set handle to USB related registers subregion used by
247257393Sian	 * generic EHCI driver.
248257393Sian	 */
249257393Sian	err = bus_space_subregion(esc->sc_io_tag,
250257393Sian	    rman_get_bushandle(sc->ehci_mem_res),
251257393Sian	    IMX_EHCI_REG_OFF, IMX_EHCI_REG_SIZE, &esc->sc_io_hdl);
252257393Sian	if (err != 0) {
253257393Sian		device_printf(dev, "bus_space_subregion() failed\n");
254257393Sian		err = ENXIO;
255257393Sian		goto out;
256257393Sian	}
257248557Sray
258257393Sian	/* Setup interrupt handler. */
259257393Sian	err = bus_setup_intr(dev, sc->ehci_irq_res, INTR_TYPE_BIO, NULL,
260257393Sian	    (driver_intr_t *)ehci_interrupt, esc, &esc->sc_intr_hdl);
261257393Sian	if (err != 0) {
262257393Sian		device_printf(dev, "Could not setup IRQ\n");
263257393Sian		goto out;
264257393Sian	}
265248557Sray
266257393Sian	/* Turn on clocks. */
267257393Sian	imx_ccm_usb_enable(dev);
268248557Sray
269257393Sian	/* Add USB bus device. */
270257393Sian	esc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
271257393Sian	if (esc->sc_bus.bdev == NULL) {
272257393Sian		device_printf(dev, "Could not add USB device\n");
273257393Sian		goto out;
274257393Sian	}
275257393Sian	device_set_ivars(esc->sc_bus.bdev, &esc->sc_bus);
276248557Sray
277257393Sian	esc->sc_id_vendor = USB_VENDOR_FREESCALE;
278257393Sian	strlcpy(esc->sc_vendor, "Freescale", sizeof(esc->sc_vendor));
279248557Sray
280257393Sian	/* Set flags that affect ehci_init() behavior. */
281257393Sian	esc->sc_flags |= EHCI_SCFLG_DONTRESET | EHCI_SCFLG_NORESTERM;
282257393Sian	err = ehci_init(esc);
283257393Sian	if (err != 0) {
284257393Sian		device_printf(dev, "USB init failed, usb_err_t=%d\n",
285257393Sian		    err);
286257393Sian		goto out;
287257393Sian	}
288257393Sian	esc->sc_flags |= EHCI_SCFLG_DONEINIT;
289248557Sray
290257393Sian	/* Probe the bus. */
291257393Sian	err = device_probe_and_attach(esc->sc_bus.bdev);
292257393Sian	if (err != 0) {
293257393Sian		device_printf(dev,
294257393Sian		    "device_probe_and_attach() failed\n");
295257393Sian		goto out;
296257393Sian	}
297248557Sray
298257393Sian	err = 0;
299248557Sray
300257393Sianout:
301248557Sray
302257393Sian	if (err != 0)
303257393Sian		imx_ehci_detach(dev);
304248557Sray
305257393Sian	return (err);
306248557Sray}
307248557Sray
308257393Sianstatic device_method_t ehci_methods[] = {
309257393Sian	/* Device interface */
310257393Sian	DEVMETHOD(device_probe, imx_ehci_probe),
311257393Sian	DEVMETHOD(device_attach, imx_ehci_attach),
312257393Sian	DEVMETHOD(device_detach, imx_ehci_detach),
313257393Sian	DEVMETHOD(device_suspend, bus_generic_suspend),
314257393Sian	DEVMETHOD(device_resume, bus_generic_resume),
315257393Sian	DEVMETHOD(device_shutdown, bus_generic_shutdown),
316248557Sray
317257393Sian	/* Bus interface */
318257393Sian	DEVMETHOD(bus_print_child, bus_generic_print_child),
319248557Sray
320257393Sian	DEVMETHOD_END
321257393Sian};
322248557Sray
323257393Sianstatic driver_t ehci_driver = {
324257393Sian	"ehci",
325257393Sian	ehci_methods,
326257393Sian	sizeof(struct imx_ehci_softc)
327257393Sian};
328248557Sray
329257393Sianstatic devclass_t ehci_devclass;
330248557Sray
331257393SianDRIVER_MODULE(ehci, simplebus, ehci_driver, ehci_devclass, 0, 0);
332257393SianMODULE_DEPEND(ehci, usb, 1, 1, 1);
333