GNU Linux-libre 4.9.314-gnu1
[releases.git] / drivers / net / ethernet / cadence / macb.c
1 /*
2  * Cadence MACB/GEM Ethernet Controller driver
3  *
4  * Copyright (C) 2004-2006 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/circ_buf.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/io.h>
21 #include <linux/gpio.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/interrupt.h>
24 #include <linux/netdevice.h>
25 #include <linux/etherdevice.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/platform_data/macb.h>
28 #include <linux/platform_device.h>
29 #include <linux/phy.h>
30 #include <linux/of.h>
31 #include <linux/of_device.h>
32 #include <linux/of_gpio.h>
33 #include <linux/of_mdio.h>
34 #include <linux/of_net.h>
35
36 #include "macb.h"
37
38 #define MACB_RX_BUFFER_SIZE     128
39 #define RX_BUFFER_MULTIPLE      64  /* bytes */
40 #define RX_RING_SIZE            512 /* must be power of 2 */
41 #define RX_RING_BYTES           (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
42
43 #define TX_RING_SIZE            128 /* must be power of 2 */
44 #define TX_RING_BYTES           (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
45
46 /* level of occupied TX descriptors under which we wake up TX process */
47 #define MACB_TX_WAKEUP_THRESH   (3 * TX_RING_SIZE / 4)
48
49 #define MACB_RX_INT_FLAGS       (MACB_BIT(RCOMP) | MACB_BIT(RXUBR)      \
50                                  | MACB_BIT(ISR_ROVR))
51 #define MACB_TX_ERR_FLAGS       (MACB_BIT(ISR_TUND)                     \
52                                         | MACB_BIT(ISR_RLE)             \
53                                         | MACB_BIT(TXERR))
54 #define MACB_TX_INT_FLAGS       (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
55
56 #define MACB_MAX_TX_LEN         ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
57 #define GEM_MAX_TX_LEN          ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
58
59 #define GEM_MTU_MIN_SIZE        68
60
61 #define MACB_WOL_HAS_MAGIC_PACKET       (0x1 << 0)
62 #define MACB_WOL_ENABLED                (0x1 << 1)
63
64 /* Graceful stop timeouts in us. We should allow up to
65  * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
66  */
67 #define MACB_HALT_TIMEOUT       1230
68
69 /* Ring buffer accessors */
70 static unsigned int macb_tx_ring_wrap(unsigned int index)
71 {
72         return index & (TX_RING_SIZE - 1);
73 }
74
75 static struct macb_dma_desc *macb_tx_desc(struct macb_queue *queue,
76                                           unsigned int index)
77 {
78         return &queue->tx_ring[macb_tx_ring_wrap(index)];
79 }
80
81 static struct macb_tx_skb *macb_tx_skb(struct macb_queue *queue,
82                                        unsigned int index)
83 {
84         return &queue->tx_skb[macb_tx_ring_wrap(index)];
85 }
86
87 static dma_addr_t macb_tx_dma(struct macb_queue *queue, unsigned int index)
88 {
89         dma_addr_t offset;
90
91         offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
92
93         return queue->tx_ring_dma + offset;
94 }
95
96 static unsigned int macb_rx_ring_wrap(unsigned int index)
97 {
98         return index & (RX_RING_SIZE - 1);
99 }
100
101 static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
102 {
103         return &bp->rx_ring[macb_rx_ring_wrap(index)];
104 }
105
106 static void *macb_rx_buffer(struct macb *bp, unsigned int index)
107 {
108         return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
109 }
110
111 /* I/O accessors */
112 static u32 hw_readl_native(struct macb *bp, int offset)
113 {
114         return __raw_readl(bp->regs + offset);
115 }
116
117 static void hw_writel_native(struct macb *bp, int offset, u32 value)
118 {
119         __raw_writel(value, bp->regs + offset);
120 }
121
122 static u32 hw_readl(struct macb *bp, int offset)
123 {
124         return readl_relaxed(bp->regs + offset);
125 }
126
127 static void hw_writel(struct macb *bp, int offset, u32 value)
128 {
129         writel_relaxed(value, bp->regs + offset);
130 }
131
132 /* Find the CPU endianness by using the loopback bit of NCR register. When the
133  * CPU is in big endian we need to program swapped mode for management
134  * descriptor access.
135  */
136 static bool hw_is_native_io(void __iomem *addr)
137 {
138         u32 value = MACB_BIT(LLB);
139
140         __raw_writel(value, addr + MACB_NCR);
141         value = __raw_readl(addr + MACB_NCR);
142
143         /* Write 0 back to disable everything */
144         __raw_writel(0, addr + MACB_NCR);
145
146         return value == MACB_BIT(LLB);
147 }
148
149 static bool hw_is_gem(void __iomem *addr, bool native_io)
150 {
151         u32 id;
152
153         if (native_io)
154                 id = __raw_readl(addr + MACB_MID);
155         else
156                 id = readl_relaxed(addr + MACB_MID);
157
158         return MACB_BFEXT(IDNUM, id) >= 0x2;
159 }
160
161 static void macb_set_hwaddr(struct macb *bp)
162 {
163         u32 bottom;
164         u16 top;
165
166         bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
167         macb_or_gem_writel(bp, SA1B, bottom);
168         top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
169         macb_or_gem_writel(bp, SA1T, top);
170
171         /* Clear unused address register sets */
172         macb_or_gem_writel(bp, SA2B, 0);
173         macb_or_gem_writel(bp, SA2T, 0);
174         macb_or_gem_writel(bp, SA3B, 0);
175         macb_or_gem_writel(bp, SA3T, 0);
176         macb_or_gem_writel(bp, SA4B, 0);
177         macb_or_gem_writel(bp, SA4T, 0);
178 }
179
180 static void macb_get_hwaddr(struct macb *bp)
181 {
182         struct macb_platform_data *pdata;
183         u32 bottom;
184         u16 top;
185         u8 addr[6];
186         int i;
187
188         pdata = dev_get_platdata(&bp->pdev->dev);
189
190         /* Check all 4 address register for valid address */
191         for (i = 0; i < 4; i++) {
192                 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
193                 top = macb_or_gem_readl(bp, SA1T + i * 8);
194
195                 if (pdata && pdata->rev_eth_addr) {
196                         addr[5] = bottom & 0xff;
197                         addr[4] = (bottom >> 8) & 0xff;
198                         addr[3] = (bottom >> 16) & 0xff;
199                         addr[2] = (bottom >> 24) & 0xff;
200                         addr[1] = top & 0xff;
201                         addr[0] = (top & 0xff00) >> 8;
202                 } else {
203                         addr[0] = bottom & 0xff;
204                         addr[1] = (bottom >> 8) & 0xff;
205                         addr[2] = (bottom >> 16) & 0xff;
206                         addr[3] = (bottom >> 24) & 0xff;
207                         addr[4] = top & 0xff;
208                         addr[5] = (top >> 8) & 0xff;
209                 }
210
211                 if (is_valid_ether_addr(addr)) {
212                         memcpy(bp->dev->dev_addr, addr, sizeof(addr));
213                         return;
214                 }
215         }
216
217         dev_info(&bp->pdev->dev, "invalid hw address, using random\n");
218         eth_hw_addr_random(bp->dev);
219 }
220
221 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
222 {
223         struct macb *bp = bus->priv;
224         int value;
225
226         macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
227                               | MACB_BF(RW, MACB_MAN_READ)
228                               | MACB_BF(PHYA, mii_id)
229                               | MACB_BF(REGA, regnum)
230                               | MACB_BF(CODE, MACB_MAN_CODE)));
231
232         /* wait for end of transfer */
233         while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
234                 cpu_relax();
235
236         value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
237
238         return value;
239 }
240
241 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
242                            u16 value)
243 {
244         struct macb *bp = bus->priv;
245
246         macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
247                               | MACB_BF(RW, MACB_MAN_WRITE)
248                               | MACB_BF(PHYA, mii_id)
249                               | MACB_BF(REGA, regnum)
250                               | MACB_BF(CODE, MACB_MAN_CODE)
251                               | MACB_BF(DATA, value)));
252
253         /* wait for end of transfer */
254         while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
255                 cpu_relax();
256
257         return 0;
258 }
259
260 /**
261  * macb_set_tx_clk() - Set a clock to a new frequency
262  * @clk         Pointer to the clock to change
263  * @rate        New frequency in Hz
264  * @dev         Pointer to the struct net_device
265  */
266 static void macb_set_tx_clk(struct clk *clk, int speed, struct net_device *dev)
267 {
268         long ferr, rate, rate_rounded;
269
270         if (!clk)
271                 return;
272
273         switch (speed) {
274         case SPEED_10:
275                 rate = 2500000;
276                 break;
277         case SPEED_100:
278                 rate = 25000000;
279                 break;
280         case SPEED_1000:
281                 rate = 125000000;
282                 break;
283         default:
284                 return;
285         }
286
287         rate_rounded = clk_round_rate(clk, rate);
288         if (rate_rounded < 0)
289                 return;
290
291         /* RGMII allows 50 ppm frequency error. Test and warn if this limit
292          * is not satisfied.
293          */
294         ferr = abs(rate_rounded - rate);
295         ferr = DIV_ROUND_UP(ferr, rate / 100000);
296         if (ferr > 5)
297                 netdev_warn(dev, "unable to generate target frequency: %ld Hz\n",
298                             rate);
299
300         if (clk_set_rate(clk, rate_rounded))
301                 netdev_err(dev, "adjusting tx_clk failed.\n");
302 }
303
304 static void macb_handle_link_change(struct net_device *dev)
305 {
306         struct macb *bp = netdev_priv(dev);
307         struct phy_device *phydev = dev->phydev;
308         unsigned long flags;
309         int status_change = 0;
310
311         spin_lock_irqsave(&bp->lock, flags);
312
313         if (phydev->link) {
314                 if ((bp->speed != phydev->speed) ||
315                     (bp->duplex != phydev->duplex)) {
316                         u32 reg;
317
318                         reg = macb_readl(bp, NCFGR);
319                         reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
320                         if (macb_is_gem(bp))
321                                 reg &= ~GEM_BIT(GBE);
322
323                         if (phydev->duplex)
324                                 reg |= MACB_BIT(FD);
325                         if (phydev->speed == SPEED_100)
326                                 reg |= MACB_BIT(SPD);
327                         if (phydev->speed == SPEED_1000 &&
328                             bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
329                                 reg |= GEM_BIT(GBE);
330
331                         macb_or_gem_writel(bp, NCFGR, reg);
332
333                         bp->speed = phydev->speed;
334                         bp->duplex = phydev->duplex;
335                         status_change = 1;
336                 }
337         }
338
339         if (phydev->link != bp->link) {
340                 if (!phydev->link) {
341                         bp->speed = 0;
342                         bp->duplex = -1;
343                 }
344                 bp->link = phydev->link;
345
346                 status_change = 1;
347         }
348
349         spin_unlock_irqrestore(&bp->lock, flags);
350
351         if (status_change) {
352                 if (phydev->link) {
353                         /* Update the TX clock rate if and only if the link is
354                          * up and there has been a link change.
355                          */
356                         macb_set_tx_clk(bp->tx_clk, phydev->speed, dev);
357
358                         netif_carrier_on(dev);
359                         netdev_info(dev, "link up (%d/%s)\n",
360                                     phydev->speed,
361                                     phydev->duplex == DUPLEX_FULL ?
362                                     "Full" : "Half");
363                 } else {
364                         netif_carrier_off(dev);
365                         netdev_info(dev, "link down\n");
366                 }
367         }
368 }
369
370 /* based on au1000_eth. c*/
371 static int macb_mii_probe(struct net_device *dev)
372 {
373         struct macb *bp = netdev_priv(dev);
374         struct macb_platform_data *pdata;
375         struct phy_device *phydev;
376         int phy_irq;
377         int ret;
378
379         phydev = phy_find_first(bp->mii_bus);
380         if (!phydev) {
381                 netdev_err(dev, "no PHY found\n");
382                 return -ENXIO;
383         }
384
385         pdata = dev_get_platdata(&bp->pdev->dev);
386         if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
387                 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin,
388                                         "phy int");
389                 if (!ret) {
390                         phy_irq = gpio_to_irq(pdata->phy_irq_pin);
391                         phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
392                 }
393         }
394
395         /* attach the mac to the phy */
396         ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
397                                  bp->phy_interface);
398         if (ret) {
399                 netdev_err(dev, "Could not attach to PHY\n");
400                 return ret;
401         }
402
403         /* mask with MAC supported features */
404         if (macb_is_gem(bp) && bp->caps & MACB_CAPS_GIGABIT_MODE_AVAILABLE)
405                 phydev->supported &= PHY_GBIT_FEATURES;
406         else
407                 phydev->supported &= PHY_BASIC_FEATURES;
408
409         if (bp->caps & MACB_CAPS_NO_GIGABIT_HALF)
410                 phydev->supported &= ~SUPPORTED_1000baseT_Half;
411
412         phydev->advertising = phydev->supported;
413
414         bp->link = 0;
415         bp->speed = 0;
416         bp->duplex = -1;
417
418         return 0;
419 }
420
421 static int macb_mii_init(struct macb *bp)
422 {
423         struct macb_platform_data *pdata;
424         struct device_node *np;
425         int err = -ENXIO, i;
426
427         /* Enable management port */
428         macb_writel(bp, NCR, MACB_BIT(MPE));
429
430         bp->mii_bus = mdiobus_alloc();
431         if (!bp->mii_bus) {
432                 err = -ENOMEM;
433                 goto err_out;
434         }
435
436         bp->mii_bus->name = "MACB_mii_bus";
437         bp->mii_bus->read = &macb_mdio_read;
438         bp->mii_bus->write = &macb_mdio_write;
439         snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
440                  bp->pdev->name, bp->pdev->id);
441         bp->mii_bus->priv = bp;
442         bp->mii_bus->parent = &bp->pdev->dev;
443         pdata = dev_get_platdata(&bp->pdev->dev);
444
445         dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
446
447         np = bp->pdev->dev.of_node;
448         if (np) {
449                 /* try dt phy registration */
450                 err = of_mdiobus_register(bp->mii_bus, np);
451
452                 /* fallback to standard phy registration if no phy were
453                  * found during dt phy registration
454                  */
455                 if (!err && !phy_find_first(bp->mii_bus)) {
456                         for (i = 0; i < PHY_MAX_ADDR; i++) {
457                                 struct phy_device *phydev;
458
459                                 phydev = mdiobus_scan(bp->mii_bus, i);
460                                 if (IS_ERR(phydev) &&
461                                     PTR_ERR(phydev) != -ENODEV) {
462                                         err = PTR_ERR(phydev);
463                                         break;
464                                 }
465                         }
466
467                         if (err)
468                                 goto err_out_unregister_bus;
469                 }
470         } else {
471                 if (pdata)
472                         bp->mii_bus->phy_mask = pdata->phy_mask;
473
474                 err = mdiobus_register(bp->mii_bus);
475         }
476
477         if (err)
478                 goto err_out_free_mdiobus;
479
480         err = macb_mii_probe(bp->dev);
481         if (err)
482                 goto err_out_unregister_bus;
483
484         return 0;
485
486 err_out_unregister_bus:
487         mdiobus_unregister(bp->mii_bus);
488 err_out_free_mdiobus:
489         mdiobus_free(bp->mii_bus);
490 err_out:
491         return err;
492 }
493
494 static void macb_update_stats(struct macb *bp)
495 {
496         u32 *p = &bp->hw_stats.macb.rx_pause_frames;
497         u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
498         int offset = MACB_PFR;
499
500         WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
501
502         for (; p < end; p++, offset += 4)
503                 *p += bp->macb_reg_readl(bp, offset);
504 }
505
506 static int macb_halt_tx(struct macb *bp)
507 {
508         unsigned long   halt_time, timeout;
509         u32             status;
510
511         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
512
513         timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
514         do {
515                 halt_time = jiffies;
516                 status = macb_readl(bp, TSR);
517                 if (!(status & MACB_BIT(TGO)))
518                         return 0;
519
520                 udelay(250);
521         } while (time_before(halt_time, timeout));
522
523         return -ETIMEDOUT;
524 }
525
526 static void macb_tx_unmap(struct macb *bp, struct macb_tx_skb *tx_skb)
527 {
528         if (tx_skb->mapping) {
529                 if (tx_skb->mapped_as_page)
530                         dma_unmap_page(&bp->pdev->dev, tx_skb->mapping,
531                                        tx_skb->size, DMA_TO_DEVICE);
532                 else
533                         dma_unmap_single(&bp->pdev->dev, tx_skb->mapping,
534                                          tx_skb->size, DMA_TO_DEVICE);
535                 tx_skb->mapping = 0;
536         }
537
538         if (tx_skb->skb) {
539                 dev_kfree_skb_any(tx_skb->skb);
540                 tx_skb->skb = NULL;
541         }
542 }
543
544 static inline void macb_set_addr(struct macb_dma_desc *desc, dma_addr_t addr)
545 {
546         desc->addr = (u32)addr;
547 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
548         desc->addrh = (u32)(addr >> 32);
549 #endif
550 }
551
552 static void macb_tx_error_task(struct work_struct *work)
553 {
554         struct macb_queue       *queue = container_of(work, struct macb_queue,
555                                                       tx_error_task);
556         struct macb             *bp = queue->bp;
557         struct macb_tx_skb      *tx_skb;
558         struct macb_dma_desc    *desc;
559         struct sk_buff          *skb;
560         unsigned int            tail;
561         unsigned long           flags;
562
563         netdev_vdbg(bp->dev, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
564                     (unsigned int)(queue - bp->queues),
565                     queue->tx_tail, queue->tx_head);
566
567         /* Prevent the queue IRQ handlers from running: each of them may call
568          * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
569          * As explained below, we have to halt the transmission before updating
570          * TBQP registers so we call netif_tx_stop_all_queues() to notify the
571          * network engine about the macb/gem being halted.
572          */
573         spin_lock_irqsave(&bp->lock, flags);
574
575         /* Make sure nobody is trying to queue up new packets */
576         netif_tx_stop_all_queues(bp->dev);
577
578         /* Stop transmission now
579          * (in case we have just queued new packets)
580          * macb/gem must be halted to write TBQP register
581          */
582         if (macb_halt_tx(bp))
583                 /* Just complain for now, reinitializing TX path can be good */
584                 netdev_err(bp->dev, "BUG: halt tx timed out\n");
585
586         /* Treat frames in TX queue including the ones that caused the error.
587          * Free transmit buffers in upper layer.
588          */
589         for (tail = queue->tx_tail; tail != queue->tx_head; tail++) {
590                 u32     ctrl;
591
592                 desc = macb_tx_desc(queue, tail);
593                 ctrl = desc->ctrl;
594                 tx_skb = macb_tx_skb(queue, tail);
595                 skb = tx_skb->skb;
596
597                 if (ctrl & MACB_BIT(TX_USED)) {
598                         /* skb is set for the last buffer of the frame */
599                         while (!skb) {
600                                 macb_tx_unmap(bp, tx_skb);
601                                 tail++;
602                                 tx_skb = macb_tx_skb(queue, tail);
603                                 skb = tx_skb->skb;
604                         }
605
606                         /* ctrl still refers to the first buffer descriptor
607                          * since it's the only one written back by the hardware
608                          */
609                         if (!(ctrl & MACB_BIT(TX_BUF_EXHAUSTED))) {
610                                 netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
611                                             macb_tx_ring_wrap(tail), skb->data);
612                                 bp->stats.tx_packets++;
613                                 bp->stats.tx_bytes += skb->len;
614                         }
615                 } else {
616                         /* "Buffers exhausted mid-frame" errors may only happen
617                          * if the driver is buggy, so complain loudly about
618                          * those. Statistics are updated by hardware.
619                          */
620                         if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
621                                 netdev_err(bp->dev,
622                                            "BUG: TX buffers exhausted mid-frame\n");
623
624                         desc->ctrl = ctrl | MACB_BIT(TX_USED);
625                 }
626
627                 macb_tx_unmap(bp, tx_skb);
628         }
629
630         /* Set end of TX queue */
631         desc = macb_tx_desc(queue, 0);
632         macb_set_addr(desc, 0);
633         desc->ctrl = MACB_BIT(TX_USED);
634
635         /* Make descriptor updates visible to hardware */
636         wmb();
637
638         /* Reinitialize the TX desc queue */
639         queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
640 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
641         queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
642 #endif
643         /* Make TX ring reflect state of hardware */
644         queue->tx_head = 0;
645         queue->tx_tail = 0;
646
647         /* Housework before enabling TX IRQ */
648         macb_writel(bp, TSR, macb_readl(bp, TSR));
649         queue_writel(queue, IER, MACB_TX_INT_FLAGS);
650
651         /* Now we are ready to start transmission again */
652         netif_tx_start_all_queues(bp->dev);
653         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
654
655         spin_unlock_irqrestore(&bp->lock, flags);
656 }
657
658 static void macb_tx_interrupt(struct macb_queue *queue)
659 {
660         unsigned int tail;
661         unsigned int head;
662         u32 status;
663         struct macb *bp = queue->bp;
664         u16 queue_index = queue - bp->queues;
665
666         status = macb_readl(bp, TSR);
667         macb_writel(bp, TSR, status);
668
669         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
670                 queue_writel(queue, ISR, MACB_BIT(TCOMP));
671
672         netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
673                     (unsigned long)status);
674
675         head = queue->tx_head;
676         for (tail = queue->tx_tail; tail != head; tail++) {
677                 struct macb_tx_skb      *tx_skb;
678                 struct sk_buff          *skb;
679                 struct macb_dma_desc    *desc;
680                 u32                     ctrl;
681
682                 desc = macb_tx_desc(queue, tail);
683
684                 /* Make hw descriptor updates visible to CPU */
685                 rmb();
686
687                 ctrl = desc->ctrl;
688
689                 /* TX_USED bit is only set by hardware on the very first buffer
690                  * descriptor of the transmitted frame.
691                  */
692                 if (!(ctrl & MACB_BIT(TX_USED)))
693                         break;
694
695                 /* Process all buffers of the current transmitted frame */
696                 for (;; tail++) {
697                         tx_skb = macb_tx_skb(queue, tail);
698                         skb = tx_skb->skb;
699
700                         /* First, update TX stats if needed */
701                         if (skb) {
702                                 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
703                                             macb_tx_ring_wrap(tail), skb->data);
704                                 bp->stats.tx_packets++;
705                                 bp->stats.tx_bytes += skb->len;
706                         }
707
708                         /* Now we can safely release resources */
709                         macb_tx_unmap(bp, tx_skb);
710
711                         /* skb is set only for the last buffer of the frame.
712                          * WARNING: at this point skb has been freed by
713                          * macb_tx_unmap().
714                          */
715                         if (skb)
716                                 break;
717                 }
718         }
719
720         queue->tx_tail = tail;
721         if (__netif_subqueue_stopped(bp->dev, queue_index) &&
722             CIRC_CNT(queue->tx_head, queue->tx_tail,
723                      TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
724                 netif_wake_subqueue(bp->dev, queue_index);
725 }
726
727 static void gem_rx_refill(struct macb *bp)
728 {
729         unsigned int            entry;
730         struct sk_buff          *skb;
731         dma_addr_t              paddr;
732
733         while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail,
734                           RX_RING_SIZE) > 0) {
735                 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
736
737                 /* Make hw descriptor updates visible to CPU */
738                 rmb();
739
740                 bp->rx_prepared_head++;
741
742                 if (!bp->rx_skbuff[entry]) {
743                         /* allocate sk_buff for this free entry in ring */
744                         skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
745                         if (unlikely(!skb)) {
746                                 netdev_err(bp->dev,
747                                            "Unable to allocate sk_buff\n");
748                                 break;
749                         }
750
751                         /* now fill corresponding descriptor entry */
752                         paddr = dma_map_single(&bp->pdev->dev, skb->data,
753                                                bp->rx_buffer_size,
754                                                DMA_FROM_DEVICE);
755                         if (dma_mapping_error(&bp->pdev->dev, paddr)) {
756                                 dev_kfree_skb(skb);
757                                 break;
758                         }
759
760                         bp->rx_skbuff[entry] = skb;
761
762                         if (entry == RX_RING_SIZE - 1)
763                                 paddr |= MACB_BIT(RX_WRAP);
764                         macb_set_addr(&(bp->rx_ring[entry]), paddr);
765                         bp->rx_ring[entry].ctrl = 0;
766
767                         /* properly align Ethernet header */
768                         skb_reserve(skb, NET_IP_ALIGN);
769                 } else {
770                         bp->rx_ring[entry].addr &= ~MACB_BIT(RX_USED);
771                         bp->rx_ring[entry].ctrl = 0;
772                 }
773         }
774
775         /* Make descriptor updates visible to hardware */
776         wmb();
777
778         netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
779                     bp->rx_prepared_head, bp->rx_tail);
780 }
781
782 /* Mark DMA descriptors from begin up to and not including end as unused */
783 static void discard_partial_frame(struct macb *bp, unsigned int begin,
784                                   unsigned int end)
785 {
786         unsigned int frag;
787
788         for (frag = begin; frag != end; frag++) {
789                 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
790
791                 desc->addr &= ~MACB_BIT(RX_USED);
792         }
793
794         /* Make descriptor updates visible to hardware */
795         wmb();
796
797         /* When this happens, the hardware stats registers for
798          * whatever caused this is updated, so we don't have to record
799          * anything.
800          */
801 }
802
803 static int gem_rx(struct macb *bp, int budget)
804 {
805         unsigned int            len;
806         unsigned int            entry;
807         struct sk_buff          *skb;
808         struct macb_dma_desc    *desc;
809         int                     count = 0;
810
811         while (count < budget) {
812                 u32 ctrl;
813                 dma_addr_t addr;
814                 bool rxused;
815
816                 entry = macb_rx_ring_wrap(bp->rx_tail);
817                 desc = &bp->rx_ring[entry];
818
819                 /* Make hw descriptor updates visible to CPU */
820                 rmb();
821
822                 rxused = (desc->addr & MACB_BIT(RX_USED)) ? true : false;
823                 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
824 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
825                 addr |= ((u64)(desc->addrh) << 32);
826 #endif
827                 ctrl = desc->ctrl;
828
829                 if (!rxused)
830                         break;
831
832                 bp->rx_tail++;
833                 count++;
834
835                 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
836                         netdev_err(bp->dev,
837                                    "not whole frame pointed by descriptor\n");
838                         bp->stats.rx_dropped++;
839                         break;
840                 }
841                 skb = bp->rx_skbuff[entry];
842                 if (unlikely(!skb)) {
843                         netdev_err(bp->dev,
844                                    "inconsistent Rx descriptor chain\n");
845                         bp->stats.rx_dropped++;
846                         break;
847                 }
848                 /* now everything is ready for receiving packet */
849                 bp->rx_skbuff[entry] = NULL;
850                 len = ctrl & bp->rx_frm_len_mask;
851
852                 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
853
854                 skb_put(skb, len);
855                 dma_unmap_single(&bp->pdev->dev, addr,
856                                  bp->rx_buffer_size, DMA_FROM_DEVICE);
857
858                 skb->protocol = eth_type_trans(skb, bp->dev);
859                 skb_checksum_none_assert(skb);
860                 if (bp->dev->features & NETIF_F_RXCSUM &&
861                     !(bp->dev->flags & IFF_PROMISC) &&
862                     GEM_BFEXT(RX_CSUM, ctrl) & GEM_RX_CSUM_CHECKED_MASK)
863                         skb->ip_summed = CHECKSUM_UNNECESSARY;
864
865                 bp->stats.rx_packets++;
866                 bp->stats.rx_bytes += skb->len;
867
868 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
869                 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
870                             skb->len, skb->csum);
871                 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
872                                skb_mac_header(skb), 16, true);
873                 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
874                                skb->data, 32, true);
875 #endif
876
877                 netif_receive_skb(skb);
878         }
879
880         gem_rx_refill(bp);
881
882         return count;
883 }
884
885 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
886                          unsigned int last_frag)
887 {
888         unsigned int len;
889         unsigned int frag;
890         unsigned int offset;
891         struct sk_buff *skb;
892         struct macb_dma_desc *desc;
893
894         desc = macb_rx_desc(bp, last_frag);
895         len = desc->ctrl & bp->rx_frm_len_mask;
896
897         netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
898                     macb_rx_ring_wrap(first_frag),
899                     macb_rx_ring_wrap(last_frag), len);
900
901         /* The ethernet header starts NET_IP_ALIGN bytes into the
902          * first buffer. Since the header is 14 bytes, this makes the
903          * payload word-aligned.
904          *
905          * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
906          * the two padding bytes into the skb so that we avoid hitting
907          * the slowpath in memcpy(), and pull them off afterwards.
908          */
909         skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
910         if (!skb) {
911                 bp->stats.rx_dropped++;
912                 for (frag = first_frag; ; frag++) {
913                         desc = macb_rx_desc(bp, frag);
914                         desc->addr &= ~MACB_BIT(RX_USED);
915                         if (frag == last_frag)
916                                 break;
917                 }
918
919                 /* Make descriptor updates visible to hardware */
920                 wmb();
921
922                 return 1;
923         }
924
925         offset = 0;
926         len += NET_IP_ALIGN;
927         skb_checksum_none_assert(skb);
928         skb_put(skb, len);
929
930         for (frag = first_frag; ; frag++) {
931                 unsigned int frag_len = bp->rx_buffer_size;
932
933                 if (offset + frag_len > len) {
934                         if (unlikely(frag != last_frag)) {
935                                 dev_kfree_skb_any(skb);
936                                 return -1;
937                         }
938                         frag_len = len - offset;
939                 }
940                 skb_copy_to_linear_data_offset(skb, offset,
941                                                macb_rx_buffer(bp, frag),
942                                                frag_len);
943                 offset += bp->rx_buffer_size;
944                 desc = macb_rx_desc(bp, frag);
945                 desc->addr &= ~MACB_BIT(RX_USED);
946
947                 if (frag == last_frag)
948                         break;
949         }
950
951         /* Make descriptor updates visible to hardware */
952         wmb();
953
954         __skb_pull(skb, NET_IP_ALIGN);
955         skb->protocol = eth_type_trans(skb, bp->dev);
956
957         bp->stats.rx_packets++;
958         bp->stats.rx_bytes += skb->len;
959         netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
960                     skb->len, skb->csum);
961         netif_receive_skb(skb);
962
963         return 0;
964 }
965
966 static inline void macb_init_rx_ring(struct macb *bp)
967 {
968         dma_addr_t addr;
969         int i;
970
971         addr = bp->rx_buffers_dma;
972         for (i = 0; i < RX_RING_SIZE; i++) {
973                 bp->rx_ring[i].addr = addr;
974                 bp->rx_ring[i].ctrl = 0;
975                 addr += bp->rx_buffer_size;
976         }
977         bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
978         bp->rx_tail = 0;
979 }
980
981 static int macb_rx(struct macb *bp, int budget)
982 {
983         bool reset_rx_queue = false;
984         int received = 0;
985         unsigned int tail;
986         int first_frag = -1;
987
988         for (tail = bp->rx_tail; budget > 0; tail++) {
989                 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
990                 u32 addr, ctrl;
991
992                 /* Make hw descriptor updates visible to CPU */
993                 rmb();
994
995                 addr = desc->addr;
996                 ctrl = desc->ctrl;
997
998                 if (!(addr & MACB_BIT(RX_USED)))
999                         break;
1000
1001                 if (ctrl & MACB_BIT(RX_SOF)) {
1002                         if (first_frag != -1)
1003                                 discard_partial_frame(bp, first_frag, tail);
1004                         first_frag = tail;
1005                 }
1006
1007                 if (ctrl & MACB_BIT(RX_EOF)) {
1008                         int dropped;
1009
1010                         if (unlikely(first_frag == -1)) {
1011                                 reset_rx_queue = true;
1012                                 continue;
1013                         }
1014
1015                         dropped = macb_rx_frame(bp, first_frag, tail);
1016                         first_frag = -1;
1017                         if (unlikely(dropped < 0)) {
1018                                 reset_rx_queue = true;
1019                                 continue;
1020                         }
1021                         if (!dropped) {
1022                                 received++;
1023                                 budget--;
1024                         }
1025                 }
1026         }
1027
1028         if (unlikely(reset_rx_queue)) {
1029                 unsigned long flags;
1030                 u32 ctrl;
1031
1032                 netdev_err(bp->dev, "RX queue corruption: reset it\n");
1033
1034                 spin_lock_irqsave(&bp->lock, flags);
1035
1036                 ctrl = macb_readl(bp, NCR);
1037                 macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1038
1039                 macb_init_rx_ring(bp);
1040                 macb_writel(bp, RBQP, bp->rx_ring_dma);
1041
1042                 macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1043
1044                 spin_unlock_irqrestore(&bp->lock, flags);
1045                 return received;
1046         }
1047
1048         if (first_frag != -1)
1049                 bp->rx_tail = first_frag;
1050         else
1051                 bp->rx_tail = tail;
1052
1053         return received;
1054 }
1055
1056 static int macb_poll(struct napi_struct *napi, int budget)
1057 {
1058         struct macb *bp = container_of(napi, struct macb, napi);
1059         int work_done;
1060         u32 status;
1061
1062         status = macb_readl(bp, RSR);
1063         macb_writel(bp, RSR, status);
1064
1065         work_done = 0;
1066
1067         netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
1068                     (unsigned long)status, budget);
1069
1070         work_done = bp->macbgem_ops.mog_rx(bp, budget);
1071         if (work_done < budget) {
1072                 napi_complete(napi);
1073
1074                 /* Packets received while interrupts were disabled */
1075                 status = macb_readl(bp, RSR);
1076                 if (status) {
1077                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1078                                 macb_writel(bp, ISR, MACB_BIT(RCOMP));
1079                         napi_reschedule(napi);
1080                 } else {
1081                         macb_writel(bp, IER, MACB_RX_INT_FLAGS);
1082                 }
1083         }
1084
1085         /* TODO: Handle errors */
1086
1087         return work_done;
1088 }
1089
1090 static irqreturn_t macb_interrupt(int irq, void *dev_id)
1091 {
1092         struct macb_queue *queue = dev_id;
1093         struct macb *bp = queue->bp;
1094         struct net_device *dev = bp->dev;
1095         u32 status, ctrl;
1096
1097         status = queue_readl(queue, ISR);
1098
1099         if (unlikely(!status))
1100                 return IRQ_NONE;
1101
1102         spin_lock(&bp->lock);
1103
1104         while (status) {
1105                 /* close possible race with dev_close */
1106                 if (unlikely(!netif_running(dev))) {
1107                         queue_writel(queue, IDR, -1);
1108                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1109                                 queue_writel(queue, ISR, -1);
1110                         break;
1111                 }
1112
1113                 netdev_vdbg(bp->dev, "queue = %u, isr = 0x%08lx\n",
1114                             (unsigned int)(queue - bp->queues),
1115                             (unsigned long)status);
1116
1117                 if (status & MACB_RX_INT_FLAGS) {
1118                         /* There's no point taking any more interrupts
1119                          * until we have processed the buffers. The
1120                          * scheduling call may fail if the poll routine
1121                          * is already scheduled, so disable interrupts
1122                          * now.
1123                          */
1124                         queue_writel(queue, IDR, MACB_RX_INT_FLAGS);
1125                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1126                                 queue_writel(queue, ISR, MACB_BIT(RCOMP));
1127
1128                         if (napi_schedule_prep(&bp->napi)) {
1129                                 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
1130                                 __napi_schedule(&bp->napi);
1131                         }
1132                 }
1133
1134                 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
1135                         queue_writel(queue, IDR, MACB_TX_INT_FLAGS);
1136                         schedule_work(&queue->tx_error_task);
1137
1138                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1139                                 queue_writel(queue, ISR, MACB_TX_ERR_FLAGS);
1140
1141                         break;
1142                 }
1143
1144                 if (status & MACB_BIT(TCOMP))
1145                         macb_tx_interrupt(queue);
1146
1147                 /* Link change detection isn't possible with RMII, so we'll
1148                  * add that if/when we get our hands on a full-blown MII PHY.
1149                  */
1150
1151                 /* There is a hardware issue under heavy load where DMA can
1152                  * stop, this causes endless "used buffer descriptor read"
1153                  * interrupts but it can be cleared by re-enabling RX. See
1154                  * the at91 manual, section 41.3.1 or the Zynq manual
1155                  * section 16.7.4 for details.
1156                  */
1157                 if (status & MACB_BIT(RXUBR)) {
1158                         ctrl = macb_readl(bp, NCR);
1159                         macb_writel(bp, NCR, ctrl & ~MACB_BIT(RE));
1160                         wmb();
1161                         macb_writel(bp, NCR, ctrl | MACB_BIT(RE));
1162
1163                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1164                                 queue_writel(queue, ISR, MACB_BIT(RXUBR));
1165                 }
1166
1167                 if (status & MACB_BIT(ISR_ROVR)) {
1168                         /* We missed at least one packet */
1169                         if (macb_is_gem(bp))
1170                                 bp->hw_stats.gem.rx_overruns++;
1171                         else
1172                                 bp->hw_stats.macb.rx_overruns++;
1173
1174                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1175                                 queue_writel(queue, ISR, MACB_BIT(ISR_ROVR));
1176                 }
1177
1178                 if (status & MACB_BIT(HRESP)) {
1179                         /* TODO: Reset the hardware, and maybe move the
1180                          * netdev_err to a lower-priority context as well
1181                          * (work queue?)
1182                          */
1183                         netdev_err(dev, "DMA bus error: HRESP not OK\n");
1184
1185                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1186                                 queue_writel(queue, ISR, MACB_BIT(HRESP));
1187                 }
1188
1189                 status = queue_readl(queue, ISR);
1190         }
1191
1192         spin_unlock(&bp->lock);
1193
1194         return IRQ_HANDLED;
1195 }
1196
1197 #ifdef CONFIG_NET_POLL_CONTROLLER
1198 /* Polling receive - used by netconsole and other diagnostic tools
1199  * to allow network i/o with interrupts disabled.
1200  */
1201 static void macb_poll_controller(struct net_device *dev)
1202 {
1203         struct macb *bp = netdev_priv(dev);
1204         struct macb_queue *queue;
1205         unsigned long flags;
1206         unsigned int q;
1207
1208         local_irq_save(flags);
1209         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue)
1210                 macb_interrupt(dev->irq, queue);
1211         local_irq_restore(flags);
1212 }
1213 #endif
1214
1215 static unsigned int macb_tx_map(struct macb *bp,
1216                                 struct macb_queue *queue,
1217                                 struct sk_buff *skb)
1218 {
1219         dma_addr_t mapping;
1220         unsigned int len, entry, i, tx_head = queue->tx_head;
1221         struct macb_tx_skb *tx_skb = NULL;
1222         struct macb_dma_desc *desc;
1223         unsigned int offset, size, count = 0;
1224         unsigned int f, nr_frags = skb_shinfo(skb)->nr_frags;
1225         unsigned int eof = 1;
1226         u32 ctrl;
1227
1228         /* First, map non-paged data */
1229         len = skb_headlen(skb);
1230         offset = 0;
1231         while (len) {
1232                 size = min(len, bp->max_tx_length);
1233                 entry = macb_tx_ring_wrap(tx_head);
1234                 tx_skb = &queue->tx_skb[entry];
1235
1236                 mapping = dma_map_single(&bp->pdev->dev,
1237                                          skb->data + offset,
1238                                          size, DMA_TO_DEVICE);
1239                 if (dma_mapping_error(&bp->pdev->dev, mapping))
1240                         goto dma_error;
1241
1242                 /* Save info to properly release resources */
1243                 tx_skb->skb = NULL;
1244                 tx_skb->mapping = mapping;
1245                 tx_skb->size = size;
1246                 tx_skb->mapped_as_page = false;
1247
1248                 len -= size;
1249                 offset += size;
1250                 count++;
1251                 tx_head++;
1252         }
1253
1254         /* Then, map paged data from fragments */
1255         for (f = 0; f < nr_frags; f++) {
1256                 const skb_frag_t *frag = &skb_shinfo(skb)->frags[f];
1257
1258                 len = skb_frag_size(frag);
1259                 offset = 0;
1260                 while (len) {
1261                         size = min(len, bp->max_tx_length);
1262                         entry = macb_tx_ring_wrap(tx_head);
1263                         tx_skb = &queue->tx_skb[entry];
1264
1265                         mapping = skb_frag_dma_map(&bp->pdev->dev, frag,
1266                                                    offset, size, DMA_TO_DEVICE);
1267                         if (dma_mapping_error(&bp->pdev->dev, mapping))
1268                                 goto dma_error;
1269
1270                         /* Save info to properly release resources */
1271                         tx_skb->skb = NULL;
1272                         tx_skb->mapping = mapping;
1273                         tx_skb->size = size;
1274                         tx_skb->mapped_as_page = true;
1275
1276                         len -= size;
1277                         offset += size;
1278                         count++;
1279                         tx_head++;
1280                 }
1281         }
1282
1283         /* Should never happen */
1284         if (unlikely(!tx_skb)) {
1285                 netdev_err(bp->dev, "BUG! empty skb!\n");
1286                 return 0;
1287         }
1288
1289         /* This is the last buffer of the frame: save socket buffer */
1290         tx_skb->skb = skb;
1291
1292         /* Update TX ring: update buffer descriptors in reverse order
1293          * to avoid race condition
1294          */
1295
1296         /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1297          * to set the end of TX queue
1298          */
1299         i = tx_head;
1300         entry = macb_tx_ring_wrap(i);
1301         ctrl = MACB_BIT(TX_USED);
1302         desc = &queue->tx_ring[entry];
1303         desc->ctrl = ctrl;
1304
1305         do {
1306                 i--;
1307                 entry = macb_tx_ring_wrap(i);
1308                 tx_skb = &queue->tx_skb[entry];
1309                 desc = &queue->tx_ring[entry];
1310
1311                 ctrl = (u32)tx_skb->size;
1312                 if (eof) {
1313                         ctrl |= MACB_BIT(TX_LAST);
1314                         eof = 0;
1315                 }
1316                 if (unlikely(entry == (TX_RING_SIZE - 1)))
1317                         ctrl |= MACB_BIT(TX_WRAP);
1318
1319                 /* Set TX buffer descriptor */
1320                 macb_set_addr(desc, tx_skb->mapping);
1321                 /* desc->addr must be visible to hardware before clearing
1322                  * 'TX_USED' bit in desc->ctrl.
1323                  */
1324                 wmb();
1325                 desc->ctrl = ctrl;
1326         } while (i != queue->tx_head);
1327
1328         queue->tx_head = tx_head;
1329
1330         return count;
1331
1332 dma_error:
1333         netdev_err(bp->dev, "TX DMA map failed\n");
1334
1335         for (i = queue->tx_head; i != tx_head; i++) {
1336                 tx_skb = macb_tx_skb(queue, i);
1337
1338                 macb_tx_unmap(bp, tx_skb);
1339         }
1340
1341         return 0;
1342 }
1343
1344 static inline int macb_clear_csum(struct sk_buff *skb)
1345 {
1346         /* no change for packets without checksum offloading */
1347         if (skb->ip_summed != CHECKSUM_PARTIAL)
1348                 return 0;
1349
1350         /* make sure we can modify the header */
1351         if (unlikely(skb_cow_head(skb, 0)))
1352                 return -1;
1353
1354         /* initialize checksum field
1355          * This is required - at least for Zynq, which otherwise calculates
1356          * wrong UDP header checksums for UDP packets with UDP data len <=2
1357          */
1358         *(__sum16 *)(skb_checksum_start(skb) + skb->csum_offset) = 0;
1359         return 0;
1360 }
1361
1362 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
1363 {
1364         u16 queue_index = skb_get_queue_mapping(skb);
1365         struct macb *bp = netdev_priv(dev);
1366         struct macb_queue *queue = &bp->queues[queue_index];
1367         unsigned long flags;
1368         unsigned int count, nr_frags, frag_size, f;
1369
1370 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1371         netdev_vdbg(bp->dev,
1372                     "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1373                     queue_index, skb->len, skb->head, skb->data,
1374                     skb_tail_pointer(skb), skb_end_pointer(skb));
1375         print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
1376                        skb->data, 16, true);
1377 #endif
1378
1379         /* Count how many TX buffer descriptors are needed to send this
1380          * socket buffer: skb fragments of jumbo frames may need to be
1381          * split into many buffer descriptors.
1382          */
1383         count = DIV_ROUND_UP(skb_headlen(skb), bp->max_tx_length);
1384         nr_frags = skb_shinfo(skb)->nr_frags;
1385         for (f = 0; f < nr_frags; f++) {
1386                 frag_size = skb_frag_size(&skb_shinfo(skb)->frags[f]);
1387                 count += DIV_ROUND_UP(frag_size, bp->max_tx_length);
1388         }
1389
1390         spin_lock_irqsave(&bp->lock, flags);
1391
1392         /* This is a hard error, log it. */
1393         if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < count) {
1394                 netif_stop_subqueue(dev, queue_index);
1395                 spin_unlock_irqrestore(&bp->lock, flags);
1396                 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
1397                            queue->tx_head, queue->tx_tail);
1398                 return NETDEV_TX_BUSY;
1399         }
1400
1401         if (macb_clear_csum(skb)) {
1402                 dev_kfree_skb_any(skb);
1403                 goto unlock;
1404         }
1405
1406         /* Map socket buffer for DMA transfer */
1407         if (!macb_tx_map(bp, queue, skb)) {
1408                 dev_kfree_skb_any(skb);
1409                 goto unlock;
1410         }
1411
1412         /* Make newly initialized descriptor visible to hardware */
1413         wmb();
1414
1415         skb_tx_timestamp(skb);
1416
1417         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1418
1419         if (CIRC_SPACE(queue->tx_head, queue->tx_tail, TX_RING_SIZE) < 1)
1420                 netif_stop_subqueue(dev, queue_index);
1421
1422 unlock:
1423         spin_unlock_irqrestore(&bp->lock, flags);
1424
1425         return NETDEV_TX_OK;
1426 }
1427
1428 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1429 {
1430         if (!macb_is_gem(bp)) {
1431                 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1432         } else {
1433                 bp->rx_buffer_size = size;
1434
1435                 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1436                         netdev_dbg(bp->dev,
1437                                    "RX buffer must be multiple of %d bytes, expanding\n",
1438                                    RX_BUFFER_MULTIPLE);
1439                         bp->rx_buffer_size =
1440                                 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1441                 }
1442         }
1443
1444         netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1445                    bp->dev->mtu, bp->rx_buffer_size);
1446 }
1447
1448 static void gem_free_rx_buffers(struct macb *bp)
1449 {
1450         struct sk_buff          *skb;
1451         struct macb_dma_desc    *desc;
1452         dma_addr_t              addr;
1453         int i;
1454
1455         if (!bp->rx_skbuff)
1456                 return;
1457
1458         for (i = 0; i < RX_RING_SIZE; i++) {
1459                 skb = bp->rx_skbuff[i];
1460
1461                 if (!skb)
1462                         continue;
1463
1464                 desc = &bp->rx_ring[i];
1465                 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1466 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1467                 addr |= ((u64)(desc->addrh) << 32);
1468 #endif
1469                 dma_unmap_single(&bp->pdev->dev, addr, bp->rx_buffer_size,
1470                                  DMA_FROM_DEVICE);
1471                 dev_kfree_skb_any(skb);
1472                 skb = NULL;
1473         }
1474
1475         kfree(bp->rx_skbuff);
1476         bp->rx_skbuff = NULL;
1477 }
1478
1479 static void macb_free_rx_buffers(struct macb *bp)
1480 {
1481         if (bp->rx_buffers) {
1482                 dma_free_coherent(&bp->pdev->dev,
1483                                   RX_RING_SIZE * bp->rx_buffer_size,
1484                                   bp->rx_buffers, bp->rx_buffers_dma);
1485                 bp->rx_buffers = NULL;
1486         }
1487 }
1488
1489 static void macb_free_consistent(struct macb *bp)
1490 {
1491         struct macb_queue *queue;
1492         unsigned int q;
1493
1494         bp->macbgem_ops.mog_free_rx_buffers(bp);
1495         if (bp->rx_ring) {
1496                 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1497                                   bp->rx_ring, bp->rx_ring_dma);
1498                 bp->rx_ring = NULL;
1499         }
1500
1501         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1502                 kfree(queue->tx_skb);
1503                 queue->tx_skb = NULL;
1504                 if (queue->tx_ring) {
1505                         dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1506                                           queue->tx_ring, queue->tx_ring_dma);
1507                         queue->tx_ring = NULL;
1508                 }
1509         }
1510 }
1511
1512 static int gem_alloc_rx_buffers(struct macb *bp)
1513 {
1514         int size;
1515
1516         size = RX_RING_SIZE * sizeof(struct sk_buff *);
1517         bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1518         if (!bp->rx_skbuff)
1519                 return -ENOMEM;
1520
1521         netdev_dbg(bp->dev,
1522                    "Allocated %d RX struct sk_buff entries at %p\n",
1523                    RX_RING_SIZE, bp->rx_skbuff);
1524         return 0;
1525 }
1526
1527 static int macb_alloc_rx_buffers(struct macb *bp)
1528 {
1529         int size;
1530
1531         size = RX_RING_SIZE * bp->rx_buffer_size;
1532         bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1533                                             &bp->rx_buffers_dma, GFP_KERNEL);
1534         if (!bp->rx_buffers)
1535                 return -ENOMEM;
1536
1537         netdev_dbg(bp->dev,
1538                    "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1539                    size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1540         return 0;
1541 }
1542
1543 static int macb_alloc_consistent(struct macb *bp)
1544 {
1545         struct macb_queue *queue;
1546         unsigned int q;
1547         int size;
1548
1549         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1550                 size = TX_RING_BYTES;
1551                 queue->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1552                                                     &queue->tx_ring_dma,
1553                                                     GFP_KERNEL);
1554                 if (!queue->tx_ring)
1555                         goto out_err;
1556                 netdev_dbg(bp->dev,
1557                            "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1558                            q, size, (unsigned long)queue->tx_ring_dma,
1559                            queue->tx_ring);
1560
1561                 size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1562                 queue->tx_skb = kmalloc(size, GFP_KERNEL);
1563                 if (!queue->tx_skb)
1564                         goto out_err;
1565         }
1566
1567         size = RX_RING_BYTES;
1568         bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1569                                          &bp->rx_ring_dma, GFP_KERNEL);
1570         if (!bp->rx_ring)
1571                 goto out_err;
1572         netdev_dbg(bp->dev,
1573                    "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1574                    size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
1575
1576         if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
1577                 goto out_err;
1578
1579         return 0;
1580
1581 out_err:
1582         macb_free_consistent(bp);
1583         return -ENOMEM;
1584 }
1585
1586 static void gem_init_rings(struct macb *bp)
1587 {
1588         struct macb_queue *queue;
1589         unsigned int q;
1590         int i;
1591
1592         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1593                 for (i = 0; i < TX_RING_SIZE; i++) {
1594                         macb_set_addr(&(queue->tx_ring[i]), 0);
1595                         queue->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1596                 }
1597                 queue->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1598                 queue->tx_head = 0;
1599                 queue->tx_tail = 0;
1600         }
1601
1602         bp->rx_tail = 0;
1603         bp->rx_prepared_head = 0;
1604
1605         gem_rx_refill(bp);
1606 }
1607
1608 static void macb_init_rings(struct macb *bp)
1609 {
1610         int i;
1611
1612         macb_init_rx_ring(bp);
1613
1614         for (i = 0; i < TX_RING_SIZE; i++) {
1615                 bp->queues[0].tx_ring[i].addr = 0;
1616                 bp->queues[0].tx_ring[i].ctrl = MACB_BIT(TX_USED);
1617         }
1618         bp->queues[0].tx_head = 0;
1619         bp->queues[0].tx_tail = 0;
1620         bp->queues[0].tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1621 }
1622
1623 static void macb_reset_hw(struct macb *bp)
1624 {
1625         struct macb_queue *queue;
1626         unsigned int q;
1627
1628         /* Disable RX and TX (XXX: Should we halt the transmission
1629          * more gracefully?)
1630          */
1631         macb_writel(bp, NCR, 0);
1632
1633         /* Clear the stats registers (XXX: Update stats first?) */
1634         macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1635
1636         /* Clear all status flags */
1637         macb_writel(bp, TSR, -1);
1638         macb_writel(bp, RSR, -1);
1639
1640         /* Disable all interrupts */
1641         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1642                 queue_writel(queue, IDR, -1);
1643                 queue_readl(queue, ISR);
1644                 if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
1645                         queue_writel(queue, ISR, -1);
1646         }
1647 }
1648
1649 static u32 gem_mdc_clk_div(struct macb *bp)
1650 {
1651         u32 config;
1652         unsigned long pclk_hz = clk_get_rate(bp->pclk);
1653
1654         if (pclk_hz <= 20000000)
1655                 config = GEM_BF(CLK, GEM_CLK_DIV8);
1656         else if (pclk_hz <= 40000000)
1657                 config = GEM_BF(CLK, GEM_CLK_DIV16);
1658         else if (pclk_hz <= 80000000)
1659                 config = GEM_BF(CLK, GEM_CLK_DIV32);
1660         else if (pclk_hz <= 120000000)
1661                 config = GEM_BF(CLK, GEM_CLK_DIV48);
1662         else if (pclk_hz <= 160000000)
1663                 config = GEM_BF(CLK, GEM_CLK_DIV64);
1664         else
1665                 config = GEM_BF(CLK, GEM_CLK_DIV96);
1666
1667         return config;
1668 }
1669
1670 static u32 macb_mdc_clk_div(struct macb *bp)
1671 {
1672         u32 config;
1673         unsigned long pclk_hz;
1674
1675         if (macb_is_gem(bp))
1676                 return gem_mdc_clk_div(bp);
1677
1678         pclk_hz = clk_get_rate(bp->pclk);
1679         if (pclk_hz <= 20000000)
1680                 config = MACB_BF(CLK, MACB_CLK_DIV8);
1681         else if (pclk_hz <= 40000000)
1682                 config = MACB_BF(CLK, MACB_CLK_DIV16);
1683         else if (pclk_hz <= 80000000)
1684                 config = MACB_BF(CLK, MACB_CLK_DIV32);
1685         else
1686                 config = MACB_BF(CLK, MACB_CLK_DIV64);
1687
1688         return config;
1689 }
1690
1691 /* Get the DMA bus width field of the network configuration register that we
1692  * should program.  We find the width from decoding the design configuration
1693  * register to find the maximum supported data bus width.
1694  */
1695 static u32 macb_dbw(struct macb *bp)
1696 {
1697         if (!macb_is_gem(bp))
1698                 return 0;
1699
1700         switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1701         case 4:
1702                 return GEM_BF(DBW, GEM_DBW128);
1703         case 2:
1704                 return GEM_BF(DBW, GEM_DBW64);
1705         case 1:
1706         default:
1707                 return GEM_BF(DBW, GEM_DBW32);
1708         }
1709 }
1710
1711 /* Configure the receive DMA engine
1712  * - use the correct receive buffer size
1713  * - set best burst length for DMA operations
1714  *   (if not supported by FIFO, it will fallback to default)
1715  * - set both rx/tx packet buffers to full memory size
1716  * These are configurable parameters for GEM.
1717  */
1718 static void macb_configure_dma(struct macb *bp)
1719 {
1720         u32 dmacfg;
1721
1722         if (macb_is_gem(bp)) {
1723                 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
1724                 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
1725                 if (bp->dma_burst_length)
1726                         dmacfg = GEM_BFINS(FBLDO, bp->dma_burst_length, dmacfg);
1727                 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
1728                 dmacfg &= ~GEM_BIT(ENDIA_PKT);
1729
1730                 if (bp->native_io)
1731                         dmacfg &= ~GEM_BIT(ENDIA_DESC);
1732                 else
1733                         dmacfg |= GEM_BIT(ENDIA_DESC); /* CPU in big endian */
1734
1735                 if (bp->dev->features & NETIF_F_HW_CSUM)
1736                         dmacfg |= GEM_BIT(TXCOEN);
1737                 else
1738                         dmacfg &= ~GEM_BIT(TXCOEN);
1739
1740                 dmacfg &= ~GEM_BIT(ADDR64);
1741 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1742                 dmacfg |= GEM_BIT(ADDR64);
1743 #endif
1744                 netdev_dbg(bp->dev, "Cadence configure DMA with 0x%08x\n",
1745                            dmacfg);
1746                 gem_writel(bp, DMACFG, dmacfg);
1747         }
1748 }
1749
1750 static void macb_init_hw(struct macb *bp)
1751 {
1752         struct macb_queue *queue;
1753         unsigned int q;
1754
1755         u32 config;
1756
1757         macb_reset_hw(bp);
1758         macb_set_hwaddr(bp);
1759
1760         config = macb_mdc_clk_div(bp);
1761         if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
1762                 config |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
1763         config |= MACB_BF(RBOF, NET_IP_ALIGN);  /* Make eth data aligned */
1764         config |= MACB_BIT(PAE);                /* PAuse Enable */
1765         config |= MACB_BIT(DRFCS);              /* Discard Rx FCS */
1766         if (bp->caps & MACB_CAPS_JUMBO)
1767                 config |= MACB_BIT(JFRAME);     /* Enable jumbo frames */
1768         else
1769                 config |= MACB_BIT(BIG);        /* Receive oversized frames */
1770         if (bp->dev->flags & IFF_PROMISC)
1771                 config |= MACB_BIT(CAF);        /* Copy All Frames */
1772         else if (macb_is_gem(bp) && bp->dev->features & NETIF_F_RXCSUM)
1773                 config |= GEM_BIT(RXCOEN);
1774         if (!(bp->dev->flags & IFF_BROADCAST))
1775                 config |= MACB_BIT(NBC);        /* No BroadCast */
1776         config |= macb_dbw(bp);
1777         macb_writel(bp, NCFGR, config);
1778         if ((bp->caps & MACB_CAPS_JUMBO) && bp->jumbo_max_len)
1779                 gem_writel(bp, JML, bp->jumbo_max_len);
1780         bp->speed = SPEED_10;
1781         bp->duplex = DUPLEX_HALF;
1782         bp->rx_frm_len_mask = MACB_RX_FRMLEN_MASK;
1783         if (bp->caps & MACB_CAPS_JUMBO)
1784                 bp->rx_frm_len_mask = MACB_RX_JFRMLEN_MASK;
1785
1786         macb_configure_dma(bp);
1787
1788         /* Initialize TX and RX buffers */
1789         macb_writel(bp, RBQP, (u32)(bp->rx_ring_dma));
1790 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1791         macb_writel(bp, RBQPH, (u32)(bp->rx_ring_dma >> 32));
1792 #endif
1793         for (q = 0, queue = bp->queues; q < bp->num_queues; ++q, ++queue) {
1794                 queue_writel(queue, TBQP, (u32)(queue->tx_ring_dma));
1795 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
1796                 queue_writel(queue, TBQPH, (u32)(queue->tx_ring_dma >> 32));
1797 #endif
1798
1799                 /* Enable interrupts */
1800                 queue_writel(queue, IER,
1801                              MACB_RX_INT_FLAGS |
1802                              MACB_TX_INT_FLAGS |
1803                              MACB_BIT(HRESP));
1804         }
1805
1806         /* Enable TX and RX */
1807         macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
1808 }
1809
1810 /* The hash address register is 64 bits long and takes up two
1811  * locations in the memory map.  The least significant bits are stored
1812  * in EMAC_HSL and the most significant bits in EMAC_HSH.
1813  *
1814  * The unicast hash enable and the multicast hash enable bits in the
1815  * network configuration register enable the reception of hash matched
1816  * frames. The destination address is reduced to a 6 bit index into
1817  * the 64 bit hash register using the following hash function.  The
1818  * hash function is an exclusive or of every sixth bit of the
1819  * destination address.
1820  *
1821  * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1822  * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1823  * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1824  * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1825  * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1826  * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1827  *
1828  * da[0] represents the least significant bit of the first byte
1829  * received, that is, the multicast/unicast indicator, and da[47]
1830  * represents the most significant bit of the last byte received.  If
1831  * the hash index, hi[n], points to a bit that is set in the hash
1832  * register then the frame will be matched according to whether the
1833  * frame is multicast or unicast.  A multicast match will be signalled
1834  * if the multicast hash enable bit is set, da[0] is 1 and the hash
1835  * index points to a bit set in the hash register.  A unicast match
1836  * will be signalled if the unicast hash enable bit is set, da[0] is 0
1837  * and the hash index points to a bit set in the hash register.  To
1838  * receive all multicast frames, the hash register should be set with
1839  * all ones and the multicast hash enable bit should be set in the
1840  * network configuration register.
1841  */
1842
1843 static inline int hash_bit_value(int bitnr, __u8 *addr)
1844 {
1845         if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1846                 return 1;
1847         return 0;
1848 }
1849
1850 /* Return the hash index value for the specified address. */
1851 static int hash_get_index(__u8 *addr)
1852 {
1853         int i, j, bitval;
1854         int hash_index = 0;
1855
1856         for (j = 0; j < 6; j++) {
1857                 for (i = 0, bitval = 0; i < 8; i++)
1858                         bitval ^= hash_bit_value(i * 6 + j, addr);
1859
1860                 hash_index |= (bitval << j);
1861         }
1862
1863         return hash_index;
1864 }
1865
1866 /* Add multicast addresses to the internal multicast-hash table. */
1867 static void macb_sethashtable(struct net_device *dev)
1868 {
1869         struct netdev_hw_addr *ha;
1870         unsigned long mc_filter[2];
1871         unsigned int bitnr;
1872         struct macb *bp = netdev_priv(dev);
1873
1874         mc_filter[0] = 0;
1875         mc_filter[1] = 0;
1876
1877         netdev_for_each_mc_addr(ha, dev) {
1878                 bitnr = hash_get_index(ha->addr);
1879                 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1880         }
1881
1882         macb_or_gem_writel(bp, HRB, mc_filter[0]);
1883         macb_or_gem_writel(bp, HRT, mc_filter[1]);
1884 }
1885
1886 /* Enable/Disable promiscuous and multicast modes. */
1887 static void macb_set_rx_mode(struct net_device *dev)
1888 {
1889         unsigned long cfg;
1890         struct macb *bp = netdev_priv(dev);
1891
1892         cfg = macb_readl(bp, NCFGR);
1893
1894         if (dev->flags & IFF_PROMISC) {
1895                 /* Enable promiscuous mode */
1896                 cfg |= MACB_BIT(CAF);
1897
1898                 /* Disable RX checksum offload */
1899                 if (macb_is_gem(bp))
1900                         cfg &= ~GEM_BIT(RXCOEN);
1901         } else {
1902                 /* Disable promiscuous mode */
1903                 cfg &= ~MACB_BIT(CAF);
1904
1905                 /* Enable RX checksum offload only if requested */
1906                 if (macb_is_gem(bp) && dev->features & NETIF_F_RXCSUM)
1907                         cfg |= GEM_BIT(RXCOEN);
1908         }
1909
1910         if (dev->flags & IFF_ALLMULTI) {
1911                 /* Enable all multicast mode */
1912                 macb_or_gem_writel(bp, HRB, -1);
1913                 macb_or_gem_writel(bp, HRT, -1);
1914                 cfg |= MACB_BIT(NCFGR_MTI);
1915         } else if (!netdev_mc_empty(dev)) {
1916                 /* Enable specific multicasts */
1917                 macb_sethashtable(dev);
1918                 cfg |= MACB_BIT(NCFGR_MTI);
1919         } else if (dev->flags & (~IFF_ALLMULTI)) {
1920                 /* Disable all multicast mode */
1921                 macb_or_gem_writel(bp, HRB, 0);
1922                 macb_or_gem_writel(bp, HRT, 0);
1923                 cfg &= ~MACB_BIT(NCFGR_MTI);
1924         }
1925
1926         macb_writel(bp, NCFGR, cfg);
1927 }
1928
1929 static int macb_open(struct net_device *dev)
1930 {
1931         struct macb *bp = netdev_priv(dev);
1932         size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
1933         int err;
1934
1935         netdev_dbg(bp->dev, "open\n");
1936
1937         /* carrier starts down */
1938         netif_carrier_off(dev);
1939
1940         /* if the phy is not yet register, retry later*/
1941         if (!dev->phydev)
1942                 return -EAGAIN;
1943
1944         /* RX buffers initialization */
1945         macb_init_rx_buffer_size(bp, bufsz);
1946
1947         err = macb_alloc_consistent(bp);
1948         if (err) {
1949                 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1950                            err);
1951                 return err;
1952         }
1953
1954         napi_enable(&bp->napi);
1955
1956         bp->macbgem_ops.mog_init_rings(bp);
1957         macb_init_hw(bp);
1958
1959         /* schedule a link state check */
1960         phy_start(dev->phydev);
1961
1962         netif_tx_start_all_queues(dev);
1963
1964         return 0;
1965 }
1966
1967 static int macb_close(struct net_device *dev)
1968 {
1969         struct macb *bp = netdev_priv(dev);
1970         unsigned long flags;
1971
1972         netif_tx_stop_all_queues(dev);
1973         napi_disable(&bp->napi);
1974
1975         if (dev->phydev)
1976                 phy_stop(dev->phydev);
1977
1978         spin_lock_irqsave(&bp->lock, flags);
1979         macb_reset_hw(bp);
1980         netif_carrier_off(dev);
1981         spin_unlock_irqrestore(&bp->lock, flags);
1982
1983         macb_free_consistent(bp);
1984
1985         return 0;
1986 }
1987
1988 static int macb_change_mtu(struct net_device *dev, int new_mtu)
1989 {
1990         struct macb *bp = netdev_priv(dev);
1991         u32 max_mtu;
1992
1993         if (netif_running(dev))
1994                 return -EBUSY;
1995
1996         max_mtu = ETH_DATA_LEN;
1997         if (bp->caps & MACB_CAPS_JUMBO)
1998                 max_mtu = gem_readl(bp, JML) - ETH_HLEN - ETH_FCS_LEN;
1999
2000         if ((new_mtu > max_mtu) || (new_mtu < GEM_MTU_MIN_SIZE))
2001                 return -EINVAL;
2002
2003         dev->mtu = new_mtu;
2004
2005         return 0;
2006 }
2007
2008 static void gem_update_stats(struct macb *bp)
2009 {
2010         unsigned int i;
2011         u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
2012
2013         for (i = 0; i < GEM_STATS_LEN; ++i, ++p) {
2014                 u32 offset = gem_statistics[i].offset;
2015                 u64 val = bp->macb_reg_readl(bp, offset);
2016
2017                 bp->ethtool_stats[i] += val;
2018                 *p += val;
2019
2020                 if (offset == GEM_OCTTXL || offset == GEM_OCTRXL) {
2021                         /* Add GEM_OCTTXH, GEM_OCTRXH */
2022                         val = bp->macb_reg_readl(bp, offset + 4);
2023                         bp->ethtool_stats[i] += ((u64)val) << 32;
2024                         *(++p) += val;
2025                 }
2026         }
2027 }
2028
2029 static struct net_device_stats *gem_get_stats(struct macb *bp)
2030 {
2031         struct gem_stats *hwstat = &bp->hw_stats.gem;
2032         struct net_device_stats *nstat = &bp->stats;
2033
2034         if (!netif_running(bp->dev))
2035                 return nstat;
2036
2037         gem_update_stats(bp);
2038
2039         nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
2040                             hwstat->rx_alignment_errors +
2041                             hwstat->rx_resource_errors +
2042                             hwstat->rx_overruns +
2043                             hwstat->rx_oversize_frames +
2044                             hwstat->rx_jabbers +
2045                             hwstat->rx_undersized_frames +
2046                             hwstat->rx_length_field_frame_errors);
2047         nstat->tx_errors = (hwstat->tx_late_collisions +
2048                             hwstat->tx_excessive_collisions +
2049                             hwstat->tx_underrun +
2050                             hwstat->tx_carrier_sense_errors);
2051         nstat->multicast = hwstat->rx_multicast_frames;
2052         nstat->collisions = (hwstat->tx_single_collision_frames +
2053                              hwstat->tx_multiple_collision_frames +
2054                              hwstat->tx_excessive_collisions);
2055         nstat->rx_length_errors = (hwstat->rx_oversize_frames +
2056                                    hwstat->rx_jabbers +
2057                                    hwstat->rx_undersized_frames +
2058                                    hwstat->rx_length_field_frame_errors);
2059         nstat->rx_over_errors = hwstat->rx_resource_errors;
2060         nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
2061         nstat->rx_frame_errors = hwstat->rx_alignment_errors;
2062         nstat->rx_fifo_errors = hwstat->rx_overruns;
2063         nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
2064         nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
2065         nstat->tx_fifo_errors = hwstat->tx_underrun;
2066
2067         return nstat;
2068 }
2069
2070 static void gem_get_ethtool_stats(struct net_device *dev,
2071                                   struct ethtool_stats *stats, u64 *data)
2072 {
2073         struct macb *bp;
2074
2075         bp = netdev_priv(dev);
2076         gem_update_stats(bp);
2077         memcpy(data, &bp->ethtool_stats, sizeof(u64) * GEM_STATS_LEN);
2078 }
2079
2080 static int gem_get_sset_count(struct net_device *dev, int sset)
2081 {
2082         switch (sset) {
2083         case ETH_SS_STATS:
2084                 return GEM_STATS_LEN;
2085         default:
2086                 return -EOPNOTSUPP;
2087         }
2088 }
2089
2090 static void gem_get_ethtool_strings(struct net_device *dev, u32 sset, u8 *p)
2091 {
2092         unsigned int i;
2093
2094         switch (sset) {
2095         case ETH_SS_STATS:
2096                 for (i = 0; i < GEM_STATS_LEN; i++, p += ETH_GSTRING_LEN)
2097                         memcpy(p, gem_statistics[i].stat_string,
2098                                ETH_GSTRING_LEN);
2099                 break;
2100         }
2101 }
2102
2103 static struct net_device_stats *macb_get_stats(struct net_device *dev)
2104 {
2105         struct macb *bp = netdev_priv(dev);
2106         struct net_device_stats *nstat = &bp->stats;
2107         struct macb_stats *hwstat = &bp->hw_stats.macb;
2108
2109         if (macb_is_gem(bp))
2110                 return gem_get_stats(bp);
2111
2112         /* read stats from hardware */
2113         macb_update_stats(bp);
2114
2115         /* Convert HW stats into netdevice stats */
2116         nstat->rx_errors = (hwstat->rx_fcs_errors +
2117                             hwstat->rx_align_errors +
2118                             hwstat->rx_resource_errors +
2119                             hwstat->rx_overruns +
2120                             hwstat->rx_oversize_pkts +
2121                             hwstat->rx_jabbers +
2122                             hwstat->rx_undersize_pkts +
2123                             hwstat->rx_length_mismatch);
2124         nstat->tx_errors = (hwstat->tx_late_cols +
2125                             hwstat->tx_excessive_cols +
2126                             hwstat->tx_underruns +
2127                             hwstat->tx_carrier_errors +
2128                             hwstat->sqe_test_errors);
2129         nstat->collisions = (hwstat->tx_single_cols +
2130                              hwstat->tx_multiple_cols +
2131                              hwstat->tx_excessive_cols);
2132         nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
2133                                    hwstat->rx_jabbers +
2134                                    hwstat->rx_undersize_pkts +
2135                                    hwstat->rx_length_mismatch);
2136         nstat->rx_over_errors = hwstat->rx_resource_errors +
2137                                    hwstat->rx_overruns;
2138         nstat->rx_crc_errors = hwstat->rx_fcs_errors;
2139         nstat->rx_frame_errors = hwstat->rx_align_errors;
2140         nstat->rx_fifo_errors = hwstat->rx_overruns;
2141         /* XXX: What does "missed" mean? */
2142         nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
2143         nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
2144         nstat->tx_fifo_errors = hwstat->tx_underruns;
2145         /* Don't know about heartbeat or window errors... */
2146
2147         return nstat;
2148 }
2149
2150 static int macb_get_regs_len(struct net_device *netdev)
2151 {
2152         return MACB_GREGS_NBR * sizeof(u32);
2153 }
2154
2155 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
2156                           void *p)
2157 {
2158         struct macb *bp = netdev_priv(dev);
2159         unsigned int tail, head;
2160         u32 *regs_buff = p;
2161
2162         regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
2163                         | MACB_GREGS_VERSION;
2164
2165         tail = macb_tx_ring_wrap(bp->queues[0].tx_tail);
2166         head = macb_tx_ring_wrap(bp->queues[0].tx_head);
2167
2168         regs_buff[0]  = macb_readl(bp, NCR);
2169         regs_buff[1]  = macb_or_gem_readl(bp, NCFGR);
2170         regs_buff[2]  = macb_readl(bp, NSR);
2171         regs_buff[3]  = macb_readl(bp, TSR);
2172         regs_buff[4]  = macb_readl(bp, RBQP);
2173         regs_buff[5]  = macb_readl(bp, TBQP);
2174         regs_buff[6]  = macb_readl(bp, RSR);
2175         regs_buff[7]  = macb_readl(bp, IMR);
2176
2177         regs_buff[8]  = tail;
2178         regs_buff[9]  = head;
2179         regs_buff[10] = macb_tx_dma(&bp->queues[0], tail);
2180         regs_buff[11] = macb_tx_dma(&bp->queues[0], head);
2181
2182         if (!(bp->caps & MACB_CAPS_USRIO_DISABLED))
2183                 regs_buff[12] = macb_or_gem_readl(bp, USRIO);
2184         if (macb_is_gem(bp))
2185                 regs_buff[13] = gem_readl(bp, DMACFG);
2186 }
2187
2188 static void macb_get_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2189 {
2190         struct macb *bp = netdev_priv(netdev);
2191
2192         wol->supported = 0;
2193         wol->wolopts = 0;
2194
2195         if (bp->wol & MACB_WOL_HAS_MAGIC_PACKET) {
2196                 wol->supported = WAKE_MAGIC;
2197
2198                 if (bp->wol & MACB_WOL_ENABLED)
2199                         wol->wolopts |= WAKE_MAGIC;
2200         }
2201 }
2202
2203 static int macb_set_wol(struct net_device *netdev, struct ethtool_wolinfo *wol)
2204 {
2205         struct macb *bp = netdev_priv(netdev);
2206
2207         if (!(bp->wol & MACB_WOL_HAS_MAGIC_PACKET) ||
2208             (wol->wolopts & ~WAKE_MAGIC))
2209                 return -EOPNOTSUPP;
2210
2211         if (wol->wolopts & WAKE_MAGIC)
2212                 bp->wol |= MACB_WOL_ENABLED;
2213         else
2214                 bp->wol &= ~MACB_WOL_ENABLED;
2215
2216         device_set_wakeup_enable(&bp->pdev->dev, bp->wol & MACB_WOL_ENABLED);
2217
2218         return 0;
2219 }
2220
2221 static const struct ethtool_ops macb_ethtool_ops = {
2222         .get_regs_len           = macb_get_regs_len,
2223         .get_regs               = macb_get_regs,
2224         .get_link               = ethtool_op_get_link,
2225         .get_ts_info            = ethtool_op_get_ts_info,
2226         .get_wol                = macb_get_wol,
2227         .set_wol                = macb_set_wol,
2228         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
2229         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
2230 };
2231
2232 static const struct ethtool_ops gem_ethtool_ops = {
2233         .get_regs_len           = macb_get_regs_len,
2234         .get_regs               = macb_get_regs,
2235         .get_link               = ethtool_op_get_link,
2236         .get_ts_info            = ethtool_op_get_ts_info,
2237         .get_ethtool_stats      = gem_get_ethtool_stats,
2238         .get_strings            = gem_get_ethtool_strings,
2239         .get_sset_count         = gem_get_sset_count,
2240         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
2241         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
2242 };
2243
2244 static int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2245 {
2246         struct phy_device *phydev = dev->phydev;
2247
2248         if (!netif_running(dev))
2249                 return -EINVAL;
2250
2251         if (!phydev)
2252                 return -ENODEV;
2253
2254         return phy_mii_ioctl(phydev, rq, cmd);
2255 }
2256
2257 static int macb_set_features(struct net_device *netdev,
2258                              netdev_features_t features)
2259 {
2260         struct macb *bp = netdev_priv(netdev);
2261         netdev_features_t changed = features ^ netdev->features;
2262
2263         /* TX checksum offload */
2264         if ((changed & NETIF_F_HW_CSUM) && macb_is_gem(bp)) {
2265                 u32 dmacfg;
2266
2267                 dmacfg = gem_readl(bp, DMACFG);
2268                 if (features & NETIF_F_HW_CSUM)
2269                         dmacfg |= GEM_BIT(TXCOEN);
2270                 else
2271                         dmacfg &= ~GEM_BIT(TXCOEN);
2272                 gem_writel(bp, DMACFG, dmacfg);
2273         }
2274
2275         /* RX checksum offload */
2276         if ((changed & NETIF_F_RXCSUM) && macb_is_gem(bp)) {
2277                 u32 netcfg;
2278
2279                 netcfg = gem_readl(bp, NCFGR);
2280                 if (features & NETIF_F_RXCSUM &&
2281                     !(netdev->flags & IFF_PROMISC))
2282                         netcfg |= GEM_BIT(RXCOEN);
2283                 else
2284                         netcfg &= ~GEM_BIT(RXCOEN);
2285                 gem_writel(bp, NCFGR, netcfg);
2286         }
2287
2288         return 0;
2289 }
2290
2291 static const struct net_device_ops macb_netdev_ops = {
2292         .ndo_open               = macb_open,
2293         .ndo_stop               = macb_close,
2294         .ndo_start_xmit         = macb_start_xmit,
2295         .ndo_set_rx_mode        = macb_set_rx_mode,
2296         .ndo_get_stats          = macb_get_stats,
2297         .ndo_do_ioctl           = macb_ioctl,
2298         .ndo_validate_addr      = eth_validate_addr,
2299         .ndo_change_mtu         = macb_change_mtu,
2300         .ndo_set_mac_address    = eth_mac_addr,
2301 #ifdef CONFIG_NET_POLL_CONTROLLER
2302         .ndo_poll_controller    = macb_poll_controller,
2303 #endif
2304         .ndo_set_features       = macb_set_features,
2305 };
2306
2307 /* Configure peripheral capabilities according to device tree
2308  * and integration options used
2309  */
2310 static void macb_configure_caps(struct macb *bp,
2311                                 const struct macb_config *dt_conf)
2312 {
2313         u32 dcfg;
2314
2315         if (dt_conf)
2316                 bp->caps = dt_conf->caps;
2317
2318         if (hw_is_gem(bp->regs, bp->native_io)) {
2319                 bp->caps |= MACB_CAPS_MACB_IS_GEM;
2320
2321                 dcfg = gem_readl(bp, DCFG1);
2322                 if (GEM_BFEXT(IRQCOR, dcfg) == 0)
2323                         bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
2324                 dcfg = gem_readl(bp, DCFG2);
2325                 if ((dcfg & (GEM_BIT(RX_PKT_BUFF) | GEM_BIT(TX_PKT_BUFF))) == 0)
2326                         bp->caps |= MACB_CAPS_FIFO_MODE;
2327         }
2328
2329         dev_dbg(&bp->pdev->dev, "Cadence caps 0x%08x\n", bp->caps);
2330 }
2331
2332 static void macb_probe_queues(void __iomem *mem,
2333                               bool native_io,
2334                               unsigned int *queue_mask,
2335                               unsigned int *num_queues)
2336 {
2337         unsigned int hw_q;
2338
2339         *queue_mask = 0x1;
2340         *num_queues = 1;
2341
2342         /* is it macb or gem ?
2343          *
2344          * We need to read directly from the hardware here because
2345          * we are early in the probe process and don't have the
2346          * MACB_CAPS_MACB_IS_GEM flag positioned
2347          */
2348         if (!hw_is_gem(mem, native_io))
2349                 return;
2350
2351         /* bit 0 is never set but queue 0 always exists */
2352         *queue_mask = readl_relaxed(mem + GEM_DCFG6) & 0xff;
2353
2354         *queue_mask |= 0x1;
2355
2356         for (hw_q = 1; hw_q < MACB_MAX_QUEUES; ++hw_q)
2357                 if (*queue_mask & (1 << hw_q))
2358                         (*num_queues)++;
2359 }
2360
2361 static int macb_clk_init(struct platform_device *pdev, struct clk **pclk,
2362                          struct clk **hclk, struct clk **tx_clk,
2363                          struct clk **rx_clk)
2364 {
2365         int err;
2366
2367         *pclk = devm_clk_get(&pdev->dev, "pclk");
2368         if (IS_ERR(*pclk)) {
2369                 err = PTR_ERR(*pclk);
2370                 dev_err(&pdev->dev, "failed to get macb_clk (%d)\n", err);
2371                 return err;
2372         }
2373
2374         *hclk = devm_clk_get(&pdev->dev, "hclk");
2375         if (IS_ERR(*hclk)) {
2376                 err = PTR_ERR(*hclk);
2377                 dev_err(&pdev->dev, "failed to get hclk (%d)\n", err);
2378                 return err;
2379         }
2380
2381         *tx_clk = devm_clk_get(&pdev->dev, "tx_clk");
2382         if (IS_ERR(*tx_clk))
2383                 *tx_clk = NULL;
2384
2385         *rx_clk = devm_clk_get(&pdev->dev, "rx_clk");
2386         if (IS_ERR(*rx_clk))
2387                 *rx_clk = NULL;
2388
2389         err = clk_prepare_enable(*pclk);
2390         if (err) {
2391                 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
2392                 return err;
2393         }
2394
2395         err = clk_prepare_enable(*hclk);
2396         if (err) {
2397                 dev_err(&pdev->dev, "failed to enable hclk (%d)\n", err);
2398                 goto err_disable_pclk;
2399         }
2400
2401         err = clk_prepare_enable(*tx_clk);
2402         if (err) {
2403                 dev_err(&pdev->dev, "failed to enable tx_clk (%d)\n", err);
2404                 goto err_disable_hclk;
2405         }
2406
2407         err = clk_prepare_enable(*rx_clk);
2408         if (err) {
2409                 dev_err(&pdev->dev, "failed to enable rx_clk (%d)\n", err);
2410                 goto err_disable_txclk;
2411         }
2412
2413         return 0;
2414
2415 err_disable_txclk:
2416         clk_disable_unprepare(*tx_clk);
2417
2418 err_disable_hclk:
2419         clk_disable_unprepare(*hclk);
2420
2421 err_disable_pclk:
2422         clk_disable_unprepare(*pclk);
2423
2424         return err;
2425 }
2426
2427 static int macb_init(struct platform_device *pdev)
2428 {
2429         struct net_device *dev = platform_get_drvdata(pdev);
2430         unsigned int hw_q, q;
2431         struct macb *bp = netdev_priv(dev);
2432         struct macb_queue *queue;
2433         int err;
2434         u32 val;
2435
2436         /* set the queue register mapping once for all: queue0 has a special
2437          * register mapping but we don't want to test the queue index then
2438          * compute the corresponding register offset at run time.
2439          */
2440         for (hw_q = 0, q = 0; hw_q < MACB_MAX_QUEUES; ++hw_q) {
2441                 if (!(bp->queue_mask & (1 << hw_q)))
2442                         continue;
2443
2444                 queue = &bp->queues[q];
2445                 queue->bp = bp;
2446                 if (hw_q) {
2447                         queue->ISR  = GEM_ISR(hw_q - 1);
2448                         queue->IER  = GEM_IER(hw_q - 1);
2449                         queue->IDR  = GEM_IDR(hw_q - 1);
2450                         queue->IMR  = GEM_IMR(hw_q - 1);
2451                         queue->TBQP = GEM_TBQP(hw_q - 1);
2452 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2453                         queue->TBQPH = GEM_TBQPH(hw_q -1);
2454 #endif
2455                 } else {
2456                         /* queue0 uses legacy registers */
2457                         queue->ISR  = MACB_ISR;
2458                         queue->IER  = MACB_IER;
2459                         queue->IDR  = MACB_IDR;
2460                         queue->IMR  = MACB_IMR;
2461                         queue->TBQP = MACB_TBQP;
2462 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
2463                         queue->TBQPH = MACB_TBQPH;
2464 #endif
2465                 }
2466
2467                 /* get irq: here we use the linux queue index, not the hardware
2468                  * queue index. the queue irq definitions in the device tree
2469                  * must remove the optional gaps that could exist in the
2470                  * hardware queue mask.
2471                  */
2472                 queue->irq = platform_get_irq(pdev, q);
2473                 err = devm_request_irq(&pdev->dev, queue->irq, macb_interrupt,
2474                                        IRQF_SHARED, dev->name, queue);
2475                 if (err) {
2476                         dev_err(&pdev->dev,
2477                                 "Unable to request IRQ %d (error %d)\n",
2478                                 queue->irq, err);
2479                         return err;
2480                 }
2481
2482                 INIT_WORK(&queue->tx_error_task, macb_tx_error_task);
2483                 q++;
2484         }
2485
2486         dev->netdev_ops = &macb_netdev_ops;
2487         netif_napi_add(dev, &bp->napi, macb_poll, 64);
2488
2489         /* setup appropriated routines according to adapter type */
2490         if (macb_is_gem(bp)) {
2491                 bp->max_tx_length = GEM_MAX_TX_LEN;
2492                 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
2493                 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
2494                 bp->macbgem_ops.mog_init_rings = gem_init_rings;
2495                 bp->macbgem_ops.mog_rx = gem_rx;
2496                 dev->ethtool_ops = &gem_ethtool_ops;
2497         } else {
2498                 bp->max_tx_length = MACB_MAX_TX_LEN;
2499                 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
2500                 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
2501                 bp->macbgem_ops.mog_init_rings = macb_init_rings;
2502                 bp->macbgem_ops.mog_rx = macb_rx;
2503                 dev->ethtool_ops = &macb_ethtool_ops;
2504         }
2505
2506         /* Set features */
2507         dev->hw_features = NETIF_F_SG;
2508         /* Checksum offload is only available on gem with packet buffer */
2509         if (macb_is_gem(bp) && !(bp->caps & MACB_CAPS_FIFO_MODE))
2510                 dev->hw_features |= NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
2511         if (bp->caps & MACB_CAPS_SG_DISABLED)
2512                 dev->hw_features &= ~NETIF_F_SG;
2513         dev->features = dev->hw_features;
2514
2515         if (!(bp->caps & MACB_CAPS_USRIO_DISABLED)) {
2516                 val = 0;
2517                 if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
2518                         val = GEM_BIT(RGMII);
2519                 else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII &&
2520                          (bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
2521                         val = MACB_BIT(RMII);
2522                 else if (!(bp->caps & MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII))
2523                         val = MACB_BIT(MII);
2524
2525                 if (bp->caps & MACB_CAPS_USRIO_HAS_CLKEN)
2526                         val |= MACB_BIT(CLKEN);
2527
2528                 macb_or_gem_writel(bp, USRIO, val);
2529         }
2530
2531         /* Set MII management clock divider */
2532         val = macb_mdc_clk_div(bp);
2533         val |= macb_dbw(bp);
2534         if (bp->phy_interface == PHY_INTERFACE_MODE_SGMII)
2535                 val |= GEM_BIT(SGMIIEN) | GEM_BIT(PCSSEL);
2536         macb_writel(bp, NCFGR, val);
2537
2538         return 0;
2539 }
2540
2541 #if defined(CONFIG_OF)
2542 /* 1518 rounded up */
2543 #define AT91ETHER_MAX_RBUFF_SZ  0x600
2544 /* max number of receive buffers */
2545 #define AT91ETHER_MAX_RX_DESCR  9
2546
2547 /* Initialize and start the Receiver and Transmit subsystems */
2548 static int at91ether_start(struct net_device *dev)
2549 {
2550         struct macb *lp = netdev_priv(dev);
2551         dma_addr_t addr;
2552         u32 ctl;
2553         int i;
2554
2555         lp->rx_ring = dma_alloc_coherent(&lp->pdev->dev,
2556                                          (AT91ETHER_MAX_RX_DESCR *
2557                                           sizeof(struct macb_dma_desc)),
2558                                          &lp->rx_ring_dma, GFP_KERNEL);
2559         if (!lp->rx_ring)
2560                 return -ENOMEM;
2561
2562         lp->rx_buffers = dma_alloc_coherent(&lp->pdev->dev,
2563                                             AT91ETHER_MAX_RX_DESCR *
2564                                             AT91ETHER_MAX_RBUFF_SZ,
2565                                             &lp->rx_buffers_dma, GFP_KERNEL);
2566         if (!lp->rx_buffers) {
2567                 dma_free_coherent(&lp->pdev->dev,
2568                                   AT91ETHER_MAX_RX_DESCR *
2569                                   sizeof(struct macb_dma_desc),
2570                                   lp->rx_ring, lp->rx_ring_dma);
2571                 lp->rx_ring = NULL;
2572                 return -ENOMEM;
2573         }
2574
2575         addr = lp->rx_buffers_dma;
2576         for (i = 0; i < AT91ETHER_MAX_RX_DESCR; i++) {
2577                 lp->rx_ring[i].addr = addr;
2578                 lp->rx_ring[i].ctrl = 0;
2579                 addr += AT91ETHER_MAX_RBUFF_SZ;
2580         }
2581
2582         /* Set the Wrap bit on the last descriptor */
2583         lp->rx_ring[AT91ETHER_MAX_RX_DESCR - 1].addr |= MACB_BIT(RX_WRAP);
2584
2585         /* Reset buffer index */
2586         lp->rx_tail = 0;
2587
2588         /* Program address of descriptor list in Rx Buffer Queue register */
2589         macb_writel(lp, RBQP, lp->rx_ring_dma);
2590
2591         /* Enable Receive and Transmit */
2592         ctl = macb_readl(lp, NCR);
2593         macb_writel(lp, NCR, ctl | MACB_BIT(RE) | MACB_BIT(TE));
2594
2595         return 0;
2596 }
2597
2598 /* Open the ethernet interface */
2599 static int at91ether_open(struct net_device *dev)
2600 {
2601         struct macb *lp = netdev_priv(dev);
2602         u32 ctl;
2603         int ret;
2604
2605         /* Clear internal statistics */
2606         ctl = macb_readl(lp, NCR);
2607         macb_writel(lp, NCR, ctl | MACB_BIT(CLRSTAT));
2608
2609         macb_set_hwaddr(lp);
2610
2611         ret = at91ether_start(dev);
2612         if (ret)
2613                 return ret;
2614
2615         /* Enable MAC interrupts */
2616         macb_writel(lp, IER, MACB_BIT(RCOMP)    |
2617                              MACB_BIT(RXUBR)    |
2618                              MACB_BIT(ISR_TUND) |
2619                              MACB_BIT(ISR_RLE)  |
2620                              MACB_BIT(TCOMP)    |
2621                              MACB_BIT(ISR_ROVR) |
2622                              MACB_BIT(HRESP));
2623
2624         /* schedule a link state check */
2625         phy_start(dev->phydev);
2626
2627         netif_start_queue(dev);
2628
2629         return 0;
2630 }
2631
2632 /* Close the interface */
2633 static int at91ether_close(struct net_device *dev)
2634 {
2635         struct macb *lp = netdev_priv(dev);
2636         u32 ctl;
2637
2638         /* Disable Receiver and Transmitter */
2639         ctl = macb_readl(lp, NCR);
2640         macb_writel(lp, NCR, ctl & ~(MACB_BIT(TE) | MACB_BIT(RE)));
2641
2642         /* Disable MAC interrupts */
2643         macb_writel(lp, IDR, MACB_BIT(RCOMP)    |
2644                              MACB_BIT(RXUBR)    |
2645                              MACB_BIT(ISR_TUND) |
2646                              MACB_BIT(ISR_RLE)  |
2647                              MACB_BIT(TCOMP)    |
2648                              MACB_BIT(ISR_ROVR) |
2649                              MACB_BIT(HRESP));
2650
2651         netif_stop_queue(dev);
2652
2653         dma_free_coherent(&lp->pdev->dev,
2654                           AT91ETHER_MAX_RX_DESCR *
2655                           sizeof(struct macb_dma_desc),
2656                           lp->rx_ring, lp->rx_ring_dma);
2657         lp->rx_ring = NULL;
2658
2659         dma_free_coherent(&lp->pdev->dev,
2660                           AT91ETHER_MAX_RX_DESCR * AT91ETHER_MAX_RBUFF_SZ,
2661                           lp->rx_buffers, lp->rx_buffers_dma);
2662         lp->rx_buffers = NULL;
2663
2664         return 0;
2665 }
2666
2667 /* Transmit packet */
2668 static int at91ether_start_xmit(struct sk_buff *skb, struct net_device *dev)
2669 {
2670         struct macb *lp = netdev_priv(dev);
2671
2672         if (macb_readl(lp, TSR) & MACB_BIT(RM9200_BNQ)) {
2673                 netif_stop_queue(dev);
2674
2675                 /* Store packet information (to free when Tx completed) */
2676                 lp->skb = skb;
2677                 lp->skb_length = skb->len;
2678                 lp->skb_physaddr = dma_map_single(NULL, skb->data, skb->len,
2679                                                         DMA_TO_DEVICE);
2680                 if (dma_mapping_error(NULL, lp->skb_physaddr)) {
2681                         dev_kfree_skb_any(skb);
2682                         dev->stats.tx_dropped++;
2683                         netdev_err(dev, "%s: DMA mapping error\n", __func__);
2684                         return NETDEV_TX_OK;
2685                 }
2686
2687                 /* Set address of the data in the Transmit Address register */
2688                 macb_writel(lp, TAR, lp->skb_physaddr);
2689                 /* Set length of the packet in the Transmit Control register */
2690                 macb_writel(lp, TCR, skb->len);
2691
2692         } else {
2693                 netdev_err(dev, "%s called, but device is busy!\n", __func__);
2694                 return NETDEV_TX_BUSY;
2695         }
2696
2697         return NETDEV_TX_OK;
2698 }
2699
2700 /* Extract received frame from buffer descriptors and sent to upper layers.
2701  * (Called from interrupt context)
2702  */
2703 static void at91ether_rx(struct net_device *dev)
2704 {
2705         struct macb *lp = netdev_priv(dev);
2706         unsigned char *p_recv;
2707         struct sk_buff *skb;
2708         unsigned int pktlen;
2709
2710         while (lp->rx_ring[lp->rx_tail].addr & MACB_BIT(RX_USED)) {
2711                 p_recv = lp->rx_buffers + lp->rx_tail * AT91ETHER_MAX_RBUFF_SZ;
2712                 pktlen = MACB_BF(RX_FRMLEN, lp->rx_ring[lp->rx_tail].ctrl);
2713                 skb = netdev_alloc_skb(dev, pktlen + 2);
2714                 if (skb) {
2715                         skb_reserve(skb, 2);
2716                         memcpy(skb_put(skb, pktlen), p_recv, pktlen);
2717
2718                         skb->protocol = eth_type_trans(skb, dev);
2719                         lp->stats.rx_packets++;
2720                         lp->stats.rx_bytes += pktlen;
2721                         netif_rx(skb);
2722                 } else {
2723                         lp->stats.rx_dropped++;
2724                 }
2725
2726                 if (lp->rx_ring[lp->rx_tail].ctrl & MACB_BIT(RX_MHASH_MATCH))
2727                         lp->stats.multicast++;
2728
2729                 /* reset ownership bit */
2730                 lp->rx_ring[lp->rx_tail].addr &= ~MACB_BIT(RX_USED);
2731
2732                 /* wrap after last buffer */
2733                 if (lp->rx_tail == AT91ETHER_MAX_RX_DESCR - 1)
2734                         lp->rx_tail = 0;
2735                 else
2736                         lp->rx_tail++;
2737         }
2738 }
2739
2740 /* MAC interrupt handler */
2741 static irqreturn_t at91ether_interrupt(int irq, void *dev_id)
2742 {
2743         struct net_device *dev = dev_id;
2744         struct macb *lp = netdev_priv(dev);
2745         u32 intstatus, ctl;
2746
2747         /* MAC Interrupt Status register indicates what interrupts are pending.
2748          * It is automatically cleared once read.
2749          */
2750         intstatus = macb_readl(lp, ISR);
2751
2752         /* Receive complete */
2753         if (intstatus & MACB_BIT(RCOMP))
2754                 at91ether_rx(dev);
2755
2756         /* Transmit complete */
2757         if (intstatus & MACB_BIT(TCOMP)) {
2758                 /* The TCOM bit is set even if the transmission failed */
2759                 if (intstatus & (MACB_BIT(ISR_TUND) | MACB_BIT(ISR_RLE)))
2760                         lp->stats.tx_errors++;
2761
2762                 if (lp->skb) {
2763                         dev_kfree_skb_irq(lp->skb);
2764                         lp->skb = NULL;
2765                         dma_unmap_single(NULL, lp->skb_physaddr,
2766                                          lp->skb_length, DMA_TO_DEVICE);
2767                         lp->stats.tx_packets++;
2768                         lp->stats.tx_bytes += lp->skb_length;
2769                 }
2770                 netif_wake_queue(dev);
2771         }
2772
2773         /* Work-around for EMAC Errata section 41.3.1 */
2774         if (intstatus & MACB_BIT(RXUBR)) {
2775                 ctl = macb_readl(lp, NCR);
2776                 macb_writel(lp, NCR, ctl & ~MACB_BIT(RE));
2777                 wmb();
2778                 macb_writel(lp, NCR, ctl | MACB_BIT(RE));
2779         }
2780
2781         if (intstatus & MACB_BIT(ISR_ROVR))
2782                 netdev_err(dev, "ROVR error\n");
2783
2784         return IRQ_HANDLED;
2785 }
2786
2787 #ifdef CONFIG_NET_POLL_CONTROLLER
2788 static void at91ether_poll_controller(struct net_device *dev)
2789 {
2790         unsigned long flags;
2791
2792         local_irq_save(flags);
2793         at91ether_interrupt(dev->irq, dev);
2794         local_irq_restore(flags);
2795 }
2796 #endif
2797
2798 static const struct net_device_ops at91ether_netdev_ops = {
2799         .ndo_open               = at91ether_open,
2800         .ndo_stop               = at91ether_close,
2801         .ndo_start_xmit         = at91ether_start_xmit,
2802         .ndo_get_stats          = macb_get_stats,
2803         .ndo_set_rx_mode        = macb_set_rx_mode,
2804         .ndo_set_mac_address    = eth_mac_addr,
2805         .ndo_do_ioctl           = macb_ioctl,
2806         .ndo_validate_addr      = eth_validate_addr,
2807         .ndo_change_mtu         = eth_change_mtu,
2808 #ifdef CONFIG_NET_POLL_CONTROLLER
2809         .ndo_poll_controller    = at91ether_poll_controller,
2810 #endif
2811 };
2812
2813 static int at91ether_clk_init(struct platform_device *pdev, struct clk **pclk,
2814                               struct clk **hclk, struct clk **tx_clk,
2815                               struct clk **rx_clk)
2816 {
2817         int err;
2818
2819         *hclk = NULL;
2820         *tx_clk = NULL;
2821         *rx_clk = NULL;
2822
2823         *pclk = devm_clk_get(&pdev->dev, "ether_clk");
2824         if (IS_ERR(*pclk))
2825                 return PTR_ERR(*pclk);
2826
2827         err = clk_prepare_enable(*pclk);
2828         if (err) {
2829                 dev_err(&pdev->dev, "failed to enable pclk (%d)\n", err);
2830                 return err;
2831         }
2832
2833         return 0;
2834 }
2835
2836 static int at91ether_init(struct platform_device *pdev)
2837 {
2838         struct net_device *dev = platform_get_drvdata(pdev);
2839         struct macb *bp = netdev_priv(dev);
2840         int err;
2841         u32 reg;
2842
2843         dev->netdev_ops = &at91ether_netdev_ops;
2844         dev->ethtool_ops = &macb_ethtool_ops;
2845
2846         err = devm_request_irq(&pdev->dev, dev->irq, at91ether_interrupt,
2847                                0, dev->name, dev);
2848         if (err)
2849                 return err;
2850
2851         macb_writel(bp, NCR, 0);
2852
2853         reg = MACB_BF(CLK, MACB_CLK_DIV32) | MACB_BIT(BIG);
2854         if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
2855                 reg |= MACB_BIT(RM9200_RMII);
2856
2857         macb_writel(bp, NCFGR, reg);
2858
2859         return 0;
2860 }
2861
2862 static const struct macb_config at91sam9260_config = {
2863         .caps = MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
2864         .clk_init = macb_clk_init,
2865         .init = macb_init,
2866 };
2867
2868 static const struct macb_config sama5d3macb_config = {
2869         .caps = MACB_CAPS_SG_DISABLED
2870               | MACB_CAPS_USRIO_HAS_CLKEN | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
2871         .clk_init = macb_clk_init,
2872         .init = macb_init,
2873 };
2874
2875 static const struct macb_config pc302gem_config = {
2876         .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE,
2877         .dma_burst_length = 16,
2878         .clk_init = macb_clk_init,
2879         .init = macb_init,
2880 };
2881
2882 static const struct macb_config sama5d2_config = {
2883         .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
2884         .dma_burst_length = 16,
2885         .clk_init = macb_clk_init,
2886         .init = macb_init,
2887 };
2888
2889 static const struct macb_config sama5d3_config = {
2890         .caps = MACB_CAPS_SG_DISABLED | MACB_CAPS_GIGABIT_MODE_AVAILABLE
2891               | MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
2892         .dma_burst_length = 16,
2893         .clk_init = macb_clk_init,
2894         .init = macb_init,
2895 };
2896
2897 static const struct macb_config sama5d4_config = {
2898         .caps = MACB_CAPS_USRIO_DEFAULT_IS_MII_GMII,
2899         .dma_burst_length = 4,
2900         .clk_init = macb_clk_init,
2901         .init = macb_init,
2902 };
2903
2904 static const struct macb_config emac_config = {
2905         .clk_init = at91ether_clk_init,
2906         .init = at91ether_init,
2907 };
2908
2909 static const struct macb_config np4_config = {
2910         .caps = MACB_CAPS_USRIO_DISABLED,
2911         .clk_init = macb_clk_init,
2912         .init = macb_init,
2913 };
2914
2915 static const struct macb_config zynqmp_config = {
2916         .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_JUMBO,
2917         .dma_burst_length = 16,
2918         .clk_init = macb_clk_init,
2919         .init = macb_init,
2920         .jumbo_max_len = 10240,
2921 };
2922
2923 static const struct macb_config zynq_config = {
2924         .caps = MACB_CAPS_GIGABIT_MODE_AVAILABLE | MACB_CAPS_NO_GIGABIT_HALF,
2925         .dma_burst_length = 16,
2926         .clk_init = macb_clk_init,
2927         .init = macb_init,
2928 };
2929
2930 static const struct of_device_id macb_dt_ids[] = {
2931         { .compatible = "cdns,at32ap7000-macb" },
2932         { .compatible = "cdns,at91sam9260-macb", .data = &at91sam9260_config },
2933         { .compatible = "cdns,macb" },
2934         { .compatible = "cdns,np4-macb", .data = &np4_config },
2935         { .compatible = "cdns,pc302-gem", .data = &pc302gem_config },
2936         { .compatible = "cdns,gem", .data = &pc302gem_config },
2937         { .compatible = "atmel,sama5d2-gem", .data = &sama5d2_config },
2938         { .compatible = "atmel,sama5d3-gem", .data = &sama5d3_config },
2939         { .compatible = "atmel,sama5d3-macb", .data = &sama5d3macb_config },
2940         { .compatible = "atmel,sama5d4-gem", .data = &sama5d4_config },
2941         { .compatible = "cdns,at91rm9200-emac", .data = &emac_config },
2942         { .compatible = "cdns,emac", .data = &emac_config },
2943         { .compatible = "cdns,zynqmp-gem", .data = &zynqmp_config},
2944         { .compatible = "cdns,zynq-gem", .data = &zynq_config },
2945         { /* sentinel */ }
2946 };
2947 MODULE_DEVICE_TABLE(of, macb_dt_ids);
2948 #endif /* CONFIG_OF */
2949
2950 static int macb_probe(struct platform_device *pdev)
2951 {
2952         int (*clk_init)(struct platform_device *, struct clk **,
2953                         struct clk **, struct clk **,  struct clk **)
2954                                               = macb_clk_init;
2955         int (*init)(struct platform_device *) = macb_init;
2956         struct device_node *np = pdev->dev.of_node;
2957         struct device_node *phy_node;
2958         const struct macb_config *macb_config = NULL;
2959         struct clk *pclk, *hclk = NULL, *tx_clk = NULL, *rx_clk = NULL;
2960         unsigned int queue_mask, num_queues;
2961         struct macb_platform_data *pdata;
2962         bool native_io;
2963         struct phy_device *phydev;
2964         struct net_device *dev;
2965         struct resource *regs;
2966         void __iomem *mem;
2967         const char *mac;
2968         struct macb *bp;
2969         int err;
2970
2971         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2972         mem = devm_ioremap_resource(&pdev->dev, regs);
2973         if (IS_ERR(mem))
2974                 return PTR_ERR(mem);
2975
2976         if (np) {
2977                 const struct of_device_id *match;
2978
2979                 match = of_match_node(macb_dt_ids, np);
2980                 if (match && match->data) {
2981                         macb_config = match->data;
2982                         clk_init = macb_config->clk_init;
2983                         init = macb_config->init;
2984                 }
2985         }
2986
2987         err = clk_init(pdev, &pclk, &hclk, &tx_clk, &rx_clk);
2988         if (err)
2989                 return err;
2990
2991         native_io = hw_is_native_io(mem);
2992
2993         macb_probe_queues(mem, native_io, &queue_mask, &num_queues);
2994         dev = alloc_etherdev_mq(sizeof(*bp), num_queues);
2995         if (!dev) {
2996                 err = -ENOMEM;
2997                 goto err_disable_clocks;
2998         }
2999
3000         dev->base_addr = regs->start;
3001
3002         SET_NETDEV_DEV(dev, &pdev->dev);
3003
3004         bp = netdev_priv(dev);
3005         bp->pdev = pdev;
3006         bp->dev = dev;
3007         bp->regs = mem;
3008         bp->native_io = native_io;
3009         if (native_io) {
3010                 bp->macb_reg_readl = hw_readl_native;
3011                 bp->macb_reg_writel = hw_writel_native;
3012         } else {
3013                 bp->macb_reg_readl = hw_readl;
3014                 bp->macb_reg_writel = hw_writel;
3015         }
3016         bp->num_queues = num_queues;
3017         bp->queue_mask = queue_mask;
3018         if (macb_config)
3019                 bp->dma_burst_length = macb_config->dma_burst_length;
3020         bp->pclk = pclk;
3021         bp->hclk = hclk;
3022         bp->tx_clk = tx_clk;
3023         bp->rx_clk = rx_clk;
3024         if (macb_config)
3025                 bp->jumbo_max_len = macb_config->jumbo_max_len;
3026
3027         bp->wol = 0;
3028         if (of_get_property(np, "magic-packet", NULL))
3029                 bp->wol |= MACB_WOL_HAS_MAGIC_PACKET;
3030         device_set_wakeup_capable(&pdev->dev, bp->wol & MACB_WOL_HAS_MAGIC_PACKET);
3031
3032 #ifdef CONFIG_ARCH_DMA_ADDR_T_64BIT
3033         if (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1)) > GEM_DBW32)
3034                 dma_set_mask(&pdev->dev, DMA_BIT_MASK(44));
3035 #endif
3036
3037         spin_lock_init(&bp->lock);
3038
3039         /* setup capabilities */
3040         macb_configure_caps(bp, macb_config);
3041
3042         platform_set_drvdata(pdev, dev);
3043
3044         dev->irq = platform_get_irq(pdev, 0);
3045         if (dev->irq < 0) {
3046                 err = dev->irq;
3047                 goto err_out_free_netdev;
3048         }
3049
3050         mac = of_get_mac_address(np);
3051         if (mac)
3052                 ether_addr_copy(bp->dev->dev_addr, mac);
3053         else
3054                 macb_get_hwaddr(bp);
3055
3056         /* Power up the PHY if there is a GPIO reset */
3057         phy_node =  of_get_next_available_child(np, NULL);
3058         if (phy_node) {
3059                 int gpio = of_get_named_gpio(phy_node, "reset-gpios", 0);
3060
3061                 if (gpio_is_valid(gpio)) {
3062                         bp->reset_gpio = gpio_to_desc(gpio);
3063                         gpiod_direction_output(bp->reset_gpio, 1);
3064                 }
3065         }
3066         of_node_put(phy_node);
3067
3068         err = of_get_phy_mode(np);
3069         if (err < 0) {
3070                 pdata = dev_get_platdata(&pdev->dev);
3071                 if (pdata && pdata->is_rmii)
3072                         bp->phy_interface = PHY_INTERFACE_MODE_RMII;
3073                 else
3074                         bp->phy_interface = PHY_INTERFACE_MODE_MII;
3075         } else {
3076                 bp->phy_interface = err;
3077         }
3078
3079         /* IP specific init */
3080         err = init(pdev);
3081         if (err)
3082                 goto err_out_free_netdev;
3083
3084         err = macb_mii_init(bp);
3085         if (err)
3086                 goto err_out_free_netdev;
3087
3088         phydev = dev->phydev;
3089
3090         netif_carrier_off(dev);
3091
3092         err = register_netdev(dev);
3093         if (err) {
3094                 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
3095                 goto err_out_unregister_mdio;
3096         }
3097
3098         phy_attached_info(phydev);
3099
3100         netdev_info(dev, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
3101                     macb_is_gem(bp) ? "GEM" : "MACB", macb_readl(bp, MID),
3102                     dev->base_addr, dev->irq, dev->dev_addr);
3103
3104         return 0;
3105
3106 err_out_unregister_mdio:
3107         phy_disconnect(dev->phydev);
3108         mdiobus_unregister(bp->mii_bus);
3109         mdiobus_free(bp->mii_bus);
3110
3111         /* Shutdown the PHY if there is a GPIO reset */
3112         if (bp->reset_gpio)
3113                 gpiod_set_value(bp->reset_gpio, 0);
3114
3115 err_out_free_netdev:
3116         free_netdev(dev);
3117
3118 err_disable_clocks:
3119         clk_disable_unprepare(tx_clk);
3120         clk_disable_unprepare(hclk);
3121         clk_disable_unprepare(pclk);
3122         clk_disable_unprepare(rx_clk);
3123
3124         return err;
3125 }
3126
3127 static int macb_remove(struct platform_device *pdev)
3128 {
3129         struct net_device *dev;
3130         struct macb *bp;
3131
3132         dev = platform_get_drvdata(pdev);
3133
3134         if (dev) {
3135                 bp = netdev_priv(dev);
3136                 if (dev->phydev)
3137                         phy_disconnect(dev->phydev);
3138                 mdiobus_unregister(bp->mii_bus);
3139                 dev->phydev = NULL;
3140                 mdiobus_free(bp->mii_bus);
3141
3142                 /* Shutdown the PHY if there is a GPIO reset */
3143                 if (bp->reset_gpio)
3144                         gpiod_set_value(bp->reset_gpio, 0);
3145
3146                 unregister_netdev(dev);
3147                 clk_disable_unprepare(bp->tx_clk);
3148                 clk_disable_unprepare(bp->hclk);
3149                 clk_disable_unprepare(bp->pclk);
3150                 clk_disable_unprepare(bp->rx_clk);
3151                 free_netdev(dev);
3152         }
3153
3154         return 0;
3155 }
3156
3157 static int __maybe_unused macb_suspend(struct device *dev)
3158 {
3159         struct platform_device *pdev = to_platform_device(dev);
3160         struct net_device *netdev = platform_get_drvdata(pdev);
3161         struct macb *bp = netdev_priv(netdev);
3162
3163         netif_carrier_off(netdev);
3164         netif_device_detach(netdev);
3165
3166         if (bp->wol & MACB_WOL_ENABLED) {
3167                 macb_writel(bp, IER, MACB_BIT(WOL));
3168                 macb_writel(bp, WOL, MACB_BIT(MAG));
3169                 enable_irq_wake(bp->queues[0].irq);
3170         } else {
3171                 clk_disable_unprepare(bp->tx_clk);
3172                 clk_disable_unprepare(bp->hclk);
3173                 clk_disable_unprepare(bp->pclk);
3174                 clk_disable_unprepare(bp->rx_clk);
3175         }
3176
3177         return 0;
3178 }
3179
3180 static int __maybe_unused macb_resume(struct device *dev)
3181 {
3182         struct platform_device *pdev = to_platform_device(dev);
3183         struct net_device *netdev = platform_get_drvdata(pdev);
3184         struct macb *bp = netdev_priv(netdev);
3185
3186         if (bp->wol & MACB_WOL_ENABLED) {
3187                 macb_writel(bp, IDR, MACB_BIT(WOL));
3188                 macb_writel(bp, WOL, 0);
3189                 disable_irq_wake(bp->queues[0].irq);
3190         } else {
3191                 clk_prepare_enable(bp->pclk);
3192                 clk_prepare_enable(bp->hclk);
3193                 clk_prepare_enable(bp->tx_clk);
3194                 clk_prepare_enable(bp->rx_clk);
3195         }
3196
3197         netif_device_attach(netdev);
3198
3199         return 0;
3200 }
3201
3202 static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
3203
3204 static struct platform_driver macb_driver = {
3205         .probe          = macb_probe,
3206         .remove         = macb_remove,
3207         .driver         = {
3208                 .name           = "macb",
3209                 .of_match_table = of_match_ptr(macb_dt_ids),
3210                 .pm     = &macb_pm_ops,
3211         },
3212 };
3213
3214 module_platform_driver(macb_driver);
3215
3216 MODULE_LICENSE("GPL");
3217 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
3218 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
3219 MODULE_ALIAS("platform:macb");