uhci_cardbus.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* $OpenBSD: uhci_cardbus.c,v 1.15 2015/03/14 03:38:47 jsg Exp $ */
  2. /*
  3. * Copyright (c) 1998 The NetBSD Foundation, Inc.
  4. * All rights reserved.
  5. *
  6. * This code is derived from software contributed to The NetBSD Foundation
  7. * by Lennart Augustsson (lennart@augustsson.net) at
  8. * Carlstedt Research & Technology.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  21. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  22. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  23. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. * POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include <sys/param.h>
  32. #include <sys/systm.h>
  33. #include <sys/kernel.h>
  34. #include <sys/device.h>
  35. #include <machine/bus.h>
  36. #include <dev/cardbus/cardbusvar.h>
  37. #include <dev/usb/usb.h>
  38. #include <dev/usb/usbdi.h>
  39. #include <dev/usb/usbdivar.h>
  40. #include <dev/usb/usb_mem.h>
  41. #include <dev/usb/uhcireg.h>
  42. #include <dev/usb/uhcivar.h>
  43. int uhci_cardbus_match(struct device *, void *, void *);
  44. void uhci_cardbus_attach(struct device *, struct device *, void *);
  45. int uhci_cardbus_detach(struct device *, int);
  46. struct uhci_cardbus_softc {
  47. struct uhci_softc sc;
  48. cardbus_chipset_tag_t sc_cc;
  49. cardbus_function_tag_t sc_cf;
  50. cardbus_devfunc_t sc_ct;
  51. void *sc_ih; /* interrupt vectoring */
  52. };
  53. struct cfattach uhci_cardbus_ca = {
  54. sizeof(struct uhci_cardbus_softc), uhci_cardbus_match,
  55. uhci_cardbus_attach, uhci_cardbus_detach, uhci_activate
  56. };
  57. #define cardbus_findvendor pci_findvendor
  58. int
  59. uhci_cardbus_match(struct device *parent, void *match, void *aux)
  60. {
  61. struct cardbus_attach_args *ca = (struct cardbus_attach_args *)aux;
  62. if (PCI_CLASS(ca->ca_class) == PCI_CLASS_SERIALBUS &&
  63. PCI_SUBCLASS(ca->ca_class) == PCI_SUBCLASS_SERIALBUS_USB &&
  64. PCI_INTERFACE(ca->ca_class) == PCI_INTERFACE_UHCI)
  65. return (1);
  66. return (0);
  67. }
  68. void
  69. uhci_cardbus_attach(struct device *parent, struct device *self, void *aux)
  70. {
  71. struct uhci_cardbus_softc *sc = (struct uhci_cardbus_softc *)self;
  72. struct cardbus_attach_args *ca = aux;
  73. cardbus_devfunc_t ct = ca->ca_ct;
  74. cardbus_chipset_tag_t cc = ct->ct_cc;
  75. pci_chipset_tag_t pc = ca->ca_pc;
  76. cardbus_function_tag_t cf = ct->ct_cf;
  77. pcireg_t csr;
  78. usbd_status r;
  79. const char *vendor;
  80. const char *devname = sc->sc.sc_bus.bdev.dv_xname;
  81. /* Map I/O registers */
  82. if (Cardbus_mapreg_map(ct, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
  83. &sc->sc.iot, &sc->sc.ioh, NULL, &sc->sc.sc_size)) {
  84. printf(": can't map io space\n");
  85. return;
  86. }
  87. /* Disable interrupts, so we don't get any spurious ones. */
  88. bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
  89. sc->sc_cc = cc;
  90. sc->sc_cf = cf;
  91. sc->sc_ct = ct;
  92. sc->sc.sc_bus.dmatag = ca->ca_dmat;
  93. (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
  94. (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
  95. /* Enable the device. */
  96. csr = pci_conf_read(pc, ca->ca_tag,
  97. PCI_COMMAND_STATUS_REG);
  98. pci_conf_write(pc, ca->ca_tag, PCI_COMMAND_STATUS_REG,
  99. csr | PCI_COMMAND_MASTER_ENABLE
  100. | PCI_COMMAND_IO_ENABLE);
  101. sc->sc_ih = cardbus_intr_establish(cc, cf, ca->ca_intrline,
  102. IPL_USB, uhci_intr, sc, devname);
  103. if (sc->sc_ih == NULL) {
  104. printf(": couldn't establish interrupt\n");
  105. return;
  106. }
  107. printf(": irq %d\n", ca->ca_intrline);
  108. /* Set LEGSUP register to its default value. */
  109. pci_conf_write(pc, ca->ca_tag, PCI_LEGSUP,
  110. PCI_LEGSUP_USBPIRQDEN);
  111. switch(pci_conf_read(pc, ca->ca_tag, PCI_USBREV) & PCI_USBREV_MASK) {
  112. case PCI_USBREV_PRE_1_0:
  113. sc->sc.sc_bus.usbrev = USBREV_PRE_1_0;
  114. break;
  115. case PCI_USBREV_1_0:
  116. sc->sc.sc_bus.usbrev = USBREV_1_0;
  117. break;
  118. case PCI_USBREV_1_1:
  119. sc->sc.sc_bus.usbrev = USBREV_1_1;
  120. break;
  121. default:
  122. sc->sc.sc_bus.usbrev = USBREV_UNKNOWN;
  123. break;
  124. }
  125. uhci_run(&sc->sc, 0); /* stop the controller */
  126. /* disable interrupts */
  127. bus_space_barrier(sc->sc.iot, sc->sc.ioh, 0, sc->sc.sc_size,
  128. BUS_SPACE_BARRIER_READ|BUS_SPACE_BARRIER_WRITE);
  129. bus_space_write_2(sc->sc.iot, sc->sc.ioh, UHCI_INTR, 0);
  130. /* Figure out vendor for root hub descriptor. */
  131. vendor = cardbus_findvendor(ca->ca_id);
  132. sc->sc.sc_id_vendor = PCI_VENDOR(ca->ca_id);
  133. if (vendor)
  134. strlcpy(sc->sc.sc_vendor, vendor, sizeof (sc->sc.sc_vendor));
  135. else
  136. snprintf(sc->sc.sc_vendor, sizeof(sc->sc.sc_vendor),
  137. "vendor 0x%04x", PCI_VENDOR(ca->ca_id));
  138. r = uhci_init(&sc->sc);
  139. if (r != USBD_NORMAL_COMPLETION) {
  140. printf("%s: init failed, error=%d\n", devname, r);
  141. bus_space_unmap(sc->sc.iot, sc->sc.ioh, sc->sc.sc_size);
  142. return;
  143. }
  144. /* Attach usb device. */
  145. config_found(self, &sc->sc.sc_bus, usbctlprint);
  146. }
  147. int
  148. uhci_cardbus_detach(struct device *self, int flags)
  149. {
  150. struct uhci_cardbus_softc *sc = (struct uhci_cardbus_softc *)self;
  151. struct cardbus_devfunc *ct = sc->sc_ct;
  152. int rv;
  153. rv = uhci_detach(self, flags);
  154. if (rv)
  155. return (rv);
  156. if (sc->sc_ih != NULL) {
  157. cardbus_intr_disestablish(sc->sc_cc, sc->sc_cf, sc->sc_ih);
  158. sc->sc_ih = NULL;
  159. }
  160. if (sc->sc.sc_size) {
  161. Cardbus_mapreg_unmap(ct, PCI_CBIO, sc->sc.iot,
  162. sc->sc.ioh, sc->sc.sc_size);
  163. sc->sc.sc_size = 0;
  164. }
  165. return (0);
  166. }