1 // SPDX-License-Identifier: GPL-2.0-only
3 * (c) Copyright 2002-2010, Ralink Technology, Inc.
4 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
5 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
16 mt7601u_set_wlan_state(struct mt7601u_dev
*dev
, u32 val
, bool enable
)
20 /* Note: we don't turn off WLAN_CLK because that makes the device
21 * not respond properly on the probe path.
22 * In case anyone (PSM?) wants to use this function we can
23 * bring the clock stuff back and fixup the probe path.
27 val
|= (MT_WLAN_FUN_CTRL_WLAN_EN
|
28 MT_WLAN_FUN_CTRL_WLAN_CLK_EN
);
30 val
&= ~(MT_WLAN_FUN_CTRL_WLAN_EN
);
32 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
36 set_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
);
38 clear_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
);
42 for (i
= 200; i
; i
--) {
43 val
= mt7601u_rr(dev
, MT_CMB_CTRL
);
45 if (val
& MT_CMB_CTRL_XTAL_RDY
&& val
& MT_CMB_CTRL_PLL_LD
)
51 /* Note: vendor driver tries to disable/enable wlan here and retry
52 * but the code which does it is so buggy it must have never
53 * triggered, so don't bother.
56 dev_err(dev
->dev
, "Error: PLL and XTAL check failed!\n");
59 static void mt7601u_chip_onoff(struct mt7601u_dev
*dev
, bool enable
, bool reset
)
63 mutex_lock(&dev
->hw_atomic_mutex
);
65 val
= mt7601u_rr(dev
, MT_WLAN_FUN_CTRL
);
68 val
|= MT_WLAN_FUN_CTRL_GPIO_OUT_EN
;
69 val
&= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL
;
71 if (val
& MT_WLAN_FUN_CTRL_WLAN_EN
) {
72 val
|= (MT_WLAN_FUN_CTRL_WLAN_RESET
|
73 MT_WLAN_FUN_CTRL_WLAN_RESET_RF
);
74 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
77 val
&= ~(MT_WLAN_FUN_CTRL_WLAN_RESET
|
78 MT_WLAN_FUN_CTRL_WLAN_RESET_RF
);
82 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
85 mt7601u_set_wlan_state(dev
, val
, enable
);
87 mutex_unlock(&dev
->hw_atomic_mutex
);
90 static void mt7601u_reset_csr_bbp(struct mt7601u_dev
*dev
)
92 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, (MT_MAC_SYS_CTRL_RESET_CSR
|
93 MT_MAC_SYS_CTRL_RESET_BBP
));
94 mt7601u_wr(dev
, MT_USB_DMA_CFG
, 0);
96 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, 0);
99 static void mt7601u_init_usb_dma(struct mt7601u_dev
*dev
)
103 val
= FIELD_PREP(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT
, MT_USB_AGGR_TIMEOUT
) |
104 FIELD_PREP(MT_USB_DMA_CFG_RX_BULK_AGG_LMT
,
105 MT_USB_AGGR_SIZE_LIMIT
) |
106 MT_USB_DMA_CFG_RX_BULK_EN
|
107 MT_USB_DMA_CFG_TX_BULK_EN
;
108 if (dev
->in_max_packet
== 512)
109 val
|= MT_USB_DMA_CFG_RX_BULK_AGG_EN
;
110 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
112 val
|= MT_USB_DMA_CFG_UDMA_RX_WL_DROP
;
113 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
114 val
&= ~MT_USB_DMA_CFG_UDMA_RX_WL_DROP
;
115 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
118 static int mt7601u_init_bbp(struct mt7601u_dev
*dev
)
122 ret
= mt7601u_wait_bbp_ready(dev
);
126 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
, bbp_common_vals
,
127 ARRAY_SIZE(bbp_common_vals
));
131 return mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
, bbp_chip_vals
,
132 ARRAY_SIZE(bbp_chip_vals
));
136 mt76_init_beacon_offsets(struct mt7601u_dev
*dev
)
138 u16 base
= MT_BEACON_BASE
;
142 for (i
= 0; i
< 16; i
++) {
143 u16 addr
= dev
->beacon_offsets
[i
];
145 regs
[i
/ 4] |= ((addr
- base
) / 64) << (8 * (i
% 4));
148 for (i
= 0; i
< 4; i
++)
149 mt7601u_wr(dev
, MT_BCN_OFFSET(i
), regs
[i
]);
152 static int mt7601u_write_mac_initvals(struct mt7601u_dev
*dev
)
156 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_WLAN
, mac_common_vals
,
157 ARRAY_SIZE(mac_common_vals
));
160 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_WLAN
,
161 mac_chip_vals
, ARRAY_SIZE(mac_chip_vals
));
165 mt76_init_beacon_offsets(dev
);
167 mt7601u_wr(dev
, MT_AUX_CLK_CFG
, 0);
172 static int mt7601u_init_wcid_mem(struct mt7601u_dev
*dev
)
177 vals
= kmalloc(sizeof(*vals
) * N_WCIDS
* 2, GFP_KERNEL
);
181 for (i
= 0; i
< N_WCIDS
; i
++) {
182 vals
[i
* 2] = 0xffffffff;
183 vals
[i
* 2 + 1] = 0x00ffffff;
186 ret
= mt7601u_burst_write_regs(dev
, MT_WCID_ADDR_BASE
,
193 static int mt7601u_init_key_mem(struct mt7601u_dev
*dev
)
197 return mt7601u_burst_write_regs(dev
, MT_SKEY_MODE_BASE_0
,
198 vals
, ARRAY_SIZE(vals
));
201 static int mt7601u_init_wcid_attr_mem(struct mt7601u_dev
*dev
)
206 vals
= kmalloc(sizeof(*vals
) * N_WCIDS
* 2, GFP_KERNEL
);
210 for (i
= 0; i
< N_WCIDS
* 2; i
++)
213 ret
= mt7601u_burst_write_regs(dev
, MT_WCID_ATTR_BASE
,
220 static void mt7601u_reset_counters(struct mt7601u_dev
*dev
)
222 mt7601u_rr(dev
, MT_RX_STA_CNT0
);
223 mt7601u_rr(dev
, MT_RX_STA_CNT1
);
224 mt7601u_rr(dev
, MT_RX_STA_CNT2
);
225 mt7601u_rr(dev
, MT_TX_STA_CNT0
);
226 mt7601u_rr(dev
, MT_TX_STA_CNT1
);
227 mt7601u_rr(dev
, MT_TX_STA_CNT2
);
230 int mt7601u_mac_start(struct mt7601u_dev
*dev
)
232 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_ENABLE_TX
);
234 if (!mt76_poll(dev
, MT_WPDMA_GLO_CFG
, MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
235 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 200000))
238 dev
->rxfilter
= MT_RX_FILTR_CFG_CRC_ERR
|
239 MT_RX_FILTR_CFG_PHY_ERR
| MT_RX_FILTR_CFG_PROMISC
|
240 MT_RX_FILTR_CFG_VER_ERR
| MT_RX_FILTR_CFG_DUP
|
241 MT_RX_FILTR_CFG_CFACK
| MT_RX_FILTR_CFG_CFEND
|
242 MT_RX_FILTR_CFG_ACK
| MT_RX_FILTR_CFG_CTS
|
243 MT_RX_FILTR_CFG_RTS
| MT_RX_FILTR_CFG_PSPOLL
|
244 MT_RX_FILTR_CFG_BA
| MT_RX_FILTR_CFG_CTRL_RSV
;
245 mt7601u_wr(dev
, MT_RX_FILTR_CFG
, dev
->rxfilter
);
247 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
,
248 MT_MAC_SYS_CTRL_ENABLE_TX
| MT_MAC_SYS_CTRL_ENABLE_RX
);
250 if (!mt76_poll(dev
, MT_WPDMA_GLO_CFG
, MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
251 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 50))
257 static void mt7601u_mac_stop_hw(struct mt7601u_dev
*dev
)
261 if (test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
264 mt76_clear(dev
, MT_BEACON_TIME_CFG
, MT_BEACON_TIME_CFG_TIMER_EN
|
265 MT_BEACON_TIME_CFG_SYNC_MODE
| MT_BEACON_TIME_CFG_TBTT_EN
|
266 MT_BEACON_TIME_CFG_BEACON_TX
);
268 if (!mt76_poll(dev
, MT_USB_DMA_CFG
, MT_USB_DMA_CFG_TX_BUSY
, 0, 1000))
269 dev_warn(dev
->dev
, "Warning: TX DMA did not stop!\n");
271 /* Page count on TxQ */
273 while (i
-- && ((mt76_rr(dev
, 0x0438) & 0xffffffff) ||
274 (mt76_rr(dev
, 0x0a30) & 0x000000ff) ||
275 (mt76_rr(dev
, 0x0a34) & 0x00ff00ff)))
278 if (!mt76_poll(dev
, MT_MAC_STATUS
, MT_MAC_STATUS_TX
, 0, 1000))
279 dev_warn(dev
->dev
, "Warning: MAC TX did not stop!\n");
281 mt76_clear(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_ENABLE_RX
|
282 MT_MAC_SYS_CTRL_ENABLE_TX
);
284 /* Page count on RxQ */
288 if (!(mt76_rr(dev
, MT_RXQ_STA
) & 0x00ff0000) &&
289 !mt76_rr(dev
, 0x0a30) &&
290 !mt76_rr(dev
, 0x0a34)) {
298 if (!mt76_poll(dev
, MT_MAC_STATUS
, MT_MAC_STATUS_RX
, 0, 1000))
299 dev_warn(dev
->dev
, "Warning: MAC RX did not stop!\n");
301 if (!mt76_poll(dev
, MT_USB_DMA_CFG
, MT_USB_DMA_CFG_RX_BUSY
, 0, 1000))
302 dev_warn(dev
->dev
, "Warning: RX DMA did not stop!\n");
305 void mt7601u_mac_stop(struct mt7601u_dev
*dev
)
307 mt7601u_mac_stop_hw(dev
);
308 flush_delayed_work(&dev
->stat_work
);
309 cancel_delayed_work_sync(&dev
->stat_work
);
312 static void mt7601u_stop_hardware(struct mt7601u_dev
*dev
)
314 mt7601u_chip_onoff(dev
, false, false);
317 int mt7601u_init_hardware(struct mt7601u_dev
*dev
)
319 static const u16 beacon_offsets
[16] = {
320 /* 512 byte per beacon */
321 0xc000, 0xc200, 0xc400, 0xc600,
322 0xc800, 0xca00, 0xcc00, 0xce00,
323 0xd000, 0xd200, 0xd400, 0xd600,
324 0xd800, 0xda00, 0xdc00, 0xde00
328 dev
->beacon_offsets
= beacon_offsets
;
330 mt7601u_chip_onoff(dev
, true, false);
332 ret
= mt7601u_wait_asic_ready(dev
);
335 ret
= mt7601u_mcu_init(dev
);
339 if (!mt76_poll_msec(dev
, MT_WPDMA_GLO_CFG
,
340 MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
341 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 100)) {
346 /* Wait for ASIC ready after FW load. */
347 ret
= mt7601u_wait_asic_ready(dev
);
351 mt7601u_reset_csr_bbp(dev
);
352 mt7601u_init_usb_dma(dev
);
354 ret
= mt7601u_mcu_cmd_init(dev
);
357 ret
= mt7601u_dma_init(dev
);
360 ret
= mt7601u_write_mac_initvals(dev
);
364 if (!mt76_poll_msec(dev
, MT_MAC_STATUS
,
365 MT_MAC_STATUS_TX
| MT_MAC_STATUS_RX
, 0, 100)) {
370 ret
= mt7601u_init_bbp(dev
);
373 ret
= mt7601u_init_wcid_mem(dev
);
376 ret
= mt7601u_init_key_mem(dev
);
379 ret
= mt7601u_init_wcid_attr_mem(dev
);
383 mt76_clear(dev
, MT_BEACON_TIME_CFG
, (MT_BEACON_TIME_CFG_TIMER_EN
|
384 MT_BEACON_TIME_CFG_SYNC_MODE
|
385 MT_BEACON_TIME_CFG_TBTT_EN
|
386 MT_BEACON_TIME_CFG_BEACON_TX
));
388 mt7601u_reset_counters(dev
);
390 mt7601u_rmw(dev
, MT_US_CYC_CFG
, MT_US_CYC_CNT
, 0x1e);
392 mt7601u_wr(dev
, MT_TXOP_CTRL_CFG
,
393 FIELD_PREP(MT_TXOP_TRUN_EN
, 0x3f) |
394 FIELD_PREP(MT_TXOP_EXT_CCA_DLY
, 0x58));
396 ret
= mt7601u_eeprom_init(dev
);
400 ret
= mt7601u_phy_init(dev
);
404 mt7601u_set_rx_path(dev
, 0);
405 mt7601u_set_tx_dac(dev
, 0);
407 mt7601u_mac_set_ctrlch(dev
, false);
408 mt7601u_bbp_set_ctrlch(dev
, false);
409 mt7601u_bbp_set_bw(dev
, MT_BW_20
);
414 mt7601u_dma_cleanup(dev
);
416 mt7601u_mcu_cmd_deinit(dev
);
418 mt7601u_chip_onoff(dev
, false, false);
422 void mt7601u_cleanup(struct mt7601u_dev
*dev
)
424 if (!test_and_clear_bit(MT7601U_STATE_INITIALIZED
, &dev
->state
))
427 mt7601u_stop_hardware(dev
);
428 mt7601u_dma_cleanup(dev
);
429 mt7601u_mcu_cmd_deinit(dev
);
432 struct mt7601u_dev
*mt7601u_alloc_device(struct device
*pdev
)
434 struct ieee80211_hw
*hw
;
435 struct mt7601u_dev
*dev
;
437 hw
= ieee80211_alloc_hw(sizeof(*dev
), &mt7601u_ops
);
444 mutex_init(&dev
->vendor_req_mutex
);
445 mutex_init(&dev
->reg_atomic_mutex
);
446 mutex_init(&dev
->hw_atomic_mutex
);
447 mutex_init(&dev
->mutex
);
448 spin_lock_init(&dev
->tx_lock
);
449 spin_lock_init(&dev
->rx_lock
);
450 spin_lock_init(&dev
->lock
);
451 spin_lock_init(&dev
->mac_lock
);
452 spin_lock_init(&dev
->con_mon_lock
);
453 atomic_set(&dev
->avg_ampdu_len
, 1);
454 skb_queue_head_init(&dev
->tx_skb_done
);
456 dev
->stat_wq
= alloc_workqueue("mt7601u", WQ_UNBOUND
, 0);
458 ieee80211_free_hw(hw
);
465 #define CHAN2G(_idx, _freq) { \
466 .band = NL80211_BAND_2GHZ, \
467 .center_freq = (_freq), \
468 .hw_value = (_idx), \
472 static const struct ieee80211_channel mt76_channels_2ghz
[] = {
489 #define CCK_RATE(_idx, _rate) { \
491 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
492 .hw_value = (MT_PHY_TYPE_CCK << 8) | _idx, \
493 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx), \
496 #define OFDM_RATE(_idx, _rate) { \
498 .hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx, \
499 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx, \
502 static struct ieee80211_rate mt76_rates
[] = {
518 mt76_init_sband(struct mt7601u_dev
*dev
, struct ieee80211_supported_band
*sband
,
519 const struct ieee80211_channel
*chan
, int n_chan
,
520 struct ieee80211_rate
*rates
, int n_rates
)
522 struct ieee80211_sta_ht_cap
*ht_cap
;
526 size
= n_chan
* sizeof(*chan
);
527 chanlist
= devm_kmemdup(dev
->dev
, chan
, size
, GFP_KERNEL
);
531 sband
->channels
= chanlist
;
532 sband
->n_channels
= n_chan
;
533 sband
->bitrates
= rates
;
534 sband
->n_bitrates
= n_rates
;
536 ht_cap
= &sband
->ht_cap
;
537 ht_cap
->ht_supported
= true;
538 ht_cap
->cap
= IEEE80211_HT_CAP_SUP_WIDTH_20_40
|
539 IEEE80211_HT_CAP_GRN_FLD
|
540 IEEE80211_HT_CAP_SGI_20
|
541 IEEE80211_HT_CAP_SGI_40
|
542 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT
);
544 ht_cap
->mcs
.rx_mask
[0] = 0xff;
545 ht_cap
->mcs
.rx_mask
[4] = 0x1;
546 ht_cap
->mcs
.tx_params
= IEEE80211_HT_MCS_TX_DEFINED
;
547 ht_cap
->ampdu_factor
= IEEE80211_HT_MAX_AMPDU_64K
;
548 ht_cap
->ampdu_density
= IEEE80211_HT_MPDU_DENSITY_2
;
550 dev
->chandef
.chan
= &sband
->channels
[0];
556 mt76_init_sband_2g(struct mt7601u_dev
*dev
)
558 dev
->sband_2g
= devm_kzalloc(dev
->dev
, sizeof(*dev
->sband_2g
),
563 dev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] = dev
->sband_2g
;
565 WARN_ON(dev
->ee
->reg
.start
- 1 + dev
->ee
->reg
.num
>
566 ARRAY_SIZE(mt76_channels_2ghz
));
568 return mt76_init_sband(dev
, dev
->sband_2g
,
569 &mt76_channels_2ghz
[dev
->ee
->reg
.start
- 1],
571 mt76_rates
, ARRAY_SIZE(mt76_rates
));
574 int mt7601u_register_device(struct mt7601u_dev
*dev
)
576 struct ieee80211_hw
*hw
= dev
->hw
;
577 struct wiphy
*wiphy
= hw
->wiphy
;
580 /* Reserve WCID 0 for mcast - thanks to this APs WCID will go to
581 * entry no. 1 like it does in the vendor driver.
583 dev
->wcid_mask
[0] |= 1;
585 /* init fake wcid for monitor interfaces */
586 dev
->mon_wcid
= devm_kmalloc(dev
->dev
, sizeof(*dev
->mon_wcid
),
590 dev
->mon_wcid
->idx
= 0xff;
591 dev
->mon_wcid
->hw_key_idx
= -1;
593 SET_IEEE80211_DEV(hw
, dev
->dev
);
596 ieee80211_hw_set(hw
, SIGNAL_DBM
);
597 ieee80211_hw_set(hw
, PS_NULLFUNC_STACK
);
598 ieee80211_hw_set(hw
, SUPPORTS_HT_CCK_RATES
);
599 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
600 ieee80211_hw_set(hw
, SUPPORTS_RC_TABLE
);
601 ieee80211_hw_set(hw
, MFP_CAPABLE
);
603 hw
->max_report_rates
= 7;
604 hw
->max_rate_tries
= 1;
606 hw
->sta_data_size
= sizeof(struct mt76_sta
);
607 hw
->vif_data_size
= sizeof(struct mt76_vif
);
609 SET_IEEE80211_PERM_ADDR(hw
, dev
->macaddr
);
611 wiphy
->features
|= NL80211_FEATURE_ACTIVE_MONITOR
;
612 wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
614 wiphy_ext_feature_set(wiphy
, NL80211_EXT_FEATURE_CQM_RSSI_LIST
);
616 ret
= mt76_init_sband_2g(dev
);
620 INIT_DELAYED_WORK(&dev
->mac_work
, mt7601u_mac_work
);
621 INIT_DELAYED_WORK(&dev
->stat_work
, mt7601u_tx_stat
);
623 ret
= ieee80211_register_hw(hw
);
627 mt7601u_init_debugfs(dev
);