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