cardbus_map.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* $OpenBSD: cardbus_map.c,v 1.15 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: cardbus_map.c,v 1.10 2000/03/07 00:31:46 mycroft Exp $ */
  3. /*
  4. * Copyright (c) 1999 and 2000
  5. * HAYAKAWA Koichi. 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. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
  21. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  26. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <sys/param.h>
  30. #include <sys/systm.h>
  31. #include <machine/bus.h>
  32. #include <dev/cardbus/cardbusvar.h>
  33. #include <dev/pci/pcireg.h> /* XXX */
  34. #if defined DEBUG && !defined CARDBUS_MAP_DEBUG
  35. #define CARDBUS_MAP_DEBUG
  36. #endif
  37. #if defined CARDBUS_MAP_DEBUG
  38. #define STATIC
  39. #define DPRINTF(a) printf a
  40. #else
  41. #ifdef DDB
  42. #define STATIC
  43. #else
  44. #define STATIC static
  45. #endif
  46. #define DPRINTF(a)
  47. #endif
  48. /*
  49. * int cardbus_mapreg_map(struct cardbus_softc *, int, int, pcireg_t,
  50. * int bus_space_tag_t *, bus_space_handle_t *,
  51. * bus_addr_t *, bus_size_t *)
  52. * This function maps bus-space on the value of Base Address
  53. * Register (BAR) indexed by the argument `reg' (the second argument).
  54. * When the value of the BAR is not valid, such as 0x00000000, a new
  55. * address should be allocated for the BAR and new address values is
  56. * written on the BAR.
  57. */
  58. int
  59. cardbus_mapreg_map(struct cardbus_softc *sc, int func, int reg,
  60. pcireg_t type, int busflags, bus_space_tag_t *tagp,
  61. bus_space_handle_t *handlep, bus_addr_t *basep, bus_size_t *sizep)
  62. {
  63. cardbus_chipset_tag_t cc = sc->sc_cc;
  64. pci_chipset_tag_t pc = sc->sc_pc;
  65. cardbus_function_tag_t cf = sc->sc_cf;
  66. bus_space_tag_t bustag;
  67. rbus_tag_t rbustag;
  68. bus_space_handle_t handle;
  69. bus_addr_t base;
  70. bus_size_t size;
  71. int flags;
  72. int status = 0;
  73. pcitag_t tag = pci_make_tag(pc, sc->sc_bus,
  74. sc->sc_device, func);
  75. DPRINTF(("cardbus_mapreg_map called: %s %x\n", sc->sc_dev.dv_xname,
  76. type));
  77. if (pci_mapreg_info(pc, tag, reg, type, &base, &size, &flags))
  78. status = 1;
  79. if (PCI_MAPREG_TYPE(type) == PCI_MAPREG_TYPE_IO) {
  80. bustag = sc->sc_iot;
  81. rbustag = sc->sc_rbus_iot;
  82. } else {
  83. bustag = sc->sc_memt;
  84. rbustag = sc->sc_rbus_memt;
  85. }
  86. if (status == 0) {
  87. bus_addr_t mask = size - 1;
  88. if (base != 0)
  89. mask = 0xffffffff;
  90. if ((*cf->cardbus_space_alloc)(cc, rbustag, base, size, mask,
  91. size, busflags | flags, &base, &handle)) {
  92. panic("io alloc");
  93. }
  94. }
  95. pci_conf_write(pc, tag, reg, base);
  96. DPRINTF(("cardbus_mapreg_map: physaddr %lx\n", (unsigned long)base));
  97. if (tagp != 0)
  98. *tagp = bustag;
  99. if (handlep != 0)
  100. *handlep = handle;
  101. if (basep != 0)
  102. *basep = base;
  103. if (sizep != 0)
  104. *sizep = size;
  105. return (0);
  106. }
  107. /*
  108. * int cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
  109. * bus_space_tag_t tag, bus_space_handle_t handle,
  110. * bus_size_t size)
  111. *
  112. * This function releases bus-space region and close memory or io
  113. * window on the bridge.
  114. *
  115. * Arguments:
  116. * struct cardbus_softc *sc; the pointer to the device structure of cardbus.
  117. * int func; the number of function on the device.
  118. * int reg; the offset of BAR register.
  119. */
  120. int
  121. cardbus_mapreg_unmap(struct cardbus_softc *sc, int func, int reg,
  122. bus_space_tag_t tag, bus_space_handle_t handle, bus_size_t size)
  123. {
  124. cardbus_chipset_tag_t cc = sc->sc_cc;
  125. pci_chipset_tag_t pc = sc->sc_pc;
  126. cardbus_function_tag_t cf = sc->sc_cf;
  127. int st = 1;
  128. pcitag_t cardbustag;
  129. rbus_tag_t rbustag;
  130. if (sc->sc_iot == tag) {
  131. /* bus space is io space */
  132. DPRINTF(("%s: unmap i/o space\n", sc->sc_dev.dv_xname));
  133. rbustag = sc->sc_rbus_iot;
  134. } else if (sc->sc_memt == tag) {
  135. /* bus space is memory space */
  136. DPRINTF(("%s: unmap mem space\n", sc->sc_dev.dv_xname));
  137. rbustag = sc->sc_rbus_memt;
  138. } else
  139. return (1);
  140. cardbustag = pci_make_tag(pc, sc->sc_bus, sc->sc_device, func);
  141. pci_conf_write(pc, cardbustag, reg, 0);
  142. (*cf->cardbus_space_free)(cc, rbustag, handle, size);
  143. return (st);
  144. }