if_re_cardbus.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* $OpenBSD: if_re_cardbus.c,v 1.27 2015/03/14 03:38:47 jsg Exp $ */
  2. /*
  3. * Copyright (c) 2005 Peter Valchev <pvalchev@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Cardbus front-end for the Realtek 8169
  19. */
  20. #include <sys/param.h>
  21. #include <sys/endian.h>
  22. #include <sys/systm.h>
  23. #include <sys/sockio.h>
  24. #include <sys/mbuf.h>
  25. #include <sys/malloc.h>
  26. #include <sys/kernel.h>
  27. #include <sys/device.h>
  28. #include <sys/timeout.h>
  29. #include <sys/socket.h>
  30. #include <net/if.h>
  31. #include <net/if_dl.h>
  32. #include <net/if_media.h>
  33. #include <netinet/in.h>
  34. #include <netinet/if_ether.h>
  35. #include <dev/mii/miivar.h>
  36. #include <dev/pci/pcidevs.h>
  37. #include <dev/cardbus/cardbusvar.h>
  38. #include <dev/ic/rtl81x9reg.h>
  39. #include <dev/ic/revar.h>
  40. struct re_cardbus_softc {
  41. /* General */
  42. struct rl_softc sc_rl;
  43. /* Cardbus-specific data */
  44. void *sc_ih;
  45. cardbus_devfunc_t ct;
  46. pcitag_t sc_tag;
  47. pci_chipset_tag_t sc_pc;
  48. int sc_csr;
  49. int sc_cben;
  50. int sc_bar_reg;
  51. pcireg_t sc_bar_val;
  52. int sc_intrline;
  53. bus_size_t sc_mapsize;
  54. };
  55. int re_cardbus_probe(struct device *, void *, void *);
  56. void re_cardbus_attach(struct device *, struct device *, void *);
  57. int re_cardbus_detach(struct device *, int);
  58. void re_cardbus_setup(struct rl_softc *);
  59. /*
  60. * Cardbus autoconfig definitions
  61. */
  62. struct cfattach re_cardbus_ca = {
  63. sizeof(struct re_cardbus_softc),
  64. re_cardbus_probe,
  65. re_cardbus_attach,
  66. re_cardbus_detach
  67. };
  68. const struct pci_matchid re_cardbus_devices[] = {
  69. { PCI_VENDOR_REALTEK, PCI_PRODUCT_REALTEK_RT8169 },
  70. };
  71. /*
  72. * Probe for a Realtek 8169/8110 chip. Check the PCI vendor and device
  73. * IDs against our list and return a device name if we find a match.
  74. */
  75. int
  76. re_cardbus_probe(struct device *parent, void *match, void *aux)
  77. {
  78. return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
  79. re_cardbus_devices, nitems(re_cardbus_devices)));
  80. }
  81. /*
  82. * Attach the interface. Allocate softc structures, do ifmedia
  83. * setup and ethernet/BPF attach.
  84. */
  85. void
  86. re_cardbus_attach(struct device *parent, struct device *self, void *aux)
  87. {
  88. struct re_cardbus_softc *csc = (struct re_cardbus_softc *)self;
  89. struct rl_softc *sc = &csc->sc_rl;
  90. struct cardbus_attach_args *ca = aux;
  91. struct cardbus_softc *psc =
  92. (struct cardbus_softc *)sc->sc_dev.dv_parent;
  93. cardbus_chipset_tag_t cc = psc->sc_cc;
  94. cardbus_function_tag_t cf = psc->sc_cf;
  95. cardbus_devfunc_t ct = ca->ca_ct;
  96. bus_addr_t adr;
  97. char intrstr[16];
  98. sc->sc_dmat = ca->ca_dmat;
  99. csc->ct = ct;
  100. csc->sc_tag = ca->ca_tag;
  101. csc->sc_pc = ca->ca_pc;
  102. csc->sc_intrline = ca->ca_intrline;
  103. /*
  104. * Map control/status registers.
  105. */
  106. if (Cardbus_mapreg_map(ct, RL_PCI_LOMEM, PCI_MAPREG_TYPE_MEM, 0,
  107. &sc->rl_btag, &sc->rl_bhandle, &adr, &csc->sc_mapsize) == 0) {
  108. csc->sc_cben = CARDBUS_MEM_ENABLE;
  109. csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
  110. csc->sc_bar_reg = RL_PCI_LOMEM;
  111. csc->sc_bar_val = adr | PCI_MAPREG_TYPE_MEM;
  112. } else {
  113. printf(": can't map mem space\n");
  114. return;
  115. }
  116. /* Enable power */
  117. Cardbus_function_enable(ct);
  118. /* Get chip out of powersave mode (if applicable), initialize
  119. * config registers */
  120. re_cardbus_setup(sc);
  121. /* Allocate interrupt */
  122. csc->sc_ih = cardbus_intr_establish(cc, cf, csc->sc_intrline,
  123. IPL_NET, re_intr, sc, sc->sc_dev.dv_xname);
  124. if (csc->sc_ih == NULL) {
  125. printf(": couldn't establish interrupt at %d",
  126. ca->ca_intrline);
  127. Cardbus_function_disable(csc->ct);
  128. return;
  129. }
  130. snprintf(intrstr, sizeof(intrstr), "irq %d", ca->ca_intrline);
  131. sc->sc_product = PCI_PRODUCT(ca->ca_id);
  132. /* Call bus-independent (common) attach routine */
  133. if (re_attach(sc, intrstr)) {
  134. cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
  135. Cardbus_mapreg_unmap(ct, csc->sc_bar_reg, sc->rl_btag,
  136. sc->rl_bhandle, csc->sc_mapsize);
  137. }
  138. }
  139. /*
  140. * Get chip out of power-saving mode, init registers
  141. */
  142. void
  143. re_cardbus_setup(struct rl_softc *sc)
  144. {
  145. struct re_cardbus_softc *csc = (struct re_cardbus_softc *)sc;
  146. cardbus_devfunc_t ct = csc->ct;
  147. cardbus_chipset_tag_t cc = ct->ct_cc;
  148. pci_chipset_tag_t pc = csc->sc_pc;
  149. pcireg_t reg, command;
  150. int pmreg;
  151. /* Handle power management nonsense */
  152. if (pci_get_capability(pc, csc->sc_tag,
  153. PCI_CAP_PWRMGMT, &pmreg, 0)) {
  154. command = pci_conf_read(pc, csc->sc_tag,
  155. pmreg + PCI_PMCSR);
  156. if (command & RL_PSTATE_MASK) {
  157. pcireg_t iobase, membase, irq;
  158. /* Save important PCI config data */
  159. iobase = pci_conf_read(pc, csc->sc_tag, RL_PCI_LOIO);
  160. membase = pci_conf_read(pc, csc->sc_tag, RL_PCI_LOMEM);
  161. irq = pci_conf_read(pc, csc->sc_tag, RL_PCI_INTLINE);
  162. /* Reset the power state */
  163. printf("%s: chip is in D%d power mode "
  164. "-- setting to D0\n", sc->sc_dev.dv_xname,
  165. command & RL_PSTATE_MASK);
  166. command &= RL_PSTATE_MASK;
  167. pci_conf_write(pc, csc->sc_tag, pmreg + PCI_PMCSR,
  168. command);
  169. /* Restore PCI config data */
  170. pci_conf_write(pc, csc->sc_tag, RL_PCI_LOIO, iobase);
  171. pci_conf_write(pc, csc->sc_tag, RL_PCI_LOMEM, membase);
  172. pci_conf_write(pc, csc->sc_tag, RL_PCI_INTLINE, irq);
  173. }
  174. }
  175. /* Make sure the right access type is on the Cardbus bridge */
  176. (*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cben);
  177. (*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
  178. /* Program the BAR */
  179. pci_conf_write(pc, csc->sc_tag, csc->sc_bar_reg, csc->sc_bar_val);
  180. /* Enable proper bits in CARDBUS CSR */
  181. reg = pci_conf_read(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG);
  182. reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
  183. reg |= csc->sc_csr;
  184. pci_conf_write(pc, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg);
  185. /* Make sure the latency timer is set to some reasonable value */
  186. reg = pci_conf_read(pc, csc->sc_tag, PCI_BHLC_REG);
  187. if (PCI_LATTIMER(reg) < 0x20) {
  188. reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
  189. reg |= (0x20 << PCI_LATTIMER_SHIFT);
  190. pci_conf_write(pc, csc->sc_tag, PCI_BHLC_REG, reg);
  191. }
  192. }
  193. /*
  194. * Cardbus detach function: deallocate all resources
  195. */
  196. int
  197. re_cardbus_detach(struct device *self, int flags)
  198. {
  199. struct re_cardbus_softc *csc = (void *)self;
  200. struct rl_softc *sc = &csc->sc_rl;
  201. struct cardbus_devfunc *ct = csc->ct;
  202. struct ifnet *ifp = &sc->sc_arpcom.ac_if;
  203. /* Remove timeout handler */
  204. timeout_del(&sc->timer_handle);
  205. /* Detach PHY */
  206. if (LIST_FIRST(&sc->sc_mii.mii_phys) != NULL)
  207. mii_detach(&sc->sc_mii, MII_PHY_ANY, MII_OFFSET_ANY);
  208. /* Delete media stuff */
  209. ifmedia_delete_instance(&sc->sc_mii.mii_media, IFM_INST_ANY);
  210. ether_ifdetach(ifp);
  211. if_detach(ifp);
  212. /* Disable interrupts */
  213. if (csc->sc_ih != NULL)
  214. cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, csc->sc_ih);
  215. /* Free cardbus resources */
  216. Cardbus_mapreg_unmap(ct, csc->sc_bar_reg, sc->rl_btag, sc->rl_bhandle,
  217. csc->sc_mapsize);
  218. return (0);
  219. }