1029e367dSAndriy Gapon /*-
2029e367dSAndriy Gapon * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
3029e367dSAndriy Gapon * Copyright (c) 2018 Andrew Turner <andrew@FreeBSD.org>
4029e367dSAndriy Gapon * All rights reserved.
5029e367dSAndriy Gapon *
6029e367dSAndriy Gapon * This software was developed by SRI International and the University of
7029e367dSAndriy Gapon * Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-10-C-0237
8029e367dSAndriy Gapon * ("CTSRD"), as part of the DARPA CRASH research programme.
9029e367dSAndriy Gapon *
10029e367dSAndriy Gapon * Redistribution and use in source and binary forms, with or without
11029e367dSAndriy Gapon * modification, are permitted provided that the following conditions
12029e367dSAndriy Gapon * are met:
13029e367dSAndriy Gapon * 1. Redistributions of source code must retain the above copyright
14029e367dSAndriy Gapon * notice, this list of conditions and the following disclaimer.
15029e367dSAndriy Gapon * 2. Redistributions in binary form must reproduce the above copyright
16029e367dSAndriy Gapon * notice, this list of conditions and the following disclaimer in the
17029e367dSAndriy Gapon * documentation and/or other materials provided with the distribution.
18029e367dSAndriy Gapon *
19029e367dSAndriy Gapon * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20029e367dSAndriy Gapon * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21029e367dSAndriy Gapon * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22029e367dSAndriy Gapon * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23029e367dSAndriy Gapon * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24029e367dSAndriy Gapon * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25029e367dSAndriy Gapon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26029e367dSAndriy Gapon * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27029e367dSAndriy Gapon * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28029e367dSAndriy Gapon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29029e367dSAndriy Gapon * SUCH DAMAGE.
30029e367dSAndriy Gapon */
31029e367dSAndriy Gapon
32029e367dSAndriy Gapon /*
33029e367dSAndriy Gapon * Allwinner USB Dual-Role Device (DRD) controller
34029e367dSAndriy Gapon */
35029e367dSAndriy Gapon
36029e367dSAndriy Gapon #include <sys/param.h>
37029e367dSAndriy Gapon #include <sys/systm.h>
38029e367dSAndriy Gapon #include <sys/bus.h>
39029e367dSAndriy Gapon #include <sys/rman.h>
40029e367dSAndriy Gapon #include <sys/kernel.h>
41029e367dSAndriy Gapon #include <sys/condvar.h>
42029e367dSAndriy Gapon #include <sys/module.h>
4326d78633SMitchell Horne
44029e367dSAndriy Gapon #include <machine/bus.h>
45029e367dSAndriy Gapon
46029e367dSAndriy Gapon #include <dev/ofw/ofw_bus.h>
47029e367dSAndriy Gapon #include <dev/ofw/ofw_bus_subr.h>
48029e367dSAndriy Gapon
49029e367dSAndriy Gapon #include <dev/usb/usb.h>
50029e367dSAndriy Gapon #include <dev/usb/usbdi.h>
51029e367dSAndriy Gapon
52029e367dSAndriy Gapon #include <dev/usb/usb_core.h>
53029e367dSAndriy Gapon #include <dev/usb/usb_busdma.h>
54029e367dSAndriy Gapon #include <dev/usb/usb_process.h>
55029e367dSAndriy Gapon #include <dev/usb/usb_util.h>
56029e367dSAndriy Gapon
57029e367dSAndriy Gapon #include <dev/usb/usb_controller.h>
58029e367dSAndriy Gapon #include <dev/usb/usb_bus.h>
59029e367dSAndriy Gapon #include <dev/usb/controller/musb_otg.h>
60029e367dSAndriy Gapon
61be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h>
621f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h>
63950a6087SEmmanuel Vadot #include <dev/phy/phy.h>
64950a6087SEmmanuel Vadot #include <dev/phy/phy_usb.h>
65029e367dSAndriy Gapon
66029e367dSAndriy Gapon #ifdef __arm__
67029e367dSAndriy Gapon #include <arm/allwinner/aw_machdep.h>
68029e367dSAndriy Gapon #include <arm/allwinner/a10_sramc.h>
69029e367dSAndriy Gapon #endif
70029e367dSAndriy Gapon
71029e367dSAndriy Gapon #define DRD_EP_MAX 5
72f2066952SAndriy Gapon #define DRD_EP_MAX_H3 4
73029e367dSAndriy Gapon
74029e367dSAndriy Gapon #define MUSB2_REG_AWIN_VEND0 0x0043
75029e367dSAndriy Gapon #define VEND0_PIO_MODE 0
76029e367dSAndriy Gapon
77029e367dSAndriy Gapon #if defined(__arm__)
78029e367dSAndriy Gapon #define bs_parent_space(bs) ((bs)->bs_parent)
79029e367dSAndriy Gapon typedef bus_space_tag_t awusb_bs_tag;
80*9eb30ef4SMitchell Horne #elif defined(__aarch64__) || defined(__riscv)
81029e367dSAndriy Gapon #define bs_parent_space(bs) (bs)
82029e367dSAndriy Gapon typedef void * awusb_bs_tag;
83029e367dSAndriy Gapon #endif
84029e367dSAndriy Gapon
85029e367dSAndriy Gapon #define AWUSB_OKAY 0x01
86029e367dSAndriy Gapon #define AWUSB_NO_CONFDATA 0x02
87029e367dSAndriy Gapon static struct ofw_compat_data compat_data[] = {
88029e367dSAndriy Gapon { "allwinner,sun4i-a10-musb", AWUSB_OKAY },
89029e367dSAndriy Gapon { "allwinner,sun6i-a31-musb", AWUSB_OKAY },
90029e367dSAndriy Gapon { "allwinner,sun8i-a33-musb", AWUSB_OKAY | AWUSB_NO_CONFDATA },
91f2066952SAndriy Gapon { "allwinner,sun8i-h3-musb", AWUSB_OKAY | AWUSB_NO_CONFDATA },
92*9eb30ef4SMitchell Horne { "allwinner,sun20i-d1-musb", AWUSB_OKAY | AWUSB_NO_CONFDATA },
93029e367dSAndriy Gapon { NULL, 0 }
94029e367dSAndriy Gapon };
95029e367dSAndriy Gapon
96029e367dSAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner[] = {
97029e367dSAndriy Gapon {
98f2066952SAndriy Gapon .ep_end = DRD_EP_MAX,
99f2066952SAndriy Gapon .ep_fifosz_shift = 9,
100f2066952SAndriy Gapon .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512,
101f2066952SAndriy Gapon },
102f2066952SAndriy Gapon {
103f2066952SAndriy Gapon .ep_end = -1,
104f2066952SAndriy Gapon },
105f2066952SAndriy Gapon };
106f2066952SAndriy Gapon
107f2066952SAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner_h3[] = {
108f2066952SAndriy Gapon {
109f2066952SAndriy Gapon .ep_end = DRD_EP_MAX_H3,
110029e367dSAndriy Gapon .ep_fifosz_shift = 9,
111029e367dSAndriy Gapon .ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512,
112029e367dSAndriy Gapon },
113029e367dSAndriy Gapon {
114029e367dSAndriy Gapon .ep_end = -1,
115029e367dSAndriy Gapon },
116029e367dSAndriy Gapon };
117029e367dSAndriy Gapon
118029e367dSAndriy Gapon struct awusbdrd_softc {
119029e367dSAndriy Gapon struct musbotg_softc sc;
120029e367dSAndriy Gapon struct resource *res[2];
121029e367dSAndriy Gapon clk_t clk;
122029e367dSAndriy Gapon hwreset_t reset;
1231668d773SAndriy Gapon phy_t phy;
124029e367dSAndriy Gapon struct bus_space bs;
125029e367dSAndriy Gapon int flags;
126029e367dSAndriy Gapon };
127029e367dSAndriy Gapon
128029e367dSAndriy Gapon static struct resource_spec awusbdrd_spec[] = {
129029e367dSAndriy Gapon { SYS_RES_MEMORY, 0, RF_ACTIVE },
130029e367dSAndriy Gapon { SYS_RES_IRQ, 0, RF_ACTIVE },
131029e367dSAndriy Gapon { -1, 0 }
132029e367dSAndriy Gapon };
133029e367dSAndriy Gapon
134029e367dSAndriy Gapon #define REMAPFLAG 0x8000
135029e367dSAndriy Gapon #define REGDECL(a, b) [(a)] = ((b) | REMAPFLAG)
136029e367dSAndriy Gapon
137029e367dSAndriy Gapon /* Allwinner USB DRD register mappings */
138029e367dSAndriy Gapon static const uint16_t awusbdrd_regmap[] = {
139029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(0), 0x0000),
140029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(1), 0x0004),
141029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(2), 0x0008),
142029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(3), 0x000c),
143029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(4), 0x0010),
144029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPFIFO(5), 0x0014),
145029e367dSAndriy Gapon REGDECL(MUSB2_REG_POWER, 0x0040),
146029e367dSAndriy Gapon REGDECL(MUSB2_REG_DEVCTL, 0x0041),
147029e367dSAndriy Gapon REGDECL(MUSB2_REG_EPINDEX, 0x0042),
148029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTTX, 0x0044),
149029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTRX, 0x0046),
150029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTTXE, 0x0048),
151029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTRXE, 0x004a),
152029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTUSB, 0x004c),
153029e367dSAndriy Gapon REGDECL(MUSB2_REG_INTUSBE, 0x0050),
154029e367dSAndriy Gapon REGDECL(MUSB2_REG_FRAME, 0x0054),
155029e367dSAndriy Gapon REGDECL(MUSB2_REG_TESTMODE, 0x007c),
156029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXMAXP, 0x0080),
157029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXCSRL, 0x0082),
158029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXCSRH, 0x0083),
159029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXMAXP, 0x0084),
160029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCSRL, 0x0086),
161029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCSRH, 0x0087),
162029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXCOUNT, 0x0088),
163029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXTI, 0x008c),
164029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXNAKLIMIT, 0x008d),
165029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXNAKLIMIT, 0x008f),
166029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXTI, 0x008e),
167029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFIFOSZ, 0x0090),
168029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFIFOADD, 0x0092),
169029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFIFOSZ, 0x0094),
170029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFIFOADD, 0x0096),
171029e367dSAndriy Gapon REGDECL(MUSB2_REG_FADDR, 0x0098),
172029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(0), 0x0098),
173029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(0), 0x009a),
174029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(0), 0x009b),
175029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(0), 0x009c),
176029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(0), 0x009e),
177029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(0), 0x009f),
178029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(1), 0x0098),
179029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(1), 0x009a),
180029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(1), 0x009b),
181029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(1), 0x009c),
182029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(1), 0x009e),
183029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(1), 0x009f),
184029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(2), 0x0098),
185029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(2), 0x009a),
186029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(2), 0x009b),
187029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(2), 0x009c),
188029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(2), 0x009e),
189029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(2), 0x009f),
190029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(3), 0x0098),
191029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(3), 0x009a),
192029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(3), 0x009b),
193029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(3), 0x009c),
194029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(3), 0x009e),
195029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(3), 0x009f),
196029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(4), 0x0098),
197029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(4), 0x009a),
198029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(4), 0x009b),
199029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(4), 0x009c),
200029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(4), 0x009e),
201029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(4), 0x009f),
202029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXFADDR(5), 0x0098),
203029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHADDR(5), 0x009a),
204029e367dSAndriy Gapon REGDECL(MUSB2_REG_TXHUBPORT(5), 0x009b),
205029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXFADDR(5), 0x009c),
206029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHADDR(5), 0x009e),
207029e367dSAndriy Gapon REGDECL(MUSB2_REG_RXHUBPORT(5), 0x009f),
208029e367dSAndriy Gapon REGDECL(MUSB2_REG_CONFDATA, 0x00c0),
209029e367dSAndriy Gapon };
210029e367dSAndriy Gapon
211029e367dSAndriy Gapon static bus_size_t
awusbdrd_reg(bus_size_t o)212029e367dSAndriy Gapon awusbdrd_reg(bus_size_t o)
213029e367dSAndriy Gapon {
214029e367dSAndriy Gapon bus_size_t v;
215029e367dSAndriy Gapon
216029e367dSAndriy Gapon KASSERT(o < nitems(awusbdrd_regmap),
217029e367dSAndriy Gapon ("%s: Invalid register %#lx", __func__, o));
218029e367dSAndriy Gapon if (o >= nitems(awusbdrd_regmap))
219029e367dSAndriy Gapon return (o);
220029e367dSAndriy Gapon
221029e367dSAndriy Gapon v = awusbdrd_regmap[o];
222029e367dSAndriy Gapon
223029e367dSAndriy Gapon KASSERT((v & REMAPFLAG) != 0, ("%s: reg %#lx not in regmap",
224029e367dSAndriy Gapon __func__, o));
225029e367dSAndriy Gapon
226029e367dSAndriy Gapon return (v & ~REMAPFLAG);
227029e367dSAndriy Gapon }
228029e367dSAndriy Gapon
229029e367dSAndriy Gapon static int
awusbdrd_filt(bus_size_t o)230029e367dSAndriy Gapon awusbdrd_filt(bus_size_t o)
231029e367dSAndriy Gapon {
232029e367dSAndriy Gapon switch (o) {
233029e367dSAndriy Gapon case MUSB2_REG_MISC:
234029e367dSAndriy Gapon case MUSB2_REG_RXDBDIS:
235029e367dSAndriy Gapon case MUSB2_REG_TXDBDIS:
236029e367dSAndriy Gapon return (1);
237029e367dSAndriy Gapon default:
238029e367dSAndriy Gapon return (0);
239029e367dSAndriy Gapon }
240029e367dSAndriy Gapon }
241029e367dSAndriy Gapon
242029e367dSAndriy Gapon static uint8_t
awusbdrd_bs_r_1(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o)243029e367dSAndriy Gapon awusbdrd_bs_r_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
244029e367dSAndriy Gapon {
24526d78633SMitchell Horne struct bus_space *bs = t;
246029e367dSAndriy Gapon
247029e367dSAndriy Gapon switch (o) {
248029e367dSAndriy Gapon case MUSB2_REG_HWVERS:
249029e367dSAndriy Gapon return (0); /* no known equivalent */
250029e367dSAndriy Gapon }
251029e367dSAndriy Gapon
252029e367dSAndriy Gapon return (bus_space_read_1(bs_parent_space(bs), h, awusbdrd_reg(o)));
253029e367dSAndriy Gapon }
254029e367dSAndriy Gapon
255029e367dSAndriy Gapon static uint8_t
awusbdrd_bs_r_1_noconf(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o)256029e367dSAndriy Gapon awusbdrd_bs_r_1_noconf(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
257029e367dSAndriy Gapon {
258029e367dSAndriy Gapon
259029e367dSAndriy Gapon /*
260029e367dSAndriy Gapon * There is no confdata register on some SoCs, return the same
261029e367dSAndriy Gapon * magic value as Linux.
262029e367dSAndriy Gapon */
263029e367dSAndriy Gapon if (o == MUSB2_REG_CONFDATA)
264029e367dSAndriy Gapon return (0xde);
265029e367dSAndriy Gapon
266029e367dSAndriy Gapon return (awusbdrd_bs_r_1(t, h, o));
267029e367dSAndriy Gapon }
268029e367dSAndriy Gapon
269029e367dSAndriy Gapon
270029e367dSAndriy Gapon static uint16_t
awusbdrd_bs_r_2(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o)271029e367dSAndriy Gapon awusbdrd_bs_r_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
272029e367dSAndriy Gapon {
27326d78633SMitchell Horne struct bus_space *bs = t;
274029e367dSAndriy Gapon
275d7abf6e7SAndriy Gapon if (awusbdrd_filt(o) != 0)
276d7abf6e7SAndriy Gapon return (0);
277029e367dSAndriy Gapon return bus_space_read_2(bs_parent_space(bs), h, awusbdrd_reg(o));
278029e367dSAndriy Gapon }
279029e367dSAndriy Gapon
280029e367dSAndriy Gapon static void
awusbdrd_bs_w_1(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,uint8_t v)281029e367dSAndriy Gapon awusbdrd_bs_w_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
282029e367dSAndriy Gapon uint8_t v)
283029e367dSAndriy Gapon {
28426d78633SMitchell Horne struct bus_space *bs = t;
285029e367dSAndriy Gapon
286029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0)
287029e367dSAndriy Gapon return;
288029e367dSAndriy Gapon
289029e367dSAndriy Gapon bus_space_write_1(bs_parent_space(bs), h, awusbdrd_reg(o), v);
290029e367dSAndriy Gapon }
291029e367dSAndriy Gapon
292029e367dSAndriy Gapon static void
awusbdrd_bs_w_2(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,uint16_t v)293029e367dSAndriy Gapon awusbdrd_bs_w_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
294029e367dSAndriy Gapon uint16_t v)
295029e367dSAndriy Gapon {
29626d78633SMitchell Horne struct bus_space *bs = t;
297029e367dSAndriy Gapon
298029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0)
299029e367dSAndriy Gapon return;
300029e367dSAndriy Gapon
301029e367dSAndriy Gapon bus_space_write_2(bs_parent_space(bs), h, awusbdrd_reg(o), v);
302029e367dSAndriy Gapon }
303029e367dSAndriy Gapon
304029e367dSAndriy Gapon static void
awusbdrd_bs_rm_1(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,uint8_t * d,bus_size_t c)305029e367dSAndriy Gapon awusbdrd_bs_rm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
306029e367dSAndriy Gapon uint8_t *d, bus_size_t c)
307029e367dSAndriy Gapon {
30826d78633SMitchell Horne struct bus_space *bs = t;
309029e367dSAndriy Gapon
310029e367dSAndriy Gapon bus_space_read_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
311029e367dSAndriy Gapon }
312029e367dSAndriy Gapon
313029e367dSAndriy Gapon static void
awusbdrd_bs_rm_4(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,uint32_t * d,bus_size_t c)314029e367dSAndriy Gapon awusbdrd_bs_rm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
315029e367dSAndriy Gapon uint32_t *d, bus_size_t c)
316029e367dSAndriy Gapon {
31726d78633SMitchell Horne struct bus_space *bs = t;
318029e367dSAndriy Gapon
319029e367dSAndriy Gapon bus_space_read_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
320029e367dSAndriy Gapon }
321029e367dSAndriy Gapon
322029e367dSAndriy Gapon static void
awusbdrd_bs_wm_1(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,const uint8_t * d,bus_size_t c)323029e367dSAndriy Gapon awusbdrd_bs_wm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
324029e367dSAndriy Gapon const uint8_t *d, bus_size_t c)
325029e367dSAndriy Gapon {
32626d78633SMitchell Horne struct bus_space *bs = t;
327029e367dSAndriy Gapon
328029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0)
329029e367dSAndriy Gapon return;
330029e367dSAndriy Gapon
331029e367dSAndriy Gapon bus_space_write_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
332029e367dSAndriy Gapon }
333029e367dSAndriy Gapon
334029e367dSAndriy Gapon static void
awusbdrd_bs_wm_4(awusb_bs_tag t,bus_space_handle_t h,bus_size_t o,const uint32_t * d,bus_size_t c)335029e367dSAndriy Gapon awusbdrd_bs_wm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
336029e367dSAndriy Gapon const uint32_t *d, bus_size_t c)
337029e367dSAndriy Gapon {
33826d78633SMitchell Horne struct bus_space *bs = t;
339029e367dSAndriy Gapon
340029e367dSAndriy Gapon if (awusbdrd_filt(o) != 0)
341029e367dSAndriy Gapon return;
342029e367dSAndriy Gapon
343029e367dSAndriy Gapon bus_space_write_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
344029e367dSAndriy Gapon }
345029e367dSAndriy Gapon
346029e367dSAndriy Gapon static void
awusbdrd_intr(void * arg)347029e367dSAndriy Gapon awusbdrd_intr(void *arg)
348029e367dSAndriy Gapon {
349029e367dSAndriy Gapon struct awusbdrd_softc *sc = arg;
350029e367dSAndriy Gapon uint8_t intusb;
351029e367dSAndriy Gapon uint16_t inttx, intrx;
352029e367dSAndriy Gapon
353029e367dSAndriy Gapon intusb = MUSB2_READ_1(&sc->sc, MUSB2_REG_INTUSB);
354029e367dSAndriy Gapon inttx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTTX);
355029e367dSAndriy Gapon intrx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTRX);
356029e367dSAndriy Gapon if (intusb == 0 && inttx == 0 && intrx == 0)
357029e367dSAndriy Gapon return;
358029e367dSAndriy Gapon
359029e367dSAndriy Gapon if (intusb)
360029e367dSAndriy Gapon MUSB2_WRITE_1(&sc->sc, MUSB2_REG_INTUSB, intusb);
361029e367dSAndriy Gapon if (inttx)
362029e367dSAndriy Gapon MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTTX, inttx);
363029e367dSAndriy Gapon if (intrx)
364029e367dSAndriy Gapon MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTRX, intrx);
365029e367dSAndriy Gapon
366029e367dSAndriy Gapon musbotg_interrupt(arg, intrx, inttx, intusb);
367029e367dSAndriy Gapon }
368029e367dSAndriy Gapon
369029e367dSAndriy Gapon static int
awusbdrd_probe(device_t dev)370029e367dSAndriy Gapon awusbdrd_probe(device_t dev)
371029e367dSAndriy Gapon {
372029e367dSAndriy Gapon if (!ofw_bus_status_okay(dev))
373029e367dSAndriy Gapon return (ENXIO);
374029e367dSAndriy Gapon
375029e367dSAndriy Gapon if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
376029e367dSAndriy Gapon return (ENXIO);
377029e367dSAndriy Gapon
378029e367dSAndriy Gapon device_set_desc(dev, "Allwinner USB DRD");
379029e367dSAndriy Gapon return (BUS_PROBE_DEFAULT);
380029e367dSAndriy Gapon }
381029e367dSAndriy Gapon
382029e367dSAndriy Gapon static int
awusbdrd_attach(device_t dev)383029e367dSAndriy Gapon awusbdrd_attach(device_t dev)
384029e367dSAndriy Gapon {
3851668d773SAndriy Gapon char usb_mode[24];
386029e367dSAndriy Gapon struct awusbdrd_softc *sc;
3871668d773SAndriy Gapon uint8_t musb_mode;
3881668d773SAndriy Gapon int phy_mode;
389029e367dSAndriy Gapon int error;
390029e367dSAndriy Gapon
391029e367dSAndriy Gapon sc = device_get_softc(dev);
392029e367dSAndriy Gapon sc->flags = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
393029e367dSAndriy Gapon
394029e367dSAndriy Gapon error = bus_alloc_resources(dev, awusbdrd_spec, sc->res);
395029e367dSAndriy Gapon if (error != 0)
396029e367dSAndriy Gapon return (error);
397029e367dSAndriy Gapon
3981668d773SAndriy Gapon musb_mode = MUSB2_HOST_MODE; /* default */
3991668d773SAndriy Gapon phy_mode = PHY_USB_MODE_HOST;
4001668d773SAndriy Gapon if (OF_getprop(ofw_bus_get_node(dev), "dr_mode",
4011668d773SAndriy Gapon &usb_mode, sizeof(usb_mode)) > 0) {
4021668d773SAndriy Gapon usb_mode[sizeof(usb_mode) - 1] = 0;
4031668d773SAndriy Gapon if (strcasecmp(usb_mode, "host") == 0) {
4041668d773SAndriy Gapon musb_mode = MUSB2_HOST_MODE;
4051668d773SAndriy Gapon phy_mode = PHY_USB_MODE_HOST;
4061668d773SAndriy Gapon } else if (strcasecmp(usb_mode, "peripheral") == 0) {
4071668d773SAndriy Gapon musb_mode = MUSB2_DEVICE_MODE;
4081668d773SAndriy Gapon phy_mode = PHY_USB_MODE_DEVICE;
4091668d773SAndriy Gapon } else if (strcasecmp(usb_mode, "otg") == 0) {
4101668d773SAndriy Gapon /*
4111668d773SAndriy Gapon * XXX phy has PHY_USB_MODE_OTG, but MUSB does not have
4121668d773SAndriy Gapon * it. It's not clear how to propagate mode changes
4131668d773SAndriy Gapon * from phy layer (that detects them) to MUSB.
4141668d773SAndriy Gapon */
4151668d773SAndriy Gapon musb_mode = MUSB2_DEVICE_MODE;
4161668d773SAndriy Gapon phy_mode = PHY_USB_MODE_DEVICE;
4171668d773SAndriy Gapon } else {
4181668d773SAndriy Gapon device_printf(dev, "Invalid FDT dr_mode: %s\n",
4191668d773SAndriy Gapon usb_mode);
4201668d773SAndriy Gapon }
4211668d773SAndriy Gapon }
4221668d773SAndriy Gapon
423029e367dSAndriy Gapon /* AHB gate clock is required */
424029e367dSAndriy Gapon error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
425029e367dSAndriy Gapon if (error != 0)
426029e367dSAndriy Gapon goto fail;
427029e367dSAndriy Gapon
428029e367dSAndriy Gapon /* AHB reset is only present on some SoCs */
429029e367dSAndriy Gapon (void)hwreset_get_by_ofw_idx(dev, 0, 0, &sc->reset);
430029e367dSAndriy Gapon
431029e367dSAndriy Gapon /* Enable clocks */
432029e367dSAndriy Gapon error = clk_enable(sc->clk);
433029e367dSAndriy Gapon if (error != 0) {
434029e367dSAndriy Gapon device_printf(dev, "failed to enable clock: %d\n", error);
435029e367dSAndriy Gapon goto fail;
436029e367dSAndriy Gapon }
437029e367dSAndriy Gapon if (sc->reset != NULL) {
438029e367dSAndriy Gapon error = hwreset_deassert(sc->reset);
439029e367dSAndriy Gapon if (error != 0) {
440029e367dSAndriy Gapon device_printf(dev, "failed to de-assert reset: %d\n",
441029e367dSAndriy Gapon error);
442029e367dSAndriy Gapon goto fail;
443029e367dSAndriy Gapon }
444029e367dSAndriy Gapon }
445029e367dSAndriy Gapon
4461668d773SAndriy Gapon /* XXX not sure if this is universally needed. */
4471668d773SAndriy Gapon (void)phy_get_by_ofw_name(dev, 0, "usb", &sc->phy);
4481668d773SAndriy Gapon if (sc->phy != NULL) {
4491668d773SAndriy Gapon device_printf(dev, "setting phy mode %d\n", phy_mode);
4501668d773SAndriy Gapon if (musb_mode == MUSB2_HOST_MODE) {
4511668d773SAndriy Gapon error = phy_enable(sc->phy);
4521668d773SAndriy Gapon if (error != 0) {
4531668d773SAndriy Gapon device_printf(dev, "Could not enable phy\n");
4541668d773SAndriy Gapon goto fail;
4551668d773SAndriy Gapon }
4561668d773SAndriy Gapon }
4571668d773SAndriy Gapon error = phy_usb_set_mode(sc->phy, phy_mode);
4581668d773SAndriy Gapon if (error != 0) {
4591668d773SAndriy Gapon device_printf(dev, "Could not set phy mode\n");
4601668d773SAndriy Gapon goto fail;
4611668d773SAndriy Gapon }
4621668d773SAndriy Gapon }
4631668d773SAndriy Gapon
464029e367dSAndriy Gapon sc->sc.sc_bus.parent = dev;
465029e367dSAndriy Gapon sc->sc.sc_bus.devices = sc->sc.sc_devices;
466029e367dSAndriy Gapon sc->sc.sc_bus.devices_max = MUSB2_MAX_DEVICES;
467029e367dSAndriy Gapon sc->sc.sc_bus.dma_bits = 32;
468029e367dSAndriy Gapon
469029e367dSAndriy Gapon error = usb_bus_mem_alloc_all(&sc->sc.sc_bus, USB_GET_DMA_TAG(dev),
470029e367dSAndriy Gapon NULL);
471029e367dSAndriy Gapon if (error != 0) {
472029e367dSAndriy Gapon error = ENOMEM;
473029e367dSAndriy Gapon goto fail;
474029e367dSAndriy Gapon }
475029e367dSAndriy Gapon
476029e367dSAndriy Gapon #if defined(__arm__)
477029e367dSAndriy Gapon sc->bs.bs_parent = rman_get_bustag(sc->res[0]);
478*9eb30ef4SMitchell Horne #elif defined(__aarch64__) || defined(__riscv)
479029e367dSAndriy Gapon sc->bs.bs_cookie = rman_get_bustag(sc->res[0]);
480029e367dSAndriy Gapon #endif
481029e367dSAndriy Gapon
482029e367dSAndriy Gapon if ((sc->flags & AWUSB_NO_CONFDATA) == AWUSB_NO_CONFDATA)
483029e367dSAndriy Gapon sc->bs.bs_r_1 = awusbdrd_bs_r_1_noconf;
484029e367dSAndriy Gapon else
485029e367dSAndriy Gapon sc->bs.bs_r_1 = awusbdrd_bs_r_1;
486029e367dSAndriy Gapon sc->bs.bs_r_2 = awusbdrd_bs_r_2;
487029e367dSAndriy Gapon sc->bs.bs_w_1 = awusbdrd_bs_w_1;
488029e367dSAndriy Gapon sc->bs.bs_w_2 = awusbdrd_bs_w_2;
489029e367dSAndriy Gapon sc->bs.bs_rm_1 = awusbdrd_bs_rm_1;
490029e367dSAndriy Gapon sc->bs.bs_rm_4 = awusbdrd_bs_rm_4;
491029e367dSAndriy Gapon sc->bs.bs_wm_1 = awusbdrd_bs_wm_1;
492029e367dSAndriy Gapon sc->bs.bs_wm_4 = awusbdrd_bs_wm_4;
493029e367dSAndriy Gapon
494029e367dSAndriy Gapon sc->sc.sc_io_tag = &sc->bs;
495029e367dSAndriy Gapon sc->sc.sc_io_hdl = rman_get_bushandle(sc->res[0]);
496029e367dSAndriy Gapon sc->sc.sc_io_size = rman_get_size(sc->res[0]);
497029e367dSAndriy Gapon
4985b56413dSWarner Losh sc->sc.sc_bus.bdev = device_add_child(dev, "usbus", DEVICE_UNIT_ANY);
499029e367dSAndriy Gapon if (sc->sc.sc_bus.bdev == NULL) {
500029e367dSAndriy Gapon error = ENXIO;
501029e367dSAndriy Gapon goto fail;
502029e367dSAndriy Gapon }
503029e367dSAndriy Gapon device_set_ivars(sc->sc.sc_bus.bdev, &sc->sc.sc_bus);
504029e367dSAndriy Gapon sc->sc.sc_id = 0;
505029e367dSAndriy Gapon sc->sc.sc_platform_data = sc;
5061668d773SAndriy Gapon sc->sc.sc_mode = musb_mode;
507f2066952SAndriy Gapon if (ofw_bus_is_compatible(dev, "allwinner,sun8i-h3-musb")) {
508f2066952SAndriy Gapon sc->sc.sc_ep_cfg = musbotg_ep_allwinner_h3;
509f2066952SAndriy Gapon sc->sc.sc_ep_max = DRD_EP_MAX_H3;
510f2066952SAndriy Gapon } else {
511029e367dSAndriy Gapon sc->sc.sc_ep_cfg = musbotg_ep_allwinner;
512f2066952SAndriy Gapon sc->sc.sc_ep_max = DRD_EP_MAX;
513f2066952SAndriy Gapon }
514029e367dSAndriy Gapon
515029e367dSAndriy Gapon error = bus_setup_intr(dev, sc->res[1], INTR_MPSAFE | INTR_TYPE_BIO,
516029e367dSAndriy Gapon NULL, awusbdrd_intr, sc, &sc->sc.sc_intr_hdl);
517029e367dSAndriy Gapon if (error != 0)
518029e367dSAndriy Gapon goto fail;
519029e367dSAndriy Gapon
520029e367dSAndriy Gapon /* Enable PIO mode */
521029e367dSAndriy Gapon bus_write_1(sc->res[0], MUSB2_REG_AWIN_VEND0, VEND0_PIO_MODE);
522029e367dSAndriy Gapon
523029e367dSAndriy Gapon #ifdef __arm__
524029e367dSAndriy Gapon /* Map SRAMD area to USB0 (sun4i/sun7i only) */
525029e367dSAndriy Gapon switch (allwinner_soc_family()) {
526029e367dSAndriy Gapon case ALLWINNERSOC_SUN4I:
527029e367dSAndriy Gapon case ALLWINNERSOC_SUN7I:
528029e367dSAndriy Gapon a10_map_to_otg();
529029e367dSAndriy Gapon break;
530029e367dSAndriy Gapon }
531029e367dSAndriy Gapon #endif
532029e367dSAndriy Gapon
533029e367dSAndriy Gapon error = musbotg_init(&sc->sc);
534029e367dSAndriy Gapon if (error != 0)
535029e367dSAndriy Gapon goto fail;
536029e367dSAndriy Gapon
537029e367dSAndriy Gapon error = device_probe_and_attach(sc->sc.sc_bus.bdev);
538029e367dSAndriy Gapon if (error != 0)
539029e367dSAndriy Gapon goto fail;
540029e367dSAndriy Gapon
541029e367dSAndriy Gapon musbotg_vbus_interrupt(&sc->sc, 1); /* XXX VBUS */
542029e367dSAndriy Gapon
543029e367dSAndriy Gapon return (0);
544029e367dSAndriy Gapon
545029e367dSAndriy Gapon fail:
5461668d773SAndriy Gapon if (sc->phy != NULL) {
5471668d773SAndriy Gapon if (musb_mode == MUSB2_HOST_MODE)
5481668d773SAndriy Gapon (void)phy_disable(sc->phy);
5491668d773SAndriy Gapon phy_release(sc->phy);
5501668d773SAndriy Gapon }
5511668d773SAndriy Gapon if (sc->reset != NULL) {
5521668d773SAndriy Gapon hwreset_assert(sc->reset);
553029e367dSAndriy Gapon hwreset_release(sc->reset);
5541668d773SAndriy Gapon }
555029e367dSAndriy Gapon if (sc->clk != NULL)
556029e367dSAndriy Gapon clk_release(sc->clk);
557029e367dSAndriy Gapon bus_release_resources(dev, awusbdrd_spec, sc->res);
558029e367dSAndriy Gapon return (error);
559029e367dSAndriy Gapon }
560029e367dSAndriy Gapon
561029e367dSAndriy Gapon static int
awusbdrd_detach(device_t dev)562029e367dSAndriy Gapon awusbdrd_detach(device_t dev)
563029e367dSAndriy Gapon {
564029e367dSAndriy Gapon struct awusbdrd_softc *sc;
565029e367dSAndriy Gapon int error;
566029e367dSAndriy Gapon
5673ddaf820SJohn Baldwin error = bus_generic_detach(dev);
5683ddaf820SJohn Baldwin if (error != 0)
5693ddaf820SJohn Baldwin return (error);
570029e367dSAndriy Gapon
5713ddaf820SJohn Baldwin sc = device_get_softc(dev);
572029e367dSAndriy Gapon
573029e367dSAndriy Gapon musbotg_uninit(&sc->sc);
574029e367dSAndriy Gapon error = bus_teardown_intr(dev, sc->res[1], sc->sc.sc_intr_hdl);
575029e367dSAndriy Gapon if (error != 0)
576029e367dSAndriy Gapon return (error);
577029e367dSAndriy Gapon
578029e367dSAndriy Gapon usb_bus_mem_free_all(&sc->sc.sc_bus, NULL);
579029e367dSAndriy Gapon
5801668d773SAndriy Gapon if (sc->phy != NULL) {
5811668d773SAndriy Gapon if (sc->sc.sc_mode == MUSB2_HOST_MODE)
5821668d773SAndriy Gapon phy_disable(sc->phy);
5831668d773SAndriy Gapon phy_release(sc->phy);
5841668d773SAndriy Gapon }
5851668d773SAndriy Gapon if (sc->reset != NULL) {
5861668d773SAndriy Gapon if (hwreset_assert(sc->reset) != 0)
5871668d773SAndriy Gapon device_printf(dev, "failed to assert reset\n");
588029e367dSAndriy Gapon hwreset_release(sc->reset);
5891668d773SAndriy Gapon }
590029e367dSAndriy Gapon if (sc->clk != NULL)
591029e367dSAndriy Gapon clk_release(sc->clk);
592029e367dSAndriy Gapon
593029e367dSAndriy Gapon bus_release_resources(dev, awusbdrd_spec, sc->res);
594029e367dSAndriy Gapon
595029e367dSAndriy Gapon return (0);
596029e367dSAndriy Gapon }
597029e367dSAndriy Gapon
598029e367dSAndriy Gapon static device_method_t awusbdrd_methods[] = {
599029e367dSAndriy Gapon /* Device interface */
600029e367dSAndriy Gapon DEVMETHOD(device_probe, awusbdrd_probe),
601029e367dSAndriy Gapon DEVMETHOD(device_attach, awusbdrd_attach),
602029e367dSAndriy Gapon DEVMETHOD(device_detach, awusbdrd_detach),
603029e367dSAndriy Gapon DEVMETHOD(device_suspend, bus_generic_suspend),
604029e367dSAndriy Gapon DEVMETHOD(device_resume, bus_generic_resume),
605029e367dSAndriy Gapon DEVMETHOD(device_shutdown, bus_generic_shutdown),
606029e367dSAndriy Gapon
607029e367dSAndriy Gapon DEVMETHOD_END
608029e367dSAndriy Gapon };
609029e367dSAndriy Gapon
610029e367dSAndriy Gapon static driver_t awusbdrd_driver = {
611029e367dSAndriy Gapon .name = "musbotg",
612029e367dSAndriy Gapon .methods = awusbdrd_methods,
613029e367dSAndriy Gapon .size = sizeof(struct awusbdrd_softc),
614029e367dSAndriy Gapon };
615029e367dSAndriy Gapon
616bc9372d7SJohn Baldwin DRIVER_MODULE(musbotg, simplebus, awusbdrd_driver, 0, 0);
617029e367dSAndriy Gapon MODULE_DEPEND(musbotg, usb, 1, 1, 1);
618