xref: /linux/drivers/net/wireless/mediatek/mt76/mt76x2/pci.c (revision 4ab5a5d2a4a2289c2af07accbec7170ca5671f41)
1 /*
2  * Copyright (C) 2016 Felix Fietkau <nbd@nbd.name>
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/pci.h>
20 
21 #include "mt76x2.h"
22 
23 static const struct pci_device_id mt76pci_device_table[] = {
24 	{ PCI_DEVICE(0x14c3, 0x7662) },
25 	{ PCI_DEVICE(0x14c3, 0x7612) },
26 	{ PCI_DEVICE(0x14c3, 0x7602) },
27 	{ },
28 };
29 
30 static int
31 mt76pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
32 {
33 	struct mt76x02_dev *dev;
34 	int ret;
35 
36 	ret = pcim_enable_device(pdev);
37 	if (ret)
38 		return ret;
39 
40 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
41 	if (ret)
42 		return ret;
43 
44 	pci_set_master(pdev);
45 
46 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
47 	if (ret)
48 		return ret;
49 
50 	dev = mt76x2_alloc_device(&pdev->dev);
51 	if (!dev)
52 		return -ENOMEM;
53 
54 	mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
55 	mt76x2_reset_wlan(dev, false);
56 
57 	dev->mt76.rev = mt76_rr(dev, MT_ASIC_VERSION);
58 	dev_info(dev->mt76.dev, "ASIC revision: %08x\n", dev->mt76.rev);
59 
60 	ret = devm_request_irq(dev->mt76.dev, pdev->irq, mt76x02_irq_handler,
61 			       IRQF_SHARED, KBUILD_MODNAME, dev);
62 	if (ret)
63 		goto error;
64 
65 	ret = mt76x2_register_device(dev);
66 	if (ret)
67 		goto error;
68 
69 	/* Fix up ASPM configuration */
70 
71 	/* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
72 	mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
73 
74 	/* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
75 	mt76_rmw_field(dev, 0x15a0c, 0xf << 28, 0xf);
76 
77 	/* RG_SSUSB_CDR_BR_PE1D = 0x3 */
78 	mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
79 
80 	return 0;
81 
82 error:
83 	ieee80211_free_hw(mt76_hw(dev));
84 	return ret;
85 }
86 
87 static void
88 mt76pci_remove(struct pci_dev *pdev)
89 {
90 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
91 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
92 
93 	mt76_unregister_device(mdev);
94 	mt76x2_cleanup(dev);
95 	ieee80211_free_hw(mdev->hw);
96 }
97 
98 MODULE_DEVICE_TABLE(pci, mt76pci_device_table);
99 MODULE_FIRMWARE(MT7662_FIRMWARE);
100 MODULE_FIRMWARE(MT7662_ROM_PATCH);
101 MODULE_LICENSE("Dual BSD/GPL");
102 
103 static struct pci_driver mt76pci_driver = {
104 	.name		= KBUILD_MODNAME,
105 	.id_table	= mt76pci_device_table,
106 	.probe		= mt76pci_probe,
107 	.remove		= mt76pci_remove,
108 };
109 
110 module_pci_driver(mt76pci_driver);
111