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