1 | /* $Id: tstDevPhy.cpp 98172 2023-01-21 13:01:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * PHY MDIO unit tests.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #ifdef USE_CPPUNIT
|
---|
33 | # include <cppunit/ui/text/TestRunner.h>
|
---|
34 | # include <cppunit/extensions/HelperMacros.h>
|
---|
35 | #else
|
---|
36 | # include "CppUnitEmulation.h"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 | #include "../DevE1000Phy.h"
|
---|
40 |
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Test fixture for PHY MDIO/MDC interface emulation.
|
---|
44 | */
|
---|
45 | class PhyTest
|
---|
46 | #ifdef USE_CPPUNIT
|
---|
47 | : public CppUnit::TestFixture
|
---|
48 | #endif
|
---|
49 | {
|
---|
50 | CPPUNIT_TEST_SUITE( tstDevPhy );
|
---|
51 |
|
---|
52 | CPPUNIT_TEST(testSize);
|
---|
53 | CPPUNIT_TEST(testReadPID);
|
---|
54 | CPPUNIT_TEST(testReadEPID);
|
---|
55 | CPPUNIT_TEST(testWriteANA);
|
---|
56 |
|
---|
57 | CPPUNIT_TEST_SUITE_END();
|
---|
58 |
|
---|
59 | private:
|
---|
60 | enum Op
|
---|
61 | {
|
---|
62 | WRITE_OP = 0x1,
|
---|
63 | READ_OP = 0x2
|
---|
64 | };
|
---|
65 |
|
---|
66 | #define PHYADR_VAL (uint16_t)0
|
---|
67 | #define ST_VAL (uint16_t)1
|
---|
68 | #define TA_VAL (uint16_t)2
|
---|
69 | #define PREAMBLE_VAL 0xFFFFFFFF
|
---|
70 |
|
---|
71 | enum BitWidths {
|
---|
72 | ST_BITS = 2,
|
---|
73 | OP_BITS = 2,
|
---|
74 | PHYADR_BITS = 5,
|
---|
75 | REGADR_BITS = 5,
|
---|
76 | TA_BITS = 2,
|
---|
77 | DATA_BITS = 16,
|
---|
78 | PREAMBLE_BITS = 32
|
---|
79 | };
|
---|
80 |
|
---|
81 | PPHY phy;
|
---|
82 |
|
---|
83 | // Helper methods
|
---|
84 | void shiftOutBits(uint32_t data, uint16_t count);
|
---|
85 | uint16_t shiftInBits(uint16_t count);
|
---|
86 | int readAt(uint16_t addr);
|
---|
87 | void writeTo(uint16_t addr, uint32_t value);
|
---|
88 |
|
---|
89 | public:
|
---|
90 | void setUp()
|
---|
91 | {
|
---|
92 | phy = new PHY;
|
---|
93 | Phy::init(phy, 0, PHY_EPID_M881000);
|
---|
94 | }
|
---|
95 |
|
---|
96 | void tearDown()
|
---|
97 | {
|
---|
98 | delete phy;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void testSize()
|
---|
102 | {
|
---|
103 | CPPUNIT_ASSERT_EQUAL(32, ST_BITS+OP_BITS+PHYADR_BITS+REGADR_BITS+TA_BITS+DATA_BITS);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void testReadPID()
|
---|
107 | {
|
---|
108 | CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
|
---|
109 | }
|
---|
110 |
|
---|
111 | void testReadEPID()
|
---|
112 | {
|
---|
113 | CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
|
---|
114 | CPPUNIT_ASSERT_EQUAL(PHY_EPID_M881000, readAt(3));
|
---|
115 | }
|
---|
116 |
|
---|
117 | void testWriteANA()
|
---|
118 | {
|
---|
119 | writeTo(4, 0xBEEF);
|
---|
120 | CPPUNIT_ASSERT_EQUAL(0xBEEF, readAt(4));
|
---|
121 | }
|
---|
122 |
|
---|
123 | };
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * shiftOutBits - Shift data bits our to MDIO
|
---|
127 | **/
|
---|
128 | void PhyTest::shiftOutBits(uint32_t data, uint16_t count) {
|
---|
129 | uint32_t mask = 0x01 << (count - 1);
|
---|
130 |
|
---|
131 | do {
|
---|
132 | Phy::writeMDIO(phy, data & mask, NULL /*pDevIns*/);
|
---|
133 | mask >>= 1;
|
---|
134 | } while (mask);
|
---|
135 | }
|
---|
136 |
|
---|
137 | /**
|
---|
138 | * shiftInBits - Shift data bits in from MDIO
|
---|
139 | **/
|
---|
140 | uint16_t PhyTest::shiftInBits(uint16_t count)
|
---|
141 | {
|
---|
142 | uint16_t data = 0;
|
---|
143 |
|
---|
144 | while (count--)
|
---|
145 | {
|
---|
146 | data <<= 1;
|
---|
147 | data |= Phy::readMDIO(phy) ? 1 : 0;
|
---|
148 | }
|
---|
149 |
|
---|
150 | return data;
|
---|
151 | }
|
---|
152 |
|
---|
153 | int PhyTest::readAt(uint16_t addr)
|
---|
154 | {
|
---|
155 | shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
|
---|
156 |
|
---|
157 | shiftOutBits(ST_VAL, ST_BITS);
|
---|
158 | shiftOutBits(READ_OP, OP_BITS);
|
---|
159 | shiftOutBits(PHYADR_VAL, PHYADR_BITS);
|
---|
160 | shiftOutBits(addr, REGADR_BITS);
|
---|
161 |
|
---|
162 | CPPUNIT_ASSERT_EQUAL((uint16_t)0, shiftInBits(1));
|
---|
163 | uint16_t u16Data = shiftInBits(DATA_BITS);
|
---|
164 | shiftInBits(1);
|
---|
165 | return u16Data;
|
---|
166 | }
|
---|
167 |
|
---|
168 | void PhyTest::writeTo(uint16_t addr, uint32_t value)
|
---|
169 | {
|
---|
170 | shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
|
---|
171 |
|
---|
172 | shiftOutBits(ST_VAL, ST_BITS);
|
---|
173 | shiftOutBits(WRITE_OP, OP_BITS);
|
---|
174 | shiftOutBits(PHYADR_VAL, PHYADR_BITS);
|
---|
175 | shiftOutBits(addr, REGADR_BITS);
|
---|
176 | shiftOutBits(TA_VAL, TA_BITS);
|
---|
177 | shiftOutBits(value, DATA_BITS);
|
---|
178 | }
|
---|
179 |
|
---|
180 | // Create text test runner and run all tests.
|
---|
181 | int main()
|
---|
182 | {
|
---|
183 | #ifdef USE_CPPUNIT
|
---|
184 | CppUnit::TextUi::TestRunner runner;
|
---|
185 | runner.addTest( PhyTest::suite() );
|
---|
186 | return runner.run() ? 0 : 1;
|
---|
187 | #else
|
---|
188 | PhyTest Test;
|
---|
189 | return Test.run();
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 |
|
---|