developer | b11a539 | 2022-03-31 00:34:47 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: ISC |
| 2 | /* Copyright (C) 2020 MediaTek Inc. |
| 3 | * |
| 4 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/kernel.h> |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/pci.h> |
| 10 | |
| 11 | #include "bersa.h" |
| 12 | #include "mac.h" |
| 13 | #include "../trace.h" |
| 14 | |
| 15 | static LIST_HEAD(hif_list); |
| 16 | static DEFINE_SPINLOCK(hif_lock); |
| 17 | static u32 hif_idx; |
| 18 | |
| 19 | static const struct pci_device_id bersa_pci_device_table[] = { |
| 20 | { PCI_DEVICE(PCI_VENDOR_ID_MEDIATEK, 0x7902) }, //bellwether |
| 21 | { }, |
| 22 | }; |
| 23 | |
| 24 | static const struct pci_device_id bersa_hif_device_table[] = { |
| 25 | { }, |
| 26 | }; |
| 27 | |
| 28 | static struct bersa_hif *bersa_pci_get_hif2(u32 idx) |
| 29 | { |
| 30 | struct bersa_hif *hif; |
| 31 | u32 val; |
| 32 | |
| 33 | spin_lock_bh(&hif_lock); |
| 34 | |
| 35 | list_for_each_entry(hif, &hif_list, list) { |
| 36 | val = readl(hif->regs + MT_PCIE_RECOG_ID); |
| 37 | val &= MT_PCIE_RECOG_ID_MASK; |
| 38 | if (val != idx) |
| 39 | continue; |
| 40 | |
| 41 | get_device(hif->dev); |
| 42 | goto out; |
| 43 | } |
| 44 | hif = NULL; |
| 45 | |
| 46 | out: |
| 47 | spin_unlock_bh(&hif_lock); |
| 48 | |
| 49 | return hif; |
| 50 | } |
| 51 | |
| 52 | static void bersa_put_hif2(struct bersa_hif *hif) |
| 53 | { |
| 54 | if (!hif) |
| 55 | return; |
| 56 | |
| 57 | put_device(hif->dev); |
| 58 | } |
| 59 | |
| 60 | static struct bersa_hif *bersa_pci_init_hif2(struct pci_dev *pdev) |
| 61 | { |
| 62 | hif_idx++; |
| 63 | if (!pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x7916, NULL) && |
| 64 | !pci_get_device(PCI_VENDOR_ID_MEDIATEK, 0x790a, NULL)) |
| 65 | return NULL; |
| 66 | |
| 67 | writel(hif_idx | MT_PCIE_RECOG_ID_SEM, |
| 68 | pcim_iomap_table(pdev)[0] + MT_PCIE_RECOG_ID); |
| 69 | |
| 70 | return bersa_pci_get_hif2(hif_idx); |
| 71 | } |
| 72 | |
| 73 | static int bersa_pci_hif2_probe(struct pci_dev *pdev) |
| 74 | { |
| 75 | struct bersa_hif *hif; |
| 76 | |
| 77 | hif = devm_kzalloc(&pdev->dev, sizeof(*hif), GFP_KERNEL); |
| 78 | if (!hif) |
| 79 | return -ENOMEM; |
| 80 | |
| 81 | hif->dev = &pdev->dev; |
| 82 | hif->regs = pcim_iomap_table(pdev)[0]; |
| 83 | hif->irq = pdev->irq; |
| 84 | spin_lock_bh(&hif_lock); |
| 85 | list_add(&hif->list, &hif_list); |
| 86 | spin_unlock_bh(&hif_lock); |
| 87 | pci_set_drvdata(pdev, hif); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int bersa_pci_probe(struct pci_dev *pdev, |
| 93 | const struct pci_device_id *id) |
| 94 | { |
| 95 | struct bersa_dev *dev; |
| 96 | struct mt76_dev *mdev; |
| 97 | struct bersa_hif *hif2; |
| 98 | int irq; |
| 99 | int ret; |
| 100 | |
| 101 | ret = pcim_enable_device(pdev); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); |
| 106 | if (ret) |
| 107 | return ret; |
| 108 | |
| 109 | pci_set_master(pdev); |
| 110 | |
| 111 | ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32)); |
| 112 | if (ret) |
| 113 | return ret; |
| 114 | |
| 115 | mt76_pci_disable_aspm(pdev); |
| 116 | |
| 117 | if (id->device == 0x790a) |
| 118 | return bersa_pci_hif2_probe(pdev); |
| 119 | |
| 120 | dev = bersa_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0], |
| 121 | id->device); |
| 122 | if (IS_ERR(dev)) |
| 123 | return PTR_ERR(dev); |
| 124 | |
| 125 | mdev = &dev->mt76; |
| 126 | hif2 = bersa_pci_init_hif2(pdev); |
| 127 | |
| 128 | ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); |
| 129 | if (ret < 0) |
| 130 | goto free_device; |
| 131 | |
| 132 | irq = pdev->irq; |
| 133 | ret = devm_request_irq(mdev->dev, irq, bersa_irq_handler, |
| 134 | IRQF_SHARED, KBUILD_MODNAME, dev); |
| 135 | if (ret) |
| 136 | goto free_irq_vector; |
| 137 | |
| 138 | mt76_wr(dev, MT_INT_MASK_CSR, 0); |
| 139 | |
| 140 | /* master switch of PCIe tnterrupt enable */ |
| 141 | mt76_wr(dev, MT_PCIE_MAC_INT_ENABLE, 0xff); |
| 142 | |
| 143 | if (hif2) { |
| 144 | dev->hif2 = hif2; |
| 145 | |
| 146 | mt76_wr(dev, MT_INT1_MASK_CSR, 0); |
| 147 | /* master switch of PCIe tnterrupt enable */ |
| 148 | mt76_wr(dev, MT_PCIE1_MAC_INT_ENABLE, 0xff); |
| 149 | |
| 150 | ret = devm_request_irq(mdev->dev, dev->hif2->irq, |
| 151 | bersa_irq_handler, IRQF_SHARED, |
| 152 | KBUILD_MODNAME "-hif", dev); |
| 153 | if (ret) |
| 154 | goto free_hif2; |
| 155 | } |
| 156 | |
| 157 | ret = bersa_register_device(dev); |
| 158 | if (ret) |
| 159 | goto free_hif2_irq; |
| 160 | |
| 161 | return 0; |
| 162 | |
| 163 | free_hif2_irq: |
| 164 | if (dev->hif2) |
| 165 | devm_free_irq(mdev->dev, dev->hif2->irq, dev); |
| 166 | free_hif2: |
| 167 | if (dev->hif2) |
| 168 | put_device(dev->hif2->dev); |
| 169 | devm_free_irq(mdev->dev, irq, dev); |
| 170 | free_irq_vector: |
| 171 | pci_free_irq_vectors(pdev); |
| 172 | free_device: |
| 173 | mt76_free_device(&dev->mt76); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | static void bersa_hif_remove(struct pci_dev *pdev) |
| 179 | { |
| 180 | struct bersa_hif *hif = pci_get_drvdata(pdev); |
| 181 | |
| 182 | list_del(&hif->list); |
| 183 | } |
| 184 | |
| 185 | static void bersa_pci_remove(struct pci_dev *pdev) |
| 186 | { |
| 187 | struct mt76_dev *mdev; |
| 188 | struct bersa_dev *dev; |
| 189 | |
| 190 | mdev = pci_get_drvdata(pdev); |
| 191 | dev = container_of(mdev, struct bersa_dev, mt76); |
| 192 | bersa_put_hif2(dev->hif2); |
| 193 | bersa_unregister_device(dev); |
| 194 | } |
| 195 | |
| 196 | struct pci_driver bersa_hif_driver = { |
| 197 | .name = KBUILD_MODNAME "_hif", |
| 198 | .id_table = bersa_hif_device_table, |
| 199 | .probe = bersa_pci_probe, |
| 200 | .remove = bersa_hif_remove, |
| 201 | }; |
| 202 | |
| 203 | struct pci_driver bersa_pci_driver = { |
| 204 | .name = KBUILD_MODNAME, |
| 205 | .id_table = bersa_pci_device_table, |
| 206 | .probe = bersa_pci_probe, |
| 207 | .remove = bersa_pci_remove, |
| 208 | }; |
| 209 | |
| 210 | MODULE_DEVICE_TABLE(pci, bersa_pci_device_table); |
| 211 | MODULE_DEVICE_TABLE(pci, bersa_hif_device_table); |
| 212 | MODULE_FIRMWARE(MT7902_FIRMWARE_WA); |
| 213 | MODULE_FIRMWARE(MT7902_FIRMWARE_WM); |
| 214 | MODULE_FIRMWARE(MT7902_ROM_PATCH); |
| 215 | MODULE_FIRMWARE(MT7902_FIRMWARE_ROM); |
| 216 | MODULE_FIRMWARE(MT7902_FIRMWARE_ROM_SRAM); |