Subversion Repositories HelenOS

Rev

Rev 3022 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3022 Rev 4055
1
/*
1
/*
2
 * Copyright (c) 2006 Martin Decky
2
 * Copyright (c) 2006 Martin Decky
3
 * All rights reserved.
3
 * All rights reserved.
4
 *
4
 *
5
 * Redistribution and use in source and binary forms, with or without
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
6
 * modification, are permitted provided that the following conditions
7
 * are met:
7
 * are met:
8
 *
8
 *
9
 * - Redistributions of source code must retain the above copyright
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
10
 *   notice, this list of conditions and the following disclaimer.
11
 * - Redistributions in binary form must reproduce the above copyright
11
 * - Redistributions in binary form must reproduce the above copyright
12
 *   notice, this list of conditions and the following disclaimer in the
12
 *   notice, this list of conditions and the following disclaimer in the
13
 *   documentation and/or other materials provided with the distribution.
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
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.
15
 *   derived from this software without specific prior written permission.
16
 *
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
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
18
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
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
23
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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
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.
26
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
 */
27
 */
28
 
28
 
29
/** @addtogroup ppc32  
29
/** @addtogroup ppc32  
30
 * @{
30
 * @{
31
 */
31
 */
32
/** @file
32
/** @file
33
 */
33
 */
34
 
34
 
35
#include <arch/drivers/cuda.h>
35
#include <arch/drivers/cuda.h>
36
#include <ipc/irq.h>
-
 
37
#include <arch/asm.h>
36
#include <arch/asm.h>
38
#include <console/console.h>
37
#include <console/console.h>
39
#include <console/chardev.h>
38
#include <console/chardev.h>
40
#include <arch/drivers/pic.h>
39
#include <arch/drivers/pic.h>
41
#include <sysinfo/sysinfo.h>
40
#include <sysinfo/sysinfo.h>
42
#include <interrupt.h>
41
#include <interrupt.h>
43
#include <stdarg.h>
42
#include <stdarg.h>
44
 
43
 
45
#define CUDA_IRQ 10
44
#define CUDA_IRQ 10
46
#define SPECIAL     '?'
45
#define SPECIAL     '?'
47
 
46
 
48
#define PACKET_ADB  0x00
47
#define PACKET_ADB  0x00
49
#define PACKET_CUDA 0x01
48
#define PACKET_CUDA 0x01
50
 
49
 
51
#define CUDA_POWERDOWN  0x0a
50
#define CUDA_POWERDOWN  0x0a
52
#define CUDA_RESET      0x11
51
#define CUDA_RESET      0x11
53
 
52
 
54
#define RS 0x200
53
#define RS 0x200
55
#define B (0 * RS)
54
#define B (0 * RS)
56
#define A (1 * RS)
55
#define A (1 * RS)
57
#define SR (10 * RS)
56
#define SR (10 * RS)
58
#define ACR (11 * RS)
57
#define ACR (11 * RS)
59
 
58
 
60
#define SR_OUT 0x10
59
#define SR_OUT 0x10
61
#define TACK 0x10
60
#define TACK 0x10
62
#define TIP 0x20
61
#define TIP 0x20
63
 
62
 
64
 
63
 
65
static volatile uint8_t *cuda = NULL;
64
static volatile uint8_t *cuda = NULL;
66
static irq_t cuda_irq;      /**< Cuda's IRQ. */
65
static irq_t cuda_irq;      /**< Cuda's IRQ. */
67
 
66
 
