aw_usbphy.c revision 309760
1/*-
2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: stable/11/sys/arm/allwinner/aw_usbphy.c 309760 2016-12-09 20:25:59Z manu $
27 */
28
29/*
30 * Allwinner USB PHY
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/aw_usbphy.c 309760 2016-12-09 20:25:59Z manu $");
35
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/bus.h>
39#include <sys/rman.h>
40#include <sys/kernel.h>
41#include <sys/module.h>
42#include <sys/gpio.h>
43
44#include <dev/ofw/ofw_bus.h>
45#include <dev/ofw/ofw_bus_subr.h>
46#include <dev/gpio/gpiobusvar.h>
47
48#include <dev/extres/clk/clk.h>
49#include <dev/extres/hwreset/hwreset.h>
50#include <dev/extres/regulator/regulator.h>
51#include <dev/extres/phy/phy.h>
52
53#include "phy_if.h"
54
55#define	USBPHY_NPHYS	4
56
57static struct ofw_compat_data compat_data[] = {
58	{ "allwinner,sun4i-a10-usb-phy",	1 },
59	{ "allwinner,sun5i-a13-usb-phy",	1 },
60	{ "allwinner,sun6i-a31-usb-phy",	1 },
61	{ "allwinner,sun7i-a20-usb-phy",	1 },
62	{ "allwinner,sun8i-a83t-usb-phy",	1 },
63	{ "allwinner,sun8i-h3-usb-phy",		1 },
64	{ NULL,					0 }
65};
66
67struct awusbphy_softc {
68	regulator_t		reg[USBPHY_NPHYS];
69	gpio_pin_t		id_det_pin;
70	int			id_det_valid;
71	gpio_pin_t		vbus_det_pin;
72	int			vbus_det_valid;
73};
74
75static int
76awusbphy_init(device_t dev)
77{
78	struct awusbphy_softc *sc;
79	phandle_t node;
80	char pname[20];
81	int error, off;
82	regulator_t reg;
83	hwreset_t rst;
84	clk_t clk;
85
86	sc = device_get_softc(dev);
87	node = ofw_bus_get_node(dev);
88
89	/* Enable clocks */
90	for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
91		error = clk_enable(clk);
92		if (error != 0) {
93			device_printf(dev, "couldn't enable clock %s\n",
94			    clk_get_name(clk));
95			return (error);
96		}
97	}
98
99	/* De-assert resets */
100	for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
101		error = hwreset_deassert(rst);
102		if (error != 0) {
103			device_printf(dev, "couldn't de-assert reset %d\n",
104			    off);
105			return (error);
106		}
107	}
108
109	/* Get regulators */
110	for (off = 0; off < USBPHY_NPHYS; off++) {
111		snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off);
112		if (regulator_get_by_ofw_property(dev, 0, pname, &reg) == 0)
113			sc->reg[off] = reg;
114	}
115
116	/* Get GPIOs */
117	error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios",
118	    &sc->id_det_pin);
119	if (error == 0)
120		sc->id_det_valid = 1;
121	error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios",
122	    &sc->vbus_det_pin);
123	if (error == 0)
124		sc->vbus_det_valid = 1;
125
126	return (0);
127}
128
129static int
130awusbphy_vbus_detect(device_t dev, int *val)
131{
132	struct awusbphy_softc *sc;
133	bool active;
134	int error;
135
136	sc = device_get_softc(dev);
137
138	if (sc->vbus_det_valid) {
139		error = gpio_pin_is_active(sc->vbus_det_pin, &active);
140		if (error != 0)
141			return (error);
142		*val = active;
143		return (0);
144	}
145
146	*val = 1;
147	return (0);
148}
149
150static int
151awusbphy_phy_enable(device_t dev, intptr_t phy, bool enable)
152{
153	struct awusbphy_softc *sc;
154	regulator_t reg;
155	int error, vbus_det;
156
157	if (phy < 0 || phy >= USBPHY_NPHYS)
158		return (ERANGE);
159
160	sc = device_get_softc(dev);
161
162	/* Regulators are optional. If not found, return success. */
163	reg = sc->reg[phy];
164	if (reg == NULL)
165		return (0);
166
167	if (enable) {
168		/* If an external vbus is detected, do not enable phy 0 */
169		if (phy == 0) {
170			error = awusbphy_vbus_detect(dev, &vbus_det);
171			if (error == 0 && vbus_det == 1)
172				return (0);
173		} else
174			error = 0;
175		if (error == 0)
176			error = regulator_enable(reg);
177	} else
178		error = regulator_disable(reg);
179	if (error != 0) {
180		device_printf(dev,
181		    "couldn't %s regulator for phy %jd\n",
182		    enable ? "enable" : "disable", (intmax_t)phy);
183		return (error);
184	}
185
186	return (0);
187}
188
189static int
190awusbphy_probe(device_t dev)
191{
192	if (!ofw_bus_status_okay(dev))
193		return (ENXIO);
194
195	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
196		return (ENXIO);
197
198	device_set_desc(dev, "Allwinner USB PHY");
199	return (BUS_PROBE_DEFAULT);
200}
201
202static int
203awusbphy_attach(device_t dev)
204{
205	int error;
206
207	error = awusbphy_init(dev);
208	if (error) {
209		device_printf(dev, "failed to initialize USB PHY, error %d\n",
210		    error);
211		return (error);
212	}
213
214	phy_register_provider(dev);
215
216	return (error);
217}
218
219static device_method_t awusbphy_methods[] = {
220	/* Device interface */
221	DEVMETHOD(device_probe,		awusbphy_probe),
222	DEVMETHOD(device_attach,	awusbphy_attach),
223
224	/* PHY interface */
225	DEVMETHOD(phy_enable,		awusbphy_phy_enable),
226
227	DEVMETHOD_END
228};
229
230static driver_t awusbphy_driver = {
231	"awusbphy",
232	awusbphy_methods,
233	sizeof(struct awusbphy_softc)
234};
235
236static devclass_t awusbphy_devclass;
237
238EARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass,
239    0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
240MODULE_VERSION(awusbphy, 1);
241