Subversion Repositories HelenOS

Rev

Rev 1919 | Rev 1921 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (C) 2006 Jakub Jermar
  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 sparc64
  30.  * @{
  31.  */
  32. /** @file
  33.  */
  34.  
  35. #include <arch/drivers/kbd.h>
  36. #include <genarch/ofw/ofw_tree.h>
  37. #ifdef CONFIG_Z8530
  38. #include <genarch/kbd/z8530.h>
  39. #endif
  40. #ifdef CONFIG_NS16550
  41. #include <genarch/kbd/ns16550.h>
  42. #endif
  43. #include <ddi/device.h>
  44. #include <ddi/irq.h>
  45. #include <arch/mm/page.h>
  46. #include <arch/types.h>
  47. #include <typedefs.h>
  48. #include <align.h>
  49. #include <func.h>
  50. #include <print.h>
  51.  
  52. volatile uint8_t *kbd_virt_address = NULL;
  53.  
  54. kbd_type_t kbd_type = KBD_UNKNOWN;
  55.  
  56. static irq_t kbd_irq;
  57.  
  58. /** Initialize keyboard.
  59.  *
  60.  * Traverse OpenFirmware device tree in order to find necessary
  61.  * info about the keyboard device.
  62.  *
  63.  * @param node Keyboard device node.
  64.  */
  65. void kbd_init(ofw_tree_node_t *node)
  66. {
  67.     size_t offset;
  68.     uintptr_t aligned_addr;
  69.     ofw_tree_property_t *prop;
  70.     const char *name;
  71.    
  72.     name = ofw_tree_node_name(node);
  73.    
  74.     /*
  75.      * Determine keyboard serial controller type.
  76.      */
  77.     if (strcmp(name, "zs") == 0)
  78.         kbd_type = KBD_Z8530;
  79.     else if (strcmp(name, "su") == 0)
  80.         kbd_type = KBD_NS16550;
  81.    
  82.     if (kbd_type == KBD_UNKNOWN) {
  83.         printf("Unknown keyboard device.\n");
  84.         return;
  85.     }
  86.    
  87.     /*
  88.      * Read 'interrupts' property.
  89.      */
  90.     uint32_t interrupts;
  91.     prop = ofw_tree_getprop(node, "interrupts");
  92.     if (!prop || !prop->value)
  93.         panic("Can't find \"interrupts\" property.\n");
  94.     interrupts = *((uint32_t *) prop->value);
  95.  
  96.     /*
  97.      * Read 'reg' property.
  98.      */
  99.     prop = ofw_tree_getprop(node, "reg");
  100.     if (!prop || !prop->value)
  101.         panic("Can't find \"reg\" property.\n");
  102.    
  103.     uintptr_t pa;
  104.     size_t size;
  105.     int inr;
  106.    
  107.     irq_initialize(&kbd_irq);
  108.    
  109.     switch (kbd_type) {
  110.     case KBD_Z8530:
  111.         size = ((ofw_fhc_reg_t *) prop->value)->size;
  112.         if (!ofw_fhc_apply_ranges(node->parent, ((ofw_fhc_reg_t *) prop->value) , &pa)) {
  113.             printf("Failed to determine keyboard address.\n");
  114.             return;
  115.         }
  116.         if (!ofw_fhc_map_interrupt(node->parent, ((ofw_fhc_reg_t *) prop->value), interrupts, &inr)) {
  117.             printf("Failed to determine keyboard interrupt.\n");
  118.             return;
  119.         } else {
  120.             kbd_irq.inr = inr;
  121.             kbd_irq.devno = device_assign_devno();
  122.             kbd_irq.trigger = IRQ_TRIGGER_LEVEL;
  123.             kbd_irq.claim = z8530_claim;
  124.             kbd_irq.handler = z8530_irq_handler;
  125.             irq_register(&kbd_irq);
  126.         }
  127.         break;
  128.        
  129.     case KBD_NS16550:
  130.         size = ((ofw_ebus_reg_t *) prop->value)->size;
  131.         if (!ofw_ebus_apply_ranges(node->parent, ((ofw_ebus_reg_t *) prop->value) , &pa)) {
  132.             printf("Failed to determine keyboard address.\n");
  133.             return;
  134.         }
  135.         if (!ofw_ebus_map_interrupt(node->parent, ((ofw_ebus_reg_t *) prop->value), interrupts, &inr)) {
  136.             printf("Failed to determine keyboard interrupt.\n");
  137.             return;
  138.         } else {
  139.             kbd_irq.inr = inr;
  140.             kbd_irq.devno = device_assign_devno();
  141.             kbd_irq.trigger = IRQ_TRIGGER_LEVEL;
  142.             kbd_irq.claim = ns16550_claim;
  143.             kbd_irq.handler = ns16550_irq_handler;
  144.             irq_register(&kbd_irq);
  145.         }
  146.         break;
  147.  
  148.     default:
  149.         panic("Unexpected type.\n");
  150.     }
  151.    
  152.     /*
  153.      * We need to pass aligned address to hw_map().
  154.      * However, the physical keyboard address can
  155.      * be pretty much unaligned, depending on the
  156.      * underlying controller.
  157.      */
  158.     aligned_addr = ALIGN_DOWN(pa, PAGE_SIZE);
  159.     offset = pa - aligned_addr;
  160.     kbd_virt_address = (uint8_t *) hw_map(aligned_addr, offset + size) + offset;
  161.  
  162.     switch (kbd_type) {
  163. #ifdef CONFIG_Z8530
  164.     case KBD_Z8530:
  165.         z8530_init();
  166.         break;
  167. #endif
  168. #ifdef CONFIG_NS16550
  169.     case KBD_NS16550:
  170.         ns16550_init();
  171.         break;
  172. #endif
  173.     default:
  174.         printf("Kernel is not compiled with the necessary keyboard driver this machine requires.\n");
  175.     }
  176. }
  177.  
  178. /** @}
  179.  */
  180.