1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2019 Emmanuel Vadot <manu@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 * Rockchip DWC3 glue
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD$");
34
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38#include <sys/rman.h>
39#include <sys/kernel.h>
40#include <sys/module.h>
41#include <sys/gpio.h>
42#include <machine/bus.h>
43
44#include <dev/fdt/simplebus.h>
45
46#include <dev/fdt/fdt_common.h>
47#include <dev/ofw/ofw_bus.h>
48#include <dev/ofw/ofw_bus_subr.h>
49#include <dev/ofw/ofw_subr.h>
50
51#include <dev/extres/clk/clk.h>
52#include <dev/extres/hwreset/hwreset.h>
53#include <dev/extres/phy/phy_usb.h>
54
55static struct ofw_compat_data compat_data[] = {
56	{ "allwinner,sun50i-h6-dwc3",	1 },
57	{ NULL,				0 }
58};
59
60struct aw_dwc3_softc {
61	struct simplebus_softc	sc;
62	device_t		dev;
63	clk_t			clk_bus;
64	hwreset_t		rst_bus;
65};
66
67static int
68aw_dwc3_probe(device_t dev)
69{
70	phandle_t node;
71
72	if (!ofw_bus_status_okay(dev))
73		return (ENXIO);
74
75	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
76		return (ENXIO);
77
78	/* Binding says that we need a child node for the actual dwc3 controller */
79	node = ofw_bus_get_node(dev);
80	if (OF_child(node) <= 0)
81		return (ENXIO);
82
83	device_set_desc(dev, "Allwinner H6 DWC3");
84	return (BUS_PROBE_DEFAULT);
85}
86
87static int
88aw_dwc3_attach(device_t dev)
89{
90	struct aw_dwc3_softc *sc;
91	device_t cdev;
92	phandle_t node, child;
93	int err;
94
95	sc = device_get_softc(dev);
96	sc->dev = dev;
97	node = ofw_bus_get_node(dev);
98
99	/* Enable the clocks */
100	if (clk_get_by_ofw_name(dev, 0, "bus", &sc->clk_bus) != 0) {
101		device_printf(dev, "Cannot get bus clock\n");
102		return (ENXIO);
103	}
104	err = clk_enable(sc->clk_bus);
105	if (err != 0) {
106		device_printf(dev, "Could not enable clock %s\n",
107		    clk_get_name(sc->clk_bus));
108		return (ENXIO);
109	}
110
111	/* Put module out of reset */
112	if (hwreset_get_by_ofw_name(dev, node, "bus", &sc->rst_bus) == 0) {
113		if (hwreset_deassert(sc->rst_bus) != 0) {
114			device_printf(dev, "Cannot deassert reset\n");
115			return (ENXIO);
116		}
117	}
118
119	simplebus_init(dev, node);
120	if (simplebus_fill_ranges(node, &sc->sc) < 0) {
121		device_printf(dev, "could not get ranges\n");
122		return (ENXIO);
123	}
124
125	for (child = OF_child(node); child > 0; child = OF_peer(child)) {
126		cdev = simplebus_add_device(dev, child, 0, NULL, -1, NULL);
127		if (cdev != NULL)
128			device_probe_and_attach(cdev);
129	}
130
131	return (bus_generic_attach(dev));
132}
133
134static device_method_t aw_dwc3_methods[] = {
135	/* Device interface */
136	DEVMETHOD(device_probe,		aw_dwc3_probe),
137	DEVMETHOD(device_attach,	aw_dwc3_attach),
138
139	DEVMETHOD_END
140};
141
142static devclass_t aw_dwc3_devclass;
143
144DEFINE_CLASS_1(aw_dwc3, aw_dwc3_driver, aw_dwc3_methods,
145    sizeof(struct aw_dwc3_softc), simplebus_driver);
146DRIVER_MODULE(aw_dwc3, simplebus, aw_dwc3_driver, aw_dwc3_devclass, 0, 0);
147