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.
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.
17 #include <linux/delay.h>
23 mt76x2_mac_pbf_init(struct mt76x02_dev *dev)
27 val = MT_PBF_SYS_CTRL_MCU_RESET |
28 MT_PBF_SYS_CTRL_DMA_RESET |
29 MT_PBF_SYS_CTRL_MAC_RESET |
30 MT_PBF_SYS_CTRL_PBF_RESET |
31 MT_PBF_SYS_CTRL_ASY_RESET;
33 mt76_set(dev, MT_PBF_SYS_CTRL, val);
34 mt76_clear(dev, MT_PBF_SYS_CTRL, val);
36 mt76_wr(dev, MT_PBF_TX_MAX_PCNT, 0xefef3f1f);
37 mt76_wr(dev, MT_PBF_RX_MAX_PCNT, 0xfebf);
41 mt76x2_fixup_xtal(struct mt76x02_dev *dev)
46 eep_val = mt76x02_eeprom_get(dev, MT_EE_XTAL_TRIM_2);
48 offset = eep_val & 0x7f;
49 if ((eep_val & 0xff) == 0xff)
51 else if (eep_val & 0x80)
55 if (eep_val == 0x00 || eep_val == 0xff) {
56 eep_val = mt76x02_eeprom_get(dev, MT_EE_XTAL_TRIM_1);
59 if (eep_val == 0x00 || eep_val == 0xff)
64 mt76_rmw_field(dev, MT_XO_CTRL5, MT_XO_CTRL5_C2_VAL, eep_val + offset);
65 mt76_set(dev, MT_XO_CTRL6, MT_XO_CTRL6_C2_CTRL);
67 eep_val = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_2);
68 switch (FIELD_GET(MT_EE_NIC_CONF_2_XTAL_OPTION, eep_val)) {
70 mt76_wr(dev, MT_XO_CTRL7, 0x5c1fee80);
73 mt76_wr(dev, MT_XO_CTRL7, 0x5c1feed0);
80 static int mt76x2_mac_reset(struct mt76x02_dev *dev, bool hard)
82 static const u8 null_addr[ETH_ALEN] = {};
83 const u8 *macaddr = dev->mt76.macaddr;
87 if (!mt76x02_wait_for_mac(&dev->mt76))
90 val = mt76_rr(dev, MT_WPDMA_GLO_CFG);
92 val &= ~(MT_WPDMA_GLO_CFG_TX_DMA_EN |
93 MT_WPDMA_GLO_CFG_TX_DMA_BUSY |
94 MT_WPDMA_GLO_CFG_RX_DMA_EN |
95 MT_WPDMA_GLO_CFG_RX_DMA_BUSY |
96 MT_WPDMA_GLO_CFG_DMA_BURST_SIZE);
97 val |= FIELD_PREP(MT_WPDMA_GLO_CFG_DMA_BURST_SIZE, 3);
99 mt76_wr(dev, MT_WPDMA_GLO_CFG, val);
101 mt76x2_mac_pbf_init(dev);
102 mt76_write_mac_initvals(dev);
103 mt76x2_fixup_xtal(dev);
105 mt76_clear(dev, MT_MAC_SYS_CTRL,
106 MT_MAC_SYS_CTRL_RESET_CSR |
107 MT_MAC_SYS_CTRL_RESET_BBP);
110 mt76_clear(dev, MT_COEXCFG0, MT_COEXCFG0_COEX_EN);
112 mt76_set(dev, MT_EXT_CCA_CFG, 0x0000f000);
113 mt76_clear(dev, MT_TX_ALC_CFG_4, BIT(31));
115 mt76_wr(dev, MT_RF_BYPASS_0, 0x06000000);
116 mt76_wr(dev, MT_RF_SETTING_0, 0x08800000);
117 usleep_range(5000, 10000);
118 mt76_wr(dev, MT_RF_BYPASS_0, 0x00000000);
120 mt76_wr(dev, MT_MCU_CLOCK_CTL, 0x1401);
121 mt76_clear(dev, MT_FCE_L2_STUFF, MT_FCE_L2_STUFF_WR_MPDU_LEN_EN);
123 mt76_wr(dev, MT_MAC_ADDR_DW0, get_unaligned_le32(macaddr));
124 mt76_wr(dev, MT_MAC_ADDR_DW1, get_unaligned_le16(macaddr + 4));
126 mt76_wr(dev, MT_MAC_BSSID_DW0, get_unaligned_le32(macaddr));
127 mt76_wr(dev, MT_MAC_BSSID_DW1, get_unaligned_le16(macaddr + 4) |
128 FIELD_PREP(MT_MAC_BSSID_DW1_MBSS_MODE, 3) | /* 8 beacons */
129 MT_MAC_BSSID_DW1_MBSS_LOCAL_BIT);
131 /* Fire a pre-TBTT interrupt 8 ms before TBTT */
132 mt76_rmw_field(dev, MT_INT_TIMER_CFG, MT_INT_TIMER_CFG_PRE_TBTT,
134 mt76_rmw_field(dev, MT_INT_TIMER_CFG, MT_INT_TIMER_CFG_GP_TIMER,
136 mt76_wr(dev, MT_INT_TIMER_EN, 0);
138 mt76_wr(dev, MT_BCN_BYPASS_MASK, 0xffff);
142 for (i = 0; i < 256 / 32; i++)
143 mt76_wr(dev, MT_WCID_DROP_BASE + i * 4, 0);
145 for (i = 0; i < 256; i++)
146 mt76x02_mac_wcid_setup(dev, i, 0, NULL);
148 for (i = 0; i < MT_MAX_VIFS; i++)
149 mt76x02_mac_wcid_setup(dev, MT_VIF_WCID(i), i, NULL);
151 for (i = 0; i < 16; i++)
152 for (k = 0; k < 4; k++)
153 mt76x02_mac_shared_key_setup(dev, i, k, NULL);
155 for (i = 0; i < 8; i++) {
156 mt76x2_mac_set_bssid(dev, i, null_addr);
157 mt76x2_mac_set_beacon(dev, i, NULL);
160 for (i = 0; i < 16; i++)
161 mt76_rr(dev, MT_TX_STAT_FIFO);
163 mt76_wr(dev, MT_CH_TIME_CFG,
164 MT_CH_TIME_CFG_TIMER_EN |
165 MT_CH_TIME_CFG_TX_AS_BUSY |
166 MT_CH_TIME_CFG_RX_AS_BUSY |
167 MT_CH_TIME_CFG_NAV_AS_BUSY |
168 MT_CH_TIME_CFG_EIFS_AS_BUSY |
169 FIELD_PREP(MT_CH_TIME_CFG_CH_TIMER_CLR, 1));
171 mt76x02_set_beacon_offsets(dev);
173 mt76x2_set_tx_ackto(dev);
178 int mt76x2_mac_start(struct mt76x02_dev *dev)
182 for (i = 0; i < 16; i++)
183 mt76_rr(dev, MT_TX_AGG_CNT(i));
185 for (i = 0; i < 16; i++)
186 mt76_rr(dev, MT_TX_STAT_FIFO);
188 memset(dev->aggr_stats, 0, sizeof(dev->aggr_stats));
189 mt76x02_mac_start(dev);
194 void mt76x2_mac_resume(struct mt76x02_dev *dev)
196 mt76_wr(dev, MT_MAC_SYS_CTRL,
197 MT_MAC_SYS_CTRL_ENABLE_TX |
198 MT_MAC_SYS_CTRL_ENABLE_RX);
202 mt76x2_power_on_rf_patch(struct mt76x02_dev *dev)
204 mt76_set(dev, 0x10130, BIT(0) | BIT(16));
207 mt76_clear(dev, 0x1001c, 0xff);
208 mt76_set(dev, 0x1001c, 0x30);
210 mt76_wr(dev, 0x10014, 0x484f);
213 mt76_set(dev, 0x10130, BIT(17));
216 mt76_clear(dev, 0x10130, BIT(16));
219 mt76_set(dev, 0x1014c, BIT(19) | BIT(20));
223 mt76x2_power_on_rf(struct mt76x02_dev *dev, int unit)
225 int shift = unit ? 8 : 0;
228 mt76_set(dev, 0x10130, BIT(0) << shift);
231 /* Enable RFDIG LDO/AFE/ABB/ADDA */
232 mt76_set(dev, 0x10130, (BIT(1) | BIT(3) | BIT(4) | BIT(5)) << shift);
235 /* Switch RFDIG power to internal LDO */
236 mt76_clear(dev, 0x10130, BIT(2) << shift);
239 mt76x2_power_on_rf_patch(dev);
241 mt76_set(dev, 0x530, 0xf);
245 mt76x2_power_on(struct mt76x02_dev *dev)
249 /* Turn on WL MTCMOS */
250 mt76_set(dev, MT_WLAN_MTC_CTRL, MT_WLAN_MTC_CTRL_MTCMOS_PWR_UP);
252 val = MT_WLAN_MTC_CTRL_STATE_UP |
253 MT_WLAN_MTC_CTRL_PWR_ACK |
254 MT_WLAN_MTC_CTRL_PWR_ACK_S;
256 mt76_poll(dev, MT_WLAN_MTC_CTRL, val, val, 1000);
258 mt76_clear(dev, MT_WLAN_MTC_CTRL, 0x7f << 16);
261 mt76_clear(dev, MT_WLAN_MTC_CTRL, 0xf << 24);
264 mt76_set(dev, MT_WLAN_MTC_CTRL, 0xf << 24);
265 mt76_clear(dev, MT_WLAN_MTC_CTRL, 0xfff);
267 /* Turn on AD/DA power down */
268 mt76_clear(dev, 0x11204, BIT(3));
270 /* WLAN function enable */
271 mt76_set(dev, 0x10080, BIT(0));
273 /* Release BBP software reset */
274 mt76_clear(dev, 0x10064, BIT(18));
276 mt76x2_power_on_rf(dev, 0);
277 mt76x2_power_on_rf(dev, 1);
280 void mt76x2_set_tx_ackto(struct mt76x02_dev *dev)
282 u8 ackto, sifs, slottime = dev->slottime;
284 /* As defined by IEEE 802.11-2007 17.3.8.6 */
285 slottime += 3 * dev->coverage_class;
286 mt76_rmw_field(dev, MT_BKOFF_SLOT_CFG,
287 MT_BKOFF_SLOT_CFG_SLOTTIME, slottime);
289 sifs = mt76_get_field(dev, MT_XIFS_TIME_CFG,
290 MT_XIFS_TIME_CFG_OFDM_SIFS);
292 ackto = slottime + sifs;
293 mt76_rmw_field(dev, MT_TX_TIMEOUT_CFG,
294 MT_TX_TIMEOUT_CFG_ACKTO, ackto);
297 int mt76x2_init_hardware(struct mt76x02_dev *dev)
301 tasklet_init(&dev->pre_tbtt_tasklet, mt76x2_pre_tbtt_tasklet,
302 (unsigned long) dev);
304 mt76x02_dma_disable(dev);
305 mt76x2_reset_wlan(dev, true);
306 mt76x2_power_on(dev);
308 ret = mt76x2_eeprom_init(dev);
312 ret = mt76x2_mac_reset(dev, true);
316 dev->mt76.rxfilter = mt76_rr(dev, MT_RX_FILTR_CFG);
318 ret = mt76x02_dma_init(dev);
322 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
323 ret = mt76x2_mac_start(dev);
327 ret = mt76x2_mcu_init(dev);
331 mt76x2_mac_stop(dev, false);
336 void mt76x2_stop_hardware(struct mt76x02_dev *dev)
338 cancel_delayed_work_sync(&dev->cal_work);
339 cancel_delayed_work_sync(&dev->mac_work);
340 mt76x02_mcu_set_radio_state(dev, false, true);
341 mt76x2_mac_stop(dev, false);
344 void mt76x2_cleanup(struct mt76x02_dev *dev)
346 tasklet_disable(&dev->dfs_pd.dfs_tasklet);
347 tasklet_disable(&dev->pre_tbtt_tasklet);
348 mt76x2_stop_hardware(dev);
349 mt76x02_dma_cleanup(dev);
350 mt76x02_mcu_cleanup(dev);
353 struct mt76x02_dev *mt76x2_alloc_device(struct device *pdev)
355 static const struct mt76_driver_ops drv_ops = {
356 .txwi_size = sizeof(struct mt76x02_txwi),
357 .update_survey = mt76x2_update_channel,
358 .tx_prepare_skb = mt76x02_tx_prepare_skb,
359 .tx_complete_skb = mt76x02_tx_complete_skb,
360 .rx_skb = mt76x02_queue_rx_skb,
361 .rx_poll_complete = mt76x02_rx_poll_complete,
362 .sta_ps = mt76x2_sta_ps,
364 struct mt76x02_dev *dev;
365 struct mt76_dev *mdev;
367 mdev = mt76_alloc_device(sizeof(*dev), &mt76x2_ops);
371 dev = container_of(mdev, struct mt76x02_dev, mt76);
373 mdev->drv = &drv_ops;
378 static void mt76x2_regd_notifier(struct wiphy *wiphy,
379 struct regulatory_request *request)
381 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
382 struct mt76x02_dev *dev = hw->priv;
384 mt76x2_dfs_set_domain(dev, request->dfs_region);
387 static const struct ieee80211_iface_limit if_limits[] = {
390 .types = BIT(NL80211_IFTYPE_ADHOC)
393 .types = BIT(NL80211_IFTYPE_STATION) |
394 #ifdef CONFIG_MAC80211_MESH
395 BIT(NL80211_IFTYPE_MESH_POINT) |
397 BIT(NL80211_IFTYPE_AP)
401 static const struct ieee80211_iface_combination if_comb[] = {
404 .n_limits = ARRAY_SIZE(if_limits),
406 .num_different_channels = 1,
407 .beacon_int_infra_match = true,
408 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
409 BIT(NL80211_CHAN_WIDTH_20) |
410 BIT(NL80211_CHAN_WIDTH_40) |
411 BIT(NL80211_CHAN_WIDTH_80),
415 static void mt76x2_led_set_config(struct mt76_dev *mt76, u8 delay_on,
418 struct mt76x02_dev *dev = container_of(mt76, struct mt76x02_dev,
422 val = MT_LED_STATUS_DURATION(0xff) |
423 MT_LED_STATUS_OFF(delay_off) |
424 MT_LED_STATUS_ON(delay_on);
426 mt76_wr(dev, MT_LED_S0(mt76->led_pin), val);
427 mt76_wr(dev, MT_LED_S1(mt76->led_pin), val);
429 val = MT_LED_CTRL_REPLAY(mt76->led_pin) |
430 MT_LED_CTRL_KICK(mt76->led_pin);
432 val |= MT_LED_CTRL_POLARITY(mt76->led_pin);
433 mt76_wr(dev, MT_LED_CTRL, val);
436 static int mt76x2_led_set_blink(struct led_classdev *led_cdev,
437 unsigned long *delay_on,
438 unsigned long *delay_off)
440 struct mt76_dev *mt76 = container_of(led_cdev, struct mt76_dev,
442 u8 delta_on, delta_off;
444 delta_off = max_t(u8, *delay_off / 10, 1);
445 delta_on = max_t(u8, *delay_on / 10, 1);
447 mt76x2_led_set_config(mt76, delta_on, delta_off);
451 static void mt76x2_led_set_brightness(struct led_classdev *led_cdev,
452 enum led_brightness brightness)
454 struct mt76_dev *mt76 = container_of(led_cdev, struct mt76_dev,
458 mt76x2_led_set_config(mt76, 0, 0xff);
460 mt76x2_led_set_config(mt76, 0xff, 0);
463 int mt76x2_register_device(struct mt76x02_dev *dev)
465 struct ieee80211_hw *hw = mt76_hw(dev);
466 struct wiphy *wiphy = hw->wiphy;
469 INIT_DELAYED_WORK(&dev->cal_work, mt76x2_phy_calibrate);
470 INIT_DELAYED_WORK(&dev->mac_work, mt76x2_mac_work);
472 mt76x2_init_device(dev);
474 ret = mt76x2_init_hardware(dev);
478 for (i = 0; i < ARRAY_SIZE(dev->macaddr_list); i++) {
479 u8 *addr = dev->macaddr_list[i].addr;
481 memcpy(addr, dev->mt76.macaddr, ETH_ALEN);
487 addr[0] ^= ((i - 1) << 2);
489 wiphy->addresses = dev->macaddr_list;
490 wiphy->n_addresses = ARRAY_SIZE(dev->macaddr_list);
492 wiphy->iface_combinations = if_comb;
493 wiphy->n_iface_combinations = ARRAY_SIZE(if_comb);
495 wiphy->reg_notifier = mt76x2_regd_notifier;
497 wiphy->interface_modes =
498 BIT(NL80211_IFTYPE_STATION) |
499 BIT(NL80211_IFTYPE_AP) |
500 #ifdef CONFIG_MAC80211_MESH
501 BIT(NL80211_IFTYPE_MESH_POINT) |
503 BIT(NL80211_IFTYPE_ADHOC);
505 wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
507 mt76x2_dfs_init_detector(dev);
509 /* init led callbacks */
510 if (IS_ENABLED(CONFIG_MT76_LEDS)) {
511 dev->mt76.led_cdev.brightness_set = mt76x2_led_set_brightness;
512 dev->mt76.led_cdev.blink_set = mt76x2_led_set_blink;
515 ret = mt76_register_device(&dev->mt76, true, mt76x02_rates,
516 ARRAY_SIZE(mt76x02_rates));
520 mt76x2_init_debugfs(dev);
521 mt76x2_init_txpower(dev, &dev->mt76.sband_2g.sband);
522 mt76x2_init_txpower(dev, &dev->mt76.sband_5g.sband);
527 mt76x2_stop_hardware(dev);