1098ca2bdSWarner Losh /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3718cf2ccSPedro F. Giffuni *
4707fed20SBenno Rice * Copyright (C) 2001 Benno Rice.
5707fed20SBenno Rice * All rights reserved.
6707fed20SBenno Rice *
7707fed20SBenno Rice * Redistribution and use in source and binary forms, with or without
8707fed20SBenno Rice * modification, are permitted provided that the following conditions
9707fed20SBenno Rice * are met:
10707fed20SBenno Rice * 1. Redistributions of source code must retain the above copyright
11707fed20SBenno Rice * notice, this list of conditions and the following disclaimer.
12707fed20SBenno Rice * 2. Redistributions in binary form must reproduce the above copyright
13707fed20SBenno Rice * notice, this list of conditions and the following disclaimer in the
14707fed20SBenno Rice * documentation and/or other materials provided with the distribution.
15707fed20SBenno Rice *
16707fed20SBenno Rice * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
17707fed20SBenno Rice * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18707fed20SBenno Rice * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19707fed20SBenno Rice * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20707fed20SBenno Rice * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21707fed20SBenno Rice * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22707fed20SBenno Rice * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23707fed20SBenno Rice * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24707fed20SBenno Rice * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25707fed20SBenno Rice * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26707fed20SBenno Rice */
27707fed20SBenno Rice
288368cf8fSDavid E. O'Brien #include <sys/cdefs.h>
29a82b25f9SDavid E. O'Brien #include "opt_ofw.h"
30761f89f9SHartmut Brandt
31707fed20SBenno Rice #include <sys/param.h>
328cabb94fSMarcel Moolenaar #include <sys/kdb.h>
33707fed20SBenno Rice #include <sys/kernel.h>
3443593547SRobert Watson #include <sys/priv.h>
35707fed20SBenno Rice #include <sys/systm.h>
36707fed20SBenno Rice #include <sys/types.h>
37707fed20SBenno Rice #include <sys/conf.h>
38707fed20SBenno Rice #include <sys/cons.h>
39707fed20SBenno Rice #include <sys/consio.h>
40707fed20SBenno Rice #include <sys/tty.h>
41707fed20SBenno Rice
42707fed20SBenno Rice #include <dev/ofw/openfirm.h>
43707fed20SBenno Rice
44761f89f9SHartmut Brandt #include <ddb/ddb.h>
45761f89f9SHartmut Brandt
46a82b25f9SDavid E. O'Brien #ifndef OFWCONS_POLL_HZ
47a82b25f9SDavid E. O'Brien #define OFWCONS_POLL_HZ 4 /* 50-100 works best on Ultra2 */
48a82b25f9SDavid E. O'Brien #endif
49a82b25f9SDavid E. O'Brien #define OFBURSTLEN 128 /* max number of bytes to write in one chunk */
50707fed20SBenno Rice
51bc093719SEd Schouten static tsw_open_t ofwtty_open;
52bc093719SEd Schouten static tsw_close_t ofwtty_close;
53bc093719SEd Schouten static tsw_outwakeup_t ofwtty_outwakeup;
54707fed20SBenno Rice
55bc093719SEd Schouten static struct ttydevsw ofw_ttydevsw = {
56bc093719SEd Schouten .tsw_flags = TF_NOPREFIX,
57bc093719SEd Schouten .tsw_open = ofwtty_open,
58bc093719SEd Schouten .tsw_close = ofwtty_close,
59bc093719SEd Schouten .tsw_outwakeup = ofwtty_outwakeup,
60707fed20SBenno Rice };
61707fed20SBenno Rice
62707fed20SBenno Rice static int polltime;
63a272a813SJohn Baldwin static struct callout ofw_timer;
64707fed20SBenno Rice
654cf75455SRobert Watson #if defined(KDB)
66761f89f9SHartmut Brandt static int alt_break_state;
67761f89f9SHartmut Brandt #endif
68761f89f9SHartmut Brandt
69707fed20SBenno Rice static void ofw_timeout(void *);
70707fed20SBenno Rice
7105c3592eSPoul-Henning Kamp static cn_probe_t ofw_cnprobe;
7205c3592eSPoul-Henning Kamp static cn_init_t ofw_cninit;
7305c3592eSPoul-Henning Kamp static cn_term_t ofw_cnterm;
7405c3592eSPoul-Henning Kamp static cn_getc_t ofw_cngetc;
7505c3592eSPoul-Henning Kamp static cn_putc_t ofw_cnputc;
769976156fSAndriy Gapon static cn_grab_t ofw_cngrab;
779976156fSAndriy Gapon static cn_ungrab_t ofw_cnungrab;
78707fed20SBenno Rice
7977dfeeadSPoul-Henning Kamp CONSOLE_DRIVER(ofw);
80707fed20SBenno Rice
8147a1c915SJake Burkholder static void
cn_drvinit(void * unused)8247a1c915SJake Burkholder cn_drvinit(void *unused)
8347a1c915SJake Burkholder {
84a121cb6aSJake Burkholder phandle_t options;
85a121cb6aSJake Burkholder char output[32];
86bc093719SEd Schouten struct tty *tp;
8747a1c915SJake Burkholder
88278667afSJake Burkholder if (ofw_consdev.cn_pri != CN_DEAD &&
89278667afSJake Burkholder ofw_consdev.cn_name[0] != '\0') {
9022b256dfSNathan Whitehorn tp = tty_alloc(&ofw_ttydevsw, NULL);
9122b256dfSNathan Whitehorn tty_makedev(tp, NULL, "%s", "ofwcons");
9222b256dfSNathan Whitehorn
93c5c5a2adSPoul-Henning Kamp /*
94c5c5a2adSPoul-Henning Kamp * XXX: This is a hack and it may result in two /dev/ttya
95c5c5a2adSPoul-Henning Kamp * XXX: devices on platforms where the sab driver works.
96c5c5a2adSPoul-Henning Kamp */
9722b256dfSNathan Whitehorn if ((options = OF_finddevice("/options")) == -1 ||
9822b256dfSNathan Whitehorn OF_getprop(options, "output-device", output,
9922b256dfSNathan Whitehorn sizeof(output)) == -1)
10022b256dfSNathan Whitehorn return;
10122b256dfSNathan Whitehorn if (strlen(output) > 0)
102e14376e4SNathan Whitehorn tty_makealias(tp, "%s", output);
103a272a813SJohn Baldwin callout_init_mtx(&ofw_timer, tty_getlock(tp), 0);
104a121cb6aSJake Burkholder }
10547a1c915SJake Burkholder }
10647a1c915SJake Burkholder
107237fdd78SRobert Watson SYSINIT(cndev, SI_SUB_CONFIGURE, SI_ORDER_MIDDLE, cn_drvinit, NULL);
10847a1c915SJake Burkholder
109d3a0a0f3SNathan Whitehorn static pcell_t stdin;
110d3a0a0f3SNathan Whitehorn static pcell_t stdout;
111707fed20SBenno Rice
112707fed20SBenno Rice static int
ofwtty_open(struct tty * tp)113bc093719SEd Schouten ofwtty_open(struct tty *tp)
114707fed20SBenno Rice {
115a82b25f9SDavid E. O'Brien polltime = hz / OFWCONS_POLL_HZ;
116bc093719SEd Schouten if (polltime < 1)
117707fed20SBenno Rice polltime = 1;
118707fed20SBenno Rice
119a272a813SJohn Baldwin callout_reset(&ofw_timer, polltime, ofw_timeout, tp);
120bc093719SEd Schouten
121bc093719SEd Schouten return (0);
122707fed20SBenno Rice }
123707fed20SBenno Rice
124bc093719SEd Schouten static void
ofwtty_close(struct tty * tp)125bc093719SEd Schouten ofwtty_close(struct tty *tp)
126707fed20SBenno Rice {
127707fed20SBenno Rice
128a272a813SJohn Baldwin callout_stop(&ofw_timer);
129707fed20SBenno Rice }
130707fed20SBenno Rice
131707fed20SBenno Rice static void
ofwtty_outwakeup(struct tty * tp)132bc093719SEd Schouten ofwtty_outwakeup(struct tty *tp)
133707fed20SBenno Rice {
134a82b25f9SDavid E. O'Brien int len;
135a82b25f9SDavid E. O'Brien u_char buf[OFBURSTLEN];
136707fed20SBenno Rice
137bc093719SEd Schouten for (;;) {
138bc093719SEd Schouten len = ttydisc_getc(tp, buf, sizeof buf);
139bc093719SEd Schouten if (len == 0)
140bc093719SEd Schouten break;
141a82b25f9SDavid E. O'Brien OF_write(stdout, buf, len);
142707fed20SBenno Rice }
143707fed20SBenno Rice }
144707fed20SBenno Rice
145707fed20SBenno Rice static void
ofw_timeout(void * v)146707fed20SBenno Rice ofw_timeout(void *v)
147707fed20SBenno Rice {
148707fed20SBenno Rice struct tty *tp;
149707fed20SBenno Rice int c;
150707fed20SBenno Rice
151707fed20SBenno Rice tp = (struct tty *)v;
152707fed20SBenno Rice
15323d53268SKyle Evans tty_assert_locked(tp);
154bc093719SEd Schouten while ((c = ofw_cngetc(NULL)) != -1)
155bc093719SEd Schouten ttydisc_rint(tp, c, 0);
156bc093719SEd Schouten ttydisc_rint_done(tp);
157707fed20SBenno Rice
158a272a813SJohn Baldwin callout_schedule(&ofw_timer, polltime);
159707fed20SBenno Rice }
160707fed20SBenno Rice
161707fed20SBenno Rice static void
ofw_cnprobe(struct consdev * cp)16277dfeeadSPoul-Henning Kamp ofw_cnprobe(struct consdev *cp)
163707fed20SBenno Rice {
164707fed20SBenno Rice int chosen;
165707fed20SBenno Rice
166707fed20SBenno Rice if ((chosen = OF_finddevice("/chosen")) == -1) {
167707fed20SBenno Rice cp->cn_pri = CN_DEAD;
168707fed20SBenno Rice return;
169707fed20SBenno Rice }
170707fed20SBenno Rice
171d3a0a0f3SNathan Whitehorn if (OF_getencprop(chosen, "stdin", &stdin, sizeof(stdin)) == -1) {
172707fed20SBenno Rice cp->cn_pri = CN_DEAD;
173707fed20SBenno Rice return;
174707fed20SBenno Rice }
175707fed20SBenno Rice
176d3a0a0f3SNathan Whitehorn if (OF_getencprop(chosen, "stdout", &stdout, sizeof(stdout)) == -1) {
177707fed20SBenno Rice cp->cn_pri = CN_DEAD;
178707fed20SBenno Rice return;
179707fed20SBenno Rice }
180707fed20SBenno Rice
1815dbb0622SJake Burkholder cp->cn_pri = CN_LOW;
182707fed20SBenno Rice }
183707fed20SBenno Rice
184707fed20SBenno Rice static void
ofw_cninit(struct consdev * cp)18505c3592eSPoul-Henning Kamp ofw_cninit(struct consdev *cp)
186707fed20SBenno Rice {
187707fed20SBenno Rice
1882f06c3e7SPoul-Henning Kamp /* XXX: This is the alias, but that should be good enough */
189c8978106SEd Schouten strcpy(cp->cn_name, "ofwcons");
190707fed20SBenno Rice }
191707fed20SBenno Rice
19205c3592eSPoul-Henning Kamp static void
ofw_cnterm(struct consdev * cp)19377dfeeadSPoul-Henning Kamp ofw_cnterm(struct consdev *cp)
194707fed20SBenno Rice {
195707fed20SBenno Rice }
196707fed20SBenno Rice
1979976156fSAndriy Gapon static void
ofw_cngrab(struct consdev * cp)1989976156fSAndriy Gapon ofw_cngrab(struct consdev *cp)
1999976156fSAndriy Gapon {
2009976156fSAndriy Gapon }
2019976156fSAndriy Gapon
2029976156fSAndriy Gapon static void
ofw_cnungrab(struct consdev * cp)2039976156fSAndriy Gapon ofw_cnungrab(struct consdev *cp)
2049976156fSAndriy Gapon {
2059976156fSAndriy Gapon }
2069976156fSAndriy Gapon
207707fed20SBenno Rice static int
ofw_cngetc(struct consdev * cp)20805c3592eSPoul-Henning Kamp ofw_cngetc(struct consdev *cp)
209707fed20SBenno Rice {
210707fed20SBenno Rice unsigned char ch;
211707fed20SBenno Rice
212cbecdd57SJake Burkholder if (OF_read(stdin, &ch, 1) > 0) {
2134cf75455SRobert Watson #if defined(KDB)
2144cf75455SRobert Watson kdb_alt_break(ch, &alt_break_state);
215761f89f9SHartmut Brandt #endif
216707fed20SBenno Rice return (ch);
217707fed20SBenno Rice }
218707fed20SBenno Rice
219707fed20SBenno Rice return (-1);
220707fed20SBenno Rice }
221707fed20SBenno Rice
222707fed20SBenno Rice static void
ofw_cnputc(struct consdev * cp,int c)22305c3592eSPoul-Henning Kamp ofw_cnputc(struct consdev *cp, int c)
224707fed20SBenno Rice {
225707fed20SBenno Rice char cbuf;
226707fed20SBenno Rice
227707fed20SBenno Rice if (c == '\n') {
228707fed20SBenno Rice cbuf = '\r';
229707fed20SBenno Rice OF_write(stdout, &cbuf, 1);
230707fed20SBenno Rice }
231707fed20SBenno Rice
232707fed20SBenno Rice cbuf = c;
233707fed20SBenno Rice OF_write(stdout, &cbuf, 1);
234707fed20SBenno Rice }
235