1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2020, Loongson Corporation 3 */ 4 5 #include <linux/clk-provider.h> 6 #include <linux/pci.h> 7 #include <linux/dmi.h> 8 #include <linux/device.h> 9 #include <linux/of_irq.h> 10 #include "stmmac.h" 11 12 static int loongson_default_data(struct plat_stmmacenet_data *plat) 13 { 14 plat->clk_csr = 2; /* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */ 15 plat->has_gmac = 1; 16 plat->force_sf_dma_mode = 1; 17 18 /* Set default value for multicast hash bins */ 19 plat->multicast_filter_bins = HASH_TABLE_SIZE; 20 21 /* Set default value for unicast filter entries */ 22 plat->unicast_filter_entries = 1; 23 24 /* Set the maxmtu to a default of JUMBO_LEN */ 25 plat->maxmtu = JUMBO_LEN; 26 27 /* Set default number of RX and TX queues to use */ 28 plat->tx_queues_to_use = 1; 29 plat->rx_queues_to_use = 1; 30 31 /* Disable Priority config by default */ 32 plat->tx_queues_cfg[0].use_prio = false; 33 plat->rx_queues_cfg[0].use_prio = false; 34 35 /* Disable RX queues routing by default */ 36 plat->rx_queues_cfg[0].pkt_route = 0x0; 37 38 /* Default to phy auto-detection */ 39 plat->phy_addr = -1; 40 41 plat->dma_cfg->pbl = 32; 42 plat->dma_cfg->pblx8 = true; 43 44 plat->multicast_filter_bins = 256; 45 return 0; 46 } 47 48 static int loongson_dwmac_probe(struct pci_dev *pdev, const struct pci_device_id *id) 49 { 50 struct plat_stmmacenet_data *plat; 51 struct stmmac_resources res; 52 struct device_node *np; 53 int ret, i, phy_mode; 54 bool mdio = false; 55 56 np = dev_of_node(&pdev->dev); 57 58 if (!np) { 59 pr_info("dwmac_loongson_pci: No OF node\n"); 60 return -ENODEV; 61 } 62 63 if (!of_device_is_compatible(np, "loongson, pci-gmac")) { 64 pr_info("dwmac_loongson_pci: Incompatible OF node\n"); 65 return -ENODEV; 66 } 67 68 plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL); 69 if (!plat) 70 return -ENOMEM; 71 72 if (plat->mdio_node) { 73 dev_err(&pdev->dev, "Found MDIO subnode\n"); 74 mdio = true; 75 } 76 77 if (mdio) { 78 plat->mdio_bus_data = devm_kzalloc(&pdev->dev, 79 sizeof(*plat->mdio_bus_data), 80 GFP_KERNEL); 81 if (!plat->mdio_bus_data) 82 return -ENOMEM; 83 plat->mdio_bus_data->needs_reset = true; 84 } 85 86 plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg), GFP_KERNEL); 87 if (!plat->dma_cfg) 88 return -ENOMEM; 89 90 /* Enable pci device */ 91 ret = pci_enable_device(pdev); 92 if (ret) { 93 dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n", __func__); 94 return ret; 95 } 96 97 /* Get the base address of device */ 98 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 99 if (pci_resource_len(pdev, i) == 0) 100 continue; 101 ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev)); 102 if (ret) 103 return ret; 104 break; 105 } 106 107 plat->bus_id = of_alias_get_id(np, "ethernet"); 108 if (plat->bus_id < 0) 109 plat->bus_id = pci_dev_id(pdev); 110 111 phy_mode = device_get_phy_mode(&pdev->dev); 112 if (phy_mode < 0) 113 dev_err(&pdev->dev, "phy_mode not found\n"); 114 115 plat->phy_interface = phy_mode; 116 plat->interface = PHY_INTERFACE_MODE_GMII; 117 118 pci_set_master(pdev); 119 120 loongson_default_data(plat); 121 pci_enable_msi(pdev); 122 memset(&res, 0, sizeof(res)); 123 res.addr = pcim_iomap_table(pdev)[0]; 124 125 res.irq = of_irq_get_byname(np, "macirq"); 126 if (res.irq < 0) { 127 dev_err(&pdev->dev, "IRQ macirq not found\n"); 128 ret = -ENODEV; 129 } 130 131 res.wol_irq = of_irq_get_byname(np, "eth_wake_irq"); 132 if (res.wol_irq < 0) { 133 dev_info(&pdev->dev, "IRQ eth_wake_irq not found, using macirq\n"); 134 res.wol_irq = res.irq; 135 } 136 137 res.lpi_irq = of_irq_get_byname(np, "eth_lpi"); 138 if (res.lpi_irq < 0) { 139 dev_err(&pdev->dev, "IRQ eth_lpi not found\n"); 140 ret = -ENODEV; 141 } 142 143 return stmmac_dvr_probe(&pdev->dev, plat, &res); 144 } 145 146 static void loongson_dwmac_remove(struct pci_dev *pdev) 147 { 148 int i; 149 150 stmmac_dvr_remove(&pdev->dev); 151 152 for (i = 0; i < PCI_STD_NUM_BARS; i++) { 153 if (pci_resource_len(pdev, i) == 0) 154 continue; 155 pcim_iounmap_regions(pdev, BIT(i)); 156 break; 157 } 158 159 pci_disable_device(pdev); 160 } 161 162 static int __maybe_unused loongson_dwmac_suspend(struct device *dev) 163 { 164 struct pci_dev *pdev = to_pci_dev(dev); 165 int ret; 166 167 ret = stmmac_suspend(dev); 168 if (ret) 169 return ret; 170 171 ret = pci_save_state(pdev); 172 if (ret) 173 return ret; 174 175 pci_disable_device(pdev); 176 pci_wake_from_d3(pdev, true); 177 return 0; 178 } 179 180 static int __maybe_unused loongson_dwmac_resume(struct device *dev) 181 { 182 struct pci_dev *pdev = to_pci_dev(dev); 183 int ret; 184 185 pci_restore_state(pdev); 186 pci_set_power_state(pdev, PCI_D0); 187 188 ret = pci_enable_device(pdev); 189 if (ret) 190 return ret; 191 192 pci_set_master(pdev); 193 194 return stmmac_resume(dev); 195 } 196 197 static SIMPLE_DEV_PM_OPS(loongson_dwmac_pm_ops, loongson_dwmac_suspend, 198 loongson_dwmac_resume); 199 200 static const struct pci_device_id loongson_dwmac_id_table[] = { 201 { PCI_VDEVICE(LOONGSON, 0x7a03) }, 202 {} 203 }; 204 MODULE_DEVICE_TABLE(pci, loongson_dwmac_id_table); 205 206 struct pci_driver loongson_dwmac_driver = { 207 .name = "dwmac-loongson-pci", 208 .id_table = loongson_dwmac_id_table, 209 .probe = loongson_dwmac_probe, 210 .remove = loongson_dwmac_remove, 211 .driver = { 212 .pm = &loongson_dwmac_pm_ops, 213 }, 214 }; 215 216 module_pci_driver(loongson_dwmac_driver); 217 218 MODULE_DESCRIPTION("Loongson DWMAC PCI driver"); 219 MODULE_AUTHOR("Qing Zhang <zhangqing@loongson.cn>"); 220 MODULE_LICENSE("GPL v2"); 221