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 hif_suspend = !test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 79 mt7615_firmware_offload(dev); 80 if (hif_suspend) { 81 err = mt7615_mcu_set_hif_suspend(dev, true); 82 if (err) 83 return err; 84 } 85 86 napi_disable(&mdev->tx_napi); 87 tasklet_kill(&mdev->tx_tasklet); 88 89 mt76_for_each_q_rx(mdev, i) { 90 napi_disable(&mdev->napi[i]); 91 } 92 tasklet_kill(&dev->irq_tasklet); 93 94 mt7615_dma_reset(dev); 95 96 err = mt7615_wait_pdma_busy(dev); 97 if (err) 98 goto restore; 99 100 if (is_mt7663(mdev)) { 101 mt76_set(dev, MT_PDMA_SLP_PROT, MT_PDMA_AXI_SLPPROT_ENABLE); 102 if (!mt76_poll_msec(dev, MT_PDMA_SLP_PROT, 103 MT_PDMA_AXI_SLPPROT_RDY, 104 MT_PDMA_AXI_SLPPROT_RDY, 1000)) { 105 dev_err(mdev->dev, "PDMA sleep protection failed\n"); 106 err = -EIO; 107 goto restore; 108 } 109 } 110 111 pci_enable_wake(pdev, pci_choose_state(pdev, state), true); 112 pci_save_state(pdev); 113 err = pci_set_power_state(pdev, pci_choose_state(pdev, state)); 114 if (err) 115 goto restore; 116 117 err = mt7615_firmware_own(dev); 118 if (err) 119 goto restore; 120 121 return 0; 122 123 restore: 124 mt76_for_each_q_rx(mdev, i) { 125 napi_enable(&mdev->napi[i]); 126 } 127 napi_enable(&mdev->tx_napi); 128 if (hif_suspend) 129 mt7615_mcu_set_hif_suspend(dev, false); 130 131 return err; 132 } 133 134 static int mt7615_pci_resume(struct pci_dev *pdev) 135 { 136 struct mt76_dev *mdev = pci_get_drvdata(pdev); 137 struct mt7615_dev *dev = container_of(mdev, struct mt7615_dev, mt76); 138 bool pdma_reset; 139 int i, err; 140 141 err = mt7615_driver_own(dev); 142 if (err < 0) 143 return err; 144 145 err = pci_set_power_state(pdev, PCI_D0); 146 if (err) 147 return err; 148 149 pci_restore_state(pdev); 150 151 if (is_mt7663(&dev->mt76)) { 152 mt76_clear(dev, MT_PDMA_SLP_PROT, MT_PDMA_AXI_SLPPROT_ENABLE); 153 mt76_wr(dev, MT_PCIE_IRQ_ENABLE, 1); 154 } 155 156 pdma_reset = !mt76_rr(dev, MT_WPDMA_TX_RING0_CTRL0) && 157 !mt76_rr(dev, MT_WPDMA_TX_RING0_CTRL1); 158 if (pdma_reset) 159 dev_err(mdev->dev, "PDMA engine must be reinitialized\n"); 160 161 mt76_for_each_q_rx(mdev, i) { 162 napi_enable(&mdev->napi[i]); 163 napi_schedule(&mdev->napi[i]); 164 } 165 napi_enable(&mdev->tx_napi); 166 napi_schedule(&mdev->tx_napi); 167 168 if (!test_bit(MT76_STATE_SUSPEND, &dev->mphy.state) && 169 mt7615_firmware_offload(dev)) 170 err = mt7615_mcu_set_hif_suspend(dev, false); 171 172 return err; 173 } 174 #endif /* CONFIG_PM */ 175 176 struct pci_driver mt7615_pci_driver = { 177 .name = KBUILD_MODNAME, 178 .id_table = mt7615_pci_device_table, 179 .probe = mt7615_pci_probe, 180 .remove = mt7615_pci_remove, 181 #ifdef CONFIG_PM 182 .suspend = mt7615_pci_suspend, 183 .resume = mt7615_pci_resume, 184 #endif /* CONFIG_PM */ 185 }; 186 187 MODULE_DEVICE_TABLE(pci, mt7615_pci_device_table); 188 MODULE_FIRMWARE(MT7615_FIRMWARE_CR4); 189 MODULE_FIRMWARE(MT7615_FIRMWARE_N9); 190 MODULE_FIRMWARE(MT7615_ROM_PATCH); 191 MODULE_FIRMWARE(MT7663_OFFLOAD_FIRMWARE_N9); 192 MODULE_FIRMWARE(MT7663_OFFLOAD_ROM_PATCH); 193 MODULE_FIRMWARE(MT7663_FIRMWARE_N9); 194 MODULE_FIRMWARE(MT7663_ROM_PATCH); 195