1/* $NetBSD: consinit.c,v 1.28 2015/01/11 19:54:23 is Exp $ */
2
3/*
4 * Copyright (c) 1998
5 * Matthias Drochner. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: consinit.c,v 1.28 2015/01/11 19:54:23 is Exp $");
31
32#include "opt_kgdb.h"
33#include "opt_puc.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 <dev/cons.h>
43
44#include "nullcons.h"
45#include "genfb.h"
46#include "vga.h"
47#include "ega.h"
48#include "pcdisplay.h"
49#include "com_puc.h"
50#if (NVGA > 0) || (NEGA > 0) || (NPCDISPLAY > 0)
51#include <dev/ic/mc6845reg.h>
52#include <dev/ic/pcdisplayvar.h>
53#if (NVGA > 0)
54#include <dev/ic/vgareg.h>
55#include <dev/ic/vgavar.h>
56#endif
57#if (NEGA > 0)
58#include <dev/isa/egavar.h>
59#endif
60#if (NPCDISPLAY > 0)
61#include <dev/isa/pcdisplayvar.h>
62#endif
63#endif
64
65#include "pckbc.h"
66#if (NPCKBC > 0)
67#include <dev/isa/isareg.h>
68#include <dev/ic/i8042reg.h>
69#include <dev/ic/pckbcvar.h>
70#include <dev/pckbport/pckbportvar.h>
71#endif
72#include "pckbd.h" /* for pckbc_machdep_cnattach */
73
74#if (NGENFB > 0)
75#include <dev/wsfb/genfbvar.h>
76#endif
77
78#include "com.h"
79#if (NCOM > 0)
80#include <sys/termios.h>
81#include <dev/ic/comreg.h>
82#include <dev/ic/comvar.h>
83#endif
84#if (NCOM_PUC > 0)
85#include <dev/pci/puccn.h>
86#endif
87
88#include "ukbd.h"
89#if (NUKBD > 0)
90#include <dev/usb/ukbdvar.h>
91#endif
92
93#ifndef CONSDEVNAME
94#define CONSDEVNAME "pc"
95#endif
96
97#if (NCOM > 0)
98#ifndef CONADDR
99#define CONADDR 0x3f8
100#endif
101#ifndef CONSPEED
102#define CONSPEED TTYDEF_SPEED
103#endif
104#ifndef CONMODE
105#define CONMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
106#endif
107int comcnmode = CONMODE;
108#endif /* NCOM */
109
110const struct btinfo_console default_consinfo = {
111 {0, 0},
112 CONSDEVNAME,
113#if (NCOM > 0)
114 CONADDR, CONSPEED
115#else
116 0, 0
117#endif
118};
119
120#ifdef KGDB
121#ifndef KGDB_DEVNAME
122#define KGDB_DEVNAME "com"
123#endif
124const char kgdb_devname[] = KGDB_DEVNAME;
125
126#if (NCOM > 0)
127#ifndef KGDB_DEVADDR
128#define KGDB_DEVADDR 0x3f8
129#endif
130int comkgdbaddr = KGDB_DEVADDR;
131#ifndef KGDB_DEVRATE
132#define KGDB_DEVRATE TTYDEF_SPEED
133#endif
134int comkgdbrate = KGDB_DEVRATE;
135#ifndef KGDB_DEVMODE
136#define KGDB_DEVMODE ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8) /* 8N1 */
137#endif
138int comkgdbmode = KGDB_DEVMODE;
139#endif /* NCOM */
140
141#endif /* KGDB */
142
143/*
144 * consinit:
145 * initialize the system console.
146 * XXX - shouldn't deal with this initted thing, but then,
147 * it shouldn't be called from init386 either.
148 */
149void
150consinit(void)
151{
152 const struct btinfo_console *consinfo;
153#if (NGENFB > 0)
154 const struct btinfo_framebuffer *fbinfo;
155#endif
156 static int initted;
157#if (NCOM > 0)
158 int rv;
159#endif
160
161 if (initted)
162 return;
163 initted = 1;
164
165#ifndef CONS_OVERRIDE
166 consinfo = lookup_bootinfo(BTINFO_CONSOLE);
167 if (!consinfo)
168#endif
169 consinfo = &default_consinfo;
170
171#if (NGENFB > 0)
172 fbinfo = lookup_bootinfo(BTINFO_FRAMEBUFFER);
173#endif
174
175 if (!strcmp(consinfo->devname, "pc")) {
176 int error;
177#if (NGENFB > 0)
178 if (fbinfo && fbinfo->physaddr > 0) {
179 if (x86_genfb_cnattach() == -1) {
180 initted = 0; /* defer */
181 return;
182 }
183 genfb_cnattach();
184 goto dokbd;
185 }
186 genfb_disable();
187#endif
188#if (NVGA > 0)
189 if (!vga_cnattach(x86_bus_space_io, x86_bus_space_mem,
190 -1, 1))
191 goto dokbd;
192#endif
193#if (NEGA > 0)
194 if (!ega_cnattach(x86_bus_space_io, x86_bus_space_mem))
195 goto dokbd;
196#endif
197#if (NPCDISPLAY > 0)
198 if (!pcdisplay_cnattach(x86_bus_space_io, x86_bus_space_mem))
199 goto dokbd;
200#endif
201 if (0) goto dokbd; /* XXX stupid gcc */
202dokbd:
203 error = ENODEV;
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, error=%d\n",
214 error);
215 return;
216 }
217#if (NCOM > 0)
218 if (!strcmp(consinfo->devname, "com")) {
219 int addr = consinfo->addr;
220 int speed = consinfo->speed;
221
222#if (NCOM_PUC > 0) && defined(PUC_CNAUTO)
223 puc_cnprobe(NULL);
224 rv = puc_cninit(NULL);
225 if (rv == 0)
226 return;
227#endif
228
229 if (addr == 0)
230 addr = CONADDR;
231 if (speed == 0)
232 speed = CONSPEED;
233
234 rv = comcnattach(x86_bus_space_io, addr, speed,
235 COM_FREQ, COM_TYPE_NORMAL, comcnmode);
236 if (rv != 0)
237 panic("can't init serial console @%x", consinfo->addr);
238 return;
239 }
240#endif
241#if (NNULLCONS > 0)
242 if (!strcmp(consinfo->devname, "nullcons")) {
243 void nullcninit(struct consdev *cn);
244
245 nullcninit(0);
246 return;
247 }
248#endif
249 panic("invalid console device %s", consinfo->devname);
250}
251
252#ifdef KGDB
253void
254kgdb_port_init(void)
255{
256#if (NCOM > 0)
257 if(!strcmp(kgdb_devname, "com")) {
258 com_kgdb_attach(x86_bus_space_io, comkgdbaddr, comkgdbrate,
259 COM_FREQ, COM_TYPE_NORMAL, comkgdbmode);
260 }
261#endif
262}
263#endif
264