Subversion Repositories HelenOS

Rev

Rev 1772 | Rev 1783 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
885 decky 1
/*
2
 * Copyright (C) 2005 Martin Decky
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 *
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
14
 * - The name of the author may not be used to endorse or promote products
15
 *   derived from this software without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
28
 
29
#include "ofw.h"
1764 jermar 30
#include <printf.h>
31
#include <asm.h>
1782 jermar 32
#include <types.h>
885 decky 33
 
1764 jermar 34
#define MAX_OFW_ARGS        10
964 decky 35
 
36
/** OpenFirmware command structure
37
 *
38
 */
39
typedef struct {
40
    const char *service;          /**< Command name */
1772 jermar 41
    unsigned long nargs;          /**< Number of in arguments */
42
    unsigned long nret;           /**< Number of out arguments */
964 decky 43
    ofw_arg_t args[MAX_OFW_ARGS]; /**< List of arguments */
44
} ofw_args_t;
45
 
46
typedef void (*ofw_entry)(ofw_args_t *);
47
 
885 decky 48
ofw_entry ofw;
49
 
50
phandle ofw_chosen;
1146 decky 51
ihandle ofw_stdout;
52
phandle ofw_root;
53
ihandle ofw_mmu;
54
phandle ofw_memory;
1130 decky 55
phandle ofw_aliases;
885 decky 56
 
1772 jermar 57
static unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
885 decky 58
{
59
    va_list list;
60
    ofw_args_t args;
61
    int i;
62
 
63
    args.service = service;
64
    args.nargs = nargs;
65
    args.nret = nret;
66
 
1146 decky 67
    va_start(list, rets);
885 decky 68
    for (i = 0; i < nargs; i++)
69
        args.args[i] = va_arg(list, ofw_arg_t);
70
    va_end(list);
71
 
72
    for (i = 0; i < nret; i++)
73
        args.args[i + nargs] = 0;
74
 
75
    ofw(&args);
76
 
1146 decky 77
    for (i = 1; i < nret; i++)
78
        rets[i - 1] = args.args[i + nargs];
79
 
956 decky 80
    return args.args[nargs];
885 decky 81
}
82
 
83
 
1771 jermar 84
phandle ofw_find_device(const char *name)
953 decky 85
{
1146 decky 86
    return ofw_call("finddevice", 1, 1, NULL, name);
964 decky 87
}
88
 
89
 
1771 jermar 90
int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
964 decky 91
{
1146 decky 92
    return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
964 decky 93
}
94
 
1146 decky 95
 
96
static unsigned int ofw_get_address_cells(const phandle device)
97
{
98
    unsigned int ret;
99
 
100
    if (ofw_get_property(device, "#address-cells", &ret, sizeof(ret)) <= 0)
101
        if (ofw_get_property(ofw_root, "#address-cells", &ret, sizeof(ret)) <= 0)
102
            ret = 1;
103
 
104
    return ret;
105
}
106
 
107
 
108
static unsigned int ofw_get_size_cells(const phandle device)
109
{
110
    unsigned int ret;
111
 
112
    if (ofw_get_property(device, "#size-cells", &ret, sizeof(ret)) <= 0)
113
        if (ofw_get_property(ofw_root, "#size-cells", &ret, sizeof(ret)) <= 0)
114
            ret = 1;
115
 
116
    return ret;
117
}
118
 
119
 
964 decky 120
static ihandle ofw_open(const char *name)
121
{
1146 decky 122
    return ofw_call("open", 1, 1, NULL, name);
953 decky 123
}
124
 
125
 
964 decky 126
void init(void)
885 decky 127
{
964 decky 128
    ofw_chosen = ofw_find_device("/chosen");
129
    if (ofw_chosen == -1)
130
        halt();
885 decky 131
 
1146 decky 132
    if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(ofw_stdout)) <= 0)
964 decky 133
        ofw_stdout = 0;
134
 
1146 decky 135
    ofw_root = ofw_find_device("/");
136
    if (ofw_root == -1) {
137
        puts("\r\nError: Unable to find / device, halted.\r\n");
1130 decky 138
        halt();
139
    }
140
 
1146 decky 141
    if (ofw_get_property(ofw_chosen, "mmu",  &ofw_mmu, sizeof(ofw_mmu)) <= 0) {
142
        puts("\r\nError: Unable to get mmu property, halted.\r\n");
964 decky 143
        halt();
144
    }