68
static char lchars[0x80] = {
67
static char lchars[0x80] = {
69
    'a',
68
    'a',
70
    's',
69
    's',
71
    'd',
70
    'd',
72
    'f',
71
    'f',
73
    'h',
72
    'h',
74
    'g',
73
    'g',
75
    'z',
74
    'z',
76
    'x',
75
    'x',
77
    'c',
76
    'c',
78
    'v',
77
    'v',
79
    SPECIAL,
78
    SPECIAL,
80
    'b',
79
    'b',
81
    'q',
80
    'q',
82
    'w',
81
    'w',
83
    'e',
82
    'e',
84
    'r',
83
    'r',
85
    'y',
84
    'y',
86
    't',
85
    't',
87
    '1',
86
    '1',
88
    '2',
87
    '2',
89
    '3',
88
    '3',
90
    '4',
89
    '4',
91
    '6',
90
    '6',
92
    '5',
91
    '5',
93
    '=',
92
    '=',
94
    '9',
93
    '9',
95
    '7',
94
    '7',
96
    '-',
95
    '-',
97
    '8',
96
    '8',
98
    '0',
97
    '0',
99
    ']',
98
    ']',
100
    'o',
99
    'o',
101
    'u',
100
    'u',
102
    '[',
101
    '[',
103
    'i',
102
    'i',
104
    'p',
103
    'p',
105
    '\n',    /* Enter */
104
    '\n',    /* Enter */
106
    'l',
105
    'l',
107
    'j',
106
    'j',
108
    '\'',
107
    '\'',
109
    'k',
108
    'k',
110
    ';',
109
    ';',
111
    '\\',
110
    '\\',
112
    ',',
111
    ',',
113
    '/',
112
    '/',
114
    'n',
113
    'n',
115
    'm',
114
    'm',
116
    '.',
115
    '.',
117
    '\t',    /* Tab */
116
    '\t',    /* Tab */
118
    ' ',
117
    ' ',
119
    '`',
118
    '`',
120
    '\b',    /* Backspace */
119
    '\b',    /* Backspace */
121
    SPECIAL,
120
    SPECIAL,
122
    SPECIAL, /* Escape */
121
    SPECIAL, /* Escape */
123
    SPECIAL, /* Ctrl */
122
    SPECIAL, /* Ctrl */
124
    SPECIAL, /* Alt */
123
    SPECIAL, /* Alt */
125
    SPECIAL, /* Shift */
124
    SPECIAL, /* Shift */
126
    SPECIAL, /* Caps-Lock */
125
    SPECIAL, /* Caps-Lock */
127
    SPECIAL, /* RAlt */
126
    SPECIAL, /* RAlt */
128
    SPECIAL, /* Left */
127
    SPECIAL, /* Left */
129
    SPECIAL, /* Right */
128
    SPECIAL, /* Right */
130
    SPECIAL, /* Down */
129
    SPECIAL, /* Down */
131
    SPECIAL, /* Up */
130
    SPECIAL, /* Up */
132
    SPECIAL,
131
    SPECIAL,
133
    SPECIAL,
132
    SPECIAL,
134
    '.',     /* Keypad . */
133
    '.',     /* Keypad . */
135
    SPECIAL,
134
    SPECIAL,
136
    '*',     /* Keypad * */
135
    '*',     /* Keypad * */
137
    SPECIAL,
136
    SPECIAL,
138
    '+',     /* Keypad + */
137
    '+',     /* Keypad + */
139
    SPECIAL,
138
    SPECIAL,
140
    SPECIAL, /* NumLock */
139
    SPECIAL, /* NumLock */
141
    SPECIAL,
140
    SPECIAL,
142
    SPECIAL,
141
    SPECIAL,
143
    SPECIAL,
142
    SPECIAL,
144
    '/',     /* Keypad / */
143
    '/',     /* Keypad / */
145
    '\n',    /* Keypad Enter */
144
    '\n',    /* Keypad Enter */
146
    SPECIAL,
145
    SPECIAL,
147
    '-',     /* Keypad - */
146
    '-',     /* Keypad - */
148
    SPECIAL,
147
    SPECIAL,
149
    SPECIAL,
148
    SPECIAL,
150
    SPECIAL,
149
    SPECIAL,
151
    '0',     /* Keypad 0 */
150
    '0',     /* Keypad 0 */
152
    '1',     /* Keypad 1 */
151
    '1',     /* Keypad 1 */
153
    '2',     /* Keypad 2 */
152
    '2',     /* Keypad 2 */
154
    '3',     /* Keypad 3 */
153
    '3',     /* Keypad 3 */
155
    '4',     /* Keypad 4 */
154
    '4',     /* Keypad 4 */
156
    '5',     /* Keypad 5 */
155
    '5',     /* Keypad 5 */
157
    '6',     /* Keypad 6 */
156
    '6',     /* Keypad 6 */
158
    '7',     /* Keypad 7 */
157
    '7',     /* Keypad 7 */
159
    SPECIAL,
158
    SPECIAL,
160
    '8',     /* Keypad 8 */
159
    '8',     /* Keypad 8 */
161
    '9',     /* Keypad 9 */
160
    '9',     /* Keypad 9 */
162
    SPECIAL,
161
    SPECIAL,
163
    SPECIAL,
162
    SPECIAL,
164
    SPECIAL,
163
    SPECIAL,
165
    SPECIAL, /* F5 */
164
    SPECIAL, /* F5 */
166
    SPECIAL, /* F6 */
165
    SPECIAL, /* F6 */
167
    SPECIAL, /* F7 */
166
    SPECIAL, /* F7 */
168
    SPECIAL, /* F3 */
167
    SPECIAL, /* F3 */
169
    SPECIAL, /* F8 */
168
    SPECIAL, /* F8 */
170
    SPECIAL, /* F9 */
169
    SPECIAL, /* F9 */
171
    SPECIAL,
170
    SPECIAL,
172
    SPECIAL, /* F11 */
171
    SPECIAL, /* F11 */
173
    SPECIAL,
172
    SPECIAL,
174
    SPECIAL, /* F13 */
173
    SPECIAL, /* F13 */
175
    SPECIAL,
174
    SPECIAL,
176
    SPECIAL, /* ScrollLock */
175
    SPECIAL, /* ScrollLock */
177
    SPECIAL,
176
    SPECIAL,
178
    SPECIAL, /* F10 */
177
    SPECIAL, /* F10 */
179
    SPECIAL,
178
    SPECIAL,
180
    SPECIAL, /* F12 */
179
    SPECIAL, /* F12 */
181
    SPECIAL,
180
    SPECIAL,
182
    SPECIAL, /* Pause */
181
    SPECIAL, /* Pause */
183
    SPECIAL, /* Insert */
182
    SPECIAL, /* Insert */
184
    SPECIAL, /* Home */
183
    SPECIAL, /* Home */
185
    SPECIAL, /* PageUp */
184
    SPECIAL, /* PageUp */
186
    SPECIAL, /* Delete */
185
    SPECIAL, /* Delete */
187
    SPECIAL, /* F4 */
186
    SPECIAL, /* F4 */
188
    SPECIAL, /* End */
187
    SPECIAL, /* End */
189
    SPECIAL, /* F2 */
188
    SPECIAL, /* F2 */
190
    SPECIAL, /* PageDown */
189
    SPECIAL, /* PageDown */
191
    SPECIAL  /* F1 */
190
    SPECIAL  /* F1 */
192
};
191
};
193
 
