1*c7fb772bSthorpej /* $NetBSD: ingenic_ohci.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $ */
2e3ba4f1cSmacallan
3e3ba4f1cSmacallan /*-
4e3ba4f1cSmacallan * Copyright (c) 2015 Michael Lorenz
5e3ba4f1cSmacallan * All rights reserved.
6e3ba4f1cSmacallan *
7e3ba4f1cSmacallan * Redistribution and use in source and binary forms, with or without
8e3ba4f1cSmacallan * modification, are permitted provided that the following conditions
9e3ba4f1cSmacallan * are met:
10e3ba4f1cSmacallan * 1. Redistributions of source code must retain the above copyright
11e3ba4f1cSmacallan * notice, this list of conditions and the following disclaimer.
12e3ba4f1cSmacallan * 2. Redistributions in binary form must reproduce the above copyright
13e3ba4f1cSmacallan * notice, this list of conditions and the following disclaimer in the
14e3ba4f1cSmacallan * documentation and/or other materials provided with the distribution.
15e3ba4f1cSmacallan *
16e3ba4f1cSmacallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17e3ba4f1cSmacallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18e3ba4f1cSmacallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19e3ba4f1cSmacallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20e3ba4f1cSmacallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21e3ba4f1cSmacallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22e3ba4f1cSmacallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23e3ba4f1cSmacallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24e3ba4f1cSmacallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25e3ba4f1cSmacallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26e3ba4f1cSmacallan * POSSIBILITY OF SUCH DAMAGE.
27e3ba4f1cSmacallan */
28e3ba4f1cSmacallan
29e3ba4f1cSmacallan #include <sys/cdefs.h>
30*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: ingenic_ohci.c,v 1.7 2021/08/07 16:18:59 thorpej Exp $");
31e3ba4f1cSmacallan
32e3ba4f1cSmacallan #include <sys/param.h>
33e3ba4f1cSmacallan #include <sys/systm.h>
34e3ba4f1cSmacallan #include <sys/device.h>
35e3ba4f1cSmacallan #include <sys/mutex.h>
36e3ba4f1cSmacallan #include <sys/bus.h>
37e3ba4f1cSmacallan #include <sys/workqueue.h>
38e3ba4f1cSmacallan
39e3ba4f1cSmacallan #include <mips/ingenic/ingenic_var.h>
40e3ba4f1cSmacallan #include <mips/ingenic/ingenic_regs.h>
41e3ba4f1cSmacallan
42e3ba4f1cSmacallan #include <dev/usb/usb.h>
43e3ba4f1cSmacallan #include <dev/usb/usbdi.h>
44e3ba4f1cSmacallan #include <dev/usb/usbdivar.h>
45e3ba4f1cSmacallan #include <dev/usb/usb_mem.h>
464f8ab635Smacallan #include <dev/usb/usbdevs.h>
47e3ba4f1cSmacallan
48e3ba4f1cSmacallan #include <dev/usb/ohcireg.h>
49e3ba4f1cSmacallan #include <dev/usb/ohcivar.h>
50e3ba4f1cSmacallan
51e3ba4f1cSmacallan #include "opt_ingenic.h"
52e3ba4f1cSmacallan
53e3ba4f1cSmacallan static int ingenic_ohci_match(device_t, struct cfdata *, void *);
54e3ba4f1cSmacallan static void ingenic_ohci_attach(device_t, device_t, void *);
55e3ba4f1cSmacallan
56e3ba4f1cSmacallan CFATTACH_DECL_NEW(ingenic_ohci, sizeof(struct ohci_softc),
57e3ba4f1cSmacallan ingenic_ohci_match, ingenic_ohci_attach, NULL, NULL);
58e3ba4f1cSmacallan
59e3ba4f1cSmacallan device_t ingenic_ohci = NULL;
60e3ba4f1cSmacallan
61e3ba4f1cSmacallan /* ARGSUSED */
62e3ba4f1cSmacallan static int
ingenic_ohci_match(device_t parent,struct cfdata * match,void * aux)63e3ba4f1cSmacallan ingenic_ohci_match(device_t parent, struct cfdata *match, void *aux)
64e3ba4f1cSmacallan {
65e3ba4f1cSmacallan struct apbus_attach_args *aa = aux;
66e3ba4f1cSmacallan
67e3ba4f1cSmacallan if (strcmp(aa->aa_name, "ohci") != 0)
68e3ba4f1cSmacallan return 0;
69e3ba4f1cSmacallan
70e3ba4f1cSmacallan return 1;
71e3ba4f1cSmacallan }
72e3ba4f1cSmacallan
73e3ba4f1cSmacallan /* ARGSUSED */
74e3ba4f1cSmacallan static void
ingenic_ohci_attach(device_t parent,device_t self,void * aux)75e3ba4f1cSmacallan ingenic_ohci_attach(device_t parent, device_t self, void *aux)
76e3ba4f1cSmacallan {
77e3ba4f1cSmacallan struct ohci_softc *sc = device_private(self);
78e3ba4f1cSmacallan struct apbus_attach_args *aa = aux;
79e3ba4f1cSmacallan void *ih;
804e8e6643Sskrll int error;
81e3ba4f1cSmacallan
82e3ba4f1cSmacallan sc->sc_dev = self;
83e3ba4f1cSmacallan
84e3ba4f1cSmacallan sc->iot = aa->aa_bst;
854e8e6643Sskrll sc->sc_bus.ub_dmatag = aa->aa_dmat;
864e8e6643Sskrll sc->sc_bus.ub_hcpriv = sc;
87e3ba4f1cSmacallan sc->sc_size = 0x1000;
88e3ba4f1cSmacallan
89e3ba4f1cSmacallan if (aa->aa_addr == 0)
90e3ba4f1cSmacallan aa->aa_addr = JZ_OHCI_BASE;
91e3ba4f1cSmacallan
92e3ba4f1cSmacallan error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x1000, 0, &sc->ioh);
93e3ba4f1cSmacallan if (error) {
94e3ba4f1cSmacallan aprint_error_dev(self,
95e3ba4f1cSmacallan "can't map registers for %s: %d\n", aa->aa_name, error);
96e3ba4f1cSmacallan return;
97e3ba4f1cSmacallan }
98e3ba4f1cSmacallan
99e3ba4f1cSmacallan aprint_naive(": OHCI USB controller\n");
100e3ba4f1cSmacallan aprint_normal(": OHCI USB controller\n");
101e3ba4f1cSmacallan
102e3ba4f1cSmacallan /* Disable OHCI interrupts */
103e3ba4f1cSmacallan bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
104e3ba4f1cSmacallan OHCI_ALL_INTRS);
105e3ba4f1cSmacallan
106613f5923Smacallan ih = evbmips_intr_establish(aa->aa_irq, ohci_intr, sc);
107e3ba4f1cSmacallan
108e3ba4f1cSmacallan if (ih == NULL) {
109e3ba4f1cSmacallan aprint_error_dev(self, "failed to establish interrupt %d\n",
110613f5923Smacallan aa->aa_irq);
111e3ba4f1cSmacallan goto fail;
112e3ba4f1cSmacallan }
113e3ba4f1cSmacallan
114e3ba4f1cSmacallan sc->sc_endian = OHCI_LITTLE_ENDIAN;
115e3ba4f1cSmacallan
1164e8e6643Sskrll error = ohci_init(sc);
1174e8e6643Sskrll if (error) {
1184e8e6643Sskrll aprint_error_dev(self, "init failed, error=%d\n", error);
119e3ba4f1cSmacallan goto fail;
120e3ba4f1cSmacallan }
121e3ba4f1cSmacallan
122e3ba4f1cSmacallan /* Attach USB device */
123*c7fb772bSthorpej sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
124e3ba4f1cSmacallan ingenic_ohci = self;
125e3ba4f1cSmacallan return;
126e3ba4f1cSmacallan
127e3ba4f1cSmacallan fail:
128e3ba4f1cSmacallan if (ih) {
129e3ba4f1cSmacallan evbmips_intr_disestablish(ih);
130e3ba4f1cSmacallan }
131e3ba4f1cSmacallan bus_space_unmap(sc->iot, sc->ioh, 0x1000);
132e3ba4f1cSmacallan }
133