3 * Alchemy Au1x00 ethernet driver
5 * Copyright 2001-2003, 2006 MontaVista Software Inc.
6 * Copyright 2002 TimeSys Corp.
7 * Added ethtool/mii-tool support,
8 * Copyright 2004 Matt Porter <mporter@kernel.crashing.org>
9 * Update: 2004 Bjoern Riemer, riemer@fokus.fraunhofer.de
10 * or riemer@riemer-nt.de: fixed the link beat detection with
11 * ioctls (SIOCGMIIPHY)
12 * Copyright 2006 Herbert Valerio Riedel <hvr@gnu.org>
13 * converted to use linux-2.6.x's PHY framework
15 * Author: MontaVista Software, Inc.
16 * ppopov@mvista.com or source@mvista.com
18 * ########################################################################
20 * This program is free software; you can distribute it and/or modify it
21 * under the terms of the GNU General Public License (Version 2) as
22 * published by the Free Software Foundation.
24 * This program is distributed in the hope it will be useful, but WITHOUT
25 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
26 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
29 * You should have received a copy of the GNU General Public License along
30 * with this program; if not, see <http://www.gnu.org/licenses/>.
32 * ########################################################################
36 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
38 #include <linux/capability.h>
39 #include <linux/dma-mapping.h>
40 #include <linux/module.h>
41 #include <linux/kernel.h>
42 #include <linux/string.h>
43 #include <linux/timer.h>
44 #include <linux/errno.h>
46 #include <linux/ioport.h>
47 #include <linux/bitops.h>
48 #include <linux/slab.h>
49 #include <linux/interrupt.h>
50 #include <linux/netdevice.h>
51 #include <linux/etherdevice.h>
52 #include <linux/ethtool.h>
53 #include <linux/mii.h>
54 #include <linux/skbuff.h>
55 #include <linux/delay.h>
56 #include <linux/crc32.h>
57 #include <linux/phy.h>
58 #include <linux/platform_device.h>
59 #include <linux/cpu.h>
62 #include <asm/mipsregs.h>
64 #include <asm/processor.h>
67 #include <au1xxx_eth.h>
70 #include "au1000_eth.h"
72 #ifdef AU1000_ETH_DEBUG
73 static int au1000_debug
= 5;
75 static int au1000_debug
= 3;
78 #define AU1000_DEF_MSG_ENABLE (NETIF_MSG_DRV | \
82 #define DRV_NAME "au1000_eth"
83 #define DRV_VERSION "1.7"
84 #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
85 #define DRV_DESC "Au1xxx on-chip Ethernet driver"
87 MODULE_AUTHOR(DRV_AUTHOR
);
88 MODULE_DESCRIPTION(DRV_DESC
);
89 MODULE_LICENSE("GPL");
90 MODULE_VERSION(DRV_VERSION
);
95 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
96 * There are four receive and four transmit descriptors. These
97 * descriptors are not in memory; rather, they are just a set of
100 * Since the Au1000 has a coherent data cache, the receive and
101 * transmit buffers are allocated from the KSEG0 segment. The
102 * hardware registers, however, are still mapped at KSEG1 to
103 * make sure there's no out-of-order writes, and that all writes
104 * complete immediately.
108 * board-specific configurations
110 * PHY detection algorithm
112 * If phy_static_config is undefined, the PHY setup is
115 * mii_probe() first searches the current MAC's MII bus for a PHY,
116 * selecting the first (or last, if phy_search_highest_addr is
117 * defined) PHY address not already claimed by another netdev.
119 * If nothing was found that way when searching for the 2nd ethernet
120 * controller's PHY and phy1_search_mac0 is defined, then
121 * the first MII bus is searched as well for an unclaimed PHY; this is
122 * needed in case of a dual-PHY accessible only through the MAC0's MII
125 * Finally, if no PHY is found, then the corresponding ethernet
126 * controller is not registered to the network subsystem.
129 /* autodetection defaults: phy1_search_mac0 */
133 * most boards PHY setup should be detectable properly with the
134 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
135 * you have a switch attached, or want to use the PHY's interrupt
136 * notification capabilities) you can provide a static PHY
139 * IRQs may only be set, if a PHY address was configured
140 * If a PHY address is given, also a bus id is required to be set
142 * ps: make sure the used irqs are configured properly in the board
146 static void au1000_enable_mac(struct net_device
*dev
, int force_reset
)
149 struct au1000_private
*aup
= netdev_priv(dev
);
151 spin_lock_irqsave(&aup
->lock
, flags
);
153 if (force_reset
|| (!aup
->mac_enabled
)) {
154 writel(MAC_EN_CLOCK_ENABLE
, aup
->enable
);
156 writel((MAC_EN_RESET0
| MAC_EN_RESET1
| MAC_EN_RESET2
157 | MAC_EN_CLOCK_ENABLE
), aup
->enable
);
160 aup
->mac_enabled
= 1;
163 spin_unlock_irqrestore(&aup
->lock
, flags
);
169 static int au1000_mdio_read(struct net_device
*dev
, int phy_addr
, int reg
)
171 struct au1000_private
*aup
= netdev_priv(dev
);
172 u32
*const mii_control_reg
= &aup
->mac
->mii_control
;
173 u32
*const mii_data_reg
= &aup
->mac
->mii_data
;
177 while (readl(mii_control_reg
) & MAC_MII_BUSY
) {
179 if (--timedout
== 0) {
180 netdev_err(dev
, "read_MII busy timeout!!\n");
185 mii_control
= MAC_SET_MII_SELECT_REG(reg
) |
186 MAC_SET_MII_SELECT_PHY(phy_addr
) | MAC_MII_READ
;
188 writel(mii_control
, mii_control_reg
);
191 while (readl(mii_control_reg
) & MAC_MII_BUSY
) {
193 if (--timedout
== 0) {
194 netdev_err(dev
, "mdio_read busy timeout!!\n");
198 return readl(mii_data_reg
);
201 static void au1000_mdio_write(struct net_device
*dev
, int phy_addr
,
204 struct au1000_private
*aup
= netdev_priv(dev
);
205 u32
*const mii_control_reg
= &aup
->mac
->mii_control
;
206 u32
*const mii_data_reg
= &aup
->mac
->mii_data
;
210 while (readl(mii_control_reg
) & MAC_MII_BUSY
) {
212 if (--timedout
== 0) {
213 netdev_err(dev
, "mdio_write busy timeout!!\n");
218 mii_control
= MAC_SET_MII_SELECT_REG(reg
) |
219 MAC_SET_MII_SELECT_PHY(phy_addr
) | MAC_MII_WRITE
;
221 writel(value
, mii_data_reg
);
222 writel(mii_control
, mii_control_reg
);
225 static int au1000_mdiobus_read(struct mii_bus
*bus
, int phy_addr
, int regnum
)
227 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
228 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus)
230 struct net_device
*const dev
= bus
->priv
;
232 /* make sure the MAC associated with this
235 au1000_enable_mac(dev
, 0);
237 return au1000_mdio_read(dev
, phy_addr
, regnum
);
240 static int au1000_mdiobus_write(struct mii_bus
*bus
, int phy_addr
, int regnum
,
243 struct net_device
*const dev
= bus
->priv
;
245 /* make sure the MAC associated with this
248 au1000_enable_mac(dev
, 0);
250 au1000_mdio_write(dev
, phy_addr
, regnum
, value
);
254 static int au1000_mdiobus_reset(struct mii_bus
*bus
)
256 struct net_device
*const dev
= bus
->priv
;
258 /* make sure the MAC associated with this
261 au1000_enable_mac(dev
, 0);
266 static void au1000_hard_stop(struct net_device
*dev
)
268 struct au1000_private
*aup
= netdev_priv(dev
);
271 netif_dbg(aup
, drv
, dev
, "hard stop\n");
273 reg
= readl(&aup
->mac
->control
);
274 reg
&= ~(MAC_RX_ENABLE
| MAC_TX_ENABLE
);
275 writel(reg
, &aup
->mac
->control
);
279 static void au1000_enable_rx_tx(struct net_device
*dev
)
281 struct au1000_private
*aup
= netdev_priv(dev
);
284 netif_dbg(aup
, hw
, dev
, "enable_rx_tx\n");
286 reg
= readl(&aup
->mac
->control
);
287 reg
|= (MAC_RX_ENABLE
| MAC_TX_ENABLE
);
288 writel(reg
, &aup
->mac
->control
);
293 au1000_adjust_link(struct net_device
*dev
)
295 struct au1000_private
*aup
= netdev_priv(dev
);
296 struct phy_device
*phydev
= aup
->phy_dev
;
300 int status_change
= 0;
302 BUG_ON(!aup
->phy_dev
);
304 spin_lock_irqsave(&aup
->lock
, flags
);
306 if (phydev
->link
&& (aup
->old_speed
!= phydev
->speed
)) {
309 switch (phydev
->speed
) {
314 netdev_warn(dev
, "Speed (%d) is not 10/100 ???\n",
319 aup
->old_speed
= phydev
->speed
;
324 if (phydev
->link
&& (aup
->old_duplex
!= phydev
->duplex
)) {
325 /* duplex mode changed */
327 /* switching duplex mode requires to disable rx and tx! */
328 au1000_hard_stop(dev
);
330 reg
= readl(&aup
->mac
->control
);
331 if (DUPLEX_FULL
== phydev
->duplex
) {
332 reg
|= MAC_FULL_DUPLEX
;
333 reg
&= ~MAC_DISABLE_RX_OWN
;
335 reg
&= ~MAC_FULL_DUPLEX
;
336 reg
|= MAC_DISABLE_RX_OWN
;
338 writel(reg
, &aup
->mac
->control
);
341 au1000_enable_rx_tx(dev
);
342 aup
->old_duplex
= phydev
->duplex
;
347 if (phydev
->link
!= aup
->old_link
) {
348 /* link state changed */
353 aup
->old_duplex
= -1;
356 aup
->old_link
= phydev
->link
;
360 spin_unlock_irqrestore(&aup
->lock
, flags
);
364 netdev_info(dev
, "link up (%d/%s)\n",
366 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
368 netdev_info(dev
, "link down\n");
372 static int au1000_mii_probe(struct net_device
*dev
)
374 struct au1000_private
*const aup
= netdev_priv(dev
);
375 struct phy_device
*phydev
= NULL
;
378 if (aup
->phy_static_config
) {
379 BUG_ON(aup
->mac_id
< 0 || aup
->mac_id
> 1);
382 phydev
= aup
->mii_bus
->phy_map
[aup
->phy_addr
];
384 netdev_info(dev
, "using PHY-less setup\n");
388 /* find the first (lowest address) PHY
389 * on the current MAC's MII bus
391 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++)
392 if (aup
->mii_bus
->phy_map
[phy_addr
]) {
393 phydev
= aup
->mii_bus
->phy_map
[phy_addr
];
394 if (!aup
->phy_search_highest_addr
)
395 /* break out with first one found */
399 if (aup
->phy1_search_mac0
) {
400 /* try harder to find a PHY */
401 if (!phydev
&& (aup
->mac_id
== 1)) {
402 /* no PHY found, maybe we have a dual PHY? */
403 dev_info(&dev
->dev
, ": no PHY found on MAC1, "
404 "let's see if it's attached to MAC0...\n");
406 /* find the first (lowest address) non-attached
407 * PHY on the MAC0 MII bus
409 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++) {
410 struct phy_device
*const tmp_phydev
=
411 aup
->mii_bus
->phy_map
[phy_addr
];
413 if (aup
->mac_id
== 1)
420 /* already claimed by MAC0 */
421 if (tmp_phydev
->attached_dev
)
425 break; /* found it */
431 netdev_err(dev
, "no PHY found\n");
435 /* now we are supposed to have a proper phydev, to attach to... */
436 BUG_ON(phydev
->attached_dev
);
438 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
),
439 &au1000_adjust_link
, PHY_INTERFACE_MODE_MII
);
441 if (IS_ERR(phydev
)) {
442 netdev_err(dev
, "Could not attach to PHY\n");
443 return PTR_ERR(phydev
);
446 /* mask with MAC supported features */
447 phydev
->supported
&= (SUPPORTED_10baseT_Half
448 | SUPPORTED_10baseT_Full
449 | SUPPORTED_100baseT_Half
450 | SUPPORTED_100baseT_Full
452 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
456 phydev
->advertising
= phydev
->supported
;
460 aup
->old_duplex
= -1;
461 aup
->phy_dev
= phydev
;
463 netdev_info(dev
, "attached PHY driver [%s] "
464 "(mii_bus:phy_addr=%s, irq=%d)\n",
465 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
472 * Buffer allocation/deallocation routines. The buffer descriptor returned
473 * has the virtual and dma address of a buffer suitable for
474 * both, receive and transmit operations.
476 static struct db_dest
*au1000_GetFreeDB(struct au1000_private
*aup
)
482 aup
->pDBfree
= pDB
->pnext
;
487 void au1000_ReleaseDB(struct au1000_private
*aup
, struct db_dest
*pDB
)
489 struct db_dest
*pDBfree
= aup
->pDBfree
;
491 pDBfree
->pnext
= pDB
;
495 static void au1000_reset_mac_unlocked(struct net_device
*dev
)
497 struct au1000_private
*const aup
= netdev_priv(dev
);
500 au1000_hard_stop(dev
);
502 writel(MAC_EN_CLOCK_ENABLE
, aup
->enable
);
504 writel(0, aup
->enable
);
508 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
509 /* reset control bits */
510 aup
->rx_dma_ring
[i
]->buff_stat
&= ~0xf;
512 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
513 /* reset control bits */
514 aup
->tx_dma_ring
[i
]->buff_stat
&= ~0xf;
517 aup
->mac_enabled
= 0;
521 static void au1000_reset_mac(struct net_device
*dev
)
523 struct au1000_private
*const aup
= netdev_priv(dev
);
526 netif_dbg(aup
, hw
, dev
, "reset mac, aup %x\n",
529 spin_lock_irqsave(&aup
->lock
, flags
);
531 au1000_reset_mac_unlocked(dev
);
533 spin_unlock_irqrestore(&aup
->lock
, flags
);
537 * Setup the receive and transmit "rings". These pointers are the addresses
538 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
539 * these are not descriptors sitting in memory.
542 au1000_setup_hw_rings(struct au1000_private
*aup
, void __iomem
*tx_base
)
546 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
547 aup
->rx_dma_ring
[i
] = (struct rx_dma
*)
548 (tx_base
+ 0x100 + sizeof(struct rx_dma
) * i
);
550 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
551 aup
->tx_dma_ring
[i
] = (struct tx_dma
*)
552 (tx_base
+ sizeof(struct tx_dma
) * i
);
560 static int au1000_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
562 struct au1000_private
*aup
= netdev_priv(dev
);
565 return phy_ethtool_gset(aup
->phy_dev
, cmd
);
570 static int au1000_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
572 struct au1000_private
*aup
= netdev_priv(dev
);
574 if (!capable(CAP_NET_ADMIN
))
578 return phy_ethtool_sset(aup
->phy_dev
, cmd
);
584 au1000_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
586 struct au1000_private
*aup
= netdev_priv(dev
);
588 strlcpy(info
->driver
, DRV_NAME
, sizeof(info
->driver
));
589 strlcpy(info
->version
, DRV_VERSION
, sizeof(info
->version
));
590 snprintf(info
->bus_info
, sizeof(info
->bus_info
), "%s %d", DRV_NAME
,
592 info
->regdump_len
= 0;
595 static void au1000_set_msglevel(struct net_device
*dev
, u32 value
)
597 struct au1000_private
*aup
= netdev_priv(dev
);
598 aup
->msg_enable
= value
;
601 static u32
au1000_get_msglevel(struct net_device
*dev
)
603 struct au1000_private
*aup
= netdev_priv(dev
);
604 return aup
->msg_enable
;
607 static const struct ethtool_ops au1000_ethtool_ops
= {
608 .get_settings
= au1000_get_settings
,
609 .set_settings
= au1000_set_settings
,
610 .get_drvinfo
= au1000_get_drvinfo
,
611 .get_link
= ethtool_op_get_link
,
612 .get_msglevel
= au1000_get_msglevel
,
613 .set_msglevel
= au1000_set_msglevel
,
618 * Initialize the interface.
620 * When the device powers up, the clocks are disabled and the
621 * mac is in reset state. When the interface is closed, we
622 * do the same -- reset the device and disable the clocks to
623 * conserve power. Thus, whenever au1000_init() is called,
624 * the device should already be in reset state.
626 static int au1000_init(struct net_device
*dev
)
628 struct au1000_private
*aup
= netdev_priv(dev
);
633 netif_dbg(aup
, hw
, dev
, "au1000_init\n");
635 /* bring the device out of reset */
636 au1000_enable_mac(dev
, 1);
638 spin_lock_irqsave(&aup
->lock
, flags
);
640 writel(0, &aup
->mac
->control
);
641 aup
->tx_head
= (aup
->tx_dma_ring
[0]->buff_stat
& 0xC) >> 2;
642 aup
->tx_tail
= aup
->tx_head
;
643 aup
->rx_head
= (aup
->rx_dma_ring
[0]->buff_stat
& 0xC) >> 2;
645 writel(dev
->dev_addr
[5]<<8 | dev
->dev_addr
[4],
646 &aup
->mac
->mac_addr_high
);
647 writel(dev
->dev_addr
[3]<<24 | dev
->dev_addr
[2]<<16 |
648 dev
->dev_addr
[1]<<8 | dev
->dev_addr
[0],
649 &aup
->mac
->mac_addr_low
);
652 for (i
= 0; i
< NUM_RX_DMA
; i
++)
653 aup
->rx_dma_ring
[i
]->buff_stat
|= RX_DMA_ENABLE
;
657 control
= MAC_RX_ENABLE
| MAC_TX_ENABLE
;
658 #ifndef CONFIG_CPU_LITTLE_ENDIAN
659 control
|= MAC_BIG_ENDIAN
;
662 if (aup
->phy_dev
->link
&& (DUPLEX_FULL
== aup
->phy_dev
->duplex
))
663 control
|= MAC_FULL_DUPLEX
;
665 control
|= MAC_DISABLE_RX_OWN
;
666 } else { /* PHY-less op, assume full-duplex */
667 control
|= MAC_FULL_DUPLEX
;
670 writel(control
, &aup
->mac
->control
);
671 writel(0x8100, &aup
->mac
->vlan1_tag
); /* activate vlan support */
674 spin_unlock_irqrestore(&aup
->lock
, flags
);
678 static inline void au1000_update_rx_stats(struct net_device
*dev
, u32 status
)
680 struct net_device_stats
*ps
= &dev
->stats
;
683 if (status
& RX_MCAST_FRAME
)
686 if (status
& RX_ERROR
) {
688 if (status
& RX_MISSED_FRAME
)
689 ps
->rx_missed_errors
++;
690 if (status
& (RX_OVERLEN
| RX_RUNT
| RX_LEN_ERROR
))
691 ps
->rx_length_errors
++;
692 if (status
& RX_CRC_ERROR
)
694 if (status
& RX_COLL
)
697 ps
->rx_bytes
+= status
& RX_FRAME_LEN_MASK
;
702 * Au1000 receive routine.
704 static int au1000_rx(struct net_device
*dev
)
706 struct au1000_private
*aup
= netdev_priv(dev
);
709 u32 buff_stat
, status
;
713 netif_dbg(aup
, rx_status
, dev
, "au1000_rx head %d\n", aup
->rx_head
);
715 prxd
= aup
->rx_dma_ring
[aup
->rx_head
];
716 buff_stat
= prxd
->buff_stat
;
717 while (buff_stat
& RX_T_DONE
) {
718 status
= prxd
->status
;
719 pDB
= aup
->rx_db_inuse
[aup
->rx_head
];
720 au1000_update_rx_stats(dev
, status
);
721 if (!(status
& RX_ERROR
)) {
724 frmlen
= (status
& RX_FRAME_LEN_MASK
);
725 frmlen
-= 4; /* Remove FCS */
726 skb
= netdev_alloc_skb(dev
, frmlen
+ 2);
728 dev
->stats
.rx_dropped
++;
731 skb_reserve(skb
, 2); /* 16 byte IP header align */
732 skb_copy_to_linear_data(skb
,
733 (unsigned char *)pDB
->vaddr
, frmlen
);
734 skb_put(skb
, frmlen
);
735 skb
->protocol
= eth_type_trans(skb
, dev
);
736 netif_rx(skb
); /* pass the packet to upper layers */
738 if (au1000_debug
> 4) {
739 pr_err("rx_error(s):");
740 if (status
& RX_MISSED_FRAME
)
742 if (status
& RX_WDOG_TIMER
)
744 if (status
& RX_RUNT
)
746 if (status
& RX_OVERLEN
)
748 if (status
& RX_COLL
)
750 if (status
& RX_MII_ERROR
)
751 pr_cont(" mii error");
752 if (status
& RX_CRC_ERROR
)
753 pr_cont(" crc error");
754 if (status
& RX_LEN_ERROR
)
755 pr_cont(" len error");
756 if (status
& RX_U_CNTRL_FRAME
)
757 pr_cont(" u control frame");
761 prxd
->buff_stat
= (u32
)(pDB
->dma_addr
| RX_DMA_ENABLE
);
762 aup
->rx_head
= (aup
->rx_head
+ 1) & (NUM_RX_DMA
- 1);
765 /* next descriptor */
766 prxd
= aup
->rx_dma_ring
[aup
->rx_head
];
767 buff_stat
= prxd
->buff_stat
;
772 static void au1000_update_tx_stats(struct net_device
*dev
, u32 status
)
774 struct au1000_private
*aup
= netdev_priv(dev
);
775 struct net_device_stats
*ps
= &dev
->stats
;
777 if (status
& TX_FRAME_ABORTED
) {
778 if (!aup
->phy_dev
|| (DUPLEX_FULL
== aup
->phy_dev
->duplex
)) {
779 if (status
& (TX_JAB_TIMEOUT
| TX_UNDERRUN
)) {
780 /* any other tx errors are only valid
781 * in half duplex mode
784 ps
->tx_aborted_errors
++;
788 ps
->tx_aborted_errors
++;
789 if (status
& (TX_NO_CARRIER
| TX_LOSS_CARRIER
))
790 ps
->tx_carrier_errors
++;
796 * Called from the interrupt service routine to acknowledge
797 * the TX DONE bits. This is a must if the irq is setup as
800 static void au1000_tx_ack(struct net_device
*dev
)
802 struct au1000_private
*aup
= netdev_priv(dev
);
805 ptxd
= aup
->tx_dma_ring
[aup
->tx_tail
];
807 while (ptxd
->buff_stat
& TX_T_DONE
) {
808 au1000_update_tx_stats(dev
, ptxd
->status
);
809 ptxd
->buff_stat
&= ~TX_T_DONE
;
813 aup
->tx_tail
= (aup
->tx_tail
+ 1) & (NUM_TX_DMA
- 1);
814 ptxd
= aup
->tx_dma_ring
[aup
->tx_tail
];
818 netif_wake_queue(dev
);
824 * Au1000 interrupt service routine.
826 static irqreturn_t
au1000_interrupt(int irq
, void *dev_id
)
828 struct net_device
*dev
= dev_id
;
830 /* Handle RX interrupts first to minimize chance of overrun */
834 return IRQ_RETVAL(1);
837 static int au1000_open(struct net_device
*dev
)
840 struct au1000_private
*aup
= netdev_priv(dev
);
842 netif_dbg(aup
, drv
, dev
, "open: dev=%p\n", dev
);
844 retval
= request_irq(dev
->irq
, au1000_interrupt
, 0,
847 netdev_err(dev
, "unable to get IRQ %d\n", dev
->irq
);
851 retval
= au1000_init(dev
);
853 netdev_err(dev
, "error in au1000_init\n");
854 free_irq(dev
->irq
, dev
);
859 /* cause the PHY state machine to schedule a link state check */
860 aup
->phy_dev
->state
= PHY_CHANGELINK
;
861 phy_start(aup
->phy_dev
);
864 netif_start_queue(dev
);
866 netif_dbg(aup
, drv
, dev
, "open: Initialization done.\n");
871 static int au1000_close(struct net_device
*dev
)
874 struct au1000_private
*const aup
= netdev_priv(dev
);
876 netif_dbg(aup
, drv
, dev
, "close: dev=%p\n", dev
);
879 phy_stop(aup
->phy_dev
);
881 spin_lock_irqsave(&aup
->lock
, flags
);
883 au1000_reset_mac_unlocked(dev
);
885 /* stop the device */
886 netif_stop_queue(dev
);
888 /* disable the interrupt */
889 free_irq(dev
->irq
, dev
);
890 spin_unlock_irqrestore(&aup
->lock
, flags
);
896 * Au1000 transmit routine.
898 static netdev_tx_t
au1000_tx(struct sk_buff
*skb
, struct net_device
*dev
)
900 struct au1000_private
*aup
= netdev_priv(dev
);
901 struct net_device_stats
*ps
= &dev
->stats
;
907 netif_dbg(aup
, tx_queued
, dev
, "tx: aup %x len=%d, data=%p, head %d\n",
908 (unsigned)aup
, skb
->len
,
909 skb
->data
, aup
->tx_head
);
911 ptxd
= aup
->tx_dma_ring
[aup
->tx_head
];
912 buff_stat
= ptxd
->buff_stat
;
913 if (buff_stat
& TX_DMA_ENABLE
) {
914 /* We've wrapped around and the transmitter is still busy */
915 netif_stop_queue(dev
);
917 return NETDEV_TX_BUSY
;
918 } else if (buff_stat
& TX_T_DONE
) {
919 au1000_update_tx_stats(dev
, ptxd
->status
);
925 netif_wake_queue(dev
);
928 pDB
= aup
->tx_db_inuse
[aup
->tx_head
];
929 skb_copy_from_linear_data(skb
, (void *)pDB
->vaddr
, skb
->len
);
930 if (skb
->len
< ETH_ZLEN
) {
931 for (i
= skb
->len
; i
< ETH_ZLEN
; i
++)
932 ((char *)pDB
->vaddr
)[i
] = 0;
934 ptxd
->len
= ETH_ZLEN
;
936 ptxd
->len
= skb
->len
;
939 ps
->tx_bytes
+= ptxd
->len
;
941 ptxd
->buff_stat
= pDB
->dma_addr
| TX_DMA_ENABLE
;
944 aup
->tx_head
= (aup
->tx_head
+ 1) & (NUM_TX_DMA
- 1);
949 * The Tx ring has been full longer than the watchdog timeout
950 * value. The transmitter must be hung?
952 static void au1000_tx_timeout(struct net_device
*dev
)
954 netdev_err(dev
, "au1000_tx_timeout: dev=%p\n", dev
);
955 au1000_reset_mac(dev
);
957 dev
->trans_start
= jiffies
; /* prevent tx timeout */
958 netif_wake_queue(dev
);
961 static void au1000_multicast_list(struct net_device
*dev
)
963 struct au1000_private
*aup
= netdev_priv(dev
);
966 netif_dbg(aup
, drv
, dev
, "%s: flags=%x\n", __func__
, dev
->flags
);
967 reg
= readl(&aup
->mac
->control
);
968 if (dev
->flags
& IFF_PROMISC
) { /* Set promiscuous. */
969 reg
|= MAC_PROMISCUOUS
;
970 } else if ((dev
->flags
& IFF_ALLMULTI
) ||
971 netdev_mc_count(dev
) > MULTICAST_FILTER_LIMIT
) {
972 reg
|= MAC_PASS_ALL_MULTI
;
973 reg
&= ~MAC_PROMISCUOUS
;
974 netdev_info(dev
, "Pass all multicast\n");
976 struct netdev_hw_addr
*ha
;
977 u32 mc_filter
[2]; /* Multicast hash filter */
979 mc_filter
[1] = mc_filter
[0] = 0;
980 netdev_for_each_mc_addr(ha
, dev
)
981 set_bit(ether_crc(ETH_ALEN
, ha
->addr
)>>26,
983 writel(mc_filter
[1], &aup
->mac
->multi_hash_high
);
984 writel(mc_filter
[0], &aup
->mac
->multi_hash_low
);
985 reg
&= ~MAC_PROMISCUOUS
;
986 reg
|= MAC_HASH_MODE
;
988 writel(reg
, &aup
->mac
->control
);
991 static int au1000_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
993 struct au1000_private
*aup
= netdev_priv(dev
);
995 if (!netif_running(dev
))
999 return -EINVAL
; /* PHY not controllable */
1001 return phy_mii_ioctl(aup
->phy_dev
, rq
, cmd
);
1004 static const struct net_device_ops au1000_netdev_ops
= {
1005 .ndo_open
= au1000_open
,
1006 .ndo_stop
= au1000_close
,
1007 .ndo_start_xmit
= au1000_tx
,
1008 .ndo_set_rx_mode
= au1000_multicast_list
,
1009 .ndo_do_ioctl
= au1000_ioctl
,
1010 .ndo_tx_timeout
= au1000_tx_timeout
,
1011 .ndo_set_mac_address
= eth_mac_addr
,
1012 .ndo_validate_addr
= eth_validate_addr
,
1013 .ndo_change_mtu
= eth_change_mtu
,
1016 static int au1000_probe(struct platform_device
*pdev
)
1018 static unsigned version_printed
;
1019 struct au1000_private
*aup
= NULL
;
1020 struct au1000_eth_platform_data
*pd
;
1021 struct net_device
*dev
= NULL
;
1022 struct db_dest
*pDB
, *pDBfree
;
1023 int irq
, i
, err
= 0;
1024 struct resource
*base
, *macen
, *macdma
;
1026 base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1028 dev_err(&pdev
->dev
, "failed to retrieve base register\n");
1033 macen
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1035 dev_err(&pdev
->dev
, "failed to retrieve MAC Enable register\n");
1040 irq
= platform_get_irq(pdev
, 0);
1042 dev_err(&pdev
->dev
, "failed to retrieve IRQ\n");
1047 macdma
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1049 dev_err(&pdev
->dev
, "failed to retrieve MACDMA registers\n");
1054 if (!request_mem_region(base
->start
, resource_size(base
),
1056 dev_err(&pdev
->dev
, "failed to request memory region for base registers\n");
1061 if (!request_mem_region(macen
->start
, resource_size(macen
),
1063 dev_err(&pdev
->dev
, "failed to request memory region for MAC enable register\n");
1068 if (!request_mem_region(macdma
->start
, resource_size(macdma
),
1070 dev_err(&pdev
->dev
, "failed to request MACDMA memory region\n");
1075 dev
= alloc_etherdev(sizeof(struct au1000_private
));
1081 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1082 platform_set_drvdata(pdev
, dev
);
1083 aup
= netdev_priv(dev
);
1085 spin_lock_init(&aup
->lock
);
1086 aup
->msg_enable
= (au1000_debug
< 4 ?
1087 AU1000_DEF_MSG_ENABLE
: au1000_debug
);
1089 /* Allocate the data buffers
1090 * Snooping works fine with eth on all au1xxx
1092 aup
->vaddr
= (u32
)dma_alloc_noncoherent(NULL
, MAX_BUF_SIZE
*
1093 (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1096 dev_err(&pdev
->dev
, "failed to allocate data buffers\n");
1101 /* aup->mac is the base address of the MAC's registers */
1102 aup
->mac
= (struct mac_reg
*)
1103 ioremap_nocache(base
->start
, resource_size(base
));
1105 dev_err(&pdev
->dev
, "failed to ioremap MAC registers\n");
1110 /* Setup some variables for quick register address access */
1111 aup
->enable
= (u32
*)ioremap_nocache(macen
->start
,
1112 resource_size(macen
));
1114 dev_err(&pdev
->dev
, "failed to ioremap MAC enable register\n");
1118 aup
->mac_id
= pdev
->id
;
1120 aup
->macdma
= ioremap_nocache(macdma
->start
, resource_size(macdma
));
1122 dev_err(&pdev
->dev
, "failed to ioremap MACDMA registers\n");
1127 au1000_setup_hw_rings(aup
, aup
->macdma
);
1129 writel(0, aup
->enable
);
1130 aup
->mac_enabled
= 0;
1132 pd
= dev_get_platdata(&pdev
->dev
);
1134 dev_info(&pdev
->dev
, "no platform_data passed,"
1135 " PHY search on MAC0\n");
1136 aup
->phy1_search_mac0
= 1;
1138 if (is_valid_ether_addr(pd
->mac
)) {
1139 memcpy(dev
->dev_addr
, pd
->mac
, ETH_ALEN
);
1141 /* Set a random MAC since no valid provided by platform_data. */
1142 eth_hw_addr_random(dev
);
1145 aup
->phy_static_config
= pd
->phy_static_config
;
1146 aup
->phy_search_highest_addr
= pd
->phy_search_highest_addr
;
1147 aup
->phy1_search_mac0
= pd
->phy1_search_mac0
;
1148 aup
->phy_addr
= pd
->phy_addr
;
1149 aup
->phy_busid
= pd
->phy_busid
;
1150 aup
->phy_irq
= pd
->phy_irq
;
1153 if (aup
->phy_busid
&& aup
->phy_busid
> 0) {
1154 dev_err(&pdev
->dev
, "MAC0-associated PHY attached 2nd MACs MII bus not supported yet\n");
1156 goto err_mdiobus_alloc
;
1159 aup
->mii_bus
= mdiobus_alloc();
1160 if (aup
->mii_bus
== NULL
) {
1161 dev_err(&pdev
->dev
, "failed to allocate mdiobus structure\n");
1163 goto err_mdiobus_alloc
;
1166 aup
->mii_bus
->priv
= dev
;
1167 aup
->mii_bus
->read
= au1000_mdiobus_read
;
1168 aup
->mii_bus
->write
= au1000_mdiobus_write
;
1169 aup
->mii_bus
->reset
= au1000_mdiobus_reset
;
1170 aup
->mii_bus
->name
= "au1000_eth_mii";
1171 snprintf(aup
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%s-%x",
1172 pdev
->name
, aup
->mac_id
);
1173 aup
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
1174 if (aup
->mii_bus
->irq
== NULL
) {
1179 for (i
= 0; i
< PHY_MAX_ADDR
; ++i
)
1180 aup
->mii_bus
->irq
[i
] = PHY_POLL
;
1181 /* if known, set corresponding PHY IRQs */
1182 if (aup
->phy_static_config
)
1183 if (aup
->phy_irq
&& aup
->phy_busid
== aup
->mac_id
)
1184 aup
->mii_bus
->irq
[aup
->phy_addr
] = aup
->phy_irq
;
1186 err
= mdiobus_register(aup
->mii_bus
);
1188 dev_err(&pdev
->dev
, "failed to register MDIO bus\n");
1189 goto err_mdiobus_reg
;
1192 err
= au1000_mii_probe(dev
);
1197 /* setup the data buffer descriptors and attach a buffer to each one */
1199 for (i
= 0; i
< (NUM_TX_BUFFS
+NUM_RX_BUFFS
); i
++) {
1200 pDB
->pnext
= pDBfree
;
1202 pDB
->vaddr
= (u32
*)((unsigned)aup
->vaddr
+ MAX_BUF_SIZE
*i
);
1203 pDB
->dma_addr
= (dma_addr_t
)virt_to_bus(pDB
->vaddr
);
1206 aup
->pDBfree
= pDBfree
;
1209 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
1210 pDB
= au1000_GetFreeDB(aup
);
1214 aup
->rx_dma_ring
[i
]->buff_stat
= (unsigned)pDB
->dma_addr
;
1215 aup
->rx_db_inuse
[i
] = pDB
;
1219 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
1220 pDB
= au1000_GetFreeDB(aup
);
1224 aup
->tx_dma_ring
[i
]->buff_stat
= (unsigned)pDB
->dma_addr
;
1225 aup
->tx_dma_ring
[i
]->len
= 0;
1226 aup
->tx_db_inuse
[i
] = pDB
;
1229 dev
->base_addr
= base
->start
;
1231 dev
->netdev_ops
= &au1000_netdev_ops
;
1232 SET_ETHTOOL_OPS(dev
, &au1000_ethtool_ops
);
1233 dev
->watchdog_timeo
= ETH_TX_TIMEOUT
;
1236 * The boot code uses the ethernet controller, so reset it to start
1237 * fresh. au1000_init() expects that the device is in reset state.
1239 au1000_reset_mac(dev
);
1241 err
= register_netdev(dev
);
1243 netdev_err(dev
, "Cannot register net device, aborting.\n");
1247 netdev_info(dev
, "Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1248 (unsigned long)base
->start
, irq
);
1249 if (version_printed
++ == 0)
1250 pr_info("%s version %s %s\n",
1251 DRV_NAME
, DRV_VERSION
, DRV_AUTHOR
);
1256 if (aup
->mii_bus
!= NULL
)
1257 mdiobus_unregister(aup
->mii_bus
);
1259 /* here we should have a valid dev plus aup-> register addresses
1260 * so we can reset the mac properly.
1262 au1000_reset_mac(dev
);
1264 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
1265 if (aup
->rx_db_inuse
[i
])
1266 au1000_ReleaseDB(aup
, aup
->rx_db_inuse
[i
]);
1268 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
1269 if (aup
->tx_db_inuse
[i
])
1270 au1000_ReleaseDB(aup
, aup
->tx_db_inuse
[i
]);
1273 mdiobus_free(aup
->mii_bus
);
1275 iounmap(aup
->macdma
);
1277 iounmap(aup
->enable
);
1281 dma_free_noncoherent(NULL
, MAX_BUF_SIZE
* (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1282 (void *)aup
->vaddr
, aup
->dma_addr
);
1286 release_mem_region(macdma
->start
, resource_size(macdma
));
1288 release_mem_region(macen
->start
, resource_size(macen
));
1290 release_mem_region(base
->start
, resource_size(base
));
1295 static int au1000_remove(struct platform_device
*pdev
)
1297 struct net_device
*dev
= platform_get_drvdata(pdev
);
1298 struct au1000_private
*aup
= netdev_priv(dev
);
1300 struct resource
*base
, *macen
;
1302 unregister_netdev(dev
);
1303 mdiobus_unregister(aup
->mii_bus
);
1304 mdiobus_free(aup
->mii_bus
);
1306 for (i
= 0; i
< NUM_RX_DMA
; i
++)
1307 if (aup
->rx_db_inuse
[i
])
1308 au1000_ReleaseDB(aup
, aup
->rx_db_inuse
[i
]);
1310 for (i
= 0; i
< NUM_TX_DMA
; i
++)
1311 if (aup
->tx_db_inuse
[i
])
1312 au1000_ReleaseDB(aup
, aup
->tx_db_inuse
[i
]);
1314 dma_free_noncoherent(NULL
, MAX_BUF_SIZE
*
1315 (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1316 (void *)aup
->vaddr
, aup
->dma_addr
);
1318 iounmap(aup
->macdma
);
1320 iounmap(aup
->enable
);
1322 base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
1323 release_mem_region(base
->start
, resource_size(base
));
1325 base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1326 release_mem_region(base
->start
, resource_size(base
));
1328 macen
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1329 release_mem_region(macen
->start
, resource_size(macen
));
1336 static struct platform_driver au1000_eth_driver
= {
1337 .probe
= au1000_probe
,
1338 .remove
= au1000_remove
,
1340 .name
= "au1000-eth",
1341 .owner
= THIS_MODULE
,
1345 module_platform_driver(au1000_eth_driver
);
1347 MODULE_ALIAS("platform:au1000-eth");