xref: /freebsd/sys/contrib/dev/mediatek/mt76/mt76x2/pci.c (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
4  */
5 
6 #include <linux/kernel.h>
7 #include <linux/module.h>
8 #include <linux/pci.h>
9 
10 #include "mt76x2.h"
11 
12 static const struct pci_device_id mt76x2e_device_table[] = {
13 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7662) },
14 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7612) },
15 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7602) },
16 	{ },
17 };
18 
19 static int
20 mt76x2e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
21 {
22 	static const struct mt76_driver_ops drv_ops = {
23 		.txwi_size = sizeof(struct mt76x02_txwi),
24 		.drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
25 			     MT_DRV_SW_RX_AIRTIME,
26 		.survey_flags = SURVEY_INFO_TIME_TX,
27 		.update_survey = mt76x02_update_channel,
28 		.tx_prepare_skb = mt76x02_tx_prepare_skb,
29 		.tx_complete_skb = mt76x02_tx_complete_skb,
30 		.rx_skb = mt76x02_queue_rx_skb,
31 		.rx_poll_complete = mt76x02_rx_poll_complete,
32 		.sta_ps = mt76x02_sta_ps,
33 		.sta_add = mt76x02_sta_add,
34 		.sta_remove = mt76x02_sta_remove,
35 	};
36 	struct mt76x02_dev *dev;
37 	struct mt76_dev *mdev;
38 	int ret;
39 
40 	ret = pcim_enable_device(pdev);
41 	if (ret)
42 		return ret;
43 
44 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
45 	if (ret)
46 		return ret;
47 
48 	pci_set_master(pdev);
49 
50 	ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
51 	if (ret)
52 		return ret;
53 
54 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x2_ops,
55 				 &drv_ops);
56 	if (!mdev)
57 		return -ENOMEM;
58 
59 	dev = container_of(mdev, struct mt76x02_dev, mt76);
60 	mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
61 	mt76x2_reset_wlan(dev, false);
62 
63 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
64 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
65 
66 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
67 
68 	ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
69 			       IRQF_SHARED, KBUILD_MODNAME, dev);
70 	if (ret)
71 		goto error;
72 
73 	ret = mt76x2_register_device(dev);
74 	if (ret)
75 		goto error;
76 
77 	/* Fix up ASPM configuration */
78 
79 	/* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
80 	mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
81 
82 	/* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
83 	mt76_rmw_field(dev, 0x15a0c, 0xfU << 28, 0xf);
84 
85 	/* RG_SSUSB_CDR_BR_PE1D = 0x3 */
86 	mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
87 
88 	mt76_pci_disable_aspm(pdev);
89 
90 	return 0;
91 
92 error:
93 	mt76_free_device(&dev->mt76);
94 
95 	return ret;
96 }
97 
98 static void
99 mt76x2e_remove(struct pci_dev *pdev)
100 {
101 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
102 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
103 
104 	mt76_unregister_device(mdev);
105 	mt76x2_cleanup(dev);
106 	mt76_free_device(mdev);
107 }
108 
109 static int __maybe_unused
110 mt76x2e_suspend(struct pci_dev *pdev, pm_message_t state)
111 {
112 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
113 	int i, err;
114 
115 	napi_disable(&mdev->tx_napi);
116 	tasklet_kill(&mdev->pre_tbtt_tasklet);
117 	mt76_worker_disable(&mdev->tx_worker);
118 
119 	mt76_for_each_q_rx(mdev, i)
120 		napi_disable(&mdev->napi[i]);
121 
122 	pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
123 	pci_save_state(pdev);
124 	err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
125 	if (err)
126 		goto restore;
127 
128 	return 0;
129 
130 restore:
131 	mt76_for_each_q_rx(mdev, i)
132 		napi_enable(&mdev->napi[i]);
133 	napi_enable(&mdev->tx_napi);
134 
135 	return err;
136 }
137 
138 static int __maybe_unused
139 mt76x2e_resume(struct pci_dev *pdev)
140 {
141 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
142 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
143 	int i, err;
144 
145 	err = pci_set_power_state(pdev, PCI_D0);
146 	if (err)
147 		return err;
148 
149 	pci_restore_state(pdev);
150 
151 	mt76_worker_enable(&mdev->tx_worker);
152 
153 	local_bh_disable();
154 	mt76_for_each_q_rx(mdev, i) {
155 		napi_enable(&mdev->napi[i]);
156 		napi_schedule(&mdev->napi[i]);
157 	}
158 	napi_enable(&mdev->tx_napi);
159 	napi_schedule(&mdev->tx_napi);
160 	local_bh_enable();
161 
162 	return mt76x2_resume_device(dev);
163 }
164 
165 MODULE_DEVICE_TABLE(pci, mt76x2e_device_table);
166 MODULE_FIRMWARE(MT7662_FIRMWARE);
167 MODULE_FIRMWARE(MT7662_ROM_PATCH);
168 MODULE_LICENSE("Dual BSD/GPL");
169 
170 static struct pci_driver mt76pci_driver = {
171 	.name		= KBUILD_MODNAME,
172 	.id_table	= mt76x2e_device_table,
173 	.probe		= mt76x2e_probe,
174 	.remove		= mt76x2e_remove,
175 #ifdef CONFIG_PM
176 	.suspend	= mt76x2e_suspend,
177 	.resume		= mt76x2e_resume,
178 #endif /* CONFIG_PM */
179 };
180 
181 module_pci_driver(mt76pci_driver);
182