Merge git://git.kernel.org/pub/scm/linux/kernel/git/paulus/powerpc
[linux-2.6] / drivers / net / tulip / pnic.c
1 /*
2         drivers/net/tulip/pnic.c
3
4         Maintained by Jeff Garzik <jgarzik@pobox.com>
5         Copyright 2000,2001  The Linux Kernel Team
6         Written/copyright 1994-2001 by Donald Becker.
7
8         This software may be used and distributed according to the terms
9         of the GNU General Public License, incorporated herein by reference.
10
11         Please refer to Documentation/DocBook/tulip-user.{pdf,ps,html}
12         for more information on this driver, or visit the project
13         Web page at http://sourceforge.net/projects/tulip/
14
15 */
16
17 #include <linux/kernel.h>
18 #include <linux/pci.h>
19 #include <linux/jiffies.h>
20 #include "tulip.h"
21
22
23 void pnic_do_nway(struct net_device *dev)
24 {
25         struct tulip_private *tp = netdev_priv(dev);
26         void __iomem *ioaddr = tp->base_addr;
27         u32 phy_reg = ioread32(ioaddr + 0xB8);
28         u32 new_csr6 = tp->csr6 & ~0x40C40200;
29
30         if (phy_reg & 0x78000000) { /* Ignore baseT4 */
31                 if (phy_reg & 0x20000000)               dev->if_port = 5;
32                 else if (phy_reg & 0x40000000)  dev->if_port = 3;
33                 else if (phy_reg & 0x10000000)  dev->if_port = 4;
34                 else if (phy_reg & 0x08000000)  dev->if_port = 0;
35                 tp->nwayset = 1;
36                 new_csr6 = (dev->if_port & 1) ? 0x01860000 : 0x00420000;
37                 iowrite32(0x32 | (dev->if_port & 1), ioaddr + CSR12);
38                 if (dev->if_port & 1)
39                         iowrite32(0x1F868, ioaddr + 0xB8);
40                 if (phy_reg & 0x30000000) {
41                         tp->full_duplex = 1;
42                         new_csr6 |= 0x00000200;
43                 }
44                 if (tulip_debug > 1)
45                         printk(KERN_DEBUG "%s: PNIC autonegotiated status %8.8x, %s.\n",
46                                    dev->name, phy_reg, medianame[dev->if_port]);
47                 if (tp->csr6 != new_csr6) {
48                         tp->csr6 = new_csr6;
49                         /* Restart Tx */
50                         tulip_restart_rxtx(tp);
51                         dev->trans_start = jiffies;
52                 }
53         }
54 }
55
56 void pnic_lnk_change(struct net_device *dev, int csr5)
57 {
58         struct tulip_private *tp = netdev_priv(dev);
59         void __iomem *ioaddr = tp->base_addr;
60         int phy_reg = ioread32(ioaddr + 0xB8);
61
62         if (tulip_debug > 1)
63                 printk(KERN_DEBUG "%s: PNIC link changed state %8.8x, CSR5 %8.8x.\n",
64                            dev->name, phy_reg, csr5);
65         if (ioread32(ioaddr + CSR5) & TPLnkFail) {
66                 iowrite32((ioread32(ioaddr + CSR7) & ~TPLnkFail) | TPLnkPass, ioaddr + CSR7);
67                 /* If we use an external MII, then we mustn't use the
68                  * internal negotiation.
69                  */
70                 if (tulip_media_cap[dev->if_port] & MediaIsMII)
71                         return;
72                 if (! tp->nwayset  ||  time_after(jiffies, dev->trans_start + 1*HZ)) {
73                         tp->csr6 = 0x00420000 | (tp->csr6 & 0x0000fdff);
74                         iowrite32(tp->csr6, ioaddr + CSR6);
75                         iowrite32(0x30, ioaddr + CSR12);
76                         iowrite32(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
77                         dev->trans_start = jiffies;
78                 }
79         } else if (ioread32(ioaddr + CSR5) & TPLnkPass) {
80                 if (tulip_media_cap[dev->if_port] & MediaIsMII) {
81                         spin_lock(&tp->lock);
82                         tulip_check_duplex(dev);
83                         spin_unlock(&tp->lock);
84                 } else {
85                         pnic_do_nway(dev);
86                 }
87                 iowrite32((ioread32(ioaddr + CSR7) & ~TPLnkPass) | TPLnkFail, ioaddr + CSR7);
88         }
89 }
90
91 void pnic_timer(unsigned long data)
92 {
93         struct net_device *dev = (struct net_device *)data;
94         struct tulip_private *tp = netdev_priv(dev);
95         void __iomem *ioaddr = tp->base_addr;
96         int next_tick = 60*HZ;
97
98         if(!ioread32(ioaddr + CSR7)) {
99                 /* the timer was called due to a work overflow
100                  * in the interrupt handler. Skip the connection
101                  * checks, the nic is definitively speaking with
102                  * his link partner.
103                  */
104                 goto too_good_connection;
105         }
106
107         if (tulip_media_cap[dev->if_port] & MediaIsMII) {
108                 spin_lock_irq(&tp->lock);
109                 if (tulip_check_duplex(dev) > 0)
110                         next_tick = 3*HZ;
111                 spin_unlock_irq(&tp->lock);
112         } else {
113                 int csr12 = ioread32(ioaddr + CSR12);
114                 int new_csr6 = tp->csr6 & ~0x40C40200;
115                 int phy_reg = ioread32(ioaddr + 0xB8);
116                 int csr5 = ioread32(ioaddr + CSR5);
117
118                 if (tulip_debug > 1)
119                         printk(KERN_DEBUG "%s: PNIC timer PHY status %8.8x, %s "
120                                    "CSR5 %8.8x.\n",
121                                    dev->name, phy_reg, medianame[dev->if_port], csr5);
122                 if (phy_reg & 0x04000000) {     /* Remote link fault */
123                         iowrite32(0x0201F078, ioaddr + 0xB8);
124                         next_tick = 1*HZ;
125                         tp->nwayset = 0;
126                 } else if (phy_reg & 0x78000000) { /* Ignore baseT4 */
127                         pnic_do_nway(dev);
128                         next_tick = 60*HZ;
129                 } else if (csr5 & TPLnkFail) { /* 100baseTx link beat */
130                         if (tulip_debug > 1)
131                                 printk(KERN_DEBUG "%s: %s link beat failed, CSR12 %4.4x, "
132                                            "CSR5 %8.8x, PHY %3.3x.\n",
133                                            dev->name, medianame[dev->if_port], csr12,
134                                            ioread32(ioaddr + CSR5), ioread32(ioaddr + 0xB8));
135                         next_tick = 3*HZ;
136                         if (tp->medialock) {
137                         } else if (tp->nwayset  &&  (dev->if_port & 1)) {
138                                 next_tick = 1*HZ;
139                         } else if (dev->if_port == 0) {
140                                 dev->if_port = 3;
141                                 iowrite32(0x33, ioaddr + CSR12);
142                                 new_csr6 = 0x01860000;
143                                 iowrite32(0x1F868, ioaddr + 0xB8);
144                         } else {
145                                 dev->if_port = 0;
146                                 iowrite32(0x32, ioaddr + CSR12);
147                                 new_csr6 = 0x00420000;
148                                 iowrite32(0x1F078, ioaddr + 0xB8);
149                         }
150                         if (tp->csr6 != new_csr6) {
151                                 tp->csr6 = new_csr6;
152                                 /* Restart Tx */
153                                 tulip_restart_rxtx(tp);
154                                 dev->trans_start = jiffies;
155                                 if (tulip_debug > 1)
156                                         printk(KERN_INFO "%s: Changing PNIC configuration to %s "
157                                                    "%s-duplex, CSR6 %8.8x.\n",
158                                                    dev->name, medianame[dev->if_port],
159                                                    tp->full_duplex ? "full" : "half", new_csr6);
160                         }
161                 }
162         }
163 too_good_connection:
164         mod_timer(&tp->timer, RUN_AT(next_tick));
165         if(!ioread32(ioaddr + CSR7)) {
166                 if (tulip_debug > 1)
167                         printk(KERN_INFO "%s: sw timer wakeup.\n", dev->name);
168                 disable_irq(dev->irq);
169                 tulip_refill_rx(dev);
170                 enable_irq(dev->irq);
171                 iowrite32(tulip_tbl[tp->chip_id].valid_intrs, ioaddr + CSR7);
172         }
173 }