Subversion Repositories HelenOS-historic

Rev

Rev 1780 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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. /** @addtogroup genarch
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <genarch/ofw/ofw.h>
  36. #include <arch/asm.h>
  37. #include <stdarg.h>
  38. #include <cpu.h>
  39. #include <arch/types.h>
  40.  
  41. uintptr_t ofw_cif;  /**< OpenFirmware Client Interface address. */
  42.  
  43. phandle ofw_chosen;
  44. ihandle ofw_stdout;
  45. ihandle ofw_mmu;
  46.  
  47. void ofw_init(void)
  48. {
  49.     ofw_chosen = ofw_find_device("/chosen");
  50.     if (ofw_chosen == -1)
  51.         ofw_done();
  52.    
  53.     if (ofw_get_property(ofw_chosen, "stdout",  &ofw_stdout, sizeof(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. }
  59.  
  60. void ofw_done(void)
  61. {
  62.     (void) ofw_call("exit", 0, 1, NULL);
  63.     cpu_halt();
  64. }
  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.  */
  75. ofw_arg_t ofw_call(const char *service, const int nargs, const int nret, ofw_arg_t *rets, ...)
  76. {
  77.     va_list list;
  78.     ofw_args_t args;
  79.     int i;
  80.    
  81.     args.service = service;
  82.     args.nargs = nargs;
  83.     args.nret = nret;
  84.    
  85.     va_start(list, rets);
  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.    
  93.     (void) ofw(&args);
  94.  
  95.     for (i = 1; i < nret; i++)
  96.         rets[i - 1] = args.args[i + nargs];
  97.  
  98.     return args.args[nargs];
  99. }
  100.  
  101. void ofw_putchar(const char ch)
  102. {
  103.     if (ofw_stdout == 0)
  104.         return;
  105.    
  106.     (void) ofw_call("write", 3, 1, NULL, ofw_stdout, &ch, 1);
  107. }
  108.  
  109. phandle ofw_find_device(const char *name)
  110. {
  111.     return (phandle) ofw_call("finddevice", 1, 1, NULL, name);
  112. }
  113.  
  114. int ofw_get_property(const phandle device, const char *name, const void *buf, const int buflen)
  115. {
  116.     return (int) ofw_call("getprop", 4, 1, NULL, device, name, buf, buflen);
  117. }
  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.
  124.  * @return NULL on failure or physical address on success.
  125.  */
  126. void *ofw_translate(const void *virt)
  127. {
  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]);
  146. }
  147.  
  148. void *ofw_claim(const void *addr, const int size, const int align)
  149. {
  150.     return (void *) ofw_call("claim", 3, 1, NULL, addr, size, align);
  151. }
  152.  
  153. /** @}
  154.  */
  155.