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