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 static void
31 mt7615_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q)
32 {
33 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
34 
35 	mt7615_irq_enable(dev, MT_INT_RX_DONE(q));
36 }
37 
38 static irqreturn_t mt7615_irq_handler(int irq, void *dev_instance)
39 {
40 	struct mt7615_dev *dev = dev_instance;
41 	u32 intr;
42 
43 	intr = mt76_rr(dev, MT_INT_SOURCE_CSR);
44 	mt76_wr(dev, MT_INT_SOURCE_CSR, intr);
45 
46 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mt76.state))
47 		return IRQ_NONE;
48 
49 	intr &= dev->mt76.mmio.irqmask;
50 
51 	if (intr & MT_INT_TX_DONE_ALL) {
52 		mt7615_irq_disable(dev, MT_INT_TX_DONE_ALL);
53 		napi_schedule(&dev->mt76.tx_napi);
54 	}
55 
56 	if (intr & MT_INT_RX_DONE(0)) {
57 		mt7615_irq_disable(dev, MT_INT_RX_DONE(0));
58 		napi_schedule(&dev->mt76.napi[0]);
59 	}
60 
61 	if (intr & MT_INT_RX_DONE(1)) {
62 		mt7615_irq_disable(dev, MT_INT_RX_DONE(1));
63 		napi_schedule(&dev->mt76.napi[1]);
64 	}
65 
66 	return IRQ_HANDLED;
67 }
68 
69 static int mt7615_pci_probe(struct pci_dev *pdev,
70 			    const struct pci_device_id *id)
71 {
72 	static const struct mt76_driver_ops drv_ops = {
73 		/* txwi_size = txd size + txp size */
74 		.txwi_size = MT_TXD_SIZE + sizeof(struct mt7615_txp),
75 		.txwi_flags = MT_TXWI_NO_FREE,
76 		.tx_prepare_skb = mt7615_tx_prepare_skb,
77 		.tx_complete_skb = mt7615_tx_complete_skb,
78 		.rx_skb = mt7615_queue_rx_skb,
79 		.rx_poll_complete = mt7615_rx_poll_complete,
80 		.sta_ps = mt7615_sta_ps,
81 		.sta_add = mt7615_sta_add,
82 		.sta_assoc = mt7615_sta_assoc,
83 		.sta_remove = mt7615_sta_remove,
84 		.update_survey = mt7615_update_channel,
85 	};
86 	struct mt7615_dev *dev;
87 	struct mt76_dev *mdev;
88 	int ret;
89 
90 	ret = pcim_enable_device(pdev);
91 	if (ret)
92 		return ret;
93 
94 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
95 	if (ret)
96 		return ret;
97 
98 	pci_set_master(pdev);
99 
100 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
101 	if (ret)
102 		return ret;
103 
104 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt7615_ops,
105 				 &drv_ops);
106 	if (!mdev)
107 		return -ENOMEM;
108 
109 	dev = container_of(mdev, struct mt7615_dev, mt76);
110 	mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
111 
112 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
113 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
114 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
115 
116 	ret = devm_request_irq(mdev->dev, pdev->irq, mt7615_irq_handler,
117 			       IRQF_SHARED, KBUILD_MODNAME, dev);
118 	if (ret)
119 		goto error;
120 
121 	ret = mt7615_register_device(dev);
122 	if (ret)
123 		goto error;
124 
125 	return 0;
126 error:
127 	ieee80211_free_hw(mt76_hw(dev));
128 	return ret;
129 }
130 
131 static void mt7615_pci_remove(struct pci_dev *pdev)
132 {
133 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
134 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
135 
136 	mt7615_unregister_device(dev);
137 }
138 
139 struct pci_driver mt7615_pci_driver = {
140 	.name		= KBUILD_MODNAME,
141 	.id_table	= mt7615_pci_device_table,
142 	.probe		= mt7615_pci_probe,
143 	.remove		= mt7615_pci_remove,
144 };
145 
146 module_pci_driver(mt7615_pci_driver);
147 
148 MODULE_DEVICE_TABLE(pci, mt7615_pci_device_table);
149 MODULE_FIRMWARE(MT7615_FIRMWARE_CR4);
150 MODULE_FIRMWARE(MT7615_FIRMWARE_N9);
151 MODULE_FIRMWARE(MT7615_ROM_PATCH);
152 MODULE_LICENSE("Dual BSD/GPL");
153