2 * Copyright (C) Paul Mackerras 1997.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
16 phandle chosen_handle
;
19 int call_prom(const char *service
, int nargs
, int nret
, ...)
26 unsigned int args
[12];
30 args
.service
= service
;
35 for (i
= 0; i
< nargs
; i
++)
36 args
.args
[i
] = va_arg(list
, unsigned int);
39 for (i
= 0; i
< nret
; i
++)
40 args
.args
[nargs
+i
] = 0;
45 return (nret
> 0)? args
.args
[nargs
]: 0;
48 int call_prom_ret(const char *service
, int nargs
, int nret
,
49 unsigned int *rets
, ...)
56 unsigned int args
[12];
60 args
.service
= service
;
65 for (i
= 0; i
< nargs
; i
++)
66 args
.args
[i
] = va_arg(list
, unsigned int);
69 for (i
= 0; i
< nret
; i
++)
70 args
.args
[nargs
+i
] = 0;
75 if (rets
!= (void *) 0)
76 for (i
= 1; i
< nret
; ++i
)
77 rets
[i
-1] = args
.args
[nargs
+i
];
79 return (nret
> 0)? args
.args
[nargs
]: 0;
82 int write(void *handle
, void *ptr
, int nb
)
84 return call_prom("write", 3, 1, handle
, ptr
, nb
);
88 * Older OF's require that when claiming a specific range of addresses,
89 * we claim the physical space in the /memory node and the virtual
90 * space in the chosen mmu node, and then do a map operation to
91 * map virtual to physical.
93 static int need_map
= -1;
94 static ihandle chosen_mmu
;
95 static phandle memory
;
97 /* returns true if s2 is a prefix of s1 */
98 static int string_match(const char *s1
, const char *s2
)
106 static int check_of_version(void)
108 phandle oprom
, chosen
;
111 oprom
= finddevice("/openprom");
112 if (oprom
== (phandle
) -1)
114 if (getprop(oprom
, "model", version
, sizeof(version
)) <= 0)
116 version
[sizeof(version
)-1] = 0;
117 printf("OF version = '%s'\r\n", version
);
118 if (!string_match(version
, "Open Firmware, 1.")
119 && !string_match(version
, "FirmWorks,3."))
121 chosen
= finddevice("/chosen");
122 if (chosen
== (phandle
) -1) {
123 chosen
= finddevice("/chosen@0");
124 if (chosen
== (phandle
) -1) {
125 printf("no chosen\n");
129 if (getprop(chosen
, "mmu", &chosen_mmu
, sizeof(chosen_mmu
)) <= 0) {
133 memory
= (ihandle
) call_prom("open", 1, 1, "/memory");
134 if (memory
== (ihandle
) -1) {
135 memory
= (ihandle
) call_prom("open", 1, 1, "/memory@0");
136 if (memory
== (ihandle
) -1) {
137 printf("no memory node\n");
141 printf("old OF detected\r\n");
145 void *claim(unsigned long virt
, unsigned long size
, unsigned long align
)
151 need_map
= check_of_version();
152 if (align
|| !need_map
)
153 return (void *) call_prom("claim", 3, 1, virt
, size
, align
);
155 ret
= call_prom_ret("call-method", 5, 2, &result
, "claim", memory
,
157 if (ret
!= 0 || result
== -1)
159 ret
= call_prom_ret("call-method", 5, 2, &result
, "claim", chosen_mmu
,
161 /* 0x12 == coherent + read/write */
162 ret
= call_prom("call-method", 6, 1, "map", chosen_mmu
,
163 0x12, size
, virt
, virt
);
164 return (void *) virt
;