Rev 3925 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3924 | svoboda | 1 | /* |
2 | * Copyright (c) 2008 Pavel Rimsky |
||
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 kbd_port |
||
30 | * @ingroup kbd |
||
31 | * @{ |
||
32 | */ |
||
33 | /** @file |
||
34 | * @brief Z8350 keyboard port driver. |
||
35 | * @brief SGCN (Serengeti Console) keyboard port driver. |
||
36 | */ |
||
37 | |||
38 | #include <as.h> |
||
39 | #include <ddi.h> |
||
40 | #include <ipc/ipc.h> |
||
41 | #include <async.h> |
||
42 | #include <kbd.h> |
||
43 | #include <kbd_port.h> |
||
44 | #include <sysinfo.h> |
||
45 | #include <stdio.h> |
||
46 | |||
47 | /** |
||
48 | * SGCN buffer header. It is placed at the very beginning of the SGCN |
||
49 | * buffer. |
||
50 | */ |
||
51 | typedef struct { |
||
52 | /** hard-wired to "CON" */ |
||
53 | char magic[4]; |
||
54 | |||
55 | /** we don't need this */ |
||
56 | char unused[8]; |
||
57 | |||
58 | /** offset within the SGCN buffer of the input buffer start */ |
||
59 | uint32_t in_begin; |
||
60 | |||
61 | /** offset within the SGCN buffer of the input buffer end */ |
||
62 | uint32_t in_end; |
||
63 | |||
64 | /** offset within the SGCN buffer of the input buffer read pointer */ |
||
65 | uint32_t in_rdptr; |
||
66 | |||
67 | /** offset within the SGCN buffer of the input buffer write pointer */ |
||
68 | uint32_t in_wrptr; |
||
69 | } __attribute__ ((packed)) sgcn_buffer_header_t; |
||
70 | |||
71 | /* |
||
72 | * Returns a pointer to the object of a given type which is placed at the given |
||
73 | * offset from the console buffer beginning. |
||
74 | */ |
||
75 | #define SGCN_BUFFER(type, offset) \ |
||
76 | ((type *) (sram_virt_addr + sram_buffer_offset + (offset))) |
||
77 | |||
78 | /** Returns a pointer to the console buffer header. */ |
||
79 | #define SGCN_BUFFER_HEADER (SGCN_BUFFER(sgcn_buffer_header_t, 0)) |
||
80 | |||
81 | /** |
||
82 | * Virtual address mapped to SRAM. |
||
83 | */ |
||
84 | static uintptr_t sram_virt_addr; |
||
85 | |||
86 | /** |
||
87 | * SGCN buffer offset within SGCN. |
||
88 | */ |
||
89 | static uintptr_t sram_buffer_offset; |
||
90 | |||
91 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call); |
||
92 | |||
93 | |||
94 | /** |
||
95 | * Initializes the SGCN driver. |
||
96 | * Maps the physical memory (SRAM) and registers the interrupt. |
||
97 | */ |
||
98 | int kbd_port_init(void) |
||
99 | { |
||
100 | async_set_interrupt_received(sgcn_irq_handler); |
||
101 | sram_virt_addr = (uintptr_t) as_get_mappable_page(sysinfo_value("sram.area.size")); |
||
102 | if (physmem_map((void *) sysinfo_value("sram.address.physical"), |
||
103 | (void *) sram_virt_addr, sysinfo_value("sram.area.size") / PAGE_SIZE, |
||
104 | AS_AREA_READ | AS_AREA_WRITE) != 0) { |
||
105 | printf("SGCN: uspace driver could not map physical memory."); |
||
106 | return -1; |
||
107 | } |
||
108 | |||
109 | sram_buffer_offset = sysinfo_value("sram.buffer.offset"); |
||
110 | ipc_register_irq(sysinfo_value("kbd.inr"), sysinfo_value("kbd.devno"), |
||
111 | 0, (void *) 0); |
||
112 | return 0; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Handler of the "key pressed" event. Reads codes of all the pressed keys from |
||
117 | * the buffer. |
||
118 | */ |
||
119 | static void sgcn_irq_handler(ipc_callid_t iid, ipc_call_t *call) |
||
120 | { |
||
121 | char c; |
||
122 | |||
123 | uint32_t begin = SGCN_BUFFER_HEADER->in_begin; |
||
124 | uint32_t end = SGCN_BUFFER_HEADER->in_end; |
||
125 | uint32_t size = end - begin; |
||
126 | |||
127 | volatile char *buf_ptr = (volatile char *) |
||
128 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
||
129 | volatile uint32_t *in_wrptr_ptr = &(SGCN_BUFFER_HEADER->in_wrptr); |
||
130 | volatile uint32_t *in_rdptr_ptr = &(SGCN_BUFFER_HEADER->in_rdptr); |
||
131 | |||
132 | while (*in_rdptr_ptr != *in_wrptr_ptr) { |
||
133 | c = *buf_ptr; |
||
134 | *in_rdptr_ptr = (((*in_rdptr_ptr) - begin + 1) % size) + begin; |
||
135 | buf_ptr = (volatile char *) |
||
136 | SGCN_BUFFER(char, SGCN_BUFFER_HEADER->in_rdptr); |
||
137 | kbd_push_scancode(c); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** @} |
||
142 | */ |