1146 decky 145
 
146
    ofw_memory = ofw_find_device("/memory");
147
    if (ofw_memory == -1) {
148
        puts("\r\nError: Unable to find /memory device, halted.\r\n");
149
        halt();
150
    }
151
 
152
    ofw_aliases = ofw_find_device("/aliases");
153
    if (ofw_aliases == -1) {
154
        puts("\r\nError: Unable to find /aliases device, halted.\r\n");
155
        halt();
156
    }
885 decky 157
}
158
 
159
 
964 decky 160
void ofw_write(const char *str, const int len)
885 decky 161
{
964 decky 162
    if (ofw_stdout == 0)
163
        return;
164
 
1146 decky 165
    ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
885 decky 166
}
167
 
168
 
956 decky 169
void *ofw_translate(const void *virt)
885 decky 170
{
1782 jermar 171
    ofw_arg_t result[4];
172
    int shift;
173
 
174
    if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0) {
1146 decky 175
        puts("Error: MMU method translate() failed, halting.\n");
176
        halt();
177
    }
1782 jermar 178
 
179
    if (sizeof(unative_t) == 8)
180
        shift = 32;
181
    else
182
        shift = 0;
183
 
184
    return (void *) (((result[2]&0xffffffff)<<shift)|((result[3])&0xffffffff));
885 decky 185
}
953 decky 186
 
187
 
1075 decky 188
int ofw_map(const void *phys, const void *virt, const int size, const int mode)
189
{
1782 jermar 190
    uintptr_t phys_hi, phys_lo;
191
 
192
    if (sizeof(unative_t) == 8) {
193
        int shift = 32;
194
        phys_hi = (uintptr_t) phys >> shift;
195
        phys_lo = (uintptr_t) phys & 0xffffffff;
196
    } else {
197
        phys_hi = 0;
198
        phys_lo = (uintptr_t) phys;
199
    }
200
 
201
    return ofw_call("call-method", 7, 1, NULL, "map", ofw_mmu, mode, size, virt,
202
        phys_hi, phys_lo);
1075 decky 203
}
204
 
205
 
964 decky 206
int ofw_memmap(memmap_t *map)
207
{
1771 jermar 208
    unsigned long buf[BUF_SIZE];
209
    int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf));
1146 decky 210
    if (ret <= 0)
964 decky 211
        return false;
1146 decky 212
 
213
    unsigned int ac = ofw_get_address_cells(ofw_memory);
214
    unsigned int sc = ofw_get_size_cells(ofw_memory);
964 decky 215
 
1146 decky 216
    int pos;
964 decky 217
    map->total = 0;
218
    map->count = 0;
1771 jermar 219
    for (pos = 0; (pos < ret / sizeof(unsigned long)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
1146 decky 220
        void * start = (void *) buf[pos + ac - 1];
221
        unsigned int size = buf[pos + ac + sc - 1];
222
 
223
        if (size > 0) {
224
            map->zones[map->count].start = start;
225
            map->zones[map->count].size = size;
226
            map->count++;
227
            map->total += size;
228
        }
964 decky 229
    }
230
}
1130 decky 231
 
232
 
233
int ofw_screen(screen_t *screen)
234
{
1146 decky 235
    char device_name[BUF_SIZE];
1130 decky 236
 
1771 jermar 237
    if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0)
1130 decky 238
        return false;
239
 
240
    phandle device = ofw_find_device(device_name);
241
    if (device == -1)
242
        return false;
243
 
244
    if (ofw_get_property(device, "address", &screen->addr, sizeof(screen->addr)) <= 0)
245
        return false;
246
 
247
    if (ofw_get_property(device, "width", &screen->width, sizeof(screen->width)) <= 0)
248
        return false;
249
 
250
    if (ofw_get_property(device, "height", &screen->height, sizeof(screen->height)) <= 0)
251
        return false;
252
 
253
    if (ofw_get_property(device, "depth", &screen->bpp, sizeof(screen->bpp)) <= 0)
254
        return false;
255
 
256
    if (ofw_get_property(device, "linebytes", &screen->scanline, sizeof(screen->scanline)) <= 0)
257
        return false;
258
 
259
    return true;
260
}