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 	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_mcu_set_eeprom(dev);
31 	mt7615_mac_init(dev);
32 	mt7615_phy_init(dev);
33 	mt7615_mcu_del_wtbl_all(dev);
34 	mt7615_check_offload_capability(dev);
35 
36 	if (dev->dbdc_support)
37 		mt7615_register_ext_phy(dev);
38 }
39 
40 static int mt7615_init_hardware(struct mt7615_dev *dev)
41 {
42 	u32 addr = mt7615_reg_map(dev, MT_EFUSE_BASE);
43 	int ret, idx;
44 
45 	mt76_wr(dev, MT_INT_SOURCE_CSR, ~0);
46 
47 	INIT_WORK(&dev->mcu_work, mt7615_init_work);
48 	spin_lock_init(&dev->token_lock);
49 	idr_init(&dev->token);
50 
51 	ret = mt7615_eeprom_init(dev, addr);
52 	if (ret < 0)
53 		return ret;
54 
55 	ret = mt7615_dma_init(dev);
56 	if (ret)
57 		return ret;
58 
59 	set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
60 
61 	/* Beacon and mgmt frames should occupy wcid 0 */
62 	idx = mt76_wcid_alloc(dev->mt76.wcid_mask, MT7615_WTBL_STA - 1);
63 	if (idx)
64 		return -ENOSPC;
65 
66 	dev->mt76.global_wcid.idx = idx;
67 	dev->mt76.global_wcid.hw_key_idx = -1;
68 	rcu_assign_pointer(dev->mt76.wcid[idx], &dev->mt76.global_wcid);
69 
70 	return 0;
71 }
72 
73 static void
74 mt7615_led_set_config(struct led_classdev *led_cdev,
75 		      u8 delay_on, u8 delay_off)
76 {
77 	struct mt7615_dev *dev;
78 	struct mt76_dev *mt76;
79 	u32 val, addr;
80 
81 	mt76 = container_of(led_cdev, struct mt76_dev, led_cdev);
82 	dev = container_of(mt76, struct mt7615_dev, mt76);
83 
84 	if (test_bit(MT76_STATE_PM, &mt76->phy.state))
85 		return;
86 
87 	val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xffff) |
88 	      FIELD_PREP(MT_LED_STATUS_OFF, delay_off) |
89 	      FIELD_PREP(MT_LED_STATUS_ON, delay_on);
90 
91 	addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin));
92 	mt76_wr(dev, addr, val);
93 	addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin));
94 	mt76_wr(dev, addr, val);
95 
96 	val = MT_LED_CTRL_REPLAY(mt76->led_pin) |
97 	      MT_LED_CTRL_KICK(mt76->led_pin);
98 	if (mt76->led_al)
99 		val |= MT_LED_CTRL_POLARITY(mt76->led_pin);
100 	addr = mt7615_reg_map(dev, MT_LED_CTRL);
101 	mt76_wr(dev, addr, val);
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 
135 	/* init led callbacks */
136 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
137 		dev->mt76.led_cdev.brightness_set = mt7615_led_set_brightness;
138 		dev->mt76.led_cdev.blink_set = mt7615_led_set_blink;
139 	}
140 
141 	ret = mt7622_wmac_init(dev);
142 	if (ret)
143 		return ret;
144 
145 	ret = mt7615_init_hardware(dev);
146 	if (ret)
147 		return ret;
148 
149 	ret = mt76_register_device(&dev->mt76, true, mt7615_rates,
150 				   ARRAY_SIZE(mt7615_rates));
151 	if (ret)
152 		return ret;
153 
154 	ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work);
155 	mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband);
156 	mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband);
157 
158 	return mt7615_init_debugfs(dev);
159 }
160 
161 void mt7615_unregister_device(struct mt7615_dev *dev)
162 {
163 	struct mt76_txwi_cache *txwi;
164 	bool mcu_running;
165 	int id;
166 
167 	mcu_running = mt7615_wait_for_mcu_init(dev);
168 
169 	mt7615_unregister_ext_phy(dev);
170 	mt76_unregister_device(&dev->mt76);
171 	if (mcu_running)
172 		mt7615_mcu_exit(dev);
173 	mt7615_dma_cleanup(dev);
174 
175 	spin_lock_bh(&dev->token_lock);
176 	idr_for_each_entry(&dev->token, txwi, id) {
177 		mt7615_txp_skb_unmap(&dev->mt76, txwi);
178 		if (txwi->skb)
179 			dev_kfree_skb_any(txwi->skb);
180 		mt76_put_txwi(&dev->mt76, txwi);
181 	}
182 	spin_unlock_bh(&dev->token_lock);
183 	idr_destroy(&dev->token);
184 
185 	tasklet_disable(&dev->irq_tasklet);
186 
187 	mt76_free_device(&dev->mt76);
188 }
189