GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / net / wireless / mediatek / mt76 / mt76x2_pci.c
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 #include "mt76x2_trace.h"
23
24 static const struct pci_device_id mt76pci_device_table[] = {
25         { PCI_DEVICE(0x14c3, 0x7662) },
26         { PCI_DEVICE(0x14c3, 0x7612) },
27         { PCI_DEVICE(0x14c3, 0x7602) },
28         { },
29 };
30
31 static int
32 mt76pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
33 {
34         struct mt76x2_dev *dev;
35         int ret;
36
37         ret = pcim_enable_device(pdev);
38         if (ret)
39                 return ret;
40
41         ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
42         if (ret)
43                 return ret;
44
45         pci_set_master(pdev);
46
47         ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
48         if (ret)
49                 return ret;
50
51         dev = mt76x2_alloc_device(&pdev->dev);
52         if (!dev)
53                 return -ENOMEM;
54
55         mt76_mmio_init(&dev->mt76, pcim_iomap_table(pdev)[0]);
56         mt76x2_reset_wlan(dev, false);
57
58         dev->mt76.rev = mt76_rr(dev, MT_ASIC_VERSION);
59         dev_info(dev->mt76.dev, "ASIC revision: %08x\n", dev->mt76.rev);
60
61         ret = devm_request_irq(dev->mt76.dev, pdev->irq, mt76x2_irq_handler,
62                                IRQF_SHARED, KBUILD_MODNAME, dev);
63         if (ret)
64                 goto error;
65
66         ret = mt76x2_register_device(dev);
67         if (ret)
68                 goto error;
69
70         /* Fix up ASPM configuration */
71
72         /* RG_SSUSB_G1_CDR_BIR_LTR = 0x9 */
73         mt76_rmw_field(dev, 0x15a10, 0x1f << 16, 0x9);
74
75         /* RG_SSUSB_G1_CDR_BIC_LTR = 0xf */
76         mt76_rmw_field(dev, 0x15a0c, 0xfU << 28, 0xf);
77
78         /* RG_SSUSB_CDR_BR_PE1D = 0x3 */
79         mt76_rmw_field(dev, 0x15c58, 0x3 << 6, 0x3);
80
81         return 0;
82
83 error:
84         ieee80211_free_hw(mt76_hw(dev));
85         return ret;
86 }
87
88 static void
89 mt76pci_remove(struct pci_dev *pdev)
90 {
91         struct mt76_dev *mdev = pci_get_drvdata(pdev);
92         struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_dev, mt76);
93
94         mt76_unregister_device(mdev);
95         mt76x2_cleanup(dev);
96         ieee80211_free_hw(mdev->hw);
97 }
98
99 MODULE_DEVICE_TABLE(pci, mt76pci_device_table);
100 /*(DEBLOBBED)*/
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);