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_pci_init_work(struct work_struct *work) 16 { 17 struct mt7615_dev *dev = container_of(work, struct mt7615_dev, 18 mcu_work); 19 int i, ret; 20 21 ret = mt7615_mcu_init(dev); 22 for (i = 0; (ret == -EAGAIN) && (i < 10); i++) { 23 msleep(200); 24 ret = mt7615_mcu_init(dev); 25 } 26 27 if (ret) 28 return; 29 30 mt7615_init_work(dev); 31 if (dev->dbdc_support) 32 mt7615_register_ext_phy(dev); 33 } 34 35 static int mt7615_init_hardware(struct mt7615_dev *dev) 36 { 37 u32 addr = mt7615_reg_map(dev, MT_EFUSE_BASE); 38 int ret, idx; 39 40 mt76_wr(dev, MT_INT_SOURCE_CSR, ~0); 41 42 INIT_WORK(&dev->mcu_work, mt7615_pci_init_work); 43 ret = mt7615_eeprom_init(dev, addr); 44 if (ret < 0) 45 return ret; 46 47 if (is_mt7663(&dev->mt76)) { 48 /* Reset RGU */ 49 mt76_clear(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1)); 50 mt76_set(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1)); 51 } 52 53 ret = mt7615_dma_init(dev); 54 if (ret) 55 return ret; 56 57 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state); 58 59 /* Beacon and mgmt frames should occupy wcid 0 */ 60 idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1); 61 if (idx) 62 return -ENOSPC; 63 64 dev->mt76.global_wcid.idx = idx; 65 dev->mt76.global_wcid.hw_key_idx = -1; 66 rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid); 67 68 return 0; 69 } 70 71 static void 72 mt7615_led_set_config(struct led_classdev *led_cdev, 73 u8 delay_on, u8 delay_off) 74 { 75 struct mt7615_dev *dev; 76 struct mt76_dev *mt76; 77 u32 val, addr; 78 79 mt76 = container_of(led_cdev, struct mt76_dev, led_cdev); 80 dev = container_of(mt76, struct mt7615_dev, mt76); 81 82 if (!mt76_connac_pm_ref(&dev->mphy, &dev->pm)) 83 return; 84 85 val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xffff) | 86 FIELD_PREP(MT_LED_STATUS_OFF, delay_off) | 87 FIELD_PREP(MT_LED_STATUS_ON, delay_on); 88 89 addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin)); 90 mt76_wr(dev, addr, val); 91 addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin)); 92 mt76_wr(dev, addr, val); 93 94 val = MT_LED_CTRL_REPLAY(mt76->led_pin) | 95 MT_LED_CTRL_KICK(mt76->led_pin); 96 if (mt76->led_al) 97 val |= MT_LED_CTRL_POLARITY(mt76->led_pin); 98 addr = mt7615_reg_map(dev, MT_LED_CTRL); 99 mt76_wr(dev, addr, val); 100 101 mt76_connac_pm_unref(&dev->pm); 102 } 103 104 static int 105 mt7615_led_set_blink(struct led_classdev *led_cdev, 106 unsigned long *delay_on, 107 unsigned long *delay_off) 108 { 109 u8 delta_on, delta_off; 110 111 delta_off = max_t(u8, *delay_off / 10, 1); 112 delta_on = max_t(u8, *delay_on / 10, 1); 113 114 mt7615_led_set_config(led_cdev, delta_on, delta_off); 115 116 return 0; 117 } 118 119 static void 120 mt7615_led_set_brightness(struct led_classdev *led_cdev, 121 enum led_brightness brightness) 122 { 123 if (!brightness) 124 mt7615_led_set_config(led_cdev, 0, 0xff); 125 else 126 mt7615_led_set_config(led_cdev, 0xff, 0); 127 } 128 129 int mt7615_register_device(struct mt7615_dev *dev) 130 { 131 int ret; 132 133 mt7615_init_device(dev); 134 INIT_WORK(&dev->reset_work, mt7615_mac_reset_work); 135 136 /* init led callbacks */ 137 if (IS_ENABLED(CONFIG_MT76_LEDS)) { 138 dev->mt76.led_cdev.brightness_set = mt7615_led_set_brightness; 139 dev->mt76.led_cdev.blink_set = mt7615_led_set_blink; 140 } 141 142 ret = mt7622_wmac_init(dev); 143 if (ret) 144 return ret; 145 146 ret = mt7615_init_hardware(dev); 147 if (ret) 148 return ret; 149 150 ret = mt76_register_device(&dev->mt76, true, mt7615_rates, 151 ARRAY_SIZE(mt7615_rates)); 152 if (ret) 153 return ret; 154 155 ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work); 156 mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband); 157 mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband); 158 159 return mt7615_init_debugfs(dev); 160 } 161 162 void mt7615_unregister_device(struct mt7615_dev *dev) 163 { 164 bool mcu_running; 165 166 mcu_running = mt7615_wait_for_mcu_init(dev); 167 168 mt7615_unregister_ext_phy(dev); 169 mt76_unregister_device(&dev->mt76); 170 if (mcu_running) 171 mt7615_mcu_exit(dev); 172 173 mt7615_tx_token_put(dev); 174 mt7615_dma_cleanup(dev); 175 tasklet_disable(&dev->irq_tasklet); 176 177 mt76_free_device(&dev->mt76); 178 } 179