1 // SPDX-License-Identifier: ISC 2 /* Copyright (C) 2019 MediaTek Inc. 3 * 4 * Author: Ryder Lee <ryder.lee@mediatek.com> 5 * Felix Fietkau <nbd@nbd.name> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 12 #include "mt7615.h" 13 14 static const struct pci_device_id mt7615_pci_device_table[] = { 15 { PCI_DEVICE(0x14c3, 0x7615) }, 16 { PCI_DEVICE(0x14c3, 0x7663) }, 17 { PCI_DEVICE(0x14c3, 0x7611) }, 18 { }, 19 }; 20 21 static int mt7615_pci_probe(struct pci_dev *pdev, 22 const struct pci_device_id *id) 23 { 24 const u32 *map; 25 int ret; 26 27 ret = pcim_enable_device(pdev); 28 if (ret) 29 return ret; 30 31 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); 32 if (ret) 33 return ret; 34 35 pci_set_master(pdev); 36 37 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_ALL_TYPES); 38 if (ret < 0) 39 return ret; 40 41 ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); 42 if (ret) 43 goto error; 44 45 mt76_pci_disable_aspm(pdev); 46 47 map = id->device == 0x7663 ? mt7663e_reg_map : mt7615e_reg_map; 48 ret = mt7615_mmio_probe(&pdev->dev, pcim_iomap_table(pdev)[0], 49 pdev->irq, map); 50 if (ret) 51 goto error; 52 53 return 0; 54 error: 55 pci_free_irq_vectors(pdev); 56 57 return ret; 58 } 59 60 static void mt7615_pci_remove(struct pci_dev *pdev) 61 { 62 struct mt76_dev *mdev = pci_get_drvdata(pdev); 63 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 64 65 mt7615_unregister_device(dev); 66 devm_free_irq(&pdev->dev, pdev->irq, dev); 67 pci_free_irq_vectors(pdev); 68 } 69 70 #ifdef CONFIG_PM 71 static int mt7615_pci_suspend(struct pci_dev *pdev, pm_message_t state) 72 { 73 struct mt76_dev *mdev = pci_get_drvdata(pdev); 74 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 75 bool hif_suspend; 76 int i, err; 77 78 err = mt7615_pm_wake(dev); 79 if (err < 0) 80 return err; 81 82 hif_suspend = !test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 83 mt7615_firmware_offload(dev); 84 if (hif_suspend) { 85 err = mt7615_mcu_set_hif_suspend(dev, true); 86 if (err) 87 return err; 88 } 89 90 napi_disable(&mdev->tx_napi); 91 tasklet_kill(&mdev->tx_tasklet); 92 93 mt76_for_each_q_rx(mdev, i) { 94 napi_disable(&mdev->napi[i]); 95 } 96 tasklet_kill(&dev->irq_tasklet); 97 98 mt7615_dma_reset(dev); 99 100 err = mt7615_wait_pdma_busy(dev); 101 if (err) 102 goto restore; 103 104 if (is_mt7663(mdev)) { 105 mt76_set(dev, MT_PDMA_SLP_PROT, MT_PDMA_AXI_SLPPROT_ENABLE); 106 if (!mt76_poll_msec(dev, MT_PDMA_SLP_PROT, 107 MT_PDMA_AXI_SLPPROT_RDY, 108 MT_PDMA_AXI_SLPPROT_RDY, 1000)) { 109 dev_err(mdev->dev, "PDMA sleep protection failed\n"); 110 err = -EIO; 111 goto restore; 112 } 113 } 114 115 pci_enable_wake(pdev, pci_choose_state(pdev, state), true); 116 pci_save_state(pdev); 117 err = pci_set_power_state(pdev, pci_choose_state(pdev, state)); 118 if (err) 119 goto restore; 120 121 err = mt7615_firmware_own(dev); 122 if (err) 123 goto restore; 124 125 return 0; 126 127 restore: 128 mt76_for_each_q_rx(mdev, i) { 129 napi_enable(&mdev->napi[i]); 130 } 131 napi_enable(&mdev->tx_napi); 132 if (hif_suspend) 133 mt7615_mcu_set_hif_suspend(dev, false); 134 135 return err; 136 } 137 138 static int mt7615_pci_resume(struct pci_dev *pdev) 139 { 140 struct mt76_dev *mdev = pci_get_drvdata(pdev); 141 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 142 bool pdma_reset; 143 int i, err; 144 145 err = mt7615_driver_own(dev); 146 if (err < 0) 147 return err; 148 149 err = pci_set_power_state(pdev, PCI_D0); 150 if (err) 151 return err; 152 153 pci_restore_state(pdev); 154 155 if (is_mt7663(&dev->mt76)) { 156 mt76_clear(dev, MT_PDMA_SLP_PROT, MT_PDMA_AXI_SLPPROT_ENABLE); 157 mt76_wr(dev, MT_PCIE_IRQ_ENABLE, 1); 158 } 159 160 pdma_reset = !mt76_rr(dev, MT_WPDMA_TX_RING0_CTRL0) && 161 !mt76_rr(dev, MT_WPDMA_TX_RING0_CTRL1); 162 if (pdma_reset) 163 dev_err(mdev->dev, "PDMA engine must be reinitialized\n"); 164 165 mt76_for_each_q_rx(mdev, i) { 166 napi_enable(&mdev->napi[i]); 167 napi_schedule(&mdev->napi[i]); 168 } 169 napi_enable(&mdev->tx_napi); 170 napi_schedule(&mdev->tx_napi); 171 172 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 173 mt7615_firmware_offload(dev)) 174 err = mt7615_mcu_set_hif_suspend(dev, false); 175 176 return err; 177 } 178 #endif /* CONFIG_PM */ 179 180 struct pci_driver mt7615_pci_driver = { 181 .name = KBUILD_MODNAME, 182 .id_table = mt7615_pci_device_table, 183 .probe = mt7615_pci_probe, 184 .remove = mt7615_pci_remove, 185 #ifdef CONFIG_PM 186 .suspend = mt7615_pci_suspend, 187 .resume = mt7615_pci_resume, 188 #endif /* CONFIG_PM */ 189 }; 190 191 MODULE_DEVICE_TABLE(pci, mt7615_pci_device_table); 192 MODULE_FIRMWARE(MT7615_FIRMWARE_CR4); 193 MODULE_FIRMWARE(MT7615_FIRMWARE_N9); 194 MODULE_FIRMWARE(MT7615_ROM_PATCH); 195 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9); 196 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH); 197 MODULE_FIRMWARE(MT7663_FIRMWARE_N9); 198 MODULE_FIRMWARE(MT7663_ROM_PATCH); 199