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 	val = FIELD_PREP(MT_LED_STATUS_DURATION, 0xffff) |
74 	      FIELD_PREP(MT_LED_STATUS_OFF, delay_off) |
75 	      FIELD_PREP(MT_LED_STATUS_ON, delay_on);
76 
77 	addr = mt7615_reg_map(dev, MT_LED_STATUS_0(mt76->led_pin));
78 	mt76_wr(dev, addr, val);
79 	addr = mt7615_reg_map(dev, MT_LED_STATUS_1(mt76->led_pin));
80 	mt76_wr(dev, addr, val);
81 
82 	val = MT_LED_CTRL_REPLAY(mt76->led_pin) |
83 	      MT_LED_CTRL_KICK(mt76->led_pin);
84 	if (mt76->led_al)
85 		val |= MT_LED_CTRL_POLARITY(mt76->led_pin);
86 	addr = mt7615_reg_map(dev, MT_LED_CTRL);
87 	mt76_wr(dev, addr, val);
88 }
89 
90 static int
91 mt7615_led_set_blink(struct led_classdev *led_cdev,
92 		     unsigned long *delay_on,
93 		     unsigned long *delay_off)
94 {
95 	u8 delta_on, delta_off;
96 
97 	delta_off = max_t(u8, *delay_off / 10, 1);
98 	delta_on = max_t(u8, *delay_on / 10, 1);
99 
100 	mt7615_led_set_config(led_cdev, delta_on, delta_off);
101 
102 	return 0;
103 }
104 
105 static void
106 mt7615_led_set_brightness(struct led_classdev *led_cdev,
107 			  enum led_brightness brightness)
108 {
109 	if (!brightness)
110 		mt7615_led_set_config(led_cdev, 0, 0xff);
111 	else
112 		mt7615_led_set_config(led_cdev, 0xff, 0);
113 }
114 
115 int mt7615_register_device(struct mt7615_dev *dev)
116 {
117 	int ret;
118 
119 	mt7615_init_device(dev);
120 
121 	/* init led callbacks */
122 	if (IS_ENABLED(CONFIG_MT76_LEDS)) {
123 		dev->mt76.led_cdev.brightness_set = mt7615_led_set_brightness;
124 		dev->mt76.led_cdev.blink_set = mt7615_led_set_blink;
125 	}
126 
127 	ret = mt7622_wmac_init(dev);
128 	if (ret)
129 		return ret;
130 
131 	ret = mt7615_init_hardware(dev);
132 	if (ret)
133 		return ret;
134 
135 	ret = mt76_register_device(&dev->mt76, true, mt7615_rates,
136 				   ARRAY_SIZE(mt7615_rates));
137 	if (ret)
138 		return ret;
139 
140 	ieee80211_queue_work(mt76_hw(dev), &dev->mcu_work);
141 	mt7615_init_txpower(dev, &dev->mphy.sband_2g.sband);
142 	mt7615_init_txpower(dev, &dev->mphy.sband_5g.sband);
143 
144 	return mt7615_init_debugfs(dev);
145 }
146 
147 void mt7615_unregister_device(struct mt7615_dev *dev)
148 {
149 	struct mt76_txwi_cache *txwi;
150 	bool mcu_running;
151 	int id;
152 
153 	mcu_running = mt7615_wait_for_mcu_init(dev);
154 
155 	mt7615_unregister_ext_phy(dev);
156 	mt76_unregister_device(&dev->mt76);
157 	if (mcu_running)
158 		mt7615_mcu_exit(dev);
159 	mt7615_dma_cleanup(dev);
160 
161 	spin_lock_bh(&dev->token_lock);
162 	idr_for_each_entry(&dev->token, txwi, id) {
163 		mt7615_txp_skb_unmap(&dev->mt76, txwi);
164 		if (txwi->skb)
165 			dev_kfree_skb_any(txwi->skb);
166 		mt76_put_txwi(&dev->mt76, txwi);
167 	}
168 	spin_unlock_bh(&dev->token_lock);
169 	idr_destroy(&dev->token);
170 
171 	tasklet_disable(&dev->irq_tasklet);
172 
173 	mt76_free_device(&dev->mt76);
174 }
175