xref: /netbsd-src/sys/arch/arm/sunxi/sun9i_a80_usbphy.c (revision 6e54367a22fbc89a1139d033e95bec0c0cf0975b)
1*6e54367aSthorpej /* $NetBSD: sun9i_a80_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $ */
2f0ea48bbSjmcneill 
3f0ea48bbSjmcneill /*-
4f0ea48bbSjmcneill  * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5f0ea48bbSjmcneill  * All rights reserved.
6f0ea48bbSjmcneill  *
7f0ea48bbSjmcneill  * Redistribution and use in source and binary forms, with or without
8f0ea48bbSjmcneill  * modification, are permitted provided that the following conditions
9f0ea48bbSjmcneill  * are met:
10f0ea48bbSjmcneill  * 1. Redistributions of source code must retain the above copyright
11f0ea48bbSjmcneill  *    notice, this list of conditions and the following disclaimer.
12f0ea48bbSjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13f0ea48bbSjmcneill  *    notice, this list of conditions and the following disclaimer in the
14f0ea48bbSjmcneill  *    documentation and/or other materials provided with the distribution.
15f0ea48bbSjmcneill  *
16f0ea48bbSjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17f0ea48bbSjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18f0ea48bbSjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19f0ea48bbSjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20f0ea48bbSjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21f0ea48bbSjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22f0ea48bbSjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23f0ea48bbSjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24f0ea48bbSjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25f0ea48bbSjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26f0ea48bbSjmcneill  * POSSIBILITY OF SUCH DAMAGE.
27f0ea48bbSjmcneill  */
28f0ea48bbSjmcneill 
29f0ea48bbSjmcneill #include <sys/cdefs.h>
30f0ea48bbSjmcneill 
31*6e54367aSthorpej __KERNEL_RCSID(0, "$NetBSD: sun9i_a80_usbphy.c,v 1.2 2021/01/27 03:10:20 thorpej Exp $");
32f0ea48bbSjmcneill 
33f0ea48bbSjmcneill #include <sys/param.h>
34f0ea48bbSjmcneill #include <sys/bus.h>
35f0ea48bbSjmcneill #include <sys/device.h>
36f0ea48bbSjmcneill #include <sys/intr.h>
37f0ea48bbSjmcneill #include <sys/systm.h>
38f0ea48bbSjmcneill #include <sys/time.h>
39f0ea48bbSjmcneill 
40f0ea48bbSjmcneill #include <dev/fdt/fdtvar.h>
41f0ea48bbSjmcneill 
42f0ea48bbSjmcneill /* PMU registers */
43f0ea48bbSjmcneill #define	PMU_CFG			0x00
44f0ea48bbSjmcneill #define	 EHCI_HS_FORCE		__BIT(20)
45f0ea48bbSjmcneill #define	 HSIC_CONNECT_DET	__BIT(17)
46f0ea48bbSjmcneill #define	 HSIC_CONNECT_INT	__BIT(16)
47f0ea48bbSjmcneill #define	 AHB_INCR16		__BIT(11)
48f0ea48bbSjmcneill #define	 AHB_INCR8		__BIT(10)
49f0ea48bbSjmcneill #define	 AHB_INCR4		__BIT(9)
50f0ea48bbSjmcneill #define	 AHB_INCRX_ALIGN	__BIT(8)
51f0ea48bbSjmcneill #define	 HSIC			__BIT(1)
52f0ea48bbSjmcneill #define	 ULPI_BYPASS		__BIT(0)
53f0ea48bbSjmcneill 
54f0ea48bbSjmcneill static int sun9i_usbphy_match(device_t, cfdata_t, void *);
55f0ea48bbSjmcneill static void sun9i_usbphy_attach(device_t, device_t, void *);
56f0ea48bbSjmcneill 
57*6e54367aSthorpej static const struct device_compatible_entry compat_data[] = {
58*6e54367aSthorpej 	{ .compat = "allwinner,sun9i-a80-usb-phy" },
59*6e54367aSthorpej 	DEVICE_COMPAT_EOL
60f0ea48bbSjmcneill };
61f0ea48bbSjmcneill 
62f0ea48bbSjmcneill struct sun9i_usbphy_softc {
63f0ea48bbSjmcneill 	device_t		sc_dev;
64f0ea48bbSjmcneill 	bus_space_tag_t		sc_bst;
65f0ea48bbSjmcneill 	bus_space_handle_t	sc_bsh;
66f0ea48bbSjmcneill 
67f0ea48bbSjmcneill 	struct clk		*sc_clk_phy;
68f0ea48bbSjmcneill 	struct clk		*sc_clk_hsic;
69f0ea48bbSjmcneill 	struct fdtbus_reset	*sc_rst;
70f0ea48bbSjmcneill 
71f0ea48bbSjmcneill 	struct fdtbus_regulator	*sc_supply;
72f0ea48bbSjmcneill };
73f0ea48bbSjmcneill 
74f0ea48bbSjmcneill #define	PHY_READ(sc, reg)				\
75f0ea48bbSjmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
76f0ea48bbSjmcneill #define	PHY_WRITE(sc, reg, val)				\
77f0ea48bbSjmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
78f0ea48bbSjmcneill 
79f0ea48bbSjmcneill CFATTACH_DECL_NEW(sunxi_a80_usbphy, sizeof(struct sun9i_usbphy_softc),
80f0ea48bbSjmcneill 	sun9i_usbphy_match, sun9i_usbphy_attach, NULL, NULL);
81f0ea48bbSjmcneill 
82f0ea48bbSjmcneill static void *
sun9i_usbphy_acquire(device_t dev,const void * data,size_t len)83f0ea48bbSjmcneill sun9i_usbphy_acquire(device_t dev, const void *data, size_t len)
84f0ea48bbSjmcneill {
85f0ea48bbSjmcneill 	struct sun9i_usbphy_softc * const sc = device_private(dev);
86f0ea48bbSjmcneill 
87f0ea48bbSjmcneill 	return sc;
88f0ea48bbSjmcneill }
89f0ea48bbSjmcneill 
90f0ea48bbSjmcneill static void
sun9i_usbphy_release(device_t dev,void * priv)91f0ea48bbSjmcneill sun9i_usbphy_release(device_t dev, void *priv)
92f0ea48bbSjmcneill {
93f0ea48bbSjmcneill }
94f0ea48bbSjmcneill 
95f0ea48bbSjmcneill static int
sun9i_usbphy_enable(device_t dev,void * priv,bool enable)96f0ea48bbSjmcneill sun9i_usbphy_enable(device_t dev, void *priv, bool enable)
97f0ea48bbSjmcneill {
98f0ea48bbSjmcneill 	struct sun9i_usbphy_softc * const sc = device_private(dev);
99f0ea48bbSjmcneill 	uint32_t passby_mask;
100f0ea48bbSjmcneill 	uint32_t val;
101f0ea48bbSjmcneill 	int error;
102f0ea48bbSjmcneill 
103f0ea48bbSjmcneill 	passby_mask = ULPI_BYPASS|AHB_INCR16|AHB_INCR8|AHB_INCR4|AHB_INCRX_ALIGN;
104f0ea48bbSjmcneill 	if (sc->sc_clk_hsic != NULL)
105f0ea48bbSjmcneill 		passby_mask |= HSIC|EHCI_HS_FORCE|HSIC_CONNECT_DET|HSIC_CONNECT_INT;
106f0ea48bbSjmcneill 
107f0ea48bbSjmcneill 	/* Enable/disable passby */
108f0ea48bbSjmcneill 	if (enable) {
109f0ea48bbSjmcneill 		error = clk_enable(sc->sc_clk_phy);
110f0ea48bbSjmcneill 		if (error != 0)
111f0ea48bbSjmcneill 			return error;
112f0ea48bbSjmcneill 
113f0ea48bbSjmcneill 		if (sc->sc_clk_hsic != NULL) {
114f0ea48bbSjmcneill 			error = clk_enable(sc->sc_clk_hsic);
115f0ea48bbSjmcneill 			if (error != 0)
116f0ea48bbSjmcneill 				return error;
117f0ea48bbSjmcneill 		}
118f0ea48bbSjmcneill 
119f0ea48bbSjmcneill 		error = fdtbus_reset_deassert(sc->sc_rst);
120f0ea48bbSjmcneill 		if (error != 0)
121f0ea48bbSjmcneill 			return error;
122f0ea48bbSjmcneill 
123f0ea48bbSjmcneill 		val = PHY_READ(sc, PMU_CFG);
124f0ea48bbSjmcneill 		val |= passby_mask;
125f0ea48bbSjmcneill 		PHY_WRITE(sc, PMU_CFG, val);
126f0ea48bbSjmcneill 	} else {
127f0ea48bbSjmcneill 		val = PHY_READ(sc, PMU_CFG);
128f0ea48bbSjmcneill 		val &= ~passby_mask;
129f0ea48bbSjmcneill 		PHY_WRITE(sc, PMU_CFG, val);
130f0ea48bbSjmcneill 
131f0ea48bbSjmcneill 		error = fdtbus_reset_assert(sc->sc_rst);
132f0ea48bbSjmcneill 		if (error != 0)
133f0ea48bbSjmcneill 			return error;
134f0ea48bbSjmcneill 
135f0ea48bbSjmcneill 		if (sc->sc_clk_hsic != NULL) {
136f0ea48bbSjmcneill 			error = clk_disable(sc->sc_clk_hsic);
137f0ea48bbSjmcneill 			if (error != 0)
138f0ea48bbSjmcneill 				return error;
139f0ea48bbSjmcneill 		}
140f0ea48bbSjmcneill 
141f0ea48bbSjmcneill 		error = clk_disable(sc->sc_clk_phy);
142f0ea48bbSjmcneill 		if (error != 0)
143f0ea48bbSjmcneill 			return error;
144f0ea48bbSjmcneill 	}
145f0ea48bbSjmcneill 
146f0ea48bbSjmcneill 	return 0;
147f0ea48bbSjmcneill }
148f0ea48bbSjmcneill 
149f0ea48bbSjmcneill const struct fdtbus_phy_controller_func sun9i_usbphy_funcs = {
150f0ea48bbSjmcneill 	.acquire = sun9i_usbphy_acquire,
151f0ea48bbSjmcneill 	.release = sun9i_usbphy_release,
152f0ea48bbSjmcneill 	.enable = sun9i_usbphy_enable,
153f0ea48bbSjmcneill };
154f0ea48bbSjmcneill 
155f0ea48bbSjmcneill static int
sun9i_usbphy_match(device_t parent,cfdata_t cf,void * aux)156f0ea48bbSjmcneill sun9i_usbphy_match(device_t parent, cfdata_t cf, void *aux)
157f0ea48bbSjmcneill {
158f0ea48bbSjmcneill 	struct fdt_attach_args * const faa = aux;
159f0ea48bbSjmcneill 
160*6e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
161f0ea48bbSjmcneill }
162f0ea48bbSjmcneill 
163f0ea48bbSjmcneill static void
sun9i_usbphy_attach(device_t parent,device_t self,void * aux)164f0ea48bbSjmcneill sun9i_usbphy_attach(device_t parent, device_t self, void *aux)
165f0ea48bbSjmcneill {
166f0ea48bbSjmcneill 	struct sun9i_usbphy_softc * const sc = device_private(self);
167f0ea48bbSjmcneill 	struct fdt_attach_args * const faa = aux;
168f0ea48bbSjmcneill 	const int phandle = faa->faa_phandle;
169f0ea48bbSjmcneill 	const char *phy_type;
170f0ea48bbSjmcneill 	bus_addr_t addr;
171f0ea48bbSjmcneill 	bus_size_t size;
172f0ea48bbSjmcneill 
173f0ea48bbSjmcneill 	sc->sc_dev = self;
174f0ea48bbSjmcneill 	sc->sc_bst = faa->faa_bst;
175f0ea48bbSjmcneill 
176f0ea48bbSjmcneill 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
177f0ea48bbSjmcneill 		aprint_error(": couldn't get registers\n");
178f0ea48bbSjmcneill 		return;
179f0ea48bbSjmcneill 	}
180f0ea48bbSjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
181f0ea48bbSjmcneill 		aprint_error(": couldn't map registers\n");
182f0ea48bbSjmcneill 		return;
183f0ea48bbSjmcneill 	}
184f0ea48bbSjmcneill 
185f0ea48bbSjmcneill 	phy_type = fdtbus_get_string(phandle, "phy_type");
186f0ea48bbSjmcneill 	if (phy_type && strcmp(phy_type, "hsic") == 0) {
187f0ea48bbSjmcneill 		sc->sc_clk_phy = fdtbus_clock_get(phandle, "hsic_480M");
188f0ea48bbSjmcneill 		sc->sc_clk_hsic = fdtbus_clock_get(phandle, "hsic_12M");
189f0ea48bbSjmcneill 		sc->sc_rst = fdtbus_reset_get(phandle, "hsic");
190f0ea48bbSjmcneill 
191f0ea48bbSjmcneill 		if (sc->sc_clk_phy == NULL || sc->sc_clk_hsic == NULL || sc->sc_rst == NULL) {
192f0ea48bbSjmcneill 			aprint_error(": couldn't get hsic resources\n");
193f0ea48bbSjmcneill 			return;
194f0ea48bbSjmcneill 		}
195f0ea48bbSjmcneill 	} else {
196f0ea48bbSjmcneill 		sc->sc_clk_phy = fdtbus_clock_get(phandle, "phy");
197f0ea48bbSjmcneill 		sc->sc_rst = fdtbus_reset_get(phandle, "phy");
198f0ea48bbSjmcneill 		if (sc->sc_clk_phy == NULL || sc->sc_rst == NULL) {
199f0ea48bbSjmcneill 			aprint_error(": couldn't get phy resources\n");
200f0ea48bbSjmcneill 			return;
201f0ea48bbSjmcneill 		}
202f0ea48bbSjmcneill 	}
203f0ea48bbSjmcneill 
204f0ea48bbSjmcneill 	aprint_naive("\n");
205f0ea48bbSjmcneill 	aprint_normal(": USB PHY\n");
206f0ea48bbSjmcneill 
207f0ea48bbSjmcneill 	sc->sc_supply = fdtbus_regulator_acquire(phandle, "phy-supply");
208f0ea48bbSjmcneill 	if (sc->sc_supply != NULL) {
209f0ea48bbSjmcneill 		if (fdtbus_regulator_enable(sc->sc_supply) != 0)
210f0ea48bbSjmcneill 			aprint_error_dev(self, "WARNING: couldn't enable power supply\n");
211f0ea48bbSjmcneill 	}
212f0ea48bbSjmcneill 
213f0ea48bbSjmcneill 	fdtbus_register_phy_controller(self, phandle, &sun9i_usbphy_funcs);
214f0ea48bbSjmcneill }
215