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, write to the Free Software Foundation, Inc.,
31 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
33 * ########################################################################
37 #include <linux/capability.h>
38 #include <linux/dma-mapping.h>
39 #include <linux/module.h>
40 #include <linux/kernel.h>
41 #include <linux/string.h>
42 #include <linux/timer.h>
43 #include <linux/errno.h>
45 #include <linux/ioport.h>
46 #include <linux/bitops.h>
47 #include <linux/slab.h>
48 #include <linux/interrupt.h>
49 #include <linux/init.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>
61 #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 DRV_NAME "au1000_eth"
79 #define DRV_VERSION "1.6"
80 #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
81 #define DRV_DESC "Au1xxx on-chip Ethernet driver"
83 MODULE_AUTHOR(DRV_AUTHOR
);
84 MODULE_DESCRIPTION(DRV_DESC
);
85 MODULE_LICENSE("GPL");
86 MODULE_VERSION(DRV_VERSION
);
91 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
92 * There are four receive and four transmit descriptors. These
93 * descriptors are not in memory; rather, they are just a set of
96 * Since the Au1000 has a coherent data cache, the receive and
97 * transmit buffers are allocated from the KSEG0 segment. The
98 * hardware registers, however, are still mapped at KSEG1 to
99 * make sure there's no out-of-order writes, and that all writes
100 * complete immediately.
103 /* These addresses are only used if yamon doesn't tell us what
104 * the mac address is, and the mac address is not passed on the
107 static unsigned char au1000_mac_addr
[6] __devinitdata
= {
108 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
111 struct au1000_private
*au_macs
[NUM_ETH_INTERFACES
];
114 * board-specific configurations
116 * PHY detection algorithm
118 * If phy_static_config is undefined, the PHY setup is
121 * mii_probe() first searches the current MAC's MII bus for a PHY,
122 * selecting the first (or last, if phy_search_highest_addr is
123 * defined) PHY address not already claimed by another netdev.
125 * If nothing was found that way when searching for the 2nd ethernet
126 * controller's PHY and phy1_search_mac0 is defined, then
127 * the first MII bus is searched as well for an unclaimed PHY; this is
128 * needed in case of a dual-PHY accessible only through the MAC0's MII
131 * Finally, if no PHY is found, then the corresponding ethernet
132 * controller is not registered to the network subsystem.
135 /* autodetection defaults: phy1_search_mac0 */
139 * most boards PHY setup should be detectable properly with the
140 * autodetection algorithm in mii_probe(), but in some cases (e.g. if
141 * you have a switch attached, or want to use the PHY's interrupt
142 * notification capabilities) you can provide a static PHY
145 * IRQs may only be set, if a PHY address was configured
146 * If a PHY address is given, also a bus id is required to be set
148 * ps: make sure the used irqs are configured properly in the board
152 static void au1000_enable_mac(struct net_device
*dev
, int force_reset
)
155 struct au1000_private
*aup
= netdev_priv(dev
);
157 spin_lock_irqsave(&aup
->lock
, flags
);
159 if(force_reset
|| (!aup
->mac_enabled
)) {
160 *aup
->enable
= MAC_EN_CLOCK_ENABLE
;
162 *aup
->enable
= (MAC_EN_RESET0
| MAC_EN_RESET1
| MAC_EN_RESET2
163 | MAC_EN_CLOCK_ENABLE
);
166 aup
->mac_enabled
= 1;
169 spin_unlock_irqrestore(&aup
->lock
, flags
);
175 static int au1000_mdio_read(struct net_device
*dev
, int phy_addr
, int reg
)
177 struct au1000_private
*aup
= netdev_priv(dev
);
178 volatile u32
*const mii_control_reg
= &aup
->mac
->mii_control
;
179 volatile u32
*const mii_data_reg
= &aup
->mac
->mii_data
;
183 while (*mii_control_reg
& MAC_MII_BUSY
) {
185 if (--timedout
== 0) {
186 printk(KERN_ERR
"%s: read_MII busy timeout!!\n",
192 mii_control
= MAC_SET_MII_SELECT_REG(reg
) |
193 MAC_SET_MII_SELECT_PHY(phy_addr
) | MAC_MII_READ
;
195 *mii_control_reg
= mii_control
;
198 while (*mii_control_reg
& MAC_MII_BUSY
) {
200 if (--timedout
== 0) {
201 printk(KERN_ERR
"%s: mdio_read busy timeout!!\n",
206 return (int)*mii_data_reg
;
209 static void au1000_mdio_write(struct net_device
*dev
, int phy_addr
,
212 struct au1000_private
*aup
= netdev_priv(dev
);
213 volatile u32
*const mii_control_reg
= &aup
->mac
->mii_control
;
214 volatile u32
*const mii_data_reg
= &aup
->mac
->mii_data
;
218 while (*mii_control_reg
& MAC_MII_BUSY
) {
220 if (--timedout
== 0) {
221 printk(KERN_ERR
"%s: mdio_write busy timeout!!\n",
227 mii_control
= MAC_SET_MII_SELECT_REG(reg
) |
228 MAC_SET_MII_SELECT_PHY(phy_addr
) | MAC_MII_WRITE
;
230 *mii_data_reg
= value
;
231 *mii_control_reg
= mii_control
;
234 static int au1000_mdiobus_read(struct mii_bus
*bus
, int phy_addr
, int regnum
)
236 /* WARNING: bus->phy_map[phy_addr].attached_dev == dev does
237 * _NOT_ hold (e.g. when PHY is accessed through other MAC's MII bus) */
238 struct net_device
*const dev
= bus
->priv
;
240 au1000_enable_mac(dev
, 0); /* make sure the MAC associated with this
241 * mii_bus is enabled */
242 return au1000_mdio_read(dev
, phy_addr
, regnum
);
245 static int au1000_mdiobus_write(struct mii_bus
*bus
, int phy_addr
, int regnum
,
248 struct net_device
*const dev
= bus
->priv
;
250 au1000_enable_mac(dev
, 0); /* make sure the MAC associated with this
251 * mii_bus is enabled */
252 au1000_mdio_write(dev
, phy_addr
, regnum
, value
);
256 static int au1000_mdiobus_reset(struct mii_bus
*bus
)
258 struct net_device
*const dev
= bus
->priv
;
260 au1000_enable_mac(dev
, 0); /* make sure the MAC associated with this
261 * mii_bus is enabled */
265 static void au1000_hard_stop(struct net_device
*dev
)
267 struct au1000_private
*aup
= netdev_priv(dev
);
269 if (au1000_debug
> 4)
270 printk(KERN_INFO
"%s: hard stop\n", dev
->name
);
272 aup
->mac
->control
&= ~(MAC_RX_ENABLE
| MAC_TX_ENABLE
);
276 static void au1000_enable_rx_tx(struct net_device
*dev
)
278 struct au1000_private
*aup
= netdev_priv(dev
);
280 if (au1000_debug
> 4)
281 printk(KERN_INFO
"%s: enable_rx_tx\n", dev
->name
);
283 aup
->mac
->control
|= (MAC_RX_ENABLE
| MAC_TX_ENABLE
);
288 au1000_adjust_link(struct net_device
*dev
)
290 struct au1000_private
*aup
= netdev_priv(dev
);
291 struct phy_device
*phydev
= aup
->phy_dev
;
294 int status_change
= 0;
296 BUG_ON(!aup
->phy_dev
);
298 spin_lock_irqsave(&aup
->lock
, flags
);
300 if (phydev
->link
&& (aup
->old_speed
!= phydev
->speed
)) {
303 switch(phydev
->speed
) {
309 "%s: Speed (%d) is not 10/100 ???\n",
310 dev
->name
, phydev
->speed
);
314 aup
->old_speed
= phydev
->speed
;
319 if (phydev
->link
&& (aup
->old_duplex
!= phydev
->duplex
)) {
320 // duplex mode changed
322 /* switching duplex mode requires to disable rx and tx! */
323 au1000_hard_stop(dev
);
325 if (DUPLEX_FULL
== phydev
->duplex
)
326 aup
->mac
->control
= ((aup
->mac
->control
328 & ~MAC_DISABLE_RX_OWN
);
330 aup
->mac
->control
= ((aup
->mac
->control
332 | MAC_DISABLE_RX_OWN
);
335 au1000_enable_rx_tx(dev
);
336 aup
->old_duplex
= phydev
->duplex
;
341 if(phydev
->link
!= aup
->old_link
) {
342 // link state changed
347 aup
->old_duplex
= -1;
350 aup
->old_link
= phydev
->link
;
354 spin_unlock_irqrestore(&aup
->lock
, flags
);
358 printk(KERN_INFO
"%s: link up (%d/%s)\n",
359 dev
->name
, phydev
->speed
,
360 DUPLEX_FULL
== phydev
->duplex
? "Full" : "Half");
362 printk(KERN_INFO
"%s: link down\n", dev
->name
);
366 static int au1000_mii_probe (struct net_device
*dev
)
368 struct au1000_private
*const aup
= netdev_priv(dev
);
369 struct phy_device
*phydev
= NULL
;
371 if (aup
->phy_static_config
) {
372 BUG_ON(aup
->mac_id
< 0 || aup
->mac_id
> 1);
375 phydev
= aup
->mii_bus
->phy_map
[aup
->phy_addr
];
377 printk (KERN_INFO DRV_NAME
":%s: using PHY-less setup\n",
383 /* find the first (lowest address) PHY on the current MAC's MII bus */
384 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++)
385 if (aup
->mii_bus
->phy_map
[phy_addr
]) {
386 phydev
= aup
->mii_bus
->phy_map
[phy_addr
];
387 if (!aup
->phy_search_highest_addr
)
388 break; /* break out with first one found */
391 if (aup
->phy1_search_mac0
) {
392 /* try harder to find a PHY */
393 if (!phydev
&& (aup
->mac_id
== 1)) {
394 /* no PHY found, maybe we have a dual PHY? */
395 printk (KERN_INFO DRV_NAME
": no PHY found on MAC1, "
396 "let's see if it's attached to MAC0...\n");
398 /* find the first (lowest address) non-attached PHY on
399 * the MAC0 MII bus */
400 for (phy_addr
= 0; phy_addr
< PHY_MAX_ADDR
; phy_addr
++) {
401 struct phy_device
*const tmp_phydev
=
402 aup
->mii_bus
->phy_map
[phy_addr
];
404 if (aup
->mac_id
== 1)
408 continue; /* no PHY here... */
410 if (tmp_phydev
->attached_dev
)
411 continue; /* already claimed by MAC0 */
414 break; /* found it */
421 printk (KERN_ERR DRV_NAME
":%s: no PHY found\n", dev
->name
);
425 /* now we are supposed to have a proper phydev, to attach to... */
426 BUG_ON(phydev
->attached_dev
);
428 phydev
= phy_connect(dev
, dev_name(&phydev
->dev
), &au1000_adjust_link
,
429 0, PHY_INTERFACE_MODE_MII
);
431 if (IS_ERR(phydev
)) {
432 printk(KERN_ERR
"%s: Could not attach to PHY\n", dev
->name
);
433 return PTR_ERR(phydev
);
436 /* mask with MAC supported features */
437 phydev
->supported
&= (SUPPORTED_10baseT_Half
438 | SUPPORTED_10baseT_Full
439 | SUPPORTED_100baseT_Half
440 | SUPPORTED_100baseT_Full
442 /* | SUPPORTED_Pause | SUPPORTED_Asym_Pause */
446 phydev
->advertising
= phydev
->supported
;
450 aup
->old_duplex
= -1;
451 aup
->phy_dev
= phydev
;
453 printk(KERN_INFO
"%s: attached PHY driver [%s] "
454 "(mii_bus:phy_addr=%s, irq=%d)\n", dev
->name
,
455 phydev
->drv
->name
, dev_name(&phydev
->dev
), phydev
->irq
);
462 * Buffer allocation/deallocation routines. The buffer descriptor returned
463 * has the virtual and dma address of a buffer suitable for
464 * both, receive and transmit operations.
466 static db_dest_t
*au1000_GetFreeDB(struct au1000_private
*aup
)
472 aup
->pDBfree
= pDB
->pnext
;
477 void au1000_ReleaseDB(struct au1000_private
*aup
, db_dest_t
*pDB
)
479 db_dest_t
*pDBfree
= aup
->pDBfree
;
481 pDBfree
->pnext
= pDB
;
485 static void au1000_reset_mac_unlocked(struct net_device
*dev
)
487 struct au1000_private
*const aup
= netdev_priv(dev
);
490 au1000_hard_stop(dev
);
492 *aup
->enable
= MAC_EN_CLOCK_ENABLE
;
498 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
499 /* reset control bits */
500 aup
->rx_dma_ring
[i
]->buff_stat
&= ~0xf;
502 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
503 /* reset control bits */
504 aup
->tx_dma_ring
[i
]->buff_stat
&= ~0xf;
507 aup
->mac_enabled
= 0;
511 static void au1000_reset_mac(struct net_device
*dev
)
513 struct au1000_private
*const aup
= netdev_priv(dev
);
516 if (au1000_debug
> 4)
517 printk(KERN_INFO
"%s: reset mac, aup %x\n",
518 dev
->name
, (unsigned)aup
);
520 spin_lock_irqsave(&aup
->lock
, flags
);
522 au1000_reset_mac_unlocked (dev
);
524 spin_unlock_irqrestore(&aup
->lock
, flags
);
528 * Setup the receive and transmit "rings". These pointers are the addresses
529 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
530 * these are not descriptors sitting in memory.
533 au1000_setup_hw_rings(struct au1000_private
*aup
, u32 rx_base
, u32 tx_base
)
537 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
538 aup
->rx_dma_ring
[i
] =
539 (volatile rx_dma_t
*) (rx_base
+ sizeof(rx_dma_t
)*i
);
541 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
542 aup
->tx_dma_ring
[i
] =
543 (volatile tx_dma_t
*) (tx_base
+ sizeof(tx_dma_t
)*i
);
551 static int au1000_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
553 struct au1000_private
*aup
= netdev_priv(dev
);
556 return phy_ethtool_gset(aup
->phy_dev
, cmd
);
561 static int au1000_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
563 struct au1000_private
*aup
= netdev_priv(dev
);
565 if (!capable(CAP_NET_ADMIN
))
569 return phy_ethtool_sset(aup
->phy_dev
, cmd
);
575 au1000_get_drvinfo(struct net_device
*dev
, struct ethtool_drvinfo
*info
)
577 struct au1000_private
*aup
= netdev_priv(dev
);
579 strcpy(info
->driver
, DRV_NAME
);
580 strcpy(info
->version
, DRV_VERSION
);
581 info
->fw_version
[0] = '\0';
582 sprintf(info
->bus_info
, "%s %d", DRV_NAME
, aup
->mac_id
);
583 info
->regdump_len
= 0;
586 static const struct ethtool_ops au1000_ethtool_ops
= {
587 .get_settings
= au1000_get_settings
,
588 .set_settings
= au1000_set_settings
,
589 .get_drvinfo
= au1000_get_drvinfo
,
590 .get_link
= ethtool_op_get_link
,
595 * Initialize the interface.
597 * When the device powers up, the clocks are disabled and the
598 * mac is in reset state. When the interface is closed, we
599 * do the same -- reset the device and disable the clocks to
600 * conserve power. Thus, whenever au1000_init() is called,
601 * the device should already be in reset state.
603 static int au1000_init(struct net_device
*dev
)
605 struct au1000_private
*aup
= netdev_priv(dev
);
610 if (au1000_debug
> 4)
611 printk("%s: au1000_init\n", dev
->name
);
613 /* bring the device out of reset */
614 au1000_enable_mac(dev
, 1);
616 spin_lock_irqsave(&aup
->lock
, flags
);
618 aup
->mac
->control
= 0;
619 aup
->tx_head
= (aup
->tx_dma_ring
[0]->buff_stat
& 0xC) >> 2;
620 aup
->tx_tail
= aup
->tx_head
;
621 aup
->rx_head
= (aup
->rx_dma_ring
[0]->buff_stat
& 0xC) >> 2;
623 aup
->mac
->mac_addr_high
= dev
->dev_addr
[5]<<8 | dev
->dev_addr
[4];
624 aup
->mac
->mac_addr_low
= dev
->dev_addr
[3]<<24 | dev
->dev_addr
[2]<<16 |
625 dev
->dev_addr
[1]<<8 | dev
->dev_addr
[0];
627 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
628 aup
->rx_dma_ring
[i
]->buff_stat
|= RX_DMA_ENABLE
;
632 control
= MAC_RX_ENABLE
| MAC_TX_ENABLE
;
633 #ifndef CONFIG_CPU_LITTLE_ENDIAN
634 control
|= MAC_BIG_ENDIAN
;
637 if (aup
->phy_dev
->link
&& (DUPLEX_FULL
== aup
->phy_dev
->duplex
))
638 control
|= MAC_FULL_DUPLEX
;
640 control
|= MAC_DISABLE_RX_OWN
;
641 } else { /* PHY-less op, assume full-duplex */
642 control
|= MAC_FULL_DUPLEX
;
645 aup
->mac
->control
= control
;
646 aup
->mac
->vlan1_tag
= 0x8100; /* activate vlan support */
649 spin_unlock_irqrestore(&aup
->lock
, flags
);
653 static inline void au1000_update_rx_stats(struct net_device
*dev
, u32 status
)
655 struct net_device_stats
*ps
= &dev
->stats
;
658 if (status
& RX_MCAST_FRAME
)
661 if (status
& RX_ERROR
) {
663 if (status
& RX_MISSED_FRAME
)
664 ps
->rx_missed_errors
++;
665 if (status
& (RX_OVERLEN
| RX_RUNT
| RX_LEN_ERROR
))
666 ps
->rx_length_errors
++;
667 if (status
& RX_CRC_ERROR
)
669 if (status
& RX_COLL
)
673 ps
->rx_bytes
+= status
& RX_FRAME_LEN_MASK
;
678 * Au1000 receive routine.
680 static int au1000_rx(struct net_device
*dev
)
682 struct au1000_private
*aup
= netdev_priv(dev
);
684 volatile rx_dma_t
*prxd
;
685 u32 buff_stat
, status
;
689 if (au1000_debug
> 5)
690 printk("%s: au1000_rx head %d\n", dev
->name
, aup
->rx_head
);
692 prxd
= aup
->rx_dma_ring
[aup
->rx_head
];
693 buff_stat
= prxd
->buff_stat
;
694 while (buff_stat
& RX_T_DONE
) {
695 status
= prxd
->status
;
696 pDB
= aup
->rx_db_inuse
[aup
->rx_head
];
697 au1000_update_rx_stats(dev
, status
);
698 if (!(status
& RX_ERROR
)) {
701 frmlen
= (status
& RX_FRAME_LEN_MASK
);
702 frmlen
-= 4; /* Remove FCS */
703 skb
= dev_alloc_skb(frmlen
+ 2);
706 "%s: Memory squeeze, dropping packet.\n",
708 dev
->stats
.rx_dropped
++;
711 skb_reserve(skb
, 2); /* 16 byte IP header align */
712 skb_copy_to_linear_data(skb
,
713 (unsigned char *)pDB
->vaddr
, frmlen
);
714 skb_put(skb
, frmlen
);
715 skb
->protocol
= eth_type_trans(skb
, dev
);
716 netif_rx(skb
); /* pass the packet to upper layers */
719 if (au1000_debug
> 4) {
720 if (status
& RX_MISSED_FRAME
)
722 if (status
& RX_WDOG_TIMER
)
724 if (status
& RX_RUNT
)
726 if (status
& RX_OVERLEN
)
727 printk("rx overlen\n");
728 if (status
& RX_COLL
)
730 if (status
& RX_MII_ERROR
)
731 printk("rx mii error\n");
732 if (status
& RX_CRC_ERROR
)
733 printk("rx crc error\n");
734 if (status
& RX_LEN_ERROR
)
735 printk("rx len error\n");
736 if (status
& RX_U_CNTRL_FRAME
)
737 printk("rx u control frame\n");
740 prxd
->buff_stat
= (u32
)(pDB
->dma_addr
| RX_DMA_ENABLE
);
741 aup
->rx_head
= (aup
->rx_head
+ 1) & (NUM_RX_DMA
- 1);
744 /* next descriptor */
745 prxd
= aup
->rx_dma_ring
[aup
->rx_head
];
746 buff_stat
= prxd
->buff_stat
;
751 static void au1000_update_tx_stats(struct net_device
*dev
, u32 status
)
753 struct au1000_private
*aup
= netdev_priv(dev
);
754 struct net_device_stats
*ps
= &dev
->stats
;
756 if (status
& TX_FRAME_ABORTED
) {
757 if (!aup
->phy_dev
|| (DUPLEX_FULL
== aup
->phy_dev
->duplex
)) {
758 if (status
& (TX_JAB_TIMEOUT
| TX_UNDERRUN
)) {
759 /* any other tx errors are only valid
760 * in half duplex mode */
762 ps
->tx_aborted_errors
++;
767 ps
->tx_aborted_errors
++;
768 if (status
& (TX_NO_CARRIER
| TX_LOSS_CARRIER
))
769 ps
->tx_carrier_errors
++;
775 * Called from the interrupt service routine to acknowledge
776 * the TX DONE bits. This is a must if the irq is setup as
779 static void au1000_tx_ack(struct net_device
*dev
)
781 struct au1000_private
*aup
= netdev_priv(dev
);
782 volatile tx_dma_t
*ptxd
;
784 ptxd
= aup
->tx_dma_ring
[aup
->tx_tail
];
786 while (ptxd
->buff_stat
& TX_T_DONE
) {
787 au1000_update_tx_stats(dev
, ptxd
->status
);
788 ptxd
->buff_stat
&= ~TX_T_DONE
;
792 aup
->tx_tail
= (aup
->tx_tail
+ 1) & (NUM_TX_DMA
- 1);
793 ptxd
= aup
->tx_dma_ring
[aup
->tx_tail
];
797 netif_wake_queue(dev
);
803 * Au1000 interrupt service routine.
805 static irqreturn_t
au1000_interrupt(int irq
, void *dev_id
)
807 struct net_device
*dev
= dev_id
;
809 /* Handle RX interrupts first to minimize chance of overrun */
813 return IRQ_RETVAL(1);
816 static int au1000_open(struct net_device
*dev
)
819 struct au1000_private
*aup
= netdev_priv(dev
);
821 if (au1000_debug
> 4)
822 printk("%s: open: dev=%p\n", dev
->name
, dev
);
824 if ((retval
= request_irq(dev
->irq
, au1000_interrupt
, 0,
826 printk(KERN_ERR
"%s: unable to get IRQ %d\n",
827 dev
->name
, dev
->irq
);
831 if ((retval
= au1000_init(dev
))) {
832 printk(KERN_ERR
"%s: error in au1000_init\n", dev
->name
);
833 free_irq(dev
->irq
, dev
);
838 /* cause the PHY state machine to schedule a link state check */
839 aup
->phy_dev
->state
= PHY_CHANGELINK
;
840 phy_start(aup
->phy_dev
);
843 netif_start_queue(dev
);
845 if (au1000_debug
> 4)
846 printk("%s: open: Initialization done.\n", dev
->name
);
851 static int au1000_close(struct net_device
*dev
)
854 struct au1000_private
*const aup
= netdev_priv(dev
);
856 if (au1000_debug
> 4)
857 printk("%s: close: dev=%p\n", dev
->name
, dev
);
860 phy_stop(aup
->phy_dev
);
862 spin_lock_irqsave(&aup
->lock
, flags
);
864 au1000_reset_mac_unlocked (dev
);
866 /* stop the device */
867 netif_stop_queue(dev
);
869 /* disable the interrupt */
870 free_irq(dev
->irq
, dev
);
871 spin_unlock_irqrestore(&aup
->lock
, flags
);
877 * Au1000 transmit routine.
879 static netdev_tx_t
au1000_tx(struct sk_buff
*skb
, struct net_device
*dev
)
881 struct au1000_private
*aup
= netdev_priv(dev
);
882 struct net_device_stats
*ps
= &dev
->stats
;
883 volatile tx_dma_t
*ptxd
;
888 if (au1000_debug
> 5)
889 printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
890 dev
->name
, (unsigned)aup
, skb
->len
,
891 skb
->data
, aup
->tx_head
);
893 ptxd
= aup
->tx_dma_ring
[aup
->tx_head
];
894 buff_stat
= ptxd
->buff_stat
;
895 if (buff_stat
& TX_DMA_ENABLE
) {
896 /* We've wrapped around and the transmitter is still busy */
897 netif_stop_queue(dev
);
899 return NETDEV_TX_BUSY
;
901 else if (buff_stat
& TX_T_DONE
) {
902 au1000_update_tx_stats(dev
, ptxd
->status
);
908 netif_wake_queue(dev
);
911 pDB
= aup
->tx_db_inuse
[aup
->tx_head
];
912 skb_copy_from_linear_data(skb
, (void *)pDB
->vaddr
, skb
->len
);
913 if (skb
->len
< ETH_ZLEN
) {
914 for (i
=skb
->len
; i
<ETH_ZLEN
; i
++) {
915 ((char *)pDB
->vaddr
)[i
] = 0;
917 ptxd
->len
= ETH_ZLEN
;
920 ptxd
->len
= skb
->len
;
923 ps
->tx_bytes
+= ptxd
->len
;
925 ptxd
->buff_stat
= pDB
->dma_addr
| TX_DMA_ENABLE
;
928 aup
->tx_head
= (aup
->tx_head
+ 1) & (NUM_TX_DMA
- 1);
929 dev
->trans_start
= jiffies
;
934 * The Tx ring has been full longer than the watchdog timeout
935 * value. The transmitter must be hung?
937 static void au1000_tx_timeout(struct net_device
*dev
)
939 printk(KERN_ERR
"%s: au1000_tx_timeout: dev=%p\n", dev
->name
, dev
);
940 au1000_reset_mac(dev
);
942 dev
->trans_start
= jiffies
;
943 netif_wake_queue(dev
);
946 static void au1000_multicast_list(struct net_device
*dev
)
948 struct au1000_private
*aup
= netdev_priv(dev
);
950 if (au1000_debug
> 4)
951 printk("%s: au1000_multicast_list: flags=%x\n", dev
->name
, dev
->flags
);
953 if (dev
->flags
& IFF_PROMISC
) { /* Set promiscuous. */
954 aup
->mac
->control
|= MAC_PROMISCUOUS
;
955 } else if ((dev
->flags
& IFF_ALLMULTI
) ||
956 netdev_mc_count(dev
) > MULTICAST_FILTER_LIMIT
) {
957 aup
->mac
->control
|= MAC_PASS_ALL_MULTI
;
958 aup
->mac
->control
&= ~MAC_PROMISCUOUS
;
959 printk(KERN_INFO
"%s: Pass all multicast\n", dev
->name
);
961 struct netdev_hw_addr
*ha
;
962 u32 mc_filter
[2]; /* Multicast hash filter */
964 mc_filter
[1] = mc_filter
[0] = 0;
965 netdev_for_each_mc_addr(ha
, dev
)
966 set_bit(ether_crc(ETH_ALEN
, ha
->addr
)>>26,
968 aup
->mac
->multi_hash_high
= mc_filter
[1];
969 aup
->mac
->multi_hash_low
= mc_filter
[0];
970 aup
->mac
->control
&= ~MAC_PROMISCUOUS
;
971 aup
->mac
->control
|= MAC_HASH_MODE
;
975 static int au1000_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
977 struct au1000_private
*aup
= netdev_priv(dev
);
979 if (!netif_running(dev
)) return -EINVAL
;
981 if (!aup
->phy_dev
) return -EINVAL
; // PHY not controllable
983 return phy_mii_ioctl(aup
->phy_dev
, if_mii(rq
), cmd
);
986 static const struct net_device_ops au1000_netdev_ops
= {
987 .ndo_open
= au1000_open
,
988 .ndo_stop
= au1000_close
,
989 .ndo_start_xmit
= au1000_tx
,
990 .ndo_set_multicast_list
= au1000_multicast_list
,
991 .ndo_do_ioctl
= au1000_ioctl
,
992 .ndo_tx_timeout
= au1000_tx_timeout
,
993 .ndo_set_mac_address
= eth_mac_addr
,
994 .ndo_validate_addr
= eth_validate_addr
,
995 .ndo_change_mtu
= eth_change_mtu
,
998 static int __devinit
au1000_probe(struct platform_device
*pdev
)
1000 static unsigned version_printed
= 0;
1001 struct au1000_private
*aup
= NULL
;
1002 struct au1000_eth_platform_data
*pd
;
1003 struct net_device
*dev
= NULL
;
1004 db_dest_t
*pDB
, *pDBfree
;
1005 int irq
, i
, err
= 0;
1006 struct resource
*base
, *macen
;
1009 base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1011 printk(KERN_ERR DRV_NAME
": failed to retrieve base register\n");
1016 macen
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1018 printk(KERN_ERR DRV_NAME
": failed to retrieve MAC Enable register\n");
1023 irq
= platform_get_irq(pdev
, 0);
1025 printk(KERN_ERR DRV_NAME
": failed to retrieve IRQ\n");
1030 if (!request_mem_region(base
->start
, resource_size(base
), pdev
->name
)) {
1031 printk(KERN_ERR DRV_NAME
": failed to request memory region for base registers\n");
1036 if (!request_mem_region(macen
->start
, resource_size(macen
), pdev
->name
)) {
1037 printk(KERN_ERR DRV_NAME
": failed to request memory region for MAC enable register\n");
1042 dev
= alloc_etherdev(sizeof(struct au1000_private
));
1044 printk(KERN_ERR
"%s: alloc_etherdev failed\n", DRV_NAME
);
1049 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1050 platform_set_drvdata(pdev
, dev
);
1051 aup
= netdev_priv(dev
);
1053 spin_lock_init(&aup
->lock
);
1055 /* Allocate the data buffers */
1056 /* Snooping works fine with eth on all au1xxx */
1057 aup
->vaddr
= (u32
)dma_alloc_noncoherent(NULL
, MAX_BUF_SIZE
*
1058 (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1061 printk(KERN_ERR DRV_NAME
": failed to allocate data buffers\n");
1066 /* aup->mac is the base address of the MAC's registers */
1067 aup
->mac
= (volatile mac_reg_t
*)ioremap_nocache(base
->start
, resource_size(base
));
1069 printk(KERN_ERR DRV_NAME
": failed to ioremap MAC registers\n");
1074 /* Setup some variables for quick register address access */
1075 aup
->enable
= (volatile u32
*)ioremap_nocache(macen
->start
, resource_size(macen
));
1077 printk(KERN_ERR DRV_NAME
": failed to ioremap MAC enable register\n");
1081 aup
->mac_id
= pdev
->id
;
1083 if (pdev
->id
== 0) {
1084 if (prom_get_ethernet_addr(ethaddr
) == 0)
1085 memcpy(au1000_mac_addr
, ethaddr
, sizeof(au1000_mac_addr
));
1087 printk(KERN_INFO
"%s: No MAC address found\n",
1089 /* Use the hard coded MAC addresses */
1092 au1000_setup_hw_rings(aup
, MAC0_RX_DMA_ADDR
, MAC0_TX_DMA_ADDR
);
1093 } else if (pdev
->id
== 1)
1094 au1000_setup_hw_rings(aup
, MAC1_RX_DMA_ADDR
, MAC1_TX_DMA_ADDR
);
1097 * Assign to the Ethernet ports two consecutive MAC addresses
1098 * to match those that are printed on their stickers
1100 memcpy(dev
->dev_addr
, au1000_mac_addr
, sizeof(au1000_mac_addr
));
1101 dev
->dev_addr
[5] += pdev
->id
;
1104 aup
->mac_enabled
= 0;
1106 pd
= pdev
->dev
.platform_data
;
1108 printk(KERN_INFO DRV_NAME
": no platform_data passed, PHY search on MAC0\n");
1109 aup
->phy1_search_mac0
= 1;
1111 aup
->phy_static_config
= pd
->phy_static_config
;
1112 aup
->phy_search_highest_addr
= pd
->phy_search_highest_addr
;
1113 aup
->phy1_search_mac0
= pd
->phy1_search_mac0
;
1114 aup
->phy_addr
= pd
->phy_addr
;
1115 aup
->phy_busid
= pd
->phy_busid
;
1116 aup
->phy_irq
= pd
->phy_irq
;
1119 if (aup
->phy_busid
&& aup
->phy_busid
> 0) {
1120 printk(KERN_ERR DRV_NAME
": MAC0-associated PHY attached 2nd MACs MII"
1121 "bus not supported yet\n");
1123 goto err_mdiobus_alloc
;
1126 aup
->mii_bus
= mdiobus_alloc();
1127 if (aup
->mii_bus
== NULL
) {
1128 printk(KERN_ERR DRV_NAME
": failed to allocate mdiobus structure\n");
1130 goto err_mdiobus_alloc
;
1133 aup
->mii_bus
->priv
= dev
;
1134 aup
->mii_bus
->read
= au1000_mdiobus_read
;
1135 aup
->mii_bus
->write
= au1000_mdiobus_write
;
1136 aup
->mii_bus
->reset
= au1000_mdiobus_reset
;
1137 aup
->mii_bus
->name
= "au1000_eth_mii";
1138 snprintf(aup
->mii_bus
->id
, MII_BUS_ID_SIZE
, "%x", aup
->mac_id
);
1139 aup
->mii_bus
->irq
= kmalloc(sizeof(int)*PHY_MAX_ADDR
, GFP_KERNEL
);
1140 if (aup
->mii_bus
->irq
== NULL
)
1143 for(i
= 0; i
< PHY_MAX_ADDR
; ++i
)
1144 aup
->mii_bus
->irq
[i
] = PHY_POLL
;
1145 /* if known, set corresponding PHY IRQs */
1146 if (aup
->phy_static_config
)
1147 if (aup
->phy_irq
&& aup
->phy_busid
== aup
->mac_id
)
1148 aup
->mii_bus
->irq
[aup
->phy_addr
] = aup
->phy_irq
;
1150 err
= mdiobus_register(aup
->mii_bus
);
1152 printk(KERN_ERR DRV_NAME
" failed to register MDIO bus\n");
1153 goto err_mdiobus_reg
;
1156 if (au1000_mii_probe(dev
) != 0)
1160 /* setup the data buffer descriptors and attach a buffer to each one */
1162 for (i
= 0; i
< (NUM_TX_BUFFS
+NUM_RX_BUFFS
); i
++) {
1163 pDB
->pnext
= pDBfree
;
1165 pDB
->vaddr
= (u32
*)((unsigned)aup
->vaddr
+ MAX_BUF_SIZE
*i
);
1166 pDB
->dma_addr
= (dma_addr_t
)virt_to_bus(pDB
->vaddr
);
1169 aup
->pDBfree
= pDBfree
;
1171 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
1172 pDB
= au1000_GetFreeDB(aup
);
1176 aup
->rx_dma_ring
[i
]->buff_stat
= (unsigned)pDB
->dma_addr
;
1177 aup
->rx_db_inuse
[i
] = pDB
;
1179 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
1180 pDB
= au1000_GetFreeDB(aup
);
1184 aup
->tx_dma_ring
[i
]->buff_stat
= (unsigned)pDB
->dma_addr
;
1185 aup
->tx_dma_ring
[i
]->len
= 0;
1186 aup
->tx_db_inuse
[i
] = pDB
;
1189 dev
->base_addr
= base
->start
;
1191 dev
->netdev_ops
= &au1000_netdev_ops
;
1192 SET_ETHTOOL_OPS(dev
, &au1000_ethtool_ops
);
1193 dev
->watchdog_timeo
= ETH_TX_TIMEOUT
;
1196 * The boot code uses the ethernet controller, so reset it to start
1197 * fresh. au1000_init() expects that the device is in reset state.
1199 au1000_reset_mac(dev
);
1201 err
= register_netdev(dev
);
1203 printk(KERN_ERR DRV_NAME
"%s: Cannot register net device, aborting.\n",
1208 printk("%s: Au1xx0 Ethernet found at 0x%lx, irq %d\n",
1209 dev
->name
, (unsigned long)base
->start
, irq
);
1210 if (version_printed
++ == 0)
1211 printk("%s version %s %s\n", DRV_NAME
, DRV_VERSION
, DRV_AUTHOR
);
1216 if (aup
->mii_bus
!= NULL
)
1217 mdiobus_unregister(aup
->mii_bus
);
1219 /* here we should have a valid dev plus aup-> register addresses
1220 * so we can reset the mac properly.*/
1221 au1000_reset_mac(dev
);
1223 for (i
= 0; i
< NUM_RX_DMA
; i
++) {
1224 if (aup
->rx_db_inuse
[i
])
1225 au1000_ReleaseDB(aup
, aup
->rx_db_inuse
[i
]);
1227 for (i
= 0; i
< NUM_TX_DMA
; i
++) {
1228 if (aup
->tx_db_inuse
[i
])
1229 au1000_ReleaseDB(aup
, aup
->tx_db_inuse
[i
]);
1232 mdiobus_free(aup
->mii_bus
);
1234 iounmap(aup
->enable
);
1238 dma_free_noncoherent(NULL
, MAX_BUF_SIZE
* (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1239 (void *)aup
->vaddr
, aup
->dma_addr
);
1243 release_mem_region(macen
->start
, resource_size(macen
));
1245 release_mem_region(base
->start
, resource_size(base
));
1250 static int __devexit
au1000_remove(struct platform_device
*pdev
)
1252 struct net_device
*dev
= platform_get_drvdata(pdev
);
1253 struct au1000_private
*aup
= netdev_priv(dev
);
1255 struct resource
*base
, *macen
;
1257 platform_set_drvdata(pdev
, NULL
);
1259 unregister_netdev(dev
);
1260 mdiobus_unregister(aup
->mii_bus
);
1261 mdiobus_free(aup
->mii_bus
);
1263 for (i
= 0; i
< NUM_RX_DMA
; i
++)
1264 if (aup
->rx_db_inuse
[i
])
1265 au1000_ReleaseDB(aup
, aup
->rx_db_inuse
[i
]);
1267 for (i
= 0; i
< NUM_TX_DMA
; i
++)
1268 if (aup
->tx_db_inuse
[i
])
1269 au1000_ReleaseDB(aup
, aup
->tx_db_inuse
[i
]);
1271 dma_free_noncoherent(NULL
, MAX_BUF_SIZE
*
1272 (NUM_TX_BUFFS
+ NUM_RX_BUFFS
),
1273 (void *)aup
->vaddr
, aup
->dma_addr
);
1276 iounmap(aup
->enable
);
1278 base
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1279 release_mem_region(base
->start
, resource_size(base
));
1281 macen
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
1282 release_mem_region(macen
->start
, resource_size(macen
));
1289 static struct platform_driver au1000_eth_driver
= {
1290 .probe
= au1000_probe
,
1291 .remove
= __devexit_p(au1000_remove
),
1293 .name
= "au1000-eth",
1294 .owner
= THIS_MODULE
,
1297 MODULE_ALIAS("platform:au1000-eth");
1300 static int __init
au1000_init_module(void)
1302 return platform_driver_register(&au1000_eth_driver
);
1305 static void __exit
au1000_exit_module(void)
1307 platform_driver_unregister(&au1000_eth_driver
);
1310 module_init(au1000_init_module
);
1311 module_exit(au1000_exit_module
);