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