btcoex.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * Copyright (c) 2009 Atheros Communications Inc.
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include "ath9k.h"

19 20 21
static const struct ath_btcoex_config ath_bt_config = { 0, true, true,
			ATH_BT_COEX_MODE_SLOTTED, true, true, 2, 5, true };

22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static const u16 ath_subsysid_tbl[] = {
	AR9280_COEX2WIRE_SUBSYSID,
	AT9285_COEX3WIRE_SA_SUBSYSID,
	AT9285_COEX3WIRE_DA_SUBSYSID
};

/*
 * Checks the subsystem id of the device to see if it
 * supports btcoex
 */
bool ath_btcoex_supported(u16 subsysid)
{
	int i;

	if (!subsysid)
		return false;

	for (i = 0; i < ARRAY_SIZE(ath_subsysid_tbl); i++)
		if (subsysid == ath_subsysid_tbl[i])
			return true;

	return false;
}
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59
static void ath_btcoex_set_weight(struct ath_btcoex_info *btcoex_info,
				  u32 bt_weight,
				  u32 wlan_weight)
{
	btcoex_info->bt_coex_weights = SM(bt_weight, AR_BTCOEX_BT_WGHT) |
				       SM(wlan_weight, AR_BTCOEX_WL_WGHT);
}

void ath9k_hw_btcoex_init_weight(struct ath_hw *ah)
{
	ath_btcoex_set_weight(&ah->btcoex_info, AR_BT_COEX_WGHT,
			      AR_STOMP_LOW_WLAN_WGHT);
}

60 61 62 63 64
/*
 * Detects if there is any priority bt traffic
 */
static void ath_detect_bt_priority(struct ath_softc *sc)
{
65
	struct ath_btcoex *btcoex = &sc->btcoex;
66
	struct ath_hw *ah = sc->sc_ah;
67

68
	if (ath9k_hw_gpio_get(sc->sc_ah, ah->btcoex_info.btpriority_gpio))
69
		btcoex->bt_priority_cnt++;
70

71
	if (time_after(jiffies, btcoex->bt_priority_time +
72
			msecs_to_jiffies(ATH_BT_PRIORITY_TIME_THRESHOLD))) {
73
		if (btcoex->bt_priority_cnt >= ATH_BT_CNT_THRESHOLD) {
74
			DPRINTF(sc->sc_ah, ATH_DBG_BTCOEX,
75 76 77 78 79 80
				"BT priority traffic detected");
			sc->sc_flags |= SC_OP_BT_PRIORITY_DETECTED;
		} else {
			sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;
		}

81 82
		btcoex->bt_priority_cnt = 0;
		btcoex->bt_priority_time = jiffies;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
	}
}

/*
 * Configures appropriate weight based on stomp type.
 */
static void ath_btcoex_bt_stomp(struct ath_softc *sc,
				struct ath_btcoex_info *btinfo,
				int stomp_type)
{

	switch (stomp_type) {
	case ATH_BTCOEX_STOMP_ALL:
		ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
				      AR_STOMP_ALL_WLAN_WGHT);
		break;
	case ATH_BTCOEX_STOMP_LOW:
		ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
				      AR_STOMP_LOW_WLAN_WGHT);
		break;
	case ATH_BTCOEX_STOMP_NONE:
		ath_btcoex_set_weight(btinfo, AR_BT_COEX_WGHT,
				      AR_STOMP_NONE_WLAN_WGHT);
		break;
	default:
108
		DPRINTF(sc->sc_ah, ATH_DBG_BTCOEX, "Invalid Stomptype\n");
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		break;
	}

	ath9k_hw_btcoex_enable(sc->sc_ah);
}

/*
 * This is the master bt coex timer which runs for every
 * 45ms, bt traffic will be given priority during 55% of this
 * period while wlan gets remaining 45%
 */

