xref: /netbsd-src/sys/arch/arm/broadcom/bcm2835_dwctwo.c (revision c7fb772b85b2b5d4cfb282f868f454b4701534fd)
1 /*	$NetBSD: bcm2835_dwctwo.c,v 1.12 2021/08/07 16:18:43 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Nick Hudson
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: bcm2835_dwctwo.c,v 1.12 2021/08/07 16:18:43 thorpej Exp $");
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/mutex.h>
39 #include <sys/bus.h>
40 #include <sys/workqueue.h>
41 
42 #include <arm/broadcom/bcm2835reg.h>
43 
44 #include <dev/fdt/fdtvar.h>
45 
46 #include <dev/usb/usb.h>
47 #include <dev/usb/usbdi.h>
48 #include <dev/usb/usbdivar.h>
49 #include <dev/usb/usb_mem.h>
50 
51 #include <dwc2/dwc2var.h>
52 
53 #include <dwc2/dwc2.h>
54 #include "dwc2_core.h"
55 
56 struct bcmdwc2_softc {
57 	struct dwc2_softc	sc_dwc2;
58 
59 	void			*sc_ih;
60 	int			sc_phandle;
61 };
62 
63 static struct dwc2_core_params bcmdwc2_params = {
64 	.otg_cap			= 0,	/* HNP/SRP capable */
65 	.otg_ver			= 0,	/* 1.3 */
66 	.dma_enable			= 1,
67 	.dma_desc_enable		= 0,
68 	.speed				= 0,	/* High Speed */
69 	.enable_dynamic_fifo		= 1,
70 	.en_multiple_tx_fifo		= 1,
71 	.host_rx_fifo_size		= 774,	/* 774 DWORDs */
72 	.host_nperio_tx_fifo_size	= 256,	/* 256 DWORDs */
73 	.host_perio_tx_fifo_size	= 512,	/* 512 DWORDs */
74 	.max_transfer_size		= 65535,
75 	.max_packet_count		= 511,
76 	.host_channels			= 8,
77 	.phy_type			= 1,	/* UTMI */
78 	.phy_utmi_width			= 8,	/* 8 bits */
79 	.phy_ulpi_ddr			= 0,	/* Single */
80 	.phy_ulpi_ext_vbus		= 0,
81 	.i2c_enable			= 0,
82 	.ulpi_fs_ls			= 0,
83 	.host_support_fs_ls_low_power	= 0,
84 	.host_ls_low_power_phy_clk	= 0,	/* 48 MHz */
85 	.ts_dline			= 0,
86 	.reload_ctl			= 0,
87 	.ahbcfg				= 0x10,
88 	.uframe_sched			= 1,
89 	.external_id_pin_ctl		= -1,
90 	.hibernation			= -1,
91 };
92 
93 static int bcmdwc2_match(device_t, struct cfdata *, void *);
94 static void bcmdwc2_attach(device_t, device_t, void *);
95 static void bcmdwc2_deferred(device_t);
96 
97 CFATTACH_DECL_NEW(bcmdwctwo, sizeof(struct bcmdwc2_softc),
98     bcmdwc2_match, bcmdwc2_attach, NULL, NULL);
99 
100 static const struct device_compatible_entry compat_data[] = {
101 	{ .compat = "brcm,bcm2708-usb" },
102 	{ .compat = "brcm,bcm2835-usb" },
103 	DEVICE_COMPAT_EOL
104 };
105 
106 /* ARGSUSED */
107 static int
bcmdwc2_match(device_t parent,struct cfdata * match,void * aux)108 bcmdwc2_match(device_t parent, struct cfdata *match, void *aux)
109 {
110 	struct fdt_attach_args * const faa = aux;
111 
112 	return of_compatible_match(faa->faa_phandle, compat_data);
113 }
114 
115 /* ARGSUSED */
116 static void
bcmdwc2_attach(device_t parent,device_t self,void * aux)117 bcmdwc2_attach(device_t parent, device_t self, void *aux)
118 {
119 	struct bcmdwc2_softc *sc = device_private(self);
120 	struct fdt_attach_args * const faa = aux;
121 	const int phandle = faa->faa_phandle;
122 	bus_addr_t addr;
123 	bus_size_t size;
124 	int error;
125 
126 	aprint_naive(": USB controller\n");
127 	aprint_normal(": USB controller\n");
128 
129 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
130 		aprint_error(": couldn't get registers\n");
131 		return;
132 	}
133 
134 	sc->sc_phandle = phandle;
135 	sc->sc_dwc2.sc_dev = self;
136 	sc->sc_dwc2.sc_iot = faa->faa_bst;
137 	sc->sc_dwc2.sc_bus.ub_dmatag = faa->faa_dmat;
138 	sc->sc_dwc2.sc_params = &bcmdwc2_params;
139 
140 	error = bus_space_map(faa->faa_bst, addr, size, 0, &sc->sc_dwc2.sc_ioh);
141 	if (error) {
142 		aprint_error(": couldn't map device\n");
143 		return;
144 	}
145 
146 	char intrstr[128];
147 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
148 		aprint_error(": failed to decode interrupt\n");
149 		return;
150 	}
151 
152 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, FDT_INTR_MPSAFE,
153 	    dwc2_intr, &sc->sc_dwc2, device_xname(self));
154 
155 	if (sc->sc_ih == NULL) {
156 		aprint_error_dev(self, "failed to establish interrupt %s\n",
157 		    intrstr);
158 		goto fail;
159 	}
160 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
161 	config_interrupts(self, bcmdwc2_deferred);
162 
163 	return;
164 
165 fail:
166 	if (sc->sc_ih) {
167 		fdtbus_intr_disestablish(sc->sc_phandle, sc->sc_ih);
168 		sc->sc_ih = NULL;
169 	}
170 	bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, size);
171 }
172 
173 static void
bcmdwc2_deferred(device_t self)174 bcmdwc2_deferred(device_t self)
175 {
176 	struct bcmdwc2_softc *sc = device_private(self);
177 	int error;
178 
179 	error = dwc2_init(&sc->sc_dwc2);
180 	if (error != 0) {
181 		aprint_error_dev(self, "couldn't initialize host, error=%d\n",
182 		    error);
183 		return;
184 	}
185 	sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
186 	    &sc->sc_dwc2.sc_bus, usbctlprint, CFARGS_NONE);
187 }
188