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