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