xref: /linux/drivers/net/ethernet/stmicro/stmmac/stmmac_pci.c (revision a7ddedc84c59a645ef970b992f7cda5bffc70cc0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3   This contains the functions to handle the pci driver.
4 
5   Copyright (C) 2011-2012  Vayavya Labs Pvt Ltd
6 
7 
8   Author: Rayagond Kokatanur <rayagond@vayavyalabs.com>
9   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
10 *******************************************************************************/
11 
12 #include <linux/clk-provider.h>
13 #include <linux/pci.h>
14 #include <linux/dmi.h>
15 
16 #include "stmmac.h"
17 
18 struct stmmac_pci_info {
19 	int (*setup)(struct pci_dev *pdev, struct plat_stmmacenet_data *plat);
20 };
21 
22 static void common_default_data(struct plat_stmmacenet_data *plat)
23 {
24 	plat->clk_csr = 2;	/* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
25 	plat->has_gmac = 1;
26 	plat->force_sf_dma_mode = 1;
27 
28 	plat->mdio_bus_data->needs_reset = true;
29 
30 	/* Set default value for multicast hash bins */
31 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
32 
33 	/* Set default value for unicast filter entries */
34 	plat->unicast_filter_entries = 1;
35 
36 	/* Set the maxmtu to a default of JUMBO_LEN */
37 	plat->maxmtu = JUMBO_LEN;
38 
39 	/* Set default number of RX and TX queues to use */
40 	plat->tx_queues_to_use = 1;
41 	plat->rx_queues_to_use = 1;
42 
43 	/* Disable Priority config by default */
44 	plat->tx_queues_cfg[0].use_prio = false;
45 	plat->rx_queues_cfg[0].use_prio = false;
46 
47 	/* Disable RX queues routing by default */
48 	plat->rx_queues_cfg[0].pkt_route = 0x0;
49 }
50 
51 static int stmmac_default_data(struct pci_dev *pdev,
52 			       struct plat_stmmacenet_data *plat)
53 {
54 	/* Set common default data first */
55 	common_default_data(plat);
56 
57 	plat->bus_id = 1;
58 	plat->phy_addr = 0;
59 	plat->phy_interface = PHY_INTERFACE_MODE_GMII;
60 
61 	plat->dma_cfg->pbl = 32;
62 	plat->dma_cfg->pblx8 = true;
63 	/* TODO: AXI */
64 
65 	return 0;
66 }
67 
68 static const struct stmmac_pci_info stmmac_pci_info = {
69 	.setup = stmmac_default_data,
70 };
71 
72 static int snps_gmac5_default_data(struct pci_dev *pdev,
73 				   struct plat_stmmacenet_data *plat)
74 {
75 	int i;
76 
77 	plat->clk_csr = 5;
78 	plat->has_gmac4 = 1;
79 	plat->force_sf_dma_mode = 1;
80 	plat->flags |= STMMAC_FLAG_TSO_EN;
81 	plat->pmt = 1;
82 
83 	/* Set default value for multicast hash bins */
84 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
85 
86 	/* Set default value for unicast filter entries */
87 	plat->unicast_filter_entries = 1;
88 
89 	/* Set the maxmtu to a default of JUMBO_LEN */
90 	plat->maxmtu = JUMBO_LEN;
91 
92 	/* Set default number of RX and TX queues to use */
93 	plat->tx_queues_to_use = 4;
94 	plat->rx_queues_to_use = 4;
95 
96 	plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
97 	for (i = 0; i < plat->tx_queues_to_use; i++) {
98 		plat->tx_queues_cfg[i].use_prio = false;
99 		plat->tx_queues_cfg[i].mode_to_use = MTL_QUEUE_DCB;
100 		plat->tx_queues_cfg[i].weight = 25;
101 		if (i > 0)
102 			plat->tx_queues_cfg[i].tbs_en = 1;
103 	}
104 
105 	plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
106 	for (i = 0; i < plat->rx_queues_to_use; i++) {
107 		plat->rx_queues_cfg[i].use_prio = false;
108 		plat->rx_queues_cfg[i].mode_to_use = MTL_QUEUE_DCB;
109 		plat->rx_queues_cfg[i].pkt_route = 0x0;
110 		plat->rx_queues_cfg[i].chan = i;
111 	}
112 
113 	plat->bus_id = 1;
114 	plat->phy_addr = -1;
115 	plat->phy_interface = PHY_INTERFACE_MODE_GMII;
116 
117 	plat->dma_cfg->pbl = 32;
118 	plat->dma_cfg->pblx8 = true;
119 
120 	/* Axi Configuration */
121 	plat->axi = devm_kzalloc(&pdev->dev, sizeof(*plat->axi), GFP_KERNEL);
122 	if (!plat->axi)
123 		return -ENOMEM;
124 
125 	plat->axi->axi_wr_osr_lmt = 31;
126 	plat->axi->axi_rd_osr_lmt = 31;
127 
128 	plat->axi->axi_fb = false;
129 	plat->axi->axi_blen[0] = 4;
130 	plat->axi->axi_blen[1] = 8;
131 	plat->axi->axi_blen[2] = 16;
132 	plat->axi->axi_blen[3] = 32;
133 
134 	return 0;
135 }
136 
137 static const struct stmmac_pci_info snps_gmac5_pci_info = {
138 	.setup = snps_gmac5_default_data,
139 };
140 
141 static int stmmac_pci_suspend(struct device *dev, void *bsp_priv)
142 {
143 	struct pci_dev *pdev = to_pci_dev(dev);
144 	int ret;
145 
146 	ret = pci_save_state(pdev);
147 	if (ret)
148 		return ret;
149 
150 	pci_disable_device(pdev);
151 	pci_wake_from_d3(pdev, true);
152 	return 0;
153 }
154 
155 static int stmmac_pci_resume(struct device *dev, void *bsp_priv)
156 {
157 	struct pci_dev *pdev = to_pci_dev(dev);
158 	int ret;
159 
160 	pci_restore_state(pdev);
161 	pci_set_power_state(pdev, PCI_D0);
162 
163 	ret = pci_enable_device(pdev);
164 	if (ret)
165 		return ret;
166 
167 	pci_set_master(pdev);
168 
169 	return 0;
170 }
171 
172 /**
173  * stmmac_pci_probe
174  *
175  * @pdev: pci device pointer
176  * @id: pointer to table of device id/id's.
177  *
178  * Description: This probing function gets called for all PCI devices which
179  * match the ID table and are not "owned" by other driver yet. This function
180  * gets passed a "struct pci_dev *" for each device whose entry in the ID table
181  * matches the device. The probe functions returns zero when the driver choose
182  * to take "ownership" of the device or an error code(-ve no) otherwise.
183  */
184 static int stmmac_pci_probe(struct pci_dev *pdev,
185 			    const struct pci_device_id *id)
186 {
187 	struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data;
188 	struct plat_stmmacenet_data *plat;
189 	struct stmmac_resources res = {};
190 	int ret;
191 	int i;
192 
193 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
194 	if (!plat)
195 		return -ENOMEM;
196 
197 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
198 					   sizeof(*plat->mdio_bus_data),
199 					   GFP_KERNEL);
200 	if (!plat->mdio_bus_data)
201 		return -ENOMEM;
202 
203 	plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
204 				     GFP_KERNEL);
205 	if (!plat->dma_cfg)
206 		return -ENOMEM;
207 
208 	plat->safety_feat_cfg = devm_kzalloc(&pdev->dev,
209 					     sizeof(*plat->safety_feat_cfg),
210 					     GFP_KERNEL);
211 	if (!plat->safety_feat_cfg)
212 		return -ENOMEM;
213 
214 	/* Enable pci device */
215 	ret = pcim_enable_device(pdev);
216 	if (ret) {
217 		dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
218 			__func__);
219 		return ret;
220 	}
221 
222 	/* Get the base address of device */
223 	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
224 		if (pci_resource_len(pdev, i) == 0)
225 			continue;
226 		res.addr = pcim_iomap_region(pdev, i, STMMAC_RESOURCE_NAME);
227 		if (IS_ERR(res.addr))
228 			return PTR_ERR(res.addr);
229 		break;
230 	}
231 
232 	pci_set_master(pdev);
233 
234 	ret = info->setup(pdev, plat);
235 	if (ret)
236 		return ret;
237 
238 	res.wol_irq = pdev->irq;
239 	res.irq = pdev->irq;
240 
241 	plat->safety_feat_cfg->tsoee = 1;
242 	plat->safety_feat_cfg->mrxpee = 1;
243 	plat->safety_feat_cfg->mestee = 1;
244 	plat->safety_feat_cfg->mrxee = 1;
245 	plat->safety_feat_cfg->mtxee = 1;
246 	plat->safety_feat_cfg->epsi = 1;
247 	plat->safety_feat_cfg->edpp = 1;
248 	plat->safety_feat_cfg->prtyen = 1;
249 	plat->safety_feat_cfg->tmouten = 1;
250 
251 	plat->suspend = stmmac_pci_suspend;
252 	plat->resume = stmmac_pci_resume;
253 
254 	return stmmac_dvr_probe(&pdev->dev, plat, &res);
255 }
256 
257 /**
258  * stmmac_pci_remove
259  *
260  * @pdev: platform device pointer
261  * Description: this function calls the main to free the net resources.
262  */
263 static void stmmac_pci_remove(struct pci_dev *pdev)
264 {
265 	stmmac_dvr_remove(&pdev->dev);
266 }
267 
268 /* synthetic ID, no official vendor */
269 #define PCI_VENDOR_ID_STMMAC		0x0700
270 
271 #define PCI_DEVICE_ID_STMMAC_STMMAC		0x1108
272 #define PCI_DEVICE_ID_SYNOPSYS_GMAC5_ID		0x7102
273 
274 static const struct pci_device_id stmmac_id_table[] = {
275 	{ PCI_DEVICE_DATA(STMMAC, STMMAC, &stmmac_pci_info) },
276 	{ PCI_DEVICE_DATA(STMICRO, MAC, &stmmac_pci_info) },
277 	{ PCI_DEVICE_DATA(SYNOPSYS, GMAC5_ID, &snps_gmac5_pci_info) },
278 	{}
279 };
280 
281 MODULE_DEVICE_TABLE(pci, stmmac_id_table);
282 
283 static struct pci_driver stmmac_pci_driver = {
284 	.name = STMMAC_RESOURCE_NAME,
285 	.id_table = stmmac_id_table,
286 	.probe = stmmac_pci_probe,
287 	.remove = stmmac_pci_remove,
288 	.driver         = {
289 		.pm     = &stmmac_simple_pm_ops,
290 	},
291 };
292 
293 module_pci_driver(stmmac_pci_driver);
294 
295 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
296 MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
297 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
298 MODULE_LICENSE("GPL");
299