1198160Srrs/*-
2198160Srrs * Copyright (c) 1998 The NetBSD Foundation, Inc.
3198160Srrs * All rights reserved.
4198160Srrs *
5198160Srrs * This code is derived from software contributed to The NetBSD Foundation
6198160Srrs * by Lennart Augustsson (augustss@carlstedt.se) at
7198160Srrs * Carlstedt Research & Technology.
8198160Srrs *
9198160Srrs * Redistribution and use in source and binary forms, with or without
10198160Srrs * modification, are permitted provided that the following conditions
11198160Srrs * are met:
12198160Srrs * 1. Redistributions of source code must retain the above copyright
13198160Srrs *    notice, this list of conditions and the following disclaimer.
14198160Srrs * 2. Redistributions in binary form must reproduce the above copyright
15198160Srrs *    notice, this list of conditions and the following disclaimer in the
16198160Srrs *    documentation and/or other materials provided with the distribution.
17198160Srrs *
18198160Srrs * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19198160Srrs * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20198160Srrs * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21198160Srrs * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22198160Srrs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23198160Srrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24198160Srrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25198160Srrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26198160Srrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27198160Srrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28198160Srrs * POSSIBILITY OF SUCH DAMAGE.
29198160Srrs */
30198160Srrs
31198160Srrs#include <sys/cdefs.h>
32202173Simp__FBSDID("$FreeBSD$");
33198160Srrs
34198160Srrs#include "opt_bus.h"
35198160Srrs
36204577Srrs#include <sys/stdint.h>
37204577Srrs#include <sys/stddef.h>
38198160Srrs#include <sys/param.h>
39204577Srrs#include <sys/queue.h>
40204577Srrs#include <sys/types.h>
41198160Srrs#include <sys/systm.h>
42198160Srrs#include <sys/kernel.h>
43204577Srrs#include <sys/bus.h>
44198160Srrs#include <sys/module.h>
45198607Srrs#include <sys/lock.h>
46204577Srrs#include <sys/mutex.h>
47204577Srrs#include <sys/condvar.h>
48204577Srrs#include <sys/sysctl.h>
49204577Srrs#include <sys/sx.h>
50204577Srrs#include <sys/unistd.h>
51204577Srrs#include <sys/callout.h>
52204577Srrs#include <sys/malloc.h>
53204577Srrs#include <sys/priv.h>
54204577Srrs
55198160Srrs#include <sys/rman.h>
56198160Srrs#include <machine/resource.h>
57198160Srrs
58198160Srrs#include <dev/usb/usb.h>
59198160Srrs#include <dev/usb/usbdi.h>
60198160Srrs
61204577Srrs#include <dev/usb/usb_core.h>
62204577Srrs#include <dev/usb/usb_busdma.h>
63204577Srrs#include <dev/usb/usb_process.h>
64204577Srrs#include <dev/usb/usb_util.h>
65198160Srrs
66204577Srrs#include <dev/usb/usb_controller.h>
67204577Srrs#include <dev/usb/usb_bus.h>
68204577Srrs#include <dev/usb/controller/ehci.h>
69204577Srrs#include <dev/usb/controller/ehcireg.h>
70204577Srrs#include <mips/rmi/pic.h>
71198625Srrs
72229096Shselaskystatic device_attach_t ehci_xls_attach;
73229096Shselaskystatic device_detach_t ehci_xls_detach;
74198160Srrs
75198160Srrsstatic const char *xlr_usb_dev_desc = "RMI XLR USB 2.0 controller";
76198160Srrsstatic const char *xlr_vendor_desc = "RMI Corp";
77204577Srrs
78198160Srrsstatic int
79198160Srrsehci_xls_probe(device_t self)
80198160Srrs{
81198160Srrs	/* TODO see if usb is enabled on the board */
82198160Srrs	device_set_desc(self, xlr_usb_dev_desc);
83198160Srrs	return BUS_PROBE_DEFAULT;
84198160Srrs}
85198160Srrs
86198160Srrsstatic int
87198160Srrsehci_xls_attach(device_t self)
88198160Srrs{
89198160Srrs	ehci_softc_t *sc = device_get_softc(self);
90198160Srrs	int err;
91198160Srrs	int rid;
92198160Srrs
93204577Srrs	sc->sc_bus.parent = self;
94204577Srrs	sc->sc_bus.devices = sc->sc_devices;
95204577Srrs	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
96198160Srrs
97204577Srrs	/* get all DMA memory */
98204577Srrs	if (usb_bus_mem_alloc_all(&sc->sc_bus,
99204577Srrs	    USB_GET_DMA_TAG(self), &ehci_iterate_hw_softc)) {
100204577Srrs		return (ENOMEM);
101204577Srrs	}
102198160Srrs
103198160Srrs	rid = 0;
104204577Srrs	sc->sc_io_res = bus_alloc_resource_any(self, SYS_RES_MEMORY, &rid,
105204577Srrs	    RF_ACTIVE);
106204577Srrs	if (!sc->sc_io_res) {
107198160Srrs		device_printf(self, "Could not map memory\n");
108204577Srrs		goto error;
109198160Srrs	}
110204577Srrs	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
111204577Srrs	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
112204577Srrs	printf("IO Resource tag %lx, hdl %lx, size %lx\n",
113204577Srrs	       (u_long)sc->sc_io_tag, (u_long)sc->sc_io_hdl,
114204577Srrs	       (u_long)sc->sc_io_size);
115198160Srrs
116198160Srrs	rid = 0;
117204577Srrs	sc->sc_irq_res = bus_alloc_resource(self, SYS_RES_IRQ, &rid,
118204577Srrs	    PIC_USB_IRQ, PIC_USB_IRQ, 1, RF_SHAREABLE | RF_ACTIVE);
119204577Srrs	if (sc->sc_irq_res == NULL) {
120198160Srrs		device_printf(self, "Could not allocate irq\n");
121204577Srrs		goto error;
122198160Srrs	}
123204577Srrs
124204577Srrs	sc->sc_bus.bdev = device_add_child(self, "usbus", -1);
125198160Srrs	if (!sc->sc_bus.bdev) {
126198160Srrs		device_printf(self, "Could not add USB device\n");
127204577Srrs		goto error;
128198160Srrs	}
129198160Srrs	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
130204577Srrs	device_set_desc(sc->sc_bus.bdev, xlr_usb_dev_desc);
131198160Srrs
132198160Srrs	sprintf(sc->sc_vendor, xlr_vendor_desc);
133198160Srrs
134204577Srrs	err = bus_setup_intr(self, sc->sc_irq_res,
135204577Srrs	    INTR_TYPE_BIO | INTR_MPSAFE, NULL,
136204577Srrs	    (driver_intr_t *) ehci_interrupt, sc, &sc->sc_intr_hdl);
137198160Srrs	if (err) {
138198160Srrs		device_printf(self, "Could not setup irq, %d\n", err);
139204577Srrs		sc->sc_intr_hdl = NULL;
140204577Srrs		goto error;
141198160Srrs	}
142198160Srrs
143198160Srrs	err = ehci_init(sc);
144198160Srrs	if (err) {
145198160Srrs		device_printf(self, "USB init failed err=%d\n", err);
146204577Srrs		goto error;
147198160Srrs	}
148204577Srrs
149204577Srrs	err = device_probe_and_attach(sc->sc_bus.bdev);
150204577Srrs	if (err) {
151204577Srrs		device_printf(self, "USB probe and attach failed err=%d\n", err);
152204577Srrs		goto error;
153204577Srrs	}
154204577Srrs
155204577Srrs	return (0);
156204577Srrs
157204577Srrserror:
158204577Srrs	ehci_xls_detach(self);
159204577Srrs	return (ENXIO);
160198160Srrs}
161198160Srrs
162198160Srrsstatic int
163198160Srrsehci_xls_detach(device_t self)
164198160Srrs{
165198160Srrs	ehci_softc_t *sc = device_get_softc(self);
166204577Srrs	device_t bdev;
167204577Srrs	int err;
168198160Srrs
169204577Srrs 	if (sc->sc_bus.bdev) {
170204577Srrs		bdev = sc->sc_bus.bdev;
171204577Srrs		device_detach(bdev);
172204577Srrs		device_delete_child(self, bdev);
173198160Srrs	}
174204577Srrs	/* during module unload there are lots of children leftover */
175229118Shselasky	device_delete_children(self);
176204577Srrs
177204577Srrs	if (sc->sc_irq_res && sc->sc_intr_hdl) {
178204577Srrs		ehci_detach(sc);
179198160Srrs
180204577Srrs		err = bus_teardown_intr(self, sc->sc_irq_res, sc->sc_intr_hdl);
181198160Srrs		if (err)
182198160Srrs			device_printf(self, "Could not tear down irq, %d\n",
183198160Srrs			    err);
184204577Srrs		sc->sc_intr_hdl = 0;
185198160Srrs	}
186204577Srrs
187204577Srrs	if (sc->sc_irq_res) {
188204577Srrs		bus_release_resource(self, SYS_RES_IRQ, 0, sc->sc_irq_res);
189204577Srrs		sc->sc_irq_res = NULL;
190198160Srrs	}
191204577Srrs	if (sc->sc_io_res) {
192204577Srrs		bus_release_resource(self, SYS_RES_MEMORY, 0,
193204577Srrs		    sc->sc_io_res);
194204577Srrs		sc->sc_io_res = NULL;
195204577Srrs		sc->sc_io_tag = 0;
196204577Srrs		sc->sc_io_hdl = 0;
197198160Srrs	}
198198160Srrs
199204577Srrs	usb_bus_mem_free_all(&sc->sc_bus, &ehci_iterate_hw_softc);
200198160Srrs
201204577Srrs	return (0);
202198160Srrs}
203198160Srrs
204198160Srrsstatic device_method_t ehci_methods[] = {
205198160Srrs	/* Device interface */
206198160Srrs	DEVMETHOD(device_probe, ehci_xls_probe),
207198160Srrs	DEVMETHOD(device_attach, ehci_xls_attach),
208198160Srrs	DEVMETHOD(device_detach, ehci_xls_detach),
209229096Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
210229096Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
211229096Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
212198160Srrs
213229093Shselasky	DEVMETHOD_END
214198160Srrs};
215198160Srrs
216198160Srrsstatic driver_t ehci_driver = {
217229096Shselasky	.name = "ehci",
218229096Shselasky	.methods = ehci_methods,
219229096Shselasky	.size = sizeof(struct ehci_softc),
220198160Srrs};
221198160Srrs
222198160Srrsstatic devclass_t ehci_devclass;
223198160Srrs
224198160SrrsDRIVER_MODULE(ehci, iodi, ehci_driver, ehci_devclass, 0, 0);
225204577SrrsMODULE_DEPEND(ehci, usb, 1, 1, 1);
226