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