Home | History | Annotate | Line # | Download | only in rockchip
      1 /* $NetBSD: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 jmcneill Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2021 Jared McNeill <jmcneill (at) 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: rk3288_usb.c,v 1.1 2021/11/12 22:02:08 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/fdt/fdtvar.h>
     42 #include <dev/fdt/syscon.h>
     43 
     44 #define	GRF_UOCn_CON0_SIDDQ	__BIT(13)
     45 
     46 static int rk3288_usb_match(device_t, cfdata_t, void *);
     47 static void rk3288_usb_attach(device_t, device_t, void *);
     48 
     49 static const struct device_compatible_entry compat_data[] = {
     50 	{ .compat = "rockchip,rk3288-usb-phy" },
     51 	DEVICE_COMPAT_EOL
     52 };
     53 
     54 CFATTACH_DECL_NEW(rk3288_usb, 0,
     55 	rk3288_usb_match, rk3288_usb_attach, NULL, NULL);
     56 
     57 static int
     58 rk3288_usb_match(device_t parent, cfdata_t cf, void *aux)
     59 {
     60 	struct fdt_attach_args * const faa = aux;
     61 
     62 	return of_compatible_match(faa->faa_phandle, compat_data);
     63 }
     64 
     65 static void
     66 rk3288_usb_attach(device_t parent, device_t self, void *aux)
     67 {
     68 	struct fdt_attach_args * const faa = aux;
     69 	const int phandle = faa->faa_phandle;
     70 	int child;
     71 
     72 	aprint_naive("\n");
     73 	aprint_normal(": USB PHY\n");
     74 
     75 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
     76 		if (!fdtbus_status_okay(child))
     77 			continue;
     78 
     79 		struct fdt_attach_args cfaa = *faa;
     80 		cfaa.faa_phandle = child;
     81 		cfaa.faa_name = fdtbus_get_string(child, "name");
     82 		cfaa.faa_quiet = false;
     83 
     84 		config_found(self, &cfaa, NULL, CFARGS_NONE);
     85 	}
     86 }
     87 
     88 /*
     89  * USB PHY
     90  */
     91 
     92 static int rk3288_usbphy_match(device_t, cfdata_t, void *);
     93 static void rk3288_usbphy_attach(device_t, device_t, void *);
     94 
     95 struct rk3288_usbphy_softc {
     96 	device_t	sc_dev;
     97 	int		sc_phandle;
     98 	struct syscon	*sc_syscon;
     99 	bus_addr_t	sc_reg;
    100 };
    101 
    102 CFATTACH_DECL_NEW(rk3288_usbphy, sizeof(struct rk3288_usbphy_softc),
    103 	rk3288_usbphy_match, rk3288_usbphy_attach, NULL, NULL);
    104 
    105 static void *
    106 rk3288_usbphy_acquire(device_t dev, const void *data, size_t len)
    107 {
    108 	struct rk3288_usbphy_softc * const sc = device_private(dev);
    109 
    110 	if (len != 0)
    111 		return NULL;
    112 
    113 	return sc;
    114 }
    115 
    116 static void
    117 rk3288_usbphy_release(device_t dev, void *priv)
    118 {
    119 }
    120 
    121 static int
    122 rk3288_usbphy_enable(device_t dev, void *priv, bool enable)
    123 {
    124 	struct rk3288_usbphy_softc * const sc = device_private(dev);
    125 	uint32_t write_mask, write_val;
    126 
    127 	write_mask = GRF_UOCn_CON0_SIDDQ << 16;
    128 	write_val = enable ? 0 : GRF_UOCn_CON0_SIDDQ;
    129 
    130 	syscon_lock(sc->sc_syscon);
    131 	syscon_write_4(sc->sc_syscon, sc->sc_reg, write_mask | write_val);
    132 	syscon_unlock(sc->sc_syscon);
    133 
    134 	return 0;
    135 }
    136 
    137 const struct fdtbus_phy_controller_func rk3288_usbphy_funcs = {
    138 	.acquire = rk3288_usbphy_acquire,
    139 	.release = rk3288_usbphy_release,
    140 	.enable = rk3288_usbphy_enable,
    141 };
    142 
    143 static int
    144 rk3288_usbphy_match(device_t parent, cfdata_t cf, void *aux)
    145 {
    146 	return 1;
    147 }
    148 
    149 static void
    150 rk3288_usbphy_attach(device_t parent, device_t self, void *aux)
    151 {
    152 	struct rk3288_usbphy_softc * const sc = device_private(self);
    153 	struct fdt_attach_args * const faa = aux;
    154 	const int phandle = faa->faa_phandle;
    155 	struct fdtbus_reset *rst;
    156 
    157 	sc->sc_dev = self;
    158 	sc->sc_phandle = phandle;
    159 	sc->sc_syscon = fdtbus_syscon_lookup(OF_parent(OF_parent(phandle)));
    160 	if (sc->sc_syscon == NULL) {
    161 		aprint_error(": couldn't find syscon\n");
    162 		return;
    163 	}
    164 	if (fdtbus_get_reg(phandle, 0, &sc->sc_reg, NULL) != 0) {
    165 		aprint_error(": couldn't find registers\n");
    166 		return;
    167 	}
    168 
    169 	rst = fdtbus_reset_get(phandle, "phy-reset");
    170 	if (rst == NULL || fdtbus_reset_deassert(rst) != 0) {
    171 		aprint_error(": couldn't de-assert reset\n");
    172 		return;
    173 	}
    174 	if (fdtbus_clock_enable(phandle, "phyclk", true) != 0) {
    175 		aprint_error(": couildn't enable clock\n");
    176 		return;
    177 	}
    178 
    179 	aprint_naive("\n");
    180 	aprint_normal(": USB port\n");
    181 
    182 	fdtbus_register_phy_controller(self, phandle, &rk3288_usbphy_funcs);
    183 }
    184