static void ath_btcoex_period_timer(unsigned long data)
{
	struct ath_softc *sc = (struct ath_softc *) data;
124
	struct ath_hw *ah = sc->sc_ah;
125
	struct ath_btcoex *btcoex = &sc->btcoex;
126
	struct ath_btcoex_info *btinfo = &ah->btcoex_info;
127 128 129

	ath_detect_bt_priority(sc);

130
	spin_lock_bh(&btcoex->btcoex_lock);
131 132 133

	ath_btcoex_bt_stomp(sc, btinfo, btinfo->bt_stomp_type);

134
	spin_unlock_bh(&btcoex->btcoex_lock);
135

136 137
	if (btcoex->btcoex_period != btcoex->btcoex_no_stomp) {
		if (btcoex->hw_timer_enabled)
138
			ath_gen_timer_stop(ah, btinfo->no_stomp_timer);
139

140
		ath_gen_timer_start(ah,
141 142
			btinfo->no_stomp_timer,
			(ath9k_hw_gettsf32(sc->sc_ah) +
143 144 145
				btcoex->btcoex_no_stomp),
				btcoex->btcoex_no_stomp * 10);
		btcoex->hw_timer_enabled = true;
146 147
	}

148
	mod_timer(&btcoex->period_timer, jiffies +
149 150 151 152 153 154 155 156 157 158 159
				  msecs_to_jiffies(ATH_BTCOEX_DEF_BT_PERIOD));
}

/*
 * Generic tsf based hw timer which configures weight
 * registers to time slice between wlan and bt traffic
 */

static void ath_btcoex_no_stomp_timer(void *arg)
{
	struct ath_softc *sc = (struct ath_softc *)arg;
160
	struct ath_hw *ah = sc->sc_ah;
161
	struct ath_btcoex *btcoex = &sc->btcoex;
162
	struct ath_btcoex_info *btinfo = &ah->btcoex_info;
163

164
	DPRINTF(ah, ATH_DBG_BTCOEX, "no stomp timer running \n");
165

166
	spin_lock_bh(&btcoex->btcoex_lock);
167 168 169 170 171 172

	if (btinfo->bt_stomp_type == ATH_BTCOEX_STOMP_LOW)
		ath_btcoex_bt_stomp(sc, btinfo, ATH_BTCOEX_STOMP_NONE);
	 else if (btinfo->bt_stomp_type == ATH_BTCOEX_STOMP_ALL)
		ath_btcoex_bt_stomp(sc, btinfo, ATH_BTCOEX_STOMP_LOW);

173
	spin_unlock_bh(&btcoex->btcoex_lock);
174 175
}

176
static int ath_init_btcoex_info(struct ath_hw *ah,
177 178
				struct ath_btcoex_info *btcoex_info)
{
179
	struct ath_btcoex *btcoex = &ah->ah_sc->btcoex;
180 181 182
	u32 i;
	int qnum;

183
	qnum = ath_tx_get_qnum(ah->ah_sc, ATH9K_TX_QUEUE_DATA, ATH9K_WME_AC_BE);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203

	btcoex_info->bt_coex_mode =
		(btcoex_info->bt_coex_mode & AR_BT_QCU_THRESH) |
		SM(ath_bt_config.bt_time_extend, AR_BT_TIME_EXTEND) |
		SM(ath_bt_config.bt_txstate_extend, AR_BT_TXSTATE_EXTEND) |
		SM(ath_bt_config.bt_txframe_extend, AR_BT_TX_FRAME_EXTEND) |
		SM(ath_bt_config.bt_mode, AR_BT_MODE) |
		SM(ath_bt_config.bt_quiet_collision, AR_BT_QUIET) |
		SM(ath_bt_config.bt_rxclear_polarity, AR_BT_RX_CLEAR_POLARITY) |
		SM(ath_bt_config.bt_priority_time, AR_BT_PRIORITY_TIME) |
		SM(ath_bt_config.bt_first_slot_time, AR_BT_FIRST_SLOT_TIME) |
		SM(qnum, AR_BT_QCU_THRESH);

