gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / net / wireless / mediatek / mt76 / mt76x0 / pci.c
blob0b520ae08d018a65610b36bd2411ab7296e52373
1 // SPDX-License-Identifier: ISC
2 /*
3 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4 */
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
10 #include "mt76x0.h"
11 #include "mcu.h"
13 static int mt76x0e_start(struct ieee80211_hw *hw)
15 struct mt76x02_dev *dev = hw->priv;
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->mphy.state);
25 return 0;
28 static void mt76x0e_stop_hw(struct mt76x02_dev *dev)
30 cancel_delayed_work_sync(&dev->cal_work);
31 cancel_delayed_work_sync(&dev->mt76.mac_work);
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);
38 mt76x0_mac_stop(dev);
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);
46 static void mt76x0e_stop(struct ieee80211_hw *hw)
48 struct mt76x02_dev *dev = hw->priv;
50 clear_bit(MT76_STATE_RUNNING, &dev->mphy.state);
51 mt76x0e_stop_hw(dev);
54 static void
55 mt76x0e_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
56 u32 queues, bool drop)
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 .sta_pre_rcu_remove = mt76_sta_pre_rcu_remove,
71 .set_key = mt76x02_set_key,
72 .conf_tx = mt76x02_conf_tx,
73 .sw_scan_start = mt76_sw_scan,
74 .sw_scan_complete = mt76x02_sw_scan_complete,
75 .ampdu_action = mt76x02_ampdu_action,
76 .sta_rate_tbl_update = mt76x02_sta_rate_tbl_update,
77 .wake_tx_queue = mt76_wake_tx_queue,
78 .get_survey = mt76_get_survey,
79 .get_txpower = mt76_get_txpower,
80 .flush = mt76x0e_flush,
81 .set_tim = mt76_set_tim,
82 .release_buffered_frames = mt76_release_buffered_frames,
83 .set_coverage_class = mt76x02_set_coverage_class,
84 .set_rts_threshold = mt76x02_set_rts_threshold,
85 .get_antenna = mt76_get_antenna,
88 static int mt76x0e_register_device(struct mt76x02_dev *dev)
90 int err;
92 mt76x0_chip_onoff(dev, true, false);
93 if (!mt76x02_wait_for_mac(&dev->mt76))
94 return -ETIMEDOUT;
96 mt76x02_dma_disable(dev);
97 err = mt76x0e_mcu_init(dev);
98 if (err < 0)
99 return err;
101 err = mt76x02_dma_init(dev);
102 if (err < 0)
103 return err;
105 err = mt76x0_init_hardware(dev);
106 if (err < 0)
107 return err;
109 mt76x02e_init_beacon_config(dev);
111 if (mt76_chip(&dev->mt76) == 0x7610) {
112 u16 val;
114 mt76_clear(dev, MT_COEXCFG0, BIT(0));
116 val = mt76x02_eeprom_get(dev, MT_EE_NIC_CONF_0);
117 if (!(val & MT_EE_NIC_CONF_0_PA_IO_CURRENT))
118 mt76_set(dev, MT_XO_CTRL7, 0xc03);
121 mt76_clear(dev, 0x110, BIT(9));
122 mt76_set(dev, MT_MAX_LEN_CFG, BIT(13));
124 err = mt76x0_register_device(dev);
125 if (err < 0)
126 return err;
128 set_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
130 return 0;
133 static int
134 mt76x0e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
136 static const struct mt76_driver_ops drv_ops = {
137 .txwi_size = sizeof(struct mt76x02_txwi),
138 .drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
139 MT_DRV_SW_RX_AIRTIME,
140 .survey_flags = SURVEY_INFO_TIME_TX,
141 .update_survey = mt76x02_update_channel,
142 .tx_prepare_skb = mt76x02_tx_prepare_skb,
143 .tx_complete_skb = mt76x02_tx_complete_skb,
144 .rx_skb = mt76x02_queue_rx_skb,
145 .rx_poll_complete = mt76x02_rx_poll_complete,
146 .sta_ps = mt76x02_sta_ps,
147 .sta_add = mt76x02_sta_add,
148 .sta_remove = mt76x02_sta_remove,
150 struct mt76x02_dev *dev;
151 struct mt76_dev *mdev;
152 int ret;
154 ret = pcim_enable_device(pdev);
155 if (ret)
156 return ret;
158 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
159 if (ret)
160 return ret;
162 pci_set_master(pdev);
164 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
165 if (ret)
166 return ret;
168 mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x0e_ops,
169 &drv_ops);
170 if (!mdev)
171 return -ENOMEM;
173 dev = container_of(mdev, struct mt76x02_dev, mt76);
174 mutex_init(&dev->phy_mutex);
176 mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
178 mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
179 dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
181 ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
182 IRQF_SHARED, KBUILD_MODNAME, dev);
183 if (ret)
184 goto error;
186 ret = mt76x0e_register_device(dev);
187 if (ret < 0)
188 goto error;
190 return 0;
192 error:
193 ieee80211_free_hw(mt76_hw(dev));
194 return ret;
197 static void mt76x0e_cleanup(struct mt76x02_dev *dev)
199 clear_bit(MT76_STATE_INITIALIZED, &dev->mphy.state);
200 tasklet_disable(&dev->mt76.pre_tbtt_tasklet);
201 mt76x0_chip_onoff(dev, false, false);
202 mt76x0e_stop_hw(dev);
203 mt76x02_dma_cleanup(dev);
204 mt76x02_mcu_cleanup(dev);
207 static void
208 mt76x0e_remove(struct pci_dev *pdev)
210 struct mt76_dev *mdev = pci_get_drvdata(pdev);
211 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
213 mt76_unregister_device(mdev);
214 mt76x0e_cleanup(dev);
215 mt76_free_device(mdev);
218 static const struct pci_device_id mt76x0e_device_table[] = {
219 { PCI_DEVICE(0x14c3, 0x7630) },
220 { PCI_DEVICE(0x14c3, 0x7650) },
221 { },
224 MODULE_DEVICE_TABLE(pci, mt76x0e_device_table);
225 MODULE_FIRMWARE(MT7610E_FIRMWARE);
226 MODULE_FIRMWARE(MT7650E_FIRMWARE);
227 MODULE_LICENSE("Dual BSD/GPL");
229 static struct pci_driver mt76x0e_driver = {
230 .name = KBUILD_MODNAME,
231 .id_table = mt76x0e_device_table,
232 .probe = mt76x0e_probe,
233 .remove = mt76x0e_remove,
236 module_pci_driver(mt76x0e_driver);