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