xref: /freebsd/sys/arm64/rockchip/rk_usb2phy.c (revision 62e8ccc3)
1c3e25952SEmmanuel Vadot /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3c3e25952SEmmanuel Vadot  *
4c3e25952SEmmanuel Vadot  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org>
5c3e25952SEmmanuel Vadot  *
6c3e25952SEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
7c3e25952SEmmanuel Vadot  * modification, are permitted provided that the following conditions
8c3e25952SEmmanuel Vadot  * are met:
9c3e25952SEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
10c3e25952SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
11c3e25952SEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
12c3e25952SEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
13c3e25952SEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
14c3e25952SEmmanuel Vadot  *
15c3e25952SEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16c3e25952SEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17c3e25952SEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18c3e25952SEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19c3e25952SEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20c3e25952SEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21c3e25952SEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22c3e25952SEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23c3e25952SEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24c3e25952SEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25c3e25952SEmmanuel Vadot  * SUCH DAMAGE.
26c3e25952SEmmanuel Vadot  */
27c3e25952SEmmanuel Vadot 
28c3e25952SEmmanuel Vadot /*
29c3e25952SEmmanuel Vadot  * Rockchip USB2PHY
30c3e25952SEmmanuel Vadot  */
31c3e25952SEmmanuel Vadot 
32c3e25952SEmmanuel Vadot #include <sys/param.h>
33c3e25952SEmmanuel Vadot #include <sys/systm.h>
34c3e25952SEmmanuel Vadot #include <sys/bus.h>
35c3e25952SEmmanuel Vadot #include <sys/rman.h>
36c3e25952SEmmanuel Vadot #include <sys/kernel.h>
37c3e25952SEmmanuel Vadot #include <sys/module.h>
38c3e25952SEmmanuel Vadot #include <sys/gpio.h>
39c3e25952SEmmanuel Vadot #include <machine/bus.h>
40c3e25952SEmmanuel Vadot 
41c3e25952SEmmanuel Vadot #include <dev/fdt/fdt_common.h>
42c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_bus.h>
43c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h>
44c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_subr.h>
45c3e25952SEmmanuel Vadot 
46be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
47950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
48b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h>
4962e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
50c3e25952SEmmanuel Vadot 
51c3e25952SEmmanuel Vadot #include "clkdev_if.h"
52c3e25952SEmmanuel Vadot #include "syscon_if.h"
53c3e25952SEmmanuel Vadot 
54c3e25952SEmmanuel Vadot struct rk_usb2phy_reg {
55c3e25952SEmmanuel Vadot 	uint32_t	offset;
56c3e25952SEmmanuel Vadot 	uint32_t	enable_mask;
57c3e25952SEmmanuel Vadot 	uint32_t	disable_mask;
58c3e25952SEmmanuel Vadot };
59c3e25952SEmmanuel Vadot 
60c3e25952SEmmanuel Vadot struct rk_usb2phy_regs {
61c3e25952SEmmanuel Vadot 	struct rk_usb2phy_reg	clk_ctl;
62c3e25952SEmmanuel Vadot };
63c3e25952SEmmanuel Vadot 
64c3e25952SEmmanuel Vadot struct rk_usb2phy_regs rk3399_regs = {
65c3e25952SEmmanuel Vadot 	.clk_ctl = {
6679c6e0b3SSøren Schmidt 		.offset = 0x0000,
67c3e25952SEmmanuel Vadot 		/* bit 4 put pll in suspend */
68c3e25952SEmmanuel Vadot 		.enable_mask = 0x100000,
69c3e25952SEmmanuel Vadot 		.disable_mask = 0x100010,
70c3e25952SEmmanuel Vadot 	}
71c3e25952SEmmanuel Vadot };
72c3e25952SEmmanuel Vadot 
7379c6e0b3SSøren Schmidt struct rk_usb2phy_regs rk3568_regs = {
7479c6e0b3SSøren Schmidt 	.clk_ctl = {
7579c6e0b3SSøren Schmidt 		.offset = 0x0008,
7679c6e0b3SSøren Schmidt 		.enable_mask = 0x100000,
7779c6e0b3SSøren Schmidt 		/* bit 4 put pll in suspend */
7879c6e0b3SSøren Schmidt 		.disable_mask = 0x100010,
7979c6e0b3SSøren Schmidt 	}
8079c6e0b3SSøren Schmidt };
8179c6e0b3SSøren Schmidt 
82c3e25952SEmmanuel Vadot static struct ofw_compat_data compat_data[] = {
83c3e25952SEmmanuel Vadot 	{ "rockchip,rk3399-usb2phy",	(uintptr_t)&rk3399_regs },
8479c6e0b3SSøren Schmidt 	{ "rockchip,rk3568-usb2phy",	(uintptr_t)&rk3568_regs },
85c3e25952SEmmanuel Vadot 	{ NULL,				0 }
86c3e25952SEmmanuel Vadot };
87c3e25952SEmmanuel Vadot 
88c3e25952SEmmanuel Vadot struct rk_usb2phy_softc {
89c3e25952SEmmanuel Vadot 	device_t		dev;
90c3e25952SEmmanuel Vadot 	struct syscon		*grf;
91c3e25952SEmmanuel Vadot 	regulator_t		phy_supply;
92c3e25952SEmmanuel Vadot 	clk_t			clk;
93c36e2d14SEmmanuel Vadot 	int			mode;
94c3e25952SEmmanuel Vadot };
95c3e25952SEmmanuel Vadot 
96c3e25952SEmmanuel Vadot /* Phy class and methods. */
97c3e25952SEmmanuel Vadot static int rk_usb2phy_enable(struct phynode *phynode, bool enable);
98c36e2d14SEmmanuel Vadot static int rk_usb2phy_get_mode(struct phynode *phy, int *mode);
99c36e2d14SEmmanuel Vadot static int rk_usb2phy_set_mode(struct phynode *phy, int mode);
100c3e25952SEmmanuel Vadot static phynode_method_t rk_usb2phy_phynode_methods[] = {
101c3e25952SEmmanuel Vadot 	PHYNODEMETHOD(phynode_enable,		rk_usb2phy_enable),
102c36e2d14SEmmanuel Vadot 	PHYNODEMETHOD(phynode_usb_get_mode,	rk_usb2phy_get_mode),
103c36e2d14SEmmanuel Vadot 	PHYNODEMETHOD(phynode_usb_set_mode,	rk_usb2phy_set_mode),
104c3e25952SEmmanuel Vadot 
105c3e25952SEmmanuel Vadot 	PHYNODEMETHOD_END
106c3e25952SEmmanuel Vadot };
107c3e25952SEmmanuel Vadot 
108c3e25952SEmmanuel Vadot DEFINE_CLASS_1(rk_usb2phy_phynode, rk_usb2phy_phynode_class,
109c3e25952SEmmanuel Vadot     rk_usb2phy_phynode_methods,
110c3e25952SEmmanuel Vadot     sizeof(struct phynode_usb_sc), phynode_usb_class);
111c3e25952SEmmanuel Vadot 
11279c6e0b3SSøren Schmidt enum RK_USBPHY {
11379c6e0b3SSøren Schmidt 	RK_USBPHY_HOST = 0,
11479c6e0b3SSøren Schmidt 	RK_USBPHY_OTG,
115c3e25952SEmmanuel Vadot };
116c3e25952SEmmanuel Vadot 
117c3e25952SEmmanuel Vadot static int
rk_usb2phy_enable(struct phynode * phynode,bool enable)118c3e25952SEmmanuel Vadot rk_usb2phy_enable(struct phynode *phynode, bool enable)
119c3e25952SEmmanuel Vadot {
120c3e25952SEmmanuel Vadot 	struct rk_usb2phy_softc *sc;
121c3e25952SEmmanuel Vadot 	device_t dev;
122c3e25952SEmmanuel Vadot 	intptr_t phy;
123c3e25952SEmmanuel Vadot 	int error;
124c3e25952SEmmanuel Vadot 
125c3e25952SEmmanuel Vadot 	dev = phynode_get_device(phynode);
126c3e25952SEmmanuel Vadot 	phy = phynode_get_id(phynode);
127c3e25952SEmmanuel Vadot 	sc = device_get_softc(dev);
128c3e25952SEmmanuel Vadot 
12979c6e0b3SSøren Schmidt 	if (phy != RK_USBPHY_HOST)
130c3e25952SEmmanuel Vadot 		return (ERANGE);
131c3e25952SEmmanuel Vadot 
132c3e25952SEmmanuel Vadot 	if (sc->phy_supply) {
133c3e25952SEmmanuel Vadot 		if (enable)
134c3e25952SEmmanuel Vadot 			error = regulator_enable(sc->phy_supply);
135c3e25952SEmmanuel Vadot 		else
136c3e25952SEmmanuel Vadot 			error = regulator_disable(sc->phy_supply);
137c3e25952SEmmanuel Vadot 		if (error != 0) {
138c3e25952SEmmanuel Vadot 			device_printf(dev, "Cannot %sable the regulator\n",
139c3e25952SEmmanuel Vadot 			    enable ? "En" : "Dis");
140c3e25952SEmmanuel Vadot 			goto fail;
141c3e25952SEmmanuel Vadot 		}
142c3e25952SEmmanuel Vadot 	}
143c3e25952SEmmanuel Vadot 
144c3e25952SEmmanuel Vadot 	return (0);
145c3e25952SEmmanuel Vadot fail:
146c3e25952SEmmanuel Vadot 	return (ENXIO);
147c3e25952SEmmanuel Vadot }
148c3e25952SEmmanuel Vadot 
149c36e2d14SEmmanuel Vadot static int
rk_usb2phy_get_mode(struct phynode * phynode,int * mode)150c36e2d14SEmmanuel Vadot rk_usb2phy_get_mode(struct phynode *phynode, int *mode)
151c36e2d14SEmmanuel Vadot {
152c36e2d14SEmmanuel Vadot 	struct rk_usb2phy_softc *sc;
153c36e2d14SEmmanuel Vadot 	intptr_t phy;
154c36e2d14SEmmanuel Vadot 	device_t dev;
155c36e2d14SEmmanuel Vadot 
156c36e2d14SEmmanuel Vadot 	dev = phynode_get_device(phynode);
157c36e2d14SEmmanuel Vadot 	phy = phynode_get_id(phynode);
158c36e2d14SEmmanuel Vadot 	sc = device_get_softc(dev);
159c36e2d14SEmmanuel Vadot 
16079c6e0b3SSøren Schmidt 	if (phy != RK_USBPHY_HOST)
161c36e2d14SEmmanuel Vadot 		return (ERANGE);
162c36e2d14SEmmanuel Vadot 
163c36e2d14SEmmanuel Vadot 	*mode = sc->mode;
164c36e2d14SEmmanuel Vadot 
165c36e2d14SEmmanuel Vadot 	return (0);
166c36e2d14SEmmanuel Vadot }
167c36e2d14SEmmanuel Vadot 
168c36e2d14SEmmanuel Vadot static int
rk_usb2phy_set_mode(struct phynode * phynode,int mode)169c36e2d14SEmmanuel Vadot rk_usb2phy_set_mode(struct phynode *phynode, int mode)
170c36e2d14SEmmanuel Vadot {
171c36e2d14SEmmanuel Vadot 	struct rk_usb2phy_softc *sc;
172c36e2d14SEmmanuel Vadot 	intptr_t phy;
173c36e2d14SEmmanuel Vadot 	device_t dev;
174c36e2d14SEmmanuel Vadot 
175c36e2d14SEmmanuel Vadot 	dev = phynode_get_device(phynode);
176c36e2d14SEmmanuel Vadot 	phy = phynode_get_id(phynode);
177c36e2d14SEmmanuel Vadot 	sc = device_get_softc(dev);
178c36e2d14SEmmanuel Vadot 
17979c6e0b3SSøren Schmidt 	if (phy != RK_USBPHY_HOST)
180c36e2d14SEmmanuel Vadot 		return (ERANGE);
181c36e2d14SEmmanuel Vadot 
182c36e2d14SEmmanuel Vadot 	sc->mode = mode;
183c36e2d14SEmmanuel Vadot 
184c36e2d14SEmmanuel Vadot 	return (0);
185c36e2d14SEmmanuel Vadot }
186c36e2d14SEmmanuel Vadot 
187c3e25952SEmmanuel Vadot /* Clock class and method */
188c3e25952SEmmanuel Vadot struct rk_usb2phy_clk_sc {
189c3e25952SEmmanuel Vadot 	device_t	clkdev;
190c3e25952SEmmanuel Vadot 	struct syscon	*grf;
191c3e25952SEmmanuel Vadot 	struct rk_usb2phy_regs	*regs;
192c3e25952SEmmanuel Vadot };
193c3e25952SEmmanuel Vadot 
194c3e25952SEmmanuel Vadot static int
rk_usb2phy_clk_init(struct clknode * clk,device_t dev)195c3e25952SEmmanuel Vadot rk_usb2phy_clk_init(struct clknode *clk, device_t dev)
196c3e25952SEmmanuel Vadot {
197c3e25952SEmmanuel Vadot 
198c3e25952SEmmanuel Vadot 	clknode_init_parent_idx(clk, 0);
199c3e25952SEmmanuel Vadot 	return (0);
200c3e25952SEmmanuel Vadot }
201c3e25952SEmmanuel Vadot 
202c3e25952SEmmanuel Vadot static int
rk_usb2phy_clk_set_gate(struct clknode * clk,bool enable)203c3e25952SEmmanuel Vadot rk_usb2phy_clk_set_gate(struct clknode *clk, bool enable)
204c3e25952SEmmanuel Vadot {
205c3e25952SEmmanuel Vadot 	struct rk_usb2phy_clk_sc *sc;
206c3e25952SEmmanuel Vadot 
207c3e25952SEmmanuel Vadot 	sc = clknode_get_softc(clk);
208c3e25952SEmmanuel Vadot 
209c3e25952SEmmanuel Vadot 	if (enable)
210c3e25952SEmmanuel Vadot 		SYSCON_WRITE_4(sc->grf, sc->regs->clk_ctl.offset,
211c3e25952SEmmanuel Vadot 		    sc->regs->clk_ctl.enable_mask);
212c3e25952SEmmanuel Vadot 	else
213c3e25952SEmmanuel Vadot 		SYSCON_WRITE_4(sc->grf, sc->regs->clk_ctl.offset,
214c3e25952SEmmanuel Vadot 		    sc->regs->clk_ctl.disable_mask);
215c3e25952SEmmanuel Vadot 	return (0);
216c3e25952SEmmanuel Vadot }
217c3e25952SEmmanuel Vadot 
218c3e25952SEmmanuel Vadot static int
rk_usb2phy_clk_recalc(struct clknode * clk,uint64_t * freq)219c3e25952SEmmanuel Vadot rk_usb2phy_clk_recalc(struct clknode *clk, uint64_t *freq)
220c3e25952SEmmanuel Vadot {
221c3e25952SEmmanuel Vadot 
222c3e25952SEmmanuel Vadot 	*freq = 480000000;
223c3e25952SEmmanuel Vadot 
224c3e25952SEmmanuel Vadot 	return (0);
225c3e25952SEmmanuel Vadot }
226c3e25952SEmmanuel Vadot 
227c3e25952SEmmanuel Vadot static clknode_method_t rk_usb2phy_clk_clknode_methods[] = {
228c3e25952SEmmanuel Vadot 	/* Device interface */
229c3e25952SEmmanuel Vadot 
230c3e25952SEmmanuel Vadot 	CLKNODEMETHOD(clknode_init,		rk_usb2phy_clk_init),
231c3e25952SEmmanuel Vadot 	CLKNODEMETHOD(clknode_set_gate,		rk_usb2phy_clk_set_gate),
232c3e25952SEmmanuel Vadot 	CLKNODEMETHOD(clknode_recalc_freq,	rk_usb2phy_clk_recalc),
233c3e25952SEmmanuel Vadot 	CLKNODEMETHOD_END
234c3e25952SEmmanuel Vadot };
235c3e25952SEmmanuel Vadot 
236c3e25952SEmmanuel Vadot DEFINE_CLASS_1(rk_usb2phy_clk_clknode, rk_usb2phy_clk_clknode_class,
237c3e25952SEmmanuel Vadot     rk_usb2phy_clk_clknode_methods, sizeof(struct rk_usb2phy_clk_sc),
238c3e25952SEmmanuel Vadot     clknode_class);
239c3e25952SEmmanuel Vadot 
240c3e25952SEmmanuel Vadot static int
rk_usb2phy_clk_ofw_map(struct clkdom * clkdom,uint32_t ncells,phandle_t * cells,struct clknode ** clk)241c3e25952SEmmanuel Vadot rk_usb2phy_clk_ofw_map(struct clkdom *clkdom, uint32_t ncells,
242c3e25952SEmmanuel Vadot     phandle_t *cells, struct clknode **clk)
243c3e25952SEmmanuel Vadot {
244c3e25952SEmmanuel Vadot 
245c3e25952SEmmanuel Vadot 	if (ncells != 0)
246c3e25952SEmmanuel Vadot 		return (ERANGE);
247c3e25952SEmmanuel Vadot 
248c3e25952SEmmanuel Vadot 	*clk = clknode_find_by_id(clkdom, 0);
249c3e25952SEmmanuel Vadot 
250c3e25952SEmmanuel Vadot 	if (*clk == NULL)
251c3e25952SEmmanuel Vadot 		return (ENXIO);
252c3e25952SEmmanuel Vadot 	return (0);
253c3e25952SEmmanuel Vadot }
254c3e25952SEmmanuel Vadot 
255c3e25952SEmmanuel Vadot static int
rk_usb2phy_export_clock(struct rk_usb2phy_softc * devsc)256c3e25952SEmmanuel Vadot rk_usb2phy_export_clock(struct rk_usb2phy_softc *devsc)
257c3e25952SEmmanuel Vadot {
258c3e25952SEmmanuel Vadot 	struct clknode_init_def def;
259c3e25952SEmmanuel Vadot 	struct rk_usb2phy_clk_sc *sc;
260c3e25952SEmmanuel Vadot 	const char **clknames;
261c3e25952SEmmanuel Vadot 	struct clkdom *clkdom;
262c3e25952SEmmanuel Vadot 	struct clknode *clk;
263c3e25952SEmmanuel Vadot 	clk_t clk_parent;
264c3e25952SEmmanuel Vadot 	phandle_t node;
265c3e25952SEmmanuel Vadot 	phandle_t regs[2];
266c3e25952SEmmanuel Vadot 	int i, nclocks, ncells, error;
267c3e25952SEmmanuel Vadot 
268c3e25952SEmmanuel Vadot 	node = ofw_bus_get_node(devsc->dev);
269c3e25952SEmmanuel Vadot 
270c3e25952SEmmanuel Vadot 	error = ofw_bus_parse_xref_list_get_length(node, "clocks",
271c3e25952SEmmanuel Vadot 	    "#clock-cells", &ncells);
272c3e25952SEmmanuel Vadot 	if (error != 0 || ncells != 1) {
273c3e25952SEmmanuel Vadot 		device_printf(devsc->dev, "couldn't find parent clock\n");
274c3e25952SEmmanuel Vadot 		return (ENXIO);
275c3e25952SEmmanuel Vadot 	}
276c3e25952SEmmanuel Vadot 
277c3e25952SEmmanuel Vadot 	nclocks = ofw_bus_string_list_to_array(node, "clock-output-names",
278c3e25952SEmmanuel Vadot 	    &clknames);
279c3e25952SEmmanuel Vadot 	if (nclocks != 1)
280c3e25952SEmmanuel Vadot 		return (ENXIO);
281c3e25952SEmmanuel Vadot 
282c3e25952SEmmanuel Vadot 	clkdom = clkdom_create(devsc->dev);
283c3e25952SEmmanuel Vadot 	clkdom_set_ofw_mapper(clkdom, rk_usb2phy_clk_ofw_map);
284c3e25952SEmmanuel Vadot 
285c3e25952SEmmanuel Vadot 	memset(&def, 0, sizeof(def));
286c3e25952SEmmanuel Vadot 	def.id = 0;
287c3e25952SEmmanuel Vadot 	def.name = clknames[0];
288c3e25952SEmmanuel Vadot 	def.parent_names = malloc(sizeof(char *) * ncells, M_OFWPROP, M_WAITOK);
289c3e25952SEmmanuel Vadot 	for (i = 0; i < ncells; i++) {
290c3e25952SEmmanuel Vadot 		error = clk_get_by_ofw_index(devsc->dev, 0, i, &clk_parent);
291c3e25952SEmmanuel Vadot 		if (error != 0) {
292c3e25952SEmmanuel Vadot 			device_printf(devsc->dev, "cannot get clock %d\n", error);
293c3e25952SEmmanuel Vadot 			return (ENXIO);
294c3e25952SEmmanuel Vadot 		}
295c3e25952SEmmanuel Vadot 		def.parent_names[i] = clk_get_name(clk_parent);
296c3e25952SEmmanuel Vadot 		clk_release(clk_parent);
297c3e25952SEmmanuel Vadot 	}
298c3e25952SEmmanuel Vadot 	def.parent_cnt = ncells;
299c3e25952SEmmanuel Vadot 
300c3e25952SEmmanuel Vadot 	clk = clknode_create(clkdom, &rk_usb2phy_clk_clknode_class, &def);
301c3e25952SEmmanuel Vadot 	if (clk == NULL) {
302c3e25952SEmmanuel Vadot 		device_printf(devsc->dev, "cannot create clknode\n");
303c3e25952SEmmanuel Vadot 		return (ENXIO);
304c3e25952SEmmanuel Vadot 	}
305c3e25952SEmmanuel Vadot 
306c3e25952SEmmanuel Vadot 	sc = clknode_get_softc(clk);
307c3e25952SEmmanuel Vadot 	sc->clkdev = device_get_parent(devsc->dev);
308c3e25952SEmmanuel Vadot 	sc->grf = devsc->grf;
309c3e25952SEmmanuel Vadot 	sc->regs = (struct rk_usb2phy_regs *)ofw_bus_search_compatible(devsc->dev, compat_data)->ocd_data;
31079c6e0b3SSøren Schmidt 	if (sc->regs->clk_ctl.offset == 0) {
311c3e25952SEmmanuel Vadot 		OF_getencprop(node, "reg", regs, sizeof(regs));
312c3e25952SEmmanuel Vadot 		sc->regs->clk_ctl.offset = regs[0];
31379c6e0b3SSøren Schmidt 	}
314c3e25952SEmmanuel Vadot 	clknode_register(clkdom, clk);
315c3e25952SEmmanuel Vadot 
316c3e25952SEmmanuel Vadot 	if (clkdom_finit(clkdom) != 0) {
317c3e25952SEmmanuel Vadot 		device_printf(devsc->dev, "cannot finalize clkdom initialization\n");
318c3e25952SEmmanuel Vadot 		return (ENXIO);
319c3e25952SEmmanuel Vadot 	}
320c3e25952SEmmanuel Vadot 
321c3e25952SEmmanuel Vadot 	if (bootverbose)
322c3e25952SEmmanuel Vadot 		clkdom_dump(clkdom);
323c3e25952SEmmanuel Vadot 
324c3e25952SEmmanuel Vadot 	return (0);
325c3e25952SEmmanuel Vadot }
326c3e25952SEmmanuel Vadot 
327c3e25952SEmmanuel Vadot static int
rk_usb2phy_probe(device_t dev)328c3e25952SEmmanuel Vadot rk_usb2phy_probe(device_t dev)
329c3e25952SEmmanuel Vadot {
330c3e25952SEmmanuel Vadot 
331c3e25952SEmmanuel Vadot 	if (!ofw_bus_status_okay(dev))
332c3e25952SEmmanuel Vadot 		return (ENXIO);
333c3e25952SEmmanuel Vadot 
334c3e25952SEmmanuel Vadot 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
335c3e25952SEmmanuel Vadot 		return (ENXIO);
336c3e25952SEmmanuel Vadot 
33779c6e0b3SSøren Schmidt 	device_set_desc(dev, "Rockchip USB2PHY");
338c3e25952SEmmanuel Vadot 	return (BUS_PROBE_DEFAULT);
339c3e25952SEmmanuel Vadot }
340c3e25952SEmmanuel Vadot 
341c3e25952SEmmanuel Vadot static int
rk_usb2phy_attach(device_t dev)342c3e25952SEmmanuel Vadot rk_usb2phy_attach(device_t dev)
343c3e25952SEmmanuel Vadot {
344c3e25952SEmmanuel Vadot 	struct rk_usb2phy_softc *sc;
345c3e25952SEmmanuel Vadot 	struct phynode_init_def phy_init;
346c3e25952SEmmanuel Vadot 	struct phynode *phynode;
347c3e25952SEmmanuel Vadot 	phandle_t node, host;
348c3e25952SEmmanuel Vadot 	int err;
349c3e25952SEmmanuel Vadot 
350c3e25952SEmmanuel Vadot 	sc = device_get_softc(dev);
351c3e25952SEmmanuel Vadot 	sc->dev = dev;
352c3e25952SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
353c3e25952SEmmanuel Vadot 
35479c6e0b3SSøren Schmidt 	if (OF_hasprop(node, "rockchip,usbgrf")) {
35579c6e0b3SSøren Schmidt 		if (syscon_get_by_ofw_property(dev, node, "rockchip,usbgrf",
35679c6e0b3SSøren Schmidt 		    &sc->grf)) {
357c3e25952SEmmanuel Vadot 			device_printf(dev, "Cannot get syscon handle\n");
358c3e25952SEmmanuel Vadot 			return (ENXIO);
359c3e25952SEmmanuel Vadot 		}
36079c6e0b3SSøren Schmidt 	}
36179c6e0b3SSøren Schmidt 	else {
36279c6e0b3SSøren Schmidt 		if (syscon_get_handle_default(dev, &sc->grf)) {
36379c6e0b3SSøren Schmidt 			device_printf(dev, "Cannot get syscon handle\n");
36479c6e0b3SSøren Schmidt 			return (ENXIO);
36579c6e0b3SSøren Schmidt 		}
36679c6e0b3SSøren Schmidt 	}
367c3e25952SEmmanuel Vadot 
368c3e25952SEmmanuel Vadot 	if (clk_get_by_ofw_name(dev, 0, "phyclk", &sc->clk) != 0) {
369c3e25952SEmmanuel Vadot 		device_printf(dev, "Cannot get clock\n");
370c3e25952SEmmanuel Vadot 		return (ENXIO);
371c3e25952SEmmanuel Vadot 	}
372c3e25952SEmmanuel Vadot 	err = clk_enable(sc->clk);
373c3e25952SEmmanuel Vadot 	if (err != 0) {
374c3e25952SEmmanuel Vadot 		device_printf(dev, "Could not enable clock %s\n",
375c3e25952SEmmanuel Vadot 		    clk_get_name(sc->clk));
376c3e25952SEmmanuel Vadot 		return (ENXIO);
377c3e25952SEmmanuel Vadot 	}
378c3e25952SEmmanuel Vadot 
379c3e25952SEmmanuel Vadot 	err = rk_usb2phy_export_clock(sc);
380c3e25952SEmmanuel Vadot 	if (err != 0)
381c3e25952SEmmanuel Vadot 		return (err);
382c3e25952SEmmanuel Vadot 
383c3e25952SEmmanuel Vadot 	/* Only host is supported right now */
384c3e25952SEmmanuel Vadot 
385c3e25952SEmmanuel Vadot 	host = ofw_bus_find_child(node, "host-port");
386c3e25952SEmmanuel Vadot 	if (host == 0) {
387c3e25952SEmmanuel Vadot 		device_printf(dev, "Cannot find host-port child node\n");
388c3e25952SEmmanuel Vadot 		return (ENXIO);
389c3e25952SEmmanuel Vadot 	}
390c3e25952SEmmanuel Vadot 
391c3e25952SEmmanuel Vadot 	if (!ofw_bus_node_status_okay(host)) {
392c3e25952SEmmanuel Vadot 		device_printf(dev, "host-port isn't okay\n");
393c3e25952SEmmanuel Vadot 		return (0);
394c3e25952SEmmanuel Vadot 	}
395c3e25952SEmmanuel Vadot 
396c3e25952SEmmanuel Vadot 	regulator_get_by_ofw_property(dev, host, "phy-supply", &sc->phy_supply);
39779c6e0b3SSøren Schmidt 	phy_init.id = RK_USBPHY_HOST;
398c3e25952SEmmanuel Vadot 	phy_init.ofw_node = host;
399c3e25952SEmmanuel Vadot 	phynode = phynode_create(dev, &rk_usb2phy_phynode_class, &phy_init);
400c3e25952SEmmanuel Vadot 	if (phynode == NULL) {
401c3e25952SEmmanuel Vadot 		device_printf(dev, "failed to create host USB2PHY\n");
402c3e25952SEmmanuel Vadot 		return (ENXIO);
403c3e25952SEmmanuel Vadot 	}
404c3e25952SEmmanuel Vadot 	if (phynode_register(phynode) == NULL) {
405c3e25952SEmmanuel Vadot 		device_printf(dev, "failed to register host USB2PHY\n");
406c3e25952SEmmanuel Vadot 		return (ENXIO);
407c3e25952SEmmanuel Vadot 	}
408c3e25952SEmmanuel Vadot 
409c3e25952SEmmanuel Vadot 	OF_device_register_xref(OF_xref_from_node(host), dev);
410c3e25952SEmmanuel Vadot 
411c3e25952SEmmanuel Vadot 	return (0);
412c3e25952SEmmanuel Vadot }
413c3e25952SEmmanuel Vadot 
414c3e25952SEmmanuel Vadot static device_method_t rk_usb2phy_methods[] = {
415c3e25952SEmmanuel Vadot 	/* Device interface */
416c3e25952SEmmanuel Vadot 	DEVMETHOD(device_probe,		rk_usb2phy_probe),
417c3e25952SEmmanuel Vadot 	DEVMETHOD(device_attach,	rk_usb2phy_attach),
418c3e25952SEmmanuel Vadot 
419c3e25952SEmmanuel Vadot 	DEVMETHOD_END
420c3e25952SEmmanuel Vadot };
421c3e25952SEmmanuel Vadot 
422c3e25952SEmmanuel Vadot static driver_t rk_usb2phy_driver = {
423c3e25952SEmmanuel Vadot 	"rk_usb2phy",
424c3e25952SEmmanuel Vadot 	rk_usb2phy_methods,
425c3e25952SEmmanuel Vadot 	sizeof(struct rk_usb2phy_softc)
426c3e25952SEmmanuel Vadot };
427c3e25952SEmmanuel Vadot 
428b2c1681aSJohn Baldwin EARLY_DRIVER_MODULE(rk_usb2phy, simplebus, rk_usb2phy_driver, 0, 0,
429b2c1681aSJohn Baldwin     BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE);
430c3e25952SEmmanuel Vadot MODULE_VERSION(rk_usb2phy, 1);
431