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