host_i2c_register_bank.v 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. `timescale 1ns/1ps
  2. // 16-bit register bank for the Host-I2C frontend. Application semantics are
  3. // reported through the response registers; bus ACK is handled by the slave.
  4. module host_i2c_register_bank (
  5. input wire clk,
  6. input wire rst_n,
  7. input wire write_strobe,
  8. input wire [15:0] write_address,
  9. input wire [7:0] write_data,
  10. input wire command_strobe,
  11. input wire [7:0] command_data,
  12. input wire [15:0] read_address,
  13. output reg [7:0] read_data,
  14. output reg response_valid,
  15. output reg [7:0] response_ok,
  16. output reg [7:0] response_command,
  17. output reg [7:0] response_status,
  18. output reg [7:0] response_detail,
  19. output reg [15:0] snapshot_length,
  20. input wire [7:0] snapshot_data,
  21. output reg manual_wr_en,
  22. output reg [4:0] manual_wr_addr,
  23. output reg [7:0] manual_wr_data,
  24. output reg manual_commit,
  25. input wire manual_commit_done,
  26. input wire manual_commit_ok,
  27. input wire hard_protect,
  28. input wire manual_active,
  29. input wire watchdog_active,
  30. input wire bmc_valid,
  31. input wire bmc_stale,
  32. input wire snapshot_valid
  33. );
  34. reg [7:0] staging [0:31];
  35. reg [31:0] written_mask;
  36. reg bad_value_seen;
  37. reg [7:0] bad_value_index;
  38. reg pending_commit;
  39. reg [7:0] pending_command;
  40. reg [15:0] snapshot_base;
  41. integer i;
  42. always @* begin
  43. read_data = 8'd0;
  44. if (read_address >= 16'h0100 && read_address <= 16'h011F)
  45. read_data = staging[read_address[4:0]];
  46. else if (read_address == 16'h0000)
  47. read_data = {snapshot_valid, hard_protect, bmc_stale, bmc_valid, watchdog_active, manual_active, response_valid, pending_commit};
  48. else if (read_address == 16'h0001) read_data = response_ok;
  49. else if (read_address == 16'h0002) read_data = response_command;
  50. else if (read_address == 16'h0003) read_data = response_status;
  51. else if (read_address == 16'h0004) read_data = response_detail;
  52. else if (read_address == 16'h0005) read_data = 8'd0;
  53. else if (read_address == 16'h0006) read_data = written_mask[7:0];
  54. else if (read_address == 16'h0007) read_data = snapshot_length[7:0];
  55. else if (read_address == 16'h0008) read_data = snapshot_length[15:8];
  56. else if (read_address >= 16'h0200 && read_address <= 16'h02FF)
  57. read_data = snapshot_data;
  58. end
  59. always @(posedge clk or negedge rst_n) begin
  60. if (!rst_n) begin
  61. written_mask <= 0; bad_value_seen <= 0; bad_value_index <= 0; pending_commit <= 0; pending_command <= 0;
  62. response_valid <= 0; response_ok <= 0; response_command <= 0;
  63. response_status <= 0; response_detail <= 0; snapshot_length <= 0;
  64. snapshot_base <= 0; manual_wr_en <= 0; manual_wr_addr <= 0;
  65. manual_wr_data <= 0; manual_commit <= 0;
  66. for (i = 0; i < 32; i = i + 1) staging[i] <= 0;
  67. end else begin
  68. manual_wr_en <= 0;
  69. manual_commit <= 0;
  70. if (write_strobe && write_address >= 16'h0100 && write_address <= 16'h011F) begin
  71. staging[write_address[4:0]] <= write_data;
  72. if (write_data <= 100) begin
  73. written_mask[write_address[4:0]] <= 1'b1;
  74. manual_wr_en <= 1;
  75. manual_wr_addr <= write_address[4:0];
  76. manual_wr_data <= write_data;
  77. end else if (!bad_value_seen) begin
  78. bad_value_seen <= 1'b1;
  79. bad_value_index <= write_address[7:0] - 8'h00;
  80. end
  81. end
  82. if (command_strobe) begin
  83. response_valid <= 0;
  84. response_command <= command_data;
  85. pending_command <= command_data;
  86. if (command_data == 8'h01) begin
  87. if (hard_protect) begin
  88. response_ok <= 0; response_status <= 4; response_detail <= 0; response_valid <= 1;
  89. end else if (bad_value_seen) begin
  90. response_ok <= 0; response_status <= 3; response_detail <= bad_value_index; response_valid <= 1;
  91. end else if (written_mask != 32'hFFFFFFFF) begin
  92. response_ok <= 0; response_status <= 2; response_detail <= 0; response_valid <= 1;
  93. end else begin
  94. pending_commit <= 1; manual_commit <= 1;
  95. end
  96. end else if (command_data == 8'h02) begin
  97. response_ok <= 1; response_status <= 0; response_detail <= 0;
  98. response_valid <= 1; snapshot_length <= 16'd201;
  99. end else begin
  100. response_ok <= 0; response_status <= 1; response_detail <= 0;
  101. response_valid <= 1;
  102. end
  103. end
  104. if (pending_commit && manual_commit_done) begin
  105. pending_commit <= 0;
  106. response_ok <= manual_commit_ok ? 1 : 0;
  107. response_status <= manual_commit_ok ? 0 : 4;
  108. response_detail <= 0;
  109. response_valid <= 1;
  110. if (manual_commit_ok) begin written_mask <= 0; bad_value_seen <= 0; end
  111. end
  112. end
  113. end
  114. endmodule