1 /*
2 	drivers/net/tulip/21142.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.{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/pci.h>
18 #include "tulip.h"
19 #include <linux/delay.h>
20 
21 
22 static u16 t21142_csr13[] = { 0x0001, 0x0009, 0x0009, 0x0000, 0x0001, };
23 u16 t21142_csr14[] =	    { 0xFFFF, 0x0705, 0x0705, 0x0000, 0x7F3D, };
24 static u16 t21142_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
25 
26 
27 /* Handle the 21143 uniquely: do autoselect with NWay, not the EEPROM list
28    of available transceivers.  */
t21142_timer(unsigned long data)29 void t21142_timer(unsigned long data)
30 {
31 	struct net_device *dev = (struct net_device *)data;
32 	struct tulip_private *tp = (struct tulip_private *)dev->priv;
33 	long ioaddr = dev->base_addr;
34 	int csr12 = inl(ioaddr + CSR12);
35 	int next_tick = 60*HZ;
36 	int new_csr6 = 0;
37 
38 	if (tulip_debug > 2)
39 		printk(KERN_INFO"%s: 21143 negotiation status %8.8x, %s.\n",
40 			   dev->name, csr12, medianame[dev->if_port]);
41 	if (tulip_media_cap[dev->if_port] & MediaIsMII) {
42 		tulip_check_duplex(dev);
43 		next_tick = 60*HZ;
44 	} else if (tp->nwayset) {
45 		/* Don't screw up a negotiated session! */
46 		if (tulip_debug > 1)
47 			printk(KERN_INFO"%s: Using NWay-set %s media, csr12 %8.8x.\n",
48 				   dev->name, medianame[dev->if_port], csr12);
49 	} else if (tp->medialock) {
50 			;
51 	} else if (dev->if_port == 3) {
52 		if (csr12 & 2) {	/* No 100mbps link beat, revert to 10mbps. */
53 			if (tulip_debug > 1)
54 				printk(KERN_INFO"%s: No 21143 100baseTx link beat, %8.8x, "
55 					   "trying NWay.\n", dev->name, csr12);
56 			t21142_start_nway(dev);
57 			next_tick = 3*HZ;
58 		}
59 	} else if ((csr12 & 0x7000) != 0x5000) {
60 		/* Negotiation failed.  Search media types. */
61 		if (tulip_debug > 1)
62 			printk(KERN_INFO"%s: 21143 negotiation failed, status %8.8x.\n",
63 				   dev->name, csr12);
64 		if (!(csr12 & 4)) {		/* 10mbps link beat good. */
65 			new_csr6 = 0x82420000;
66 			dev->if_port = 0;
67 			outl(0, ioaddr + CSR13);
68 			outl(0x0003FFFF, ioaddr + CSR14);
69 			outw(t21142_csr15[dev->if_port], ioaddr + CSR15);
70 			outl(t21142_csr13[dev->if_port], ioaddr + CSR13);
71 		} else {
72 			/* Select 100mbps port to check for link beat. */
73 			new_csr6 = 0x83860000;
74 			dev->if_port = 3;
75 			outl(0, ioaddr + CSR13);
76 			outl(0x0003FF7F, ioaddr + CSR14);
77 			outw(8, ioaddr + CSR15);
78 			outl(1, ioaddr + CSR13);
79 		}
80 		if (tulip_debug > 1)
81 			printk(KERN_INFO"%s: Testing new 21143 media %s.\n",
82 				   dev->name, medianame[dev->if_port]);
83 		if (new_csr6 != (tp->csr6 & ~0x00D5)) {
84 			tp->csr6 &= 0x00D5;
85 			tp->csr6 |= new_csr6;
86 			outl(0x0301, ioaddr + CSR12);
87 			tulip_restart_rxtx(tp);
88 		}
89 		next_tick = 3*HZ;
90 	}
91 
92 	/* mod_timer synchronizes us with potential add_timer calls
93 	 * from interrupts.
94 	 */
95 	mod_timer(&tp->timer, RUN_AT(next_tick));
96 }
97 
98 
t21142_start_nway(struct net_device * dev)99 void t21142_start_nway(struct net_device *dev)
100 {
101 	struct tulip_private *tp = (struct tulip_private *)dev->priv;
102 	long ioaddr = dev->base_addr;
103 	int csr14 = ((tp->sym_advertise & 0x0780) << 9)  |
104 		((tp->sym_advertise & 0x0020) << 1) | 0xffbf;
105 
106 	dev->if_port = 0;
107 	tp->nway = tp->mediasense = 1;
108 	tp->nwayset = tp->lpar = 0;
109 	if (tulip_debug > 1)
110 		printk(KERN_DEBUG "%s: Restarting 21143 autonegotiation, csr14=%8.8x.\n",
111 			   dev->name, csr14);
112 	outl(0x0001, ioaddr + CSR13);
113 	udelay(100);
114 	outl(csr14, ioaddr + CSR14);
115 	tp->csr6 = 0x82420000 | (tp->sym_advertise & 0x0040 ? FullDuplex : 0);
116 	outl(tp->csr6, ioaddr + CSR6);
117 	if (tp->mtable  &&  tp->mtable->csr15dir) {
118 		outl(tp->mtable->csr15dir, ioaddr + CSR15);
119 		outl(tp->mtable->csr15val, ioaddr + CSR15);
120 	} else
121 		outw(0x0008, ioaddr + CSR15);
122 	outl(0x1301, ioaddr + CSR12); 		/* Trigger NWAY. */
123 }
124 
125 
126 
t21142_lnk_change(struct net_device * dev,int csr5)127 void t21142_lnk_change(struct net_device *dev, int csr5)
128 {
129 	struct tulip_private *tp = (struct tulip_private *)dev->priv;
130 	long ioaddr = dev->base_addr;
131 	int csr12 = inl(ioaddr + CSR12);
132 
133 	if (tulip_debug > 1)
134 		printk(KERN_INFO"%s: 21143 link status interrupt %8.8x, CSR5 %x, "
135 			   "%8.8x.\n", dev->name, csr12, csr5, inl(ioaddr + CSR14));
136 
137 	/* If NWay finished and we have a negotiated partner capability. */
138 	if (tp->nway  &&  !tp->nwayset  &&  (csr12 & 0x7000) == 0x5000) {
139 		int setup_done = 0;
140 		int negotiated = tp->sym_advertise & (csr12 >> 16);
141 		tp->lpar = csr12 >> 16;
142 		tp->nwayset = 1;
143 		if (negotiated & 0x0100)		dev->if_port = 5;
144 		else if (negotiated & 0x0080)	dev->if_port = 3;
145 		else if (negotiated & 0x0040)	dev->if_port = 4;
146 		else if (negotiated & 0x0020)	dev->if_port = 0;
147 		else {
148 			tp->nwayset = 0;
149 			if ((csr12 & 2) == 0  &&  (tp->sym_advertise & 0x0180))
150 				dev->if_port = 3;
151 		}
152 		tp->full_duplex = (tulip_media_cap[dev->if_port] & MediaAlwaysFD) ? 1:0;
153 
154 		if (tulip_debug > 1) {
155 			if (tp->nwayset)
156 				printk(KERN_INFO "%s: Switching to %s based on link "
157 					   "negotiation %4.4x & %4.4x = %4.4x.\n",
158 					   dev->name, medianame[dev->if_port], tp->sym_advertise,
159 					   tp->lpar, negotiated);
160 			else
161 				printk(KERN_INFO "%s: Autonegotiation failed, using %s,"
162 					   " link beat status %4.4x.\n",
163 					   dev->name, medianame[dev->if_port], csr12);
164 		}
165 
166 		if (tp->mtable) {
167 			int i;
168 			for (i = 0; i < tp->mtable->leafcount; i++)
169 				if (tp->mtable->mleaf[i].media == dev->if_port) {
170 					int startup = ! ((tp->chip_id == DC21143 && tp->revision == 65));
171 					tp->cur_index = i;
172 					tulip_select_media(dev, startup);
173 					setup_done = 1;
174 					break;
175 				}
176 		}
177 		if ( ! setup_done) {
178 			tp->csr6 = (dev->if_port & 1 ? 0x838E0000 : 0x82420000) | (tp->csr6 & 0x20ff);
179 			if (tp->full_duplex)
180 				tp->csr6 |= 0x0200;
181 			outl(1, ioaddr + CSR13);
182 		}
183 #if 0							/* Restart shouldn't be needed. */
184 		outl(tp->csr6 | RxOn, ioaddr + CSR6);
185 		if (tulip_debug > 2)
186 			printk(KERN_DEBUG "%s:  Restarting Tx and Rx, CSR5 is %8.8x.\n",
187 				   dev->name, inl(ioaddr + CSR5));
188 #endif
189 		tulip_start_rxtx(tp);
190 		if (tulip_debug > 2)
191 			printk(KERN_DEBUG "%s:  Setting CSR6 %8.8x/%x CSR12 %8.8x.\n",
192 				   dev->name, tp->csr6, inl(ioaddr + CSR6),
193 				   inl(ioaddr + CSR12));
194 	} else if ((tp->nwayset  &&  (csr5 & 0x08000000)
195 				&& (dev->if_port == 3  ||  dev->if_port == 5)
196 				&& (csr12 & 2) == 2) ||
197 			   (tp->nway && (csr5 & (TPLnkFail)))) {
198 		/* Link blew? Maybe restart NWay. */
199 		del_timer_sync(&tp->timer);
200 		t21142_start_nway(dev);
201 		tp->timer.expires = RUN_AT(3*HZ);
202 		add_timer(&tp->timer);
203 	} else if (dev->if_port == 3  ||  dev->if_port == 5) {
204 		if (tulip_debug > 1)
205 			printk(KERN_INFO"%s: 21143 %s link beat %s.\n",
206 				   dev->name, medianame[dev->if_port],
207 				   (csr12 & 2) ? "failed" : "good");
208 		if ((csr12 & 2)  &&  ! tp->medialock) {
209 			del_timer_sync(&tp->timer);
210 			t21142_start_nway(dev);
211 			tp->timer.expires = RUN_AT(3*HZ);
212 			add_timer(&tp->timer);
213 		} else if (dev->if_port == 5)
214 			outl(inl(ioaddr + CSR14) & ~0x080, ioaddr + CSR14);
215 	} else if (dev->if_port == 0  ||  dev->if_port == 4) {
216 		if ((csr12 & 4) == 0)
217 			printk(KERN_INFO"%s: 21143 10baseT link beat good.\n",
218 				   dev->name);
219 	} else if (!(csr12 & 4)) {		/* 10mbps link beat good. */
220 		if (tulip_debug)
221 			printk(KERN_INFO"%s: 21143 10mbps sensed media.\n",
222 				   dev->name);
223 		dev->if_port = 0;
224 	} else if (tp->nwayset) {
225 		if (tulip_debug)
226 			printk(KERN_INFO"%s: 21143 using NWay-set %s, csr6 %8.8x.\n",
227 				   dev->name, medianame[dev->if_port], tp->csr6);
228 	} else {		/* 100mbps link beat good. */
229 		if (tulip_debug)
230 			printk(KERN_INFO"%s: 21143 100baseTx sensed media.\n",
231 				   dev->name);
232 		dev->if_port = 3;
233 		tp->csr6 = 0x838E0000 | (tp->csr6 & 0x20ff);
234 		outl(0x0003FF7F, ioaddr + CSR14);
235 		outl(0x0301, ioaddr + CSR12);
236 		tulip_restart_rxtx(tp);
237 	}
238 }
239 
240 
241