	btcoex_info->bt_coex_mode2 =
		SM(ath_bt_config.bt_hold_rx_clear, AR_BT_HOLD_RX_CLEAR) |
		SM(ATH_BTCOEX_BMISS_THRESH, AR_BT_BCN_MISS_THRESH) |
		AR_BT_DISABLE_BT_ANT;

	btcoex_info->bt_stomp_type = ATH_BTCOEX_STOMP_LOW;

204
	btcoex->btcoex_period = ATH_BTCOEX_DEF_BT_PERIOD * 1000;
205

206 207
	btcoex->btcoex_no_stomp = (100 - ATH_BTCOEX_DEF_DUTY_CYCLE) *
		btcoex->btcoex_period / 100;
208 209

	for (i = 0; i < 32; i++)
210
		ah->hw_gen_timers.gen_timer_index[(debruijn32 << i) >> 27] = i;
211

212
	setup_timer(&btcoex->period_timer, ath_btcoex_period_timer,
213
			(unsigned long) ah->ah_sc);
214

215
	btcoex_info->no_stomp_timer = ath_gen_timer_alloc(ah,
216 217
			ath_btcoex_no_stomp_timer,
			ath_btcoex_no_stomp_timer,
218
			(void *)ah->ah_sc, AR_FIRST_NDP_TIMER);
219 220 221 222

	if (btcoex_info->no_stomp_timer == NULL)
		return -ENOMEM;

223
	spin_lock_init(&btcoex->btcoex_lock);
224 225 226 227

	return 0;
}

228
static void ath9k_hw_btcoex_init_2wire(struct ath_hw *ah)
229
{
230
	struct ath_btcoex_info *btcoex_info = &ah->btcoex_info;
231

232 233 234 235
	/* connect bt_active to baseband */
	REG_CLR_BIT(ah, AR_GPIO_INPUT_EN_VAL,
		    (AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_DEF |
		     AR_GPIO_INPUT_EN_VAL_BT_FREQUENCY_DEF));
236

237 238
	REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
		    AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB);
239

240 241 242 243
	/* Set input mux for bt_active to gpio pin */
	REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
		      AR_GPIO_INPUT_MUX1_BT_ACTIVE,
		      btcoex_info->btactive_gpio);
244

245 246 247 248 249 250 251
	/* Configure the desired gpio port for input */
	ath9k_hw_cfg_gpio_input(ah, btcoex_info->btactive_gpio);
}

static void ath9k_hw_btcoex_init_3wire(struct ath_hw *ah)
{
	struct ath_btcoex_info *btcoex_info = &ah->btcoex_info;
252

253 254 255 256
	/* btcoex 3-wire */
	REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
			(AR_GPIO_INPUT_EN_VAL_BT_PRIORITY_BB |
			 AR_GPIO_INPUT_EN_VAL_BT_ACTIVE_BB));
257

258 259 260 261 262
	/* Set input mux for bt_prority_async and
	 *                  bt_active_async to GPIO pins */
	REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
			AR_GPIO_INPUT_MUX1_BT_ACTIVE,
			btcoex_info->btactive_gpio);
263

264 265 266
	REG_RMW_FIELD(ah, AR_GPIO_INPUT_MUX1,
			AR_GPIO_INPUT_MUX1_BT_PRIORITY,
			btcoex_info->btpriority_gpio);
267

268 269 270 271 272 273 274 275 276 277
	/* Configure the desired GPIO ports for input */

	ath9k_hw_cfg_gpio_input(ah, btcoex_info->btactive_gpio);
	ath9k_hw_cfg_gpio_input(ah, btcoex_info->btpriority_gpio);
}

int ath9k_hw_btcoex_init(struct ath_hw *ah)
{
	struct ath_btcoex_info *btcoex_info = &ah->btcoex_info;
	int ret = 0;
278

279 280 281 282
	if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_2WIRE)
		ath9k_hw_btcoex_init_2wire(ah);
	else {
		ath9k_hw_btcoex_init_3wire(ah);
283 284 285 286
		ret = ath_init_btcoex_info(ah, btcoex_info);
	}

	return ret;
