Subversion Repositories HelenOS-historic

Rev

Rev 1130 | Rev 1620 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1130 Rev 1146
Line 29... Line 29...
29
#include "ofw.h"
29
#include "ofw.h"
30
#include "asm.h"
30
#include "asm.h"
31
#include "printf.h"
31
#include "printf.h"
32
 
32
 
33
#define MAX_OFW_ARGS    10
33
#define MAX_OFW_ARGS    10
34
#define STRING_SIZE     1024
34
#define BUF_SIZE        1024
35
 
35
 
36
typedef unsigned int ofw_arg_t;
36
typedef unsigned int ofw_arg_t;
37
typedef unsigned int ihandle;
37
typedef unsigned int ihandle;
38
typedef unsigned int phandle;
38
typedef unsigned int phandle;
39
 
39
 
Line 51... Line 51...
51
 
51
 
52
 
52
 
53
ofw_entry ofw;
53
ofw_entry ofw;
54
 
54
 
55
phandle ofw_chosen;
55
phandle ofw_chosen;
56
phandle ofw_aliases;
-
 
57
ihandle ofw_mmu;
-
 
58
ihandle ofw_stdout;
56
ihandle ofw_stdout;
-
 
57
phandle ofw_root;
-
 
58
ihandle ofw_mmu;
-
 
59
phandle ofw_memory;
-
 
60
phandle ofw_aliases;
59
 
61
 
60
 
62
 
61
static int ofw_call(const char *service, const int nargs, const int nret, ...)
63
static int ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
62
{
64
{
63
    va_list list;
65
    va_list list;
64
    ofw_args_t args;
66
    ofw_args_t args;
65
    int i;
67
    int i;
66
   
68
   
67
    args.service = service;
69
    args.service = service;
68
    args.nargs = nargs;
70
    args.nargs = nargs;
69
    args.nret = nret;
71
    args.nret = nret;
70
   
72
   
71
    va_start(list, nret);
73
    va_start(list, rets);
72
    for (i = 0; i < nargs; i++)
74
    for (i = 0; i < nargs; i++)
73
        args.args[i] = va_arg(list, ofw_arg_t);
75
        args.args[i] = va_arg(list, ofw_arg_t);
74
    va_end(list);
76
    va_end(list);
75
   
77
   
76
    for (i = 0; i < nret; i++)
78
    for (i = 0; i < nret; i++)
77
        args.args[i + nargs] = 0;
79
        args.args[i + nargs] = 0;
78
   
80
   
79
    ofw(&args);
81
    ofw(&args);
80
   
82
   
-
 
83
    for (i = 1; i < nret; i++)
-
 
84
        rets[i - 1] = args.args[i + nargs];
-
 
85
   
81
    return args.args[nargs];
86
    return args.args[nargs];
82
}
87
}
83
 
88
 
84
 
89
 
85
static phandle ofw_find_device(const char *name)
90
static phandle ofw_find_device(const char *name)
86
{
91
{
87
    return ofw_call("finddevice", 1, 1, name);
92
    return ofw_call("finddevice", 1, 1, NULL, name);
88
}
93
}
89
 
94
 
90
 
95
 
91
static int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
96
static int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
92
{
97
{
93
    return ofw_call("getprop", 4, 1, device, name, buf, buflen);
98
    return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
-
 
99
}
-
 
100
 
-
 
101
 
-
 
102
static unsigned int ofw_get_address_cells(const phandle device)
-
 
103
{
-
 
104
    unsigned int ret;
-
 
105
   
-
 
106
    if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0)
-
 
107
        if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0)
-
 
108
            ret = 1;
-
 
109
   
-
 
110
    return ret;
94
}
111
}
95
 
112
 
-
 
113
 
-
 
114
static unsigned int ofw_get_size_cells(const phandle device)
-
 
115
{
-
 
116
    unsigned int ret;
-
 
117
   
-
 
118
    if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0)
-
 
119
        if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0)
-
 
120
            ret = 1;
-
 
121
   
-
 
122
    return ret;
-
 
123
}
-
 
124
 
-
 
125
 
96
static ihandle ofw_open(const char *name)
126
static ihandle ofw_open(const char *name)
97
{
127
{
98
    return ofw_call("open", 1, 1, name);
128
    return ofw_call("open", 1, 1, NULL, name);
99
}
129
}
100
 
130
 
101
 
131
 
102
void init(void)
132
void init(void)
103
{
133
{
104
    ofw_chosen = ofw_find_device("/chosen");
134
    ofw_chosen = ofw_find_device("/chosen");
105
    if (ofw_chosen == -1)
135
    if (ofw_chosen == -1)
106
        halt();
136
        halt();
107
   
137
   
108
    if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(ofw_stdout)) <= 0) 
138
    if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(ofw_stdout)) <= 0)
109
        ofw_stdout = 0;
139
        ofw_stdout = 0;
110
   
140
   
111
    ofw_aliases = ofw_find_device("/aliases");
141
    ofw_root = ofw_find_device("/");
