xref: /openbsd-src/sys/dev/pci/if_atw_pci.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_atw_pci.c,v 1.10 2008/06/26 05:42:17 ray Exp $	*/
2 /*	$NetBSD: if_atw_pci.c,v 1.7 2004/07/23 07:07:55 dyoung Exp $	*/
3 
4 /*-
5  * Copyright (c) 1998, 1999, 2000, 2002 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10  * NASA Ames Research Center; Charles M. Hannum; and David Young.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /*
35  * PCI bus front-end for the ADMtek ADM8211 802.11 MAC/BBP chip.
36  *
37  * Derived from the ``Tulip'' PCI bus front-end.
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/mbuf.h>
43 #include <sys/malloc.h>
44 #include <sys/kernel.h>
45 #include <sys/socket.h>
46 #include <sys/ioctl.h>
47 #include <sys/errno.h>
48 #include <sys/device.h>
49 
50 #include <machine/endian.h>
51 
52 #include <net/if.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #ifdef INET
56 #include <netinet/in.h>
57 #include <netinet/if_ether.h>
58 #endif
59 
60 #include <net80211/ieee80211_radiotap.h>
61 #include <net80211/ieee80211_var.h>
62 
63 #include <machine/bus.h>
64 #include <machine/intr.h>
65 
66 #include <dev/ic/atwreg.h>
67 #include <dev/ic/rf3000reg.h>
68 #include <dev/ic/si4136reg.h>
69 #include <dev/ic/atwvar.h>
70 
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcireg.h>
73 #include <dev/pci/pcidevs.h>
74 
75 /*
76  * PCI configuration space registers used by the ADM8211.
77  */
78 #define	ATW_PCI_IOBA		0x10	/* i/o mapped base */
79 #define	ATW_PCI_MMBA		0x14	/* memory mapped base */
80 
81 struct atw_pci_softc {
82 	struct atw_softc	psc_atw;	/* real ADM8211 softc */
83 
84 	pci_intr_handle_t	psc_ih;		/* interrupt handle */
85 	void			*psc_intrcookie;
86 
87 	pci_chipset_tag_t	psc_pc;		/* our PCI chipset */
88 	pcitag_t		psc_pcitag;	/* our PCI tag */
89 };
90 
91 int	atw_pci_match(struct device *, void *, void *);
92 void	atw_pci_attach(struct device *, struct device *, void *);
93 
94 struct cfattach atw_pci_ca = {
95     sizeof (struct atw_softc), atw_pci_match, atw_pci_attach
96 };
97 
98 const struct pci_matchid atw_pci_devices[] = {
99 	{ PCI_VENDOR_ADMTEK,		PCI_PRODUCT_ADMTEK_ADM8211 },
100 };
101 
102 int
103 atw_pci_match(struct device *parent, void *match, void *aux)
104 {
105 	return (pci_matchbyid((struct pci_attach_args *)aux, atw_pci_devices,
106 	    sizeof(atw_pci_devices)/sizeof(atw_pci_devices[0])));
107 }
108 
109 static int
110 atw_pci_enable(struct atw_softc *sc)
111 {
112 	struct atw_pci_softc *psc = (void *)sc;
113 
114 	/* Establish the interrupt. */
115 	psc->psc_intrcookie = pci_intr_establish(psc->psc_pc, psc->psc_ih,
116 	    IPL_NET, atw_intr, sc, sc->sc_dev.dv_xname);
117 	if (psc->psc_intrcookie == NULL) {
118 		printf("%s: unable to establish interrupt\n",
119 		    sc->sc_dev.dv_xname);
120 		return (1);
121 	}
122 
123 	return (0);
124 }
125 
126 static void
127 atw_pci_disable(struct atw_softc *sc)
128 {
129 	struct atw_pci_softc *psc = (void *)sc;
130 
131 	/* Unhook the interrupt handler. */
132 	pci_intr_disestablish(psc->psc_pc, psc->psc_intrcookie);
133 	psc->psc_intrcookie = NULL;
134 }
135 
136 void
137 atw_pci_attach(struct device *parent, struct device *self, void *aux)
138 {
139 	struct atw_pci_softc *psc = (void *) self;
140 	struct atw_softc *sc = &psc->psc_atw;
141 	struct pci_attach_args *pa = aux;
142 	pci_chipset_tag_t pc = pa->pa_pc;
143 	const char *intrstr = NULL;
144 	bus_space_tag_t iot, memt;
145 	bus_space_handle_t ioh, memh;
146 	int ioh_valid, memh_valid;
147 	int state;
148 
149 	psc->psc_pc = pa->pa_pc;
150 	psc->psc_pcitag = pa->pa_tag;
151 
152 	/*
153 	 * No power management hooks.
154 	 * XXX Maybe we should add some!
155 	 */
156 	sc->sc_flags |= ATWF_ENABLED;
157 
158 	/*
159 	 * Get revision info, and set some chip-specific variables.
160 	 */
161 	sc->sc_rev = PCI_REVISION(pa->pa_class);
162 
163 	/*
164 	 * Check to see if the device is in power-save mode, and
165 	 * being it out if necessary.
166 	 *
167 	 * XXX This code comes almost verbatim from if_tlp_pci.c. I do
168 	 * not understand it. Tulip clears the "sleep mode" bit in the
169 	 * CFDA register, first.  There is an equivalent (?) register at the
170 	 * same place in the ADM8211, but the docs do not assign its bits
171 	 * any meanings. -dcy
172 	 */
173 	state = pci_set_powerstate(pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
174 	if (state == PCI_PMCSR_STATE_D3) {
175 		/*
176 		 * The card has lost all configuration data in
177 		 * this state, so punt.
178 		 */
179 		printf(": unable to wake up from power state D3, "
180 		    "reboot required.\n");
181 		return;
182 	}
183 
184 	/*
185 	 * Map the device.
186 	 */
187 	ioh_valid = (pci_mapreg_map(pa, ATW_PCI_IOBA,
188 	    PCI_MAPREG_TYPE_IO, 0,
189 	    &iot, &ioh, NULL, NULL, 0) == 0);
190 	memh_valid = (pci_mapreg_map(pa, ATW_PCI_MMBA,
191 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
192 	    &memt, &memh, NULL, NULL, 0) == 0);
193 
194 	if (memh_valid) {
195 		sc->sc_st = memt;
196 		sc->sc_sh = memh;
197 	} else if (ioh_valid) {
198 		sc->sc_st = iot;
199 		sc->sc_sh = ioh;
200 	} else {
201 		printf(": unable to map device registers\n");
202 		return;
203 	}
204 
205 	sc->sc_dmat = pa->pa_dmat;
206 
207 	/*
208 	 * Get the cacheline size.
209 	 */
210 	sc->sc_cacheline = PCI_CACHELINE(pci_conf_read(pc, pa->pa_tag,
211 	    PCI_BHLC_REG));
212 
213 	/*
214 	 * Get PCI data moving command info.
215 	 */
216 	if (pa->pa_flags & PCI_FLAGS_MRL_OKAY) /* read line */
217 		sc->sc_flags |= ATWF_MRL;
218 	if (pa->pa_flags & PCI_FLAGS_MRM_OKAY) /* read multiple */
219 		sc->sc_flags |= ATWF_MRM;
220 	if (pa->pa_flags & PCI_FLAGS_MWI_OKAY) /* write invalidate */
221 		sc->sc_flags |= ATWF_MWI;
222 
223 	/*
224 	 * Map and establish our interrupt.
225 	 */
226 	if (pci_intr_map(pa, &psc->psc_ih)) {
227 		printf(": unable to map interrupt\n");
228 		return;
229 	}
230 	intrstr = pci_intr_string(pc, psc->psc_ih);
231 	psc->psc_intrcookie = pci_intr_establish(pc, psc->psc_ih, IPL_NET,
232 	    atw_intr, sc, sc->sc_dev.dv_xname);
233 	if (psc->psc_intrcookie == NULL) {
234 		printf(": unable to establish interrupt");
235 		if (intrstr != NULL)
236 			printf(" at %s", intrstr);
237 		printf("\n");
238 		return;
239 	}
240 
241 	printf(": %s\n", intrstr);
242 
243 	sc->sc_enable = atw_pci_enable;
244 	sc->sc_disable = atw_pci_disable;
245 
246 	/*
247 	 * Finish off the attach.
248 	 */
249 	atw_attach(sc);
250 }
251