[ce8725be] | 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"
|
---|
| 30 | #include <printf.h>
|
---|
| 31 | #include <asm.h>
|
---|
[822b64e] | 32 | #include <types.h>
|
---|
[ce8725be] | 33 |
|
---|
| 34 | #define MAX_OFW_ARGS 10
|
---|
| 35 |
|
---|
| 36 | /** OpenFirmware command structure
|
---|
| 37 | *
|
---|
| 38 | */
|
---|
| 39 | typedef struct {
|
---|
| 40 | const char *service; /**< Command name */
|
---|
[b95b717] | 41 | unsigned long nargs; /**< Number of in arguments */
|
---|
| 42 | unsigned long nret; /**< Number of out arguments */
|
---|
[ce8725be] | 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 |
|
---|
| 48 | ofw_entry ofw;
|
---|
| 49 |
|
---|
| 50 | phandle ofw_chosen;
|
---|
| 51 | ihandle ofw_stdout;
|
---|
| 52 | phandle ofw_root;
|
---|
| 53 | ihandle ofw_mmu;
|
---|
| 54 | phandle ofw_memory;
|
---|
| 55 | phandle ofw_aliases;
|
---|
| 56 |
|
---|
[b95b717] | 57 | static unsigned long ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
|
---|
[ce8725be] | 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 |
|
---|
| 67 | va_start(list, rets);
|
---|
| 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 |
|
---|
| 77 | for (i = 1; i < nret; i++)
|
---|
| 78 | rets[i - 1] = args.args[i + nargs];
|
---|
| 79 |
|
---|
| 80 | return args.args[nargs];
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 |
|
---|
[b7b5f83] | 84 | phandle ofw_find_device(const char *name)
|
---|
[ce8725be] | 85 | {
|
---|
| 86 | return ofw_call("finddevice", 1, 1, NULL, name);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 |
|
---|
[b7b5f83] | 90 | int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
|
---|
[ce8725be] | 91 | {
|
---|
| 92 | return ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 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 |
|
---|
| 120 | static ihandle ofw_open(const char *name)
|
---|
| 121 | {
|
---|
| 122 | return ofw_call("open", 1, 1, NULL, name);
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 |
|
---|
| 126 | void init(void)
|
---|
| 127 | {
|
---|
| 128 | ofw_chosen = ofw_find_device("/chosen");
|
---|
| 129 | if (ofw_chosen == -1)
|
---|
| 130 | halt();
|
---|
| 131 |
|
---|
| 132 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0)
|
---|
| 133 | ofw_stdout = 0;
|
---|
| 134 |
|
---|
| 135 | ofw_root = ofw_find_device("/");
|
---|
| 136 | if (ofw_root == -1) {
|
---|
| 137 | puts("\r\nError: Unable to find / device, halted.\r\n");
|
---|
| 138 | halt();
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 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");
|
---|
| 143 | halt();
|
---|
| 144 | }
|
---|
| 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 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
| 160 | void ofw_write(const char *str, const int len)
|
---|
| 161 | {
|
---|
| 162 | if (ofw_stdout == 0)
|
---|
| 163 | return;
|
---|
| 164 |
|
---|
| 165 | ofw_call("write", 3, 1, NULL, ofw_stdout, str, len);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 | void *ofw_translate(const void *virt)
|
---|
| 170 | {
|
---|
[822b64e] | 171 | ofw_arg_t result[4];
|
---|
| 172 | int shift;
|
---|
| 173 |
|
---|
| 174 | if (ofw_call("call-method", 3, 5, result, "translate", ofw_mmu, virt) != 0) {
|
---|
[ce8725be] | 175 | puts("Error: MMU method translate() failed, halting.\n");
|
---|
| 176 | halt();
|
---|
| 177 | }
|
---|
[822b64e] | 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));
|
---|
[ce8725be] | 185 | }
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | int ofw_map(const void *phys, const void *virt, const int size, const int mode)
|
---|
| 189 | {
|
---|
[822b64e] | 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);
|
---|
[ce8725be] | 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | int ofw_memmap(memmap_t *map)
|
---|
| 207 | {
|
---|
[b7b5f83] | 208 | unsigned long buf[BUF_SIZE];
|
---|
| 209 | int ret = ofw_get_property(ofw_memory, "reg", buf, sizeof(buf));
|
---|
[ce8725be] | 210 | if (ret <= 0)
|
---|
| 211 | return false;
|
---|
| 212 |
|
---|
| 213 | unsigned int ac = ofw_get_address_cells(ofw_memory);
|
---|
| 214 | unsigned int sc = ofw_get_size_cells(ofw_memory);
|
---|
| 215 |
|
---|
| 216 | int pos;
|
---|
| 217 | map->total = 0;
|
---|
| 218 | map->count = 0;
|
---|
[b7b5f83] | 219 | for (pos = 0; (pos < ret / sizeof(unsigned long)) && (map->count < MEMMAP_MAX_RECORDS); pos += ac + sc) {
|
---|
[ce8725be] | 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 | }
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 | int ofw_screen(screen_t *screen)
|
---|
| 234 | {
|
---|
| 235 | char device_name[BUF_SIZE];
|
---|
| 236 |
|
---|
[b7b5f83] | 237 | if (ofw_get_property(ofw_aliases, "screen", device_name, sizeof(device_name)) <= 0)
|
---|
[ce8725be] | 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 | }
|
---|