1 /*	$NetBSD: promlib.h,v 1.24 2010/01/21 11:40:01 martin Exp $ */
2 
3 /*-
4  * Copyright (c) 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Paul Kranenburg.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * OPENPROM functions.  These are here mainly to hide the OPENPROM interface
34  * from the rest of the kernel.
35  */
36 
37 #ifndef _SPARC_PROMLIB_H_
38 #define _SPARC_PROMLIB_H_
39 
40 #ifdef _KERNEL_OPT
41 #include "opt_multiprocessor.h"
42 #endif
43 
44 #include <machine/idprom.h>
45 #include <machine/bsd_openprom.h>
46 #include <machine/openfirm.h>
47 
48 /*
49  * A set of methods to access the firmware.
50  * We use this to support various versions of Open Boot Prom
51  * or Open Firmware implementations.
52  */
53 struct promops {
54 	int	po_version;		/* PROM version number */
55 #define PROM_OLDMON	0
56 #define PROM_OBP_V0	1
57 #define PROM_OBP_V2	2
58 #define PROM_OBP_V3	3
59 #define PROM_OPENFIRM	4
60 	int	po_revision;		/* revision level */
61 	int	po_stdin;		/* stdio handles */
62 	int	po_stdout;		/* */
63 	void	*po_bootcookie;
64 
65 	/* Access to boot arguments */
66 	const char *(*po_bootpath)(void);
67 	const char *(*po_bootfile)(void);
68 	const char *(*po_bootargs)(void);
69 
70 	/* I/O functions */
71 	int	(*po_getchar)(void);
72 	int	(*po_peekchar)(void);
73 	void	(*po_putchar)(int);
74 	void	(*po_putstr)(const char *, int);
75 	int	(*po_open)(const char *);
76 	void	(*po_close)(int);
77 	int	(*po_read)(int, void *, int);
78 	int	(*po_write)(int, const void *, int);
79 	int	(*po_seek)(int, u_quad_t);
80 
81 	int	(*po_instance_to_package)(int);
82 
83 	/* Misc functions (common in OBP 0,2,3) */
84 	void	(*po_halt)(void)	__attribute__((__noreturn__));
85 	void	(*po_reboot)(const char *)	__attribute__((__noreturn__));
86 	void	(*po_abort)(void);
87 	void	(*po_interpret)(const char *);
88 	void	(*po_setcallback)(void (*)(void));
89 	int	(*po_ticks)(void);
90 	void	*po_tickdata;
91 
92 	/* sun4/sun4c only */
93 	void	(*po_setcontext)(int ctxt, void *va, int pmeg);
94 
95 	/* MP functions (OBP v3 only) */
96 	int	(*po_cpustart)(int, struct openprom_addr *, int, void *);
97 	int	(*po_cpustop)(int);
98 	int	(*po_cpuidle)(int);
99 	int	(*po_cpuresume)(int);
100 
101 	/* Device node traversal (OBP v0, v2, v3; but not sun4) */
102 	int	(*po_firstchild)(int);
103 	int	(*po_nextsibling)(int);
104 
105 	/* Device node properties */
106 	int	(*po_getproplen)(int, const char *);
107 	int	(*po_getprop)(int, const char *, void *, int);
108 	int	(*po_setprop)(int, const char *, const void *, int);
109 	char	*(*po_nextprop)(int, const char *);
110 
111 	int	(*po_finddevice)(const char *);
112 
113 };
114 
115 extern struct promops	promops;
116 
117 /*
118  * Memory description array.
119  * Same as version 2 rom meminfo property.
120  */
121 struct memarr {
122 	long	zero;
123 	u_long	addr;
124 	u_long	len;
125 };
126 int	prom_makememarr(struct memarr *, int, int);
127 #define	MEMARR_AVAILPHYS	0
128 #define	MEMARR_TOTALPHYS	1
129 
130 struct idprom	*prom_getidprom(void);
131 void		prom_getether(int, u_char *);
132 bool		prom_get_node_ether(int, u_char*);
133 const char	*prom_pa_location(u_int, u_int);
134 
135 void	prom_init(void);	/* To setup promops */
136 
137 /* Utility routines */
138 int	prom_getprop(int, const char *, size_t, int *, void *);
139 int	prom_getpropint(int, const char *, int);
140 char	*prom_getpropstring(int, const char *);
141 char	*prom_getpropstringA(int, const char *, char *, size_t);
142 void	prom_printf(const char *, ...);
143 
144 int	prom_findroot(void);
145 int	prom_findnode(int, const char *);
146 int	prom_search(int, const char *);
147 int	prom_opennode(const char *);
148 int	prom_node_has_property(int, const char *);
149 int	prom_getoptionsnode(void);
150 int	prom_getoption(const char *, char *, int);
151 
152 #define	findroot()		prom_findroot()
153 #define	findnode(node,name)	prom_findnode(node,name)
154 #define	opennode(name)		prom_opennode(name)
155 #define	node_has_property(node,prop)	prom_node_has_property(node,prop)
156 
157 void	prom_halt(void)		__attribute__((__noreturn__));
158 void	prom_boot(char *)	__attribute__((__noreturn__));
159 
160 #if defined(MULTIPROCESSOR)
161 #define callrom() do {		\
162 	mp_pause_cpus();	\
163 	prom_abort();		\
164 	mp_resume_cpus();	\
165 } while (0)
166 #else
167 #define callrom()		prom_abort()
168 #endif
169 
170 #define prom_version()		(promops.po_version)
171 #define prom_revision()		(promops.po_revision)
172 #define prom_stdin()		(promops.po_stdin)
173 #define prom_stdout()		(promops.po_stdout)
174 #define _prom_halt()		((*promops.po_halt)(/*void*/))
175 #define _prom_boot(a)		((*promops.po_reboot)(a))
176 #define prom_abort()		((*promops.po_abort)(/*void*/))
177 #define prom_interpret(a)	((*promops.po_interpret)(a))
178 #define prom_setcallback(f)	((*promops.po_setcallback)(f))
179 #define prom_setcontext(c,a,p)	((*promops.po_setcontext)(c,a,p))
180 #define prom_getbootpath()	((*promops.po_bootpath)(/*void*/))
181 #define prom_getbootfile()	((*promops.po_bootfile)(/*void*/))
182 #define prom_getbootargs()	((*promops.po_bootargs)(/*void*/))
183 #define prom_ticks()		((*promops.po_ticks)(/*void*/))
184 
185 
186 #define prom_open(name)		((*promops.po_open)(name))
187 #define prom_close(fd)		((*promops.po_close)(fd))
188 #define prom_instance_to_package(fd) \
189 				((*promops.po_instance_to_package)(fd))
190 #define prom_read(fd,b,n)	((*promops.po_read)(fd,b,n))
191 #define prom_write(fd,b,n)	((*promops.po_write)(fd,b,n))
192 #define prom_seek(fd,o)		((*promops.po_seek)(fd,o))
193 #define prom_getchar()		((*promops.po_getchar)(/*void*/))
194 #define prom_peekchar()		((*promops.po_peekchar)(/*void*/))
195 #define prom_putchar(c)		((*promops.po_putchar)(c))
196 #define prom_putstr(b,n)	((*promops.po_putstr)(b,n))
197 
198 /* Node traversal */
199 #define prom_firstchild(node)	((*promops.po_firstchild)(node))
200 #define prom_nextsibling(node)	((*promops.po_nextsibling)(node))
201 #define prom_proplen(node,name)	((*promops.po_getproplen)(node, name))
202 #define _prom_getprop(node, name, buf, len) \
203 				((*promops.po_getprop)(node, name, buf, len))
204 #define prom_setprop(node, name, value, len) \
205 				((*promops.po_setprop)(node, name, value, len))
206 #define prom_nextprop(node,name)	((*promops.po_nextprop)(node, name))
207 #define prom_finddevice(name)	((*promops.po_finddevice)(name))
208 
209 #define firstchild(node)	prom_firstchild(node)
210 #define nextsibling(node)	prom_nextsibling(node)
211 #define prom_getproplen(node,name)	prom_proplen(node, name)
212 
213 
214 /* MP stuff - not currently used */
215 #define prom_cpustart(m,a,c,pc)	((*promops.po_cpustart)(m,a,c,pc))
216 #define prom_cpustop(m)		((*promops.po_cpustop)(m))
217 #define prom_cpuidle(m)		((*promops.po_cpuidle)(m))
218 #define prom_cpuresume(m)	((*promops.po_cpuresume)(m))
219 
220 extern void	*romp;		/* PROM-supplied argument (see locore) */
221 
222 #endif /* _SPARC_PROMLIB_H_ */
223