ingenic_ehci.c revision 1.1 1 /* $NetBSD: ingenic_ehci.c,v 1.1 2015/03/08 17:14:27 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2015 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_ehci.c,v 1.1 2015/03/08 17:14:27 macallan Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/mutex.h>
36 #include <sys/bus.h>
37 #include <sys/workqueue.h>
38
39 #include <mips/ingenic/ingenic_var.h>
40 #include <mips/ingenic/ingenic_regs.h>
41
42 #include <dev/usb/usb.h>
43 #include <dev/usb/usbdi.h>
44 #include <dev/usb/usbdivar.h>
45 #include <dev/usb/usb_mem.h>
46
47 #include <dev/usb/ehcireg.h>
48 #include <dev/usb/ehcivar.h>
49
50 #include "opt_ingenic.h"
51 #include "ohci.h"
52
53 static int ingenic_ehci_match(device_t, struct cfdata *, void *);
54 static void ingenic_ehci_attach(device_t, device_t, void *);
55
56 CFATTACH_DECL_NEW(ingenic_ehci, sizeof(struct ehci_softc),
57 ingenic_ehci_match, ingenic_ehci_attach, NULL, NULL);
58
59 #if NOHCI > 0
60 extern device_t ingenic_ohci;
61 #endif
62
63 /* ARGSUSED */
64 static int
65 ingenic_ehci_match(device_t parent, struct cfdata *match, void *aux)
66 {
67 struct apbus_attach_args *aa = aux;
68
69 if (strcmp(aa->aa_name, "ehci") != 0)
70 return 0;
71
72 return 1;
73 }
74
75 /* ARGSUSED */
76 static void
77 ingenic_ehci_attach(device_t parent, device_t self, void *aux)
78 {
79 struct ehci_softc *sc = device_private(self);
80 struct apbus_attach_args *aa = aux;
81 void *ih;
82 int error, status;
83 uint32_t reg;
84
85 sc->sc_dev = self;
86
87 sc->iot = aa->aa_bst;
88 sc->sc_bus.dmatag = aa->aa_dmat;
89 sc->sc_bus.hci_private = sc;
90 sc->sc_size = 0x1000;
91 sc->sc_bus.usbrev = USBREV_2_0;
92
93 if (aa->aa_addr == 0)
94 aa->aa_addr = JZ_EHCI_BASE;
95
96 error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x1000, 0, &sc->ioh);
97 if (error) {
98 aprint_error_dev(self,
99 "can't map registers for %s: %d\n", aa->aa_name, error);
100 return;
101 }
102
103 aprint_naive(": EHCI USB controller\n");
104 aprint_normal(": EHCI USB controller\n");
105
106 /*
107 * voodoo from the linux driver:
108 * select utmi data bus width of controller to 16bit
109 */
110 reg = bus_space_read_4(sc->iot, sc->ioh, 0xb0);
111 reg |= 1 << 6;
112 bus_space_write_4(sc->iot, sc->ioh, 0xb0, reg);
113
114 /* Disable EHCI interrupts */
115 bus_space_write_4(sc->iot, sc->ioh, EHCI_USBINTR, 0);
116
117 ih = evbmips_intr_establish(20, ehci_intr, sc);
118
119 if (ih == NULL) {
120 aprint_error_dev(self, "failed to establish interrupt %d\n",
121 20);
122 goto fail;
123 }
124
125 #if NOHCI > 0
126 if (ingenic_ohci != NULL) {
127 sc->sc_ncomp = 1;
128 sc->sc_comps[0] = ingenic_ohci;
129 } else
130 sc->sc_ncomp = 0;
131 #else
132 sc->sc_ncomp = 0;
133 #endif
134
135 status = ehci_init(sc);
136 if (status != USBD_NORMAL_COMPLETION) {
137 aprint_error_dev(self, "init failed, error=%d\n", status);
138 goto fail;
139 }
140
141 /* Attach USB device */
142 sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint);
143
144 return;
145
146 fail:
147 if (ih) {
148 evbmips_intr_disestablish(ih);
149 }
150 bus_space_unmap(sc->iot, sc->ioh, 0x1000);
151 }
152