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 
loongson_default_data(struct plat_stmmacenet_data * plat)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 
loongson_dwmac_probe(struct pci_dev * pdev,const struct pci_device_id * id)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 
55 	np = dev_of_node(&pdev->dev);
56 
57 	if (!np) {
58 		pr_info("dwmac_loongson_pci: No OF node\n");
59 		return -ENODEV;
60 	}
61 
62 	if (!of_device_is_compatible(np, "loongson, pci-gmac")) {
63 		pr_info("dwmac_loongson_pci: Incompatible OF node\n");
64 		return -ENODEV;
65 	}
66 
67 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
68 	if (!plat)
69 		return -ENOMEM;
70 
71 	plat->mdio_node = of_get_child_by_name(np, "mdio");
72 	if (plat->mdio_node) {
73 		dev_info(&pdev->dev, "Found MDIO subnode\n");
74 
75 		plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
76 						   sizeof(*plat->mdio_bus_data),
77 						   GFP_KERNEL);
78 		if (!plat->mdio_bus_data) {
79 			ret = -ENOMEM;
80 			goto err_put_node;
81 		}
82 		plat->mdio_bus_data->needs_reset = true;
83 	}
84 
85 	plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg), GFP_KERNEL);
86 	if (!plat->dma_cfg) {
87 		ret = -ENOMEM;
88 		goto err_put_node;
89 	}
90 
91 	/* Enable pci device */
92 	ret = pci_enable_device(pdev);
93 	if (ret) {
94 		dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n", __func__);
95 		goto err_put_node;
96 	}
97 
98 	/* Get the base address of device */
99 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
100 		if (pci_resource_len(pdev, i) == 0)
101 			continue;
102 		ret = pcim_iomap_regions(pdev, BIT(0), pci_name(pdev));
103 		if (ret)
104 			goto err_disable_device;
105 		break;
106 	}
107 
108 	plat->bus_id = of_alias_get_id(np, "ethernet");
109 	if (plat->bus_id < 0)
110 		plat->bus_id = pci_dev_id(pdev);
111 
112 	phy_mode = device_get_phy_mode(&pdev->dev);
113 	if (phy_mode < 0) {
114 		dev_err(&pdev->dev, "phy_mode not found\n");
115 		ret = phy_mode;
116 		goto err_disable_device;
117 	}
118 
119 	plat->phy_interface = phy_mode;
120 	plat->interface = PHY_INTERFACE_MODE_GMII;
121 
122 	pci_set_master(pdev);
123 
124 	loongson_default_data(plat);
125 	pci_enable_msi(pdev);
126 	memset(&res, 0, sizeof(res));
127 	res.addr = pcim_iomap_table(pdev)[0];
128 
129 	res.irq = of_irq_get_byname(np, "macirq");
130 	if (res.irq < 0) {
131 		dev_err(&pdev->dev, "IRQ macirq not found\n");
132 		ret = -ENODEV;
133 		goto err_disable_msi;
134 	}
135 
136 	res.wol_irq = of_irq_get_byname(np, "eth_wake_irq");
137 	if (res.wol_irq < 0) {
138 		dev_info(&pdev->dev, "IRQ eth_wake_irq not found, using macirq\n");
139 		res.wol_irq = res.irq;
140 	}
141 
142 	res.lpi_irq = of_irq_get_byname(np, "eth_lpi");
143 	if (res.lpi_irq < 0) {
144 		dev_err(&pdev->dev, "IRQ eth_lpi not found\n");
145 		ret = -ENODEV;
146 		goto err_disable_msi;
147 	}
148 
149 	ret = stmmac_dvr_probe(&pdev->dev, plat, &res);
150 	if (ret)
151 		goto err_disable_msi;
152 
153 	return ret;
154 
155 err_disable_msi:
156 	pci_disable_msi(pdev);
157 err_disable_device:
158 	pci_disable_device(pdev);
159 err_put_node:
160 	of_node_put(plat->mdio_node);
161 	return ret;
162 }
163 
loongson_dwmac_remove(struct pci_dev * pdev)164 static void loongson_dwmac_remove(struct pci_dev *pdev)
165 {
166 	struct net_device *ndev = dev_get_drvdata(&pdev->dev);
167 	struct stmmac_priv *priv = netdev_priv(ndev);
168 	int i;
169 
170 	of_node_put(priv->plat->mdio_node);
171 	stmmac_dvr_remove(&pdev->dev);
172 
173 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
174 		if (pci_resource_len(pdev, i) == 0)
175 			continue;
176 		pcim_iounmap_regions(pdev, BIT(i));
177 		break;
178 	}
179 
180 	pci_disable_msi(pdev);
181 	pci_disable_device(pdev);
182 }
183 
loongson_dwmac_suspend(struct device * dev)184 static int __maybe_unused loongson_dwmac_suspend(struct device *dev)
185 {
186 	struct pci_dev *pdev = to_pci_dev(dev);
187 	int ret;
188 
189 	ret = stmmac_suspend(dev);
190 	if (ret)
191 		return ret;
192 
193 	ret = pci_save_state(pdev);
194 	if (ret)
195 		return ret;
196 
197 	pci_disable_device(pdev);
198 	pci_wake_from_d3(pdev, true);
199 	return 0;
200 }
201 
loongson_dwmac_resume(struct device * dev)202 static int __maybe_unused loongson_dwmac_resume(struct device *dev)
203 {
204 	struct pci_dev *pdev = to_pci_dev(dev);
205 	int ret;
206 
207 	pci_restore_state(pdev);
208 	pci_set_power_state(pdev, PCI_D0);
209 
210 	ret = pci_enable_device(pdev);
211 	if (ret)
212 		return ret;
213 
214 	pci_set_master(pdev);
215 
216 	return stmmac_resume(dev);
217 }
218 
219 static SIMPLE_DEV_PM_OPS(loongson_dwmac_pm_ops, loongson_dwmac_suspend,
220 			 loongson_dwmac_resume);
221 
222 static const struct pci_device_id loongson_dwmac_id_table[] = {
223 	{ PCI_VDEVICE(LOONGSON, 0x7a03) },
224 	{}
225 };
226 MODULE_DEVICE_TABLE(pci, loongson_dwmac_id_table);
227 
228 static struct pci_driver loongson_dwmac_driver = {
229 	.name = "dwmac-loongson-pci",
230 	.id_table = loongson_dwmac_id_table,
231 	.probe = loongson_dwmac_probe,
232 	.remove = loongson_dwmac_remove,
233 	.driver = {
234 		.pm = &loongson_dwmac_pm_ops,
235 	},
236 };
237 
238 module_pci_driver(loongson_dwmac_driver);
239 
240 MODULE_DESCRIPTION("Loongson DWMAC PCI driver");
241 MODULE_AUTHOR("Qing Zhang <zhangqing@loongson.cn>");
242 MODULE_LICENSE("GPL v2");
243