2 drivers/net/tulip/media.c
4 Maintained by Jeff Garzik <jgarzik@mandrakesoft.com>
5 Copyright 2000 The Linux Kernel Team
6 Written/copyright 1994-1999 by Donald Becker.
8 This software may be used and distributed according to the terms
9 of the GNU Public License, incorporated herein by reference.
11 Please refer to Documentation/networking/tulip.txt for more
12 information on this driver.
20 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
21 to support a pre-NWay full-duplex signaling mechanism using short frames.
22 No one knows what it should be, but if left at its default value some
23 10base2(!) packets trigger a full-duplex-request interrupt. */
24 #define FULL_DUPLEX_MAGIC 0x6969
27 /* MII transceiver control section.
28 Read and write the MII registers using software-generated serial
29 MDIO protocol. See the MII specifications or DP83840A data sheet
32 int tulip_mdio_read(struct net_device
*dev
, int phy_id
, int location
)
34 struct tulip_private
*tp
= (struct tulip_private
*)dev
->priv
;
36 int read_cmd
= (0xf6 << 10) | (phy_id
<< 5) | location
;
38 long ioaddr
= dev
->base_addr
;
39 long mdio_addr
= ioaddr
+ CSR9
;
41 if (tp
->chip_id
== LC82C168
) {
43 outl(0x60020000 + (phy_id
<<23) + (location
<<18), ioaddr
+ 0xA0);
47 if ( ! ((retval
= inl(ioaddr
+ 0xA0)) & 0x80000000))
48 return retval
& 0xffff;
52 if (tp
->chip_id
== COMET
) {
55 return inl(ioaddr
+ 0xB4 + (location
<<2));
56 else if (location
== 17)
57 return inl(ioaddr
+ 0xD0);
58 else if (location
>= 29 && location
<= 31)
59 return inl(ioaddr
+ 0xD4 + ((location
-29)<<2));
64 /* Establish sync by sending at least 32 logic ones. */
65 for (i
= 32; i
>= 0; i
--) {
66 outl(MDIO_ENB
| MDIO_DATA_WRITE1
, mdio_addr
);
68 outl(MDIO_ENB
| MDIO_DATA_WRITE1
| MDIO_SHIFT_CLK
, mdio_addr
);
71 /* Shift the read command bits out. */
72 for (i
= 15; i
>= 0; i
--) {
73 int dataval
= (read_cmd
& (1 << i
)) ? MDIO_DATA_WRITE1
: 0;
75 outl(MDIO_ENB
| dataval
, mdio_addr
);
77 outl(MDIO_ENB
| dataval
| MDIO_SHIFT_CLK
, mdio_addr
);
80 /* Read the two transition, 16 data, and wire-idle bits. */
81 for (i
= 19; i
> 0; i
--) {
82 outl(MDIO_ENB_IN
, mdio_addr
);
84 retval
= (retval
<< 1) | ((inl(mdio_addr
) & MDIO_DATA_READ
) ? 1 : 0);
85 outl(MDIO_ENB_IN
| MDIO_SHIFT_CLK
, mdio_addr
);
88 return (retval
>>1) & 0xffff;
91 void tulip_mdio_write(struct net_device
*dev
, int phy_id
, int location
, int value
)
93 struct tulip_private
*tp
= (struct tulip_private
*)dev
->priv
;
95 int cmd
= (0x5002 << 16) | (phy_id
<< 23) | (location
<<18) | value
;
96 long ioaddr
= dev
->base_addr
;
97 long mdio_addr
= ioaddr
+ CSR9
;
99 if (tp
->chip_id
== LC82C168
) {
101 outl(cmd
, ioaddr
+ 0xA0);
103 if ( ! (inl(ioaddr
+ 0xA0) & 0x80000000))
109 if (tp
->chip_id
== COMET
) {
113 outl(value
, ioaddr
+ 0xB4 + (location
<<2));
114 else if (location
== 17)
115 outl(value
, ioaddr
+ 0xD0);
116 else if (location
>= 29 && location
<= 31)
117 outl(value
, ioaddr
+ 0xD4 + ((location
-29)<<2));
121 /* Establish sync by sending 32 logic ones. */
122 for (i
= 32; i
>= 0; i
--) {
123 outl(MDIO_ENB
| MDIO_DATA_WRITE1
, mdio_addr
);
125 outl(MDIO_ENB
| MDIO_DATA_WRITE1
| MDIO_SHIFT_CLK
, mdio_addr
);
128 /* Shift the command bits out. */
129 for (i
= 31; i
>= 0; i
--) {
130 int dataval
= (cmd
& (1 << i
)) ? MDIO_DATA_WRITE1
: 0;
131 outl(MDIO_ENB
| dataval
, mdio_addr
);
133 outl(MDIO_ENB
| dataval
| MDIO_SHIFT_CLK
, mdio_addr
);
136 /* Clear out extra bits. */
137 for (i
= 2; i
> 0; i
--) {
138 outl(MDIO_ENB_IN
, mdio_addr
);
140 outl(MDIO_ENB_IN
| MDIO_SHIFT_CLK
, mdio_addr
);
146 /* Set up the transceiver control registers for the selected media type. */
147 void tulip_select_media(struct net_device
*dev
, int startup
)
149 long ioaddr
= dev
->base_addr
;
150 struct tulip_private
*tp
= (struct tulip_private
*)dev
->priv
;
151 struct mediatable
*mtable
= tp
->mtable
;
156 struct medialeaf
*mleaf
= &mtable
->mleaf
[tp
->cur_index
];
157 unsigned char *p
= mleaf
->leafdata
;
158 switch (mleaf
->type
) {
159 case 0: /* 21140 non-MII xcvr. */
161 printk(KERN_DEBUG
"%s: Using a 21140 non-MII transceiver"
162 " with control setting %2.2x.\n",
166 outl(mtable
->csr12dir
| 0x100, ioaddr
+ CSR12
);
167 outl(p
[1], ioaddr
+ CSR12
);
168 new_csr6
= 0x02000000 | ((p
[2] & 0x71) << 18);
172 u32 csr13val
, csr14val
, csr15dir
, csr15val
;
173 for (i
= 0; i
< 5; i
++)
174 setup
[i
] = get_u16(&p
[i
*2 + 1]);
176 dev
->if_port
= p
[0] & 15;
177 if (tulip_media_cap
[dev
->if_port
] & MediaAlwaysFD
)
180 if (startup
&& mtable
->has_reset
) {
181 struct medialeaf
*rleaf
= &mtable
->mleaf
[mtable
->has_reset
];
182 unsigned char *rst
= rleaf
->leafdata
;
184 printk(KERN_DEBUG
"%s: Resetting the transceiver.\n",
186 for (i
= 0; i
< rst
[0]; i
++)
187 outl(get_u16(rst
+ 1 + (i
<<1)) << 16, ioaddr
+ CSR15
);
190 printk(KERN_DEBUG
"%s: 21143 non-MII %s transceiver control "
192 dev
->name
, medianame
[dev
->if_port
], setup
[0], setup
[1]);
193 if (p
[0] & 0x40) { /* SIA (CSR13-15) setup values are provided. */
196 csr15dir
= (setup
[3]<<16) | setup
[2];
197 csr15val
= (setup
[4]<<16) | setup
[2];
198 outl(0, ioaddr
+ CSR13
);
199 outl(csr14val
, ioaddr
+ CSR14
);
200 outl(csr15dir
, ioaddr
+ CSR15
); /* Direction */
201 outl(csr15val
, ioaddr
+ CSR15
); /* Data */
202 outl(csr13val
, ioaddr
+ CSR13
);
205 csr14val
= 0x0003FF7F;
206 csr15dir
= (setup
[0]<<16) | 0x0008;
207 csr15val
= (setup
[1]<<16) | 0x0008;
208 if (dev
->if_port
<= 4)
209 csr14val
= t21142_csr14
[dev
->if_port
];
211 outl(0, ioaddr
+ CSR13
);
212 outl(csr14val
, ioaddr
+ CSR14
);
214 outl(csr15dir
, ioaddr
+ CSR15
); /* Direction */
215 outl(csr15val
, ioaddr
+ CSR15
); /* Data */
216 if (startup
) outl(csr13val
, ioaddr
+ CSR13
);
219 printk(KERN_DEBUG
"%s: Setting CSR15 to %8.8x/%8.8x.\n",
220 dev
->name
, csr15dir
, csr15val
);
221 if (mleaf
->type
== 4)
222 new_csr6
= 0x82020000 | ((setup
[2] & 0x71) << 18);
224 new_csr6
= 0x82420000;
229 int init_length
= p
[1];
234 new_csr6
= 0x020E0000;
235 if (mleaf
->type
== 3) { /* 21142 */
236 u16
*init_sequence
= (u16
*)(p
+2);
237 u16
*reset_sequence
= &((u16
*)(p
+3))[init_length
];
238 int reset_length
= p
[2 + init_length
*2];
239 misc_info
= reset_sequence
+ reset_length
;
241 for (i
= 0; i
< reset_length
; i
++)
242 outl(get_u16(&reset_sequence
[i
]) << 16, ioaddr
+ CSR15
);
243 for (i
= 0; i
< init_length
; i
++)
244 outl(get_u16(&init_sequence
[i
]) << 16, ioaddr
+ CSR15
);
246 u8
*init_sequence
= p
+ 2;
247 u8
*reset_sequence
= p
+ 3 + init_length
;
248 int reset_length
= p
[2 + init_length
];
249 misc_info
= (u16
*)(reset_sequence
+ reset_length
);
251 outl(mtable
->csr12dir
| 0x100, ioaddr
+ CSR12
);
252 for (i
= 0; i
< reset_length
; i
++)
253 outl(reset_sequence
[i
], ioaddr
+ CSR12
);
255 for (i
= 0; i
< init_length
; i
++)
256 outl(init_sequence
[i
], ioaddr
+ CSR12
);
258 to_advertise
= (get_u16(&misc_info
[1]) & tp
->to_advertise
) | 1;
259 tp
->advertising
[phy_num
] = to_advertise
;
261 printk(KERN_DEBUG
"%s: Advertising %4.4x on PHY %d (%d).\n",
262 dev
->name
, to_advertise
, phy_num
, tp
->phys
[phy_num
]);
263 /* Bogus: put in by a committee? */
264 tulip_mdio_write(dev
, tp
->phys
[phy_num
], 4, to_advertise
);
268 printk(KERN_DEBUG
"%s: Invalid media table selection %d.\n",
269 dev
->name
, mleaf
->type
);
270 new_csr6
= 0x020E0000;
273 printk(KERN_DEBUG
"%s: Using media type %s, CSR12 is %2.2x.\n",
274 dev
->name
, medianame
[dev
->if_port
],
275 inl(ioaddr
+ CSR12
) & 0xff);
276 } else if (tp
->chip_id
== DC21041
) {
277 int port
= dev
->if_port
<= 4 ? dev
->if_port
: 0;
279 printk(KERN_DEBUG
"%s: 21041 using media %s, CSR12 is %4.4x.\n",
280 dev
->name
, medianame
[port
== 3 ? 12: port
],
281 inl(ioaddr
+ CSR12
));
282 outl(0x00000000, ioaddr
+ CSR13
); /* Reset the serial interface */
283 outl(t21041_csr14
[port
], ioaddr
+ CSR14
);
284 outl(t21041_csr15
[port
], ioaddr
+ CSR15
);
285 outl(t21041_csr13
[port
], ioaddr
+ CSR13
);
286 new_csr6
= 0x80020000;
287 } else if (tp
->chip_id
== LC82C168
) {
288 if (startup
&& ! tp
->medialock
)
289 dev
->if_port
= tp
->mii_cnt
? 11 : 0;
291 printk(KERN_DEBUG
"%s: PNIC PHY status is %3.3x, media %s.\n",
292 dev
->name
, inl(ioaddr
+ 0xB8), medianame
[dev
->if_port
]);
294 new_csr6
= 0x810C0000;
295 outl(0x0001, ioaddr
+ CSR15
);
296 outl(0x0201B07A, ioaddr
+ 0xB8);
297 } else if (startup
) {
298 /* Start with 10mbps to do autonegotiation. */
299 outl(0x32, ioaddr
+ CSR12
);
300 new_csr6
= 0x00420000;
301 outl(0x0001B078, ioaddr
+ 0xB8);
302 outl(0x0201B078, ioaddr
+ 0xB8);
303 } else if (dev
->if_port
== 3 || dev
->if_port
== 5) {
304 outl(0x33, ioaddr
+ CSR12
);
305 new_csr6
= 0x01860000;
306 /* Trigger autonegotiation. */
307 outl(startup
? 0x0201F868 : 0x0001F868, ioaddr
+ 0xB8);
309 outl(0x32, ioaddr
+ CSR12
);
310 new_csr6
= 0x00420000;
311 outl(0x1F078, ioaddr
+ 0xB8);
313 } else if (tp
->chip_id
== DC21040
) { /* 21040 */
314 /* Turn on the xcvr interface. */
315 int csr12
= inl(ioaddr
+ CSR12
);
317 printk(KERN_DEBUG
"%s: 21040 media type is %s, CSR12 is %2.2x.\n",
318 dev
->name
, medianame
[dev
->if_port
], csr12
);
319 if (tulip_media_cap
[dev
->if_port
] & MediaAlwaysFD
)
322 /* Set the full duplux match frame. */
323 outl(FULL_DUPLEX_MAGIC
, ioaddr
+ CSR11
);
324 outl(0x00000000, ioaddr
+ CSR13
); /* Reset the serial interface */
325 if (t21040_csr13
[dev
->if_port
] & 8) {
326 outl(0x0705, ioaddr
+ CSR14
);
327 outl(0x0006, ioaddr
+ CSR15
);
329 outl(0xffff, ioaddr
+ CSR14
);
330 outl(0x0000, ioaddr
+ CSR15
);
332 outl(0x8f01 | t21040_csr13
[dev
->if_port
], ioaddr
+ CSR13
);
333 } else { /* Unknown chip type with no media table. */
334 if (tp
->default_port
== 0)
335 dev
->if_port
= tp
->mii_cnt
? 11 : 3;
336 if (tulip_media_cap
[dev
->if_port
] & MediaIsMII
) {
337 new_csr6
= 0x020E0000;
338 } else if (tulip_media_cap
[dev
->if_port
] & MediaIsFx
) {
339 new_csr6
= 0x028600000;
341 new_csr6
= 0x038600000;
343 printk(KERN_DEBUG
"%s: No media description table, assuming "
344 "%s transceiver, CSR12 %2.2x.\n",
345 dev
->name
, medianame
[dev
->if_port
],
346 inl(ioaddr
+ CSR12
));
349 tp
->csr6
= new_csr6
| (tp
->csr6
& 0xfdff) | (tp
->full_duplex
? 0x0200 : 0);
354 Check the MII negotiated duplex, and change the CSR6 setting if
356 Return 0 if everything is OK.
357 Return < 0 if the transceiver is missing or has no link beat.
359 int tulip_check_duplex(struct net_device
*dev
)
361 struct tulip_private
*tp
= (struct tulip_private
*)dev
->priv
;
362 int mii_reg1
, mii_reg5
, negotiated
, duplex
;
364 if (tp
->full_duplex_lock
)
366 mii_reg1
= tulip_mdio_read(dev
, tp
->phys
[0], 1);
367 mii_reg5
= tulip_mdio_read(dev
, tp
->phys
[0], 5);
369 printk(KERN_INFO
"%s: MII status %4.4x, Link partner report "
370 "%4.4x.\n", dev
->name
, mii_reg1
, mii_reg5
);
371 if (mii_reg1
== 0xffff)
373 if ((mii_reg1
& 0x0004) == 0) {
374 int new_reg1
= tulip_mdio_read(dev
, tp
->phys
[0], 1);
375 if ((new_reg1
& 0x0004) == 0) {
377 printk(KERN_INFO
"%s: No link beat on the MII interface,"
378 " status %4.4x.\n", dev
->name
, new_reg1
);
382 negotiated
= mii_reg5
& tp
->advertising
[0];
383 duplex
= ((negotiated
& 0x0300) == 0x0100
384 || (negotiated
& 0x00C0) == 0x0040);
385 /* 100baseTx-FD or 10T-FD, but not 100-HD */
386 if (tp
->full_duplex
!= duplex
) {
387 tp
->full_duplex
= duplex
;
388 if (negotiated
& 0x038) /* 100mbps. */
389 tp
->csr6
&= ~0x00400000;
390 if (tp
->full_duplex
) tp
->csr6
|= 0x0200;
391 else tp
->csr6
&= ~0x0200;
392 tulip_outl_CSR6(tp
, tp
->csr6
| 0x0002);
393 tulip_outl_CSR6(tp
, tp
->csr6
| 0x2002);
395 printk(KERN_INFO
"%s: Setting %s-duplex based on MII"
396 "#%d link partner capability of %4.4x.\n",
397 dev
->name
, tp
->full_duplex
? "full" : "half",
398 tp
->phys
[0], mii_reg5
);