xref: /freebsd/sys/dev/qcom_dwc3/qcom_dwc3.c (revision dc1d78b2c2d293565017542006aeaed64a390196)
1777963afSAdrian Chadd /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3777963afSAdrian Chadd  *
4777963afSAdrian Chadd  * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.Org>
5777963afSAdrian Chadd  *
6777963afSAdrian Chadd  * Redistribution and use in source and binary forms, with or without
7777963afSAdrian Chadd  * modification, are permitted provided that the following conditions
8777963afSAdrian Chadd  * are met:
9777963afSAdrian Chadd  * 1. Redistributions of source code must retain the above copyright
10777963afSAdrian Chadd  *    notice, this list of conditions and the following disclaimer.
11777963afSAdrian Chadd  * 2. Redistributions in binary form must reproduce the above copyright
12777963afSAdrian Chadd  *    notice, this list of conditions and the following disclaimer in the
13777963afSAdrian Chadd  *    documentation and/or other materials provided with the distribution.
14777963afSAdrian Chadd  *
15777963afSAdrian Chadd  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16777963afSAdrian Chadd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17777963afSAdrian Chadd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18777963afSAdrian Chadd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19777963afSAdrian Chadd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20777963afSAdrian Chadd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21777963afSAdrian Chadd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22777963afSAdrian Chadd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23777963afSAdrian Chadd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24777963afSAdrian Chadd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25777963afSAdrian Chadd  * SUCH DAMAGE.
26777963afSAdrian Chadd  */
27777963afSAdrian Chadd 
28777963afSAdrian Chadd /*
29777963afSAdrian Chadd  * Qualcomm DWC3 glue
30777963afSAdrian Chadd  */
31777963afSAdrian Chadd 
32777963afSAdrian Chadd #include <sys/param.h>
33777963afSAdrian Chadd #include <sys/systm.h>
34777963afSAdrian Chadd #include <sys/bus.h>
35777963afSAdrian Chadd #include <sys/rman.h>
36777963afSAdrian Chadd #include <sys/kernel.h>
37777963afSAdrian Chadd #include <sys/module.h>
38777963afSAdrian Chadd #include <sys/gpio.h>
39777963afSAdrian Chadd #include <machine/bus.h>
40777963afSAdrian Chadd 
41777963afSAdrian Chadd #include <dev/fdt/simplebus.h>
42777963afSAdrian Chadd 
43777963afSAdrian Chadd #include <dev/fdt/fdt_common.h>
44777963afSAdrian Chadd #include <dev/ofw/ofw_bus.h>
45777963afSAdrian Chadd #include <dev/ofw/ofw_bus_subr.h>
46777963afSAdrian Chadd #include <dev/ofw/ofw_subr.h>
47777963afSAdrian Chadd 
48be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
491f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
50950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
5162e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
52777963afSAdrian Chadd 
53777963afSAdrian Chadd static struct ofw_compat_data compat_data[] = {
54777963afSAdrian Chadd 	{ "qcom,dwc3",			1},
55777963afSAdrian Chadd 	{ NULL,				0 }
56777963afSAdrian Chadd };
57777963afSAdrian Chadd 
58777963afSAdrian Chadd struct qcom_dwc3_softc {
59777963afSAdrian Chadd 	struct simplebus_softc	sc;
60777963afSAdrian Chadd 	device_t		dev;
61*dc1d78b2SAdrian Chadd 	clk_t			clk_core;
62777963afSAdrian Chadd 	clk_t			clk_sleep;
63777963afSAdrian Chadd 	clk_t			clk_mock_utmi;
64777963afSAdrian Chadd 	int			type;
65777963afSAdrian Chadd };
66777963afSAdrian Chadd 
67777963afSAdrian Chadd static int
qcom_dwc3_probe(device_t dev)68777963afSAdrian Chadd qcom_dwc3_probe(device_t dev)
69777963afSAdrian Chadd {
70777963afSAdrian Chadd 	phandle_t node;
71777963afSAdrian Chadd 
72777963afSAdrian Chadd 	if (!ofw_bus_status_okay(dev))
73777963afSAdrian Chadd 		return (ENXIO);
74777963afSAdrian Chadd 
75777963afSAdrian Chadd 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
76777963afSAdrian Chadd 		return (ENXIO);
77777963afSAdrian Chadd 
78777963afSAdrian Chadd 	/* Binding says that we need a child node for the actual dwc3 controller */
79777963afSAdrian Chadd 	node = ofw_bus_get_node(dev);
80777963afSAdrian Chadd 	if (OF_child(node) <= 0)
81777963afSAdrian Chadd 		return (ENXIO);
82777963afSAdrian Chadd 
83777963afSAdrian Chadd 	device_set_desc(dev, "Qualcomm DWC3");
84777963afSAdrian Chadd 	return (BUS_PROBE_DEFAULT);
85777963afSAdrian Chadd }
86777963afSAdrian Chadd 
87777963afSAdrian Chadd static int
qcom_dwc3_attach(device_t dev)88777963afSAdrian Chadd qcom_dwc3_attach(device_t dev)
89777963afSAdrian Chadd {
90777963afSAdrian Chadd 	struct qcom_dwc3_softc *sc;
91777963afSAdrian Chadd 	device_t cdev;
92777963afSAdrian Chadd 	phandle_t node, child;
93777963afSAdrian Chadd 	int err;
94777963afSAdrian Chadd 
95777963afSAdrian Chadd 	sc = device_get_softc(dev);
96777963afSAdrian Chadd 	sc->dev = dev;
97777963afSAdrian Chadd 	node = ofw_bus_get_node(dev);
98777963afSAdrian Chadd 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
99777963afSAdrian Chadd 
100777963afSAdrian Chadd 	/* Mandatory clocks */
101*dc1d78b2SAdrian Chadd 	if (clk_get_by_ofw_name(dev, 0, "core", &sc->clk_core) != 0) {
102*dc1d78b2SAdrian Chadd 		device_printf(dev, "Cannot get core clock\n");
103777963afSAdrian Chadd 		return (ENXIO);
104777963afSAdrian Chadd 	}
105777963afSAdrian Chadd 
106777963afSAdrian Chadd 	if (clk_get_by_ofw_name(dev, 0, "sleep", &sc->clk_sleep) != 0) {
107777963afSAdrian Chadd 		device_printf(dev, "Cannot get sleep clock\n");
108777963afSAdrian Chadd 		return (ENXIO);
109777963afSAdrian Chadd 	}
110777963afSAdrian Chadd 
111777963afSAdrian Chadd 	if (clk_get_by_ofw_name(dev, 0, "mock_utmi", &sc->clk_mock_utmi) != 0) {
112777963afSAdrian Chadd 		device_printf(dev, "Cannot get mock_utmi clock\n");
113777963afSAdrian Chadd 		return (ENXIO);
114777963afSAdrian Chadd 	}
115777963afSAdrian Chadd 
116777963afSAdrian Chadd 	/*
117777963afSAdrian Chadd 	 * TODO: when we support optional reset blocks, take things
118777963afSAdrian Chadd 	 * out of reset (well, put them into reset, then take out of reset.)
119777963afSAdrian Chadd 	 */
120777963afSAdrian Chadd 
121777963afSAdrian Chadd 	/*
122777963afSAdrian Chadd 	 * Now, iterate over the clocks and enable them.
123777963afSAdrian Chadd 	 */
124*dc1d78b2SAdrian Chadd 	err = clk_enable(sc->clk_core);
125777963afSAdrian Chadd 	if (err != 0) {
126777963afSAdrian Chadd 		device_printf(dev, "Could not enable clock %s\n",
127*dc1d78b2SAdrian Chadd 		    clk_get_name(sc->clk_core));
128777963afSAdrian Chadd 		return (ENXIO);
129777963afSAdrian Chadd 	}
130777963afSAdrian Chadd 	err = clk_enable(sc->clk_sleep);
131777963afSAdrian Chadd 	if (err != 0) {
132777963afSAdrian Chadd 		device_printf(dev, "Could not enable clock %s\n",
133777963afSAdrian Chadd 		    clk_get_name(sc->clk_sleep));
134777963afSAdrian Chadd 		return (ENXIO);
135777963afSAdrian Chadd 	}
136777963afSAdrian Chadd 	err = clk_enable(sc->clk_mock_utmi);
137777963afSAdrian Chadd 	if (err != 0) {
138777963afSAdrian Chadd 		device_printf(dev, "Could not enable clock %s\n",
139777963afSAdrian Chadd 		    clk_get_name(sc->clk_mock_utmi));
140777963afSAdrian Chadd 		return (ENXIO);
141777963afSAdrian Chadd 	}
142777963afSAdrian Chadd 
143777963afSAdrian Chadd 	/*
144777963afSAdrian Chadd 	 * Rest is glue code.
145777963afSAdrian Chadd 	 */
146777963afSAdrian Chadd 
147777963afSAdrian Chadd 	simplebus_init(dev, node);
148777963afSAdrian Chadd 	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
149777963afSAdrian Chadd 		device_printf(dev, "could not get ranges\n");
150777963afSAdrian Chadd 		return (ENXIO);
151777963afSAdrian Chadd 	}
152777963afSAdrian Chadd 
153777963afSAdrian Chadd 	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
154777963afSAdrian Chadd 		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
155777963afSAdrian Chadd 		if (cdev != NULL)
156777963afSAdrian Chadd 			device_probe_and_attach(cdev);
157777963afSAdrian Chadd 	}
158777963afSAdrian Chadd 
15918250ec6SJohn Baldwin 	bus_attach_children(dev);
16018250ec6SJohn Baldwin 	return (0);
161777963afSAdrian Chadd }
162777963afSAdrian Chadd 
163777963afSAdrian Chadd static device_method_t qcom_dwc3_methods[] = {
164777963afSAdrian Chadd 	/* Device interface */
165777963afSAdrian Chadd 	DEVMETHOD(device_probe,		qcom_dwc3_probe),
166777963afSAdrian Chadd 	DEVMETHOD(device_attach,	qcom_dwc3_attach),
167777963afSAdrian Chadd 	/* XXX TODO suspend */
168777963afSAdrian Chadd 	/* XXX TODO resume */
169777963afSAdrian Chadd 
170777963afSAdrian Chadd 	DEVMETHOD_END
171777963afSAdrian Chadd };
172777963afSAdrian Chadd 
173777963afSAdrian Chadd DEFINE_CLASS_1(qcom_dwc3, qcom_dwc3_driver, qcom_dwc3_methods,
174777963afSAdrian Chadd     sizeof(struct qcom_dwc3_softc), simplebus_driver);
17553e1cbefSJohn Baldwin DRIVER_MODULE(qcom_dwc3, simplebus, qcom_dwc3_driver, 0, 0);
176