meson_usbphy.c revision 1.2.4.2 1 1.2.4.2 christos /* $NetBSD: meson_usbphy.c,v 1.2.4.2 2019/06/10 22:05:51 christos Exp $ */
2 1.2.4.2 christos
3 1.2.4.2 christos /*-
4 1.2.4.2 christos * Copyright (c) 2019 Jared McNeill <jmcneill (at) invisible.ca>
5 1.2.4.2 christos * All rights reserved.
6 1.2.4.2 christos *
7 1.2.4.2 christos * Redistribution and use in source and binary forms, with or without
8 1.2.4.2 christos * modification, are permitted provided that the following conditions
9 1.2.4.2 christos * are met:
10 1.2.4.2 christos * 1. Redistributions of source code must retain the above copyright
11 1.2.4.2 christos * notice, this list of conditions and the following disclaimer.
12 1.2.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.4.2 christos * notice, this list of conditions and the following disclaimer in the
14 1.2.4.2 christos * documentation and/or other materials provided with the distribution.
15 1.2.4.2 christos *
16 1.2.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.2.4.2 christos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.2.4.2 christos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.2.4.2 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.2.4.2 christos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.2.4.2 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.2.4.2 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.2.4.2 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.2.4.2 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.2.4.2 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.2.4.2 christos * POSSIBILITY OF SUCH DAMAGE.
27 1.2.4.2 christos */
28 1.2.4.2 christos
29 1.2.4.2 christos #include <sys/cdefs.h>
30 1.2.4.2 christos
31 1.2.4.2 christos __KERNEL_RCSID(0, "$NetBSD: meson_usbphy.c,v 1.2.4.2 2019/06/10 22:05:51 christos Exp $");
32 1.2.4.2 christos
33 1.2.4.2 christos #include <sys/param.h>
34 1.2.4.2 christos #include <sys/bus.h>
35 1.2.4.2 christos #include <sys/device.h>
36 1.2.4.2 christos #include <sys/intr.h>
37 1.2.4.2 christos #include <sys/systm.h>
38 1.2.4.2 christos #include <sys/time.h>
39 1.2.4.2 christos
40 1.2.4.2 christos #include <dev/fdt/fdtvar.h>
41 1.2.4.2 christos
42 1.2.4.2 christos #define CBUS_REG(x) ((x) << 2)
43 1.2.4.2 christos #define PREI_USB_PHY_CFG_REG CBUS_REG(0x00)
44 1.2.4.2 christos #define PREI_USB_PHY_CFG_CLK_32K_ALT_SEL __BIT(15)
45 1.2.4.2 christos #define PREI_USB_PHY_CTRL_REG CBUS_REG(0x01)
46 1.2.4.2 christos #define PREI_USB_PHY_CTRL_FSEL __BITS(24,22)
47 1.2.4.2 christos #define PREI_USB_PHY_CTRL_FSEL_24M 5
48 1.2.4.2 christos #define PREI_USB_PHY_CTRL_FSEL_12M 2
49 1.2.4.2 christos #define PREI_USB_PHY_CTRL_POR __BIT(15)
50 1.2.4.2 christos #define PREI_USB_PHY_CTRL_CLK_DET __BIT(8)
51 1.2.4.2 christos #define PREI_USB_PHY_ADP_BC_REG CBUS_REG(0x03)
52 1.2.4.2 christos #define PREI_USB_PHY_ADP_BC_ACA_FLOATING __BIT(26)
53 1.2.4.2 christos #define PREI_USB_PHY_ADP_BC_ACA_ENABLE __BIT(16)
54 1.2.4.2 christos
55 1.2.4.2 christos static int meson_usbphy_match(device_t, cfdata_t, void *);
56 1.2.4.2 christos static void meson_usbphy_attach(device_t, device_t, void *);
57 1.2.4.2 christos
58 1.2.4.2 christos enum meson_usbphy_type {
59 1.2.4.2 christos USBPHY_MESON8B,
60 1.2.4.2 christos };
61 1.2.4.2 christos
62 1.2.4.2 christos static const struct of_compat_data compat_data[] = {
63 1.2.4.2 christos { "amlogic,meson8b-usb2-phy", USBPHY_MESON8B },
64 1.2.4.2 christos { "amlogic,meson-gxbb-usb2-phy", USBPHY_MESON8B },
65 1.2.4.2 christos { NULL }
66 1.2.4.2 christos };
67 1.2.4.2 christos
68 1.2.4.2 christos struct meson_usbphy_softc {
69 1.2.4.2 christos device_t sc_dev;
70 1.2.4.2 christos bus_space_tag_t sc_bst;
71 1.2.4.2 christos bus_space_handle_t sc_bsh;
72 1.2.4.2 christos int sc_phandle;
73 1.2.4.2 christos const char *sc_dr_mode;
74 1.2.4.2 christos enum meson_usbphy_type sc_type;
75 1.2.4.2 christos };
76 1.2.4.2 christos
77 1.2.4.2 christos #define PHY_READ(sc, reg) \
78 1.2.4.2 christos bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
79 1.2.4.2 christos #define PHY_WRITE(sc, reg, val) \
80 1.2.4.2 christos bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
81 1.2.4.2 christos
82 1.2.4.2 christos CFATTACH_DECL_NEW(meson_usbphy, sizeof(struct meson_usbphy_softc),
83 1.2.4.2 christos meson_usbphy_match, meson_usbphy_attach, NULL, NULL);
84 1.2.4.2 christos
85 1.2.4.2 christos static const char *
86 1.2.4.2 christos meson_usbphy_dr_mode(struct meson_usbphy_softc *sc)
87 1.2.4.2 christos {
88 1.2.4.2 christos int index, phandle;
89 1.2.4.2 christos
90 1.2.4.2 christos index = 0;
91 1.2.4.2 christos while ((phandle = fdt_find_with_property("phys", &index)) != -1) {
92 1.2.4.2 christos const int phy_phandle = fdtbus_get_phandle(phandle, "phys");
93 1.2.4.2 christos if (phy_phandle != sc->sc_phandle)
94 1.2.4.2 christos continue;
95 1.2.4.2 christos return fdtbus_get_string(phandle, "dr_mode");
96 1.2.4.2 christos }
97 1.2.4.2 christos
98 1.2.4.2 christos return NULL;
99 1.2.4.2 christos }
100 1.2.4.2 christos
101 1.2.4.2 christos static void *
102 1.2.4.2 christos meson_usbphy_acquire(device_t dev, const void *data, size_t len)
103 1.2.4.2 christos {
104 1.2.4.2 christos if (len != 0)
105 1.2.4.2 christos return NULL;
106 1.2.4.2 christos
107 1.2.4.2 christos return (void *)(uintptr_t)1;
108 1.2.4.2 christos }
109 1.2.4.2 christos
110 1.2.4.2 christos static void
111 1.2.4.2 christos meson_usbphy_release(device_t dev, void *priv)
112 1.2.4.2 christos {
113 1.2.4.2 christos }
114 1.2.4.2 christos
115 1.2.4.2 christos static int
116 1.2.4.2 christos meson_usbphy_enable(device_t dev, void *priv, bool enable)
117 1.2.4.2 christos {
118 1.2.4.2 christos struct meson_usbphy_softc * const sc = device_private(dev);
119 1.2.4.2 christos struct fdtbus_regulator *reg;
120 1.2.4.2 christos uint32_t val;
121 1.2.4.2 christos int error;
122 1.2.4.2 christos
123 1.2.4.2 christos if (enable) {
124 1.2.4.2 christos if (of_hasprop(sc->sc_phandle, "phy-supply")) {
125 1.2.4.2 christos reg = fdtbus_regulator_acquire(sc->sc_phandle, "phy-supply");
126 1.2.4.2 christos if (reg != NULL) {
127 1.2.4.2 christos error = fdtbus_regulator_enable(reg);
128 1.2.4.2 christos if (error != 0)
129 1.2.4.2 christos device_printf(dev, "WARNING: couldn't enable phy-supply: %d\n", error);
130 1.2.4.2 christos } else {
131 1.2.4.2 christos device_printf(dev, "WARNING: couldn't acquire phy-supply\n");
132 1.2.4.2 christos }
133 1.2.4.2 christos }
134 1.2.4.2 christos
135 1.2.4.2 christos delay(1000);
136 1.2.4.2 christos
137 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_CFG_REG);
138 1.2.4.2 christos val |= PREI_USB_PHY_CFG_CLK_32K_ALT_SEL;
139 1.2.4.2 christos PHY_WRITE(sc, PREI_USB_PHY_CFG_REG, val);
140 1.2.4.2 christos
141 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
142 1.2.4.2 christos val &= ~PREI_USB_PHY_CTRL_FSEL;
143 1.2.4.2 christos val |= __SHIFTIN(PREI_USB_PHY_CTRL_FSEL_24M,
144 1.2.4.2 christos PREI_USB_PHY_CTRL_FSEL);
145 1.2.4.2 christos val |= PREI_USB_PHY_CTRL_POR;
146 1.2.4.2 christos PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
147 1.2.4.2 christos
148 1.2.4.2 christos delay(1000);
149 1.2.4.2 christos
150 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
151 1.2.4.2 christos val &= ~PREI_USB_PHY_CTRL_POR;
152 1.2.4.2 christos PHY_WRITE(sc, PREI_USB_PHY_CTRL_REG, val);
153 1.2.4.2 christos
154 1.2.4.2 christos delay(50000);
155 1.2.4.2 christos
156 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_CTRL_REG);
157 1.2.4.2 christos if ((val & PREI_USB_PHY_CTRL_CLK_DET) == 0)
158 1.2.4.2 christos aprint_error_dev(dev, "WARNING: USB PHY clock not detected\n");
159 1.2.4.2 christos
160 1.2.4.2 christos if (sc->sc_dr_mode && strcmp(sc->sc_dr_mode, "host") == 0) {
161 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
162 1.2.4.2 christos val |= PREI_USB_PHY_ADP_BC_ACA_ENABLE;
163 1.2.4.2 christos PHY_WRITE(sc, PREI_USB_PHY_ADP_BC_REG, val);
164 1.2.4.2 christos
165 1.2.4.2 christos delay(1000);
166 1.2.4.2 christos
167 1.2.4.2 christos val = PHY_READ(sc, PREI_USB_PHY_ADP_BC_REG);
168 1.2.4.2 christos if ((val & PREI_USB_PHY_ADP_BC_ACA_FLOATING) != 0)
169 1.2.4.2 christos aprint_error_dev(dev, "WARNING: USB PHY failed to enable ACA detection\n");
170 1.2.4.2 christos }
171 1.2.4.2 christos }
172 1.2.4.2 christos
173 1.2.4.2 christos return 0;
174 1.2.4.2 christos }
175 1.2.4.2 christos
176 1.2.4.2 christos const struct fdtbus_phy_controller_func meson_usbphy_funcs = {
177 1.2.4.2 christos .acquire = meson_usbphy_acquire,
178 1.2.4.2 christos .release = meson_usbphy_release,
179 1.2.4.2 christos .enable = meson_usbphy_enable,
180 1.2.4.2 christos };
181 1.2.4.2 christos
182 1.2.4.2 christos static int
183 1.2.4.2 christos meson_usbphy_match(device_t parent, cfdata_t cf, void *aux)
184 1.2.4.2 christos {
185 1.2.4.2 christos struct fdt_attach_args * const faa = aux;
186 1.2.4.2 christos
187 1.2.4.2 christos return of_match_compat_data(faa->faa_phandle, compat_data);
188 1.2.4.2 christos }
189 1.2.4.2 christos
190 1.2.4.2 christos static void
191 1.2.4.2 christos meson_usbphy_attach(device_t parent, device_t self, void *aux)
192 1.2.4.2 christos {
193 1.2.4.2 christos struct meson_usbphy_softc * const sc = device_private(self);
194 1.2.4.2 christos struct fdt_attach_args * const faa = aux;
195 1.2.4.2 christos const int phandle = faa->faa_phandle;
196 1.2.4.2 christos struct fdtbus_reset *rst;
197 1.2.4.2 christos struct clk *clk;
198 1.2.4.2 christos bus_addr_t addr;
199 1.2.4.2 christos bus_size_t size;
200 1.2.4.2 christos u_int n;
201 1.2.4.2 christos
202 1.2.4.2 christos sc->sc_dev = self;
203 1.2.4.2 christos sc->sc_bst = faa->faa_bst;
204 1.2.4.2 christos sc->sc_phandle = phandle;
205 1.2.4.2 christos sc->sc_type = of_search_compatible(phandle, compat_data)->data;
206 1.2.4.2 christos
207 1.2.4.2 christos if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
208 1.2.4.2 christos aprint_error(": couldn't get registers\n");
209 1.2.4.2 christos return;
210 1.2.4.2 christos }
211 1.2.4.2 christos if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
212 1.2.4.2 christos aprint_error(": couldn't map registers\n");
213 1.2.4.2 christos return;
214 1.2.4.2 christos }
215 1.2.4.2 christos
216 1.2.4.2 christos /* Enable clocks */
217 1.2.4.2 christos for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
218 1.2.4.2 christos if (clk_enable(clk) != 0) {
219 1.2.4.2 christos aprint_error(": couldn't enable clock #%d\n", n);
220 1.2.4.2 christos return;
221 1.2.4.2 christos }
222 1.2.4.2 christos /* De-assert resets */
223 1.2.4.2 christos for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
224 1.2.4.2 christos if (fdtbus_reset_deassert(rst) != 0) {
225 1.2.4.2 christos aprint_error(": couldn't de-assert reset #%d\n", n);
226 1.2.4.2 christos return;
227 1.2.4.2 christos }
228 1.2.4.2 christos
229 1.2.4.2 christos sc->sc_dr_mode = meson_usbphy_dr_mode(sc);
230 1.2.4.2 christos
231 1.2.4.2 christos aprint_naive("\n");
232 1.2.4.2 christos aprint_normal(": USB2 PHY (%s)\n", sc->sc_dr_mode ? sc->sc_dr_mode : "unknown mode");
233 1.2.4.2 christos
234 1.2.4.2 christos fdtbus_register_phy_controller(self, phandle, &meson_usbphy_funcs);
235 1.2.4.2 christos }
236