Subversion Repositories HelenOS

Rev

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

Rev 1944 Rev 2071
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 <ddi/irq.h>
36
#include <ddi/irq.h>
37
#include <arch/asm.h>
37
#include <arch/asm.h>
38
#include <console/console.h>
38
#include <console/console.h>
39
#include <console/chardev.h>
39
#include <console/chardev.h>
40
#include <arch/drivers/pic.h>
40
#include <arch/drivers/pic.h>
41
#include <sysinfo/sysinfo.h>
41
#include <sysinfo/sysinfo.h>
42
#include <interrupt.h>
42
#include <interrupt.h>
43
#include <stdarg.h>
43
#include <stdarg.h>
44
 
44
 
45
#define CUDA_IRQ 10
45
#define CUDA_IRQ 10
46
#define SPECIAL     '?'
46
#define SPECIAL     '?'
47
 
47
 
48
#define PACKET_ADB  0x00
48
#define PACKET_ADB  0x00
49
#define PACKET_CUDA 0x01
49
#define PACKET_CUDA 0x01
50
 
50
 
51
#define CUDA_POWERDOWN 0x0a
51
#define CUDA_POWERDOWN 0x0a
52
 
52
 
53
#define RS 0x200
53
#define RS 0x200
54
#define B (0 * RS)
54
#define B (0 * RS)
55
#define A (1 * RS)
55
#define A (1 * RS)
56
#define SR (10 * RS)
56
#define SR (10 * RS)
57
#define ACR (11 * RS)
57
#define ACR (11 * RS)
58
 
58
 
59
#define SR_OUT 0x10
59
#define SR_OUT 0x10
60
#define TACK 0x10
60
#define TACK 0x10
61
#define TIP 0x20
61
#define TIP 0x20
62
 
62
 
63
 
63
 
64
static volatile uint8_t *cuda = NULL;
64
static volatile uint8_t *cuda = NULL;
65
static irq_t cuda_irq;      /**< Cuda's IRQ. */
65
static irq_t cuda_irq;      /**< Cuda's IRQ. */
66
 
66
 
