1 // SPDX-License-Identifier: ISC
2 /* Copyright (C) 2020 MediaTek Inc.
3  *
4  * Author: Ryder Lee <ryder.lee@mediatek.com>
5  */
6 
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/pci.h>
10 
11 #include "mt7915.h"
12 #include "mac.h"
13 #include "../trace.h"
14 
15 static const struct pci_device_id mt7915_pci_device_table[] = {
16 	{ PCI_DEVICE(0x14c3, 0x7915) },
17 	{ },
18 };
19 
20 static void
21 mt7915_rx_poll_complete(struct mt76_dev *mdev, enum mt76_rxq_id q)
22 {
23 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
24 
25 	mt7915_irq_enable(dev, MT_INT_RX_DONE(q));
26 }
27 
28 /* TODO: support 2/4/6/8 MSI-X vectors */
29 static irqreturn_t mt7915_irq_handler(int irq, void *dev_instance)
30 {
31 	struct mt7915_dev *dev = dev_instance;
32 	u32 intr;
33 
34 	intr = mt76_rr(dev, MT_INT_SOURCE_CSR);
35 	mt76_wr(dev, MT_INT_SOURCE_CSR, intr);
36 
37 	if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
38 		return IRQ_NONE;
39 
40 	trace_dev_irq(&dev->mt76, intr, dev->mt76.mmio.irqmask);
41 
42 	intr &= dev->mt76.mmio.irqmask;
43 
44 	if (intr & MT_INT_TX_DONE_ALL) {
45 		mt7915_irq_disable(dev, MT_INT_TX_DONE_ALL);
46 		napi_schedule(&dev->mt76.tx_napi);
47 	}
48 
49 	if (intr & MT_INT_RX_DONE_DATA) {
50 		mt7915_irq_disable(dev, MT_INT_RX_DONE_DATA);
51 		napi_schedule(&dev->mt76.napi[0]);
52 	}
53 
54 	if (intr & MT_INT_RX_DONE_WM) {
55 		mt7915_irq_disable(dev, MT_INT_RX_DONE_WM);
56 		napi_schedule(&dev->mt76.napi[1]);
57 	}
58 
59 	if (intr & MT_INT_RX_DONE_WA) {
60 		mt7915_irq_disable(dev, MT_INT_RX_DONE_WA);
61 		napi_schedule(&dev->mt76.napi[2]);
62 	}
63 
64 	if (intr & MT_INT_MCU_CMD) {
65 		u32 val = mt76_rr(dev, MT_MCU_CMD);
66 
67 		mt76_wr(dev, MT_MCU_CMD, val);
68 		if (val & MT_MCU_CMD_ERROR_MASK) {
69 			dev->reset_state = val;
70 			ieee80211_queue_work(mt76_hw(dev), &dev->reset_work);
71 			wake_up(&dev->reset_wait);
72 		}
73 	}
74 
75 	return IRQ_HANDLED;
76 }
77 
78 static int
79 mt7915_alloc_device(struct pci_dev *pdev, struct mt7915_dev *dev)
80 {
81 #define NUM_BANDS	2
82 	int i;
83 	s8 **sku;
84 
85 	sku = devm_kzalloc(&pdev->dev, NUM_BANDS * sizeof(*sku), GFP_KERNEL);
86 	if (!sku)
87 		return -ENOMEM;
88 
89 	for (i = 0; i < NUM_BANDS; i++) {
90 		sku[i] = devm_kzalloc(&pdev->dev, MT7915_SKU_TABLE_SIZE *
91 				      sizeof(**sku), GFP_KERNEL);
92 		if (!sku[i])
93 			return -ENOMEM;
94 	}
95 	dev->rate_power = sku;
96 
97 	return 0;
98 }
99 
100 static int mt7915_pci_probe(struct pci_dev *pdev,
101 			    const struct pci_device_id *id)
102 {
103 	static const struct mt76_driver_ops drv_ops = {
104 		/* txwi_size = txd size + txp size */
105 		.txwi_size = MT_TXD_SIZE + sizeof(struct mt7915_txp),
106 		.drv_flags = MT_DRV_TXWI_NO_FREE,
107 		.survey_flags = SURVEY_INFO_TIME_TX |
108 				SURVEY_INFO_TIME_RX |
109 				SURVEY_INFO_TIME_BSS_RX,
110 		.tx_prepare_skb = mt7915_tx_prepare_skb,
111 		.tx_complete_skb = mt7915_tx_complete_skb,
112 		.rx_skb = mt7915_queue_rx_skb,
113 		.rx_poll_complete = mt7915_rx_poll_complete,
114 		.sta_ps = mt7915_sta_ps,
115 		.sta_add = mt7915_mac_sta_add,
116 		.sta_remove = mt7915_mac_sta_remove,
117 		.update_survey = mt7915_update_channel,
118 	};
119 	struct mt7915_dev *dev;
120 	struct mt76_dev *mdev;
121 	int ret;
122 
123 	ret = pcim_enable_device(pdev);
124 	if (ret)
125 		return ret;
126 
127 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
128 	if (ret)
129 		return ret;
130 
131 	pci_set_master(pdev);
132 
133 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
134 	if (ret)
135 		return ret;
136 
137 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt7915_ops,
138 				 &drv_ops);
139 	if (!mdev)
140 		return -ENOMEM;
141 
142 	dev = container_of(mdev, struct mt7915_dev, mt76);
143 	ret = mt7915_alloc_device(pdev, dev);
144 	if (ret)
145 		return ret;
146 
147 	mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
148 	mdev->rev = (mt7915_l1_rr(dev, MT_HW_CHIPID) << 16) |
149 		    (mt7915_l1_rr(dev, MT_HW_REV) & 0xff);
150 	dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
151 
152 	/* master switch of PCIe tnterrupt enable */
153 	mt7915_l1_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff);
154 
155 	ret = devm_request_irq(mdev->dev, pdev->irq, mt7915_irq_handler,
156 			       IRQF_SHARED, KBUILD_MODNAME, dev);
157 	if (ret)
158 		goto error;
159 
160 	ret = mt7915_register_device(dev);
161 	if (ret)
162 		goto error;
163 
164 	return 0;
165 error:
166 	ieee80211_free_hw(mt76_hw(dev));
167 	return ret;
168 }
169 
170 static void mt7915_pci_remove(struct pci_dev *pdev)
171 {
172 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
173 	struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
174 
175 	mt7915_unregister_device(dev);
176 }
177 
178 struct pci_driver mt7915_pci_driver = {
179 	.name		= KBUILD_MODNAME,
180 	.id_table	= mt7915_pci_device_table,
181 	.probe		= mt7915_pci_probe,
182 	.remove		= mt7915_pci_remove,
183 };
184 
185 module_pci_driver(mt7915_pci_driver);
186 
187 MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
188 MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
189 MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
190 MODULE_FIRMWARE(MT7915_ROM_PATCH);
191 MODULE_LICENSE("Dual BSD/GPL");
192