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 	};
85 	struct mt7615_dev *dev;
86 	struct mt76_dev *mdev;
87 	int ret;
88 
89 	ret = pcim_enable_device(pdev);
90 	if (ret)
91 		return ret;
92 
93 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
94 	if (ret)
95 		return ret;
96 
97 	pci_set_master(pdev);
98 
99 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
100 	if (ret)
101 		return ret;
102 
103 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt7615_ops,
104 				 &drv_ops);
105 	if (!mdev)
106 		return -ENOMEM;
107 
108 	dev = container_of(mdev, struct mt7615_dev, mt76);
109 	mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
110 
111 	mdev->rev = (mt76_rr(dev, MT_HW_CHIPID) << 16) |
112 		    (mt76_rr(dev, MT_HW_REV) & 0xff);
113 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
114 
115 	ret = devm_request_irq(mdev->dev, pdev->irq, mt7615_irq_handler,
116 			       IRQF_SHARED, KBUILD_MODNAME, dev);
117 	if (ret)
118 		goto error;
119 
120 	ret = mt7615_register_device(dev);
121 	if (ret)
122 		goto error;
123 
124 	return 0;
125 error:
126 	ieee80211_free_hw(mt76_hw(dev));
127 	return ret;
128 }
129 
130 static void mt7615_pci_remove(struct pci_dev *pdev)
131 {
132 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
133 	struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76);
134 
135 	mt7615_unregister_device(dev);
136 }
137 
138 struct pci_driver mt7615_pci_driver = {
139 	.name		= KBUILD_MODNAME,
140 	.id_table	= mt7615_pci_device_table,
141 	.probe		= mt7615_pci_probe,
142 	.remove		= mt7615_pci_remove,
143 };
144 
145 module_pci_driver(mt7615_pci_driver);
146 
147 MODULE_DEVICE_TABLE(pci, mt7615_pci_device_table);
148 MODULE_FIRMWARE(MT7615_FIRMWARE_CR4);
149 MODULE_FIRMWARE(MT7615_FIRMWARE_N9);
150 MODULE_FIRMWARE(MT7615_ROM_PATCH);
151 MODULE_LICENSE("Dual BSD/GPL");
152