67
static char lchars[0x80] = {
67
static char lchars[0x80] = {
68
    'a',
68
    'a',
69
    's',
69
    's',
70
    'd',
70
    'd',
71
    'f',
71
    'f',
72
    'h',
72
    'h',
73
    'g',
73
    'g',
74
    'z',
74
    'z',
75
    'x',
75
    'x',
76
    'c',
76
    'c',
77
    'v',
77
    'v',
78
    SPECIAL,
78
    SPECIAL,
79
    'b',
79
    'b',
80
    'q',
80
    'q',
81
    'w',
81
    'w',
82
    'e',
82
    'e',
83
    'r',
83
    'r',
84
    'y',
84
    'y',
85
    't',
85
    't',
86
    '1',
86
    '1',
87
    '2',
87
    '2',
88
    '3',
88
    '3',
89
    '4',
89
    '4',
90
    '6',
90
    '6',
91
    '5',
91
    '5',
92
    '=',
92
    '=',
93
    '9',
93
    '9',
94
    '7',
94
    '7',
95
    '-',
95
    '-',
96
    '8',
96
    '8',
97
    '0',
97
    '0',
98
    ']',
98
    ']',
99
    'o',
99
    'o',
100
    'u',
100
    'u',
101
    '[',
101
    '[',
102
    'i',
102
    'i',
103
    'p',
103
    'p',
104
    '\n',    /* Enter */
104
    '\n',    /* Enter */
105
    'l',
105
    'l',
106
    'j',
106
    'j',
107
    '\'',
107
    '\'',
108
    'k',
108
    'k',
109
    ';',
109
    ';',
110
    '\\',
110
    '\\',
111
    ',',
111
    ',',
112
    '/',
112
    '/',
113
    'n',
113
    'n',
114
    'm',
114
    'm',
115
    '.',
115
    '.',
116
    '\t',    /* Tab */
116
    '\t',    /* Tab */
117
    ' ',
117
    ' ',
118
    '`',
118
    '`',
119
    '\b',    /* Backspace */
119
    '\b',    /* Backspace */
120
    SPECIAL,
120
    SPECIAL,
121
    SPECIAL, /* Escape */
121
    SPECIAL, /* Escape */
122
    SPECIAL, /* Ctrl */
122
    SPECIAL, /* Ctrl */
123
    SPECIAL, /* Alt */
123
    SPECIAL, /* Alt */
124
    SPECIAL, /* Shift */
124
    SPECIAL, /* Shift */
125
    SPECIAL, /* Caps-Lock */
125
    SPECIAL, /* Caps-Lock */
126
    SPECIAL, /* RAlt */
126
    SPECIAL, /* RAlt */
127
    SPECIAL, /* Left */
127
    SPECIAL, /* Left */
128
    SPECIAL, /* Right */
128
    SPECIAL, /* Right */
129
    SPECIAL, /* Down */
129
    SPECIAL, /* Down */
130
    SPECIAL, /* Up */
130
    SPECIAL, /* Up */
131
    SPECIAL,
131
    SPECIAL,
132
    SPECIAL,
132
    SPECIAL,
133
    '.',     /* Keypad . */
133
    '.',     /* Keypad . */
134
    SPECIAL,
134
    SPECIAL,
135
    '*',     /* Keypad * */
135
    '*',     /* Keypad * */
136
    SPECIAL,
136
    SPECIAL,
137
    '+',     /* Keypad + */
137
    '+',     /* Keypad + */
138
    SPECIAL,
138
    SPECIAL,
139
    SPECIAL, /* NumLock */
139
    SPECIAL, /* NumLock */
140
    SPECIAL,
140
    SPECIAL,
141
    SPECIAL,
141
    SPECIAL,
142
    SPECIAL,
142
    SPECIAL,
143
    '/',     /* Keypad / */
143
    '/',     /* Keypad / */
144
    '\n',    /* Keypad Enter */
144
    '\n',    /* Keypad Enter */
145
    SPECIAL,
145
    SPECIAL,
146
    '-',     /* Keypad - */
146
    '-',     /* Keypad - */
147
    SPECIAL,
147
    SPECIAL,
148
    SPECIAL,
148
    SPECIAL,
149
    SPECIAL,
149
    SPECIAL,
150
    '0',     /* Keypad 0 */
150
    '0',     /* Keypad 0 */
151
    '1',     /* Keypad 1 */
151
    '1',     /* Keypad 1 */
152
    '2',     /* Keypad 2 */
152
    '2',     /* Keypad 2 */
153
    '3',     /* Keypad 3 */
153
    '3',     /* Keypad 3 */
154
    '4',     /* Keypad 4 */
154
    '4',     /* Keypad 4 */
155
    '5',     /* Keypad 5 */
155
    '5',     /* Keypad 5 */
156
    '6',     /* Keypad 6 */
156
    '6',     /* Keypad 6 */
157
    '7',     /* Keypad 7 */
157
    '7',     /* Keypad 7 */
158
    SPECIAL,
158
    SPECIAL,
159
    '8',     /* Keypad 8 */
159
    '8',     /* Keypad 8 */
160
    '9',     /* Keypad 9 */
160
    '9',     /* Keypad 9 */
161
    SPECIAL,
161
    SPECIAL,
162
    SPECIAL,
162
    SPECIAL,
163
    SPECIAL,
163
    SPECIAL,
164
    SPECIAL, /* F5 */
164
    SPECIAL, /* F5 */
165
    SPECIAL, /* F6 */
165
    SPECIAL, /* F6 */
166
    SPECIAL, /* F7 */
166
    SPECIAL, /* F7 */
167
    SPECIAL, /* F3 */
167
    SPECIAL, /* F3 */
168
    SPECIAL, /* F8 */
168
    SPECIAL, /* F8 */
169
    SPECIAL, /* F9 */
169
    SPECIAL, /* F9 */
170
    SPECIAL,
170
    SPECIAL,
171
    SPECIAL, /* F11 */
171
    SPECIAL, /* F11 */
172
    SPECIAL,
172
    SPECIAL,
173
    SPECIAL, /* F13 */
173
    SPECIAL, /* F13 */
174
    SPECIAL,
174
    SPECIAL,
175
    SPECIAL, /* ScrollLock */
175
    SPECIAL, /* ScrollLock */
176
    SPECIAL,
176
    SPECIAL,
177
    SPECIAL, /* F10 */
177
    SPECIAL, /* F10 */
178
    SPECIAL,
178
    SPECIAL,
179
    SPECIAL, /* F12 */
179
    SPECIAL, /* F12 */
180
    SPECIAL,
180
    SPECIAL,
181
    SPECIAL, /* Pause */
181
    SPECIAL, /* Pause */
182
    SPECIAL, /* Insert */
182
    SPECIAL, /* Insert */
183
    SPECIAL, /* Home */
183
    SPECIAL, /* Home */
184
    SPECIAL, /* PageUp */
184
    SPECIAL, /* PageUp */
185
    SPECIAL, /* Delete */
185
    SPECIAL, /* Delete */
186
    SPECIAL, /* F4 */
186
    SPECIAL, /* F4 */
187
    SPECIAL, /* End */
187
    SPECIAL, /* End */
188
    SPECIAL, /* F2 */
188
    SPECIAL, /* F2 */
189
    SPECIAL, /* PageDown */
189
    SPECIAL, /* PageDown */
190
    SPECIAL  /* F1 */
190
    SPECIAL  /* F1 */
191
};
191
};
192
 