192
 
194
 
193
 
195
static void receive_packet(uint8_t *kind, index_t count, uint8_t data[])
194
static void receive_packet(uint8_t *kind, index_t count, uint8_t data[])
196
{
195
{
197
    cuda[B] = cuda[B] & ~TIP;
196
    cuda[B] = cuda[B] & ~TIP;
198
    *kind = cuda[SR];
197
    *kind = cuda[SR];
199
   
198
   
200
    index_t i;
199
    index_t i;
201
    for (i = 0; i < count; i++)
200
    for (i = 0; i < count; i++)
202
        data[i] = cuda[SR];
201
        data[i] = cuda[SR];
203
   
202
   
204
    cuda[B] = cuda[B] | TIP;
203
    cuda[B] = cuda[B] | TIP;
205
}
204
}
206
 
205
 
207
 
206
 
208
/* Called from getc(). */
207
/* Called from getc(). */
209
static void cuda_resume(chardev_t *d)
208
static void cuda_resume(chardev_t *d)
210
{
209
{
211
}
210
}
212
 
211
 
213
 
212
 
214
/* Called from getc(). */
213
/* Called from getc(). */
215
static void cuda_suspend(chardev_t *d)
214
static void cuda_suspend(chardev_t *d)
216
{
215
{
217
}
216
}
218
 
217
 
219
 
218
 
