xref: /freebsd/sys/dev/cfe/cfe_console.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
1ae36cccdSWarner Losh /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni  *
4ae36cccdSWarner Losh  * Copyright (c) 2007 Bruce M. Simpson.
5ae36cccdSWarner Losh  * All rights reserved.
6ae36cccdSWarner Losh  *
7ae36cccdSWarner Losh  * Redistribution and use in source and binary forms, with or without
8ae36cccdSWarner Losh  * modification, are permitted provided that the following conditions
9ae36cccdSWarner Losh  * are met:
10ae36cccdSWarner Losh  * 1. Redistributions of source code must retain the above copyright
11ae36cccdSWarner Losh  *    notice, this list of conditions and the following disclaimer.
12ae36cccdSWarner Losh  * 2. Redistributions in binary form must reproduce the above copyright
13ae36cccdSWarner Losh  *    notice, this list of conditions and the following disclaimer in the
14ae36cccdSWarner Losh  *    documentation and/or other materials provided with the distribution.
15ae36cccdSWarner Losh  *
16ae36cccdSWarner Losh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17ae36cccdSWarner Losh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18ae36cccdSWarner Losh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ae36cccdSWarner Losh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20ae36cccdSWarner Losh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ae36cccdSWarner Losh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22ae36cccdSWarner Losh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23ae36cccdSWarner Losh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24ae36cccdSWarner Losh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25ae36cccdSWarner Losh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26ae36cccdSWarner Losh  * SUCH DAMAGE.
27ae36cccdSWarner Losh  */
28ae36cccdSWarner Losh 
29ae36cccdSWarner Losh #include <sys/param.h>
30ae36cccdSWarner Losh #include <sys/kdb.h>
31ae36cccdSWarner Losh #include <sys/kernel.h>
32ae36cccdSWarner Losh #include <sys/priv.h>
33ae36cccdSWarner Losh #include <sys/systm.h>
34ae36cccdSWarner Losh #include <sys/types.h>
35ae36cccdSWarner Losh #include <sys/conf.h>
36ae36cccdSWarner Losh #include <sys/cons.h>
37ae36cccdSWarner Losh #include <sys/consio.h>
38ae36cccdSWarner Losh #include <sys/tty.h>
39ae36cccdSWarner Losh 
40ae36cccdSWarner Losh #include <dev/cfe/cfe_api.h>
41ae36cccdSWarner Losh #include <dev/cfe/cfe_error.h>
42ae36cccdSWarner Losh 
43ae36cccdSWarner Losh #include <ddb/ddb.h>
44ae36cccdSWarner Losh 
45ae36cccdSWarner Losh #ifndef	CFECONS_POLL_HZ
468e8ee9aaSWarner Losh #define	CFECONS_POLL_HZ	4
47ae36cccdSWarner Losh #endif
48ae36cccdSWarner Losh #define CFEBURSTLEN	128	/* max number of bytes to write in one chunk */
49ae36cccdSWarner Losh 
506f079ba1SWarner Losh static tsw_open_t cfe_tty_open;
516f079ba1SWarner Losh static tsw_close_t cfe_tty_close;
526f079ba1SWarner Losh static tsw_outwakeup_t cfe_tty_outwakeup;
53ae36cccdSWarner Losh 
546f079ba1SWarner Losh static struct ttydevsw cfe_ttydevsw = {
556f079ba1SWarner Losh 	.tsw_flags	= TF_NOPREFIX,
566f079ba1SWarner Losh 	.tsw_open	= cfe_tty_open,
576f079ba1SWarner Losh 	.tsw_close	= cfe_tty_close,
586f079ba1SWarner Losh 	.tsw_outwakeup	= cfe_tty_outwakeup,
59ae36cccdSWarner Losh };
60ae36cccdSWarner Losh 
61ae36cccdSWarner Losh static int			conhandle = -1;
62ae36cccdSWarner Losh /* XXX does cfe have to poll? */
63ae36cccdSWarner Losh static int			polltime;
64f1f7034dSJohn Baldwin static struct callout		cfe_timer;
65ae36cccdSWarner Losh 
664cf75455SRobert Watson #if defined(KDB)
67ae36cccdSWarner Losh static int			alt_break_state;
68ae36cccdSWarner Losh #endif
69ae36cccdSWarner Losh 
70ae36cccdSWarner Losh static void	cfe_timeout(void *);
71ae36cccdSWarner Losh 
72ae36cccdSWarner Losh static cn_probe_t	cfe_cnprobe;
73ae36cccdSWarner Losh static cn_init_t	cfe_cninit;
74ae36cccdSWarner Losh static cn_term_t	cfe_cnterm;
75ae36cccdSWarner Losh static cn_getc_t	cfe_cngetc;
76ae36cccdSWarner Losh static cn_putc_t	cfe_cnputc;
779976156fSAndriy Gapon static cn_grab_t	cfe_cngrab;
789976156fSAndriy Gapon static cn_ungrab_t	cfe_cnungrab;
79ae36cccdSWarner Losh 
80ae36cccdSWarner Losh CONSOLE_DRIVER(cfe);
81ae36cccdSWarner Losh 
82ae36cccdSWarner Losh static void
cn_drvinit(void * unused)83ae36cccdSWarner Losh cn_drvinit(void *unused)
84ae36cccdSWarner Losh {
856f079ba1SWarner Losh 	struct tty *tp;
86ae36cccdSWarner Losh 
87ae36cccdSWarner Losh 	if (cfe_consdev.cn_pri != CN_DEAD &&
88ae36cccdSWarner Losh 	    cfe_consdev.cn_name[0] != '\0') {
89c5e30cc0SEd Schouten 		tp = tty_alloc(&cfe_ttydevsw, NULL);
90f1f7034dSJohn Baldwin 		callout_init_mtx(&cfe_timer, tty_getlock(tp), 0);
9161f7c762SNeel Natu 		tty_makedev(tp, NULL, "cfecons");
92ae36cccdSWarner Losh 	}
93ae36cccdSWarner Losh }
94ae36cccdSWarner Losh 
95ae36cccdSWarner Losh static int
cfe_tty_open(struct tty * tp)966f079ba1SWarner Losh cfe_tty_open(struct tty *tp)
97ae36cccdSWarner Losh {
98ae36cccdSWarner Losh 	polltime = hz / CFECONS_POLL_HZ;
996f079ba1SWarner Losh 	if (polltime < 1)
100ae36cccdSWarner Losh 		polltime = 1;
101f1f7034dSJohn Baldwin 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
1026f079ba1SWarner Losh 
1036f079ba1SWarner Losh 	return (0);
104ae36cccdSWarner Losh }
105ae36cccdSWarner Losh 
1066f079ba1SWarner Losh static void
cfe_tty_close(struct tty * tp)1076f079ba1SWarner Losh cfe_tty_close(struct tty *tp)
108ae36cccdSWarner Losh {
109ae36cccdSWarner Losh 
110f1f7034dSJohn Baldwin 	callout_stop(&cfe_timer);
111ae36cccdSWarner Losh }
112ae36cccdSWarner Losh 
113ae36cccdSWarner Losh static void
cfe_tty_outwakeup(struct tty * tp)1146f079ba1SWarner Losh cfe_tty_outwakeup(struct tty *tp)
115ae36cccdSWarner Losh {
11661f7c762SNeel Natu 	int len, written, rc;
117ae36cccdSWarner Losh 	u_char buf[CFEBURSTLEN];
118ae36cccdSWarner Losh 
1196f079ba1SWarner Losh 	for (;;) {
1206f079ba1SWarner Losh 		len = ttydisc_getc(tp, buf, sizeof buf);
1216f079ba1SWarner Losh 		if (len == 0)
1226f079ba1SWarner Losh 			break;
12361f7c762SNeel Natu 
12461f7c762SNeel Natu 		written = 0;
12561f7c762SNeel Natu 		while (written < len) {
12661f7c762SNeel Natu 			rc = cfe_write(conhandle, &buf[written], len - written);
12761f7c762SNeel Natu 			if (rc < 0)
12861f7c762SNeel Natu 				break;
12961f7c762SNeel Natu 			written += rc;
13061f7c762SNeel Natu 		}
131ae36cccdSWarner Losh 	}
132ae36cccdSWarner Losh }
133ae36cccdSWarner Losh 
134ae36cccdSWarner Losh static void
cfe_timeout(void * v)135ae36cccdSWarner Losh cfe_timeout(void *v)
136ae36cccdSWarner Losh {
137ae36cccdSWarner Losh 	struct	tty *tp;
138ae36cccdSWarner Losh 	int 	c;
139ae36cccdSWarner Losh 
140ae36cccdSWarner Losh 	tp = (struct tty *)v;
141ae36cccdSWarner Losh 
14223d53268SKyle Evans 	tty_assert_locked(tp);
1436f079ba1SWarner Losh 	while ((c = cfe_cngetc(NULL)) != -1)
1446f079ba1SWarner Losh 		ttydisc_rint(tp, c, 0);
1456f079ba1SWarner Losh 	ttydisc_rint_done(tp);
146ae36cccdSWarner Losh 
147f1f7034dSJohn Baldwin 	callout_reset(&cfe_timer, polltime, cfe_timeout, tp);
148ae36cccdSWarner Losh }
149ae36cccdSWarner Losh 
150ae36cccdSWarner Losh static void
cfe_cnprobe(struct consdev * cp)151ae36cccdSWarner Losh cfe_cnprobe(struct consdev *cp)
152ae36cccdSWarner Losh {
153ae36cccdSWarner Losh 
154ae36cccdSWarner Losh 	conhandle = cfe_getstdhandle(CFE_STDHANDLE_CONSOLE);
155ae36cccdSWarner Losh 	if (conhandle < 0) {
156ae36cccdSWarner Losh 		cp->cn_pri = CN_DEAD;
157ae36cccdSWarner Losh 		return;
158ae36cccdSWarner Losh 	}
159ae36cccdSWarner Losh 
160ae36cccdSWarner Losh 	/* XXX */
161ae36cccdSWarner Losh 	if (bootverbose) {
162ae36cccdSWarner Losh 		char *bootmsg = "Using CFE firmware console.\n";
163ae36cccdSWarner Losh 		int i;
164ae36cccdSWarner Losh 
165ae36cccdSWarner Losh 		for (i = 0; i < strlen(bootmsg); i++)
166ae36cccdSWarner Losh 			cfe_cnputc(cp, bootmsg[i]);
167ae36cccdSWarner Losh 	}
168ae36cccdSWarner Losh 
169ae36cccdSWarner Losh 	cp->cn_pri = CN_LOW;
170ae36cccdSWarner Losh }
171ae36cccdSWarner Losh 
172ae36cccdSWarner Losh static void
cfe_cninit(struct consdev * cp)173ae36cccdSWarner Losh cfe_cninit(struct consdev *cp)
174ae36cccdSWarner Losh {
175ae36cccdSWarner Losh 
176c8978106SEd Schouten 	strcpy(cp->cn_name, "cfecons");
177ae36cccdSWarner Losh }
178ae36cccdSWarner Losh 
179ae36cccdSWarner Losh static void
cfe_cnterm(struct consdev * cp)180ae36cccdSWarner Losh cfe_cnterm(struct consdev *cp)
181ae36cccdSWarner Losh {
182ae36cccdSWarner Losh 
183ae36cccdSWarner Losh }
184ae36cccdSWarner Losh 
1859976156fSAndriy Gapon static void
cfe_cngrab(struct consdev * cp)1869976156fSAndriy Gapon cfe_cngrab(struct consdev *cp)
1879976156fSAndriy Gapon {
1889976156fSAndriy Gapon 
1899976156fSAndriy Gapon }
1909976156fSAndriy Gapon 
1919976156fSAndriy Gapon static void
cfe_cnungrab(struct consdev * cp)1929976156fSAndriy Gapon cfe_cnungrab(struct consdev *cp)
1939976156fSAndriy Gapon {
1949976156fSAndriy Gapon 
1959976156fSAndriy Gapon }
1969976156fSAndriy Gapon 
197ae36cccdSWarner Losh static int
cfe_cngetc(struct consdev * cp)198ae36cccdSWarner Losh cfe_cngetc(struct consdev *cp)
199ae36cccdSWarner Losh {
200ae36cccdSWarner Losh 	unsigned char ch;
201ae36cccdSWarner Losh 
20261f7c762SNeel Natu 	if (cfe_read(conhandle, &ch, 1) == 1) {
2034cf75455SRobert Watson #if defined(KDB)
2044cf75455SRobert Watson 		kdb_alt_break(ch, &alt_break_state);
205ae36cccdSWarner Losh #endif
206ae36cccdSWarner Losh 		return (ch);
207ae36cccdSWarner Losh 	}
208ae36cccdSWarner Losh 
209ae36cccdSWarner Losh 	return (-1);
210ae36cccdSWarner Losh }
211ae36cccdSWarner Losh 
212ae36cccdSWarner Losh static void
cfe_cnputc(struct consdev * cp,int c)213ae36cccdSWarner Losh cfe_cnputc(struct consdev *cp, int c)
214ae36cccdSWarner Losh {
215ae36cccdSWarner Losh 	char cbuf;
216ae36cccdSWarner Losh 
217ae36cccdSWarner Losh 	if (c == '\n')
218ae36cccdSWarner Losh 		cfe_cnputc(cp, '\r');
219ae36cccdSWarner Losh 
220ae36cccdSWarner Losh 	cbuf = c;
221ae36cccdSWarner Losh 	while (cfe_write(conhandle, &cbuf, 1) == 0)
222750595efSWarner Losh 		continue;
223ae36cccdSWarner Losh }
224ae36cccdSWarner Losh 
2256f079ba1SWarner Losh SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
226