192
 
193
 
193
 
194
void send_packet(const uint8_t kind, index_t count, ...);
194
void send_packet(const uint8_t kind, index_t count, ...);
195
 
195
 
196
 
196
 
197
static void receive_packet(uint8_t *kind, index_t count, uint8_t data[])
197
static void receive_packet(uint8_t *kind, index_t count, uint8_t data[])
198
{
198
{
199
    cuda[B] = cuda[B] & ~TIP;
199
    cuda[B] = cuda[B] & ~TIP;
200
    *kind = cuda[SR];
200
    *kind = cuda[SR];
201
   
201
   
202
    index_t i;
202
    index_t i;
203
    for (i = 0; i < count; i++)
203
    for (i = 0; i < count; i++)
204
        data[i] = cuda[SR];
204
        data[i] = cuda[SR];
205
   
205
   
206
    cuda[B] = cuda[B] | TIP;
206
    cuda[B] = cuda[B] | TIP;
207
}
207
}
208
 
208
 
209
 
209
 
210
/* Called from getc(). */
210
/* Called from getc(). */
211
static void cuda_resume(chardev_t *d)
211
static void cuda_resume(chardev_t *d)
212
{
212
{
213
}
213
}
214
 
214
 
215
 
215
 
216
/* Called from getc(). */
216
/* Called from getc(). */
217
static void cuda_suspend(chardev_t *d)
217
static void cuda_suspend(chardev_t *d)
218
{
218
{
219
}
219
}
220
 
220
 
221
 
221
 
222
static char key_read(chardev_t *d)
222
static char key_read(chardev_t *d)
223
{
223
{
224
    char ch;
224
    char ch;
225
   
225
   
226
    ch = 0;
226
    ch = 0;
227
    return ch;
227
    return ch;
228
}
228
}
229
 
229
 
230
 
230
 
231
static chardev_t kbrd;
231
static chardev_t kbrd;
232
static chardev_operations_t ops = {
232
static chardev_operations_t ops = {
233
    .suspend = cuda_suspend,
233
    .suspend = cuda_suspend,
234
    .resume = cuda_resume,
234
    .resume = cuda_resume,
235
    .read = key_read
235
    .read = key_read
236
};
236
};
237
 
237
 
238
 
238
 
