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/fhc.h>
|
---|
38 | #include <genarch/ofw/ebus.h>
|
---|
39 | #include <console/console.h>
|
---|
40 | #include <ddi/irq.h>
|
---|
41 | #include <mm/page.h>
|
---|
42 | #include <arch/mm/page.h>
|
---|
43 | #include <arch/types.h>
|
---|
44 | #include <align.h>
|
---|
45 | #include <string.h>
|
---|
46 | #include <print.h>
|
---|
47 | #include <sysinfo/sysinfo.h>
|
---|
48 |
|
---|
49 | #ifdef CONFIG_SUN_KBD
|
---|
50 | #include <genarch/kbrd/kbrd.h>
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #ifdef CONFIG_Z8530
|
---|
54 | #include <genarch/drivers/z8530/z8530.h>
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #ifdef CONFIG_NS16550
|
---|
58 | #include <genarch/drivers/ns16550/ns16550.h>
|
---|
59 | #endif
|
---|
60 |
|
---|
61 | #ifdef CONFIG_SUN_KBD
|
---|
62 |
|
---|
63 | #ifdef CONFIG_Z8530
|
---|
64 |
|
---|
65 | static bool kbd_z8530_init(ofw_tree_node_t *node)
|
---|
66 | {
|
---|
67 | const char *name = ofw_tree_node_name(node);
|
---|
68 |
|
---|
69 | if (str_cmp(name, "zs") != 0)
|
---|
70 | return false;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Read 'interrupts' property.
|
---|
74 | */
|
---|
75 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "interrupts");
|
---|
76 | if ((!prop) || (!prop->value)) {
|
---|
77 | printf("z8530: Unable to find interrupts property\n");
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | uint32_t interrupts = *((uint32_t *) prop->value);
|
---|
82 |
|
---|
83 | /*
|
---|
84 | * Read 'reg' property.
|
---|
85 | */
|
---|
86 | prop = ofw_tree_getprop(node, "reg");
|
---|
87 | if ((!prop) || (!prop->value)) {
|
---|
88 | printf("z8530: Unable to find reg property\n");
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 | size_t size = ((ofw_fhc_reg_t *) prop->value)->size;
|
---|
93 |
|
---|
94 | uintptr_t pa;
|
---|
95 | if (!ofw_fhc_apply_ranges(node->parent,
|
---|
96 | ((ofw_fhc_reg_t *) prop->value), &pa)) {
|
---|
97 | printf("z8530: Failed to determine address\n");
|
---|
98 | return false;
|
---|
99 | }
|
---|
100 |
|
---|
101 | inr_t inr;
|
---|
102 | cir_t cir;
|
---|
103 | void *cir_arg;
|
---|
104 | if (!ofw_fhc_map_interrupt(node->parent,
|
---|
105 | ((ofw_fhc_reg_t *) prop->value), interrupts, &inr, &cir,
|
---|
106 | &cir_arg)) {
|
---|
107 | printf("z8530: Failed to determine interrupt\n");
|
---|
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 | z8530_t *z8530 = (z8530_t *)
|
---|
121 | (hw_map(aligned_addr, offset + size) + offset);
|
---|
122 |
|
---|
123 | z8530_instance_t *z8530_instance = z8530_init(z8530, inr, cir, cir_arg);
|
---|
124 | if (z8530_instance) {
|
---|
125 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
---|
126 | if (kbrd_instance) {
|
---|
127 | indev_t *sink = stdin_wire();
|
---|
128 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
---|
129 | z8530_wire(z8530_instance, kbrd);
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | /*
|
---|
134 | * This is the necessary evil until the userspace drivers are
|
---|
135 | * entirely self-sufficient.
|
---|
136 | */
|
---|
137 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
138 | sysinfo_set_item_val("kbd.inr", NULL, inr);
|
---|
139 | sysinfo_set_item_val("kbd.address.kernel", NULL,
|
---|
140 | (uintptr_t) z8530);
|
---|
141 | sysinfo_set_item_val("kbd.address.physical", NULL, pa);
|
---|
142 | sysinfo_set_item_val("kbd.type.z8530", NULL, true);
|
---|
143 |
|
---|
144 | return true;
|
---|
145 | }
|
---|
146 |
|
---|
147 | #endif /* CONFIG_Z8530 */
|
---|
148 |
|
---|
149 | #ifdef CONFIG_NS16550
|
---|
150 |
|
---|
151 | static bool kbd_ns16550_init(ofw_tree_node_t *node)
|
---|
152 | {
|
---|
153 | const char *name = ofw_tree_node_name(node);
|
---|
154 |
|
---|
155 | if (str_cmp(name, "su") != 0)
|
---|
156 | return false;
|
---|
157 |
|
---|
158 | /*
|
---|
159 | * Read 'interrupts' property.
|
---|
160 | */
|
---|
161 | ofw_tree_property_t *prop = ofw_tree_getprop(node, "interrupts");
|
---|
162 | if ((!prop) || (!prop->value)) {
|
---|
163 | printf("ns16550: Unable to find interrupts property\n");
|
---|
164 | return false;
|
---|
165 | }
|
---|
166 |
|
---|
167 | uint32_t interrupts = *((uint32_t *) prop->value);
|
---|
168 |
|
---|
169 | /*
|
---|
170 | * Read 'reg' property.
|
---|
171 | */
|
---|
172 | prop = ofw_tree_getprop(node, "reg");
|
---|
173 | if ((!prop) || (!prop->value)) {
|
---|
174 | printf("ns16550: Unable to find reg property\n");
|
---|
175 | return false;
|
---|
176 | }
|
---|
177 |
|
---|
178 | size_t size = ((ofw_ebus_reg_t *) prop->value)->size;
|
---|
179 |
|
---|
180 | uintptr_t pa;
|
---|
181 | if (!ofw_ebus_apply_ranges(node->parent,
|
---|
182 | ((ofw_ebus_reg_t *) prop->value), &pa)) {
|
---|
183 | printf("ns16550: Failed to determine address\n");
|
---|
184 | return false;
|
---|
185 | }
|
---|
186 |
|
---|
187 | inr_t inr;
|
---|
188 | cir_t cir;
|
---|
189 | void *cir_arg;
|
---|
190 | if (!ofw_ebus_map_interrupt(node->parent,
|
---|
191 | ((ofw_ebus_reg_t *) prop->value), interrupts, &inr, &cir,
|
---|
192 | &cir_arg)) {
|
---|
193 | printf("ns16550: Failed to determine interrupt\n");
|
---|
194 | return false;
|
---|
195 | }
|
---|
196 |
|
---|
197 | /*
|
---|
198 | * We need to pass aligned address to hw_map().
|
---|
199 | * However, the physical keyboard address can
|
---|
200 | * be pretty much unaligned, depending on the
|
---|
201 | * underlying controller.
|
---|
202 | */
|
---|
203 | uintptr_t aligned_addr = ALIGN_DOWN(pa, PAGE_SIZE);
|
---|
204 | size_t offset = pa - aligned_addr;
|
---|
205 |
|
---|
206 | ns16550_t *ns16550 = (ns16550_t *)
|
---|
207 | (hw_map(aligned_addr, offset + size) + offset);
|
---|
208 |
|
---|
209 | ns16550_instance_t *ns16550_instance = ns16550_init(ns16550, inr, cir, cir_arg);
|
---|
210 | if (ns16550_instance) {
|
---|
211 | kbrd_instance_t *kbrd_instance = kbrd_init();
|
---|
212 | if (kbrd_instance) {
|
---|
213 | indev_t *sink = stdin_wire();
|
---|
214 | indev_t *kbrd = kbrd_wire(kbrd_instance, sink);
|
---|
215 | ns16550_wire(ns16550_instance, kbrd);
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /*
|
---|
220 | * This is the necessary evil until the userspace drivers are
|
---|
221 | * entirely self-sufficient.
|
---|
222 | */
|
---|
223 | sysinfo_set_item_val("kbd", NULL, true);
|
---|
224 | sysinfo_set_item_val("kbd.inr", NULL, inr);
|
---|
225 | sysinfo_set_item_val("kbd.address.kernel", NULL,
|
---|
226 | (uintptr_t) ns16550);
|
---|
227 | sysinfo_set_item_val("kbd.address.physical", NULL, pa);
|
---|
228 | sysinfo_set_item_val("kbd.type.ns16550", NULL, true);
|
---|
229 |
|
---|
230 | return true;
|
---|
231 | }
|
---|
232 |
|
---|
233 | #endif /* CONFIG_NS16550 */
|
---|
234 |
|
---|
235 | /** Initialize keyboard.
|
---|
236 | *
|
---|
237 | * Traverse OpenFirmware device tree in order to find necessary
|
---|
238 | * info about the keyboard device.
|
---|
239 | *
|
---|
240 | * @param node Keyboard device node.
|
---|
241 | *
|
---|
242 | */
|
---|
243 | void kbd_init(ofw_tree_node_t *node)
|
---|
244 | {
|
---|
245 | #ifdef CONFIG_Z8530
|
---|
246 | kbd_z8530_init(node);
|
---|
247 | #endif
|
---|
248 |
|
---|
249 | #ifdef CONFIG_NS16550
|
---|
250 | kbd_ns16550_init(node);
|
---|
251 | #endif
|
---|
252 | }
|
---|
253 |
|
---|
254 | #endif /* CONFIG_SUN_KBD */
|
---|
255 |
|
---|
256 | /** @}
|
---|
257 | */
|
---|