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