xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/pci.c (revision 781eef5b34c57d9d8c772e2402d07086722e89e4)
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[] = {
131613c621SLorenzo Bianconi 	{ PCI_DEVICE(0x14c3, 0x7662) },
141613c621SLorenzo Bianconi 	{ PCI_DEVICE(0x14c3, 0x7612) },
151613c621SLorenzo Bianconi 	{ PCI_DEVICE(0x14c3, 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 
501613c621SLorenzo Bianconi 	ret = pci_set_dma_mask(pdev, 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:
931613c621SLorenzo Bianconi 	ieee80211_free_hw(mt76_hw(dev));
941613c621SLorenzo Bianconi 	return ret;
951613c621SLorenzo Bianconi }
961613c621SLorenzo Bianconi 
971613c621SLorenzo Bianconi static void
986ed942afSLorenzo Bianconi mt76x2e_remove(struct pci_dev *pdev)
991613c621SLorenzo Bianconi {
1001613c621SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
101e40803f2SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
1021613c621SLorenzo Bianconi 
1031613c621SLorenzo Bianconi 	mt76_unregister_device(mdev);
1041613c621SLorenzo Bianconi 	mt76x2_cleanup(dev);
105def34a2fSLorenzo Bianconi 	mt76_free_device(mdev);
1061613c621SLorenzo Bianconi }
1071613c621SLorenzo Bianconi 
10840ba9a93SLorenzo Bianconi static int __maybe_unused
10940ba9a93SLorenzo Bianconi mt76x2e_suspend(struct pci_dev *pdev, pm_message_t state)
11040ba9a93SLorenzo Bianconi {
11140ba9a93SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
11240ba9a93SLorenzo Bianconi 	int i, err;
11340ba9a93SLorenzo Bianconi 
11440ba9a93SLorenzo Bianconi 	napi_disable(&mdev->tx_napi);
11540ba9a93SLorenzo Bianconi 	tasklet_kill(&mdev->pre_tbtt_tasklet);
116*781eef5bSFelix Fietkau 	mt76_worker_disable(&mdev->tx_worker);
11740ba9a93SLorenzo Bianconi 
11840ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i)
11940ba9a93SLorenzo Bianconi 		napi_disable(&mdev->napi[i]);
12040ba9a93SLorenzo Bianconi 
12140ba9a93SLorenzo Bianconi 	pci_enable_wake(pdev, pci_choose_state(pdev, state), true);
12240ba9a93SLorenzo Bianconi 	pci_save_state(pdev);
12340ba9a93SLorenzo Bianconi 	err = pci_set_power_state(pdev, pci_choose_state(pdev, state));
12440ba9a93SLorenzo Bianconi 	if (err)
12540ba9a93SLorenzo Bianconi 		goto restore;
12640ba9a93SLorenzo Bianconi 
12740ba9a93SLorenzo Bianconi 	return 0;
12840ba9a93SLorenzo Bianconi 
12940ba9a93SLorenzo Bianconi restore:
13040ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i)
13140ba9a93SLorenzo Bianconi 		napi_enable(&mdev->napi[i]);
13240ba9a93SLorenzo Bianconi 	napi_enable(&mdev->tx_napi);
13340ba9a93SLorenzo Bianconi 
13440ba9a93SLorenzo Bianconi 	return err;
13540ba9a93SLorenzo Bianconi }
13640ba9a93SLorenzo Bianconi 
13740ba9a93SLorenzo Bianconi static int __maybe_unused
13840ba9a93SLorenzo Bianconi mt76x2e_resume(struct pci_dev *pdev)
13940ba9a93SLorenzo Bianconi {
14040ba9a93SLorenzo Bianconi 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
14140ba9a93SLorenzo Bianconi 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
14240ba9a93SLorenzo Bianconi 	int i, err;
14340ba9a93SLorenzo Bianconi 
14440ba9a93SLorenzo Bianconi 	err = pci_set_power_state(pdev, PCI_D0);
14540ba9a93SLorenzo Bianconi 	if (err)
14640ba9a93SLorenzo Bianconi 		return err;
14740ba9a93SLorenzo Bianconi 
14840ba9a93SLorenzo Bianconi 	pci_restore_state(pdev);
14940ba9a93SLorenzo Bianconi 
150*781eef5bSFelix Fietkau 	mt76_worker_enable(&mdev->tx_worker);
15140ba9a93SLorenzo Bianconi 	mt76_for_each_q_rx(mdev, i) {
15240ba9a93SLorenzo Bianconi 		napi_enable(&mdev->napi[i]);
15340ba9a93SLorenzo Bianconi 		napi_schedule(&mdev->napi[i]);
15440ba9a93SLorenzo Bianconi 	}
15540ba9a93SLorenzo Bianconi 	napi_enable(&mdev->tx_napi);
15640ba9a93SLorenzo Bianconi 	napi_schedule(&mdev->tx_napi);
15740ba9a93SLorenzo Bianconi 
15840ba9a93SLorenzo Bianconi 	return mt76x2_resume_device(dev);
15940ba9a93SLorenzo Bianconi }
16040ba9a93SLorenzo Bianconi 
1616ed942afSLorenzo Bianconi MODULE_DEVICE_TABLE(pci, mt76x2e_device_table);
1621613c621SLorenzo Bianconi MODULE_FIRMWARE(MT7662_FIRMWARE);
1631613c621SLorenzo Bianconi MODULE_FIRMWARE(MT7662_ROM_PATCH);
1641613c621SLorenzo Bianconi MODULE_LICENSE("Dual BSD/GPL");
1651613c621SLorenzo Bianconi 
1661613c621SLorenzo Bianconi static struct pci_driver mt76pci_driver = {
1671613c621SLorenzo Bianconi 	.name		= KBUILD_MODNAME,
1686ed942afSLorenzo Bianconi 	.id_table	= mt76x2e_device_table,
1696ed942afSLorenzo Bianconi 	.probe		= mt76x2e_probe,
1706ed942afSLorenzo Bianconi 	.remove		= mt76x2e_remove,
17140ba9a93SLorenzo Bianconi #ifdef CONFIG_PM
17240ba9a93SLorenzo Bianconi 	.suspend	= mt76x2e_suspend,
17340ba9a93SLorenzo Bianconi 	.resume		= mt76x2e_resume,
17440ba9a93SLorenzo Bianconi #endif /* CONFIG_PM */
1751613c621SLorenzo Bianconi };
1761613c621SLorenzo Bianconi 
1771613c621SLorenzo Bianconi module_pci_driver(mt76pci_driver);
178