xref: /openbmc/linux/drivers/net/ethernet/freescale/fman/mac.c (revision 8585bdadc2478e3038f390e94f448bb27df7cb3a)
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
2 /*
3  * Copyright 2008 - 2015 Freescale Semiconductor Inc.
4  */
5 
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
7 
8 #include <linux/init.h>
9 #include <linux/module.h>
10 #include <linux/of_address.h>
11 #include <linux/of_platform.h>
12 #include <linux/of_net.h>
13 #include <linux/of_mdio.h>
14 #include <linux/device.h>
15 #include <linux/phy.h>
16 #include <linux/netdevice.h>
17 #include <linux/phy_fixed.h>
18 #include <linux/etherdevice.h>
19 #include <linux/libfdt_env.h>
20 
21 #include "mac.h"
22 #include "fman_mac.h"
23 #include "fman_dtsec.h"
24 #include "fman_tgec.h"
25 #include "fman_memac.h"
26 
27 MODULE_LICENSE("Dual BSD/GPL");
28 MODULE_DESCRIPTION("FSL FMan MAC API based driver");
29 
30 struct mac_priv_s {
31 	struct device			*dev;
32 	void __iomem			*vaddr;
33 	u8				cell_index;
34 	struct fman			*fman;
35 	struct device_node		*internal_phy_node;
36 	/* List of multicast addresses */
37 	struct list_head		mc_addr_list;
38 	struct platform_device		*eth_dev;
39 	struct fixed_phy_status		*fixed_link;
40 	u16				speed;
41 	u16				max_speed;
42 
43 	int (*enable)(struct fman_mac *mac_dev, enum comm_mode mode);
44 	int (*disable)(struct fman_mac *mac_dev, enum comm_mode mode);
45 };
46 
47 struct mac_address {
48 	u8 addr[ETH_ALEN];
49 	struct list_head list;
50 };
51 
52 static void mac_exception(void *handle, enum fman_mac_exceptions ex)
53 {
54 	struct mac_device	*mac_dev;
55 	struct mac_priv_s	*priv;
56 
57 	mac_dev = handle;
58 	priv = mac_dev->priv;
59 
60 	if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
61 		/* don't flag RX FIFO after the first */
62 		mac_dev->set_exception(mac_dev->fman_mac,
63 				       FM_MAC_EX_10G_RX_FIFO_OVFL, false);
64 		dev_err(priv->dev, "10G MAC got RX FIFO Error = %x\n", ex);
65 	}
66 
67 	dev_dbg(priv->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
68 		__func__, ex);
69 }
70 
71 static int set_fman_mac_params(struct mac_device *mac_dev,
72 			       struct fman_mac_params *params)
73 {
74 	struct mac_priv_s *priv = mac_dev->priv;
75 
76 	params->base_addr = (typeof(params->base_addr))
77 		devm_ioremap(priv->dev, mac_dev->res->start,
78 			     resource_size(mac_dev->res));
79 	if (!params->base_addr)
80 		return -ENOMEM;
81 
82 	memcpy(&params->addr, mac_dev->addr, sizeof(mac_dev->addr));
83 	params->max_speed	= priv->max_speed;
84 	params->phy_if		= mac_dev->phy_if;
85 	params->basex_if	= false;
86 	params->mac_id		= priv->cell_index;
87 	params->fm		= (void *)priv->fman;
88 	params->exception_cb	= mac_exception;
89 	params->event_cb	= mac_exception;
90 	params->dev_id		= mac_dev;
91 	params->internal_phy_node = priv->internal_phy_node;
92 
93 	return 0;
94 }
95 
96 static int tgec_initialization(struct mac_device *mac_dev)
97 {
98 	int err;
99 	struct mac_priv_s	*priv;
100 	struct fman_mac_params	params;
101 	u32			version;
102 
103 	priv = mac_dev->priv;
104 
105 	err = set_fman_mac_params(mac_dev, &params);
106 	if (err)
107 		goto _return;
108 
109 	mac_dev->fman_mac = tgec_config(&params);
110 	if (!mac_dev->fman_mac) {
111 		err = -EINVAL;
112 		goto _return;
113 	}
114 
115 	err = tgec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
116 	if (err < 0)
117 		goto _return_fm_mac_free;
118 
119 	err = tgec_init(mac_dev->fman_mac);
120 	if (err < 0)
121 		goto _return_fm_mac_free;
122 
123 	/* For 10G MAC, disable Tx ECC exception */
124 	err = mac_dev->set_exception(mac_dev->fman_mac,
125 				     FM_MAC_EX_10G_TX_ECC_ER, false);
126 	if (err < 0)
127 		goto _return_fm_mac_free;
128 
129 	err = tgec_get_version(mac_dev->fman_mac, &version);
130 	if (err < 0)
131 		goto _return_fm_mac_free;
132 
133 	dev_info(priv->dev, "FMan XGEC version: 0x%08x\n", version);
134 
135 	goto _return;
136 
137 _return_fm_mac_free:
138 	tgec_free(mac_dev->fman_mac);
139 
140 _return:
141 	return err;
142 }
143 
144 static int dtsec_initialization(struct mac_device *mac_dev)
145 {
146 	int			err;
147 	struct mac_priv_s	*priv;
148 	struct fman_mac_params	params;
149 	u32			version;
150 
151 	priv = mac_dev->priv;
152 
153 	err = set_fman_mac_params(mac_dev, &params);
154 	if (err)
155 		goto _return;
156 
157 	mac_dev->fman_mac = dtsec_config(&params);
158 	if (!mac_dev->fman_mac) {
159 		err = -EINVAL;
160 		goto _return;
161 	}
162 
163 	err = dtsec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
164 	if (err < 0)
165 		goto _return_fm_mac_free;
166 
167 	err = dtsec_cfg_pad_and_crc(mac_dev->fman_mac, true);
168 	if (err < 0)
169 		goto _return_fm_mac_free;
170 
171 	err = dtsec_init(mac_dev->fman_mac);
172 	if (err < 0)
173 		goto _return_fm_mac_free;
174 
175 	/* For 1G MAC, disable by default the MIB counters overflow interrupt */
176 	err = mac_dev->set_exception(mac_dev->fman_mac,
177 				     FM_MAC_EX_1G_RX_MIB_CNT_OVFL, false);
178 	if (err < 0)
179 		goto _return_fm_mac_free;
180 
181 	err = dtsec_get_version(mac_dev->fman_mac, &version);
182 	if (err < 0)
183 		goto _return_fm_mac_free;
184 
185 	dev_info(priv->dev, "FMan dTSEC version: 0x%08x\n", version);
186 
187 	goto _return;
188 
189 _return_fm_mac_free:
190 	dtsec_free(mac_dev->fman_mac);
191 
192 _return:
193 	return err;
194 }
195 
196 static int memac_initialization(struct mac_device *mac_dev)
197 {
198 	int			 err;
199 	struct mac_priv_s	*priv;
200 	struct fman_mac_params	 params;
201 
202 	priv = mac_dev->priv;
203 
204 	err = set_fman_mac_params(mac_dev, &params);
205 	if (err)
206 		goto _return;
207 
208 	if (priv->max_speed == SPEED_10000)
209 		params.phy_if = PHY_INTERFACE_MODE_XGMII;
210 
211 	mac_dev->fman_mac = memac_config(&params);
212 	if (!mac_dev->fman_mac) {
213 		err = -EINVAL;
214 		goto _return;
215 	}
216 
217 	err = memac_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
218 	if (err < 0)
219 		goto _return_fm_mac_free;
220 
221 	err = memac_cfg_reset_on_init(mac_dev->fman_mac, true);
222 	if (err < 0)
223 		goto _return_fm_mac_free;
224 
225 	err = memac_cfg_fixed_link(mac_dev->fman_mac, priv->fixed_link);
226 	if (err < 0)
227 		goto _return_fm_mac_free;
228 
229 	err = memac_init(mac_dev->fman_mac);
230 	if (err < 0)
231 		goto _return_fm_mac_free;
232 
233 	dev_info(priv->dev, "FMan MEMAC\n");
234 
235 	goto _return;
236 
237 _return_fm_mac_free:
238 	memac_free(mac_dev->fman_mac);
239 
240 _return:
241 	return err;
242 }
243 
244 static int start(struct mac_device *mac_dev)
245 {
246 	int	 err;
247 	struct phy_device *phy_dev = mac_dev->phy_dev;
248 	struct mac_priv_s *priv = mac_dev->priv;
249 
250 	err = priv->enable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
251 	if (!err && phy_dev)
252 		phy_start(phy_dev);
253 
254 	return err;
255 }
256 
257 static int stop(struct mac_device *mac_dev)
258 {
259 	struct mac_priv_s *priv = mac_dev->priv;
260 
261 	if (mac_dev->phy_dev)
262 		phy_stop(mac_dev->phy_dev);
263 
264 	return priv->disable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
265 }
266 
267 static int set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
268 {
269 	struct mac_priv_s	*priv;
270 	struct mac_address	*old_addr, *tmp;
271 	struct netdev_hw_addr	*ha;
272 	int			err;
273 	enet_addr_t		*addr;
274 
275 	priv = mac_dev->priv;
276 
277 	/* Clear previous address list */
278 	list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
279 		addr = (enet_addr_t *)old_addr->addr;
280 		err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
281 		if (err < 0)
282 			return err;
283 
284 		list_del(&old_addr->list);
285 		kfree(old_addr);
286 	}
287 
288 	/* Add all the addresses from the new list */
289 	netdev_for_each_mc_addr(ha, net_dev) {
290 		addr = (enet_addr_t *)ha->addr;
291 		err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
292 		if (err < 0)
293 			return err;
294 
295 		tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
296 		if (!tmp)
297 			return -ENOMEM;
298 
299 		ether_addr_copy(tmp->addr, ha->addr);
300 		list_add(&tmp->list, &priv->mc_addr_list);
301 	}
302 	return 0;
303 }
304 
305 /**
306  * fman_set_mac_active_pause
307  * @mac_dev:	A pointer to the MAC device
308  * @rx:		Pause frame setting for RX
309  * @tx:		Pause frame setting for TX
310  *
311  * Set the MAC RX/TX PAUSE frames settings
312  *
313  * Avoid redundant calls to FMD, if the MAC driver already contains the desired
314  * active PAUSE settings. Otherwise, the new active settings should be reflected
315  * in FMan.
316  *
317  * Return: 0 on success; Error code otherwise.
318  */
319 int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
320 {
321 	struct fman_mac *fman_mac = mac_dev->fman_mac;
322 	int err = 0;
323 
324 	if (rx != mac_dev->rx_pause_active) {
325 		err = mac_dev->set_rx_pause(fman_mac, rx);
326 		if (likely(err == 0))
327 			mac_dev->rx_pause_active = rx;
328 	}
329 
330 	if (tx != mac_dev->tx_pause_active) {
331 		u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
332 					 FSL_FM_PAUSE_TIME_DISABLE);
333 
334 		err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
335 
336 		if (likely(err == 0))
337 			mac_dev->tx_pause_active = tx;
338 	}
339 
340 	return err;
341 }
342 EXPORT_SYMBOL(fman_set_mac_active_pause);
343 
344 /**
345  * fman_get_pause_cfg
346  * @mac_dev:	A pointer to the MAC device
347  * @rx_pause:	Return value for RX setting
348  * @tx_pause:	Return value for TX setting
349  *
350  * Determine the MAC RX/TX PAUSE frames settings based on PHY
351  * autonegotiation or values set by eththool.
352  *
353  * Return: Pointer to FMan device.
354  */
355 void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
356 			bool *tx_pause)
357 {
358 	struct phy_device *phy_dev = mac_dev->phy_dev;
359 	u16 lcl_adv, rmt_adv;
360 	u8 flowctrl;
361 
362 	*rx_pause = *tx_pause = false;
363 
364 	if (!phy_dev->duplex)
365 		return;
366 
367 	/* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
368 	 * are those set by ethtool.
369 	 */
370 	if (!mac_dev->autoneg_pause) {
371 		*rx_pause = mac_dev->rx_pause_req;
372 		*tx_pause = mac_dev->tx_pause_req;
373 		return;
374 	}
375 
376 	/* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
377 	 * settings depend on the result of the link negotiation.
378 	 */
379 
380 	/* get local capabilities */
381 	lcl_adv = linkmode_adv_to_lcl_adv_t(phy_dev->advertising);
382 
383 	/* get link partner capabilities */
384 	rmt_adv = 0;
385 	if (phy_dev->pause)
386 		rmt_adv |= LPA_PAUSE_CAP;
387 	if (phy_dev->asym_pause)
388 		rmt_adv |= LPA_PAUSE_ASYM;
389 
390 	/* Calculate TX/RX settings based on local and peer advertised
391 	 * symmetric/asymmetric PAUSE capabilities.
392 	 */
393 	flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
394 	if (flowctrl & FLOW_CTRL_RX)
395 		*rx_pause = true;
396 	if (flowctrl & FLOW_CTRL_TX)
397 		*tx_pause = true;
398 }
399 EXPORT_SYMBOL(fman_get_pause_cfg);
400 
401 static void adjust_link_void(struct mac_device *mac_dev)
402 {
403 }
404 
405 static void adjust_link_dtsec(struct mac_device *mac_dev)
406 {
407 	struct phy_device *phy_dev = mac_dev->phy_dev;
408 	struct fman_mac *fman_mac;
409 	bool rx_pause, tx_pause;
410 	int err;
411 
412 	fman_mac = mac_dev->fman_mac;
413 	if (!phy_dev->link) {
414 		dtsec_restart_autoneg(fman_mac);
415 
416 		return;
417 	}
418 
419 	dtsec_adjust_link(fman_mac, phy_dev->speed);
420 	fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
421 	err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
422 	if (err < 0)
423 		dev_err(mac_dev->priv->dev, "fman_set_mac_active_pause() = %d\n",
424 			err);
425 }
426 
427 static void adjust_link_memac(struct mac_device *mac_dev)
428 {
429 	struct phy_device *phy_dev = mac_dev->phy_dev;
430 	struct fman_mac *fman_mac;
431 	bool rx_pause, tx_pause;
432 	int err;
433 
434 	fman_mac = mac_dev->fman_mac;
435 	memac_adjust_link(fman_mac, phy_dev->speed);
436 
437 	fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
438 	err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
439 	if (err < 0)
440 		dev_err(mac_dev->priv->dev, "fman_set_mac_active_pause() = %d\n",
441 			err);
442 }
443 
444 static void setup_dtsec(struct mac_device *mac_dev)
445 {
446 	mac_dev->init			= dtsec_initialization;
447 	mac_dev->set_promisc		= dtsec_set_promiscuous;
448 	mac_dev->change_addr		= dtsec_modify_mac_address;
449 	mac_dev->add_hash_mac_addr	= dtsec_add_hash_mac_address;
450 	mac_dev->remove_hash_mac_addr	= dtsec_del_hash_mac_address;
451 	mac_dev->set_tx_pause		= dtsec_set_tx_pause_frames;
452 	mac_dev->set_rx_pause		= dtsec_accept_rx_pause_frames;
453 	mac_dev->set_exception		= dtsec_set_exception;
454 	mac_dev->set_allmulti		= dtsec_set_allmulti;
455 	mac_dev->set_tstamp		= dtsec_set_tstamp;
456 	mac_dev->set_multi		= set_multi;
457 	mac_dev->start			= start;
458 	mac_dev->stop			= stop;
459 	mac_dev->adjust_link            = adjust_link_dtsec;
460 	mac_dev->priv->enable		= dtsec_enable;
461 	mac_dev->priv->disable		= dtsec_disable;
462 }
463 
464 static void setup_tgec(struct mac_device *mac_dev)
465 {
466 	mac_dev->init			= tgec_initialization;
467 	mac_dev->set_promisc		= tgec_set_promiscuous;
468 	mac_dev->change_addr		= tgec_modify_mac_address;
469 	mac_dev->add_hash_mac_addr	= tgec_add_hash_mac_address;
470 	mac_dev->remove_hash_mac_addr	= tgec_del_hash_mac_address;
471 	mac_dev->set_tx_pause		= tgec_set_tx_pause_frames;
472 	mac_dev->set_rx_pause		= tgec_accept_rx_pause_frames;
473 	mac_dev->set_exception		= tgec_set_exception;
474 	mac_dev->set_allmulti		= tgec_set_allmulti;
475 	mac_dev->set_tstamp		= tgec_set_tstamp;
476 	mac_dev->set_multi		= set_multi;
477 	mac_dev->start			= start;
478 	mac_dev->stop			= stop;
479 	mac_dev->adjust_link            = adjust_link_void;
480 	mac_dev->priv->enable		= tgec_enable;
481 	mac_dev->priv->disable		= tgec_disable;
482 }
483 
484 static void setup_memac(struct mac_device *mac_dev)
485 {
486 	mac_dev->init			= memac_initialization;
487 	mac_dev->set_promisc		= memac_set_promiscuous;
488 	mac_dev->change_addr		= memac_modify_mac_address;
489 	mac_dev->add_hash_mac_addr	= memac_add_hash_mac_address;
490 	mac_dev->remove_hash_mac_addr	= memac_del_hash_mac_address;
491 	mac_dev->set_tx_pause		= memac_set_tx_pause_frames;
492 	mac_dev->set_rx_pause		= memac_accept_rx_pause_frames;
493 	mac_dev->set_exception		= memac_set_exception;
494 	mac_dev->set_allmulti		= memac_set_allmulti;
495 	mac_dev->set_tstamp		= memac_set_tstamp;
496 	mac_dev->set_multi		= set_multi;
497 	mac_dev->start			= start;
498 	mac_dev->stop			= stop;
499 	mac_dev->adjust_link            = adjust_link_memac;
500 	mac_dev->priv->enable		= memac_enable;
501 	mac_dev->priv->disable		= memac_disable;
502 }
503 
504 #define DTSEC_SUPPORTED \
505 	(SUPPORTED_10baseT_Half \
506 	| SUPPORTED_10baseT_Full \
507 	| SUPPORTED_100baseT_Half \
508 	| SUPPORTED_100baseT_Full \
509 	| SUPPORTED_Autoneg \
510 	| SUPPORTED_Pause \
511 	| SUPPORTED_Asym_Pause \
512 	| SUPPORTED_FIBRE \
513 	| SUPPORTED_MII)
514 
515 static DEFINE_MUTEX(eth_lock);
516 
517 static const u16 phy2speed[] = {
518 	[PHY_INTERFACE_MODE_MII]		= SPEED_100,
519 	[PHY_INTERFACE_MODE_GMII]		= SPEED_1000,
520 	[PHY_INTERFACE_MODE_SGMII]		= SPEED_1000,
521 	[PHY_INTERFACE_MODE_TBI]		= SPEED_1000,
522 	[PHY_INTERFACE_MODE_RMII]		= SPEED_100,
523 	[PHY_INTERFACE_MODE_RGMII]		= SPEED_1000,
524 	[PHY_INTERFACE_MODE_RGMII_ID]		= SPEED_1000,
525 	[PHY_INTERFACE_MODE_RGMII_RXID]	= SPEED_1000,
526 	[PHY_INTERFACE_MODE_RGMII_TXID]	= SPEED_1000,
527 	[PHY_INTERFACE_MODE_RTBI]		= SPEED_1000,
528 	[PHY_INTERFACE_MODE_QSGMII]		= SPEED_1000,
529 	[PHY_INTERFACE_MODE_XGMII]		= SPEED_10000
530 };
531 
532 static struct platform_device *dpaa_eth_add_device(int fman_id,
533 						   struct mac_device *mac_dev)
534 {
535 	struct platform_device *pdev;
536 	struct dpaa_eth_data data;
537 	struct mac_priv_s	*priv;
538 	static int dpaa_eth_dev_cnt;
539 	int ret;
540 
541 	priv = mac_dev->priv;
542 
543 	data.mac_dev = mac_dev;
544 	data.mac_hw_id = priv->cell_index;
545 	data.fman_hw_id = fman_id;
546 
547 	mutex_lock(&eth_lock);
548 	pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
549 	if (!pdev) {
550 		ret = -ENOMEM;
551 		goto no_mem;
552 	}
553 
554 	pdev->dev.parent = priv->dev;
555 
556 	ret = platform_device_add_data(pdev, &data, sizeof(data));
557 	if (ret)
558 		goto err;
559 
560 	ret = platform_device_add(pdev);
561 	if (ret)
562 		goto err;
563 
564 	dpaa_eth_dev_cnt++;
565 	mutex_unlock(&eth_lock);
566 
567 	return pdev;
568 
569 err:
570 	platform_device_put(pdev);
571 no_mem:
572 	mutex_unlock(&eth_lock);
573 
574 	return ERR_PTR(ret);
575 }
576 
577 static const struct of_device_id mac_match[] = {
578 	{ .compatible	= "fsl,fman-dtsec" },
579 	{ .compatible	= "fsl,fman-xgec" },
580 	{ .compatible	= "fsl,fman-memac" },
581 	{}
582 };
583 MODULE_DEVICE_TABLE(of, mac_match);
584 
585 static int mac_probe(struct platform_device *_of_dev)
586 {
587 	int			 err, i, nph;
588 	struct device		*dev;
589 	struct device_node	*mac_node, *dev_node;
590 	struct mac_device	*mac_dev;
591 	struct platform_device	*of_dev;
592 	struct resource		 res;
593 	struct mac_priv_s	*priv;
594 	u32			 val;
595 	u8			fman_id;
596 	phy_interface_t          phy_if;
597 
598 	dev = &_of_dev->dev;
599 	mac_node = dev->of_node;
600 
601 	mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
602 	if (!mac_dev) {
603 		err = -ENOMEM;
604 		goto _return;
605 	}
606 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
607 	if (!priv) {
608 		err = -ENOMEM;
609 		goto _return;
610 	}
611 
612 	/* Save private information */
613 	mac_dev->priv = priv;
614 	priv->dev = dev;
615 
616 	if (of_device_is_compatible(mac_node, "fsl,fman-dtsec")) {
617 		setup_dtsec(mac_dev);
618 		priv->internal_phy_node = of_parse_phandle(mac_node,
619 							  "tbi-handle", 0);
620 	} else if (of_device_is_compatible(mac_node, "fsl,fman-xgec")) {
621 		setup_tgec(mac_dev);
622 	} else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
623 		setup_memac(mac_dev);
624 		priv->internal_phy_node = of_parse_phandle(mac_node,
625 							  "pcsphy-handle", 0);
626 	} else {
627 		dev_err(dev, "MAC node (%pOF) contains unsupported MAC\n",
628 			mac_node);
629 		err = -EINVAL;
630 		goto _return;
631 	}
632 
633 	INIT_LIST_HEAD(&priv->mc_addr_list);
634 
635 	/* Get the FM node */
636 	dev_node = of_get_parent(mac_node);
637 	if (!dev_node) {
638 		dev_err(dev, "of_get_parent(%pOF) failed\n",
639 			mac_node);
640 		err = -EINVAL;
641 		goto _return_of_get_parent;
642 	}
643 
644 	of_dev = of_find_device_by_node(dev_node);
645 	if (!of_dev) {
646 		dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
647 		err = -EINVAL;
648 		goto _return_of_node_put;
649 	}
650 
651 	/* Get the FMan cell-index */
652 	err = of_property_read_u32(dev_node, "cell-index", &val);
653 	if (err) {
654 		dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
655 		err = -EINVAL;
656 		goto _return_of_node_put;
657 	}
658 	/* cell-index 0 => FMan id 1 */
659 	fman_id = (u8)(val + 1);
660 
661 	priv->fman = fman_bind(&of_dev->dev);
662 	if (!priv->fman) {
663 		dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
664 		err = -ENODEV;
665 		goto _return_of_node_put;
666 	}
667 
668 	of_node_put(dev_node);
669 
670 	/* Get the address of the memory mapped registers */
671 	err = of_address_to_resource(mac_node, 0, &res);
672 	if (err < 0) {
673 		dev_err(dev, "of_address_to_resource(%pOF) = %d\n",
674 			mac_node, err);
675 		goto _return_of_get_parent;
676 	}
677 
678 	mac_dev->res = __devm_request_region(dev,
679 					     fman_get_mem_region(priv->fman),
680 					     res.start, resource_size(&res),
681 					     "mac");
682 	if (!mac_dev->res) {
683 		dev_err(dev, "__devm_request_mem_region(mac) failed\n");
684 		err = -EBUSY;
685 		goto _return_of_get_parent;
686 	}
687 
688 	priv->vaddr = devm_ioremap(dev, mac_dev->res->start,
689 				   resource_size(mac_dev->res));
690 	if (!priv->vaddr) {
691 		dev_err(dev, "devm_ioremap() failed\n");
692 		err = -EIO;
693 		goto _return_of_get_parent;
694 	}
695 
696 	if (!of_device_is_available(mac_node)) {
697 		err = -ENODEV;
698 		goto _return_of_get_parent;
699 	}
700 
701 	/* Get the cell-index */
702 	err = of_property_read_u32(mac_node, "cell-index", &val);
703 	if (err) {
704 		dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
705 		err = -EINVAL;
706 		goto _return_of_get_parent;
707 	}
708 	priv->cell_index = (u8)val;
709 
710 	/* Get the MAC address */
711 	err = of_get_mac_address(mac_node, mac_dev->addr);
712 	if (err)
713 		dev_warn(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
714 
715 	/* Get the port handles */
716 	nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
717 	if (unlikely(nph < 0)) {
718 		dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
719 			mac_node);
720 		err = nph;
721 		goto _return_of_get_parent;
722 	}
723 
724 	if (nph != ARRAY_SIZE(mac_dev->port)) {
725 		dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
726 			mac_node);
727 		err = -EINVAL;
728 		goto _return_of_get_parent;
729 	}
730 
731 	for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
732 		/* Find the port node */
733 		dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
734 		if (!dev_node) {
735 			dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
736 				mac_node);
737 			err = -EINVAL;
738 			goto _return_of_node_put;
739 		}
740 
741 		of_dev = of_find_device_by_node(dev_node);
742 		if (!of_dev) {
743 			dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
744 				dev_node);
745 			err = -EINVAL;
746 			goto _return_of_node_put;
747 		}
748 
749 		mac_dev->port[i] = fman_port_bind(&of_dev->dev);
750 		if (!mac_dev->port[i]) {
751 			dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
752 				dev_node);
753 			err = -EINVAL;
754 			goto _return_of_node_put;
755 		}
756 		of_node_put(dev_node);
757 	}
758 
759 	/* Get the PHY connection type */
760 	err = of_get_phy_mode(mac_node, &phy_if);
761 	if (err) {
762 		dev_warn(dev,
763 			 "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
764 			 mac_node);
765 		phy_if = PHY_INTERFACE_MODE_SGMII;
766 	}
767 	mac_dev->phy_if = phy_if;
768 
769 	priv->speed		= phy2speed[mac_dev->phy_if];
770 	priv->max_speed		= priv->speed;
771 	mac_dev->if_support	= DTSEC_SUPPORTED;
772 	/* We don't support half-duplex in SGMII mode */
773 	if (mac_dev->phy_if == PHY_INTERFACE_MODE_SGMII)
774 		mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
775 					SUPPORTED_100baseT_Half);
776 
777 	/* Gigabit support (no half-duplex) */
778 	if (priv->max_speed == 1000)
779 		mac_dev->if_support |= SUPPORTED_1000baseT_Full;
780 
781 	/* The 10G interface only supports one mode */
782 	if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
783 		mac_dev->if_support = SUPPORTED_10000baseT_Full;
784 
785 	/* Get the rest of the PHY information */
786 	mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
787 	if (!mac_dev->phy_node && of_phy_is_fixed_link(mac_node)) {
788 		struct phy_device *phy;
789 
790 		err = of_phy_register_fixed_link(mac_node);
791 		if (err)
792 			goto _return_of_get_parent;
793 
794 		priv->fixed_link = kzalloc(sizeof(*priv->fixed_link),
795 					   GFP_KERNEL);
796 		if (!priv->fixed_link) {
797 			err = -ENOMEM;
798 			goto _return_of_get_parent;
799 		}
800 
801 		mac_dev->phy_node = of_node_get(mac_node);
802 		phy = of_phy_find_device(mac_dev->phy_node);
803 		if (!phy) {
804 			err = -EINVAL;
805 			of_node_put(mac_dev->phy_node);
806 			goto _return_of_get_parent;
807 		}
808 
809 		priv->fixed_link->link = phy->link;
810 		priv->fixed_link->speed = phy->speed;
811 		priv->fixed_link->duplex = phy->duplex;
812 		priv->fixed_link->pause = phy->pause;
813 		priv->fixed_link->asym_pause = phy->asym_pause;
814 
815 		put_device(&phy->mdio.dev);
816 	}
817 
818 	err = mac_dev->init(mac_dev);
819 	if (err < 0) {
820 		dev_err(dev, "mac_dev->init() = %d\n", err);
821 		of_node_put(mac_dev->phy_node);
822 		goto _return_of_get_parent;
823 	}
824 
825 	/* pause frame autonegotiation enabled */
826 	mac_dev->autoneg_pause = true;
827 
828 	/* By intializing the values to false, force FMD to enable PAUSE frames
829 	 * on RX and TX
830 	 */
831 	mac_dev->rx_pause_req = true;
832 	mac_dev->tx_pause_req = true;
833 	mac_dev->rx_pause_active = false;
834 	mac_dev->tx_pause_active = false;
835 	err = fman_set_mac_active_pause(mac_dev, true, true);
836 	if (err < 0)
837 		dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
838 
839 	if (!is_zero_ether_addr(mac_dev->addr))
840 		dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr);
841 
842 	priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
843 	if (IS_ERR(priv->eth_dev)) {
844 		dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
845 			priv->cell_index);
846 		priv->eth_dev = NULL;
847 	}
848 
849 	goto _return;
850 
851 _return_of_node_put:
852 	of_node_put(dev_node);
853 _return_of_get_parent:
854 	kfree(priv->fixed_link);
855 _return:
856 	return err;
857 }
858 
859 static struct platform_driver mac_driver = {
860 	.driver = {
861 		.name		= KBUILD_MODNAME,
862 		.of_match_table	= mac_match,
863 	},
864 	.probe		= mac_probe,
865 };
866 
867 builtin_platform_driver(mac_driver);
868