xref: /freebsd/sys/arm/qualcomm/ipq4018_usb_hs_phy.c (revision 29fc4075e69fd27de0cded313ac6000165d99f8b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2019 Michal Meloun <mmel@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/bus.h>
34 #include <sys/gpio.h>
35 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
38 #include <sys/rman.h>
39 
40 #include <machine/bus.h>
41 
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>
47 
48 #include <dev/fdt/simple_mfd.h>
49 #include "phynode_if.h"
50 #include "phynode_usb_if.h"
51 
52 static struct ofw_compat_data compat_data[] = {
53 	{"qcom,usb-hs-ipq4019-phy",	1},
54 	{NULL,				0},
55 };
56 
57 struct ipq4018_usb_hs_phy_softc {
58 	device_t		dev;
59 };
60 
61 struct ipq4018_usb_hs_phynode_sc {
62 	struct phynode_usb_sc	usb_sc;
63 	int			mode;
64 	hwreset_t		por_rst;
65 	hwreset_t		srif_rst;
66 };
67 
68 static int
69 ipq4018_usb_hs_phynode_phy_enable(struct phynode *phynode, bool enable)
70 {
71 	struct ipq4018_usb_hs_phynode_sc *sc;
72 	device_t dev;
73 	int rv;
74 
75 	dev = phynode_get_device(phynode);
76 	sc = phynode_get_softc(phynode);
77 
78 	/*
79 	 *
80 	 * For power-off - assert por, sleep for 10ms, assert srif,
81 	 * sleep for 10ms
82 	 */
83 	rv = hwreset_assert(sc->por_rst);
84 	if (rv != 0)
85 		goto done;
86 	DELAY(10*1000);
87 	rv = hwreset_assert(sc->srif_rst);
88 	if (rv != 0)
89 		goto done;
90 	DELAY(10*1000);
91 
92 	/*
93 	 * For power-on - power off first, then deassert srif, then
94 	 * sleep for 10ms, then deassert por.
95 	 */
96 	if (enable) {
97 		rv = hwreset_deassert(sc->srif_rst);
98 		if (rv != 0)
99 			goto done;
100 		DELAY(10*1000);
101 		rv = hwreset_deassert(sc->por_rst);
102 		if (rv != 0)
103 			goto done;
104 		DELAY(10*1000);
105 	}
106 
107 done:
108 	if (rv != 0) {
109 		device_printf(dev, "%s: failed, rv=%d\n", __func__, rv);
110 	}
111 	return (rv);
112 }
113 
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),
117 	PHYNODEUSBMETHOD_END
118 };
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);
122 
123 static int
124 ipq4018_usb_hs_usbphy_init_phy(struct ipq4018_usb_hs_phy_softc *sc,
125     phandle_t node)
126 {
127 	struct phynode *phynode;
128 	struct phynode_init_def phy_init;
129 	struct ipq4018_usb_hs_phynode_sc *phy_sc;
130 	int rv;
131 	hwreset_t por_rst = NULL, srif_rst = NULL;
132 
133 	/* FDT resources */
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");
137 		goto fail;
138 	}
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");
142 		goto fail;
143 	}
144 
145 	/* Create and register phy. */
146 	bzero(&phy_init, sizeof(phy_init));
147 	phy_init.id = 1;
148 	phy_init.ofw_node = node;
149 	phynode = phynode_create(sc->dev, &ipq4018_usb_hs_phynode_class,
150 	    &phy_init);
151 	if (phynode == NULL) {
152 		device_printf(sc->dev, "Cannot create phy.\n");
153 		return (ENXIO);
154 	}
155 
156 	phy_sc = phynode_get_softc(phynode);
157 	phy_sc->por_rst = por_rst;
158 	phy_sc->srif_rst = srif_rst;
159 
160 	if (phynode_register(phynode) == NULL) {
161 		device_printf(sc->dev, "Cannot register phy.\n");
162 		return (ENXIO);
163 	}
164 
165 	(void) ipq4018_usb_hs_phynode_phy_enable(phynode, true);
166 
167 	return (0);
168 
169 fail:
170 	if (por_rst != NULL)
171 		 hwreset_release(por_rst);
172 	if (srif_rst != NULL)
173 		 hwreset_release(srif_rst);
174 
175 	return (ENXIO);
176 }
177 
178 static int
179 ipq4018_usb_hs_usbphy_probe(device_t dev)
180 {
181 
182 	if (!ofw_bus_status_okay(dev))
183 		return (ENXIO);
184 
185 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
186 		return (ENXIO);
187 
188 	device_set_desc(dev, "IPQ4018/IPQ4019 USB HS PHY");
189 	return (BUS_PROBE_DEFAULT);
190 }
191 
192 static int
193 ipq4018_usb_hs_usbphy_attach(device_t dev)
194 {
195 	struct ipq4018_usb_hs_phy_softc *sc;
196 	phandle_t node;
197 	int rv;
198 
199 	sc = device_get_softc(dev);
200 	sc->dev = dev;
201 	node = ofw_bus_get_node(sc->dev);
202 
203 	rv = ipq4018_usb_hs_usbphy_init_phy(sc, node);
204 	if (rv != 0)
205 		goto fail;
206 	return (bus_generic_attach(dev));
207 
208 fail:
209 	return (ENXIO);
210 }
211 
212 static int
213 ipq4018_usb_hs_usbphy_detach(device_t dev)
214 {
215 	struct ipq4018_usb_hs_phy_softc *sc;
216 	sc = device_get_softc(dev);
217 
218 	return (0);
219 }
220 
221 static device_method_t ipq4018_usb_hs_usbphy_methods[] = {
222 	/* Device interface */
223 	DEVMETHOD(device_probe,			ipq4018_usb_hs_usbphy_probe),
224 	DEVMETHOD(device_attach,		ipq4018_usb_hs_usbphy_attach),
225 	DEVMETHOD(device_detach,		ipq4018_usb_hs_usbphy_detach),
226 	DEVMETHOD_END
227 };
228 
229 static DEFINE_CLASS_0(ipq4018_usb_hs_usbphy, ipq4018_usb_hs_usbphy_driver,
230     ipq4018_usb_hs_usbphy_methods,
231     sizeof(struct ipq4018_usb_hs_phy_softc));
232 EARLY_DRIVER_MODULE(ipq4018_usb_hs_usbphy, simplebus,
233     ipq4018_usb_hs_usbphy_driver, NULL, NULL,
234     BUS_PASS_TIMER + BUS_PASS_ORDER_LAST);
235