1297670Ssgalabov#include <sys/cdefs.h>
2297670Ssgalabov__FBSDID("$FreeBSD: releng/11.0/sys/mips/mediatek/mtk_dotg.c 297670 2016-04-07 11:20:03Z sgalabov $");
3297670Ssgalabov
4297670Ssgalabov/*-
5297670Ssgalabov * Copyright (c) 2015-2016 Stanislav Galabov. All rights reserved.
6297670Ssgalabov * Copyright (c) 2010,2011 Aleksandr Rybalko. All rights reserved.
7297670Ssgalabov * Copyright (c) 2007-2008 Hans Petter Selasky. All rights reserved.
8297670Ssgalabov *
9297670Ssgalabov * Redistribution and use in source and binary forms, with or without
10297670Ssgalabov * modification, are permitted provided that the following conditions
11297670Ssgalabov * are met:
12297670Ssgalabov * 1. Redistributions of source code must retain the above copyright
13297670Ssgalabov *    notice, this list of conditions and the following disclaimer.
14297670Ssgalabov * 2. Redistributions in binary form must reproduce the above copyright
15297670Ssgalabov *    notice, this list of conditions and the following disclaimer in the
16297670Ssgalabov *    documentation and/or other materials provided with the distribution.
17297670Ssgalabov *
18297670Ssgalabov * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19297670Ssgalabov * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20297670Ssgalabov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21297670Ssgalabov * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22297670Ssgalabov * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23297670Ssgalabov * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24297670Ssgalabov * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25297670Ssgalabov * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26297670Ssgalabov * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27297670Ssgalabov * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28297670Ssgalabov * SUCH DAMAGE.
29297670Ssgalabov */
30297670Ssgalabov
31297670Ssgalabov#include <sys/stdint.h>
32297670Ssgalabov#include <sys/stddef.h>
33297670Ssgalabov#include <sys/param.h>
34297670Ssgalabov#include <sys/queue.h>
35297670Ssgalabov#include <sys/types.h>
36297670Ssgalabov#include <sys/systm.h>
37297670Ssgalabov#include <sys/kernel.h>
38297670Ssgalabov#include <sys/bus.h>
39297670Ssgalabov#include <sys/module.h>
40297670Ssgalabov#include <sys/lock.h>
41297670Ssgalabov#include <sys/mutex.h>
42297670Ssgalabov#include <sys/condvar.h>
43297670Ssgalabov#include <sys/sysctl.h>
44297670Ssgalabov#include <sys/sx.h>
45297670Ssgalabov#include <sys/unistd.h>
46297670Ssgalabov#include <sys/callout.h>
47297670Ssgalabov#include <sys/malloc.h>
48297670Ssgalabov#include <sys/priv.h>
49297670Ssgalabov#include <sys/rman.h>
50297670Ssgalabov
51297670Ssgalabov#include <dev/usb/usb.h>
52297670Ssgalabov#include <dev/usb/usbdi.h>
53297670Ssgalabov
54297670Ssgalabov#include <dev/usb/usb_core.h>
55297670Ssgalabov#include <dev/usb/usb_busdma.h>
56297670Ssgalabov#include <dev/usb/usb_process.h>
57297670Ssgalabov#include <dev/usb/usb_util.h>
58297670Ssgalabov
59297670Ssgalabov#include <dev/usb/usb_controller.h>
60297670Ssgalabov#include <dev/usb/usb_bus.h>
61297670Ssgalabov
62297670Ssgalabov#include <dev/usb/controller/dwc_otg.h>
63297670Ssgalabov
64297670Ssgalabov#include <dev/ofw/openfirm.h>
65297670Ssgalabov#include <dev/ofw/ofw_bus.h>
66297670Ssgalabov#include <dev/ofw/ofw_bus_subr.h>
67297670Ssgalabov
68297670Ssgalabov#define	MEM_RID	0
69297670Ssgalabov
70297670Ssgalabovstatic device_probe_t dotg_fdt_probe;
71297670Ssgalabovstatic device_attach_t dotg_fdt_attach;
72297670Ssgalabovstatic device_detach_t dotg_fdt_detach;
73297670Ssgalabov
74297670Ssgalabovstatic int
75297670Ssgalabovdotg_fdt_probe(device_t dev)
76297670Ssgalabov{
77297670Ssgalabov
78297670Ssgalabov	if (!ofw_bus_status_okay(dev))
79297670Ssgalabov		return (ENXIO);
80297670Ssgalabov
81297670Ssgalabov	if (!ofw_bus_is_compatible(dev, "ralink,rt3050-otg"))
82297670Ssgalabov		return (ENXIO);
83297670Ssgalabov
84297670Ssgalabov	device_set_desc(dev, "MTK DWC-OTG USB Controller");
85297670Ssgalabov	return (0);
86297670Ssgalabov}
87297670Ssgalabov
88297670Ssgalabovstatic int
89297670Ssgalabovdotg_fdt_attach(device_t dev)
90297670Ssgalabov{
91297670Ssgalabov	struct dwc_otg_softc *sc = device_get_softc(dev);
92297670Ssgalabov	int err, rid;
93297670Ssgalabov
94297670Ssgalabov	/* setup controller interface softc */
95297670Ssgalabov
96297670Ssgalabov	/* initialise some bus fields */
97297670Ssgalabov	sc->sc_mode = DWC_MODE_HOST;
98297670Ssgalabov	sc->sc_bus.parent = dev;
99297670Ssgalabov	sc->sc_bus.devices = sc->sc_devices;
100297670Ssgalabov	sc->sc_bus.devices_max = DWC_OTG_MAX_DEVICES;
101297670Ssgalabov	sc->sc_bus.dma_bits = 32;
102297670Ssgalabov
103297670Ssgalabov	/* get all DMA memory */
104297670Ssgalabov	if (usb_bus_mem_alloc_all(&sc->sc_bus,
105297670Ssgalabov	    USB_GET_DMA_TAG(dev), NULL)) {
106297670Ssgalabov		printf("No mem\n");
107297670Ssgalabov		return (ENOMEM);
108297670Ssgalabov	}
109297670Ssgalabov	rid = 0;
110297670Ssgalabov	sc->sc_io_res =
111297670Ssgalabov	    bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
112297670Ssgalabov	if (!(sc->sc_io_res)) {
113297670Ssgalabov		printf("Can`t alloc MEM\n");
114297670Ssgalabov		goto error;
115297670Ssgalabov	}
116297670Ssgalabov	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
117297670Ssgalabov	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
118297670Ssgalabov	sc->sc_io_size = rman_get_size(sc->sc_io_res);
119297670Ssgalabov
120297670Ssgalabov	rid = 0;
121297670Ssgalabov	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ,
122297670Ssgalabov	    &rid, RF_ACTIVE);
123297670Ssgalabov	if (!(sc->sc_irq_res)) {
124297670Ssgalabov		printf("Can`t alloc IRQ\n");
125297670Ssgalabov		goto error;
126297670Ssgalabov	}
127297670Ssgalabov
128297670Ssgalabov	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
129297670Ssgalabov	if (!(sc->sc_bus.bdev)) {
130297670Ssgalabov		printf("Can`t add usbus\n");
131297670Ssgalabov		goto error;
132297670Ssgalabov	}
133297670Ssgalabov	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
134297670Ssgalabov
135297670Ssgalabov	err = bus_setup_intr(dev, sc->sc_irq_res,
136297670Ssgalabov	    INTR_TYPE_TTY | INTR_MPSAFE, dwc_otg_filter_interrupt,
137297670Ssgalabov	    dwc_otg_interrupt, sc, &sc->sc_intr_hdl);
138297670Ssgalabov	if (err) {
139297670Ssgalabov		sc->sc_intr_hdl = NULL;
140297670Ssgalabov		printf("Can`t set IRQ handle\n");
141297670Ssgalabov		goto error;
142297670Ssgalabov	}
143297670Ssgalabov
144297670Ssgalabov	err = dwc_otg_init(sc);
145297670Ssgalabov	if (err) printf("dotg_init fail\n");
146297670Ssgalabov	if (!err) {
147297670Ssgalabov		err = device_probe_and_attach(sc->sc_bus.bdev);
148297670Ssgalabov		if (err) printf("device_probe_and_attach fail %d\n", err);
149297670Ssgalabov	}
150297670Ssgalabov	if (err) {
151297670Ssgalabov		goto error;
152297670Ssgalabov	}
153297670Ssgalabov	return (0);
154297670Ssgalabov
155297670Ssgalaboverror:
156297670Ssgalabov	dotg_fdt_detach(dev);
157297670Ssgalabov	return (ENXIO);
158297670Ssgalabov}
159297670Ssgalabov
160297670Ssgalabovstatic int
161297670Ssgalabovdotg_fdt_detach(device_t dev)
162297670Ssgalabov{
163297670Ssgalabov	struct dwc_otg_softc *sc = device_get_softc(dev);
164297670Ssgalabov	device_t bdev;
165297670Ssgalabov	int err;
166297670Ssgalabov
167297670Ssgalabov	if (sc->sc_bus.bdev) {
168297670Ssgalabov		bdev = sc->sc_bus.bdev;
169297670Ssgalabov		device_detach(bdev);
170297670Ssgalabov		device_delete_child(dev, bdev);
171297670Ssgalabov	}
172297670Ssgalabov	/* during module unload there are lots of children leftover */
173297670Ssgalabov	device_delete_children(dev);
174297670Ssgalabov
175297670Ssgalabov	if (sc->sc_irq_res && sc->sc_intr_hdl) {
176297670Ssgalabov		/*
177297670Ssgalabov		 * only call dotg_fdt_uninit() after dotg_fdt_init()
178297670Ssgalabov		 */
179297670Ssgalabov		dwc_otg_uninit(sc);
180297670Ssgalabov
181297670Ssgalabov		err = bus_teardown_intr(dev, sc->sc_irq_res,
182297670Ssgalabov		    sc->sc_intr_hdl);
183297670Ssgalabov		sc->sc_intr_hdl = NULL;
184297670Ssgalabov	}
185297670Ssgalabov	if (sc->sc_irq_res) {
186297670Ssgalabov		bus_release_resource(dev, SYS_RES_IRQ, 0,
187297670Ssgalabov		    sc->sc_irq_res);
188297670Ssgalabov		sc->sc_irq_res = NULL;
189297670Ssgalabov	}
190297670Ssgalabov	if (sc->sc_io_res) {
191297670Ssgalabov		bus_release_resource(dev, SYS_RES_MEMORY, 0,
192297670Ssgalabov		    sc->sc_io_res);
193297670Ssgalabov		sc->sc_io_res = NULL;
194297670Ssgalabov	}
195297670Ssgalabov	usb_bus_mem_free_all(&sc->sc_bus, NULL);
196297670Ssgalabov
197297670Ssgalabov	return (0);
198297670Ssgalabov}
199297670Ssgalabov
200297670Ssgalabovstatic device_method_t dotg_fdt_methods[] = {
201297670Ssgalabov	/* Device interface */
202297670Ssgalabov	DEVMETHOD(device_probe, dotg_fdt_probe),
203297670Ssgalabov	DEVMETHOD(device_attach, dotg_fdt_attach),
204297670Ssgalabov	DEVMETHOD(device_detach, dotg_fdt_detach),
205297670Ssgalabov	DEVMETHOD(device_suspend, bus_generic_suspend),
206297670Ssgalabov	DEVMETHOD(device_resume, bus_generic_resume),
207297670Ssgalabov	DEVMETHOD(device_shutdown, bus_generic_shutdown),
208297670Ssgalabov
209297670Ssgalabov	DEVMETHOD_END
210297670Ssgalabov};
211297670Ssgalabov
212297670Ssgalabovstatic driver_t dotg_fdt_driver = {
213297670Ssgalabov	.name = "dwcotg",
214297670Ssgalabov	.methods = dotg_fdt_methods,
215297670Ssgalabov	.size = sizeof(struct dwc_otg_softc),
216297670Ssgalabov};
217297670Ssgalabov
218297670Ssgalabovstatic devclass_t dotg_fdt_devclass;
219297670Ssgalabov
220297670SsgalabovDRIVER_MODULE(dotg, simplebus, dotg_fdt_driver, dotg_fdt_devclass, 0, 0);
221