SummerCart64/fw/rtl/usb/usb_ft1248.sv

351 lines
9.9 KiB
Systemverilog
Raw Normal View History

2022-05-15 15:47:12 +02:00
interface usb_scb ();
logic fifo_flush;
logic reset_pending;
logic reset_ack;
logic write_buffer_flush;
2022-09-08 23:36:46 +02:00
logic [10:0] rx_count;
logic [10:0] tx_count;
2022-05-15 15:47:12 +02:00
modport controller (
output fifo_flush,
input reset_pending,
output reset_ack,
2022-09-08 23:36:46 +02:00
output write_buffer_flush,
input rx_count,
input tx_count
2022-05-15 15:47:12 +02:00
);
modport usb (
input fifo_flush,
output reset_pending,
input reset_ack,
2022-09-08 23:36:46 +02:00
input write_buffer_flush,
output rx_count,
output tx_count
2022-05-15 15:47:12 +02:00
);
endinterface
module usb_ft1248 (
2022-02-02 19:07:43 +01:00
input clk,
input reset,
2022-05-15 15:47:12 +02:00
usb_scb.usb usb_scb,
fifo_bus.fifo fifo_bus,
input usb_pwrsav,
output logic usb_clk,
output logic usb_cs,
input usb_miso,
2022-05-15 15:47:12 +02:00
inout [7:0] usb_miosi
);
logic rx_full;
2022-02-02 19:07:43 +01:00
logic rx_almost_full;
logic rx_write;
2022-02-04 19:23:32 +01:00
logic [7:0] rx_wdata;
logic tx_empty;
2022-02-02 19:07:43 +01:00
logic tx_almost_empty;
logic tx_read;
logic [7:0] tx_rdata;
2022-05-15 15:47:12 +02:00
fifo_8kb fifo_8kb_rx_inst (
.clk(clk),
.reset(reset || usb_scb.fifo_flush),
2022-05-15 15:47:12 +02:00
.empty(fifo_bus.rx_empty),
.almost_empty(fifo_bus.rx_almost_empty),
.read(fifo_bus.rx_read),
.rdata(fifo_bus.rx_rdata),
.full(rx_full),
2022-02-02 19:07:43 +01:00
.almost_full(rx_almost_full),
2022-05-15 15:47:12 +02:00
.write(rx_write),
2022-09-08 23:36:46 +02:00
.wdata(rx_wdata),
.count(usb_scb.rx_count)
);
2022-05-15 15:47:12 +02:00
fifo_8kb fifo_8kb_tx_inst (
.clk(clk),
.reset(reset || usb_scb.fifo_flush),
.empty(tx_empty),
2022-02-02 19:07:43 +01:00
.almost_empty(tx_almost_empty),
2022-05-15 15:47:12 +02:00
.read(tx_read),
.rdata(tx_rdata),
2022-05-15 15:47:12 +02:00
.full(fifo_bus.tx_full),
.almost_full(fifo_bus.tx_almost_full),
.write(fifo_bus.tx_write),
2022-09-08 23:36:46 +02:00
.wdata(fifo_bus.tx_wdata),
.count(usb_scb.tx_count)
);
2022-05-15 15:47:12 +02:00
logic [1:0] usb_pwrsav_ff;
2022-02-04 19:23:32 +01:00
logic [7:0] usb_miosi_out;
logic usb_oe;
2022-05-15 15:47:12 +02:00
logic ft_pwrsav;
2022-02-04 19:23:32 +01:00
logic ft_clk;
logic ft_cs;
logic ft_miso;
logic [7:0] ft_miosi_in;
logic [7:0] ft_miosi_out;
logic ft_oe;
always_ff @(posedge clk) begin
2022-05-15 15:47:12 +02:00
usb_pwrsav_ff <= {usb_pwrsav_ff[0], usb_pwrsav};
ft_pwrsav <= usb_pwrsav_ff[1];
2022-02-04 19:23:32 +01:00
usb_clk <= ft_clk;
usb_cs <= ft_cs;
ft_miso <= usb_miso;
ft_miosi_in <= usb_miosi;
usb_miosi_out <= ft_miosi_out;
usb_oe <= ft_oe;
end
2022-05-15 15:47:12 +02:00
assign usb_miosi = usb_oe ? usb_miosi_out : 8'hZZ;
2022-02-04 19:23:32 +01:00
2022-02-02 19:07:43 +01:00
typedef enum bit [2:0] {
STATE_IDLE,
STATE_SELECT,
STATE_COMMAND,
STATE_STATUS,
STATE_DATA,
STATE_DESELECT
} e_state;
typedef enum bit [7:0] {
2022-02-02 19:07:43 +01:00
CMD_WRITE = 8'h00,
CMD_READ = 8'h40,
CMD_READ_MODEM_STATUS = 8'h20,
CMD_WRITE_MODEM_STATUS = 8'h60,
CMD_WRITE_BUFFER_FLUSH = 8'h08
2022-05-15 15:47:12 +02:00
} e_cmd;
e_state state;
2022-02-04 19:23:32 +01:00
e_state next_state;
2022-05-15 15:47:12 +02:00
e_cmd cmd;
e_cmd next_cmd;
2022-02-04 19:23:32 +01:00
logic [3:0] phase;
2022-05-15 15:47:12 +02:00
logic last_tx_failed;
2022-02-04 19:23:32 +01:00
logic reset_reply;
logic last_reset_status;
logic [4:0] modem_status_counter;
logic write_modem_status_pending;
logic write_buffer_flush_pending;
2022-02-02 19:07:43 +01:00
always_ff @(posedge clk) begin
2022-02-04 19:23:32 +01:00
state <= next_state;
cmd <= next_cmd;
2022-02-02 19:07:43 +01:00
phase <= {phase[2:0], phase[3]};
2022-02-04 19:23:32 +01:00
if (state == STATE_IDLE) begin
phase <= 4'b0100;
end
2022-02-02 19:07:43 +01:00
if (reset) begin
2022-05-15 15:47:12 +02:00
last_tx_failed <= 1'b0;
usb_scb.reset_pending <= 1'b0;
2022-02-02 19:07:43 +01:00
last_reset_status <= 1'b0;
2022-02-04 19:23:32 +01:00
modem_status_counter <= 5'd0;
write_modem_status_pending <= 1'b0;
write_buffer_flush_pending <= 1'b0;
2022-02-02 19:07:43 +01:00
end else begin
2022-05-15 15:47:12 +02:00
if (usb_scb.reset_ack) begin
usb_scb.reset_pending <= 1'b0;
2022-02-02 19:07:43 +01:00
reset_reply <= 1'b1;
2022-02-07 22:24:37 +01:00
write_modem_status_pending <= 1'b1;
2022-02-02 19:07:43 +01:00
end
2022-05-15 15:47:12 +02:00
if (usb_scb.write_buffer_flush) begin
2022-02-04 19:23:32 +01:00
write_buffer_flush_pending <= 1'b1;
end
if (state == STATE_IDLE) begin
modem_status_counter <= modem_status_counter + 1'd1;
end
2022-05-15 15:47:12 +02:00
if ((state == STATE_DATA) && (cmd == CMD_WRITE) && phase[3]) begin
last_tx_failed <= ft_miso;
end
2022-02-04 19:23:32 +01:00
if (!ft_miso && (state == STATE_DATA) && phase[3]) begin
if (cmd == CMD_READ_MODEM_STATUS) begin
last_reset_status <= ft_miosi_in[0];
if (!last_reset_status && ft_miosi_in[0]) begin
2022-05-15 15:47:12 +02:00
usb_scb.reset_pending <= 1'b1;
2022-02-04 19:23:32 +01:00
end
if (last_reset_status && !ft_miosi_in[0]) begin
reset_reply <= 1'b0;
2022-02-07 22:24:37 +01:00
write_modem_status_pending <= 1'b1;
2022-02-04 19:23:32 +01:00
end
2022-02-02 19:07:43 +01:00
end
2022-02-04 19:23:32 +01:00
if (cmd == CMD_WRITE_MODEM_STATUS) begin
write_modem_status_pending <= 1'b0;
end
if (cmd == CMD_WRITE_BUFFER_FLUSH) begin
write_buffer_flush_pending <= 1'b0;
2022-02-02 19:07:43 +01:00
end
2022-01-22 00:10:47 +01:00
end
2022-02-04 19:23:32 +01:00
end
end
always_comb begin
ft_clk = 1'b0;
ft_cs = 1'b1;
ft_miosi_out = 8'hFF;
ft_oe = 1'b0;
2022-01-22 00:10:47 +01:00
2022-02-04 19:23:32 +01:00
if (state == STATE_SELECT) begin
ft_cs = 1'b0;
end
if (state == STATE_COMMAND) begin
if (phase[0] || phase[1]) begin
ft_clk = 1'b1;
end
ft_cs = 1'b0;
ft_miosi_out = cmd;
ft_oe = 1'b1;
end
if (state == STATE_STATUS) begin
if (phase[0] || phase[1]) begin
ft_clk = 1'b1;
end
ft_cs = 1'b0;
end
if (state == STATE_DATA) begin
if (phase[0] || phase[1]) begin
ft_clk = 1'b1;
end
2022-02-07 22:24:37 +01:00
ft_cs = 1'b0;
2022-02-04 19:23:32 +01:00
if (cmd == CMD_WRITE) begin
ft_miosi_out = tx_rdata;
ft_oe = 1'b1;
end
if (cmd == CMD_WRITE_MODEM_STATUS) begin
ft_miosi_out = {2'b00, reset_reply, 5'b00000};
ft_oe = 1'b1;
end
end
end
always_comb begin
rx_write = 1'b0;
tx_read = 1'b0;
rx_wdata = ft_miosi_in;
2022-05-15 15:47:12 +02:00
if (!ft_miso && phase[3]) begin
case (state)
STATE_STATUS: begin
if (cmd == CMD_WRITE && !last_tx_failed) begin
tx_read = 1'b1;
end
end
STATE_DATA: begin
if (cmd == CMD_READ) begin
rx_write = 1'b1;
end
if (cmd == CMD_WRITE && !tx_empty) begin
tx_read = 1'b1;
end
end
endcase
2022-02-04 19:23:32 +01:00
end
end
always_comb begin
next_state = state;
next_cmd = cmd;
if (reset) begin
next_state = STATE_IDLE;
end else begin
case (state)
2022-02-02 19:07:43 +01:00
STATE_IDLE: begin
2022-05-15 15:47:12 +02:00
if (ft_pwrsav) begin
if (write_modem_status_pending) begin
next_state = STATE_SELECT;
next_cmd = CMD_WRITE_MODEM_STATUS;
end else if (&modem_status_counter) begin
next_state = STATE_SELECT;
next_cmd = CMD_READ_MODEM_STATUS;
end else if (!tx_empty || last_tx_failed) begin
next_state = STATE_SELECT;
next_cmd = CMD_WRITE;
end else if (write_buffer_flush_pending) begin
next_state = STATE_SELECT;
next_cmd = CMD_WRITE_BUFFER_FLUSH;
end else if (!rx_full) begin
next_state = STATE_SELECT;
next_cmd = CMD_READ;
end
end
end
2022-02-02 19:07:43 +01:00
STATE_SELECT: begin
2022-02-04 19:23:32 +01:00
if (phase[3]) begin
next_state = STATE_COMMAND;
end
end
2022-02-02 19:07:43 +01:00
STATE_COMMAND: begin
2022-02-04 19:23:32 +01:00
if (phase[3]) begin
next_state = STATE_STATUS;
end
end
2022-02-02 19:07:43 +01:00
STATE_STATUS: begin
2022-02-04 19:23:32 +01:00
if (phase[3]) begin
if (ft_miso) begin
next_state = STATE_DESELECT;
end else begin
next_state = STATE_DATA;
end
end
2022-02-02 19:07:43 +01:00
end
STATE_DATA: begin
2022-02-04 19:23:32 +01:00
if (phase[3]) begin
if (ft_miso) begin
next_state = STATE_DESELECT;
end else if (cmd == CMD_READ) begin
2022-02-02 19:07:43 +01:00
if (rx_almost_full) begin
2022-02-04 19:23:32 +01:00
next_state = STATE_DESELECT;
2022-02-02 19:07:43 +01:00
end
2022-02-07 22:24:37 +01:00
end else if (cmd == CMD_WRITE) begin
2022-05-15 15:47:12 +02:00
if (tx_empty) begin
2022-02-07 22:24:37 +01:00
next_state = STATE_DESELECT;
end
2022-02-04 19:23:32 +01:00
end else begin
next_state = STATE_DESELECT;
end
end
end
2022-02-02 19:07:43 +01:00
STATE_DESELECT: begin
if (phase[1]) begin
2022-02-04 19:23:32 +01:00
next_state = STATE_IDLE;
2022-02-02 19:07:43 +01:00
end
end
2022-02-04 19:23:32 +01:00
default: begin
next_state = STATE_IDLE;
end
endcase
end
end
endmodule