if_xl_cardbus.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* $OpenBSD: if_xl_cardbus.c,v 1.31 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: if_xl_cardbus.c,v 1.13 2000/03/07 00:32:52 mycroft Exp $ */
  3. /*
  4. * CardBus specific routines for 3Com 3C575-family CardBus ethernet adapter
  5. *
  6. * Copyright (c) 1998 and 1999
  7. * HAYAKAWA Koichi. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY HAYAKAWA KOICHI ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL TAKESHI OHASHI OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. *
  31. */
  32. #include "bpfilter.h"
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/mbuf.h>
  36. #include <sys/socket.h>
  37. #include <sys/ioctl.h>
  38. #include <sys/errno.h>
  39. #include <sys/malloc.h>
  40. #include <sys/kernel.h>
  41. #include <sys/device.h>
  42. #include <net/if.h>
  43. #include <net/if_dl.h>
  44. #include <net/if_types.h>
  45. #include <net/if_media.h>
  46. #include <netinet/in.h>
  47. #include <netinet/if_ether.h>
  48. #include <machine/cpu.h>
  49. #include <machine/bus.h>
  50. #include <dev/cardbus/cardbusvar.h>
  51. #include <dev/pci/pcidevs.h>
  52. #include <dev/mii/miivar.h>
  53. #include <dev/ic/xlreg.h>
  54. #if defined XL_DEBUG
  55. #define DPRINTF(a) printf a
  56. #else
  57. #define DPRINTF(a)
  58. #endif
  59. #define CARDBUS_3C575BTX_FUNCSTAT_PCIREG CARDBUS_BASE2_REG /* means 0x18 */
  60. int xl_cardbus_match(struct device *, void *, void *);
  61. void xl_cardbus_attach(struct device *, struct device *,void *);
  62. int xl_cardbus_detach(struct device *, int);
  63. void xl_cardbus_intr_ack(struct xl_softc *);
  64. #define XL_CARDBUS_BOOMERANG 0x0001
  65. #define XL_CARDBUS_CYCLONE 0x0002
  66. #define XL_CARDBUS_INTR 0x0004
  67. #define XL_CARDBUS_INTR_ACK 0x8000
  68. struct xl_cardbus_softc {
  69. struct xl_softc sc_softc;
  70. cardbus_devfunc_t sc_ct;
  71. int sc_intrline;
  72. u_int8_t sc_cardbus_flags;
  73. u_int8_t sc_cardtype;
  74. /* CardBus function status space. 575B requests it. */
  75. bus_space_tag_t sc_funct;
  76. bus_space_handle_t sc_funch;
  77. bus_size_t sc_funcsize;
  78. bus_size_t sc_mapsize; /* size of mapped bus space region */
  79. };
  80. struct cfattach xl_cardbus_ca = {
  81. sizeof(struct xl_cardbus_softc), xl_cardbus_match,
  82. xl_cardbus_attach, xl_cardbus_detach
  83. };
  84. const struct xl_cardbus_product {
  85. u_int32_t ecp_prodid; /* CardBus product ID */
  86. int ecp_flags; /* initial softc flags */
  87. pcireg_t ecp_csr; /* PCI CSR flags */
  88. int ecp_cardtype; /* card type */
  89. } xl_cardbus_products[] = {
  90. { PCI_PRODUCT_3COM_3C575,
  91. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM,
  92. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE,
  93. XL_CARDBUS_BOOMERANG },
  94. { PCI_PRODUCT_3COM_3CCFE575BT,
  95. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM |
  96. XL_FLAG_INVERT_LED_PWR,
  97. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
  98. PCI_COMMAND_MASTER_ENABLE,
  99. XL_CARDBUS_CYCLONE },
  100. { PCI_PRODUCT_3COM_3CCFE575CT,
  101. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM |
  102. XL_FLAG_INVERT_MII_PWR,
  103. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
  104. PCI_COMMAND_MASTER_ENABLE,
  105. XL_CARDBUS_CYCLONE },
  106. { PCI_PRODUCT_3COM_3CCFEM656,
  107. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM |
  108. XL_FLAG_INVERT_LED_PWR | XL_FLAG_INVERT_MII_PWR,
  109. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
  110. PCI_COMMAND_MASTER_ENABLE,
  111. XL_CARDBUS_CYCLONE },
  112. { PCI_PRODUCT_3COM_3CCFEM656B,
  113. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM |
  114. XL_FLAG_INVERT_LED_PWR | XL_FLAG_INVERT_MII_PWR,
  115. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
  116. PCI_COMMAND_MASTER_ENABLE,
  117. XL_CARDBUS_CYCLONE },
  118. { PCI_PRODUCT_3COM_3CCFEM656C,
  119. XL_FLAG_PHYOK | XL_FLAG_EEPROM_OFFSET_30 | XL_FLAG_8BITROM |
  120. XL_FLAG_INVERT_MII_PWR,
  121. PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MEM_ENABLE |
  122. PCI_COMMAND_MASTER_ENABLE,
  123. XL_CARDBUS_CYCLONE },
  124. { 0,
  125. 0,
  126. 0,
  127. 0 },
  128. };
  129. const struct xl_cardbus_product *xl_cardbus_lookup(const struct cardbus_attach_args *);
  130. const struct xl_cardbus_product *
  131. xl_cardbus_lookup(const struct cardbus_attach_args *ca)
  132. {
  133. const struct xl_cardbus_product *ecp;
  134. if (PCI_VENDOR(ca->ca_id) != PCI_VENDOR_3COM)
  135. return (NULL);
  136. for (ecp = xl_cardbus_products; ecp->ecp_prodid != 0; ecp++)
  137. if (PCI_PRODUCT(ca->ca_id) == ecp->ecp_prodid)
  138. return (ecp);
  139. return (NULL);
  140. }
  141. int
  142. xl_cardbus_match(struct device *parent, void *match, void *aux)
  143. {
  144. struct cardbus_attach_args *ca = aux;
  145. if (xl_cardbus_lookup(ca) != NULL)
  146. return (1);
  147. return (0);
  148. }
  149. void
  150. xl_cardbus_attach(struct device *parent, struct device *self, void *aux)
  151. {
  152. struct xl_cardbus_softc *csc = (void *)self;
  153. struct xl_softc *sc = &csc->sc_softc;
  154. struct cardbus_attach_args *ca = aux;
  155. cardbus_devfunc_t ct = ca->ca_ct;
  156. cardbus_chipset_tag_t cc = ct->ct_cc;
  157. cardbus_function_tag_t cf = ct->ct_cf;
  158. pcireg_t command, bhlc;
  159. const struct xl_cardbus_product *ecp;
  160. bus_space_handle_t ioh;
  161. bus_addr_t adr;
  162. if (Cardbus_mapreg_map(ct, CARDBUS_BASE0_REG, PCI_MAPREG_TYPE_IO, 0,
  163. &sc->xl_btag, &ioh, &adr, &csc->sc_mapsize)) {
  164. printf(": can't map i/o space\n");
  165. return;
  166. }
  167. ecp = xl_cardbus_lookup(ca);
  168. if (ecp == NULL) {
  169. printf("\n");
  170. panic("xl_cardbus_attach: impossible");
  171. }
  172. sc->xl_flags = ecp->ecp_flags;
  173. sc->sc_dmat = ca->ca_dmat;
  174. sc->xl_bhandle = ioh;
  175. (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_IO_ENABLE);
  176. command = pci_conf_read(ca->ca_pc, ca->ca_tag,
  177. PCI_COMMAND_STATUS_REG);
  178. command |= ecp->ecp_csr;
  179. csc->sc_cardtype = ecp->ecp_cardtype;
  180. if (csc->sc_cardtype == XL_CARDBUS_CYCLONE) {
  181. /* map CardBus function status window */
  182. if (Cardbus_mapreg_map(ct, CARDBUS_BASE2_REG,
  183. PCI_MAPREG_TYPE_MEM, 0, &csc->sc_funct,
  184. &csc->sc_funch, 0, &csc->sc_funcsize)) {
  185. printf("%s: unable to map function status window\n",
  186. self->dv_xname);
  187. return;
  188. }
  189. /*
  190. * Make sure CardBus bridge can access memory space. Usually
  191. * memory access is enabled by BIOS, but some BIOSes do not
  192. * enable it.
  193. */
  194. (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_MEM_ENABLE);
  195. }
  196. (ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
  197. pci_conf_write(ca->ca_pc, ca->ca_tag, PCI_COMMAND_STATUS_REG,
  198. command);
  199. /*
  200. * set latency timer
  201. */
  202. bhlc = pci_conf_read(ca->ca_pc, ca->ca_tag, PCI_BHLC_REG);
  203. if (PCI_LATTIMER(bhlc) < 0x20) {
  204. /* at least the value of latency timer should 0x20. */
  205. DPRINTF(("if_xl_cardbus: lattimer 0x%x -> 0x20\n",
  206. PCI_LATTIMER(bhlc)));
  207. bhlc &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
  208. bhlc |= (0x20 << PCI_LATTIMER_SHIFT);
  209. pci_conf_write(ca->ca_pc, ca->ca_tag, PCI_BHLC_REG, bhlc);
  210. }
  211. csc->sc_ct = ca->ca_ct;
  212. csc->sc_intrline = ca->ca_intrline;
  213. /* Map and establish the interrupt. */
  214. sc->xl_intrhand = cardbus_intr_establish(cc, cf, ca->ca_intrline,
  215. IPL_NET, xl_intr, csc, self->dv_xname);
  216. if (sc->xl_intrhand == NULL) {
  217. printf(": couldn't establish interrupt");
  218. printf(" at %d", ca->ca_intrline);
  219. printf("\n");
  220. return;
  221. }
  222. printf(": irq %d", ca->ca_intrline);
  223. sc->intr_ack = xl_cardbus_intr_ack;
  224. xl_attach(sc);
  225. if (csc->sc_cardtype == XL_CARDBUS_CYCLONE)
  226. bus_space_write_4(csc->sc_funct, csc->sc_funch,
  227. XL_CARDBUS_INTR, XL_CARDBUS_INTR_ACK);
  228. }
  229. int
  230. xl_cardbus_detach(struct device *self, int arg)
  231. {
  232. struct xl_cardbus_softc *csc = (void *)self;
  233. struct xl_softc *sc = &csc->sc_softc;
  234. struct cardbus_devfunc *ct = csc->sc_ct;
  235. cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf,
  236. sc->xl_intrhand);
  237. xl_detach(sc);
  238. if (csc->sc_cardtype == XL_CARDBUS_CYCLONE)
  239. Cardbus_mapreg_unmap(ct, CARDBUS_BASE2_REG,
  240. csc->sc_funct, csc->sc_funch, csc->sc_funcsize);
  241. Cardbus_mapreg_unmap(ct, CARDBUS_BASE0_REG, sc->xl_btag,
  242. sc->xl_bhandle, csc->sc_mapsize);
  243. return (0);
  244. }
  245. void
  246. xl_cardbus_intr_ack(struct xl_softc *sc)
  247. {
  248. struct xl_cardbus_softc *csc = (struct xl_cardbus_softc *)sc;
  249. bus_space_write_4(csc->sc_funct, csc->sc_funch, XL_CARDBUS_INTR,
  250. XL_CARDBUS_INTR_ACK);
  251. }