Index: xhci_fdt.c =================================================================== RCS file: /cvs/src/sys/dev/fdt/xhci_fdt.c,v retrieving revision 1.22 diff -u -p -r1.22 xhci_fdt.c --- xhci_fdt.c 10 Mar 2023 10:22:55 -0000 1.22 +++ xhci_fdt.c 20 Mar 2023 12:11:02 -0000 @@ -362,32 +362,16 @@ xhci_init_phy(struct xhci_fdt_softc *sc, } void -xhci_init_phys(struct xhci_fdt_softc *sc) +xhci_phy_enable(struct xhci_fdt_softc *sc, char *name) { uint32_t *phys; uint32_t *phy; - uint32_t usb_phy; - int len, idx; - - /* - * Legacy binding; assume there only is a single USB PHY. - */ - usb_phy = OF_getpropint(sc->sc_node, "usb-phy", 0); - if (usb_phy) { - xhci_init_phy(sc, &usb_phy); - return; - } + int idx, len; - /* - * Generic PHY binding; only initialize USB 3 PHY for now. - */ - idx = OF_getindex(sc->sc_node, "usb3-phy", "phy-names"); + idx = OF_getindex(sc->sc_node, name, "phy-names"); if (idx < 0) return; - if (phy_enable_idx(sc->sc_node, idx) != ENXIO) - return; - len = OF_getproplen(sc->sc_node, "phys"); if (len <= 0) return; @@ -407,6 +391,26 @@ xhci_init_phys(struct xhci_fdt_softc *sc idx--; } free(phys, M_TEMP, len); +} + +void +xhci_init_phys(struct xhci_fdt_softc *sc) +{ + int rv; + + rv = phy_enable_prop_idx(sc->sc_node, "usb-phy", 0); + if (rv != 0) { + rv = phy_enable(sc->sc_node, "usb2-phy"); + if (rv != 0) + xhci_phy_enable(sc, "usb2-phy"); + } + + rv = phy_enable_prop_idx(sc->sc_node, "usb-phy", 1); + if (rv != 0) { + rv = phy_enable(sc->sc_node, "usb3-phy"); + if (rv != 0) + xhci_phy_enable(sc, "usb3-phy"); + } } /* Index: rkuiphy.c =================================================================== RCS file: rkuiphy.c diff -N rkuiphy.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ rkuiphy.c 20 Mar 2023 12:11:02 -0000 @@ -0,0 +1,255 @@ +/* $OpenBSD$ */ + +/* + * Copyright (c) 2023 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/* + * Rockchip USB2PHY with Innosilicon IP + */ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#define RKUIPHY_PORT_OTG 0 +#define RKUIPHY_PORT_HOST 1 +#define RKUIPHY_PORT_COUNT 2 + +/* + * chip stuff + */ + +struct rkuiphy_reg { + bus_size_t r_offs; +}; + +struct rkuiphy_port_regs { + /* phy suspend */ + struct rkuiphy_reg pr_phy_sus; + struct rkuiphy_reg pr_bvalid_det_en; +}; + +struct rkuiphy_chip { + bus_addr_t c_base_addr; + struct rkuiphy_port_regs c_regs; +}; + +static const struct rkuiphy_chip rkuiphy_rk3568[] = { + { + .c_base_addr = 0xfe8a0000, + }, + { + .c_base_addr = 0xfe8b0000, + }, +}; + +#define RKUIPHY_GRF_PROP "rockchip,usbgrf" +#define RKUIPHY_GRF_NAME "rockchip,rk3568-usb2phy-grf" + +/* + * driver stuff + */ + +struct rkuiphy_softc { + struct device sc_dev; + const struct rkuiphy_chip *sc_chip; + struct regmap *sc_grf; + int sc_node; + + struct phy_device sc_otg_phy; + struct phy_device sc_host_phy; +}; +#define DEVNAME(_sc) ((_sc)->sc_dev.dv_xname) + +static int rkuiphy_match(struct device *, void *, void *); +static void rkuiphy_attach(struct device *, struct device *, void *); + +static int rkuiphy_otg_phy_enable(void *, uint32_t *); +static int rkuiphy_host_phy_enable(void *, uint32_t *); + +struct rkuiphy_port_config { + const char *pc_name; + int (*pc_enable)(void *, uint32_t *); +}; + +static void rkuiphy_register(struct rkuiphy_softc *, + struct phy_device *, const struct rkuiphy_port_config *); + +static const struct rkuiphy_port_config rkuiphy_otg_config = { + .pc_name = "otg-port", + .pc_enable = rkuiphy_otg_phy_enable, +}; + +static const struct rkuiphy_port_config rkuiphy_host_config = { + .pc_name = "host-port", + .pc_enable = rkuiphy_host_phy_enable, +}; + +const struct cfattach rkuiphy_ca = { + sizeof (struct rkuiphy_softc), rkuiphy_match, rkuiphy_attach +}; + +struct cfdriver rkuiphy_cd = { + NULL, "rkuiphy", DV_DULL +}; + +struct rkuiphy_id { + const char *id_name; + const struct rkuiphy_chip *id_chips; + size_t id_nchips; +}; + +#define RKUIPHY_ID(_n, _c) { _n, _c, nitems(_c) } + +static const struct rkuiphy_id rkuiphy_ids[] = { + RKUIPHY_ID("rockchip,rk3568-usb2phy", rkuiphy_rk3568), +}; + +static const struct rkuiphy_id * +rkuiphy_lookup(struct fdt_attach_args *faa) +{ + size_t i; + + for (i = 0; i < nitems(rkuiphy_ids); i++) { + const struct rkuiphy_id *id = &rkuiphy_ids[i]; + if (OF_is_compatible(faa->fa_node, id->id_name)) + return (id); + } + + return (NULL); +} + +static int +rkuiphy_match(struct device *parent, void *match, void *aux) +{ + struct fdt_attach_args *faa = aux; + + return (rkuiphy_lookup(faa) != NULL ? 1 : 0); +} + +static void +rkuiphy_attach(struct device *parent, struct device *self, void *aux) +{ + struct rkuiphy_softc *sc = (struct rkuiphy_softc *)self; + struct fdt_attach_args *faa = aux; + const struct rkuiphy_id *id = rkuiphy_lookup(faa); + size_t i; + uint32_t grfph; + + if (faa->fa_nreg < 1) { + printf(": no registers\n"); + return; + } + + for (i = 0; i < id->id_nchips; i++) { + const struct rkuiphy_chip *c = &id->id_chips[i]; + if (faa->fa_reg[0].addr == c->c_base_addr) { + printf(": phy %zu\n", i); + sc->sc_chip = c; + break; + } + } + if (sc->sc_chip == NULL) { + printf(": unknown base address 0x%llu\n", faa->fa_reg[0].addr); + return; + } + + sc->sc_node = faa->fa_node; + + grfph = OF_getpropint(sc->sc_node, RKUIPHY_GRF_PROP, 0); + if (grfph == 0) { + printf("%s: invalid or missing %s property\n", DEVNAME(sc), + RKUIPHY_GRF_PROP); + return; + } + + sc->sc_grf = regmap_byphandle(grfph); + if (sc->sc_grf == NULL) { + printf("%s: %s 0x%x not found\n", DEVNAME(sc), + RKUIPHY_GRF_PROP, grfph); + return; + } + if (!OF_is_compatible(regmap_node(sc->sc_grf), RKUIPHY_GRF_NAME)) { + printf("%s: %s,usbgrf 0x%x is not compat with %s\n", + DEVNAME(sc), RKUIPHY_GRF_PROP, grfph, RKUIPHY_GRF_NAME); + return; + } + + reset_assert_all(sc->sc_node); + + rkuiphy_register(sc, &sc->sc_otg_phy, &rkuiphy_otg_config); + rkuiphy_register(sc, &sc->sc_host_phy, &rkuiphy_host_config); +} + +static void +rkuiphy_register(struct rkuiphy_softc *sc, struct phy_device *pd, + const struct rkuiphy_port_config *pc) +{ + int node; + + node = OF_getnodebyname(sc->sc_node, pc->pc_name); + if (node == 0) { + printf("%s: cannot find %s\n", DEVNAME(sc), pc->pc_name); + return; + } + + if (OF_getpropint(node, "phy-supply", 0) == 0) { + printf("%s: cannot find %s phy-supply property\n", DEVNAME(sc), + pc->pc_name); + return; + } + + pd->pd_node = node; + pd->pd_cookie = sc; + pd->pd_enable = pc->pc_enable; + phy_register(pd); +} + +static int +rkuiphy_otg_phy_enable(void *cookie, uint32_t *cells) +{ + struct rkuiphy_softc *sc = cookie; + int node = sc->sc_otg_phy.pd_node; + + printf("%s: %s\n", DEVNAME(sc), __func__); + + regulator_enable(OF_getpropint(node, "phy-supply", 0)); + + return (EINVAL); +} + +static int +rkuiphy_host_phy_enable(void *cookie, uint32_t *cells) +{ + struct rkuiphy_softc *sc = cookie; + int node = sc->sc_otg_phy.pd_node; + + printf("%s: %s\n", DEVNAME(sc), __func__); + + regulator_enable(OF_getpropint(node, "phy-supply", 0)); + + return (EINVAL); +}