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