3 * Alchemy Au1x00 ethernet driver
5 * Copyright 2001,2002,2003 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 * Author: MontaVista Software, Inc.
13 * ppopov@mvista.com or source@mvista.com
15 * ########################################################################
17 * This program is free software; you can distribute it and/or modify it
18 * under the terms of the GNU General Public License (Version 2) as
19 * published by the Free Software Foundation.
21 * This program is distributed in the hope it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with this program; if not, write to the Free Software Foundation, Inc.,
28 * 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
30 * ########################################################################
35 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/sched.h>
38 #include <linux/string.h>
39 #include <linux/timer.h>
40 #include <linux/errno.h>
42 #include <linux/ioport.h>
43 #include <linux/bitops.h>
44 #include <linux/slab.h>
45 #include <linux/interrupt.h>
46 #include <linux/pci.h>
47 #include <linux/init.h>
48 #include <linux/netdevice.h>
49 #include <linux/etherdevice.h>
50 #include <linux/ethtool.h>
51 #include <linux/mii.h>
52 #include <linux/skbuff.h>
53 #include <linux/delay.h>
54 #include <asm/mipsregs.h>
57 #include <asm/processor.h>
59 #include <asm/mach-au1x00/au1000.h>
61 #include "au1000_eth.h"
63 #ifdef AU1000_ETH_DEBUG
64 static int au1000_debug = 5;
66 static int au1000_debug = 3;
69 #define DRV_NAME "au1000eth"
70 #define DRV_VERSION "1.5"
71 #define DRV_AUTHOR "Pete Popov <ppopov@embeddedalley.com>"
72 #define DRV_DESC "Au1xxx on-chip Ethernet driver"
74 MODULE_AUTHOR(DRV_AUTHOR);
75 MODULE_DESCRIPTION(DRV_DESC);
76 MODULE_LICENSE("GPL");
79 static void hard_stop(struct net_device *);
80 static void enable_rx_tx(struct net_device *dev);
81 static struct net_device * au1000_probe(u32 ioaddr, int irq, int port_num);
82 static int au1000_init(struct net_device *);
83 static int au1000_open(struct net_device *);
84 static int au1000_close(struct net_device *);
85 static int au1000_tx(struct sk_buff *, struct net_device *);
86 static int au1000_rx(struct net_device *);
87 static irqreturn_t au1000_interrupt(int, void *, struct pt_regs *);
88 static void au1000_tx_timeout(struct net_device *);
89 static int au1000_set_config(struct net_device *dev, struct ifmap *map);
90 static void set_rx_mode(struct net_device *);
91 static struct net_device_stats *au1000_get_stats(struct net_device *);
92 static inline void update_tx_stats(struct net_device *, u32, u32);
93 static inline void update_rx_stats(struct net_device *, u32);
94 static void au1000_timer(unsigned long);
95 static int au1000_ioctl(struct net_device *, struct ifreq *, int);
96 static int mdio_read(struct net_device *, int, int);
97 static void mdio_write(struct net_device *, int, int, u16);
98 static void dump_mii(struct net_device *dev, int phy_id);
101 extern void ack_rise_edge_irq(unsigned int);
102 extern int get_ethernet_addr(char *ethernet_addr);
103 extern void str2eaddr(unsigned char *ea, unsigned char *str);
104 extern char * __init prom_getcmdline(void);
107 * Theory of operation
109 * The Au1000 MACs use a simple rx and tx descriptor ring scheme.
110 * There are four receive and four transmit descriptors. These
111 * descriptors are not in memory; rather, they are just a set of
112 * hardware registers.
114 * Since the Au1000 has a coherent data cache, the receive and
115 * transmit buffers are allocated from the KSEG0 segment. The
116 * hardware registers, however, are still mapped at KSEG1 to
117 * make sure there's no out-of-order writes, and that all writes
118 * complete immediately.
121 /* These addresses are only used if yamon doesn't tell us what
122 * the mac address is, and the mac address is not passed on the
125 static unsigned char au1000_mac_addr[6] __devinitdata = {
126 0x00, 0x50, 0xc2, 0x0c, 0x30, 0x00
129 #define nibswap(x) ((((x) >> 4) & 0x0f) | (((x) << 4) & 0xf0))
130 #define RUN_AT(x) (jiffies + (x))
132 // For reading/writing 32-bit words from/to DMA memory
133 #define cpu_to_dma32 cpu_to_be32
134 #define dma32_to_cpu be32_to_cpu
136 struct au1000_private *au_macs[NUM_ETH_INTERFACES];
139 * All of the PHY code really should be detached from the MAC
143 /* Default advertise */
144 #define GENMII_DEFAULT_ADVERTISE \
145 ADVERTISED_10baseT_Half | ADVERTISED_10baseT_Full | \
146 ADVERTISED_100baseT_Half | ADVERTISED_100baseT_Full | \
149 #define GENMII_DEFAULT_FEATURES \
150 SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full | \
151 SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full | \
154 static char *phy_link[] =
156 "10Base2", "10BaseT",
158 "100BaseT", "100BaseTX", "100BaseFX"
161 int bcm_5201_init(struct net_device *dev, int phy_addr)
165 /* Stop auto-negotiation */
166 data = mdio_read(dev, phy_addr, MII_CONTROL);
167 mdio_write(dev, phy_addr, MII_CONTROL, data & ~MII_CNTL_AUTO);
169 /* Set advertisement to 10/100 and Half/Full duplex
170 * (full capabilities) */
171 data = mdio_read(dev, phy_addr, MII_ANADV);
172 data |= MII_NWAY_TX | MII_NWAY_TX_FDX | MII_NWAY_T_FDX | MII_NWAY_T;
173 mdio_write(dev, phy_addr, MII_ANADV, data);
175 /* Restart auto-negotiation */
176 data = mdio_read(dev, phy_addr, MII_CONTROL);
177 data |= MII_CNTL_RST_AUTO | MII_CNTL_AUTO;
178 mdio_write(dev, phy_addr, MII_CONTROL, data);
180 if (au1000_debug > 4)
181 dump_mii(dev, phy_addr);
185 int bcm_5201_reset(struct net_device *dev, int phy_addr)
187 s16 mii_control, timeout;
189 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
190 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
192 for (timeout = 100; timeout > 0; --timeout) {
193 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
194 if ((mii_control & MII_CNTL_RESET) == 0)
198 if (mii_control & MII_CNTL_RESET) {
199 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
206 bcm_5201_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
209 struct au1000_private *aup;
212 printk(KERN_ERR "bcm_5201_status error: NULL dev\n");
215 aup = (struct au1000_private *) dev->priv;
217 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
218 if (mii_data & MII_STAT_LINK) {
220 mii_data = mdio_read(dev, aup->phy_addr, MII_AUX_CNTRL);
221 if (mii_data & MII_AUX_100) {
222 if (mii_data & MII_AUX_FDX) {
223 *speed = IF_PORT_100BASEFX;
224 dev->if_port = IF_PORT_100BASEFX;
227 *speed = IF_PORT_100BASETX;
228 dev->if_port = IF_PORT_100BASETX;
232 *speed = IF_PORT_10BASET;
233 dev->if_port = IF_PORT_10BASET;
240 dev->if_port = IF_PORT_UNKNOWN;
245 int lsi_80227_init(struct net_device *dev, int phy_addr)
247 if (au1000_debug > 4)
248 printk("lsi_80227_init\n");
250 /* restart auto-negotiation */
251 mdio_write(dev, phy_addr, MII_CONTROL,
252 MII_CNTL_F100 | MII_CNTL_AUTO | MII_CNTL_RST_AUTO); // | MII_CNTL_FDX);
255 /* set up LEDs to correct display */
256 #ifdef CONFIG_MIPS_MTX1
257 mdio_write(dev, phy_addr, 17, 0xff80);
259 mdio_write(dev, phy_addr, 17, 0xffc0);
262 if (au1000_debug > 4)
263 dump_mii(dev, phy_addr);
267 int lsi_80227_reset(struct net_device *dev, int phy_addr)
269 s16 mii_control, timeout;
271 if (au1000_debug > 4) {
272 printk("lsi_80227_reset\n");
273 dump_mii(dev, phy_addr);
276 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
277 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
279 for (timeout = 100; timeout > 0; --timeout) {
280 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
281 if ((mii_control & MII_CNTL_RESET) == 0)
285 if (mii_control & MII_CNTL_RESET) {
286 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
293 lsi_80227_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
296 struct au1000_private *aup;
299 printk(KERN_ERR "lsi_80227_status error: NULL dev\n");
302 aup = (struct au1000_private *) dev->priv;
304 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
305 if (mii_data & MII_STAT_LINK) {
307 mii_data = mdio_read(dev, aup->phy_addr, MII_LSI_PHY_STAT);
308 if (mii_data & MII_LSI_PHY_STAT_SPD) {
309 if (mii_data & MII_LSI_PHY_STAT_FDX) {
310 *speed = IF_PORT_100BASEFX;
311 dev->if_port = IF_PORT_100BASEFX;
314 *speed = IF_PORT_100BASETX;
315 dev->if_port = IF_PORT_100BASETX;
319 *speed = IF_PORT_10BASET;
320 dev->if_port = IF_PORT_10BASET;
327 dev->if_port = IF_PORT_UNKNOWN;
332 int am79c901_init(struct net_device *dev, int phy_addr)
334 printk("am79c901_init\n");
338 int am79c901_reset(struct net_device *dev, int phy_addr)
340 printk("am79c901_reset\n");
345 am79c901_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
350 int am79c874_init(struct net_device *dev, int phy_addr)
354 /* 79c874 has quit resembled bit assignments to BCM5201 */
355 if (au1000_debug > 4)
356 printk("am79c847_init\n");
358 /* Stop auto-negotiation */
359 data = mdio_read(dev, phy_addr, MII_CONTROL);
360 mdio_write(dev, phy_addr, MII_CONTROL, data & ~MII_CNTL_AUTO);
362 /* Set advertisement to 10/100 and Half/Full duplex
363 * (full capabilities) */
364 data = mdio_read(dev, phy_addr, MII_ANADV);
365 data |= MII_NWAY_TX | MII_NWAY_TX_FDX | MII_NWAY_T_FDX | MII_NWAY_T;
366 mdio_write(dev, phy_addr, MII_ANADV, data);
368 /* Restart auto-negotiation */
369 data = mdio_read(dev, phy_addr, MII_CONTROL);
370 data |= MII_CNTL_RST_AUTO | MII_CNTL_AUTO;
372 mdio_write(dev, phy_addr, MII_CONTROL, data);
374 if (au1000_debug > 4) dump_mii(dev, phy_addr);
378 int am79c874_reset(struct net_device *dev, int phy_addr)
380 s16 mii_control, timeout;
382 if (au1000_debug > 4)
383 printk("am79c874_reset\n");
385 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
386 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
388 for (timeout = 100; timeout > 0; --timeout) {
389 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
390 if ((mii_control & MII_CNTL_RESET) == 0)
394 if (mii_control & MII_CNTL_RESET) {
395 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
402 am79c874_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
405 struct au1000_private *aup;
407 // printk("am79c874_status\n");
409 printk(KERN_ERR "am79c874_status error: NULL dev\n");
413 aup = (struct au1000_private *) dev->priv;
414 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
416 if (mii_data & MII_STAT_LINK) {
418 mii_data = mdio_read(dev, aup->phy_addr, MII_AMD_PHY_STAT);
419 if (mii_data & MII_AMD_PHY_STAT_SPD) {
420 if (mii_data & MII_AMD_PHY_STAT_FDX) {
421 *speed = IF_PORT_100BASEFX;
422 dev->if_port = IF_PORT_100BASEFX;
425 *speed = IF_PORT_100BASETX;
426 dev->if_port = IF_PORT_100BASETX;
430 *speed = IF_PORT_10BASET;
431 dev->if_port = IF_PORT_10BASET;
438 dev->if_port = IF_PORT_UNKNOWN;
443 int lxt971a_init(struct net_device *dev, int phy_addr)
445 if (au1000_debug > 4)
446 printk("lxt971a_init\n");
448 /* restart auto-negotiation */
449 mdio_write(dev, phy_addr, MII_CONTROL,
450 MII_CNTL_F100 | MII_CNTL_AUTO | MII_CNTL_RST_AUTO | MII_CNTL_FDX);
452 /* set up LEDs to correct display */
453 mdio_write(dev, phy_addr, 20, 0x0422);
455 if (au1000_debug > 4)
456 dump_mii(dev, phy_addr);
460 int lxt971a_reset(struct net_device *dev, int phy_addr)
462 s16 mii_control, timeout;
464 if (au1000_debug > 4) {
465 printk("lxt971a_reset\n");
466 dump_mii(dev, phy_addr);
469 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
470 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
472 for (timeout = 100; timeout > 0; --timeout) {
473 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
474 if ((mii_control & MII_CNTL_RESET) == 0)
478 if (mii_control & MII_CNTL_RESET) {
479 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
486 lxt971a_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
489 struct au1000_private *aup;
492 printk(KERN_ERR "lxt971a_status error: NULL dev\n");
495 aup = (struct au1000_private *) dev->priv;
497 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
498 if (mii_data & MII_STAT_LINK) {
500 mii_data = mdio_read(dev, aup->phy_addr, MII_INTEL_PHY_STAT);
501 if (mii_data & MII_INTEL_PHY_STAT_SPD) {
502 if (mii_data & MII_INTEL_PHY_STAT_FDX) {
503 *speed = IF_PORT_100BASEFX;
504 dev->if_port = IF_PORT_100BASEFX;
507 *speed = IF_PORT_100BASETX;
508 dev->if_port = IF_PORT_100BASETX;
512 *speed = IF_PORT_10BASET;
513 dev->if_port = IF_PORT_10BASET;
520 dev->if_port = IF_PORT_UNKNOWN;
525 int ks8995m_init(struct net_device *dev, int phy_addr)
529 // printk("ks8995m_init\n");
530 /* Stop auto-negotiation */
531 data = mdio_read(dev, phy_addr, MII_CONTROL);
532 mdio_write(dev, phy_addr, MII_CONTROL, data & ~MII_CNTL_AUTO);
534 /* Set advertisement to 10/100 and Half/Full duplex
535 * (full capabilities) */
536 data = mdio_read(dev, phy_addr, MII_ANADV);
537 data |= MII_NWAY_TX | MII_NWAY_TX_FDX | MII_NWAY_T_FDX | MII_NWAY_T;
538 mdio_write(dev, phy_addr, MII_ANADV, data);
540 /* Restart auto-negotiation */
541 data = mdio_read(dev, phy_addr, MII_CONTROL);
542 data |= MII_CNTL_RST_AUTO | MII_CNTL_AUTO;
543 mdio_write(dev, phy_addr, MII_CONTROL, data);
545 if (au1000_debug > 4) dump_mii(dev, phy_addr);
550 int ks8995m_reset(struct net_device *dev, int phy_addr)
552 s16 mii_control, timeout;
554 // printk("ks8995m_reset\n");
555 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
556 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
558 for (timeout = 100; timeout > 0; --timeout) {
559 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
560 if ((mii_control & MII_CNTL_RESET) == 0)
564 if (mii_control & MII_CNTL_RESET) {
565 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
571 int ks8995m_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
574 struct au1000_private *aup;
577 printk(KERN_ERR "ks8995m_status error: NULL dev\n");
580 aup = (struct au1000_private *) dev->priv;
582 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
583 if (mii_data & MII_STAT_LINK) {
585 mii_data = mdio_read(dev, aup->phy_addr, MII_AUX_CNTRL);
586 if (mii_data & MII_AUX_100) {
587 if (mii_data & MII_AUX_FDX) {
588 *speed = IF_PORT_100BASEFX;
589 dev->if_port = IF_PORT_100BASEFX;
592 *speed = IF_PORT_100BASETX;
593 dev->if_port = IF_PORT_100BASETX;
597 *speed = IF_PORT_10BASET;
598 dev->if_port = IF_PORT_10BASET;
605 dev->if_port = IF_PORT_UNKNOWN;
611 smsc_83C185_init (struct net_device *dev, int phy_addr)
615 if (au1000_debug > 4)
616 printk("smsc_83C185_init\n");
618 /* Stop auto-negotiation */
619 data = mdio_read(dev, phy_addr, MII_CONTROL);
620 mdio_write(dev, phy_addr, MII_CONTROL, data & ~MII_CNTL_AUTO);
622 /* Set advertisement to 10/100 and Half/Full duplex
623 * (full capabilities) */
624 data = mdio_read(dev, phy_addr, MII_ANADV);
625 data |= MII_NWAY_TX | MII_NWAY_TX_FDX | MII_NWAY_T_FDX | MII_NWAY_T;
626 mdio_write(dev, phy_addr, MII_ANADV, data);
628 /* Restart auto-negotiation */
629 data = mdio_read(dev, phy_addr, MII_CONTROL);
630 data |= MII_CNTL_RST_AUTO | MII_CNTL_AUTO;
632 mdio_write(dev, phy_addr, MII_CONTROL, data);
634 if (au1000_debug > 4) dump_mii(dev, phy_addr);
639 smsc_83C185_reset (struct net_device *dev, int phy_addr)
641 s16 mii_control, timeout;
643 if (au1000_debug > 4)
644 printk("smsc_83C185_reset\n");
646 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
647 mdio_write(dev, phy_addr, MII_CONTROL, mii_control | MII_CNTL_RESET);
649 for (timeout = 100; timeout > 0; --timeout) {
650 mii_control = mdio_read(dev, phy_addr, MII_CONTROL);
651 if ((mii_control & MII_CNTL_RESET) == 0)
655 if (mii_control & MII_CNTL_RESET) {
656 printk(KERN_ERR "%s PHY reset timeout !\n", dev->name);
663 smsc_83C185_status (struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
666 struct au1000_private *aup;
669 printk(KERN_ERR "smsc_83C185_status error: NULL dev\n");
673 aup = (struct au1000_private *) dev->priv;
674 mii_data = mdio_read(dev, aup->phy_addr, MII_STATUS);
676 if (mii_data & MII_STAT_LINK) {
678 mii_data = mdio_read(dev, aup->phy_addr, 0x1f);
679 if (mii_data & (1<<3)) {
680 if (mii_data & (1<<4)) {
681 *speed = IF_PORT_100BASEFX;
682 dev->if_port = IF_PORT_100BASEFX;
685 *speed = IF_PORT_100BASETX;
686 dev->if_port = IF_PORT_100BASETX;
690 *speed = IF_PORT_10BASET;
691 dev->if_port = IF_PORT_10BASET;
697 dev->if_port = IF_PORT_UNKNOWN;
703 #ifdef CONFIG_MIPS_BOSPORUS
704 int stub_init(struct net_device *dev, int phy_addr)
706 //printk("PHY stub_init\n");
710 int stub_reset(struct net_device *dev, int phy_addr)
712 //printk("PHY stub_reset\n");
717 stub_status(struct net_device *dev, int phy_addr, u16 *link, u16 *speed)
719 //printk("PHY stub_status\n");
722 *speed = IF_PORT_100BASEFX;
723 dev->if_port = IF_PORT_100BASEFX;
728 struct phy_ops bcm_5201_ops = {
734 struct phy_ops am79c874_ops = {
740 struct phy_ops am79c901_ops = {
746 struct phy_ops lsi_80227_ops = {
752 struct phy_ops lxt971a_ops = {
758 struct phy_ops ks8995m_ops = {
764 struct phy_ops smsc_83C185_ops = {
770 #ifdef CONFIG_MIPS_BOSPORUS
771 struct phy_ops stub_ops = {
778 static struct mii_chip_info {
782 struct phy_ops *phy_ops;
784 } mii_chip_table[] = {
785 {"Broadcom BCM5201 10/100 BaseT PHY",0x0040,0x6212, &bcm_5201_ops,0},
786 {"Broadcom BCM5221 10/100 BaseT PHY",0x0040,0x61e4, &bcm_5201_ops,0},
787 {"Broadcom BCM5222 10/100 BaseT PHY",0x0040,0x6322, &bcm_5201_ops,1},
788 {"AMD 79C901 HomePNA PHY",0x0000,0x35c8, &am79c901_ops,0},
789 {"AMD 79C874 10/100 BaseT PHY",0x0022,0x561b, &am79c874_ops,0},
790 {"LSI 80227 10/100 BaseT PHY",0x0016,0xf840, &lsi_80227_ops,0},
791 {"Intel LXT971A Dual Speed PHY",0x0013,0x78e2, &lxt971a_ops,0},
792 {"Kendin KS8995M 10/100 BaseT PHY",0x0022,0x1450, &ks8995m_ops,0},
793 {"SMSC LAN83C185 10/100 BaseT PHY",0x0007,0xc0a3, &smsc_83C185_ops,0},
794 #ifdef CONFIG_MIPS_BOSPORUS
795 {"Stub", 0x1234, 0x5678, &stub_ops },
800 static int mdio_read(struct net_device *dev, int phy_id, int reg)
802 struct au1000_private *aup = (struct au1000_private *) dev->priv;
803 volatile u32 *mii_control_reg;
804 volatile u32 *mii_data_reg;
808 #ifdef CONFIG_BCM5222_DUAL_PHY
809 /* First time we probe, it's for the mac0 phy.
810 * Since we haven't determined yet that we have a dual phy,
811 * aup->mii->mii_control_reg won't be setup and we'll
812 * default to the else statement.
813 * By the time we probe for the mac1 phy, the mii_control_reg
814 * will be setup to be the address of the mac0 phy control since
815 * both phys are controlled through mac0.
817 if (aup->mii && aup->mii->mii_control_reg) {
818 mii_control_reg = aup->mii->mii_control_reg;
819 mii_data_reg = aup->mii->mii_data_reg;
821 else if (au_macs[0]->mii && au_macs[0]->mii->mii_control_reg) {
822 /* assume both phys are controlled through mac0 */
823 mii_control_reg = au_macs[0]->mii->mii_control_reg;
824 mii_data_reg = au_macs[0]->mii->mii_data_reg;
829 /* default control and data reg addresses */
830 mii_control_reg = &aup->mac->mii_control;
831 mii_data_reg = &aup->mac->mii_data;
834 while (*mii_control_reg & MAC_MII_BUSY) {
836 if (--timedout == 0) {
837 printk(KERN_ERR "%s: read_MII busy timeout!!\n",
843 mii_control = MAC_SET_MII_SELECT_REG(reg) |
844 MAC_SET_MII_SELECT_PHY(phy_id) | MAC_MII_READ;
846 *mii_control_reg = mii_control;
849 while (*mii_control_reg & MAC_MII_BUSY) {
851 if (--timedout == 0) {
852 printk(KERN_ERR "%s: mdio_read busy timeout!!\n",
857 return (int)*mii_data_reg;
860 static void mdio_write(struct net_device *dev, int phy_id, int reg, u16 value)
862 struct au1000_private *aup = (struct au1000_private *) dev->priv;
863 volatile u32 *mii_control_reg;
864 volatile u32 *mii_data_reg;
868 #ifdef CONFIG_BCM5222_DUAL_PHY
869 if (aup->mii && aup->mii->mii_control_reg) {
870 mii_control_reg = aup->mii->mii_control_reg;
871 mii_data_reg = aup->mii->mii_data_reg;
873 else if (au_macs[0]->mii && au_macs[0]->mii->mii_control_reg) {
874 /* assume both phys are controlled through mac0 */
875 mii_control_reg = au_macs[0]->mii->mii_control_reg;
876 mii_data_reg = au_macs[0]->mii->mii_data_reg;
881 /* default control and data reg addresses */
882 mii_control_reg = &aup->mac->mii_control;
883 mii_data_reg = &aup->mac->mii_data;
886 while (*mii_control_reg & MAC_MII_BUSY) {
888 if (--timedout == 0) {
889 printk(KERN_ERR "%s: mdio_write busy timeout!!\n",
895 mii_control = MAC_SET_MII_SELECT_REG(reg) |
896 MAC_SET_MII_SELECT_PHY(phy_id) | MAC_MII_WRITE;
898 *mii_data_reg = value;
899 *mii_control_reg = mii_control;
903 static void dump_mii(struct net_device *dev, int phy_id)
907 for (i = 0; i < 7; i++) {
908 if ((val = mdio_read(dev, phy_id, i)) >= 0)
909 printk("%s: MII Reg %d=%x\n", dev->name, i, val);
911 for (i = 16; i < 25; i++) {
912 if ((val = mdio_read(dev, phy_id, i)) >= 0)
913 printk("%s: MII Reg %d=%x\n", dev->name, i, val);
917 static int mii_probe (struct net_device * dev)
919 struct au1000_private *aup = (struct au1000_private *) dev->priv;
921 #ifdef CONFIG_MIPS_BOSPORUS
925 /* search for total of 32 possible mii phy addresses */
926 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
928 u16 phy_id0, phy_id1;
931 #ifdef CONFIG_BCM5222_DUAL_PHY
932 /* Mask the already found phy, try next one */
933 if (au_macs[0]->mii && au_macs[0]->mii->mii_control_reg) {
934 if (au_macs[0]->phy_addr == phy_addr)
939 mii_status = mdio_read(dev, phy_addr, MII_STATUS);
940 if (mii_status == 0xffff || mii_status == 0x0000)
941 /* the mii is not accessable, try next one */
944 phy_id0 = mdio_read(dev, phy_addr, MII_PHY_ID0);
945 phy_id1 = mdio_read(dev, phy_addr, MII_PHY_ID1);
947 /* search our mii table for the current mii */
948 for (i = 0; mii_chip_table[i].phy_id1; i++) {
949 if (phy_id0 == mii_chip_table[i].phy_id0 &&
950 phy_id1 == mii_chip_table[i].phy_id1) {
951 struct mii_phy * mii_phy = aup->mii;
953 printk(KERN_INFO "%s: %s at phy address %d\n",
954 dev->name, mii_chip_table[i].name,
956 #ifdef CONFIG_MIPS_BOSPORUS
959 mii_phy->chip_info = mii_chip_table+i;
960 aup->phy_addr = phy_addr;
961 aup->want_autoneg = 1;
962 aup->phy_ops = mii_chip_table[i].phy_ops;
963 aup->phy_ops->phy_init(dev,phy_addr);
965 // Check for dual-phy and then store required
966 // values and set indicators. We need to do
967 // this now since mdio_{read,write} need the
968 // control and data register addresses.
969 #ifdef CONFIG_BCM5222_DUAL_PHY
970 if ( mii_chip_table[i].dual_phy) {
972 /* assume both phys are controlled
973 * through MAC0. Board specific? */
976 if (!au_macs[0] || !au_macs[0]->mii)
978 aup->mii->mii_control_reg = (u32 *)
979 &au_macs[0]->mac->mii_control;
980 aup->mii->mii_data_reg = (u32 *)
981 &au_macs[0]->mac->mii_data;
990 #ifdef CONFIG_MIPS_BOSPORUS
991 /* This is a workaround for the Micrel/Kendin 5 port switch
992 The second MAC doesn't see a PHY connected... so we need to
993 trick it into thinking we have one.
995 If this kernel is run on another Au1500 development board
996 the stub will be found as well as the actual PHY. However,
997 the last found PHY will be used... usually at Addr 31 (Db1500).
1001 u16 phy_id0, phy_id1;
1007 /* search our mii table for the current mii */
1008 for (i = 0; mii_chip_table[i].phy_id1; i++) {
1009 if (phy_id0 == mii_chip_table[i].phy_id0 &&
1010 phy_id1 == mii_chip_table[i].phy_id1) {
1011 struct mii_phy * mii_phy;
1013 printk(KERN_INFO "%s: %s at phy address %d\n",
1014 dev->name, mii_chip_table[i].name,
1016 mii_phy = kmalloc(sizeof(struct mii_phy),
1019 mii_phy->chip_info = mii_chip_table+i;
1020 aup->phy_addr = phy_addr;
1021 mii_phy->next = aup->mii;
1023 mii_chip_table[i].phy_ops;
1025 aup->phy_ops->phy_init(dev,phy_addr);
1027 printk(KERN_ERR "%s: out of memory\n",
1031 mii_phy->chip_info = mii_chip_table+i;
1032 aup->phy_addr = phy_addr;
1033 aup->phy_ops = mii_chip_table[i].phy_ops;
1034 aup->phy_ops->phy_init(dev,phy_addr);
1039 if (aup->mac_id == 0) {
1040 /* the Bosporus phy responds to addresses 0-5 but
1041 * 5 is the correct one.
1047 if (aup->mii->chip_info == NULL) {
1048 printk(KERN_ERR "%s: Au1x No MII transceivers found!\n",
1053 printk(KERN_INFO "%s: Using %s as default\n",
1054 dev->name, aup->mii->chip_info->name);
1061 * Buffer allocation/deallocation routines. The buffer descriptor returned
1062 * has the virtual and dma address of a buffer suitable for
1063 * both, receive and transmit operations.
1065 static db_dest_t *GetFreeDB(struct au1000_private *aup)
1071 aup->pDBfree = pDB->pnext;
1076 void ReleaseDB(struct au1000_private *aup, db_dest_t *pDB)
1078 db_dest_t *pDBfree = aup->pDBfree;
1080 pDBfree->pnext = pDB;
1084 static void enable_rx_tx(struct net_device *dev)
1086 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1088 if (au1000_debug > 4)
1089 printk(KERN_INFO "%s: enable_rx_tx\n", dev->name);
1091 aup->mac->control |= (MAC_RX_ENABLE | MAC_TX_ENABLE);
1095 static void hard_stop(struct net_device *dev)
1097 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1099 if (au1000_debug > 4)
1100 printk(KERN_INFO "%s: hard stop\n", dev->name);
1102 aup->mac->control &= ~(MAC_RX_ENABLE | MAC_TX_ENABLE);
1107 static void reset_mac(struct net_device *dev)
1111 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1113 if (au1000_debug > 4)
1114 printk(KERN_INFO "%s: reset mac, aup %x\n",
1115 dev->name, (unsigned)aup);
1117 spin_lock_irqsave(&aup->lock, flags);
1118 if (aup->timer.function == &au1000_timer) {/* check if timer initted */
1119 del_timer(&aup->timer);
1123 #ifdef CONFIG_BCM5222_DUAL_PHY
1124 if (aup->mac_id != 0) {
1126 /* If BCM5222, we can't leave MAC0 in reset because then
1127 * we can't access the dual phy for ETH1 */
1128 *aup->enable = MAC_EN_CLOCK_ENABLE;
1132 #ifdef CONFIG_BCM5222_DUAL_PHY
1136 for (i = 0; i < NUM_RX_DMA; i++) {
1137 /* reset control bits */
1138 aup->rx_dma_ring[i]->buff_stat &= ~0xf;
1140 for (i = 0; i < NUM_TX_DMA; i++) {
1141 /* reset control bits */
1142 aup->tx_dma_ring[i]->buff_stat &= ~0xf;
1144 spin_unlock_irqrestore(&aup->lock, flags);
1149 * Setup the receive and transmit "rings". These pointers are the addresses
1150 * of the rx and tx MAC DMA registers so they are fixed by the hardware --
1151 * these are not descriptors sitting in memory.
1154 setup_hw_rings(struct au1000_private *aup, u32 rx_base, u32 tx_base)
1158 for (i = 0; i < NUM_RX_DMA; i++) {
1159 aup->rx_dma_ring[i] =
1160 (volatile rx_dma_t *) (rx_base + sizeof(rx_dma_t)*i);
1162 for (i = 0; i < NUM_TX_DMA; i++) {
1163 aup->tx_dma_ring[i] =
1164 (volatile tx_dma_t *) (tx_base + sizeof(tx_dma_t)*i);
1173 struct net_device *dev;
1179 * Setup the base address and interupt of the Au1xxx ethernet macs
1180 * based on cpu type and whether the interface is enabled in sys_pinfunc
1181 * register. The last interface is enabled if SYS_PF_NI2 (bit 4) is 0.
1183 static int __init au1000_init_module(void)
1185 struct cpuinfo_mips *c = ¤t_cpu_data;
1186 int ni = (int)((au_readl(SYS_PINFUNC) & (u32)(SYS_PF_NI2)) >> 4);
1187 struct net_device *dev;
1188 int i, found_one = 0;
1190 switch (c->cputype) {
1191 #ifdef CONFIG_SOC_AU1000
1194 iflist[0].base_addr = AU1000_ETH0_BASE;
1195 iflist[1].base_addr = AU1000_ETH1_BASE;
1196 iflist[0].macen_addr = AU1000_MAC0_ENABLE;
1197 iflist[1].macen_addr = AU1000_MAC1_ENABLE;
1198 iflist[0].irq = AU1000_MAC0_DMA_INT;
1199 iflist[1].irq = AU1000_MAC1_DMA_INT;
1202 #ifdef CONFIG_SOC_AU1100
1205 iflist[0].base_addr = AU1100_ETH0_BASE;
1206 iflist[0].macen_addr = AU1100_MAC0_ENABLE;
1207 iflist[0].irq = AU1100_MAC0_DMA_INT;
1210 #ifdef CONFIG_SOC_AU1500
1213 iflist[0].base_addr = AU1500_ETH0_BASE;
1214 iflist[1].base_addr = AU1500_ETH1_BASE;
1215 iflist[0].macen_addr = AU1500_MAC0_ENABLE;
1216 iflist[1].macen_addr = AU1500_MAC1_ENABLE;
1217 iflist[0].irq = AU1500_MAC0_DMA_INT;
1218 iflist[1].irq = AU1500_MAC1_DMA_INT;
1221 #ifdef CONFIG_SOC_AU1550
1224 iflist[0].base_addr = AU1550_ETH0_BASE;
1225 iflist[1].base_addr = AU1550_ETH1_BASE;
1226 iflist[0].macen_addr = AU1550_MAC0_ENABLE;
1227 iflist[1].macen_addr = AU1550_MAC1_ENABLE;
1228 iflist[0].irq = AU1550_MAC0_DMA_INT;
1229 iflist[1].irq = AU1550_MAC1_DMA_INT;
1235 for(i = 0; i < num_ifs; i++) {
1236 dev = au1000_probe(iflist[i].base_addr, iflist[i].irq, i);
1237 iflist[i].dev = dev;
1246 static int au1000_setup_aneg(struct net_device *dev, u32 advertise)
1248 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1251 /* Setup standard advertise */
1252 adv = mdio_read(dev, aup->phy_addr, MII_ADVERTISE);
1253 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4);
1254 if (advertise & ADVERTISED_10baseT_Half)
1255 adv |= ADVERTISE_10HALF;
1256 if (advertise & ADVERTISED_10baseT_Full)
1257 adv |= ADVERTISE_10FULL;
1258 if (advertise & ADVERTISED_100baseT_Half)
1259 adv |= ADVERTISE_100HALF;
1260 if (advertise & ADVERTISED_100baseT_Full)
1261 adv |= ADVERTISE_100FULL;
1262 mdio_write(dev, aup->phy_addr, MII_ADVERTISE, adv);
1264 /* Start/Restart aneg */
1265 ctl = mdio_read(dev, aup->phy_addr, MII_BMCR);
1266 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
1267 mdio_write(dev, aup->phy_addr, MII_BMCR, ctl);
1272 static int au1000_setup_forced(struct net_device *dev, int speed, int fd)
1274 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1277 ctl = mdio_read(dev, aup->phy_addr, MII_BMCR);
1278 ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_ANENABLE);
1280 /* First reset the PHY */
1281 mdio_write(dev, aup->phy_addr, MII_BMCR, ctl | BMCR_RESET);
1283 /* Select speed & duplex */
1288 ctl |= BMCR_SPEED100;
1294 if (fd == DUPLEX_FULL)
1295 ctl |= BMCR_FULLDPLX;
1296 mdio_write(dev, aup->phy_addr, MII_BMCR, ctl);
1303 au1000_start_link(struct net_device *dev, struct ethtool_cmd *cmd)
1305 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1311 /* Default advertise */
1312 advertise = GENMII_DEFAULT_ADVERTISE;
1313 autoneg = aup->want_autoneg;
1314 forced_speed = SPEED_100;
1315 forced_duplex = DUPLEX_FULL;
1317 /* Setup link parameters */
1319 if (cmd->autoneg == AUTONEG_ENABLE) {
1320 advertise = cmd->advertising;
1325 forced_speed = cmd->speed;
1326 forced_duplex = cmd->duplex;
1330 /* Configure PHY & start aneg */
1331 aup->want_autoneg = autoneg;
1333 au1000_setup_aneg(dev, advertise);
1335 au1000_setup_forced(dev, forced_speed, forced_duplex);
1336 mod_timer(&aup->timer, jiffies + HZ);
1339 static int au1000_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1341 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1344 cmd->supported = GENMII_DEFAULT_FEATURES;
1345 cmd->advertising = GENMII_DEFAULT_ADVERTISE;
1346 cmd->port = PORT_MII;
1347 cmd->transceiver = XCVR_EXTERNAL;
1348 cmd->phy_address = aup->phy_addr;
1349 spin_lock_irq(&aup->lock);
1350 cmd->autoneg = aup->want_autoneg;
1351 aup->phy_ops->phy_status(dev, aup->phy_addr, &link, &speed);
1352 if ((speed == IF_PORT_100BASETX) || (speed == IF_PORT_100BASEFX))
1353 cmd->speed = SPEED_100;
1354 else if (speed == IF_PORT_10BASET)
1355 cmd->speed = SPEED_10;
1356 if (link && (dev->if_port == IF_PORT_100BASEFX))
1357 cmd->duplex = DUPLEX_FULL;
1359 cmd->duplex = DUPLEX_HALF;
1360 spin_unlock_irq(&aup->lock);
1364 static int au1000_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1366 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1367 unsigned long features = GENMII_DEFAULT_FEATURES;
1369 if (!capable(CAP_NET_ADMIN))
1372 if (cmd->autoneg != AUTONEG_ENABLE && cmd->autoneg != AUTONEG_DISABLE)
1374 if (cmd->autoneg == AUTONEG_ENABLE && cmd->advertising == 0)
1376 if (cmd->duplex != DUPLEX_HALF && cmd->duplex != DUPLEX_FULL)
1378 if (cmd->autoneg == AUTONEG_DISABLE)
1379 switch (cmd->speed) {
1381 if (cmd->duplex == DUPLEX_HALF &&
1382 (features & SUPPORTED_10baseT_Half) == 0)
1384 if (cmd->duplex == DUPLEX_FULL &&
1385 (features & SUPPORTED_10baseT_Full) == 0)
1389 if (cmd->duplex == DUPLEX_HALF &&
1390 (features & SUPPORTED_100baseT_Half) == 0)
1392 if (cmd->duplex == DUPLEX_FULL &&
1393 (features & SUPPORTED_100baseT_Full) == 0)
1399 else if ((features & SUPPORTED_Autoneg) == 0)
1402 spin_lock_irq(&aup->lock);
1403 au1000_start_link(dev, cmd);
1404 spin_unlock_irq(&aup->lock);
1408 static int au1000_nway_reset(struct net_device *dev)
1410 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1412 if (!aup->want_autoneg)
1414 spin_lock_irq(&aup->lock);
1415 au1000_start_link(dev, NULL);
1416 spin_unlock_irq(&aup->lock);
1421 au1000_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
1423 struct au1000_private *aup = (struct au1000_private *)dev->priv;
1425 strcpy(info->driver, DRV_NAME);
1426 strcpy(info->version, DRV_VERSION);
1427 info->fw_version[0] = '\0';
1428 sprintf(info->bus_info, "%s %d", DRV_NAME, aup->mac_id);
1429 info->regdump_len = 0;
1432 static u32 au1000_get_link(struct net_device *dev)
1434 return netif_carrier_ok(dev);
1437 static struct ethtool_ops au1000_ethtool_ops = {
1438 .get_settings = au1000_get_settings,
1439 .set_settings = au1000_set_settings,
1440 .get_drvinfo = au1000_get_drvinfo,
1441 .nway_reset = au1000_nway_reset,
1442 .get_link = au1000_get_link
1445 static struct net_device *
1446 au1000_probe(u32 ioaddr, int irq, int port_num)
1448 static unsigned version_printed = 0;
1449 struct au1000_private *aup = NULL;
1450 struct net_device *dev = NULL;
1451 db_dest_t *pDB, *pDBfree;
1452 char *pmac, *argptr;
1456 if (!request_mem_region(CPHYSADDR(ioaddr), MAC_IOSIZE, "Au1x00 ENET"))
1459 if (version_printed++ == 0)
1460 printk("%s version %s %s\n", DRV_NAME, DRV_VERSION, DRV_AUTHOR);
1462 dev = alloc_etherdev(sizeof(struct au1000_private));
1464 printk (KERN_ERR "au1000 eth: alloc_etherdev failed\n");
1468 if ((err = register_netdev(dev))) {
1469 printk(KERN_ERR "Au1x_eth Cannot register net device err %d\n",
1475 printk("%s: Au1x Ethernet found at 0x%x, irq %d\n",
1476 dev->name, ioaddr, irq);
1480 /* Allocate the data buffers */
1481 /* Snooping works fine with eth on all au1xxx */
1482 aup->vaddr = (u32)dma_alloc_noncoherent(NULL,
1483 MAX_BUF_SIZE * (NUM_TX_BUFFS+NUM_RX_BUFFS),
1488 release_mem_region(CPHYSADDR(ioaddr), MAC_IOSIZE);
1492 /* aup->mac is the base address of the MAC's registers */
1493 aup->mac = (volatile mac_reg_t *)((unsigned long)ioaddr);
1494 /* Setup some variables for quick register address access */
1495 if (ioaddr == iflist[0].base_addr)
1497 /* check env variables first */
1498 if (!get_ethernet_addr(ethaddr)) {
1499 memcpy(au1000_mac_addr, ethaddr, sizeof(au1000_mac_addr));
1501 /* Check command line */
1502 argptr = prom_getcmdline();
1503 if ((pmac = strstr(argptr, "ethaddr=")) == NULL) {
1504 printk(KERN_INFO "%s: No mac address found\n",
1506 /* use the hard coded mac addresses */
1508 str2eaddr(ethaddr, pmac + strlen("ethaddr="));
1509 memcpy(au1000_mac_addr, ethaddr,
1510 sizeof(au1000_mac_addr));
1513 aup->enable = (volatile u32 *)
1514 ((unsigned long)iflist[0].macen_addr);
1515 memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
1516 setup_hw_rings(aup, MAC0_RX_DMA_ADDR, MAC0_TX_DMA_ADDR);
1521 if (ioaddr == iflist[1].base_addr)
1523 aup->enable = (volatile u32 *)
1524 ((unsigned long)iflist[1].macen_addr);
1525 memcpy(dev->dev_addr, au1000_mac_addr, sizeof(au1000_mac_addr));
1526 dev->dev_addr[4] += 0x10;
1527 setup_hw_rings(aup, MAC1_RX_DMA_ADDR, MAC1_TX_DMA_ADDR);
1533 printk(KERN_ERR "%s: bad ioaddr\n", dev->name);
1536 /* bring the device out of reset, otherwise probing the mii
1538 *aup->enable = MAC_EN_CLOCK_ENABLE;
1540 *aup->enable = MAC_EN_RESET0 | MAC_EN_RESET1 |
1541 MAC_EN_RESET2 | MAC_EN_CLOCK_ENABLE;
1544 aup->mii = kmalloc(sizeof(struct mii_phy), GFP_KERNEL);
1546 printk(KERN_ERR "%s: out of memory\n", dev->name);
1549 aup->mii->mii_control_reg = 0;
1550 aup->mii->mii_data_reg = 0;
1552 if (mii_probe(dev) != 0) {
1557 /* setup the data buffer descriptors and attach a buffer to each one */
1559 for (i = 0; i < (NUM_TX_BUFFS+NUM_RX_BUFFS); i++) {
1560 pDB->pnext = pDBfree;
1562 pDB->vaddr = (u32 *)((unsigned)aup->vaddr + MAX_BUF_SIZE*i);
1563 pDB->dma_addr = (dma_addr_t)virt_to_bus(pDB->vaddr);
1566 aup->pDBfree = pDBfree;
1568 for (i = 0; i < NUM_RX_DMA; i++) {
1569 pDB = GetFreeDB(aup);
1573 aup->rx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1574 aup->rx_db_inuse[i] = pDB;
1576 for (i = 0; i < NUM_TX_DMA; i++) {
1577 pDB = GetFreeDB(aup);
1581 aup->tx_dma_ring[i]->buff_stat = (unsigned)pDB->dma_addr;
1582 aup->tx_dma_ring[i]->len = 0;
1583 aup->tx_db_inuse[i] = pDB;
1586 spin_lock_init(&aup->lock);
1587 dev->base_addr = ioaddr;
1589 dev->open = au1000_open;
1590 dev->hard_start_xmit = au1000_tx;
1591 dev->stop = au1000_close;
1592 dev->get_stats = au1000_get_stats;
1593 dev->set_multicast_list = &set_rx_mode;
1594 dev->do_ioctl = &au1000_ioctl;
1595 SET_ETHTOOL_OPS(dev, &au1000_ethtool_ops);
1596 dev->set_config = &au1000_set_config;
1597 dev->tx_timeout = au1000_tx_timeout;
1598 dev->watchdog_timeo = ETH_TX_TIMEOUT;
1601 * The boot code uses the ethernet controller, so reset it to start
1602 * fresh. au1000_init() expects that the device is in reset state.
1609 /* here we should have a valid dev plus aup-> register addresses
1610 * so we can reset the mac properly.*/
1614 for (i = 0; i < NUM_RX_DMA; i++) {
1615 if (aup->rx_db_inuse[i])
1616 ReleaseDB(aup, aup->rx_db_inuse[i]);
1618 for (i = 0; i < NUM_TX_DMA; i++) {
1619 if (aup->tx_db_inuse[i])
1620 ReleaseDB(aup, aup->tx_db_inuse[i]);
1622 dma_free_noncoherent(NULL,
1623 MAX_BUF_SIZE * (NUM_TX_BUFFS+NUM_RX_BUFFS),
1626 unregister_netdev(dev);
1628 release_mem_region(CPHYSADDR(ioaddr), MAC_IOSIZE);
1633 * Initialize the interface.
1635 * When the device powers up, the clocks are disabled and the
1636 * mac is in reset state. When the interface is closed, we
1637 * do the same -- reset the device and disable the clocks to
1638 * conserve power. Thus, whenever au1000_init() is called,
1639 * the device should already be in reset state.
1641 static int au1000_init(struct net_device *dev)
1643 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1649 if (au1000_debug > 4)
1650 printk("%s: au1000_init\n", dev->name);
1652 spin_lock_irqsave(&aup->lock, flags);
1654 /* bring the device out of reset */
1655 *aup->enable = MAC_EN_CLOCK_ENABLE;
1657 *aup->enable = MAC_EN_RESET0 | MAC_EN_RESET1 |
1658 MAC_EN_RESET2 | MAC_EN_CLOCK_ENABLE;
1661 aup->mac->control = 0;
1662 aup->tx_head = (aup->tx_dma_ring[0]->buff_stat & 0xC) >> 2;
1663 aup->tx_tail = aup->tx_head;
1664 aup->rx_head = (aup->rx_dma_ring[0]->buff_stat & 0xC) >> 2;
1666 aup->mac->mac_addr_high = dev->dev_addr[5]<<8 | dev->dev_addr[4];
1667 aup->mac->mac_addr_low = dev->dev_addr[3]<<24 | dev->dev_addr[2]<<16 |
1668 dev->dev_addr[1]<<8 | dev->dev_addr[0];
1670 for (i = 0; i < NUM_RX_DMA; i++) {
1671 aup->rx_dma_ring[i]->buff_stat |= RX_DMA_ENABLE;
1675 aup->phy_ops->phy_status(dev, aup->phy_addr, &link, &speed);
1676 control = MAC_DISABLE_RX_OWN | MAC_RX_ENABLE | MAC_TX_ENABLE;
1677 #ifndef CONFIG_CPU_LITTLE_ENDIAN
1678 control |= MAC_BIG_ENDIAN;
1680 if (link && (dev->if_port == IF_PORT_100BASEFX)) {
1681 control |= MAC_FULL_DUPLEX;
1684 aup->mac->control = control;
1685 aup->mac->vlan1_tag = 0x8100; /* activate vlan support */
1688 spin_unlock_irqrestore(&aup->lock, flags);
1692 static void au1000_timer(unsigned long data)
1694 struct net_device *dev = (struct net_device *)data;
1695 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1696 unsigned char if_port;
1700 /* fatal error, don't restart the timer */
1701 printk(KERN_ERR "au1000_timer error: NULL dev\n");
1705 if_port = dev->if_port;
1706 if (aup->phy_ops->phy_status(dev, aup->phy_addr, &link, &speed) == 0) {
1708 if (!netif_carrier_ok(dev)) {
1709 netif_carrier_on(dev);
1710 printk(KERN_INFO "%s: link up\n", dev->name);
1714 if (netif_carrier_ok(dev)) {
1715 netif_carrier_off(dev);
1717 printk(KERN_INFO "%s: link down\n", dev->name);
1722 if (link && (dev->if_port != if_port) &&
1723 (dev->if_port != IF_PORT_UNKNOWN)) {
1725 if (dev->if_port == IF_PORT_100BASEFX) {
1726 printk(KERN_INFO "%s: going to full duplex\n",
1728 aup->mac->control |= MAC_FULL_DUPLEX;
1732 aup->mac->control &= ~MAC_FULL_DUPLEX;
1738 aup->timer.expires = RUN_AT((1*HZ));
1739 aup->timer.data = (unsigned long)dev;
1740 aup->timer.function = &au1000_timer; /* timer handler */
1741 add_timer(&aup->timer);
1745 static int au1000_open(struct net_device *dev)
1748 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1750 if (au1000_debug > 4)
1751 printk("%s: open: dev=%p\n", dev->name, dev);
1753 if ((retval = au1000_init(dev))) {
1754 printk(KERN_ERR "%s: error in au1000_init\n", dev->name);
1755 free_irq(dev->irq, dev);
1758 netif_start_queue(dev);
1760 if ((retval = request_irq(dev->irq, &au1000_interrupt, 0,
1762 printk(KERN_ERR "%s: unable to get IRQ %d\n",
1763 dev->name, dev->irq);
1767 init_timer(&aup->timer); /* used in ioctl() */
1768 aup->timer.expires = RUN_AT((3*HZ));
1769 aup->timer.data = (unsigned long)dev;
1770 aup->timer.function = &au1000_timer; /* timer handler */
1771 add_timer(&aup->timer);
1773 if (au1000_debug > 4)
1774 printk("%s: open: Initialization done.\n", dev->name);
1779 static int au1000_close(struct net_device *dev)
1782 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1784 if (au1000_debug > 4)
1785 printk("%s: close: dev=%p\n", dev->name, dev);
1789 spin_lock_irqsave(&aup->lock, flags);
1791 /* stop the device */
1792 netif_stop_queue(dev);
1794 /* disable the interrupt */
1795 free_irq(dev->irq, dev);
1796 spin_unlock_irqrestore(&aup->lock, flags);
1801 static void __exit au1000_cleanup_module(void)
1804 struct net_device *dev;
1805 struct au1000_private *aup;
1807 for (i = 0; i < num_ifs; i++) {
1808 dev = iflist[i].dev;
1810 aup = (struct au1000_private *) dev->priv;
1811 unregister_netdev(dev);
1814 for (j = 0; j < NUM_RX_DMA; j++) {
1815 if (aup->rx_db_inuse[j])
1816 ReleaseDB(aup, aup->rx_db_inuse[j]);
1818 for (j = 0; j < NUM_TX_DMA; j++) {
1819 if (aup->tx_db_inuse[j])
1820 ReleaseDB(aup, aup->tx_db_inuse[j]);
1822 dma_free_noncoherent(NULL,
1823 MAX_BUF_SIZE * (NUM_TX_BUFFS+NUM_RX_BUFFS),
1827 release_mem_region(CPHYSADDR(iflist[i].base_addr), MAC_IOSIZE);
1834 update_tx_stats(struct net_device *dev, u32 status, u32 pkt_len)
1836 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1837 struct net_device_stats *ps = &aup->stats;
1840 ps->tx_bytes += pkt_len;
1842 if (status & TX_FRAME_ABORTED) {
1843 if (dev->if_port == IF_PORT_100BASEFX) {
1844 if (status & (TX_JAB_TIMEOUT | TX_UNDERRUN)) {
1845 /* any other tx errors are only valid
1846 * in half duplex mode */
1848 ps->tx_aborted_errors++;
1853 ps->tx_aborted_errors++;
1854 if (status & (TX_NO_CARRIER | TX_LOSS_CARRIER))
1855 ps->tx_carrier_errors++;
1862 * Called from the interrupt service routine to acknowledge
1863 * the TX DONE bits. This is a must if the irq is setup as
1866 static void au1000_tx_ack(struct net_device *dev)
1868 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1869 volatile tx_dma_t *ptxd;
1871 ptxd = aup->tx_dma_ring[aup->tx_tail];
1873 while (ptxd->buff_stat & TX_T_DONE) {
1874 update_tx_stats(dev, ptxd->status, ptxd->len & 0x3ff);
1875 ptxd->buff_stat &= ~TX_T_DONE;
1879 aup->tx_tail = (aup->tx_tail + 1) & (NUM_TX_DMA - 1);
1880 ptxd = aup->tx_dma_ring[aup->tx_tail];
1884 netif_wake_queue(dev);
1891 * Au1000 transmit routine.
1893 static int au1000_tx(struct sk_buff *skb, struct net_device *dev)
1895 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1896 volatile tx_dma_t *ptxd;
1901 if (au1000_debug > 5)
1902 printk("%s: tx: aup %x len=%d, data=%p, head %d\n",
1903 dev->name, (unsigned)aup, skb->len,
1904 skb->data, aup->tx_head);
1906 ptxd = aup->tx_dma_ring[aup->tx_head];
1907 buff_stat = ptxd->buff_stat;
1908 if (buff_stat & TX_DMA_ENABLE) {
1909 /* We've wrapped around and the transmitter is still busy */
1910 netif_stop_queue(dev);
1914 else if (buff_stat & TX_T_DONE) {
1915 update_tx_stats(dev, ptxd->status, ptxd->len & 0x3ff);
1921 netif_wake_queue(dev);
1924 pDB = aup->tx_db_inuse[aup->tx_head];
1925 memcpy((void *)pDB->vaddr, skb->data, skb->len);
1926 if (skb->len < ETH_ZLEN) {
1927 for (i=skb->len; i<ETH_ZLEN; i++) {
1928 ((char *)pDB->vaddr)[i] = 0;
1930 ptxd->len = ETH_ZLEN;
1933 ptxd->len = skb->len;
1935 ptxd->buff_stat = pDB->dma_addr | TX_DMA_ENABLE;
1938 aup->tx_head = (aup->tx_head + 1) & (NUM_TX_DMA - 1);
1939 dev->trans_start = jiffies;
1944 static inline void update_rx_stats(struct net_device *dev, u32 status)
1946 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1947 struct net_device_stats *ps = &aup->stats;
1950 if (status & RX_MCAST_FRAME)
1953 if (status & RX_ERROR) {
1955 if (status & RX_MISSED_FRAME)
1956 ps->rx_missed_errors++;
1957 if (status & (RX_OVERLEN | RX_OVERLEN | RX_LEN_ERROR))
1958 ps->rx_length_errors++;
1959 if (status & RX_CRC_ERROR)
1960 ps->rx_crc_errors++;
1961 if (status & RX_COLL)
1965 ps->rx_bytes += status & RX_FRAME_LEN_MASK;
1970 * Au1000 receive routine.
1972 static int au1000_rx(struct net_device *dev)
1974 struct au1000_private *aup = (struct au1000_private *) dev->priv;
1975 struct sk_buff *skb;
1976 volatile rx_dma_t *prxd;
1977 u32 buff_stat, status;
1981 if (au1000_debug > 5)
1982 printk("%s: au1000_rx head %d\n", dev->name, aup->rx_head);
1984 prxd = aup->rx_dma_ring[aup->rx_head];
1985 buff_stat = prxd->buff_stat;
1986 while (buff_stat & RX_T_DONE) {
1987 status = prxd->status;
1988 pDB = aup->rx_db_inuse[aup->rx_head];
1989 update_rx_stats(dev, status);
1990 if (!(status & RX_ERROR)) {
1993 frmlen = (status & RX_FRAME_LEN_MASK);
1994 frmlen -= 4; /* Remove FCS */
1995 skb = dev_alloc_skb(frmlen + 2);
1998 "%s: Memory squeeze, dropping packet.\n",
2000 aup->stats.rx_dropped++;
2004 skb_reserve(skb, 2); /* 16 byte IP header align */
2005 eth_copy_and_sum(skb,
2006 (unsigned char *)pDB->vaddr, frmlen, 0);
2007 skb_put(skb, frmlen);
2008 skb->protocol = eth_type_trans(skb, dev);
2009 netif_rx(skb); /* pass the packet to upper layers */
2012 if (au1000_debug > 4) {
2013 if (status & RX_MISSED_FRAME)
2014 printk("rx miss\n");
2015 if (status & RX_WDOG_TIMER)
2016 printk("rx wdog\n");
2017 if (status & RX_RUNT)
2018 printk("rx runt\n");
2019 if (status & RX_OVERLEN)
2020 printk("rx overlen\n");
2021 if (status & RX_COLL)
2022 printk("rx coll\n");
2023 if (status & RX_MII_ERROR)
2024 printk("rx mii error\n");
2025 if (status & RX_CRC_ERROR)
2026 printk("rx crc error\n");
2027 if (status & RX_LEN_ERROR)
2028 printk("rx len error\n");
2029 if (status & RX_U_CNTRL_FRAME)
2030 printk("rx u control frame\n");
2031 if (status & RX_MISSED_FRAME)
2032 printk("rx miss\n");
2035 prxd->buff_stat = (u32)(pDB->dma_addr | RX_DMA_ENABLE);
2036 aup->rx_head = (aup->rx_head + 1) & (NUM_RX_DMA - 1);
2039 /* next descriptor */
2040 prxd = aup->rx_dma_ring[aup->rx_head];
2041 buff_stat = prxd->buff_stat;
2042 dev->last_rx = jiffies;
2049 * Au1000 interrupt service routine.
2051 static irqreturn_t au1000_interrupt(int irq, void *dev_id, struct pt_regs *regs)
2053 struct net_device *dev = (struct net_device *) dev_id;
2056 printk(KERN_ERR "%s: isr: null dev ptr\n", dev->name);
2057 return IRQ_RETVAL(1);
2060 /* Handle RX interrupts first to minimize chance of overrun */
2064 return IRQ_RETVAL(1);
2069 * The Tx ring has been full longer than the watchdog timeout
2070 * value. The transmitter must be hung?
2072 static void au1000_tx_timeout(struct net_device *dev)
2074 printk(KERN_ERR "%s: au1000_tx_timeout: dev=%p\n", dev->name, dev);
2077 dev->trans_start = jiffies;
2078 netif_wake_queue(dev);
2082 static unsigned const ethernet_polynomial = 0x04c11db7U;
2083 static inline u32 ether_crc(int length, unsigned char *data)
2087 while(--length >= 0) {
2088 unsigned char current_octet = *data++;
2090 for (bit = 0; bit < 8; bit++, current_octet >>= 1)
2092 ((crc < 0) ^ (current_octet & 1) ?
2093 ethernet_polynomial : 0);
2098 static void set_rx_mode(struct net_device *dev)
2100 struct au1000_private *aup = (struct au1000_private *) dev->priv;
2102 if (au1000_debug > 4)
2103 printk("%s: set_rx_mode: flags=%x\n", dev->name, dev->flags);
2105 if (dev->flags & IFF_PROMISC) { /* Set promiscuous. */
2106 aup->mac->control |= MAC_PROMISCUOUS;
2107 printk(KERN_INFO "%s: Promiscuous mode enabled.\n", dev->name);
2108 } else if ((dev->flags & IFF_ALLMULTI) ||
2109 dev->mc_count > MULTICAST_FILTER_LIMIT) {
2110 aup->mac->control |= MAC_PASS_ALL_MULTI;
2111 aup->mac->control &= ~MAC_PROMISCUOUS;
2112 printk(KERN_INFO "%s: Pass all multicast\n", dev->name);
2115 struct dev_mc_list *mclist;
2116 u32 mc_filter[2]; /* Multicast hash filter */
2118 mc_filter[1] = mc_filter[0] = 0;
2119 for (i = 0, mclist = dev->mc_list; mclist && i < dev->mc_count;
2120 i++, mclist = mclist->next) {
2121 set_bit(ether_crc(ETH_ALEN, mclist->dmi_addr)>>26,
2124 aup->mac->multi_hash_high = mc_filter[1];
2125 aup->mac->multi_hash_low = mc_filter[0];
2126 aup->mac->control &= ~MAC_PROMISCUOUS;
2127 aup->mac->control |= MAC_HASH_MODE;
2132 static int au1000_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
2134 struct au1000_private *aup = (struct au1000_private *)dev->priv;
2135 u16 *data = (u16 *)&rq->ifr_ifru;
2138 case SIOCDEVPRIVATE: /* Get the address of the PHY in use. */
2140 if (!netif_running(dev)) return -EINVAL;
2141 data[0] = aup->phy_addr;
2142 case SIOCDEVPRIVATE+1: /* Read the specified MII register. */
2144 data[3] = mdio_read(dev, data[0], data[1]);
2146 case SIOCDEVPRIVATE+2: /* Write the specified MII register */
2148 if (!capable(CAP_NET_ADMIN))
2150 mdio_write(dev, data[0], data[1],data[2]);
2159 static int au1000_set_config(struct net_device *dev, struct ifmap *map)
2161 struct au1000_private *aup = (struct au1000_private *) dev->priv;
2164 if (au1000_debug > 4) {
2165 printk("%s: set_config called: dev->if_port %d map->port %x\n",
2166 dev->name, dev->if_port, map->port);
2170 case IF_PORT_UNKNOWN: /* use auto here */
2171 printk(KERN_INFO "%s: config phy for aneg\n",
2173 dev->if_port = map->port;
2174 /* Link Down: the timer will bring it up */
2175 netif_carrier_off(dev);
2177 /* read current control */
2178 control = mdio_read(dev, aup->phy_addr, MII_CONTROL);
2179 control &= ~(MII_CNTL_FDX | MII_CNTL_F100);
2181 /* enable auto negotiation and reset the negotiation */
2182 mdio_write(dev, aup->phy_addr, MII_CONTROL,
2183 control | MII_CNTL_AUTO |
2188 case IF_PORT_10BASET: /* 10BaseT */
2189 printk(KERN_INFO "%s: config phy for 10BaseT\n",
2191 dev->if_port = map->port;
2193 /* Link Down: the timer will bring it up */
2194 netif_carrier_off(dev);
2196 /* set Speed to 10Mbps, Half Duplex */
2197 control = mdio_read(dev, aup->phy_addr, MII_CONTROL);
2198 control &= ~(MII_CNTL_F100 | MII_CNTL_AUTO |
2201 /* disable auto negotiation and force 10M/HD mode*/
2202 mdio_write(dev, aup->phy_addr, MII_CONTROL, control);
2205 case IF_PORT_100BASET: /* 100BaseT */
2206 case IF_PORT_100BASETX: /* 100BaseTx */
2207 printk(KERN_INFO "%s: config phy for 100BaseTX\n",
2209 dev->if_port = map->port;
2211 /* Link Down: the timer will bring it up */
2212 netif_carrier_off(dev);
2214 /* set Speed to 100Mbps, Half Duplex */
2215 /* disable auto negotiation and enable 100MBit Mode */
2216 control = mdio_read(dev, aup->phy_addr, MII_CONTROL);
2217 control &= ~(MII_CNTL_AUTO | MII_CNTL_FDX);
2218 control |= MII_CNTL_F100;
2219 mdio_write(dev, aup->phy_addr, MII_CONTROL, control);
2222 case IF_PORT_100BASEFX: /* 100BaseFx */
2223 printk(KERN_INFO "%s: config phy for 100BaseFX\n",
2225 dev->if_port = map->port;
2227 /* Link Down: the timer will bring it up */
2228 netif_carrier_off(dev);
2230 /* set Speed to 100Mbps, Full Duplex */
2231 /* disable auto negotiation and enable 100MBit Mode */
2232 control = mdio_read(dev, aup->phy_addr, MII_CONTROL);
2233 control &= ~MII_CNTL_AUTO;
2234 control |= MII_CNTL_F100 | MII_CNTL_FDX;
2235 mdio_write(dev, aup->phy_addr, MII_CONTROL, control);
2237 case IF_PORT_10BASE2: /* 10Base2 */
2238 case IF_PORT_AUI: /* AUI */
2239 /* These Modes are not supported (are they?)*/
2240 printk(KERN_ERR "%s: 10Base2/AUI not supported",
2246 printk(KERN_ERR "%s: Invalid media selected",
2253 static struct net_device_stats *au1000_get_stats(struct net_device *dev)
2255 struct au1000_private *aup = (struct au1000_private *) dev->priv;
2257 if (au1000_debug > 4)
2258 printk("%s: au1000_get_stats: dev=%p\n", dev->name, dev);
2260 if (netif_device_present(dev)) {
2266 module_init(au1000_init_module);
2267 module_exit(au1000_cleanup_module);