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