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 	static const struct mt76_driver_ops drv_ops = {
34 		.txwi_size = sizeof(struct mt76x02_txwi),
35 		.tx_aligned4_skbs = true,
36 		.update_survey = mt76x02_update_channel,
37 		.tx_prepare_skb = mt76x02_tx_prepare_skb,
38 		.tx_complete_skb = mt76x02_tx_complete_skb,
39 		.rx_skb = mt76x02_queue_rx_skb,
40 		.rx_poll_complete = mt76x02_rx_poll_complete,
41 		.sta_ps = mt76x02_sta_ps,
42 		.sta_add = mt76x02_sta_add,
43 		.sta_remove = mt76x02_sta_remove,
44 	};
45 	struct mt76x02_dev *dev;
46 	struct mt76_dev *mdev;
47 	int ret;
48 
49 	ret = pcim_enable_device(pdev);
50 	if (ret)
51 		return ret;
52 
53 	ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
54 	if (ret)
55 		return ret;
56 
57 	pci_set_master(pdev);
58 
59 	ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
60 	if (ret)
61 		return ret;
62 
63 	mdev = mt76_alloc_device(&pdev->dev, sizeof(*dev), &mt76x2_ops,
64 				 &drv_ops);
65 	if (!mdev)
66 		return -ENOMEM;
67 
68 	dev = container_of(mdev, struct mt76x02_dev, mt76);
69 	mt76_mmio_init(mdev, pcim_iomap_table(pdev)[0]);
70 	mt76x2_reset_wlan(dev, false);
71 
72 	mdev->rev = mt76_rr(dev, MT_ASIC_VERSION);
73 	dev_info(mdev->dev, "ASIC revision: %08x\n", mdev->rev);
74 
75 	ret = devm_request_irq(mdev->dev, pdev->irq, mt76x02_irq_handler,
76 			       IRQF_SHARED, KBUILD_MODNAME, dev);
77 	if (ret)
78 		goto error;
79 
80 	ret = mt76x2_register_device(dev);
81 	if (ret)
82 		goto error;
83 
84 	/* Fix up ASPM configuration */
85 
86 	/* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
87 	mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
88 
89 	/* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
90 	mt76_rmw_field(dev, 0x15a0c, 0xf << 28, 0xf);
91 
92 	/* RG_SSUSB_CDR_BR_PE1D = 0x3 */
93 	mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
94 
95 	return 0;
96 
97 error:
98 	ieee80211_free_hw(mt76_hw(dev));
99 	return ret;
100 }
101 
102 static void
103 mt76pci_remove(struct pci_dev *pdev)
104 {
105 	struct mt76_dev *mdev = pci_get_drvdata(pdev);
106 	struct mt76x02_dev *dev = container_of(mdev, struct mt76x02_dev, mt76);
107 
108 	mt76_unregister_device(mdev);
109 	mt76x2_cleanup(dev);
110 	mt76_free_device(mdev);
111 }
112 
113 MODULE_DEVICE_TABLE(pci, mt76pci_device_table);
114 MODULE_FIRMWARE(MT7662_FIRMWARE);
115 MODULE_FIRMWARE(MT7662_ROM_PATCH);
116 MODULE_LICENSE("Dual BSD/GPL");
117 
118 static struct pci_driver mt76pci_driver = {
119 	.name		= KBUILD_MODNAME,
120 	.id_table	= mt76pci_device_table,
121 	.probe		= mt76pci_probe,
122 	.remove		= mt76pci_remove,
123 };
124 
125 module_pci_driver(mt76pci_driver);
126