1 /*        $NetBSD: consinit.c,v 1.18 2023/10/17 12:07:42 bouyer Exp $ */
2 /*        NetBSD: consinit.c,v 1.4 2004/03/13 17:31:34 bjh21 Exp      */
3 
4 /*
5  * Copyright (c) 1998
6  *        Matthias Drochner.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  */
29 
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: consinit.c,v 1.18 2023/10/17 12:07:42 bouyer Exp $");
32 
33 #include "opt_kgdb.h"
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38 #include <sys/bus.h>
39 #include <machine/bootinfo.h>
40 #include <arch/x86/include/genfb_machdep.h>
41 
42 #include "xencons.h"
43 #include "vga.h"
44 #include "ega.h"
45 #include "pcdisplay.h"
46 #include "genfb.h"
47 #if (NVGA > 0) || (NEGA > 0) || (NPCDISPLAY > 0)
48 #include <dev/ic/mc6845reg.h>
49 #include <dev/ic/pcdisplayvar.h>
50 #if (NVGA > 0)
51 #include <dev/ic/vgareg.h>
52 #include <dev/ic/vgavar.h>
53 #endif
54 #if (NEGA > 0)
55 #include <dev/isa/egavar.h>
56 #endif
57 #if (NPCDISPLAY > 0)
58 #include <dev/isa/pcdisplayvar.h>
59 #endif
60 #endif
61 
62 #include "pckbc.h"
63 #if (NPCKBC > 0)
64 #include <dev/isa/isareg.h>
65 #include <dev/ic/i8042reg.h>
66 #include <dev/ic/pckbcvar.h>
67 #endif
68 #include "pckbd.h" /* for pckbc_machdep_cnattach */
69 
70 #include "ukbd.h"
71 #if (NUKBD > 0)
72 #include <dev/usb/ukbdvar.h>
73 #endif
74 
75 #if (NGENFB > 0)
76 #include <dev/wsfb/genfbvar.h>
77 #endif
78 
79 #include "opt_xen.h"
80 #if (XEN > 0)
81 #include <xen/xen.h>
82 #include <dev/pckbport/pckbportvar.h>
83 #include <xen/hypervisor.h>
84 #include "xen_def_cons.h"
85 #endif
86 
87 #include "com.h"
88 #if (NCOM > 0)
89 #include <sys/termios.h>
90 #include <dev/ic/comreg.h>
91 #include <dev/ic/comvar.h>
92 #endif
93 
94 #ifndef CONSDEVNAME
95 #define CONSDEVNAME "pc"
96 #endif
97 
98 #if (NCOM > 0)
99 #ifndef CONADDR
100 #define CONADDR 0x3f8
101 #endif
102 #ifndef CONSPEED
103 #define CONSPEED TTYDEF_SPEED
104 #endif
105 #ifndef CONMODE
106 #define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
107 #endif
108 int comcnmode = CONMODE;
109 #endif /* NCOM */
110 
111 const struct btinfo_console default_consinfo = {
112           {0, 0},
113           CONSDEVNAME,
114 #if (NCOM > 0)
115           CONADDR, CONSPEED
116 #else
117           0, 0
118 #endif
119 };
120 
121 #ifdef KGDB
122 #ifndef KGDB_DEVNAME
123 #define KGDB_DEVNAME "com"
124 #endif
125 const char kgdb_devname[] = KGDB_DEVNAME;
126 
127 #if (NCOM > 0)
128 #ifndef KGDB_DEVADDR
129 #define KGDB_DEVADDR 0x3f8
130 #endif
131 int comkgdbaddr = KGDB_DEVADDR;
132 #ifndef KGDB_DEVRATE
133 #define KGDB_DEVRATE TTYDEF_SPEED
134 #endif
135 int comkgdbrate = KGDB_DEVRATE;
136 #ifndef KGDB_DEVMODE
137 #define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
138 #endif
139 int comkgdbmode = KGDB_DEVMODE;
140 #endif /* NCOM */
141 
142 #endif /* KGDB */
143 
144 /*
145  * consinit:
146  * initialize the system console.
147  * XXX - shouldn't deal with this initted thing, but then,
148  * it shouldn't be called from init386 either.
149  */
150 void
consinit(void)151 consinit(void)
152 {
153           static int initted = 0;
154           union xen_cmdline_parseinfo xcp;
155 #if (NGENFB > 0)
156         const struct btinfo_framebuffer *fbinfo = NULL;
157 #endif
158 
159 
160           if (initted) {
161                     return;
162           }
163 
164           xen_early_console();
165 
166 #if (NGENFB > 0) && defined(DOM0OPS)
167           if (xendomain_is_dom0())
168                     fbinfo = xen_genfb_getbtinfo();
169 #endif
170 
171           initted = 1;
172           xen_parse_cmdline(XEN_PARSE_CONSOLE, &xcp);
173 
174 #if (NVGA > 0) || (NGENFB > 0)
175           if (xendomain_is_privileged()) {
176 #ifdef CONS_OVERRIDE
177                     if (strcmp(default_consinfo.devname, "tty0") == 0 ||
178                         strcmp(default_consinfo.devname, "pc") == 0) {
179 #else
180                     if (strcmp(xcp.xcp_console, "tty0") == 0 || /* linux name */
181                         strcmp(xcp.xcp_console, "pc") == 0) { /* NetBSD name */
182 #endif /* CONS_OVERRIDE */
183                               int error;
184 
185 #if (NGENFB > 0)
186                               if (fbinfo && fbinfo->physaddr > 0) {
187                                         if (x86_genfb_cnattach() == -1) {
188                                                   initted = 0; /* defer */
189                                                   return;
190                                         }
191                                         genfb_cnattach();
192                                         goto dokbd;
193                               } else {
194                                         genfb_disable();
195                               }
196 #endif
197                               vga_cnattach(x86_bus_space_io, x86_bus_space_mem,
198                                   -1, 1);
199 #if (NGENFB > 0)
200 dokbd:
201 #endif
202                               error = ENODEV;
203 
204 #if (NPCKBC > 0)
205                               error = pckbc_cnattach(x86_bus_space_io, IO_KBD, KBCMDP,
206                                   PCKBC_KBD_SLOT, 0);
207 #endif
208 #if (NUKBD > 0)
209                               if (error)
210                                         error = ukbd_cnattach();
211 #endif
212                               if (error)
213                                         printf("WARNING: no console keyboard, "
214                                             "error=%d\n", error);
215                               return;
216                     }
217           }
218 #endif /* NVGA */
219 #if NXENCONS > 0
220           xenconscn_attach();
221           return;
222 #endif /* NXENCONS */
223           panic("consinit: no console");
224 }
225 
226 #ifdef KGDB
227 void
228 kgdb_port_init(void)
229 {
230 #if (NCOM > 0)
231           if(!strcmp(kgdb_devname, "com")) {
232                     bus_space_tag_t tag = x86_bus_space_io;
233 
234                     com_kgdb_attach(tag, comkgdbaddr, comkgdbrate, COM_FREQ,
235                         COM_TYPE_NORMAL, comkgdbmode);
236           }
237 #endif
238 }
239 #endif
240