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