112
    if (ofw_aliases == -1) {
142
    if (ofw_root == -1) {
113
        puts("\nUnable to find /aliases device\n");
143
        puts("\r\nError: Unable to find / device, halted.\r\n");
-
 
144
        halt();
-
 
145
    }
-
 
146
   
-
 
147
    if (ofw_get_property(ofw_chosen, "mmu",  &ofw_mmu, sizeof(ofw_mmu)) <= 0) {
-
 
148
        puts("\r\nError: Unable to get mmu property, halted.\r\n");
114
        halt();
149
        halt();
115
    }
150
    }
116
   
151
   
117
    ofw_mmu = ofw_open("/mmu");
152
    ofw_memory = ofw_find_device("/memory");
118
    if (ofw_mmu == -1) {
153
    if (ofw_memory == -1) {
119
        puts("\nUnable to open /mmu node\n");
154
        puts("\r\nError: Unable to find /memory device, halted.\r\n");
-
 
155
        halt();
-
 
156
    }
-
 
157
   
-
 
158
    ofw_aliases = ofw_find_device("/aliases");
-
 
159
    if (ofw_aliases == -1) {
-
 
160
        puts("\r\nError: Unable to find /aliases device, halted.\r\n");
120
        halt();
161
        halt();
121
    }
162
    }
122
}
163
}
123
 
164
 
124
 
165
 
125
void ofw_write(const char *str, const int len)
166
void ofw_write(const char *str, const int len)
126
{
167
{
127
    if (ofw_stdout == 0)
168
    if (ofw_stdout == 0)
128
        return;
169
        return;
129
   
170
   
130
    ofw_call("write", 3, 1, ofw_stdout, str, len);
171
    ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
131
}
172
}
132
 
173
 
133
 
174
 
134
void *ofw_translate(const void *virt)
175
void *ofw_translate(const void *virt)
135
{
176
{
-
 
177
    ofw_arg_t result[3];
-
 
178
   
136
    return (void *) ofw_call("call-method", 7, 1, "translate", ofw_mmu, virt, 0, 0, 0, 0);
179
    if (ofw_call("call-method", 4, 4, result, "translate", ofw_mmu, virt, 1) != 0) {
-
 
180
        puts("Error: MMU method translate() failed, halting.\n");
-
 
181
        halt();
-
 
182
    }
-
 
183
    return (void *) result[2];
137
}
184
}
138
 
185
 
139
 
186
 
140
int ofw_map(const void *phys, const void *virt, const int size, const int mode)
187
int ofw_map(const void *phys, const void *virt, const int size, const int mode)
141
{
188
{
142
    return ofw_call("call-method", 6, 1, "map", ofw_mmu, mode, size, virt, phys);
189
    return ofw_call("call-method", 6, 1, NULL, "map", ofw_mmu, mode, size, virt, phys);
143
}
190
}
144
 
191
 
145
 
192
 
146
int ofw_memmap(memmap_t *map)
193
int ofw_memmap(memmap_t *map)
147
{
194
{
148
    int i;
-
 
149
    int ret;
-
 
150
 
-
 
151
    phandle handle = ofw_find_device("/memory");
-
 
152
    if (handle == -1)
-
 
153
        return false;
195
    unsigned int buf[BUF_SIZE];
154
   
-
 
155
    ret = ofw_get_property(handle, "reg", &map->zones, sizeof(map->zones));
196
    int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(unsigned int) * BUF_SIZE);
156
    if (ret == -1)
197
    if (ret <= 0)
157
        return false;
198
        return false;
-
 
199
       
-
 
200
    unsigned int ac = ofw_get_address_cells(ofw_memory);
-
 
201
    unsigned int sc = ofw_get_size_cells(ofw_memory);
158
   
202
   
-
 
203
    int pos;
159
    map->total = 0;
204
    map->total = 0;
160
    map->count = 0;
205
    map->count = 0;
161
    for (i = 0; i < MEMMAP_MAX_RECORDS; i++) {
206
    for (pos = 0; (pos < ret / sizeof(unsigned int)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
162
        if (map->zones[i].size == 0)
207
        void * start = (void *) buf[pos + ac - 1];
-
 
208
        unsigned int size = buf[pos + ac + sc - 1];
-
 
209
       
163
            break;
210
        if (size > 0) {
-
 
211
            map->zones[map->count].start = start;
-
 
212
            map->zones[map->count].size = size;
164
        map->count++;
213
            map->count++;
165
        map->total += map->zones[i].size;
214
            map->total += size;
-
 
215
        }
166
    }
216
    }
167
}
217
}
168
 
218
 
169
 
219
 
170
int ofw_screen(screen_t *screen)
220
int ofw_screen(screen_t *screen)
171
{
221
{
172
    char device_name[STRING_SIZE];
222
    char device_name[BUF_SIZE];
173
   
223
   
174
    if (ofw_get_property(ofw_aliases, "screen", device_name, STRING_SIZE) <= 0)
224
    if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(char) * BUF_SIZE) <= 0)
175
        return false;
225
        return false;
176
   
226
   
177
    phandle device = ofw_find_device(device_name);
227
    phandle device = ofw_find_device(device_name);
178
    if (device == -1)
228
    if (device == -1)
179
        return false;
229
        return false;