xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_usb3phy.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sunxi_usb3phy.c,v 1.5 2021/01/27 03:10:20 thorpej Exp $ */
2e2b0aa16Sjmcneill 
3e2b0aa16Sjmcneill /*-
4e2b0aa16Sjmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill@invisible.ca>
5e2b0aa16Sjmcneill  * All rights reserved.
6e2b0aa16Sjmcneill  *
7e2b0aa16Sjmcneill  * Redistribution and use in source and binary forms, with or without
8e2b0aa16Sjmcneill  * modification, are permitted provided that the following conditions
9e2b0aa16Sjmcneill  * are met:
10e2b0aa16Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11e2b0aa16Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12e2b0aa16Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13e2b0aa16Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14e2b0aa16Sjmcneill  *    documentation and/or other materials provided with the distribution.
15e2b0aa16Sjmcneill  *
16e2b0aa16Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17e2b0aa16Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18e2b0aa16Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19e2b0aa16Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20e2b0aa16Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21e2b0aa16Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22e2b0aa16Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23e2b0aa16Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24e2b0aa16Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25e2b0aa16Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26e2b0aa16Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
27e2b0aa16Sjmcneill  */
28e2b0aa16Sjmcneill 
29e2b0aa16Sjmcneill #include <sys/cdefs.h>
30e2b0aa16Sjmcneill 
31*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sunxi_usb3phy.c,v 1.5 2021/01/27 03:10:20 thorpej Exp $");
32e2b0aa16Sjmcneill 
33e2b0aa16Sjmcneill #include <sys/param.h>
34e2b0aa16Sjmcneill #include <sys/bus.h>
35e2b0aa16Sjmcneill #include <sys/device.h>
36e2b0aa16Sjmcneill #include <sys/intr.h>
37e2b0aa16Sjmcneill #include <sys/systm.h>
38e2b0aa16Sjmcneill #include <sys/time.h>
39e2b0aa16Sjmcneill 
40e2b0aa16Sjmcneill #include <dev/fdt/fdtvar.h>
41e2b0aa16Sjmcneill 
42e2b0aa16Sjmcneill #define	SUNXI_APP			0x00
43e2b0aa16Sjmcneill #define	 APP_FORCE_VBUS			__BITS(13,12)
44e2b0aa16Sjmcneill 
45e2b0aa16Sjmcneill #define	SUNXI_PIPE_CLOCK_CONTROL	0x14
46e2b0aa16Sjmcneill #define	 PCC_PIPE_CLK_OPEN		__BIT(6)
47e2b0aa16Sjmcneill 
48e2b0aa16Sjmcneill #define	SUNXI_PHY_TUNE_LOW		0x18
49e2b0aa16Sjmcneill #define	 PTL_MAGIC			0x0047fc87
50e2b0aa16Sjmcneill 
51e2b0aa16Sjmcneill #define	SUNXI_PHY_TUNE_HIGH		0x1c
52e2b0aa16Sjmcneill #define	 PTH_TX_DEEMPH_3P5DB		__BITS(24,19)
53e2b0aa16Sjmcneill #define	 PTH_TX_DEEMPH_6DB		__BITS(18,13)
54e2b0aa16Sjmcneill #define	 PTH_TX_SWING_FULL		__BITS(12,6)
55e2b0aa16Sjmcneill #define	 PTH_LOS_BIAS			__BITS(5,3)
56e2b0aa16Sjmcneill #define	 PTH_TX_BOOST_LVL		__BITS(2,0)
57e2b0aa16Sjmcneill 
58e2b0aa16Sjmcneill #define	SUNXI_PHY_EXTERNAL_CONTROL	0x20
59e2b0aa16Sjmcneill #define	 PEC_REF_SSP_EN			__BIT(26)
60e2b0aa16Sjmcneill #define	 PEC_SSC_EN			__BIT(24)
61e2b0aa16Sjmcneill #define	 PEC_EXTERN_VBUS		__BITS(2,1)
62e2b0aa16Sjmcneill 
63e2b0aa16Sjmcneill static int sunxi_usb3phy_match(device_t, cfdata_t, void *);
64e2b0aa16Sjmcneill static void sunxi_usb3phy_attach(device_t, device_t, void *);
65e2b0aa16Sjmcneill 
66e2b0aa16Sjmcneill enum sunxi_usb3phy_type {
67e2b0aa16Sjmcneill 	USB3PHY_H6 = 1,
68e2b0aa16Sjmcneill };
69e2b0aa16Sjmcneill 
70646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
71646c0f59Sthorpej 	{ .compat = "allwinner,sun50i-h6-usb3-phy",	.value = USB3PHY_H6 },
72ec189949Sthorpej 	DEVICE_COMPAT_EOL
73e2b0aa16Sjmcneill };
74e2b0aa16Sjmcneill 
75e2b0aa16Sjmcneill struct sunxi_usb3phy {
76e2b0aa16Sjmcneill 	bus_space_tag_t		phy_bst;
77e2b0aa16Sjmcneill 	bus_space_handle_t	phy_bsh;
78e2b0aa16Sjmcneill 	struct fdtbus_regulator *phy_reg;
79e2b0aa16Sjmcneill };
80e2b0aa16Sjmcneill 
81e2b0aa16Sjmcneill struct sunxi_usb3phy_softc {
82e2b0aa16Sjmcneill 	device_t		sc_dev;
83e2b0aa16Sjmcneill 	enum sunxi_usb3phy_type	sc_type;
84e2b0aa16Sjmcneill 
85e2b0aa16Sjmcneill 	struct sunxi_usb3phy	sc_phy;
86e2b0aa16Sjmcneill 
87e2b0aa16Sjmcneill 	struct fdtbus_gpio_pin	*sc_gpio_id_det;
88e2b0aa16Sjmcneill 	struct fdtbus_gpio_pin	*sc_gpio_vbus_det;
89e2b0aa16Sjmcneill };
90e2b0aa16Sjmcneill 
91e2b0aa16Sjmcneill #define	PHY_READ(phy, reg)				\
92e2b0aa16Sjmcneill 	bus_space_read_4((phy)->phy_bst, (phy)->phy_bsh, (reg))
93e2b0aa16Sjmcneill #define	PHY_WRITE(phy, reg, val)				\
94e2b0aa16Sjmcneill 	bus_space_write_4((phy)->phy_bst, (phy)->phy_bsh, (reg), (val))
95e2b0aa16Sjmcneill 
96e2b0aa16Sjmcneill CFATTACH_DECL_NEW(sunxi_usb3phy, sizeof(struct sunxi_usb3phy_softc),
97e2b0aa16Sjmcneill 	sunxi_usb3phy_match, sunxi_usb3phy_attach, NULL, NULL);
98e2b0aa16Sjmcneill 
99e2b0aa16Sjmcneill static void *
sunxi_usb3phy_acquire(device_t dev,const void * data,size_t len)100e2b0aa16Sjmcneill sunxi_usb3phy_acquire(device_t dev, const void *data, size_t len)
101e2b0aa16Sjmcneill {
102e2b0aa16Sjmcneill 	struct sunxi_usb3phy_softc * const sc = device_private(dev);
103e2b0aa16Sjmcneill 
104e2b0aa16Sjmcneill 	return &sc->sc_phy;
105e2b0aa16Sjmcneill }
106e2b0aa16Sjmcneill 
107e2b0aa16Sjmcneill static void
sunxi_usb3phy_release(device_t dev,void * priv)108e2b0aa16Sjmcneill sunxi_usb3phy_release(device_t dev, void *priv)
109e2b0aa16Sjmcneill {
110e2b0aa16Sjmcneill }
111e2b0aa16Sjmcneill 
112e2b0aa16Sjmcneill static int
sunxi_usb3phy_enable(device_t dev,void * priv,bool enable)113e2b0aa16Sjmcneill sunxi_usb3phy_enable(device_t dev, void *priv, bool enable)
114e2b0aa16Sjmcneill {
115e2b0aa16Sjmcneill 	struct sunxi_usb3phy * const phy = priv;
116e2b0aa16Sjmcneill 	uint32_t val;
117e2b0aa16Sjmcneill 
118e2b0aa16Sjmcneill 	if (enable) {
119e2b0aa16Sjmcneill 		val = PHY_READ(phy, SUNXI_PHY_EXTERNAL_CONTROL);
120e2b0aa16Sjmcneill 		val |= PEC_EXTERN_VBUS;
121e2b0aa16Sjmcneill 		val |= PEC_SSC_EN;
122e2b0aa16Sjmcneill 		val |= PEC_REF_SSP_EN;
123e2b0aa16Sjmcneill 		PHY_WRITE(phy, SUNXI_PHY_EXTERNAL_CONTROL, val);
124e2b0aa16Sjmcneill 
125e2b0aa16Sjmcneill 		val = PHY_READ(phy, SUNXI_PIPE_CLOCK_CONTROL);
126e2b0aa16Sjmcneill 		val |= PCC_PIPE_CLK_OPEN;
127e2b0aa16Sjmcneill 		PHY_WRITE(phy, SUNXI_PIPE_CLOCK_CONTROL, val);
128e2b0aa16Sjmcneill 
129e2b0aa16Sjmcneill 		val = PHY_READ(phy, SUNXI_APP);
130e2b0aa16Sjmcneill 		val |= APP_FORCE_VBUS;
131e2b0aa16Sjmcneill 		PHY_WRITE(phy, SUNXI_APP, val);
132e2b0aa16Sjmcneill 
133e2b0aa16Sjmcneill 		PHY_WRITE(phy, SUNXI_PHY_TUNE_LOW, PTL_MAGIC);
134e2b0aa16Sjmcneill 
135e2b0aa16Sjmcneill 		val = PHY_READ(phy, SUNXI_PHY_TUNE_HIGH);
136e2b0aa16Sjmcneill 		val |= PTH_TX_BOOST_LVL;
137e2b0aa16Sjmcneill 		val |= PTH_LOS_BIAS;
138e2b0aa16Sjmcneill 		val &= ~PTH_TX_SWING_FULL;
139e2b0aa16Sjmcneill 		val |= __SHIFTIN(0x55, PTH_TX_SWING_FULL);
140e2b0aa16Sjmcneill 		val &= ~PTH_TX_DEEMPH_6DB;
141e2b0aa16Sjmcneill 		val |= __SHIFTIN(0x20, PTH_TX_DEEMPH_6DB);
142e2b0aa16Sjmcneill 		val &= ~PTH_TX_DEEMPH_3P5DB;
143e2b0aa16Sjmcneill 		val |= __SHIFTIN(0x15, PTH_TX_DEEMPH_3P5DB);
144e2b0aa16Sjmcneill 		PHY_WRITE(phy, SUNXI_PHY_TUNE_HIGH, val);
145e2b0aa16Sjmcneill 
146e2b0aa16Sjmcneill 		return phy->phy_reg ? fdtbus_regulator_enable(phy->phy_reg) : 0;
147e2b0aa16Sjmcneill 	} else {
148e2b0aa16Sjmcneill 		return phy->phy_reg ? fdtbus_regulator_disable(phy->phy_reg) : 0;
149e2b0aa16Sjmcneill 	}
150e2b0aa16Sjmcneill }
151e2b0aa16Sjmcneill 
152e2b0aa16Sjmcneill const struct fdtbus_phy_controller_func sunxi_usb3phy_funcs = {
153e2b0aa16Sjmcneill 	.acquire = sunxi_usb3phy_acquire,
154e2b0aa16Sjmcneill 	.release = sunxi_usb3phy_release,
155e2b0aa16Sjmcneill 	.enable = sunxi_usb3phy_enable,
156e2b0aa16Sjmcneill };
157e2b0aa16Sjmcneill 
158e2b0aa16Sjmcneill static int
sunxi_usb3phy_match(device_t parent,cfdata_t cf,void * aux)159e2b0aa16Sjmcneill sunxi_usb3phy_match(device_t parent, cfdata_t cf, void *aux)
160e2b0aa16Sjmcneill {
161e2b0aa16Sjmcneill 	struct fdt_attach_args * const faa = aux;
162e2b0aa16Sjmcneill 
163*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
164e2b0aa16Sjmcneill }
165e2b0aa16Sjmcneill 
166e2b0aa16Sjmcneill static void
sunxi_usb3phy_attach(device_t parent,device_t self,void * aux)167e2b0aa16Sjmcneill sunxi_usb3phy_attach(device_t parent, device_t self, void *aux)
168e2b0aa16Sjmcneill {
169e2b0aa16Sjmcneill 	struct sunxi_usb3phy_softc * const sc = device_private(self);
170e2b0aa16Sjmcneill 	struct sunxi_usb3phy *phy = &sc->sc_phy;
171e2b0aa16Sjmcneill 	struct fdt_attach_args * const faa = aux;
172e2b0aa16Sjmcneill 	const int phandle = faa->faa_phandle;
173e2b0aa16Sjmcneill 	struct fdtbus_reset *rst;
174e2b0aa16Sjmcneill 	struct clk *clk;
175e2b0aa16Sjmcneill 	bus_addr_t addr;
176e2b0aa16Sjmcneill 	bus_size_t size;
177e2b0aa16Sjmcneill 	u_int n;
178e2b0aa16Sjmcneill 
179e2b0aa16Sjmcneill 	sc->sc_dev = self;
180*6e54367aSthorpej 	sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
181e2b0aa16Sjmcneill 
182e2b0aa16Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
183e2b0aa16Sjmcneill 		aprint_error(": couldn't get phy registers\n");
184e2b0aa16Sjmcneill 		return;
185e2b0aa16Sjmcneill 	}
186e2b0aa16Sjmcneill 
187e2b0aa16Sjmcneill 	phy->phy_bst = faa->faa_bst;
188e2b0aa16Sjmcneill 	if (bus_space_map(phy->phy_bst, addr, size, 0, &phy->phy_bsh) != 0) {
189e2b0aa16Sjmcneill 		aprint_error(": couldn't map phy registers\n");
190e2b0aa16Sjmcneill 		return;
191e2b0aa16Sjmcneill 	}
192e2b0aa16Sjmcneill 
193e2b0aa16Sjmcneill 	/* Get optional regulator */
194e2b0aa16Sjmcneill 	phy->phy_reg = fdtbus_regulator_acquire(phandle, "phy-supply");
195e2b0aa16Sjmcneill 
196e2b0aa16Sjmcneill 	/* Enable clocks */
197e2b0aa16Sjmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
198e2b0aa16Sjmcneill 		if (clk_enable(clk) != 0) {
199e2b0aa16Sjmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
200e2b0aa16Sjmcneill 			return;
201e2b0aa16Sjmcneill 		}
202e2b0aa16Sjmcneill 	/* De-assert resets */
203e2b0aa16Sjmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
204e2b0aa16Sjmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
205e2b0aa16Sjmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
206e2b0aa16Sjmcneill 			return;
207e2b0aa16Sjmcneill 		}
208e2b0aa16Sjmcneill 
209e2b0aa16Sjmcneill 	aprint_naive("\n");
210e2b0aa16Sjmcneill 	aprint_normal(": USB3 PHY\n");
211e2b0aa16Sjmcneill 
212e2b0aa16Sjmcneill 	fdtbus_register_phy_controller(self, phandle, &sunxi_usb3phy_funcs);
213e2b0aa16Sjmcneill }
214