From c95bf721bb437af5b68fddc10e2f698db47eb0bc Mon Sep 17 00:00:00 2001 From: Ray Lau Date: Mon, 16 Nov 2026 10:52:15 +0000 Subject: [PATCH] [rtl,doc] Align FIFO mode RCVR interrupt logic to 16550 Signed-off-by: Ray Lau --- hw/ip/uart16550/doc/registers.md | 2 +- hw/ip/uart16550/rtl/uart16550.sv | 52 ++++++++- hw/ip/uart16550/rtl/uart16550_init.sv | 119 +++++++++++++++++++++ hw/ip/uart16550/rtl/uart16550_translate.sv | 58 ++++++---- hw/ip/uart16550/uart16550.core | 1 + 5 files changed, 205 insertions(+), 27 deletions(-) create mode 100644 hw/ip/uart16550/rtl/uart16550_init.sv diff --git a/hw/ip/uart16550/doc/registers.md b/hw/ip/uart16550/doc/registers.md index dc4b4c2..2b6c6b1 100644 --- a/hw/ip/uart16550/doc/registers.md +++ b/hw/ip/uart16550/doc/registers.md @@ -11,7 +11,7 @@ The difference in functionality between the [16550 specification][16550-spec] an | RBR | 0x0 (DLAB=0) | RO | Reads from an empty RCVR FIFO returns 0. | | THR | 0x0 (DLAB=0) | WO | Writes to a full XMIT FIFO are dropped. | | IER | 0x4 (DLAB=0) | RW | Values written to EDSSI are kept, but will have no other effect as MODEM status interrupts are never raised. | -| IIR | 0x8 | RO | Bit 3 is set when FIFO is enabled and there is an RCVR FIFO overflow interrupt. FIFO mode timeout interrupts are aliased to received data available interrupts, and are raised immediately when the trigger level is reached without a timeout. Software must set a RCVR FIFO trigger level of 1 byte. THRE interrupt will not be cleared by reading IIR. A fix for bit 3 and FIFO mode timeout interrupt are tracked in [#9](https://github.com/lowRISC/uart-16550/issues/9). | +| IIR | 0x8 | RO | Received data available indicated by `IIR[3:1] == 0b010` has a higher priority than receive FIFO timeout indicated by `IIR[3:1] == 0b110`. THRE interrupt will not be cleared by reading IIR. | | FCR | 0x8 | WO | 16450 mode is not supported. Values written to FIFO enable are kept, but will have no other effect as the OpenTitan UART always uses a FIFO. Changing FIFO enable does not clear the FIFOs. All bits of FCR are writable regardless of the value of FIFO enable. Writes to bits 3-5 have no effect. Writing `0b11` to the RCVR FIFO trigger level will set a trigger level of 16 bytes. | | LCR | 0xC | RW | WLS writes are ignored and reads return `0b11` to indicate 8-bit word length. STB writes are ignored and reads return `0b0` to indicate 1 stop bit. EPS resets to `0b1` to indicate it is enabled by default. Writes to stick parity are ignored and reads return `0b0` to indicate that stick parity is always disabled. | | MCR | 0x10 | RW | Values written to DTR, RTS and OUT are kept but have no effect on external pins. | diff --git a/hw/ip/uart16550/rtl/uart16550.sv b/hw/ip/uart16550/rtl/uart16550.sv index 4591123..633eba5 100644 --- a/hw/ip/uart16550/rtl/uart16550.sv +++ b/hw/ip/uart16550/rtl/uart16550.sv @@ -47,10 +47,33 @@ module uart16550 ); // TileLink signals. - // Translated requests to the OpenTitan UART core + // Initialisation requests to the MUX + tlul_pkg::tl_h2d_t tl_init_h2d; + tlul_pkg::tl_d2h_t tl_init_d2h; + + // Translated requests to the MUX tlul_pkg::tl_h2d_t tl_translated_h2d; tlul_pkg::tl_d2h_t tl_translated_d2h; + // Multiplexed requests to the OpenTitan UART core + tlul_pkg::tl_h2d_t tl_mux_h2d; + tlul_pkg::tl_d2h_t tl_mux_d2h; + + // Initialisation select signal + // When high, the external TL-UL interface is blocked and the + // initialisation block has access to the OpenTitan UART core + logic init_sel; + + // UART initialisation block + // Sends requests on initialisation to better match 16550 default behaviour + uart16550_init u_uart_init( + .clk_i (clk_i), + .rst_ni (rst_ni), + .init_sel_o (init_sel), + .uart_tl_o (tl_init_h2d), + .uart_tl_i (tl_init_d2h) + ); + // Register translation block uart16550_translate u_reg_translate( .clk_i (clk_i), @@ -61,6 +84,29 @@ module uart16550 .uart_tl_i (tl_translated_d2h) ); + // TL-UL MUX + // Allows either u_uart_init or u_reg_translate to have access to the + // OpenTitan UART core depending on the value of init_sel + always_comb begin + if (init_sel) begin + // Pass through initialisation requests + tl_mux_h2d = tl_init_h2d; + tl_init_d2h = tl_mux_d2h; + + // Block translated requests + tl_translated_d2h = tlul_pkg::TL_D2H_DEFAULT; + tl_translated_d2h.a_ready = 1'b0; + end else begin + // Pass through translated requests + tl_mux_h2d = tl_translated_h2d; + tl_translated_d2h = tl_mux_d2h; + + // Block initialisation requests + tl_init_d2h = tlul_pkg::TL_D2H_DEFAULT; + tl_init_d2h.a_ready = 1'b0; + end + end + // OpenTitan UART core instance uart #( .AlertAsyncOn ( AlertAsyncOn ), @@ -71,8 +117,8 @@ module uart16550 ) u_uart_core( .clk_i (clk_i), .rst_ni (rst_ni), - .tl_i (tl_translated_h2d), - .tl_o (tl_translated_d2h), + .tl_i (tl_mux_h2d), + .tl_o (tl_mux_d2h), .alert_rx_i (alert_rx_i), .alert_tx_o (alert_tx_o), .racl_policies_i (racl_policies_i), diff --git a/hw/ip/uart16550/rtl/uart16550_init.sv b/hw/ip/uart16550/rtl/uart16550_init.sv new file mode 100644 index 0000000..f2e383b --- /dev/null +++ b/hw/ip/uart16550/rtl/uart16550_init.sv @@ -0,0 +1,119 @@ +// Copyright lowRISC and contributors (COSMIC project). +// Licensed under the Apache License, Version 2.0, see LICENSE for details. +// SPDX-License-Identifier: Apache-2.0 + +module uart16550_init( + // Clock and reset + input logic clk_i, + input logic rst_ni, + + // UART initialisation select + output logic init_sel_o, + + // TL-UL interface to the UART core + output tlul_pkg::tl_h2d_t uart_tl_o, + input tlul_pkg::tl_d2h_t uart_tl_i +); + + // Unused signals + logic unused_sig; + assign unused_sig = |{uart_tl_i}; + + /////////////////// + // Control logic // + /////////////////// + + // TODO state enc + localparam int StateWidth = 10; + typedef enum logic [StateWidth-1:0] { + ResetSt = 10'b1110001010, + InitReqSt = 10'b1001000001, + InitReqWaitSt = 10'b0010101101, + InitDoneSt = 10'b0111110000 + } state_e; + + state_e state_d, state_q; + + // Select output registers + logic init_sel_d; + logic init_sel_q; + + assign init_sel_o = init_sel_q; + + always_comb begin : p_fsm + // Default + state_d = state_q; + uart_tl_o = tlul_pkg::TL_H2D_DEFAULT; + uart_tl_o.a_size = 2; + uart_tl_o.a_mask = '1; + + init_sel_d = init_sel_q; + + unique case (state_q) + // Wait for reset release + ResetSt: begin + state_d = InitReqSt; + end + // Send initialisation request to UART + InitReqSt: begin + uart_tl_o.a_valid = 1; + uart_tl_o.a_opcode = tlul_pkg::PutFullData; + uart_tl_o.a_address = 'h30; // TIMEOUT_CTRL reg + uart_tl_o.a_data = '0; + // Parity is disabled by default + // 4 character times is 4*(1 + 8 + 1) = 40 bit times + uart_tl_o.a_data[23:0] = 24'd40; + uart_tl_o.a_data[31] = 1'b1; // EN = 1 + + if (uart_tl_i.a_ready) begin + state_d = InitReqWaitSt; + end + end + // Wait for response from UART + InitReqWaitSt: begin + if (uart_tl_i.d_valid) begin + // Assume that there is no error as the request is fixed and + // the first to be done after reset is released + state_d = InitDoneSt; + end + end + // Signal that initialisation is done + InitDoneSt: begin + init_sel_d = 1'b0; + end + endcase + + // Calculate UART TL-UL integrity + uart_tl_o.a_user = '{ + rsvd: '0, + instr_type: prim_mubi_pkg::MuBi4False, + cmd_intg: tlul_pkg::get_cmd_intg(.tl(uart_tl_o)), + data_intg: tlul_pkg::get_data_intg(.data(uart_tl_o.a_data)) + }; + end + + ////////// + // Regs // + ////////// + + // State register + prim_sparse_fsm_flop #( + .StateEnumT(state_e), + .Width($bits(state_e)), + .ResetValue(ResetSt) + ) u_state_regs ( + .clk_i ( clk_i ), + .rst_ni ( rst_ni ), + .state_i ( state_d ), + .state_o ( state_q ) + ); + + always_ff @(posedge clk_i or negedge rst_ni) begin + if (!rst_ni) begin + init_sel_q <= 1'b1; + end else begin + init_sel_q <= init_sel_d; + end + end + +endmodule diff --git a/hw/ip/uart16550/rtl/uart16550_translate.sv b/hw/ip/uart16550/rtl/uart16550_translate.sv index 30c29a7..aeeec8d 100644 --- a/hw/ip/uart16550/rtl/uart16550_translate.sv +++ b/hw/ip/uart16550/rtl/uart16550_translate.sv @@ -312,6 +312,7 @@ module uart16550_translate( uart_tl_o.a_data = '0; // 16550 to OT mapping uart_tl_o.a_data[1] = req_data_q[0]; // rx_watermark = ERBFI + uart_tl_o.a_data[6] = req_data_q[0]; // rx_timeout = ERBFI uart_tl_o.a_data[8] = req_data_q[1]; // tx_empty = ETBEI uart_tl_o.a_data[3] = req_data_q[2]; // rx_overflow = ELSI uart_tl_o.a_data[4] = req_data_q[2]; // rx_frame_err = ELSI @@ -345,13 +346,14 @@ module uart16550_translate( if (req_opcode_q == tlul_pkg::Get) begin // OT to 16550 mapping host_tl_o.d_data = '0; - host_tl_o.d_data[0] = resp_data_q[1]; // ERBFI = rx_watermark - host_tl_o.d_data[1] = resp_data_q[8]; // ETBEI = tx_empty + host_tl_o.d_data[0] = (resp_data_q[1] & // ERBFI = rx_watermark & + resp_data_q[6]); // rx_timeout + host_tl_o.d_data[1] = resp_data_q[8]; // ETBEI = tx_empty host_tl_o.d_data[2] = (resp_data_q[3] & // ELSI = rx_overflow & - resp_data_q[4] & // rx_frame_err & - resp_data_q[5] & // rx_break_err & - resp_data_q[7]); // rx_parity_err - host_tl_o.d_data[3] = edssi_q; // EDSSI read back + resp_data_q[4] & // rx_frame_err & + resp_data_q[5] & // rx_break_err & + resp_data_q[7]); // rx_parity_err + host_tl_o.d_data[3] = edssi_q; // EDSSI read back end else begin host_tl_o.d_data = '1; end @@ -406,27 +408,23 @@ module uart16550_translate( // OT to 16550 mapping host_tl_o.d_data = '0; // Interrupt pending, active low - host_tl_o.d_data[0] = ~(resp_data_q[1] | - resp_data_q[3] | - resp_data_q[4] | - resp_data_q[5] | - resp_data_q[7] | - resp_data_q[8]); + host_tl_o.d_data[0] = ~(|{resp_data_q[8:3], resp_data_q[1]}); if (resp_data_q[3] || resp_data_q[4] || resp_data_q[5] || resp_data_q[7]) begin // Receiver line status has highest priority - host_tl_o.d_data[2:1] = 2'b11; + host_tl_o.d_data[3:1] = 3'b011; end else if (resp_data_q[1]) begin // Received data available has highest priority - host_tl_o.d_data[2:1] = 2'b10; + host_tl_o.d_data[3:1] = 3'b010; + end else if (resp_data_q[6]) begin + // Character timeout indication has highest priority + host_tl_o.d_data[3:1] = 3'b110; end else if (resp_data_q[8]) begin // Transmitter holding register empty has highest priority - host_tl_o.d_data[2:1] = 2'b01; + host_tl_o.d_data[3:1] = 3'b001; end else begin // MODEM status has highest priority or no pending interrupt - host_tl_o.d_data[2:1] = 2'b00; + host_tl_o.d_data[3:1] = 3'b000; end - // Bit 3 = FIFO enable AND timeout error - host_tl_o.d_data[3] = fifo_en_q & resp_data_q[3]; // Bit 6,7 = FIFO enable host_tl_o.d_data[6] = fifo_en_q; host_tl_o.d_data[7] = fifo_en_q; @@ -488,13 +486,15 @@ module uart16550_translate( end // LCR write: send request to UART LCRReqSt: begin - // Write CTRL / OVRD + // Write CTRL / OVRD / TIMEOUT_CTRL uart_tl_o.a_valid = 1; uart_tl_o.a_opcode = tlul_pkg::PutFullData; if (req_step_q == 0) begin uart_tl_o.a_address = 'h10; // CTRL reg - end else begin + end else if (req_step_q == 1) begin uart_tl_o.a_address = 'h28; // OVRD reg + end else begin + uart_tl_o.a_address = 'h30; // TIMEOUT_CTRL reg end // 16550 to OT mapping if (req_step_q == 0) begin @@ -507,11 +507,23 @@ module uart16550_translate( uart_tl_o.a_data[6] = pen_d; // PARITY_EN = PEN uart_tl_o.a_data[7] = ~eps_d; // PARITY_ODD = !EPS uart_tl_o.a_data[31:16] = 16'd20972; // NCO default value - end else begin + end else if (req_step_q == 1) begin break_d = req_data_q[6]; uart_tl_o.a_data = '0; uart_tl_o.a_data[0] = break_d; // TXEN = break uart_tl_o.a_data[1] = 1'b0; // TXVAL = 0 + end else begin + uart_tl_o.a_data = '0; + if (pen_q) begin + // Parity enabled + // 4 character times is 4*(1 + 8 + 1 + 1) = 44 bit times + uart_tl_o.a_data[23:0] = 24'd44; + end else begin + // Parity disabled + // 4 character times is 4*(1 + 8 + 1) = 40 bit times + uart_tl_o.a_data[23:0] = 24'd40; + end + uart_tl_o.a_data[31] = 1'b1; // EN = 1 end // DLAB write @@ -532,9 +544,9 @@ module uart16550_translate( resp_error_d = resp_error_q | uart_tl_i.d_error; end - if (req_step_q == 0) begin + if (req_step_q != 2) begin // Next step, write another register - req_step_d = 1; + req_step_d = req_step_q + 1; state_d = LCRReqSt; end else begin // UART writes done diff --git a/hw/ip/uart16550/uart16550.core b/hw/ip/uart16550/uart16550.core index e7696dc..1e5aaa2 100644 --- a/hw/ip/uart16550/uart16550.core +++ b/hw/ip/uart16550/uart16550.core @@ -13,6 +13,7 @@ filesets: - lowrisc:ip:uart files: - rtl/uart16550_translate.sv + - rtl/uart16550_init.sv - rtl/uart16550.sv file_type: systemVerilogSource