Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
227 changes: 169 additions & 58 deletions src/ctrl/bus_tx_flow.sv
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
/*
SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: Apache-2.0

This module controls the low-level aspects of the SDA transmit data flow on the I3C bus. It drives
the both the data value and the drive mode (OpenDrain or PushPull) of the SDA pad.
Note: No logic apart from static and guaranteed glitch-free muxing must be inserted between this
module and the physical SDA pad!
This module controls the low-level aspects of the SDA transmit data flow on the I3C bus. It drives
the both the data value and the drive mode (OpenDrain or PushPull) of the SDA pad.
Note: No logic apart from static and guaranteed glitch-free muxing must be inserted between this
module and the physical SDA pad!

External modules can send following request types to drive the bus:
- Send data byte,
- Initiate an IBI with address as byte payload
- Send Tbit/(N)ACK.
External modules can send following request types to drive the bus:
- Send raw data bits, including regular ACK/NACK,
- Send raw data bytes
- Initiate an IBI with address as byte payload
- Send an ACK following a write request by the contoller which requires special handoff
- Send TbitAbort in a private read, requiring half-bit drive mode change
- Send TbitContinue in a private read, requiring special checks for controller-side abort

Requests are made through the tx_req_i struct port which encapsulates all required information.
Feedback is provided through tx_rsp_o, including any error states.
Requests are made through the tx_req_i struct port which encapsulates all required information.
Feedback is provided through tx_rsp_o, including any error states.
*/

