arn9003.c revision 1.1 1 1.1 christos /* $NetBSD: arn9003.c,v 1.1 2013/03/30 02:53:01 christos Exp $ */
2 1.1 christos /* $OpenBSD: ar9003.c,v 1.25 2012/10/20 09:53:32 stsp Exp $ */
3 1.1 christos
4 1.1 christos /*-
5 1.1 christos * Copyright (c) 2010 Damien Bergamini <damien.bergamini (at) free.fr>
6 1.1 christos * Copyright (c) 2010 Atheros Communications Inc.
7 1.1 christos *
8 1.1 christos * Permission to use, copy, modify, and/or distribute this software for any
9 1.1 christos * purpose with or without fee is hereby granted, provided that the above
10 1.1 christos * copyright notice and this permission notice appear in all copies.
11 1.1 christos *
12 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.1 christos */
20 1.1 christos
21 1.1 christos /*
22 1.1 christos * Driver for Atheros 802.11a/g/n chipsets.
23 1.1 christos * Routines for AR9003 family.
24 1.1 christos */
25 1.1 christos
26 1.1 christos #include <sys/cdefs.h>
27 1.1 christos __KERNEL_RCSID(0, "$NetBSD: arn9003.c,v 1.1 2013/03/30 02:53:01 christos Exp $");
28 1.1 christos
29 1.1 christos #include <sys/param.h>
30 1.1 christos #include <sys/sockio.h>
31 1.1 christos #include <sys/mbuf.h>
32 1.1 christos #include <sys/kernel.h>
33 1.1 christos #include <sys/socket.h>
34 1.1 christos #include <sys/systm.h>
35 1.1 christos #include <sys/malloc.h>
36 1.1 christos #include <sys/queue.h>
37 1.1 christos #include <sys/callout.h>
38 1.1 christos #include <sys/conf.h>
39 1.1 christos #include <sys/device.h>
40 1.1 christos
41 1.1 christos #include <sys/bus.h>
42 1.1 christos #include <sys/endian.h>
43 1.1 christos #include <sys/intr.h>
44 1.1 christos
45 1.1 christos #include <net/bpf.h>
46 1.1 christos #include <net/if.h>
47 1.1 christos #include <net/if_arp.h>
48 1.1 christos #include <net/if_dl.h>
49 1.1 christos #include <net/if_media.h>
50 1.1 christos #include <net/if_types.h>
51 1.1 christos
52 1.1 christos #include <netinet/in.h>
53 1.1 christos #include <netinet/in_systm.h>
54 1.1 christos #include <netinet/in_var.h>
55 1.1 christos #include <netinet/ip.h>
56 1.1 christos
57 1.1 christos #include <net80211/ieee80211_var.h>
58 1.1 christos #include <net80211/ieee80211_amrr.h>
59 1.1 christos #include <net80211/ieee80211_radiotap.h>
60 1.1 christos
61 1.1 christos #include <dev/ic/athnreg.h>
62 1.1 christos #include <dev/ic/athnvar.h>
63 1.1 christos #include <dev/ic/arn9003reg.h>
64 1.1 christos #include <dev/ic/arn9003.h>
65 1.1 christos
66 1.1 christos #define Static static
67 1.1 christos
68 1.1 christos Static void ar9003_calib_iq(struct athn_softc *);
69 1.1 christos Static int ar9003_calib_tx_iq(struct athn_softc *);
70 1.1 christos Static int ar9003_compute_predistortion(struct athn_softc *,
71 1.1 christos const uint32_t *, const uint32_t *);
72 1.1 christos Static void ar9003_disable_ofdm_weak_signal(struct athn_softc *);
73 1.1 christos Static void ar9003_disable_phy(struct athn_softc *);
74 1.1 christos Static int ar9003_dma_alloc(struct athn_softc *);
75 1.1 christos Static void ar9003_dma_free(struct athn_softc *);
76 1.1 christos Static void ar9003_do_calib(struct athn_softc *);
77 1.1 christos Static void ar9003_do_noisefloor_calib(struct athn_softc *);
78 1.1 christos Static void ar9003_enable_antenna_diversity(struct athn_softc *);
79 1.1 christos Static void ar9003_enable_ofdm_weak_signal(struct athn_softc *);
80 1.1 christos Static void ar9003_enable_predistorter(struct athn_softc *, int);
81 1.1 christos Static int ar9003_find_rom(struct athn_softc *);
82 1.1 christos Static void ar9003_force_txgain(struct athn_softc *, uint32_t);
83 1.1 christos Static int ar9003_get_desired_txgain(struct athn_softc *, int, int);
84 1.1 christos Static int ar9003_get_iq_corr(struct athn_softc *, int32_t[], int32_t[]);
85 1.1 christos Static void ar9003_gpio_config_input(struct athn_softc *, int);
86 1.1 christos Static void ar9003_gpio_config_output(struct athn_softc *, int, int);
87 1.1 christos Static int ar9003_gpio_read(struct athn_softc *, int);
88 1.1 christos Static void ar9003_gpio_write(struct athn_softc *, int, int);
89 1.1 christos Static void ar9003_hw_init(struct athn_softc *, struct ieee80211_channel *,
90 1.1 christos struct ieee80211_channel *);
91 1.1 christos Static void ar9003_init_baseband(struct athn_softc *);
92 1.1 christos Static void ar9003_init_chains(struct athn_softc *);
93 1.1 christos Static int ar9003_intr(struct athn_softc *);
94 1.1 christos Static void ar9003_next_calib(struct athn_softc *);
95 1.1 christos Static void ar9003_paprd_enable(struct athn_softc *);
96 1.1 christos Static int ar9003_paprd_tx_tone(struct athn_softc *);
97 1.1 christos Static void ar9003_paprd_tx_tone_done(struct athn_softc *);
98 1.1 christos Static int ar9003_read_eep_data(struct athn_softc *, uint32_t, void *,
99 1.1 christos int);
100 1.1 christos Static int ar9003_read_eep_word(struct athn_softc *, uint32_t,
101 1.1 christos uint16_t *);
102 1.1 christos Static int ar9003_read_otp_data(struct athn_softc *, uint32_t, void *,
103 1.1 christos int);
104 1.1 christos Static int ar9003_read_otp_word(struct athn_softc *, uint32_t,
105 1.1 christos uint32_t *);
106 1.1 christos Static int ar9003_read_rom(struct athn_softc *);
107 1.1 christos Static void ar9003_reset_rx_gain(struct athn_softc *,
108 1.1 christos struct ieee80211_channel *);
109 1.1 christos Static void ar9003_reset_tx_gain(struct athn_softc *,
110 1.1 christos struct ieee80211_channel *);
111 1.1 christos Static int ar9003_restore_rom_block(struct athn_softc *, uint8_t,
112 1.1 christos uint8_t, const uint8_t *, size_t);
113 1.1 christos Static void ar9003_rf_bus_release(struct athn_softc *);
114 1.1 christos Static int ar9003_rf_bus_request(struct athn_softc *);
115 1.1 christos Static void ar9003_rfsilent_init(struct athn_softc *);
116 1.1 christos Static int ar9003_rx_alloc(struct athn_softc *, int, int);
117 1.1 christos Static void ar9003_rx_enable(struct athn_softc *);
118 1.1 christos Static void ar9003_rx_free(struct athn_softc *, int);
119 1.1 christos Static void ar9003_rx_intr(struct athn_softc *, int);
120 1.1 christos Static int ar9003_rx_process(struct athn_softc *, int);
121 1.1 christos Static void ar9003_rx_radiotap(struct athn_softc *, struct mbuf *,
122 1.1 christos struct ar_rx_status *);
123 1.1 christos Static void ar9003_set_cck_weak_signal(struct athn_softc *, int);
124 1.1 christos Static void ar9003_set_delta_slope(struct athn_softc *,
125 1.1 christos struct ieee80211_channel *, struct ieee80211_channel *);
126 1.1 christos Static void ar9003_set_firstep_level(struct athn_softc *, int);
127 1.1 christos Static void ar9003_set_noise_immunity_level(struct athn_softc *, int);
128 1.1 christos Static void ar9003_set_phy(struct athn_softc *, struct ieee80211_channel *,
129 1.1 christos struct ieee80211_channel *);
130 1.1 christos Static void ar9003_set_rf_mode(struct athn_softc *,
131 1.1 christos struct ieee80211_channel *);
132 1.1 christos Static void ar9003_set_rxchains(struct athn_softc *);
133 1.1 christos Static void ar9003_set_spur_immunity_level(struct athn_softc *, int);
134 1.1 christos Static void ar9003_set_training_gain(struct athn_softc *, int);
135 1.1 christos Static int ar9003_swba_intr(struct athn_softc *);
136 1.1 christos Static int ar9003_tx(struct athn_softc *, struct mbuf *,
137 1.1 christos struct ieee80211_node *, int);
138 1.1 christos Static int ar9003_tx_alloc(struct athn_softc *);
139 1.1 christos Static void ar9003_tx_free(struct athn_softc *);
140 1.1 christos Static void ar9003_tx_intr(struct athn_softc *);
141 1.1 christos Static int ar9003_tx_process(struct athn_softc *);
142 1.1 christos
143 1.1 christos #ifdef notused
144 1.1 christos Static void ar9003_bb_load_noisefloor(struct athn_softc *);
145 1.1 christos Static void ar9003_get_noisefloor(struct athn_softc *,
146 1.1 christos struct ieee80211_channel *);
147 1.1 christos Static void ar9003_paprd_calib(struct athn_softc *,
148 1.1 christos struct ieee80211_channel *);
149 1.1 christos Static void ar9003_read_noisefloor(struct athn_softc *, int16_t *,
150 1.1 christos int16_t *);
151 1.1 christos Static void ar9003_write_noisefloor(struct athn_softc *, int16_t *,
152 1.1 christos int16_t *);
153 1.1 christos Static void ar9300_noisefloor_calib(struct athn_softc *);
154 1.1 christos #endif /* notused */
155 1.1 christos
156 1.1 christos /*
157 1.1 christos * XXX: See if_iwn.c:MCLGETIalt() for a better solution.
158 1.1 christos * XXX: Put this in a header or in athn.c so it can be shared between
159 1.1 christos * ar5008.c and ar9003.c?
160 1.1 christos */
161 1.1 christos static struct mbuf *
162 1.1 christos MCLGETI(struct athn_softc *sc __unused, int how,
163 1.1 christos struct ifnet *ifp __unused, u_int size)
164 1.1 christos {
165 1.1 christos struct mbuf *m;
166 1.1 christos
167 1.1 christos MGETHDR(m, how, MT_DATA);
168 1.1 christos if (m == NULL)
169 1.1 christos return NULL;
170 1.1 christos
171 1.1 christos MEXTMALLOC(m, size, how);
172 1.1 christos if ((m->m_flags & M_EXT) == 0) {
173 1.1 christos m_freem(m);
174 1.1 christos return NULL;
175 1.1 christos }
176 1.1 christos return m;
177 1.1 christos }
178 1.1 christos
179 1.1 christos PUBLIC int
180 1.1 christos ar9003_attach(struct athn_softc *sc)
181 1.1 christos {
182 1.1 christos struct athn_ops *ops = &sc->sc_ops;
183 1.1 christos int error;
184 1.1 christos
185 1.1 christos /* Set callbacks for AR9003 family. */
186 1.1 christos ops->gpio_read = ar9003_gpio_read;
187 1.1 christos ops->gpio_write = ar9003_gpio_write;
188 1.1 christos ops->gpio_config_input = ar9003_gpio_config_input;
189 1.1 christos ops->gpio_config_output = ar9003_gpio_config_output;
190 1.1 christos ops->rfsilent_init = ar9003_rfsilent_init;
191 1.1 christos
192 1.1 christos ops->dma_alloc = ar9003_dma_alloc;
193 1.1 christos ops->dma_free = ar9003_dma_free;
194 1.1 christos ops->rx_enable = ar9003_rx_enable;
195 1.1 christos ops->intr = ar9003_intr;
196 1.1 christos ops->tx = ar9003_tx;
197 1.1 christos
198 1.1 christos ops->set_rf_mode = ar9003_set_rf_mode;
199 1.1 christos ops->rf_bus_request = ar9003_rf_bus_request;
200 1.1 christos ops->rf_bus_release = ar9003_rf_bus_release;
201 1.1 christos ops->set_phy = ar9003_set_phy;
202 1.1 christos ops->set_delta_slope = ar9003_set_delta_slope;
203 1.1 christos ops->enable_antenna_diversity = ar9003_enable_antenna_diversity;
204 1.1 christos ops->init_baseband = ar9003_init_baseband;
205 1.1 christos ops->disable_phy = ar9003_disable_phy;
206 1.1 christos ops->set_rxchains = ar9003_set_rxchains;
207 1.1 christos ops->noisefloor_calib = ar9003_do_noisefloor_calib;
208 1.1 christos ops->do_calib = ar9003_do_calib;
209 1.1 christos ops->next_calib = ar9003_next_calib;
210 1.1 christos ops->hw_init = ar9003_hw_init;
211 1.1 christos
212 1.1 christos ops->set_noise_immunity_level = ar9003_set_noise_immunity_level;
213 1.1 christos ops->enable_ofdm_weak_signal = ar9003_enable_ofdm_weak_signal;
214 1.1 christos ops->disable_ofdm_weak_signal = ar9003_disable_ofdm_weak_signal;
215 1.1 christos ops->set_cck_weak_signal = ar9003_set_cck_weak_signal;
216 1.1 christos ops->set_firstep_level = ar9003_set_firstep_level;
217 1.1 christos ops->set_spur_immunity_level = ar9003_set_spur_immunity_level;
218 1.1 christos
219 1.1 christos /* Set MAC registers offsets. */
220 1.1 christos sc->sc_obs_off = AR_OBS;
221 1.1 christos sc->sc_gpio_input_en_off = AR_GPIO_INPUT_EN_VAL;
222 1.1 christos
223 1.1 christos if (!(sc->sc_flags & ATHN_FLAG_PCIE))
224 1.1 christos athn_config_nonpcie(sc);
225 1.1 christos else
226 1.1 christos athn_config_pcie(sc);
227 1.1 christos
228 1.1 christos /* Determine ROM type and location. */
229 1.1 christos if ((error = ar9003_find_rom(sc)) != 0) {
230 1.1 christos printf("%s: could not find ROM\n", device_xname(sc->sc_dev));
231 1.1 christos return error;
232 1.1 christos }
233 1.1 christos /* Read entire ROM content in memory. */
234 1.1 christos if ((error = ar9003_read_rom(sc)) != 0) {
235 1.1 christos printf("%s: could not read ROM\n", device_xname(sc->sc_dev));
236 1.1 christos return error;
237 1.1 christos }
238 1.1 christos
239 1.1 christos /* Determine if it is a non-enterprise AR9003 card. */
240 1.1 christos if (AR_READ(sc, AR_ENT_OTP) & AR_ENT_OTP_MPSD)
241 1.1 christos sc->sc_flags |= ATHN_FLAG_NON_ENTERPRISE;
242 1.1 christos
243 1.1 christos ops->setup(sc);
244 1.1 christos return 0;
245 1.1 christos }
246 1.1 christos
247 1.1 christos /*
248 1.1 christos * Read 16-bit word from EEPROM.
249 1.1 christos */
250 1.1 christos Static int
251 1.1 christos ar9003_read_eep_word(struct athn_softc *sc, uint32_t addr, uint16_t *val)
252 1.1 christos {
253 1.1 christos uint32_t reg;
254 1.1 christos int ntries;
255 1.1 christos
256 1.1 christos reg = AR_READ(sc, AR_EEPROM_OFFSET(addr));
257 1.1 christos for (ntries = 0; ntries < 1000; ntries++) {
258 1.1 christos reg = AR_READ(sc, AR_EEPROM_STATUS_DATA);
259 1.1 christos if (!(reg & (AR_EEPROM_STATUS_DATA_BUSY |
260 1.1 christos AR_EEPROM_STATUS_DATA_PROT_ACCESS))) {
261 1.1 christos *val = MS(reg, AR_EEPROM_STATUS_DATA_VAL);
262 1.1 christos return 0;
263 1.1 christos }
264 1.1 christos DELAY(10);
265 1.1 christos }
266 1.1 christos *val = 0xffff;
267 1.1 christos return ETIMEDOUT;
268 1.1 christos }
269 1.1 christos
270 1.1 christos /*
271 1.1 christos * Read an arbitrary number of bytes at a specified address in EEPROM.
272 1.1 christos * NB: The address may not be 16-bit aligned.
273 1.1 christos */
274 1.1 christos Static int
275 1.1 christos ar9003_read_eep_data(struct athn_softc *sc, uint32_t addr, void *buf, int len)
276 1.1 christos {
277 1.1 christos uint8_t *dst = buf;
278 1.1 christos uint16_t val;
279 1.1 christos int error;
280 1.1 christos
281 1.1 christos if (len > 0 && (addr & 1)) {
282 1.1 christos /* Deal with non-aligned reads. */
283 1.1 christos addr >>= 1;
284 1.1 christos error = ar9003_read_eep_word(sc, addr, &val);
285 1.1 christos if (error != 0)
286 1.1 christos return error;
287 1.1 christos *dst++ = val & 0xff;
288 1.1 christos addr--;
289 1.1 christos len--;
290 1.1 christos }
291 1.1 christos else
292 1.1 christos addr >>= 1;
293 1.1 christos for (; len >= 2; addr--, len -= 2) {
294 1.1 christos error = ar9003_read_eep_word(sc, addr, &val);
295 1.1 christos if (error != 0)
296 1.1 christos return error;
297 1.1 christos *dst++ = val >> 8;
298 1.1 christos *dst++ = val & 0xff;
299 1.1 christos }
300 1.1 christos if (len > 0) {
301 1.1 christos error = ar9003_read_eep_word(sc, addr, &val);
302 1.1 christos if (error != 0)
303 1.1 christos return error;
304 1.1 christos *dst++ = val >> 8;
305 1.1 christos }
306 1.1 christos return 0;
307 1.1 christos }
308 1.1 christos
309 1.1 christos /*
310 1.1 christos * Read 32-bit word from OTPROM.
311 1.1 christos */
312 1.1 christos Static int
313 1.1 christos ar9003_read_otp_word(struct athn_softc *sc, uint32_t addr, uint32_t *val)
314 1.1 christos {
315 1.1 christos uint32_t reg;
316 1.1 christos int ntries;
317 1.1 christos
318 1.1 christos reg = AR_READ(sc, AR_OTP_BASE(addr));
319 1.1 christos for (ntries = 0; ntries < 1000; ntries++) {
320 1.1 christos reg = AR_READ(sc, AR_OTP_STATUS);
321 1.1 christos if (MS(reg, AR_OTP_STATUS_TYPE) == AR_OTP_STATUS_VALID) {
322 1.1 christos *val = AR_READ(sc, AR_OTP_READ_DATA);
323 1.1 christos return 0;
324 1.1 christos }
325 1.1 christos DELAY(10);
326 1.1 christos }
327 1.1 christos return ETIMEDOUT;
328 1.1 christos }
329 1.1 christos
330 1.1 christos /*
331 1.1 christos * Read an arbitrary number of bytes at a specified address in OTPROM.
332 1.1 christos * NB: The address may not be 32-bit aligned.
333 1.1 christos */
334 1.1 christos Static int
335 1.1 christos ar9003_read_otp_data(struct athn_softc *sc, uint32_t addr, void *buf, int len)
336 1.1 christos {
337 1.1 christos uint8_t *dst = buf;
338 1.1 christos uint32_t val;
339 1.1 christos int error;
340 1.1 christos
341 1.1 christos /* NB: not optimal for non-aligned reads, but correct. */
342 1.1 christos for (; len > 0; addr--, len--) {
343 1.1 christos error = ar9003_read_otp_word(sc, addr >> 2, &val);
344 1.1 christos if (error != 0)
345 1.1 christos return error;
346 1.1 christos *dst++ = (val >> ((addr & 3) * 8)) & 0xff;
347 1.1 christos }
348 1.1 christos return 0;
349 1.1 christos }
350 1.1 christos
351 1.1 christos /*
352 1.1 christos * Determine if the chip has an external EEPROM or an OTPROM and its size.
353 1.1 christos */
354 1.1 christos Static int
355 1.1 christos ar9003_find_rom(struct athn_softc *sc)
356 1.1 christos {
357 1.1 christos struct athn_ops *ops = &sc->sc_ops;
358 1.1 christos uint32_t hdr;
359 1.1 christos int error;
360 1.1 christos
361 1.1 christos /* Try EEPROM. */
362 1.1 christos ops->read_rom_data = ar9003_read_eep_data;
363 1.1 christos
364 1.1 christos sc->sc_eep_size = AR_SREV_9485(sc) ? 4096 : 1024;
365 1.1 christos sc->sc_eep_base = sc->sc_eep_size - 1;
366 1.1 christos error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
367 1.1 christos if (error == 0 && hdr != 0 && hdr != 0xffffffff)
368 1.1 christos return 0;
369 1.1 christos
370 1.1 christos sc->sc_eep_size = 512;
371 1.1 christos sc->sc_eep_base = sc->sc_eep_size - 1;
372 1.1 christos error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
373 1.1 christos if (error == 0 && hdr != 0 && hdr != 0xffffffff)
374 1.1 christos return 0;
375 1.1 christos
376 1.1 christos /* Try OTPROM. */
377 1.1 christos ops->read_rom_data = ar9003_read_otp_data;
378 1.1 christos
379 1.1 christos sc->sc_eep_size = 1024;
380 1.1 christos sc->sc_eep_base = sc->sc_eep_size - 1;
381 1.1 christos error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
382 1.1 christos if (error == 0 && hdr != 0 && hdr != 0xffffffff)
383 1.1 christos return 0;
384 1.1 christos
385 1.1 christos sc->sc_eep_size = 512;
386 1.1 christos sc->sc_eep_base = sc->sc_eep_size - 1;
387 1.1 christos error = ops->read_rom_data(sc, sc->sc_eep_base, &hdr, sizeof(hdr));
388 1.1 christos if (error == 0 && hdr != 0 && hdr != 0xffffffff)
389 1.1 christos return 0;
390 1.1 christos
391 1.1 christos return EIO; /* Not found. */
392 1.1 christos }
393 1.1 christos
394 1.1 christos Static int
395 1.1 christos ar9003_restore_rom_block(struct athn_softc *sc, uint8_t alg, uint8_t ref,
396 1.1 christos const uint8_t *buf, size_t len)
397 1.1 christos {
398 1.1 christos const uint8_t *def, *ptr, *end;
399 1.1 christos uint8_t *eep = sc->sc_eep;
400 1.1 christos size_t off, clen;
401 1.1 christos
402 1.1 christos if (alg == AR_EEP_COMPRESS_BLOCK) {
403 1.1 christos /* Block contains chunks that shadow ROM template. */
404 1.1 christos def = sc->sc_ops.get_rom_template(sc, ref);
405 1.1 christos if (def == NULL) {
406 1.1 christos DPRINTFN(DBG_INIT, sc, "unknown template image %d\n",
407 1.1 christos ref);
408 1.1 christos return EINVAL;
409 1.1 christos }
410 1.1 christos /* Start with template. */
411 1.1 christos memcpy(eep, def, sc->sc_eep_size);
412 1.1 christos /* Shadow template with chunks. */
413 1.1 christos off = 0; /* Offset in ROM image. */
414 1.1 christos ptr = buf; /* Offset in block. */
415 1.1 christos end = buf + len;
416 1.1 christos /* Process chunks. */
417 1.1 christos while (ptr + 2 <= end) {
418 1.1 christos off += *ptr++; /* Gap with previous chunk. */
419 1.1 christos clen = *ptr++; /* Chunk length. */
420 1.1 christos /* Make sure block is large enough. */
421 1.1 christos if (ptr + clen > end)
422 1.1 christos return EINVAL;
423 1.1 christos /* Make sure chunk fits in ROM image. */
424 1.1 christos if (off + clen > sc->sc_eep_size)
425 1.1 christos return EINVAL;
426 1.1 christos /* Restore chunk. */
427 1.1 christos DPRINTFN(DBG_INIT, sc, "ROM chunk @%zd/%zd\n",
428 1.1 christos off, clen);
429 1.1 christos memcpy(&eep[off], ptr, clen);
430 1.1 christos ptr += clen;
431 1.1 christos off += clen;
432 1.1 christos }
433 1.1 christos }
434 1.1 christos else if (alg == AR_EEP_COMPRESS_NONE) {
435 1.1 christos /* Block contains full ROM image. */
436 1.1 christos if (len != sc->sc_eep_size) {
437 1.1 christos DPRINTFN(DBG_INIT, sc, "block length mismatch %zd\n",
438 1.1 christos len);
439 1.1 christos return EINVAL;
440 1.1 christos }
441 1.1 christos memcpy(eep, buf, len);
442 1.1 christos }
443 1.1 christos return 0;
444 1.1 christos }
445 1.1 christos
446 1.1 christos Static int
447 1.1 christos ar9003_read_rom(struct athn_softc *sc)
448 1.1 christos {
449 1.1 christos struct athn_ops *ops = &sc->sc_ops;
450 1.1 christos uint8_t *buf, *ptr, alg, ref;
451 1.1 christos uint16_t sum, rsum;
452 1.1 christos uint32_t hdr;
453 1.1 christos int error, addr;
454 1.1 christos size_t len, i, j;
455 1.1 christos
456 1.1 christos /* Allocate space to store ROM in host memory. */
457 1.1 christos sc->sc_eep = malloc(sc->sc_eep_size, M_DEVBUF, M_NOWAIT);
458 1.1 christos if (sc->sc_eep == NULL)
459 1.1 christos return ENOMEM;
460 1.1 christos
461 1.1 christos /* Allocate temporary buffer to store ROM blocks. */
462 1.1 christos buf = malloc(2048, M_DEVBUF, M_NOWAIT);
463 1.1 christos if (buf == NULL)
464 1.1 christos return ENOMEM;
465 1.1 christos
466 1.1 christos /* Restore vendor-specified ROM blocks. */
467 1.1 christos addr = sc->sc_eep_base;
468 1.1 christos for (i = 0; i < 100; i++) {
469 1.1 christos /* Read block header. */
470 1.1 christos error = ops->read_rom_data(sc, addr, &hdr, sizeof(hdr));
471 1.1 christos if (error != 0)
472 1.1 christos break;
473 1.1 christos if (hdr == 0 || hdr == 0xffffffff)
474 1.1 christos break;
475 1.1 christos addr -= sizeof(hdr);
476 1.1 christos
477 1.1 christos /* Extract bits from header. */
478 1.1 christos ptr = (uint8_t *)&hdr;
479 1.1 christos alg = (ptr[0] & 0xe0) >> 5;
480 1.1 christos ref = (ptr[1] & 0x80) >> 2 | (ptr[0] & 0x1f);
481 1.1 christos len = (ptr[1] & 0x7f) << 4 | (ptr[2] & 0xf0) >> 4;
482 1.1 christos DPRINTFN(DBG_INIT, sc,
483 1.1 christos "ROM block %zd: alg=%d ref=%d len=%zd\n",
484 1.1 christos i, alg, ref, len);
485 1.1 christos
486 1.1 christos /* Read block data (len <= 0x7ff). */
487 1.1 christos error = ops->read_rom_data(sc, addr, buf, len);
488 1.1 christos if (error != 0)
489 1.1 christos break;
490 1.1 christos addr -= len;
491 1.1 christos
492 1.1 christos /* Read block checksum. */
493 1.1 christos error = ops->read_rom_data(sc, addr, &sum, sizeof(sum));
494 1.1 christos if (error != 0)
495 1.1 christos break;
496 1.1 christos addr -= sizeof(sum);
497 1.1 christos
498 1.1 christos /* Compute block checksum. */
499 1.1 christos rsum = 0;
500 1.1 christos for (j = 0; j < len; j++)
501 1.1 christos rsum += buf[j];
502 1.1 christos /* Compare to that in ROM. */
503 1.1 christos if (le16toh(sum) != rsum) {
504 1.1 christos DPRINTFN(DBG_INIT, sc,
505 1.1 christos "bad block checksum 0x%x/0x%x\n",
506 1.1 christos le16toh(sum), rsum);
507 1.1 christos continue; /* Skip bad block. */
508 1.1 christos }
509 1.1 christos /* Checksum is correct, restore block. */
510 1.1 christos ar9003_restore_rom_block(sc, alg, ref, buf, len);
511 1.1 christos }
512 1.1 christos #if BYTE_ORDER == BIG_ENDIAN
513 1.1 christos /* NB: ROM is always little endian. */
514 1.1 christos if (error == 0)
515 1.1 christos ops->swap_rom(sc);
516 1.1 christos #endif
517 1.1 christos free(buf, M_DEVBUF);
518 1.1 christos return error;
519 1.1 christos }
520 1.1 christos
521 1.1 christos /*
522 1.1 christos * Access to General Purpose Input/Output ports.
523 1.1 christos */
524 1.1 christos Static int
525 1.1 christos ar9003_gpio_read(struct athn_softc *sc, int pin)
526 1.1 christos {
527 1.1 christos
528 1.1 christos KASSERT(pin < sc->sc_ngpiopins);
529 1.1 christos return ((AR_READ(sc, AR_GPIO_IN) & AR9300_GPIO_IN_VAL) &
530 1.1 christos (1 << pin)) != 0;
531 1.1 christos }
532 1.1 christos
533 1.1 christos Static void
534 1.1 christos ar9003_gpio_write(struct athn_softc *sc, int pin, int set)
535 1.1 christos {
536 1.1 christos uint32_t reg;
537 1.1 christos
538 1.1 christos KASSERT(pin < sc->sc_ngpiopins);
539 1.1 christos reg = AR_READ(sc, AR_GPIO_IN_OUT);
540 1.1 christos if (set)
541 1.1 christos reg |= 1 << pin;
542 1.1 christos else
543 1.1 christos reg &= ~(1 << pin);
544 1.1 christos AR_WRITE(sc, AR_GPIO_IN_OUT, reg);
545 1.1 christos AR_WRITE_BARRIER(sc);
546 1.1 christos }
547 1.1 christos
548 1.1 christos Static void
549 1.1 christos ar9003_gpio_config_input(struct athn_softc *sc, int pin)
550 1.1 christos {
551 1.1 christos uint32_t reg;
552 1.1 christos
553 1.1 christos reg = AR_READ(sc, AR_GPIO_OE_OUT);
554 1.1 christos reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
555 1.1 christos reg |= AR_GPIO_OE_OUT_DRV_NO << (pin * 2);
556 1.1 christos AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
557 1.1 christos AR_WRITE_BARRIER(sc);
558 1.1 christos }
559 1.1 christos
560 1.1 christos Static void
561 1.1 christos ar9003_gpio_config_output(struct athn_softc *sc, int pin, int type)
562 1.1 christos {
563 1.1 christos uint32_t reg;
564 1.1 christos int mux, off;
565 1.1 christos
566 1.1 christos mux = pin / 6;
567 1.1 christos off = pin % 6;
568 1.1 christos
569 1.1 christos reg = AR_READ(sc, AR_GPIO_OUTPUT_MUX(mux));
570 1.1 christos reg &= ~(0x1f << (off * 5));
571 1.1 christos reg |= (type & 0x1f) << (off * 5);
572 1.1 christos AR_WRITE(sc, AR_GPIO_OUTPUT_MUX(mux), reg);
573 1.1 christos
574 1.1 christos reg = AR_READ(sc, AR_GPIO_OE_OUT);
575 1.1 christos reg &= ~(AR_GPIO_OE_OUT_DRV_M << (pin * 2));
576 1.1 christos reg |= AR_GPIO_OE_OUT_DRV_ALL << (pin * 2);
577 1.1 christos AR_WRITE(sc, AR_GPIO_OE_OUT, reg);
578 1.1 christos AR_WRITE_BARRIER(sc);
579 1.1 christos }
580 1.1 christos
581 1.1 christos Static void
582 1.1 christos ar9003_rfsilent_init(struct athn_softc *sc)
583 1.1 christos {
584 1.1 christos uint32_t reg;
585 1.1 christos
586 1.1 christos /* Configure hardware radio switch. */
587 1.1 christos AR_SETBITS(sc, AR_GPIO_INPUT_EN_VAL, AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
588 1.1 christos reg = AR_READ(sc, AR_GPIO_INPUT_MUX2);
589 1.1 christos reg = RW(reg, AR_GPIO_INPUT_MUX2_RFSILENT, 0);
590 1.1 christos AR_WRITE(sc, AR_GPIO_INPUT_MUX2, reg);
591 1.1 christos ar9003_gpio_config_input(sc, sc->sc_rfsilent_pin);
592 1.1 christos AR_SETBITS(sc, AR_PHY_TEST, AR_PHY_TEST_RFSILENT_BB);
593 1.1 christos if (!(sc->sc_flags & ATHN_FLAG_RFSILENT_REVERSED)) {
594 1.1 christos AR_SETBITS(sc, AR_GPIO_INTR_POL,
595 1.1 christos AR_GPIO_INTR_POL_PIN(sc->sc_rfsilent_pin));
596 1.1 christos }
597 1.1 christos AR_WRITE_BARRIER(sc);
598 1.1 christos }
599 1.1 christos
600 1.1 christos Static int
601 1.1 christos ar9003_dma_alloc(struct athn_softc *sc)
602 1.1 christos {
603 1.1 christos int error;
604 1.1 christos
605 1.1 christos error = ar9003_tx_alloc(sc);
606 1.1 christos if (error != 0)
607 1.1 christos return error;
608 1.1 christos
609 1.1 christos error = ar9003_rx_alloc(sc, ATHN_QID_LP, AR9003_RX_LP_QDEPTH);
610 1.1 christos if (error != 0)
611 1.1 christos return error;
612 1.1 christos
613 1.1 christos error = ar9003_rx_alloc(sc, ATHN_QID_HP, AR9003_RX_HP_QDEPTH);
614 1.1 christos if (error != 0)
615 1.1 christos return error;
616 1.1 christos
617 1.1 christos return 0;
618 1.1 christos }
619 1.1 christos
620 1.1 christos Static void
621 1.1 christos ar9003_dma_free(struct athn_softc *sc)
622 1.1 christos {
623 1.1 christos
624 1.1 christos ar9003_tx_free(sc);
625 1.1 christos ar9003_rx_free(sc, ATHN_QID_LP);
626 1.1 christos ar9003_rx_free(sc, ATHN_QID_HP);
627 1.1 christos }
628 1.1 christos
629 1.1 christos Static int
630 1.1 christos ar9003_tx_alloc(struct athn_softc *sc)
631 1.1 christos {
632 1.1 christos struct athn_tx_buf *bf;
633 1.1 christos bus_size_t size;
634 1.1 christos int error, nsegs, i;
635 1.1 christos
636 1.1 christos /*
637 1.1 christos * Allocate Tx status ring.
638 1.1 christos */
639 1.1 christos size = AR9003_NTXSTATUS * sizeof(struct ar_tx_status);
640 1.1 christos
641 1.1 christos error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
642 1.1 christos BUS_DMA_NOWAIT, &sc->sc_txsmap);
643 1.1 christos if (error != 0)
644 1.1 christos goto fail;
645 1.1 christos
646 1.1 christos error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_txsseg, 1,
647 1.1 christos // XXX &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
648 1.1 christos &nsegs, BUS_DMA_NOWAIT);
649 1.1 christos if (error != 0)
650 1.1 christos goto fail;
651 1.1 christos
652 1.1 christos error = bus_dmamem_map(sc->sc_dmat, &sc->sc_txsseg, 1, size,
653 1.1 christos (void **)&sc->sc_txsring, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
654 1.1 christos if (error != 0)
655 1.1 christos goto fail;
656 1.1 christos
657 1.1 christos error = bus_dmamap_load_raw(sc->sc_dmat, sc->sc_txsmap, &sc->sc_txsseg,
658 1.1 christos 1, size, BUS_DMA_NOWAIT | BUS_DMA_READ);
659 1.1 christos if (error != 0)
660 1.1 christos goto fail;
661 1.1 christos
662 1.1 christos /*
663 1.1 christos * Allocate a pool of Tx descriptors shared between all Tx queues.
664 1.1 christos */
665 1.1 christos size = ATHN_NTXBUFS * sizeof(struct ar_tx_desc);
666 1.1 christos
667 1.1 christos error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
668 1.1 christos BUS_DMA_NOWAIT, &sc->sc_map);
669 1.1 christos if (error != 0)
670 1.1 christos goto fail;
671 1.1 christos
672 1.1 christos error = bus_dmamem_alloc(sc->sc_dmat, size, 4, 0, &sc->sc_seg, 1,
673 1.1 christos // XXX &nsegs, BUS_DMA_NOWAIT | BUS_DMA_ZERO);
674 1.1 christos &nsegs, BUS_DMA_NOWAIT);
675 1.1 christos if (error != 0)
676 1.1 christos goto fail;
677 1.1 christos
678 1.1 christos error = bus_dmamem_map(sc->sc_dmat, &sc->sc_seg, 1, size,
679 1.1 christos (void **)&sc->sc_descs, BUS_DMA_NOWAIT | BUS_DMA_COHERENT);
680 1.1 christos if (error != 0)
681 1.1 christos goto fail;
682 1.1 christos
683 1.1 christos error = bus_dmamap_load_raw(sc->sc_dmat, sc->sc_map, &sc->sc_seg, 1, size,
684 1.1 christos BUS_DMA_NOWAIT | BUS_DMA_WRITE);
685 1.1 christos if (error != 0)
686 1.1 christos goto fail;
687 1.1 christos
688 1.1 christos SIMPLEQ_INIT(&sc->sc_txbufs);
689 1.1 christos for (i = 0; i < ATHN_NTXBUFS; i++) {
690 1.1 christos bf = &sc->sc_txpool[i];
691 1.1 christos
692 1.1 christos error = bus_dmamap_create(sc->sc_dmat, ATHN_TXBUFSZ,
693 1.1 christos AR9003_MAX_SCATTER, ATHN_TXBUFSZ, 0, BUS_DMA_NOWAIT,
694 1.1 christos &bf->bf_map);
695 1.1 christos if (error != 0) {
696 1.1 christos printf("%s: could not create Tx buf DMA map\n",
697 1.1 christos device_xname(sc->sc_dev));
698 1.1 christos goto fail;
699 1.1 christos }
700 1.1 christos
701 1.1 christos bf->bf_descs = &((struct ar_tx_desc *)sc->sc_descs)[i];
702 1.1 christos bf->bf_daddr = sc->sc_map->dm_segs[0].ds_addr +
703 1.1 christos i * sizeof(struct ar_tx_desc);
704 1.1 christos
705 1.1 christos SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
706 1.1 christos }
707 1.1 christos return 0;
708 1.1 christos fail:
709 1.1 christos ar9003_tx_free(sc);
710 1.1 christos return error;
711 1.1 christos }
712 1.1 christos
713 1.1 christos Static void
714 1.1 christos ar9003_tx_free(struct athn_softc *sc)
715 1.1 christos {
716 1.1 christos struct athn_tx_buf *bf;
717 1.1 christos int i;
718 1.1 christos
719 1.1 christos for (i = 0; i < ATHN_NTXBUFS; i++) {
720 1.1 christos bf = &sc->sc_txpool[i];
721 1.1 christos
722 1.1 christos if (bf->bf_map != NULL)
723 1.1 christos bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
724 1.1 christos }
725 1.1 christos /* Free Tx descriptors. */
726 1.1 christos if (sc->sc_map != NULL) {
727 1.1 christos if (sc->sc_descs != NULL) {
728 1.1 christos bus_dmamap_unload(sc->sc_dmat, sc->sc_map);
729 1.1 christos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_descs,
730 1.1 christos ATHN_NTXBUFS * sizeof(struct ar_tx_desc));
731 1.1 christos bus_dmamem_free(sc->sc_dmat, &sc->sc_seg, 1);
732 1.1 christos }
733 1.1 christos bus_dmamap_destroy(sc->sc_dmat, sc->sc_map);
734 1.1 christos }
735 1.1 christos /* Free Tx status ring. */
736 1.1 christos if (sc->sc_txsmap != NULL) {
737 1.1 christos if (sc->sc_txsring != NULL) {
738 1.1 christos bus_dmamap_unload(sc->sc_dmat, sc->sc_txsmap);
739 1.1 christos bus_dmamem_unmap(sc->sc_dmat, (void *)sc->sc_txsring,
740 1.1 christos AR9003_NTXSTATUS * sizeof(struct ar_tx_status));
741 1.1 christos bus_dmamem_free(sc->sc_dmat, &sc->sc_txsseg, 1);
742 1.1 christos }
743 1.1 christos bus_dmamap_destroy(sc->sc_dmat, sc->sc_txsmap);
744 1.1 christos }
745 1.1 christos }
746 1.1 christos
747 1.1 christos Static int
748 1.1 christos ar9003_rx_alloc(struct athn_softc *sc, int qid, int count)
749 1.1 christos {
750 1.1 christos struct athn_rxq *rxq = &sc->sc_rxq[qid];
751 1.1 christos struct athn_rx_buf *bf;
752 1.1 christos struct ar_rx_status *ds;
753 1.1 christos int error, i;
754 1.1 christos
755 1.1 christos rxq->bf = malloc(count * sizeof(*bf), M_DEVBUF, M_NOWAIT | M_ZERO);
756 1.1 christos if (rxq->bf == NULL)
757 1.1 christos return ENOMEM;
758 1.1 christos
759 1.1 christos rxq->count = count;
760 1.1 christos
761 1.1 christos for (i = 0; i < rxq->count; i++) {
762 1.1 christos bf = &rxq->bf[i];
763 1.1 christos
764 1.1 christos error = bus_dmamap_create(sc->sc_dmat, ATHN_RXBUFSZ, 1,
765 1.1 christos ATHN_RXBUFSZ, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
766 1.1 christos &bf->bf_map);
767 1.1 christos if (error != 0) {
768 1.1 christos printf("%s: could not create Rx buf DMA map\n",
769 1.1 christos device_xname(sc->sc_dev));
770 1.1 christos goto fail;
771 1.1 christos }
772 1.1 christos /*
773 1.1 christos * Assumes MCLGETI returns cache-line-size aligned buffers.
774 1.1 christos */
775 1.1 christos bf->bf_m = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
776 1.1 christos if (bf->bf_m == NULL) {
777 1.1 christos printf("%s: could not allocate Rx mbuf\n",
778 1.1 christos device_xname(sc->sc_dev));
779 1.1 christos error = ENOBUFS;
780 1.1 christos goto fail;
781 1.1 christos }
782 1.1 christos
783 1.1 christos error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
784 1.1 christos mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
785 1.1 christos BUS_DMA_NOWAIT);
786 1.1 christos if (error != 0) {
787 1.1 christos printf("%s: could not DMA map Rx buffer\n",
788 1.1 christos device_xname(sc->sc_dev));
789 1.1 christos goto fail;
790 1.1 christos }
791 1.1 christos
792 1.1 christos ds = mtod(bf->bf_m, struct ar_rx_status *);
793 1.1 christos memset(ds, 0, sizeof(*ds));
794 1.1 christos bf->bf_desc = ds;
795 1.1 christos bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
796 1.1 christos
797 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
798 1.1 christos BUS_DMASYNC_PREREAD);
799 1.1 christos }
800 1.1 christos return 0;
801 1.1 christos fail:
802 1.1 christos ar9003_rx_free(sc, qid);
803 1.1 christos return error;
804 1.1 christos }
805 1.1 christos
806 1.1 christos Static void
807 1.1 christos ar9003_rx_free(struct athn_softc *sc, int qid)
808 1.1 christos {
809 1.1 christos struct athn_rxq *rxq = &sc->sc_rxq[qid];
810 1.1 christos struct athn_rx_buf *bf;
811 1.1 christos int i;
812 1.1 christos
813 1.1 christos if (rxq->bf == NULL)
814 1.1 christos return;
815 1.1 christos for (i = 0; i < rxq->count; i++) {
816 1.1 christos bf = &rxq->bf[i];
817 1.1 christos
818 1.1 christos if (bf->bf_map != NULL)
819 1.1 christos bus_dmamap_destroy(sc->sc_dmat, bf->bf_map);
820 1.1 christos if (bf->bf_m != NULL)
821 1.1 christos m_freem(bf->bf_m);
822 1.1 christos }
823 1.1 christos free(rxq->bf, M_DEVBUF);
824 1.1 christos }
825 1.1 christos
826 1.1 christos PUBLIC void
827 1.1 christos ar9003_reset_txsring(struct athn_softc *sc)
828 1.1 christos {
829 1.1 christos
830 1.1 christos sc->sc_txscur = 0;
831 1.1 christos memset(sc->sc_txsring, 0, AR9003_NTXSTATUS * sizeof(struct ar_tx_status));
832 1.1 christos AR_WRITE(sc, AR_Q_STATUS_RING_START,
833 1.1 christos sc->sc_txsmap->dm_segs[0].ds_addr);
834 1.1 christos AR_WRITE(sc, AR_Q_STATUS_RING_END,
835 1.1 christos sc->sc_txsmap->dm_segs[0].ds_addr + sc->sc_txsmap->dm_segs[0].ds_len);
836 1.1 christos AR_WRITE_BARRIER(sc);
837 1.1 christos }
838 1.1 christos
839 1.1 christos Static void
840 1.1 christos ar9003_rx_enable(struct athn_softc *sc)
841 1.1 christos {
842 1.1 christos struct athn_rxq *rxq;
843 1.1 christos struct athn_rx_buf *bf;
844 1.1 christos struct ar_rx_status *ds;
845 1.1 christos uint32_t reg;
846 1.1 christos int qid, i;
847 1.1 christos
848 1.1 christos reg = AR_READ(sc, AR_RXBP_THRESH);
849 1.1 christos reg = RW(reg, AR_RXBP_THRESH_HP, 1);
850 1.1 christos reg = RW(reg, AR_RXBP_THRESH_LP, 1);
851 1.1 christos AR_WRITE(sc, AR_RXBP_THRESH, reg);
852 1.1 christos
853 1.1 christos /* Set Rx buffer size. */
854 1.1 christos AR_WRITE(sc, AR_DATABUF_SIZE, ATHN_RXBUFSZ - sizeof(*ds));
855 1.1 christos
856 1.1 christos for (qid = 0; qid < 2; qid++) {
857 1.1 christos rxq = &sc->sc_rxq[qid];
858 1.1 christos
859 1.1 christos /* Setup Rx status descriptors. */
860 1.1 christos SIMPLEQ_INIT(&rxq->head);
861 1.1 christos for (i = 0; i < rxq->count; i++) {
862 1.1 christos bf = &rxq->bf[i];
863 1.1 christos ds = bf->bf_desc;
864 1.1 christos
865 1.1 christos memset(ds, 0, sizeof(*ds));
866 1.1 christos if (qid == ATHN_QID_LP)
867 1.1 christos AR_WRITE(sc, AR_LP_RXDP, bf->bf_daddr);
868 1.1 christos else
869 1.1 christos AR_WRITE(sc, AR_HP_RXDP, bf->bf_daddr);
870 1.1 christos AR_WRITE_BARRIER(sc);
871 1.1 christos SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
872 1.1 christos }
873 1.1 christos }
874 1.1 christos /* Enable Rx. */
875 1.1 christos AR_WRITE(sc, AR_CR, 0);
876 1.1 christos AR_WRITE_BARRIER(sc);
877 1.1 christos }
878 1.1 christos
879 1.1 christos Static void
880 1.1 christos ar9003_rx_radiotap(struct athn_softc *sc, struct mbuf *m,
881 1.1 christos struct ar_rx_status *ds)
882 1.1 christos {
883 1.1 christos struct athn_rx_radiotap_header *tap = &sc->sc_rxtap;
884 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
885 1.1 christos uint64_t tsf;
886 1.1 christos uint32_t tstamp;
887 1.1 christos uint8_t rate;
888 1.1 christos
889 1.1 christos /* Extend the 15-bit timestamp from Rx status to 64-bit TSF. */
890 1.1 christos tstamp = ds->ds_status3;
891 1.1 christos tsf = AR_READ(sc, AR_TSF_U32);
892 1.1 christos tsf = tsf << 32 | AR_READ(sc, AR_TSF_L32);
893 1.1 christos if ((tsf & 0x7fff) < tstamp)
894 1.1 christos tsf -= 0x8000;
895 1.1 christos tsf = (tsf & ~0x7fff) | tstamp;
896 1.1 christos
897 1.1 christos tap->wr_flags = IEEE80211_RADIOTAP_F_FCS;
898 1.1 christos tap->wr_tsft = htole64(tsf);
899 1.1 christos tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
900 1.1 christos tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
901 1.1 christos tap->wr_dbm_antsignal = MS(ds->ds_status5, AR_RXS5_RSSI_COMBINED);
902 1.1 christos /* XXX noise. */
903 1.1 christos tap->wr_antenna = MS(ds->ds_status4, AR_RXS4_ANTENNA);
904 1.1 christos tap->wr_rate = 0; /* In case it can't be found below. */
905 1.1 christos rate = MS(ds->ds_status1, AR_RXS1_RATE);
906 1.1 christos if (rate & 0x80) { /* HT. */
907 1.1 christos /* Bit 7 set means HT MCS instead of rate. */
908 1.1 christos tap->wr_rate = rate;
909 1.1 christos if (!(ds->ds_status4 & AR_RXS4_GI))
910 1.1 christos tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
911 1.1 christos
912 1.1 christos }
913 1.1 christos else if (rate & 0x10) { /* CCK. */
914 1.1 christos if (rate & 0x04)
915 1.1 christos tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
916 1.1 christos switch (rate & ~0x14) {
917 1.1 christos case 0xb: tap->wr_rate = 2; break;
918 1.1 christos case 0xa: tap->wr_rate = 4; break;
919 1.1 christos case 0x9: tap->wr_rate = 11; break;
920 1.1 christos case 0x8: tap->wr_rate = 22; break;
921 1.1 christos }
922 1.1 christos }
923 1.1 christos else { /* OFDM. */
924 1.1 christos switch (rate) {
925 1.1 christos case 0xb: tap->wr_rate = 12; break;
926 1.1 christos case 0xf: tap->wr_rate = 18; break;
927 1.1 christos case 0xa: tap->wr_rate = 24; break;
928 1.1 christos case 0xe: tap->wr_rate = 36; break;
929 1.1 christos case 0x9: tap->wr_rate = 48; break;
930 1.1 christos case 0xd: tap->wr_rate = 72; break;
931 1.1 christos case 0x8: tap->wr_rate = 96; break;
932 1.1 christos case 0xc: tap->wr_rate = 108; break;
933 1.1 christos }
934 1.1 christos }
935 1.1 christos bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m);
936 1.1 christos }
937 1.1 christos
938 1.1 christos Static int
939 1.1 christos ar9003_rx_process(struct athn_softc *sc, int qid)
940 1.1 christos {
941 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
942 1.1 christos struct ifnet *ifp = &sc->sc_if;
943 1.1 christos struct athn_rxq *rxq = &sc->sc_rxq[qid];
944 1.1 christos struct athn_rx_buf *bf;
945 1.1 christos struct ar_rx_status *ds;
946 1.1 christos struct ieee80211_frame *wh;
947 1.1 christos struct ieee80211_node *ni;
948 1.1 christos struct mbuf *m, *m1;
949 1.1 christos size_t len;
950 1.1 christos u_int32_t rstamp;
951 1.1 christos int error, rssi;
952 1.1 christos
953 1.1 christos bf = SIMPLEQ_FIRST(&rxq->head);
954 1.1 christos if (__predict_false(bf == NULL)) { /* Should not happen. */
955 1.1 christos printf("%s: Rx queue is empty!\n", device_xname(sc->sc_dev));
956 1.1 christos return ENOENT;
957 1.1 christos }
958 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
959 1.1 christos BUS_DMASYNC_POSTREAD);
960 1.1 christos
961 1.1 christos ds = mtod(bf->bf_m, struct ar_rx_status *);
962 1.1 christos if (!(ds->ds_status1 & AR_RXS1_DONE))
963 1.1 christos return EBUSY;
964 1.1 christos
965 1.1 christos /* Check that it is a valid Rx status descriptor. */
966 1.1 christos if ((ds->ds_info & (AR_RXI_DESC_ID_M | AR_RXI_DESC_TX |
967 1.1 christos AR_RXI_CTRL_STAT)) != SM(AR_RXI_DESC_ID, AR_VENDOR_ATHEROS))
968 1.1 christos goto skip;
969 1.1 christos
970 1.1 christos if (!(ds->ds_status11 & AR_RXS11_FRAME_OK)) {
971 1.1 christos if (ds->ds_status11 & AR_RXS11_CRC_ERR)
972 1.1 christos DPRINTFN(DBG_RX, sc, "CRC error\n");
973 1.1 christos else if (ds->ds_status11 & AR_RXS11_PHY_ERR)
974 1.1 christos DPRINTFN(DBG_RX, sc, "PHY error=0x%x\n",
975 1.1 christos MS(ds->ds_status11, AR_RXS11_PHY_ERR_CODE));
976 1.1 christos else if (ds->ds_status11 & AR_RXS11_DECRYPT_CRC_ERR)
977 1.1 christos DPRINTFN(DBG_RX, sc, "Decryption CRC error\n");
978 1.1 christos else if (ds->ds_status11 & AR_RXS11_MICHAEL_ERR) {
979 1.1 christos DPRINTFN(DBG_RX, sc, "Michael MIC failure\n");
980 1.1 christos /* Report Michael MIC failures to net80211. */
981 1.1 christos
982 1.1 christos len = MS(ds->ds_status2, AR_RXS2_DATA_LEN);
983 1.1 christos m = bf->bf_m;
984 1.1 christos m->m_pkthdr.rcvif = ifp;
985 1.1 christos m->m_data = (void *)&ds[1];
986 1.1 christos m->m_pkthdr.len = m->m_len = len;
987 1.1 christos wh = mtod(m, struct ieee80211_frame *);
988 1.1 christos
989 1.1 christos ieee80211_notify_michael_failure(ic, wh,
990 1.1 christos 0 /* XXX: keyix */);
991 1.1 christos }
992 1.1 christos ifp->if_ierrors++;
993 1.1 christos goto skip;
994 1.1 christos }
995 1.1 christos
996 1.1 christos len = MS(ds->ds_status2, AR_RXS2_DATA_LEN);
997 1.1 christos if (__predict_false(len < IEEE80211_MIN_LEN ||
998 1.1 christos len > ATHN_RXBUFSZ - sizeof(*ds))) {
999 1.1 christos DPRINTFN(DBG_RX, sc, "corrupted descriptor length=%zd\n",
1000 1.1 christos len);
1001 1.1 christos ifp->if_ierrors++;
1002 1.1 christos goto skip;
1003 1.1 christos }
1004 1.1 christos
1005 1.1 christos /* Allocate a new Rx buffer. */
1006 1.1 christos m1 = MCLGETI(NULL, M_DONTWAIT, NULL, ATHN_RXBUFSZ);
1007 1.1 christos if (__predict_false(m1 == NULL)) {
1008 1.1 christos ic->ic_stats.is_rx_nobuf++;
1009 1.1 christos ifp->if_ierrors++;
1010 1.1 christos goto skip;
1011 1.1 christos }
1012 1.1 christos
1013 1.1 christos /* Unmap the old Rx buffer. */
1014 1.1 christos bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
1015 1.1 christos
1016 1.1 christos /* Map the new Rx buffer. */
1017 1.1 christos error = bus_dmamap_load(sc->sc_dmat, bf->bf_map, mtod(m1, void *),
1018 1.1 christos ATHN_RXBUFSZ, NULL, BUS_DMA_NOWAIT | BUS_DMA_READ);
1019 1.1 christos if (__predict_false(error != 0)) {
1020 1.1 christos m_freem(m1);
1021 1.1 christos
1022 1.1 christos /* Remap the old Rx buffer or panic. */
1023 1.1 christos error = bus_dmamap_load(sc->sc_dmat, bf->bf_map,
1024 1.1 christos mtod(bf->bf_m, void *), ATHN_RXBUFSZ, NULL,
1025 1.1 christos BUS_DMA_NOWAIT | BUS_DMA_READ);
1026 1.1 christos KASSERT(error != 0);
1027 1.1 christos bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
1028 1.1 christos ifp->if_ierrors++;
1029 1.1 christos goto skip;
1030 1.1 christos }
1031 1.1 christos bf->bf_desc = mtod(m1, struct ar_rx_status *);
1032 1.1 christos bf->bf_daddr = bf->bf_map->dm_segs[0].ds_addr;
1033 1.1 christos
1034 1.1 christos m = bf->bf_m;
1035 1.1 christos bf->bf_m = m1;
1036 1.1 christos
1037 1.1 christos /* Finalize mbuf. */
1038 1.1 christos m->m_pkthdr.rcvif = ifp;
1039 1.1 christos /* Strip Rx status descriptor from head. */
1040 1.1 christos m->m_data = (void *)&ds[1];
1041 1.1 christos m->m_pkthdr.len = m->m_len = len;
1042 1.1 christos
1043 1.1 christos /* Grab a reference to the source node. */
1044 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1045 1.1 christos ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh);
1046 1.1 christos
1047 1.1 christos /* Remove any HW padding after the 802.11 header. */
1048 1.1 christos if (!(wh->i_fc[0] & IEEE80211_FC0_TYPE_CTL)) {
1049 1.1 christos u_int hdrlen = ieee80211_anyhdrsize(wh);
1050 1.1 christos if (hdrlen & 3) {
1051 1.1 christos ovbcopy(wh, (uint8_t *)wh + 2, hdrlen);
1052 1.1 christos m_adj(m, 2);
1053 1.1 christos }
1054 1.1 christos }
1055 1.1 christos if (__predict_false(sc->sc_drvbpf != NULL))
1056 1.1 christos ar9003_rx_radiotap(sc, m, ds);
1057 1.1 christos /* Trim 802.11 FCS after radiotap. */
1058 1.1 christos m_adj(m, -IEEE80211_CRC_LEN);
1059 1.1 christos
1060 1.1 christos /* Send the frame to the 802.11 layer. */
1061 1.1 christos rssi = MS(ds->ds_status5, AR_RXS5_RSSI_COMBINED);
1062 1.1 christos rstamp = ds->ds_status3;
1063 1.1 christos ieee80211_input(ic, m, ni, rssi, rstamp);
1064 1.1 christos
1065 1.1 christos /* Node is no longer needed. */
1066 1.1 christos ieee80211_free_node(ni);
1067 1.1 christos
1068 1.1 christos skip:
1069 1.1 christos /* Unlink this descriptor from head. */
1070 1.1 christos SIMPLEQ_REMOVE_HEAD(&rxq->head, bf_list);
1071 1.1 christos memset(bf->bf_desc, 0, sizeof(*ds));
1072 1.1 christos
1073 1.1 christos /* Re-use this descriptor and link it to tail. */
1074 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, ATHN_RXBUFSZ,
1075 1.1 christos BUS_DMASYNC_PREREAD);
1076 1.1 christos
1077 1.1 christos if (qid == ATHN_QID_LP)
1078 1.1 christos AR_WRITE(sc, AR_LP_RXDP, bf->bf_daddr);
1079 1.1 christos else
1080 1.1 christos AR_WRITE(sc, AR_HP_RXDP, bf->bf_daddr);
1081 1.1 christos AR_WRITE_BARRIER(sc);
1082 1.1 christos SIMPLEQ_INSERT_TAIL(&rxq->head, bf, bf_list);
1083 1.1 christos
1084 1.1 christos /* Re-enable Rx. */
1085 1.1 christos AR_WRITE(sc, AR_CR, 0);
1086 1.1 christos AR_WRITE_BARRIER(sc);
1087 1.1 christos return 0;
1088 1.1 christos }
1089 1.1 christos
1090 1.1 christos Static void
1091 1.1 christos ar9003_rx_intr(struct athn_softc *sc, int qid)
1092 1.1 christos {
1093 1.1 christos
1094 1.1 christos while (ar9003_rx_process(sc, qid) == 0)
1095 1.1 christos continue;
1096 1.1 christos }
1097 1.1 christos
1098 1.1 christos Static int
1099 1.1 christos ar9003_tx_process(struct athn_softc *sc)
1100 1.1 christos {
1101 1.1 christos struct ifnet *ifp = &sc->sc_if;
1102 1.1 christos struct athn_txq *txq;
1103 1.1 christos struct athn_node *an;
1104 1.1 christos struct athn_tx_buf *bf;
1105 1.1 christos struct ar_tx_status *ds;
1106 1.1 christos uint8_t qid, failcnt;
1107 1.1 christos
1108 1.1 christos ds = &((struct ar_tx_status *)sc->sc_txsring)[sc->sc_txscur];
1109 1.1 christos if (!(ds->ds_status8 & AR_TXS8_DONE))
1110 1.1 christos return EBUSY;
1111 1.1 christos
1112 1.1 christos sc->sc_txscur = (sc->sc_txscur + 1) % AR9003_NTXSTATUS;
1113 1.1 christos
1114 1.1 christos /* Check that it is a valid Tx status descriptor. */
1115 1.1 christos if ((ds->ds_info & (AR_TXI_DESC_ID_M | AR_TXI_DESC_TX)) !=
1116 1.1 christos (SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) | AR_TXI_DESC_TX)) {
1117 1.1 christos memset(ds, 0, sizeof(*ds));
1118 1.1 christos return 0;
1119 1.1 christos }
1120 1.1 christos /* Retrieve the queue that was used to send this PDU. */
1121 1.1 christos qid = MS(ds->ds_info, AR_TXI_QCU_NUM);
1122 1.1 christos txq = &sc->sc_txq[qid];
1123 1.1 christos
1124 1.1 christos bf = SIMPLEQ_FIRST(&txq->head);
1125 1.1 christos if (bf == NULL || bf == txq->wait) {
1126 1.1 christos memset(ds, 0, sizeof(*ds));
1127 1.1 christos return 0;
1128 1.1 christos }
1129 1.1 christos SIMPLEQ_REMOVE_HEAD(&txq->head, bf_list);
1130 1.1 christos ifp->if_opackets++;
1131 1.1 christos
1132 1.1 christos sc->sc_tx_timer = 0;
1133 1.1 christos
1134 1.1 christos if (ds->ds_status3 & AR_TXS3_EXCESSIVE_RETRIES)
1135 1.1 christos ifp->if_oerrors++;
1136 1.1 christos
1137 1.1 christos if (ds->ds_status3 & AR_TXS3_UNDERRUN)
1138 1.1 christos athn_inc_tx_trigger_level(sc);
1139 1.1 christos
1140 1.1 christos /* Wakeup PA predistortion state machine. */
1141 1.1 christos if (bf->bf_txflags & ATHN_TXFLAG_PAPRD)
1142 1.1 christos ar9003_paprd_tx_tone_done(sc);
1143 1.1 christos
1144 1.1 christos an = (struct athn_node *)bf->bf_ni;
1145 1.1 christos /*
1146 1.1 christos * NB: the data fail count contains the number of un-acked tries
1147 1.1 christos * for the final series used. We must add the number of tries for
1148 1.1 christos * each series that was fully processed.
1149 1.1 christos */
1150 1.1 christos failcnt = MS(ds->ds_status3, AR_TXS3_DATA_FAIL_CNT);
1151 1.1 christos /* NB: Assume two tries per series. */
1152 1.1 christos failcnt += MS(ds->ds_status8, AR_TXS8_FINAL_IDX) * 2;
1153 1.1 christos
1154 1.1 christos /* Update rate control statistics. */
1155 1.1 christos an->amn.amn_txcnt++;
1156 1.1 christos if (failcnt > 0)
1157 1.1 christos an->amn.amn_retrycnt++;
1158 1.1 christos
1159 1.1 christos DPRINTFN(DBG_TX, sc, "Tx done qid=%d status3=%d fail count=%d\n",
1160 1.1 christos qid, ds->ds_status3, failcnt);
1161 1.1 christos
1162 1.1 christos /* Reset Tx status descriptor. */
1163 1.1 christos memset(ds, 0, sizeof(*ds));
1164 1.1 christos
1165 1.1 christos /* Unmap Tx buffer. */
1166 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
1167 1.1 christos BUS_DMASYNC_POSTWRITE);
1168 1.1 christos bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
1169 1.1 christos
1170 1.1 christos m_freem(bf->bf_m);
1171 1.1 christos bf->bf_m = NULL;
1172 1.1 christos ieee80211_free_node(bf->bf_ni);
1173 1.1 christos bf->bf_ni = NULL;
1174 1.1 christos
1175 1.1 christos /* Link Tx buffer back to global free list. */
1176 1.1 christos SIMPLEQ_INSERT_TAIL(&sc->sc_txbufs, bf, bf_list);
1177 1.1 christos
1178 1.1 christos /* Queue buffers that are waiting if there is new room. */
1179 1.1 christos if (--txq->queued < AR9003_TX_QDEPTH && txq->wait != NULL) {
1180 1.1 christos AR_WRITE(sc, AR_QTXDP(qid), txq->wait->bf_daddr);
1181 1.1 christos AR_WRITE_BARRIER(sc);
1182 1.1 christos txq->wait = SIMPLEQ_NEXT(txq->wait, bf_list);
1183 1.1 christos }
1184 1.1 christos return 0;
1185 1.1 christos }
1186 1.1 christos
1187 1.1 christos Static void
1188 1.1 christos ar9003_tx_intr(struct athn_softc *sc)
1189 1.1 christos {
1190 1.1 christos struct ifnet *ifp = &sc->sc_if;
1191 1.1 christos
1192 1.1 christos while (ar9003_tx_process(sc) == 0);
1193 1.1 christos
1194 1.1 christos if (!SIMPLEQ_EMPTY(&sc->sc_txbufs)) {
1195 1.1 christos ifp->if_flags &= ~IFF_OACTIVE;
1196 1.1 christos ifp->if_start(ifp);
1197 1.1 christos }
1198 1.1 christos }
1199 1.1 christos
1200 1.1 christos #ifndef IEEE80211_STA_ONLY
1201 1.1 christos /*
1202 1.1 christos * Process Software Beacon Alert interrupts.
1203 1.1 christos */
1204 1.1 christos Static int
1205 1.1 christos ar9003_swba_intr(struct athn_softc *sc)
1206 1.1 christos {
1207 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
1208 1.1 christos struct ifnet *ifp = &sc->sc_if;
1209 1.1 christos struct ieee80211_node *ni = ic->ic_bss;
1210 1.1 christos struct athn_tx_buf *bf = sc->sc_bcnbuf;
1211 1.1 christos struct ieee80211_frame *wh;
1212 1.1 christos struct ieee80211_beacon_offsets bo;
1213 1.1 christos struct ar_tx_desc *ds;
1214 1.1 christos struct mbuf *m;
1215 1.1 christos uint32_t sum;
1216 1.1 christos uint8_t ridx, hwrate;
1217 1.1 christos int error, totlen;
1218 1.1 christos
1219 1.1 christos #if notyet
1220 1.1 christos if (ic->ic_tim_mcast_pending &&
1221 1.1 christos IF_IS_EMPTY(&ni->ni_savedq) &&
1222 1.1 christos SIMPLEQ_EMPTY(&sc->sc_txq[ATHN_QID_CAB].head))
1223 1.1 christos ic->ic_tim_mcast_pending = 0;
1224 1.1 christos #endif
1225 1.1 christos if (ic->ic_dtim_count == 0)
1226 1.1 christos ic->ic_dtim_count = ic->ic_dtim_period - 1;
1227 1.1 christos else
1228 1.1 christos ic->ic_dtim_count--;
1229 1.1 christos
1230 1.1 christos /* Make sure previous beacon has been sent. */
1231 1.1 christos if (athn_tx_pending(sc, ATHN_QID_BEACON)) {
1232 1.1 christos DPRINTFN(DBG_INTR, sc, "beacon stuck\n");
1233 1.1 christos return EBUSY;
1234 1.1 christos }
1235 1.1 christos /* Get new beacon. */
1236 1.1 christos m = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
1237 1.1 christos if (__predict_false(m == NULL))
1238 1.1 christos return ENOBUFS;
1239 1.1 christos /* Assign sequence number. */
1240 1.1 christos /* XXX: use non-QoS tid? */
1241 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1242 1.1 christos *(uint16_t *)&wh->i_seq[0] =
1243 1.1 christos htole16(ic->ic_bss->ni_txseqs[0] << IEEE80211_SEQ_SEQ_SHIFT);
1244 1.1 christos ic->ic_bss->ni_txseqs[0]++;
1245 1.1 christos
1246 1.1 christos /* Unmap and free old beacon if any. */
1247 1.1 christos if (__predict_true(bf->bf_m != NULL)) {
1248 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0,
1249 1.1 christos bf->bf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1250 1.1 christos bus_dmamap_unload(sc->sc_dmat, bf->bf_map);
1251 1.1 christos m_freem(bf->bf_m);
1252 1.1 christos bf->bf_m = NULL;
1253 1.1 christos }
1254 1.1 christos /* DMA map new beacon. */
1255 1.1 christos error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
1256 1.1 christos BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1257 1.1 christos if (__predict_false(error != 0)) {
1258 1.1 christos m_freem(m);
1259 1.1 christos return error;
1260 1.1 christos }
1261 1.1 christos bf->bf_m = m;
1262 1.1 christos
1263 1.1 christos /* Setup Tx descriptor (simplified ar9003_tx()). */
1264 1.1 christos ds = bf->bf_descs;
1265 1.1 christos memset(ds, 0, sizeof(*ds));
1266 1.1 christos
1267 1.1 christos ds->ds_info =
1268 1.1 christos SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) |
1269 1.1 christos SM(AR_TXI_DESC_NDWORDS, 23) |
1270 1.1 christos SM(AR_TXI_QCU_NUM, ATHN_QID_BEACON) |
1271 1.1 christos AR_TXI_DESC_TX | AR_TXI_CTRL_STAT;
1272 1.1 christos
1273 1.1 christos totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1274 1.1 christos ds->ds_ctl11 = SM(AR_TXC11_FRAME_LEN, totlen);
1275 1.1 christos ds->ds_ctl11 |= SM(AR_TXC11_XMIT_POWER, AR_MAX_RATE_POWER);
1276 1.1 christos ds->ds_ctl12 = SM(AR_TXC12_FRAME_TYPE, AR_FRAME_TYPE_BEACON);
1277 1.1 christos ds->ds_ctl12 |= AR_TXC12_NO_ACK;
1278 1.1 christos ds->ds_ctl17 = SM(AR_TXC17_ENCR_TYPE, AR_ENCR_TYPE_CLEAR);
1279 1.1 christos
1280 1.1 christos /* Write number of tries. */
1281 1.1 christos ds->ds_ctl13 = SM(AR_TXC13_XMIT_DATA_TRIES0, 1);
1282 1.1 christos
1283 1.1 christos /* Write Tx rate. */
1284 1.1 christos ridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
1285 1.1 christos ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
1286 1.1 christos hwrate = athn_rates[ridx].hwrate;
1287 1.1 christos ds->ds_ctl14 = SM(AR_TXC14_XMIT_RATE0, hwrate);
1288 1.1 christos
1289 1.1 christos /* Write Tx chains. */
1290 1.1 christos ds->ds_ctl18 = SM(AR_TXC18_CHAIN_SEL0, sc->sc_txchainmask);
1291 1.1 christos
1292 1.1 christos ds->ds_segs[0].ds_data = bf->bf_map->dm_segs[0].ds_addr;
1293 1.1 christos /* Segment length must be a multiple of 4. */
1294 1.1 christos ds->ds_segs[0].ds_ctl |= SM(AR_TXC_BUF_LEN,
1295 1.1 christos (bf->bf_map->dm_segs[0].ds_len + 3) & ~3);
1296 1.1 christos /* Compute Tx descriptor checksum. */
1297 1.1 christos sum = ds->ds_info;
1298 1.1 christos sum += ds->ds_segs[0].ds_data;
1299 1.1 christos sum += ds->ds_segs[0].ds_ctl;
1300 1.1 christos sum = (sum >> 16) + (sum & 0xffff);
1301 1.1 christos ds->ds_ctl10 = SM(AR_TXC10_PTR_CHK_SUM, sum);
1302 1.1 christos
1303 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
1304 1.1 christos BUS_DMASYNC_PREWRITE);
1305 1.1 christos
1306 1.1 christos /* Stop Tx DMA before putting the new beacon on the queue. */
1307 1.1 christos athn_stop_tx_dma(sc, ATHN_QID_BEACON);
1308 1.1 christos
1309 1.1 christos AR_WRITE(sc, AR_QTXDP(ATHN_QID_BEACON), bf->bf_daddr);
1310 1.1 christos
1311 1.1 christos for(;;) {
1312 1.1 christos if (SIMPLEQ_EMPTY(&sc->sc_txbufs))
1313 1.1 christos break;
1314 1.1 christos
1315 1.1 christos IF_DEQUEUE(&ni->ni_savedq, m);
1316 1.1 christos if (m == NULL)
1317 1.1 christos break;
1318 1.1 christos if (!IF_IS_EMPTY(&ni->ni_savedq)) {
1319 1.1 christos /* more queued frames, set the more data bit */
1320 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1321 1.1 christos wh->i_fc[1] |= IEEE80211_FC1_MORE_DATA;
1322 1.1 christos }
1323 1.1 christos
1324 1.1 christos if (sc->sc_ops.tx(sc, m, ni, ATHN_TXFLAG_CAB) != 0) {
1325 1.1 christos ieee80211_free_node(ni);
1326 1.1 christos ifp->if_oerrors++;
1327 1.1 christos break;
1328 1.1 christos }
1329 1.1 christos }
1330 1.1 christos
1331 1.1 christos /* Kick Tx. */
1332 1.1 christos AR_WRITE(sc, AR_Q_TXE, 1 << ATHN_QID_BEACON);
1333 1.1 christos AR_WRITE_BARRIER(sc);
1334 1.1 christos return 0;
1335 1.1 christos }
1336 1.1 christos #endif
1337 1.1 christos
1338 1.1 christos Static int
1339 1.1 christos ar9003_intr(struct athn_softc *sc)
1340 1.1 christos {
1341 1.1 christos uint32_t intr, intr2, intr5, sync;
1342 1.1 christos
1343 1.1 christos /* Get pending interrupts. */
1344 1.1 christos intr = AR_READ(sc, AR_INTR_ASYNC_CAUSE);
1345 1.1 christos if (!(intr & AR_INTR_MAC_IRQ) || intr == AR_INTR_SPURIOUS) {
1346 1.1 christos intr = AR_READ(sc, AR_INTR_SYNC_CAUSE);
1347 1.1 christos if (intr == AR_INTR_SPURIOUS || (intr & sc->sc_isync) == 0)
1348 1.1 christos return 0; /* Not for us. */
1349 1.1 christos }
1350 1.1 christos
1351 1.1 christos if ((AR_READ(sc, AR_INTR_ASYNC_CAUSE) & AR_INTR_MAC_IRQ) &&
1352 1.1 christos (AR_READ(sc, AR_RTC_STATUS) & AR_RTC_STATUS_M) == AR_RTC_STATUS_ON)
1353 1.1 christos intr = AR_READ(sc, AR_ISR);
1354 1.1 christos else
1355 1.1 christos intr = 0;
1356 1.1 christos sync = AR_READ(sc, AR_INTR_SYNC_CAUSE) & sc->sc_isync;
1357 1.1 christos if (intr == 0 && sync == 0)
1358 1.1 christos return 0; /* Not for us. */
1359 1.1 christos
1360 1.1 christos if (intr != 0) {
1361 1.1 christos if (intr & AR_ISR_BCNMISC) {
1362 1.1 christos intr2 = AR_READ(sc, AR_ISR_S2);
1363 1.1 christos #ifdef notyet
1364 1.1 christos if (intr2 & AR_ISR_S2_TIM)
1365 1.1 christos /* TBD */;
1366 1.1 christos if (intr2 & AR_ISR_S2_TSFOOR)
1367 1.1 christos /* TBD */;
1368 1.1 christos if (intr2 & AR_ISR_S2_BB_WATCHDOG)
1369 1.1 christos /* TBD */;
1370 1.1 christos #endif
1371 1.1 christos }
1372 1.1 christos intr = AR_READ(sc, AR_ISR_RAC);
1373 1.1 christos if (intr == AR_INTR_SPURIOUS)
1374 1.1 christos return 1;
1375 1.1 christos
1376 1.1 christos #ifndef IEEE80211_STA_ONLY
1377 1.1 christos if (intr & AR_ISR_SWBA)
1378 1.1 christos ar9003_swba_intr(sc);
1379 1.1 christos #endif
1380 1.1 christos if (intr & (AR_ISR_RXMINTR | AR_ISR_RXINTM))
1381 1.1 christos ar9003_rx_intr(sc, ATHN_QID_LP);
1382 1.1 christos if (intr & (AR_ISR_LP_RXOK | AR_ISR_RXERR))
1383 1.1 christos ar9003_rx_intr(sc, ATHN_QID_LP);
1384 1.1 christos if (intr & AR_ISR_HP_RXOK)
1385 1.1 christos ar9003_rx_intr(sc, ATHN_QID_HP);
1386 1.1 christos
1387 1.1 christos if (intr & (AR_ISR_TXMINTR | AR_ISR_TXINTM))
1388 1.1 christos ar9003_tx_intr(sc);
1389 1.1 christos if (intr & (AR_ISR_TXOK | AR_ISR_TXERR | AR_ISR_TXEOL))
1390 1.1 christos ar9003_tx_intr(sc);
1391 1.1 christos
1392 1.1 christos if (intr & AR_ISR_GENTMR) {
1393 1.1 christos intr5 = AR_READ(sc, AR_ISR_S5_S);
1394 1.1 christos DPRINTFN(DBG_INTR, sc,
1395 1.1 christos "GENTMR trigger=%d thresh=%d\n",
1396 1.1 christos MS(intr5, AR_ISR_S5_GENTIMER_TRIG),
1397 1.1 christos MS(intr5, AR_ISR_S5_GENTIMER_THRESH));
1398 1.1 christos }
1399 1.1 christos }
1400 1.1 christos if (sync != 0) {
1401 1.1 christos if (sync & AR_INTR_SYNC_RADM_CPL_TIMEOUT) {
1402 1.1 christos AR_WRITE(sc, AR_RC, AR_RC_HOSTIF);
1403 1.1 christos AR_WRITE(sc, AR_RC, 0);
1404 1.1 christos }
1405 1.1 christos
1406 1.1 christos if ((sc->sc_flags & ATHN_FLAG_RFSILENT) &&
1407 1.1 christos (sync & AR_INTR_SYNC_GPIO_PIN(sc->sc_rfsilent_pin))) {
1408 1.1 christos struct ifnet *ifp = &sc->sc_if;
1409 1.1 christos
1410 1.1 christos printf("%s: radio switch turned off\n",
1411 1.1 christos device_xname(sc->sc_dev));
1412 1.1 christos /* Turn the interface down. */
1413 1.1 christos ifp->if_flags &= ~IFF_UP;
1414 1.1 christos athn_stop(ifp, 1);
1415 1.1 christos return 1;
1416 1.1 christos }
1417 1.1 christos
1418 1.1 christos AR_WRITE(sc, AR_INTR_SYNC_CAUSE, sync);
1419 1.1 christos (void)AR_READ(sc, AR_INTR_SYNC_CAUSE);
1420 1.1 christos }
1421 1.1 christos return 1;
1422 1.1 christos }
1423 1.1 christos
1424 1.1 christos Static int
1425 1.1 christos ar9003_tx(struct athn_softc *sc, struct mbuf *m, struct ieee80211_node *ni,
1426 1.1 christos int txflags)
1427 1.1 christos {
1428 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
1429 1.1 christos struct ieee80211_key *k = NULL;
1430 1.1 christos struct ieee80211_frame *wh;
1431 1.1 christos struct athn_series series[4];
1432 1.1 christos struct ar_tx_desc *ds;
1433 1.1 christos struct athn_txq *txq;
1434 1.1 christos struct athn_tx_buf *bf;
1435 1.1 christos struct athn_node *an = (void *)ni;
1436 1.1 christos struct mbuf *m1;
1437 1.1 christos uint32_t sum;
1438 1.1 christos uint16_t qos;
1439 1.1 christos uint8_t txpower, type, encrtype, ridx[4];
1440 1.1 christos int i, error, totlen, hasqos, qid;
1441 1.1 christos
1442 1.1 christos /* Grab a Tx buffer from our global free list. */
1443 1.1 christos bf = SIMPLEQ_FIRST(&sc->sc_txbufs);
1444 1.1 christos KASSERT(bf != NULL);
1445 1.1 christos
1446 1.1 christos /* Map 802.11 frame type to hardware frame type. */
1447 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1448 1.1 christos if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1449 1.1 christos IEEE80211_FC0_TYPE_MGT) {
1450 1.1 christos /* NB: Beacons do not use ar9003_tx(). */
1451 1.1 christos if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1452 1.1 christos IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1453 1.1 christos type = AR_FRAME_TYPE_PROBE_RESP;
1454 1.1 christos else if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1455 1.1 christos IEEE80211_FC0_SUBTYPE_ATIM)
1456 1.1 christos type = AR_FRAME_TYPE_ATIM;
1457 1.1 christos else
1458 1.1 christos type = AR_FRAME_TYPE_NORMAL;
1459 1.1 christos }
1460 1.1 christos else if ((wh->i_fc[0] &
1461 1.1 christos (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1462 1.1 christos (IEEE80211_FC0_TYPE_CTL | IEEE80211_FC0_SUBTYPE_PS_POLL)) {
1463 1.1 christos type = AR_FRAME_TYPE_PSPOLL;
1464 1.1 christos }
1465 1.1 christos else
1466 1.1 christos type = AR_FRAME_TYPE_NORMAL;
1467 1.1 christos
1468 1.1 christos if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
1469 1.1 christos k = ieee80211_crypto_encap(ic, ni, m);
1470 1.1 christos if (k == NULL)
1471 1.1 christos return ENOBUFS;
1472 1.1 christos
1473 1.1 christos /* packet header may have moved, reset our local pointer */
1474 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1475 1.1 christos }
1476 1.1 christos
1477 1.1 christos /* XXX 2-byte padding for QoS and 4-addr headers. */
1478 1.1 christos
1479 1.1 christos /* Select the HW Tx queue to use for this frame. */
1480 1.1 christos if ((hasqos = ieee80211_has_qos(wh))) {
1481 1.1 christos #ifdef notyet_edca
1482 1.1 christos uint8_t tid;
1483 1.1 christos
1484 1.1 christos qos = ieee80211_get_qos(wh);
1485 1.1 christos tid = qos & IEEE80211_QOS_TID;
1486 1.1 christos qid = athn_ac2qid[ieee80211_up_to_ac(ic, tid)];
1487 1.1 christos #else
1488 1.1 christos qos = ieee80211_get_qos(wh);
1489 1.1 christos qid = ATHN_QID_AC_BE;
1490 1.1 christos #endif /* notyet_edca */
1491 1.1 christos }
1492 1.1 christos else if (type == AR_FRAME_TYPE_PSPOLL) {
1493 1.1 christos qos = 0;
1494 1.1 christos qid = ATHN_QID_PSPOLL;
1495 1.1 christos }
1496 1.1 christos else if (txflags & ATHN_TXFLAG_CAB) {
1497 1.1 christos qos = 0;
1498 1.1 christos qid = ATHN_QID_CAB;
1499 1.1 christos }
1500 1.1 christos else {
1501 1.1 christos qos = 0;
1502 1.1 christos qid = ATHN_QID_AC_BE;
1503 1.1 christos }
1504 1.1 christos txq = &sc->sc_txq[qid];
1505 1.1 christos
1506 1.1 christos /* Select the transmit rates to use for this frame. */
1507 1.1 christos if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1508 1.1 christos (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) !=
1509 1.1 christos IEEE80211_FC0_TYPE_DATA) {
1510 1.1 christos /* Use lowest rate for all tries. */
1511 1.1 christos ridx[0] = ridx[1] = ridx[2] = ridx[3] =
1512 1.1 christos (ic->ic_curmode == IEEE80211_MODE_11A) ?
1513 1.1 christos ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK1;
1514 1.1 christos }
1515 1.1 christos else if (ic->ic_fixed_rate != -1) {
1516 1.1 christos /* Use same fixed rate for all tries. */
1517 1.1 christos ridx[0] = ridx[1] = ridx[2] = ridx[3] =
1518 1.1 christos sc->sc_fixed_ridx;
1519 1.1 christos }
1520 1.1 christos else {
1521 1.1 christos int txrate = ni->ni_txrate;
1522 1.1 christos /* Use fallback table of the node. */
1523 1.1 christos for (i = 0; i < 4; i++) {
1524 1.1 christos ridx[i] = an->ridx[txrate];
1525 1.1 christos txrate = an->fallback[txrate];
1526 1.1 christos }
1527 1.1 christos }
1528 1.1 christos
1529 1.1 christos if (__predict_false(sc->sc_drvbpf != NULL)) {
1530 1.1 christos struct athn_tx_radiotap_header *tap = &sc->sc_txtap;
1531 1.1 christos
1532 1.1 christos tap->wt_flags = 0;
1533 1.1 christos /* Use initial transmit rate. */
1534 1.1 christos tap->wt_rate = athn_rates[ridx[0]].rate;
1535 1.1 christos tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1536 1.1 christos tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1537 1.1 christos // XXX tap->wt_hwqueue = qid;
1538 1.1 christos if (ridx[0] != ATHN_RIDX_CCK1 &&
1539 1.1 christos (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1540 1.1 christos tap->wt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
1541 1.1 christos
1542 1.1 christos bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m);
1543 1.1 christos }
1544 1.1 christos
1545 1.1 christos /* DMA map mbuf. */
1546 1.1 christos error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
1547 1.1 christos BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1548 1.1 christos if (__predict_false(error != 0)) {
1549 1.1 christos if (error != EFBIG) {
1550 1.1 christos printf("%s: can't map mbuf (error %d)\n",
1551 1.1 christos device_xname(sc->sc_dev), error);
1552 1.1 christos m_freem(m);
1553 1.1 christos return error;
1554 1.1 christos }
1555 1.1 christos /*
1556 1.1 christos * DMA mapping requires too many DMA segments; linearize
1557 1.1 christos * mbuf in kernel virtual address space and retry.
1558 1.1 christos */
1559 1.1 christos MGETHDR(m1, M_DONTWAIT, MT_DATA);
1560 1.1 christos if (m1 == NULL) {
1561 1.1 christos m_freem(m);
1562 1.1 christos return ENOBUFS;
1563 1.1 christos }
1564 1.1 christos if (m->m_pkthdr.len > (int)MHLEN) {
1565 1.1 christos MCLGET(m1, M_DONTWAIT);
1566 1.1 christos if (!(m1->m_flags & M_EXT)) {
1567 1.1 christos m_freem(m);
1568 1.1 christos m_freem(m1);
1569 1.1 christos return ENOBUFS;
1570 1.1 christos }
1571 1.1 christos }
1572 1.1 christos m_copydata(m, 0, m->m_pkthdr.len, mtod(m1, void *));
1573 1.1 christos m1->m_pkthdr.len = m1->m_len = m->m_pkthdr.len;
1574 1.1 christos m_freem(m);
1575 1.1 christos m = m1;
1576 1.1 christos
1577 1.1 christos error = bus_dmamap_load_mbuf(sc->sc_dmat, bf->bf_map, m,
1578 1.1 christos BUS_DMA_NOWAIT | BUS_DMA_WRITE);
1579 1.1 christos if (error != 0) {
1580 1.1 christos printf("%s: can't map mbuf (error %d)\n",
1581 1.1 christos device_xname(sc->sc_dev), error);
1582 1.1 christos m_freem(m);
1583 1.1 christos return error;
1584 1.1 christos }
1585 1.1 christos }
1586 1.1 christos bf->bf_m = m;
1587 1.1 christos bf->bf_ni = ni;
1588 1.1 christos bf->bf_txflags = txflags;
1589 1.1 christos
1590 1.1 christos wh = mtod(m, struct ieee80211_frame *);
1591 1.1 christos
1592 1.1 christos totlen = m->m_pkthdr.len + IEEE80211_CRC_LEN;
1593 1.1 christos
1594 1.1 christos /* Setup Tx descriptor. */
1595 1.1 christos ds = bf->bf_descs;
1596 1.1 christos memset(ds, 0, sizeof(*ds));
1597 1.1 christos
1598 1.1 christos ds->ds_info =
1599 1.1 christos SM(AR_TXI_DESC_ID, AR_VENDOR_ATHEROS) |
1600 1.1 christos SM(AR_TXI_DESC_NDWORDS, 23) |
1601 1.1 christos SM(AR_TXI_QCU_NUM, qid) |
1602 1.1 christos AR_TXI_DESC_TX | AR_TXI_CTRL_STAT;
1603 1.1 christos
1604 1.1 christos ds->ds_ctl11 = AR_TXC11_CLR_DEST_MASK;
1605 1.1 christos txpower = AR_MAX_RATE_POWER; /* Get from per-rate registers. */
1606 1.1 christos ds->ds_ctl11 |= SM(AR_TXC11_XMIT_POWER, txpower);
1607 1.1 christos
1608 1.1 christos ds->ds_ctl12 = SM(AR_TXC12_FRAME_TYPE, type);
1609 1.1 christos
1610 1.1 christos if (IEEE80211_IS_MULTICAST(wh->i_addr1) ||
1611 1.1 christos (hasqos && (qos & IEEE80211_QOS_ACKPOLICY_MASK) ==
1612 1.1 christos IEEE80211_QOS_ACKPOLICY_NOACK))
1613 1.1 christos ds->ds_ctl12 |= AR_TXC12_NO_ACK;
1614 1.1 christos
1615 1.1 christos #if notyet
1616 1.1 christos if (0 && k != NULL) {
1617 1.1 christos uintptr_t entry;
1618 1.1 christos
1619 1.1 christos /*
1620 1.1 christos * Map 802.11 cipher to hardware encryption type and
1621 1.1 christos * compute MIC+ICV overhead.
1622 1.1 christos */
1623 1.1 christos switch (k->k_cipher) {
1624 1.1 christos case IEEE80211_CIPHER_WEP40:
1625 1.1 christos case IEEE80211_CIPHER_WEP104:
1626 1.1 christos encrtype = AR_ENCR_TYPE_WEP;
1627 1.1 christos totlen += 4;
1628 1.1 christos break;
1629 1.1 christos case IEEE80211_CIPHER_TKIP:
1630 1.1 christos encrtype = AR_ENCR_TYPE_TKIP;
1631 1.1 christos totlen += 12;
1632 1.1 christos break;
1633 1.1 christos case IEEE80211_CIPHER_CCMP:
1634 1.1 christos encrtype = AR_ENCR_TYPE_AES;
1635 1.1 christos totlen += 8;
1636 1.1 christos break;
1637 1.1 christos default:
1638 1.1 christos panic("unsupported cipher");
1639 1.1 christos }
1640 1.1 christos /*
1641 1.1 christos * NB: The key cache entry index is stored in the key
1642 1.1 christos * private field when the key is installed.
1643 1.1 christos */
1644 1.1 christos entry = (uintptr_t)k->k_priv;
1645 1.1 christos ds->ds_ctl12 |= SM(AR_TXC12_DEST_IDX, entry);
1646 1.1 christos ds->ds_ctl11 |= AR_TXC11_DEST_IDX_VALID;
1647 1.1 christos }
1648 1.1 christos else
1649 1.1 christos #endif
1650 1.1 christos encrtype = AR_ENCR_TYPE_CLEAR;
1651 1.1 christos ds->ds_ctl17 = SM(AR_TXC17_ENCR_TYPE, encrtype);
1652 1.1 christos
1653 1.1 christos /* Check if frame must be protected using RTS/CTS or CTS-to-self. */
1654 1.1 christos if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1655 1.1 christos /* NB: Group frames are sent using CCK in 802.11b/g. */
1656 1.1 christos if (totlen > ic->ic_rtsthreshold) {
1657 1.1 christos ds->ds_ctl11 |= AR_TXC11_RTS_ENABLE;
1658 1.1 christos }
1659 1.1 christos else if ((ic->ic_flags & IEEE80211_F_USEPROT) &&
1660 1.1 christos athn_rates[ridx[0]].phy == IEEE80211_T_OFDM) {
1661 1.1 christos if (ic->ic_protmode == IEEE80211_PROT_RTSCTS)
1662 1.1 christos ds->ds_ctl11 |= AR_TXC11_RTS_ENABLE;
1663 1.1 christos else if (ic->ic_protmode == IEEE80211_PROT_CTSONLY)
1664 1.1 christos ds->ds_ctl11 |= AR_TXC11_CTS_ENABLE;
1665 1.1 christos }
1666 1.1 christos }
1667 1.1 christos if (ds->ds_ctl11 & (AR_TXC11_RTS_ENABLE | AR_TXC11_CTS_ENABLE)) {
1668 1.1 christos /* Disable multi-rate retries when protection is used. */
1669 1.1 christos ridx[1] = ridx[2] = ridx[3] = ridx[0];
1670 1.1 christos }
1671 1.1 christos /* Setup multi-rate retries. */
1672 1.1 christos for (i = 0; i < 4; i++) {
1673 1.1 christos series[i].hwrate = athn_rates[ridx[i]].hwrate;
1674 1.1 christos if (athn_rates[ridx[i]].phy == IEEE80211_T_DS &&
1675 1.1 christos ridx[i] != ATHN_RIDX_CCK1 &&
1676 1.1 christos (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1677 1.1 christos series[i].hwrate |= 0x04;
1678 1.1 christos series[i].dur = 0;
1679 1.1 christos }
1680 1.1 christos if (!(ds->ds_ctl12 & AR_TXC12_NO_ACK)) {
1681 1.1 christos /* Compute duration for each series. */
1682 1.1 christos for (i = 0; i < 4; i++) {
1683 1.1 christos series[i].dur = athn_txtime(sc, IEEE80211_ACK_LEN,
1684 1.1 christos athn_rates[ridx[i]].rspridx, ic->ic_flags);
1685 1.1 christos }
1686 1.1 christos }
1687 1.1 christos /* If this is a PA training frame, select the Tx chain to use. */
1688 1.1 christos if (__predict_false(txflags & ATHN_TXFLAG_PAPRD)) {
1689 1.1 christos ds->ds_ctl12 |= SM(AR_TXC12_PAPRD_CHAIN_MASK,
1690 1.1 christos 1 << sc->sc_paprd_curchain);
1691 1.1 christos }
1692 1.1 christos
1693 1.1 christos /* Write number of tries for each series. */
1694 1.1 christos ds->ds_ctl13 =
1695 1.1 christos SM(AR_TXC13_XMIT_DATA_TRIES0, 2) |
1696 1.1 christos SM(AR_TXC13_XMIT_DATA_TRIES1, 2) |
1697 1.1 christos SM(AR_TXC13_XMIT_DATA_TRIES2, 2) |
1698 1.1 christos SM(AR_TXC13_XMIT_DATA_TRIES3, 4);
1699 1.1 christos
1700 1.1 christos /* Tell HW to update duration field in 802.11 header. */
1701 1.1 christos if (type != AR_FRAME_TYPE_PSPOLL)
1702 1.1 christos ds->ds_ctl13 |= AR_TXC13_DUR_UPDATE_ENA;
1703 1.1 christos
1704 1.1 christos /* Write Tx rate for each series. */
1705 1.1 christos ds->ds_ctl14 =
1706 1.1 christos SM(AR_TXC14_XMIT_RATE0, series[0].hwrate) |
1707 1.1 christos SM(AR_TXC14_XMIT_RATE1, series[1].hwrate) |
1708 1.1 christos SM(AR_TXC14_XMIT_RATE2, series[2].hwrate) |
1709 1.1 christos SM(AR_TXC14_XMIT_RATE3, series[3].hwrate);
1710 1.1 christos
1711 1.1 christos /* Write duration for each series. */
1712 1.1 christos ds->ds_ctl15 =
1713 1.1 christos SM(AR_TXC15_PACKET_DUR0, series[0].dur) |
1714 1.1 christos SM(AR_TXC15_PACKET_DUR1, series[1].dur);
1715 1.1 christos ds->ds_ctl16 =
1716 1.1 christos SM(AR_TXC16_PACKET_DUR2, series[2].dur) |
1717 1.1 christos SM(AR_TXC16_PACKET_DUR3, series[3].dur);
1718 1.1 christos
1719 1.1 christos if ((sc->sc_flags & ATHN_FLAG_3TREDUCE_CHAIN) &&
1720 1.1 christos ic->ic_curmode == IEEE80211_MODE_11A) {
1721 1.1 christos /*
1722 1.1 christos * In order to not exceed PCIe power requirements, we only
1723 1.1 christos * use two Tx chains for MCS0~15 on 5GHz band on these chips.
1724 1.1 christos */
1725 1.1 christos ds->ds_ctl18 =
1726 1.1 christos SM(AR_TXC18_CHAIN_SEL0,
1727 1.1 christos (ridx[0] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
1728 1.1 christos SM(AR_TXC18_CHAIN_SEL1,
1729 1.1 christos (ridx[1] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
1730 1.1 christos SM(AR_TXC18_CHAIN_SEL2,
1731 1.1 christos (ridx[2] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask) |
1732 1.1 christos SM(AR_TXC18_CHAIN_SEL3,
1733 1.1 christos (ridx[3] <= ATHN_RIDX_MCS15) ? 0x3 : sc->sc_txchainmask);
1734 1.1 christos }
1735 1.1 christos else {
1736 1.1 christos /* Use the same Tx chains for all tries. */
1737 1.1 christos ds->ds_ctl18 =
1738 1.1 christos SM(AR_TXC18_CHAIN_SEL0, sc->sc_txchainmask) |
1739 1.1 christos SM(AR_TXC18_CHAIN_SEL1, sc->sc_txchainmask) |
1740 1.1 christos SM(AR_TXC18_CHAIN_SEL2, sc->sc_txchainmask) |
1741 1.1 christos SM(AR_TXC18_CHAIN_SEL3, sc->sc_txchainmask);
1742 1.1 christos }
1743 1.1 christos #ifdef notyet
1744 1.1 christos #ifndef IEEE80211_NO_HT
1745 1.1 christos /* Use the same short GI setting for all tries. */
1746 1.1 christos if (ic->ic_flags & IEEE80211_F_SHGI)
1747 1.1 christos ds->ds_ctl18 |= AR_TXC18_GI0123;
1748 1.1 christos /* Use the same channel width for all tries. */
1749 1.1 christos if (ic->ic_flags & IEEE80211_F_CBW40)
1750 1.1 christos ds->ds_ctl18 |= AR_TXC18_2040_0123;
1751 1.1 christos #endif
1752 1.1 christos #endif
1753 1.1 christos
1754 1.1 christos if (ds->ds_ctl11 & (AR_TXC11_RTS_ENABLE | AR_TXC11_CTS_ENABLE)) {
1755 1.1 christos uint8_t protridx, hwrate;
1756 1.1 christos uint16_t dur = 0;
1757 1.1 christos
1758 1.1 christos /* Use the same protection mode for all tries. */
1759 1.1 christos if (ds->ds_ctl11 & AR_TXC11_RTS_ENABLE) {
1760 1.1 christos ds->ds_ctl15 |= AR_TXC15_RTSCTS_QUAL01;
1761 1.1 christos ds->ds_ctl16 |= AR_TXC16_RTSCTS_QUAL23;
1762 1.1 christos }
1763 1.1 christos /* Select protection rate (suboptimal but ok). */
1764 1.1 christos protridx = (ic->ic_curmode == IEEE80211_MODE_11A) ?
1765 1.1 christos ATHN_RIDX_OFDM6 : ATHN_RIDX_CCK2;
1766 1.1 christos if (ds->ds_ctl11 & AR_TXC11_RTS_ENABLE) {
1767 1.1 christos /* Account for CTS duration. */
1768 1.1 christos dur += athn_txtime(sc, IEEE80211_ACK_LEN,
1769 1.1 christos athn_rates[protridx].rspridx, ic->ic_flags);
1770 1.1 christos }
1771 1.1 christos dur += athn_txtime(sc, totlen, ridx[0], ic->ic_flags);
1772 1.1 christos if (!(ds->ds_ctl12 & AR_TXC12_NO_ACK)) {
1773 1.1 christos /* Account for ACK duration. */
1774 1.1 christos dur += athn_txtime(sc, IEEE80211_ACK_LEN,
1775 1.1 christos athn_rates[ridx[0]].rspridx, ic->ic_flags);
1776 1.1 christos }
1777 1.1 christos /* Write protection frame duration and rate. */
1778 1.1 christos ds->ds_ctl13 |= SM(AR_TXC13_BURST_DUR, dur);
1779 1.1 christos hwrate = athn_rates[protridx].hwrate;
1780 1.1 christos if (protridx == ATHN_RIDX_CCK2 &&
1781 1.1 christos (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1782 1.1 christos hwrate |= 0x04;
1783 1.1 christos ds->ds_ctl18 |= SM(AR_TXC18_RTSCTS_RATE, hwrate);
1784 1.1 christos }
1785 1.1 christos
1786 1.1 christos ds->ds_ctl11 |= SM(AR_TXC11_FRAME_LEN, totlen);
1787 1.1 christos ds->ds_ctl19 = AR_TXC19_NOT_SOUNDING;
1788 1.1 christos
1789 1.1 christos for (i = 0; i < bf->bf_map->dm_nsegs; i++) {
1790 1.1 christos ds->ds_segs[i].ds_data = bf->bf_map->dm_segs[i].ds_addr;
1791 1.1 christos ds->ds_segs[i].ds_ctl = SM(AR_TXC_BUF_LEN,
1792 1.1 christos bf->bf_map->dm_segs[i].ds_len);
1793 1.1 christos }
1794 1.1 christos /* Compute Tx descriptor checksum. */
1795 1.1 christos sum = ds->ds_info + ds->ds_link;
1796 1.1 christos for (i = 0; i < 4; i++) {
1797 1.1 christos sum += ds->ds_segs[i].ds_data;
1798 1.1 christos sum += ds->ds_segs[i].ds_ctl;
1799 1.1 christos }
1800 1.1 christos sum = (sum >> 16) + (sum & 0xffff);
1801 1.1 christos ds->ds_ctl10 = SM(AR_TXC10_PTR_CHK_SUM, sum);
1802 1.1 christos
1803 1.1 christos bus_dmamap_sync(sc->sc_dmat, bf->bf_map, 0, bf->bf_map->dm_mapsize,
1804 1.1 christos BUS_DMASYNC_PREWRITE);
1805 1.1 christos
1806 1.1 christos DPRINTFN(DBG_TX, sc,
1807 1.1 christos "Tx qid=%d nsegs=%d ctl11=0x%x ctl12=0x%x ctl14=0x%x\n",
1808 1.1 christos qid, bf->bf_map->dm_nsegs, ds->ds_ctl11, ds->ds_ctl12,
1809 1.1 christos ds->ds_ctl14);
1810 1.1 christos
1811 1.1 christos SIMPLEQ_REMOVE_HEAD(&sc->sc_txbufs, bf_list);
1812 1.1 christos SIMPLEQ_INSERT_TAIL(&txq->head, bf, bf_list);
1813 1.1 christos
1814 1.1 christos /* Queue buffer unless hardware FIFO is already full. */
1815 1.1 christos if (++txq->queued <= AR9003_TX_QDEPTH) {
1816 1.1 christos AR_WRITE(sc, AR_QTXDP(qid), bf->bf_daddr);
1817 1.1 christos AR_WRITE_BARRIER(sc);
1818 1.1 christos }
1819 1.1 christos else if (txq->wait == NULL)
1820 1.1 christos txq->wait = bf;
1821 1.1 christos return 0;
1822 1.1 christos }
1823 1.1 christos
1824 1.1 christos Static void
1825 1.1 christos ar9003_set_rf_mode(struct athn_softc *sc, struct ieee80211_channel *c)
1826 1.1 christos {
1827 1.1 christos uint32_t reg;
1828 1.1 christos
1829 1.1 christos reg = IEEE80211_IS_CHAN_2GHZ(c) ?
1830 1.1 christos AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
1831 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c) &&
1832 1.1 christos (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
1833 1.1 christos reg |= AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE;
1834 1.1 christos }
1835 1.1 christos AR_WRITE(sc, AR_PHY_MODE, reg);
1836 1.1 christos AR_WRITE_BARRIER(sc);
1837 1.1 christos }
1838 1.1 christos
1839 1.1 christos static __inline uint32_t
1840 1.1 christos ar9003_synth_delay(struct athn_softc *sc)
1841 1.1 christos {
1842 1.1 christos uint32_t delay;
1843 1.1 christos
1844 1.1 christos delay = MS(AR_READ(sc, AR_PHY_RX_DELAY), AR_PHY_RX_DELAY_DELAY);
1845 1.1 christos if (sc->sc_ic.ic_curmode == IEEE80211_MODE_11B)
1846 1.1 christos delay = (delay * 4) / 22;
1847 1.1 christos else
1848 1.1 christos delay = delay / 10; /* in 100ns steps */
1849 1.1 christos return delay;
1850 1.1 christos }
1851 1.1 christos
1852 1.1 christos Static int
1853 1.1 christos ar9003_rf_bus_request(struct athn_softc *sc)
1854 1.1 christos {
1855 1.1 christos int ntries;
1856 1.1 christos
1857 1.1 christos /* Request RF Bus grant. */
1858 1.1 christos AR_WRITE(sc, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
1859 1.1 christos for (ntries = 0; ntries < 10000; ntries++) {
1860 1.1 christos if (AR_READ(sc, AR_PHY_RFBUS_GRANT) & AR_PHY_RFBUS_GRANT_EN)
1861 1.1 christos return 0;
1862 1.1 christos DELAY(10);
1863 1.1 christos }
1864 1.1 christos DPRINTFN(DBG_RF, sc, "could not kill baseband Rx");
1865 1.1 christos return ETIMEDOUT;
1866 1.1 christos }
1867 1.1 christos
1868 1.1 christos Static void
1869 1.1 christos ar9003_rf_bus_release(struct athn_softc *sc)
1870 1.1 christos {
1871 1.1 christos /* Wait for the synthesizer to settle. */
1872 1.1 christos DELAY(AR_BASE_PHY_ACTIVE_DELAY + ar9003_synth_delay(sc));
1873 1.1 christos
1874 1.1 christos /* Release the RF Bus grant. */
1875 1.1 christos AR_WRITE(sc, AR_PHY_RFBUS_REQ, 0);
1876 1.1 christos AR_WRITE_BARRIER(sc);
1877 1.1 christos }
1878 1.1 christos
1879 1.1 christos Static void
1880 1.1 christos ar9003_set_phy(struct athn_softc *sc, struct ieee80211_channel *c,
1881 1.1 christos struct ieee80211_channel *extc)
1882 1.1 christos {
1883 1.1 christos uint32_t phy;
1884 1.1 christos
1885 1.1 christos phy = AR_READ(sc, AR_PHY_GEN_CTRL);
1886 1.1 christos phy |= AR_PHY_GC_HT_EN | AR_PHY_GC_SHORT_GI_40 |
1887 1.1 christos AR_PHY_GC_SINGLE_HT_LTF1 | AR_PHY_GC_WALSH;
1888 1.1 christos #ifndef IEEE80211_NO_HT
1889 1.1 christos if (extc != NULL) {
1890 1.1 christos phy |= AR_PHY_GC_DYN2040_EN;
1891 1.1 christos if (extc > c) /* XXX */
1892 1.1 christos phy |= AR_PHY_GC_DYN2040_PRI_CH;
1893 1.1 christos }
1894 1.1 christos #endif
1895 1.1 christos /* Turn off Green Field detection for now. */
1896 1.1 christos phy &= ~AR_PHY_GC_GF_DETECT_EN;
1897 1.1 christos AR_WRITE(sc, AR_PHY_GEN_CTRL, phy);
1898 1.1 christos
1899 1.1 christos AR_WRITE(sc, AR_2040_MODE,
1900 1.1 christos (extc != NULL) ? AR_2040_JOINED_RX_CLEAR : 0);
1901 1.1 christos
1902 1.1 christos /* Set global transmit timeout. */
1903 1.1 christos AR_WRITE(sc, AR_GTXTO, SM(AR_GTXTO_TIMEOUT_LIMIT, 25));
1904 1.1 christos /* Set carrier sense timeout. */
1905 1.1 christos AR_WRITE(sc, AR_CST, SM(AR_CST_TIMEOUT_LIMIT, 15));
1906 1.1 christos AR_WRITE_BARRIER(sc);
1907 1.1 christos }
1908 1.1 christos
1909 1.1 christos Static void
1910 1.1 christos ar9003_set_delta_slope(struct athn_softc *sc, struct ieee80211_channel *c,
1911 1.1 christos struct ieee80211_channel *extc)
1912 1.1 christos {
1913 1.1 christos uint32_t coeff, exp, man, reg;
1914 1.1 christos
1915 1.1 christos /* Set Delta Slope (exponent and mantissa). */
1916 1.1 christos coeff = (100 << 24) / c->ic_freq;
1917 1.1 christos athn_get_delta_slope(coeff, &exp, &man);
1918 1.1 christos DPRINTFN(DBG_RF, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
1919 1.1 christos
1920 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING3);
1921 1.1 christos reg = RW(reg, AR_PHY_TIMING3_DSC_EXP, exp);
1922 1.1 christos reg = RW(reg, AR_PHY_TIMING3_DSC_MAN, man);
1923 1.1 christos AR_WRITE(sc, AR_PHY_TIMING3, reg);
1924 1.1 christos
1925 1.1 christos /* For Short GI, coeff is 9/10 that of normal coeff. */
1926 1.1 christos coeff = (9 * coeff) / 10;
1927 1.1 christos athn_get_delta_slope(coeff, &exp, &man);
1928 1.1 christos DPRINTFN(DBG_RF, sc, "delta slope coeff exp=%u man=%u\n", exp, man);
1929 1.1 christos
1930 1.1 christos reg = AR_READ(sc, AR_PHY_SGI_DELTA);
1931 1.1 christos reg = RW(reg, AR_PHY_SGI_DSC_EXP, exp);
1932 1.1 christos reg = RW(reg, AR_PHY_SGI_DSC_MAN, man);
1933 1.1 christos AR_WRITE(sc, AR_PHY_SGI_DELTA, reg);
1934 1.1 christos AR_WRITE_BARRIER(sc);
1935 1.1 christos }
1936 1.1 christos
1937 1.1 christos Static void
1938 1.1 christos ar9003_enable_antenna_diversity(struct athn_softc *sc)
1939 1.1 christos {
1940 1.1 christos AR_SETBITS(sc, AR_PHY_CCK_DETECT,
1941 1.1 christos AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
1942 1.1 christos AR_WRITE_BARRIER(sc);
1943 1.1 christos }
1944 1.1 christos
1945 1.1 christos Static void
1946 1.1 christos ar9003_init_baseband(struct athn_softc *sc)
1947 1.1 christos {
1948 1.1 christos uint32_t synth_delay;
1949 1.1 christos
1950 1.1 christos synth_delay = ar9003_synth_delay(sc);
1951 1.1 christos /* Activate the PHY (includes baseband activate and synthesizer on). */
1952 1.1 christos AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
1953 1.1 christos AR_WRITE_BARRIER(sc);
1954 1.1 christos DELAY(AR_BASE_PHY_ACTIVE_DELAY + synth_delay);
1955 1.1 christos }
1956 1.1 christos
1957 1.1 christos Static void
1958 1.1 christos ar9003_disable_phy(struct athn_softc *sc)
1959 1.1 christos {
1960 1.1 christos AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
1961 1.1 christos AR_WRITE_BARRIER(sc);
1962 1.1 christos }
1963 1.1 christos
1964 1.1 christos Static void
1965 1.1 christos ar9003_init_chains(struct athn_softc *sc)
1966 1.1 christos {
1967 1.1 christos if (sc->sc_rxchainmask == 0x5 || sc->sc_txchainmask == 0x5)
1968 1.1 christos AR_SETBITS(sc, AR_PHY_ANALOG_SWAP, AR_PHY_SWAP_ALT_CHAIN);
1969 1.1 christos
1970 1.1 christos /* Setup chain masks. */
1971 1.1 christos AR_WRITE(sc, AR_PHY_RX_CHAINMASK, sc->sc_rxchainmask);
1972 1.1 christos AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
1973 1.1 christos
1974 1.1 christos if (sc->sc_flags & ATHN_FLAG_3TREDUCE_CHAIN) {
1975 1.1 christos /*
1976 1.1 christos * All self-generated frames are sent using two Tx chains
1977 1.1 christos * on these chips to not exceed PCIe power requirements.
1978 1.1 christos */
1979 1.1 christos AR_WRITE(sc, AR_SELFGEN_MASK, 0x3);
1980 1.1 christos }
1981 1.1 christos else
1982 1.1 christos AR_WRITE(sc, AR_SELFGEN_MASK, sc->sc_txchainmask);
1983 1.1 christos AR_WRITE_BARRIER(sc);
1984 1.1 christos }
1985 1.1 christos
1986 1.1 christos Static void
1987 1.1 christos ar9003_set_rxchains(struct athn_softc *sc)
1988 1.1 christos {
1989 1.1 christos if (sc->sc_rxchainmask == 0x3 || sc->sc_rxchainmask == 0x5) {
1990 1.1 christos AR_WRITE(sc, AR_PHY_RX_CHAINMASK, sc->sc_rxchainmask);
1991 1.1 christos AR_WRITE(sc, AR_PHY_CAL_CHAINMASK, sc->sc_rxchainmask);
1992 1.1 christos AR_WRITE_BARRIER(sc);
1993 1.1 christos }
1994 1.1 christos }
1995 1.1 christos
1996 1.1 christos #ifdef notused
1997 1.1 christos Static void
1998 1.1 christos ar9003_read_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
1999 1.1 christos {
2000 1.1 christos /* Sign-extends 9-bit value (assumes upper bits are zeroes). */
2001 1.1 christos #define SIGN_EXT(v) (((v) ^ 0x100) - 0x100)
2002 1.1 christos uint32_t reg;
2003 1.1 christos int i;
2004 1.1 christos
2005 1.1 christos for (i = 0; i < sc->sc_nrxchains; i++) {
2006 1.1 christos reg = AR_READ(sc, AR_PHY_CCA(i));
2007 1.1 christos nf[i] = MS(reg, AR_PHY_MINCCA_PWR);
2008 1.1 christos nf[i] = SIGN_EXT(nf[i]);
2009 1.1 christos
2010 1.1 christos reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
2011 1.1 christos nf_ext[i] = MS(reg, AR_PHY_EXT_MINCCA_PWR);
2012 1.1 christos nf_ext[i] = SIGN_EXT(nf_ext[i]);
2013 1.1 christos }
2014 1.1 christos #undef SIGN_EXT
2015 1.1 christos }
2016 1.1 christos #endif /* notused */
2017 1.1 christos
2018 1.1 christos #ifdef notused
2019 1.1 christos Static void
2020 1.1 christos ar9003_write_noisefloor(struct athn_softc *sc, int16_t *nf, int16_t *nf_ext)
2021 1.1 christos {
2022 1.1 christos uint32_t reg;
2023 1.1 christos int i;
2024 1.1 christos
2025 1.1 christos for (i = 0; i < sc->sc_nrxchains; i++) {
2026 1.1 christos reg = AR_READ(sc, AR_PHY_CCA(i));
2027 1.1 christos reg = RW(reg, AR_PHY_MAXCCA_PWR, nf[i]);
2028 1.1 christos AR_WRITE(sc, AR_PHY_CCA(i), reg);
2029 1.1 christos
2030 1.1 christos reg = AR_READ(sc, AR_PHY_EXT_CCA(i));
2031 1.1 christos reg = RW(reg, AR_PHY_EXT_MAXCCA_PWR, nf_ext[i]);
2032 1.1 christos AR_WRITE(sc, AR_PHY_EXT_CCA(i), reg);
2033 1.1 christos }
2034 1.1 christos AR_WRITE_BARRIER(sc);
2035 1.1 christos }
2036 1.1 christos #endif /* notused */
2037 1.1 christos
2038 1.1 christos #ifdef notused
2039 1.1 christos Static void
2040 1.1 christos ar9003_get_noisefloor(struct athn_softc *sc, struct ieee80211_channel *c)
2041 1.1 christos {
2042 1.1 christos int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
2043 1.1 christos int16_t cca_min, cca_max;
2044 1.1 christos int i;
2045 1.1 christos
2046 1.1 christos if (AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF) {
2047 1.1 christos /* Noisefloor calibration not finished. */
2048 1.1 christos return;
2049 1.1 christos }
2050 1.1 christos /* Noisefloor calibration is finished. */
2051 1.1 christos ar9003_read_noisefloor(sc, nf, nf_ext);
2052 1.1 christos
2053 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c)) {
2054 1.1 christos cca_min = sc->sc_cca_min_2g;
2055 1.1 christos cca_max = sc->sc_cca_max_2g;
2056 1.1 christos }
2057 1.1 christos else {
2058 1.1 christos cca_min = sc->sc_cca_min_5g;
2059 1.1 christos cca_max = sc->sc_cca_max_5g;
2060 1.1 christos }
2061 1.1 christos /* Update noisefloor history. */
2062 1.1 christos for (i = 0; i < sc->sc_nrxchains; i++) {
2063 1.1 christos if (nf[i] < cca_min)
2064 1.1 christos nf[i] = cca_min;
2065 1.1 christos else if (nf[i] > cca_max)
2066 1.1 christos nf[i] = cca_max;
2067 1.1 christos if (nf_ext[i] < cca_min)
2068 1.1 christos nf_ext[i] = cca_min;
2069 1.1 christos else if (nf_ext[i] > cca_max)
2070 1.1 christos nf_ext[i] = cca_max;
2071 1.1 christos
2072 1.1 christos sc->sc_nf_hist[sc->sc_nf_hist_cur].nf[i] = nf[i];
2073 1.1 christos sc->sc_nf_hist[sc->sc_nf_hist_cur].nf_ext[i] = nf_ext[i];
2074 1.1 christos }
2075 1.1 christos if (++sc->sc_nf_hist_cur >= ATHN_NF_CAL_HIST_MAX)
2076 1.1 christos sc->sc_nf_hist_cur = 0;
2077 1.1 christos }
2078 1.1 christos #endif /* notused */
2079 1.1 christos
2080 1.1 christos #ifdef notused
2081 1.1 christos Static void
2082 1.1 christos ar9003_bb_load_noisefloor(struct athn_softc *sc)
2083 1.1 christos {
2084 1.1 christos int16_t nf[AR_MAX_CHAINS], nf_ext[AR_MAX_CHAINS];
2085 1.1 christos int i, ntries;
2086 1.1 christos
2087 1.1 christos /* Write filtered noisefloor values. */
2088 1.1 christos for (i = 0; i < sc->sc_nrxchains; i++) {
2089 1.1 christos nf[i] = sc->sc_nf_priv[i] * 2;
2090 1.1 christos nf_ext[i] = sc->sc_nf_ext_priv[i] * 2;
2091 1.1 christos }
2092 1.1 christos ar9003_write_noisefloor(sc, nf, nf_ext);
2093 1.1 christos
2094 1.1 christos /* Load filtered noisefloor values into baseband. */
2095 1.1 christos AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
2096 1.1 christos AR_CLRBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
2097 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
2098 1.1 christos /* Wait for load to complete. */
2099 1.1 christos for (ntries = 0; ntries < 1000; ntries++) {
2100 1.1 christos if (!(AR_READ(sc, AR_PHY_AGC_CONTROL) & AR_PHY_AGC_CONTROL_NF))
2101 1.1 christos break;
2102 1.1 christos DELAY(10);
2103 1.1 christos }
2104 1.1 christos if (ntries == 1000) {
2105 1.1 christos DPRINTFN(DBG_RF, sc, "failed to load noisefloor values\n");
2106 1.1 christos return;
2107 1.1 christos }
2108 1.1 christos
2109 1.1 christos /* Restore noisefloor values to initial (max) values. */
2110 1.1 christos for (i = 0; i < AR_MAX_CHAINS; i++)
2111 1.1 christos nf[i] = nf_ext[i] = -50 * 2;
2112 1.1 christos ar9003_write_noisefloor(sc, nf, nf_ext);
2113 1.1 christos }
2114 1.1 christos #endif /* notused */
2115 1.1 christos
2116 1.1 christos #ifdef notused
2117 1.1 christos Static void
2118 1.1 christos ar9300_noisefloor_calib(struct athn_softc *sc)
2119 1.1 christos {
2120 1.1 christos
2121 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_ENABLE_NF);
2122 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
2123 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
2124 1.1 christos }
2125 1.1 christos #endif /* notused */
2126 1.1 christos
2127 1.1 christos Static void
2128 1.1 christos ar9003_do_noisefloor_calib(struct athn_softc *sc)
2129 1.1 christos {
2130 1.1 christos
2131 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
2132 1.1 christos }
2133 1.1 christos
2134 1.1 christos PUBLIC int
2135 1.1 christos ar9003_init_calib(struct athn_softc *sc)
2136 1.1 christos {
2137 1.1 christos uint8_t txchainmask, rxchainmask;
2138 1.1 christos uint32_t reg;
2139 1.1 christos int ntries;
2140 1.1 christos
2141 1.1 christos /* Save chains masks. */
2142 1.1 christos txchainmask = sc->sc_txchainmask;
2143 1.1 christos rxchainmask = sc->sc_rxchainmask;
2144 1.1 christos /* Configure hardware before calibration. */
2145 1.1 christos if (AR_READ(sc, AR_ENT_OTP) & AR_ENT_OTP_CHAIN2_DISABLE)
2146 1.1 christos txchainmask = rxchainmask = 0x3;
2147 1.1 christos else
2148 1.1 christos txchainmask = rxchainmask = 0x7;
2149 1.1 christos ar9003_init_chains(sc);
2150 1.1 christos
2151 1.1 christos /* Perform Tx IQ calibration. */
2152 1.1 christos ar9003_calib_tx_iq(sc);
2153 1.1 christos /* Disable and re-enable the PHY chips. */
2154 1.1 christos AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
2155 1.1 christos AR_WRITE_BARRIER(sc);
2156 1.1 christos DELAY(5);
2157 1.1 christos AR_WRITE(sc, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
2158 1.1 christos
2159 1.1 christos /* Calibrate the AGC. */
2160 1.1 christos AR_SETBITS(sc, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_CAL);
2161 1.1 christos /* Poll for offset calibration completion. */
2162 1.1 christos for (ntries = 0; ntries < 10000; ntries++) {
2163 1.1 christos reg = AR_READ(sc, AR_PHY_AGC_CONTROL);
2164 1.1 christos if (!(reg & AR_PHY_AGC_CONTROL_CAL))
2165 1.1 christos break;
2166 1.1 christos DELAY(10);
2167 1.1 christos }
2168 1.1 christos if (ntries == 10000)
2169 1.1 christos return ETIMEDOUT;
2170 1.1 christos
2171 1.1 christos /* Restore chains masks. */
2172 1.1 christos sc->sc_txchainmask = txchainmask;
2173 1.1 christos sc->sc_rxchainmask = rxchainmask;
2174 1.1 christos ar9003_init_chains(sc);
2175 1.1 christos
2176 1.1 christos return 0;
2177 1.1 christos }
2178 1.1 christos
2179 1.1 christos Static void
2180 1.1 christos ar9003_do_calib(struct athn_softc *sc)
2181 1.1 christos {
2182 1.1 christos uint32_t reg;
2183 1.1 christos
2184 1.1 christos if (sc->sc_cur_calib_mask & ATHN_CAL_IQ) {
2185 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING4);
2186 1.1 christos reg = RW(reg, AR_PHY_TIMING4_IQCAL_LOG_COUNT_MAX, 10);
2187 1.1 christos AR_WRITE(sc, AR_PHY_TIMING4, reg);
2188 1.1 christos AR_WRITE(sc, AR_PHY_CALMODE, AR_PHY_CALMODE_IQ);
2189 1.1 christos AR_SETBITS(sc, AR_PHY_TIMING4, AR_PHY_TIMING4_DO_CAL);
2190 1.1 christos AR_WRITE_BARRIER(sc);
2191 1.1 christos }
2192 1.1 christos else if (sc->sc_cur_calib_mask & ATHN_CAL_TEMP) {
2193 1.1 christos AR_SETBITS(sc, AR_PHY_65NM_CH0_THERM,
2194 1.1 christos AR_PHY_65NM_CH0_THERM_LOCAL);
2195 1.1 christos AR_SETBITS(sc, AR_PHY_65NM_CH0_THERM,
2196 1.1 christos AR_PHY_65NM_CH0_THERM_START);
2197 1.1 christos AR_WRITE_BARRIER(sc);
2198 1.1 christos }
2199 1.1 christos }
2200 1.1 christos
2201 1.1 christos Static void
2202 1.1 christos ar9003_next_calib(struct athn_softc *sc)
2203 1.1 christos {
2204 1.1 christos /* Check if we have any calibration in progress. */
2205 1.1 christos if (sc->sc_cur_calib_mask != 0) {
2206 1.1 christos if (!(AR_READ(sc, AR_PHY_TIMING4) & AR_PHY_TIMING4_DO_CAL)) {
2207 1.1 christos /* Calibration completed for current sample. */
2208 1.1 christos ar9003_calib_iq(sc);
2209 1.1 christos }
2210 1.1 christos }
2211 1.1 christos }
2212 1.1 christos
2213 1.1 christos Static void
2214 1.1 christos ar9003_calib_iq(struct athn_softc *sc)
2215 1.1 christos {
2216 1.1 christos struct athn_iq_cal *cal;
2217 1.1 christos uint32_t reg, i_coff_denom, q_coff_denom;
2218 1.1 christos int32_t i_coff, q_coff;
2219 1.1 christos int i, iq_corr_neg;
2220 1.1 christos
2221 1.1 christos for (i = 0; i < AR_MAX_CHAINS; i++) {
2222 1.1 christos cal = &sc->sc_calib.iq[i];
2223 1.1 christos
2224 1.1 christos /* Read IQ calibration measures (clear on read). */
2225 1.1 christos cal->pwr_meas_i = AR_READ(sc, AR_PHY_IQ_ADC_MEAS_0_B(i));
2226 1.1 christos cal->pwr_meas_q = AR_READ(sc, AR_PHY_IQ_ADC_MEAS_1_B(i));
2227 1.1 christos cal->iq_corr_meas =
2228 1.1 christos (int32_t)AR_READ(sc, AR_PHY_IQ_ADC_MEAS_2_B(i));
2229 1.1 christos }
2230 1.1 christos
2231 1.1 christos for (i = 0; i < sc->sc_nrxchains; i++) {
2232 1.1 christos cal = &sc->sc_calib.iq[i];
2233 1.1 christos
2234 1.1 christos if (cal->pwr_meas_q == 0)
2235 1.1 christos continue;
2236 1.1 christos
2237 1.1 christos if ((iq_corr_neg = cal->iq_corr_meas < 0))
2238 1.1 christos cal->iq_corr_meas = -cal->iq_corr_meas;
2239 1.1 christos
2240 1.1 christos i_coff_denom =
2241 1.1 christos (cal->pwr_meas_i / 2 + cal->pwr_meas_q / 2) / 256;
2242 1.1 christos q_coff_denom = cal->pwr_meas_q / 64;
2243 1.1 christos
2244 1.1 christos if (i_coff_denom == 0 || q_coff_denom == 0)
2245 1.1 christos continue; /* Prevents division by zero. */
2246 1.1 christos
2247 1.1 christos i_coff = cal->iq_corr_meas / i_coff_denom;
2248 1.1 christos q_coff = (cal->pwr_meas_i / q_coff_denom) - 64;
2249 1.1 christos
2250 1.1 christos if (i_coff > 63)
2251 1.1 christos i_coff = 63;
2252 1.1 christos else if (i_coff < -63)
2253 1.1 christos i_coff = -63;
2254 1.1 christos /* Negate i_coff if iq_corr_meas is positive. */
2255 1.1 christos if (!iq_corr_neg)
2256 1.1 christos i_coff = -i_coff;
2257 1.1 christos if (q_coff > 63)
2258 1.1 christos q_coff = 63;
2259 1.1 christos else if (q_coff < -63)
2260 1.1 christos q_coff = -63;
2261 1.1 christos
2262 1.1 christos DPRINTFN(DBG_RF, sc, "IQ calibration for chain %d\n", i);
2263 1.1 christos reg = AR_READ(sc, AR_PHY_RX_IQCAL_CORR_B(i));
2264 1.1 christos reg = RW(reg, AR_PHY_RX_IQCAL_CORR_IQCORR_Q_I_COFF, i_coff);
2265 1.1 christos reg = RW(reg, AR_PHY_RX_IQCAL_CORR_IQCORR_Q_Q_COFF, q_coff);
2266 1.1 christos AR_WRITE(sc, AR_PHY_RX_IQCAL_CORR_B(i), reg);
2267 1.1 christos }
2268 1.1 christos
2269 1.1 christos /* Apply new settings. */
2270 1.1 christos AR_SETBITS(sc, AR_PHY_RX_IQCAL_CORR_B(0),
2271 1.1 christos AR_PHY_RX_IQCAL_CORR_IQCORR_ENABLE);
2272 1.1 christos AR_WRITE_BARRIER(sc);
2273 1.1 christos
2274 1.1 christos /* IQ calibration done. */
2275 1.1 christos sc->sc_cur_calib_mask &= ~ATHN_CAL_IQ;
2276 1.1 christos memset(&sc->sc_calib, 0, sizeof(sc->sc_calib));
2277 1.1 christos }
2278 1.1 christos
2279 1.1 christos #define DELPT 32
2280 1.1 christos Static int
2281 1.1 christos ar9003_get_iq_corr(struct athn_softc *sc, int32_t res[6], int32_t coeff[2])
2282 1.1 christos {
2283 1.1 christos /* Sign-extends 12-bit value (assumes upper bits are zeroes). */
2284 1.1 christos #define SIGN_EXT(v) (((v) ^ 0x800) - 0x800)
2285 1.1 christos #define SCALE (1 << 15)
2286 1.1 christos #define SHIFT (1 << 8)
2287 1.1 christos struct {
2288 1.1 christos int32_t m, p, c;
2289 1.1 christos } val[2][2];
2290 1.1 christos int32_t mag[2][2], phs[2][2], cos[2], sin[2];
2291 1.1 christos int32_t div, f1, f2, f3, m, p, c;
2292 1.1 christos int32_t txmag, txphs, rxmag, rxphs;
2293 1.1 christos int32_t q_coff, i_coff;
2294 1.1 christos int i, j;
2295 1.1 christos
2296 1.1 christos /* Extract our twelve signed 12-bit values from res[] array. */
2297 1.1 christos val[0][0].m = res[0] & 0xfff;
2298 1.1 christos val[0][0].p = (res[0] >> 12) & 0xfff;
2299 1.1 christos val[0][0].c = ((res[0] >> 24) & 0xff) | (res[1] & 0xf) << 8;
2300 1.1 christos
2301 1.1 christos val[0][1].m = (res[1] >> 4) & 0xfff;
2302 1.1 christos val[0][1].p = res[2] & 0xfff;
2303 1.1 christos val[0][1].c = (res[2] >> 12) & 0xfff;
2304 1.1 christos
2305 1.1 christos val[1][0].m = ((res[2] >> 24) & 0xff) | (res[3] & 0xf) << 8;
2306 1.1 christos val[1][0].p = (res[3] >> 4) & 0xfff;
2307 1.1 christos val[1][0].c = res[4] & 0xfff;
2308 1.1 christos
2309 1.1 christos val[1][1].m = (res[4] >> 12) & 0xfff;
2310 1.1 christos val[1][1].p = ((res[4] >> 24) & 0xff) | (res[5] & 0xf) << 8;
2311 1.1 christos val[1][1].c = (res[5] >> 4) & 0xfff;
2312 1.1 christos
2313 1.1 christos for (i = 0; i < 2; i++) {
2314 1.1 christos int32_t ymin, ymax;
2315 1.1 christos for (j = 0; j < 2; j++) {
2316 1.1 christos m = SIGN_EXT(val[i][j].m);
2317 1.1 christos p = SIGN_EXT(val[i][j].p);
2318 1.1 christos c = SIGN_EXT(val[i][j].c);
2319 1.1 christos
2320 1.1 christos if (p == 0)
2321 1.1 christos return 1; /* Prevent division by 0. */
2322 1.1 christos
2323 1.1 christos mag[i][j] = (m * SCALE) / p;
2324 1.1 christos phs[i][j] = (c * SCALE) / p;
2325 1.1 christos }
2326 1.1 christos sin[i] = ((mag[i][0] - mag[i][1]) * SHIFT) / DELPT;
2327 1.1 christos cos[i] = ((phs[i][0] - phs[i][1]) * SHIFT) / DELPT;
2328 1.1 christos /* Find magnitude by approximation. */
2329 1.1 christos ymin = MIN(abs(sin[i]), abs(cos[i]));
2330 1.1 christos ymax = MAX(abs(sin[i]), abs(cos[i]));
2331 1.1 christos div = ymax - (ymax / 32) + (ymin / 8) + (ymin / 4);
2332 1.1 christos if (div == 0)
2333 1.1 christos return 1; /* Prevent division by 0. */
2334 1.1 christos /* Normalize sin and cos by magnitude. */
2335 1.1 christos sin[i] = (sin[i] * SCALE) / div;
2336 1.1 christos cos[i] = (cos[i] * SCALE) / div;
2337 1.1 christos }
2338 1.1 christos
2339 1.1 christos /* Compute IQ mismatch (solve 4x4 linear equation). */
2340 1.1 christos f1 = cos[0] - cos[1];
2341 1.1 christos f3 = sin[0] - sin[1];
2342 1.1 christos f2 = (f1 * f1 + f3 * f3) / SCALE;
2343 1.1 christos if (f2 == 0)
2344 1.1 christos return 1; /* Prevent division by 0. */
2345 1.1 christos
2346 1.1 christos /* Compute Tx magnitude mismatch. */
2347 1.1 christos txmag = (f1 * ( mag[0][0] - mag[1][0]) +
2348 1.1 christos f3 * ( phs[0][0] - phs[1][0])) / f2;
2349 1.1 christos /* Compute Tx phase mismatch. */
2350 1.1 christos txphs = (f3 * (-mag[0][0] + mag[1][0]) +
2351 1.1 christos f1 * ( phs[0][0] - phs[1][0])) / f2;
2352 1.1 christos
2353 1.1 christos if (txmag == SCALE)
2354 1.1 christos return 1; /* Prevent division by 0. */
2355 1.1 christos
2356 1.1 christos /* Compute Rx magnitude mismatch. */
2357 1.1 christos rxmag = mag[0][0] - (cos[0] * txmag + sin[0] * txphs) / SCALE;
2358 1.1 christos /* Compute Rx phase mismatch. */
2359 1.1 christos rxphs = phs[0][0] + (sin[0] * txmag - cos[0] * txphs) / SCALE;
2360 1.1 christos
2361 1.1 christos if (-rxmag == SCALE)
2362 1.1 christos return 1; /* Prevent division by 0. */
2363 1.1 christos
2364 1.1 christos txmag = (txmag * SCALE) / (SCALE - txmag);
2365 1.1 christos txphs = -txphs;
2366 1.1 christos
2367 1.1 christos q_coff = (txmag * 128) / SCALE;
2368 1.1 christos if (q_coff < -63)
2369 1.1 christos q_coff = -63;
2370 1.1 christos else if (q_coff > 63)
2371 1.1 christos q_coff = 63;
2372 1.1 christos i_coff = (txphs * 256) / SCALE;
2373 1.1 christos if (i_coff < -63)
2374 1.1 christos i_coff = -63;
2375 1.1 christos else if (i_coff > 63)
2376 1.1 christos i_coff = 63;
2377 1.1 christos coeff[0] = q_coff * 128 + i_coff;
2378 1.1 christos
2379 1.1 christos rxmag = (-rxmag * SCALE) / (SCALE + rxmag);
2380 1.1 christos rxphs = -rxphs;
2381 1.1 christos
2382 1.1 christos q_coff = (rxmag * 128) / SCALE;
2383 1.1 christos if (q_coff < -63)
2384 1.1 christos q_coff = -63;
2385 1.1 christos else if (q_coff > 63)
2386 1.1 christos q_coff = 63;
2387 1.1 christos i_coff = (rxphs * 256) / SCALE;
2388 1.1 christos if (i_coff < -63)
2389 1.1 christos i_coff = -63;
2390 1.1 christos else if (i_coff > 63)
2391 1.1 christos i_coff = 63;
2392 1.1 christos coeff[1] = q_coff * 128 + i_coff;
2393 1.1 christos
2394 1.1 christos return 0;
2395 1.1 christos #undef SHIFT
2396 1.1 christos #undef SCALE
2397 1.1 christos #undef SIGN_EXT
2398 1.1 christos }
2399 1.1 christos
2400 1.1 christos Static int
2401 1.1 christos ar9003_calib_tx_iq(struct athn_softc *sc)
2402 1.1 christos {
2403 1.1 christos uint32_t reg;
2404 1.1 christos int32_t res[6], coeff[2];
2405 1.1 christos int i, j, ntries;
2406 1.1 christos
2407 1.1 christos reg = AR_READ(sc, AR_PHY_TX_IQCAL_CONTROL_1);
2408 1.1 christos reg = RW(reg, AR_PHY_TX_IQCAQL_CONTROL_1_IQCORR_I_Q_COFF_DELPT, DELPT);
2409 1.1 christos AR_WRITE(sc, AR_PHY_TX_IQCAL_CONTROL_1, reg);
2410 1.1 christos
2411 1.1 christos /* Start Tx IQ calibration. */
2412 1.1 christos AR_SETBITS(sc, AR_PHY_TX_IQCAL_START, AR_PHY_TX_IQCAL_START_DO_CAL);
2413 1.1 christos /* Wait for completion. */
2414 1.1 christos for (ntries = 0; ntries < 10000; ntries++) {
2415 1.1 christos reg = AR_READ(sc, AR_PHY_TX_IQCAL_START);
2416 1.1 christos if (!(reg & AR_PHY_TX_IQCAL_START_DO_CAL))
2417 1.1 christos break;
2418 1.1 christos DELAY(10);
2419 1.1 christos }
2420 1.1 christos if (ntries == 10000)
2421 1.1 christos return ETIMEDOUT;
2422 1.1 christos
2423 1.1 christos for (i = 0; i < sc->sc_ntxchains; i++) {
2424 1.1 christos /* Read Tx IQ calibration status for this chain. */
2425 1.1 christos reg = AR_READ(sc, AR_PHY_TX_IQCAL_STATUS_B(i));
2426 1.1 christos if (reg & AR_PHY_TX_IQCAL_STATUS_FAILED)
2427 1.1 christos return EIO;
2428 1.1 christos /*
2429 1.1 christos * Read Tx IQ calibration results for this chain.
2430 1.1 christos * This consists in twelve signed 12-bit values.
2431 1.1 christos */
2432 1.1 christos for (j = 0; j < 3; j++) {
2433 1.1 christos AR_CLRBITS(sc, AR_PHY_CHAN_INFO_MEMORY,
2434 1.1 christos AR_PHY_CHAN_INFO_TAB_S2_READ);
2435 1.1 christos reg = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(i, j));
2436 1.1 christos res[j * 2 + 0] = reg;
2437 1.1 christos
2438 1.1 christos AR_SETBITS(sc, AR_PHY_CHAN_INFO_MEMORY,
2439 1.1 christos AR_PHY_CHAN_INFO_TAB_S2_READ);
2440 1.1 christos reg = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(i, j));
2441 1.1 christos res[j * 2 + 1] = reg & 0xffff;
2442 1.1 christos }
2443 1.1 christos
2444 1.1 christos /* Compute Tx IQ correction. */
2445 1.1 christos if (ar9003_get_iq_corr(sc, res, coeff) != 0)
2446 1.1 christos return EIO;
2447 1.1 christos
2448 1.1 christos /* Write Tx IQ correction coefficients. */
2449 1.1 christos reg = AR_READ(sc, AR_PHY_TX_IQCAL_CORR_COEFF_01_B(i));
2450 1.1 christos reg = RW(reg, AR_PHY_TX_IQCAL_CORR_COEFF_01_COEFF_TABLE,
2451 1.1 christos coeff[0]);
2452 1.1 christos AR_WRITE(sc, AR_PHY_TX_IQCAL_CORR_COEFF_01_B(i), reg);
2453 1.1 christos
2454 1.1 christos reg = AR_READ(sc, AR_PHY_RX_IQCAL_CORR_B(i));
2455 1.1 christos reg = RW(reg, AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_Q_COFF,
2456 1.1 christos coeff[1] >> 7);
2457 1.1 christos reg = RW(reg, AR_PHY_RX_IQCAL_CORR_LOOPBACK_IQCORR_Q_I_COFF,
2458 1.1 christos coeff[1]);
2459 1.1 christos AR_WRITE(sc, AR_PHY_RX_IQCAL_CORR_B(i), reg);
2460 1.1 christos AR_WRITE_BARRIER(sc);
2461 1.1 christos }
2462 1.1 christos
2463 1.1 christos /* Enable Tx IQ correction. */
2464 1.1 christos AR_SETBITS(sc, AR_PHY_TX_IQCAL_CONTROL_3,
2465 1.1 christos AR_PHY_TX_IQCAL_CONTROL_3_IQCORR_EN);
2466 1.1 christos AR_SETBITS(sc, AR_PHY_RX_IQCAL_CORR_B(0),
2467 1.1 christos AR_PHY_RX_IQCAL_CORR_B0_LOOPBACK_IQCORR_EN);
2468 1.1 christos AR_WRITE_BARRIER(sc);
2469 1.1 christos return 0;
2470 1.1 christos }
2471 1.1 christos #undef DELPT
2472 1.1 christos
2473 1.1 christos /*-
2474 1.1 christos * The power amplifier predistortion state machine works as follows:
2475 1.1 christos * 1) Disable digital predistorters for all Tx chains
2476 1.1 christos * 2) Repeat steps 3~7 for all Tx chains
2477 1.1 christos * 3) Force Tx gain to that of training signal
2478 1.1 christos * 4) Send training signal (asynchronous)
2479 1.1 christos * 5) Wait for training signal to complete (asynchronous)
2480 1.1 christos * 6) Read PA measurements (input power, output power, output phase)
2481 1.1 christos * 7) Compute the predistortion function that linearizes PA output
2482 1.1 christos * 8) Write predistortion functions to hardware tables for all Tx chains
2483 1.1 christos * 9) Enable digital predistorters for all Tx chains
2484 1.1 christos */
2485 1.1 christos #ifdef notused
2486 1.1 christos Static void
2487 1.1 christos ar9003_paprd_calib(struct athn_softc *sc, struct ieee80211_channel *c)
2488 1.1 christos {
2489 1.1 christos static const int scaling[] = {
2490 1.1 christos 261376, 248079, 233759, 220464,
2491 1.1 christos 208194, 196949, 185706, 175487
2492 1.1 christos };
2493 1.1 christos struct athn_ops *ops = &sc->sc_ops;
2494 1.1 christos uint32_t reg, ht20mask, ht40mask;
2495 1.1 christos int i;
2496 1.1 christos
2497 1.1 christos /* Read PA predistortion masks from ROM. */
2498 1.1 christos ops->get_paprd_masks(sc, c, &ht20mask, &ht40mask);
2499 1.1 christos
2500 1.1 christos /* AM-to-AM: amplifier's amplitude characteristic. */
2501 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_AM2AM);
2502 1.1 christos reg = RW(reg, AR_PHY_PAPRD_AM2AM_MASK, ht20mask);
2503 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_AM2AM, reg);
2504 1.1 christos
2505 1.1 christos /* AM-to-PM: amplifier's phase transfer characteristic. */
2506 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_AM2PM);
2507 1.1 christos reg = RW(reg, AR_PHY_PAPRD_AM2PM_MASK, ht20mask);
2508 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_AM2PM, reg);
2509 1.1 christos
2510 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_HT40);
2511 1.1 christos reg = RW(reg, AR_PHY_PAPRD_HT40_MASK, ht40mask);
2512 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_HT40, reg);
2513 1.1 christos
2514 1.1 christos for (i = 0; i < AR9003_MAX_CHAINS; i++) {
2515 1.1 christos AR_SETBITS(sc, AR_PHY_PAPRD_CTRL0_B(i),
2516 1.1 christos AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE);
2517 1.1 christos
2518 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_CTRL1_B(i));
2519 1.1 christos reg = RW(reg, AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT, 181);
2520 1.1 christos reg = RW(reg, AR_PHY_PAPRD_CTRL1_MAG_SCALE_FACT, 361);
2521 1.1 christos reg &= ~AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA;
2522 1.1 christos reg |= AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENA;
2523 1.1 christos reg |= AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENA;
2524 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_CTRL1_B(i), reg);
2525 1.1 christos
2526 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_CTRL0_B(i));
2527 1.1 christos reg = RW(reg, AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
2528 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_CTRL0_B(i), reg);
2529 1.1 christos }
2530 1.1 christos
2531 1.1 christos /* Disable all digital predistorters during calibration. */
2532 1.1 christos for (i = 0; i < AR9003_MAX_CHAINS; i++) {
2533 1.1 christos AR_CLRBITS(sc, AR_PHY_PAPRD_CTRL0_B(i),
2534 1.1 christos AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE);
2535 1.1 christos }
2536 1.1 christos AR_WRITE_BARRIER(sc);
2537 1.1 christos
2538 1.1 christos /*
2539 1.1 christos * Configure training signal.
2540 1.1 christos */
2541 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL1);
2542 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL1_AGC2_SETTLING, 28);
2543 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL1_LB_SKIP, 0x30);
2544 1.1 christos reg &= ~AR_PHY_PAPRD_TRAINER_CNTL1_RX_BB_GAIN_FORCE;
2545 1.1 christos reg &= ~AR_PHY_PAPRD_TRAINER_CNTL1_IQCORR_ENABLE;
2546 1.1 christos reg |= AR_PHY_PAPRD_TRAINER_CNTL1_LB_ENABLE;
2547 1.1 christos reg |= AR_PHY_PAPRD_TRAINER_CNTL1_TX_GAIN_FORCE;
2548 1.1 christos reg |= AR_PHY_PAPRD_TRAINER_CNTL1_TRAIN_ENABLE;
2549 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL1, reg);
2550 1.1 christos
2551 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL2, 147);
2552 1.1 christos
2553 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL3);
2554 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_FINE_CORR_LEN, 4);
2555 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_COARSE_CORR_LEN, 4);
2556 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_NUM_CORR_STAGES, 7);
2557 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_MIN_LOOPBACK_DEL, 1);
2558 1.1 christos if (AR_SREV_9485(sc))
2559 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_QUICK_DROP, -3);
2560 1.1 christos else
2561 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_QUICK_DROP, -6);
2562 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL3_ADC_DESIRED_SIZE, -15);
2563 1.1 christos reg |= AR_PHY_PAPRD_TRAINER_CNTL3_BBTXMIX_DISABLE;
2564 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL3, reg);
2565 1.1 christos
2566 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_TRAINER_CNTL4);
2567 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_SAFETY_DELTA, 0);
2568 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_MIN_CORR, 400);
2569 1.1 christos reg = RW(reg, AR_PHY_PAPRD_TRAINER_CNTL4_NUM_TRAIN_SAMPLES, 100);
2570 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_TRAINER_CNTL4, reg);
2571 1.1 christos
2572 1.1 christos for (i = 0; i < __arraycount(scaling); i++) {
2573 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_PRE_POST_SCALE_B0(i));
2574 1.1 christos reg = RW(reg, AR_PHY_PAPRD_PRE_POST_SCALING, scaling[i]);
2575 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_PRE_POST_SCALE_B0(i), reg);
2576 1.1 christos }
2577 1.1 christos
2578 1.1 christos /* Save Tx gain table. */
2579 1.1 christos for (i = 0; i < AR9003_TX_GAIN_TABLE_SIZE; i++)
2580 1.1 christos sc->sc_txgain[i] = AR_READ(sc, AR_PHY_TXGAIN_TABLE(i));
2581 1.1 christos
2582 1.1 christos /* Set Tx power of training signal (use setting for MCS0). */
2583 1.1 christos sc->sc_trainpow = MS(AR_READ(sc, AR_PHY_PWRTX_RATE5),
2584 1.1 christos AR_PHY_PWRTX_RATE5_POWERTXHT20_0) - 4;
2585 1.1 christos
2586 1.1 christos /*
2587 1.1 christos * Start PA predistortion calibration state machine.
2588 1.1 christos */
2589 1.1 christos /* Find first available Tx chain. */
2590 1.1 christos sc->sc_paprd_curchain = 0;
2591 1.1 christos while (!(sc->sc_txchainmask & (1 << sc->sc_paprd_curchain)))
2592 1.1 christos sc->sc_paprd_curchain++;
2593 1.1 christos
2594 1.1 christos /* Make sure training done bit is clear. */
2595 1.1 christos AR_CLRBITS(sc, AR_PHY_PAPRD_TRAINER_STAT1,
2596 1.1 christos AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE);
2597 1.1 christos AR_WRITE_BARRIER(sc);
2598 1.1 christos
2599 1.1 christos /* Transmit training signal. */
2600 1.1 christos ar9003_paprd_tx_tone(sc);
2601 1.1 christos }
2602 1.1 christos #endif /* notused */
2603 1.1 christos
2604 1.1 christos Static int
2605 1.1 christos ar9003_get_desired_txgain(struct athn_softc *sc, int chain, int pow)
2606 1.1 christos {
2607 1.1 christos int32_t scale, atemp, avolt, tempcal, voltcal, temp, volt;
2608 1.1 christos int32_t tempcorr, voltcorr;
2609 1.1 christos uint32_t reg;
2610 1.1 christos int8_t delta;
2611 1.1 christos
2612 1.1 christos scale = MS(AR_READ(sc, AR_PHY_TPC_12),
2613 1.1 christos AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
2614 1.1 christos
2615 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_19);
2616 1.1 christos atemp = MS(reg, AR_PHY_TPC_19_ALPHA_THERM);
2617 1.1 christos avolt = MS(reg, AR_PHY_TPC_19_ALPHA_VOLT);
2618 1.1 christos
2619 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_18);
2620 1.1 christos tempcal = MS(reg, AR_PHY_TPC_18_THERM_CAL);
2621 1.1 christos voltcal = MS(reg, AR_PHY_TPC_18_VOLT_CAL);
2622 1.1 christos
2623 1.1 christos reg = AR_READ(sc, AR_PHY_BB_THERM_ADC_4);
2624 1.1 christos temp = MS(reg, AR_PHY_BB_THERM_ADC_4_LATEST_THERM);
2625 1.1 christos volt = MS(reg, AR_PHY_BB_THERM_ADC_4_LATEST_VOLT);
2626 1.1 christos
2627 1.1 christos delta = (int8_t)MS(AR_READ(sc, AR_PHY_TPC_11_B(chain)),
2628 1.1 christos AR_PHY_TPC_11_OLPC_GAIN_DELTA);
2629 1.1 christos
2630 1.1 christos /* Compute temperature and voltage correction. */
2631 1.1 christos tempcorr = (atemp * (temp - tempcal) + 128) / 256;
2632 1.1 christos voltcorr = (avolt * (volt - voltcal) + 64) / 128;
2633 1.1 christos
2634 1.1 christos /* Compute desired Tx gain. */
2635 1.1 christos return pow - delta - tempcorr - voltcorr + scale;
2636 1.1 christos }
2637 1.1 christos
2638 1.1 christos Static void
2639 1.1 christos ar9003_force_txgain(struct athn_softc *sc, uint32_t txgain)
2640 1.1 christos {
2641 1.1 christos uint32_t reg;
2642 1.1 christos
2643 1.1 christos reg = AR_READ(sc, AR_PHY_TX_FORCED_GAIN);
2644 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXBB1DBGAIN,
2645 1.1 christos MS(txgain, AR_PHY_TXGAIN_TXBB1DBGAIN));
2646 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXBB6DBGAIN,
2647 1.1 christos MS(txgain, AR_PHY_TXGAIN_TXBB6DBGAIN));
2648 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_TXMXRGAIN,
2649 1.1 christos MS(txgain, AR_PHY_TXGAIN_TXMXRGAIN));
2650 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNA,
2651 1.1 christos MS(txgain, AR_PHY_TXGAIN_PADRVGNA));
2652 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNB,
2653 1.1 christos MS(txgain, AR_PHY_TXGAIN_PADRVGNB));
2654 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGNC,
2655 1.1 christos MS(txgain, AR_PHY_TXGAIN_PADRVGNC));
2656 1.1 christos reg = RW(reg, AR_PHY_TX_FORCED_GAIN_PADRVGND,
2657 1.1 christos MS(txgain, AR_PHY_TXGAIN_PADRVGND));
2658 1.1 christos reg &= ~AR_PHY_TX_FORCED_GAIN_ENABLE_PAL;
2659 1.1 christos reg &= ~AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN;
2660 1.1 christos AR_WRITE(sc, AR_PHY_TX_FORCED_GAIN, reg);
2661 1.1 christos
2662 1.1 christos reg = AR_READ(sc, AR_PHY_TPC_1);
2663 1.1 christos reg = RW(reg, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
2664 1.1 christos reg &= ~AR_PHY_TPC_1_FORCE_DAC_GAIN;
2665 1.1 christos AR_WRITE(sc, AR_PHY_TPC_1, reg);
2666 1.1 christos AR_WRITE_BARRIER(sc);
2667 1.1 christos }
2668 1.1 christos
2669 1.1 christos Static void
2670 1.1 christos ar9003_set_training_gain(struct athn_softc *sc, int chain)
2671 1.1 christos {
2672 1.1 christos size_t i;
2673 1.1 christos int gain;
2674 1.1 christos
2675 1.1 christos /*
2676 1.1 christos * Get desired gain for training signal power (take into account
2677 1.1 christos * current temperature/voltage).
2678 1.1 christos */
2679 1.1 christos gain = ar9003_get_desired_txgain(sc, chain, sc->sc_trainpow);
2680 1.1 christos /* Find entry in table. */
2681 1.1 christos for (i = 0; i < AR9003_TX_GAIN_TABLE_SIZE - 1; i++)
2682 1.1 christos if ((int)MS(sc->sc_txgain[i], AR_PHY_TXGAIN_INDEX) >= gain)
2683 1.1 christos break;
2684 1.1 christos ar9003_force_txgain(sc, sc->sc_txgain[i]);
2685 1.1 christos }
2686 1.1 christos
2687 1.1 christos Static int
2688 1.1 christos ar9003_paprd_tx_tone(struct athn_softc *sc)
2689 1.1 christos {
2690 1.1 christos #define TONE_LEN 1800
2691 1.1 christos struct ieee80211com *ic = &sc->sc_ic;
2692 1.1 christos struct ieee80211_frame *wh;
2693 1.1 christos struct ieee80211_node *ni;
2694 1.1 christos struct mbuf *m;
2695 1.1 christos int error;
2696 1.1 christos
2697 1.1 christos /* Build a Null (no data) frame of TONE_LEN bytes. */
2698 1.1 christos m = MCLGETI(NULL, M_DONTWAIT, NULL, TONE_LEN);
2699 1.1 christos if (m == NULL)
2700 1.1 christos return ENOBUFS;
2701 1.1 christos memset(mtod(m, void *), 0, TONE_LEN);
2702 1.1 christos wh = mtod(m, struct ieee80211_frame *);
2703 1.1 christos wh->i_fc[0] = IEEE80211_FC0_TYPE_DATA | IEEE80211_FC0_SUBTYPE_NODATA;
2704 1.1 christos wh->i_fc[1] = IEEE80211_FC1_DIR_NODS;
2705 1.1 christos *(uint16_t *)wh->i_dur = htole16(10); /* XXX */
2706 1.1 christos IEEE80211_ADDR_COPY(wh->i_addr1, ic->ic_myaddr);
2707 1.1 christos IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr);
2708 1.1 christos IEEE80211_ADDR_COPY(wh->i_addr3, ic->ic_myaddr);
2709 1.1 christos m->m_pkthdr.len = m->m_len = TONE_LEN;
2710 1.1 christos
2711 1.1 christos /* Set gain of training signal. */
2712 1.1 christos ar9003_set_training_gain(sc, sc->sc_paprd_curchain);
2713 1.1 christos
2714 1.1 christos /* Transmit training signal. */
2715 1.1 christos ni = ieee80211_ref_node(ic->ic_bss);
2716 1.1 christos if ((error = ar9003_tx(sc, m, ni, ATHN_TXFLAG_PAPRD)) != 0)
2717 1.1 christos ieee80211_free_node(ni);
2718 1.1 christos return error;
2719 1.1 christos #undef TONE_LEN
2720 1.1 christos }
2721 1.1 christos
2722 1.1 christos static __inline int
2723 1.1 christos get_scale(int val)
2724 1.1 christos {
2725 1.1 christos int log = 0;
2726 1.1 christos
2727 1.1 christos /* Find the log base 2 (position of highest bit set). */
2728 1.1 christos while (val >>= 1)
2729 1.1 christos log++;
2730 1.1 christos
2731 1.1 christos return (log > 10) ? log - 10 : 0;
2732 1.1 christos }
2733 1.1 christos
2734 1.1 christos /*
2735 1.1 christos * Compute predistortion function to linearize power amplifier output based
2736 1.1 christos * on feedback from training signal.
2737 1.1 christos */
2738 1.1 christos Static int
2739 1.1 christos ar9003_compute_predistortion(struct athn_softc *sc, const uint32_t *lo,
2740 1.1 christos const uint32_t *hi)
2741 1.1 christos {
2742 1.1 christos #define NBINS 23
2743 1.1 christos int chain = sc->sc_paprd_curchain;
2744 1.1 christos int x[NBINS + 1], y[NBINS + 1], t[NBINS + 1];
2745 1.1 christos int b1[NBINS + 1], b2[NBINS + 1], xtilde[NBINS + 1];
2746 1.1 christos int nsamples, txsum, rxsum, rosum, maxidx;
2747 1.1 christos int order, order5x, order5xrem, order3x, order3xrem, y5, y3;
2748 1.1 christos int icept, G, I, L, M, angle, xnonlin, y2, y4, sumy2, sumy4;
2749 1.1 christos int alpha, beta, scale, Qalpha, Qbeta, Qscale, Qx, Qb1, Qb2;
2750 1.1 christos int tavg, ttilde, maxb1abs, maxb2abs, maxxtildeabs, in;
2751 1.1 christos int tmp, i;
2752 1.1 christos
2753 1.1 christos /* Set values at origin. */
2754 1.1 christos x[0] = y[0] = t[0] = 0;
2755 1.1 christos
2756 1.1 christos #define SCALE 32
2757 1.1 christos maxidx = 0;
2758 1.1 christos for (i = 0; i < NBINS; i++) {
2759 1.1 christos nsamples = lo[i] & 0xffff;
2760 1.1 christos /* Skip bins that contain 16 or less samples. */
2761 1.1 christos if (nsamples <= 16) {
2762 1.1 christos x[i + 1] = y[i + 1] = t[i + 1] = 0;
2763 1.1 christos continue;
2764 1.1 christos }
2765 1.1 christos txsum = (hi[i] & 0x7ff) << 16 | lo[i] >> 16;
2766 1.1 christos rxsum = (lo[i + NBINS] & 0xffff) << 5 |
2767 1.1 christos ((hi[i] >> 11) & 0x1f);
2768 1.1 christos rosum = (hi[i + NBINS] & 0x7ff) << 16 | hi[i + NBINS] >> 16;
2769 1.1 christos /* Sign-extend 27-bit value. */
2770 1.1 christos rosum = (rosum ^ 0x4000000) - 0x4000000;
2771 1.1 christos
2772 1.1 christos txsum *= SCALE;
2773 1.1 christos rxsum *= SCALE;
2774 1.1 christos rosum *= SCALE;
2775 1.1 christos
2776 1.1 christos x[i + 1] = ((txsum + nsamples) / nsamples + SCALE) / SCALE;
2777 1.1 christos y[i + 1] = ((rxsum + nsamples) / nsamples + SCALE) / SCALE +
2778 1.1 christos SCALE * maxidx + SCALE / 2;
2779 1.1 christos t[i + 1] = (rosum + nsamples) / nsamples;
2780 1.1 christos maxidx++;
2781 1.1 christos }
2782 1.1 christos #undef SCALE
2783 1.1 christos
2784 1.1 christos #define SCALE_LOG 8
2785 1.1 christos #define SCALE (1 << SCALE_LOG)
2786 1.1 christos if (x[6] == x[3])
2787 1.1 christos return 1; /* Prevent division by 0. */
2788 1.1 christos G = ((y[6] - y[3]) * SCALE + (x[6] - x[3])) / (x[6] - x[3]);
2789 1.1 christos if (G == 0)
2790 1.1 christos return 1; /* Prevent division by 0. */
2791 1.1 christos
2792 1.1 christos sc->sc_gain1[chain] = G; /* Save low signal gain. */
2793 1.1 christos
2794 1.1 christos /* Find interception point. */
2795 1.1 christos icept = (G * (x[0] - x[3]) + SCALE) / SCALE + y[3];
2796 1.1 christos for (i = 0; i <= 3; i++) {
2797 1.1 christos y[i] = i * 32;
2798 1.1 christos x[i] = (y[i] * SCALE + G) / G;
2799 1.1 christos }
2800 1.1 christos for (i = 4; i <= maxidx; i++)
2801 1.1 christos y[i] -= icept;
2802 1.1 christos
2803 1.1 christos xnonlin = x[maxidx] - (y[maxidx] * SCALE + G) / G;
2804 1.1 christos order = (xnonlin + y[maxidx]) / y[maxidx];
2805 1.1 christos if (order == 0)
2806 1.1 christos M = 10;
2807 1.1 christos else if (order == 1)
2808 1.1 christos M = 9;
2809 1.1 christos else
2810 1.1 christos M = 8;
2811 1.1 christos
2812 1.1 christos I = (maxidx >= 16) ? 7 : maxidx / 2;
2813 1.1 christos L = maxidx - I;
2814 1.1 christos
2815 1.1 christos sumy2 = sumy4 = y2 = y4 = 0;
2816 1.1 christos for (i = 0; i <= L; i++) {
2817 1.1 christos if (y[i + I] == 0)
2818 1.1 christos return 1; /* Prevent division by 0. */
2819 1.1 christos
2820 1.1 christos xnonlin = x[i + I] - ((y[i + I] * SCALE) + G) / G;
2821 1.1 christos xtilde[i] = ((xnonlin << M) + y[i + I]) / y[i + I];
2822 1.1 christos xtilde[i] = ((xtilde[i] << M) + y[i + I]) / y[i + I];
2823 1.1 christos xtilde[i] = ((xtilde[i] << M) + y[i + I]) / y[i + I];
2824 1.1 christos
2825 1.1 christos y2 = (y[i + I] * y[i + I] + SCALE * SCALE) / (SCALE * SCALE);
2826 1.1 christos
2827 1.1 christos sumy2 += y2;
2828 1.1 christos sumy4 += y2 * y2;
2829 1.1 christos
2830 1.1 christos b1[i] = y2 * (L + 1);
2831 1.1 christos b2[i] = y2;
2832 1.1 christos }
2833 1.1 christos for (i = 0; i <= L; i++) {
2834 1.1 christos b1[i] -= sumy2;
2835 1.1 christos b2[i] = sumy4 - sumy2 * b2[i];
2836 1.1 christos }
2837 1.1 christos
2838 1.1 christos maxxtildeabs = maxb1abs = maxb2abs = 0;
2839 1.1 christos for (i = 0; i <= L; i++) {
2840 1.1 christos tmp = abs(xtilde[i]);
2841 1.1 christos if (tmp > maxxtildeabs)
2842 1.1 christos maxxtildeabs = tmp;
2843 1.1 christos
2844 1.1 christos tmp = abs(b1[i]);
2845 1.1 christos if (tmp > maxb1abs)
2846 1.1 christos maxb1abs = tmp;
2847 1.1 christos
2848 1.1 christos tmp = abs(b2[i]);
2849 1.1 christos if (tmp > maxb2abs)
2850 1.1 christos maxb2abs = tmp;
2851 1.1 christos }
2852 1.1 christos Qx = get_scale(maxxtildeabs);
2853 1.1 christos Qb1 = get_scale(maxb1abs);
2854 1.1 christos Qb2 = get_scale(maxb2abs);
2855 1.1 christos for (i = 0; i <= L; i++) {
2856 1.1 christos xtilde[i] /= 1 << Qx;
2857 1.1 christos b1[i] /= 1 << Qb1;
2858 1.1 christos b2[i] /= 1 << Qb2;
2859 1.1 christos }
2860 1.1 christos
2861 1.1 christos alpha = beta = 0;
2862 1.1 christos for (i = 0; i <= L; i++) {
2863 1.1 christos alpha += b1[i] * xtilde[i];
2864 1.1 christos beta += b2[i] * xtilde[i];
2865 1.1 christos }
2866 1.1 christos
2867 1.1 christos scale = ((y4 / SCALE_LOG) * (L + 1) -
2868 1.1 christos (y2 / SCALE_LOG) * sumy2) * SCALE_LOG;
2869 1.1 christos
2870 1.1 christos Qscale = get_scale(abs(scale));
2871 1.1 christos scale /= 1 << Qscale;
2872 1.1 christos Qalpha = get_scale(abs(alpha));
2873 1.1 christos alpha /= 1 << Qalpha;
2874 1.1 christos Qbeta = get_scale(abs(beta));
2875 1.1 christos beta /= 1 << Qbeta;
2876 1.1 christos
2877 1.1 christos order = 3 * M - Qx - Qb1 - Qbeta + 10 + Qscale;
2878 1.1 christos order5x = 1 << (order / 5);
2879 1.1 christos order5xrem = 1 << (order % 5);
2880 1.1 christos
2881 1.1 christos order = 3 * M - Qx - Qb2 - Qalpha + 10 + Qscale;
2882 1.1 christos order3x = 1 << (order / 3);
2883 1.1 christos order3xrem = 1 << (order % 3);
2884 1.1 christos
2885 1.1 christos for (i = 0; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
2886 1.1 christos tmp = i * 32;
2887 1.1 christos
2888 1.1 christos /* Fifth order. */
2889 1.1 christos y5 = ((beta * tmp) / 64) / order5x;
2890 1.1 christos y5 = (y5 * tmp) / order5x;
2891 1.1 christos y5 = (y5 * tmp) / order5x;
2892 1.1 christos y5 = (y5 * tmp) / order5x;
2893 1.1 christos y5 = (y5 * tmp) / order5x;
2894 1.1 christos y5 = y5 / order5xrem;
2895 1.1 christos
2896 1.1 christos /* Third oder. */
2897 1.1 christos y3 = (alpha * tmp) / order3x;
2898 1.1 christos y3 = (y3 * tmp) / order3x;
2899 1.1 christos y3 = (y3 * tmp) / order3x;
2900 1.1 christos y3 = y3 / order3xrem;
2901 1.1 christos
2902 1.1 christos in = y5 + y3 + (SCALE * tmp) / G;
2903 1.1 christos if (i >= 2 && in < sc->sc_pa_in[chain][i - 1]) {
2904 1.1 christos in = sc->sc_pa_in[chain][i - 1] +
2905 1.1 christos (sc->sc_pa_in[chain][i - 1] -
2906 1.1 christos sc->sc_pa_in[chain][i - 2]);
2907 1.1 christos }
2908 1.1 christos if (in > 1400)
2909 1.1 christos in = 1400;
2910 1.1 christos sc->sc_pa_in[chain][i] = in;
2911 1.1 christos }
2912 1.1 christos
2913 1.1 christos /* Compute average theta of first 5 bins (linear region). */
2914 1.1 christos tavg = 0;
2915 1.1 christos for (i = 1; i <= 5; i++)
2916 1.1 christos tavg += t[i];
2917 1.1 christos tavg /= 5;
2918 1.1 christos for (i = 1; i <= 5; i++)
2919 1.1 christos t[i] = 0;
2920 1.1 christos for (i = 6; i <= maxidx; i++)
2921 1.1 christos t[i] -= tavg;
2922 1.1 christos
2923 1.1 christos alpha = beta = 0;
2924 1.1 christos for (i = 0; i <= L; i++) {
2925 1.1 christos ttilde = ((t[i + I] << M) + y[i + I]) / y[i + I];
2926 1.1 christos ttilde = ((ttilde << M) + y[i + I]) / y[i + I];
2927 1.1 christos ttilde = ((ttilde << M) + y[i + I]) / y[i + I];
2928 1.1 christos
2929 1.1 christos alpha += b2[i] * ttilde;
2930 1.1 christos beta += b1[i] * ttilde;
2931 1.1 christos }
2932 1.1 christos
2933 1.1 christos Qalpha = get_scale(abs(alpha));
2934 1.1 christos alpha /= 1 << Qalpha;
2935 1.1 christos Qbeta = get_scale(abs(beta));
2936 1.1 christos beta /= 1 << Qbeta;
2937 1.1 christos
2938 1.1 christos order = 3 * M - Qx - Qb1 - Qbeta + 10 + Qscale + 5;
2939 1.1 christos order5x = 1 << (order / 5);
2940 1.1 christos order5xrem = 1 << (order % 5);
2941 1.1 christos
2942 1.1 christos order = 3 * M - Qx - Qb2 - Qalpha + 10 + Qscale + 5;
2943 1.1 christos order3x = 1 << (order / 3);
2944 1.1 christos order3xrem = 1 << (order % 3);
2945 1.1 christos
2946 1.1 christos for (i = 0; i <= 4; i++)
2947 1.1 christos sc->sc_angle[chain][i] = 0; /* Linear at that range. */
2948 1.1 christos for (i = 5; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
2949 1.1 christos tmp = i * 32;
2950 1.1 christos
2951 1.1 christos /* Fifth order. */
2952 1.1 christos if (beta > 0)
2953 1.1 christos y5 = (((beta * tmp - 64) / 64) - order5x) / order5x;
2954 1.1 christos else
2955 1.1 christos y5 = (((beta * tmp - 64) / 64) + order5x) / order5x;
2956 1.1 christos y5 = (y5 * tmp) / order5x;
2957 1.1 christos y5 = (y5 * tmp) / order5x;
2958 1.1 christos y5 = (y5 * tmp) / order5x;
2959 1.1 christos y5 = (y5 * tmp) / order5x;
2960 1.1 christos y5 = y5 / order5xrem;
2961 1.1 christos
2962 1.1 christos /* Third oder. */
2963 1.1 christos if (beta > 0) /* XXX alpha? */
2964 1.1 christos y3 = (alpha * tmp - order3x) / order3x;
2965 1.1 christos else
2966 1.1 christos y3 = (alpha * tmp + order3x) / order3x;
2967 1.1 christos y3 = (y3 * tmp) / order3x;
2968 1.1 christos y3 = (y3 * tmp) / order3x;
2969 1.1 christos y3 = y3 / order3xrem;
2970 1.1 christos
2971 1.1 christos angle = y5 + y3;
2972 1.1 christos if (angle < -150)
2973 1.1 christos angle = -150;
2974 1.1 christos else if (angle > 150)
2975 1.1 christos angle = 150;
2976 1.1 christos sc->sc_angle[chain][i] = angle;
2977 1.1 christos }
2978 1.1 christos /* Angle for entry 4 is derived from angle for entry 5. */
2979 1.1 christos sc->sc_angle[chain][4] = (sc->sc_angle[chain][5] + 2) / 2;
2980 1.1 christos
2981 1.1 christos return 0;
2982 1.1 christos #undef SCALE
2983 1.1 christos #undef SCALE_LOG
2984 1.1 christos #undef NBINS
2985 1.1 christos }
2986 1.1 christos
2987 1.1 christos Static void
2988 1.1 christos ar9003_enable_predistorter(struct athn_softc *sc, int chain)
2989 1.1 christos {
2990 1.1 christos uint32_t reg;
2991 1.1 christos int i;
2992 1.1 christos
2993 1.1 christos /* Write digital predistorter lookup table. */
2994 1.1 christos for (i = 0; i < AR9003_PAPRD_MEM_TAB_SIZE; i++) {
2995 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_MEM_TAB_B(chain, i),
2996 1.1 christos SM(AR_PHY_PAPRD_PA_IN, sc->sc_pa_in[chain][i]) |
2997 1.1 christos SM(AR_PHY_PAPRD_ANGLE, sc->sc_angle[chain][i]));
2998 1.1 christos }
2999 1.1 christos
3000 1.1 christos reg = AR_READ(sc, AR_PHY_PA_GAIN123_B(chain));
3001 1.1 christos reg = RW(reg, AR_PHY_PA_GAIN123_PA_GAIN1, sc->sc_gain1[chain]);
3002 1.1 christos AR_WRITE(sc, AR_PHY_PA_GAIN123_B(chain), reg);
3003 1.1 christos
3004 1.1 christos /* Indicate Tx power used for calibration (training signal). */
3005 1.1 christos reg = AR_READ(sc, AR_PHY_PAPRD_CTRL1_B(chain));
3006 1.1 christos reg = RW(reg, AR_PHY_PAPRD_CTRL1_POWER_AT_AM2AM_CAL, sc->sc_trainpow);
3007 1.1 christos AR_WRITE(sc, AR_PHY_PAPRD_CTRL1_B(chain), reg);
3008 1.1 christos
3009 1.1 christos /* Enable digital predistorter for this chain. */
3010 1.1 christos AR_SETBITS(sc, AR_PHY_PAPRD_CTRL0_B(chain),
3011 1.1 christos AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE);
3012 1.1 christos AR_WRITE_BARRIER(sc);
3013 1.1 christos }
3014 1.1 christos
3015 1.1 christos Static void
3016 1.1 christos ar9003_paprd_enable(struct athn_softc *sc)
3017 1.1 christos {
3018 1.1 christos int i;
3019 1.1 christos
3020 1.1 christos /* Enable digital predistorters for all Tx chains. */
3021 1.1 christos for (i = 0; i < AR9003_MAX_CHAINS; i++)
3022 1.1 christos if (sc->sc_txchainmask & (1 << i))
3023 1.1 christos ar9003_enable_predistorter(sc, i);
3024 1.1 christos }
3025 1.1 christos
3026 1.1 christos /*
3027 1.1 christos * This function is called when our training signal has been sent.
3028 1.1 christos */
3029 1.1 christos Static void
3030 1.1 christos ar9003_paprd_tx_tone_done(struct athn_softc *sc)
3031 1.1 christos {
3032 1.1 christos uint32_t lo[48], hi[48];
3033 1.1 christos size_t i;
3034 1.1 christos
3035 1.1 christos /* Make sure training is complete. */
3036 1.1 christos if (!(AR_READ(sc, AR_PHY_PAPRD_TRAINER_STAT1) &
3037 1.1 christos AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE))
3038 1.1 christos return;
3039 1.1 christos
3040 1.1 christos /* Read feedback from training signal. */
3041 1.1 christos AR_CLRBITS(sc, AR_PHY_CHAN_INFO_MEMORY, AR_PHY_CHAN_INFO_TAB_S2_READ);
3042 1.1 christos for (i = 0; i < __arraycount(lo); i++)
3043 1.1 christos lo[i] = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(0, i));
3044 1.1 christos AR_SETBITS(sc, AR_PHY_CHAN_INFO_MEMORY, AR_PHY_CHAN_INFO_TAB_S2_READ);
3045 1.1 christos for (i = 0; i < __arraycount(hi); i++)
3046 1.1 christos hi[i] = AR_READ(sc, AR_PHY_CHAN_INFO_TAB(0, i));
3047 1.1 christos
3048 1.1 christos AR_CLRBITS(sc, AR_PHY_PAPRD_TRAINER_STAT1,
3049 1.1 christos AR_PHY_PAPRD_TRAINER_STAT1_TRAIN_DONE);
3050 1.1 christos
3051 1.1 christos /* Compute predistortion function based on this feedback. */
3052 1.1 christos if (ar9003_compute_predistortion(sc, lo, hi) != 0)
3053 1.1 christos return;
3054 1.1 christos
3055 1.1 christos /* Get next available Tx chain. */
3056 1.1 christos while (++sc->sc_paprd_curchain < AR9003_MAX_CHAINS)
3057 1.1 christos if (sc->sc_txchainmask & (1 << sc->sc_paprd_curchain))
3058 1.1 christos break;
3059 1.1 christos if (sc->sc_paprd_curchain == AR9003_MAX_CHAINS) {
3060 1.1 christos /* All Tx chains measured; enable digital predistortion. */
3061 1.1 christos ar9003_paprd_enable(sc);
3062 1.1 christos }
3063 1.1 christos else /* Measure next Tx chain. */
3064 1.1 christos ar9003_paprd_tx_tone(sc);
3065 1.1 christos }
3066 1.1 christos
3067 1.1 christos PUBLIC void
3068 1.1 christos ar9003_write_txpower(struct athn_softc *sc, int16_t power[ATHN_POWER_COUNT])
3069 1.1 christos {
3070 1.1 christos
3071 1.1 christos /* Make sure forced gain is disabled. */
3072 1.1 christos AR_WRITE(sc, AR_PHY_TX_FORCED_GAIN, 0);
3073 1.1 christos
3074 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE1,
3075 1.1 christos (power[ATHN_POWER_OFDM18 ] & 0x3f) << 24 |
3076 1.1 christos (power[ATHN_POWER_OFDM12 ] & 0x3f) << 16 |
3077 1.1 christos (power[ATHN_POWER_OFDM9 ] & 0x3f) << 8 |
3078 1.1 christos (power[ATHN_POWER_OFDM6 ] & 0x3f));
3079 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE2,
3080 1.1 christos (power[ATHN_POWER_OFDM54 ] & 0x3f) << 24 |
3081 1.1 christos (power[ATHN_POWER_OFDM48 ] & 0x3f) << 16 |
3082 1.1 christos (power[ATHN_POWER_OFDM36 ] & 0x3f) << 8 |
3083 1.1 christos (power[ATHN_POWER_OFDM24 ] & 0x3f));
3084 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE3,
3085 1.1 christos (power[ATHN_POWER_CCK2_SP ] & 0x3f) << 24 |
3086 1.1 christos (power[ATHN_POWER_CCK2_LP ] & 0x3f) << 16 |
3087 1.1 christos /* NB: No eXtended Range for AR9003. */
3088 1.1 christos (power[ATHN_POWER_CCK1_LP ] & 0x3f));
3089 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE4,
3090 1.1 christos (power[ATHN_POWER_CCK11_SP] & 0x3f) << 24 |
3091 1.1 christos (power[ATHN_POWER_CCK11_LP] & 0x3f) << 16 |
3092 1.1 christos (power[ATHN_POWER_CCK55_SP] & 0x3f) << 8 |
3093 1.1 christos (power[ATHN_POWER_CCK55_LP] & 0x3f));
3094 1.1 christos /*
3095 1.1 christos * NB: AR_PHY_PWRTX_RATE5 needs to be written even if HT is disabled
3096 1.1 christos * because it is read by PA predistortion functions.
3097 1.1 christos */
3098 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE5,
3099 1.1 christos (power[ATHN_POWER_HT20( 5)] & 0x3f) << 24 |
3100 1.1 christos (power[ATHN_POWER_HT20( 4)] & 0x3f) << 16 |
3101 1.1 christos (power[ATHN_POWER_HT20( 1)] & 0x3f) << 8 |
3102 1.1 christos (power[ATHN_POWER_HT20( 0)] & 0x3f));
3103 1.1 christos #ifndef IEEE80211_NO_HT
3104 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE6,
3105 1.1 christos (power[ATHN_POWER_HT20(13)] & 0x3f) << 24 |
3106 1.1 christos (power[ATHN_POWER_HT20(12)] & 0x3f) << 16 |
3107 1.1 christos (power[ATHN_POWER_HT20( 7)] & 0x3f) << 8 |
3108 1.1 christos (power[ATHN_POWER_HT20( 6)] & 0x3f));
3109 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE7,
3110 1.1 christos (power[ATHN_POWER_HT40( 5)] & 0x3f) << 24 |
3111 1.1 christos (power[ATHN_POWER_HT40( 4)] & 0x3f) << 16 |
3112 1.1 christos (power[ATHN_POWER_HT40( 1)] & 0x3f) << 8 |
3113 1.1 christos (power[ATHN_POWER_HT40( 0)] & 0x3f));
3114 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE8,
3115 1.1 christos (power[ATHN_POWER_HT40(13)] & 0x3f) << 24 |
3116 1.1 christos (power[ATHN_POWER_HT40(12)] & 0x3f) << 16 |
3117 1.1 christos (power[ATHN_POWER_HT40( 7)] & 0x3f) << 8 |
3118 1.1 christos (power[ATHN_POWER_HT40( 6)] & 0x3f));
3119 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE10,
3120 1.1 christos (power[ATHN_POWER_HT20(21)] & 0x3f) << 24 |
3121 1.1 christos (power[ATHN_POWER_HT20(20)] & 0x3f) << 16 |
3122 1.1 christos (power[ATHN_POWER_HT20(15)] & 0x3f) << 8 |
3123 1.1 christos (power[ATHN_POWER_HT20(14)] & 0x3f));
3124 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE11,
3125 1.1 christos (power[ATHN_POWER_HT40(23)] & 0x3f) << 24 |
3126 1.1 christos (power[ATHN_POWER_HT40(22)] & 0x3f) << 16 |
3127 1.1 christos (power[ATHN_POWER_HT20(23)] & 0x3f) << 8 |
3128 1.1 christos (power[ATHN_POWER_HT20(22)] & 0x3f));
3129 1.1 christos AR_WRITE(sc, AR_PHY_PWRTX_RATE12,
3130 1.1 christos (power[ATHN_POWER_HT40(21)] & 0x3f) << 24 |
3131 1.1 christos (power[ATHN_POWER_HT40(20)] & 0x3f) << 16 |
3132 1.1 christos (power[ATHN_POWER_HT40(15)] & 0x3f) << 8 |
3133 1.1 christos (power[ATHN_POWER_HT40(14)] & 0x3f));
3134 1.1 christos #endif
3135 1.1 christos AR_WRITE_BARRIER(sc);
3136 1.1 christos }
3137 1.1 christos
3138 1.1 christos Static void
3139 1.1 christos ar9003_reset_rx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
3140 1.1 christos {
3141 1.1 christos #define X(x) ((uint32_t)(x) << 2)
3142 1.1 christos const struct athn_gain *prog = sc->sc_rx_gain;
3143 1.1 christos const uint32_t *pvals;
3144 1.1 christos int i;
3145 1.1 christos
3146 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
3147 1.1 christos pvals = prog->vals_2g;
3148 1.1 christos else
3149 1.1 christos pvals = prog->vals_5g;
3150 1.1 christos for (i = 0; i < prog->nregs; i++)
3151 1.1 christos AR_WRITE(sc, X(prog->regs[i]), pvals[i]);
3152 1.1 christos AR_WRITE_BARRIER(sc);
3153 1.1 christos #undef X
3154 1.1 christos }
3155 1.1 christos
3156 1.1 christos Static void
3157 1.1 christos ar9003_reset_tx_gain(struct athn_softc *sc, struct ieee80211_channel *c)
3158 1.1 christos {
3159 1.1 christos #define X(x) ((uint32_t)(x) << 2)
3160 1.1 christos const struct athn_gain *prog = sc->sc_tx_gain;
3161 1.1 christos const uint32_t *pvals;
3162 1.1 christos int i;
3163 1.1 christos
3164 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
3165 1.1 christos pvals = prog->vals_2g;
3166 1.1 christos else
3167 1.1 christos pvals = prog->vals_5g;
3168 1.1 christos for (i = 0; i < prog->nregs; i++)
3169 1.1 christos AR_WRITE(sc, X(prog->regs[i]), pvals[i]);
3170 1.1 christos AR_WRITE_BARRIER(sc);
3171 1.1 christos #undef X
3172 1.1 christos }
3173 1.1 christos
3174 1.1 christos Static void
3175 1.1 christos ar9003_hw_init(struct athn_softc *sc, struct ieee80211_channel *c,
3176 1.1 christos struct ieee80211_channel *extc)
3177 1.1 christos {
3178 1.1 christos #define X(x) ((uint32_t)(x) << 2)
3179 1.1 christos struct athn_ops *ops = &sc->sc_ops;
3180 1.1 christos const struct athn_ini *ini = sc->sc_ini;
3181 1.1 christos const uint32_t *pvals;
3182 1.1 christos uint32_t reg;
3183 1.1 christos int i;
3184 1.1 christos
3185 1.1 christos /*
3186 1.1 christos * The common init values include the pre and core phases for the
3187 1.1 christos * SoC, MAC, BB and Radio subsystems.
3188 1.1 christos */
3189 1.1 christos DPRINTFN(DBG_INIT, sc, "writing pre and core init vals\n");
3190 1.1 christos for (i = 0; i < ini->ncmregs; i++) {
3191 1.1 christos AR_WRITE(sc, X(ini->cmregs[i]), ini->cmvals[i]);
3192 1.1 christos if (AR_IS_ANALOG_REG(X(ini->cmregs[i])))
3193 1.1 christos DELAY(100);
3194 1.1 christos if ((i & 0x1f) == 0)
3195 1.1 christos DELAY(1);
3196 1.1 christos }
3197 1.1 christos
3198 1.1 christos /*
3199 1.1 christos * The modal init values include the post phase for the SoC, MAC,
3200 1.1 christos * BB and Radio subsystems.
3201 1.1 christos */
3202 1.1 christos #ifndef IEEE80211_NO_HT
3203 1.1 christos if (extc != NULL) {
3204 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
3205 1.1 christos pvals = ini->vals_2g40;
3206 1.1 christos else
3207 1.1 christos pvals = ini->vals_5g40;
3208 1.1 christos }
3209 1.1 christos else
3210 1.1 christos #endif
3211 1.1 christos {
3212 1.1 christos if (IEEE80211_IS_CHAN_2GHZ(c))
3213 1.1 christos pvals = ini->vals_2g20;
3214 1.1 christos else
3215 1.1 christos pvals = ini->vals_5g20;
3216 1.1 christos }
3217 1.1 christos DPRINTFN(DBG_INIT, sc, "writing post init vals\n");
3218 1.1 christos for (i = 0; i < ini->nregs; i++) {
3219 1.1 christos AR_WRITE(sc, X(ini->regs[i]), pvals[i]);
3220 1.1 christos if (AR_IS_ANALOG_REG(X(ini->regs[i])))
3221 1.1 christos DELAY(100);
3222 1.1 christos if ((i & 0x1f) == 0)
3223 1.1 christos DELAY(1);
3224 1.1 christos }
3225 1.1 christos
3226 1.1 christos if (sc->sc_rx_gain != NULL)
3227 1.1 christos ar9003_reset_rx_gain(sc, c);
3228 1.1 christos if (sc->sc_tx_gain != NULL)
3229 1.1 christos ar9003_reset_tx_gain(sc, c);
3230 1.1 christos
3231 1.1 christos if (IEEE80211_IS_CHAN_5GHZ(c) &&
3232 1.1 christos (sc->sc_flags & ATHN_FLAG_FAST_PLL_CLOCK)) {
3233 1.1 christos /* Update modal values for fast PLL clock. */
3234 1.1 christos #ifndef IEEE80211_NO_HT
3235 1.1 christos if (extc != NULL)
3236 1.1 christos pvals = ini->fastvals_5g40;
3237 1.1 christos else
3238 1.1 christos #endif
3239 1.1 christos pvals = ini->fastvals_5g20;
3240 1.1 christos DPRINTFN(DBG_INIT, sc, "writing fast pll clock init vals\n");
3241 1.1 christos for (i = 0; i < ini->nfastregs; i++) {
3242 1.1 christos AR_WRITE(sc, X(ini->fastregs[i]), pvals[i]);
3243 1.1 christos if (AR_IS_ANALOG_REG(X(ini->fastregs[i])))
3244 1.1 christos DELAY(100);
3245 1.1 christos if ((i & 0x1f) == 0)
3246 1.1 christos DELAY(1);
3247 1.1 christos }
3248 1.1 christos }
3249 1.1 christos
3250 1.1 christos /*
3251 1.1 christos * Set the RX_ABORT and RX_DIS bits to prevent frames with corrupted
3252 1.1 christos * descriptor status.
3253 1.1 christos */
3254 1.1 christos AR_SETBITS(sc, AR_DIAG_SW, AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT);
3255 1.1 christos
3256 1.1 christos reg = AR_READ(sc, AR_PCU_MISC_MODE2);
3257 1.1 christos reg &= ~AR_PCU_MISC_MODE2_ADHOC_MCAST_KEYID_ENABLE;
3258 1.1 christos reg |= AR_PCU_MISC_MODE2_AGG_WEP_ENABLE_FIX;
3259 1.1 christos reg |= AR_PCU_MISC_MODE2_ENABLE_AGGWEP;
3260 1.1 christos AR_WRITE(sc, AR_PCU_MISC_MODE2, reg);
3261 1.1 christos AR_WRITE_BARRIER(sc);
3262 1.1 christos
3263 1.1 christos ar9003_set_phy(sc, c, extc);
3264 1.1 christos ar9003_init_chains(sc);
3265 1.1 christos
3266 1.1 christos ops->set_txpower(sc, c, extc);
3267 1.1 christos #undef X
3268 1.1 christos }
3269 1.1 christos
3270 1.1 christos PUBLIC void
3271 1.1 christos ar9003_get_lg_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
3272 1.1 christos uint8_t ctl, const uint8_t *fbins,
3273 1.1 christos const struct ar_cal_target_power_leg *tgt, int nchans, uint8_t tpow[4])
3274 1.1 christos {
3275 1.1 christos uint8_t fbin;
3276 1.1 christos int i, delta, lo, hi;
3277 1.1 christos
3278 1.1 christos lo = hi = -1;
3279 1.1 christos fbin = athn_chan2fbin(c);
3280 1.1 christos for (i = 0; i < nchans; i++) {
3281 1.1 christos delta = fbin - fbins[i];
3282 1.1 christos /* Find the largest sample that is <= our frequency. */
3283 1.1 christos if (delta >= 0 && (lo == -1 || delta < fbin - fbins[lo]))
3284 1.1 christos lo = i;
3285 1.1 christos /* Find the smallest sample that is >= our frequency. */
3286 1.1 christos if (delta <= 0 && (hi == -1 || delta > fbin - fbins[hi]))
3287 1.1 christos hi = i;
3288 1.1 christos }
3289 1.1 christos if (lo == -1)
3290 1.1 christos lo = hi;
3291 1.1 christos else if (hi == -1)
3292 1.1 christos hi = lo;
3293 1.1 christos /* Interpolate values. */
3294 1.1 christos for (i = 0; i < 4; i++) {
3295 1.1 christos tpow[i] = athn_interpolate(fbin,
3296 1.1 christos fbins[lo], tgt[lo].tPow2x[i],
3297 1.1 christos fbins[hi], tgt[hi].tPow2x[i]);
3298 1.1 christos }
3299 1.1 christos /* XXX Apply conformance test limit. */
3300 1.1 christos }
3301 1.1 christos
3302 1.1 christos PUBLIC void
3303 1.1 christos ar9003_get_ht_tpow(struct athn_softc *sc, struct ieee80211_channel *c,
3304 1.1 christos uint8_t ctl, const uint8_t *fbins,
3305 1.1 christos const struct ar_cal_target_power_ht *tgt, int nchans, uint8_t tpow[14])
3306 1.1 christos {
3307 1.1 christos uint8_t fbin;
3308 1.1 christos int i, delta, lo, hi;
3309 1.1 christos
3310 1.1 christos lo = hi = -1;
3311 1.1 christos fbin = athn_chan2fbin(c);
3312 1.1 christos for (i = 0; i < nchans; i++) {
3313 1.1 christos delta = fbin - fbins[i];
3314 1.1 christos /* Find the largest sample that is <= our frequency. */
3315 1.1 christos if (delta >= 0 && (lo == -1 || delta < fbin - fbins[lo]))
3316 1.1 christos lo = i;
3317 1.1 christos /* Find the smallest sample that is >= our frequency. */
3318 1.1 christos if (delta <= 0 && (hi == -1 || delta > fbin - fbins[hi]))
3319 1.1 christos hi = i;
3320 1.1 christos }
3321 1.1 christos if (lo == -1)
3322 1.1 christos lo = hi;
3323 1.1 christos else if (hi == -1)
3324 1.1 christos hi = lo;
3325 1.1 christos /* Interpolate values. */
3326 1.1 christos for (i = 0; i < 14; i++) {
3327 1.1 christos tpow[i] = athn_interpolate(fbin,
3328 1.1 christos fbins[lo], tgt[lo].tPow2x[i],
3329 1.1 christos fbins[hi], tgt[hi].tPow2x[i]);
3330 1.1 christos }
3331 1.1 christos /* XXX Apply conformance test limit. */
3332 1.1 christos }
3333 1.1 christos
3334 1.1 christos /*
3335 1.1 christos * Adaptive noise immunity.
3336 1.1 christos */
3337 1.1 christos Static void
3338 1.1 christos ar9003_set_noise_immunity_level(struct athn_softc *sc, int level)
3339 1.1 christos {
3340 1.1 christos int high = level == 4;
3341 1.1 christos uint32_t reg;
3342 1.1 christos
3343 1.1 christos reg = AR_READ(sc, AR_PHY_DESIRED_SZ);
3344 1.1 christos reg = RW(reg, AR_PHY_DESIRED_SZ_TOT_DES, high ? -62 : -55);
3345 1.1 christos AR_WRITE(sc, AR_PHY_DESIRED_SZ, reg);
3346 1.1 christos
3347 1.1 christos reg = AR_READ(sc, AR_PHY_AGC);
3348 1.1 christos reg = RW(reg, AR_PHY_AGC_COARSE_LOW, high ? -70 : -64);
3349 1.1 christos reg = RW(reg, AR_PHY_AGC_COARSE_HIGH, high ? -12 : -14);
3350 1.1 christos AR_WRITE(sc, AR_PHY_AGC, reg);
3351 1.1 christos
3352 1.1 christos reg = AR_READ(sc, AR_PHY_FIND_SIG);
3353 1.1 christos reg = RW(reg, AR_PHY_FIND_SIG_FIRPWR, high ? -80 : -78);
3354 1.1 christos AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
3355 1.1 christos AR_WRITE_BARRIER(sc);
3356 1.1 christos }
3357 1.1 christos
3358 1.1 christos Static void
3359 1.1 christos ar9003_enable_ofdm_weak_signal(struct athn_softc *sc)
3360 1.1 christos {
3361 1.1 christos uint32_t reg;
3362 1.1 christos
3363 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
3364 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 50);
3365 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 40);
3366 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 48);
3367 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
3368 1.1 christos
3369 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR);
3370 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 77);
3371 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 64);
3372 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 16);
3373 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR, reg);
3374 1.1 christos
3375 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
3376 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 50);
3377 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 40);
3378 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 77);
3379 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 64);
3380 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
3381 1.1 christos
3382 1.1 christos AR_SETBITS(sc, AR_PHY_SFCORR_LOW,
3383 1.1 christos AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
3384 1.1 christos AR_WRITE_BARRIER(sc);
3385 1.1 christos }
3386 1.1 christos
3387 1.1 christos Static void
3388 1.1 christos ar9003_disable_ofdm_weak_signal(struct athn_softc *sc)
3389 1.1 christos {
3390 1.1 christos uint32_t reg;
3391 1.1 christos
3392 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR_LOW);
3393 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M1_THRESH_LOW, 127);
3394 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M2_THRESH_LOW, 127);
3395 1.1 christos reg = RW(reg, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW, 63);
3396 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_LOW, reg);
3397 1.1 christos
3398 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR);
3399 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M1_THRESH, 127);
3400 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M2_THRESH, 127);
3401 1.1 christos reg = RW(reg, AR_PHY_SFCORR_M2COUNT_THR, 31);
3402 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR, reg);
3403 1.1 christos
3404 1.1 christos reg = AR_READ(sc, AR_PHY_SFCORR_EXT);
3405 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH_LOW, 127);
3406 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH_LOW, 127);
3407 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M1_THRESH, 127);
3408 1.1 christos reg = RW(reg, AR_PHY_SFCORR_EXT_M2_THRESH, 127);
3409 1.1 christos AR_WRITE(sc, AR_PHY_SFCORR_EXT, reg);
3410 1.1 christos
3411 1.1 christos AR_CLRBITS(sc, AR_PHY_SFCORR_LOW,
3412 1.1 christos AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
3413 1.1 christos AR_WRITE_BARRIER(sc);
3414 1.1 christos }
3415 1.1 christos
3416 1.1 christos Static void
3417 1.1 christos ar9003_set_cck_weak_signal(struct athn_softc *sc, int high)
3418 1.1 christos {
3419 1.1 christos uint32_t reg;
3420 1.1 christos
3421 1.1 christos reg = AR_READ(sc, AR_PHY_CCK_DETECT);
3422 1.1 christos reg = RW(reg, AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK, high ? 6 : 8);
3423 1.1 christos AR_WRITE(sc, AR_PHY_CCK_DETECT, reg);
3424 1.1 christos AR_WRITE_BARRIER(sc);
3425 1.1 christos }
3426 1.1 christos
3427 1.1 christos Static void
3428 1.1 christos ar9003_set_firstep_level(struct athn_softc *sc, int level)
3429 1.1 christos {
3430 1.1 christos uint32_t reg;
3431 1.1 christos
3432 1.1 christos reg = AR_READ(sc, AR_PHY_FIND_SIG);
3433 1.1 christos reg = RW(reg, AR_PHY_FIND_SIG_FIRSTEP, level * 4);
3434 1.1 christos AR_WRITE(sc, AR_PHY_FIND_SIG, reg);
3435 1.1 christos AR_WRITE_BARRIER(sc);
3436 1.1 christos }
3437 1.1 christos
3438 1.1 christos Static void
3439 1.1 christos ar9003_set_spur_immunity_level(struct athn_softc *sc, int level)
3440 1.1 christos {
3441 1.1 christos uint32_t reg;
3442 1.1 christos
3443 1.1 christos reg = AR_READ(sc, AR_PHY_TIMING5);
3444 1.1 christos reg = RW(reg, AR_PHY_TIMING5_CYCPWR_THR1, (level + 1) * 2);
3445 1.1 christos AR_WRITE(sc, AR_PHY_TIMING5, reg);
3446 1.1 christos AR_WRITE_BARRIER(sc);
3447 1.1 christos }
3448