xls_ehci.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
3 *
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss@carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 *    notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 *    notice, this list of conditions and the following disclaimer in the
18 *    documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#include <sys/cdefs.h>
34__FBSDID("$FreeBSD: stable/11/sys/mips/rmi/xls_ehci.c 330897 2018-03-14 03:19:51Z eadler $");
35
36#include "opt_bus.h"
37
38#include <sys/stdint.h>
39#include <sys/stddef.h>
40#include <sys/param.h>
41#include <sys/queue.h>
42#include <sys/types.h>
43#include <sys/systm.h>
44#include <sys/kernel.h>
45#include <sys/bus.h>
46#include <sys/module.h>
47#include <sys/lock.h>
48#include <sys/mutex.h>
49#include <sys/condvar.h>
50#include <sys/sysctl.h>
51#include <sys/sx.h>
52#include <sys/unistd.h>
53#include <sys/callout.h>
54#include <sys/malloc.h>
55#include <sys/priv.h>
56
57#include <sys/rman.h>
58#include <machine/resource.h>
59
60#include <dev/usb/usb.h>
61#include <dev/usb/usbdi.h>
62
63#include <dev/usb/usb_core.h>
64#include <dev/usb/usb_busdma.h>
65#include <dev/usb/usb_process.h>
66#include <dev/usb/usb_util.h>
67
68#include <dev/usb/usb_controller.h>
69#include <dev/usb/usb_bus.h>
70#include <dev/usb/controller/ehci.h>
71#include <dev/usb/controller/ehcireg.h>
72#include <mips/rmi/pic.h>
73
74static device_attach_t ehci_xls_attach;
75static device_detach_t ehci_xls_detach;
76
77static const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
78static const char *xlr_vendor_desc = "RMI Corp";
79
80static int
81ehci_xls_probe(device_t self)
82{
83	/* TODO see if usb is enabled on the board */
84	device_set_desc(self, xlr_usb_dev_desc);
85	return BUS_PROBE_DEFAULT;
86}
87
88static int
89ehci_xls_attach(device_t self)
90{
91	ehci_softc_t *sc = device_get_softc(self);
92	int err;
93	int rid;
94
95	sc->sc_bus.parent = self;
96	sc->sc_bus.devices = sc->sc_devices;
97	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
98	sc->sc_bus.dma_bits = 32;
99
100	/* get all DMA memory */
101	if (usb_bus_mem_alloc_all(&sc->sc_bus,
102	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
103		return (ENOMEM);
104	}
105
106	rid = 0;
107	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
108	    RF_ACTIVE);
109	if (!sc->sc_io_res) {
110		device_printf(self, "Could not map memory\n");
111		goto error;
112	}
113	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
114	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
115	printf("IO Resource tag %lx, hdl %lx, size %lx\n",
116	       (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl,
117	       (u_long)sc->sc_io_size);
118
119	rid = 0;
120	sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
121	    PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
122	if (sc->sc_irq_res == NULL) {
123		device_printf(self, "Could not allocate irq\n");
124		goto error;
125	}
126
127	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
128	if (!sc->sc_bus.bdev) {
129		device_printf(self, "Could not add USB device\n");
130		goto error;
131	}
132	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
133	device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
134
135	strlcpy(sc->sc_vendor, xlr_vendor_desc, sizeof(sc->sc_vendor));
136
137	err = bus_setup_intr(self, sc->sc_irq_res,
138	    INTR_TYPE_BIO | INTR_MPSAFE, NULL,
139	    (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
140	if (err) {
141		device_printf(self, "Could not setup irq, %d\n", err);
142		sc->sc_intr_hdl = NULL;
143		goto error;
144	}
145
146	err = ehci_init(sc);
147	if (err) {
148		device_printf(self, "USB init failed err=%d\n", err);
149		goto error;
150	}
151
152	err = device_probe_and_attach(sc->sc_bus.bdev);
153	if (err) {
154		device_printf(self, "USB probe and attach failed err=%d\n", err);
155		goto error;
156	}
157
158	return (0);
159
160error:
161	ehci_xls_detach(self);
162	return (ENXIO);
163}
164
165static int
166ehci_xls_detach(device_t self)
167{
168	ehci_softc_t *sc = device_get_softc(self);
169	int err;
170
171	/* during module unload there are lots of children leftover */
172	device_delete_children(self);
173
174	if (sc->sc_irq_res && sc->sc_intr_hdl) {
175		ehci_detach(sc);
176
177		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
178		if (err)
179			device_printf(self, "Could not tear down irq, %d\n",
180			    err);
181		sc->sc_intr_hdl = 0;
182	}
183
184	if (sc->sc_irq_res) {
185		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
186		sc->sc_irq_res = NULL;
187	}
188	if (sc->sc_io_res) {
189		bus_release_resource(self, SYS_RES_MEMORY, 0,
190		    sc->sc_io_res);
191		sc->sc_io_res = NULL;
192		sc->sc_io_tag = 0;
193		sc->sc_io_hdl = 0;
194	}
195
196	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
197
198	return (0);
199}
200
201static device_method_t ehci_methods[] = {
202	/* Device interface */
203	DEVMETHOD(device_probe, ehci_xls_probe),
204	DEVMETHOD(device_attach, ehci_xls_attach),
205	DEVMETHOD(device_detach, ehci_xls_detach),
206	DEVMETHOD(device_suspend, bus_generic_suspend),
207	DEVMETHOD(device_resume, bus_generic_resume),
208	DEVMETHOD(device_shutdown, bus_generic_shutdown),
209
210	DEVMETHOD_END
211};
212
213static driver_t ehci_driver = {
214	.name = "ehci",
215	.methods = ehci_methods,
216	.size = sizeof(struct ehci_softc),
217};
218
219static devclass_t ehci_devclass;
220
221DRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
222MODULE_DEPEND(ehci, usb, 1, 1, 1);
223