ofw_console.c revision 45720
1/* $Id: prom.c,v 1.1.1.1 1998/08/21 03:17:42 msmith Exp $ */
2/* $NetBSD: prom.c,v 1.3 1997/09/06 14:03:58 drochner Exp $ */
3
4/*
5 * Mach Operating System
6 * Copyright (c) 1992 Carnegie Mellon University
7 * All Rights Reserved.
8 *
9 * Permission to use, copy, modify and distribute this software and its
10 * documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
17 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
22 *  School of Computer Science
23 *  Carnegie Mellon University
24 *  Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie Mellon
27 * the rights to redistribute these changes.
28 */
29
30#include <sys/types.h>
31
32#include <machine/prom.h>
33#include <machine/rpb.h>
34
35#include "common.h"
36#include "bootstrap.h"
37
38int console;
39
40static void prom_probe(struct console *cp);
41static int prom_init(int);
42void prom_putchar(int);
43int prom_getchar(void);
44int prom_poll(void);
45
46struct console promconsole = {
47    "prom",
48    "SRM firmware console",
49    0,
50    prom_probe,
51    prom_init,
52    prom_putchar,
53    prom_getchar,
54    prom_poll,
55};
56
57void
58init_prom_calls()
59{
60    extern struct prom_vec prom_dispatch_v;
61    struct rpb *r;
62    struct crb *c;
63    char buf[4];
64
65    r = (struct rpb *)HWRPB_ADDR;
66    c = (struct crb *)((u_int8_t *)r + r->rpb_crb_off);
67
68    prom_dispatch_v.routine_arg = c->crb_v_dispatch;
69    prom_dispatch_v.routine = c->crb_v_dispatch->entry_va;
70
71    /* Look for console tty. */
72    prom_getenv(PROM_E_TTY_DEV, buf, 4);
73    console = buf[0] - '0';
74}
75
76static void
77prom_probe(struct console *cp)
78{
79    init_prom_calls();
80    cp->c_flags |= C_PRESENTIN|C_PRESENTOUT;
81}
82
83static int
84prom_init(int arg)
85{
86    return 0;
87}
88
89void
90prom_putchar(int c)
91{
92    prom_return_t ret;
93    char cbuf;
94
95    cbuf = c;
96    do {
97	ret.bits = prom_dispatch(PROM_R_PUTS, console, &cbuf, 1);
98    } while ((ret.u.retval & 1) == 0);
99}
100
101static int saved_char = -1;
102
103int
104prom_getchar()
105{
106    prom_return_t ret;
107
108    if (saved_char != -1) {
109	int c = saved_char;
110	saved_char = -1;
111	return c;
112    }
113
114    for (;;) {
115	ret.bits = prom_dispatch(PROM_R_GETC, console);
116	if (ret.u.status == 0 || ret.u.status == 1)
117	    return (ret.u.retval);
118    }
119}
120
121int
122prom_poll()
123{
124    prom_return_t ret;
125
126    if (saved_char != -1)
127	return 1;
128
129    ret.bits = prom_dispatch(PROM_R_GETC, console);
130    if (ret.u.status == 0 || ret.u.status == 1) {
131	saved_char = ret.u.retval;
132	return 1;
133    }
134
135    return 0;
136}
137
138int
139prom_getenv(id, buf, len)
140    int id, len;
141    char *buf;
142{
143    prom_return_t ret;
144
145    ret.bits = prom_dispatch(PROM_R_GETENV, id, buf, len-1);
146    if (ret.u.status & 0x4)
147	ret.u.retval = 0;
148    buf[ret.u.retval] = '\0';
149
150    return (ret.u.retval);
151}
152
153int
154prom_open(dev, len)
155    char *dev;
156    int len;
157{
158    prom_return_t ret;
159
160    ret.bits = prom_dispatch(PROM_R_OPEN, dev, len);
161    if (ret.u.status & 0x4)
162	return (-1);
163    else
164	return (ret.u.retval);
165}
166