xref: /freebsd/sys/dev/usb/controller/dwc3/aw_dwc3.c (revision fe75646a0234a261c0013bf1840fdac4acaf0cec)
1*fe75646aSEmmanuel Vadot /*-
2*fe75646aSEmmanuel Vadot  * SPDX-License-Identifier: BSD-2-Clause
3*fe75646aSEmmanuel Vadot  *
4*fe75646aSEmmanuel Vadot  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org>
5*fe75646aSEmmanuel Vadot  *
6*fe75646aSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7*fe75646aSEmmanuel Vadot  * modification, are permitted provided that the following conditions
8*fe75646aSEmmanuel Vadot  * are met:
9*fe75646aSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10*fe75646aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11*fe75646aSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12*fe75646aSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13*fe75646aSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14*fe75646aSEmmanuel Vadot  *
15*fe75646aSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*fe75646aSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*fe75646aSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*fe75646aSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*fe75646aSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*fe75646aSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*fe75646aSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*fe75646aSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*fe75646aSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*fe75646aSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*fe75646aSEmmanuel Vadot  * SUCH DAMAGE.
26*fe75646aSEmmanuel Vadot  */
27*fe75646aSEmmanuel Vadot 
28*fe75646aSEmmanuel Vadot /*
29*fe75646aSEmmanuel Vadot  * Rockchip DWC3 glue
30*fe75646aSEmmanuel Vadot  */
31*fe75646aSEmmanuel Vadot 
32*fe75646aSEmmanuel Vadot #include <sys/param.h>
33*fe75646aSEmmanuel Vadot #include <sys/systm.h>
34*fe75646aSEmmanuel Vadot #include <sys/bus.h>
35*fe75646aSEmmanuel Vadot #include <sys/rman.h>
36*fe75646aSEmmanuel Vadot #include <sys/kernel.h>
37*fe75646aSEmmanuel Vadot #include <sys/module.h>
38*fe75646aSEmmanuel Vadot #include <sys/gpio.h>
39*fe75646aSEmmanuel Vadot #include <machine/bus.h>
40*fe75646aSEmmanuel Vadot 
41*fe75646aSEmmanuel Vadot #include <dev/fdt/simplebus.h>
42*fe75646aSEmmanuel Vadot 
43*fe75646aSEmmanuel Vadot #include <dev/fdt/fdt_common.h>
44*fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
45*fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
46*fe75646aSEmmanuel Vadot #include <dev/ofw/ofw_subr.h>
47*fe75646aSEmmanuel Vadot 
48*fe75646aSEmmanuel Vadot #include <dev/extres/clk/clk.h>
49*fe75646aSEmmanuel Vadot #include <dev/extres/hwreset/hwreset.h>
50*fe75646aSEmmanuel Vadot #include <dev/extres/phy/phy_usb.h>
51*fe75646aSEmmanuel Vadot 
52*fe75646aSEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
53*fe75646aSEmmanuel Vadot 	{ "allwinner,sun50i-h6-dwc3",	1 },
54*fe75646aSEmmanuel Vadot 	{ NULL,				0 }
55*fe75646aSEmmanuel Vadot };
56*fe75646aSEmmanuel Vadot 
57*fe75646aSEmmanuel Vadot struct aw_dwc3_softc {
58*fe75646aSEmmanuel Vadot 	struct simplebus_softc	sc;
59*fe75646aSEmmanuel Vadot 	device_t		dev;
60*fe75646aSEmmanuel Vadot 	clk_t			clk_bus;
61*fe75646aSEmmanuel Vadot 	hwreset_t		rst_bus;
62*fe75646aSEmmanuel Vadot };
63*fe75646aSEmmanuel Vadot 
64*fe75646aSEmmanuel Vadot static int
65*fe75646aSEmmanuel Vadot aw_dwc3_probe(device_t dev)
66*fe75646aSEmmanuel Vadot {
67*fe75646aSEmmanuel Vadot 	phandle_t node;
68*fe75646aSEmmanuel Vadot 
69*fe75646aSEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
70*fe75646aSEmmanuel Vadot 		return (ENXIO);
71*fe75646aSEmmanuel Vadot 
72*fe75646aSEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
73*fe75646aSEmmanuel Vadot 		return (ENXIO);
74*fe75646aSEmmanuel Vadot 
75*fe75646aSEmmanuel Vadot 	/* Binding says that we need a child node for the actual dwc3 controller */
76*fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
77*fe75646aSEmmanuel Vadot 	if (OF_child(node) <= 0)
78*fe75646aSEmmanuel Vadot 		return (ENXIO);
79*fe75646aSEmmanuel Vadot 
80*fe75646aSEmmanuel Vadot 	device_set_desc(dev, "Allwinner H6 DWC3");
81*fe75646aSEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
82*fe75646aSEmmanuel Vadot }
83*fe75646aSEmmanuel Vadot 
84*fe75646aSEmmanuel Vadot static int
85*fe75646aSEmmanuel Vadot aw_dwc3_attach(device_t dev)
86*fe75646aSEmmanuel Vadot {
87*fe75646aSEmmanuel Vadot 	struct aw_dwc3_softc *sc;
88*fe75646aSEmmanuel Vadot 	device_t cdev;
89*fe75646aSEmmanuel Vadot 	phandle_t node, child;
90*fe75646aSEmmanuel Vadot 	int err;
91*fe75646aSEmmanuel Vadot 
92*fe75646aSEmmanuel Vadot 	sc = device_get_softc(dev);
93*fe75646aSEmmanuel Vadot 	sc->dev = dev;
94*fe75646aSEmmanuel Vadot 	node = ofw_bus_get_node(dev);
95*fe75646aSEmmanuel Vadot 
96*fe75646aSEmmanuel Vadot 	/* Enable the clocks */
97*fe75646aSEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "bus", &sc->clk_bus) != 0) {
98*fe75646aSEmmanuel Vadot 		device_printf(dev, "Cannot get bus clock\n");
99*fe75646aSEmmanuel Vadot 		return (ENXIO);
100*fe75646aSEmmanuel Vadot 	}
101*fe75646aSEmmanuel Vadot 	err = clk_enable(sc->clk_bus);
102*fe75646aSEmmanuel Vadot 	if (err != 0) {
103*fe75646aSEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
104*fe75646aSEmmanuel Vadot 		    clk_get_name(sc->clk_bus));
105*fe75646aSEmmanuel Vadot 		return (ENXIO);
106*fe75646aSEmmanuel Vadot 	}
107*fe75646aSEmmanuel Vadot 
108*fe75646aSEmmanuel Vadot 	/* Put module out of reset */
109*fe75646aSEmmanuel Vadot 	if (hwreset_get_by_ofw_name(dev, node, "bus", &sc->rst_bus) == 0) {
110*fe75646aSEmmanuel Vadot 		if (hwreset_deassert(sc->rst_bus) != 0) {
111*fe75646aSEmmanuel Vadot 			device_printf(dev, "Cannot deassert reset\n");
112*fe75646aSEmmanuel Vadot 			return (ENXIO);
113*fe75646aSEmmanuel Vadot 		}
114*fe75646aSEmmanuel Vadot 	}
115*fe75646aSEmmanuel Vadot 
116*fe75646aSEmmanuel Vadot 	simplebus_init(dev, node);
117*fe75646aSEmmanuel Vadot 	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
118*fe75646aSEmmanuel Vadot 		device_printf(dev, "could not get ranges\n");
119*fe75646aSEmmanuel Vadot 		return (ENXIO);
120*fe75646aSEmmanuel Vadot 	}
121*fe75646aSEmmanuel Vadot 
122*fe75646aSEmmanuel Vadot 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
123*fe75646aSEmmanuel Vadot 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
124*fe75646aSEmmanuel Vadot 		if (cdev != NULL)
125*fe75646aSEmmanuel Vadot 			device_probe_and_attach(cdev);
126*fe75646aSEmmanuel Vadot 	}
127*fe75646aSEmmanuel Vadot 
128*fe75646aSEmmanuel Vadot 	return (bus_generic_attach(dev));
129*fe75646aSEmmanuel Vadot }
130*fe75646aSEmmanuel Vadot 
131*fe75646aSEmmanuel Vadot static device_method_t aw_dwc3_methods[] = {
132*fe75646aSEmmanuel Vadot 	/* Device interface */
133*fe75646aSEmmanuel Vadot 	DEVMETHOD(device_probe,		aw_dwc3_probe),
134*fe75646aSEmmanuel Vadot 	DEVMETHOD(device_attach,	aw_dwc3_attach),
135*fe75646aSEmmanuel Vadot 
136*fe75646aSEmmanuel Vadot 	DEVMETHOD_END
137*fe75646aSEmmanuel Vadot };
138*fe75646aSEmmanuel Vadot 
139*fe75646aSEmmanuel Vadot DEFINE_CLASS_1(aw_dwc3, aw_dwc3_driver, aw_dwc3_methods,
140*fe75646aSEmmanuel Vadot     sizeof(struct aw_dwc3_softc), simplebus_driver);
141*fe75646aSEmmanuel Vadot DRIVER_MODULE(aw_dwc3, simplebus, aw_dwc3_driver, 0, 0);
142