ohci_cardbus.c 5.9 KB

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