blob: e8bfb01ee78a4a276716cac88b4cc90dea7d7e64 [file] [log] [blame]
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +05301/*
2 * Copyright (c) 2009 Atheros Communications Inc.
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17#include "ath9k.h"
18
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +053019static const struct ath_btcoex_config ath_bt_config = { 0, true, true,
20 ATH_BT_COEX_MODE_SLOTTED, true, true, 2, 5, true };
21
22
23/*
24 * Detects if there is any priority bt traffic
25 */
26static void ath_detect_bt_priority(struct ath_softc *sc)
27{
28 struct ath_btcoex_info *btinfo = &sc->btcoex_info;
29
30 if (ath9k_hw_gpio_get(sc->sc_ah, btinfo->btpriority_gpio))
31 btinfo->bt_priority_cnt++;
32
33 if (time_after(jiffies, btinfo->bt_priority_time +
34 msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
35 if (btinfo->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
36 DPRINTF(sc, ATH_DBG_BTCOEX,
37 "BT priority traffic detected");
38 sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
39 } else {
40 sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
41 }
42
43 btinfo->bt_priority_cnt = 0;
44 btinfo->bt_priority_time = jiffies;
45 }
46}
47
48/*
49 * Configures appropriate weight based on stomp type.
50 */
51static void ath_btcoex_bt_stomp(struct ath_softc *sc,
52 struct ath_btcoex_info *btinfo,
53 int stomp_type)
54{
55
56 switch (stomp_type) {
57 case ATH_BTCOEX_STOMP_ALL:
58 ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
59 AR_STOMP_ALL_WLAN_WGHT);
60 break;
61 case ATH_BTCOEX_STOMP_LOW:
62 ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
63 AR_STOMP_LOW_WLAN_WGHT);
64 break;
65 case ATH_BTCOEX_STOMP_NONE:
66 ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
67 AR_STOMP_NONE_WLAN_WGHT);
68 break;
69 default:
70 DPRINTF(sc, ATH_DBG_BTCOEX, "Invalid Stomptype\n");
71 break;
72 }
73
74 ath9k_hw_btcoex_enable(sc->sc_ah);
75}
76
77/*
78 * This is the master bt coex timer which runs for every
79 * 45ms, bt traffic will be given priority during 55% of this
80 * period while wlan gets remaining 45%
81 */
82
83static void ath_btcoex_period_timer(unsigned long data)
84{
85 struct ath_softc *sc = (struct ath_softc *) data;
86 struct ath_btcoex_info *btinfo = &sc->btcoex_info;
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +053087
88 ath_detect_bt_priority(sc);
89
Vasanthakumar Thiagarajan8f431612009-09-01 17:46:33 +053090 spin_lock_bh(&btinfo->btcoex_lock);
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +053091
92 ath_btcoex_bt_stomp(sc, btinfo, btinfo->bt_stomp_type);
93
Vasanthakumar Thiagarajan8f431612009-09-01 17:46:33 +053094 spin_unlock_bh(&btinfo->btcoex_lock);
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +053095
96 if (btinfo->btcoex_period != btinfo->btcoex_no_stomp) {
97 if (btinfo->hw_timer_enabled)
98 ath_gen_timer_stop(sc->sc_ah, btinfo->no_stomp_timer);
99
100 ath_gen_timer_start(sc->sc_ah,
101 btinfo->no_stomp_timer,
102 (ath9k_hw_gettsf32(sc->sc_ah) +
103 btinfo->btcoex_no_stomp),
104 btinfo->btcoex_no_stomp * 10);
105 btinfo->hw_timer_enabled = true;
106 }
107
108 mod_timer(&btinfo->period_timer, jiffies +
109 msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
110}
111
112/*
113 * Generic tsf based hw timer which configures weight
114 * registers to time slice between wlan and bt traffic
115 */
116
117static void ath_btcoex_no_stomp_timer(void *arg)
118{
119 struct ath_softc *sc = (struct ath_softc *)arg;
120 struct ath_btcoex_info *btinfo = &sc->btcoex_info;
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530121
122 DPRINTF(sc, ATH_DBG_BTCOEX, "no stomp timer running \n");
123
Vasanthakumar Thiagarajan8f431612009-09-01 17:46:33 +0530124 spin_lock_bh(&btinfo->btcoex_lock);
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530125
126 if (btinfo->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
127 ath_btcoex_bt_stomp(sc, btinfo, ATH_BTCOEX_STOMP_NONE);
128 else if (btinfo->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
129 ath_btcoex_bt_stomp(sc, btinfo, ATH_BTCOEX_STOMP_LOW);
130
Vasanthakumar Thiagarajan8f431612009-09-01 17:46:33 +0530131 spin_unlock_bh(&btinfo->btcoex_lock);
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530132}
133
134static int ath_init_btcoex_info(struct ath_hw *hw,
135 struct ath_btcoex_info *btcoex_info)
136{
137 u32 i;
138 int qnum;
139
140 qnum = ath_tx_get_qnum(hw->ah_sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
141
142 btcoex_info->bt_coex_mode =
143 (btcoex_info->bt_coex_mode & AR_BT_QCU_THRESH) |
144 SM(ath_bt_config.bt_time_extend, AR_BT_TIME_EXTEND) |
145 SM(ath_bt_config.bt_txstate_extend, AR_BT_TXSTATE_EXTEND) |
146 SM(ath_bt_config.bt_txframe_extend, AR_BT_TX_FRAME_EXTEND) |
147 SM(ath_bt_config.bt_mode, AR_BT_MODE) |
148 SM(ath_bt_config.bt_quiet_collision, AR_BT_QUIET) |
149 SM(ath_bt_config.bt_rxclear_polarity, AR_BT_RX_CLEAR_POLARITY) |
150 SM(ath_bt_config.bt_priority_time, AR_BT_PRIORITY_TIME) |
151 SM(ath_bt_config.bt_first_slot_time, AR_BT_FIRST_SLOT_TIME) |
152 SM(qnum, AR_BT_QCU_THRESH);
153
154 btcoex_info->bt_coex_mode2 =
155 SM(ath_bt_config.bt_hold_rx_clear, AR_BT_HOLD_RX_CLEAR) |
156 SM(ATH_BTCOEX_BMISS_THRESH, AR_BT_BCN_MISS_THRESH) |
157 AR_BT_DISABLE_BT_ANT;
158
159 btcoex_info->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;
160
161 btcoex_info->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
162
163 btcoex_info->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
164 btcoex_info->btcoex_period / 100;
165
166 for (i = 0; i < 32; i++)
167 hw->hw_gen_timers.gen_timer_index[(debruijn32 << i) >> 27] = i;
168
169 setup_timer(&btcoex_info->period_timer, ath_btcoex_period_timer,
170 (unsigned long) hw->ah_sc);
171
172 btcoex_info->no_stomp_timer = ath_gen_timer_alloc(hw,
173 ath_btcoex_no_stomp_timer,
174 ath_btcoex_no_stomp_timer,
175 (void *)hw->ah_sc, AR_FIRST_NDP_TIMER);
176
177 if (btcoex_info->no_stomp_timer == NULL)
178 return -ENOMEM;
179
180 spin_lock_init(&btcoex_info->btcoex_lock);
181
182 return 0;
183}
184
185int ath9k_hw_btcoex_init(struct ath_hw *ah)
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530186{
Vasanthakumar Thiagarajanf14462c2009-08-26 21:08:46 +0530187 struct ath_btcoex_info *btcoex_info = &ah->ah_sc->btcoex_info;
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530188 int ret = 0;
Vasanthakumar Thiagarajanf14462c2009-08-26 21:08:46 +0530189
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530190 if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_2WIRE) {
191 /* connect bt_active to baseband */
192 REG_CLR_BIT(ah, AR_GPIO_INPUT_EN_VAL,
193 (AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_DEF |
194 AR_GPIO_INPUT_EN_VAL_BT_FREQUENCY_DEF));
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530195
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530196 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
197 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB);
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530198
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530199 /* Set input mux for bt_active to gpio pin */
200 REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
201 AR_GPIO_INPUT_MUX1_BT_ACTIVE,
202 btcoex_info->btactive_gpio);
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530203
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530204 /* Configure the desired gpio port for input */
205 ath9k_hw_cfg_gpio_input(ah, btcoex_info->btactive_gpio);
206 } else {
207 /* btcoex 3-wire */
208 REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
209 (AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_BB |
210 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB));
211
212 /* Set input mux for bt_prority_async and
213 * bt_active_async to GPIO pins */
214 REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
215 AR_GPIO_INPUT_MUX1_BT_ACTIVE,
216 btcoex_info->btactive_gpio);
217
218 REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
219 AR_GPIO_INPUT_MUX1_BT_PRIORITY,
220 btcoex_info->btpriority_gpio);
221
222 /* Configure the desired GPIO ports for input */
223
224 ath9k_hw_cfg_gpio_input(ah, btcoex_info->btactive_gpio);
225 ath9k_hw_cfg_gpio_input(ah, btcoex_info->btpriority_gpio);
226
227 ret = ath_init_btcoex_info(ah, btcoex_info);
228 }
229
230 return ret;
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530231}
232
233void ath9k_hw_btcoex_enable(struct ath_hw *ah)
234{
Vasanthakumar Thiagarajanf14462c2009-08-26 21:08:46 +0530235 struct ath_btcoex_info *btcoex_info = &ah->ah_sc->btcoex_info;
236
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530237 if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_2WIRE) {
238 /* Configure the desired GPIO port for TX_FRAME output */
239 ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
240 AR_GPIO_OUTPUT_MUX_AS_TX_FRAME);
241 } else {
242 /*
243 * Program coex mode and weight registers to
244 * enable coex 3-wire
245 */
246 REG_WRITE(ah, AR_BT_COEX_MODE, btcoex_info->bt_coex_mode);
247 REG_WRITE(ah, AR_BT_COEX_WEIGHT, btcoex_info->bt_coex_weights);
248 REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex_info->bt_coex_mode2);
249
250 REG_RMW_FIELD(ah, AR_QUIET1,
251 AR_QUIET1_QUIET_ACK_CTS_ENABLE, 1);
252 REG_RMW_FIELD(ah, AR_PCU_MISC,
253 AR_PCU_BT_ANT_PREVENT_RX, 0);
254
255 ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
256 AR_GPIO_OUTPUT_MUX_AS_RX_CLEAR_EXTERNAL);
257 }
258
259 REG_RMW(ah, AR_GPIO_PDPU,
260 (0x2 << (btcoex_info->btactive_gpio * 2)),
261 (0x3 << (btcoex_info->btactive_gpio * 2)));
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530262
263 ah->ah_sc->sc_flags |= SC_OP_BTCOEX_ENABLED;
264}
265
266void ath9k_hw_btcoex_disable(struct ath_hw *ah)
267{
Vasanthakumar Thiagarajanf14462c2009-08-26 21:08:46 +0530268 struct ath_btcoex_info *btcoex_info = &ah->ah_sc->btcoex_info;
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530269
Vasanthakumar Thiagarajanf14462c2009-08-26 21:08:46 +0530270 ath9k_hw_set_gpio(ah, btcoex_info->wlanactive_gpio, 0);
271
272 ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530273 AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
274
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530275 if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_3WIRE) {
276 REG_WRITE(ah, AR_BT_COEX_MODE, AR_BT_QUIET | AR_BT_MODE);
277 REG_WRITE(ah, AR_BT_COEX_WEIGHT, 0);
278 REG_WRITE(ah, AR_BT_COEX_MODE2, 0);
279 }
280
Vasanthakumar Thiagarajan17d50d12009-08-26 21:08:44 +0530281 ah->ah_sc->sc_flags &= ~SC_OP_BTCOEX_ENABLED;
282}
Vasanthakumar Thiagarajan17739122009-08-26 21:08:50 +0530283
284/*
285 * Pause btcoex timer and bt duty cycle timer
286 */
287void ath_btcoex_timer_pause(struct ath_softc *sc,
288 struct ath_btcoex_info *btinfo)
289{
290
291 del_timer_sync(&btinfo->period_timer);
292
293 if (btinfo->hw_timer_enabled)
294 ath_gen_timer_stop(sc->sc_ah, btinfo->no_stomp_timer);
295
296 btinfo->hw_timer_enabled = false;
297}
298
299/*
300 * (Re)start btcoex timers
301 */
302void ath_btcoex_timer_resume(struct ath_softc *sc,
303 struct ath_btcoex_info *btinfo)
304{
305
306 DPRINTF(sc, ATH_DBG_BTCOEX, "Starting btcoex timers");
307
308 /* make sure duty cycle timer is also stopped when resuming */
309 if (btinfo->hw_timer_enabled)
310 ath_gen_timer_stop(sc->sc_ah, btinfo->no_stomp_timer);
311
312 btinfo->bt_priority_cnt = 0;
313 btinfo->bt_priority_time = jiffies;
314 sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
315
316 mod_timer(&btinfo->period_timer, jiffies);
317}