module bus_tx_flow import i3c_pkg::*; (
input logic clk_i,
input logic rst_ni,

// Input I3C Bus events
input logic scl_negedge_i,
input logic scl_posedge_i,
input logic scl_stable_low_i,
input bus_state_t bus_i,

// Tx request in
input bus_tx_req_t tx_req_i,
Expand All @@ -33,6 +34,9 @@ module bus_tx_flow import i3c_pkg::*; (
// Open Drain / Push Pull
output logic sel_od_pp_o,

// Output enable for SDA pad
output logic sda_oe_o,

// Output I3C SDA bus line
output logic sda_o
);
Expand All @@ -41,51 +45,106 @@ module bus_tx_flow import i3c_pkg::*; (
logic bit_counter_en;
logic [3:0] bit_counter_q, bit_counter_d;

logic [2:0] reqs;
logic req_any;
// Registers for all critical signals directly connected to the SDA pad
i3c_byte_t req_value_q, req_value_d;
i3c_drive_e drive_mode_q, drive_mode_d;
logic sda_oe_q, sda_oe_d;

logic tx_done; // Indicates finished bit write
logic bus_tx_done; // Feedback to requester that transfer is done
logic req_error;

// TODO Can probably be removed
assign req_error = 1'b0;

typedef enum logic [2:0] {
Idle,
DriveByte,
NextTaskDecision,
WaitNegEdge,
WaitPosEdge
WaitPosEdge,
TReadContSpecial
} tx_state_e;

tx_state_e state_d, state_q;

// SDA is simply the MSB of the data shift register. No further logic or muxing.
// Similar for pad drive mode and output enable.
assign sda_o = req_value_q[7];
assign sel_od_pp_o = drive_mode_q;
assign sda_oe_o = sda_oe_q;

/*
Truth table for sda_oe.

sel_od_pp_o | sda_o || sda_oe_o | IO state
------------+--------++----------+-----------
0 | 0 || 1 | 0
0 | 1 || 0 | hi-z
1 | 0 || 1 | 0
1 | 1 || 1 | 1
*/
assign sda_oe_d = drive_mode_d || !req_value_d[7];

// Common logic whenever a transfer gets started, including back-to-back transfers
function automatic tx_state_e start_transfer(
input bus_tx_req_t bus_tx_req,
output i3c_byte_t req_value_out,
output logic bit_counter_en_out
output i3c_byte_t req_value,
output i3c_drive_e drive_mode,
output logic counter_en
);
req_value_out[7] = bus_tx_req.data[7];
req_value_out[6:0] = bus_tx_req.req_bit ? '1 : bus_tx_req.data[6:0];
if (bus_tx_req.req_bit) begin
// Only one bit to send; wait for posedge
bit_counter_en_out = 1'b0;
return WaitPosEdge;
end else begin

// Unconditional default values to keep Lint happy
req_value = '1;
drive_mode = OpenDrain;
counter_en = 1'b0;

// Decide on value and drive mode following SCL negedge
case (bus_tx_req.req_type)
Comment thread
clayton8 marked this conversation as resolved.
Resolved
RawByte: begin
req_value = bus_tx_req.data;
drive_mode = bus_tx_req.drive_type;
end
RawBit: begin
req_value = {bus_tx_req.data[7], {7{1'b1}}};
drive_mode = bus_tx_req.drive_type;
end
AckIbi: begin
req_value = '1;
drive_mode = OpenDrain;
end
AckRegular, AckWrite: begin
req_value[7] = 1'b0;
drive_mode = OpenDrain;
end
TReadCont: begin
req_value[7] = 1'b1;
drive_mode = PushPull;
end
TReadEnd: begin
req_value[7] = 1'b0;
drive_mode = PushPull;
end
InitIbi: begin
req_value = bus_tx_req.data; // Data is IBI address
drive_mode = OpenDrain;
end
default: ;
endcase

// Most request types are only a single bit in length; completion can be signalled at the next
// SCL rising edge.
if (bus_tx_req.req_type inside {RawByte, InitIbi}) begin
// Enable bit counter and work on full byte in DriveByte
bit_counter_en_out = 1'b1;
counter_en = 1'b1;
return DriveByte;
end else begin
counter_en = 1'b0;
return WaitPosEdge;
end

endfunction : start_transfer

assign reqs = {tx_req_i.req_byte, tx_req_i.req_bit, tx_req_i.req_ibi};
assign req_any = |reqs;
// Clever way to ensure that only one bit is HIGH
// Source: https://stackoverflow.com/a/11235598
// It might be optimized if we're sure there are only 2 requests at most
assign req_error = |(reqs & (reqs - 1));

// Bit counter used for byte transfers
always_comb begin
bit_counter_d = bit_counter_q;
Expand All @@ -99,26 +158,26 @@ module bus_tx_flow import i3c_pkg::*; (
end
end

// SDA is simply the MSB of the data shift register. No further logic or muxing.
assign sda_o = req_value_q[7];

// Main FSM which handles all low-level bus details in terms of transmitting
always_comb begin : tx_fsm
bit_counter_en = 1'b0;

tx_done = 1'b0;
bus_tx_done = 1'b0;

req_value_d = req_value_q;
state_d = state_q;
req_value_d = req_value_q;
drive_mode_d = drive_mode_q;
state_d = state_q;
unique case (state_q)
Idle: begin
if (req_any) begin
if (tx_req_i.req_ibi) begin
if (tx_req_i.req_valid) begin
if (tx_req_i.req_type == InitIbi) begin
// Drive 0 in OD on SDA and wait until the controller gives us a negedge on SCL
// Note: The controller also could've started to drive SDA below and it will take the
// delay through the SDA synchronizer for us to detect this, however, this does not
// present any timing or driving conflict issue.
req_value_d[7] = 1'b0;
drive_mode_d = OpenDrain;
end
// For IBI, this state is 2-in-1: First, SDA is driven low above to initiate the IBI by
// generating a "target-side start condition". Next, on the SCL negedge that follows,
Expand All @@ -127,23 +186,23 @@ module bus_tx_flow import i3c_pkg::*; (
// non-IBI frames/transfers.
// Since no clocking event happens between these two phases, we cannot make any state
// transition and have to handle both in the same state.
if (scl_negedge_i || scl_stable_low_i) begin
state_d = start_transfer(tx_req_i, req_value_d, bit_counter_en);
if (bus_i.scl.neg_edge || bus_i.scl.stable_low) begin
state_d = start_transfer(tx_req_i, req_value_d, drive_mode_d, bit_counter_en);
end else begin
state_d = WaitNegEdge;
end
end
end
WaitNegEdge: begin
if (scl_negedge_i) begin
state_d = start_transfer(tx_req_i, req_value_d, bit_counter_en);
if (bus_i.scl.neg_edge) begin
state_d = start_transfer(tx_req_i, req_value_d, drive_mode_d, bit_counter_en);
end
end
DriveByte: begin
if (tx_req_i.req_byte || tx_req_i.req_ibi) begin
if (tx_req_i.req_valid) begin
bit_counter_en = 1'b1;
// Simply wait for next edge
if (scl_negedge_i) begin
if (bus_i.scl.neg_edge) begin
tx_done = 1'b1;
// Shift the register which drives sda left to get next bit
req_value_d = {req_value_q[6:0], 1'b1};
Expand All @@ -155,25 +214,75 @@ module bus_tx_flow import i3c_pkg::*; (
end else begin
// Requester cancelled the transaction, e.g., a bus stop condition has occurred or
// arbitration was lost during the address phase.
req_value_d = '1;
drive_mode_d = OpenDrain;
req_value_d = '1;
state_d = Idle;
end
end
WaitPosEdge: begin
// Wait for posedge to avoid following rx requests sampling this bit as well
if (scl_posedge_i) begin
bus_tx_done = 1'b1;
state_d = NextTaskDecision;
if (bus_i.scl.pos_edge) begin
// Handle all cases that require half-bit changes to SDA data and drive mode
case (tx_req_i.req_type)
AckWrite: begin
// Release SDA in the middle of ACK to high-Z; Controller takes over driving
// Specified in Section 5.1.2.3.1
req_value_d[7] = 1'b1;
end
AckIbi: begin
Comment thread
clayton8 marked this conversation as resolved.
Resolved
// Take over to drive SDA low in PP in case of an IBI Ack by the controller
// Specified in Section 5.1.2.3.2
if (bus_i.sda.value == 1'b0) begin
req_value_d[7] = 1'b0;
drive_mode_d = PushPull;
end
end
TReadEnd, TReadCont: begin
Comment thread
clayton8 marked this conversation as resolved.
Resolved
// Both cases require to release SDA to high-Z; in case of TReadEnd, the Controller
// will take over driving SDA low. In case of TReadCont, the Controller may drive SDA
// low after the rising edge to signal a read abort to us.
// Specified in Section 5.1.2.3.4
req_value_d[7] = 1'b1;
drive_mode_d = OpenDrain;
end
default: ;
endcase
// In most cases the transaction is completed; flag this to the requester. For TReadCont,
// we however must wait until the next SCL negedge, as the Controller may sent an Rs
// between then and the SCL posedge that just happened to deny our request to continue.
if (tx_req_i.req_type == TReadCont) begin
state_d = TReadContSpecial;
end else begin
bus_tx_done = 1'b1;
state_d = NextTaskDecision;
end
end
end
TReadContSpecial: begin
// If we see an SDA negedge before the next SCL negedge, the Controller aborted the read
if (bus_i.sda.neg_edge) begin
state_d = Idle;
end
// Controller has accepted to continue and the requester has provided the respective data
// already as part of the TReadCont request such that we are able to clock out the MSB
// on this very negedge. Continue with the remaining 7 bits in DriveByte.
if (bus_i.scl.neg_edge) begin
bus_tx_done = 1'b1;
bit_counter_en = 1'b1;
drive_mode_d = PushPull;
req_value_d = tx_req_i.data;
state_d = DriveByte;
end
end
NextTaskDecision: begin
if (scl_negedge_i) begin
if (req_any) begin
if (bus_i.scl.neg_edge) begin
if (tx_req_i.req_valid) begin
// Back-to-back transfer pending, immediately service it
state_d = start_transfer(tx_req_i, req_value_d, bit_counter_en);
state_d = start_transfer(tx_req_i, req_value_d, drive_mode_d, bit_counter_en);
end else begin
// Reset sda_o to OpenDrain-high & back to Idle
req_value_d = '1;
// Reset sda_o to High-Z & back to Idle
drive_mode_d = OpenDrain;
req_value_d = '1;
state_d = Idle;
end
end
Expand All @@ -193,17 +302,19 @@ module bus_tx_flow import i3c_pkg::*; (
done: bus_tx_done
};

assign sel_od_pp_o = tx_req_i.drive_type; // TODO FIXME - Feedthrough for now

// Sequential process for all flops
always_ff @(posedge clk_i or negedge rst_ni) begin
if (~rst_ni) begin
bit_counter_q <= '0;
req_value_q <= '1;
drive_mode_q <= OpenDrain;
sda_oe_q <= 1'b0;
state_q <= Idle;
end else begin
bit_counter_q <= bit_counter_d;
req_value_q <= req_value_d;
drive_mode_q <= drive_mode_d;
sda_oe_q <= sda_oe_d;
state_q <= state_d;
end
end
Expand Down
Loading