exynos_usbdrdphy.c revision 1.3 1 1.3 thorpej /* $NetBSD: exynos_usbdrdphy.c,v 1.3 2021/01/18 02:35:49 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2018 Jared McNeill <jmcneill (at) invisible.ca>
5 1.1 jmcneill * All rights reserved.
6 1.1 jmcneill *
7 1.1 jmcneill * Redistribution and use in source and binary forms, with or without
8 1.1 jmcneill * modification, are permitted provided that the following conditions
9 1.1 jmcneill * are met:
10 1.1 jmcneill * 1. Redistributions of source code must retain the above copyright
11 1.1 jmcneill * notice, this list of conditions and the following disclaimer.
12 1.1 jmcneill * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 jmcneill * notice, this list of conditions and the following disclaimer in the
14 1.1 jmcneill * documentation and/or other materials provided with the distribution.
15 1.1 jmcneill *
16 1.1 jmcneill * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 jmcneill * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 jmcneill * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 jmcneill * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 jmcneill * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 jmcneill * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 jmcneill * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 jmcneill * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 jmcneill * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 jmcneill * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 jmcneill * POSSIBILITY OF SUCH DAMAGE.
27 1.1 jmcneill */
28 1.1 jmcneill
29 1.1 jmcneill #include <sys/cdefs.h>
30 1.1 jmcneill
31 1.3 thorpej __KERNEL_RCSID(0, "$NetBSD: exynos_usbdrdphy.c,v 1.3 2021/01/18 02:35:49 thorpej Exp $");
32 1.1 jmcneill
33 1.1 jmcneill #include <sys/param.h>
34 1.1 jmcneill #include <sys/bus.h>
35 1.1 jmcneill #include <sys/device.h>
36 1.1 jmcneill #include <sys/intr.h>
37 1.1 jmcneill #include <sys/systm.h>
38 1.1 jmcneill #include <sys/kmem.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill #include <dev/fdt/syscon.h>
42 1.1 jmcneill
43 1.1 jmcneill /*
44 1.1 jmcneill * PHY Registers
45 1.1 jmcneill */
46 1.1 jmcneill #define PHY_LINK_SYSTEM 0x04
47 1.1 jmcneill #define PHY_LINK_SYSTEM_XHCI_VERCTL __BIT(27)
48 1.1 jmcneill #define PHY_LINK_SYSTEM_FLADJ __BITS(6,1)
49 1.1 jmcneill #define PHY_UTMI 0x08
50 1.1 jmcneill #define PHY_UTMI_OTGDISABLE __BIT(6)
51 1.1 jmcneill #define PHY_CLK_RST 0x10
52 1.1 jmcneill #define PHY_CLK_RST_SSC_REFCLKSEL __BITS(30,23)
53 1.1 jmcneill #define PHY_CLK_RST_SSC_EN __BIT(20)
54 1.1 jmcneill #define PHY_CLK_RST_REF_SSP_EN __BIT(19)
55 1.1 jmcneill #define PHY_CLK_RST_MPLL_MULT __BITS(17,11)
56 1.1 jmcneill #define PHY_CLK_RST_MPLL_MULT_24M 0x68
57 1.1 jmcneill #define PHY_CLK_RST_FSEL __BITS(10,5)
58 1.2 jmcneill #define PHY_CLK_RST_FSEL_24M 0x5
59 1.1 jmcneill #define PHY_CLK_RST_RETENABLEN __BIT(4)
60 1.1 jmcneill #define PHY_CLK_RST_REFCLKSEL __BITS(3,2)
61 1.1 jmcneill #define PHY_CLK_RST_REFCLKSEL_EXT 3
62 1.1 jmcneill #define PHY_CLK_RST_PORTRESET __BIT(1)
63 1.1 jmcneill #define PHY_CLK_RST_COMMONONN __BIT(0)
64 1.1 jmcneill #define PHY_REG0 0x14
65 1.1 jmcneill #define PHY_PARAM0 0x1c
66 1.1 jmcneill #define PHY_PARAM0_REF_USE_PAD __BIT(31)
67 1.1 jmcneill #define PHY_PARAM0_REF_LOSLEVEL __BITS(30,26)
68 1.1 jmcneill #define PHY_PARAM1 0x20
69 1.1 jmcneill #define PHY_PARAM1_TXDEEMPH __BITS(4,0)
70 1.1 jmcneill #define PHY_TEST 0x28
71 1.1 jmcneill #define PHY_TEST_POWERDOWN_SSP __BIT(3)
72 1.1 jmcneill #define PHY_TEST_POWERDOWN_HSP __BIT(2)
73 1.1 jmcneill #define PHY_BATCHG 0x30
74 1.1 jmcneill #define PHY_BATCHG_UTMI_CLKSEL __BIT(2)
75 1.1 jmcneill #define PHY_RESUME 0x34
76 1.1 jmcneill
77 1.1 jmcneill /*
78 1.1 jmcneill * PMU Registers
79 1.1 jmcneill */
80 1.1 jmcneill #define USBDRD_PHY_CTRL(n) (0x704 + (n) * 4)
81 1.1 jmcneill #define USBDRD_PHY_CTRL_EN __BIT(0)
82 1.1 jmcneill
83 1.1 jmcneill static int exynos_usbdrdphy_match(device_t, cfdata_t, void *);
84 1.1 jmcneill static void exynos_usbdrdphy_attach(device_t, device_t, void *);
85 1.1 jmcneill
86 1.1 jmcneill enum {
87 1.1 jmcneill PHY_ID_UTMI_PLUS = 0,
88 1.1 jmcneill PHY_ID_PIPE3,
89 1.1 jmcneill NPHY_ID
90 1.1 jmcneill };
91 1.1 jmcneill
92 1.3 thorpej static const struct device_compatible_entry compat_data[] = {
93 1.3 thorpej { .compat = "samsung,exynos5420-usbdrd-phy" },
94 1.3 thorpej
95 1.3 thorpej { 0 }
96 1.1 jmcneill };
97 1.1 jmcneill
98 1.1 jmcneill struct exynos_usbdrdphy_softc;
99 1.1 jmcneill
100 1.1 jmcneill struct exynos_usbdrdphy {
101 1.1 jmcneill struct exynos_usbdrdphy_softc *phy_sc;
102 1.1 jmcneill u_int phy_index;
103 1.1 jmcneill };
104 1.1 jmcneill
105 1.1 jmcneill struct exynos_usbdrdphy_softc {
106 1.1 jmcneill device_t sc_dev;
107 1.1 jmcneill bus_space_tag_t sc_bst;
108 1.1 jmcneill bus_space_handle_t sc_bsh;
109 1.1 jmcneill int sc_phandle;
110 1.1 jmcneill struct syscon *sc_pmureg;
111 1.1 jmcneill
112 1.1 jmcneill struct exynos_usbdrdphy *sc_phy;
113 1.1 jmcneill u_int sc_nphy;
114 1.1 jmcneill
115 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_id_det;
116 1.1 jmcneill struct fdtbus_gpio_pin *sc_gpio_vbus_det;
117 1.1 jmcneill };
118 1.1 jmcneill
119 1.1 jmcneill #define PHY_READ(sc, reg) \
120 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
121 1.1 jmcneill #define PHY_WRITE(sc, reg, val) \
122 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
123 1.1 jmcneill
124 1.1 jmcneill CFATTACH_DECL_NEW(exynos_usbdrdphy, sizeof(struct exynos_usbdrdphy_softc),
125 1.1 jmcneill exynos_usbdrdphy_match, exynos_usbdrdphy_attach, NULL, NULL);
126 1.1 jmcneill
127 1.1 jmcneill static void *
128 1.1 jmcneill exynos_usbdrdphy_acquire(device_t dev, const void *data, size_t len)
129 1.1 jmcneill {
130 1.1 jmcneill struct exynos_usbdrdphy_softc * const sc = device_private(dev);
131 1.1 jmcneill
132 1.1 jmcneill if (len != 4)
133 1.1 jmcneill return NULL;
134 1.1 jmcneill
135 1.1 jmcneill const u_int index = be32dec(data);
136 1.1 jmcneill if (index >= sc->sc_nphy)
137 1.1 jmcneill return NULL;
138 1.1 jmcneill
139 1.1 jmcneill return &sc->sc_phy[index];
140 1.1 jmcneill }
141 1.1 jmcneill
142 1.1 jmcneill static void
143 1.1 jmcneill exynos_usbdrdphy_release(device_t dev, void *priv)
144 1.1 jmcneill {
145 1.1 jmcneill }
146 1.1 jmcneill
147 1.1 jmcneill static int
148 1.1 jmcneill exynos_usbdrdphy_enable(device_t dev, void *priv, bool enable)
149 1.1 jmcneill {
150 1.1 jmcneill struct exynos_usbdrdphy * const phy = priv;
151 1.1 jmcneill struct exynos_usbdrdphy_softc * const sc = phy->phy_sc;
152 1.1 jmcneill uint32_t val;
153 1.1 jmcneill
154 1.1 jmcneill syscon_lock(sc->sc_pmureg);
155 1.1 jmcneill val = syscon_read_4(sc->sc_pmureg, USBDRD_PHY_CTRL(phy->phy_index));
156 1.1 jmcneill if (enable)
157 1.1 jmcneill val |= USBDRD_PHY_CTRL_EN;
158 1.1 jmcneill else
159 1.1 jmcneill val &= ~USBDRD_PHY_CTRL_EN;
160 1.1 jmcneill syscon_write_4(sc->sc_pmureg, USBDRD_PHY_CTRL(phy->phy_index), val);
161 1.1 jmcneill syscon_unlock(sc->sc_pmureg);
162 1.1 jmcneill
163 1.1 jmcneill return 0;
164 1.1 jmcneill }
165 1.1 jmcneill
166 1.1 jmcneill const struct fdtbus_phy_controller_func exynos_usbdrdphy_funcs = {
167 1.1 jmcneill .acquire = exynos_usbdrdphy_acquire,
168 1.1 jmcneill .release = exynos_usbdrdphy_release,
169 1.1 jmcneill .enable = exynos_usbdrdphy_enable,
170 1.1 jmcneill };
171 1.1 jmcneill
172 1.1 jmcneill static void
173 1.1 jmcneill exynos_usbdrdphy_init(struct exynos_usbdrdphy_softc *sc)
174 1.1 jmcneill {
175 1.1 jmcneill uint32_t val;
176 1.1 jmcneill
177 1.1 jmcneill PHY_WRITE(sc, PHY_REG0, 0);
178 1.1 jmcneill
179 1.1 jmcneill val = PHY_READ(sc, PHY_PARAM0);
180 1.1 jmcneill val &= ~PHY_PARAM0_REF_USE_PAD;
181 1.1 jmcneill val &= ~PHY_PARAM0_REF_LOSLEVEL;
182 1.1 jmcneill val |= __SHIFTIN(9, PHY_PARAM0_REF_LOSLEVEL);
183 1.1 jmcneill PHY_WRITE(sc, PHY_PARAM0, val);
184 1.1 jmcneill
185 1.1 jmcneill PHY_WRITE(sc, PHY_RESUME, 0);
186 1.1 jmcneill
187 1.1 jmcneill val = PHY_READ(sc, PHY_LINK_SYSTEM);
188 1.1 jmcneill val |= PHY_LINK_SYSTEM_XHCI_VERCTL;
189 1.1 jmcneill val &= ~PHY_LINK_SYSTEM_FLADJ;
190 1.1 jmcneill val |= __SHIFTIN(0x20, PHY_LINK_SYSTEM_FLADJ);
191 1.1 jmcneill PHY_WRITE(sc, PHY_LINK_SYSTEM, val);
192 1.1 jmcneill
193 1.1 jmcneill val = PHY_READ(sc, PHY_PARAM1);
194 1.1 jmcneill val &= ~PHY_PARAM1_TXDEEMPH;
195 1.1 jmcneill val |= __SHIFTIN(0x1c, PHY_PARAM1_TXDEEMPH);
196 1.1 jmcneill PHY_WRITE(sc, PHY_PARAM1, val);
197 1.1 jmcneill
198 1.1 jmcneill val = PHY_READ(sc, PHY_BATCHG);
199 1.1 jmcneill val |= PHY_BATCHG_UTMI_CLKSEL;
200 1.1 jmcneill PHY_WRITE(sc, PHY_BATCHG, val);
201 1.1 jmcneill
202 1.1 jmcneill val = PHY_READ(sc, PHY_TEST);
203 1.1 jmcneill val &= ~PHY_TEST_POWERDOWN_SSP;
204 1.1 jmcneill val &= ~PHY_TEST_POWERDOWN_HSP;
205 1.1 jmcneill PHY_WRITE(sc, PHY_TEST, val);
206 1.1 jmcneill
207 1.1 jmcneill PHY_WRITE(sc, PHY_UTMI, PHY_UTMI_OTGDISABLE);
208 1.1 jmcneill
209 1.1 jmcneill val = __SHIFTIN(PHY_CLK_RST_REFCLKSEL_EXT, PHY_CLK_RST_REFCLKSEL);
210 1.1 jmcneill val |= __SHIFTIN(PHY_CLK_RST_FSEL_24M, PHY_CLK_RST_FSEL);
211 1.1 jmcneill val |= __SHIFTIN(PHY_CLK_RST_MPLL_MULT_24M, PHY_CLK_RST_MPLL_MULT);
212 1.1 jmcneill val |= __SHIFTIN(0x88, PHY_CLK_RST_SSC_REFCLKSEL);
213 1.1 jmcneill val |= PHY_CLK_RST_PORTRESET;
214 1.1 jmcneill val |= PHY_CLK_RST_RETENABLEN;
215 1.1 jmcneill val |= PHY_CLK_RST_REF_SSP_EN;
216 1.1 jmcneill val |= PHY_CLK_RST_SSC_EN;
217 1.1 jmcneill val |= PHY_CLK_RST_COMMONONN;
218 1.1 jmcneill PHY_WRITE(sc, PHY_CLK_RST, val);
219 1.1 jmcneill
220 1.1 jmcneill delay(50000);
221 1.1 jmcneill
222 1.1 jmcneill val &= ~PHY_CLK_RST_PORTRESET;
223 1.1 jmcneill PHY_WRITE(sc, PHY_CLK_RST, val);
224 1.1 jmcneill }
225 1.1 jmcneill
226 1.1 jmcneill static int
227 1.1 jmcneill exynos_usbdrdphy_match(device_t parent, cfdata_t cf, void *aux)
228 1.1 jmcneill {
229 1.1 jmcneill struct fdt_attach_args * const faa = aux;
230 1.1 jmcneill
231 1.1 jmcneill return of_match_compat_data(faa->faa_phandle, compat_data);
232 1.1 jmcneill }
233 1.1 jmcneill
234 1.1 jmcneill static void
235 1.1 jmcneill exynos_usbdrdphy_attach(device_t parent, device_t self, void *aux)
236 1.1 jmcneill {
237 1.1 jmcneill struct exynos_usbdrdphy_softc * const sc = device_private(self);
238 1.1 jmcneill struct fdt_attach_args * const faa = aux;
239 1.1 jmcneill const int phandle = faa->faa_phandle;
240 1.1 jmcneill struct clk *clk;
241 1.1 jmcneill bus_addr_t addr;
242 1.1 jmcneill bus_size_t size;
243 1.1 jmcneill u_int n;
244 1.1 jmcneill
245 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
246 1.1 jmcneill aprint_error(": couldn't get phy registers\n");
247 1.1 jmcneill return;
248 1.1 jmcneill }
249 1.1 jmcneill
250 1.1 jmcneill sc->sc_dev = self;
251 1.1 jmcneill sc->sc_phandle = phandle;
252 1.1 jmcneill sc->sc_bst = faa->faa_bst;
253 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
254 1.1 jmcneill aprint_error(": couldn't map phy registers\n");
255 1.1 jmcneill return;
256 1.1 jmcneill }
257 1.1 jmcneill
258 1.1 jmcneill sc->sc_nphy = NPHY_ID;
259 1.1 jmcneill sc->sc_phy = kmem_alloc(sizeof(*sc->sc_phy) * sc->sc_nphy, KM_SLEEP);
260 1.1 jmcneill for (n = 0; n < sc->sc_nphy; n++) {
261 1.1 jmcneill sc->sc_phy[n].phy_sc = sc;
262 1.1 jmcneill sc->sc_phy[n].phy_index = n;
263 1.1 jmcneill }
264 1.1 jmcneill
265 1.1 jmcneill sc->sc_pmureg = fdtbus_syscon_acquire(phandle, "samsung,pmu-syscon");
266 1.1 jmcneill if (sc->sc_pmureg == NULL) {
267 1.1 jmcneill aprint_error(": couldn't acquire pmureg syscon\n");
268 1.1 jmcneill return;
269 1.1 jmcneill }
270 1.1 jmcneill
271 1.1 jmcneill /* Enable clocks */
272 1.1 jmcneill clk = fdtbus_clock_get(phandle, "phy");
273 1.1 jmcneill if (clk == NULL || clk_enable(clk) != 0) {
274 1.1 jmcneill aprint_error(": couldn't enable phy clock\n");
275 1.1 jmcneill return;
276 1.1 jmcneill }
277 1.1 jmcneill clk = fdtbus_clock_get(phandle, "ref");
278 1.1 jmcneill if (clk == NULL || clk_enable(clk) != 0) {
279 1.1 jmcneill aprint_error(": couldn't enable ref clock\n");
280 1.1 jmcneill return;
281 1.1 jmcneill }
282 1.1 jmcneill
283 1.1 jmcneill aprint_naive("\n");
284 1.1 jmcneill aprint_normal(": USB DRD PHY\n");
285 1.1 jmcneill
286 1.1 jmcneill exynos_usbdrdphy_init(sc);
287 1.1 jmcneill
288 1.1 jmcneill fdtbus_register_phy_controller(self, phandle, &exynos_usbdrdphy_funcs);
289 1.1 jmcneill }
290