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