xls_ehci.c revision 323202
1314817Sngie/*-
2272343Sngie * Copyright (c) 1998 The NetBSD Foundation, Inc.
3272343Sngie * All rights reserved.
4272343Sngie *
5272343Sngie * This code is derived from software contributed to The NetBSD Foundation
6272343Sngie * by Lennart Augustsson (augustss@carlstedt.se) at
7272343Sngie * Carlstedt Research & Technology.
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie * 1. Redistributions of source code must retain the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer.
14272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
15272343Sngie *    notice, this list of conditions and the following disclaimer in the
16272343Sngie *    documentation and/or other materials provided with the distribution.
17272343Sngie *
18272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21272343Sngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28272343Sngie * POSSIBILITY OF SUCH DAMAGE.
29272343Sngie */
30272343Sngie
31272343Sngie#include <sys/cdefs.h>
32272343Sngie__FBSDID("$FreeBSD: stable/11/sys/mips/rmi/xls_ehci.c 323202 2017-09-06 02:04:14Z emaste $");
33272343Sngie
34272343Sngie#include "opt_bus.h"
35272343Sngie
36272343Sngie#include <sys/stdint.h>
37272343Sngie#include <sys/stddef.h>
38272343Sngie#include <sys/param.h>
39272343Sngie#include <sys/queue.h>
40272343Sngie#include <sys/types.h>
41272343Sngie#include <sys/systm.h>
42272343Sngie#include <sys/kernel.h>
43272343Sngie#include <sys/bus.h>
44314817Sngie#include <sys/module.h>
45272343Sngie#include <sys/lock.h>
46272343Sngie#include <sys/mutex.h>
47272343Sngie#include <sys/condvar.h>
48272343Sngie#include <sys/sysctl.h>
49272343Sngie#include <sys/sx.h>
50272343Sngie#include <sys/unistd.h>
51272343Sngie#include <sys/callout.h>
52272343Sngie#include <sys/malloc.h>
53272343Sngie#include <sys/priv.h>
54272343Sngie
55272343Sngie#include <sys/rman.h>
56272343Sngie#include <machine/resource.h>
57272343Sngie
58272343Sngie#include <dev/usb/usb.h>
59272343Sngie#include <dev/usb/usbdi.h>
60272343Sngie
61272343Sngie#include <dev/usb/usb_core.h>
62272343Sngie#include <dev/usb/usb_busdma.h>
63272343Sngie#include <dev/usb/usb_process.h>
64272343Sngie#include <dev/usb/usb_util.h>
65272343Sngie
66272343Sngie#include <dev/usb/usb_controller.h>
67272343Sngie#include <dev/usb/usb_bus.h>
68272343Sngie#include <dev/usb/controller/ehci.h>
69272343Sngie#include <dev/usb/controller/ehcireg.h>
70272343Sngie#include <mips/rmi/pic.h>
71272343Sngie
72272343Sngiestatic device_attach_t ehci_xls_attach;
73272343Sngiestatic device_detach_t ehci_xls_detach;
74272343Sngie
75272343Sngiestatic const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
76272343Sngiestatic const char *xlr_vendor_desc = "RMI Corp";
77272343Sngie
78272343Sngiestatic int
79272343Sngieehci_xls_probe(device_t self)
80272343Sngie{
81272343Sngie	/* TODO see if usb is enabled on the board */
82272343Sngie	device_set_desc(self, xlr_usb_dev_desc);
83272343Sngie	return BUS_PROBE_DEFAULT;
84272343Sngie}
85272343Sngie
86272343Sngiestatic int
87272343Sngieehci_xls_attach(device_t self)
88272343Sngie{
89272343Sngie	ehci_softc_t *sc = device_get_softc(self);
90272343Sngie	int err;
91272343Sngie	int rid;
92272343Sngie
93272343Sngie	sc->sc_bus.parent = self;
94272343Sngie	sc->sc_bus.devices = sc->sc_devices;
95272343Sngie	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
96272343Sngie	sc->sc_bus.dma_bits = 32;
97272343Sngie
98272343Sngie	/* get all DMA memory */
99272343Sngie	if (usb_bus_mem_alloc_all(&sc->sc_bus,
100272343Sngie	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
101272343Sngie		return (ENOMEM);
102272343Sngie	}
103272343Sngie
104272343Sngie	rid = 0;
105272343Sngie	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
106272343Sngie	    RF_ACTIVE);
107272343Sngie	if (!sc->sc_io_res) {
108272343Sngie		device_printf(self, "Could not map memory\n");
109272343Sngie		goto error;
110272343Sngie	}
111272343Sngie	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
112272343Sngie	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
113272343Sngie	printf("IO Resource tag %lx, hdl %lx, size %lx\n",
114272343Sngie	       (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl,
115272343Sngie	       (u_long)sc->sc_io_size);
116272343Sngie
117272343Sngie	rid = 0;
118272343Sngie	sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
119272343Sngie	    PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
120272343Sngie	if (sc->sc_irq_res == NULL) {
121272343Sngie		device_printf(self, "Could not allocate irq\n");
122		goto error;
123	}
124
125	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
126	if (!sc->sc_bus.bdev) {
127		device_printf(self, "Could not add USB device\n");
128		goto error;
129	}
130	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
131	device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
132
133	strlcpy(sc->sc_vendor, xlr_vendor_desc, sizeof(sc->sc_vendor));
134
135	err = bus_setup_intr(self, sc->sc_irq_res,
136	    INTR_TYPE_BIO | INTR_MPSAFE, NULL,
137	    (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
138	if (err) {
139		device_printf(self, "Could not setup irq, %d\n", err);
140		sc->sc_intr_hdl = NULL;
141		goto error;
142	}
143
144	err = ehci_init(sc);
145	if (err) {
146		device_printf(self, "USB init failed err=%d\n", err);
147		goto error;
148	}
149
150	err = device_probe_and_attach(sc->sc_bus.bdev);
151	if (err) {
152		device_printf(self, "USB probe and attach failed err=%d\n", err);
153		goto error;
154	}
155
156	return (0);
157
158error:
159	ehci_xls_detach(self);
160	return (ENXIO);
161}
162
163static int
164ehci_xls_detach(device_t self)
165{
166	ehci_softc_t *sc = device_get_softc(self);
167	int err;
168
169	/* during module unload there are lots of children leftover */
170	device_delete_children(self);
171
172	if (sc->sc_irq_res && sc->sc_intr_hdl) {
173		ehci_detach(sc);
174
175		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
176		if (err)
177			device_printf(self, "Could not tear down irq, %d\n",
178			    err);
179		sc->sc_intr_hdl = 0;
180	}
181
182	if (sc->sc_irq_res) {
183		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
184		sc->sc_irq_res = NULL;
185	}
186	if (sc->sc_io_res) {
187		bus_release_resource(self, SYS_RES_MEMORY, 0,
188		    sc->sc_io_res);
189		sc->sc_io_res = NULL;
190		sc->sc_io_tag = 0;
191		sc->sc_io_hdl = 0;
192	}
193
194	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
195
196	return (0);
197}
198
199static device_method_t ehci_methods[] = {
200	/* Device interface */
201	DEVMETHOD(device_probe, ehci_xls_probe),
202	DEVMETHOD(device_attach, ehci_xls_attach),
203	DEVMETHOD(device_detach, ehci_xls_detach),
204	DEVMETHOD(device_suspend, bus_generic_suspend),
205	DEVMETHOD(device_resume, bus_generic_resume),
206	DEVMETHOD(device_shutdown, bus_generic_shutdown),
207
208	DEVMETHOD_END
209};
210
211static driver_t ehci_driver = {
212	.name = "ehci",
213	.methods = ehci_methods,
214	.size = sizeof(struct ehci_softc),
215};
216
217static devclass_t ehci_devclass;
218
219DRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
220MODULE_DEPEND(ehci, usb, 1, 1, 1);
221