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