| 1 | /* $NetBSD: if_ral_cardbus.c,v 1.24 2016/07/14 10:19:06 msaitoh Exp $ */ |
| 2 | /* $OpenBSD: if_ral_cardbus.c,v 1.6 2006/01/09 20:03:31 damien Exp $ */ |
| 3 | |
| 4 | /*- |
| 5 | * Copyright (c) 2005, 2006 |
| 6 | * Damien Bergamini <damien.bergamini@free.fr> |
| 7 | * |
| 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * CardBus front-end for the Ralink RT2560/RT2561/RT2561S/RT2661 driver. |
| 23 | */ |
| 24 | #include <sys/cdefs.h> |
| 25 | __KERNEL_RCSID(0, "$NetBSD: if_ral_cardbus.c,v 1.24 2016/07/14 10:19:06 msaitoh Exp $" ); |
| 26 | |
| 27 | |
| 28 | #include <sys/param.h> |
| 29 | #include <sys/sockio.h> |
| 30 | #include <sys/mbuf.h> |
| 31 | #include <sys/kernel.h> |
| 32 | #include <sys/socket.h> |
| 33 | #include <sys/systm.h> |
| 34 | #include <sys/malloc.h> |
| 35 | #include <sys/callout.h> |
| 36 | #include <sys/device.h> |
| 37 | |
| 38 | #include <sys/bus.h> |
| 39 | #include <sys/intr.h> |
| 40 | |
| 41 | #include <net/if.h> |
| 42 | #include <net/if_dl.h> |
| 43 | #include <net/if_media.h> |
| 44 | #include <net/if_ether.h> |
| 45 | |
| 46 | #include <netinet/in.h> |
| 47 | |
| 48 | #include <net80211/ieee80211_var.h> |
| 49 | #include <net80211/ieee80211_amrr.h> |
| 50 | #include <net80211/ieee80211_rssadapt.h> |
| 51 | #include <net80211/ieee80211_radiotap.h> |
| 52 | |
| 53 | #include <dev/ic/rt2560var.h> |
| 54 | #include <dev/ic/rt2661var.h> |
| 55 | |
| 56 | #include <dev/pci/pcireg.h> |
| 57 | #include <dev/pci/pcivar.h> |
| 58 | #include <dev/pci/pcidevs.h> |
| 59 | |
| 60 | #include <dev/cardbus/cardbusvar.h> |
| 61 | |
| 62 | static struct ral_opns { |
| 63 | int (*attach)(void *, int); |
| 64 | int (*detach)(void *); |
| 65 | int (*intr)(void *); |
| 66 | |
| 67 | } ral_rt2560_opns = { |
| 68 | rt2560_attach, |
| 69 | rt2560_detach, |
| 70 | rt2560_intr |
| 71 | |
| 72 | }, ral_rt2661_opns = { |
| 73 | rt2661_attach, |
| 74 | rt2661_detach, |
| 75 | rt2661_intr |
| 76 | }; |
| 77 | |
| 78 | struct ral_cardbus_softc { |
| 79 | union { |
| 80 | struct rt2560_softc sc_rt2560; |
| 81 | struct rt2661_softc sc_rt2661; |
| 82 | } u; |
| 83 | #define sc_sc u.sc_rt2560 |
| 84 | |
| 85 | /* cardbus specific goo */ |
| 86 | struct ral_opns *sc_opns; |
| 87 | cardbus_devfunc_t sc_ct; |
| 88 | pcitag_t sc_tag; |
| 89 | void *sc_ih; |
| 90 | bus_size_t sc_mapsize; |
| 91 | pcireg_t sc_bar_val; |
| 92 | }; |
| 93 | |
| 94 | int ral_cardbus_match(device_t, cfdata_t, void *); |
| 95 | void ral_cardbus_attach(device_t, device_t, void *); |
| 96 | int ral_cardbus_detach(device_t, int); |
| 97 | |
| 98 | CFATTACH_DECL_NEW(ral_cardbus, sizeof (struct ral_cardbus_softc), |
| 99 | ral_cardbus_match, ral_cardbus_attach, ral_cardbus_detach, NULL); |
| 100 | |
| 101 | int ral_cardbus_enable(struct rt2560_softc *); |
| 102 | void ral_cardbus_disable(struct rt2560_softc *); |
| 103 | void ral_cardbus_power(struct rt2560_softc *, int); |
| 104 | void ral_cardbus_setup(struct ral_cardbus_softc *); |
| 105 | |
| 106 | int |
| 107 | ral_cardbus_match(device_t parent, cfdata_t cfdata, void *aux) |
| 108 | { |
| 109 | struct cardbus_attach_args *ca = aux; |
| 110 | |
| 111 | if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_RALINK) { |
| 112 | switch (PCI_PRODUCT(ca->ca_id)) { |
| 113 | case PCI_PRODUCT_RALINK_RT2560: |
| 114 | case PCI_PRODUCT_RALINK_RT2561: |
| 115 | case PCI_PRODUCT_RALINK_RT2561S: |
| 116 | case PCI_PRODUCT_RALINK_RT2661: |
| 117 | return 1; |
| 118 | default: |
| 119 | return 0; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | ral_cardbus_attach(device_t parent, device_t self, void *aux) |
| 128 | { |
| 129 | struct ral_cardbus_softc *csc = device_private(self); |
| 130 | struct rt2560_softc *sc = &csc->sc_sc; |
| 131 | struct cardbus_attach_args *ca = aux; |
| 132 | cardbus_devfunc_t ct = ca->ca_ct; |
| 133 | char devinfo[256]; |
| 134 | bus_addr_t base; |
| 135 | int error, revision; |
| 136 | |
| 137 | pci_devinfo(ca->ca_id, ca->ca_class, 0, devinfo, sizeof(devinfo)); |
| 138 | revision = PCI_REVISION(ca->ca_class); |
| 139 | aprint_normal(": %s (rev. 0x%02x)\n" , devinfo, revision); |
| 140 | |
| 141 | csc->sc_opns = |
| 142 | (PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_RALINK_RT2560) ? |
| 143 | &ral_rt2560_opns : &ral_rt2661_opns; |
| 144 | |
| 145 | sc->sc_dev = self; |
| 146 | sc->sc_dmat = ca->ca_dmat; |
| 147 | csc->sc_ct = ct; |
| 148 | csc->sc_tag = ca->ca_tag; |
| 149 | |
| 150 | /* power management hooks */ |
| 151 | sc->sc_enable = ral_cardbus_enable; |
| 152 | sc->sc_disable = ral_cardbus_disable; |
| 153 | |
| 154 | /* map control/status registers */ |
| 155 | error = Cardbus_mapreg_map(ct, PCI_BAR0, |
| 156 | PCI_MAPREG_TYPE_MEM, 0, &sc->sc_st, &sc->sc_sh, &base, |
| 157 | &csc->sc_mapsize); |
| 158 | if (error != 0) { |
| 159 | aprint_error(": could not map memory space\n" ); |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | csc->sc_bar_val = base | PCI_MAPREG_TYPE_MEM; |
| 164 | |
| 165 | /* set up the PCI configuration registers */ |
| 166 | ral_cardbus_setup(csc); |
| 167 | |
| 168 | (*csc->sc_opns->attach)(sc, PCI_PRODUCT(ca->ca_id)); |
| 169 | |
| 170 | Cardbus_function_disable(ct); |
| 171 | } |
| 172 | |
| 173 | int |
| 174 | ral_cardbus_detach(device_t self, int flags) |
| 175 | { |
| 176 | struct ral_cardbus_softc *csc = device_private(self); |
| 177 | struct rt2560_softc *sc = &csc->sc_sc; |
| 178 | cardbus_devfunc_t ct = csc->sc_ct; |
| 179 | int error; |
| 180 | |
| 181 | error = (*csc->sc_opns->detach)(sc); |
| 182 | if (error != 0) |
| 183 | return error; |
| 184 | |
| 185 | /* unhook the interrupt handler */ |
| 186 | if (csc->sc_ih != NULL) { |
| 187 | Cardbus_intr_disestablish(ct, csc->sc_ih); |
| 188 | csc->sc_ih = NULL; |
| 189 | } |
| 190 | |
| 191 | /* release bus space and close window */ |
| 192 | Cardbus_mapreg_unmap(ct, PCI_BAR0, sc->sc_st, sc->sc_sh, |
| 193 | csc->sc_mapsize); |
| 194 | |
| 195 | return 0; |
| 196 | } |
| 197 | |
| 198 | int |
| 199 | ral_cardbus_enable(struct rt2560_softc *sc) |
| 200 | { |
| 201 | struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc; |
| 202 | cardbus_devfunc_t ct = csc->sc_ct; |
| 203 | |
| 204 | /* power on the socket */ |
| 205 | Cardbus_function_enable(ct); |
| 206 | |
| 207 | /* setup the PCI configuration registers */ |
| 208 | ral_cardbus_setup(csc); |
| 209 | |
| 210 | /* map and establish the interrupt handler */ |
| 211 | csc->sc_ih = Cardbus_intr_establish(ct, IPL_NET, |
| 212 | csc->sc_opns->intr, sc); |
| 213 | if (csc->sc_ih == NULL) { |
| 214 | aprint_error_dev(sc->sc_dev, |
| 215 | "could not establish interrupt\n" ); |
| 216 | Cardbus_function_disable(ct); |
| 217 | return 1; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | |
| 223 | void |
| 224 | ral_cardbus_disable(struct rt2560_softc *sc) |
| 225 | { |
| 226 | struct ral_cardbus_softc *csc = (struct ral_cardbus_softc *)sc; |
| 227 | cardbus_devfunc_t ct = csc->sc_ct; |
| 228 | |
| 229 | /* unhook the interrupt handler */ |
| 230 | Cardbus_intr_disestablish(ct, csc->sc_ih); |
| 231 | csc->sc_ih = NULL; |
| 232 | |
| 233 | /* power down the socket */ |
| 234 | Cardbus_function_disable(ct); |
| 235 | } |
| 236 | |
| 237 | void |
| 238 | ral_cardbus_setup(struct ral_cardbus_softc *csc) |
| 239 | { |
| 240 | cardbus_devfunc_t ct = csc->sc_ct; |
| 241 | pcireg_t reg; |
| 242 | |
| 243 | /* program the BAR */ |
| 244 | Cardbus_conf_write(ct, csc->sc_tag, PCI_BAR0, csc->sc_bar_val); |
| 245 | |
| 246 | /* enable the appropriate bits in the PCI CSR */ |
| 247 | reg = Cardbus_conf_read(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG); |
| 248 | reg |= PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_MEM_ENABLE; |
| 249 | Cardbus_conf_write(ct, csc->sc_tag, PCI_COMMAND_STATUS_REG, reg); |
| 250 | } |
| 251 | |