diff options
Diffstat (limited to 'src/mac_rgmii.v')
| -rw-r--r-- | src/mac_rgmii.v | 254 |
1 files changed, 199 insertions, 55 deletions
diff --git a/src/mac_rgmii.v b/src/mac_rgmii.v index b6ae629..0a1109f 100644 --- a/src/mac_rgmii.v +++ b/src/mac_rgmii.v @@ -20,11 +20,12 @@ * */ -module mac_rgmii #(parameter PHY_NUM=0) ( +module mac_rgmii #(parameter PHY_NUM=0, MLE_EOD_FLAG=0) ( input rstn, input phy_resetn, // The external PHY has its reset signal asserted - input rx_clk, // rx_clk + input rx_clk, // input tx_clk, + input mle_clk, input tap_port, // Control Interface @@ -37,12 +38,10 @@ module mac_rgmii #(parameter PHY_NUM=0) ( output cont_tgt_ready, // ML Engine Interface - input mle_active, + output reg mle_if_empty, input mle_if_enable, - input mle_if_oe, output reg mle_if_we, - output reg mle_if_empty, - output [8:0] mle_if_d_o, + output reg [8:0] mle_if_d_o, // Line State input fixed_speed, // 0 = 100 MBit, 1 = GigE @@ -76,7 +75,7 @@ module mac_rgmii #(parameter PHY_NUM=0) ( // MAC RX / FIFO Write output rx_fifo_we, output [8:0] rx_fifo_d, - output rx_keep, + output reg rx_trigger, output reg rx_wr_done, output reg [10:0] rx_byte_cnt, output reg rx_idle, @@ -150,6 +149,12 @@ module mac_rgmii #(parameter PHY_NUM=0) ( TX_ST_EOP=4'h8, TX_ST_9=4'h9, TX_ST_A=4'ha, TX_ST_B=4'hb, TX_ST_C=4'hc, TX_ST_D=4'hd, TX_ST_E=4'he, TX_ST_F=4'hf; + localparam MAX_IPG_CNT = 16'hffff; + + localparam MLE_ST_IDLE = 3'h0, MLE_ST_WAIT = 3'h1, MLE_ST_WR_ID = 3'h2, MLE_ST_WR_CODE = 3'h3, MLE_ST_XFER = 3'h4, MLE_ST_DONE = 3'h5; + localparam MLE_CNT_START_XFER = 6'd4; + localparam MLE_CNT_DONE = 6'd10; + // Controller Address Space localparam RX_PKT_CNT_ADDR = 16'h0000, @@ -194,12 +199,25 @@ module mac_rgmii #(parameter PHY_NUM=0) ( reg [2:0] rx_enet_bcast_cnt; // ML Engine - reg [3:0] mle_if_cnt; - reg [8:0] mle_if_d; + // rx_clk domain: + reg mle_enable_m1, mle_enable_m2; + reg mle_start; + reg [7:0] mle_pkt_cnt, mle_pkt_cnt_o; + reg [31:0] mle_rx_byte_cnt, mle_rx_byte_cnt_o; + reg [31:0] mle_rx_ipg_cnt, mle_rx_ipg_cnt_o; + + // mle_clk domain: + reg mle_start_m1, mle_start_m2; + reg [2:0] mle_state; + reg mle_rx_eop_m1, mle_rx_eop_m2; + reg [5:0] mle_cnt; - // Metrics - reg [15:0] rx_pkt_cnt; - reg [15:0] tx_pkt_cnt; + // Controller Mapped Metrics + reg [15:0] rx_cont_pkt_cnt; + reg [15:0] tx_cont_pkt_cnt; + + // MLE Metrics + reg [15:0] rx_ipg_cnt; /* @@ -215,8 +233,8 @@ module mac_rgmii #(parameter PHY_NUM=0) ( cont_d_o <= 16'hcccc; else case (cont_addr) - RX_PKT_CNT_ADDR: cont_d_o <= rx_pkt_cnt; - TX_PKT_CNT_ADDR: cont_d_o <= tx_pkt_cnt; + RX_PKT_CNT_ADDR: cont_d_o <= rx_cont_pkt_cnt; + TX_PKT_CNT_ADDR: cont_d_o <= tx_cont_pkt_cnt; default: cont_d_o <= cont_d_o; endcase @@ -228,46 +246,158 @@ module mac_rgmii #(parameter PHY_NUM=0) ( * ML Engine Interface * */ - - // mle_if_cnt: - // TODO: write cnt at end, rework this logic and make it more meaningful - always @(posedge tx_clk, negedge rstn) + + // Meta regs for mle_if_enable + always @(posedge rx_clk, negedge rstn) + if (!rstn) begin + mle_enable_m1 <= 1'b0; + mle_enable_m2 <= 1'b0; + end + else begin + mle_enable_m1 <= mle_if_enable; + mle_enable_m2 <= mle_enable_m1; + end + + // mle_start: one shot to trigger data exchange and locking between CDC domains + always @(posedge rx_clk, negedge rstn) + if(!rstn) + mle_start <= 1'b0; + else if (!mle_enable_m2 && mle_enable_m1) + mle_start <= 1'b1; + else + mle_start <= 1'b0; + + // capture byte cnt (for now). + always @(posedge rx_clk, negedge rstn) if(!rstn) begin - mle_if_cnt <= 4'd0; - mle_if_empty <= 1'b1; - mle_if_we <= 1'b0; + mle_pkt_cnt <= 'd0; + mle_rx_ipg_cnt <= 'd0; + mle_rx_byte_cnt <= 'd0; + end + else if (rx_eop && mle_start) begin + mle_pkt_cnt <= 'd1; + mle_rx_ipg_cnt <= rx_ipg_cnt; + mle_rx_byte_cnt <= rx_byte_cnt; end else if (rx_eop) begin - mle_if_cnt <= 4'd4; - mle_if_empty <= 1'b0; - mle_if_we <= 1'b0; + mle_pkt_cnt <= mle_pkt_cnt + 1'b1; + mle_rx_ipg_cnt <= mle_rx_ipg_cnt + rx_ipg_cnt; + mle_rx_byte_cnt <= rx_byte_cnt + mle_rx_byte_cnt; end - else if (mle_if_cnt > 4'd0 && mle_if_enable) begin - mle_if_cnt <= mle_if_cnt - 1'b1; - mle_if_empty <= 1'b0; - mle_if_we <= 1'b1; + else if (mle_start) begin + mle_pkt_cnt <= 'd0; + mle_rx_ipg_cnt <= 'd0; + mle_rx_byte_cnt <= 'd0; + end + + // update and lock the data, so it can be clocked out by the MLE + always @(posedge rx_clk, negedge rstn) + if(!rstn) begin + mle_pkt_cnt_o <= 'd0; + mle_rx_ipg_cnt_o <= 'd0; + mle_rx_byte_cnt_o <= 'd0; + end + else if (mle_start) begin + mle_pkt_cnt_o <= mle_pkt_cnt; + mle_rx_ipg_cnt_o <= mle_rx_ipg_cnt; + mle_rx_byte_cnt_o <= mle_rx_byte_cnt; + end + + // Meta regs for mle_start + always @(posedge mle_clk, negedge rstn) + if (!rstn) begin + mle_start_m1 <= 1'b0; + mle_start_m2 <= 1'b0; + end + else begin + mle_start_m1 <= mle_start; + mle_start_m2 <= mle_start_m1; end - else if (mle_if_cnt == 'd0 && mle_if_enable) begin + + // mle_state: state machine for transferring out data to the MLE + always @(posedge mle_clk, negedge rstn) + if (!rstn) + mle_state <= MLE_ST_IDLE; + else + case(mle_state) + MLE_ST_IDLE: if (mle_start_m2) + mle_state <= MLE_ST_WAIT; + MLE_ST_WAIT: if (mle_cnt == MLE_CNT_START_XFER) + mle_state <= MLE_ST_WR_ID; + MLE_ST_WR_ID: + mle_state <= MLE_ST_WR_CODE; + MLE_ST_WR_CODE: + mle_state <= MLE_ST_XFER; + MLE_ST_XFER: if (mle_cnt == MLE_CNT_DONE) + mle_state <= MLE_ST_DONE; + MLE_ST_DONE: + mle_state <= MLE_ST_IDLE; + default: + mle_state <= mle_state; + endcase + + + // mle_cnt: + always @(posedge mle_clk, negedge rstn) + if(!rstn) + mle_cnt <= 4'd0; + else if (mle_start_m2) + mle_cnt <= 4'd0; + else if (mle_state != MLE_ST_IDLE && mle_cnt <= MLE_CNT_DONE) + mle_cnt <= mle_cnt + 1'b1; + + // mle_rx_eop: CDC one shot in the tx_clock domain + always @(posedge mle_clk, negedge rstn) + if (!rstn) begin + mle_rx_eop_m1 <= 1'b0; + mle_rx_eop_m2 <= 1'b0; + end + else begin + mle_rx_eop_m1 <= rx_eop; + mle_rx_eop_m2 <= mle_rx_eop_m1; + end + + // mle_if_empty: indicate we have data once a packet has finished being received + always @(posedge tx_clk, negedge rstn) + if(!rstn) mle_if_empty <= 1'b1; + else if (mle_rx_eop_m2) + mle_if_empty <= 1'b0; + else if (mle_state == MLE_ST_DONE) + mle_if_empty <= 1'b1; + + // mle_if_we + always @(posedge tx_clk, negedge rstn) + if(!rstn) mle_if_we <= 1'b0; - end - + else if (mle_start_m2) + mle_if_we <= 1'b0; + else if (mle_state >= MLE_ST_WR_ID && mle_state <= MLE_ST_XFER) + mle_if_we <= 1'b1; + else + mle_if_we <= 1'b0; - // mle_if_d: - always @(posedge tx_clk, negedge rstn) - if(!rstn) - mle_if_d <= 'd0; - else if (mle_if_enable) begin - if (mle_if_cnt > 4'd1) - mle_if_d <= mle_if_d + 1'b1; - else if (mle_if_cnt == 4'd1) - mle_if_d <= (mle_if_d + 1'b1) | 9'h100; - else - mle_if_d <= 'd0; - end - - assign mle_if_d_o = mle_if_d + PHY_NUM; + // mle_if_d_o[7:0] data out mux + always @(*) + if (mle_state > MLE_ST_WR_ID && mle_state <= MLE_ST_DONE) + case(mle_cnt) + MLE_CNT_START_XFER+2: mle_if_d_o[7:0] = PHY_NUM; + MLE_CNT_START_XFER+3: mle_if_d_o[7:0] = 8'hef; + MLE_CNT_START_XFER+4: mle_if_d_o[7:0] = mle_rx_byte_cnt_o[31:24]; + MLE_CNT_START_XFER+5: mle_if_d_o[7:0] = mle_rx_byte_cnt_o[23:16]; + MLE_CNT_START_XFER+6: mle_if_d_o[7:0] = mle_rx_byte_cnt_o[15:8]; + MLE_CNT_START_XFER+7: mle_if_d_o[7:0] = mle_rx_byte_cnt_o[7:0]; + default: mle_if_d_o[7:0] = 8'h00; + endcase + else + mle_if_d_o[7:0] = 8'h00; + // mle_d_[8] end of data flag + always @(*) + if (MLE_EOD_FLAG && mle_state == MLE_ST_DONE) + mle_if_d_o[8] <= 1'b1; + else + mle_if_d_o[8] <= 1'b0; /* @@ -402,6 +532,7 @@ module mac_rgmii #(parameter PHY_NUM=0) ( /* * rx_fifo_we + * asserted first byte of Dest MAC Address through second byte of FCS */ assign rx_fifo_we = ( rx_sample && ( rx_state >= RX_ST_SFD && rx_state <= RX_ST_DATA_DONE1 ) ) ? 1'b1 : 1'b0; @@ -465,11 +596,15 @@ module mac_rgmii #(parameter PHY_NUM=0) ( rx_ipv4_arp <= 1'b0; /* - * rx_keep flag - * signals must be one shot + * rx_trigger: must be one shot */ - // assign rx_keep = rx_enet_bcast | rx_ipv4_arp; - assign rx_keep = rx_state == RX_ST_MAC_TYPE1; + always @(posedge rx_clk, negedge rstn) + if (!rstn) + rx_trigger <= 1'b0; + else if (rx_state == RX_ST_DATA && rx_byte_cnt == 11'd20) + rx_trigger <= 1'b1; + else + rx_trigger <= 1'b0; // rx_error always @(*) @@ -479,6 +614,15 @@ module mac_rgmii #(parameter PHY_NUM=0) ( rx_error = 1'b1; else rx_error = 1'b0; + + /* rx_ipg_cnt */ + always @(posedge rx_clk, negedge rstn) + if (!rstn) + rx_ipg_cnt <= 16'd0; + else if ( rx_eop ) // sync with MLE processing + rx_ipg_cnt <= 16'd0; + else if ( rx_state == RX_ST_IDLE && rx_ipg_cnt < MAX_IPG_CNT) // don't let it roll over + rx_ipg_cnt <= rx_ipg_cnt + 'b1; /* rx_byte_cnt */ always @(posedge rx_clk, negedge rstn) @@ -520,7 +664,7 @@ module mac_rgmii #(parameter PHY_NUM=0) ( if (rx_pkt_length >= 12'd46) rx_packet_complete = (rx_sample && rx_state >= RX_ST_DATA && rx_pkt_length == rx_byte_cnt); else - rx_packet_complete = (rx_sample && rx_state >= RX_ST_DATA && rx_byte_cnt == 12'd46); + rx_packet_complete = (rx_sample && rx_state >= RX_ST_DATA && rx_byte_cnt == 11'd46); // FIFO data interface assign rx_fifo_d[7:0] = rx_d_m1; @@ -580,12 +724,12 @@ module mac_rgmii #(parameter PHY_NUM=0) ( else rx_active <= 1'b0; - // rx_pkt_cnt + // rx_cont_pkt_cnt: # of received packets always @(posedge rx_clk or negedge rstn) if (!rstn) - rx_pkt_cnt <= 16'd0; + rx_cont_pkt_cnt <= 16'd0; else if (rx_eop) - rx_pkt_cnt <= rx_pkt_cnt + 1'b1; + rx_cont_pkt_cnt <= rx_cont_pkt_cnt + 1'b1; always @(*) if (mode_1Gbit && (rx_state > RX_ST_SFD && rx_state <= RX_ST_DATA_DONE0) ) @@ -992,9 +1136,9 @@ module mac_rgmii #(parameter PHY_NUM=0) ( always @(posedge tx_clk, negedge rstn) if (!rstn) - tx_pkt_cnt <= 16'd0; + tx_cont_pkt_cnt <= 16'd0; else if (tx_eop) - tx_pkt_cnt <= tx_pkt_cnt + 1'b1; + tx_cont_pkt_cnt <= tx_cont_pkt_cnt + 1'b1; |

