2 * (c) Copyright 2002-2010, Ralink Technology, Inc.
3 * Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org>
4 * Copyright (C) 2015 Jakub Kicinski <kubakici@wp.pl>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
24 mt7601u_set_wlan_state(struct mt7601u_dev
*dev
, u32 val
, bool enable
)
28 /* Note: we don't turn off WLAN_CLK because that makes the device
29 * not respond properly on the probe path.
30 * In case anyone (PSM?) wants to use this function we can
31 * bring the clock stuff back and fixup the probe path.
35 val
|= (MT_WLAN_FUN_CTRL_WLAN_EN
|
36 MT_WLAN_FUN_CTRL_WLAN_CLK_EN
);
38 val
&= ~(MT_WLAN_FUN_CTRL_WLAN_EN
);
40 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
44 set_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
);
46 clear_bit(MT7601U_STATE_WLAN_RUNNING
, &dev
->state
);
50 for (i
= 200; i
; i
--) {
51 val
= mt7601u_rr(dev
, MT_CMB_CTRL
);
53 if (val
& MT_CMB_CTRL_XTAL_RDY
&& val
& MT_CMB_CTRL_PLL_LD
)
59 /* Note: vendor driver tries to disable/enable wlan here and retry
60 * but the code which does it is so buggy it must have never
61 * triggered, so don't bother.
64 dev_err(dev
->dev
, "Error: PLL and XTAL check failed!\n");
67 static void mt7601u_chip_onoff(struct mt7601u_dev
*dev
, bool enable
, bool reset
)
71 mutex_lock(&dev
->hw_atomic_mutex
);
73 val
= mt7601u_rr(dev
, MT_WLAN_FUN_CTRL
);
76 val
|= MT_WLAN_FUN_CTRL_GPIO_OUT_EN
;
77 val
&= ~MT_WLAN_FUN_CTRL_FRC_WL_ANT_SEL
;
79 if (val
& MT_WLAN_FUN_CTRL_WLAN_EN
) {
80 val
|= (MT_WLAN_FUN_CTRL_WLAN_RESET
|
81 MT_WLAN_FUN_CTRL_WLAN_RESET_RF
);
82 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
85 val
&= ~(MT_WLAN_FUN_CTRL_WLAN_RESET
|
86 MT_WLAN_FUN_CTRL_WLAN_RESET_RF
);
90 mt7601u_wr(dev
, MT_WLAN_FUN_CTRL
, val
);
93 mt7601u_set_wlan_state(dev
, val
, enable
);
95 mutex_unlock(&dev
->hw_atomic_mutex
);
98 static void mt7601u_reset_csr_bbp(struct mt7601u_dev
*dev
)
100 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, (MT_MAC_SYS_CTRL_RESET_CSR
|
101 MT_MAC_SYS_CTRL_RESET_BBP
));
102 mt7601u_wr(dev
, MT_USB_DMA_CFG
, 0);
104 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, 0);
107 static void mt7601u_init_usb_dma(struct mt7601u_dev
*dev
)
111 val
= MT76_SET(MT_USB_DMA_CFG_RX_BULK_AGG_TOUT
, MT_USB_AGGR_TIMEOUT
) |
112 MT76_SET(MT_USB_DMA_CFG_RX_BULK_AGG_LMT
, MT_USB_AGGR_SIZE_LIMIT
) |
113 MT_USB_DMA_CFG_RX_BULK_EN
|
114 MT_USB_DMA_CFG_TX_BULK_EN
;
115 if (dev
->in_max_packet
== 512)
116 val
|= MT_USB_DMA_CFG_RX_BULK_AGG_EN
;
117 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
119 val
|= MT_USB_DMA_CFG_UDMA_RX_WL_DROP
;
120 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
121 val
&= ~MT_USB_DMA_CFG_UDMA_RX_WL_DROP
;
122 mt7601u_wr(dev
, MT_USB_DMA_CFG
, val
);
125 static int mt7601u_init_bbp(struct mt7601u_dev
*dev
)
129 ret
= mt7601u_wait_bbp_ready(dev
);
133 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
, bbp_common_vals
,
134 ARRAY_SIZE(bbp_common_vals
));
138 return mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_BBP
, bbp_chip_vals
,
139 ARRAY_SIZE(bbp_chip_vals
));
143 mt76_init_beacon_offsets(struct mt7601u_dev
*dev
)
145 u16 base
= MT_BEACON_BASE
;
149 for (i
= 0; i
< 16; i
++) {
150 u16 addr
= dev
->beacon_offsets
[i
];
152 regs
[i
/ 4] |= ((addr
- base
) / 64) << (8 * (i
% 4));
155 for (i
= 0; i
< 4; i
++)
156 mt7601u_wr(dev
, MT_BCN_OFFSET(i
), regs
[i
]);
159 static int mt7601u_write_mac_initvals(struct mt7601u_dev
*dev
)
163 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_WLAN
, mac_common_vals
,
164 ARRAY_SIZE(mac_common_vals
));
167 ret
= mt7601u_write_reg_pairs(dev
, MT_MCU_MEMMAP_WLAN
,
168 mac_chip_vals
, ARRAY_SIZE(mac_chip_vals
));
172 mt76_init_beacon_offsets(dev
);
174 mt7601u_wr(dev
, MT_AUX_CLK_CFG
, 0);
179 static int mt7601u_init_wcid_mem(struct mt7601u_dev
*dev
)
184 vals
= kmalloc(sizeof(*vals
) * N_WCIDS
* 2, GFP_KERNEL
);
188 for (i
= 0; i
< N_WCIDS
; i
++) {
189 vals
[i
* 2] = 0xffffffff;
190 vals
[i
* 2 + 1] = 0x00ffffff;
193 ret
= mt7601u_burst_write_regs(dev
, MT_WCID_ADDR_BASE
,
200 static int mt7601u_init_key_mem(struct mt7601u_dev
*dev
)
204 return mt7601u_burst_write_regs(dev
, MT_SKEY_MODE_BASE_0
,
205 vals
, ARRAY_SIZE(vals
));
208 static int mt7601u_init_wcid_attr_mem(struct mt7601u_dev
*dev
)
213 vals
= kmalloc(sizeof(*vals
) * N_WCIDS
* 2, GFP_KERNEL
);
217 for (i
= 0; i
< N_WCIDS
* 2; i
++)
220 ret
= mt7601u_burst_write_regs(dev
, MT_WCID_ATTR_BASE
,
227 static void mt7601u_reset_counters(struct mt7601u_dev
*dev
)
229 mt7601u_rr(dev
, MT_RX_STA_CNT0
);
230 mt7601u_rr(dev
, MT_RX_STA_CNT1
);
231 mt7601u_rr(dev
, MT_RX_STA_CNT2
);
232 mt7601u_rr(dev
, MT_TX_STA_CNT0
);
233 mt7601u_rr(dev
, MT_TX_STA_CNT1
);
234 mt7601u_rr(dev
, MT_TX_STA_CNT2
);
237 int mt7601u_mac_start(struct mt7601u_dev
*dev
)
239 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_ENABLE_TX
);
241 if (!mt76_poll(dev
, MT_WPDMA_GLO_CFG
, MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
242 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 200000))
245 dev
->rxfilter
= MT_RX_FILTR_CFG_CRC_ERR
|
246 MT_RX_FILTR_CFG_PHY_ERR
| MT_RX_FILTR_CFG_PROMISC
|
247 MT_RX_FILTR_CFG_VER_ERR
| MT_RX_FILTR_CFG_DUP
|
248 MT_RX_FILTR_CFG_CFACK
| MT_RX_FILTR_CFG_CFEND
|
249 MT_RX_FILTR_CFG_ACK
| MT_RX_FILTR_CFG_CTS
|
250 MT_RX_FILTR_CFG_RTS
| MT_RX_FILTR_CFG_PSPOLL
|
251 MT_RX_FILTR_CFG_BA
| MT_RX_FILTR_CFG_CTRL_RSV
;
252 mt7601u_wr(dev
, MT_RX_FILTR_CFG
, dev
->rxfilter
);
254 mt7601u_wr(dev
, MT_MAC_SYS_CTRL
,
255 MT_MAC_SYS_CTRL_ENABLE_TX
| MT_MAC_SYS_CTRL_ENABLE_RX
);
257 if (!mt76_poll(dev
, MT_WPDMA_GLO_CFG
, MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
258 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 50))
264 static void mt7601u_mac_stop_hw(struct mt7601u_dev
*dev
)
268 if (test_bit(MT7601U_STATE_REMOVED
, &dev
->state
))
271 mt76_clear(dev
, MT_BEACON_TIME_CFG
, MT_BEACON_TIME_CFG_TIMER_EN
|
272 MT_BEACON_TIME_CFG_SYNC_MODE
| MT_BEACON_TIME_CFG_TBTT_EN
|
273 MT_BEACON_TIME_CFG_BEACON_TX
);
275 if (!mt76_poll(dev
, MT_USB_DMA_CFG
, MT_USB_DMA_CFG_TX_BUSY
, 0, 1000))
276 dev_warn(dev
->dev
, "Warning: TX DMA did not stop!\n");
278 /* Page count on TxQ */
280 while (i
-- && ((mt76_rr(dev
, 0x0438) & 0xffffffff) ||
281 (mt76_rr(dev
, 0x0a30) & 0x000000ff) ||
282 (mt76_rr(dev
, 0x0a34) & 0x00ff00ff)))
285 if (!mt76_poll(dev
, MT_MAC_STATUS
, MT_MAC_STATUS_TX
, 0, 1000))
286 dev_warn(dev
->dev
, "Warning: MAC TX did not stop!\n");
288 mt76_clear(dev
, MT_MAC_SYS_CTRL
, MT_MAC_SYS_CTRL_ENABLE_RX
|
289 MT_MAC_SYS_CTRL_ENABLE_TX
);
291 /* Page count on RxQ */
295 if ((mt76_rr(dev
, 0x0430) & 0x00ff0000) ||
296 (mt76_rr(dev
, 0x0a30) & 0xffffffff) ||
297 (mt76_rr(dev
, 0x0a34) & 0xffffffff))
305 if (!mt76_poll(dev
, MT_MAC_STATUS
, MT_MAC_STATUS_RX
, 0, 1000))
306 dev_warn(dev
->dev
, "Warning: MAC RX did not stop!\n");
308 if (!mt76_poll(dev
, MT_USB_DMA_CFG
, MT_USB_DMA_CFG_RX_BUSY
, 0, 1000))
309 dev_warn(dev
->dev
, "Warning: RX DMA did not stop!\n");
312 void mt7601u_mac_stop(struct mt7601u_dev
*dev
)
314 mt7601u_mac_stop_hw(dev
);
315 flush_delayed_work(&dev
->stat_work
);
316 cancel_delayed_work_sync(&dev
->stat_work
);
319 static void mt7601u_stop_hardware(struct mt7601u_dev
*dev
)
321 mt7601u_chip_onoff(dev
, false, false);
324 int mt7601u_init_hardware(struct mt7601u_dev
*dev
)
326 static const u16 beacon_offsets
[16] = {
327 /* 512 byte per beacon */
328 0xc000, 0xc200, 0xc400, 0xc600,
329 0xc800, 0xca00, 0xcc00, 0xce00,
330 0xd000, 0xd200, 0xd400, 0xd600,
331 0xd800, 0xda00, 0xdc00, 0xde00
335 dev
->beacon_offsets
= beacon_offsets
;
337 mt7601u_chip_onoff(dev
, true, false);
339 ret
= mt7601u_wait_asic_ready(dev
);
342 ret
= mt7601u_mcu_init(dev
);
346 if (!mt76_poll_msec(dev
, MT_WPDMA_GLO_CFG
,
347 MT_WPDMA_GLO_CFG_TX_DMA_BUSY
|
348 MT_WPDMA_GLO_CFG_RX_DMA_BUSY
, 0, 100)) {
353 /* Wait for ASIC ready after FW load. */
354 ret
= mt7601u_wait_asic_ready(dev
);
358 mt7601u_reset_csr_bbp(dev
);
359 mt7601u_init_usb_dma(dev
);
361 ret
= mt7601u_mcu_cmd_init(dev
);
364 ret
= mt7601u_dma_init(dev
);
367 ret
= mt7601u_write_mac_initvals(dev
);
371 if (!mt76_poll_msec(dev
, MT_MAC_STATUS
,
372 MT_MAC_STATUS_TX
| MT_MAC_STATUS_RX
, 0, 100)) {
377 ret
= mt7601u_init_bbp(dev
);
380 ret
= mt7601u_init_wcid_mem(dev
);
383 ret
= mt7601u_init_key_mem(dev
);
386 ret
= mt7601u_init_wcid_attr_mem(dev
);
390 mt76_clear(dev
, MT_BEACON_TIME_CFG
, (MT_BEACON_TIME_CFG_TIMER_EN
|
391 MT_BEACON_TIME_CFG_SYNC_MODE
|
392 MT_BEACON_TIME_CFG_TBTT_EN
|
393 MT_BEACON_TIME_CFG_BEACON_TX
));
395 mt7601u_reset_counters(dev
);
397 mt7601u_rmw(dev
, MT_US_CYC_CFG
, MT_US_CYC_CNT
, 0x1e);
399 mt7601u_wr(dev
, MT_TXOP_CTRL_CFG
, MT76_SET(MT_TXOP_TRUN_EN
, 0x3f) |
400 MT76_SET(MT_TXOP_EXT_CCA_DLY
, 0x58));
402 ret
= mt7601u_eeprom_init(dev
);
406 ret
= mt7601u_phy_init(dev
);
410 mt7601u_set_rx_path(dev
, 0);
411 mt7601u_set_tx_dac(dev
, 0);
413 mt7601u_mac_set_ctrlch(dev
, false);
414 mt7601u_bbp_set_ctrlch(dev
, false);
415 mt7601u_bbp_set_bw(dev
, MT_BW_20
);
420 mt7601u_dma_cleanup(dev
);
422 mt7601u_mcu_cmd_deinit(dev
);
424 mt7601u_chip_onoff(dev
, false, false);
428 void mt7601u_cleanup(struct mt7601u_dev
*dev
)
430 if (!test_and_clear_bit(MT7601U_STATE_INITIALIZED
, &dev
->state
))
433 mt7601u_stop_hardware(dev
);
434 mt7601u_dma_cleanup(dev
);
435 mt7601u_mcu_cmd_deinit(dev
);
438 struct mt7601u_dev
*mt7601u_alloc_device(struct device
*pdev
)
440 struct ieee80211_hw
*hw
;
441 struct mt7601u_dev
*dev
;
443 hw
= ieee80211_alloc_hw(sizeof(*dev
), &mt7601u_ops
);
450 mutex_init(&dev
->vendor_req_mutex
);
451 mutex_init(&dev
->reg_atomic_mutex
);
452 mutex_init(&dev
->hw_atomic_mutex
);
453 mutex_init(&dev
->mutex
);
454 spin_lock_init(&dev
->tx_lock
);
455 spin_lock_init(&dev
->rx_lock
);
456 spin_lock_init(&dev
->lock
);
457 spin_lock_init(&dev
->mac_lock
);
458 spin_lock_init(&dev
->con_mon_lock
);
459 atomic_set(&dev
->avg_ampdu_len
, 1);
460 skb_queue_head_init(&dev
->tx_skb_done
);
462 dev
->stat_wq
= alloc_workqueue("mt7601u", WQ_UNBOUND
, 0);
464 ieee80211_free_hw(hw
);
471 #define CHAN2G(_idx, _freq) { \
472 .band = NL80211_BAND_2GHZ, \
473 .center_freq = (_freq), \
474 .hw_value = (_idx), \
478 static const struct ieee80211_channel mt76_channels_2ghz
[] = {
495 #define CCK_RATE(_idx, _rate) { \
497 .flags = IEEE80211_RATE_SHORT_PREAMBLE, \
498 .hw_value = (MT_PHY_TYPE_CCK << 8) | _idx, \
499 .hw_value_short = (MT_PHY_TYPE_CCK << 8) | (8 + _idx), \
502 #define OFDM_RATE(_idx, _rate) { \
504 .hw_value = (MT_PHY_TYPE_OFDM << 8) | _idx, \
505 .hw_value_short = (MT_PHY_TYPE_OFDM << 8) | _idx, \
508 static struct ieee80211_rate mt76_rates
[] = {
524 mt76_init_sband(struct mt7601u_dev
*dev
, struct ieee80211_supported_band
*sband
,
525 const struct ieee80211_channel
*chan
, int n_chan
,
526 struct ieee80211_rate
*rates
, int n_rates
)
528 struct ieee80211_sta_ht_cap
*ht_cap
;
532 size
= n_chan
* sizeof(*chan
);
533 chanlist
= devm_kmemdup(dev
->dev
, chan
, size
, GFP_KERNEL
);
537 sband
->channels
= chanlist
;
538 sband
->n_channels
= n_chan
;
539 sband
->bitrates
= rates
;
540 sband
->n_bitrates
= n_rates
;
542 ht_cap
= &sband
->ht_cap
;
543 ht_cap
->ht_supported
= true;
544 ht_cap
->cap
= IEEE80211_HT_CAP_SUP_WIDTH_20_40
|
545 IEEE80211_HT_CAP_GRN_FLD
|
546 IEEE80211_HT_CAP_SGI_20
|
547 IEEE80211_HT_CAP_SGI_40
|
548 (1 << IEEE80211_HT_CAP_RX_STBC_SHIFT
);
550 ht_cap
->mcs
.rx_mask
[0] = 0xff;
551 ht_cap
->mcs
.rx_mask
[4] = 0x1;
552 ht_cap
->mcs
.tx_params
= IEEE80211_HT_MCS_TX_DEFINED
;
553 ht_cap
->ampdu_factor
= IEEE80211_HT_MAX_AMPDU_64K
;
554 ht_cap
->ampdu_density
= IEEE80211_HT_MPDU_DENSITY_2
;
556 dev
->chandef
.chan
= &sband
->channels
[0];
562 mt76_init_sband_2g(struct mt7601u_dev
*dev
)
564 dev
->sband_2g
= devm_kzalloc(dev
->dev
, sizeof(*dev
->sband_2g
),
566 dev
->hw
->wiphy
->bands
[NL80211_BAND_2GHZ
] = dev
->sband_2g
;
568 WARN_ON(dev
->ee
->reg
.start
- 1 + dev
->ee
->reg
.num
>
569 ARRAY_SIZE(mt76_channels_2ghz
));
571 return mt76_init_sband(dev
, dev
->sband_2g
,
572 &mt76_channels_2ghz
[dev
->ee
->reg
.start
- 1],
574 mt76_rates
, ARRAY_SIZE(mt76_rates
));
577 int mt7601u_register_device(struct mt7601u_dev
*dev
)
579 struct ieee80211_hw
*hw
= dev
->hw
;
580 struct wiphy
*wiphy
= hw
->wiphy
;
583 /* Reserve WCID 0 for mcast - thanks to this APs WCID will go to
584 * entry no. 1 like it does in the vendor driver.
586 dev
->wcid_mask
[0] |= 1;
588 /* init fake wcid for monitor interfaces */
589 dev
->mon_wcid
= devm_kmalloc(dev
->dev
, sizeof(*dev
->mon_wcid
),
593 dev
->mon_wcid
->idx
= 0xff;
594 dev
->mon_wcid
->hw_key_idx
= -1;
596 SET_IEEE80211_DEV(hw
, dev
->dev
);
599 ieee80211_hw_set(hw
, SIGNAL_DBM
);
600 ieee80211_hw_set(hw
, PS_NULLFUNC_STACK
);
601 ieee80211_hw_set(hw
, SUPPORTS_HT_CCK_RATES
);
602 ieee80211_hw_set(hw
, AMPDU_AGGREGATION
);
603 ieee80211_hw_set(hw
, SUPPORTS_RC_TABLE
);
605 hw
->max_report_rates
= 7;
606 hw
->max_rate_tries
= 1;
608 hw
->sta_data_size
= sizeof(struct mt76_sta
);
609 hw
->vif_data_size
= sizeof(struct mt76_vif
);
611 SET_IEEE80211_PERM_ADDR(hw
, dev
->macaddr
);
613 wiphy
->features
|= NL80211_FEATURE_ACTIVE_MONITOR
;
614 wiphy
->interface_modes
= BIT(NL80211_IFTYPE_STATION
);
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
);