xref: /openbsd-src/sys/dev/pci/if_malo_pci.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: if_malo_pci.c,v 1.6 2010/08/28 23:19:29 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/workq.h>
28 #include <sys/mbuf.h>
29 #include <sys/kernel.h>
30 #include <sys/socket.h>
31 #include <sys/systm.h>
32 #include <sys/malloc.h>
33 #include <sys/timeout.h>
34 #include <sys/device.h>
35 
36 #include <machine/bus.h>
37 #include <machine/intr.h>
38 
39 #include <net/if.h>
40 #include <net/if_dl.h>
41 #include <net/if_media.h>
42 
43 #include <netinet/in.h>
44 #include <netinet/if_ether.h>
45 
46 #include <net80211/ieee80211_var.h>
47 #include <net80211/ieee80211_radiotap.h>
48 
49 #include <dev/ic/malo.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 int	malo_pci_match(struct device *, void *, void *);
60 void	malo_pci_attach(struct device *, struct device *, void *);
61 int	malo_pci_detach(struct device *, int);
62 int	malo_pci_activate(struct device *, int);
63 void	malo_pci_resume(void *, void *);
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 struct cfattach malo_pci_ca = {
76 	sizeof(struct malo_pci_softc), malo_pci_match, malo_pci_attach,
77 	malo_pci_detach, malo_pci_activate
78 };
79 
80 const struct pci_matchid malo_pci_devices[] = {
81 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8310 },
82 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_1 },
83 	{ PCI_VENDOR_MARVELL, PCI_PRODUCT_MARVELL_88W8335_2 }
84 };
85 
86 int
87 malo_pci_match(struct device *parent, void *match, void *aux)
88 {
89 	return (pci_matchbyid((struct pci_attach_args *)aux, malo_pci_devices,
90 	    sizeof(malo_pci_devices) / sizeof(malo_pci_devices[0])));
91 }
92 
93 void
94 malo_pci_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct malo_pci_softc *psc = (struct malo_pci_softc *)self;
97 	struct pci_attach_args *pa = aux;
98 	struct malo_softc *sc = &psc->sc_malo;
99 	const char *intrstr = NULL;
100 	pci_intr_handle_t ih;
101 	int error;
102 
103 	sc->sc_dmat = pa->pa_dmat;
104 	psc->sc_pc = pa->pa_pc;
105 
106 	/* map control / status registers */
107 	error = pci_mapreg_map(pa, MALO_PCI_BAR1,
108 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
109 	    &sc->sc_mem1_bt, &sc->sc_mem1_bh, NULL, &psc->sc_mapsize1, 0);
110 	if (error != 0) {
111 		printf(": can't map 1st mem space\n");
112 		return;
113 	}
114 
115 	/* map control / status registers */
116 	error = pci_mapreg_map(pa, MALO_PCI_BAR2,
117 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT, 0,
118 	    &sc->sc_mem2_bt, &sc->sc_mem2_bh, NULL, &psc->sc_mapsize2, 0);
119 	if (error != 0) {
120 		printf(": can't map 2nd mem space\n");
121 		return;
122 	}
123 
124 	/* map interrupt */
125 	if (pci_intr_map(pa, &ih) != 0) {
126 		printf(": can't map interrupt\n");
127 		return;
128 	}
129 
130 	/* establish interrupt */
131 	intrstr = pci_intr_string(psc->sc_pc, ih);
132 	psc->sc_ih = pci_intr_establish(psc->sc_pc, ih, IPL_NET, malo_intr, sc,
133 	    sc->sc_dev.dv_xname);
134 	if (psc->sc_ih == NULL) {
135 		printf(": could not establish interrupt");
136 		if (intrstr != NULL)
137 			printf(" at %s", intrstr);
138 		printf("\n");
139 		return;
140 	}
141 	printf(": %s", intrstr);
142 
143 	malo_attach(sc);
144 }
145 
146 int
147 malo_pci_detach(struct device *self, int flags)
148 {
149 	struct malo_pci_softc *psc = (struct malo_pci_softc *)self;
150 	struct malo_softc *sc = &psc->sc_malo;
151 
152 	malo_detach(sc);
153 	pci_intr_disestablish(psc->sc_pc, psc->sc_ih);
154 
155 	return (0);
156 }
157 
158 int
159 malo_pci_activate(struct device *self, int act)
160 {
161 	struct malo_pci_softc *psc = (struct malo_pci_softc *)self;
162 	struct malo_softc *sc = &psc->sc_malo;
163 	struct ifnet *ifp = &sc->sc_ic.ic_if;
164 
165 	switch (act) {
166 	case DVACT_SUSPEND:
167 		if (ifp->if_flags & IFF_RUNNING)
168 			malo_stop(sc);
169 		break;
170 	case DVACT_RESUME:
171 		workq_queue_task(NULL, &sc->sc_resume_wqt, 0,
172 		    malo_pci_resume, sc, NULL);
173 		break;
174 	}
175 	return (0);
176 }
177 
178 void
179 malo_pci_resume(void *arg1, void *arg2)
180 {
181 	struct malo_softc *sc = arg1;
182 	struct ifnet *ifp = &sc->sc_ic.ic_if;
183 
184 	if (ifp->if_flags & IFF_UP)
185 		malo_init(ifp);
186 }
187