xref: /linux/drivers/net/wireless/mediatek/mt76/mt7615/pci_init.c (revision 7340c6df49df1b261892d287444c255d0a378063)
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 }
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_pci_init_work);
41 	ret = mt7615_eeprom_init(dev, addr);
42 	if (ret < 0)
43 		return ret;
44 
45 	if (is_mt7663(&dev->mt76)) {
46 		/* Reset RGU */
47 		mt76_clear(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1));
48 		mt76_set(dev, MT_MCU_CIRQ_IRQ_SEL(4), BIT(1));
49 	}
50 
51 	ret = mt7615_dma_init(dev);
52 	if (ret)
53 		return ret;
54 
55 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
56 
57 	/* Beacon and mgmt frames should occupy wcid 0 */
58 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
59 	if (idx)
60 		return -ENOSPC;
61 
62 	dev->mt76.global_wcid.idx = idx;
63 	dev->mt76.global_wcid.hw_key_idx = -1;
64 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
65 
66 	return 0;
67 }
68 
69 int mt7615_register_device(struct mt7615_dev *dev)
70 {
71 	int ret;
72 
73 	mt7615_init_device(dev);
74 	INIT_WORK(&dev->reset_work, mt7615_mac_reset_work);
75 
76 	/* init led callbacks */
77 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
78 		dev->mphy.leds.cdev.brightness_set = mt7615_led_set_brightness;
79 		dev->mphy.leds.cdev.blink_set = mt7615_led_set_blink;
80 	}
81 
82 	ret = mt7622_wmac_init(dev);
83 	if (ret)
84 		return ret;
85 
86 	ret = mt7615_init_hardware(dev);
87 	if (ret)
88 		return ret;
89 
90 	ret = mt76_register_device(&dev->mt76, true, mt76_rates,
91 				   ARRAY_SIZE(mt76_rates));
92 	if (ret)
93 		return ret;
94 
95 	ret = mt7615_thermal_init(dev);
96 	if (ret)
97 		return ret;
98 
99 	ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work);
100 	mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband);
101 	mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband);
102 
103 	if (dev->dbdc_support) {
104 		ret = mt7615_register_ext_phy(dev);
105 		if (ret)
106 			return ret;
107 	}
108 
109 	return mt7615_init_debugfs(dev);
110 }
111 
112 void mt7615_unregister_device(struct mt7615_dev *dev)
113 {
114 	bool mcu_running;
115 
116 	mcu_running = mt7615_wait_for_mcu_init(dev);
117 
118 	mt7615_unregister_ext_phy(dev);
119 	mt76_unregister_device(&dev->mt76);
120 	if (mcu_running)
121 		mt7615_mcu_exit(dev);
122 
123 	mt7615_tx_token_put(dev);
124 	mt7615_dma_cleanup(dev);
125 	tasklet_disable(&dev->mt76.irq_tasklet);
126 
127 	mt76_free_device(&dev->mt76);
128 }
129