1 /* $NetBSD: usbnopphy.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2019 Jared McNeill <jmcneill@invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30
31 __KERNEL_RCSID(0, "$NetBSD: usbnopphy.c,v 1.2 2021/01/27 03:10:21 thorpej Exp $");
32
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/device.h>
36 #include <sys/intr.h>
37 #include <sys/systm.h>
38 #include <sys/time.h>
39
40 #include <dev/fdt/fdtvar.h>
41
42 static int usbnopphy_match(device_t, cfdata_t, void *);
43 static void usbnopphy_attach(device_t, device_t, void *);
44
45 static const struct device_compatible_entry compat_data[] = {
46 { .compat = "usb-nop-xceiv" },
47 DEVICE_COMPAT_EOL
48 };
49
50 struct usbnopphy_softc {
51 device_t sc_dev;
52
53 struct clk *sc_clk;
54 struct fdtbus_regulator *sc_reg;
55 struct fdtbus_gpio_pin *sc_pin_reset;
56 struct fdtbus_gpio_pin *sc_pin_vbus_det;
57 };
58
59 CFATTACH_DECL_NEW(usbnopphy, sizeof(struct usbnopphy_softc),
60 usbnopphy_match, usbnopphy_attach, NULL, NULL);
61
62 static void *
usbnopphy_acquire(device_t dev,const void * data,size_t len)63 usbnopphy_acquire(device_t dev, const void *data, size_t len)
64 {
65 struct usbnopphy_softc * const sc = device_private(dev);
66
67 return sc;
68 }
69
70 static void
usbnopphy_release(device_t dev,void * priv)71 usbnopphy_release(device_t dev, void *priv)
72 {
73 }
74
75 static int
usbnopphy_enable(device_t dev,void * priv,bool enable)76 usbnopphy_enable(device_t dev, void *priv, bool enable)
77 {
78 struct usbnopphy_softc * const sc = device_private(dev);
79 int error;
80
81 if (enable) {
82 if (sc->sc_reg != NULL) {
83 error = fdtbus_regulator_enable(sc->sc_reg);
84 if (error != 0)
85 return error;
86 }
87 if (sc->sc_clk != NULL) {
88 error = clk_enable(sc->sc_clk);
89 if (error != 0)
90 return error;
91 }
92 if (sc->sc_pin_reset != NULL) {
93 fdtbus_gpio_write(sc->sc_pin_reset, 1);
94 delay(20000);
95 fdtbus_gpio_write(sc->sc_pin_reset, 0);
96 }
97 } else {
98 if (sc->sc_pin_reset != NULL)
99 fdtbus_gpio_write(sc->sc_pin_reset, 1);
100 if (sc->sc_reg != NULL)
101 fdtbus_regulator_disable(sc->sc_reg);
102 if (sc->sc_clk != NULL)
103 clk_disable(sc->sc_clk);
104 }
105
106 return 0;
107 }
108
109 const struct fdtbus_phy_controller_func usbnopphy_funcs = {
110 .acquire = usbnopphy_acquire,
111 .release = usbnopphy_release,
112 .enable = usbnopphy_enable,
113 };
114
115 static int
usbnopphy_match(device_t parent,cfdata_t cf,void * aux)116 usbnopphy_match(device_t parent, cfdata_t cf, void *aux)
117 {
118 struct fdt_attach_args * const faa = aux;
119
120 return of_compatible_match(faa->faa_phandle, compat_data);
121 }
122
123 static void
usbnopphy_attach(device_t parent,device_t self,void * aux)124 usbnopphy_attach(device_t parent, device_t self, void *aux)
125 {
126 struct usbnopphy_softc * const sc = device_private(self);
127 struct fdt_attach_args * const faa = aux;
128 const int phandle = faa->faa_phandle;
129
130 sc->sc_dev = self;
131
132 sc->sc_reg = fdtbus_regulator_acquire(phandle, "vbus-regulator");
133 sc->sc_clk = fdtbus_clock_get(phandle, "main_clk");
134 sc->sc_pin_reset = fdtbus_gpio_acquire(phandle, "reset-gpios", GPIO_PIN_OUTPUT);
135
136 aprint_naive("\n");
137 aprint_normal(": USB PHY\n");
138
139 fdtbus_register_phy_controller(self, phandle, &usbnopphy_funcs);
140 }
141