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