1217044Snwhitehorn/*-
2217044Snwhitehorn * Copyright (C) 2010 Nathan Whitehorn
3217044Snwhitehorn * All rights reserved.
4217044Snwhitehorn *
5217044Snwhitehorn * Redistribution and use in source and binary forms, with or without
6217044Snwhitehorn * modification, are permitted provided that the following conditions
7217044Snwhitehorn * are met:
8217044Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9217044Snwhitehorn *    notice, this list of conditions and the following disclaimer.
10217044Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
11217044Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
12217044Snwhitehorn *    documentation and/or other materials provided with the distribution.
13217044Snwhitehorn *
14217044Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15217044Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16217044Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17217044Snwhitehorn * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18217044Snwhitehorn * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19217044Snwhitehorn * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20217044Snwhitehorn * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21217044Snwhitehorn * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22217044Snwhitehorn * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23217044Snwhitehorn * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24217044Snwhitehorn *
25217044Snwhitehorn * $FreeBSD$
26217044Snwhitehorn */
27217044Snwhitehorn
28217044Snwhitehorn#include <sys/cdefs.h>
29217044Snwhitehorn__FBSDID("$FreeBSD$");
30217044Snwhitehorn
31217044Snwhitehorn#include <sys/stdint.h>
32217044Snwhitehorn#include <sys/stddef.h>
33217044Snwhitehorn#include <sys/param.h>
34217044Snwhitehorn#include <sys/queue.h>
35217044Snwhitehorn#include <sys/types.h>
36217044Snwhitehorn#include <sys/systm.h>
37217044Snwhitehorn#include <sys/kernel.h>
38217044Snwhitehorn#include <sys/bus.h>
39217044Snwhitehorn#include <sys/module.h>
40217044Snwhitehorn#include <sys/lock.h>
41217044Snwhitehorn#include <sys/mutex.h>
42217044Snwhitehorn#include <sys/condvar.h>
43217044Snwhitehorn#include <sys/sysctl.h>
44217044Snwhitehorn#include <sys/sx.h>
45217044Snwhitehorn#include <sys/unistd.h>
46217044Snwhitehorn#include <sys/callout.h>
47217044Snwhitehorn#include <sys/malloc.h>
48217044Snwhitehorn#include <sys/priv.h>
49217044Snwhitehorn
50217044Snwhitehorn#include <sys/rman.h>
51217044Snwhitehorn
52217044Snwhitehorn#include <dev/usb/usb.h>
53217044Snwhitehorn#include <dev/usb/usbdi.h>
54217044Snwhitehorn
55217044Snwhitehorn#include <dev/usb/usb_core.h>
56217044Snwhitehorn#include <dev/usb/usb_busdma.h>
57217044Snwhitehorn#include <dev/usb/usb_process.h>
58217044Snwhitehorn#include <dev/usb/usb_util.h>
59217044Snwhitehorn
60217044Snwhitehorn#include <dev/usb/usb_controller.h>
61217044Snwhitehorn#include <dev/usb/usb_bus.h>
62217044Snwhitehorn#include <dev/usb/controller/ehci.h>
63217044Snwhitehorn#include <dev/usb/controller/ehcireg.h>
64217044Snwhitehorn
65217044Snwhitehorn#include "ps3bus.h"
66217044Snwhitehorn
67217044Snwhitehornstruct ps3_ehci_softc {
68217044Snwhitehorn	ehci_softc_t            base;
69217044Snwhitehorn	struct bus_space         tag;
70217044Snwhitehorn};
71217044Snwhitehorn
72294989Smmelstatic void
73294989Smmelehci_ps3_post_reset(struct ehci_softc *ehci_softc)
74294989Smmel{
75294989Smmel	uint32_t usbmode;
76294989Smmel
77294989Smmel	/* Select big-endian mode */
78294989Smmel	usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
79294989Smmel	usbmode |= EHCI_UM_ES_BE;
80294989Smmel	EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
81294989Smmel}
82294989Smmel
83217044Snwhitehornstatic int
84217044Snwhitehornehci_ps3_probe(device_t dev)
85217044Snwhitehorn{
86217044Snwhitehorn	if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
87217044Snwhitehorn	    ps3bus_get_devtype(dev) != PS3_DEVTYPE_USB)
88217044Snwhitehorn		return (ENXIO);
89217044Snwhitehorn
90217044Snwhitehorn	device_set_desc(dev, "Playstation 3 USB 2.0 controller");
91217044Snwhitehorn	return (BUS_PROBE_SPECIFIC);
92217044Snwhitehorn}
93217044Snwhitehorn
94217044Snwhitehornstatic int
95217044Snwhitehornehci_ps3_attach(device_t dev)
96217044Snwhitehorn{
97217044Snwhitehorn	ehci_softc_t *sc = device_get_softc(dev);
98217044Snwhitehorn	int rid, err;
99217044Snwhitehorn
100217044Snwhitehorn	sc->sc_bus.parent = dev;
101217044Snwhitehorn	sc->sc_bus.devices = sc->sc_devices;
102217044Snwhitehorn	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
103276717Shselasky	sc->sc_bus.dma_bits = 32;
104217044Snwhitehorn
105276717Shselasky	/* get all DMA memory */
106217044Snwhitehorn	if (usb_bus_mem_alloc_all(&sc->sc_bus,
107217044Snwhitehorn	    USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc))
108217044Snwhitehorn		return (ENOMEM);
109217044Snwhitehorn
110217044Snwhitehorn	rid = 1;
111217044Snwhitehorn	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
112217044Snwhitehorn	    &rid, RF_ACTIVE);
113217044Snwhitehorn
114217044Snwhitehorn	if (!sc->sc_io_res) {
115217044Snwhitehorn		device_printf(dev, "Could not map memory\n");
116217044Snwhitehorn		goto error;
117217044Snwhitehorn	}
118217044Snwhitehorn
119217044Snwhitehorn	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
120217044Snwhitehorn	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
121217044Snwhitehorn	sc->sc_io_size = rman_get_size(sc->sc_io_res);
122217044Snwhitehorn
123217044Snwhitehorn	rid = 1;
124217044Snwhitehorn	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
125217044Snwhitehorn	    RF_SHAREABLE | RF_ACTIVE);
126217044Snwhitehorn
127217044Snwhitehorn	if (sc->sc_irq_res == NULL) {
128217044Snwhitehorn		device_printf(dev, "Could not allocate irq\n");
129217044Snwhitehorn		return (ENXIO);
130217044Snwhitehorn	}
131217044Snwhitehorn
132217044Snwhitehorn	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
133217044Snwhitehorn	if (!sc->sc_bus.bdev) {
134217044Snwhitehorn		device_printf(dev, "Could not add USB device\n");
135217044Snwhitehorn		return (ENXIO);
136217044Snwhitehorn	}
137217044Snwhitehorn
138217044Snwhitehorn	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
139217044Snwhitehorn
140217044Snwhitehorn	sprintf(sc->sc_vendor, "Sony");
141217044Snwhitehorn
142217044Snwhitehorn	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
143217044Snwhitehorn	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
144217044Snwhitehorn	if (err) {
145217044Snwhitehorn		device_printf(dev, "Could not setup error irq, %d\n", err);
146217044Snwhitehorn		goto error;
147217044Snwhitehorn	}
148217044Snwhitehorn
149294989Smmel	sc->sc_vendor_post_reset = ehci_ps3_post_reset;
150217044Snwhitehorn	err = ehci_init(sc);
151217044Snwhitehorn	if (err) {
152217044Snwhitehorn		device_printf(dev, "USB init failed err=%d\n", err);
153217044Snwhitehorn		goto error;
154217044Snwhitehorn	}
155217044Snwhitehorn
156217044Snwhitehorn	err = device_probe_and_attach(sc->sc_bus.bdev);
157217044Snwhitehorn	if (err == 0)
158217044Snwhitehorn		return (0);
159217044Snwhitehorn
160217044Snwhitehornerror:
161217044Snwhitehorn	return (ENXIO);
162217044Snwhitehorn}
163217044Snwhitehorn
164217044Snwhitehornstatic device_method_t ehci_ps3_methods[] = {
165217044Snwhitehorn	/* Device interface */
166217044Snwhitehorn	DEVMETHOD(device_probe, ehci_ps3_probe),
167217044Snwhitehorn	DEVMETHOD(device_attach, ehci_ps3_attach),
168228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
169228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
170228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
171217044Snwhitehorn
172227843Smarius	DEVMETHOD_END
173217044Snwhitehorn};
174217044Snwhitehorn
175217044Snwhitehornstatic driver_t ehci_ps3_driver = {
176228483Shselasky	.name = "ehci",
177228483Shselasky	.methods = ehci_ps3_methods,
178228483Shselasky	.size = sizeof(ehci_softc_t),
179217044Snwhitehorn};
180217044Snwhitehorn
181217044Snwhitehornstatic devclass_t ehci_ps3_devclass;
182217044Snwhitehorn
183217044SnwhitehornDRIVER_MODULE(ehci_ps3, ps3bus, ehci_ps3_driver, ehci_ps3_devclass, 0, 0);
184217044SnwhitehornMODULE_DEPEND(ehci_ps3, usb, 1, 1, 1);
185