| 1 | /* $NetBSD: if_malo_pci.c,v 1.5 2014/03/29 19:28:25 christos 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.5 2014/03/29 19:28:25 christos 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 | return; |
| 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 | return; |
| 151 | } |
| 152 | |
| 153 | /* map interrupt */ |
| 154 | if (pci_intr_map(pa, &ih) != 0) { |
| 155 | aprint_error_dev(self, "can't map interrupt\n" ); |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | /* establish interrupt */ |
| 160 | intrstr = pci_intr_string(psc->sc_pc, ih, intrbuf, sizeof(intrbuf)); |
| 161 | psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc); |
| 162 | if (psc->sc_ih == NULL) { |
| 163 | aprint_error_dev(self, "could not establish interrupt" ); |
| 164 | if (intrstr != NULL) |
| 165 | aprint_error(" at %s" , intrstr); |
| 166 | aprint_error("\n" ); |
| 167 | return; |
| 168 | } |
| 169 | aprint_normal_dev(self, "interrupting at %s\n" , intrstr); |
| 170 | |
| 171 | malo_attach(sc); |
| 172 | |
| 173 | if (pmf_device_register(self, malo_pci_suspend, malo_pci_resume)) |
| 174 | pmf_class_network_register(self, &sc->sc_if); |
| 175 | else |
| 176 | aprint_error_dev(self, "couldn't establish power handler\n" ); |
| 177 | } |
| 178 | |
| 179 | int |
| 180 | malo_pci_detach(device_t self, int flags) |
| 181 | { |
| 182 | struct malo_pci_softc *psc = device_private(self); |
| 183 | struct malo_softc *sc = &psc->sc_malo; |
| 184 | |
| 185 | malo_detach(sc); |
| 186 | pci_intr_disestablish(psc->sc_pc, psc->sc_ih); |
| 187 | |
| 188 | return (0); |
| 189 | } |
| 190 | |
| 191 | static bool |
| 192 | malo_pci_suspend(device_t self, const pmf_qual_t *qual) |
| 193 | { |
| 194 | struct malo_pci_softc *psc = device_private(self); |
| 195 | struct malo_softc *sc = &psc->sc_malo; |
| 196 | struct ifnet *ifp = &sc->sc_if; |
| 197 | |
| 198 | malo_stop(ifp, 1); |
| 199 | |
| 200 | return true; |
| 201 | } |
| 202 | |
| 203 | static bool |
| 204 | malo_pci_resume(device_t self, const pmf_qual_t *qual) |
| 205 | { |
| 206 | struct malo_pci_softc *psc = device_private(self); |
| 207 | struct malo_softc *sc = &psc->sc_malo; |
| 208 | struct ifnet *ifp = &sc->sc_if; |
| 209 | |
| 210 | if (ifp->if_flags & IFF_UP) |
| 211 | malo_init(ifp); |
| 212 | |
| 213 | return true; |
| 214 | } |
| 215 | |