xref: /netbsd-src/sys/arch/arm/amlogic/meson_usbphy.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: meson_usbphy.c,v 1.6 2021/01/27 03:10:18 thorpej Exp $ */
2912cfa14Sjmcneill 
3912cfa14Sjmcneill /*-
4912cfa14Sjmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5912cfa14Sjmcneill  * All rights reserved.
6912cfa14Sjmcneill  *
7912cfa14Sjmcneill  * Redistribution and use in source and binary forms, with or without
8912cfa14Sjmcneill  * modification, are permitted provided that the following conditions
9912cfa14Sjmcneill  * are met:
10912cfa14Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12912cfa14Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13912cfa14Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14912cfa14Sjmcneill  *    documentation and/or other materials provided with the distribution.
15912cfa14Sjmcneill  *
16912cfa14Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17912cfa14Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18912cfa14Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19912cfa14Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20912cfa14Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21912cfa14Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22912cfa14Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23912cfa14Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24912cfa14Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25912cfa14Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26912cfa14Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
27912cfa14Sjmcneill  */
28912cfa14Sjmcneill 
29912cfa14Sjmcneill #include <sys/cdefs.h>
30912cfa14Sjmcneill 
31*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: meson_usbphy.c,v 1.6 2021/01/27 03:10:18 thorpej Exp $");
32912cfa14Sjmcneill 
33912cfa14Sjmcneill #include <sys/param.h>
34912cfa14Sjmcneill #include <sys/bus.h>
35912cfa14Sjmcneill #include <sys/device.h>
36912cfa14Sjmcneill #include <sys/intr.h>
37912cfa14Sjmcneill #include <sys/systm.h>
38912cfa14Sjmcneill #include <sys/time.h>
39912cfa14Sjmcneill 
40912cfa14Sjmcneill #include <dev/fdt/fdtvar.h>
41912cfa14Sjmcneill 
42912cfa14Sjmcneill #define	CBUS_REG(x)			((x) << 2)
43912cfa14Sjmcneill #define	PREI_USB_PHY_CFG_REG		CBUS_REG(0x00)
44912cfa14Sjmcneill #define	 PREI_USB_PHY_CFG_CLK_32K_ALT_SEL	__BIT(15)
45912cfa14Sjmcneill #define	PREI_USB_PHY_CTRL_REG		CBUS_REG(0x01)
46912cfa14Sjmcneill #define	 PREI_USB_PHY_CTRL_FSEL		__BITS(24,22)
47912cfa14Sjmcneill #define	 PREI_USB_PHY_CTRL_FSEL_24M	5
48912cfa14Sjmcneill #define	 PREI_USB_PHY_CTRL_FSEL_12M	2
49912cfa14Sjmcneill #define	 PREI_USB_PHY_CTRL_POR		__BIT(15)
50912cfa14Sjmcneill #define	 PREI_USB_PHY_CTRL_CLK_DET	__BIT(8)
51912cfa14Sjmcneill #define	PREI_USB_PHY_ADP_BC_REG		CBUS_REG(0x03)
52912cfa14Sjmcneill #define	 PREI_USB_PHY_ADP_BC_ACA_FLOATING __BIT(26)
53912cfa14Sjmcneill #define	 PREI_USB_PHY_ADP_BC_ACA_ENABLE	__BIT(16)
54912cfa14Sjmcneill 
55912cfa14Sjmcneill static int meson_usbphy_match(device_t, cfdata_t, void *);
56912cfa14Sjmcneill static void meson_usbphy_attach(device_t, device_t, void *);
57912cfa14Sjmcneill 
58912cfa14Sjmcneill enum meson_usbphy_type {
59912cfa14Sjmcneill 	USBPHY_MESON8B,
60912cfa14Sjmcneill };
61912cfa14Sjmcneill 
62646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
63646c0f59Sthorpej 	{ .compat = "amlogic,meson8b-usb2-phy",
64646c0f59Sthorpej 	  .value = USBPHY_MESON8B },
65646c0f59Sthorpej 	{ .compat = "amlogic,meson-gxbb-usb2-phy",
66646c0f59Sthorpej 	  .value = USBPHY_MESON8B },
67646c0f59Sthorpej 
682dcdd1cdSthorpej 	DEVICE_COMPAT_EOL
69912cfa14Sjmcneill };
70912cfa14Sjmcneill 
71912cfa14Sjmcneill struct meson_usbphy_softc {
72912cfa14Sjmcneill 	device_t		sc_dev;
73912cfa14Sjmcneill 	bus_space_tag_t		sc_bst;
74912cfa14Sjmcneill 	bus_space_handle_t	sc_bsh;
75912cfa14Sjmcneill 	int			sc_phandle;
76912cfa14Sjmcneill 	const char		*sc_dr_mode;
77912cfa14Sjmcneill 	enum meson_usbphy_type	sc_type;
78912cfa14Sjmcneill };
79912cfa14Sjmcneill 
80912cfa14Sjmcneill #define	PHY_READ(sc, reg)				\
81912cfa14Sjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
82912cfa14Sjmcneill #define	PHY_WRITE(sc, reg, val)			\
83912cfa14Sjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
84912cfa14Sjmcneill 
85912cfa14Sjmcneill CFATTACH_DECL_NEW(meson_usbphy, sizeof(struct meson_usbphy_softc),
86912cfa14Sjmcneill 	meson_usbphy_match, meson_usbphy_attach, NULL, NULL);
87912cfa14Sjmcneill 
88912cfa14Sjmcneill static const char *
meson_usbphy_dr_mode(struct meson_usbphy_softc * sc)89912cfa14Sjmcneill meson_usbphy_dr_mode(struct meson_usbphy_softc *sc)
90912cfa14Sjmcneill {
91912cfa14Sjmcneill 	int index, phandle;
92912cfa14Sjmcneill 
93912cfa14Sjmcneill 	index = 0;
94912cfa14Sjmcneill 	while ((phandle = fdt_find_with_property("phys", &index)) != -1) {
95912cfa14Sjmcneill 		const int phy_phandle = fdtbus_get_phandle(phandle, "phys");
96912cfa14Sjmcneill 		if (phy_phandle != sc->sc_phandle)
97912cfa14Sjmcneill 			continue;
98912cfa14Sjmcneill 		return fdtbus_get_string(phandle, "dr_mode");
99912cfa14Sjmcneill 	}
100912cfa14Sjmcneill 
101912cfa14Sjmcneill 	return NULL;
102912cfa14Sjmcneill }
103912cfa14Sjmcneill 
104912cfa14Sjmcneill static void *
meson_usbphy_acquire(device_t dev,const void * data,size_t len)105912cfa14Sjmcneill meson_usbphy_acquire(device_t dev, const void *data, size_t len)
106912cfa14Sjmcneill {
107912cfa14Sjmcneill 	if (len != 0)
108912cfa14Sjmcneill 		return NULL;
109912cfa14Sjmcneill 
110912cfa14Sjmcneill 	return (void *)(uintptr_t)1;
111912cfa14Sjmcneill }
112912cfa14Sjmcneill 
113912cfa14Sjmcneill static void
meson_usbphy_release(device_t dev,void * priv)114912cfa14Sjmcneill meson_usbphy_release(device_t dev, void *priv)
115912cfa14Sjmcneill {
116912cfa14Sjmcneill }
117912cfa14Sjmcneill 
118912cfa14Sjmcneill static int
meson_usbphy_enable(device_t dev,void * priv,bool enable)119912cfa14Sjmcneill meson_usbphy_enable(device_t dev, void *priv, bool enable)
120912cfa14Sjmcneill {
121912cfa14Sjmcneill 	struct meson_usbphy_softc * const sc = device_private(dev);
1227e38c880Sjmcneill 	struct fdtbus_regulator *reg;
123912cfa14Sjmcneill 	uint32_t val;
1247e38c880Sjmcneill 	int error;
125912cfa14Sjmcneill 
126912cfa14Sjmcneill 	if (enable) {
1277e38c880Sjmcneill 		if (of_hasprop(sc->sc_phandle, "phy-supply")) {
1287e38c880Sjmcneill 			reg = fdtbus_regulator_acquire(sc->sc_phandle, "phy-supply");
1297e38c880Sjmcneill 			if (reg != NULL) {
1307e38c880Sjmcneill 				error = fdtbus_regulator_enable(reg);
1317e38c880Sjmcneill 				if (error != 0)
1327e38c880Sjmcneill 					device_printf(dev, "WARNING: couldn't enable phy-supply: %d\n", error);
1337e38c880Sjmcneill 			} else {
1347e38c880Sjmcneill 				device_printf(dev, "WARNING: couldn't acquire phy-supply\n");
1357e38c880Sjmcneill 			}
1367e38c880Sjmcneill 		}
1377e38c880Sjmcneill 
138912cfa14Sjmcneill 		delay(1000);
139912cfa14Sjmcneill 
140912cfa14Sjmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CFG_REG);
141912cfa14Sjmcneill 		val |= PREI_USB_PHY_CFG_CLK_32K_ALT_SEL;
142912cfa14Sjmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CFG_REG, val);
143912cfa14Sjmcneill 
144912cfa14Sjmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
145912cfa14Sjmcneill 		val &= ~PREI_USB_PHY_CTRL_FSEL;
146912cfa14Sjmcneill 		val |= __SHIFTIN(PREI_USB_PHY_CTRL_FSEL_24M,
147912cfa14Sjmcneill 				 PREI_USB_PHY_CTRL_FSEL);
148912cfa14Sjmcneill 		val |= PREI_USB_PHY_CTRL_POR;
149912cfa14Sjmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
150912cfa14Sjmcneill 
151912cfa14Sjmcneill 		delay(1000);
152912cfa14Sjmcneill 
153912cfa14Sjmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
154912cfa14Sjmcneill 		val &= ~PREI_USB_PHY_CTRL_POR;
155912cfa14Sjmcneill 		PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
156912cfa14Sjmcneill 
157912cfa14Sjmcneill 		delay(50000);
158912cfa14Sjmcneill 
159912cfa14Sjmcneill 		val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
160912cfa14Sjmcneill 		if ((val & PREI_USB_PHY_CTRL_CLK_DET) == 0)
161912cfa14Sjmcneill 			aprint_error_dev(dev, "WARNING: USB PHY clock not detected\n");
162912cfa14Sjmcneill 
163912cfa14Sjmcneill 		if (sc->sc_dr_mode && strcmp(sc->sc_dr_mode, "host") == 0) {
164912cfa14Sjmcneill 			val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
165912cfa14Sjmcneill 			val |= PREI_USB_PHY_ADP_BC_ACA_ENABLE;
166912cfa14Sjmcneill 			PHY_WRITE(sc, PREI_USB_PHY_ADP_BC_REG, val);
167912cfa14Sjmcneill 
168912cfa14Sjmcneill 			delay(1000);
169912cfa14Sjmcneill 
170912cfa14Sjmcneill 			val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
171912cfa14Sjmcneill 			if ((val & PREI_USB_PHY_ADP_BC_ACA_FLOATING) != 0)
172912cfa14Sjmcneill 				aprint_error_dev(dev, "WARNING: USB PHY failed to enable ACA detection\n");
173912cfa14Sjmcneill 		}
174912cfa14Sjmcneill 	}
175912cfa14Sjmcneill 
176912cfa14Sjmcneill 	return 0;
177912cfa14Sjmcneill }
178912cfa14Sjmcneill 
179912cfa14Sjmcneill const struct fdtbus_phy_controller_func meson_usbphy_funcs = {
180912cfa14Sjmcneill 	.acquire = meson_usbphy_acquire,
181912cfa14Sjmcneill 	.release = meson_usbphy_release,
182912cfa14Sjmcneill 	.enable = meson_usbphy_enable,
183912cfa14Sjmcneill };
184912cfa14Sjmcneill 
185912cfa14Sjmcneill static int
meson_usbphy_match(device_t parent,cfdata_t cf,void * aux)186912cfa14Sjmcneill meson_usbphy_match(device_t parent, cfdata_t cf, void *aux)
187912cfa14Sjmcneill {
188912cfa14Sjmcneill 	struct fdt_attach_args * const faa = aux;
189912cfa14Sjmcneill 
190*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
191912cfa14Sjmcneill }
192912cfa14Sjmcneill 
193912cfa14Sjmcneill static void
meson_usbphy_attach(device_t parent,device_t self,void * aux)194912cfa14Sjmcneill meson_usbphy_attach(device_t parent, device_t self, void *aux)
195912cfa14Sjmcneill {
196912cfa14Sjmcneill 	struct meson_usbphy_softc * const sc = device_private(self);
197912cfa14Sjmcneill 	struct fdt_attach_args * const faa = aux;
198912cfa14Sjmcneill 	const int phandle = faa->faa_phandle;
199912cfa14Sjmcneill 	struct fdtbus_reset *rst;
200912cfa14Sjmcneill 	struct clk *clk;
201912cfa14Sjmcneill 	bus_addr_t addr;
202912cfa14Sjmcneill 	bus_size_t size;
203912cfa14Sjmcneill 	u_int n;
204912cfa14Sjmcneill 
205912cfa14Sjmcneill 	sc->sc_dev = self;
206912cfa14Sjmcneill 	sc->sc_bst = faa->faa_bst;
207912cfa14Sjmcneill 	sc->sc_phandle = phandle;
208*6e54367aSthorpej 	sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
209912cfa14Sjmcneill 
210912cfa14Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
211912cfa14Sjmcneill 		aprint_error(": couldn't get registers\n");
212912cfa14Sjmcneill 		return;
213912cfa14Sjmcneill 	}
214912cfa14Sjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
215912cfa14Sjmcneill 		aprint_error(": couldn't map registers\n");
216912cfa14Sjmcneill 		return;
217912cfa14Sjmcneill 	}
218912cfa14Sjmcneill 
219912cfa14Sjmcneill 	/* Enable clocks */
220912cfa14Sjmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
221912cfa14Sjmcneill 		if (clk_enable(clk) != 0) {
222912cfa14Sjmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
223912cfa14Sjmcneill 			return;
224912cfa14Sjmcneill 		}
225912cfa14Sjmcneill 	/* De-assert resets */
226912cfa14Sjmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
227912cfa14Sjmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
228912cfa14Sjmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
229912cfa14Sjmcneill 			return;
230912cfa14Sjmcneill 		}
231912cfa14Sjmcneill 
232912cfa14Sjmcneill 	sc->sc_dr_mode = meson_usbphy_dr_mode(sc);
233912cfa14Sjmcneill 
234912cfa14Sjmcneill 	aprint_naive("\n");
235912cfa14Sjmcneill 	aprint_normal(": USB2 PHY (%s)\n", sc->sc_dr_mode ? sc->sc_dr_mode : "unknown mode");
236912cfa14Sjmcneill 
237912cfa14Sjmcneill 	fdtbus_register_phy_controller(self, phandle, &meson_usbphy_funcs);
238912cfa14Sjmcneill }
239