1 /* $NetBSD: if_malo_pci.c,v 1.6 2017/02/02 10:05:35 nonaka Exp $ */ 2 /* $OpenBSD: if_malo_pci.c,v 1.6 2010/08/28 23:19:29 deraadt Exp $ */ 3 4 /* 5 * Copyright (c) 2006 Marcus Glocker <mglocker@openbsd.org> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 /* 21 * PCI front-end for the Marvell Libertas 22 */ 23 24 #include <sys/cdefs.h> 25 __KERNEL_RCSID(0, "$NetBSD: if_malo_pci.c,v 1.6 2017/02/02 10:05:35 nonaka Exp $"); 26 27 #include <sys/param.h> 28 #include <sys/sockio.h> 29 #include <sys/mbuf.h> 30 #include <sys/kernel.h> 31 #include <sys/socket.h> 32 #include <sys/systm.h> 33 #include <sys/malloc.h> 34 #include <sys/device.h> 35 #include <sys/bus.h> 36 37 #include <machine/intr.h> 38 39 #include <net/if.h> 40 #include <net/if_dl.h> 41 #include <net/if_media.h> 42 #include <net/if_ether.h> 43 44 #include <netinet/in.h> 45 46 #include <net80211/ieee80211_var.h> 47 #include <net80211/ieee80211_radiotap.h> 48 49 #include <dev/ic/malovar.h> 50 51 #include <dev/pci/pcireg.h> 52 #include <dev/pci/pcivar.h> 53 #include <dev/pci/pcidevs.h> 54 55 /* Base Address Register */ 56 #define MALO_PCI_BAR1 0x10 57 #define MALO_PCI_BAR2 0x14 58 59 static int malo_pci_match(device_t parent, cfdata_t match, void *aux); 60 static void malo_pci_attach(device_t, device_t, void *); 61 static int malo_pci_detach(device_t, int); 62 static bool malo_pci_suspend(device_t, const pmf_qual_t *); 63 static bool malo_pci_resume(device_t, const pmf_qual_t *); 64 65 struct malo_pci_softc { 66 struct malo_softc sc_malo; 67 68 pci_chipset_tag_t sc_pc; 69 void *sc_ih; 70 71 bus_size_t sc_mapsize1; 72 bus_size_t sc_mapsize2; 73 }; 74 75 CFATTACH_DECL_NEW(malo_pci, sizeof(struct malo_pci_softc), 76 malo_pci_match, malo_pci_attach, malo_pci_detach, NULL); 77 78 static int 79 malo_pci_match(device_t parent, cfdata_t match, void *aux) 80 { 81 struct pci_attach_args *pa = aux; 82 83 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_MARVELL) 84 return (0); 85 86 switch (PCI_PRODUCT(pa->pa_id)) { 87 case PCI_PRODUCT_MARVELL_88W8310: 88 case PCI_PRODUCT_MARVELL_88W8335_1: 89 case PCI_PRODUCT_MARVELL_88W8335_2: 90 return (1); 91 } 92 93 return (0); 94 } 95 96 static void 97 malo_pci_attach(device_t parent, device_t self, void *aux) 98 { 99 struct malo_pci_softc *psc = device_private(self); 100 struct pci_attach_args *pa = aux; 101 struct malo_softc *sc = &psc->sc_malo; 102 const char *intrstr = NULL; 103 pci_intr_handle_t ih; 104 pcireg_t memtype1, memtype2; 105 int error; 106 char intrbuf[PCI_INTRSTR_LEN]; 107 108 sc->sc_dev = self; 109 sc->sc_dmat = pa->pa_dmat; 110 psc->sc_pc = pa->pa_pc; 111 112 aprint_normal("\n"); 113 aprint_normal_dev(self,"Marvell Libertas Wireless\n"); 114 115 /* map control / status registers */ 116 memtype1 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1); 117 switch (memtype1) { 118 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 119 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 120 break; 121 default: 122 aprint_error_dev(self, "invalid base address register\n"); 123 return; 124 } 125 126 error = pci_mapreg_map(pa, MALO_PCI_BAR1, 127 memtype1, 0, &sc->sc_mem1_bt, &sc->sc_mem1_bh, 128 NULL, &psc->sc_mapsize1); 129 if (error != 0) { 130 aprint_error_dev(self, "can't map 1st mem space\n"); 131 return; 132 } 133 134 /* map control / status registers */ 135 memtype2 = pci_mapreg_type(pa->pa_pc, pa->pa_tag, MALO_PCI_BAR1); 136 switch (memtype2) { 137 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT: 138 case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT: 139 break; 140 default: 141 aprint_error_dev(self, "invalid base address register\n"); 142 goto unmap1; 143 } 144 145 error = pci_mapreg_map(pa, MALO_PCI_BAR2, 146 memtype2, 0, &sc->sc_mem2_bt, &sc->sc_mem2_bh, 147 NULL, &psc->sc_mapsize2); 148 if (error != 0) { 149 aprint_error_dev(self, "can't map 2nd mem space\n"); 150 goto unmap1; 151 } 152 153 sc->sc_soft_ih = softint_establish(SOFTINT_NET, malo_softintr, sc); 154 if (sc->sc_soft_ih == NULL) { 155 aprint_error_dev(self, "could not establish softint\n"); 156 goto unmap2; 157 } 158 159 /* map interrupt */ 160 if (pci_intr_map(pa, &ih) != 0) { 161 aprint_error_dev(self, "can't map interrupt\n"); 162 goto failsi; 163 } 164 165 /* establish interrupt */ 166 intrstr = pci_intr_string(psc->sc_pc, ih, intrbuf, sizeof(intrbuf)); 167 psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc); 168 if (psc->sc_ih == NULL) { 169 aprint_error_dev(self, "could not establish interrupt"); 170 if (intrstr != NULL) 171 aprint_error(" at %s", intrstr); 172 aprint_error("\n"); 173 goto failsi; 174 } 175 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 176 177 if (malo_attach(sc)) 178 goto failih; 179 180 if (pmf_device_register(self, malo_pci_suspend, malo_pci_resume)) 181 pmf_class_network_register(self, &sc->sc_if); 182 else 183 aprint_error_dev(self, "couldn't establish power handler\n"); 184 return; 185 186 failih: pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 187 psc->sc_ih = NULL; 188 failsi: softint_disestablish(sc->sc_soft_ih); 189 sc->sc_soft_ih = NULL; 190 unmap2: bus_space_unmap(sc->sc_mem2_bt, sc->sc_mem2_bh, psc->sc_mapsize2); 191 unmap1: bus_space_unmap(sc->sc_mem1_bt, sc->sc_mem1_bh, psc->sc_mapsize1); 192 } 193 194 int 195 malo_pci_detach(device_t self, int flags) 196 { 197 struct malo_pci_softc *psc = device_private(self); 198 struct malo_softc *sc = &psc->sc_malo; 199 200 malo_detach(sc); 201 if (psc->sc_ih != NULL) { 202 pci_intr_disestablish(psc->sc_pc, psc->sc_ih); 203 psc->sc_ih = NULL; 204 } 205 if (sc->sc_soft_ih != NULL) { 206 softint_disestablish(sc->sc_soft_ih); 207 sc->sc_soft_ih = NULL; 208 } 209 bus_space_unmap(sc->sc_mem2_bt, sc->sc_mem2_bh, psc->sc_mapsize2); 210 bus_space_unmap(sc->sc_mem1_bt, sc->sc_mem1_bh, psc->sc_mapsize1); 211 212 return (0); 213 } 214 215 static bool 216 malo_pci_suspend(device_t self, const pmf_qual_t *qual) 217 { 218 struct malo_pci_softc *psc = device_private(self); 219 struct malo_softc *sc = &psc->sc_malo; 220 struct ifnet *ifp = &sc->sc_if; 221 222 malo_stop(ifp, 1); 223 224 return true; 225 } 226 227 static bool 228 malo_pci_resume(device_t self, const pmf_qual_t *qual) 229 { 230 struct malo_pci_softc *psc = device_private(self); 231 struct malo_softc *sc = &psc->sc_malo; 232 struct ifnet *ifp = &sc->sc_if; 233 234 if (ifp->if_flags & IFF_UP) 235 malo_init(ifp); 236 237 return true; 238 } 239