xref: /freebsd/sys/arm/qualcomm/ipq4018_usb_ss_phy.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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-ss-ipq4019-phy",	1},
52 	{NULL,				0},
53 };
54 
55 struct ipq4018_usb_ss_phy_softc {
56 	device_t		dev;
57 };
58 
59 struct ipq4018_usb_ss_phynode_sc {
60 	struct phynode_usb_sc	usb_sc;
61 	int			mode;
62 	hwreset_t		por_rst;
63 };
64 
65 static int
66 ipq4018_usb_ss_phynode_phy_enable(struct phynode *phynode, bool enable)
67 {
68 	struct ipq4018_usb_ss_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 	 * For power-off - assert por, sleep for 10ms
77 	 */
78 	rv = hwreset_assert(sc->por_rst);
79 	if (rv != 0)
80 		goto done;
81 	DELAY(10*1000);
82 
83 	/*
84 	 * For power-on - power off first, then deassert por.
85 	 */
86 	if (enable) {
87 		rv = hwreset_deassert(sc->por_rst);
88 		if (rv != 0)
89 			goto done;
90 		DELAY(10*1000);
91 	}
92 
93 done:
94 	if (rv != 0) {
95 		device_printf(dev, "%s: failed, rv=%d\n", __func__, rv);
96 	}
97 	return (rv);
98 }
99 
100  /* Phy controller class and methods. */
101 static phynode_method_t ipq4018_usb_ss_phynode_methods[] = {
102 	PHYNODEUSBMETHOD(phynode_enable,	ipq4018_usb_ss_phynode_phy_enable),
103 	PHYNODEUSBMETHOD_END
104 };
105 DEFINE_CLASS_1(ipq4018_usb_ss_phynode, ipq4018_usb_ss_phynode_class,
106     ipq4018_usb_ss_phynode_methods,
107     sizeof(struct ipq4018_usb_ss_phynode_sc), phynode_usb_class);
108 
109 static int
110 ipq4018_usb_ss_usbphy_init_phy(struct ipq4018_usb_ss_phy_softc *sc,
111     phandle_t node)
112 {
113 	struct phynode *phynode;
114 	struct phynode_init_def phy_init;
115 	struct ipq4018_usb_ss_phynode_sc *phy_sc;
116 	int rv;
117 	hwreset_t por_rst = NULL;
118 
119 	/* FDT resources */
120 	rv = hwreset_get_by_ofw_name(sc->dev, node, "por_rst", &por_rst);
121 	if (rv != 0 && rv != ENOENT) {
122 		device_printf(sc->dev, "Cannot get 'por_rst' reset\n");
123 		goto fail;
124 	}
125 
126 	/* Create and register phy. */
127 	bzero(&phy_init, sizeof(phy_init));
128 	phy_init.id = 1;
129 	phy_init.ofw_node = node;
130 	phynode = phynode_create(sc->dev, &ipq4018_usb_ss_phynode_class,
131 	    &phy_init);
132 	if (phynode == NULL) {
133 		device_printf(sc->dev, "Cannot create phy.\n");
134 		return (ENXIO);
135 	}
136 
137 	phy_sc = phynode_get_softc(phynode);
138 	phy_sc->por_rst = por_rst;
139 
140 	if (phynode_register(phynode) == NULL) {
141 		device_printf(sc->dev, "Cannot register phy.\n");
142 		return (ENXIO);
143 	}
144 
145 	(void) ipq4018_usb_ss_phynode_phy_enable(phynode, true);
146 
147 	return (0);
148 
149 fail:
150 	if (por_rst != NULL)
151 		 hwreset_release(por_rst);
152 
153 	return (ENXIO);
154 }
155 
156 static int
157 ipq4018_usb_ss_usbphy_probe(device_t dev)
158 {
159 
160 	if (!ofw_bus_status_okay(dev))
161 		return (ENXIO);
162 
163 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
164 		return (ENXIO);
165 
166 	device_set_desc(dev, "IPQ4018/IPQ4019 USB SS PHY");
167 	return (BUS_PROBE_DEFAULT);
168 }
169 
170 static int
171 ipq4018_usb_ss_usbphy_attach(device_t dev)
172 {
173 	struct ipq4018_usb_ss_phy_softc *sc;
174 	phandle_t node;
175 	int rv;
176 
177 	sc = device_get_softc(dev);
178 	sc->dev = dev;
179 	node = ofw_bus_get_node(sc->dev);
180 
181 	rv = ipq4018_usb_ss_usbphy_init_phy(sc, node);
182 	if (rv != 0)
183 		goto fail;
184 	return (bus_generic_attach(dev));
185 
186 fail:
187 	return (ENXIO);
188 }
189 
190 static int
191 ipq4018_usb_ss_usbphy_detach(device_t dev)
192 {
193 
194 	return (0);
195 }
196 
197 static device_method_t ipq4018_usb_ss_usbphy_methods[] = {
198 	/* Device interface */
199 	DEVMETHOD(device_probe,			ipq4018_usb_ss_usbphy_probe),
200 	DEVMETHOD(device_attach,		ipq4018_usb_ss_usbphy_attach),
201 	DEVMETHOD(device_detach,		ipq4018_usb_ss_usbphy_detach),
202 	DEVMETHOD_END
203 };
204 
205 static DEFINE_CLASS_0(ipq4018_usb_ss_usbphy, ipq4018_usb_ss_usbphy_driver,
206     ipq4018_usb_ss_usbphy_methods,
207     sizeof(struct ipq4018_usb_ss_phy_softc));
208 EARLY_DRIVER_MODULE(ipq4018_usb_ss_usbphy, simplebus,
209     ipq4018_usb_ss_usbphy_driver, NULL, NULL,
210     BUS_PASS_TIMER + BUS_PASS_ORDER_LAST);
211