aw_usbphy.c revision 309760
1296284Sjmcneill/*-
2296284Sjmcneill * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3296284Sjmcneill * All rights reserved.
4296284Sjmcneill *
5296284Sjmcneill * Redistribution and use in source and binary forms, with or without
6296284Sjmcneill * modification, are permitted provided that the following conditions
7296284Sjmcneill * are met:
8296284Sjmcneill * 1. Redistributions of source code must retain the above copyright
9296284Sjmcneill *    notice, this list of conditions and the following disclaimer.
10296284Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
11296284Sjmcneill *    notice, this list of conditions and the following disclaimer in the
12296284Sjmcneill *    documentation and/or other materials provided with the distribution.
13296284Sjmcneill *
14296284Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15296284Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16296284Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17296284Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18296284Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19296284Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20296284Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
21296284Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22296284Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23296284Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24296284Sjmcneill * SUCH DAMAGE.
25296284Sjmcneill *
26296284Sjmcneill * $FreeBSD: stable/11/sys/arm/allwinner/aw_usbphy.c 309760 2016-12-09 20:25:59Z manu $
27296284Sjmcneill */
28296284Sjmcneill
29296284Sjmcneill/*
30296284Sjmcneill * Allwinner USB PHY
31296284Sjmcneill */
32296284Sjmcneill
33296284Sjmcneill#include <sys/cdefs.h>
34296284Sjmcneill__FBSDID("$FreeBSD: stable/11/sys/arm/allwinner/aw_usbphy.c 309760 2016-12-09 20:25:59Z manu $");
35296284Sjmcneill
36296284Sjmcneill#include <sys/param.h>
37296284Sjmcneill#include <sys/systm.h>
38296284Sjmcneill#include <sys/bus.h>
39296284Sjmcneill#include <sys/rman.h>
40296284Sjmcneill#include <sys/kernel.h>
41296284Sjmcneill#include <sys/module.h>
42296284Sjmcneill#include <sys/gpio.h>
43296284Sjmcneill
44296284Sjmcneill#include <dev/ofw/ofw_bus.h>
45296284Sjmcneill#include <dev/ofw/ofw_bus_subr.h>
46300728Sjmcneill#include <dev/gpio/gpiobusvar.h>
47296284Sjmcneill
48297627Sjmcneill#include <dev/extres/clk/clk.h>
49297627Sjmcneill#include <dev/extres/hwreset/hwreset.h>
50297627Sjmcneill#include <dev/extres/regulator/regulator.h>
51300728Sjmcneill#include <dev/extres/phy/phy.h>
52296284Sjmcneill
53300728Sjmcneill#include "phy_if.h"
54296284Sjmcneill
55300728Sjmcneill#define	USBPHY_NPHYS	4
56300728Sjmcneill
57296284Sjmcneillstatic struct ofw_compat_data compat_data[] = {
58296284Sjmcneill	{ "allwinner,sun4i-a10-usb-phy",	1 },
59296284Sjmcneill	{ "allwinner,sun5i-a13-usb-phy",	1 },
60296284Sjmcneill	{ "allwinner,sun6i-a31-usb-phy",	1 },
61296284Sjmcneill	{ "allwinner,sun7i-a20-usb-phy",	1 },
62299113Sjmcneill	{ "allwinner,sun8i-a83t-usb-phy",	1 },
63299688Smanu	{ "allwinner,sun8i-h3-usb-phy",		1 },
64296284Sjmcneill	{ NULL,					0 }
65296284Sjmcneill};
66296284Sjmcneill
67300728Sjmcneillstruct awusbphy_softc {
68300728Sjmcneill	regulator_t		reg[USBPHY_NPHYS];
69300728Sjmcneill	gpio_pin_t		id_det_pin;
70300728Sjmcneill	int			id_det_valid;
71300728Sjmcneill	gpio_pin_t		vbus_det_pin;
72300728Sjmcneill	int			vbus_det_valid;
73300728Sjmcneill};
74300728Sjmcneill
75296284Sjmcneillstatic int
76296284Sjmcneillawusbphy_init(device_t dev)
77296284Sjmcneill{
78300728Sjmcneill	struct awusbphy_softc *sc;
79300728Sjmcneill	phandle_t node;
80296284Sjmcneill	char pname[20];
81296284Sjmcneill	int error, off;
82297627Sjmcneill	regulator_t reg;
83297627Sjmcneill	hwreset_t rst;
84297627Sjmcneill	clk_t clk;
85296284Sjmcneill
86300728Sjmcneill	sc = device_get_softc(dev);
87300728Sjmcneill	node = ofw_bus_get_node(dev);
88300728Sjmcneill
89297627Sjmcneill	/* Enable clocks */
90308324Smmel	for (off = 0; clk_get_by_ofw_index(dev, 0, off, &clk) == 0; off++) {
91297627Sjmcneill		error = clk_enable(clk);
92297627Sjmcneill		if (error != 0) {
93297627Sjmcneill			device_printf(dev, "couldn't enable clock %s\n",
94297627Sjmcneill			    clk_get_name(clk));
95296284Sjmcneill			return (error);
96297627Sjmcneill		}
97297627Sjmcneill	}
98296284Sjmcneill
99297627Sjmcneill	/* De-assert resets */
100308324Smmel	for (off = 0; hwreset_get_by_ofw_idx(dev, 0, off, &rst) == 0; off++) {
101297627Sjmcneill		error = hwreset_deassert(rst);
102297627Sjmcneill		if (error != 0) {
103297627Sjmcneill			device_printf(dev, "couldn't de-assert reset %d\n",
104297627Sjmcneill			    off);
105296284Sjmcneill			return (error);
106297627Sjmcneill		}
107297627Sjmcneill	}
108296284Sjmcneill
109300728Sjmcneill	/* Get regulators */
110300728Sjmcneill	for (off = 0; off < USBPHY_NPHYS; off++) {
111296284Sjmcneill		snprintf(pname, sizeof(pname), "usb%d_vbus-supply", off);
112308324Smmel		if (regulator_get_by_ofw_property(dev, 0, pname, &reg) == 0)
113300728Sjmcneill			sc->reg[off] = reg;
114300728Sjmcneill	}
115300728Sjmcneill
116300728Sjmcneill	/* Get GPIOs */
117300728Sjmcneill	error = gpio_pin_get_by_ofw_property(dev, node, "usb0_id_det-gpios",
118300728Sjmcneill	    &sc->id_det_pin);
119300728Sjmcneill	if (error == 0)
120300728Sjmcneill		sc->id_det_valid = 1;
121300728Sjmcneill	error = gpio_pin_get_by_ofw_property(dev, node, "usb0_vbus_det-gpios",
122300728Sjmcneill	    &sc->vbus_det_pin);
123300728Sjmcneill	if (error == 0)
124300728Sjmcneill		sc->vbus_det_valid = 1;
125300728Sjmcneill
126300728Sjmcneill	return (0);
127300728Sjmcneill}
128300728Sjmcneill
129300728Sjmcneillstatic int
130300728Sjmcneillawusbphy_vbus_detect(device_t dev, int *val)
131300728Sjmcneill{
132300728Sjmcneill	struct awusbphy_softc *sc;
133300728Sjmcneill	bool active;
134300728Sjmcneill	int error;
135300728Sjmcneill
136300728Sjmcneill	sc = device_get_softc(dev);
137300728Sjmcneill
138300728Sjmcneill	if (sc->vbus_det_valid) {
139300728Sjmcneill		error = gpio_pin_is_active(sc->vbus_det_pin, &active);
140300728Sjmcneill		if (error != 0)
141296284Sjmcneill			return (error);
142300728Sjmcneill		*val = active;
143300728Sjmcneill		return (0);
144296284Sjmcneill	}
145296284Sjmcneill
146300728Sjmcneill	*val = 1;
147296284Sjmcneill	return (0);
148296284Sjmcneill}
149296284Sjmcneill
150296284Sjmcneillstatic int
151309760Smanuawusbphy_phy_enable(device_t dev, intptr_t phy, bool enable)
152300728Sjmcneill{
153300728Sjmcneill	struct awusbphy_softc *sc;
154300728Sjmcneill	regulator_t reg;
155300728Sjmcneill	int error, vbus_det;
156300728Sjmcneill
157300728Sjmcneill	if (phy < 0 || phy >= USBPHY_NPHYS)
158300728Sjmcneill		return (ERANGE);
159300728Sjmcneill
160300728Sjmcneill	sc = device_get_softc(dev);
161300728Sjmcneill
162300728Sjmcneill	/* Regulators are optional. If not found, return success. */
163300728Sjmcneill	reg = sc->reg[phy];
164300728Sjmcneill	if (reg == NULL)
165300728Sjmcneill		return (0);
166300728Sjmcneill
167300728Sjmcneill	if (enable) {
168300728Sjmcneill		/* If an external vbus is detected, do not enable phy 0 */
169300728Sjmcneill		if (phy == 0) {
170300728Sjmcneill			error = awusbphy_vbus_detect(dev, &vbus_det);
171300728Sjmcneill			if (error == 0 && vbus_det == 1)
172300728Sjmcneill				return (0);
173300728Sjmcneill		} else
174300728Sjmcneill			error = 0;
175300728Sjmcneill		if (error == 0)
176300728Sjmcneill			error = regulator_enable(reg);
177300728Sjmcneill	} else
178300728Sjmcneill		error = regulator_disable(reg);
179300728Sjmcneill	if (error != 0) {
180309760Smanu		device_printf(dev,
181309760Smanu		    "couldn't %s regulator for phy %jd\n",
182309760Smanu		    enable ? "enable" : "disable", (intmax_t)phy);
183300728Sjmcneill		return (error);
184300728Sjmcneill	}
185300728Sjmcneill
186300728Sjmcneill	return (0);
187300728Sjmcneill}
188300728Sjmcneill
189300728Sjmcneillstatic int
190296284Sjmcneillawusbphy_probe(device_t dev)
191296284Sjmcneill{
192296284Sjmcneill	if (!ofw_bus_status_okay(dev))
193296284Sjmcneill		return (ENXIO);
194296284Sjmcneill
195296284Sjmcneill	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
196296284Sjmcneill		return (ENXIO);
197296284Sjmcneill
198296284Sjmcneill	device_set_desc(dev, "Allwinner USB PHY");
199296284Sjmcneill	return (BUS_PROBE_DEFAULT);
200296284Sjmcneill}
201296284Sjmcneill
202296284Sjmcneillstatic int
203296284Sjmcneillawusbphy_attach(device_t dev)
204296284Sjmcneill{
205296284Sjmcneill	int error;
206296284Sjmcneill
207296284Sjmcneill	error = awusbphy_init(dev);
208300728Sjmcneill	if (error) {
209296284Sjmcneill		device_printf(dev, "failed to initialize USB PHY, error %d\n",
210296284Sjmcneill		    error);
211300728Sjmcneill		return (error);
212300728Sjmcneill	}
213296284Sjmcneill
214300728Sjmcneill	phy_register_provider(dev);
215300728Sjmcneill
216296284Sjmcneill	return (error);
217296284Sjmcneill}
218296284Sjmcneill
219296284Sjmcneillstatic device_method_t awusbphy_methods[] = {
220296284Sjmcneill	/* Device interface */
221296284Sjmcneill	DEVMETHOD(device_probe,		awusbphy_probe),
222296284Sjmcneill	DEVMETHOD(device_attach,	awusbphy_attach),
223296284Sjmcneill
224300728Sjmcneill	/* PHY interface */
225300728Sjmcneill	DEVMETHOD(phy_enable,		awusbphy_phy_enable),
226300728Sjmcneill
227296284Sjmcneill	DEVMETHOD_END
228296284Sjmcneill};
229296284Sjmcneill
230296284Sjmcneillstatic driver_t awusbphy_driver = {
231296284Sjmcneill	"awusbphy",
232296284Sjmcneill	awusbphy_methods,
233300728Sjmcneill	sizeof(struct awusbphy_softc)
234296284Sjmcneill};
235296284Sjmcneill
236296284Sjmcneillstatic devclass_t awusbphy_devclass;
237296284Sjmcneill
238300728SjmcneillEARLY_DRIVER_MODULE(awusbphy, simplebus, awusbphy_driver, awusbphy_devclass,
239300728Sjmcneill    0, 0, BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);
240296284SjmcneillMODULE_VERSION(awusbphy, 1);
241