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/pci.h>
13 #include <linux/dmi.h>
14 
15 #include "stmmac.h"
16 
17 /*
18  * This struct is used to associate PCI Function of MAC controller on a board,
19  * discovered via DMI, with the address of PHY connected to the MAC. The
20  * negative value of the address means that MAC controller is not connected
21  * with PHY.
22  */
23 struct stmmac_pci_func_data {
24 	unsigned int func;
25 	int phy_addr;
26 };
27 
28 struct stmmac_pci_dmi_data {
29 	const struct stmmac_pci_func_data *func;
30 	size_t nfuncs;
31 };
32 
33 struct stmmac_pci_info {
34 	int (*setup)(struct pci_dev *pdev, struct plat_stmmacenet_data *plat);
35 };
36 
37 static int stmmac_pci_find_phy_addr(struct pci_dev *pdev,
38 				    const struct dmi_system_id *dmi_list)
39 {
40 	const struct stmmac_pci_func_data *func_data;
41 	const struct stmmac_pci_dmi_data *dmi_data;
42 	const struct dmi_system_id *dmi_id;
43 	int func = PCI_FUNC(pdev->devfn);
44 	size_t n;
45 
46 	dmi_id = dmi_first_match(dmi_list);
47 	if (!dmi_id)
48 		return -ENODEV;
49 
50 	dmi_data = dmi_id->driver_data;
51 	func_data = dmi_data->func;
52 
53 	for (n = 0; n < dmi_data->nfuncs; n++, func_data++)
54 		if (func_data->func == func)
55 			return func_data->phy_addr;
56 
57 	return -ENODEV;
58 }
59 
60 static void common_default_data(struct plat_stmmacenet_data *plat)
61 {
62 	plat->clk_csr = 2;	/* clk_csr_i = 20-35MHz & MDC = clk_csr_i/16 */
63 	plat->has_gmac = 1;
64 	plat->force_sf_dma_mode = 1;
65 
66 	plat->mdio_bus_data->phy_reset = NULL;
67 	plat->mdio_bus_data->phy_mask = 0;
68 
69 	/* Set default value for multicast hash bins */
70 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
71 
72 	/* Set default value for unicast filter entries */
73 	plat->unicast_filter_entries = 1;
74 
75 	/* Set the maxmtu to a default of JUMBO_LEN */
76 	plat->maxmtu = JUMBO_LEN;
77 
78 	/* Set default number of RX and TX queues to use */
79 	plat->tx_queues_to_use = 1;
80 	plat->rx_queues_to_use = 1;
81 
82 	/* Disable Priority config by default */
83 	plat->tx_queues_cfg[0].use_prio = false;
84 	plat->rx_queues_cfg[0].use_prio = false;
85 
86 	/* Disable RX queues routing by default */
87 	plat->rx_queues_cfg[0].pkt_route = 0x0;
88 }
89 
90 static int stmmac_default_data(struct pci_dev *pdev,
91 			       struct plat_stmmacenet_data *plat)
92 {
93 	/* Set common default data first */
94 	common_default_data(plat);
95 
96 	plat->bus_id = 1;
97 	plat->phy_addr = 0;
98 	plat->interface = PHY_INTERFACE_MODE_GMII;
99 
100 	plat->dma_cfg->pbl = 32;
101 	plat->dma_cfg->pblx8 = true;
102 	/* TODO: AXI */
103 
104 	return 0;
105 }
106 
107 static const struct stmmac_pci_info stmmac_pci_info = {
108 	.setup = stmmac_default_data,
109 };
110 
111 static const struct stmmac_pci_func_data galileo_stmmac_func_data[] = {
112 	{
113 		.func = 6,
114 		.phy_addr = 1,
115 	},
116 };
117 
118 static const struct stmmac_pci_dmi_data galileo_stmmac_dmi_data = {
119 	.func = galileo_stmmac_func_data,
120 	.nfuncs = ARRAY_SIZE(galileo_stmmac_func_data),
121 };
122 
123 static const struct stmmac_pci_func_data iot2040_stmmac_func_data[] = {
124 	{
125 		.func = 6,
126 		.phy_addr = 1,
127 	},
128 	{
129 		.func = 7,
130 		.phy_addr = 1,
131 	},
132 };
133 
134 static const struct stmmac_pci_dmi_data iot2040_stmmac_dmi_data = {
135 	.func = iot2040_stmmac_func_data,
136 	.nfuncs = ARRAY_SIZE(iot2040_stmmac_func_data),
137 };
138 
139 static const struct dmi_system_id quark_pci_dmi[] = {
140 	{
141 		.matches = {
142 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "Galileo"),
143 		},
144 		.driver_data = (void *)&galileo_stmmac_dmi_data,
145 	},
146 	{
147 		.matches = {
148 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "GalileoGen2"),
149 		},
150 		.driver_data = (void *)&galileo_stmmac_dmi_data,
151 	},
152 	/*
153 	 * There are 2 types of SIMATIC IOT2000: IOT2020 and IOT2040.
154 	 * The asset tag "6ES7647-0AA00-0YA2" is only for IOT2020 which
155 	 * has only one pci network device while other asset tags are
156 	 * for IOT2040 which has two.
157 	 */
158 	{
159 		.matches = {
160 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
161 			DMI_EXACT_MATCH(DMI_BOARD_ASSET_TAG,
162 					"6ES7647-0AA00-0YA2"),
163 		},
164 		.driver_data = (void *)&galileo_stmmac_dmi_data,
165 	},
166 	{
167 		.matches = {
168 			DMI_EXACT_MATCH(DMI_BOARD_NAME, "SIMATIC IOT2000"),
169 		},
170 		.driver_data = (void *)&iot2040_stmmac_dmi_data,
171 	},
172 	{}
173 };
174 
175 static int quark_default_data(struct pci_dev *pdev,
176 			      struct plat_stmmacenet_data *plat)
177 {
178 	int ret;
179 
180 	/* Set common default data first */
181 	common_default_data(plat);
182 
183 	/*
184 	 * Refuse to load the driver and register net device if MAC controller
185 	 * does not connect to any PHY interface.
186 	 */
187 	ret = stmmac_pci_find_phy_addr(pdev, quark_pci_dmi);
188 	if (ret < 0) {
189 		/* Return error to the caller on DMI enabled boards. */
190 		if (dmi_get_system_info(DMI_BOARD_NAME))
191 			return ret;
192 
193 		/*
194 		 * Galileo boards with old firmware don't support DMI. We always
195 		 * use 1 here as PHY address, so at least the first found MAC
196 		 * controller would be probed.
197 		 */
198 		ret = 1;
199 	}
200 
201 	plat->bus_id = pci_dev_id(pdev);
202 	plat->phy_addr = ret;
203 	plat->interface = PHY_INTERFACE_MODE_RMII;
204 
205 	plat->dma_cfg->pbl = 16;
206 	plat->dma_cfg->pblx8 = true;
207 	plat->dma_cfg->fixed_burst = 1;
208 	/* AXI (TODO) */
209 
210 	return 0;
211 }
212 
213 static const struct stmmac_pci_info quark_pci_info = {
214 	.setup = quark_default_data,
215 };
216 
217 /**
218  * stmmac_pci_probe
219  *
220  * @pdev: pci device pointer
221  * @id: pointer to table of device id/id's.
222  *
223  * Description: This probing function gets called for all PCI devices which
224  * match the ID table and are not "owned" by other driver yet. This function
225  * gets passed a "struct pci_dev *" for each device whose entry in the ID table
226  * matches the device. The probe functions returns zero when the driver choose
227  * to take "ownership" of the device or an error code(-ve no) otherwise.
228  */
229 static int stmmac_pci_probe(struct pci_dev *pdev,
230 			    const struct pci_device_id *id)
231 {
232 	struct stmmac_pci_info *info = (struct stmmac_pci_info *)id->driver_data;
233 	struct plat_stmmacenet_data *plat;
234 	struct stmmac_resources res;
235 	int i;
236 	int ret;
237 
238 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
239 	if (!plat)
240 		return -ENOMEM;
241 
242 	plat->mdio_bus_data = devm_kzalloc(&pdev->dev,
243 					   sizeof(*plat->mdio_bus_data),
244 					   GFP_KERNEL);
245 	if (!plat->mdio_bus_data)
246 		return -ENOMEM;
247 
248 	plat->dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*plat->dma_cfg),
249 				     GFP_KERNEL);
250 	if (!plat->dma_cfg)
251 		return -ENOMEM;
252 
253 	/* Enable pci device */
254 	ret = pci_enable_device(pdev);
255 	if (ret) {
256 		dev_err(&pdev->dev, "%s: ERROR: failed to enable device\n",
257 			__func__);
258 		return ret;
259 	}
260 
261 	/* Get the base address of device */
262 	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
263 		if (pci_resource_len(pdev, i) == 0)
264 			continue;
265 		ret = pcim_iomap_regions(pdev, BIT(i), pci_name(pdev));
266 		if (ret)
267 			return ret;
268 		break;
269 	}
270 
271 	pci_set_master(pdev);
272 
273 	ret = info->setup(pdev, plat);
274 	if (ret)
275 		return ret;
276 
277 	pci_enable_msi(pdev);
278 
279 	memset(&res, 0, sizeof(res));
280 	res.addr = pcim_iomap_table(pdev)[i];
281 	res.wol_irq = pdev->irq;
282 	res.irq = pdev->irq;
283 
284 	return stmmac_dvr_probe(&pdev->dev, plat, &res);
285 }
286 
287 /**
288  * stmmac_pci_remove
289  *
290  * @pdev: platform device pointer
291  * Description: this function calls the main to free the net resources
292  * and releases the PCI resources.
293  */
294 static void stmmac_pci_remove(struct pci_dev *pdev)
295 {
296 	int i;
297 
298 	stmmac_dvr_remove(&pdev->dev);
299 
300 	for (i = 0; i <= PCI_STD_RESOURCE_END; i++) {
301 		if (pci_resource_len(pdev, i) == 0)
302 			continue;
303 		pcim_iounmap_regions(pdev, BIT(i));
304 		break;
305 	}
306 
307 	pci_disable_device(pdev);
308 }
309 
310 static int __maybe_unused stmmac_pci_suspend(struct device *dev)
311 {
312 	struct pci_dev *pdev = to_pci_dev(dev);
313 	int ret;
314 
315 	ret = stmmac_suspend(dev);
316 	if (ret)
317 		return ret;
318 
319 	ret = pci_save_state(pdev);
320 	if (ret)
321 		return ret;
322 
323 	pci_disable_device(pdev);
324 	pci_wake_from_d3(pdev, true);
325 	return 0;
326 }
327 
328 static int __maybe_unused stmmac_pci_resume(struct device *dev)
329 {
330 	struct pci_dev *pdev = to_pci_dev(dev);
331 	int ret;
332 
333 	pci_restore_state(pdev);
334 	pci_set_power_state(pdev, PCI_D0);
335 
336 	ret = pci_enable_device(pdev);
337 	if (ret)
338 		return ret;
339 
340 	pci_set_master(pdev);
341 
342 	return stmmac_resume(dev);
343 }
344 
345 static SIMPLE_DEV_PM_OPS(stmmac_pm_ops, stmmac_pci_suspend, stmmac_pci_resume);
346 
347 /* synthetic ID, no official vendor */
348 #define PCI_VENDOR_ID_STMMAC 0x700
349 
350 #define STMMAC_QUARK_ID  0x0937
351 #define STMMAC_DEVICE_ID 0x1108
352 
353 #define STMMAC_DEVICE(vendor_id, dev_id, info)	{	\
354 	PCI_VDEVICE(vendor_id, dev_id),			\
355 	.driver_data = (kernel_ulong_t)&info		\
356 	}
357 
358 static const struct pci_device_id stmmac_id_table[] = {
359 	STMMAC_DEVICE(STMMAC, STMMAC_DEVICE_ID, stmmac_pci_info),
360 	STMMAC_DEVICE(STMICRO, PCI_DEVICE_ID_STMICRO_MAC, stmmac_pci_info),
361 	STMMAC_DEVICE(INTEL, STMMAC_QUARK_ID, quark_pci_info),
362 	{}
363 };
364 
365 MODULE_DEVICE_TABLE(pci, stmmac_id_table);
366 
367 static struct pci_driver stmmac_pci_driver = {
368 	.name = STMMAC_RESOURCE_NAME,
369 	.id_table = stmmac_id_table,
370 	.probe = stmmac_pci_probe,
371 	.remove = stmmac_pci_remove,
372 	.driver         = {
373 		.pm     = &stmmac_pm_ops,
374 	},
375 };
376 
377 module_pci_driver(stmmac_pci_driver);
378 
379 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet PCI driver");
380 MODULE_AUTHOR("Rayagond Kokatanur <rayagond.kokatanur@vayavyalabs.com>");
381 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
382 MODULE_LICENSE("GPL");
383