xref: /openbmc/linux/drivers/net/ethernet/stmicro/stmmac/stmmac_platform.c (revision f6d73b12ca9fd3b1c29a6a725cd751b972c740cf)
14fa9c49fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2bfab27a1SGiuseppe CAVALLARO /*******************************************************************************
3bfab27a1SGiuseppe CAVALLARO   This contains the functions to handle the platform driver.
4bfab27a1SGiuseppe CAVALLARO 
5bfab27a1SGiuseppe CAVALLARO   Copyright (C) 2007-2011  STMicroelectronics Ltd
6bfab27a1SGiuseppe CAVALLARO 
7bfab27a1SGiuseppe CAVALLARO 
8bfab27a1SGiuseppe CAVALLARO   Author: Giuseppe Cavallaro <peppe.cavallaro@st.com>
9bfab27a1SGiuseppe CAVALLARO *******************************************************************************/
10bfab27a1SGiuseppe CAVALLARO 
11d7406542SBartosz Golaszewski #include <linux/device.h>
12bfab27a1SGiuseppe CAVALLARO #include <linux/platform_device.h>
13276aae37SJoakim Zhang #include <linux/pm_runtime.h>
14f0e9fc50SPaul Gortmaker #include <linux/module.h>
15bfab27a1SGiuseppe CAVALLARO #include <linux/io.h>
166a228452SStefan Roese #include <linux/of.h>
176a228452SStefan Roese #include <linux/of_net.h>
185790cf3cSMathieu Olivari #include <linux/of_mdio.h>
19f10f9fb2SAndy Shevchenko 
20bfab27a1SGiuseppe CAVALLARO #include "stmmac.h"
21f10f9fb2SAndy Shevchenko #include "stmmac_platform.h"
22bfab27a1SGiuseppe CAVALLARO 
236a228452SStefan Roese #ifdef CONFIG_OF
243b57de95SVince Bridgers 
25732fdf0eSGiuseppe CAVALLARO /**
26732fdf0eSGiuseppe CAVALLARO  * dwmac1000_validate_mcast_bins - validates the number of Multicast filter bins
27c3a502deSAndy Shevchenko  * @dev: struct device of the platform device
28732fdf0eSGiuseppe CAVALLARO  * @mcast_bins: Multicast filtering bins
29732fdf0eSGiuseppe CAVALLARO  * Description:
30732fdf0eSGiuseppe CAVALLARO  * this function validates the number of Multicast filtering bins specified
313b57de95SVince Bridgers  * by the configuration through the device tree. The Synopsys GMAC supports
323b57de95SVince Bridgers  * 64 bins, 128 bins, or 256 bins. "bins" refer to the division of CRC
333b57de95SVince Bridgers  * number space. 64 bins correspond to 6 bits of the CRC, 128 corresponds
343b57de95SVince Bridgers  * to 7 bits, and 256 refers to 8 bits of the CRC. Any other setting is
353b57de95SVince Bridgers  * invalid and will cause the filtering algorithm to use Multicast
363b57de95SVince Bridgers  * promiscuous mode.
373b57de95SVince Bridgers  */
dwmac1000_validate_mcast_bins(struct device * dev,int mcast_bins)38c3a502deSAndy Shevchenko static int dwmac1000_validate_mcast_bins(struct device *dev, int mcast_bins)
393b57de95SVince Bridgers {
403b57de95SVince Bridgers 	int x = mcast_bins;
413b57de95SVince Bridgers 
423b57de95SVince Bridgers 	switch (x) {
433b57de95SVince Bridgers 	case HASH_TABLE_SIZE:
443b57de95SVince Bridgers 	case 128:
453b57de95SVince Bridgers 	case 256:
463b57de95SVince Bridgers 		break;
473b57de95SVince Bridgers 	default:
483b57de95SVince Bridgers 		x = 0;
49c3a502deSAndy Shevchenko 		dev_info(dev, "Hash table entries set to unexpected value %d\n",
503b57de95SVince Bridgers 			 mcast_bins);
513b57de95SVince Bridgers 		break;
523b57de95SVince Bridgers 	}
533b57de95SVince Bridgers 	return x;
543b57de95SVince Bridgers }
553b57de95SVince Bridgers 
56732fdf0eSGiuseppe CAVALLARO /**
57732fdf0eSGiuseppe CAVALLARO  * dwmac1000_validate_ucast_entries - validate the Unicast address entries
58c3a502deSAndy Shevchenko  * @dev: struct device of the platform device
59732fdf0eSGiuseppe CAVALLARO  * @ucast_entries: number of Unicast address entries
60732fdf0eSGiuseppe CAVALLARO  * Description:
61732fdf0eSGiuseppe CAVALLARO  * This function validates the number of Unicast address entries supported
623b57de95SVince Bridgers  * by a particular Synopsys 10/100/1000 controller. The Synopsys controller
63edf2ef72SJongsung Kim  * supports 1..32, 64, or 128 Unicast filter entries for it's Unicast filter
643b57de95SVince Bridgers  * logic. This function validates a valid, supported configuration is
653b57de95SVince Bridgers  * selected, and defaults to 1 Unicast address if an unsupported
663b57de95SVince Bridgers  * configuration is selected.
673b57de95SVince Bridgers  */
dwmac1000_validate_ucast_entries(struct device * dev,int ucast_entries)68c3a502deSAndy Shevchenko static int dwmac1000_validate_ucast_entries(struct device *dev,
69c3a502deSAndy Shevchenko 					    int ucast_entries)
703b57de95SVince Bridgers {
713b57de95SVince Bridgers 	int x = ucast_entries;
723b57de95SVince Bridgers 
733b57de95SVince Bridgers 	switch (x) {
74edf2ef72SJongsung Kim 	case 1 ... 32:
753b57de95SVince Bridgers 	case 64:
763b57de95SVince Bridgers 	case 128:
773b57de95SVince Bridgers 		break;
783b57de95SVince Bridgers 	default:
793b57de95SVince Bridgers 		x = 1;
80c3a502deSAndy Shevchenko 		dev_info(dev, "Unicast table entries set to unexpected value %d\n",
813b57de95SVince Bridgers 			 ucast_entries);
823b57de95SVince Bridgers 		break;
833b57de95SVince Bridgers 	}
843b57de95SVince Bridgers 	return x;
853b57de95SVince Bridgers }
863b57de95SVince Bridgers 
87732fdf0eSGiuseppe CAVALLARO /**
88afea0365SGiuseppe Cavallaro  * stmmac_axi_setup - parse DT parameters for programming the AXI register
89afea0365SGiuseppe Cavallaro  * @pdev: platform device
90afea0365SGiuseppe Cavallaro  * Description:
91afea0365SGiuseppe Cavallaro  * if required, from device-tree the AXI internal register can be tuned
92afea0365SGiuseppe Cavallaro  * by using platform parameters.
93afea0365SGiuseppe Cavallaro  */
stmmac_axi_setup(struct platform_device * pdev)94afea0365SGiuseppe Cavallaro static struct stmmac_axi *stmmac_axi_setup(struct platform_device *pdev)
95afea0365SGiuseppe Cavallaro {
96afea0365SGiuseppe Cavallaro 	struct device_node *np;
97afea0365SGiuseppe Cavallaro 	struct stmmac_axi *axi;
98afea0365SGiuseppe Cavallaro 
99afea0365SGiuseppe Cavallaro 	np = of_parse_phandle(pdev->dev.of_node, "snps,axi-config", 0);
100afea0365SGiuseppe Cavallaro 	if (!np)
101afea0365SGiuseppe Cavallaro 		return NULL;
102afea0365SGiuseppe Cavallaro 
10364f48e59SJoao Pinto 	axi = devm_kzalloc(&pdev->dev, sizeof(*axi), GFP_KERNEL);
1044613b279SPeter Chen 	if (!axi) {
1054613b279SPeter Chen 		of_node_put(np);
106afea0365SGiuseppe Cavallaro 		return ERR_PTR(-ENOMEM);
1074613b279SPeter Chen 	}
108afea0365SGiuseppe Cavallaro 
109afea0365SGiuseppe Cavallaro 	axi->axi_lpi_en = of_property_read_bool(np, "snps,lpi_en");
110afea0365SGiuseppe Cavallaro 	axi->axi_xit_frm = of_property_read_bool(np, "snps,xit_frm");
11161d4f140SJisheng Zhang 	axi->axi_kbbe = of_property_read_bool(np, "snps,kbbe");
11261d4f140SJisheng Zhang 	axi->axi_fb = of_property_read_bool(np, "snps,fb");
11361d4f140SJisheng Zhang 	axi->axi_mb = of_property_read_bool(np, "snps,mb");
11461d4f140SJisheng Zhang 	axi->axi_rb =  of_property_read_bool(np, "snps,rb");
115afea0365SGiuseppe Cavallaro 
1166b3374cbSNiklas Cassel 	if (of_property_read_u32(np, "snps,wr_osr_lmt", &axi->axi_wr_osr_lmt))
1176b3374cbSNiklas Cassel 		axi->axi_wr_osr_lmt = 1;
1186b3374cbSNiklas Cassel 	if (of_property_read_u32(np, "snps,rd_osr_lmt", &axi->axi_rd_osr_lmt))
1196b3374cbSNiklas Cassel 		axi->axi_rd_osr_lmt = 1;
120afea0365SGiuseppe Cavallaro 	of_property_read_u32_array(np, "snps,blen", axi->axi_blen, AXI_BLEN);
1214613b279SPeter Chen 	of_node_put(np);
122afea0365SGiuseppe Cavallaro 
123afea0365SGiuseppe Cavallaro 	return axi;
124afea0365SGiuseppe Cavallaro }
125afea0365SGiuseppe Cavallaro 
126afea0365SGiuseppe Cavallaro /**
127d976a525SJoao Pinto  * stmmac_mtl_setup - parse DT parameters for multiple queues configuration
128d976a525SJoao Pinto  * @pdev: platform device
129d0ea5cbdSJesse Brandeburg  * @plat: enet data
130d976a525SJoao Pinto  */
stmmac_mtl_setup(struct platform_device * pdev,struct plat_stmmacenet_data * plat)1312ee2132fSNiklas Cassel static int stmmac_mtl_setup(struct platform_device *pdev,
132d976a525SJoao Pinto 			    struct plat_stmmacenet_data *plat)
133d976a525SJoao Pinto {
134d976a525SJoao Pinto 	struct device_node *q_node;
135d976a525SJoao Pinto 	struct device_node *rx_node;
136d976a525SJoao Pinto 	struct device_node *tx_node;
137d976a525SJoao Pinto 	u8 queue = 0;
1382ee2132fSNiklas Cassel 	int ret = 0;
139d976a525SJoao Pinto 
14033e85b8dSThierry Reding 	/* For backwards-compatibility with device trees that don't have any
14133e85b8dSThierry Reding 	 * snps,mtl-rx-config or snps,mtl-tx-config properties, we fall back
14233e85b8dSThierry Reding 	 * to one RX and TX queues each.
14333e85b8dSThierry Reding 	 */
14433e85b8dSThierry Reding 	plat->rx_queues_to_use = 1;
14533e85b8dSThierry Reding 	plat->tx_queues_to_use = 1;
14633e85b8dSThierry Reding 
1476d9f0790SJose Abreu 	/* First Queue must always be in DCB mode. As MTL_QUEUE_DCB = 1 we need
1486d9f0790SJose Abreu 	 * to always set this, otherwise Queue will be classified as AVB
1496d9f0790SJose Abreu 	 * (because MTL_QUEUE_AVB = 0).
1506d9f0790SJose Abreu 	 */
1516d9f0790SJose Abreu 	plat->rx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
1526d9f0790SJose Abreu 	plat->tx_queues_cfg[0].mode_to_use = MTL_QUEUE_DCB;
1536d9f0790SJose Abreu 
154d976a525SJoao Pinto 	rx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-rx-config", 0);
155d976a525SJoao Pinto 	if (!rx_node)
1562ee2132fSNiklas Cassel 		return ret;
157d976a525SJoao Pinto 
158d976a525SJoao Pinto 	tx_node = of_parse_phandle(pdev->dev.of_node, "snps,mtl-tx-config", 0);
159d976a525SJoao Pinto 	if (!tx_node) {
160d976a525SJoao Pinto 		of_node_put(rx_node);
1612ee2132fSNiklas Cassel 		return ret;
162d976a525SJoao Pinto 	}
163d976a525SJoao Pinto 
164d976a525SJoao Pinto 	/* Processing RX queues common config */
165e73b49ebSBhadram Varka 	if (of_property_read_u32(rx_node, "snps,rx-queues-to-use",
166d976a525SJoao Pinto 				 &plat->rx_queues_to_use))
167d976a525SJoao Pinto 		plat->rx_queues_to_use = 1;
168d976a525SJoao Pinto 
169d976a525SJoao Pinto 	if (of_property_read_bool(rx_node, "snps,rx-sched-sp"))
170d976a525SJoao Pinto 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
171d976a525SJoao Pinto 	else if (of_property_read_bool(rx_node, "snps,rx-sched-wsp"))
172d976a525SJoao Pinto 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_WSP;
173d976a525SJoao Pinto 	else
174d976a525SJoao Pinto 		plat->rx_sched_algorithm = MTL_RX_ALGORITHM_SP;
175d976a525SJoao Pinto 
176d976a525SJoao Pinto 	/* Processing individual RX queue config */
177d976a525SJoao Pinto 	for_each_child_of_node(rx_node, q_node) {
178d976a525SJoao Pinto 		if (queue >= plat->rx_queues_to_use)
179d976a525SJoao Pinto 			break;
180d976a525SJoao Pinto 
181d976a525SJoao Pinto 		if (of_property_read_bool(q_node, "snps,dcb-algorithm"))
18219d91873SJoao Pinto 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
183d976a525SJoao Pinto 		else if (of_property_read_bool(q_node, "snps,avb-algorithm"))
18419d91873SJoao Pinto 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
185d976a525SJoao Pinto 		else
18619d91873SJoao Pinto 			plat->rx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
187d976a525SJoao Pinto 
188e73b49ebSBhadram Varka 		if (of_property_read_u32(q_node, "snps,map-to-dma-channel",
189d976a525SJoao Pinto 					 &plat->rx_queues_cfg[queue].chan))
190d976a525SJoao Pinto 			plat->rx_queues_cfg[queue].chan = queue;
191d976a525SJoao Pinto 		/* TODO: Dynamic mapping to be included in the future */
192d976a525SJoao Pinto 
193a8f5102aSJoao Pinto 		if (of_property_read_u32(q_node, "snps,priority",
194a8f5102aSJoao Pinto 					&plat->rx_queues_cfg[queue].prio)) {
195a8f5102aSJoao Pinto 			plat->rx_queues_cfg[queue].prio = 0;
196a8f5102aSJoao Pinto 			plat->rx_queues_cfg[queue].use_prio = false;
197a8f5102aSJoao Pinto 		} else {
198a8f5102aSJoao Pinto 			plat->rx_queues_cfg[queue].use_prio = true;
199a8f5102aSJoao Pinto 		}
200a8f5102aSJoao Pinto 
201abe80fdcSJoao Pinto 		/* RX queue specific packet type routing */
202abe80fdcSJoao Pinto 		if (of_property_read_bool(q_node, "snps,route-avcp"))
203abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = PACKET_AVCPQ;
204abe80fdcSJoao Pinto 		else if (of_property_read_bool(q_node, "snps,route-ptp"))
205abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = PACKET_PTPQ;
206abe80fdcSJoao Pinto 		else if (of_property_read_bool(q_node, "snps,route-dcbcp"))
207abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = PACKET_DCBCPQ;
208abe80fdcSJoao Pinto 		else if (of_property_read_bool(q_node, "snps,route-up"))
209abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = PACKET_UPQ;
210abe80fdcSJoao Pinto 		else if (of_property_read_bool(q_node, "snps,route-multi-broad"))
211abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = PACKET_MCBCQ;
212abe80fdcSJoao Pinto 		else
213abe80fdcSJoao Pinto 			plat->rx_queues_cfg[queue].pkt_route = 0x0;
214abe80fdcSJoao Pinto 
215d976a525SJoao Pinto 		queue++;
216d976a525SJoao Pinto 	}
2172ee2132fSNiklas Cassel 	if (queue != plat->rx_queues_to_use) {
2182ee2132fSNiklas Cassel 		ret = -EINVAL;
2192ee2132fSNiklas Cassel 		dev_err(&pdev->dev, "Not all RX queues were configured\n");
2202ee2132fSNiklas Cassel 		goto out;
2212ee2132fSNiklas Cassel 	}
222d976a525SJoao Pinto 
223d976a525SJoao Pinto 	/* Processing TX queues common config */
224e73b49ebSBhadram Varka 	if (of_property_read_u32(tx_node, "snps,tx-queues-to-use",
225d976a525SJoao Pinto 				 &plat->tx_queues_to_use))
226d976a525SJoao Pinto 		plat->tx_queues_to_use = 1;
227d976a525SJoao Pinto 
228d976a525SJoao Pinto 	if (of_property_read_bool(tx_node, "snps,tx-sched-wrr"))
229d976a525SJoao Pinto 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WRR;
230d976a525SJoao Pinto 	else if (of_property_read_bool(tx_node, "snps,tx-sched-wfq"))
231d976a525SJoao Pinto 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_WFQ;
232d976a525SJoao Pinto 	else if (of_property_read_bool(tx_node, "snps,tx-sched-dwrr"))
233d976a525SJoao Pinto 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_DWRR;
234d976a525SJoao Pinto 	else
235d976a525SJoao Pinto 		plat->tx_sched_algorithm = MTL_TX_ALGORITHM_SP;
236d976a525SJoao Pinto 
237d976a525SJoao Pinto 	queue = 0;
238d976a525SJoao Pinto 
239d976a525SJoao Pinto 	/* Processing individual TX queue config */
240d976a525SJoao Pinto 	for_each_child_of_node(tx_node, q_node) {
241d976a525SJoao Pinto 		if (queue >= plat->tx_queues_to_use)
242d976a525SJoao Pinto 			break;
243d976a525SJoao Pinto 
244e73b49ebSBhadram Varka 		if (of_property_read_u32(q_node, "snps,weight",
245d976a525SJoao Pinto 					 &plat->tx_queues_cfg[queue].weight))
246d976a525SJoao Pinto 			plat->tx_queues_cfg[queue].weight = 0x10 + queue;
247d976a525SJoao Pinto 
24819d91873SJoao Pinto 		if (of_property_read_bool(q_node, "snps,dcb-algorithm")) {
24919d91873SJoao Pinto 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
25019d91873SJoao Pinto 		} else if (of_property_read_bool(q_node,
25119d91873SJoao Pinto 						 "snps,avb-algorithm")) {
25219d91873SJoao Pinto 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_AVB;
25319d91873SJoao Pinto 
25419d91873SJoao Pinto 			/* Credit Base Shaper parameters used by AVB */
25519d91873SJoao Pinto 			if (of_property_read_u32(q_node, "snps,send_slope",
25619d91873SJoao Pinto 				&plat->tx_queues_cfg[queue].send_slope))
25719d91873SJoao Pinto 				plat->tx_queues_cfg[queue].send_slope = 0x0;
25819d91873SJoao Pinto 			if (of_property_read_u32(q_node, "snps,idle_slope",
25919d91873SJoao Pinto 				&plat->tx_queues_cfg[queue].idle_slope))
26019d91873SJoao Pinto 				plat->tx_queues_cfg[queue].idle_slope = 0x0;
26119d91873SJoao Pinto 			if (of_property_read_u32(q_node, "snps,high_credit",
26219d91873SJoao Pinto 				&plat->tx_queues_cfg[queue].high_credit))
26319d91873SJoao Pinto 				plat->tx_queues_cfg[queue].high_credit = 0x0;
26419d91873SJoao Pinto 			if (of_property_read_u32(q_node, "snps,low_credit",
26519d91873SJoao Pinto 				&plat->tx_queues_cfg[queue].low_credit))
26619d91873SJoao Pinto 				plat->tx_queues_cfg[queue].low_credit = 0x0;
26719d91873SJoao Pinto 		} else {
26819d91873SJoao Pinto 			plat->tx_queues_cfg[queue].mode_to_use = MTL_QUEUE_DCB;
26919d91873SJoao Pinto 		}
27019d91873SJoao Pinto 
271a8f5102aSJoao Pinto 		if (of_property_read_u32(q_node, "snps,priority",
272a8f5102aSJoao Pinto 					&plat->tx_queues_cfg[queue].prio)) {
273a8f5102aSJoao Pinto 			plat->tx_queues_cfg[queue].prio = 0;
274a8f5102aSJoao Pinto 			plat->tx_queues_cfg[queue].use_prio = false;
275a8f5102aSJoao Pinto 		} else {
276a8f5102aSJoao Pinto 			plat->tx_queues_cfg[queue].use_prio = true;
277a8f5102aSJoao Pinto 		}
278a8f5102aSJoao Pinto 
279b643b836SRohan G Thomas 		plat->tx_queues_cfg[queue].coe_unsupported =
280b643b836SRohan G Thomas 			of_property_read_bool(q_node, "snps,coe-unsupported");
281b643b836SRohan G Thomas 
282d976a525SJoao Pinto 		queue++;
283d976a525SJoao Pinto 	}
2842ee2132fSNiklas Cassel 	if (queue != plat->tx_queues_to_use) {
2852ee2132fSNiklas Cassel 		ret = -EINVAL;
2862ee2132fSNiklas Cassel 		dev_err(&pdev->dev, "Not all TX queues were configured\n");
2872ee2132fSNiklas Cassel 		goto out;
2882ee2132fSNiklas Cassel 	}
289d976a525SJoao Pinto 
2902ee2132fSNiklas Cassel out:
291d976a525SJoao Pinto 	of_node_put(rx_node);
292d976a525SJoao Pinto 	of_node_put(tx_node);
293d976a525SJoao Pinto 	of_node_put(q_node);
2942ee2132fSNiklas Cassel 
2952ee2132fSNiklas Cassel 	return ret;
296d976a525SJoao Pinto }
297d976a525SJoao Pinto 
298d976a525SJoao Pinto /**
299a7657f12SGiuseppe CAVALLARO  * stmmac_dt_phy - parse device-tree driver parameters to allocate PHY resources
300a7657f12SGiuseppe CAVALLARO  * @plat: driver data platform structure
301a7657f12SGiuseppe CAVALLARO  * @np: device tree node
302a7657f12SGiuseppe CAVALLARO  * @dev: device pointer
303a7657f12SGiuseppe CAVALLARO  * Description:
304a7657f12SGiuseppe CAVALLARO  * The mdio bus will be allocated in case of a phy transceiver is on board;
305a7657f12SGiuseppe CAVALLARO  * it will be NULL if the fixed-link is configured.
306a7657f12SGiuseppe CAVALLARO  * If there is the "snps,dwmac-mdio" sub-node the mdio will be allocated
307a7657f12SGiuseppe CAVALLARO  * in any case (for DSA, mdio must be registered even if fixed-link).
308a7657f12SGiuseppe CAVALLARO  * The table below sums the supported configurations:
309a7657f12SGiuseppe CAVALLARO  *	-------------------------------
310a7657f12SGiuseppe CAVALLARO  *	snps,phy-addr	|     Y
311a7657f12SGiuseppe CAVALLARO  *	-------------------------------
312a7657f12SGiuseppe CAVALLARO  *	phy-handle	|     Y
313a7657f12SGiuseppe CAVALLARO  *	-------------------------------
314a7657f12SGiuseppe CAVALLARO  *	fixed-link	|     N
315a7657f12SGiuseppe CAVALLARO  *	-------------------------------
316a7657f12SGiuseppe CAVALLARO  *	snps,dwmac-mdio	|
317a7657f12SGiuseppe CAVALLARO  *	  even if	|     Y
318a7657f12SGiuseppe CAVALLARO  *	fixed-link	|
319a7657f12SGiuseppe CAVALLARO  *	-------------------------------
320a7657f12SGiuseppe CAVALLARO  *
321a7657f12SGiuseppe CAVALLARO  * It returns 0 in case of success otherwise -ENODEV.
322a7657f12SGiuseppe CAVALLARO  */
stmmac_dt_phy(struct plat_stmmacenet_data * plat,struct device_node * np,struct device * dev)323a7657f12SGiuseppe CAVALLARO static int stmmac_dt_phy(struct plat_stmmacenet_data *plat,
324a7657f12SGiuseppe CAVALLARO 			 struct device_node *np, struct device *dev)
325a7657f12SGiuseppe CAVALLARO {
326da29f2d8SJose Abreu 	bool mdio = !of_phy_is_fixed_link(np);
3279f93ac8dSLABBE Corentin 	static const struct of_device_id need_mdio_ids[] = {
3289f93ac8dSLABBE Corentin 		{ .compatible = "snps,dwc-qos-ethernet-4.10" },
329f0ef1f4fSThomas Meyer 		{},
3309f93ac8dSLABBE Corentin 	};
331a7657f12SGiuseppe CAVALLARO 
3329f93ac8dSLABBE Corentin 	if (of_match_node(need_mdio_ids, np)) {
333d8256121Sjpinto 		plat->mdio_node = of_get_child_by_name(np, "mdio");
334d8256121Sjpinto 	} else {
335d8256121Sjpinto 		/**
336d8256121Sjpinto 		 * If snps,dwmac-mdio is passed from DT, always register
337d8256121Sjpinto 		 * the MDIO
338d8256121Sjpinto 		 */
339a7657f12SGiuseppe CAVALLARO 		for_each_child_of_node(np, plat->mdio_node) {
340d8256121Sjpinto 			if (of_device_is_compatible(plat->mdio_node,
341d8256121Sjpinto 						    "snps,dwmac-mdio"))
342a7657f12SGiuseppe CAVALLARO 				break;
343a7657f12SGiuseppe CAVALLARO 		}
344d8256121Sjpinto 	}
345a7657f12SGiuseppe CAVALLARO 
346a7657f12SGiuseppe CAVALLARO 	if (plat->mdio_node) {
347a7657f12SGiuseppe CAVALLARO 		dev_dbg(dev, "Found MDIO subnode\n");
348a7657f12SGiuseppe CAVALLARO 		mdio = true;
349a7657f12SGiuseppe CAVALLARO 	}
350a7657f12SGiuseppe CAVALLARO 
3511a981c05SThierry Reding 	if (mdio) {
352a7657f12SGiuseppe CAVALLARO 		plat->mdio_bus_data =
353a7657f12SGiuseppe CAVALLARO 			devm_kzalloc(dev, sizeof(struct stmmac_mdio_bus_data),
354a7657f12SGiuseppe CAVALLARO 				     GFP_KERNEL);
3551a981c05SThierry Reding 		if (!plat->mdio_bus_data)
3561a981c05SThierry Reding 			return -ENOMEM;
3571a981c05SThierry Reding 
3581a981c05SThierry Reding 		plat->mdio_bus_data->needs_reset = true;
3591a981c05SThierry Reding 	}
3601a981c05SThierry Reding 
361a7657f12SGiuseppe CAVALLARO 	return 0;
362a7657f12SGiuseppe CAVALLARO }
363a7657f12SGiuseppe CAVALLARO 
364a7657f12SGiuseppe CAVALLARO /**
3650060c878SAlexandru Ardelean  * stmmac_of_get_mac_mode - retrieves the interface of the MAC
366d0ea5cbdSJesse Brandeburg  * @np: - device-tree node
3670060c878SAlexandru Ardelean  * Description:
3680060c878SAlexandru Ardelean  * Similar to `of_get_phy_mode()`, this function will retrieve (from
3690060c878SAlexandru Ardelean  * the device-tree) the interface mode on the MAC side. This assumes
3700060c878SAlexandru Ardelean  * that there is mode converter in-between the MAC & PHY
3710060c878SAlexandru Ardelean  * (e.g. GMII-to-RGMII).
3720060c878SAlexandru Ardelean  */
stmmac_of_get_mac_mode(struct device_node * np)3730060c878SAlexandru Ardelean static int stmmac_of_get_mac_mode(struct device_node *np)
3740060c878SAlexandru Ardelean {
3750060c878SAlexandru Ardelean 	const char *pm;
3760060c878SAlexandru Ardelean 	int err, i;
3770060c878SAlexandru Ardelean 
3780060c878SAlexandru Ardelean 	err = of_property_read_string(np, "mac-mode", &pm);
3790060c878SAlexandru Ardelean 	if (err < 0)
3800060c878SAlexandru Ardelean 		return err;
3810060c878SAlexandru Ardelean 
3820060c878SAlexandru Ardelean 	for (i = 0; i < PHY_INTERFACE_MODE_MAX; i++) {
3830060c878SAlexandru Ardelean 		if (!strcasecmp(pm, phy_modes(i)))
3840060c878SAlexandru Ardelean 			return i;
3850060c878SAlexandru Ardelean 	}
3860060c878SAlexandru Ardelean 
3870060c878SAlexandru Ardelean 	return -ENODEV;
3880060c878SAlexandru Ardelean }
3890060c878SAlexandru Ardelean 
3900060c878SAlexandru Ardelean /**
391732fdf0eSGiuseppe CAVALLARO  * stmmac_probe_config_dt - parse device-tree driver parameters
392732fdf0eSGiuseppe CAVALLARO  * @pdev: platform_device structure
393732fdf0eSGiuseppe CAVALLARO  * @mac: MAC address to use
394732fdf0eSGiuseppe CAVALLARO  * Description:
395732fdf0eSGiuseppe CAVALLARO  * this function is to read the driver parameters from device-tree and
396732fdf0eSGiuseppe CAVALLARO  * set some private fields that will be used by the main at runtime.
397732fdf0eSGiuseppe CAVALLARO  */
398402dae0bSJoachim Eastwood struct plat_stmmacenet_data *
stmmac_probe_config_dt(struct platform_device * pdev,u8 * mac)39983216e39SMichael Walle stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
4006a228452SStefan Roese {
4016a228452SStefan Roese 	struct device_node *np = pdev->dev.of_node;
4024ed2d8fcSJoachim Eastwood 	struct plat_stmmacenet_data *plat;
40325c83b5cSSrinivas Kandagatla 	struct stmmac_dma_cfg *dma_cfg;
404eca81f09SYueHaibing 	int phy_mode;
405bb3222f7SJisheng Zhang 	void *ret;
4062ee2132fSNiklas Cassel 	int rc;
4076a228452SStefan Roese 
4084ed2d8fcSJoachim Eastwood 	plat = devm_kzalloc(&pdev->dev, sizeof(*plat), GFP_KERNEL);
4094ed2d8fcSJoachim Eastwood 	if (!plat)
410b0003eadSJoachim Eastwood 		return ERR_PTR(-ENOMEM);
4114ed2d8fcSJoachim Eastwood 
41283216e39SMichael Walle 	rc = of_get_mac_address(np, mac);
41383216e39SMichael Walle 	if (rc) {
41483216e39SMichael Walle 		if (rc == -EPROBE_DEFER)
41583216e39SMichael Walle 			return ERR_PTR(rc);
416195b2919SMartin Blumenstingl 
41783216e39SMichael Walle 		eth_zero_addr(mac);
418195b2919SMartin Blumenstingl 	}
419195b2919SMartin Blumenstingl 
420eca81f09SYueHaibing 	phy_mode = device_get_phy_mode(&pdev->dev);
421eca81f09SYueHaibing 	if (phy_mode < 0)
422eca81f09SYueHaibing 		return ERR_PTR(phy_mode);
4230060c878SAlexandru Ardelean 
424eca81f09SYueHaibing 	plat->phy_interface = phy_mode;
425b5947239SRussell King (Oracle) 	rc = stmmac_of_get_mac_mode(np);
426b5947239SRussell King (Oracle) 	plat->mac_interface = rc < 0 ? plat->phy_interface : rc;
4274838a540SJose Abreu 
4284838a540SJose Abreu 	/* Some wrapper drivers still rely on phy_node. Let's save it while
4294838a540SJose Abreu 	 * they are not converted to phylink. */
4304838a540SJose Abreu 	plat->phy_node = of_parse_phandle(np, "phy-handle", 0);
4314838a540SJose Abreu 
4324838a540SJose Abreu 	/* PHYLINK automatically parses the phy-handle property */
433e80af2acSRussell King (Oracle) 	plat->port_node = of_fwnode_handle(np);
43425c83b5cSSrinivas Kandagatla 
4359cbadf09SSrinivas Kandagatla 	/* Get max speed of operation from device tree */
436c21cabb0SChen-Yu Tsai 	of_property_read_u32(np, "max-speed", &plat->max_speed);
4379cbadf09SSrinivas Kandagatla 
43825c83b5cSSrinivas Kandagatla 	plat->bus_id = of_alias_get_id(np, "ethernet");
43925c83b5cSSrinivas Kandagatla 	if (plat->bus_id < 0)
44025c83b5cSSrinivas Kandagatla 		plat->bus_id = 0;
44125c83b5cSSrinivas Kandagatla 
442436f7ecdSChen-Yu Tsai 	/* Default to phy auto-detection */
443436f7ecdSChen-Yu Tsai 	plat->phy_addr = -1;
444436f7ecdSChen-Yu Tsai 
445c64655f3SBhupesh Sharma 	/* Default to get clk_csr from stmmac_clk_csr_set(),
4465e7f7fc5SBiao Huang 	 * or get clk_csr from device tree.
4475e7f7fc5SBiao Huang 	 */
4485e7f7fc5SBiao Huang 	plat->clk_csr = -1;
44983936ea8SJianguo Zhang 	if (of_property_read_u32(np, "snps,clk-csr", &plat->clk_csr))
45081311c03SChristophe Roullier 		of_property_read_u32(np, "clk_csr", &plat->clk_csr);
45181311c03SChristophe Roullier 
452436f7ecdSChen-Yu Tsai 	/* "snps,phy-addr" is not a standard property. Mark it as deprecated
453436f7ecdSChen-Yu Tsai 	 * and warn of its use. Remove this when phy node support is added.
454436f7ecdSChen-Yu Tsai 	 */
455436f7ecdSChen-Yu Tsai 	if (of_property_read_u32(np, "snps,phy-addr", &plat->phy_addr) == 0)
456436f7ecdSChen-Yu Tsai 		dev_warn(&pdev->dev, "snps,phy-addr property is deprecated\n");
45725c83b5cSSrinivas Kandagatla 
458a7657f12SGiuseppe CAVALLARO 	/* To Configure PHY by using all device-tree supported properties */
459ce339abcSNiklas Cassel 	rc = stmmac_dt_phy(plat, np, &pdev->dev);
460ce339abcSNiklas Cassel 	if (rc)
461ce339abcSNiklas Cassel 		return ERR_PTR(rc);
4626a228452SStefan Roese 
463e7877f52SVince Bridgers 	of_property_read_u32(np, "tx-fifo-depth", &plat->tx_fifo_size);
464e7877f52SVince Bridgers 
465e7877f52SVince Bridgers 	of_property_read_u32(np, "rx-fifo-depth", &plat->rx_fifo_size);
466e7877f52SVince Bridgers 
4678c2a7a5dSGiuseppe CAVALLARO 	plat->force_sf_dma_mode =
4688c2a7a5dSGiuseppe CAVALLARO 		of_property_read_bool(np, "snps,force_sf_dma_mode");
4696aedb8c0SChen-Yu Tsai 
4709d0c0d5eSBartosz Golaszewski 	if (of_property_read_bool(np, "snps,en-tx-lpi-clockgating"))
4719d0c0d5eSBartosz Golaszewski 		plat->flags |= STMMAC_FLAG_EN_TX_LPI_CLOCKGATING;
472b4b7b772Sjpinto 
4732618abb7SVince Bridgers 	/* Set the maxmtu to a default of JUMBO_LEN in case the
4742618abb7SVince Bridgers 	 * parameter is not present in the device tree.
4752618abb7SVince Bridgers 	 */
4762618abb7SVince Bridgers 	plat->maxmtu = JUMBO_LEN;
4772618abb7SVince Bridgers 
4784ed2d8fcSJoachim Eastwood 	/* Set default value for multicast hash bins */
4794ed2d8fcSJoachim Eastwood 	plat->multicast_filter_bins = HASH_TABLE_SIZE;
4804ed2d8fcSJoachim Eastwood 
4814ed2d8fcSJoachim Eastwood 	/* Set default value for unicast filter entries */
4824ed2d8fcSJoachim Eastwood 	plat->unicast_filter_entries = 1;
4834ed2d8fcSJoachim Eastwood 
4846a228452SStefan Roese 	/*
4856a228452SStefan Roese 	 * Currently only the properties needed on SPEAr600
4866a228452SStefan Roese 	 * are provided. All other properties should be added
4876a228452SStefan Roese 	 * once needed on other platforms.
4886a228452SStefan Roese 	 */
48984c9f8c4SDinh Nguyen 	if (of_device_is_compatible(np, "st,spear600-gmac") ||
490f9a09687SAlexandre TORGUE 		of_device_is_compatible(np, "snps,dwmac-3.50a") ||
49184c9f8c4SDinh Nguyen 		of_device_is_compatible(np, "snps,dwmac-3.70a") ||
49284c9f8c4SDinh Nguyen 		of_device_is_compatible(np, "snps,dwmac")) {
4932618abb7SVince Bridgers 		/* Note that the max-frame-size parameter as defined in the
4942618abb7SVince Bridgers 		 * ePAPR v1.1 spec is defined as max-frame-size, it's
4952618abb7SVince Bridgers 		 * actually used as the IEEE definition of MAC Client
4962618abb7SVince Bridgers 		 * data, or MTU. The ePAPR specification is confusing as
4972618abb7SVince Bridgers 		 * the definition is max-frame-size, but usage examples
4982618abb7SVince Bridgers 		 * are clearly MTUs
4992618abb7SVince Bridgers 		 */
5002618abb7SVince Bridgers 		of_property_read_u32(np, "max-frame-size", &plat->maxmtu);
5013b57de95SVince Bridgers 		of_property_read_u32(np, "snps,multicast-filter-bins",
5023b57de95SVince Bridgers 				     &plat->multicast_filter_bins);
5033b57de95SVince Bridgers 		of_property_read_u32(np, "snps,perfect-filter-entries",
5043b57de95SVince Bridgers 				     &plat->unicast_filter_entries);
5053b57de95SVince Bridgers 		plat->unicast_filter_entries = dwmac1000_validate_ucast_entries(
506c3a502deSAndy Shevchenko 				&pdev->dev, plat->unicast_filter_entries);
5073b57de95SVince Bridgers 		plat->multicast_filter_bins = dwmac1000_validate_mcast_bins(
508c3a502deSAndy Shevchenko 				&pdev->dev, plat->multicast_filter_bins);
5096a228452SStefan Roese 		plat->has_gmac = 1;
5106a228452SStefan Roese 		plat->pmt = 1;
5116a228452SStefan Roese 	}
5126a228452SStefan Roese 
5139cb1d19fSHerve Codina 	if (of_device_is_compatible(np, "snps,dwmac-3.40a")) {
5149cb1d19fSHerve Codina 		plat->has_gmac = 1;
5159cb1d19fSHerve Codina 		plat->enh_desc = 1;
5169cb1d19fSHerve Codina 		plat->tx_coe = 1;
5179cb1d19fSHerve Codina 		plat->bugged_jumbo = 1;
5189cb1d19fSHerve Codina 		plat->pmt = 1;
5199cb1d19fSHerve Codina 	}
5209cb1d19fSHerve Codina 
521ee2ae1edSAlexandre TORGUE 	if (of_device_is_compatible(np, "snps,dwmac-4.00") ||
522026e5758SChristophe Roullier 	    of_device_is_compatible(np, "snps,dwmac-4.10a") ||
523139df98bSFugang Duan 	    of_device_is_compatible(np, "snps,dwmac-4.20a") ||
52465a1d72fSEmil Renner Berthing 	    of_device_is_compatible(np, "snps,dwmac-5.10a") ||
52565a1d72fSEmil Renner Berthing 	    of_device_is_compatible(np, "snps,dwmac-5.20")) {
526ee2ae1edSAlexandre TORGUE 		plat->has_gmac4 = 1;
5277cc99fd2SNiklas Cassel 		plat->has_gmac = 0;
528ee2ae1edSAlexandre TORGUE 		plat->pmt = 1;
52968861a3bSBartosz Golaszewski 		if (of_property_read_bool(np, "snps,tso"))
53068861a3bSBartosz Golaszewski 			plat->flags |= STMMAC_FLAG_TSO_EN;
531ee2ae1edSAlexandre TORGUE 	}
532ee2ae1edSAlexandre TORGUE 
53325c83b5cSSrinivas Kandagatla 	if (of_device_is_compatible(np, "snps,dwmac-3.610") ||
53425c83b5cSSrinivas Kandagatla 		of_device_is_compatible(np, "snps,dwmac-3.710")) {
53525c83b5cSSrinivas Kandagatla 		plat->enh_desc = 1;
53625c83b5cSSrinivas Kandagatla 		plat->bugged_jumbo = 1;
53725c83b5cSSrinivas Kandagatla 		plat->force_sf_dma_mode = 1;
53825c83b5cSSrinivas Kandagatla 	}
53925c83b5cSSrinivas Kandagatla 
540a3f14247SJose Abreu 	if (of_device_is_compatible(np, "snps,dwxgmac")) {
541a3f14247SJose Abreu 		plat->has_xgmac = 1;
542a3f14247SJose Abreu 		plat->pmt = 1;
54368861a3bSBartosz Golaszewski 		if (of_property_read_bool(np, "snps,tso"))
54468861a3bSBartosz Golaszewski 			plat->flags |= STMMAC_FLAG_TSO_EN;
545a3f14247SJose Abreu 	}
546a3f14247SJose Abreu 
54764c3b252SByungho An 	dma_cfg = devm_kzalloc(&pdev->dev, sizeof(*dma_cfg),
54864c3b252SByungho An 			       GFP_KERNEL);
54927732381SMathieu Olivari 	if (!dma_cfg) {
550d2ed0a77SJohan Hovold 		stmmac_remove_config_dt(pdev, plat);
551b0003eadSJoachim Eastwood 		return ERR_PTR(-ENOMEM);
55227732381SMathieu Olivari 	}
55325c83b5cSSrinivas Kandagatla 	plat->dma_cfg = dma_cfg;
554a332e2faSNiklas Cassel 
55525c83b5cSSrinivas Kandagatla 	of_property_read_u32(np, "snps,pbl", &dma_cfg->pbl);
556a332e2faSNiklas Cassel 	if (!dma_cfg->pbl)
557a332e2faSNiklas Cassel 		dma_cfg->pbl = DEFAULT_DMA_PBL;
55889caaa2dSNiklas Cassel 	of_property_read_u32(np, "snps,txpbl", &dma_cfg->txpbl);
55989caaa2dSNiklas Cassel 	of_property_read_u32(np, "snps,rxpbl", &dma_cfg->rxpbl);
5604022d039SNiklas Cassel 	dma_cfg->pblx8 = !of_property_read_bool(np, "snps,no-pbl-x8");
561a332e2faSNiklas Cassel 
562afea0365SGiuseppe Cavallaro 	dma_cfg->aal = of_property_read_bool(np, "snps,aal");
563a332e2faSNiklas Cassel 	dma_cfg->fixed_burst = of_property_read_bool(np, "snps,fixed-burst");
564a332e2faSNiklas Cassel 	dma_cfg->mixed_burst = of_property_read_bool(np, "snps,mixed-burst");
565a332e2faSNiklas Cassel 
566e2a240c7SSonic Zhang 	plat->force_thresh_dma_mode = of_property_read_bool(np, "snps,force_thresh_dma_mode");
56705d7623aSCristian Ciocaltea 	if (plat->force_thresh_dma_mode && plat->force_sf_dma_mode) {
568e2a240c7SSonic Zhang 		plat->force_sf_dma_mode = 0;
569c3a502deSAndy Shevchenko 		dev_warn(&pdev->dev,
570c3a502deSAndy Shevchenko 			 "force_sf_dma_mode is ignored if force_thresh_dma_mode is set.\n");
571356f9e74SOlof Johansson 	}
57225c83b5cSSrinivas Kandagatla 
57302e57b9dSGiuseppe CAVALLARO 	of_property_read_u32(np, "snps,ps-speed", &plat->mac_port_sel_speed);
57402e57b9dSGiuseppe CAVALLARO 
575afea0365SGiuseppe Cavallaro 	plat->axi = stmmac_axi_setup(pdev);
576afea0365SGiuseppe Cavallaro 
5772ee2132fSNiklas Cassel 	rc = stmmac_mtl_setup(pdev, plat);
5782ee2132fSNiklas Cassel 	if (rc) {
5792ee2132fSNiklas Cassel 		stmmac_remove_config_dt(pdev, plat);
5802ee2132fSNiklas Cassel 		return ERR_PTR(rc);
5812ee2132fSNiklas Cassel 	}
582d976a525SJoao Pinto 
583f573c0b9Sjpinto 	/* clock setup */
584ddfbee9eSThierry Reding 	if (!of_device_is_compatible(np, "snps,dwc-qos-ethernet-4.10")) {
585f573c0b9Sjpinto 		plat->stmmac_clk = devm_clk_get(&pdev->dev,
586f573c0b9Sjpinto 						STMMAC_RESOURCE_NAME);
587f573c0b9Sjpinto 		if (IS_ERR(plat->stmmac_clk)) {
588f573c0b9Sjpinto 			dev_warn(&pdev->dev, "Cannot get CSR clock\n");
589f573c0b9Sjpinto 			plat->stmmac_clk = NULL;
590f573c0b9Sjpinto 		}
591f573c0b9Sjpinto 		clk_prepare_enable(plat->stmmac_clk);
592ddfbee9eSThierry Reding 	}
593f573c0b9Sjpinto 
594bb3222f7SJisheng Zhang 	plat->pclk = devm_clk_get_optional(&pdev->dev, "pclk");
595f573c0b9Sjpinto 	if (IS_ERR(plat->pclk)) {
596bb3222f7SJisheng Zhang 		ret = plat->pclk;
597f573c0b9Sjpinto 		goto error_pclk_get;
598f573c0b9Sjpinto 	}
599f573c0b9Sjpinto 	clk_prepare_enable(plat->pclk);
600f573c0b9Sjpinto 
601f573c0b9Sjpinto 	/* Fall-back to main clock in case of no PTP ref is passed */
6029fbb9dd8SThierry Reding 	plat->clk_ptp_ref = devm_clk_get(&pdev->dev, "ptp_ref");
603f573c0b9Sjpinto 	if (IS_ERR(plat->clk_ptp_ref)) {
604f573c0b9Sjpinto 		plat->clk_ptp_rate = clk_get_rate(plat->stmmac_clk);
605f573c0b9Sjpinto 		plat->clk_ptp_ref = NULL;
60607cc79efSAhmad Fatoum 		dev_info(&pdev->dev, "PTP uses main clock\n");
607f573c0b9Sjpinto 	} else {
608f573c0b9Sjpinto 		plat->clk_ptp_rate = clk_get_rate(plat->clk_ptp_ref);
609fd3984e6SHeiner Kallweit 		dev_dbg(&pdev->dev, "PTP rate %d\n", plat->clk_ptp_rate);
610f573c0b9Sjpinto 	}
611f573c0b9Sjpinto 
612bb3222f7SJisheng Zhang 	plat->stmmac_rst = devm_reset_control_get_optional(&pdev->dev,
613f573c0b9Sjpinto 							   STMMAC_RESOURCE_NAME);
614f573c0b9Sjpinto 	if (IS_ERR(plat->stmmac_rst)) {
615bb3222f7SJisheng Zhang 		ret = plat->stmmac_rst;
616f573c0b9Sjpinto 		goto error_hw_init;
617f573c0b9Sjpinto 	}
618f573c0b9Sjpinto 
619e67f325eSMatthew Hagan 	plat->stmmac_ahb_rst = devm_reset_control_get_optional_shared(
620e67f325eSMatthew Hagan 							&pdev->dev, "ahb");
621e67f325eSMatthew Hagan 	if (IS_ERR(plat->stmmac_ahb_rst)) {
622e67f325eSMatthew Hagan 		ret = plat->stmmac_ahb_rst;
623e67f325eSMatthew Hagan 		goto error_hw_init;
624e67f325eSMatthew Hagan 	}
625e67f325eSMatthew Hagan 
626b0003eadSJoachim Eastwood 	return plat;
627f573c0b9Sjpinto 
628f573c0b9Sjpinto error_hw_init:
629f573c0b9Sjpinto 	clk_disable_unprepare(plat->pclk);
630f573c0b9Sjpinto error_pclk_get:
631f573c0b9Sjpinto 	clk_disable_unprepare(plat->stmmac_clk);
632f573c0b9Sjpinto 
633bb3222f7SJisheng Zhang 	return ret;
6346a228452SStefan Roese }
635d2ed0a77SJohan Hovold 
devm_stmmac_remove_config_dt(void * data)636d7406542SBartosz Golaszewski static void devm_stmmac_remove_config_dt(void *data)
637d7406542SBartosz Golaszewski {
638d7406542SBartosz Golaszewski 	struct plat_stmmacenet_data *plat = data;
639d7406542SBartosz Golaszewski 
640d7406542SBartosz Golaszewski 	/* Platform data argument is unused */
641d7406542SBartosz Golaszewski 	stmmac_remove_config_dt(NULL, plat);
642d7406542SBartosz Golaszewski }
643d7406542SBartosz Golaszewski 
644d7406542SBartosz Golaszewski /**
645d7406542SBartosz Golaszewski  * devm_stmmac_probe_config_dt
646d7406542SBartosz Golaszewski  * @pdev: platform_device structure
647d7406542SBartosz Golaszewski  * @mac: MAC address to use
648d7406542SBartosz Golaszewski  * Description: Devres variant of stmmac_probe_config_dt(). Does not require
649d7406542SBartosz Golaszewski  * the user to call stmmac_remove_config_dt() at driver detach.
650d7406542SBartosz Golaszewski  */
651d7406542SBartosz Golaszewski struct plat_stmmacenet_data *
devm_stmmac_probe_config_dt(struct platform_device * pdev,u8 * mac)652d7406542SBartosz Golaszewski devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
653d7406542SBartosz Golaszewski {
654d7406542SBartosz Golaszewski 	struct plat_stmmacenet_data *plat;
655d7406542SBartosz Golaszewski 	int ret;
656d7406542SBartosz Golaszewski 
657d7406542SBartosz Golaszewski 	plat = stmmac_probe_config_dt(pdev, mac);
658d7406542SBartosz Golaszewski 	if (IS_ERR(plat))
659d7406542SBartosz Golaszewski 		return plat;
660d7406542SBartosz Golaszewski 
661d7406542SBartosz Golaszewski 	ret = devm_add_action_or_reset(&pdev->dev,
662d7406542SBartosz Golaszewski 				       devm_stmmac_remove_config_dt, plat);
663d7406542SBartosz Golaszewski 	if (ret)
664d7406542SBartosz Golaszewski 		return ERR_PTR(ret);
665d7406542SBartosz Golaszewski 
666d7406542SBartosz Golaszewski 	return plat;
667d7406542SBartosz Golaszewski }
668d7406542SBartosz Golaszewski 
669d2ed0a77SJohan Hovold /**
670d2ed0a77SJohan Hovold  * stmmac_remove_config_dt - undo the effects of stmmac_probe_config_dt()
671d2ed0a77SJohan Hovold  * @pdev: platform_device structure
672d2ed0a77SJohan Hovold  * @plat: driver data platform structure
673d2ed0a77SJohan Hovold  *
674d2ed0a77SJohan Hovold  * Release resources claimed by stmmac_probe_config_dt().
675d2ed0a77SJohan Hovold  */
stmmac_remove_config_dt(struct platform_device * pdev,struct plat_stmmacenet_data * plat)676d2ed0a77SJohan Hovold void stmmac_remove_config_dt(struct platform_device *pdev,
677d2ed0a77SJohan Hovold 			     struct plat_stmmacenet_data *plat)
678d2ed0a77SJohan Hovold {
6798f269102SJoakim Zhang 	clk_disable_unprepare(plat->stmmac_clk);
6808f269102SJoakim Zhang 	clk_disable_unprepare(plat->pclk);
6814838a540SJose Abreu 	of_node_put(plat->phy_node);
682a249708bSJulia Lawall 	of_node_put(plat->mdio_node);
683d2ed0a77SJohan Hovold }
6846a228452SStefan Roese #else
685402dae0bSJoachim Eastwood struct plat_stmmacenet_data *
stmmac_probe_config_dt(struct platform_device * pdev,u8 * mac)68683216e39SMichael Walle stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
6876a228452SStefan Roese {
688b2a8315aSLABBE Corentin 	return ERR_PTR(-EINVAL);
6896a228452SStefan Roese }
690d2ed0a77SJohan Hovold 
691d7406542SBartosz Golaszewski struct plat_stmmacenet_data *
devm_stmmac_probe_config_dt(struct platform_device * pdev,u8 * mac)692d7406542SBartosz Golaszewski devm_stmmac_probe_config_dt(struct platform_device *pdev, u8 *mac)
693d7406542SBartosz Golaszewski {
694d7406542SBartosz Golaszewski 	return ERR_PTR(-EINVAL);
695d7406542SBartosz Golaszewski }
696d7406542SBartosz Golaszewski 
stmmac_remove_config_dt(struct platform_device * pdev,struct plat_stmmacenet_data * plat)697d2ed0a77SJohan Hovold void stmmac_remove_config_dt(struct platform_device *pdev,
698d2ed0a77SJohan Hovold 			     struct plat_stmmacenet_data *plat)
699d2ed0a77SJohan Hovold {
700d2ed0a77SJohan Hovold }
7016a228452SStefan Roese #endif /* CONFIG_OF */
702402dae0bSJoachim Eastwood EXPORT_SYMBOL_GPL(stmmac_probe_config_dt);
703d7406542SBartosz Golaszewski EXPORT_SYMBOL_GPL(devm_stmmac_probe_config_dt);
704d2ed0a77SJohan Hovold EXPORT_SYMBOL_GPL(stmmac_remove_config_dt);
7056a228452SStefan Roese 
stmmac_get_platform_resources(struct platform_device * pdev,struct stmmac_resources * stmmac_res)706402dae0bSJoachim Eastwood int stmmac_get_platform_resources(struct platform_device *pdev,
707f396cb01SJoachim Eastwood 				  struct stmmac_resources *stmmac_res)
708bfab27a1SGiuseppe CAVALLARO {
709f396cb01SJoachim Eastwood 	memset(stmmac_res, 0, sizeof(*stmmac_res));
7108f02d8daSAlexey Brodkin 
7118f02d8daSAlexey Brodkin 	/* Get IRQ information early to have an ability to ask for deferred
7128f02d8daSAlexey Brodkin 	 * probe if needed before we went too far with resource allocation.
7138f02d8daSAlexey Brodkin 	 */
714f396cb01SJoachim Eastwood 	stmmac_res->irq = platform_get_irq_byname(pdev, "macirq");
715d1a55841SStephen Boyd 	if (stmmac_res->irq < 0)
716f396cb01SJoachim Eastwood 		return stmmac_res->irq;
7178f02d8daSAlexey Brodkin 
7188f02d8daSAlexey Brodkin 	/* On some platforms e.g. SPEAr the wake up irq differs from the mac irq
7198f02d8daSAlexey Brodkin 	 * The external wake up irq can be passed through the platform code
7208f02d8daSAlexey Brodkin 	 * named as "eth_wake_irq"
7218f02d8daSAlexey Brodkin 	 *
7228f02d8daSAlexey Brodkin 	 * In case the wake up interrupt is not passed from the platform
7238f02d8daSAlexey Brodkin 	 * so the driver will continue to use the mac irq (ndev->irq)
7248f02d8daSAlexey Brodkin 	 */
725fc191af1SMarkus Fuchs 	stmmac_res->wol_irq =
726fc191af1SMarkus Fuchs 		platform_get_irq_byname_optional(pdev, "eth_wake_irq");
727f396cb01SJoachim Eastwood 	if (stmmac_res->wol_irq < 0) {
728f396cb01SJoachim Eastwood 		if (stmmac_res->wol_irq == -EPROBE_DEFER)
7298f02d8daSAlexey Brodkin 			return -EPROBE_DEFER;
730fc191af1SMarkus Fuchs 		dev_info(&pdev->dev, "IRQ eth_wake_irq not found\n");
731f396cb01SJoachim Eastwood 		stmmac_res->wol_irq = stmmac_res->irq;
7328f02d8daSAlexey Brodkin 	}
7338f02d8daSAlexey Brodkin 
734fc191af1SMarkus Fuchs 	stmmac_res->lpi_irq =
735fc191af1SMarkus Fuchs 		platform_get_irq_byname_optional(pdev, "eth_lpi");
736fc191af1SMarkus Fuchs 	if (stmmac_res->lpi_irq < 0) {
737f396cb01SJoachim Eastwood 		if (stmmac_res->lpi_irq == -EPROBE_DEFER)
7388f02d8daSAlexey Brodkin 			return -EPROBE_DEFER;
739fc191af1SMarkus Fuchs 		dev_info(&pdev->dev, "IRQ eth_lpi not found\n");
740fc191af1SMarkus Fuchs 	}
741bfab27a1SGiuseppe CAVALLARO 
7427a1d0e61SDejin Zheng 	stmmac_res->addr = devm_platform_ioremap_resource(pdev, 0);
743f396cb01SJoachim Eastwood 
744a04c0aefSFengguang Wu 	return PTR_ERR_OR_ZERO(stmmac_res->addr);
745f396cb01SJoachim Eastwood }
746402dae0bSJoachim Eastwood EXPORT_SYMBOL_GPL(stmmac_get_platform_resources);
747f396cb01SJoachim Eastwood 
748f396cb01SJoachim Eastwood /**
74997117eb5SBartosz Golaszewski  * stmmac_pltfr_init
75097117eb5SBartosz Golaszewski  * @pdev: pointer to the platform device
75197117eb5SBartosz Golaszewski  * @plat: driver data platform structure
75297117eb5SBartosz Golaszewski  * Description: Call the platform's init callback (if any) and propagate
75397117eb5SBartosz Golaszewski  * the return value.
75497117eb5SBartosz Golaszewski  */
stmmac_pltfr_init(struct platform_device * pdev,struct plat_stmmacenet_data * plat)75597117eb5SBartosz Golaszewski int stmmac_pltfr_init(struct platform_device *pdev,
75697117eb5SBartosz Golaszewski 		      struct plat_stmmacenet_data *plat)
75797117eb5SBartosz Golaszewski {
75897117eb5SBartosz Golaszewski 	int ret = 0;
75997117eb5SBartosz Golaszewski 
76097117eb5SBartosz Golaszewski 	if (plat->init)
76197117eb5SBartosz Golaszewski 		ret = plat->init(pdev, plat->bsp_priv);
76297117eb5SBartosz Golaszewski 
76397117eb5SBartosz Golaszewski 	return ret;
76497117eb5SBartosz Golaszewski }
76597117eb5SBartosz Golaszewski EXPORT_SYMBOL_GPL(stmmac_pltfr_init);
76697117eb5SBartosz Golaszewski 
76797117eb5SBartosz Golaszewski /**
7685b0acf8dSBartosz Golaszewski  * stmmac_pltfr_exit
7695b0acf8dSBartosz Golaszewski  * @pdev: pointer to the platform device
7705b0acf8dSBartosz Golaszewski  * @plat: driver data platform structure
7715b0acf8dSBartosz Golaszewski  * Description: Call the platform's exit callback (if any).
7725b0acf8dSBartosz Golaszewski  */
stmmac_pltfr_exit(struct platform_device * pdev,struct plat_stmmacenet_data * plat)7735b0acf8dSBartosz Golaszewski void stmmac_pltfr_exit(struct platform_device *pdev,
7745b0acf8dSBartosz Golaszewski 		       struct plat_stmmacenet_data *plat)
7755b0acf8dSBartosz Golaszewski {
7765b0acf8dSBartosz Golaszewski 	if (plat->exit)
7775b0acf8dSBartosz Golaszewski 		plat->exit(pdev, plat->bsp_priv);
7785b0acf8dSBartosz Golaszewski }
7795b0acf8dSBartosz Golaszewski EXPORT_SYMBOL_GPL(stmmac_pltfr_exit);
7805b0acf8dSBartosz Golaszewski 
7815b0acf8dSBartosz Golaszewski /**
7823d5bf75dSBartosz Golaszewski  * stmmac_pltfr_probe
7833d5bf75dSBartosz Golaszewski  * @pdev: platform device pointer
7843d5bf75dSBartosz Golaszewski  * @plat: driver data platform structure
7853d5bf75dSBartosz Golaszewski  * @res: stmmac resources structure
7863d5bf75dSBartosz Golaszewski  * Description: This calls the platform's init() callback and probes the
7873d5bf75dSBartosz Golaszewski  * stmmac driver.
7883d5bf75dSBartosz Golaszewski  */
stmmac_pltfr_probe(struct platform_device * pdev,struct plat_stmmacenet_data * plat,struct stmmac_resources * res)7893d5bf75dSBartosz Golaszewski int stmmac_pltfr_probe(struct platform_device *pdev,
7903d5bf75dSBartosz Golaszewski 		       struct plat_stmmacenet_data *plat,
7913d5bf75dSBartosz Golaszewski 		       struct stmmac_resources *res)
7923d5bf75dSBartosz Golaszewski {
7933d5bf75dSBartosz Golaszewski 	int ret;
7943d5bf75dSBartosz Golaszewski 
7953d5bf75dSBartosz Golaszewski 	ret = stmmac_pltfr_init(pdev, plat);
7963d5bf75dSBartosz Golaszewski 	if (ret)
7973d5bf75dSBartosz Golaszewski 		return ret;
7983d5bf75dSBartosz Golaszewski 
7993d5bf75dSBartosz Golaszewski 	ret = stmmac_dvr_probe(&pdev->dev, plat, res);
8003d5bf75dSBartosz Golaszewski 	if (ret) {
8013d5bf75dSBartosz Golaszewski 		stmmac_pltfr_exit(pdev, plat);
8023d5bf75dSBartosz Golaszewski 		return ret;
8033d5bf75dSBartosz Golaszewski 	}
8043d5bf75dSBartosz Golaszewski 
8053d5bf75dSBartosz Golaszewski 	return ret;
8063d5bf75dSBartosz Golaszewski }
8073d5bf75dSBartosz Golaszewski EXPORT_SYMBOL_GPL(stmmac_pltfr_probe);
8083d5bf75dSBartosz Golaszewski 
devm_stmmac_pltfr_remove(void * data)809fc9ee2acSBartosz Golaszewski static void devm_stmmac_pltfr_remove(void *data)
810fc9ee2acSBartosz Golaszewski {
811fc9ee2acSBartosz Golaszewski 	struct platform_device *pdev = data;
812fc9ee2acSBartosz Golaszewski 
813*a39058e0SJisheng Zhang 	stmmac_pltfr_remove(pdev);
814fc9ee2acSBartosz Golaszewski }
815fc9ee2acSBartosz Golaszewski 
816fc9ee2acSBartosz Golaszewski /**
817fc9ee2acSBartosz Golaszewski  * devm_stmmac_pltfr_probe
818fc9ee2acSBartosz Golaszewski  * @pdev: pointer to the platform device
819fc9ee2acSBartosz Golaszewski  * @plat: driver data platform structure
820fc9ee2acSBartosz Golaszewski  * @res: stmmac resources
821fc9ee2acSBartosz Golaszewski  * Description: Devres variant of stmmac_pltfr_probe(). Allows users to skip
822fc9ee2acSBartosz Golaszewski  * calling stmmac_pltfr_remove() on driver detach.
823fc9ee2acSBartosz Golaszewski  */
devm_stmmac_pltfr_probe(struct platform_device * pdev,struct plat_stmmacenet_data * plat,struct stmmac_resources * res)824fc9ee2acSBartosz Golaszewski int devm_stmmac_pltfr_probe(struct platform_device *pdev,
825fc9ee2acSBartosz Golaszewski 			    struct plat_stmmacenet_data *plat,
826fc9ee2acSBartosz Golaszewski 			    struct stmmac_resources *res)
827fc9ee2acSBartosz Golaszewski {
828fc9ee2acSBartosz Golaszewski 	int ret;
829fc9ee2acSBartosz Golaszewski 
830fc9ee2acSBartosz Golaszewski 	ret = stmmac_pltfr_probe(pdev, plat, res);
831fc9ee2acSBartosz Golaszewski 	if (ret)
832fc9ee2acSBartosz Golaszewski 		return ret;
833fc9ee2acSBartosz Golaszewski 
834fc9ee2acSBartosz Golaszewski 	return devm_add_action_or_reset(&pdev->dev, devm_stmmac_pltfr_remove,
835fc9ee2acSBartosz Golaszewski 					pdev);
836fc9ee2acSBartosz Golaszewski }
837fc9ee2acSBartosz Golaszewski EXPORT_SYMBOL_GPL(devm_stmmac_pltfr_probe);
838fc9ee2acSBartosz Golaszewski 
8393d5bf75dSBartosz Golaszewski /**
840*a39058e0SJisheng Zhang  * stmmac_pltfr_remove
8411be0c9d6SBartosz Golaszewski  * @pdev: pointer to the platform device
8421be0c9d6SBartosz Golaszewski  * Description: This undoes the effects of stmmac_pltfr_probe() by removing the
8431be0c9d6SBartosz Golaszewski  * driver and calling the platform's exit() callback.
8441be0c9d6SBartosz Golaszewski  */
stmmac_pltfr_remove(struct platform_device * pdev)8453246627fSUwe Kleine-König void stmmac_pltfr_remove(struct platform_device *pdev)
846bfab27a1SGiuseppe CAVALLARO {
847bfab27a1SGiuseppe CAVALLARO 	struct net_device *ndev = platform_get_drvdata(pdev);
848bfab27a1SGiuseppe CAVALLARO 	struct stmmac_priv *priv = netdev_priv(ndev);
849d2ed0a77SJohan Hovold 	struct plat_stmmacenet_data *plat = priv->plat;
850ff0011cfSUwe Kleine-König 
851*a39058e0SJisheng Zhang 	stmmac_dvr_remove(&pdev->dev);
852*a39058e0SJisheng Zhang 	stmmac_pltfr_exit(pdev, plat);
853bfab27a1SGiuseppe CAVALLARO }
854902b1607SJoachim Eastwood EXPORT_SYMBOL_GPL(stmmac_pltfr_remove);
855bfab27a1SGiuseppe CAVALLARO 
856732fdf0eSGiuseppe CAVALLARO /**
857732fdf0eSGiuseppe CAVALLARO  * stmmac_pltfr_suspend
858732fdf0eSGiuseppe CAVALLARO  * @dev: device pointer
859732fdf0eSGiuseppe CAVALLARO  * Description: this function is invoked when suspend the driver and it direcly
860732fdf0eSGiuseppe CAVALLARO  * call the main suspend function and then, if required, on some platform, it
861732fdf0eSGiuseppe CAVALLARO  * can call an exit helper.
862732fdf0eSGiuseppe CAVALLARO  */
stmmac_pltfr_suspend(struct device * dev)8637ec05a60SWei Yongjun static int __maybe_unused stmmac_pltfr_suspend(struct device *dev)
864bfab27a1SGiuseppe CAVALLARO {
86533a23e22SSrinivas Kandagatla 	int ret;
866bfab27a1SGiuseppe CAVALLARO 	struct net_device *ndev = dev_get_drvdata(dev);
86733a23e22SSrinivas Kandagatla 	struct stmmac_priv *priv = netdev_priv(ndev);
86833a23e22SSrinivas Kandagatla 	struct platform_device *pdev = to_platform_device(dev);
869bfab27a1SGiuseppe CAVALLARO 
870f4e7bd81SJoachim Eastwood 	ret = stmmac_suspend(dev);
8715b0acf8dSBartosz Golaszewski 	stmmac_pltfr_exit(pdev, priv->plat);
87233a23e22SSrinivas Kandagatla 
87333a23e22SSrinivas Kandagatla 	return ret;
874bfab27a1SGiuseppe CAVALLARO }
875bfab27a1SGiuseppe CAVALLARO 
876732fdf0eSGiuseppe CAVALLARO /**
877732fdf0eSGiuseppe CAVALLARO  * stmmac_pltfr_resume
878732fdf0eSGiuseppe CAVALLARO  * @dev: device pointer
879732fdf0eSGiuseppe CAVALLARO  * Description: this function is invoked when resume the driver before calling
880732fdf0eSGiuseppe CAVALLARO  * the main resume function, on some platforms, it can call own init helper
881732fdf0eSGiuseppe CAVALLARO  * if required.
882732fdf0eSGiuseppe CAVALLARO  */
stmmac_pltfr_resume(struct device * dev)8837ec05a60SWei Yongjun static int __maybe_unused stmmac_pltfr_resume(struct device *dev)
884bfab27a1SGiuseppe CAVALLARO {
885bfab27a1SGiuseppe CAVALLARO 	struct net_device *ndev = dev_get_drvdata(dev);
88633a23e22SSrinivas Kandagatla 	struct stmmac_priv *priv = netdev_priv(ndev);
88733a23e22SSrinivas Kandagatla 	struct platform_device *pdev = to_platform_device(dev);
88897117eb5SBartosz Golaszewski 	int ret;
88933a23e22SSrinivas Kandagatla 
8906b09edc1SClark Wang 	ret = stmmac_pltfr_init(pdev, priv->plat);
89197117eb5SBartosz Golaszewski 	if (ret)
89297117eb5SBartosz Golaszewski 		return ret;
893bfab27a1SGiuseppe CAVALLARO 
894f4e7bd81SJoachim Eastwood 	return stmmac_resume(dev);
895bfab27a1SGiuseppe CAVALLARO }
8965ec55823SJoakim Zhang 
stmmac_runtime_suspend(struct device * dev)8977ec05a60SWei Yongjun static int __maybe_unused stmmac_runtime_suspend(struct device *dev)
8985ec55823SJoakim Zhang {
8995ec55823SJoakim Zhang 	struct net_device *ndev = dev_get_drvdata(dev);
9005ec55823SJoakim Zhang 	struct stmmac_priv *priv = netdev_priv(ndev);
9015ec55823SJoakim Zhang 
9025ec55823SJoakim Zhang 	stmmac_bus_clks_config(priv, false);
9035ec55823SJoakim Zhang 
9045ec55823SJoakim Zhang 	return 0;
9055ec55823SJoakim Zhang }
9065ec55823SJoakim Zhang 
stmmac_runtime_resume(struct device * dev)9077ec05a60SWei Yongjun static int __maybe_unused stmmac_runtime_resume(struct device *dev)
9085ec55823SJoakim Zhang {
9095ec55823SJoakim Zhang 	struct net_device *ndev = dev_get_drvdata(dev);
9105ec55823SJoakim Zhang 	struct stmmac_priv *priv = netdev_priv(ndev);
9115ec55823SJoakim Zhang 
9125ec55823SJoakim Zhang 	return stmmac_bus_clks_config(priv, true);
9135ec55823SJoakim Zhang }
914bfab27a1SGiuseppe CAVALLARO 
stmmac_pltfr_noirq_suspend(struct device * dev)9152a48d96fSJoakim Zhang static int __maybe_unused stmmac_pltfr_noirq_suspend(struct device *dev)
916276aae37SJoakim Zhang {
917276aae37SJoakim Zhang 	struct net_device *ndev = dev_get_drvdata(dev);
918276aae37SJoakim Zhang 	struct stmmac_priv *priv = netdev_priv(ndev);
919276aae37SJoakim Zhang 	int ret;
920276aae37SJoakim Zhang 
921276aae37SJoakim Zhang 	if (!netif_running(ndev))
922276aae37SJoakim Zhang 		return 0;
923276aae37SJoakim Zhang 
924276aae37SJoakim Zhang 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
925276aae37SJoakim Zhang 		/* Disable clock in case of PWM is off */
926276aae37SJoakim Zhang 		clk_disable_unprepare(priv->plat->clk_ptp_ref);
927276aae37SJoakim Zhang 
928276aae37SJoakim Zhang 		ret = pm_runtime_force_suspend(dev);
929276aae37SJoakim Zhang 		if (ret)
930276aae37SJoakim Zhang 			return ret;
931276aae37SJoakim Zhang 	}
932276aae37SJoakim Zhang 
933276aae37SJoakim Zhang 	return 0;
934276aae37SJoakim Zhang }
935276aae37SJoakim Zhang 
stmmac_pltfr_noirq_resume(struct device * dev)9362a48d96fSJoakim Zhang static int __maybe_unused stmmac_pltfr_noirq_resume(struct device *dev)
937276aae37SJoakim Zhang {
938276aae37SJoakim Zhang 	struct net_device *ndev = dev_get_drvdata(dev);
939276aae37SJoakim Zhang 	struct stmmac_priv *priv = netdev_priv(ndev);
940276aae37SJoakim Zhang 	int ret;
941276aae37SJoakim Zhang 
942276aae37SJoakim Zhang 	if (!netif_running(ndev))
943276aae37SJoakim Zhang 		return 0;
944276aae37SJoakim Zhang 
945276aae37SJoakim Zhang 	if (!device_may_wakeup(priv->device) || !priv->plat->pmt) {
946276aae37SJoakim Zhang 		/* enable the clk previously disabled */
947276aae37SJoakim Zhang 		ret = pm_runtime_force_resume(dev);
948276aae37SJoakim Zhang 		if (ret)
949276aae37SJoakim Zhang 			return ret;
950276aae37SJoakim Zhang 
951f4c7d894SBiao Huang 		ret = clk_prepare_enable(priv->plat->clk_ptp_ref);
952f4c7d894SBiao Huang 		if (ret < 0) {
953f4c7d894SBiao Huang 			netdev_warn(priv->dev,
954f4c7d894SBiao Huang 				    "failed to enable PTP reference clock: %pe\n",
955f4c7d894SBiao Huang 				    ERR_PTR(ret));
956f4c7d894SBiao Huang 			return ret;
957f4c7d894SBiao Huang 		}
958276aae37SJoakim Zhang 	}
959276aae37SJoakim Zhang 
960276aae37SJoakim Zhang 	return 0;
961276aae37SJoakim Zhang }
962276aae37SJoakim Zhang 
9635ec55823SJoakim Zhang const struct dev_pm_ops stmmac_pltfr_pm_ops = {
9645ec55823SJoakim Zhang 	SET_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_suspend, stmmac_pltfr_resume)
9655ec55823SJoakim Zhang 	SET_RUNTIME_PM_OPS(stmmac_runtime_suspend, stmmac_runtime_resume, NULL)
966276aae37SJoakim Zhang 	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(stmmac_pltfr_noirq_suspend, stmmac_pltfr_noirq_resume)
9675ec55823SJoakim Zhang };
968902b1607SJoachim Eastwood EXPORT_SYMBOL_GPL(stmmac_pltfr_pm_ops);
969ea111545SJoachim Eastwood 
970ea111545SJoachim Eastwood MODULE_DESCRIPTION("STMMAC 10/100/1000 Ethernet platform support");
971ea111545SJoachim Eastwood MODULE_AUTHOR("Giuseppe Cavallaro <peppe.cavallaro@st.com>");
972ea111545SJoachim Eastwood MODULE_LICENSE("GPL");
973