1/*
2	drivers/net/ethernet/dec/tulip/pnic2.c
3
4	Copyright 2000,2001  The Linux Kernel Team
5	Written/copyright 1994-2001 by Donald Becker.
6        Modified to hep support PNIC_II by Kevin B. Hendricks
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 submit bugs to http://bugzilla.kernel.org/ .
12*/
13
14
15/* Understanding the PNIC_II - everything is this file is based
16 * on the PNIC_II_PDF datasheet which is sorely lacking in detail
17 *
18 * As I understand things, here are the registers and bits that
19 * explain the masks and constants used in this file that are
20 * either different from the 21142/3 or important for basic operation.
21 *
22 *
23 * CSR 6  (mask = 0xfe3bd1fd of bits not to change)
24 * -----
25 * Bit 24    - SCR
26 * Bit 23    - PCS
27 * Bit 22    - TTM (Trasmit Threshold Mode)
28 * Bit 18    - Port Select
29 * Bit 13    - Start - 1, Stop - 0 Transmissions
30 * Bit 11:10 - Loop Back Operation Mode
31 * Bit 9     - Full Duplex mode (Advertise 10BaseT-FD is CSR14<7> is set)
32 * Bit 1     - Start - 1, Stop - 0 Receive
33 *
34 *
35 * CSR 14  (mask = 0xfff0ee39 of bits not to change)
36 * ------
37 * Bit 19    - PAUSE-Pause
38 * Bit 18    - Advertise T4
39 * Bit 17    - Advertise 100baseTx-FD
40 * Bit 16    - Advertise 100baseTx-HD
41 * Bit 12    - LTE - Link Test Enable
42 * Bit 7     - ANE - Auto Negotiate Enable
43 * Bit 6     - HDE - Advertise 10baseT-HD
44 * Bit 2     - Reset to Power down - kept as 1 for normal operation
45 * Bit 1     -  Loop Back enable for 10baseT MCC
46 *
47 *
48 * CSR 12
49 * ------
50 * Bit 25    - Partner can do T4
51 * Bit 24    - Partner can do 100baseTx-FD
52 * Bit 23    - Partner can do 100baseTx-HD
53 * Bit 22    - Partner can do 10baseT-FD
54 * Bit 21    - Partner can do 10baseT-HD
55 * Bit 15    - LPN is 1 if all above bits are valid other wise 0
56 * Bit 14:12 - autonegotiation state (write 001 to start autonegotiate)
57 * Bit 3     - Autopolarity state
58 * Bit 2     - LS10B - link state of 10baseT 0 - good, 1 - failed
59 * Bit 1     - LS100B - link state of 100baseT 0 - good, 1 - failed
60 *
61 *
62 * Data Port Selection Info
63 *-------------------------
64 *
65 * CSR14<7>   CSR6<18>    CSR6<22>    CSR6<23>    CSR6<24>   MODE/PORT
66 *   1           0           0 (X)       0 (X)       1        NWAY
67 *   0           0           1           0 (X)       0        10baseT
68 *   0           1           0           1           1 (X)    100baseT
69 *
70 *
71 */
72
73
74
75#include "tulip.h"
76#include <linux/delay.h>
77
78
79void pnic2_timer(unsigned long data)
80{
81	struct net_device *dev = (struct net_device *)data;
82	struct tulip_private *tp = netdev_priv(dev);
83	void __iomem *ioaddr = tp->base_addr;
84	int next_tick = 60*HZ;
85
86	if (tulip_debug > 3)
87		dev_info(&dev->dev, "PNIC2 negotiation status %08x\n",
88			 ioread32(ioaddr + CSR12));
89
90	if (next_tick) {
91		mod_timer(&tp->timer, RUN_AT(next_tick));
92	}
93}
94
95
96void pnic2_start_nway(struct net_device *dev)
97{
98	struct tulip_private *tp = netdev_priv(dev);
99	void __iomem *ioaddr = tp->base_addr;
100        int csr14;
101        int csr12;
102
103        /* set up what to advertise during the negotiation */
104
105        /* load in csr14  and mask off bits not to touch
106         * comment at top of file explains mask value
107         */
108	csr14 = (ioread32(ioaddr + CSR14) & 0xfff0ee39);
109
110        /* bit 17 - advetise 100baseTx-FD */
111        if (tp->sym_advertise & 0x0100) csr14 |= 0x00020000;
112
113        /* bit 16 - advertise 100baseTx-HD */
114        if (tp->sym_advertise & 0x0080) csr14 |= 0x00010000;
115
116        /* bit 6 - advertise 10baseT-HD */
117        if (tp->sym_advertise & 0x0020) csr14 |= 0x00000040;
118
119        /* Now set bit 12 Link Test Enable, Bit 7 Autonegotiation Enable
120         * and bit 0 Don't PowerDown 10baseT
121         */
122        csr14 |= 0x00001184;
123
124	if (tulip_debug > 1)
125		netdev_dbg(dev, "Restarting PNIC2 autonegotiation, csr14=%08x\n",
126			   csr14);
127
128        /* tell pnic2_lnk_change we are doing an nway negotiation */
129	dev->if_port = 0;
130	tp->nway = tp->mediasense = 1;
131	tp->nwayset = tp->lpar = 0;
132
133        /* now we have to set up csr6 for NWAY state */
134
135	tp->csr6 = ioread32(ioaddr + CSR6);
136	if (tulip_debug > 1)
137		netdev_dbg(dev, "On Entry to Nway, csr6=%08x\n", tp->csr6);
138
139        /* mask off any bits not to touch
140         * comment at top of file explains mask value
141         */
142	tp->csr6 = tp->csr6 & 0xfe3bd1fd;
143
144        /* don't forget that bit 9 is also used for advertising */
145        /* advertise 10baseT-FD for the negotiation (bit 9) */
146        if (tp->sym_advertise & 0x0040) tp->csr6 |= 0x00000200;
147
148        /* set bit 24 for nway negotiation mode ...
149         * see Data Port Selection comment at top of file
150         * and "Stop" - reset both Transmit (bit 13) and Receive (bit 1)
151         */
152        tp->csr6 |= 0x01000000;
153	iowrite32(csr14, ioaddr + CSR14);
154	iowrite32(tp->csr6, ioaddr + CSR6);
155        udelay(100);
156
157        /* all set up so now force the negotiation to begin */
158
159        /* read in current values and mask off all but the
160	 * Autonegotiation bits 14:12.  Writing a 001 to those bits
161         * should start the autonegotiation
162         */
163        csr12 = (ioread32(ioaddr + CSR12) & 0xffff8fff);
164        csr12 |= 0x1000;
165	iowrite32(csr12, ioaddr + CSR12);
166}
167
168
169
170void pnic2_lnk_change(struct net_device *dev, int csr5)
171{
172	struct tulip_private *tp = netdev_priv(dev);
173	void __iomem *ioaddr = tp->base_addr;
174        int csr14;
175
176        /* read the staus register to find out what is up */
177	int csr12 = ioread32(ioaddr + CSR12);
178
179	if (tulip_debug > 1)
180		dev_info(&dev->dev,
181			 "PNIC2 link status interrupt %08x,  CSR5 %x, %08x\n",
182			 csr12, csr5, ioread32(ioaddr + CSR14));
183
184	/* If NWay finished and we have a negotiated partner capability.
185         * check bits 14:12 for bit pattern 101 - all is good
186         */
187	if (tp->nway  &&  !tp->nwayset) {
188
189	        /* we did an auto negotiation */
190
191                if ((csr12 & 0x7000) == 0x5000) {
192
193	               /* negotiation ended successfully */
194
195	               /* get the link partners reply and mask out all but
196                        * bits 24-21 which show the partners capabilities
197                        * and match those to what we advertised
198                        *
199                        * then begin to interpret the results of the negotiation.
200                        * Always go in this order : (we are ignoring T4 for now)
201                        *     100baseTx-FD, 100baseTx-HD, 10baseT-FD, 10baseT-HD
202                        */
203
204		        int negotiated = ((csr12 >> 16) & 0x01E0) & tp->sym_advertise;
205		        tp->lpar = (csr12 >> 16);
206		        tp->nwayset = 1;
207
208                        if (negotiated & 0x0100)        dev->if_port = 5;
209		        else if (negotiated & 0x0080)	dev->if_port = 3;
210		        else if (negotiated & 0x0040)	dev->if_port = 4;
211			else if (negotiated & 0x0020)	dev->if_port = 0;
212			else {
213			     if (tulip_debug > 1)
214				     dev_info(&dev->dev,
215					      "funny autonegotiate result csr12 %08x advertising %04x\n",
216					      csr12, tp->sym_advertise);
217			     tp->nwayset = 0;
218			     /* so check  if 100baseTx link state is okay */
219			     if ((csr12 & 2) == 0  &&  (tp->sym_advertise & 0x0180))
220			       dev->if_port = 3;
221			}
222
223			/* now record the duplex that was negotiated */
224			tp->full_duplex = 0;
225			if ((dev->if_port == 4) || (dev->if_port == 5))
226			       tp->full_duplex = 1;
227
228			if (tulip_debug > 1) {
229			       if (tp->nwayset)
230				       dev_info(&dev->dev,
231						"Switching to %s based on link negotiation %04x & %04x = %04x\n",
232						medianame[dev->if_port],
233						tp->sym_advertise, tp->lpar,
234						negotiated);
235			}
236
237                        /* remember to turn off bit 7 - autonegotiate
238                         * enable so we can properly end nway mode and
239                         * set duplex (ie. use csr6<9> again)
240                         */
241	                csr14 = (ioread32(ioaddr + CSR14) & 0xffffff7f);
242                        iowrite32(csr14,ioaddr + CSR14);
243
244
245                        /* now set the data port and operating mode
246			 * (see the Data Port Selection comments at
247			 * the top of the file
248			 */
249
250			/* get current csr6 and mask off bits not to touch */
251			/* see comment at top of file */
252
253			tp->csr6 = (ioread32(ioaddr + CSR6) & 0xfe3bd1fd);
254
255			/* so if using if_port 3 or 5 then select the 100baseT
256			 * port else select the 10baseT port.
257			 * See the Data Port Selection table at the top
258			 * of the file which was taken from the PNIC_II.PDF
259			 * datasheet
260			 */
261			if (dev->if_port & 1) tp->csr6 |= 0x01840000;
262			else tp->csr6 |= 0x00400000;
263
264			/* now set the full duplex bit appropriately */
265			if (tp->full_duplex) tp->csr6 |= 0x00000200;
266
267			iowrite32(1, ioaddr + CSR13);
268
269			if (tulip_debug > 2)
270				netdev_dbg(dev, "Setting CSR6 %08x/%x CSR12 %08x\n",
271					   tp->csr6,
272					   ioread32(ioaddr + CSR6),
273					   ioread32(ioaddr + CSR12));
274
275			/* now the following actually writes out the
276			 * new csr6 values
277			 */
278			tulip_start_rxtx(tp);
279
280                        return;
281
282	        } else {
283	                dev_info(&dev->dev,
284				 "Autonegotiation failed, using %s, link beat status %04x\n",
285				 medianame[dev->if_port], csr12);
286
287                        /* remember to turn off bit 7 - autonegotiate
288                         * enable so we don't forget
289                         */
290	                csr14 = (ioread32(ioaddr + CSR14) & 0xffffff7f);
291                        iowrite32(csr14,ioaddr + CSR14);
292
293                        /* what should we do when autonegotiate fails?
294                         * should we try again or default to baseline
295                         * case.  I just don't know.
296                         *
297                         * for now default to some baseline case
298                         */
299
300	                 dev->if_port = 0;
301                         tp->nway = 0;
302                         tp->nwayset = 1;
303
304                         /* set to 10baseTx-HD - see Data Port Selection
305                          * comment given at the top of the file
306                          */
307	                 tp->csr6 = (ioread32(ioaddr + CSR6) & 0xfe3bd1fd);
308                         tp->csr6 |= 0x00400000;
309
310	                 tulip_restart_rxtx(tp);
311
312                         return;
313
314		}
315	}
316
317	if ((tp->nwayset  &&  (csr5 & 0x08000000) &&
318	     (dev->if_port == 3  ||  dev->if_port == 5) &&
319	     (csr12 & 2) == 2) || (tp->nway && (csr5 & (TPLnkFail)))) {
320
321		/* Link blew? Maybe restart NWay. */
322
323		if (tulip_debug > 2)
324			netdev_dbg(dev, "Ugh! Link blew?\n");
325
326		del_timer_sync(&tp->timer);
327		pnic2_start_nway(dev);
328		tp->timer.expires = RUN_AT(3*HZ);
329		add_timer(&tp->timer);
330
331                return;
332	}
333
334
335        if (dev->if_port == 3  ||  dev->if_port == 5) {
336
337	        /* we are at 100mb and a potential link change occurred */
338
339		if (tulip_debug > 1)
340			dev_info(&dev->dev, "PNIC2 %s link beat %s\n",
341				 medianame[dev->if_port],
342				 (csr12 & 2) ? "failed" : "good");
343
344                /* check 100 link beat */
345
346                tp->nway = 0;
347                tp->nwayset = 1;
348
349                /* if failed then try doing an nway to get in sync */
350		if ((csr12 & 2)  &&  ! tp->medialock) {
351			del_timer_sync(&tp->timer);
352			pnic2_start_nway(dev);
353			tp->timer.expires = RUN_AT(3*HZ);
354       			add_timer(&tp->timer);
355                }
356
357                return;
358        }
359
360	if (dev->if_port == 0  ||  dev->if_port == 4) {
361
362	        /* we are at 10mb and a potential link change occurred */
363
364		if (tulip_debug > 1)
365			dev_info(&dev->dev, "PNIC2 %s link beat %s\n",
366				 medianame[dev->if_port],
367				 (csr12 & 4) ? "failed" : "good");
368
369
370                tp->nway = 0;
371                tp->nwayset = 1;
372
373                /* if failed, try doing an nway to get in sync */
374		if ((csr12 & 4)  &&  ! tp->medialock) {
375			del_timer_sync(&tp->timer);
376			pnic2_start_nway(dev);
377			tp->timer.expires = RUN_AT(3*HZ);
378       			add_timer(&tp->timer);
379                }
380
381                return;
382        }
383
384
385	if (tulip_debug > 1)
386		dev_info(&dev->dev, "PNIC2 Link Change Default?\n");
387
388        /* if all else fails default to trying 10baseT-HD */
389	dev->if_port = 0;
390
391        /* make sure autonegotiate enable is off */
392	csr14 = (ioread32(ioaddr + CSR14) & 0xffffff7f);
393        iowrite32(csr14,ioaddr + CSR14);
394
395        /* set to 10baseTx-HD - see Data Port Selection
396         * comment given at the top of the file
397         */
398	tp->csr6 = (ioread32(ioaddr + CSR6) & 0xfe3bd1fd);
399        tp->csr6 |= 0x00400000;
400
401	tulip_restart_rxtx(tp);
402}
403
404