mesongxl_usb2phy.c revision 1.2 1 1.2 thorpej /* $NetBSD: mesongxl_usb2phy.c,v 1.2 2021/01/27 03:10:18 thorpej Exp $ */
2 1.1 jmcneill
3 1.1 jmcneill /*-
4 1.1 jmcneill * Copyright (c) 2019 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.2 thorpej __KERNEL_RCSID(0, "$NetBSD: mesongxl_usb2phy.c,v 1.2 2021/01/27 03:10:18 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/time.h>
39 1.1 jmcneill
40 1.1 jmcneill #include <dev/fdt/fdtvar.h>
41 1.1 jmcneill
42 1.1 jmcneill #define USB2PHY_REG0 0x00
43 1.1 jmcneill #define REG0_POWER_ON_RESET __BIT(22)
44 1.1 jmcneill #define REG0_ID_PULLUP __BIT(13)
45 1.1 jmcneill #define REG0_DP_PULLDOWN __BIT(6)
46 1.1 jmcneill #define REG0_DM_PULLDOWN __BIT(5)
47 1.1 jmcneill
48 1.1 jmcneill static int mesongxl_usb2phy_match(device_t, cfdata_t, void *);
49 1.1 jmcneill static void mesongxl_usb2phy_attach(device_t, device_t, void *);
50 1.1 jmcneill
51 1.2 thorpej static const struct device_compatible_entry compat_data[] = {
52 1.2 thorpej { .compat = "amlogic,meson-gxl-usb2-phy" },
53 1.2 thorpej DEVICE_COMPAT_EOL
54 1.1 jmcneill };
55 1.1 jmcneill
56 1.1 jmcneill struct mesongxl_usb2phy_softc {
57 1.1 jmcneill device_t sc_dev;
58 1.1 jmcneill bus_space_tag_t sc_bst;
59 1.1 jmcneill bus_space_handle_t sc_bsh;
60 1.1 jmcneill int sc_phandle;
61 1.1 jmcneill struct clk *sc_clk;
62 1.1 jmcneill struct fdtbus_reset *sc_rst;
63 1.1 jmcneill struct fdtbus_regulator *sc_supply;
64 1.1 jmcneill };
65 1.1 jmcneill
66 1.1 jmcneill #define PHY_READ(sc, reg) \
67 1.1 jmcneill bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
68 1.1 jmcneill #define PHY_WRITE(sc, reg, val) \
69 1.1 jmcneill bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
70 1.1 jmcneill
71 1.1 jmcneill CFATTACH_DECL_NEW(mesongxl_usb2phy, sizeof(struct mesongxl_usb2phy_softc),
72 1.1 jmcneill mesongxl_usb2phy_match, mesongxl_usb2phy_attach, NULL, NULL);
73 1.1 jmcneill
74 1.1 jmcneill static void *
75 1.1 jmcneill mesongxl_usb2phy_acquire(device_t dev, const void *data, size_t len)
76 1.1 jmcneill {
77 1.1 jmcneill if (len != 0)
78 1.1 jmcneill return NULL;
79 1.1 jmcneill
80 1.1 jmcneill return (void *)(uintptr_t)1;
81 1.1 jmcneill }
82 1.1 jmcneill
83 1.1 jmcneill static void
84 1.1 jmcneill mesongxl_usb2phy_release(device_t dev, void *priv)
85 1.1 jmcneill {
86 1.1 jmcneill }
87 1.1 jmcneill
88 1.1 jmcneill static int
89 1.1 jmcneill mesongxl_usb2phy_enable(device_t dev, void *priv, bool enable)
90 1.1 jmcneill {
91 1.1 jmcneill struct mesongxl_usb2phy_softc * const sc = device_private(dev);
92 1.1 jmcneill uint32_t val;
93 1.1 jmcneill
94 1.1 jmcneill if (enable) {
95 1.1 jmcneill /* Power on PHY */
96 1.1 jmcneill val = PHY_READ(sc, USB2PHY_REG0);
97 1.1 jmcneill val &= ~REG0_POWER_ON_RESET;
98 1.1 jmcneill PHY_WRITE(sc, USB2PHY_REG0, val);
99 1.1 jmcneill
100 1.1 jmcneill /* Configure PHY for host mode */
101 1.1 jmcneill val = PHY_READ(sc, USB2PHY_REG0);
102 1.1 jmcneill val |= REG0_DM_PULLDOWN;
103 1.1 jmcneill val |= REG0_DP_PULLDOWN;
104 1.1 jmcneill val &= ~REG0_ID_PULLUP;
105 1.1 jmcneill PHY_WRITE(sc, USB2PHY_REG0, val);
106 1.1 jmcneill
107 1.1 jmcneill /* Reset the PHY */
108 1.1 jmcneill val = PHY_READ(sc, USB2PHY_REG0);
109 1.1 jmcneill val |= REG0_POWER_ON_RESET;
110 1.1 jmcneill PHY_WRITE(sc, USB2PHY_REG0, val);
111 1.1 jmcneill delay(500);
112 1.1 jmcneill val = PHY_READ(sc, USB2PHY_REG0);
113 1.1 jmcneill val &= ~REG0_POWER_ON_RESET;
114 1.1 jmcneill PHY_WRITE(sc, USB2PHY_REG0, val);
115 1.1 jmcneill delay(500);
116 1.1 jmcneill
117 1.1 jmcneill if (sc->sc_supply != NULL) {
118 1.1 jmcneill if (fdtbus_regulator_enable(sc->sc_supply) != 0)
119 1.1 jmcneill aprint_error(": couldn't enable supply\n");
120 1.1 jmcneill }
121 1.1 jmcneill } else {
122 1.1 jmcneill /* Power off PHY */
123 1.1 jmcneill val = PHY_READ(sc, USB2PHY_REG0);
124 1.1 jmcneill val |= REG0_POWER_ON_RESET;
125 1.1 jmcneill PHY_WRITE(sc, USB2PHY_REG0, val);
126 1.1 jmcneill }
127 1.1 jmcneill
128 1.1 jmcneill return 0;
129 1.1 jmcneill }
130 1.1 jmcneill
131 1.1 jmcneill const struct fdtbus_phy_controller_func mesongxl_usb2phy_funcs = {
132 1.1 jmcneill .acquire = mesongxl_usb2phy_acquire,
133 1.1 jmcneill .release = mesongxl_usb2phy_release,
134 1.1 jmcneill .enable = mesongxl_usb2phy_enable,
135 1.1 jmcneill };
136 1.1 jmcneill
137 1.1 jmcneill static int
138 1.1 jmcneill mesongxl_usb2phy_match(device_t parent, cfdata_t cf, void *aux)
139 1.1 jmcneill {
140 1.1 jmcneill struct fdt_attach_args * const faa = aux;
141 1.1 jmcneill
142 1.2 thorpej return of_compatible_match(faa->faa_phandle, compat_data);
143 1.1 jmcneill }
144 1.1 jmcneill
145 1.1 jmcneill static void
146 1.1 jmcneill mesongxl_usb2phy_attach(device_t parent, device_t self, void *aux)
147 1.1 jmcneill {
148 1.1 jmcneill struct mesongxl_usb2phy_softc * const sc = device_private(self);
149 1.1 jmcneill struct fdt_attach_args * const faa = aux;
150 1.1 jmcneill const int phandle = faa->faa_phandle;
151 1.1 jmcneill bus_addr_t addr;
152 1.1 jmcneill bus_size_t size;
153 1.1 jmcneill
154 1.1 jmcneill sc->sc_dev = self;
155 1.1 jmcneill sc->sc_bst = faa->faa_bst;
156 1.1 jmcneill sc->sc_phandle = phandle;
157 1.1 jmcneill
158 1.1 jmcneill if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
159 1.1 jmcneill aprint_error(": couldn't get registers\n");
160 1.1 jmcneill return;
161 1.1 jmcneill }
162 1.1 jmcneill if (bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
163 1.1 jmcneill aprint_error(": couldn't map registers\n");
164 1.1 jmcneill return;
165 1.1 jmcneill }
166 1.1 jmcneill
167 1.1 jmcneill sc->sc_clk = fdtbus_clock_get_index(phandle, 0);
168 1.1 jmcneill if (sc->sc_clk == NULL) {
169 1.1 jmcneill aprint_error(": couldn't get clock\n");
170 1.1 jmcneill return;
171 1.1 jmcneill }
172 1.1 jmcneill sc->sc_rst = fdtbus_reset_get_index(phandle, 0);
173 1.1 jmcneill
174 1.1 jmcneill if (sc->sc_rst != NULL) {
175 1.1 jmcneill if (fdtbus_reset_deassert(sc->sc_rst) != 0) {
176 1.1 jmcneill aprint_error(": couldn't de-assert reset\n");
177 1.1 jmcneill return;
178 1.1 jmcneill }
179 1.1 jmcneill }
180 1.1 jmcneill if (clk_enable(sc->sc_clk) != 0) {
181 1.1 jmcneill aprint_error(": couldn't enable clock\n");
182 1.1 jmcneill return;
183 1.1 jmcneill }
184 1.1 jmcneill
185 1.1 jmcneill sc->sc_supply = fdtbus_regulator_acquire(phandle, "phy-supply");
186 1.1 jmcneill
187 1.1 jmcneill aprint_naive("\n");
188 1.1 jmcneill aprint_normal(": USB2 PHY\n");
189 1.1 jmcneill
190 1.1 jmcneill fdtbus_register_phy_controller(self, phandle, &mesongxl_usb2phy_funcs);
191 1.1 jmcneill }
192