init.c 9.4 KB
Newer Older
S
Stanislaw Gruszka 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * (c) Copyright 2002-2010, Ralink Technology, Inc.
 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
 * Copyright (C) 2018 Stanislaw Gruszka <stf_xl@wp.pl>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include "mt76x0.h"
#include "eeprom.h"
#include "trace.h"
#include "mcu.h"
#include "initvals.h"

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
static void mt76x0_vht_cap_mask(struct ieee80211_supported_band *sband)
{
	struct ieee80211_sta_vht_cap *vht_cap = &sband->vht_cap;
	u16 mcs_map = 0;
	int i;

	vht_cap->cap &= ~IEEE80211_VHT_CAP_RXLDPC;
	for (i = 0; i < 8; i++) {
		if (!i)
			mcs_map |= (IEEE80211_VHT_MCS_SUPPORT_0_7 << (i * 2));
		else
			mcs_map |=
				(IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2));
	}
	vht_cap->vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
	vht_cap->vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
}

S
Stanislaw Gruszka 已提交
41
static void
42
mt76x0_set_wlan_state(struct mt76x02_dev *dev, u32 val, bool enable)
S
Stanislaw Gruszka 已提交
43
{
44
	u32 mask = MT_CMB_CTRL_XTAL_RDY | MT_CMB_CTRL_PLL_LD;
S
Stanislaw Gruszka 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

	/* Note: we don't turn off WLAN_CLK because that makes the device
	 *	 not respond properly on the probe path.
	 *	 In case anyone (PSM?) wants to use this function we can
	 *	 bring the clock stuff back and fixup the probe path.
	 */

	if (enable)
		val |= (MT_WLAN_FUN_CTRL_WLAN_EN |
			MT_WLAN_FUN_CTRL_WLAN_CLK_EN);
	else
		val &= ~(MT_WLAN_FUN_CTRL_WLAN_EN);

	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
	udelay(20);

	/* Note: vendor driver tries to disable/enable wlan here and retry
	 *       but the code which does it is so buggy it must have never
	 *       triggered, so don't bother.
	 */
65 66
	if (enable && !mt76_poll(dev, MT_CMB_CTRL, mask, mask, 2000))
		dev_err(dev->mt76.dev, "PLL and XTAL check failed\n");
S
Stanislaw Gruszka 已提交
67 68
}

69
void mt76x0_chip_onoff(struct mt76x02_dev *dev, bool enable, bool reset)
S
Stanislaw Gruszka 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
{
	u32 val;

	val = mt76_rr(dev, MT_WLAN_FUN_CTRL);

	if (reset) {
		val |= MT_WLAN_FUN_CTRL_GPIO_OUT_EN;
		val &= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL;

		if (val & MT_WLAN_FUN_CTRL_WLAN_EN) {
			val |= (MT_WLAN_FUN_CTRL_WLAN_RESET |
				MT_WLAN_FUN_CTRL_WLAN_RESET_RF);
			mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
			udelay(20);

			val &= ~(MT_WLAN_FUN_CTRL_WLAN_RESET |
				 MT_WLAN_FUN_CTRL_WLAN_RESET_RF);
		}
	}

	mt76_wr(dev, MT_WLAN_FUN_CTRL, val);
	udelay(20);

	mt76x0_set_wlan_state(dev, val, enable);
}
95
EXPORT_SYMBOL_GPL(mt76x0_chip_onoff);
S
Stanislaw Gruszka 已提交
96

