saf1761_otg_fdt.c revision 266742
1/* $FreeBSD: head/sys/dev/usb/controller/saf1761_otg_fdt.c 266742 2014-05-27 10:12:16Z hselasky $ */
2/*-
3 * Copyright (c) 2014 Hans Petter Selasky <hselasky@FreeBSD.org>
4 * All rights reserved.
5 *
6 * This software was developed by SRI International and the University of
7 * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
8 * ("CTSRD"), as part of the DARPA CRASH research programme.
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#ifdef USB_GLOBAL_INCLUDE_FILE
33#include USB_GLOBAL_INCLUDE_FILE
34#else
35#include <sys/stdint.h>
36#include <sys/stddef.h>
37#include <sys/param.h>
38#include <sys/queue.h>
39#include <sys/types.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/bus.h>
43#include <sys/module.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/condvar.h>
47#include <sys/sysctl.h>
48#include <sys/sx.h>
49#include <sys/unistd.h>
50#include <sys/callout.h>
51#include <sys/malloc.h>
52#include <sys/priv.h>
53#include <sys/rman.h>
54
55#include <dev/fdt/fdt_common.h>
56
57#include <dev/ofw/openfirm.h>
58#include <dev/ofw/ofw_bus.h>
59#include <dev/ofw/ofw_bus_subr.h>
60
61#include <dev/usb/usb.h>
62#include <dev/usb/usbdi.h>
63
64#include <dev/usb/usb_core.h>
65#include <dev/usb/usb_busdma.h>
66#include <dev/usb/usb_process.h>
67#include <dev/usb/usb_transfer.h>
68#include <dev/usb/usb_device.h>
69#include <dev/usb/usb_hub.h>
70#include <dev/usb/usb_util.h>
71
72#include <dev/usb/usb_controller.h>
73#include <dev/usb/usb_bus.h>
74#endif					/* USB_GLOBAL_INCLUDE_FILE */
75
76#include <dev/usb/controller/saf1761_otg.h>
77#include <dev/usb/controller/saf1761_otg_reg.h>
78
79static device_probe_t saf1761_otg_fdt_probe;
80static device_attach_t saf1761_otg_fdt_attach;
81static device_detach_t saf1761_otg_fdt_detach;
82
83static device_method_t saf1761_otg_methods[] = {
84	/* Device interface */
85	DEVMETHOD(device_probe, saf1761_otg_fdt_probe),
86	DEVMETHOD(device_attach, saf1761_otg_fdt_attach),
87	DEVMETHOD(device_detach, saf1761_otg_fdt_detach),
88	DEVMETHOD(device_suspend, bus_generic_suspend),
89	DEVMETHOD(device_resume, bus_generic_resume),
90	DEVMETHOD(device_shutdown, bus_generic_shutdown),
91
92	DEVMETHOD_END
93};
94
95static driver_t saf1761_otg_driver = {
96	.name = "saf1761",
97	.methods = saf1761_otg_methods,
98	.size = sizeof(struct saf1761_otg_softc),
99};
100
101static devclass_t saf1761_otg_devclass;
102
103DRIVER_MODULE(saf1761, simplebus, saf1761_otg_driver, saf1761_otg_devclass, 0, 0);
104MODULE_DEPEND(saf1761, usb, 1, 1, 1);
105
106static int
107saf1761_otg_fdt_probe(device_t dev)
108{
109	if (!ofw_bus_status_okay(dev))
110		return (ENXIO);
111
112	if (!ofw_bus_is_compatible(dev, "nxp,usb-isp1761"))
113		return (ENXIO);
114
115	device_set_desc(dev, "ISP1761/SAF1761 DCI USB 2.0 Device Controller");
116
117	return (0);
118}
119
120static int
121saf1761_otg_fdt_attach(device_t dev)
122{
123	struct saf1761_otg_softc *sc = device_get_softc(dev);
124	char param[24];
125	int err;
126	int rid;
127
128	/* get configuration from FDT */
129
130	/* get bus-width, if any */
131	if (OF_getprop(ofw_bus_get_node(dev), "bus-width",
132	    &param, sizeof(param)) > 0) {
133		param[sizeof(param) - 1] = 0;
134		if (strcmp(param, "32") == 0)
135			sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
136	} else {
137		/* assume 32-bit data bus */
138		sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DATA_BUS_WIDTH;
139	}
140
141	/* get analog over-current setting */
142	if (OF_getprop(ofw_bus_get_node(dev), "analog-oc",
143	    &param, sizeof(param)) > 0) {
144		sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_ANA_DIGI_OC;
145	}
146
147	/* get DACK polarity */
148	if (OF_getprop(ofw_bus_get_node(dev), "dack-polarity",
149	    &param, sizeof(param)) > 0) {
150		sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DACK_POL;
151	}
152
153	/* get DREQ polarity */
154	if (OF_getprop(ofw_bus_get_node(dev), "dreq-polarity",
155	    &param, sizeof(param)) > 0) {
156		sc->sc_hw_mode |= SOTG_HW_MODE_CTRL_DREQ_POL;
157	}
158
159	/* get IRQ polarity */
160	if (OF_getprop(ofw_bus_get_node(dev), "int-polarity",
161	    &param, sizeof(param)) > 0) {
162		sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTPOL;
163	}
164
165	/* get IRQ level triggering */
166	if (OF_getprop(ofw_bus_get_node(dev), "int-level",
167	    &param, sizeof(param)) > 0) {
168		sc->sc_interrupt_cfg |= SOTG_INTERRUPT_CFG_INTLVL;
169	}
170
171	/* initialise some bus fields */
172	sc->sc_bus.parent = dev;
173	sc->sc_bus.devices = sc->sc_devices;
174	sc->sc_bus.devices_max = SOTG_MAX_DEVICES;
175
176	/* get all DMA memory */
177	if (usb_bus_mem_alloc_all(&sc->sc_bus,
178	    USB_GET_DMA_TAG(dev), NULL)) {
179		return (ENOMEM);
180	}
181	rid = 0;
182	sc->sc_io_res =
183	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
184
185	if (!sc->sc_io_res) {
186		goto error;
187	}
188	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
189	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
190	sc->sc_io_size = rman_get_size(sc->sc_io_res);
191
192	rid = 0;
193	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
194	    RF_SHAREABLE | RF_ACTIVE);
195	if (sc->sc_irq_res == NULL) {
196		goto error;
197	}
198	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
199	if (!(sc->sc_bus.bdev)) {
200		goto error;
201	}
202	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
203
204	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
205	    NULL, (driver_intr_t *)saf1761_otg_interrupt, sc, &sc->sc_intr_hdl);
206	if (err) {
207		sc->sc_intr_hdl = NULL;
208		goto error;
209	}
210	err = saf1761_otg_init(sc);
211	if (err) {
212		device_printf(dev, "Init failed\n");
213		goto error;
214	}
215	err = device_probe_and_attach(sc->sc_bus.bdev);
216	if (err) {
217		device_printf(dev, "USB probe and attach failed\n");
218		goto error;
219	}
220	return (0);
221
222error:
223	saf1761_otg_fdt_detach(dev);
224	return (ENXIO);
225}
226
227static int
228saf1761_otg_fdt_detach(device_t dev)
229{
230	struct saf1761_otg_softc *sc = device_get_softc(dev);
231	device_t bdev;
232	int err;
233
234	if (sc->sc_bus.bdev) {
235		bdev = sc->sc_bus.bdev;
236		device_detach(bdev);
237		device_delete_child(dev, bdev);
238	}
239	/* during module unload there are lots of children leftover */
240	device_delete_children(dev);
241
242	if (sc->sc_irq_res && sc->sc_intr_hdl) {
243		/*
244		 * Only call uninit() after init()
245		 */
246		saf1761_otg_uninit(sc);
247
248		err = bus_teardown_intr(dev, sc->sc_irq_res,
249		    sc->sc_intr_hdl);
250		sc->sc_intr_hdl = NULL;
251	}
252	if (sc->sc_irq_res) {
253		bus_release_resource(dev, SYS_RES_IRQ, 0,
254		    sc->sc_irq_res);
255		sc->sc_irq_res = NULL;
256	}
257	if (sc->sc_io_res) {
258		bus_release_resource(dev, SYS_RES_MEMORY, 0,
259		    sc->sc_io_res);
260		sc->sc_io_res = NULL;
261	}
262	usb_bus_mem_free_all(&sc->sc_bus, NULL);
263
264	return (0);
265}
266