Received: from list by lists.gnu.org with archive (Exim 4.90_1) id 1p8EqK-0002YD-2p for mharc-grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:12 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p8EqH-0002Xn-TR for grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:09 -0500 Received: from gate.crashing.org ([63.228.1.57]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p8EqF-0002gk-VU for grub-devel@gnu.org; Thu, 22 Dec 2022 01:14:09 -0500 Received: from u0f3226c756e75a.ant.amazon.com (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 2BM6CxBh025856; Thu, 22 Dec 2022 00:13:05 -0600 From: Benjamin Herrenschmidt To: grub-devel@gnu.org Cc: Daniel Kiper Subject: [PATCH v2 3/8] ns8250: Add base support for MMIO UARTs Date: Thu, 22 Dec 2022 17:12:52 +1100 Message-Id: <20221222061257.1598483-4-benh@kernel.crashing.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20221222061257.1598483-1-benh@kernel.crashing.org> References: <20221222061257.1598483-1-benh@kernel.crashing.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Received-SPF: pass client-ip=63.228.1.57; envelope-from=benh@kernel.crashing.org; helo=gate.crashing.org X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Dec 2022 06:14:10 -0000 From: Benjamin Herrenschmidt This adds the ability for the driver to access UARTs via MMIO instead of PIO selectively at runtime, and exposes a new function to add an MMIO port. In an ideal world, MMIO accessors would be generic and have architecture specific memory barriers. However, existing drivers don't have them and most of those "bare metal" drivers tend to be for x86 which doesn't need them. If necessary, those can be added later. Signed-off-by: Benjamin Herrenschmidt --- grub-core/term/ns8250.c | 80 ++++++++++++++++++++++++++++++++--------- grub-core/term/serial.c | 8 +++-- include/grub/serial.h | 11 +++++- 3 files changed, 80 insertions(+), 19 deletions(-) diff --git a/grub-core/term/ns8250.c b/grub-core/term/ns8250.c index 83b25990c..43b9b3719 100644 --- a/grub-core/term/ns8250.c +++ b/grub-core/term/ns8250.c @@ -43,6 +43,24 @@ static int dead_ports = 0; #define DEFAULT_BASE_CLOCK 115200 #endif +static grub_uint8_t +ns8250_reg_read (struct grub_serial_port *port, grub_addr_t reg) +{ + asm volatile("" : : : "memory"); + if (port->mmio) + return *((volatile grub_uint8_t *) (port->mmio_base + reg)); + return grub_inb (port->port + reg); +} + +static void +ns8250_reg_write (struct grub_serial_port *port, grub_uint8_t, grub_addr_t reg) +{ + asm volatile("" : : : "memory"); + if (port->mmio) + *((volatile grub_uint8_t *) (port->mmio_base + reg)) = value; + else + grub_outb (value, port->port + reg); +} /* Convert speed to divisor. */ static unsigned short @@ -93,43 +111,42 @@ do_real_config (struct grub_serial_port *port) divisor = serial_get_divisor (port, &port->config); /* Turn off the interrupt. */ - grub_outb (0, port->port + UART_IER); + ns8250_reg_write (port, 0, UART_IER); /* Set DLAB. */ - grub_outb (UART_DLAB, port->port + UART_LCR); + ns8250_reg_write (port, UART_DLAB, UART_LCR); - /* Set the baud rate. */ - grub_outb (divisor & 0xFF, port->port + UART_DLL); - grub_outb (divisor >> 8, port->port + UART_DLH); + ns8250_reg_write (port, divisor & 0xFF, UART_DLL); + ns8250_reg_write (port, divisor >> 8, UART_DLH); /* Set the line status. */ status |= (parities[port->config.parity] | (port->config.word_len - 5) | stop_bits[port->config.stop_bits]); - grub_outb (status, port->port + UART_LCR); + ns8250_reg_write (port, status, UART_LCR); if (port->config.rtscts) { /* Enable the FIFO. */ - grub_outb (UART_ENABLE_FIFO_TRIGGER1, port->port + UART_FCR); + ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER1, UART_FCR); /* Turn on DTR and RTS. */ - grub_outb (UART_ENABLE_DTRRTS, port->port + UART_MCR); + ns8250_reg_write (port, UART_ENABLE_DTRRTS, UART_MCR); } else { /* Enable the FIFO. */ - grub_outb (UART_ENABLE_FIFO_TRIGGER14, port->port + UART_FCR); + ns8250_reg_write (port, UART_ENABLE_FIFO_TRIGGER14, UART_FCR); /* Turn on DTR, RTS, and OUT2. */ - grub_outb (UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, port->port + UART_MCR); + ns8250_reg_write (port, UART_ENABLE_DTRRTS | UART_ENABLE_OUT2, UART_MCR); } /* Drain the input buffer. */ endtime = grub_get_time_ms () + 1000; - while (grub_inb (port->port + UART_LSR) & UART_DATA_READY) + while (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY) { - grub_inb (port->port + UART_RX); + ns8250_reg_read (port, UART_RX); if (grub_get_time_ms () > endtime) { port->broken = 1; @@ -145,8 +162,8 @@ static int serial_hw_fetch (struct grub_serial_port *port) { do_real_config (port); - if (grub_inb (port->port + UART_LSR) & UART_DATA_READY) - return grub_inb (port->port + UART_RX); + if (ns8250_reg_read (port, UART_LSR) & UART_DATA_READY) + return ns8250_reg_read (port, UART_RX); return -1; } @@ -166,7 +183,7 @@ serial_hw_put (struct grub_serial_port *port, const int c) else endtime = grub_get_time_ms () + 200; /* Wait until the transmitter holding register is empty. */ - while ((grub_inb (port->port + UART_LSR) & UART_EMPTY_TRANSMITTER) == 0) + while ((ns8250_reg_read (port, UART_LSR) & UART_EMPTY_TRANSMITTER) == 0) { if (grub_get_time_ms () > endtime) { @@ -179,7 +196,7 @@ serial_hw_put (struct grub_serial_port *port, const int c) if (port->broken) port->broken--; - grub_outb (c, port->port + UART_TX); + ns8250_reg_write (port, c, UART_TX); } /* Initialize a serial device. PORT is the port number for a serial device. @@ -262,6 +279,7 @@ grub_ns8250_init (void) com_ports[i].name = com_names[i]; com_ports[i].driver = &grub_ns8250_driver; com_ports[i].port = serial_hw_io_addr[i]; + com_ports[i].mmio = false; err = grub_serial_config_defaults (&com_ports[i]); if (err) grub_print_error (); @@ -289,6 +307,7 @@ grub_serial_ns8250_add_port (grub_port_t port) { struct grub_serial_port *p; unsigned i; + for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++) if (com_ports[i].port == port) { @@ -316,8 +335,37 @@ grub_serial_ns8250_add_port (grub_port_t port) } p->driver = &grub_ns8250_driver; grub_serial_config_defaults (p); + p->mmio = false; p->port = port; grub_serial_register (p); return p->name; } + +char * +grub_serial_ns8250_add_mmio (grub_addr_t addr) +{ + struct grub_serial_port *p; + unsigned i; + + for (i = 0; i < GRUB_SERIAL_PORT_NUM; i++) + if (com_ports[i].mmio && com_ports[i].mmio_base == addr) + return com_names[i]; + + p = grub_malloc (sizeof (*p)); + if (p == NULL) + return NULL; + p->name = grub_xasprintf ("mmio,%llx", (unsigned long long) addr); + if (p->name == NULL) + { + grub_free (p); + return NULL; + } + p->driver = &grub_ns8250_driver; + grub_serial_config_defaults (p); + p->mmio = true; + p->mmio_base = addr; + grub_serial_register (p); + + return p->name; +} diff --git a/grub-core/term/serial.c b/grub-core/term/serial.c index 842322b1c..c8f5f02db 100644 --- a/grub-core/term/serial.c +++ b/grub-core/term/serial.c @@ -201,8 +201,12 @@ grub_cmd_serial (grub_extcmd_context_t ctxt, int argc, char **args) if (state[OPTION_PORT].set) { - grub_snprintf (pname, sizeof (pname), "port%lx", - grub_strtoul (state[1].arg, 0, 0)); + if (grub_memcmp (state[OPTION_PORT].arg, "mmio", 4) == 0) + grub_snprintf(pname, sizeof (pname), "%s", state[1].arg); + else + grub_snprintf (pname, sizeof (pname), "port%lx", + grub_strtoul (state[1].arg, 0, 0)); + name = pname; } diff --git a/include/grub/serial.h b/include/grub/serial.h index 67379de45..5e008f0f5 100644 --- a/include/grub/serial.h +++ b/include/grub/serial.h @@ -86,9 +86,17 @@ struct grub_serial_port */ union { + struct + { + bool mmio; + union + { #if defined(__mips__) || defined (__i386__) || defined (__x86_64__) - grub_port_t port; + grub_port_t port; #endif + grub_addr_t mmio_base; + }; + }; struct { grub_usb_device_t usbdev; @@ -178,6 +186,7 @@ grub_serial_config_defaults (struct grub_serial_port *port) #if defined(__mips__) || defined (__i386__) || defined (__x86_64__) void grub_ns8250_init (void); char *grub_serial_ns8250_add_port (grub_port_t port); +char *grub_serial_ns8250_add_mmio (grub_addr_t addr); #endif #ifdef GRUB_MACHINE_IEEE1275 void grub_ofserial_init (void); -- 2.34.1