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