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