220
static char key_read(chardev_t *d)
219
static char key_read(chardev_t *d)
221
{
220
{
222
    char ch;
221
    char ch;
223
   
222
   
224
    ch = 0;
223
    ch = 0;
225
    return ch;
224
    return ch;
226
}
225
}
227
 
226
 
228
 
227
 
229
static chardev_t kbrd;
228
static chardev_t kbrd;
230
static chardev_operations_t ops = {
229
static chardev_operations_t ops = {
231
    .suspend = cuda_suspend,
230
    .suspend = cuda_suspend,
232
    .resume = cuda_resume,
231
    .resume = cuda_resume,
233
    .read = key_read
232
    .read = key_read
234
};
233
};
235
 
234
 
236
 
235
 
237
int cuda_get_scancode(void)
236
int cuda_get_scancode(void)
238
{
237
{
-
 
238
    if (cuda) {
239
    uint8_t kind;
239
        uint8_t kind;
240
    uint8_t data[4];
240
        uint8_t data[4];
241
   
241
       
242
    receive_packet(&kind, 4, data);
242
        receive_packet(&kind, 4, data);
243
   
243
       
244
    if ((kind == PACKET_ADB) && (data[0] == 0x40) && (data[1] == 0x2c))
244
        if ((kind == PACKET_ADB) && (data[0] == 0x40) && (data[1] == 0x2c))
245
        return data[2];
245
            return data[2];
-
 
246
    }
246
   
247
   
247
    return -1;
248
    return -1;
248
}
249
}
249
 
250
 
250
static void cuda_irq_handler(irq_t *irq, void *arg, ...)
251
static void cuda_irq_handler(irq_t *irq)
251
{
252
{
252
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
-
 
253
        ipc_irq_send_notif(irq);
-
 
254
    else {
-
 
255
        int scan_code = cuda_get_scancode();
253
    int scan_code = cuda_get_scancode();
256
       
254
       
257
        if (scan_code != -1) {
255
    if (scan_code != -1) {
258
            uint8_t scancode = (uint8_t) scan_code;
256
        uint8_t scancode = (uint8_t) scan_code;
259
            if ((scancode & 0x80) != 0x80)
257
        if ((scancode & 0x80) != 0x80)
260
                chardev_push_character(&kbrd, lchars[scancode & 0x7f]);
258
            chardev_push_character(&kbrd, lchars[scancode & 0x7f]);
261
        }
-
 
262
    }
259
    }
263
}
260
}
264
 
261
 
265
static irq_ownership_t cuda_claim(void)
262
static irq_ownership_t cuda_claim(irq_t *irq)
266
{
263
{
267
    return IRQ_ACCEPT;
264
    return IRQ_ACCEPT;
268
}
265
}
269
 
266
 
270
 
-
 
271
/** Initialize keyboard and service interrupts using kernel routine */
-
 
272
void cuda_grab(void)
-
 
273
{
-
 
274
    ipl_t ipl = interrupts_disable();
-
 
275
    spinlock_lock(&cuda_irq.lock);
-
 
276
    cuda_irq.notif_cfg.notify = false;
-
 
277
    spinlock_unlock(&cuda_irq.lock);
-
 
278
    interrupts_restore(ipl);
-
 
279
}
-
 
280
 
-
 
281
 
-
 
282
/** Resume the former interrupt vector */
-
 
283
void cuda_release(void)
-
 
284
{
-
 
285
    ipl_t ipl = interrupts_disable();
-
 
286
    spinlock_lock(&cuda_irq.lock);
-
 
287
    if (cuda_irq.notif_cfg.answerbox)
-
 
288
        cuda_irq.notif_cfg.notify = true;
-
 
289
    spinlock_unlock(&cuda_irq.unlock);
-
 
290
    interrupts_restore(ipl);
-
 
291
}
-
 
292
 
-
 
293
 
-
 