287 288 289 290
}

void ath9k_hw_btcoex_enable(struct ath_hw *ah)
{
291
	struct ath_btcoex_info *btcoex_info = &ah->btcoex_info;
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
	if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_2WIRE) {
		/* Configure the desired GPIO port for TX_FRAME output */
		ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
				AR_GPIO_OUTPUT_MUX_AS_TX_FRAME);
	} else {
		/*
		 * Program coex mode and weight registers to
		 * enable coex 3-wire
		 */
		REG_WRITE(ah, AR_BT_COEX_MODE, btcoex_info->bt_coex_mode);
		REG_WRITE(ah, AR_BT_COEX_WEIGHT, btcoex_info->bt_coex_weights);
		REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex_info->bt_coex_mode2);

		REG_RMW_FIELD(ah, AR_QUIET1,
				AR_QUIET1_QUIET_ACK_CTS_ENABLE, 1);
		REG_RMW_FIELD(ah, AR_PCU_MISC,
				AR_PCU_BT_ANT_PREVENT_RX, 0);

		ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
				AR_GPIO_OUTPUT_MUX_AS_RX_CLEAR_EXTERNAL);
	}

	REG_RMW(ah, AR_GPIO_PDPU,
		(0x2 << (btcoex_info->btactive_gpio * 2)),
		(0x3 << (btcoex_info->btactive_gpio * 2)));
318 319 320 321 322 323

	ah->ah_sc->sc_flags |= SC_OP_BTCOEX_ENABLED;
}

void ath9k_hw_btcoex_disable(struct ath_hw *ah)
{
324
	struct ath_btcoex_info *btcoex_info = &ah->btcoex_info;
325 326

	ath9k_hw_set_gpio(ah, btcoex_info->wlanactive_gpio, 0);
327

328
	ath9k_hw_cfg_output(ah, btcoex_info->wlanactive_gpio,
329 330
			AR_GPIO_OUTPUT_MUX_AS_OUTPUT);

331 332 333 334 335 336
	if (btcoex_info->btcoex_scheme == ATH_BTCOEX_CFG_3WIRE) {
		REG_WRITE(ah, AR_BT_COEX_MODE, AR_BT_QUIET | AR_BT_MODE);
		REG_WRITE(ah, AR_BT_COEX_WEIGHT, 0);
		REG_WRITE(ah, AR_BT_COEX_MODE2, 0);
	}

337 338
	ah->ah_sc->sc_flags &= ~SC_OP_BTCOEX_ENABLED;
}
339 340 341 342

/*
 * Pause btcoex timer and bt duty cycle timer
 */
343
void ath_btcoex_timer_pause(struct ath_softc *sc)
344
{
345
	struct ath_btcoex *btcoex = &sc->btcoex;
346
	struct ath_hw *ah = sc->sc_ah;
347

348
	del_timer_sync(&btcoex->period_timer);
349

350
	if (btcoex->hw_timer_enabled)
351
		ath_gen_timer_stop(ah, ah->btcoex_info.no_stomp_timer);
352

353
	btcoex->hw_timer_enabled = false;
354 355 356 357 358
}

/*
 * (Re)start btcoex timers
 */
359
void ath_btcoex_timer_resume(struct ath_softc *sc)
360
{
361
	struct ath_btcoex *btcoex = &sc->btcoex;
362
	struct ath_hw *ah = sc->sc_ah;
363

364
	DPRINTF(ah, ATH_DBG_BTCOEX, "Starting btcoex timers");
365 366

	/* make sure duty cycle timer is also stopped when resuming */
367
	if (btcoex->hw_timer_enabled)
368
		ath_gen_timer_stop(sc->sc_ah, ah->btcoex_info.no_stomp_timer);
369

370 371
	btcoex->bt_priority_cnt = 0;
	btcoex->bt_priority_time = jiffies;
372 373
	sc->sc_flags &= ~SC_OP_BT_PRIORITY_DETECTED;

374
	mod_timer(&btcoex->period_timer, jiffies);
375
}