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
mt76pci_probe(struct pci_dev * pdev,const struct pci_device_id * id)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
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, mt76x2_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
mt76pci_remove(struct pci_dev * pdev)88 mt76pci_remove(struct pci_dev *pdev)
89 {
90 struct mt76_dev *mdev = pci_get_drvdata(pdev);
91 struct mt76x2_dev *dev = container_of(mdev, struct mt76x2_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