Page MenuHomeFreeBSD

D61191.1794909455.diff
No OneTemporary

Authored By
Unknown
Size
14 KB
Referenced Files
None
Subscribers
None

D61191.1794909455.diff

diff --git a/sys/arm64/conf/std.mediatek b/sys/arm64/conf/std.mediatek
--- a/sys/arm64/conf/std.mediatek
+++ b/sys/arm64/conf/std.mediatek
@@ -6,6 +6,9 @@
options SOC_MEDIATEK
options SOC_MEDIATEK_MT7986
+# Serial (COM) ports
+device uart_ns8250 # ns8250-type UART driver
+
options FDT
# DTBs
diff --git a/sys/conf/files.arm64 b/sys/conf/files.arm64
--- a/sys/conf/files.arm64
+++ b/sys/conf/files.arm64
@@ -521,6 +521,7 @@
dev/spibus/controller/rockchip/rk_spi.c optional fdt rk_spi
dev/uart/uart_cpu_arm64.c optional uart
+dev/uart/uart_dev_mtk.c optional uart uart_ns8250 fdt soc_mediatek
dev/uart/uart_dev_mu.c optional uart uart_mu fdt
dev/uart/uart_dev_pl011.c optional uart pl011
diff --git a/sys/dev/uart/uart_dev_mtk.c b/sys/dev/uart/uart_dev_mtk.c
new file mode 100644
--- /dev/null
+++ b/sys/dev/uart/uart_dev_mtk.c
@@ -0,0 +1,500 @@
+/*-
+ * SPDX-License-Identifier: BSD-2-Clause
+ *
+ * Copyright (c) 2026 Hayzam Sherif <hayzam@alchemilla.io>
+ */
+
+/*
+ * MediaTek UART ("mediatek,mt6577-uart" and compatibles).
+ *
+ * A 16550 with 32-bit spaced registers and an extended baud rate
+ * generator. In mode 3 a bit lasts DLM:DLL * (SAMPLE_COUNT + 1) clock
+ * cycles, and FRACDIV_L/M add one sample period to selected bits:
+ * FRACDIV_L to the data bits, FRACDIV_M bit 0 to the parity bit and bit 1
+ * to a stop bit. With n extended bits in a frame of b bits:
+ *
+ * baud = rclk / (div * (count + n / b))
+ *
+ * Measured on the MT7986: an extension only applies to bits the frame
+ * has, and the stop bit one only with two stop bits. With five data bits,
+ * two stop bits are one sample period short, and the stop bit extension
+ * is not used there because it gave inconsistent frame lengths.
+ */
+
+#include <sys/param.h>
+#include <sys/systm.h>
+#include <sys/bus.h>
+#include <sys/limits.h>
+
+#include <machine/bus.h>
+
+#include <dev/clk/clk.h>
+#include <dev/fdt/fdt_pinctrl.h>
+#include <dev/ic/ns16550.h>
+#include <dev/ofw/ofw_bus.h>
+#include <dev/ofw/ofw_bus_subr.h>
+#include <dev/uart/uart.h>
+#include <dev/uart/uart_bus.h>
+#include <dev/uart/uart_cpu.h>
+#include <dev/uart/uart_cpu_fdt.h>
+#include <dev/uart/uart_dev_ns8250.h>
+
+#include "uart_if.h"
+
+/* MediaTek registers, as register indices (4 bytes apart). */
+#define MTK_UART_HIGHSPEED 0x09
+#define MTK_UART_HIGHSPEED_SAMPLE 3 /* baud from SAMPLE_COUNT */
+#define MTK_UART_SAMPLE_COUNT 0x0a
+#define MTK_UART_SAMPLE_POINT 0x0b
+#define MTK_UART_RATEFIX 0x0d
+#define MTK_UART_RATEFIX_EN 0x01 /* divide the clock by 2 */
+#define MTK_UART_RATEFIX_DIV4 0x04 /* by 4 instead */
+#define MTK_UART_FRACDIV_L 0x15 /* data bits 0-7 */
+#define MTK_UART_FRACDIV_M 0x16
+#define MTK_UART_FRACDIV_M_PARITY 0x01
+#define MTK_UART_FRACDIV_M_STOP 0x02
+
+#define MTK_UART_TOLERANCE 30 /* accepted error, in 0.1 % */
+
+struct mtk_uart_softc {
+ struct ns8250_softc ns8250;
+ clk_t baud;
+ clk_t bus; /* optional */
+};
+
+struct mtk_uart_divisor {
+ uint32_t div; /* DLM:DLL */
+ uint32_t count; /* sample periods per bit */
+ uint32_t fracl;
+ uint32_t fracm;
+};
+
+/* A frame lasts bits * count - trim sample periods, plus extensions. */
+struct mtk_uart_frame {
+ u_int bits;
+ u_int databits;
+ u_int trim;
+ bool parity;
+ bool stop2;
+};
+
+static void
+mtk_uart_frame(struct mtk_uart_frame *f, int databits, int stopbits,
+ int parity)
+{
+ f->databits = databits;
+ f->parity = parity != UART_PARITY_NONE;
+ f->stop2 = stopbits > 1;
+ f->bits = 1 + databits + (f->parity ? 1 : 0) + (f->stop2 ? 2 : 1);
+ f->trim = (databits == 5 && f->stop2) ? 1 : 0;
+}
+
+/* Number of bits that can be extended. */
+static u_int
+mtk_uart_frame_slots(const struct mtk_uart_frame *f)
+{
+ return (f->databits + (f->parity ? 1 : 0) +
+ (f->stop2 && f->databits > 5 ? 1 : 0));
+}
+
+/*
+ * Use the smallest divisor whose bit time fits SAMPLE_COUNT, and spread the
+ * rest of the frame time over the bits that can be extended.
+ */
+static int
+mtk_uart_divisor(u_int rclk, int baudrate, const struct mtk_uart_frame *f,
+ struct mtk_uart_divisor *d)
+{
+ uint64_t got, periods, want, diff;
+ uint32_t div, k, i, slots;
+
+ if (baudrate <= 0 || rclk == 0 || f->databits < 5 || f->databits > 8)
+ return (EINVAL);
+ div = howmany((uint64_t)rclk, (uint64_t)baudrate * 256);
+ if (div > 0xffff)
+ return (EINVAL);
+
+ /* Frame time in sample periods, rounded. */
+ periods = ((uint64_t)rclk * f->bits + (uint64_t)div * baudrate / 2) /
+ ((uint64_t)div * baudrate);
+ d->div = div;
+ d->count = (periods + f->trim) / f->bits;
+ k = (periods + f->trim) % f->bits;
+ slots = mtk_uart_frame_slots(f);
+ if (k > slots) {
+ /* Too many to extend: round to the nearer limit. */
+ if (k - slots <= f->bits - k) {
+ k = slots;
+ } else {
+ d->count++;
+ k = 0;
+ }
+ }
+ if (d->count < 4 || d->count > 256)
+ return (EINVAL);
+ periods = (uint64_t)d->count * f->bits - f->trim + k;
+
+ /* Reject rates that are more than MTK_UART_TOLERANCE off. */
+ got = (uint64_t)rclk * f->bits;
+ want = (uint64_t)baudrate * div * periods;
+ diff = got > want ? got - want : want - got;
+ if (diff * 1000 > want * MTK_UART_TOLERANCE)
+ return (EINVAL);
+
+ d->fracl = d->fracm = 0;
+ for (i = 0; i < slots; i++) {
+ if ((i + 1) * k / slots == i * k / slots)
+ continue;
+ if (i < f->databits)
+ d->fracl |= 1u << i;
+ else if (i == f->databits && f->parity)
+ d->fracm |= MTK_UART_FRACDIV_M_PARITY;
+ else
+ d->fracm |= MTK_UART_FRACDIV_M_STOP;
+ }
+ return (0);
+}
+
+static void
+mtk_uart_set_divisor(struct uart_bas *bas, const struct mtk_uart_divisor *d)
+{
+ uint8_t lcr;
+
+ uart_setreg(bas, MTK_UART_RATEFIX, 0);
+ uart_setreg(bas, MTK_UART_HIGHSPEED, MTK_UART_HIGHSPEED_SAMPLE);
+ uart_setreg(bas, MTK_UART_SAMPLE_COUNT, d->count - 1);
+ /* Sample in the middle of the bit. */
+ uart_setreg(bas, MTK_UART_SAMPLE_POINT, (d->count - 3) / 2);
+ uart_setreg(bas, MTK_UART_FRACDIV_L, d->fracl);
+ uart_setreg(bas, MTK_UART_FRACDIV_M, d->fracm);
+
+ lcr = uart_getreg(bas, REG_LCR);
+ uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
+ uart_barrier(bas);
+ uart_setreg(bas, REG_DLL, d->div & 0xff);
+ uart_setreg(bas, REG_DLH, (d->div >> 8) & 0xff);
+ uart_setreg(bas, REG_LCR, lcr);
+ uart_barrier(bas);
+}
+
+/* Back to plain 16550 mode, which the ns8250 code expects. */
+static void
+mtk_uart_reset_generator(struct uart_bas *bas)
+{
+ uart_setreg(bas, MTK_UART_RATEFIX, 0);
+ uart_setreg(bas, MTK_UART_HIGHSPEED, 0);
+ uart_setreg(bas, MTK_UART_FRACDIV_L, 0);
+ uart_setreg(bas, MTK_UART_FRACDIV_M, 0);
+ uart_barrier(bas);
+}
+
+/* Current baud rate, from the registers. */
+static int
+mtk_uart_get_baud(struct uart_bas *bas)
+{
+ struct mtk_uart_frame f;
+ uint64_t periods;
+ uint32_t div, ext, fracm, rclk, rf;
+ uint8_t lcr;
+
+ lcr = uart_getreg(bas, REG_LCR);
+ uart_setreg(bas, REG_LCR, lcr | LCR_DLAB);
+ uart_barrier(bas);
+ div = uart_getreg(bas, REG_DLL) | (uart_getreg(bas, REG_DLH) << 8);
+ uart_setreg(bas, REG_LCR, lcr);
+ uart_barrier(bas);
+ if (div == 0)
+ return (0);
+
+ rclk = bas->rclk;
+ rf = uart_getreg(bas, MTK_UART_RATEFIX);
+ if ((rf & MTK_UART_RATEFIX_EN) != 0)
+ rclk /= (rf & MTK_UART_RATEFIX_DIV4) != 0 ? 4 : 2;
+
+ switch (uart_getreg(bas, MTK_UART_HIGHSPEED) & 0x3) {
+ case 0:
+ return (rclk / (div * 16));
+ case 1:
+ return (rclk / (div * 8));
+ case 2:
+ return (rclk / (div * 4));
+ }
+
+ /* Mode 3: average the extended bits over the frame. */
+ mtk_uart_frame(&f, (lcr & LCR_8BITS) + 5,
+ (lcr & LCR_STOPB) != 0 ? 2 : 1,
+ (lcr & LCR_PENAB) != 0 ? UART_PARITY_EVEN : UART_PARITY_NONE);
+ fracm = uart_getreg(bas, MTK_UART_FRACDIV_M);
+ ext = bitcount32(uart_getreg(bas, MTK_UART_FRACDIV_L) &
+ ((1u << f.databits) - 1));
+ if (f.parity && (fracm & MTK_UART_FRACDIV_M_PARITY) != 0)
+ ext++;
+ if (f.stop2 && (fracm & MTK_UART_FRACDIV_M_STOP) != 0)
+ ext++;
+ periods = (uint64_t)(uart_getreg(bas, MTK_UART_SAMPLE_COUNT) + 1) *
+ f.bits - f.trim + ext;
+ return ((uint64_t)rclk * f.bits / (div * periods));
+}
+
+/*
+ * Low-level console operations: the ns8250 ones, except that the rate is
+ * set here. Without a rate or clock, the firmware settings are kept.
+ */
+static int
+mtk_uart_probe(struct uart_bas *bas)
+{
+ return (uart_ns8250_ops.probe(bas));
+}
+
+static void
+mtk_uart_init(struct uart_bas *bas, int baudrate, int databits, int stopbits,
+ int parity)
+{
+ struct mtk_uart_divisor d;
+ struct mtk_uart_frame f;
+
+ uart_ns8250_ops.init(bas, 0, databits, stopbits, parity);
+ mtk_uart_frame(&f, databits, stopbits, parity);
+ if (mtk_uart_divisor(bas->rclk, baudrate, &f, &d) == 0)
+ mtk_uart_set_divisor(bas, &d);
+}
+
+static void
+mtk_uart_term(struct uart_bas *bas)
+{
+ uart_ns8250_ops.term(bas);
+}
+
+static void
+mtk_uart_putc(struct uart_bas *bas, int c)
+{
+ uart_ns8250_ops.putc(bas, c);
+}
+
+static int
+mtk_uart_rxready(struct uart_bas *bas)
+{
+ return (uart_ns8250_ops.rxready(bas));
+}
+
+static int
+mtk_uart_getc(struct uart_bas *bas, struct mtx *m)
+{
+ return (uart_ns8250_ops.getc(bas, m));
+}
+
+static struct uart_ops uart_mtk_ops = {
+ .probe = mtk_uart_probe,
+ .init = mtk_uart_init,
+ .term = mtk_uart_term,
+ .putc = mtk_uart_putc,
+ .rxready = mtk_uart_rxready,
+ .getc = mtk_uart_getc,
+};
+
+/*
+ * The baud clock is named "baud" or is the only one. The bus clock is
+ * optional, but one that is listed must be found.
+ */
+static int
+mtk_uart_get_clocks(device_t dev, clk_t *baud, clk_t *bus)
+{
+ phandle_t node;
+ int error;
+
+ node = ofw_bus_get_node(dev);
+ *bus = NULL;
+ if (clk_get_by_ofw_name(dev, node, "baud", baud) != 0 &&
+ clk_get_by_ofw_index(dev, node, 0, baud) != 0)
+ return (ENXIO);
+ error = clk_get_by_ofw_name(dev, node, "bus", bus);
+ if (error != 0 && error != ENOENT) {
+ clk_release(*baud);
+ return (ENXIO);
+ }
+ return (0);
+}
+
+static int
+mtk_uart_enable_clocks(clk_t baud, clk_t bus)
+{
+ int error;
+
+ if (bus != NULL && (error = clk_enable(bus)) != 0)
+ return (error);
+ return (clk_enable(baud));
+}
+
+/* clk_release() also drops the enable references. */
+static void
+mtk_uart_release_clocks(clk_t baud, clk_t bus)
+{
+ clk_release(baud);
+ if (bus != NULL)
+ clk_release(bus);
+}
+
+static int
+mtk_uart_get_rclk(clk_t baud, u_int *rclk)
+{
+ uint64_t freq;
+
+ if (clk_get_freq(baud, &freq) != 0 || freq == 0 || freq > INT_MAX)
+ return (ENXIO);
+ *rclk = freq;
+ return (0);
+}
+
+/*
+ * The ns8250 probe needs the clock rate, which the generic FDT attachment
+ * does not know, and uses the registers. The console runs on the clocks
+ * the firmware set up; other ports have them enabled for the probe only.
+ * The ns8250 probe also sets a rate the 16550 way, so ports other than the
+ * console go back to that mode first.
+ */
+static int
+mtk_uart_bus_probe(struct uart_softc *sc)
+{
+ clk_t baud, bus;
+ int error;
+
+ if (mtk_uart_get_clocks(sc->sc_dev, &baud, &bus) != 0)
+ return (ENXIO);
+ if ((error = mtk_uart_get_rclk(baud, &sc->sc_bas.rclk)) != 0)
+ goto out;
+ if (sc->sc_sysdev == NULL) {
+ if ((error = mtk_uart_enable_clocks(baud, bus)) != 0)
+ goto out;
+ mtk_uart_reset_generator(&sc->sc_bas);
+ } else if (sc->sc_sysdev->bas.rclk == 0)
+ sc->sc_sysdev->bas.rclk = sc->sc_bas.rclk;
+ error = ns8250_bus_probe(sc);
+ if (error == 0)
+ device_set_desc(sc->sc_dev, "MediaTek UART");
+out:
+ mtk_uart_release_clocks(baud, bus);
+ return (error);
+}
+
+static int
+mtk_uart_bus_attach(struct uart_softc *sc)
+{
+ struct mtk_uart_softc *msc = (struct mtk_uart_softc *)sc;
+ device_t dev = sc->sc_dev;
+ int error;
+
+ if (mtk_uart_get_clocks(dev, &msc->baud, &msc->bus) != 0) {
+ device_printf(dev, "cannot get clocks\n");
+ return (ENXIO);
+ }
+ error = clk_set_assigned(dev, ofw_bus_get_node(dev));
+ if (error != 0 && error != ENOENT) {
+ device_printf(dev, "cannot set assigned clocks\n");
+ goto fail;
+ }
+ if ((error = mtk_uart_enable_clocks(msc->baud, msc->bus)) != 0) {
+ device_printf(dev, "cannot enable clocks\n");
+ goto fail;
+ }
+ /* The assigned clocks can change the rate. */
+ if ((error = mtk_uart_get_rclk(msc->baud, &sc->sc_bas.rclk)) != 0)
+ goto fail;
+
+ /* Apply the default pin state, if there is a pin controller. */
+ fdt_pinctrl_configure_by_name(dev, "default");
+ error = ns8250_bus_attach(sc);
+ if (error == 0)
+ return (0);
+fail:
+ mtk_uart_release_clocks(msc->baud, msc->bus);
+ return (error);
+}
+
+static int
+mtk_uart_bus_detach(struct uart_softc *sc)
+{
+ struct mtk_uart_softc *msc = (struct mtk_uart_softc *)sc;
+ int error;
+
+ error = ns8250_bus_detach(sc);
+ /* The low-level console code keeps using the console port. */
+ if (sc->sc_sysdev == NULL)
+ mtk_uart_release_clocks(msc->baud, msc->bus);
+ return (error);
+}
+
+static int
+mtk_uart_bus_param(struct uart_softc *sc, int baudrate, int databits,
+ int stopbits, int parity)
+{
+ struct mtk_uart_divisor d;
+ struct mtk_uart_frame f;
+ int error;
+
+ mtk_uart_frame(&f, databits, stopbits, parity);
+ if (baudrate > 0 &&
+ mtk_uart_divisor(sc->sc_bas.rclk, baudrate, &f, &d) != 0)
+ return (EINVAL);
+ error = ns8250_bus_param(sc, 0, databits, stopbits, parity);
+ if (error != 0 || baudrate <= 0)
+ return (error);
+ uart_lock(sc->sc_hwmtx);
+ mtk_uart_set_divisor(&sc->sc_bas, &d);
+ uart_unlock(sc->sc_hwmtx);
+ return (0);
+}
+
+static int
+mtk_uart_bus_ioctl(struct uart_softc *sc, int request, intptr_t data)
+{
+ int baud;
+
+ if (request != UART_IOCTL_BAUD)
+ return (ns8250_bus_ioctl(sc, request, data));
+ uart_lock(sc->sc_hwmtx);
+ baud = mtk_uart_get_baud(&sc->sc_bas);
+ uart_unlock(sc->sc_hwmtx);
+ if (baud <= 0)
+ return (ENXIO);
+ *(int *)data = baud;
+ return (0);
+}
+
+static kobj_method_t mtk_uart_methods[] = {
+ KOBJMETHOD(uart_probe, mtk_uart_bus_probe),
+ KOBJMETHOD(uart_attach, mtk_uart_bus_attach),
+ KOBJMETHOD(uart_detach, mtk_uart_bus_detach),
+ KOBJMETHOD(uart_flush, ns8250_bus_flush),
+ KOBJMETHOD(uart_getsig, ns8250_bus_getsig),
+ KOBJMETHOD(uart_ioctl, mtk_uart_bus_ioctl),
+ KOBJMETHOD(uart_ipend, ns8250_bus_ipend),
+ KOBJMETHOD(uart_param, mtk_uart_bus_param),
+ KOBJMETHOD(uart_receive, ns8250_bus_receive),
+ KOBJMETHOD(uart_setsig, ns8250_bus_setsig),
+ KOBJMETHOD(uart_transmit, ns8250_bus_transmit),
+ KOBJMETHOD(uart_txbusy, ns8250_bus_txbusy),
+ KOBJMETHOD(uart_grab, ns8250_bus_grab),
+ KOBJMETHOD(uart_ungrab, ns8250_bus_ungrab),
+ KOBJMETHOD_END
+};
+
+/*
+ * No default rate: it comes from the clock framework. A nonzero value
+ * would override it for the console in uart_bus_probe().
+ */
+static struct uart_class uart_mtk_class = {
+ "mtk",
+ mtk_uart_methods,
+ sizeof(struct mtk_uart_softc),
+ .uc_ops = &uart_mtk_ops,
+ .uc_range = MTK_UART_FRACDIV_M + 1,
+ .uc_rclk = 0,
+ .uc_rshift = 2,
+ .uc_riowidth = 4,
+};
+
+static struct ofw_compat_data compat_data[] = {
+ { "mediatek,mt6577-uart", (uintptr_t)&uart_mtk_class },
+ { NULL, 0 }
+};
+UART_FDT_CLASS_AND_DEVICE(compat_data);

File Metadata

Mime Type
text/plain
Expires
Tue, Nov 17, 9:57 AM (2 h, 7 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
41784888
Default Alt Text
D61191.1794909455.diff (14 KB)

Event Timeline