1 | /* $Id: tstDevPhy.cpp 31880 2010-08-24 07:08:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PHY MDIO unit tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * Oracle Corporation confidential
|
---|
10 | * All rights reserved
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include <cppunit/ui/text/TestRunner.h>
|
---|
14 | #include <cppunit/extensions/HelperMacros.h>
|
---|
15 |
|
---|
16 | #include "../DevE1000Phy.h"
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Test fixture for PHY MDIO/MDC interface emulation.
|
---|
20 | */
|
---|
21 | class PhyTest : public CppUnit::TestFixture {
|
---|
22 | CPPUNIT_TEST_SUITE( PhyTest );
|
---|
23 |
|
---|
24 | CPPUNIT_TEST(testSize);
|
---|
25 | CPPUNIT_TEST(testReadPID);
|
---|
26 | CPPUNIT_TEST(testReadEPID);
|
---|
27 | CPPUNIT_TEST(testWriteANA);
|
---|
28 |
|
---|
29 | CPPUNIT_TEST_SUITE_END();
|
---|
30 |
|
---|
31 | private:
|
---|
32 | enum Op
|
---|
33 | {
|
---|
34 | WRITE_OP = 0x1,
|
---|
35 | READ_OP = 0x2
|
---|
36 | };
|
---|
37 |
|
---|
38 | #define PHYADR_VAL (uint16_t)0
|
---|
39 | #define ST_VAL (uint16_t)1
|
---|
40 | #define TA_VAL (uint16_t)2
|
---|
41 | #define PREAMBLE_VAL 0xFFFFFFFF
|
---|
42 |
|
---|
43 | enum BitWidths {
|
---|
44 | ST_BITS = 2,
|
---|
45 | OP_BITS = 2,
|
---|
46 | PHYADR_BITS = 5,
|
---|
47 | REGADR_BITS = 5,
|
---|
48 | TA_BITS = 2,
|
---|
49 | DATA_BITS = 16,
|
---|
50 | PREAMBLE_BITS = 32
|
---|
51 | };
|
---|
52 |
|
---|
53 | PPHY phy;
|
---|
54 |
|
---|
55 | // Helper methods
|
---|
56 | void shiftOutBits(uint32_t data, uint16_t count);
|
---|
57 | uint16_t shiftInBits(uint16_t count);
|
---|
58 | int readAt(uint16_t addr);
|
---|
59 | void writeTo(uint16_t addr, uint32_t value);
|
---|
60 |
|
---|
61 | public:
|
---|
62 | void setUp()
|
---|
63 | {
|
---|
64 | phy = new PHY;
|
---|
65 | Phy::init(phy, 0, PHY_EPID_M881000);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void tearDown()
|
---|
69 | {
|
---|
70 | delete phy;
|
---|
71 | }
|
---|
72 |
|
---|
73 | void testSize()
|
---|
74 | {
|
---|
75 | CPPUNIT_ASSERT_EQUAL(32, ST_BITS+OP_BITS+PHYADR_BITS+REGADR_BITS+TA_BITS+DATA_BITS);
|
---|
76 | }
|
---|
77 |
|
---|
78 | void testReadPID()
|
---|
79 | {
|
---|
80 | CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
|
---|
81 | }
|
---|
82 |
|
---|
83 | void testReadEPID()
|
---|
84 | {
|
---|
85 | CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
|
---|
86 | CPPUNIT_ASSERT_EQUAL(PHY_EPID_M881000, readAt(3));
|
---|
87 | }
|
---|
88 |
|
---|
89 | void testWriteANA()
|
---|
90 | {
|
---|
91 | writeTo(4, 0xBEEF);
|
---|
92 | CPPUNIT_ASSERT_EQUAL(0xBEEF, readAt(4));
|
---|
93 | }
|
---|
94 |
|
---|
95 | };
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * shiftOutBits - Shift data bits our to MDIO
|
---|
99 | **/
|
---|
100 | void PhyTest::shiftOutBits(uint32_t data, uint16_t count) {
|
---|
101 | uint32_t mask = 0x01 << (count - 1);
|
---|
102 |
|
---|
103 | do {
|
---|
104 | Phy::writeMDIO(phy, data & mask);
|
---|
105 | mask >>= 1;
|
---|
106 | } while (mask);
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * shiftInBits - Shift data bits in from MDIO
|
---|
111 | **/
|
---|
112 | uint16_t PhyTest::shiftInBits(uint16_t count)
|
---|
113 | {
|
---|
114 | uint16_t data = 0;
|
---|
115 |
|
---|
116 | while (count--)
|
---|
117 | {
|
---|
118 | data <<= 1;
|
---|
119 | data |= Phy::readMDIO(phy) ? 1 : 0;
|
---|
120 | }
|
---|
121 |
|
---|
122 | return data;
|
---|
123 | }
|
---|
124 |
|
---|
125 | int PhyTest::readAt(uint16_t addr)
|
---|
126 | {
|
---|
127 | shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
|
---|
128 |
|
---|
129 | shiftOutBits(ST_VAL, ST_BITS);
|
---|
130 | shiftOutBits(READ_OP, OP_BITS);
|
---|
131 | shiftOutBits(PHYADR_VAL, PHYADR_BITS);
|
---|
132 | shiftOutBits(addr, REGADR_BITS);
|
---|
133 |
|
---|
134 | CPPUNIT_ASSERT_EQUAL((uint16_t)0, shiftInBits(1));
|
---|
135 | uint16_t u16Data = shiftInBits(DATA_BITS);
|
---|
136 | shiftInBits(1);
|
---|
137 | return u16Data;
|
---|
138 | }
|
---|
139 |
|
---|
140 | void PhyTest::writeTo(uint16_t addr, uint32_t value)
|
---|
141 | {
|
---|
142 | shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
|
---|
143 |
|
---|
144 | shiftOutBits(ST_VAL, ST_BITS);
|
---|
145 | shiftOutBits(WRITE_OP, OP_BITS);
|
---|
146 | shiftOutBits(PHYADR_VAL, PHYADR_BITS);
|
---|
147 | shiftOutBits(addr, REGADR_BITS);
|
---|
148 | shiftOutBits(TA_VAL, TA_BITS);
|
---|
149 | shiftOutBits(value, DATA_BITS);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Create text test runner and run all tests.
|
---|
153 | int main( int argc, char **argv)
|
---|
154 | {
|
---|
155 | CppUnit::TextUi::TestRunner runner;
|
---|
156 | runner.addTest( PhyTest::suite() );
|
---|
157 | return runner.run() ? 0 : 1;
|
---|
158 | }
|
---|
159 |
|
---|