1 /* $OpenBSD: if_malo_pci.c,v 1.9 2015/03/14 03:38:48 jsg Exp $ */ 2 3 /* 4 * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 /* 20 * PCI front-end for the Marvell Libertas 21 */ 22 23 #include "bpfilter.h" 24 25 #include <sys/param.h> 26 #include <sys/sockio.h> 27 #include <sys/mbuf.h> 28 #include <sys/kernel.h> 29 #include <sys/socket.h> 30 #include <sys/systm.h> 31 #include <sys/malloc.h> 32 #include <sys/timeout.h> 33 #include <sys/device.h> 34 35 #include <machine/bus.h> 36 37 #include <net/if.h> 38 #include <net/if_dl.h> 39 #include <net/if_media.h> 40 41 #include <netinet/in.h> 42 #include <netinet/if_ether.h> 43 44 #include <net80211/ieee80211_var.h> 45 #include <net80211/ieee80211_radiotap.h> 46 47 #include <dev/ic/malo.h> 48 49 #include <dev/pci/pcireg.h> 50 #include <dev/pci/pcivar.h> 51 #include <dev/pci/pcidevs.h> 52 53 /* Base Address Register */ 54 #define MALO_PCI_BAR1 0x10 55 #define MALO_PCI_BAR2 0x14 56 57 int malo_pci_match(struct device *, void *, void *); 58 void malo_pci_attach(struct device *, struct device *, void *); 59 int malo_pci_detach(struct device *, int); 60 int malo_pci_activate(struct device *, int); 61 void malo_pci_wakeup(struct malo_softc *); 62 63 struct malo_pci_softc { 64 struct malo_softc sc_malo; 65 66 pci_chipset_tag_t sc_pc; 67 void *sc_ih; 68 69 bus_size_t sc_mapsize1; 70 bus_size_t sc_mapsize2; 71 }; 72 73 struct cfattach malo_pci_ca = { 74 sizeof(struct malo_pci_softc), malo_pci_match, malo_pci_attach, 75 malo_pci_detach, malo_pci_activate 76 }; 77 78 const struct pci_matchid malo_pci_devices[] = { 79 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8310 }, 80 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_1 }, 81 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_2 } 82 }; 83 84 int 85 malo_pci_match(struct device *parent, void *match, void *aux) 86 { 87 return (pci_matchbyid((struct pci_attach_args *)aux, malo_pci_devices, 88 sizeof(malo_pci_devices) / sizeof(malo_pci_devices[0]))); 89 } 90 91 void 92 malo_pci_attach(struct device *parent, struct device *self, void *aux) 93 { 94 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 95 struct pci_attach_args *pa = aux; 96 struct malo_softc *sc = &psc->sc_malo; 97 const char *intrstr = NULL; 98 pci_intr_handle_t ih; 99 int error; 100 101 sc->sc_dmat = pa->pa_dmat; 102 psc->sc_pc = pa->pa_pc; 103 104 /* map control / status registers */ 105 error = pci_mapreg_map(pa, MALO_PCI_BAR1, 106 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 107 &sc->sc_mem1_bt, &sc->sc_mem1_bh, NULL, &psc->sc_mapsize1, 0); 108 if (error != 0) { 109 printf(": can't map 1st mem space\n"); 110 return; 111 } 112 113 /* map control / status registers */ 114 error = pci_mapreg_map(pa, MALO_PCI_BAR2, 115 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 116 &sc->sc_mem2_bt, &sc->sc_mem2_bh, NULL, &psc->sc_mapsize2, 0); 117 if (error != 0) { 118 printf(": can't map 2nd mem space\n"); 119 return; 120 } 121 122 /* map interrupt */ 123 if (pci_intr_map(pa, &ih) != 0) { 124 printf(": can't map interrupt\n"); 125 return; 126 } 127 128 /* establish interrupt */ 129 intrstr = pci_intr_string(psc->sc_pc, ih); 130 psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc, 131 sc->sc_dev.dv_xname); 132 if (psc->sc_ih == NULL) { 133 printf(": could not establish interrupt"); 134 if (intrstr != NULL) 135 printf(" at %s", intrstr); 136 printf("\n"); 137 return; 138 } 139 printf(": %s", intrstr); 140 141 malo_attach(sc); 142 } 143 144 int 145 malo_pci_detach(struct device *self, int flags) 146 { 147 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 148 struct malo_softc *sc = &psc->sc_malo; 149 150 malo_detach(sc); 151 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 152 153 return (0); 154 } 155 156 int 157 malo_pci_activate(struct device *self, int act) 158 { 159 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 160 struct malo_softc *sc = &psc->sc_malo; 161 struct ifnet *ifp = &sc->sc_ic.ic_if; 162 163 switch (act) { 164 case DVACT_SUSPEND: 165 if (ifp->if_flags & IFF_RUNNING) 166 malo_stop(sc); 167 break; 168 case DVACT_WAKEUP: 169 malo_pci_wakeup(sc); 170 break; 171 } 172 return (0); 173 } 174 175 void 176 malo_pci_wakeup(struct malo_softc *sc) 177 { 178 struct ifnet *ifp = &sc->sc_ic.ic_if; 179 180 if (ifp->if_flags & IFF_UP) 181 malo_init(ifp); 182 } 183