Files
templates/hdl/logic-udp-ethernet/rtl/rtl8211f_init.v
T

106 lines
3.5 KiB
Verilog

`timescale 1ns / 1ps
// Configure PHY2 at MDIO address 1 for PHY-provided RX and TX clock delays.
module rtl8211f_init (
input wire clk,
input wire reset,
output wire phy_reset_n,
output reg mdc,
input wire mdio_i,
output wire mdio_o,
output wire mdio_t,
output reg ready
);
// At 125 MHz, 50 cycles give the PHY 400 ns to return each MDIO bit.
// RTL8211F can take up to 300 ns after MDC rises.
reg [22:0] boot_wait;
reg [5:0] phase;
reg [3:0] step;
reg [6:0] bits_left;
reg [63:0] frame;
reg reading;
reg [15:0] read_data;
reg read_ack;
reg [15:0] tx_control, rx_control;
wire waiting = boot_wait < 7500000;
assign phy_reset_n = !reset && boot_wait >= 1250000;
assign mdio_o = frame[63];
assign mdio_t = bits_left == 0 || (reading && bits_left <= 18);
task transaction;
input is_read;
input [4:0] addr;
input [15:0] value;
begin
frame <= {32'hffffffff, 2'b01, (is_read ? 2'b10 : 2'b01),
5'd1, addr, 2'b10, value};
reading <= is_read;
bits_left <= 64;
phase <= 0;
read_ack <= 0;
end
endtask
always @(posedge clk) begin
if (reset) begin
boot_wait <= 0;
phase <= 0;
step <= 0;
bits_left <= 0;
frame <= 0;
reading <= 0;
read_data <= 0;
read_ack <= 0;
tx_control <= 0;
rx_control <= 0;
mdc <= 0;
ready <= 0;
end else if (waiting) begin
boot_wait <= boot_wait + 1'b1;
end else if (bits_left != 0) begin
if (phase == 49) begin
phase <= 0;
mdc <= !mdc;
if (mdc) begin
if (reading) begin
// PHY launches ACK during the first turnaround clock.
// The final clock lets it release the bus before the next preamble.
if (bits_left == 18)
read_ack <= !mdio_i;
else if (bits_left >= 2 && bits_left <= 17)
read_data <= {read_data[14:0], mdio_i};
end
bits_left <= bits_left - 1'b1;
frame <= frame << 1;
if (bits_left == 1)
step <= step + 1'b1;
end
end else begin
phase <= phase + 1'b1;
end
end else begin
case (step)
0: transaction(0, 5'h1f, 16'h0d08);
1: transaction(1, 5'h11, 0);
2: if (read_ack) begin
tx_control <= read_data | 16'h0100;
transaction(0, 5'h11, read_data | 16'h0100);
end else step <= 0;
3: transaction(1, 5'h15, 0);
4: if (read_ack) begin
rx_control <= read_data | 16'h0008;
transaction(0, 5'h15, read_data | 16'h0008);
end else step <= 0;
5: transaction(1, 5'h11, 0);
6: if (read_ack && read_data == tx_control)
transaction(1, 5'h15, 0);
else step <= 0;
7: if (read_ack && read_data == rx_control)
transaction(0, 5'h1f, 0);
else step <= 0;
8: ready <= 1;
default: step <= 0;
endcase
end
end
endmodule