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-ss-ipq4019-phy", 1},
57 struct ipq4018_usb_ss_phy_softc {
61 struct ipq4018_usb_ss_phynode_sc {
62 struct phynode_usb_sc usb_sc;
68 ipq4018_usb_ss_phynode_phy_enable(struct phynode *phynode, bool enable)
70 struct ipq4018_usb_ss_phynode_sc *sc;
74 dev = phynode_get_device(phynode);
75 sc = phynode_get_softc(phynode);
78 * For power-off - assert por, sleep for 10ms
80 rv = hwreset_assert(sc->por_rst);
86 * For power-on - power off first, then deassert por.
89 rv = hwreset_deassert(sc->por_rst);
97 device_printf(dev, "%s: failed, rv=%d\n", __func__, rv);
102 /* Phy controller class and methods. */
103 static phynode_method_t ipq4018_usb_ss_phynode_methods[] = {
104 PHYNODEUSBMETHOD(phynode_enable, ipq4018_usb_ss_phynode_phy_enable),
107 DEFINE_CLASS_1(ipq4018_usb_ss_phynode, ipq4018_usb_ss_phynode_class,
108 ipq4018_usb_ss_phynode_methods,
109 sizeof(struct ipq4018_usb_ss_phynode_sc), phynode_usb_class);
112 ipq4018_usb_ss_usbphy_init_phy(struct ipq4018_usb_ss_phy_softc *sc,
115 struct phynode *phynode;
116 struct phynode_init_def phy_init;
117 struct ipq4018_usb_ss_phynode_sc *phy_sc;
119 hwreset_t por_rst = NULL;
122 rv = hwreset_get_by_ofw_name(sc->dev, node, "por_rst", &por_rst);
123 if (rv != 0 && rv != ENOENT) {
124 device_printf(sc->dev, "Cannot get 'por_rst' reset\n");
128 /* Create and register phy. */
129 bzero(&phy_init, sizeof(phy_init));
131 phy_init.ofw_node = node;
132 phynode = phynode_create(sc->dev, &ipq4018_usb_ss_phynode_class,
134 if (phynode == NULL) {
135 device_printf(sc->dev, "Cannot create phy.\n");
139 phy_sc = phynode_get_softc(phynode);
140 phy_sc->por_rst = por_rst;
142 if (phynode_register(phynode) == NULL) {
143 device_printf(sc->dev, "Cannot register phy.\n");
147 (void) ipq4018_usb_ss_phynode_phy_enable(phynode, true);
153 hwreset_release(por_rst);
159 ipq4018_usb_ss_usbphy_probe(device_t dev)
162 if (!ofw_bus_status_okay(dev))
165 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
168 device_set_desc(dev, "IPQ4018/IPQ4019 USB SS PHY");
169 return (BUS_PROBE_DEFAULT);
173 ipq4018_usb_ss_usbphy_attach(device_t dev)
175 struct ipq4018_usb_ss_phy_softc *sc;
179 sc = device_get_softc(dev);
181 node = ofw_bus_get_node(sc->dev);
183 rv = ipq4018_usb_ss_usbphy_init_phy(sc, node);
186 return (bus_generic_attach(dev));
193 ipq4018_usb_ss_usbphy_detach(device_t dev)
195 struct ipq4018_usb_ss_phy_softc *sc;
196 sc = device_get_softc(dev);
201 static device_method_t ipq4018_usb_ss_usbphy_methods[] = {
202 /* Device interface */
203 DEVMETHOD(device_probe, ipq4018_usb_ss_usbphy_probe),
204 DEVMETHOD(device_attach, ipq4018_usb_ss_usbphy_attach),
205 DEVMETHOD(device_detach, ipq4018_usb_ss_usbphy_detach),
209 static DEFINE_CLASS_0(ipq4018_usb_ss_usbphy, ipq4018_usb_ss_usbphy_driver,
210 ipq4018_usb_ss_usbphy_methods,
211 sizeof(struct ipq4018_usb_ss_phy_softc));
212 EARLY_DRIVER_MODULE(ipq4018_usb_ss_usbphy, simplebus,
213 ipq4018_usb_ss_usbphy_driver, NULL, NULL,
214 BUS_PASS_TIMER + BUS_PASS_ORDER_LAST);