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