1 /* $NetBSD: ti_motgphy.c,v 1.3 2026/07/30 15:08:08 yurix Exp $ */ 2 /*- 3 * Copyright (c) 2026 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Yuri Honegger and Brook Milligan. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Driver for the USB phy on TI AM335X SoCs. 33 * 34 * While we implement fdt_phy, we only have support for enabling all phys at 35 * once. Enabling individual phys is possible, but it requires parsing the alias 36 * name of the phy node, which is beyond ugly. Furthermore, the usb controller 37 * doesn't support suspend/resume, so we only ever enable the phy. If the need 38 * for a full phy implementation arises, it should be possible to extend the 39 * driver without much restructuring. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: ti_motgphy.c,v 1.3 2026/07/30 15:08:08 yurix Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/bus.h> 47 #include <sys/device.h> 48 49 #include <arm/ti/ti_otgreg.h> 50 51 #include <dev/fdt/fdtvar.h> 52 53 struct ti_motgphy_softc { 54 bus_space_tag_t sc_bst; 55 bus_space_handle_t sc_bsh; 56 }; 57 58 static int ti_motgphy_match(device_t, cfdata_t, void *); 59 static void ti_motgphy_attach(device_t, device_t, void *); 60 static void * ti_motgphy_acquire(device_t, const void *, size_t); 61 static void ti_motgphy_release(device_t, void *); 62 static int ti_motgphy_enable(device_t, void *, bool); 63 64 #define PHY_READ(sc, reg) \ 65 bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, reg) 66 #define PHY_WRITE(sc, reg, val) \ 67 bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, reg, val) 68 69 CFATTACH_DECL_NEW(timotgphy, sizeof(struct ti_motgphy_softc), ti_motgphy_match, 70 ti_motgphy_attach, NULL, NULL); 71 72 static const struct fdtbus_phy_controller_func ti_motgphy_funcs = { 73 .acquire = &ti_motgphy_acquire, 74 .release = &ti_motgphy_release, 75 .enable = &ti_motgphy_enable, 76 }; 77 78 static const struct device_compatible_entry compat_data[] = { 79 { .compat = "ti,am335x-usb-phy" }, 80 DEVICE_COMPAT_EOL 81 }; 82 83 static int 84 ti_motgphy_match(device_t parent, cfdata_t match, void *aux) 85 { 86 struct fdt_attach_args * const faa = aux; 87 88 return of_compatible_match(faa->faa_phandle, compat_data); 89 } 90 91 static void 92 ti_motgphy_attach(device_t parent, device_t self, void *aux) 93 { 94 struct ti_motgphy_softc *sc = device_private(self); 95 struct fdt_attach_args *const faa = aux; 96 const int phandle = faa->faa_phandle; 97 int ctrl_phandle; 98 bus_addr_t addr; 99 bus_size_t size; 100 101 ctrl_phandle = fdtbus_get_phandle(phandle, "ti,ctrl_mod"); 102 if (ctrl_phandle < 0) { 103 aprint_error(": failed to get usb phy ctrl module\n"); 104 return; 105 } 106 107 if (fdtbus_get_reg_byname(ctrl_phandle, "phy_ctrl", &addr, &size)) { 108 aprint_error(": couldn't get registers\n"); 109 return; 110 } 111 112 sc->sc_bst = faa->faa_bst; 113 if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) { 114 aprint_error(": couldn't map registers\n"); 115 return; 116 } 117 118 aprint_normal("\n"); 119 aprint_naive("\n"); 120 121 fdtbus_register_phy_controller(self, phandle, &ti_motgphy_funcs); 122 } 123 124 static void * 125 ti_motgphy_acquire(device_t self, const void *data, size_t len) 126 { 127 KASSERT(len == 0); 128 return device_private(self); 129 } 130 131 static void 132 ti_motgphy_release(device_t self, void *priv) 133 { 134 /* do nothing */ 135 } 136 137 static int 138 ti_motgphy_enable(device_t self, void *priv, bool enable) 139 { 140 struct ti_motgphy_softc * const sc = priv; 141 142 /* 143 * We only support enabling the phy, and even then we always enable both 144 * phys at once. 145 */ 146 KASSERT(enable); 147 uint32_t usb_ctrl; 148 for (int i = 0; i < 2; ++i) { 149 usb_ctrl = PHY_READ(sc, CM_USBCTRL(i)); 150 usb_ctrl &= ~(CM_USBCTRL_CM_PWRDN | CM_USBCTRL_OTG_PWRDN); 151 usb_ctrl |= (CM_USBCTRL_OTGVDET_EN | CM_USBCTRL_OTGSESSENDEN); 152 PHY_WRITE(sc, CM_USBCTRL(i), usb_ctrl); 153 } 154 155 return 0; 156 } 157 158