2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
40 #include <machine/bus.h>
42 #include <dev/extres/hwreset/hwreset.h>
43 #include <dev/extres/phy/phy_usb.h>
44 #include <dev/extres/regulator/regulator.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/fdt/simple_mfd.h>
49 #include "phynode_if.h"
50 #include "phynode_usb_if.h"
52 static struct ofw_compat_data compat_data[] = {
53 {"qcom,usb-hs-ipq4019-phy", 1},
57 struct ipq4018_usb_hs_phy_softc {
61 struct ipq4018_usb_hs_phynode_sc {
62 struct phynode_usb_sc usb_sc;
69 ipq4018_usb_hs_phynode_phy_enable(struct phynode *phynode, bool enable)
71 struct ipq4018_usb_hs_phynode_sc *sc;
75 dev = phynode_get_device(phynode);
76 sc = phynode_get_softc(phynode);
80 * For power-off - assert por, sleep for 10ms, assert srif,
83 rv = hwreset_assert(sc->por_rst);
87 rv = hwreset_assert(sc->srif_rst);
93 * For power-on - power off first, then deassert srif, then
94 * sleep for 10ms, then deassert por.
97 rv = hwreset_deassert(sc->srif_rst);
101 rv = hwreset_deassert(sc->por_rst);
109 device_printf(dev, "%s: failed, rv=%d\n", __func__, rv);
114 /* Phy controller class and methods. */
115 static phynode_method_t ipq4018_usb_hs_phynode_methods[] = {
116 PHYNODEUSBMETHOD(phynode_enable, ipq4018_usb_hs_phynode_phy_enable),
119 DEFINE_CLASS_1(ipq4018_usb_hs_phynode, ipq4018_usb_hs_phynode_class,
120 ipq4018_usb_hs_phynode_methods,
121 sizeof(struct ipq4018_usb_hs_phynode_sc), phynode_usb_class);
124 ipq4018_usb_hs_usbphy_init_phy(struct ipq4018_usb_hs_phy_softc *sc,
127 struct phynode *phynode;
128 struct phynode_init_def phy_init;
129 struct ipq4018_usb_hs_phynode_sc *phy_sc;
131 hwreset_t por_rst = NULL, srif_rst = NULL;
134 rv = hwreset_get_by_ofw_name(sc->dev, node, "por_rst", &por_rst);
135 if (rv != 0 && rv != ENOENT) {
136 device_printf(sc->dev, "Cannot get 'por_rst' reset\n");
139 rv = hwreset_get_by_ofw_name(sc->dev, node, "srif_rst", &srif_rst);
140 if (rv != 0 && rv != ENOENT) {
141 device_printf(sc->dev, "Cannot get 'srif_rst' reset\n");
145 /* Create and register phy. */
146 bzero(&phy_init, sizeof(phy_init));
148 phy_init.ofw_node = node;
149 phynode = phynode_create(sc->dev, &ipq4018_usb_hs_phynode_class,
151 if (phynode == NULL) {
152 device_printf(sc->dev, "Cannot create phy.\n");
156 phy_sc = phynode_get_softc(phynode);
157 phy_sc->por_rst = por_rst;
158 phy_sc->srif_rst = srif_rst;
160 if (phynode_register(phynode) == NULL) {
161 device_printf(sc->dev, "Cannot register phy.\n");
165 (void) ipq4018_usb_hs_phynode_phy_enable(phynode, true);
171 hwreset_release(por_rst);
172 if (srif_rst != NULL)
173 hwreset_release(srif_rst);
179 ipq4018_usb_hs_usbphy_probe(device_t dev)
182 if (!ofw_bus_status_okay(dev))
185 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
188 device_set_desc(dev, "IPQ4018/IPQ4019 USB HS PHY");
189 return (BUS_PROBE_DEFAULT);
193 ipq4018_usb_hs_usbphy_attach(device_t dev)
195 struct ipq4018_usb_hs_phy_softc *sc;
199 sc = device_get_softc(dev);
201 node = ofw_bus_get_node(sc->dev);
203 rv = ipq4018_usb_hs_usbphy_init_phy(sc, node);
206 return (bus_generic_attach(dev));
213 ipq4018_usb_hs_usbphy_detach(device_t dev)
219 static device_method_t ipq4018_usb_hs_usbphy_methods[] = {
220 /* Device interface */
221 DEVMETHOD(device_probe, ipq4018_usb_hs_usbphy_probe),
222 DEVMETHOD(device_attach, ipq4018_usb_hs_usbphy_attach),
223 DEVMETHOD(device_detach, ipq4018_usb_hs_usbphy_detach),
227 static DEFINE_CLASS_0(ipq4018_usb_hs_usbphy, ipq4018_usb_hs_usbphy_driver,
228 ipq4018_usb_hs_usbphy_methods,
229 sizeof(struct ipq4018_usb_hs_phy_softc));
230 EARLY_DRIVER_MODULE(ipq4018_usb_hs_usbphy, simplebus,
231 ipq4018_usb_hs_usbphy_driver, NULL, NULL,
232 BUS_PASS_TIMER + BUS_PASS_ORDER_LAST);