xref: /freebsd/sys/dev/qcom_dwc3/qcom_dwc3.c (revision c4fde87a3236b500246d4fce76dbca2a3d64f73a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2021 Adrian Chadd <adrian@FreeBSD.Org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /*
29  * Qualcomm DWC3 glue
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/rman.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/gpio.h>
39 #include <machine/bus.h>
40 
41 #include <dev/fdt/simplebus.h>
42 
43 #include <dev/fdt/fdt_common.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofw_subr.h>
47 
48 #include <dev/clk/clk.h>
49 #include <dev/hwreset/hwreset.h>
50 #include <dev/phy/phy_usb.h>
51 #include <dev/syscon/syscon.h>
52 
53 static struct ofw_compat_data compat_data[] = {
54 	{ "qcom,dwc3",			1},
55 	{ NULL,				0 }
56 };
57 
58 struct qcom_dwc3_softc {
59 	device_t		dev;
60 	clk_t			clk_core;
61 	clk_t			clk_sleep;
62 	clk_t			clk_mock_utmi;
63 	int			type;
64 };
65 
66 static int
67 qcom_dwc3_probe(device_t dev)
68 {
69 	phandle_t node;
70 
71 	if (!ofw_bus_status_okay(dev))
72 		return (ENXIO);
73 
74 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
75 		return (ENXIO);
76 
77 	/* Binding says that we need a child node for the actual dwc3 controller */
78 	node = ofw_bus_get_node(dev);
79 	if (OF_child(node) <= 0)
80 		return (ENXIO);
81 
82 	device_set_desc(dev, "Qualcomm DWC3");
83 	return (BUS_PROBE_DEFAULT);
84 }
85 
86 static int
87 qcom_dwc3_attach(device_t dev)
88 {
89 	struct qcom_dwc3_softc *sc;
90 	device_t cdev;
91 	phandle_t node, child;
92 	int err;
93 
94 	sc = device_get_softc(dev);
95 	sc->dev = dev;
96 	node = ofw_bus_get_node(dev);
97 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
98 
99 	/* Mandatory clocks */
100 	if (clk_get_by_ofw_name(dev, 0, "core", &sc->clk_core) != 0) {
101 		device_printf(dev, "Cannot get core clock\n");
102 		return (ENXIO);
103 	}
104 
105 	if (clk_get_by_ofw_name(dev, 0, "sleep", &sc->clk_sleep) != 0) {
106 		device_printf(dev, "Cannot get sleep clock\n");
107 		return (ENXIO);
108 	}
109 
110 	if (clk_get_by_ofw_name(dev, 0, "mock_utmi", &sc->clk_mock_utmi) != 0) {
111 		device_printf(dev, "Cannot get mock_utmi clock\n");
112 		return (ENXIO);
113 	}
114 
115 	/*
116 	 * TODO: when we support optional reset blocks, take things
117 	 * out of reset (well, put them into reset, then take out of reset.)
118 	 */
119 
120 	/*
121 	 * Now, iterate over the clocks and enable them.
122 	 */
123 	err = clk_enable(sc->clk_core);
124 	if (err != 0) {
125 		device_printf(dev, "Could not enable clock %s\n",
126 		    clk_get_name(sc->clk_core));
127 		return (ENXIO);
128 	}
129 	err = clk_enable(sc->clk_sleep);
130 	if (err != 0) {
131 		device_printf(dev, "Could not enable clock %s\n",
132 		    clk_get_name(sc->clk_sleep));
133 		return (ENXIO);
134 	}
135 	err = clk_enable(sc->clk_mock_utmi);
136 	if (err != 0) {
137 		device_printf(dev, "Could not enable clock %s\n",
138 		    clk_get_name(sc->clk_mock_utmi));
139 		return (ENXIO);
140 	}
141 
142 	/*
143 	 * Rest is glue code.
144 	 */
145 
146 	err = simplebus_attach_impl(dev, 0, node);
147 	if (err != 0)
148 		return (ENXIO);
149 	bus_attach_children(dev);
150 	return (0);
151 }
152 
153 static device_method_t qcom_dwc3_methods[] = {
154 	/* Device interface */
155 	DEVMETHOD(device_probe,		qcom_dwc3_probe),
156 	DEVMETHOD(device_attach,	qcom_dwc3_attach),
157 	/* XXX TODO suspend */
158 	/* XXX TODO resume */
159 
160 	DEVMETHOD_END
161 };
162 
163 DEFINE_CLASS_1(qcom_dwc3, qcom_dwc3_driver, qcom_dwc3_methods,
164     sizeof(struct qcom_dwc3_softc), simplebus_driver);
165 DRIVER_MODULE(qcom_dwc3, simplebus, qcom_dwc3_driver, 0, 0);
166