blob: c3288a3446b3602bc8583c6d8295ea559c7aa29b [file] [log] [blame]
David Gibson2e601612007-06-13 14:52:54 +10001/*
2 * Copyright (C) Paul Mackerras 1997.
3 *
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.
8 */
9#include <stddef.h>
10#include "types.h"
11#include "elf.h"
12#include "string.h"
13#include "stdio.h"
14#include "page.h"
15#include "ops.h"
16
17#include "of.h"
18
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020019/* The following structure is used to communicate with open firmware.
20 * All arguments in and out are in big endian format. */
21struct prom_args {
22 __be32 service; /* Address of service name string. */
23 __be32 nargs; /* Number of input arguments. */
24 __be32 nret; /* Number of output arguments. */
25 __be32 args[10]; /* Input/output arguments. */
26};
27
David Gibson2e601612007-06-13 14:52:54 +100028static int (*prom) (void *);
29
30void of_init(void *promptr)
31{
32 prom = (int (*)(void *))promptr;
33}
34
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020035#define ADDR(x) (u32)(unsigned long)(x)
36
David Gibson2e601612007-06-13 14:52:54 +100037int of_call_prom(const char *service, int nargs, int nret, ...)
38{
39 int i;
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020040 struct prom_args args;
David Gibson2e601612007-06-13 14:52:54 +100041 va_list list;
42
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020043 args.service = ADDR(service);
David Gibson2e601612007-06-13 14:52:54 +100044 args.nargs = nargs;
45 args.nret = nret;
46
47 va_start(list, nret);
48 for (i = 0; i < nargs; i++)
49 args.args[i] = va_arg(list, unsigned int);
50 va_end(list);
51
52 for (i = 0; i < nret; i++)
53 args.args[nargs+i] = 0;
54
55 if (prom(&args) < 0)
56 return -1;
57
58 return (nret > 0)? args.args[nargs]: 0;
59}
60
61static int of_call_prom_ret(const char *service, int nargs, int nret,
62 unsigned int *rets, ...)
63{
64 int i;
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020065 struct prom_args args;
David Gibson2e601612007-06-13 14:52:54 +100066 va_list list;
67
Cédric Le Goaterfed23ed2014-04-24 09:23:26 +020068 args.service = ADDR(service);
David Gibson2e601612007-06-13 14:52:54 +100069 args.nargs = nargs;
70 args.nret = nret;
71
72 va_start(list, rets);
73 for (i = 0; i < nargs; i++)
74 args.args[i] = va_arg(list, unsigned int);
75 va_end(list);
76
77 for (i = 0; i < nret; i++)
78 args.args[nargs+i] = 0;
79
80 if (prom(&args) < 0)
81 return -1;
82
83 if (rets != (void *) 0)
84 for (i = 1; i < nret; ++i)
85 rets[i-1] = args.args[nargs+i];
86
87 return (nret > 0)? args.args[nargs]: 0;
88}
89
90/* returns true if s2 is a prefix of s1 */
91static int string_match(const char *s1, const char *s2)
92{
93 for (; *s2; ++s2)
94 if (*s1++ != *s2)
95 return 0;
96 return 1;
97}
98
99/*
100 * Older OF's require that when claiming a specific range of addresses,
101 * we claim the physical space in the /memory node and the virtual
102 * space in the chosen mmu node, and then do a map operation to
103 * map virtual to physical.
104 */
105static int need_map = -1;
106static ihandle chosen_mmu;
107static phandle memory;
108
109static int check_of_version(void)
110{
111 phandle oprom, chosen;
112 char version[64];
113
David Gibson08464712007-06-27 16:54:58 +1000114 oprom = of_finddevice("/openprom");
David Gibson2e601612007-06-13 14:52:54 +1000115 if (oprom == (phandle) -1)
116 return 0;
David Gibson08464712007-06-27 16:54:58 +1000117 if (of_getprop(oprom, "model", version, sizeof(version)) <= 0)
David Gibson2e601612007-06-13 14:52:54 +1000118 return 0;
119 version[sizeof(version)-1] = 0;
120 printf("OF version = '%s'\r\n", version);
121 if (!string_match(version, "Open Firmware, 1.")
122 && !string_match(version, "FirmWorks,3."))
123 return 0;
David Gibson08464712007-06-27 16:54:58 +1000124 chosen = of_finddevice("/chosen");
David Gibson2e601612007-06-13 14:52:54 +1000125 if (chosen == (phandle) -1) {
David Gibson08464712007-06-27 16:54:58 +1000126 chosen = of_finddevice("/chosen@0");
David Gibson2e601612007-06-13 14:52:54 +1000127 if (chosen == (phandle) -1) {
128 printf("no chosen\n");
129 return 0;
130 }
131 }
David Gibson08464712007-06-27 16:54:58 +1000132 if (of_getprop(chosen, "mmu", &chosen_mmu, sizeof(chosen_mmu)) <= 0) {
David Gibson2e601612007-06-13 14:52:54 +1000133 printf("no mmu\n");
134 return 0;
135 }
136 memory = (ihandle) of_call_prom("open", 1, 1, "/memory");
137 if (memory == (ihandle) -1) {
138 memory = (ihandle) of_call_prom("open", 1, 1, "/memory@0");
139 if (memory == (ihandle) -1) {
140 printf("no memory node\n");
141 return 0;
142 }
143 }
144 printf("old OF detected\r\n");
145 return 1;
146}
147
148void *of_claim(unsigned long virt, unsigned long size, unsigned long align)
149{
150 int ret;
151 unsigned int result;
152
153 if (need_map < 0)
154 need_map = check_of_version();
155 if (align || !need_map)
156 return (void *) of_call_prom("claim", 3, 1, virt, size, align);
157
158 ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", memory,
159 align, size, virt);
160 if (ret != 0 || result == -1)
161 return (void *) -1;
162 ret = of_call_prom_ret("call-method", 5, 2, &result, "claim", chosen_mmu,
163 align, size, virt);
164 /* 0x12 == coherent + read/write */
165 ret = of_call_prom("call-method", 6, 1, "map", chosen_mmu,
166 0x12, size, virt, virt);
167 return (void *) virt;
168}
169
David Gibson08464712007-06-27 16:54:58 +1000170void *of_vmlinux_alloc(unsigned long size)
171{
Tony Breeds9b09c6d2008-06-24 14:20:29 +1000172 unsigned long start = (unsigned long)_start, end = (unsigned long)_end;
173 void *addr;
174 void *p;
David Gibson08464712007-06-27 16:54:58 +1000175
Tony Breeds9b09c6d2008-06-24 14:20:29 +1000176 /* With some older POWER4 firmware we need to claim the area the kernel
177 * will reside in. Newer firmwares don't need this so we just ignore
178 * the return value.
179 */
180 addr = of_claim(start, end - start, 0);
181 printf("Trying to claim from 0x%lx to 0x%lx (0x%lx) got %p\r\n",
182 start, end, end - start, addr);
183
184 p = malloc(size);
David Gibson08464712007-06-27 16:54:58 +1000185 if (!p)
186 fatal("Can't allocate memory for kernel image!\n\r");
187
188 return p;
189}
190
David Gibson2e601612007-06-13 14:52:54 +1000191void of_exit(void)
192{
193 of_call_prom("exit", 0, 0);
194}
David Gibson08464712007-06-27 16:54:58 +1000195
196/*
197 * OF device tree routines
198 */
199void *of_finddevice(const char *name)
200{
201 return (phandle) of_call_prom("finddevice", 1, 1, name);
202}
203
204int of_getprop(const void *phandle, const char *name, void *buf,
205 const int buflen)
206{
207 return of_call_prom("getprop", 4, 1, phandle, name, buf, buflen);
208}
209
210int of_setprop(const void *phandle, const char *name, const void *buf,
211 const int buflen)
212{
213 return of_call_prom("setprop", 4, 1, phandle, name, buf, buflen);
214}