294
void cuda_init(devno_t devno, uintptr_t base, size_t size)
267
void cuda_init(devno_t devno, uintptr_t base, size_t size)
295
{
268
{
296
    cuda = (uint8_t *) hw_map(base, size); 
269
    cuda = (uint8_t *) hw_map(base, size);
297
   
270
   
298
    chardev_initialize("cuda_kbd", &kbrd, &ops);
271
    chardev_initialize("cuda_kbd", &kbrd, &ops);
299
    stdin = &kbrd;
272
    stdin = &kbrd;
300
   
273
   
301
    irq_initialize(&cuda_irq);
274
    irq_initialize(&cuda_irq);
302
    cuda_irq.devno = devno;
275
    cuda_irq.devno = devno;
303
    cuda_irq.inr = CUDA_IRQ;
276
    cuda_irq.inr = CUDA_IRQ;
304
    cuda_irq.claim = cuda_claim;
277
    cuda_irq.claim = cuda_claim;
305
    cuda_irq.handler = cuda_irq_handler;
278
    cuda_irq.handler = cuda_irq_handler;
306
    irq_register(&cuda_irq);
279
    irq_register(&cuda_irq);
307
   
280
   
308
    pic_enable_interrupt(CUDA_IRQ);
281
    pic_enable_interrupt(CUDA_IRQ);
309
 
282
   
310
    sysinfo_set_item_val("kbd", NULL, true);
283
    sysinfo_set_item_val("kbd", NULL, true);
311
    sysinfo_set_item_val("kbd.devno", NULL, devno);
284
    sysinfo_set_item_val("kbd.devno", NULL, devno);
312
    sysinfo_set_item_val("kbd.inr", NULL, CUDA_IRQ);
285
    sysinfo_set_item_val("kbd.inr", NULL, CUDA_IRQ);
313
    sysinfo_set_item_val("kbd.address.virtual", NULL, base);
286
    sysinfo_set_item_val("kbd.address.virtual", NULL, base);
314
}
287
}
315
 
288
 
316
 
289
 
317
static void send_packet(const uint8_t kind, count_t count, ...)
290
static void send_packet(const uint8_t kind, count_t count, ...)
318
{
291
{
319
    index_t i;
292
    index_t i;
320
    va_list va;
293
    va_list va;
321
   
294
   
322
    cuda[B] = cuda[B] | TIP;
295
    cuda[B] = cuda[B] | TIP;
323
    cuda[ACR] = cuda[ACR] | SR_OUT;
296
    cuda[ACR] = cuda[ACR] | SR_OUT;
324
    cuda[SR] = kind;
297
    cuda[SR] = kind;
325
    cuda[B] = cuda[B] & ~TIP;
298
    cuda[B] = cuda[B] & ~TIP;
326
   
299
   
327
    va_start(va, count);
300
    va_start(va, count);
328
   
301
   
329
    for (i = 0; i < count; i++) {
302
    for (i = 0; i < count; i++) {
330
        cuda[ACR] = cuda[ACR] | SR_OUT;
303
        cuda[ACR] = cuda[ACR] | SR_OUT;
331
        cuda[SR] = va_arg(va, int);
304
        cuda[SR] = va_arg(va, int);
332
        cuda[B] = cuda[B] | TACK;
305
        cuda[B] = cuda[B] | TACK;
333
    }
306
    }
334
   
307
   
335
    va_end(va);
308
    va_end(va);
336
   
309
   
337
    cuda[B] = cuda[B] | TIP;
310
    cuda[B] = cuda[B] | TIP;
338
}
311
}
339
 
312
 
340
 
313
 
341
void cpu_halt(void) {
314
void cpu_halt(void) {
342
    asm volatile (
315
    asm volatile (
343
        "b 0\n"
316
        "b 0\n"
344
    );
317
    );
345
}
318
}
346
 
319
 
347
void arch_reboot(void) {
320
void arch_reboot(void) {
-
 
321
    if (cuda)
348
    send_packet(PACKET_CUDA, 1, CUDA_RESET);
322
        send_packet(PACKET_CUDA, 1, CUDA_RESET);
-
 
323
   
349
    asm volatile (
324
    asm volatile (
350
        "b 0\n"
325
        "b 0\n"
351
    );
326
    );
352
}
327
}
353
 
328
 
354
/** @}
329
/** @}
355
 */
330
 */
356
 
331