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
mt7915_rx_poll_complete(struct mt76_dev * mdev,enum mt76_rxq_id q)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 */
mt7915_irq_handler(int irq,void * dev_instance)29 static irqreturn_t mt7915_irq_handler(int irq, void *dev_instance)
30 {
31 struct mt7915_dev *dev = dev_instance;
32 u32 intr, mask;
33
34 intr = mt76_rr(dev, MT_INT_SOURCE_CSR);
35 intr &= dev->mt76.mmio.irqmask;
36 mt76_wr(dev, MT_INT_SOURCE_CSR, intr);
37
38 if (!test_bit(MT76_STATE_INITIALIZED, &dev->mphy.state))
39 return IRQ_NONE;
40
41 trace_dev_irq(&dev->mt76, intr, dev->mt76.mmio.irqmask);
42
43 mask = intr & MT_INT_RX_DONE_ALL;
44 if (intr & MT_INT_TX_DONE_MCU)
45 mask |= MT_INT_TX_DONE_MCU;
46
47 mt7915_irq_disable(dev, mask);
48
49 if (intr & MT_INT_TX_DONE_MCU)
50 napi_schedule(&dev->mt76.tx_napi);
51
52 if (intr & MT_INT_RX_DONE_DATA)
53 napi_schedule(&dev->mt76.napi[0]);
54
55 if (intr & MT_INT_RX_DONE_WM)
56 napi_schedule(&dev->mt76.napi[1]);
57
58 if (intr & MT_INT_RX_DONE_WA)
59 napi_schedule(&dev->mt76.napi[2]);
60
61 if (intr & MT_INT_MCU_CMD) {
62 u32 val = mt76_rr(dev, MT_MCU_CMD);
63
64 mt76_wr(dev, MT_MCU_CMD, val);
65 if (val & MT_MCU_CMD_ERROR_MASK) {
66 dev->reset_state = val;
67 ieee80211_queue_work(mt76_hw(dev), &dev->reset_work);
68 wake_up(&dev->reset_wait);
69 }
70 }
71
72 return IRQ_HANDLED;
73 }
74
75 static int
mt7915_alloc_device(struct pci_dev * pdev,struct mt7915_dev * dev)76 mt7915_alloc_device(struct pci_dev *pdev, struct mt7915_dev *dev)
77 {
78 #define NUM_BANDS 2
79 int i;
80 s8 **sku;
81
82 sku = devm_kzalloc(&pdev->dev, NUM_BANDS * sizeof(*sku), GFP_KERNEL);
83 if (!sku)
84 return -ENOMEM;
85
86 for (i = 0; i < NUM_BANDS; i++) {
87 sku[i] = devm_kzalloc(&pdev->dev, MT7915_SKU_TABLE_SIZE *
88 sizeof(**sku), GFP_KERNEL);
89 if (!sku[i])
90 return -ENOMEM;
91 }
92 dev->rate_power = sku;
93
94 return 0;
95 }
96
mt7915_pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)97 static int mt7915_pci_probe(struct pci_dev *pdev,
98 const struct pci_device_id *id)
99 {
100 static const struct mt76_driver_ops drv_ops = {
101 /* txwi_size = txd size + txp size */
102 .txwi_size = MT_TXD_SIZE + sizeof(struct mt7915_txp),
103 .drv_flags = MT_DRV_TXWI_NO_FREE | MT_DRV_HW_MGMT_TXQ |
104 MT_DRV_AMSDU_OFFLOAD,
105 .survey_flags = SURVEY_INFO_TIME_TX |
106 SURVEY_INFO_TIME_RX |
107 SURVEY_INFO_TIME_BSS_RX,
108 .tx_prepare_skb = mt7915_tx_prepare_skb,
109 .tx_complete_skb = mt7915_tx_complete_skb,
110 .rx_skb = mt7915_queue_rx_skb,
111 .rx_poll_complete = mt7915_rx_poll_complete,
112 .sta_ps = mt7915_sta_ps,
113 .sta_add = mt7915_mac_sta_add,
114 .sta_remove = mt7915_mac_sta_remove,
115 .update_survey = mt7915_update_channel,
116 };
117 struct mt7915_dev *dev;
118 struct mt76_dev *mdev;
119 int ret;
120
121 ret = pcim_enable_device(pdev);
122 if (ret)
123 return ret;
124
125 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
126 if (ret)
127 return ret;
128
129 pci_set_master(pdev);
130
131 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
132 if (ret)
133 return ret;
134
135 mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt7915_ops,
136 &drv_ops);
137 if (!mdev)
138 return -ENOMEM;
139
140 dev = container_of(mdev, struct mt7915_dev, mt76);
141 ret = mt7915_alloc_device(pdev, dev);
142 if (ret)
143 goto error;
144
145 mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
146 mdev->rev = (mt7915_l1_rr(dev, MT_HW_CHIPID) << 16) |
147 (mt7915_l1_rr(dev, MT_HW_REV) & 0xff);
148 dev_dbg(mdev->dev, "ASIC revision: %04x\n", mdev->rev);
149
150 mt76_wr(dev, MT_INT_MASK_CSR, 0);
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 mt76_free_device(&dev->mt76);
167
168 return ret;
169 }
170
mt7915_pci_remove(struct pci_dev * pdev)171 static void mt7915_pci_remove(struct pci_dev *pdev)
172 {
173 struct mt76_dev *mdev = pci_get_drvdata(pdev);
174 struct mt7915_dev *dev = container_of(mdev, struct mt7915_dev, mt76);
175
176 mt7915_unregister_device(dev);
177 }
178
179 struct pci_driver mt7915_pci_driver = {
180 .name = KBUILD_MODNAME,
181 .id_table = mt7915_pci_device_table,
182 .probe = mt7915_pci_probe,
183 .remove = mt7915_pci_remove,
184 };
185
186 module_pci_driver(mt7915_pci_driver);
187
188 MODULE_DEVICE_TABLE(pci, mt7915_pci_device_table);
189 MODULE_FIRMWARE(MT7915_FIRMWARE_WA);
190 MODULE_FIRMWARE(MT7915_FIRMWARE_WM);
191 MODULE_FIRMWARE(MT7915_ROM_PATCH);
192 MODULE_LICENSE("Dual BSD/GPL");
193