436aecc317327b95b23e530beac9c5ea01f04417
[linux-drm-fsl-dcu.git] / drivers / net / ethernet / cadence / macb.c
1 /*
2  * Cadence MACB/GEM Ethernet Controller driver
3  *
4  * Copyright (C) 2004-2006 Atmel Corporation
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/circ_buf.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
20 #include <linux/io.h>
21 #include <linux/gpio.h>
22 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_data/macb.h>
27 #include <linux/platform_device.h>
28 #include <linux/phy.h>
29 #include <linux/of.h>
30 #include <linux/of_device.h>
31 #include <linux/of_mdio.h>
32 #include <linux/of_net.h>
33 #include <linux/pinctrl/consumer.h>
34
35 #include "macb.h"
36
37 #define MACB_RX_BUFFER_SIZE     128
38 #define RX_BUFFER_MULTIPLE      64  /* bytes */
39 #define RX_RING_SIZE            512 /* must be power of 2 */
40 #define RX_RING_BYTES           (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
41
42 #define TX_RING_SIZE            128 /* must be power of 2 */
43 #define TX_RING_BYTES           (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
44
45 /* level of occupied TX descriptors under which we wake up TX process */
46 #define MACB_TX_WAKEUP_THRESH   (3 * TX_RING_SIZE / 4)
47
48 #define MACB_RX_INT_FLAGS       (MACB_BIT(RCOMP) | MACB_BIT(RXUBR)      \
49                                  | MACB_BIT(ISR_ROVR))
50 #define MACB_TX_ERR_FLAGS       (MACB_BIT(ISR_TUND)                     \
51                                         | MACB_BIT(ISR_RLE)             \
52                                         | MACB_BIT(TXERR))
53 #define MACB_TX_INT_FLAGS       (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
54
55 /*
56  * Graceful stop timeouts in us. We should allow up to
57  * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
58  */
59 #define MACB_HALT_TIMEOUT       1230
60
61 /* Ring buffer accessors */
62 static unsigned int macb_tx_ring_wrap(unsigned int index)
63 {
64         return index & (TX_RING_SIZE - 1);
65 }
66
67 static struct macb_dma_desc *macb_tx_desc(struct macb *bp, unsigned int index)
68 {
69         return &bp->tx_ring[macb_tx_ring_wrap(index)];
70 }
71
72 static struct macb_tx_skb *macb_tx_skb(struct macb *bp, unsigned int index)
73 {
74         return &bp->tx_skb[macb_tx_ring_wrap(index)];
75 }
76
77 static dma_addr_t macb_tx_dma(struct macb *bp, unsigned int index)
78 {
79         dma_addr_t offset;
80
81         offset = macb_tx_ring_wrap(index) * sizeof(struct macb_dma_desc);
82
83         return bp->tx_ring_dma + offset;
84 }
85
86 static unsigned int macb_rx_ring_wrap(unsigned int index)
87 {
88         return index & (RX_RING_SIZE - 1);
89 }
90
91 static struct macb_dma_desc *macb_rx_desc(struct macb *bp, unsigned int index)
92 {
93         return &bp->rx_ring[macb_rx_ring_wrap(index)];
94 }
95
96 static void *macb_rx_buffer(struct macb *bp, unsigned int index)
97 {
98         return bp->rx_buffers + bp->rx_buffer_size * macb_rx_ring_wrap(index);
99 }
100
101 void macb_set_hwaddr(struct macb *bp)
102 {
103         u32 bottom;
104         u16 top;
105
106         bottom = cpu_to_le32(*((u32 *)bp->dev->dev_addr));
107         macb_or_gem_writel(bp, SA1B, bottom);
108         top = cpu_to_le16(*((u16 *)(bp->dev->dev_addr + 4)));
109         macb_or_gem_writel(bp, SA1T, top);
110
111         /* Clear unused address register sets */
112         macb_or_gem_writel(bp, SA2B, 0);
113         macb_or_gem_writel(bp, SA2T, 0);
114         macb_or_gem_writel(bp, SA3B, 0);
115         macb_or_gem_writel(bp, SA3T, 0);
116         macb_or_gem_writel(bp, SA4B, 0);
117         macb_or_gem_writel(bp, SA4T, 0);
118 }
119 EXPORT_SYMBOL_GPL(macb_set_hwaddr);
120
121 void macb_get_hwaddr(struct macb *bp)
122 {
123         struct macb_platform_data *pdata;
124         u32 bottom;
125         u16 top;
126         u8 addr[6];
127         int i;
128
129         pdata = dev_get_platdata(&bp->pdev->dev);
130
131         /* Check all 4 address register for vaild address */
132         for (i = 0; i < 4; i++) {
133                 bottom = macb_or_gem_readl(bp, SA1B + i * 8);
134                 top = macb_or_gem_readl(bp, SA1T + i * 8);
135
136                 if (pdata && pdata->rev_eth_addr) {
137                         addr[5] = bottom & 0xff;
138                         addr[4] = (bottom >> 8) & 0xff;
139                         addr[3] = (bottom >> 16) & 0xff;
140                         addr[2] = (bottom >> 24) & 0xff;
141                         addr[1] = top & 0xff;
142                         addr[0] = (top & 0xff00) >> 8;
143                 } else {
144                         addr[0] = bottom & 0xff;
145                         addr[1] = (bottom >> 8) & 0xff;
146                         addr[2] = (bottom >> 16) & 0xff;
147                         addr[3] = (bottom >> 24) & 0xff;
148                         addr[4] = top & 0xff;
149                         addr[5] = (top >> 8) & 0xff;
150                 }
151
152                 if (is_valid_ether_addr(addr)) {
153                         memcpy(bp->dev->dev_addr, addr, sizeof(addr));
154                         return;
155                 }
156         }
157
158         netdev_info(bp->dev, "invalid hw address, using random\n");
159         eth_hw_addr_random(bp->dev);
160 }
161 EXPORT_SYMBOL_GPL(macb_get_hwaddr);
162
163 static int macb_mdio_read(struct mii_bus *bus, int mii_id, int regnum)
164 {
165         struct macb *bp = bus->priv;
166         int value;
167
168         macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
169                               | MACB_BF(RW, MACB_MAN_READ)
170                               | MACB_BF(PHYA, mii_id)
171                               | MACB_BF(REGA, regnum)
172                               | MACB_BF(CODE, MACB_MAN_CODE)));
173
174         /* wait for end of transfer */
175         while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
176                 cpu_relax();
177
178         value = MACB_BFEXT(DATA, macb_readl(bp, MAN));
179
180         return value;
181 }
182
183 static int macb_mdio_write(struct mii_bus *bus, int mii_id, int regnum,
184                            u16 value)
185 {
186         struct macb *bp = bus->priv;
187
188         macb_writel(bp, MAN, (MACB_BF(SOF, MACB_MAN_SOF)
189                               | MACB_BF(RW, MACB_MAN_WRITE)
190                               | MACB_BF(PHYA, mii_id)
191                               | MACB_BF(REGA, regnum)
192                               | MACB_BF(CODE, MACB_MAN_CODE)
193                               | MACB_BF(DATA, value)));
194
195         /* wait for end of transfer */
196         while (!MACB_BFEXT(IDLE, macb_readl(bp, NSR)))
197                 cpu_relax();
198
199         return 0;
200 }
201
202 static int macb_mdio_reset(struct mii_bus *bus)
203 {
204         return 0;
205 }
206
207 static void macb_handle_link_change(struct net_device *dev)
208 {
209         struct macb *bp = netdev_priv(dev);
210         struct phy_device *phydev = bp->phy_dev;
211         unsigned long flags;
212
213         int status_change = 0;
214
215         spin_lock_irqsave(&bp->lock, flags);
216
217         if (phydev->link) {
218                 if ((bp->speed != phydev->speed) ||
219                     (bp->duplex != phydev->duplex)) {
220                         u32 reg;
221
222                         reg = macb_readl(bp, NCFGR);
223                         reg &= ~(MACB_BIT(SPD) | MACB_BIT(FD));
224                         if (macb_is_gem(bp))
225                                 reg &= ~GEM_BIT(GBE);
226
227                         if (phydev->duplex)
228                                 reg |= MACB_BIT(FD);
229                         if (phydev->speed == SPEED_100)
230                                 reg |= MACB_BIT(SPD);
231                         if (phydev->speed == SPEED_1000)
232                                 reg |= GEM_BIT(GBE);
233
234                         macb_or_gem_writel(bp, NCFGR, reg);
235
236                         bp->speed = phydev->speed;
237                         bp->duplex = phydev->duplex;
238                         status_change = 1;
239                 }
240         }
241
242         if (phydev->link != bp->link) {
243                 if (!phydev->link) {
244                         bp->speed = 0;
245                         bp->duplex = -1;
246                 }
247                 bp->link = phydev->link;
248
249                 status_change = 1;
250         }
251
252         spin_unlock_irqrestore(&bp->lock, flags);
253
254         if (status_change) {
255                 if (phydev->link) {
256                         netif_carrier_on(dev);
257                         netdev_info(dev, "link up (%d/%s)\n",
258                                     phydev->speed,
259                                     phydev->duplex == DUPLEX_FULL ?
260                                     "Full" : "Half");
261                 } else {
262                         netif_carrier_off(dev);
263                         netdev_info(dev, "link down\n");
264                 }
265         }
266 }
267
268 /* based on au1000_eth. c*/
269 static int macb_mii_probe(struct net_device *dev)
270 {
271         struct macb *bp = netdev_priv(dev);
272         struct macb_platform_data *pdata;
273         struct phy_device *phydev;
274         int phy_irq;
275         int ret;
276
277         phydev = phy_find_first(bp->mii_bus);
278         if (!phydev) {
279                 netdev_err(dev, "no PHY found\n");
280                 return -ENXIO;
281         }
282
283         pdata = dev_get_platdata(&bp->pdev->dev);
284         if (pdata && gpio_is_valid(pdata->phy_irq_pin)) {
285                 ret = devm_gpio_request(&bp->pdev->dev, pdata->phy_irq_pin, "phy int");
286                 if (!ret) {
287                         phy_irq = gpio_to_irq(pdata->phy_irq_pin);
288                         phydev->irq = (phy_irq < 0) ? PHY_POLL : phy_irq;
289                 }
290         }
291
292         /* attach the mac to the phy */
293         ret = phy_connect_direct(dev, phydev, &macb_handle_link_change,
294                                  bp->phy_interface);
295         if (ret) {
296                 netdev_err(dev, "Could not attach to PHY\n");
297                 return ret;
298         }
299
300         /* mask with MAC supported features */
301         if (macb_is_gem(bp))
302                 phydev->supported &= PHY_GBIT_FEATURES;
303         else
304                 phydev->supported &= PHY_BASIC_FEATURES;
305
306         phydev->advertising = phydev->supported;
307
308         bp->link = 0;
309         bp->speed = 0;
310         bp->duplex = -1;
311         bp->phy_dev = phydev;
312
313         return 0;
314 }
315
316 int macb_mii_init(struct macb *bp)
317 {
318         struct macb_platform_data *pdata;
319         struct device_node *np;
320         int err = -ENXIO, i;
321
322         /* Enable management port */
323         macb_writel(bp, NCR, MACB_BIT(MPE));
324
325         bp->mii_bus = mdiobus_alloc();
326         if (bp->mii_bus == NULL) {
327                 err = -ENOMEM;
328                 goto err_out;
329         }
330
331         bp->mii_bus->name = "MACB_mii_bus";
332         bp->mii_bus->read = &macb_mdio_read;
333         bp->mii_bus->write = &macb_mdio_write;
334         bp->mii_bus->reset = &macb_mdio_reset;
335         snprintf(bp->mii_bus->id, MII_BUS_ID_SIZE, "%s-%x",
336                 bp->pdev->name, bp->pdev->id);
337         bp->mii_bus->priv = bp;
338         bp->mii_bus->parent = &bp->dev->dev;
339         pdata = dev_get_platdata(&bp->pdev->dev);
340
341         bp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
342         if (!bp->mii_bus->irq) {
343                 err = -ENOMEM;
344                 goto err_out_free_mdiobus;
345         }
346
347         dev_set_drvdata(&bp->dev->dev, bp->mii_bus);
348
349         np = bp->pdev->dev.of_node;
350         if (np) {
351                 /* try dt phy registration */
352                 err = of_mdiobus_register(bp->mii_bus, np);
353
354                 /* fallback to standard phy registration if no phy were
355                    found during dt phy registration */
356                 if (!err && !phy_find_first(bp->mii_bus)) {
357                         for (i = 0; i < PHY_MAX_ADDR; i++) {
358                                 struct phy_device *phydev;
359
360                                 phydev = mdiobus_scan(bp->mii_bus, i);
361                                 if (IS_ERR(phydev)) {
362                                         err = PTR_ERR(phydev);
363                                         break;
364                                 }
365                         }
366
367                         if (err)
368                                 goto err_out_unregister_bus;
369                 }
370         } else {
371                 for (i = 0; i < PHY_MAX_ADDR; i++)
372                         bp->mii_bus->irq[i] = PHY_POLL;
373
374                 if (pdata)
375                         bp->mii_bus->phy_mask = pdata->phy_mask;
376
377                 err = mdiobus_register(bp->mii_bus);
378         }
379
380         if (err)
381                 goto err_out_free_mdio_irq;
382
383         err = macb_mii_probe(bp->dev);
384         if (err)
385                 goto err_out_unregister_bus;
386
387         return 0;
388
389 err_out_unregister_bus:
390         mdiobus_unregister(bp->mii_bus);
391 err_out_free_mdio_irq:
392         kfree(bp->mii_bus->irq);
393 err_out_free_mdiobus:
394         mdiobus_free(bp->mii_bus);
395 err_out:
396         return err;
397 }
398 EXPORT_SYMBOL_GPL(macb_mii_init);
399
400 static void macb_update_stats(struct macb *bp)
401 {
402         u32 __iomem *reg = bp->regs + MACB_PFR;
403         u32 *p = &bp->hw_stats.macb.rx_pause_frames;
404         u32 *end = &bp->hw_stats.macb.tx_pause_frames + 1;
405
406         WARN_ON((unsigned long)(end - p - 1) != (MACB_TPF - MACB_PFR) / 4);
407
408         for(; p < end; p++, reg++)
409                 *p += __raw_readl(reg);
410 }
411
412 static int macb_halt_tx(struct macb *bp)
413 {
414         unsigned long   halt_time, timeout;
415         u32             status;
416
417         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(THALT));
418
419         timeout = jiffies + usecs_to_jiffies(MACB_HALT_TIMEOUT);
420         do {
421                 halt_time = jiffies;
422                 status = macb_readl(bp, TSR);
423                 if (!(status & MACB_BIT(TGO)))
424                         return 0;
425
426                 usleep_range(10, 250);
427         } while (time_before(halt_time, timeout));
428
429         return -ETIMEDOUT;
430 }
431
432 static void macb_tx_error_task(struct work_struct *work)
433 {
434         struct macb     *bp = container_of(work, struct macb, tx_error_task);
435         struct macb_tx_skb      *tx_skb;
436         struct sk_buff          *skb;
437         unsigned int            tail;
438
439         netdev_vdbg(bp->dev, "macb_tx_error_task: t = %u, h = %u\n",
440                     bp->tx_tail, bp->tx_head);
441
442         /* Make sure nobody is trying to queue up new packets */
443         netif_stop_queue(bp->dev);
444
445         /*
446          * Stop transmission now
447          * (in case we have just queued new packets)
448          */
449         if (macb_halt_tx(bp))
450                 /* Just complain for now, reinitializing TX path can be good */
451                 netdev_err(bp->dev, "BUG: halt tx timed out\n");
452
453         /* No need for the lock here as nobody will interrupt us anymore */
454
455         /*
456          * Treat frames in TX queue including the ones that caused the error.
457          * Free transmit buffers in upper layer.
458          */
459         for (tail = bp->tx_tail; tail != bp->tx_head; tail++) {
460                 struct macb_dma_desc    *desc;
461                 u32                     ctrl;
462
463                 desc = macb_tx_desc(bp, tail);
464                 ctrl = desc->ctrl;
465                 tx_skb = macb_tx_skb(bp, tail);
466                 skb = tx_skb->skb;
467
468                 if (ctrl & MACB_BIT(TX_USED)) {
469                         netdev_vdbg(bp->dev, "txerr skb %u (data %p) TX complete\n",
470                                     macb_tx_ring_wrap(tail), skb->data);
471                         bp->stats.tx_packets++;
472                         bp->stats.tx_bytes += skb->len;
473                 } else {
474                         /*
475                          * "Buffers exhausted mid-frame" errors may only happen
476                          * if the driver is buggy, so complain loudly about those.
477                          * Statistics are updated by hardware.
478                          */
479                         if (ctrl & MACB_BIT(TX_BUF_EXHAUSTED))
480                                 netdev_err(bp->dev,
481                                            "BUG: TX buffers exhausted mid-frame\n");
482
483                         desc->ctrl = ctrl | MACB_BIT(TX_USED);
484                 }
485
486                 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
487                                  DMA_TO_DEVICE);
488                 tx_skb->skb = NULL;
489                 dev_kfree_skb(skb);
490         }
491
492         /* Make descriptor updates visible to hardware */
493         wmb();
494
495         /* Reinitialize the TX desc queue */
496         macb_writel(bp, TBQP, bp->tx_ring_dma);
497         /* Make TX ring reflect state of hardware */
498         bp->tx_head = bp->tx_tail = 0;
499
500         /* Now we are ready to start transmission again */
501         netif_wake_queue(bp->dev);
502
503         /* Housework before enabling TX IRQ */
504         macb_writel(bp, TSR, macb_readl(bp, TSR));
505         macb_writel(bp, IER, MACB_TX_INT_FLAGS);
506 }
507
508 static void macb_tx_interrupt(struct macb *bp)
509 {
510         unsigned int tail;
511         unsigned int head;
512         u32 status;
513
514         status = macb_readl(bp, TSR);
515         macb_writel(bp, TSR, status);
516
517         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
518                 macb_writel(bp, ISR, MACB_BIT(TCOMP));
519
520         netdev_vdbg(bp->dev, "macb_tx_interrupt status = 0x%03lx\n",
521                 (unsigned long)status);
522
523         head = bp->tx_head;
524         for (tail = bp->tx_tail; tail != head; tail++) {
525                 struct macb_tx_skb      *tx_skb;
526                 struct sk_buff          *skb;
527                 struct macb_dma_desc    *desc;
528                 u32                     ctrl;
529
530                 desc = macb_tx_desc(bp, tail);
531
532                 /* Make hw descriptor updates visible to CPU */
533                 rmb();
534
535                 ctrl = desc->ctrl;
536
537                 if (!(ctrl & MACB_BIT(TX_USED)))
538                         break;
539
540                 tx_skb = macb_tx_skb(bp, tail);
541                 skb = tx_skb->skb;
542
543                 netdev_vdbg(bp->dev, "skb %u (data %p) TX complete\n",
544                         macb_tx_ring_wrap(tail), skb->data);
545                 dma_unmap_single(&bp->pdev->dev, tx_skb->mapping, skb->len,
546                                  DMA_TO_DEVICE);
547                 bp->stats.tx_packets++;
548                 bp->stats.tx_bytes += skb->len;
549                 tx_skb->skb = NULL;
550                 dev_kfree_skb_irq(skb);
551         }
552
553         bp->tx_tail = tail;
554         if (netif_queue_stopped(bp->dev)
555                         && CIRC_CNT(bp->tx_head, bp->tx_tail,
556                                     TX_RING_SIZE) <= MACB_TX_WAKEUP_THRESH)
557                 netif_wake_queue(bp->dev);
558 }
559
560 static void gem_rx_refill(struct macb *bp)
561 {
562         unsigned int            entry;
563         struct sk_buff          *skb;
564         struct macb_dma_desc    *desc;
565         dma_addr_t              paddr;
566
567         while (CIRC_SPACE(bp->rx_prepared_head, bp->rx_tail, RX_RING_SIZE) > 0) {
568                 u32 addr, ctrl;
569
570                 entry = macb_rx_ring_wrap(bp->rx_prepared_head);
571                 desc = &bp->rx_ring[entry];
572
573                 /* Make hw descriptor updates visible to CPU */
574                 rmb();
575
576                 addr = desc->addr;
577                 ctrl = desc->ctrl;
578                 bp->rx_prepared_head++;
579
580                 if ((addr & MACB_BIT(RX_USED)))
581                         continue;
582
583                 if (bp->rx_skbuff[entry] == NULL) {
584                         /* allocate sk_buff for this free entry in ring */
585                         skb = netdev_alloc_skb(bp->dev, bp->rx_buffer_size);
586                         if (unlikely(skb == NULL)) {
587                                 netdev_err(bp->dev,
588                                            "Unable to allocate sk_buff\n");
589                                 break;
590                         }
591                         bp->rx_skbuff[entry] = skb;
592
593                         /* now fill corresponding descriptor entry */
594                         paddr = dma_map_single(&bp->pdev->dev, skb->data,
595                                                bp->rx_buffer_size, DMA_FROM_DEVICE);
596
597                         if (entry == RX_RING_SIZE - 1)
598                                 paddr |= MACB_BIT(RX_WRAP);
599                         bp->rx_ring[entry].addr = paddr;
600                         bp->rx_ring[entry].ctrl = 0;
601
602                         /* properly align Ethernet header */
603                         skb_reserve(skb, NET_IP_ALIGN);
604                 }
605         }
606
607         /* Make descriptor updates visible to hardware */
608         wmb();
609
610         netdev_vdbg(bp->dev, "rx ring: prepared head %d, tail %d\n",
611                    bp->rx_prepared_head, bp->rx_tail);
612 }
613
614 /* Mark DMA descriptors from begin up to and not including end as unused */
615 static void discard_partial_frame(struct macb *bp, unsigned int begin,
616                                   unsigned int end)
617 {
618         unsigned int frag;
619
620         for (frag = begin; frag != end; frag++) {
621                 struct macb_dma_desc *desc = macb_rx_desc(bp, frag);
622                 desc->addr &= ~MACB_BIT(RX_USED);
623         }
624
625         /* Make descriptor updates visible to hardware */
626         wmb();
627
628         /*
629          * When this happens, the hardware stats registers for
630          * whatever caused this is updated, so we don't have to record
631          * anything.
632          */
633 }
634
635 static int gem_rx(struct macb *bp, int budget)
636 {
637         unsigned int            len;
638         unsigned int            entry;
639         struct sk_buff          *skb;
640         struct macb_dma_desc    *desc;
641         int                     count = 0;
642
643         while (count < budget) {
644                 u32 addr, ctrl;
645
646                 entry = macb_rx_ring_wrap(bp->rx_tail);
647                 desc = &bp->rx_ring[entry];
648
649                 /* Make hw descriptor updates visible to CPU */
650                 rmb();
651
652                 addr = desc->addr;
653                 ctrl = desc->ctrl;
654
655                 if (!(addr & MACB_BIT(RX_USED)))
656                         break;
657
658                 desc->addr &= ~MACB_BIT(RX_USED);
659                 bp->rx_tail++;
660                 count++;
661
662                 if (!(ctrl & MACB_BIT(RX_SOF) && ctrl & MACB_BIT(RX_EOF))) {
663                         netdev_err(bp->dev,
664                                    "not whole frame pointed by descriptor\n");
665                         bp->stats.rx_dropped++;
666                         break;
667                 }
668                 skb = bp->rx_skbuff[entry];
669                 if (unlikely(!skb)) {
670                         netdev_err(bp->dev,
671                                    "inconsistent Rx descriptor chain\n");
672                         bp->stats.rx_dropped++;
673                         break;
674                 }
675                 /* now everything is ready for receiving packet */
676                 bp->rx_skbuff[entry] = NULL;
677                 len = MACB_BFEXT(RX_FRMLEN, ctrl);
678
679                 netdev_vdbg(bp->dev, "gem_rx %u (len %u)\n", entry, len);
680
681                 skb_put(skb, len);
682                 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, addr));
683                 dma_unmap_single(&bp->pdev->dev, addr,
684                                  len, DMA_FROM_DEVICE);
685
686                 skb->protocol = eth_type_trans(skb, bp->dev);
687                 skb_checksum_none_assert(skb);
688
689                 bp->stats.rx_packets++;
690                 bp->stats.rx_bytes += skb->len;
691
692 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
693                 netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
694                             skb->len, skb->csum);
695                 print_hex_dump(KERN_DEBUG, " mac: ", DUMP_PREFIX_ADDRESS, 16, 1,
696                                skb->mac_header, 16, true);
697                 print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_ADDRESS, 16, 1,
698                                skb->data, 32, true);
699 #endif
700
701                 netif_receive_skb(skb);
702         }
703
704         gem_rx_refill(bp);
705
706         return count;
707 }
708
709 static int macb_rx_frame(struct macb *bp, unsigned int first_frag,
710                          unsigned int last_frag)
711 {
712         unsigned int len;
713         unsigned int frag;
714         unsigned int offset;
715         struct sk_buff *skb;
716         struct macb_dma_desc *desc;
717
718         desc = macb_rx_desc(bp, last_frag);
719         len = MACB_BFEXT(RX_FRMLEN, desc->ctrl);
720
721         netdev_vdbg(bp->dev, "macb_rx_frame frags %u - %u (len %u)\n",
722                 macb_rx_ring_wrap(first_frag),
723                 macb_rx_ring_wrap(last_frag), len);
724
725         /*
726          * The ethernet header starts NET_IP_ALIGN bytes into the
727          * first buffer. Since the header is 14 bytes, this makes the
728          * payload word-aligned.
729          *
730          * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
731          * the two padding bytes into the skb so that we avoid hitting
732          * the slowpath in memcpy(), and pull them off afterwards.
733          */
734         skb = netdev_alloc_skb(bp->dev, len + NET_IP_ALIGN);
735         if (!skb) {
736                 bp->stats.rx_dropped++;
737                 for (frag = first_frag; ; frag++) {
738                         desc = macb_rx_desc(bp, frag);
739                         desc->addr &= ~MACB_BIT(RX_USED);
740                         if (frag == last_frag)
741                                 break;
742                 }
743
744                 /* Make descriptor updates visible to hardware */
745                 wmb();
746
747                 return 1;
748         }
749
750         offset = 0;
751         len += NET_IP_ALIGN;
752         skb_checksum_none_assert(skb);
753         skb_put(skb, len);
754
755         for (frag = first_frag; ; frag++) {
756                 unsigned int frag_len = bp->rx_buffer_size;
757
758                 if (offset + frag_len > len) {
759                         BUG_ON(frag != last_frag);
760                         frag_len = len - offset;
761                 }
762                 skb_copy_to_linear_data_offset(skb, offset,
763                                 macb_rx_buffer(bp, frag), frag_len);
764                 offset += bp->rx_buffer_size;
765                 desc = macb_rx_desc(bp, frag);
766                 desc->addr &= ~MACB_BIT(RX_USED);
767
768                 if (frag == last_frag)
769                         break;
770         }
771
772         /* Make descriptor updates visible to hardware */
773         wmb();
774
775         __skb_pull(skb, NET_IP_ALIGN);
776         skb->protocol = eth_type_trans(skb, bp->dev);
777
778         bp->stats.rx_packets++;
779         bp->stats.rx_bytes += skb->len;
780         netdev_vdbg(bp->dev, "received skb of length %u, csum: %08x\n",
781                    skb->len, skb->csum);
782         netif_receive_skb(skb);
783
784         return 0;
785 }
786
787 static int macb_rx(struct macb *bp, int budget)
788 {
789         int received = 0;
790         unsigned int tail;
791         int first_frag = -1;
792
793         for (tail = bp->rx_tail; budget > 0; tail++) {
794                 struct macb_dma_desc *desc = macb_rx_desc(bp, tail);
795                 u32 addr, ctrl;
796
797                 /* Make hw descriptor updates visible to CPU */
798                 rmb();
799
800                 addr = desc->addr;
801                 ctrl = desc->ctrl;
802
803                 if (!(addr & MACB_BIT(RX_USED)))
804                         break;
805
806                 if (ctrl & MACB_BIT(RX_SOF)) {
807                         if (first_frag != -1)
808                                 discard_partial_frame(bp, first_frag, tail);
809                         first_frag = tail;
810                 }
811
812                 if (ctrl & MACB_BIT(RX_EOF)) {
813                         int dropped;
814                         BUG_ON(first_frag == -1);
815
816                         dropped = macb_rx_frame(bp, first_frag, tail);
817                         first_frag = -1;
818                         if (!dropped) {
819                                 received++;
820                                 budget--;
821                         }
822                 }
823         }
824
825         if (first_frag != -1)
826                 bp->rx_tail = first_frag;
827         else
828                 bp->rx_tail = tail;
829
830         return received;
831 }
832
833 static int macb_poll(struct napi_struct *napi, int budget)
834 {
835         struct macb *bp = container_of(napi, struct macb, napi);
836         int work_done;
837         u32 status;
838
839         status = macb_readl(bp, RSR);
840         macb_writel(bp, RSR, status);
841
842         work_done = 0;
843
844         netdev_vdbg(bp->dev, "poll: status = %08lx, budget = %d\n",
845                    (unsigned long)status, budget);
846
847         work_done = bp->macbgem_ops.mog_rx(bp, budget);
848         if (work_done < budget) {
849                 napi_complete(napi);
850
851                 /*
852                  * We've done what we can to clean the buffers. Make sure we
853                  * get notified when new packets arrive.
854                  */
855                 macb_writel(bp, IER, MACB_RX_INT_FLAGS);
856
857                 /* Packets received while interrupts were disabled */
858                 status = macb_readl(bp, RSR);
859                 if (unlikely(status))
860                         napi_reschedule(napi);
861         }
862
863         /* TODO: Handle errors */
864
865         return work_done;
866 }
867
868 static irqreturn_t macb_interrupt(int irq, void *dev_id)
869 {
870         struct net_device *dev = dev_id;
871         struct macb *bp = netdev_priv(dev);
872         u32 status;
873
874         status = macb_readl(bp, ISR);
875
876         if (unlikely(!status))
877                 return IRQ_NONE;
878
879         spin_lock(&bp->lock);
880
881         while (status) {
882                 /* close possible race with dev_close */
883                 if (unlikely(!netif_running(dev))) {
884                         macb_writel(bp, IDR, -1);
885                         break;
886                 }
887
888                 netdev_vdbg(bp->dev, "isr = 0x%08lx\n", (unsigned long)status);
889
890                 if (status & MACB_RX_INT_FLAGS) {
891                         /*
892                          * There's no point taking any more interrupts
893                          * until we have processed the buffers. The
894                          * scheduling call may fail if the poll routine
895                          * is already scheduled, so disable interrupts
896                          * now.
897                          */
898                         macb_writel(bp, IDR, MACB_RX_INT_FLAGS);
899                         if (bp->caps & MACB_CAPS_ISR_CLEAR_ON_WRITE)
900                                 macb_writel(bp, ISR, MACB_BIT(RCOMP));
901
902                         if (napi_schedule_prep(&bp->napi)) {
903                                 netdev_vdbg(bp->dev, "scheduling RX softirq\n");
904                                 __napi_schedule(&bp->napi);
905                         }
906                 }
907
908                 if (unlikely(status & (MACB_TX_ERR_FLAGS))) {
909                         macb_writel(bp, IDR, MACB_TX_INT_FLAGS);
910                         schedule_work(&bp->tx_error_task);
911                         break;
912                 }
913
914                 if (status & MACB_BIT(TCOMP))
915                         macb_tx_interrupt(bp);
916
917                 /*
918                  * Link change detection isn't possible with RMII, so we'll
919                  * add that if/when we get our hands on a full-blown MII PHY.
920                  */
921
922                 if (status & MACB_BIT(ISR_ROVR)) {
923                         /* We missed at least one packet */
924                         if (macb_is_gem(bp))
925                                 bp->hw_stats.gem.rx_overruns++;
926                         else
927                                 bp->hw_stats.macb.rx_overruns++;
928                 }
929
930                 if (status & MACB_BIT(HRESP)) {
931                         /*
932                          * TODO: Reset the hardware, and maybe move the
933                          * netdev_err to a lower-priority context as well
934                          * (work queue?)
935                          */
936                         netdev_err(dev, "DMA bus error: HRESP not OK\n");
937                 }
938
939                 status = macb_readl(bp, ISR);
940         }
941
942         spin_unlock(&bp->lock);
943
944         return IRQ_HANDLED;
945 }
946
947 #ifdef CONFIG_NET_POLL_CONTROLLER
948 /*
949  * Polling receive - used by netconsole and other diagnostic tools
950  * to allow network i/o with interrupts disabled.
951  */
952 static void macb_poll_controller(struct net_device *dev)
953 {
954         unsigned long flags;
955
956         local_irq_save(flags);
957         macb_interrupt(dev->irq, dev);
958         local_irq_restore(flags);
959 }
960 #endif
961
962 static int macb_start_xmit(struct sk_buff *skb, struct net_device *dev)
963 {
964         struct macb *bp = netdev_priv(dev);
965         dma_addr_t mapping;
966         unsigned int len, entry;
967         struct macb_dma_desc *desc;
968         struct macb_tx_skb *tx_skb;
969         u32 ctrl;
970         unsigned long flags;
971
972 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
973         netdev_vdbg(bp->dev,
974                    "start_xmit: len %u head %p data %p tail %p end %p\n",
975                    skb->len, skb->head, skb->data,
976                    skb_tail_pointer(skb), skb_end_pointer(skb));
977         print_hex_dump(KERN_DEBUG, "data: ", DUMP_PREFIX_OFFSET, 16, 1,
978                        skb->data, 16, true);
979 #endif
980
981         len = skb->len;
982         spin_lock_irqsave(&bp->lock, flags);
983
984         /* This is a hard error, log it. */
985         if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1) {
986                 netif_stop_queue(dev);
987                 spin_unlock_irqrestore(&bp->lock, flags);
988                 netdev_err(bp->dev, "BUG! Tx Ring full when queue awake!\n");
989                 netdev_dbg(bp->dev, "tx_head = %u, tx_tail = %u\n",
990                            bp->tx_head, bp->tx_tail);
991                 return NETDEV_TX_BUSY;
992         }
993
994         entry = macb_tx_ring_wrap(bp->tx_head);
995         bp->tx_head++;
996         netdev_vdbg(bp->dev, "Allocated ring entry %u\n", entry);
997         mapping = dma_map_single(&bp->pdev->dev, skb->data,
998                                  len, DMA_TO_DEVICE);
999
1000         tx_skb = &bp->tx_skb[entry];
1001         tx_skb->skb = skb;
1002         tx_skb->mapping = mapping;
1003         netdev_vdbg(bp->dev, "Mapped skb data %p to DMA addr %08lx\n",
1004                    skb->data, (unsigned long)mapping);
1005
1006         ctrl = MACB_BF(TX_FRMLEN, len);
1007         ctrl |= MACB_BIT(TX_LAST);
1008         if (entry == (TX_RING_SIZE - 1))
1009                 ctrl |= MACB_BIT(TX_WRAP);
1010
1011         desc = &bp->tx_ring[entry];
1012         desc->addr = mapping;
1013         desc->ctrl = ctrl;
1014
1015         /* Make newly initialized descriptor visible to hardware */
1016         wmb();
1017
1018         skb_tx_timestamp(skb);
1019
1020         macb_writel(bp, NCR, macb_readl(bp, NCR) | MACB_BIT(TSTART));
1021
1022         if (CIRC_SPACE(bp->tx_head, bp->tx_tail, TX_RING_SIZE) < 1)
1023                 netif_stop_queue(dev);
1024
1025         spin_unlock_irqrestore(&bp->lock, flags);
1026
1027         return NETDEV_TX_OK;
1028 }
1029
1030 static void macb_init_rx_buffer_size(struct macb *bp, size_t size)
1031 {
1032         if (!macb_is_gem(bp)) {
1033                 bp->rx_buffer_size = MACB_RX_BUFFER_SIZE;
1034         } else {
1035                 bp->rx_buffer_size = size;
1036
1037                 if (bp->rx_buffer_size % RX_BUFFER_MULTIPLE) {
1038                         netdev_dbg(bp->dev,
1039                                     "RX buffer must be multiple of %d bytes, expanding\n",
1040                                     RX_BUFFER_MULTIPLE);
1041                         bp->rx_buffer_size =
1042                                 roundup(bp->rx_buffer_size, RX_BUFFER_MULTIPLE);
1043                 }
1044         }
1045
1046         netdev_dbg(bp->dev, "mtu [%u] rx_buffer_size [%Zu]\n",
1047                    bp->dev->mtu, bp->rx_buffer_size);
1048 }
1049
1050 static void gem_free_rx_buffers(struct macb *bp)
1051 {
1052         struct sk_buff          *skb;
1053         struct macb_dma_desc    *desc;
1054         dma_addr_t              addr;
1055         int i;
1056
1057         if (!bp->rx_skbuff)
1058                 return;
1059
1060         for (i = 0; i < RX_RING_SIZE; i++) {
1061                 skb = bp->rx_skbuff[i];
1062
1063                 if (skb == NULL)
1064                         continue;
1065
1066                 desc = &bp->rx_ring[i];
1067                 addr = MACB_BF(RX_WADDR, MACB_BFEXT(RX_WADDR, desc->addr));
1068                 dma_unmap_single(&bp->pdev->dev, addr, skb->len,
1069                                  DMA_FROM_DEVICE);
1070                 dev_kfree_skb_any(skb);
1071                 skb = NULL;
1072         }
1073
1074         kfree(bp->rx_skbuff);
1075         bp->rx_skbuff = NULL;
1076 }
1077
1078 static void macb_free_rx_buffers(struct macb *bp)
1079 {
1080         if (bp->rx_buffers) {
1081                 dma_free_coherent(&bp->pdev->dev,
1082                                   RX_RING_SIZE * bp->rx_buffer_size,
1083                                   bp->rx_buffers, bp->rx_buffers_dma);
1084                 bp->rx_buffers = NULL;
1085         }
1086 }
1087
1088 static void macb_free_consistent(struct macb *bp)
1089 {
1090         if (bp->tx_skb) {
1091                 kfree(bp->tx_skb);
1092                 bp->tx_skb = NULL;
1093         }
1094         bp->macbgem_ops.mog_free_rx_buffers(bp);
1095         if (bp->rx_ring) {
1096                 dma_free_coherent(&bp->pdev->dev, RX_RING_BYTES,
1097                                   bp->rx_ring, bp->rx_ring_dma);
1098                 bp->rx_ring = NULL;
1099         }
1100         if (bp->tx_ring) {
1101                 dma_free_coherent(&bp->pdev->dev, TX_RING_BYTES,
1102                                   bp->tx_ring, bp->tx_ring_dma);
1103                 bp->tx_ring = NULL;
1104         }
1105 }
1106
1107 static int gem_alloc_rx_buffers(struct macb *bp)
1108 {
1109         int size;
1110
1111         size = RX_RING_SIZE * sizeof(struct sk_buff *);
1112         bp->rx_skbuff = kzalloc(size, GFP_KERNEL);
1113         if (!bp->rx_skbuff)
1114                 return -ENOMEM;
1115         else
1116                 netdev_dbg(bp->dev,
1117                            "Allocated %d RX struct sk_buff entries at %p\n",
1118                            RX_RING_SIZE, bp->rx_skbuff);
1119         return 0;
1120 }
1121
1122 static int macb_alloc_rx_buffers(struct macb *bp)
1123 {
1124         int size;
1125
1126         size = RX_RING_SIZE * bp->rx_buffer_size;
1127         bp->rx_buffers = dma_alloc_coherent(&bp->pdev->dev, size,
1128                                             &bp->rx_buffers_dma, GFP_KERNEL);
1129         if (!bp->rx_buffers)
1130                 return -ENOMEM;
1131         else
1132                 netdev_dbg(bp->dev,
1133                            "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1134                            size, (unsigned long)bp->rx_buffers_dma, bp->rx_buffers);
1135         return 0;
1136 }
1137
1138 static int macb_alloc_consistent(struct macb *bp)
1139 {
1140         int size;
1141
1142         size = TX_RING_SIZE * sizeof(struct macb_tx_skb);
1143         bp->tx_skb = kmalloc(size, GFP_KERNEL);
1144         if (!bp->tx_skb)
1145                 goto out_err;
1146
1147         size = RX_RING_BYTES;
1148         bp->rx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1149                                          &bp->rx_ring_dma, GFP_KERNEL);
1150         if (!bp->rx_ring)
1151                 goto out_err;
1152         netdev_dbg(bp->dev,
1153                    "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1154                    size, (unsigned long)bp->rx_ring_dma, bp->rx_ring);
1155
1156         size = TX_RING_BYTES;
1157         bp->tx_ring = dma_alloc_coherent(&bp->pdev->dev, size,
1158                                          &bp->tx_ring_dma, GFP_KERNEL);
1159         if (!bp->tx_ring)
1160                 goto out_err;
1161         netdev_dbg(bp->dev,
1162                    "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
1163                    size, (unsigned long)bp->tx_ring_dma, bp->tx_ring);
1164
1165         if (bp->macbgem_ops.mog_alloc_rx_buffers(bp))
1166                 goto out_err;
1167
1168         return 0;
1169
1170 out_err:
1171         macb_free_consistent(bp);
1172         return -ENOMEM;
1173 }
1174
1175 static void gem_init_rings(struct macb *bp)
1176 {
1177         int i;
1178
1179         for (i = 0; i < TX_RING_SIZE; i++) {
1180                 bp->tx_ring[i].addr = 0;
1181                 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1182         }
1183         bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1184
1185         bp->rx_tail = bp->rx_prepared_head = bp->tx_head = bp->tx_tail = 0;
1186
1187         gem_rx_refill(bp);
1188 }
1189
1190 static void macb_init_rings(struct macb *bp)
1191 {
1192         int i;
1193         dma_addr_t addr;
1194
1195         addr = bp->rx_buffers_dma;
1196         for (i = 0; i < RX_RING_SIZE; i++) {
1197                 bp->rx_ring[i].addr = addr;
1198                 bp->rx_ring[i].ctrl = 0;
1199                 addr += bp->rx_buffer_size;
1200         }
1201         bp->rx_ring[RX_RING_SIZE - 1].addr |= MACB_BIT(RX_WRAP);
1202
1203         for (i = 0; i < TX_RING_SIZE; i++) {
1204                 bp->tx_ring[i].addr = 0;
1205                 bp->tx_ring[i].ctrl = MACB_BIT(TX_USED);
1206         }
1207         bp->tx_ring[TX_RING_SIZE - 1].ctrl |= MACB_BIT(TX_WRAP);
1208
1209         bp->rx_tail = bp->tx_head = bp->tx_tail = 0;
1210 }
1211
1212 static void macb_reset_hw(struct macb *bp)
1213 {
1214         /*
1215          * Disable RX and TX (XXX: Should we halt the transmission
1216          * more gracefully?)
1217          */
1218         macb_writel(bp, NCR, 0);
1219
1220         /* Clear the stats registers (XXX: Update stats first?) */
1221         macb_writel(bp, NCR, MACB_BIT(CLRSTAT));
1222
1223         /* Clear all status flags */
1224         macb_writel(bp, TSR, -1);
1225         macb_writel(bp, RSR, -1);
1226
1227         /* Disable all interrupts */
1228         macb_writel(bp, IDR, -1);
1229         macb_readl(bp, ISR);
1230 }
1231
1232 static u32 gem_mdc_clk_div(struct macb *bp)
1233 {
1234         u32 config;
1235         unsigned long pclk_hz = clk_get_rate(bp->pclk);
1236
1237         if (pclk_hz <= 20000000)
1238                 config = GEM_BF(CLK, GEM_CLK_DIV8);
1239         else if (pclk_hz <= 40000000)
1240                 config = GEM_BF(CLK, GEM_CLK_DIV16);
1241         else if (pclk_hz <= 80000000)
1242                 config = GEM_BF(CLK, GEM_CLK_DIV32);
1243         else if (pclk_hz <= 120000000)
1244                 config = GEM_BF(CLK, GEM_CLK_DIV48);
1245         else if (pclk_hz <= 160000000)
1246                 config = GEM_BF(CLK, GEM_CLK_DIV64);
1247         else
1248                 config = GEM_BF(CLK, GEM_CLK_DIV96);
1249
1250         return config;
1251 }
1252
1253 static u32 macb_mdc_clk_div(struct macb *bp)
1254 {
1255         u32 config;
1256         unsigned long pclk_hz;
1257
1258         if (macb_is_gem(bp))
1259                 return gem_mdc_clk_div(bp);
1260
1261         pclk_hz = clk_get_rate(bp->pclk);
1262         if (pclk_hz <= 20000000)
1263                 config = MACB_BF(CLK, MACB_CLK_DIV8);
1264         else if (pclk_hz <= 40000000)
1265                 config = MACB_BF(CLK, MACB_CLK_DIV16);
1266         else if (pclk_hz <= 80000000)
1267                 config = MACB_BF(CLK, MACB_CLK_DIV32);
1268         else
1269                 config = MACB_BF(CLK, MACB_CLK_DIV64);
1270
1271         return config;
1272 }
1273
1274 /*
1275  * Get the DMA bus width field of the network configuration register that we
1276  * should program.  We find the width from decoding the design configuration
1277  * register to find the maximum supported data bus width.
1278  */
1279 static u32 macb_dbw(struct macb *bp)
1280 {
1281         if (!macb_is_gem(bp))
1282                 return 0;
1283
1284         switch (GEM_BFEXT(DBWDEF, gem_readl(bp, DCFG1))) {
1285         case 4:
1286                 return GEM_BF(DBW, GEM_DBW128);
1287         case 2:
1288                 return GEM_BF(DBW, GEM_DBW64);
1289         case 1:
1290         default:
1291                 return GEM_BF(DBW, GEM_DBW32);
1292         }
1293 }
1294
1295 /*
1296  * Configure the receive DMA engine
1297  * - use the correct receive buffer size
1298  * - set the possibility to use INCR16 bursts
1299  *   (if not supported by FIFO, it will fallback to default)
1300  * - set both rx/tx packet buffers to full memory size
1301  * These are configurable parameters for GEM.
1302  */
1303 static void macb_configure_dma(struct macb *bp)
1304 {
1305         u32 dmacfg;
1306
1307         if (macb_is_gem(bp)) {
1308                 dmacfg = gem_readl(bp, DMACFG) & ~GEM_BF(RXBS, -1L);
1309                 dmacfg |= GEM_BF(RXBS, bp->rx_buffer_size / RX_BUFFER_MULTIPLE);
1310                 dmacfg |= GEM_BF(FBLDO, 16);
1311                 dmacfg |= GEM_BIT(TXPBMS) | GEM_BF(RXBMS, -1L);
1312                 dmacfg &= ~GEM_BIT(ENDIA);
1313                 gem_writel(bp, DMACFG, dmacfg);
1314         }
1315 }
1316
1317 /*
1318  * Configure peripheral capacities according to integration options used
1319  */
1320 static void macb_configure_caps(struct macb *bp)
1321 {
1322         if (macb_is_gem(bp)) {
1323                 if (GEM_BFEXT(IRQCOR, gem_readl(bp, DCFG1)) == 0)
1324                         bp->caps |= MACB_CAPS_ISR_CLEAR_ON_WRITE;
1325         }
1326 }
1327
1328 static void macb_init_hw(struct macb *bp)
1329 {
1330         u32 config;
1331
1332         macb_reset_hw(bp);
1333         macb_set_hwaddr(bp);
1334
1335         config = macb_mdc_clk_div(bp);
1336         config |= MACB_BF(RBOF, NET_IP_ALIGN);  /* Make eth data aligned */
1337         config |= MACB_BIT(PAE);                /* PAuse Enable */
1338         config |= MACB_BIT(DRFCS);              /* Discard Rx FCS */
1339         config |= MACB_BIT(BIG);                /* Receive oversized frames */
1340         if (bp->dev->flags & IFF_PROMISC)
1341                 config |= MACB_BIT(CAF);        /* Copy All Frames */
1342         if (!(bp->dev->flags & IFF_BROADCAST))
1343                 config |= MACB_BIT(NBC);        /* No BroadCast */
1344         config |= macb_dbw(bp);
1345         macb_writel(bp, NCFGR, config);
1346         bp->speed = SPEED_10;
1347         bp->duplex = DUPLEX_HALF;
1348
1349         macb_configure_dma(bp);
1350         macb_configure_caps(bp);
1351
1352         /* Initialize TX and RX buffers */
1353         macb_writel(bp, RBQP, bp->rx_ring_dma);
1354         macb_writel(bp, TBQP, bp->tx_ring_dma);
1355
1356         /* Enable TX and RX */
1357         macb_writel(bp, NCR, MACB_BIT(RE) | MACB_BIT(TE) | MACB_BIT(MPE));
1358
1359         /* Enable interrupts */
1360         macb_writel(bp, IER, (MACB_RX_INT_FLAGS
1361                               | MACB_TX_INT_FLAGS
1362                               | MACB_BIT(HRESP)));
1363
1364 }
1365
1366 /*
1367  * The hash address register is 64 bits long and takes up two
1368  * locations in the memory map.  The least significant bits are stored
1369  * in EMAC_HSL and the most significant bits in EMAC_HSH.
1370  *
1371  * The unicast hash enable and the multicast hash enable bits in the
1372  * network configuration register enable the reception of hash matched
1373  * frames. The destination address is reduced to a 6 bit index into
1374  * the 64 bit hash register using the following hash function.  The
1375  * hash function is an exclusive or of every sixth bit of the
1376  * destination address.
1377  *
1378  * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1379  * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1380  * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1381  * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1382  * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1383  * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1384  *
1385  * da[0] represents the least significant bit of the first byte
1386  * received, that is, the multicast/unicast indicator, and da[47]
1387  * represents the most significant bit of the last byte received.  If
1388  * the hash index, hi[n], points to a bit that is set in the hash
1389  * register then the frame will be matched according to whether the
1390  * frame is multicast or unicast.  A multicast match will be signalled
1391  * if the multicast hash enable bit is set, da[0] is 1 and the hash
1392  * index points to a bit set in the hash register.  A unicast match
1393  * will be signalled if the unicast hash enable bit is set, da[0] is 0
1394  * and the hash index points to a bit set in the hash register.  To
1395  * receive all multicast frames, the hash register should be set with
1396  * all ones and the multicast hash enable bit should be set in the
1397  * network configuration register.
1398  */
1399
1400 static inline int hash_bit_value(int bitnr, __u8 *addr)
1401 {
1402         if (addr[bitnr / 8] & (1 << (bitnr % 8)))
1403                 return 1;
1404         return 0;
1405 }
1406
1407 /*
1408  * Return the hash index value for the specified address.
1409  */
1410 static int hash_get_index(__u8 *addr)
1411 {
1412         int i, j, bitval;
1413         int hash_index = 0;
1414
1415         for (j = 0; j < 6; j++) {
1416                 for (i = 0, bitval = 0; i < 8; i++)
1417                         bitval ^= hash_bit_value(i*6 + j, addr);
1418
1419                 hash_index |= (bitval << j);
1420         }
1421
1422         return hash_index;
1423 }
1424
1425 /*
1426  * Add multicast addresses to the internal multicast-hash table.
1427  */
1428 static void macb_sethashtable(struct net_device *dev)
1429 {
1430         struct netdev_hw_addr *ha;
1431         unsigned long mc_filter[2];
1432         unsigned int bitnr;
1433         struct macb *bp = netdev_priv(dev);
1434
1435         mc_filter[0] = mc_filter[1] = 0;
1436
1437         netdev_for_each_mc_addr(ha, dev) {
1438                 bitnr = hash_get_index(ha->addr);
1439                 mc_filter[bitnr >> 5] |= 1 << (bitnr & 31);
1440         }
1441
1442         macb_or_gem_writel(bp, HRB, mc_filter[0]);
1443         macb_or_gem_writel(bp, HRT, mc_filter[1]);
1444 }
1445
1446 /*
1447  * Enable/Disable promiscuous and multicast modes.
1448  */
1449 void macb_set_rx_mode(struct net_device *dev)
1450 {
1451         unsigned long cfg;
1452         struct macb *bp = netdev_priv(dev);
1453
1454         cfg = macb_readl(bp, NCFGR);
1455
1456         if (dev->flags & IFF_PROMISC)
1457                 /* Enable promiscuous mode */
1458                 cfg |= MACB_BIT(CAF);
1459         else if (dev->flags & (~IFF_PROMISC))
1460                  /* Disable promiscuous mode */
1461                 cfg &= ~MACB_BIT(CAF);
1462
1463         if (dev->flags & IFF_ALLMULTI) {
1464                 /* Enable all multicast mode */
1465                 macb_or_gem_writel(bp, HRB, -1);
1466                 macb_or_gem_writel(bp, HRT, -1);
1467                 cfg |= MACB_BIT(NCFGR_MTI);
1468         } else if (!netdev_mc_empty(dev)) {
1469                 /* Enable specific multicasts */
1470                 macb_sethashtable(dev);
1471                 cfg |= MACB_BIT(NCFGR_MTI);
1472         } else if (dev->flags & (~IFF_ALLMULTI)) {
1473                 /* Disable all multicast mode */
1474                 macb_or_gem_writel(bp, HRB, 0);
1475                 macb_or_gem_writel(bp, HRT, 0);
1476                 cfg &= ~MACB_BIT(NCFGR_MTI);
1477         }
1478
1479         macb_writel(bp, NCFGR, cfg);
1480 }
1481 EXPORT_SYMBOL_GPL(macb_set_rx_mode);
1482
1483 static int macb_open(struct net_device *dev)
1484 {
1485         struct macb *bp = netdev_priv(dev);
1486         size_t bufsz = dev->mtu + ETH_HLEN + ETH_FCS_LEN + NET_IP_ALIGN;
1487         int err;
1488
1489         netdev_dbg(bp->dev, "open\n");
1490
1491         /* carrier starts down */
1492         netif_carrier_off(dev);
1493
1494         /* if the phy is not yet register, retry later*/
1495         if (!bp->phy_dev)
1496                 return -EAGAIN;
1497
1498         /* RX buffers initialization */
1499         macb_init_rx_buffer_size(bp, bufsz);
1500
1501         err = macb_alloc_consistent(bp);
1502         if (err) {
1503                 netdev_err(dev, "Unable to allocate DMA memory (error %d)\n",
1504                            err);
1505                 return err;
1506         }
1507
1508         napi_enable(&bp->napi);
1509
1510         bp->macbgem_ops.mog_init_rings(bp);
1511         macb_init_hw(bp);
1512
1513         /* schedule a link state check */
1514         phy_start(bp->phy_dev);
1515
1516         netif_start_queue(dev);
1517
1518         return 0;
1519 }
1520
1521 static int macb_close(struct net_device *dev)
1522 {
1523         struct macb *bp = netdev_priv(dev);
1524         unsigned long flags;
1525
1526         netif_stop_queue(dev);
1527         napi_disable(&bp->napi);
1528
1529         if (bp->phy_dev)
1530                 phy_stop(bp->phy_dev);
1531
1532         spin_lock_irqsave(&bp->lock, flags);
1533         macb_reset_hw(bp);
1534         netif_carrier_off(dev);
1535         spin_unlock_irqrestore(&bp->lock, flags);
1536
1537         macb_free_consistent(bp);
1538
1539         return 0;
1540 }
1541
1542 static void gem_update_stats(struct macb *bp)
1543 {
1544         u32 __iomem *reg = bp->regs + GEM_OTX;
1545         u32 *p = &bp->hw_stats.gem.tx_octets_31_0;
1546         u32 *end = &bp->hw_stats.gem.rx_udp_checksum_errors + 1;
1547
1548         for (; p < end; p++, reg++)
1549                 *p += __raw_readl(reg);
1550 }
1551
1552 static struct net_device_stats *gem_get_stats(struct macb *bp)
1553 {
1554         struct gem_stats *hwstat = &bp->hw_stats.gem;
1555         struct net_device_stats *nstat = &bp->stats;
1556
1557         gem_update_stats(bp);
1558
1559         nstat->rx_errors = (hwstat->rx_frame_check_sequence_errors +
1560                             hwstat->rx_alignment_errors +
1561                             hwstat->rx_resource_errors +
1562                             hwstat->rx_overruns +
1563                             hwstat->rx_oversize_frames +
1564                             hwstat->rx_jabbers +
1565                             hwstat->rx_undersized_frames +
1566                             hwstat->rx_length_field_frame_errors);
1567         nstat->tx_errors = (hwstat->tx_late_collisions +
1568                             hwstat->tx_excessive_collisions +
1569                             hwstat->tx_underrun +
1570                             hwstat->tx_carrier_sense_errors);
1571         nstat->multicast = hwstat->rx_multicast_frames;
1572         nstat->collisions = (hwstat->tx_single_collision_frames +
1573                              hwstat->tx_multiple_collision_frames +
1574                              hwstat->tx_excessive_collisions);
1575         nstat->rx_length_errors = (hwstat->rx_oversize_frames +
1576                                    hwstat->rx_jabbers +
1577                                    hwstat->rx_undersized_frames +
1578                                    hwstat->rx_length_field_frame_errors);
1579         nstat->rx_over_errors = hwstat->rx_resource_errors;
1580         nstat->rx_crc_errors = hwstat->rx_frame_check_sequence_errors;
1581         nstat->rx_frame_errors = hwstat->rx_alignment_errors;
1582         nstat->rx_fifo_errors = hwstat->rx_overruns;
1583         nstat->tx_aborted_errors = hwstat->tx_excessive_collisions;
1584         nstat->tx_carrier_errors = hwstat->tx_carrier_sense_errors;
1585         nstat->tx_fifo_errors = hwstat->tx_underrun;
1586
1587         return nstat;
1588 }
1589
1590 struct net_device_stats *macb_get_stats(struct net_device *dev)
1591 {
1592         struct macb *bp = netdev_priv(dev);
1593         struct net_device_stats *nstat = &bp->stats;
1594         struct macb_stats *hwstat = &bp->hw_stats.macb;
1595
1596         if (macb_is_gem(bp))
1597                 return gem_get_stats(bp);
1598
1599         /* read stats from hardware */
1600         macb_update_stats(bp);
1601
1602         /* Convert HW stats into netdevice stats */
1603         nstat->rx_errors = (hwstat->rx_fcs_errors +
1604                             hwstat->rx_align_errors +
1605                             hwstat->rx_resource_errors +
1606                             hwstat->rx_overruns +
1607                             hwstat->rx_oversize_pkts +
1608                             hwstat->rx_jabbers +
1609                             hwstat->rx_undersize_pkts +
1610                             hwstat->sqe_test_errors +
1611                             hwstat->rx_length_mismatch);
1612         nstat->tx_errors = (hwstat->tx_late_cols +
1613                             hwstat->tx_excessive_cols +
1614                             hwstat->tx_underruns +
1615                             hwstat->tx_carrier_errors);
1616         nstat->collisions = (hwstat->tx_single_cols +
1617                              hwstat->tx_multiple_cols +
1618                              hwstat->tx_excessive_cols);
1619         nstat->rx_length_errors = (hwstat->rx_oversize_pkts +
1620                                    hwstat->rx_jabbers +
1621                                    hwstat->rx_undersize_pkts +
1622                                    hwstat->rx_length_mismatch);
1623         nstat->rx_over_errors = hwstat->rx_resource_errors +
1624                                    hwstat->rx_overruns;
1625         nstat->rx_crc_errors = hwstat->rx_fcs_errors;
1626         nstat->rx_frame_errors = hwstat->rx_align_errors;
1627         nstat->rx_fifo_errors = hwstat->rx_overruns;
1628         /* XXX: What does "missed" mean? */
1629         nstat->tx_aborted_errors = hwstat->tx_excessive_cols;
1630         nstat->tx_carrier_errors = hwstat->tx_carrier_errors;
1631         nstat->tx_fifo_errors = hwstat->tx_underruns;
1632         /* Don't know about heartbeat or window errors... */
1633
1634         return nstat;
1635 }
1636 EXPORT_SYMBOL_GPL(macb_get_stats);
1637
1638 static int macb_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1639 {
1640         struct macb *bp = netdev_priv(dev);
1641         struct phy_device *phydev = bp->phy_dev;
1642
1643         if (!phydev)
1644                 return -ENODEV;
1645
1646         return phy_ethtool_gset(phydev, cmd);
1647 }
1648
1649 static int macb_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1650 {
1651         struct macb *bp = netdev_priv(dev);
1652         struct phy_device *phydev = bp->phy_dev;
1653
1654         if (!phydev)
1655                 return -ENODEV;
1656
1657         return phy_ethtool_sset(phydev, cmd);
1658 }
1659
1660 static int macb_get_regs_len(struct net_device *netdev)
1661 {
1662         return MACB_GREGS_NBR * sizeof(u32);
1663 }
1664
1665 static void macb_get_regs(struct net_device *dev, struct ethtool_regs *regs,
1666                           void *p)
1667 {
1668         struct macb *bp = netdev_priv(dev);
1669         unsigned int tail, head;
1670         u32 *regs_buff = p;
1671
1672         regs->version = (macb_readl(bp, MID) & ((1 << MACB_REV_SIZE) - 1))
1673                         | MACB_GREGS_VERSION;
1674
1675         tail = macb_tx_ring_wrap(bp->tx_tail);
1676         head = macb_tx_ring_wrap(bp->tx_head);
1677
1678         regs_buff[0]  = macb_readl(bp, NCR);
1679         regs_buff[1]  = macb_or_gem_readl(bp, NCFGR);
1680         regs_buff[2]  = macb_readl(bp, NSR);
1681         regs_buff[3]  = macb_readl(bp, TSR);
1682         regs_buff[4]  = macb_readl(bp, RBQP);
1683         regs_buff[5]  = macb_readl(bp, TBQP);
1684         regs_buff[6]  = macb_readl(bp, RSR);
1685         regs_buff[7]  = macb_readl(bp, IMR);
1686
1687         regs_buff[8]  = tail;
1688         regs_buff[9]  = head;
1689         regs_buff[10] = macb_tx_dma(bp, tail);
1690         regs_buff[11] = macb_tx_dma(bp, head);
1691
1692         if (macb_is_gem(bp)) {
1693                 regs_buff[12] = gem_readl(bp, USRIO);
1694                 regs_buff[13] = gem_readl(bp, DMACFG);
1695         }
1696 }
1697
1698 const struct ethtool_ops macb_ethtool_ops = {
1699         .get_settings           = macb_get_settings,
1700         .set_settings           = macb_set_settings,
1701         .get_regs_len           = macb_get_regs_len,
1702         .get_regs               = macb_get_regs,
1703         .get_link               = ethtool_op_get_link,
1704         .get_ts_info            = ethtool_op_get_ts_info,
1705 };
1706 EXPORT_SYMBOL_GPL(macb_ethtool_ops);
1707
1708 int macb_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1709 {
1710         struct macb *bp = netdev_priv(dev);
1711         struct phy_device *phydev = bp->phy_dev;
1712
1713         if (!netif_running(dev))
1714                 return -EINVAL;
1715
1716         if (!phydev)
1717                 return -ENODEV;
1718
1719         return phy_mii_ioctl(phydev, rq, cmd);
1720 }
1721 EXPORT_SYMBOL_GPL(macb_ioctl);
1722
1723 static const struct net_device_ops macb_netdev_ops = {
1724         .ndo_open               = macb_open,
1725         .ndo_stop               = macb_close,
1726         .ndo_start_xmit         = macb_start_xmit,
1727         .ndo_set_rx_mode        = macb_set_rx_mode,
1728         .ndo_get_stats          = macb_get_stats,
1729         .ndo_do_ioctl           = macb_ioctl,
1730         .ndo_validate_addr      = eth_validate_addr,
1731         .ndo_change_mtu         = eth_change_mtu,
1732         .ndo_set_mac_address    = eth_mac_addr,
1733 #ifdef CONFIG_NET_POLL_CONTROLLER
1734         .ndo_poll_controller    = macb_poll_controller,
1735 #endif
1736 };
1737
1738 #if defined(CONFIG_OF)
1739 static const struct of_device_id macb_dt_ids[] = {
1740         { .compatible = "cdns,at32ap7000-macb" },
1741         { .compatible = "cdns,at91sam9260-macb" },
1742         { .compatible = "cdns,macb" },
1743         { .compatible = "cdns,pc302-gem" },
1744         { .compatible = "cdns,gem" },
1745         { /* sentinel */ }
1746 };
1747 MODULE_DEVICE_TABLE(of, macb_dt_ids);
1748 #endif
1749
1750 static int __init macb_probe(struct platform_device *pdev)
1751 {
1752         struct macb_platform_data *pdata;
1753         struct resource *regs;
1754         struct net_device *dev;
1755         struct macb *bp;
1756         struct phy_device *phydev;
1757         u32 config;
1758         int err = -ENXIO;
1759         struct pinctrl *pinctrl;
1760         const char *mac;
1761
1762         regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1763         if (!regs) {
1764                 dev_err(&pdev->dev, "no mmio resource defined\n");
1765                 goto err_out;
1766         }
1767
1768         pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
1769         if (IS_ERR(pinctrl)) {
1770                 err = PTR_ERR(pinctrl);
1771                 if (err == -EPROBE_DEFER)
1772                         goto err_out;
1773
1774                 dev_warn(&pdev->dev, "No pinctrl provided\n");
1775         }
1776
1777         err = -ENOMEM;
1778         dev = alloc_etherdev(sizeof(*bp));
1779         if (!dev)
1780                 goto err_out;
1781
1782         SET_NETDEV_DEV(dev, &pdev->dev);
1783
1784         /* TODO: Actually, we have some interesting features... */
1785         dev->features |= 0;
1786
1787         bp = netdev_priv(dev);
1788         bp->pdev = pdev;
1789         bp->dev = dev;
1790
1791         spin_lock_init(&bp->lock);
1792         INIT_WORK(&bp->tx_error_task, macb_tx_error_task);
1793
1794         bp->pclk = devm_clk_get(&pdev->dev, "pclk");
1795         if (IS_ERR(bp->pclk)) {
1796                 err = PTR_ERR(bp->pclk);
1797                 dev_err(&pdev->dev, "failed to get macb_clk (%u)\n", err);
1798                 goto err_out_free_dev;
1799         }
1800
1801         bp->hclk = devm_clk_get(&pdev->dev, "hclk");
1802         if (IS_ERR(bp->hclk)) {
1803                 err = PTR_ERR(bp->hclk);
1804                 dev_err(&pdev->dev, "failed to get hclk (%u)\n", err);
1805                 goto err_out_free_dev;
1806         }
1807
1808         err = clk_prepare_enable(bp->pclk);
1809         if (err) {
1810                 dev_err(&pdev->dev, "failed to enable pclk (%u)\n", err);
1811                 goto err_out_free_dev;
1812         }
1813
1814         err = clk_prepare_enable(bp->hclk);
1815         if (err) {
1816                 dev_err(&pdev->dev, "failed to enable hclk (%u)\n", err);
1817                 goto err_out_disable_pclk;
1818         }
1819
1820         bp->regs = devm_ioremap(&pdev->dev, regs->start, resource_size(regs));
1821         if (!bp->regs) {
1822                 dev_err(&pdev->dev, "failed to map registers, aborting.\n");
1823                 err = -ENOMEM;
1824                 goto err_out_disable_clocks;
1825         }
1826
1827         dev->irq = platform_get_irq(pdev, 0);
1828         err = request_irq(dev->irq, macb_interrupt, 0, dev->name, dev);
1829         if (err) {
1830                 dev_err(&pdev->dev, "Unable to request IRQ %d (error %d)\n",
1831                         dev->irq, err);
1832                 goto err_out_disable_clocks;
1833         }
1834
1835         dev->netdev_ops = &macb_netdev_ops;
1836         netif_napi_add(dev, &bp->napi, macb_poll, 64);
1837         dev->ethtool_ops = &macb_ethtool_ops;
1838
1839         dev->base_addr = regs->start;
1840
1841         /* setup appropriated routines according to adapter type */
1842         if (macb_is_gem(bp)) {
1843                 bp->macbgem_ops.mog_alloc_rx_buffers = gem_alloc_rx_buffers;
1844                 bp->macbgem_ops.mog_free_rx_buffers = gem_free_rx_buffers;
1845                 bp->macbgem_ops.mog_init_rings = gem_init_rings;
1846                 bp->macbgem_ops.mog_rx = gem_rx;
1847         } else {
1848                 bp->macbgem_ops.mog_alloc_rx_buffers = macb_alloc_rx_buffers;
1849                 bp->macbgem_ops.mog_free_rx_buffers = macb_free_rx_buffers;
1850                 bp->macbgem_ops.mog_init_rings = macb_init_rings;
1851                 bp->macbgem_ops.mog_rx = macb_rx;
1852         }
1853
1854         /* Set MII management clock divider */
1855         config = macb_mdc_clk_div(bp);
1856         config |= macb_dbw(bp);
1857         macb_writel(bp, NCFGR, config);
1858
1859         mac = of_get_mac_address(pdev->dev.of_node);
1860         if (mac)
1861                 memcpy(bp->dev->dev_addr, mac, ETH_ALEN);
1862         else
1863                 macb_get_hwaddr(bp);
1864
1865         err = of_get_phy_mode(pdev->dev.of_node);
1866         if (err < 0) {
1867                 pdata = dev_get_platdata(&pdev->dev);
1868                 if (pdata && pdata->is_rmii)
1869                         bp->phy_interface = PHY_INTERFACE_MODE_RMII;
1870                 else
1871                         bp->phy_interface = PHY_INTERFACE_MODE_MII;
1872         } else {
1873                 bp->phy_interface = err;
1874         }
1875
1876         if (bp->phy_interface == PHY_INTERFACE_MODE_RGMII)
1877                 macb_or_gem_writel(bp, USRIO, GEM_BIT(RGMII));
1878         else if (bp->phy_interface == PHY_INTERFACE_MODE_RMII)
1879 #if defined(CONFIG_ARCH_AT91)
1880                 macb_or_gem_writel(bp, USRIO, (MACB_BIT(RMII) |
1881                                                MACB_BIT(CLKEN)));
1882 #else
1883                 macb_or_gem_writel(bp, USRIO, 0);
1884 #endif
1885         else
1886 #if defined(CONFIG_ARCH_AT91)
1887                 macb_or_gem_writel(bp, USRIO, MACB_BIT(CLKEN));
1888 #else
1889                 macb_or_gem_writel(bp, USRIO, MACB_BIT(MII));
1890 #endif
1891
1892         err = register_netdev(dev);
1893         if (err) {
1894                 dev_err(&pdev->dev, "Cannot register net device, aborting.\n");
1895                 goto err_out_free_irq;
1896         }
1897
1898         err = macb_mii_init(bp);
1899         if (err)
1900                 goto err_out_unregister_netdev;
1901
1902         platform_set_drvdata(pdev, dev);
1903
1904         netif_carrier_off(dev);
1905
1906         netdev_info(dev, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1907                     macb_is_gem(bp) ? "GEM" : "MACB", dev->base_addr,
1908                     dev->irq, dev->dev_addr);
1909
1910         phydev = bp->phy_dev;
1911         netdev_info(dev, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1912                     phydev->drv->name, dev_name(&phydev->dev), phydev->irq);
1913
1914         return 0;
1915
1916 err_out_unregister_netdev:
1917         unregister_netdev(dev);
1918 err_out_free_irq:
1919         free_irq(dev->irq, dev);
1920 err_out_disable_clocks:
1921         clk_disable_unprepare(bp->hclk);
1922 err_out_disable_pclk:
1923         clk_disable_unprepare(bp->pclk);
1924 err_out_free_dev:
1925         free_netdev(dev);
1926 err_out:
1927         return err;
1928 }
1929
1930 static int __exit macb_remove(struct platform_device *pdev)
1931 {
1932         struct net_device *dev;
1933         struct macb *bp;
1934
1935         dev = platform_get_drvdata(pdev);
1936
1937         if (dev) {
1938                 bp = netdev_priv(dev);
1939                 if (bp->phy_dev)
1940                         phy_disconnect(bp->phy_dev);
1941                 mdiobus_unregister(bp->mii_bus);
1942                 kfree(bp->mii_bus->irq);
1943                 mdiobus_free(bp->mii_bus);
1944                 unregister_netdev(dev);
1945                 free_irq(dev->irq, dev);
1946                 clk_disable_unprepare(bp->hclk);
1947                 clk_disable_unprepare(bp->pclk);
1948                 free_netdev(dev);
1949         }
1950
1951         return 0;
1952 }
1953
1954 #ifdef CONFIG_PM
1955 static int macb_suspend(struct device *dev)
1956 {
1957         struct platform_device *pdev = to_platform_device(dev);
1958         struct net_device *netdev = platform_get_drvdata(pdev);
1959         struct macb *bp = netdev_priv(netdev);
1960
1961         netif_carrier_off(netdev);
1962         netif_device_detach(netdev);
1963
1964         clk_disable_unprepare(bp->hclk);
1965         clk_disable_unprepare(bp->pclk);
1966
1967         return 0;
1968 }
1969
1970 static int macb_resume(struct device *dev)
1971 {
1972         struct platform_device *pdev = to_platform_device(dev);
1973         struct net_device *netdev = platform_get_drvdata(pdev);
1974         struct macb *bp = netdev_priv(netdev);
1975
1976         clk_prepare_enable(bp->pclk);
1977         clk_prepare_enable(bp->hclk);
1978
1979         netif_device_attach(netdev);
1980
1981         return 0;
1982 }
1983 #endif
1984
1985 static SIMPLE_DEV_PM_OPS(macb_pm_ops, macb_suspend, macb_resume);
1986
1987 static struct platform_driver macb_driver = {
1988         .remove         = __exit_p(macb_remove),
1989         .driver         = {
1990                 .name           = "macb",
1991                 .owner  = THIS_MODULE,
1992                 .of_match_table = of_match_ptr(macb_dt_ids),
1993                 .pm     = &macb_pm_ops,
1994         },
1995 };
1996
1997 module_platform_driver_probe(macb_driver, macb_probe);
1998
1999 MODULE_LICENSE("GPL");
2000 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
2001 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
2002 MODULE_ALIAS("platform:macb");