750c6ce31734d7230b9600e1fac7a8f1dba517f5
[openwrt/staging/wigyori.git] / target / linux / layerscape / patches-5.4 / 701-net-0157-staging-fsl-dpaa2-mac-Add-Freescale-DPAA2-mac-driver.patch
1 From 50ba3c63dc3f4fbeac32fc5675a00756723d786d Mon Sep 17 00:00:00 2001
2 From: Bogdan Purcareata <bogdan.purcareata@nxp.com>
3 Date: Wed, 12 Apr 2017 12:42:07 +0000
4 Subject: [PATCH] staging: fsl-dpaa2/mac: Add Freescale DPAA2 mac driver
5
6 Introduce the DPAA2 mac driver, which manages Datapath
7 Media Access Control (DPMAC) objects discovered on the
8 MC bus.
9
10 This driver works as a proxy between phylib including phy
11 drivers and the Management Complex firmware. It receives
12 updates on link state changes from PHY lib and forwards
13 them to the Management Complex and receives interrupts
14 from the Management Complex whenever a request is made to
15 change the link state.
16
17 This is a squashed commit containing contributions of the
18 following owners:
19 Signed-off-by: Ioana Radulescu <ruxandra.radulescu@nxp.com>
20 Signed-off-by: Alex Marginean <alexandru.marginean@nxp.com>
21 Signed-off-by: Bogdan Hamciuc <bogdan.hamciuc@nxp.com>
22 Signed-off-by: Razvan Stefanescu <razvan.stefanescu@nxp.com>
23 Signed-off-by: Stuart Yoder <stuart.yoder@freescale.com>
24 Signed-off-by: J. German Rivera <German.Rivera@freescale.com>
25 Signed-off-by: Itai Katz <itai.katz@freescale.com>
26
27 Signed-off-by: Bogdan Purcareata <bogdan.purcareata@nxp.com>
28 ---
29 drivers/staging/fsl-dpaa2/Kconfig | 2 +
30 drivers/staging/fsl-dpaa2/Makefile | 1 +
31 drivers/staging/fsl-dpaa2/mac/Kconfig | 23 ++
32 drivers/staging/fsl-dpaa2/mac/Makefile | 10 +
33 drivers/staging/fsl-dpaa2/mac/mac.c | 670 +++++++++++++++++++++++++++++++++
34 5 files changed, 706 insertions(+)
35 create mode 100644 drivers/staging/fsl-dpaa2/mac/Kconfig
36 create mode 100644 drivers/staging/fsl-dpaa2/mac/Makefile
37 create mode 100644 drivers/staging/fsl-dpaa2/mac/mac.c
38
39 --- a/drivers/staging/fsl-dpaa2/Kconfig
40 +++ b/drivers/staging/fsl-dpaa2/Kconfig
41 @@ -17,3 +17,5 @@ config FSL_DPAA2_ETHSW
42 help
43 Driver for Freescale DPAA2 Ethernet Switch. Select
44 BRIDGE to have support for bridge tools.
45 +
46 +source "drivers/staging/fsl-dpaa2/mac/Kconfig"
47 --- a/drivers/staging/fsl-dpaa2/Makefile
48 +++ b/drivers/staging/fsl-dpaa2/Makefile
49 @@ -4,3 +4,4 @@
50 #
51
52 obj-$(CONFIG_FSL_DPAA2_ETHSW) += ethsw/
53 +obj-$(CONFIG_FSL_DPAA2_MAC) += mac/
54 --- /dev/null
55 +++ b/drivers/staging/fsl-dpaa2/mac/Kconfig
56 @@ -0,0 +1,23 @@
57 +config FSL_DPAA2_MAC
58 + tristate "DPAA2 MAC / PHY interface"
59 + depends on FSL_MC_BUS && FSL_DPAA2
60 + select MDIO_BUS_MUX_MMIOREG
61 + select FSL_XGMAC_MDIO
62 + select FIXED_PHY
63 + ---help---
64 + Prototype driver for DPAA2 MAC / PHY interface object.
65 + This driver works as a proxy between phylib including phy drivers and
66 + the MC firmware. It receives updates on link state changes from PHY
67 + lib and forwards them to MC and receives interrupt from MC whenever
68 + a request is made to change the link state.
69 +
70 +
71 +config FSL_DPAA2_MAC_NETDEVS
72 + bool "Expose net interfaces for PHYs"
73 + default n
74 + depends on FSL_DPAA2_MAC
75 + ---help---
76 + Exposes macX net interfaces which allow direct control over MACs and
77 + PHYs.
78 + .
79 + Leave disabled if unsure.
80 --- /dev/null
81 +++ b/drivers/staging/fsl-dpaa2/mac/Makefile
82 @@ -0,0 +1,10 @@
83 +
84 +obj-$(CONFIG_FSL_DPAA2_MAC) += dpaa2-mac.o
85 +
86 +dpaa2-mac-objs := mac.o dpmac.o
87 +
88 +all:
89 + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
90 +
91 +clean:
92 + make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
93 --- /dev/null
94 +++ b/drivers/staging/fsl-dpaa2/mac/mac.c
95 @@ -0,0 +1,670 @@
96 +/* Copyright 2015 Freescale Semiconductor Inc.
97 + *
98 + * Redistribution and use in source and binary forms, with or without
99 + * modification, are permitted provided that the following conditions are met:
100 + * * Redistributions of source code must retain the above copyright
101 + * notice, this list of conditions and the following disclaimer.
102 + * * Redistributions in binary form must reproduce the above copyright
103 + * notice, this list of conditions and the following disclaimer in the
104 + * documentation and/or other materials provided with the distribution.
105 + * * Neither the name of Freescale Semiconductor nor the
106 + * names of its contributors may be used to endorse or promote products
107 + * derived from this software without specific prior written permission.
108 + *
109 + *
110 + * ALTERNATIVELY, this software may be distributed under the terms of the
111 + * GNU General Public License ("GPL") as published by the Free Software
112 + * Foundation, either version 2 of that License or (at your option) any
113 + * later version.
114 + *
115 + * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
116 + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
117 + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
118 + * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
119 + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
120 + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
121 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
122 + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
123 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
124 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
125 + */
126 +
127 +#include <linux/module.h>
128 +
129 +#include <linux/netdevice.h>
130 +#include <linux/etherdevice.h>
131 +#include <linux/msi.h>
132 +#include <linux/rtnetlink.h>
133 +#include <linux/if_vlan.h>
134 +
135 +#include <uapi/linux/if_bridge.h>
136 +#include <net/netlink.h>
137 +
138 +#include <linux/of.h>
139 +#include <linux/of_mdio.h>
140 +#include <linux/of_net.h>
141 +#include <linux/phy.h>
142 +#include <linux/phy_fixed.h>
143 +
144 +#include "../../fsl-mc/include/mc.h"
145 +#include "../../fsl-mc/include/mc-sys.h"
146 +
147 +#include "dpmac.h"
148 +#include "dpmac-cmd.h"
149 +
150 +struct dpaa2_mac_priv {
151 + struct net_device *netdev;
152 + struct fsl_mc_device *mc_dev;
153 + struct dpmac_attr attr;
154 + struct dpmac_link_state old_state;
155 +};
156 +
157 +/* TODO: fix the 10G modes, mapping can't be right:
158 + * XGMII is paralel
159 + * XAUI is serial, using 8b/10b encoding
160 + * XFI is also serial but using 64b/66b encoding
161 + * they can't all map to XGMII...
162 + *
163 + * This must be kept in sync with enum dpmac_eth_if.
164 + */
165 +static phy_interface_t dpaa2_mac_iface_mode[] = {
166 + PHY_INTERFACE_MODE_MII, /* DPMAC_ETH_IF_MII */
167 + PHY_INTERFACE_MODE_RMII, /* DPMAC_ETH_IF_RMII */
168 + PHY_INTERFACE_MODE_SMII, /* DPMAC_ETH_IF_SMII */
169 + PHY_INTERFACE_MODE_GMII, /* DPMAC_ETH_IF_GMII */
170 + PHY_INTERFACE_MODE_RGMII, /* DPMAC_ETH_IF_RGMII */
171 + PHY_INTERFACE_MODE_SGMII, /* DPMAC_ETH_IF_SGMII */
172 + PHY_INTERFACE_MODE_QSGMII, /* DPMAC_ETH_IF_QSGMII */
173 + PHY_INTERFACE_MODE_XGMII, /* DPMAC_ETH_IF_XAUI */
174 + PHY_INTERFACE_MODE_XGMII, /* DPMAC_ETH_IF_XFI */
175 +};
176 +
177 +static void dpaa2_mac_link_changed(struct net_device *netdev)
178 +{
179 + struct phy_device *phydev;
180 + struct dpmac_link_state state = { 0 };
181 + struct dpaa2_mac_priv *priv = netdev_priv(netdev);
182 + int err;
183 +
184 + /* the PHY just notified us of link state change */
185 + phydev = netdev->phydev;
186 +
187 + state.up = !!phydev->link;
188 + if (phydev->link) {
189 + state.rate = phydev->speed;
190 +
191 + if (!phydev->duplex)
192 + state.options |= DPMAC_LINK_OPT_HALF_DUPLEX;
193 + if (phydev->autoneg)
194 + state.options |= DPMAC_LINK_OPT_AUTONEG;
195 +
196 + netif_carrier_on(netdev);
197 + } else {
198 + netif_carrier_off(netdev);
199 + }
200 +
201 + if (priv->old_state.up != state.up ||
202 + priv->old_state.rate != state.rate ||
203 + priv->old_state.options != state.options) {
204 + priv->old_state = state;
205 + phy_print_status(phydev);
206 + }
207 +
208 + /* We must interrogate MC at all times, because we don't know
209 + * when and whether a potential DPNI may have read the link state.
210 + */
211 + err = dpmac_set_link_state(priv->mc_dev->mc_io, 0,
212 + priv->mc_dev->mc_handle, &state);
213 + if (unlikely(err))
214 + dev_err(&priv->mc_dev->dev, "dpmac_set_link_state: %d\n", err);
215 +}
216 +
217 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
218 +static netdev_tx_t dpaa2_mac_drop_frame(struct sk_buff *skb,
219 + struct net_device *dev)
220 +{
221 + /* we don't support I/O for now, drop the frame */
222 + dev_kfree_skb_any(skb);
223 + return NETDEV_TX_OK;
224 +}
225 +
226 +static int dpaa2_mac_open(struct net_device *netdev)
227 +{
228 + /* start PHY state machine */
229 + phy_start(netdev->phydev);
230 +
231 + return 0;
232 +}
233 +
234 +static int dpaa2_mac_stop(struct net_device *netdev)
235 +{
236 + if (!netdev->phydev)
237 + goto done;
238 +
239 + /* stop PHY state machine */
240 + phy_stop(netdev->phydev);
241 +
242 + /* signal link down to firmware */
243 + netdev->phydev->link = 0;
244 + dpaa2_mac_link_changed(netdev);
245 +
246 +done:
247 + return 0;
248 +}
249 +
250 +static int dpaa2_mac_get_settings(struct net_device *netdev,
251 + struct ethtool_cmd *cmd)
252 +{
253 + return phy_ethtool_gset(netdev->phydev, cmd);
254 +}
255 +
256 +static int dpaa2_mac_set_settings(struct net_device *netdev,
257 + struct ethtool_cmd *cmd)
258 +{
259 + return phy_ethtool_sset(netdev->phydev, cmd);
260 +}
261 +
262 +static void dpaa2_mac_get_stats(struct net_device *netdev,
263 + struct rtnl_link_stats64 *storage)
264 +{
265 + struct dpaa2_mac_priv *priv = netdev_priv(netdev);
266 + u64 tmp;
267 + int err;
268 +
269 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
270 + DPMAC_CNT_EGR_MCAST_FRAME,
271 + &storage->tx_packets);
272 + if (err)
273 + goto error;
274 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
275 + DPMAC_CNT_EGR_BCAST_FRAME, &tmp);
276 + if (err)
277 + goto error;
278 + storage->tx_packets += tmp;
279 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
280 + DPMAC_CNT_EGR_UCAST_FRAME, &tmp);
281 + if (err)
282 + goto error;
283 + storage->tx_packets += tmp;
284 +
285 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
286 + DPMAC_CNT_EGR_UNDERSIZED, &storage->tx_dropped);
287 + if (err)
288 + goto error;
289 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
290 + DPMAC_CNT_EGR_BYTE, &storage->tx_bytes);
291 + if (err)
292 + goto error;
293 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
294 + DPMAC_CNT_EGR_ERR_FRAME, &storage->tx_errors);
295 + if (err)
296 + goto error;
297 +
298 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
299 + DPMAC_CNT_ING_ALL_FRAME, &storage->rx_packets);
300 + if (err)
301 + goto error;
302 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
303 + DPMAC_CNT_ING_MCAST_FRAME, &storage->multicast);
304 + if (err)
305 + goto error;
306 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
307 + DPMAC_CNT_ING_FRAME_DISCARD,
308 + &storage->rx_dropped);
309 + if (err)
310 + goto error;
311 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
312 + DPMAC_CNT_ING_ALIGN_ERR, &storage->rx_errors);
313 + if (err)
314 + goto error;
315 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
316 + DPMAC_CNT_ING_OVERSIZED, &tmp);
317 + if (err)
318 + goto error;
319 + storage->rx_errors += tmp;
320 + err = dpmac_get_counter(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle,
321 + DPMAC_CNT_ING_BYTE, &storage->rx_bytes);
322 + if (err)
323 + goto error;
324 +
325 + return;
326 +error:
327 + netdev_err(netdev, "dpmac_get_counter err %d\n", err);
328 +}
329 +
330 +static struct {
331 + enum dpmac_counter id;
332 + char name[ETH_GSTRING_LEN];
333 +} dpaa2_mac_counters[] = {
334 + {DPMAC_CNT_ING_ALL_FRAME, "rx all frames"},
335 + {DPMAC_CNT_ING_GOOD_FRAME, "rx frames ok"},
336 + {DPMAC_CNT_ING_ERR_FRAME, "rx frame errors"},
337 + {DPMAC_CNT_ING_FRAME_DISCARD, "rx frame discards"},
338 + {DPMAC_CNT_ING_UCAST_FRAME, "rx u-cast"},
339 + {DPMAC_CNT_ING_BCAST_FRAME, "rx b-cast"},
340 + {DPMAC_CNT_ING_MCAST_FRAME, "rx m-cast"},
341 + {DPMAC_CNT_ING_FRAME_64, "rx 64 bytes"},
342 + {DPMAC_CNT_ING_FRAME_127, "rx 65-127 bytes"},
343 + {DPMAC_CNT_ING_FRAME_255, "rx 128-255 bytes"},
344 + {DPMAC_CNT_ING_FRAME_511, "rx 256-511 bytes"},
345 + {DPMAC_CNT_ING_FRAME_1023, "rx 512-1023 bytes"},
346 + {DPMAC_CNT_ING_FRAME_1518, "rx 1024-1518 bytes"},
347 + {DPMAC_CNT_ING_FRAME_1519_MAX, "rx 1519-max bytes"},
348 + {DPMAC_CNT_ING_FRAG, "rx frags"},
349 + {DPMAC_CNT_ING_JABBER, "rx jabber"},
350 + {DPMAC_CNT_ING_ALIGN_ERR, "rx align errors"},
351 + {DPMAC_CNT_ING_OVERSIZED, "rx oversized"},
352 + {DPMAC_CNT_ING_VALID_PAUSE_FRAME, "rx pause"},
353 + {DPMAC_CNT_ING_BYTE, "rx bytes"},
354 + {DPMAC_CNT_ENG_GOOD_FRAME, "tx frames ok"},
355 + {DPMAC_CNT_EGR_UCAST_FRAME, "tx u-cast"},
356 + {DPMAC_CNT_EGR_MCAST_FRAME, "tx m-cast"},
357 + {DPMAC_CNT_EGR_BCAST_FRAME, "tx b-cast"},
358 + {DPMAC_CNT_EGR_ERR_FRAME, "tx frame errors"},
359 + {DPMAC_CNT_EGR_UNDERSIZED, "tx undersized"},
360 + {DPMAC_CNT_EGR_VALID_PAUSE_FRAME, "tx b-pause"},
361 + {DPMAC_CNT_EGR_BYTE, "tx bytes"},
362 +
363 +};
364 +
365 +static void dpaa2_mac_get_strings(struct net_device *netdev,
366 + u32 stringset, u8 *data)
367 +{
368 + int i;
369 +
370 + switch (stringset) {
371 + case ETH_SS_STATS:
372 + for (i = 0; i < ARRAY_SIZE(dpaa2_mac_counters); i++)
373 + memcpy(data + i * ETH_GSTRING_LEN,
374 + dpaa2_mac_counters[i].name,
375 + ETH_GSTRING_LEN);
376 + break;
377 + }
378 +}
379 +
380 +static void dpaa2_mac_get_ethtool_stats(struct net_device *netdev,
381 + struct ethtool_stats *stats,
382 + u64 *data)
383 +{
384 + struct dpaa2_mac_priv *priv = netdev_priv(netdev);
385 + int i;
386 + int err;
387 +
388 + for (i = 0; i < ARRAY_SIZE(dpaa2_mac_counters); i++) {
389 + err = dpmac_get_counter(priv->mc_dev->mc_io,
390 + 0,
391 + priv->mc_dev->mc_handle,
392 + dpaa2_mac_counters[i].id, &data[i]);
393 + if (err)
394 + netdev_err(netdev, "dpmac_get_counter[%s] err %d\n",
395 + dpaa2_mac_counters[i].name, err);
396 + }
397 +}
398 +
399 +static int dpaa2_mac_get_sset_count(struct net_device *dev, int sset)
400 +{
401 + switch (sset) {
402 + case ETH_SS_STATS:
403 + return ARRAY_SIZE(dpaa2_mac_counters);
404 + default:
405 + return -EOPNOTSUPP;
406 + }
407 +}
408 +
409 +static const struct net_device_ops dpaa2_mac_ndo_ops = {
410 + .ndo_start_xmit = &dpaa2_mac_drop_frame,
411 + .ndo_open = &dpaa2_mac_open,
412 + .ndo_stop = &dpaa2_mac_stop,
413 + .ndo_get_stats64 = &dpaa2_mac_get_stats,
414 +};
415 +
416 +static const struct ethtool_ops dpaa2_mac_ethtool_ops = {
417 + .get_settings = &dpaa2_mac_get_settings,
418 + .set_settings = &dpaa2_mac_set_settings,
419 + .get_strings = &dpaa2_mac_get_strings,
420 + .get_ethtool_stats = &dpaa2_mac_get_ethtool_stats,
421 + .get_sset_count = &dpaa2_mac_get_sset_count,
422 +};
423 +#endif /* CONFIG_FSL_DPAA2_MAC_NETDEVS */
424 +
425 +static void configure_link(struct dpaa2_mac_priv *priv,
426 + struct dpmac_link_cfg *cfg)
427 +{
428 + struct phy_device *phydev = priv->netdev->phydev;
429 +
430 + if (unlikely(!phydev))
431 + return;
432 +
433 + phydev->speed = cfg->rate;
434 + phydev->duplex = !!(cfg->options & DPMAC_LINK_OPT_HALF_DUPLEX);
435 +
436 + if (cfg->options & DPMAC_LINK_OPT_AUTONEG) {
437 + phydev->autoneg = 1;
438 + phydev->advertising |= ADVERTISED_Autoneg;
439 + } else {
440 + phydev->autoneg = 0;
441 + phydev->advertising &= ~ADVERTISED_Autoneg;
442 + }
443 +
444 + phy_start_aneg(phydev);
445 +}
446 +
447 +static irqreturn_t dpaa2_mac_irq_handler(int irq_num, void *arg)
448 +{
449 + struct device *dev = (struct device *)arg;
450 + struct fsl_mc_device *mc_dev = to_fsl_mc_device(dev);
451 + struct dpaa2_mac_priv *priv = dev_get_drvdata(dev);
452 + struct dpmac_link_cfg link_cfg;
453 + u32 status;
454 + int err;
455 +
456 + err = dpmac_get_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
457 + DPMAC_IRQ_INDEX, &status);
458 + if (unlikely(err || !status))
459 + return IRQ_NONE;
460 +
461 + /* DPNI-initiated link configuration; 'ifconfig up' also calls this */
462 + if (status & DPMAC_IRQ_EVENT_LINK_CFG_REQ) {
463 + err = dpmac_get_link_cfg(mc_dev->mc_io, 0, mc_dev->mc_handle,
464 + &link_cfg);
465 + if (unlikely(err))
466 + goto out;
467 +
468 + configure_link(priv, &link_cfg);
469 + }
470 +
471 +out:
472 + dpmac_clear_irq_status(mc_dev->mc_io, 0, mc_dev->mc_handle,
473 + DPMAC_IRQ_INDEX, status);
474 +
475 + return IRQ_HANDLED;
476 +}
477 +
478 +static int setup_irqs(struct fsl_mc_device *mc_dev)
479 +{
480 + int err = 0;
481 + struct fsl_mc_device_irq *irq;
482 +
483 + err = fsl_mc_allocate_irqs(mc_dev);
484 + if (err) {
485 + dev_err(&mc_dev->dev, "fsl_mc_allocate_irqs err %d\n", err);
486 + return err;
487 + }
488 +
489 + irq = mc_dev->irqs[0];
490 + err = devm_request_threaded_irq(&mc_dev->dev, irq->msi_desc->irq,
491 + NULL, &dpaa2_mac_irq_handler,
492 + IRQF_NO_SUSPEND | IRQF_ONESHOT,
493 + dev_name(&mc_dev->dev), &mc_dev->dev);
494 + if (err) {
495 + dev_err(&mc_dev->dev, "devm_request_threaded_irq err %d\n",
496 + err);
497 + goto free_irq;
498 + }
499 +
500 + err = dpmac_set_irq_mask(mc_dev->mc_io, 0, mc_dev->mc_handle,
501 + DPMAC_IRQ_INDEX, DPMAC_IRQ_EVENT_LINK_CFG_REQ);
502 + if (err) {
503 + dev_err(&mc_dev->dev, "dpmac_set_irq_mask err %d\n", err);
504 + goto free_irq;
505 + }
506 + err = dpmac_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle,
507 + DPMAC_IRQ_INDEX, 1);
508 + if (err) {
509 + dev_err(&mc_dev->dev, "dpmac_set_irq_enable err %d\n", err);
510 + goto free_irq;
511 + }
512 +
513 + return 0;
514 +
515 +free_irq:
516 + fsl_mc_free_irqs(mc_dev);
517 +
518 + return err;
519 +}
520 +
521 +static void teardown_irqs(struct fsl_mc_device *mc_dev)
522 +{
523 + int err;
524 +
525 + err = dpmac_set_irq_enable(mc_dev->mc_io, 0, mc_dev->mc_handle,
526 + DPMAC_IRQ_INDEX, 0);
527 + if (err)
528 + dev_err(&mc_dev->dev, "dpmac_set_irq_enable err %d\n", err);
529 +
530 + fsl_mc_free_irqs(mc_dev);
531 +}
532 +
533 +static struct device_node *find_dpmac_node(struct device *dev, u16 dpmac_id)
534 +{
535 + struct device_node *dpmacs, *dpmac = NULL;
536 + struct device_node *mc_node = dev->of_node;
537 + u32 id;
538 + int err;
539 +
540 + dpmacs = of_find_node_by_name(mc_node, "dpmacs");
541 + if (!dpmacs) {
542 + dev_err(dev, "No dpmacs subnode in device-tree\n");
543 + return NULL;
544 + }
545 +
546 + while ((dpmac = of_get_next_child(dpmacs, dpmac))) {
547 + err = of_property_read_u32(dpmac, "reg", &id);
548 + if (err)
549 + continue;
550 + if (id == dpmac_id)
551 + return dpmac;
552 + }
553 +
554 + return NULL;
555 +}
556 +
557 +static int dpaa2_mac_probe(struct fsl_mc_device *mc_dev)
558 +{
559 + struct device *dev;
560 + struct dpaa2_mac_priv *priv = NULL;
561 + struct device_node *phy_node, *dpmac_node;
562 + struct net_device *netdev;
563 + phy_interface_t if_mode;
564 + int err = 0;
565 +
566 + dev = &mc_dev->dev;
567 +
568 + /* prepare a net_dev structure to make the phy lib API happy */
569 + netdev = alloc_etherdev(sizeof(*priv));
570 + if (!netdev) {
571 + dev_err(dev, "alloc_etherdev error\n");
572 + err = -ENOMEM;
573 + goto err_exit;
574 + }
575 + priv = netdev_priv(netdev);
576 + priv->mc_dev = mc_dev;
577 + priv->netdev = netdev;
578 +
579 + SET_NETDEV_DEV(netdev, dev);
580 +
581 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
582 + snprintf(netdev->name, IFNAMSIZ, "mac%d", mc_dev->obj_desc.id);
583 +#endif
584 +
585 + dev_set_drvdata(dev, priv);
586 +
587 + err = fsl_mc_portal_allocate(mc_dev, 0, &mc_dev->mc_io);
588 + if (err || !mc_dev->mc_io) {
589 + dev_err(dev, "fsl_mc_portal_allocate error: %d\n", err);
590 + err = -ENODEV;
591 + goto err_free_netdev;
592 + }
593 +
594 + err = dpmac_open(mc_dev->mc_io, 0, mc_dev->obj_desc.id,
595 + &mc_dev->mc_handle);
596 + if (err || !mc_dev->mc_handle) {
597 + dev_err(dev, "dpmac_open error: %d\n", err);
598 + err = -ENODEV;
599 + goto err_free_mcp;
600 + }
601 +
602 + err = dpmac_get_attributes(mc_dev->mc_io, 0,
603 + mc_dev->mc_handle, &priv->attr);
604 + if (err) {
605 + dev_err(dev, "dpmac_get_attributes err %d\n", err);
606 + err = -EINVAL;
607 + goto err_close;
608 + }
609 +
610 + /* Look up the DPMAC node in the device-tree. */
611 + dpmac_node = find_dpmac_node(dev, priv->attr.id);
612 + if (!dpmac_node) {
613 + dev_err(dev, "No dpmac@%d subnode found.\n", priv->attr.id);
614 + err = -ENODEV;
615 + goto err_close;
616 + }
617 +
618 + err = setup_irqs(mc_dev);
619 + if (err) {
620 + err = -EFAULT;
621 + goto err_close;
622 + }
623 +
624 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
625 + /* OPTIONAL, register netdev just to make it visible to the user */
626 + netdev->netdev_ops = &dpaa2_mac_ndo_ops;
627 + netdev->ethtool_ops = &dpaa2_mac_ethtool_ops;
628 +
629 + /* phy starts up enabled so netdev should be up too */
630 + netdev->flags |= IFF_UP;
631 +
632 + err = register_netdev(priv->netdev);
633 + if (err < 0) {
634 + dev_err(dev, "register_netdev error %d\n", err);
635 + err = -ENODEV;
636 + goto err_free_irq;
637 + }
638 +#endif /* CONFIG_FSL_DPAA2_MAC_NETDEVS */
639 +
640 + /* probe the PHY as a fixed-link if the link type declared in DPC
641 + * explicitly mandates this
642 + */
643 + if (priv->attr.link_type == DPMAC_LINK_TYPE_FIXED)
644 + goto probe_fixed_link;
645 +
646 + if (priv->attr.eth_if < ARRAY_SIZE(dpaa2_mac_iface_mode)) {
647 + if_mode = dpaa2_mac_iface_mode[priv->attr.eth_if];
648 + dev_dbg(dev, "\tusing if mode %s for eth_if %d\n",
649 + phy_modes(if_mode), priv->attr.eth_if);
650 + } else {
651 + dev_warn(dev, "Unexpected interface mode %d, will probe as fixed link\n",
652 + priv->attr.eth_if);
653 + goto probe_fixed_link;
654 + }
655 +
656 + /* try to connect to the PHY */
657 + phy_node = of_parse_phandle(dpmac_node, "phy-handle", 0);
658 + if (!phy_node) {
659 + if (!phy_node) {
660 + dev_err(dev, "dpmac node has no phy-handle property\n");
661 + err = -ENODEV;
662 + goto err_no_phy;
663 + }
664 + }
665 + netdev->phydev = of_phy_connect(netdev, phy_node,
666 + &dpaa2_mac_link_changed, 0, if_mode);
667 + if (!netdev->phydev) {
668 + /* No need for dev_err(); the kernel's loud enough as it is. */
669 + dev_dbg(dev, "Can't of_phy_connect() now.\n");
670 + /* We might be waiting for the MDIO MUX to probe, so defer
671 + * our own probing.
672 + */
673 + err = -EPROBE_DEFER;
674 + goto err_defer;
675 + }
676 + dev_info(dev, "Connected to %s PHY.\n", phy_modes(if_mode));
677 +
678 +probe_fixed_link:
679 + if (!netdev->phydev) {
680 + struct fixed_phy_status status = {
681 + .link = 1,
682 + /* fixed-phys don't support 10Gbps speed for now */
683 + .speed = 1000,
684 + .duplex = 1,
685 + };
686 +
687 + /* try to register a fixed link phy */
688 + netdev->phydev = fixed_phy_register(PHY_POLL, &status, NULL);
689 + if (!netdev->phydev || IS_ERR(netdev->phydev)) {
690 + dev_err(dev, "error trying to register fixed PHY\n");
691 + /* So we don't crash unregister_netdev() later on */
692 + netdev->phydev = NULL;
693 + err = -EFAULT;
694 + goto err_no_phy;
695 + }
696 + dev_info(dev, "Registered fixed PHY.\n");
697 + }
698 +
699 + /* start PHY state machine */
700 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
701 + dpaa2_mac_open(netdev);
702 +#else /* CONFIG_FSL_DPAA2_MAC_NETDEVS */
703 + phy_start(netdev->phydev);
704 +#endif /* CONFIG_FSL_DPAA2_MAC_NETDEVS */
705 + return 0;
706 +
707 +err_defer:
708 +err_no_phy:
709 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
710 + unregister_netdev(netdev);
711 +err_free_irq:
712 +#endif
713 + teardown_irqs(mc_dev);
714 +err_close:
715 + dpmac_close(mc_dev->mc_io, 0, mc_dev->mc_handle);
716 +err_free_mcp:
717 + fsl_mc_portal_free(mc_dev->mc_io);
718 +err_free_netdev:
719 + free_netdev(netdev);
720 +err_exit:
721 + return err;
722 +}
723 +
724 +static int dpaa2_mac_remove(struct fsl_mc_device *mc_dev)
725 +{
726 + struct device *dev = &mc_dev->dev;
727 + struct dpaa2_mac_priv *priv = dev_get_drvdata(dev);
728 +
729 +#ifdef CONFIG_FSL_DPAA2_MAC_NETDEVS
730 + unregister_netdev(priv->netdev);
731 +#endif
732 + teardown_irqs(priv->mc_dev);
733 + dpmac_close(priv->mc_dev->mc_io, 0, priv->mc_dev->mc_handle);
734 + fsl_mc_portal_free(priv->mc_dev->mc_io);
735 + free_netdev(priv->netdev);
736 +
737 + dev_set_drvdata(dev, NULL);
738 + kfree(priv);
739 +
740 + return 0;
741 +}
742 +
743 +static const struct fsl_mc_device_id dpaa2_mac_match_id_table[] = {
744 + {
745 + .vendor = FSL_MC_VENDOR_FREESCALE,
746 + .obj_type = "dpmac",
747 + },
748 + { .vendor = 0x0 }
749 +};
750 +MODULE_DEVICE_TABLE(fslmc, dpaa2_mac_match_id_table);
751 +
752 +static struct fsl_mc_driver dpaa2_mac_drv = {
753 + .driver = {
754 + .name = KBUILD_MODNAME,
755 + .owner = THIS_MODULE,
756 + },
757 + .probe = dpaa2_mac_probe,
758 + .remove = dpaa2_mac_remove,
759 + .match_id_table = dpaa2_mac_match_id_table,
760 +};
761 +
762 +module_fsl_mc_driver(dpaa2_mac_drv);
763 +
764 +MODULE_LICENSE("GPL");
765 +MODULE_DESCRIPTION("DPAA2 PHY proxy interface driver");