ehci_ps3.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*-
  2. * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
  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. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  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. #include <sys/rman.h>
  50. #include <dev/usb/usb.h>
  51. #include <dev/usb/usbdi.h>
  52. #include <dev/usb/usb_core.h>
  53. #include <dev/usb/usb_busdma.h>
  54. #include <dev/usb/usb_process.h>
  55. #include <dev/usb/usb_util.h>
  56. #include <dev/usb/usb_controller.h>
  57. #include <dev/usb/usb_bus.h>
  58. #include <dev/usb/controller/ehci.h>
  59. #include <dev/usb/controller/ehcireg.h>
  60. #include "ps3bus.h"
  61. struct ps3_ehci_softc {
  62. ehci_softc_t base;
  63. struct bus_space tag;
  64. };
  65. static void
  66. ehci_ps3_post_reset(struct ehci_softc *ehci_softc)
  67. {
  68. uint32_t usbmode;
  69. /* Select big-endian mode */
  70. usbmode = EOREAD4(ehci_softc, EHCI_USBMODE_NOLPM);
  71. usbmode |= EHCI_UM_ES_BE;
  72. EOWRITE4(ehci_softc, EHCI_USBMODE_NOLPM, usbmode);
  73. }
  74. static int
  75. ehci_ps3_probe(device_t dev)
  76. {
  77. if (ps3bus_get_bustype(dev) != PS3_BUSTYPE_SYSBUS ||
  78. ps3bus_get_devtype(dev) != PS3_DEVTYPE_USB)
  79. return (ENXIO);
  80. device_set_desc(dev, "Playstation 3 USB 2.0 controller");
  81. return (BUS_PROBE_SPECIFIC);
  82. }
  83. static int
  84. ehci_ps3_attach(device_t dev)
  85. {
  86. ehci_softc_t *sc = device_get_softc(dev);
  87. int rid, err;
  88. sc->sc_bus.parent = dev;
  89. sc->sc_bus.devices = sc->sc_devices;
  90. sc->sc_bus.devices_max = EHCI_MAX_DEVICES;
  91. sc->sc_bus.dma_bits = 32;
  92. /* get all DMA memory */
  93. if (usb_bus_mem_alloc_all(&sc->sc_bus,
  94. USB_GET_DMA_TAG(dev), &ehci_iterate_hw_softc))
  95. return (ENOMEM);
  96. rid = 1;
  97. sc->sc_io_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
  98. &rid, RF_ACTIVE);
  99. if (!sc->sc_io_res) {
  100. device_printf(dev, "Could not map memory\n");
  101. goto error;
  102. }
  103. sc->sc_io_tag = rman_get_bustag(sc->sc_io_res);
  104. sc->sc_io_hdl = rman_get_bushandle(sc->sc_io_res);
  105. sc->sc_io_size = rman_get_size(sc->sc_io_res);
  106. rid = 1;
  107. sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
  108. RF_SHAREABLE | RF_ACTIVE);
  109. if (sc->sc_irq_res == NULL) {
  110. device_printf(dev, "Could not allocate irq\n");
  111. return (ENXIO);
  112. }
  113. sc->sc_bus.bdev = device_add_child(dev, "usbus", -1);
  114. if (!sc->sc_bus.bdev) {
  115. device_printf(dev, "Could not add USB device\n");
  116. return (ENXIO);
  117. }
  118. device_set_ivars(sc->sc_bus.bdev, &sc->sc_bus);
  119. sprintf(sc->sc_vendor, "Sony");
  120. err = bus_setup_intr(dev, sc->sc_irq_res, INTR_TYPE_BIO | INTR_MPSAFE,
  121. NULL, (driver_intr_t *)ehci_interrupt, sc, &sc->sc_intr_hdl);
  122. if (err) {
  123. device_printf(dev, "Could not setup error irq, %d\n", err);
  124. goto error;
  125. }
  126. sc->sc_vendor_post_reset = ehci_ps3_post_reset;
  127. err = ehci_init(sc);
  128. if (err) {
  129. device_printf(dev, "USB init failed err=%d\n", err);
  130. goto error;
  131. }
  132. err = device_probe_and_attach(sc->sc_bus.bdev);
  133. if (err == 0)
  134. return (0);
  135. error:
  136. return (ENXIO);
  137. }
  138. static device_method_t ehci_ps3_methods[] = {
  139. /* Device interface */
  140. DEVMETHOD(device_probe, ehci_ps3_probe),
  141. DEVMETHOD(device_attach, ehci_ps3_attach),
  142. DEVMETHOD(device_resume, bus_generic_resume),
  143. DEVMETHOD(device_suspend, bus_generic_suspend),
  144. DEVMETHOD(device_shutdown, bus_generic_shutdown),
  145. DEVMETHOD_END
  146. };
  147. static driver_t ehci_ps3_driver = {
  148. .name = "ehci",
  149. .methods = ehci_ps3_methods,
  150. .size = sizeof(ehci_softc_t),
  151. };
  152. static devclass_t ehci_ps3_devclass;
  153. DRIVER_MODULE(ehci_ps3, ps3bus, ehci_ps3_driver, ehci_ps3_devclass, 0, 0);
  154. MODULE_DEPEND(ehci_ps3, usb, 1, 1, 1);