1 /* Copyright 2008-2015 Freescale Semiconductor, Inc.
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5 * * Redistributions of source code must retain the above copyright
6 * notice, this list of conditions and the following disclaimer.
7 * * Redistributions in binary form must reproduce the above copyright
8 * notice, this list of conditions and the following disclaimer in the
9 * documentation and/or other materials provided with the distribution.
10 * * Neither the name of Freescale Semiconductor nor the
11 * names of its contributors may be used to endorse or promote products
12 * derived from this software without specific prior written permission.
15 * ALTERNATIVELY, this software may be distributed under the terms of the
16 * GNU General Public License ("GPL") as published by the Free Software
17 * Foundation, either version 2 of that License or (at your option) any
20 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
21 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/of_address.h>
37 #include <linux/of_platform.h>
38 #include <linux/of_net.h>
39 #include <linux/of_mdio.h>
40 #include <linux/device.h>
41 #include <linux/phy.h>
42 #include <linux/netdevice.h>
43 #include <linux/phy_fixed.h>
44 #include <linux/etherdevice.h>
45 #include <linux/libfdt_env.h>
49 #include "fman_dtsec.h"
50 #include "fman_tgec.h"
51 #include "fman_memac.h"
53 MODULE_LICENSE("Dual BSD/GPL");
54 MODULE_DESCRIPTION("FSL FMan MAC API based driver");
60 phy_interface_t phy_if;
62 struct device_node *phy_node;
63 struct device_node *internal_phy_node;
64 /* List of multicast addresses */
65 struct list_head mc_addr_list;
66 struct platform_device *eth_dev;
67 struct fixed_phy_status *fixed_link;
71 int (*enable)(struct fman_mac *mac_dev, enum comm_mode mode);
72 int (*disable)(struct fman_mac *mac_dev, enum comm_mode mode);
77 struct list_head list;
80 static void mac_exception(void *handle, enum fman_mac_exceptions ex)
82 struct mac_device *mac_dev;
83 struct mac_priv_s *priv;
88 if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
89 /* don't flag RX FIFO after the first */
90 mac_dev->set_exception(mac_dev->fman_mac,
91 FM_MAC_EX_10G_RX_FIFO_OVFL, false);
92 dev_err(priv->dev, "10G MAC got RX FIFO Error = %x\n", ex);
95 dev_dbg(priv->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
99 static void set_fman_mac_params(struct mac_device *mac_dev,
100 struct fman_mac_params *params)
102 struct mac_priv_s *priv = mac_dev->priv;
104 params->base_addr = (typeof(params->base_addr))
105 devm_ioremap(priv->dev, mac_dev->res->start,
106 resource_size(mac_dev->res));
107 memcpy(¶ms->addr, mac_dev->addr, sizeof(mac_dev->addr));
108 params->max_speed = priv->max_speed;
109 params->phy_if = priv->phy_if;
110 params->basex_if = false;
111 params->mac_id = priv->cell_index;
112 params->fm = (void *)priv->fman;
113 params->exception_cb = mac_exception;
114 params->event_cb = mac_exception;
115 params->dev_id = mac_dev;
116 params->internal_phy_node = priv->internal_phy_node;
119 static int tgec_initialization(struct mac_device *mac_dev)
122 struct mac_priv_s *priv;
123 struct fman_mac_params params;
126 priv = mac_dev->priv;
128 set_fman_mac_params(mac_dev, ¶ms);
130 mac_dev->fman_mac = tgec_config(¶ms);
131 if (!mac_dev->fman_mac) {
136 err = tgec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
138 goto _return_fm_mac_free;
140 err = tgec_init(mac_dev->fman_mac);
142 goto _return_fm_mac_free;
144 /* For 10G MAC, disable Tx ECC exception */
145 err = mac_dev->set_exception(mac_dev->fman_mac,
146 FM_MAC_EX_10G_TX_ECC_ER, false);
148 goto _return_fm_mac_free;
150 err = tgec_get_version(mac_dev->fman_mac, &version);
152 goto _return_fm_mac_free;
154 dev_info(priv->dev, "FMan XGEC version: 0x%08x\n", version);
159 tgec_free(mac_dev->fman_mac);
165 static int dtsec_initialization(struct mac_device *mac_dev)
168 struct mac_priv_s *priv;
169 struct fman_mac_params params;
172 priv = mac_dev->priv;
174 set_fman_mac_params(mac_dev, ¶ms);
176 mac_dev->fman_mac = dtsec_config(¶ms);
177 if (!mac_dev->fman_mac) {
182 err = dtsec_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
184 goto _return_fm_mac_free;
186 err = dtsec_cfg_pad_and_crc(mac_dev->fman_mac, true);
188 goto _return_fm_mac_free;
190 err = dtsec_init(mac_dev->fman_mac);
192 goto _return_fm_mac_free;
194 /* For 1G MAC, disable by default the MIB counters overflow interrupt */
195 err = mac_dev->set_exception(mac_dev->fman_mac,
196 FM_MAC_EX_1G_RX_MIB_CNT_OVFL, false);
198 goto _return_fm_mac_free;
200 err = dtsec_get_version(mac_dev->fman_mac, &version);
202 goto _return_fm_mac_free;
204 dev_info(priv->dev, "FMan dTSEC version: 0x%08x\n", version);
209 dtsec_free(mac_dev->fman_mac);
215 static int memac_initialization(struct mac_device *mac_dev)
218 struct mac_priv_s *priv;
219 struct fman_mac_params params;
221 priv = mac_dev->priv;
223 set_fman_mac_params(mac_dev, ¶ms);
225 if (priv->max_speed == SPEED_10000)
226 params.phy_if = PHY_INTERFACE_MODE_XGMII;
228 mac_dev->fman_mac = memac_config(¶ms);
229 if (!mac_dev->fman_mac) {
234 err = memac_cfg_max_frame_len(mac_dev->fman_mac, fman_get_max_frm());
236 goto _return_fm_mac_free;
238 err = memac_cfg_reset_on_init(mac_dev->fman_mac, true);
240 goto _return_fm_mac_free;
242 err = memac_cfg_fixed_link(mac_dev->fman_mac, priv->fixed_link);
244 goto _return_fm_mac_free;
246 err = memac_init(mac_dev->fman_mac);
248 goto _return_fm_mac_free;
250 dev_info(priv->dev, "FMan MEMAC\n");
255 memac_free(mac_dev->fman_mac);
261 static int start(struct mac_device *mac_dev)
264 struct phy_device *phy_dev = mac_dev->phy_dev;
265 struct mac_priv_s *priv = mac_dev->priv;
267 err = priv->enable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
274 static int stop(struct mac_device *mac_dev)
276 struct mac_priv_s *priv = mac_dev->priv;
278 if (mac_dev->phy_dev)
279 phy_stop(mac_dev->phy_dev);
281 return priv->disable(mac_dev->fman_mac, COMM_MODE_RX_AND_TX);
284 static int set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
286 struct mac_priv_s *priv;
287 struct mac_address *old_addr, *tmp;
288 struct netdev_hw_addr *ha;
292 priv = mac_dev->priv;
294 /* Clear previous address list */
295 list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
296 addr = (enet_addr_t *)old_addr->addr;
297 err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
301 list_del(&old_addr->list);
305 /* Add all the addresses from the new list */
306 netdev_for_each_mc_addr(ha, net_dev) {
307 addr = (enet_addr_t *)ha->addr;
308 err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
312 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
316 ether_addr_copy(tmp->addr, ha->addr);
317 list_add(&tmp->list, &priv->mc_addr_list);
323 * fman_set_mac_active_pause
324 * @mac_dev: A pointer to the MAC device
325 * @rx: Pause frame setting for RX
326 * @tx: Pause frame setting for TX
328 * Set the MAC RX/TX PAUSE frames settings
330 * Avoid redundant calls to FMD, if the MAC driver already contains the desired
331 * active PAUSE settings. Otherwise, the new active settings should be reflected
334 * Return: 0 on success; Error code otherwise.
336 int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
338 struct fman_mac *fman_mac = mac_dev->fman_mac;
341 if (rx != mac_dev->rx_pause_active) {
342 err = mac_dev->set_rx_pause(fman_mac, rx);
343 if (likely(err == 0))
344 mac_dev->rx_pause_active = rx;
347 if (tx != mac_dev->tx_pause_active) {
348 u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
349 FSL_FM_PAUSE_TIME_DISABLE);
351 err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
353 if (likely(err == 0))
354 mac_dev->tx_pause_active = tx;
359 EXPORT_SYMBOL(fman_set_mac_active_pause);
363 * @mac_dev: A pointer to the MAC device
364 * @rx: Return value for RX setting
365 * @tx: Return value for TX setting
367 * Determine the MAC RX/TX PAUSE frames settings based on PHY
368 * autonegotiation or values set by eththool.
370 * Return: Pointer to FMan device.
372 void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
375 struct phy_device *phy_dev = mac_dev->phy_dev;
376 u16 lcl_adv, rmt_adv;
379 *rx_pause = *tx_pause = false;
381 if (!phy_dev->duplex)
384 /* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
385 * are those set by ethtool.
387 if (!mac_dev->autoneg_pause) {
388 *rx_pause = mac_dev->rx_pause_req;
389 *tx_pause = mac_dev->tx_pause_req;
393 /* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
394 * settings depend on the result of the link negotiation.
397 /* get local capabilities */
399 if (phy_dev->advertising & ADVERTISED_Pause)
400 lcl_adv |= ADVERTISE_PAUSE_CAP;
401 if (phy_dev->advertising & ADVERTISED_Asym_Pause)
402 lcl_adv |= ADVERTISE_PAUSE_ASYM;
404 /* get link partner capabilities */
407 rmt_adv |= LPA_PAUSE_CAP;
408 if (phy_dev->asym_pause)
409 rmt_adv |= LPA_PAUSE_ASYM;
411 /* Calculate TX/RX settings based on local and peer advertised
412 * symmetric/asymmetric PAUSE capabilities.
414 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
415 if (flowctrl & FLOW_CTRL_RX)
417 if (flowctrl & FLOW_CTRL_TX)
420 EXPORT_SYMBOL(fman_get_pause_cfg);
422 static void adjust_link_void(struct net_device *net_dev)
426 static void adjust_link_dtsec(struct net_device *net_dev)
428 struct device *dev = net_dev->dev.parent;
429 struct dpaa_eth_data *eth_data = dev->platform_data;
430 struct mac_device *mac_dev = eth_data->mac_dev;
431 struct phy_device *phy_dev = mac_dev->phy_dev;
432 struct fman_mac *fman_mac;
433 bool rx_pause, tx_pause;
436 fman_mac = mac_dev->fman_mac;
437 if (!phy_dev->link) {
438 dtsec_restart_autoneg(fman_mac);
443 dtsec_adjust_link(fman_mac, phy_dev->speed);
444 fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
445 err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
447 netdev_err(net_dev, "fman_set_mac_active_pause() = %d\n", err);
450 static void adjust_link_memac(struct net_device *net_dev)
452 struct device *dev = net_dev->dev.parent;
453 struct dpaa_eth_data *eth_data = dev->platform_data;
454 struct mac_device *mac_dev = eth_data->mac_dev;
455 struct phy_device *phy_dev = mac_dev->phy_dev;
456 struct fman_mac *fman_mac;
457 bool rx_pause, tx_pause;
460 fman_mac = mac_dev->fman_mac;
461 memac_adjust_link(fman_mac, phy_dev->speed);
463 fman_get_pause_cfg(mac_dev, &rx_pause, &tx_pause);
464 err = fman_set_mac_active_pause(mac_dev, rx_pause, tx_pause);
466 netdev_err(net_dev, "fman_set_mac_active_pause() = %d\n", err);
469 /* Initializes driver's PHY state, and attaches to the PHY.
470 * Returns 0 on success.
472 static int init_phy(struct net_device *net_dev,
473 struct mac_device *mac_dev,
474 void (*adj_lnk)(struct net_device *))
476 struct phy_device *phy_dev;
477 struct mac_priv_s *priv = mac_dev->priv;
479 phy_dev = of_phy_connect(net_dev, priv->phy_node, adj_lnk, 0,
482 netdev_err(net_dev, "Could not connect to PHY\n");
486 /* Remove any features not supported by the controller */
487 phy_dev->supported &= mac_dev->if_support;
488 /* Enable the symmetric and asymmetric PAUSE frame advertisements,
489 * as most of the PHY drivers do not enable them by default.
491 phy_dev->supported |= (SUPPORTED_Pause | SUPPORTED_Asym_Pause);
492 phy_dev->advertising = phy_dev->supported;
494 mac_dev->phy_dev = phy_dev;
499 static int dtsec_init_phy(struct net_device *net_dev,
500 struct mac_device *mac_dev)
502 return init_phy(net_dev, mac_dev, &adjust_link_dtsec);
505 static int tgec_init_phy(struct net_device *net_dev,
506 struct mac_device *mac_dev)
508 return init_phy(net_dev, mac_dev, adjust_link_void);
511 static int memac_init_phy(struct net_device *net_dev,
512 struct mac_device *mac_dev)
514 return init_phy(net_dev, mac_dev, &adjust_link_memac);
517 static void setup_dtsec(struct mac_device *mac_dev)
519 mac_dev->init_phy = dtsec_init_phy;
520 mac_dev->init = dtsec_initialization;
521 mac_dev->set_promisc = dtsec_set_promiscuous;
522 mac_dev->change_addr = dtsec_modify_mac_address;
523 mac_dev->add_hash_mac_addr = dtsec_add_hash_mac_address;
524 mac_dev->remove_hash_mac_addr = dtsec_del_hash_mac_address;
525 mac_dev->set_tx_pause = dtsec_set_tx_pause_frames;
526 mac_dev->set_rx_pause = dtsec_accept_rx_pause_frames;
527 mac_dev->set_exception = dtsec_set_exception;
528 mac_dev->set_multi = set_multi;
529 mac_dev->start = start;
530 mac_dev->stop = stop;
532 mac_dev->priv->enable = dtsec_enable;
533 mac_dev->priv->disable = dtsec_disable;
536 static void setup_tgec(struct mac_device *mac_dev)
538 mac_dev->init_phy = tgec_init_phy;
539 mac_dev->init = tgec_initialization;
540 mac_dev->set_promisc = tgec_set_promiscuous;
541 mac_dev->change_addr = tgec_modify_mac_address;
542 mac_dev->add_hash_mac_addr = tgec_add_hash_mac_address;
543 mac_dev->remove_hash_mac_addr = tgec_del_hash_mac_address;
544 mac_dev->set_tx_pause = tgec_set_tx_pause_frames;
545 mac_dev->set_rx_pause = tgec_accept_rx_pause_frames;
546 mac_dev->set_exception = tgec_set_exception;
547 mac_dev->set_multi = set_multi;
548 mac_dev->start = start;
549 mac_dev->stop = stop;
551 mac_dev->priv->enable = tgec_enable;
552 mac_dev->priv->disable = tgec_disable;
555 static void setup_memac(struct mac_device *mac_dev)
557 mac_dev->init_phy = memac_init_phy;
558 mac_dev->init = memac_initialization;
559 mac_dev->set_promisc = memac_set_promiscuous;
560 mac_dev->change_addr = memac_modify_mac_address;
561 mac_dev->add_hash_mac_addr = memac_add_hash_mac_address;
562 mac_dev->remove_hash_mac_addr = memac_del_hash_mac_address;
563 mac_dev->set_tx_pause = memac_set_tx_pause_frames;
564 mac_dev->set_rx_pause = memac_accept_rx_pause_frames;
565 mac_dev->set_exception = memac_set_exception;
566 mac_dev->set_multi = set_multi;
567 mac_dev->start = start;
568 mac_dev->stop = stop;
570 mac_dev->priv->enable = memac_enable;
571 mac_dev->priv->disable = memac_disable;
574 #define DTSEC_SUPPORTED \
575 (SUPPORTED_10baseT_Half \
576 | SUPPORTED_10baseT_Full \
577 | SUPPORTED_100baseT_Half \
578 | SUPPORTED_100baseT_Full \
579 | SUPPORTED_Autoneg \
581 | SUPPORTED_Asym_Pause \
584 static DEFINE_MUTEX(eth_lock);
586 static const char phy_str[][11] = {
587 [PHY_INTERFACE_MODE_MII] = "mii",
588 [PHY_INTERFACE_MODE_GMII] = "gmii",
589 [PHY_INTERFACE_MODE_SGMII] = "sgmii",
590 [PHY_INTERFACE_MODE_TBI] = "tbi",
591 [PHY_INTERFACE_MODE_RMII] = "rmii",
592 [PHY_INTERFACE_MODE_RGMII] = "rgmii",
593 [PHY_INTERFACE_MODE_RGMII_ID] = "rgmii-id",
594 [PHY_INTERFACE_MODE_RGMII_RXID] = "rgmii-rxid",
595 [PHY_INTERFACE_MODE_RGMII_TXID] = "rgmii-txid",
596 [PHY_INTERFACE_MODE_RTBI] = "rtbi",
597 [PHY_INTERFACE_MODE_XGMII] = "xgmii"
600 static phy_interface_t __pure __attribute__((nonnull)) str2phy(const char *str)
604 for (i = 0; i < ARRAY_SIZE(phy_str); i++)
605 if (strcmp(str, phy_str[i]) == 0)
606 return (phy_interface_t)i;
608 return PHY_INTERFACE_MODE_MII;
611 static const u16 phy2speed[] = {
612 [PHY_INTERFACE_MODE_MII] = SPEED_100,
613 [PHY_INTERFACE_MODE_GMII] = SPEED_1000,
614 [PHY_INTERFACE_MODE_SGMII] = SPEED_1000,
615 [PHY_INTERFACE_MODE_TBI] = SPEED_1000,
616 [PHY_INTERFACE_MODE_RMII] = SPEED_100,
617 [PHY_INTERFACE_MODE_RGMII] = SPEED_1000,
618 [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000,
619 [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000,
620 [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000,
621 [PHY_INTERFACE_MODE_RTBI] = SPEED_1000,
622 [PHY_INTERFACE_MODE_XGMII] = SPEED_10000
625 static struct platform_device *dpaa_eth_add_device(int fman_id,
626 struct mac_device *mac_dev,
627 struct device_node *node)
629 struct platform_device *pdev;
630 struct dpaa_eth_data data;
631 struct mac_priv_s *priv;
632 static int dpaa_eth_dev_cnt;
635 priv = mac_dev->priv;
637 data.mac_dev = mac_dev;
638 data.mac_hw_id = priv->cell_index;
639 data.fman_hw_id = fman_id;
640 data.mac_node = node;
642 mutex_lock(ð_lock);
644 pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
650 ret = platform_device_add_data(pdev, &data, sizeof(data));
654 ret = platform_device_add(pdev);
659 mutex_unlock(ð_lock);
664 platform_device_put(pdev);
666 mutex_unlock(ð_lock);
671 static const struct of_device_id mac_match[] = {
672 { .compatible = "fsl,fman-dtsec" },
673 { .compatible = "fsl,fman-xgec" },
674 { .compatible = "fsl,fman-memac" },
677 MODULE_DEVICE_TABLE(of, mac_match);
679 static int mac_probe(struct platform_device *_of_dev)
681 int err, i, lenp, nph;
683 struct device_node *mac_node, *dev_node;
684 struct mac_device *mac_dev;
685 struct platform_device *of_dev;
687 struct mac_priv_s *priv;
689 const char *char_prop;
694 mac_node = dev->of_node;
696 mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
699 dev_err(dev, "devm_kzalloc() = %d\n", err);
702 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
708 /* Save private information */
709 mac_dev->priv = priv;
712 if (of_device_is_compatible(mac_node, "fsl,fman-dtsec")) {
713 setup_dtsec(mac_dev);
714 priv->internal_phy_node = of_parse_phandle(mac_node,
716 } else if (of_device_is_compatible(mac_node, "fsl,fman-xgec")) {
718 } else if (of_device_is_compatible(mac_node, "fsl,fman-memac")) {
719 setup_memac(mac_dev);
720 priv->internal_phy_node = of_parse_phandle(mac_node,
723 dev_err(dev, "MAC node (%s) contains unsupported MAC\n",
724 mac_node->full_name);
729 /* Register mac_dev */
730 dev_set_drvdata(dev, mac_dev);
732 INIT_LIST_HEAD(&priv->mc_addr_list);
734 /* Get the FM node */
735 dev_node = of_get_parent(mac_node);
737 dev_err(dev, "of_get_parent(%s) failed\n",
738 mac_node->full_name);
740 goto _return_dev_set_drvdata;
743 of_dev = of_find_device_by_node(dev_node);
745 dev_err(dev, "of_find_device_by_node(%s) failed\n",
746 dev_node->full_name);
748 goto _return_of_node_put;
751 /* Get the FMan cell-index */
752 u32_prop = of_get_property(dev_node, "cell-index", &lenp);
754 dev_err(dev, "of_get_property(%s, cell-index) failed\n",
755 dev_node->full_name);
757 goto _return_of_node_put;
759 WARN_ON(lenp != sizeof(u32));
760 /* cell-index 0 => FMan id 1 */
761 fman_id = (u8)(fdt32_to_cpu(u32_prop[0]) + 1);
763 priv->fman = fman_bind(&of_dev->dev);
765 dev_err(dev, "fman_bind(%s) failed\n", dev_node->full_name);
767 goto _return_of_node_put;
770 of_node_put(dev_node);
772 /* Get the address of the memory mapped registers */
773 err = of_address_to_resource(mac_node, 0, &res);
775 dev_err(dev, "of_address_to_resource(%s) = %d\n",
776 mac_node->full_name, err);
777 goto _return_dev_set_drvdata;
780 mac_dev->res = __devm_request_region(dev,
781 fman_get_mem_region(priv->fman),
782 res.start, res.end + 1 - res.start,
785 dev_err(dev, "__devm_request_mem_region(mac) failed\n");
787 goto _return_dev_set_drvdata;
790 priv->vaddr = devm_ioremap(dev, mac_dev->res->start,
791 mac_dev->res->end + 1 - mac_dev->res->start);
793 dev_err(dev, "devm_ioremap() failed\n");
795 goto _return_dev_set_drvdata;
798 if (!of_device_is_available(mac_node)) {
799 devm_iounmap(dev, priv->vaddr);
800 __devm_release_region(dev, fman_get_mem_region(priv->fman),
801 res.start, res.end + 1 - res.start);
802 devm_kfree(dev, mac_dev);
803 dev_set_drvdata(dev, NULL);
807 /* Get the cell-index */
808 u32_prop = of_get_property(mac_node, "cell-index", &lenp);
810 dev_err(dev, "of_get_property(%s, cell-index) failed\n",
811 mac_node->full_name);
813 goto _return_dev_set_drvdata;
815 WARN_ON(lenp != sizeof(u32));
816 priv->cell_index = (u8)fdt32_to_cpu(u32_prop[0]);
818 /* Get the MAC address */
819 mac_addr = of_get_mac_address(mac_node);
821 dev_err(dev, "of_get_mac_address(%s) failed\n",
822 mac_node->full_name);
824 goto _return_dev_set_drvdata;
826 memcpy(mac_dev->addr, mac_addr, sizeof(mac_dev->addr));
828 /* Get the port handles */
829 nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
830 if (unlikely(nph < 0)) {
831 dev_err(dev, "of_count_phandle_with_args(%s, fsl,fman-ports) failed\n",
832 mac_node->full_name);
834 goto _return_dev_set_drvdata;
837 if (nph != ARRAY_SIZE(mac_dev->port)) {
838 dev_err(dev, "Not supported number of fman-ports handles of mac node %s from device tree\n",
839 mac_node->full_name);
841 goto _return_dev_set_drvdata;
844 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
845 /* Find the port node */
846 dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
848 dev_err(dev, "of_parse_phandle(%s, fsl,fman-ports) failed\n",
849 mac_node->full_name);
851 goto _return_of_node_put;
854 of_dev = of_find_device_by_node(dev_node);
856 dev_err(dev, "of_find_device_by_node(%s) failed\n",
857 dev_node->full_name);
859 goto _return_of_node_put;
862 mac_dev->port[i] = fman_port_bind(&of_dev->dev);
863 if (!mac_dev->port[i]) {
864 dev_err(dev, "dev_get_drvdata(%s) failed\n",
865 dev_node->full_name);
867 goto _return_of_node_put;
869 of_node_put(dev_node);
872 /* Get the PHY connection type */
873 char_prop = (const char *)of_get_property(mac_node,
874 "phy-connection-type", NULL);
877 "of_get_property(%s, phy-connection-type) failed. Defaulting to MII\n",
878 mac_node->full_name);
879 priv->phy_if = PHY_INTERFACE_MODE_MII;
881 priv->phy_if = str2phy(char_prop);
884 priv->speed = phy2speed[priv->phy_if];
885 priv->max_speed = priv->speed;
886 mac_dev->if_support = DTSEC_SUPPORTED;
887 /* We don't support half-duplex in SGMII mode */
888 if (priv->phy_if == PHY_INTERFACE_MODE_SGMII)
889 mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
890 SUPPORTED_100baseT_Half);
892 /* Gigabit support (no half-duplex) */
893 if (priv->max_speed == 1000)
894 mac_dev->if_support |= SUPPORTED_1000baseT_Full;
896 /* The 10G interface only supports one mode */
897 if (priv->phy_if == PHY_INTERFACE_MODE_XGMII)
898 mac_dev->if_support = SUPPORTED_10000baseT_Full;
900 /* Get the rest of the PHY information */
901 priv->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
902 if (!priv->phy_node && of_phy_is_fixed_link(mac_node)) {
903 struct phy_device *phy;
905 err = of_phy_register_fixed_link(mac_node);
907 goto _return_dev_set_drvdata;
909 priv->fixed_link = kzalloc(sizeof(*priv->fixed_link),
911 if (!priv->fixed_link)
912 goto _return_dev_set_drvdata;
914 priv->phy_node = of_node_get(mac_node);
915 phy = of_phy_find_device(priv->phy_node);
917 goto _return_dev_set_drvdata;
919 priv->fixed_link->link = phy->link;
920 priv->fixed_link->speed = phy->speed;
921 priv->fixed_link->duplex = phy->duplex;
922 priv->fixed_link->pause = phy->pause;
923 priv->fixed_link->asym_pause = phy->asym_pause;
926 err = mac_dev->init(mac_dev);
928 dev_err(dev, "mac_dev->init() = %d\n", err);
929 of_node_put(priv->phy_node);
930 goto _return_dev_set_drvdata;
933 /* pause frame autonegotiation enabled */
934 mac_dev->autoneg_pause = true;
936 /* By intializing the values to false, force FMD to enable PAUSE frames
939 mac_dev->rx_pause_req = true;
940 mac_dev->tx_pause_req = true;
941 mac_dev->rx_pause_active = false;
942 mac_dev->tx_pause_active = false;
943 err = fman_set_mac_active_pause(mac_dev, true, true);
945 dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
947 dev_info(dev, "FMan MAC address: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
948 mac_dev->addr[0], mac_dev->addr[1], mac_dev->addr[2],
949 mac_dev->addr[3], mac_dev->addr[4], mac_dev->addr[5]);
951 priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev, mac_node);
952 if (IS_ERR(priv->eth_dev)) {
953 dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
955 priv->eth_dev = NULL;
961 of_node_put(dev_node);
962 _return_dev_set_drvdata:
963 kfree(priv->fixed_link);
964 dev_set_drvdata(dev, NULL);
969 static struct platform_driver mac_driver = {
971 .name = KBUILD_MODNAME,
972 .of_match_table = mac_match,
977 builtin_platform_driver(mac_driver);