ingenic_ehci.c revision 1.5.16.2 1 1.5.16.2 jdolecek /* $NetBSD: ingenic_ehci.c,v 1.5.16.2 2017/12/03 11:36:28 jdolecek Exp $ */
2 1.5.16.2 jdolecek
3 1.5.16.2 jdolecek /*-
4 1.5.16.2 jdolecek * Copyright (c) 2015 Michael Lorenz
5 1.5.16.2 jdolecek * All rights reserved.
6 1.5.16.2 jdolecek *
7 1.5.16.2 jdolecek * Redistribution and use in source and binary forms, with or without
8 1.5.16.2 jdolecek * modification, are permitted provided that the following conditions
9 1.5.16.2 jdolecek * are met:
10 1.5.16.2 jdolecek * 1. Redistributions of source code must retain the above copyright
11 1.5.16.2 jdolecek * notice, this list of conditions and the following disclaimer.
12 1.5.16.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
13 1.5.16.2 jdolecek * notice, this list of conditions and the following disclaimer in the
14 1.5.16.2 jdolecek * documentation and/or other materials provided with the distribution.
15 1.5.16.2 jdolecek *
16 1.5.16.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.5.16.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.5.16.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.5.16.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.5.16.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.5.16.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.5.16.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.5.16.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.5.16.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.5.16.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.5.16.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
27 1.5.16.2 jdolecek */
28 1.5.16.2 jdolecek
29 1.5.16.2 jdolecek #include <sys/cdefs.h>
30 1.5.16.2 jdolecek __KERNEL_RCSID(0, "$NetBSD: ingenic_ehci.c,v 1.5.16.2 2017/12/03 11:36:28 jdolecek Exp $");
31 1.5.16.2 jdolecek
32 1.5.16.2 jdolecek #include <sys/param.h>
33 1.5.16.2 jdolecek #include <sys/systm.h>
34 1.5.16.2 jdolecek #include <sys/device.h>
35 1.5.16.2 jdolecek #include <sys/mutex.h>
36 1.5.16.2 jdolecek #include <sys/bus.h>
37 1.5.16.2 jdolecek #include <sys/workqueue.h>
38 1.5.16.2 jdolecek
39 1.5.16.2 jdolecek #include <mips/ingenic/ingenic_var.h>
40 1.5.16.2 jdolecek #include <mips/ingenic/ingenic_regs.h>
41 1.5.16.2 jdolecek
42 1.5.16.2 jdolecek #include <dev/usb/usb.h>
43 1.5.16.2 jdolecek #include <dev/usb/usbdi.h>
44 1.5.16.2 jdolecek #include <dev/usb/usbdivar.h>
45 1.5.16.2 jdolecek #include <dev/usb/usb_mem.h>
46 1.5.16.2 jdolecek
47 1.5.16.2 jdolecek #include <dev/usb/ehcireg.h>
48 1.5.16.2 jdolecek #include <dev/usb/ehcivar.h>
49 1.5.16.2 jdolecek
50 1.5.16.2 jdolecek #include <dev/usb/usbdevs.h>
51 1.5.16.2 jdolecek
52 1.5.16.2 jdolecek #include "opt_ingenic.h"
53 1.5.16.2 jdolecek #include "ohci.h"
54 1.5.16.2 jdolecek
55 1.5.16.2 jdolecek static int ingenic_ehci_match(device_t, struct cfdata *, void *);
56 1.5.16.2 jdolecek static void ingenic_ehci_attach(device_t, device_t, void *);
57 1.5.16.2 jdolecek
58 1.5.16.2 jdolecek CFATTACH_DECL_NEW(ingenic_ehci, sizeof(struct ehci_softc),
59 1.5.16.2 jdolecek ingenic_ehci_match, ingenic_ehci_attach, NULL, NULL);
60 1.5.16.2 jdolecek
61 1.5.16.2 jdolecek #if NOHCI > 0
62 1.5.16.2 jdolecek extern device_t ingenic_ohci;
63 1.5.16.2 jdolecek #endif
64 1.5.16.2 jdolecek
65 1.5.16.2 jdolecek /* ARGSUSED */
66 1.5.16.2 jdolecek static int
67 1.5.16.2 jdolecek ingenic_ehci_match(device_t parent, struct cfdata *match, void *aux)
68 1.5.16.2 jdolecek {
69 1.5.16.2 jdolecek struct apbus_attach_args *aa = aux;
70 1.5.16.2 jdolecek
71 1.5.16.2 jdolecek if (strcmp(aa->aa_name, "ehci") != 0)
72 1.5.16.2 jdolecek return 0;
73 1.5.16.2 jdolecek
74 1.5.16.2 jdolecek return 1;
75 1.5.16.2 jdolecek }
76 1.5.16.2 jdolecek
77 1.5.16.2 jdolecek static int
78 1.5.16.2 jdolecek ingenic_ehci_enable(struct ehci_softc *sc)
79 1.5.16.2 jdolecek {
80 1.5.16.2 jdolecek uint32_t reg;
81 1.5.16.2 jdolecek
82 1.5.16.2 jdolecek /* Togle VBUS pin */
83 1.5.16.2 jdolecek gpio_set(5, 15, 0);
84 1.5.16.2 jdolecek delay(250000);
85 1.5.16.2 jdolecek gpio_set(5, 15, 1);
86 1.5.16.2 jdolecek delay(250000);
87 1.5.16.2 jdolecek
88 1.5.16.2 jdolecek /* Enable OTG, should not be necessary since we use PLL clock */
89 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR);
90 1.5.16.2 jdolecek reg &= ~(PCR_OTG_DISABLE);
91 1.5.16.2 jdolecek writereg(JZ_USBPCR, reg);
92 1.5.16.2 jdolecek
93 1.5.16.2 jdolecek /* Select CORE as PLL reference */
94 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR1);
95 1.5.16.2 jdolecek reg |= PCR_REFCLK_CORE;
96 1.5.16.2 jdolecek writereg(JZ_USBPCR1, reg);
97 1.5.16.2 jdolecek
98 1.5.16.2 jdolecek /* Configure OTG PHY clock frequency */
99 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR1);
100 1.5.16.2 jdolecek reg &= ~PCR_CLK_M;
101 1.5.16.2 jdolecek reg |= PCR_CLK_48;
102 1.5.16.2 jdolecek writereg(JZ_USBPCR1, reg);
103 1.5.16.2 jdolecek
104 1.5.16.2 jdolecek /* Do not force port1 to suspend mode */
105 1.5.16.2 jdolecek reg = readreg(JZ_OPCR);
106 1.5.16.2 jdolecek reg |= OPCR_SPENDN1;
107 1.5.16.2 jdolecek writereg(JZ_OPCR, reg);
108 1.5.16.2 jdolecek
109 1.5.16.2 jdolecek /* D- pulldown */
110 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR1);
111 1.5.16.2 jdolecek reg |= PCR_DMPD1;
112 1.5.16.2 jdolecek writereg(JZ_USBPCR1, reg);
113 1.5.16.2 jdolecek
114 1.5.16.2 jdolecek /* D+ pulldown */
115 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR1);
116 1.5.16.2 jdolecek reg |= PCR_DPPD1;
117 1.5.16.2 jdolecek writereg(JZ_USBPCR1, reg);
118 1.5.16.2 jdolecek
119 1.5.16.2 jdolecek /* 16 bit bus witdth for port 1 (and 0) */
120 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR1);
121 1.5.16.2 jdolecek reg |= PCR_WORD_I_F1 | PCR_WORD_I_F0;
122 1.5.16.2 jdolecek writereg(JZ_USBPCR1, reg);
123 1.5.16.2 jdolecek
124 1.5.16.2 jdolecek /* Reset USB */
125 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR);
126 1.5.16.2 jdolecek reg |= PCR_POR;
127 1.5.16.2 jdolecek writereg(JZ_USBPCR, reg);
128 1.5.16.2 jdolecek delay(1);
129 1.5.16.2 jdolecek reg = readreg(JZ_USBPCR);
130 1.5.16.2 jdolecek reg &= ~(PCR_POR);
131 1.5.16.2 jdolecek writereg(JZ_USBPCR, reg);
132 1.5.16.2 jdolecek
133 1.5.16.2 jdolecek /* Soft-reset USB */
134 1.5.16.2 jdolecek reg = readreg(JZ_SRBC);
135 1.5.16.2 jdolecek reg |= (1 << 14);
136 1.5.16.2 jdolecek writereg(JZ_SRBC, reg);
137 1.5.16.2 jdolecek /* 300ms */
138 1.5.16.2 jdolecek delay(300000);
139 1.5.16.2 jdolecek
140 1.5.16.2 jdolecek reg = readreg(JZ_SRBC);
141 1.5.16.2 jdolecek reg &= ~(1 << 14);
142 1.5.16.2 jdolecek writereg(JZ_SRBC, reg);
143 1.5.16.2 jdolecek
144 1.5.16.2 jdolecek /* 300ms */
145 1.5.16.2 jdolecek delay(300000);
146 1.5.16.2 jdolecek
147 1.5.16.2 jdolecek return (0);
148 1.5.16.2 jdolecek }
149 1.5.16.2 jdolecek
150 1.5.16.2 jdolecek /* ARGSUSED */
151 1.5.16.2 jdolecek static void
152 1.5.16.2 jdolecek ingenic_ehci_attach(device_t parent, device_t self, void *aux)
153 1.5.16.2 jdolecek {
154 1.5.16.2 jdolecek struct ehci_softc *sc = device_private(self);
155 1.5.16.2 jdolecek struct apbus_attach_args *aa = aux;
156 1.5.16.2 jdolecek void *ih;
157 1.5.16.2 jdolecek int error;
158 1.5.16.2 jdolecek uint32_t reg;
159 1.5.16.2 jdolecek
160 1.5.16.2 jdolecek sc->sc_dev = self;
161 1.5.16.2 jdolecek
162 1.5.16.2 jdolecek sc->iot = aa->aa_bst;
163 1.5.16.2 jdolecek sc->sc_bus.ub_dmatag = aa->aa_dmat;
164 1.5.16.2 jdolecek sc->sc_bus.ub_hcpriv = sc;
165 1.5.16.2 jdolecek sc->sc_size = 0x1000;
166 1.5.16.2 jdolecek sc->sc_bus.ub_revision = USBREV_2_0;
167 1.5.16.2 jdolecek
168 1.5.16.2 jdolecek if (aa->aa_addr == 0)
169 1.5.16.2 jdolecek aa->aa_addr = JZ_EHCI_BASE;
170 1.5.16.2 jdolecek
171 1.5.16.2 jdolecek error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x1000, 0, &sc->ioh);
172 1.5.16.2 jdolecek if (error) {
173 1.5.16.2 jdolecek aprint_error_dev(self,
174 1.5.16.2 jdolecek "can't map registers for %s: %d\n", aa->aa_name, error);
175 1.5.16.2 jdolecek return;
176 1.5.16.2 jdolecek }
177 1.5.16.2 jdolecek
178 1.5.16.2 jdolecek aprint_naive(": EHCI USB controller\n");
179 1.5.16.2 jdolecek aprint_normal(": EHCI USB controller\n");
180 1.5.16.2 jdolecek
181 1.5.16.2 jdolecek ingenic_ehci_enable(sc);
182 1.5.16.2 jdolecek
183 1.5.16.2 jdolecek /* Disable EHCI interrupts */
184 1.5.16.2 jdolecek bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
185 1.5.16.2 jdolecek
186 1.5.16.2 jdolecek ih = evbmips_intr_establish(aa->aa_irq, ehci_intr, sc);
187 1.5.16.2 jdolecek
188 1.5.16.2 jdolecek if (ih == NULL) {
189 1.5.16.2 jdolecek aprint_error_dev(self, "failed to establish interrupt %d\n",
190 1.5.16.2 jdolecek aa->aa_irq);
191 1.5.16.2 jdolecek goto fail;
192 1.5.16.2 jdolecek }
193 1.5.16.2 jdolecek
194 1.5.16.2 jdolecek #if NOHCI > 0
195 1.5.16.2 jdolecek if (ingenic_ohci != NULL) {
196 1.5.16.2 jdolecek sc->sc_ncomp = 1;
197 1.5.16.2 jdolecek sc->sc_comps[0] = ingenic_ohci;
198 1.5.16.2 jdolecek } else
199 1.5.16.2 jdolecek sc->sc_ncomp = 0;
200 1.5.16.2 jdolecek #else
201 1.5.16.2 jdolecek sc->sc_ncomp = 0;
202 1.5.16.2 jdolecek sc->sc_npcomp = 0;
203 1.5.16.2 jdolecek #endif
204 1.5.16.2 jdolecek sc->sc_id_vendor = USB_VENDOR_INGENIC;
205 1.5.16.2 jdolecek strlcpy(sc->sc_vendor, "Ingenic", sizeof(sc->sc_vendor));
206 1.5.16.2 jdolecek
207 1.5.16.2 jdolecek error = ehci_init(sc);
208 1.5.16.2 jdolecek if (error) {
209 1.5.16.2 jdolecek aprint_error_dev(self, "init failed, error=%d\n", error);
210 1.5.16.2 jdolecek goto fail;
211 1.5.16.2 jdolecek }
212 1.5.16.2 jdolecek
213 1.5.16.2 jdolecek /*
214 1.5.16.2 jdolecek * voodoo from the linux driver:
215 1.5.16.2 jdolecek * select utmi data bus width of controller to 16bit
216 1.5.16.2 jdolecek */
217 1.5.16.2 jdolecek reg = bus_space_read_4(sc->iot, sc->ioh, 0xb0);
218 1.5.16.2 jdolecek reg |= 1 << 6;
219 1.5.16.2 jdolecek bus_space_write_4(sc->iot, sc->ioh, 0xb0, reg);
220 1.5.16.2 jdolecek
221 1.5.16.2 jdolecek /* Attach USB device */
222 1.5.16.2 jdolecek sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
223 1.5.16.2 jdolecek
224 1.5.16.2 jdolecek return;
225 1.5.16.2 jdolecek
226 1.5.16.2 jdolecek fail:
227 1.5.16.2 jdolecek if (ih) {
228 1.5.16.2 jdolecek evbmips_intr_disestablish(ih);
229 1.5.16.2 jdolecek }
230 1.5.16.2 jdolecek bus_space_unmap(sc->iot, sc->ioh, 0x1000);
231 1.5.16.2 jdolecek }
232