Rev 1780 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 186 | 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 | |||
| 1777 | jermar | 29 | /** @addtogroup genarch |
| 1702 | cejka | 30 | * @{ |
| 31 | */ |
||
| 32 | /** @file |
||
| 33 | */ |
||
| 34 | |||
| 493 | jermar | 35 | #include <genarch/ofw/ofw.h> |
| 254 | decky | 36 | #include <arch/asm.h> |
| 186 | decky | 37 | #include <stdarg.h> |
| 201 | decky | 38 | #include <cpu.h> |
| 421 | jermar | 39 | #include <arch/types.h> |
| 186 | decky | 40 | |
| 1784 | jermar | 41 | uintptr_t ofw_cif; /**< OpenFirmware Client Interface address. */ |
| 186 | decky | 42 | |
| 191 | decky | 43 | phandle ofw_chosen; |
| 44 | ihandle ofw_stdout; |
||
| 1784 | jermar | 45 | ihandle ofw_mmu; |
| 191 | decky | 46 | |
| 186 | decky | 47 | void ofw_init(void) |
| 48 | { |
||
| 191 | decky | 49 | ofw_chosen = ofw_find_device("/chosen"); |
| 186 | decky | 50 | if (ofw_chosen == -1) |
| 51 | ofw_done(); |
||
| 52 | |||
| 210 | decky | 53 | if (ofw_get_property(ofw_chosen, "stdout", &ofw_stdout, sizeof(ofw_stdout)) <= 0) |
| 669 | jermar | 54 | ofw_stdout = 0; |
| 1784 | jermar | 55 | |
| 56 | if (ofw_get_property(ofw_chosen, "mmu", &ofw_mmu, sizeof(ofw_mmu)) <= 0) |
||
| 57 | ofw_mmu = 0; |
||
| 186 | decky | 58 | } |
| 59 | |||
| 60 | void ofw_done(void) |
||
| 61 | { |
||
| 1784 | jermar | 62 | (void) ofw_call("exit", 0, 1, NULL); |
| 186 | decky | 63 | cpu_halt(); |
| 64 | } |
||
| 65 | |||
| 1784 | jermar | 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 | */ |
||
| 75 | ofw_arg_t ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...) |
||
| 186 | decky | 76 | { |
| 77 | va_list list; |
||
| 191 | decky | 78 | ofw_args_t args; |
| 186 | decky | 79 | int i; |
| 80 | |||
| 81 | args.service = service; |
||
| 82 | args.nargs = nargs; |
||
| 83 | args.nret = nret; |
||
| 84 | |||
| 1784 | jermar | 85 | va_start(list, rets); |
| 186 | decky | 86 | for (i = 0; i < nargs; i++) |
| 87 | args.args[i] = va_arg(list, ofw_arg_t); |
||
| 88 | va_end(list); |
||
| 89 | |||
| 90 | for (i = 0; i < nret; i++) |
||
| 91 | args.args[i + nargs] = 0; |
||
| 92 | |||
| 1784 | jermar | 93 | (void) ofw(&args); |
| 94 | |||
| 95 | for (i = 1; i < nret; i++) |
||
| 96 | rets[i - 1] = args.args[i + nargs]; |
||
| 97 | |||
| 186 | decky | 98 | return args.args[nargs]; |
| 99 | } |
||
| 100 | |||
| 101 | void ofw_putchar(const char ch) |
||
| 102 | { |
||
| 103 | if (ofw_stdout == 0) |
||
| 104 | return; |
||
| 105 | |||
| 1784 | jermar | 106 | (void) ofw_call("write", 3, 1, NULL, ofw_stdout, &ch, 1); |
| 186 | decky | 107 | } |
| 108 | |||
| 1784 | jermar | 109 | phandle ofw_find_device(const char *name) |
| 669 | jermar | 110 | { |
| 1784 | jermar | 111 | return (phandle) ofw_call("finddevice", 1, 1, NULL, name); |
| 669 | jermar | 112 | } |
| 113 | |||
| 1784 | jermar | 114 | int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen) |
| 191 | decky | 115 | { |
| 1784 | jermar | 116 | return (int) ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen); |
| 191 | decky | 117 | } |
| 118 | |||
| 1784 | jermar | 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. |
||
| 124 | * @return NULL on failure or physical address on success. |
||
| 125 | */ |
||
| 126 | void *ofw_translate(const void *virt) |
||
| 191 | decky | 127 | { |
| 1784 | jermar | 128 | ofw_arg_t result[4]; |
| 129 | int shift; |
||
| 130 | |||
| 131 | if (!ofw_mmu) |
||
| 132 | return NULL; |
||
| 133 | |||
| 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]); |
||
| 191 | decky | 146 | } |
| 147 | |||
| 207 | decky | 148 | void *ofw_claim(const void *addr, const int size, const int align) |
| 149 | { |
||
| 1784 | jermar | 150 | return (void *) ofw_call("claim", 3, 1, NULL, addr, size, align); |
| 207 | decky | 151 | } |
| 1702 | cejka | 152 | |
| 1777 | jermar | 153 | /** @} |
| 1702 | cejka | 154 | */ |