xref: /freebsd/sys/powerpc/ps3/ohci_ps3.c (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
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/linker_set.h>
40 #include <sys/module.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/condvar.h>
44 #include <sys/sysctl.h>
45 #include <sys/sx.h>
46 #include <sys/unistd.h>
47 #include <sys/callout.h>
48 #include <sys/malloc.h>
49 #include <sys/priv.h>
50 
51 #include <sys/rman.h>
52 
53 #include <dev/usb/usb.h>
54 #include <dev/usb/usbdi.h>
55 
56 #include <dev/usb/usb_core.h>
57 #include <dev/usb/usb_busdma.h>
58 #include <dev/usb/usb_process.h>
59 #include <dev/usb/usb_util.h>
60 
61 #include <dev/usb/usb_controller.h>
62 #include <dev/usb/usb_bus.h>
63 #include <dev/usb/controller/ohci.h>
64 #include <dev/usb/controller/ohcireg.h>
65 
66 #include "ps3bus.h"
67 
68 static int
69 ohci_ps3_probe(device_t dev)
70 {
71 	if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
72 	    ps3bus_get_devtype(dev) != PS3_DEVTYPE_USB)
73 		return (ENXIO);
74 
75 	device_set_desc(dev, "Playstation 3 USB 2.0 controller");
76 	return (BUS_PROBE_SPECIFIC);
77 }
78 
79 static int
80 ohci_ps3_attach(device_t dev)
81 {
82 	ohci_softc_t *sc = device_get_softc(dev);
83 	int rid, err;
84 
85 	sc->sc_bus.parent = dev;
86 	sc->sc_bus.devices = sc->sc_devices;
87 	sc->sc_bus.devices_max = OHCI_MAX_DEVICES;
88 	sc->sc_bus.dma_bits = 32;
89 
90 	/* get all DMA memory */
91 	if (usb_bus_mem_alloc_all(&sc->sc_bus,
92 	    USB_GET_DMA_TAG(dev), &ohci_iterate_hw_softc))
93 		return (ENOMEM);
94 
95 	rid = 0;
96 	sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
97 	    &rid, RF_ACTIVE);
98 
99 	if (!sc->sc_io_res) {
100 		device_printf(dev, "Could not map memory\n");
101 		goto error;
102 	}
103 
104 	sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
105 	sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
106 	sc->sc_io_size = rman_get_size(sc->sc_io_res);
107 
108 	rid = 0;
109 	sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
110 	    RF_SHAREABLE | RF_ACTIVE);
111 
112 	if (sc->sc_irq_res == NULL) {
113 		device_printf(dev, "Could not allocate irq\n");
114 		return (ENXIO);
115 	}
116 
117 	sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
118 	if (!sc->sc_bus.bdev) {
119 		device_printf(dev, "Could not add USB device\n");
120 		return (ENXIO);
121 	}
122 
123 	device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
124 
125 	sprintf(sc->sc_vendor, "Sony");
126 
127 	err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
128 	    NULL, (driver_intr_t *)ohci_interrupt, sc, &sc->sc_intr_hdl);
129 	if (err) {
130 		device_printf(dev, "Could not setup error irq, %d\n", err);
131 		goto error;
132 	}
133 
134 	//sc->sc_flags |= EHCI_SCFLG_BIGEMMIO;
135 	bus_space_write_4(sc->sc_io_tag, sc->sc_io_hdl,
136 	    OHCI_CONTROL, 0);
137 	err = ohci_init(sc);
138 	if (err) {
139 		device_printf(dev, "USB init failed err=%d\n", err);
140 		goto error;
141 	}
142 
143 	err = device_probe_and_attach(sc->sc_bus.bdev);
144 	if (err == 0)
145 		return (0);
146 
147 error:
148 	return (ENXIO);
149 }
150 
151 static device_method_t ohci_ps3_methods[] = {
152 	/* Device interface */
153 	DEVMETHOD(device_probe, ohci_ps3_probe),
154 	DEVMETHOD(device_attach, ohci_ps3_attach),
155 	DEVMETHOD(device_resume, bus_generic_resume),
156 	DEVMETHOD(device_suspend, bus_generic_suspend),
157 	DEVMETHOD(device_shutdown, bus_generic_shutdown),
158 
159 	DEVMETHOD_END
160 };
161 
162 static driver_t ohci_ps3_driver = {
163 	.name = "ohci",
164 	.methods = ohci_ps3_methods,
165 	.size = sizeof(ohci_softc_t),
166 };
167 
168 static devclass_t ohci_ps3_devclass;
169 
170 DRIVER_MODULE(ohci_ps3, ps3bus, ohci_ps3_driver, ohci_ps3_devclass, 0, 0);
171 MODULE_DEPEND(ohci_ps3, usb, 1, 1, 1);
172 
173