All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Glenn Washburn <development@efficientek.com>
Cc: The development of GNU GRUB <grub-devel@gnu.org>,
	Daniel Kiper <dkiper@net-space.pl>
Subject: Re: [PATCH] term/serial: Add support for PCI serial devices
Date: Thu, 25 Aug 2022 10:10:45 +0200	[thread overview]
Message-ID: <YwcuhbNktRBM6sPT@worktop.programming.kicks-ass.net> (raw)
In-Reply-To: <20220824183627.62abec4e@crass-HP-ZBook-15-G2>



How's this then? seems to build. If this looks allright, I'll post it as
v2 I suppose.

---
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index 5212dfab1369..c0683da2353b 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -224,12 +224,14 @@ kernel = {
 
   i386_efi = kern/i386/efi/init.c;
   i386_efi = bus/pci.c;
+  i386_efi_cppflags = '-DGRUB_HAS_PCI';
 
   x86_64 = kern/x86_64/dl.c;
   x86_64_xen = kern/x86_64/dl.c;
   x86_64_efi = kern/x86_64/efi/callwrap.S;
   x86_64_efi = kern/i386/efi/init.c;
   x86_64_efi = bus/pci.c;
+  x86_64_efi_cppflags = '-DGRUB_HAS_PCI';
 
   xen = kern/i386/tsc.c;
   xen = kern/i386/xen/tsc.c;
@@ -271,6 +273,7 @@ kernel = {
   i386_pc = term/i386/pc/console.c;
 
   i386_qemu = bus/pci.c;
+  i386_qemu_cppflags = '-DGRUB_HAS_PCI';
   i386_qemu = kern/vga_init.c;
   i386_qemu = kern/i386/qemu/mmap.c;
 
@@ -303,6 +306,7 @@ kernel = {
   mips_loongson = bus/bonito.c;
   mips_loongson = bus/cs5536.c;
   mips_loongson = bus/pci.c;
+  mips_loongson_cppflags = '-DGRUB_HAS_PCI';
   mips_loongson = kern/mips/loongson/init.c;
   mips_loongson = term/at_keyboard.c;
   mips_loongson = term/ps2.c;
@@ -2047,6 +2051,8 @@ module = {
   ieee1275 = term/ieee1275/serial.c;
   mips_arc = term/arc/serial.c;
   efi = term/efi/serial.c;
+  x86 = term/pci/serial.c;
+  mips_loongson = term/pci/serial.c;
 
   enable = terminfomodule;
   enable = ieee1275;
diff --git a/grub-core/term/pci/serial.c b/grub-core/term/pci/serial.c
new file mode 100644
index 000000000000..581d0760f9f4
--- /dev/null
+++ b/grub-core/term/pci/serial.c
@@ -0,0 +1,83 @@
+/*
+ *  GRUB  --  GRand Unified Bootloader
+ *  Copyright (C) 2000,2001,2002,2003,2004,2005,2007,2008,2009,2010  Free Software Foundation, Inc.
+ *
+ *  GRUB is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  GRUB is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <grub/serial.h>
+#include <grub/term.h>
+#include <grub/types.h>
+#include <grub/pci.h>
+#include <grub/mm.h>
+#include <grub/misc.h>
+
+static int
+find_pciserial (grub_pci_device_t dev, grub_pci_id_t pciid, void *data)
+{
+  grub_pci_address_t cmd_addr, class_addr, bar_addr;
+  struct grub_serial_port *port;
+  grub_uint32_t class, bar;
+  grub_uint16_t cmdreg;
+  int *nr = data;
+  int err;
+
+  (void)pciid;
+
+  cmd_addr = grub_pci_make_address (dev, GRUB_PCI_REG_COMMAND);
+  cmdreg = grub_pci_read (cmd_addr);
+
+  class_addr = grub_pci_make_address (dev, GRUB_PCI_REG_REVISION);
+  class = grub_pci_read (class_addr);
+
+  bar_addr = grub_pci_make_address (dev, GRUB_PCI_REG_ADDRESS_REG0);
+  bar = grub_pci_read (bar_addr);
+
+  /* 16550 compattible MODEM or SERIAL */
+  if (((class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_MODEM &&
+       (class >> 16) != GRUB_PCI_CLASS_COMMUNICATION_SERIAL) ||
+      ((class >> 8) & 0xff) != GRUB_PCI_SERIAL_16550_COMPATIBLE)
+    return 0;
+
+  if ((bar & GRUB_PCI_ADDR_SPACE_MASK) != GRUB_PCI_ADDR_SPACE_IO)
+    return 0;
+
+  port = grub_zalloc (sizeof (*port));
+  if (!port)
+    return 0;
+
+  grub_pci_write (cmd_addr, cmdreg | GRUB_PCI_COMMAND_IO_ENABLED);
+
+  port->name = grub_xasprintf ("pci%d", (*nr)++);
+  port->driver = &grub_ns8250_driver;
+  port->port = bar & GRUB_PCI_ADDR_IO_MASK;
+  err = grub_serial_config_defaults (port);
+  if (err)
+    {
+      grub_print_error ();
+      return 0;
+    }
+
+  grub_serial_register (port);
+
+  return 0;
+}
+
+void
+grub_pciserial_init (void)
+{
+  int nr = 0;
+
+  grub_pci_iterate (find_pciserial, &nr);
+}
diff --git a/grub-core/term/serial.c b/grub-core/term/serial.c
index 842322b1c607..09c9e1cd1fdc 100644
--- a/grub-core/term/serial.c
+++ b/grub-core/term/serial.c
@@ -448,6 +448,9 @@ GRUB_MOD_INIT(serial)
 #ifdef GRUB_MACHINE_ARC
   grub_arcserial_init ();
 #endif
+#ifdef GRUB_HAS_PCI
+  grub_pciserial_init ();
+#endif
 }
 
 GRUB_MOD_FINI(serial)
diff --git a/include/grub/pci.h b/include/grub/pci.h
index 262c89b748bb..b228bb1b71fd 100644
--- a/include/grub/pci.h
+++ b/include/grub/pci.h
@@ -80,8 +80,13 @@
 
 #define  GRUB_PCI_STATUS_DEVSEL_TIMING_SHIFT 9
 #define  GRUB_PCI_STATUS_DEVSEL_TIMING_MASK 0x0600
+
 #define  GRUB_PCI_CLASS_SUBCLASS_VGA  0x0300
 #define  GRUB_PCI_CLASS_SUBCLASS_USB  0x0c03
+#define  GRUB_PCI_CLASS_COMMUNICATION_SERIAL  0x0700
+#define  GRUB_PCI_CLASS_COMMUNICATION_MODEM   0x0703
+
+#define  GRUB_PCI_SERIAL_16550_COMPATIBLE 0x02
 
 #ifndef ASM_FILE
 
diff --git a/include/grub/serial.h b/include/grub/serial.h
index 67379de45dd1..c57aed19ff2b 100644
--- a/include/grub/serial.h
+++ b/include/grub/serial.h
@@ -193,6 +193,8 @@ const char *
 grub_arcserial_add_port (const char *path);
 #endif
 
+void grub_pciserial_init (void);
+
 struct grub_serial_port *grub_serial_find (const char *name);
 extern struct grub_serial_driver grub_ns8250_driver;
 void EXPORT_FUNC(grub_serial_unregister_driver) (struct grub_serial_driver *driver);


  parent reply	other threads:[~2022-08-25  8:11 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-24 11:28 [PATCH] term/serial: Add support for PCI serial devices Peter Zijlstra
2022-08-24 20:13 ` Glenn Washburn
2022-08-24 21:01   ` Peter Zijlstra
2022-08-24 23:36     ` Glenn Washburn
2022-08-25  7:36       ` Peter Zijlstra
2022-08-25  7:57       ` Peter Zijlstra
2022-08-25 15:51         ` Glenn Washburn
2022-08-25 19:33           ` Peter Zijlstra
2022-08-26  1:01             ` Glenn Washburn
2022-08-25  8:10       ` Peter Zijlstra [this message]
2022-08-25 19:23         ` Glenn Washburn
2022-08-25 19:59           ` Peter Zijlstra
2022-08-26  4:02             ` Glenn Washburn
2022-08-25  8:17   ` Peter Zijlstra
2022-08-25 15:46     ` Glenn Washburn
2022-08-25  8:03 ` Gerd Hoffmann

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=YwcuhbNktRBM6sPT@worktop.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=development@efficientek.com \
    --cc=dkiper@net-space.pl \
    --cc=grub-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.