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