if_atw_cardbus.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* $OpenBSD: if_atw_cardbus.c,v 1.23 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: if_atw_cardbus.c,v 1.9 2004/07/23 07:07:55 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 1999, 2000, 2003 The NetBSD Foundation, Inc.
  5. * All rights reserved.
  6. *
  7. * This code is derived from software contributed to The NetBSD Foundation
  8. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  9. * NASA Ames Research Center. This code was adapted for the ADMtek ADM8211
  10. * by David Young.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  22. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  23. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  25. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. * POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. * CardBus bus front-end for the ADMtek ADM8211 802.11 MAC/BBP driver.
  35. */
  36. #include "bpfilter.h"
  37. #include <sys/param.h>
  38. #include <sys/systm.h>
  39. #include <sys/mbuf.h>
  40. #include <sys/malloc.h>
  41. #include <sys/kernel.h>
  42. #include <sys/socket.h>
  43. #include <sys/ioctl.h>
  44. #include <sys/errno.h>
  45. #include <sys/device.h>
  46. #include <sys/endian.h>
  47. #include <net/if.h>
  48. #include <net/if_dl.h>
  49. #include <net/if_media.h>
  50. #include <netinet/in.h>
  51. #include <netinet/if_ether.h>
  52. #include <net80211/ieee80211_radiotap.h>
  53. #include <net80211/ieee80211_var.h>
  54. #if NBPFILTER > 0
  55. #include <net/bpf.h>
  56. #endif
  57. #include <machine/bus.h>
  58. #include <machine/intr.h>
  59. #include <dev/ic/atwreg.h>
  60. #include <dev/ic/si4136reg.h>
  61. #include <dev/ic/atwvar.h>
  62. #include <dev/pci/pcivar.h>
  63. #include <dev/pci/pcireg.h>
  64. #include <dev/pci/pcidevs.h>
  65. #include <dev/cardbus/cardbusvar.h>
  66. /*
  67. * PCI configuration space registers used by the ADM8211.
  68. */
  69. #define ATW_PCI_IOBA 0x10 /* i/o mapped base */
  70. #define ATW_PCI_MMBA 0x14 /* memory mapped base */
  71. struct atw_cardbus_softc {
  72. struct atw_softc sc_atw; /* real ADM8211 softc */
  73. /* CardBus-specific goo. */
  74. void *sc_ih; /* interrupt handle */
  75. cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
  76. pcitag_t sc_tag; /* our CardBus tag */
  77. int sc_csr; /* CSR bits */
  78. bus_size_t sc_mapsize; /* the size of mapped bus space
  79. region */
  80. int sc_cben; /* CardBus enables */
  81. int sc_bar_reg; /* which BAR to use */
  82. pcireg_t sc_bar_val; /* value of the BAR */
  83. int sc_intrline; /* interrupt line */
  84. pci_chipset_tag_t sc_pc;
  85. };
  86. int atw_cardbus_match(struct device *, void *, void *);
  87. void atw_cardbus_attach(struct device *, struct device *, void *);
  88. int atw_cardbus_detach(struct device *, int);
  89. struct cfattach atw_cardbus_ca = {
  90. sizeof(struct atw_cardbus_softc), atw_cardbus_match, atw_cardbus_attach,
  91. atw_cardbus_detach
  92. };
  93. void atw_cardbus_setup(struct atw_cardbus_softc *);
  94. int atw_cardbus_enable(struct atw_softc *);
  95. void atw_cardbus_disable(struct atw_softc *);
  96. void atw_cardbus_power(struct atw_softc *, int);
  97. const struct pci_matchid atw_cardbus_devices[] = {
  98. { PCI_VENDOR_ADMTEK, PCI_PRODUCT_ADMTEK_ADM8211 },
  99. { PCI_VENDOR_3COM, PCI_PRODUCT_3COM_3CRSHPW796 },
  100. };
  101. int
  102. atw_cardbus_match(struct device *parent, void *match, void *aux)
  103. {
  104. return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
  105. atw_cardbus_devices, nitems(atw_cardbus_devices)));
  106. }
  107. void
  108. atw_cardbus_attach(struct device *parent, struct device *self, void *aux)
  109. {
  110. struct atw_cardbus_softc *csc = (void *)self;
  111. struct atw_softc *sc = &csc->sc_atw;
  112. struct cardbus_attach_args *ca = aux;
  113. cardbus_devfunc_t ct = ca->ca_ct;
  114. bus_addr_t adr;
  115. sc->sc_dmat = ca->ca_dmat;
  116. csc->sc_ct = ct;
  117. csc->sc_tag = ca->ca_tag;
  118. csc->sc_pc = ca->ca_pc;
  119. /*
  120. * Power management hooks.
  121. */
  122. sc->sc_enable = atw_cardbus_enable;
  123. sc->sc_disable = atw_cardbus_disable;
  124. sc->sc_power = atw_cardbus_power;
  125. /* Get revision info. */
  126. sc->sc_rev = PCI_REVISION(ca->ca_class);
  127. #if 0
  128. printf(": signature %08x\n%s",
  129. pci_conf_read(ca->ca_pc, csc->sc_tag, 0x80),
  130. sc->sc_dev.dv_xname);
  131. #endif
  132. /*
  133. * Map the device.
  134. */
  135. csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
  136. if (Cardbus_mapreg_map(ct, ATW_PCI_MMBA,
  137. PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &adr,
  138. &csc->sc_mapsize) == 0) {
  139. #if 0
  140. printf(": atw_cardbus_attach mapped %d bytes mem space\n%s",
  141. csc->sc_mapsize, sc->sc_dev.dv_xname);
  142. #endif
  143. csc->sc_cben = CARDBUS_MEM_ENABLE;
  144. csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
  145. csc->sc_bar_reg = ATW_PCI_MMBA;
  146. csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
  147. } else if (Cardbus_mapreg_map(ct, ATW_PCI_IOBA,
  148. PCI_MAPREG_TYPE_IO, 0, &sc->sc_st, &sc->sc_sh, &adr,
  149. &csc->sc_mapsize) == 0) {
  150. #if 0
  151. printf(": atw_cardbus_attach mapped %d bytes I/O space\n%s",
  152. csc->sc_mapsize, sc->sc_dev.dv_xname);
  153. #endif
  154. csc->sc_cben = CARDBUS_IO_ENABLE;
  155. csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
  156. csc->sc_bar_reg = ATW_PCI_IOBA;
  157. csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
  158. } else {
  159. printf(": unable to map device registers\n");
  160. return;
  161. }
  162. /*
  163. * Bring the chip out of powersave mode and initialize the
  164. * configuration registers.
  165. */
  166. atw_cardbus_setup(csc);
  167. /* Remember which interrupt line. */
  168. csc->sc_intrline = ca->ca_intrline;
  169. printf(": revision %d.%d: irq %d\n",
  170. (sc->sc_rev >> 4) & 0xf, sc->sc_rev & 0xf, csc->sc_intrline);
  171. #if 0
  172. /*
  173. * The CardBus cards will make it to store-and-forward mode as
  174. * soon as you put them under any kind of load, so just start
  175. * out there.
  176. */
  177. sc->sc_txthresh = 3; /* TBD name constant */
  178. #endif
  179. /*
  180. * Finish off the attach.
  181. */
  182. atw_attach(sc);
  183. ATW_WRITE(sc, ATW_FER, ATW_FER_INTR);
  184. /*
  185. * Power down the socket.
  186. */
  187. Cardbus_function_disable(csc->sc_ct);
  188. }
  189. int
  190. atw_cardbus_detach(struct device *self, int flags)
  191. {
  192. struct atw_cardbus_softc *csc = (void *)self;
  193. struct atw_softc *sc = &csc->sc_atw;
  194. struct cardbus_devfunc *ct = csc->sc_ct;
  195. int rv;
  196. #if defined(DIAGNOSTIC)
  197. if (ct == NULL)
  198. panic("%s: data structure lacks", sc->sc_dev.dv_xname);
  199. #endif
  200. rv = atw_detach(sc);
  201. if (rv)
  202. return (rv);
  203. /*
  204. * Unhook the interrupt handler.
  205. */
  206. if (csc->sc_ih != NULL)
  207. cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
  208. /*
  209. * Release bus space and close window.
  210. */
  211. if (csc->sc_bar_reg != 0)
  212. Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
  213. sc->sc_st, sc->sc_sh, csc->sc_mapsize);
  214. return (0);
  215. }
  216. int
  217. atw_cardbus_enable(struct atw_softc *sc)
  218. {
  219. struct atw_cardbus_softc *csc = (void *) sc;
  220. cardbus_devfunc_t ct = csc->sc_ct;
  221. cardbus_chipset_tag_t cc = ct->ct_cc;
  222. cardbus_function_tag_t cf = ct->ct_cf;
  223. /*
  224. * Power on the socket.
  225. */
  226. Cardbus_function_enable(ct);
  227. /*
  228. * Set up the PCI configuration registers.
  229. */
  230. atw_cardbus_setup(csc);
  231. /*
  232. * Map and establish the interrupt.
  233. */
  234. csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
  235. atw_intr, sc, sc->sc_dev.dv_xname);
  236. if (csc->sc_ih == NULL) {
  237. printf("%s: unable to establish interrupt at %d\n",
  238. sc->sc_dev.dv_xname, csc->sc_intrline);
  239. Cardbus_function_disable(csc->sc_ct);
  240. return (1);
  241. }
  242. return (0);
  243. }
  244. void
  245. atw_cardbus_disable(struct atw_softc *sc)
  246. {
  247. struct atw_cardbus_softc *csc = (void *) sc;
  248. cardbus_devfunc_t ct = csc->sc_ct;
  249. cardbus_chipset_tag_t cc = ct->ct_cc;
  250. cardbus_function_tag_t cf = ct->ct_cf;
  251. /* Unhook the interrupt handler. */
  252. cardbus_intr_disestablish(cc, cf, csc->sc_ih);
  253. csc->sc_ih = NULL;
  254. /* Power down the socket. */
  255. Cardbus_function_disable(ct);
  256. }
  257. void
  258. atw_cardbus_power(struct atw_softc *sc, int why)
  259. {
  260. if (why == DVACT_RESUME)
  261. atw_enable(sc);
  262. }
  263. void
  264. atw_cardbus_setup(struct atw_cardbus_softc *csc)
  265. {
  266. #ifdef notyet
  267. struct atw_softc *sc = &csc->sc_atw;
  268. #endif
  269. cardbus_devfunc_t ct = csc->sc_ct;
  270. cardbus_chipset_tag_t cc = ct->ct_cc;
  271. pci_chipset_tag_t pc = csc->sc_pc;
  272. pcireg_t reg;
  273. #ifdef notyet
  274. (void)cardbus_setpowerstate(sc->sc_dev.dv_xname, ct, csc->sc_tag,
  275. PCI_PWR_D0);
  276. #endif
  277. /* Program the BAR. */
  278. pci_conf_write(pc, csc->sc_tag, csc->sc_bar_reg,
  279. csc->sc_bar_val);
  280. /* Make sure the right access type is on the CardBus bridge. */
  281. (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
  282. (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
  283. /* Enable the appropriate bits in the PCI CSR. */
  284. reg = pci_conf_read(pc, csc->sc_tag,
  285. PCI_COMMAND_STATUS_REG);
  286. reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
  287. reg |= csc->sc_csr;
  288. pci_conf_write(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG,
  289. reg);
  290. /*
  291. * Make sure the latency timer is set to some reasonable
  292. * value.
  293. */
  294. reg = pci_conf_read(pc, csc->sc_tag, PCI_BHLC_REG);
  295. if (PCI_LATTIMER(reg) < 0x20) {
  296. reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
  297. reg |= (0x20 << PCI_LATTIMER_SHIFT);
  298. pci_conf_write(pc, csc->sc_tag, PCI_BHLC_REG, reg);
  299. }
  300. }