239
int cuda_get_scancode(void)
239
int cuda_get_scancode(void)
240
{
240
{
241
    uint8_t kind;
241
    uint8_t kind;
242
    uint8_t data[4];
242
    uint8_t data[4];
243
   
243
   
244
    receive_packet(&kind, 4, data);
244
    receive_packet(&kind, 4, data);
245
   
245
   
246
    if ((kind == PACKET_ADB) && (data[0] == 0x40) && (data[1] == 0x2c))
246
    if ((kind == PACKET_ADB) && (data[0] == 0x40) && (data[1] == 0x2c))
247
        return data[2];
247
        return data[2];
248
   
248
   
249
    return -1;
249
    return -1;
250
}
250
}
251
 
251
 
252
static void cuda_irq_handler(irq_t *irq, void *arg, ...)
252
static void cuda_irq_handler(irq_t *irq, void *arg, ...)
253
{
253
{
254
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
254
    if ((irq->notif_cfg.notify) && (irq->notif_cfg.answerbox))
255
        ipc_irq_send_notif(irq);
255
        ipc_irq_send_notif(irq);
256
    else {
256
    else {
257
        int scan_code = cuda_get_scancode();
257
        int scan_code = cuda_get_scancode();
258
       
258
       
259
        if (scan_code != -1) {
259
        if (scan_code != -1) {
260
            uint8_t scancode = (uint8_t) scan_code;
260
            uint8_t scancode = (uint8_t) scan_code;
261
            if ((scancode & 0x80) != 0x80)
261
            if ((scancode & 0x80) != 0x80)
262
                chardev_push_character(&kbrd, lchars[scancode & 0x7f]);
262
                chardev_push_character(&kbrd, lchars[scancode & 0x7f]);
263
        }
263
        }
264
    }
264
    }
265
}
265
}
266
 
266
 
267
static irq_ownership_t cuda_claim(void)
267
static irq_ownership_t cuda_claim(void)
268
{
268
{
269
    return IRQ_ACCEPT;
269
    return IRQ_ACCEPT;
270
}
270
}
271
 
271
 
272
 
272
 
273
/** Initialize keyboard and service interrupts using kernel routine */
273
/** Initialize keyboard and service interrupts using kernel routine */
274
void cuda_grab(void)
274
void cuda_grab(void)
275
{
275
{
276
    ipl_t ipl = interrupts_disable();
276
    ipl_t ipl = interrupts_disable();
277
    spinlock_lock(&cuda_irq.lock);
277
    spinlock_lock(&cuda_irq.lock);
278
    cuda_irq.notif_cfg.notify = false;
278
    cuda_irq.notif_cfg.notify = false;
279
    spinlock_unlock(&cuda_irq.lock);
279
    spinlock_unlock(&cuda_irq.lock);
280
    interrupts_restore(ipl);
280
    interrupts_restore(ipl);
281
}
281
}
282
 
282
 
283
 
283
 
284
/** Resume the former interrupt vector */
284
/** Resume the former interrupt vector */
285
void cuda_release(void)
285
void cuda_release(void)
286
{
286
{
287
    ipl_t ipl = interrupts_disable();
287
    ipl_t ipl = interrupts_disable();
288
    spinlock_lock(&cuda_irq.lock);
288
    spinlock_lock(&cuda_irq.lock);
289
    if (cuda_irq.notif_cfg.answerbox)
289
    if (cuda_irq.notif_cfg.answerbox)
290
        cuda_irq.notif_cfg.notify = true;
290
        cuda_irq.notif_cfg.notify = true;
291
    spinlock_unlock(&cuda_irq.unlock);
291
    spinlock_unlock(&cuda_irq.unlock);
292
    interrupts_restore(ipl);
292
    interrupts_restore(ipl);
293
}
293
}
294
 
294
 
295
 
295
 
