ingenic_dwctwo.c revision 1.2 1 /* $NetBSD: ingenic_dwctwo.c,v 1.2 2014/12/23 15:13:30 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2014 Michael Lorenz
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: ingenic_dwctwo.c,v 1.2 2014/12/23 15:13:30 macallan Exp $");
31
32 /*
33 * adapted from bcm2835_dwctwo.c
34 */
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/device.h>
39 #include <sys/mutex.h>
40 #include <sys/bus.h>
41 #include <sys/workqueue.h>
42
43 #include <mips/ingenic/ingenic_var.h>
44 #include <mips/ingenic/ingenic_regs.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 ingenic_dwc2_softc {
57 struct dwc2_softc sc_dwc2;
58
59 void *sc_ih;
60 };
61
62 static struct dwc2_core_params ingenic_dwc2_params = {
63 .otg_cap = 0, /* HNP/SRP capable */
64 .otg_ver = 0, /* 1.3 */
65 .dma_enable = 1,
66 .dma_desc_enable = 0,
67 .speed = 0, /* High Speed */
68 .enable_dynamic_fifo = 1,
69 .en_multiple_tx_fifo = 1,
70 .host_rx_fifo_size = 774, /* 774 DWORDs */
71 .host_nperio_tx_fifo_size = 256, /* 256 DWORDs */
72 .host_perio_tx_fifo_size = 512, /* 512 DWORDs */
73 .max_transfer_size = 65535,
74 .max_packet_count = 511,
75 .host_channels = 8,
76 .phy_type = 1, /* UTMI */
77 .phy_utmi_width = 8, /* 8 bits */
78 .phy_ulpi_ddr = 0, /* Single */
79 .phy_ulpi_ext_vbus = 0,
80 .i2c_enable = 0,
81 .ulpi_fs_ls = 0,
82 .host_support_fs_ls_low_power = 0,
83 .host_ls_low_power_phy_clk = 0, /* 48 MHz */
84 .ts_dline = 0,
85 .reload_ctl = 0,
86 .ahbcfg = 0x10,
87 .uframe_sched = 1,
88 };
89
90 static int ingenic_dwc2_match(device_t, struct cfdata *, void *);
91 static void ingenic_dwc2_attach(device_t, device_t, void *);
92 static void ingenic_dwc2_deferred(device_t);
93
94 CFATTACH_DECL_NEW(ingenic_dwctwo, sizeof(struct ingenic_dwc2_softc),
95 ingenic_dwc2_match, ingenic_dwc2_attach, NULL, NULL);
96
97 /* ARGSUSED */
98 static int
99 ingenic_dwc2_match(device_t parent, struct cfdata *match, void *aux)
100 {
101 struct apbus_attach_args *aa = aux;
102
103 if (strcmp(aa->aa_name, "dwctwo") != 0)
104 return 0;
105
106 return 1;
107 }
108
109 /* ARGSUSED */
110 static void
111 ingenic_dwc2_attach(device_t parent, device_t self, void *aux)
112 {
113 struct ingenic_dwc2_softc *sc = device_private(self);
114 struct apbus_attach_args *aa = aux;
115 uint32_t reg;
116 int error;
117
118 sc->sc_dwc2.sc_dev = self;
119
120 sc->sc_dwc2.sc_iot = aa->aa_bst;
121 sc->sc_dwc2.sc_bus.dmatag = aa->aa_dmat;
122 sc->sc_dwc2.sc_params = &ingenic_dwc2_params;
123
124 if (aa->aa_addr == 0)
125 aa->aa_addr = 0x13500000;
126
127 error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x20000, 0,
128 &sc->sc_dwc2.sc_ioh);
129 if (error) {
130 aprint_error_dev(self,
131 "can't map registers for %s: %d\n", aa->aa_name, error);
132 return;
133 }
134
135 aprint_naive(": USB controller\n");
136 aprint_normal(": USB controller\n");
137
138 reg = readreg(JZ_USBPCR);
139 reg |= VBUSVLDEXTSEL;
140 reg |= VBUSVLDEXT;
141 reg |= USB_MODE;
142 reg |= COMMONONN;
143 reg &= ~OTG_DISABLE;
144 writereg(JZ_USBPCR, reg);
145
146 reg = readreg(JZ_USBPCR1);
147 reg |= PCR_SYNOPSYS;
148 reg |= PCR_REFCLK_CORE;
149 reg &= ~PCR_CLK_M;
150 reg |= PCR_CLK_48;
151 reg |= PCR_WORD_I_F0;
152 writereg(JZ_USBPCR1, reg);
153
154 delay(10000);
155
156 sc->sc_ih = evbmips_intr_establish(21, dwc2_intr, &sc->sc_dwc2);
157
158 if (sc->sc_ih == NULL) {
159 aprint_error_dev(self, "failed to establish interrupt %d\n",
160 21);
161 goto fail;
162 }
163
164 config_defer(self, ingenic_dwc2_deferred);
165
166 return;
167
168 fail:
169 if (sc->sc_ih) {
170 evbmips_intr_disestablish(sc->sc_ih);
171 sc->sc_ih = NULL;
172 }
173 bus_space_unmap(sc->sc_dwc2.sc_iot, sc->sc_dwc2.sc_ioh, 0x20000);
174 }
175
176 static void
177 ingenic_dwc2_deferred(device_t self)
178 {
179 struct ingenic_dwc2_softc *sc = device_private(self);
180 int error;
181
182 error = dwc2_init(&sc->sc_dwc2);
183 if (error != 0) {
184 aprint_error_dev(self, "couldn't initialize host, error=%d\n",
185 error);
186 return;
187 }
188 sc->sc_dwc2.sc_child = config_found(sc->sc_dwc2.sc_dev,
189 &sc->sc_dwc2.sc_bus, usbctlprint);
190 }
191