xref: /freebsd/sys/dev/usb/controller/musb_otg_allwinner.c (revision 950a6087ec18cd22464b3297573f54a6d9223c99)
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>
63*950a6087SEmmanuel Vadot #include <dev/phy/phy.h>
64*950a6087SEmmanuel 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;
80029e367dSAndriy Gapon #elif defined(__aarch64__)
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 },
92029e367dSAndriy Gapon 	{ NULL,				0 }
93029e367dSAndriy Gapon };
94029e367dSAndriy Gapon 
95029e367dSAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner[] = {
96029e367dSAndriy Gapon 	{
97f2066952SAndriy Gapon 		.ep_end = DRD_EP_MAX,
98f2066952SAndriy Gapon 		.ep_fifosz_shift = 9,
99f2066952SAndriy Gapon 		.ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512,
100f2066952SAndriy Gapon 	},
101f2066952SAndriy Gapon 	{
102f2066952SAndriy Gapon 		.ep_end = -1,
103f2066952SAndriy Gapon 	},
104f2066952SAndriy Gapon };
105f2066952SAndriy Gapon 
106f2066952SAndriy Gapon static const struct musb_otg_ep_cfg musbotg_ep_allwinner_h3[] = {
107f2066952SAndriy Gapon 	{
108f2066952SAndriy Gapon 		.ep_end = DRD_EP_MAX_H3,
109029e367dSAndriy Gapon 		.ep_fifosz_shift = 9,
110029e367dSAndriy Gapon 		.ep_fifosz_reg = MUSB2_VAL_FIFOSZ_512,
111029e367dSAndriy Gapon 	},
112029e367dSAndriy Gapon 	{
113029e367dSAndriy Gapon 		.ep_end = -1,
114029e367dSAndriy Gapon 	},
115029e367dSAndriy Gapon };
116029e367dSAndriy Gapon 
117029e367dSAndriy Gapon struct awusbdrd_softc {
118029e367dSAndriy Gapon 	struct musbotg_softc	sc;
119029e367dSAndriy Gapon 	struct resource		*res[2];
120029e367dSAndriy Gapon 	clk_t			clk;
121029e367dSAndriy Gapon 	hwreset_t		reset;
1221668d773SAndriy Gapon 	phy_t			phy;
123029e367dSAndriy Gapon 	struct bus_space	bs;
124029e367dSAndriy Gapon 	int			flags;
125029e367dSAndriy Gapon };
126029e367dSAndriy Gapon 
127029e367dSAndriy Gapon static struct resource_spec awusbdrd_spec[] = {
128029e367dSAndriy Gapon 	{ SYS_RES_MEMORY,	0,	RF_ACTIVE },
129029e367dSAndriy Gapon 	{ SYS_RES_IRQ,		0,	RF_ACTIVE },
130029e367dSAndriy Gapon 	{ -1, 0 }
131029e367dSAndriy Gapon };
132029e367dSAndriy Gapon 
133029e367dSAndriy Gapon #define	REMAPFLAG	0x8000
134029e367dSAndriy Gapon #define	REGDECL(a, b)	[(a)] = ((b) | REMAPFLAG)
135029e367dSAndriy Gapon 
136029e367dSAndriy Gapon /* Allwinner USB DRD register mappings */
137029e367dSAndriy Gapon static const uint16_t awusbdrd_regmap[] = {
138029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(0),	0x0000),
139029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(1),	0x0004),
140029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(2),	0x0008),
141029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(3),	0x000c),
142029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(4),	0x0010),
143029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPFIFO(5),	0x0014),
144029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_POWER,	0x0040),
145029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_DEVCTL,	0x0041),
146029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_EPINDEX,	0x0042),
147029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTTX,	0x0044),
148029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTRX,	0x0046),
149029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTTXE,	0x0048),
150029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTRXE,	0x004a),
151029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTUSB,	0x004c),
152029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_INTUSBE,	0x0050),
153029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_FRAME,	0x0054),
154029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TESTMODE,	0x007c),
155029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXMAXP,	0x0080),
156029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXCSRL,	0x0082),
157029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXCSRH,	0x0083),
158029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXMAXP,	0x0084),
159029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXCSRL,	0x0086),
160029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXCSRH,	0x0087),
161029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXCOUNT,	0x0088),
162029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXTI,		0x008c),
163029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXNAKLIMIT,	0x008d),
164029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXNAKLIMIT,	0x008f),
165029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXTI,		0x008e),
166029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFIFOSZ,	0x0090),
167029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFIFOADD,	0x0092),
168029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFIFOSZ,	0x0094),
169029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFIFOADD,	0x0096),
170029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_FADDR,	0x0098),
171029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(0),	0x0098),
172029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(0),	0x009a),
173029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(0),	0x009b),
174029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(0),	0x009c),
175029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(0),	0x009e),
176029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(0),	0x009f),
177029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(1),	0x0098),
178029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(1),	0x009a),
179029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(1),	0x009b),
180029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(1),	0x009c),
181029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(1),	0x009e),
182029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(1),	0x009f),
183029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(2),	0x0098),
184029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(2),	0x009a),
185029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(2),	0x009b),
186029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(2),	0x009c),
187029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(2),	0x009e),
188029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(2),	0x009f),
189029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(3),	0x0098),
190029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(3),	0x009a),
191029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(3),	0x009b),
192029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(3),	0x009c),
193029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(3),	0x009e),
194029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(3),	0x009f),
195029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(4),	0x0098),
196029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(4),	0x009a),
197029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(4),	0x009b),
198029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(4),	0x009c),
199029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(4),	0x009e),
200029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(4),	0x009f),
201029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXFADDR(5),	0x0098),
202029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHADDR(5),	0x009a),
203029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_TXHUBPORT(5),	0x009b),
204029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXFADDR(5),	0x009c),
205029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHADDR(5),	0x009e),
206029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_RXHUBPORT(5),	0x009f),
207029e367dSAndriy Gapon 	REGDECL(MUSB2_REG_CONFDATA,	0x00c0),
208029e367dSAndriy Gapon };
209029e367dSAndriy Gapon 
210029e367dSAndriy Gapon static bus_size_t
211029e367dSAndriy Gapon awusbdrd_reg(bus_size_t o)
212029e367dSAndriy Gapon {
213029e367dSAndriy Gapon 	bus_size_t v;
214029e367dSAndriy Gapon 
215029e367dSAndriy Gapon 	KASSERT(o < nitems(awusbdrd_regmap),
216029e367dSAndriy Gapon 	    ("%s: Invalid register %#lx", __func__, o));
217029e367dSAndriy Gapon 	if (o >= nitems(awusbdrd_regmap))
218029e367dSAndriy Gapon 		return (o);
219029e367dSAndriy Gapon 
220029e367dSAndriy Gapon 	v = awusbdrd_regmap[o];
221029e367dSAndriy Gapon 
222029e367dSAndriy Gapon 	KASSERT((v & REMAPFLAG) != 0, ("%s: reg %#lx not in regmap",
223029e367dSAndriy Gapon 	    __func__, o));
224029e367dSAndriy Gapon 
225029e367dSAndriy Gapon 	return (v & ~REMAPFLAG);
226029e367dSAndriy Gapon }
227029e367dSAndriy Gapon 
228029e367dSAndriy Gapon static int
229029e367dSAndriy Gapon awusbdrd_filt(bus_size_t o)
230029e367dSAndriy Gapon {
231029e367dSAndriy Gapon 	switch (o) {
232029e367dSAndriy Gapon 	case MUSB2_REG_MISC:
233029e367dSAndriy Gapon 	case MUSB2_REG_RXDBDIS:
234029e367dSAndriy Gapon 	case MUSB2_REG_TXDBDIS:
235029e367dSAndriy Gapon 		return (1);
236029e367dSAndriy Gapon 	default:
237029e367dSAndriy Gapon 		return (0);
238029e367dSAndriy Gapon 	}
239029e367dSAndriy Gapon }
240029e367dSAndriy Gapon 
241029e367dSAndriy Gapon static uint8_t
242029e367dSAndriy Gapon awusbdrd_bs_r_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
243029e367dSAndriy Gapon {
24426d78633SMitchell Horne 	struct bus_space *bs = t;
245029e367dSAndriy Gapon 
246029e367dSAndriy Gapon 	switch (o) {
247029e367dSAndriy Gapon 	case MUSB2_REG_HWVERS:
248029e367dSAndriy Gapon 		return (0);	/* no known equivalent */
249029e367dSAndriy Gapon 	}
250029e367dSAndriy Gapon 
251029e367dSAndriy Gapon 	return (bus_space_read_1(bs_parent_space(bs), h, awusbdrd_reg(o)));
252029e367dSAndriy Gapon }
253029e367dSAndriy Gapon 
254029e367dSAndriy Gapon static uint8_t
255029e367dSAndriy Gapon awusbdrd_bs_r_1_noconf(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
256029e367dSAndriy Gapon {
257029e367dSAndriy Gapon 
258029e367dSAndriy Gapon 	/*
259029e367dSAndriy Gapon 	 * There is no confdata register on some SoCs, return the same
260029e367dSAndriy Gapon 	 * magic value as Linux.
261029e367dSAndriy Gapon 	 */
262029e367dSAndriy Gapon 	if (o == MUSB2_REG_CONFDATA)
263029e367dSAndriy Gapon 		return (0xde);
264029e367dSAndriy Gapon 
265029e367dSAndriy Gapon 	return (awusbdrd_bs_r_1(t, h, o));
266029e367dSAndriy Gapon }
267029e367dSAndriy Gapon 
268029e367dSAndriy Gapon 
269029e367dSAndriy Gapon static uint16_t
270029e367dSAndriy Gapon awusbdrd_bs_r_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o)
271029e367dSAndriy Gapon {
27226d78633SMitchell Horne 	struct bus_space *bs = t;
273029e367dSAndriy Gapon 
274d7abf6e7SAndriy Gapon 	if (awusbdrd_filt(o) != 0)
275d7abf6e7SAndriy Gapon 		return (0);
276029e367dSAndriy Gapon 	return bus_space_read_2(bs_parent_space(bs), h, awusbdrd_reg(o));
277029e367dSAndriy Gapon }
278029e367dSAndriy Gapon 
279029e367dSAndriy Gapon static void
280029e367dSAndriy Gapon awusbdrd_bs_w_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
281029e367dSAndriy Gapon     uint8_t v)
282029e367dSAndriy Gapon {
28326d78633SMitchell Horne 	struct bus_space *bs = t;
284029e367dSAndriy Gapon 
285029e367dSAndriy Gapon 	if (awusbdrd_filt(o) != 0)
286029e367dSAndriy Gapon 		return;
287029e367dSAndriy Gapon 
288029e367dSAndriy Gapon 	bus_space_write_1(bs_parent_space(bs), h, awusbdrd_reg(o), v);
289029e367dSAndriy Gapon }
290029e367dSAndriy Gapon 
291029e367dSAndriy Gapon static void
292029e367dSAndriy Gapon awusbdrd_bs_w_2(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
293029e367dSAndriy Gapon     uint16_t v)
294029e367dSAndriy Gapon {
29526d78633SMitchell Horne 	struct bus_space *bs = t;
296029e367dSAndriy Gapon 
297029e367dSAndriy Gapon 	if (awusbdrd_filt(o) != 0)
298029e367dSAndriy Gapon 		return;
299029e367dSAndriy Gapon 
300029e367dSAndriy Gapon 	bus_space_write_2(bs_parent_space(bs), h, awusbdrd_reg(o), v);
301029e367dSAndriy Gapon }
302029e367dSAndriy Gapon 
303029e367dSAndriy Gapon static void
304029e367dSAndriy Gapon awusbdrd_bs_rm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
305029e367dSAndriy Gapon     uint8_t *d, bus_size_t c)
306029e367dSAndriy Gapon {
30726d78633SMitchell Horne 	struct bus_space *bs = t;
308029e367dSAndriy Gapon 
309029e367dSAndriy Gapon 	bus_space_read_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
310029e367dSAndriy Gapon }
311029e367dSAndriy Gapon 
312029e367dSAndriy Gapon static void
313029e367dSAndriy Gapon awusbdrd_bs_rm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
314029e367dSAndriy Gapon     uint32_t *d, bus_size_t c)
315029e367dSAndriy Gapon {
31626d78633SMitchell Horne 	struct bus_space *bs = t;
317029e367dSAndriy Gapon 
318029e367dSAndriy Gapon 	bus_space_read_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
319029e367dSAndriy Gapon }
320029e367dSAndriy Gapon 
321029e367dSAndriy Gapon static void
322029e367dSAndriy Gapon awusbdrd_bs_wm_1(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
323029e367dSAndriy Gapon     const uint8_t *d, bus_size_t c)
324029e367dSAndriy Gapon {
32526d78633SMitchell Horne 	struct bus_space *bs = t;
326029e367dSAndriy Gapon 
327029e367dSAndriy Gapon 	if (awusbdrd_filt(o) != 0)
328029e367dSAndriy Gapon 		return;
329029e367dSAndriy Gapon 
330029e367dSAndriy Gapon 	bus_space_write_multi_1(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
331029e367dSAndriy Gapon }
332029e367dSAndriy Gapon 
333029e367dSAndriy Gapon static void
334029e367dSAndriy Gapon awusbdrd_bs_wm_4(awusb_bs_tag t, bus_space_handle_t h, bus_size_t o,
335029e367dSAndriy Gapon     const uint32_t *d, bus_size_t c)
336029e367dSAndriy Gapon {
33726d78633SMitchell Horne 	struct bus_space *bs = t;
338029e367dSAndriy Gapon 
339029e367dSAndriy Gapon 	if (awusbdrd_filt(o) != 0)
340029e367dSAndriy Gapon 		return;
341029e367dSAndriy Gapon 
342029e367dSAndriy Gapon 	bus_space_write_multi_4(bs_parent_space(bs), h, awusbdrd_reg(o), d, c);
343029e367dSAndriy Gapon }
344029e367dSAndriy Gapon 
345029e367dSAndriy Gapon static void
346029e367dSAndriy Gapon awusbdrd_intr(void *arg)
347029e367dSAndriy Gapon {
348029e367dSAndriy Gapon 	struct awusbdrd_softc *sc = arg;
349029e367dSAndriy Gapon 	uint8_t intusb;
350029e367dSAndriy Gapon 	uint16_t inttx, intrx;
351029e367dSAndriy Gapon 
352029e367dSAndriy Gapon 	intusb = MUSB2_READ_1(&sc->sc, MUSB2_REG_INTUSB);
353029e367dSAndriy Gapon 	inttx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTTX);
354029e367dSAndriy Gapon 	intrx = MUSB2_READ_2(&sc->sc, MUSB2_REG_INTRX);
355029e367dSAndriy Gapon 	if (intusb == 0 && inttx == 0 && intrx == 0)
356029e367dSAndriy Gapon 		return;
357029e367dSAndriy Gapon 
358029e367dSAndriy Gapon 	if (intusb)
359029e367dSAndriy Gapon 		MUSB2_WRITE_1(&sc->sc, MUSB2_REG_INTUSB, intusb);
360029e367dSAndriy Gapon 	if (inttx)
361029e367dSAndriy Gapon 		MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTTX, inttx);
362029e367dSAndriy Gapon 	if (intrx)
363029e367dSAndriy Gapon 		MUSB2_WRITE_2(&sc->sc, MUSB2_REG_INTRX, intrx);
364029e367dSAndriy Gapon 
365029e367dSAndriy Gapon 	musbotg_interrupt(arg, intrx, inttx, intusb);
366029e367dSAndriy Gapon }
367029e367dSAndriy Gapon 
368029e367dSAndriy Gapon static int
369029e367dSAndriy Gapon awusbdrd_probe(device_t dev)
370029e367dSAndriy Gapon {
371029e367dSAndriy Gapon 	if (!ofw_bus_status_okay(dev))
372029e367dSAndriy Gapon 		return (ENXIO);
373029e367dSAndriy Gapon 
374029e367dSAndriy Gapon 	if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)
375029e367dSAndriy Gapon 		return (ENXIO);
376029e367dSAndriy Gapon 
377029e367dSAndriy Gapon 	device_set_desc(dev, "Allwinner USB DRD");
378029e367dSAndriy Gapon 	return (BUS_PROBE_DEFAULT);
379029e367dSAndriy Gapon }
380029e367dSAndriy Gapon 
381029e367dSAndriy Gapon static int
382029e367dSAndriy Gapon awusbdrd_attach(device_t dev)
383029e367dSAndriy Gapon {
3841668d773SAndriy Gapon 	char usb_mode[24];
385029e367dSAndriy Gapon 	struct awusbdrd_softc *sc;
3861668d773SAndriy Gapon 	uint8_t musb_mode;
3871668d773SAndriy Gapon 	int phy_mode;
388029e367dSAndriy Gapon 	int error;
389029e367dSAndriy Gapon 
390029e367dSAndriy Gapon 	sc = device_get_softc(dev);
391029e367dSAndriy Gapon 	sc->flags = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
392029e367dSAndriy Gapon 
393029e367dSAndriy Gapon 	error = bus_alloc_resources(dev, awusbdrd_spec, sc->res);
394029e367dSAndriy Gapon 	if (error != 0)
395029e367dSAndriy Gapon 		return (error);
396029e367dSAndriy Gapon 
3971668d773SAndriy Gapon 	musb_mode = MUSB2_HOST_MODE;	/* default */
3981668d773SAndriy Gapon 	phy_mode = PHY_USB_MODE_HOST;
3991668d773SAndriy Gapon 	if (OF_getprop(ofw_bus_get_node(dev), "dr_mode",
4001668d773SAndriy Gapon 	    &usb_mode, sizeof(usb_mode)) > 0) {
4011668d773SAndriy Gapon 		usb_mode[sizeof(usb_mode) - 1] = 0;
4021668d773SAndriy Gapon 		if (strcasecmp(usb_mode, "host") == 0) {
4031668d773SAndriy Gapon 			musb_mode = MUSB2_HOST_MODE;
4041668d773SAndriy Gapon 			phy_mode = PHY_USB_MODE_HOST;
4051668d773SAndriy Gapon 		} else if (strcasecmp(usb_mode, "peripheral") == 0) {
4061668d773SAndriy Gapon 			musb_mode = MUSB2_DEVICE_MODE;
4071668d773SAndriy Gapon 			phy_mode = PHY_USB_MODE_DEVICE;
4081668d773SAndriy Gapon 		} else if (strcasecmp(usb_mode, "otg") == 0) {
4091668d773SAndriy Gapon 			/*
4101668d773SAndriy Gapon 			 * XXX phy has PHY_USB_MODE_OTG, but MUSB does not have
4111668d773SAndriy Gapon 			 * it.  It's not clear how to propagate mode changes
4121668d773SAndriy Gapon 			 * from phy layer (that detects them) to MUSB.
4131668d773SAndriy Gapon 			 */
4141668d773SAndriy Gapon 			musb_mode = MUSB2_DEVICE_MODE;
4151668d773SAndriy Gapon 			phy_mode = PHY_USB_MODE_DEVICE;
4161668d773SAndriy Gapon 		} else {
4171668d773SAndriy Gapon 			device_printf(dev, "Invalid FDT dr_mode: %s\n",
4181668d773SAndriy Gapon 			    usb_mode);
4191668d773SAndriy Gapon 		}
4201668d773SAndriy Gapon 	}
4211668d773SAndriy Gapon 
422029e367dSAndriy Gapon 	/* AHB gate clock is required */
423029e367dSAndriy Gapon 	error = clk_get_by_ofw_index(dev, 0, 0, &sc->clk);
424029e367dSAndriy Gapon 	if (error != 0)
425029e367dSAndriy Gapon 		goto fail;
426029e367dSAndriy Gapon 
427029e367dSAndriy Gapon 	/* AHB reset is only present on some SoCs */
428029e367dSAndriy Gapon 	(void)hwreset_get_by_ofw_idx(dev, 0, 0, &sc->reset);
429029e367dSAndriy Gapon 
430029e367dSAndriy Gapon 	/* Enable clocks */
431029e367dSAndriy Gapon 	error = clk_enable(sc->clk);
432029e367dSAndriy Gapon 	if (error != 0) {
433029e367dSAndriy Gapon 		device_printf(dev, "failed to enable clock: %d\n", error);
434029e367dSAndriy Gapon 		goto fail;
435029e367dSAndriy Gapon 	}
436029e367dSAndriy Gapon 	if (sc->reset != NULL) {
437029e367dSAndriy Gapon 		error = hwreset_deassert(sc->reset);
438029e367dSAndriy Gapon 		if (error != 0) {
439029e367dSAndriy Gapon 			device_printf(dev, "failed to de-assert reset: %d\n",
440029e367dSAndriy Gapon 			    error);
441029e367dSAndriy Gapon 			goto fail;
442029e367dSAndriy Gapon 		}
443029e367dSAndriy Gapon 	}
444029e367dSAndriy Gapon 
4451668d773SAndriy Gapon 	/* XXX not sure if this is universally needed. */
4461668d773SAndriy Gapon 	(void)phy_get_by_ofw_name(dev, 0, "usb", &sc->phy);
4471668d773SAndriy Gapon 	if (sc->phy != NULL) {
4481668d773SAndriy Gapon 		device_printf(dev, "setting phy mode %d\n", phy_mode);
4491668d773SAndriy Gapon 		if (musb_mode == MUSB2_HOST_MODE) {
4501668d773SAndriy Gapon 			error = phy_enable(sc->phy);
4511668d773SAndriy Gapon 			if (error != 0) {
4521668d773SAndriy Gapon 				device_printf(dev, "Could not enable phy\n");
4531668d773SAndriy Gapon 				goto fail;
4541668d773SAndriy Gapon 			}
4551668d773SAndriy Gapon 		}
4561668d773SAndriy Gapon 		error = phy_usb_set_mode(sc->phy, phy_mode);
4571668d773SAndriy Gapon 		if (error != 0) {
4581668d773SAndriy Gapon 			device_printf(dev, "Could not set phy mode\n");
4591668d773SAndriy Gapon 			goto fail;
4601668d773SAndriy Gapon 		}
4611668d773SAndriy Gapon 	}
4621668d773SAndriy Gapon 
463029e367dSAndriy Gapon 	sc->sc.sc_bus.parent = dev;
464029e367dSAndriy Gapon 	sc->sc.sc_bus.devices = sc->sc.sc_devices;
465029e367dSAndriy Gapon 	sc->sc.sc_bus.devices_max = MUSB2_MAX_DEVICES;
466029e367dSAndriy Gapon 	sc->sc.sc_bus.dma_bits = 32;
467029e367dSAndriy Gapon 
468029e367dSAndriy Gapon 	error = usb_bus_mem_alloc_all(&sc->sc.sc_bus, USB_GET_DMA_TAG(dev),
469029e367dSAndriy Gapon 	    NULL);
470029e367dSAndriy Gapon 	if (error != 0) {
471029e367dSAndriy Gapon 		error = ENOMEM;
472029e367dSAndriy Gapon 		goto fail;
473029e367dSAndriy Gapon 	}
474029e367dSAndriy Gapon 
475029e367dSAndriy Gapon #if defined(__arm__)
476029e367dSAndriy Gapon 	sc->bs.bs_parent = rman_get_bustag(sc->res[0]);
477029e367dSAndriy Gapon #elif defined(__aarch64__)
478029e367dSAndriy Gapon 	sc->bs.bs_cookie = rman_get_bustag(sc->res[0]);
479029e367dSAndriy Gapon #endif
480029e367dSAndriy Gapon 
481029e367dSAndriy Gapon 	if ((sc->flags & AWUSB_NO_CONFDATA) == AWUSB_NO_CONFDATA)
482029e367dSAndriy Gapon 		sc->bs.bs_r_1 = awusbdrd_bs_r_1_noconf;
483029e367dSAndriy Gapon 	else
484029e367dSAndriy Gapon 		sc->bs.bs_r_1 = awusbdrd_bs_r_1;
485029e367dSAndriy Gapon 	sc->bs.bs_r_2 = awusbdrd_bs_r_2;
486029e367dSAndriy Gapon 	sc->bs.bs_w_1 = awusbdrd_bs_w_1;
487029e367dSAndriy Gapon 	sc->bs.bs_w_2 = awusbdrd_bs_w_2;
488029e367dSAndriy Gapon 	sc->bs.bs_rm_1 = awusbdrd_bs_rm_1;
489029e367dSAndriy Gapon 	sc->bs.bs_rm_4 = awusbdrd_bs_rm_4;
490029e367dSAndriy Gapon 	sc->bs.bs_wm_1 = awusbdrd_bs_wm_1;
491029e367dSAndriy Gapon 	sc->bs.bs_wm_4 = awusbdrd_bs_wm_4;
492029e367dSAndriy Gapon 
493029e367dSAndriy Gapon 	sc->sc.sc_io_tag = &sc->bs;
494029e367dSAndriy Gapon 	sc->sc.sc_io_hdl = rman_get_bushandle(sc->res[0]);
495029e367dSAndriy Gapon 	sc->sc.sc_io_size = rman_get_size(sc->res[0]);
496029e367dSAndriy Gapon 
497029e367dSAndriy Gapon 	sc->sc.sc_bus.bdev = device_add_child(dev, "usbus", -1);
498029e367dSAndriy Gapon 	if (sc->sc.sc_bus.bdev == NULL) {
499029e367dSAndriy Gapon 		error = ENXIO;
500029e367dSAndriy Gapon 		goto fail;
501029e367dSAndriy Gapon 	}
502029e367dSAndriy Gapon 	device_set_ivars(sc->sc.sc_bus.bdev, &sc->sc.sc_bus);
503029e367dSAndriy Gapon 	sc->sc.sc_id = 0;
504029e367dSAndriy Gapon 	sc->sc.sc_platform_data = sc;
5051668d773SAndriy Gapon 	sc->sc.sc_mode = musb_mode;
506f2066952SAndriy Gapon 	if (ofw_bus_is_compatible(dev, "allwinner,sun8i-h3-musb")) {
507f2066952SAndriy Gapon 		sc->sc.sc_ep_cfg = musbotg_ep_allwinner_h3;
508f2066952SAndriy Gapon 		sc->sc.sc_ep_max = DRD_EP_MAX_H3;
509f2066952SAndriy Gapon 	} else {
510029e367dSAndriy Gapon 		sc->sc.sc_ep_cfg = musbotg_ep_allwinner;
511f2066952SAndriy Gapon 		sc->sc.sc_ep_max = DRD_EP_MAX;
512f2066952SAndriy Gapon 	}
513029e367dSAndriy Gapon 
514029e367dSAndriy Gapon 	error = bus_setup_intr(dev, sc->res[1], INTR_MPSAFE | INTR_TYPE_BIO,
515029e367dSAndriy Gapon 	    NULL, awusbdrd_intr, sc, &sc->sc.sc_intr_hdl);
516029e367dSAndriy Gapon 	if (error != 0)
517029e367dSAndriy Gapon 		goto fail;
518029e367dSAndriy Gapon 
519029e367dSAndriy Gapon 	/* Enable PIO mode */
520029e367dSAndriy Gapon 	bus_write_1(sc->res[0], MUSB2_REG_AWIN_VEND0, VEND0_PIO_MODE);
521029e367dSAndriy Gapon 
522029e367dSAndriy Gapon #ifdef __arm__
523029e367dSAndriy Gapon 	/* Map SRAMD area to USB0 (sun4i/sun7i only) */
524029e367dSAndriy Gapon 	switch (allwinner_soc_family()) {
525029e367dSAndriy Gapon 	case ALLWINNERSOC_SUN4I:
526029e367dSAndriy Gapon 	case ALLWINNERSOC_SUN7I:
527029e367dSAndriy Gapon 		a10_map_to_otg();
528029e367dSAndriy Gapon 		break;
529029e367dSAndriy Gapon 	}
530029e367dSAndriy Gapon #endif
531029e367dSAndriy Gapon 
532029e367dSAndriy Gapon 	error = musbotg_init(&sc->sc);
533029e367dSAndriy Gapon 	if (error != 0)
534029e367dSAndriy Gapon 		goto fail;
535029e367dSAndriy Gapon 
536029e367dSAndriy Gapon 	error = device_probe_and_attach(sc->sc.sc_bus.bdev);
537029e367dSAndriy Gapon 	if (error != 0)
538029e367dSAndriy Gapon 		goto fail;
539029e367dSAndriy Gapon 
540029e367dSAndriy Gapon 	musbotg_vbus_interrupt(&sc->sc, 1);	/* XXX VBUS */
541029e367dSAndriy Gapon 
542029e367dSAndriy Gapon 	return (0);
543029e367dSAndriy Gapon 
544029e367dSAndriy Gapon fail:
5451668d773SAndriy Gapon 	if (sc->phy != NULL) {
5461668d773SAndriy Gapon 		if (musb_mode == MUSB2_HOST_MODE)
5471668d773SAndriy Gapon 			(void)phy_disable(sc->phy);
5481668d773SAndriy Gapon 		phy_release(sc->phy);
5491668d773SAndriy Gapon 	}
5501668d773SAndriy Gapon 	if (sc->reset != NULL) {
5511668d773SAndriy Gapon 		hwreset_assert(sc->reset);
552029e367dSAndriy Gapon 		hwreset_release(sc->reset);
5531668d773SAndriy Gapon 	}
554029e367dSAndriy Gapon 	if (sc->clk != NULL)
555029e367dSAndriy Gapon 		clk_release(sc->clk);
556029e367dSAndriy Gapon 	bus_release_resources(dev, awusbdrd_spec, sc->res);
557029e367dSAndriy Gapon 	return (error);
558029e367dSAndriy Gapon }
559029e367dSAndriy Gapon 
560029e367dSAndriy Gapon static int
561029e367dSAndriy Gapon awusbdrd_detach(device_t dev)
562029e367dSAndriy Gapon {
563029e367dSAndriy Gapon 	struct awusbdrd_softc *sc;
564029e367dSAndriy Gapon 	device_t bdev;
565029e367dSAndriy Gapon 	int error;
566029e367dSAndriy Gapon 
567029e367dSAndriy Gapon 	sc = device_get_softc(dev);
568029e367dSAndriy Gapon 
569029e367dSAndriy Gapon 	if (sc->sc.sc_bus.bdev != NULL) {
570029e367dSAndriy Gapon 		bdev = sc->sc.sc_bus.bdev;
571029e367dSAndriy Gapon 		device_detach(bdev);
572029e367dSAndriy Gapon 		device_delete_child(dev, bdev);
573029e367dSAndriy Gapon 	}
574029e367dSAndriy Gapon 
575029e367dSAndriy Gapon 	musbotg_uninit(&sc->sc);
576029e367dSAndriy Gapon 	error = bus_teardown_intr(dev, sc->res[1], sc->sc.sc_intr_hdl);
577029e367dSAndriy Gapon 	if (error != 0)
578029e367dSAndriy Gapon 		return (error);
579029e367dSAndriy Gapon 
580029e367dSAndriy Gapon 	usb_bus_mem_free_all(&sc->sc.sc_bus, NULL);
581029e367dSAndriy Gapon 
5821668d773SAndriy Gapon 	if (sc->phy != NULL) {
5831668d773SAndriy Gapon 		if (sc->sc.sc_mode == MUSB2_HOST_MODE)
5841668d773SAndriy Gapon 			phy_disable(sc->phy);
5851668d773SAndriy Gapon 		phy_release(sc->phy);
5861668d773SAndriy Gapon 	}
5871668d773SAndriy Gapon 	if (sc->reset != NULL) {
5881668d773SAndriy Gapon 		if (hwreset_assert(sc->reset) != 0)
5891668d773SAndriy Gapon 			device_printf(dev, "failed to assert reset\n");
590029e367dSAndriy Gapon 		hwreset_release(sc->reset);
5911668d773SAndriy Gapon 	}
592029e367dSAndriy Gapon 	if (sc->clk != NULL)
593029e367dSAndriy Gapon 		clk_release(sc->clk);
594029e367dSAndriy Gapon 
595029e367dSAndriy Gapon 	bus_release_resources(dev, awusbdrd_spec, sc->res);
596029e367dSAndriy Gapon 
597029e367dSAndriy Gapon 	device_delete_children(dev);
598029e367dSAndriy Gapon 
599029e367dSAndriy Gapon 	return (0);
600029e367dSAndriy Gapon }
601029e367dSAndriy Gapon 
602029e367dSAndriy Gapon static device_method_t awusbdrd_methods[] = {
603029e367dSAndriy Gapon 	/* Device interface */
604029e367dSAndriy Gapon 	DEVMETHOD(device_probe,		awusbdrd_probe),
605029e367dSAndriy Gapon 	DEVMETHOD(device_attach,	awusbdrd_attach),
606029e367dSAndriy Gapon 	DEVMETHOD(device_detach,	awusbdrd_detach),
607029e367dSAndriy Gapon 	DEVMETHOD(device_suspend,	bus_generic_suspend),
608029e367dSAndriy Gapon 	DEVMETHOD(device_resume,	bus_generic_resume),
609029e367dSAndriy Gapon 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
610029e367dSAndriy Gapon 
611029e367dSAndriy Gapon 	DEVMETHOD_END
612029e367dSAndriy Gapon };
613029e367dSAndriy Gapon 
614029e367dSAndriy Gapon static driver_t awusbdrd_driver = {
615029e367dSAndriy Gapon 	.name = "musbotg",
616029e367dSAndriy Gapon 	.methods = awusbdrd_methods,
617029e367dSAndriy Gapon 	.size = sizeof(struct awusbdrd_softc),
618029e367dSAndriy Gapon };
619029e367dSAndriy Gapon 
620bc9372d7SJohn Baldwin DRIVER_MODULE(musbotg, simplebus, awusbdrd_driver, 0, 0);
621029e367dSAndriy Gapon MODULE_DEPEND(musbotg, usb, 1, 1, 1);
622