1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Ryder Lee <ryder.lee@mediatek.com> 5 * Felix Fietkau <nbd@nbd.name> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 12 #include "mt7615.h" 13 #include "mac.h" 14 15 static const struct pci_device_id mt7615_pci_device_table[] = { 16 { PCI_DEVICE(0x14c3, 0x7615) }, 17 { }, 18 }; 19 20 u32 mt7615_reg_map(struct mt7615_dev *dev, u32 addr) 21 { 22 u32 base = addr & MT_MCU_PCIE_REMAP_2_BASE; 23 u32 offset = addr & MT_MCU_PCIE_REMAP_2_OFFSET; 24 25 mt76_wr(dev, MT_MCU_PCIE_REMAP_2, base); 26 27 return MT_PCIE_REMAP_BASE_2 + offset; 28 } 29 30 void mt7615_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q) 31 { 32 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 33 34 mt7615_irq_enable(dev, MT_INT_RX_DONE(q)); 35 } 36 37 irqreturn_t mt7615_irq_handler(int irq, void *dev_instance) 38 { 39 struct mt7615_dev *dev = dev_instance; 40 u32 intr; 41 42 intr = mt76_rr(dev, MT_INT_SOURCE_CSR); 43 mt76_wr(dev, MT_INT_SOURCE_CSR, intr); 44 45 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state)) 46 return IRQ_NONE; 47 48 intr &= dev->mt76.mmio.irqmask; 49 50 if (intr & MT_INT_TX_DONE_ALL) { 51 mt7615_irq_disable(dev, MT_INT_TX_DONE_ALL); 52 tasklet_schedule(&dev->mt76.tx_tasklet); 53 } 54 55 if (intr & MT_INT_RX_DONE(0)) { 56 mt7615_irq_disable(dev, MT_INT_RX_DONE(0)); 57 napi_schedule(&dev->mt76.napi[0]); 58 } 59 60 if (intr & MT_INT_RX_DONE(1)) { 61 mt7615_irq_disable(dev, MT_INT_RX_DONE(1)); 62 napi_schedule(&dev->mt76.napi[1]); 63 } 64 65 return IRQ_HANDLED; 66 } 67 68 static int mt7615_pci_probe(struct pci_dev *pdev, 69 const struct pci_device_id *id) 70 { 71 static const struct mt76_driver_ops drv_ops = { 72 /* txwi_size = txd size + txp size */ 73 .txwi_size = MT_TXD_SIZE + sizeof(struct mt7615_txp), 74 .txwi_flags = MT_TXWI_NO_FREE, 75 .tx_prepare_skb = mt7615_tx_prepare_skb, 76 .tx_complete_skb = mt7615_tx_complete_skb, 77 .rx_skb = mt7615_queue_rx_skb, 78 .rx_poll_complete = mt7615_rx_poll_complete, 79 .sta_ps = mt7615_sta_ps, 80 .sta_add = mt7615_sta_add, 81 .sta_assoc = mt7615_sta_assoc, 82 .sta_remove = mt7615_sta_remove, 83 }; 84 struct mt7615_dev *dev; 85 struct mt76_dev *mdev; 86 int ret; 87 88 ret = pcim_enable_device(pdev); 89 if (ret) 90 return ret; 91 92 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); 93 if (ret) 94 return ret; 95 96 pci_set_master(pdev); 97 98 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 99 if (ret) 100 return ret; 101 102 mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt7615_ops, 103 &drv_ops); 104 if (!mdev) 105 return -ENOMEM; 106 107 dev = container_of(mdev, struct mt7615_dev, mt76); 108 mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]); 109 110 mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) | 111 (mt76_rr(dev, MT_HW_REV) & 0xff); 112 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev); 113 114 ret = devm_request_irq(mdev->dev, pdev->irq, mt7615_irq_handler, 115 IRQF_SHARED, KBUILD_MODNAME, dev); 116 if (ret) 117 goto error; 118 119 ret = mt7615_register_device(dev); 120 if (ret) 121 goto error; 122 123 return 0; 124 error: 125 ieee80211_free_hw(mt76_hw(dev)); 126 return ret; 127 } 128 129 static void mt7615_pci_remove(struct pci_dev *pdev) 130 { 131 struct mt76_dev *mdev = pci_get_drvdata(pdev); 132 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 133 134 mt7615_unregister_device(dev); 135 } 136 137 struct pci_driver mt7615_pci_driver = { 138 .name = KBUILD_MODNAME, 139 .id_table = mt7615_pci_device_table, 140 .probe = mt7615_pci_probe, 141 .remove = mt7615_pci_remove, 142 }; 143 144 module_pci_driver(mt7615_pci_driver); 145 146 MODULE_DEVICE_TABLE(pci, mt7615_pci_device_table); 147 MODULE_FIRMWARE(MT7615_FIRMWARE_CR4); 148 MODULE_FIRMWARE(MT7615_FIRMWARE_N9); 149 MODULE_FIRMWARE(MT7615_ROM_PATCH); 150 MODULE_LICENSE("Dual BSD/GPL"); 151