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: releng/10.3/sys/powerpc/ps3/ehci_ps3.c 278278 2015-02-05 20:03:02Z hselasky $
26217044Snwhitehorn */
27217044Snwhitehorn
28217044Snwhitehorn#include <sys/cdefs.h>
29217044Snwhitehorn__FBSDID("$FreeBSD: releng/10.3/sys/powerpc/ps3/ehci_ps3.c 278278 2015-02-05 20:03:02Z hselasky $");
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
72217044Snwhitehornstatic int
73217044Snwhitehornehci_ps3_probe(device_t dev)
74217044Snwhitehorn{
75217044Snwhitehorn	if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
76217044Snwhitehorn	    ps3bus_get_devtype(dev) != PS3_DEVTYPE_USB)
77217044Snwhitehorn		return (ENXIO);
78217044Snwhitehorn
79217044Snwhitehorn	device_set_desc(dev, "Playstation 3 USB 2.0 controller");
80217044Snwhitehorn	return (BUS_PROBE_SPECIFIC);
81217044Snwhitehorn}
82217044Snwhitehorn
83217044Snwhitehornstatic int
84217044Snwhitehornehci_ps3_attach(device_t dev)
85217044Snwhitehorn{
86217044Snwhitehorn	ehci_softc_t *sc = device_get_softc(dev);
87217044Snwhitehorn	int rid, err;
88217044Snwhitehorn
89217044Snwhitehorn	sc->sc_bus.parent = dev;
90217044Snwhitehorn	sc->sc_bus.devices = sc->sc_devices;
91217044Snwhitehorn	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
92278278Shselasky	sc->sc_bus.dma_bits = 32;
93217044Snwhitehorn
94278278Shselasky	/* get all DMA memory */
95217044Snwhitehorn	if (usb_bus_mem_alloc_all(&sc->sc_bus,
96217044Snwhitehorn	    USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc))
97217044Snwhitehorn		return (ENOMEM);
98217044Snwhitehorn
99217044Snwhitehorn	rid = 1;
100217044Snwhitehorn	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
101217044Snwhitehorn	    &rid, RF_ACTIVE);
102217044Snwhitehorn
103217044Snwhitehorn	if (!sc->sc_io_res) {
104217044Snwhitehorn		device_printf(dev, "Could not map memory\n");
105217044Snwhitehorn		goto error;
106217044Snwhitehorn	}
107217044Snwhitehorn
108217044Snwhitehorn	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
109217044Snwhitehorn	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
110217044Snwhitehorn	sc->sc_io_size = rman_get_size(sc->sc_io_res);
111217044Snwhitehorn
112217044Snwhitehorn	rid = 1;
113217044Snwhitehorn	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
114217044Snwhitehorn	    RF_SHAREABLE | RF_ACTIVE);
115217044Snwhitehorn
116217044Snwhitehorn	if (sc->sc_irq_res == NULL) {
117217044Snwhitehorn		device_printf(dev, "Could not allocate irq\n");
118217044Snwhitehorn		return (ENXIO);
119217044Snwhitehorn	}
120217044Snwhitehorn
121217044Snwhitehorn	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
122217044Snwhitehorn	if (!sc->sc_bus.bdev) {
123217044Snwhitehorn		device_printf(dev, "Could not add USB device\n");
124217044Snwhitehorn		return (ENXIO);
125217044Snwhitehorn	}
126217044Snwhitehorn
127217044Snwhitehorn	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
128217044Snwhitehorn
129217044Snwhitehorn	sprintf(sc->sc_vendor, "Sony");
130217044Snwhitehorn
131217044Snwhitehorn	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
132217044Snwhitehorn	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
133217044Snwhitehorn	if (err) {
134217044Snwhitehorn		device_printf(dev, "Could not setup error irq, %d\n", err);
135217044Snwhitehorn		goto error;
136217044Snwhitehorn	}
137217044Snwhitehorn
138217044Snwhitehorn	sc->sc_flags |= EHCI_SCFLG_BIGEMMIO;
139217044Snwhitehorn	err = ehci_init(sc);
140217044Snwhitehorn	if (err) {
141217044Snwhitehorn		device_printf(dev, "USB init failed err=%d\n", err);
142217044Snwhitehorn		goto error;
143217044Snwhitehorn	}
144217044Snwhitehorn
145217044Snwhitehorn	err = device_probe_and_attach(sc->sc_bus.bdev);
146217044Snwhitehorn	if (err == 0)
147217044Snwhitehorn		return (0);
148217044Snwhitehorn
149217044Snwhitehornerror:
150217044Snwhitehorn	return (ENXIO);
151217044Snwhitehorn}
152217044Snwhitehorn
153217044Snwhitehornstatic device_method_t ehci_ps3_methods[] = {
154217044Snwhitehorn	/* Device interface */
155217044Snwhitehorn	DEVMETHOD(device_probe, ehci_ps3_probe),
156217044Snwhitehorn	DEVMETHOD(device_attach, ehci_ps3_attach),
157228483Shselasky	DEVMETHOD(device_resume, bus_generic_resume),
158228483Shselasky	DEVMETHOD(device_suspend, bus_generic_suspend),
159228483Shselasky	DEVMETHOD(device_shutdown, bus_generic_shutdown),
160217044Snwhitehorn
161227843Smarius	DEVMETHOD_END
162217044Snwhitehorn};
163217044Snwhitehorn
164217044Snwhitehornstatic driver_t ehci_ps3_driver = {
165228483Shselasky	.name = "ehci",
166228483Shselasky	.methods = ehci_ps3_methods,
167228483Shselasky	.size = sizeof(ehci_softc_t),
168217044Snwhitehorn};
169217044Snwhitehorn
170217044Snwhitehornstatic devclass_t ehci_ps3_devclass;
171217044Snwhitehorn
172217044SnwhitehornDRIVER_MODULE(ehci_ps3, ps3bus, ehci_ps3_driver, ehci_ps3_devclass, 0, 0);
173217044SnwhitehornMODULE_DEPEND(ehci_ps3, usb, 1, 1, 1);
174