296
void cuda_init(devno_t devno, uintptr_t base, size_t size)
296
void cuda_init(devno_t devno, uintptr_t base, size_t size)
297
{
297
{
298
    cuda = (uint8_t *) hw_map(base, size); 
298
    cuda = (uint8_t *) hw_map(base, size); 
299
   
299
   
300
    chardev_initialize("cuda_kbd", &kbrd, &ops);
300
    chardev_initialize("cuda_kbd", &kbrd, &ops);
301
    stdin = &kbrd;
301
    stdin = &kbrd;
302
   
302
   
303
    irq_initialize(&cuda_irq);
303
    irq_initialize(&cuda_irq);
304
    cuda_irq.devno = devno;
304
    cuda_irq.devno = devno;
305
    cuda_irq.inr = CUDA_IRQ;
305
    cuda_irq.inr = CUDA_IRQ;
306
    cuda_irq.claim = cuda_claim;
306
    cuda_irq.claim = cuda_claim;
307
    cuda_irq.handler = cuda_irq_handler;
307
    cuda_irq.handler = cuda_irq_handler;
308
    irq_register(&cuda_irq);
308
    irq_register(&cuda_irq);
309
   
309
   
310
    pic_enable_interrupt(CUDA_IRQ);
310
    pic_enable_interrupt(CUDA_IRQ);
311
 
311
 
312
    sysinfo_set_item_val("kbd", NULL, true);
312
    sysinfo_set_item_val("kbd", NULL, true);
313
    sysinfo_set_item_val("kbd.devno", NULL, devno);
313
    sysinfo_set_item_val("kbd.devno", NULL, devno);
314
    sysinfo_set_item_val("kbd.inr", NULL, CUDA_IRQ);
314
    sysinfo_set_item_val("kbd.inr", NULL, CUDA_IRQ);
315
    sysinfo_set_item_val("kbd.address.virtual", NULL, base);
315
    sysinfo_set_item_val("kbd.address.virtual", NULL, base);
316
}
316
}
317
 
317
 
318
 
318
 
319
void send_packet(const uint8_t kind, index_t count, ...)
319
void send_packet(const uint8_t kind, index_t count, ...)
320
{
320
{
321
    index_t i;
321
    index_t i;
322
    va_list va;
322
    va_list va;
323
   
323
   
324
    cuda[B] = cuda[B] | TIP;
324
    cuda[B] = cuda[B] | TIP;
325
    cuda[ACR] = cuda[ACR] | SR_OUT;
325
    cuda[ACR] = cuda[ACR] | SR_OUT;
326
    cuda[SR] = kind;
326
    cuda[SR] = kind;
327
    cuda[B] = cuda[B] & ~TIP;
327
    cuda[B] = cuda[B] & ~TIP;
328
   
328
   
329
    va_start(va, count);
329
    va_start(va, count);
330
   
330
   
331
    for (i = 0; i < count; i++) {
331
    for (i = 0; i < count; i++) {
332
        cuda[ACR] = cuda[ACR] | SR_OUT;
332
        cuda[ACR] = cuda[ACR] | SR_OUT;
333
        cuda[SR] = va_arg(va, int);
333
        cuda[SR] = va_arg(va, int);
334
        cuda[B] = cuda[B] | TACK;
334
        cuda[B] = cuda[B] | TACK;
335
    }
335
    }
336
   
336
   
337
    va_end(va);
337
    va_end(va);
338
   
338
   
339
    cuda[B] = cuda[B] | TIP;
339
    cuda[B] = cuda[B] | TIP;
340
}
340
}
341
 
341
 
342
 
342
 
343
void cpu_halt(void) {
343
void cpu_halt(void) {
344
#ifdef CONFIG_POWEROFF
344
#ifdef CONFIG_POWEROFF
345
    send_packet(PACKET_CUDA, 1, CUDA_POWERDOWN);
345
    send_packet(PACKET_CUDA, 1, CUDA_POWERDOWN);
346
#endif
346
#endif
347
    asm volatile (
347
    asm volatile (
348
        "b 0\n"
348
        "b 0\n"
349
    );
349
    );
350
}
350
}
351
 
351
 
352
/** @}
352
/** @}
353
 */
353
 */
354
 
354