1 1.4 dsl /* $NetBSD: Locore.c,v 1.4 2009/03/14 15:36:14 dsl Exp $ */ 2 1.1 thorpej 3 1.1 thorpej /* 4 1.1 thorpej * Copyright (C) 1995, 1996 Wolfgang Solfrank. 5 1.1 thorpej * Copyright (C) 1995, 1996 TooLs GmbH. 6 1.1 thorpej * All rights reserved. 7 1.1 thorpej * 8 1.1 thorpej * Redistribution and use in source and binary forms, with or without 9 1.1 thorpej * modification, are permitted provided that the following conditions 10 1.1 thorpej * are met: 11 1.1 thorpej * 1. Redistributions of source code must retain the above copyright 12 1.1 thorpej * notice, this list of conditions and the following disclaimer. 13 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright 14 1.1 thorpej * notice, this list of conditions and the following disclaimer in the 15 1.1 thorpej * documentation and/or other materials provided with the distribution. 16 1.1 thorpej * 3. All advertising materials mentioning features or use of this software 17 1.1 thorpej * must display the following acknowledgement: 18 1.1 thorpej * This product includes software developed by TooLs GmbH. 19 1.1 thorpej * 4. The name of TooLs GmbH may not be used to endorse or promote products 20 1.1 thorpej * derived from this software without specific prior written permission. 21 1.1 thorpej * 22 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR 23 1.1 thorpej * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 24 1.1 thorpej * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 25 1.1 thorpej * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 1.1 thorpej * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 27 1.1 thorpej * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 28 1.1 thorpej * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 1.1 thorpej * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 1.1 thorpej * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 1.1 thorpej * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 thorpej */ 33 1.1 thorpej 34 1.1 thorpej #include <lib/libsa/stand.h> 35 1.1 thorpej 36 1.1 thorpej #include <machine/cpu.h> 37 1.1 thorpej 38 1.1 thorpej #include <arm/armreg.h> 39 1.1 thorpej 40 1.1 thorpej #include "cache.h" 41 1.2 christos #include "extern.h" 42 1.1 thorpej #include "openfirm.h" 43 1.1 thorpej 44 1.2 christos static int (*openfirmware_entry)(void *); 45 1.2 christos static int openfirmware(void *); 46 1.1 thorpej 47 1.2 christos void startup(int (*)(void *), char *, int); 48 1.2 christos static void setup(void); 49 1.1 thorpej 50 1.1 thorpej void (*cache_syncI)(void); 51 1.1 thorpej 52 1.1 thorpej void abort(void); 53 1.1 thorpej void abort(void) 54 1.1 thorpej { 55 1.1 thorpej 56 1.1 thorpej /* Stupid compiler (__dead). */ 57 1.2 christos for (;;) 58 1.2 christos continue; 59 1.1 thorpej } 60 1.1 thorpej 61 1.1 thorpej static int 62 1.4 dsl openfirmware(void *arg) 63 1.1 thorpej { 64 1.1 thorpej 65 1.2 christos (*openfirmware_entry)(arg); 66 1.2 christos return 0; 67 1.1 thorpej } 68 1.1 thorpej 69 1.1 thorpej static vaddr_t 70 1.1 thorpej ofw_getcleaninfo(void) 71 1.1 thorpej { 72 1.1 thorpej int cpu, vclean; 73 1.1 thorpej 74 1.1 thorpej if ((cpu = OF_finddevice("/cpu")) == -1) 75 1.1 thorpej panic("no /cpu from OFW"); 76 1.1 thorpej 77 1.1 thorpej if (OF_getprop(cpu, "d-cache-flush-address", &vclean, 78 1.1 thorpej sizeof(vclean)) != sizeof(vclean)) { 79 1.1 thorpej printf("WARNING: no OFW d-cache-flush-address property\n"); 80 1.1 thorpej return (RELOC); 81 1.1 thorpej } 82 1.1 thorpej 83 1.1 thorpej return (of_decode_int((unsigned char *)&vclean)); 84 1.1 thorpej } 85 1.1 thorpej 86 1.1 thorpej void 87 1.2 christos startup(int (*openfirm)(void *), char *arg, int argl) 88 1.1 thorpej { 89 1.1 thorpej u_int cputype = cpufunc_id() & CPU_ID_CPU_MASK; 90 1.1 thorpej 91 1.1 thorpej openfirmware_entry = openfirm; 92 1.1 thorpej setup(); 93 1.1 thorpej 94 1.1 thorpej /* 95 1.1 thorpej * Determine the CPU type, and set up the appropriate 96 1.1 thorpej * I$ sync routine. 97 1.1 thorpej */ 98 1.1 thorpej if (cputype == CPU_ID_SA110 || cputype == CPU_ID_SA1100 || 99 1.1 thorpej cputype == CPU_ID_SA1110) { 100 1.1 thorpej extern vaddr_t sa110_cache_clean_addr; 101 1.1 thorpej cache_syncI = sa110_cache_syncI; 102 1.1 thorpej sa110_cache_clean_addr = ofw_getcleaninfo(); 103 1.1 thorpej } else { 104 1.1 thorpej printf("WARNING: no I$ sync routine for CPU 0x%x\n", 105 1.1 thorpej cputype); 106 1.1 thorpej } 107 1.1 thorpej 108 1.1 thorpej main(); 109 1.1 thorpej OF_exit(); 110 1.1 thorpej } 111 1.1 thorpej 112 1.1 thorpej int 113 1.1 thorpej of_decode_int(const u_char *p) 114 1.1 thorpej { 115 1.1 thorpej unsigned int i = *p++ << 8; 116 1.1 thorpej i = (i + *p++) << 8; 117 1.1 thorpej i = (i + *p++) << 8; 118 1.1 thorpej return (i + *p); 119 1.1 thorpej } 120 1.1 thorpej 121 1.1 thorpej __dead void 122 1.2 christos OF_exit(void) 123 1.1 thorpej { 124 1.1 thorpej static struct { 125 1.2 christos const char *name; 126 1.1 thorpej int nargs; 127 1.1 thorpej int nreturns; 128 1.1 thorpej } args = { 129 1.1 thorpej "exit", 130 1.1 thorpej 0, 131 1.1 thorpej 0 132 1.1 thorpej }; 133 1.1 thorpej 134 1.1 thorpej openfirmware(&args); 135 1.1 thorpej for (;;); /* just in case */ 136 1.1 thorpej } 137 1.1 thorpej 138 1.1 thorpej int 139 1.2 christos OF_finddevice(const char *name) 140 1.1 thorpej { 141 1.1 thorpej static struct { 142 1.2 christos const char *name; 143 1.1 thorpej int nargs; 144 1.1 thorpej int nreturns; 145 1.2 christos const char *device; 146 1.1 thorpej int phandle; 147 1.1 thorpej } args = { 148 1.1 thorpej "finddevice", 149 1.1 thorpej 1, 150 1.1 thorpej 1, 151 1.1 thorpej }; 152 1.1 thorpej 153 1.1 thorpej args.device = name; 154 1.1 thorpej if (openfirmware(&args) == -1) 155 1.1 thorpej return -1; 156 1.1 thorpej return args.phandle; 157 1.1 thorpej } 158 1.1 thorpej 159 1.1 thorpej int 160 1.2 christos OF_instance_to_package(int ihandle) 161 1.1 thorpej { 162 1.1 thorpej static struct { 163 1.2 christos const char *name; 164 1.1 thorpej int nargs; 165 1.1 thorpej int nreturns; 166 1.1 thorpej int ihandle; 167 1.1 thorpej int phandle; 168 1.1 thorpej } args = { 169 1.1 thorpej "instance-to-package", 170 1.1 thorpej 1, 171 1.1 thorpej 1, 172 1.1 thorpej }; 173 1.1 thorpej 174 1.1 thorpej args.ihandle = ihandle; 175 1.1 thorpej if (openfirmware(&args) == -1) 176 1.1 thorpej return -1; 177 1.1 thorpej return args.phandle; 178 1.1 thorpej } 179 1.1 thorpej 180 1.1 thorpej int 181 1.2 christos OF_getprop(int handle, const char *prop, void *buf, int buflen) 182 1.1 thorpej { 183 1.1 thorpej static struct { 184 1.2 christos const char *name; 185 1.1 thorpej int nargs; 186 1.1 thorpej int nreturns; 187 1.1 thorpej int phandle; 188 1.2 christos const char *prop; 189 1.1 thorpej void *buf; 190 1.1 thorpej int buflen; 191 1.1 thorpej int size; 192 1.1 thorpej } args = { 193 1.1 thorpej "getprop", 194 1.1 thorpej 4, 195 1.1 thorpej 1, 196 1.1 thorpej }; 197 1.1 thorpej 198 1.1 thorpej args.phandle = handle; 199 1.1 thorpej args.prop = prop; 200 1.1 thorpej args.buf = buf; 201 1.1 thorpej args.buflen = buflen; 202 1.1 thorpej if (openfirmware(&args) == -1) 203 1.1 thorpej return -1; 204 1.1 thorpej return args.size; 205 1.1 thorpej } 206 1.1 thorpej 207 1.1 thorpej #ifdef __notyet__ /* Has a bug on FirePower */ 208 1.1 thorpej int 209 1.2 christos OF_setprop(int handle, const char *prop, void *buf, int len) 210 1.1 thorpej { 211 1.1 thorpej static struct { 212 1.2 christos const char *name; 213 1.1 thorpej int nargs; 214 1.1 thorpej int nreturns; 215 1.1 thorpej int phandle; 216 1.2 christos const char *prop; 217 1.1 thorpej void *buf; 218 1.1 thorpej int len; 219 1.1 thorpej int size; 220 1.1 thorpej } args = { 221 1.1 thorpej "setprop", 222 1.1 thorpej 4, 223 1.1 thorpej 1, 224 1.1 thorpej }; 225 1.1 thorpej 226 1.1 thorpej args.phandle = handle; 227 1.1 thorpej args.prop = prop; 228 1.1 thorpej args.buf = buf; 229 1.1 thorpej args.len = len; 230 1.1 thorpej if (openfirmware(&args) == -1) 231 1.1 thorpej return -1; 232 1.1 thorpej return args.size; 233 1.1 thorpej } 234 1.1 thorpej #endif 235 1.1 thorpej 236 1.1 thorpej int 237 1.2 christos OF_open(char *dname) 238 1.1 thorpej { 239 1.1 thorpej static struct { 240 1.2 christos const char *name; 241 1.1 thorpej int nargs; 242 1.1 thorpej int nreturns; 243 1.1 thorpej char *dname; 244 1.1 thorpej int handle; 245 1.1 thorpej } args = { 246 1.1 thorpej "open", 247 1.1 thorpej 1, 248 1.1 thorpej 1, 249 1.1 thorpej }; 250 1.1 thorpej 251 1.1 thorpej #ifdef OFW_DEBUG 252 1.1 thorpej printf("OF_open(%s) -> ", dname); 253 1.1 thorpej #endif 254 1.1 thorpej args.dname = dname; 255 1.1 thorpej if (openfirmware(&args) == -1 || 256 1.1 thorpej args.handle == 0) { 257 1.1 thorpej #ifdef OFW_DEBUG 258 1.1 thorpej printf("lose\n"); 259 1.1 thorpej #endif 260 1.1 thorpej return -1; 261 1.1 thorpej } 262 1.1 thorpej #ifdef OFW_DEBUG 263 1.1 thorpej printf("%d\n", args.handle); 264 1.1 thorpej #endif 265 1.1 thorpej return args.handle; 266 1.1 thorpej } 267 1.1 thorpej 268 1.1 thorpej void 269 1.2 christos OF_close(int handle) 270 1.1 thorpej { 271 1.1 thorpej static struct { 272 1.2 christos const char *name; 273 1.1 thorpej int nargs; 274 1.1 thorpej int nreturns; 275 1.1 thorpej int handle; 276 1.1 thorpej } args = { 277 1.1 thorpej "close", 278 1.1 thorpej 1, 279 1.1 thorpej 0, 280 1.1 thorpej }; 281 1.1 thorpej 282 1.1 thorpej #ifdef OFW_DEBUG 283 1.1 thorpej printf("OF_close(%d)\n", handle); 284 1.1 thorpej #endif 285 1.1 thorpej args.handle = handle; 286 1.1 thorpej openfirmware(&args); 287 1.1 thorpej } 288 1.1 thorpej 289 1.1 thorpej int 290 1.2 christos OF_write(int handle, void *addr, int len) 291 1.1 thorpej { 292 1.1 thorpej static struct { 293 1.2 christos const char *name; 294 1.1 thorpej int nargs; 295 1.1 thorpej int nreturns; 296 1.1 thorpej int ihandle; 297 1.1 thorpej void *addr; 298 1.1 thorpej int len; 299 1.1 thorpej int actual; 300 1.1 thorpej } args = { 301 1.1 thorpej "write", 302 1.1 thorpej 3, 303 1.1 thorpej 1, 304 1.1 thorpej }; 305 1.1 thorpej 306 1.1 thorpej #ifdef OFW_DEBUG 307 1.1 thorpej if (len != 1) 308 1.1 thorpej printf("OF_write(%d, %x, %x) -> ", handle, addr, len); 309 1.1 thorpej #endif 310 1.1 thorpej args.ihandle = handle; 311 1.1 thorpej args.addr = addr; 312 1.1 thorpej args.len = len; 313 1.1 thorpej if (openfirmware(&args) == -1) { 314 1.1 thorpej #ifdef OFW_DEBUG 315 1.1 thorpej printf("lose\n"); 316 1.1 thorpej #endif 317 1.1 thorpej return -1; 318 1.1 thorpej } 319 1.1 thorpej #ifdef OFW_DEBUG 320 1.1 thorpej if (len != 1) 321 1.1 thorpej printf("%x\n", args.actual); 322 1.1 thorpej #endif 323 1.1 thorpej return args.actual; 324 1.1 thorpej } 325 1.1 thorpej 326 1.1 thorpej int 327 1.2 christos OF_read(int handle, void *addr, int len) 328 1.1 thorpej { 329 1.1 thorpej static struct { 330 1.2 christos const char *name; 331 1.1 thorpej int nargs; 332 1.1 thorpej int nreturns; 333 1.1 thorpej int ihandle; 334 1.1 thorpej void *addr; 335 1.1 thorpej int len; 336 1.1 thorpej int actual; 337 1.1 thorpej } args = { 338 1.1 thorpej "read", 339 1.1 thorpej 3, 340 1.1 thorpej 1, 341 1.1 thorpej }; 342 1.1 thorpej 343 1.1 thorpej #ifdef OFW_DEBUG 344 1.1 thorpej if (len != 1) 345 1.1 thorpej printf("OF_read(%d, %x, %x) -> ", handle, addr, len); 346 1.1 thorpej #endif 347 1.1 thorpej args.ihandle = handle; 348 1.1 thorpej args.addr = addr; 349 1.1 thorpej args.len = len; 350 1.1 thorpej if (openfirmware(&args) == -1) { 351 1.1 thorpej #ifdef OFW_DEBUG 352 1.1 thorpej printf("lose\n"); 353 1.1 thorpej #endif 354 1.1 thorpej return -1; 355 1.1 thorpej } 356 1.1 thorpej #ifdef OFW_DEBUG 357 1.1 thorpej if (len != 1) 358 1.1 thorpej printf("%x\n", args.actual); 359 1.1 thorpej #endif 360 1.1 thorpej return args.actual; 361 1.1 thorpej } 362 1.1 thorpej 363 1.1 thorpej int 364 1.2 christos OF_seek(int handle, u_quad_t pos) 365 1.1 thorpej { 366 1.1 thorpej static struct { 367 1.2 christos const char *name; 368 1.1 thorpej int nargs; 369 1.1 thorpej int nreturns; 370 1.1 thorpej int handle; 371 1.1 thorpej int poshi; 372 1.1 thorpej int poslo; 373 1.1 thorpej int status; 374 1.1 thorpej } args = { 375 1.1 thorpej "seek", 376 1.1 thorpej 3, 377 1.1 thorpej 1, 378 1.1 thorpej }; 379 1.1 thorpej 380 1.1 thorpej #ifdef OFW_DEBUG 381 1.1 thorpej printf("OF_seek(%d, %x, %x) -> ", handle, (int)(pos >> 32), (int)pos); 382 1.1 thorpej #endif 383 1.1 thorpej args.handle = handle; 384 1.1 thorpej args.poshi = (int)(pos >> 32); 385 1.1 thorpej args.poslo = (int)pos; 386 1.1 thorpej if (openfirmware(&args) == -1) { 387 1.1 thorpej #ifdef OFW_DEBUG 388 1.1 thorpej printf("lose\n"); 389 1.1 thorpej #endif 390 1.1 thorpej return -1; 391 1.1 thorpej } 392 1.1 thorpej #ifdef OFW_DEBUG 393 1.1 thorpej printf("%d\n", args.status); 394 1.1 thorpej #endif 395 1.1 thorpej return args.status; 396 1.1 thorpej } 397 1.1 thorpej 398 1.1 thorpej void * 399 1.2 christos OF_claim(void *virt, u_int size, u_int align) 400 1.1 thorpej { 401 1.1 thorpej static struct { 402 1.2 christos const char *name; 403 1.1 thorpej int nargs; 404 1.1 thorpej int nreturns; 405 1.1 thorpej void *virt; 406 1.1 thorpej u_int size; 407 1.1 thorpej u_int align; 408 1.1 thorpej void *baseaddr; 409 1.1 thorpej } args = { 410 1.1 thorpej "claim", 411 1.1 thorpej 3, 412 1.1 thorpej 1, 413 1.1 thorpej }; 414 1.1 thorpej 415 1.1 thorpej #ifdef OFW_DEBUG 416 1.1 thorpej printf("OF_claim(%x, %x, %x) -> ", virt, size, align); 417 1.1 thorpej #endif 418 1.1 thorpej args.virt = virt; 419 1.1 thorpej args.size = size; 420 1.1 thorpej args.align = align; 421 1.1 thorpej if (openfirmware(&args) == -1) { 422 1.1 thorpej #ifdef OFW_DEBUG 423 1.1 thorpej printf("lose\n"); 424 1.1 thorpej #endif 425 1.1 thorpej return (void *)-1; 426 1.1 thorpej } 427 1.1 thorpej #ifdef OFW_DEBUG 428 1.1 thorpej printf("%x\n", args.baseaddr); 429 1.1 thorpej #endif 430 1.1 thorpej return args.baseaddr; 431 1.1 thorpej } 432 1.1 thorpej 433 1.1 thorpej void 434 1.2 christos OF_release(void *virt, u_int size) 435 1.1 thorpej { 436 1.1 thorpej static struct { 437 1.2 christos const char *name; 438 1.1 thorpej int nargs; 439 1.1 thorpej int nreturns; 440 1.1 thorpej void *virt; 441 1.1 thorpej u_int size; 442 1.1 thorpej } args = { 443 1.1 thorpej "release", 444 1.1 thorpej 2, 445 1.1 thorpej 0, 446 1.1 thorpej }; 447 1.1 thorpej 448 1.1 thorpej #ifdef OFW_DEBUG 449 1.1 thorpej printf("OF_release(%x, %x)\n", virt, size); 450 1.1 thorpej #endif 451 1.1 thorpej args.virt = virt; 452 1.1 thorpej args.size = size; 453 1.1 thorpej openfirmware(&args); 454 1.1 thorpej } 455 1.1 thorpej 456 1.1 thorpej int 457 1.2 christos OF_milliseconds(void) 458 1.1 thorpej { 459 1.1 thorpej static struct { 460 1.2 christos const char *name; 461 1.1 thorpej int nargs; 462 1.1 thorpej int nreturns; 463 1.1 thorpej int ms; 464 1.1 thorpej } args = { 465 1.1 thorpej "milliseconds", 466 1.1 thorpej 0, 467 1.1 thorpej 1, 468 1.1 thorpej }; 469 1.1 thorpej 470 1.1 thorpej openfirmware(&args); 471 1.1 thorpej return args.ms; 472 1.1 thorpej } 473 1.1 thorpej 474 1.1 thorpej void 475 1.2 christos OF_chain(void *virt, u_int size, void (*entry)(int (*)(void *), void *, u_int), 476 1.2 christos void *arg, u_int len) 477 1.1 thorpej { 478 1.1 thorpej struct { 479 1.2 christos const char *name; 480 1.1 thorpej int nargs; 481 1.1 thorpej int nreturns; 482 1.1 thorpej void *virt; 483 1.1 thorpej u_int size; 484 1.2 christos void (*entry)(int (*)(void *), void *, u_int); 485 1.1 thorpej void *arg; 486 1.1 thorpej u_int len; 487 1.1 thorpej } args; 488 1.1 thorpej 489 1.1 thorpej args.name = "chain"; 490 1.1 thorpej args.nargs = 5; 491 1.1 thorpej args.nreturns = 0; 492 1.1 thorpej args.virt = virt; 493 1.1 thorpej args.size = size; 494 1.1 thorpej args.entry = entry; 495 1.1 thorpej args.arg = arg; 496 1.1 thorpej args.len = len; 497 1.1 thorpej #if 1 498 1.1 thorpej openfirmware(&args); 499 1.1 thorpej #else 500 1.1 thorpej entry(openfirmware_entry, arg, len); 501 1.1 thorpej #endif 502 1.1 thorpej } 503 1.1 thorpej 504 1.1 thorpej static int stdin; 505 1.1 thorpej static int stdout; 506 1.1 thorpej 507 1.1 thorpej static void 508 1.2 christos setup(void) 509 1.1 thorpej { 510 1.1 thorpej u_char buf[sizeof(int)]; 511 1.1 thorpej int chosen; 512 1.1 thorpej 513 1.1 thorpej if ((chosen = OF_finddevice("/chosen")) == -1) 514 1.1 thorpej OF_exit(); 515 1.1 thorpej 516 1.1 thorpej if (OF_getprop(chosen, "stdin", buf, sizeof(buf)) != sizeof(buf)) 517 1.1 thorpej OF_exit(); 518 1.1 thorpej stdin = of_decode_int(buf); 519 1.1 thorpej 520 1.1 thorpej if (OF_getprop(chosen, "stdout", buf, sizeof(buf)) != sizeof(buf)) 521 1.1 thorpej OF_exit(); 522 1.1 thorpej stdout = of_decode_int(buf); 523 1.1 thorpej } 524 1.1 thorpej 525 1.1 thorpej void 526 1.2 christos putchar(int c) 527 1.1 thorpej { 528 1.1 thorpej char ch = c; 529 1.1 thorpej 530 1.1 thorpej if (c == '\n') 531 1.1 thorpej putchar('\r'); 532 1.1 thorpej OF_write(stdout, &ch, 1); 533 1.1 thorpej } 534 1.1 thorpej 535 1.1 thorpej int 536 1.2 christos getchar(void) 537 1.1 thorpej { 538 1.1 thorpej unsigned char ch = '\0'; 539 1.1 thorpej int l; 540 1.1 thorpej 541 1.1 thorpej while ((l = OF_read(stdin, &ch, 1)) != 1) 542 1.1 thorpej if (l != -2 && l != 0) 543 1.1 thorpej return -1; 544 1.1 thorpej return ch; 545 1.1 thorpej } 546