MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / tulip / media.c
blob35f2989fac6824b928cb61ebdade1875a615d39f
1 /*
2 drivers/net/tulip/media.c
4 Maintained by Jeff Garzik <jgarzik@pobox.com>
5 Copyright 2000,2001 The Linux Kernel Team
6 Written/copyright 1994-2001 by Donald Becker.
8 This software may be used and distributed according to the terms
9 of the GNU General Public License, incorporated herein by reference.
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/
17 #include <linux/kernel.h>
18 #include <linux/mii.h>
19 #include <linux/init.h>
20 #include <linux/delay.h>
21 #include "tulip.h"
24 /* The maximum data clock rate is 2.5 Mhz. The minimum timing is usually
25 met by back-to-back PCI I/O cycles, but we insert a delay to avoid
26 "overclocking" issues or future 66Mhz PCI. */
27 #define mdio_delay() inl(mdio_addr)
29 /* Read and write the MII registers using software-generated serial
30 MDIO protocol. It is just different enough from the EEPROM protocol
31 to not share code. The maxium data clock rate is 2.5 Mhz. */
32 #define MDIO_SHIFT_CLK 0x10000
33 #define MDIO_DATA_WRITE0 0x00000
34 #define MDIO_DATA_WRITE1 0x20000
35 #define MDIO_ENB 0x00000 /* Ignore the 0x02000 databook setting. */
36 #define MDIO_ENB_IN 0x40000
37 #define MDIO_DATA_READ 0x80000
39 static const unsigned char comet_miireg2offset[32] = {
40 0xB4, 0xB8, 0xBC, 0xC0, 0xC4, 0xC8, 0xCC, 0, 0,0,0,0, 0,0,0,0,
41 0,0xD0,0,0, 0,0,0,0, 0,0,0,0, 0, 0xD4, 0xD8, 0xDC, };
44 /* MII transceiver control section.
45 Read and write the MII registers using software-generated serial
46 MDIO protocol. See the MII specifications or DP83840A data sheet
47 for details. */
49 int tulip_mdio_read(struct net_device *dev, int phy_id, int location)
51 struct tulip_private *tp = netdev_priv(dev);
52 int i;
53 int read_cmd = (0xf6 << 10) | ((phy_id & 0x1f) << 5) | location;
54 int retval = 0;
55 long ioaddr = dev->base_addr;
56 long mdio_addr = ioaddr + CSR9;
57 unsigned long flags;
59 if (location & ~0x1f)
60 return 0xffff;
62 if (tp->chip_id == COMET && phy_id == 30) {
63 if (comet_miireg2offset[location])
64 return inl(ioaddr + comet_miireg2offset[location]);
65 return 0xffff;
68 spin_lock_irqsave(&tp->mii_lock, flags);
69 if (tp->chip_id == LC82C168) {
70 int i = 1000;
71 outl(0x60020000 + (phy_id<<23) + (location<<18), ioaddr + 0xA0);
72 inl(ioaddr + 0xA0);
73 inl(ioaddr + 0xA0);
74 while (--i > 0) {
75 barrier();
76 if ( ! ((retval = inl(ioaddr + 0xA0)) & 0x80000000))
77 break;
79 spin_unlock_irqrestore(&tp->mii_lock, flags);
80 return retval & 0xffff;
83 /* Establish sync by sending at least 32 logic ones. */
84 for (i = 32; i >= 0; i--) {
85 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
86 mdio_delay();
87 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
88 mdio_delay();
90 /* Shift the read command bits out. */
91 for (i = 15; i >= 0; i--) {
92 int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
94 outl(MDIO_ENB | dataval, mdio_addr);
95 mdio_delay();
96 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
97 mdio_delay();
99 /* Read the two transition, 16 data, and wire-idle bits. */
100 for (i = 19; i > 0; i--) {
101 outl(MDIO_ENB_IN, mdio_addr);
102 mdio_delay();
103 retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
104 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
105 mdio_delay();
108 spin_unlock_irqrestore(&tp->mii_lock, flags);
109 return (retval>>1) & 0xffff;
112 void tulip_mdio_write(struct net_device *dev, int phy_id, int location, int val)
114 struct tulip_private *tp = netdev_priv(dev);
115 int i;
116 int cmd = (0x5002 << 16) | ((phy_id & 0x1f) << 23) | (location<<18) | (val & 0xffff);
117 long ioaddr = dev->base_addr;
118 long mdio_addr = ioaddr + CSR9;
119 unsigned long flags;
121 if (location & ~0x1f)
122 return;
124 if (tp->chip_id == COMET && phy_id == 30) {
125 if (comet_miireg2offset[location])
126 outl(val, ioaddr + comet_miireg2offset[location]);
127 return;
130 spin_lock_irqsave(&tp->mii_lock, flags);
131 if (tp->chip_id == LC82C168) {
132 int i = 1000;
133 outl(cmd, ioaddr + 0xA0);
134 do {
135 barrier();
136 if ( ! (inl(ioaddr + 0xA0) & 0x80000000))
137 break;
138 } while (--i > 0);
139 spin_unlock_irqrestore(&tp->mii_lock, flags);
140 return;
143 /* Establish sync by sending 32 logic ones. */
144 for (i = 32; i >= 0; i--) {
145 outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
146 mdio_delay();
147 outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
148 mdio_delay();
150 /* Shift the command bits out. */
151 for (i = 31; i >= 0; i--) {
152 int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
153 outl(MDIO_ENB | dataval, mdio_addr);
154 mdio_delay();
155 outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
156 mdio_delay();
158 /* Clear out extra bits. */
159 for (i = 2; i > 0; i--) {
160 outl(MDIO_ENB_IN, mdio_addr);
161 mdio_delay();
162 outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
163 mdio_delay();
166 spin_unlock_irqrestore(&tp->mii_lock, flags);
170 /* Set up the transceiver control registers for the selected media type. */
171 void tulip_select_media(struct net_device *dev, int startup)
173 long ioaddr = dev->base_addr;
174 struct tulip_private *tp = netdev_priv(dev);
175 struct mediatable *mtable = tp->mtable;
176 u32 new_csr6;
177 int i;
179 if (mtable) {
180 struct medialeaf *mleaf = &mtable->mleaf[tp->cur_index];
181 unsigned char *p = mleaf->leafdata;
182 switch (mleaf->type) {
183 case 0: /* 21140 non-MII xcvr. */
184 if (tulip_debug > 1)
185 printk(KERN_DEBUG "%s: Using a 21140 non-MII transceiver"
186 " with control setting %2.2x.\n",
187 dev->name, p[1]);
188 dev->if_port = p[0];
189 if (startup)
190 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
191 outl(p[1], ioaddr + CSR12);
192 new_csr6 = 0x02000000 | ((p[2] & 0x71) << 18);
193 break;
194 case 2: case 4: {
195 u16 setup[5];
196 u32 csr13val, csr14val, csr15dir, csr15val;
197 for (i = 0; i < 5; i++)
198 setup[i] = get_u16(&p[i*2 + 1]);
200 dev->if_port = p[0] & MEDIA_MASK;
201 if (tulip_media_cap[dev->if_port] & MediaAlwaysFD)
202 tp->full_duplex = 1;
204 if (startup && mtable->has_reset) {
205 struct medialeaf *rleaf = &mtable->mleaf[mtable->has_reset];
206 unsigned char *rst = rleaf->leafdata;
207 if (tulip_debug > 1)
208 printk(KERN_DEBUG "%s: Resetting the transceiver.\n",
209 dev->name);
210 for (i = 0; i < rst[0]; i++)
211 outl(get_u16(rst + 1 + (i<<1)) << 16, ioaddr + CSR15);
213 if (tulip_debug > 1)
214 printk(KERN_DEBUG "%s: 21143 non-MII %s transceiver control "
215 "%4.4x/%4.4x.\n",
216 dev->name, medianame[dev->if_port], setup[0], setup[1]);
217 if (p[0] & 0x40) { /* SIA (CSR13-15) setup values are provided. */
218 csr13val = setup[0];
219 csr14val = setup[1];
220 csr15dir = (setup[3]<<16) | setup[2];
221 csr15val = (setup[4]<<16) | setup[2];
222 outl(0, ioaddr + CSR13);
223 outl(csr14val, ioaddr + CSR14);
224 outl(csr15dir, ioaddr + CSR15); /* Direction */
225 outl(csr15val, ioaddr + CSR15); /* Data */
226 outl(csr13val, ioaddr + CSR13);
227 } else {
228 csr13val = 1;
229 csr14val = 0;
230 csr15dir = (setup[0]<<16) | 0x0008;
231 csr15val = (setup[1]<<16) | 0x0008;
232 if (dev->if_port <= 4)
233 csr14val = t21142_csr14[dev->if_port];
234 if (startup) {
235 outl(0, ioaddr + CSR13);
236 outl(csr14val, ioaddr + CSR14);
238 outl(csr15dir, ioaddr + CSR15); /* Direction */
239 outl(csr15val, ioaddr + CSR15); /* Data */
240 if (startup) outl(csr13val, ioaddr + CSR13);
242 if (tulip_debug > 1)
243 printk(KERN_DEBUG "%s: Setting CSR15 to %8.8x/%8.8x.\n",
244 dev->name, csr15dir, csr15val);
245 if (mleaf->type == 4)
246 new_csr6 = 0x82020000 | ((setup[2] & 0x71) << 18);
247 else
248 new_csr6 = 0x82420000;
249 break;
251 case 1: case 3: {
252 int phy_num = p[0];
253 int init_length = p[1];
254 u16 *misc_info, tmp_info;
256 dev->if_port = 11;
257 new_csr6 = 0x020E0000;
258 if (mleaf->type == 3) { /* 21142 */
259 u16 *init_sequence = (u16*)(p+2);
260 u16 *reset_sequence = &((u16*)(p+3))[init_length];
261 int reset_length = p[2 + init_length*2];
262 misc_info = reset_sequence + reset_length;
263 if (startup)
264 for (i = 0; i < reset_length; i++)
265 outl(get_u16(&reset_sequence[i]) << 16, ioaddr + CSR15);
266 for (i = 0; i < init_length; i++)
267 outl(get_u16(&init_sequence[i]) << 16, ioaddr + CSR15);
268 } else {
269 u8 *init_sequence = p + 2;
270 u8 *reset_sequence = p + 3 + init_length;
271 int reset_length = p[2 + init_length];
272 misc_info = (u16*)(reset_sequence + reset_length);
273 if (startup) {
274 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
275 for (i = 0; i < reset_length; i++)
276 outl(reset_sequence[i], ioaddr + CSR12);
278 for (i = 0; i < init_length; i++)
279 outl(init_sequence[i], ioaddr + CSR12);
281 tmp_info = get_u16(&misc_info[1]);
282 if (tmp_info)
283 tp->advertising[phy_num] = tmp_info | 1;
284 if (tmp_info && startup < 2) {
285 if (tp->mii_advertise == 0)
286 tp->mii_advertise = tp->advertising[phy_num];
287 if (tulip_debug > 1)
288 printk(KERN_DEBUG "%s: Advertising %4.4x on MII %d.\n",
289 dev->name, tp->mii_advertise, tp->phys[phy_num]);
290 tulip_mdio_write(dev, tp->phys[phy_num], 4, tp->mii_advertise);
292 break;
294 case 5: case 6: {
295 u16 setup[5];
297 new_csr6 = 0; /* FIXME */
299 for (i = 0; i < 5; i++)
300 setup[i] = get_u16(&p[i*2 + 1]);
302 if (startup && mtable->has_reset) {
303 struct medialeaf *rleaf = &mtable->mleaf[mtable->has_reset];
304 unsigned char *rst = rleaf->leafdata;
305 if (tulip_debug > 1)
306 printk(KERN_DEBUG "%s: Resetting the transceiver.\n",
307 dev->name);
308 for (i = 0; i < rst[0]; i++)
309 outl(get_u16(rst + 1 + (i<<1)) << 16, ioaddr + CSR15);
312 break;
314 default:
315 printk(KERN_DEBUG "%s: Invalid media table selection %d.\n",
316 dev->name, mleaf->type);
317 new_csr6 = 0x020E0000;
319 if (tulip_debug > 1)
320 printk(KERN_DEBUG "%s: Using media type %s, CSR12 is %2.2x.\n",
321 dev->name, medianame[dev->if_port],
322 inl(ioaddr + CSR12) & 0xff);
323 } else if (tp->chip_id == LC82C168) {
324 if (startup && ! tp->medialock)
325 dev->if_port = tp->mii_cnt ? 11 : 0;
326 if (tulip_debug > 1)
327 printk(KERN_DEBUG "%s: PNIC PHY status is %3.3x, media %s.\n",
328 dev->name, inl(ioaddr + 0xB8), medianame[dev->if_port]);
329 if (tp->mii_cnt) {
330 new_csr6 = 0x810C0000;
331 outl(0x0001, ioaddr + CSR15);
332 outl(0x0201B07A, ioaddr + 0xB8);
333 } else if (startup) {
334 /* Start with 10mbps to do autonegotiation. */
335 outl(0x32, ioaddr + CSR12);
336 new_csr6 = 0x00420000;
337 outl(0x0001B078, ioaddr + 0xB8);
338 outl(0x0201B078, ioaddr + 0xB8);
339 } else if (dev->if_port == 3 || dev->if_port == 5) {
340 outl(0x33, ioaddr + CSR12);
341 new_csr6 = 0x01860000;
342 /* Trigger autonegotiation. */
343 outl(startup ? 0x0201F868 : 0x0001F868, ioaddr + 0xB8);
344 } else {
345 outl(0x32, ioaddr + CSR12);
346 new_csr6 = 0x00420000;
347 outl(0x1F078, ioaddr + 0xB8);
349 } else { /* Unknown chip type with no media table. */
350 if (tp->default_port == 0)
351 dev->if_port = tp->mii_cnt ? 11 : 3;
352 if (tulip_media_cap[dev->if_port] & MediaIsMII) {
353 new_csr6 = 0x020E0000;
354 } else if (tulip_media_cap[dev->if_port] & MediaIsFx) {
355 new_csr6 = 0x02860000;
356 } else
357 new_csr6 = 0x03860000;
358 if (tulip_debug > 1)
359 printk(KERN_DEBUG "%s: No media description table, assuming "
360 "%s transceiver, CSR12 %2.2x.\n",
361 dev->name, medianame[dev->if_port],
362 inl(ioaddr + CSR12));
365 tp->csr6 = new_csr6 | (tp->csr6 & 0xfdff) | (tp->full_duplex ? 0x0200 : 0);
366 return;
370 Check the MII negotiated duplex and change the CSR6 setting if
371 required.
372 Return 0 if everything is OK.
373 Return < 0 if the transceiver is missing or has no link beat.
375 int tulip_check_duplex(struct net_device *dev)
377 struct tulip_private *tp = netdev_priv(dev);
378 unsigned int bmsr, lpa, negotiated, new_csr6;
380 bmsr = tulip_mdio_read(dev, tp->phys[0], MII_BMSR);
381 lpa = tulip_mdio_read(dev, tp->phys[0], MII_LPA);
382 if (tulip_debug > 1)
383 printk(KERN_INFO "%s: MII status %4.4x, Link partner report "
384 "%4.4x.\n", dev->name, bmsr, lpa);
385 if (bmsr == 0xffff)
386 return -2;
387 if ((bmsr & BMSR_LSTATUS) == 0) {
388 int new_bmsr = tulip_mdio_read(dev, tp->phys[0], MII_BMSR);
389 if ((new_bmsr & BMSR_LSTATUS) == 0) {
390 if (tulip_debug > 1)
391 printk(KERN_INFO "%s: No link beat on the MII interface,"
392 " status %4.4x.\n", dev->name, new_bmsr);
393 return -1;
396 negotiated = lpa & tp->advertising[0];
397 tp->full_duplex = mii_duplex(tp->full_duplex_lock, negotiated);
399 new_csr6 = tp->csr6;
401 if (negotiated & LPA_100) new_csr6 &= ~TxThreshold;
402 else new_csr6 |= TxThreshold;
403 if (tp->full_duplex) new_csr6 |= FullDuplex;
404 else new_csr6 &= ~FullDuplex;
406 if (new_csr6 != tp->csr6) {
407 tp->csr6 = new_csr6;
408 tulip_restart_rxtx(tp);
410 if (tulip_debug > 0)
411 printk(KERN_INFO "%s: Setting %s-duplex based on MII"
412 "#%d link partner capability of %4.4x.\n",
413 dev->name, tp->full_duplex ? "full" : "half",
414 tp->phys[0], lpa);
415 return 1;
418 return 0;
421 void __devinit tulip_find_mii (struct net_device *dev, int board_idx)
423 struct tulip_private *tp = netdev_priv(dev);
424 int phyn, phy_idx = 0;
425 int mii_reg0;
426 int mii_advert;
427 unsigned int to_advert, new_bmcr, ane_switch;
429 /* Find the connected MII xcvrs.
430 Doing this in open() would allow detecting external xcvrs later,
431 but takes much time. */
432 for (phyn = 1; phyn <= 32 && phy_idx < sizeof (tp->phys); phyn++) {
433 int phy = phyn & 0x1f;
434 int mii_status = tulip_mdio_read (dev, phy, MII_BMSR);
435 if ((mii_status & 0x8301) == 0x8001 ||
436 ((mii_status & BMSR_100BASE4) == 0
437 && (mii_status & 0x7800) != 0)) {
438 /* preserve Becker logic, gain indentation level */
439 } else {
440 continue;
443 mii_reg0 = tulip_mdio_read (dev, phy, MII_BMCR);
444 mii_advert = tulip_mdio_read (dev, phy, MII_ADVERTISE);
445 ane_switch = 0;
447 /* if not advertising at all, gen an
448 * advertising value from the capability
449 * bits in BMSR
451 if ((mii_advert & ADVERTISE_ALL) == 0) {
452 unsigned int tmpadv = tulip_mdio_read (dev, phy, MII_BMSR);
453 mii_advert = ((tmpadv >> 6) & 0x3e0) | 1;
456 if (tp->mii_advertise) {
457 tp->advertising[phy_idx] =
458 to_advert = tp->mii_advertise;
459 } else if (tp->advertising[phy_idx]) {
460 to_advert = tp->advertising[phy_idx];
461 } else {
462 tp->advertising[phy_idx] =
463 tp->mii_advertise =
464 to_advert = mii_advert;
467 tp->phys[phy_idx++] = phy;
469 printk (KERN_INFO "tulip%d: MII transceiver #%d "
470 "config %4.4x status %4.4x advertising %4.4x.\n",
471 board_idx, phy, mii_reg0, mii_status, mii_advert);
473 /* Fixup for DLink with miswired PHY. */
474 if (mii_advert != to_advert) {
475 printk (KERN_DEBUG "tulip%d: Advertising %4.4x on PHY %d,"
476 " previously advertising %4.4x.\n",
477 board_idx, to_advert, phy, mii_advert);
478 tulip_mdio_write (dev, phy, 4, to_advert);
481 /* Enable autonegotiation: some boards default to off. */
482 if (tp->default_port == 0) {
483 new_bmcr = mii_reg0 | BMCR_ANENABLE;
484 if (new_bmcr != mii_reg0) {
485 new_bmcr |= BMCR_ANRESTART;
486 ane_switch = 1;
489 /* ...or disable nway, if forcing media */
490 else {
491 new_bmcr = mii_reg0 & ~BMCR_ANENABLE;
492 if (new_bmcr != mii_reg0)
493 ane_switch = 1;
496 /* clear out bits we never want at this point */
497 new_bmcr &= ~(BMCR_CTST | BMCR_FULLDPLX | BMCR_ISOLATE |
498 BMCR_PDOWN | BMCR_SPEED100 | BMCR_LOOPBACK |
499 BMCR_RESET);
501 if (tp->full_duplex)
502 new_bmcr |= BMCR_FULLDPLX;
503 if (tulip_media_cap[tp->default_port] & MediaIs100)
504 new_bmcr |= BMCR_SPEED100;
506 if (new_bmcr != mii_reg0) {
507 /* some phys need the ANE switch to
508 * happen before forced media settings
509 * will "take." However, we write the
510 * same value twice in order not to
511 * confuse the sane phys.
513 if (ane_switch) {
514 tulip_mdio_write (dev, phy, MII_BMCR, new_bmcr);
515 udelay (10);
517 tulip_mdio_write (dev, phy, MII_BMCR, new_bmcr);
520 tp->mii_cnt = phy_idx;
521 if (tp->mtable && tp->mtable->has_mii && phy_idx == 0) {
522 printk (KERN_INFO "tulip%d: ***WARNING***: No MII transceiver found!\n",
523 board_idx);
524 tp->phys[0] = 1;