xref: /netbsd-src/sys/arch/arm/rockchip/rk3288_usb.c (revision 0147092315289aff59a21718467bc2cb642218fe)
1*01470923Sjmcneill /* $NetBSD: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 jmcneill Exp $ */
2*01470923Sjmcneill 
3*01470923Sjmcneill /*-
4*01470923Sjmcneill  * Copyright (c) 2021 Jared McNeill <jmcneill@invisible.ca>
5*01470923Sjmcneill  * All rights reserved.
6*01470923Sjmcneill  *
7*01470923Sjmcneill  * Redistribution and use in source and binary forms, with or without
8*01470923Sjmcneill  * modification, are permitted provided that the following conditions
9*01470923Sjmcneill  * are met:
10*01470923Sjmcneill  * 1. Redistributions of source code must retain the above copyright
11*01470923Sjmcneill  *    notice, this list of conditions and the following disclaimer.
12*01470923Sjmcneill  * 2. Redistributions in binary form must reproduce the above copyright
13*01470923Sjmcneill  *    notice, this list of conditions and the following disclaimer in the
14*01470923Sjmcneill  *    documentation and/or other materials provided with the distribution.
15*01470923Sjmcneill  *
16*01470923Sjmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*01470923Sjmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*01470923Sjmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*01470923Sjmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*01470923Sjmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*01470923Sjmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*01470923Sjmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*01470923Sjmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*01470923Sjmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*01470923Sjmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*01470923Sjmcneill  * POSSIBILITY OF SUCH DAMAGE.
27*01470923Sjmcneill  */
28*01470923Sjmcneill 
29*01470923Sjmcneill #include <sys/cdefs.h>
30*01470923Sjmcneill 
31*01470923Sjmcneill __KERNEL_RCSID(0, "$NetBSD: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 jmcneill Exp $");
32*01470923Sjmcneill 
33*01470923Sjmcneill #include <sys/param.h>
34*01470923Sjmcneill #include <sys/bus.h>
35*01470923Sjmcneill #include <sys/device.h>
36*01470923Sjmcneill #include <sys/intr.h>
37*01470923Sjmcneill #include <sys/systm.h>
38*01470923Sjmcneill #include <sys/time.h>
39*01470923Sjmcneill #include <sys/kmem.h>
40*01470923Sjmcneill 
41*01470923Sjmcneill #include <dev/fdt/fdtvar.h>
42*01470923Sjmcneill #include <dev/fdt/syscon.h>
43*01470923Sjmcneill 
44*01470923Sjmcneill #define	GRF_UOCn_CON0_SIDDQ	__BIT(13)
45*01470923Sjmcneill 
46*01470923Sjmcneill static int rk3288_usb_match(device_t, cfdata_t, void *);
47*01470923Sjmcneill static void rk3288_usb_attach(device_t, device_t, void *);
48*01470923Sjmcneill 
49*01470923Sjmcneill static const struct device_compatible_entry compat_data[] = {
50*01470923Sjmcneill 	{ .compat = "rockchip,rk3288-usb-phy" },
51*01470923Sjmcneill 	DEVICE_COMPAT_EOL
52*01470923Sjmcneill };
53*01470923Sjmcneill 
54*01470923Sjmcneill CFATTACH_DECL_NEW(rk3288_usb, 0,
55*01470923Sjmcneill 	rk3288_usb_match, rk3288_usb_attach, NULL, NULL);
56*01470923Sjmcneill 
57*01470923Sjmcneill static int
rk3288_usb_match(device_t parent,cfdata_t cf,void * aux)58*01470923Sjmcneill rk3288_usb_match(device_t parent, cfdata_t cf, void *aux)
59*01470923Sjmcneill {
60*01470923Sjmcneill 	struct fdt_attach_args * const faa = aux;
61*01470923Sjmcneill 
62*01470923Sjmcneill 	return of_compatible_match(faa->faa_phandle, compat_data);
63*01470923Sjmcneill }
64*01470923Sjmcneill 
65*01470923Sjmcneill static void
rk3288_usb_attach(device_t parent,device_t self,void * aux)66*01470923Sjmcneill rk3288_usb_attach(device_t parent, device_t self, void *aux)
67*01470923Sjmcneill {
68*01470923Sjmcneill 	struct fdt_attach_args * const faa = aux;
69*01470923Sjmcneill 	const int phandle = faa->faa_phandle;
70*01470923Sjmcneill 	int child;
71*01470923Sjmcneill 
72*01470923Sjmcneill 	aprint_naive("\n");
73*01470923Sjmcneill 	aprint_normal(": USB PHY\n");
74*01470923Sjmcneill 
75*01470923Sjmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
76*01470923Sjmcneill 		if (!fdtbus_status_okay(child))
77*01470923Sjmcneill 			continue;
78*01470923Sjmcneill 
79*01470923Sjmcneill 		struct fdt_attach_args cfaa = *faa;
80*01470923Sjmcneill 		cfaa.faa_phandle = child;
81*01470923Sjmcneill 		cfaa.faa_name = fdtbus_get_string(child, "name");
82*01470923Sjmcneill 		cfaa.faa_quiet = false;
83*01470923Sjmcneill 
84*01470923Sjmcneill 		config_found(self, &cfaa, NULL, CFARGS_NONE);
85*01470923Sjmcneill 	}
86*01470923Sjmcneill }
87*01470923Sjmcneill 
88*01470923Sjmcneill /*
89*01470923Sjmcneill  * USB PHY
90*01470923Sjmcneill  */
91*01470923Sjmcneill 
92*01470923Sjmcneill static int rk3288_usbphy_match(device_t, cfdata_t, void *);
93*01470923Sjmcneill static void rk3288_usbphy_attach(device_t, device_t, void *);
94*01470923Sjmcneill 
95*01470923Sjmcneill struct rk3288_usbphy_softc {
96*01470923Sjmcneill 	device_t	sc_dev;
97*01470923Sjmcneill 	int		sc_phandle;
98*01470923Sjmcneill 	struct syscon	*sc_syscon;
99*01470923Sjmcneill 	bus_addr_t	sc_reg;
100*01470923Sjmcneill };
101*01470923Sjmcneill 
102*01470923Sjmcneill CFATTACH_DECL_NEW(rk3288_usbphy, sizeof(struct rk3288_usbphy_softc),
103*01470923Sjmcneill 	rk3288_usbphy_match, rk3288_usbphy_attach, NULL, NULL);
104*01470923Sjmcneill 
105*01470923Sjmcneill static void *
rk3288_usbphy_acquire(device_t dev,const void * data,size_t len)106*01470923Sjmcneill rk3288_usbphy_acquire(device_t dev, const void *data, size_t len)
107*01470923Sjmcneill {
108*01470923Sjmcneill 	struct rk3288_usbphy_softc * const sc = device_private(dev);
109*01470923Sjmcneill 
110*01470923Sjmcneill 	if (len != 0)
111*01470923Sjmcneill 		return NULL;
112*01470923Sjmcneill 
113*01470923Sjmcneill 	return sc;
114*01470923Sjmcneill }
115*01470923Sjmcneill 
116*01470923Sjmcneill static void
rk3288_usbphy_release(device_t dev,void * priv)117*01470923Sjmcneill rk3288_usbphy_release(device_t dev, void *priv)
118*01470923Sjmcneill {
119*01470923Sjmcneill }
120*01470923Sjmcneill 
121*01470923Sjmcneill static int
rk3288_usbphy_enable(device_t dev,void * priv,bool enable)122*01470923Sjmcneill rk3288_usbphy_enable(device_t dev, void *priv, bool enable)
123*01470923Sjmcneill {
124*01470923Sjmcneill 	struct rk3288_usbphy_softc * const sc = device_private(dev);
125*01470923Sjmcneill 	uint32_t write_mask, write_val;
126*01470923Sjmcneill 
127*01470923Sjmcneill 	write_mask = GRF_UOCn_CON0_SIDDQ << 16;
128*01470923Sjmcneill 	write_val = enable ? 0 : GRF_UOCn_CON0_SIDDQ;
129*01470923Sjmcneill 
130*01470923Sjmcneill 	syscon_lock(sc->sc_syscon);
131*01470923Sjmcneill 	syscon_write_4(sc->sc_syscon, sc->sc_reg, write_mask | write_val);
132*01470923Sjmcneill 	syscon_unlock(sc->sc_syscon);
133*01470923Sjmcneill 
134*01470923Sjmcneill 	return 0;
135*01470923Sjmcneill }
136*01470923Sjmcneill 
137*01470923Sjmcneill const struct fdtbus_phy_controller_func rk3288_usbphy_funcs = {
138*01470923Sjmcneill 	.acquire = rk3288_usbphy_acquire,
139*01470923Sjmcneill 	.release = rk3288_usbphy_release,
140*01470923Sjmcneill 	.enable = rk3288_usbphy_enable,
141*01470923Sjmcneill };
142*01470923Sjmcneill 
143*01470923Sjmcneill static int
rk3288_usbphy_match(device_t parent,cfdata_t cf,void * aux)144*01470923Sjmcneill rk3288_usbphy_match(device_t parent, cfdata_t cf, void *aux)
145*01470923Sjmcneill {
146*01470923Sjmcneill 	return 1;
147*01470923Sjmcneill }
148*01470923Sjmcneill 
149*01470923Sjmcneill static void
rk3288_usbphy_attach(device_t parent,device_t self,void * aux)150*01470923Sjmcneill rk3288_usbphy_attach(device_t parent, device_t self, void *aux)
151*01470923Sjmcneill {
152*01470923Sjmcneill 	struct rk3288_usbphy_softc * const sc = device_private(self);
153*01470923Sjmcneill 	struct fdt_attach_args * const faa = aux;
154*01470923Sjmcneill 	const int phandle = faa->faa_phandle;
155*01470923Sjmcneill 	struct fdtbus_reset *rst;
156*01470923Sjmcneill 
157*01470923Sjmcneill 	sc->sc_dev = self;
158*01470923Sjmcneill 	sc->sc_phandle = phandle;
159*01470923Sjmcneill 	sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(OF_parent(phandle)));
160*01470923Sjmcneill 	if (sc->sc_syscon == NULL) {
161*01470923Sjmcneill 		aprint_error(": couldn't find syscon\n");
162*01470923Sjmcneill 		return;
163*01470923Sjmcneill 	}
164*01470923Sjmcneill 	if (fdtbus_get_reg(phandle, 0, &sc->sc_reg, NULL) != 0) {
165*01470923Sjmcneill 		aprint_error(": couldn't find registers\n");
166*01470923Sjmcneill 		return;
167*01470923Sjmcneill 	}
168*01470923Sjmcneill 
169*01470923Sjmcneill 	rst = fdtbus_reset_get(phandle, "phy-reset");
170*01470923Sjmcneill 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
171*01470923Sjmcneill 		aprint_error(": couldn't de-assert reset\n");
172*01470923Sjmcneill 		return;
173*01470923Sjmcneill 	}
174*01470923Sjmcneill 	if (fdtbus_clock_enable(phandle, "phyclk", true) != 0) {
175*01470923Sjmcneill 		aprint_error(": couildn't enable clock\n");
176*01470923Sjmcneill 		return;
177*01470923Sjmcneill 	}
178*01470923Sjmcneill 
179*01470923Sjmcneill 	aprint_naive("\n");
180*01470923Sjmcneill 	aprint_normal(": USB port\n");
181*01470923Sjmcneill 
182*01470923Sjmcneill 	fdtbus_register_phy_controller(self, phandle, &rk3288_usbphy_funcs);
183*01470923Sjmcneill }
184