1 /* $OpenBSD: if_malo_pci.c,v 1.11 2022/03/11 18:00:45 mpi 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_media.h> 39 40 #include <netinet/in.h> 41 #include <netinet/if_ether.h> 42 43 #include <net80211/ieee80211_var.h> 44 #include <net80211/ieee80211_radiotap.h> 45 46 #include <dev/ic/malo.h> 47 48 #include <dev/pci/pcireg.h> 49 #include <dev/pci/pcivar.h> 50 #include <dev/pci/pcidevs.h> 51 52 /* Base Address Register */ 53 #define MALO_PCI_BAR1 0x10 54 #define MALO_PCI_BAR2 0x14 55 56 int malo_pci_match(struct device *, void *, void *); 57 void malo_pci_attach(struct device *, struct device *, void *); 58 int malo_pci_detach(struct device *, int); 59 int malo_pci_activate(struct device *, int); 60 void malo_pci_wakeup(struct malo_softc *); 61 62 struct malo_pci_softc { 63 struct malo_softc sc_malo; 64 65 pci_chipset_tag_t sc_pc; 66 void *sc_ih; 67 68 bus_size_t sc_mapsize1; 69 bus_size_t sc_mapsize2; 70 }; 71 72 const struct cfattach malo_pci_ca = { 73 sizeof(struct malo_pci_softc), malo_pci_match, malo_pci_attach, 74 malo_pci_detach, malo_pci_activate 75 }; 76 77 const struct pci_matchid malo_pci_devices[] = { 78 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8310 }, 79 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_1 }, 80 { PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_2 } 81 }; 82 83 int 84 malo_pci_match(struct device *parent, void *match, void *aux) 85 { 86 return (pci_matchbyid((struct pci_attach_args *)aux, malo_pci_devices, 87 sizeof(malo_pci_devices) / sizeof(malo_pci_devices[0]))); 88 } 89 90 void 91 malo_pci_attach(struct device *parent, struct device *self, void *aux) 92 { 93 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 94 struct pci_attach_args *pa = aux; 95 struct malo_softc *sc = &psc->sc_malo; 96 const char *intrstr = NULL; 97 pci_intr_handle_t ih; 98 int error; 99 100 sc->sc_dmat = pa->pa_dmat; 101 psc->sc_pc = pa->pa_pc; 102 103 /* map control / status registers */ 104 error = pci_mapreg_map(pa, MALO_PCI_BAR1, 105 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 106 &sc->sc_mem1_bt, &sc->sc_mem1_bh, NULL, &psc->sc_mapsize1, 0); 107 if (error != 0) { 108 printf(": can't map 1st mem space\n"); 109 return; 110 } 111 112 /* map control / status registers */ 113 error = pci_mapreg_map(pa, MALO_PCI_BAR2, 114 PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0, 115 &sc->sc_mem2_bt, &sc->sc_mem2_bh, NULL, &psc->sc_mapsize2, 0); 116 if (error != 0) { 117 printf(": can't map 2nd mem space\n"); 118 return; 119 } 120 121 /* map interrupt */ 122 if (pci_intr_map(pa, &ih) != 0) { 123 printf(": can't map interrupt\n"); 124 return; 125 } 126 127 /* establish interrupt */ 128 intrstr = pci_intr_string(psc->sc_pc, ih); 129 psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc, 130 sc->sc_dev.dv_xname); 131 if (psc->sc_ih == NULL) { 132 printf(": could not establish interrupt"); 133 if (intrstr != NULL) 134 printf(" at %s", intrstr); 135 printf("\n"); 136 return; 137 } 138 printf(": %s", intrstr); 139 140 malo_attach(sc); 141 } 142 143 int 144 malo_pci_detach(struct device *self, int flags) 145 { 146 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 147 struct malo_softc *sc = &psc->sc_malo; 148 149 malo_detach(sc); 150 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 151 152 return (0); 153 } 154 155 int 156 malo_pci_activate(struct device *self, int act) 157 { 158 struct malo_pci_softc *psc = (struct malo_pci_softc *)self; 159 struct malo_softc *sc = &psc->sc_malo; 160 struct ifnet *ifp = &sc->sc_ic.ic_if; 161 162 switch (act) { 163 case DVACT_SUSPEND: 164 if (ifp->if_flags & IFF_RUNNING) 165 malo_stop(sc); 166 break; 167 case DVACT_WAKEUP: 168 malo_pci_wakeup(sc); 169 break; 170 } 171 return (0); 172 } 173 174 void 175 malo_pci_wakeup(struct malo_softc *sc) 176 { 177 struct ifnet *ifp = &sc->sc_ic.ic_if; 178 179 if (ifp->if_flags & IFF_UP) 180 malo_init(ifp); 181 } 182