xref: /netbsd-src/sys/arch/arm/sunxi/sunxi_usbphy.c (revision c7941324122a924b6e20c322a6b49a6a3b6137e7)
1*c7941324Sskrll /* $NetBSD: sunxi_usbphy.c,v 1.18 2024/08/13 07:20:23 skrll Exp $ */
21e6185e4Sjmcneill 
31e6185e4Sjmcneill /*-
41e6185e4Sjmcneill  * Copyright (c) 2017 Jared McNeill <jmcneill@invisible.ca>
51e6185e4Sjmcneill  * All rights reserved.
61e6185e4Sjmcneill  *
71e6185e4Sjmcneill  * Redistribution and use in source and binary forms, with or without
81e6185e4Sjmcneill  * modification, are permitted provided that the following conditions
91e6185e4Sjmcneill  * are met:
101e6185e4Sjmcneill  * 1. Redistributions of source code must retain the above copyright
111e6185e4Sjmcneill  *    notice, this list of conditions and the following disclaimer.
121e6185e4Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
131e6185e4Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
141e6185e4Sjmcneill  *    documentation and/or other materials provided with the distribution.
151e6185e4Sjmcneill  *
161e6185e4Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
171e6185e4Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
181e6185e4Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
191e6185e4Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
201e6185e4Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
211e6185e4Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
221e6185e4Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
231e6185e4Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
241e6185e4Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
251e6185e4Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
261e6185e4Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
271e6185e4Sjmcneill  */
281e6185e4Sjmcneill 
291e6185e4Sjmcneill #include <sys/cdefs.h>
301e6185e4Sjmcneill 
31*c7941324Sskrll __KERNEL_RCSID(0, "$NetBSD: sunxi_usbphy.c,v 1.18 2024/08/13 07:20:23 skrll Exp $");
321e6185e4Sjmcneill 
331e6185e4Sjmcneill #include <sys/param.h>
341e6185e4Sjmcneill #include <sys/bus.h>
351e6185e4Sjmcneill #include <sys/device.h>
361e6185e4Sjmcneill #include <sys/intr.h>
371e6185e4Sjmcneill #include <sys/systm.h>
381e6185e4Sjmcneill #include <sys/time.h>
391e6185e4Sjmcneill 
401e6185e4Sjmcneill #include <dev/fdt/fdtvar.h>
411e6185e4Sjmcneill 
42662852e2Sjmcneill /* PHY control registers */
43662852e2Sjmcneill #define	PHYCTL_ICR		0x00
44662852e2Sjmcneill #define	 PHYCTL_ICR_ID_PULLUP	__BIT(17)
45662852e2Sjmcneill #define	 PHYCTL_ICR_DPDM_PULLUP	__BIT(16)
46662852e2Sjmcneill #define	 PHYCTL_ICR_FORCE_ID	__BITS(15,14)
47662852e2Sjmcneill #define	  PHYCTL_ICR_FORCE_ID_LOW	2
48662852e2Sjmcneill #define	  PHYCTL_ICR_FORCE_ID_HIGH	3
49662852e2Sjmcneill #define	 PHYCTL_ICR_FORCE_VBUS	__BITS(13,12)
50662852e2Sjmcneill #define	  PHYCTL_ICR_FORCE_VBUS_LOW	2
51662852e2Sjmcneill #define	  PHYCTL_ICR_FORCE_VBUS_HIGH	3
52662852e2Sjmcneill #define	PHYCTL_A10		0x04
53662852e2Sjmcneill #define	PHYCTL_A33		0x10
54662852e2Sjmcneill #define	 PHYCTL_ADDR		__BITS(15,8)
55662852e2Sjmcneill #define	 PHYCTL_DATA		__BIT(7)
56662852e2Sjmcneill #define	PHYCTL_OTG_CFG		0x20
57662852e2Sjmcneill #define	 PHYCTL_OTG_ROUTE_OTG	__BIT(0)
581e6185e4Sjmcneill 
59662852e2Sjmcneill /* PHY registers */
60662852e2Sjmcneill #define	PHY_RES45_CAL_EN	0x0c
61662852e2Sjmcneill #define	PHY_TX_AMPLITUDE_TUNE	0x20
62662852e2Sjmcneill #define	PHY_DISCON_TH_SEL	0x2a
63662852e2Sjmcneill 
64662852e2Sjmcneill /* PMU registers */
65662852e2Sjmcneill #define	PMU_CFG			0x00
66662852e2Sjmcneill #define	 AHB_INCR8		__BIT(10)
67662852e2Sjmcneill #define	 AHB_INCR4		__BIT(9)
68662852e2Sjmcneill #define	 AHB_INCRX_ALIGN	__BIT(8)
69662852e2Sjmcneill #define	 ULPI_BYPASS		__BIT(0)
701e6185e4Sjmcneill #define	PMU_UNK_H3		0x10
711e6185e4Sjmcneill #define	 PMU_UNK_H3_CLR		__BIT(1)
721e6185e4Sjmcneill 
731e6185e4Sjmcneill static int sunxi_usbphy_match(device_t, cfdata_t, void *);
741e6185e4Sjmcneill static void sunxi_usbphy_attach(device_t, device_t, void *);
751e6185e4Sjmcneill 
76ddb9dd9cSjmcneill enum sunxi_usbphy_type {
77c32c9726Sjmcneill 	USBPHY_A10 = 1,
7869b44ac7Sjmcneill 	USBPHY_A13,
79c32c9726Sjmcneill 	USBPHY_A20,
80ddb9dd9cSjmcneill 	USBPHY_A31,
810c019004Sjmcneill 	USBPHY_A64,
823907a27cSjmcneill 	USBPHY_A83T,
83*c7941324Sskrll 	USBPHY_D1,
843907a27cSjmcneill 	USBPHY_H3,
857c742dc3Sjmcneill 	USBPHY_H6,
86ddb9dd9cSjmcneill };
87ddb9dd9cSjmcneill 
88646c0f59Sthorpej static const struct device_compatible_entry compat_data[] = {
89646c0f59Sthorpej 	{ .compat = "allwinner,sun4i-a10-usb-phy",	.value = USBPHY_A10 },
90646c0f59Sthorpej 	{ .compat = "allwinner,sun5i-a13-usb-phy",	.value = USBPHY_A13 },
91646c0f59Sthorpej 	{ .compat = "allwinner,sun6i-a31-usb-phy",	.value = USBPHY_A31 },
92646c0f59Sthorpej 	{ .compat = "allwinner,sun7i-a20-usb-phy",	.value = USBPHY_A20 },
93646c0f59Sthorpej 	{ .compat = "allwinner,sun8i-a83t-usb-phy",	.value = USBPHY_A83T },
94646c0f59Sthorpej 	{ .compat = "allwinner,sun8i-h3-usb-phy",	.value = USBPHY_H3 },
955639be33Sskrll 	{ .compat = "allwinner,sun8i-v3s-usb-phy",	.value = USBPHY_H3 },
96*c7941324Sskrll 	{ .compat = "allwinner,sun20i-d1-usb-phy",	.value = USBPHY_D1 },
97646c0f59Sthorpej 	{ .compat = "allwinner,sun50i-a64-usb-phy",	.value = USBPHY_A64 },
98646c0f59Sthorpej 	{ .compat = "allwinner,sun50i-h6-usb-phy",	.value = USBPHY_H6 },
99ec189949Sthorpej 	DEVICE_COMPAT_EOL
1001e6185e4Sjmcneill };
1011e6185e4Sjmcneill 
10249314569Sjmcneill #define	SUNXI_MAXUSBPHY		4
1031e6185e4Sjmcneill 
1041e6185e4Sjmcneill struct sunxi_usbphy {
1051e6185e4Sjmcneill 	u_int			phy_index;
1061e6185e4Sjmcneill 	bus_space_handle_t	phy_bsh;
1071e6185e4Sjmcneill 	struct fdtbus_regulator *phy_reg;
1081e6185e4Sjmcneill };
1091e6185e4Sjmcneill 
1101e6185e4Sjmcneill struct sunxi_usbphy_softc {
1111e6185e4Sjmcneill 	device_t		sc_dev;
1121e6185e4Sjmcneill 	bus_space_tag_t		sc_bst;
11349314569Sjmcneill 	bus_space_handle_t	sc_bsh_phy_ctrl;
114ddb9dd9cSjmcneill 	enum sunxi_usbphy_type	sc_type;
1151e6185e4Sjmcneill 
1161e6185e4Sjmcneill 	struct sunxi_usbphy	sc_phys[SUNXI_MAXUSBPHY];
1171e6185e4Sjmcneill 	u_int			sc_nphys;
1181e6185e4Sjmcneill 
1191e6185e4Sjmcneill 	struct fdtbus_gpio_pin	*sc_gpio_id_det;
1201e6185e4Sjmcneill 	struct fdtbus_gpio_pin	*sc_gpio_vbus_det;
1211e6185e4Sjmcneill };
1221e6185e4Sjmcneill 
123662852e2Sjmcneill #define	PHYCTL_READ(sc, reg)				\
124662852e2Sjmcneill 	bus_space_read_4((sc)->sc_bst,			\
125662852e2Sjmcneill 	    (sc)->sc_bsh_phy_ctrl, (reg))
126662852e2Sjmcneill #define	PHYCTL_WRITE(sc, reg, val)			\
127662852e2Sjmcneill 	bus_space_write_4((sc)->sc_bst,			\
128662852e2Sjmcneill 	    (sc)->sc_bsh_phy_ctrl, (reg), (val))
129662852e2Sjmcneill #define	PMU_READ(sc, id, reg)			\
1301e6185e4Sjmcneill 	bus_space_read_4((sc)->sc_bst,			\
1311e6185e4Sjmcneill 	    (sc)->sc_phys[(id)].phy_bsh, (reg))
132662852e2Sjmcneill #define	PMU_WRITE(sc, id, reg, val)			\
1331e6185e4Sjmcneill 	bus_space_write_4((sc)->sc_bst,			\
1341e6185e4Sjmcneill 	    (sc)->sc_phys[(id)].phy_bsh, (reg), (val))
1351e6185e4Sjmcneill 
1361e6185e4Sjmcneill CFATTACH_DECL_NEW(sunxi_usbphy, sizeof(struct sunxi_usbphy_softc),
1371e6185e4Sjmcneill 	sunxi_usbphy_match, sunxi_usbphy_attach, NULL, NULL);
1381e6185e4Sjmcneill 
139662852e2Sjmcneill static void
140662852e2Sjmcneill sunxi_usbphy_write(struct sunxi_usbphy_softc *sc,
141662852e2Sjmcneill     struct sunxi_usbphy *phy, u_int bit_addr, u_int bits,
142662852e2Sjmcneill     u_int len)
143662852e2Sjmcneill {
1448012ca3fSmsaitoh 	const uint32_t usbc_mask = __BIT(phy->phy_index * 2);
145662852e2Sjmcneill 	bus_size_t reg;
146662852e2Sjmcneill 	uint32_t val;
147662852e2Sjmcneill 
148662852e2Sjmcneill 	switch (sc->sc_type) {
149c32c9726Sjmcneill 	case USBPHY_A10:
150662852e2Sjmcneill 	case USBPHY_A13:
151c32c9726Sjmcneill 	case USBPHY_A20:
152662852e2Sjmcneill 	case USBPHY_A31:
153662852e2Sjmcneill 		reg = PHYCTL_A10;
154662852e2Sjmcneill 		break;
155*c7941324Sskrll 	case USBPHY_D1:
156662852e2Sjmcneill 	case USBPHY_H3:
1577c742dc3Sjmcneill 	case USBPHY_H6:
158662852e2Sjmcneill 	case USBPHY_A64:
1593907a27cSjmcneill 	case USBPHY_A83T:
160662852e2Sjmcneill 		reg = PHYCTL_A33;
161662852e2Sjmcneill 		break;
162662852e2Sjmcneill 	default:
163662852e2Sjmcneill 		panic("unsupported phy type");
164662852e2Sjmcneill 	}
165662852e2Sjmcneill 
166662852e2Sjmcneill 	if (reg == PHYCTL_A33)
167662852e2Sjmcneill 		PHYCTL_WRITE(sc, reg, 0);
168662852e2Sjmcneill 
169662852e2Sjmcneill 	for (; len > 0; bit_addr++, bits >>= 1, len--) {
170662852e2Sjmcneill 		val = PHYCTL_READ(sc, reg);
171662852e2Sjmcneill 		val &= ~PHYCTL_ADDR;
172662852e2Sjmcneill 		val |= __SHIFTIN(bit_addr, PHYCTL_ADDR);
173662852e2Sjmcneill 		PHYCTL_WRITE(sc, reg, val);
174662852e2Sjmcneill 
175662852e2Sjmcneill 		val = PHYCTL_READ(sc, reg);
176662852e2Sjmcneill 		val &= ~PHYCTL_DATA;
177662852e2Sjmcneill 		val |= __SHIFTIN(bits & 1, PHYCTL_DATA);
178662852e2Sjmcneill 		PHYCTL_WRITE(sc, reg, val);
179662852e2Sjmcneill 
180662852e2Sjmcneill 		PHYCTL_READ(sc, reg);
181662852e2Sjmcneill 		val |= usbc_mask;
182662852e2Sjmcneill 		PHYCTL_WRITE(sc, reg, val);
183662852e2Sjmcneill 
184662852e2Sjmcneill 		PHYCTL_READ(sc, reg);
185662852e2Sjmcneill 		val &= ~usbc_mask;
186662852e2Sjmcneill 		PHYCTL_WRITE(sc, reg, val);
187662852e2Sjmcneill 	}
188662852e2Sjmcneill }
189662852e2Sjmcneill 
1901e6185e4Sjmcneill static bool
1911e6185e4Sjmcneill sunxi_usbphy_vbus_detect(struct sunxi_usbphy_softc *sc)
1921e6185e4Sjmcneill {
1931e6185e4Sjmcneill 	if (sc->sc_gpio_vbus_det)
1941e6185e4Sjmcneill 		return fdtbus_gpio_read(sc->sc_gpio_vbus_det);
1951e6185e4Sjmcneill 	return 1;
1961e6185e4Sjmcneill }
1971e6185e4Sjmcneill 
1981e6185e4Sjmcneill static void *
1991e6185e4Sjmcneill sunxi_usbphy_acquire(device_t dev, const void *data, size_t len)
2001e6185e4Sjmcneill {
2011e6185e4Sjmcneill 	struct sunxi_usbphy_softc * const sc = device_private(dev);
2021e6185e4Sjmcneill 
2031e6185e4Sjmcneill 	if (len != 4)
2041e6185e4Sjmcneill 		return NULL;
2051e6185e4Sjmcneill 
2061e6185e4Sjmcneill 	const int phy_id = be32dec(data);
2077c742dc3Sjmcneill 	if (phy_id >= sc->sc_nphys || !sc->sc_phys[phy_id].phy_bsh)
2081e6185e4Sjmcneill 		return NULL;
2091e6185e4Sjmcneill 
2101e6185e4Sjmcneill 	return &sc->sc_phys[phy_id];
2111e6185e4Sjmcneill }
2121e6185e4Sjmcneill 
2131e6185e4Sjmcneill static void
2141e6185e4Sjmcneill sunxi_usbphy_release(device_t dev, void *priv)
2151e6185e4Sjmcneill {
2161e6185e4Sjmcneill }
2171e6185e4Sjmcneill 
2181e6185e4Sjmcneill static int
2191e6185e4Sjmcneill sunxi_usbphy_enable(device_t dev, void *priv, bool enable)
2201e6185e4Sjmcneill {
2211e6185e4Sjmcneill 	struct sunxi_usbphy_softc * const sc = device_private(dev);
2221e6185e4Sjmcneill 	struct sunxi_usbphy * const phy = priv;
223662852e2Sjmcneill 	u_int disc_thresh;
224662852e2Sjmcneill 	bool phy0_reroute;
2251e6185e4Sjmcneill 	uint32_t val;
2261e6185e4Sjmcneill 
227662852e2Sjmcneill 	switch (sc->sc_type) {
228662852e2Sjmcneill 	case USBPHY_A13:
229662852e2Sjmcneill 		disc_thresh = 0x2;
230662852e2Sjmcneill 		phy0_reroute = false;
231662852e2Sjmcneill 		break;
232c32c9726Sjmcneill 	case USBPHY_A10:
233c32c9726Sjmcneill 	case USBPHY_A20:
234662852e2Sjmcneill 	case USBPHY_A31:
235662852e2Sjmcneill 		disc_thresh = 0x3;
236662852e2Sjmcneill 		phy0_reroute = false;
237662852e2Sjmcneill 		break;
238662852e2Sjmcneill 	case USBPHY_A64:
239*c7941324Sskrll 	case USBPHY_D1:
240662852e2Sjmcneill 	case USBPHY_H3:
2417c742dc3Sjmcneill 	case USBPHY_H6:
242662852e2Sjmcneill 		disc_thresh = 0x3;
243662852e2Sjmcneill 		phy0_reroute = true;
244662852e2Sjmcneill 		break;
2453907a27cSjmcneill 	case USBPHY_A83T:
2463907a27cSjmcneill 		disc_thresh = 0x0;
2473907a27cSjmcneill 		phy0_reroute = false;
2483907a27cSjmcneill 		break;
249c32c9726Sjmcneill 	default:
250c32c9726Sjmcneill 		aprint_error_dev(dev, "unsupported board\n");
251c32c9726Sjmcneill 		return ENXIO;
252662852e2Sjmcneill 	}
253662852e2Sjmcneill 
254662852e2Sjmcneill 	if (phy->phy_bsh) {
255662852e2Sjmcneill 		/* Enable/disable passby */
256662852e2Sjmcneill 		const uint32_t mask =
257662852e2Sjmcneill 		    ULPI_BYPASS|AHB_INCR8|AHB_INCR4|AHB_INCRX_ALIGN;
258662852e2Sjmcneill 		val = PMU_READ(sc, phy->phy_index, PMU_CFG);
259662852e2Sjmcneill 		if (enable)
260662852e2Sjmcneill 			val |= mask;
261662852e2Sjmcneill 		else
262662852e2Sjmcneill 			val &= ~mask;
263662852e2Sjmcneill 		PMU_WRITE(sc, phy->phy_index, PMU_CFG, val);
2641e6185e4Sjmcneill 	}
2651e6185e4Sjmcneill 
266d69628c1Sjmcneill 	switch (sc->sc_type) {
267d69628c1Sjmcneill 	case USBPHY_H3:
268d69628c1Sjmcneill 	case USBPHY_A64:
269662852e2Sjmcneill 		if (enable && phy->phy_bsh) {
270662852e2Sjmcneill 			val = PMU_READ(sc, phy->phy_index, PMU_UNK_H3);
2711e6185e4Sjmcneill 			val &= ~PMU_UNK_H3_CLR;
272662852e2Sjmcneill 			PMU_WRITE(sc, phy->phy_index, PMU_UNK_H3, val);
273662852e2Sjmcneill 		}
274d69628c1Sjmcneill 		break;
275d69628c1Sjmcneill 	default:
276d69628c1Sjmcneill 		break;
277ddb9dd9cSjmcneill 	}
2781e6185e4Sjmcneill 
279662852e2Sjmcneill 	if (enable) {
2807c742dc3Sjmcneill 		switch (sc->sc_type) {
2817c742dc3Sjmcneill 		case USBPHY_A83T:
2827c742dc3Sjmcneill 		case USBPHY_H6:
2837c742dc3Sjmcneill 			break;
2847c742dc3Sjmcneill 		default:
285662852e2Sjmcneill 			if (phy->phy_index == 0)
286662852e2Sjmcneill 				sunxi_usbphy_write(sc, phy, PHY_RES45_CAL_EN, 0x1, 1);
287662852e2Sjmcneill 			sunxi_usbphy_write(sc, phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
288662852e2Sjmcneill 			sunxi_usbphy_write(sc, phy, PHY_DISCON_TH_SEL, disc_thresh, 2);
2897c742dc3Sjmcneill 			break;
2907c742dc3Sjmcneill 		}
291662852e2Sjmcneill 	}
292662852e2Sjmcneill 
293662852e2Sjmcneill 	if (phy->phy_index == 0) {
294662852e2Sjmcneill 		const uint32_t mask =
295662852e2Sjmcneill 		    PHYCTL_ICR_ID_PULLUP|PHYCTL_ICR_DPDM_PULLUP;
296662852e2Sjmcneill 		val = PHYCTL_READ(sc, PHYCTL_ICR);
297662852e2Sjmcneill 
298662852e2Sjmcneill 		if (enable)
299662852e2Sjmcneill 			val |= mask;
300662852e2Sjmcneill 		else
301662852e2Sjmcneill 			val &= ~mask;
302662852e2Sjmcneill 
303662852e2Sjmcneill 		/* XXX only host mode is supported */
304662852e2Sjmcneill 		val &= ~PHYCTL_ICR_FORCE_ID;
305662852e2Sjmcneill 		val |= __SHIFTIN(PHYCTL_ICR_FORCE_ID_LOW, PHYCTL_ICR_FORCE_ID);
306662852e2Sjmcneill 		val &= ~PHYCTL_ICR_FORCE_VBUS;
307662852e2Sjmcneill 		val |= __SHIFTIN(PHYCTL_ICR_FORCE_VBUS_HIGH, PHYCTL_ICR_FORCE_VBUS);
308662852e2Sjmcneill 
309662852e2Sjmcneill 		PHYCTL_WRITE(sc, PHYCTL_ICR, val);
310662852e2Sjmcneill 
311662852e2Sjmcneill 		if (phy0_reroute) {
312662852e2Sjmcneill 			val = PHYCTL_READ(sc, PHYCTL_OTG_CFG);
313662852e2Sjmcneill 			val &= ~PHYCTL_OTG_ROUTE_OTG;
314662852e2Sjmcneill 			PHYCTL_WRITE(sc, PHYCTL_OTG_CFG, val);
315662852e2Sjmcneill 		}
316662852e2Sjmcneill 	}
317662852e2Sjmcneill 
3181e6185e4Sjmcneill 	if (phy->phy_reg == NULL)
3191e6185e4Sjmcneill 		return 0;
3201e6185e4Sjmcneill 
3211e6185e4Sjmcneill 	if (enable) {
3221e6185e4Sjmcneill 		/* If an external vbus is detected, do not enable phy 0 */
3231e6185e4Sjmcneill 		if (phy->phy_index == 0 && sunxi_usbphy_vbus_detect(sc))
3241e6185e4Sjmcneill 			return 0;
3251e6185e4Sjmcneill 		return fdtbus_regulator_enable(phy->phy_reg);
3261e6185e4Sjmcneill 	} else {
3271e6185e4Sjmcneill 		return fdtbus_regulator_disable(phy->phy_reg);
3281e6185e4Sjmcneill 	}
3291e6185e4Sjmcneill }
3301e6185e4Sjmcneill 
3311e6185e4Sjmcneill const struct fdtbus_phy_controller_func sunxi_usbphy_funcs = {
3321e6185e4Sjmcneill 	.acquire = sunxi_usbphy_acquire,
3331e6185e4Sjmcneill 	.release = sunxi_usbphy_release,
3341e6185e4Sjmcneill 	.enable = sunxi_usbphy_enable,
3351e6185e4Sjmcneill };
3361e6185e4Sjmcneill 
3371e6185e4Sjmcneill static int
3381e6185e4Sjmcneill sunxi_usbphy_match(device_t parent, cfdata_t cf, void *aux)
3391e6185e4Sjmcneill {
3401e6185e4Sjmcneill 	struct fdt_attach_args * const faa = aux;
3411e6185e4Sjmcneill 
3426e54367aSthorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
3431e6185e4Sjmcneill }
3441e6185e4Sjmcneill 
3451e6185e4Sjmcneill static void
3461e6185e4Sjmcneill sunxi_usbphy_attach(device_t parent, device_t self, void *aux)
3471e6185e4Sjmcneill {
3481e6185e4Sjmcneill 	struct sunxi_usbphy_softc * const sc = device_private(self);
3491e6185e4Sjmcneill 	struct fdt_attach_args * const faa = aux;
3501e6185e4Sjmcneill 	const int phandle = faa->faa_phandle;
3511e6185e4Sjmcneill 	struct fdtbus_reset *rst;
3521e6185e4Sjmcneill 	struct sunxi_usbphy *phy;
3531e6185e4Sjmcneill 	struct clk *clk;
3541e6185e4Sjmcneill 	bus_addr_t addr;
3551e6185e4Sjmcneill 	bus_size_t size;
3561e6185e4Sjmcneill 	char pname[20];
3571e6185e4Sjmcneill 	u_int n;
3581e6185e4Sjmcneill 
3591e6185e4Sjmcneill 	sc->sc_dev = self;
3601e6185e4Sjmcneill 	sc->sc_bst = faa->faa_bst;
3616e54367aSthorpej 	sc->sc_type = of_compatible_lookup(phandle, compat_data)->value;
3621e6185e4Sjmcneill 
36351b8db60Sjmcneill 	if (fdtbus_get_reg_byname(phandle, "phy_ctrl", &addr, &size) != 0) {
36449314569Sjmcneill 		aprint_error(": couldn't get phy ctrl registers\n");
36549314569Sjmcneill 		return;
36649314569Sjmcneill 	}
36749314569Sjmcneill 	if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh_phy_ctrl) != 0) {
36849314569Sjmcneill 		aprint_error(": couldn't map phy ctrl registers\n");
36949314569Sjmcneill 		return;
37049314569Sjmcneill 	}
37149314569Sjmcneill 
3721e6185e4Sjmcneill 	for (sc->sc_nphys = 0; sc->sc_nphys < SUNXI_MAXUSBPHY; sc->sc_nphys++) {
3731e6185e4Sjmcneill 		phy = &sc->sc_phys[sc->sc_nphys];
3741e6185e4Sjmcneill 		phy->phy_index = sc->sc_nphys;
37551b8db60Sjmcneill 		snprintf(pname, sizeof(pname), "pmu%d", sc->sc_nphys);
37651b8db60Sjmcneill 		if (fdtbus_get_reg_byname(phandle, pname, &addr, &size) != 0) {
3777c742dc3Sjmcneill 			continue;
37851b8db60Sjmcneill 		} else if (bus_space_map(sc->sc_bst, addr, size, 0, &phy->phy_bsh) != 0) {
3791e6185e4Sjmcneill 			aprint_error(": failed to map reg #%d\n", sc->sc_nphys);
3801e6185e4Sjmcneill 			return;
3811e6185e4Sjmcneill 		}
3821e6185e4Sjmcneill 		/* Get optional regulator */
3831e6185e4Sjmcneill 		snprintf(pname, sizeof(pname), "usb%d_vbus-supply", sc->sc_nphys);
3841e6185e4Sjmcneill 		phy->phy_reg = fdtbus_regulator_acquire(phandle, pname);
3851e6185e4Sjmcneill 	}
3861e6185e4Sjmcneill 
3871e6185e4Sjmcneill 	/* Enable clocks */
3881e6185e4Sjmcneill 	for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
3891e6185e4Sjmcneill 		if (clk_enable(clk) != 0) {
3901e6185e4Sjmcneill 			aprint_error(": couldn't enable clock #%d\n", n);
3911e6185e4Sjmcneill 			return;
3921e6185e4Sjmcneill 		}
3931e6185e4Sjmcneill 	/* De-assert resets */
3941e6185e4Sjmcneill 	for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
3951e6185e4Sjmcneill 		if (fdtbus_reset_deassert(rst) != 0) {
3961e6185e4Sjmcneill 			aprint_error(": couldn't de-assert reset #%d\n", n);
3971e6185e4Sjmcneill 			return;
3981e6185e4Sjmcneill 		}
3991e6185e4Sjmcneill 
4001e6185e4Sjmcneill 	aprint_naive("\n");
4011e6185e4Sjmcneill 	aprint_normal(": USB PHY\n");
4021e6185e4Sjmcneill 
4031e6185e4Sjmcneill 	fdtbus_register_phy_controller(self, phandle, &sunxi_usbphy_funcs);
4041e6185e4Sjmcneill }
405