xref: /freebsd/sys/powerpc/ps3/ehci_ps3.c (revision 1f4bcc459a76b7aa664f3fd557684cd0ba6da352)
1 /*-
2  * Copyright (C) 2010 Nathan Whitehorn
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 #include <sys/stdint.h>
32 #include <sys/stddef.h>
33 #include <sys/param.h>
34 #include <sys/queue.h>
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/module.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/condvar.h>
43 #include <sys/sysctl.h>
44 #include <sys/sx.h>
45 #include <sys/unistd.h>
46 #include <sys/callout.h>
47 #include <sys/malloc.h>
48 #include <sys/priv.h>
49 
50 #include <sys/rman.h>
51 
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54 
55 #include <dev/usb/usb_core.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_process.h>
58 #include <dev/usb/usb_util.h>
59 
60 #include <dev/usb/usb_controller.h>
61 #include <dev/usb/usb_bus.h>
62 #include <dev/usb/controller/ehci.h>
63 #include <dev/usb/controller/ehcireg.h>
64 
65 #include "ps3bus.h"
66 
67 struct ps3_ehci_softc {
68 	ehci_softc_t            base;
69 	struct bus_space         tag;
70 };
71 
72 static void
73 ehci_ps3_post_reset(struct ehci_softc *ehci_softc)
74 {
75 	uint32_t usbmode;
76 
77 	/* Select big-endian mode */
78 	usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
79 	usbmode |= EHCI_UM_ES_BE;
80 	EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
81 }
82 
83 static int
84 ehci_ps3_probe(device_t dev)
85 {
86 	if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
87 	    ps3bus_get_devtype(dev) != PS3_DEVTYPE_USB)
88 		return (ENXIO);
89 
90 	device_set_desc(dev, "Playstation 3 USB 2.0 controller");
91 	return (BUS_PROBE_SPECIFIC);
92 }
93 
94 static int
95 ehci_ps3_attach(device_t dev)
96 {
97 	ehci_softc_t *sc = device_get_softc(dev);
98 	int rid, err;
99 
100 	sc->sc_bus.parent = dev;
101 	sc->sc_bus.devices = sc->sc_devices;
102 	sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
103 	sc->sc_bus.dma_bits = 32;
104 
105 	/* get all DMA memory */
106 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
107 	    USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc))
108 		return (ENOMEM);
109 
110 	rid = 1;
111 	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
112 	    &rid, RF_ACTIVE);
113 
114 	if (!sc->sc_io_res) {
115 		device_printf(dev, "Could not map memory\n");
116 		goto error;
117 	}
118 
119 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
120 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
121 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
122 
123 	rid = 1;
124 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
125 	    RF_SHAREABLE | RF_ACTIVE);
126 
127 	if (sc->sc_irq_res == NULL) {
128 		device_printf(dev, "Could not allocate irq\n");
129 		return (ENXIO);
130 	}
131 
132 	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
133 	if (!sc->sc_bus.bdev) {
134 		device_printf(dev, "Could not add USB device\n");
135 		return (ENXIO);
136 	}
137 
138 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
139 
140 	sprintf(sc->sc_vendor, "Sony");
141 
142 	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
143 	    NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
144 	if (err) {
145 		device_printf(dev, "Could not setup error irq, %d\n", err);
146 		goto error;
147 	}
148 
149 	sc->sc_vendor_post_reset = ehci_ps3_post_reset;
150 	err = ehci_init(sc);
151 	if (err) {
152 		device_printf(dev, "USB init failed err=%d\n", err);
153 		goto error;
154 	}
155 
156 	err = device_probe_and_attach(sc->sc_bus.bdev);
157 	if (err == 0)
158 		return (0);
159 
160 error:
161 	return (ENXIO);
162 }
163 
164 static device_method_t ehci_ps3_methods[] = {
165 	/* Device interface */
166 	DEVMETHOD(device_probe, ehci_ps3_probe),
167 	DEVMETHOD(device_attach, ehci_ps3_attach),
168 	DEVMETHOD(device_resume, bus_generic_resume),
169 	DEVMETHOD(device_suspend, bus_generic_suspend),
170 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
171 
172 	DEVMETHOD_END
173 };
174 
175 static driver_t ehci_ps3_driver = {
176 	.name = "ehci",
177 	.methods = ehci_ps3_methods,
178 	.size = sizeof(ehci_softc_t),
179 };
180 
181 static devclass_t ehci_ps3_devclass;
182 
183 DRIVER_MODULE(ehci_ps3, ps3bus, ehci_ps3_driver, ehci_ps3_devclass, 0, 0);
184 MODULE_DEPEND(ehci_ps3, usb, 1, 1, 1);
185