| 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 | #include <genarch/ofw/ebus.h>
|
|---|
| 38 | #include <console/console.h>
|
|---|
| 39 | #include <ddi/irq.h>
|
|---|
| 40 | #include <mm/page.h>
|
|---|
| 41 | #include <arch/mm/page.h>
|
|---|
| 42 | #include <mm/km.h>
|
|---|
| 43 | #include <typedefs.h>
|
|---|
| 44 | #include <align.h>
|
|---|
| 45 | #include <str.h>
|
|---|
| 46 | #include <log.h>
|
|---|
| 47 | #include <sysinfo/sysinfo.h>
|
|---|
| 48 |
|
|---|
| 49 | #ifdef CONFIG_SUN_KBD
|
|---|
| 50 | #include <genarch/kbrd/kbrd.h>
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | #ifdef CONFIG_NS16550
|
|---|
| 54 | #include <genarch/drivers/ns16550/ns16550.h>
|
|---|
| 55 | #endif
|
|---|
| 56 |
|
|---|
| 57 | #ifdef CONFIG_SUN_KBD
|
|---|
| 58 |
|
|---|
| 59 | #ifdef CONFIG_NS16550
|
|---|
| 60 |
|
|---|
| 61 | static bool kbd_ns16550_init(ofw_tree_node_t *node)
|
|---|
| 62 | {
|
|---|
| 63 | const char *name = ofw_tree_node_name(node);
|
|---|
| 64 |
|
|---|
| 65 | if (str_cmp(name, "su") != 0)
|
|---|
| 66 | return false;
|
|---|
| 67 |
|
|---|
| 68 | /*
|
|---|
| 69 | * Read 'interrupts' property.
|
|---|
| 70 | */
|
|---|
| 71 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "interrupts");
|
|---|
| 72 | if ((!prop) || (!prop->value)) {
|
|---|
| 73 | log(LF_ARCH, LVL_ERROR,
|
|---|
| 74 | "ns16550: Unable to find interrupts property");
|
|---|
| 75 | return false;
|
|---|
| 76 | }
|
|---|
| 77 |
|
|---|
| 78 | uint32_t interrupts = *((uint32_t *) prop->value);
|
|---|
| 79 |
|
|---|
| 80 | /*
|
|---|
| 81 | * Read 'reg' property.
|
|---|
| 82 | */
|
|---|
| 83 | prop = ofw_tree_getprop(node, "reg");
|
|---|
| 84 | if ((!prop) || (!prop->value)) {
|
|---|
| 85 | log(LF_ARCH, LVL_ERROR,
|
|---|
| 86 | "ns16550: Unable to find reg property");
|
|---|
| 87 | return false;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | size_t size = ((ofw_ebus_reg_t *) prop->value)->size;
|
|---|
| 91 |
|
|---|
| 92 | uintptr_t pa;
|
|---|
| 93 | if (!ofw_ebus_apply_ranges(node->parent,
|
|---|
| 94 | ((ofw_ebus_reg_t *) prop->value), &pa)) {
|
|---|
| 95 | log(LF_ARCH, LVL_ERROR,
|
|---|
| 96 | "ns16550: Failed to determine address");
|
|---|
| 97 | return false;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | inr_t inr;
|
|---|
| 101 | cir_t cir;
|
|---|
| 102 | void *cir_arg;
|
|---|
| 103 | if (!ofw_ebus_map_interrupt(node->parent,
|
|---|
| 104 | ((ofw_ebus_reg_t *) prop->value), interrupts, &inr, &cir,
|
|---|
| 105 | &cir_arg)) {
|
|---|
| 106 | log(LF_ARCH, LVL_ERROR,
|
|---|
| 107 | "ns16550: Failed to determine interrupt");
|
|---|
| 108 | return false;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /*
|
|---|
| 112 | * We need to pass aligned address to hw_map().
|
|---|
| 113 | * However, the physical keyboard address can
|
|---|
| 114 | * be pretty much unaligned, depending on the
|
|---|
| 115 | * underlying controller.
|
|---|
| 116 | */
|
|---|
| 117 | uintptr_t aligned_addr = ALIGN_DOWN(pa, PAGE_SIZE);
|
|---|
| 118 | size_t offset = pa - aligned_addr;
|
|---|
| 119 |
|
|---|
| 120 | ns16550_t *ns16550 = (ns16550_t *) (km_map(aligned_addr, offset + size,
|
|---|
| 121 | PAGE_WRITE | PAGE_NOT_CACHEABLE) + offset);
|
|---|
| 122 |
|
|---|
| 123 | ns16550_instance_t *ns16550_instance = ns16550_init(ns16550, inr, cir,
|
|---|
| 124 | cir_arg, NULL);
|
|---|
| 125 | if (ns16550_instance) {
|
|---|
| 126 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
|---|
| 127 | if (kbrd_instance) {
|
|---|
| 128 | indev_t *sink = stdin_wire();
|
|---|
| 129 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
|---|
| 130 | ns16550_wire(ns16550_instance, kbrd);
|
|---|
| 131 | }
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | /*
|
|---|
| 135 | * This is the necessary evil until the userspace drivers are
|
|---|
| 136 | * entirely self-sufficient.
|
|---|
| 137 | */
|
|---|
| 138 | sysinfo_set_item_val("kbd", NULL, true);
|
|---|
| 139 | sysinfo_set_item_val("kbd.inr", NULL, inr);
|
|---|
| 140 | sysinfo_set_item_val("kbd.address.physical", NULL, pa);
|
|---|
| 141 | sysinfo_set_item_val("kbd.type.ns16550", NULL, true);
|
|---|
| 142 |
|
|---|
| 143 | return true;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 146 | #endif /* CONFIG_NS16550 */
|
|---|
| 147 |
|
|---|
| 148 | /** Initialize keyboard.
|
|---|
| 149 | *
|
|---|
| 150 | * Traverse OpenFirmware device tree in order to find necessary
|
|---|
| 151 | * info about the keyboard device.
|
|---|
| 152 | *
|
|---|
| 153 | * @param node Keyboard device node.
|
|---|
| 154 | *
|
|---|
| 155 | */
|
|---|
| 156 | void kbd_init(ofw_tree_node_t *node)
|
|---|
| 157 | {
|
|---|
| 158 | #ifdef CONFIG_NS16550
|
|---|
| 159 | kbd_ns16550_init(node);
|
|---|
| 160 | #endif
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | #endif /* CONFIG_SUN_KBD */
|
|---|
| 164 |
|
|---|
| 165 | /** @}
|
|---|
| 166 | */
|
|---|