xref: /netbsd/sys/arch/alpha/stand/common/prom.c (revision bf9ec67e)
1 /* $NetBSD: prom.c,v 1.10 1999/04/02 03:11:57 cgd Exp $ */
2 
3 /*
4  * Mach Operating System
5  * Copyright (c) 1992 Carnegie Mellon University
6  * All Rights Reserved.
7  *
8  * Permission to use, copy, modify and distribute this software and its
9  * documentation is hereby granted, provided that both the copyright
10  * notice and this permission notice appear in all copies of the
11  * software, derivative works or modified versions, and any portions
12  * thereof, and that both notices appear in supporting documentation.
13  *
14  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
15  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
16  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
17  *
18  * Carnegie Mellon requests users of this software to return to
19  *
20  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
21  *  School of Computer Science
22  *  Carnegie Mellon University
23  *  Pittsburgh PA 15213-3890
24  *
25  * any improvements or extensions that they make and grant Carnegie Mellon
26  * the rights to redistribute these changes.
27  */
28 
29 #include <sys/types.h>
30 
31 #include <machine/prom.h>
32 #include <machine/rpb.h>
33 
34 #include "common.h"
35 
36 int console;
37 
38 #if !defined(NO_GETCHAR) || !defined(NO_PUTCHAR_HALT)
39 static int test_getchar(int *);
40 #endif
41 static void putonechar(int c);
42 
43 void
44 init_prom_calls()
45 {
46 	extern struct prom_vec prom_dispatch_v;
47 	struct rpb *r;
48 	struct crb *c;
49 	char buf[4];
50 
51 	r = (struct rpb *)HWRPB_ADDR;
52 	c = (struct crb *)((u_int8_t *)r + r->rpb_crb_off);
53 
54 	prom_dispatch_v.routine_arg = c->crb_v_dispatch;
55 	prom_dispatch_v.routine = c->crb_v_dispatch->entry_va;
56 
57 	/* Look for console tty. */
58 	prom_getenv(PROM_E_TTY_DEV, buf, 4);
59 	console = buf[0] - '0';
60 }
61 
62 #if !defined(NO_GETCHAR) || !defined(NO_PUTCHAR_HALT)
63 static int
64 test_getchar(xc)
65 	int *xc;
66 {
67 	prom_return_t ret;
68 
69 	ret.bits = prom_dispatch(PROM_R_GETC, console);
70 	*xc = ret.u.retval;
71 	return ret.u.status == 0 || ret.u.status == 1;
72 }
73 #endif
74 
75 #if !defined(NO_GETCHAR)
76 int
77 getchar()
78 {
79 	int c;
80 
81 	for (;;) {
82 		if (test_getchar(&c)) {
83 			if (c == 3)
84 				halt();
85 			return c;
86 		}
87 	}
88 }
89 #endif
90 
91 static void
92 putonechar(c)
93 	int c;
94 {
95 	prom_return_t ret;
96 	char cbuf = c;
97 
98 	do {
99 		ret.bits = prom_dispatch(PROM_R_PUTS, console, &cbuf, 1);
100 	} while ((ret.u.retval & 1) == 0);
101 }
102 
103 void
104 putchar(c)
105 	int c;
106 {
107 #if !defined(NO_PUTCHAR_HALT)
108 	int typed_c;
109 #endif
110 
111 	if (c == '\r' || c == '\n') {
112 		putonechar('\r');
113 		c = '\n';
114 	}
115 	putonechar(c);
116 #if !defined(NO_PUTCHAR_HALT)
117 	if (test_getchar(&typed_c))
118 		if (typed_c == 3)
119 			halt();
120 #endif
121 }
122 
123 int
124 prom_getenv(id, buf, len)
125 	int id, len;
126 	char *buf;
127 {
128 	prom_return_t ret;
129 
130 	ret.bits = prom_dispatch(PROM_R_GETENV, id, buf, len-1);
131 	if (ret.u.status & 0x4)
132 		ret.u.retval = 0;
133 	buf[ret.u.retval] = '\0';
134 
135 	return (ret.u.retval);
136 }
137