1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Roy Luo <royluo@google.com> 5 * Ryder Lee <ryder.lee@mediatek.com> 6 * Felix Fietkau <nbd@nbd.name> 7 * Lorenzo Bianconi <lorenzo@kernel.org> 8 */ 9 10 #include <linux/etherdevice.h> 11 #include "mt7615.h" 12 #include "mac.h" 13 #include "eeprom.h" 14 15 static void mt7615_init_work(struct work_struct *work) 16 { 17 struct mt7615_dev *dev = container_of(work, struct mt7615_dev, 18 mcu_work); 19 20 if (mt7615_mcu_init(dev)) 21 return; 22 23 mt7615_mcu_set_eeprom(dev); 24 mt7615_mac_init(dev); 25 mt7615_phy_init(dev); 26 mt7615_mcu_del_wtbl_all(dev); 27 mt7615_check_offload_capability(dev); 28 29 if (dev->dbdc_support) 30 mt7615_register_ext_phy(dev); 31 } 32 33 static int mt7615_init_hardware(struct mt7615_dev *dev) 34 { 35 u32 addr = mt7615_reg_map(dev, MT_EFUSE_BASE); 36 int ret, idx; 37 38 mt76_wr(dev, MT_INT_SOURCE_CSR, ~0); 39 40 INIT_WORK(&dev->mcu_work, mt7615_init_work); 41 spin_lock_init(&dev->token_lock); 42 idr_init(&dev->token); 43 44 ret = mt7615_eeprom_init(dev, addr); 45 if (ret < 0) 46 return ret; 47 48 ret = mt7615_dma_init(dev); 49 if (ret) 50 return ret; 51 52 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 53 54 /* Beacon and mgmt frames should occupy wcid 0 */ 55 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1); 56 if (idx) 57 return -ENOSPC; 58 59 dev->mt76.global_wcid.idx = idx; 60 dev->mt76.global_wcid.hw_key_idx = -1; 61 rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid); 62 63 return 0; 64 } 65 66 static void 67 mt7615_led_set_config(struct led_classdev *led_cdev, 68 u8 delay_on, u8 delay_off) 69 { 70 struct mt7615_dev *dev; 71 struct mt76_dev *mt76; 72 u32 val, addr; 73 74 mt76 = container_of(led_cdev, struct mt76_dev, led_cdev); 75 dev = container_of(mt76, struct mt7615_dev, mt76); 76 77 if (test_bit(MT76_STATE_PM, &mt76->phy.state)) 78 return; 79 80 val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xffff) | 81 FIELD_PREP(MT_LED_STATUS_OFF, delay_off) | 82 FIELD_PREP(MT_LED_STATUS_ON, delay_on); 83 84 addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin)); 85 mt76_wr(dev, addr, val); 86 addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin)); 87 mt76_wr(dev, addr, val); 88 89 val = MT_LED_CTRL_REPLAY(mt76->led_pin) | 90 MT_LED_CTRL_KICK(mt76->led_pin); 91 if (mt76->led_al) 92 val |= MT_LED_CTRL_POLARITY(mt76->led_pin); 93 addr = mt7615_reg_map(dev, MT_LED_CTRL); 94 mt76_wr(dev, addr, val); 95 } 96 97 static int 98 mt7615_led_set_blink(struct led_classdev *led_cdev, 99 unsigned long *delay_on, 100 unsigned long *delay_off) 101 { 102 u8 delta_on, delta_off; 103 104 delta_off = max_t(u8, *delay_off / 10, 1); 105 delta_on = max_t(u8, *delay_on / 10, 1); 106 107 mt7615_led_set_config(led_cdev, delta_on, delta_off); 108 109 return 0; 110 } 111 112 static void 113 mt7615_led_set_brightness(struct led_classdev *led_cdev, 114 enum led_brightness brightness) 115 { 116 if (!brightness) 117 mt7615_led_set_config(led_cdev, 0, 0xff); 118 else 119 mt7615_led_set_config(led_cdev, 0xff, 0); 120 } 121 122 int mt7615_register_device(struct mt7615_dev *dev) 123 { 124 int ret; 125 126 mt7615_init_device(dev); 127 128 /* init led callbacks */ 129 if (IS_ENABLED(CONFIG_MT76_LEDS)) { 130 dev->mt76.led_cdev.brightness_set = mt7615_led_set_brightness; 131 dev->mt76.led_cdev.blink_set = mt7615_led_set_blink; 132 } 133 134 ret = mt7622_wmac_init(dev); 135 if (ret) 136 return ret; 137 138 ret = mt7615_init_hardware(dev); 139 if (ret) 140 return ret; 141 142 ret = mt76_register_device(&dev->mt76, true, mt7615_rates, 143 ARRAY_SIZE(mt7615_rates)); 144 if (ret) 145 return ret; 146 147 ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work); 148 mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband); 149 mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband); 150 151 return mt7615_init_debugfs(dev); 152 } 153 154 void mt7615_unregister_device(struct mt7615_dev *dev) 155 { 156 struct mt76_txwi_cache *txwi; 157 bool mcu_running; 158 int id; 159 160 mcu_running = mt7615_wait_for_mcu_init(dev); 161 162 mt7615_unregister_ext_phy(dev); 163 mt76_unregister_device(&dev->mt76); 164 if (mcu_running) 165 mt7615_mcu_exit(dev); 166 mt7615_dma_cleanup(dev); 167 168 spin_lock_bh(&dev->token_lock); 169 idr_for_each_entry(&dev->token, txwi, id) { 170 mt7615_txp_skb_unmap(&dev->mt76, txwi); 171 if (txwi->skb) 172 dev_kfree_skb_any(txwi->skb); 173 mt76_put_txwi(&dev->mt76, txwi); 174 } 175 spin_unlock_bh(&dev->token_lock); 176 idr_destroy(&dev->token); 177 178 tasklet_disable(&dev->irq_tasklet); 179 180 mt76_free_device(&dev->mt76); 181 } 182