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>
20 #include <linux/gpio.h>
21 #include <linux/interrupt.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/platform_data/macb.h>
26 #include <linux/platform_device.h>
27 #include <linux/phy.h>
29 #include <linux/of_device.h>
30 #include <linux/of_net.h>
31 #include <linux/pinctrl/consumer.h>
35 #define RX_BUFFER_SIZE 128
36 #define RX_RING_SIZE 512 /* must be power of 2 */
37 #define RX_RING_BYTES (sizeof(struct macb_dma_desc) * RX_RING_SIZE)
39 #define TX_RING_SIZE 128 /* must be power of 2 */
40 #define TX_RING_BYTES (sizeof(struct macb_dma_desc) * TX_RING_SIZE)
42 /* level of occupied TX descriptors under which we wake up TX process */
43 #define MACB_TX_WAKEUP_THRESH (3 * TX_RING_SIZE / 4)
45 #define MACB_RX_INT_FLAGS (MACB_BIT(RCOMP) | MACB_BIT(RXUBR) \
47 #define MACB_TX_ERR_FLAGS (MACB_BIT(ISR_TUND) \
50 #define MACB_TX_INT_FLAGS (MACB_TX_ERR_FLAGS | MACB_BIT(TCOMP))
53 * Graceful stop timeouts in us. We should allow up to
54 * 1 frame time (10 Mbits/s, full-duplex, ignoring collisions)
56 #define MACB_HALT_TIMEOUT 1230
58 /* Ring buffer accessors */
59 static unsigned int macb_tx_ring_wrap(unsigned int index
)
61 return index
& (TX_RING_SIZE
- 1);
64 static struct macb_dma_desc
*macb_tx_desc(struct macb
*bp
, unsigned int index
)
66 return &bp
->tx_ring
[macb_tx_ring_wrap(index
)];
69 static struct macb_tx_skb
*macb_tx_skb(struct macb
*bp
, unsigned int index
)
71 return &bp
->tx_skb
[macb_tx_ring_wrap(index
)];
74 static dma_addr_t
macb_tx_dma(struct macb
*bp
, unsigned int index
)
78 offset
= macb_tx_ring_wrap(index
) * sizeof(struct macb_dma_desc
);
80 return bp
->tx_ring_dma
+ offset
;
83 static unsigned int macb_rx_ring_wrap(unsigned int index
)
85 return index
& (RX_RING_SIZE
- 1);
88 static struct macb_dma_desc
*macb_rx_desc(struct macb
*bp
, unsigned int index
)
90 return &bp
->rx_ring
[macb_rx_ring_wrap(index
)];
93 static void *macb_rx_buffer(struct macb
*bp
, unsigned int index
)
95 return bp
->rx_buffers
+ RX_BUFFER_SIZE
* macb_rx_ring_wrap(index
);
98 void macb_set_hwaddr(struct macb
*bp
)
103 bottom
= cpu_to_le32(*((u32
*)bp
->dev
->dev_addr
));
104 macb_or_gem_writel(bp
, SA1B
, bottom
);
105 top
= cpu_to_le16(*((u16
*)(bp
->dev
->dev_addr
+ 4)));
106 macb_or_gem_writel(bp
, SA1T
, top
);
108 /* Clear unused address register sets */
109 macb_or_gem_writel(bp
, SA2B
, 0);
110 macb_or_gem_writel(bp
, SA2T
, 0);
111 macb_or_gem_writel(bp
, SA3B
, 0);
112 macb_or_gem_writel(bp
, SA3T
, 0);
113 macb_or_gem_writel(bp
, SA4B
, 0);
114 macb_or_gem_writel(bp
, SA4T
, 0);
116 EXPORT_SYMBOL_GPL(macb_set_hwaddr
);
118 void macb_get_hwaddr(struct macb
*bp
)
120 struct macb_platform_data
*pdata
;
126 pdata
= bp
->pdev
->dev
.platform_data
;
128 /* Check all 4 address register for vaild address */
129 for (i
= 0; i
< 4; i
++) {
130 bottom
= macb_or_gem_readl(bp
, SA1B
+ i
* 8);
131 top
= macb_or_gem_readl(bp
, SA1T
+ i
* 8);
133 if (pdata
&& pdata
->rev_eth_addr
) {
134 addr
[5] = bottom
& 0xff;
135 addr
[4] = (bottom
>> 8) & 0xff;
136 addr
[3] = (bottom
>> 16) & 0xff;
137 addr
[2] = (bottom
>> 24) & 0xff;
138 addr
[1] = top
& 0xff;
139 addr
[0] = (top
& 0xff00) >> 8;
141 addr
[0] = bottom
& 0xff;
142 addr
[1] = (bottom
>> 8) & 0xff;
143 addr
[2] = (bottom
>> 16) & 0xff;
144 addr
[3] = (bottom
>> 24) & 0xff;
145 addr
[4] = top
& 0xff;
146 addr
[5] = (top
>> 8) & 0xff;
149 if (is_valid_ether_addr(addr
)) {
150 memcpy(bp
->dev
->dev_addr
, addr
, sizeof(addr
));
155 netdev_info(bp
->dev
, "invalid hw address, using random\n");
156 eth_hw_addr_random(bp
->dev
);
158 EXPORT_SYMBOL_GPL(macb_get_hwaddr
);
160 static int macb_mdio_read(struct mii_bus
*bus
, int mii_id
, int regnum
)
162 struct macb
*bp
= bus
->priv
;
165 macb_writel(bp
, MAN
, (MACB_BF(SOF
, MACB_MAN_SOF
)
166 | MACB_BF(RW
, MACB_MAN_READ
)
167 | MACB_BF(PHYA
, mii_id
)
168 | MACB_BF(REGA
, regnum
)
169 | MACB_BF(CODE
, MACB_MAN_CODE
)));
171 /* wait for end of transfer */
172 while (!MACB_BFEXT(IDLE
, macb_readl(bp
, NSR
)))
175 value
= MACB_BFEXT(DATA
, macb_readl(bp
, MAN
));
180 static int macb_mdio_write(struct mii_bus
*bus
, int mii_id
, int regnum
,
183 struct macb
*bp
= bus
->priv
;
185 macb_writel(bp
, MAN
, (MACB_BF(SOF
, MACB_MAN_SOF
)
186 | MACB_BF(RW
, MACB_MAN_WRITE
)
187 | MACB_BF(PHYA
, mii_id
)
188 | MACB_BF(REGA
, regnum
)
189 | MACB_BF(CODE
, MACB_MAN_CODE
)
190 | MACB_BF(DATA
, value
)));
192 /* wait for end of transfer */
193 while (!MACB_BFEXT(IDLE
, macb_readl(bp
, NSR
)))
199 static int macb_mdio_reset(struct mii_bus
*bus
)
204 static void macb_handle_link_change(struct net_device
*dev
)
206 struct macb
*bp
= netdev_priv(dev
);
207 struct phy_device
*phydev
= bp
->phy_dev
;
210 int status_change
= 0;
212 spin_lock_irqsave(&bp
->lock
, flags
);
215 if ((bp
->speed
!= phydev
->speed
) ||
216 (bp
->duplex
!= phydev
->duplex
)) {
219 reg
= macb_readl(bp
, NCFGR
);
220 reg
&= ~(MACB_BIT(SPD
) | MACB_BIT(FD
));
222 reg
&= ~GEM_BIT(GBE
);
226 if (phydev
->speed
== SPEED_100
)
227 reg
|= MACB_BIT(SPD
);
228 if (phydev
->speed
== SPEED_1000
)
231 macb_or_gem_writel(bp
, NCFGR
, reg
);
233 bp
->speed
= phydev
->speed
;
234 bp
->duplex
= phydev
->duplex
;
239 if (phydev
->link
!= bp
->link
) {
244 bp
->link
= phydev
->link
;
249 spin_unlock_irqrestore(&bp
->lock
, flags
);
253 netif_carrier_on(dev
);
254 netdev_info(dev
, "link up (%d/%s)\n",
256 phydev
->duplex
== DUPLEX_FULL
?
259 netif_carrier_off(dev
);
260 netdev_info(dev
, "link down\n");
265 /* based on au1000_eth. c*/
266 static int macb_mii_probe(struct net_device
*dev
)
268 struct macb
*bp
= netdev_priv(dev
);
269 struct macb_platform_data
*pdata
;
270 struct phy_device
*phydev
;
274 phydev
= phy_find_first(bp
->mii_bus
);
276 netdev_err(dev
, "no PHY found\n");
280 pdata
= dev_get_platdata(&bp
->pdev
->dev
);
281 if (pdata
&& gpio_is_valid(pdata
->phy_irq_pin
)) {
282 ret
= devm_gpio_request(&bp
->pdev
->dev
, pdata
->phy_irq_pin
, "phy int");
284 phy_irq
= gpio_to_irq(pdata
->phy_irq_pin
);
285 phydev
->irq
= (phy_irq
< 0) ? PHY_POLL
: phy_irq
;
289 /* attach the mac to the phy */
290 ret
= phy_connect_direct(dev
, phydev
, &macb_handle_link_change
,
293 netdev_err(dev
, "Could not attach to PHY\n");
297 /* mask with MAC supported features */
299 phydev
->supported
&= PHY_GBIT_FEATURES
;
301 phydev
->supported
&= PHY_BASIC_FEATURES
;
303 phydev
->advertising
= phydev
->supported
;
308 bp
->phy_dev
= phydev
;
313 int macb_mii_init(struct macb
*bp
)
315 struct macb_platform_data
*pdata
;
318 /* Enable management port */
319 macb_writel(bp
, NCR
, MACB_BIT(MPE
));
321 bp
->mii_bus
= mdiobus_alloc();
322 if (bp
->mii_bus
== NULL
) {
327 bp
->mii_bus
->name
= "MACB_mii_bus";
328 bp
->mii_bus
->read
= &macb_mdio_read
;
329 bp
->mii_bus
->write
= &macb_mdio_write
;
330 bp
->mii_bus
->reset
= &macb_mdio_reset
;
331 snprintf(bp
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
332 bp
->pdev
->name
, bp
->pdev
->id
);
333 bp
->mii_bus
->priv
= bp
;
334 bp
->mii_bus
->parent
= &bp
->dev
->dev
;
335 pdata
= bp
->pdev
->dev
.platform_data
;
338 bp
->mii_bus
->phy_mask
= pdata
->phy_mask
;
340 bp
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
341 if (!bp
->mii_bus
->irq
) {
343 goto err_out_free_mdiobus
;
346 for (i
= 0; i
< PHY_MAX_ADDR
; i
++)
347 bp
->mii_bus
->irq
[i
] = PHY_POLL
;
349 dev_set_drvdata(&bp
->dev
->dev
, bp
->mii_bus
);
351 if (mdiobus_register(bp
->mii_bus
))
352 goto err_out_free_mdio_irq
;
354 if (macb_mii_probe(bp
->dev
) != 0) {
355 goto err_out_unregister_bus
;
360 err_out_unregister_bus
:
361 mdiobus_unregister(bp
->mii_bus
);
362 err_out_free_mdio_irq
:
363 kfree(bp
->mii_bus
->irq
);
364 err_out_free_mdiobus
:
365 mdiobus_free(bp
->mii_bus
);
369 EXPORT_SYMBOL_GPL(macb_mii_init
);
371 static void macb_update_stats(struct macb
*bp
)
373 u32 __iomem
*reg
= bp
->regs
+ MACB_PFR
;
374 u32
*p
= &bp
->hw_stats
.macb
.rx_pause_frames
;
375 u32
*end
= &bp
->hw_stats
.macb
.tx_pause_frames
+ 1;
377 WARN_ON((unsigned long)(end
- p
- 1) != (MACB_TPF
- MACB_PFR
) / 4);
379 for(; p
< end
; p
++, reg
++)
380 *p
+= __raw_readl(reg
);
383 static int macb_halt_tx(struct macb
*bp
)
385 unsigned long halt_time
, timeout
;
388 macb_writel(bp
, NCR
, macb_readl(bp
, NCR
) | MACB_BIT(THALT
));
390 timeout
= jiffies
+ usecs_to_jiffies(MACB_HALT_TIMEOUT
);
393 status
= macb_readl(bp
, TSR
);
394 if (!(status
& MACB_BIT(TGO
)))
397 usleep_range(10, 250);
398 } while (time_before(halt_time
, timeout
));
403 static void macb_tx_error_task(struct work_struct
*work
)
405 struct macb
*bp
= container_of(work
, struct macb
, tx_error_task
);
406 struct macb_tx_skb
*tx_skb
;
410 netdev_vdbg(bp
->dev
, "macb_tx_error_task: t = %u, h = %u\n",
411 bp
->tx_tail
, bp
->tx_head
);
413 /* Make sure nobody is trying to queue up new packets */
414 netif_stop_queue(bp
->dev
);
417 * Stop transmission now
418 * (in case we have just queued new packets)
420 if (macb_halt_tx(bp
))
421 /* Just complain for now, reinitializing TX path can be good */
422 netdev_err(bp
->dev
, "BUG: halt tx timed out\n");
424 /* No need for the lock here as nobody will interrupt us anymore */
427 * Treat frames in TX queue including the ones that caused the error.
428 * Free transmit buffers in upper layer.
430 for (tail
= bp
->tx_tail
; tail
!= bp
->tx_head
; tail
++) {
431 struct macb_dma_desc
*desc
;
434 desc
= macb_tx_desc(bp
, tail
);
436 tx_skb
= macb_tx_skb(bp
, tail
);
439 if (ctrl
& MACB_BIT(TX_USED
)) {
440 netdev_vdbg(bp
->dev
, "txerr skb %u (data %p) TX complete\n",
441 macb_tx_ring_wrap(tail
), skb
->data
);
442 bp
->stats
.tx_packets
++;
443 bp
->stats
.tx_bytes
+= skb
->len
;
446 * "Buffers exhausted mid-frame" errors may only happen
447 * if the driver is buggy, so complain loudly about those.
448 * Statistics are updated by hardware.
450 if (ctrl
& MACB_BIT(TX_BUF_EXHAUSTED
))
452 "BUG: TX buffers exhausted mid-frame\n");
454 desc
->ctrl
= ctrl
| MACB_BIT(TX_USED
);
457 dma_unmap_single(&bp
->pdev
->dev
, tx_skb
->mapping
, skb
->len
,
463 /* Make descriptor updates visible to hardware */
466 /* Reinitialize the TX desc queue */
467 macb_writel(bp
, TBQP
, bp
->tx_ring_dma
);
468 /* Make TX ring reflect state of hardware */
469 bp
->tx_head
= bp
->tx_tail
= 0;
471 /* Now we are ready to start transmission again */
472 netif_wake_queue(bp
->dev
);
474 /* Housework before enabling TX IRQ */
475 macb_writel(bp
, TSR
, macb_readl(bp
, TSR
));
476 macb_writel(bp
, IER
, MACB_TX_INT_FLAGS
);
479 static void macb_tx_interrupt(struct macb
*bp
)
485 status
= macb_readl(bp
, TSR
);
486 macb_writel(bp
, TSR
, status
);
488 netdev_vdbg(bp
->dev
, "macb_tx_interrupt status = 0x%03lx\n",
489 (unsigned long)status
);
492 for (tail
= bp
->tx_tail
; tail
!= head
; tail
++) {
493 struct macb_tx_skb
*tx_skb
;
495 struct macb_dma_desc
*desc
;
498 desc
= macb_tx_desc(bp
, tail
);
500 /* Make hw descriptor updates visible to CPU */
505 if (!(ctrl
& MACB_BIT(TX_USED
)))
508 tx_skb
= macb_tx_skb(bp
, tail
);
511 netdev_vdbg(bp
->dev
, "skb %u (data %p) TX complete\n",
512 macb_tx_ring_wrap(tail
), skb
->data
);
513 dma_unmap_single(&bp
->pdev
->dev
, tx_skb
->mapping
, skb
->len
,
515 bp
->stats
.tx_packets
++;
516 bp
->stats
.tx_bytes
+= skb
->len
;
518 dev_kfree_skb_irq(skb
);
522 if (netif_queue_stopped(bp
->dev
)
523 && CIRC_CNT(bp
->tx_head
, bp
->tx_tail
,
524 TX_RING_SIZE
) <= MACB_TX_WAKEUP_THRESH
)
525 netif_wake_queue(bp
->dev
);
528 static int macb_rx_frame(struct macb
*bp
, unsigned int first_frag
,
529 unsigned int last_frag
)
535 struct macb_dma_desc
*desc
;
537 desc
= macb_rx_desc(bp
, last_frag
);
538 len
= MACB_BFEXT(RX_FRMLEN
, desc
->ctrl
);
540 netdev_vdbg(bp
->dev
, "macb_rx_frame frags %u - %u (len %u)\n",
541 macb_rx_ring_wrap(first_frag
),
542 macb_rx_ring_wrap(last_frag
), len
);
545 * The ethernet header starts NET_IP_ALIGN bytes into the
546 * first buffer. Since the header is 14 bytes, this makes the
547 * payload word-aligned.
549 * Instead of calling skb_reserve(NET_IP_ALIGN), we just copy
550 * the two padding bytes into the skb so that we avoid hitting
551 * the slowpath in memcpy(), and pull them off afterwards.
553 skb
= netdev_alloc_skb(bp
->dev
, len
+ NET_IP_ALIGN
);
555 bp
->stats
.rx_dropped
++;
556 for (frag
= first_frag
; ; frag
++) {
557 desc
= macb_rx_desc(bp
, frag
);
558 desc
->addr
&= ~MACB_BIT(RX_USED
);
559 if (frag
== last_frag
)
563 /* Make descriptor updates visible to hardware */
571 skb_checksum_none_assert(skb
);
574 for (frag
= first_frag
; ; frag
++) {
575 unsigned int frag_len
= RX_BUFFER_SIZE
;
577 if (offset
+ frag_len
> len
) {
578 BUG_ON(frag
!= last_frag
);
579 frag_len
= len
- offset
;
581 skb_copy_to_linear_data_offset(skb
, offset
,
582 macb_rx_buffer(bp
, frag
), frag_len
);
583 offset
+= RX_BUFFER_SIZE
;
584 desc
= macb_rx_desc(bp
, frag
);
585 desc
->addr
&= ~MACB_BIT(RX_USED
);
587 if (frag
== last_frag
)
591 /* Make descriptor updates visible to hardware */
594 __skb_pull(skb
, NET_IP_ALIGN
);
595 skb
->protocol
= eth_type_trans(skb
, bp
->dev
);
597 bp
->stats
.rx_packets
++;
598 bp
->stats
.rx_bytes
+= skb
->len
;
599 netdev_vdbg(bp
->dev
, "received skb of length %u, csum: %08x\n",
600 skb
->len
, skb
->csum
);
601 netif_receive_skb(skb
);
606 /* Mark DMA descriptors from begin up to and not including end as unused */
607 static void discard_partial_frame(struct macb
*bp
, unsigned int begin
,
612 for (frag
= begin
; frag
!= end
; frag
++) {
613 struct macb_dma_desc
*desc
= macb_rx_desc(bp
, frag
);
614 desc
->addr
&= ~MACB_BIT(RX_USED
);
617 /* Make descriptor updates visible to hardware */
621 * When this happens, the hardware stats registers for
622 * whatever caused this is updated, so we don't have to record
627 static int macb_rx(struct macb
*bp
, int budget
)
633 for (tail
= bp
->rx_tail
; budget
> 0; tail
++) {
634 struct macb_dma_desc
*desc
= macb_rx_desc(bp
, tail
);
637 /* Make hw descriptor updates visible to CPU */
643 if (!(addr
& MACB_BIT(RX_USED
)))
646 if (ctrl
& MACB_BIT(RX_SOF
)) {
647 if (first_frag
!= -1)
648 discard_partial_frame(bp
, first_frag
, tail
);
652 if (ctrl
& MACB_BIT(RX_EOF
)) {
654 BUG_ON(first_frag
== -1);
656 dropped
= macb_rx_frame(bp
, first_frag
, tail
);
665 if (first_frag
!= -1)
666 bp
->rx_tail
= first_frag
;
673 static int macb_poll(struct napi_struct
*napi
, int budget
)
675 struct macb
*bp
= container_of(napi
, struct macb
, napi
);
679 status
= macb_readl(bp
, RSR
);
680 macb_writel(bp
, RSR
, status
);
684 netdev_vdbg(bp
->dev
, "poll: status = %08lx, budget = %d\n",
685 (unsigned long)status
, budget
);
687 work_done
= macb_rx(bp
, budget
);
688 if (work_done
< budget
) {
692 * We've done what we can to clean the buffers. Make sure we
693 * get notified when new packets arrive.
695 macb_writel(bp
, IER
, MACB_RX_INT_FLAGS
);
698 /* TODO: Handle errors */
703 static irqreturn_t
macb_interrupt(int irq
, void *dev_id
)
705 struct net_device
*dev
= dev_id
;
706 struct macb
*bp
= netdev_priv(dev
);
709 status
= macb_readl(bp
, ISR
);
711 if (unlikely(!status
))
714 spin_lock(&bp
->lock
);
717 /* close possible race with dev_close */
718 if (unlikely(!netif_running(dev
))) {
719 macb_writel(bp
, IDR
, -1);
723 netdev_vdbg(bp
->dev
, "isr = 0x%08lx\n", (unsigned long)status
);
725 if (status
& MACB_RX_INT_FLAGS
) {
727 * There's no point taking any more interrupts
728 * until we have processed the buffers. The
729 * scheduling call may fail if the poll routine
730 * is already scheduled, so disable interrupts
733 macb_writel(bp
, IDR
, MACB_RX_INT_FLAGS
);
735 if (napi_schedule_prep(&bp
->napi
)) {
736 netdev_vdbg(bp
->dev
, "scheduling RX softirq\n");
737 __napi_schedule(&bp
->napi
);
741 if (unlikely(status
& (MACB_TX_ERR_FLAGS
))) {
742 macb_writel(bp
, IDR
, MACB_TX_INT_FLAGS
);
743 schedule_work(&bp
->tx_error_task
);
747 if (status
& MACB_BIT(TCOMP
))
748 macb_tx_interrupt(bp
);
751 * Link change detection isn't possible with RMII, so we'll
752 * add that if/when we get our hands on a full-blown MII PHY.
755 if (status
& MACB_BIT(ISR_ROVR
)) {
756 /* We missed at least one packet */
758 bp
->hw_stats
.gem
.rx_overruns
++;
760 bp
->hw_stats
.macb
.rx_overruns
++;
763 if (status
& MACB_BIT(HRESP
)) {
765 * TODO: Reset the hardware, and maybe move the
766 * netdev_err to a lower-priority context as well
769 netdev_err(dev
, "DMA bus error: HRESP not OK\n");
772 status
= macb_readl(bp
, ISR
);
775 spin_unlock(&bp
->lock
);
780 #ifdef CONFIG_NET_POLL_CONTROLLER
782 * Polling receive - used by netconsole and other diagnostic tools
783 * to allow network i/o with interrupts disabled.
785 static void macb_poll_controller(struct net_device
*dev
)
789 local_irq_save(flags
);
790 macb_interrupt(dev
->irq
, dev
);
791 local_irq_restore(flags
);
795 static int macb_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
797 struct macb
*bp
= netdev_priv(dev
);
799 unsigned int len
, entry
;
800 struct macb_dma_desc
*desc
;
801 struct macb_tx_skb
*tx_skb
;
805 #if defined(DEBUG) && defined(VERBOSE_DEBUG)
807 "start_xmit: len %u head %p data %p tail %p end %p\n",
808 skb
->len
, skb
->head
, skb
->data
,
809 skb_tail_pointer(skb
), skb_end_pointer(skb
));
810 print_hex_dump(KERN_DEBUG
, "data: ", DUMP_PREFIX_OFFSET
, 16, 1,
811 skb
->data
, 16, true);
815 spin_lock_irqsave(&bp
->lock
, flags
);
817 /* This is a hard error, log it. */
818 if (CIRC_SPACE(bp
->tx_head
, bp
->tx_tail
, TX_RING_SIZE
) < 1) {
819 netif_stop_queue(dev
);
820 spin_unlock_irqrestore(&bp
->lock
, flags
);
821 netdev_err(bp
->dev
, "BUG! Tx Ring full when queue awake!\n");
822 netdev_dbg(bp
->dev
, "tx_head = %u, tx_tail = %u\n",
823 bp
->tx_head
, bp
->tx_tail
);
824 return NETDEV_TX_BUSY
;
827 entry
= macb_tx_ring_wrap(bp
->tx_head
);
829 netdev_vdbg(bp
->dev
, "Allocated ring entry %u\n", entry
);
830 mapping
= dma_map_single(&bp
->pdev
->dev
, skb
->data
,
833 tx_skb
= &bp
->tx_skb
[entry
];
835 tx_skb
->mapping
= mapping
;
836 netdev_vdbg(bp
->dev
, "Mapped skb data %p to DMA addr %08lx\n",
837 skb
->data
, (unsigned long)mapping
);
839 ctrl
= MACB_BF(TX_FRMLEN
, len
);
840 ctrl
|= MACB_BIT(TX_LAST
);
841 if (entry
== (TX_RING_SIZE
- 1))
842 ctrl
|= MACB_BIT(TX_WRAP
);
844 desc
= &bp
->tx_ring
[entry
];
845 desc
->addr
= mapping
;
848 /* Make newly initialized descriptor visible to hardware */
851 skb_tx_timestamp(skb
);
853 macb_writel(bp
, NCR
, macb_readl(bp
, NCR
) | MACB_BIT(TSTART
));
855 if (CIRC_SPACE(bp
->tx_head
, bp
->tx_tail
, TX_RING_SIZE
) < 1)
856 netif_stop_queue(dev
);
858 spin_unlock_irqrestore(&bp
->lock
, flags
);
863 static void macb_free_consistent(struct macb
*bp
)
870 dma_free_coherent(&bp
->pdev
->dev
, RX_RING_BYTES
,
871 bp
->rx_ring
, bp
->rx_ring_dma
);
875 dma_free_coherent(&bp
->pdev
->dev
, TX_RING_BYTES
,
876 bp
->tx_ring
, bp
->tx_ring_dma
);
879 if (bp
->rx_buffers
) {
880 dma_free_coherent(&bp
->pdev
->dev
,
881 RX_RING_SIZE
* RX_BUFFER_SIZE
,
882 bp
->rx_buffers
, bp
->rx_buffers_dma
);
883 bp
->rx_buffers
= NULL
;
887 static int macb_alloc_consistent(struct macb
*bp
)
891 size
= TX_RING_SIZE
* sizeof(struct macb_tx_skb
);
892 bp
->tx_skb
= kmalloc(size
, GFP_KERNEL
);
896 size
= RX_RING_BYTES
;
897 bp
->rx_ring
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
898 &bp
->rx_ring_dma
, GFP_KERNEL
);
902 "Allocated RX ring of %d bytes at %08lx (mapped %p)\n",
903 size
, (unsigned long)bp
->rx_ring_dma
, bp
->rx_ring
);
905 size
= TX_RING_BYTES
;
906 bp
->tx_ring
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
907 &bp
->tx_ring_dma
, GFP_KERNEL
);
911 "Allocated TX ring of %d bytes at %08lx (mapped %p)\n",
912 size
, (unsigned long)bp
->tx_ring_dma
, bp
->tx_ring
);
914 size
= RX_RING_SIZE
* RX_BUFFER_SIZE
;
915 bp
->rx_buffers
= dma_alloc_coherent(&bp
->pdev
->dev
, size
,
916 &bp
->rx_buffers_dma
, GFP_KERNEL
);
920 "Allocated RX buffers of %d bytes at %08lx (mapped %p)\n",
921 size
, (unsigned long)bp
->rx_buffers_dma
, bp
->rx_buffers
);
926 macb_free_consistent(bp
);
930 static void macb_init_rings(struct macb
*bp
)
935 addr
= bp
->rx_buffers_dma
;
936 for (i
= 0; i
< RX_RING_SIZE
; i
++) {
937 bp
->rx_ring
[i
].addr
= addr
;
938 bp
->rx_ring
[i
].ctrl
= 0;
939 addr
+= RX_BUFFER_SIZE
;
941 bp
->rx_ring
[RX_RING_SIZE
- 1].addr
|= MACB_BIT(RX_WRAP
);
943 for (i
= 0; i
< TX_RING_SIZE
; i
++) {
944 bp
->tx_ring
[i
].addr
= 0;
945 bp
->tx_ring
[i
].ctrl
= MACB_BIT(TX_USED
);
947 bp
->tx_ring
[TX_RING_SIZE
- 1].ctrl
|= MACB_BIT(TX_WRAP
);
949 bp
->rx_tail
= bp
->tx_head
= bp
->tx_tail
= 0;
952 static void macb_reset_hw(struct macb
*bp
)
955 * Disable RX and TX (XXX: Should we halt the transmission
958 macb_writel(bp
, NCR
, 0);
960 /* Clear the stats registers (XXX: Update stats first?) */
961 macb_writel(bp
, NCR
, MACB_BIT(CLRSTAT
));
963 /* Clear all status flags */
964 macb_writel(bp
, TSR
, -1);
965 macb_writel(bp
, RSR
, -1);
967 /* Disable all interrupts */
968 macb_writel(bp
, IDR
, -1);
972 static u32
gem_mdc_clk_div(struct macb
*bp
)
975 unsigned long pclk_hz
= clk_get_rate(bp
->pclk
);
977 if (pclk_hz
<= 20000000)
978 config
= GEM_BF(CLK
, GEM_CLK_DIV8
);
979 else if (pclk_hz
<= 40000000)
980 config
= GEM_BF(CLK
, GEM_CLK_DIV16
);
981 else if (pclk_hz
<= 80000000)
982 config
= GEM_BF(CLK
, GEM_CLK_DIV32
);
983 else if (pclk_hz
<= 120000000)
984 config
= GEM_BF(CLK
, GEM_CLK_DIV48
);
985 else if (pclk_hz
<= 160000000)
986 config
= GEM_BF(CLK
, GEM_CLK_DIV64
);
988 config
= GEM_BF(CLK
, GEM_CLK_DIV96
);
993 static u32
macb_mdc_clk_div(struct macb
*bp
)
996 unsigned long pclk_hz
;
999 return gem_mdc_clk_div(bp
);
1001 pclk_hz
= clk_get_rate(bp
->pclk
);
1002 if (pclk_hz
<= 20000000)
1003 config
= MACB_BF(CLK
, MACB_CLK_DIV8
);
1004 else if (pclk_hz
<= 40000000)
1005 config
= MACB_BF(CLK
, MACB_CLK_DIV16
);
1006 else if (pclk_hz
<= 80000000)
1007 config
= MACB_BF(CLK
, MACB_CLK_DIV32
);
1009 config
= MACB_BF(CLK
, MACB_CLK_DIV64
);
1015 * Get the DMA bus width field of the network configuration register that we
1016 * should program. We find the width from decoding the design configuration
1017 * register to find the maximum supported data bus width.
1019 static u32
macb_dbw(struct macb
*bp
)
1021 if (!macb_is_gem(bp
))
1024 switch (GEM_BFEXT(DBWDEF
, gem_readl(bp
, DCFG1
))) {
1026 return GEM_BF(DBW
, GEM_DBW128
);
1028 return GEM_BF(DBW
, GEM_DBW64
);
1031 return GEM_BF(DBW
, GEM_DBW32
);
1036 * Configure the receive DMA engine
1037 * - use the correct receive buffer size
1038 * - set the possibility to use INCR16 bursts
1039 * (if not supported by FIFO, it will fallback to default)
1040 * - set both rx/tx packet buffers to full memory size
1041 * These are configurable parameters for GEM.
1043 static void macb_configure_dma(struct macb
*bp
)
1047 if (macb_is_gem(bp
)) {
1048 dmacfg
= gem_readl(bp
, DMACFG
) & ~GEM_BF(RXBS
, -1L);
1049 dmacfg
|= GEM_BF(RXBS
, RX_BUFFER_SIZE
/ 64);
1050 dmacfg
|= GEM_BF(FBLDO
, 16);
1051 dmacfg
|= GEM_BIT(TXPBMS
) | GEM_BF(RXBMS
, -1L);
1052 gem_writel(bp
, DMACFG
, dmacfg
);
1056 static void macb_init_hw(struct macb
*bp
)
1061 macb_set_hwaddr(bp
);
1063 config
= macb_mdc_clk_div(bp
);
1064 config
|= MACB_BF(RBOF
, NET_IP_ALIGN
); /* Make eth data aligned */
1065 config
|= MACB_BIT(PAE
); /* PAuse Enable */
1066 config
|= MACB_BIT(DRFCS
); /* Discard Rx FCS */
1067 config
|= MACB_BIT(BIG
); /* Receive oversized frames */
1068 if (bp
->dev
->flags
& IFF_PROMISC
)
1069 config
|= MACB_BIT(CAF
); /* Copy All Frames */
1070 if (!(bp
->dev
->flags
& IFF_BROADCAST
))
1071 config
|= MACB_BIT(NBC
); /* No BroadCast */
1072 config
|= macb_dbw(bp
);
1073 macb_writel(bp
, NCFGR
, config
);
1074 bp
->speed
= SPEED_10
;
1075 bp
->duplex
= DUPLEX_HALF
;
1077 macb_configure_dma(bp
);
1079 /* Initialize TX and RX buffers */
1080 macb_writel(bp
, RBQP
, bp
->rx_ring_dma
);
1081 macb_writel(bp
, TBQP
, bp
->tx_ring_dma
);
1083 /* Enable TX and RX */
1084 macb_writel(bp
, NCR
, MACB_BIT(RE
) | MACB_BIT(TE
) | MACB_BIT(MPE
));
1086 /* Enable interrupts */
1087 macb_writel(bp
, IER
, (MACB_RX_INT_FLAGS
1089 | MACB_BIT(HRESP
)));
1094 * The hash address register is 64 bits long and takes up two
1095 * locations in the memory map. The least significant bits are stored
1096 * in EMAC_HSL and the most significant bits in EMAC_HSH.
1098 * The unicast hash enable and the multicast hash enable bits in the
1099 * network configuration register enable the reception of hash matched
1100 * frames. The destination address is reduced to a 6 bit index into
1101 * the 64 bit hash register using the following hash function. The
1102 * hash function is an exclusive or of every sixth bit of the
1103 * destination address.
1105 * hi[5] = da[5] ^ da[11] ^ da[17] ^ da[23] ^ da[29] ^ da[35] ^ da[41] ^ da[47]
1106 * hi[4] = da[4] ^ da[10] ^ da[16] ^ da[22] ^ da[28] ^ da[34] ^ da[40] ^ da[46]
1107 * hi[3] = da[3] ^ da[09] ^ da[15] ^ da[21] ^ da[27] ^ da[33] ^ da[39] ^ da[45]
1108 * hi[2] = da[2] ^ da[08] ^ da[14] ^ da[20] ^ da[26] ^ da[32] ^ da[38] ^ da[44]
1109 * hi[1] = da[1] ^ da[07] ^ da[13] ^ da[19] ^ da[25] ^ da[31] ^ da[37] ^ da[43]
1110 * hi[0] = da[0] ^ da[06] ^ da[12] ^ da[18] ^ da[24] ^ da[30] ^ da[36] ^ da[42]
1112 * da[0] represents the least significant bit of the first byte
1113 * received, that is, the multicast/unicast indicator, and da[47]
1114 * represents the most significant bit of the last byte received. If
1115 * the hash index, hi[n], points to a bit that is set in the hash
1116 * register then the frame will be matched according to whether the
1117 * frame is multicast or unicast. A multicast match will be signalled
1118 * if the multicast hash enable bit is set, da[0] is 1 and the hash
1119 * index points to a bit set in the hash register. A unicast match
1120 * will be signalled if the unicast hash enable bit is set, da[0] is 0
1121 * and the hash index points to a bit set in the hash register. To
1122 * receive all multicast frames, the hash register should be set with
1123 * all ones and the multicast hash enable bit should be set in the
1124 * network configuration register.
1127 static inline int hash_bit_value(int bitnr
, __u8
*addr
)
1129 if (addr
[bitnr
/ 8] & (1 << (bitnr
% 8)))
1135 * Return the hash index value for the specified address.
1137 static int hash_get_index(__u8
*addr
)
1142 for (j
= 0; j
< 6; j
++) {
1143 for (i
= 0, bitval
= 0; i
< 8; i
++)
1144 bitval
^= hash_bit_value(i
*6 + j
, addr
);
1146 hash_index
|= (bitval
<< j
);
1153 * Add multicast addresses to the internal multicast-hash table.
1155 static void macb_sethashtable(struct net_device
*dev
)
1157 struct netdev_hw_addr
*ha
;
1158 unsigned long mc_filter
[2];
1160 struct macb
*bp
= netdev_priv(dev
);
1162 mc_filter
[0] = mc_filter
[1] = 0;
1164 netdev_for_each_mc_addr(ha
, dev
) {
1165 bitnr
= hash_get_index(ha
->addr
);
1166 mc_filter
[bitnr
>> 5] |= 1 << (bitnr
& 31);
1169 macb_or_gem_writel(bp
, HRB
, mc_filter
[0]);
1170 macb_or_gem_writel(bp
, HRT
, mc_filter
[1]);
1174 * Enable/Disable promiscuous and multicast modes.
1176 void macb_set_rx_mode(struct net_device
*dev
)
1179 struct macb
*bp
= netdev_priv(dev
);
1181 cfg
= macb_readl(bp
, NCFGR
);
1183 if (dev
->flags
& IFF_PROMISC
)
1184 /* Enable promiscuous mode */
1185 cfg
|= MACB_BIT(CAF
);
1186 else if (dev
->flags
& (~IFF_PROMISC
))
1187 /* Disable promiscuous mode */
1188 cfg
&= ~MACB_BIT(CAF
);
1190 if (dev
->flags
& IFF_ALLMULTI
) {
1191 /* Enable all multicast mode */
1192 macb_or_gem_writel(bp
, HRB
, -1);
1193 macb_or_gem_writel(bp
, HRT
, -1);
1194 cfg
|= MACB_BIT(NCFGR_MTI
);
1195 } else if (!netdev_mc_empty(dev
)) {
1196 /* Enable specific multicasts */
1197 macb_sethashtable(dev
);
1198 cfg
|= MACB_BIT(NCFGR_MTI
);
1199 } else if (dev
->flags
& (~IFF_ALLMULTI
)) {
1200 /* Disable all multicast mode */
1201 macb_or_gem_writel(bp
, HRB
, 0);
1202 macb_or_gem_writel(bp
, HRT
, 0);
1203 cfg
&= ~MACB_BIT(NCFGR_MTI
);
1206 macb_writel(bp
, NCFGR
, cfg
);
1208 EXPORT_SYMBOL_GPL(macb_set_rx_mode
);
1210 static int macb_open(struct net_device
*dev
)
1212 struct macb
*bp
= netdev_priv(dev
);
1215 netdev_dbg(bp
->dev
, "open\n");
1217 /* carrier starts down */
1218 netif_carrier_off(dev
);
1220 /* if the phy is not yet register, retry later*/
1224 err
= macb_alloc_consistent(bp
);
1226 netdev_err(dev
, "Unable to allocate DMA memory (error %d)\n",
1231 napi_enable(&bp
->napi
);
1233 macb_init_rings(bp
);
1236 /* schedule a link state check */
1237 phy_start(bp
->phy_dev
);
1239 netif_start_queue(dev
);
1244 static int macb_close(struct net_device
*dev
)
1246 struct macb
*bp
= netdev_priv(dev
);
1247 unsigned long flags
;
1249 netif_stop_queue(dev
);
1250 napi_disable(&bp
->napi
);
1253 phy_stop(bp
->phy_dev
);
1255 spin_lock_irqsave(&bp
->lock
, flags
);
1257 netif_carrier_off(dev
);
1258 spin_unlock_irqrestore(&bp
->lock
, flags
);
1260 macb_free_consistent(bp
);
1265 static void gem_update_stats(struct macb
*bp
)
1267 u32 __iomem
*reg
= bp
->regs
+ GEM_OTX
;
1268 u32
*p
= &bp
->hw_stats
.gem
.tx_octets_31_0
;
1269 u32
*end
= &bp
->hw_stats
.gem
.rx_udp_checksum_errors
+ 1;
1271 for (; p
< end
; p
++, reg
++)
1272 *p
+= __raw_readl(reg
);
1275 static struct net_device_stats
*gem_get_stats(struct macb
*bp
)
1277 struct gem_stats
*hwstat
= &bp
->hw_stats
.gem
;
1278 struct net_device_stats
*nstat
= &bp
->stats
;
1280 gem_update_stats(bp
);
1282 nstat
->rx_errors
= (hwstat
->rx_frame_check_sequence_errors
+
1283 hwstat
->rx_alignment_errors
+
1284 hwstat
->rx_resource_errors
+
1285 hwstat
->rx_overruns
+
1286 hwstat
->rx_oversize_frames
+
1287 hwstat
->rx_jabbers
+
1288 hwstat
->rx_undersized_frames
+
1289 hwstat
->rx_length_field_frame_errors
);
1290 nstat
->tx_errors
= (hwstat
->tx_late_collisions
+
1291 hwstat
->tx_excessive_collisions
+
1292 hwstat
->tx_underrun
+
1293 hwstat
->tx_carrier_sense_errors
);
1294 nstat
->multicast
= hwstat
->rx_multicast_frames
;
1295 nstat
->collisions
= (hwstat
->tx_single_collision_frames
+
1296 hwstat
->tx_multiple_collision_frames
+
1297 hwstat
->tx_excessive_collisions
);
1298 nstat
->rx_length_errors
= (hwstat
->rx_oversize_frames
+
1299 hwstat
->rx_jabbers
+
1300 hwstat
->rx_undersized_frames
+
1301 hwstat
->rx_length_field_frame_errors
);
1302 nstat
->rx_over_errors
= hwstat
->rx_resource_errors
;
1303 nstat
->rx_crc_errors
= hwstat
->rx_frame_check_sequence_errors
;
1304 nstat
->rx_frame_errors
= hwstat
->rx_alignment_errors
;
1305 nstat
->rx_fifo_errors
= hwstat
->rx_overruns
;
1306 nstat
->tx_aborted_errors
= hwstat
->tx_excessive_collisions
;
1307 nstat
->tx_carrier_errors
= hwstat
->tx_carrier_sense_errors
;
1308 nstat
->tx_fifo_errors
= hwstat
->tx_underrun
;
1313 struct net_device_stats
*macb_get_stats(struct net_device
*dev
)
1315 struct macb
*bp
= netdev_priv(dev
);
1316 struct net_device_stats
*nstat
= &bp
->stats
;
1317 struct macb_stats
*hwstat
= &bp
->hw_stats
.macb
;
1319 if (macb_is_gem(bp
))
1320 return gem_get_stats(bp
);
1322 /* read stats from hardware */
1323 macb_update_stats(bp
);
1325 /* Convert HW stats into netdevice stats */
1326 nstat
->rx_errors
= (hwstat
->rx_fcs_errors
+
1327 hwstat
->rx_align_errors
+
1328 hwstat
->rx_resource_errors
+
1329 hwstat
->rx_overruns
+
1330 hwstat
->rx_oversize_pkts
+
1331 hwstat
->rx_jabbers
+
1332 hwstat
->rx_undersize_pkts
+
1333 hwstat
->sqe_test_errors
+
1334 hwstat
->rx_length_mismatch
);
1335 nstat
->tx_errors
= (hwstat
->tx_late_cols
+
1336 hwstat
->tx_excessive_cols
+
1337 hwstat
->tx_underruns
+
1338 hwstat
->tx_carrier_errors
);
1339 nstat
->collisions
= (hwstat
->tx_single_cols
+
1340 hwstat
->tx_multiple_cols
+
1341 hwstat
->tx_excessive_cols
);
1342 nstat
->rx_length_errors
= (hwstat
->rx_oversize_pkts
+
1343 hwstat
->rx_jabbers
+
1344 hwstat
->rx_undersize_pkts
+
1345 hwstat
->rx_length_mismatch
);
1346 nstat
->rx_over_errors
= hwstat
->rx_resource_errors
+
1347 hwstat
->rx_overruns
;
1348 nstat
->rx_crc_errors
= hwstat
->rx_fcs_errors
;
1349 nstat
->rx_frame_errors
= hwstat
->rx_align_errors
;
1350 nstat
->rx_fifo_errors
= hwstat
->rx_overruns
;
1351 /* XXX: What does "missed" mean? */
1352 nstat
->tx_aborted_errors
= hwstat
->tx_excessive_cols
;
1353 nstat
->tx_carrier_errors
= hwstat
->tx_carrier_errors
;
1354 nstat
->tx_fifo_errors
= hwstat
->tx_underruns
;
1355 /* Don't know about heartbeat or window errors... */
1359 EXPORT_SYMBOL_GPL(macb_get_stats
);
1361 static int macb_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1363 struct macb
*bp
= netdev_priv(dev
);
1364 struct phy_device
*phydev
= bp
->phy_dev
;
1369 return phy_ethtool_gset(phydev
, cmd
);
1372 static int macb_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
1374 struct macb
*bp
= netdev_priv(dev
);
1375 struct phy_device
*phydev
= bp
->phy_dev
;
1380 return phy_ethtool_sset(phydev
, cmd
);
1383 static int macb_get_regs_len(struct net_device
*netdev
)
1385 return MACB_GREGS_NBR
* sizeof(u32
);
1388 static void macb_get_regs(struct net_device
*dev
, struct ethtool_regs
*regs
,
1391 struct macb
*bp
= netdev_priv(dev
);
1392 unsigned int tail
, head
;
1395 regs
->version
= (macb_readl(bp
, MID
) & ((1 << MACB_REV_SIZE
) - 1))
1396 | MACB_GREGS_VERSION
;
1398 tail
= macb_tx_ring_wrap(bp
->tx_tail
);
1399 head
= macb_tx_ring_wrap(bp
->tx_head
);
1401 regs_buff
[0] = macb_readl(bp
, NCR
);
1402 regs_buff
[1] = macb_or_gem_readl(bp
, NCFGR
);
1403 regs_buff
[2] = macb_readl(bp
, NSR
);
1404 regs_buff
[3] = macb_readl(bp
, TSR
);
1405 regs_buff
[4] = macb_readl(bp
, RBQP
);
1406 regs_buff
[5] = macb_readl(bp
, TBQP
);
1407 regs_buff
[6] = macb_readl(bp
, RSR
);
1408 regs_buff
[7] = macb_readl(bp
, IMR
);
1410 regs_buff
[8] = tail
;
1411 regs_buff
[9] = head
;
1412 regs_buff
[10] = macb_tx_dma(bp
, tail
);
1413 regs_buff
[11] = macb_tx_dma(bp
, head
);
1415 if (macb_is_gem(bp
)) {
1416 regs_buff
[12] = gem_readl(bp
, USRIO
);
1417 regs_buff
[13] = gem_readl(bp
, DMACFG
);
1421 const struct ethtool_ops macb_ethtool_ops
= {
1422 .get_settings
= macb_get_settings
,
1423 .set_settings
= macb_set_settings
,
1424 .get_regs_len
= macb_get_regs_len
,
1425 .get_regs
= macb_get_regs
,
1426 .get_link
= ethtool_op_get_link
,
1427 .get_ts_info
= ethtool_op_get_ts_info
,
1429 EXPORT_SYMBOL_GPL(macb_ethtool_ops
);
1431 int macb_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
1433 struct macb
*bp
= netdev_priv(dev
);
1434 struct phy_device
*phydev
= bp
->phy_dev
;
1436 if (!netif_running(dev
))
1442 return phy_mii_ioctl(phydev
, rq
, cmd
);
1444 EXPORT_SYMBOL_GPL(macb_ioctl
);
1446 static const struct net_device_ops macb_netdev_ops
= {
1447 .ndo_open
= macb_open
,
1448 .ndo_stop
= macb_close
,
1449 .ndo_start_xmit
= macb_start_xmit
,
1450 .ndo_set_rx_mode
= macb_set_rx_mode
,
1451 .ndo_get_stats
= macb_get_stats
,
1452 .ndo_do_ioctl
= macb_ioctl
,
1453 .ndo_validate_addr
= eth_validate_addr
,
1454 .ndo_change_mtu
= eth_change_mtu
,
1455 .ndo_set_mac_address
= eth_mac_addr
,
1456 #ifdef CONFIG_NET_POLL_CONTROLLER
1457 .ndo_poll_controller
= macb_poll_controller
,
1461 #if defined(CONFIG_OF)
1462 static const struct of_device_id macb_dt_ids
[] = {
1463 { .compatible
= "cdns,at32ap7000-macb" },
1464 { .compatible
= "cdns,at91sam9260-macb" },
1465 { .compatible
= "cdns,macb" },
1466 { .compatible
= "cdns,pc302-gem" },
1467 { .compatible
= "cdns,gem" },
1471 MODULE_DEVICE_TABLE(of
, macb_dt_ids
);
1473 static int macb_get_phy_mode_dt(struct platform_device
*pdev
)
1475 struct device_node
*np
= pdev
->dev
.of_node
;
1478 return of_get_phy_mode(np
);
1483 static int macb_get_hwaddr_dt(struct macb
*bp
)
1485 struct device_node
*np
= bp
->pdev
->dev
.of_node
;
1487 const char *mac
= of_get_mac_address(np
);
1489 memcpy(bp
->dev
->dev_addr
, mac
, ETH_ALEN
);
1497 static int macb_get_phy_mode_dt(struct platform_device
*pdev
)
1501 static int macb_get_hwaddr_dt(struct macb
*bp
)
1507 static int __init
macb_probe(struct platform_device
*pdev
)
1509 struct macb_platform_data
*pdata
;
1510 struct resource
*regs
;
1511 struct net_device
*dev
;
1513 struct phy_device
*phydev
;
1516 struct pinctrl
*pinctrl
;
1518 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1520 dev_err(&pdev
->dev
, "no mmio resource defined\n");
1524 pinctrl
= devm_pinctrl_get_select_default(&pdev
->dev
);
1525 if (IS_ERR(pinctrl
)) {
1526 err
= PTR_ERR(pinctrl
);
1527 if (err
== -EPROBE_DEFER
)
1530 dev_warn(&pdev
->dev
, "No pinctrl provided\n");
1534 dev
= alloc_etherdev(sizeof(*bp
));
1538 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1540 /* TODO: Actually, we have some interesting features... */
1543 bp
= netdev_priv(dev
);
1547 spin_lock_init(&bp
->lock
);
1548 INIT_WORK(&bp
->tx_error_task
, macb_tx_error_task
);
1550 bp
->pclk
= clk_get(&pdev
->dev
, "pclk");
1551 if (IS_ERR(bp
->pclk
)) {
1552 dev_err(&pdev
->dev
, "failed to get macb_clk\n");
1553 goto err_out_free_dev
;
1555 clk_enable(bp
->pclk
);
1557 bp
->hclk
= clk_get(&pdev
->dev
, "hclk");
1558 if (IS_ERR(bp
->hclk
)) {
1559 dev_err(&pdev
->dev
, "failed to get hclk\n");
1560 goto err_out_put_pclk
;
1562 clk_enable(bp
->hclk
);
1564 bp
->regs
= ioremap(regs
->start
, resource_size(regs
));
1566 dev_err(&pdev
->dev
, "failed to map registers, aborting.\n");
1568 goto err_out_disable_clocks
;
1571 dev
->irq
= platform_get_irq(pdev
, 0);
1572 err
= request_irq(dev
->irq
, macb_interrupt
, 0, dev
->name
, dev
);
1574 dev_err(&pdev
->dev
, "Unable to request IRQ %d (error %d)\n",
1576 goto err_out_iounmap
;
1579 dev
->netdev_ops
= &macb_netdev_ops
;
1580 netif_napi_add(dev
, &bp
->napi
, macb_poll
, 64);
1581 dev
->ethtool_ops
= &macb_ethtool_ops
;
1583 dev
->base_addr
= regs
->start
;
1585 /* Set MII management clock divider */
1586 config
= macb_mdc_clk_div(bp
);
1587 config
|= macb_dbw(bp
);
1588 macb_writel(bp
, NCFGR
, config
);
1590 err
= macb_get_hwaddr_dt(bp
);
1592 macb_get_hwaddr(bp
);
1594 err
= macb_get_phy_mode_dt(pdev
);
1596 pdata
= pdev
->dev
.platform_data
;
1597 if (pdata
&& pdata
->is_rmii
)
1598 bp
->phy_interface
= PHY_INTERFACE_MODE_RMII
;
1600 bp
->phy_interface
= PHY_INTERFACE_MODE_MII
;
1602 bp
->phy_interface
= err
;
1605 if (bp
->phy_interface
== PHY_INTERFACE_MODE_RGMII
)
1606 macb_or_gem_writel(bp
, USRIO
, GEM_BIT(RGMII
));
1607 else if (bp
->phy_interface
== PHY_INTERFACE_MODE_RMII
)
1608 #if defined(CONFIG_ARCH_AT91)
1609 macb_or_gem_writel(bp
, USRIO
, (MACB_BIT(RMII
) |
1612 macb_or_gem_writel(bp
, USRIO
, 0);
1615 #if defined(CONFIG_ARCH_AT91)
1616 macb_or_gem_writel(bp
, USRIO
, MACB_BIT(CLKEN
));
1618 macb_or_gem_writel(bp
, USRIO
, MACB_BIT(MII
));
1621 err
= register_netdev(dev
);
1623 dev_err(&pdev
->dev
, "Cannot register net device, aborting.\n");
1624 goto err_out_free_irq
;
1627 if (macb_mii_init(bp
) != 0) {
1628 goto err_out_unregister_netdev
;
1631 platform_set_drvdata(pdev
, dev
);
1633 netif_carrier_off(dev
);
1635 netdev_info(dev
, "Cadence %s at 0x%08lx irq %d (%pM)\n",
1636 macb_is_gem(bp
) ? "GEM" : "MACB", dev
->base_addr
,
1637 dev
->irq
, dev
->dev_addr
);
1639 phydev
= bp
->phy_dev
;
1640 netdev_info(dev
, "attached PHY driver [%s] (mii_bus:phy_addr=%s, irq=%d)\n",
1641 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
1645 err_out_unregister_netdev
:
1646 unregister_netdev(dev
);
1648 free_irq(dev
->irq
, dev
);
1651 err_out_disable_clocks
:
1652 clk_disable(bp
->hclk
);
1654 clk_disable(bp
->pclk
);
1660 platform_set_drvdata(pdev
, NULL
);
1664 static int __exit
macb_remove(struct platform_device
*pdev
)
1666 struct net_device
*dev
;
1669 dev
= platform_get_drvdata(pdev
);
1672 bp
= netdev_priv(dev
);
1674 phy_disconnect(bp
->phy_dev
);
1675 mdiobus_unregister(bp
->mii_bus
);
1676 kfree(bp
->mii_bus
->irq
);
1677 mdiobus_free(bp
->mii_bus
);
1678 unregister_netdev(dev
);
1679 free_irq(dev
->irq
, dev
);
1681 clk_disable(bp
->hclk
);
1683 clk_disable(bp
->pclk
);
1686 platform_set_drvdata(pdev
, NULL
);
1693 static int macb_suspend(struct platform_device
*pdev
, pm_message_t state
)
1695 struct net_device
*netdev
= platform_get_drvdata(pdev
);
1696 struct macb
*bp
= netdev_priv(netdev
);
1698 netif_carrier_off(netdev
);
1699 netif_device_detach(netdev
);
1701 clk_disable(bp
->hclk
);
1702 clk_disable(bp
->pclk
);
1707 static int macb_resume(struct platform_device
*pdev
)
1709 struct net_device
*netdev
= platform_get_drvdata(pdev
);
1710 struct macb
*bp
= netdev_priv(netdev
);
1712 clk_enable(bp
->pclk
);
1713 clk_enable(bp
->hclk
);
1715 netif_device_attach(netdev
);
1720 #define macb_suspend NULL
1721 #define macb_resume NULL
1724 static struct platform_driver macb_driver
= {
1725 .remove
= __exit_p(macb_remove
),
1726 .suspend
= macb_suspend
,
1727 .resume
= macb_resume
,
1730 .owner
= THIS_MODULE
,
1731 .of_match_table
= of_match_ptr(macb_dt_ids
),
1735 static int __init
macb_init(void)
1737 return platform_driver_probe(&macb_driver
, macb_probe
);
1740 static void __exit
macb_exit(void)
1742 platform_driver_unregister(&macb_driver
);
1745 module_init(macb_init
);
1746 module_exit(macb_exit
);
1748 MODULE_LICENSE("GPL");
1749 MODULE_DESCRIPTION("Cadence MACB/GEM Ethernet driver");
1750 MODULE_AUTHOR("Haavard Skinnemoen (Atmel)");
1751 MODULE_ALIAS("platform:macb");