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