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