VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/DevE1000Phy.cpp@ 26597

Last change on this file since 26597 was 23965, checked in by vboxsync, 15 years ago

DevE1000Phy: nit picking.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 18.2 KB
Line 
1/** $Id: DevE1000Phy.cpp 23965 2009-10-22 09:56:20Z vboxsync $ */
2/** @file
3 * DevE1000Phy - Intel 82540EM Ethernet Controller Internal PHY Emulation.
4 *
5 * Implemented in accordance with the specification:
6 * PCI/PCI-X Family of Gigabit Ethernet Controllers Software Developer�s Manual
7 * 82540EP/EM, 82541xx, 82544GC/EI, 82545GM/EM, 82546GB/EB, and 82547xx
8 *
9 * 317453-002 Revision 3.5
10 */
11
12/*
13 * Copyright (C) 2007 Sun Microsystems, Inc.
14 *
15 * This file is part of VirtualBox Open Source Edition (OSE), as
16 * available from http://www.virtualbox.org. This file is free software;
17 * you can redistribute it and/or modify it under the terms of the GNU
18 * General Public License (GPL) as published by the Free Software
19 * Foundation, in version 2 as it comes in the "COPYING" file of the
20 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
21 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
22 *
23 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
24 * Clara, CA 95054 USA or visit http://www.sun.com if you need
25 * additional information or have any questions.
26 */
27
28#define LOG_GROUP LOG_GROUP_DEV_E1000
29
30/** @todo Remove me! For now I want asserts to work in release code. */
31// #ifndef RT_STRICT
32// #define RT_STRICT
33#include <iprt/assert.h>
34// #undef RT_STRICT
35// #endif
36
37#include <VBox/err.h>
38#include <VBox/log.h>
39#include <VBox/ssm.h>
40#include "DevE1000Phy.h"
41
42/* Little helpers ************************************************************/
43#ifdef PHY_UNIT_TEST
44# define SSMR3PutMem(a,b,c)
45# define SSMR3GetMem(a,b,c)
46#include <stdio.h>
47# define PhyLog(a) printf a
48#else /* PHY_UNIT_TEST */
49# define PhyLog(a) Log(a)
50#endif /* PHY_UNIT_TEST */
51
52#define REG(x) pPhy->au16Regs[x##_IDX]
53
54
55/* Internals */
56namespace Phy {
57 /** Retrieves state name by id */
58 static const char * getStateName(uint16_t u16State);
59 /** Look up register index by address. */
60 static int lookupRegister(uint32_t u32Address);
61 /** Software-triggered reset. */
62 static void softReset(PPHY pPhy);
63
64 /* Generic handlers ******************************************************/
65 static uint16_t regReadDefault (PPHY pPhy, uint32_t index);
66 static void regWriteDefault (PPHY pPhy, uint32_t index, uint16_t u16Value);
67 static uint16_t regReadForbidden (PPHY pPhy, uint32_t index);
68 static void regWriteForbidden (PPHY pPhy, uint32_t index, uint16_t u16Value);
69 static uint16_t regReadUnimplemented (PPHY pPhy, uint32_t index);
70 static void regWriteUnimplemented(PPHY pPhy, uint32_t index, uint16_t u16Value);
71 /* Register-specific handlers ********************************************/
72 static void regWritePCTRL (PPHY pPhy, uint32_t index, uint16_t u16Value);
73 static uint16_t regReadPSTATUS (PPHY pPhy, uint32_t index);
74 static uint16_t regReadGSTATUS (PPHY pPhy, uint32_t index);
75
76 /**
77 * PHY register map table.
78 *
79 * Override pfnRead and pfnWrite to implement register-specific behavior.
80 */
81 static struct RegMap_st
82 {
83 /** PHY register address. */
84 uint32_t u32Address;
85 /** Read callback. */
86 uint16_t (*pfnRead)(PPHY pPhy, uint32_t index);
87 /** Write callback. */
88 void (*pfnWrite)(PPHY pPhy, uint32_t index, uint16_t u16Value);
89 /** Abbreviated name. */
90 const char *szAbbrev;
91 /** Full name. */
92 const char *szName;
93 } s_regMap[NUM_OF_PHY_REGS] =
94 {
95 /*ra read callback write callback abbrev full name */
96 /*-- ------------------------- -------------------------- ---------- ------------------------------*/
97 { 0, Phy::regReadDefault , Phy::regWritePCTRL , "PCTRL" , "PHY Control" },
98 { 1, Phy::regReadPSTATUS , Phy::regWriteForbidden , "PSTATUS" , "PHY Status" },
99 { 2, Phy::regReadDefault , Phy::regWriteForbidden , "PID" , "PHY Identifier" },
100 { 3, Phy::regReadDefault , Phy::regWriteForbidden , "EPID" , "Extended PHY Identifier" },
101 { 4, Phy::regReadDefault , Phy::regWriteDefault , "ANA" , "Auto-Negotiation Advertisement" },
102 { 5, Phy::regReadDefault , Phy::regWriteForbidden , "LPA" , "Link Partner Ability" },
103 { 6, Phy::regReadUnimplemented, Phy::regWriteForbidden , "ANE" , "Auto-Negotiation Expansion" },
104 { 7, Phy::regReadUnimplemented, Phy::regWriteUnimplemented, "NPT" , "Next Page Transmit" },
105 { 8, Phy::regReadUnimplemented, Phy::regWriteForbidden , "LPN" , "Link Partner Next Page" },
106 { 9, Phy::regReadDefault , Phy::regWriteUnimplemented, "GCON" , "1000BASE-T Control" },
107 { 10, Phy::regReadGSTATUS , Phy::regWriteForbidden , "GSTATUS" , "1000BASE-T Status" },
108 { 15, Phy::regReadUnimplemented, Phy::regWriteForbidden , "EPSTATUS" , "Extended PHY Status" },
109 { 16, Phy::regReadDefault , Phy::regWriteDefault , "PSCON" , "PHY Specific Control" },
110 { 17, Phy::regReadDefault , Phy::regWriteForbidden , "PSSTAT" , "PHY Specific Status" },
111 { 18, Phy::regReadUnimplemented, Phy::regWriteUnimplemented, "PINTE" , "PHY Interrupt Enable" },
112 { 19, Phy::regReadUnimplemented, Phy::regWriteForbidden , "PINTS" , "PHY Interrupt Status" },
113 { 20, Phy::regReadUnimplemented, Phy::regWriteUnimplemented, "EPSCON1" , "Extended PHY Specific Control 1" },
114 { 21, Phy::regReadUnimplemented, Phy::regWriteForbidden , "PREC" , "PHY Receive Error Counter" },
115 { 26, Phy::regReadUnimplemented, Phy::regWriteUnimplemented, "EPSCON2" , "Extended PHY Specific Control 2" },
116 { 29, Phy::regReadForbidden , Phy::regWriteUnimplemented, "R30PS" , "MDI Register 30 Page Select" },
117 { 30, Phy::regReadUnimplemented, Phy::regWriteUnimplemented, "R30AW" , "MDI Register 30 Access Window" }
118 };
119}
120
121/**
122 * Default read handler.
123 *
124 * Fetches register value from the state structure.
125 *
126 * @returns Register value
127 *
128 * @param index Register index in register array.
129 */
130static uint16_t Phy::regReadDefault(PPHY pPhy, uint32_t index)
131{
132 AssertReturn(index<Phy::NUM_OF_PHY_REGS, 0);
133 return pPhy->au16Regs[index];
134}
135
136/**
137 * Default write handler.
138 *
139 * Writes the specified register value to the state structure.
140 *
141 * @param index Register index in register array.
142 * @param value The value to store (ignored).
143 */
144static void Phy::regWriteDefault(PPHY pPhy, uint32_t index, uint16_t u16Value)
145{
146 AssertReturnVoid(index<NUM_OF_PHY_REGS);
147 pPhy->au16Regs[index] = u16Value;
148}
149
150/**
151 * Read handler for write-only registers.
152 *
153 * Merely reports reads from write-only registers.
154 *
155 * @returns Register value (always 0)
156 *
157 * @param index Register index in register array.
158 */
159static uint16_t Phy::regReadForbidden(PPHY pPhy, uint32_t index)
160{
161 PhyLog(("PHY#%d At %02d read attempted from write-only '%s'\n",
162 pPhy->iInstance, s_regMap[index].u32Address, s_regMap[index].szName));
163 return 0;
164}
165
166/**
167 * Write handler for read-only registers.
168 *
169 * Merely reports writes to read-only registers.
170 *
171 * @param index Register index in register array.
172 * @param value The value to store (ignored).
173 */
174static void Phy::regWriteForbidden(PPHY pPhy, uint32_t index, uint16_t u16Value)
175{
176 PhyLog(("PHY#%d At %02d write attempted to read-only '%s'\n",
177 pPhy->iInstance, s_regMap[index].u32Address, s_regMap[index].szName));
178}
179
180/**
181 * Read handler for unimplemented registers.
182 *
183 * Merely reports reads from unimplemented registers.
184 *
185 * @returns Register value (always 0)
186 *
187 * @param index Register index in register array.
188 */
189static uint16_t Phy::regReadUnimplemented(PPHY pPhy, uint32_t index)
190{
191 PhyLog(("PHY#%d At %02d read attempted from unimplemented '%s'\n",
192 pPhy->iInstance, s_regMap[index].u32Address, s_regMap[index].szName));
193 return 0;
194}
195
196/**
197 * Write handler for unimplemented registers.
198 *
199 * Merely reports writes to unimplemented registers.
200 *
201 * @param index Register index in register array.
202 * @param value The value to store (ignored).
203 */
204static void Phy::regWriteUnimplemented(PPHY pPhy, uint32_t index, uint16_t u16Value)
205{
206 PhyLog(("PHY#%d At %02d write attempted to unimplemented '%s'\n",
207 pPhy->iInstance, s_regMap[index].u32Address, s_regMap[index].szName));
208}
209
210
211/**
212 * Search PHY register table for register with matching address.
213 *
214 * @returns Index in the register table or -1 if not found.
215 *
216 * @param u32Address Register address.
217 */
218static int Phy::lookupRegister(uint32_t u32Address)
219{
220 unsigned int index;
221
222 for (index = 0; index < RT_ELEMENTS(s_regMap); index++)
223 {
224 if (s_regMap[index].u32Address == u32Address)
225 {
226 return index;
227 }
228 }
229
230 return -1;
231}
232
233/**
234 * Read PHY register.
235 *
236 * @returns Value of specified PHY register.
237 *
238 * @param u32Address Register address.
239 */
240uint16_t Phy::readRegister(PPHY pPhy, uint32_t u32Address)
241{
242 int index = Phy::lookupRegister(u32Address);
243 uint16_t u16 = 0;
244
245 if (index != -1)
246 {
247 u16 = s_regMap[index].pfnRead(pPhy, index);
248 PhyLog(("PHY#%d At %02d read %04X from %s (%s)\n",
249 pPhy->iInstance, s_regMap[index].u32Address, u16,
250 s_regMap[index].szAbbrev, s_regMap[index].szName));
251 }
252 else
253 {
254 PhyLog(("PHY#%d read attempted from non-existing register %08x\n",
255 pPhy->iInstance, u32Address));
256 }
257 return u16;
258}
259
260/**
261 * Write to PHY register.
262 *
263 * @param u32Address Register address.
264 * @param u16Value Value to store.
265 */
266void Phy::writeRegister(PPHY pPhy, uint32_t u32Address, uint16_t u16Value)
267{
268 int index = Phy::lookupRegister(u32Address);
269
270 if (index != -1)
271 {
272 PhyLog(("PHY#%d At %02d write %04X to %s (%s)\n",
273 pPhy->iInstance, s_regMap[index].u32Address, u16Value,
274 s_regMap[index].szAbbrev, s_regMap[index].szName));
275 s_regMap[index].pfnWrite(pPhy, index, u16Value);
276 }
277 else
278 {
279 PhyLog(("PHY#%d write attempted to non-existing register %08x\n",
280 pPhy->iInstance, u32Address));
281 }
282}
283
284/**
285 * PHY contructor.
286 *
287 * Stores E1000 instance internally. Triggers PHY hard reset.
288 *
289 * @param iNICInstance Number of network controller instance this PHY is
290 * attached to.
291 * @param u16EPid Extended PHY Id.
292 */
293void Phy::init(PPHY pPhy, int iNICInstance, uint16_t u16EPid)
294{
295 pPhy->iInstance = iNICInstance;
296 /* Make sure the link is down */
297 REG(PSTATUS) = 0;
298 /* The PHY identifier composed of bits 3 through 18 of the OUI */
299 /* (Organizationally Unique Identifier). OUI is 0x05043. */
300 REG(PID) = 0x0141;
301 /* Extended PHY identifier */
302 REG(EPID) = u16EPid;
303 hardReset(pPhy);
304}
305
306/**
307 * Hardware PHY reset.
308 *
309 * Sets all PHY registers to their initial values.
310 */
311void Phy::hardReset(PPHY pPhy)
312{
313 PhyLog(("PHY#%d Hard reset\n", pPhy->iInstance));
314 REG(PCTRL) = PCTRL_SPDSELM | PCTRL_DUPMOD | PCTRL_ANEG;
315 /*
316 * 100 and 10 FD/HD, MF Preamble Suppresion, Auto-Negotiation Complete,
317 * AUTO NEG AB, EXT CAP
318 */
319 REG(PSTATUS) = (REG(PSTATUS) & ~PSTATUS_LNKSTAT) | 0x7969;
320 REG(ANA) = 0x01E1;
321 /* No flow control by our link partner, all speeds */
322 REG(LPA) = 0x01E0;
323 REG(ANE) = 0x0000;
324 REG(NPT) = 0x2001;
325 REG(LPN) = 0x0000;
326 REG(GCON) = 0x1E00;
327 REG(GSTATUS) = 0x0000;
328 REG(EPSTATUS) = 0x3000;
329 REG(PSCON) = 0x0068;
330 REG(PSSTAT) = 0x0000;
331 REG(PINTE) = 0x0000;
332 REG(PINTS) = 0x0000;
333 REG(EPSCON1) = 0x0D60;
334 REG(PREC) = 0x0000;
335 REG(EPSCON2) = 0x000C;
336 REG(R30PS) = 0x0000;
337 REG(R30AW) = 0x0000;
338
339 pPhy->u16State = MDIO_IDLE;
340}
341
342/**
343 * Software PHY reset.
344 */
345static void Phy::softReset(PPHY pPhy)
346{
347 PhyLog(("PHY#%d Soft reset is not yet implemented!\n", pPhy->iInstance));
348}
349
350/**
351 * Get the current state of the link.
352 *
353 * @returns true if link is up.
354 */
355bool Phy::isLinkUp(PPHY pPhy)
356{
357 return (REG(PSSTAT) & PSSTAT_LINK) != 0;
358}
359
360/**
361 * Set the current state of the link.
362 *
363 * @remarks Link Status bit in PHY Status register is latched-low and does
364 * not change the state when the link goes up.
365 *
366 * @param fLinkIsUp New state of the link.
367 */
368void Phy::setLinkStatus(PPHY pPhy, bool fLinkIsUp)
369{
370 if (fLinkIsUp)
371 REG(PSSTAT) |= PSSTAT_LINK;
372 else
373 {
374 REG(PSSTAT) &= ~PSSTAT_LINK;
375 REG(PSTATUS) &= ~PSTATUS_LNKSTAT;
376 }
377}
378
379#ifdef IN_RING3
380/**
381 * Save PHY state.
382 *
383 * @remarks Since PHY is agregated into E1K it does not currently supports
384 * versioning of its own.
385 *
386 * @returns VBox status code.
387 * @param pSSMHandle The handle to save the state to.
388 * @param pPhy The pointer to this instance.
389 */
390int Phy::saveState(PSSMHANDLE pSSMHandle, PPHY pPhy)
391{
392 SSMR3PutMem(pSSMHandle, pPhy->au16Regs, sizeof(pPhy->au16Regs));
393 return VINF_SUCCESS;
394}
395
396/**
397 * Restore previously saved PHY state.
398 *
399 * @remarks Since PHY is agregated into E1K it does not currently supports
400 * versioning of its own.
401 *
402 * @returns VBox status code.
403 * @param pSSMHandle The handle to save the state to.
404 * @param pPhy The pointer to this instance.
405 */
406int Phy::loadState(PSSMHANDLE pSSMHandle, PPHY pPhy)
407{
408 return SSMR3GetMem(pSSMHandle, pPhy->au16Regs, sizeof(pPhy->au16Regs));
409}
410#endif /* IN_RING3 */
411
412/* Register-specific handlers ************************************************/
413
414/**
415 * Write handler for PHY Control register.
416 *
417 * Handles reset.
418 *
419 * @param index Register index in register array.
420 * @param value The value to store (ignored).
421 */
422static void Phy::regWritePCTRL(PPHY pPhy, uint32_t index, uint16_t u16Value)
423{
424 if (u16Value & PCTRL_RESET)
425 softReset(pPhy);
426 else
427 regWriteDefault(pPhy, index, u16Value);
428}
429
430/**
431 * Read handler for PHY Status register.
432 *
433 * Handles Latched-Low Link Status bit.
434 *
435 * @returns Register value
436 *
437 * @param index Register index in register array.
438 */
439static uint16_t Phy::regReadPSTATUS(PPHY pPhy, uint32_t index)
440{
441 /* Read latched value */
442 uint16_t u16 = REG(PSTATUS);
443 if (REG(PSSTAT) & PSSTAT_LINK)
444 REG(PSTATUS) |= PSTATUS_LNKSTAT;
445 else
446 REG(PSTATUS) &= ~PSTATUS_LNKSTAT;
447 return u16;
448}
449
450/**
451 * Read handler for 1000BASE-T Status register.
452 *
453 * @returns Register value
454 *
455 * @param index Register index in register array.
456 */
457static uint16_t Phy::regReadGSTATUS(PPHY pPhy, uint32_t index)
458{
459 /*
460 * - Link partner is capable of 1000BASE-T half duplex
461 * - Link partner is capable of 1000BASE-T full duplex
462 * - Remote receiver OK
463 * - Local receiver OK
464 * - Local PHY config resolved to SLAVE
465 */
466 return 0x3C00;
467}
468
469static const char * Phy::getStateName(uint16_t u16State)
470{
471 static const char *pcszState[] =
472 {
473 "MDIO_IDLE",
474 "MDIO_ST",
475 "MDIO_OP_ADR",
476 "MDIO_TA_RD",
477 "MDIO_TA_WR",
478 "MDIO_READ",
479 "MDIO_WRITE"
480 };
481
482 return (u16State < RT_ELEMENTS(pcszState)) ? pcszState[u16State] : "<invalid>";
483}
484
485bool Phy::readMDIO(PPHY pPhy)
486{
487 bool fPin = false;
488
489 switch (pPhy->u16State)
490 {
491 case MDIO_TA_RD:
492 Assert(pPhy->u16Cnt == 1);
493 fPin = false;
494 pPhy->u16State = MDIO_READ;
495 pPhy->u16Cnt = 16;
496 break;
497 case MDIO_READ:
498 /* Bits are shifted out in MSB to LSB order */
499 fPin = (pPhy->u16Acc & 0x8000) != 0;
500 pPhy->u16Acc <<= 1;
501 if (--pPhy->u16Cnt == 0)
502 pPhy->u16State = MDIO_IDLE;
503 break;
504 default:
505 PhyLog(("PHY#%d WARNING! MDIO pin read in %s state\n", pPhy->iInstance, Phy::getStateName(pPhy->u16State)));
506 pPhy->u16State = MDIO_IDLE;
507 }
508 return fPin;
509}
510
511/** Set the value of MDIO pin. */
512void Phy::writeMDIO(PPHY pPhy, bool fPin)
513{
514 switch (pPhy->u16State)
515 {
516 case MDIO_IDLE:
517 if (!fPin)
518 pPhy->u16State = MDIO_ST;
519 break;
520 case MDIO_ST:
521 if (fPin)
522 {
523 pPhy->u16State = MDIO_OP_ADR;
524 pPhy->u16Cnt = 12; /* OP + PHYADR + REGADR */
525 pPhy->u16Acc = 0;
526 }
527 break;
528 case MDIO_OP_ADR:
529 Assert(pPhy->u16Cnt);
530 /* Shift in 'u16Cnt' bits into accumulator */
531 pPhy->u16Acc <<= 1;
532 if (fPin)
533 pPhy->u16Acc |= 1;
534 if (--pPhy->u16Cnt == 0)
535 {
536 /* Got OP(2) + PHYADR(5) + REGADR(5) */
537 /* Note: A single PHY is supported, ignore PHYADR */
538 switch (pPhy->u16Acc >> 10)
539 {
540 case MDIO_READ_OP:
541 pPhy->u16Acc = readRegister(pPhy, pPhy->u16Acc & 0x1F);
542 pPhy->u16State = MDIO_TA_RD;
543 pPhy->u16Cnt = 1;
544 break;
545 case MDIO_WRITE_OP:
546 pPhy->u16RegAdr = pPhy->u16Acc & 0x1F;
547 pPhy->u16State = MDIO_TA_WR;
548 pPhy->u16Cnt = 2;
549 break;
550 default:
551 PhyLog(("PHY#%d ERROR! Invalid MDIO op: %d\n", pPhy->iInstance, pPhy->u16Acc >> 10));
552 pPhy->u16State = MDIO_IDLE;
553 break;
554 }
555 }
556 break;
557 case MDIO_TA_WR:
558 Assert(pPhy->u16Cnt <= 2);
559 Assert(pPhy->u16Cnt > 0);
560 if (--pPhy->u16Cnt == 0)
561 {
562 pPhy->u16State = MDIO_WRITE;
563 pPhy->u16Cnt = 16;
564 }
565 break;
566 case MDIO_WRITE:
567 Assert(pPhy->u16Cnt);
568 pPhy->u16Acc <<= 1;
569 if (fPin)
570 pPhy->u16Acc |= 1;
571 if (--pPhy->u16Cnt == 0)
572 {
573 writeRegister(pPhy, pPhy->u16RegAdr, pPhy->u16Acc);
574 pPhy->u16State = MDIO_IDLE;
575 }
576 break;
577 default:
578 PhyLog(("PHY#%d ERROR! MDIO pin write in %s state\n", pPhy->iInstance, Phy::getStateName(pPhy->u16State)));
579 pPhy->u16State = MDIO_IDLE;
580 break;
581 }
582}
583
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette