1 /* 2 * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name> 3 * 4 * Permission to use, copy, modify, and/or distribute this software for any 5 * purpose with or without fee is hereby granted, provided that the above 6 * copyright notice and this permission notice appear in all copies. 7 * 8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/pci.h> 20 21 #include "mt76x0.h" 22 #include "mcu.h" 23 24 static int mt76x0e_start(struct ieee80211_hw *hw) 25 { 26 struct mt76x02_dev *dev = hw->priv; 27 28 mutex_lock(&dev->mt76.mutex); 29 30 mt76x02_mac_start(dev); 31 mt76x0_phy_calibrate(dev, true); 32 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->mac_work, 33 MT_CALIBRATE_INTERVAL); 34 ieee80211_queue_delayed_work(dev->mt76.hw, &dev->cal_work, 35 MT_CALIBRATE_INTERVAL); 36 set_bit(MT76_STATE_RUNNING, &dev->mt76.state); 37 38 mutex_unlock(&dev->mt76.mutex); 39 40 return 0; 41 } 42 43 static void mt76x0e_stop_hw(struct mt76x02_dev *dev) 44 { 45 cancel_delayed_work_sync(&dev->cal_work); 46 cancel_delayed_work_sync(&dev->mac_work); 47 48 if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_BUSY, 49 0, 1000)) 50 dev_warn(dev->mt76.dev, "TX DMA did not stop\n"); 51 mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_TX_DMA_EN); 52 53 mt76x0_mac_stop(dev); 54 55 if (!mt76_poll(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_RX_DMA_BUSY, 56 0, 1000)) 57 dev_warn(dev->mt76.dev, "TX DMA did not stop\n"); 58 mt76_clear(dev, MT_WPDMA_GLO_CFG, MT_WPDMA_GLO_CFG_RX_DMA_EN); 59 } 60 61 static void mt76x0e_stop(struct ieee80211_hw *hw) 62 { 63 struct mt76x02_dev *dev = hw->priv; 64 65 mutex_lock(&dev->mt76.mutex); 66 clear_bit(MT76_STATE_RUNNING, &dev->mt76.state); 67 mt76x0e_stop_hw(dev); 68 mutex_unlock(&dev->mt76.mutex); 69 } 70 71 static const struct ieee80211_ops mt76x0e_ops = { 72 .tx = mt76x02_tx, 73 .start = mt76x0e_start, 74 .stop = mt76x0e_stop, 75 .add_interface = mt76x02_add_interface, 76 .remove_interface = mt76x02_remove_interface, 77 .config = mt76x0_config, 78 .configure_filter = mt76x02_configure_filter, 79 .sta_add = mt76x02_sta_add, 80 .sta_remove = mt76x02_sta_remove, 81 .set_key = mt76x02_set_key, 82 .conf_tx = mt76x02_conf_tx, 83 .sw_scan_start = mt76x0_sw_scan, 84 .sw_scan_complete = mt76x0_sw_scan_complete, 85 .ampdu_action = mt76x02_ampdu_action, 86 .sta_rate_tbl_update = mt76x02_sta_rate_tbl_update, 87 .wake_tx_queue = mt76_wake_tx_queue, 88 }; 89 90 static int mt76x0e_register_device(struct mt76x02_dev *dev) 91 { 92 int err; 93 94 mt76x0_chip_onoff(dev, true, false); 95 if (!mt76x02_wait_for_mac(&dev->mt76)) 96 return -ETIMEDOUT; 97 98 mt76x02_dma_disable(dev); 99 err = mt76x0e_mcu_init(dev); 100 if (err < 0) 101 return err; 102 103 err = mt76x02_dma_init(dev); 104 if (err < 0) 105 return err; 106 107 err = mt76x0_init_hardware(dev); 108 if (err < 0) 109 return err; 110 111 if (mt76_chip(&dev->mt76) == 0x7610) { 112 u16 val; 113 114 mt76_clear(dev, MT_COEXCFG0, BIT(0)); 115 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); 119 } 120 121 mt76_clear(dev, 0x110, BIT(9)); 122 mt76_set(dev, MT_MAX_LEN_CFG, BIT(13)); 123 124 err = mt76x0_register_device(dev); 125 if (err < 0) 126 return err; 127 128 set_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); 129 130 return 0; 131 } 132 133 static int 134 mt76x0e_probe(struct pci_dev *pdev, const struct pci_device_id *id) 135 { 136 static const struct mt76_driver_ops drv_ops = { 137 .txwi_size = sizeof(struct mt76x02_txwi), 138 .tx_prepare_skb = mt76x02_tx_prepare_skb, 139 .tx_complete_skb = mt76x02_tx_complete_skb, 140 .rx_skb = mt76x02_queue_rx_skb, 141 .rx_poll_complete = mt76x02_rx_poll_complete, 142 }; 143 struct mt76x02_dev *dev; 144 int ret; 145 146 ret = pcim_enable_device(pdev); 147 if (ret) 148 return ret; 149 150 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); 151 if (ret) 152 return ret; 153 154 pci_set_master(pdev); 155 156 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 157 if (ret) 158 return ret; 159 160 dev = mt76x0_alloc_device(&pdev->dev, &drv_ops, &mt76x0e_ops); 161 if (!dev) 162 return -ENOMEM; 163 164 mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]); 165 166 dev->mt76.rev = mt76_rr(dev, MT_ASIC_VERSION); 167 dev_info(dev->mt76.dev, "ASIC revision: %08x\n", dev->mt76.rev); 168 169 ret = devm_request_irq(dev->mt76.dev, pdev->irq, mt76x02_irq_handler, 170 IRQF_SHARED, KBUILD_MODNAME, dev); 171 if (ret) 172 goto error; 173 174 ret = mt76x0e_register_device(dev); 175 if (ret < 0) 176 goto error; 177 178 return 0; 179 180 error: 181 ieee80211_free_hw(mt76_hw(dev)); 182 return ret; 183 } 184 185 static void mt76x0e_cleanup(struct mt76x02_dev *dev) 186 { 187 clear_bit(MT76_STATE_INITIALIZED, &dev->mt76.state); 188 mt76x0_chip_onoff(dev, false, false); 189 mt76x0e_stop_hw(dev); 190 mt76x02_dma_cleanup(dev); 191 mt76x02_mcu_cleanup(dev); 192 } 193 194 static void 195 mt76x0e_remove(struct pci_dev *pdev) 196 { 197 struct mt76_dev *mdev = pci_get_drvdata(pdev); 198 struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76); 199 200 mt76_unregister_device(mdev); 201 mt76x0e_cleanup(dev); 202 ieee80211_free_hw(mdev->hw); 203 } 204 205 static const struct pci_device_id mt76x0e_device_table[] = { 206 { PCI_DEVICE(0x14c3, 0x7630) }, 207 { PCI_DEVICE(0x14c3, 0x7650) }, 208 { }, 209 }; 210 211 MODULE_DEVICE_TABLE(pci, mt76x0e_device_table); 212 MODULE_LICENSE("Dual BSD/GPL"); 213 214 static struct pci_driver mt76x0e_driver = { 215 .name = KBUILD_MODNAME, 216 .id_table = mt76x0e_device_table, 217 .probe = mt76x0e_probe, 218 .remove = mt76x0e_remove, 219 }; 220 221 module_pci_driver(mt76x0e_driver); 222