1/*	$NetBSD: ingenic_ohci.c,v 1.7 2021/08/07 16:18:59 thorpej 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_ohci.c,v 1.7 2021/08/07 16:18:59 thorpej 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#include <dev/usb/usbdevs.h>
47
48#include <dev/usb/ohcireg.h>
49#include <dev/usb/ohcivar.h>
50
51#include "opt_ingenic.h"
52
53static int ingenic_ohci_match(device_t, struct cfdata *, void *);
54static void ingenic_ohci_attach(device_t, device_t, void *);
55
56CFATTACH_DECL_NEW(ingenic_ohci, sizeof(struct ohci_softc),
57    ingenic_ohci_match, ingenic_ohci_attach, NULL, NULL);
58
59device_t ingenic_ohci = NULL;
60
61/* ARGSUSED */
62static int
63ingenic_ohci_match(device_t parent, struct cfdata *match, void *aux)
64{
65	struct apbus_attach_args *aa = aux;
66
67	if (strcmp(aa->aa_name, "ohci") != 0)
68		return 0;
69
70	return 1;
71}
72
73/* ARGSUSED */
74static void
75ingenic_ohci_attach(device_t parent, device_t self, void *aux)
76{
77	struct ohci_softc *sc = device_private(self);
78	struct apbus_attach_args *aa = aux;
79	void *ih;
80	int error;
81
82	sc->sc_dev = self;
83
84	sc->iot = aa->aa_bst;
85	sc->sc_bus.ub_dmatag = aa->aa_dmat;
86	sc->sc_bus.ub_hcpriv = sc;
87	sc->sc_size = 0x1000;
88
89	if (aa->aa_addr == 0)
90		aa->aa_addr = JZ_OHCI_BASE;
91
92	error = bus_space_map(aa->aa_bst, aa->aa_addr, 0x1000, 0, &sc->ioh);
93	if (error) {
94		aprint_error_dev(self,
95		    "can't map registers for %s: %d\n", aa->aa_name, error);
96		return;
97	}
98
99	aprint_naive(": OHCI USB controller\n");
100	aprint_normal(": OHCI USB controller\n");
101
102	/* Disable OHCI interrupts */
103	bus_space_write_4(sc->iot, sc->ioh, OHCI_INTERRUPT_DISABLE,
104	    OHCI_ALL_INTRS);
105
106	ih = evbmips_intr_establish(aa->aa_irq, ohci_intr, sc);
107
108	if (ih == NULL) {
109		aprint_error_dev(self, "failed to establish interrupt %d\n",
110		     aa->aa_irq);
111		goto fail;
112	}
113
114	sc->sc_endian = OHCI_LITTLE_ENDIAN;
115
116	error = ohci_init(sc);
117	if (error) {
118		aprint_error_dev(self, "init failed, error=%d\n", error);
119		goto fail;
120	}
121
122	/* Attach USB device */
123	sc->sc_child = config_found(self, &sc->sc_bus, usbctlprint, CFARGS_NONE);
124	ingenic_ohci = self;
125	return;
126
127fail:
128	if (ih) {
129		evbmips_intr_disestablish(ih);
130	}
131	bus_space_unmap(sc->iot, sc->ioh, 0x1000);
132}
133