1c3e25952SEmmanuel Vadot /*- 2c3e25952SEmmanuel Vadot * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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/cdefs.h> 33c3e25952SEmmanuel Vadot __FBSDID("$FreeBSD$"); 34c3e25952SEmmanuel Vadot 35c3e25952SEmmanuel Vadot #include <sys/param.h> 36c3e25952SEmmanuel Vadot #include <sys/systm.h> 37c3e25952SEmmanuel Vadot #include <sys/bus.h> 38c3e25952SEmmanuel Vadot #include <sys/rman.h> 39c3e25952SEmmanuel Vadot #include <sys/kernel.h> 40c3e25952SEmmanuel Vadot #include <sys/module.h> 41c3e25952SEmmanuel Vadot #include <sys/gpio.h> 42c3e25952SEmmanuel Vadot #include <machine/bus.h> 43c3e25952SEmmanuel Vadot 44c3e25952SEmmanuel Vadot #include <dev/fdt/fdt_common.h> 45c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 46c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 47c3e25952SEmmanuel Vadot #include <dev/ofw/ofw_subr.h> 48c3e25952SEmmanuel Vadot 49c3e25952SEmmanuel Vadot #include <dev/extres/clk/clk.h> 50c3e25952SEmmanuel Vadot #include <dev/extres/phy/phy_usb.h> 51c3e25952SEmmanuel Vadot #include <dev/extres/regulator/regulator.h> 52c3e25952SEmmanuel Vadot #include <dev/extres/syscon/syscon.h> 53c3e25952SEmmanuel Vadot 54c3e25952SEmmanuel Vadot #include "clkdev_if.h" 55c3e25952SEmmanuel Vadot #include "syscon_if.h" 56c3e25952SEmmanuel Vadot 57c3e25952SEmmanuel Vadot #define RK3399_GRF_USB20_PHY0_CON0 0x0 58c3e25952SEmmanuel Vadot #define RK3399_GRF_USB20_PHY0_CON1 0x4 59c3e25952SEmmanuel Vadot #define RK3399_GRF_USB20_PHY0_CON2 0x8 60c3e25952SEmmanuel Vadot #define RK3399_GRF_USB20_PHY0_CON3 0xC 61c3e25952SEmmanuel Vadot 62c3e25952SEmmanuel Vadot struct rk_usb2phy_reg { 63c3e25952SEmmanuel Vadot uint32_t offset; 64c3e25952SEmmanuel Vadot uint32_t enable_mask; 65c3e25952SEmmanuel Vadot uint32_t disable_mask; 66c3e25952SEmmanuel Vadot }; 67c3e25952SEmmanuel Vadot 68c3e25952SEmmanuel Vadot struct rk_usb2phy_regs { 69c3e25952SEmmanuel Vadot struct rk_usb2phy_reg clk_ctl; 70c3e25952SEmmanuel Vadot }; 71c3e25952SEmmanuel Vadot 72c3e25952SEmmanuel Vadot struct rk_usb2phy_regs rk3399_regs = { 73c3e25952SEmmanuel Vadot .clk_ctl = { 74c3e25952SEmmanuel Vadot /* bit 4 put pll in suspend */ 75c3e25952SEmmanuel Vadot .enable_mask = 0x100000, 76c3e25952SEmmanuel Vadot .disable_mask = 0x100010, 77c3e25952SEmmanuel Vadot } 78c3e25952SEmmanuel Vadot }; 79c3e25952SEmmanuel Vadot 80c3e25952SEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 81c3e25952SEmmanuel Vadot { "rockchip,rk3399-usb2phy", (uintptr_t)&rk3399_regs }, 82c3e25952SEmmanuel Vadot { NULL, 0 } 83c3e25952SEmmanuel Vadot }; 84c3e25952SEmmanuel Vadot 85c3e25952SEmmanuel Vadot struct rk_usb2phy_softc { 86c3e25952SEmmanuel Vadot device_t dev; 87c3e25952SEmmanuel Vadot struct syscon *grf; 88c3e25952SEmmanuel Vadot regulator_t phy_supply; 89c3e25952SEmmanuel Vadot clk_t clk; 90c36e2d14SEmmanuel Vadot int mode; 91c3e25952SEmmanuel Vadot }; 92c3e25952SEmmanuel Vadot 93c3e25952SEmmanuel Vadot /* Phy class and methods. */ 94c3e25952SEmmanuel Vadot static int rk_usb2phy_enable(struct phynode *phynode, bool enable); 95c36e2d14SEmmanuel Vadot static int rk_usb2phy_get_mode(struct phynode *phy, int *mode); 96c36e2d14SEmmanuel Vadot static int rk_usb2phy_set_mode(struct phynode *phy, int mode); 97c3e25952SEmmanuel Vadot static phynode_method_t rk_usb2phy_phynode_methods[] = { 98c3e25952SEmmanuel Vadot PHYNODEMETHOD(phynode_enable, rk_usb2phy_enable), 99c36e2d14SEmmanuel Vadot PHYNODEMETHOD(phynode_usb_get_mode, rk_usb2phy_get_mode), 100c36e2d14SEmmanuel Vadot PHYNODEMETHOD(phynode_usb_set_mode, rk_usb2phy_set_mode), 101c3e25952SEmmanuel Vadot 102c3e25952SEmmanuel Vadot PHYNODEMETHOD_END 103c3e25952SEmmanuel Vadot }; 104c3e25952SEmmanuel Vadot 105c3e25952SEmmanuel Vadot DEFINE_CLASS_1(rk_usb2phy_phynode, rk_usb2phy_phynode_class, 106c3e25952SEmmanuel Vadot rk_usb2phy_phynode_methods, 107c3e25952SEmmanuel Vadot sizeof(struct phynode_usb_sc), phynode_usb_class); 108c3e25952SEmmanuel Vadot 109c3e25952SEmmanuel Vadot enum RK3399_USBPHY { 110c3e25952SEmmanuel Vadot RK3399_USBPHY_HOST = 0, 111c3e25952SEmmanuel Vadot RK3399_USBPHY_OTG, 112c3e25952SEmmanuel Vadot }; 113c3e25952SEmmanuel Vadot 114c3e25952SEmmanuel Vadot static int 115c3e25952SEmmanuel Vadot rk_usb2phy_enable(struct phynode *phynode, bool enable) 116c3e25952SEmmanuel Vadot { 117c3e25952SEmmanuel Vadot struct rk_usb2phy_softc *sc; 118c3e25952SEmmanuel Vadot device_t dev; 119c3e25952SEmmanuel Vadot intptr_t phy; 120c3e25952SEmmanuel Vadot int error; 121c3e25952SEmmanuel Vadot 122c3e25952SEmmanuel Vadot dev = phynode_get_device(phynode); 123c3e25952SEmmanuel Vadot phy = phynode_get_id(phynode); 124c3e25952SEmmanuel Vadot sc = device_get_softc(dev); 125c3e25952SEmmanuel Vadot 126c3e25952SEmmanuel Vadot if (phy != RK3399_USBPHY_HOST) 127c3e25952SEmmanuel Vadot return (ERANGE); 128c3e25952SEmmanuel Vadot 129c3e25952SEmmanuel Vadot if (sc->phy_supply) { 130c3e25952SEmmanuel Vadot if (enable) 131c3e25952SEmmanuel Vadot error = regulator_enable(sc->phy_supply); 132c3e25952SEmmanuel Vadot else 133c3e25952SEmmanuel Vadot error = regulator_disable(sc->phy_supply); 134c3e25952SEmmanuel Vadot if (error != 0) { 135c3e25952SEmmanuel Vadot device_printf(dev, "Cannot %sable the regulator\n", 136c3e25952SEmmanuel Vadot enable ? "En" : "Dis"); 137c3e25952SEmmanuel Vadot goto fail; 138c3e25952SEmmanuel Vadot } 139c3e25952SEmmanuel Vadot } 140c3e25952SEmmanuel Vadot 141c3e25952SEmmanuel Vadot return (0); 142c3e25952SEmmanuel Vadot fail: 143c3e25952SEmmanuel Vadot return (ENXIO); 144c3e25952SEmmanuel Vadot } 145c3e25952SEmmanuel Vadot 146c36e2d14SEmmanuel Vadot static int 147c36e2d14SEmmanuel Vadot rk_usb2phy_get_mode(struct phynode *phynode, int *mode) 148c36e2d14SEmmanuel Vadot { 149c36e2d14SEmmanuel Vadot struct rk_usb2phy_softc *sc; 150c36e2d14SEmmanuel Vadot intptr_t phy; 151c36e2d14SEmmanuel Vadot device_t dev; 152c36e2d14SEmmanuel Vadot 153c36e2d14SEmmanuel Vadot dev = phynode_get_device(phynode); 154c36e2d14SEmmanuel Vadot phy = phynode_get_id(phynode); 155c36e2d14SEmmanuel Vadot sc = device_get_softc(dev); 156c36e2d14SEmmanuel Vadot 157c36e2d14SEmmanuel Vadot if (phy != RK3399_USBPHY_HOST) 158c36e2d14SEmmanuel Vadot return (ERANGE); 159c36e2d14SEmmanuel Vadot 160c36e2d14SEmmanuel Vadot *mode = sc->mode; 161c36e2d14SEmmanuel Vadot 162c36e2d14SEmmanuel Vadot return (0); 163c36e2d14SEmmanuel Vadot } 164c36e2d14SEmmanuel Vadot 165c36e2d14SEmmanuel Vadot static int 166c36e2d14SEmmanuel Vadot rk_usb2phy_set_mode(struct phynode *phynode, int mode) 167c36e2d14SEmmanuel Vadot { 168c36e2d14SEmmanuel Vadot struct rk_usb2phy_softc *sc; 169c36e2d14SEmmanuel Vadot intptr_t phy; 170c36e2d14SEmmanuel Vadot device_t dev; 171c36e2d14SEmmanuel Vadot 172c36e2d14SEmmanuel Vadot dev = phynode_get_device(phynode); 173c36e2d14SEmmanuel Vadot phy = phynode_get_id(phynode); 174c36e2d14SEmmanuel Vadot sc = device_get_softc(dev); 175c36e2d14SEmmanuel Vadot 176c36e2d14SEmmanuel Vadot if (phy != RK3399_USBPHY_HOST) 177c36e2d14SEmmanuel Vadot return (ERANGE); 178c36e2d14SEmmanuel Vadot 179c36e2d14SEmmanuel Vadot sc->mode = mode; 180c36e2d14SEmmanuel Vadot 181c36e2d14SEmmanuel Vadot return (0); 182c36e2d14SEmmanuel Vadot } 183c36e2d14SEmmanuel Vadot 184c3e25952SEmmanuel Vadot /* Clock class and method */ 185c3e25952SEmmanuel Vadot struct rk_usb2phy_clk_sc { 186c3e25952SEmmanuel Vadot device_t clkdev; 187c3e25952SEmmanuel Vadot struct syscon *grf; 188c3e25952SEmmanuel Vadot struct rk_usb2phy_regs *regs; 189c3e25952SEmmanuel Vadot }; 190c3e25952SEmmanuel Vadot 191c3e25952SEmmanuel Vadot static int 192c3e25952SEmmanuel Vadot rk_usb2phy_clk_init(struct clknode *clk, device_t dev) 193c3e25952SEmmanuel Vadot { 194c3e25952SEmmanuel Vadot 195c3e25952SEmmanuel Vadot clknode_init_parent_idx(clk, 0); 196c3e25952SEmmanuel Vadot return (0); 197c3e25952SEmmanuel Vadot } 198c3e25952SEmmanuel Vadot 199c3e25952SEmmanuel Vadot static int 200c3e25952SEmmanuel Vadot rk_usb2phy_clk_set_gate(struct clknode *clk, bool enable) 201c3e25952SEmmanuel Vadot { 202c3e25952SEmmanuel Vadot struct rk_usb2phy_clk_sc *sc; 203c3e25952SEmmanuel Vadot 204c3e25952SEmmanuel Vadot sc = clknode_get_softc(clk); 205c3e25952SEmmanuel Vadot 206c3e25952SEmmanuel Vadot if (enable) 207c3e25952SEmmanuel Vadot SYSCON_WRITE_4(sc->grf, sc->regs->clk_ctl.offset, 208c3e25952SEmmanuel Vadot sc->regs->clk_ctl.enable_mask); 209c3e25952SEmmanuel Vadot else 210c3e25952SEmmanuel Vadot SYSCON_WRITE_4(sc->grf, sc->regs->clk_ctl.offset, 211c3e25952SEmmanuel Vadot sc->regs->clk_ctl.disable_mask); 212c3e25952SEmmanuel Vadot return (0); 213c3e25952SEmmanuel Vadot } 214c3e25952SEmmanuel Vadot 215c3e25952SEmmanuel Vadot static int 216c3e25952SEmmanuel Vadot rk_usb2phy_clk_recalc(struct clknode *clk, uint64_t *freq) 217c3e25952SEmmanuel Vadot { 218c3e25952SEmmanuel Vadot 219c3e25952SEmmanuel Vadot *freq = 480000000; 220c3e25952SEmmanuel Vadot 221c3e25952SEmmanuel Vadot return (0); 222c3e25952SEmmanuel Vadot } 223c3e25952SEmmanuel Vadot 224c3e25952SEmmanuel Vadot static clknode_method_t rk_usb2phy_clk_clknode_methods[] = { 225c3e25952SEmmanuel Vadot /* Device interface */ 226c3e25952SEmmanuel Vadot 227c3e25952SEmmanuel Vadot CLKNODEMETHOD(clknode_init, rk_usb2phy_clk_init), 228c3e25952SEmmanuel Vadot CLKNODEMETHOD(clknode_set_gate, rk_usb2phy_clk_set_gate), 229c3e25952SEmmanuel Vadot CLKNODEMETHOD(clknode_recalc_freq, rk_usb2phy_clk_recalc), 230c3e25952SEmmanuel Vadot CLKNODEMETHOD_END 231c3e25952SEmmanuel Vadot }; 232c3e25952SEmmanuel Vadot 233c3e25952SEmmanuel Vadot DEFINE_CLASS_1(rk_usb2phy_clk_clknode, rk_usb2phy_clk_clknode_class, 234c3e25952SEmmanuel Vadot rk_usb2phy_clk_clknode_methods, sizeof(struct rk_usb2phy_clk_sc), 235c3e25952SEmmanuel Vadot clknode_class); 236c3e25952SEmmanuel Vadot 237c3e25952SEmmanuel Vadot static int 238c3e25952SEmmanuel Vadot rk_usb2phy_clk_ofw_map(struct clkdom *clkdom, uint32_t ncells, 239c3e25952SEmmanuel Vadot phandle_t *cells, struct clknode **clk) 240c3e25952SEmmanuel Vadot { 241c3e25952SEmmanuel Vadot 242c3e25952SEmmanuel Vadot if (ncells != 0) 243c3e25952SEmmanuel Vadot return (ERANGE); 244c3e25952SEmmanuel Vadot 245c3e25952SEmmanuel Vadot *clk = clknode_find_by_id(clkdom, 0); 246c3e25952SEmmanuel Vadot 247c3e25952SEmmanuel Vadot if (*clk == NULL) 248c3e25952SEmmanuel Vadot return (ENXIO); 249c3e25952SEmmanuel Vadot return (0); 250c3e25952SEmmanuel Vadot } 251c3e25952SEmmanuel Vadot 252c3e25952SEmmanuel Vadot static int 253c3e25952SEmmanuel Vadot rk_usb2phy_export_clock(struct rk_usb2phy_softc *devsc) 254c3e25952SEmmanuel Vadot { 255c3e25952SEmmanuel Vadot struct clknode_init_def def; 256c3e25952SEmmanuel Vadot struct rk_usb2phy_clk_sc *sc; 257c3e25952SEmmanuel Vadot const char **clknames; 258c3e25952SEmmanuel Vadot struct clkdom *clkdom; 259c3e25952SEmmanuel Vadot struct clknode *clk; 260c3e25952SEmmanuel Vadot clk_t clk_parent; 261c3e25952SEmmanuel Vadot phandle_t node; 262c3e25952SEmmanuel Vadot phandle_t regs[2]; 263c3e25952SEmmanuel Vadot int i, nclocks, ncells, error; 264c3e25952SEmmanuel Vadot 265c3e25952SEmmanuel Vadot node = ofw_bus_get_node(devsc->dev); 266c3e25952SEmmanuel Vadot 267c3e25952SEmmanuel Vadot error = ofw_bus_parse_xref_list_get_length(node, "clocks", 268c3e25952SEmmanuel Vadot "#clock-cells", &ncells); 269c3e25952SEmmanuel Vadot if (error != 0 || ncells != 1) { 270c3e25952SEmmanuel Vadot device_printf(devsc->dev, "couldn't find parent clock\n"); 271c3e25952SEmmanuel Vadot return (ENXIO); 272c3e25952SEmmanuel Vadot } 273c3e25952SEmmanuel Vadot 274c3e25952SEmmanuel Vadot nclocks = ofw_bus_string_list_to_array(node, "clock-output-names", 275c3e25952SEmmanuel Vadot &clknames); 276c3e25952SEmmanuel Vadot if (nclocks != 1) 277c3e25952SEmmanuel Vadot return (ENXIO); 278c3e25952SEmmanuel Vadot 279c3e25952SEmmanuel Vadot clkdom = clkdom_create(devsc->dev); 280c3e25952SEmmanuel Vadot clkdom_set_ofw_mapper(clkdom, rk_usb2phy_clk_ofw_map); 281c3e25952SEmmanuel Vadot 282c3e25952SEmmanuel Vadot memset(&def, 0, sizeof(def)); 283c3e25952SEmmanuel Vadot def.id = 0; 284c3e25952SEmmanuel Vadot def.name = clknames[0]; 285c3e25952SEmmanuel Vadot def.parent_names = malloc(sizeof(char *) * ncells, M_OFWPROP, M_WAITOK); 286c3e25952SEmmanuel Vadot for (i = 0; i < ncells; i++) { 287c3e25952SEmmanuel Vadot error = clk_get_by_ofw_index(devsc->dev, 0, i, &clk_parent); 288c3e25952SEmmanuel Vadot if (error != 0) { 289c3e25952SEmmanuel Vadot device_printf(devsc->dev, "cannot get clock %d\n", error); 290c3e25952SEmmanuel Vadot return (ENXIO); 291c3e25952SEmmanuel Vadot } 292c3e25952SEmmanuel Vadot def.parent_names[i] = clk_get_name(clk_parent); 293c3e25952SEmmanuel Vadot clk_release(clk_parent); 294c3e25952SEmmanuel Vadot } 295c3e25952SEmmanuel Vadot def.parent_cnt = ncells; 296c3e25952SEmmanuel Vadot 297c3e25952SEmmanuel Vadot clk = clknode_create(clkdom, &rk_usb2phy_clk_clknode_class, &def); 298c3e25952SEmmanuel Vadot if (clk == NULL) { 299c3e25952SEmmanuel Vadot device_printf(devsc->dev, "cannot create clknode\n"); 300c3e25952SEmmanuel Vadot return (ENXIO); 301c3e25952SEmmanuel Vadot } 302c3e25952SEmmanuel Vadot 303c3e25952SEmmanuel Vadot sc = clknode_get_softc(clk); 304c3e25952SEmmanuel Vadot sc->clkdev = device_get_parent(devsc->dev); 305c3e25952SEmmanuel Vadot sc->grf = devsc->grf; 306c3e25952SEmmanuel Vadot sc->regs = (struct rk_usb2phy_regs *)ofw_bus_search_compatible(devsc->dev, compat_data)->ocd_data; 307c3e25952SEmmanuel Vadot OF_getencprop(node, "reg", regs, sizeof(regs)); 308c3e25952SEmmanuel Vadot sc->regs->clk_ctl.offset = regs[0]; 309c3e25952SEmmanuel Vadot clknode_register(clkdom, clk); 310c3e25952SEmmanuel Vadot 311c3e25952SEmmanuel Vadot if (clkdom_finit(clkdom) != 0) { 312c3e25952SEmmanuel Vadot device_printf(devsc->dev, "cannot finalize clkdom initialization\n"); 313c3e25952SEmmanuel Vadot return (ENXIO); 314c3e25952SEmmanuel Vadot } 315c3e25952SEmmanuel Vadot 316c3e25952SEmmanuel Vadot if (bootverbose) 317c3e25952SEmmanuel Vadot clkdom_dump(clkdom); 318c3e25952SEmmanuel Vadot 319c3e25952SEmmanuel Vadot return (0); 320c3e25952SEmmanuel Vadot } 321c3e25952SEmmanuel Vadot 322c3e25952SEmmanuel Vadot static int 323c3e25952SEmmanuel Vadot rk_usb2phy_probe(device_t dev) 324c3e25952SEmmanuel Vadot { 325c3e25952SEmmanuel Vadot 326c3e25952SEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 327c3e25952SEmmanuel Vadot return (ENXIO); 328c3e25952SEmmanuel Vadot 329c3e25952SEmmanuel Vadot if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0) 330c3e25952SEmmanuel Vadot return (ENXIO); 331c3e25952SEmmanuel Vadot 332c3e25952SEmmanuel Vadot device_set_desc(dev, "Rockchip RK3399 USB2PHY"); 333c3e25952SEmmanuel Vadot return (BUS_PROBE_DEFAULT); 334c3e25952SEmmanuel Vadot } 335c3e25952SEmmanuel Vadot 336c3e25952SEmmanuel Vadot static int 337c3e25952SEmmanuel Vadot rk_usb2phy_attach(device_t dev) 338c3e25952SEmmanuel Vadot { 339c3e25952SEmmanuel Vadot struct rk_usb2phy_softc *sc; 340c3e25952SEmmanuel Vadot struct phynode_init_def phy_init; 341c3e25952SEmmanuel Vadot struct phynode *phynode; 342c3e25952SEmmanuel Vadot phandle_t node, host; 343c3e25952SEmmanuel Vadot int err; 344c3e25952SEmmanuel Vadot 345c3e25952SEmmanuel Vadot sc = device_get_softc(dev); 346c3e25952SEmmanuel Vadot sc->dev = dev; 347c3e25952SEmmanuel Vadot node = ofw_bus_get_node(dev); 348c3e25952SEmmanuel Vadot 349c3e25952SEmmanuel Vadot if (syscon_get_handle_default(dev, &sc->grf) != 0) { 350c3e25952SEmmanuel Vadot device_printf(dev, "Cannot get syscon handle\n"); 351c3e25952SEmmanuel Vadot return (ENXIO); 352c3e25952SEmmanuel Vadot } 353c3e25952SEmmanuel Vadot 354c3e25952SEmmanuel Vadot if (clk_get_by_ofw_name(dev, 0, "phyclk", &sc->clk) != 0) { 355c3e25952SEmmanuel Vadot device_printf(dev, "Cannot get clock\n"); 356c3e25952SEmmanuel Vadot return (ENXIO); 357c3e25952SEmmanuel Vadot } 358c3e25952SEmmanuel Vadot err = clk_enable(sc->clk); 359c3e25952SEmmanuel Vadot if (err != 0) { 360c3e25952SEmmanuel Vadot device_printf(dev, "Could not enable clock %s\n", 361c3e25952SEmmanuel Vadot clk_get_name(sc->clk)); 362c3e25952SEmmanuel Vadot return (ENXIO); 363c3e25952SEmmanuel Vadot } 364c3e25952SEmmanuel Vadot 365c3e25952SEmmanuel Vadot err = rk_usb2phy_export_clock(sc); 366c3e25952SEmmanuel Vadot if (err != 0) 367c3e25952SEmmanuel Vadot return (err); 368c3e25952SEmmanuel Vadot 369c3e25952SEmmanuel Vadot /* Only host is supported right now */ 370c3e25952SEmmanuel Vadot 371c3e25952SEmmanuel Vadot host = ofw_bus_find_child(node, "host-port"); 372c3e25952SEmmanuel Vadot if (host == 0) { 373c3e25952SEmmanuel Vadot device_printf(dev, "Cannot find host-port child node\n"); 374c3e25952SEmmanuel Vadot return (ENXIO); 375c3e25952SEmmanuel Vadot } 376c3e25952SEmmanuel Vadot 377c3e25952SEmmanuel Vadot if (!ofw_bus_node_status_okay(host)) { 378c3e25952SEmmanuel Vadot device_printf(dev, "host-port isn't okay\n"); 379c3e25952SEmmanuel Vadot return (0); 380c3e25952SEmmanuel Vadot } 381c3e25952SEmmanuel Vadot 382c3e25952SEmmanuel Vadot regulator_get_by_ofw_property(dev, host, "phy-supply", &sc->phy_supply); 383c3e25952SEmmanuel Vadot phy_init.id = RK3399_USBPHY_HOST; 384c3e25952SEmmanuel Vadot phy_init.ofw_node = host; 385c3e25952SEmmanuel Vadot phynode = phynode_create(dev, &rk_usb2phy_phynode_class, &phy_init); 386c3e25952SEmmanuel Vadot if (phynode == NULL) { 387c3e25952SEmmanuel Vadot device_printf(dev, "failed to create host USB2PHY\n"); 388c3e25952SEmmanuel Vadot return (ENXIO); 389c3e25952SEmmanuel Vadot } 390c3e25952SEmmanuel Vadot if (phynode_register(phynode) == NULL) { 391c3e25952SEmmanuel Vadot device_printf(dev, "failed to register host USB2PHY\n"); 392c3e25952SEmmanuel Vadot return (ENXIO); 393c3e25952SEmmanuel Vadot } 394c3e25952SEmmanuel Vadot 395c3e25952SEmmanuel Vadot OF_device_register_xref(OF_xref_from_node(host), dev); 396c3e25952SEmmanuel Vadot 397c3e25952SEmmanuel Vadot return (0); 398c3e25952SEmmanuel Vadot } 399c3e25952SEmmanuel Vadot 400c3e25952SEmmanuel Vadot static device_method_t rk_usb2phy_methods[] = { 401c3e25952SEmmanuel Vadot /* Device interface */ 402c3e25952SEmmanuel Vadot DEVMETHOD(device_probe, rk_usb2phy_probe), 403c3e25952SEmmanuel Vadot DEVMETHOD(device_attach, rk_usb2phy_attach), 404c3e25952SEmmanuel Vadot 405c3e25952SEmmanuel Vadot DEVMETHOD_END 406c3e25952SEmmanuel Vadot }; 407c3e25952SEmmanuel Vadot 408c3e25952SEmmanuel Vadot static driver_t rk_usb2phy_driver = { 409c3e25952SEmmanuel Vadot "rk_usb2phy", 410c3e25952SEmmanuel Vadot rk_usb2phy_methods, 411c3e25952SEmmanuel Vadot sizeof(struct rk_usb2phy_softc) 412c3e25952SEmmanuel Vadot }; 413c3e25952SEmmanuel Vadot 414*b2c1681aSJohn Baldwin EARLY_DRIVER_MODULE(rk_usb2phy, simplebus, rk_usb2phy_driver, 0, 0, 415*b2c1681aSJohn Baldwin BUS_PASS_SUPPORTDEV + BUS_PASS_ORDER_MIDDLE); 416c3e25952SEmmanuel Vadot MODULE_VERSION(rk_usb2phy, 1); 417