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