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