1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 
10 #include "mt76x0.h"
11 #include "mcu.h"
12 
13 static int mt76x0e_start(struct ieee80211_hw *hw)
14 {
15 	struct mt76x02_dev *dev = hw->priv;
16 
17 	mt76x02_mac_start(dev);
18 	mt76x0_phy_calibrate(dev, true);
19 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mt76.mac_work,
20 				     MT_MAC_WORK_INTERVAL);
21 	ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work,
22 				     MT_CALIBRATE_INTERVAL);
23 	set_bit(MT76_STATE_RUNNING, &dev->mt76.state);
24 
25 	return 0;
26 }
27 
28 static void mt76x0e_stop_hw(struct mt76x02_dev *dev)
29 {
30 	cancel_delayed_work_sync(&dev->cal_work);
31 	cancel_delayed_work_sync(&dev->mt76.mac_work);
32 
33 	if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_BUSY,
34 		       0, 1000))
35 		dev_warn(dev->mt76.dev, "TX DMA did not stop\n");
36 	mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_EN);
37 
38 	mt76x0_mac_stop(dev);
39 
40 	if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_RX_DMA_BUSY,
41 		       0, 1000))
42 		dev_warn(dev->mt76.dev, "TX DMA did not stop\n");
43 	mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_RX_DMA_EN);
44 }
45 
46 static void mt76x0e_stop(struct ieee80211_hw *hw)
47 {
48 	struct mt76x02_dev *dev = hw->priv;
49 
50 	clear_bit(MT76_STATE_RUNNING, &dev->mt76.state);
51 	mt76x0e_stop_hw(dev);
52 }
53 
54 static void
55 mt76x0e_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
56 	      u32 queues, bool drop)
57 {
58 }
59 
60 static const struct ieee80211_ops mt76x0e_ops = {
61 	.tx = mt76x02_tx,
62 	.start = mt76x0e_start,
63 	.stop = mt76x0e_stop,
64 	.add_interface = mt76x02_add_interface,
65 	.remove_interface = mt76x02_remove_interface,
66 	.config = mt76x0_config,
67 	.configure_filter = mt76x02_configure_filter,
68 	.bss_info_changed = mt76x02_bss_info_changed,
69 	.sta_state = mt76_sta_state,
70 	.set_key = mt76x02_set_key,
71 	.conf_tx = mt76x02_conf_tx,
72 	.sw_scan_start = mt76_sw_scan,
73 	.sw_scan_complete = mt76x02_sw_scan_complete,
74 	.ampdu_action = mt76x02_ampdu_action,
75 	.sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
76 	.wake_tx_queue = mt76_wake_tx_queue,
77 	.get_survey = mt76_get_survey,
78 	.get_txpower = mt76_get_txpower,
79 	.flush = mt76x0e_flush,
80 	.set_tim = mt76_set_tim,
81 	.release_buffered_frames = mt76_release_buffered_frames,
82 	.set_coverage_class = mt76x02_set_coverage_class,
83 	.set_rts_threshold = mt76x02_set_rts_threshold,
84 	.get_antenna = mt76_get_antenna,
85 };
86 
87 static int mt76x0e_register_device(struct mt76x02_dev *dev)
88 {
89 	int err;
90 
91 	mt76x0_chip_onoff(dev, true, false);
92 	if (!mt76x02_wait_for_mac(&dev->mt76))
93 		return -ETIMEDOUT;
94 
95 	mt76x02_dma_disable(dev);
96 	err = mt76x0e_mcu_init(dev);
97 	if (err < 0)
98 		return err;
99 
100 	err = mt76x02_dma_init(dev);
101 	if (err < 0)
102 		return err;
103 
104 	err = mt76x0_init_hardware(dev);
105 	if (err < 0)
106 		return err;
107 
108 	mt76x02e_init_beacon_config(dev);
109 
110 	if (mt76_chip(&dev->mt76) == 0x7610) {
111 		u16 val;
112 
113 		mt76_clear(dev, MT_COEXCFG0, BIT(0));
114 
115 		val = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_0);
116 		if (!(val & MT_EE_NIC_CONF_0_PA_IO_CURRENT))
117 			mt76_set(dev, MT_XO_CTRL7, 0xc03);
118 	}
119 
120 	mt76_clear(dev, 0x110, BIT(9));
121 	mt76_set(dev, MT_MAX_LEN_CFG, BIT(13));
122 
123 	err = mt76x0_register_device(dev);
124 	if (err < 0)
125 		return err;
126 
127 	set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
128 
129 	return 0;
130 }
131 
132 static int
133 mt76x0e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
134 {
135 	static const struct mt76_driver_ops drv_ops = {
136 		.txwi_size = sizeof(struct mt76x02_txwi),
137 		.drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
138 			     MT_DRV_SW_RX_AIRTIME,
139 		.survey_flags = SURVEY_INFO_TIME_TX,
140 		.update_survey = mt76x02_update_channel,
141 		.tx_prepare_skb = mt76x02_tx_prepare_skb,
142 		.tx_complete_skb = mt76x02_tx_complete_skb,
143 		.rx_skb = mt76x02_queue_rx_skb,
144 		.rx_poll_complete = mt76x02_rx_poll_complete,
145 		.sta_ps = mt76x02_sta_ps,
146 		.sta_add = mt76x02_sta_add,
147 		.sta_remove = mt76x02_sta_remove,
148 	};
149 	struct mt76x02_dev *dev;
150 	struct mt76_dev *mdev;
151 	int ret;
152 
153 	ret = pcim_enable_device(pdev);
154 	if (ret)
155 		return ret;
156 
157 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
158 	if (ret)
159 		return ret;
160 
161 	pci_set_master(pdev);
162 
163 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
164 	if (ret)
165 		return ret;
166 
167 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x0e_ops,
168 				 &drv_ops);
169 	if (!mdev)
170 		return -ENOMEM;
171 
172 	dev = container_of(mdev, struct mt76x02_dev, mt76);
173 	mutex_init(&dev->phy_mutex);
174 
175 	mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
176 
177 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
178 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
179 
180 	ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
181 			       IRQF_SHARED, KBUILD_MODNAME, dev);
182 	if (ret)
183 		goto error;
184 
185 	ret = mt76x0e_register_device(dev);
186 	if (ret < 0)
187 		goto error;
188 
189 	return 0;
190 
191 error:
192 	ieee80211_free_hw(mt76_hw(dev));
193 	return ret;
194 }
195 
196 static void mt76x0e_cleanup(struct mt76x02_dev *dev)
197 {
198 	clear_bit(MT76_STATE_INITIALIZED, &dev->mt76.state);
199 	tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
200 	mt76x0_chip_onoff(dev, false, false);
201 	mt76x0e_stop_hw(dev);
202 	mt76x02_dma_cleanup(dev);
203 	mt76x02_mcu_cleanup(dev);
204 }
205 
206 static void
207 mt76x0e_remove(struct pci_dev *pdev)
208 {
209 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
210 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
211 
212 	mt76_unregister_device(mdev);
213 	mt76x0e_cleanup(dev);
214 	mt76_free_device(mdev);
215 }
216 
217 static const struct pci_device_id mt76x0e_device_table[] = {
218 	{ PCI_DEVICE(0x14c3, 0x7630) },
219 	{ PCI_DEVICE(0x14c3, 0x7650) },
220 	{ },
221 };
222 
223 MODULE_DEVICE_TABLE(pci, mt76x0e_device_table);
224 MODULE_FIRMWARE(MT7610E_FIRMWARE);
225 MODULE_FIRMWARE(MT7650E_FIRMWARE);
226 MODULE_LICENSE("Dual BSD/GPL");
227 
228 static struct pci_driver mt76x0e_driver = {
229 	.name		= KBUILD_MODNAME,
230 	.id_table	= mt76x0e_device_table,
231 	.probe		= mt76x0e_probe,
232 	.remove		= mt76x0e_remove,
233 };
234 
235 module_pci_driver(mt76x0e_driver);
236