2 * Cadence MACB/GEM Ethernet Controller driver
4 * Copyright (C) 2004-2006 Atmel Corporation
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.
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 #include <linux/clk.h>
13 #include <linux/module.h>
14 #include <linux/moduleparam.h>
15 #include <linux/kernel.h>
16 #include <linux/types.h>
17 #include <linux/circ_buf.h>
18 #include <linux/slab.h>
19 #include <linux/init.h>
21 #include <linux/gpio.h>
22 #include <linux/interrupt.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/platform_data/macb.h>
27 #include <linux/platform_device.h>
28 #include <linux/phy.h>
30 #include <linux/of_device.h>
31 #include <linux/of_mdio.h>
32 #include <linux/of_net.h>
36 #define MACB_RX_BUFFER_SIZE 128
37 #define RX_BUFFER_MULTIPLE 64 /* bytes */
38 #define RX_RING_SIZE 512 /* must be power of 2 */
39 #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
41 #define TX_RING_SIZE 128 /* must be power of 2 */
42 #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
44 /* level of occupied TX descriptors under which we wake up TX process */
45 #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
47 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
49 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
52 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
54 #define MACB_MAX_TX_LEN ((unsigned int)((1 << MACB_TX_FRMLEN_SIZE) - 1))
55 #define GEM_MAX_TX_LEN ((unsigned int)((1 << GEM_TX_FRMLEN_SIZE) - 1))
57 #define GEM_MTU_MIN_SIZE 68
60 * Graceful stop timeouts in us. We should allow up to
61 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
63 #define MACB_HALT_TIMEOUT 1230
65 /* Ring buffer accessors */
66 static unsigned int macb_tx_ring_wrap(unsigned int index
)
68 return index
& (TX_RING_SIZE
- 1);
71 static struct macb_dma_desc
*macb_tx_desc(struct macb_queue
*queue
,
74 return &queue
->tx_ring
[macb_tx_ring_wrap(index
)];
77 static struct macb_tx_skb
*macb_tx_skb(struct macb_queue
*queue
,
80 return &queue
->tx_skb
[macb_tx_ring_wrap(index
)];
83 static dma_addr_t
macb_tx_dma(struct macb_queue
*queue
, unsigned int index
)
87 offset
= macb_tx_ring_wrap(index
) * sizeof(struct macb_dma_desc
);
89 return queue
->tx_ring_dma
+ offset
;
92 static unsigned int macb_rx_ring_wrap(unsigned int index
)
94 return index
& (RX_RING_SIZE
- 1);
97 static struct macb_dma_desc
*macb_rx_desc(struct macb
*bp
, unsigned int index
)
99 return &bp
->rx_ring
[macb_rx_ring_wrap(index
)];
102 static void *macb_rx_buffer(struct macb
*bp
, unsigned int index
)
104 return bp
->rx_buffers
+ bp
->rx_buffer_size
* macb_rx_ring_wrap(index
);
108 static u32
hw_readl_native(struct macb
*bp
, int offset
)
110 return __raw_readl(bp
->regs
+ offset
);
113 static void hw_writel_native(struct macb
*bp
, int offset
, u32 value
)
115 __raw_writel(value
, bp
->regs
+ offset
);
118 static u32
hw_readl(struct macb
*bp
, int offset
)
120 return readl_relaxed(bp
->regs
+ offset
);
123 static void hw_writel(struct macb
*bp
, int offset
, u32 value
)
125 writel_relaxed(value
, bp
->regs
+ offset
);
129 * Find the CPU endianness by using the loopback bit of NCR register. When the
130 * CPU is in big endian we need to program swaped mode for management
133 static bool hw_is_native_io(void __iomem
*addr
)
135 u32 value
= MACB_BIT(LLB
);
137 __raw_writel(value
, addr
+ MACB_NCR
);
138 value
= __raw_readl(addr
+ MACB_NCR
);
140 /* Write 0 back to disable everything */
141 __raw_writel(0, addr
+ MACB_NCR
);
143 return value
== MACB_BIT(LLB
);
146 static bool hw_is_gem(void __iomem
*addr
, bool native_io
)
151 id
= __raw_readl(addr
+ MACB_MID
);
153 id
= readl_relaxed(addr
+ MACB_MID
);
155 return MACB_BFEXT(IDNUM
, id
) >= 0x2;
158 static void macb_set_hwaddr(struct macb
*bp
)
163 bottom
= cpu_to_le32(*((u32
*)bp
->dev
->dev_addr
));
164 macb_or_gem_writel(bp
, SA1B
, bottom
);
165 top
= cpu_to_le16(*((u16
*)(bp
->dev
->dev_addr
+ 4)));
166 macb_or_gem_writel(bp
, SA1T
, top
);
168 /* Clear unused address register sets */
169 macb_or_gem_writel(bp
, SA2B
, 0);
170 macb_or_gem_writel(bp
, SA2T
, 0);
171 macb_or_gem_writel(bp
, SA3B
, 0);
172 macb_or_gem_writel(bp
, SA3T
, 0);
173 macb_or_gem_writel(bp
, SA4B
, 0);
174 macb_or_gem_writel(bp
, SA4T
, 0);
177 static void macb_get_hwaddr(struct macb
*bp
)
179 struct macb_platform_data
*pdata
;
185 pdata
= dev_get_platdata(&bp
->pdev
->dev
);
187 /* Check all 4 address register for vaild address */
188 for (i
= 0; i
< 4; i
++) {
189 bottom
= macb_or_gem_readl(bp
, SA1B
+ i
* 8);
190 top
= macb_or_gem_readl(bp
, SA1T
+ i
* 8);
192 if (pdata
&& pdata
->rev_eth_addr
) {
193 addr
[5] = bottom
& 0xff;
194 addr
[4] = (bottom
>> 8) & 0xff;
195 addr
[3] = (bottom
>> 16) & 0xff;
196 addr
[2] = (bottom
>> 24) & 0xff;
197 addr
[1] = top
& 0xff;
198 addr
[0] = (top
& 0xff00) >> 8;
200 addr
[0] = bottom
& 0xff;
201 addr
[1] = (bottom
>> 8) & 0xff;
202 addr
[2] = (bottom
>> 16) & 0xff;
203 addr
[3] = (bottom
>> 24) & 0xff;
204 addr
[4] = top
& 0xff;
205 addr
[5] = (top
>> 8) & 0xff;
208 if (is_valid_ether_addr(addr
)) {
209 memcpy(bp
->dev
->dev_addr
, addr
, sizeof(addr
));
214 dev_info(&bp
->pdev
->dev
, "invalid hw address, using random\n");
215 eth_hw_addr_random(bp
->dev
);
218 static int macb_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
220 struct macb
*bp
= bus
->priv
;
223 macb_writel(bp
, MAN
, (MACB_BF(SOF
, MACB_MAN_SOF
)
224 | MACB_BF(RW
, MACB_MAN_READ
)
225 | MACB_BF(PHYA
, mii_id
)
226 | MACB_BF(REGA
, regnum
)
227 | MACB_BF(CODE
, MACB_MAN_CODE
)));
229 /* wait for end of transfer */
230 while (!MACB_BFEXT(IDLE
, macb_readl(bp
, NSR
)))
233 value
= MACB_BFEXT(DATA
, macb_readl(bp
, MAN
));
238 static int macb_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
241 struct macb
*bp
= bus
->priv
;
243 macb_writel(bp
, MAN
, (MACB_BF(SOF
, MACB_MAN_SOF
)
244 | MACB_BF(RW
, MACB_MAN_WRITE
)
245 | MACB_BF(PHYA
, mii_id
)
246 | MACB_BF(REGA
, regnum
)
247 | MACB_BF(CODE
, MACB_MAN_CODE
)
248 | MACB_BF(DATA
, value
)));
250 /* wait for end of transfer */
251 while (!MACB_BFEXT(IDLE
, macb_readl(bp
, NSR
)))
258 * macb_set_tx_clk() - Set a clock to a new frequency
259 * @clk Pointer to the clock to change
260 * @rate New frequency in Hz
261 * @dev Pointer to the struct net_device
263 static void macb_set_tx_clk(struct clk
*clk
, int speed
, struct net_device
*dev
)
265 long ferr
, rate
, rate_rounded
;
284 rate_rounded
= clk_round_rate(clk
, rate
);
285 if (rate_rounded
< 0)
288 /* RGMII allows 50 ppm frequency error. Test and warn if this limit
291 ferr
= abs(rate_rounded
- rate
);
292 ferr
= DIV_ROUND_UP(ferr
, rate
/ 100000);
294 netdev_warn(dev
, "unable to generate target frequency: %ld Hz\n",
297 if (clk_set_rate(clk
, rate_rounded
))
298 netdev_err(dev
, "adjusting tx_clk failed.\n");
301 static void macb_handle_link_change(struct net_device
*dev
)
303 struct macb
*bp
= netdev_priv(dev
);
304 struct phy_device
*phydev
= bp
->phy_dev
;
306 int status_change
= 0;
308 spin_lock_irqsave(&bp
->lock
, flags
);
311 if ((bp
->speed
!= phydev
->speed
) ||
312 (bp
->duplex
!= phydev
->duplex
)) {
315 reg
= macb_readl(bp
, NCFGR
);
316 reg
&= ~(MACB_BIT(SPD
) | MACB_BIT(FD
));
318 reg
&= ~GEM_BIT(GBE
);
322 if (phydev
->speed
== SPEED_100
)
323 reg
|= MACB_BIT(SPD
);
324 if (phydev
->speed
== SPEED_1000
&&
325 bp
->caps
& MACB_CAPS_GIGABIT_MODE_AVAILABLE
)
328 macb_or_gem_writel(bp
, NCFGR
, reg
);
330 bp
->speed
= phydev
->speed
;
331 bp
->duplex
= phydev
->duplex
;
336 if (phydev
->link
!= bp
->link
) {
341 bp
->link
= phydev
->link
;
346 spin_unlock_irqrestore(&bp
->lock
, flags
);
350 /* Update the TX clock rate if and only if the link is
351 * up and there has been a link change.
353 macb_set_tx_clk(bp
->tx_clk
, phydev
->speed
, dev
);
355 netif_carrier_on(dev
);
356 netdev_info(dev
, "link up (%d/%s)\n",
358 phydev
->duplex
== DUPLEX_FULL
?
361 netif_carrier_off(dev
);
362 netdev_info(dev
, "link down\n");
367 /* based on au1000_eth. c*/
368 static int macb_mii_probe(struct net_device
*dev
)
370 struct macb
*bp
= netdev_priv(dev
);
371 struct macb_platform_data
*pdata
;
372 struct phy_device
*phydev
;
376 phydev
= phy_find_first(bp
->mii_bus
);
378 netdev_err(dev
, "no PHY found\n");
382 pdata
= dev_get_platdata(&bp
->pdev
->dev
);
383 if (pdata
&& gpio_is_valid(pdata
->phy_irq_pin
)) {
384 ret
= devm_gpio_request(&bp
->pdev
->dev
, pdata
->phy_irq_pin
, "phy int");
386 phy_irq
= gpio_to_irq(pdata
->phy_irq_pin
);
387 phydev
->irq
= (phy_irq
< 0) ? PHY_POLL
: phy_irq
;
391 /* attach the mac to the phy */
392 ret
= phy_connect_direct(dev
, phydev
, &macb_handle_link_change
,
395 netdev_err(dev
, "Could not attach to PHY\n");
399 /* mask with MAC supported features */
400 if (macb_is_gem(bp
) && bp
->caps
& MACB_CAPS_GIGABIT_MODE_AVAILABLE
)
401 phydev
->supported
&= PHY_GBIT_FEATURES
;
403 phydev
->supported
&= PHY_BASIC_FEATURES
;
405 if (bp
->caps
& MACB_CAPS_NO_GIGABIT_HALF
)
406 phydev
->supported
&= ~SUPPORTED_1000baseT_Half
;
408 phydev
->advertising
= phydev
->supported
;
413 bp
->phy_dev
= phydev
;
418 static int macb_mii_init(struct macb
*bp
)
420 struct macb_platform_data
*pdata
;
421 struct device_node
*np
;
424 /* Enable management port */
425 macb_writel(bp
, NCR
, MACB_BIT(MPE
));
427 bp
->mii_bus
= mdiobus_alloc();
428 if (bp
->mii_bus
== NULL
) {
433 bp
->mii_bus
->name
= "MACB_mii_bus";
434 bp
->mii_bus
->read
= &macb_mdio_read
;
435 bp
->mii_bus
->write
= &macb_mdio_write
;
436 snprintf(bp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
437 bp
->pdev
->name
, bp
->pdev
->id
);
438 bp
->mii_bus
->priv
= bp
;
439 bp
->mii_bus
->parent
= &bp
->dev
->dev
;
440 pdata
= dev_get_platdata(&bp
->pdev
->dev
);
442 bp
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
443 if (!bp
->mii_bus
->irq
) {
445 goto err_out_free_mdiobus
;
448 dev_set_drvdata(&bp
->dev
->dev
, bp
->mii_bus
);
450 np
= bp
->pdev
->dev
.of_node
;
452 /* try dt phy registration */
453 err
= of_mdiobus_register(bp
->mii_bus
, np
);
455 /* fallback to standard phy registration if no phy were
456 found during dt phy registration */
457 if (!err
&& !phy_find_first(bp
->mii_bus
)) {
458 for (i
= 0; i
< PHY_MAX_ADDR
; i
++) {
459 struct phy_device
*phydev
;
461 phydev
= mdiobus_scan(bp
->mii_bus
, i
);
462 if (IS_ERR(phydev
)) {
463 err
= PTR_ERR(phydev
);
469 goto err_out_unregister_bus
;
472 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
473 bp
->mii_bus
->irq
[i
] = PHY_POLL
;
476 bp
->mii_bus
->phy_mask
= pdata
->phy_mask
;
478 err
= mdiobus_register(bp
->mii_bus
);
482 goto err_out_free_mdio_irq
;
484 err
= macb_mii_probe(bp
->dev
);
486 goto err_out_unregister_bus
;
490 err_out_unregister_bus
:
491 mdiobus_unregister(bp
->mii_bus
);
492 err_out_free_mdio_irq
:
493 kfree(bp
->mii_bus
->irq
);
494 err_out_free_mdiobus
:
495 mdiobus_free(bp
->mii_bus
);
500 static void macb_update_stats(struct macb
*bp
)
502 u32
*p
= &bp
->hw_stats
.macb
.rx_pause_frames
;
503 u32
*end
= &bp
->hw_stats
.macb
.tx_pause_frames
+ 1;
504 int offset
= MACB_PFR
;
506 WARN_ON((unsigned long)(end
- p
- 1) != (MACB_TPF
- MACB_PFR
) / 4);
508 for(; p
< end
; p
++, offset
+= 4)
509 *p
+= bp
->macb_reg_readl(bp
, offset
);
512 static int macb_halt_tx(struct macb
*bp
)
514 unsigned long halt_time
, timeout
;
517 macb_writel(bp
, NCR
, macb_readl(bp
, NCR
) | MACB_BIT(THALT
));
519 timeout
= jiffies
+ usecs_to_jiffies(MACB_HALT_TIMEOUT
);
522 status
= macb_readl(bp
, TSR
);
523 if (!(status
& MACB_BIT(TGO
)))
526 usleep_range(10, 250);
527 } while (time_before(halt_time
, timeout
));
532 static void macb_tx_unmap(struct macb
*bp
, struct macb_tx_skb
*tx_skb
)
534 if (tx_skb
->mapping
) {
535 if (tx_skb
->mapped_as_page
)
536 dma_unmap_page(&bp
->pdev
->dev
, tx_skb
->mapping
,
537 tx_skb
->size
, DMA_TO_DEVICE
);
539 dma_unmap_single(&bp
->pdev
->dev
, tx_skb
->mapping
,
540 tx_skb
->size
, DMA_TO_DEVICE
);
545 dev_kfree_skb_any(tx_skb
->skb
);
550 static void macb_tx_error_task(struct work_struct
*work
)
552 struct macb_queue
*queue
= container_of(work
, struct macb_queue
,
554 struct macb
*bp
= queue
->bp
;
555 struct macb_tx_skb
*tx_skb
;
556 struct macb_dma_desc
*desc
;
561 netdev_vdbg(bp
->dev
, "macb_tx_error_task: q = %u, t = %u, h = %u\n",
562 (unsigned int)(queue
- bp
->queues
),
563 queue
->tx_tail
, queue
->tx_head
);
565 /* Prevent the queue IRQ handlers from running: each of them may call
566 * macb_tx_interrupt(), which in turn may call netif_wake_subqueue().
567 * As explained below, we have to halt the transmission before updating
568 * TBQP registers so we call netif_tx_stop_all_queues() to notify the
569 * network engine about the macb/gem being halted.
571 spin_lock_irqsave(&bp
->lock
, flags
);
573 /* Make sure nobody is trying to queue up new packets */
574 netif_tx_stop_all_queues(bp
->dev
);
577 * Stop transmission now
578 * (in case we have just queued new packets)
579 * macb/gem must be halted to write TBQP register
581 if (macb_halt_tx(bp
))
582 /* Just complain for now, reinitializing TX path can be good */
583 netdev_err(bp
->dev
, "BUG: halt tx timed out\n");
586 * Treat frames in TX queue including the ones that caused the error.
587 * Free transmit buffers in upper layer.
589 for (tail
= queue
->tx_tail
; tail
!= queue
->tx_head
; tail
++) {
592 desc
= macb_tx_desc(queue
, tail
);
594 tx_skb
= macb_tx_skb(queue
, tail
);
597 if (ctrl
& MACB_BIT(TX_USED
)) {
598 /* skb is set for the last buffer of the frame */
600 macb_tx_unmap(bp
, tx_skb
);
602 tx_skb
= macb_tx_skb(queue
, tail
);
606 /* ctrl still refers to the first buffer descriptor
607 * since it's the only one written back by the hardware
609 if (!(ctrl
& MACB_BIT(TX_BUF_EXHAUSTED
))) {
610 netdev_vdbg(bp
->dev
, "txerr skb %u (data %p) TX complete\n",
611 macb_tx_ring_wrap(tail
), skb
->data
);
612 bp
->stats
.tx_packets
++;
613 bp
->stats
.tx_bytes
+= skb
->len
;
617 * "Buffers exhausted mid-frame" errors may only happen
618 * if the driver is buggy, so complain loudly about those.
619 * Statistics are updated by hardware.
621 if (ctrl
& MACB_BIT(TX_BUF_EXHAUSTED
))
623 "BUG: TX buffers exhausted mid-frame\n");
625 desc
->ctrl
= ctrl
| MACB_BIT(TX_USED
);
628 macb_tx_unmap(bp
, tx_skb
);
631 /* Set end of TX queue */
632 desc
= macb_tx_desc(queue
, 0);
634 desc
->ctrl
= MACB_BIT(TX_USED
);
636 /* Make descriptor updates visible to hardware */
639 /* Reinitialize the TX desc queue */
640 queue_writel(queue
, TBQP
, queue
->tx_ring_dma
);
641 /* Make TX ring reflect state of hardware */
645 /* Housework before enabling TX IRQ */
646 macb_writel(bp
, TSR
, macb_readl(bp
, TSR
));
647 queue_writel(queue
, IER
, MACB_TX_INT_FLAGS
);
649 /* Now we are ready to start transmission again */
650 netif_tx_start_all_queues(bp
->dev
);
651 macb_writel(bp
, NCR
, macb_readl(bp
, NCR
) | MACB_BIT(TSTART
));
653 spin_unlock_irqrestore(&bp
->lock
, flags
);
656 static void macb_tx_interrupt(struct macb_queue
*queue
)
661 struct macb
*bp
= queue
->bp
;
662 u16 queue_index
= queue
- bp
->queues
;
664 status
= macb_readl(bp
, TSR
);
665 macb_writel(bp
, TSR
, status
);
667 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
668 queue_writel(queue
, ISR
, MACB_BIT(TCOMP
));
670 netdev_vdbg(bp
->dev
, "macb_tx_interrupt status = 0x%03lx\n",
671 (unsigned long)status
);
673 head
= queue
->tx_head
;
674 for (tail
= queue
->tx_tail
; tail
!= head
; tail
++) {
675 struct macb_tx_skb
*tx_skb
;
677 struct macb_dma_desc
*desc
;
680 desc
= macb_tx_desc(queue
, tail
);
682 /* Make hw descriptor updates visible to CPU */
687 /* TX_USED bit is only set by hardware on the very first buffer
688 * descriptor of the transmitted frame.
690 if (!(ctrl
& MACB_BIT(TX_USED
)))
693 /* Process all buffers of the current transmitted frame */
695 tx_skb
= macb_tx_skb(queue
, tail
);
698 /* First, update TX stats if needed */
700 netdev_vdbg(bp
->dev
, "skb %u (data %p) TX complete\n",
701 macb_tx_ring_wrap(tail
), skb
->data
);
702 bp
->stats
.tx_packets
++;
703 bp
->stats
.tx_bytes
+= skb
->len
;
706 /* Now we can safely release resources */
707 macb_tx_unmap(bp
, tx_skb
);
709 /* skb is set only for the last buffer of the frame.
710 * WARNING: at this point skb has been freed by
718 queue
->tx_tail
= tail
;
719 if (__netif_subqueue_stopped(bp
->dev
, queue_index
) &&
720 CIRC_CNT(queue
->tx_head
, queue
->tx_tail
,
721 TX_RING_SIZE
) <= MACB_TX_WAKEUP_THRESH
)
722 netif_wake_subqueue(bp
->dev
, queue_index
);
725 static void gem_rx_refill(struct macb
*bp
)
731 while (CIRC_SPACE(bp
->rx_prepared_head
, bp
->rx_tail
, RX_RING_SIZE
) > 0) {
732 entry
= macb_rx_ring_wrap(bp
->rx_prepared_head
);
734 /* Make hw descriptor updates visible to CPU */
737 bp
->rx_prepared_head
++;
739 if (bp
->rx_skbuff
[entry
] == NULL
) {
740 /* allocate sk_buff for this free entry in ring */
741 skb
= netdev_alloc_skb(bp
->dev
, bp
->rx_buffer_size
);
742 if (unlikely(skb
== NULL
)) {
744 "Unable to allocate sk_buff\n");
748 /* now fill corresponding descriptor entry */
749 paddr
= dma_map_single(&bp
->pdev
->dev
, skb
->data
,
750 bp
->rx_buffer_size
, DMA_FROM_DEVICE
);
751 if (dma_mapping_error(&bp
->pdev
->dev
, paddr
)) {
756 bp
->rx_skbuff
[entry
] = skb
;
758 if (entry
== RX_RING_SIZE
- 1)
759 paddr
|= MACB_BIT(RX_WRAP
);
760 bp
->rx_ring
[entry
].addr
= paddr
;
761 bp
->rx_ring
[entry
].ctrl
= 0;
763 /* properly align Ethernet header */
764 skb_reserve(skb
, NET_IP_ALIGN
);
766 bp
->rx_ring
[entry
].addr
&= ~MACB_BIT(RX_USED
);
767 bp
->rx_ring
[entry
].ctrl
= 0;
771 /* Make descriptor updates visible to hardware */
774 netdev_vdbg(bp
->dev
, "rx ring: prepared head %d, tail %d\n",
775 bp
->rx_prepared_head
, bp
->rx_tail
);
778 /* Mark DMA descriptors from begin up to and not including end as unused */
779 static void discard_partial_frame(struct macb
*bp
, unsigned int begin
,
784 for (frag
= begin
; frag
!= end
; frag
++) {
785 struct macb_dma_desc
*desc
= macb_rx_desc(bp
, frag
);
786 desc
->addr
&= ~MACB_BIT(RX_USED
);
789 /* Make descriptor updates visible to hardware */
793 * When this happens, the hardware stats registers for
794 * whatever caused this is updated, so we don't have to record
799 static int gem_rx(struct macb
*bp
, int budget
)
804 struct macb_dma_desc
*desc
;
807 while (count
< budget
) {
810 entry
= macb_rx_ring_wrap(bp
->rx_tail
);
811 desc
= &bp
->rx_ring
[entry
];
813 /* Make hw descriptor updates visible to CPU */
819 if (!(addr
& MACB_BIT(RX_USED
)))
825 if (!(ctrl
& MACB_BIT(RX_SOF
) && ctrl
& MACB_BIT(RX_EOF
))) {
827 "not whole frame pointed by descriptor\n");
828 bp
->stats
.rx_dropped
++;
831 skb
= bp
->rx_skbuff
[entry
];
832 if (unlikely(!skb
)) {
834 "inconsistent Rx descriptor chain\n");
835 bp
->stats
.rx_dropped
++;
838 /* now everything is ready for receiving packet */
839 bp
->rx_skbuff
[entry
] = NULL
;
840 len
= ctrl
& bp
->rx_frm_len_mask
;
842 netdev_vdbg(bp
->dev
, "gem_rx %u (len %u)\n", entry
, len
);
845 addr
= MACB_BF(RX_WADDR
, MACB_BFEXT(RX_WADDR
, addr
));
846 dma_unmap_single(&bp
->pdev
->dev
, addr
,
847 bp
->rx_buffer_size
, DMA_FROM_DEVICE
);
849 skb
->protocol
= eth_type_trans(skb
, bp
->dev
);
850 skb_checksum_none_assert(skb
);
851 if (bp
->dev
->features
& NETIF_F_RXCSUM
&&
852 !(bp
->dev
->flags
& IFF_PROMISC
) &&
853 GEM_BFEXT(RX_CSUM
, ctrl
) & GEM_RX_CSUM_CHECKED_MASK
)
854 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
856 bp
->stats
.rx_packets
++;
857 bp
->stats
.rx_bytes
+= skb
->len
;
859 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
860 netdev_vdbg(bp
->dev
, "received skb of length %u, csum: %08x\n",
861 skb
->len
, skb
->csum
);
862 print_hex_dump(KERN_DEBUG
, " mac: ", DUMP_PREFIX_ADDRESS
, 16, 1,
863 skb_mac_header(skb
), 16, true);
864 print_hex_dump(KERN_DEBUG
, "data: ", DUMP_PREFIX_ADDRESS
, 16, 1,
865 skb
->data
, 32, true);
868 netif_receive_skb(skb
);
876 static int macb_rx_frame(struct macb
*bp
, unsigned int first_frag
,
877 unsigned int last_frag
)
883 struct macb_dma_desc
*desc
;
885 desc
= macb_rx_desc(bp
, last_frag
);
886 len
= desc
->ctrl
& bp
->rx_frm_len_mask
;
888 netdev_vdbg(bp
->dev
, "macb_rx_frame frags %u - %u (len %u)\n",
889 macb_rx_ring_wrap(first_frag
),
890 macb_rx_ring_wrap(last_frag
), len
);
893 * The ethernet header starts NET_IP_ALIGN bytes into the
894 * first buffer. Since the header is 14 bytes, this makes the
895 * payload word-aligned.
897 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
898 * the two padding bytes into the skb so that we avoid hitting
899 * the slowpath in memcpy(), and pull them off afterwards.
901 skb
= netdev_alloc_skb(bp
->dev
, len
+ NET_IP_ALIGN
);
903 bp
->stats
.rx_dropped
++;
904 for (frag
= first_frag
; ; frag
++) {
905 desc
= macb_rx_desc(bp
, frag
);
906 desc
->addr
&= ~MACB_BIT(RX_USED
);
907 if (frag
== last_frag
)
911 /* Make descriptor updates visible to hardware */
919 skb_checksum_none_assert(skb
);
922 for (frag
= first_frag
; ; frag
++) {
923 unsigned int frag_len
= bp
->rx_buffer_size
;
925 if (offset
+ frag_len
> len
) {
926 BUG_ON(frag
!= last_frag
);
927 frag_len
= len
- offset
;
929 skb_copy_to_linear_data_offset(skb
, offset
,
930 macb_rx_buffer(bp
, frag
), frag_len
);
931 offset
+= bp
->rx_buffer_size
;
932 desc
= macb_rx_desc(bp
, frag
);
933 desc
->addr
&= ~MACB_BIT(RX_USED
);
935 if (frag
== last_frag
)
939 /* Make descriptor updates visible to hardware */
942 __skb_pull(skb
, NET_IP_ALIGN
);
943 skb
->protocol
= eth_type_trans(skb
, bp
->dev
);
945 bp
->stats
.rx_packets
++;
946 bp
->stats
.rx_bytes
+= skb
->len
;
947 netdev_vdbg(bp
->dev
, "received skb of length %u, csum: %08x\n",
948 skb
->len
, skb
->csum
);
949 netif_receive_skb(skb
);
954 static int macb_rx(struct macb
*bp
, int budget
)
960 for (tail
= bp
->rx_tail
; budget
> 0; tail
++) {
961 struct macb_dma_desc
*desc
= macb_rx_desc(bp
, tail
);
964 /* Make hw descriptor updates visible to CPU */
970 if (!(addr
& MACB_BIT(RX_USED
)))
973 if (ctrl
& MACB_BIT(RX_SOF
)) {
974 if (first_frag
!= -1)
975 discard_partial_frame(bp
, first_frag
, tail
);
979 if (ctrl
& MACB_BIT(RX_EOF
)) {
981 BUG_ON(first_frag
== -1);
983 dropped
= macb_rx_frame(bp
, first_frag
, tail
);
992 if (first_frag
!= -1)
993 bp
->rx_tail
= first_frag
;
1000 static int macb_poll(struct napi_struct
*napi
, int budget
)
1002 struct macb
*bp
= container_of(napi
, struct macb
, napi
);
1006 status
= macb_readl(bp
, RSR
);
1007 macb_writel(bp
, RSR
, status
);
1011 netdev_vdbg(bp
->dev
, "poll: status = %08lx, budget = %d\n",
1012 (unsigned long)status
, budget
);
1014 work_done
= bp
->macbgem_ops
.mog_rx(bp
, budget
);
1015 if (work_done
< budget
) {
1016 napi_complete(napi
);
1018 /* Packets received while interrupts were disabled */
1019 status
= macb_readl(bp
, RSR
);
1021 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1022 macb_writel(bp
, ISR
, MACB_BIT(RCOMP
));
1023 napi_reschedule(napi
);
1025 macb_writel(bp
, IER
, MACB_RX_INT_FLAGS
);
1029 /* TODO: Handle errors */
1034 static irqreturn_t
macb_interrupt(int irq
, void *dev_id
)
1036 struct macb_queue
*queue
= dev_id
;
1037 struct macb
*bp
= queue
->bp
;
1038 struct net_device
*dev
= bp
->dev
;
1041 status
= queue_readl(queue
, ISR
);
1043 if (unlikely(!status
))
1046 spin_lock(&bp
->lock
);
1049 /* close possible race with dev_close */
1050 if (unlikely(!netif_running(dev
))) {
1051 queue_writel(queue
, IDR
, -1);
1055 netdev_vdbg(bp
->dev
, "queue = %u, isr = 0x%08lx\n",
1056 (unsigned int)(queue
- bp
->queues
),
1057 (unsigned long)status
);
1059 if (status
& MACB_RX_INT_FLAGS
) {
1061 * There's no point taking any more interrupts
1062 * until we have processed the buffers. The
1063 * scheduling call may fail if the poll routine
1064 * is already scheduled, so disable interrupts
1067 queue_writel(queue
, IDR
, MACB_RX_INT_FLAGS
);
1068 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1069 queue_writel(queue
, ISR
, MACB_BIT(RCOMP
));
1071 if (napi_schedule_prep(&bp
->napi
)) {
1072 netdev_vdbg(bp
->dev
, "scheduling RX softirq\n");
1073 __napi_schedule(&bp
->napi
);
1077 if (unlikely(status
& (MACB_TX_ERR_FLAGS
))) {
1078 queue_writel(queue
, IDR
, MACB_TX_INT_FLAGS
);
1079 schedule_work(&queue
->tx_error_task
);
1081 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1082 queue_writel(queue
, ISR
, MACB_TX_ERR_FLAGS
);
1087 if (status
& MACB_BIT(TCOMP
))
1088 macb_tx_interrupt(queue
);
1091 * Link change detection isn't possible with RMII, so we'll
1092 * add that if/when we get our hands on a full-blown MII PHY.
1095 /* There is a hardware issue under heavy load where DMA can
1096 * stop, this causes endless "used buffer descriptor read"
1097 * interrupts but it can be cleared by re-enabling RX. See
1098 * the at91 manual, section 41.3.1 or the Zynq manual
1099 * section 16.7.4 for details.
1101 if (status
& MACB_BIT(RXUBR
)) {
1102 ctrl
= macb_readl(bp
, NCR
);
1103 macb_writel(bp
, NCR
, ctrl
& ~MACB_BIT(RE
));
1104 macb_writel(bp
, NCR
, ctrl
| MACB_BIT(RE
));
1106 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1107 macb_writel(bp
, ISR
, MACB_BIT(RXUBR
));
1110 if (status
& MACB_BIT(ISR_ROVR
)) {
1111 /* We missed at least one packet */
1112 if (macb_is_gem(bp
))
1113 bp
->hw_stats
.gem
.rx_overruns
++;
1115 bp
->hw_stats
.macb
.rx_overruns
++;
1117 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1118 queue_writel(queue
, ISR
, MACB_BIT(ISR_ROVR
));
1121 if (status
& MACB_BIT(HRESP
)) {
1123 * TODO: Reset the hardware, and maybe move the
1124 * netdev_err to a lower-priority context as well
1127 netdev_err(dev
, "DMA bus error: HRESP not OK\n");
1129 if (bp
->caps
& MACB_CAPS_ISR_CLEAR_ON_WRITE
)
1130 queue_writel(queue
, ISR
, MACB_BIT(HRESP
));
1133 status
= queue_readl(queue
, ISR
);
1136 spin_unlock(&bp
->lock
);
1141 #ifdef CONFIG_NET_POLL_CONTROLLER
1143 * Polling receive - used by netconsole and other diagnostic tools
1144 * to allow network i/o with interrupts disabled.
1146 static void macb_poll_controller(struct net_device
*dev
)
1148 struct macb
*bp
= netdev_priv(dev
);
1149 struct macb_queue
*queue
;
1150 unsigned long flags
;
1153 local_irq_save(flags
);
1154 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
)
1155 macb_interrupt(dev
->irq
, queue
);
1156 local_irq_restore(flags
);
1160 static unsigned int macb_tx_map(struct macb
*bp
,
1161 struct macb_queue
*queue
,
1162 struct sk_buff
*skb
)
1165 unsigned int len
, entry
, i
, tx_head
= queue
->tx_head
;
1166 struct macb_tx_skb
*tx_skb
= NULL
;
1167 struct macb_dma_desc
*desc
;
1168 unsigned int offset
, size
, count
= 0;
1169 unsigned int f
, nr_frags
= skb_shinfo(skb
)->nr_frags
;
1170 unsigned int eof
= 1;
1173 /* First, map non-paged data */
1174 len
= skb_headlen(skb
);
1177 size
= min(len
, bp
->max_tx_length
);
1178 entry
= macb_tx_ring_wrap(tx_head
);
1179 tx_skb
= &queue
->tx_skb
[entry
];
1181 mapping
= dma_map_single(&bp
->pdev
->dev
,
1183 size
, DMA_TO_DEVICE
);
1184 if (dma_mapping_error(&bp
->pdev
->dev
, mapping
))
1187 /* Save info to properly release resources */
1189 tx_skb
->mapping
= mapping
;
1190 tx_skb
->size
= size
;
1191 tx_skb
->mapped_as_page
= false;
1199 /* Then, map paged data from fragments */
1200 for (f
= 0; f
< nr_frags
; f
++) {
1201 const skb_frag_t
*frag
= &skb_shinfo(skb
)->frags
[f
];
1203 len
= skb_frag_size(frag
);
1206 size
= min(len
, bp
->max_tx_length
);
1207 entry
= macb_tx_ring_wrap(tx_head
);
1208 tx_skb
= &queue
->tx_skb
[entry
];
1210 mapping
= skb_frag_dma_map(&bp
->pdev
->dev
, frag
,
1211 offset
, size
, DMA_TO_DEVICE
);
1212 if (dma_mapping_error(&bp
->pdev
->dev
, mapping
))
1215 /* Save info to properly release resources */
1217 tx_skb
->mapping
= mapping
;
1218 tx_skb
->size
= size
;
1219 tx_skb
->mapped_as_page
= true;
1228 /* Should never happen */
1229 if (unlikely(tx_skb
== NULL
)) {
1230 netdev_err(bp
->dev
, "BUG! empty skb!\n");
1234 /* This is the last buffer of the frame: save socket buffer */
1237 /* Update TX ring: update buffer descriptors in reverse order
1238 * to avoid race condition
1241 /* Set 'TX_USED' bit in buffer descriptor at tx_head position
1242 * to set the end of TX queue
1245 entry
= macb_tx_ring_wrap(i
);
1246 ctrl
= MACB_BIT(TX_USED
);
1247 desc
= &queue
->tx_ring
[entry
];
1252 entry
= macb_tx_ring_wrap(i
);
1253 tx_skb
= &queue
->tx_skb
[entry
];
1254 desc
= &queue
->tx_ring
[entry
];
1256 ctrl
= (u32
)tx_skb
->size
;
1258 ctrl
|= MACB_BIT(TX_LAST
);
1261 if (unlikely(entry
== (TX_RING_SIZE
- 1)))
1262 ctrl
|= MACB_BIT(TX_WRAP
);
1264 /* Set TX buffer descriptor */
1265 desc
->addr
= tx_skb
->mapping
;
1266 /* desc->addr must be visible to hardware before clearing
1267 * 'TX_USED' bit in desc->ctrl.
1271 } while (i
!= queue
->tx_head
);
1273 queue
->tx_head
= tx_head
;
1278 netdev_err(bp
->dev
, "TX DMA map failed\n");
1280 for (i
= queue
->tx_head
; i
!= tx_head
; i
++) {
1281 tx_skb
= macb_tx_skb(queue
, i
);
1283 macb_tx_unmap(bp
, tx_skb
);
1289 static int macb_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
1291 u16 queue_index
= skb_get_queue_mapping(skb
);
1292 struct macb
*bp
= netdev_priv(dev
);
1293 struct macb_queue
*queue
= &bp
->queues
[queue_index
];
1294 unsigned long flags
;
1295 unsigned int count
, nr_frags
, frag_size
, f
;
1297 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
1298 netdev_vdbg(bp
->dev
,
1299 "start_xmit: queue %hu len %u head %p data %p tail %p end %p\n",
1300 queue_index
, skb
->len
, skb
->head
, skb
->data
,
1301 skb_tail_pointer(skb
), skb_end_pointer(skb
));
1302 print_hex_dump(KERN_DEBUG
, "data: ", DUMP_PREFIX_OFFSET
, 16, 1,
1303 skb
->data
, 16, true);
1306 /* Count how many TX buffer descriptors are needed to send this
1307 * socket buffer: skb fragments of jumbo frames may need to be
1308 * splitted into many buffer descriptors.
1310 count
= DIV_ROUND_UP(skb_headlen(skb
), bp
->max_tx_length
);
1311 nr_frags
= skb_shinfo(skb
)->nr_frags
;
1312 for (f
= 0; f
< nr_frags
; f
++) {
1313 frag_size
= skb_frag_size(&skb_shinfo(skb
)->frags
[f
]);
1314 count
+= DIV_ROUND_UP(frag_size
, bp
->max_tx_length
);
1317 spin_lock_irqsave(&bp
->lock
, flags
);
1319 /* This is a hard error, log it. */
1320 if (CIRC_SPACE(queue
->tx_head
, queue
->tx_tail
, TX_RING_SIZE
) < count
) {
1321 netif_stop_subqueue(dev
, queue_index
);
1322 spin_unlock_irqrestore(&bp
->lock
, flags
);
1323 netdev_dbg(bp
->dev
, "tx_head = %u, tx_tail = %u\n",
1324 queue
->tx_head
, queue
->tx_tail
);
1325 return NETDEV_TX_BUSY
;
1328 /* Map socket buffer for DMA transfer */
1329 if (!macb_tx_map(bp
, queue
, skb
)) {
1330 dev_kfree_skb_any(skb
);
1334 /* Make newly initialized descriptor visible to hardware */
1337 skb_tx_timestamp(skb
);
1339 macb_writel(bp
, NCR
, macb_readl(bp
, NCR
) | MACB_BIT(TSTART
));
1341 if (CIRC_SPACE(queue
->tx_head
, queue
->tx_tail
, TX_RING_SIZE
) < 1)
1342 netif_stop_subqueue(dev
, queue_index
);
1345 spin_unlock_irqrestore(&bp
->lock
, flags
);
1347 return NETDEV_TX_OK
;
1350 static void macb_init_rx_buffer_size(struct macb
*bp
, size_t size
)
1352 if (!macb_is_gem(bp
)) {
1353 bp
->rx_buffer_size
= MACB_RX_BUFFER_SIZE
;
1355 bp
->rx_buffer_size
= size
;
1357 if (bp
->rx_buffer_size
% RX_BUFFER_MULTIPLE
) {
1359 "RX buffer must be multiple of %d bytes, expanding\n",
1360 RX_BUFFER_MULTIPLE
);
1361 bp
->rx_buffer_size
=
1362 roundup(bp
->rx_buffer_size
, RX_BUFFER_MULTIPLE
);
1366 netdev_dbg(bp
->dev
, "mtu [%u] rx_buffer_size [%Zu]\n",
1367 bp
->dev
->mtu
, bp
->rx_buffer_size
);
1370 static void gem_free_rx_buffers(struct macb
*bp
)
1372 struct sk_buff
*skb
;
1373 struct macb_dma_desc
*desc
;
1380 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1381 skb
= bp
->rx_skbuff
[i
];
1386 desc
= &bp
->rx_ring
[i
];
1387 addr
= MACB_BF(RX_WADDR
, MACB_BFEXT(RX_WADDR
, desc
->addr
));
1388 dma_unmap_single(&bp
->pdev
->dev
, addr
, bp
->rx_buffer_size
,
1390 dev_kfree_skb_any(skb
);
1394 kfree(bp
->rx_skbuff
);
1395 bp
->rx_skbuff
= NULL
;
1398 static void macb_free_rx_buffers(struct macb
*bp
)
1400 if (bp
->rx_buffers
) {
1401 dma_free_coherent(&bp
->pdev
->dev
,
1402 RX_RING_SIZE
* bp
->rx_buffer_size
,
1403 bp
->rx_buffers
, bp
->rx_buffers_dma
);
1404 bp
->rx_buffers
= NULL
;
1408 static void macb_free_consistent(struct macb
*bp
)
1410 struct macb_queue
*queue
;
1413 bp
->macbgem_ops
.mog_free_rx_buffers(bp
);
1415 dma_free_coherent(&bp
->pdev
->dev
, RX_RING_BYTES
,
1416 bp
->rx_ring
, bp
->rx_ring_dma
);
1420 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
) {
1421 kfree(queue
->tx_skb
);
1422 queue
->tx_skb
= NULL
;
1423 if (queue
->tx_ring
) {
1424 dma_free_coherent(&bp
->pdev
->dev
, TX_RING_BYTES
,
1425 queue
->tx_ring
, queue
->tx_ring_dma
);
1426 queue
->tx_ring
= NULL
;
1431 static int gem_alloc_rx_buffers(struct macb
*bp
)
1435 size
= RX_RING_SIZE
* sizeof(struct sk_buff
*);
1436 bp
->rx_skbuff
= kzalloc(size
, GFP_KERNEL
);
1441 "Allocated %d RX struct sk_buff entries at %p\n",
1442 RX_RING_SIZE
, bp
->rx_skbuff
);
1446 static int macb_alloc_rx_buffers(struct macb
*bp
)
1450 size
= RX_RING_SIZE
* bp
->rx_buffer_size
;
1451 bp
->rx_buffers
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
1452 &bp
->rx_buffers_dma
, GFP_KERNEL
);
1453 if (!bp
->rx_buffers
)
1457 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
1458 size
, (unsigned long)bp
->rx_buffers_dma
, bp
->rx_buffers
);
1462 static int macb_alloc_consistent(struct macb
*bp
)
1464 struct macb_queue
*queue
;
1468 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
) {
1469 size
= TX_RING_BYTES
;
1470 queue
->tx_ring
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
1471 &queue
->tx_ring_dma
,
1473 if (!queue
->tx_ring
)
1476 "Allocated TX ring for queue %u of %d bytes at %08lx (mapped %p)\n",
1477 q
, size
, (unsigned long)queue
->tx_ring_dma
,
1480 size
= TX_RING_SIZE
* sizeof(struct macb_tx_skb
);
1481 queue
->tx_skb
= kmalloc(size
, GFP_KERNEL
);
1486 size
= RX_RING_BYTES
;
1487 bp
->rx_ring
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
1488 &bp
->rx_ring_dma
, GFP_KERNEL
);
1492 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
1493 size
, (unsigned long)bp
->rx_ring_dma
, bp
->rx_ring
);
1495 if (bp
->macbgem_ops
.mog_alloc_rx_buffers(bp
))
1501 macb_free_consistent(bp
);
1505 static void gem_init_rings(struct macb
*bp
)
1507 struct macb_queue
*queue
;
1511 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
) {
1512 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1513 queue
->tx_ring
[i
].addr
= 0;
1514 queue
->tx_ring
[i
].ctrl
= MACB_BIT(TX_USED
);
1516 queue
->tx_ring
[TX_RING_SIZE
- 1].ctrl
|= MACB_BIT(TX_WRAP
);
1522 bp
->rx_prepared_head
= 0;
1527 static void macb_init_rings(struct macb
*bp
)
1532 addr
= bp
->rx_buffers_dma
;
1533 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
1534 bp
->rx_ring
[i
].addr
= addr
;
1535 bp
->rx_ring
[i
].ctrl
= 0;
1536 addr
+= bp
->rx_buffer_size
;
1538 bp
->rx_ring
[RX_RING_SIZE
- 1].addr
|= MACB_BIT(RX_WRAP
);
1540 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
1541 bp
->queues
[0].tx_ring
[i
].addr
= 0;
1542 bp
->queues
[0].tx_ring
[i
].ctrl
= MACB_BIT(TX_USED
);
1544 bp
->queues
[0].tx_head
= 0;
1545 bp
->queues
[0].tx_tail
= 0;
1546 bp
->queues
[0].tx_ring
[TX_RING_SIZE
- 1].ctrl
|= MACB_BIT(TX_WRAP
);
1551 static void macb_reset_hw(struct macb
*bp
)
1553 struct macb_queue
*queue
;
1557 * Disable RX and TX (XXX: Should we halt the transmission
1560 macb_writel(bp
, NCR
, 0);
1562 /* Clear the stats registers (XXX: Update stats first?) */
1563 macb_writel(bp
, NCR
, MACB_BIT(CLRSTAT
));
1565 /* Clear all status flags */
1566 macb_writel(bp
, TSR
, -1);
1567 macb_writel(bp
, RSR
, -1);
1569 /* Disable all interrupts */
1570 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
) {
1571 queue_writel(queue
, IDR
, -1);
1572 queue_readl(queue
, ISR
);
1576 static u32
gem_mdc_clk_div(struct macb
*bp
)
1579 unsigned long pclk_hz
= clk_get_rate(bp
->pclk
);
1581 if (pclk_hz
<= 20000000)
1582 config
= GEM_BF(CLK
, GEM_CLK_DIV8
);
1583 else if (pclk_hz
<= 40000000)
1584 config
= GEM_BF(CLK
, GEM_CLK_DIV16
);
1585 else if (pclk_hz
<= 80000000)
1586 config
= GEM_BF(CLK
, GEM_CLK_DIV32
);
1587 else if (pclk_hz
<= 120000000)
1588 config
= GEM_BF(CLK
, GEM_CLK_DIV48
);
1589 else if (pclk_hz
<= 160000000)
1590 config
= GEM_BF(CLK
, GEM_CLK_DIV64
);
1592 config
= GEM_BF(CLK
, GEM_CLK_DIV96
);
1597 static u32
macb_mdc_clk_div(struct macb
*bp
)
1600 unsigned long pclk_hz
;
1602 if (macb_is_gem(bp
))
1603 return gem_mdc_clk_div(bp
);
1605 pclk_hz
= clk_get_rate(bp
->pclk
);
1606 if (pclk_hz
<= 20000000)
1607 config
= MACB_BF(CLK
, MACB_CLK_DIV8
);
1608 else if (pclk_hz
<= 40000000)
1609 config
= MACB_BF(CLK
, MACB_CLK_DIV16
);
1610 else if (pclk_hz
<= 80000000)
1611 config
= MACB_BF(CLK
, MACB_CLK_DIV32
);
1613 config
= MACB_BF(CLK
, MACB_CLK_DIV64
);
1619 * Get the DMA bus width field of the network configuration register that we
1620 * should program. We find the width from decoding the design configuration
1621 * register to find the maximum supported data bus width.
1623 static u32
macb_dbw(struct macb
*bp
)
1625 if (!macb_is_gem(bp
))
1628 switch (GEM_BFEXT(DBWDEF
, gem_readl(bp
, DCFG1
))) {
1630 return GEM_BF(DBW
, GEM_DBW128
);
1632 return GEM_BF(DBW
, GEM_DBW64
);
1635 return GEM_BF(DBW
, GEM_DBW32
);
1640 * Configure the receive DMA engine
1641 * - use the correct receive buffer size
1642 * - set best burst length for DMA operations
1643 * (if not supported by FIFO, it will fallback to default)
1644 * - set both rx/tx packet buffers to full memory size
1645 * These are configurable parameters for GEM.
1647 static void macb_configure_dma(struct macb
*bp
)
1651 if (macb_is_gem(bp
)) {
1652 dmacfg
= gem_readl(bp
, DMACFG
) & ~GEM_BF(RXBS
, -1L);
1653 dmacfg
|= GEM_BF(RXBS
, bp
->rx_buffer_size
/ RX_BUFFER_MULTIPLE
);
1654 if (bp
->dma_burst_length
)
1655 dmacfg
= GEM_BFINS(FBLDO
, bp
->dma_burst_length
, dmacfg
);
1656 dmacfg
|= GEM_BIT(TXPBMS
) | GEM_BF(RXBMS
, -1L);
1657 dmacfg
&= ~GEM_BIT(ENDIA_PKT
);
1660 dmacfg
&= ~GEM_BIT(ENDIA_DESC
);
1662 dmacfg
|= GEM_BIT(ENDIA_DESC
); /* CPU in big endian */
1664 if (bp
->dev
->features
& NETIF_F_HW_CSUM
)
1665 dmacfg
|= GEM_BIT(TXCOEN
);
1667 dmacfg
&= ~GEM_BIT(TXCOEN
);
1668 netdev_dbg(bp
->dev
, "Cadence configure DMA with 0x%08x\n",
1670 gem_writel(bp
, DMACFG
, dmacfg
);
1674 static void macb_init_hw(struct macb
*bp
)
1676 struct macb_queue
*queue
;
1682 macb_set_hwaddr(bp
);
1684 config
= macb_mdc_clk_div(bp
);
1685 config
|= MACB_BF(RBOF
, NET_IP_ALIGN
); /* Make eth data aligned */
1686 config
|= MACB_BIT(PAE
); /* PAuse Enable */
1687 config
|= MACB_BIT(DRFCS
); /* Discard Rx FCS */
1688 if (bp
->caps
& MACB_CAPS_JUMBO
)
1689 config
|= MACB_BIT(JFRAME
); /* Enable jumbo frames */
1691 config
|= MACB_BIT(BIG
); /* Receive oversized frames */
1692 if (bp
->dev
->flags
& IFF_PROMISC
)
1693 config
|= MACB_BIT(CAF
); /* Copy All Frames */
1694 else if (macb_is_gem(bp
) && bp
->dev
->features
& NETIF_F_RXCSUM
)
1695 config
|= GEM_BIT(RXCOEN
);
1696 if (!(bp
->dev
->flags
& IFF_BROADCAST
))
1697 config
|= MACB_BIT(NBC
); /* No BroadCast */
1698 config
|= macb_dbw(bp
);
1699 macb_writel(bp
, NCFGR
, config
);
1700 if ((bp
->caps
& MACB_CAPS_JUMBO
) && bp
->jumbo_max_len
)
1701 gem_writel(bp
, JML
, bp
->jumbo_max_len
);
1702 bp
->speed
= SPEED_10
;
1703 bp
->duplex
= DUPLEX_HALF
;
1704 bp
->rx_frm_len_mask
= MACB_RX_FRMLEN_MASK
;
1705 if (bp
->caps
& MACB_CAPS_JUMBO
)
1706 bp
->rx_frm_len_mask
= MACB_RX_JFRMLEN_MASK
;
1708 macb_configure_dma(bp
);
1710 /* Initialize TX and RX buffers */
1711 macb_writel(bp
, RBQP
, bp
->rx_ring_dma
);
1712 for (q
= 0, queue
= bp
->queues
; q
< bp
->num_queues
; ++q
, ++queue
) {
1713 queue_writel(queue
, TBQP
, queue
->tx_ring_dma
);
1715 /* Enable interrupts */
1716 queue_writel(queue
, IER
,
1722 /* Enable TX and RX */
1723 macb_writel(bp
, NCR
, MACB_BIT(RE
) | MACB_BIT(TE
) | MACB_BIT(MPE
));
1727 * The hash address register is 64 bits long and takes up two
1728 * locations in the memory map. The least significant bits are stored
1729 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1731 * The unicast hash enable and the multicast hash enable bits in the
1732 * network configuration register enable the reception of hash matched
1733 * frames. The destination address is reduced to a 6 bit index into
1734 * the 64 bit hash register using the following hash function. The
1735 * hash function is an exclusive or of every sixth bit of the
1736 * destination address.
1738 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1739 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1740 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1741 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1742 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1743 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1745 * da[0] represents the least significant bit of the first byte
1746 * received, that is, the multicast/unicast indicator, and da[47]
1747 * represents the most significant bit of the last byte received. If
1748 * the hash index, hi[n], points to a bit that is set in the hash
1749 * register then the frame will be matched according to whether the
1750 * frame is multicast or unicast. A multicast match will be signalled
1751 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1752 * index points to a bit set in the hash register. A unicast match
1753 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1754 * and the hash index points to a bit set in the hash register. To
1755 * receive all multicast frames, the hash register should be set with
1756 * all ones and the multicast hash enable bit should be set in the
1757 * network configuration register.
1760 static inline int hash_bit_value(int bitnr
, __u8
*addr
)
1762 if (addr
[bitnr
/ 8] & (1 << (bitnr
% 8)))
1768 * Return the hash index value for the specified address.
1770 static int hash_get_index(__u8
*addr
)
1775 for (j
= 0; j
< 6; j
++) {
1776 for (i
= 0, bitval
= 0; i
< 8; i
++)
1777 bitval
^= hash_bit_value(i
* 6 + j
, addr
);
1779 hash_index
|= (bitval
<< j
);
1786 * Add multicast addresses to the internal multicast-hash table.
1788 static void macb_sethashtable(struct net_device
*dev
)
1790 struct netdev_hw_addr
*ha
;
1791 unsigned long mc_filter
[2];
1793 struct macb
*bp
= netdev_priv(dev
);
1795 mc_filter
[0] = mc_filter
[1] = 0;
1797 netdev_for_each_mc_addr(ha
, dev
) {
1798 bitnr
= hash_get_index(ha
->addr
);
1799 mc_filter
[bitnr
>> 5] |= 1 << (bitnr
& 31);
1802 macb_or_gem_writel(bp
, HRB
, mc_filter
[0]);
1803 macb_or_gem_writel(bp
, HRT
, mc_filter
[1]);
1807 * Enable/Disable promiscuous and multicast modes.
1809 static void macb_set_rx_mode(struct net_device
*dev
)
1812 struct macb
*bp
= netdev_priv(dev
);
1814 cfg
= macb_readl(bp
, NCFGR
);
1816 if (dev
->flags
& IFF_PROMISC
) {
1817 /* Enable promiscuous mode */
1818 cfg
|= MACB_BIT(CAF
);
1820 /* Disable RX checksum offload */
1821 if (macb_is_gem(bp
))
1822 cfg
&= ~GEM_BIT(RXCOEN
);
1824 /* Disable promiscuous mode */
1825 cfg
&= ~MACB_BIT(CAF
);
1827 /* Enable RX checksum offload only if requested */
1828 if (macb_is_gem(bp
) && dev
->features
& NETIF_F_RXCSUM
)
1829 cfg
|= GEM_BIT(RXCOEN
);
1832 if (dev
->flags
& IFF_ALLMULTI
) {
1833 /* Enable all multicast mode */
1834 macb_or_gem_writel(bp
, HRB
, -1);
1835 macb_or_gem_writel(bp
, HRT
, -1);
1836 cfg
|= MACB_BIT(NCFGR_MTI
);
1837 } else if (!netdev_mc_empty(dev
)) {
1838 /* Enable specific multicasts */
1839 macb_sethashtable(dev
);
1840 cfg
|= MACB_BIT(NCFGR_MTI
);
1841 } else if (dev
->flags
& (~IFF_ALLMULTI
)) {
1842 /* Disable all multicast mode */
1843 macb_or_gem_writel(bp
, HRB
, 0);
1844 macb_or_gem_writel(bp
, HRT
, 0);
1845 cfg
&= ~MACB_BIT(NCFGR_MTI
);
1848 macb_writel(bp
, NCFGR
, cfg
);
1851 static int macb_open(struct net_device
*dev
)
1853 struct macb
*bp
= netdev_priv(dev
);
1854 size_t bufsz
= dev
->mtu
+ ETH_HLEN
+ ETH_FCS_LEN
+ NET_IP_ALIGN
;
1857 netdev_dbg(bp
->dev
, "open\n");
1859 /* carrier starts down */
1860 netif_carrier_off(dev
);
1862 /* if the phy is not yet register, retry later*/
1866 /* RX buffers initialization */
1867 macb_init_rx_buffer_size(bp
, bufsz
);
1869 err
= macb_alloc_consistent(bp
);
1871 netdev_err(dev
, "Unable to allocate DMA memory (error %d)\n",
1876 napi_enable(&bp
->napi
);
1878 bp
->macbgem_ops
.mog_init_rings(bp
);
1881 /* schedule a link state check */
1882 phy_start(bp
->phy_dev
);
1884 netif_tx_start_all_queues(dev
);
1889 static int macb_close(struct net_device
*dev
)
1891 struct macb
*bp
= netdev_priv(dev
);
1892 unsigned long flags
;
1894 netif_tx_stop_all_queues(dev
);
1895 napi_disable(&bp
->napi
);
1898 phy_stop(bp
->phy_dev
);
1900 spin_lock_irqsave(&bp
->lock
, flags
);
1902 netif_carrier_off(dev
);
1903 spin_unlock_irqrestore(&bp
->lock
, flags
);
1905 macb_free_consistent(bp
);
1910 static int macb_change_mtu(struct net_device
*dev
, int new_mtu
)
1912 struct macb
*bp
= netdev_priv(dev
);
1915 if (netif_running(dev
))
1918 max_mtu
= ETH_DATA_LEN
;
1919 if (bp
->caps
& MACB_CAPS_JUMBO
)
1920 max_mtu
= gem_readl(bp
, JML
) - ETH_HLEN
- ETH_FCS_LEN
;
1922 if ((new_mtu
> max_mtu
) || (new_mtu
< GEM_MTU_MIN_SIZE
))
1930 static void gem_update_stats(struct macb
*bp
)
1933 u32
*p
= &bp
->hw_stats
.gem
.tx_octets_31_0
;
1935 for (i
= 0; i
< GEM_STATS_LEN
; ++i
, ++p
) {
1936 u32 offset
= gem_statistics
[i
].offset
;
1937 u64 val
= bp
->macb_reg_readl(bp
, offset
);
1939 bp
->ethtool_stats
[i
] += val
;
1942 if (offset
== GEM_OCTTXL
|| offset
== GEM_OCTRXL
) {
1943 /* Add GEM_OCTTXH, GEM_OCTRXH */
1944 val
= bp
->macb_reg_readl(bp
, offset
+ 4);
1945 bp
->ethtool_stats
[i
] += ((u64
)val
) << 32;
1951 static struct net_device_stats
*gem_get_stats(struct macb
*bp
)
1953 struct gem_stats
*hwstat
= &bp
->hw_stats
.gem
;
1954 struct net_device_stats
*nstat
= &bp
->stats
;
1956 gem_update_stats(bp
);
1958 nstat
->rx_errors
= (hwstat
->rx_frame_check_sequence_errors
+
1959 hwstat
->rx_alignment_errors
+
1960 hwstat
->rx_resource_errors
+
1961 hwstat
->rx_overruns
+
1962 hwstat
->rx_oversize_frames
+
1963 hwstat
->rx_jabbers
+
1964 hwstat
->rx_undersized_frames
+
1965 hwstat
->rx_length_field_frame_errors
);
1966 nstat
->tx_errors
= (hwstat
->tx_late_collisions
+
1967 hwstat
->tx_excessive_collisions
+
1968 hwstat
->tx_underrun
+
1969 hwstat
->tx_carrier_sense_errors
);
1970 nstat
->multicast
= hwstat
->rx_multicast_frames
;
1971 nstat
->collisions
= (hwstat
->tx_single_collision_frames
+
1972 hwstat
->tx_multiple_collision_frames
+
1973 hwstat
->tx_excessive_collisions
);
1974 nstat
->rx_length_errors
= (hwstat
->rx_oversize_frames
+
1975 hwstat
->rx_jabbers
+
1976 hwstat
->rx_undersized_frames
+
1977 hwstat
->rx_length_field_frame_errors
);
1978 nstat
->rx_over_errors
= hwstat
->rx_resource_errors
;
1979 nstat
->rx_crc_errors
= hwstat
->rx_frame_check_sequence_errors
;
1980 nstat
->rx_frame_errors
= hwstat
->rx_alignment_errors
;
1981 nstat
->rx_fifo_errors
= hwstat
->rx_overruns
;
1982 nstat
->tx_aborted_errors
= hwstat
->tx_excessive_collisions
;
1983 nstat
->tx_carrier_errors
= hwstat
->tx_carrier_sense_errors
;
1984 nstat
->tx_fifo_errors
= hwstat
->tx_underrun
;
1989 static void gem_get_ethtool_stats(struct net_device
*dev
,
1990 struct ethtool_stats
*stats
, u64
*data
)
1994 bp
= netdev_priv(dev
);
1995 gem_update_stats(bp
);
1996 memcpy(data
, &bp
->ethtool_stats
, sizeof(u64
) * GEM_STATS_LEN
);
1999 static int gem_get_sset_count(struct net_device
*dev
, int sset
)
2003 return GEM_STATS_LEN
;
2009 static void gem_get_ethtool_strings(struct net_device
*dev
, u32 sset
, u8
*p
)
2015 for (i
= 0; i
< GEM_STATS_LEN
; i
++, p
+= ETH_GSTRING_LEN
)
2016 memcpy(p
, gem_statistics
[i
].stat_string
,
2022 static struct net_device_stats
*macb_get_stats(struct net_device
*dev
)
2024 struct macb
*bp
= netdev_priv(dev
);
2025 struct net_device_stats
*nstat
= &bp
->stats
;
2026 struct macb_stats
*hwstat
= &bp
->hw_stats
.macb
;
2028 if (macb_is_gem(bp
))
2029 return gem_get_stats(bp
);
2031 /* read stats from hardware */
2032 macb_update_stats(bp
);
2034 /* Convert HW stats into netdevice stats */
2035 nstat
->rx_errors
= (hwstat
->rx_fcs_errors
+
2036 hwstat
->rx_align_errors
+
2037 hwstat
->rx_resource_errors
+
2038 hwstat
->rx_overruns
+
2039 hwstat
->rx_oversize_pkts
+
2040 hwstat
->rx_jabbers
+
2041 hwstat
->rx_undersize_pkts
+
2042 hwstat
->rx_length_mismatch
);
2043 nstat
->tx_errors
= (hwstat
->tx_late_cols
+
2044 hwstat
->tx_excessive_cols
+
2045 hwstat
->tx_underruns
+
2046 hwstat
->tx_carrier_errors
+
2047 hwstat
->sqe_test_errors
);
2048 nstat
->collisions
= (hwstat
->tx_single_cols
+
2049 hwstat
->tx_multiple_cols
+
2050 hwstat
->tx_excessive_cols
);
2051 nstat
->rx_length_errors
= (hwstat
->rx_oversize_pkts
+
2052 hwstat
->rx_jabbers
+
2053 hwstat
->rx_undersize_pkts
+
2054 hwstat
->rx_length_mismatch
);
2055 nstat
->rx_over_errors
= hwstat
->rx_resource_errors
+
2056 hwstat
->rx_overruns
;
2057 nstat
->rx_crc_errors
= hwstat
->rx_fcs_errors
;
2058 nstat
->rx_frame_errors
= hwstat
->rx_align_errors
;
2059 nstat
->rx_fifo_errors
= hwstat
->rx_overruns
;
2060 /* XXX: What does "missed" mean? */
2061 nstat
->tx_aborted_errors
= hwstat
->tx_excessive_cols
;
2062 nstat
->tx_carrier_errors
= hwstat
->tx_carrier_errors
;
2063 nstat
->tx_fifo_errors
= hwstat
->tx_underruns
;
2064 /* Don't know about heartbeat or window errors... */
2069 static int macb_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2071 struct macb
*bp
= netdev_priv(dev
);
2072 struct phy_device
*phydev
= bp
->phy_dev
;
2077 return phy_ethtool_gset(phydev
, cmd
);
2080 static int macb_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
2082 struct macb
*bp
= netdev_priv(dev
);
2083 struct phy_device
*phydev
= bp
->phy_dev
;
2088 return phy_ethtool_sset(phydev
, cmd
);
2091 static int macb_get_regs_len(struct net_device
*netdev
)
2093 return MACB_GREGS_NBR
* sizeof(u32
);
2096 static void macb_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
2099 struct macb
*bp
= netdev_priv(dev
);
2100 unsigned int tail
, head
;
2103 regs
->version
= (macb_readl(bp
, MID
) & ((1 << MACB_REV_SIZE
) - 1))
2104 | MACB_GREGS_VERSION
;
2106 tail
= macb_tx_ring_wrap(bp
->queues
[0].tx_tail
);
2107 head
= macb_tx_ring_wrap(bp
->queues
[0].tx_head
);
2109 regs_buff
[0] = macb_readl(bp
, NCR
);
2110 regs_buff
[1] = macb_or_gem_readl(bp
, NCFGR
);
2111 regs_buff
[2] = macb_readl(bp
, NSR
);
2112 regs_buff
[3] = macb_readl(bp
, TSR
);
2113 regs_buff
[4] = macb_readl(bp
, RBQP
);
2114 regs_buff
[5] = macb_readl(bp
, TBQP
);
2115 regs_buff
[6] = macb_readl(bp
, RSR
);
2116 regs_buff
[7] = macb_readl(bp
, IMR
);
2118 regs_buff
[8] = tail
;
2119 regs_buff
[9] = head
;
2120 regs_buff
[10] = macb_tx_dma(&bp
->queues
[0], tail
);
2121 regs_buff
[11] = macb_tx_dma(&bp
->queues
[0], head
);
2123 regs_buff
[12] = macb_or_gem_readl(bp
, USRIO
);
2124 if (macb_is_gem(bp
)) {
2125 regs_buff
[13] = gem_readl(bp
, DMACFG
);
2129 static const struct ethtool_ops macb_ethtool_ops
= {
2130 .get_settings
= macb_get_settings
,
2131 .set_settings
= macb_set_settings
,
2132 .get_regs_len
= macb_get_regs_len
,
2133 .get_regs
= macb_get_regs
,
2134 .get_link
= ethtool_op_get_link
,
2135 .get_ts_info
= ethtool_op_get_ts_info
,
2138 static const struct ethtool_ops gem_ethtool_ops
= {
2139 .get_settings
= macb_get_settings
,
2140 .set_settings
= macb_set_settings
,
2141 .get_regs_len
= macb_get_regs_len
,
2142 .get_regs
= macb_get_regs
,
2143 .get_link
= ethtool_op_get_link
,
2144 .get_ts_info
= ethtool_op_get_ts_info
,
2145 .get_ethtool_stats
= gem_get_ethtool_stats
,
2146 .get_strings
= gem_get_ethtool_strings
,
2147 .get_sset_count
= gem_get_sset_count
,
2150 static int macb_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
2152 struct macb
*bp
= netdev_priv(dev
);
2153 struct phy_device
*phydev
= bp
->phy_dev
;
2155 if (!netif_running(dev
))
2161 return phy_mii_ioctl(phydev
, rq
, cmd
);
2164 static int macb_set_features(struct net_device
*netdev
,
2165 netdev_features_t features
)
2167 struct macb
*bp
= netdev_priv(netdev
);
2168 netdev_features_t changed
= features
^ netdev
->features
;
2170 /* TX checksum offload */
2171 if ((changed
& NETIF_F_HW_CSUM
) && macb_is_gem(bp
)) {
2174 dmacfg
= gem_readl(bp
, DMACFG
);
2175 if (features
& NETIF_F_HW_CSUM
)
2176 dmacfg
|= GEM_BIT(TXCOEN
);
2178 dmacfg
&= ~GEM_BIT(TXCOEN
);
2179 gem_writel(bp
, DMACFG
, dmacfg
);
2182 /* RX checksum offload */
2183 if ((changed
& NETIF_F_RXCSUM
) && macb_is_gem(bp
)) {
2186 netcfg
= gem_readl(bp
, NCFGR
);
2187 if (features
& NETIF_F_RXCSUM
&&
2188 !(netdev
->flags
& IFF_PROMISC
))
2189 netcfg
|= GEM_BIT(RXCOEN
);
2191 netcfg
&= ~GEM_BIT(RXCOEN
);
2192 gem_writel(bp
, NCFGR
, netcfg
);
2198 static const struct net_device_ops macb_netdev_ops
= {
2199 .ndo_open
= macb_open
,
2200 .ndo_stop
= macb_close
,
2201 .ndo_start_xmit
= macb_start_xmit
,
2202 .ndo_set_rx_mode
= macb_set_rx_mode
,
2203 .ndo_get_stats
= macb_get_stats
,
2204 .ndo_do_ioctl
= macb_ioctl
,
2205 .ndo_validate_addr
= eth_validate_addr
,
2206 .ndo_change_mtu
= macb_change_mtu
,
2207 .ndo_set_mac_address
= eth_mac_addr
,
2208 #ifdef CONFIG_NET_POLL_CONTROLLER
2209 .ndo_poll_controller
= macb_poll_controller
,
2211 .ndo_set_features
= macb_set_features
,
2215 * Configure peripheral capabilities according to device tree
2216 * and integration options used
2218 static void macb_configure_caps(struct macb
*bp
, const struct macb_config
*dt_conf
)
2223 bp
->caps
= dt_conf
->caps
;
2225 if (hw_is_gem(bp
->regs
, bp
->native_io
)) {
2226 bp
->caps
|= MACB_CAPS_MACB_IS_GEM
;
2228 dcfg
= gem_readl(bp
, DCFG1
);
2229 if (GEM_BFEXT(IRQCOR
, dcfg
) == 0)
2230 bp
->caps
|= MACB_CAPS_ISR_CLEAR_ON_WRITE
;
2231 dcfg
= gem_readl(bp
, DCFG2
);
2232 if ((dcfg
& (GEM_BIT(RX_PKT_BUFF
) | GEM_BIT(TX_PKT_BUFF
))) == 0)
2233 bp
->caps
|= MACB_CAPS_FIFO_MODE
;
2236 dev_dbg(&bp
->pdev
->dev
, "Cadence caps 0x%08x\n", bp
->caps
);
2239 static void macb_probe_queues(void __iomem
*mem
,
2241 unsigned int *queue_mask
,
2242 unsigned int *num_queues
)
2249 /* is it macb or gem ?
2251 * We need to read directly from the hardware here because
2252 * we are early in the probe process and don't have the
2253 * MACB_CAPS_MACB_IS_GEM flag positioned
2255 if (!hw_is_gem(mem
, native_io
))
2258 /* bit 0 is never set but queue 0 always exists */
2259 *queue_mask
= readl_relaxed(mem
+ GEM_DCFG6
) & 0xff;
2263 for (hw_q
= 1; hw_q
< MACB_MAX_QUEUES
; ++hw_q
)
2264 if (*queue_mask
& (1 << hw_q
))
2268 static int macb_clk_init(struct platform_device
*pdev
, struct clk
**pclk
,
2269 struct clk
**hclk
, struct clk
**tx_clk
)
2273 *pclk
= devm_clk_get(&pdev
->dev
, "pclk");
2274 if (IS_ERR(*pclk
)) {
2275 err
= PTR_ERR(*pclk
);
2276 dev_err(&pdev
->dev
, "failed to get macb_clk (%u)\n", err
);
2280 *hclk
= devm_clk_get(&pdev
->dev
, "hclk");
2281 if (IS_ERR(*hclk
)) {
2282 err
= PTR_ERR(*hclk
);
2283 dev_err(&pdev
->dev
, "failed to get hclk (%u)\n", err
);
2287 *tx_clk
= devm_clk_get(&pdev
->dev
, "tx_clk");
2288 if (IS_ERR(*tx_clk
))
2291 err
= clk_prepare_enable(*pclk
);
2293 dev_err(&pdev
->dev
, "failed to enable pclk (%u)\n", err
);
2297 err
= clk_prepare_enable(*hclk
);
2299 dev_err(&pdev
->dev
, "failed to enable hclk (%u)\n", err
);
2300 goto err_disable_pclk
;
2303 err
= clk_prepare_enable(*tx_clk
);
2305 dev_err(&pdev
->dev
, "failed to enable tx_clk (%u)\n", err
);
2306 goto err_disable_hclk
;
2312 clk_disable_unprepare(*hclk
);
2315 clk_disable_unprepare(*pclk
);
2320 static int macb_init(struct platform_device
*pdev
)
2322 struct net_device
*dev
= platform_get_drvdata(pdev
);
2323 unsigned int hw_q
, q
;
2324 struct macb
*bp
= netdev_priv(dev
);
2325 struct macb_queue
*queue
;
2329 /* set the queue register mapping once for all: queue0 has a special
2330 * register mapping but we don't want to test the queue index then
2331 * compute the corresponding register offset at run time.
2333 for (hw_q
= 0, q
= 0; hw_q
< MACB_MAX_QUEUES
; ++hw_q
) {
2334 if (!(bp
->queue_mask
& (1 << hw_q
)))
2337 queue
= &bp
->queues
[q
];
2340 queue
->ISR
= GEM_ISR(hw_q
- 1);
2341 queue
->IER
= GEM_IER(hw_q
- 1);
2342 queue
->IDR
= GEM_IDR(hw_q
- 1);
2343 queue
->IMR
= GEM_IMR(hw_q
- 1);
2344 queue
->TBQP
= GEM_TBQP(hw_q
- 1);
2346 /* queue0 uses legacy registers */
2347 queue
->ISR
= MACB_ISR
;
2348 queue
->IER
= MACB_IER
;
2349 queue
->IDR
= MACB_IDR
;
2350 queue
->IMR
= MACB_IMR
;
2351 queue
->TBQP
= MACB_TBQP
;
2354 /* get irq: here we use the linux queue index, not the hardware
2355 * queue index. the queue irq definitions in the device tree
2356 * must remove the optional gaps that could exist in the
2357 * hardware queue mask.
2359 queue
->irq
= platform_get_irq(pdev
, q
);
2360 err
= devm_request_irq(&pdev
->dev
, queue
->irq
, macb_interrupt
,
2361 IRQF_SHARED
, dev
->name
, queue
);
2364 "Unable to request IRQ %d (error %d)\n",
2369 INIT_WORK(&queue
->tx_error_task
, macb_tx_error_task
);
2373 dev
->netdev_ops
= &macb_netdev_ops
;
2374 netif_napi_add(dev
, &bp
->napi
, macb_poll
, 64);
2376 /* setup appropriated routines according to adapter type */
2377 if (macb_is_gem(bp
)) {
2378 bp
->max_tx_length
= GEM_MAX_TX_LEN
;
2379 bp
->macbgem_ops
.mog_alloc_rx_buffers
= gem_alloc_rx_buffers
;
2380 bp
->macbgem_ops
.mog_free_rx_buffers
= gem_free_rx_buffers
;
2381 bp
->macbgem_ops
.mog_init_rings
= gem_init_rings
;
2382 bp
->macbgem_ops
.mog_rx
= gem_rx
;
2383 dev
->ethtool_ops
= &gem_ethtool_ops
;
2385 bp
->max_tx_length
= MACB_MAX_TX_LEN
;
2386 bp
->macbgem_ops
.mog_alloc_rx_buffers
= macb_alloc_rx_buffers
;
2387 bp
->macbgem_ops
.mog_free_rx_buffers
= macb_free_rx_buffers
;
2388 bp
->macbgem_ops
.mog_init_rings
= macb_init_rings
;
2389 bp
->macbgem_ops
.mog_rx
= macb_rx
;
2390 dev
->ethtool_ops
= &macb_ethtool_ops
;
2394 dev
->hw_features
= NETIF_F_SG
;
2395 /* Checksum offload is only available on gem with packet buffer */
2396 if (macb_is_gem(bp
) && !(bp
->caps
& MACB_CAPS_FIFO_MODE
))
2397 dev
->hw_features
|= NETIF_F_HW_CSUM
| NETIF_F_RXCSUM
;
2398 if (bp
->caps
& MACB_CAPS_SG_DISABLED
)
2399 dev
->hw_features
&= ~NETIF_F_SG
;
2400 dev
->features
= dev
->hw_features
;
2403 if (bp
->phy_interface
== PHY_INTERFACE_MODE_RGMII
)
2404 val
= GEM_BIT(RGMII
);
2405 else if (bp
->phy_interface
== PHY_INTERFACE_MODE_RMII
&&
2406 (bp
->caps
& MACB_CAPS_USRIO_DEFAULT_IS_MII
))
2407 val
= MACB_BIT(RMII
);
2408 else if (!(bp
->caps
& MACB_CAPS_USRIO_DEFAULT_IS_MII
))
2409 val
= MACB_BIT(MII
);
2411 if (bp
->caps
& MACB_CAPS_USRIO_HAS_CLKEN
)
2412 val
|= MACB_BIT(CLKEN
);
2414 macb_or_gem_writel(bp
, USRIO
, val
);
2416 /* Set MII management clock divider */
2417 val
= macb_mdc_clk_div(bp
);
2418 val
|= macb_dbw(bp
);
2419 macb_writel(bp
, NCFGR
, val
);
2424 #if defined(CONFIG_OF)
2425 /* 1518 rounded up */
2426 #define AT91ETHER_MAX_RBUFF_SZ 0x600
2427 /* max number of receive buffers */
2428 #define AT91ETHER_MAX_RX_DESCR 9
2430 /* Initialize and start the Receiver and Transmit subsystems */
2431 static int at91ether_start(struct net_device
*dev
)
2433 struct macb
*lp
= netdev_priv(dev
);
2438 lp
->rx_ring
= dma_alloc_coherent(&lp
->pdev
->dev
,
2439 (AT91ETHER_MAX_RX_DESCR
*
2440 sizeof(struct macb_dma_desc
)),
2441 &lp
->rx_ring_dma
, GFP_KERNEL
);
2445 lp
->rx_buffers
= dma_alloc_coherent(&lp
->pdev
->dev
,
2446 AT91ETHER_MAX_RX_DESCR
*
2447 AT91ETHER_MAX_RBUFF_SZ
,
2448 &lp
->rx_buffers_dma
, GFP_KERNEL
);
2449 if (!lp
->rx_buffers
) {
2450 dma_free_coherent(&lp
->pdev
->dev
,
2451 AT91ETHER_MAX_RX_DESCR
*
2452 sizeof(struct macb_dma_desc
),
2453 lp
->rx_ring
, lp
->rx_ring_dma
);
2458 addr
= lp
->rx_buffers_dma
;
2459 for (i
= 0; i
< AT91ETHER_MAX_RX_DESCR
; i
++) {
2460 lp
->rx_ring
[i
].addr
= addr
;
2461 lp
->rx_ring
[i
].ctrl
= 0;
2462 addr
+= AT91ETHER_MAX_RBUFF_SZ
;
2465 /* Set the Wrap bit on the last descriptor */
2466 lp
->rx_ring
[AT91ETHER_MAX_RX_DESCR
- 1].addr
|= MACB_BIT(RX_WRAP
);
2468 /* Reset buffer index */
2471 /* Program address of descriptor list in Rx Buffer Queue register */
2472 macb_writel(lp
, RBQP
, lp
->rx_ring_dma
);
2474 /* Enable Receive and Transmit */
2475 ctl
= macb_readl(lp
, NCR
);
2476 macb_writel(lp
, NCR
, ctl
| MACB_BIT(RE
) | MACB_BIT(TE
));
2481 /* Open the ethernet interface */
2482 static int at91ether_open(struct net_device
*dev
)
2484 struct macb
*lp
= netdev_priv(dev
);
2488 /* Clear internal statistics */
2489 ctl
= macb_readl(lp
, NCR
);
2490 macb_writel(lp
, NCR
, ctl
| MACB_BIT(CLRSTAT
));
2492 macb_set_hwaddr(lp
);
2494 ret
= at91ether_start(dev
);
2498 /* Enable MAC interrupts */
2499 macb_writel(lp
, IER
, MACB_BIT(RCOMP
) |
2501 MACB_BIT(ISR_TUND
) |
2504 MACB_BIT(ISR_ROVR
) |
2507 /* schedule a link state check */
2508 phy_start(lp
->phy_dev
);
2510 netif_start_queue(dev
);
2515 /* Close the interface */
2516 static int at91ether_close(struct net_device
*dev
)
2518 struct macb
*lp
= netdev_priv(dev
);
2521 /* Disable Receiver and Transmitter */
2522 ctl
= macb_readl(lp
, NCR
);
2523 macb_writel(lp
, NCR
, ctl
& ~(MACB_BIT(TE
) | MACB_BIT(RE
)));
2525 /* Disable MAC interrupts */
2526 macb_writel(lp
, IDR
, MACB_BIT(RCOMP
) |
2528 MACB_BIT(ISR_TUND
) |
2531 MACB_BIT(ISR_ROVR
) |
2534 netif_stop_queue(dev
);
2536 dma_free_coherent(&lp
->pdev
->dev
,
2537 AT91ETHER_MAX_RX_DESCR
*
2538 sizeof(struct macb_dma_desc
),
2539 lp
->rx_ring
, lp
->rx_ring_dma
);
2542 dma_free_coherent(&lp
->pdev
->dev
,
2543 AT91ETHER_MAX_RX_DESCR
* AT91ETHER_MAX_RBUFF_SZ
,
2544 lp
->rx_buffers
, lp
->rx_buffers_dma
);
2545 lp
->rx_buffers
= NULL
;
2550 /* Transmit packet */
2551 static int at91ether_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
2553 struct macb
*lp
= netdev_priv(dev
);
2555 if (macb_readl(lp
, TSR
) & MACB_BIT(RM9200_BNQ
)) {
2556 netif_stop_queue(dev
);
2558 /* Store packet information (to free when Tx completed) */
2560 lp
->skb_length
= skb
->len
;
2561 lp
->skb_physaddr
= dma_map_single(NULL
, skb
->data
, skb
->len
,
2564 /* Set address of the data in the Transmit Address register */
2565 macb_writel(lp
, TAR
, lp
->skb_physaddr
);
2566 /* Set length of the packet in the Transmit Control register */
2567 macb_writel(lp
, TCR
, skb
->len
);
2570 netdev_err(dev
, "%s called, but device is busy!\n", __func__
);
2571 return NETDEV_TX_BUSY
;
2574 return NETDEV_TX_OK
;
2577 /* Extract received frame from buffer descriptors and sent to upper layers.
2578 * (Called from interrupt context)
2580 static void at91ether_rx(struct net_device
*dev
)
2582 struct macb
*lp
= netdev_priv(dev
);
2583 unsigned char *p_recv
;
2584 struct sk_buff
*skb
;
2585 unsigned int pktlen
;
2587 while (lp
->rx_ring
[lp
->rx_tail
].addr
& MACB_BIT(RX_USED
)) {
2588 p_recv
= lp
->rx_buffers
+ lp
->rx_tail
* AT91ETHER_MAX_RBUFF_SZ
;
2589 pktlen
= MACB_BF(RX_FRMLEN
, lp
->rx_ring
[lp
->rx_tail
].ctrl
);
2590 skb
= netdev_alloc_skb(dev
, pktlen
+ 2);
2592 skb_reserve(skb
, 2);
2593 memcpy(skb_put(skb
, pktlen
), p_recv
, pktlen
);
2595 skb
->protocol
= eth_type_trans(skb
, dev
);
2596 lp
->stats
.rx_packets
++;
2597 lp
->stats
.rx_bytes
+= pktlen
;
2600 lp
->stats
.rx_dropped
++;
2603 if (lp
->rx_ring
[lp
->rx_tail
].ctrl
& MACB_BIT(RX_MHASH_MATCH
))
2604 lp
->stats
.multicast
++;
2606 /* reset ownership bit */
2607 lp
->rx_ring
[lp
->rx_tail
].addr
&= ~MACB_BIT(RX_USED
);
2609 /* wrap after last buffer */
2610 if (lp
->rx_tail
== AT91ETHER_MAX_RX_DESCR
- 1)
2617 /* MAC interrupt handler */
2618 static irqreturn_t
at91ether_interrupt(int irq
, void *dev_id
)
2620 struct net_device
*dev
= dev_id
;
2621 struct macb
*lp
= netdev_priv(dev
);
2624 /* MAC Interrupt Status register indicates what interrupts are pending.
2625 * It is automatically cleared once read.
2627 intstatus
= macb_readl(lp
, ISR
);
2629 /* Receive complete */
2630 if (intstatus
& MACB_BIT(RCOMP
))
2633 /* Transmit complete */
2634 if (intstatus
& MACB_BIT(TCOMP
)) {
2635 /* The TCOM bit is set even if the transmission failed */
2636 if (intstatus
& (MACB_BIT(ISR_TUND
) | MACB_BIT(ISR_RLE
)))
2637 lp
->stats
.tx_errors
++;
2640 dev_kfree_skb_irq(lp
->skb
);
2642 dma_unmap_single(NULL
, lp
->skb_physaddr
,
2643 lp
->skb_length
, DMA_TO_DEVICE
);
2644 lp
->stats
.tx_packets
++;
2645 lp
->stats
.tx_bytes
+= lp
->skb_length
;
2647 netif_wake_queue(dev
);
2650 /* Work-around for EMAC Errata section 41.3.1 */
2651 if (intstatus
& MACB_BIT(RXUBR
)) {
2652 ctl
= macb_readl(lp
, NCR
);
2653 macb_writel(lp
, NCR
, ctl
& ~MACB_BIT(RE
));
2654 macb_writel(lp
, NCR
, ctl
| MACB_BIT(RE
));
2657 if (intstatus
& MACB_BIT(ISR_ROVR
))
2658 netdev_err(dev
, "ROVR error\n");
2663 #ifdef CONFIG_NET_POLL_CONTROLLER
2664 static void at91ether_poll_controller(struct net_device
*dev
)
2666 unsigned long flags
;
2668 local_irq_save(flags
);
2669 at91ether_interrupt(dev
->irq
, dev
);
2670 local_irq_restore(flags
);
2674 static const struct net_device_ops at91ether_netdev_ops
= {
2675 .ndo_open
= at91ether_open
,
2676 .ndo_stop
= at91ether_close
,
2677 .ndo_start_xmit
= at91ether_start_xmit
,
2678 .ndo_get_stats
= macb_get_stats
,
2679 .ndo_set_rx_mode
= macb_set_rx_mode
,
2680 .ndo_set_mac_address
= eth_mac_addr
,
2681 .ndo_do_ioctl
= macb_ioctl
,
2682 .ndo_validate_addr
= eth_validate_addr
,
2683 .ndo_change_mtu
= eth_change_mtu
,
2684 #ifdef CONFIG_NET_POLL_CONTROLLER
2685 .ndo_poll_controller
= at91ether_poll_controller
,
2689 static int at91ether_clk_init(struct platform_device
*pdev
, struct clk
**pclk
,
2690 struct clk
**hclk
, struct clk
**tx_clk
)
2697 *pclk
= devm_clk_get(&pdev
->dev
, "ether_clk");
2699 return PTR_ERR(*pclk
);
2701 err
= clk_prepare_enable(*pclk
);
2703 dev_err(&pdev
->dev
, "failed to enable pclk (%u)\n", err
);
2710 static int at91ether_init(struct platform_device
*pdev
)
2712 struct net_device
*dev
= platform_get_drvdata(pdev
);
2713 struct macb
*bp
= netdev_priv(dev
);
2717 dev
->netdev_ops
= &at91ether_netdev_ops
;
2718 dev
->ethtool_ops
= &macb_ethtool_ops
;
2720 err
= devm_request_irq(&pdev
->dev
, dev
->irq
, at91ether_interrupt
,
2725 macb_writel(bp
, NCR
, 0);
2727 reg
= MACB_BF(CLK
, MACB_CLK_DIV32
) | MACB_BIT(BIG
);
2728 if (bp
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
2729 reg
|= MACB_BIT(RM9200_RMII
);
2731 macb_writel(bp
, NCFGR
, reg
);
2736 static const struct macb_config at91sam9260_config
= {
2737 .caps
= MACB_CAPS_USRIO_HAS_CLKEN
| MACB_CAPS_USRIO_DEFAULT_IS_MII
,
2738 .clk_init
= macb_clk_init
,
2742 static const struct macb_config pc302gem_config
= {
2743 .caps
= MACB_CAPS_SG_DISABLED
| MACB_CAPS_GIGABIT_MODE_AVAILABLE
,
2744 .dma_burst_length
= 16,
2745 .clk_init
= macb_clk_init
,
2749 static const struct macb_config sama5d2_config
= {
2751 .dma_burst_length
= 16,
2752 .clk_init
= macb_clk_init
,
2756 static const struct macb_config sama5d3_config
= {
2757 .caps
= MACB_CAPS_SG_DISABLED
| MACB_CAPS_GIGABIT_MODE_AVAILABLE
,
2758 .dma_burst_length
= 16,
2759 .clk_init
= macb_clk_init
,
2763 static const struct macb_config sama5d4_config
= {
2765 .dma_burst_length
= 4,
2766 .clk_init
= macb_clk_init
,
2770 static const struct macb_config emac_config
= {
2771 .clk_init
= at91ether_clk_init
,
2772 .init
= at91ether_init
,
2776 static const struct macb_config zynqmp_config
= {
2777 .caps
= MACB_CAPS_SG_DISABLED
| MACB_CAPS_GIGABIT_MODE_AVAILABLE
|
2779 .dma_burst_length
= 16,
2780 .clk_init
= macb_clk_init
,
2782 .jumbo_max_len
= 10240,
2785 static const struct macb_config zynq_config
= {
2786 .caps
= MACB_CAPS_SG_DISABLED
| MACB_CAPS_GIGABIT_MODE_AVAILABLE
|
2787 MACB_CAPS_NO_GIGABIT_HALF
,
2788 .dma_burst_length
= 16,
2789 .clk_init
= macb_clk_init
,
2793 static const struct of_device_id macb_dt_ids
[] = {
2794 { .compatible
= "cdns,at32ap7000-macb" },
2795 { .compatible
= "cdns,at91sam9260-macb", .data
= &at91sam9260_config
},
2796 { .compatible
= "cdns,macb" },
2797 { .compatible
= "cdns,pc302-gem", .data
= &pc302gem_config
},
2798 { .compatible
= "cdns,gem", .data
= &pc302gem_config
},
2799 { .compatible
= "atmel,sama5d2-gem", .data
= &sama5d2_config
},
2800 { .compatible
= "atmel,sama5d3-gem", .data
= &sama5d3_config
},
2801 { .compatible
= "atmel,sama5d4-gem", .data
= &sama5d4_config
},
2802 { .compatible
= "cdns,at91rm9200-emac", .data
= &emac_config
},
2803 { .compatible
= "cdns,emac", .data
= &emac_config
},
2804 { .compatible
= "cdns,zynqmp-gem", .data
= &zynqmp_config
},
2805 { .compatible
= "cdns,zynq-gem", .data
= &zynq_config
},
2808 MODULE_DEVICE_TABLE(of
, macb_dt_ids
);
2809 #endif /* CONFIG_OF */
2811 static int macb_probe(struct platform_device
*pdev
)
2813 int (*clk_init
)(struct platform_device
*, struct clk
**,
2814 struct clk
**, struct clk
**)
2816 int (*init
)(struct platform_device
*) = macb_init
;
2817 struct device_node
*np
= pdev
->dev
.of_node
;
2818 const struct macb_config
*macb_config
= NULL
;
2819 struct clk
*pclk
, *hclk
, *tx_clk
;
2820 unsigned int queue_mask
, num_queues
;
2821 struct macb_platform_data
*pdata
;
2823 struct phy_device
*phydev
;
2824 struct net_device
*dev
;
2825 struct resource
*regs
;
2831 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
2832 mem
= devm_ioremap_resource(&pdev
->dev
, regs
);
2834 return PTR_ERR(mem
);
2837 const struct of_device_id
*match
;
2839 match
= of_match_node(macb_dt_ids
, np
);
2840 if (match
&& match
->data
) {
2841 macb_config
= match
->data
;
2842 clk_init
= macb_config
->clk_init
;
2843 init
= macb_config
->init
;
2847 err
= clk_init(pdev
, &pclk
, &hclk
, &tx_clk
);
2851 native_io
= hw_is_native_io(mem
);
2853 macb_probe_queues(mem
, native_io
, &queue_mask
, &num_queues
);
2854 dev
= alloc_etherdev_mq(sizeof(*bp
), num_queues
);
2857 goto err_disable_clocks
;
2860 dev
->base_addr
= regs
->start
;
2862 SET_NETDEV_DEV(dev
, &pdev
->dev
);
2864 bp
= netdev_priv(dev
);
2868 bp
->native_io
= native_io
;
2870 bp
->macb_reg_readl
= hw_readl_native
;
2871 bp
->macb_reg_writel
= hw_writel_native
;
2873 bp
->macb_reg_readl
= hw_readl
;
2874 bp
->macb_reg_writel
= hw_writel
;
2876 bp
->num_queues
= num_queues
;
2877 bp
->queue_mask
= queue_mask
;
2879 bp
->dma_burst_length
= macb_config
->dma_burst_length
;
2882 bp
->tx_clk
= tx_clk
;
2884 bp
->jumbo_max_len
= macb_config
->jumbo_max_len
;
2886 spin_lock_init(&bp
->lock
);
2888 /* setup capabilities */
2889 macb_configure_caps(bp
, macb_config
);
2891 platform_set_drvdata(pdev
, dev
);
2893 dev
->irq
= platform_get_irq(pdev
, 0);
2896 goto err_disable_clocks
;
2899 mac
= of_get_mac_address(np
);
2901 memcpy(bp
->dev
->dev_addr
, mac
, ETH_ALEN
);
2903 macb_get_hwaddr(bp
);
2905 err
= of_get_phy_mode(np
);
2907 pdata
= dev_get_platdata(&pdev
->dev
);
2908 if (pdata
&& pdata
->is_rmii
)
2909 bp
->phy_interface
= PHY_INTERFACE_MODE_RMII
;
2911 bp
->phy_interface
= PHY_INTERFACE_MODE_MII
;
2913 bp
->phy_interface
= err
;
2916 /* IP specific init */
2919 goto err_out_free_netdev
;
2921 err
= register_netdev(dev
);
2923 dev_err(&pdev
->dev
, "Cannot register net device, aborting.\n");
2924 goto err_out_unregister_netdev
;
2927 err
= macb_mii_init(bp
);
2929 goto err_out_unregister_netdev
;
2931 netif_carrier_off(dev
);
2933 netdev_info(dev
, "Cadence %s rev 0x%08x at 0x%08lx irq %d (%pM)\n",
2934 macb_is_gem(bp
) ? "GEM" : "MACB", macb_readl(bp
, MID
),
2935 dev
->base_addr
, dev
->irq
, dev
->dev_addr
);
2937 phydev
= bp
->phy_dev
;
2938 netdev_info(dev
, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
2939 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
2943 err_out_unregister_netdev
:
2944 unregister_netdev(dev
);
2946 err_out_free_netdev
:
2950 clk_disable_unprepare(tx_clk
);
2951 clk_disable_unprepare(hclk
);
2952 clk_disable_unprepare(pclk
);
2957 static int macb_remove(struct platform_device
*pdev
)
2959 struct net_device
*dev
;
2962 dev
= platform_get_drvdata(pdev
);
2965 bp
= netdev_priv(dev
);
2967 phy_disconnect(bp
->phy_dev
);
2968 mdiobus_unregister(bp
->mii_bus
);
2969 kfree(bp
->mii_bus
->irq
);
2970 mdiobus_free(bp
->mii_bus
);
2971 unregister_netdev(dev
);
2972 clk_disable_unprepare(bp
->tx_clk
);
2973 clk_disable_unprepare(bp
->hclk
);
2974 clk_disable_unprepare(bp
->pclk
);
2981 static int __maybe_unused
macb_suspend(struct device
*dev
)
2983 struct platform_device
*pdev
= to_platform_device(dev
);
2984 struct net_device
*netdev
= platform_get_drvdata(pdev
);
2985 struct macb
*bp
= netdev_priv(netdev
);
2987 netif_carrier_off(netdev
);
2988 netif_device_detach(netdev
);
2990 clk_disable_unprepare(bp
->tx_clk
);
2991 clk_disable_unprepare(bp
->hclk
);
2992 clk_disable_unprepare(bp
->pclk
);
2997 static int __maybe_unused
macb_resume(struct device
*dev
)
2999 struct platform_device
*pdev
= to_platform_device(dev
);
3000 struct net_device
*netdev
= platform_get_drvdata(pdev
);
3001 struct macb
*bp
= netdev_priv(netdev
);
3003 clk_prepare_enable(bp
->pclk
);
3004 clk_prepare_enable(bp
->hclk
);
3005 clk_prepare_enable(bp
->tx_clk
);
3007 netif_device_attach(netdev
);
3012 static SIMPLE_DEV_PM_OPS(macb_pm_ops
, macb_suspend
, macb_resume
);
3014 static struct platform_driver macb_driver
= {
3015 .probe
= macb_probe
,
3016 .remove
= macb_remove
,
3019 .of_match_table
= of_match_ptr(macb_dt_ids
),
3024 module_platform_driver(macb_driver
);
3026 MODULE_LICENSE("GPL");
3027 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
3028 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
3029 MODULE_ALIAS("platform:macb");