interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / core / uart_rx.h

177 lines · 6996 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/* uart_rx.h -- polled console UART receive: register decode for the PL011
18 * (QEMU virt, SBSA) and the 16550 (the DGX Spark console, SPCR type 0x01 at
19 * 0x16A00000). Shared by core/console.c (the real MMIO base) and the host
20 * test tests/test_console_rx.c (a simulated register file).
21 *
22 * POLLING ONLY. Nothing here enables, routes or acknowledges an interrupt,
23 * and nothing writes a UART register: the firmware's line settings (baud,
24 * word length, FIFO) are used as found. The caller polls between lifecycle
25 * steps and between generation steps.
26 *
27 * Registers (no-write reads only):
28 * PL011 (Linux include/linux/amba/serial.h): UARTDR 0x00 (data bits 7:0,
29 * FE bit 8, PE bit 9, BE bit 10, OE bit 11), UARTFR 0x18 (RXFE bit 4),
30 * UARTCR 0x30 (UARTEN bit 0, RXE bit 9).
31 * 16550 (Linux include/uapi/linux/serial_reg.h): RBR 0 (DLAB=0), LCR 3
32 * (DLAB bit 7), LSR 5 (DR 0x01, OE 0x02, PE 0x04, FE 0x08, BI 0x10).
33 * Register n sits at n * stride; stride 4 means 32-bit accesses (SPCR
34 * GAS access size 3, Linux UPIO_MEM32 on the Spark), stride 1 byte
35 * accesses. Reading LSR clears OE/PE/FE/BI, so it is read exactly once
36 * per byte and every flag is taken from that one read.
37 *
38 * Decode result: 0..255 a good byte; CK_UART_RX_EMPTY nothing waiting;
39 * CK_UART_RX_ERROR a byte was lost or arrived damaged (overrun, parity,
40 * framing, break): the byte is dropped and counted, and the line assembler
41 * (core/line_rx.c) refuses the line it belonged to. */
42#ifndef AIENOS_CK_UART_RX_H
43#define AIENOS_CK_UART_RX_H
44
45#include <stdint.h>
46
47enum ck_uart_kind { CK_UART_NONE = 0, CK_UART_PL011 = 1, CK_UART_16550 = 2 };
48
49#define CK_UART_RX_EMPTY (-1)
50#define CK_UART_RX_ERROR (-2)
51
52#define CK_PL011_DR 0x00u
53#define CK_PL011_FR 0x18u
54#define CK_PL011_CR 0x30u
55#define CK_PL011_FR_RXFE (1u << 4)
56#define CK_PL011_DR_FE (1u << 8)
57#define CK_PL011_DR_PE (1u << 9)
58#define CK_PL011_DR_BE (1u << 10)
59#define CK_PL011_DR_OE (1u << 11)
60#define CK_PL011_CR_UARTEN (1u << 0)
61#define CK_PL011_CR_RXE (1u << 9)
62
63#define CK_16550_RBR 0u
64#define CK_16550_LCR 3u
65#define CK_16550_LSR 5u
66#define CK_16550_LCR_DLAB 0x80u
67#define CK_16550_LSR_DR 0x01u
68#define CK_16550_LSR_OE 0x02u
69#define CK_16550_LSR_PE 0x04u
70#define CK_16550_LSR_FE 0x08u
71#define CK_16550_LSR_BI 0x10u
72
73struct ck_uart_rx_count {
74 unsigned errors; /* every dropped byte (overrun, parity, framing, break) */
75 unsigned overruns; /* of those: overruns (a byte was lost before this one) */
76};
77
78/* SPCR interface type -> UART kind and 16550 register stride. -1: no
79 * console UART of a kind this kernel can drive (output dropped, no input).
80 * Types from Linux include/acpi/actbl1.h (ACPI_DBG2_*). */
81static inline int ck_uart_from_spcr(uint8_t interface_type, uint8_t access_size, int *kind, unsigned *stride)
82{
83 switch (interface_type) {
84 case 0x03: /* ARM PL011 */
85 case 0x0d: /* ARM SBSA generic UART, 32-bit only */
86 case 0x0e: /* ARM SBSA generic UART */
87 *kind = CK_UART_PL011;
88 *stride = 4;
89 return 0;
90 case 0x00: /* 16550 fully compatible */
91 case 0x01: /* 16550 subset (DBGP rev 1): the DGX Spark's SPCR */
92 case 0x12: /* 16550 with parameters in the GAS */
93 *kind = CK_UART_16550;
94 *stride = access_size == 3 ? 4 : access_size == 4 ? 8 : 1;
95 return 0;
96 default:
97 return -1;
98 }
99}
100
101static inline uint32_t ck_uart_rd(volatile uint8_t *base, unsigned off, unsigned width)
102{
103 if (width == 1)
104 return *(volatile uint8_t *)(base + off);
105 return *(volatile uint32_t *)(base + off);
106}
107
108/* 16550 register access width: 32-bit for stride 4 and 8 (the low 32 bits
109 * of a qword-spaced register), byte for stride 1. */
110static inline unsigned ck_16550_width(unsigned stride)
111{
112 return stride == 1 ? 1 : 4;
113}
114
115/* 1 when the receiver can deliver bytes; *regs gets the register that
116 * proves it (PL011 UARTCR; 16550 LCR << 8 | LSR) for the report line.
117 * 16550: there is no receiver-enable bit; not ready when LSR reads all ones
118 * (nothing answering at that address) or LCR.DLAB is set (offset 0 is then
119 * the divisor latch, not the receive buffer). */
120static inline int ck_uart_rx_ready(int kind, volatile uint8_t *base, unsigned stride, uint32_t *regs)
121{
122 if (!base)
123 return 0;
124 if (kind == CK_UART_PL011) {
125 uint32_t cr = ck_uart_rd(base, CK_PL011_CR, 4);
126 if (regs)
127 *regs = cr;
128 return (cr & CK_PL011_CR_UARTEN) && (cr & CK_PL011_CR_RXE);
129 }
130 if (kind == CK_UART_16550) {
131 unsigned w = ck_16550_width(stride);
132 uint32_t lcr = ck_uart_rd(base, CK_16550_LCR * stride, w) & 0xffu;
133 uint32_t lsr = ck_uart_rd(base, CK_16550_LSR * stride, w) & 0xffu;
134 if (regs)
135 *regs = lcr << 8 | lsr;
136 return lsr != 0xffu && !(lcr & CK_16550_LCR_DLAB);
137 }
138 return 0;
139}
140
141/* One polled receive: a byte, CK_UART_RX_EMPTY or CK_UART_RX_ERROR. */
142static inline int ck_uart_rx_take(int kind, volatile uint8_t *base, unsigned stride, struct ck_uart_rx_count *c)
143{
144 if (!base)
145 return CK_UART_RX_EMPTY;
146 if (kind == CK_UART_PL011) {
147 if (ck_uart_rd(base, CK_PL011_FR, 4) & CK_PL011_FR_RXFE)
148 return CK_UART_RX_EMPTY;
149 uint32_t d = ck_uart_rd(base, CK_PL011_DR, 4);
150 if (d & (CK_PL011_DR_FE | CK_PL011_DR_PE | CK_PL011_DR_BE | CK_PL011_DR_OE)) {
151 c->errors++;
152 if (d & CK_PL011_DR_OE)
153 c->overruns++;
154 return CK_UART_RX_ERROR;
155 }
156 return (int)(d & 0xffu);
157 }
158 if (kind == CK_UART_16550) {
159 unsigned w = ck_16550_width(stride);
160 uint32_t lsr = ck_uart_rd(base, CK_16550_LSR * stride, w) & 0xffu;
161 if (lsr == 0xffu)
162 return CK_UART_RX_EMPTY; /* nothing answering: never a byte stream of 0xff */
163 int byte = CK_UART_RX_EMPTY;
164 if (lsr & CK_16550_LSR_DR)
165 byte = (int)(ck_uart_rd(base, CK_16550_RBR * stride, w) & 0xffu); /* pops it either way */
166 if (lsr & (CK_16550_LSR_OE | CK_16550_LSR_PE | CK_16550_LSR_FE | CK_16550_LSR_BI)) {
167 c->errors++;
168 if (lsr & CK_16550_LSR_OE)
169 c->overruns++;
170 return CK_UART_RX_ERROR; /* OE alone: an earlier byte was lost, the line is damaged */
171 }
172 return byte;
173 }
174 return CK_UART_RX_EMPTY;
175}
176
177#endif