Subversion Repositories HelenOS-historic

Rev

Rev 1780 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1780 Rev 1784
Line 36... Line 36...
36
#include <arch/asm.h>
36
#include <arch/asm.h>
37
#include <stdarg.h>
37
#include <stdarg.h>
38
#include <cpu.h>
38
#include <cpu.h>
39
#include <arch/types.h>
39
#include <arch/types.h>
40
 
40
 
41
ofw_entry ofw;
41
uintptr_t ofw_cif;  /**< OpenFirmware Client Interface address. */
42
 
42
 
43
phandle ofw_chosen;
43
phandle ofw_chosen;
44
ihandle ofw_stdin;
-
 
45
ihandle ofw_stdout;
44
ihandle ofw_stdout;
-
 
45
ihandle ofw_mmu;
46
 
46
 
47
void ofw_init(void)
47
void ofw_init(void)
48
{
48
{
49
    ofw_chosen = ofw_find_device("/chosen");
49
    ofw_chosen = ofw_find_device("/chosen");
50
    if (ofw_chosen == -1)
50
    if (ofw_chosen == -1)
51
        ofw_done();
51
        ofw_done();
52
   
52
   
53
    if (ofw_get_property(ofw_chosen, "stdin",  &ofw_stdin, sizeof(ofw_stdin)) <= 0)
-
 
54
        ofw_stdin = 0;
-
 
55
       
-
 
56
    if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(ofw_stdout)) <= 0)
53
    if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(ofw_stdout)) <= 0)
57
        ofw_stdout = 0;
54
        ofw_stdout = 0;
-
 
55
 
-
 
56
    if (ofw_get_property(ofw_chosen, "mmu",  &ofw_mmu, sizeof(ofw_mmu)) <= 0)
-
 
57
        ofw_mmu = 0;
58
}
58
}
59
 
59
 
60
void ofw_done(void)
60
void ofw_done(void)
61
{
61
{
62
    (void) ofw_call("exit", 0, 0);
62
    (void) ofw_call("exit", 0, 1, NULL);
63
    cpu_halt();
63
    cpu_halt();
64
}
64
}
65
 
65
 
-
 
66
/** Perform a call to OpenFirmware client interface.
-
 
67
 *
-
 
68
 * @param service String identifying the service requested.
-
 
69
 * @param nargs Number of input arguments.
-
 
70
 * @param nret Number of output arguments. This includes the return value.
-
 
71
 * @param rets Buffer for output arguments or NULL. The buffer must accomodate nret - 1 items.
-
 
72
 *
-
 
73
 * @return Return value returned by the client interface.
-
 
74
 */
66
unative_t ofw_call(const char *service, const int nargs, const int nret, ...)
75
ofw_arg_t ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
67
{
76
{
68
    va_list list;
77
    va_list list;
69
    ofw_args_t args;
78
    ofw_args_t args;
70
    int i;
79
    int i;
71
   
80
   
72
    args.service = service;
81
    args.service = service;
73
    args.nargs = nargs;
82
    args.nargs = nargs;
74
    args.nret = nret;
83
    args.nret = nret;
75
   
84
   
76
    va_start(list, nret);
85
    va_start(list, rets);
77
    for (i = 0; i < nargs; i++)
86
    for (i = 0; i < nargs; i++)
78
        args.args[i] = va_arg(list, ofw_arg_t);
87
        args.args[i] = va_arg(list, ofw_arg_t);
79
    va_end(list);
88
    va_end(list);
80
   
89
   
81
    for (i = 0; i < nret; i++)
90
    for (i = 0; i < nret; i++)
82
        args.args[i + nargs] = 0;
91
        args.args[i + nargs] = 0;
83
   
92
   
84
    ofw(&args);
93
    (void) ofw(&args);
85
   
94
 
-
 
95
    for (i = 1; i < nret; i++)
-
 
96
        rets[i - 1] = args.args[i + nargs];
-
 
97
 
86
    return args.args[nargs];
98
    return args.args[nargs];
87
}
99
}
88
 
100
 
89
void ofw_putchar(const char ch)
101
void ofw_putchar(const char ch)
90
{
102
{
91
    if (ofw_stdout == 0)
103
    if (ofw_stdout == 0)
92
        return;
104
        return;
93
   
105
   
94
    (void) ofw_call("write", 3, 1, ofw_stdout, &ch, 1);
106
    (void) ofw_call("write", 3, 1, NULL, ofw_stdout, &ch, 1);
95
}
107
}
96
 
108
 
97
/** Read character from OFW's input.
-
 
98
 *
-
 
99
 * This call is non-blocking.
-
 
100
 *
-
 
101
 * @return 0 if no character was read, character read otherwise.
-
 
102
 */
-
 
103
char ofw_getchar(void)
109
phandle ofw_find_device(const char *name)
104
{
110
{
105
    char ch;
-
 
106
 
-
 
107
    if (ofw_stdin == 0)
-
 
108
        return 0;
-
 
109
   
-
 
110
    if (ofw_call("read", 3, 1, ofw_stdin, &ch, 1) == 1)
111
    return (phandle) ofw_call("finddevice", 1, 1, NULL, name);
111
        return ch;
-
 
112
    else
-
 
113
        return 0;
-
 
114
}
112
}
115
 
113
 
116
phandle ofw_find_device(const char *name)
114
int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
117
{
115
{
118
    return (phandle) ofw_call("finddevice", 1, 1, name);
116
    return (int) ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
119
}
117
}
120
 
118
 
-
 
119
/** Translate virtual address to physical address using OpenFirmware.
-
 
120
 *
-
 
121
 * Use this function only when OpenFirmware is in charge.
-
 
122
 *
-
 
123
 * @param virt Virtual address.
121
int ofw_get_property(const phandle device, const char *name, void *buf, const int buflen)
124
 * @return NULL on failure or physical address on success.
-
 
125
 */
-
 
126
void *ofw_translate(const void *virt)
122
{
127
{
-
 
128
    ofw_arg_t result[4];
-
 
129
    int shift;
-
 
130
   
-
 
131
    if (!ofw_mmu)
-
 
132
        return NULL;
-
 
133
   
123
    return (int) ofw_call("getprop", 4, 1, device, name, buf, buflen);
134
    if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0)
-
 
135
        return NULL;
-
 
136
 
-
 
137
    if (result[0] != -1)
-
 
138
        return NULL;
-
 
139
                               
-
 
140
    if (sizeof(unative_t) == 8)
-
 
141
        shift = 32;
-
 
142
    else
-
 
143
        shift = 0;
-
 
144
   
-
 
145
    return (void *) ((result[2]<<shift)|result[3]);
124
}
146
}
125
 
147
 
126
void *ofw_claim(const void *addr, const int size, const int align)
148
void *ofw_claim(const void *addr, const int size, const int align)
127
{
149
{
128
    return (void *) ofw_call("claim", 3, 1, addr, size, align);
150
    return (void *) ofw_call("claim", 3, 1, NULL, addr, size, align);
129
}
151
}
130
 
152
 
131
/** @}
153
/** @}
132
 */
154
 */