1b0352107SMichal Meloun /*-
24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3b0352107SMichal Meloun *
4b0352107SMichal Meloun * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org>
5b0352107SMichal Meloun *
6b0352107SMichal Meloun * Redistribution and use in source and binary forms, with or without
7b0352107SMichal Meloun * modification, are permitted provided that the following conditions
8b0352107SMichal Meloun * are met:
9b0352107SMichal Meloun * 1. Redistributions of source code must retain the above copyright
10b0352107SMichal Meloun * notice, this list of conditions and the following disclaimer.
11b0352107SMichal Meloun * 2. Redistributions in binary form must reproduce the above copyright
12b0352107SMichal Meloun * notice, this list of conditions and the following disclaimer in the
13b0352107SMichal Meloun * documentation and/or other materials provided with the distribution.
14b0352107SMichal Meloun *
15b0352107SMichal Meloun * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16b0352107SMichal Meloun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17b0352107SMichal Meloun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18b0352107SMichal Meloun * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19b0352107SMichal Meloun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20b0352107SMichal Meloun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21b0352107SMichal Meloun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22b0352107SMichal Meloun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23b0352107SMichal Meloun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24b0352107SMichal Meloun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25b0352107SMichal Meloun * SUCH DAMAGE.
26b0352107SMichal Meloun */
27b0352107SMichal Meloun
28b0352107SMichal Meloun #include <sys/param.h>
29b0352107SMichal Meloun #include <sys/systm.h>
30b0352107SMichal Meloun #include <sys/bus.h>
31b0352107SMichal Meloun #include <sys/gpio.h>
32b0352107SMichal Meloun #include <sys/kernel.h>
33b0352107SMichal Meloun #include <sys/module.h>
34b0352107SMichal Meloun #include <sys/malloc.h>
35b0352107SMichal Meloun #include <sys/rman.h>
36b0352107SMichal Meloun
37b0352107SMichal Meloun #include <machine/bus.h>
38b0352107SMichal Meloun
39be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
401f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
41950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
42b2f0caf1SEmmanuel Vadot #include <dev/regulator/regulator.h>
43*62e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
44b0352107SMichal Meloun #include <dev/ofw/ofw_bus.h>
45b0352107SMichal Meloun #include <dev/ofw/ofw_bus_subr.h>
46b0352107SMichal Meloun
47*62e8ccc3SEmmanuel Vadot #include <dev/syscon/syscon.h>
48b0352107SMichal Meloun #include <dev/fdt/simple_mfd.h>
49b0352107SMichal Meloun #include "phynode_if.h"
50b0352107SMichal Meloun #include "phynode_usb_if.h"
51b0352107SMichal Meloun #include "syscon_if.h"
52b0352107SMichal Meloun
53b0352107SMichal Meloun
54b0352107SMichal Meloun
55b0352107SMichal Meloun /* Phy registers */
56b0352107SMichal Meloun #define UOC_CON0 0x00
57b0352107SMichal Meloun #define UOC_CON0_SIDDQ (1 << 13)
58b0352107SMichal Meloun #define UOC_CON0_DISABLE (1 << 4)
59b0352107SMichal Meloun #define UOC_CON0_COMMON_ON_N (1 << 0)
60b0352107SMichal Meloun
61b0352107SMichal Meloun #define UOC_CON2 0x08
62b0352107SMichal Meloun #define UOC_CON2_SOFT_CON_SEL (1 << 2)
63b0352107SMichal Meloun
64b0352107SMichal Meloun #define UOC_CON3 0x0c
65b0352107SMichal Meloun
66b0352107SMichal Meloun
67b0352107SMichal Meloun #define WR4(_sc, _r, _v) bus_write_4((_sc)->mem_res, (_r), (_v))
68b0352107SMichal Meloun #define RD4(_sc, _r) bus_read_4((_sc)->mem_res, (_r))
69b0352107SMichal Meloun
70b0352107SMichal Meloun
71b0352107SMichal Meloun static struct ofw_compat_data compat_data[] = {
72b0352107SMichal Meloun {"rockchip,rk3288-usb-phy", 1},
73b0352107SMichal Meloun {NULL, 0},
74b0352107SMichal Meloun };
75b0352107SMichal Meloun
76b0352107SMichal Meloun struct rk_usbphy_softc {
77b0352107SMichal Meloun device_t dev;
78b0352107SMichal Meloun };
79b0352107SMichal Meloun
80b0352107SMichal Meloun struct rk_phynode_sc {
81b0352107SMichal Meloun struct phynode_usb_sc usb_sc;
82b0352107SMichal Meloun uint32_t base;
83b0352107SMichal Meloun int mode;
84b0352107SMichal Meloun clk_t clk;
85b0352107SMichal Meloun hwreset_t hwreset;
86b0352107SMichal Meloun regulator_t supply_vbus;
87b0352107SMichal Meloun struct syscon *syscon;
88b0352107SMichal Meloun };
89b0352107SMichal Meloun
90b0352107SMichal Meloun static int
rk_phynode_phy_enable(struct phynode * phy,bool enable)91b0352107SMichal Meloun rk_phynode_phy_enable(struct phynode *phy, bool enable)
92b0352107SMichal Meloun {
93b0352107SMichal Meloun struct rk_phynode_sc *sc;
94b0352107SMichal Meloun int rv;
95b0352107SMichal Meloun
96b0352107SMichal Meloun sc = phynode_get_softc(phy);
97b0352107SMichal Meloun
98b0352107SMichal Meloun rv = SYSCON_MODIFY_4(sc->syscon,
99b0352107SMichal Meloun sc->base + UOC_CON0,
100b0352107SMichal Meloun UOC_CON0_SIDDQ << 16 | UOC_CON0_SIDDQ,
101b0352107SMichal Meloun enable ? 0 : UOC_CON0_SIDDQ);
102b0352107SMichal Meloun
103b0352107SMichal Meloun return (rv);
104b0352107SMichal Meloun
105b0352107SMichal Meloun }
106b0352107SMichal Meloun
107b0352107SMichal Meloun static int
rk_phynode_get_mode(struct phynode * phynode,int * mode)108b0352107SMichal Meloun rk_phynode_get_mode(struct phynode *phynode, int *mode)
109b0352107SMichal Meloun {
110b0352107SMichal Meloun struct rk_phynode_sc *sc;
111b0352107SMichal Meloun
112b0352107SMichal Meloun sc = phynode_get_softc(phynode);
113b0352107SMichal Meloun *mode = sc->mode;
114b0352107SMichal Meloun return (0);
115b0352107SMichal Meloun }
116b0352107SMichal Meloun
117b0352107SMichal Meloun static int
rk_phynode_set_mode(struct phynode * phynode,int mode)118b0352107SMichal Meloun rk_phynode_set_mode(struct phynode *phynode, int mode)
119b0352107SMichal Meloun {
120b0352107SMichal Meloun struct rk_phynode_sc *sc;
121b0352107SMichal Meloun
122b0352107SMichal Meloun sc = phynode_get_softc(phynode);
123b0352107SMichal Meloun sc->mode = mode;
124b0352107SMichal Meloun
125b0352107SMichal Meloun return (0);
126b0352107SMichal Meloun }
127b0352107SMichal Meloun
128b0352107SMichal Meloun
129b0352107SMichal Meloun /* Phy controller class and methods. */
130b0352107SMichal Meloun static phynode_method_t rk_phynode_methods[] = {
131b0352107SMichal Meloun PHYNODEUSBMETHOD(phynode_enable, rk_phynode_phy_enable),
132b0352107SMichal Meloun PHYNODEMETHOD(phynode_usb_get_mode, rk_phynode_get_mode),
133b0352107SMichal Meloun PHYNODEMETHOD(phynode_usb_set_mode, rk_phynode_set_mode),
134b0352107SMichal Meloun PHYNODEUSBMETHOD_END
135b0352107SMichal Meloun };
136b0352107SMichal Meloun DEFINE_CLASS_1(rk_phynode, rk_phynode_class, rk_phynode_methods,
137b0352107SMichal Meloun sizeof(struct rk_phynode_sc), phynode_usb_class);
138b0352107SMichal Meloun
139b0352107SMichal Meloun static int
rk_usbphy_init_phy(struct rk_usbphy_softc * sc,phandle_t node)140b0352107SMichal Meloun rk_usbphy_init_phy(struct rk_usbphy_softc *sc, phandle_t node)
141b0352107SMichal Meloun {
142b0352107SMichal Meloun struct phynode *phynode;
143b0352107SMichal Meloun struct phynode_init_def phy_init;
144b0352107SMichal Meloun struct rk_phynode_sc *phy_sc;
145b0352107SMichal Meloun int rv;
146b0352107SMichal Meloun uint32_t base;
147b0352107SMichal Meloun clk_t clk;
148b0352107SMichal Meloun hwreset_t hwreset;
149b0352107SMichal Meloun regulator_t supply_vbus;
150b0352107SMichal Meloun struct syscon *syscon;
151b0352107SMichal Meloun
152b0352107SMichal Meloun clk = NULL;
153b0352107SMichal Meloun hwreset = NULL;
154b0352107SMichal Meloun supply_vbus = NULL;
155b0352107SMichal Meloun
156b0352107SMichal Meloun rv = OF_getencprop(node, "reg", &base, sizeof(base));
157b0352107SMichal Meloun if (rv <= 0) {
158b0352107SMichal Meloun device_printf(sc->dev, "cannot get 'reg' property.\n");
159b0352107SMichal Meloun goto fail;
160b0352107SMichal Meloun }
161b0352107SMichal Meloun
162b0352107SMichal Meloun /* FDT resources. All are optional. */
163b0352107SMichal Meloun rv = clk_get_by_ofw_name(sc->dev, node, "phyclk", &clk);
164b0352107SMichal Meloun if (rv != 0 && rv != ENOENT) {
165b0352107SMichal Meloun device_printf(sc->dev, "cannot get 'phyclk' clock.\n");
166b0352107SMichal Meloun goto fail;
167b0352107SMichal Meloun }
168b0352107SMichal Meloun rv = hwreset_get_by_ofw_name(sc->dev, node, "phy-reset", &hwreset);
169b0352107SMichal Meloun if (rv != 0 && rv != ENOENT) {
170b0352107SMichal Meloun device_printf(sc->dev, "Cannot get 'phy-reset' reset\n");
171b0352107SMichal Meloun goto fail;
172b0352107SMichal Meloun }
173b0352107SMichal Meloun rv = regulator_get_by_ofw_property(sc->dev, node, "vbus-supply",
174b0352107SMichal Meloun &supply_vbus);
175b0352107SMichal Meloun if (rv != 0 && rv != ENOENT) {
176b0352107SMichal Meloun device_printf(sc->dev, "Cannot get 'vbus' regulator.\n");
177b0352107SMichal Meloun goto fail;
178b0352107SMichal Meloun }
179b0352107SMichal Meloun
180b0352107SMichal Meloun rv = SYSCON_GET_HANDLE(sc->dev, &syscon);
181b0352107SMichal Meloun if (rv != 0) {
182b0352107SMichal Meloun device_printf(sc->dev, "Cannot get parent syscon\n");
183b0352107SMichal Meloun goto fail;
184b0352107SMichal Meloun }
185b0352107SMichal Meloun
186b0352107SMichal Meloun /* Init HW resources */
187b0352107SMichal Meloun if (hwreset != NULL) {
188b0352107SMichal Meloun rv = hwreset_assert(hwreset);
189b0352107SMichal Meloun if (rv != 0) {
190b0352107SMichal Meloun device_printf(sc->dev, "Cannot assert reset\n");
191b0352107SMichal Meloun goto fail;
192b0352107SMichal Meloun }
193b0352107SMichal Meloun }
194b0352107SMichal Meloun if (clk != NULL) {
195b0352107SMichal Meloun rv = clk_enable(clk);
196b0352107SMichal Meloun if (rv != 0) {
197b0352107SMichal Meloun device_printf(sc->dev,
198b0352107SMichal Meloun "Cannot enable 'phyclk' clock.\n");
199b0352107SMichal Meloun goto fail;
200b0352107SMichal Meloun }
201b0352107SMichal Meloun }
202b0352107SMichal Meloun
203b0352107SMichal Meloun if (hwreset != NULL) {
204b0352107SMichal Meloun rv = hwreset_deassert(hwreset);
205b0352107SMichal Meloun if (rv != 0) {
206b0352107SMichal Meloun device_printf(sc->dev, "Cannot deassert reset\n");
207b0352107SMichal Meloun goto fail;
208b0352107SMichal Meloun }
209b0352107SMichal Meloun }
210b0352107SMichal Meloun
211b0352107SMichal Meloun /* Create and register phy. */
212b0352107SMichal Meloun bzero(&phy_init, sizeof(phy_init));
213b0352107SMichal Meloun phy_init.id = 1;
214b0352107SMichal Meloun phy_init.ofw_node = node;
215b0352107SMichal Meloun phynode = phynode_create(sc->dev, &rk_phynode_class, &phy_init);
216b0352107SMichal Meloun if (phynode == NULL) {
217b0352107SMichal Meloun device_printf(sc->dev, "Cannot create phy.\n");
218b0352107SMichal Meloun return (ENXIO);
219b0352107SMichal Meloun }
220b0352107SMichal Meloun
221b0352107SMichal Meloun phy_sc = phynode_get_softc(phynode);
222b0352107SMichal Meloun phy_sc->base = base;
223b0352107SMichal Meloun phy_sc->clk = clk;
224b0352107SMichal Meloun phy_sc->hwreset = hwreset;
225b0352107SMichal Meloun phy_sc->supply_vbus = supply_vbus;
226b0352107SMichal Meloun phy_sc->syscon = syscon;
227b0352107SMichal Meloun if (phynode_register(phynode) == NULL) {
228b0352107SMichal Meloun device_printf(sc->dev, "Cannot register phy.\n");
229b0352107SMichal Meloun return (ENXIO);
230b0352107SMichal Meloun }
231b0352107SMichal Meloun /* XXX It breaks boot */
232b0352107SMichal Meloun /* rk_phynode_phy_enable(phynode, 1); */
233b0352107SMichal Meloun return (0);
234b0352107SMichal Meloun
235b0352107SMichal Meloun fail:
236b0352107SMichal Meloun if (supply_vbus != NULL)
237b0352107SMichal Meloun regulator_release(supply_vbus);
238b0352107SMichal Meloun if (clk != NULL)
239b0352107SMichal Meloun clk_release(clk);
240b0352107SMichal Meloun if (hwreset != NULL)
241b0352107SMichal Meloun hwreset_release(hwreset);
242b0352107SMichal Meloun
243b0352107SMichal Meloun return (ENXIO);
244b0352107SMichal Meloun }
245b0352107SMichal Meloun
246b0352107SMichal Meloun static int
rk_usbphy_probe(device_t dev)247b0352107SMichal Meloun rk_usbphy_probe(device_t dev)
248b0352107SMichal Meloun {
249b0352107SMichal Meloun
250b0352107SMichal Meloun if (!ofw_bus_status_okay(dev))
251b0352107SMichal Meloun return (ENXIO);
252b0352107SMichal Meloun
253b0352107SMichal Meloun if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
254b0352107SMichal Meloun return (ENXIO);
255b0352107SMichal Meloun
256b0352107SMichal Meloun device_set_desc(dev, "RockChip USB Phy");
257b0352107SMichal Meloun return (BUS_PROBE_DEFAULT);
258b0352107SMichal Meloun }
259b0352107SMichal Meloun
260b0352107SMichal Meloun static int
rk_usbphy_attach(device_t dev)261b0352107SMichal Meloun rk_usbphy_attach(device_t dev)
262b0352107SMichal Meloun {
263b0352107SMichal Meloun struct rk_usbphy_softc *sc;
264b0352107SMichal Meloun phandle_t node, child;
265b0352107SMichal Meloun int rv;
266b0352107SMichal Meloun
267b0352107SMichal Meloun sc = device_get_softc(dev);
268b0352107SMichal Meloun sc->dev = dev;
269b0352107SMichal Meloun node = ofw_bus_get_node(sc->dev);
270b0352107SMichal Meloun
271b0352107SMichal Meloun /* Attach child devices */
272b0352107SMichal Meloun for (child = OF_child(node); child > 0; child = OF_peer(child)) {
273b0352107SMichal Meloun rv = rk_usbphy_init_phy(sc, child);
274b0352107SMichal Meloun if (rv != 0)
275b0352107SMichal Meloun goto fail;
276b0352107SMichal Meloun }
277b0352107SMichal Meloun return (bus_generic_attach(dev));
278b0352107SMichal Meloun
279b0352107SMichal Meloun fail:
280b0352107SMichal Meloun return (ENXIO);
281b0352107SMichal Meloun }
282b0352107SMichal Meloun
283b0352107SMichal Meloun static int
rk_usbphy_detach(device_t dev)284b0352107SMichal Meloun rk_usbphy_detach(device_t dev)
285b0352107SMichal Meloun {
286b0352107SMichal Meloun return (0);
287b0352107SMichal Meloun }
288b0352107SMichal Meloun
289b0352107SMichal Meloun static device_method_t rk_usbphy_methods[] = {
290b0352107SMichal Meloun /* Device interface */
291b0352107SMichal Meloun DEVMETHOD(device_probe, rk_usbphy_probe),
292b0352107SMichal Meloun DEVMETHOD(device_attach, rk_usbphy_attach),
293b0352107SMichal Meloun DEVMETHOD(device_detach, rk_usbphy_detach),
294b0352107SMichal Meloun DEVMETHOD_END
295b0352107SMichal Meloun };
296b0352107SMichal Meloun
297b0352107SMichal Meloun static DEFINE_CLASS_0(rk_usbphy, rk_usbphy_driver, rk_usbphy_methods,
298b0352107SMichal Meloun sizeof(struct rk_usbphy_softc));
299b2c1681aSJohn Baldwin EARLY_DRIVER_MODULE(rk_usbphy, simplebus, rk_usbphy_driver, NULL, NULL,
300b2c1681aSJohn Baldwin BUS_PASS_TIMER + BUS_PASS_ORDER_LAST);
301