1 /* $NetBSD: rk_usb.c,v 1.3 2018/06/30 18:07:32 jmcneill Exp $ */ 2 3 /*- 4 * Copyright (c) 2018 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: rk_usb.c,v 1.3 2018/06/30 18:07:32 jmcneill 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 #include <sys/kmem.h> 40 41 #include <dev/clk/clk_backend.h> 42 43 #include <dev/fdt/fdtvar.h> 44 #include <dev/fdt/syscon.h> 45 46 static int rk_usb_match(device_t, cfdata_t, void *); 47 static void rk_usb_attach(device_t, device_t, void *); 48 49 #define CON0_REG 0x100 50 #define CON1_REG 0x104 51 #define CON2_REG 0x108 52 #define USBPHY_COMMONONN __BIT(4) 53 54 enum rk_usb_type { 55 USB_RK3328 = 1, 56 }; 57 58 static const struct of_compat_data compat_data[] = { 59 { "rockchip,rk3328-usb2phy", USB_RK3328 }, 60 { NULL } 61 }; 62 63 struct rk_usb_clk { 64 struct clk base; 65 }; 66 67 struct rk_usb_softc { 68 device_t sc_dev; 69 struct syscon *sc_syscon; 70 enum rk_usb_type sc_type; 71 72 struct clk_domain sc_clkdom; 73 struct rk_usb_clk sc_usbclk; 74 }; 75 76 CFATTACH_DECL_NEW(rk_usb, sizeof(struct rk_usb_softc), 77 rk_usb_match, rk_usb_attach, NULL, NULL); 78 79 static struct clk * 80 rk_usb_clk_get(void *priv, const char *name) 81 { 82 struct rk_usb_softc * const sc = priv; 83 84 if (strcmp(name, sc->sc_usbclk.base.name) != 0) 85 return NULL; 86 87 return &sc->sc_usbclk.base; 88 } 89 90 static void 91 rk_usb_clk_put(void *priv, struct clk *clk) 92 { 93 } 94 95 static u_int 96 rk_usb_clk_get_rate(void *priv, struct clk *clk) 97 { 98 return 480000000; 99 } 100 101 static int 102 rk_usb_clk_enable(void *priv, struct clk *clk) 103 { 104 struct rk_usb_softc * const sc = priv; 105 106 const uint32_t write_mask = USBPHY_COMMONONN << 16; 107 const uint32_t write_val = 0; 108 109 syscon_lock(sc->sc_syscon); 110 syscon_write_4(sc->sc_syscon, CON2_REG, write_mask | write_val); 111 syscon_unlock(sc->sc_syscon); 112 113 return 0; 114 } 115 116 static int 117 rk_usb_clk_disable(void *priv, struct clk *clk) 118 { 119 struct rk_usb_softc * const sc = priv; 120 121 const uint32_t write_mask = USBPHY_COMMONONN << 16; 122 const uint32_t write_val = USBPHY_COMMONONN; 123 124 syscon_lock(sc->sc_syscon); 125 syscon_write_4(sc->sc_syscon, CON2_REG, write_mask | write_val); 126 syscon_unlock(sc->sc_syscon); 127 128 return 0; 129 } 130 131 static const struct clk_funcs rk_usb_clk_funcs = { 132 .get = rk_usb_clk_get, 133 .put = rk_usb_clk_put, 134 .get_rate = rk_usb_clk_get_rate, 135 .enable = rk_usb_clk_enable, 136 .disable = rk_usb_clk_disable, 137 }; 138 139 static struct clk * 140 rk_usb_fdt_decode(device_t dev, const void *data, size_t len) 141 { 142 struct rk_usb_softc * const sc = device_private(dev); 143 144 if (len != 0) 145 return NULL; 146 147 return &sc->sc_usbclk.base; 148 } 149 150 static const struct fdtbus_clock_controller_func rk_usb_fdt_funcs = { 151 .decode = rk_usb_fdt_decode 152 }; 153 154 static int 155 rk_usb_match(device_t parent, cfdata_t cf, void *aux) 156 { 157 struct fdt_attach_args * const faa = aux; 158 159 return of_match_compat_data(faa->faa_phandle, compat_data); 160 } 161 162 static void 163 rk_usb_attach(device_t parent, device_t self, void *aux) 164 { 165 struct rk_usb_softc * const sc = device_private(self); 166 struct fdt_attach_args * const faa = aux; 167 const int phandle = faa->faa_phandle; 168 int child; 169 170 sc->sc_dev = self; 171 sc->sc_type = of_search_compatible(phandle, compat_data)->data; 172 sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(phandle)); 173 if (sc->sc_syscon == NULL) { 174 aprint_error(": couldn't get grf syscon\n"); 175 return; 176 } 177 178 const char *clkname = fdtbus_get_string(phandle, "clock-output-names"); 179 if (clkname == NULL) 180 clkname = faa->faa_name; 181 182 sc->sc_clkdom.name = device_xname(self); 183 sc->sc_clkdom.funcs = &rk_usb_clk_funcs; 184 sc->sc_clkdom.priv = sc; 185 sc->sc_usbclk.base.domain = &sc->sc_clkdom; 186 sc->sc_usbclk.base.name = kmem_asprintf("%s", clkname); 187 clk_attach(&sc->sc_usbclk.base); 188 189 aprint_naive("\n"); 190 aprint_normal(": USB2 PHY\n"); 191 192 fdtbus_register_clock_controller(self, phandle, &rk_usb_fdt_funcs); 193 194 for (child = OF_child(phandle); child; child = OF_peer(child)) { 195 if (!fdtbus_status_okay(child)) 196 continue; 197 198 struct fdt_attach_args cfaa = *faa; 199 cfaa.faa_phandle = child; 200 cfaa.faa_name = fdtbus_get_string(child, "name"); 201 cfaa.faa_quiet = false; 202 203 config_found(self, &cfaa, NULL); 204 } 205 } 206 207 /* 208 * USB PHY 209 */ 210 211 static int rk_usbphy_match(device_t, cfdata_t, void *); 212 static void rk_usbphy_attach(device_t, device_t, void *); 213 214 struct rk_usbphy_softc { 215 device_t sc_dev; 216 int sc_phandle; 217 }; 218 219 CFATTACH_DECL_NEW(rk_usbphy, sizeof(struct rk_usbphy_softc), 220 rk_usbphy_match, rk_usbphy_attach, NULL, NULL); 221 222 static void * 223 rk_usbphy_acquire(device_t dev, const void *data, size_t len) 224 { 225 struct rk_usbphy_softc * const sc = device_private(dev); 226 227 if (len != 0) 228 return NULL; 229 230 return sc; 231 } 232 233 static void 234 rk_usbphy_release(device_t dev, void *priv) 235 { 236 } 237 238 static int 239 rk_usbphy_otg_enable(device_t dev, void *priv, bool enable) 240 { 241 struct rk_usb_softc * const usb_sc = device_private(device_parent(dev)); 242 243 const uint32_t write_mask = 0x1ffU << 16; 244 const uint32_t write_val = enable ? 0 : 0x1d1; 245 246 syscon_lock(usb_sc->sc_syscon); 247 syscon_write_4(usb_sc->sc_syscon, CON0_REG, write_mask | write_val); 248 syscon_unlock(usb_sc->sc_syscon); 249 250 return 0; 251 } 252 253 static int 254 rk_usbphy_host_enable(device_t dev, void *priv, bool enable) 255 { 256 struct rk_usb_softc * const usb_sc = device_private(device_parent(dev)); 257 258 const uint32_t write_mask = 0x1ffU << 16; 259 const uint32_t write_val = enable ? 0 : 0x1d1; 260 261 syscon_lock(usb_sc->sc_syscon); 262 syscon_write_4(usb_sc->sc_syscon, CON1_REG, write_mask | write_val); 263 syscon_unlock(usb_sc->sc_syscon); 264 265 return 0; 266 } 267 268 const struct fdtbus_phy_controller_func rk_usbphy_otg_funcs = { 269 .acquire = rk_usbphy_acquire, 270 .release = rk_usbphy_release, 271 .enable = rk_usbphy_otg_enable, 272 }; 273 274 const struct fdtbus_phy_controller_func rk_usbphy_host_funcs = { 275 .acquire = rk_usbphy_acquire, 276 .release = rk_usbphy_release, 277 .enable = rk_usbphy_host_enable, 278 }; 279 280 static int 281 rk_usbphy_match(device_t parent, cfdata_t cf, void *aux) 282 { 283 struct fdt_attach_args * const faa = aux; 284 const int phandle = faa->faa_phandle; 285 const char *name = fdtbus_get_string(phandle, "name"); 286 287 if (strcmp(name, "otg-port") == 0 || strcmp(name, "host-port") == 0) 288 return 1; 289 290 return 0; 291 } 292 293 static void 294 rk_usbphy_attach(device_t parent, device_t self, void *aux) 295 { 296 struct rk_usbphy_softc * const sc = device_private(self); 297 struct fdt_attach_args * const faa = aux; 298 const int phandle = faa->faa_phandle; 299 const char *name = fdtbus_get_string(phandle, "name"); 300 301 sc->sc_dev = self; 302 sc->sc_phandle = phandle; 303 304 aprint_naive("\n"); 305 306 if (strcmp(name, "otg-port") == 0) { 307 aprint_normal(": USB2 OTG port\n"); 308 fdtbus_register_phy_controller(self, phandle, &rk_usbphy_otg_funcs); 309 } else if (strcmp(name, "host-port") == 0) { 310 aprint_normal(": USB2 host port\n"); 311 fdtbus_register_phy_controller(self, phandle, &rk_usbphy_host_funcs); 312 } 313 } 314