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