ofw_subr.S revision 1.7 1 1.7 garbled /* $NetBSD: ofw_subr.S,v 1.7 2008/02/14 19:41:54 garbled Exp $ */
2 1.1 matt
3 1.1 matt /*
4 1.1 matt * Copyright (C) 1995, 1996 Wolfgang Solfrank.
5 1.1 matt * Copyright (C) 1995, 1996 TooLs GmbH.
6 1.1 matt * All rights reserved.
7 1.1 matt *
8 1.1 matt * Redistribution and use in source and binary forms, with or without
9 1.1 matt * modification, are permitted provided that the following conditions
10 1.1 matt * are met:
11 1.1 matt * 1. Redistributions of source code must retain the above copyright
12 1.1 matt * notice, this list of conditions and the following disclaimer.
13 1.1 matt * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 matt * notice, this list of conditions and the following disclaimer in the
15 1.1 matt * documentation and/or other materials provided with the distribution.
16 1.1 matt * 3. All advertising materials mentioning features or use of this software
17 1.1 matt * must display the following acknowledgement:
18 1.1 matt * This product includes software developed by TooLs GmbH.
19 1.1 matt * 4. The name of TooLs GmbH may not be used to endorse or promote products
20 1.1 matt * derived from this software without specific prior written permission.
21 1.1 matt *
22 1.1 matt * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
23 1.1 matt * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.1 matt * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.1 matt * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 1.1 matt * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 1.1 matt * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 1.1 matt * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 1.1 matt * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 1.1 matt * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 1.1 matt * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.1 matt */
33 1.1 matt
34 1.1 matt
35 1.1 matt .local firmstk
36 1.3 sanjayl .globl openfirmware_entry
37 1.1 matt .local ofwsrsave
38 1.1 matt .local OF_buffer
39 1.1 matt
40 1.1 matt .data
41 1.1 matt GLOBAL(ofmsr)
42 1.1 matt .long 0,0,0,0,0 /* msr & sprg[0-3] used in OF */
43 1.1 matt
44 1.1 matt GLOBAL(ofwsprg0save)
45 1.1 matt .long 0
46 1.1 matt
47 1.4 aymeric GLOBAL(ofwreal_incharge)
48 1.4 aymeric .long 0
49 1.4 aymeric
50 1.1 matt .comm firmstk,NBPG,8
51 1.1 matt .comm OF_buffer,NBPG,4
52 1.1 matt .comm openfirmware_entry,4,4 /* openfirmware entry point */
53 1.1 matt .comm ofwsrsave,64,4 /* openfirmware SR savearea */
54 1.1 matt
55 1.1 matt /*
56 1.1 matt * Called by start to save the initial OFW state so we can restore it
57 1.1 matt * when call back to OFW.
58 1.1 matt */
59 1.1 matt ENTRY_NOPROFILE(ofwinit)
60 1.1 matt #ifdef FIRMWORKSBUGS
61 1.7 garbled mfmsr %r0
62 1.7 garbled andi. %r0,%r0,PSL_IR|PSL_DR
63 1.1 matt beq 1f
64 1.1 matt
65 1.7 garbled li %r8,1
66 1.7 garbled lis %r9,ofwreal_incharge@ha
67 1.7 garbled stw %r8,ofwreal_incharge@l(9)
68 1.4 aymeric
69 1.7 garbled mflr %r30
70 1.1 matt bl _C_LABEL(ofwr_init)
71 1.7 garbled mtlr %r30
72 1.1 matt 1:
73 1.1 matt #endif
74 1.7 garbled lis %r8,openfirmware_entry@ha
75 1.7 garbled stw %r5,openfirmware_entry@l(%r8) /* save client interface handler*/
76 1.1 matt
77 1.7 garbled mfmsr %r0
78 1.7 garbled lis %r9,ofmsr@ha
79 1.7 garbled stwu %r0,ofmsr@l(%r9) /* save initial MSR value */
80 1.7 garbled
81 1.7 garbled mfsprg %r0,0 /* save SPRGs */
82 1.7 garbled stwu %r0,4(%r9)
83 1.7 garbled mfsprg %r0,1
84 1.7 garbled stwu %r0,4(%r9)
85 1.7 garbled mfsprg %r0,2
86 1.7 garbled stwu %r0,4(%r9)
87 1.7 garbled mfsprg %r0,3
88 1.7 garbled stw %r0,4(%r9)
89 1.7 garbled
90 1.7 garbled lis %r8,OF_buffer@ha
91 1.7 garbled addi %r8,%r8,OF_buffer@l
92 1.7 garbled lis %r9,_C_LABEL(OF_buf)@ha
93 1.7 garbled stw %r8,_C_LABEL(OF_buf)@l(%r9)
94 1.1 matt
95 1.1 matt blr
96 1.1 matt
97 1.1 matt /*
98 1.1 matt * OpenFirmware entry point
99 1.1 matt */
100 1.1 matt .text
101 1.1 matt ENTRY(openfirmware)
102 1.7 garbled mflr %r0 /* save return address */
103 1.7 garbled stw %r0,4(%r1)
104 1.7 garbled stwu %r1,-16(%r1) /* setup stack frame */
105 1.7 garbled
106 1.7 garbled lis %r4,openfirmware_entry@ha /* get firmware entry point */
107 1.7 garbled lwz %r4,openfirmware_entry@l(%r4)
108 1.7 garbled mtlr %r4
109 1.7 garbled
110 1.7 garbled mfsprg %r5,0 /* save current sprg0 (curcpu) */
111 1.7 garbled lis %r4,ofwsprg0save@ha
112 1.7 garbled addi %r4,%r4,ofwsprg0save@l
113 1.7 garbled stw %r5,0(%r4)
114 1.4 aymeric
115 1.4 aymeric #ifdef FIRMWORKSBUGS
116 1.7 garbled lis %r4,ofwreal_incharge@ha
117 1.7 garbled lwz %r4,ofwreal_incharge@l(%r4)
118 1.7 garbled cmpwi %r4,1
119 1.4 aymeric bne 1f
120 1.4 aymeric blrl
121 1.6 garbled b 4f
122 1.4 aymeric 1:
123 1.6 garbled #endif
124 1.7 garbled mfmsr %r4 /* save msr */
125 1.7 garbled stw %r4,8(%r1)
126 1.4 aymeric
127 1.7 garbled li %r0,0 /* clear battable translations */
128 1.6 garbled #if defined (PPC_OEA) || defined (PPC_OEA64_BRIDGE)
129 1.7 garbled mtdbatu 2,%r0
130 1.7 garbled mtdbatu 3,%r0
131 1.7 garbled mtibatu 2,%r0
132 1.7 garbled mtibatu 3,%r0
133 1.3 sanjayl #endif /* PPC_OEA */
134 1.1 matt
135 1.7 garbled lis %r4,ofwsrsave@ha /* save current SRs */
136 1.7 garbled addi %r4,%r4,ofwsrsave@l
137 1.7 garbled li %r5,0
138 1.7 garbled 1: mfsrin %r0,%r5
139 1.7 garbled stw %r0,0(%r4)
140 1.7 garbled addi %r4,%r4,4
141 1.7 garbled addis %r5,%r5,0x10000000@h
142 1.7 garbled cmpwi %r5,0
143 1.1 matt bne 1b
144 1.1 matt
145 1.7 garbled lis %r4,_C_LABEL(ofw_pmap)@ha /* load OFW SR */
146 1.7 garbled addi %r4,%r4,_C_LABEL(ofw_pmap)@l
147 1.7 garbled lwz %r0,PM_KERNELSR(%r4)
148 1.7 garbled cmpwi %r0,0 /* pm_sr[KERNEL_SR] == 0? */
149 1.1 matt beq 2f /* then skip (not initialized yet) */
150 1.7 garbled li %r5,0
151 1.7 garbled 1: lwz %r0,0(%r4)
152 1.7 garbled mtsrin %r0,%r5
153 1.7 garbled addi %r4,%r4,4
154 1.7 garbled addis %r5,%r5,0x10000000@h
155 1.7 garbled cmpwi %r5,0
156 1.1 matt bne 1b
157 1.1 matt 2:
158 1.7 garbled lis %r4,ofmsr@ha /* Open Firmware msr + sprg[0-3] */
159 1.7 garbled lwzu %r5,ofmsr+16@l(%r4)
160 1.7 garbled mtsprg 3,%r5
161 1.7 garbled lwzu %r5,-4(%r4)
162 1.7 garbled mtsprg 2,%r5
163 1.7 garbled lwzu %r5,-4(%r4)
164 1.7 garbled mtsprg 1,%r5
165 1.7 garbled lwzu %r5,-4(%r4)
166 1.7 garbled mtsprg 0,%r5
167 1.7 garbled lwz %r5,-4(%r4)
168 1.7 garbled mtmsr %r5
169 1.1 matt isync
170 1.1 matt
171 1.1 matt blrl /* call Open Firmware */
172 1.1 matt
173 1.7 garbled lis %r4,ofwsrsave@ha /* restore saved SRs */
174 1.7 garbled addi %r4,%r4,ofwsrsave@l
175 1.7 garbled li %r5,0
176 1.7 garbled 1: lwz %r0,0(%r4)
177 1.7 garbled mtsrin %r0,%r5
178 1.7 garbled addi %r4,%r4,4
179 1.7 garbled addis %r5,%r5,0x10000000@h
180 1.7 garbled cmpwi %r5,0
181 1.1 matt bne 1b
182 1.1 matt
183 1.7 garbled lwz %r4,8(%r1) /* restore msr */
184 1.7 garbled mtmsr %r4
185 1.1 matt isync
186 1.6 garbled 4:
187 1.7 garbled lis %r4,ofwsprg0save@ha /* restore saved sprg0 (curcpu) */
188 1.7 garbled addi %r4,%r4,ofwsprg0save@l
189 1.7 garbled lwz %r5,0(%r4)
190 1.7 garbled mtsprg 0,%r5
191 1.7 garbled
192 1.7 garbled lwz %r1,0(%r1) /* and return */
193 1.7 garbled lwz %r0,4(%r1)
194 1.7 garbled mtlr %r0
195 1.1 matt blr
196 1.1 matt
197 1.1 matt /*
198 1.1 matt * Switch to/from OpenFirmware real mode stack
199 1.1 matt *
200 1.1 matt * Note: has to be called as the very first thing in OpenFirmware interface
201 1.1 matt * routines.
202 1.1 matt * E.g.:
203 1.1 matt * int
204 1.1 matt * OF_xxx(arg1, arg2)
205 1.1 matt * type arg1, arg2;
206 1.1 matt * {
207 1.1 matt * static struct {
208 1.1 matt * char *name;
209 1.1 matt * int nargs;
210 1.1 matt * int nreturns;
211 1.1 matt * char *method;
212 1.1 matt * int arg1;
213 1.1 matt * int arg2;
214 1.1 matt * int ret;
215 1.1 matt * } args = {
216 1.1 matt * "xxx",
217 1.1 matt * 2,
218 1.1 matt * 1,
219 1.1 matt * };
220 1.1 matt *
221 1.1 matt * ofw_stack();
222 1.1 matt * args.arg1 = arg1;
223 1.1 matt * args.arg2 = arg2;
224 1.1 matt * if (openfirmware(&args) < 0)
225 1.1 matt * return -1;
226 1.1 matt * return args.ret;
227 1.1 matt * }
228 1.1 matt */
229 1.1 matt
230 1.1 matt ENTRY(ofw_stack)
231 1.7 garbled mfmsr %r8 /* turn off interrupts */
232 1.7 garbled andi. %r0,%r8,~(PSL_EE|PSL_RI)@l
233 1.7 garbled mtmsr %r0
234 1.7 garbled stw %r8,4(%r1) /* abuse return address slot */
235 1.7 garbled
236 1.7 garbled lwz %r5,0(%r1) /* get length of stack frame */
237 1.7 garbled subf %r5,%r1,%r5
238 1.7 garbled
239 1.7 garbled lis %r7,firmstk+NBPG-8@ha
240 1.7 garbled addi %r7,%r7,firmstk+NBPG-8@l
241 1.7 garbled lis %r6,ofw_back@ha
242 1.7 garbled addi %r6,%r6,ofw_back@l
243 1.7 garbled subf %r4,%r5,%r7 /* make room for stack frame on
244 1.1 matt new stack */
245 1.7 garbled stw %r6,-4(%r7) /* setup return pointer */
246 1.7 garbled stwu %r1,-8(%r7)
247 1.1 matt
248 1.7 garbled stw %r7,-8(%r4)
249 1.1 matt
250 1.7 garbled addi %r3,%r1,8
251 1.7 garbled addi %r1,%r4,-8
252 1.7 garbled subi %r5,%r5,8
253 1.1 matt
254 1.1 matt b _C_LABEL(ofbcopy) /* and copy it */
255 1.1 matt
256 1.1 matt ofw_back:
257 1.7 garbled lwz %r1,0(%r1) /* get callers original stack pointer */
258 1.1 matt
259 1.7 garbled lwz %r0,4(%r1) /* get saved msr from abused slot */
260 1.7 garbled mtmsr %r0
261 1.1 matt
262 1.7 garbled lwz %r1,0(%r1) /* return */
263 1.7 garbled lwz %r0,4(%r1)
264 1.7 garbled mtlr %r0
265 1.1 matt blr
266