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