xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/pci.c (revision 970be1dff26dc387591688a08e4c21ebd13c3fc2)
10e3d6777SRyder Lee // SPDX-License-Identifier: ISC
21613c621SLorenzo Bianconi /*
31613c621SLorenzo Bianconi  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
41613c621SLorenzo Bianconi  */
51613c621SLorenzo Bianconi 
61613c621SLorenzo Bianconi #include <linux/kernel.h>
71613c621SLorenzo Bianconi #include <linux/module.h>
81613c621SLorenzo Bianconi #include <linux/pci.h>
91613c621SLorenzo Bianconi 
101613c621SLorenzo Bianconi #include "mt76x2.h"
111613c621SLorenzo Bianconi 
126ed942afSLorenzo Bianconi static const struct pci_device_id mt76x2e_device_table[] = {
137715a1d5SRyder Lee 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7662) },
147715a1d5SRyder Lee 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7612) },
157715a1d5SRyder Lee 	{ PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7602) },
161613c621SLorenzo Bianconi 	{ },
171613c621SLorenzo Bianconi };
181613c621SLorenzo Bianconi 
191613c621SLorenzo Bianconi static int
206ed942afSLorenzo Bianconi mt76x2e_probe(struct pci_dev *pdev, const struct pci_device_id *id)
211613c621SLorenzo Bianconi {
22ecd25b54SLorenzo Bianconi 	static const struct mt76_driver_ops drv_ops = {
23ecd25b54SLorenzo Bianconi 		.txwi_size = sizeof(struct mt76x02_txwi),
245ce09c1aSFelix Fietkau 		.drv_flags = MT_DRV_TX_ALIGNED4_SKBS |
255ce09c1aSFelix Fietkau 			     MT_DRV_SW_RX_AIRTIME,
26355f8d00SFelix Fietkau 		.survey_flags = SURVEY_INFO_TIME_TX,
27ecd25b54SLorenzo Bianconi 		.update_survey = mt76x02_update_channel,
28ecd25b54SLorenzo Bianconi 		.tx_prepare_skb = mt76x02_tx_prepare_skb,
29ecd25b54SLorenzo Bianconi 		.tx_complete_skb = mt76x02_tx_complete_skb,
30ecd25b54SLorenzo Bianconi 		.rx_skb = mt76x02_queue_rx_skb,
31ecd25b54SLorenzo Bianconi 		.rx_poll_complete = mt76x02_rx_poll_complete,
32ecd25b54SLorenzo Bianconi 		.sta_ps = mt76x02_sta_ps,
33ecd25b54SLorenzo Bianconi 		.sta_add = mt76x02_sta_add,
34ecd25b54SLorenzo Bianconi 		.sta_remove = mt76x02_sta_remove,
35ecd25b54SLorenzo Bianconi 	};
36e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev;
37ecd25b54SLorenzo Bianconi 	struct mt76_dev *mdev;
381613c621SLorenzo Bianconi 	int ret;
391613c621SLorenzo Bianconi 
401613c621SLorenzo Bianconi 	ret = pcim_enable_device(pdev);
411613c621SLorenzo Bianconi 	if (ret)
421613c621SLorenzo Bianconi 		return ret;
431613c621SLorenzo Bianconi 
441613c621SLorenzo Bianconi 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
451613c621SLorenzo Bianconi 	if (ret)
461613c621SLorenzo Bianconi 		return ret;
471613c621SLorenzo Bianconi 
481613c621SLorenzo Bianconi 	pci_set_master(pdev);
491613c621SLorenzo Bianconi 
50a2e75961SChristophe JAILLET 	ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
511613c621SLorenzo Bianconi 	if (ret)
521613c621SLorenzo Bianconi 		return ret;
531613c621SLorenzo Bianconi 
54ecd25b54SLorenzo Bianconi 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x2_ops,
55ecd25b54SLorenzo Bianconi 				 &drv_ops);
56ecd25b54SLorenzo Bianconi 	if (!mdev)
571613c621SLorenzo Bianconi 		return -ENOMEM;
581613c621SLorenzo Bianconi 
59ecd25b54SLorenzo Bianconi 	dev = container_of(mdev, struct mt76x02_dev, mt76);
60ecd25b54SLorenzo Bianconi 	mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
611613c621SLorenzo Bianconi 	mt76x2_reset_wlan(dev, false);
621613c621SLorenzo Bianconi 
63ecd25b54SLorenzo Bianconi 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
64ecd25b54SLorenzo Bianconi 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
651613c621SLorenzo Bianconi 
669df8c539SFelix Fietkau 	mt76_wr(dev, MT_INT_MASK_CSR, 0);
679df8c539SFelix Fietkau 
68ecd25b54SLorenzo Bianconi 	ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
691613c621SLorenzo Bianconi 			       IRQF_SHARED, KBUILD_MODNAME, dev);
701613c621SLorenzo Bianconi 	if (ret)
711613c621SLorenzo Bianconi 		goto error;
721613c621SLorenzo Bianconi 
731613c621SLorenzo Bianconi 	ret = mt76x2_register_device(dev);
741613c621SLorenzo Bianconi 	if (ret)
751613c621SLorenzo Bianconi 		goto error;
761613c621SLorenzo Bianconi 
771613c621SLorenzo Bianconi 	/* Fix up ASPM configuration */
781613c621SLorenzo Bianconi 
791613c621SLorenzo Bianconi 	/* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
801613c621SLorenzo Bianconi 	mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
811613c621SLorenzo Bianconi 
821613c621SLorenzo Bianconi 	/* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
831613c621SLorenzo Bianconi 	mt76_rmw_field(dev, 0x15a0c, 0xf << 28, 0xf);
841613c621SLorenzo Bianconi 
851613c621SLorenzo Bianconi 	/* RG_SSUSB_CDR_BR_PE1D = 0x3 */
861613c621SLorenzo Bianconi 	mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
871613c621SLorenzo Bianconi 
88f37f0550SLorenzo Bianconi 	mt76_pci_disable_aspm(pdev);
89f37f0550SLorenzo Bianconi 
901613c621SLorenzo Bianconi 	return 0;
911613c621SLorenzo Bianconi 
921613c621SLorenzo Bianconi error:
93bc348defSLorenzo Bianconi 	mt76_free_device(&dev->mt76);
94bc348defSLorenzo Bianconi 
951613c621SLorenzo Bianconi 	return ret;
961613c621SLorenzo Bianconi }
971613c621SLorenzo Bianconi 
981613c621SLorenzo Bianconi static void
996ed942afSLorenzo Bianconi mt76x2e_remove(struct pci_dev *pdev)
1001613c621SLorenzo Bianconi {
1011613c621SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
102e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
1031613c621SLorenzo Bianconi 
1041613c621SLorenzo Bianconi 	mt76_unregister_device(mdev);
1051613c621SLorenzo Bianconi 	mt76x2_cleanup(dev);
106def34a2fSLorenzo Bianconi 	mt76_free_device(mdev);
1071613c621SLorenzo Bianconi }
1081613c621SLorenzo Bianconi 
10940ba9a93SLorenzo Bianconi static int __maybe_unused
11040ba9a93SLorenzo Bianconi mt76x2e_suspend(struct pci_dev *pdev, pm_message_t state)
11140ba9a93SLorenzo Bianconi {
11240ba9a93SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
11340ba9a93SLorenzo Bianconi 	int i, err;
11440ba9a93SLorenzo Bianconi 
11540ba9a93SLorenzo Bianconi 	napi_disable(&mdev->tx_napi);
11640ba9a93SLorenzo Bianconi 	tasklet_kill(&mdev->pre_tbtt_tasklet);
117781eef5bSFelix Fietkau 	mt76_worker_disable(&mdev->tx_worker);
11840ba9a93SLorenzo Bianconi 
11940ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i)
12040ba9a93SLorenzo Bianconi 		napi_disable(&mdev->napi[i]);
12140ba9a93SLorenzo Bianconi 
12240ba9a93SLorenzo Bianconi 	pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
12340ba9a93SLorenzo Bianconi 	pci_save_state(pdev);
12440ba9a93SLorenzo Bianconi 	err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
12540ba9a93SLorenzo Bianconi 	if (err)
12640ba9a93SLorenzo Bianconi 		goto restore;
12740ba9a93SLorenzo Bianconi 
12840ba9a93SLorenzo Bianconi 	return 0;
12940ba9a93SLorenzo Bianconi 
13040ba9a93SLorenzo Bianconi restore:
13140ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i)
13240ba9a93SLorenzo Bianconi 		napi_enable(&mdev->napi[i]);
13340ba9a93SLorenzo Bianconi 	napi_enable(&mdev->tx_napi);
13440ba9a93SLorenzo Bianconi 
13540ba9a93SLorenzo Bianconi 	return err;
13640ba9a93SLorenzo Bianconi }
13740ba9a93SLorenzo Bianconi 
13840ba9a93SLorenzo Bianconi static int __maybe_unused
13940ba9a93SLorenzo Bianconi mt76x2e_resume(struct pci_dev *pdev)
14040ba9a93SLorenzo Bianconi {
14140ba9a93SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
14240ba9a93SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
14340ba9a93SLorenzo Bianconi 	int i, err;
14440ba9a93SLorenzo Bianconi 
14540ba9a93SLorenzo Bianconi 	err = pci_set_power_state(pdev, PCI_D0);
14640ba9a93SLorenzo Bianconi 	if (err)
14740ba9a93SLorenzo Bianconi 		return err;
14840ba9a93SLorenzo Bianconi 
14940ba9a93SLorenzo Bianconi 	pci_restore_state(pdev);
15040ba9a93SLorenzo Bianconi 
151781eef5bSFelix Fietkau 	mt76_worker_enable(&mdev->tx_worker);
152*970be1dfSFelix Fietkau 
153*970be1dfSFelix Fietkau 	local_bh_disable();
15440ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i) {
15540ba9a93SLorenzo Bianconi 		napi_enable(&mdev->napi[i]);
15640ba9a93SLorenzo Bianconi 		napi_schedule(&mdev->napi[i]);
15740ba9a93SLorenzo Bianconi 	}
15840ba9a93SLorenzo Bianconi 	napi_enable(&mdev->tx_napi);
15940ba9a93SLorenzo Bianconi 	napi_schedule(&mdev->tx_napi);
160*970be1dfSFelix Fietkau 	local_bh_enable();
16140ba9a93SLorenzo Bianconi 
16240ba9a93SLorenzo Bianconi 	return mt76x2_resume_device(dev);
16340ba9a93SLorenzo Bianconi }
16440ba9a93SLorenzo Bianconi 
1656ed942afSLorenzo Bianconi MODULE_DEVICE_TABLE(pci, mt76x2e_device_table);
1661613c621SLorenzo Bianconi MODULE_FIRMWARE(MT7662_FIRMWARE);
1671613c621SLorenzo Bianconi MODULE_FIRMWARE(MT7662_ROM_PATCH);
1681613c621SLorenzo Bianconi MODULE_LICENSE("Dual BSD/GPL");
1691613c621SLorenzo Bianconi 
1701613c621SLorenzo Bianconi static struct pci_driver mt76pci_driver = {
1711613c621SLorenzo Bianconi 	.name		= KBUILD_MODNAME,
1726ed942afSLorenzo Bianconi 	.id_table	= mt76x2e_device_table,
1736ed942afSLorenzo Bianconi 	.probe		= mt76x2e_probe,
1746ed942afSLorenzo Bianconi 	.remove		= mt76x2e_remove,
17540ba9a93SLorenzo Bianconi #ifdef CONFIG_PM
17640ba9a93SLorenzo Bianconi 	.suspend	= mt76x2e_suspend,
17740ba9a93SLorenzo Bianconi 	.resume		= mt76x2e_resume,
17840ba9a93SLorenzo Bianconi #endif /* CONFIG_PM */
1791613c621SLorenzo Bianconi };
1801613c621SLorenzo Bianconi 
1811613c621SLorenzo Bianconi module_pci_driver(mt76pci_driver);
182