if_rtw_cardbus.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /* $OpenBSD: if_rtw_cardbus.c,v 1.25 2015/03/14 03:38:47 jsg Exp $ */
  2. /* $NetBSD: if_rtw_cardbus.c,v 1.4 2004/12/20 21:05:34 dyoung Exp $ */
  3. /*-
  4. * Copyright (c) 2004, 2005 David Young. All rights reserved.
  5. *
  6. * Adapted for the RTL8180 by David Young.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The name of David Young may not be used to endorse or promote
  17. * products derived from this software without specific prior
  18. * written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY David Young ``AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  23. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David
  24. * Young BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  26. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  28. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. * OF SUCH DAMAGE.
  32. */
  33. /*-
  34. * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
  35. * All rights reserved.
  36. *
  37. * This code is derived from software contributed to The NetBSD Foundation
  38. * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
  39. * NASA Ames Research Center.
  40. *
  41. * Redistribution and use in source and binary forms, with or without
  42. * modification, are permitted provided that the following conditions
  43. * are met:
  44. * 1. Redistributions of source code must retain the above copyright
  45. * notice, this list of conditions and the following disclaimer.
  46. * 2. Redistributions in binary form must reproduce the above copyright
  47. * notice, this list of conditions and the following disclaimer in the
  48. * documentation and/or other materials provided with the distribution.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
  51. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  52. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  53. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
  54. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  55. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  56. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  57. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  58. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  59. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  60. * POSSIBILITY OF SUCH DAMAGE.
  61. */
  62. /*
  63. * Cardbus front-end for the Realtek RTL8180 802.11 MAC/BBP driver.
  64. *
  65. * TBD factor with atw, tlp Cardbus front-ends?
  66. */
  67. #include "bpfilter.h"
  68. #include <sys/param.h>
  69. #include <sys/systm.h>
  70. #include <sys/mbuf.h>
  71. #include <sys/malloc.h>
  72. #include <sys/kernel.h>
  73. #include <sys/socket.h>
  74. #include <sys/ioctl.h>
  75. #include <sys/errno.h>
  76. #include <sys/device.h>
  77. #include <sys/endian.h>
  78. #include <net/if.h>
  79. #include <net/if_dl.h>
  80. #include <net/if_media.h>
  81. #include <netinet/in.h>
  82. #include <netinet/if_ether.h>
  83. #include <net80211/ieee80211_radiotap.h>
  84. #include <net80211/ieee80211_var.h>
  85. #if NBPFILTER > 0
  86. #include <net/bpf.h>
  87. #endif
  88. #include <machine/bus.h>
  89. #include <dev/ic/rtwreg.h>
  90. #include <dev/ic/rtwvar.h>
  91. #include <dev/pci/pcivar.h>
  92. #include <dev/pci/pcireg.h>
  93. #include <dev/pci/pcidevs.h>
  94. #include <dev/cardbus/cardbusvar.h>
  95. /*
  96. * PCI configuration space registers used by the RTL8180.
  97. */
  98. #define RTW_PCI_IOBA 0x10 /* i/o mapped base */
  99. #define RTW_PCI_MMBA 0x14 /* memory mapped base */
  100. struct rtw_cardbus_softc {
  101. struct rtw_softc sc_rtw; /* real RTL8180 softc */
  102. /* CardBus-specific goo. */
  103. void *sc_ih; /* interrupt handle */
  104. cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
  105. pcitag_t sc_tag; /* our CardBus tag */
  106. pci_chipset_tag_t sc_pc; /* PCI chipset */
  107. int sc_csr; /* CSR bits */
  108. bus_size_t sc_mapsize; /* size of the mapped bus space
  109. * region
  110. */
  111. int sc_cben; /* CardBus enables */
  112. int sc_bar_reg; /* which BAR to use */
  113. pcireg_t sc_bar_val; /* value of the BAR */
  114. int sc_intrline; /* interrupt line */
  115. };
  116. int rtw_cardbus_match(struct device *, void *, void *);
  117. void rtw_cardbus_attach(struct device *, struct device *, void *);
  118. int rtw_cardbus_detach(struct device *, int);
  119. void rtw_cardbus_intr_ack(struct rtw_regs *);
  120. void rtw_cardbus_funcregen(struct rtw_regs *, int);
  121. struct cfattach rtw_cardbus_ca = {
  122. sizeof(struct rtw_cardbus_softc), rtw_cardbus_match, rtw_cardbus_attach,
  123. rtw_cardbus_detach
  124. };
  125. void rtw_cardbus_setup(struct rtw_cardbus_softc *);
  126. int rtw_cardbus_enable(struct rtw_softc *);
  127. void rtw_cardbus_disable(struct rtw_softc *);
  128. void rtw_cardbus_power(struct rtw_softc *, int);
  129. const struct pci_matchid rtw_cardbus_devices[] = {
  130. { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8180 },
  131. #ifdef RTW_DEBUG
  132. { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8185 },
  133. { PCI_VENDOR_BELKIN2, PCI_PRODUCT_BELKIN2_F5D7010 },
  134. #endif
  135. { PCI_VENDOR_BELKIN2, PCI_PRODUCT_BELKIN2_F5D6020V3 },
  136. { PCI_VENDOR_DLINK, PCI_PRODUCT_DLINK_DWL610 }
  137. };
  138. int
  139. rtw_cardbus_match(struct device *parent, void *match, void *aux)
  140. {
  141. return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
  142. rtw_cardbus_devices, nitems(rtw_cardbus_devices)));
  143. }
  144. void
  145. rtw_cardbus_intr_ack(struct rtw_regs *regs)
  146. {
  147. RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
  148. }
  149. void
  150. rtw_cardbus_funcregen(struct rtw_regs *regs, int enable)
  151. {
  152. u_int32_t reg;
  153. rtw_config0123_enable(regs, 1);
  154. reg = RTW_READ(regs, RTW_CONFIG3);
  155. if (enable) {
  156. RTW_WRITE(regs, RTW_CONFIG3, reg | RTW_CONFIG3_FUNCREGEN);
  157. } else {
  158. RTW_WRITE(regs, RTW_CONFIG3, reg & ~RTW_CONFIG3_FUNCREGEN);
  159. }
  160. rtw_config0123_enable(regs, 0);
  161. }
  162. void
  163. rtw_cardbus_attach(struct device *parent, struct device *self, void *aux)
  164. {
  165. struct rtw_cardbus_softc *csc = (void *)self;
  166. struct rtw_softc *sc = &csc->sc_rtw;
  167. struct rtw_regs *regs = &sc->sc_regs;
  168. struct cardbus_attach_args *ca = aux;
  169. cardbus_devfunc_t ct = ca->ca_ct;
  170. bus_addr_t adr;
  171. int rev;
  172. sc->sc_dmat = ca->ca_dmat;
  173. csc->sc_ct = ct;
  174. csc->sc_tag = ca->ca_tag;
  175. csc->sc_pc = ca->ca_pc;
  176. /*
  177. * Power management hooks.
  178. */
  179. sc->sc_enable = rtw_cardbus_enable;
  180. sc->sc_disable = rtw_cardbus_disable;
  181. sc->sc_power = rtw_cardbus_power;
  182. sc->sc_intr_ack = rtw_cardbus_intr_ack;
  183. /* Get revision info. */
  184. rev = PCI_REVISION(ca->ca_class);
  185. RTW_DPRINTF(RTW_DEBUG_ATTACH,
  186. ("%s: pass %d.%d signature %08x\n", sc->sc_dev.dv_xname,
  187. (rev >> 4) & 0xf, rev & 0xf,
  188. pci_conf_read(ca->ca_pc, csc->sc_tag, 0x80)));
  189. /*
  190. * Map the device.
  191. */
  192. csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
  193. if (Cardbus_mapreg_map(ct, RTW_PCI_MMBA,
  194. PCI_MAPREG_TYPE_MEM, 0, &regs->r_bt, &regs->r_bh, &adr,
  195. &csc->sc_mapsize) == 0) {
  196. RTW_DPRINTF(RTW_DEBUG_ATTACH,
  197. ("%s: %s mapped %lu bytes mem space\n",
  198. sc->sc_dev.dv_xname, __func__, (long)csc->sc_mapsize));
  199. csc->sc_cben = CARDBUS_MEM_ENABLE;
  200. csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
  201. csc->sc_bar_reg = RTW_PCI_MMBA;
  202. csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
  203. } else if (Cardbus_mapreg_map(ct, RTW_PCI_IOBA,
  204. PCI_MAPREG_TYPE_IO, 0, &regs->r_bt, &regs->r_bh, &adr,
  205. &csc->sc_mapsize) == 0) {
  206. RTW_DPRINTF(RTW_DEBUG_ATTACH,
  207. ("%s: %s mapped %lu bytes I/O space\n",
  208. sc->sc_dev.dv_xname, __func__, (long)csc->sc_mapsize));
  209. csc->sc_cben = CARDBUS_IO_ENABLE;
  210. csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
  211. csc->sc_bar_reg = RTW_PCI_IOBA;
  212. csc->sc_bar_val = adr | PCI_MAPREG_TYPE_IO;
  213. } else {
  214. printf("%s: unable to map device registers\n",
  215. sc->sc_dev.dv_xname);
  216. return;
  217. }
  218. /*
  219. * Bring the chip out of powersave mode and initialize the
  220. * configuration registers.
  221. */
  222. rtw_cardbus_setup(csc);
  223. /* Remember which interrupt line. */
  224. csc->sc_intrline = ca->ca_intrline;
  225. printf(": irq %d\n", csc->sc_intrline);
  226. /*
  227. * Finish off the attach.
  228. */
  229. rtw_attach(sc);
  230. rtw_cardbus_funcregen(regs, 1);
  231. RTW_WRITE(regs, RTW_FEMR, RTW_FEMR_INTR);
  232. RTW_WRITE(regs, RTW_FER, RTW_FER_INTR);
  233. /*
  234. * Power down the socket.
  235. */
  236. Cardbus_function_disable(csc->sc_ct);
  237. }
  238. int
  239. rtw_cardbus_detach(struct device *self, int flags)
  240. {
  241. struct rtw_cardbus_softc *csc = (void *)self;
  242. struct rtw_softc *sc = &csc->sc_rtw;
  243. struct rtw_regs *regs = &sc->sc_regs;
  244. struct cardbus_devfunc *ct = csc->sc_ct;
  245. int rv;
  246. #if defined(DIAGNOSTIC)
  247. if (ct == NULL)
  248. panic("%s: data structure lacks", sc->sc_dev.dv_xname);
  249. #endif
  250. rv = rtw_detach(sc);
  251. if (rv)
  252. return (rv);
  253. rtw_cardbus_funcregen(regs, 0);
  254. /*
  255. * Unhook the interrupt handler.
  256. */
  257. if (csc->sc_ih != NULL)
  258. cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
  259. /*
  260. * Release bus space and close window.
  261. */
  262. if (csc->sc_bar_reg != 0)
  263. Cardbus_mapreg_unmap(ct, csc->sc_bar_reg,
  264. regs->r_bt, regs->r_bh, csc->sc_mapsize);
  265. return (0);
  266. }
  267. int
  268. rtw_cardbus_enable(struct rtw_softc *sc)
  269. {
  270. struct rtw_cardbus_softc *csc = (void *) sc;
  271. cardbus_devfunc_t ct = csc->sc_ct;
  272. cardbus_chipset_tag_t cc = ct->ct_cc;
  273. cardbus_function_tag_t cf = ct->ct_cf;
  274. /*
  275. * Power on the socket.
  276. */
  277. Cardbus_function_enable(ct);
  278. /*
  279. * Set up the PCI configuration registers.
  280. */
  281. rtw_cardbus_setup(csc);
  282. /*
  283. * Map and establish the interrupt.
  284. */
  285. csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline, IPL_NET,
  286. rtw_intr, sc, sc->sc_dev.dv_xname);
  287. if (csc->sc_ih == NULL) {
  288. printf("%s: unable to establish interrupt at %d\n",
  289. sc->sc_dev.dv_xname, csc->sc_intrline);
  290. Cardbus_function_disable(csc->sc_ct);
  291. return (1);
  292. }
  293. rtw_cardbus_funcregen(&sc->sc_regs, 1);
  294. RTW_WRITE(&sc->sc_regs, RTW_FEMR, RTW_FEMR_INTR);
  295. RTW_WRITE(&sc->sc_regs, RTW_FER, RTW_FER_INTR);
  296. return (0);
  297. }
  298. void
  299. rtw_cardbus_disable(struct rtw_softc *sc)
  300. {
  301. struct rtw_cardbus_softc *csc = (void *) sc;
  302. cardbus_devfunc_t ct = csc->sc_ct;
  303. cardbus_chipset_tag_t cc = ct->ct_cc;
  304. cardbus_function_tag_t cf = ct->ct_cf;
  305. RTW_WRITE(&sc->sc_regs, RTW_FEMR,
  306. RTW_READ(&sc->sc_regs, RTW_FEMR) & ~RTW_FEMR_INTR);
  307. rtw_cardbus_funcregen(&sc->sc_regs, 0);
  308. /* Unhook the interrupt handler. */
  309. cardbus_intr_disestablish(cc, cf, csc->sc_ih);
  310. csc->sc_ih = NULL;
  311. /* Power down the socket. */
  312. Cardbus_function_disable(ct);
  313. }
  314. void
  315. rtw_cardbus_power(struct rtw_softc *sc, int why)
  316. {
  317. RTW_DPRINTF(RTW_DEBUG_ATTACH,
  318. ("%s: rtw_cardbus_power\n", sc->sc_dev.dv_xname));
  319. if (why == DVACT_RESUME)
  320. rtw_enable(sc);
  321. }
  322. void
  323. rtw_cardbus_setup(struct rtw_cardbus_softc *csc)
  324. {
  325. struct rtw_softc *sc = &csc->sc_rtw;
  326. cardbus_devfunc_t ct = csc->sc_ct;
  327. cardbus_chipset_tag_t cc = ct->ct_cc;
  328. pci_chipset_tag_t pc = csc->sc_pc;
  329. pcireg_t reg;
  330. int pmreg;
  331. if (pci_get_capability(pc, csc->sc_tag,
  332. PCI_CAP_PWRMGMT, &pmreg, 0)) {
  333. reg = pci_conf_read(pc, csc->sc_tag, pmreg + 4) & 0x03;
  334. #if 1 /* XXX Probably not right for CardBus. */
  335. if (reg == 3) {
  336. /*
  337. * The card has lost all configuration data in
  338. * this state, so punt.
  339. */
  340. printf("%s: unable to wake up from power state D3\n",
  341. sc->sc_dev.dv_xname);
  342. return;
  343. }
  344. #endif
  345. if (reg != 0) {
  346. printf("%s: waking up from power state D%d\n",
  347. sc->sc_dev.dv_xname, reg);
  348. pci_conf_write(pc, csc->sc_tag,
  349. pmreg + 4, 0);
  350. }
  351. }
  352. /* Program the BAR. */
  353. pci_conf_write(pc, csc->sc_tag, csc->sc_bar_reg,
  354. csc->sc_bar_val);
  355. /* Make sure the right access type is on the CardBus bridge. */
  356. (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
  357. (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
  358. /* Enable the appropriate bits in the PCI CSR. */
  359. reg = pci_conf_read(pc, csc->sc_tag,
  360. PCI_COMMAND_STATUS_REG);
  361. reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
  362. reg |= csc->sc_csr;
  363. pci_conf_write(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG,
  364. reg);
  365. /*
  366. * Make sure the latency timer is set to some reasonable
  367. * value.
  368. */
  369. reg = pci_conf_read(pc, csc->sc_tag, PCI_BHLC_REG);
  370. if (PCI_LATTIMER(reg) < 0x20) {
  371. reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
  372. reg |= (0x20 << PCI_LATTIMER_SHIFT);
  373. pci_conf_write(pc, csc->sc_tag, PCI_BHLC_REG, reg);
  374. }
  375. }