-
Notifications
You must be signed in to change notification settings - Fork 13
/
APB_MDIO.sv
241 lines (180 loc) · 7.67 KB
/
APB_MDIO.sv
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
`timescale 1ns/1ps
`default_nettype none
/***********************************************************************************************************************
* *
* ANTIKERNEL *
* *
* Copyright (c) 2012-2024 Andrew D. Zonenberg *
* All rights reserved. *
* *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the *
* following conditions are met: *
* *
* * Redistributions of source code must retain the above copyright notice, this list of conditions, and the *
* following disclaimer. *
* *
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the *
* following disclaimer in the documentation and/or other materials provided with the distribution. *
* *
* * Neither the name of the author nor the names of any contributors may be used to endorse or promote products *
* derived from this software without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
* *
***********************************************************************************************************************/
/**
@file
@author Andrew D. Zonenberg
@brief APB register access wrapper around a MDIO controller
Write process:
* verify STATUS is zero
* write register / PHY ID to CMD_ADDR and set write bit
* write register value to DATA
Read process:
* verify STATUS is zero
* write register / PHY ID to CMD_ADDR
* wait for STATUS to be zero
* read register value from DATA
Reads from DATA may return stale or garbage data if BUSY is set, but will not stall the bus.
*/
module APB_MDIO #(
parameter CLK_DIV = 16 //clock divider from PCLK to MDIO clock domain
) (
//The APB bus
APB.completer apb,
//The MDIO bus
inout wire mdio,
output wire mdc
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// We only support 16 or 32-bit APB, throw synthesis error for anything else
if( (apb.DATA_WIDTH != 16) && (apb.DATA_WIDTH != 32) )
apb_bus_width_is_invalid();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Tie off unused APB signals
assign apb.pruser = 0;
assign apb.pbuser = 0;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Register map
//Align all writable registers to 0x20 boundaries to work around STM32H7 OCTOSPI bugs
typedef enum logic[apb.ADDR_WIDTH-1:0]
{
CMD_ADDR = 'h00, //15 1=write, 0=read (always RAZ)
//12:8 Register address
//4:0 PHY address
DATA = 'h20, //Read/write data
STATUS = 'h40, //Busy flag
STATUS2 = 'h60 //Duplicate of STATUS at 0x20, 32 bytes offset
//(workaround for inability to disable prefetch cache on STM32H7)
} regid_t;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The MDIO transceiver itself
wire mdio_tx_data;
wire mdio_tx_en;
wire mdio_rx_data;
BidirectionalBuffer mdio_obuf(
.fabric_in(mdio_rx_data),
.fabric_out(mdio_tx_data),
.pad(mdio),
.oe(mdio_tx_en)
);
logic[4:0] phy_reg_addr;
logic[15:0] phy_wr_data;
wire[15:0] phy_rd_data;
logic phy_reg_wr;
logic phy_reg_rd;
logic[4:0] phy_md_addr;
wire mdio_busy;
EthernetMDIOTransceiver #(
.CLK_DIV(CLK_DIV)
) mdio_txvr (
.clk(apb.pclk),
.phy_md_addr(phy_md_addr),
.mdio_tx_data(mdio_tx_data),
.mdio_tx_en(mdio_tx_en),
.mdio_rx_data(mdio_rx_data),
.mdc(mdc),
.mgmt_busy_fwd(mdio_busy),
.phy_reg_addr(phy_reg_addr),
.phy_wr_data(phy_wr_data),
.phy_rd_data(phy_rd_data),
.phy_reg_wr(phy_reg_wr),
.phy_reg_rd(phy_reg_rd)
);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// APB interface logic
always_comb begin
//Combinatorially assert PREADY when selected
apb.pready = apb.psel && apb.penable;
//Default to no errors and no read data
apb.prdata = 0;
apb.pslverr = 0;
if(apb.pready) begin
//Write path
if(apb.pwrite) begin
case(apb.paddr)
//Command and address
CMD_ADDR: begin
end
DATA: begin
end
//invalid address
default: apb.pslverr = 1;
endcase
end
//Read data muxing
else begin
case(apb.paddr)
CMD_ADDR: apb.prdata = {4'h0, phy_reg_addr, 3'h0, phy_md_addr};
DATA: apb.prdata = phy_rd_data;
STATUS: apb.prdata = { 15'h0, mdio_busy };
STATUS2: apb.prdata = { 15'h0, mdio_busy };
//unmapped address, error
default: apb.pslverr = 1;
endcase
end
end
end
always_ff @(posedge apb.pclk or negedge apb.preset_n) begin
//Reset
if(!apb.preset_n) begin
phy_reg_wr <= 0;
phy_reg_rd <= 0;
phy_md_addr <= 0;
phy_reg_addr <= 0;
end
//Normal path
else begin
//Clear MDIO control signals
phy_reg_wr <= 0;
phy_reg_rd <= 0;
//Registered writes
if(apb.pready && apb.pwrite) begin
case(apb.paddr)
//Command and address
CMD_ADDR: begin
//start read operation on write with MSB clear
if(!apb.pwdata[15])
phy_reg_rd <= 1;
//extract address
phy_reg_addr <= apb.pwdata[12:8];
phy_md_addr <= apb.pwdata[4:0];
end
DATA: begin
//start write operation
phy_reg_wr <= 1;
phy_wr_data <= apb.pwdata;
end
default: begin
end
endcase
end
end
end
endmodule