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