1/* $NetBSD: imx6_usbphy.c,v 1.3 2023/05/04 13:29:33 bouyer Exp $ */ 2 3/*- 4 * Copyright (c) 2019 Genetec Corporation. All rights reserved. 5 * Written by Hashimoto Kenichi for Genetec Corporation. 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 AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__KERNEL_RCSID(1, "$NetBSD: imx6_usbphy.c,v 1.3 2023/05/04 13:29:33 bouyer Exp $"); 31 32#include "opt_fdt.h" 33 34#include "locators.h" 35#include "ohci.h" 36#include "ehci.h" 37 38#include <sys/param.h> 39#include <sys/bus.h> 40#include <sys/device.h> 41 42#include <arm/nxp/imx6_usbphyreg.h> 43 44#include <dev/fdt/fdtvar.h> 45 46struct imx6_usbphy_softc { 47 device_t sc_dev; 48 49 bus_space_tag_t sc_iot; 50 bus_space_handle_t sc_ioh; 51 52 struct clk *sc_clk; 53}; 54 55static int imx6_usbphy_match(device_t, cfdata_t, void *); 56static void imx6_usbphy_attach(device_t, device_t, void *); 57 58static int imx6_usbphy_init_clocks(device_t); 59static int imx6_usbphy_enable(device_t, void *, bool); 60 61CFATTACH_DECL_NEW(imxusbphy, sizeof(struct imx6_usbphy_softc), 62 imx6_usbphy_match, imx6_usbphy_attach, NULL, NULL); 63 64static const struct device_compatible_entry compat_data[] = { 65 { .compat = "fsl,imx6q-usbphy" }, 66 { .compat = "fsl,imx6sx-usbphy" }, 67 DEVICE_COMPAT_EOL 68}; 69 70static int 71imx6_usbphy_match(device_t parent, cfdata_t cf, void *aux) 72{ 73 struct fdt_attach_args * const faa = aux; 74 75 return of_compatible_match(faa->faa_phandle, compat_data); 76} 77 78static void 79imx6_usbphy_attach(device_t parent, device_t self, void *aux) 80{ 81 struct imx6_usbphy_softc *sc = device_private(self); 82 struct fdt_attach_args * const faa = aux; 83 const int phandle = faa->faa_phandle; 84 bus_space_tag_t bst = faa->faa_bst; 85 bus_space_handle_t bsh; 86 bus_addr_t addr; 87 bus_size_t size; 88 int error; 89 90 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) { 91 aprint_error(": couldn't get iomux registers\n"); 92 return; 93 } 94 95 error = bus_space_map(bst, addr, size, 0, &bsh); 96 if (error) { 97 aprint_error(": couldn't map %#" PRIxBUSADDR ": %d", addr, error); 98 return; 99 } 100 101 sc->sc_clk = fdtbus_clock_get_index(phandle, 0); 102 if (sc->sc_clk == NULL) { 103 aprint_error(": couldn't get clock\n"); 104 return; 105 } 106 107 sc->sc_dev = self; 108 sc->sc_iot = bst; 109 sc->sc_ioh = bsh; 110 111 aprint_naive("\n"); 112 aprint_normal(": USB PHY\n"); 113 114 imx6_usbphy_init_clocks(self); 115 imx6_usbphy_enable(self, NULL, true); 116} 117 118static int 119imx6_usbphy_init_clocks(device_t dev) 120{ 121 struct imx6_usbphy_softc * const sc = device_private(dev); 122 int error; 123 124 error = clk_enable(sc->sc_clk); 125 if (error) { 126 aprint_error_dev(sc->sc_dev, "couldn't enable: %d\n", error); 127 return error; 128 } 129 130 return 0; 131} 132 133#define USBPHY_READ(sc, reg) \ 134 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, (reg)) 135#define USBPHY_WRITE(sc, reg, val) \ 136 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, (reg), (val)) 137 138static int 139imx6_usbphy_enable(device_t dev, void *priv, bool enable) 140{ 141 struct imx6_usbphy_softc * const sc = device_private(dev); 142 143 /* USBPHY enable */ 144 USBPHY_WRITE(sc, USBPHY_CTRL, USBPHY_CTRL_CLKGATE); 145 146 /* do reset */ 147 USBPHY_WRITE(sc, USBPHY_CTRL_SET, USBPHY_CTRL_SFTRST); 148 delay(100); 149 150 /* clear reset, and run clocks */ 151 USBPHY_WRITE(sc, USBPHY_CTRL_CLR, 152 USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE); 153 delay(100); 154 155 /* power on */ 156 USBPHY_WRITE(sc, USBPHY_PWD, 0); 157 158 /* UTMI+Level2, Level3 */ 159 USBPHY_WRITE(sc, USBPHY_CTRL_SET, 160 USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3); 161 162 return 0; 163} 164 165