1 // SPDX-License-Identifier: GPL-2.0-only
2 /*******************************************************************************
3   This contains the functions to handle the platform driver.
4 
5   Copyright (C) 2007-2011  STMicroelectronics Ltd
6 
7 
8   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
9 *******************************************************************************/
10 
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_runtime.h>
14 #include <linux/module.h>
15 #include <linux/io.h>
16 #include <linux/of.h>
17 #include <linux/of_net.h>
18 #include <linux/of_device.h>
19 #include <linux/of_mdio.h>
20 
21 #include "stmmac.h"
22 #include "stmmac_platform.h"
23 
24 #ifdef CONFIG_OF
25 
26 /**
27  * dwmac1000_validate_mcast_bins - validates the number of Multicast filter bins
28  * @dev: struct device of the platform device
29  * @mcast_bins: Multicast filtering bins
30  * Description:
31  * this function validates the number of Multicast filtering bins specified
32  * by the configuration through the device tree. The Synopsys GMAC supports
33  * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC
34  * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds
35  * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is
36  * invalid and will cause the filtering algorithm to use Multicast
37  * promiscuous mode.
38  */
39 static int dwmac1000_validate_mcast_bins(struct device *dev, int mcast_bins)
40 {
41 	int x = mcast_bins;
42 
43 	switch (x) {
44 	case HASH_TABLE_SIZE:
45 	case 128:
46 	case 256:
47 		break;
48 	default:
49 		x = 0;
50 		dev_info(dev, "Hash table entries set to unexpected value %d\n",
51 			 mcast_bins);
52 		break;
53 	}
54 	return x;
55 }
56 
57 /**
58  * dwmac1000_validate_ucast_entries - validate the Unicast address entries
59  * @dev: struct device of the platform device
60  * @ucast_entries: number of Unicast address entries
61  * Description:
62  * This function validates the number of Unicast address entries supported
63  * by a particular Synopsys 10/100/1000 controller. The Synopsys controller
64  * supports 1..32, 64, or 128 Unicast filter entries for it's Unicast filter
65  * logic. This function validates a valid, supported configuration is
66  * selected, and defaults to 1 Unicast address if an unsupported
67  * configuration is selected.
68  */
69 static int dwmac1000_validate_ucast_entries(struct device *dev,
70 					    int ucast_entries)
71 {
72 	int x = ucast_entries;
73 
74 	switch (x) {
75 	case 1 ... 32:
76 	case 64:
77 	case 128:
78 		break;
79 	default:
80 		x = 1;
81 		dev_info(dev, "Unicast table entries set to unexpected value %d\n",
82 			 ucast_entries);
83 		break;
84 	}
85 	return x;
86 }
87 
88 /**
89  * stmmac_axi_setup - parse DT parameters for programming the AXI register
90  * @pdev: platform device
91  * Description:
92  * if required, from device-tree the AXI internal register can be tuned
93  * by using platform parameters.
94  */
95 static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev)
96 {
97 	struct device_node *np;
98 	struct stmmac_axi *axi;
99 
100 	np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0);
101 	if (!np)
102 		return NULL;
103 
104 	axi = devm_kzalloc(&pdev->dev, sizeof(*axi), GFP_KERNEL);
105 	if (!axi) {
106 		of_node_put(np);
107 		return ERR_PTR(-ENOMEM);
108 	}
109 
110 	axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en");
111 	axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm");
112 	axi->axi_kbbe = of_property_read_bool(np, "snps,kbbe");
113 	axi->axi_fb = of_property_read_bool(np, "snps,fb");
114 	axi->axi_mb = of_property_read_bool(np, "snps,mb");
115 	axi->axi_rb =  of_property_read_bool(np, "snps,rb");
116 
117 	if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
118 		axi->axi_wr_osr_lmt = 1;
119 	if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
120 		axi->axi_rd_osr_lmt = 1;
121 	of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
122 	of_node_put(np);
123 
124 	return axi;
125 }
126 
127 /**
128  * stmmac_mtl_setup - parse DT parameters for multiple queues configuration
129  * @pdev: platform device
130  * @plat: enet data
131  */
132 static int stmmac_mtl_setup(struct platform_device *pdev,
133 			    struct plat_stmmacenet_data *plat)
134 {
135 	struct device_node *q_node;
136 	struct device_node *rx_node;
137 	struct device_node *tx_node;
138 	u8 queue = 0;
139 	int ret = 0;
140 
141 	/* For backwards-compatibility with device trees that don't have any
142 	 * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back
143 	 * to one RX and TX queues each.
144 	 */
145 	plat->rx_queues_to_use = 1;
146 	plat->tx_queues_to_use = 1;
147 
148 	/* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need
149 	 * to always set this, otherwise Queue will be classified as AVB
150 	 * (because MTL_QUEUE_AVB = 0).
151 	 */
152 	plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
153 	plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
154 
155 	rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0);
156 	if (!rx_node)
157 		return ret;
158 
159 	tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0);
160 	if (!tx_node) {
161 		of_node_put(rx_node);
162 		return ret;
163 	}
164 
165 	/* Processing RX queues common config */
166 	if (of_property_read_u32(rx_node, "snps,rx-queues-to-use",
167 				 &plat->rx_queues_to_use))
168 		plat->rx_queues_to_use = 1;
169 
170 	if (of_property_read_bool(rx_node, "snps,rx-sched-sp"))
171 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
172 	else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp"))
173 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP;
174 	else
175 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
176 
177 	/* Processing individual RX queue config */
178 	for_each_child_of_node(rx_node, q_node) {
179 		if (queue >= plat->rx_queues_to_use)
180 			break;
181 
182 		if (of_property_read_bool(q_node, "snps,dcb-algorithm"))
183 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
184 		else if (of_property_read_bool(q_node, "snps,avb-algorithm"))
185 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
186 		else
187 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
188 
189 		if (of_property_read_u32(q_node, "snps,map-to-dma-channel",
190 					 &plat->rx_queues_cfg[queue].chan))
191 			plat->rx_queues_cfg[queue].chan = queue;
192 		/* TODO: Dynamic mapping to be included in the future */
193 
194 		if (of_property_read_u32(q_node, "snps,priority",
195 					&plat->rx_queues_cfg[queue].prio)) {
196 			plat->rx_queues_cfg[queue].prio = 0;
197 			plat->rx_queues_cfg[queue].use_prio = false;
198 		} else {
199 			plat->rx_queues_cfg[queue].use_prio = true;
200 		}
201 
202 		/* RX queue specific packet type routing */
203 		if (of_property_read_bool(q_node, "snps,route-avcp"))
204 			plat->rx_queues_cfg[queue].pkt_route = PACKET_AVCPQ;
205 		else if (of_property_read_bool(q_node, "snps,route-ptp"))
206 			plat->rx_queues_cfg[queue].pkt_route = PACKET_PTPQ;
207 		else if (of_property_read_bool(q_node, "snps,route-dcbcp"))
208 			plat->rx_queues_cfg[queue].pkt_route = PACKET_DCBCPQ;
209 		else if (of_property_read_bool(q_node, "snps,route-up"))
210 			plat->rx_queues_cfg[queue].pkt_route = PACKET_UPQ;
211 		else if (of_property_read_bool(q_node, "snps,route-multi-broad"))
212 			plat->rx_queues_cfg[queue].pkt_route = PACKET_MCBCQ;
213 		else
214 			plat->rx_queues_cfg[queue].pkt_route = 0x0;
215 
216 		queue++;
217 	}
218 	if (queue != plat->rx_queues_to_use) {
219 		ret = -EINVAL;
220 		dev_err(&pdev->dev, "Not all RX queues were configured\n");
221 		goto out;
222 	}
223 
224 	/* Processing TX queues common config */
225 	if (of_property_read_u32(tx_node, "snps,tx-queues-to-use",
226 				 &plat->tx_queues_to_use))
227 		plat->tx_queues_to_use = 1;
228 
229 	if (of_property_read_bool(tx_node, "snps,tx-sched-wrr"))
230 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
231 	else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq"))
232 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ;
233 	else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr"))
234 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR;
235 	else
236 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
237 
238 	queue = 0;
239 
240 	/* Processing individual TX queue config */
241 	for_each_child_of_node(tx_node, q_node) {
242 		if (queue >= plat->tx_queues_to_use)
243 			break;
244 
245 		if (of_property_read_u32(q_node, "snps,weight",
246 					 &plat->tx_queues_cfg[queue].weight))
247 			plat->tx_queues_cfg[queue].weight = 0x10 + queue;
248 
249 		if (of_property_read_bool(q_node, "snps,dcb-algorithm")) {
250 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
251 		} else if (of_property_read_bool(q_node,
252 						 "snps,avb-algorithm")) {
253 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
254 
255 			/* Credit Base Shaper parameters used by AVB */
256 			if (of_property_read_u32(q_node, "snps,send_slope",
257 				&plat->tx_queues_cfg[queue].send_slope))
258 				plat->tx_queues_cfg[queue].send_slope = 0x0;
259 			if (of_property_read_u32(q_node, "snps,idle_slope",
260 				&plat->tx_queues_cfg[queue].idle_slope))
261 				plat->tx_queues_cfg[queue].idle_slope = 0x0;
262 			if (of_property_read_u32(q_node, "snps,high_credit",
263 				&plat->tx_queues_cfg[queue].high_credit))
264 				plat->tx_queues_cfg[queue].high_credit = 0x0;
265 			if (of_property_read_u32(q_node, "snps,low_credit",
266 				&plat->tx_queues_cfg[queue].low_credit))
267 				plat->tx_queues_cfg[queue].low_credit = 0x0;
268 		} else {
269 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
270 		}
271 
272 		if (of_property_read_u32(q_node, "snps,priority",
273 					&plat->tx_queues_cfg[queue].prio)) {
274 			plat->tx_queues_cfg[queue].prio = 0;
275 			plat->tx_queues_cfg[queue].use_prio = false;
276 		} else {
277 			plat->tx_queues_cfg[queue].use_prio = true;
278 		}
279 
280 		queue++;
281 	}
282 	if (queue != plat->tx_queues_to_use) {
283 		ret = -EINVAL;
284 		dev_err(&pdev->dev, "Not all TX queues were configured\n");
285 		goto out;
286 	}
287 
288 out:
289 	of_node_put(rx_node);
290 	of_node_put(tx_node);
291 	of_node_put(q_node);
292 
293 	return ret;
294 }
295 
296 /**
297  * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
298  * @plat: driver data platform structure
299  * @np: device tree node
300  * @dev: device pointer
301  * Description:
302  * The mdio bus will be allocated in case of a phy transceiver is on board;
303  * it will be NULL if the fixed-link is configured.
304  * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
305  * in any case (for DSA, mdio must be registered even if fixed-link).
306  * The table below sums the supported configurations:
307  *	-------------------------------
308  *	snps,phy-addr	|     Y
309  *	-------------------------------
310  *	phy-handle	|     Y
311  *	-------------------------------
312  *	fixed-link	|     N
313  *	-------------------------------
314  *	snps,dwmac-mdio	|
315  *	  even if	|     Y
316  *	fixed-link	|
317  *	-------------------------------
318  *
319  * It returns 0 in case of success otherwise -ENODEV.
320  */
321 static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
322 			 struct device_node *np, struct device *dev)
323 {
324 	bool mdio = !of_phy_is_fixed_link(np);
325 	static const struct of_device_id need_mdio_ids[] = {
326 		{ .compatible = "snps,dwc-qos-ethernet-4.10" },
327 		{},
328 	};
329 
330 	if (of_match_node(need_mdio_ids, np)) {
331 		plat->mdio_node = of_get_child_by_name(np, "mdio");
332 	} else {
333 		/**
334 		 * If snps,dwmac-mdio is passed from DT, always register
335 		 * the MDIO
336 		 */
337 		for_each_child_of_node(np, plat->mdio_node) {
338 			if (of_device_is_compatible(plat->mdio_node,
339 						    "snps,dwmac-mdio"))
340 				break;
341 		}
342 	}
343 
344 	if (plat->mdio_node) {
345 		dev_dbg(dev, "Found MDIO subnode\n");
346 		mdio = true;
347 	}
348 
349 	if (mdio) {
350 		plat->mdio_bus_data =
351 			devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
352 				     GFP_KERNEL);
353 		if (!plat->mdio_bus_data)
354 			return -ENOMEM;
355 
356 		plat->mdio_bus_data->needs_reset = true;
357 	}
358 
359 	return 0;
360 }
361 
362 /**
363  * stmmac_of_get_mac_mode - retrieves the interface of the MAC
364  * @np: - device-tree node
365  * Description:
366  * Similar to `of_get_phy_mode()`, this function will retrieve (from
367  * the device-tree) the interface mode on the MAC side. This assumes
368  * that there is mode converter in-between the MAC & PHY
369  * (e.g. GMII-to-RGMII).
370  */
371 static int stmmac_of_get_mac_mode(struct device_node *np)
372 {
373 	const char *pm;
374 	int err, i;
375 
376 	err = of_property_read_string(np, "mac-mode", &pm);
377 	if (err < 0)
378 		return err;
379 
380 	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) {
381 		if (!strcasecmp(pm, phy_modes(i)))
382 			return i;
383 	}
384 
385 	return -ENODEV;
386 }
387 
388 /**
389  * stmmac_probe_config_dt - parse device-tree driver parameters
390  * @pdev: platform_device structure
391  * @mac: MAC address to use
392  * Description:
393  * this function is to read the driver parameters from device-tree and
394  * set some private fields that will be used by the main at runtime.
395  */
396 struct plat_stmmacenet_data *
397 stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
398 {
399 	struct device_node *np = pdev->dev.of_node;
400 	struct plat_stmmacenet_data *plat;
401 	struct stmmac_dma_cfg *dma_cfg;
402 	int phy_mode;
403 	void *ret;
404 	int rc;
405 
406 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
407 	if (!plat)
408 		return ERR_PTR(-ENOMEM);
409 
410 	rc = of_get_mac_address(np, mac);
411 	if (rc) {
412 		if (rc == -EPROBE_DEFER)
413 			return ERR_PTR(rc);
414 
415 		eth_zero_addr(mac);
416 	}
417 
418 	phy_mode = device_get_phy_mode(&pdev->dev);
419 	if (phy_mode < 0)
420 		return ERR_PTR(phy_mode);
421 
422 	plat->phy_interface = phy_mode;
423 	plat->interface = stmmac_of_get_mac_mode(np);
424 	if (plat->interface < 0)
425 		plat->interface = plat->phy_interface;
426 
427 	/* Some wrapper drivers still rely on phy_node. Let's save it while
428 	 * they are not converted to phylink. */
429 	plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
430 
431 	/* PHYLINK automatically parses the phy-handle property */
432 	plat->phylink_node = np;
433 
434 	/* Get max speed of operation from device tree */
435 	of_property_read_u32(np, "max-speed", &plat->max_speed);
436 
437 	plat->bus_id = of_alias_get_id(np, "ethernet");
438 	if (plat->bus_id < 0)
439 		plat->bus_id = 0;
440 
441 	/* Default to phy auto-detection */
442 	plat->phy_addr = -1;
443 
444 	/* Default to get clk_csr from stmmac_clk_csr_set(),
445 	 * or get clk_csr from device tree.
446 	 */
447 	plat->clk_csr = -1;
448 	if (of_property_read_u32(np, "snps,clk-csr", &plat->clk_csr))
449 		of_property_read_u32(np, "clk_csr", &plat->clk_csr);
450 
451 	/* "snps,phy-addr" is not a standard property. Mark it as deprecated
452 	 * and warn of its use. Remove this when phy node support is added.
453 	 */
454 	if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
455 		dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
456 
457 	/* To Configure PHY by using all device-tree supported properties */
458 	rc = stmmac_dt_phy(plat, np, &pdev->dev);
459 	if (rc)
460 		return ERR_PTR(rc);
461 
462 	of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
463 
464 	of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
465 
466 	plat->force_sf_dma_mode =
467 		of_property_read_bool(np, "snps,force_sf_dma_mode");
468 
469 	if (of_property_read_bool(np, "snps,en-tx-lpi-clockgating"))
470 		plat->flags |= STMMAC_FLAG_EN_TX_LPI_CLOCKGATING;
471 
472 	/* Set the maxmtu to a default of JUMBO_LEN in case the
473 	 * parameter is not present in the device tree.
474 	 */
475 	plat->maxmtu = JUMBO_LEN;
476 
477 	/* Set default value for multicast hash bins */
478 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
479 
480 	/* Set default value for unicast filter entries */
481 	plat->unicast_filter_entries = 1;
482 
483 	/*
484 	 * Currently only the properties needed on SPEAr600
485 	 * are provided. All other properties should be added
486 	 * once needed on other platforms.
487 	 */
488 	if (of_device_is_compatible(np, "st,spear600-gmac") ||
489 		of_device_is_compatible(np, "snps,dwmac-3.50a") ||
490 		of_device_is_compatible(np, "snps,dwmac-3.70a") ||
491 		of_device_is_compatible(np, "snps,dwmac")) {
492 		/* Note that the max-frame-size parameter as defined in the
493 		 * ePAPR v1.1 spec is defined as max-frame-size, it's
494 		 * actually used as the IEEE definition of MAC Client
495 		 * data, or MTU. The ePAPR specification is confusing as
496 		 * the definition is max-frame-size, but usage examples
497 		 * are clearly MTUs
498 		 */
499 		of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
500 		of_property_read_u32(np, "snps,multicast-filter-bins",
501 				     &plat->multicast_filter_bins);
502 		of_property_read_u32(np, "snps,perfect-filter-entries",
503 				     &plat->unicast_filter_entries);
504 		plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
505 				&pdev->dev, plat->unicast_filter_entries);
506 		plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
507 				&pdev->dev, plat->multicast_filter_bins);
508 		plat->has_gmac = 1;
509 		plat->pmt = 1;
510 	}
511 
512 	if (of_device_is_compatible(np, "snps,dwmac-3.40a")) {
513 		plat->has_gmac = 1;
514 		plat->enh_desc = 1;
515 		plat->tx_coe = 1;
516 		plat->bugged_jumbo = 1;
517 		plat->pmt = 1;
518 	}
519 
520 	if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
521 	    of_device_is_compatible(np, "snps,dwmac-4.10a") ||
522 	    of_device_is_compatible(np, "snps,dwmac-4.20a") ||
523 	    of_device_is_compatible(np, "snps,dwmac-5.10a") ||
524 	    of_device_is_compatible(np, "snps,dwmac-5.20")) {
525 		plat->has_gmac4 = 1;
526 		plat->has_gmac = 0;
527 		plat->pmt = 1;
528 		if (of_property_read_bool(np, "snps,tso"))
529 			plat->flags |= STMMAC_FLAG_TSO_EN;
530 	}
531 
532 	if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
533 		of_device_is_compatible(np, "snps,dwmac-3.710")) {
534 		plat->enh_desc = 1;
535 		plat->bugged_jumbo = 1;
536 		plat->force_sf_dma_mode = 1;
537 	}
538 
539 	if (of_device_is_compatible(np, "snps,dwxgmac")) {
540 		plat->has_xgmac = 1;
541 		plat->pmt = 1;
542 		if (of_property_read_bool(np, "snps,tso"))
543 			plat->flags |= STMMAC_FLAG_TSO_EN;
544 	}
545 
546 	dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
547 			       GFP_KERNEL);
548 	if (!dma_cfg) {
549 		stmmac_remove_config_dt(pdev, plat);
550 		return ERR_PTR(-ENOMEM);
551 	}
552 	plat->dma_cfg = dma_cfg;
553 
554 	of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
555 	if (!dma_cfg->pbl)
556 		dma_cfg->pbl = DEFAULT_DMA_PBL;
557 	of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
558 	of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
559 	dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
560 
561 	dma_cfg->aal = of_property_read_bool(np, "snps,aal");
562 	dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");
563 	dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");
564 
565 	plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
566 	if (plat->force_thresh_dma_mode && plat->force_sf_dma_mode) {
567 		plat->force_sf_dma_mode = 0;
568 		dev_warn(&pdev->dev,
569 			 "force_sf_dma_mode is ignored if force_thresh_dma_mode is set.\n");
570 	}
571 
572 	of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
573 
574 	plat->axi = stmmac_axi_setup(pdev);
575 
576 	rc = stmmac_mtl_setup(pdev, plat);
577 	if (rc) {
578 		stmmac_remove_config_dt(pdev, plat);
579 		return ERR_PTR(rc);
580 	}
581 
582 	/* clock setup */
583 	if (!of_device_is_compatible(np, "snps,dwc-qos-ethernet-4.10")) {
584 		plat->stmmac_clk = devm_clk_get(&pdev->dev,
585 						STMMAC_RESOURCE_NAME);
586 		if (IS_ERR(plat->stmmac_clk)) {
587 			dev_warn(&pdev->dev, "Cannot get CSR clock\n");
588 			plat->stmmac_clk = NULL;
589 		}
590 		clk_prepare_enable(plat->stmmac_clk);
591 	}
592 
593 	plat->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
594 	if (IS_ERR(plat->pclk)) {
595 		ret = plat->pclk;
596 		goto error_pclk_get;
597 	}
598 	clk_prepare_enable(plat->pclk);
599 
600 	/* Fall-back to main clock in case of no PTP ref is passed */
601 	plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "ptp_ref");
602 	if (IS_ERR(plat->clk_ptp_ref)) {
603 		plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
604 		plat->clk_ptp_ref = NULL;
605 		dev_info(&pdev->dev, "PTP uses main clock\n");
606 	} else {
607 		plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref);
608 		dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate);
609 	}
610 
611 	plat->stmmac_rst = devm_reset_control_get_optional(&pdev->dev,
612 							   STMMAC_RESOURCE_NAME);
613 	if (IS_ERR(plat->stmmac_rst)) {
614 		ret = plat->stmmac_rst;
615 		goto error_hw_init;
616 	}
617 
618 	plat->stmmac_ahb_rst = devm_reset_control_get_optional_shared(
619 							&pdev->dev, "ahb");
620 	if (IS_ERR(plat->stmmac_ahb_rst)) {
621 		ret = plat->stmmac_ahb_rst;
622 		goto error_hw_init;
623 	}
624 
625 	return plat;
626 
627 error_hw_init:
628 	clk_disable_unprepare(plat->pclk);
629 error_pclk_get:
630 	clk_disable_unprepare(plat->stmmac_clk);
631 
632 	return ret;
633 }
634 
635 static void devm_stmmac_remove_config_dt(void *data)
636 {
637 	struct plat_stmmacenet_data *plat = data;
638 
639 	/* Platform data argument is unused */
640 	stmmac_remove_config_dt(NULL, plat);
641 }
642 
643 /**
644  * devm_stmmac_probe_config_dt
645  * @pdev: platform_device structure
646  * @mac: MAC address to use
647  * Description: Devres variant of stmmac_probe_config_dt(). Does not require
648  * the user to call stmmac_remove_config_dt() at driver detach.
649  */
650 struct plat_stmmacenet_data *
651 devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
652 {
653 	struct plat_stmmacenet_data *plat;
654 	int ret;
655 
656 	plat = stmmac_probe_config_dt(pdev, mac);
657 	if (IS_ERR(plat))
658 		return plat;
659 
660 	ret = devm_add_action_or_reset(&pdev->dev,
661 				       devm_stmmac_remove_config_dt, plat);
662 	if (ret)
663 		return ERR_PTR(ret);
664 
665 	return plat;
666 }
667 
668 /**
669  * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
670  * @pdev: platform_device structure
671  * @plat: driver data platform structure
672  *
673  * Release resources claimed by stmmac_probe_config_dt().
674  */
675 void stmmac_remove_config_dt(struct platform_device *pdev,
676 			     struct plat_stmmacenet_data *plat)
677 {
678 	clk_disable_unprepare(plat->stmmac_clk);
679 	clk_disable_unprepare(plat->pclk);
680 	of_node_put(plat->phy_node);
681 	of_node_put(plat->mdio_node);
682 }
683 #else
684 struct plat_stmmacenet_data *
685 stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
686 {
687 	return ERR_PTR(-EINVAL);
688 }
689 
690 struct plat_stmmacenet_data *
691 devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
692 {
693 	return ERR_PTR(-EINVAL);
694 }
695 
696 void stmmac_remove_config_dt(struct platform_device *pdev,
697 			     struct plat_stmmacenet_data *plat)
698 {
699 }
700 #endif /* CONFIG_OF */
701 EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
702 EXPORT_SYMBOL_GPL(devm_stmmac_probe_config_dt);
703 EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
704 
705 int stmmac_get_platform_resources(struct platform_device *pdev,
706 				  struct stmmac_resources *stmmac_res)
707 {
708 	memset(stmmac_res, 0, sizeof(*stmmac_res));
709 
710 	/* Get IRQ information early to have an ability to ask for deferred
711 	 * probe if needed before we went too far with resource allocation.
712 	 */
713 	stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
714 	if (stmmac_res->irq < 0)
715 		return stmmac_res->irq;
716 
717 	/* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
718 	 * The external wake up irq can be passed through the platform code
719 	 * named as "eth_wake_irq"
720 	 *
721 	 * In case the wake up interrupt is not passed from the platform
722 	 * so the driver will continue to use the mac irq (ndev->irq)
723 	 */
724 	stmmac_res->wol_irq =
725 		platform_get_irq_byname_optional(pdev, "eth_wake_irq");
726 	if (stmmac_res->wol_irq < 0) {
727 		if (stmmac_res->wol_irq == -EPROBE_DEFER)
728 			return -EPROBE_DEFER;
729 		dev_info(&pdev->dev, "IRQ eth_wake_irq not found\n");
730 		stmmac_res->wol_irq = stmmac_res->irq;
731 	}
732 
733 	stmmac_res->lpi_irq =
734 		platform_get_irq_byname_optional(pdev, "eth_lpi");
735 	if (stmmac_res->lpi_irq < 0) {
736 		if (stmmac_res->lpi_irq == -EPROBE_DEFER)
737 			return -EPROBE_DEFER;
738 		dev_info(&pdev->dev, "IRQ eth_lpi not found\n");
739 	}
740 
741 	stmmac_res->addr = devm_platform_ioremap_resource(pdev, 0);
742 
743 	return PTR_ERR_OR_ZERO(stmmac_res->addr);
744 }
745 EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
746 
747 /**
748  * stmmac_pltfr_init
749  * @pdev: pointer to the platform device
750  * @plat: driver data platform structure
751  * Description: Call the platform's init callback (if any) and propagate
752  * the return value.
753  */
754 int stmmac_pltfr_init(struct platform_device *pdev,
755 		      struct plat_stmmacenet_data *plat)
756 {
757 	int ret = 0;
758 
759 	if (plat->init)
760 		ret = plat->init(pdev, plat->bsp_priv);
761 
762 	return ret;
763 }
764 EXPORT_SYMBOL_GPL(stmmac_pltfr_init);
765 
766 /**
767  * stmmac_pltfr_exit
768  * @pdev: pointer to the platform device
769  * @plat: driver data platform structure
770  * Description: Call the platform's exit callback (if any).
771  */
772 void stmmac_pltfr_exit(struct platform_device *pdev,
773 		       struct plat_stmmacenet_data *plat)
774 {
775 	if (plat->exit)
776 		plat->exit(pdev, plat->bsp_priv);
777 }
778 EXPORT_SYMBOL_GPL(stmmac_pltfr_exit);
779 
780 /**
781  * stmmac_pltfr_probe
782  * @pdev: platform device pointer
783  * @plat: driver data platform structure
784  * @res: stmmac resources structure
785  * Description: This calls the platform's init() callback and probes the
786  * stmmac driver.
787  */
788 int stmmac_pltfr_probe(struct platform_device *pdev,
789 		       struct plat_stmmacenet_data *plat,
790 		       struct stmmac_resources *res)
791 {
792 	int ret;
793 
794 	ret = stmmac_pltfr_init(pdev, plat);
795 	if (ret)
796 		return ret;
797 
798 	ret = stmmac_dvr_probe(&pdev->dev, plat, res);
799 	if (ret) {
800 		stmmac_pltfr_exit(pdev, plat);
801 		return ret;
802 	}
803 
804 	return ret;
805 }
806 EXPORT_SYMBOL_GPL(stmmac_pltfr_probe);
807 
808 static void devm_stmmac_pltfr_remove(void *data)
809 {
810 	struct platform_device *pdev = data;
811 
812 	stmmac_pltfr_remove_no_dt(pdev);
813 }
814 
815 /**
816  * devm_stmmac_pltfr_probe
817  * @pdev: pointer to the platform device
818  * @plat: driver data platform structure
819  * @res: stmmac resources
820  * Description: Devres variant of stmmac_pltfr_probe(). Allows users to skip
821  * calling stmmac_pltfr_remove() on driver detach.
822  */
823 int devm_stmmac_pltfr_probe(struct platform_device *pdev,
824 			    struct plat_stmmacenet_data *plat,
825 			    struct stmmac_resources *res)
826 {
827 	int ret;
828 
829 	ret = stmmac_pltfr_probe(pdev, plat, res);
830 	if (ret)
831 		return ret;
832 
833 	return devm_add_action_or_reset(&pdev->dev, devm_stmmac_pltfr_remove,
834 					pdev);
835 }
836 EXPORT_SYMBOL_GPL(devm_stmmac_pltfr_probe);
837 
838 /**
839  * stmmac_pltfr_remove_no_dt
840  * @pdev: pointer to the platform device
841  * Description: This undoes the effects of stmmac_pltfr_probe() by removing the
842  * driver and calling the platform's exit() callback.
843  */
844 void stmmac_pltfr_remove_no_dt(struct platform_device *pdev)
845 {
846 	struct net_device *ndev = platform_get_drvdata(pdev);
847 	struct stmmac_priv *priv = netdev_priv(ndev);
848 	struct plat_stmmacenet_data *plat = priv->plat;
849 
850 	stmmac_dvr_remove(&pdev->dev);
851 	stmmac_pltfr_exit(pdev, plat);
852 }
853 EXPORT_SYMBOL_GPL(stmmac_pltfr_remove_no_dt);
854 
855 /**
856  * stmmac_pltfr_remove
857  * @pdev: platform device pointer
858  * Description: this function calls the main to free the net resources
859  * and calls the platforms hook and release the resources (e.g. mem).
860  */
861 void stmmac_pltfr_remove(struct platform_device *pdev)
862 {
863 	struct net_device *ndev = platform_get_drvdata(pdev);
864 	struct stmmac_priv *priv = netdev_priv(ndev);
865 	struct plat_stmmacenet_data *plat = priv->plat;
866 
867 	stmmac_pltfr_remove_no_dt(pdev);
868 	stmmac_remove_config_dt(pdev, plat);
869 }
870 EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
871 
872 /**
873  * stmmac_pltfr_suspend
874  * @dev: device pointer
875  * Description: this function is invoked when suspend the driver and it direcly
876  * call the main suspend function and then, if required, on some platform, it
877  * can call an exit helper.
878  */
879 static int __maybe_unused stmmac_pltfr_suspend(struct device *dev)
880 {
881 	int ret;
882 	struct net_device *ndev = dev_get_drvdata(dev);
883 	struct stmmac_priv *priv = netdev_priv(ndev);
884 	struct platform_device *pdev = to_platform_device(dev);
885 
886 	ret = stmmac_suspend(dev);
887 	stmmac_pltfr_exit(pdev, priv->plat);
888 
889 	return ret;
890 }
891 
892 /**
893  * stmmac_pltfr_resume
894  * @dev: device pointer
895  * Description: this function is invoked when resume the driver before calling
896  * the main resume function, on some platforms, it can call own init helper
897  * if required.
898  */
899 static int __maybe_unused stmmac_pltfr_resume(struct device *dev)
900 {
901 	struct net_device *ndev = dev_get_drvdata(dev);
902 	struct stmmac_priv *priv = netdev_priv(ndev);
903 	struct platform_device *pdev = to_platform_device(dev);
904 	int ret;
905 
906 	ret = stmmac_pltfr_init(pdev, priv->plat->bsp_priv);
907 	if (ret)
908 		return ret;
909 
910 	return stmmac_resume(dev);
911 }
912 
913 static int __maybe_unused stmmac_runtime_suspend(struct device *dev)
914 {
915 	struct net_device *ndev = dev_get_drvdata(dev);
916 	struct stmmac_priv *priv = netdev_priv(ndev);
917 
918 	stmmac_bus_clks_config(priv, false);
919 
920 	return 0;
921 }
922 
923 static int __maybe_unused stmmac_runtime_resume(struct device *dev)
924 {
925 	struct net_device *ndev = dev_get_drvdata(dev);
926 	struct stmmac_priv *priv = netdev_priv(ndev);
927 
928 	return stmmac_bus_clks_config(priv, true);
929 }
930 
931 static int __maybe_unused stmmac_pltfr_noirq_suspend(struct device *dev)
932 {
933 	struct net_device *ndev = dev_get_drvdata(dev);
934 	struct stmmac_priv *priv = netdev_priv(ndev);
935 	int ret;
936 
937 	if (!netif_running(ndev))
938 		return 0;
939 
940 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
941 		/* Disable clock in case of PWM is off */
942 		clk_disable_unprepare(priv->plat->clk_ptp_ref);
943 
944 		ret = pm_runtime_force_suspend(dev);
945 		if (ret)
946 			return ret;
947 	}
948 
949 	return 0;
950 }
951 
952 static int __maybe_unused stmmac_pltfr_noirq_resume(struct device *dev)
953 {
954 	struct net_device *ndev = dev_get_drvdata(dev);
955 	struct stmmac_priv *priv = netdev_priv(ndev);
956 	int ret;
957 
958 	if (!netif_running(ndev))
959 		return 0;
960 
961 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
962 		/* enable the clk previously disabled */
963 		ret = pm_runtime_force_resume(dev);
964 		if (ret)
965 			return ret;
966 
967 		ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
968 		if (ret < 0) {
969 			netdev_warn(priv->dev,
970 				    "failed to enable PTP reference clock: %pe\n",
971 				    ERR_PTR(ret));
972 			return ret;
973 		}
974 	}
975 
976 	return 0;
977 }
978 
979 const struct dev_pm_ops stmmac_pltfr_pm_ops = {
980 	SET_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_suspend, stmmac_pltfr_resume)
981 	SET_RUNTIME_PM_OPS(stmmac_runtime_suspend, stmmac_runtime_resume, NULL)
982 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_noirq_suspend, stmmac_pltfr_noirq_resume)
983 };
984 EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
985 
986 MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
987 MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
988 MODULE_LICENSE("GPL");
989