Home | History | Annotate | Line # | Download | only in rockchip
rk_usb.c revision 1.1
      1  1.1  jmcneill /* $NetBSD: rk_usb.c,v 1.1 2018/06/16 00:19:04 jmcneill Exp $ */
      2  1.1  jmcneill 
      3  1.1  jmcneill /*-
      4  1.1  jmcneill  * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
      5  1.1  jmcneill  * All rights reserved.
      6  1.1  jmcneill  *
      7  1.1  jmcneill  * Redistribution and use in source and binary forms, with or without
      8  1.1  jmcneill  * modification, are permitted provided that the following conditions
      9  1.1  jmcneill  * are met:
     10  1.1  jmcneill  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jmcneill  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jmcneill  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jmcneill  *    documentation and/or other materials provided with the distribution.
     15  1.1  jmcneill  *
     16  1.1  jmcneill  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1  jmcneill  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jmcneill  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jmcneill  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1  jmcneill  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1  jmcneill  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1  jmcneill  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1  jmcneill  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1  jmcneill  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1  jmcneill  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1  jmcneill  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jmcneill  */
     28  1.1  jmcneill 
     29  1.1  jmcneill #include <sys/cdefs.h>
     30  1.1  jmcneill 
     31  1.1  jmcneill __KERNEL_RCSID(0, "$NetBSD: rk_usb.c,v 1.1 2018/06/16 00:19:04 jmcneill Exp $");
     32  1.1  jmcneill 
     33  1.1  jmcneill #include <sys/param.h>
     34  1.1  jmcneill #include <sys/bus.h>
     35  1.1  jmcneill #include <sys/device.h>
     36  1.1  jmcneill #include <sys/intr.h>
     37  1.1  jmcneill #include <sys/systm.h>
     38  1.1  jmcneill #include <sys/time.h>
     39  1.1  jmcneill #include <sys/kmem.h>
     40  1.1  jmcneill 
     41  1.1  jmcneill #include <dev/clk/clk_backend.h>
     42  1.1  jmcneill 
     43  1.1  jmcneill #include <dev/fdt/fdtvar.h>
     44  1.1  jmcneill 
     45  1.1  jmcneill static int rk_usb_match(device_t, cfdata_t, void *);
     46  1.1  jmcneill static void rk_usb_attach(device_t, device_t, void *);
     47  1.1  jmcneill 
     48  1.1  jmcneill #define	CON0_REG	0x00
     49  1.1  jmcneill #define	CON1_REG	0x04
     50  1.1  jmcneill #define	CON2_REG	0x08
     51  1.1  jmcneill #define	 USBPHY_COMMONONN	__BIT(4)
     52  1.1  jmcneill 
     53  1.1  jmcneill enum rk_usb_type {
     54  1.1  jmcneill 	USB_RK3328 = 1,
     55  1.1  jmcneill };
     56  1.1  jmcneill 
     57  1.1  jmcneill static const struct of_compat_data compat_data[] = {
     58  1.1  jmcneill 	{ "rockchip,rk3328-usb2phy",		USB_RK3328 },
     59  1.1  jmcneill 	{ NULL }
     60  1.1  jmcneill };
     61  1.1  jmcneill 
     62  1.1  jmcneill struct rk_usb_clk {
     63  1.1  jmcneill 	struct clk		base;
     64  1.1  jmcneill 	bus_size_t		reg;
     65  1.1  jmcneill };
     66  1.1  jmcneill 
     67  1.1  jmcneill struct rk_usb_softc {
     68  1.1  jmcneill 	device_t		sc_dev;
     69  1.1  jmcneill 	bus_space_tag_t		sc_bst;
     70  1.1  jmcneill 	bus_space_handle_t	sc_bsh;
     71  1.1  jmcneill 	enum rk_usb_type	sc_type;
     72  1.1  jmcneill 
     73  1.1  jmcneill 	struct clk_domain	sc_clkdom;
     74  1.1  jmcneill 	struct rk_usb_clk	sc_usbclk;
     75  1.1  jmcneill };
     76  1.1  jmcneill 
     77  1.1  jmcneill #define USB_READ(sc, reg)			\
     78  1.1  jmcneill 	bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     79  1.1  jmcneill #define USB_WRITE(sc, reg, val)			\
     80  1.1  jmcneill 	bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     81  1.1  jmcneill 
     82  1.1  jmcneill CFATTACH_DECL_NEW(rk_usb, sizeof(struct rk_usb_softc),
     83  1.1  jmcneill 	rk_usb_match, rk_usb_attach, NULL, NULL);
     84  1.1  jmcneill 
     85  1.1  jmcneill static struct clk *
     86  1.1  jmcneill rk_usb_clk_get(void *priv, const char *name)
     87  1.1  jmcneill {
     88  1.1  jmcneill 	struct rk_usb_softc * const sc = priv;
     89  1.1  jmcneill 
     90  1.1  jmcneill 	if (strcmp(name, sc->sc_usbclk.base.name) != 0)
     91  1.1  jmcneill 		return NULL;
     92  1.1  jmcneill 
     93  1.1  jmcneill 	return &sc->sc_usbclk.base;
     94  1.1  jmcneill }
     95  1.1  jmcneill 
     96  1.1  jmcneill static void
     97  1.1  jmcneill rk_usb_clk_put(void *priv, struct clk *clk)
     98  1.1  jmcneill {
     99  1.1  jmcneill }
    100  1.1  jmcneill 
    101  1.1  jmcneill static u_int
    102  1.1  jmcneill rk_usb_clk_get_rate(void *priv, struct clk *clk)
    103  1.1  jmcneill {
    104  1.1  jmcneill 	return 480000000;
    105  1.1  jmcneill }
    106  1.1  jmcneill 
    107  1.1  jmcneill static int
    108  1.1  jmcneill rk_usb_clk_enable(void *priv, struct clk *clk)
    109  1.1  jmcneill {
    110  1.1  jmcneill 	struct rk_usb_softc * const sc = priv;
    111  1.1  jmcneill 
    112  1.1  jmcneill 	const uint32_t write_mask = USBPHY_COMMONONN << 16;
    113  1.1  jmcneill 	const uint32_t write_val = 0;
    114  1.1  jmcneill 	USB_WRITE(sc, CON2_REG, write_mask | write_val);
    115  1.1  jmcneill 
    116  1.1  jmcneill 	return 0;
    117  1.1  jmcneill }
    118  1.1  jmcneill 
    119  1.1  jmcneill static int
    120  1.1  jmcneill rk_usb_clk_disable(void *priv, struct clk *clk)
    121  1.1  jmcneill {
    122  1.1  jmcneill 	struct rk_usb_softc * const sc = priv;
    123  1.1  jmcneill 
    124  1.1  jmcneill 	const uint32_t write_mask = USBPHY_COMMONONN << 16;
    125  1.1  jmcneill 	const uint32_t write_val = USBPHY_COMMONONN;
    126  1.1  jmcneill 	USB_WRITE(sc, CON2_REG, write_mask | write_val);
    127  1.1  jmcneill 
    128  1.1  jmcneill 	return 0;
    129  1.1  jmcneill }
    130  1.1  jmcneill 
    131  1.1  jmcneill static const struct clk_funcs rk_usb_clk_funcs = {
    132  1.1  jmcneill 	.get = rk_usb_clk_get,
    133  1.1  jmcneill 	.put = rk_usb_clk_put,
    134  1.1  jmcneill 	.get_rate = rk_usb_clk_get_rate,
    135  1.1  jmcneill 	.enable = rk_usb_clk_enable,
    136  1.1  jmcneill 	.disable = rk_usb_clk_disable,
    137  1.1  jmcneill };
    138  1.1  jmcneill 
    139  1.1  jmcneill static struct clk *
    140  1.1  jmcneill rk_usb_fdt_decode(device_t dev, const void *data, size_t len)
    141  1.1  jmcneill {
    142  1.1  jmcneill 	struct rk_usb_softc * const sc = device_private(dev);
    143  1.1  jmcneill 
    144  1.1  jmcneill 	if (len != 0)
    145  1.1  jmcneill 		return NULL;
    146  1.1  jmcneill 
    147  1.1  jmcneill 	return &sc->sc_usbclk.base;
    148  1.1  jmcneill }
    149  1.1  jmcneill 
    150  1.1  jmcneill static const struct fdtbus_clock_controller_func rk_usb_fdt_funcs = {
    151  1.1  jmcneill 	.decode = rk_usb_fdt_decode
    152  1.1  jmcneill };
    153  1.1  jmcneill 
    154  1.1  jmcneill static int
    155  1.1  jmcneill rk_usb_match(device_t parent, cfdata_t cf, void *aux)
    156  1.1  jmcneill {
    157  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    158  1.1  jmcneill 
    159  1.1  jmcneill 	return of_match_compat_data(faa->faa_phandle, compat_data);
    160  1.1  jmcneill }
    161  1.1  jmcneill 
    162  1.1  jmcneill static void
    163  1.1  jmcneill rk_usb_attach(device_t parent, device_t self, void *aux)
    164  1.1  jmcneill {
    165  1.1  jmcneill 	struct rk_usb_softc * const sc = device_private(self);
    166  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    167  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    168  1.1  jmcneill 	bus_addr_t grf_addr, phy_addr, phy_size;
    169  1.1  jmcneill 	int child;
    170  1.1  jmcneill 
    171  1.1  jmcneill 	sc->sc_dev = self;
    172  1.1  jmcneill 	sc->sc_bst = faa->faa_bst;
    173  1.1  jmcneill 	sc->sc_type = of_search_compatible(phandle, compat_data)->data;
    174  1.1  jmcneill 
    175  1.1  jmcneill 	if (fdtbus_get_reg(OF_parent(phandle), 0, &grf_addr, NULL) != 0) {
    176  1.1  jmcneill 		aprint_error(": couldn't get grf registers\n");
    177  1.1  jmcneill 	}
    178  1.1  jmcneill 	if (fdtbus_get_reg(phandle, 0, &phy_addr, &phy_size) != 0) {
    179  1.1  jmcneill 		aprint_error(": couldn't get phy registers\n");
    180  1.1  jmcneill 		return;
    181  1.1  jmcneill 	}
    182  1.1  jmcneill 	if (bus_space_map(sc->sc_bst, grf_addr + phy_addr, phy_size, 0, &sc->sc_bsh) != 0) {
    183  1.1  jmcneill 		aprint_error(": couldn't map phy registers\n");
    184  1.1  jmcneill 		return;
    185  1.1  jmcneill 	}
    186  1.1  jmcneill 
    187  1.1  jmcneill 	const char *clkname = fdtbus_get_string(phandle, "clock-output-names");
    188  1.1  jmcneill 	if (clkname == NULL)
    189  1.1  jmcneill 		clkname = faa->faa_name;
    190  1.1  jmcneill 
    191  1.1  jmcneill 	sc->sc_clkdom.name = device_xname(self);
    192  1.1  jmcneill 	sc->sc_clkdom.funcs = &rk_usb_clk_funcs;
    193  1.1  jmcneill 	sc->sc_clkdom.priv = sc;
    194  1.1  jmcneill 	sc->sc_usbclk.base.domain = &sc->sc_clkdom;
    195  1.1  jmcneill 	sc->sc_usbclk.base.name = kmem_asprintf("%s", clkname);
    196  1.1  jmcneill 	clk_attach(&sc->sc_usbclk.base);
    197  1.1  jmcneill 
    198  1.1  jmcneill 	aprint_naive("\n");
    199  1.1  jmcneill 	aprint_normal(": USB2 PHY\n");
    200  1.1  jmcneill 
    201  1.1  jmcneill 	fdtbus_register_clock_controller(self, phandle, &rk_usb_fdt_funcs);
    202  1.1  jmcneill 
    203  1.1  jmcneill 	for (child = OF_child(phandle); child; child = OF_peer(child)) {
    204  1.1  jmcneill 		if (!fdtbus_status_okay(child))
    205  1.1  jmcneill 			continue;
    206  1.1  jmcneill 
    207  1.1  jmcneill 		struct fdt_attach_args cfaa = *faa;
    208  1.1  jmcneill 		cfaa.faa_phandle = child;
    209  1.1  jmcneill 		cfaa.faa_name = fdtbus_get_string(child, "name");
    210  1.1  jmcneill 		cfaa.faa_quiet = false;
    211  1.1  jmcneill 
    212  1.1  jmcneill 		config_found(self, &cfaa, NULL);
    213  1.1  jmcneill 	}
    214  1.1  jmcneill }
    215  1.1  jmcneill 
    216  1.1  jmcneill /*
    217  1.1  jmcneill  * USB PHY
    218  1.1  jmcneill  */
    219  1.1  jmcneill 
    220  1.1  jmcneill static int rk_usbphy_match(device_t, cfdata_t, void *);
    221  1.1  jmcneill static void rk_usbphy_attach(device_t, device_t, void *);
    222  1.1  jmcneill 
    223  1.1  jmcneill struct rk_usbphy_softc {
    224  1.1  jmcneill 	device_t	sc_dev;
    225  1.1  jmcneill 	int		sc_phandle;
    226  1.1  jmcneill };
    227  1.1  jmcneill 
    228  1.1  jmcneill CFATTACH_DECL_NEW(rk_usbphy, sizeof(struct rk_usbphy_softc),
    229  1.1  jmcneill 	rk_usbphy_match, rk_usbphy_attach, NULL, NULL);
    230  1.1  jmcneill 
    231  1.1  jmcneill static void *
    232  1.1  jmcneill rk_usbphy_acquire(device_t dev, const void *data, size_t len)
    233  1.1  jmcneill {
    234  1.1  jmcneill 	struct rk_usbphy_softc * const sc = device_private(dev);
    235  1.1  jmcneill 
    236  1.1  jmcneill 	if (len != 0)
    237  1.1  jmcneill 		return NULL;
    238  1.1  jmcneill 
    239  1.1  jmcneill 	return sc;
    240  1.1  jmcneill }
    241  1.1  jmcneill 
    242  1.1  jmcneill static void
    243  1.1  jmcneill rk_usbphy_release(device_t dev, void *priv)
    244  1.1  jmcneill {
    245  1.1  jmcneill }
    246  1.1  jmcneill 
    247  1.1  jmcneill static int
    248  1.1  jmcneill rk_usbphy_otg_enable(device_t dev, void *priv, bool enable)
    249  1.1  jmcneill {
    250  1.1  jmcneill 	struct rk_usb_softc * const usb_sc = device_private(device_parent(dev));
    251  1.1  jmcneill 
    252  1.1  jmcneill 	const uint32_t write_mask = 0x1ffU << 16;
    253  1.1  jmcneill 	const uint32_t write_val = enable ? 0 : 0x1d1;
    254  1.1  jmcneill 	USB_WRITE(usb_sc, CON0_REG, write_mask | write_val);
    255  1.1  jmcneill 
    256  1.1  jmcneill 	return 0;
    257  1.1  jmcneill }
    258  1.1  jmcneill 
    259  1.1  jmcneill static int
    260  1.1  jmcneill rk_usbphy_host_enable(device_t dev, void *priv, bool enable)
    261  1.1  jmcneill {
    262  1.1  jmcneill 	struct rk_usb_softc * const usb_sc = device_private(device_parent(dev));
    263  1.1  jmcneill 
    264  1.1  jmcneill 	const uint32_t write_mask = 0x1ffU << 16;
    265  1.1  jmcneill 	const uint32_t write_val = enable ? 0 : 0x1d1;
    266  1.1  jmcneill 	USB_WRITE(usb_sc, CON1_REG, write_mask | write_val);
    267  1.1  jmcneill 
    268  1.1  jmcneill 	return 0;
    269  1.1  jmcneill }
    270  1.1  jmcneill 
    271  1.1  jmcneill const struct fdtbus_phy_controller_func rk_usbphy_otg_funcs = {
    272  1.1  jmcneill 	.acquire = rk_usbphy_acquire,
    273  1.1  jmcneill 	.release = rk_usbphy_release,
    274  1.1  jmcneill 	.enable = rk_usbphy_otg_enable,
    275  1.1  jmcneill };
    276  1.1  jmcneill 
    277  1.1  jmcneill const struct fdtbus_phy_controller_func rk_usbphy_host_funcs = {
    278  1.1  jmcneill 	.acquire = rk_usbphy_acquire,
    279  1.1  jmcneill 	.release = rk_usbphy_release,
    280  1.1  jmcneill 	.enable = rk_usbphy_host_enable,
    281  1.1  jmcneill };
    282  1.1  jmcneill 
    283  1.1  jmcneill static int
    284  1.1  jmcneill rk_usbphy_match(device_t parent, cfdata_t cf, void *aux)
    285  1.1  jmcneill {
    286  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    287  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    288  1.1  jmcneill 	const char *name = fdtbus_get_string(phandle, "name");
    289  1.1  jmcneill 
    290  1.1  jmcneill 	if (strcmp(name, "otg-port") == 0 || strcmp(name, "host-port") == 0)
    291  1.1  jmcneill 		return 1;
    292  1.1  jmcneill 
    293  1.1  jmcneill 	return 0;
    294  1.1  jmcneill }
    295  1.1  jmcneill 
    296  1.1  jmcneill static void
    297  1.1  jmcneill rk_usbphy_attach(device_t parent, device_t self, void *aux)
    298  1.1  jmcneill {
    299  1.1  jmcneill 	struct rk_usbphy_softc * const sc = device_private(self);
    300  1.1  jmcneill 	struct fdt_attach_args * const faa = aux;
    301  1.1  jmcneill 	const int phandle = faa->faa_phandle;
    302  1.1  jmcneill 	const char *name = fdtbus_get_string(phandle, "name");
    303  1.1  jmcneill 
    304  1.1  jmcneill 	sc->sc_dev = self;
    305  1.1  jmcneill 	sc->sc_phandle = phandle;
    306  1.1  jmcneill 
    307  1.1  jmcneill 	aprint_naive("\n");
    308  1.1  jmcneill 
    309  1.1  jmcneill 	if (strcmp(name, "otg-port") == 0) {
    310  1.1  jmcneill 		aprint_normal(": USB2 OTG port\n");
    311  1.1  jmcneill 		fdtbus_register_phy_controller(self, phandle, &rk_usbphy_otg_funcs);
    312  1.1  jmcneill 	} else if (strcmp(name, "host-port") == 0) {
    313  1.1  jmcneill 		aprint_normal(": USB2 host port\n");
    314  1.1  jmcneill 		fdtbus_register_phy_controller(self, phandle, &rk_usbphy_host_funcs);
    315  1.1  jmcneill 	}
    316  1.1  jmcneill }
    317