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