sunxi_musb.c revision 1.1 1 /* $NetBSD: sunxi_musb.c,v 1.1 2017/09/09 12:01:04 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2017 Jared McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: sunxi_musb.c,v 1.1 2017/09/09 12:01:04 jmcneill Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/device.h>
35 #include <sys/intr.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/pool.h>
39
40 #include <dev/usb/usb.h>
41 #include <dev/usb/usbdi.h>
42 #include <dev/usb/usbdivar.h>
43 #include <dev/usb/motgvar.h>
44
45 #include <dev/fdt/fdtvar.h>
46
47 #define MUSB2_REG_AWIN_VEND0 0x43
48 #define MUSB2_REG_INTTX 0x44
49 #define MUSB2_REG_INTRX 0x46
50 #define MUSB2_REG_INTUSB 0x4c
51
52 static int sunxi_musb_match(device_t, cfdata_t, void *);
53 static void sunxi_musb_attach(device_t, device_t, void *);
54
55 CFATTACH_DECL_NEW(sunxi_musb, sizeof(struct motg_softc),
56 sunxi_musb_match, sunxi_musb_attach, NULL, NULL);
57
58 static const struct of_compat_data compat_data[] = {
59 { "allwinner,sun4i-a10-musb", 5 },
60 { "allwinner,sun6i-a13-musb", 5 },
61 { "allwinner,sun8i-h3-musb", 4 },
62 { "allwinner,sun8i-a33-musb", 5 },
63 { NULL }
64 };
65
66 static int
67 sunxi_musb_intr(void *priv)
68 {
69 struct motg_softc * const sc = priv;
70 uint16_t inttx, intrx;
71 uint8_t intusb;
72
73 mutex_enter(&sc->sc_intr_lock);
74
75 intusb = bus_space_read_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB);
76 inttx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX);
77 intrx = bus_space_read_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX);
78 if (!intusb && !inttx && !intrx) {
79 mutex_exit(&sc->sc_intr_lock);
80 return 0;
81 }
82
83 if (intusb)
84 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTUSB, intusb);
85 if (inttx)
86 bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTTX, inttx);
87 if (intrx)
88 bus_space_write_2(sc->sc_iot, sc->sc_ioh, MUSB2_REG_INTRX, intrx);
89
90 motg_intr(sc, intrx, inttx, intusb);
91
92 mutex_exit(&sc->sc_intr_lock);
93
94 return 1;
95 }
96
97 static void
98 sunxi_musb_poll(void *priv)
99 {
100 sunxi_musb_intr(priv);
101 }
102
103 static int
104 sunxi_musb_match(device_t parent, cfdata_t cf, void *aux)
105 {
106 struct fdt_attach_args * const faa = aux;
107
108 return of_match_compat_data(faa->faa_phandle, compat_data);
109 }
110
111 static void
112 sunxi_musb_attach(device_t parent, device_t self, void *aux)
113 {
114 struct motg_softc * const sc = device_private(self);
115 struct fdt_attach_args * const faa = aux;
116 const int phandle = faa->faa_phandle;
117 struct fdtbus_reset *rst;
118 struct fdtbus_phy *phy;
119 struct clk *clk;
120 char intrstr[128];
121 const char *dr_mode;
122 bus_addr_t addr;
123 bus_size_t size;
124 void *ih;
125 u_int n;
126
127 /* Only "host" mode is supported */
128 dr_mode = fdtbus_get_string(phandle, "dr_mode");
129 if (dr_mode == NULL || strcmp(dr_mode, "host") != 0) {
130 aprint_normal(": '%s' mode not supported\n", dr_mode);
131 return;
132 }
133
134 if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
135 aprint_error(": couldn't get registers\n");
136 return;
137 }
138
139 /* Enable clocks */
140 for (n = 0; (clk = fdtbus_clock_get_index(phandle, n)) != NULL; n++)
141 if (clk_enable(clk) != 0) {
142 aprint_error(": couldn't enable clock #%d\n", n);
143 return;
144 }
145 /* De-assert resets */
146 for (n = 0; (rst = fdtbus_reset_get_index(phandle, n)) != NULL; n++)
147 if (fdtbus_reset_deassert(rst) != 0) {
148 aprint_error(": couldn't de-assert reset #%d\n", n);
149 return;
150 }
151
152 /* Enable optional phy */
153 phy = fdtbus_phy_get(phandle, "usb");
154 if (phy && fdtbus_phy_enable(phy, true) != 0) {
155 aprint_error(": couldn't enable phy\n");
156 return;
157 }
158
159 sc->sc_dev = self;
160 sc->sc_bus.ub_hcpriv = sc;
161 sc->sc_bus.ub_dmatag = faa->faa_dmat;
162 strlcpy(sc->sc_vendor, "Allwinner", sizeof(sc->sc_vendor));
163 sc->sc_size = size;
164 sc->sc_iot = faa->faa_bst;
165 if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
166 aprint_error(": couldn't map registers\n");
167 return;
168 }
169 sc->sc_intr_poll = sunxi_musb_poll;
170 sc->sc_intr_poll_arg = sc;
171 sc->sc_mode = MOTG_MODE_HOST;
172 sc->sc_ep_max = of_search_compatible(phandle, compat_data)->data;
173 sc->sc_ep_fifosize = 512;
174
175 aprint_naive("\n");
176 aprint_normal(": USB OTG\n");
177
178 if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
179 aprint_error_dev(self, "failed to decode interrupt\n");
180 return;
181 }
182
183 ih = fdtbus_intr_establish(phandle, 0, IPL_USB, FDT_INTR_MPSAFE,
184 sunxi_musb_intr, sc);
185 if (ih == NULL) {
186 aprint_error_dev(self, "couldn't establish interrupt on %s\n",
187 intrstr);
188 return;
189 }
190 aprint_normal_dev(self, "interrupting on %s\n", intrstr);
191
192 bus_space_write_1(sc->sc_iot, sc->sc_ioh, MUSB2_REG_AWIN_VEND0, 0);
193
194 motg_init(sc);
195 }
196