1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-or-later
3 * Copyright 2008 - 2015 Freescale Semiconductor Inc.
6 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
23 #include "fman_dtsec.h"
24 #include "fman_tgec.h"
25 #include "fman_memac.h"
27 MODULE_LICENSE("Dual BSD/GPL");
28 MODULE_DESCRIPTION("FSL FMan MAC API based driver");
33 /* List of multicast addresses */
34 struct list_head mc_addr_list;
35 struct platform_device *eth_dev;
41 struct list_head list;
44 static void mac_exception(struct mac_device *mac_dev,
45 enum fman_mac_exceptions ex)
47 if (ex == FM_MAC_EX_10G_RX_FIFO_OVFL) {
48 /* don't flag RX FIFO after the first */
49 mac_dev->set_exception(mac_dev->fman_mac,
50 FM_MAC_EX_10G_RX_FIFO_OVFL, false);
51 dev_err(mac_dev->dev, "10G MAC got RX FIFO Error = %x\n", ex);
54 dev_dbg(mac_dev->dev, "%s:%s() -> %d\n", KBUILD_BASENAME ".c",
58 int fman_set_multi(struct net_device *net_dev, struct mac_device *mac_dev)
60 struct mac_priv_s *priv;
61 struct mac_address *old_addr, *tmp;
62 struct netdev_hw_addr *ha;
68 /* Clear previous address list */
69 list_for_each_entry_safe(old_addr, tmp, &priv->mc_addr_list, list) {
70 addr = (enet_addr_t *)old_addr->addr;
71 err = mac_dev->remove_hash_mac_addr(mac_dev->fman_mac, addr);
75 list_del(&old_addr->list);
79 /* Add all the addresses from the new list */
80 netdev_for_each_mc_addr(ha, net_dev) {
81 addr = (enet_addr_t *)ha->addr;
82 err = mac_dev->add_hash_mac_addr(mac_dev->fman_mac, addr);
86 tmp = kmalloc(sizeof(*tmp), GFP_ATOMIC);
90 ether_addr_copy(tmp->addr, ha->addr);
91 list_add(&tmp->list, &priv->mc_addr_list);
97 * fman_set_mac_active_pause
98 * @mac_dev: A pointer to the MAC device
99 * @rx: Pause frame setting for RX
100 * @tx: Pause frame setting for TX
102 * Set the MAC RX/TX PAUSE frames settings
104 * Avoid redundant calls to FMD, if the MAC driver already contains the desired
105 * active PAUSE settings. Otherwise, the new active settings should be reflected
108 * Return: 0 on success; Error code otherwise.
110 int fman_set_mac_active_pause(struct mac_device *mac_dev, bool rx, bool tx)
112 struct fman_mac *fman_mac = mac_dev->fman_mac;
115 if (rx != mac_dev->rx_pause_active) {
116 err = mac_dev->set_rx_pause(fman_mac, rx);
117 if (likely(err == 0))
118 mac_dev->rx_pause_active = rx;
121 if (tx != mac_dev->tx_pause_active) {
122 u16 pause_time = (tx ? FSL_FM_PAUSE_TIME_ENABLE :
123 FSL_FM_PAUSE_TIME_DISABLE);
125 err = mac_dev->set_tx_pause(fman_mac, 0, pause_time, 0);
127 if (likely(err == 0))
128 mac_dev->tx_pause_active = tx;
133 EXPORT_SYMBOL(fman_set_mac_active_pause);
137 * @mac_dev: A pointer to the MAC device
138 * @rx_pause: Return value for RX setting
139 * @tx_pause: Return value for TX setting
141 * Determine the MAC RX/TX PAUSE frames settings based on PHY
142 * autonegotiation or values set by eththool.
144 * Return: Pointer to FMan device.
146 void fman_get_pause_cfg(struct mac_device *mac_dev, bool *rx_pause,
149 struct phy_device *phy_dev = mac_dev->phy_dev;
150 u16 lcl_adv, rmt_adv;
153 *rx_pause = *tx_pause = false;
155 if (!phy_dev->duplex)
158 /* If PAUSE autonegotiation is disabled, the TX/RX PAUSE settings
159 * are those set by ethtool.
161 if (!mac_dev->autoneg_pause) {
162 *rx_pause = mac_dev->rx_pause_req;
163 *tx_pause = mac_dev->tx_pause_req;
167 /* Else if PAUSE autonegotiation is enabled, the TX/RX PAUSE
168 * settings depend on the result of the link negotiation.
171 /* get local capabilities */
172 lcl_adv = linkmode_adv_to_lcl_adv_t(phy_dev->advertising);
174 /* get link partner capabilities */
177 rmt_adv |= LPA_PAUSE_CAP;
178 if (phy_dev->asym_pause)
179 rmt_adv |= LPA_PAUSE_ASYM;
181 /* Calculate TX/RX settings based on local and peer advertised
182 * symmetric/asymmetric PAUSE capabilities.
184 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
185 if (flowctrl & FLOW_CTRL_RX)
187 if (flowctrl & FLOW_CTRL_TX)
190 EXPORT_SYMBOL(fman_get_pause_cfg);
192 #define DTSEC_SUPPORTED \
193 (SUPPORTED_10baseT_Half \
194 | SUPPORTED_10baseT_Full \
195 | SUPPORTED_100baseT_Half \
196 | SUPPORTED_100baseT_Full \
197 | SUPPORTED_Autoneg \
199 | SUPPORTED_Asym_Pause \
203 static DEFINE_MUTEX(eth_lock);
205 static const u16 phy2speed[] = {
206 [PHY_INTERFACE_MODE_MII] = SPEED_100,
207 [PHY_INTERFACE_MODE_GMII] = SPEED_1000,
208 [PHY_INTERFACE_MODE_SGMII] = SPEED_1000,
209 [PHY_INTERFACE_MODE_TBI] = SPEED_1000,
210 [PHY_INTERFACE_MODE_RMII] = SPEED_100,
211 [PHY_INTERFACE_MODE_RGMII] = SPEED_1000,
212 [PHY_INTERFACE_MODE_RGMII_ID] = SPEED_1000,
213 [PHY_INTERFACE_MODE_RGMII_RXID] = SPEED_1000,
214 [PHY_INTERFACE_MODE_RGMII_TXID] = SPEED_1000,
215 [PHY_INTERFACE_MODE_RTBI] = SPEED_1000,
216 [PHY_INTERFACE_MODE_QSGMII] = SPEED_1000,
217 [PHY_INTERFACE_MODE_XGMII] = SPEED_10000
220 static struct platform_device *dpaa_eth_add_device(int fman_id,
221 struct mac_device *mac_dev)
223 struct platform_device *pdev;
224 struct dpaa_eth_data data;
225 struct mac_priv_s *priv;
226 static int dpaa_eth_dev_cnt;
229 priv = mac_dev->priv;
231 data.mac_dev = mac_dev;
232 data.mac_hw_id = priv->cell_index;
233 data.fman_hw_id = fman_id;
235 mutex_lock(ð_lock);
236 pdev = platform_device_alloc("dpaa-ethernet", dpaa_eth_dev_cnt);
242 pdev->dev.parent = mac_dev->dev;
244 ret = platform_device_add_data(pdev, &data, sizeof(data));
248 ret = platform_device_add(pdev);
253 mutex_unlock(ð_lock);
258 platform_device_put(pdev);
260 mutex_unlock(ð_lock);
265 static const struct of_device_id mac_match[] = {
266 { .compatible = "fsl,fman-dtsec", .data = dtsec_initialization },
267 { .compatible = "fsl,fman-xgec", .data = tgec_initialization },
268 { .compatible = "fsl,fman-memac", .data = memac_initialization },
271 MODULE_DEVICE_TABLE(of, mac_match);
273 static int mac_probe(struct platform_device *_of_dev)
276 int (*init)(struct mac_device *mac_dev, struct device_node *mac_node,
277 struct fman_mac_params *params);
279 struct device_node *mac_node, *dev_node;
280 struct mac_device *mac_dev;
281 struct platform_device *of_dev;
282 struct mac_priv_s *priv;
283 struct fman_mac_params params;
286 phy_interface_t phy_if;
289 mac_node = dev->of_node;
290 init = of_device_get_match_data(dev);
292 mac_dev = devm_kzalloc(dev, sizeof(*mac_dev), GFP_KERNEL);
295 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
299 /* Save private information */
300 mac_dev->priv = priv;
303 INIT_LIST_HEAD(&priv->mc_addr_list);
305 /* Get the FM node */
306 dev_node = of_get_parent(mac_node);
308 dev_err(dev, "of_get_parent(%pOF) failed\n",
313 of_dev = of_find_device_by_node(dev_node);
315 dev_err(dev, "of_find_device_by_node(%pOF) failed\n", dev_node);
317 goto _return_of_node_put;
320 /* Get the FMan cell-index */
321 err = of_property_read_u32(dev_node, "cell-index", &val);
323 dev_err(dev, "failed to read cell-index for %pOF\n", dev_node);
325 goto _return_of_node_put;
327 /* cell-index 0 => FMan id 1 */
328 fman_id = (u8)(val + 1);
330 priv->fman = fman_bind(&of_dev->dev);
332 dev_err(dev, "fman_bind(%pOF) failed\n", dev_node);
334 goto _return_of_node_put;
337 of_node_put(dev_node);
339 /* Get the address of the memory mapped registers */
340 mac_dev->res = platform_get_mem_or_io(_of_dev, 0);
342 dev_err(dev, "could not get registers\n");
346 err = devm_request_resource(dev, fman_get_mem_region(priv->fman),
349 dev_err_probe(dev, err, "could not request resource\n");
353 mac_dev->vaddr = devm_ioremap(dev, mac_dev->res->start,
354 resource_size(mac_dev->res));
355 if (!mac_dev->vaddr) {
356 dev_err(dev, "devm_ioremap() failed\n");
360 if (!of_device_is_available(mac_node))
363 /* Get the cell-index */
364 err = of_property_read_u32(mac_node, "cell-index", &val);
366 dev_err(dev, "failed to read cell-index for %pOF\n", mac_node);
369 priv->cell_index = (u8)val;
371 /* Get the MAC address */
372 err = of_get_mac_address(mac_node, mac_dev->addr);
374 dev_warn(dev, "of_get_mac_address(%pOF) failed\n", mac_node);
376 /* Get the port handles */
377 nph = of_count_phandle_with_args(mac_node, "fsl,fman-ports", NULL);
378 if (unlikely(nph < 0)) {
379 dev_err(dev, "of_count_phandle_with_args(%pOF, fsl,fman-ports) failed\n",
384 if (nph != ARRAY_SIZE(mac_dev->port)) {
385 dev_err(dev, "Not supported number of fman-ports handles of mac node %pOF from device tree\n",
390 for (i = 0; i < ARRAY_SIZE(mac_dev->port); i++) {
391 /* Find the port node */
392 dev_node = of_parse_phandle(mac_node, "fsl,fman-ports", i);
394 dev_err(dev, "of_parse_phandle(%pOF, fsl,fman-ports) failed\n",
399 of_dev = of_find_device_by_node(dev_node);
401 dev_err(dev, "of_find_device_by_node(%pOF) failed\n",
404 goto _return_of_node_put;
407 mac_dev->port[i] = fman_port_bind(&of_dev->dev);
408 if (!mac_dev->port[i]) {
409 dev_err(dev, "dev_get_drvdata(%pOF) failed\n",
412 goto _return_of_node_put;
414 of_node_put(dev_node);
417 /* Get the PHY connection type */
418 err = of_get_phy_mode(mac_node, &phy_if);
421 "of_get_phy_mode() for %pOF failed. Defaulting to SGMII\n",
423 phy_if = PHY_INTERFACE_MODE_SGMII;
425 mac_dev->phy_if = phy_if;
427 priv->speed = phy2speed[mac_dev->phy_if];
428 params.max_speed = priv->speed;
429 mac_dev->if_support = DTSEC_SUPPORTED;
430 /* We don't support half-duplex in SGMII mode */
431 if (mac_dev->phy_if == PHY_INTERFACE_MODE_SGMII)
432 mac_dev->if_support &= ~(SUPPORTED_10baseT_Half |
433 SUPPORTED_100baseT_Half);
435 /* Gigabit support (no half-duplex) */
436 if (params.max_speed == 1000)
437 mac_dev->if_support |= SUPPORTED_1000baseT_Full;
439 /* The 10G interface only supports one mode */
440 if (mac_dev->phy_if == PHY_INTERFACE_MODE_XGMII)
441 mac_dev->if_support = SUPPORTED_10000baseT_Full;
443 /* Get the rest of the PHY information */
444 mac_dev->phy_node = of_parse_phandle(mac_node, "phy-handle", 0);
446 params.basex_if = false;
447 params.mac_id = priv->cell_index;
448 params.fm = (void *)priv->fman;
449 params.exception_cb = mac_exception;
450 params.event_cb = mac_exception;
452 err = init(mac_dev, mac_node, ¶ms);
454 dev_err(dev, "mac_dev->init() = %d\n", err);
455 of_node_put(mac_dev->phy_node);
459 /* pause frame autonegotiation enabled */
460 mac_dev->autoneg_pause = true;
462 /* By intializing the values to false, force FMD to enable PAUSE frames
465 mac_dev->rx_pause_req = true;
466 mac_dev->tx_pause_req = true;
467 mac_dev->rx_pause_active = false;
468 mac_dev->tx_pause_active = false;
469 err = fman_set_mac_active_pause(mac_dev, true, true);
471 dev_err(dev, "fman_set_mac_active_pause() = %d\n", err);
473 if (!is_zero_ether_addr(mac_dev->addr))
474 dev_info(dev, "FMan MAC address: %pM\n", mac_dev->addr);
476 priv->eth_dev = dpaa_eth_add_device(fman_id, mac_dev);
477 if (IS_ERR(priv->eth_dev)) {
478 dev_err(dev, "failed to add Ethernet platform device for MAC %d\n",
480 priv->eth_dev = NULL;
486 of_node_put(dev_node);
490 static struct platform_driver mac_driver = {
492 .name = KBUILD_MODNAME,
493 .of_match_table = mac_match,
498 builtin_platform_driver(mac_driver);