97
static void mt76x0_reset_csr_bbp(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
98
{
99 100 101
	mt76_wr(dev, MT_MAC_SYS_CTRL,
		MT_MAC_SYS_CTRL_RESET_CSR |
		MT_MAC_SYS_CTRL_RESET_BBP);
S
Stanislaw Gruszka 已提交
102
	msleep(200);
103 104 105
	mt76_clear(dev, MT_MAC_SYS_CTRL,
		   MT_MAC_SYS_CTRL_RESET_CSR |
		   MT_MAC_SYS_CTRL_RESET_BBP);
S
Stanislaw Gruszka 已提交
106 107
}

108 109 110
#define RANDOM_WRITE(dev, tab)			\
	mt76_wr_rp(dev, MT_MCU_MEMMAP_WLAN,	\
		   tab, ARRAY_SIZE(tab))
S
Stanislaw Gruszka 已提交
111

112
static int mt76x0_init_bbp(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
{
	int ret, i;

	ret = mt76x0_wait_bbp_ready(dev);
	if (ret)
		return ret;

	RANDOM_WRITE(dev, mt76x0_bbp_init_tab);

	for (i = 0; i < ARRAY_SIZE(mt76x0_bbp_switch_tab); i++) {
		const struct mt76x0_bbp_switch_item *item = &mt76x0_bbp_switch_tab[i];
		const struct mt76_reg_pair *pair = &item->reg_pair;

		if (((RF_G_BAND | RF_BW_20) & item->bw_band) == (RF_G_BAND | RF_BW_20))
			mt76_wr(dev, pair->reg, pair->value);
	}

	RANDOM_WRITE(dev, mt76x0_dcoc_tab);

	return 0;
}

135
static void mt76x0_init_mac_registers(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
136 137 138 139 140
{
	u32 reg;

	RANDOM_WRITE(dev, common_mac_reg_table);

141
	mt76x02_set_beacon_offsets(dev);
S
Stanislaw Gruszka 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

	/* Enable PBF and MAC clock SYS_CTRL[11:10] = 0x3 */
	RANDOM_WRITE(dev, mt76x0_mac_reg_table);

	/* Release BBP and MAC reset MAC_SYS_CTRL[1:0] = 0x0 */
	reg = mt76_rr(dev, MT_MAC_SYS_CTRL);
	reg &= ~0x3;
	mt76_wr(dev, MT_MAC_SYS_CTRL, reg);

	/* Set 0x141C[15:12]=0xF */
	reg = mt76_rr(dev, MT_EXT_CCA_CFG);
	reg |= 0x0000F000;
	mt76_wr(dev, MT_EXT_CCA_CFG, reg);

	mt76_clear(dev, MT_FCE_L2_STUFF, MT_FCE_L2_STUFF_WR_MPDU_LEN_EN);

	/*
		TxRing 9 is for Mgmt frame.
		TxRing 8 is for In-band command frame.
		WMM_RG0_TXQMA: This register setting is for FCE to define the rule of TxRing 9.
		WMM_RG1_TXQMA: This register setting is for FCE to define the rule of TxRing 8.
	*/
	reg = mt76_rr(dev, MT_WMM_CTRL);
	reg &= ~0x000003FF;
	reg |= 0x00000201;
	mt76_wr(dev, MT_WMM_CTRL, reg);
}

170
static int mt76x0_init_wcid_mem(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
171 172
{
	u32 *vals;
173
	int i;
S
Stanislaw Gruszka 已提交
174

175
	vals = kmalloc(sizeof(*vals) * MT76_N_WCIDS * 2, GFP_KERNEL);
S
Stanislaw Gruszka 已提交
176 177 178
	if (!vals)
		return -ENOMEM;

179
	for (i = 0; i < MT76_N_WCIDS; i++)  {
S
Stanislaw Gruszka 已提交
180 181 182 183
		vals[i * 2] = 0xffffffff;
		vals[i * 2 + 1] = 0x00ffffff;
	}

184
	mt76_wr_copy(dev, MT_WCID_ADDR_BASE, vals, MT76_N_WCIDS * 2);
S
Stanislaw Gruszka 已提交
185
	kfree(vals);
186
	return 0;
S
Stanislaw Gruszka 已提交
187 188
}

189
static void mt76x0_init_key_mem(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
190 191 192
{
	u32 vals[4] = {};

193
	mt76_wr_copy(dev, MT_SKEY_MODE_BASE_0, vals, ARRAY_SIZE(vals));
S
Stanislaw Gruszka 已提交
194 195
}

196
static int mt76x0_init_wcid_attr_mem(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
197 198
{
	u32 *vals;
199
	int i;
S
Stanislaw Gruszka 已提交
200

201
	vals = kmalloc(sizeof(*vals) * MT76_N_WCIDS * 2, GFP_KERNEL);
S
Stanislaw Gruszka 已提交
202 203 204
	if (!vals)
		return -ENOMEM;

205
	for (i = 0; i < MT76_N_WCIDS * 2; i++)
S
Stanislaw Gruszka 已提交
206 207
		vals[i] = 1;

208
	mt76_wr_copy(dev, MT_WCID_ATTR_BASE, vals, MT76_N_WCIDS * 2);
S
Stanislaw Gruszka 已提交
209
	kfree(vals);
210
	return 0;
S
Stanislaw Gruszka 已提交
211 212
}

213
static void mt76x0_reset_counters(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
214
{
215 216 217 218 219 220
	mt76_rr(dev, MT_RX_STAT_0);
	mt76_rr(dev, MT_RX_STAT_1);
	mt76_rr(dev, MT_RX_STAT_2);
	mt76_rr(dev, MT_TX_STA_0);
	mt76_rr(dev, MT_TX_STA_1);
	mt76_rr(dev, MT_TX_STA_2);
S
Stanislaw Gruszka 已提交
221 222
}

223
int mt76x0_mac_start(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
224 225 226
{
	mt76_wr(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_TX);

227
	if (!mt76x02_wait_for_wpdma(&dev->mt76, 200000))
S
Stanislaw Gruszka 已提交
228 229
		return -ETIMEDOUT;

230
	mt76_wr(dev, MT_RX_FILTR_CFG, dev->mt76.rxfilter);
S
Stanislaw Gruszka 已提交
231
	mt76_wr(dev, MT_MAC_SYS_CTRL,
232
		MT_MAC_SYS_CTRL_ENABLE_TX | MT_MAC_SYS_CTRL_ENABLE_RX);
S
Stanislaw Gruszka 已提交
233

234
	return !mt76x02_wait_for_wpdma(&dev->mt76, 50) ? -ETIMEDOUT : 0;
S
Stanislaw Gruszka 已提交
235
}
236
EXPORT_SYMBOL_GPL(mt76x0_mac_start);
S
Stanislaw Gruszka 已提交
237

238
void mt76x0_mac_stop(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
239
{
240
	int i = 200, ok = 0;
S
Stanislaw Gruszka 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254

	/* Page count on TxQ */
	while (i-- && ((mt76_rr(dev, 0x0438) & 0xffffffff) ||
		       (mt76_rr(dev, 0x0a30) & 0x000000ff) ||
		       (mt76_rr(dev, 0x0a34) & 0x00ff00ff)))
		msleep(10);

	if (!mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_TX, 0, 1000))
		dev_warn(dev->mt76.dev, "Warning: MAC TX did not stop!\n");

	mt76_clear(dev, MT_MAC_SYS_CTRL, MT_MAC_SYS_CTRL_ENABLE_RX |
					 MT_MAC_SYS_CTRL_ENABLE_TX);

	/* Page count on RxQ */
255
	for (i = 0; i < 200; i++) {
S
Stanislaw Gruszka 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268
		if (!(mt76_rr(dev, MT_RXQ_STA) & 0x00ff0000) &&
		    !mt76_rr(dev, 0x0a30) &&
		    !mt76_rr(dev, 0x0a34)) {
			if (ok++ > 5)
				break;
			continue;
		}
		msleep(1);
	}

	if (!mt76_poll(dev, MT_MAC_STATUS, MT_MAC_STATUS_RX, 0, 1000))
		dev_warn(dev->mt76.dev, "Warning: MAC RX did not stop!\n");
}
269
EXPORT_SYMBOL_GPL(mt76x0_mac_stop);
S
Stanislaw Gruszka 已提交
270

271
int mt76x0_init_hardware(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
272 273 274
{
	int ret;

275
	if (!mt76x02_wait_for_wpdma(&dev->mt76, 1000))
276
		return -EIO;
S
Stanislaw Gruszka 已提交
277 278

	/* Wait for ASIC ready after FW load. */
279 280
	if (!mt76x02_wait_for_mac(&dev->mt76))
		return -ETIMEDOUT;
S
Stanislaw Gruszka 已提交
281 282

	mt76x0_reset_csr_bbp(dev);
283
	ret = mt76x02_mcu_function_select(dev, Q_SELECT, 1, false);
S
Stanislaw Gruszka 已提交
284
	if (ret)
285
		return ret;
286

S
Stanislaw Gruszka 已提交
287 288
	mt76x0_init_mac_registers(dev);

289
	if (!mt76x02_wait_for_txrx_idle(&dev->mt76))
290
		return -EIO;
S
Stanislaw Gruszka 已提交
291 292 293

	ret = mt76x0_init_bbp(dev);
	if (ret)
294
		return ret;
S
Stanislaw Gruszka 已提交
295

296 297
	dev->mt76.rxfilter = mt76_rr(dev, MT_RX_FILTR_CFG);

S
Stanislaw Gruszka 已提交
298 299
	ret = mt76x0_init_wcid_mem(dev);
	if (ret)
300 301
		return ret;

302
	mt76x0_init_key_mem(dev);
303

S
Stanislaw Gruszka 已提交
304 305
	ret = mt76x0_init_wcid_attr_mem(dev);
	if (ret)
306
		return ret;
S
Stanislaw Gruszka 已提交
307 308 309 310 311 312 313 314 315 316

	mt76_clear(dev, MT_BEACON_TIME_CFG, (MT_BEACON_TIME_CFG_TIMER_EN |
					     MT_BEACON_TIME_CFG_SYNC_MODE |
					     MT_BEACON_TIME_CFG_TBTT_EN |
					     MT_BEACON_TIME_CFG_BEACON_TX));

	mt76x0_reset_counters(dev);

	ret = mt76x0_eeprom_init(dev);
	if (ret)
317
		return ret;
S
Stanislaw Gruszka 已提交
318 319 320

	mt76x0_phy_init(dev);

321
	return 0;
S
Stanislaw Gruszka 已提交
322
}
323
EXPORT_SYMBOL_GPL(mt76x0_init_hardware);
S
Stanislaw Gruszka 已提交
324

325
struct mt76x02_dev *
326 327 328
mt76x0_alloc_device(struct device *pdev,
		    const struct mt76_driver_ops *drv_ops,
		    const struct ieee80211_ops *ops)
S
Stanislaw Gruszka 已提交
329
{
330
	struct mt76x02_dev *dev;
331
	struct mt76_dev *mdev;
S
Stanislaw Gruszka 已提交
332

333
	mdev = mt76_alloc_device(sizeof(*dev), ops);
334
	if (!mdev)
S
Stanislaw Gruszka 已提交
335 336
		return NULL;

337
	mdev->dev = pdev;
338
	mdev->drv = drv_ops;
339

340 341
	dev = container_of(mdev, struct mt76x02_dev, mt76);
	mutex_init(&dev->phy_mutex);
S
Stanislaw Gruszka 已提交
342 343 344 345
	atomic_set(&dev->avg_ampdu_len, 1);

	return dev;
}
346
EXPORT_SYMBOL_GPL(mt76x0_alloc_device);
S
Stanislaw Gruszka 已提交
347

348
int mt76x0_register_device(struct mt76x02_dev *dev)
S
Stanislaw Gruszka 已提交
349
{
350 351
	struct mt76_dev *mdev = &dev->mt76;
	struct ieee80211_hw *hw = mdev->hw;
S
Stanislaw Gruszka 已提交
352 353 354 355 356 357
	struct wiphy *wiphy = hw->wiphy;
	int ret;

	/* Reserve WCID 0 for mcast - thanks to this APs WCID will go to
	 * entry no. 1 like it does in the vendor driver.
	 */
358
	mdev->wcid_mask[0] |= 1;
S
Stanislaw Gruszka 已提交
359 360

	/* init fake wcid for monitor interfaces */
361 362
	mdev->global_wcid.idx = 0xff;
	mdev->global_wcid.hw_key_idx = -1;
S
Stanislaw Gruszka 已提交
363

364 365
	/* init antenna configuration */
	mdev->antenna_mask = 1;
S
Stanislaw Gruszka 已提交
366 367 368 369 370

	hw->queues = 4;
	hw->max_rates = 1;
	hw->max_report_rates = 7;
	hw->max_rate_tries = 1;
371
	hw->extra_tx_headroom = sizeof(struct mt76x02_txwi) + 4 + 2;
S
Stanislaw Gruszka 已提交
372

373
	hw->sta_data_size = sizeof(struct mt76x02_sta);
374
	hw->vif_data_size = sizeof(struct mt76x02_vif);
S
Stanislaw Gruszka 已提交
375 376 377 378 379

	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);

	INIT_DELAYED_WORK(&dev->mac_work, mt76x0_mac_work);

380 381
	ret = mt76_register_device(mdev, true, mt76x02_rates,
				   ARRAY_SIZE(mt76x02_rates));
S
Stanislaw Gruszka 已提交
382 383 384
	if (ret)
		return ret;

385 386 387 388
	/* overwrite unsupported features */
	if (mdev->cap.has_5ghz)
		mt76x0_vht_cap_mask(&dev->mt76.sband_5g.sband);

S
Stanislaw Gruszka 已提交
389 390 391 392
	mt76x0_init_debugfs(dev);

	return 0;
}
393
EXPORT_SYMBOL_GPL(mt76x0_register_device);