net: ethernet: mediatek: add the missing of_node_put() after node is used done
[cascardo/linux.git] / drivers / net / ethernet / mediatek / mtk_eth_soc.c
1 /*   This program is free software; you can redistribute it and/or modify
2  *   it under the terms of the GNU General Public License as published by
3  *   the Free Software Foundation; version 2 of the License
4  *
5  *   This program is distributed in the hope that it will be useful,
6  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
7  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8  *   GNU General Public License for more details.
9  *
10  *   Copyright (C) 2009-2016 John Crispin <blogic@openwrt.org>
11  *   Copyright (C) 2009-2016 Felix Fietkau <nbd@openwrt.org>
12  *   Copyright (C) 2013-2016 Michael Lee <igvtee@gmail.com>
13  */
14
15 #include <linux/of_device.h>
16 #include <linux/of_mdio.h>
17 #include <linux/of_net.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/regmap.h>
20 #include <linux/clk.h>
21 #include <linux/if_vlan.h>
22 #include <linux/reset.h>
23 #include <linux/tcp.h>
24
25 #include "mtk_eth_soc.h"
26
27 static int mtk_msg_level = -1;
28 module_param_named(msg_level, mtk_msg_level, int, 0);
29 MODULE_PARM_DESC(msg_level, "Message level (-1=defaults,0=none,...,16=all)");
30
31 #define MTK_ETHTOOL_STAT(x) { #x, \
32                               offsetof(struct mtk_hw_stats, x) / sizeof(u64) }
33
34 /* strings used by ethtool */
35 static const struct mtk_ethtool_stats {
36         char str[ETH_GSTRING_LEN];
37         u32 offset;
38 } mtk_ethtool_stats[] = {
39         MTK_ETHTOOL_STAT(tx_bytes),
40         MTK_ETHTOOL_STAT(tx_packets),
41         MTK_ETHTOOL_STAT(tx_skip),
42         MTK_ETHTOOL_STAT(tx_collisions),
43         MTK_ETHTOOL_STAT(rx_bytes),
44         MTK_ETHTOOL_STAT(rx_packets),
45         MTK_ETHTOOL_STAT(rx_overflow),
46         MTK_ETHTOOL_STAT(rx_fcs_errors),
47         MTK_ETHTOOL_STAT(rx_short_errors),
48         MTK_ETHTOOL_STAT(rx_long_errors),
49         MTK_ETHTOOL_STAT(rx_checksum_errors),
50         MTK_ETHTOOL_STAT(rx_flow_control_packets),
51 };
52
53 void mtk_w32(struct mtk_eth *eth, u32 val, unsigned reg)
54 {
55         __raw_writel(val, eth->base + reg);
56 }
57
58 u32 mtk_r32(struct mtk_eth *eth, unsigned reg)
59 {
60         return __raw_readl(eth->base + reg);
61 }
62
63 static int mtk_mdio_busy_wait(struct mtk_eth *eth)
64 {
65         unsigned long t_start = jiffies;
66
67         while (1) {
68                 if (!(mtk_r32(eth, MTK_PHY_IAC) & PHY_IAC_ACCESS))
69                         return 0;
70                 if (time_after(jiffies, t_start + PHY_IAC_TIMEOUT))
71                         break;
72                 usleep_range(10, 20);
73         }
74
75         dev_err(eth->dev, "mdio: MDIO timeout\n");
76         return -1;
77 }
78
79 static u32 _mtk_mdio_write(struct mtk_eth *eth, u32 phy_addr,
80                            u32 phy_register, u32 write_data)
81 {
82         if (mtk_mdio_busy_wait(eth))
83                 return -1;
84
85         write_data &= 0xffff;
86
87         mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_WRITE |
88                 (phy_register << PHY_IAC_REG_SHIFT) |
89                 (phy_addr << PHY_IAC_ADDR_SHIFT) | write_data,
90                 MTK_PHY_IAC);
91
92         if (mtk_mdio_busy_wait(eth))
93                 return -1;
94
95         return 0;
96 }
97
98 static u32 _mtk_mdio_read(struct mtk_eth *eth, int phy_addr, int phy_reg)
99 {
100         u32 d;
101
102         if (mtk_mdio_busy_wait(eth))
103                 return 0xffff;
104
105         mtk_w32(eth, PHY_IAC_ACCESS | PHY_IAC_START | PHY_IAC_READ |
106                 (phy_reg << PHY_IAC_REG_SHIFT) |
107                 (phy_addr << PHY_IAC_ADDR_SHIFT),
108                 MTK_PHY_IAC);
109
110         if (mtk_mdio_busy_wait(eth))
111                 return 0xffff;
112
113         d = mtk_r32(eth, MTK_PHY_IAC) & 0xffff;
114
115         return d;
116 }
117
118 static int mtk_mdio_write(struct mii_bus *bus, int phy_addr,
119                           int phy_reg, u16 val)
120 {
121         struct mtk_eth *eth = bus->priv;
122
123         return _mtk_mdio_write(eth, phy_addr, phy_reg, val);
124 }
125
126 static int mtk_mdio_read(struct mii_bus *bus, int phy_addr, int phy_reg)
127 {
128         struct mtk_eth *eth = bus->priv;
129
130         return _mtk_mdio_read(eth, phy_addr, phy_reg);
131 }
132
133 static void mtk_phy_link_adjust(struct net_device *dev)
134 {
135         struct mtk_mac *mac = netdev_priv(dev);
136         u16 lcl_adv = 0, rmt_adv = 0;
137         u8 flowctrl;
138         u32 mcr = MAC_MCR_MAX_RX_1536 | MAC_MCR_IPG_CFG |
139                   MAC_MCR_FORCE_MODE | MAC_MCR_TX_EN |
140                   MAC_MCR_RX_EN | MAC_MCR_BACKOFF_EN |
141                   MAC_MCR_BACKPR_EN;
142
143         switch (mac->phy_dev->speed) {
144         case SPEED_1000:
145                 mcr |= MAC_MCR_SPEED_1000;
146                 break;
147         case SPEED_100:
148                 mcr |= MAC_MCR_SPEED_100;
149                 break;
150         };
151
152         if (mac->phy_dev->link)
153                 mcr |= MAC_MCR_FORCE_LINK;
154
155         if (mac->phy_dev->duplex) {
156                 mcr |= MAC_MCR_FORCE_DPX;
157
158                 if (mac->phy_dev->pause)
159                         rmt_adv = LPA_PAUSE_CAP;
160                 if (mac->phy_dev->asym_pause)
161                         rmt_adv |= LPA_PAUSE_ASYM;
162
163                 if (mac->phy_dev->advertising & ADVERTISED_Pause)
164                         lcl_adv |= ADVERTISE_PAUSE_CAP;
165                 if (mac->phy_dev->advertising & ADVERTISED_Asym_Pause)
166                         lcl_adv |= ADVERTISE_PAUSE_ASYM;
167
168                 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
169
170                 if (flowctrl & FLOW_CTRL_TX)
171                         mcr |= MAC_MCR_FORCE_TX_FC;
172                 if (flowctrl & FLOW_CTRL_RX)
173                         mcr |= MAC_MCR_FORCE_RX_FC;
174
175                 netif_dbg(mac->hw, link, dev, "rx pause %s, tx pause %s\n",
176                           flowctrl & FLOW_CTRL_RX ? "enabled" : "disabled",
177                           flowctrl & FLOW_CTRL_TX ? "enabled" : "disabled");
178         }
179
180         mtk_w32(mac->hw, mcr, MTK_MAC_MCR(mac->id));
181
182         if (mac->phy_dev->link)
183                 netif_carrier_on(dev);
184         else
185                 netif_carrier_off(dev);
186 }
187
188 static int mtk_phy_connect_node(struct mtk_eth *eth, struct mtk_mac *mac,
189                                 struct device_node *phy_node)
190 {
191         const __be32 *_addr = NULL;
192         struct phy_device *phydev;
193         int phy_mode, addr;
194
195         _addr = of_get_property(phy_node, "reg", NULL);
196
197         if (!_addr || (be32_to_cpu(*_addr) >= 0x20)) {
198                 pr_err("%s: invalid phy address\n", phy_node->name);
199                 return -EINVAL;
200         }
201         addr = be32_to_cpu(*_addr);
202         phy_mode = of_get_phy_mode(phy_node);
203         if (phy_mode < 0) {
204                 dev_err(eth->dev, "incorrect phy-mode %d\n", phy_mode);
205                 return -EINVAL;
206         }
207
208         phydev = of_phy_connect(eth->netdev[mac->id], phy_node,
209                                 mtk_phy_link_adjust, 0, phy_mode);
210         if (!phydev) {
211                 dev_err(eth->dev, "could not connect to PHY\n");
212                 return -ENODEV;
213         }
214
215         dev_info(eth->dev,
216                  "connected mac %d to PHY at %s [uid=%08x, driver=%s]\n",
217                  mac->id, phydev_name(phydev), phydev->phy_id,
218                  phydev->drv->name);
219
220         mac->phy_dev = phydev;
221
222         return 0;
223 }
224
225 static int mtk_phy_connect(struct mtk_mac *mac)
226 {
227         struct mtk_eth *eth = mac->hw;
228         struct device_node *np;
229         u32 val, ge_mode;
230
231         np = of_parse_phandle(mac->of_node, "phy-handle", 0);
232         if (!np && of_phy_is_fixed_link(mac->of_node))
233                 if (!of_phy_register_fixed_link(mac->of_node))
234                         np = of_node_get(mac->of_node);
235         if (!np)
236                 return -ENODEV;
237
238         switch (of_get_phy_mode(np)) {
239         case PHY_INTERFACE_MODE_RGMII_TXID:
240         case PHY_INTERFACE_MODE_RGMII_RXID:
241         case PHY_INTERFACE_MODE_RGMII_ID:
242         case PHY_INTERFACE_MODE_RGMII:
243                 ge_mode = 0;
244                 break;
245         case PHY_INTERFACE_MODE_MII:
246                 ge_mode = 1;
247                 break;
248         case PHY_INTERFACE_MODE_RMII:
249                 ge_mode = 2;
250                 break;
251         default:
252                 dev_err(eth->dev, "invalid phy_mode\n");
253                 return -1;
254         }
255
256         /* put the gmac into the right mode */
257         regmap_read(eth->ethsys, ETHSYS_SYSCFG0, &val);
258         val &= ~SYSCFG0_GE_MODE(SYSCFG0_GE_MASK, mac->id);
259         val |= SYSCFG0_GE_MODE(ge_mode, mac->id);
260         regmap_write(eth->ethsys, ETHSYS_SYSCFG0, val);
261
262         mtk_phy_connect_node(eth, mac, np);
263         mac->phy_dev->autoneg = AUTONEG_ENABLE;
264         mac->phy_dev->speed = 0;
265         mac->phy_dev->duplex = 0;
266         mac->phy_dev->supported &= PHY_GBIT_FEATURES | SUPPORTED_Pause |
267                                    SUPPORTED_Asym_Pause;
268         mac->phy_dev->advertising = mac->phy_dev->supported |
269                                     ADVERTISED_Autoneg;
270         phy_start_aneg(mac->phy_dev);
271
272         of_node_put(np);
273
274         return 0;
275 }
276
277 static int mtk_mdio_init(struct mtk_eth *eth)
278 {
279         struct device_node *mii_np;
280         int err;
281
282         mii_np = of_get_child_by_name(eth->dev->of_node, "mdio-bus");
283         if (!mii_np) {
284                 dev_err(eth->dev, "no %s child node found", "mdio-bus");
285                 return -ENODEV;
286         }
287
288         if (!of_device_is_available(mii_np)) {
289                 err = 0;
290                 goto err_put_node;
291         }
292
293         eth->mii_bus = mdiobus_alloc();
294         if (!eth->mii_bus) {
295                 err = -ENOMEM;
296                 goto err_put_node;
297         }
298
299         eth->mii_bus->name = "mdio";
300         eth->mii_bus->read = mtk_mdio_read;
301         eth->mii_bus->write = mtk_mdio_write;
302         eth->mii_bus->priv = eth;
303         eth->mii_bus->parent = eth->dev;
304
305         snprintf(eth->mii_bus->id, MII_BUS_ID_SIZE, "%s", mii_np->name);
306         err = of_mdiobus_register(eth->mii_bus, mii_np);
307         if (err)
308                 goto err_free_bus;
309
310         return 0;
311
312 err_free_bus:
313         mdiobus_free(eth->mii_bus);
314
315 err_put_node:
316         of_node_put(mii_np);
317         eth->mii_bus = NULL;
318         return err;
319 }
320
321 static void mtk_mdio_cleanup(struct mtk_eth *eth)
322 {
323         if (!eth->mii_bus)
324                 return;
325
326         mdiobus_unregister(eth->mii_bus);
327         of_node_put(eth->mii_bus->dev.of_node);
328         mdiobus_free(eth->mii_bus);
329 }
330
331 static inline void mtk_irq_disable(struct mtk_eth *eth, u32 mask)
332 {
333         unsigned long flags;
334         u32 val;
335
336         spin_lock_irqsave(&eth->irq_lock, flags);
337         val = mtk_r32(eth, MTK_QDMA_INT_MASK);
338         mtk_w32(eth, val & ~mask, MTK_QDMA_INT_MASK);
339         spin_unlock_irqrestore(&eth->irq_lock, flags);
340 }
341
342 static inline void mtk_irq_enable(struct mtk_eth *eth, u32 mask)
343 {
344         unsigned long flags;
345         u32 val;
346
347         spin_lock_irqsave(&eth->irq_lock, flags);
348         val = mtk_r32(eth, MTK_QDMA_INT_MASK);
349         mtk_w32(eth, val | mask, MTK_QDMA_INT_MASK);
350         spin_unlock_irqrestore(&eth->irq_lock, flags);
351 }
352
353 static int mtk_set_mac_address(struct net_device *dev, void *p)
354 {
355         int ret = eth_mac_addr(dev, p);
356         struct mtk_mac *mac = netdev_priv(dev);
357         const char *macaddr = dev->dev_addr;
358         unsigned long flags;
359
360         if (ret)
361                 return ret;
362
363         spin_lock_irqsave(&mac->hw->page_lock, flags);
364         mtk_w32(mac->hw, (macaddr[0] << 8) | macaddr[1],
365                 MTK_GDMA_MAC_ADRH(mac->id));
366         mtk_w32(mac->hw, (macaddr[2] << 24) | (macaddr[3] << 16) |
367                 (macaddr[4] << 8) | macaddr[5],
368                 MTK_GDMA_MAC_ADRL(mac->id));
369         spin_unlock_irqrestore(&mac->hw->page_lock, flags);
370
371         return 0;
372 }
373
374 void mtk_stats_update_mac(struct mtk_mac *mac)
375 {
376         struct mtk_hw_stats *hw_stats = mac->hw_stats;
377         unsigned int base = MTK_GDM1_TX_GBCNT;
378         u64 stats;
379
380         base += hw_stats->reg_offset;
381
382         u64_stats_update_begin(&hw_stats->syncp);
383
384         hw_stats->rx_bytes += mtk_r32(mac->hw, base);
385         stats =  mtk_r32(mac->hw, base + 0x04);
386         if (stats)
387                 hw_stats->rx_bytes += (stats << 32);
388         hw_stats->rx_packets += mtk_r32(mac->hw, base + 0x08);
389         hw_stats->rx_overflow += mtk_r32(mac->hw, base + 0x10);
390         hw_stats->rx_fcs_errors += mtk_r32(mac->hw, base + 0x14);
391         hw_stats->rx_short_errors += mtk_r32(mac->hw, base + 0x18);
392         hw_stats->rx_long_errors += mtk_r32(mac->hw, base + 0x1c);
393         hw_stats->rx_checksum_errors += mtk_r32(mac->hw, base + 0x20);
394         hw_stats->rx_flow_control_packets +=
395                                         mtk_r32(mac->hw, base + 0x24);
396         hw_stats->tx_skip += mtk_r32(mac->hw, base + 0x28);
397         hw_stats->tx_collisions += mtk_r32(mac->hw, base + 0x2c);
398         hw_stats->tx_bytes += mtk_r32(mac->hw, base + 0x30);
399         stats =  mtk_r32(mac->hw, base + 0x34);
400         if (stats)
401                 hw_stats->tx_bytes += (stats << 32);
402         hw_stats->tx_packets += mtk_r32(mac->hw, base + 0x38);
403         u64_stats_update_end(&hw_stats->syncp);
404 }
405
406 static void mtk_stats_update(struct mtk_eth *eth)
407 {
408         int i;
409
410         for (i = 0; i < MTK_MAC_COUNT; i++) {
411                 if (!eth->mac[i] || !eth->mac[i]->hw_stats)
412                         continue;
413                 if (spin_trylock(&eth->mac[i]->hw_stats->stats_lock)) {
414                         mtk_stats_update_mac(eth->mac[i]);
415                         spin_unlock(&eth->mac[i]->hw_stats->stats_lock);
416                 }
417         }
418 }
419
420 static struct rtnl_link_stats64 *mtk_get_stats64(struct net_device *dev,
421                                         struct rtnl_link_stats64 *storage)
422 {
423         struct mtk_mac *mac = netdev_priv(dev);
424         struct mtk_hw_stats *hw_stats = mac->hw_stats;
425         unsigned int start;
426
427         if (netif_running(dev) && netif_device_present(dev)) {
428                 if (spin_trylock(&hw_stats->stats_lock)) {
429                         mtk_stats_update_mac(mac);
430                         spin_unlock(&hw_stats->stats_lock);
431                 }
432         }
433
434         do {
435                 start = u64_stats_fetch_begin_irq(&hw_stats->syncp);
436                 storage->rx_packets = hw_stats->rx_packets;
437                 storage->tx_packets = hw_stats->tx_packets;
438                 storage->rx_bytes = hw_stats->rx_bytes;
439                 storage->tx_bytes = hw_stats->tx_bytes;
440                 storage->collisions = hw_stats->tx_collisions;
441                 storage->rx_length_errors = hw_stats->rx_short_errors +
442                         hw_stats->rx_long_errors;
443                 storage->rx_over_errors = hw_stats->rx_overflow;
444                 storage->rx_crc_errors = hw_stats->rx_fcs_errors;
445                 storage->rx_errors = hw_stats->rx_checksum_errors;
446                 storage->tx_aborted_errors = hw_stats->tx_skip;
447         } while (u64_stats_fetch_retry_irq(&hw_stats->syncp, start));
448
449         storage->tx_errors = dev->stats.tx_errors;
450         storage->rx_dropped = dev->stats.rx_dropped;
451         storage->tx_dropped = dev->stats.tx_dropped;
452
453         return storage;
454 }
455
456 static inline int mtk_max_frag_size(int mtu)
457 {
458         /* make sure buf_size will be at least MTK_MAX_RX_LENGTH */
459         if (mtu + MTK_RX_ETH_HLEN < MTK_MAX_RX_LENGTH)
460                 mtu = MTK_MAX_RX_LENGTH - MTK_RX_ETH_HLEN;
461
462         return SKB_DATA_ALIGN(MTK_RX_HLEN + mtu) +
463                 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
464 }
465
466 static inline int mtk_max_buf_size(int frag_size)
467 {
468         int buf_size = frag_size - NET_SKB_PAD - NET_IP_ALIGN -
469                        SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
470
471         WARN_ON(buf_size < MTK_MAX_RX_LENGTH);
472
473         return buf_size;
474 }
475
476 static inline void mtk_rx_get_desc(struct mtk_rx_dma *rxd,
477                                    struct mtk_rx_dma *dma_rxd)
478 {
479         rxd->rxd1 = READ_ONCE(dma_rxd->rxd1);
480         rxd->rxd2 = READ_ONCE(dma_rxd->rxd2);
481         rxd->rxd3 = READ_ONCE(dma_rxd->rxd3);
482         rxd->rxd4 = READ_ONCE(dma_rxd->rxd4);
483 }
484
485 /* the qdma core needs scratch memory to be setup */
486 static int mtk_init_fq_dma(struct mtk_eth *eth)
487 {
488         dma_addr_t phy_ring_tail;
489         int cnt = MTK_DMA_SIZE;
490         dma_addr_t dma_addr;
491         int i;
492
493         eth->scratch_ring = dma_alloc_coherent(eth->dev,
494                                                cnt * sizeof(struct mtk_tx_dma),
495                                                &eth->phy_scratch_ring,
496                                                GFP_ATOMIC | __GFP_ZERO);
497         if (unlikely(!eth->scratch_ring))
498                 return -ENOMEM;
499
500         eth->scratch_head = kcalloc(cnt, MTK_QDMA_PAGE_SIZE,
501                                     GFP_KERNEL);
502         if (unlikely(!eth->scratch_head))
503                 return -ENOMEM;
504
505         dma_addr = dma_map_single(eth->dev,
506                                   eth->scratch_head, cnt * MTK_QDMA_PAGE_SIZE,
507                                   DMA_FROM_DEVICE);
508         if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
509                 return -ENOMEM;
510
511         memset(eth->scratch_ring, 0x0, sizeof(struct mtk_tx_dma) * cnt);
512         phy_ring_tail = eth->phy_scratch_ring +
513                         (sizeof(struct mtk_tx_dma) * (cnt - 1));
514
515         for (i = 0; i < cnt; i++) {
516                 eth->scratch_ring[i].txd1 =
517                                         (dma_addr + (i * MTK_QDMA_PAGE_SIZE));
518                 if (i < cnt - 1)
519                         eth->scratch_ring[i].txd2 = (eth->phy_scratch_ring +
520                                 ((i + 1) * sizeof(struct mtk_tx_dma)));
521                 eth->scratch_ring[i].txd3 = TX_DMA_SDL(MTK_QDMA_PAGE_SIZE);
522         }
523
524         mtk_w32(eth, eth->phy_scratch_ring, MTK_QDMA_FQ_HEAD);
525         mtk_w32(eth, phy_ring_tail, MTK_QDMA_FQ_TAIL);
526         mtk_w32(eth, (cnt << 16) | cnt, MTK_QDMA_FQ_CNT);
527         mtk_w32(eth, MTK_QDMA_PAGE_SIZE << 16, MTK_QDMA_FQ_BLEN);
528
529         return 0;
530 }
531
532 static inline void *mtk_qdma_phys_to_virt(struct mtk_tx_ring *ring, u32 desc)
533 {
534         void *ret = ring->dma;
535
536         return ret + (desc - ring->phys);
537 }
538
539 static inline struct mtk_tx_buf *mtk_desc_to_tx_buf(struct mtk_tx_ring *ring,
540                                                     struct mtk_tx_dma *txd)
541 {
542         int idx = txd - ring->dma;
543
544         return &ring->buf[idx];
545 }
546
547 static void mtk_tx_unmap(struct device *dev, struct mtk_tx_buf *tx_buf)
548 {
549         if (tx_buf->flags & MTK_TX_FLAGS_SINGLE0) {
550                 dma_unmap_single(dev,
551                                  dma_unmap_addr(tx_buf, dma_addr0),
552                                  dma_unmap_len(tx_buf, dma_len0),
553                                  DMA_TO_DEVICE);
554         } else if (tx_buf->flags & MTK_TX_FLAGS_PAGE0) {
555                 dma_unmap_page(dev,
556                                dma_unmap_addr(tx_buf, dma_addr0),
557                                dma_unmap_len(tx_buf, dma_len0),
558                                DMA_TO_DEVICE);
559         }
560         tx_buf->flags = 0;
561         if (tx_buf->skb &&
562             (tx_buf->skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC))
563                 dev_kfree_skb_any(tx_buf->skb);
564         tx_buf->skb = NULL;
565 }
566
567 static int mtk_tx_map(struct sk_buff *skb, struct net_device *dev,
568                       int tx_num, struct mtk_tx_ring *ring, bool gso)
569 {
570         struct mtk_mac *mac = netdev_priv(dev);
571         struct mtk_eth *eth = mac->hw;
572         struct mtk_tx_dma *itxd, *txd;
573         struct mtk_tx_buf *tx_buf;
574         dma_addr_t mapped_addr;
575         unsigned int nr_frags;
576         int i, n_desc = 1;
577         u32 txd4 = 0;
578
579         itxd = ring->next_free;
580         if (itxd == ring->last_free)
581                 return -ENOMEM;
582
583         /* set the forward port */
584         txd4 |= (mac->id + 1) << TX_DMA_FPORT_SHIFT;
585
586         tx_buf = mtk_desc_to_tx_buf(ring, itxd);
587         memset(tx_buf, 0, sizeof(*tx_buf));
588
589         if (gso)
590                 txd4 |= TX_DMA_TSO;
591
592         /* TX Checksum offload */
593         if (skb->ip_summed == CHECKSUM_PARTIAL)
594                 txd4 |= TX_DMA_CHKSUM;
595
596         /* VLAN header offload */
597         if (skb_vlan_tag_present(skb))
598                 txd4 |= TX_DMA_INS_VLAN | skb_vlan_tag_get(skb);
599
600         mapped_addr = dma_map_single(&dev->dev, skb->data,
601                                      skb_headlen(skb), DMA_TO_DEVICE);
602         if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
603                 return -ENOMEM;
604
605         WRITE_ONCE(itxd->txd1, mapped_addr);
606         tx_buf->flags |= MTK_TX_FLAGS_SINGLE0;
607         dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
608         dma_unmap_len_set(tx_buf, dma_len0, skb_headlen(skb));
609
610         /* TX SG offload */
611         txd = itxd;
612         nr_frags = skb_shinfo(skb)->nr_frags;
613         for (i = 0; i < nr_frags; i++) {
614                 struct skb_frag_struct *frag = &skb_shinfo(skb)->frags[i];
615                 unsigned int offset = 0;
616                 int frag_size = skb_frag_size(frag);
617
618                 while (frag_size) {
619                         bool last_frag = false;
620                         unsigned int frag_map_size;
621
622                         txd = mtk_qdma_phys_to_virt(ring, txd->txd2);
623                         if (txd == ring->last_free)
624                                 goto err_dma;
625
626                         n_desc++;
627                         frag_map_size = min(frag_size, MTK_TX_DMA_BUF_LEN);
628                         mapped_addr = skb_frag_dma_map(&dev->dev, frag, offset,
629                                                        frag_map_size,
630                                                        DMA_TO_DEVICE);
631                         if (unlikely(dma_mapping_error(&dev->dev, mapped_addr)))
632                                 goto err_dma;
633
634                         if (i == nr_frags - 1 &&
635                             (frag_size - frag_map_size) == 0)
636                                 last_frag = true;
637
638                         WRITE_ONCE(txd->txd1, mapped_addr);
639                         WRITE_ONCE(txd->txd3, (TX_DMA_SWC |
640                                                TX_DMA_PLEN0(frag_map_size) |
641                                                last_frag * TX_DMA_LS0));
642                         WRITE_ONCE(txd->txd4, 0);
643
644                         tx_buf->skb = (struct sk_buff *)MTK_DMA_DUMMY_DESC;
645                         tx_buf = mtk_desc_to_tx_buf(ring, txd);
646                         memset(tx_buf, 0, sizeof(*tx_buf));
647
648                         tx_buf->flags |= MTK_TX_FLAGS_PAGE0;
649                         dma_unmap_addr_set(tx_buf, dma_addr0, mapped_addr);
650                         dma_unmap_len_set(tx_buf, dma_len0, frag_map_size);
651                         frag_size -= frag_map_size;
652                         offset += frag_map_size;
653                 }
654         }
655
656         /* store skb to cleanup */
657         tx_buf->skb = skb;
658
659         WRITE_ONCE(itxd->txd4, txd4);
660         WRITE_ONCE(itxd->txd3, (TX_DMA_SWC | TX_DMA_PLEN0(skb_headlen(skb)) |
661                                 (!nr_frags * TX_DMA_LS0)));
662
663         netdev_sent_queue(dev, skb->len);
664         skb_tx_timestamp(skb);
665
666         ring->next_free = mtk_qdma_phys_to_virt(ring, txd->txd2);
667         atomic_sub(n_desc, &ring->free_count);
668
669         /* make sure that all changes to the dma ring are flushed before we
670          * continue
671          */
672         wmb();
673
674         if (netif_xmit_stopped(netdev_get_tx_queue(dev, 0)) || !skb->xmit_more)
675                 mtk_w32(eth, txd->txd2, MTK_QTX_CTX_PTR);
676
677         return 0;
678
679 err_dma:
680         do {
681                 tx_buf = mtk_desc_to_tx_buf(ring, itxd);
682
683                 /* unmap dma */
684                 mtk_tx_unmap(&dev->dev, tx_buf);
685
686                 itxd->txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
687                 itxd = mtk_qdma_phys_to_virt(ring, itxd->txd2);
688         } while (itxd != txd);
689
690         return -ENOMEM;
691 }
692
693 static inline int mtk_cal_txd_req(struct sk_buff *skb)
694 {
695         int i, nfrags;
696         struct skb_frag_struct *frag;
697
698         nfrags = 1;
699         if (skb_is_gso(skb)) {
700                 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
701                         frag = &skb_shinfo(skb)->frags[i];
702                         nfrags += DIV_ROUND_UP(frag->size, MTK_TX_DMA_BUF_LEN);
703                 }
704         } else {
705                 nfrags += skb_shinfo(skb)->nr_frags;
706         }
707
708         return nfrags;
709 }
710
711 static int mtk_queue_stopped(struct mtk_eth *eth)
712 {
713         int i;
714
715         for (i = 0; i < MTK_MAC_COUNT; i++) {
716                 if (!eth->netdev[i])
717                         continue;
718                 if (netif_queue_stopped(eth->netdev[i]))
719                         return 1;
720         }
721
722         return 0;
723 }
724
725 static void mtk_wake_queue(struct mtk_eth *eth)
726 {
727         int i;
728
729         for (i = 0; i < MTK_MAC_COUNT; i++) {
730                 if (!eth->netdev[i])
731                         continue;
732                 netif_wake_queue(eth->netdev[i]);
733         }
734 }
735
736 static void mtk_stop_queue(struct mtk_eth *eth)
737 {
738         int i;
739
740         for (i = 0; i < MTK_MAC_COUNT; i++) {
741                 if (!eth->netdev[i])
742                         continue;
743                 netif_stop_queue(eth->netdev[i]);
744         }
745 }
746
747 static int mtk_start_xmit(struct sk_buff *skb, struct net_device *dev)
748 {
749         struct mtk_mac *mac = netdev_priv(dev);
750         struct mtk_eth *eth = mac->hw;
751         struct mtk_tx_ring *ring = &eth->tx_ring;
752         struct net_device_stats *stats = &dev->stats;
753         unsigned long flags;
754         bool gso = false;
755         int tx_num;
756
757         /* normally we can rely on the stack not calling this more than once,
758          * however we have 2 queues running on the same ring so we need to lock
759          * the ring access
760          */
761         spin_lock_irqsave(&eth->page_lock, flags);
762
763         tx_num = mtk_cal_txd_req(skb);
764         if (unlikely(atomic_read(&ring->free_count) <= tx_num)) {
765                 mtk_stop_queue(eth);
766                 netif_err(eth, tx_queued, dev,
767                           "Tx Ring full when queue awake!\n");
768                 spin_unlock_irqrestore(&eth->page_lock, flags);
769                 return NETDEV_TX_BUSY;
770         }
771
772         /* TSO: fill MSS info in tcp checksum field */
773         if (skb_is_gso(skb)) {
774                 if (skb_cow_head(skb, 0)) {
775                         netif_warn(eth, tx_err, dev,
776                                    "GSO expand head fail.\n");
777                         goto drop;
778                 }
779
780                 if (skb_shinfo(skb)->gso_type &
781                                 (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)) {
782                         gso = true;
783                         tcp_hdr(skb)->check = htons(skb_shinfo(skb)->gso_size);
784                 }
785         }
786
787         if (mtk_tx_map(skb, dev, tx_num, ring, gso) < 0)
788                 goto drop;
789
790         if (unlikely(atomic_read(&ring->free_count) <= ring->thresh))
791                 mtk_stop_queue(eth);
792
793         spin_unlock_irqrestore(&eth->page_lock, flags);
794
795         return NETDEV_TX_OK;
796
797 drop:
798         spin_unlock_irqrestore(&eth->page_lock, flags);
799         stats->tx_dropped++;
800         dev_kfree_skb(skb);
801         return NETDEV_TX_OK;
802 }
803
804 static int mtk_poll_rx(struct napi_struct *napi, int budget,
805                        struct mtk_eth *eth)
806 {
807         struct mtk_rx_ring *ring = &eth->rx_ring;
808         int idx = ring->calc_idx;
809         struct sk_buff *skb;
810         u8 *data, *new_data;
811         struct mtk_rx_dma *rxd, trxd;
812         int done = 0;
813
814         while (done < budget) {
815                 struct net_device *netdev;
816                 unsigned int pktlen;
817                 dma_addr_t dma_addr;
818                 int mac = 0;
819
820                 idx = NEXT_RX_DESP_IDX(idx);
821                 rxd = &ring->dma[idx];
822                 data = ring->data[idx];
823
824                 mtk_rx_get_desc(&trxd, rxd);
825                 if (!(trxd.rxd2 & RX_DMA_DONE))
826                         break;
827
828                 /* find out which mac the packet come from. values start at 1 */
829                 mac = (trxd.rxd4 >> RX_DMA_FPORT_SHIFT) &
830                       RX_DMA_FPORT_MASK;
831                 mac--;
832
833                 netdev = eth->netdev[mac];
834
835                 /* alloc new buffer */
836                 new_data = napi_alloc_frag(ring->frag_size);
837                 if (unlikely(!new_data)) {
838                         netdev->stats.rx_dropped++;
839                         goto release_desc;
840                 }
841                 dma_addr = dma_map_single(&eth->netdev[mac]->dev,
842                                           new_data + NET_SKB_PAD,
843                                           ring->buf_size,
844                                           DMA_FROM_DEVICE);
845                 if (unlikely(dma_mapping_error(&netdev->dev, dma_addr))) {
846                         skb_free_frag(new_data);
847                         netdev->stats.rx_dropped++;
848                         goto release_desc;
849                 }
850
851                 /* receive data */
852                 skb = build_skb(data, ring->frag_size);
853                 if (unlikely(!skb)) {
854                         put_page(virt_to_head_page(new_data));
855                         netdev->stats.rx_dropped++;
856                         goto release_desc;
857                 }
858                 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN);
859
860                 dma_unmap_single(&netdev->dev, trxd.rxd1,
861                                  ring->buf_size, DMA_FROM_DEVICE);
862                 pktlen = RX_DMA_GET_PLEN0(trxd.rxd2);
863                 skb->dev = netdev;
864                 skb_put(skb, pktlen);
865                 if (trxd.rxd4 & RX_DMA_L4_VALID)
866                         skb->ip_summed = CHECKSUM_UNNECESSARY;
867                 else
868                         skb_checksum_none_assert(skb);
869                 skb->protocol = eth_type_trans(skb, netdev);
870
871                 if (netdev->features & NETIF_F_HW_VLAN_CTAG_RX &&
872                     RX_DMA_VID(trxd.rxd3))
873                         __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
874                                                RX_DMA_VID(trxd.rxd3));
875                 napi_gro_receive(napi, skb);
876
877                 ring->data[idx] = new_data;
878                 rxd->rxd1 = (unsigned int)dma_addr;
879
880 release_desc:
881                 rxd->rxd2 = RX_DMA_PLEN0(ring->buf_size);
882
883                 ring->calc_idx = idx;
884                 /* make sure that all changes to the dma ring are flushed before
885                  * we continue
886                  */
887                 wmb();
888                 mtk_w32(eth, ring->calc_idx, MTK_QRX_CRX_IDX0);
889                 done++;
890         }
891
892         if (done < budget)
893                 mtk_w32(eth, MTK_RX_DONE_INT, MTK_QMTK_INT_STATUS);
894
895         return done;
896 }
897
898 static int mtk_poll_tx(struct mtk_eth *eth, int budget)
899 {
900         struct mtk_tx_ring *ring = &eth->tx_ring;
901         struct mtk_tx_dma *desc;
902         struct sk_buff *skb;
903         struct mtk_tx_buf *tx_buf;
904         unsigned int done[MTK_MAX_DEVS];
905         unsigned int bytes[MTK_MAX_DEVS];
906         u32 cpu, dma;
907         static int condition;
908         int total = 0, i;
909
910         memset(done, 0, sizeof(done));
911         memset(bytes, 0, sizeof(bytes));
912
913         cpu = mtk_r32(eth, MTK_QTX_CRX_PTR);
914         dma = mtk_r32(eth, MTK_QTX_DRX_PTR);
915
916         desc = mtk_qdma_phys_to_virt(ring, cpu);
917
918         while ((cpu != dma) && budget) {
919                 u32 next_cpu = desc->txd2;
920                 int mac;
921
922                 desc = mtk_qdma_phys_to_virt(ring, desc->txd2);
923                 if ((desc->txd3 & TX_DMA_OWNER_CPU) == 0)
924                         break;
925
926                 mac = (desc->txd4 >> TX_DMA_FPORT_SHIFT) &
927                        TX_DMA_FPORT_MASK;
928                 mac--;
929
930                 tx_buf = mtk_desc_to_tx_buf(ring, desc);
931                 skb = tx_buf->skb;
932                 if (!skb) {
933                         condition = 1;
934                         break;
935                 }
936
937                 if (skb != (struct sk_buff *)MTK_DMA_DUMMY_DESC) {
938                         bytes[mac] += skb->len;
939                         done[mac]++;
940                         budget--;
941                 }
942                 mtk_tx_unmap(eth->dev, tx_buf);
943
944                 ring->last_free = desc;
945                 atomic_inc(&ring->free_count);
946
947                 cpu = next_cpu;
948         }
949
950         mtk_w32(eth, cpu, MTK_QTX_CRX_PTR);
951
952         for (i = 0; i < MTK_MAC_COUNT; i++) {
953                 if (!eth->netdev[i] || !done[i])
954                         continue;
955                 netdev_completed_queue(eth->netdev[i], done[i], bytes[i]);
956                 total += done[i];
957         }
958
959         if (mtk_queue_stopped(eth) &&
960             (atomic_read(&ring->free_count) > ring->thresh))
961                 mtk_wake_queue(eth);
962
963         return total;
964 }
965
966 static void mtk_handle_status_irq(struct mtk_eth *eth)
967 {
968         u32 status2 = mtk_r32(eth, MTK_INT_STATUS2);
969
970         if (unlikely(status2 & (MTK_GDM1_AF | MTK_GDM2_AF))) {
971                 mtk_stats_update(eth);
972                 mtk_w32(eth, (MTK_GDM1_AF | MTK_GDM2_AF),
973                         MTK_INT_STATUS2);
974         }
975 }
976
977 static int mtk_napi_tx(struct napi_struct *napi, int budget)
978 {
979         struct mtk_eth *eth = container_of(napi, struct mtk_eth, tx_napi);
980         u32 status, mask;
981         int tx_done = 0;
982
983         mtk_handle_status_irq(eth);
984         mtk_w32(eth, MTK_TX_DONE_INT, MTK_QMTK_INT_STATUS);
985         tx_done = mtk_poll_tx(eth, budget);
986
987         if (unlikely(netif_msg_intr(eth))) {
988                 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
989                 mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
990                 dev_info(eth->dev,
991                          "done tx %d, intr 0x%08x/0x%x\n",
992                          tx_done, status, mask);
993         }
994
995         if (tx_done == budget)
996                 return budget;
997
998         status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
999         if (status & MTK_TX_DONE_INT)
1000                 return budget;
1001
1002         napi_complete(napi);
1003         mtk_irq_enable(eth, MTK_TX_DONE_INT);
1004
1005         return tx_done;
1006 }
1007
1008 static int mtk_napi_rx(struct napi_struct *napi, int budget)
1009 {
1010         struct mtk_eth *eth = container_of(napi, struct mtk_eth, rx_napi);
1011         u32 status, mask;
1012         int rx_done = 0;
1013
1014         mtk_handle_status_irq(eth);
1015         mtk_w32(eth, MTK_RX_DONE_INT, MTK_QMTK_INT_STATUS);
1016         rx_done = mtk_poll_rx(napi, budget, eth);
1017
1018         if (unlikely(netif_msg_intr(eth))) {
1019                 status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
1020                 mask = mtk_r32(eth, MTK_QDMA_INT_MASK);
1021                 dev_info(eth->dev,
1022                          "done rx %d, intr 0x%08x/0x%x\n",
1023                          rx_done, status, mask);
1024         }
1025
1026         if (rx_done == budget)
1027                 return budget;
1028
1029         status = mtk_r32(eth, MTK_QMTK_INT_STATUS);
1030         if (status & MTK_RX_DONE_INT)
1031                 return budget;
1032
1033         napi_complete(napi);
1034         mtk_irq_enable(eth, MTK_RX_DONE_INT);
1035
1036         return rx_done;
1037 }
1038
1039 static int mtk_tx_alloc(struct mtk_eth *eth)
1040 {
1041         struct mtk_tx_ring *ring = &eth->tx_ring;
1042         int i, sz = sizeof(*ring->dma);
1043
1044         ring->buf = kcalloc(MTK_DMA_SIZE, sizeof(*ring->buf),
1045                                GFP_KERNEL);
1046         if (!ring->buf)
1047                 goto no_tx_mem;
1048
1049         ring->dma = dma_alloc_coherent(eth->dev,
1050                                           MTK_DMA_SIZE * sz,
1051                                           &ring->phys,
1052                                           GFP_ATOMIC | __GFP_ZERO);
1053         if (!ring->dma)
1054                 goto no_tx_mem;
1055
1056         memset(ring->dma, 0, MTK_DMA_SIZE * sz);
1057         for (i = 0; i < MTK_DMA_SIZE; i++) {
1058                 int next = (i + 1) % MTK_DMA_SIZE;
1059                 u32 next_ptr = ring->phys + next * sz;
1060
1061                 ring->dma[i].txd2 = next_ptr;
1062                 ring->dma[i].txd3 = TX_DMA_LS0 | TX_DMA_OWNER_CPU;
1063         }
1064
1065         atomic_set(&ring->free_count, MTK_DMA_SIZE - 2);
1066         ring->next_free = &ring->dma[0];
1067         ring->last_free = &ring->dma[MTK_DMA_SIZE - 1];
1068         ring->thresh = MAX_SKB_FRAGS;
1069
1070         /* make sure that all changes to the dma ring are flushed before we
1071          * continue
1072          */
1073         wmb();
1074
1075         mtk_w32(eth, ring->phys, MTK_QTX_CTX_PTR);
1076         mtk_w32(eth, ring->phys, MTK_QTX_DTX_PTR);
1077         mtk_w32(eth,
1078                 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1079                 MTK_QTX_CRX_PTR);
1080         mtk_w32(eth,
1081                 ring->phys + ((MTK_DMA_SIZE - 1) * sz),
1082                 MTK_QTX_DRX_PTR);
1083
1084         return 0;
1085
1086 no_tx_mem:
1087         return -ENOMEM;
1088 }
1089
1090 static void mtk_tx_clean(struct mtk_eth *eth)
1091 {
1092         struct mtk_tx_ring *ring = &eth->tx_ring;
1093         int i;
1094
1095         if (ring->buf) {
1096                 for (i = 0; i < MTK_DMA_SIZE; i++)
1097                         mtk_tx_unmap(eth->dev, &ring->buf[i]);
1098                 kfree(ring->buf);
1099                 ring->buf = NULL;
1100         }
1101
1102         if (ring->dma) {
1103                 dma_free_coherent(eth->dev,
1104                                   MTK_DMA_SIZE * sizeof(*ring->dma),
1105                                   ring->dma,
1106                                   ring->phys);
1107                 ring->dma = NULL;
1108         }
1109 }
1110
1111 static int mtk_rx_alloc(struct mtk_eth *eth)
1112 {
1113         struct mtk_rx_ring *ring = &eth->rx_ring;
1114         int i;
1115
1116         ring->frag_size = mtk_max_frag_size(ETH_DATA_LEN);
1117         ring->buf_size = mtk_max_buf_size(ring->frag_size);
1118         ring->data = kcalloc(MTK_DMA_SIZE, sizeof(*ring->data),
1119                              GFP_KERNEL);
1120         if (!ring->data)
1121                 return -ENOMEM;
1122
1123         for (i = 0; i < MTK_DMA_SIZE; i++) {
1124                 ring->data[i] = netdev_alloc_frag(ring->frag_size);
1125                 if (!ring->data[i])
1126                         return -ENOMEM;
1127         }
1128
1129         ring->dma = dma_alloc_coherent(eth->dev,
1130                                        MTK_DMA_SIZE * sizeof(*ring->dma),
1131                                        &ring->phys,
1132                                        GFP_ATOMIC | __GFP_ZERO);
1133         if (!ring->dma)
1134                 return -ENOMEM;
1135
1136         for (i = 0; i < MTK_DMA_SIZE; i++) {
1137                 dma_addr_t dma_addr = dma_map_single(eth->dev,
1138                                 ring->data[i] + NET_SKB_PAD,
1139                                 ring->buf_size,
1140                                 DMA_FROM_DEVICE);
1141                 if (unlikely(dma_mapping_error(eth->dev, dma_addr)))
1142                         return -ENOMEM;
1143                 ring->dma[i].rxd1 = (unsigned int)dma_addr;
1144
1145                 ring->dma[i].rxd2 = RX_DMA_PLEN0(ring->buf_size);
1146         }
1147         ring->calc_idx = MTK_DMA_SIZE - 1;
1148         /* make sure that all changes to the dma ring are flushed before we
1149          * continue
1150          */
1151         wmb();
1152
1153         mtk_w32(eth, eth->rx_ring.phys, MTK_QRX_BASE_PTR0);
1154         mtk_w32(eth, MTK_DMA_SIZE, MTK_QRX_MAX_CNT0);
1155         mtk_w32(eth, eth->rx_ring.calc_idx, MTK_QRX_CRX_IDX0);
1156         mtk_w32(eth, MTK_PST_DRX_IDX0, MTK_QDMA_RST_IDX);
1157         mtk_w32(eth, (QDMA_RES_THRES << 8) | QDMA_RES_THRES, MTK_QTX_CFG(0));
1158
1159         return 0;
1160 }
1161
1162 static void mtk_rx_clean(struct mtk_eth *eth)
1163 {
1164         struct mtk_rx_ring *ring = &eth->rx_ring;
1165         int i;
1166
1167         if (ring->data && ring->dma) {
1168                 for (i = 0; i < MTK_DMA_SIZE; i++) {
1169                         if (!ring->data[i])
1170                                 continue;
1171                         if (!ring->dma[i].rxd1)
1172                                 continue;
1173                         dma_unmap_single(eth->dev,
1174                                          ring->dma[i].rxd1,
1175                                          ring->buf_size,
1176                                          DMA_FROM_DEVICE);
1177                         skb_free_frag(ring->data[i]);
1178                 }
1179                 kfree(ring->data);
1180                 ring->data = NULL;
1181         }
1182
1183         if (ring->dma) {
1184                 dma_free_coherent(eth->dev,
1185                                   MTK_DMA_SIZE * sizeof(*ring->dma),
1186                                   ring->dma,
1187                                   ring->phys);
1188                 ring->dma = NULL;
1189         }
1190 }
1191
1192 /* wait for DMA to finish whatever it is doing before we start using it again */
1193 static int mtk_dma_busy_wait(struct mtk_eth *eth)
1194 {
1195         unsigned long t_start = jiffies;
1196
1197         while (1) {
1198                 if (!(mtk_r32(eth, MTK_QDMA_GLO_CFG) &
1199                       (MTK_RX_DMA_BUSY | MTK_TX_DMA_BUSY)))
1200                         return 0;
1201                 if (time_after(jiffies, t_start + MTK_DMA_BUSY_TIMEOUT))
1202                         break;
1203         }
1204
1205         dev_err(eth->dev, "DMA init timeout\n");
1206         return -1;
1207 }
1208
1209 static int mtk_dma_init(struct mtk_eth *eth)
1210 {
1211         int err;
1212
1213         if (mtk_dma_busy_wait(eth))
1214                 return -EBUSY;
1215
1216         /* QDMA needs scratch memory for internal reordering of the
1217          * descriptors
1218          */
1219         err = mtk_init_fq_dma(eth);
1220         if (err)
1221                 return err;
1222
1223         err = mtk_tx_alloc(eth);
1224         if (err)
1225                 return err;
1226
1227         err = mtk_rx_alloc(eth);
1228         if (err)
1229                 return err;
1230
1231         /* Enable random early drop and set drop threshold automatically */
1232         mtk_w32(eth, FC_THRES_DROP_MODE | FC_THRES_DROP_EN | FC_THRES_MIN,
1233                 MTK_QDMA_FC_THRES);
1234         mtk_w32(eth, 0x0, MTK_QDMA_HRED2);
1235
1236         return 0;
1237 }
1238
1239 static void mtk_dma_free(struct mtk_eth *eth)
1240 {
1241         int i;
1242
1243         for (i = 0; i < MTK_MAC_COUNT; i++)
1244                 if (eth->netdev[i])
1245                         netdev_reset_queue(eth->netdev[i]);
1246         if (eth->scratch_ring) {
1247                 dma_free_coherent(eth->dev,
1248                                   MTK_DMA_SIZE * sizeof(struct mtk_tx_dma),
1249                                   eth->scratch_ring,
1250                                   eth->phy_scratch_ring);
1251                 eth->scratch_ring = NULL;
1252                 eth->phy_scratch_ring = 0;
1253         }
1254         mtk_tx_clean(eth);
1255         mtk_rx_clean(eth);
1256         kfree(eth->scratch_head);
1257 }
1258
1259 static void mtk_tx_timeout(struct net_device *dev)
1260 {
1261         struct mtk_mac *mac = netdev_priv(dev);
1262         struct mtk_eth *eth = mac->hw;
1263
1264         eth->netdev[mac->id]->stats.tx_errors++;
1265         netif_err(eth, tx_err, dev,
1266                   "transmit timed out\n");
1267         schedule_work(&eth->pending_work);
1268 }
1269
1270 static irqreturn_t mtk_handle_irq_rx(int irq, void *_eth)
1271 {
1272         struct mtk_eth *eth = _eth;
1273
1274         if (likely(napi_schedule_prep(&eth->rx_napi))) {
1275                 __napi_schedule(&eth->rx_napi);
1276                 mtk_irq_disable(eth, MTK_RX_DONE_INT);
1277         }
1278
1279         return IRQ_HANDLED;
1280 }
1281
1282 static irqreturn_t mtk_handle_irq_tx(int irq, void *_eth)
1283 {
1284         struct mtk_eth *eth = _eth;
1285
1286         if (likely(napi_schedule_prep(&eth->tx_napi))) {
1287                 __napi_schedule(&eth->tx_napi);
1288                 mtk_irq_disable(eth, MTK_TX_DONE_INT);
1289         }
1290
1291         return IRQ_HANDLED;
1292 }
1293
1294 #ifdef CONFIG_NET_POLL_CONTROLLER
1295 static void mtk_poll_controller(struct net_device *dev)
1296 {
1297         struct mtk_mac *mac = netdev_priv(dev);
1298         struct mtk_eth *eth = mac->hw;
1299         u32 int_mask = MTK_TX_DONE_INT | MTK_RX_DONE_INT;
1300
1301         mtk_irq_disable(eth, int_mask);
1302         mtk_handle_irq_rx(eth->irq[2], dev);
1303         mtk_irq_enable(eth, int_mask);
1304 }
1305 #endif
1306
1307 static int mtk_start_dma(struct mtk_eth *eth)
1308 {
1309         int err;
1310
1311         err = mtk_dma_init(eth);
1312         if (err) {
1313                 mtk_dma_free(eth);
1314                 return err;
1315         }
1316
1317         mtk_w32(eth,
1318                 MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN |
1319                 MTK_RX_2B_OFFSET | MTK_DMA_SIZE_16DWORDS |
1320                 MTK_RX_BT_32DWORDS | MTK_NDP_CO_PRO,
1321                 MTK_QDMA_GLO_CFG);
1322
1323         return 0;
1324 }
1325
1326 static int mtk_open(struct net_device *dev)
1327 {
1328         struct mtk_mac *mac = netdev_priv(dev);
1329         struct mtk_eth *eth = mac->hw;
1330
1331         /* we run 2 netdevs on the same dma ring so we only bring it up once */
1332         if (!atomic_read(&eth->dma_refcnt)) {
1333                 int err = mtk_start_dma(eth);
1334
1335                 if (err)
1336                         return err;
1337
1338                 napi_enable(&eth->tx_napi);
1339                 napi_enable(&eth->rx_napi);
1340                 mtk_irq_enable(eth, MTK_TX_DONE_INT | MTK_RX_DONE_INT);
1341         }
1342         atomic_inc(&eth->dma_refcnt);
1343
1344         phy_start(mac->phy_dev);
1345         netif_start_queue(dev);
1346
1347         return 0;
1348 }
1349
1350 static void mtk_stop_dma(struct mtk_eth *eth, u32 glo_cfg)
1351 {
1352         unsigned long flags;
1353         u32 val;
1354         int i;
1355
1356         /* stop the dma engine */
1357         spin_lock_irqsave(&eth->page_lock, flags);
1358         val = mtk_r32(eth, glo_cfg);
1359         mtk_w32(eth, val & ~(MTK_TX_WB_DDONE | MTK_RX_DMA_EN | MTK_TX_DMA_EN),
1360                 glo_cfg);
1361         spin_unlock_irqrestore(&eth->page_lock, flags);
1362
1363         /* wait for dma stop */
1364         for (i = 0; i < 10; i++) {
1365                 val = mtk_r32(eth, glo_cfg);
1366                 if (val & (MTK_TX_DMA_BUSY | MTK_RX_DMA_BUSY)) {
1367                         msleep(20);
1368                         continue;
1369                 }
1370                 break;
1371         }
1372 }
1373
1374 static int mtk_stop(struct net_device *dev)
1375 {
1376         struct mtk_mac *mac = netdev_priv(dev);
1377         struct mtk_eth *eth = mac->hw;
1378
1379         netif_tx_disable(dev);
1380         phy_stop(mac->phy_dev);
1381
1382         /* only shutdown DMA if this is the last user */
1383         if (!atomic_dec_and_test(&eth->dma_refcnt))
1384                 return 0;
1385
1386         mtk_irq_disable(eth, MTK_TX_DONE_INT | MTK_RX_DONE_INT);
1387         napi_disable(&eth->tx_napi);
1388         napi_disable(&eth->rx_napi);
1389
1390         mtk_stop_dma(eth, MTK_QDMA_GLO_CFG);
1391
1392         mtk_dma_free(eth);
1393
1394         return 0;
1395 }
1396
1397 static int __init mtk_hw_init(struct mtk_eth *eth)
1398 {
1399         int err, i;
1400
1401         /* reset the frame engine */
1402         reset_control_assert(eth->rstc);
1403         usleep_range(10, 20);
1404         reset_control_deassert(eth->rstc);
1405         usleep_range(10, 20);
1406
1407         /* Set GE2 driving and slew rate */
1408         regmap_write(eth->pctl, GPIO_DRV_SEL10, 0xa00);
1409
1410         /* set GE2 TDSEL */
1411         regmap_write(eth->pctl, GPIO_OD33_CTRL8, 0x5);
1412
1413         /* set GE2 TUNE */
1414         regmap_write(eth->pctl, GPIO_BIAS_CTRL, 0x0);
1415
1416         /* GE1, Force 1000M/FD, FC ON */
1417         mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(0));
1418
1419         /* GE2, Force 1000M/FD, FC ON */
1420         mtk_w32(eth, MAC_MCR_FIXED_LINK, MTK_MAC_MCR(1));
1421
1422         /* Enable RX VLan Offloading */
1423         mtk_w32(eth, 1, MTK_CDMP_EG_CTRL);
1424
1425         err = devm_request_irq(eth->dev, eth->irq[1], mtk_handle_irq_tx, 0,
1426                                dev_name(eth->dev), eth);
1427         if (err)
1428                 return err;
1429         err = devm_request_irq(eth->dev, eth->irq[2], mtk_handle_irq_rx, 0,
1430                                dev_name(eth->dev), eth);
1431         if (err)
1432                 return err;
1433
1434         err = mtk_mdio_init(eth);
1435         if (err)
1436                 return err;
1437
1438         /* disable delay and normal interrupt */
1439         mtk_w32(eth, 0, MTK_QDMA_DELAY_INT);
1440         mtk_irq_disable(eth, ~0);
1441         mtk_w32(eth, RST_GL_PSE, MTK_RST_GL);
1442         mtk_w32(eth, 0, MTK_RST_GL);
1443
1444         /* FE int grouping */
1445         mtk_w32(eth, MTK_TX_DONE_INT, MTK_PDMA_INT_GRP1);
1446         mtk_w32(eth, MTK_RX_DONE_INT, MTK_PDMA_INT_GRP2);
1447         mtk_w32(eth, MTK_TX_DONE_INT, MTK_QDMA_INT_GRP1);
1448         mtk_w32(eth, MTK_RX_DONE_INT, MTK_QDMA_INT_GRP2);
1449         mtk_w32(eth, 0x21021000, MTK_FE_INT_GRP);
1450
1451         for (i = 0; i < 2; i++) {
1452                 u32 val = mtk_r32(eth, MTK_GDMA_FWD_CFG(i));
1453
1454                 /* setup the forward port to send frame to QDMA */
1455                 val &= ~0xffff;
1456                 val |= 0x5555;
1457
1458                 /* Enable RX checksum */
1459                 val |= MTK_GDMA_ICS_EN | MTK_GDMA_TCS_EN | MTK_GDMA_UCS_EN;
1460
1461                 /* setup the mac dma */
1462                 mtk_w32(eth, val, MTK_GDMA_FWD_CFG(i));
1463         }
1464
1465         return 0;
1466 }
1467
1468 static int __init mtk_init(struct net_device *dev)
1469 {
1470         struct mtk_mac *mac = netdev_priv(dev);
1471         struct mtk_eth *eth = mac->hw;
1472         const char *mac_addr;
1473
1474         mac_addr = of_get_mac_address(mac->of_node);
1475         if (mac_addr)
1476                 ether_addr_copy(dev->dev_addr, mac_addr);
1477
1478         /* If the mac address is invalid, use random mac address  */
1479         if (!is_valid_ether_addr(dev->dev_addr)) {
1480                 random_ether_addr(dev->dev_addr);
1481                 dev_err(eth->dev, "generated random MAC address %pM\n",
1482                         dev->dev_addr);
1483                 dev->addr_assign_type = NET_ADDR_RANDOM;
1484         }
1485
1486         return mtk_phy_connect(mac);
1487 }
1488
1489 static void mtk_uninit(struct net_device *dev)
1490 {
1491         struct mtk_mac *mac = netdev_priv(dev);
1492         struct mtk_eth *eth = mac->hw;
1493
1494         phy_disconnect(mac->phy_dev);
1495         mtk_mdio_cleanup(eth);
1496         mtk_irq_disable(eth, ~0);
1497         free_irq(eth->irq[1], dev);
1498         free_irq(eth->irq[2], dev);
1499 }
1500
1501 static int mtk_do_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
1502 {
1503         struct mtk_mac *mac = netdev_priv(dev);
1504
1505         switch (cmd) {
1506         case SIOCGMIIPHY:
1507         case SIOCGMIIREG:
1508         case SIOCSMIIREG:
1509                 return phy_mii_ioctl(mac->phy_dev, ifr, cmd);
1510         default:
1511                 break;
1512         }
1513
1514         return -EOPNOTSUPP;
1515 }
1516
1517 static void mtk_pending_work(struct work_struct *work)
1518 {
1519         struct mtk_eth *eth = container_of(work, struct mtk_eth, pending_work);
1520         int err, i;
1521         unsigned long restart = 0;
1522
1523         rtnl_lock();
1524
1525         /* stop all devices to make sure that dma is properly shut down */
1526         for (i = 0; i < MTK_MAC_COUNT; i++) {
1527                 if (!eth->netdev[i])
1528                         continue;
1529                 mtk_stop(eth->netdev[i]);
1530                 __set_bit(i, &restart);
1531         }
1532
1533         /* restart DMA and enable IRQs */
1534         for (i = 0; i < MTK_MAC_COUNT; i++) {
1535                 if (!test_bit(i, &restart))
1536                         continue;
1537                 err = mtk_open(eth->netdev[i]);
1538                 if (err) {
1539                         netif_alert(eth, ifup, eth->netdev[i],
1540                               "Driver up/down cycle failed, closing device.\n");
1541                         dev_close(eth->netdev[i]);
1542                 }
1543         }
1544         rtnl_unlock();
1545 }
1546
1547 static int mtk_cleanup(struct mtk_eth *eth)
1548 {
1549         int i;
1550
1551         for (i = 0; i < MTK_MAC_COUNT; i++) {
1552                 if (!eth->netdev[i])
1553                         continue;
1554
1555                 unregister_netdev(eth->netdev[i]);
1556                 free_netdev(eth->netdev[i]);
1557         }
1558         cancel_work_sync(&eth->pending_work);
1559
1560         return 0;
1561 }
1562
1563 static int mtk_get_settings(struct net_device *dev,
1564                             struct ethtool_cmd *cmd)
1565 {
1566         struct mtk_mac *mac = netdev_priv(dev);
1567         int err;
1568
1569         err = phy_read_status(mac->phy_dev);
1570         if (err)
1571                 return -ENODEV;
1572
1573         return phy_ethtool_gset(mac->phy_dev, cmd);
1574 }
1575
1576 static int mtk_set_settings(struct net_device *dev,
1577                             struct ethtool_cmd *cmd)
1578 {
1579         struct mtk_mac *mac = netdev_priv(dev);
1580
1581         if (cmd->phy_address != mac->phy_dev->mdio.addr) {
1582                 mac->phy_dev = mdiobus_get_phy(mac->hw->mii_bus,
1583                                                cmd->phy_address);
1584                 if (!mac->phy_dev)
1585                         return -ENODEV;
1586         }
1587
1588         return phy_ethtool_sset(mac->phy_dev, cmd);
1589 }
1590
1591 static void mtk_get_drvinfo(struct net_device *dev,
1592                             struct ethtool_drvinfo *info)
1593 {
1594         struct mtk_mac *mac = netdev_priv(dev);
1595
1596         strlcpy(info->driver, mac->hw->dev->driver->name, sizeof(info->driver));
1597         strlcpy(info->bus_info, dev_name(mac->hw->dev), sizeof(info->bus_info));
1598         info->n_stats = ARRAY_SIZE(mtk_ethtool_stats);
1599 }
1600
1601 static u32 mtk_get_msglevel(struct net_device *dev)
1602 {
1603         struct mtk_mac *mac = netdev_priv(dev);
1604
1605         return mac->hw->msg_enable;
1606 }
1607
1608 static void mtk_set_msglevel(struct net_device *dev, u32 value)
1609 {
1610         struct mtk_mac *mac = netdev_priv(dev);
1611
1612         mac->hw->msg_enable = value;
1613 }
1614
1615 static int mtk_nway_reset(struct net_device *dev)
1616 {
1617         struct mtk_mac *mac = netdev_priv(dev);
1618
1619         return genphy_restart_aneg(mac->phy_dev);
1620 }
1621
1622 static u32 mtk_get_link(struct net_device *dev)
1623 {
1624         struct mtk_mac *mac = netdev_priv(dev);
1625         int err;
1626
1627         err = genphy_update_link(mac->phy_dev);
1628         if (err)
1629                 return ethtool_op_get_link(dev);
1630
1631         return mac->phy_dev->link;
1632 }
1633
1634 static void mtk_get_strings(struct net_device *dev, u32 stringset, u8 *data)
1635 {
1636         int i;
1637
1638         switch (stringset) {
1639         case ETH_SS_STATS:
1640                 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++) {
1641                         memcpy(data, mtk_ethtool_stats[i].str, ETH_GSTRING_LEN);
1642                         data += ETH_GSTRING_LEN;
1643                 }
1644                 break;
1645         }
1646 }
1647
1648 static int mtk_get_sset_count(struct net_device *dev, int sset)
1649 {
1650         switch (sset) {
1651         case ETH_SS_STATS:
1652                 return ARRAY_SIZE(mtk_ethtool_stats);
1653         default:
1654                 return -EOPNOTSUPP;
1655         }
1656 }
1657
1658 static void mtk_get_ethtool_stats(struct net_device *dev,
1659                                   struct ethtool_stats *stats, u64 *data)
1660 {
1661         struct mtk_mac *mac = netdev_priv(dev);
1662         struct mtk_hw_stats *hwstats = mac->hw_stats;
1663         u64 *data_src, *data_dst;
1664         unsigned int start;
1665         int i;
1666
1667         if (netif_running(dev) && netif_device_present(dev)) {
1668                 if (spin_trylock(&hwstats->stats_lock)) {
1669                         mtk_stats_update_mac(mac);
1670                         spin_unlock(&hwstats->stats_lock);
1671                 }
1672         }
1673
1674         do {
1675                 data_src = (u64*)hwstats;
1676                 data_dst = data;
1677                 start = u64_stats_fetch_begin_irq(&hwstats->syncp);
1678
1679                 for (i = 0; i < ARRAY_SIZE(mtk_ethtool_stats); i++)
1680                         *data_dst++ = *(data_src + mtk_ethtool_stats[i].offset);
1681         } while (u64_stats_fetch_retry_irq(&hwstats->syncp, start));
1682 }
1683
1684 static struct ethtool_ops mtk_ethtool_ops = {
1685         .get_settings           = mtk_get_settings,
1686         .set_settings           = mtk_set_settings,
1687         .get_drvinfo            = mtk_get_drvinfo,
1688         .get_msglevel           = mtk_get_msglevel,
1689         .set_msglevel           = mtk_set_msglevel,
1690         .nway_reset             = mtk_nway_reset,
1691         .get_link               = mtk_get_link,
1692         .get_strings            = mtk_get_strings,
1693         .get_sset_count         = mtk_get_sset_count,
1694         .get_ethtool_stats      = mtk_get_ethtool_stats,
1695 };
1696
1697 static const struct net_device_ops mtk_netdev_ops = {
1698         .ndo_init               = mtk_init,
1699         .ndo_uninit             = mtk_uninit,
1700         .ndo_open               = mtk_open,
1701         .ndo_stop               = mtk_stop,
1702         .ndo_start_xmit         = mtk_start_xmit,
1703         .ndo_set_mac_address    = mtk_set_mac_address,
1704         .ndo_validate_addr      = eth_validate_addr,
1705         .ndo_do_ioctl           = mtk_do_ioctl,
1706         .ndo_change_mtu         = eth_change_mtu,
1707         .ndo_tx_timeout         = mtk_tx_timeout,
1708         .ndo_get_stats64        = mtk_get_stats64,
1709 #ifdef CONFIG_NET_POLL_CONTROLLER
1710         .ndo_poll_controller    = mtk_poll_controller,
1711 #endif
1712 };
1713
1714 static int mtk_add_mac(struct mtk_eth *eth, struct device_node *np)
1715 {
1716         struct mtk_mac *mac;
1717         const __be32 *_id = of_get_property(np, "reg", NULL);
1718         int id, err;
1719
1720         if (!_id) {
1721                 dev_err(eth->dev, "missing mac id\n");
1722                 return -EINVAL;
1723         }
1724
1725         id = be32_to_cpup(_id);
1726         if (id >= MTK_MAC_COUNT) {
1727                 dev_err(eth->dev, "%d is not a valid mac id\n", id);
1728                 return -EINVAL;
1729         }
1730
1731         if (eth->netdev[id]) {
1732                 dev_err(eth->dev, "duplicate mac id found: %d\n", id);
1733                 return -EINVAL;
1734         }
1735
1736         eth->netdev[id] = alloc_etherdev(sizeof(*mac));
1737         if (!eth->netdev[id]) {
1738                 dev_err(eth->dev, "alloc_etherdev failed\n");
1739                 return -ENOMEM;
1740         }
1741         mac = netdev_priv(eth->netdev[id]);
1742         eth->mac[id] = mac;
1743         mac->id = id;
1744         mac->hw = eth;
1745         mac->of_node = np;
1746
1747         mac->hw_stats = devm_kzalloc(eth->dev,
1748                                      sizeof(*mac->hw_stats),
1749                                      GFP_KERNEL);
1750         if (!mac->hw_stats) {
1751                 dev_err(eth->dev, "failed to allocate counter memory\n");
1752                 err = -ENOMEM;
1753                 goto free_netdev;
1754         }
1755         spin_lock_init(&mac->hw_stats->stats_lock);
1756         u64_stats_init(&mac->hw_stats->syncp);
1757         mac->hw_stats->reg_offset = id * MTK_STAT_OFFSET;
1758
1759         SET_NETDEV_DEV(eth->netdev[id], eth->dev);
1760         eth->netdev[id]->watchdog_timeo = 5 * HZ;
1761         eth->netdev[id]->netdev_ops = &mtk_netdev_ops;
1762         eth->netdev[id]->base_addr = (unsigned long)eth->base;
1763         eth->netdev[id]->vlan_features = MTK_HW_FEATURES &
1764                 ~(NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_CTAG_RX);
1765         eth->netdev[id]->features |= MTK_HW_FEATURES;
1766         eth->netdev[id]->ethtool_ops = &mtk_ethtool_ops;
1767
1768         err = register_netdev(eth->netdev[id]);
1769         if (err) {
1770                 dev_err(eth->dev, "error bringing up device\n");
1771                 goto free_netdev;
1772         }
1773         eth->netdev[id]->irq = eth->irq[0];
1774         netif_info(eth, probe, eth->netdev[id],
1775                    "mediatek frame engine at 0x%08lx, irq %d\n",
1776                    eth->netdev[id]->base_addr, eth->irq[0]);
1777
1778         return 0;
1779
1780 free_netdev:
1781         free_netdev(eth->netdev[id]);
1782         return err;
1783 }
1784
1785 static int mtk_probe(struct platform_device *pdev)
1786 {
1787         struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1788         struct device_node *mac_np;
1789         const struct of_device_id *match;
1790         struct mtk_soc_data *soc;
1791         struct mtk_eth *eth;
1792         int err;
1793         int i;
1794
1795         match = of_match_device(of_mtk_match, &pdev->dev);
1796         soc = (struct mtk_soc_data *)match->data;
1797
1798         eth = devm_kzalloc(&pdev->dev, sizeof(*eth), GFP_KERNEL);
1799         if (!eth)
1800                 return -ENOMEM;
1801
1802         eth->base = devm_ioremap_resource(&pdev->dev, res);
1803         if (IS_ERR(eth->base))
1804                 return PTR_ERR(eth->base);
1805
1806         spin_lock_init(&eth->page_lock);
1807         spin_lock_init(&eth->irq_lock);
1808
1809         eth->ethsys = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1810                                                       "mediatek,ethsys");
1811         if (IS_ERR(eth->ethsys)) {
1812                 dev_err(&pdev->dev, "no ethsys regmap found\n");
1813                 return PTR_ERR(eth->ethsys);
1814         }
1815
1816         eth->pctl = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
1817                                                     "mediatek,pctl");
1818         if (IS_ERR(eth->pctl)) {
1819                 dev_err(&pdev->dev, "no pctl regmap found\n");
1820                 return PTR_ERR(eth->pctl);
1821         }
1822
1823         eth->rstc = devm_reset_control_get(&pdev->dev, "eth");
1824         if (IS_ERR(eth->rstc)) {
1825                 dev_err(&pdev->dev, "no eth reset found\n");
1826                 return PTR_ERR(eth->rstc);
1827         }
1828
1829         for (i = 0; i < 3; i++) {
1830                 eth->irq[i] = platform_get_irq(pdev, i);
1831                 if (eth->irq[i] < 0) {
1832                         dev_err(&pdev->dev, "no IRQ%d resource found\n", i);
1833                         return -ENXIO;
1834                 }
1835         }
1836
1837         eth->clk_ethif = devm_clk_get(&pdev->dev, "ethif");
1838         eth->clk_esw = devm_clk_get(&pdev->dev, "esw");
1839         eth->clk_gp1 = devm_clk_get(&pdev->dev, "gp1");
1840         eth->clk_gp2 = devm_clk_get(&pdev->dev, "gp2");
1841         if (IS_ERR(eth->clk_esw) || IS_ERR(eth->clk_gp1) ||
1842             IS_ERR(eth->clk_gp2) || IS_ERR(eth->clk_ethif))
1843                 return -ENODEV;
1844
1845         clk_prepare_enable(eth->clk_ethif);
1846         clk_prepare_enable(eth->clk_esw);
1847         clk_prepare_enable(eth->clk_gp1);
1848         clk_prepare_enable(eth->clk_gp2);
1849
1850         eth->dev = &pdev->dev;
1851         eth->msg_enable = netif_msg_init(mtk_msg_level, MTK_DEFAULT_MSG_ENABLE);
1852         INIT_WORK(&eth->pending_work, mtk_pending_work);
1853
1854         err = mtk_hw_init(eth);
1855         if (err)
1856                 return err;
1857
1858         for_each_child_of_node(pdev->dev.of_node, mac_np) {
1859                 if (!of_device_is_compatible(mac_np,
1860                                              "mediatek,eth-mac"))
1861                         continue;
1862
1863                 if (!of_device_is_available(mac_np))
1864                         continue;
1865
1866                 err = mtk_add_mac(eth, mac_np);
1867                 if (err)
1868                         goto err_free_dev;
1869         }
1870
1871         /* we run 2 devices on the same DMA ring so we need a dummy device
1872          * for NAPI to work
1873          */
1874         init_dummy_netdev(&eth->dummy_dev);
1875         netif_napi_add(&eth->dummy_dev, &eth->tx_napi, mtk_napi_tx,
1876                        MTK_NAPI_WEIGHT);
1877         netif_napi_add(&eth->dummy_dev, &eth->rx_napi, mtk_napi_rx,
1878                        MTK_NAPI_WEIGHT);
1879
1880         platform_set_drvdata(pdev, eth);
1881
1882         return 0;
1883
1884 err_free_dev:
1885         mtk_cleanup(eth);
1886         return err;
1887 }
1888
1889 static int mtk_remove(struct platform_device *pdev)
1890 {
1891         struct mtk_eth *eth = platform_get_drvdata(pdev);
1892
1893         clk_disable_unprepare(eth->clk_ethif);
1894         clk_disable_unprepare(eth->clk_esw);
1895         clk_disable_unprepare(eth->clk_gp1);
1896         clk_disable_unprepare(eth->clk_gp2);
1897
1898         netif_napi_del(&eth->tx_napi);
1899         netif_napi_del(&eth->rx_napi);
1900         mtk_cleanup(eth);
1901         platform_set_drvdata(pdev, NULL);
1902
1903         return 0;
1904 }
1905
1906 const struct of_device_id of_mtk_match[] = {
1907         { .compatible = "mediatek,mt7623-eth" },
1908         {},
1909 };
1910
1911 static struct platform_driver mtk_driver = {
1912         .probe = mtk_probe,
1913         .remove = mtk_remove,
1914         .driver = {
1915                 .name = "mtk_soc_eth",
1916                 .of_match_table = of_mtk_match,
1917         },
1918 };
1919
1920 module_platform_driver(mtk_driver);
1921
1922 MODULE_LICENSE("GPL");
1923 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
1924 MODULE_DESCRIPTION("Ethernet driver for MediaTek SoC");